218e99
From da3719ca3f47e716394f3a572f0f1403518a8442 Mon Sep 17 00:00:00 2001
218e99
From: Kevin Wolf <kwolf@redhat.com>
218e99
Date: Mon, 9 Sep 2013 14:28:07 +0200
218e99
Subject: [PATCH 16/38] qapi-types.py: Implement 'base' for unions
218e99
218e99
RH-Author: Kevin Wolf <kwolf@redhat.com>
218e99
Message-id: <1378736903-18489-17-git-send-email-kwolf@redhat.com>
218e99
Patchwork-id: 54203
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 16/32] qapi-types.py: Implement 'base' for unions
218e99
Bugzilla: 1005818
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Max Reitz <mreitz@redhat.com>
218e99
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
218e99
Bugzilla: 1005818
218e99
218e99
The new 'base' key in a union definition refers to a struct type, which
218e99
is inlined into the union definition and can represent fields common to
218e99
all kinds.
218e99
218e99
For example the following schema definition...
218e99
218e99
    { 'type': 'BlockOptionsBase', 'data': { 'read-only': 'bool' } }
218e99
218e99
    { 'union': 'BlockOptions',
218e99
      'base': 'BlockOptionsBase',
218e99
      'data': {
218e99
          'raw': 'BlockOptionsRaw'
218e99
          'qcow2': 'BlockOptionsQcow2'
218e99
      } }
218e99
218e99
...would result in this generated C struct:
218e99
218e99
    struct BlockOptions
218e99
    {
218e99
        BlockOptionsKind kind;
218e99
        union {
218e99
            void *data;
218e99
            BlockOptionsRaw * raw;
218e99
            BlockOptionsQcow2 * qcow2;
218e99
        };
218e99
        bool read_only;
218e99
    };
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
(cherry picked from commit e2503f5e213e30e3e9a397d454a35c10b5bdc899)
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
---
218e99
 scripts/qapi-types.py | 16 ++++++++++++++--
218e99
 1 file changed, 14 insertions(+), 2 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 scripts/qapi-types.py |   16 ++++++++++++++--
218e99
 1 files changed, 14 insertions(+), 2 deletions(-)
218e99
218e99
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
218e99
index 9e19920..c0684a7 100644
218e99
--- a/scripts/qapi-types.py
218e99
+++ b/scripts/qapi-types.py
218e99
@@ -131,7 +131,12 @@ typedef enum %(name)s
218e99
 
218e99
     return lookup_decl + enum_decl
218e99
 
218e99
-def generate_union(name, typeinfo):
218e99
+def generate_union(expr):
218e99
+
218e99
+    name = expr['union']
218e99
+    typeinfo = expr['data']
218e99
+    base = expr.get('base')
218e99
+
218e99
     ret = mcgen('''
218e99
 struct %(name)s
218e99
 {
218e99
@@ -150,6 +155,13 @@ struct %(name)s
218e99
 
218e99
     ret += mcgen('''
218e99
     };
218e99
+''')
218e99
+
218e99
+    if base:
218e99
+        struct = find_struct(base)
218e99
+        ret += generate_struct_fields(struct['data'])
218e99
+
218e99
+    ret += mcgen('''
218e99
 };
218e99
 ''')
218e99
 
218e99
@@ -307,7 +319,7 @@ for expr in exprs:
218e99
         ret += generate_type_cleanup_decl(expr['type'])
218e99
         fdef.write(generate_type_cleanup(expr['type']) + "\n")
218e99
     elif expr.has_key('union'):
218e99
-        ret += generate_union(expr['union'], expr['data'])
218e99
+        ret += generate_union(expr)
218e99
         ret += generate_type_cleanup_decl(expr['union'] + "List")
218e99
         fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
218e99
         ret += generate_type_cleanup_decl(expr['union'])
218e99
-- 
218e99
1.7.1
218e99