From patchwork Thu Feb 25 23:38:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 588463 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 6D95D140326 for ; Fri, 26 Feb 2016 10:39:49 +1100 (AEDT) Received: from localhost ([::1]:46420 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aZ5Vb-00044w-FI for incoming@patchwork.ozlabs.org; Thu, 25 Feb 2016 18:39:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51272) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aZ5V0-0002o2-1h for qemu-devel@nongnu.org; Thu, 25 Feb 2016 18:39:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aZ5Uz-0005SC-70 for qemu-devel@nongnu.org; Thu, 25 Feb 2016 18:39:10 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53956) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aZ5Uz-0005S0-0B for qemu-devel@nongnu.org; Thu, 25 Feb 2016 18:39:09 -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 9F275C0C4263; Thu, 25 Feb 2016 23:39:08 +0000 (UTC) Received: from red.redhat.com (ovpn-113-105.phx2.redhat.com [10.3.113.105]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1PNd2Ni002433; Thu, 25 Feb 2016 18:39:08 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 25 Feb 2016 16:38:39 -0700 Message-Id: <1456443528-13901-11-git-send-email-eblake@redhat.com> In-Reply-To: <1456443528-13901-1-git-send-email-eblake@redhat.com> References: <1456443528-13901-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 v2 10/19] qapi-visit: Factor out gen_visit_members_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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 member of the implicit type. The work is made easier by factoring out a new helper, gen_visit_members_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 --- v2: retitle, rebase to s/fields/members/ changes --- scripts/qapi-visit.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index a712e9a..dbae00c 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -34,6 +34,25 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp); c_name=c_name(name)) +def gen_visit_members_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_members(v, %(c_name)s, &err); +''', + c_type=typ.c_name(), c_name=c_name) + return ret + + def gen_visit_object_members(name, base, members, variants): ret = mcgen(''' @@ -45,10 +64,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) c_name=c_name(name)) if base: - ret += mcgen(''' - visit_type_%(c_type)s_members(v, (%(c_type)s *)obj, &err); -''', - c_type=base.c_name()) + ret += gen_visit_members_call(base, '(%s *)obj' % base.c_name()) ret += gen_err_check() ret += gen_visit_members(members, prefix='obj->') @@ -60,26 +76,16 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) 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_members(v, &obj->u.%(c_name)s, &err); -''', - c_type=var.type.c_name(), - c_name=c_name(var.name)) + push_indent() + ret += gen_visit_members_call(var.type, + '&obj->u.' + c_name(var.name)) + pop_indent() ret += mcgen(''' break; ''')