[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 25/32] qapi: add 'if' to union members
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PULL 25/32] qapi: add 'if' to union members |
Date: |
Thu, 13 Dec 2018 19:43:33 +0100 |
From: Marc-André Lureau <address@hidden>
Add 'if' key to union members:
{ 'union': 'TestIfUnion', 'data':
'mem': { 'type': 'str', 'if': 'COND'} }
The generated code remains unconditional for now. Later patches
generate the conditionals.
Signed-off-by: Marc-André Lureau <address@hidden>
Reviewed-by: Markus Armbruster <address@hidden>
Message-Id: <address@hidden>
Signed-off-by: Markus Armbruster <address@hidden>
---
docs/devel/qapi-code-gen.txt | 2 +-
scripts/qapi/common.py | 15 ++++++++-------
tests/qapi-schema/qapi-schema-test.json | 4 +++-
tests/qapi-schema/qapi-schema-test.out | 4 ++++
tests/qapi-schema/test-qapi.py | 1 +
5 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt
index 3895808b4a..0a0e53ede5 100644
--- a/docs/devel/qapi-code-gen.txt
+++ b/docs/devel/qapi-code-gen.txt
@@ -754,7 +754,7 @@ gets its generated code guarded like this:
Where a member can be defined with a single string value for its type,
it is also possible to supply a dictionary instead with both 'type'
-and 'if' keys. (TODO: union and alternate)
+and 'if' keys. (TODO: alternate)
Example: a conditional 'bar' member
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index 98e9d6f109..44fe8868ed 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -803,7 +803,7 @@ def check_union(expr, info):
check_name(info, "Member of union '%s'" % name, key)
check_known_keys(info, "member '%s' of union '%s'" % (key, name),
- value, ['type'], [])
+ value, ['type'], ['if'])
# Each value must name a known type
check_type(info, "Member '%s' of union '%s'" % (key, name),
value['type'],
@@ -1483,8 +1483,8 @@ class QAPISchemaObjectTypeVariants(object):
class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
role = 'branch'
- def __init__(self, name, typ):
- QAPISchemaObjectTypeMember.__init__(self, name, typ, False)
+ def __init__(self, name, typ, ifcond=None):
+ QAPISchemaObjectTypeMember.__init__(self, name, typ, False, ifcond)
class QAPISchemaAlternateType(QAPISchemaType):
@@ -1757,14 +1757,14 @@ class QAPISchema(object):
def _make_variant(self, case, typ):
return QAPISchemaObjectTypeVariant(case, typ)
- def _make_simple_variant(self, case, typ, info):
+ def _make_simple_variant(self, case, typ, ifcond, info):
if isinstance(typ, list):
assert len(typ) == 1
typ = self._make_array_type(typ[0], info)
typ = self._make_implicit_object_type(
typ, info, None, self.lookup_type(typ),
'wrapper', [self._make_member('data', typ, None, info)])
- return QAPISchemaObjectTypeVariant(case, typ)
+ return QAPISchemaObjectTypeVariant(case, typ, ifcond)
def _def_union_type(self, expr, info, doc):
name = expr['union']
@@ -1782,9 +1782,10 @@ class QAPISchema(object):
for (key, value) in data.items()]
members = []
else:
- variants = [self._make_simple_variant(key, value['type'], info)
+ variants = [self._make_simple_variant(key, value['type'],
+ value.get('if'), info)
for (key, value) in data.items()]
- enum = [{'name': v.name} for v in variants]
+ enum = [{'name': v.name, 'if': v.ifcond} for v in variants]
typ = self._make_implicit_enum_type(name, info, ifcond, enum)
tag_member = QAPISchemaObjectTypeMember('type', typ, False)
members = [tag_member]
diff --git a/tests/qapi-schema/qapi-schema-test.json
b/tests/qapi-schema/qapi-schema-test.json
index c46f3b5732..a33eff8f86 100644
--- a/tests/qapi-schema/qapi-schema-test.json
+++ b/tests/qapi-schema/qapi-schema-test.json
@@ -210,7 +210,9 @@
[ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ],
'if': 'defined(TEST_IF_ENUM)' }
-{ 'union': 'TestIfUnion', 'data': { 'foo': 'TestStruct' },
+{ 'union': 'TestIfUnion', 'data':
+ { 'foo': 'TestStruct',
+ 'union_bar': { 'type': 'str', 'if': 'defined(TEST_IF_UNION_BAR)'} },
'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
{ 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' },
diff --git a/tests/qapi-schema/qapi-schema-test.out
b/tests/qapi-schema/qapi-schema-test.out
index 7987b23403..f4c11f1e6a 100644
--- a/tests/qapi-schema/qapi-schema-test.out
+++ b/tests/qapi-schema/qapi-schema-test.out
@@ -280,11 +280,15 @@ object q_obj_TestStruct-wrapper
member data: TestStruct optional=False
enum TestIfUnionKind
member foo
+ member union_bar
+ if ['defined(TEST_IF_UNION_BAR)']
if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
object TestIfUnion
member type: TestIfUnionKind optional=False
tag type
case foo: q_obj_TestStruct-wrapper
+ case union_bar: q_obj_str-wrapper
+ if ['defined(TEST_IF_UNION_BAR)']
if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
object q_obj_TestIfUnionCmd-arg
member union_cmd_arg: TestIfUnion optional=False
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index 27081cb50c..d592854601 100644
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -68,6 +68,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
print(' tag %s' % variants.tag_member.name)
for v in variants.variants:
print(' case %s: %s' % (v.name, v.type.name))
+ QAPISchemaTestVisitor._print_if(v.ifcond, indent=8)
@staticmethod
def _print_if(ifcond, indent=4):
--
2.17.2
- [Qemu-devel] [PULL 02/32] cutils: Fix qemu_strtosz() & friends to reject non-finite sizes, (continued)
- [Qemu-devel] [PULL 02/32] cutils: Fix qemu_strtosz() & friends to reject non-finite sizes, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 19/32] qapi: improve reporting of unknown or missing keys, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 30/32] qapi: add condition to variants documentation, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 14/32] qapi: break long lines at 'data' member, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 21/32] qapi: add 'if' to enum members, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 01/32] cutils: Add qemu_strtod() and qemu_strtod_finite(), Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 09/32] test-string-input-visitor: Add range overflow tests, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 32/32] qapi: add conditions to REPLICATION type/commands on the schema, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 26/32] qapi: add 'if' to alternate members, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 20/32] qapi: add a dictionary form with 'name' key for enum members, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 25/32] qapi: add 'if' to union members,
Markus Armbruster <=
- [Qemu-devel] [PULL 11/32] json: Fix to reject duplicate object member names, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 29/32] qapi: add 'If:' condition to struct members documentation, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 16/32] qapi: change enum visitor and gen_enum* to take QAPISchemaMember, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 18/32] qapi: factor out checking for keys, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 24/32] qapi: Add 'if' to implicit struct members, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 22/32] qapi-events: add 'if' condition to implicit event enum, Markus Armbruster, 2018/12/13
- [Qemu-devel] [PULL 23/32] qapi: add a dictionary form for TYPE, Markus Armbruster, 2018/12/13
- Re: [Qemu-devel] [PULL 00/32] QAPI patches for 2018-12-13, Markus Armbruster, 2018/12/14