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-2021 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"
35 /* Constructor for a lambda expression. */
38 build_lambda_expr (void)
40 tree lambda
= make_node (LAMBDA_EXPR
);
41 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
) = CPLD_NONE
;
42 LAMBDA_EXPR_CAPTURE_LIST (lambda
) = NULL_TREE
;
43 LAMBDA_EXPR_THIS_CAPTURE (lambda
) = NULL_TREE
;
44 LAMBDA_EXPR_REGEN_INFO (lambda
) = NULL_TREE
;
45 LAMBDA_EXPR_PENDING_PROXIES (lambda
) = NULL
;
46 LAMBDA_EXPR_MUTABLE_P (lambda
) = false;
50 /* Create the closure object for a LAMBDA_EXPR. */
53 build_lambda_object (tree lambda_expr
)
55 /* Build aggregate constructor call.
56 - cp_parser_braced_list
57 - cp_parser_functional_cast */
58 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
59 tree node
, expr
, type
;
61 if (processing_template_decl
|| lambda_expr
== error_mark_node
)
64 /* Make sure any error messages refer to the lambda-introducer. */
65 location_t loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
66 iloc_sentinel
il (loc
);
68 for (node
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
70 node
= TREE_CHAIN (node
))
72 tree field
= TREE_PURPOSE (node
);
73 tree val
= TREE_VALUE (node
);
75 if (field
== error_mark_node
)
77 expr
= error_mark_node
;
81 if (TREE_CODE (val
) == TREE_LIST
)
82 val
= build_x_compound_expr_from_list (val
, ELK_INIT
,
88 /* Mere mortals can't copy arrays with aggregate initialization, so
89 do some magic to make it work here. */
90 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
)
91 val
= build_array_copy (val
);
92 else if (DECL_NORMAL_CAPTURE_P (field
)
93 && !DECL_VLA_CAPTURE_P (field
)
94 && !TYPE_REF_P (TREE_TYPE (field
)))
96 /* "the entities that are captured by copy are used to
97 direct-initialize each corresponding non-static data
98 member of the resulting closure object."
100 There's normally no way to express direct-initialization
101 from an element of a CONSTRUCTOR, so we build up a special
102 TARGET_EXPR to bypass the usual copy-initialization. */
103 val
= force_rvalue (val
, tf_warning_or_error
);
104 if (TREE_CODE (val
) == TARGET_EXPR
)
105 TARGET_EXPR_DIRECT_INIT_P (val
) = true;
108 CONSTRUCTOR_APPEND_ELT (elts
, DECL_NAME (field
), val
);
111 expr
= build_constructor (init_list_type_node
, elts
);
112 CONSTRUCTOR_IS_DIRECT_INIT (expr
) = 1;
114 /* N2927: "[The closure] class type is not an aggregate."
115 But we briefly treat it as an aggregate to make this simpler. */
116 type
= LAMBDA_EXPR_CLOSURE (lambda_expr
);
117 CLASSTYPE_NON_AGGREGATE (type
) = 0;
118 expr
= finish_compound_literal (type
, expr
, tf_warning_or_error
);
119 protected_set_expr_location (expr
, loc
);
120 CLASSTYPE_NON_AGGREGATE (type
) = 1;
126 /* Return an initialized RECORD_TYPE for LAMBDA.
127 LAMBDA must have its explicit captures already. */
130 begin_lambda_type (tree lambda
)
132 /* Lambda names are nearly but not quite anonymous. */
133 tree name
= make_anon_name ();
134 IDENTIFIER_LAMBDA_P (name
) = true;
136 /* Create the new RECORD_TYPE for this lambda. */
137 tree type
= xref_tag (/*tag_code=*/record_type
, name
);
138 if (type
== error_mark_node
)
139 return error_mark_node
;
141 /* Designate it as a struct so that we can use aggregate initialization. */
142 CLASSTYPE_DECLARED_CLASS (type
) = false;
144 /* Cross-reference the expression and the type. */
145 LAMBDA_EXPR_CLOSURE (lambda
) = type
;
146 CLASSTYPE_LAMBDA_EXPR (type
) = lambda
;
148 /* In C++17, assume the closure is literal; we'll clear the flag later if
150 if (cxx_dialect
>= cxx17
)
151 CLASSTYPE_LITERAL_P (type
) = true;
153 /* Clear base types. */
154 xref_basetypes (type
, /*bases=*/NULL_TREE
);
156 /* Start the class. */
157 type
= begin_class_definition (type
);
162 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
166 lambda_function (tree lambda
)
169 if (TREE_CODE (lambda
) == LAMBDA_EXPR
)
170 type
= LAMBDA_EXPR_CLOSURE (lambda
);
173 gcc_assert (LAMBDA_TYPE_P (type
));
174 /* Don't let debug_tree cause instantiation. */
175 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
176 && !COMPLETE_OR_OPEN_TYPE_P (type
))
178 lambda
= lookup_member (type
, call_op_identifier
,
179 /*protect=*/0, /*want_type=*/false,
180 tf_warning_or_error
);
182 lambda
= STRIP_TEMPLATE (get_first_fn (lambda
));
186 /* Returns the type to use for the FIELD_DECL corresponding to the
187 capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
188 C++14 init capture, and BY_REFERENCE_P indicates whether we're
189 capturing by reference. */
192 lambda_capture_field_type (tree expr
, bool explicit_init_p
,
196 bool is_this
= is_this_parameter (tree_strip_nop_conversions (expr
));
199 type
= TREE_TYPE (expr
);
200 else if (explicit_init_p
)
202 tree auto_node
= make_auto ();
206 /* Add the reference now, so deduction doesn't lose
207 outermost CV qualifiers of EXPR. */
208 type
= build_reference_type (type
);
209 if (uses_parameter_packs (expr
))
210 /* Stick with 'auto' even if the type could be deduced. */;
212 type
= do_auto_deduction (type
, expr
, auto_node
);
214 else if (type_dependent_expression_p (expr
))
216 type
= cxx_make_type (DECLTYPE_TYPE
);
217 DECLTYPE_TYPE_EXPR (type
) = expr
;
218 DECLTYPE_FOR_LAMBDA_CAPTURE (type
) = true;
219 DECLTYPE_FOR_REF_CAPTURE (type
) = by_reference_p
;
220 SET_TYPE_STRUCTURAL_EQUALITY (type
);
224 if (!by_reference_p
&& is_capture_proxy (expr
))
226 /* When capturing by-value another capture proxy from an enclosing
227 lambda, consider the type of the corresponding field instead,
228 as the proxy may be additionally const-qualifed if the enclosing
229 lambda is non-mutable (PR94376). */
230 gcc_assert (TREE_CODE (DECL_VALUE_EXPR (expr
)) == COMPONENT_REF
);
231 expr
= TREE_OPERAND (DECL_VALUE_EXPR (expr
), 1);
234 type
= non_reference (unlowered_expr_type (expr
));
236 if (by_reference_p
|| TREE_CODE (type
) == FUNCTION_TYPE
)
237 type
= build_reference_type (type
);
243 /* Returns true iff DECL is a lambda capture proxy variable created by
244 build_capture_proxy. */
247 is_capture_proxy (tree decl
)
250 && DECL_HAS_VALUE_EXPR_P (decl
)
251 && !DECL_ANON_UNION_VAR_P (decl
)
252 && !DECL_DECOMPOSITION_P (decl
)
253 && !DECL_FNAME_P (decl
)
254 && !(DECL_ARTIFICIAL (decl
)
255 && DECL_LANG_SPECIFIC (decl
)
256 && DECL_OMP_PRIVATIZED_MEMBER (decl
))
257 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl
)));
260 /* Returns true iff DECL is a capture proxy for a normal capture
261 (i.e. without explicit initializer). */
264 is_normal_capture_proxy (tree decl
)
266 if (!is_capture_proxy (decl
))
267 /* It's not a capture proxy. */
270 return (DECL_LANG_SPECIFIC (decl
)
271 && DECL_CAPTURED_VARIABLE (decl
));
274 /* Returns true iff DECL is a capture proxy for a normal capture
275 of a constant variable. */
278 is_constant_capture_proxy (tree decl
)
280 if (is_normal_capture_proxy (decl
))
281 return decl_constant_var_p (DECL_CAPTURED_VARIABLE (decl
));
285 /* VAR is a capture proxy created by build_capture_proxy; add it to the
286 current function, which is the operator() for the appropriate lambda. */
289 insert_capture_proxy (tree var
)
291 if (is_normal_capture_proxy (var
))
293 tree cap
= DECL_CAPTURED_VARIABLE (var
);
296 gcc_assert (!is_normal_capture_proxy (cap
));
297 tree old
= retrieve_local_specialization (cap
);
299 gcc_assert (DECL_CONTEXT (old
) != DECL_CONTEXT (var
));
301 register_local_specialization (var
, cap
);
304 /* Put the capture proxy in the extra body block so that it won't clash
305 with a later local variable. */
306 pushdecl_outermost_localscope (var
);
308 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
309 var
= build_stmt (DECL_SOURCE_LOCATION (var
), DECL_EXPR
, var
);
310 tree stmt_list
= (*stmt_list_stack
)[1];
311 gcc_assert (stmt_list
);
312 append_to_statement_list_force (var
, &stmt_list
);
315 /* We've just finished processing a lambda; if the containing scope is also
316 a lambda, insert any capture proxies that were created while processing
317 the nested lambda. */
320 insert_pending_capture_proxies (void)
323 vec
<tree
, va_gc
> *proxies
;
326 if (!current_function_decl
|| !LAMBDA_FUNCTION_P (current_function_decl
))
329 lam
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl
));
330 proxies
= LAMBDA_EXPR_PENDING_PROXIES (lam
);
331 for (i
= 0; i
< vec_safe_length (proxies
); ++i
)
333 tree var
= (*proxies
)[i
];
334 insert_capture_proxy (var
);
336 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam
));
337 LAMBDA_EXPR_PENDING_PROXIES (lam
) = NULL
;
340 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
341 return the type we want the proxy to have: the type of the field itself,
342 with added const-qualification if the lambda isn't mutable and the
343 capture is by value. */
346 lambda_proxy_type (tree ref
)
349 if (ref
== error_mark_node
)
350 return error_mark_node
;
351 if (REFERENCE_REF_P (ref
))
352 ref
= TREE_OPERAND (ref
, 0);
353 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
354 type
= TREE_TYPE (ref
);
355 if (!type
|| WILDCARD_TYPE_P (non_reference (type
)))
357 type
= cxx_make_type (DECLTYPE_TYPE
);
358 DECLTYPE_TYPE_EXPR (type
) = ref
;
359 DECLTYPE_FOR_LAMBDA_PROXY (type
) = true;
360 SET_TYPE_STRUCTURAL_EQUALITY (type
);
362 if (DECL_PACK_P (TREE_OPERAND (ref
, 1)))
363 type
= make_pack_expansion (type
);
367 /* MEMBER is a capture field in a lambda closure class. Now that we're
368 inside the operator(), build a placeholder var for future lookups and
372 build_capture_proxy (tree member
, tree init
)
374 tree var
, object
, fn
, closure
, name
, lam
, type
;
376 if (PACK_EXPANSION_P (member
))
377 member
= PACK_EXPANSION_PATTERN (member
);
379 closure
= DECL_CONTEXT (member
);
380 fn
= lambda_function (closure
);
381 lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
383 /* The proxy variable forwards to the capture field. */
384 object
= build_fold_indirect_ref (DECL_ARGUMENTS (fn
));
385 object
= finish_non_static_data_member (member
, object
, NULL_TREE
);
386 if (REFERENCE_REF_P (object
))
387 object
= TREE_OPERAND (object
, 0);
389 /* Remove the __ inserted by add_capture. */
390 name
= get_identifier (IDENTIFIER_POINTER (DECL_NAME (member
)) + 2);
392 type
= lambda_proxy_type (object
);
394 if (name
== this_identifier
&& !INDIRECT_TYPE_P (type
))
396 type
= build_pointer_type (type
);
397 type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
398 object
= build_fold_addr_expr_with_type (object
, type
);
401 if (DECL_VLA_CAPTURE_P (member
))
403 /* Rebuild the VLA type from the pointer and maxindex. */
404 tree field
= next_initializable_field (TYPE_FIELDS (type
));
405 tree ptr
= build_simple_component_ref (object
, field
);
406 field
= next_initializable_field (DECL_CHAIN (field
));
407 tree max
= build_simple_component_ref (object
, field
);
408 type
= build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr
)),
409 build_index_type (max
));
410 type
= build_reference_type (type
);
411 object
= convert (type
, ptr
);
414 complete_type (type
);
416 var
= build_decl (input_location
, VAR_DECL
, name
, type
);
417 SET_DECL_VALUE_EXPR (var
, object
);
418 DECL_HAS_VALUE_EXPR_P (var
) = 1;
419 DECL_ARTIFICIAL (var
) = 1;
421 DECL_CONTEXT (var
) = fn
;
423 if (DECL_NORMAL_CAPTURE_P (member
))
425 if (DECL_VLA_CAPTURE_P (member
))
427 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
428 init
= TREE_OPERAND (init
, 0); // Strip ADDR_EXPR.
429 init
= TREE_OPERAND (init
, 0); // Strip ARRAY_REF.
433 if (PACK_EXPANSION_P (init
))
434 init
= PACK_EXPANSION_PATTERN (init
);
437 if (INDIRECT_REF_P (init
))
438 init
= TREE_OPERAND (init
, 0);
441 gcc_assert (VAR_P (init
) || TREE_CODE (init
) == PARM_DECL
);
442 while (is_normal_capture_proxy (init
))
443 init
= DECL_CAPTURED_VARIABLE (init
);
444 retrofit_lang_decl (var
);
445 DECL_CAPTURED_VARIABLE (var
) = init
;
448 if (name
== this_identifier
)
450 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam
) == member
);
451 LAMBDA_EXPR_THIS_CAPTURE (lam
) = var
;
454 if (fn
== current_function_decl
)
455 insert_capture_proxy (var
);
457 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam
), var
);
462 static GTY(()) tree ptr_id
;
463 static GTY(()) tree max_id
;
465 /* Return a struct containing a pointer and a length for lambda capture of
466 an array of runtime length. */
469 vla_capture_type (tree array_type
)
471 tree type
= xref_tag (record_type
, make_anon_name ());
472 xref_basetypes (type
, NULL_TREE
);
473 type
= begin_class_definition (type
);
476 ptr_id
= get_identifier ("ptr");
477 max_id
= get_identifier ("max");
479 tree ptrtype
= build_pointer_type (TREE_TYPE (array_type
));
480 tree field
= build_decl (input_location
, FIELD_DECL
, ptr_id
, ptrtype
);
481 finish_member_declaration (field
);
482 field
= build_decl (input_location
, FIELD_DECL
, max_id
, sizetype
);
483 finish_member_declaration (field
);
484 return finish_struct (type
, NULL_TREE
);
487 /* From an ID and INITIALIZER, create a capture (by reference if
488 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
489 and return it. If ID is `this', BY_REFERENCE_P says whether
490 `*this' is captured by reference. */
493 add_capture (tree lambda
, tree id
, tree orig_init
, bool by_reference_p
,
494 bool explicit_init_p
)
497 tree type
, member
, name
;
499 bool variadic
= false;
500 tree initializer
= orig_init
;
502 if (PACK_EXPANSION_P (initializer
))
504 initializer
= PACK_EXPANSION_PATTERN (initializer
);
508 if (TREE_CODE (initializer
) == TREE_LIST
509 /* A pack expansion might end up with multiple elements. */
510 && !PACK_EXPANSION_P (TREE_VALUE (initializer
)))
511 initializer
= build_x_compound_expr_from_list (initializer
, ELK_INIT
,
512 tf_warning_or_error
);
513 type
= TREE_TYPE (initializer
);
514 if (type
== error_mark_node
)
515 return error_mark_node
;
517 if (!dependent_type_p (type
) && array_of_runtime_bound_p (type
))
521 error ("array of runtime bound cannot be captured by copy, "
522 "only by reference");
524 /* For a VLA, we capture the address of the first element and the
525 maximum index, and then reconstruct the VLA for the proxy. */
526 tree elt
= cp_build_array_ref (input_location
, initializer
,
527 integer_zero_node
, tf_warning_or_error
);
528 initializer
= build_constructor_va (init_list_type_node
, 2,
529 NULL_TREE
, build_address (elt
),
530 NULL_TREE
, array_type_nelts (type
));
531 type
= vla_capture_type (type
);
533 else if (!dependent_type_p (type
)
534 && variably_modified_type_p (type
, NULL_TREE
))
536 sorry ("capture of variably-modified type %qT that is not an N3639 array "
537 "of runtime bound", type
);
538 if (TREE_CODE (type
) == ARRAY_TYPE
539 && variably_modified_type_p (TREE_TYPE (type
), NULL_TREE
))
540 inform (input_location
, "because the array element type %qT has "
541 "variable size", TREE_TYPE (type
));
542 return error_mark_node
;
546 type
= lambda_capture_field_type (initializer
, explicit_init_p
,
548 if (type
== error_mark_node
)
549 return error_mark_node
;
551 if (id
== this_identifier
&& !by_reference_p
)
553 gcc_assert (INDIRECT_TYPE_P (type
));
554 type
= TREE_TYPE (type
);
555 initializer
= cp_build_fold_indirect_ref (initializer
);
558 if (dependent_type_p (type
))
560 else if (id
!= this_identifier
&& by_reference_p
)
562 if (!lvalue_p (initializer
))
564 error ("cannot capture %qE by reference", initializer
);
565 return error_mark_node
;
570 /* Capture by copy requires a complete type. */
571 type
= complete_type (type
);
572 if (!COMPLETE_TYPE_P (type
))
574 error ("capture by copy of incomplete type %qT", type
);
575 cxx_incomplete_type_inform (type
);
576 return error_mark_node
;
578 else if (!verify_type_context (input_location
,
579 TCTX_CAPTURE_BY_COPY
, type
))
580 return error_mark_node
;
584 /* Add __ to the beginning of the field name so that user code
585 won't find the field with name lookup. We can't just leave the name
586 unset because template instantiation uses the name to find
587 instantiated fields. */
588 buf
= (char *) alloca (IDENTIFIER_LENGTH (id
) + 3);
589 buf
[1] = buf
[0] = '_';
590 memcpy (buf
+ 2, IDENTIFIER_POINTER (id
),
591 IDENTIFIER_LENGTH (id
) + 1);
592 name
= get_identifier (buf
);
596 type
= make_pack_expansion (type
);
598 /* With an explicit initializer 'type' is auto, which isn't really a
599 parameter pack in this context. We will want as many fields as we
600 have elements in the expansion of the initializer, so use its packs
603 PACK_EXPANSION_PARAMETER_PACKS (type
)
604 = uses_parameter_packs (initializer
);
605 PACK_EXPANSION_AUTO_P (type
) = true;
609 /* Make member variable. */
610 member
= build_decl (input_location
, FIELD_DECL
, name
, type
);
611 DECL_VLA_CAPTURE_P (member
) = vla
;
613 if (!explicit_init_p
)
614 /* Normal captures are invisible to name lookup but uses are replaced
615 with references to the capture field; we implement this by only
616 really making them invisible in unevaluated context; see
617 qualify_lookup. For now, let's make explicitly initialized captures
619 DECL_NORMAL_CAPTURE_P (member
) = true;
621 if (id
== this_identifier
)
622 LAMBDA_EXPR_THIS_CAPTURE (lambda
) = member
;
624 /* Add it to the appropriate closure class if we've started it. */
625 if (current_class_type
626 && current_class_type
== LAMBDA_EXPR_CLOSURE (lambda
))
628 if (COMPLETE_TYPE_P (current_class_type
))
629 internal_error ("trying to capture %qD in instantiation of "
630 "generic lambda", id
);
631 finish_member_declaration (member
);
634 tree listmem
= member
;
637 listmem
= make_pack_expansion (member
);
638 initializer
= orig_init
;
640 LAMBDA_EXPR_CAPTURE_LIST (lambda
)
641 = tree_cons (listmem
, initializer
, LAMBDA_EXPR_CAPTURE_LIST (lambda
));
643 if (LAMBDA_EXPR_CLOSURE (lambda
))
644 return build_capture_proxy (member
, initializer
);
645 /* For explicit captures we haven't started the function yet, so we wait
646 and build the proxy from cp_parser_lambda_body. */
647 LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda
)) = true;
651 /* Register all the capture members on the list CAPTURES, which is the
652 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
655 register_capture_members (tree captures
)
657 if (captures
== NULL_TREE
)
660 register_capture_members (TREE_CHAIN (captures
));
662 tree field
= TREE_PURPOSE (captures
);
663 if (PACK_EXPANSION_P (field
))
664 field
= PACK_EXPANSION_PATTERN (field
);
666 finish_member_declaration (field
);
669 /* Similar to add_capture, except this works on a stack of nested lambdas.
670 BY_REFERENCE_P in this case is derived from the default capture mode.
671 Returns the capture for the lambda at the bottom of the stack. */
674 add_default_capture (tree lambda_stack
, tree id
, tree initializer
)
676 bool this_capture_p
= (id
== this_identifier
);
677 tree var
= NULL_TREE
;
678 tree saved_class_type
= current_class_type
;
680 for (tree node
= lambda_stack
;
682 node
= TREE_CHAIN (node
))
684 tree lambda
= TREE_VALUE (node
);
686 current_class_type
= LAMBDA_EXPR_CLOSURE (lambda
);
687 if (DECL_PACK_P (initializer
))
688 initializer
= make_pack_expansion (initializer
);
689 var
= add_capture (lambda
,
694 || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
)
696 /*explicit_init_p=*/false);
697 initializer
= convert_from_reference (var
);
699 /* Warn about deprecated implicit capture of this via [=]. */
700 if (cxx_dialect
>= cxx20
702 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
) == CPLD_COPY
)
704 if (warning_at (LAMBDA_EXPR_LOCATION (lambda
), OPT_Wdeprecated
,
705 "implicit capture of %qE via %<[=]%> is deprecated "
706 "in C++20", this_identifier
))
707 inform (LAMBDA_EXPR_LOCATION (lambda
), "add explicit %<this%> or "
708 "%<*this%> capture");
712 current_class_type
= saved_class_type
;
717 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
718 form of an INDIRECT_REF, possibly adding it through default
719 capturing, if ADD_CAPTURE_P is nonzero. If ADD_CAPTURE_P is negative,
720 try to capture but don't complain if we can't. */
723 lambda_expr_this_capture (tree lambda
, int add_capture_p
)
727 tree this_capture
= LAMBDA_EXPR_THIS_CAPTURE (lambda
);
729 /* In unevaluated context this isn't an odr-use, so don't capture. */
730 if (cp_unevaluated_operand
)
731 add_capture_p
= false;
733 /* Try to default capture 'this' if we can. */
736 tree lambda_stack
= NULL_TREE
;
737 tree init
= NULL_TREE
;
739 /* If we are in a lambda function, we can move out until we hit:
740 1. a non-lambda function or NSDMI,
741 2. a lambda function capturing 'this', or
742 3. a non-default capturing lambda function. */
743 for (tree tlambda
= lambda
; ;)
746 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda
) == CPLD_NONE
)
747 /* tlambda won't let us capture 'this'. */
751 lambda_stack
= tree_cons (NULL_TREE
,
755 tree closure
= LAMBDA_EXPR_CLOSURE (tlambda
);
756 tree containing_function
757 = decl_function_context (TYPE_NAME (closure
));
759 tree ex
= LAMBDA_EXPR_EXTRA_SCOPE (tlambda
);
760 if (ex
&& TREE_CODE (ex
) == FIELD_DECL
)
762 /* Lambda in an NSDMI. We don't have a function to look up
763 'this' in, but we can find (or rebuild) the fake one from
764 inject_this_parameter. */
765 if (!containing_function
&& !COMPLETE_TYPE_P (closure
))
766 /* If we're parsing a lambda in a non-local class,
767 we can find the fake 'this' in scope_chain. */
768 init
= scope_chain
->x_current_class_ptr
;
770 /* Otherwise it's either gone or buried in
771 function_context_stack, so make another. */
772 init
= build_this_parm (NULL_TREE
, DECL_CONTEXT (ex
),
775 (init
&& (TREE_TYPE (TREE_TYPE (init
))
776 == current_nonlambda_class_type ()));
780 if (containing_function
== NULL_TREE
)
781 /* We ran out of scopes; there's no 'this' to capture. */
784 if (!LAMBDA_FUNCTION_P (containing_function
))
786 /* We found a non-lambda function. */
787 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function
))
788 /* First parameter is 'this'. */
789 init
= DECL_ARGUMENTS (containing_function
);
794 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function
));
796 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda
))
798 /* An outer lambda has already captured 'this'. */
799 init
= LAMBDA_EXPR_THIS_CAPTURE (tlambda
);
807 this_capture
= add_default_capture (lambda_stack
,
808 /*id=*/this_identifier
,
815 if (cp_unevaluated_operand
)
816 result
= this_capture
;
817 else if (!this_capture
)
819 if (add_capture_p
== 1)
821 error ("%<this%> was not captured for this lambda function");
822 result
= error_mark_node
;
829 /* To make sure that current_class_ref is for the lambda. */
830 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref
))
831 == LAMBDA_EXPR_CLOSURE (lambda
));
833 result
= this_capture
;
835 /* If 'this' is captured, each use of 'this' is transformed into an
836 access to the corresponding unnamed data member of the closure
837 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
838 ensures that the transformed expression is an rvalue. ] */
839 result
= rvalue (result
);
845 /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
848 current_lambda_expr (void)
850 tree type
= current_class_type
;
851 while (type
&& !LAMBDA_TYPE_P (type
))
852 type
= decl_type_context (TYPE_NAME (type
));
854 return CLASSTYPE_LAMBDA_EXPR (type
);
859 /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
860 object. NULL otherwise.. */
863 resolvable_dummy_lambda (tree object
)
865 if (!is_dummy_object (object
))
868 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (object
));
869 gcc_assert (!TYPE_PTR_P (type
));
871 if (type
!= current_class_type
872 && current_class_type
873 && LAMBDA_TYPE_P (current_class_type
)
874 && lambda_function (current_class_type
)
875 && DERIVED_FROM_P (type
, nonlambda_method_basetype()))
876 return CLASSTYPE_LAMBDA_EXPR (current_class_type
);
881 /* We don't want to capture 'this' until we know we need it, i.e. after
882 overload resolution has chosen a non-static member function. At that
883 point we call this function to turn a dummy object into a use of the
887 maybe_resolve_dummy (tree object
, bool add_capture_p
)
889 if (tree lam
= resolvable_dummy_lambda (object
))
890 if (tree cap
= lambda_expr_this_capture (lam
, add_capture_p
))
891 if (cap
!= error_mark_node
)
892 object
= build_fold_indirect_ref (cap
);
897 /* When parsing a generic lambda containing an argument-dependent
898 member function call we defer overload resolution to instantiation
899 time. But we have to know now whether to capture this or not.
900 Do that if FNS contains any non-static fns.
901 The std doesn't anticipate this case, but I expect this to be the
902 outcome of discussion. */
905 maybe_generic_this_capture (tree object
, tree fns
)
907 if (tree lam
= resolvable_dummy_lambda (object
))
908 if (!LAMBDA_EXPR_THIS_CAPTURE (lam
))
910 /* We've not yet captured, so look at the function set of
912 if (BASELINK_P (fns
))
913 fns
= BASELINK_FUNCTIONS (fns
);
914 bool id_expr
= TREE_CODE (fns
) == TEMPLATE_ID_EXPR
;
916 fns
= TREE_OPERAND (fns
, 0);
918 for (lkp_iterator
iter (fns
); iter
; ++iter
)
919 if (((!id_expr
&& TREE_CODE (*iter
) != USING_DECL
)
920 || TREE_CODE (*iter
) == TEMPLATE_DECL
)
921 && DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter
))
923 /* Found a non-static member. Capture this. */
924 lambda_expr_this_capture (lam
, /*maybe*/-1);
930 /* Returns the innermost non-lambda function. */
933 current_nonlambda_function (void)
935 tree fn
= current_function_decl
;
936 while (fn
&& LAMBDA_FUNCTION_P (fn
))
937 fn
= decl_function_context (fn
);
941 /* Returns the method basetype of the innermost non-lambda function, including
942 a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
945 nonlambda_method_basetype (void)
947 if (!current_class_ref
)
950 tree type
= current_class_type
;
951 if (!type
|| !LAMBDA_TYPE_P (type
))
956 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
957 tree ex
= LAMBDA_EXPR_EXTRA_SCOPE (lam
);
958 if (ex
&& TREE_CODE (ex
) == FIELD_DECL
)
959 /* Lambda in an NSDMI. */
960 return DECL_CONTEXT (ex
);
962 tree fn
= TYPE_CONTEXT (type
);
963 if (!fn
|| TREE_CODE (fn
) != FUNCTION_DECL
964 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
965 /* No enclosing non-lambda method. */
967 if (!LAMBDA_FUNCTION_P (fn
))
968 /* Found an enclosing non-lambda method. */
969 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn
));
970 type
= DECL_CONTEXT (fn
);
974 /* Like current_scope, but looking through lambdas. */
977 current_nonlambda_scope (void)
979 tree scope
= current_scope ();
982 if (TREE_CODE (scope
) == FUNCTION_DECL
983 && LAMBDA_FUNCTION_P (scope
))
985 scope
= CP_TYPE_CONTEXT (DECL_CONTEXT (scope
));
988 else if (LAMBDA_TYPE_P (scope
))
990 scope
= CP_TYPE_CONTEXT (scope
);
998 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
999 indicated FN and NARGS, but do not initialize the return type or any of the
1003 prepare_op_call (tree fn
, int nargs
)
1007 t
= build_vl_exp (CALL_EXPR
, nargs
+ 3);
1008 CALL_EXPR_FN (t
) = fn
;
1009 CALL_EXPR_STATIC_CHAIN (t
) = NULL
;
1014 /* Return true iff CALLOP is the op() for a generic lambda. */
1017 generic_lambda_fn_p (tree callop
)
1019 return (LAMBDA_FUNCTION_P (callop
)
1020 && DECL_TEMPLATE_INFO (callop
)
1021 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop
)));
1024 /* If the closure TYPE has a static op(), also add a conversion to function
1028 maybe_add_lambda_conv_op (tree type
)
1030 bool nested
= (cfun
!= NULL
);
1031 bool nested_def
= decl_function_context (TYPE_MAIN_DECL (type
));
1032 tree callop
= lambda_function (type
);
1033 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
1035 if (LAMBDA_EXPR_CAPTURE_LIST (lam
) != NULL_TREE
1036 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) != CPLD_NONE
)
1039 if (processing_template_decl
)
1042 bool const generic_lambda_p
= generic_lambda_fn_p (callop
);
1044 if (!generic_lambda_p
&& undeduced_auto_decl (callop
))
1046 /* If the op() wasn't deduced due to errors, give up. */
1047 gcc_assert (errorcount
|| sorrycount
);
1051 /* Non-generic non-capturing lambdas only have a conversion function to
1052 pointer to function when the trailing requires-clause's constraints are
1054 if (!generic_lambda_p
&& !constraints_satisfied_p (callop
))
1057 /* Non-template conversion operators are defined directly with build_call_a
1058 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1059 deferred and the CALL is built in-place. In the case of a deduced return
1060 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1061 the return type is also built in-place. The arguments of DECLTYPE_CALL in
1062 the return expression may differ in flags from those in the body CALL. In
1063 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1064 the body CALL, but not in DECLTYPE_CALL. */
1066 vec
<tree
, va_gc
> *direct_argvec
= 0;
1067 tree decltype_call
= 0, call
= 0;
1068 tree optype
= TREE_TYPE (callop
);
1069 tree fn_result
= TREE_TYPE (optype
);
1071 tree thisarg
= build_int_cst (TREE_TYPE (DECL_ARGUMENTS (callop
)), 0);
1072 if (generic_lambda_p
)
1074 ++processing_template_decl
;
1076 /* Prepare the dependent member call for the static member function
1077 '_FUN' and, potentially, prepare another call to be used in a decltype
1078 return expression for a deduced return call op to allow for simple
1079 implementation of the conversion operator. */
1081 tree instance
= cp_build_fold_indirect_ref (thisarg
);
1082 tree objfn
= lookup_template_function (DECL_NAME (callop
),
1083 DECL_TI_ARGS (callop
));
1084 objfn
= build_min (COMPONENT_REF
, NULL_TREE
,
1085 instance
, objfn
, NULL_TREE
);
1086 int nargs
= list_length (DECL_ARGUMENTS (callop
)) - 1;
1088 call
= prepare_op_call (objfn
, nargs
);
1089 if (type_uses_auto (fn_result
))
1090 decltype_call
= prepare_op_call (objfn
, nargs
);
1094 direct_argvec
= make_tree_vector ();
1095 direct_argvec
->quick_push (thisarg
);
1098 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1099 declare the static member function "_FUN" below. For each arg append to
1100 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1101 call args (for the template case). If a parameter pack is found, expand
1102 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1104 tree fn_args
= NULL_TREE
;
1107 tree src
= DECL_CHAIN (DECL_ARGUMENTS (callop
));
1112 tree new_node
= copy_node (src
);
1113 /* We set DECL_CONTEXT of NEW_NODE to the statfn below.
1114 Notice this is creating a recursive type! */
1116 /* Clear TREE_ADDRESSABLE on thunk arguments. */
1117 TREE_ADDRESSABLE (new_node
) = 0;
1120 fn_args
= tgt
= new_node
;
1123 TREE_CHAIN (tgt
) = new_node
;
1127 mark_exp_read (tgt
);
1129 if (generic_lambda_p
)
1132 if (DECL_PACK_P (tgt
))
1134 a
= make_pack_expansion (a
);
1135 PACK_EXPANSION_LOCAL_P (a
) = true;
1137 CALL_EXPR_ARG (call
, ix
) = a
;
1141 /* Avoid capturing variables in this context. */
1142 ++cp_unevaluated_operand
;
1143 CALL_EXPR_ARG (decltype_call
, ix
) = forward_parm (tgt
);
1144 --cp_unevaluated_operand
;
1150 vec_safe_push (direct_argvec
, tgt
);
1152 src
= TREE_CHAIN (src
);
1156 if (generic_lambda_p
)
1160 fn_result
= finish_decltype_type
1161 (decltype_call
, /*id_expression_or_member_access_p=*/false,
1162 tf_warning_or_error
);
1166 call
= build_call_a (callop
,
1167 direct_argvec
->length (),
1168 direct_argvec
->address ());
1170 CALL_FROM_THUNK_P (call
) = 1;
1171 SET_EXPR_LOCATION (call
, UNKNOWN_LOCATION
);
1173 tree stattype
= build_function_type (fn_result
, FUNCTION_ARG_CHAIN (callop
));
1174 stattype
= (cp_build_type_attribute_variant
1175 (stattype
, TYPE_ATTRIBUTES (optype
)));
1176 if (flag_noexcept_type
1177 && TYPE_NOTHROW_P (TREE_TYPE (callop
)))
1178 stattype
= build_exception_variant (stattype
, noexcept_true_spec
);
1180 if (generic_lambda_p
)
1181 --processing_template_decl
;
1183 /* First build up the conversion op. */
1185 tree rettype
= build_pointer_type (stattype
);
1186 tree name
= make_conv_op_name (rettype
);
1187 tree thistype
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
1188 tree fntype
= build_method_type_directly (thistype
, rettype
, void_list_node
);
1189 /* DR 1722: The conversion function should be noexcept. */
1190 fntype
= build_exception_variant (fntype
, noexcept_true_spec
);
1191 tree convfn
= build_lang_decl (FUNCTION_DECL
, name
, fntype
);
1192 SET_DECL_LANGUAGE (convfn
, lang_cplusplus
);
1194 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (callop
);
1195 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
1196 grokclassfn (type
, fn
, NO_SPECIAL
);
1197 set_linkage_according_to_type (type
, fn
);
1198 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
1199 DECL_IN_AGGR_P (fn
) = 1;
1200 DECL_ARTIFICIAL (fn
) = 1;
1201 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1202 DECL_DECLARED_INLINE_P (fn
) = 1;
1203 DECL_DECLARED_CONSTEXPR_P (fn
) = DECL_DECLARED_CONSTEXPR_P (callop
);
1204 if (DECL_IMMEDIATE_FUNCTION_P (callop
))
1205 SET_DECL_IMMEDIATE_FUNCTION_P (fn
);
1206 DECL_ARGUMENTS (fn
) = build_this_parm (fn
, fntype
, TYPE_QUAL_CONST
);
1209 DECL_INTERFACE_KNOWN (fn
) = 1;
1211 if (generic_lambda_p
)
1212 fn
= add_inherited_template_parms (fn
, DECL_TI_TEMPLATE (callop
));
1214 add_method (type
, fn
, false);
1216 /* Generic thunk code fails for varargs; we'll complain in mark_used if
1217 the conversion op is used. */
1218 if (varargs_function_p (callop
))
1220 DECL_DELETED_FN (fn
) = 1;
1224 /* Now build up the thunk to be returned. */
1226 tree statfn
= build_lang_decl (FUNCTION_DECL
, fun_identifier
, stattype
);
1227 SET_DECL_LANGUAGE (statfn
, lang_cplusplus
);
1229 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (callop
);
1230 grokclassfn (type
, fn
, NO_SPECIAL
);
1231 set_linkage_according_to_type (type
, fn
);
1232 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
1233 DECL_IN_AGGR_P (fn
) = 1;
1234 DECL_ARTIFICIAL (fn
) = 1;
1235 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1236 DECL_DECLARED_INLINE_P (fn
) = 1;
1237 DECL_STATIC_FUNCTION_P (fn
) = 1;
1238 DECL_DECLARED_CONSTEXPR_P (fn
) = DECL_DECLARED_CONSTEXPR_P (callop
);
1239 if (DECL_IMMEDIATE_FUNCTION_P (callop
))
1240 SET_DECL_IMMEDIATE_FUNCTION_P (fn
);
1241 DECL_ARGUMENTS (fn
) = fn_args
;
1242 for (tree arg
= fn_args
; arg
; arg
= DECL_CHAIN (arg
))
1244 /* Avoid duplicate -Wshadow warnings. */
1245 DECL_NAME (arg
) = NULL_TREE
;
1246 DECL_CONTEXT (arg
) = fn
;
1249 DECL_INTERFACE_KNOWN (fn
) = 1;
1251 if (generic_lambda_p
)
1252 fn
= add_inherited_template_parms (fn
, DECL_TI_TEMPLATE (callop
));
1254 if (flag_sanitize
& SANITIZE_NULL
)
1255 /* Don't UBsan this function; we're deliberately calling op() with a null
1257 add_no_sanitize_value (fn
, SANITIZE_UNDEFINED
);
1259 add_method (type
, fn
, false);
1262 push_function_context ();
1264 /* Still increment function_depth so that we don't GC in the
1265 middle of an expression. */
1268 /* Generate the body of the thunk. */
1270 start_preparsed_function (statfn
, NULL_TREE
,
1271 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1272 tree body
= begin_function_body ();
1273 tree compound_stmt
= begin_compound_stmt (0);
1274 if (!generic_lambda_p
)
1276 set_flags_from_callee (call
);
1277 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call
)))
1278 call
= build_cplus_new (TREE_TYPE (call
), call
, tf_warning_or_error
);
1280 call
= convert_from_reference (call
);
1281 finish_return_stmt (call
);
1283 finish_compound_stmt (compound_stmt
);
1284 finish_function_body (body
);
1286 fn
= finish_function (/*inline_p=*/true);
1287 if (!generic_lambda_p
)
1288 expand_or_defer_fn (fn
);
1290 /* Generate the body of the conversion op. */
1292 start_preparsed_function (convfn
, NULL_TREE
,
1293 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1294 body
= begin_function_body ();
1295 compound_stmt
= begin_compound_stmt (0);
1297 /* decl_needed_p needs to see that it's used. */
1298 TREE_USED (statfn
) = 1;
1299 finish_return_stmt (decay_conversion (statfn
, tf_warning_or_error
));
1301 finish_compound_stmt (compound_stmt
);
1302 finish_function_body (body
);
1304 fn
= finish_function (/*inline_p=*/true);
1305 if (!generic_lambda_p
)
1306 expand_or_defer_fn (fn
);
1309 pop_function_context ();
1314 /* True if FN is the static function "_FUN" that gets returned from the lambda
1315 conversion operator. */
1318 lambda_static_thunk_p (tree fn
)
1320 return (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1321 && DECL_ARTIFICIAL (fn
)
1322 && DECL_STATIC_FUNCTION_P (fn
)
1323 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn
)));
1327 call_from_lambda_thunk_p (tree call
)
1329 return (CALL_FROM_THUNK_P (call
)
1330 && lambda_static_thunk_p (current_function_decl
));
1333 /* Returns true iff VAL is a lambda-related declaration which should
1334 be ignored by unqualified lookup. */
1337 is_lambda_ignored_entity (tree val
)
1339 /* Look past normal, non-VLA capture proxies. */
1340 if (is_normal_capture_proxy (val
)
1341 && !variably_modified_type_p (TREE_TYPE (val
), NULL_TREE
))
1344 /* Always ignore lambda fields, their names are only for debugging. */
1345 if (TREE_CODE (val
) == FIELD_DECL
1346 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val
)))
1349 /* None of the lookups that use qualify_lookup want the op() from the
1350 lambda; they want the one from the enclosing class. */
1351 if (tree fns
= maybe_get_fns (val
))
1352 if (LAMBDA_FUNCTION_P (OVL_FIRST (fns
)))
1358 /* Lambdas that appear in variable initializer or default argument scope
1359 get that in their mangling, so we need to record it. We might as well
1360 use the count for function and namespace scopes as well. */
1361 static GTY(()) tree lambda_scope
;
1362 static GTY(()) int lambda_count
;
1363 struct GTY(()) tree_int
1368 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
1371 start_lambda_scope (tree decl
)
1375 /* Once we're inside a function, we ignore variable scope and just push
1376 the function again so that popping works properly. */
1377 if (current_function_decl
&& TREE_CODE (decl
) == VAR_DECL
)
1378 decl
= current_function_decl
;
1379 ti
.t
= lambda_scope
;
1380 ti
.i
= lambda_count
;
1381 vec_safe_push (lambda_scope_stack
, ti
);
1382 if (lambda_scope
!= decl
)
1384 /* Don't reset the count if we're still in the same function. */
1385 lambda_scope
= decl
;
1391 record_lambda_scope (tree lambda
)
1393 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
1394 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
1397 tree closure
= LAMBDA_EXPR_CLOSURE (lambda
);
1398 gcc_checking_assert (closure
);
1399 maybe_attach_decl (lambda_scope
, TYPE_NAME (closure
));
1403 /* This lambda is an instantiation of a lambda in a template default argument
1404 that got no LAMBDA_EXPR_EXTRA_SCOPE, so this shouldn't either. But we do
1405 need to use and increment the global count to avoid collisions. */
1408 record_null_lambda_scope (tree lambda
)
1410 if (vec_safe_is_empty (lambda_scope_stack
))
1411 record_lambda_scope (lambda
);
1414 tree_int
*p
= lambda_scope_stack
->begin();
1415 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = p
->t
;
1416 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = p
->i
++;
1418 gcc_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda
) == NULL_TREE
);
1422 finish_lambda_scope (void)
1424 tree_int
*p
= &lambda_scope_stack
->last ();
1425 if (lambda_scope
!= p
->t
)
1427 lambda_scope
= p
->t
;
1428 lambda_count
= p
->i
;
1430 lambda_scope_stack
->pop ();
1434 start_lambda_function (tree fco
, tree lambda_expr
)
1436 /* Let the front end know that we are going to be defining this
1438 start_preparsed_function (fco
,
1440 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1442 tree body
= begin_function_body ();
1444 /* Push the proxies for any explicit captures. */
1445 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
1446 cap
= TREE_CHAIN (cap
))
1447 build_capture_proxy (TREE_PURPOSE (cap
), TREE_VALUE (cap
));
1452 /* Subroutine of prune_lambda_captures: CAP is a node in
1453 LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
1454 might optimize away the capture, or NULL_TREE if there is no such
1458 var_to_maybe_prune (tree cap
)
1460 if (LAMBDA_CAPTURE_EXPLICIT_P (cap
))
1461 /* Don't prune explicit captures. */
1464 tree mem
= TREE_PURPOSE (cap
);
1465 if (!DECL_P (mem
) || !DECL_NORMAL_CAPTURE_P (mem
))
1466 /* Packs and init-captures aren't captures of constant vars. */
1469 tree init
= TREE_VALUE (cap
);
1470 if (is_normal_capture_proxy (init
))
1471 init
= DECL_CAPTURED_VARIABLE (init
);
1472 if (decl_constant_var_p (init
))
1478 /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
1479 for constant variables are actually used in the lambda body.
1481 There will always be a DECL_EXPR for the capture proxy; remember it when we
1482 see it, but replace it with any other use. */
1485 mark_const_cap_r (tree
*t
, int *walk_subtrees
, void *data
)
1487 hash_map
<tree
,tree
*> &const_vars
= *(hash_map
<tree
,tree
*>*)data
;
1489 tree var
= NULL_TREE
;
1490 if (TREE_CODE (*t
) == DECL_EXPR
)
1492 tree decl
= DECL_EXPR_DECL (*t
);
1493 if (is_constant_capture_proxy (decl
))
1495 var
= DECL_CAPTURED_VARIABLE (decl
);
1499 else if (is_constant_capture_proxy (*t
))
1500 var
= DECL_CAPTURED_VARIABLE (*t
);
1504 tree
*&slot
= const_vars
.get_or_insert (var
);
1505 if (!slot
|| VAR_P (*t
))
1512 /* We're at the end of processing a lambda; go back and remove any captures of
1513 constant variables for which we've folded away all uses. */
1516 prune_lambda_captures (tree body
)
1518 tree lam
= current_lambda_expr ();
1519 if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam
))
1520 /* No uses were optimized away. */
1522 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) == CPLD_NONE
)
1523 /* No default captures, and we don't prune explicit captures. */
1526 hash_map
<tree
,tree
*> const_vars
;
1528 cp_walk_tree_without_duplicates (&body
, mark_const_cap_r
, &const_vars
);
1530 tree
*fieldp
= &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam
));
1531 for (tree
*capp
= &LAMBDA_EXPR_CAPTURE_LIST (lam
); *capp
; )
1534 if (tree var
= var_to_maybe_prune (cap
))
1536 tree
**use
= const_vars
.get (var
);
1537 if (use
&& TREE_CODE (**use
) == DECL_EXPR
)
1539 /* All uses of this capture were folded away, leaving only the
1540 proxy declaration. */
1542 /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
1543 *capp
= TREE_CHAIN (cap
);
1545 /* And out of TYPE_FIELDS. */
1546 tree field
= TREE_PURPOSE (cap
);
1547 while (*fieldp
!= field
)
1548 fieldp
= &DECL_CHAIN (*fieldp
);
1549 *fieldp
= DECL_CHAIN (*fieldp
);
1551 /* And remove the capture proxy declaration. */
1557 capp
= &TREE_CHAIN (cap
);
1562 finish_lambda_function (tree body
)
1564 finish_function_body (body
);
1566 prune_lambda_captures (body
);
1568 /* Finish the function and generate code for it if necessary. */
1569 tree fn
= finish_function (/*inline_p=*/true);
1571 /* Only expand if the call op is not a template. */
1572 if (!DECL_TEMPLATE_INFO (fn
))
1573 expand_or_defer_fn (fn
);
1576 #include "gt-cp-lambda.h"