Branch data Line data Source code
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.
5 : :
6 : : Copyright (C) 1998-2025 Free Software Foundation, Inc.
7 : :
8 : : This file is part of GCC.
9 : :
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)
13 : : any later version.
14 : :
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.
19 : :
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/>. */
23 : :
24 : : #include "config.h"
25 : : #include "system.h"
26 : : #include "coretypes.h"
27 : : #include "cp-tree.h"
28 : : #include "stringpool.h"
29 : : #include "cgraph.h"
30 : : #include "tree-iterator.h"
31 : : #include "toplev.h"
32 : : #include "gimplify.h"
33 : : #include "target.h"
34 : : #include "decl.h"
35 : : #include "flags.h"
36 : :
37 : : /* Constructor for a lambda expression. */
38 : :
39 : : tree
40 : 903266 : build_lambda_expr (void)
41 : : {
42 : 903266 : tree lambda = make_node (LAMBDA_EXPR);
43 : 903266 : LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
44 : 903266 : LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
45 : 903266 : LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
46 : 903266 : LAMBDA_EXPR_REGEN_INFO (lambda) = NULL_TREE;
47 : 903266 : LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
48 : 903266 : return lambda;
49 : : }
50 : :
51 : : /* Create the closure object for a LAMBDA_EXPR. */
52 : :
53 : : tree
54 : 903325 : build_lambda_object (tree lambda_expr)
55 : : {
56 : : /* Build aggregate constructor call.
57 : : - cp_parser_braced_list
58 : : - cp_parser_functional_cast */
59 : 903325 : vec<constructor_elt, va_gc> *elts = NULL;
60 : 903325 : tree node, expr, type;
61 : :
62 : 903325 : if (processing_template_decl && !in_template_context
63 : 75 : && current_binding_level->requires_expression)
64 : : /* As in cp_parser_lambda_expression, don't get confused by
65 : : cp_parser_requires_expression setting processing_template_decl. In that
66 : : case we want to return the result of finish_compound_literal, to avoid
67 : : tsubst_lambda_expr. */;
68 : 903310 : else if (processing_template_decl || lambda_expr == error_mark_node)
69 : : return lambda_expr;
70 : :
71 : : /* Make sure any error messages refer to the lambda-introducer. */
72 : 294838 : location_t loc = LAMBDA_EXPR_LOCATION (lambda_expr);
73 : 294838 : iloc_sentinel il (loc);
74 : :
75 : 294838 : for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
76 : 862076 : node;
77 : 567238 : node = TREE_CHAIN (node))
78 : : {
79 : 567238 : tree field = TREE_PURPOSE (node);
80 : 567238 : tree val = TREE_VALUE (node);
81 : :
82 : 567238 : if (field == error_mark_node)
83 : : {
84 : 0 : expr = error_mark_node;
85 : 0 : goto out;
86 : : }
87 : :
88 : 567238 : if (TREE_CODE (val) == TREE_LIST)
89 : 0 : val = build_x_compound_expr_from_list (val, ELK_INIT,
90 : : tf_warning_or_error);
91 : :
92 : 567238 : if (DECL_P (val))
93 : 321039 : mark_used (val);
94 : :
95 : : /* Mere mortals can't copy arrays with aggregate initialization, so
96 : : do some magic to make it work here. */
97 : 567238 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
98 : 19 : val = build_array_copy (val);
99 : 567219 : else if (DECL_NORMAL_CAPTURE_P (field)
100 : 566665 : && !DECL_VLA_CAPTURE_P (field)
101 : 1133842 : && !TYPE_REF_P (TREE_TYPE (field)))
102 : : {
103 : : /* "the entities that are captured by copy are used to
104 : : direct-initialize each corresponding non-static data
105 : : member of the resulting closure object."
106 : :
107 : : There's normally no way to express direct-initialization
108 : : from an element of a CONSTRUCTOR, so we build up a special
109 : : TARGET_EXPR to bypass the usual copy-initialization. */
110 : 209976 : val = force_rvalue (val, tf_warning_or_error);
111 : 209976 : if (TREE_CODE (val) == TARGET_EXPR)
112 : 554 : TARGET_EXPR_DIRECT_INIT_P (val) = true;
113 : : }
114 : :
115 : 567238 : CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
116 : : }
117 : :
118 : 294838 : expr = build_constructor (init_list_type_node, elts);
119 : 294838 : CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
120 : :
121 : : /* N2927: "[The closure] class type is not an aggregate."
122 : : But we briefly treat it as an aggregate to make this simpler. */
123 : 294838 : type = LAMBDA_EXPR_CLOSURE (lambda_expr);
124 : 294838 : CLASSTYPE_NON_AGGREGATE (type) = 0;
125 : 294838 : expr = finish_compound_literal (type, expr, tf_warning_or_error);
126 : 294838 : protected_set_expr_location (expr, loc);
127 : 294838 : CLASSTYPE_NON_AGGREGATE (type) = 1;
128 : :
129 : 294838 : out:
130 : 294838 : return expr;
131 : 294838 : }
132 : :
133 : : /* Return an initialized RECORD_TYPE for LAMBDA.
134 : : LAMBDA must have its explicit captures already. */
135 : :
136 : : tree
137 : 903257 : begin_lambda_type (tree lambda)
138 : : {
139 : : /* Lambda names are nearly but not quite anonymous. */
140 : 903257 : tree name = make_anon_name ();
141 : 903257 : IDENTIFIER_LAMBDA_P (name) = true;
142 : :
143 : : /* Create the new RECORD_TYPE for this lambda. */
144 : 903257 : tree type = xref_tag (/*tag_code=*/record_type, name);
145 : 903257 : if (type == error_mark_node)
146 : : return error_mark_node;
147 : :
148 : : /* Designate it as a struct so that we can use aggregate initialization. */
149 : 903257 : CLASSTYPE_DECLARED_CLASS (type) = false;
150 : :
151 : : /* Cross-reference the expression and the type. */
152 : 903257 : LAMBDA_EXPR_CLOSURE (lambda) = type;
153 : 903257 : SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
154 : :
155 : : /* In C++17, assume the closure is literal; we'll clear the flag later if
156 : : necessary. */
157 : 903257 : if (cxx_dialect >= cxx17)
158 : 899955 : CLASSTYPE_LITERAL_P (type) = true;
159 : :
160 : : /* Clear base types. */
161 : 903257 : xref_basetypes (type, /*bases=*/NULL_TREE);
162 : :
163 : : /* Start the class. */
164 : 903257 : type = begin_class_definition (type);
165 : :
166 : 903257 : return type;
167 : : }
168 : :
169 : : /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
170 : : closure type. */
171 : :
172 : : tree
173 : 7619895 : lambda_function (tree lambda)
174 : : {
175 : 7619895 : tree type;
176 : 7619895 : if (TREE_CODE (lambda) == LAMBDA_EXPR)
177 : 3108106 : type = LAMBDA_EXPR_CLOSURE (lambda);
178 : : else
179 : : type = lambda;
180 : 15239790 : gcc_assert (LAMBDA_TYPE_P (type));
181 : : /* Don't let debug_tree cause instantiation. */
182 : 7619895 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
183 : 7619895 : && !COMPLETE_OR_OPEN_TYPE_P (type))
184 : : return NULL_TREE;
185 : 7619895 : lambda = get_class_binding_direct (type, call_op_identifier);
186 : 7619895 : if (lambda)
187 : 7619865 : lambda = STRIP_TEMPLATE (get_first_fn (lambda));
188 : : return lambda;
189 : : }
190 : :
191 : : /* True if EXPR is an expression whose type can be used directly in lambda
192 : : capture. Not to be used for 'auto'. */
193 : :
194 : : static bool
195 : 1040464 : type_deducible_expression_p (tree expr)
196 : : {
197 : 1040464 : if (!type_dependent_expression_p (expr))
198 : : return true;
199 : 0 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
200 : 596092 : || TREE_CODE (expr) == EXPR_PACK_EXPANSION)
201 : : return false;
202 : 596092 : tree t = non_reference (TREE_TYPE (expr));
203 : 596092 : return (t && TREE_CODE (t) != TYPE_PACK_EXPANSION
204 : 285250 : && !WILDCARD_TYPE_P (t) && !LAMBDA_TYPE_P (t)
205 : 342919 : && !array_of_unknown_bound_p (t)
206 : 938988 : && !type_uses_auto (t));
207 : : }
208 : :
209 : : /* Returns the type to use for the FIELD_DECL corresponding to the
210 : : capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
211 : : C++14 init capture, and BY_REFERENCE_P indicates whether we're
212 : : capturing by reference. */
213 : :
214 : : tree
215 : 1047451 : lambda_capture_field_type (tree expr, bool explicit_init_p,
216 : : bool by_reference_p)
217 : : {
218 : 1047451 : tree type;
219 : 1047451 : bool is_this = is_this_parameter (tree_strip_nop_conversions (expr));
220 : :
221 : 1047451 : if (explicit_init_p)
222 : : {
223 : 6987 : tree auto_node = make_auto ();
224 : :
225 : 6987 : type = auto_node;
226 : 6987 : if (by_reference_p)
227 : : /* Add the reference now, so deduction doesn't lose
228 : : outermost CV qualifiers of EXPR. */
229 : 62 : type = build_reference_type (type);
230 : 6987 : if (uses_parameter_packs (expr))
231 : : /* Stick with 'auto' even if the type could be deduced. */
232 : 38 : TEMPLATE_TYPE_PARAMETER_PACK (auto_node) = true;
233 : : else
234 : 6949 : type = do_auto_deduction (type, expr, auto_node);
235 : : }
236 : 1040464 : else if (!type_deducible_expression_p (expr))
237 : : {
238 : 253199 : type = cxx_make_type (DECLTYPE_TYPE);
239 : 253199 : DECLTYPE_TYPE_EXPR (type) = expr;
240 : 253199 : DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
241 : 253199 : DECLTYPE_FOR_REF_CAPTURE (type) = by_reference_p;
242 : 253199 : SET_TYPE_STRUCTURAL_EQUALITY (type);
243 : : }
244 : : else
245 : : {
246 : 787265 : STRIP_ANY_LOCATION_WRAPPER (expr);
247 : :
248 : 787265 : if (!by_reference_p && is_capture_proxy (expr))
249 : : {
250 : : /* When capturing by-value another capture proxy from an enclosing
251 : : lambda, consider the type of the corresponding field instead,
252 : : as the proxy may be additionally const-qualifed if the enclosing
253 : : lambda is non-mutable (PR94376). */
254 : 114 : gcc_assert (TREE_CODE (DECL_VALUE_EXPR (expr)) == COMPONENT_REF);
255 : 114 : expr = TREE_OPERAND (DECL_VALUE_EXPR (expr), 1);
256 : : }
257 : :
258 : 787265 : type = non_reference (unlowered_expr_type (expr));
259 : :
260 : 787265 : if ((by_reference_p && !is_this) || TREE_CODE (type) == FUNCTION_TYPE)
261 : 404807 : type = build_reference_type (type);
262 : : }
263 : :
264 : 1047451 : return type;
265 : : }
266 : :
267 : : /* Returns true iff DECL is a lambda capture proxy variable created by
268 : : build_capture_proxy. */
269 : :
270 : : bool
271 : 4432041128 : is_capture_proxy (tree decl)
272 : : {
273 : : /* Location wrappers should be stripped or otherwise handled by the
274 : : caller before using this predicate. */
275 : 4432041128 : gcc_checking_assert (!location_wrapper_p (decl));
276 : :
277 : 4432041128 : return (VAR_P (decl)
278 : 1215265852 : && DECL_HAS_VALUE_EXPR_P (decl)
279 : 19200457 : && !DECL_ANON_UNION_VAR_P (decl)
280 : 19198384 : && !DECL_DECOMPOSITION_P (decl)
281 : 18041642 : && !DECL_FNAME_P (decl)
282 : 17739434 : && !(DECL_ARTIFICIAL (decl)
283 : 17738385 : && DECL_LANG_SPECIFIC (decl)
284 : 17628952 : && DECL_OMP_PRIVATIZED_MEMBER (decl))
285 : 4467496109 : && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
286 : : }
287 : :
288 : : /* Returns true iff DECL is a capture proxy for a normal capture
289 : : (i.e. without explicit initializer). */
290 : :
291 : : bool
292 : 4309883921 : is_normal_capture_proxy (tree decl)
293 : : {
294 : 4309883921 : if (!is_capture_proxy (decl))
295 : : /* It's not a capture proxy. */
296 : : return false;
297 : :
298 : 13189367 : return (DECL_LANG_SPECIFIC (decl)
299 : 13189367 : && DECL_CAPTURED_VARIABLE (decl));
300 : : }
301 : :
302 : : /* If DECL is a normal capture proxy, return the variable it captures.
303 : : Otherwise, just return DECL. */
304 : :
305 : : tree
306 : 2280956 : strip_normal_capture_proxy (tree decl)
307 : : {
308 : 2300425 : while (is_normal_capture_proxy (decl))
309 : 19469 : decl = DECL_CAPTURED_VARIABLE (decl);
310 : 2280956 : return decl;
311 : : }
312 : :
313 : : /* Returns true iff DECL is a capture proxy for a normal capture
314 : : of a constant variable. */
315 : :
316 : : bool
317 : 5375 : is_constant_capture_proxy (tree decl)
318 : : {
319 : 5375 : if (is_normal_capture_proxy (decl))
320 : 440 : return decl_constant_var_p (DECL_CAPTURED_VARIABLE (decl));
321 : : return false;
322 : : }
323 : :
324 : : /* VAR is a capture proxy created by build_capture_proxy; add it to the
325 : : current function, which is the operator() for the appropriate lambda. */
326 : :
327 : : void
328 : 1501976 : insert_capture_proxy (tree var)
329 : : {
330 : 1501976 : if (is_normal_capture_proxy (var))
331 : : {
332 : 1494652 : tree cap = DECL_CAPTURED_VARIABLE (var);
333 : 1494652 : if (CHECKING_P)
334 : : {
335 : 1494652 : gcc_assert (!is_normal_capture_proxy (cap));
336 : 1494652 : tree old = retrieve_local_specialization (cap);
337 : 1494652 : if (old)
338 : 13981 : gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var));
339 : : }
340 : 1494652 : register_local_specialization (var, cap);
341 : : }
342 : :
343 : : /* Put the capture proxy in the extra body block so that it won't clash
344 : : with a later local variable. */
345 : 1501976 : pushdecl_outermost_localscope (var);
346 : :
347 : : /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
348 : 1501976 : var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
349 : : /* The first stmt_list is from start_preparsed_function. Then there's a
350 : : possible stmt_list from begin_eh_spec_block, then the one from the
351 : : lambda's outer {}. */
352 : 1501976 : unsigned index = 1 + use_eh_spec_block (current_function_decl);
353 : 1501976 : tree stmt_list = (*stmt_list_stack)[index];
354 : 1501976 : gcc_assert (stmt_list);
355 : 1501976 : append_to_statement_list_force (var, &stmt_list);
356 : 1501976 : }
357 : :
358 : : /* We've just finished processing a lambda; if the containing scope is also
359 : : a lambda, insert any capture proxies that were created while processing
360 : : the nested lambda. */
361 : :
362 : : void
363 : 903257 : insert_pending_capture_proxies (void)
364 : : {
365 : 903257 : tree lam;
366 : 903257 : vec<tree, va_gc> *proxies;
367 : 903257 : unsigned i;
368 : :
369 : 954292 : if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
370 : : return;
371 : :
372 : 18747 : lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
373 : 18747 : proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
374 : 21468 : for (i = 0; i < vec_safe_length (proxies); ++i)
375 : : {
376 : 2721 : tree var = (*proxies)[i];
377 : 2721 : insert_capture_proxy (var);
378 : : }
379 : 18747 : release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
380 : 18747 : LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
381 : : }
382 : :
383 : : /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
384 : : return the type we want the proxy to have: the type of the field itself,
385 : : with added const-qualification if the lambda isn't mutable and the
386 : : capture is by value. */
387 : :
388 : : tree
389 : 1018279 : lambda_proxy_type (tree ref)
390 : : {
391 : 1018279 : tree type;
392 : 1018279 : if (ref == error_mark_node)
393 : : return error_mark_node;
394 : 1018270 : if (REFERENCE_REF_P (ref))
395 : 0 : ref = TREE_OPERAND (ref, 0);
396 : 1018270 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
397 : 1018270 : type = TREE_TYPE (ref);
398 : 1018270 : if (!type || WILDCARD_TYPE_P (non_reference (type)))
399 : : {
400 : 258451 : type = cxx_make_type (DECLTYPE_TYPE);
401 : 258451 : DECLTYPE_TYPE_EXPR (type) = ref;
402 : 258451 : DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
403 : 258451 : SET_TYPE_STRUCTURAL_EQUALITY (type);
404 : : }
405 : 1018270 : if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
406 : 1683 : type = make_pack_expansion (type);
407 : : return type;
408 : : }
409 : :
410 : : /* MEMBER is a capture field in a lambda closure class. Now that we're
411 : : inside the operator(), build a placeholder var for future lookups and
412 : : debugging. */
413 : :
414 : : static tree
415 : 1257512 : build_capture_proxy (tree member, tree init)
416 : : {
417 : 1257512 : tree var, object, fn, closure, name, lam, type;
418 : :
419 : 1257512 : if (PACK_EXPANSION_P (member))
420 : 1674 : member = PACK_EXPANSION_PATTERN (member);
421 : :
422 : 1257512 : closure = DECL_CONTEXT (member);
423 : 1257512 : fn = lambda_function (closure);
424 : 1257512 : lam = CLASSTYPE_LAMBDA_EXPR (closure);
425 : :
426 : 1257512 : object = DECL_ARGUMENTS (fn);
427 : : /* The proxy variable forwards to the capture field. */
428 : 1257512 : if (INDIRECT_TYPE_P (TREE_TYPE (object)))
429 : 1257372 : object = build_fold_indirect_ref (object);
430 : 1257512 : object = finish_non_static_data_member (member, object, NULL_TREE);
431 : 1257512 : if (REFERENCE_REF_P (object))
432 : 572402 : object = TREE_OPERAND (object, 0);
433 : :
434 : : /* Remove the __ inserted by add_capture. */
435 : 1257512 : if (IDENTIFIER_POINTER (DECL_NAME (member))[2] == '_'
436 : 1257512 : && IDENTIFIER_POINTER (DECL_NAME (member))[3] == '.')
437 : 18 : name = get_identifier ("_");
438 : : else
439 : 1257494 : name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
440 : :
441 : 1257512 : if (name == this_identifier && TYPE_PTR_P (TREE_TYPE (member)))
442 : : /* Avoid DECLTYPE_TYPE for by-ref 'this' capture in an xobj lambda; the
443 : : constness of the closure doesn't matter just like it doesn't matter to
444 : : other by-ref capture. It's simpler to handle this special case here
445 : : than in lambda_proxy_type. */
446 : 239418 : type = TREE_TYPE (member);
447 : : else
448 : : {
449 : 1018094 : type = lambda_proxy_type (object);
450 : 1018094 : if (name == this_identifier)
451 : : {
452 : 133 : type = build_pointer_type (type);
453 : 133 : type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
454 : 133 : object = build_fold_addr_expr_with_type (object, type);
455 : : }
456 : : }
457 : :
458 : 1257512 : if (DECL_VLA_CAPTURE_P (member))
459 : : {
460 : : /* Rebuild the VLA type from the pointer and maxindex. */
461 : 45 : tree field = next_aggregate_field (TYPE_FIELDS (type));
462 : 45 : tree ptr = build_simple_component_ref (object, field);
463 : 45 : field = next_aggregate_field (DECL_CHAIN (field));
464 : 45 : tree max = build_simple_component_ref (object, field);
465 : 45 : type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
466 : : build_index_type (max));
467 : 45 : type = build_reference_type (type);
468 : 45 : object = convert (type, ptr);
469 : : }
470 : :
471 : 1257512 : complete_type (type);
472 : :
473 : 1257512 : var = build_decl (input_location, VAR_DECL, name, type);
474 : 1257512 : SET_DECL_VALUE_EXPR (var, object);
475 : 1257512 : DECL_HAS_VALUE_EXPR_P (var) = 1;
476 : 1257512 : DECL_ARTIFICIAL (var) = 1;
477 : 1257512 : TREE_USED (var) = 1;
478 : 1257512 : DECL_CONTEXT (var) = fn;
479 : :
480 : 1257512 : if (DECL_NORMAL_CAPTURE_P (member))
481 : : {
482 : 1250260 : if (DECL_VLA_CAPTURE_P (member))
483 : : {
484 : 42 : init = CONSTRUCTOR_ELT (init, 0)->value;
485 : 42 : init = TREE_OPERAND (init, 0); // Strip ADDR_EXPR.
486 : 42 : init = TREE_OPERAND (init, 0); // Strip ARRAY_REF.
487 : : }
488 : : else
489 : : {
490 : 1250218 : if (PACK_EXPANSION_P (init))
491 : 1648 : init = PACK_EXPANSION_PATTERN (init);
492 : : }
493 : :
494 : 1250260 : if (INDIRECT_REF_P (init))
495 : 291604 : init = TREE_OPERAND (init, 0);
496 : 1250260 : STRIP_NOPS (init);
497 : :
498 : 1250260 : gcc_assert (VAR_P (init) || TREE_CODE (init) == PARM_DECL);
499 : 1250260 : init = strip_normal_capture_proxy (init);
500 : 1250260 : retrofit_lang_decl (var);
501 : 1250260 : DECL_CAPTURED_VARIABLE (var) = init;
502 : : }
503 : :
504 : 1257512 : if (name == this_identifier)
505 : : {
506 : 239551 : gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
507 : 239551 : LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
508 : : }
509 : :
510 : 1257512 : if (fn == current_function_decl)
511 : 1254791 : insert_capture_proxy (var);
512 : : else
513 : 2721 : vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
514 : :
515 : 1257512 : return var;
516 : : }
517 : :
518 : : static GTY(()) tree ptr_id;
519 : : static GTY(()) tree max_id;
520 : :
521 : : /* Return a struct containing a pointer and a length for lambda capture of
522 : : an array of runtime length. */
523 : :
524 : : static tree
525 : 45 : vla_capture_type (tree array_type)
526 : : {
527 : 45 : tree type = xref_tag (record_type, make_anon_name ());
528 : 45 : xref_basetypes (type, NULL_TREE);
529 : 45 : type = begin_class_definition (type);
530 : 45 : if (!ptr_id)
531 : : {
532 : 36 : ptr_id = get_identifier ("ptr");
533 : 36 : max_id = get_identifier ("max");
534 : : }
535 : 45 : tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
536 : 45 : tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
537 : 45 : finish_member_declaration (field);
538 : 45 : field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
539 : 45 : finish_member_declaration (field);
540 : 45 : return finish_struct (type, NULL_TREE);
541 : : }
542 : :
543 : : /* From an ID and INITIALIZER, create a capture (by reference if
544 : : BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
545 : : and return it. If ID is `this', BY_REFERENCE_P says whether
546 : : `*this' is captured by reference. */
547 : :
548 : : tree
549 : 907022 : add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
550 : : bool explicit_init_p, unsigned *name_independent_cnt)
551 : : {
552 : 907022 : char *buf;
553 : 907022 : tree type, member, name;
554 : 907022 : bool vla = false;
555 : 907022 : bool variadic = false;
556 : 907022 : tree initializer = orig_init;
557 : :
558 : 907022 : if (PACK_EXPANSION_P (initializer))
559 : : {
560 : 1674 : initializer = PACK_EXPANSION_PATTERN (initializer);
561 : : variadic = true;
562 : : }
563 : :
564 : 907022 : if (TREE_CODE (initializer) == TREE_LIST
565 : : /* A pack expansion might end up with multiple elements. */
566 : 907022 : && !PACK_EXPANSION_P (TREE_VALUE (initializer)))
567 : 9 : initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
568 : : tf_warning_or_error);
569 : 907022 : type = TREE_TYPE (initializer);
570 : 907022 : if (type == error_mark_node)
571 : : return error_mark_node;
572 : :
573 : 907001 : if (!dependent_type_p (type) && array_of_runtime_bound_p (type))
574 : : {
575 : 45 : vla = true;
576 : 45 : if (!by_reference_p)
577 : 3 : error ("array of runtime bound cannot be captured by copy, "
578 : : "only by reference");
579 : :
580 : : /* For a VLA, we capture the address of the first element and the
581 : : maximum index, and then reconstruct the VLA for the proxy. */
582 : 45 : tree elt = cp_build_array_ref (input_location, initializer,
583 : : integer_zero_node, tf_warning_or_error);
584 : 45 : initializer = build_constructor_va (init_list_type_node, 2,
585 : : NULL_TREE, build_address (elt),
586 : : NULL_TREE,
587 : : array_type_nelts_minus_one (type));
588 : 45 : type = vla_capture_type (type);
589 : : }
590 : 906956 : else if (!dependent_type_p (type)
591 : 906956 : && variably_modified_type_p (type, NULL_TREE))
592 : : {
593 : 18 : auto_diagnostic_group d;
594 : 18 : sorry ("capture of variably-modified type %qT that is not an N3639 array "
595 : : "of runtime bound", type);
596 : 18 : if (TREE_CODE (type) == ARRAY_TYPE
597 : 18 : && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
598 : 12 : inform (input_location, "because the array element type %qT has "
599 : 12 : "variable size", TREE_TYPE (type));
600 : 18 : return error_mark_node;
601 : 18 : }
602 : : else
603 : : {
604 : 906938 : type = lambda_capture_field_type (initializer, explicit_init_p,
605 : : by_reference_p);
606 : 906938 : if (type == error_mark_node)
607 : : return error_mark_node;
608 : :
609 : 906935 : if (id == this_identifier && !by_reference_p)
610 : : {
611 : 127 : gcc_assert (INDIRECT_TYPE_P (type));
612 : 127 : type = TREE_TYPE (type);
613 : 127 : initializer = cp_build_fold_indirect_ref (initializer);
614 : : }
615 : :
616 : 906935 : if (dependent_type_p (type))
617 : : ;
618 : 307007 : else if (id != this_identifier && by_reference_p)
619 : : {
620 : 114248 : if (!lvalue_p (initializer))
621 : : {
622 : 3 : error ("cannot capture %qE by reference", initializer);
623 : 3 : return error_mark_node;
624 : : }
625 : : }
626 : : else
627 : : {
628 : : /* Capture by copy requires a complete type. */
629 : 192759 : type = complete_type (type);
630 : 192759 : if (!COMPLETE_TYPE_P (type))
631 : : {
632 : 6 : auto_diagnostic_group d;
633 : 6 : error ("capture by copy of incomplete type %qT", type);
634 : 6 : cxx_incomplete_type_inform (type);
635 : 6 : return error_mark_node;
636 : 6 : }
637 : 192753 : else if (!verify_type_context (input_location,
638 : : TCTX_CAPTURE_BY_COPY, type))
639 : 0 : return error_mark_node;
640 : : }
641 : :
642 : 906926 : if (cxx_dialect < cxx20 && !explicit_init_p)
643 : : {
644 : 354910 : auto_diagnostic_group d;
645 : 354910 : tree stripped_init = tree_strip_any_location_wrapper (initializer);
646 : 112434 : if (DECL_DECOMPOSITION_P (stripped_init)
647 : 467344 : && pedwarn (input_location, OPT_Wc__20_extensions,
648 : : "captured structured bindings are a C++20 extension"))
649 : 17 : inform (DECL_SOURCE_LOCATION (stripped_init), "declared here");
650 : 354910 : }
651 : : }
652 : :
653 : : /* Add __ to the beginning of the field name so that user code
654 : : won't find the field with name lookup. We can't just leave the name
655 : : unset because template instantiation uses the name to find
656 : : instantiated fields. */
657 : 906971 : if (id_equal (id, "_") && name_independent_cnt)
658 : : {
659 : 48 : if (*name_independent_cnt == 0)
660 : 30 : name = get_identifier ("___");
661 : : else
662 : : {
663 : : /* For 2nd and later name-independent capture use
664 : : unique names. */
665 : 18 : char buf2[5 + (HOST_BITS_PER_INT + 2) / 3];
666 : 18 : sprintf (buf2, "___.%u", *name_independent_cnt);
667 : 18 : name = get_identifier (buf2);
668 : : }
669 : 48 : name_independent_cnt[0]++;
670 : : }
671 : : else
672 : : {
673 : 906923 : buf = XALLOCAVEC (char, IDENTIFIER_LENGTH (id) + 3);
674 : 906923 : buf[1] = buf[0] = '_';
675 : 906923 : memcpy (buf + 2, IDENTIFIER_POINTER (id),
676 : 906923 : IDENTIFIER_LENGTH (id) + 1);
677 : 906923 : name = get_identifier (buf);
678 : : }
679 : :
680 : 906971 : if (variadic)
681 : : {
682 : 1674 : type = make_pack_expansion (type);
683 : 1674 : if (explicit_init_p)
684 : : /* With an explicit initializer 'type' is auto, which isn't really a
685 : : parameter pack in this context. We will want as many fields as we
686 : : have elements in the expansion of the initializer, so use its packs
687 : : instead. */
688 : : {
689 : 64 : PACK_EXPANSION_PARAMETER_PACKS (type)
690 : 32 : = uses_parameter_packs (initializer);
691 : 32 : PACK_EXPANSION_AUTO_P (type) = true;
692 : : }
693 : : }
694 : :
695 : : /* Make member variable. */
696 : 906971 : member = build_decl (input_location, FIELD_DECL, name, type);
697 : 906971 : DECL_VLA_CAPTURE_P (member) = vla;
698 : :
699 : 906971 : if (!explicit_init_p)
700 : : /* Normal captures are invisible to name lookup but uses are replaced
701 : : with references to the capture field; we implement this by only
702 : : really making them invisible in unevaluated context; see
703 : : qualify_lookup. For now, let's make explicitly initialized captures
704 : : always visible. */
705 : 899987 : DECL_NORMAL_CAPTURE_P (member) = true;
706 : :
707 : 906971 : if (id == this_identifier)
708 : 205205 : LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
709 : :
710 : : /* Add it to the appropriate closure class if we've started it. */
711 : 906971 : if (current_class_type
712 : 906971 : && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
713 : : {
714 : 520837 : if (COMPLETE_TYPE_P (current_class_type))
715 : 0 : internal_error ("trying to capture %qD in instantiation of "
716 : : "generic lambda", id);
717 : 520837 : finish_member_declaration (member);
718 : : }
719 : :
720 : 906971 : tree listmem = member;
721 : 906971 : if (variadic)
722 : : {
723 : 1674 : listmem = make_pack_expansion (member);
724 : 1674 : initializer = orig_init;
725 : : }
726 : 906971 : LAMBDA_EXPR_CAPTURE_LIST (lambda)
727 : 906971 : = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
728 : :
729 : 906971 : if (LAMBDA_EXPR_CLOSURE (lambda))
730 : 520837 : return build_capture_proxy (member, initializer);
731 : : /* For explicit captures we haven't started the function yet, so we wait
732 : : and build the proxy from cp_parser_lambda_body. */
733 : 386134 : LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda)) = true;
734 : 386134 : return NULL_TREE;
735 : : }
736 : :
737 : : /* Register all the capture members on the list CAPTURES, which is the
738 : : LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
739 : :
740 : : void
741 : 1639934 : register_capture_members (tree captures)
742 : : {
743 : 1639934 : if (captures == NULL_TREE)
744 : : return;
745 : :
746 : 736677 : register_capture_members (TREE_CHAIN (captures));
747 : :
748 : 736677 : tree field = TREE_PURPOSE (captures);
749 : 736677 : if (PACK_EXPANSION_P (field))
750 : 1674 : field = PACK_EXPANSION_PATTERN (field);
751 : :
752 : 736677 : finish_member_declaration (field);
753 : : }
754 : :
755 : : /* Similar to add_capture, except this works on a stack of nested lambdas.
756 : : BY_REFERENCE_P in this case is derived from the default capture mode.
757 : : Returns the capture for the lambda at the bottom of the stack. */
758 : :
759 : : tree
760 : 518134 : add_default_capture (tree lambda_stack, tree id, tree initializer)
761 : : {
762 : 518134 : bool this_capture_p = (id == this_identifier);
763 : 518134 : tree var = NULL_TREE;
764 : 518134 : tree saved_class_type = current_class_type;
765 : :
766 : 518134 : for (tree node = lambda_stack;
767 : 1038995 : node;
768 : 520861 : node = TREE_CHAIN (node))
769 : : {
770 : 520861 : tree lambda = TREE_VALUE (node);
771 : :
772 : 520861 : current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
773 : 520861 : if (DECL_PACK_P (initializer))
774 : 0 : initializer = make_pack_expansion (initializer);
775 : 520861 : var = add_capture (lambda,
776 : : id,
777 : : initializer,
778 : : /*by_reference_p=*/
779 : : (this_capture_p
780 : 520861 : || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
781 : 458738 : == CPLD_REFERENCE)),
782 : : /*explicit_init_p=*/false, NULL);
783 : 520861 : initializer = convert_from_reference (var);
784 : :
785 : : /* Warn about deprecated implicit capture of this via [=]. */
786 : 520861 : if (cxx_dialect >= cxx20
787 : 334911 : && this_capture_p
788 : 560728 : && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_COPY)
789 : : {
790 : 39 : auto_diagnostic_group d;
791 : 39 : if (warning_at (LAMBDA_EXPR_LOCATION (lambda), OPT_Wdeprecated,
792 : : "implicit capture of %qE via %<[=]%> is deprecated "
793 : : "in C++20", this_identifier))
794 : 35 : inform (LAMBDA_EXPR_LOCATION (lambda), "add explicit %<this%> or "
795 : : "%<*this%> capture");
796 : 39 : }
797 : : }
798 : :
799 : 518134 : current_class_type = saved_class_type;
800 : :
801 : 518134 : return var;
802 : : }
803 : :
804 : : /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
805 : : form of an INDIRECT_REF, possibly adding it through default
806 : : capturing, if ADD_CAPTURE_P is nonzero. If ADD_CAPTURE_P is negative,
807 : : try to capture but don't complain if we can't. */
808 : :
809 : : tree
810 : 809849 : lambda_expr_this_capture (tree lambda, int add_capture_p)
811 : : {
812 : 809849 : tree result;
813 : :
814 : 809849 : tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
815 : 809849 : if (this_capture)
816 : 684265 : if (tree spec = retrieve_local_specialization (this_capture))
817 : : {
818 : 959 : gcc_checking_assert (generic_lambda_fn_p (lambda_function (lambda)));
819 : : this_capture = spec;
820 : : }
821 : :
822 : : /* In unevaluated context this isn't an odr-use, so don't capture. */
823 : 809849 : if (cp_unevaluated_operand)
824 : 116 : add_capture_p = false;
825 : :
826 : : /* If we captured 'this' but don't have a capture proxy yet, look up the
827 : : captured 'this' again. */
828 : 809849 : if (this_capture && TREE_CODE (this_capture) == FIELD_DECL)
829 : : {
830 : 0 : gcc_assert (!add_capture_p);
831 : : this_capture = NULL_TREE;
832 : : }
833 : :
834 : : /* Try to default capture 'this' if we can. */
835 : : if (!this_capture)
836 : : {
837 : : tree lambda_stack = NULL_TREE;
838 : 126063 : tree init = NULL_TREE;
839 : : bool saw_complete = false;
840 : :
841 : : /* If we are in a lambda function, we can move out until we hit:
842 : : 1. a non-lambda function or NSDMI,
843 : : 2. a lambda function capturing 'this', or
844 : : 3. a non-default capturing lambda function. */
845 : : for (tree tlambda = lambda; ;)
846 : : {
847 : 126063 : if (add_capture_p
848 : 188205 : && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
849 : : /* tlambda won't let us capture 'this'. */
850 : : break;
851 : :
852 : 126047 : if (add_capture_p)
853 : 62126 : lambda_stack = tree_cons (NULL_TREE,
854 : : tlambda,
855 : : lambda_stack);
856 : :
857 : 126047 : tree closure = LAMBDA_EXPR_CLOSURE (tlambda);
858 : 126047 : if (COMPLETE_TYPE_P (closure))
859 : : /* We're instantiating a generic lambda op(), the containing
860 : : scope may be gone. */
861 : 3966 : saw_complete = true;
862 : :
863 : 126047 : tree containing_function
864 : 126047 : = decl_function_context (TYPE_NAME (closure));
865 : :
866 : 126047 : tree ex = LAMBDA_EXPR_EXTRA_SCOPE (tlambda);
867 : 126047 : if (ex && TREE_CODE (ex) == FIELD_DECL)
868 : : {
869 : : /* Lambda in an NSDMI. We don't have a function to look up
870 : : 'this' in, but we can find (or rebuild) the fake one from
871 : : inject_this_parameter. */
872 : 63 : if (!containing_function && !saw_complete)
873 : : /* If we're parsing a lambda in a non-local class,
874 : : we can find the fake 'this' in scope_chain. */
875 : 45 : init = scope_chain->x_current_class_ptr;
876 : : else
877 : : /* Otherwise it's either gone or buried in
878 : : function_context_stack, so make another. */
879 : 18 : init = build_this_parm (NULL_TREE, DECL_CONTEXT (ex),
880 : : TYPE_UNQUALIFIED);
881 : 63 : gcc_checking_assert
882 : : (init && (TREE_TYPE (TREE_TYPE (init))
883 : : == current_nonlambda_class_type ()));
884 : : break;
885 : : }
886 : :
887 : 125984 : if (containing_function == NULL_TREE)
888 : : /* We ran out of scopes; there's no 'this' to capture. */
889 : : break;
890 : :
891 : 125193 : if (!LAMBDA_FUNCTION_P (containing_function))
892 : : {
893 : : /* We found a non-lambda function.
894 : : There is no this pointer in xobj member functions. */
895 : 123909 : if (DECL_IOBJ_MEMBER_FUNCTION_P (containing_function))
896 : : /* First parameter is 'this'. */
897 : 118527 : init = DECL_ARGUMENTS (containing_function);
898 : : break;
899 : : }
900 : :
901 : 501 : tlambda
902 : 501 : = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
903 : :
904 : 501 : if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
905 : : {
906 : : /* An outer lambda has already captured 'this'. */
907 : : init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
908 : : break;
909 : : }
910 : : }
911 : :
912 : 120202 : if (init)
913 : : {
914 : 118612 : if (add_capture_p)
915 : 62095 : this_capture = add_default_capture (lambda_stack,
916 : : /*id=*/this_identifier,
917 : : init);
918 : : else
919 : : this_capture = init;
920 : : }
921 : : }
922 : :
923 : 809849 : if (cp_unevaluated_operand)
924 : : result = this_capture;
925 : 809733 : else if (!this_capture)
926 : : {
927 : 6960 : if (add_capture_p == 1)
928 : : {
929 : 16 : error ("%<this%> was not captured for this lambda function");
930 : 16 : result = error_mark_node;
931 : : }
932 : : else
933 : : result = NULL_TREE;
934 : : }
935 : : else
936 : : {
937 : : /* To make sure that current_class_ref is for the lambda. */
938 : 802773 : gcc_assert (!current_class_ref
939 : : || (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
940 : : == LAMBDA_EXPR_CLOSURE (lambda)));
941 : :
942 : 802773 : result = this_capture;
943 : :
944 : : /* If 'this' is captured, each use of 'this' is transformed into an
945 : : access to the corresponding unnamed data member of the closure
946 : : type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
947 : : ensures that the transformed expression is an rvalue. ] */
948 : 802773 : result = rvalue (result);
949 : : }
950 : :
951 : 802905 : gcc_checking_assert (!result || result == error_mark_node
952 : : || TYPE_PTR_P (TREE_TYPE (result)));
953 : :
954 : 809849 : return result;
955 : : }
956 : :
957 : : /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
958 : :
959 : : tree
960 : 37917303 : current_lambda_expr (void)
961 : : {
962 : 37917303 : tree type = current_class_type;
963 : 50604259 : while (type && !LAMBDA_TYPE_P (type))
964 : 5103705 : type = decl_type_context (TYPE_NAME (type));
965 : 37917303 : if (type)
966 : 2627241 : return CLASSTYPE_LAMBDA_EXPR (type);
967 : : else
968 : : return NULL_TREE;
969 : : }
970 : :
971 : : /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
972 : : object. NULL otherwise.. */
973 : :
974 : : static tree
975 : 213386792 : resolvable_dummy_lambda (tree object)
976 : : {
977 : 213386792 : if (!is_dummy_object (object))
978 : : return NULL_TREE;
979 : :
980 : 14107801 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
981 : 14107801 : gcc_assert (!TYPE_PTR_P (type));
982 : :
983 : 14107801 : if (type != current_class_type
984 : 11701362 : && current_class_type
985 : 16432161 : && LAMBDA_TYPE_P (current_class_type)
986 : 340559 : && lambda_function (current_class_type)
987 : 14448348 : && DERIVED_FROM_P (type, nonlambda_method_basetype()))
988 : 327507 : return CLASSTYPE_LAMBDA_EXPR (current_class_type);
989 : :
990 : : return NULL_TREE;
991 : : }
992 : :
993 : : /* We don't want to capture 'this' until we know we need it, i.e. after
994 : : overload resolution has chosen a non-static member function. At that
995 : : point we call this function to turn a dummy object into a use of the
996 : : 'this' capture. */
997 : :
998 : : tree
999 : 126114099 : maybe_resolve_dummy (tree object, bool add_capture_p)
1000 : : {
1001 : 126114099 : if (tree lam = resolvable_dummy_lambda (object))
1002 : 301107 : if (tree cap = lambda_expr_this_capture (lam, add_capture_p))
1003 : 301107 : if (cap != error_mark_node)
1004 : 301101 : object = build_fold_indirect_ref (cap);
1005 : :
1006 : 126114099 : return object;
1007 : : }
1008 : :
1009 : : /* When parsing a generic lambda containing an argument-dependent
1010 : : member function call we defer overload resolution to instantiation
1011 : : time. But we have to know now whether to capture this or not.
1012 : : Do that if FNS contains any non-static fns as per
1013 : : [expr.prim.lambda.capture]/7.1. */
1014 : :
1015 : : void
1016 : 87272693 : maybe_generic_this_capture (tree object, tree fns)
1017 : : {
1018 : 87272693 : if (tree lam = resolvable_dummy_lambda (object))
1019 : 26400 : if (!LAMBDA_EXPR_THIS_CAPTURE (lam))
1020 : : {
1021 : : /* We've not yet captured, so look at the function set of
1022 : : interest. */
1023 : 1490 : if (BASELINK_P (fns))
1024 : 51 : fns = BASELINK_FUNCTIONS (fns);
1025 : 1490 : bool id_expr = TREE_CODE (fns) == TEMPLATE_ID_EXPR;
1026 : 1490 : if (id_expr)
1027 : 1445 : fns = TREE_OPERAND (fns, 0);
1028 : :
1029 : 1499 : for (lkp_iterator iter (fns); iter; ++iter)
1030 : 54 : if (((!id_expr && TREE_CODE (*iter) != USING_DECL)
1031 : 1448 : || TREE_CODE (*iter) == TEMPLATE_DECL)
1032 : 2995 : && DECL_OBJECT_MEMBER_FUNCTION_P (*iter))
1033 : : {
1034 : : /* Found a non-static member. Capture this. */
1035 : 1490 : lambda_expr_this_capture (lam, /*maybe*/-1);
1036 : 1490 : break;
1037 : : }
1038 : : }
1039 : 87272693 : }
1040 : :
1041 : : /* Returns the innermost non-lambda function. */
1042 : :
1043 : : tree
1044 : 77 : current_nonlambda_function (void)
1045 : : {
1046 : 77 : tree fn = current_function_decl;
1047 : 83 : while (fn && LAMBDA_FUNCTION_P (fn))
1048 : 3 : fn = decl_function_context (fn);
1049 : 77 : return fn;
1050 : : }
1051 : :
1052 : : /* Returns the method basetype of the innermost non-lambda function, including
1053 : : a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
1054 : :
1055 : : tree
1056 : 340930 : nonlambda_method_basetype (void)
1057 : : {
1058 : 340930 : tree type = current_class_type;
1059 : 681582 : if (!type || !LAMBDA_TYPE_P (type))
1060 : 652 : return current_class_ref ? type : NULL_TREE;
1061 : :
1062 : 341619 : while (true)
1063 : : {
1064 : 341089 : tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1065 : 341089 : tree ex = LAMBDA_EXPR_EXTRA_SCOPE (lam);
1066 : 341089 : if (ex && TREE_CODE (ex) == FIELD_DECL)
1067 : : /* Lambda in an NSDMI. */
1068 : 30 : return DECL_CONTEXT (ex);
1069 : :
1070 : 341059 : tree fn = TYPE_CONTEXT (type);
1071 : 341059 : if (!fn || TREE_CODE (fn) != FUNCTION_DECL
1072 : 680536 : || !DECL_OBJECT_MEMBER_FUNCTION_P (fn))
1073 : : /* No enclosing non-lambda method. */
1074 : : return NULL_TREE;
1075 : 334431 : if (!LAMBDA_FUNCTION_P (fn))
1076 : : /* Found an enclosing non-lambda method. */
1077 : 333224 : return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1078 : 530 : type = DECL_CONTEXT (fn);
1079 : 530 : }
1080 : : }
1081 : :
1082 : : /* Like current_scope, but looking through lambdas. If ONLY_SKIP_CLOSURES_P,
1083 : : only look through closure types. */
1084 : :
1085 : : tree
1086 : 42997644 : current_nonlambda_scope (bool only_skip_closures_p/*=false*/)
1087 : : {
1088 : 42997644 : tree scope = current_scope ();
1089 : 43222331 : for (;;)
1090 : : {
1091 : 43446913 : if (!only_skip_closures_p
1092 : 42982507 : && TREE_CODE (scope) == FUNCTION_DECL
1093 : 81348866 : && LAMBDA_FUNCTION_P (scope))
1094 : : {
1095 : 224582 : scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope));
1096 : 224582 : continue;
1097 : : }
1098 : 48513144 : else if (LAMBDA_TYPE_P (scope))
1099 : : {
1100 : 105 : scope = CP_TYPE_CONTEXT (scope);
1101 : 105 : continue;
1102 : : }
1103 : 42997644 : break;
1104 : : }
1105 : 42997644 : return scope;
1106 : : }
1107 : :
1108 : : /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
1109 : : indicated FN and NARGS, but do not initialize the return type or any of the
1110 : : argument slots. */
1111 : :
1112 : : static tree
1113 : 21588 : prepare_op_call (tree fn, int nargs)
1114 : : {
1115 : 21588 : tree t;
1116 : :
1117 : 21588 : t = build_vl_exp (CALL_EXPR, nargs + 3);
1118 : 21588 : CALL_EXPR_FN (t) = fn;
1119 : 21588 : CALL_EXPR_STATIC_CHAIN (t) = NULL;
1120 : :
1121 : 21588 : return t;
1122 : : }
1123 : :
1124 : : /* Return true iff CALLOP is the op() for a generic lambda. */
1125 : :
1126 : : bool
1127 : 423045 : generic_lambda_fn_p (tree callop)
1128 : : {
1129 : 846090 : return (LAMBDA_FUNCTION_P (callop)
1130 : 423045 : && DECL_TEMPLATE_INFO (callop)
1131 : 802635 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
1132 : : }
1133 : :
1134 : : /* If the closure TYPE has a static op(), also add a conversion to function
1135 : : pointer. */
1136 : :
1137 : : void
1138 : 903214 : maybe_add_lambda_conv_op (tree type)
1139 : : {
1140 : 903214 : bool nested = (cfun != NULL);
1141 : 903214 : bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
1142 : 903214 : tree callop = lambda_function (type);
1143 : 903214 : tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1144 : :
1145 : 903214 : if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE
1146 : 903214 : || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE)
1147 : 849192 : return;
1148 : :
1149 : 260981 : if (processing_template_decl)
1150 : : return;
1151 : :
1152 : 54402 : bool const generic_lambda_p = generic_lambda_fn_p (callop);
1153 : :
1154 : 54402 : if (!generic_lambda_p && undeduced_auto_decl (callop))
1155 : : {
1156 : : /* If the op() wasn't deduced due to errors, give up. */
1157 : 28 : gcc_assert (errorcount || sorrycount);
1158 : : return;
1159 : : }
1160 : :
1161 : : /* Non-generic non-capturing lambdas only have a conversion function to
1162 : : pointer to function when the trailing requires-clause's constraints are
1163 : : satisfied. */
1164 : 54374 : if (!generic_lambda_p && !constraints_satisfied_p (callop))
1165 : : return;
1166 : :
1167 : : /* Non-template conversion operators are defined directly with build_call_a
1168 : : and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1169 : : deferred and the CALL is built in-place. In the case of a deduced return
1170 : : call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1171 : : the return type is also built in-place. The arguments of DECLTYPE_CALL in
1172 : : the return expression may differ in flags from those in the body CALL. In
1173 : : particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1174 : : the body CALL, but not in DECLTYPE_CALL. */
1175 : :
1176 : 54356 : vec<tree, va_gc> *direct_argvec = 0;
1177 : 54356 : tree decltype_call = 0, call = 0;
1178 : 54356 : tree optype = TREE_TYPE (callop);
1179 : 54356 : tree fn_result = TREE_TYPE (optype);
1180 : :
1181 : 54356 : tree thisarg = NULL_TREE;
1182 : 54356 : if (TREE_CODE (optype) == METHOD_TYPE)
1183 : 54223 : thisarg = build_int_cst (TREE_TYPE (DECL_ARGUMENTS (callop)), 0);
1184 : 54356 : if (generic_lambda_p)
1185 : : {
1186 : 10947 : ++processing_template_decl;
1187 : :
1188 : : /* Prepare the dependent member call for the static member function
1189 : : '_FUN' and, potentially, prepare another call to be used in a decltype
1190 : : return expression for a deduced return call op to allow for simple
1191 : : implementation of the conversion operator. */
1192 : :
1193 : 10947 : tree objfn;
1194 : 10947 : int nargs = list_length (DECL_ARGUMENTS (callop));
1195 : 10947 : if (thisarg)
1196 : : {
1197 : 10847 : tree instance = cp_build_fold_indirect_ref (thisarg);
1198 : 10847 : objfn = lookup_template_function (DECL_NAME (callop),
1199 : 10847 : DECL_TI_ARGS (callop));
1200 : 10847 : objfn = build_min (COMPONENT_REF, NULL_TREE,
1201 : : instance, objfn, NULL_TREE);
1202 : 10847 : --nargs;
1203 : 10847 : call = prepare_op_call (objfn, nargs);
1204 : : }
1205 : : else
1206 : : objfn = callop;
1207 : :
1208 : 10947 : if (type_uses_auto (fn_result))
1209 : 10741 : decltype_call = prepare_op_call (objfn, nargs);
1210 : : }
1211 : 43409 : else if (thisarg)
1212 : : {
1213 : 43376 : direct_argvec = make_tree_vector ();
1214 : 43376 : direct_argvec->quick_push (thisarg);
1215 : : }
1216 : :
1217 : : /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1218 : : declare the static member function "_FUN" below. For each arg append to
1219 : : DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1220 : : call args (for the template case). If a parameter pack is found, expand
1221 : : it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1222 : :
1223 : 54356 : tree fn_args = NULL_TREE;
1224 : 54356 : {
1225 : 54356 : int ix = 0;
1226 : 54356 : tree src = FUNCTION_FIRST_USER_PARM (callop);
1227 : 54356 : tree tgt = NULL;
1228 : :
1229 : 54356 : if (!thisarg && !decltype_call)
1230 : 54356 : src = NULL_TREE;
1231 : 93499 : while (src)
1232 : : {
1233 : 39143 : tree new_node = copy_node (src);
1234 : : /* We set DECL_CONTEXT of NEW_NODE to the statfn below.
1235 : : Notice this is creating a recursive type! */
1236 : :
1237 : : /* Clear TREE_ADDRESSABLE on thunk arguments. */
1238 : 39143 : TREE_ADDRESSABLE (new_node) = 0;
1239 : :
1240 : 39143 : if (!fn_args)
1241 : 26229 : fn_args = tgt = new_node;
1242 : : else
1243 : : {
1244 : 12914 : TREE_CHAIN (tgt) = new_node;
1245 : 12914 : tgt = new_node;
1246 : : }
1247 : :
1248 : 39143 : mark_exp_read (tgt);
1249 : :
1250 : 39143 : if (generic_lambda_p)
1251 : : {
1252 : 17600 : tree a = tgt;
1253 : 17600 : if (thisarg)
1254 : : {
1255 : 17499 : if (DECL_PACK_P (tgt))
1256 : : {
1257 : 569 : a = make_pack_expansion (a);
1258 : 569 : PACK_EXPANSION_LOCAL_P (a) = true;
1259 : : }
1260 : 17499 : CALL_EXPR_ARG (call, ix) = a;
1261 : : }
1262 : :
1263 : 17600 : if (decltype_call)
1264 : : {
1265 : : /* Avoid capturing variables in this context. */
1266 : 17314 : ++cp_unevaluated_operand;
1267 : 17314 : CALL_EXPR_ARG (decltype_call, ix) = forward_parm (tgt);
1268 : 17314 : --cp_unevaluated_operand;
1269 : : }
1270 : :
1271 : 17600 : ++ix;
1272 : : }
1273 : : else
1274 : 21543 : vec_safe_push (direct_argvec, tgt);
1275 : :
1276 : 39143 : src = TREE_CHAIN (src);
1277 : : }
1278 : : }
1279 : :
1280 : 54356 : if (generic_lambda_p)
1281 : : {
1282 : 10947 : if (decltype_call)
1283 : : {
1284 : 10741 : fn_result = finish_decltype_type
1285 : 10741 : (decltype_call, /*id_expression_or_member_access_p=*/false,
1286 : : tf_warning_or_error);
1287 : : }
1288 : : }
1289 : 43409 : else if (thisarg)
1290 : : {
1291 : : /* Don't warn on deprecated or unavailable lambda declarations, unless
1292 : : the lambda is actually called. */
1293 : 43376 : auto du = make_temp_override (deprecated_state,
1294 : 43376 : UNAVAILABLE_DEPRECATED_SUPPRESS);
1295 : 43376 : call = build_call_a (callop, direct_argvec->length (),
1296 : : direct_argvec->address ());
1297 : 43376 : }
1298 : :
1299 : 54356 : if (thisarg)
1300 : : {
1301 : 54223 : CALL_FROM_THUNK_P (call) = 1;
1302 : 54223 : SET_EXPR_LOCATION (call, UNKNOWN_LOCATION);
1303 : : }
1304 : :
1305 : 54356 : tree stattype
1306 : 54356 : = build_function_type (fn_result, FUNCTION_FIRST_USER_PARMTYPE (callop));
1307 : 54356 : stattype = (cp_build_type_attribute_variant
1308 : 54356 : (stattype, TYPE_ATTRIBUTES (optype)));
1309 : 54356 : if (flag_noexcept_type
1310 : 54356 : && TYPE_NOTHROW_P (TREE_TYPE (callop)))
1311 : 77 : stattype = build_exception_variant (stattype, noexcept_true_spec);
1312 : :
1313 : 54356 : if (generic_lambda_p)
1314 : 10947 : --processing_template_decl;
1315 : :
1316 : : /* First build up the conversion op. */
1317 : :
1318 : 54356 : tree rettype = build_pointer_type (stattype);
1319 : 54356 : tree name = make_conv_op_name (rettype);
1320 : 54356 : tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1321 : 54356 : tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
1322 : : /* DR 1722: The conversion function should be noexcept. */
1323 : 54356 : fntype = build_exception_variant (fntype, noexcept_true_spec);
1324 : 54356 : tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
1325 : 54356 : SET_DECL_LANGUAGE (convfn, lang_cplusplus);
1326 : 54356 : tree fn = convfn;
1327 : 54356 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1328 : 54356 : SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
1329 : 54356 : grokclassfn (type, fn, NO_SPECIAL);
1330 : 54356 : set_linkage_according_to_type (type, fn);
1331 : 54356 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1332 : 54356 : DECL_IN_AGGR_P (fn) = 1;
1333 : 54356 : DECL_ARTIFICIAL (fn) = 1;
1334 : 54356 : DECL_NOT_REALLY_EXTERN (fn) = 1;
1335 : 54356 : DECL_DECLARED_INLINE_P (fn) = 1;
1336 : 54356 : DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop);
1337 : 108712 : if (DECL_IMMEDIATE_FUNCTION_P (callop))
1338 : 1245 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
1339 : 54356 : DECL_ARGUMENTS (fn) = build_this_parm (fn, fntype, TYPE_QUAL_CONST);
1340 : :
1341 : 54356 : if (nested_def)
1342 : 51128 : DECL_INTERFACE_KNOWN (fn) = 1;
1343 : :
1344 : 54356 : if (generic_lambda_p)
1345 : 10947 : fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1346 : :
1347 : 54356 : add_method (type, fn, false);
1348 : :
1349 : 54356 : if (thisarg == NULL_TREE)
1350 : : {
1351 : : /* For static lambda, just return operator(). */
1352 : 133 : if (nested)
1353 : 128 : push_function_context ();
1354 : : else
1355 : : /* Still increment function_depth so that we don't GC in the
1356 : : middle of an expression. */
1357 : 5 : ++function_depth;
1358 : :
1359 : : /* Generate the body of the conversion op. */
1360 : :
1361 : 133 : start_preparsed_function (convfn, NULL_TREE,
1362 : : SF_PRE_PARSED | SF_INCLASS_INLINE);
1363 : 133 : tree body = begin_function_body ();
1364 : 133 : tree compound_stmt = begin_compound_stmt (0);
1365 : :
1366 : : /* decl_needed_p needs to see that it's used. */
1367 : 133 : TREE_USED (callop) = 1;
1368 : 133 : finish_return_stmt (decay_conversion (callop, tf_warning_or_error));
1369 : :
1370 : 133 : finish_compound_stmt (compound_stmt);
1371 : 133 : finish_function_body (body);
1372 : :
1373 : 133 : fn = finish_function (/*inline_p=*/true);
1374 : 133 : if (!generic_lambda_p)
1375 : 33 : expand_or_defer_fn (fn);
1376 : :
1377 : 133 : if (nested)
1378 : 128 : pop_function_context ();
1379 : : else
1380 : 5 : --function_depth;
1381 : 133 : return;
1382 : : }
1383 : :
1384 : : /* Generic thunk code fails for varargs; we'll complain in mark_used if
1385 : : the conversion op is used. */
1386 : 54223 : if (varargs_function_p (callop))
1387 : : {
1388 : 201 : DECL_DELETED_FN (fn) = 1;
1389 : 201 : return;
1390 : : }
1391 : :
1392 : : /* Now build up the thunk to be returned. */
1393 : :
1394 : 54022 : tree statfn = build_lang_decl (FUNCTION_DECL, fun_identifier, stattype);
1395 : 54022 : SET_DECL_LANGUAGE (statfn, lang_cplusplus);
1396 : 54022 : fn = statfn;
1397 : 54022 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1398 : 54022 : grokclassfn (type, fn, NO_SPECIAL);
1399 : 54022 : set_linkage_according_to_type (type, fn);
1400 : 54022 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1401 : 54022 : DECL_IN_AGGR_P (fn) = 1;
1402 : 54022 : DECL_ARTIFICIAL (fn) = 1;
1403 : 54022 : DECL_NOT_REALLY_EXTERN (fn) = 1;
1404 : 54022 : DECL_DECLARED_INLINE_P (fn) = 1;
1405 : 54022 : DECL_STATIC_FUNCTION_P (fn) = 1;
1406 : 54022 : DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop);
1407 : 108044 : if (DECL_IMMEDIATE_FUNCTION_P (callop))
1408 : 1245 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
1409 : 54022 : DECL_ARGUMENTS (fn) = fn_args;
1410 : 92876 : for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
1411 : : {
1412 : : /* Avoid duplicate -Wshadow warnings. */
1413 : 38854 : DECL_NAME (arg) = NULL_TREE;
1414 : 38854 : DECL_CONTEXT (arg) = fn;
1415 : : }
1416 : 54022 : if (nested_def)
1417 : 50811 : DECL_INTERFACE_KNOWN (fn) = 1;
1418 : :
1419 : 54022 : if (generic_lambda_p)
1420 : 10835 : fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1421 : :
1422 : 54022 : if (flag_sanitize & SANITIZE_NULL)
1423 : : /* Don't UBsan this function; we're deliberately calling op() with a null
1424 : : object argument. */
1425 : 37 : add_no_sanitize_value (fn, SANITIZE_UNDEFINED);
1426 : :
1427 : 54022 : add_method (type, fn, false);
1428 : :
1429 : 54022 : if (nested)
1430 : 50866 : push_function_context ();
1431 : : else
1432 : : /* Still increment function_depth so that we don't GC in the
1433 : : middle of an expression. */
1434 : 3156 : ++function_depth;
1435 : :
1436 : : /* Generate the body of the thunk. */
1437 : :
1438 : 54022 : start_preparsed_function (statfn, NULL_TREE,
1439 : : SF_PRE_PARSED | SF_INCLASS_INLINE);
1440 : 54022 : tree body = begin_function_body ();
1441 : 54022 : tree compound_stmt = begin_compound_stmt (0);
1442 : 54022 : if (!generic_lambda_p)
1443 : : {
1444 : 43187 : set_flags_from_callee (call);
1445 : 43187 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1446 : 2213 : call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1447 : : }
1448 : 54022 : call = convert_from_reference (call);
1449 : 54022 : finish_return_stmt (call);
1450 : :
1451 : 54022 : finish_compound_stmt (compound_stmt);
1452 : 54022 : finish_function_body (body);
1453 : :
1454 : 54022 : fn = finish_function (/*inline_p=*/true);
1455 : 54022 : if (!generic_lambda_p)
1456 : 43187 : expand_or_defer_fn (fn);
1457 : :
1458 : : /* Generate the body of the conversion op. */
1459 : :
1460 : 54022 : start_preparsed_function (convfn, NULL_TREE,
1461 : : SF_PRE_PARSED | SF_INCLASS_INLINE);
1462 : 54022 : body = begin_function_body ();
1463 : 54022 : compound_stmt = begin_compound_stmt (0);
1464 : :
1465 : : /* decl_needed_p needs to see that it's used. */
1466 : 54022 : TREE_USED (statfn) = 1;
1467 : 54022 : finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1468 : :
1469 : 54022 : finish_compound_stmt (compound_stmt);
1470 : 54022 : finish_function_body (body);
1471 : :
1472 : 54022 : fn = finish_function (/*inline_p=*/true);
1473 : 54022 : if (!generic_lambda_p)
1474 : 43187 : expand_or_defer_fn (fn);
1475 : :
1476 : 54022 : if (nested)
1477 : 50866 : pop_function_context ();
1478 : : else
1479 : 3156 : --function_depth;
1480 : : }
1481 : :
1482 : : /* True if FN is the static function "_FUN" that gets returned from the lambda
1483 : : conversion operator. */
1484 : :
1485 : : bool
1486 : 1121178 : lambda_static_thunk_p (tree fn)
1487 : : {
1488 : 1121178 : return (fn && TREE_CODE (fn) == FUNCTION_DECL
1489 : 1121178 : && DECL_ARTIFICIAL (fn)
1490 : 58192 : && DECL_STATIC_FUNCTION_P (fn)
1491 : 1232382 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn)));
1492 : : }
1493 : :
1494 : : bool
1495 : 165245296 : call_from_lambda_thunk_p (tree call)
1496 : : {
1497 : 165245296 : return (CALL_FROM_THUNK_P (call)
1498 : 165245296 : && lambda_static_thunk_p (current_function_decl));
1499 : : }
1500 : :
1501 : : /* Returns true iff VAL is a lambda-related declaration which should
1502 : : be ignored by unqualified lookup. */
1503 : :
1504 : : bool
1505 : 3073510652 : is_lambda_ignored_entity (tree val)
1506 : : {
1507 : : /* Look past normal, non-VLA capture proxies. */
1508 : 3073510652 : if (is_normal_capture_proxy (val)
1509 : 3073510652 : && !variably_modified_type_p (TREE_TYPE (val), NULL_TREE))
1510 : : return true;
1511 : :
1512 : : /* Always ignore lambda fields, their names are only for debugging. */
1513 : 3070979588 : if (TREE_CODE (val) == FIELD_DECL
1514 : 3070979588 : && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1515 : : return true;
1516 : :
1517 : : /* None of the lookups that use qualify_lookup want the op() from the
1518 : : lambda; they want the one from the enclosing class. */
1519 : 3070979561 : if (tree fns = maybe_get_fns (val))
1520 : 1026874867 : if (LAMBDA_FUNCTION_P (OVL_FIRST (fns)))
1521 : : return true;
1522 : :
1523 : : return false;
1524 : : }
1525 : :
1526 : : /* Lambdas that appear in variable initializer or default argument
1527 : : scope get that in their mangling, so we need to record it. Also,
1528 : : multiple lambdas in the same scope may need a mangling
1529 : : discriminator. In ABI <= 17, there is a single per-scope sequence
1530 : : number. In ABI >= 18, there are per-scope per-signature sequence
1531 : : numbers. */
1532 : : struct GTY(()) lambda_sig_count
1533 : : {
1534 : : tree fn; // The lambda fn whose sig this is.
1535 : : unsigned count;
1536 : : };
1537 : : struct GTY(()) lambda_discriminator
1538 : : {
1539 : : tree scope;
1540 : : unsigned nesting; // Inside a function, VAR_DECLs get the function
1541 : : // as scope. This counts that nesting.
1542 : : unsigned count; // The per-scope counter.
1543 : : vec<lambda_sig_count, va_gc> *discriminators; // Per-signature counters
1544 : : };
1545 : : // The current scope.
1546 : : static GTY(()) lambda_discriminator lambda_scope;
1547 : : // Stack of previous scopes.
1548 : : static GTY(()) vec<lambda_discriminator, va_gc> *lambda_scope_stack;
1549 : :
1550 : : // Push DECL as lambda extra scope, also new discriminator counters.
1551 : :
1552 : : void
1553 : 265378414 : start_lambda_scope (tree decl)
1554 : : {
1555 : 265378414 : gcc_checking_assert (decl);
1556 : 265378414 : if (current_function_decl && VAR_P (decl))
1557 : : // If we're inside a function, we ignore variable scope. Don't push.
1558 : 42136630 : lambda_scope.nesting++;
1559 : : else
1560 : : {
1561 : 223241784 : vec_safe_push (lambda_scope_stack, lambda_scope);
1562 : 223241784 : lambda_scope.scope = decl;
1563 : 223241784 : lambda_scope.nesting = 0;
1564 : 223241784 : lambda_scope.count = 0;
1565 : 223241784 : lambda_scope.discriminators = nullptr;
1566 : : }
1567 : 265378414 : }
1568 : :
1569 : : // Pop from the current lambda extra scope.
1570 : :
1571 : : void
1572 : 265375594 : finish_lambda_scope (void)
1573 : : {
1574 : 265375594 : if (!lambda_scope.nesting--)
1575 : : {
1576 : 223238964 : lambda_scope = lambda_scope_stack->last ();
1577 : 223238964 : lambda_scope_stack->pop ();
1578 : : }
1579 : 265375594 : }
1580 : :
1581 : : // Record the current lambda scope into LAMBDA
1582 : :
1583 : : void
1584 : 902908 : record_lambda_scope (tree lambda)
1585 : : {
1586 : 902908 : tree closure = LAMBDA_EXPR_CLOSURE (lambda);
1587 : 902908 : gcc_checking_assert (closure);
1588 : :
1589 : : /* Before ABI v20, lambdas in static data member initializers did not
1590 : : get a dedicated lambda scope. */
1591 : 902908 : tree scope = lambda_scope.scope;
1592 : 902908 : if (is_static_data_member_initialized_in_class (scope))
1593 : : {
1594 : 46009 : if (!abi_version_at_least (20))
1595 : 46009 : scope = NULL_TREE;
1596 : 137098 : if (warn_abi && abi_version_crosses (20) && !processing_template_decl)
1597 : : {
1598 : 18 : if (abi_version_at_least (20))
1599 : 18 : warning_at (location_of (closure), OPT_Wabi,
1600 : : "the mangled name of %qT changed in "
1601 : : "%<-fabi-version=20%> (GCC 15.1)", closure);
1602 : : else
1603 : 0 : warning_at (location_of (closure), OPT_Wabi,
1604 : : "the mangled name of %qT changes in "
1605 : : "%<-fabi-version=20%> (GCC 15.1)", closure);
1606 : : }
1607 : : }
1608 : :
1609 : : /* An otherwise unattached class-scope lambda in a member template
1610 : : should not have a mangling scope, as the mangling scope will not
1611 : : correctly inherit on instantiation. */
1612 : 902908 : tree ctx = TYPE_CONTEXT (closure);
1613 : 902908 : if (scope
1614 : 902908 : && ctx
1615 : 902427 : && CLASS_TYPE_P (ctx)
1616 : 46524 : && ctx == TREE_TYPE (scope)
1617 : 903052 : && current_template_depth > template_class_depth (ctx))
1618 : : scope = NULL_TREE;
1619 : :
1620 : 902908 : LAMBDA_EXPR_EXTRA_SCOPE (lambda) = scope;
1621 : 902908 : if (scope)
1622 : 902390 : maybe_key_decl (scope, TYPE_NAME (closure));
1623 : 902908 : }
1624 : :
1625 : : // Compare lambda template heads TMPL_A and TMPL_B, used for both
1626 : : // templated lambdas, and template template parameters of said lambda.
1627 : :
1628 : : static bool
1629 : 9276 : compare_lambda_template_head (tree tmpl_a, tree tmpl_b)
1630 : : {
1631 : : // We only need one level of template parms
1632 : 9276 : tree inner_a = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_a));
1633 : 9276 : tree inner_b = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_b));
1634 : :
1635 : : // We only compare explicit template parms, ignoring trailing
1636 : : // synthetic ones.
1637 : 9276 : int len_a = TREE_VEC_LENGTH (inner_a);
1638 : 9276 : int len_b = TREE_VEC_LENGTH (inner_b);
1639 : :
1640 : 9867 : for (int ix = 0, len = MAX (len_a, len_b); ix != len; ix++)
1641 : : {
1642 : 9320 : tree parm_a = NULL_TREE;
1643 : 9320 : if (ix < len_a)
1644 : : {
1645 : 9308 : parm_a = TREE_VEC_ELT (inner_a, ix);
1646 : 9308 : if (parm_a == error_mark_node)
1647 : : return false;
1648 : 9308 : parm_a = TREE_VALUE (parm_a);
1649 : 9308 : if (parm_a == error_mark_node)
1650 : : return false;
1651 : 9308 : if (DECL_VIRTUAL_P (parm_a))
1652 : 5703 : parm_a = NULL_TREE;
1653 : : }
1654 : :
1655 : 9320 : tree parm_b = NULL_TREE;
1656 : 9320 : if (ix < len_b)
1657 : : {
1658 : 9300 : parm_b = TREE_VEC_ELT (inner_b, ix);
1659 : 9300 : if (parm_b == error_mark_node)
1660 : : return false;
1661 : 9300 : parm_b = TREE_VALUE (parm_b);
1662 : 9300 : if (parm_b == error_mark_node)
1663 : : return false;
1664 : 9294 : if (DECL_VIRTUAL_P (parm_b))
1665 : 5653 : parm_b = NULL_TREE;
1666 : : }
1667 : :
1668 : 9314 : if (!parm_a && !parm_b)
1669 : : // we're done
1670 : : break;
1671 : :
1672 : 3684 : if (!(parm_a && parm_b))
1673 : : return false;
1674 : :
1675 : 3593 : if (TREE_CODE (parm_a) != TREE_CODE (parm_b))
1676 : : return false;
1677 : :
1678 : 3593 : if (TREE_CODE (parm_a) == PARM_DECL)
1679 : : {
1680 : 3014 : if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_a))
1681 : 3014 : != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_b)))
1682 : : return false;
1683 : :
1684 : 21 : if (!same_type_p (TREE_TYPE (parm_a), TREE_TYPE (parm_b)))
1685 : : return false;
1686 : : }
1687 : : else
1688 : : {
1689 : 579 : if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_a))
1690 : 579 : != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_b)))
1691 : : return false;
1692 : :
1693 : 579 : if (TREE_CODE (parm_a) != TEMPLATE_DECL)
1694 : 579 : gcc_checking_assert (TREE_CODE (parm_a) == TYPE_DECL);
1695 : 0 : else if (!compare_lambda_template_head (parm_a, parm_b))
1696 : : return false;
1697 : : }
1698 : : }
1699 : :
1700 : : return true;
1701 : : }
1702 : :
1703 : : // Compare lambda signatures FN_A and FN_B, they may be TEMPLATE_DECLs too.
1704 : :
1705 : : static bool
1706 : 122383 : compare_lambda_sig (tree fn_a, tree fn_b)
1707 : : {
1708 : 122383 : if (TREE_CODE (fn_a) == TEMPLATE_DECL
1709 : 17183 : && TREE_CODE (fn_b) == TEMPLATE_DECL)
1710 : : {
1711 : 9276 : if (!compare_lambda_template_head (fn_a, fn_b))
1712 : : return false;
1713 : 6177 : fn_a = DECL_TEMPLATE_RESULT (fn_a);
1714 : 6177 : fn_b = DECL_TEMPLATE_RESULT (fn_b);
1715 : : }
1716 : 113107 : else if (TREE_CODE (fn_a) == TEMPLATE_DECL
1717 : 105200 : || TREE_CODE (fn_b) == TEMPLATE_DECL)
1718 : : return false;
1719 : :
1720 : 111096 : if (fn_a == error_mark_node
1721 : 111095 : || fn_b == error_mark_node)
1722 : : return false;
1723 : :
1724 : 111095 : for (tree args_a = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn_a))),
1725 : 111095 : args_b = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn_b)));
1726 : 204009 : args_a || args_b;
1727 : 92914 : args_a = TREE_CHAIN (args_a), args_b = TREE_CHAIN (args_b))
1728 : : {
1729 : 151933 : if (!args_a || !args_b)
1730 : : return false;
1731 : : // This check also deals with differing variadicness
1732 : 151928 : if (!same_type_p (TREE_VALUE (args_a), TREE_VALUE (args_b)))
1733 : : return false;
1734 : : }
1735 : :
1736 : : return true;
1737 : : }
1738 : :
1739 : : // Record the per-scope discriminator of LAMBDA. If the extra scope
1740 : : // is empty, we must use the empty scope counter, which might not be
1741 : : // the live one.
1742 : :
1743 : : void
1744 : 903257 : record_lambda_scope_discriminator (tree lambda)
1745 : : {
1746 : 1805982 : auto *slot = (vec_safe_is_empty (lambda_scope_stack)
1747 : 902725 : || LAMBDA_EXPR_EXTRA_SCOPE (lambda)
1748 : 335 : ? &lambda_scope : lambda_scope_stack->begin ());
1749 : 903257 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda) = slot->count++;
1750 : 903257 : }
1751 : :
1752 : : // Record the per-scope per-signature discriminator of LAMBDA. If the
1753 : : // extra scope is empty, we must use the empty scope counter, which
1754 : : // might not be the live one.
1755 : :
1756 : : void
1757 : 903245 : record_lambda_scope_sig_discriminator (tree lambda, tree fn)
1758 : : {
1759 : 1805958 : auto *slot = (vec_safe_is_empty (lambda_scope_stack)
1760 : 902713 : || LAMBDA_EXPR_EXTRA_SCOPE (lambda)
1761 : 332 : ? &lambda_scope : lambda_scope_stack->begin ());
1762 : 903245 : gcc_checking_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda) == slot->scope);
1763 : :
1764 : : // A linear search, we're not expecting this to be a big list, and
1765 : : // this avoids needing a signature hash function.
1766 : 903245 : lambda_sig_count *sig;
1767 : 903245 : if (unsigned ix = vec_safe_length (slot->discriminators))
1768 : 186041 : for (sig = slot->discriminators->begin (); ix--; sig++)
1769 : 122383 : if (compare_lambda_sig (fn, sig->fn))
1770 : 52076 : goto found;
1771 : 851169 : {
1772 : 851169 : lambda_sig_count init = {fn, 0};
1773 : 851169 : sig = vec_safe_push (slot->discriminators, init);
1774 : : }
1775 : 903245 : found:
1776 : 903245 : LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda) = sig->count++;
1777 : 903245 : }
1778 : :
1779 : : tree
1780 : 903214 : start_lambda_function (tree fco, tree lambda_expr)
1781 : : {
1782 : : /* Let the front end know that we are going to be defining this
1783 : : function. */
1784 : 903214 : start_preparsed_function (fco,
1785 : : NULL_TREE,
1786 : : SF_PRE_PARSED | SF_INCLASS_INLINE);
1787 : :
1788 : 903214 : tree body = begin_function_body ();
1789 : :
1790 : : /* Push the proxies for any explicit captures. */
1791 : 1639889 : for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
1792 : 736675 : cap = TREE_CHAIN (cap))
1793 : 736675 : build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap));
1794 : :
1795 : 903214 : return body;
1796 : : }
1797 : :
1798 : : /* Subroutine of prune_lambda_captures: CAP is a node in
1799 : : LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
1800 : : might optimize away the capture, or NULL_TREE if there is no such
1801 : : variable. */
1802 : :
1803 : : static tree
1804 : 297 : var_to_maybe_prune (tree cap)
1805 : : {
1806 : 297 : if (LAMBDA_CAPTURE_EXPLICIT_P (cap))
1807 : : /* Don't prune explicit captures. */
1808 : : return NULL_TREE;
1809 : :
1810 : 288 : tree mem = TREE_PURPOSE (cap);
1811 : 576 : if (!DECL_P (mem) || !DECL_NORMAL_CAPTURE_P (mem))
1812 : : /* Packs and init-captures aren't captures of constant vars. */
1813 : : return NULL_TREE;
1814 : :
1815 : 288 : tree init = TREE_VALUE (cap);
1816 : 288 : if (is_normal_capture_proxy (init))
1817 : 3 : init = DECL_CAPTURED_VARIABLE (init);
1818 : 288 : if (decl_constant_var_p (init))
1819 : : return init;
1820 : :
1821 : : return NULL_TREE;
1822 : : }
1823 : :
1824 : : /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
1825 : : for constant variables are actually used in the lambda body.
1826 : :
1827 : : There will always be a DECL_EXPR for the capture proxy; remember it when we
1828 : : see it, but replace it with any other use. */
1829 : :
1830 : : static tree
1831 : 5572 : mark_const_cap_r (tree *t, int *walk_subtrees, void *data)
1832 : : {
1833 : 5572 : hash_map<tree,tree*> &const_vars = *(hash_map<tree,tree*>*)data;
1834 : :
1835 : 5572 : tree var = NULL_TREE;
1836 : 5572 : if (TREE_CODE (*t) == DECL_EXPR)
1837 : : {
1838 : 418 : tree decl = DECL_EXPR_DECL (*t);
1839 : 418 : if (is_constant_capture_proxy (decl))
1840 : : {
1841 : 175 : var = DECL_CAPTURED_VARIABLE (decl);
1842 : 175 : *walk_subtrees = 0;
1843 : : }
1844 : : }
1845 : 5154 : else if (!location_wrapper_p (*t) /* is_capture_proxy dislikes them. */
1846 : 5154 : && is_constant_capture_proxy (*t))
1847 : 21 : var = DECL_CAPTURED_VARIABLE (*t);
1848 : :
1849 : 5572 : if (var)
1850 : : {
1851 : 196 : tree *&slot = const_vars.get_or_insert (var);
1852 : 196 : if (!slot || VAR_P (*t))
1853 : 196 : slot = t;
1854 : : }
1855 : :
1856 : 5572 : return NULL_TREE;
1857 : : }
1858 : :
1859 : : /* We're at the end of processing a lambda; go back and remove any captures of
1860 : : constant variables for which we've folded away all uses. */
1861 : :
1862 : : static void
1863 : 903214 : prune_lambda_captures (tree body)
1864 : : {
1865 : 903214 : tree lam = current_lambda_expr ();
1866 : 903214 : if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam))
1867 : : /* No uses were optimized away. */
1868 : 903059 : return;
1869 : 206 : if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
1870 : : /* No default captures, and we don't prune explicit captures. */
1871 : : return;
1872 : : /* Don't bother pruning in a template, we'll prune at instantiation time. */
1873 : 194 : if (dependent_type_p (TREE_TYPE (lam)))
1874 : : return;
1875 : :
1876 : 155 : hash_map<tree,tree*> const_vars;
1877 : :
1878 : 155 : cp_walk_tree_without_duplicates (&body, mark_const_cap_r, &const_vars);
1879 : :
1880 : 155 : tree bind_expr = expr_single (DECL_SAVED_TREE (lambda_function (lam)));
1881 : 322 : bool noexcept_p = (bind_expr
1882 : 155 : && TREE_CODE (bind_expr) == MUST_NOT_THROW_EXPR);
1883 : 12 : if (noexcept_p)
1884 : 12 : bind_expr = expr_single (TREE_OPERAND (bind_expr, 0));
1885 : :
1886 : 155 : tree *fieldp = &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam));
1887 : 452 : for (tree *capp = &LAMBDA_EXPR_CAPTURE_LIST (lam); *capp; )
1888 : : {
1889 : 297 : tree cap = *capp;
1890 : 297 : if (tree var = var_to_maybe_prune (cap))
1891 : : {
1892 : 172 : tree **use = const_vars.get (var);
1893 : 172 : if (TREE_CODE (**use) == DECL_EXPR)
1894 : : {
1895 : : /* All uses of this capture were folded away, leaving only the
1896 : : proxy declaration. */
1897 : :
1898 : 151 : if (noexcept_p)
1899 : : {
1900 : : /* We didn't handle noexcept lambda captures correctly before
1901 : : the fix for PR c++/119764. */
1902 : 33 : if (abi_version_crosses (21))
1903 : 3 : warning_at (location_of (lam), OPT_Wabi, "%qD is no longer"
1904 : : " captured in noexcept lambda in ABI v21 "
1905 : : "(GCC 16)", var);
1906 : 12 : if (!abi_version_at_least (21))
1907 : 0 : goto next;
1908 : : }
1909 : :
1910 : : /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
1911 : 151 : *capp = TREE_CHAIN (cap);
1912 : :
1913 : : /* And out of TYPE_FIELDS. */
1914 : 151 : tree field = TREE_PURPOSE (cap);
1915 : 276 : while (*fieldp != field)
1916 : 125 : fieldp = &DECL_CHAIN (*fieldp);
1917 : 151 : *fieldp = DECL_CHAIN (*fieldp);
1918 : :
1919 : : /* And out of the bindings for the function. */
1920 : 151 : tree *blockp = &BLOCK_VARS (current_binding_level->blocks);
1921 : 197 : while (*blockp != DECL_EXPR_DECL (**use))
1922 : 46 : blockp = &DECL_CHAIN (*blockp);
1923 : 151 : *blockp = DECL_CHAIN (*blockp);
1924 : :
1925 : : /* And maybe out of the vars declared in the containing
1926 : : BIND_EXPR, if it's listed there. */
1927 : 151 : tree *bindp = &BIND_EXPR_VARS (bind_expr);
1928 : 366 : while (*bindp && *bindp != DECL_EXPR_DECL (**use))
1929 : 64 : bindp = &DECL_CHAIN (*bindp);
1930 : 151 : if (*bindp)
1931 : 119 : *bindp = DECL_CHAIN (*bindp);
1932 : :
1933 : : /* And remove the capture proxy declaration. */
1934 : 151 : **use = void_node;
1935 : 151 : continue;
1936 : 151 : }
1937 : : }
1938 : :
1939 : 146 : next:
1940 : 146 : capp = &TREE_CHAIN (cap);
1941 : : }
1942 : 155 : }
1943 : :
1944 : : // Record the per-scope per-signature discriminator of LAMBDA. If the
1945 : : // extra scope is empty, we must use the empty scope counter, which
1946 : : // might not be the live one.
1947 : :
1948 : : void
1949 : 903214 : finish_lambda_function (tree body)
1950 : : {
1951 : 903214 : finish_function_body (body);
1952 : :
1953 : 903214 : prune_lambda_captures (cur_stmt_list);
1954 : :
1955 : : /* Finish the function and generate code for it if necessary. */
1956 : 903214 : tree fn = finish_function (/*inline_p=*/true);
1957 : :
1958 : : /* Only expand if the call op is not a template. */
1959 : 903214 : if (!DECL_TEMPLATE_INFO (fn))
1960 : 266699 : expand_or_defer_fn (fn);
1961 : 903214 : }
1962 : :
1963 : : #include "gt-cp-lambda.h"
|