Change BroType::ShallowClone() to return IntrusivePtr

This commit is contained in:
Jon Siwek 2020-05-06 15:53:52 -07:00
parent b05e5c7686
commit 89dd668aff
3 changed files with 28 additions and 26 deletions

View file

@ -69,7 +69,7 @@ BroType::BroType(TypeTag t, bool arg_base_type)
{ {
} }
BroType* BroType::ShallowClone() IntrusivePtr<BroType> BroType::ShallowClone()
{ {
switch ( tag ) { switch ( tag ) {
case TYPE_VOID: case TYPE_VOID:
@ -87,7 +87,7 @@ BroType* BroType::ShallowClone()
case TYPE_ADDR: case TYPE_ADDR:
case TYPE_SUBNET: case TYPE_SUBNET:
case TYPE_ANY: case TYPE_ANY:
return new BroType(tag, base_type); return make_intrusive<BroType>(tag, base_type);
default: default:
reporter->InternalError("cloning illegal base BroType"); reporter->InternalError("cloning illegal base BroType");
@ -346,9 +346,9 @@ TableType::TableType(IntrusivePtr<TypeList> ind, IntrusivePtr<BroType> yield)
} }
} }
TableType* TableType::ShallowClone() IntrusivePtr<BroType> TableType::ShallowClone()
{ {
return new TableType(indices, yield_type); return make_intrusive<TableType>(indices, yield_type);
} }
bool TableType::IsUnspecifiedTable() const bool TableType::IsUnspecifiedTable() const
@ -419,9 +419,9 @@ SetType::SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements
} }
} }
SetType* SetType::ShallowClone() IntrusivePtr<BroType> SetType::ShallowClone()
{ {
return new SetType(indices, elements); return make_intrusive<SetType>(indices, elements);
} }
SetType::~SetType() = default; SetType::~SetType() = default;
@ -457,9 +457,9 @@ FuncType::FuncType(IntrusivePtr<RecordType> arg_args,
prototypes.emplace_back(Prototype{false, args, std::move(offsets)}); prototypes.emplace_back(Prototype{false, args, std::move(offsets)});
} }
FuncType* FuncType::ShallowClone() IntrusivePtr<BroType> FuncType::ShallowClone()
{ {
auto f = new FuncType(); auto f = make_intrusive<FuncType>();
f->args = {NewRef{}, args->AsRecordType()}; f->args = {NewRef{}, args->AsRecordType()};
f->arg_types = {NewRef{}, arg_types->AsTypeList()}; f->arg_types = {NewRef{}, arg_types->AsTypeList()};
f->yield = yield; 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 // in this case the clone is actually not so shallow, since
// it gets modified by everyone. // it gets modified by everyone.
RecordType* RecordType::ShallowClone() IntrusivePtr<BroType> RecordType::ShallowClone()
{ {
auto pass = new type_decl_list(); auto pass = new type_decl_list();
for ( const auto& type : *types ) for ( const auto& type : *types )
pass->push_back(new TypeDecl(*type)); pass->push_back(new TypeDecl(*type));
return new RecordType(pass); return make_intrusive<RecordType>(pass);
} }
RecordType::~RecordType() RecordType::~RecordType()
@ -1128,12 +1128,12 @@ EnumType::EnumType(const EnumType* e)
SetName(e->GetName()); SetName(e->GetName());
} }
EnumType* EnumType::ShallowClone() IntrusivePtr<BroType> EnumType::ShallowClone()
{ {
if ( counter == 0 ) if ( counter == 0 )
return new EnumType(GetName()); return make_intrusive<EnumType>(GetName());
return new EnumType(this); return make_intrusive<EnumType>(this);
} }
EnumType::~EnumType() = default; EnumType::~EnumType() = default;
@ -1356,9 +1356,9 @@ VectorType::VectorType(IntrusivePtr<BroType> element_type)
{ {
} }
VectorType* VectorType::ShallowClone() IntrusivePtr<BroType> VectorType::ShallowClone()
{ {
return new VectorType(yield_type); return make_intrusive<VectorType>(yield_type);
} }
VectorType::~VectorType() = default; VectorType::~VectorType() = default;

View file

@ -151,7 +151,7 @@ public:
// Clone operations will mostly be implemented in the derived classes; // Clone operations will mostly be implemented in the derived classes;
// in addition cloning will be limited to classes that can be reached by // in addition cloning will be limited to classes that can be reached by
// the script-level. // the script-level.
virtual BroType* ShallowClone(); virtual IntrusivePtr<BroType> ShallowClone();
TypeTag Tag() const { return tag; } TypeTag Tag() const { return tag; }
InternalTypeTag InternalType() const { return internal_tag; } InternalTypeTag InternalType() const { return internal_tag; }
@ -415,7 +415,7 @@ class TableType : public IndexType {
public: public:
TableType(IntrusivePtr<TypeList> ind, IntrusivePtr<BroType> yield); TableType(IntrusivePtr<TypeList> ind, IntrusivePtr<BroType> yield);
TableType* ShallowClone() override; IntrusivePtr<BroType> ShallowClone() override;
// Returns true if this table type is "unspecified", which is // Returns true if this table type is "unspecified", which is
// what one gets using an empty "set()" or "table()" constructor. // what one gets using an empty "set()" or "table()" constructor.
@ -430,7 +430,7 @@ public:
SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements); SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements);
~SetType() override; ~SetType() override;
SetType* ShallowClone() override; IntrusivePtr<BroType> ShallowClone() override;
ListExpr* SetElements() const { return elements.get(); } ListExpr* SetElements() const { return elements.get(); }
@ -453,7 +453,7 @@ public:
FuncType(IntrusivePtr<RecordType> args, IntrusivePtr<BroType> yield, FuncType(IntrusivePtr<RecordType> args, IntrusivePtr<BroType> yield,
function_flavor f); function_flavor f);
FuncType* ShallowClone() override; IntrusivePtr<BroType> ShallowClone() override;
~FuncType() override; ~FuncType() override;
@ -493,6 +493,8 @@ public:
{ return prototypes; } { return prototypes; }
protected: protected:
friend IntrusivePtr<FuncType> make_intrusive<FuncType>();
FuncType() : BroType(TYPE_FUNC) { flavor = FUNC_FLAVOR_FUNCTION; } FuncType() : BroType(TYPE_FUNC) { flavor = FUNC_FLAVOR_FUNCTION; }
IntrusivePtr<RecordType> args; IntrusivePtr<RecordType> args;
IntrusivePtr<TypeList> arg_types; IntrusivePtr<TypeList> arg_types;
@ -504,7 +506,7 @@ protected:
class TypeType final : public BroType { class TypeType final : public BroType {
public: public:
explicit TypeType(IntrusivePtr<BroType> t) : BroType(TYPE_TYPE), type(std::move(t)) {} explicit TypeType(IntrusivePtr<BroType> t) : BroType(TYPE_TYPE), type(std::move(t)) {}
TypeType* ShallowClone() override { return new TypeType(type); } IntrusivePtr<BroType> ShallowClone() override { return make_intrusive<TypeType>(type); }
BroType* Type() { return type.get(); } BroType* Type() { return type.get(); }
const BroType* Type() const { return type.get(); } const BroType* Type() const { return type.get(); }
@ -534,7 +536,7 @@ typedef PList<TypeDecl> type_decl_list;
class RecordType final : public BroType { class RecordType final : public BroType {
public: public:
explicit RecordType(type_decl_list* types); explicit RecordType(type_decl_list* types);
RecordType* ShallowClone() override; IntrusivePtr<BroType> ShallowClone() override;
~RecordType() override; ~RecordType() override;
@ -604,7 +606,7 @@ public:
class FileType final : public BroType { class FileType final : public BroType {
public: public:
explicit FileType(IntrusivePtr<BroType> yield_type); explicit FileType(IntrusivePtr<BroType> yield_type);
FileType* ShallowClone() override { return new FileType(yield); } IntrusivePtr<BroType> ShallowClone() override { return make_intrusive<FileType>(yield); }
~FileType() override; ~FileType() override;
BroType* YieldType() override; BroType* YieldType() override;
@ -618,7 +620,7 @@ protected:
class OpaqueType final : public BroType { class OpaqueType final : public BroType {
public: public:
explicit OpaqueType(const std::string& name); explicit OpaqueType(const std::string& name);
OpaqueType* ShallowClone() override { return new OpaqueType(name); } IntrusivePtr<BroType> ShallowClone() override { return make_intrusive<OpaqueType>(name); }
~OpaqueType() override { }; ~OpaqueType() override { };
const std::string& Name() const { return name; } const std::string& Name() const { return name; }
@ -638,7 +640,7 @@ public:
explicit EnumType(const EnumType* e); explicit EnumType(const EnumType* e);
explicit EnumType(const std::string& arg_name); explicit EnumType(const std::string& arg_name);
EnumType* ShallowClone() override; IntrusivePtr<BroType> ShallowClone() override;
~EnumType() override; ~EnumType() override;
// The value of this name is next internal counter value, starting // The value of this name is next internal counter value, starting
@ -688,7 +690,7 @@ protected:
class VectorType final : public BroType { class VectorType final : public BroType {
public: public:
explicit VectorType(IntrusivePtr<BroType> t); explicit VectorType(IntrusivePtr<BroType> t);
VectorType* ShallowClone() override; IntrusivePtr<BroType> ShallowClone() override;
~VectorType() override; ~VectorType() override;
BroType* YieldType() override; BroType* YieldType() override;
const BroType* YieldType() const override; const BroType* YieldType() const override;

View file

@ -363,7 +363,7 @@ void add_type(ID* id, IntrusivePtr<BroType> t, attr_list* attr)
tnew = std::move(t); tnew = std::move(t);
else else
// Clone the type to preserve type name aliasing. // Clone the type to preserve type name aliasing.
tnew = {AdoptRef{}, t->ShallowClone()}; tnew = t->ShallowClone();
BroType::AddAlias(new_type_name, tnew.get()); BroType::AddAlias(new_type_name, tnew.get());