Received: from eggs.gnu.org ([2001:4830:134:3::10]:46797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VBzFR-0008Dw-O2 for qemu-devel@nongnu.org; Tue, 20 Aug 2013 23:38:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VBzFK-0005XS-2X for qemu-devel@nongnu.org; Tue, 20 Aug 2013 23:38:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:2797) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VBzFJ-0005Wm-Qz for qemu-devel@nongnu.org; Tue, 20 Aug 2013 23:38:09 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r7L3c7W2026759 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 20 Aug 2013 23:38:07 -0400 Date: Wed, 21 Aug 2013 11:38:05 +0800 From: Amos Kong Message-ID: <20130821033805.GA7028@amosk.info> References: <1374584606-5615-1-git-send-email-kwolf@redhat.com> <1374584606-5615-3-git-send-email-kwolf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1374584606-5615-3-git-send-email-kwolf@redhat.com> Subject: Re: [Qemu-devel] [PATCH 02/18] qapi-types.py: Implement 'base' for unions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: lcapitulino@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com, armbru@redhat.com On Tue, Jul 23, 2013 at 03:03:10PM +0200, Kevin Wolf wrote: > The new 'base' key in a union definition refers to a struct type, which > is inlined into the union definition and can represent fields common to > all kinds. > > For example the following schema definition... > > { 'type': 'BlockOptionsBase', 'data': { 'read-only': 'bool' } } > > { 'union': 'BlockOptions', > 'base': 'BlockOptionsBase', > 'data': { > 'raw': 'BlockOptionsRaw' > 'qcow2': 'BlockOptionsQcow2' > } } > > ...would result in this generated C struct: > > struct BlockOptions > { > BlockOptionsKind kind; > union { > void *data; > BlockOptionsRaw * raw; > BlockOptionsQcow2 * qcow2; > }; > bool read_only; > }; > > Signed-off-by: Kevin Wolf > --- > scripts/qapi-types.py | 16 ++++++++++++++-- > 1 file changed, 14 insertions(+), 2 deletions(-) > > diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py > index e1239e1..9882b95 100644 > --- a/scripts/qapi-types.py > +++ b/scripts/qapi-types.py > @@ -157,7 +157,12 @@ typedef enum %(name)s > > return lookup_decl + enum_decl > > -def generate_union(name, typeinfo): > +def generate_union(expr): > + > + name = expr['union'] > + typeinfo = expr['data'] > + base = expr.get('base') > + > ret = mcgen(''' > struct %(name)s > { > @@ -176,6 +181,13 @@ struct %(name)s > > ret += mcgen(''' > }; > +''') > + > + if base: > + struct = find_struct(base) > + ret += generate_struct_fields(struct['data']) generate_struct_fields() doesn't exist in upstream. [qemu-upstream]$ grep generate_struct_fields -r * scripts/qapi-types.py: ret += generate_struct_fields(struct['data']) [qemu-upstream]$ > + > + ret += mcgen(''' > }; > ''') > > @@ -359,7 +371,7 @@ for expr in exprs: > ret += generate_type_cleanup_decl(expr['type']) > fdef.write(generate_type_cleanup(expr['type']) + "\n") > elif expr.has_key('union'): > - ret += generate_union(expr['union'], expr['data']) > + ret += generate_union(expr) > ret += generate_type_cleanup_decl(expr['union'] + "List") > fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n") > ret += generate_type_cleanup_decl(expr['union']) > -- > 1.8.1.4 > -- Amos.