More delayed lambda capture fixes.

* call.c (add_function_candidate): Use build_address.
	(build_op_call_1): Call mark_lvalue_use early.
	(build_over_call): Handle error from build_this.
	* constexpr.c (cxx_bind_parameters_in_call): Use build_address.
	(cxx_eval_increment_expression): Don't use rvalue().
	* cvt.c (convert_to_void): Use mark_discarded_use.
	* expr.c (mark_use): Handle PARM_DECL, NON_DEPENDENT_EXPR.  Fix
	reference handling.  Don't copy the expression.
	(mark_discarded_use): New.
	* lambda.c (insert_capture_proxy): Add some sanity checking.
	(maybe_add_lambda_conv_op): Set cp_unevaluated_operand.
	* pt.c (register_local_specialization): Add sanity check.
	* semantics.c (process_outer_var_ref): Fix check for existing proxy.
	* typeck.c (cp_build_addr_expr_1): Handle error from
	mark_lvalue_use.
	(cp_build_modify_expr): Call mark_lvalue_use_nonread, handle error
	from rvalue.

	Handle generic lambda capture in dependent expressions.
	* lambda.c (need_generic_capture, dependent_capture_r)
	(do_dependent_capture): New.
	* pt.c (processing_nonlambda_template): Use need_generic_capture.
	* semantics.c (maybe_cleanup_point_expr)
	(maybe_cleanup_point_expr_void, finish_goto_stmt)
	(maybe_convert_cond): Call do_dependent_capture.
	* typeck.c (build_static_cast): Remove dependent capture handling.

From-SVN: r253601
This commit is contained in:
Jason Merrill 2017-10-10 14:04:02 -04:00 committed by Jason Merrill
parent e1bea3412a
commit 84dd815ff8
16 changed files with 300 additions and 74 deletions

View file

@ -1,5 +1,33 @@
2017-10-10 Jason Merrill <jason@redhat.com>
More delayed lambda capture fixes.
* call.c (add_function_candidate): Use build_address.
(build_op_call_1): Call mark_lvalue_use early.
(build_over_call): Handle error from build_this.
* constexpr.c (cxx_bind_parameters_in_call): Use build_address.
(cxx_eval_increment_expression): Don't use rvalue().
* cvt.c (convert_to_void): Use mark_discarded_use.
* expr.c (mark_use): Handle PARM_DECL, NON_DEPENDENT_EXPR. Fix
reference handling. Don't copy the expression.
(mark_discarded_use): New.
* lambda.c (insert_capture_proxy): Add some sanity checking.
(maybe_add_lambda_conv_op): Set cp_unevaluated_operand.
* pt.c (register_local_specialization): Add sanity check.
* semantics.c (process_outer_var_ref): Fix check for existing proxy.
* typeck.c (cp_build_addr_expr_1): Handle error from
mark_lvalue_use.
(cp_build_modify_expr): Call mark_lvalue_use_nonread, handle error
from rvalue.
Handle generic lambda capture in dependent expressions.
* lambda.c (need_generic_capture, dependent_capture_r)
(do_dependent_capture): New.
* pt.c (processing_nonlambda_template): Use need_generic_capture.
* semantics.c (maybe_cleanup_point_expr)
(maybe_cleanup_point_expr_void, finish_goto_stmt)
(maybe_convert_cond): Call do_dependent_capture.
* typeck.c (build_static_cast): Remove dependent capture handling.
* typeck.c (condition_conversion): Assert !processing_template_decl.
* semantics.c (finish_omp_clauses): Don't
fold_build_cleanup_point_expr if processing_template_decl.

View file

@ -2160,7 +2160,10 @@ add_function_candidate (struct z_candidate **candidates,
else
{
parmtype = build_pointer_type (parmtype);
arg = build_this (arg);
/* We don't use build_this here because we don't want to
capture the object argument until we've chosen a
non-static member function. */
arg = build_address (arg);
argtype = lvalue_type (arg);
}
}
@ -4446,14 +4449,17 @@ build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
{
struct z_candidate *candidates = 0, *cand;
tree fns, convs, first_mem_arg = NULL_TREE;
tree type = TREE_TYPE (obj);
bool any_viable_p;
tree result = NULL_TREE;
void *p;
obj = mark_lvalue_use (obj);
if (error_operand_p (obj))
return error_mark_node;
tree type = TREE_TYPE (obj);
obj = prep_operand (obj);
if (TYPE_PTRMEMFUNC_P (type))
@ -7772,6 +7778,9 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
tree converted_arg;
tree base_binfo;
if (arg == error_mark_node)
return error_mark_node;
if (convs[i]->bad_p)
{
if (complain & tf_error)

View file

@ -1268,7 +1268,10 @@ cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
&& is_dummy_object (x))
{
x = ctx->object;
x = cp_build_addr_expr (x, tf_warning_or_error);
/* We don't use cp_build_addr_expr here because we don't want to
capture the object argument until we've chosen a non-static member
function. */
x = build_address (x);
}
bool lval = false;
arg = cxx_eval_constant_expression (ctx, x, lval,
@ -3642,9 +3645,9 @@ cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
non_constant_p, overflow_p);
/* The operand as an rvalue. */
tree val = rvalue (op);
val = cxx_eval_constant_expression (ctx, val, false,
non_constant_p, overflow_p);
tree val
= cxx_eval_constant_expression (ctx, op, false,
non_constant_p, overflow_p);
/* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
a local array in a constexpr function. */
bool ptr = POINTER_TYPE_P (TREE_TYPE (val));

View file

@ -6270,6 +6270,7 @@ extern tree mark_rvalue_use (tree,
extern tree mark_lvalue_use (tree);
extern tree mark_lvalue_use_nonread (tree);
extern tree mark_type_use (tree);
extern tree mark_discarded_use (tree);
extern void mark_exp_read (tree);
/* friend.c */
@ -6432,6 +6433,7 @@ extern tree lookup_template_variable (tree, tree);
extern int uses_template_parms (tree);
extern bool uses_template_parms_level (tree, int);
extern bool in_template_function (void);
extern bool need_generic_capture (void);
extern bool processing_nonlambda_template (void);
extern tree instantiate_class_template (tree);
extern tree instantiate_template (tree, tree, tsubst_flags_t);
@ -6833,6 +6835,7 @@ extern tree current_nonlambda_function (void);
extern tree nonlambda_method_basetype (void);
extern tree current_nonlambda_scope (void);
extern bool generic_lambda_fn_p (tree);
extern tree do_dependent_capture (tree, bool = false);
extern bool lambda_fn_in_template_p (tree);
extern void maybe_add_lambda_conv_op (tree);
extern bool is_lambda_ignored_entity (tree);

View file

@ -1055,24 +1055,10 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
|| TREE_TYPE (expr) == error_mark_node)
return error_mark_node;
expr = mark_discarded_use (expr);
if (implicit == ICV_CAST)
/* An explicit cast to void avoids all -Wunused-but-set* warnings. */
mark_exp_read (expr);
else
{
tree exprv = expr;
while (TREE_CODE (exprv) == COMPOUND_EXPR)
exprv = TREE_OPERAND (exprv, 1);
if (DECL_P (exprv)
|| handled_component_p (exprv)
|| INDIRECT_REF_P (exprv))
/* Expr is not being 'used' here, otherwise we whould have
called mark_{rl}value_use use here, which would have in turn
called mark_exp_read. Rather, we call mark_exp_read directly
to avoid some warnings when
-Wunused-but-set-{variable,parameter} is in effect. */
mark_exp_read (exprv);
}
if (!TREE_TYPE (expr))
return expr;

View file

@ -6867,6 +6867,8 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
DECL_INITIAL (decl) = NULL_TREE;
}
init = do_dependent_capture (init);
/* Generally, initializers in templates are expanded when the
template is instantiated. But, if DECL is a variable constant
then it can be used in future constant expressions, so its value

View file

@ -96,16 +96,21 @@ mark_use (tree expr, bool rvalue_p, bool read_p,
{
#define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin)
if (expr == NULL_TREE || expr == error_mark_node)
return expr;
if (reject_builtin && reject_gcc_builtin (expr, loc))
return error_mark_node;
if (read_p)
mark_exp_read (expr);
tree oexpr = expr;
bool recurse_op[3] = { false, false, false };
switch (TREE_CODE (expr))
{
case VAR_DECL:
case PARM_DECL:
if (outer_automatic_var_p (expr)
&& decl_constant_var_p (expr))
{
@ -119,10 +124,13 @@ mark_use (tree expr, bool rvalue_p, bool read_p,
}
}
expr = process_outer_var_ref (expr, tf_warning_or_error, true);
expr = convert_from_reference (expr);
if (!(TREE_TYPE (oexpr)
&& TREE_CODE (TREE_TYPE (oexpr)) == REFERENCE_TYPE))
expr = convert_from_reference (expr);
}
break;
case COMPONENT_REF:
case NON_DEPENDENT_EXPR:
recurse_op[0] = true;
break;
case COMPOUND_EXPR:
@ -140,35 +148,23 @@ mark_use (tree expr, bool rvalue_p, bool read_p,
tree ref = TREE_OPERAND (expr, 0);
tree r = mark_rvalue_use (ref, loc, reject_builtin);
if (r != ref)
{
expr = copy_node (expr);
TREE_OPERAND (expr, 0) = r;
}
expr = convert_from_reference (r);
}
break;
default:
break;
}
bool changed = false;
tree ops[3];
for (int i = 0; i < 3; ++i)
if (recurse_op[i])
{
tree op = TREE_OPERAND (expr, i);
ops[i] = RECUR (op);
if (ops[i] != op)
changed = true;
op = RECUR (op);
if (op == error_mark_node)
return error_mark_node;
TREE_OPERAND (expr, i) = op;
}
if (changed)
{
expr = copy_node (expr);
for (int i = 0; i < 3; ++i)
if (recurse_op[i])
TREE_OPERAND (expr, i) = ops[i];
}
return expr;
#undef RECUR
}
@ -187,6 +183,52 @@ mark_rvalue_use (tree e,
return mark_use (e, true, true, loc, reject_builtin);
}
/* Called when expr appears as a discarded-value expression. */
tree
mark_discarded_use (tree expr)
{
/* The lvalue-to-rvalue conversion (7.1) is applied if and only if the
expression is a glvalue of volatile-qualified type and it is one of the
following:
* ( expression ), where expression is one of these expressions,
* id-expression (8.1.4),
* subscripting (8.2.1),
* class member access (8.2.5),
* indirection (8.3.1),
* pointer-to-member operation (8.5),
* conditional expression (8.16) where both the second and the third
operands are one of these expressions, or
* comma expression (8.19) where the right operand is one of these
expressions. */
if (expr == NULL_TREE)
return expr;
switch (TREE_CODE (expr))
{
case COND_EXPR:
TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2));
gcc_fallthrough ();
case COMPOUND_EXPR:
TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1));
return expr;
case COMPONENT_REF:
case ARRAY_REF:
case INDIRECT_REF:
case MEMBER_REF:
break;
default:
if (DECL_P (expr))
break;
else
return expr;
}
/* Like mark_rvalue_use, but don't reject built-ins. */
return mark_use (expr, true, true, input_location, false);
}
/* Called whenever an expression is used in an lvalue context. */
tree

View file

@ -297,7 +297,17 @@ void
insert_capture_proxy (tree var)
{
if (is_normal_capture_proxy (var))
register_local_specialization (var, DECL_CAPTURED_VARIABLE (var));
{
tree cap = DECL_CAPTURED_VARIABLE (var);
if (CHECKING_P)
{
gcc_assert (!is_normal_capture_proxy (cap));
tree old = retrieve_local_specialization (cap);
if (old)
gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var));
}
register_local_specialization (var, cap);
}
/* Put the capture proxy in the extra body block so that it won't clash
with a later local variable. */
@ -977,6 +987,121 @@ generic_lambda_fn_p (tree callop)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
}
/* Returns true iff we need to consider default capture for an enclosing
generic lambda. */
bool
need_generic_capture (void)
{
if (!processing_template_decl)
return false;
tree outer_closure = NULL_TREE;
for (tree t = current_class_type; t;
t = decl_type_context (TYPE_MAIN_DECL (t)))
{
tree lam = CLASSTYPE_LAMBDA_EXPR (t);
if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
/* No default capture. */
break;
outer_closure = t;
}
if (!outer_closure)
/* No lambda. */
return false;
else if (dependent_type_p (outer_closure))
/* The enclosing context isn't instantiated. */
return false;
else
return true;
}
/* A lambda-expression...is said to implicitly capture the entity...if the
compound-statement...names the entity in a potentially-evaluated
expression where the enclosing full-expression depends on a generic lambda
parameter declared within the reaching scope of the lambda-expression. */
static tree
dependent_capture_r (tree *tp, int *walk_subtrees, void *data)
{
hash_set<tree> *pset = (hash_set<tree> *)data;
if (TYPE_P (*tp))
*walk_subtrees = 0;
if (outer_automatic_var_p (*tp))
{
tree t = process_outer_var_ref (*tp, tf_warning_or_error, /*force*/true);
if (t != *tp
&& TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
&& TREE_CODE (TREE_TYPE (*tp)) != REFERENCE_TYPE)
t = convert_from_reference (t);
*tp = t;
}
if (pset->add (*tp))
*walk_subtrees = 0;
switch (TREE_CODE (*tp))
{
/* Don't walk into unevaluated context or another lambda. */
case SIZEOF_EXPR:
case ALIGNOF_EXPR:
case TYPEID_EXPR:
case NOEXCEPT_EXPR:
case LAMBDA_EXPR:
*walk_subtrees = 0;
break;
/* Don't walk into statements whose subexpressions we already
handled. */
case TRY_BLOCK:
case EH_SPEC_BLOCK:
case HANDLER:
case IF_STMT:
case FOR_STMT:
case RANGE_FOR_STMT:
case WHILE_STMT:
case DO_STMT:
case SWITCH_STMT:
case STATEMENT_LIST:
case RETURN_EXPR:
*walk_subtrees = 0;
break;
case DECL_EXPR:
{
tree decl = DECL_EXPR_DECL (*tp);
if (VAR_P (decl))
{
/* walk_tree_1 won't step in here. */
cp_walk_tree (&DECL_INITIAL (decl),
dependent_capture_r, &pset, NULL);
*walk_subtrees = 0;
}
}
break;
default:
break;
}
return NULL_TREE;
}
tree
do_dependent_capture (tree expr, bool force)
{
if (!need_generic_capture ()
|| (!force && !instantiation_dependent_expression_p (expr)))
return expr;
hash_set<tree> pset;
cp_walk_tree (&expr, dependent_capture_r, &pset, NULL);
return expr;
}
/* If the closure TYPE has a static op(), also add a conversion to function
pointer. */
@ -1073,7 +1198,10 @@ maybe_add_lambda_conv_op (tree type)
if (generic_lambda_p)
{
/* Avoid capturing variables in this context. */
++cp_unevaluated_operand;
tree a = forward_parm (tgt);
--cp_unevaluated_operand;
CALL_EXPR_ARG (call, ix) = a;
if (decltype_call)

View file

@ -11873,6 +11873,8 @@ cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
tree iter_type, begin_expr, end_expr;
tree condition, expression;
range_expr = mark_lvalue_use (range_expr);
if (range_decl == error_mark_node || range_expr == error_mark_node)
/* If an error happened previously do nothing or else a lot of
unhelpful errors would be issued. */

View file

@ -1895,6 +1895,7 @@ reregister_specialization (tree spec, tree tinfo, tree new_spec)
void
register_local_specialization (tree spec, tree tmpl)
{
gcc_assert (tmpl != spec);
local_specializations->put (tmpl, spec);
}
@ -9494,30 +9495,14 @@ in_template_function (void)
return ret;
}
/* Returns true iff we are currently within a template other than a generic
lambda. We test this by finding the outermost closure type and checking
whether it is dependent. */
/* Returns true iff we are currently within a template other than a
default-capturing generic lambda, so we don't need to worry about semantic
processing. */
bool
processing_nonlambda_template (void)
{
if (!processing_template_decl)
return false;
tree outer_closure = NULL_TREE;
for (tree t = current_class_type; t;
t = decl_type_context (TYPE_MAIN_DECL (t)))
{
if (LAMBDA_TYPE_P (t))
outer_closure = t;
else
break;
}
if (outer_closure)
return dependent_type_p (outer_closure);
else
return true;
return processing_template_decl && !need_generic_capture ();
}
/* Returns true if T depends on any template parameter with level LEVEL. */

View file

@ -410,6 +410,8 @@ maybe_cleanup_point_expr (tree expr)
{
if (!processing_template_decl && stmts_are_full_exprs_p ())
expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
else
expr = do_dependent_capture (expr);
return expr;
}
@ -423,6 +425,8 @@ maybe_cleanup_point_expr_void (tree expr)
{
if (!processing_template_decl && stmts_are_full_exprs_p ())
expr = fold_build_cleanup_point_expr (void_type_node, expr);
else
expr = do_dependent_capture (expr);
return expr;
}
@ -629,6 +633,8 @@ finish_goto_stmt (tree destination)
= fold_build_cleanup_point_expr (TREE_TYPE (destination),
destination);
}
else
destination = do_dependent_capture (destination);
}
check_goto (destination);
@ -650,7 +656,7 @@ maybe_convert_cond (tree cond)
/* Wait until we instantiate templates before doing conversion. */
if (processing_template_decl)
return cond;
return do_dependent_capture (cond);
if (warn_sequence_point)
verify_sequence_points (cond);
@ -3314,8 +3320,12 @@ process_outer_var_ref (tree decl, tsubst_flags_t complain, bool force_use)
if (containing_function && LAMBDA_FUNCTION_P (containing_function))
{
/* Check whether we've already built a proxy. */
tree d = retrieve_local_specialization (decl);
if (d && is_capture_proxy (d))
tree var = decl;
while (is_normal_capture_proxy (var))
var = DECL_CAPTURED_VARIABLE (var);
tree d = retrieve_local_specialization (var);
if (d && d != decl && is_capture_proxy (d))
{
if (DECL_CONTEXT (d) == containing_function)
/* We already have an inner proxy. */

View file

@ -5654,6 +5654,9 @@ cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
return error_mark_node;
arg = mark_lvalue_use (arg);
if (error_operand_p (arg))
return error_mark_node;
argtype = lvalue_type (arg);
gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
@ -7059,11 +7062,7 @@ build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
if (dependent)
{
tmpl:
expr = oexpr;
if (dependent)
/* Handle generic lambda capture. */
expr = mark_lvalue_use (expr);
expr = build_min (STATIC_CAST_EXPR, type, expr);
expr = build_min (STATIC_CAST_EXPR, type, oexpr);
/* We don't know if it will or will not have side effects. */
TREE_SIDE_EFFECTS (expr) = 1;
return convert_from_reference (expr);
@ -7702,6 +7701,8 @@ tree
cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
tree rhs, tsubst_flags_t complain)
{
lhs = mark_lvalue_use_nonread (lhs);
tree result = NULL_TREE;
tree newrhs = rhs;
tree lhstype = TREE_TYPE (lhs);
@ -7924,6 +7925,8 @@ cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
operator. -- end note ] */
lhs = cp_stabilize_reference (lhs);
rhs = rvalue (rhs);
if (rhs == error_mark_node)
return error_mark_node;
rhs = stabilize_expr (rhs, &init);
newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
if (newrhs == error_mark_node)
@ -9109,7 +9112,7 @@ check_return_expr (tree retval, bool *no_warning)
dependent:
/* We should not have changed the return value. */
gcc_assert (retval == saved_retval);
return retval;
return do_dependent_capture (retval, /*force*/true);
}
/* The fabled Named Return Value optimization, as per [class.copy]/15:

View file

@ -1,10 +1,17 @@
// PR c++/34395
// { dg-do compile { target c++11 } }
template<int... N> void foo (int... x[N]) // { dg-message "int \\\[N\\\]\\.\\.\\. x" }
void f(...);
template<int... N> void foo (int... x[N]) // { dg-message "declared here" }
{
struct A
{
A () { x; } // { dg-error "use of parameter from containing function" }
A () { f(x...); } // { dg-error "use of parameter from containing function" }
};
}
int main()
{
int ar[4];
foo<4>(ar);
}

View file

@ -0,0 +1,18 @@
// { dg-do compile { target c++14 } }
struct A { void operator()(int) const {} };
template <class T>
void f()
{
constexpr A a {};
[=](auto b) {
a(b);
}(42);
}
int main()
{
f<int>();
}

View file

@ -12,7 +12,7 @@ using Void = void;
template<typename F,typename A>
auto
bar(F f, A a) -> decltype( ( f(a) , 0 ) ) // { dg-error "no match" }
bar(F f, A a) -> decltype( ( f(a) , 0 ) ) // { dg-message "" }
{ return {}; }

View file

@ -1,5 +1,5 @@
// PR c++/50861
template<class T> struct A {A(int b=k(0));}; // { dg-error "parameter|arguments" }
template<class T> struct A {A(int b=k(0));}; // { dg-error "parameter|argument" }
void f(int k){A<int> a;} // // { dg-message "declared" }
// { dg-message "note" "note" { target *-*-* } 3 }