Type: use class IntrusivePtr in TypeList

This commit is contained in:
Max Kellermann 2020-03-03 17:01:21 +01:00
parent de0289125b
commit 73cea5dcad
13 changed files with 62 additions and 74 deletions

View file

@ -173,9 +173,9 @@ void DNS_Mgr_mapping_delete_func(void* v)
static TableVal* empty_addr_set()
{
BroType* addr_t = base_type(TYPE_ADDR);
IntrusivePtr<BroType> addr_t{AdoptRef{}, base_type(TYPE_ADDR)};
auto set_index = make_intrusive<TypeList>(addr_t);
set_index->Append(addr_t);
set_index->Append(std::move(addr_t));
auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
return new TableVal(std::move(s));
}

View file

@ -3109,7 +3109,7 @@ TableConstructorExpr::TableConstructorExpr(IntrusivePtr<ListExpr> constructor_li
else
{
if ( op->AsListExpr()->Exprs().empty() )
SetType(make_intrusive<TableType>(make_intrusive<TypeList>(base_type(TYPE_ANY)), nullptr));
SetType(make_intrusive<TableType>(make_intrusive<TypeList>(IntrusivePtr{AdoptRef{}, base_type(TYPE_ANY)}), nullptr));
else
{
SetType({AdoptRef{}, init_type(op.get())});
@ -3223,7 +3223,7 @@ SetConstructorExpr::SetConstructorExpr(IntrusivePtr<ListExpr> constructor_list,
else
{
if ( op->AsListExpr()->Exprs().empty() )
SetType(make_intrusive<::SetType>(make_intrusive<TypeList>(base_type(TYPE_ANY)), nullptr));
SetType(make_intrusive<::SetType>(make_intrusive<TypeList>(IntrusivePtr{AdoptRef{}, base_type(TYPE_ANY)}), nullptr));
else
SetType({AdoptRef{}, init_type(op.get())});
}
@ -3841,7 +3841,7 @@ FlattenExpr::FlattenExpr(IntrusivePtr<Expr> arg_op)
auto tl = make_intrusive<TypeList>();
for ( int i = 0; i < num_fields; ++i )
tl->Append(rt->FieldType(i)->Ref());
tl->Append({NewRef{}, rt->FieldType(i)});
Unref(rt);
SetType(std::move(tl));
@ -4481,7 +4481,7 @@ ListExpr::~ListExpr()
void ListExpr::Append(IntrusivePtr<Expr> e)
{
exprs.push_back(e.release());
((TypeList*) type.get())->Append(exprs.back()->Type()->Ref());
((TypeList*) type.get())->Append({NewRef{}, exprs.back()->Type()});
}
bool ListExpr::IsPure() const
@ -4568,12 +4568,12 @@ IntrusivePtr<BroType> ListExpr::InitType() const
if ( ! til->IsPure() ||
! til->AllMatch(til->PureType(), 1) )
tl->Append(til->Ref());
tl->Append({NewRef{}, til});
else
tl->Append(til->PureType()->Ref());
tl->Append({NewRef{}, til->PureType()});
}
else
tl->Append(ti->Ref());
tl->Append({NewRef{}, ti});
}
return tl;
@ -5121,7 +5121,7 @@ int check_and_promote_args(ListExpr* const args, RecordType* types)
TypeList* tl = new TypeList();
for ( int i = 0; i < types->NumFields(); ++i )
tl->Append(types->FieldType(i)->Ref());
tl->Append({NewRef{}, types->FieldType(i)});
int rval = check_and_promote_exprs(args, tl);
Unref(tl);

View file

@ -793,8 +793,8 @@ bool BloomFilterVal::Typify(BroType* arg_type)
type = arg_type;
type->Ref();
auto tl = make_intrusive<TypeList>(type);
tl->Append(type->Ref());
auto tl = make_intrusive<TypeList>(IntrusivePtr{NewRef{}, type});
tl->Append({NewRef{}, type});
hash = new CompositeHash(std::move(tl));
return true;
@ -963,8 +963,8 @@ bool CardinalityVal::Typify(BroType* arg_type)
type = arg_type;
type->Ref();
auto tl = make_intrusive<TypeList>(type);
tl->Append(type->Ref());
auto tl = make_intrusive<TypeList>(IntrusivePtr{NewRef{}, type});
tl->Append({NewRef{}, type});
hash = new CompositeHash(std::move(tl));
return true;

View file

@ -145,8 +145,8 @@ RuleConditionEval::RuleConditionEval(const char* func)
rules_error("eval function type must yield a 'bool'", func);
TypeList tl;
tl.Append(internal_type("signature_state")->Ref());
tl.Append(base_type(TYPE_STRING));
tl.Append({NewRef{}, internal_type("signature_state")});
tl.Append({AdoptRef{}, base_type(TYPE_STRING)});
if ( ! f->CheckArgs(tl.Types()) )
rules_error("eval function parameters must be a 'signature_state' "

View file

@ -587,7 +587,7 @@ static void int_del_func(void* v)
void SwitchStmt::Init()
{
auto t = make_intrusive<TypeList>();
t->Append(e->Type()->Ref());
t->Append({NewRef{}, e->Type()});
comp_hash = new CompositeHash(std::move(t));
case_label_value_map.SetDeleteFunc(int_del_func);

View file

@ -155,8 +155,6 @@ TypeList::~TypeList()
{
for ( const auto& type : types )
Unref(type);
Unref(pure_type);
}
int TypeList::AllMatch(const BroType* t, int is_init) const
@ -167,23 +165,20 @@ int TypeList::AllMatch(const BroType* t, int is_init) const
return 1;
}
void TypeList::Append(BroType* t)
void TypeList::Append(IntrusivePtr<BroType> t)
{
if ( pure_type && ! same_type(t, pure_type) )
if ( pure_type && ! same_type(t.get(), pure_type.get()) )
reporter->InternalError("pure type-list violation");
types.push_back(t);
types.push_back(t.release());
}
void TypeList::AppendEvenIfNotPure(BroType* t)
void TypeList::AppendEvenIfNotPure(IntrusivePtr<BroType> t)
{
if ( pure_type && ! same_type(t, pure_type) )
{
Unref(pure_type);
if ( pure_type && ! same_type(t.get(), pure_type.get()) )
pure_type = 0;
}
types.push_back(t);
types.push_back(t.release());
}
void TypeList::Describe(ODesc* d) const
@ -368,7 +363,7 @@ TypeList* TableType::ExpandRecordIndex(RecordType* rt) const
for ( int i = 0; i < n; ++i )
{
TypeDecl* td = rt->FieldDecl(i);
tl->Append(td->type->Ref());
tl->Append({NewRef{}, td->type});
}
return tl;
@ -396,21 +391,17 @@ SetType::SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements
else if ( tl->length() == 1 )
{
BroType* t = flatten_type((*tl)[0]->Ref());
IntrusivePtr<BroType> t{AdoptRef{}, flatten_type((*tl)[0]->Ref())};
indices = make_intrusive<TypeList>(t);
indices->Append(t->Ref());
indices->Append(std::move(t));
}
else
{
BroType* t = merge_types((*tl)[0], (*tl)[1]);
IntrusivePtr<BroType> t{AdoptRef{}, merge_types((*tl)[0], (*tl)[1])};
for ( int i = 2; t && i < tl->length(); ++i )
{
BroType* t_new = merge_types(t, (*tl)[i]);
Unref(t);
t = t_new;
}
t = {AdoptRef{}, merge_types(t.get(), (*tl)[i])};
if ( ! t )
{
@ -419,7 +410,7 @@ SetType::SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements
}
indices = make_intrusive<TypeList>(t);
indices->Append(t);
indices->Append(std::move(t));
}
}
}
@ -457,7 +448,7 @@ FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg
args->Error(err_str);
}
arg_types->Append(args->FieldType(i)->Ref());
arg_types->Append({NewRef{}, args->FieldType(i)});
}
}
@ -1832,11 +1823,11 @@ BroType* merge_types(const BroType* t1, const BroType* t2)
loop_over_list(*tl1, i)
{
BroType* tl3_i = merge_types((*tl1)[i], (*tl2)[i]);
IntrusivePtr<BroType> tl3_i{AdoptRef{}, merge_types((*tl1)[i], (*tl2)[i])};
if ( ! tl3_i )
return 0;
tl3->Append(tl3_i);
tl3->Append(std::move(tl3_i));
}
}
@ -1952,7 +1943,7 @@ BroType* merge_types(const BroType* t1, const BroType* t2)
TypeList* tl3 = new TypeList();
loop_over_list(*l1, i)
tl3->Append(merge_types((*l1)[i], (*l2)[i]));
tl3->Append({AdoptRef{}, merge_types((*l1)[i], (*l2)[i])});
return tl3;
}
@ -2105,8 +2096,8 @@ BroType* init_type(Expr* init)
// it one, as that's what's required for creating a set type.
if ( t->Tag() != TYPE_LIST )
{
auto tl = make_intrusive<TypeList>(t.get()->Ref());
tl->Append(t.release());
auto tl = make_intrusive<TypeList>(t);
tl->Append(std::move(t));
t = std::move(tl);
}

View file

@ -347,11 +347,8 @@ private:
class TypeList : public BroType {
public:
explicit TypeList(BroType* arg_pure_type = 0) : BroType(TYPE_LIST)
explicit TypeList(IntrusivePtr<BroType> arg_pure_type = nullptr) : BroType(TYPE_LIST), pure_type(std::move(arg_pure_type))
{
pure_type = arg_pure_type;
if ( pure_type )
pure_type->Ref();
}
~TypeList() override;
@ -362,23 +359,23 @@ public:
// Returns the underlying pure type, or nil if the list
// is not pure or is empty.
BroType* PureType() { return pure_type; }
const BroType* PureType() const { return pure_type; }
BroType* PureType() { return pure_type.get(); }
const BroType* PureType() const { return pure_type.get(); }
// True if all of the types match t, false otherwise. If
// is_init is true, then the matching is done in the context
// of an initialization.
int AllMatch(const BroType* t, int is_init) const;
void Append(BroType* t);
void AppendEvenIfNotPure(BroType* t);
void Append(IntrusivePtr<BroType> t);
void AppendEvenIfNotPure(IntrusivePtr<BroType> t);
void Describe(ODesc* d) const override;
unsigned int MemoryAllocation() const override;
protected:
BroType* pure_type;
IntrusivePtr<BroType> pure_type;
type_list types;
};

View file

@ -1185,7 +1185,7 @@ IntrusivePtr<Val> PatternVal::DoClone(CloneState* state)
}
ListVal::ListVal(TypeTag t)
: Val(new TypeList(t == TYPE_ANY ? 0 : base_type_no_ref(t)))
: Val(new TypeList({NewRef{}, t == TYPE_ANY ? 0 : base_type_no_ref(t)}))
{
tag = t;
}
@ -1226,7 +1226,7 @@ void ListVal::Append(Val* v)
}
vals.push_back(v);
type->AsTypeList()->Append(v->Type()->Ref());
type->AsTypeList()->Append({NewRef{}, v->Type()});
}
TableVal* ListVal::ConvertToSet() const
@ -1234,8 +1234,8 @@ TableVal* ListVal::ConvertToSet() const
if ( tag == TYPE_ANY )
Internal("conversion of heterogeneous list to set");
auto set_index = make_intrusive<TypeList>(type->AsTypeList()->PureType());
set_index->Append(base_type(tag));
auto set_index = make_intrusive<TypeList>(IntrusivePtr{NewRef{}, type->AsTypeList()->PureType()});
set_index->Append({AdoptRef{}, base_type(tag)});
auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
TableVal* t = new TableVal(std::move(s));

View file

@ -21,8 +21,8 @@ static void analyzer_del_func(void* v)
AnalyzerSet::AnalyzerSet(File* arg_file) : file(arg_file)
{
auto t = make_intrusive<TypeList>();
t->Append(file_mgr->GetTagEnumType()->Ref());
t->Append(BifType::Record::Files::AnalyzerArgs->Ref());
t->Append({NewRef{}, file_mgr->GetTagEnumType()});
t->Append({NewRef{}, BifType::Record::Files::AnalyzerArgs});
analyzer_hash = new CompositeHash(std::move(t));
analyzer_map.SetDeleteFunc(analyzer_del_func);
}

View file

@ -23,8 +23,8 @@ using namespace file_analysis;
static Val* empty_connection_table()
{
auto tbl_index = make_intrusive<TypeList>(conn_id);
tbl_index->Append(conn_id->Ref());
auto tbl_index = make_intrusive<TypeList>(IntrusivePtr{NewRef{}, conn_id});
tbl_index->Append({NewRef{}, conn_id});
auto tbl_type = make_intrusive<TableType>(std::move(tbl_index), IntrusivePtr{NewRef{}, connection_type});
return new TableVal(std::move(tbl_type));
}

View file

@ -2377,8 +2377,8 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, BroType* request_typ
{
// all entries have to have the same type...
BroType* type = request_type->AsTableType()->Indices()->PureType();
auto set_index = make_intrusive<TypeList>(type->Ref());
set_index->Append(type->Ref());
auto set_index = make_intrusive<TypeList>(IntrusivePtr{NewRef{}, type});
set_index->Append({NewRef{}, type});
auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
TableVal* t = new TableVal(std::move(s));
for ( int j = 0; j < val->val.set_val.size; j++ )
@ -2534,7 +2534,7 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co
if ( stag == TYPE_VOID )
TypeTag stag = val->val.set_val.vals[0]->type;
BroType* index_type;
IntrusivePtr<BroType> index_type;
if ( stag == TYPE_ENUM )
{
@ -2552,13 +2552,13 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co
return nullptr;
}
index_type = enum_id->Type()->AsEnumType();
index_type = {NewRef{}, enum_id->Type()->AsEnumType()};
}
else
index_type = base_type_no_ref(stag);
index_type = {NewRef{}, base_type_no_ref(stag)};
set_index = make_intrusive<TypeList>(index_type);
set_index->Append(index_type->Ref());
set_index->Append(std::move(index_type));
}
auto s = make_intrusive<SetType>(std::move(set_index), nullptr);

View file

@ -1027,11 +1027,11 @@ type:
type_list:
type_list ',' type
{ $1->AppendEvenIfNotPure($3); }
{ $1->AppendEvenIfNotPure({AdoptRef{}, $3}); }
| type
{
$$ = new TypeList($1);
$$->Append($1);
$$ = new TypeList({NewRef{}, $1});
$$->Append({AdoptRef{}, $1});
}
;

View file

@ -27,8 +27,8 @@ void TopkVal::Typify(BroType* t)
{
assert(!hash && !type);
type = t->Ref();
auto tl = make_intrusive<TypeList>(t);
tl->Append(t->Ref());
auto tl = make_intrusive<TypeList>(IntrusivePtr{NewRef{}, t});
tl->Append({NewRef{}, t});
hash = new CompositeHash(std::move(tl));
}
@ -199,8 +199,8 @@ VectorVal* TopkVal::GetTopK(int k) const // returns vector
return 0;
}
TypeList* vector_index = new TypeList(type);
vector_index->Append(type->Ref());
TypeList* vector_index = new TypeList({NewRef{}, type});
vector_index->Append({NewRef{}, type});
VectorType* v = new VectorType(vector_index);
VectorVal* t = new VectorVal(v);