Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> --- scripts/qapi.py | 14 +++++++++----- scripts/qapi-introspect.py | 3 ++- scripts/qapi-types.py | 2 ++ scripts/qapi-visit.py | 4 ++++ tests/qapi-schema/qapi-schema-test.json | 6 +++++- tests/qapi-schema/qapi-schema-test.out | 9 ++++++++- tests/qapi-schema/test-qapi.py | 3 ++- 7 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/scripts/qapi.py b/scripts/qapi.py index 1eb40590fb..333a5c0d1e 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -1398,8 +1398,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): @@ -1646,6 +1646,10 @@ class QAPISchema(object): return QAPISchemaObjectTypeVariant(case, typ) def _make_simple_variant(self, case, typ, info): + ifcond = None + if isinstance(typ, dict): + ifcond = typ.get('if') + typ = typ['type'] if isinstance(typ, list): assert len(typ) == 1 typ = self._make_array_type(typ[0], info) @@ -1653,7 +1657,7 @@ class QAPISchema(object): typ = self._make_implicit_object_type( typ, info, None, 'wrapper', [self._make_member('data', typ, info)], type_entity.ifcond) - return QAPISchemaObjectTypeVariant(case, typ) + return QAPISchemaObjectTypeVariant(case, typ, ifcond) def _def_union_type(self, expr, info, doc): name = expr['union'] @@ -1672,8 +1676,8 @@ class QAPISchema(object): else: variants = [self._make_simple_variant(key, value, info) for (key, value) in data.iteritems()] - typ = self._make_implicit_enum_type(name, info, - [v.name for v in variants], + values = [{'name': v.name, 'if': v.ifcond} for v in variants] + typ = self._make_implicit_enum_type(name, info, values, expr.get('if')) tag_member = QAPISchemaObjectTypeMember('type', typ, False) members = [tag_member] diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py index 98b320a79e..a867ea5de1 100644 --- a/scripts/qapi-introspect.py +++ b/scripts/qapi-introspect.py @@ -139,7 +139,8 @@ const QLitObject %(c_name)s = %(c_string)s; 'variants': [self._gen_variant(v) for v in variants]} def _gen_variant(self, variant): - return {'case': variant.name, 'type': self._use_type(variant.type)} + return ({'case': variant.name, 'type': self._use_type(variant.type)}, + variant.ifcond) def visit_builtin_type(self, name, info, json_type): self._gen_qlit(name, 'builtin', {'json-type': json_type}, None) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 659fb1da86..d6c8feb55a 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -128,11 +128,13 @@ def gen_variants(variants): c_name=c_name(variants.tag_member.name)) for var in variants.variants: + ret += gen_if(var.ifcond) ret += mcgen(''' %(c_type)s %(c_name)s; ''', c_type=var.type.c_unboxed_type(), c_name=c_name(var.name)) + ret += gen_endif(var.ifcond) ret += mcgen(''' } u; diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index f400cef13c..0f5cae33e4 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -82,6 +82,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) c_name=c_name(variants.tag_member.name)) for var in variants.variants: + ret += gen_if(var.ifcond) ret += mcgen(''' case %(case)s: visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err); @@ -92,6 +93,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) variants.tag_member.type.prefix), c_type=var.type.c_name(), c_name=c_name(var.name)) + ret += gen_endif(var.ifcond) ret += mcgen(''' default: abort(); @@ -182,6 +184,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error c_name=c_name(name)) for var in variants.variants: + ret += gen_if(var.ifcond) ret += mcgen(''' case %(case)s: ''', @@ -209,6 +212,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error ret += mcgen(''' break; ''') + ret += gen_endif(var.ifcond) ret += mcgen(''' case QTYPE_NONE: diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qapi-schema-test.json index bb515280d2..01f7db58dc 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -199,9 +199,13 @@ [ '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' }, + 'if': 'defined(TEST_IF_UNION)' } + { 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' }, 'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' } diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out index b17c32a45f..7908eed6fc 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -73,8 +73,12 @@ 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) -enum TestIfUnionKind ['foo'] +command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None + gen=True success_response=True boxed=False + if defined(TEST_IF_UNION) +enum TestIfUnionKind ['foo', ('union_bar', 'defined(TEST_IF_UNION_BAR)')] if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT) object TestStruct member integer: int optional=False @@ -204,6 +208,9 @@ object q_obj_TestIfEvent-arg member foo: TestIfStruct optional=False member bar: TestIfEnum optional=False if=TEST_IF_EVT_BAR if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT) +object q_obj_TestIfUnionCmd-arg + member union_cmd_arg: TestIfUnion optional=False + if defined(TEST_IF_UNION) object q_obj_TestStruct-wrapper member data: TestStruct optional=False object q_obj_UserDefFlatUnion2-base diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index 5d2f67a1d3..b360d44df1 100644 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -59,7 +59,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): if variants: print ' tag %s' % variants.tag_member.name for v in variants.variants: - print ' case %s: %s' % (v.name, v.type.name) + print ' case %s: %s' % (v.name, v.type.name) + \ + (' if=%s' % v.ifcond if v.ifcond else '') @staticmethod def _print_if(ifcond): -- 2.14.0.rc0.1.g40ca67566