diff --git a/src/Type.cc b/src/Type.cc index 934d82d926..2b9a79887b 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -69,7 +69,7 @@ BroType::BroType(TypeTag t, bool arg_base_type) { } -BroType* BroType::ShallowClone() +IntrusivePtr BroType::ShallowClone() { switch ( tag ) { case TYPE_VOID: @@ -87,7 +87,7 @@ BroType* BroType::ShallowClone() case TYPE_ADDR: case TYPE_SUBNET: case TYPE_ANY: - return new BroType(tag, base_type); + return make_intrusive(tag, base_type); default: reporter->InternalError("cloning illegal base BroType"); @@ -346,9 +346,9 @@ TableType::TableType(IntrusivePtr ind, IntrusivePtr yield) } } -TableType* TableType::ShallowClone() +IntrusivePtr TableType::ShallowClone() { - return new TableType(indices, yield_type); + return make_intrusive(indices, yield_type); } bool TableType::IsUnspecifiedTable() const @@ -419,9 +419,9 @@ SetType::SetType(IntrusivePtr ind, IntrusivePtr arg_elements } } -SetType* SetType::ShallowClone() +IntrusivePtr SetType::ShallowClone() { - return new SetType(indices, elements); + return make_intrusive(indices, elements); } SetType::~SetType() = default; @@ -457,9 +457,9 @@ FuncType::FuncType(IntrusivePtr arg_args, prototypes.emplace_back(Prototype{false, args, std::move(offsets)}); } -FuncType* FuncType::ShallowClone() +IntrusivePtr FuncType::ShallowClone() { - auto f = new FuncType(); + auto f = make_intrusive(); f->args = {NewRef{}, args->AsRecordType()}; f->arg_types = {NewRef{}, arg_types->AsTypeList()}; f->yield = yield; @@ -672,12 +672,12 @@ RecordType::RecordType(type_decl_list* arg_types) : BroType(TYPE_RECORD) // in this case the clone is actually not so shallow, since // it gets modified by everyone. -RecordType* RecordType::ShallowClone() +IntrusivePtr RecordType::ShallowClone() { auto pass = new type_decl_list(); for ( const auto& type : *types ) pass->push_back(new TypeDecl(*type)); - return new RecordType(pass); + return make_intrusive(pass); } RecordType::~RecordType() @@ -1128,12 +1128,12 @@ EnumType::EnumType(const EnumType* e) SetName(e->GetName()); } -EnumType* EnumType::ShallowClone() +IntrusivePtr EnumType::ShallowClone() { if ( counter == 0 ) - return new EnumType(GetName()); + return make_intrusive(GetName()); - return new EnumType(this); + return make_intrusive(this); } EnumType::~EnumType() = default; @@ -1356,9 +1356,9 @@ VectorType::VectorType(IntrusivePtr element_type) { } -VectorType* VectorType::ShallowClone() +IntrusivePtr VectorType::ShallowClone() { - return new VectorType(yield_type); + return make_intrusive(yield_type); } VectorType::~VectorType() = default; diff --git a/src/Type.h b/src/Type.h index daa6a7fb13..7c509fae7c 100644 --- a/src/Type.h +++ b/src/Type.h @@ -151,7 +151,7 @@ public: // Clone operations will mostly be implemented in the derived classes; // in addition cloning will be limited to classes that can be reached by // the script-level. - virtual BroType* ShallowClone(); + virtual IntrusivePtr ShallowClone(); TypeTag Tag() const { return tag; } InternalTypeTag InternalType() const { return internal_tag; } @@ -415,7 +415,7 @@ class TableType : public IndexType { public: TableType(IntrusivePtr ind, IntrusivePtr yield); - TableType* ShallowClone() override; + IntrusivePtr ShallowClone() override; // Returns true if this table type is "unspecified", which is // what one gets using an empty "set()" or "table()" constructor. @@ -430,7 +430,7 @@ public: SetType(IntrusivePtr ind, IntrusivePtr arg_elements); ~SetType() override; - SetType* ShallowClone() override; + IntrusivePtr ShallowClone() override; ListExpr* SetElements() const { return elements.get(); } @@ -453,7 +453,7 @@ public: FuncType(IntrusivePtr args, IntrusivePtr yield, function_flavor f); - FuncType* ShallowClone() override; + IntrusivePtr ShallowClone() override; ~FuncType() override; @@ -493,6 +493,8 @@ public: { return prototypes; } protected: + friend IntrusivePtr make_intrusive(); + FuncType() : BroType(TYPE_FUNC) { flavor = FUNC_FLAVOR_FUNCTION; } IntrusivePtr args; IntrusivePtr arg_types; @@ -504,7 +506,7 @@ protected: class TypeType final : public BroType { public: explicit TypeType(IntrusivePtr t) : BroType(TYPE_TYPE), type(std::move(t)) {} - TypeType* ShallowClone() override { return new TypeType(type); } + IntrusivePtr ShallowClone() override { return make_intrusive(type); } BroType* Type() { return type.get(); } const BroType* Type() const { return type.get(); } @@ -534,7 +536,7 @@ typedef PList type_decl_list; class RecordType final : public BroType { public: explicit RecordType(type_decl_list* types); - RecordType* ShallowClone() override; + IntrusivePtr ShallowClone() override; ~RecordType() override; @@ -604,7 +606,7 @@ public: class FileType final : public BroType { public: explicit FileType(IntrusivePtr yield_type); - FileType* ShallowClone() override { return new FileType(yield); } + IntrusivePtr ShallowClone() override { return make_intrusive(yield); } ~FileType() override; BroType* YieldType() override; @@ -618,7 +620,7 @@ protected: class OpaqueType final : public BroType { public: explicit OpaqueType(const std::string& name); - OpaqueType* ShallowClone() override { return new OpaqueType(name); } + IntrusivePtr ShallowClone() override { return make_intrusive(name); } ~OpaqueType() override { }; const std::string& Name() const { return name; } @@ -638,7 +640,7 @@ public: explicit EnumType(const EnumType* e); explicit EnumType(const std::string& arg_name); - EnumType* ShallowClone() override; + IntrusivePtr ShallowClone() override; ~EnumType() override; // The value of this name is next internal counter value, starting @@ -688,7 +690,7 @@ protected: class VectorType final : public BroType { public: explicit VectorType(IntrusivePtr t); - VectorType* ShallowClone() override; + IntrusivePtr ShallowClone() override; ~VectorType() override; BroType* YieldType() override; const BroType* YieldType() const override; diff --git a/src/Var.cc b/src/Var.cc index 54837ae8c3..8fc1214755 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -363,7 +363,7 @@ void add_type(ID* id, IntrusivePtr t, attr_list* attr) tnew = std::move(t); else // Clone the type to preserve type name aliasing. - tnew = {AdoptRef{}, t->ShallowClone()}; + tnew = t->ShallowClone(); BroType::AddAlias(new_type_name, tnew.get());