These patches implement a couple bits of the C++14 constexpr enhancements.
The first patch adds support for local variables in a constexpr function with intializers that can just be substituted into the return expression.
The second patch adds diagnostics for things that are still not permitted in a constexpr function.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 9bf3eee3ac0225c7aea376d85e51e5da987fc401 Author: Jason Merrill <[email protected]> Date: Thu Oct 2 15:51:03 2014 -0400 * semantics.c (constexpr_fn_retval): Ignore declarations in C++14. (var_in_constexpr_fn): New. (cxx_eval_constant_expression): Look into DECL_INITIAL. (potential_constant_expression_1): Allow constexpr-local vars. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index fe1651e..857af76 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5833,6 +5833,7 @@ extern tree maybe_constant_value (tree); extern tree maybe_constant_init (tree); extern bool is_sub_constant_expr (tree); extern bool reduced_constant_expression_p (tree); +extern bool var_in_constexpr_fn (tree); extern void explain_invalid_constexpr_fn (tree); extern vec<tree> cx_error_context (void); extern bool is_this_parameter (tree); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 7569826..6c6a5c8 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -8018,6 +8018,8 @@ constexpr_fn_retval (tree body) case DECL_EXPR: if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL) return NULL_TREE; + if (cxx_dialect >= cxx14) + return NULL_TREE; return error_mark_node; case CLEANUP_POINT_EXPR: @@ -9596,6 +9598,14 @@ cxx_eval_trinary_expression (const constexpr_call *call, tree t, return val; } +bool +var_in_constexpr_fn (tree t) +{ + tree ctx = DECL_CONTEXT (t); + return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL + && DECL_DECLARED_CONSTEXPR_P (ctx)); +} + /* Attempt to reduce the expression T to a constant value. On failure, issue diagnostic and return error_mark_node. */ /* FIXME unify with c_fully_fold */ @@ -9635,6 +9645,11 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, if (TREE_CODE (r) == TARGET_EXPR && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) r = TARGET_EXPR_INITIAL (r); + if (DECL_P (r) && var_in_constexpr_fn (r) + && DECL_INITIAL (r)) + r = cxx_eval_constant_expression (call, DECL_INITIAL (r), + allow_non_constant, false, + non_constant_p, overflow_p); if (DECL_P (r)) { if (!allow_non_constant) @@ -10320,6 +10335,7 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) case VAR_DECL: if (want_rval && !decl_constant_var_p (t) + && !var_in_constexpr_fn (t) && !dependent_type_p (TREE_TYPE (t))) { if (flags & tf_error) diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-local1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-local1.C new file mode 100644 index 0000000..39c3ee8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-local1.C @@ -0,0 +1,9 @@ +// { dg-do compile { target c++14 } } + +constexpr int f(int i) { int j = i+1; return j; } + +constexpr int i = f(41); + +#define SA(X) static_assert((X),#X) + +SA(i==42); commit 239f96028401a2836208f21f3432c4be91265740 Author: Jason Merrill <[email protected]> Date: Fri Oct 3 06:15:02 2014 -0400 * decl.c (start_decl): Complain about static/thread_local vars in constexpr function. (check_for_uninitialized_const_var): Also uninitialized vars. * parser.c (cp_parser_jump_statement): And gotos. (cp_parser_asm_operand_list): And asm. (cp_parser_try_block): And try. * semantics.c (ensure_literal_type_for_constexpr_object): And non-literal. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 7856dd8..9c8ecc0 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -4767,6 +4767,16 @@ start_decl (const cp_declarator *declarator, DECL_THIS_STATIC (decl) = 1; } + if (current_function_decl && VAR_P (decl) + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + { + if (DECL_THREAD_LOCAL_P (decl)) + error ("%qD declared %<thread_local%> in %<constexpr%> function", + decl); + else if (TREE_STATIC (decl)) + error ("%qD declared %<static%> in %<constexpr%> function", decl); + } + if (!processing_template_decl && VAR_P (decl)) start_decl_1 (decl, initialized); @@ -5135,15 +5145,20 @@ check_for_uninitialized_const_var (tree decl) 7.1.6 */ if (VAR_P (decl) && TREE_CODE (type) != REFERENCE_TYPE - && CP_TYPE_CONST_P (type) + && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl)) && !DECL_INITIAL (decl)) { tree field = default_init_uninitialized_part (type); if (!field) return; - permerror (DECL_SOURCE_LOCATION (decl), - "uninitialized const %qD", decl); + if (CP_TYPE_CONST_P (type)) + permerror (DECL_SOURCE_LOCATION (decl), + "uninitialized const %qD", decl); + else + error_at (DECL_SOURCE_LOCATION (decl), + "uninitialized variable %qD in %<constexpr%> function", + decl); if (CLASS_TYPE_P (type)) { diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 0050b8d..18cae5b 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -10915,6 +10915,10 @@ cp_parser_jump_statement (cp_parser* parser) break; case RID_GOTO: + if (parser->in_function_body + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + error ("%<goto%> in %<constexpr%> function"); + /* Create the goto-statement. */ if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) { @@ -16484,6 +16488,11 @@ cp_parser_asm_definition (cp_parser* parser) /* Look for the `asm' keyword. */ cp_parser_require_keyword (parser, RID_ASM, RT_ASM); + + if (parser->in_function_body + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + error ("%<asm%> in %<constexpr%> function"); + /* See if the next token is `volatile'. */ if (cp_parser_allow_gnu_extensions_p (parser) && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) @@ -21441,6 +21450,10 @@ cp_parser_try_block (cp_parser* parser) tree try_block; cp_parser_require_keyword (parser, RID_TRY, RT_TRY); + if (parser->in_function_body + && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) + error ("%<try%> in %<constexpr%> function"); + try_block = begin_try_block (); cp_parser_compound_statement (parser, NULL, true, false); finish_try_block (try_block); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 6c6a5c8..5d1aafc 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7537,7 +7537,9 @@ tree ensure_literal_type_for_constexpr_object (tree decl) { tree type = TREE_TYPE (decl); - if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl) + if (VAR_P (decl) + && (DECL_DECLARED_CONSTEXPR_P (decl) + || var_in_constexpr_fn (decl)) && !processing_template_decl) { tree stype = strip_array_types (type); @@ -7546,8 +7548,12 @@ ensure_literal_type_for_constexpr_object (tree decl) when we try to initialize the variable. */; else if (!literal_type_p (type)) { - error ("the type %qT of constexpr variable %qD is not literal", - type, decl); + if (DECL_DECLARED_CONSTEXPR_P (decl)) + error ("the type %qT of constexpr variable %qD is not literal", + type, decl); + else + error ("variable %qD of non-literal type %qT in %<constexpr%> " + "function", decl, type); explain_non_literal_class (type); return NULL; } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C new file mode 100644 index 0000000..ae3dcc6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-neg1.C @@ -0,0 +1,17 @@ +// { dg-do compile { target c++14 } } + +struct A { A(); }; + +constexpr int f(int i) { + static int j = i; // { dg-error "static" } + thread_local int l = i; // { dg-error "thread_local" } + goto foo; // { dg-error "goto" } + foo: + asm("foo"); // { dg-error "asm" } + int k; // { dg-error "uninitialized" } + A a; // { dg-error "non-literal" } + return i; +} + +// FIXME remove +// { dg-prune-output "return" }
