fn = build_addr_func (fn, complain);
if (fn == error_mark_node)
return error_mark_node;
+
+ /* We're actually invoking the function. (Immediate functions get an
+ & when invoking it even though the user didn't use &.) */
+ ADDR_EXPR_DENOTES_CALL_P (fn) = true;
}
tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
if (TREE_CODE (c) == CALL_EXPR)
suppress_warning (c /* Suppress all warnings. */);
}
- if (TREE_CODE (fn) == ADDR_EXPR)
- {
- tree fndecl = STRIP_TEMPLATE (TREE_OPERAND (fn, 0));
- if (immediate_invocation_p (fndecl))
- {
- tree obj_arg = NULL_TREE;
- /* Undo convert_from_reference called by build_cxx_call. */
- if (REFERENCE_REF_P (call))
- call = TREE_OPERAND (call, 0);
- if (DECL_CONSTRUCTOR_P (fndecl))
- obj_arg = cand->first_arg ? cand->first_arg : (*args)[0];
- if (obj_arg && is_dummy_object (obj_arg))
- {
- call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain);
- obj_arg = NULL_TREE;
- }
- /* Look through *(const T *)&obj. */
- else if (obj_arg && INDIRECT_REF_P (obj_arg))
- {
- tree addr = TREE_OPERAND (obj_arg, 0);
- STRIP_NOPS (addr);
- if (TREE_CODE (addr) == ADDR_EXPR)
- {
- tree typeo = TREE_TYPE (obj_arg);
- tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
- if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
- obj_arg = TREE_OPERAND (addr, 0);
- }
- }
- call = cxx_constant_value (call, obj_arg, complain);
- if (obj_arg && !error_operand_p (call))
- call = cp_build_init_expr (obj_arg, call);
- call = convert_from_reference (call);
- }
- }
+
return call;
}
unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
+ /* Make sure we fold std::is_constant_evaluated to true in an
+ immediate function. */
+ if (DECL_IMMEDIATE_FUNCTION_P (fun))
+ call_ctx.manifestly_const_eval = mce_true;
+
/* If this is a constexpr destructor, the object's const and volatile
semantics are no longer in effect; see [class.dtor]p5. */
if (new_obj && DECL_DESTRUCTOR_P (fun))
}
/* Subroutine of cxx_eval_constant_expression.
- Attempt to evaluate condition expressions. Dead branches are not
- looked into. */
+ Attempt to evaluate condition expressions. */
static tree
cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
boolean_type_node);
}
/* Don't VERIFY_CONSTANT the other operands. */
- if (integer_zerop (val))
+ const bool zero_p = integer_zerop (val);
+ if (zero_p)
val = TREE_OPERAND (t, 2);
else
val = TREE_OPERAND (t, 1);
if (TREE_CODE (t) == IF_STMT && !val)
val = void_node;
+
+ /* P2564: a subexpression of a manifestly constant-evaluated expression
+ or conversion is an immediate function context. */
+ if (ctx->manifestly_const_eval != mce_true
+ && !in_immediate_context ()
+ && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
+ ctx->manifestly_const_eval))
+ {
+ *non_constant_p = true;
+ return t;
+ }
+
/* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
serve as the initializer for the same object as the outer TARGET_EXPR,
as in
definitely not in a manifestly constant-evaluated
context. */
ff_mce_false = 1 << 1,
+ /* Whether we're being called from cp_fold_immediate. */
+ ff_fold_immediate = 1 << 2,
};
using fold_flags_t = int;
replacement when cp_folding TARGET_EXPR to preserve the invariant that
AGGR_INIT_EXPR_SLOT agrees with the enclosing TARGET_EXPR_SLOT. */
-bool
+static bool
maybe_replace_decl (tree *tp, tree decl, tree replacement)
{
if (!*tp || !VOID_TYPE_P (TREE_TYPE (*tp)))
bool handle_invisiref_parm_p;
};
-/* Perform any pre-gimplification folding of C++ front end trees to
- GENERIC.
- Note: The folding of non-omp cases is something to move into
- the middle-end. As for now we have most foldings only on GENERIC
- in fold-const, we need to perform this before transformation to
- GIMPLE-form. */
+/* A subroutine of cp_fold_r to handle immediate functions. */
static tree
-cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
+cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
{
- cp_fold_data *data = (cp_fold_data*)data_;
+ auto data = static_cast<cp_fold_data *>(data_);
tree stmt = *stmt_p;
- enum tree_code code = TREE_CODE (stmt);
+ /* The purpose of this is not to emit errors for mce_unknown. */
+ const tsubst_flags_t complain = (data->flags & ff_mce_false
+ ? tf_error : tf_none);
- switch (code)
+ /* No need to look into types or unevaluated operands.
+ NB: This affects cp_fold_r as well. */
+ if (TYPE_P (stmt) || unevaluated_p (TREE_CODE (stmt)))
+ {
+ *walk_subtrees = 0;
+ return NULL_TREE;
+ }
+
+ switch (TREE_CODE (stmt))
{
+ /* Unfortunately we must handle code like
+ false ? bar () : 42
+ where we have to check bar too. The cp_fold call in cp_fold_r could
+ fold the ?: into a constant before we see it here. */
+ case COND_EXPR:
+ /* If we are called from cp_fold_immediate, we don't need to worry about
+ cp_fold folding away the COND_EXPR. */
+ if (data->flags & ff_fold_immediate)
+ break;
+ if (TREE_OPERAND (stmt, 1)
+ && cp_walk_tree (&TREE_OPERAND (stmt, 1), cp_fold_immediate_r, data,
+ nullptr))
+ return error_mark_node;
+ if (TREE_OPERAND (stmt, 2)
+ && cp_walk_tree (&TREE_OPERAND (stmt, 2), cp_fold_immediate_r, data,
+ nullptr))
+ return error_mark_node;
+ /* We're done here. Don't clear *walk_subtrees here though: we're called
+ from cp_fold_r and we must let it recurse on the expression with
+ cp_fold. */
+ break;
case PTRMEM_CST:
if (TREE_CODE (PTRMEM_CST_MEMBER (stmt)) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (stmt)))
{
- if (!data->pset.add (stmt))
- error_at (PTRMEM_CST_LOCATION (stmt),
- "taking address of an immediate function %qD",
- PTRMEM_CST_MEMBER (stmt));
- stmt = *stmt_p = build_zero_cst (TREE_TYPE (stmt));
- break;
+ if (!data->pset.add (stmt) && (complain & tf_error))
+ {
+ error_at (PTRMEM_CST_LOCATION (stmt),
+ "taking address of an immediate function %qD",
+ PTRMEM_CST_MEMBER (stmt));
+ *stmt_p = build_zero_cst (TREE_TYPE (stmt));
+ }
+ return error_mark_node;
}
break;
+ /* Expand immediate invocations. */
+ case CALL_EXPR:
+ case AGGR_INIT_EXPR:
+ if (tree fn = cp_get_callee (stmt))
+ if (TREE_CODE (fn) != ADDR_EXPR || ADDR_EXPR_DENOTES_CALL_P (fn))
+ if (tree fndecl = cp_get_fndecl_from_callee (fn, /*fold*/false))
+ if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
+ {
+ stmt = cxx_constant_value (stmt, complain);
+ if (stmt == error_mark_node)
+ {
+ if (complain & tf_error)
+ *stmt_p = error_mark_node;
+ return error_mark_node;
+ }
+ *stmt_p = stmt;
+ }
+ break;
+
case ADDR_EXPR:
if (TREE_CODE (TREE_OPERAND (stmt, 0)) == FUNCTION_DECL
- && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (stmt, 0)))
+ && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (stmt, 0))
+ && !ADDR_EXPR_DENOTES_CALL_P (stmt))
{
- error_at (EXPR_LOCATION (stmt),
- "taking address of an immediate function %qD",
- TREE_OPERAND (stmt, 0));
- stmt = *stmt_p = build_zero_cst (TREE_TYPE (stmt));
- break;
+ if (complain & tf_error)
+ {
+ error_at (EXPR_LOCATION (stmt),
+ "taking address of an immediate function %qD",
+ TREE_OPERAND (stmt, 0));
+ *stmt_p = build_zero_cst (TREE_TYPE (stmt));
+ }
+ return error_mark_node;
}
break;
break;
}
+ return NULL_TREE;
+}
+
+/* A wrapper around cp_fold_immediate_r. Return true if we found
+ a non-constant immediate function, or taking the address of an
+ immediate function. */
+
+bool
+cp_fold_immediate (tree *tp, mce_value manifestly_const_eval)
+{
+ if (cxx_dialect <= cxx17)
+ return false;
+
+ fold_flags_t flags = ff_fold_immediate;
+ if (manifestly_const_eval == mce_false)
+ flags |= ff_mce_false;
+
+ cp_fold_data data (flags);
+ return !!cp_walk_tree_without_duplicates (tp, cp_fold_immediate_r, &data);
+}
+
+/* Perform any pre-gimplification folding of C++ front end trees to
+ GENERIC.
+ Note: The folding of non-omp cases is something to move into
+ the middle-end. As for now we have most foldings only on GENERIC
+ in fold-const, we need to perform this before transformation to
+ GIMPLE-form. */
+
+static tree
+cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
+{
+ cp_fold_data *data = (cp_fold_data*)data_;
+ tree stmt = *stmt_p;
+ enum tree_code code = TREE_CODE (stmt);
+
+ if (cxx_dialect > cxx17)
+ cp_fold_immediate_r (stmt_p, walk_subtrees, data);
+
*stmt_p = stmt = cp_fold (*stmt_p, data->flags);
if (data->pset.add (stmt))
always the same tree, which the first time cp_fold_r has been
called on it had the subtrees walked. */
*walk_subtrees = 0;
- return NULL;
+ return NULL_TREE;
}
code = TREE_CODE (stmt);
}
cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL);
*walk_subtrees = 0;
- return NULL;
+ return NULL_TREE;
case IF_STMT:
if (IF_STMT_CONSTEVAL_P (stmt))
cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL);
cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL);
*walk_subtrees = 0;
- return NULL;
+ return NULL_TREE;
}
break;
break;
}
- return NULL;
+ return NULL_TREE;
}
/* Fold ALL the trees! FIXME we should be able to remove this, but
#define PTRMEM_OK_P(NODE) \
TREE_LANG_FLAG_0 (TREE_CHECK3 ((NODE), ADDR_EXPR, OFFSET_REF, SCOPE_REF))
+/* True if this ADDR_EXPR denotes a function call; that is, it's
+ fn() rather than &fn. */
+#define ADDR_EXPR_DENOTES_CALL_P(NODE) \
+ (ADDR_EXPR_CHECK(NODE)->base.protected_flag)
+
/* Get the POINTER_TYPE to the METHOD_TYPE associated with this
pointer to member function. TYPE_PTRMEMFUNC_P _must_ be true,
before using this macro. */
extern int module_dump_id;
extern int raw_dump_id;
+/* Whether the current context is manifestly constant-evaluated.
+ Used by the constexpr machinery to control folding of
+ __builtin_is_constant_evaluated. */
+
+enum class mce_value
+{
+ /* Unknown, so treat __builtin_is_constant_evaluated as non-constant. */
+ mce_unknown = 0,
+ /* Fold it to true. */
+ mce_true = 1,
+ /* Fold it to false. Primarily used during cp_fold_function and
+ cp_fully_fold_init. */
+ mce_false = -1,
+};
+constexpr mce_value mce_unknown = mce_value::mce_unknown;
+constexpr mce_value mce_true = mce_value::mce_true;
+constexpr mce_value mce_false = mce_value::mce_false;
+
/* in call.cc */
extern bool check_dtor_name (tree, tree);
int magic_varargs_p (tree);
extern bool simple_empty_class_p (tree, tree, tree_code);
extern tree fold_builtin_source_location (const_tree);
extern tree get_source_location_impl_type ();
+extern bool cp_fold_immediate (tree *, mce_value);
/* in name-lookup.cc */
extern tree strip_using_decl (tree);
tree result;
};
-/* Whether the current context is manifestly constant-evaluated.
- Used by the constexpr machinery to control folding of
- __builtin_is_constant_evaluated. */
-
-enum class mce_value
-{
- /* Unknown, so treat __builtin_is_constant_evaluated as non-constant. */
- mce_unknown = 0,
- /* Fold it to true. */
- mce_true = 1,
- /* Fold it to false. Primarily used during cp_fold_function and
- cp_fully_fold_init. */
- mce_false = -1,
-};
-constexpr mce_value mce_unknown = mce_value::mce_unknown;
-constexpr mce_value mce_true = mce_value::mce_true;
-constexpr mce_value mce_false = mce_value::mce_false;
-
extern void fini_constexpr (void);
extern bool literal_type_p (tree);
extern void maybe_save_constexpr_fundef (tree);
variables. */
static tree
-bot_replace (tree* t, int* walk_subtrees, void* data_)
+bot_replace (tree* t, int */*walk_subtrees*/, void* data_)
{
bot_data &data = *(bot_data*)data_;
splay_tree target_remap = data.target_remap;
/*check_access=*/false, /*nonnull=*/true,
tf_warning_or_error);
}
- else if (cxx_dialect >= cxx20
- && (TREE_CODE (*t) == CALL_EXPR
- || TREE_CODE (*t) == AGGR_INIT_EXPR)
- && !in_immediate_context ())
- {
- /* Expand immediate invocations. */
- if (tree fndecl = cp_get_callee_fndecl_nofold (*t))
- if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
- {
- /* Make in_immediate_context true within the args. */
- in_consteval_if_p_temp_override ito;
- in_consteval_if_p = true;
- int nargs = call_expr_nargs (*t);
- for (int i = 0; i < nargs; ++i)
- cp_walk_tree (&get_nth_callarg (*t, i), bot_replace, data_, NULL);
- *t = cxx_constant_value (*t);
- if (*t == error_mark_node)
- return error_mark_node;
- *walk_subtrees = 0;
- }
- }
return NULL_TREE;
}
return r;
}
+// This function is not instantiated so NDR.
template <typename T>
constexpr int
qux (int x)
int r = 0;
if not consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } }
{
- r += foo (x); // { dg-error "'x' is not a constant expression" }
+ r += foo (x); // { dg-error "'x' is not a constant expression" "" { xfail *-*-* } }
}
else
{
fixed_string::size_static(-1); // { dg-message "expansion of" }
s(); // { dg-bogus "" }
}
+
+void
+do_test ()
+{
+ fixed_string f;
+ VerifyHash<int>(f);
+}
constexpr int a = bar (1);
constexpr int b = bar (2); // { dg-message "in 'constexpr' expansion of" }
-constexpr int c = 0 ? bar (3) : 1; // { dg-message "in 'constexpr' expansion of" }
+constexpr int c = 0 ? bar (3) : 1;
const int d = bar (4); // { dg-message "in 'constexpr' expansion of" }
-const int e = 0 ? bar (5) : 1; // { dg-message "in 'constexpr' expansion of" }
+const int e = 0 ? bar (5) : 1;
int f = bar (1);
int g = bar (6); // { dg-message "in 'constexpr' expansion of" }
-int h = 0 ? bar (7) : 1; // { dg-message "in 'constexpr' expansion of" }
+int h = 0 ? bar (7) : 1;
void
foo ()
{
constexpr int a = bar (1);
constexpr int b = bar (2); // { dg-message "in 'constexpr' expansion of" }
- constexpr int c = 0 ? bar (3) : 1; // { dg-message "in 'constexpr' expansion of" }
+ constexpr int c = 0 ? bar (3) : 1;
const int d = bar (4); // { dg-message "in 'constexpr' expansion of" }
- const int e = 0 ? bar (5) : 1; // { dg-message "in 'constexpr' expansion of" }
+ const int e = 0 ? bar (5) : 1;
int f = bar (1);
int g = bar (6); // { dg-message "in 'constexpr' expansion of" }
int h = 0 ? bar (7) : 1; // { dg-message "in 'constexpr' expansion of" }
else
bar (12); // { dg-message "in 'constexpr' expansion of" }
if constexpr (0)
- bar (13); // { dg-message "in 'constexpr' expansion of" }
+ bar (13);
else
bar (14); // { dg-message "in 'constexpr' expansion of" }
if constexpr (1)
bar (15); // { dg-message "in 'constexpr' expansion of" }
else
- bar (16); // { dg-message "in 'constexpr' expansion of" }
+ bar (16);
}
consteval int
void
qux ()
{
+ // Used to give errors errors here, but not since we moved consteval
+ // function folding to cp_fold_r which isn't called on uninstantiated
+ // templates.
if (0)
- bar (2); // { dg-message "in 'constexpr' expansion of" }
+ bar (2);
else
- bar (3); // { dg-message "in 'constexpr' expansion of" }
+ bar (3);
if (1)
- bar (4); // { dg-message "in 'constexpr' expansion of" }
+ bar (4);
else
- bar (5); // { dg-message "in 'constexpr' expansion of" }
+ bar (5);
if constexpr (0)
- bar (6); // { dg-message "in 'constexpr' expansion of" }
+ bar (6);
else
- bar (7); // { dg-message "in 'constexpr' expansion of" }
+ bar (7);
if constexpr (1)
- bar (8); // { dg-message "in 'constexpr' expansion of" }
+ bar (8);
else
- bar (9); // { dg-message "in 'constexpr' expansion of" }
+ bar (9);
if (0)
bar ((T) 2);
else
int d = 6; // { dg-message "'int d' is not const" }
int e = f6 (d); // { dg-error "the value of 'd' is not usable in a constant expression" }
constexpr int f7 (int x) { return f6 (x); } // { dg-error "'x' is not a constant expression" }
-constexpr int f = f7 (5); // { dg-error "" }
- // { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 }
+constexpr int f = f7 (5);
using fnptr = int (int);
fnptr *g = f6; // { dg-error "taking address of an immediate function 'consteval int f6\\(int\\)'" }
int f8 (fnptr *);
--- /dev/null
+// { dg-do compile { target c++20 } }
+
+consteval int foo () { return 42; }
+int bar () { return (*(&foo)) (); } // { dg-error "taking address" }
--- /dev/null
+// { dg-do compile { target c++20 } }
+
+consteval int id (int i) { return i; }
+consteval int add (int i, int j) { return i + j; }
+
+constexpr int
+foo (int i = id (42))
+{
+ return i + id (id (id (0)));
+}
+
+constexpr int
+bar (int i = id (id (id (42))))
+{
+ return i;
+}
+
+constexpr int
+baz (int i = add (add (id (1), id (2)), id (3)))
+{
+ return i;
+}
+
+void
+g ()
+{
+ foo ();
+ bar ();
+ baz ();
+}
+
+static_assert (foo () == 42);
+static_assert (bar () == 42);
+static_assert (baz () == 6);
--- /dev/null
+// { dg-do compile { target c++20 } }
+
+consteval int bar (int i) { if (i != 1) throw 1; return 0; } // { dg-error "is not a constant expression" }
+
+constexpr int
+foo (bool b)
+{
+ return b ? bar (3) : 2; // { dg-message "in .constexpr. expansion" }
+}
+
+static_assert (foo (false) == 2);
+
+__extension__ constexpr int g1 = false ?: bar (2); // { dg-message "in .constexpr. expansion" }
+__extension__ constexpr int g2 = false ?: (1 + bar (2)); // { dg-message "in .constexpr. expansion" }
+__extension__ constexpr int g3 = true ?: bar (2);
+__extension__ constexpr int g4 = true ?: (1 + bar (2));
+constexpr int g5 = bar (2) ? 1 : 2; // { dg-message "in .constexpr. expansion" }
+constexpr int g6 = bar (2) - 1 ? 1 : 2; // { dg-message "in .constexpr. expansion" }
+
+void
+g ()
+{
+ __extension__ int a1[bar(3)]; // { dg-message "in .constexpr. expansion" }
+ int a2[sizeof (bar(3))];
+
+ int a3 = false ? (1 + bar (8)) : 1; // { dg-message "in .constexpr. expansion" }
+ a3 += false ? (1 + bar (8)) : 1; // { dg-message "in .constexpr. expansion" }
+
+ __extension__ int a4 = false ?: (1 + bar (8)); // { dg-message "in .constexpr. expansion" }
+ __extension__ int a5 = true ?: (1 + bar (8)); // { dg-message "in .constexpr. expansion" }
+ int a6 = bar (2) ? 1 : 2; // { dg-message "in .constexpr. expansion" }
+ int a7 = bar (2) - 1 ? 1 : 2; // { dg-message "in .constexpr. expansion" }
+}
--- /dev/null
+// { dg-do compile { target c++20 } }
+
+template <typename T, typename F>
+constexpr bool is_not(T t, F f) {
+ return not f(t);
+}
+
+consteval bool is_even(int i) { return i % 2 == 0; }
+
+static_assert(is_not(5, is_even)); // ok
int a = bar (N); // { dg-message "in 'constexpr' expansion of 'bar\\(2\\)'" }
}
+// This function is not instantiated so NDR.
template <int N>
void quux ()
{
- int a = bar (5); // { dg-message "in 'constexpr' expansion of 'bar\\(5\\)'" }
+ int a = bar (5);
}
void
a.deallocate(p, n);
return true;
}
-static_assert( test_pr105957() );
+static_assert( test_pr105957() ); // { dg-error "non-constant" }
// { dg-error "throw_bad_array_new_length" "" { target *-*-* } 0 }