}
}
+/* Complain about a const object OBJ being modified in a constant expression.
+ EXPR is the MODIFY_EXPR expression performing the modification. */
+
+static void
+modifying_const_object_error (tree expr, tree obj)
+{
+ location_t loc = cp_expr_loc_or_input_loc (expr);
+ auto_diagnostic_group d;
+ error_at (loc, "modifying a const object %qE is not allowed in "
+ "a constant expression", TREE_OPERAND (expr, 0));
+ inform (location_of (obj), "originally declared %<const%> here");
+}
+
/* Subroutine of cxx_eval_constant_expression.
Evaluate the call expression tree T in the context of OLD_CALL expression
evaluation. */
depth_ok = push_cx_call_context (t);
+ /* Remember the object we are constructing. */
+ tree new_obj = NULL_TREE;
+ if (DECL_CONSTRUCTOR_P (fun))
+ {
+ /* In a constructor, it should be the first `this' argument.
+ At this point it has already been evaluated in the call
+ to cxx_bind_parameters_in_call. */
+ new_obj = TREE_VEC_ELT (new_call.bindings, 0);
+ STRIP_NOPS (new_obj);
+ if (TREE_CODE (new_obj) == ADDR_EXPR)
+ new_obj = TREE_OPERAND (new_obj, 0);
+ }
+
tree result = NULL_TREE;
constexpr_call *entry = NULL;
}
}
+ /* At this point, the object's constructor will have run, so
+ the object is no longer under construction, and its possible
+ 'const' semantics now apply. Make a note of this fact by
+ marking the CONSTRUCTOR TREE_READONLY. */
+ if (new_obj
+ && CLASS_TYPE_P (TREE_TYPE (new_obj))
+ && CP_TYPE_CONST_P (TREE_TYPE (new_obj)))
+ {
+ /* Subobjects might not be stored in ctx->values but we can
+ get its CONSTRUCTOR by evaluating *this. */
+ tree e = cxx_eval_constant_expression (ctx, new_obj,
+ /*lval*/false,
+ non_constant_p,
+ overflow_p);
+ TREE_READONLY (e) = true;
+ }
+
/* Forget the saved values of the callee's SAVE_EXPRs. */
unsigned int i;
tree save_expr;
}
}
+/* Return true if we are modifying something that is const during constant
+ expression evaluation. CODE is the code of the statement, OBJ is the
+ object in question, MUTABLE_P is true if one of the subobjects were
+ declared mutable. */
+
+static bool
+modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
+{
+ /* If this is initialization, there's no problem. */
+ if (code != MODIFY_EXPR)
+ return false;
+
+ /* [basic.type.qualifier] "A const object is an object of type
+ const T or a non-mutable subobject of a const object." */
+ if (mutable_p)
+ return false;
+
+ return (TREE_READONLY (obj) || CP_TYPE_CONST_P (TREE_TYPE (obj)));
+}
+
/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
static tree
/* Find the underlying variable. */
releasing_vec refs;
tree object = NULL_TREE;
+ /* If we're modifying a const object, save it. */
+ tree const_object_being_modified = NULL_TREE;
+ bool mutable_p = false;
for (tree probe = target; object == NULL_TREE; )
{
switch (TREE_CODE (probe))
{
tree ob = TREE_OPERAND (probe, 0);
tree elt = TREE_OPERAND (probe, 1);
+ if (DECL_P (elt) && DECL_MUTABLE_P (elt))
+ mutable_p = true;
+ if (evaluated
+ && modifying_const_object_p (TREE_CODE (t), probe, mutable_p)
+ && const_object_being_modified == NULL_TREE)
+ const_object_being_modified = probe;
if (TREE_CODE (probe) == ARRAY_REF)
{
elt = eval_and_check_array_index (ctx, probe, false,
}
}
+ if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
+ && const_object_being_modified == NULL_TREE)
+ const_object_being_modified = object;
+
/* And then find/build up our initializer for the path to the subobject
we're initializing. */
tree *valp;
valp = &cep->value;
}
+ /* Detect modifying a constant object in constexpr evaluation.
+ We have found a const object that is being modified. Figure out
+ if we need to issue an error. Consider
+
+ struct A {
+ int n;
+ constexpr A() : n(1) { n = 2; } // #1
+ };
+ struct B {
+ const A a;
+ constexpr B() { a.n = 3; } // #2
+ };
+ constexpr B b{};
+
+ #1 is OK, since we're modifying an object under construction, but
+ #2 is wrong, since "a" is const and has been fully constructed.
+ To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
+ which means that the object is read-only. For the example above, the
+ *ctors stack at the point of #2 will look like:
+
+ ctors[0] = {.a={.n=2}} TREE_READONLY = 0
+ ctors[1] = {.n=2} TREE_READONLY = 1
+
+ and we're modifying "b.a", so we search the stack and see if the
+ constructor for "b.a" has already run. */
+ if (const_object_being_modified)
+ {
+ bool fail = false;
+ if (!CLASS_TYPE_P (TREE_TYPE (const_object_being_modified)))
+ fail = true;
+ else
+ {
+ /* [class.ctor]p5 "A constructor can be invoked for a const,
+ volatile, or const volatile object. const and volatile
+ semantics are not applied on an object under construction.
+ They come into effect when the constructor for the most
+ derived object ends." */
+ tree elt;
+ unsigned int i;
+ FOR_EACH_VEC_ELT (*ctors, i, elt)
+ if (same_type_ignoring_top_level_qualifiers_p
+ (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
+ {
+ fail = TREE_READONLY (elt);
+ break;
+ }
+ }
+ if (fail)
+ {
+ if (!ctx->quiet)
+ modifying_const_object_error (t, const_object_being_modified);
+ *non_constant_p = true;
+ return t;
+ }
+ }
+
if (!preeval)
{
/* Create a new CONSTRUCTOR in case evaluation of the initializer
VERIFY_CONSTANT (mod);
/* Storing the modified value. */
- tree store = build2 (MODIFY_EXPR, type, op, mod);
+ tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
+ MODIFY_EXPR, type, op, mod);
cxx_eval_constant_expression (ctx, store,
true, non_constant_p, overflow_p);
ggc_free (store);
non_constant_p, overflow_p);
/* Don't share a CONSTRUCTOR that might be changed. */
init = unshare_constructor (init);
+ /* Remember that a constant object's constructor has already
+ run. */
+ if (CLASS_TYPE_P (TREE_TYPE (r))
+ && CP_TYPE_CONST_P (TREE_TYPE (r)))
+ TREE_READONLY (init) = true;
ctx->values->put (r, init);
}
else if (ctx == &new_ctx)