qapi: add 'if' to union members

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 <marcandre.lureau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20181213123724.4866-16-marcandre.lureau@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Marc-André Lureau 2018-12-13 16:37:17 +04:00 committed by Markus Armbruster
parent ccadd6bcba
commit a2724280fb
5 changed files with 17 additions and 9 deletions

View file

@ -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, 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' 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 Example: a conditional 'bar' member

View file

@ -803,7 +803,7 @@ def check_union(expr, info):
check_name(info, "Member of union '%s'" % name, key) check_name(info, "Member of union '%s'" % name, key)
check_known_keys(info, "member '%s' of union '%s'" % (key, name), check_known_keys(info, "member '%s' of union '%s'" % (key, name),
value, ['type'], []) value, ['type'], ['if'])
# Each value must name a known type # Each value must name a known type
check_type(info, "Member '%s' of union '%s'" % (key, name), check_type(info, "Member '%s' of union '%s'" % (key, name),
value['type'], value['type'],
@ -1483,8 +1483,8 @@ class QAPISchemaObjectTypeVariants(object):
class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember): class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
role = 'branch' role = 'branch'
def __init__(self, name, typ): def __init__(self, name, typ, ifcond=None):
QAPISchemaObjectTypeMember.__init__(self, name, typ, False) QAPISchemaObjectTypeMember.__init__(self, name, typ, False, ifcond)
class QAPISchemaAlternateType(QAPISchemaType): class QAPISchemaAlternateType(QAPISchemaType):
@ -1757,14 +1757,14 @@ class QAPISchema(object):
def _make_variant(self, case, typ): def _make_variant(self, case, typ):
return QAPISchemaObjectTypeVariant(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): if isinstance(typ, list):
assert len(typ) == 1 assert len(typ) == 1
typ = self._make_array_type(typ[0], info) typ = self._make_array_type(typ[0], info)
typ = self._make_implicit_object_type( typ = self._make_implicit_object_type(
typ, info, None, self.lookup_type(typ), typ, info, None, self.lookup_type(typ),
'wrapper', [self._make_member('data', typ, None, info)]) '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): def _def_union_type(self, expr, info, doc):
name = expr['union'] name = expr['union']
@ -1782,9 +1782,10 @@ class QAPISchema(object):
for (key, value) in data.items()] for (key, value) in data.items()]
members = [] members = []
else: 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()] 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) typ = self._make_implicit_enum_type(name, info, ifcond, enum)
tag_member = QAPISchemaObjectTypeMember('type', typ, False) tag_member = QAPISchemaObjectTypeMember('type', typ, False)
members = [tag_member] members = [tag_member]

View file

@ -210,7 +210,9 @@
[ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ], [ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ],
'if': 'defined(TEST_IF_ENUM)' } '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)' } 'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
{ 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' }, { 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' },

View file

@ -280,11 +280,15 @@ object q_obj_TestStruct-wrapper
member data: TestStruct optional=False member data: TestStruct optional=False
enum TestIfUnionKind enum TestIfUnionKind
member foo member foo
member union_bar
if ['defined(TEST_IF_UNION_BAR)']
if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
object TestIfUnion object TestIfUnion
member type: TestIfUnionKind optional=False member type: TestIfUnionKind optional=False
tag type tag type
case foo: q_obj_TestStruct-wrapper 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)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']
object q_obj_TestIfUnionCmd-arg object q_obj_TestIfUnionCmd-arg
member union_cmd_arg: TestIfUnion optional=False member union_cmd_arg: TestIfUnion optional=False

View file

@ -68,6 +68,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
print(' tag %s' % variants.tag_member.name) print(' tag %s' % variants.tag_member.name)
for v in variants.variants: for v in variants.variants:
print(' case %s: %s' % (v.name, v.type.name)) print(' case %s: %s' % (v.name, v.type.name))
QAPISchemaTestVisitor._print_if(v.ifcond, indent=8)
@staticmethod @staticmethod
def _print_if(ifcond, indent=4): def _print_if(ifcond, indent=4):