From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:59277) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Se8xB-0006ye-9o for qemu-devel@nongnu.org; Mon, 11 Jun 2012 14:03:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Se8x9-0006Pa-3c for qemu-devel@nongnu.org; Mon, 11 Jun 2012 14:03:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46236) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Se8x8-0006P3-Rt for qemu-devel@nongnu.org; Mon, 11 Jun 2012 14:02:59 -0400 Date: Mon, 11 Jun 2012 14:00:11 -0300 From: Luiz Capitulino Message-ID: <20120611140011.48c0e682@doriath.home> In-Reply-To: <4FD0177E.2040505@redhat.com> References: <1338591268-23962-1-git-send-email-akong@redhat.com> <1338591268-23962-6-git-send-email-akong@redhat.com> <20120606154037.48405791@doriath.home> <20120607002600.GF7733@illuin> <4FD0177E.2040505@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2 5/6] qapi: generate list struct and visit_list for enum List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Amos Kong Cc: Paolo Bonzini , aliguori@us.ibm.com, eblake@redhat.com, Michael Roth , qemu-devel@nongnu.org On Thu, 07 Jun 2012 10:52:46 +0800 Amos Kong wrote: > On 07/06/12 08:26, Michael Roth wrote: > > On Wed, Jun 06, 2012 at 03:40:37PM -0300, Luiz Capitulino wrote: > >> On Sat, 2 Jun 2012 06:54:27 +0800 > >> Amos Kong wrote: > >> > >>> Currently, if define an 'enum' and use it in one command's data, > >>> List struct for enum could not be generated, but it's used in > >>> qmp function. > >>> > >>> For example: KeyCodesList could not be generated. > >>>>>> qapi-schema.json: > >>> { 'enum': 'KeyCodes', > >>> 'data': [ 'shift', 'alt' ... ] } > >>> { 'command': 'sendkey', > >>> 'data': { 'keys': ['KeyCodes'], '*hold-time': 'int' } } > >>> > >>>>>> qmp-command.h: > >>> void qmp_sendkey(KeyCodesList * keys, bool has_hold_time, int64_t > >>> hold_time, Error **errp); > >>> > >>> This patch makes qapi can generate List struct for enum. > >> > >> This patch does it the simple way, just like any type. It generates a enum list > >> type and the functions qapi_free_yourenum() and qapi_free_yourenumlist(). > > > > Had a couple suggestions, but approach/patch seems reasonable to me. > > > >> > >> The qapi_free_yourenum() list ends up doing nothing, so it could be a good > >> idea to generate an empty body (also note that we're copying the argument's > >> value, this could bite us in the future). > > > > I think we can omit qapi_free_yourenum() completely. Humans will know > > not to free non-allocated types, and the generated marshallers > > don't use these interfaces. > > > >> > >> Another point I was wondering is that, all enums will end up having the > >> exact same code. So maybe we could generate a default int list and use it > >> for all enums. Not sure it's worth it though. > > > > Since it's generated code I don't think it's worth it, personally. > > > >> > >> Michael, Paolo, ideas? > >> > >> More review comments below. > > > Thanks for your review! > > >>> Signed-off-by: Amos Kong > >>> --- > >>> scripts/qapi-types.py | 33 +++++++++++++++++++++++++++++---- > >>> scripts/qapi-visit.py | 14 +++++++++----- > >>> 2 files changed, 38 insertions(+), 9 deletions(-) > >>> > >>> diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py > >>> index 4a734f5..c9641fb 100644 > >>> --- a/scripts/qapi-types.py > >>> +++ b/scripts/qapi-types.py > >>> @@ -16,17 +16,36 @@ import os > >>> import getopt > >>> import errno > >>> > >>> -def generate_fwd_struct(name, members): > >>> - return mcgen(''' > >>> +def generate_fwd_struct(name, members, enum=False): > >> > >> I think it's better to have generate_fwd_enum_struct(). > > I tried this before, too many duplicate codes would be introduced, > but it's more clear. You could try to move the duplicated code to a third function (or move the specific bits to generate_fwd_enum_struct() and generate_enum_declaration()). > > two functions need to be added > qapi-types.py: def generate_fwd_enum_struct(name, members): > qapi-visit.py: def generate_enum_declaration(name, members, > genlist=True): > > >>> + ret = "" > >>> + if not enum: > >>> + ret += mcgen(''' > >>> typedef struct %(name)s %(name)s; > >>> > >>> +''', > >>> + name=name) > >>> + ret += mcgen(''' > >>> typedef struct %(name)sList > >>> { > >>> - %(name)s *value; > >>> +''', > >>> + name=name) > >>> + if enum: > >>> + ret += mcgen(''' > >>> + %(name)s value; > >>> +''', > >>> + name=name) > >>> + else: > >>> + ret += mcgen(''' > >>> + %(name)s * value; > >>> +''', > >>> + name=name) > >>> + > >>> + ret += mcgen(''' > >>> struct %(name)sList *next; > >>> } %(name)sList; > >>> ''', > >>> name=name) > >>> + return ret > >>> > >>> def generate_struct(structname, fieldname, members): > >>> ret = mcgen(''' > >>> @@ -265,7 +284,8 @@ for expr in exprs: > >>> if expr.has_key('type'): > >>> ret += generate_fwd_struct(expr['type'], expr['data']) > >>> elif expr.has_key('enum'): > >>> - ret += generate_enum(expr['enum'], expr['data']) > >>> + ret += generate_enum(expr['enum'], expr['data']) + "\n" > >> > >> The new-line should be returned by generate_enum(). Same applies for the > >> occurrences below. > > > "\n" is used to add a blank line here. > > >>> + ret += generate_fwd_struct(expr['enum'], expr['data'], True) > >>> fdef.write(generate_enum_lookup(expr['enum'], expr['data'])) > >>> elif expr.has_key('union'): > >>> ret += generate_fwd_struct(expr['union'], expr['data']) + "\n" > >>> @@ -289,6 +309,11 @@ for expr in exprs: > >>> fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n") > >>> ret += generate_type_cleanup_decl(expr['union']) > >>> fdef.write(generate_type_cleanup(expr['union']) + "\n") > >>> + elif expr.has_key('enum'): > >>> + ret += generate_type_cleanup_decl(expr['enum'] + "List") > >>> + fdef.write(generate_type_cleanup(expr['enum'] + "List") + "\n") > >>> + ret += generate_type_cleanup_decl(expr['enum']) > >>> + fdef.write(generate_type_cleanup(expr['enum']) + "\n") > >>> else: > >>> continue > >>> fdecl.write(ret) > >>> diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py > >>> index 8d4e94a..e44edfa 100644 > >>> --- a/scripts/qapi-visit.py > >>> +++ b/scripts/qapi-visit.py > >>> @@ -81,7 +81,7 @@ end: > >>> ''') > >>> return ret > >>> > >>> -def generate_visit_list(name, members): > >>> +def generate_visit_list(name, members, enum=False): > >>> return mcgen(''' > >>> > >>> void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp) > >>> @@ -160,12 +160,14 @@ end: > >>> > >>> return ret > >>> > >>> -def generate_declaration(name, members, genlist=True): > >>> - ret = mcgen(''' > >>> +def generate_declaration(name, members, genlist=True, enum=False): > >>> + ret = "" > >>> + if not enum: > >>> + ret = mcgen(''' > > >>> void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp); > >>> ''', > >>> - name=name) > >>> + name=name) > >> > >> Why this change? > > > Indent of above "mcgen(" changes, make them aligned. > > >>> > >>> if genlist: > >>> ret += mcgen(''' > >>> @@ -293,10 +295,12 @@ for expr in exprs: > >>> ret += generate_declaration(expr['union'], expr['data']) > >>> fdecl.write(ret) > >>> elif expr.has_key('enum'): > >>> - ret = generate_visit_enum(expr['enum'], expr['data']) > >>> + ret = generate_visit_list(expr['enum'], expr['data'], True) > >>> + ret += generate_visit_enum(expr['enum'], expr['data']) > >>> fdef.write(ret) > >>> > >>> ret = generate_decl_enum(expr['enum'], expr['data']) > >>> + ret += generate_declaration(expr['enum'], expr['data'], enum=True) > >>> fdecl.write(ret) > >>> > >>> fdecl.write(''' > >> > >> >