Implementing += operator for record types.

This is per #375.

Record types can now get additional fields later via '+='. The added
fields must however either be &optional or have a &default value.

Example:

    type Foo: record {
        a: count;
        b: count &optional;
    };

    redef record Foo += {
        c: count &default=42;
        d: count &optional;
    };

    global f: Foo = [$a=21];

    print f;

Output:

    [a=21, b=<uninitialized>, c=42, d=<uninitialized>]
This commit is contained in:
Robin Sommer 2011-02-07 16:04:32 -08:00
parent cdb20e61b7
commit 95069f0993
3 changed files with 44 additions and 2 deletions

View file

@ -799,8 +799,8 @@ decl:
| TOK_REDEF global_id opt_type init_class opt_init opt_attr ';'
{ add_global($2, $3, $4, $5, $6, VAR_REDEF); }
| TOK_REDEF TOK_ENUM global_id TOK_ADD_TO
'{' enum_id_list opt_comma '}' ';'
| TOK_REDEF TOK_ENUM global_id TOK_ADD_TO
'{' enum_id_list opt_comma '}' ';'
{
if ( ! $3->Type() )
$3->Error("unknown identifier");
@ -815,6 +815,24 @@ decl:
}
}
| TOK_REDEF TOK_RECORD global_id TOK_ADD_TO
'{' type_decl_list '}' ';'
{
if ( ! $3->Type() )
$3->Error("unknown identifier");
else
{
RecordType* add_to = $3->Type()->AsRecordType();
if ( ! add_to )
$3->Error("not a record type");
else {
const char* error = add_to->AddFields($6);
if ( error )
$3->Error(error);
}
}
}
| TOK_TYPE global_id ':' refined_type opt_attr ';'
{
add_type($2, $4, $5, 0);