Type: use class IntrusivePtr in IndexType

This commit is contained in:
Max Kellermann 2020-03-02 20:33:22 +01:00
parent 674e141a15
commit de0289125b
8 changed files with 45 additions and 77 deletions

View file

@ -32,6 +32,7 @@
#include <algorithm> #include <algorithm>
#include "BroString.h" #include "BroString.h"
#include "Expr.h"
#include "Event.h" #include "Event.h"
#include "Net.h" #include "Net.h"
#include "Val.h" #include "Val.h"
@ -173,9 +174,9 @@ void DNS_Mgr_mapping_delete_func(void* v)
static TableVal* empty_addr_set() static TableVal* empty_addr_set()
{ {
BroType* addr_t = base_type(TYPE_ADDR); BroType* addr_t = base_type(TYPE_ADDR);
TypeList* set_index = new TypeList(addr_t); auto set_index = make_intrusive<TypeList>(addr_t);
set_index->Append(addr_t); set_index->Append(addr_t);
auto s = make_intrusive<SetType>(set_index, nullptr); auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
return new TableVal(std::move(s)); return new TableVal(std::move(s));
} }

View file

@ -2300,7 +2300,7 @@ IntrusivePtr<BroType> AssignExpr::InitType() const
if ( tl->Tag() != TYPE_LIST ) if ( tl->Tag() != TYPE_LIST )
Internal("inconsistent list expr in AssignExpr::InitType"); Internal("inconsistent list expr in AssignExpr::InitType");
return make_intrusive<TableType>(tl->Ref()->AsTypeList(), op2->Type()->Ref()); return make_intrusive<TableType>(IntrusivePtr{NewRef{}, tl->AsTypeList()}, IntrusivePtr{NewRef{}, op2->Type()});
} }
void AssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) const void AssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) const
@ -3109,7 +3109,7 @@ TableConstructorExpr::TableConstructorExpr(IntrusivePtr<ListExpr> constructor_li
else else
{ {
if ( op->AsListExpr()->Exprs().empty() ) if ( op->AsListExpr()->Exprs().empty() )
SetType(make_intrusive<TableType>(new TypeList(base_type(TYPE_ANY)), nullptr)); SetType(make_intrusive<TableType>(make_intrusive<TypeList>(base_type(TYPE_ANY)), nullptr));
else else
{ {
SetType({AdoptRef{}, init_type(op.get())}); SetType({AdoptRef{}, init_type(op.get())});
@ -3223,7 +3223,7 @@ SetConstructorExpr::SetConstructorExpr(IntrusivePtr<ListExpr> constructor_list,
else else
{ {
if ( op->AsListExpr()->Exprs().empty() ) if ( op->AsListExpr()->Exprs().empty() )
SetType(make_intrusive<::SetType>(new TypeList(base_type(TYPE_ANY)), nullptr)); SetType(make_intrusive<::SetType>(make_intrusive<TypeList>(base_type(TYPE_ANY)), nullptr));
else else
SetType({AdoptRef{}, init_type(op.get())}); SetType({AdoptRef{}, init_type(op.get())});
} }

View file

@ -220,11 +220,7 @@ unsigned int TypeList::MemoryAllocation() const
+ types.MemoryAllocation() - padded_sizeof(types); + types.MemoryAllocation() - padded_sizeof(types);
} }
IndexType::~IndexType() IndexType::~IndexType() = default;
{
Unref(indices);
Unref(yield_type);
}
int IndexType::MatchesIndex(ListExpr* const index) const int IndexType::MatchesIndex(ListExpr* const index) const
{ {
@ -242,12 +238,12 @@ int IndexType::MatchesIndex(ListExpr* const index) const
BroType* IndexType::YieldType() BroType* IndexType::YieldType()
{ {
return yield_type; return yield_type.get();
} }
const BroType* IndexType::YieldType() const const BroType* IndexType::YieldType() const
{ {
return yield_type; return yield_type.get();
} }
void IndexType::Describe(ODesc* d) const void IndexType::Describe(ODesc* d) const
@ -326,8 +322,8 @@ bool IndexType::IsSubNetIndex() const
return false; return false;
} }
TableType::TableType(TypeList* ind, BroType* yield) TableType::TableType(IntrusivePtr<TypeList> ind, IntrusivePtr<BroType> yield)
: IndexType(TYPE_TABLE, ind, yield) : IndexType(TYPE_TABLE, std::move(ind), std::move(yield))
{ {
if ( ! indices ) if ( ! indices )
return; return;
@ -355,11 +351,6 @@ TableType::TableType(TypeList* ind, BroType* yield)
TableType* TableType::ShallowClone() TableType* TableType::ShallowClone()
{ {
if ( indices )
indices->Ref();
if ( yield_type )
yield_type->Ref();
return new TableType(indices, yield_type); return new TableType(indices, yield_type);
} }
@ -383,14 +374,13 @@ TypeList* TableType::ExpandRecordIndex(RecordType* rt) const
return tl; return tl;
} }
SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0) SetType::SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements) : TableType(std::move(ind), nullptr), elements(std::move(arg_elements))
{ {
elements = arg_elements;
if ( elements ) if ( elements )
{ {
if ( indices ) if ( indices )
{ // We already have a type. { // We already have a type.
if ( ! check_and_promote_exprs(elements, indices) ) if ( ! check_and_promote_exprs(elements.get(), indices.get()) )
SetError(); SetError();
} }
else else
@ -407,7 +397,7 @@ SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0)
else if ( tl->length() == 1 ) else if ( tl->length() == 1 )
{ {
BroType* t = flatten_type((*tl)[0]->Ref()); BroType* t = flatten_type((*tl)[0]->Ref());
indices = new TypeList(t); indices = make_intrusive<TypeList>(t);
indices->Append(t->Ref()); indices->Append(t->Ref());
} }
@ -428,7 +418,7 @@ SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0)
return; return;
} }
indices = new TypeList(t); indices = make_intrusive<TypeList>(t);
indices->Append(t); indices->Append(t);
} }
} }
@ -437,19 +427,10 @@ SetType::SetType(TypeList* ind, ListExpr* arg_elements) : TableType(ind, 0)
SetType* SetType::ShallowClone() SetType* SetType::ShallowClone()
{ {
if ( elements )
elements->Ref();
if ( indices )
indices->Ref();
return new SetType(indices, elements); return new SetType(indices, elements);
} }
SetType::~SetType() SetType::~SetType() = default;
{
Unref(elements);
}
FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg_flavor) FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg_flavor)
: BroType(TYPE_FUNC) : BroType(TYPE_FUNC)
@ -1837,7 +1818,7 @@ BroType* merge_types(const BroType* t1, const BroType* t2)
const type_list* tl1 = it1->IndexTypes(); const type_list* tl1 = it1->IndexTypes();
const type_list* tl2 = it2->IndexTypes(); const type_list* tl2 = it2->IndexTypes();
TypeList* tl3 = 0; IntrusivePtr<TypeList> tl3;
if ( tl1 || tl2 ) if ( tl1 || tl2 )
{ {
@ -1847,16 +1828,13 @@ BroType* merge_types(const BroType* t1, const BroType* t2)
return 0; return 0;
} }
tl3 = new TypeList(); tl3 = make_intrusive<TypeList>();
loop_over_list(*tl1, i) loop_over_list(*tl1, i)
{ {
BroType* tl3_i = merge_types((*tl1)[i], (*tl2)[i]); BroType* tl3_i = merge_types((*tl1)[i], (*tl2)[i]);
if ( ! tl3_i ) if ( ! tl3_i )
{
Unref(tl3);
return 0; return 0;
}
tl3->Append(tl3_i); tl3->Append(tl3_i);
} }
@ -1864,29 +1842,25 @@ BroType* merge_types(const BroType* t1, const BroType* t2)
const BroType* y1 = t1->YieldType(); const BroType* y1 = t1->YieldType();
const BroType* y2 = t2->YieldType(); const BroType* y2 = t2->YieldType();
BroType* y3 = 0; IntrusivePtr<BroType> y3;
if ( y1 || y2 ) if ( y1 || y2 )
{ {
if ( ! y1 || ! y2 ) if ( ! y1 || ! y2 )
{ {
t1->Error("incompatible types", t2); t1->Error("incompatible types", t2);
Unref(tl3);
return 0; return 0;
} }
y3 = merge_types(y1, y2); y3 = {AdoptRef{}, merge_types(y1, y2)};
if ( ! y3 ) if ( ! y3 )
{
Unref(tl3);
return 0; return 0;
}
} }
if ( t1->IsSet() ) if ( t1->IsSet() )
return new SetType(tl3, 0); return new SetType(std::move(tl3), nullptr);
else else
return new TableType(tl3, y3); return new TableType(std::move(tl3), std::move(y3));
} }
case TYPE_FUNC: case TYPE_FUNC:
@ -2136,7 +2110,7 @@ BroType* init_type(Expr* init)
t = std::move(tl); t = std::move(tl);
} }
return new SetType(t.release()->AsTypeList(), 0); return new SetType({AdoptRef{}, t.release()->AsTypeList()}, nullptr);
} }
bool is_atomic_type(const BroType* t) bool is_atomic_type(const BroType* t)

View file

@ -5,6 +5,7 @@
#include "Obj.h" #include "Obj.h"
#include "Attr.h" #include "Attr.h"
#include "BroList.h" #include "BroList.h"
#include "IntrusivePtr.h"
#include <string> #include <string>
#include <set> #include <set>
@ -117,7 +118,6 @@ constexpr InternalTypeTag to_internal_type_tag(TypeTag tag) noexcept
// Returns the name of the type. // Returns the name of the type.
extern const char* type_name(TypeTag t); extern const char* type_name(TypeTag t);
template <class T> class IntrusivePtr;
class Expr; class Expr;
class Attributes; class Attributes;
class TypeList; class TypeList;
@ -386,7 +386,7 @@ class IndexType : public BroType {
public: public:
int MatchesIndex(ListExpr* index) const override; int MatchesIndex(ListExpr* index) const override;
TypeList* Indices() const { return indices; } TypeList* Indices() const { return indices.get(); }
const type_list* IndexTypes() const { return indices->Types(); } const type_list* IndexTypes() const { return indices->Types(); }
BroType* YieldType() override; BroType* YieldType() override;
const BroType* YieldType() const override; const BroType* YieldType() const override;
@ -398,22 +398,19 @@ public:
bool IsSubNetIndex() const; bool IsSubNetIndex() const;
protected: protected:
IndexType(){ indices = 0; yield_type = 0; } IndexType(TypeTag t, IntrusivePtr<TypeList> arg_indices, IntrusivePtr<BroType> arg_yield_type) :
IndexType(TypeTag t, TypeList* arg_indices, BroType* arg_yield_type) : BroType(t), indices(std::move(arg_indices)), yield_type(std::move(arg_yield_type))
BroType(t)
{ {
indices = arg_indices;
yield_type = arg_yield_type;
} }
~IndexType() override; ~IndexType() override;
TypeList* indices; IntrusivePtr<TypeList> indices;
BroType* yield_type; IntrusivePtr<BroType> yield_type;
}; };
class TableType : public IndexType { class TableType : public IndexType {
public: public:
TableType(TypeList* ind, BroType* yield); TableType(IntrusivePtr<TypeList> ind, IntrusivePtr<BroType> yield);
TableType* ShallowClone() override; TableType* ShallowClone() override;
@ -422,24 +419,20 @@ public:
bool IsUnspecifiedTable() const; bool IsUnspecifiedTable() const;
protected: protected:
TableType() {}
TypeList* ExpandRecordIndex(RecordType* rt) const; TypeList* ExpandRecordIndex(RecordType* rt) const;
}; };
class SetType : public TableType { class SetType : public TableType {
public: public:
SetType(TypeList* ind, ListExpr* arg_elements); SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements);
~SetType() override; ~SetType() override;
SetType* ShallowClone() override; SetType* ShallowClone() override;
ListExpr* SetElements() const { return elements; } ListExpr* SetElements() const { return elements.get(); }
protected: protected:
SetType() {} IntrusivePtr<ListExpr> elements;
ListExpr* elements;
}; };
class FuncType : public BroType { class FuncType : public BroType {

View file

@ -1234,9 +1234,9 @@ TableVal* ListVal::ConvertToSet() const
if ( tag == TYPE_ANY ) if ( tag == TYPE_ANY )
Internal("conversion of heterogeneous list to set"); Internal("conversion of heterogeneous list to set");
TypeList* set_index = new TypeList(type->AsTypeList()->PureType()); auto set_index = make_intrusive<TypeList>(type->AsTypeList()->PureType());
set_index->Append(base_type(tag)); set_index->Append(base_type(tag));
auto s = make_intrusive<SetType>(set_index, nullptr); auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
TableVal* t = new TableVal(std::move(s)); TableVal* t = new TableVal(std::move(s));
for ( const auto& val : vals ) for ( const auto& val : vals )

View file

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

View file

@ -2377,9 +2377,9 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, BroType* request_typ
{ {
// all entries have to have the same type... // all entries have to have the same type...
BroType* type = request_type->AsTableType()->Indices()->PureType(); BroType* type = request_type->AsTableType()->Indices()->PureType();
TypeList* set_index = new TypeList(type->Ref()); auto set_index = make_intrusive<TypeList>(type->Ref());
set_index->Append(type->Ref()); set_index->Append(type->Ref());
auto s = make_intrusive<SetType>(set_index, nullptr); auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
TableVal* t = new TableVal(std::move(s)); TableVal* t = new TableVal(std::move(s));
for ( int j = 0; j < val->val.set_val.size; j++ ) for ( int j = 0; j < val->val.set_val.size; j++ )
{ {
@ -2523,10 +2523,10 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co
case TYPE_TABLE: case TYPE_TABLE:
{ {
TypeList* set_index; IntrusivePtr<TypeList> set_index;
if ( val->val.set_val.size == 0 && val->subtype == TYPE_VOID ) if ( val->val.set_val.size == 0 && val->subtype == TYPE_VOID )
// don't know type - unspecified table. // don't know type - unspecified table.
set_index = new TypeList(); set_index = make_intrusive<TypeList>();
else else
{ {
// all entries have to have the same type... // all entries have to have the same type...
@ -2557,11 +2557,11 @@ Val* Manager::ValueToVal(const Stream* i, const Value* val, bool& have_error) co
else else
index_type = base_type_no_ref(stag); index_type = base_type_no_ref(stag);
set_index = new TypeList(index_type); set_index = make_intrusive<TypeList>(index_type);
set_index->Append(index_type->Ref()); set_index->Append(index_type->Ref());
} }
auto s = make_intrusive<SetType>(set_index, nullptr); auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
TableVal* t = new TableVal(std::move(s)); TableVal* t = new TableVal(std::move(s));
for ( int j = 0; j < val->val.set_val.size; j++ ) for ( int j = 0; j < val->val.set_val.size; j++ )
{ {

View file

@ -915,13 +915,13 @@ type:
| TOK_TABLE '[' type_list ']' TOK_OF type | TOK_TABLE '[' type_list ']' TOK_OF type
{ {
set_location(@1, @6); set_location(@1, @6);
$$ = new TableType($3, $6); $$ = new TableType({AdoptRef{}, $3}, {AdoptRef{}, $6});
} }
| TOK_SET '[' type_list ']' | TOK_SET '[' type_list ']'
{ {
set_location(@1, @4); set_location(@1, @4);
$$ = new SetType($3, 0); $$ = new SetType({AdoptRef{}, $3}, nullptr);
} }
| TOK_RECORD '{' | TOK_RECORD '{'