Implement a Shallow Clone operation for types.

This is needed to track name changes for the documentation.

With this things, which do not need val-cloning, generally seem to work
again. There are a whole bunch of test failures at the moment.
This commit is contained in:
Johanna Amann 2019-05-17 11:13:04 -07:00
parent 474efe9e69
commit ffa173abc0
3 changed files with 92 additions and 12 deletions

View file

@ -121,9 +121,29 @@ BroType::BroType(TypeTag t, bool arg_base_type)
}
BroType* BroType::Clone() const
BroType* BroType::ShallowClone() const
{
// Fixme: Johanna
switch ( tag ) {
case TYPE_VOID:
case TYPE_BOOL:
case TYPE_INT:
case TYPE_COUNT:
case TYPE_COUNTER:
case TYPE_DOUBLE:
case TYPE_TIME:
case TYPE_INTERVAL:
case TYPE_STRING:
case TYPE_PATTERN:
case TYPE_TIMER:
case TYPE_PORT:
case TYPE_ADDR:
case TYPE_SUBNET:
case TYPE_ANY:
return new BroType(tag, base_type);
default:
reporter->InternalError("cloning illegal base BroType");
}
return nullptr;
}
@ -381,6 +401,16 @@ TableType::TableType(TypeList* ind, BroType* yield)
}
}
TableType* TableType::ShallowClone() const
{
if ( indices )
indices->Ref();
if ( yield_type )
yield_type->Ref();
return new TableType(indices, yield_type);
}
bool TableType::IsUnspecifiedTable() const
{
// Unspecified types have an empty list of indices.
@ -488,6 +518,16 @@ FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg
}
}
FuncType* FuncType::ShallowClone() const
{
auto f = new FuncType();
f->args = args->Ref()->AsRecordType();
f->arg_types = arg_types->Ref()->AsTypeList();
f->yield = yield->Ref();
f->flavor = flavor;
return f;
}
string FuncType::FlavorString() const
{
switch ( flavor ) {
@ -646,6 +686,16 @@ RecordType::RecordType(type_decl_list* arg_types) : BroType(TYPE_RECORD)
num_fields = types ? types->length() : 0;
}
// in this case the clone is actually not so shallow, since
// it gets modified by everyone.
RecordType* RecordType::ShallowClone() const
{
auto pass = new type_decl_list();
loop_over_list(*types, i)
pass->append(new TypeDecl(*(*types)[i]));
return new RecordType(pass);
}
RecordType::~RecordType()
{
if ( types )
@ -991,18 +1041,26 @@ EnumType::EnumType(const string& name)
SetName(name);
}
EnumType::EnumType(EnumType* e)
EnumType::EnumType(const EnumType* e)
: BroType(TYPE_ENUM)
{
counter = e->counter;
SetName(e->GetName());
for ( NameMap::iterator it = e->names.begin(); it != e->names.end(); ++it )
for ( auto it = e->names.begin(); it != e->names.end(); ++it )
names[it->first] = it->second;
vals = e->vals;
}
EnumType* EnumType::ShallowClone() const
{
if ( counter == 0 )
return new EnumType(GetName());
return new EnumType(this);
}
EnumType::~EnumType()
{
for ( auto& kv : vals )
@ -1231,6 +1289,11 @@ VectorType::VectorType(BroType* element_type)
{
}
VectorType* VectorType::ShallowClone() const
{
return new VectorType(yield_type);
}
VectorType::~VectorType()
{
Unref(yield_type);