From patchwork Fri Jul 8 14:22:16 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 103852 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 273AAB6F00 for ; Sat, 9 Jul 2011 00:22:42 +1000 (EST) Received: (qmail 31540 invoked by alias); 8 Jul 2011 14:22:37 -0000 Received: (qmail 31530 invoked by uid 22791); 8 Jul 2011 14:22:36 -0000 X-SWARE-Spam-Status: No, hits=-6.4 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; Fri, 08 Jul 2011 14:22:18 +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.14.4/8.14.4) with ESMTP id p68EMH0e010903 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 8 Jul 2011 10:22:17 -0400 Received: from [127.0.0.1] (ovpn-113-70.phx2.redhat.com [10.3.113.70]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p68EMGrJ012643 for ; Fri, 8 Jul 2011 10:22:17 -0400 Message-ID: <4E171298.3040203@redhat.com> Date: Fri, 08 Jul 2011 10:22:16 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/49673 (constexpr init should go in rodata) 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 Now that we have constexpr constructors, having a non-trivial constructor no longer precludes a variable being TREE_READONLY. The front end will clear TREE_READONLY if the variable requires non-constant initialization. Tested x86_64-pc-linux-gnu, applying to trunk. commit bd0343de0277ae5d66f60a42d6479df0161fc075 Author: Jason Merrill Date: Fri Jul 8 09:32:45 2011 -0400 PR c++/49673 gcc/c-family/ * c-common.c (c_apply_type_quals_to_decl): Don't check TYPE_NEEDS_CONSTRUCTING. gcc/cp/ * typeck.c (cp_apply_type_quals_to_decl): Don't check TYPE_NEEDS_CONSTRUCTING. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 67291de..f61b9cc 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -4058,14 +4058,11 @@ c_apply_type_quals_to_decl (int type_quals, tree decl) if (type == error_mark_node) return; - if (((type_quals & TYPE_QUAL_CONST) - || (type && TREE_CODE (type) == REFERENCE_TYPE)) - /* An object declared 'const' is only readonly after it is - initialized. We don't have any way of expressing this currently, - so we need to be conservative and unset TREE_READONLY for types - with constructors. Otherwise aliasing code will ignore stores in - an inline constructor. */ - && !(type && TYPE_NEEDS_CONSTRUCTING (type))) + if ((type_quals & TYPE_QUAL_CONST) + || (type && TREE_CODE (type) == REFERENCE_TYPE)) + /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr + constructor can produce constant init, so rely on the front end to + clear TREE_READONLY if the variable has non-constant init. */ TREE_READONLY (decl) = 1; if (type_quals & TYPE_QUAL_VOLATILE) { diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 2acb18e..f0d68c3 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -8127,12 +8127,12 @@ cp_apply_type_quals_to_decl (int type_quals, tree decl) && type_quals != TYPE_UNQUALIFIED)); /* Avoid setting TREE_READONLY incorrectly. */ - if (/* If the object has a constructor, the constructor may modify - the object. */ - TYPE_NEEDS_CONSTRUCTING (type) - /* If the type isn't complete, we don't know yet if it will need + /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr + constructor can produce constant init, so rely on cp_finish_decl to + clear TREE_READONLY if the variable has non-constant init. */ + if (/* If the type isn't complete, we don't know yet if it will need constructing. */ - || !COMPLETE_TYPE_P (type) + !COMPLETE_TYPE_P (type) /* If the type has a mutable component, that component might be modified. */ || TYPE_HAS_MUTABLE_P (type)) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C new file mode 100644 index 0000000..e2edb2e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C @@ -0,0 +1,11 @@ +// PR c++/49673: check that test_data goes into .rodata +// { dg-options -std=c++0x } +// { dg-final { scan-assembler "rodata" } } + +struct Data +{ + int i; + constexpr Data(int i = 0) : i(i+1) {} +}; + +extern const Data test_data = { 1 };