Enabling automatic coercion from record type A to be B as long as A

has all the types that B has.
This commit is contained in:
Robin Sommer 2011-02-10 19:35:40 -08:00
parent e00acaddd8
commit af3267acc3

View file

@ -3994,15 +3994,8 @@ RecordCoerceExpr::RecordCoerceExpr(Expr* op, RecordType* r)
{
int t_i = t_r->FieldOffset(sub_r->FieldName(i));
if ( t_i < 0 )
{
// Same as in RecordConstructorExpr::InitVal.
char buf[512];
safe_snprintf(buf, sizeof(buf),
"orphan record field \"%s\"",
sub_r->FieldName(i));
Error(buf);
// Orphane field in rhs, that's ok.
continue;
}
BroType* sub_t_i = sub_r->FieldType(i);
BroType* sup_t_i = t_r->FieldType(t_i);
@ -4047,7 +4040,21 @@ Val* RecordCoerceExpr::Fold(Val* v) const
for ( int i = 0; i < map_size; ++i )
{
if ( map[i] >= 0 )
val->Assign(i, rv->Lookup(map[i])->Ref());
{
Val* rhs = rv->Lookup(map[i]);
if ( ! rhs )
{
const Attr* def = rv->Type()->AsRecordType()->FieldDecl(map[i])->FindAttr(ATTR_DEFAULT);
if ( def )
rhs = def->AttrExpr()->Eval(0);
}
if ( rhs )
rhs = rhs->Ref();
assert(rhs || Type()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_OPTIONAL));
val->Assign(i, rhs);
}
else
val->Assign(i, 0);
}