Enabling record coercion for a table's &default attribute.

This commit is contained in:
Robin Sommer 2011-03-08 16:27:07 -08:00
parent 51f9cb2ac5
commit 83bd6584ee
2 changed files with 26 additions and 1 deletions

View file

@ -219,7 +219,17 @@ void Attributes::CheckAttr(Attr* a)
Error("&default function type clash");
}
else
{
BroType* ytype = tt->YieldType();
// Table defaults may be promotable.
if ( atype->Tag() == TYPE_RECORD && ytype->Tag() == TYPE_RECORD &&
record_promotion_compatible(atype->AsRecordType(), ytype->AsRecordType()) )
// Ok.
break;
Error("&default value has inconsistent type");
}
}
}
break;

View file

@ -2023,7 +2023,22 @@ Val* TableVal::Default(Val* index)
return 0;
if ( ! def_val )
def_val = def_attr->AttrExpr()->Eval(0);
{
BroType* ytype = Type()->YieldType();
BroType* dtype = def_attr->AttrExpr()->Type();
if ( dtype->Tag() == TYPE_RECORD && ytype->Tag() == TYPE_RECORD &&
! same_type(dtype, ytype) &&
record_promotion_compatible(dtype->AsRecordType(), ytype->AsRecordType()) )
{
Expr* coerce = new RecordCoerceExpr(def_attr->AttrExpr(), ytype->AsRecordType());
def_val = coerce->Eval(0);
Unref(coerce);
}
else
def_val = def_attr->AttrExpr()->Eval(0);
}
if ( ! def_val )
{