From nobody Tue Feb 10 12:42:19 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1515709076869899.9563050218151; Thu, 11 Jan 2018 14:17:56 -0800 (PST) Received: from localhost ([::1]:46638 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZlAT-0001OW-Qw for importer@patchew.org; Thu, 11 Jan 2018 17:17:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51383) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZkWA-0007Jg-Tq for qemu-devel@nongnu.org; Thu, 11 Jan 2018 16:36:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZkW9-00040z-KW for qemu-devel@nongnu.org; Thu, 11 Jan 2018 16:36:10 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48268) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eZkW9-0003zv-Bl for qemu-devel@nongnu.org; Thu, 11 Jan 2018 16:36:09 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 842EE5D685; Thu, 11 Jan 2018 21:36:08 +0000 (UTC) Received: from localhost (ovpn-112-44.ams2.redhat.com [10.36.112.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id E45D617982; Thu, 11 Jan 2018 21:36:06 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Thu, 11 Jan 2018 22:32:28 +0100 Message-Id: <20180111213250.16511-30-marcandre.lureau@redhat.com> In-Reply-To: <20180111213250.16511-1-marcandre.lureau@redhat.com> References: <20180111213250.16511-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 11 Jan 2018 21:36:08 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 29/51] qapi: add 'if' on union members X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Michael Roth , armbru@redhat.com, Cleber Rosa , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add 'if' key to union members: { 'union': 'TestIfUnion', 'data': 'mem': { 'type': 'str', 'if': 'COND'} } Generated code is not changed by this patch but with "qapi: add #if conditions to generated code". Signed-off-by: Marc-Andr=C3=A9 Lureau --- scripts/qapi.py | 17 +++++++++-------- tests/qapi-schema/qapi-schema-test.json | 7 ++++++- tests/qapi-schema/qapi-schema-test.out | 10 ++++++++++ tests/qapi-schema/test-qapi.py | 1 + 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 0f6d5dd179..24d6aef13f 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -803,7 +803,7 @@ def check_union(expr, info): for (key, value) in members.items(): source =3D "Member of union '%s'" % name check_name(info, source, key) - check_known_keys(info, source, value, ['type'], []) + check_known_keys(info, source, value, ['type'], ['if']) typ =3D value['type'] =20 # Each value must name a known type @@ -1460,8 +1460,8 @@ class QAPISchemaObjectTypeVariants(object): class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember): role =3D 'branch' =20 - def __init__(self, name, typ): - QAPISchemaObjectTypeMember.__init__(self, name, typ, False) + def __init__(self, name, typ, ifcond=3DNone): + QAPISchemaObjectTypeMember.__init__(self, name, typ, False, ifcond) =20 =20 class QAPISchemaAlternateType(QAPISchemaType): @@ -1722,14 +1722,14 @@ class QAPISchema(object): def _make_variant(self, case, typ): return QAPISchemaObjectTypeVariant(case, typ) =20 - def _make_simple_variant(self, case, typ, info): + def _make_simple_variant(self, case, typ, ifcond, info): if isinstance(typ, list): assert len(typ) =3D=3D 1 typ =3D self._make_array_type(typ[0], info) typ =3D 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) =20 def _def_union_type(self, expr, info, doc): name =3D expr['union'] @@ -1747,10 +1747,11 @@ class QAPISchema(object): for (key, value) in data.iteritems()] members =3D [] else: - variants =3D [self._make_simple_variant(key, value['type'], in= fo) + variants =3D [self._make_simple_variant(key, value['type'], + value.get('if'), info) for (key, value) in data.iteritems()] - typ =3D self._make_implicit_enum_type(name, info, ifcond, - [v.name for v in variants]) + enum =3D [{'name': v.name, 'if': v.ifcond} for v in variants] + typ =3D self._make_implicit_enum_type(name, info, ifcond, enum) tag_member =3D QAPISchemaObjectTypeMember('type', typ, False) members =3D [tag_member] self._def_entity( diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qa= pi-schema-test.json index e29c9f0769..b4b8a0a2cc 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -200,9 +200,14 @@ [ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ], 'if': 'defined(TEST_IF_ENUM)' } =20 -{ '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)' } =20 +{ 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' }, + 'if': 'defined(TEST_IF_UNION)' } + { 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStru= ct' }, 'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' } =20 diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qap= i-schema-test.out index 734d3f53aa..001fa143cd 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -89,9 +89,16 @@ object TestIfUnion member type: TestIfUnionKind optional=3DFalse 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)'] +command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None + gen=3DTrue success_response=3DTrue boxed=3DFalse + if ['defined(TEST_IF_UNION)'] enum TestIfUnionKind member foo + member union_bar + if ['defined(TEST_IF_UNION_BAR)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] object TestStruct member integer: int optional=3DFalse @@ -239,6 +246,9 @@ object q_obj_TestIfEvent-arg member bar: TestIfEnum optional=3DFalse if ['defined(TEST_IF_EVT_BAR)'] if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)'] +object q_obj_TestIfUnionCmd-arg + member union_cmd_arg: TestIfUnion optional=3DFalse + if ['defined(TEST_IF_UNION)'] object q_obj_TestStruct-wrapper member data: TestStruct optional=3DFalse object q_obj_UserDefFlatUnion2-base diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index 1269b8fbc5..61fb8394d7 100644 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -65,6 +65,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, 8) =20 @staticmethod def _print_if(ifcond, indent=3D4): --=20 2.16.0.rc1.1.gef27df75a1