提交 ccadd6bc 编写于 作者: M Marc-André Lureau 提交者: Markus Armbruster

qapi: Add 'if' to implicit struct members

The generated code is for now *unconditional*.  Later patches generate
the conditionals.

Note that union discriminators may not have 'if' conditionals.
Signed-off-by: NMarc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: NMarkus Armbruster <armbru@redhat.com>
Message-Id: <20181213123724.4866-14-marcandre.lureau@redhat.com>
Message-Id: <20181213123724.4866-15-marcandre.lureau@redhat.com>
[Patches squashed, commit message tweaked]
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
上级 87adbbff
...@@ -752,6 +752,16 @@ gets its generated code guarded like this: ...@@ -752,6 +752,16 @@ gets its generated code guarded like this:
#endif /* defined(HAVE_BAR) */ #endif /* defined(HAVE_BAR) */
#endif /* defined(CONFIG_FOO) */ #endif /* defined(CONFIG_FOO) */
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)
Example: a conditional 'bar' member
{ 'struct': 'IfStruct', 'data':
{ 'foo': 'int',
'bar': { 'type': 'int', 'if': 'defined(IFCOND)'} } }
An enum value can be replaced by a dictionary with a 'name' and a 'if' An enum value can be replaced by a dictionary with a 'name' and a 'if'
key. key.
......
...@@ -705,7 +705,7 @@ def check_type(info, source, value, allow_array=False, ...@@ -705,7 +705,7 @@ def check_type(info, source, value, allow_array=False,
# Todo: allow dictionaries to represent default values of # Todo: allow dictionaries to represent default values of
# an optional argument. # an optional argument.
check_known_keys(info, "member '%s' of %s" % (key, source), check_known_keys(info, "member '%s' of %s" % (key, source),
arg, ['type'], []) arg, ['type'], ['if'])
check_type(info, "Member '%s' of %s" % (key, source), check_type(info, "Member '%s' of %s" % (key, source),
arg['type'], allow_array=True, arg['type'], allow_array=True,
allow_metas=['built-in', 'union', 'alternate', 'struct', allow_metas=['built-in', 'union', 'alternate', 'struct',
...@@ -784,6 +784,10 @@ def check_union(expr, info): ...@@ -784,6 +784,10 @@ def check_union(expr, info):
"Discriminator '%s' is not a member of base " "Discriminator '%s' is not a member of base "
"struct '%s'" "struct '%s'"
% (discriminator, base)) % (discriminator, base))
if discriminator_value.get('if'):
raise QAPISemError(info, 'The discriminator %s.%s for union %s '
'must not be conditional' %
(base, discriminator, name))
enum_define = enum_types.get(discriminator_value['type']) enum_define = enum_types.get(discriminator_value['type'])
allow_metas = ['struct'] allow_metas = ['struct']
# Do not allow string discriminator # Do not allow string discriminator
...@@ -1412,8 +1416,8 @@ class QAPISchemaMember(object): ...@@ -1412,8 +1416,8 @@ class QAPISchemaMember(object):
class QAPISchemaObjectTypeMember(QAPISchemaMember): class QAPISchemaObjectTypeMember(QAPISchemaMember):
def __init__(self, name, typ, optional): def __init__(self, name, typ, optional, ifcond=None):
QAPISchemaMember.__init__(self, name) QAPISchemaMember.__init__(self, name, ifcond)
assert isinstance(typ, str) assert isinstance(typ, str)
assert isinstance(optional, bool) assert isinstance(optional, bool)
self._type_name = typ self._type_name = typ
...@@ -1727,7 +1731,7 @@ class QAPISchema(object): ...@@ -1727,7 +1731,7 @@ class QAPISchema(object):
name, info, doc, ifcond, name, info, doc, ifcond,
self._make_enum_members(data), prefix)) self._make_enum_members(data), prefix))
def _make_member(self, name, typ, info): def _make_member(self, name, typ, ifcond, info):
optional = False optional = False
if name.startswith('*'): if name.startswith('*'):
name = name[1:] name = name[1:]
...@@ -1735,10 +1739,10 @@ class QAPISchema(object): ...@@ -1735,10 +1739,10 @@ class QAPISchema(object):
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)
return QAPISchemaObjectTypeMember(name, typ, optional) return QAPISchemaObjectTypeMember(name, typ, optional, ifcond)
def _make_members(self, data, info): def _make_members(self, data, info):
return [self._make_member(key, value['type'], info) return [self._make_member(key, value['type'], value.get('if'), info)
for (key, value) in data.items()] for (key, value) in data.items()]
def _def_struct_type(self, expr, info, doc): def _def_struct_type(self, expr, info, doc):
...@@ -1759,7 +1763,7 @@ class QAPISchema(object): ...@@ -1759,7 +1763,7 @@ class QAPISchema(object):
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, info)]) 'wrapper', [self._make_member('data', typ, None, info)])
return QAPISchemaObjectTypeVariant(case, typ) return QAPISchemaObjectTypeVariant(case, typ)
def _def_union_type(self, expr, info, doc): def _def_union_type(self, expr, info, doc):
......
...@@ -409,6 +409,7 @@ qapi-schema += flat-union-inline-invalid-dict.json ...@@ -409,6 +409,7 @@ qapi-schema += flat-union-inline-invalid-dict.json
qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-int-branch.json
qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-branch-key.json
qapi-schema += flat-union-invalid-discriminator.json qapi-schema += flat-union-invalid-discriminator.json
qapi-schema += flat-union-invalid-if-discriminator.json
qapi-schema += flat-union-no-base.json qapi-schema += flat-union-no-base.json
qapi-schema += flat-union-optional-discriminator.json qapi-schema += flat-union-optional-discriminator.json
qapi-schema += flat-union-string-discriminator.json qapi-schema += flat-union-string-discriminator.json
......
tests/qapi-schema/flat-union-invalid-if-discriminator.json:13: The discriminator TestBase.enum1 for union TestUnion must not be conditional
{ 'enum': 'TestEnum',
'data': [ 'value1', 'value2' ] }
{ 'struct': 'TestBase',
'data': { 'enum1': { 'type': 'TestEnum', 'if': 'FOO' } } }
{ 'struct': 'TestTypeA',
'data': { 'string': 'str' } }
{ 'struct': 'TestTypeB',
'data': { 'integer': 'int' } }
{ 'union': 'TestUnion',
'base': 'TestBase',
'discriminator': 'enum1',
'data': { 'value1': 'TestTypeA',
'value2': 'TestTypeB' } }
...@@ -201,7 +201,9 @@ ...@@ -201,7 +201,9 @@
# test 'if' condition handling # test 'if' condition handling
{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' }, { 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',
'bar': { 'type': 'int', 'if': 'defined(TEST_IF_STRUCT_BAR)'} },
'if': 'defined(TEST_IF_STRUCT)' } 'if': 'defined(TEST_IF_STRUCT)' }
{ 'enum': 'TestIfEnum', 'data': { 'enum': 'TestIfEnum', 'data':
...@@ -220,11 +222,15 @@ ...@@ -220,11 +222,15 @@
{ 'command': 'TestIfAlternateCmd', 'data': { 'alt_cmd_arg': 'TestIfAlternate' }, { 'command': 'TestIfAlternateCmd', 'data': { 'alt_cmd_arg': 'TestIfAlternate' },
'if': 'defined(TEST_IF_ALT)' } 'if': 'defined(TEST_IF_ALT)' }
{ 'command': 'TestIfCmd', 'data': { 'foo': 'TestIfStruct', 'bar': 'TestIfEnum' }, { 'command': 'TestIfCmd', 'data':
{ 'foo': 'TestIfStruct',
'bar': { 'type': 'TestIfEnum', 'if': 'defined(TEST_IF_CMD_BAR)' } },
'returns': 'UserDefThree', 'returns': 'UserDefThree',
'if': ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] } 'if': ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] }
{ 'command': 'TestCmdReturnDefThree', 'returns': 'UserDefThree' } { 'command': 'TestCmdReturnDefThree', 'returns': 'UserDefThree' }
{ 'event': 'TestIfEvent', 'data': { 'foo': 'TestIfStruct' }, { 'event': 'TestIfEvent', 'data':
{ 'foo': 'TestIfStruct',
'bar': { 'type': 'TestIfEnum', 'if': 'defined(TEST_IF_EVT_BAR)' } },
'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' } 'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' }
...@@ -268,6 +268,8 @@ command __org.qemu_x-command q_obj___org.qemu_x-command-arg -> __org.qemu_x-Unio ...@@ -268,6 +268,8 @@ command __org.qemu_x-command q_obj___org.qemu_x-command-arg -> __org.qemu_x-Unio
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
object TestIfStruct object TestIfStruct
member foo: int optional=False member foo: int optional=False
member bar: int optional=False
if ['defined(TEST_IF_STRUCT_BAR)']
if ['defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_STRUCT)']
enum TestIfEnum enum TestIfEnum
member foo member foo
...@@ -304,6 +306,7 @@ command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None ...@@ -304,6 +306,7 @@ command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None
object q_obj_TestIfCmd-arg object q_obj_TestIfCmd-arg
member foo: TestIfStruct optional=False member foo: TestIfStruct optional=False
member bar: TestIfEnum optional=False member bar: TestIfEnum optional=False
if ['defined(TEST_IF_CMD_BAR)']
if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)']
command TestIfCmd q_obj_TestIfCmd-arg -> UserDefThree command TestIfCmd q_obj_TestIfCmd-arg -> UserDefThree
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
...@@ -312,6 +315,8 @@ command TestCmdReturnDefThree None -> UserDefThree ...@@ -312,6 +315,8 @@ command TestCmdReturnDefThree None -> UserDefThree
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
object q_obj_TestIfEvent-arg object q_obj_TestIfEvent-arg
member foo: TestIfStruct optional=False member foo: TestIfStruct optional=False
member bar: TestIfEnum optional=False
if ['defined(TEST_IF_EVT_BAR)']
if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)'] if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)']
event TestIfEvent q_obj_TestIfEvent-arg event TestIfEvent q_obj_TestIfEvent-arg
boxed=False boxed=False
......
...@@ -39,6 +39,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): ...@@ -39,6 +39,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
for m in members: for m in members:
print(' member %s: %s optional=%s' print(' member %s: %s optional=%s'
% (m.name, m.type.name, m.optional)) % (m.name, m.type.name, m.optional))
self._print_if(m.ifcond, 8)
self._print_variants(variants) self._print_variants(variants)
self._print_if(ifcond) self._print_if(ifcond)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册