Merge remote-tracking branch 'origin/topic/jsiwek/972'

Closes #972.

* origin/topic/jsiwek/972:
  Allow default function/hook/event parameters.  Addresses #972.
This commit is contained in:
Robin Sommer 2013-05-10 19:28:30 -07:00
commit e89e8d7306
17 changed files with 256 additions and 29 deletions

View file

@ -318,6 +318,30 @@ void add_type(ID* id, BroType* t, attr_list* attr, int /* is_event */)
id->SetAttrs(new Attributes(attr, tnew, false));
}
static void transfer_arg_defaults(RecordType* args, RecordType* recv)
{
for ( int i = 0; i < args->NumFields(); ++i )
{
TypeDecl* args_i = args->FieldDecl(i);
TypeDecl* recv_i = recv->FieldDecl(i);
Attr* def = args_i->attrs ? args_i->attrs->FindAttr(ATTR_DEFAULT) : 0;
if ( ! def )
continue;
if ( ! recv_i->attrs )
{
attr_list* a = new attr_list();
a->append(def);
recv_i->attrs = new Attributes(a, recv_i->type, true);
}
else if ( ! recv_i->attrs->FindAttr(ATTR_DEFAULT) )
recv_i->attrs->AddAttr(def);
}
}
void begin_func(ID* id, const char* module_name, function_flavor flavor,
int is_redef, FuncType* t)
{
@ -335,6 +359,11 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor,
{
if ( ! same_type(id->Type(), t) )
id->Type()->Error("incompatible types", t);
// If a previous declaration of the function had &default params,
// automatically transfer any that are missing (convenience so that
// implementations don't need to specify the &default expression again).
transfer_arg_defaults(id->Type()->AsFuncType()->Args(), t->Args());
}
else if ( is_redef )