From patchwork Thu May 12 17:38:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 95345 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 EA910B6F14 for ; Fri, 13 May 2011 03:38:36 +1000 (EST) Received: (qmail 29983 invoked by alias); 12 May 2011 17:38:35 -0000 Received: (qmail 29973 invoked by uid 22791); 12 May 2011 17:38:34 -0000 X-SWARE-Spam-Status: No, hits=-6.3 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; Thu, 12 May 2011 17:38:15 +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 p4CHcFhs008740 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 12 May 2011 13:38:15 -0400 Received: from [127.0.0.1] (ovpn-113-60.phx2.redhat.com [10.3.113.60]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p4CHcENB000923 for ; Thu, 12 May 2011 13:38:14 -0400 Message-ID: <4DCC1B06.2000702@redhat.com> Date: Thu, 12 May 2011 13:38:14 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110421 Fedora/3.1.9-2.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: Re: C++ PATCH for c++/48948 (rejecting constexpr friend that takes the current class) References: <4DCAFF44.3000801@redhat.com> In-Reply-To: <4DCAFF44.3000801@redhat.com> 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 While thinking about this issue some more it occurred to me that this deferral can be problematic for constructors, since we use whether or not a constructor is really constexpr to decide whether or not a class is literal. The rule that seems to me to make the most sense is to say that if we don't know yet whether a constructor is really constexpr, it doesn't satisfy the literal class requirement for a constexpr constructor. Tested x86_64-pc-linux-gnu, applied to trunk. commit 7919b46302ba6182d2dfa31d33eacbc2335de31e Author: Jason Merrill Date: Thu May 12 12:33:08 2011 -0400 * cp-tree.h (DECL_DEFERRED_CONSTEXPR_CHECK): New. * semantics.c (validate_constexpr_fundecl): Set it. (check_deferred_constexpr_decls): Clear it. (register_constexpr_fundef): Make sure it isn't set. * decl.c (grok_special_member_properties): Check it. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index ec59346..bcf78f8 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -93,6 +93,7 @@ c-common.h, not after. TYPENAME_IS_RESOLVING_P (in TYPE_NAME_TYPE) LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (in LAMBDA_EXPR) TARGET_EXPR_DIRECT_INIT_P (in TARGET_EXPR) + DECL_DEFERRED_CONSTEXPR_CHECK (in FUNCTION_DECL) 3: (TREE_REFERENCE_EXPR) (in NON_LVALUE_EXPR) (commented-out). ICS_BAD_FLAG (in _CONV) FN_TRY_BLOCK_P (in TRY_BLOCK) @@ -2338,6 +2339,11 @@ struct GTY((variable_size)) lang_decl { #define DECL_DECLARED_CONSTEXPR_P(DECL) \ DECL_LANG_FLAG_8 (VAR_OR_FUNCTION_DECL_CHECK (STRIP_TEMPLATE (DECL))) +/* True if we can't tell yet whether the argument/return types of DECL + are literal because one is still being defined. */ +#define DECL_DEFERRED_CONSTEXPR_CHECK(DECL) \ + TREE_LANG_FLAG_2 (FUNCTION_DECL_CHECK (STRIP_TEMPLATE (DECL))) + /* Nonzero if this DECL is the __PRETTY_FUNCTION__ variable in a template function. */ #define DECL_PRETTY_FUNCTION_P(NODE) \ diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 87be112..7939140 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -10681,6 +10681,9 @@ grok_special_member_properties (tree decl) TYPE_HAS_LIST_CTOR (class_type) = 1; if (DECL_DECLARED_CONSTEXPR_P (decl) + /* It doesn't count if we can't tell yet whether or not + the constructor is actually constexpr. */ + && !DECL_DEFERRED_CONSTEXPR_CHECK (decl) && !copy_fn_p (decl) && !move_fn_p (decl)) TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1; } diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index ffabad1..f1f3121 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -5483,7 +5483,10 @@ check_deferred_constexpr_decls (void) deferred_constexpr_decls = NULL; FOR_EACH_VEC_ELT (tree, vec, i, fn) - validate_constexpr_fundecl (fn); + { + DECL_DEFERRED_CONSTEXPR_CHECK (fn) = false; + validate_constexpr_fundecl (fn); + } if (deferred_constexpr_decls == NULL) { @@ -5516,6 +5519,7 @@ validate_constexpr_fundecl (tree fun) /*defer_ok=*/true); if (valid < 0) { + DECL_DEFERRED_CONSTEXPR_CHECK (fun) = true; VEC_safe_push (tree, gc, deferred_constexpr_decls, fun); return NULL; } @@ -5764,6 +5768,9 @@ register_constexpr_fundef (tree fun, tree body) constexpr_fundef entry; constexpr_fundef **slot; + gcc_assert (DECL_DECLARED_CONSTEXPR_P (fun) + && !DECL_DEFERRED_CONSTEXPR_CHECK (fun)); + if (DECL_CONSTRUCTOR_P (fun)) body = build_constexpr_constructor_member_initializers (DECL_CONTEXT (fun), body); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-incomplete2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-incomplete2.C new file mode 100644 index 0000000..7a9a24d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-incomplete2.C @@ -0,0 +1,31 @@ +// A constructor that might or might not be constexpr doesn't make +// its class literal. +// { dg-options -std=c++0x } + +template +struct B +{ + constexpr B(T) { } + constexpr B() {} +}; + +struct A +{ + B b; +}; + +constexpr A a {}; + +template +struct C +{ + constexpr C(T) { } + C() {} +}; + +struct D +{ + C c; +}; + +constexpr D d {}; // { dg-error "not literal" }