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_PENDING_PROXIES (lambda
) = NULL
;
45 LAMBDA_EXPR_MUTABLE_P (lambda
) = false;
49 /* Create the closure object for a LAMBDA_EXPR. */
52 build_lambda_object (tree lambda_expr
)
54 /* Build aggregate constructor call.
55 - cp_parser_braced_list
56 - cp_parser_functional_cast */
57 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
58 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 saved_loc
= input_location
;
66 input_location
= LAMBDA_EXPR_LOCATION (lambda_expr
);
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 CLASSTYPE_NON_AGGREGATE (type
) = 1;
122 input_location
= saved_loc
;
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 /* Returns the type to use for the return type of the operator() of a
166 lambda_return_type (tree expr
)
168 if (expr
== NULL_TREE
)
169 return void_type_node
;
170 if (type_unknown_p (expr
)
171 || BRACE_ENCLOSED_INITIALIZER_P (expr
))
173 cxx_incomplete_type_error (expr
, TREE_TYPE (expr
));
174 return error_mark_node
;
176 gcc_checking_assert (!type_dependent_expression_p (expr
));
177 return cv_unqualified (type_decays_to (unlowered_expr_type (expr
)));
180 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
184 lambda_function (tree lambda
)
187 if (TREE_CODE (lambda
) == LAMBDA_EXPR
)
188 type
= LAMBDA_EXPR_CLOSURE (lambda
);
191 gcc_assert (LAMBDA_TYPE_P (type
));
192 /* Don't let debug_tree cause instantiation. */
193 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
194 && !COMPLETE_OR_OPEN_TYPE_P (type
))
196 lambda
= lookup_member (type
, call_op_identifier
,
197 /*protect=*/0, /*want_type=*/false,
198 tf_warning_or_error
);
200 lambda
= STRIP_TEMPLATE (get_first_fn (lambda
));
204 /* Returns the type to use for the FIELD_DECL corresponding to the
205 capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
206 C++14 init capture, and BY_REFERENCE_P indicates whether we're
207 capturing by reference. */
210 lambda_capture_field_type (tree expr
, bool explicit_init_p
,
214 bool is_this
= is_this_parameter (tree_strip_nop_conversions (expr
));
216 if (!is_this
&& explicit_init_p
)
218 tree auto_node
= make_auto ();
222 /* Add the reference now, so deduction doesn't lose
223 outermost CV qualifiers of EXPR. */
224 type
= build_reference_type (type
);
225 if (uses_parameter_packs (expr
))
226 /* Stick with 'auto' even if the type could be deduced. */;
228 type
= do_auto_deduction (type
, expr
, auto_node
);
230 else if (!is_this
&& type_dependent_expression_p (expr
))
232 type
= cxx_make_type (DECLTYPE_TYPE
);
233 DECLTYPE_TYPE_EXPR (type
) = expr
;
234 DECLTYPE_FOR_LAMBDA_CAPTURE (type
) = true;
235 DECLTYPE_FOR_REF_CAPTURE (type
) = by_reference_p
;
236 SET_TYPE_STRUCTURAL_EQUALITY (type
);
240 type
= non_reference (unlowered_expr_type (expr
));
243 && (by_reference_p
|| TREE_CODE (type
) == FUNCTION_TYPE
))
244 type
= build_reference_type (type
);
250 /* Returns true iff DECL is a lambda capture proxy variable created by
251 build_capture_proxy. */
254 is_capture_proxy (tree decl
)
257 && DECL_HAS_VALUE_EXPR_P (decl
)
258 && !DECL_ANON_UNION_VAR_P (decl
)
259 && !DECL_DECOMPOSITION_P (decl
)
260 && !DECL_FNAME_P (decl
)
261 && !(DECL_ARTIFICIAL (decl
)
262 && DECL_LANG_SPECIFIC (decl
)
263 && DECL_OMP_PRIVATIZED_MEMBER (decl
))
264 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl
)));
267 /* Returns true iff DECL is a capture proxy for a normal capture
268 (i.e. without explicit initializer). */
271 is_normal_capture_proxy (tree decl
)
273 if (!is_capture_proxy (decl
))
274 /* It's not a capture proxy. */
277 return (DECL_LANG_SPECIFIC (decl
)
278 && DECL_CAPTURED_VARIABLE (decl
));
281 /* Returns true iff DECL is a capture proxy for a normal capture
282 of a constant variable. */
285 is_constant_capture_proxy (tree decl
)
287 if (is_normal_capture_proxy (decl
))
288 return decl_constant_var_p (DECL_CAPTURED_VARIABLE (decl
));
292 /* VAR is a capture proxy created by build_capture_proxy; add it to the
293 current function, which is the operator() for the appropriate lambda. */
296 insert_capture_proxy (tree var
)
298 if (is_normal_capture_proxy (var
))
300 tree cap
= DECL_CAPTURED_VARIABLE (var
);
303 gcc_assert (!is_normal_capture_proxy (cap
));
304 tree old
= retrieve_local_specialization (cap
);
306 gcc_assert (DECL_CONTEXT (old
) != DECL_CONTEXT (var
));
308 register_local_specialization (var
, cap
);
311 /* Put the capture proxy in the extra body block so that it won't clash
312 with a later local variable. */
313 pushdecl_outermost_localscope (var
);
315 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
316 var
= build_stmt (DECL_SOURCE_LOCATION (var
), DECL_EXPR
, var
);
317 tree stmt_list
= (*stmt_list_stack
)[1];
318 gcc_assert (stmt_list
);
319 append_to_statement_list_force (var
, &stmt_list
);
322 /* We've just finished processing a lambda; if the containing scope is also
323 a lambda, insert any capture proxies that were created while processing
324 the nested lambda. */
327 insert_pending_capture_proxies (void)
330 vec
<tree
, va_gc
> *proxies
;
333 if (!current_function_decl
|| !LAMBDA_FUNCTION_P (current_function_decl
))
336 lam
= CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl
));
337 proxies
= LAMBDA_EXPR_PENDING_PROXIES (lam
);
338 for (i
= 0; i
< vec_safe_length (proxies
); ++i
)
340 tree var
= (*proxies
)[i
];
341 insert_capture_proxy (var
);
343 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam
));
344 LAMBDA_EXPR_PENDING_PROXIES (lam
) = NULL
;
347 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
348 return the type we want the proxy to have: the type of the field itself,
349 with added const-qualification if the lambda isn't mutable and the
350 capture is by value. */
353 lambda_proxy_type (tree ref
)
356 if (ref
== error_mark_node
)
357 return error_mark_node
;
358 if (REFERENCE_REF_P (ref
))
359 ref
= TREE_OPERAND (ref
, 0);
360 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
361 type
= TREE_TYPE (ref
);
362 if (!type
|| WILDCARD_TYPE_P (non_reference (type
)))
364 type
= cxx_make_type (DECLTYPE_TYPE
);
365 DECLTYPE_TYPE_EXPR (type
) = ref
;
366 DECLTYPE_FOR_LAMBDA_PROXY (type
) = true;
367 SET_TYPE_STRUCTURAL_EQUALITY (type
);
369 if (DECL_PACK_P (TREE_OPERAND (ref
, 1)))
370 type
= make_pack_expansion (type
);
374 /* MEMBER is a capture field in a lambda closure class. Now that we're
375 inside the operator(), build a placeholder var for future lookups and
379 build_capture_proxy (tree member
, tree init
)
381 tree var
, object
, fn
, closure
, name
, lam
, type
;
383 if (PACK_EXPANSION_P (member
))
384 member
= PACK_EXPANSION_PATTERN (member
);
386 closure
= DECL_CONTEXT (member
);
387 fn
= lambda_function (closure
);
388 lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
390 /* The proxy variable forwards to the capture field. */
391 object
= build_fold_indirect_ref (DECL_ARGUMENTS (fn
));
392 object
= finish_non_static_data_member (member
, object
, NULL_TREE
);
393 if (REFERENCE_REF_P (object
))
394 object
= TREE_OPERAND (object
, 0);
396 /* Remove the __ inserted by add_capture. */
397 name
= get_identifier (IDENTIFIER_POINTER (DECL_NAME (member
)) + 2);
399 type
= lambda_proxy_type (object
);
401 if (name
== this_identifier
&& !INDIRECT_TYPE_P (type
))
403 type
= build_pointer_type (type
);
404 type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
405 object
= build_fold_addr_expr_with_type (object
, type
);
408 if (DECL_VLA_CAPTURE_P (member
))
410 /* Rebuild the VLA type from the pointer and maxindex. */
411 tree field
= next_initializable_field (TYPE_FIELDS (type
));
412 tree ptr
= build_simple_component_ref (object
, field
);
413 field
= next_initializable_field (DECL_CHAIN (field
));
414 tree max
= build_simple_component_ref (object
, field
);
415 type
= build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr
)),
416 build_index_type (max
));
417 type
= build_reference_type (type
);
418 object
= convert (type
, ptr
);
421 complete_type (type
);
423 var
= build_decl (input_location
, VAR_DECL
, name
, type
);
424 SET_DECL_VALUE_EXPR (var
, object
);
425 DECL_HAS_VALUE_EXPR_P (var
) = 1;
426 DECL_ARTIFICIAL (var
) = 1;
428 DECL_CONTEXT (var
) = fn
;
430 if (DECL_NORMAL_CAPTURE_P (member
))
432 if (DECL_VLA_CAPTURE_P (member
))
434 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
435 init
= TREE_OPERAND (init
, 0); // Strip ADDR_EXPR.
436 init
= TREE_OPERAND (init
, 0); // Strip ARRAY_REF.
440 if (PACK_EXPANSION_P (init
))
441 init
= PACK_EXPANSION_PATTERN (init
);
444 if (INDIRECT_REF_P (init
))
445 init
= TREE_OPERAND (init
, 0);
448 gcc_assert (VAR_P (init
) || TREE_CODE (init
) == PARM_DECL
);
449 while (is_normal_capture_proxy (init
))
450 init
= DECL_CAPTURED_VARIABLE (init
);
451 retrofit_lang_decl (var
);
452 DECL_CAPTURED_VARIABLE (var
) = init
;
455 if (name
== this_identifier
)
457 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam
) == member
);
458 LAMBDA_EXPR_THIS_CAPTURE (lam
) = var
;
461 if (fn
== current_function_decl
)
462 insert_capture_proxy (var
);
464 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam
), var
);
469 static GTY(()) tree ptr_id
;
470 static GTY(()) tree max_id
;
472 /* Return a struct containing a pointer and a length for lambda capture of
473 an array of runtime length. */
476 vla_capture_type (tree array_type
)
478 tree type
= xref_tag (record_type
, make_anon_name ());
479 xref_basetypes (type
, NULL_TREE
);
480 type
= begin_class_definition (type
);
483 ptr_id
= get_identifier ("ptr");
484 max_id
= get_identifier ("max");
486 tree ptrtype
= build_pointer_type (TREE_TYPE (array_type
));
487 tree field
= build_decl (input_location
, FIELD_DECL
, ptr_id
, ptrtype
);
488 finish_member_declaration (field
);
489 field
= build_decl (input_location
, FIELD_DECL
, max_id
, sizetype
);
490 finish_member_declaration (field
);
491 return finish_struct (type
, NULL_TREE
);
494 /* From an ID and INITIALIZER, create a capture (by reference if
495 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
496 and return it. If ID is `this', BY_REFERENCE_P says whether
497 `*this' is captured by reference. */
500 add_capture (tree lambda
, tree id
, tree orig_init
, bool by_reference_p
,
501 bool explicit_init_p
)
504 tree type
, member
, name
;
506 bool variadic
= false;
507 tree initializer
= orig_init
;
509 if (PACK_EXPANSION_P (initializer
))
511 initializer
= PACK_EXPANSION_PATTERN (initializer
);
515 if (TREE_CODE (initializer
) == TREE_LIST
516 /* A pack expansion might end up with multiple elements. */
517 && !PACK_EXPANSION_P (TREE_VALUE (initializer
)))
518 initializer
= build_x_compound_expr_from_list (initializer
, ELK_INIT
,
519 tf_warning_or_error
);
520 type
= TREE_TYPE (initializer
);
521 if (type
== error_mark_node
)
522 return error_mark_node
;
524 if (!dependent_type_p (type
) && array_of_runtime_bound_p (type
))
528 error ("array of runtime bound cannot be captured by copy, "
529 "only by reference");
531 /* For a VLA, we capture the address of the first element and the
532 maximum index, and then reconstruct the VLA for the proxy. */
533 tree elt
= cp_build_array_ref (input_location
, initializer
,
534 integer_zero_node
, tf_warning_or_error
);
535 initializer
= build_constructor_va (init_list_type_node
, 2,
536 NULL_TREE
, build_address (elt
),
537 NULL_TREE
, array_type_nelts (type
));
538 type
= vla_capture_type (type
);
540 else if (!dependent_type_p (type
)
541 && variably_modified_type_p (type
, NULL_TREE
))
543 sorry ("capture of variably-modified type %qT that is not an N3639 array "
544 "of runtime bound", type
);
545 if (TREE_CODE (type
) == ARRAY_TYPE
546 && variably_modified_type_p (TREE_TYPE (type
), NULL_TREE
))
547 inform (input_location
, "because the array element type %qT has "
548 "variable size", TREE_TYPE (type
));
549 return error_mark_node
;
553 type
= lambda_capture_field_type (initializer
, explicit_init_p
,
555 if (type
== error_mark_node
)
556 return error_mark_node
;
558 if (id
== this_identifier
&& !by_reference_p
)
560 gcc_assert (INDIRECT_TYPE_P (type
));
561 type
= TREE_TYPE (type
);
562 initializer
= cp_build_fold_indirect_ref (initializer
);
565 if (dependent_type_p (type
))
567 else if (id
!= this_identifier
&& by_reference_p
)
569 if (!lvalue_p (initializer
))
571 error ("cannot capture %qE by reference", initializer
);
572 return error_mark_node
;
577 /* Capture by copy requires a complete type. */
578 type
= complete_type (type
);
579 if (!COMPLETE_TYPE_P (type
))
581 error ("capture by copy of incomplete type %qT", type
);
582 cxx_incomplete_type_inform (type
);
583 return error_mark_node
;
585 else if (!verify_type_context (input_location
,
586 TCTX_CAPTURE_BY_COPY
, type
))
587 return error_mark_node
;
591 /* Add __ to the beginning of the field name so that user code
592 won't find the field with name lookup. We can't just leave the name
593 unset because template instantiation uses the name to find
594 instantiated fields. */
595 buf
= (char *) alloca (IDENTIFIER_LENGTH (id
) + 3);
596 buf
[1] = buf
[0] = '_';
597 memcpy (buf
+ 2, IDENTIFIER_POINTER (id
),
598 IDENTIFIER_LENGTH (id
) + 1);
599 name
= get_identifier (buf
);
603 type
= make_pack_expansion (type
);
605 /* With an explicit initializer 'type' is auto, which isn't really a
606 parameter pack in this context. We will want as many fields as we
607 have elements in the expansion of the initializer, so use its packs
609 PACK_EXPANSION_PARAMETER_PACKS (type
)
610 = uses_parameter_packs (initializer
);
613 /* Make member variable. */
614 member
= build_decl (input_location
, FIELD_DECL
, name
, type
);
615 DECL_VLA_CAPTURE_P (member
) = vla
;
617 if (!explicit_init_p
)
618 /* Normal captures are invisible to name lookup but uses are replaced
619 with references to the capture field; we implement this by only
620 really making them invisible in unevaluated context; see
621 qualify_lookup. For now, let's make explicitly initialized captures
623 DECL_NORMAL_CAPTURE_P (member
) = true;
625 if (id
== this_identifier
)
626 LAMBDA_EXPR_THIS_CAPTURE (lambda
) = member
;
628 /* Add it to the appropriate closure class if we've started it. */
629 if (current_class_type
630 && current_class_type
== LAMBDA_EXPR_CLOSURE (lambda
))
632 if (COMPLETE_TYPE_P (current_class_type
))
633 internal_error ("trying to capture %qD in instantiation of "
634 "generic lambda", id
);
635 finish_member_declaration (member
);
638 tree listmem
= member
;
641 listmem
= make_pack_expansion (member
);
642 initializer
= orig_init
;
644 LAMBDA_EXPR_CAPTURE_LIST (lambda
)
645 = tree_cons (listmem
, initializer
, LAMBDA_EXPR_CAPTURE_LIST (lambda
));
647 if (LAMBDA_EXPR_CLOSURE (lambda
))
648 return build_capture_proxy (member
, initializer
);
649 /* For explicit captures we haven't started the function yet, so we wait
650 and build the proxy from cp_parser_lambda_body. */
651 LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda
)) = true;
655 /* Register all the capture members on the list CAPTURES, which is the
656 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
659 register_capture_members (tree captures
)
661 if (captures
== NULL_TREE
)
664 register_capture_members (TREE_CHAIN (captures
));
666 tree field
= TREE_PURPOSE (captures
);
667 if (PACK_EXPANSION_P (field
))
668 field
= PACK_EXPANSION_PATTERN (field
);
670 finish_member_declaration (field
);
673 /* Similar to add_capture, except this works on a stack of nested lambdas.
674 BY_REFERENCE_P in this case is derived from the default capture mode.
675 Returns the capture for the lambda at the bottom of the stack. */
678 add_default_capture (tree lambda_stack
, tree id
, tree initializer
)
680 bool this_capture_p
= (id
== this_identifier
);
681 tree var
= NULL_TREE
;
682 tree saved_class_type
= current_class_type
;
684 for (tree node
= lambda_stack
;
686 node
= TREE_CHAIN (node
))
688 tree lambda
= TREE_VALUE (node
);
690 current_class_type
= LAMBDA_EXPR_CLOSURE (lambda
);
691 if (DECL_PACK_P (initializer
))
692 initializer
= make_pack_expansion (initializer
);
693 var
= add_capture (lambda
,
698 || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
)
700 /*explicit_init_p=*/false);
701 initializer
= convert_from_reference (var
);
703 /* Warn about deprecated implicit capture of this via [=]. */
704 if (cxx_dialect
>= cxx20
706 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda
) == CPLD_COPY
)
708 if (warning_at (LAMBDA_EXPR_LOCATION (lambda
), OPT_Wdeprecated
,
709 "implicit capture of %qE via %<[=]%> is deprecated "
710 "in C++20", this_identifier
))
711 inform (LAMBDA_EXPR_LOCATION (lambda
), "add explicit %<this%> or "
712 "%<*this%> capture");
716 current_class_type
= saved_class_type
;
721 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
722 form of an INDIRECT_REF, possibly adding it through default
723 capturing, if ADD_CAPTURE_P is nonzero. If ADD_CAPTURE_P is negative,
724 try to capture but don't complain if we can't. */
727 lambda_expr_this_capture (tree lambda
, int add_capture_p
)
731 tree this_capture
= LAMBDA_EXPR_THIS_CAPTURE (lambda
);
733 /* In unevaluated context this isn't an odr-use, so don't capture. */
734 if (cp_unevaluated_operand
)
735 add_capture_p
= false;
737 /* Try to default capture 'this' if we can. */
740 tree lambda_stack
= NULL_TREE
;
741 tree init
= NULL_TREE
;
743 /* If we are in a lambda function, we can move out until we hit:
744 1. a non-lambda function or NSDMI,
745 2. a lambda function capturing 'this', or
746 3. a non-default capturing lambda function. */
747 for (tree tlambda
= lambda
; ;)
750 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda
) == CPLD_NONE
)
751 /* tlambda won't let us capture 'this'. */
755 lambda_stack
= tree_cons (NULL_TREE
,
759 tree closure
= LAMBDA_EXPR_CLOSURE (tlambda
);
760 tree containing_function
761 = decl_function_context (TYPE_NAME (closure
));
763 tree ex
= LAMBDA_EXPR_EXTRA_SCOPE (tlambda
);
764 if (ex
&& TREE_CODE (ex
) == FIELD_DECL
)
766 /* Lambda in an NSDMI. We don't have a function to look up
767 'this' in, but we can find (or rebuild) the fake one from
768 inject_this_parameter. */
769 if (!containing_function
&& !COMPLETE_TYPE_P (closure
))
770 /* If we're parsing a lambda in a non-local class,
771 we can find the fake 'this' in scope_chain. */
772 init
= scope_chain
->x_current_class_ptr
;
774 /* Otherwise it's either gone or buried in
775 function_context_stack, so make another. */
776 init
= build_this_parm (NULL_TREE
, DECL_CONTEXT (ex
),
779 (init
&& (TREE_TYPE (TREE_TYPE (init
))
780 == current_nonlambda_class_type ()));
784 if (containing_function
== NULL_TREE
)
785 /* We ran out of scopes; there's no 'this' to capture. */
788 if (!LAMBDA_FUNCTION_P (containing_function
))
790 /* We found a non-lambda function. */
791 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function
))
792 /* First parameter is 'this'. */
793 init
= DECL_ARGUMENTS (containing_function
);
798 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function
));
800 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda
))
802 /* An outer lambda has already captured 'this'. */
803 init
= LAMBDA_EXPR_THIS_CAPTURE (tlambda
);
811 this_capture
= add_default_capture (lambda_stack
,
812 /*id=*/this_identifier
,
819 if (cp_unevaluated_operand
)
820 result
= this_capture
;
821 else if (!this_capture
)
823 if (add_capture_p
== 1)
825 error ("%<this%> was not captured for this lambda function");
826 result
= error_mark_node
;
833 /* To make sure that current_class_ref is for the lambda. */
834 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref
))
835 == LAMBDA_EXPR_CLOSURE (lambda
));
837 result
= this_capture
;
839 /* If 'this' is captured, each use of 'this' is transformed into an
840 access to the corresponding unnamed data member of the closure
841 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
842 ensures that the transformed expression is an rvalue. ] */
843 result
= rvalue (result
);
849 /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
852 current_lambda_expr (void)
854 tree type
= current_class_type
;
855 while (type
&& !LAMBDA_TYPE_P (type
))
856 type
= decl_type_context (TYPE_NAME (type
));
858 return CLASSTYPE_LAMBDA_EXPR (type
);
863 /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
864 object. NULL otherwise.. */
867 resolvable_dummy_lambda (tree object
)
869 if (!is_dummy_object (object
))
872 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (object
));
873 gcc_assert (!TYPE_PTR_P (type
));
875 if (type
!= current_class_type
876 && current_class_type
877 && LAMBDA_TYPE_P (current_class_type
)
878 && lambda_function (current_class_type
)
879 && DERIVED_FROM_P (type
, nonlambda_method_basetype()))
880 return CLASSTYPE_LAMBDA_EXPR (current_class_type
);
885 /* We don't want to capture 'this' until we know we need it, i.e. after
886 overload resolution has chosen a non-static member function. At that
887 point we call this function to turn a dummy object into a use of the
891 maybe_resolve_dummy (tree object
, bool add_capture_p
)
893 if (tree lam
= resolvable_dummy_lambda (object
))
894 if (tree cap
= lambda_expr_this_capture (lam
, add_capture_p
))
895 if (cap
!= error_mark_node
)
896 object
= build_fold_indirect_ref (cap
);
901 /* When parsing a generic lambda containing an argument-dependent
902 member function call we defer overload resolution to instantiation
903 time. But we have to know now whether to capture this or not.
904 Do that if FNS contains any non-static fns.
905 The std doesn't anticipate this case, but I expect this to be the
906 outcome of discussion. */
909 maybe_generic_this_capture (tree object
, tree fns
)
911 if (tree lam
= resolvable_dummy_lambda (object
))
912 if (!LAMBDA_EXPR_THIS_CAPTURE (lam
))
914 /* We've not yet captured, so look at the function set of
916 if (BASELINK_P (fns
))
917 fns
= BASELINK_FUNCTIONS (fns
);
918 bool id_expr
= TREE_CODE (fns
) == TEMPLATE_ID_EXPR
;
920 fns
= TREE_OPERAND (fns
, 0);
922 for (lkp_iterator
iter (fns
); iter
; ++iter
)
923 if (((!id_expr
&& TREE_CODE (*iter
) != USING_DECL
)
924 || TREE_CODE (*iter
) == TEMPLATE_DECL
)
925 && DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter
))
927 /* Found a non-static member. Capture this. */
928 lambda_expr_this_capture (lam
, /*maybe*/-1);
934 /* Returns the innermost non-lambda function. */
937 current_nonlambda_function (void)
939 tree fn
= current_function_decl
;
940 while (fn
&& LAMBDA_FUNCTION_P (fn
))
941 fn
= decl_function_context (fn
);
945 /* Returns the method basetype of the innermost non-lambda function, including
946 a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
949 nonlambda_method_basetype (void)
951 if (!current_class_ref
)
954 tree type
= current_class_type
;
955 if (!type
|| !LAMBDA_TYPE_P (type
))
960 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
961 tree ex
= LAMBDA_EXPR_EXTRA_SCOPE (lam
);
962 if (ex
&& TREE_CODE (ex
) == FIELD_DECL
)
963 /* Lambda in an NSDMI. */
964 return DECL_CONTEXT (ex
);
966 tree fn
= TYPE_CONTEXT (type
);
967 if (!fn
|| TREE_CODE (fn
) != FUNCTION_DECL
968 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
969 /* No enclosing non-lambda method. */
971 if (!LAMBDA_FUNCTION_P (fn
))
972 /* Found an enclosing non-lambda method. */
973 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn
));
974 type
= DECL_CONTEXT (fn
);
978 /* Like current_scope, but looking through lambdas. */
981 current_nonlambda_scope (void)
983 tree scope
= current_scope ();
986 if (TREE_CODE (scope
) == FUNCTION_DECL
987 && LAMBDA_FUNCTION_P (scope
))
989 scope
= CP_TYPE_CONTEXT (DECL_CONTEXT (scope
));
992 else if (LAMBDA_TYPE_P (scope
))
994 scope
= CP_TYPE_CONTEXT (scope
);
1002 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
1003 indicated FN and NARGS, but do not initialize the return type or any of the
1007 prepare_op_call (tree fn
, int nargs
)
1011 t
= build_vl_exp (CALL_EXPR
, nargs
+ 3);
1012 CALL_EXPR_FN (t
) = fn
;
1013 CALL_EXPR_STATIC_CHAIN (t
) = NULL
;
1018 /* Return true iff CALLOP is the op() for a generic lambda. */
1021 generic_lambda_fn_p (tree callop
)
1023 return (LAMBDA_FUNCTION_P (callop
)
1024 && DECL_TEMPLATE_INFO (callop
)
1025 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop
)));
1028 /* If the closure TYPE has a static op(), also add a conversion to function
1032 maybe_add_lambda_conv_op (tree type
)
1034 bool nested
= (cfun
!= NULL
);
1035 bool nested_def
= decl_function_context (TYPE_MAIN_DECL (type
));
1036 tree callop
= lambda_function (type
);
1037 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
1039 if (LAMBDA_EXPR_CAPTURE_LIST (lam
) != NULL_TREE
1040 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) != CPLD_NONE
)
1043 if (processing_template_decl
)
1046 bool const generic_lambda_p
= generic_lambda_fn_p (callop
);
1048 if (!generic_lambda_p
&& undeduced_auto_decl (callop
))
1050 /* If the op() wasn't deduced due to errors, give up. */
1051 gcc_assert (errorcount
|| sorrycount
);
1055 /* Non-generic non-capturing lambdas only have a conversion function to
1056 pointer to function when the trailing requires-clause's constraints are
1058 if (!generic_lambda_p
&& !constraints_satisfied_p (callop
))
1061 /* Non-template conversion operators are defined directly with build_call_a
1062 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1063 deferred and the CALL is built in-place. In the case of a deduced return
1064 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1065 the return type is also built in-place. The arguments of DECLTYPE_CALL in
1066 the return expression may differ in flags from those in the body CALL. In
1067 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1068 the body CALL, but not in DECLTYPE_CALL. */
1070 vec
<tree
, va_gc
> *direct_argvec
= 0;
1071 tree decltype_call
= 0, call
= 0;
1072 tree optype
= TREE_TYPE (callop
);
1073 tree fn_result
= TREE_TYPE (optype
);
1075 tree thisarg
= build_int_cst (TREE_TYPE (DECL_ARGUMENTS (callop
)), 0);
1076 if (generic_lambda_p
)
1078 ++processing_template_decl
;
1080 /* Prepare the dependent member call for the static member function
1081 '_FUN' and, potentially, prepare another call to be used in a decltype
1082 return expression for a deduced return call op to allow for simple
1083 implementation of the conversion operator. */
1085 tree instance
= cp_build_fold_indirect_ref (thisarg
);
1086 tree objfn
= lookup_template_function (DECL_NAME (callop
),
1087 DECL_TI_ARGS (callop
));
1088 objfn
= build_min (COMPONENT_REF
, NULL_TREE
,
1089 instance
, objfn
, 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
);
1117 /* We set DECL_CONTEXT of NEW_NODE to the statfn below.
1118 Notice this is creating a recursive type! */
1120 /* Clear TREE_ADDRESSABLE on thunk arguments. */
1121 TREE_ADDRESSABLE (new_node
) = 0;
1124 fn_args
= tgt
= new_node
;
1127 TREE_CHAIN (tgt
) = new_node
;
1131 mark_exp_read (tgt
);
1133 if (generic_lambda_p
)
1136 if (DECL_PACK_P (tgt
))
1138 a
= make_pack_expansion (a
);
1139 PACK_EXPANSION_LOCAL_P (a
) = true;
1141 CALL_EXPR_ARG (call
, ix
) = a
;
1145 /* Avoid capturing variables in this context. */
1146 ++cp_unevaluated_operand
;
1147 CALL_EXPR_ARG (decltype_call
, ix
) = forward_parm (tgt
);
1148 --cp_unevaluated_operand
;
1154 vec_safe_push (direct_argvec
, tgt
);
1156 src
= TREE_CHAIN (src
);
1160 if (generic_lambda_p
)
1164 fn_result
= finish_decltype_type
1165 (decltype_call
, /*id_expression_or_member_access_p=*/false,
1166 tf_warning_or_error
);
1170 call
= build_call_a (callop
,
1171 direct_argvec
->length (),
1172 direct_argvec
->address ());
1174 CALL_FROM_THUNK_P (call
) = 1;
1175 SET_EXPR_LOCATION (call
, UNKNOWN_LOCATION
);
1177 tree stattype
= build_function_type (fn_result
, FUNCTION_ARG_CHAIN (callop
));
1178 stattype
= (cp_build_type_attribute_variant
1179 (stattype
, TYPE_ATTRIBUTES (optype
)));
1180 if (flag_noexcept_type
1181 && TYPE_NOTHROW_P (TREE_TYPE (callop
)))
1182 stattype
= build_exception_variant (stattype
, noexcept_true_spec
);
1184 if (generic_lambda_p
)
1185 --processing_template_decl
;
1187 /* First build up the conversion op. */
1189 tree rettype
= build_pointer_type (stattype
);
1190 tree name
= make_conv_op_name (rettype
);
1191 tree thistype
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
1192 tree fntype
= build_method_type_directly (thistype
, rettype
, void_list_node
);
1193 /* DR 1722: The conversion function should be noexcept. */
1194 fntype
= build_exception_variant (fntype
, noexcept_true_spec
);
1195 tree convfn
= build_lang_decl (FUNCTION_DECL
, name
, fntype
);
1196 SET_DECL_LANGUAGE (convfn
, lang_cplusplus
);
1198 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (callop
);
1199 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
1200 grokclassfn (type
, fn
, NO_SPECIAL
);
1201 set_linkage_according_to_type (type
, fn
);
1202 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
1203 DECL_IN_AGGR_P (fn
) = 1;
1204 DECL_ARTIFICIAL (fn
) = 1;
1205 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1206 DECL_DECLARED_INLINE_P (fn
) = 1;
1207 DECL_DECLARED_CONSTEXPR_P (fn
) = DECL_DECLARED_CONSTEXPR_P (callop
);
1208 if (DECL_IMMEDIATE_FUNCTION_P (callop
))
1209 SET_DECL_IMMEDIATE_FUNCTION_P (fn
);
1210 DECL_ARGUMENTS (fn
) = build_this_parm (fn
, fntype
, TYPE_QUAL_CONST
);
1213 DECL_INTERFACE_KNOWN (fn
) = 1;
1215 if (generic_lambda_p
)
1216 fn
= add_inherited_template_parms (fn
, DECL_TI_TEMPLATE (callop
));
1218 add_method (type
, fn
, false);
1220 /* Generic thunk code fails for varargs; we'll complain in mark_used if
1221 the conversion op is used. */
1222 if (varargs_function_p (callop
))
1224 DECL_DELETED_FN (fn
) = 1;
1228 /* Now build up the thunk to be returned. */
1230 tree statfn
= build_lang_decl (FUNCTION_DECL
, fun_identifier
, stattype
);
1231 SET_DECL_LANGUAGE (statfn
, lang_cplusplus
);
1233 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (callop
);
1234 grokclassfn (type
, fn
, NO_SPECIAL
);
1235 set_linkage_according_to_type (type
, fn
);
1236 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
1237 DECL_IN_AGGR_P (fn
) = 1;
1238 DECL_ARTIFICIAL (fn
) = 1;
1239 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1240 DECL_DECLARED_INLINE_P (fn
) = 1;
1241 DECL_STATIC_FUNCTION_P (fn
) = 1;
1242 DECL_DECLARED_CONSTEXPR_P (fn
) = DECL_DECLARED_CONSTEXPR_P (callop
);
1243 if (DECL_IMMEDIATE_FUNCTION_P (callop
))
1244 SET_DECL_IMMEDIATE_FUNCTION_P (fn
);
1245 DECL_ARGUMENTS (fn
) = fn_args
;
1246 for (tree arg
= fn_args
; arg
; arg
= DECL_CHAIN (arg
))
1248 /* Avoid duplicate -Wshadow warnings. */
1249 DECL_NAME (arg
) = NULL_TREE
;
1250 DECL_CONTEXT (arg
) = fn
;
1253 DECL_INTERFACE_KNOWN (fn
) = 1;
1255 if (generic_lambda_p
)
1256 fn
= add_inherited_template_parms (fn
, DECL_TI_TEMPLATE (callop
));
1258 if (flag_sanitize
& SANITIZE_NULL
)
1259 /* Don't UBsan this function; we're deliberately calling op() with a null
1261 add_no_sanitize_value (fn
, SANITIZE_UNDEFINED
);
1263 add_method (type
, fn
, false);
1266 push_function_context ();
1268 /* Still increment function_depth so that we don't GC in the
1269 middle of an expression. */
1272 /* Generate the body of the thunk. */
1274 start_preparsed_function (statfn
, NULL_TREE
,
1275 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1276 tree body
= begin_function_body ();
1277 tree compound_stmt
= begin_compound_stmt (0);
1278 if (!generic_lambda_p
)
1280 set_flags_from_callee (call
);
1281 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call
)))
1282 call
= build_cplus_new (TREE_TYPE (call
), call
, tf_warning_or_error
);
1284 call
= convert_from_reference (call
);
1285 finish_return_stmt (call
);
1287 finish_compound_stmt (compound_stmt
);
1288 finish_function_body (body
);
1290 fn
= finish_function (/*inline_p=*/true);
1291 if (!generic_lambda_p
)
1292 expand_or_defer_fn (fn
);
1294 /* Generate the body of the conversion op. */
1296 start_preparsed_function (convfn
, NULL_TREE
,
1297 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1298 body
= begin_function_body ();
1299 compound_stmt
= begin_compound_stmt (0);
1301 /* decl_needed_p needs to see that it's used. */
1302 TREE_USED (statfn
) = 1;
1303 finish_return_stmt (decay_conversion (statfn
, tf_warning_or_error
));
1305 finish_compound_stmt (compound_stmt
);
1306 finish_function_body (body
);
1308 fn
= finish_function (/*inline_p=*/true);
1309 if (!generic_lambda_p
)
1310 expand_or_defer_fn (fn
);
1313 pop_function_context ();
1318 /* True if FN is the static function "_FUN" that gets returned from the lambda
1319 conversion operator. */
1322 lambda_static_thunk_p (tree fn
)
1324 return (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1325 && DECL_ARTIFICIAL (fn
)
1326 && DECL_STATIC_FUNCTION_P (fn
)
1327 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn
)));
1331 call_from_lambda_thunk_p (tree call
)
1333 return (CALL_FROM_THUNK_P (call
)
1334 && lambda_static_thunk_p (current_function_decl
));
1337 /* Returns true iff VAL is a lambda-related declaration which should
1338 be ignored by unqualified lookup. */
1341 is_lambda_ignored_entity (tree val
)
1343 /* Look past normal, non-VLA capture proxies. */
1344 if (is_normal_capture_proxy (val
)
1345 && !variably_modified_type_p (TREE_TYPE (val
), NULL_TREE
))
1348 /* Always ignore lambda fields, their names are only for debugging. */
1349 if (TREE_CODE (val
) == FIELD_DECL
1350 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val
)))
1353 /* None of the lookups that use qualify_lookup want the op() from the
1354 lambda; they want the one from the enclosing class. */
1355 if (TREE_CODE (val
) == FUNCTION_DECL
&& LAMBDA_FUNCTION_P (val
))
1361 /* Lambdas that appear in variable initializer or default argument scope
1362 get that in their mangling, so we need to record it. We might as well
1363 use the count for function and namespace scopes as well. */
1364 static GTY(()) tree lambda_scope
;
1365 static GTY(()) int lambda_count
;
1366 struct GTY(()) tree_int
1371 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
1374 start_lambda_scope (tree decl
)
1378 /* Once we're inside a function, we ignore variable scope and just push
1379 the function again so that popping works properly. */
1380 if (current_function_decl
&& TREE_CODE (decl
) == VAR_DECL
)
1381 decl
= current_function_decl
;
1382 ti
.t
= lambda_scope
;
1383 ti
.i
= lambda_count
;
1384 vec_safe_push (lambda_scope_stack
, ti
);
1385 if (lambda_scope
!= decl
)
1387 /* Don't reset the count if we're still in the same function. */
1388 lambda_scope
= decl
;
1394 record_lambda_scope (tree lambda
)
1396 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
1397 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
1400 tree closure
= LAMBDA_EXPR_CLOSURE (lambda
);
1401 gcc_checking_assert (closure
);
1402 maybe_attach_decl (lambda_scope
, TYPE_NAME (closure
));
1406 /* This lambda is an instantiation of a lambda in a template default argument
1407 that got no LAMBDA_EXPR_EXTRA_SCOPE, so this shouldn't either. But we do
1408 need to use and increment the global count to avoid collisions. */
1411 record_null_lambda_scope (tree lambda
)
1413 if (vec_safe_is_empty (lambda_scope_stack
))
1414 record_lambda_scope (lambda
);
1417 tree_int
*p
= lambda_scope_stack
->begin();
1418 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = p
->t
;
1419 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = p
->i
++;
1421 gcc_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda
) == NULL_TREE
);
1425 finish_lambda_scope (void)
1427 tree_int
*p
= &lambda_scope_stack
->last ();
1428 if (lambda_scope
!= p
->t
)
1430 lambda_scope
= p
->t
;
1431 lambda_count
= p
->i
;
1433 lambda_scope_stack
->pop ();
1437 start_lambda_function (tree fco
, tree lambda_expr
)
1439 /* Let the front end know that we are going to be defining this
1441 start_preparsed_function (fco
,
1443 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
1445 tree body
= begin_function_body ();
1447 /* Push the proxies for any explicit captures. */
1448 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
1449 cap
= TREE_CHAIN (cap
))
1450 build_capture_proxy (TREE_PURPOSE (cap
), TREE_VALUE (cap
));
1455 /* Subroutine of prune_lambda_captures: CAP is a node in
1456 LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
1457 might optimize away the capture, or NULL_TREE if there is no such
1461 var_to_maybe_prune (tree cap
)
1463 if (LAMBDA_CAPTURE_EXPLICIT_P (cap
))
1464 /* Don't prune explicit captures. */
1467 tree mem
= TREE_PURPOSE (cap
);
1468 if (!DECL_P (mem
) || !DECL_NORMAL_CAPTURE_P (mem
))
1469 /* Packs and init-captures aren't captures of constant vars. */
1472 tree init
= TREE_VALUE (cap
);
1473 if (is_normal_capture_proxy (init
))
1474 init
= DECL_CAPTURED_VARIABLE (init
);
1475 if (decl_constant_var_p (init
))
1481 /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
1482 for constant variables are actually used in the lambda body.
1484 There will always be a DECL_EXPR for the capture proxy; remember it when we
1485 see it, but replace it with any other use. */
1488 mark_const_cap_r (tree
*t
, int *walk_subtrees
, void *data
)
1490 hash_map
<tree
,tree
*> &const_vars
= *(hash_map
<tree
,tree
*>*)data
;
1492 tree var
= NULL_TREE
;
1493 if (TREE_CODE (*t
) == DECL_EXPR
)
1495 tree decl
= DECL_EXPR_DECL (*t
);
1496 if (is_constant_capture_proxy (decl
))
1498 var
= DECL_CAPTURED_VARIABLE (decl
);
1502 else if (is_constant_capture_proxy (*t
))
1503 var
= DECL_CAPTURED_VARIABLE (*t
);
1507 tree
*&slot
= const_vars
.get_or_insert (var
);
1508 if (!slot
|| VAR_P (*t
))
1515 /* We're at the end of processing a lambda; go back and remove any captures of
1516 constant variables for which we've folded away all uses. */
1519 prune_lambda_captures (tree body
)
1521 tree lam
= current_lambda_expr ();
1522 if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam
))
1523 /* No uses were optimized away. */
1525 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam
) == CPLD_NONE
)
1526 /* No default captures, and we don't prune explicit captures. */
1529 hash_map
<tree
,tree
*> const_vars
;
1531 cp_walk_tree_without_duplicates (&body
, mark_const_cap_r
, &const_vars
);
1533 tree
*fieldp
= &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam
));
1534 for (tree
*capp
= &LAMBDA_EXPR_CAPTURE_LIST (lam
); *capp
; )
1537 if (tree var
= var_to_maybe_prune (cap
))
1539 tree
**use
= const_vars
.get (var
);
1540 if (use
&& TREE_CODE (**use
) == DECL_EXPR
)
1542 /* All uses of this capture were folded away, leaving only the
1543 proxy declaration. */
1545 /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
1546 *capp
= TREE_CHAIN (cap
);
1548 /* And out of TYPE_FIELDS. */
1549 tree field
= TREE_PURPOSE (cap
);
1550 while (*fieldp
!= field
)
1551 fieldp
= &DECL_CHAIN (*fieldp
);
1552 *fieldp
= DECL_CHAIN (*fieldp
);
1554 /* And remove the capture proxy declaration. */
1560 capp
= &TREE_CHAIN (cap
);
1565 finish_lambda_function (tree body
)
1567 finish_function_body (body
);
1569 prune_lambda_captures (body
);
1571 /* Finish the function and generate code for it if necessary. */
1572 tree fn
= finish_function (/*inline_p=*/true);
1574 /* Only expand if the call op is not a template. */
1575 if (!DECL_TEMPLATE_INFO (fn
))
1576 expand_or_defer_fn (fn
);
1579 #include "gt-cp-lambda.h"