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