if (is_really_empty_class (type, /*ignore_vptr*/true))
{
/* Avoid copying empty classes, but ensure op= returns an lvalue even
- if the object argument isn't one. This isn't needed in other cases
- since MODIFY_EXPR is always considered an lvalue. */
- to = cp_build_addr_expr (to, tf_none);
- to = cp_build_indirect_ref (input_location, to, RO_ARROW, complain);
+ if the object argument isn't one. */
+ to = force_lvalue (to, complain);
val = build2 (COMPOUND_EXPR, type, arg, to);
suppress_warning (val, OPT_Wunused);
}
tree array_type, alias_set;
arg2 = TYPE_SIZE_UNIT (as_base);
+ /* Ensure op= returns an lvalue even if the object argument isn't
+ one. */
+ to = force_lvalue (to, complain);
to = cp_stabilize_reference (to);
arg0 = cp_build_addr_expr (to, complain);
tsubst_flags_t);
extern tree convert_from_reference (tree);
extern tree force_rvalue (tree, tsubst_flags_t);
+extern tree force_lvalue (tree, tsubst_flags_t);
extern tree ocp_convert (tree, tree, int, int,
tsubst_flags_t);
extern tree cp_convert (tree, tree, tsubst_flags_t);
return expr;
}
+/* Force EXPR to be an lvalue, if it isn't already. */
+
+tree
+force_lvalue (tree expr, tsubst_flags_t complain)
+{
+ if (!lvalue_p (expr))
+ {
+ expr = cp_build_addr_expr (expr, complain);
+ expr = cp_build_indirect_ref (input_location, expr, RO_ARROW, complain);
+ }
+ return expr;
+}
+
\f
/* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
return error_mark_node;
}
else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
- && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))
- /* Don't let this change the value category. '*&TARGET_EXPR'
- is an lvalue, but folding it into 'TARGET_EXPR' would turn
- it into a prvalue of class type. */
- && lvalue_p (TREE_OPERAND (pointer, 0)))
+ && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
/* The POINTER was something like `&x'. We simplify `*&x' to
- `x'. */
+ `x'. This can change the value category: '*&TARGET_EXPR'
+ is an lvalue and folding it into 'TARGET_EXPR' turns it into
+ a prvalue of class type. */
return TREE_OPERAND (pointer, 0);
else
{
--- /dev/null
+// PR c++/119383
+// { dg-do run { target c++11 } }
+
+int g;
+
+struct base {
+ virtual base *clone() const = 0;
+ ~base() { }
+};
+
+struct impl : virtual base {
+ base *clone() const { return new impl; } // #1
+ impl() { ++g; }
+ ~impl() { --g; }
+};
+
+const base *
+make_a_clone ()
+{
+ const base &base = impl{}; // #2
+ return base.clone();
+}
+
+int
+main ()
+{
+ make_a_clone ();
+ // impl::impl() is called twice (#1 and #2), impl::~impl() once,
+ // at the end of make_a_clone.
+ if (g != 1)
+ __builtin_abort ();
+}