Type: use class IntrusivePtr in TypeDecl

This commit is contained in:
Max Kellermann 2020-03-03 16:55:24 +01:00
parent 838bba5206
commit 43af5f8349
8 changed files with 32 additions and 38 deletions

View file

@ -182,7 +182,7 @@ char* CompositeHash::SingleValHash(int type_check, char* kp0,
{ {
auto rv_i = rv->Lookup(i); auto rv_i = rv->Lookup(i);
Attributes* a = rt->FieldDecl(i)->attrs; Attributes* a = rt->FieldDecl(i)->attrs.get();
bool optional = (a && a->FindAttr(ATTR_OPTIONAL)); bool optional = (a && a->FindAttr(ATTR_OPTIONAL));
if ( ! (rv_i || optional) ) if ( ! (rv_i || optional) )
@ -519,7 +519,7 @@ int CompositeHash::SingleTypeKeySize(BroType* bt, const Val* v,
for ( int i = 0; i < num_fields; ++i ) for ( int i = 0; i < num_fields; ++i )
{ {
Attributes* a = rt->FieldDecl(i)->attrs; Attributes* a = rt->FieldDecl(i)->attrs.get();
bool optional = (a && a->FindAttr(ATTR_OPTIONAL)); bool optional = (a && a->FindAttr(ATTR_OPTIONAL));
sz = SingleTypeKeySize(rt->FieldType(i), sz = SingleTypeKeySize(rt->FieldType(i),
@ -914,7 +914,7 @@ const char* CompositeHash::RecoverOneVal(const HashKey* k, const char* kp0,
{ {
IntrusivePtr<Val> v; IntrusivePtr<Val> v;
Attributes* a = rt->FieldDecl(i)->attrs; Attributes* a = rt->FieldDecl(i)->attrs.get();
bool optional = (a && a->FindAttr(ATTR_OPTIONAL)); bool optional = (a && a->FindAttr(ATTR_OPTIONAL));
kp = RecoverOneVal(k, kp, k_end, kp = RecoverOneVal(k, kp, k_end,

View file

@ -2154,7 +2154,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
const TypeDecl* td1 = rt1->FieldDecl(i); const TypeDecl* td1 = rt1->FieldDecl(i);
const TypeDecl* td2 = rt2->FieldDecl(i); const TypeDecl* td2 = rt2->FieldDecl(i);
if ( same_attrs(td1->attrs, td2->attrs) ) if ( same_attrs(td1->attrs.get(), td2->attrs.get()) )
// Everything matches. // Everything matches.
return true; return true;
} }
@ -2438,7 +2438,7 @@ bool AssignExpr::IsRecordElement(TypeDecl* td) const
if ( td ) if ( td )
{ {
const NameExpr* n = (const NameExpr*) op1.get(); const NameExpr* n = (const NameExpr*) op1.get();
td->type = op2->Type()->Ref(); td->type = {NewRef{}, op2->Type()};
td->id = copy_string(n->Id()->Name()); td->id = copy_string(n->Id()->Name());
} }
@ -3034,9 +3034,9 @@ RecordConstructorExpr::RecordConstructorExpr(IntrusivePtr<ListExpr> constructor_
} }
FieldAssignExpr* field = (FieldAssignExpr*) e; FieldAssignExpr* field = (FieldAssignExpr*) e;
BroType* field_type = field->Type()->Ref(); IntrusivePtr<BroType> field_type{NewRef{}, field->Type()};
char* field_name = copy_string(field->FieldName()); char* field_name = copy_string(field->FieldName());
record_types->push_back(new TypeDecl(field_type, field_name)); record_types->push_back(new TypeDecl(std::move(field_type), field_name));
} }
SetType(make_intrusive<RecordType>(record_types)); SetType(make_intrusive<RecordType>(record_types));
@ -3454,7 +3454,7 @@ bool FieldAssignExpr::IsRecordElement(TypeDecl* td) const
{ {
if ( td ) if ( td )
{ {
td->type = op->Type()->Ref(); td->type = {NewRef{}, op->Type()};
td->id = copy_string(field_name.c_str()); td->id = copy_string(field_name.c_str());
} }
@ -5015,7 +5015,7 @@ int check_and_promote_expr(Expr*& e, BroType* t)
const TypeDecl* td1 = t_r->FieldDecl(i); const TypeDecl* td1 = t_r->FieldDecl(i);
const TypeDecl* td2 = et_r->FieldDecl(i); const TypeDecl* td2 = et_r->FieldDecl(i);
if ( same_attrs(td1->attrs, td2->attrs) ) if ( same_attrs(td1->attrs.get(), td2->attrs.get()) )
// Everything matches perfectly. // Everything matches perfectly.
return 1; return 1;
} }

View file

@ -188,7 +188,7 @@ void ID::UpdateValAttrs()
TypeDecl* fd = rt->FieldDecl(i); TypeDecl* fd = rt->FieldDecl(i);
if ( ! fd->attrs ) if ( ! fd->attrs )
fd->attrs = new Attributes(new attr_list, rt->FieldType(i), true, IsGlobal()); fd->attrs = make_intrusive<Attributes>(new attr_list, rt->FieldType(i), true, IsGlobal());
fd->attrs->AddAttr(new Attr(ATTR_LOG)); fd->attrs->AddAttr(new Attr(ATTR_LOG));
} }

View file

@ -363,7 +363,7 @@ TypeList* TableType::ExpandRecordIndex(RecordType* rt) const
for ( int i = 0; i < n; ++i ) for ( int i = 0; i < n; ++i )
{ {
TypeDecl* td = rt->FieldDecl(i); TypeDecl* td = rt->FieldDecl(i);
tl->Append({NewRef{}, td->type}); tl->Append(td->type);
} }
return tl; return tl;
@ -569,28 +569,23 @@ void FuncType::DescribeReST(ODesc* d, bool roles_only) const
} }
} }
TypeDecl::TypeDecl(BroType* t, const char* i, attr_list* arg_attrs, bool in_record) TypeDecl::TypeDecl(IntrusivePtr<BroType> t, const char* i, attr_list* arg_attrs, bool in_record)
:type(std::move(t)),
attrs(arg_attrs ? make_intrusive<Attributes>(arg_attrs, type.get(), in_record, false) : nullptr),
id(i)
{ {
type = t;
attrs = arg_attrs ? new Attributes(arg_attrs, t, in_record, false) : 0;
id = i;
} }
TypeDecl::TypeDecl(const TypeDecl& other) TypeDecl::TypeDecl(const TypeDecl& other)
{ {
type = other.type->Ref(); type = other.type;
attrs = other.attrs; attrs = other.attrs;
if ( attrs )
::Ref(attrs);
id = copy_string(other.id); id = copy_string(other.id);
} }
TypeDecl::~TypeDecl() TypeDecl::~TypeDecl()
{ {
Unref(type);
Unref(attrs);
delete [] id; delete [] id;
} }
@ -655,7 +650,7 @@ BroType* RecordType::FieldType(const char* field) const
BroType* RecordType::FieldType(int field) const BroType* RecordType::FieldType(int field) const
{ {
return (*types)[field]->type; return (*types)[field]->type.get();
} }
Val* RecordType::FieldDefault(int field) const Val* RecordType::FieldDefault(int field) const
@ -826,7 +821,7 @@ const char* RecordType::AddFields(type_decl_list* others, attr_list* attr)
if ( log ) if ( log )
{ {
if ( ! td->attrs ) if ( ! td->attrs )
td->attrs = new Attributes(new attr_list, td->type, true, false); td->attrs = make_intrusive<Attributes>(new attr_list, td->type.get(), true, false);
td->attrs->AddAttr(new Attr(ATTR_LOG)); td->attrs->AddAttr(new Attr(ATTR_LOG));
} }
@ -853,7 +848,7 @@ void RecordType::DescribeFields(ODesc* d) const
d->Add(td->id); d->Add(td->id);
d->Add(":"); d->Add(":");
if ( d->FindType(td->type) ) if ( d->FindType(td->type.get()) )
d->Add("<recursion>"); d->Add("<recursion>");
else else
td->type->Describe(d); td->type->Describe(d);
@ -899,7 +894,7 @@ void RecordType::DescribeFieldsReST(ODesc* d, bool func_args) const
const TypeDecl* td = FieldDecl(i); const TypeDecl* td = FieldDecl(i);
if ( d->FindType(td->type) ) if ( d->FindType(td->type.get()) )
d->Add("<recursion>"); d->Add("<recursion>");
else else
{ {
@ -1549,7 +1544,7 @@ int same_type(const BroType* t1, const BroType* t2, int is_init, bool match_reco
const TypeDecl* td2 = rt2->FieldDecl(i); const TypeDecl* td2 = rt2->FieldDecl(i);
if ( (match_record_field_names && ! streq(td1->id, td2->id)) || if ( (match_record_field_names && ! streq(td1->id, td2->id)) ||
! same_type(td1->type, td2->type, is_init, match_record_field_names) ) ! same_type(td1->type.get(), td2->type.get(), is_init, match_record_field_names) )
return 0; return 0;
} }
@ -1876,17 +1871,16 @@ BroType* merge_types(const BroType* t1, const BroType* t2)
{ {
const TypeDecl* td1 = rt1->FieldDecl(i); const TypeDecl* td1 = rt1->FieldDecl(i);
const TypeDecl* td2 = rt2->FieldDecl(i); const TypeDecl* td2 = rt2->FieldDecl(i);
BroType* tdl3_i = merge_types(td1->type, td2->type); IntrusivePtr<BroType> tdl3_i{AdoptRef{}, merge_types(td1->type.get(), td2->type.get())};
if ( ! streq(td1->id, td2->id) || ! tdl3_i ) if ( ! streq(td1->id, td2->id) || ! tdl3_i )
{ {
t1->Error("incompatible record fields", t2); t1->Error("incompatible record fields", t2);
delete tdl3; delete tdl3;
Unref(tdl3_i);
return 0; return 0;
} }
tdl3->push_back(new TypeDecl(tdl3_i, copy_string(td1->id))); tdl3->push_back(new TypeDecl(std::move(tdl3_i), copy_string(td1->id)));
} }
return new RecordType(tdl3); return new RecordType(tdl3);

View file

@ -479,7 +479,7 @@ protected:
class TypeDecl final { class TypeDecl final {
public: public:
TypeDecl(BroType* t, const char* i, attr_list* attrs = 0, bool in_record = false); TypeDecl(IntrusivePtr<BroType> t, const char* i, attr_list* attrs = 0, bool in_record = false);
TypeDecl(const TypeDecl& other); TypeDecl(const TypeDecl& other);
~TypeDecl(); ~TypeDecl();
@ -488,8 +488,8 @@ public:
void DescribeReST(ODesc* d, bool roles_only = false) const; void DescribeReST(ODesc* d, bool roles_only = false) const;
BroType* type; IntrusivePtr<BroType> type;
Attributes* attrs; IntrusivePtr<Attributes> attrs;
const char* id; const char* id;
}; };

View file

@ -2550,10 +2550,10 @@ RecordVal::RecordVal(RecordType* t, bool init_fields) : Val(t)
// by default). // by default).
for ( int i = 0; i < n; ++i ) for ( int i = 0; i < n; ++i )
{ {
Attributes* a = t->FieldDecl(i)->attrs; Attributes* a = t->FieldDecl(i)->attrs.get();
Attr* def_attr = a ? a->FindAttr(ATTR_DEFAULT) : nullptr; Attr* def_attr = a ? a->FindAttr(ATTR_DEFAULT) : nullptr;
auto def = def_attr ? def_attr->AttrExpr()->Eval(nullptr) : nullptr; auto def = def_attr ? def_attr->AttrExpr()->Eval(nullptr) : nullptr;
BroType* type = t->FieldDecl(i)->type; BroType* type = t->FieldDecl(i)->type.get();
if ( def && type->Tag() == TYPE_RECORD && if ( def && type->Tag() == TYPE_RECORD &&
def->Type()->Tag() == TYPE_RECORD && def->Type()->Tag() == TYPE_RECORD &&

View file

@ -312,7 +312,7 @@ static void transfer_arg_defaults(RecordType* args, RecordType* recv)
if ( ! recv_i->attrs ) if ( ! recv_i->attrs )
{ {
attr_list* a = new attr_list{def}; attr_list* a = new attr_list{def};
recv_i->attrs = new Attributes(a, recv_i->type, true, false); recv_i->attrs = make_intrusive<Attributes>(a, recv_i->type.get(), true, false);
} }
else if ( ! recv_i->attrs->FindAttr(ATTR_DEFAULT) ) else if ( ! recv_i->attrs->FindAttr(ATTR_DEFAULT) )
@ -408,7 +408,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor,
arg_id->Error("argument name used twice"); arg_id->Error("argument name used twice");
arg_id = install_ID(arg_i->id, module_name, false, false); arg_id = install_ID(arg_i->id, module_name, false, false);
arg_id->SetType({NewRef{}, arg_i->type}); arg_id->SetType(arg_i->type);
} }
if ( Attr* depr_attr = find_attr(attrs, ATTR_DEPRECATED) ) if ( Attr* depr_attr = find_attr(attrs, ATTR_DEPRECATED) )

View file

@ -1050,7 +1050,7 @@ type_decl:
TOK_ID ':' type opt_attr ';' TOK_ID ':' type opt_attr ';'
{ {
set_location(@1, @4); set_location(@1, @4);
$$ = new TypeDecl($3, $1, $4, (in_record > 0)); $$ = new TypeDecl({AdoptRef{}, $3}, $1, $4, (in_record > 0));
if ( in_record > 0 && cur_decl_type_id ) if ( in_record > 0 && cur_decl_type_id )
zeekygen_mgr->RecordField(cur_decl_type_id, $$, ::filename); zeekygen_mgr->RecordField(cur_decl_type_id, $$, ::filename);
@ -1079,7 +1079,7 @@ formal_args_decl:
TOK_ID ':' type opt_attr TOK_ID ':' type opt_attr
{ {
set_location(@1, @4); set_location(@1, @4);
$$ = new TypeDecl($3, $1, $4, true); $$ = new TypeDecl({AdoptRef{}, $3}, $1, $4, true);
} }
; ;