Commit e2503f5e authored by Kevin Wolf's avatar Kevin Wolf
Browse files

qapi-types.py: Implement 'base' for unions



The new 'base' key in a union definition refers to a struct type, which
is inlined into the union definition and can represent fields common to
all kinds.

For example the following schema definition...

    { 'type': 'BlockOptionsBase', 'data': { 'read-only': 'bool' } }

    { 'union': 'BlockOptions',
      'base': 'BlockOptionsBase',
      'data': {
          'raw': 'BlockOptionsRaw'
          'qcow2': 'BlockOptionsQcow2'
      } }

...would result in this generated C struct:

    struct BlockOptions
    {
        BlockOptionsKind kind;
        union {
            void *data;
            BlockOptionsRaw * raw;
            BlockOptionsQcow2 * qcow2;
        };
        bool read_only;
    };

Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
Reviewed-by: default avatarEric Blake <eblake@redhat.com>
parent 003e26bc
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -150,7 +150,12 @@ typedef enum %(name)s

    return lookup_decl + enum_decl

def generate_union(name, typeinfo):
def generate_union(expr):

    name = expr['union']
    typeinfo = expr['data']
    base = expr.get('base')

    ret = mcgen('''
struct %(name)s
{
@@ -169,6 +174,13 @@ struct %(name)s

    ret += mcgen('''
    };
''')

    if base:
        struct = find_struct(base)
        ret += generate_struct_fields(struct['data'])

    ret += mcgen('''
};
''')

@@ -352,7 +364,7 @@ for expr in exprs:
        ret += generate_type_cleanup_decl(expr['type'])
        fdef.write(generate_type_cleanup(expr['type']) + "\n")
    elif expr.has_key('union'):
        ret += generate_union(expr['union'], expr['data'])
        ret += generate_union(expr)
        ret += generate_type_cleanup_decl(expr['union'] + "List")
        fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
        ret += generate_type_cleanup_decl(expr['union'])