Deprecate Expr::Type(), replace with GetType()

This commit is contained in:
Jon Siwek 2020-05-11 13:09:01 -07:00
parent 1eb723fc9d
commit e3f7b38890
8 changed files with 248 additions and 253 deletions

View file

@ -94,10 +94,10 @@ void Attr::DescribeReST(ODesc* d, bool shorten) const
d->Add("`"); d->Add("`");
} }
else if ( expr->Type()->Tag() == TYPE_FUNC ) else if ( expr->GetType()->Tag() == TYPE_FUNC )
{ {
d->Add(":zeek:type:`"); d->Add(":zeek:type:`");
d->Add(expr->Type()->AsFuncType()->FlavorString()); d->Add(expr->GetType()->AsFuncType()->FlavorString());
d->Add("`"); d->Add("`");
} }
@ -262,7 +262,7 @@ void Attributes::CheckAttr(Attr* a)
{ {
bool is_add = a->Tag() == ATTR_ADD_FUNC; bool is_add = a->Tag() == ATTR_ADD_FUNC;
BroType* at = a->AttrExpr()->Type(); const auto& at = a->AttrExpr()->GetType();
if ( at->Tag() != TYPE_FUNC ) if ( at->Tag() != TYPE_FUNC )
{ {
a->AttrExpr()->Error( a->AttrExpr()->Error(
@ -294,7 +294,7 @@ void Attributes::CheckAttr(Attr* a)
break; break;
} }
BroType* atype = a->AttrExpr()->Type(); BroType* atype = a->AttrExpr()->GetType().get();
if ( type->Tag() != TYPE_TABLE || (type->IsSet() && ! in_record) ) if ( type->Tag() != TYPE_TABLE || (type->IsSet() && ! in_record) )
{ {
@ -448,10 +448,10 @@ void Attributes::CheckAttr(Attr* a)
const Expr* expire_func = a->AttrExpr(); 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"); 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 ) if ( e_ft->Yield()->Tag() != TYPE_INTERVAL )
{ {
@ -495,10 +495,10 @@ void Attributes::CheckAttr(Attr* a)
const Expr* change_func = a->AttrExpr(); 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"); 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 ) if ( c_ft->Yield()->Tag() != TYPE_VOID )
{ {
@ -588,7 +588,7 @@ void Attributes::CheckAttr(Attr* a)
break; break;
} }
BroType* atype = a->AttrExpr()->Type(); const auto& atype = a->AttrExpr()->GetType();
if ( atype->Tag() != TYPE_STRING ) { if ( atype->Tag() != TYPE_STRING ) {
Error("type column needs to have a string argument"); Error("type column needs to have a string argument");

File diff suppressed because it is too large Load diff

View file

@ -75,7 +75,16 @@ struct function_ingredients;
class Expr : public BroObj { class Expr : public BroObj {
public: public:
[[deprecated("Remove in v4.1. Use GetType().")]]
BroType* Type() const { return type.get(); } BroType* Type() const { return type.get(); }
const IntrusivePtr<BroType>& GetType() const
{ return type; }
template <class T>
IntrusivePtr<T> GetType() const
{ return cast_intrusive<T>(type); }
BroExprTag Tag() const { return tag; } BroExprTag Tag() const { return tag; }
Expr* Ref() { ::Ref(this); return this; } Expr* Ref() { ::Ref(this); return this; }
@ -946,4 +955,4 @@ std::optional<std::vector<IntrusivePtr<Val>>> eval_list(Frame* f, const ListExpr
extern bool expr_greater(const Expr* e1, const Expr* e2); extern bool expr_greater(const Expr* e1, const Expr* e2);
// True if the given Val* has a vector type // 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; }

View file

@ -795,7 +795,7 @@ bool check_built_in_call(BuiltinFunc* f, CallExpr* call)
} }
const Expr* fmt_str_arg = args[0]; 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"); call->Error("first argument to fmt() needs to be a format string");
return false; return false;

View file

@ -136,7 +136,7 @@ ExprListStmt::ExprListStmt(BroStmtTag t, IntrusivePtr<ListExpr> arg_l)
const expr_list& e = l->Exprs(); const expr_list& e = l->Exprs();
for ( const auto& expr : e ) for ( const auto& expr : e )
{ {
const BroType* t = expr->Type(); const auto& t = expr->GetType();
if ( ! t || t->Tag() == TYPE_VOID ) if ( ! t || t->Tag() == TYPE_VOID )
Error("value of type void illegal"); Error("value of type void illegal");
} }
@ -366,7 +366,7 @@ IfStmt::IfStmt(IntrusivePtr<Expr> test,
: ExprStmt(STMT_IF, std::move(test)), : ExprStmt(STMT_IF, std::move(test)),
s1(std::move(arg_s1)), s2(std::move(arg_s2)) 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"); e->Error("conditional in test must be boolean");
const Location* loc1 = s1->GetLocationInfo(); const Location* loc1 = s1->GetLocationInfo();
@ -580,7 +580,7 @@ static void int_del_func(void* v)
void SwitchStmt::Init() void SwitchStmt::Init()
{ {
auto t = make_intrusive<TypeList>(); auto t = make_intrusive<TypeList>();
t->Append({NewRef{}, e->Type()}); t->Append(e->GetType());
comp_hash = new CompositeHash(std::move(t)); comp_hash = new CompositeHash(std::move(t));
case_label_value_map.SetDeleteFunc(int_del_func); case_label_value_map.SetDeleteFunc(int_del_func);
@ -605,10 +605,10 @@ SwitchStmt::SwitchStmt(IntrusivePtr<Expr> index, case_list* arg_cases)
{ {
have_exprs = true; 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"); 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()); le->Error("case expression type differs from switch type", e.get());
continue; continue;
@ -679,7 +679,7 @@ SwitchStmt::SwitchStmt(IntrusivePtr<Expr> index, case_list* arg_cases)
{ {
const auto& ct = t->GetType(); 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"); c->Error("cannot cast switch expression to case type");
continue; continue;
@ -724,7 +724,7 @@ bool SwitchStmt::AddCaseLabelValueMapping(const Val* v, int idx)
{ {
reporter->PushLocation(e->GetLocationInfo()); reporter->PushLocation(e->GetLocationInfo());
reporter->InternalError("switch expression type mismatch (%s/%s)", 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); int* label_idx = case_label_value_map.Lookup(hk);
@ -768,7 +768,7 @@ std::pair<int, ID*> SwitchStmt::FindCaseLabelMatch(const Val* v) const
{ {
reporter->PushLocation(e->GetLocationInfo()); reporter->PushLocation(e->GetLocationInfo());
reporter->Error("switch expression type mismatch (%s/%s)", 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); return std::make_pair(-1, nullptr);
} }
@ -987,7 +987,7 @@ WhileStmt::WhileStmt(IntrusivePtr<Expr> arg_loop_condition,
: loop_condition(std::move(arg_loop_condition)), body(std::move(arg_body)) : loop_condition(std::move(arg_loop_condition)), body(std::move(arg_body))
{ {
if ( ! loop_condition->IsError() && if ( ! loop_condition->IsError() &&
! IsBool(loop_condition->Type()->Tag()) ) ! IsBool(loop_condition->GetType()->Tag()) )
loop_condition->Error("while conditional must be boolean"); loop_condition->Error("while conditional must be boolean");
} }
@ -1067,9 +1067,9 @@ ForStmt::ForStmt(id_list* arg_loop_vars, IntrusivePtr<Expr> loop_expr)
loop_vars = arg_loop_vars; loop_vars = arg_loop_vars;
body = nullptr; 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<int>(indices.size()) != loop_vars->length() ) if ( static_cast<int>(indices.size()) != loop_vars->length() )
{ {
@ -1097,7 +1097,7 @@ ForStmt::ForStmt(id_list* arg_loop_vars, IntrusivePtr<Expr> loop_expr)
} }
} }
else if ( e->Type()->Tag() == TYPE_VECTOR ) else if ( e->GetType()->Tag() == TYPE_VECTOR )
{ {
if ( loop_vars->length() != 1 ) if ( loop_vars->length() != 1 )
{ {
@ -1118,7 +1118,7 @@ ForStmt::ForStmt(id_list* arg_loop_vars, IntrusivePtr<Expr> loop_expr)
} }
} }
else if ( e->Type()->Tag() == TYPE_STRING ) else if ( e->GetType()->Tag() == TYPE_STRING )
{ {
if ( loop_vars->length() != 1 ) if ( loop_vars->length() != 1 )
{ {
@ -1149,9 +1149,9 @@ ForStmt::ForStmt(id_list* arg_loop_vars,
{ {
value_var = std::move(val_var); 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 // Verify value_vars type if its already been defined
if ( value_var->GetType() ) if ( value_var->GetType() )
@ -1432,7 +1432,7 @@ ReturnStmt::ReturnStmt(IntrusivePtr<Expr> arg_e)
{ {
if ( e ) if ( e )
{ {
ft->SetYieldType({NewRef{}, e->Type()}); ft->SetYieldType(e->GetType());
s->ScopeID()->SetInferReturnType(false); s->ScopeID()->SetInferReturnType(false);
} }
} }
@ -1752,7 +1752,7 @@ WhenStmt::WhenStmt(IntrusivePtr<Expr> arg_cond,
assert(cond); assert(cond);
assert(s1); assert(s1);
if ( ! cond->IsError() && ! IsBool(cond->Type()->Tag()) ) if ( ! cond->IsError() && ! IsBool(cond->GetType()->Tag()) )
cond->Error("conditional in test must be boolean"); cond->Error("conditional in test must be boolean");
if ( timeout ) if ( timeout )
@ -1760,7 +1760,7 @@ WhenStmt::WhenStmt(IntrusivePtr<Expr> arg_cond,
if ( timeout->IsError() ) if ( timeout->IsError() )
return; return;
TypeTag bt = timeout->Type()->Tag(); TypeTag bt = timeout->GetType()->Tag();
if ( bt != TYPE_TIME && bt != TYPE_INTERVAL ) if ( bt != TYPE_TIME && bt != TYPE_INTERVAL )
cond->Error("when timeout requires a time or time interval"); cond->Error("when timeout requires a time or time interval");
} }

View file

@ -228,7 +228,7 @@ int IndexType::MatchesIndex(ListExpr* const index) const
const expr_list& exprs = index->Exprs(); const expr_list& exprs = index->Exprs();
if ( types.size() == 1 && types[0]->Tag() == TYPE_SUBNET && 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 MATCHES_INDEX_SCALAR;
return check_and_promote_exprs(index, Indices()) ? return check_and_promote_exprs(index, Indices()) ?
@ -366,7 +366,7 @@ SetType::SetType(IntrusivePtr<TypeList> ind, IntrusivePtr<ListExpr> arg_elements
} }
else else
{ {
TypeList* tl_type = elements->Type()->AsTypeList(); TypeList* tl_type = elements->GetType()->AsTypeList();
const auto& tl = tl_type->Types(); const auto& tl = tl_type->Types();
if ( tl.size() < 1 ) if ( tl.size() < 1 )
@ -1355,13 +1355,13 @@ int VectorType::MatchesIndex(ListExpr* const index) const
if ( el.length() == 2 ) if ( el.length() == 2 )
return MATCHES_INDEX_VECTOR; return MATCHES_INDEX_VECTOR;
else if ( el[0]->Type()->Tag() == TYPE_VECTOR ) else if ( el[0]->GetType()->Tag() == TYPE_VECTOR )
return (IsIntegral(el[0]->Type()->Yield()->Tag()) || return (IsIntegral(el[0]->GetType()->Yield()->Tag()) ||
IsBool(el[0]->Type()->Yield()->Tag())) ? IsBool(el[0]->GetType()->Yield()->Tag())) ?
MATCHES_INDEX_VECTOR : DOES_NOT_MATCH_INDEX; MATCHES_INDEX_VECTOR : DOES_NOT_MATCH_INDEX;
else else
return (IsIntegral(el[0]->Type()->Tag()) || return (IsIntegral(el[0]->GetType()->Tag()) ||
IsBool(el[0]->Type()->Tag())) ? IsBool(el[0]->GetType()->Tag())) ?
MATCHES_INDEX_SCALAR : DOES_NOT_MATCH_INDEX; MATCHES_INDEX_SCALAR : DOES_NOT_MATCH_INDEX;
} }
@ -1955,7 +1955,7 @@ IntrusivePtr<BroType> merge_types(const BroType* t1, const BroType* t2)
IntrusivePtr<BroType> merge_type_list(ListExpr* elements) IntrusivePtr<BroType> merge_type_list(ListExpr* elements)
{ {
TypeList* tl_type = elements->Type()->AsTypeList(); TypeList* tl_type = elements->GetType()->AsTypeList();
const auto& tl = tl_type->Types(); const auto& tl = tl_type->Types();
if ( tl.size() < 1 ) if ( tl.size() < 1 )

View file

@ -1483,7 +1483,7 @@ void TableVal::CheckExpireAttr(attr_tag at)
{ {
expire_time = {NewRef{}, a->AttrExpr()}; expire_time = {NewRef{}, a->AttrExpr()};
if ( expire_time->Type()->Tag() != TYPE_INTERVAL ) if ( expire_time->GetType()->Tag() != TYPE_INTERVAL )
{ {
if ( ! expire_time->IsError() ) if ( ! expire_time->IsError() )
expire_time->SetError("expiration interval has wrong type"); expire_time->SetError("expiration interval has wrong type");
@ -1807,10 +1807,10 @@ IntrusivePtr<Val> TableVal::Default(Val* index)
if ( ! def_val ) if ( ! def_val )
{ {
const auto& ytype = GetType()->Yield(); 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 && 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(), record_promotion_compatible(dtype->AsRecordType(),
ytype->AsRecordType()) ) ytype->AsRecordType()) )
{ {
@ -2303,10 +2303,10 @@ void TableVal::InitDefaultFunc(Frame* f)
return; return;
const auto& ytype = GetType()->Yield(); 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 && 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(), record_promotion_compatible(dtype->AsRecordType(),
ytype->AsRecordType()) ) ytype->AsRecordType()) )
return; // TableVal::Default will handle this. return; // TableVal::Default will handle this.

View file

@ -231,7 +231,7 @@ static bool expr_is_table_type_name(const Expr* expr)
if ( expr->Tag() != EXPR_NAME ) if ( expr->Tag() != EXPR_NAME )
return false; return false;
BroType* type = expr->Type(); const auto& type = expr->GetType();
if ( type->IsTable() ) if ( type->IsTable() )
return true; return true;
@ -743,7 +743,7 @@ expr:
set_location(@1, @3); set_location(@1, @3);
IntrusivePtr<Expr> e{AdoptRef{}, $2}; IntrusivePtr<Expr> e{AdoptRef{}, $2};
if ( IsIntegral(e->Type()->Tag()) ) if ( IsIntegral(e->GetType()->Tag()) )
e = make_intrusive<ArithCoerceExpr>(std::move(e), TYPE_INT); e = make_intrusive<ArithCoerceExpr>(std::move(e), TYPE_INT);
$$ = new SizeExpr(std::move(e)); $$ = new SizeExpr(std::move(e));
@ -1323,7 +1323,7 @@ index_slice:
make_intrusive<SizeExpr>( make_intrusive<SizeExpr>(
IntrusivePtr<Expr>{NewRef{}, $1}); IntrusivePtr<Expr>{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"); reporter->Error("slice notation must have integral values as indexes");
auto le = make_intrusive<ListExpr>(std::move(low)); auto le = make_intrusive<ListExpr>(std::move(low));