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 "BroString.h"
#include "Expr.h"
#include "Event.h"
#include "Net.h"
#include "Val.h"
@ -173,9 +174,9 @@ void DNS_Mgr_mapping_delete_func(void* v)
static TableVal* empty_addr_set()
{
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);
auto s = make_intrusive<SetType>(set_index, nullptr);
auto s = make_intrusive<SetType>(std::move(set_index), nullptr);
return new TableVal(std::move(s));
}

View file

@ -2300,7 +2300,7 @@ IntrusivePtr<BroType> AssignExpr::InitType() const
if ( tl->Tag() != TYPE_LIST )
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
@ -3109,7 +3109,7 @@ TableConstructorExpr::TableConstructorExpr(IntrusivePtr<ListExpr> constructor_li
else
{
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
{
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>(new TypeList(base_type(TYPE_ANY)), nullptr));
SetType(make_intrusive<::SetType>(make_intrusive<TypeList>(base_type(TYPE_ANY)), nullptr));
else
SetType({AdoptRef{}, init_type(op.get())});
}

View file

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

View file

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

View file

@ -1234,9 +1234,9 @@ TableVal* ListVal::ConvertToSet() const
if ( tag == TYPE_ANY )
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));
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));
for ( const auto& val : vals )

View file

@ -23,9 +23,9 @@ using namespace file_analysis;
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());
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));
}

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...
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());
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));
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:
{
TypeList* set_index;
IntrusivePtr<TypeList> set_index;
if ( val->val.set_val.size == 0 && val->subtype == TYPE_VOID )
// don't know type - unspecified table.
set_index = new TypeList();
set_index = make_intrusive<TypeList>();
else
{
// 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
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());
}
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));
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
{
set_location(@1, @6);
$$ = new TableType($3, $6);
$$ = new TableType({AdoptRef{}, $3}, {AdoptRef{}, $6});
}
| TOK_SET '[' type_list ']'
{
set_location(@1, @4);
$$ = new SetType($3, 0);
$$ = new SetType({AdoptRef{}, $3}, nullptr);
}
| TOK_RECORD '{'