Received: from eggs.gnu.org ([2001:4830:134:3::10]:44834) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGlEs-0001qp-RG for qemu-devel@nongnu.org; Fri, 21 Feb 2014 03:13:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WGlEo-0008E3-4O for qemu-devel@nongnu.org; Fri, 21 Feb 2014 03:13:42 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39009) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGlEn-0008Dr-Sw for qemu-devel@nongnu.org; Fri, 21 Feb 2014 03:13:38 -0500 From: Markus Armbruster References: <1392875695-15627-1-git-send-email-xiawenc@linux.vnet.ibm.com> <1392875695-15627-8-git-send-email-xiawenc@linux.vnet.ibm.com> <87a9dlwxh9.fsf@blackfin.pond.sub.org> <53069B34.2060006@linux.vnet.ibm.com> Date: Fri, 21 Feb 2014 09:13:31 +0100 In-Reply-To: <53069B34.2060006@linux.vnet.ibm.com> (Wenchao Xia's message of "Fri, 21 Feb 2014 08:17:56 +0800") Message-ID: <871tywrihg.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH V7 07/11] qapi script: support pre-defined enum type as discriminator in union List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Wenchao Xia Cc: kwolf@redhat.com, mdroth@linux.vnet.ibm.com, qemu-devel@nongnu.org, lcapitulino@redhat.com Wenchao Xia writes: > =E4=BA=8E 2014/2/21 0:38, Markus Armbruster =E5=86=99=E9=81=93: >> Wenchao Xia writes: >>=20 >>> By default, any union will automatically generate a enum type as >>> "[UnionName]Kind" in C code, and it is duplicated when the discriminator >>> is specified as a pre-defined enum type in schema. After this patch, >>> the pre-defined enum type will be really used as the switch case >>> condition in generated C code, if discriminator is an enum field. >>> >>> Signed-off-by: Wenchao Xia >>> --- >>> docs/qapi-code-gen.txt | 8 ++++++-- >>> scripts/qapi-types.py | 20 ++++++++++++++++---- >>> scripts/qapi-visit.py | 27 ++++++++++++++++++++------- >>> scripts/qapi.py | 13 ++++++++++++- >>> 4 files changed, 54 insertions(+), 14 deletions(-) >>> >>> diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt >>> index 0728f36..a2e7921 100644 >>> --- a/docs/qapi-code-gen.txt >>> +++ b/docs/qapi-code-gen.txt >>> @@ -123,11 +123,15 @@ And it looks like this on the wire: >>>=20=20=20 >>> Flat union types avoid the nesting on the wire. They are used wheneve= r a >>> specific field of the base type is declared as the discriminator ('ty= pe' is >>> -then no longer generated). The discriminator must always be a string f= ield. >>> +then no longer generated). The discriminator can be a string field or a >>> +predefined enum field. If it is a string field, a hidden enum type wil= l be >>> +generated as "[UNION_NAME]Kind". If it is an enum field, a compile tim= e check >>> +will be done to verify the correctness. It is recommended to use an en= um field. >>> The above example can then be modified as follows: >>>=20=20=20 >>> + { 'enum': 'BlockdevDriver', 'data': [ 'raw', 'qcow2' ] } >>> { 'type': 'BlockdevCommonOptions', >>> - 'data': { 'driver': 'str', 'readonly': 'bool' } } >>> + 'data': { 'driver': 'BlockdevDriver', 'readonly': 'bool' } } >>> { 'union': 'BlockdevOptions', >>> 'base': 'BlockdevCommonOptions', >>> 'discriminator': 'driver', >>> diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py >>> index 656a9a0..4098c60 100644 >>> --- a/scripts/qapi-types.py >>> +++ b/scripts/qapi-types.py >>> @@ -201,14 +201,22 @@ def generate_union(expr): >>> base =3D expr.get('base') >>> discriminator =3D expr.get('discriminator') >>>=20=20=20 >>> + expr_elem =3D {'expr': expr} >>> + enum_define =3D discriminator_find_enum_define(expr_elem) >>=20 >> expr_elem has no fp, line. What if discriminator_find_enum_define >> throws a QAPIExprError? >>=20 > It shouldn't happen, since all error check happens in parse_schema(). Impossible errors often mean the abstractions aren't quite right. But this series is progress, and I don't want to delay it by demanding perfection. We can improve on it in tree if we want. >> More of the same below. [...] >>> diff --git a/scripts/qapi.py b/scripts/qapi.py >>> index 130dced..2a5eb59 100644 >>> --- a/scripts/qapi.py >>> +++ b/scripts/qapi.py >>> @@ -250,11 +250,22 @@ def parse_schema(fp): >>> add_enum(expr['enum'], expr['data']) >>> elif expr.has_key('union'): >>> add_union(expr) >>> - add_enum('%sKind' % expr['union']) >>> elif expr.has_key('type'): >>> add_struct(expr) >>> exprs.append(expr) >>>=20=20=20 >>> + # Try again for hidden UnionKind enum >>> + for expr_elem in schema.exprs: >>> + expr =3D expr_elem['expr'] >>> + if expr.has_key('union'): >>> + try: >>> + enum_define =3D discriminator_find_enum_define(expr_el= em) >>> + except QAPIExprError, e: >>> + print >>sys.stderr, e >>> + exit(1) >>> + if not enum_define: >>> + add_enum('%sKind' % expr['union']) >>> + >>> try: >>> check_exprs(schema) >>> except QAPIExprError, e: >>=20 >> I guess you move this into its own loop because when base types are used >> before they're defined, or an enum type is used for a discriminator >> before it's defined, then discriminator_find_enum_define() complains. >> Correct? >>=20 > Exactly, which allow enum define after usage in schema. Do we want to (have to?) support "use before define" in schemas? Eric, what do you think? If yes, we should add suitable tests. Outside the scope of this series.