From patchwork Wed Dec 21 19:18:38 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 132720 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 27D24B7027 for ; Thu, 22 Dec 2011 06:18:56 +1100 (EST) Received: (qmail 20806 invoked by alias); 21 Dec 2011 19:18:55 -0000 Received: (qmail 20798 invoked by uid 22791); 21 Dec 2011 19:18:54 -0000 X-SWARE-Spam-Status: No, hits=-7.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_WT X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 21 Dec 2011 19:18:40 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pBLJIec0007164 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 21 Dec 2011 14:18:40 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id pBLJIeue010531 for ; Wed, 21 Dec 2011 14:18:40 -0500 Received: from [0.0.0.0] ([10.3.113.3]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id pBLJIc59027246 for ; Wed, 21 Dec 2011 14:18:39 -0500 Message-ID: <4EF2310E.4060805@redhat.com> Date: Wed, 21 Dec 2011 14:18:38 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111112 Thunderbird/8.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/51611 (conversion to vbase in NSDMI) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org We handled use of 'this' in an NSDMI, but conversion to a virtual base still broke because it depends on having a function context set up. This patch fixes this problem by not building such conversions until we use the NSDMI in an actual constructor. Tested x86_64-pc-linux-gnu, applying to trunk. commit 1499daa7ea067fd905536a5ead1876107c8fbbc5 Author: Jason Merrill Date: Wed Dec 21 13:42:30 2011 -0500 PR c++/51611 * cp-tree.h (CONVERT_EXPR_VBASE_PATH): New. * class.c (build_base_path): Defer vbase conversion in an NSDMI. * tree.c (bot_replace): Expand it here. * cp-gimplify.c (cp_genericize_r): Make sure deferred conversion doesn't leak into GENERIC. diff --git a/gcc/cp/class.c b/gcc/cp/class.c index c96f7bf..79686a2 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -318,6 +318,19 @@ build_base_path (enum tree_code code, return expr; } + /* If we're in an NSDMI, we don't have the full constructor context yet + that we need for converting to a virtual base, so just build a stub + CONVERT_EXPR and expand it later in bot_replace. */ + if (virtual_access && fixed_type_p < 0 + && current_scope () != current_function_decl) + { + expr = build1 (CONVERT_EXPR, ptr_target_type, expr); + CONVERT_EXPR_VBASE_PATH (expr) = true; + if (!want_pointer) + expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL); + return expr; + } + /* Do we need to check for a null pointer? */ if (want_pointer && !nonnull) { diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index c68069d..e06c545 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -1100,6 +1100,8 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data) wtd->omp_ctx = omp_ctx.outer; splay_tree_delete (omp_ctx.variables); } + else if (TREE_CODE (stmt) == CONVERT_EXPR) + gcc_assert (!CONVERT_EXPR_VBASE_PATH (stmt)); pointer_set_insert (p_set, *stmt_p); diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 633b6b4..6e62bd1 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -74,6 +74,7 @@ c-common.h, not after. DECL_OVERRIDE_P (in FUNCTION_DECL) IMPLICIT_CONV_EXPR_DIRECT_INIT (in IMPLICIT_CONV_EXPR) TRANSACTION_EXPR_IS_STMT (in TRANSACTION_EXPR) + CONVERT_EXPR_VBASE_PATH (in CONVERT_EXPR) 1: IDENTIFIER_VIRTUAL_P (in IDENTIFIER_NODE) TI_PENDING_TEMPLATE_FLAG. TEMPLATE_PARMS_FOR_INLINE. @@ -4011,6 +4012,11 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter) (TREE_CODE (EXPR) == TARGET_EXPR && TREE_LANG_FLAG_2 (EXPR) \ && same_type_ignoring_top_level_qualifiers_p (TYPE, TREE_TYPE (EXPR))) +/* True if this CONVERT_EXPR is for a conversion to virtual base in + an NSDMI, and should be re-evaluated when used in a constructor. */ +#define CONVERT_EXPR_VBASE_PATH(NODE) \ + TREE_LANG_FLAG_0 (CONVERT_EXPR_CHECK (NODE)) + /* An enumeration of the kind of tags that C++ accepts. */ enum tag_types { none_type = 0, /* Not a tag type. */ diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index aabe863..634c267 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -1944,6 +1944,20 @@ bot_replace (tree* t, parsing with the real one for this function. */ *t = current_class_ptr; } + else if (TREE_CODE (*t) == CONVERT_EXPR + && CONVERT_EXPR_VBASE_PATH (*t)) + { + /* In an NSDMI build_base_path defers building conversions to virtual + bases, and we handle it here. */ + tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t))); + VEC(tree,gc) *vbases = CLASSTYPE_VBASECLASSES (current_class_type); + int i; tree binfo; + FOR_EACH_VEC_ELT (tree, vbases, i, binfo) + if (BINFO_TYPE (binfo) == basetype) + break; + *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true, + tf_warning_or_error); + } return NULL_TREE; } diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-virtual1.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-virtual1.C new file mode 100644 index 0000000..4aa8d48 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-virtual1.C @@ -0,0 +1,26 @@ +// PR c++/51611 +// { dg-options -std=c++0x } +// { dg-do run } + +struct A +{ + A(): i(42) { } + int i; + int f() { return i; } +}; + +struct B : virtual A +{ + int j = i + f(); + int k = A::i + A::f(); +}; + +struct C: B { int pad; }; + +int main() +{ + C c; + if (c.j != 84 || c.k != 84) + __builtin_abort(); +} +