diff --git a/src/Val.cc b/src/Val.cc index 9433ab4202..e2cc1e58e5 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1768,57 +1768,6 @@ bool TableVal::IsSubsetOf(const TableVal& tv) const return true; } -bool TableVal::ExpandAndInit(ValPtr index, ValPtr new_val) - { - const auto& index_type = index->GetType(); - - if ( index_type->IsSet() ) - { - index = index->AsTableVal()->ToListVal(); - return ExpandAndInit(std::move(index), std::move(new_val)); - } - - if ( index_type->Tag() != TYPE_LIST ) - // Nothing to expand. - return CheckAndAssign(std::move(index), std::move(new_val)); - - ListVal* iv = index->AsListVal(); - if ( iv->BaseTag() != TYPE_ANY ) - { - if ( table_type->GetIndices()->GetTypes().size() != 1 ) - reporter->InternalError("bad singleton list index"); - - for ( int i = 0; i < iv->Length(); ++i ) - if ( ! ExpandAndInit(iv->Idx(i), new_val) ) - return false; - - return true; - } - - else - { // Compound table. - int i; - - for ( i = 0; i < iv->Length(); ++i ) - { - const auto& v = iv->Idx(i); - // ### if CompositeHash::ComputeHash did flattening - // of 1-element lists (like ComputeSingletonHash does), - // then we could optimize here. - const auto& t = v->GetType(); - - if ( t->IsSet() || t->Tag() == TYPE_LIST ) - break; - } - - if ( i >= iv->Length() ) - // Nothing to expand. - return CheckAndAssign(std::move(index), std::move(new_val)); - else - return ExpandCompoundAndInit(iv, i, std::move(new_val)); - } - } - ValPtr TableVal::Default(const ValPtr& index) { const auto& def_attr = GetAttr(detail::ATTR_DEFAULT); @@ -2446,49 +2395,6 @@ void TableVal::Describe(ODesc* d) const } } -bool TableVal::ExpandCompoundAndInit(ListVal* lv, int k, ValPtr new_val) - { - Val* ind_k_v = lv->Idx(k).get(); - auto ind_k = ind_k_v->GetType()->IsSet() ? ind_k_v->AsTableVal()->ToListVal() - : ListValPtr{NewRef{}, ind_k_v->AsListVal()}; - - for ( int i = 0; i < ind_k->Length(); ++i ) - { - const auto& ind_k_i = ind_k->Idx(i); - auto expd = make_intrusive(TYPE_ANY); - - for ( auto j = 0; j < lv->Length(); ++j ) - { - const auto& v = lv->Idx(j); - - if ( j == k ) - expd->Append(ind_k_i); - else - expd->Append(v); - } - - if ( ! ExpandAndInit(std::move(expd), new_val) ) - return false; - } - - return true; - } - -bool TableVal::CheckAndAssign(ValPtr index, ValPtr new_val) - { - Val* v = nullptr; - if ( subnets ) - // We need an exact match here. - v = (Val*)subnets->Lookup(index.get(), true); - else - v = Find(index).get(); - - if ( v ) - index->Warn("multiple initializations for index"); - - return Assign(std::move(index), std::move(new_val)); - } - void TableVal::InitDefaultFunc(detail::Frame* f) { // Value aready initialized. @@ -2959,13 +2865,12 @@ ValPtr RecordVal::GetFieldOrDefault(const char* field) const return GetFieldOrDefault(idx); } -RecordValPtr RecordVal::CoerceTo(RecordTypePtr t, RecordValPtr aggr, bool allow_orphaning) const +RecordValPtr RecordVal::DoCoerceTo(RecordTypePtr t, bool allow_orphaning) const { if ( ! record_promotion_compatible(t.get(), GetType()->AsRecordType()) ) return nullptr; - if ( ! aggr ) - aggr = make_intrusive(std::move(t)); + auto aggr = make_intrusive(std::move(t)); RecordType* ar_t = aggr->GetType()->AsRecordType(); const RecordType* rv_t = GetType()->AsRecordType(); @@ -3023,7 +2928,7 @@ RecordValPtr RecordVal::CoerceTo(RecordTypePtr t, bool allow_orphaning) if ( same_type(GetType(), t) ) return {NewRef{}, this}; - return CoerceTo(std::move(t), nullptr, allow_orphaning); + return DoCoerceTo(std::move(t), allow_orphaning); } TableValPtr RecordVal::GetRecordFieldsVal() const diff --git a/src/Val.h b/src/Val.h index acbca8edec..8f78a59f10 100644 --- a/src/Val.h +++ b/src/Val.h @@ -828,10 +828,6 @@ public: // of the given set. bool IsSubsetOf(const TableVal& v) const; - // Expands any lists in the index into multiple initializations. - // Returns true if the initializations typecheck, false if not. - bool ExpandAndInit(ValPtr index, ValPtr new_val); - /** * Finds an index in the table and returns its associated value. * @param index The index to lookup in the table. @@ -1003,8 +999,6 @@ protected: void RebuildTable(ParseTimeTableState ptts); void CheckExpireAttr(detail::AttrTag at); - bool ExpandCompoundAndInit(ListVal* lv, int k, ValPtr new_val); - bool CheckAndAssign(ValPtr index, ValPtr new_val); // Calculates default value for index. Returns nullptr if none. ValPtr Default(const ValPtr& index); @@ -1359,18 +1353,16 @@ public: Obj* GetOrigin() const { return origin; } // Returns a new value representing the value coerced to the given - // type. If coercion is not possible, returns 0. The non-const + // type. If coercion is not possible, returns nil. The non-const // version may return the current value ref'ed if its type matches // directly. // - // *aggr* is optional; if non-zero, we add to it. See - // Expr::InitVal(). We leave it out in the non-const version to make - // the choice unambigious. - // // The *allow_orphaning* parameter allows for a record to be demoted // down to a record type that contains less fields. - RecordValPtr CoerceTo(RecordTypePtr other, RecordValPtr aggr, - bool allow_orphaning = false) const; + RecordValPtr CoerceTo(RecordTypePtr other, bool allow_orphaning = false) const + { + return DoCoerceTo(other, allow_orphaning); + } RecordValPtr CoerceTo(RecordTypePtr other, bool allow_orphaning = false); [[deprecated("Remove in v5.1. MemoryAllocation() is deprecated and will be removed. See " @@ -1390,6 +1382,8 @@ public: protected: friend class zeek::detail::ZBody; + RecordValPtr DoCoerceTo(RecordTypePtr other, bool allow_orphaning) const; + /** * Appends a value to the record's fields. The caller is responsible * for ensuring that fields are appended in the correct order and