From patchwork Mon Dec 13 20:46:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 75404 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 C0C691007D1 for ; Tue, 14 Dec 2010 07:46:28 +1100 (EST) Received: (qmail 7487 invoked by alias); 13 Dec 2010 20:46:26 -0000 Received: (qmail 7478 invoked by uid 22791); 13 Dec 2010 20:46:25 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD 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; Mon, 13 Dec 2010 20:46:20 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oBDKkIjD031689 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 13 Dec 2010 15:46:18 -0500 Received: from [127.0.0.1] (ovpn-113-91.phx2.redhat.com [10.3.113.91]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oBDKkHsX031736 for ; Mon, 13 Dec 2010 15:46:18 -0500 Message-ID: <4D068619.9070004@redhat.com> Date: Mon, 13 Dec 2010 15:46:17 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20101206 Lightning/1.0b1 Shredder/3.0.11pre MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/46873, 46877 (ICE with constexpr constructor) 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 can also get a NOP_EXPR for the initialized member if the data member is const, so we need to handle that. Tested x86_64-pc-linux-gnu, applied to trunk. commit e84b5c98c91b6d48e532ba5ade0e05b7d2b116f2 Author: Jason Merrill Date: Mon Dec 13 13:52:09 2010 -0500 PR c++/46873 PR c++/46877 * semantics.c (build_data_member_initialization): Handle cv-qualified data member. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 1b3bfa3..27e2982 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -5483,49 +5483,43 @@ build_data_member_initialization (tree t, VEC(constructor_elt,gc) **vec) { member = TREE_OPERAND (t, 0); init = unshare_expr (TREE_OPERAND (t, 1)); - if (TREE_CODE (member) == INDIRECT_REF) - { - tree op = TREE_OPERAND (member, 0); - STRIP_NOPS (op); - gcc_assert (TREE_CODE (op) == ADDR_EXPR); - op = TREE_OPERAND (op, 0); - if (TREE_CODE (op) == COMPONENT_REF) - /* Initializing a cv-qualified member; we just looked through - the const_cast. */ - member = op; - else - { - /* Initializing an empty base; just skip it. */ - gcc_assert (is_empty_class (TREE_TYPE (member))); - return true; - } - } } else { - tree memtype; gcc_assert (TREE_CODE (t) == CALL_EXPR); member = CALL_EXPR_ARG (t, 0); - memtype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (member))); - if (TREE_CODE (member) == NOP_EXPR) - { - /* We don't put out anything for an empty base. */ - gcc_assert (is_empty_class (memtype)); - /* But if the constructor used isn't constexpr, leave in the call - so we complain later. */ - if (potential_constant_expression (t, tf_none)) - return true; - } - else - { - gcc_assert (TREE_CODE (member) == ADDR_EXPR); - member = TREE_OPERAND (member, 0); - } /* We don't use build_cplus_new here because it complains about - abstract bases. T has the wrong type, but - cxx_eval_constant_expression doesn't care. */ + abstract bases. Leaving the call unwrapped means that it has the + wrong type, but cxx_eval_constant_expression doesn't care. */ init = unshare_expr (t); } + if (TREE_CODE (member) == INDIRECT_REF) + member = TREE_OPERAND (member, 0); + if (TREE_CODE (member) == NOP_EXPR) + { + tree op = member; + STRIP_NOPS (op); + if (TREE_CODE (op) == ADDR_EXPR) + { + gcc_assert (same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (TREE_TYPE (op)), + TREE_TYPE (TREE_TYPE (member)))); + /* Initializing a cv-qualified member; we need to look through + the const_cast. */ + member = op; + } + else + { + /* We don't put out anything for an empty base. */ + gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); + /* But if the initializer isn't constexpr, leave it in so we + complain later. */ + if (potential_constant_expression (init, tf_none)) + return true; + } + } + if (TREE_CODE (member) == ADDR_EXPR) + member = TREE_OPERAND (member, 0); if (TREE_CODE (member) == COMPONENT_REF) member = TREE_OPERAND (member, 1); CONSTRUCTOR_APPEND_ELT (*vec, member, init); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor4.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor4.C new file mode 100644 index 0000000..397b4b0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor4.C @@ -0,0 +1,15 @@ +// PR c++/46873 +// { dg-options -std=c++0x } + +struct S +{ + int i:1; +}; + +struct T +{ + const S s; + constexpr T (S a = S ()) : s (a) { } +}; + +T t; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor5.C new file mode 100644 index 0000000..36b0178 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor5.C @@ -0,0 +1,30 @@ +// PR c++/46877 +// { dg-options -std=c++0x } + +struct new_allocator +{ + constexpr new_allocator (); +}; + +struct string +{ + constexpr string () + { + } + new_allocator a; +}; + +struct pair +{ + const string first; + constexpr pair () + { + } +}; + +constexpr +new_allocator::new_allocator () +{ +} + +pair p;