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;
}
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<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)
{
// 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<RecordVal>(std::move(t));
auto aggr = make_intrusive<RecordVal>(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