GH-1624: Migrate check_and_promote and a few Expr methods to IntrusivePtr

This commit is contained in:
Tim Wojtulewicz 2021-12-13 13:19:12 -07:00
parent 75b7bd0c6e
commit edf90a51e4
7 changed files with 76 additions and 65 deletions

View file

@ -263,7 +263,7 @@ bool Expr::InvertSense()
return false; return false;
} }
void Expr::EvalIntoAggregate(const zeek::Type* /* t */, Val* /* aggr */, Frame* /* f */) const void Expr::EvalIntoAggregate(const TypePtr& /* t */, ValPtr /* aggr */, Frame* /* f */) const
{ {
Internal("Expr::EvalIntoAggregate called"); Internal("Expr::EvalIntoAggregate called");
} }
@ -416,7 +416,7 @@ bool Expr::IsPure() const
return true; return true;
} }
ValPtr Expr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr Expr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( aggr ) if ( aggr )
{ {
@ -2637,7 +2637,7 @@ TypePtr AssignExpr::InitType() const
return make_intrusive<TableType>(IntrusivePtr{NewRef{}, tl->AsTypeList()}, op2->GetType()); return make_intrusive<TableType>(IntrusivePtr{NewRef{}, tl->AsTypeList()}, op2->GetType());
} }
void AssignExpr::EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) const void AssignExpr::EvalIntoAggregate(const TypePtr& t, ValPtr aggr, Frame* f) const
{ {
if ( IsError() ) if ( IsError() )
return; return;
@ -2677,7 +2677,7 @@ void AssignExpr::EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) con
TableVal* tv = aggr->AsTableVal(); TableVal* tv = aggr->AsTableVal();
auto index = op1->Eval(f); auto index = op1->Eval(f);
auto v = check_and_promote(op2->Eval(f), t->Yield().get(), true); auto v = check_and_promote(op2->Eval(f), t->Yield(), true);
if ( ! index || ! v ) if ( ! index || ! v )
return; return;
@ -2686,7 +2686,7 @@ void AssignExpr::EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) con
RuntimeError("type clash in table assignment"); RuntimeError("type clash in table assignment");
} }
ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr AssignExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( ! aggr ) if ( ! aggr )
{ {
@ -2703,7 +2703,7 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
{ {
if ( t->Tag() != TYPE_RECORD ) if ( t->Tag() != TYPE_RECORD )
{ {
Error("not a record initializer", t); Error("not a record initializer", t.get());
return nullptr; return nullptr;
} }
@ -2721,7 +2721,7 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
RecordVal* aggr_r = aggr->AsRecordVal(); RecordVal* aggr_r = aggr->AsRecordVal();
auto v = op2->InitVal(rt->GetFieldType(td.id).get(), nullptr); auto v = op2->InitVal(rt->GetFieldType(td.id), nullptr);
if ( ! v ) if ( ! v )
return nullptr; return nullptr;
@ -2734,7 +2734,7 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
{ {
if ( t->Tag() != TYPE_TABLE ) if ( t->Tag() != TYPE_TABLE )
{ {
Error("not a table initialization", t); Error("not a table initialization", t.get());
return nullptr; return nullptr;
} }
@ -2745,7 +2745,7 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
const TableType* tt = tv->GetType()->AsTableType(); const TableType* tt = tv->GetType()->AsTableType();
const auto& yt = tv->GetType()->Yield(); const auto& yt = tv->GetType()->Yield();
auto index = op1->InitVal(tt->GetIndices().get(), nullptr); auto index = op1->InitVal(tt->GetIndices(), nullptr);
if ( yt->Tag() == TYPE_RECORD ) if ( yt->Tag() == TYPE_RECORD )
{ {
@ -2776,7 +2776,7 @@ ValPtr AssignExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
} }
} }
auto v = op2->InitVal(yt.get(), nullptr); auto v = op2->InitVal(yt, nullptr);
if ( ! index || ! v ) if ( ! index || ! v )
return nullptr; return nullptr;
@ -3372,7 +3372,7 @@ RecordConstructorExpr::RecordConstructorExpr(RecordTypePtr known_rt, ListExprPtr
} }
} }
ValPtr RecordConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr RecordConstructorExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( IsError() ) if ( IsError() )
{ {
@ -3388,8 +3388,7 @@ ValPtr RecordConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
if ( v ) if ( v )
{ {
RecordVal* rv = v->AsRecordVal(); RecordVal* rv = v->AsRecordVal();
auto bt = const_cast<zeek::Type*>(t); RecordTypePtr rt{NewRef{}, t->AsRecordType()};
RecordTypePtr rt{NewRef{}, bt->AsRecordType()};
auto aggr_rec = cast_intrusive<RecordVal>(std::move(aggr)); auto aggr_rec = cast_intrusive<RecordVal>(std::move(aggr));
auto ar = rv->CoerceTo(std::move(rt), std::move(aggr_rec)); auto ar = rv->CoerceTo(std::move(rt), std::move(aggr_rec));
@ -3573,14 +3572,14 @@ ValPtr TableConstructorExpr::Eval(Frame* f) const
const ExprPList& exprs = op->AsListExpr()->Exprs(); const ExprPList& exprs = op->AsListExpr()->Exprs();
for ( const auto& expr : exprs ) for ( const auto& expr : exprs )
expr->EvalIntoAggregate(type.get(), aggr.get(), f); expr->EvalIntoAggregate(type, aggr, f);
aggr->InitDefaultFunc(f); aggr->InitDefaultFunc(f);
return aggr; return aggr;
} }
ValPtr TableConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr TableConstructorExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( IsError() ) if ( IsError() )
return nullptr; return nullptr;
@ -3595,7 +3594,7 @@ ValPtr TableConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
const ExprPList& exprs = op->AsListExpr()->Exprs(); const ExprPList& exprs = op->AsListExpr()->Exprs();
for ( const auto& expr : exprs ) for ( const auto& expr : exprs )
expr->EvalIntoAggregate(t, tval.get(), nullptr); expr->EvalIntoAggregate(t, tval, nullptr);
return tval; return tval;
} }
@ -3664,7 +3663,7 @@ SetConstructorExpr::SetConstructorExpr(ListExprPtr constructor_list,
ListExpr* le = ce->AsListExpr(); ListExpr* le = ce->AsListExpr();
assert(ce->Tag() == EXPR_LIST); assert(ce->Tag() == EXPR_LIST);
if ( check_and_promote_exprs(le, type->AsTableType()->GetIndices().get()) ) if ( check_and_promote_exprs(le, type->AsTableType()->GetIndices()) )
{ {
if ( le != cle[i] ) if ( le != cle[i] )
cle.replace(i, le); cle.replace(i, le);
@ -3694,7 +3693,7 @@ ValPtr SetConstructorExpr::Eval(Frame* f) const
return aggr; return aggr;
} }
ValPtr SetConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr SetConstructorExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( IsError() ) if ( IsError() )
return nullptr; return nullptr;
@ -3710,7 +3709,7 @@ ValPtr SetConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
for ( const auto& e : exprs ) for ( const auto& e : exprs )
{ {
auto element = check_and_promote(e->Eval(nullptr), index_type.get(), true); auto element = check_and_promote(e->Eval(nullptr), index_type, true);
if ( ! element || ! tval->Assign(std::move(element), nullptr) ) if ( ! element || ! tval->Assign(std::move(element), nullptr) )
{ {
@ -3792,7 +3791,7 @@ ValPtr VectorConstructorExpr::Eval(Frame* f) const
return vec; return vec;
} }
ValPtr VectorConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr VectorConstructorExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( IsError() ) if ( IsError() )
return nullptr; return nullptr;
@ -3808,7 +3807,7 @@ ValPtr VectorConstructorExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
loop_over_list(exprs, i) loop_over_list(exprs, i)
{ {
Expr* e = exprs[i]; Expr* e = exprs[i];
auto v = check_and_promote(e->Eval(nullptr), t->Yield().get(), true); auto v = check_and_promote(e->Eval(nullptr), t->Yield(), true);
if ( ! v || ! vec->Assign(i, std::move(v)) ) if ( ! v || ! vec->Assign(i, std::move(v)) )
{ {
@ -3839,7 +3838,7 @@ bool FieldAssignExpr::PromoteTo(TypePtr t)
return op != nullptr; return op != nullptr;
} }
void FieldAssignExpr::EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) const void FieldAssignExpr::EvalIntoAggregate(const TypePtr& t, ValPtr aggr, Frame* f) const
{ {
if ( IsError() ) if ( IsError() )
return; return;
@ -3910,7 +3909,7 @@ ArithCoerceExpr::ArithCoerceExpr(ExprPtr arg_op, TypeTag t)
ValPtr ArithCoerceExpr::FoldSingleVal(ValPtr v, const TypePtr& t) const ValPtr ArithCoerceExpr::FoldSingleVal(ValPtr v, const TypePtr& t) const
{ {
return check_and_promote(v, t.get(), false, location); return check_and_promote(v, t, false, location);
} }
ValPtr ArithCoerceExpr::Fold(Val* v) const ValPtr ArithCoerceExpr::Fold(Val* v) const
@ -4047,7 +4046,7 @@ RecordCoerceExpr::RecordCoerceExpr(ExprPtr arg_op, RecordTypePtr r)
} }
} }
ValPtr RecordCoerceExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr RecordCoerceExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
if ( IsError() ) if ( IsError() )
{ {
@ -4061,8 +4060,7 @@ ValPtr RecordCoerceExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
if ( auto v = Eval(nullptr) ) if ( auto v = Eval(nullptr) )
{ {
RecordVal* rv = v->AsRecordVal(); RecordVal* rv = v->AsRecordVal();
auto bt = const_cast<zeek::Type*>(t); RecordTypePtr rt{NewRef{}, t->AsRecordType()};
RecordTypePtr rt{NewRef{}, bt->AsRecordType()};
auto aggr_rec = cast_intrusive<RecordVal>(std::move(aggr)); auto aggr_rec = cast_intrusive<RecordVal>(std::move(aggr));
if ( auto ar = rv->CoerceTo(std::move(rt), std::move(aggr_rec)) ) if ( auto ar = rv->CoerceTo(std::move(rt), std::move(aggr_rec)) )
@ -4134,7 +4132,7 @@ RecordValPtr coerce_to_record(RecordTypePtr rt, Val* v, const std::vector<int>&
else if ( BothArithmetic(rhs_type->Tag(), field_type->Tag()) && else if ( BothArithmetic(rhs_type->Tag(), field_type->Tag()) &&
! same_type(rhs_type, field_type) ) ! same_type(rhs_type, field_type) )
{ {
auto new_val = check_and_promote(rhs, field_type.get(), false); auto new_val = check_and_promote(rhs, field_type, false);
rhs = std::move(new_val); rhs = std::move(new_val);
} }
@ -4929,7 +4927,7 @@ TypePtr ListExpr::InitType() const
} }
} }
ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const ValPtr ListExpr::InitVal(const TypePtr& t, ValPtr aggr) const
{ {
// While fairly similar to the EvalIntoAggregate() code, // While fairly similar to the EvalIntoAggregate() code,
// we keep this separate since it also deals with initialization // we keep this separate since it also deals with initialization
@ -4947,13 +4945,13 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
if ( exprs.length() != static_cast<int>(tl.size()) ) if ( exprs.length() != static_cast<int>(tl.size()) )
{ {
Error("index mismatch", t); Error("index mismatch", t.get());
return nullptr; return nullptr;
} }
loop_over_list(exprs, i) loop_over_list(exprs, i)
{ {
auto vi = exprs[i]->InitVal(tl[i].get(), nullptr); auto vi = exprs[i]->InitVal(tl[i], nullptr);
if ( ! vi ) if ( ! vi )
return nullptr; return nullptr;
@ -4967,7 +4965,7 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
{ {
if ( aggr ) if ( aggr )
{ {
Error("bad use of list in initialization", t); Error("bad use of list in initialization", t.get());
return nullptr; return nullptr;
} }
@ -4975,7 +4973,7 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
if ( exprs.length() != static_cast<int>(tl.size()) ) if ( exprs.length() != static_cast<int>(tl.size()) )
{ {
Error("index mismatch", t); Error("index mismatch", t.get());
return nullptr; return nullptr;
} }
@ -4983,7 +4981,7 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
loop_over_list(exprs, i) loop_over_list(exprs, i)
{ {
auto vi = exprs[i]->InitVal(tl[i].get(), nullptr); auto vi = exprs[i]->InitVal(tl[i], nullptr);
if ( ! vi ) if ( ! vi )
return nullptr; return nullptr;
@ -5001,7 +4999,7 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
return exprs[0]->InitVal(t, aggr); return exprs[0]->InitVal(t, aggr);
else else
{ {
Error("aggregate initializer for scalar type", t); Error("aggregate initializer for scalar type", t.get());
return nullptr; return nullptr;
} }
} }
@ -5052,7 +5050,7 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
{ {
if ( t->Tag() == TYPE_RECORD ) if ( t->Tag() == TYPE_RECORD )
{ {
e->Error("bad record initializer", t); e->Error("bad record initializer", t.get());
return nullptr; return nullptr;
} }
@ -5060,7 +5058,7 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
if ( ! same_type(v->GetType(), t) ) if ( ! same_type(v->GetType(), t) )
{ {
v->GetType()->Error("type clash in table initializer", t); v->GetType()->Error("type clash in table initializer", t.get());
return nullptr; return nullptr;
} }
@ -5072,14 +5070,14 @@ ValPtr ListExpr::InitVal(const zeek::Type* t, ValPtr aggr) const
return aggr; return aggr;
} }
ValPtr ListExpr::AddSetInit(const zeek::Type* t, ValPtr aggr) const ValPtr ListExpr::AddSetInit(TypePtr t, ValPtr aggr) const
{ {
if ( aggr->GetType()->Tag() != TYPE_TABLE ) if ( aggr->GetType()->Tag() != TYPE_TABLE )
Internal("bad aggregate in ListExpr::AddSetInit"); Internal("bad aggregate in ListExpr::AddSetInit");
TableVal* tv = aggr->AsTableVal(); TableVal* tv = aggr->AsTableVal();
const TableType* tt = tv->GetType()->AsTableType(); const TableType* tt = tv->GetType()->AsTableType();
const TypeList* it = tt->GetIndices().get(); TypeListPtr it = tt->GetIndices();
for ( const auto& expr : exprs ) for ( const auto& expr : exprs )
{ {
@ -5091,7 +5089,7 @@ ValPtr ListExpr::AddSetInit(const zeek::Type* t, ValPtr aggr) const
else if ( expr->GetType()->Tag() == TYPE_LIST ) else if ( expr->GetType()->Tag() == TYPE_LIST )
element = expr->InitVal(it, nullptr); element = expr->InitVal(it, nullptr);
else else
element = expr->InitVal(it->GetTypes()[0].get(), nullptr); element = expr->InitVal(it->GetTypes()[0], nullptr);
if ( ! element ) if ( ! element )
return nullptr; return nullptr;
@ -5100,7 +5098,7 @@ ValPtr ListExpr::AddSetInit(const zeek::Type* t, ValPtr aggr) const
{ {
if ( ! same_type(element->GetType(), t) ) if ( ! same_type(element->GetType(), t) )
{ {
element->Error("type clash in set initializer", t); element->Error("type clash in set initializer", t.get());
return nullptr; return nullptr;
} }
@ -5113,7 +5111,7 @@ ValPtr ListExpr::AddSetInit(const zeek::Type* t, ValPtr aggr) const
if ( expr->GetType()->Tag() == TYPE_LIST ) if ( expr->GetType()->Tag() == TYPE_LIST )
element = check_and_promote(std::move(element), it, true); element = check_and_promote(std::move(element), it, true);
else else
element = check_and_promote(std::move(element), it->GetTypes()[0].get(), true); element = check_and_promote(std::move(element), it->GetTypes()[0], true);
if ( ! element ) if ( ! element )
return nullptr; return nullptr;
@ -5389,7 +5387,7 @@ ExprPtr check_and_promote_expr(ExprPtr e, TypePtr t)
return e; return e;
} }
bool check_and_promote_exprs(ListExpr* const elements, TypeList* types) bool check_and_promote_exprs(ListExpr* const elements, const TypeListPtr& types)
{ {
ExprPList& el = elements->Exprs(); ExprPList& el = elements->Exprs();
const auto& tl = types->GetTypes(); const auto& tl = types->GetTypes();
@ -5464,13 +5462,12 @@ bool check_and_promote_args(ListExpr* const args, const RecordType* types)
el.push_back(def_elements[--ne].release()); el.push_back(def_elements[--ne].release());
} }
TypeList* tl = new TypeList(); auto tl = make_intrusive<TypeList>();
for ( int i = 0; i < types->NumFields(); ++i ) for ( int i = 0; i < types->NumFields(); ++i )
tl->Append(types->GetFieldType(i)); tl->Append(types->GetFieldType(i));
int rval = check_and_promote_exprs(args, tl); int rval = check_and_promote_exprs(args, tl);
Unref(tl);
return rval; return rval;
} }

View file

@ -167,7 +167,7 @@ public:
// into the given aggregate of the given type. Note that // into the given aggregate of the given type. Note that
// return type is void since it's updating an existing // return type is void since it's updating an existing
// value, rather than creating a new one. // value, rather than creating a new one.
virtual void EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) const; virtual void EvalIntoAggregate(const TypePtr& t, ValPtr aggr, Frame* f) const;
// Assign to the given value, if appropriate. // Assign to the given value, if appropriate.
virtual void Assign(Frame* f, ValPtr v); virtual void Assign(Frame* f, ValPtr v);
@ -187,7 +187,7 @@ public:
// with the given type. If "aggr" is non-nil, then this expression // with the given type. If "aggr" is non-nil, then this expression
// is an element of the given aggregate, and it is added to it // is an element of the given aggregate, and it is added to it
// accordingly. // accordingly.
virtual ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const; virtual ValPtr InitVal(const TypePtr& t, ValPtr aggr) const;
// True if the expression has no side effects, false otherwise. // True if the expression has no side effects, false otherwise.
virtual bool IsPure() const; virtual bool IsPure() const;
@ -939,10 +939,10 @@ public:
const AttributesPtr& attrs = nullptr, bool type_check = true); const AttributesPtr& attrs = nullptr, bool type_check = true);
ValPtr Eval(Frame* f) const override; ValPtr Eval(Frame* f) const override;
void EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) const override; void EvalIntoAggregate(const TypePtr& t, ValPtr aggr, Frame* f) const override;
TypePtr InitType() const override; TypePtr InitType() const override;
bool IsRecordElement(TypeDecl* td) const override; bool IsRecordElement(TypeDecl* td) const override;
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
bool IsPure() const override; bool IsPure() const override;
// Optimization-related: // Optimization-related:
@ -1149,7 +1149,7 @@ public:
StmtPtr ReduceToSingletons(Reducer* c) override; StmtPtr ReduceToSingletons(Reducer* c) override;
protected: protected:
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
void ExprDescribe(ODesc* d) const override; void ExprDescribe(ODesc* d) const override;
@ -1175,7 +1175,7 @@ public:
StmtPtr ReduceToSingletons(Reducer* c) override; StmtPtr ReduceToSingletons(Reducer* c) override;
protected: protected:
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
void ExprDescribe(ODesc* d) const override; void ExprDescribe(ODesc* d) const override;
@ -1200,7 +1200,7 @@ public:
StmtPtr ReduceToSingletons(Reducer* c) override; StmtPtr ReduceToSingletons(Reducer* c) override;
protected: protected:
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
void ExprDescribe(ODesc* d) const override; void ExprDescribe(ODesc* d) const override;
@ -1220,7 +1220,7 @@ public:
bool HasReducedOps(Reducer* c) const override; bool HasReducedOps(Reducer* c) const override;
protected: protected:
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
void ExprDescribe(ODesc* d) const override; void ExprDescribe(ODesc* d) const override;
}; };
@ -1240,7 +1240,7 @@ public:
// (in which case an error is reported). // (in which case an error is reported).
bool PromoteTo(TypePtr t); bool PromoteTo(TypePtr t);
void EvalIntoAggregate(const zeek::Type* t, Val* aggr, Frame* f) const override; void EvalIntoAggregate(const TypePtr& t, ValPtr aggr, Frame* f) const override;
bool IsRecordElement(TypeDecl* td) const override; bool IsRecordElement(TypeDecl* td) const override;
// Optimization-related: // Optimization-related:
@ -1282,7 +1282,7 @@ public:
const std::vector<int>& Map() const { return map; } const std::vector<int>& Map() const { return map; }
protected: protected:
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
ValPtr Fold(Val* v) const override; ValPtr Fold(Val* v) const override;
// For each super-record slot, gives subrecord slot with which to // For each super-record slot, gives subrecord slot with which to
@ -1470,7 +1470,7 @@ public:
ValPtr Eval(Frame* f) const override; ValPtr Eval(Frame* f) const override;
TypePtr InitType() const override; TypePtr InitType() const override;
ValPtr InitVal(const zeek::Type* t, ValPtr aggr) const override; ValPtr InitVal(const TypePtr& t, ValPtr aggr) const override;
ExprPtr MakeLvalue() override; ExprPtr MakeLvalue() override;
void Assign(Frame* f, ValPtr v) override; void Assign(Frame* f, ValPtr v) override;
@ -1486,7 +1486,7 @@ public:
StmtPtr ReduceToSingletons(Reducer* c) override; StmtPtr ReduceToSingletons(Reducer* c) override;
protected: protected:
ValPtr AddSetInit(const zeek::Type* t, ValPtr aggr) const; ValPtr AddSetInit(TypePtr t, ValPtr aggr) const;
void ExprDescribe(ODesc* d) const override; void ExprDescribe(ODesc* d) const override;
@ -1767,7 +1767,7 @@ ExprPtr get_assign_expr(ExprPtr op1, ExprPtr op2, bool is_init);
*/ */
extern ExprPtr check_and_promote_expr(ExprPtr e, TypePtr t); extern ExprPtr check_and_promote_expr(ExprPtr e, TypePtr t);
extern bool check_and_promote_exprs(ListExpr* elements, TypeList* types); extern bool check_and_promote_exprs(ListExpr* elements, const TypeListPtr& types);
extern bool check_and_promote_args(ListExpr* args, const RecordType* types); extern bool check_and_promote_args(ListExpr* args, const RecordType* types);
extern bool check_and_promote_exprs_to_type(ListExpr* elements, TypePtr type); extern bool check_and_promote_exprs_to_type(ListExpr* elements, TypePtr type);

View file

@ -345,7 +345,7 @@ int IndexType::MatchesIndex(detail::ListExpr* const index) const
exprs[0]->GetType()->Tag() == TYPE_ADDR ) exprs[0]->GetType()->Tag() == TYPE_ADDR )
return MATCHES_INDEX_SCALAR; return MATCHES_INDEX_SCALAR;
return check_and_promote_exprs(index, GetIndices().get()) ? MATCHES_INDEX_SCALAR return check_and_promote_exprs(index, GetIndices()) ? MATCHES_INDEX_SCALAR
: DOES_NOT_MATCH_INDEX; : DOES_NOT_MATCH_INDEX;
} }
@ -590,7 +590,7 @@ SetType::SetType(TypeListPtr ind, detail::ListExprPtr arg_elements)
{ {
if ( indices ) if ( indices )
{ // We already have a type. { // We already have a type.
if ( ! check_and_promote_exprs(elements.get(), indices.get()) ) if ( ! check_and_promote_exprs(elements.get(), indices) )
SetError(); SetError();
} }
else else

View file

@ -3730,8 +3730,19 @@ void VectorVal::ValDescribe(ODesc* d) const
d->Add("]"); d->Add("]");
} }
ValPtr check_and_promote(ValPtr v, const Type* t, bool is_init, ValPtr check_and_promote(ValPtr v, const TypePtr& t, bool is_init,
const detail::Location* expr_location) const detail::Location* expr_location)
{
// Once 5.0 comes out, this function can merge with the deprecated one below it, and this
// pragma block can go away.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return check_and_promote(v, t.get(), is_init, expr_location);
#pragma GCC diagnostic pop
}
[[deprecated("Remove in v5.1. Use version that takes TypePtr instead.")]] ValPtr
check_and_promote(ValPtr v, const Type* t, bool is_init, const detail::Location* expr_location)
{ {
if ( ! v ) if ( ! v )
return nullptr; return nullptr;

View file

@ -1699,7 +1699,10 @@ UNDERLYING_ACCESSOR_DEF(TypeVal, zeek::Type*, AsType)
// exact match, returns it. If promotable, returns the promoted version. // exact match, returns it. If promotable, returns the promoted version.
// If not a match, generates an error message and return nil. If is_init is // If not a match, generates an error message and return nil. If is_init is
// true, then the checking is done in the context of an initialization. // true, then the checking is done in the context of an initialization.
extern ValPtr check_and_promote(ValPtr v, const Type* t, bool is_init, extern ValPtr check_and_promote(ValPtr v, const TypePtr& t, bool is_init,
const detail::Location* expr_location = nullptr);
[[deprecated("Remove in v5.1. Use version that takes TypePtr instead.")]] extern ValPtr
check_and_promote(ValPtr v, const Type* t, bool is_init,
const detail::Location* expr_location = nullptr); const detail::Location* expr_location = nullptr);
extern bool same_val(const Val* v1, const Val* v2); extern bool same_val(const Val* v1, const Val* v2);

View file

@ -23,7 +23,7 @@
namespace zeek::detail namespace zeek::detail
{ {
static ValPtr init_val(Expr* init, const Type* t, ValPtr aggr) static ValPtr init_val(ExprPtr init, TypePtr t, ValPtr aggr)
{ {
try try
{ {
@ -289,7 +289,7 @@ static void make_var(const IDPtr& id, TypePtr t, InitClass c, ExprPtr init,
if ( init ) if ( init )
{ {
v = init_val(init.get(), t.get(), aggr); v = init_val(init, t, aggr);
if ( ! v ) if ( ! v )
return; return;

View file

@ -197,7 +197,7 @@ TableValPtr set_constructor__CPP(vector<ValPtr> elements, TableTypePtr t, vector
TableValPtr table_constructor__CPP(vector<ValPtr> indices, vector<ValPtr> vals, TableTypePtr t, TableValPtr table_constructor__CPP(vector<ValPtr> indices, vector<ValPtr> vals, TableTypePtr t,
vector<int> attr_tags, vector<ValPtr> attr_vals) vector<int> attr_tags, vector<ValPtr> attr_vals)
{ {
const auto& yt = t->Yield().get(); const auto& yt = t->Yield();
auto n = indices.size(); auto n = indices.size();
auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals)); auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals));