This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Fix up __builtin_is_constant_evaluated handling in array type sizes (PR c++/88446, take 2)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>, Marek Polacek <polacek at redhat dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Tue, 11 Dec 2018 22:38:16 +0100
- Subject: [C++ PATCH] Fix up __builtin_is_constant_evaluated handling in array type sizes (PR c++/88446, take 2)
- References: <20181211163925.GY12380@tucnak> <20181211203539.GW21364@redhat.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Tue, Dec 11, 2018 at 03:35:39PM -0500, Marek Polacek wrote:
> > tree
> > -maybe_constant_value (tree t, tree decl)
> > +maybe_constant_value (tree t, tree decl, bool pretend_const_required)
> > {
> > tree r;
> >
>
> Could you please describe the new param in the comment?
>
> Perhaps use /*pretend_const_required=*/true?
Thanks, changed both, plus I've changed the other function's descriptions
of that argument from the P0595R1 term (required to be const-evaluated)
to the P0595R2 term (manifestly const-evaluated).
Ok for trunk? Or should I also change pretend_const_required argument
name to manifestly_const_eval or something similar?
2018-12-11 Jakub Jelinek <jakub@redhat.com>
PR c++/88446
* cp-tree.h (maybe_constant_value): Add pretend_const_required
argument.
* constexpr.c (cxx_eval_builtin_function_call): Formatting fix.
(cxx_eval_outermost_constant_expr, maybe_constant_init_1): Adjust
pretend_const_required argument description in function comments.
(maybe_constant_value): Likewise. If true, don't cache and call
cxx_eval_outermost_constant_expr with true as pretend_const_required.
* decl.c (compute_array_index_type_loc): Call maybe_constant_value
with true as pretend_const_required.
* g++.dg/cpp2a/is-constant-evaluated3.C: New test.
--- gcc/cp/cp-tree.h.jj 2018-12-11 18:15:27.495406106 +0100
+++ gcc/cp/cp-tree.h 2018-12-11 22:25:29.994213808 +0100
@@ -7663,7 +7663,7 @@ extern bool require_rvalue_constant_expr
extern bool require_potential_rvalue_constant_expression (tree);
extern tree cxx_constant_value (tree, tree = NULL_TREE);
extern tree cxx_constant_init (tree, tree = NULL_TREE);
-extern tree maybe_constant_value (tree, tree = NULL_TREE);
+extern tree maybe_constant_value (tree, tree = NULL_TREE, bool = false);
extern tree maybe_constant_init (tree, tree = NULL_TREE, bool = false);
extern tree fold_non_dependent_expr (tree, tsubst_flags_t = tf_warning_or_error);
extern tree fold_simple (tree);
--- gcc/cp/constexpr.c.jj 2018-12-11 21:35:05.450412282 +0100
+++ gcc/cp/constexpr.c 2018-12-11 22:30:57.785880411 +0100
@@ -1204,7 +1204,7 @@ cxx_eval_builtin_function_call (const co
/* For __builtin_is_constant_evaluated, defer it if not
ctx->pretend_const_required, otherwise fold it to true. */
if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
- BUILT_IN_FRONTEND))
+ BUILT_IN_FRONTEND))
{
if (!ctx->pretend_const_required)
{
@@ -5014,7 +5014,7 @@ instantiate_constexpr_fns (tree t)
STRICT has the same sense as for constant_value_1: true if we only allow
conforming C++ constant expressions, or false if we want a constant value
even if it doesn't conform.
- PRETEND_CONST_REQUIRED is true if T is required to be const-evaluated as
+ PRETEND_CONST_REQUIRED is true if T is manifestly const-evaluated as
per P0595 even when ALLOW_NON_CONSTANT is true. */
static tree
@@ -5266,12 +5266,14 @@ fold_simple (tree t)
/* If T is a constant expression, returns its reduced value.
Otherwise, if T does not have TREE_CONSTANT set, returns T.
- Otherwise, returns a version of T without TREE_CONSTANT. */
+ Otherwise, returns a version of T without TREE_CONSTANT.
+ PRETEND_CONST_REQUIRED is true if T is manifestly const-evaluated
+ as per P0595. */
static GTY((deletable)) hash_map<tree, tree> *cv_cache;
tree
-maybe_constant_value (tree t, tree decl)
+maybe_constant_value (tree t, tree decl, bool pretend_const_required)
{
tree r;
@@ -5288,6 +5290,9 @@ maybe_constant_value (tree t, tree decl)
/* No caching or evaluation needed. */
return t;
+ if (pretend_const_required)
+ return cxx_eval_outermost_constant_expr (t, true, true, true, decl);
+
if (cv_cache == NULL)
cv_cache = hash_map<tree, tree>::create_ggc (101);
if (tree *cached = cv_cache->get (t))
@@ -5391,7 +5396,7 @@ fold_non_dependent_expr (tree t,
/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
than wrapped in a TARGET_EXPR.
ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
- PRETEND_CONST_REQUIRED is true if T is required to be const-evaluated as
+ PRETEND_CONST_REQUIRED is true if T is manifestly const-evaluated as
per P0595 even when ALLOW_NON_CONSTANT is true. */
static tree
--- gcc/cp/decl.c.jj 2018-12-11 21:27:27.735860813 +0100
+++ gcc/cp/decl.c 2018-12-11 22:31:35.953259399 +0100
@@ -9646,7 +9646,11 @@ compute_array_index_type_loc (location_t
{
size = instantiate_non_dependent_expr_sfinae (size, complain);
size = build_converted_constant_expr (size_type_node, size, complain);
- size = maybe_constant_value (size);
+ /* Pedantically a constant expression is required here and so
+ __builtin_is_constant_evaluated () should fold to true if it
+ is successfully folded into a constant. */
+ size = maybe_constant_value (size, NULL_TREE,
+ /*pretend_const_required=*/true);
if (!TREE_CONSTANT (size))
size = osize;
--- gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated3.C.jj 2018-12-11 22:25:30.000213710 +0100
+++ gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated3.C 2018-12-11 22:25:30.000213710 +0100
@@ -0,0 +1,26 @@
+// P0595R1
+// { dg-do run { target c++14 } }
+
+struct false_type { static constexpr bool value = false; };
+struct true_type { static constexpr bool value = true; };
+template<class T, class U>
+struct is_same : false_type {};
+template<class T>
+struct is_same<T, T> : true_type {};
+
+int a[__builtin_is_constant_evaluated () ? 1 : 2];
+int b[1];
+static_assert (is_same<decltype (a), decltype (b)>::value, "");
+
+int
+main ()
+{
+ int c[__builtin_is_constant_evaluated () ? 3 : 4];
+ int d[3];
+ static_assert (is_same<decltype (c), decltype (d)>::value, "");
+ int (*e)[7][9] = new int[__builtin_is_constant_evaluated () ? -1 : 5]
+ [__builtin_is_constant_evaluated () ? 7 : 8]
+ [__builtin_is_constant_evaluated () ? 9 : 10];
+ e[0][0][0] = 6;
+ delete[] e;
+}
Jakub