From e3f7b388908e839c7690c649b973b45bc88edcb2 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Mon, 11 May 2020 13:09:01 -0700 Subject: [PATCH] Deprecate Expr::Type(), replace with GetType() --- src/Attr.cc | 18 +-- src/Expr.cc | 402 +++++++++++++++++++++++++--------------------------- src/Expr.h | 11 +- src/Func.cc | 2 +- src/Stmt.cc | 36 ++--- src/Type.cc | 16 +-- src/Val.cc | 10 +- src/parse.y | 6 +- 8 files changed, 248 insertions(+), 253 deletions(-) diff --git a/src/Attr.cc b/src/Attr.cc index 747994a396..d26fa443b5 100644 --- a/src/Attr.cc +++ b/src/Attr.cc @@ -94,10 +94,10 @@ void Attr::DescribeReST(ODesc* d, bool shorten) const d->Add("`"); } - else if ( expr->Type()->Tag() == TYPE_FUNC ) + else if ( expr->GetType()->Tag() == TYPE_FUNC ) { d->Add(":zeek:type:`"); - d->Add(expr->Type()->AsFuncType()->FlavorString()); + d->Add(expr->GetType()->AsFuncType()->FlavorString()); d->Add("`"); } @@ -262,7 +262,7 @@ void Attributes::CheckAttr(Attr* a) { bool is_add = a->Tag() == ATTR_ADD_FUNC; - BroType* at = a->AttrExpr()->Type(); + const auto& at = a->AttrExpr()->GetType(); if ( at->Tag() != TYPE_FUNC ) { a->AttrExpr()->Error( @@ -294,7 +294,7 @@ void Attributes::CheckAttr(Attr* a) break; } - BroType* atype = a->AttrExpr()->Type(); + BroType* atype = a->AttrExpr()->GetType().get(); if ( type->Tag() != TYPE_TABLE || (type->IsSet() && ! in_record) ) { @@ -448,10 +448,10 @@ void Attributes::CheckAttr(Attr* a) const Expr* expire_func = a->AttrExpr(); - if ( expire_func->Type()->Tag() != TYPE_FUNC ) + if ( expire_func->GetType()->Tag() != TYPE_FUNC ) Error("&expire_func attribute is not a function"); - const FuncType* e_ft = expire_func->Type()->AsFuncType(); + const FuncType* e_ft = expire_func->GetType()->AsFuncType(); if ( e_ft->Yield()->Tag() != TYPE_INTERVAL ) { @@ -495,10 +495,10 @@ void Attributes::CheckAttr(Attr* a) const Expr* change_func = a->AttrExpr(); - if ( change_func->Type()->Tag() != TYPE_FUNC || change_func->Type()->AsFuncType()->Flavor() != FUNC_FLAVOR_FUNCTION ) + if ( change_func->GetType()->Tag() != TYPE_FUNC || change_func->GetType()->AsFuncType()->Flavor() != FUNC_FLAVOR_FUNCTION ) Error("&on_change attribute is not a function"); - const FuncType* c_ft = change_func->Type()->AsFuncType(); + const FuncType* c_ft = change_func->GetType()->AsFuncType(); if ( c_ft->Yield()->Tag() != TYPE_VOID ) { @@ -588,7 +588,7 @@ void Attributes::CheckAttr(Attr* a) break; } - BroType* atype = a->AttrExpr()->Type(); + const auto& atype = a->AttrExpr()->GetType(); if ( atype->Tag() != TYPE_STRING ) { Error("type column needs to have a string argument"); diff --git a/src/Expr.cc b/src/Expr.cc index 85404a7991..4b794d606e 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -357,10 +357,10 @@ IntrusivePtr UnaryExpr::Eval(Frame* f) const VectorVal* v_op = v->AsVectorVal(); IntrusivePtr out_t; - if ( Type()->Tag() == TYPE_ANY ) + if ( GetType()->Tag() == TYPE_ANY ) out_t = v->GetType(); else - out_t = {NewRef{}, Type()->AsVectorType()}; + out_t = GetType(); auto result = make_intrusive(std::move(out_t)); @@ -421,7 +421,7 @@ void UnaryExpr::ExprDescribe(ODesc* d) const if ( d->IsReadable() && is_coerce ) { d->Add(" to "); - Type()->Describe(d); + GetType()->Describe(d); d->Add(")"); } } @@ -455,7 +455,7 @@ IntrusivePtr BinaryExpr::Eval(Frame* f) const return nullptr; } - auto v_result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto v_result = make_intrusive(GetType()); for ( unsigned int i = 0; i < v_op1->Size(); ++i ) { @@ -469,10 +469,10 @@ IntrusivePtr BinaryExpr::Eval(Frame* f) const return v_result; } - if ( IsVector(Type()->Tag()) && (is_vec1 || is_vec2) ) + if ( IsVector(GetType()->Tag()) && (is_vec1 || is_vec2) ) { // fold vector against scalar VectorVal* vv = (is_vec1 ? v1 : v2)->AsVectorVal(); - auto v_result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto v_result = make_intrusive(GetType()); for ( unsigned int i = 0; i < vv->Size(); ++i ) { @@ -674,10 +674,7 @@ IntrusivePtr BinaryExpr::Fold(Val* v1, Val* v2) const BadTag("BinaryExpr::Fold", expr_name(tag)); } - BroType* ret_type = Type(); - - if ( IsVector(ret_type->Tag()) ) - ret_type = ret_type->Yield().get(); + const auto& ret_type = IsVector(GetType()->Tag()) ? GetType()->Yield() : GetType(); if ( ret_type->Tag() == TYPE_INTERVAL ) return make_intrusive(d3, 1.0); @@ -857,16 +854,16 @@ void BinaryExpr::SwapOps() void BinaryExpr::PromoteOps(TypeTag t) { - TypeTag bt1 = op1->Type()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); bool is_vec1 = IsVector(bt1); bool is_vec2 = IsVector(bt2); if ( is_vec1 ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); if ( is_vec2 ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); if ( (is_vec1 || is_vec2) && ! (is_vec1 && is_vec2) ) reporter->Warning("mixing vector and scalar operands is deprecated"); @@ -893,8 +890,7 @@ CloneExpr::CloneExpr(IntrusivePtr arg_op) if ( IsError() ) return; - BroType* t = op->Type(); - SetType({NewRef{}, t}); + SetType(op->GetType()); } IntrusivePtr CloneExpr::Eval(Frame* f) const @@ -919,7 +915,7 @@ IncrExpr::IncrExpr(BroExprTag arg_tag, IntrusivePtr arg_op) if ( IsError() ) return; - BroType* t = op->Type(); + const auto& t = op->GetType(); if ( IsVector(t->Tag()) ) { @@ -928,7 +924,7 @@ IncrExpr::IncrExpr(BroExprTag arg_tag, IntrusivePtr arg_op) else { reporter->Warning("increment/decrement operations for vectors deprecated"); - SetType({NewRef{}, t}); + SetType(t); } } else @@ -936,7 +932,7 @@ IncrExpr::IncrExpr(BroExprTag arg_tag, IntrusivePtr arg_op) if ( ! IsIntegral(t->Tag()) ) ExprError("requires an integral operand"); else - SetType({NewRef{}, t}); + SetType(t); } } @@ -955,9 +951,7 @@ IntrusivePtr IncrExpr::DoSingleEval(Frame* f, Val* v) const RuntimeError("count underflow"); } - BroType* ret_type = Type(); - if ( IsVector(ret_type->Tag()) ) - ret_type = Type()->Yield().get(); + const auto& ret_type = IsVector(GetType()->Tag()) ? GetType()->Yield() : GetType(); if ( ret_type->Tag() == TYPE_INT ) return val_mgr->Int(k); @@ -1009,7 +1003,7 @@ ComplementExpr::ComplementExpr(IntrusivePtr arg_op) if ( IsError() ) return; - BroType* t = op->Type(); + const auto& t = op->GetType(); TypeTag bt = t->Tag(); if ( bt != TYPE_COUNT ) @@ -1029,7 +1023,7 @@ NotExpr::NotExpr(IntrusivePtr arg_op) if ( IsError() ) return; - TypeTag bt = op->Type()->Tag(); + TypeTag bt = op->GetType()->Tag(); if ( ! IsIntegral(bt) && bt != TYPE_BOOL ) ExprError("requires an integral or boolean operand"); @@ -1048,10 +1042,7 @@ PosExpr::PosExpr(IntrusivePtr arg_op) if ( IsError() ) return; - BroType* t = op->Type(); - - if ( IsVector(t->Tag()) ) - t = t->AsVectorType()->Yield().get(); + const auto& t = IsVector(op->GetType()->Tag()) ? op->GetType()->Yield() : op->GetType(); TypeTag bt = t->Tag(); IntrusivePtr base_result_type; @@ -1060,7 +1051,7 @@ PosExpr::PosExpr(IntrusivePtr arg_op) // Promote count and counter to int. base_result_type = base_type(TYPE_INT); else if ( bt == TYPE_INTERVAL || bt == TYPE_DOUBLE ) - base_result_type = {NewRef{}, t}; + base_result_type = t; else ExprError("requires an integral or double operand"); @@ -1086,10 +1077,7 @@ NegExpr::NegExpr(IntrusivePtr arg_op) if ( IsError() ) return; - BroType* t = op->Type(); - - if ( IsVector(t->Tag()) ) - t = t->AsVectorType()->Yield().get(); + const auto& t = IsVector(op->GetType()->Tag()) ? op->GetType()->Yield() : op->GetType(); TypeTag bt = t->Tag(); IntrusivePtr base_result_type; @@ -1098,7 +1086,7 @@ NegExpr::NegExpr(IntrusivePtr arg_op) // Promote count and counter to int. base_result_type = base_type(TYPE_INT); else if ( bt == TYPE_INTERVAL || bt == TYPE_DOUBLE ) - base_result_type = {NewRef{}, t}; + base_result_type = t; else ExprError("requires an integral or double operand"); @@ -1124,7 +1112,7 @@ SizeExpr::SizeExpr(IntrusivePtr arg_op) if ( IsError() ) return; - if ( op->Type()->InternalType() == TYPE_INTERNAL_DOUBLE ) + if ( op->GetType()->InternalType() == TYPE_INTERNAL_DOUBLE ) SetType(base_type(TYPE_DOUBLE)); else SetType(base_type(TYPE_COUNT)); @@ -1151,15 +1139,15 @@ AddExpr::AddExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) if ( IsError() ) return; - TypeTag bt1 = op1->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); if ( IsVector(bt1) ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( IsVector(bt2) ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); IntrusivePtr base_result_type; @@ -1186,8 +1174,8 @@ AddExpr::AddExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) void AddExpr::Canonicize() { if ( expr_greater(op2.get(), op1.get()) || - (op1->Type()->Tag() == TYPE_INTERVAL && - op2->Type()->Tag() == TYPE_TIME) || + (op1->GetType()->Tag() == TYPE_INTERVAL && + op2->GetType()->Tag() == TYPE_TIME) || (op2->IsConst() && ! is_vector(op2->ExprVal()) && ! op1->IsConst())) SwapOps(); } @@ -1200,8 +1188,8 @@ AddToExpr::AddToExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) if ( IsError() ) return; - TypeTag bt1 = op1->Type()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( BothArithmetic(bt1, bt2) ) PromoteType(max_type(bt1, bt2), is_vector(op1.get()) || is_vector(op2.get())); @@ -1210,7 +1198,7 @@ AddToExpr::AddToExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) else if ( IsVector(bt1) ) { - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); if ( IsArithmetic(bt1) ) { @@ -1219,7 +1207,7 @@ AddToExpr::AddToExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) if ( bt2 != bt1 ) op2 = make_intrusive(std::move(op2), bt1); - SetType({NewRef{}, op1->Type()}); + SetType(op1->GetType()); } else @@ -1231,7 +1219,7 @@ AddToExpr::AddToExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) type_name(bt1), type_name(bt2))); else - SetType({NewRef{}, op1->Type()}); + SetType(op1->GetType()); } else @@ -1275,8 +1263,8 @@ SubExpr::SubExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) if ( IsError() ) return; - const BroType* t1 = op1->Type(); - const BroType* t2 = op2->Type(); + const auto& t1 = op1->GetType(); + const auto& t2 = op2->GetType(); TypeTag bt1 = t1->Tag(); if ( IsVector(bt1) ) @@ -1296,8 +1284,8 @@ SubExpr::SubExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) else if ( t1->IsSet() && t2->IsSet() ) { - if ( same_type(t1, t2) ) - SetType({NewRef{}, op1->Type()}); + if ( same_type(t1.get(), t2.get()) ) + SetType(op1->GetType()); else ExprError("incompatible \"set\" operands"); } @@ -1324,8 +1312,8 @@ RemoveFromExpr::RemoveFromExpr(IntrusivePtr arg_op1, if ( IsError() ) return; - TypeTag bt1 = op1->Type()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( BothArithmetic(bt1, bt2) ) PromoteType(max_type(bt1, bt2), is_vector(op1.get()) || is_vector(op2.get())); @@ -1364,15 +1352,15 @@ TimesExpr::TimesExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) Canonicize(); - TypeTag bt1 = op1->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); if ( IsVector(bt1) ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( IsVector(bt2) ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); if ( bt1 == TYPE_INTERVAL || bt2 == TYPE_INTERVAL ) { @@ -1389,7 +1377,7 @@ TimesExpr::TimesExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) void TimesExpr::Canonicize() { - if ( expr_greater(op2.get(), op1.get()) || op2->Type()->Tag() == TYPE_INTERVAL || + if ( expr_greater(op2.get(), op1.get()) || op2->GetType()->Tag() == TYPE_INTERVAL || (op2->IsConst() && ! is_vector(op2->ExprVal()) && ! op1->IsConst()) ) SwapOps(); } @@ -1401,15 +1389,15 @@ DivideExpr::DivideExpr(IntrusivePtr arg_op1, if ( IsError() ) return; - TypeTag bt1 = op1->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); if ( IsVector(bt1) ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( IsVector(bt2) ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); if ( bt1 == TYPE_INTERVAL || bt2 == TYPE_INTERVAL ) { @@ -1468,15 +1456,15 @@ ModExpr::ModExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) if ( IsError() ) return; - TypeTag bt1 = op1->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); if ( IsVector(bt1) ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( IsVector(bt2) ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); if ( BothIntegral(bt1, bt2) ) PromoteType(max_type(bt1, bt2), is_vector(op1.get()) || is_vector(op2.get())); @@ -1491,15 +1479,15 @@ BoolExpr::BoolExpr(BroExprTag arg_tag, if ( IsError() ) return; - TypeTag bt1 = op1->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); if ( IsVector(bt1) ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( IsVector(bt2) ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); if ( BothBool(bt1, bt2) ) { @@ -1584,7 +1572,7 @@ IntrusivePtr BoolExpr::Eval(Frame* f) const if ( scalar_v->IsZero() == is_and ) { - result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + result = make_intrusive(GetType()); result->Resize(vector_v->Size()); result->AssignRepeat(0, result->Size(), scalar_v.get()); } @@ -1609,7 +1597,7 @@ IntrusivePtr BoolExpr::Eval(Frame* f) const return nullptr; } - auto result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto result = make_intrusive(GetType()); result->Resize(vec_v1->Size()); for ( unsigned int i = 0; i < vec_v1->Size(); ++i ) @@ -1638,8 +1626,8 @@ BitExpr::BitExpr(BroExprTag arg_tag, if ( IsError() ) return; - const BroType* t1 = op1->Type(); - const BroType* t2 = op2->Type(); + const auto& t1 = op1->GetType(); + const auto& t2 = op2->GetType(); TypeTag bt1 = t1->Tag(); @@ -1674,8 +1662,8 @@ BitExpr::BitExpr(BroExprTag arg_tag, else if ( t1->IsSet() && t2->IsSet() ) { - if ( same_type(t1, t2) ) - SetType({NewRef{}, op1->Type()}); + if ( same_type(t1.get(), t2.get()) ) + SetType(op1->GetType()); else ExprError("incompatible \"set\" operands"); } @@ -1693,8 +1681,8 @@ EqExpr::EqExpr(BroExprTag arg_tag, Canonicize(); - const BroType* t1 = op1->Type(); - const BroType* t2 = op2->Type(); + const auto& t1 = op1->GetType(); + const auto& t2 = op2->GetType(); TypeTag bt1 = t1->Tag(); if ( IsVector(bt1) ) @@ -1732,14 +1720,14 @@ EqExpr::EqExpr(BroExprTag arg_tag, break; case TYPE_ENUM: - if ( ! same_type(t1, t2) ) + if ( ! same_type(t1.get(), t2.get()) ) ExprError("illegal enum comparison"); break; case TYPE_TABLE: if ( t1->IsSet() && t2->IsSet() ) { - if ( ! same_type(t1, t2) ) + if ( ! same_type(t1.get(), t2.get()) ) ExprError("incompatible sets in comparison"); break; } @@ -1760,10 +1748,10 @@ EqExpr::EqExpr(BroExprTag arg_tag, void EqExpr::Canonicize() { - if ( op2->Type()->Tag() == TYPE_PATTERN ) + if ( op2->GetType()->Tag() == TYPE_PATTERN ) SwapOps(); - else if ( op1->Type()->Tag() == TYPE_PATTERN ) + else if ( op1->GetType()->Tag() == TYPE_PATTERN ) ; else if ( expr_greater(op2.get(), op1.get()) ) @@ -1772,7 +1760,7 @@ void EqExpr::Canonicize() IntrusivePtr EqExpr::Fold(Val* v1, Val* v2) const { - if ( op1->Type()->Tag() == TYPE_PATTERN ) + if ( op1->GetType()->Tag() == TYPE_PATTERN ) { RE_Matcher* re = v1->AsPattern(); const BroString* s = v2->AsString(); @@ -1795,8 +1783,8 @@ RelExpr::RelExpr(BroExprTag arg_tag, Canonicize(); - const BroType* t1 = op1->Type(); - const BroType* t2 = op2->Type(); + const auto& t1 = op1->GetType(); + const auto& t2 = op2->GetType(); TypeTag bt1 = t1->Tag(); if ( IsVector(bt1) ) @@ -1816,7 +1804,7 @@ RelExpr::RelExpr(BroExprTag arg_tag, else if ( t1->IsSet() && t2->IsSet() ) { - if ( ! same_type(t1, t2) ) + if ( ! same_type(t1.get(), t2.get()) ) ExprError("incompatible sets in comparison"); } @@ -1849,10 +1837,10 @@ CondExpr::CondExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2, : Expr(EXPR_COND), op1(std::move(arg_op1)), op2(std::move(arg_op2)), op3(std::move(arg_op3)) { - TypeTag bt1 = op1->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); if ( IsVector(bt1) ) - bt1 = op1->Type()->AsVectorType()->Yield()->Tag(); + bt1 = op1->GetType()->AsVectorType()->Yield()->Tag(); if ( op1->IsError() || op2->IsError() || op3->IsError() ) SetError(); @@ -1862,15 +1850,15 @@ CondExpr::CondExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2, else { - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( is_vector(op2.get()) ) - bt2 = op2->Type()->AsVectorType()->Yield()->Tag(); + bt2 = op2->GetType()->AsVectorType()->Yield()->Tag(); - TypeTag bt3 = op3->Type()->Tag(); + TypeTag bt3 = op3->GetType()->Tag(); if ( IsVector(bt3) ) - bt3 = op3->Type()->AsVectorType()->Yield()->Tag(); + bt3 = op3->GetType()->AsVectorType()->Yield()->Tag(); if ( is_vector(op1.get()) && ! (is_vector(op2.get()) && is_vector(op3.get())) ) { @@ -1898,10 +1886,10 @@ CondExpr::CondExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2, else { if ( IsRecord(bt2) && IsRecord(bt3) && - ! same_type(op2->Type(), op3->Type()) ) + ! same_type(op2->GetType().get(), op3->GetType().get()) ) ExprError("operands must be of the same type"); else - SetType({NewRef{}, op2->Type()}); + SetType(op2->GetType()); } } } @@ -1941,7 +1929,7 @@ IntrusivePtr CondExpr::Eval(Frame* f) const return nullptr; } - auto result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto result = make_intrusive(GetType()); result->Resize(cond->Size()); for ( unsigned int i = 0; i < cond->Size(); ++i ) @@ -1998,10 +1986,10 @@ RefExpr::RefExpr(IntrusivePtr arg_op) if ( IsError() ) return; - if ( ! ::is_assignable(op->Type()) ) + if ( ! ::is_assignable(op->GetType().get()) ) ExprError("illegal assignment target"); else - SetType({NewRef{}, op->Type()}); + SetType(op->GetType()); } IntrusivePtr RefExpr::MakeLvalue() @@ -2030,7 +2018,7 @@ AssignExpr::AssignExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2, if ( arg_val ) SetType(arg_val->GetType()); else - SetType({NewRef{}, op1->Type()}); + SetType(op1->GetType()); if ( is_init ) { @@ -2050,8 +2038,8 @@ AssignExpr::AssignExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2, bool AssignExpr::TypeCheck(attr_list* attrs) { - TypeTag bt1 = op1->Type()->Tag(); - TypeTag bt2 = op2->Type()->Tag(); + TypeTag bt1 = op1->GetType()->Tag(); + TypeTag bt2 = op2->GetType()->Tag(); if ( bt1 == TYPE_LIST && bt2 == TYPE_ANY ) // This is ok because we cannot explicitly declare lists on @@ -2075,10 +2063,9 @@ bool AssignExpr::TypeCheck(attr_list* attrs) } if ( bt1 == TYPE_TABLE && bt2 == bt1 && - op2->Type()->AsTableType()->IsUnspecifiedTable() ) + op2->GetType()->AsTableType()->IsUnspecifiedTable() ) { - op2 = make_intrusive(std::move(op2), - IntrusivePtr{NewRef{}, op1->Type()->AsTableType()}); + op2 = make_intrusive(std::move(op2), op1->GetType()); return true; } @@ -2094,16 +2081,16 @@ bool AssignExpr::TypeCheck(attr_list* attrs) bool empty_list_assignment = (op2->AsListExpr()->Exprs().empty()); - if ( op1->Type()->IsSet() ) + if ( op1->GetType()->IsSet() ) op2 = make_intrusive( IntrusivePtr{NewRef{}, op2->AsListExpr()}, attr_copy); else op2 = make_intrusive( IntrusivePtr{NewRef{}, op2->AsListExpr()}, attr_copy); - if ( ! empty_list_assignment && ! same_type(op1->Type(), op2->Type()) ) + if ( ! empty_list_assignment && ! same_type(op1->GetType().get(), op2->GetType().get()) ) { - if ( op1->Type()->IsSet() ) + if ( op1->GetType()->IsSet() ) ExprError("set type mismatch in assignment"); else ExprError("table type mismatch in assignment"); @@ -2116,10 +2103,9 @@ bool AssignExpr::TypeCheck(attr_list* attrs) if ( bt1 == TYPE_VECTOR ) { - if ( bt2 == bt1 && op2->Type()->AsVectorType()->IsUnspecifiedVector() ) + if ( bt2 == bt1 && op2->GetType()->AsVectorType()->IsUnspecifiedVector() ) { - op2 = make_intrusive(std::move(op2), - IntrusivePtr{NewRef{}, op1->Type()->AsVectorType()}); + op2 = make_intrusive(std::move(op2), op1->GetType()); return true; } @@ -2127,18 +2113,18 @@ bool AssignExpr::TypeCheck(attr_list* attrs) { op2 = make_intrusive( IntrusivePtr{AdoptRef{}, op2.release()->AsListExpr()}, - IntrusivePtr{NewRef{}, op1->Type()}); + op1->GetType()); return true; } } - if ( op1->Type()->Tag() == TYPE_RECORD && - op2->Type()->Tag() == TYPE_RECORD ) + if ( op1->GetType()->Tag() == TYPE_RECORD && + op2->GetType()->Tag() == TYPE_RECORD ) { - if ( same_type(op1->Type(), op2->Type()) ) + if ( same_type(op1->GetType().get(), op2->GetType().get()) ) { - RecordType* rt1 = op1->Type()->AsRecordType(); - RecordType* rt2 = op2->Type()->AsRecordType(); + RecordType* rt1 = op1->GetType()->AsRecordType(); + RecordType* rt2 = op2->GetType()->AsRecordType(); // Make sure the attributes match as well. for ( int i = 0; i < rt1->NumFields(); ++i ) @@ -2153,12 +2139,11 @@ bool AssignExpr::TypeCheck(attr_list* attrs) } // Need to coerce. - op2 = make_intrusive(std::move(op2), - IntrusivePtr{NewRef{}, op1->Type()->AsRecordType()}); + op2 = make_intrusive(std::move(op2), op1->GetType()); return true; } - if ( ! same_type(op1->Type(), op2->Type()) ) + if ( ! same_type(op1->GetType().get(), op2->GetType().get()) ) { if ( bt1 == TYPE_TABLE && bt2 == TYPE_TABLE ) { @@ -2195,7 +2180,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs) int errors_before = reporter->Errors(); op2 = make_intrusive( IntrusivePtr{NewRef{}, ctor_list}, attr_copy, - IntrusivePtr{NewRef{}, op1->Type()}); + op1->GetType()); int errors_after = reporter->Errors(); if ( errors_after > errors_before ) @@ -2234,7 +2219,7 @@ bool AssignExpr::TypeCheckArithmetics(TypeTag bt1, TypeTag bt2) { Warn("dangerous assignment of double to integral"); op2 = make_intrusive(std::move(op2), bt1); - bt2 = op2->Type()->Tag(); + bt2 = op2->GetType()->Tag(); } if ( bt1 == TYPE_INT ) @@ -2285,12 +2270,12 @@ IntrusivePtr AssignExpr::InitType() const return nullptr; } - BroType* tl = op1->Type(); + const auto& tl = op1->GetType(); if ( tl->Tag() != TYPE_LIST ) Internal("inconsistent list expr in AssignExpr::InitType"); return make_intrusive(IntrusivePtr{NewRef{}, tl->AsTypeList()}, - IntrusivePtr{NewRef{}, op2->Type()}); + op2->GetType()); } void AssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) const @@ -2427,7 +2412,7 @@ bool AssignExpr::IsRecordElement(TypeDecl* td) const if ( td ) { const NameExpr* n = (const NameExpr*) op1.get(); - td->type = {NewRef{}, op2->Type()}; + td->type = op2->GetType(); td->id = copy_string(n->Id()->Name()); } @@ -2472,11 +2457,11 @@ IndexExpr::IndexExpr(IntrusivePtr arg_op1, if ( is_slice ) { - if ( ! IsString(op1->Type()->Tag()) && ! IsVector(op1->Type()->Tag()) ) + if ( ! IsString(op1->GetType()->Tag()) && ! IsVector(op1->GetType()->Tag()) ) ExprError("slice notation indexing only supported for strings and vectors currently"); } - else if ( IsString(op1->Type()->Tag()) ) + else if ( IsString(op1->GetType()->Tag()) ) { if ( op2->AsListExpr()->Exprs().length() != 1 ) ExprError("invalid string index expression"); @@ -2485,19 +2470,19 @@ IndexExpr::IndexExpr(IntrusivePtr arg_op1, if ( IsError() ) return; - int match_type = op1->Type()->MatchesIndex(op2->AsListExpr()); + int match_type = op1->GetType()->MatchesIndex(op2->AsListExpr()); if ( match_type == DOES_NOT_MATCH_INDEX ) { std::string error_msg = fmt("expression with type '%s' is not a type that can be indexed", - type_name(op1->Type()->Tag())); + type_name(op1->GetType()->Tag())); SetError(error_msg.data()); } - else if ( ! op1->Type()->Yield() ) + else if ( ! op1->GetType()->Yield() ) { - if ( IsString(op1->Type()->Tag()) && match_type == MATCHES_INDEX_SCALAR ) + if ( IsString(op1->GetType()->Tag()) && match_type == MATCHES_INDEX_SCALAR ) SetType(base_type(TYPE_STRING)); else // It's a set - so indexing it yields void. We don't @@ -2508,10 +2493,10 @@ IndexExpr::IndexExpr(IntrusivePtr arg_op1, } else if ( match_type == MATCHES_INDEX_SCALAR ) - SetType(op1->Type()->Yield()); + SetType(op1->GetType()->Yield()); else if ( match_type == MATCHES_INDEX_VECTOR ) - SetType(make_intrusive(op1->Type()->Yield())); + SetType(make_intrusive(op1->GetType()->Yield())); else ExprError("Unknown MatchesIndex() return value"); @@ -2523,7 +2508,7 @@ bool IndexExpr::CanAdd() const return true; // avoid cascading the error report // "add" only allowed if our type is "set". - return op1->Type()->IsSet(); + return op1->GetType()->IsSet(); } bool IndexExpr::CanDel() const @@ -2531,7 +2516,7 @@ bool IndexExpr::CanDel() const if ( IsError() ) return true; // avoid cascading the error report - return op1->Type()->Tag() == TYPE_TABLE; + return op1->GetType()->Tag() == TYPE_TABLE; } void IndexExpr::Add(Frame* f) @@ -2572,7 +2557,7 @@ void IndexExpr::Delete(Frame* f) IntrusivePtr IndexExpr::MakeLvalue() { - if ( IsString(op1->Type()->Tag()) ) + if ( IsString(op1->GetType()->Tag()) ) ExprError("cannot assign to string index expression"); return make_intrusive(IntrusivePtr{NewRef{}, this}); @@ -2596,7 +2581,7 @@ IntrusivePtr IndexExpr::Eval(Frame* f) const { VectorVal* v_v1 = v1->AsVectorVal(); VectorVal* v_v2 = indv->AsVectorVal(); - auto v_result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto v_result = make_intrusive(GetType()); // Booleans select each element (or not). if ( IsBool(v_v2->GetType()->Yield()->Tag()) ) @@ -2860,11 +2845,11 @@ FieldExpr::FieldExpr(IntrusivePtr arg_op, const char* arg_field_name) if ( IsError() ) return; - if ( ! IsRecord(op->Type()->Tag()) ) + if ( ! IsRecord(op->GetType()->Tag()) ) ExprError("not a record"); else { - RecordType* rt = op->Type()->AsRecordType(); + RecordType* rt = op->GetType()->AsRecordType(); field = rt->FieldOffset(field_name); if ( field < 0 ) @@ -2952,11 +2937,11 @@ HasFieldExpr::HasFieldExpr(IntrusivePtr arg_op, if ( IsError() ) return; - if ( ! IsRecord(op->Type()->Tag()) ) + if ( ! IsRecord(op->GetType()->Tag()) ) ExprError("not a record"); else { - RecordType* rt = op->Type()->AsRecordType(); + RecordType* rt = op->GetType()->AsRecordType(); field = rt->FieldOffset(field_name); if ( field < 0 ) @@ -3016,9 +3001,9 @@ RecordConstructorExpr::RecordConstructorExpr(IntrusivePtr constructor_ } FieldAssignExpr* field = (FieldAssignExpr*) e; - IntrusivePtr field_type{NewRef{}, field->Type()}; + const auto& field_type = field->GetType(); char* field_name = copy_string(field->FieldName()); - record_types->push_back(new TypeDecl(std::move(field_type), field_name)); + record_types->push_back(new TypeDecl(field_type, field_name)); } SetType(make_intrusive(record_types)); @@ -3153,7 +3138,7 @@ IntrusivePtr TableConstructorExpr::Eval(Frame* f) const if ( IsError() ) return nullptr; - auto aggr = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsTableType()}, + auto aggr = make_intrusive(GetType(), IntrusivePtr{NewRef{}, attrs}); const expr_list& exprs = op->AsListExpr()->Exprs(); @@ -3170,10 +3155,11 @@ IntrusivePtr TableConstructorExpr::InitVal(const BroType* t, IntrusivePtrAsTableType(); + auto tt = GetType(); + auto tval = aggr ? IntrusivePtr{AdoptRef{}, aggr.release()->AsTableVal()} : - make_intrusive(IntrusivePtr{NewRef{}, tt}, IntrusivePtr{NewRef{}, attrs}); + make_intrusive(std::move(tt), IntrusivePtr{NewRef{}, attrs}); const expr_list& exprs = op->AsListExpr()->Exprs(); for ( const auto& expr : exprs ) @@ -3281,10 +3267,10 @@ IntrusivePtr SetConstructorExpr::InitVal(const BroType* t, IntrusivePtrAsTableType()->Indices(); - TableType* tt = Type()->AsTableType(); + auto tt = GetType(); auto tval = aggr ? IntrusivePtr{AdoptRef{}, aggr.release()->AsTableVal()} : - make_intrusive(IntrusivePtr{NewRef{}, tt}, IntrusivePtr{NewRef{}, attrs}); + make_intrusive(std::move(tt), IntrusivePtr{NewRef{}, attrs}); const expr_list& exprs = op->AsListExpr()->Exprs(); for ( const auto& e : exprs ) @@ -3356,7 +3342,7 @@ IntrusivePtr VectorConstructorExpr::Eval(Frame* f) const if ( IsError() ) return nullptr; - auto vec = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto vec = make_intrusive(GetType()); const expr_list& exprs = op->AsListExpr()->Exprs(); loop_over_list(exprs, i) @@ -3378,10 +3364,10 @@ IntrusivePtr VectorConstructorExpr::InitVal(const BroType* t, IntrusivePtr< if ( IsError() ) return nullptr; - VectorType* vt = Type()->AsVectorType(); + auto vt = GetType(); auto vec = aggr ? IntrusivePtr{AdoptRef{}, aggr.release()->AsVectorVal()} : - make_intrusive(IntrusivePtr{NewRef{}, vt}); + make_intrusive(std::move(vt)); const expr_list& exprs = op->AsListExpr()->Exprs(); loop_over_list(exprs, i) @@ -3410,7 +3396,7 @@ FieldAssignExpr::FieldAssignExpr(const char* arg_field_name, IntrusivePtr value) : UnaryExpr(EXPR_FIELD_ASSIGN, std::move(value)), field_name(arg_field_name) { - SetType({NewRef{}, op->Type()}); + SetType(op->GetType()); } void FieldAssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f) @@ -3438,7 +3424,7 @@ bool FieldAssignExpr::IsRecordElement(TypeDecl* td) const { if ( td ) { - td->type = {NewRef{}, op->Type()}; + td->type = op->GetType(); td->id = copy_string(field_name.c_str()); } @@ -3459,13 +3445,13 @@ ArithCoerceExpr::ArithCoerceExpr(IntrusivePtr arg_op, TypeTag t) if ( IsError() ) return; - TypeTag bt = op->Type()->Tag(); + TypeTag bt = op->GetType()->Tag(); TypeTag vbt = bt; if ( IsVector(bt) ) { SetType(make_intrusive(base_type(t))); - vbt = op->Type()->AsVectorType()->Yield()->Tag(); + vbt = op->GetType()->AsVectorType()->Yield()->Tag(); } else SetType(base_type(t)); @@ -3510,15 +3496,15 @@ IntrusivePtr ArithCoerceExpr::Fold(Val* v) const // invocation is being done per-element rather than on // the whole vector. Correct the type tag if necessary. if ( type->Tag() == TYPE_VECTOR ) - t = Type()->AsVectorType()->Yield()->InternalType(); + t = GetType()->AsVectorType()->Yield()->InternalType(); return FoldSingleVal(v, t); } - t = Type()->AsVectorType()->Yield()->InternalType(); + t = GetType()->AsVectorType()->Yield()->InternalType(); VectorVal* vv = v->AsVectorVal(); - auto result = make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + auto result = make_intrusive(GetType()); for ( unsigned int i = 0; i < vv->Size(); ++i ) { @@ -3541,16 +3527,16 @@ RecordCoerceExpr::RecordCoerceExpr(IntrusivePtr arg_op, SetType(std::move(r)); - if ( Type()->Tag() != TYPE_RECORD ) + if ( GetType()->Tag() != TYPE_RECORD ) ExprError("coercion to non-record"); - else if ( op->Type()->Tag() != TYPE_RECORD ) + else if ( op->GetType()->Tag() != TYPE_RECORD ) ExprError("coercion of non-record to record"); else { RecordType* t_r = type->AsRecordType(); - RecordType* sub_r = op->Type()->AsRecordType(); + RecordType* sub_r = op->GetType()->AsRecordType(); map_size = t_r->NumFields(); map = new int[map_size]; @@ -3659,7 +3645,7 @@ IntrusivePtr RecordCoerceExpr::InitVal(const BroType* t, IntrusivePtr IntrusivePtr RecordCoerceExpr::Fold(Val* v) const { - auto val = make_intrusive(Type()->AsRecordType()); + auto val = make_intrusive(GetType()->AsRecordType()); RecordType* val_type = val->GetType()->AsRecordType(); RecordVal* rv = v->AsRecordVal(); @@ -3679,7 +3665,7 @@ IntrusivePtr RecordCoerceExpr::Fold(Val* v) const rhs = def->AttrExpr()->Eval(nullptr); } - assert(rhs || Type()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_OPTIONAL)); + assert(rhs || GetType()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_OPTIONAL)); if ( ! rhs ) { @@ -3711,11 +3697,11 @@ IntrusivePtr RecordCoerceExpr::Fold(Val* v) const } else { - if ( const Attr* def = Type()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_DEFAULT) ) + if ( const Attr* def = GetType()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_DEFAULT) ) { auto def_val = def->AttrExpr()->Eval(nullptr); const auto& def_type = def_val->GetType(); - const auto& field_type = Type()->AsRecordType()->GetFieldType(i); + const auto& field_type = GetType()->AsRecordType()->GetFieldType(i); if ( def_type->Tag() == TYPE_RECORD && field_type->Tag() == TYPE_RECORD && @@ -3747,10 +3733,10 @@ TableCoerceExpr::TableCoerceExpr(IntrusivePtr arg_op, SetType(std::move(r)); - if ( Type()->Tag() != TYPE_TABLE ) + if ( GetType()->Tag() != TYPE_TABLE ) ExprError("coercion to non-table"); - else if ( op->Type()->Tag() != TYPE_TABLE ) + else if ( op->GetType()->Tag() != TYPE_TABLE ) ExprError("coercion of non-table/set to table/set"); } @@ -3766,7 +3752,7 @@ IntrusivePtr TableCoerceExpr::Fold(Val* v) const if ( tv->Size() > 0 ) RuntimeErrorWithCallStack("coercion of non-empty table/set"); - return make_intrusive(IntrusivePtr{NewRef{}, Type()->AsTableType()}, + return make_intrusive(GetType(), IntrusivePtr{NewRef{}, tv->Attrs()}); } @@ -3779,10 +3765,10 @@ VectorCoerceExpr::VectorCoerceExpr(IntrusivePtr arg_op, SetType(std::move(v)); - if ( Type()->Tag() != TYPE_VECTOR ) + if ( GetType()->Tag() != TYPE_VECTOR ) ExprError("coercion to non-vector"); - else if ( op->Type()->Tag() != TYPE_VECTOR ) + else if ( op->GetType()->Tag() != TYPE_VECTOR ) ExprError("coercion of non-vector to vector"); } @@ -3798,7 +3784,7 @@ IntrusivePtr VectorCoerceExpr::Fold(Val* v) const if ( vv->Size() > 0 ) RuntimeErrorWithCallStack("coercion of non-empty vector"); - return make_intrusive(IntrusivePtr{NewRef{}, Type()->AsVectorType()}); + return make_intrusive(GetType()); } FlattenExpr::FlattenExpr(IntrusivePtr arg_op) @@ -3807,7 +3793,7 @@ FlattenExpr::FlattenExpr(IntrusivePtr arg_op) if ( IsError() ) return; - BroType* t = op->Type(); + const auto& t = op->GetType(); if ( t->Tag() != TYPE_RECORD ) Internal("bad type in FlattenExpr::FlattenExpr"); @@ -3873,7 +3859,7 @@ ScheduleExpr::ScheduleExpr(IntrusivePtr arg_when, if ( IsError() || when->IsError() || event->IsError() ) return; - TypeTag bt = when->Type()->Tag(); + TypeTag bt = when->GetType()->Tag(); if ( bt != TYPE_TIME && bt != TYPE_INTERVAL ) ExprError("schedule expression requires a time or time interval"); @@ -3898,7 +3884,7 @@ IntrusivePtr ScheduleExpr::Eval(Frame* f) const double dt = when_val->InternalDouble(); - if ( when->Type()->Tag() == TYPE_INTERVAL ) + if ( when->GetType()->Tag() == TYPE_INTERVAL ) dt += network_time; auto args = eval_list(f, event->Args()); @@ -3950,34 +3936,34 @@ InExpr::InExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) if ( IsError() ) return; - if ( op1->Type()->Tag() == TYPE_PATTERN ) + if ( op1->GetType()->Tag() == TYPE_PATTERN ) { - if ( op2->Type()->Tag() != TYPE_STRING ) + if ( op2->GetType()->Tag() != TYPE_STRING ) { - op2->Type()->Error("pattern requires string index", op1.get()); + op2->GetType()->Error("pattern requires string index", op1.get()); SetError(); } else SetType(base_type(TYPE_BOOL)); } - else if ( op1->Type()->Tag() == TYPE_RECORD ) + else if ( op1->GetType()->Tag() == TYPE_RECORD ) { - if ( op2->Type()->Tag() != TYPE_TABLE ) + if ( op2->GetType()->Tag() != TYPE_TABLE ) { - op2->Type()->Error("table/set required"); + op2->GetType()->Error("table/set required"); SetError(); } else { - const BroType* t1 = op1->Type(); + const auto& t1 = op1->GetType(); const TypeList* it = - op2->Type()->AsTableType()->Indices(); + op2->GetType()->AsTableType()->Indices(); - if ( ! same_type(t1, it) ) + if ( ! same_type(t1.get(), it) ) { - t1->Error("indexing mismatch", op2->Type()); + t1->Error("indexing mismatch", op2->GetType().get()); SetError(); } else @@ -3985,8 +3971,8 @@ InExpr::InExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) } } - else if ( op1->Type()->Tag() == TYPE_STRING && - op2->Type()->Tag() == TYPE_STRING ) + else if ( op1->GetType()->Tag() == TYPE_STRING && + op2->GetType()->Tag() == TYPE_STRING ) SetType(base_type(TYPE_BOOL)); else @@ -3994,16 +3980,16 @@ InExpr::InExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) // Check for: in // in set[subnet] // in table[subnet] of ... - if ( op1->Type()->Tag() == TYPE_ADDR ) + if ( op1->GetType()->Tag() == TYPE_ADDR ) { - if ( op2->Type()->Tag() == TYPE_SUBNET ) + if ( op2->GetType()->Tag() == TYPE_SUBNET ) { SetType(base_type(TYPE_BOOL)); return; } - if ( op2->Type()->Tag() == TYPE_TABLE && - op2->Type()->AsTableType()->IsSubNetIndex() ) + if ( op2->GetType()->Tag() == TYPE_TABLE && + op2->GetType()->AsTableType()->IsSubNetIndex() ) { SetType(base_type(TYPE_BOOL)); return; @@ -4015,7 +4001,7 @@ InExpr::InExpr(IntrusivePtr arg_op1, IntrusivePtr arg_op2) ListExpr* lop1 = op1->AsListExpr(); - if ( ! op2->Type()->MatchesIndex(lop1) ) + if ( ! op2->GetType()->MatchesIndex(lop1) ) SetError("not an index type"); else SetType(base_type(TYPE_BOOL)); @@ -4066,7 +4052,7 @@ CallExpr::CallExpr(IntrusivePtr arg_func, return; } - BroType* func_type = func->Type(); + const auto& func_type = func->GetType(); if ( ! IsFunc(func_type->Tag()) ) { @@ -4455,7 +4441,7 @@ ListExpr::~ListExpr() void ListExpr::Append(IntrusivePtr e) { exprs.push_back(e.release()); - ((TypeList*) type.get())->Append({NewRef{}, exprs.back()->Type()}); + ((TypeList*) type.get())->Append(exprs.back()->GetType()); } bool ListExpr::IsPure() const @@ -4522,7 +4508,7 @@ IntrusivePtr ListExpr::InitType() const for ( const auto& e : exprs ) { - BroType* ti = e->Type(); + const auto& ti = e->GetType(); // Collapse any embedded sets or lists. if ( ti->IsSet() || ti->Tag() == TYPE_LIST ) @@ -4538,7 +4524,7 @@ IntrusivePtr ListExpr::InitType() const tl->Append(til->GetPureType()); } else - tl->Append({NewRef{}, ti}); + tl->Append(ti); } return tl; @@ -4702,10 +4688,10 @@ IntrusivePtr ListExpr::AddSetInit(const BroType* t, IntrusivePtr aggr) { IntrusivePtr element; - if ( expr->Type()->IsSet() ) + if ( expr->GetType()->IsSet() ) // A set to flatten. element = expr->Eval(nullptr); - else if ( expr->Type()->Tag() == TYPE_LIST ) + else if ( expr->GetType()->Tag() == TYPE_LIST ) element = expr->InitVal(it, nullptr); else element = expr->InitVal(it->Types()[0].get(), nullptr); @@ -4727,7 +4713,7 @@ IntrusivePtr ListExpr::AddSetInit(const BroType* t, IntrusivePtr aggr) continue; } - if ( expr->Type()->Tag() == TYPE_LIST ) + if ( expr->GetType()->Tag() == TYPE_LIST ) element = check_and_promote(std::move(element), it, true); else element = check_and_promote(std::move(element), it->Types()[0].get(), true); @@ -4795,7 +4781,7 @@ RecordAssignExpr::RecordAssignExpr(const IntrusivePtr& record, { const expr_list& inits = init_list->AsListExpr()->Exprs(); - RecordType* lhs = record->Type()->AsRecordType(); + RecordType* lhs = record->GetType()->AsRecordType(); // The inits have two forms: // 1) other records -- use all matching field names+types @@ -4804,9 +4790,9 @@ RecordAssignExpr::RecordAssignExpr(const IntrusivePtr& record, for ( const auto& init : inits ) { - if ( init->Type()->Tag() == TYPE_RECORD ) + if ( init->GetType()->Tag() == TYPE_RECORD ) { - RecordType* t = init->Type()->AsRecordType(); + RecordType* t = init->GetType()->AsRecordType(); for ( int j = 0; j < t->NumFields(); ++j ) { @@ -4855,11 +4841,11 @@ RecordAssignExpr::RecordAssignExpr(const IntrusivePtr& record, CastExpr::CastExpr(IntrusivePtr arg_op, IntrusivePtr t) : UnaryExpr(EXPR_CAST, std::move(arg_op)) { - auto stype = Op()->Type(); + auto stype = Op()->GetType(); SetType(std::move(t)); - if ( ! can_cast_value_to_type(stype, Type()) ) + if ( ! can_cast_value_to_type(stype.get(), GetType().get()) ) ExprError("cast not supported"); } @@ -4873,7 +4859,7 @@ IntrusivePtr CastExpr::Eval(Frame* f) const if ( ! v ) return nullptr; - auto nv = cast_value_to_type(v.get(), Type()); + auto nv = cast_value_to_type(v.get(), GetType().get()); if ( nv ) return nv; @@ -4882,7 +4868,7 @@ IntrusivePtr CastExpr::Eval(Frame* f) const d.Add("invalid cast of value with type '"); v->GetType()->Describe(&d); d.Add("' to type '"); - Type()->Describe(&d); + GetType()->Describe(&d); d.Add("'"); if ( same_type(v->GetType().get(), bro_broker::DataVal::ScriptDataType()) && @@ -4897,7 +4883,7 @@ void CastExpr::ExprDescribe(ODesc* d) const { Op()->Describe(d); d->Add(" as "); - Type()->Describe(d); + GetType()->Describe(d); } IsExpr::IsExpr(IntrusivePtr arg_op, IntrusivePtr arg_t) @@ -4924,8 +4910,8 @@ void IsExpr::ExprDescribe(ODesc* d) const IntrusivePtr get_assign_expr(IntrusivePtr op1, IntrusivePtr op2, bool is_init) { - if ( op1->Type()->Tag() == TYPE_RECORD && - op2->Type()->Tag() == TYPE_LIST ) + if ( op1->GetType()->Tag() == TYPE_RECORD && + op2->GetType()->Tag() == TYPE_LIST ) return make_intrusive(std::move(op1), std::move(op2), is_init); @@ -4940,7 +4926,7 @@ IntrusivePtr get_assign_expr(IntrusivePtr op1, IntrusivePtr check_and_promote_expr(Expr* const e, BroType* t) { - BroType* et = e->Type(); + const auto& et = e->GetType(); TypeTag e_tag = et->Tag(); TypeTag t_tag = t->Tag(); @@ -4973,7 +4959,7 @@ IntrusivePtr check_and_promote_expr(Expr* const e, BroType* t) RecordType* t_r = t->AsRecordType(); RecordType* et_r = et->AsRecordType(); - if ( same_type(t, et) ) + if ( same_type(t, et.get()) ) { // Make sure the attributes match as well. for ( int i = 0; i < t_r->NumFields(); ++i ) @@ -4996,7 +4982,7 @@ IntrusivePtr check_and_promote_expr(Expr* const e, BroType* t) } - if ( ! same_type(t, et) ) + if ( ! same_type(t, et.get()) ) { if ( t->Tag() == TYPE_TABLE && et->Tag() == TYPE_TABLE && et->AsTableType()->IsUnspecifiedTable() ) diff --git a/src/Expr.h b/src/Expr.h index e540d45b2d..a00304009c 100644 --- a/src/Expr.h +++ b/src/Expr.h @@ -75,7 +75,16 @@ struct function_ingredients; class Expr : public BroObj { public: + [[deprecated("Remove in v4.1. Use GetType().")]] BroType* Type() const { return type.get(); } + + const IntrusivePtr& GetType() const + { return type; } + + template + IntrusivePtr GetType() const + { return cast_intrusive(type); } + BroExprTag Tag() const { return tag; } Expr* Ref() { ::Ref(this); return this; } @@ -946,4 +955,4 @@ std::optional>> eval_list(Frame* f, const ListExpr extern bool expr_greater(const Expr* e1, const Expr* e2); // True if the given Val* has a vector type -inline bool is_vector(Expr* e) { return e->Type()->Tag() == TYPE_VECTOR; } +inline bool is_vector(Expr* e) { return e->GetType()->Tag() == TYPE_VECTOR; } diff --git a/src/Func.cc b/src/Func.cc index ae0fd88809..b60df3f4a3 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -795,7 +795,7 @@ bool check_built_in_call(BuiltinFunc* f, CallExpr* call) } const Expr* fmt_str_arg = args[0]; - if ( fmt_str_arg->Type()->Tag() != TYPE_STRING ) + if ( fmt_str_arg->GetType()->Tag() != TYPE_STRING ) { call->Error("first argument to fmt() needs to be a format string"); return false; diff --git a/src/Stmt.cc b/src/Stmt.cc index f20e154d57..b5d927b2ef 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -136,7 +136,7 @@ ExprListStmt::ExprListStmt(BroStmtTag t, IntrusivePtr arg_l) const expr_list& e = l->Exprs(); for ( const auto& expr : e ) { - const BroType* t = expr->Type(); + const auto& t = expr->GetType(); if ( ! t || t->Tag() == TYPE_VOID ) Error("value of type void illegal"); } @@ -366,7 +366,7 @@ IfStmt::IfStmt(IntrusivePtr test, : ExprStmt(STMT_IF, std::move(test)), s1(std::move(arg_s1)), s2(std::move(arg_s2)) { - if ( ! e->IsError() && ! IsBool(e->Type()->Tag()) ) + if ( ! e->IsError() && ! IsBool(e->GetType()->Tag()) ) e->Error("conditional in test must be boolean"); const Location* loc1 = s1->GetLocationInfo(); @@ -580,7 +580,7 @@ static void int_del_func(void* v) void SwitchStmt::Init() { auto t = make_intrusive(); - t->Append({NewRef{}, e->Type()}); + t->Append(e->GetType()); comp_hash = new CompositeHash(std::move(t)); case_label_value_map.SetDeleteFunc(int_del_func); @@ -605,10 +605,10 @@ SwitchStmt::SwitchStmt(IntrusivePtr index, case_list* arg_cases) { have_exprs = true; - if ( ! is_atomic_type(e->Type()) ) + if ( ! is_atomic_type(e->GetType().get()) ) e->Error("switch expression must be of an atomic type when cases are expressions"); - if ( ! le->Type()->AsTypeList()->AllMatch(e->Type(), false) ) + if ( ! le->GetType()->AsTypeList()->AllMatch(e->GetType().get(), false) ) { le->Error("case expression type differs from switch type", e.get()); continue; @@ -679,7 +679,7 @@ SwitchStmt::SwitchStmt(IntrusivePtr index, case_list* arg_cases) { const auto& ct = t->GetType(); - if ( ! can_cast_value_to_type(e->Type(), ct.get()) ) + if ( ! can_cast_value_to_type(e->GetType().get(), ct.get()) ) { c->Error("cannot cast switch expression to case type"); continue; @@ -724,7 +724,7 @@ bool SwitchStmt::AddCaseLabelValueMapping(const Val* v, int idx) { reporter->PushLocation(e->GetLocationInfo()); reporter->InternalError("switch expression type mismatch (%s/%s)", - type_name(v->GetType()->Tag()), type_name(e->Type()->Tag())); + type_name(v->GetType()->Tag()), type_name(e->GetType()->Tag())); } int* label_idx = case_label_value_map.Lookup(hk); @@ -768,7 +768,7 @@ std::pair SwitchStmt::FindCaseLabelMatch(const Val* v) const { reporter->PushLocation(e->GetLocationInfo()); reporter->Error("switch expression type mismatch (%s/%s)", - type_name(v->GetType()->Tag()), type_name(e->Type()->Tag())); + type_name(v->GetType()->Tag()), type_name(e->GetType()->Tag())); return std::make_pair(-1, nullptr); } @@ -987,7 +987,7 @@ WhileStmt::WhileStmt(IntrusivePtr arg_loop_condition, : loop_condition(std::move(arg_loop_condition)), body(std::move(arg_body)) { if ( ! loop_condition->IsError() && - ! IsBool(loop_condition->Type()->Tag()) ) + ! IsBool(loop_condition->GetType()->Tag()) ) loop_condition->Error("while conditional must be boolean"); } @@ -1067,9 +1067,9 @@ ForStmt::ForStmt(id_list* arg_loop_vars, IntrusivePtr loop_expr) loop_vars = arg_loop_vars; body = nullptr; - if ( e->Type()->Tag() == TYPE_TABLE ) + if ( e->GetType()->Tag() == TYPE_TABLE ) { - const auto& indices = e->Type()->AsTableType()->IndexTypes(); + const auto& indices = e->GetType()->AsTableType()->IndexTypes(); if ( static_cast(indices.size()) != loop_vars->length() ) { @@ -1097,7 +1097,7 @@ ForStmt::ForStmt(id_list* arg_loop_vars, IntrusivePtr loop_expr) } } - else if ( e->Type()->Tag() == TYPE_VECTOR ) + else if ( e->GetType()->Tag() == TYPE_VECTOR ) { if ( loop_vars->length() != 1 ) { @@ -1118,7 +1118,7 @@ ForStmt::ForStmt(id_list* arg_loop_vars, IntrusivePtr loop_expr) } } - else if ( e->Type()->Tag() == TYPE_STRING ) + else if ( e->GetType()->Tag() == TYPE_STRING ) { if ( loop_vars->length() != 1 ) { @@ -1149,9 +1149,9 @@ ForStmt::ForStmt(id_list* arg_loop_vars, { value_var = std::move(val_var); - if ( e->Type()->IsTable() ) + if ( e->GetType()->IsTable() ) { - const auto& yield_type = e->Type()->AsTableType()->Yield(); + const auto& yield_type = e->GetType()->AsTableType()->Yield(); // Verify value_vars type if its already been defined if ( value_var->GetType() ) @@ -1432,7 +1432,7 @@ ReturnStmt::ReturnStmt(IntrusivePtr arg_e) { if ( e ) { - ft->SetYieldType({NewRef{}, e->Type()}); + ft->SetYieldType(e->GetType()); s->ScopeID()->SetInferReturnType(false); } } @@ -1752,7 +1752,7 @@ WhenStmt::WhenStmt(IntrusivePtr arg_cond, assert(cond); assert(s1); - if ( ! cond->IsError() && ! IsBool(cond->Type()->Tag()) ) + if ( ! cond->IsError() && ! IsBool(cond->GetType()->Tag()) ) cond->Error("conditional in test must be boolean"); if ( timeout ) @@ -1760,7 +1760,7 @@ WhenStmt::WhenStmt(IntrusivePtr arg_cond, if ( timeout->IsError() ) return; - TypeTag bt = timeout->Type()->Tag(); + TypeTag bt = timeout->GetType()->Tag(); if ( bt != TYPE_TIME && bt != TYPE_INTERVAL ) cond->Error("when timeout requires a time or time interval"); } diff --git a/src/Type.cc b/src/Type.cc index 122f5370cd..2826a741a4 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -228,7 +228,7 @@ int IndexType::MatchesIndex(ListExpr* const index) const const expr_list& exprs = index->Exprs(); if ( types.size() == 1 && types[0]->Tag() == TYPE_SUBNET && - exprs.length() == 1 && exprs[0]->Type()->Tag() == TYPE_ADDR ) + exprs.length() == 1 && exprs[0]->GetType()->Tag() == TYPE_ADDR ) return MATCHES_INDEX_SCALAR; return check_and_promote_exprs(index, Indices()) ? @@ -366,7 +366,7 @@ SetType::SetType(IntrusivePtr ind, IntrusivePtr arg_elements } else { - TypeList* tl_type = elements->Type()->AsTypeList(); + TypeList* tl_type = elements->GetType()->AsTypeList(); const auto& tl = tl_type->Types(); if ( tl.size() < 1 ) @@ -1355,13 +1355,13 @@ int VectorType::MatchesIndex(ListExpr* const index) const if ( el.length() == 2 ) return MATCHES_INDEX_VECTOR; - else if ( el[0]->Type()->Tag() == TYPE_VECTOR ) - return (IsIntegral(el[0]->Type()->Yield()->Tag()) || - IsBool(el[0]->Type()->Yield()->Tag())) ? + else if ( el[0]->GetType()->Tag() == TYPE_VECTOR ) + return (IsIntegral(el[0]->GetType()->Yield()->Tag()) || + IsBool(el[0]->GetType()->Yield()->Tag())) ? MATCHES_INDEX_VECTOR : DOES_NOT_MATCH_INDEX; else - return (IsIntegral(el[0]->Type()->Tag()) || - IsBool(el[0]->Type()->Tag())) ? + return (IsIntegral(el[0]->GetType()->Tag()) || + IsBool(el[0]->GetType()->Tag())) ? MATCHES_INDEX_SCALAR : DOES_NOT_MATCH_INDEX; } @@ -1955,7 +1955,7 @@ IntrusivePtr merge_types(const BroType* t1, const BroType* t2) IntrusivePtr merge_type_list(ListExpr* elements) { - TypeList* tl_type = elements->Type()->AsTypeList(); + TypeList* tl_type = elements->GetType()->AsTypeList(); const auto& tl = tl_type->Types(); if ( tl.size() < 1 ) diff --git a/src/Val.cc b/src/Val.cc index cd5b0c2934..e1e2dc77ec 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1483,7 +1483,7 @@ void TableVal::CheckExpireAttr(attr_tag at) { expire_time = {NewRef{}, a->AttrExpr()}; - if ( expire_time->Type()->Tag() != TYPE_INTERVAL ) + if ( expire_time->GetType()->Tag() != TYPE_INTERVAL ) { if ( ! expire_time->IsError() ) expire_time->SetError("expiration interval has wrong type"); @@ -1807,10 +1807,10 @@ IntrusivePtr TableVal::Default(Val* index) if ( ! def_val ) { const auto& ytype = GetType()->Yield(); - BroType* dtype = def_attr->AttrExpr()->Type(); + const auto& dtype = def_attr->AttrExpr()->GetType(); if ( dtype->Tag() == TYPE_RECORD && ytype->Tag() == TYPE_RECORD && - ! same_type(dtype, ytype.get()) && + ! same_type(dtype.get(), ytype.get()) && record_promotion_compatible(dtype->AsRecordType(), ytype->AsRecordType()) ) { @@ -2303,10 +2303,10 @@ void TableVal::InitDefaultFunc(Frame* f) return; const auto& ytype = GetType()->Yield(); - BroType* dtype = def_attr->AttrExpr()->Type(); + const auto& dtype = def_attr->AttrExpr()->GetType(); if ( dtype->Tag() == TYPE_RECORD && ytype->Tag() == TYPE_RECORD && - ! same_type(dtype, ytype.get()) && + ! same_type(dtype.get(), ytype.get()) && record_promotion_compatible(dtype->AsRecordType(), ytype->AsRecordType()) ) return; // TableVal::Default will handle this. diff --git a/src/parse.y b/src/parse.y index 645a64e399..5ed3aa4797 100644 --- a/src/parse.y +++ b/src/parse.y @@ -231,7 +231,7 @@ static bool expr_is_table_type_name(const Expr* expr) if ( expr->Tag() != EXPR_NAME ) return false; - BroType* type = expr->Type(); + const auto& type = expr->GetType(); if ( type->IsTable() ) return true; @@ -743,7 +743,7 @@ expr: set_location(@1, @3); IntrusivePtr e{AdoptRef{}, $2}; - if ( IsIntegral(e->Type()->Tag()) ) + if ( IsIntegral(e->GetType()->Tag()) ) e = make_intrusive(std::move(e), TYPE_INT); $$ = new SizeExpr(std::move(e)); @@ -1323,7 +1323,7 @@ index_slice: make_intrusive( IntrusivePtr{NewRef{}, $1}); - if ( ! IsIntegral(low->Type()->Tag()) || ! IsIntegral(high->Type()->Tag()) ) + if ( ! IsIntegral(low->GetType()->Tag()) || ! IsIntegral(high->GetType()->Tag()) ) reporter->Error("slice notation must have integral values as indexes"); auto le = make_intrusive(std::move(low));