1 /* Perform the semantic phase of lambda parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
28 #include "stringpool.h"
30 #include "tree-iterator.h"
34 /* Constructor for a lambda expression. */
37 build_lambda_expr (void)
39 tree lambda
= make_node (LAMBDA_EXPR
);
40 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
) = CPLD_NONE
;
41 LAMBDA_EXPR_CAPTURE_LIST (lambda
) = NULL_TREE
;
42 LAMBDA_EXPR_THIS_CAPTURE (lambda
) = NULL_TREE
;
43 LAMBDA_EXPR_PENDING_PROXIES (lambda
) = NULL
;
44 LAMBDA_EXPR_MUTABLE_P (lambda
) = false;
48 /* Create the closure object for a LAMBDA_EXPR. */
51 build_lambda_object (tree lambda_expr
)
53 /* Build aggregate constructor call.
54 - cp_parser_braced_list
55 - cp_parser_functional_cast */
56 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
57 tree node
, expr
, type
;
60 if (processing_template_decl
|| lambda_expr
== error_mark_node
)
63 /* Make sure any error messages refer to the lambda-introducer. */
64 saved_loc
= input_location
;
65 input_location
= LAMBDA_EXPR_LOCATION (lambda_expr
);
67 for (node
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
69 node
= TREE_CHAIN (node
))
71 tree field
= TREE_PURPOSE (node
);
72 tree val
= TREE_VALUE (node
);
74 if (field
== error_mark_node
)
76 expr
= error_mark_node
;
80 if (TREE_CODE (val
) == TREE_LIST
)
81 val
= build_x_compound_expr_from_list (val
, ELK_INIT
,
87 /* Mere mortals can't copy arrays with aggregate initialization, so
88 do some magic to make it work here. */
89 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
90 val
= build_array_copy (val
);
91 else if (DECL_NORMAL_CAPTURE_P (field
)
92 && !DECL_VLA_CAPTURE_P (field
)
93 && !TYPE_REF_P (TREE_TYPE (field
)))
95 /* "the entities that are captured by copy are used to
96 direct-initialize each corresponding non-static data
97 member of the resulting closure object."
99 There's normally no way to express direct-initialization
100 from an element of a CONSTRUCTOR, so we build up a special
101 TARGET_EXPR to bypass the usual copy-initialization. */
102 val
= force_rvalue (val
, tf_warning_or_error
);
103 if (TREE_CODE (val
) == TARGET_EXPR
)
104 TARGET_EXPR_DIRECT_INIT_P (val
) = true;
107 CONSTRUCTOR_APPEND_ELT (elts
, DECL_NAME (field
), val
);
110 expr
= build_constructor (init_list_type_node
, elts
);
111 CONSTRUCTOR_IS_DIRECT_INIT (expr
) = 1;
113 /* N2927: "[The closure] class type is not an aggregate."
114 But we briefly treat it as an aggregate to make this simpler. */
115 type
= LAMBDA_EXPR_CLOSURE (lambda_expr
);
116 CLASSTYPE_NON_AGGREGATE (type
) = 0;
117 expr
= finish_compound_literal (type
, expr
, tf_warning_or_error
);
118 CLASSTYPE_NON_AGGREGATE (type
) = 1;
121 input_location
= saved_loc
;
125 /* Return an initialized RECORD_TYPE for LAMBDA.
126 LAMBDA must have its explicit captures already. */
129 begin_lambda_type (tree lambda
)
134 /* Unique name. This is just like an unnamed class, but we cannot use
135 make_anon_name because of certain checks against TYPE_UNNAMED_P. */
137 name
= make_lambda_name ();
139 /* Create the new RECORD_TYPE for this lambda. */
140 type
= xref_tag (/*tag_code=*/record_type
,
143 /*template_header_p=*/false);
144 if (type
== error_mark_node
)
145 return error_mark_node
;
148 /* Designate it as a struct so that we can use aggregate initialization. */
149 CLASSTYPE_DECLARED_CLASS (type
) = false;
151 /* Cross-reference the expression and the type. */
152 LAMBDA_EXPR_CLOSURE (lambda
) = type
;
153 CLASSTYPE_LAMBDA_EXPR (type
) = lambda
;
155 /* In C++17, assume the closure is literal; we'll clear the flag later if
157 if (cxx_dialect
>= cxx17
)
158 CLASSTYPE_LITERAL_P (type
) = true;
160 /* Clear base types. */
161 xref_basetypes (type
, /*bases=*/NULL_TREE
);
163 /* Start the class. */
164 type
= begin_class_definition (type
);
169 /* Returns the type to use for the return type of the operator() of a
173 lambda_return_type (tree expr
)
175 if (expr
== NULL_TREE
)
176 return void_type_node
;
177 if (type_unknown_p (expr
)
178 || BRACE_ENCLOSED_INITIALIZER_P (expr
))
180 cxx_incomplete_type_error (expr
, TREE_TYPE (expr
));
181 return error_mark_node
;
183 gcc_checking_assert (!type_dependent_expression_p (expr
));
184 return cv_unqualified (type_decays_to (unlowered_expr_type (expr
)));
187 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
191 lambda_function (tree lambda
)
194 if (TREE_CODE (lambda
) == LAMBDA_EXPR
)
195 type
= LAMBDA_EXPR_CLOSURE (lambda
);
198 gcc_assert (LAMBDA_TYPE_P (type
));
199 /* Don't let debug_tree cause instantiation. */
200 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
201 && !COMPLETE_OR_OPEN_TYPE_P (type
))
203 lambda
= lookup_member (type
, call_op_identifier
,
204 /*protect=*/0, /*want_type=*/false,
205 tf_warning_or_error
);
207 lambda
= STRIP_TEMPLATE (get_first_fn (lambda
));
211 /* Returns the type to use for the FIELD_DECL corresponding to the
212 capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
213 C++14 init capture, and BY_REFERENCE_P indicates whether we're
214 capturing by reference. */
217 lambda_capture_field_type (tree expr
, bool explicit_init_p
,
221 bool is_this
= is_this_parameter (tree_strip_nop_conversions (expr
));
223 if (!is_this
&& type_dependent_expression_p (expr
))
225 type
= cxx_make_type (DECLTYPE_TYPE
);
226 DECLTYPE_TYPE_EXPR (type
) = expr
;
227 DECLTYPE_FOR_LAMBDA_CAPTURE (type
) = true;
228 DECLTYPE_FOR_INIT_CAPTURE (type
) = explicit_init_p
;
229 DECLTYPE_FOR_REF_CAPTURE (type
) = by_reference_p
;
230 SET_TYPE_STRUCTURAL_EQUALITY (type
);
232 else if (!is_this
&& explicit_init_p
)
234 tree auto_node
= make_auto ();
238 /* Add the reference now, so deduction doesn't lose
239 outermost CV qualifiers of EXPR. */
240 type
= build_reference_type (type
);
241 type
= do_auto_deduction (type
, expr
, auto_node
);
245 type
= non_reference (unlowered_expr_type (expr
));
248 && (by_reference_p
|| TREE_CODE (type
) == FUNCTION_TYPE
))
249 type
= build_reference_type (type
);
255 /* Returns true iff DECL is a lambda capture proxy variable created by
256 build_capture_proxy. */
259 is_capture_proxy (tree decl
)
262 && DECL_HAS_VALUE_EXPR_P (decl
)
263 && !DECL_ANON_UNION_VAR_P (decl
)
264 && !DECL_DECOMPOSITION_P (decl
)
265 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl
)));
268 /* Returns true iff DECL is a capture proxy for a normal capture
269 (i.e. without explicit initializer). */
272 is_normal_capture_proxy (tree decl
)
274 if (!is_capture_proxy (decl
))
275 /* It's not a capture proxy. */
278 if (variably_modified_type_p (TREE_TYPE (decl
), NULL_TREE
))
282 /* It is a capture proxy, is it a normal capture? */
283 tree val
= DECL_VALUE_EXPR (decl
);
284 if (val
== error_mark_node
)
287 if (TREE_CODE (val
) == ADDR_EXPR
)
288 val
= TREE_OPERAND (val
, 0);
289 gcc_assert (TREE_CODE (val
) == COMPONENT_REF
);
290 val
= TREE_OPERAND (val
, 1);
291 return DECL_NORMAL_CAPTURE_P (val
);
294 /* Returns true iff DECL is a capture proxy for a normal capture
295 of a constant variable. */
298 is_constant_capture_proxy (tree decl
)
300 if (is_normal_capture_proxy (decl
))
301 return decl_constant_var_p (DECL_CAPTURED_VARIABLE (decl
));
305 /* VAR is a capture proxy created by build_capture_proxy; add it to the
306 current function, which is the operator() for the appropriate lambda. */
309 insert_capture_proxy (tree var
)
311 if (is_normal_capture_proxy (var
))
313 tree cap
= DECL_CAPTURED_VARIABLE (var
);
316 gcc_assert (!is_normal_capture_proxy (cap
));
317 tree old
= retrieve_local_specialization (cap
);
319 gcc_assert (DECL_CONTEXT (old
) != DECL_CONTEXT (var
));
321 register_local_specialization (var
, cap
);
324 /* Put the capture proxy in the extra body block so that it won't clash
325 with a later local variable. */
326 pushdecl_outermost_localscope (var
);
328 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
329 var
= build_stmt (DECL_SOURCE_LOCATION (var
), DECL_EXPR
, var
);
330 tree stmt_list
= (*stmt_list_stack
)[1];
331 gcc_assert (stmt_list
);
332 append_to_statement_list_force (var
, &stmt_list
);
335 /* We've just finished processing a lambda; if the containing scope is also
336 a lambda, insert any capture proxies that were created while processing
337 the nested lambda. */
340 insert_pending_capture_proxies (void)
343 vec
<tree
, va_gc
> *proxies
;
346 if (!current_function_decl
|| !LAMBDA_FUNCTION_P (current_function_decl
))
349 lam
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl
));
350 proxies
= LAMBDA_EXPR_PENDING_PROXIES (lam
);
351 for (i
= 0; i
< vec_safe_length (proxies
); ++i
)
353 tree var
= (*proxies
)[i
];
354 insert_capture_proxy (var
);
356 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam
));
357 LAMBDA_EXPR_PENDING_PROXIES (lam
) = NULL
;
360 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
361 return the type we want the proxy to have: the type of the field itself,
362 with added const-qualification if the lambda isn't mutable and the
363 capture is by value. */
366 lambda_proxy_type (tree ref
)
369 if (ref
== error_mark_node
)
370 return error_mark_node
;
371 if (REFERENCE_REF_P (ref
))
372 ref
= TREE_OPERAND (ref
, 0);
373 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
374 type
= TREE_TYPE (ref
);
375 if (!type
|| WILDCARD_TYPE_P (non_reference (type
)))
377 type
= cxx_make_type (DECLTYPE_TYPE
);
378 DECLTYPE_TYPE_EXPR (type
) = ref
;
379 DECLTYPE_FOR_LAMBDA_PROXY (type
) = true;
380 SET_TYPE_STRUCTURAL_EQUALITY (type
);
382 if (DECL_PACK_P (TREE_OPERAND (ref
, 1)))
383 type
= make_pack_expansion (type
);
387 /* MEMBER is a capture field in a lambda closure class. Now that we're
388 inside the operator(), build a placeholder var for future lookups and
392 build_capture_proxy (tree member
, tree init
)
394 tree var
, object
, fn
, closure
, name
, lam
, type
;
396 if (PACK_EXPANSION_P (member
))
397 member
= PACK_EXPANSION_PATTERN (member
);
399 closure
= DECL_CONTEXT (member
);
400 fn
= lambda_function (closure
);
401 lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
403 /* The proxy variable forwards to the capture field. */
404 object
= build_fold_indirect_ref (DECL_ARGUMENTS (fn
));
405 object
= finish_non_static_data_member (member
, object
, NULL_TREE
);
406 if (REFERENCE_REF_P (object
))
407 object
= TREE_OPERAND (object
, 0);
409 /* Remove the __ inserted by add_capture. */
410 name
= get_identifier (IDENTIFIER_POINTER (DECL_NAME (member
)) + 2);
412 type
= lambda_proxy_type (object
);
414 if (name
== this_identifier
&& !INDIRECT_TYPE_P (type
))
416 type
= build_pointer_type (type
);
417 type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
418 object
= build_fold_addr_expr_with_type (object
, type
);
421 if (DECL_VLA_CAPTURE_P (member
))
423 /* Rebuild the VLA type from the pointer and maxindex. */
424 tree field
= next_initializable_field (TYPE_FIELDS (type
));
425 tree ptr
= build_simple_component_ref (object
, field
);
426 field
= next_initializable_field (DECL_CHAIN (field
));
427 tree max
= build_simple_component_ref (object
, field
);
428 type
= build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr
)),
429 build_index_type (max
));
430 type
= build_reference_type (type
);
431 REFERENCE_VLA_OK (type
) = true;
432 object
= convert (type
, ptr
);
435 complete_type (type
);
437 var
= build_decl (input_location
, VAR_DECL
, name
, type
);
438 SET_DECL_VALUE_EXPR (var
, object
);
439 DECL_HAS_VALUE_EXPR_P (var
) = 1;
440 DECL_ARTIFICIAL (var
) = 1;
442 DECL_CONTEXT (var
) = fn
;
444 if (DECL_NORMAL_CAPTURE_P (member
))
446 if (DECL_VLA_CAPTURE_P (member
))
448 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
449 init
= TREE_OPERAND (init
, 0); // Strip ADDR_EXPR.
450 init
= TREE_OPERAND (init
, 0); // Strip ARRAY_REF.
454 if (PACK_EXPANSION_P (init
))
455 init
= PACK_EXPANSION_PATTERN (init
);
458 if (INDIRECT_REF_P (init
))
459 init
= TREE_OPERAND (init
, 0);
462 gcc_assert (VAR_P (init
) || TREE_CODE (init
) == PARM_DECL
);
463 while (is_normal_capture_proxy (init
))
464 init
= DECL_CAPTURED_VARIABLE (init
);
465 retrofit_lang_decl (var
);
466 DECL_CAPTURED_VARIABLE (var
) = init
;
469 if (name
== this_identifier
)
471 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam
) == member
);
472 LAMBDA_EXPR_THIS_CAPTURE (lam
) = var
;
475 if (fn
== current_function_decl
)
476 insert_capture_proxy (var
);
478 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam
), var
);
483 static GTY(()) tree ptr_id
;
484 static GTY(()) tree max_id
;
486 /* Return a struct containing a pointer and a length for lambda capture of
487 an array of runtime length. */
490 vla_capture_type (tree array_type
)
492 tree type
= xref_tag (record_type
, make_anon_name (), ts_current
, false);
493 xref_basetypes (type
, NULL_TREE
);
494 type
= begin_class_definition (type
);
497 ptr_id
= get_identifier ("ptr");
498 max_id
= get_identifier ("max");
500 tree ptrtype
= build_pointer_type (TREE_TYPE (array_type
));
501 tree field
= build_decl (input_location
, FIELD_DECL
, ptr_id
, ptrtype
);
502 finish_member_declaration (field
);
503 field
= build_decl (input_location
, FIELD_DECL
, max_id
, sizetype
);
504 finish_member_declaration (field
);
505 return finish_struct (type
, NULL_TREE
);
508 /* From an ID and INITIALIZER, create a capture (by reference if
509 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
510 and return it. If ID is `this', BY_REFERENCE_P says whether
511 `*this' is captured by reference. */
514 add_capture (tree lambda
, tree id
, tree orig_init
, bool by_reference_p
,
515 bool explicit_init_p
)
518 tree type
, member
, name
;
520 bool variadic
= false;
521 tree initializer
= orig_init
;
523 if (PACK_EXPANSION_P (initializer
))
525 initializer
= PACK_EXPANSION_PATTERN (initializer
);
529 if (TREE_CODE (initializer
) == TREE_LIST
530 /* A pack expansion might end up with multiple elements. */
531 && !PACK_EXPANSION_P (TREE_VALUE (initializer
)))
532 initializer
= build_x_compound_expr_from_list (initializer
, ELK_INIT
,
533 tf_warning_or_error
);
534 type
= TREE_TYPE (initializer
);
535 if (type
== error_mark_node
)
536 return error_mark_node
;
538 if (array_of_runtime_bound_p (type
))
542 error ("array of runtime bound cannot be captured by copy, "
543 "only by reference");
545 /* For a VLA, we capture the address of the first element and the
546 maximum index, and then reconstruct the VLA for the proxy. */
547 tree elt
= cp_build_array_ref (input_location
, initializer
,
548 integer_zero_node
, tf_warning_or_error
);
549 initializer
= build_constructor_va (init_list_type_node
, 2,
550 NULL_TREE
, build_address (elt
),
551 NULL_TREE
, array_type_nelts (type
));
552 type
= vla_capture_type (type
);
554 else if (!dependent_type_p (type
)
555 && variably_modified_type_p (type
, NULL_TREE
))
557 sorry ("capture of variably-modified type %qT that is not an N3639 array "
558 "of runtime bound", type
);
559 if (TREE_CODE (type
) == ARRAY_TYPE
560 && variably_modified_type_p (TREE_TYPE (type
), NULL_TREE
))
561 inform (input_location
, "because the array element type %qT has "
562 "variable size", TREE_TYPE (type
));
563 return error_mark_node
;
567 type
= lambda_capture_field_type (initializer
, explicit_init_p
,
569 if (type
== error_mark_node
)
570 return error_mark_node
;
572 if (id
== this_identifier
&& !by_reference_p
)
574 gcc_assert (INDIRECT_TYPE_P (type
));
575 type
= TREE_TYPE (type
);
576 initializer
= cp_build_fold_indirect_ref (initializer
);
579 if (dependent_type_p (type
))
581 else if (id
!= this_identifier
&& by_reference_p
)
583 if (!lvalue_p (initializer
))
585 error ("cannot capture %qE by reference", initializer
);
586 return error_mark_node
;
591 /* Capture by copy requires a complete type. */
592 type
= complete_type (type
);
593 if (!COMPLETE_TYPE_P (type
))
595 error ("capture by copy of incomplete type %qT", type
);
596 cxx_incomplete_type_inform (type
);
597 return error_mark_node
;
602 /* Add __ to the beginning of the field name so that user code
603 won't find the field with name lookup. We can't just leave the name
604 unset because template instantiation uses the name to find
605 instantiated fields. */
606 buf
= (char *) alloca (IDENTIFIER_LENGTH (id
) + 3);
607 buf
[1] = buf
[0] = '_';
608 memcpy (buf
+ 2, IDENTIFIER_POINTER (id
),
609 IDENTIFIER_LENGTH (id
) + 1);
610 name
= get_identifier (buf
);
612 /* If TREE_TYPE isn't set, we're still in the introducer, so check
614 if (!LAMBDA_EXPR_CLOSURE (lambda
))
616 if (IDENTIFIER_MARKED (name
))
618 pedwarn (input_location
, 0,
619 "already captured %qD in lambda expression", id
);
622 IDENTIFIER_MARKED (name
) = true;
626 type
= make_pack_expansion (type
);
628 /* Make member variable. */
629 member
= build_decl (input_location
, FIELD_DECL
, name
, type
);
630 DECL_VLA_CAPTURE_P (member
) = vla
;
632 if (!explicit_init_p
)
633 /* Normal captures are invisible to name lookup but uses are replaced
634 with references to the capture field; we implement this by only
635 really making them invisible in unevaluated context; see
636 qualify_lookup. For now, let's make explicitly initialized captures
638 DECL_NORMAL_CAPTURE_P (member
) = true;
640 if (id
== this_identifier
)
641 LAMBDA_EXPR_THIS_CAPTURE (lambda
) = member
;
643 /* Add it to the appropriate closure class if we've started it. */
644 if (current_class_type
645 && current_class_type
== LAMBDA_EXPR_CLOSURE (lambda
))
647 if (COMPLETE_TYPE_P (current_class_type
))
648 internal_error ("trying to capture %qD in instantiation of "
649 "generic lambda", id
);
650 finish_member_declaration (member
);
653 tree listmem
= member
;
656 listmem
= make_pack_expansion (member
);
657 initializer
= orig_init
;
659 LAMBDA_EXPR_CAPTURE_LIST (lambda
)
660 = tree_cons (listmem
, initializer
, LAMBDA_EXPR_CAPTURE_LIST (lambda
));
662 if (LAMBDA_EXPR_CLOSURE (lambda
))
663 return build_capture_proxy (member
, initializer
);
664 /* For explicit captures we haven't started the function yet, so we wait
665 and build the proxy from cp_parser_lambda_body. */
666 LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda
)) = true;
670 /* Register all the capture members on the list CAPTURES, which is the
671 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
674 register_capture_members (tree captures
)
676 if (captures
== NULL_TREE
)
679 register_capture_members (TREE_CHAIN (captures
));
681 tree field
= TREE_PURPOSE (captures
);
682 if (PACK_EXPANSION_P (field
))
683 field
= PACK_EXPANSION_PATTERN (field
);
685 /* We set this in add_capture to avoid duplicates. */
686 IDENTIFIER_MARKED (DECL_NAME (field
)) = false;
687 finish_member_declaration (field
);
690 /* Similar to add_capture, except this works on a stack of nested lambdas.
691 BY_REFERENCE_P in this case is derived from the default capture mode.
692 Returns the capture for the lambda at the bottom of the stack. */
695 add_default_capture (tree lambda_stack
, tree id
, tree initializer
)
697 bool this_capture_p
= (id
== this_identifier
);
699 tree var
= NULL_TREE
;
701 tree saved_class_type
= current_class_type
;
705 for (node
= lambda_stack
;
707 node
= TREE_CHAIN (node
))
709 tree lambda
= TREE_VALUE (node
);
711 current_class_type
= LAMBDA_EXPR_CLOSURE (lambda
);
712 if (DECL_PACK_P (initializer
))
713 initializer
= make_pack_expansion (initializer
);
714 var
= add_capture (lambda
,
719 || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
)
721 /*explicit_init_p=*/false);
722 initializer
= convert_from_reference (var
);
725 current_class_type
= saved_class_type
;
730 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
731 form of an INDIRECT_REF, possibly adding it through default
732 capturing, if ADD_CAPTURE_P is true. */
735 lambda_expr_this_capture (tree lambda
, bool add_capture_p
)
739 tree this_capture
= LAMBDA_EXPR_THIS_CAPTURE (lambda
);
741 /* In unevaluated context this isn't an odr-use, so don't capture. */
742 if (cp_unevaluated_operand
)
743 add_capture_p
= false;
745 /* Try to default capture 'this' if we can. */
748 tree lambda_stack
= NULL_TREE
;
749 tree init
= NULL_TREE
;
751 /* If we are in a lambda function, we can move out until we hit:
752 1. a non-lambda function or NSDMI,
753 2. a lambda function capturing 'this', or
754 3. a non-default capturing lambda function. */
755 for (tree tlambda
= lambda
; ;)
758 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda
) == CPLD_NONE
)
759 /* tlambda won't let us capture 'this'. */
763 lambda_stack
= tree_cons (NULL_TREE
,
767 tree closure
= LAMBDA_EXPR_CLOSURE (tlambda
);
768 tree containing_function
769 = decl_function_context (TYPE_NAME (closure
));
771 tree ex
= LAMBDA_EXPR_EXTRA_SCOPE (tlambda
);
772 if (ex
&& TREE_CODE (ex
) == FIELD_DECL
)
774 /* Lambda in an NSDMI. We don't have a function to look up
775 'this' in, but we can find (or rebuild) the fake one from
776 inject_this_parameter. */
777 if (!containing_function
&& !COMPLETE_TYPE_P (closure
))
778 /* If we're parsing a lambda in a non-local class,
779 we can find the fake 'this' in scope_chain. */
780 init
= scope_chain
->x_current_class_ptr
;
782 /* Otherwise it's either gone or buried in
783 function_context_stack, so make another. */
784 init
= build_this_parm (NULL_TREE
, DECL_CONTEXT (ex
),
787 (init
&& (TREE_TYPE (TREE_TYPE (init
))
788 == current_nonlambda_class_type ()));
792 if (containing_function
== NULL_TREE
)
793 /* We ran out of scopes; there's no 'this' to capture. */
796 if (!LAMBDA_FUNCTION_P (containing_function
))
798 /* We found a non-lambda function. */
799 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function
))
800 /* First parameter is 'this'. */
801 init
= DECL_ARGUMENTS (containing_function
);
806 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function
));
808 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda
))
810 /* An outer lambda has already captured 'this'. */
811 init
= LAMBDA_EXPR_THIS_CAPTURE (tlambda
);
819 this_capture
= add_default_capture (lambda_stack
,
820 /*id=*/this_identifier
,
827 if (cp_unevaluated_operand
)
828 result
= this_capture
;
829 else if (!this_capture
)
833 error ("%<this%> was not captured for this lambda function");
834 result
= error_mark_node
;
841 /* To make sure that current_class_ref is for the lambda. */
842 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref
))
843 == LAMBDA_EXPR_CLOSURE (lambda
));
845 result
= this_capture
;
847 /* If 'this' is captured, each use of 'this' is transformed into an
848 access to the corresponding unnamed data member of the closure
849 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
850 ensures that the transformed expression is an rvalue. ] */
851 result
= rvalue (result
);
857 /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
860 current_lambda_expr (void)
862 tree type
= current_class_type
;
863 while (type
&& !LAMBDA_TYPE_P (type
))
864 type
= decl_type_context (TYPE_NAME (type
));
866 return CLASSTYPE_LAMBDA_EXPR (type
);
871 /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
872 object. NULL otherwise.. */
875 resolvable_dummy_lambda (tree object
)
877 if (!is_dummy_object (object
))
880 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (object
));
881 gcc_assert (!TYPE_PTR_P (type
));
883 if (type
!= current_class_type
884 && current_class_type
885 && LAMBDA_TYPE_P (current_class_type
)
886 && lambda_function (current_class_type
)
887 && DERIVED_FROM_P (type
, nonlambda_method_basetype()))
888 return CLASSTYPE_LAMBDA_EXPR (current_class_type
);
893 /* We don't want to capture 'this' until we know we need it, i.e. after
894 overload resolution has chosen a non-static member function. At that
895 point we call this function to turn a dummy object into a use of the
899 maybe_resolve_dummy (tree object
, bool add_capture_p
)
901 if (tree lam
= resolvable_dummy_lambda (object
))
902 if (tree cap
= lambda_expr_this_capture (lam
, add_capture_p
))
903 if (cap
!= error_mark_node
)
904 object
= build_fold_indirect_ref (cap
);
909 /* When parsing a generic lambda containing an argument-dependent
910 member function call we defer overload resolution to instantiation
911 time. But we have to know now whether to capture this or not.
912 Do that if FNS contains any non-static fns.
913 The std doesn't anticipate this case, but I expect this to be the
914 outcome of discussion. */
917 maybe_generic_this_capture (tree object
, tree fns
)
919 if (tree lam
= resolvable_dummy_lambda (object
))
920 if (!LAMBDA_EXPR_THIS_CAPTURE (lam
))
922 /* We've not yet captured, so look at the function set of
924 if (BASELINK_P (fns
))
925 fns
= BASELINK_FUNCTIONS (fns
);
926 bool id_expr
= TREE_CODE (fns
) == TEMPLATE_ID_EXPR
;
928 fns
= TREE_OPERAND (fns
, 0);
930 for (lkp_iterator
iter (fns
); iter
; ++iter
)
931 if ((!id_expr
|| TREE_CODE (*iter
) == TEMPLATE_DECL
)
932 && DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter
))
934 /* Found a non-static member. Capture this. */
935 lambda_expr_this_capture (lam
, true);
941 /* Returns the innermost non-lambda function. */
944 current_nonlambda_function (void)
946 tree fn
= current_function_decl
;
947 while (fn
&& LAMBDA_FUNCTION_P (fn
))
948 fn
= decl_function_context (fn
);
952 /* Returns the method basetype of the innermost non-lambda function, including
953 a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
956 nonlambda_method_basetype (void)
958 if (!current_class_ref
)
961 tree type
= current_class_type
;
962 if (!type
|| !LAMBDA_TYPE_P (type
))
967 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
968 tree ex
= LAMBDA_EXPR_EXTRA_SCOPE (lam
);
969 if (ex
&& TREE_CODE (ex
) == FIELD_DECL
)
970 /* Lambda in an NSDMI. */
971 return DECL_CONTEXT (ex
);
973 tree fn
= TYPE_CONTEXT (type
);
974 if (!fn
|| TREE_CODE (fn
) != FUNCTION_DECL
975 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
976 /* No enclosing non-lambda method. */
978 if (!LAMBDA_FUNCTION_P (fn
))
979 /* Found an enclosing non-lambda method. */
980 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn
));
981 type
= DECL_CONTEXT (fn
);
985 /* Like current_scope, but looking through lambdas. */
988 current_nonlambda_scope (void)
990 tree scope
= current_scope ();
993 if (TREE_CODE (scope
) == FUNCTION_DECL
994 && LAMBDA_FUNCTION_P (scope
))
996 scope
= CP_TYPE_CONTEXT (DECL_CONTEXT (scope
));
999 else if (LAMBDA_TYPE_P (scope
))
1001 scope
= CP_TYPE_CONTEXT (scope
);
1009 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
1010 indicated FN and NARGS, but do not initialize the return type or any of the
1014 prepare_op_call (tree fn
, int nargs
)
1018 t
= build_vl_exp (CALL_EXPR
, nargs
+ 3);
1019 CALL_EXPR_FN (t
) = fn
;
1020 CALL_EXPR_STATIC_CHAIN (t
) = NULL
;
1025 /* Return true iff CALLOP is the op() for a generic lambda. */
1028 generic_lambda_fn_p (tree callop
)
1030 return (LAMBDA_FUNCTION_P (callop
)
1031 && DECL_TEMPLATE_INFO (callop
)
1032 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop
)));
1035 /* If the closure TYPE has a static op(), also add a conversion to function
1039 maybe_add_lambda_conv_op (tree type
)
1041 bool nested
= (cfun
!= NULL
);
1042 bool nested_def
= decl_function_context (TYPE_MAIN_DECL (type
));
1043 tree callop
= lambda_function (type
);
1044 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
1046 if (LAMBDA_EXPR_CAPTURE_LIST (lam
) != NULL_TREE
1047 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) != CPLD_NONE
)
1050 if (processing_template_decl
)
1053 bool const generic_lambda_p
= generic_lambda_fn_p (callop
);
1055 if (!generic_lambda_p
&& DECL_INITIAL (callop
) == NULL_TREE
)
1057 /* If the op() wasn't instantiated due to errors, give up. */
1058 gcc_assert (errorcount
|| sorrycount
);
1062 /* Non-template conversion operators are defined directly with build_call_a
1063 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1064 deferred and the CALL is built in-place. In the case of a deduced return
1065 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1066 the return type is also built in-place. The arguments of DECLTYPE_CALL in
1067 the return expression may differ in flags from those in the body CALL. In
1068 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1069 the body CALL, but not in DECLTYPE_CALL. */
1071 vec
<tree
, va_gc
> *direct_argvec
= 0;
1072 tree decltype_call
= 0, call
= 0;
1073 tree optype
= TREE_TYPE (callop
);
1074 tree fn_result
= TREE_TYPE (optype
);
1076 tree thisarg
= build_nop (TREE_TYPE (DECL_ARGUMENTS (callop
)),
1078 if (generic_lambda_p
)
1080 ++processing_template_decl
;
1082 /* Prepare the dependent member call for the static member function
1083 '_FUN' and, potentially, prepare another call to be used in a decltype
1084 return expression for a deduced return call op to allow for simple
1085 implementation of the conversion operator. */
1087 tree instance
= cp_build_fold_indirect_ref (thisarg
);
1088 tree objfn
= build_min (COMPONENT_REF
, NULL_TREE
,
1089 instance
, DECL_NAME (callop
), NULL_TREE
);
1090 int nargs
= list_length (DECL_ARGUMENTS (callop
)) - 1;
1092 call
= prepare_op_call (objfn
, nargs
);
1093 if (type_uses_auto (fn_result
))
1094 decltype_call
= prepare_op_call (objfn
, nargs
);
1098 direct_argvec
= make_tree_vector ();
1099 direct_argvec
->quick_push (thisarg
);
1102 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1103 declare the static member function "_FUN" below. For each arg append to
1104 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1105 call args (for the template case). If a parameter pack is found, expand
1106 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1108 tree fn_args
= NULL_TREE
;
1111 tree src
= DECL_CHAIN (DECL_ARGUMENTS (callop
));
1116 tree new_node
= copy_node (src
);
1119 fn_args
= tgt
= new_node
;
1122 TREE_CHAIN (tgt
) = new_node
;
1126 mark_exp_read (tgt
);
1128 if (generic_lambda_p
)
1130 /* Avoid capturing variables in this context. */
1131 ++cp_unevaluated_operand
;
1132 tree a
= forward_parm (tgt
);
1133 --cp_unevaluated_operand
;
1135 CALL_EXPR_ARG (call
, ix
) = a
;
1137 CALL_EXPR_ARG (decltype_call
, ix
) = unshare_expr (a
);
1139 if (PACK_EXPANSION_P (a
))
1140 /* Set this after unsharing so it's not in decltype_call. */
1141 PACK_EXPANSION_LOCAL_P (a
) = true;
1146 vec_safe_push (direct_argvec
, tgt
);
1148 src
= TREE_CHAIN (src
);
1152 if (generic_lambda_p
)
1156 fn_result
= finish_decltype_type
1157 (decltype_call
, /*id_expression_or_member_access_p=*/false,
1158 tf_warning_or_error
);
1162 call
= build_call_a (callop
,
1163 direct_argvec
->length (),
1164 direct_argvec
->address ());
1166 CALL_FROM_THUNK_P (call
) = 1;
1167 SET_EXPR_LOCATION (call
, UNKNOWN_LOCATION
);
1169 tree stattype
= build_function_type (fn_result
, FUNCTION_ARG_CHAIN (callop
));
1170 stattype
= (cp_build_type_attribute_variant
1171 (stattype
, TYPE_ATTRIBUTES (optype
)));
1172 if (flag_noexcept_type
1173 && TYPE_NOTHROW_P (TREE_TYPE (callop
)))
1174 stattype
= build_exception_variant (stattype
, noexcept_true_spec
);
1176 if (generic_lambda_p
)
1177 --processing_template_decl
;
1179 /* First build up the conversion op. */
1181 tree rettype
= build_pointer_type (stattype
);
1182 tree name
= make_conv_op_name (rettype
);
1183 tree thistype
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
1184 tree fntype
= build_method_type_directly (thistype
, rettype
, void_list_node
);
1185 tree convfn
= build_lang_decl (FUNCTION_DECL
, name
, fntype
);
1186 SET_DECL_LANGUAGE (convfn
, lang_cplusplus
);
1188 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (callop
);
1189 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
1190 grokclassfn (type
, fn
, NO_SPECIAL
);
1191 set_linkage_according_to_type (type
, fn
);
1192 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
1193 DECL_IN_AGGR_P (fn
) = 1;
1194 DECL_ARTIFICIAL (fn
) = 1;
1195 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1196 DECL_DECLARED_INLINE_P (fn
) = 1;
1197 DECL_ARGUMENTS (fn
) = build_this_parm (fn
, fntype
, TYPE_QUAL_CONST
);
1200 DECL_INTERFACE_KNOWN (fn
) = 1;
1202 if (generic_lambda_p
)
1203 fn
= add_inherited_template_parms (fn
, DECL_TI_TEMPLATE (callop
));
1205 add_method (type
, fn
, false);
1207 /* Generic thunk code fails for varargs; we'll complain in mark_used if
1208 the conversion op is used. */
1209 if (varargs_function_p (callop
))
1211 DECL_DELETED_FN (fn
) = 1;
1215 /* Now build up the thunk to be returned. */
1217 name
= get_identifier ("_FUN");
1218 tree statfn
= build_lang_decl (FUNCTION_DECL
, name
, stattype
);
1219 SET_DECL_LANGUAGE (statfn
, lang_cplusplus
);
1221 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (callop
);
1222 grokclassfn (type
, fn
, NO_SPECIAL
);
1223 set_linkage_according_to_type (type
, fn
);
1224 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
1225 DECL_IN_AGGR_P (fn
) = 1;
1226 DECL_ARTIFICIAL (fn
) = 1;
1227 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1228 DECL_DECLARED_INLINE_P (fn
) = 1;
1229 DECL_STATIC_FUNCTION_P (fn
) = 1;
1230 DECL_ARGUMENTS (fn
) = fn_args
;
1231 for (tree arg
= fn_args
; arg
; arg
= DECL_CHAIN (arg
))
1233 /* Avoid duplicate -Wshadow warnings. */
1234 DECL_NAME (arg
) = NULL_TREE
;
1235 DECL_CONTEXT (arg
) = fn
;
1238 DECL_INTERFACE_KNOWN (fn
) = 1;
1240 if (generic_lambda_p
)
1241 fn
= add_inherited_template_parms (fn
, DECL_TI_TEMPLATE (callop
));
1243 if (flag_sanitize
& SANITIZE_NULL
)
1244 /* Don't UBsan this function; we're deliberately calling op() with a null
1246 add_no_sanitize_value (fn
, SANITIZE_UNDEFINED
);
1248 add_method (type
, fn
, false);
1251 push_function_context ();
1253 /* Still increment function_depth so that we don't GC in the
1254 middle of an expression. */
1257 /* Generate the body of the thunk. */
1259 start_preparsed_function (statfn
, NULL_TREE
,
1260 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1261 if (DECL_ONE_ONLY (statfn
))
1263 /* Put the thunk in the same comdat group as the call op. */
1264 cgraph_node::get_create (statfn
)->add_to_same_comdat_group
1265 (cgraph_node::get_create (callop
));
1267 tree body
= begin_function_body ();
1268 tree compound_stmt
= begin_compound_stmt (0);
1269 if (!generic_lambda_p
)
1271 set_flags_from_callee (call
);
1272 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call
)))
1273 call
= build_cplus_new (TREE_TYPE (call
), call
, tf_warning_or_error
);
1275 call
= convert_from_reference (call
);
1276 finish_return_stmt (call
);
1278 finish_compound_stmt (compound_stmt
);
1279 finish_function_body (body
);
1281 fn
= finish_function (/*inline_p=*/true);
1282 if (!generic_lambda_p
)
1283 expand_or_defer_fn (fn
);
1285 /* Generate the body of the conversion op. */
1287 start_preparsed_function (convfn
, NULL_TREE
,
1288 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1289 body
= begin_function_body ();
1290 compound_stmt
= begin_compound_stmt (0);
1292 /* decl_needed_p needs to see that it's used. */
1293 TREE_USED (statfn
) = 1;
1294 finish_return_stmt (decay_conversion (statfn
, tf_warning_or_error
));
1296 finish_compound_stmt (compound_stmt
);
1297 finish_function_body (body
);
1299 fn
= finish_function (/*inline_p=*/true);
1300 if (!generic_lambda_p
)
1301 expand_or_defer_fn (fn
);
1304 pop_function_context ();
1309 /* True if FN is the static function "_FUN" that gets returned from the lambda
1310 conversion operator. */
1313 lambda_static_thunk_p (tree fn
)
1315 return (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1316 && DECL_ARTIFICIAL (fn
)
1317 && DECL_STATIC_FUNCTION_P (fn
)
1318 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn
)));
1321 /* Returns true iff VAL is a lambda-related declaration which should
1322 be ignored by unqualified lookup. */
1325 is_lambda_ignored_entity (tree val
)
1327 /* Look past normal capture proxies. */
1328 if (is_normal_capture_proxy (val
))
1331 /* Always ignore lambda fields, their names are only for debugging. */
1332 if (TREE_CODE (val
) == FIELD_DECL
1333 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val
)))
1336 /* None of the lookups that use qualify_lookup want the op() from the
1337 lambda; they want the one from the enclosing class. */
1338 if (TREE_CODE (val
) == FUNCTION_DECL
&& LAMBDA_FUNCTION_P (val
))
1344 /* Lambdas that appear in variable initializer or default argument scope
1345 get that in their mangling, so we need to record it. We might as well
1346 use the count for function and namespace scopes as well. */
1347 static GTY(()) tree lambda_scope
;
1348 static GTY(()) int lambda_count
;
1349 struct GTY(()) tree_int
1354 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
1357 start_lambda_scope (tree decl
)
1361 /* Once we're inside a function, we ignore variable scope and just push
1362 the function again so that popping works properly. */
1363 if (current_function_decl
&& TREE_CODE (decl
) == VAR_DECL
)
1364 decl
= current_function_decl
;
1365 ti
.t
= lambda_scope
;
1366 ti
.i
= lambda_count
;
1367 vec_safe_push (lambda_scope_stack
, ti
);
1368 if (lambda_scope
!= decl
)
1370 /* Don't reset the count if we're still in the same function. */
1371 lambda_scope
= decl
;
1377 record_lambda_scope (tree lambda
)
1379 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
1380 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
1383 /* This lambda is an instantiation of a lambda in a template default argument
1384 that got no LAMBDA_EXPR_EXTRA_SCOPE, so this shouldn't either. But we do
1385 need to use and increment the global count to avoid collisions. */
1388 record_null_lambda_scope (tree lambda
)
1390 if (vec_safe_is_empty (lambda_scope_stack
))
1391 record_lambda_scope (lambda
);
1394 tree_int
*p
= lambda_scope_stack
->begin();
1395 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = p
->t
;
1396 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = p
->i
++;
1398 gcc_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda
) == NULL_TREE
);
1402 finish_lambda_scope (void)
1404 tree_int
*p
= &lambda_scope_stack
->last ();
1405 if (lambda_scope
!= p
->t
)
1407 lambda_scope
= p
->t
;
1408 lambda_count
= p
->i
;
1410 lambda_scope_stack
->pop ();
1414 start_lambda_function (tree fco
, tree lambda_expr
)
1416 /* Let the front end know that we are going to be defining this
1418 start_preparsed_function (fco
,
1420 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1422 tree body
= begin_function_body ();
1424 /* Push the proxies for any explicit captures. */
1425 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
1426 cap
= TREE_CHAIN (cap
))
1427 build_capture_proxy (TREE_PURPOSE (cap
), TREE_VALUE (cap
));
1432 /* Subroutine of prune_lambda_captures: CAP is a node in
1433 LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
1434 might optimize away the capture, or NULL_TREE if there is no such
1438 var_to_maybe_prune (tree cap
)
1440 if (LAMBDA_CAPTURE_EXPLICIT_P (cap
))
1441 /* Don't prune explicit captures. */
1444 tree mem
= TREE_PURPOSE (cap
);
1445 if (!DECL_P (mem
) || !DECL_NORMAL_CAPTURE_P (mem
))
1446 /* Packs and init-captures aren't captures of constant vars. */
1449 tree init
= TREE_VALUE (cap
);
1450 if (is_normal_capture_proxy (init
))
1451 init
= DECL_CAPTURED_VARIABLE (init
);
1452 if (decl_constant_var_p (init
))
1458 /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
1459 for constant variables are actually used in the lambda body.
1461 There will always be a DECL_EXPR for the capture proxy; remember it when we
1462 see it, but replace it with any other use. */
1465 mark_const_cap_r (tree
*t
, int *walk_subtrees
, void *data
)
1467 hash_map
<tree
,tree
*> &const_vars
= *(hash_map
<tree
,tree
*>*)data
;
1469 tree var
= NULL_TREE
;
1470 if (TREE_CODE (*t
) == DECL_EXPR
)
1472 tree decl
= DECL_EXPR_DECL (*t
);
1473 if (is_constant_capture_proxy (decl
))
1474 var
= DECL_CAPTURED_VARIABLE (decl
);
1477 else if (is_constant_capture_proxy (*t
))
1478 var
= DECL_CAPTURED_VARIABLE (*t
);
1482 tree
*&slot
= const_vars
.get_or_insert (var
);
1483 if (!slot
|| VAR_P (*t
))
1490 /* We're at the end of processing a lambda; go back and remove any captures of
1491 constant variables for which we've folded away all uses. */
1494 prune_lambda_captures (tree body
)
1496 tree lam
= current_lambda_expr ();
1497 if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam
))
1498 /* No uses were optimized away. */
1500 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) == CPLD_NONE
)
1501 /* No default captures, and we don't prune explicit captures. */
1504 hash_map
<tree
,tree
*> const_vars
;
1506 cp_walk_tree_without_duplicates (&body
, mark_const_cap_r
, &const_vars
);
1508 tree
*fieldp
= &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam
));
1509 for (tree
*capp
= &LAMBDA_EXPR_CAPTURE_LIST (lam
); *capp
; )
1512 if (tree var
= var_to_maybe_prune (cap
))
1514 tree
*use
= *const_vars
.get (var
);
1515 if (TREE_CODE (*use
) == DECL_EXPR
)
1517 /* All uses of this capture were folded away, leaving only the
1518 proxy declaration. */
1520 /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
1521 *capp
= TREE_CHAIN (cap
);
1523 /* And out of TYPE_FIELDS. */
1524 tree field
= TREE_PURPOSE (cap
);
1525 while (*fieldp
!= field
)
1526 fieldp
= &DECL_CHAIN (*fieldp
);
1527 *fieldp
= DECL_CHAIN (*fieldp
);
1529 /* And remove the capture proxy declaration. */
1535 capp
= &TREE_CHAIN (cap
);
1540 finish_lambda_function (tree body
)
1542 finish_function_body (body
);
1544 prune_lambda_captures (body
);
1546 /* Finish the function and generate code for it if necessary. */
1547 tree fn
= finish_function (/*inline_p=*/true);
1549 /* Only expand if the call op is not a template. */
1550 if (!DECL_TEMPLATE_INFO (fn
))
1551 expand_or_defer_fn (fn
);
1554 #include "gt-cp-lambda.h"