From patchwork Fri Oct 16 04:15:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 531008 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 60E311402BF for ; Fri, 16 Oct 2015 15:16:45 +1100 (AEDT) Received: from localhost ([::1]:50755 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmwRf-0001CY-5W for incoming@patchwork.ozlabs.org; Fri, 16 Oct 2015 00:16:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49499) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmwQq-00005s-ET for qemu-devel@nongnu.org; Fri, 16 Oct 2015 00:15:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZmwQm-0004UX-U7 for qemu-devel@nongnu.org; Fri, 16 Oct 2015 00:15:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48783) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmwQm-0004UF-Lf for qemu-devel@nongnu.org; Fri, 16 Oct 2015 00:15:48 -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 2E114319B60; Fri, 16 Oct 2015 04:15:48 +0000 (UTC) Received: from red.redhat.com (ovpn-113-61.phx2.redhat.com [10.3.113.61]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9G4FijG004494; Fri, 16 Oct 2015 00:15:47 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 15 Oct 2015 22:15:31 -0600 Message-Id: <1444968943-11254-7-git-send-email-eblake@redhat.com> In-Reply-To: <1444968943-11254-1-git-send-email-eblake@redhat.com> References: <1444968943-11254-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 v9 06/17] 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() which covers all fields of Base; while the code for visiting the base class of a flat union created visit_type_Union_fields() covering all fields of the base except the discriminator. But if the base class were to always visit all its fields, then we wouldn't need a separate visit of the discriminator for a union. Not only is consistently visiting all fields easier to understand, it lets us share code. Now that gen_visit_struct_fields() can potentially collect more than one function into 'ret', a regular expression searching for whether a label was used may hit a false positive within the body of the first function. But using a regex was overkill, since we can easily determine when we jumped to a label. Signed-off-by: Eric Blake --- v9: (no v6-8): hoist from v5 35/46; rebase to master; fix indentation botch in gen_visit_union(); polish commit message --- scripts/qapi-visit.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 8aae8da..91bf350 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -90,7 +90,7 @@ static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **e ret += gen_visit_fields(members, prefix='(*obj)->') - if re.search('^ *goto out;', ret, re.MULTILINE): + if base or members: ret += mcgen(''' out: @@ -221,8 +221,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: # Ugly special case for simple union TODO get rid of it @@ -247,31 +247,30 @@ void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error if base: ret += mcgen(''' - visit_type_%(c_name)s_fields(v, obj, &err); + visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err); ''', - c_name=c_name(name)) - ret += gen_err_check(label='out_obj') - - tag_key = variants.tag_member.name - if not variants.tag_name: - # we pointlessly use a different key for simple unions - tag_key = 'type' - ret += mcgen(''' + c_name=base.c_name()) + else: + ret += mcgen(''' visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err); - if (err) { - goto out_obj; - } +''', + c_type=variants.tag_member.type.c_name(), + # TODO ugly special case for simple union + # Use same tag name in C as on the wire to get rid of + # it, then: c_name=c_name(variants.tag_member.name) + c_name='kind', + name=variants.tag_member.name) + ret += gen_err_check(label='out_obj') + ret += mcgen(''' if (!visit_start_union(v, !!(*obj)->data, &err) || err) { goto out_obj; } switch ((*obj)->%(c_name)s) { ''', - c_type=variants.tag_member.type.c_name(), # TODO ugly special case for simple union # Use same tag name in C as on the wire to get rid of # it, then: c_name=c_name(variants.tag_member.name) - c_name=c_name(variants.tag_name or 'kind'), - name=tag_key) + c_name=c_name(variants.tag_name or 'kind')) for var in variants.variants: # TODO ugly special case for simple union