diff mbox series

[pushed] c++: Constant expression parsing and parameters.

Message ID 20200521184507.15354-1-jason@redhat.com
State New
Headers show
Series [pushed] c++: Constant expression parsing and parameters. | expand

Commit Message

Jason Merrill May 21, 2020, 6:45 p.m. UTC
The difference between a "potential" constant-expression and a regular
constant-expression is the treatment of parameters; in a constexpr function,
a parameter is potentially constant when evaluating a call to that function,
but it is not constant during parsing of the function.
cp_parser_constant_expression should check the latter rather than the
former.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog
2020-05-21  Jason Merrill  <jason@redhat.com>

	* constexpr.c (is_rvalue_constant_expression): New.
	* parser.c (cp_parser_constant_expression): Use it.
	* decl.c (cp_finish_decl): Try to treat a constexpr initializer in a
	template as constant.
---
 gcc/cp/cp-tree.h   | 1 +
 gcc/cp/constexpr.c | 8 ++++++++
 gcc/cp/decl.c      | 2 +-
 gcc/cp/parser.c    | 4 ++--
 4 files changed, 12 insertions(+), 3 deletions(-)


base-commit: 149c8c7c27a17a2941d07e2f76b1e1c823e2fa80
diff mbox series

Patch

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 07c16144c98..db125a3a1db 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7939,6 +7939,7 @@  extern tree constexpr_fn_retval		(tree);
 extern tree ensure_literal_type_for_constexpr_object (tree);
 extern bool potential_constant_expression       (tree);
 extern bool is_constant_expression (tree);
+extern bool is_rvalue_constant_expression (tree);
 extern bool is_nondependent_constant_expression (tree);
 extern bool is_nondependent_static_init_expression (tree);
 extern bool is_static_init_expression    (tree);
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index c1ab9288f72..98c974e657f 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -8359,6 +8359,14 @@  is_constant_expression (tree t)
   return potential_constant_expression_1 (t, false, true, true, tf_none);
 }
 
+/* As above, but expect an rvalue.  */
+
+bool
+is_rvalue_constant_expression (tree t)
+{
+  return potential_constant_expression_1 (t, true, true, true, tf_none);
+}
+
 /* Like above, but complain about non-constant expressions.  */
 
 bool
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index a389579ee52..6d1c08e064a 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7612,7 +7612,7 @@  cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
 	  init = boolean_true_node;
 	}
       else if (init
-	       && init_const_expr_p
+	       && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl))
 	       && !TYPE_REF_P (type)
 	       && decl_maybe_constant_var_p (decl)
 	       && !(dep_init = value_dependent_init_p (init)))
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index a8082d39aca..6a0ef4d76ee 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -10184,10 +10184,10 @@  cp_parser_constant_expression (cp_parser* parser,
       if (TREE_TYPE (expression)
 	  && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
 	decay = build_address (expression);
-      bool is_const = potential_rvalue_constant_expression (decay);
+      bool is_const = is_rvalue_constant_expression (decay);
       parser->non_integral_constant_expression_p = !is_const;
       if (!is_const && !allow_non_constant_p)
-	require_potential_rvalue_constant_expression (decay);
+	require_rvalue_constant_expression (decay);
     }
   if (allow_non_constant_p)
     *non_constant_p = parser->non_integral_constant_expression_p;