From patchwork Tue Aug 18 14:19:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 508346 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 F35FC140293 for ; Wed, 19 Aug 2015 00:29:58 +1000 (AEST) Received: from localhost ([::1]:57525 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRhtk-0006S6-QS for incoming@patchwork.ozlabs.org; Tue, 18 Aug 2015 10:29:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33955) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRhkq-0002OS-BZ for qemu-devel@nongnu.org; Tue, 18 Aug 2015 10:20:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZRhkk-0005Xd-2O for qemu-devel@nongnu.org; Tue, 18 Aug 2015 10:20:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39102) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRhkj-0005Wh-TR for qemu-devel@nongnu.org; Tue, 18 Aug 2015 10:20:38 -0400 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 9AC5F35419D; Tue, 18 Aug 2015 14:20:37 +0000 (UTC) Received: from red.redhat.com (ovpn-113-180.phx2.redhat.com [10.3.113.180]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t7IEKBPc029844; Tue, 18 Aug 2015 10:20:36 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 18 Aug 2015 07:19:46 -0700 Message-Id: <1439907602-11414-5-git-send-email-eblake@redhat.com> In-Reply-To: <1439907602-11414-1-git-send-email-eblake@redhat.com> References: <1439907602-11414-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 RFC v3 04/20] qapi-visit: Remove redundant functions for flat union base 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 The code for visiting the base class of a child struct created visit_type_Base_fields(); the code for visiting the base class of a flat union created visit_type_Union_fields(). If the same type is shared between a struct and a union, the two functions differed only by whether they visited the discriminator used by the union. But if the base class always visits all its fields, then we don't need to revisit the discriminator specially for a union. By consistently visiting the base class fields under the name of the base class, we can eliminate some redundant code. Signed-off-by: Eric Blake --- scripts/qapi-visit.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index ef96345..b131c0d 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -115,7 +115,7 @@ if (err) { ''') pop_indent() - if re.search('^ *goto out;', ret, re.MULTILINE): + if base or members: ret += mcgen(''' out: @@ -241,8 +241,8 @@ def gen_visit_union(name, base, variants): ret = '' if base: - members = [m for m in base.members if m != variants.tag_member] - ret += gen_visit_struct_fields(name, None, members) + ret += gen_visit_struct_fields(base.name, base.base, + base.local_members) for var in variants.variants: # TODO ugly special case for simple union @@ -263,29 +263,31 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error ''', c_name=c_name(name), name=name) - if base: - ret += mcgen(''' - visit_type_%(c_name)s_fields(m, obj, &err); - if (err) { - goto out_obj; - } -''', - c_name=c_name(name)) - tag_key = variants.tag_member.name - ret += mcgen(''' + if base: + ret += mcgen(''' + visit_type_%(c_name)s_fields(m, (%(c_name)s **)obj, &err); + if (err) { + goto out_obj; + } +''', + c_name=c_name(base.name)) + else: + ret += mcgen(''' visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err); if (err) { goto out_obj; } +''', + c_type=variants.tag_member.type.c_name(), + c_name=c_name(tag_key), name=tag_key) + ret += mcgen(''' if (!visit_start_union(m, !!(*obj)->data, &err) || err) { goto out_obj; } switch ((*obj)->%(c_name)s) { ''', - c_type=variants.tag_member.type.c_name(), - c_name=c_name(variants.tag_member.name), - name=tag_key) + c_name=c_name(tag_key)) for var in variants.variants: # TODO ugly special case for simple union