simplification of Val classes now that they don't have to support initialization

This commit is contained in:
Vern Paxson 2022-03-11 14:23:28 -08:00 committed by Tim Wojtulewicz
parent 9b6a3e8b74
commit c3e2871a5e
2 changed files with 10 additions and 111 deletions

View file

@ -1768,57 +1768,6 @@ bool TableVal::IsSubsetOf(const TableVal& tv) const
return true; 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) ValPtr TableVal::Default(const ValPtr& index)
{ {
const auto& def_attr = GetAttr(detail::ATTR_DEFAULT); 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<ListVal>(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) void TableVal::InitDefaultFunc(detail::Frame* f)
{ {
// Value aready initialized. // Value aready initialized.
@ -2959,13 +2865,12 @@ ValPtr RecordVal::GetFieldOrDefault(const char* field) const
return GetFieldOrDefault(idx); 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()) ) if ( ! record_promotion_compatible(t.get(), GetType()->AsRecordType()) )
return nullptr; return nullptr;
if ( ! aggr ) auto aggr = make_intrusive<RecordVal>(std::move(t));
aggr = make_intrusive<RecordVal>(std::move(t));
RecordType* ar_t = aggr->GetType()->AsRecordType(); RecordType* ar_t = aggr->GetType()->AsRecordType();
const RecordType* rv_t = 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) ) if ( same_type(GetType(), t) )
return {NewRef{}, this}; return {NewRef{}, this};
return CoerceTo(std::move(t), nullptr, allow_orphaning); return DoCoerceTo(std::move(t), allow_orphaning);
} }
TableValPtr RecordVal::GetRecordFieldsVal() const TableValPtr RecordVal::GetRecordFieldsVal() const

View file

@ -828,10 +828,6 @@ public:
// of the given set. // of the given set.
bool IsSubsetOf(const TableVal& v) const; 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. * Finds an index in the table and returns its associated value.
* @param index The index to lookup in the table. * @param index The index to lookup in the table.
@ -1003,8 +999,6 @@ protected:
void RebuildTable(ParseTimeTableState ptts); void RebuildTable(ParseTimeTableState ptts);
void CheckExpireAttr(detail::AttrTag at); 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. // Calculates default value for index. Returns nullptr if none.
ValPtr Default(const ValPtr& index); ValPtr Default(const ValPtr& index);
@ -1359,18 +1353,16 @@ public:
Obj* GetOrigin() const { return origin; } Obj* GetOrigin() const { return origin; }
// Returns a new value representing the value coerced to the given // 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 // version may return the current value ref'ed if its type matches
// directly. // 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 // The *allow_orphaning* parameter allows for a record to be demoted
// down to a record type that contains less fields. // down to a record type that contains less fields.
RecordValPtr CoerceTo(RecordTypePtr other, RecordValPtr aggr, RecordValPtr CoerceTo(RecordTypePtr other, bool allow_orphaning = false) const
bool allow_orphaning = false) const; {
return DoCoerceTo(other, allow_orphaning);
}
RecordValPtr CoerceTo(RecordTypePtr other, bool allow_orphaning = false); RecordValPtr CoerceTo(RecordTypePtr other, bool allow_orphaning = false);
[[deprecated("Remove in v5.1. MemoryAllocation() is deprecated and will be removed. See " [[deprecated("Remove in v5.1. MemoryAllocation() is deprecated and will be removed. See "
@ -1390,6 +1382,8 @@ public:
protected: protected:
friend class zeek::detail::ZBody; 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 * Appends a value to the record's fields. The caller is responsible
* for ensuring that fields are appended in the correct order and * for ensuring that fields are appended in the correct order and