From patchwork Tue Nov 9 03:52:22 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 70493 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 9D9D9B712C for ; Tue, 9 Nov 2010 14:52:32 +1100 (EST) Received: (qmail 15485 invoked by alias); 9 Nov 2010 03:52:31 -0000 Received: (qmail 15476 invoked by uid 22791); 9 Nov 2010 03:52:30 -0000 X-SWARE-Spam-Status: No, hits=-6.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_TM, 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; Tue, 09 Nov 2010 03:52:25 +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.13.8/8.13.8) with ESMTP id oA93qOph010641 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 8 Nov 2010 22:52:24 -0500 Received: from [127.0.0.1] (ovpn-113-31.phx2.redhat.com [10.3.113.31]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oA93qMJ1032178 for ; Mon, 8 Nov 2010 22:52:23 -0500 Message-ID: <4CD8C576.5010602@redhat.com> Date: Mon, 08 Nov 2010 21:52:22 -0600 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101104 Lightning/1.0b1 Shredder/3.0.11pre MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/46382 (not allowing static_assert in constexpr template ctor) 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 allow typedefs and static_asserts in constexpr functions, but were failing to do so in a template constructor. Tested x86_64-pc-linux-gnu, applied to trunk. commit 707cb9659373766b84378409287a8c7e71ff38d4 Author: Jason Merrill Date: Mon Nov 8 18:46:30 2010 -0600 PR c++/46382 * semantics.c (check_constexpr_ctor_body): New fn. * parser.c (cp_parser_ctor_initializer_opt_and_function_body): Call it. * cp-tree.h: Declare it. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 241805c..67f4f93 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5249,6 +5249,7 @@ extern void finish_cleanup (tree, tree); extern bool literal_type_p (tree); extern tree validate_constexpr_fundecl (tree); extern tree register_constexpr_fundef (tree, tree); +extern bool check_constexpr_ctor_body (tree, tree); extern tree ensure_literal_type_for_constexpr_object (tree); extern bool potential_constant_expression (tree, tsubst_flags_t); extern tree cxx_constant_value (tree); diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 6a9e4d7..4897941 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -16347,14 +16347,8 @@ cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) } /* Parse the function-body. */ cp_parser_function_body (parser); - if (check_body_p - && (TREE_CODE (list) != STATEMENT_LIST - || (last == NULL && STATEMENT_LIST_TAIL (list) != NULL) - || (last != NULL && last != STATEMENT_LIST_TAIL (list)->stmt))) - { - error ("constexpr constructor does not have empty body"); - DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; - } + if (check_body_p) + check_constexpr_ctor_body (last, list); /* Finish the function body. */ finish_function_body (body); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 494247e7..b48559e 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -5522,6 +5522,48 @@ build_data_member_initialization (tree t, VEC(constructor_elt,gc) **vec) return true; } +/* Make sure that there are no statements after LAST in the constructor + body represented by LIST. */ + +bool +check_constexpr_ctor_body (tree last, tree list) +{ + bool ok = true; + if (TREE_CODE (list) == STATEMENT_LIST) + { + tree_stmt_iterator i = tsi_last (list); + for (; !tsi_end_p (i); tsi_prev (&i)) + { + tree t = tsi_stmt (i); + if (t == last) + break; + if (TREE_CODE (t) == BIND_EXPR) + { + if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t))) + return false; + else + continue; + } + /* We currently allow typedefs and static_assert. + FIXME allow them in the standard, too. */ + if (TREE_CODE (t) != STATIC_ASSERT) + { + ok = false; + break; + } + } + } + else if (list != last + && TREE_CODE (list) != STATIC_ASSERT) + ok = false; + if (!ok) + { + error ("constexpr constructor does not have empty body"); + DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; + } + return ok; +} + /* Build compile-time evalable representations of member-initializer list for a constexpr constructor. */ diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-sassert.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-sassert.C new file mode 100644 index 0000000..3e08fb0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-sassert.C @@ -0,0 +1,13 @@ +// Allow static_assert in constexpr constructors, too. +// { dg-options -std=c++0x } + +template +struct A +{ + int i; + + constexpr A(int i) : i(i) + { + static_assert(sizeof(T) == 1, ""); + } +};