|
|
9ae3a8 |
From e25370368e98e6d8766e50b7f13e3543e1c3ef9b Mon Sep 17 00:00:00 2001
|
|
|
9ae3a8 |
From: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
Date: Mon, 9 Sep 2013 14:28:12 +0200
|
|
|
9ae3a8 |
Subject: [PATCH 21/38] qapi: Flat unions with arbitrary discriminator
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
RH-Author: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
Message-id: <1378736903-18489-22-git-send-email-kwolf@redhat.com>
|
|
|
9ae3a8 |
Patchwork-id: 54208
|
|
|
9ae3a8 |
O-Subject: [RHEL-7.0 qemu-kvm PATCH 21/32] qapi: Flat unions with arbitrary discriminator
|
|
|
9ae3a8 |
Bugzilla: 1005818
|
|
|
9ae3a8 |
RH-Acked-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Max Reitz <mreitz@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Bugzilla: 1005818
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Instead of the rather verbose syntax that distinguishes base and
|
|
|
9ae3a8 |
subclass fields...
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
{ "type": "file",
|
|
|
9ae3a8 |
"read-only": true,
|
|
|
9ae3a8 |
"data": {
|
|
|
9ae3a8 |
"filename": "test"
|
|
|
9ae3a8 |
} }
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
...we can now have both in the same namespace, allowing a more direct
|
|
|
9ae3a8 |
mapping of the command line, and moving fields between the common base
|
|
|
9ae3a8 |
and subclasses without breaking the API:
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
{ "driver": "file",
|
|
|
9ae3a8 |
"read-only": true,
|
|
|
9ae3a8 |
"filename": "test" }
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
Reviewed-by: Eric Blake <eblake@redhat.com>
|
|
|
9ae3a8 |
(cherry picked from commit 50f2bdc75c5ee00617ad874c9ceac2cea660aa1e)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
docs/qapi-code-gen.txt | 22 ++++++++++++
|
|
|
9ae3a8 |
scripts/qapi-types.py | 11 ++++--
|
|
|
9ae3a8 |
scripts/qapi-visit.py | 90 +++++++++++++++++++++++++++++++++++++-------------
|
|
|
9ae3a8 |
3 files changed, 98 insertions(+), 25 deletions(-)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
docs/qapi-code-gen.txt | 22 ++++++++++++
|
|
|
9ae3a8 |
scripts/qapi-types.py | 11 +++++-
|
|
|
9ae3a8 |
scripts/qapi-visit.py | 90 +++++++++++++++++++++++++++++++++++------------
|
|
|
9ae3a8 |
3 files changed, 98 insertions(+), 25 deletions(-)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt
|
|
|
9ae3a8 |
index f6f8d33..11f19cf 100644
|
|
|
9ae3a8 |
--- a/docs/qapi-code-gen.txt
|
|
|
9ae3a8 |
+++ b/docs/qapi-code-gen.txt
|
|
|
9ae3a8 |
@@ -103,6 +103,28 @@ And it looks like this on the wire:
|
|
|
9ae3a8 |
"data" : { "backing-file": "/some/place/my-image",
|
|
|
9ae3a8 |
"lazy-refcounts": true } }
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+Flat union types avoid the nesting on the wire. They are used whenever a
|
|
|
9ae3a8 |
+specific field of the base type is declared as the discriminator ('type' is
|
|
|
9ae3a8 |
+then no longer generated). The discriminator must always be a string field.
|
|
|
9ae3a8 |
+The above example can then be modified as follows:
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ { 'type': 'BlockdevCommonOptions',
|
|
|
9ae3a8 |
+ 'data': { 'driver': 'str', 'readonly': 'bool' } }
|
|
|
9ae3a8 |
+ { 'union': 'BlockdevOptions',
|
|
|
9ae3a8 |
+ 'base': 'BlockdevCommonOptions',
|
|
|
9ae3a8 |
+ 'discriminator': 'driver',
|
|
|
9ae3a8 |
+ 'data': { 'raw': 'RawOptions',
|
|
|
9ae3a8 |
+ 'qcow2': 'Qcow2Options' } }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+Resulting in this JSON object:
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ { "driver": "qcow2",
|
|
|
9ae3a8 |
+ "readonly": false,
|
|
|
9ae3a8 |
+ "backing-file": "/some/place/my-image",
|
|
|
9ae3a8 |
+ "lazy-refcounts": true }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
=== Commands ===
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Commands are defined by using a list containing three members. The first
|
|
|
9ae3a8 |
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
|
|
|
9ae3a8 |
index c0684a7..f139bfe 100644
|
|
|
9ae3a8 |
--- a/scripts/qapi-types.py
|
|
|
9ae3a8 |
+++ b/scripts/qapi-types.py
|
|
|
9ae3a8 |
@@ -135,7 +135,9 @@ def generate_union(expr):
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
name = expr['union']
|
|
|
9ae3a8 |
typeinfo = expr['data']
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
base = expr.get('base')
|
|
|
9ae3a8 |
+ discriminator = expr.get('discriminator')
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
ret = mcgen('''
|
|
|
9ae3a8 |
struct %(name)s
|
|
|
9ae3a8 |
@@ -158,8 +160,13 @@ struct %(name)s
|
|
|
9ae3a8 |
''')
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
if base:
|
|
|
9ae3a8 |
- struct = find_struct(base)
|
|
|
9ae3a8 |
- ret += generate_struct_fields(struct['data'])
|
|
|
9ae3a8 |
+ base_fields = find_struct(base)['data']
|
|
|
9ae3a8 |
+ if discriminator:
|
|
|
9ae3a8 |
+ base_fields = base_fields.copy()
|
|
|
9ae3a8 |
+ del base_fields[discriminator]
|
|
|
9ae3a8 |
+ ret += generate_struct_fields(base_fields)
|
|
|
9ae3a8 |
+ else:
|
|
|
9ae3a8 |
+ assert not discriminator
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
};
|
|
|
9ae3a8 |
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
|
|
|
9ae3a8 |
index be56ba4..b1c1ad6 100644
|
|
|
9ae3a8 |
--- a/scripts/qapi-visit.py
|
|
|
9ae3a8 |
+++ b/scripts/qapi-visit.py
|
|
|
9ae3a8 |
@@ -17,8 +17,30 @@ import os
|
|
|
9ae3a8 |
import getopt
|
|
|
9ae3a8 |
import errno
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
-def generate_visit_struct_fields(field_prefix, members):
|
|
|
9ae3a8 |
+def generate_visit_struct_fields(name, field_prefix, fn_prefix, members):
|
|
|
9ae3a8 |
+ substructs = []
|
|
|
9ae3a8 |
ret = ''
|
|
|
9ae3a8 |
+ full_name = name if not fn_prefix else "%s_%s" % (name, fn_prefix)
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ for argname, argentry, optional, structured in parse_args(members):
|
|
|
9ae3a8 |
+ if structured:
|
|
|
9ae3a8 |
+ if not fn_prefix:
|
|
|
9ae3a8 |
+ nested_fn_prefix = argname
|
|
|
9ae3a8 |
+ else:
|
|
|
9ae3a8 |
+ nested_fn_prefix = "%s_%s" % (fn_prefix, argname)
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ nested_field_prefix = "%s%s." % (field_prefix, argname)
|
|
|
9ae3a8 |
+ ret += generate_visit_struct_fields(name, nested_field_prefix,
|
|
|
9ae3a8 |
+ nested_fn_prefix, argentry)
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret += mcgen('''
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s ** obj, Error **errp)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ Error *err = NULL;
|
|
|
9ae3a8 |
+''',
|
|
|
9ae3a8 |
+ name=name, full_name=full_name)
|
|
|
9ae3a8 |
+ push_indent()
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
for argname, argentry, optional, structured in parse_args(members):
|
|
|
9ae3a8 |
if optional:
|
|
|
9ae3a8 |
@@ -31,7 +53,7 @@ if (obj && (*obj)->%(prefix)shas_%(c_name)s) {
|
|
|
9ae3a8 |
push_indent()
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
if structured:
|
|
|
9ae3a8 |
- ret += generate_visit_struct_body(field_prefix + argname, argname, argentry)
|
|
|
9ae3a8 |
+ ret += generate_visit_struct_body(full_name, argname, argentry)
|
|
|
9ae3a8 |
else:
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
visit_type_%(type)s(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", &err;;
|
|
|
9ae3a8 |
@@ -47,6 +69,12 @@ visit_type_%(type)s(m, obj ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s",
|
|
|
9ae3a8 |
visit_end_optional(m, &err;;
|
|
|
9ae3a8 |
''')
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+ pop_indent()
|
|
|
9ae3a8 |
+ ret += mcgen('''
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ error_propagate(errp, err);
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+''')
|
|
|
9ae3a8 |
return ret
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
@@ -56,8 +84,9 @@ if (!error_is_set(errp)) {
|
|
|
9ae3a8 |
''')
|
|
|
9ae3a8 |
push_indent()
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+ full_name = name if not field_prefix else "%s_%s" % (field_prefix, name)
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
if len(field_prefix):
|
|
|
9ae3a8 |
- field_prefix = field_prefix + "."
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
Error **errp = &err; /* from outer scope */
|
|
|
9ae3a8 |
Error *err = NULL;
|
|
|
9ae3a8 |
@@ -74,20 +103,14 @@ visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err;;
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
if (!err) {
|
|
|
9ae3a8 |
if (!obj || *obj) {
|
|
|
9ae3a8 |
-''')
|
|
|
9ae3a8 |
- push_indent()
|
|
|
9ae3a8 |
- push_indent()
|
|
|
9ae3a8 |
-
|
|
|
9ae3a8 |
- ret += generate_visit_struct_fields(field_prefix, members)
|
|
|
9ae3a8 |
- pop_indent()
|
|
|
9ae3a8 |
- ret += mcgen('''
|
|
|
9ae3a8 |
- error_propagate(errp, err);
|
|
|
9ae3a8 |
- err = NULL;
|
|
|
9ae3a8 |
-}
|
|
|
9ae3a8 |
-''')
|
|
|
9ae3a8 |
+ visit_type_%(name)s_fields(m, obj, &err;;
|
|
|
9ae3a8 |
+ error_propagate(errp, err);
|
|
|
9ae3a8 |
+ err = NULL;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+''',
|
|
|
9ae3a8 |
+ name=full_name)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
pop_indent()
|
|
|
9ae3a8 |
- pop_indent()
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
/* Always call end_struct if start_struct succeeded. */
|
|
|
9ae3a8 |
visit_end_struct(m, &err;;
|
|
|
9ae3a8 |
@@ -98,7 +121,9 @@ if (!err) {
|
|
|
9ae3a8 |
return ret
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
def generate_visit_struct(name, members):
|
|
|
9ae3a8 |
- ret = mcgen('''
|
|
|
9ae3a8 |
+ ret = generate_visit_struct_fields(name, "", "", members)
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret += mcgen('''
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
|
|
|
9ae3a8 |
{
|
|
|
9ae3a8 |
@@ -157,9 +182,17 @@ def generate_visit_union(expr):
|
|
|
9ae3a8 |
members = expr['data']
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
base = expr.get('base')
|
|
|
9ae3a8 |
+ discriminator = expr.get('discriminator')
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
ret = generate_visit_enum('%sKind' % name, members.keys())
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+ if base:
|
|
|
9ae3a8 |
+ base_fields = find_struct(base)['data']
|
|
|
9ae3a8 |
+ if discriminator:
|
|
|
9ae3a8 |
+ base_fields = base_fields.copy()
|
|
|
9ae3a8 |
+ del base_fields[discriminator]
|
|
|
9ae3a8 |
+ ret += generate_visit_struct_fields(name, "", "", base_fields)
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
|
|
|
9ae3a8 |
@@ -179,23 +212,34 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **
|
|
|
9ae3a8 |
push_indent()
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
if base:
|
|
|
9ae3a8 |
- struct = find_struct(base)
|
|
|
9ae3a8 |
- push_indent()
|
|
|
9ae3a8 |
- ret += generate_visit_struct_fields("", struct['data'])
|
|
|
9ae3a8 |
- pop_indent()
|
|
|
9ae3a8 |
+ ret += mcgen('''
|
|
|
9ae3a8 |
+ visit_type_%(name)s_fields(m, obj, &err;;
|
|
|
9ae3a8 |
+''',
|
|
|
9ae3a8 |
+ name=name)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
pop_indent()
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
- visit_type_%(name)sKind(m, &(*obj)->kind, "type", &err;;
|
|
|
9ae3a8 |
+ visit_type_%(name)sKind(m, &(*obj)->kind, "%(type)s", &err;;
|
|
|
9ae3a8 |
if (!err) {
|
|
|
9ae3a8 |
switch ((*obj)->kind) {
|
|
|
9ae3a8 |
''',
|
|
|
9ae3a8 |
- name=name)
|
|
|
9ae3a8 |
+ name=name, type="type" if not discriminator else discriminator)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
for key in members:
|
|
|
9ae3a8 |
+ if not discriminator:
|
|
|
9ae3a8 |
+ fmt = 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err;;'
|
|
|
9ae3a8 |
+ else:
|
|
|
9ae3a8 |
+ fmt = '''visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(c_type)s), &err;;
|
|
|
9ae3a8 |
+ if (!err) {
|
|
|
9ae3a8 |
+ visit_type_%(c_type)s_fields(m, &(*obj)->%(c_name)s, &err;;
|
|
|
9ae3a8 |
+ error_propagate(errp, err);
|
|
|
9ae3a8 |
+ err = NULL;
|
|
|
9ae3a8 |
+ visit_end_implicit_struct(m, &err;;
|
|
|
9ae3a8 |
+ }'''
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
ret += mcgen('''
|
|
|
9ae3a8 |
case %(abbrev)s_KIND_%(enum)s:
|
|
|
9ae3a8 |
- visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err;;
|
|
|
9ae3a8 |
+ ''' + fmt + '''
|
|
|
9ae3a8 |
break;
|
|
|
9ae3a8 |
''',
|
|
|
9ae3a8 |
abbrev = de_camel_case(name).upper(),
|
|
|
9ae3a8 |
--
|
|
|
9ae3a8 |
1.7.1
|
|
|
9ae3a8 |
|