static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
- bool, bool, bool *, bool *);
+ bool, bool, bool *, bool *, tree *);
/* Compute a hash value for a constexpr call representation. */
{
args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
if (allow_non_constant && *non_constant_p)
return t;
}
}
arg = cxx_eval_constant_expression (ctx, x, allow_non_constant,
TREE_CODE (type) == REFERENCE_TYPE,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
/* Don't VERIFY_CONSTANT here. */
if (*non_constant_p && allow_non_constant)
return;
/* Might be a constexpr function pointer. */
fun = cxx_eval_constant_expression (ctx, fun, allow_non_constant,
/*addr*/false, non_constant_p,
- overflow_p);
+ overflow_p, NULL);
STRIP_NOPS (fun);
if (TREE_CODE (fun) == ADDR_EXPR)
fun = TREE_OPERAND (fun, 0);
{
tree arg = convert_from_reference (get_nth_callarg (t, 1));
return cxx_eval_constant_expression (ctx, arg, allow_non_constant,
- addr, non_constant_p, overflow_p);
+ addr, non_constant_p,
+ overflow_p, NULL);
}
else if (TREE_CODE (t) == AGGR_INIT_EXPR
&& AGGR_INIT_ZERO_FIRST (t))
result = (cxx_eval_constant_expression
(&new_ctx, new_call.fundef->body,
allow_non_constant, addr,
- non_constant_p, overflow_p));
+ non_constant_p, overflow_p, NULL));
}
else
{
else
ctx->values->put (res, NULL_TREE);
+ tree jump_target = NULL_TREE;
cxx_eval_constant_expression (ctx, body, allow_non_constant,
- addr, non_constant_p, overflow_p);
+ addr, non_constant_p, overflow_p,
+ &jump_target);
if (VOID_TYPE_P (TREE_TYPE (res)))
/* This can be null for a subobject constructor call, in
tree r;
tree orig_arg = TREE_OPERAND (t, 0);
tree arg = cxx_eval_constant_expression (ctx, orig_arg, allow_non_constant,
- addr, non_constant_p, overflow_p);
+ addr, non_constant_p, overflow_p,
+ NULL);
VERIFY_CONSTANT (arg);
if (arg == orig_arg)
return t;
tree lhs, rhs;
lhs = cxx_eval_constant_expression (ctx, orig_lhs,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
VERIFY_CONSTANT (lhs);
rhs = cxx_eval_constant_expression (ctx, orig_rhs,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
VERIFY_CONSTANT (rhs);
if (lhs == orig_lhs && rhs == orig_rhs)
return t;
static tree
cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
bool allow_non_constant, bool addr,
- bool *non_constant_p, bool *overflow_p)
+ bool *non_constant_p, bool *overflow_p,
+ tree *jump_target)
{
tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
VERIFY_CONSTANT (val);
/* Don't VERIFY_CONSTANT the other operands. */
if (integer_zerop (val))
return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ jump_target);
return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ jump_target);
}
/* Subroutine of cxx_eval_constant_expression.
tree oldary = TREE_OPERAND (t, 0);
tree ary = cxx_eval_constant_expression (ctx, oldary,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
tree index, oldidx;
HOST_WIDE_INT i;
tree elem_type;
oldidx = TREE_OPERAND (t, 1);
index = cxx_eval_constant_expression (ctx, oldidx,
allow_non_constant, false,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
VERIFY_CONSTANT (index);
if (addr && ary == oldary && index == oldidx)
return t;
tree val = build_value_init (elem_type, tf_warning_or_error);
return cxx_eval_constant_expression (ctx, val,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
}
if (!allow_non_constant)
tree orig_whole = TREE_OPERAND (t, 0);
tree whole = cxx_eval_constant_expression (ctx, orig_whole,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
if (whole == orig_whole)
return t;
if (addr)
value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
return cxx_eval_constant_expression (ctx, value,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
}
/* Subroutine of cxx_eval_constant_expression.
HOST_WIDE_INT istart, isize;
tree whole = cxx_eval_constant_expression (ctx, orig_whole,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
tree start, field, value;
unsigned HOST_WIDE_INT i;
tree r;
tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
VERIFY_CONSTANT (lhs);
if (tree_int_cst_equal (lhs, bailout_value))
return lhs;
gcc_assert (tree_int_cst_equal (lhs, continue_value));
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
- allow_non_constant, addr, non_constant_p, overflow_p);
+ allow_non_constant, addr, non_constant_p,
+ overflow_p, NULL);
VERIFY_CONSTANT (r);
return r;
}
CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
tree elt = cxx_eval_constant_expression (&new_ctx, value,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
/* Don't VERIFY_CONSTANT here. */
if (allow_non_constant && *non_constant_p)
break;
we just pre-built above. */
eltinit = (cxx_eval_constant_expression
(&new_ctx, init, allow_non_constant,
- addr, non_constant_p, overflow_p));
+ addr, non_constant_p, overflow_p, NULL));
}
else
{
eltinit = force_rvalue (eltinit, tf_warning_or_error);
eltinit = (cxx_eval_constant_expression
(&new_ctx, eltinit, allow_non_constant, addr,
- non_constant_p, overflow_p));
+ non_constant_p, overflow_p, NULL));
}
if (*non_constant_p && !allow_non_constant)
break;
{
tree orig_op0 = TREE_OPERAND (t, 0);
tree op0 = cxx_eval_constant_expression (ctx, orig_op0, allow_non_constant,
- /*addr*/false, non_constant_p, overflow_p);
+ /*addr*/false, non_constant_p,
+ overflow_p, NULL);
bool empty_base = false;
tree r;
if (r)
r = cxx_eval_constant_expression (ctx, r, allow_non_constant,
- addr, non_constant_p, overflow_p);
+ addr, non_constant_p, overflow_p, NULL);
else
{
tree sub = op0;
{
args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
VERIFY_CONSTANT (args[i]);
}
tree target = TREE_OPERAND (t, 0);
target = cxx_eval_constant_expression (ctx, target,
allow_non_constant, true,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
if (*non_constant_p)
return t;
tree init = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 1),
allow_non_constant, false,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
if (target == object)
/* The hash table might have moved since the get earlier. */
ctx->values->put (object, init);
/* The operand as an lvalue. */
op = cxx_eval_constant_expression (ctx, op, allow_non_constant, true,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
/* The operand as an rvalue. */
tree val = rvalue (op);
val = cxx_eval_constant_expression (ctx, val, allow_non_constant, false,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
VERIFY_CONSTANT (val);
/* The modified value. */
/* Storing the modified value. */
tree store = build2 (MODIFY_EXPR, type, op, mod);
cxx_eval_constant_expression (ctx, store, allow_non_constant,
- true, non_constant_p, overflow_p);
+ true, non_constant_p, overflow_p, NULL);
/* And the value of the expression. */
if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
return val;
}
+/* Predicates for the meaning of *jump_target. */
+
+static bool
+returns (tree *jump_target)
+{
+ return *jump_target
+ && TREE_CODE (*jump_target) == RETURN_EXPR;
+}
+
+static bool
+breaks (tree *jump_target)
+{
+ return *jump_target
+ && TREE_CODE (*jump_target) == LABEL_DECL
+ && LABEL_DECL_BREAK (*jump_target);
+}
+
+static bool
+continues (tree *jump_target)
+{
+ return *jump_target
+ && TREE_CODE (*jump_target) == LABEL_DECL
+ && LABEL_DECL_CONTINUE (*jump_target);
+}
+
+static bool
+switches (tree *jump_target)
+{
+ return *jump_target
+ && TREE_CODE (*jump_target) == INTEGER_CST;
+}
+
+/* Subroutine of cxx_eval_statement_list. Determine whether the statement
+ at I matches *jump_target. If we're looking for a case label and we see
+ the default label, copy I into DEFAULT_LABEL. */
+
+static bool
+label_matches (tree *jump_target, tree_stmt_iterator i,
+ tree_stmt_iterator& default_label)
+{
+ tree stmt = tsi_stmt (i);
+ switch (TREE_CODE (*jump_target))
+ {
+ case LABEL_DECL:
+ if (TREE_CODE (stmt) == LABEL_EXPR
+ && LABEL_EXPR_LABEL (stmt) == *jump_target)
+ return true;
+ break;
+
+ case INTEGER_CST:
+ if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
+ {
+ if (!CASE_LOW (stmt))
+ default_label = i;
+ else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
+ return true;
+ }
+ break;
+
+ default:
+ gcc_unreachable ();
+ }
+ return false;
+}
+
+/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
+ semantics, for switch, break, continue, and return. */
+
+static tree
+cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
+ bool allow_non_constant,
+ bool *non_constant_p, bool *overflow_p,
+ tree *jump_target)
+{
+ tree_stmt_iterator i;
+ tree_stmt_iterator default_label = tree_stmt_iterator();
+ for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
+ {
+ reenter:
+ tree stmt = tsi_stmt (i);
+ if (*jump_target)
+ {
+ if (TREE_CODE (stmt) == STATEMENT_LIST)
+ /* The label we want might be inside. */;
+ else if (label_matches (jump_target, i, default_label))
+ /* Found it. */
+ *jump_target = NULL_TREE;
+ else
+ continue;
+ }
+ cxx_eval_constant_expression (ctx, stmt,
+ allow_non_constant, false,
+ non_constant_p, overflow_p,
+ jump_target);
+ if (*non_constant_p)
+ break;
+ if (returns (jump_target) || breaks (jump_target))
+ break;
+ }
+ if (switches (jump_target) && !tsi_end_p (default_label))
+ {
+ i = default_label;
+ *jump_target = NULL_TREE;
+ goto reenter;
+ }
+ return NULL_TREE;
+}
+
+/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
+ semantics; continue semantics are covered by cxx_eval_statement_list. */
+
+static tree
+cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
+ bool allow_non_constant,
+ bool *non_constant_p, bool *overflow_p,
+ tree *jump_target)
+{
+ tree body = TREE_OPERAND (t, 0);
+ while (true)
+ {
+ cxx_eval_statement_list (ctx, body, allow_non_constant,
+ non_constant_p, overflow_p, jump_target);
+ if (returns (jump_target) || breaks (jump_target))
+ break;
+ }
+ if (breaks (jump_target))
+ *jump_target = NULL_TREE;
+ return NULL_TREE;
+}
+
+/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
+ semantics. */
+
+static tree
+cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
+ bool allow_non_constant,
+ bool *non_constant_p, bool *overflow_p,
+ tree *jump_target)
+{
+ tree cond = TREE_OPERAND (t, 0);
+ cond = cxx_eval_constant_expression (ctx, cond, allow_non_constant, false,
+ non_constant_p, overflow_p, NULL);
+ VERIFY_CONSTANT (cond);
+ *jump_target = cond;
+
+ tree body = TREE_OPERAND (t, 1);
+ cxx_eval_statement_list (ctx, body, allow_non_constant,
+ non_constant_p, overflow_p, jump_target);
+ if (breaks (jump_target) || switches (jump_target))
+ *jump_target = NULL_TREE;
+ return NULL_TREE;
+}
+
/* 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 */
static tree
cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
bool allow_non_constant, bool addr,
- bool *non_constant_p, bool *overflow_p)
+ bool *non_constant_p, bool *overflow_p,
+ tree *jump_target)
{
constexpr_ctx new_ctx;
tree r = t;
case FUNCTION_DECL:
case TEMPLATE_DECL:
case LABEL_DECL:
+ case LABEL_EXPR:
+ case CASE_LABEL_EXPR:
return t;
case PARM_DECL:
{
init = cxx_eval_constant_expression (ctx, init,
allow_non_constant, false,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
ctx->values->put (r, init);
}
else if (ctx == &new_ctx)
initialization of a temporary. */
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
allow_non_constant, false,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
if (!*non_constant_p)
/* Adjust the type of the result to the type of the temporary. */
r = adjust_temp_type (TREE_TYPE (t), r);
not the side-effect of the initialization. */
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
allow_non_constant, false,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
break;
}
/* else fall through */
case SCOPE_REF:
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p, NULL);
break;
case RETURN_EXPR:
+ r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
+ allow_non_constant, addr,
+ non_constant_p, overflow_p, NULL);
+ *jump_target = t;
+ break;
+
case NON_LVALUE_EXPR:
case TRY_CATCH_EXPR:
case CLEANUP_POINT_EXPR:
case EH_SPEC_BLOCK:
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ jump_target);
break;
/* These differ from cxx_eval_unary_expression in that this doesn't
tree op = cxx_eval_constant_expression (ctx, oldop,
allow_non_constant,
/*addr*/true,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
/* Don't VERIFY_CONSTANT here. */
if (*non_constant_p)
return t;
if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
|| TREE_CODE (op1) == EMPTY_CLASS_EXPR)
r = cxx_eval_constant_expression (ctx, op0, allow_non_constant,
- addr, non_constant_p, overflow_p);
+ addr, non_constant_p, overflow_p,
+ jump_target);
else
{
/* Check that the LHS is constant and then discard it. */
cxx_eval_constant_expression (ctx, op0, allow_non_constant,
- false, non_constant_p, overflow_p);
+ false, non_constant_p, overflow_p,
+ jump_target);
op1 = TREE_OPERAND (t, 1);
r = cxx_eval_constant_expression (ctx, op1, allow_non_constant,
- addr, non_constant_p, overflow_p);
+ addr, non_constant_p, overflow_p,
+ jump_target);
}
}
break;
case COND_EXPR:
case VEC_COND_EXPR:
r = cxx_eval_conditional_expression (ctx, t, allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ jump_target);
break;
case CONSTRUCTOR:
tree oldop = TREE_OPERAND (t, 0);
tree op = cxx_eval_constant_expression (ctx, oldop,
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ NULL);
if (*non_constant_p)
return t;
if (POINTER_TYPE_P (TREE_TYPE (t))
return t;
case STATEMENT_LIST:
- {
- new_ctx = *ctx;
- new_ctx.ctor = new_ctx.object = NULL_TREE;
- tree_stmt_iterator i;
- for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
- {
- cxx_eval_constant_expression (&new_ctx, tsi_stmt (i),
- allow_non_constant, false,
- non_constant_p, overflow_p);
- if (*non_constant_p)
- break;
- }
- }
- break;
+ new_ctx = *ctx;
+ new_ctx.ctor = new_ctx.object = NULL_TREE;
+ return cxx_eval_statement_list (&new_ctx, t, allow_non_constant,
+ non_constant_p, overflow_p, jump_target);
case BIND_EXPR:
return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
allow_non_constant, addr,
- non_constant_p, overflow_p);
+ non_constant_p, overflow_p,
+ jump_target);
case PREINCREMENT_EXPR:
case POSTINCREMENT_EXPR:
gcc_assert (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (t), TREE_TYPE (ctor)));
return cxx_eval_constant_expression
- (ctx, ctor, allow_non_constant, addr, non_constant_p, overflow_p);
+ (ctx, ctor, allow_non_constant, addr,
+ non_constant_p, overflow_p, NULL);
}
break;
case GOTO_EXPR:
+ *jump_target = TREE_OPERAND (t, 0);
+ gcc_assert (breaks (jump_target) || continues (jump_target));
+ break;
+
case LOOP_EXPR:
+ cxx_eval_loop_expr (ctx, t, allow_non_constant,
+ non_constant_p, overflow_p, jump_target);
+ break;
+
case SWITCH_EXPR:
- if (!allow_non_constant)
- sorry ("%qs in constant expression", get_tree_code_name (TREE_CODE (t)));
- *non_constant_p = true;
+ cxx_eval_switch_expr (ctx, t, allow_non_constant,
+ non_constant_p, overflow_p, jump_target);
break;
default:
}
r = cxx_eval_constant_expression (&ctx, r, allow_non_constant,
- false, &non_constant_p, &overflow_p);
+ false, &non_constant_p, &overflow_p, NULL);
verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
hash_map <tree, tree> map;
ctx.values = ↦
cxx_eval_constant_expression (&ctx, t, true, false, &non_constant_p,
- &overflow_p);
+ &overflow_p, NULL);
return !non_constant_p && !overflow_p;
}