From 83bd6584eee4877c8d8c014f9c6342e01f6eb118 Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Tue, 8 Mar 2011 16:27:07 -0800 Subject: [PATCH] Enabling record coercion for a table's &default attribute. --- src/Attr.cc | 10 ++++++++++ src/Val.cc | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Attr.cc b/src/Attr.cc index e03367c41e..98be9a9465 100644 --- a/src/Attr.cc +++ b/src/Attr.cc @@ -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; diff --git a/src/Val.cc b/src/Val.cc index ba1cda41e7..a665249539 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -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 ) {