From patchwork Sat Feb 20 00:19:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8365201 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BBDB6C0553 for ; Sat, 20 Feb 2016 00:24:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CFA6120570 for ; Sat, 20 Feb 2016 00:24:24 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 06450200D0 for ; Sat, 20 Feb 2016 00:24:24 +0000 (UTC) Received: from localhost ([::1]:56542 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWvLT-0003nt-C6 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 19 Feb 2016 19:24:23 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49032) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWvHB-0004ct-ET for qemu-devel@nongnu.org; Fri, 19 Feb 2016 19:19:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWvH8-0003uJ-TY for qemu-devel@nongnu.org; Fri, 19 Feb 2016 19:19:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38769) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWvH8-0003u0-ME for qemu-devel@nongnu.org; Fri, 19 Feb 2016 19:19:54 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 5F7AD486A5; Sat, 20 Feb 2016 00:19:54 +0000 (UTC) Received: from red.redhat.com (ovpn-113-75.phx2.redhat.com [10.3.113.75]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1K0Jn6P025514; Fri, 19 Feb 2016 19:19:54 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Fri, 19 Feb 2016 17:19:38 -0700 Message-Id: <1455927587-28033-9-git-send-email-eblake@redhat.com> In-Reply-To: <1455927587-28033-1-git-send-email-eblake@redhat.com> References: <1455927587-28033-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH 08/17] qapi-visit: Factor out gen_visit_fields_call() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Upcoming patches will be adding several contexts where we want to handle the visit of an implicit type (an anonymous base type, or an anonymous branch of a flat union) by directly inlining the visit of each of the implicit type's member fields. The work is made easier by factoring out a new helper method, gen_visit_fields_call(), so that the caller doesn't need to care whether the type it is visiting is implicit or normal. For now, the only implicit type we encounter are the branches of a simple union; the initial implementation of the helper method is hard-coded to that usage, but it gets us one step closer to completely dropping the hack of simple_union_type(). Generated output is unchanged. Signed-off-by: Eric Blake --- scripts/qapi-visit.py | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 2308268..ab773f6 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -31,7 +31,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_type)sobj, Error ** def gen_visit_fields_decl(typ): - if typ.name in struct_fields_seen: + if typ.is_implicit() or typ.name in struct_fields_seen: return '' struct_fields_seen.add(typ.name) return mcgen(''' @@ -41,6 +41,25 @@ static void visit_type_%(c_type)s_fields(Visitor *v, %(c_type)s *obj, Error **er c_type=typ.c_name()) +def gen_visit_fields_call(typ, c_name): + ret = '' + assert isinstance(typ, QAPISchemaObjectType) + if typ.is_implicit(): + # TODO ugly special case for simple union + assert len(typ.members) == 1 + assert not typ.variants + ret += mcgen(''' + visit_type_%(c_type)s(v, "data", %(c_name)s, &err); +''', + c_type=typ.members[0].type.c_name(), c_name=c_name) + else: + ret += mcgen(''' + visit_type_%(c_type)s_fields(v, %(c_name)s, &err); +''', + c_type=typ.c_name(), c_name=c_name) + return ret + + def gen_visit_struct_fields(name, base, members, variants): ret = '' @@ -48,9 +67,7 @@ def gen_visit_struct_fields(name, base, members, variants): ret += gen_visit_fields_decl(base) if variants: for var in variants.variants: - # Ugly special case for simple union TODO get rid of it - if not var.simple_union_type(): - ret += gen_visit_fields_decl(var.type) + ret += gen_visit_fields_decl(var.type) struct_fields_seen.add(name) ret += mcgen(''' @@ -63,10 +80,7 @@ static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s *obj, Error **er c_name=c_name(name)) if base: - ret += mcgen(''' - visit_type_%(c_type)s_fields(v, (%(c_type)s *)obj, &err); -''', - c_type=base.c_name()) + ret += gen_visit_fields_call(base, '(%s *)obj' % base.c_name()) ret += gen_err_check() ret += gen_visit_fields(members, prefix='obj->') @@ -78,26 +92,16 @@ static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s *obj, Error **er c_name=c_name(variants.tag_member.name)) for var in variants.variants: - # TODO ugly special case for simple union - simple_union_type = var.simple_union_type() ret += mcgen(''' case %(case)s: ''', case=c_enum_const(variants.tag_member.type.name, var.name, variants.tag_member.type.prefix)) - if simple_union_type: - ret += mcgen(''' - visit_type_%(c_type)s(v, "data", &obj->u.%(c_name)s, &err); -''', - c_type=simple_union_type.c_name(), - c_name=c_name(var.name)) - else: - ret += mcgen(''' - visit_type_%(c_type)s_fields(v, &obj->u.%(c_name)s, &err); -''', - c_type=var.type.c_name(), - c_name=c_name(var.name)) + push_indent() + ret += gen_visit_fields_call(var.type, + '&obj->u.' + c_name(var.name)) + pop_indent() ret += mcgen(''' break; ''')