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 1665866 : build_lambda_expr (void)
42 : {
43 1665866 : tree lambda = make_node (LAMBDA_EXPR);
44 1665866 : LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
45 1665866 : LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
46 1665866 : LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
47 1665866 : LAMBDA_EXPR_REGEN_INFO (lambda) = NULL_TREE;
48 1665866 : LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
49 1665866 : return lambda;
50 : }
51 :
52 : /* Create the closure object for a LAMBDA_EXPR. */
53 :
54 : tree
55 1667546 : build_lambda_object (tree lambda_expr)
56 : {
57 : /* Build aggregate constructor call.
58 : - cp_parser_braced_list
59 : - cp_parser_functional_cast */
60 1667546 : vec<constructor_elt, va_gc> *elts = NULL;
61 1667546 : tree node, expr, type;
62 :
63 1667546 : 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 1667531 : 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 405333 : location_t loc = LAMBDA_EXPR_LOCATION (lambda_expr);
74 405333 : iloc_sentinel il (loc);
75 :
76 405333 : for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
77 1015572 : node;
78 610239 : node = TREE_CHAIN (node))
79 : {
80 610239 : tree field = TREE_PURPOSE (node);
81 610239 : tree val = TREE_VALUE (node);
82 :
83 610239 : if (field == error_mark_node)
84 : {
85 0 : expr = error_mark_node;
86 0 : goto out;
87 : }
88 :
89 610239 : 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 610239 : if (DECL_P (val))
94 294242 : 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 610239 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
99 19 : val = build_array_copy (val);
100 610220 : else if (DECL_NORMAL_CAPTURE_P (field)
101 608938 : && !DECL_VLA_CAPTURE_P (field)
102 1219116 : && !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 282545 : val = force_rvalue (val, tf_warning_or_error);
112 282545 : if (TREE_CODE (val) == TARGET_EXPR)
113 1166 : TARGET_EXPR_DIRECT_INIT_P (val) = true;
114 : }
115 :
116 610239 : CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
117 : }
118 :
119 405333 : expr = build_constructor (init_list_type_node, elts);
120 405333 : 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 405333 : type = LAMBDA_EXPR_CLOSURE (lambda_expr);
125 405333 : CLASSTYPE_NON_AGGREGATE (type) = 0;
126 405333 : expr = finish_compound_literal (type, expr, tf_warning_or_error);
127 405333 : protected_set_expr_location (expr, loc);
128 405333 : CLASSTYPE_NON_AGGREGATE (type) = 1;
129 :
130 405333 : out:
131 405333 : return expr;
132 405333 : }
133 :
134 : /* Return an initialized RECORD_TYPE for LAMBDA.
135 : LAMBDA must have its explicit captures already. */
136 :
137 : tree
138 1665857 : begin_lambda_type (tree lambda)
139 : {
140 : /* Lambda names are nearly but not quite anonymous. */
141 1665857 : tree name = make_anon_name ();
142 1665857 : IDENTIFIER_LAMBDA_P (name) = true;
143 :
144 : /* Create the new RECORD_TYPE for this lambda. */
145 1665857 : tree type = xref_tag (/*tag_code=*/record_type, name);
146 1665857 : 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 1665857 : CLASSTYPE_DECLARED_CLASS (type) = false;
151 :
152 : /* Cross-reference the expression and the type. */
153 1665857 : LAMBDA_EXPR_CLOSURE (lambda) = type;
154 1665857 : 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 1665857 : if (cxx_dialect >= cxx17)
159 1661955 : CLASSTYPE_LITERAL_P (type) = true;
160 :
161 : /* Clear base types. */
162 1665857 : xref_basetypes (type, /*bases=*/NULL_TREE);
163 :
164 : /* Start the class. */
165 1665857 : type = begin_class_definition (type);
166 :
167 1665857 : 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 14869856 : lambda_function (tree lambda)
175 : {
176 14869856 : tree type;
177 14869856 : if (TREE_CODE (lambda) == LAMBDA_EXPR)
178 6391464 : type = LAMBDA_EXPR_CLOSURE (lambda);
179 : else
180 : type = lambda;
181 29739712 : gcc_assert (LAMBDA_TYPE_P (type));
182 : /* Don't let debug_tree cause instantiation. */
183 14869856 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
184 14869856 : && !COMPLETE_OR_OPEN_TYPE_P (type))
185 : return NULL_TREE;
186 14869856 : lambda = get_class_binding_direct (type, call_op_identifier);
187 14869856 : if (lambda)
188 14869824 : 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 2031769 : type_deducible_expression_p (tree expr)
197 : {
198 2031769 : if (!type_dependent_expression_p (expr))
199 : return true;
200 0 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
201 1275173 : || TREE_CODE (expr) == EXPR_PACK_EXPANSION)
202 : return false;
203 1275173 : tree t = non_reference (TREE_TYPE (expr));
204 1275173 : return (t && TREE_CODE (t) != TYPE_PACK_EXPANSION
205 532564 : && !WILDCARD_TYPE_P (t) && !LAMBDA_TYPE_P (t)
206 617045 : && !array_of_unknown_bound_p (t)
207 1892179 : && !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 2046243 : lambda_capture_field_type (tree expr, bool explicit_init_p,
217 : bool by_reference_p)
218 : {
219 2046243 : tree type;
220 2046243 : bool is_this = is_this_parameter (tree_strip_nop_conversions (expr));
221 :
222 2046243 : if (explicit_init_p)
223 : {
224 14474 : 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 14291 : tree auto_node = make_auto ();
234 14291 : type = auto_node;
235 14291 : if (by_reference_p)
236 : /* Add the reference now, so deduction doesn't lose
237 : outermost CV qualifiers of EXPR. */
238 72 : type = build_reference_type (type);
239 14291 : type = do_auto_deduction (type, expr, auto_node);
240 : }
241 : }
242 2031769 : else if (!type_deducible_expression_p (expr))
243 : {
244 658170 : type = cxx_make_type (DECLTYPE_TYPE);
245 658170 : DECLTYPE_TYPE_EXPR (type) = expr;
246 658170 : DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
247 658170 : DECLTYPE_FOR_REF_CAPTURE (type) = by_reference_p;
248 658170 : SET_TYPE_STRUCTURAL_EQUALITY (type);
249 : }
250 : else
251 : {
252 1373599 : STRIP_ANY_LOCATION_WRAPPER (expr);
253 :
254 1373599 : 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 1373599 : type = non_reference (unlowered_expr_type (expr));
265 :
266 1373599 : if ((by_reference_p && !is_this) || TREE_CODE (type) == FUNCTION_TYPE)
267 723134 : type = build_reference_type (type);
268 : }
269 :
270 2046243 : 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 5937750781 : is_capture_proxy (tree decl)
278 : {
279 : /* Location wrappers should be stripped or otherwise handled by the
280 : caller before using this predicate. */
281 5937750781 : gcc_checking_assert (!location_wrapper_p (decl));
282 :
283 5937750781 : return (VAR_P (decl)
284 1652257543 : && DECL_HAS_VALUE_EXPR_P (decl)
285 44347947 : && !DECL_ANON_UNION_VAR_P (decl)
286 44345607 : && !DECL_DECOMPOSITION_P (decl)
287 40151373 : && !DECL_FNAME_P (decl)
288 39769426 : && !(DECL_ARTIFICIAL (decl)
289 39767644 : && DECL_LANG_SPECIFIC (decl)
290 38418857 : && DECL_OMP_PRIVATIZED_MEMBER (decl))
291 6017250918 : && 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 5621109585 : is_normal_capture_proxy (tree decl)
299 : {
300 5621109585 : if (!is_capture_proxy (decl))
301 : /* It's not a capture proxy. */
302 : return false;
303 :
304 30081813 : return (DECL_LANG_SPECIFIC (decl)
305 30081813 : && 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 5546063 : strip_normal_capture_proxy (tree decl)
313 : {
314 5598990 : while (is_normal_capture_proxy (decl))
315 52927 : decl = DECL_CAPTURED_VARIABLE (decl);
316 5546063 : 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 72132 : is_constant_capture_proxy (tree decl)
324 : {
325 72132 : if (is_normal_capture_proxy (decl))
326 7538 : 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 2389097 : insert_capture_proxy (tree var)
335 : {
336 2389097 : if (is_normal_capture_proxy (var))
337 : {
338 2373682 : tree cap = DECL_CAPTURED_VARIABLE (var);
339 2373682 : if (CHECKING_P)
340 : {
341 2373682 : gcc_assert (!is_normal_capture_proxy (cap));
342 2373682 : tree old = retrieve_local_specialization (cap);
343 2373682 : if (old)
344 21986 : gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var));
345 : }
346 2373682 : 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 2389097 : pushdecl_outermost_localscope (var);
352 :
353 : /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
354 2389097 : 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 2389097 : unsigned index = 1 + use_eh_spec_block (current_function_decl);
359 2389097 : tree stmt_list = (*stmt_list_stack)[index];
360 2389097 : gcc_assert (stmt_list);
361 2389097 : append_to_statement_list_force (var, &stmt_list);
362 2389097 : }
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 1665857 : insert_pending_capture_proxies (void)
370 : {
371 1665857 : tree lam;
372 1665857 : vec<tree, va_gc> *proxies;
373 1665857 : unsigned i;
374 :
375 1786998 : if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
376 : return;
377 :
378 29114 : lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
379 29114 : proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
380 36481 : for (i = 0; i < vec_safe_length (proxies); ++i)
381 : {
382 7367 : tree var = (*proxies)[i];
383 7367 : insert_capture_proxy (var);
384 : }
385 29114 : release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
386 29114 : 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 2630599 : lambda_proxy_type (tree ref)
396 : {
397 2630599 : tree type;
398 2630599 : if (ref == error_mark_node)
399 : return error_mark_node;
400 2630581 : if (REFERENCE_REF_P (ref))
401 0 : ref = TREE_OPERAND (ref, 0);
402 2630581 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
403 2630581 : type = TREE_TYPE (ref);
404 2630581 : if (!type || WILDCARD_TYPE_P (non_reference (type)))
405 : {
406 837575 : type = cxx_make_type (DECLTYPE_TYPE);
407 837575 : DECLTYPE_TYPE_EXPR (type) = ref;
408 837575 : DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
409 837575 : SET_TYPE_STRUCTURAL_EQUALITY (type);
410 : }
411 2630581 : if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
412 3008 : 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 3377803 : build_capture_proxy (tree member, tree init, bool early_p)
423 : {
424 3377803 : tree var, object, fn, closure, name, lam, type;
425 :
426 3377803 : if (PACK_EXPANSION_P (member))
427 2990 : member = PACK_EXPANSION_PATTERN (member);
428 :
429 3377803 : closure = DECL_CONTEXT (member);
430 3377803 : fn = lambda_function (closure);
431 3377803 : lam = CLASSTYPE_LAMBDA_EXPR (closure);
432 :
433 3377803 : object = DECL_ARGUMENTS (fn);
434 : /* The proxy variable forwards to the capture field. */
435 3377803 : if (INDIRECT_TYPE_P (TREE_TYPE (object)))
436 3377587 : object = build_fold_indirect_ref (object);
437 3377803 : object = finish_non_static_data_member (member, object, NULL_TREE);
438 3377803 : if (REFERENCE_REF_P (object))
439 1304539 : object = TREE_OPERAND (object, 0);
440 :
441 : /* Remove the __ inserted by add_capture. */
442 3377803 : if (IDENTIFIER_POINTER (DECL_NAME (member))[2] == '_'
443 3377803 : && IDENTIFIER_POINTER (DECL_NAME (member))[3] == '.')
444 36 : name = get_identifier ("_");
445 : else
446 3377767 : name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
447 :
448 3377803 : 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 747614 : type = cp_build_qualified_type (TREE_TYPE (member), TYPE_QUAL_CONST);
454 : else
455 : {
456 2630189 : type = lambda_proxy_type (object);
457 2630189 : 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 3377803 : 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 3377803 : complete_type (type);
479 :
480 3377803 : var = build_decl (input_location, VAR_DECL, name, type);
481 3377803 : SET_DECL_VALUE_EXPR (var, object);
482 3377803 : DECL_HAS_VALUE_EXPR_P (var) = 1;
483 3377803 : DECL_ARTIFICIAL (var) = 1;
484 3377803 : TREE_USED (var) = 1;
485 3377803 : DECL_CONTEXT (var) = fn;
486 :
487 3377803 : if (DECL_NORMAL_CAPTURE_P (member))
488 : {
489 3347194 : 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 3347122 : if (PACK_EXPANSION_P (init))
498 2648 : init = PACK_EXPANSION_PATTERN (init);
499 : }
500 :
501 3347194 : init = strip_contract_const_wrapper (init);
502 :
503 3347194 : if (INDIRECT_REF_P (init))
504 715469 : init = TREE_OPERAND (init, 0);
505 3347194 : STRIP_NOPS (init);
506 :
507 3347194 : gcc_assert (VAR_P (init) || TREE_CODE (init) == PARM_DECL);
508 3347194 : init = strip_normal_capture_proxy (init);
509 3347194 : retrofit_lang_decl (var);
510 3347194 : DECL_CAPTURED_VARIABLE (var) = init;
511 : }
512 :
513 3377803 : if (name == this_identifier)
514 : {
515 747892 : if (early_p)
516 : return var;
517 401828 : gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
518 401828 : LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
519 : }
520 :
521 3031739 : if (early_p)
522 : {
523 804183 : gcc_checking_assert (current_binding_level->kind == sk_lambda);
524 : /* insert_capture_proxy below wouldn't push into the lambda scope. */
525 804183 : pushdecl (var);
526 : }
527 2227556 : else if (fn == current_function_decl)
528 2220189 : insert_capture_proxy (var);
529 : else
530 7367 : 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 1894387 : add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
567 : bool explicit_init_p, unsigned *name_independent_cnt)
568 : {
569 1894387 : char *buf;
570 1894387 : tree type, member, name;
571 1894387 : bool vla = false;
572 1894387 : bool variadic = false;
573 1894387 : tree initializer = orig_init;
574 :
575 1894387 : if (PACK_EXPANSION_P (initializer))
576 : {
577 1495 : initializer = PACK_EXPANSION_PATTERN (initializer);
578 : variadic = true;
579 : }
580 :
581 1894387 : if (TREE_CODE (initializer) == TREE_LIST
582 : /* A pack expansion might end up with multiple elements. */
583 1894387 : && !PACK_EXPANSION_P (TREE_VALUE (initializer)))
584 15 : initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
585 : tf_warning_or_error);
586 1894387 : type = TREE_TYPE (initializer);
587 1894387 : if (type == error_mark_node)
588 : return error_mark_node;
589 :
590 1894363 : 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 1894318 : else if (!dependent_type_p (type)
608 1894318 : && 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 1894300 : type = lambda_capture_field_type (initializer, explicit_init_p,
622 : by_reference_p);
623 1894300 : if (type == error_mark_node)
624 : return error_mark_node;
625 :
626 1894297 : 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 1894297 : if (dependent_type_p (type))
634 : ;
635 607798 : else if (id != this_identifier && by_reference_p)
636 : {
637 262217 : if (!lvalue_p (initializer))
638 : {
639 3 : error ("cannot capture %qE by reference", initializer);
640 3 : return error_mark_node;
641 : }
642 : }
643 : else
644 : {
645 : /* Capture by copy requires a complete type. */
646 345581 : type = complete_type (type);
647 345581 : if (!COMPLETE_TYPE_P (type))
648 : {
649 6 : auto_diagnostic_group d;
650 6 : error ("capture by copy of incomplete type %qT", type);
651 6 : cxx_incomplete_type_inform (type);
652 6 : return error_mark_node;
653 6 : }
654 345575 : else if (!verify_type_context (input_location,
655 : TCTX_CAPTURE_BY_COPY, type))
656 0 : return error_mark_node;
657 : }
658 :
659 1894288 : if (cxx_dialect < cxx20 && !explicit_init_p)
660 : {
661 8154 : auto_diagnostic_group d;
662 8154 : tree stripped_init = tree_strip_any_location_wrapper (initializer);
663 4239 : if (DECL_DECOMPOSITION_P (stripped_init)
664 12393 : && pedwarn (input_location, OPT_Wc__20_extensions,
665 : "captured structured bindings are a C++20 extension"))
666 179 : inform (DECL_SOURCE_LOCATION (stripped_init), "declared here");
667 8154 : }
668 : }
669 :
670 : /* Add __ to the beginning of the field name so that user code
671 : won't find the field with name lookup. We can't just leave the name
672 : unset because template instantiation uses the name to find
673 : instantiated fields. */
674 1894333 : if (id_equal (id, "_") && name_independent_cnt)
675 : {
676 48 : if (*name_independent_cnt == 0)
677 30 : name = get_identifier ("___");
678 : else
679 : {
680 : /* For 2nd and later name-independent capture use
681 : unique names. */
682 18 : char buf2[5 + (HOST_BITS_PER_INT + 2) / 3];
683 18 : sprintf (buf2, "___.%u", *name_independent_cnt);
684 18 : name = get_identifier (buf2);
685 : }
686 48 : name_independent_cnt[0]++;
687 : }
688 : else
689 : {
690 1894285 : buf = XALLOCAVEC (char, IDENTIFIER_LENGTH (id) + 3);
691 1894285 : buf[1] = buf[0] = '_';
692 1894285 : memcpy (buf + 2, IDENTIFIER_POINTER (id),
693 1894285 : IDENTIFIER_LENGTH (id) + 1);
694 1894285 : name = get_identifier (buf);
695 : }
696 :
697 1894333 : if (variadic)
698 : {
699 1495 : type = make_pack_expansion (type);
700 1495 : if (explicit_init_p)
701 : /* With an explicit initializer 'type' is auto, which isn't really a
702 : parameter pack in this context. We will want as many fields as we
703 : have elements in the expansion of the initializer, so use its packs
704 : instead. */
705 : {
706 354 : PACK_EXPANSION_PARAMETER_PACKS (type)
707 177 : = uses_parameter_packs (initializer);
708 177 : PACK_EXPANSION_AUTO_P (type) = true;
709 : }
710 : }
711 :
712 : /* Make member variable. */
713 1894333 : member = build_decl (input_location, FIELD_DECL, name, type);
714 1894333 : DECL_VLA_CAPTURE_P (member) = vla;
715 :
716 1894333 : if (!explicit_init_p)
717 : /* Normal captures are invisible to name lookup but uses are replaced
718 : with references to the capture field; we implement this by only
719 : really making them invisible in unevaluated context; see
720 : qualify_lookup. For now, let's make explicitly initialized captures
721 : always visible. */
722 1879862 : DECL_NORMAL_CAPTURE_P (member) = true;
723 :
724 1894333 : if (id == this_identifier)
725 367494 : LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
726 :
727 : /* Add it to the appropriate closure class if we've started it. */
728 1894333 : if (current_class_type
729 1894333 : && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
730 : {
731 1077316 : if (COMPLETE_TYPE_P (current_class_type))
732 : {
733 : /* This can happen for code like [&]<auto e> { [:e:]; } where
734 : we can't figure out what the splice will designate while parsing;
735 : we'll only know it after we've substituted the splice, but then
736 : it's too late to capture anything. This code is ill-formed as
737 : per [expr.prim.splice]/2.1.4.1: The expression is ill-formed if
738 : S is ... a local entity such that ... there is a lambda scope that
739 : intervenes between the expression and the point at which S was
740 : introduced and the expression would be potentially evaluated. */
741 4 : error ("trying to capture %qD in instantiation of generic lambda",
742 : id);
743 4 : return error_mark_node;
744 : }
745 1077312 : finish_member_declaration (member);
746 : }
747 :
748 1894329 : tree listmem = member;
749 1894329 : if (variadic)
750 : {
751 1495 : listmem = make_pack_expansion (member);
752 1495 : initializer = orig_init;
753 : }
754 1894329 : LAMBDA_EXPR_CAPTURE_LIST (lambda)
755 1894329 : = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
756 :
757 1894329 : if (LAMBDA_EXPR_CLOSURE (lambda))
758 1077312 : return build_capture_proxy (member, initializer, /*early_p=*/false);
759 : /* For explicit captures we haven't started the function yet, so we wait
760 : and build the proxy from cp_parser_lambda_body. */
761 817017 : LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda)) = true;
762 817017 : return NULL_TREE;
763 : }
764 :
765 : /* Register all the capture members on the list CAPTURES, which is the
766 : LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
767 :
768 : void
769 2816104 : register_capture_members (tree captures)
770 : {
771 2816104 : if (captures == NULL_TREE)
772 : return;
773 :
774 1150247 : register_capture_members (TREE_CHAIN (captures));
775 :
776 1150247 : tree field = TREE_PURPOSE (captures);
777 1150247 : if (PACK_EXPANSION_P (field))
778 1495 : field = PACK_EXPANSION_PATTERN (field);
779 :
780 1150247 : finish_member_declaration (field);
781 : }
782 :
783 : /* Similar to add_capture, except this works on a stack of nested lambdas.
784 : BY_REFERENCE_P in this case is derived from the default capture mode.
785 : Returns the capture for the lambda at the bottom of the stack. */
786 :
787 : tree
788 1069967 : add_default_capture (tree lambda_stack, tree id, tree initializer)
789 : {
790 1069967 : bool this_capture_p = (id == this_identifier);
791 1069967 : tree var = NULL_TREE;
792 1069967 : tree saved_class_type = current_class_type;
793 :
794 1069967 : for (tree node = lambda_stack;
795 2147307 : node;
796 1077340 : node = TREE_CHAIN (node))
797 : {
798 1077340 : tree lambda = TREE_VALUE (node);
799 :
800 1077340 : current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
801 1077340 : if (DECL_PACK_P (initializer))
802 0 : initializer = make_pack_expansion (initializer);
803 2154680 : var = add_capture (lambda,
804 : id,
805 : initializer,
806 : /*by_reference_p=*/
807 : (this_capture_p
808 1021576 : || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
809 1021576 : == CPLD_REFERENCE)),
810 : /*explicit_init_p=*/false, NULL);
811 1077340 : initializer = convert_from_reference (var);
812 :
813 : /* Warn about deprecated implicit capture of this via [=]. */
814 1077340 : if (cxx_dialect >= cxx20
815 1071118 : && this_capture_p
816 1132849 : && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_COPY)
817 : {
818 64 : auto_diagnostic_group d;
819 64 : if (warning_at (LAMBDA_EXPR_LOCATION (lambda), OPT_Wdeprecated,
820 : "implicit capture of %qE via %<[=]%> is deprecated "
821 : "in C++20", this_identifier))
822 60 : inform (LAMBDA_EXPR_LOCATION (lambda), "add explicit %<this%> or "
823 : "%<*this%> capture");
824 64 : }
825 : }
826 :
827 1069967 : current_class_type = saved_class_type;
828 :
829 1069967 : return var;
830 : }
831 :
832 : /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
833 : form of an INDIRECT_REF, possibly adding it through default
834 : capturing, if ADD_CAPTURE_P is nonzero. If ADD_CAPTURE_P is negative,
835 : try to capture but don't complain if we can't. */
836 :
837 : tree
838 1143808 : lambda_expr_this_capture (tree lambda, int add_capture_p)
839 : {
840 1143808 : tree result;
841 :
842 1143808 : tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
843 1143808 : if (this_capture)
844 1025561 : if (tree spec = retrieve_local_specialization (this_capture))
845 : {
846 2328 : gcc_checking_assert (generic_lambda_fn_p (lambda_function (lambda)));
847 : this_capture = spec;
848 : }
849 :
850 : /* In unevaluated context this isn't an odr-use, so don't capture. */
851 1143808 : if (cp_unevaluated_operand)
852 136 : add_capture_p = false;
853 :
854 : /* If we captured 'this' but don't have a capture proxy yet, look up the
855 : captured 'this' again. */
856 1143808 : if (this_capture && TREE_CODE (this_capture) == FIELD_DECL)
857 : {
858 0 : gcc_assert (!add_capture_p);
859 : this_capture = NULL_TREE;
860 : }
861 :
862 : /* Try to default capture 'this' if we can. */
863 : if (!this_capture)
864 : {
865 : tree lambda_stack = NULL_TREE;
866 121271 : tree init = NULL_TREE;
867 : bool saw_complete = false;
868 :
869 : /* If we are in a lambda function, we can move out until we hit:
870 : 1. a non-lambda function or NSDMI,
871 : 2. a lambda function capturing 'this', or
872 : 3. a non-default capturing lambda function. */
873 : for (tree tlambda = lambda; ;)
874 : {
875 121271 : if (add_capture_p
876 177054 : && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
877 : /* tlambda won't let us capture 'this'. */
878 : break;
879 :
880 121255 : if (add_capture_p)
881 55767 : lambda_stack = tree_cons (NULL_TREE,
882 : tlambda,
883 : lambda_stack);
884 :
885 121255 : tree closure = LAMBDA_EXPR_CLOSURE (tlambda);
886 121255 : if (COMPLETE_TYPE_P (closure))
887 : /* We're instantiating a generic lambda op(), the containing
888 : scope may be gone. */
889 8574 : saw_complete = true;
890 :
891 121255 : tree containing_function
892 121255 : = decl_function_context (TYPE_NAME (closure));
893 :
894 121255 : tree ex = LAMBDA_EXPR_EXTRA_SCOPE (tlambda);
895 121255 : if (ex && TREE_CODE (ex) == FIELD_DECL)
896 : {
897 : /* Lambda in an NSDMI. We don't have a function to look up
898 : 'this' in, but we can find (or rebuild) the fake one from
899 : inject_this_parameter. */
900 63 : if (!containing_function && !saw_complete)
901 : /* If we're parsing a lambda in a non-local class,
902 : we can find the fake 'this' in scope_chain. */
903 45 : init = scope_chain->x_current_class_ptr;
904 : else
905 : /* Otherwise it's either gone or buried in
906 : function_context_stack, so make another. */
907 18 : init = build_this_parm (NULL_TREE, DECL_CONTEXT (ex),
908 : TYPE_UNQUALIFIED);
909 63 : gcc_checking_assert
910 : (init && (TREE_TYPE (TREE_TYPE (init))
911 : == current_nonlambda_class_type ()));
912 : break;
913 : }
914 :
915 121192 : if (containing_function == NULL_TREE)
916 : /* We ran out of scopes; there's no 'this' to capture. */
917 : break;
918 :
919 120669 : if (!LAMBDA_FUNCTION_P (containing_function))
920 : {
921 : /* We found a non-lambda function.
922 : There is no this pointer in xobj member functions. */
923 114411 : if (DECL_IOBJ_MEMBER_FUNCTION_P (containing_function))
924 : /* First parameter is 'this'. */
925 103273 : init = DECL_ARGUMENTS (containing_function);
926 : break;
927 : }
928 :
929 3048 : tlambda
930 3048 : = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
931 :
932 3048 : if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
933 : {
934 : /* An outer lambda has already captured 'this'. */
935 : init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
936 : break;
937 : }
938 : }
939 :
940 107109 : if (init)
941 : {
942 103360 : if (add_capture_p)
943 55735 : this_capture = add_default_capture (lambda_stack,
944 : /*id=*/this_identifier,
945 : init);
946 : else
947 : this_capture = init;
948 : }
949 : }
950 :
951 1143808 : if (cp_unevaluated_operand)
952 : result = this_capture;
953 1143672 : else if (!this_capture)
954 : {
955 14875 : if (add_capture_p == 1)
956 : {
957 16 : error ("%<this%> was not captured for this lambda function");
958 16 : result = error_mark_node;
959 : }
960 : else
961 : result = NULL_TREE;
962 : }
963 : else
964 : {
965 : /* To make sure that current_class_ref is for the lambda. */
966 1128797 : gcc_assert (!current_class_ref
967 : || (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
968 : == LAMBDA_EXPR_CLOSURE (lambda)));
969 :
970 1128797 : result = this_capture;
971 :
972 : /* If 'this' is captured, each use of 'this' is transformed into an
973 : access to the corresponding unnamed data member of the closure
974 : type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
975 : ensures that the transformed expression is an rvalue. ] */
976 1128797 : result = rvalue (result);
977 : }
978 :
979 1128949 : gcc_checking_assert (!result || result == error_mark_node
980 : || TYPE_PTR_P (TREE_TYPE (result)));
981 :
982 1143808 : return result;
983 : }
984 :
985 : /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
986 :
987 : tree
988 84305923 : current_lambda_expr (void)
989 : {
990 84305923 : tree type = current_class_type;
991 151490501 : while (type && !LAMBDA_TYPE_P (type))
992 30363599 : type = decl_type_context (TYPE_NAME (type));
993 84305923 : if (type)
994 6621946 : return CLASSTYPE_LAMBDA_EXPR (type);
995 : else
996 : return NULL_TREE;
997 : }
998 :
999 : /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
1000 : object. NULL otherwise.. */
1001 :
1002 : static tree
1003 254302708 : resolvable_dummy_lambda (tree object)
1004 : {
1005 254302708 : if (!is_dummy_object (object))
1006 : return NULL_TREE;
1007 :
1008 12579984 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
1009 12579984 : gcc_assert (!TYPE_PTR_P (type));
1010 :
1011 12579984 : tree fn;
1012 12579984 : if (type != current_class_type
1013 8872103 : && current_class_type
1014 10284169 : && LAMBDA_TYPE_P (current_class_type)
1015 481557 : && (fn = lambda_function (current_class_type))
1016 : /* Even dummy lambdas have an operator() since P2036, but the
1017 : dummy operator() doesn't have this set. */
1018 481546 : && DECL_LAMBDA_FUNCTION_P (fn)
1019 13061527 : && DERIVED_FROM_P (type, nonlambda_method_basetype()))
1020 462914 : return CLASSTYPE_LAMBDA_EXPR (current_class_type);
1021 :
1022 : return NULL_TREE;
1023 : }
1024 :
1025 : /* We don't want to capture 'this' until we know we need it, i.e. after
1026 : overload resolution has chosen a non-static member function. At that
1027 : point we call this function to turn a dummy object into a use of the
1028 : 'this' capture. */
1029 :
1030 : tree
1031 152677827 : maybe_resolve_dummy (tree object, bool add_capture_p)
1032 : {
1033 152677827 : if (tree lam = resolvable_dummy_lambda (object))
1034 414598 : if (tree cap = lambda_expr_this_capture (lam, add_capture_p))
1035 414598 : if (cap != error_mark_node)
1036 414592 : object = build_fold_indirect_ref (cap);
1037 :
1038 152677827 : return object;
1039 : }
1040 :
1041 : /* When parsing a generic lambda containing an argument-dependent
1042 : member function call we defer overload resolution to instantiation
1043 : time. But we have to know now whether to capture this or not.
1044 : Do that if FNS contains any non-static fns as per
1045 : [expr.prim.lambda.capture]/7.1. */
1046 :
1047 : void
1048 101624881 : maybe_generic_this_capture (tree object, tree fns)
1049 : {
1050 101624881 : if (tree lam = resolvable_dummy_lambda (object))
1051 48316 : if (!LAMBDA_EXPR_THIS_CAPTURE (lam))
1052 : {
1053 : /* We've not yet captured, so look at the function set of
1054 : interest. */
1055 4399 : if (BASELINK_P (fns))
1056 3320 : fns = BASELINK_FUNCTIONS (fns);
1057 4399 : bool id_expr = TREE_CODE (fns) == TEMPLATE_ID_EXPR;
1058 4399 : if (id_expr)
1059 1085 : fns = TREE_OPERAND (fns, 0);
1060 :
1061 4408 : for (lkp_iterator iter (fns); iter; ++iter)
1062 3323 : if (((!id_expr && TREE_CODE (*iter) != USING_DECL)
1063 1088 : || TREE_CODE (*iter) == TEMPLATE_DECL)
1064 8813 : && DECL_OBJECT_MEMBER_FUNCTION_P (*iter))
1065 : {
1066 : /* Found a non-static member. Capture this. */
1067 4399 : lambda_expr_this_capture (lam, /*maybe*/-1);
1068 4399 : break;
1069 : }
1070 : }
1071 101624881 : }
1072 :
1073 : /* Returns the innermost non-lambda function. If ONLY_SKIP_CONSTEVAL_BLOCK_P,
1074 : we only skip lambda functions that represent consteval blocks. */
1075 :
1076 : tree
1077 297878 : current_nonlambda_function (bool only_skip_consteval_block_p/*=false*/)
1078 : {
1079 297878 : tree fn = current_function_decl;
1080 297878 : tree lam;
1081 603775 : while (fn && LAMBDA_FUNCTION_P (fn)
1082 298845 : && (!only_skip_consteval_block_p
1083 : /* Only keep going if FN represents a consteval block. */
1084 943 : || ((lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fn)))
1085 943 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))))
1086 21 : fn = decl_function_context (fn);
1087 297878 : return fn;
1088 : }
1089 :
1090 : /* Returns the method basetype of the innermost non-lambda function, including
1091 : a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
1092 :
1093 : tree
1094 481953 : nonlambda_method_basetype (void)
1095 : {
1096 481953 : tree type = current_class_type;
1097 963601 : if (!type || !LAMBDA_TYPE_P (type))
1098 398 : return current_class_ref ? type : NULL_TREE;
1099 :
1100 487707 : while (true)
1101 : {
1102 484631 : tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1103 484631 : tree ex = LAMBDA_EXPR_EXTRA_SCOPE (lam);
1104 484631 : if (ex && TREE_CODE (ex) == FIELD_DECL)
1105 : /* Lambda in an NSDMI. */
1106 30 : return DECL_CONTEXT (ex);
1107 :
1108 484601 : tree fn = TYPE_CONTEXT (type);
1109 484601 : if (!fn || TREE_CODE (fn) != FUNCTION_DECL
1110 965439 : || !DECL_OBJECT_MEMBER_FUNCTION_P (fn))
1111 : /* No enclosing non-lambda method. */
1112 : return NULL_TREE;
1113 472495 : if (!LAMBDA_FUNCTION_P (fn))
1114 : /* Found an enclosing non-lambda method. */
1115 466256 : return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1116 3076 : type = DECL_CONTEXT (fn);
1117 3076 : }
1118 : }
1119 :
1120 : /* Like current_scope, but looking through lambdas. If ONLY_SKIP_CLOSURES_P,
1121 : only look through closure types. */
1122 :
1123 : tree
1124 48203007 : current_nonlambda_scope (bool only_skip_closures_p/*=false*/)
1125 : {
1126 48203007 : tree scope = current_scope ();
1127 48318960 : for (;;)
1128 : {
1129 48434715 : if (!only_skip_closures_p
1130 47833378 : && TREE_CODE (scope) == FUNCTION_DECL
1131 90142839 : && LAMBDA_FUNCTION_P (scope))
1132 : {
1133 115755 : scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope));
1134 115755 : continue;
1135 : }
1136 54982274 : else if (LAMBDA_TYPE_P (scope))
1137 : {
1138 198 : scope = CP_TYPE_CONTEXT (scope);
1139 198 : continue;
1140 : }
1141 48203007 : break;
1142 : }
1143 48203007 : return scope;
1144 : }
1145 :
1146 : /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
1147 : indicated FN and NARGS, but do not initialize the return type or any of the
1148 : argument slots. */
1149 :
1150 : static tree
1151 56814 : prepare_op_call (tree fn, int nargs)
1152 : {
1153 56814 : tree t;
1154 :
1155 56814 : t = build_vl_exp (CALL_EXPR, nargs + 3);
1156 56814 : CALL_EXPR_FN (t) = fn;
1157 56814 : CALL_EXPR_STATIC_CHAIN (t) = NULL;
1158 :
1159 56814 : return t;
1160 : }
1161 :
1162 : /* Return true iff CALLOP is the op() for a generic lambda. */
1163 :
1164 : bool
1165 644700997 : generic_lambda_fn_p (tree callop)
1166 : {
1167 697909095 : return (callop && LAMBDA_FUNCTION_P (callop)
1168 7282077 : && DECL_TEMPLATE_INFO (callop)
1169 651900280 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
1170 : }
1171 :
1172 : /* If the closure TYPE has a static op(), also add a conversion to function
1173 : pointer. */
1174 :
1175 : void
1176 1665810 : maybe_add_lambda_conv_op (tree type)
1177 : {
1178 1665810 : bool nested = (cfun != NULL);
1179 1665810 : bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
1180 1665810 : tree callop = lambda_function (type);
1181 1665810 : tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1182 :
1183 1665810 : if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE
1184 521940 : || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE
1185 : /* CWG2561 ...and no explicit object parameter. */
1186 2175408 : || DECL_XOBJ_MEMBER_FUNCTION_P (callop))
1187 1555352 : return;
1188 :
1189 509396 : if (processing_template_decl)
1190 : return;
1191 :
1192 111333 : bool const generic_lambda_p = generic_lambda_fn_p (callop);
1193 :
1194 111333 : if (!generic_lambda_p && undeduced_auto_decl (callop))
1195 : {
1196 : /* If the op() wasn't deduced due to errors, give up. */
1197 46 : gcc_assert (errorcount || sorrycount);
1198 : return;
1199 : }
1200 :
1201 : /* Non-generic non-capturing lambdas only have a conversion function to
1202 : pointer to function when the trailing requires-clause's constraints are
1203 : satisfied. */
1204 111287 : if (!generic_lambda_p && !constraints_satisfied_p (callop))
1205 : return;
1206 :
1207 : /* Non-template conversion operators are defined directly with build_call_a
1208 : and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1209 : deferred and the CALL is built in-place. In the case of a deduced return
1210 : call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1211 : the return type is also built in-place. The arguments of DECLTYPE_CALL in
1212 : the return expression may differ in flags from those in the body CALL. In
1213 : particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1214 : the body CALL, but not in DECLTYPE_CALL. */
1215 :
1216 111269 : vec<tree, va_gc> *direct_argvec = 0;
1217 111269 : tree decltype_call = 0, call = 0;
1218 111269 : tree optype = TREE_TYPE (callop);
1219 111269 : tree fn_result = TREE_TYPE (optype);
1220 :
1221 111269 : tree thisarg = NULL_TREE;
1222 111269 : if (TREE_CODE (optype) == METHOD_TYPE)
1223 110700 : thisarg = build_int_cst (TREE_TYPE (DECL_ARGUMENTS (callop)), 0);
1224 111269 : if (generic_lambda_p)
1225 : {
1226 28539 : ++processing_template_decl;
1227 :
1228 : /* Prepare the dependent member call for the static member function
1229 : '_FUN' and, potentially, prepare another call to be used in a decltype
1230 : return expression for a deduced return call op to allow for simple
1231 : implementation of the conversion operator. */
1232 :
1233 28539 : tree objfn;
1234 28539 : int nargs = list_length (DECL_ARGUMENTS (callop));
1235 28539 : if (thisarg)
1236 : {
1237 28495 : tree instance = cp_build_fold_indirect_ref (thisarg);
1238 28495 : objfn = lookup_template_function (DECL_NAME (callop),
1239 28495 : DECL_TI_ARGS (callop));
1240 28495 : objfn = build_min (COMPONENT_REF, NULL_TREE,
1241 : instance, objfn, NULL_TREE);
1242 28495 : --nargs;
1243 28495 : call = prepare_op_call (objfn, nargs);
1244 : }
1245 : else
1246 : objfn = callop;
1247 :
1248 28539 : if (type_uses_auto (fn_result))
1249 28319 : decltype_call = prepare_op_call (objfn, nargs);
1250 : }
1251 82730 : else if (thisarg)
1252 : {
1253 82205 : direct_argvec = make_tree_vector ();
1254 82205 : direct_argvec->quick_push (thisarg);
1255 : }
1256 :
1257 : /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1258 : declare the static member function "_FUN" below. For each arg append to
1259 : DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1260 : call args (for the template case). If a parameter pack is found, expand
1261 : it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1262 :
1263 111269 : tree fn_args = NULL_TREE;
1264 111269 : {
1265 111269 : int ix = 0;
1266 111269 : tree src = FUNCTION_FIRST_USER_PARM (callop);
1267 111269 : tree tgt = NULL;
1268 :
1269 111269 : if (!thisarg && !decltype_call)
1270 111269 : src = NULL_TREE;
1271 223098 : while (src)
1272 : {
1273 111829 : tree new_node = copy_node (src);
1274 : /* We set DECL_CONTEXT of NEW_NODE to the statfn below.
1275 : Notice this is creating a recursive type! */
1276 :
1277 : /* Clear TREE_ADDRESSABLE on thunk arguments. */
1278 111829 : TREE_ADDRESSABLE (new_node) = 0;
1279 :
1280 111829 : if (!fn_args)
1281 111829 : fn_args = tgt = new_node;
1282 : else
1283 : {
1284 37097 : TREE_CHAIN (tgt) = new_node;
1285 37097 : tgt = new_node;
1286 : }
1287 :
1288 111829 : mark_exp_read (tgt);
1289 :
1290 111829 : if (generic_lambda_p)
1291 : {
1292 52524 : tree a = tgt;
1293 52524 : if (thisarg)
1294 : {
1295 52480 : if (DECL_PACK_P (tgt))
1296 : {
1297 640 : a = make_pack_expansion (a);
1298 640 : PACK_EXPANSION_LOCAL_P (a) = true;
1299 : }
1300 52480 : CALL_EXPR_ARG (call, ix) = a;
1301 : }
1302 :
1303 52524 : if (decltype_call)
1304 : {
1305 : /* Avoid capturing variables in this context. */
1306 52217 : ++cp_unevaluated_operand;
1307 52217 : CALL_EXPR_ARG (decltype_call, ix) = forward_parm (tgt);
1308 52217 : --cp_unevaluated_operand;
1309 : }
1310 :
1311 52524 : ++ix;
1312 : }
1313 : else
1314 59305 : vec_safe_push (direct_argvec, tgt);
1315 :
1316 111829 : src = TREE_CHAIN (src);
1317 : }
1318 : }
1319 :
1320 111269 : if (generic_lambda_p)
1321 : {
1322 28539 : if (decltype_call)
1323 : {
1324 28319 : fn_result = finish_decltype_type
1325 28319 : (decltype_call, /*id_expression_or_member_access_p=*/false,
1326 : tf_warning_or_error);
1327 : }
1328 : }
1329 82730 : else if (thisarg)
1330 : {
1331 : /* Don't warn on deprecated or unavailable lambda declarations, unless
1332 : the lambda is actually called. */
1333 82205 : auto du = make_temp_override (deprecated_state,
1334 82205 : UNAVAILABLE_DEPRECATED_SUPPRESS);
1335 82205 : call = build_call_a (callop, direct_argvec->length (),
1336 : direct_argvec->address ());
1337 82205 : }
1338 :
1339 111269 : if (thisarg)
1340 : {
1341 110700 : CALL_FROM_THUNK_P (call) = 1;
1342 110700 : SET_EXPR_LOCATION (call, UNKNOWN_LOCATION);
1343 : }
1344 :
1345 111269 : tree stattype
1346 111269 : = cp_build_function_type (fn_result,
1347 111269 : FUNCTION_FIRST_USER_PARMTYPE (callop));
1348 111269 : stattype = cp_build_type_attribute_variant (stattype,
1349 111269 : TYPE_ATTRIBUTES (optype));
1350 111269 : if (flag_noexcept_type
1351 111269 : && TYPE_NOTHROW_P (TREE_TYPE (callop)))
1352 117 : stattype = build_exception_variant (stattype, noexcept_true_spec);
1353 :
1354 111269 : if (generic_lambda_p)
1355 28539 : --processing_template_decl;
1356 :
1357 : /* First build up the conversion op. */
1358 :
1359 111269 : tree rettype = build_pointer_type (stattype);
1360 111269 : tree name = make_conv_op_name (rettype);
1361 111269 : tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1362 111269 : tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
1363 : /* DR 1722: The conversion function should be noexcept. */
1364 111269 : fntype = build_exception_variant (fntype, noexcept_true_spec);
1365 111269 : tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
1366 111269 : SET_DECL_LANGUAGE (convfn, lang_cplusplus);
1367 111269 : tree fn = convfn;
1368 111269 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1369 111269 : SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
1370 111269 : grokclassfn (type, fn, NO_SPECIAL);
1371 111269 : set_linkage_according_to_type (type, fn);
1372 111269 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1373 111269 : DECL_IN_AGGR_P (fn) = 1;
1374 111269 : DECL_ARTIFICIAL (fn) = 1;
1375 111269 : DECL_NOT_REALLY_EXTERN (fn) = 1;
1376 111269 : DECL_DECLARED_INLINE_P (fn) = 1;
1377 111269 : DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop);
1378 222538 : if (DECL_IMMEDIATE_FUNCTION_P (callop))
1379 5296 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
1380 111269 : DECL_ARGUMENTS (fn) = build_this_parm (fn, fntype, TYPE_QUAL_CONST);
1381 :
1382 111269 : if (nested_def)
1383 99938 : DECL_INTERFACE_KNOWN (fn) = 1;
1384 :
1385 111269 : if (generic_lambda_p)
1386 28539 : fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1387 :
1388 111269 : add_method (type, fn, false);
1389 :
1390 111269 : if (thisarg == NULL_TREE)
1391 : {
1392 : /* For static lambda, just return operator(). */
1393 569 : if (nested)
1394 172 : push_function_context ();
1395 : else
1396 : /* Still increment function_depth so that we don't GC in the
1397 : middle of an expression. */
1398 397 : ++function_depth;
1399 :
1400 : /* Generate the body of the conversion op. */
1401 :
1402 569 : start_preparsed_function (convfn, NULL_TREE,
1403 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1404 569 : tree body = begin_function_body ();
1405 569 : tree compound_stmt = begin_compound_stmt (0);
1406 :
1407 : /* decl_needed_p needs to see that it's used. */
1408 569 : TREE_USED (callop) = 1;
1409 569 : finish_return_stmt (decay_conversion (callop, tf_warning_or_error));
1410 :
1411 569 : finish_compound_stmt (compound_stmt);
1412 569 : finish_function_body (body);
1413 :
1414 569 : fn = finish_function (/*inline_p=*/true);
1415 569 : if (!generic_lambda_p)
1416 525 : expand_or_defer_fn (fn);
1417 :
1418 569 : if (nested)
1419 172 : pop_function_context ();
1420 : else
1421 397 : --function_depth;
1422 : return;
1423 : }
1424 :
1425 : /* Generic thunk code fails for varargs; we'll complain in mark_used if
1426 : the conversion op is used. */
1427 110700 : if (varargs_function_p (callop))
1428 : {
1429 242 : DECL_DELETED_FN (fn) = 1;
1430 242 : return;
1431 : }
1432 :
1433 : /* Now build up the thunk to be returned. */
1434 :
1435 110458 : tree statfn = build_lang_decl (FUNCTION_DECL, fun_identifier, stattype);
1436 110458 : SET_DECL_LANGUAGE (statfn, lang_cplusplus);
1437 110458 : fn = statfn;
1438 110458 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1439 110458 : grokclassfn (type, fn, NO_SPECIAL);
1440 110458 : set_linkage_according_to_type (type, fn);
1441 110458 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1442 110458 : DECL_IN_AGGR_P (fn) = 1;
1443 110458 : DECL_ARTIFICIAL (fn) = 1;
1444 110458 : DECL_NOT_REALLY_EXTERN (fn) = 1;
1445 110458 : DECL_DECLARED_INLINE_P (fn) = 1;
1446 110458 : DECL_STATIC_FUNCTION_P (fn) = 1;
1447 110458 : DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop);
1448 220916 : if (DECL_IMMEDIATE_FUNCTION_P (callop))
1449 4800 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
1450 110458 : DECL_ARGUMENTS (fn) = fn_args;
1451 222017 : for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
1452 : {
1453 : /* Avoid duplicate -Wshadow warnings. */
1454 111559 : DECL_NAME (arg) = NULL_TREE;
1455 111559 : DECL_CONTEXT (arg) = fn;
1456 : }
1457 110458 : if (nested_def)
1458 99536 : DECL_INTERFACE_KNOWN (fn) = 1;
1459 :
1460 110458 : if (generic_lambda_p)
1461 28483 : fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1462 :
1463 110458 : if (flag_sanitize & SANITIZE_NULL)
1464 : /* Don't UBsan this function; we're deliberately calling op() with a null
1465 : object argument. */
1466 37 : add_no_sanitize_value (fn, SANITIZE_UNDEFINED);
1467 :
1468 110458 : add_method (type, fn, false);
1469 :
1470 110458 : if (nested)
1471 99614 : push_function_context ();
1472 : else
1473 : /* Still increment function_depth so that we don't GC in the
1474 : middle of an expression. */
1475 10844 : ++function_depth;
1476 :
1477 : /* Generate the body of the thunk. */
1478 :
1479 110458 : start_preparsed_function (statfn, NULL_TREE,
1480 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1481 110458 : tree body = begin_function_body ();
1482 110458 : tree compound_stmt = begin_compound_stmt (0);
1483 110458 : if (!generic_lambda_p)
1484 : {
1485 81975 : set_flags_from_callee (call);
1486 81975 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1487 3202 : call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1488 : }
1489 110458 : call = convert_from_reference (call);
1490 110458 : finish_return_stmt (call);
1491 :
1492 110458 : finish_compound_stmt (compound_stmt);
1493 110458 : finish_function_body (body);
1494 :
1495 110458 : fn = finish_function (/*inline_p=*/true);
1496 110458 : if (!generic_lambda_p)
1497 81975 : expand_or_defer_fn (fn);
1498 :
1499 : /* Generate the body of the conversion op. */
1500 :
1501 110458 : start_preparsed_function (convfn, NULL_TREE,
1502 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1503 110458 : body = begin_function_body ();
1504 110458 : compound_stmt = begin_compound_stmt (0);
1505 :
1506 : /* decl_needed_p needs to see that it's used. */
1507 110458 : TREE_USED (statfn) = 1;
1508 110458 : finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1509 :
1510 110458 : finish_compound_stmt (compound_stmt);
1511 110458 : finish_function_body (body);
1512 :
1513 110458 : fn = finish_function (/*inline_p=*/true);
1514 110458 : if (!generic_lambda_p)
1515 81975 : expand_or_defer_fn (fn);
1516 :
1517 110458 : if (nested)
1518 99614 : pop_function_context ();
1519 : else
1520 10844 : --function_depth;
1521 : }
1522 :
1523 : /* True if FN is the static function "_FUN" that gets returned from the lambda
1524 : conversion operator. */
1525 :
1526 : bool
1527 1302039 : lambda_static_thunk_p (tree fn)
1528 : {
1529 1302039 : return (fn && TREE_CODE (fn) == FUNCTION_DECL
1530 1302039 : && DECL_ARTIFICIAL (fn)
1531 127748 : && DECL_STATIC_FUNCTION_P (fn)
1532 1552063 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn)));
1533 : }
1534 :
1535 : bool
1536 206656658 : call_from_lambda_thunk_p (tree call)
1537 : {
1538 206656658 : return (CALL_FROM_THUNK_P (call)
1539 206656658 : && lambda_static_thunk_p (current_function_decl));
1540 : }
1541 :
1542 : /* Returns true iff VAL is a lambda-related declaration which should
1543 : be ignored by unqualified lookup. */
1544 :
1545 : bool
1546 3774628324 : is_lambda_ignored_entity (tree val)
1547 : {
1548 : /* Look past normal, non-VLA capture proxies. */
1549 3774628324 : if (is_normal_capture_proxy (val)
1550 3774628324 : && !variably_modified_type_p (TREE_TYPE (val), NULL_TREE))
1551 : return true;
1552 :
1553 : /* Always ignore lambda fields, their names are only for debugging. */
1554 3766247043 : if (TREE_CODE (val) == FIELD_DECL
1555 3766247043 : && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1556 : return true;
1557 :
1558 : /* None of the lookups that use qualify_lookup want the op() from the
1559 : lambda; they want the one from the enclosing class. */
1560 3766247016 : if (tree fns = maybe_get_fns (val))
1561 1259324435 : if (LAMBDA_FUNCTION_P (OVL_FIRST (fns)))
1562 612033 : return true;
1563 :
1564 : return false;
1565 : }
1566 :
1567 : /* Lambdas that appear in variable initializer or default argument
1568 : scope get that in their mangling, so we need to record it. Also,
1569 : multiple lambdas in the same scope may need a mangling
1570 : discriminator. In ABI <= 17, there is a single per-scope sequence
1571 : number. In ABI >= 18, there are per-scope per-signature sequence
1572 : numbers. */
1573 : struct GTY(()) lambda_sig_count
1574 : {
1575 : tree fn; // The lambda fn whose sig this is.
1576 : unsigned count;
1577 : };
1578 : struct GTY(()) lambda_discriminator
1579 : {
1580 : tree scope;
1581 : unsigned nesting; // Inside a function, VAR_DECLs get the function
1582 : // as scope. This counts that nesting.
1583 : unsigned count; // The per-scope counter.
1584 : vec<lambda_sig_count, va_gc> *discriminators; // Per-signature counters
1585 : };
1586 : // The current scope.
1587 : static GTY(()) lambda_discriminator lambda_scope;
1588 : // Stack of previous scopes.
1589 : static GTY(()) vec<lambda_discriminator, va_gc> *lambda_scope_stack;
1590 :
1591 : // Push DECL as lambda extra scope, also new discriminator counters.
1592 :
1593 : void
1594 342438729 : start_lambda_scope (tree decl)
1595 : {
1596 342438729 : gcc_checking_assert (decl);
1597 342438729 : if (current_function_decl && VAR_P (decl))
1598 : // If we're inside a function, we ignore variable scope. Don't push.
1599 51819364 : lambda_scope.nesting++;
1600 : else
1601 : {
1602 290619365 : vec_safe_push (lambda_scope_stack, lambda_scope);
1603 290619365 : lambda_scope.scope = decl;
1604 290619365 : lambda_scope.nesting = 0;
1605 290619365 : lambda_scope.count = 0;
1606 290619365 : lambda_scope.discriminators = nullptr;
1607 : }
1608 342438729 : }
1609 :
1610 : // Pop from the current lambda extra scope.
1611 :
1612 : void
1613 342435870 : finish_lambda_scope (void)
1614 : {
1615 342435870 : if (!lambda_scope.nesting--)
1616 : {
1617 290616506 : lambda_scope = lambda_scope_stack->last ();
1618 290616506 : lambda_scope_stack->pop ();
1619 : }
1620 342435870 : }
1621 :
1622 : // Record the current lambda scope into LAMBDA
1623 :
1624 : void
1625 1662640 : record_lambda_scope (tree lambda)
1626 : {
1627 1662640 : tree closure = LAMBDA_EXPR_CLOSURE (lambda);
1628 1662640 : gcc_checking_assert (closure);
1629 :
1630 : /* Before ABI v20, lambdas in static data member initializers did not
1631 : get a dedicated lambda scope. */
1632 1662640 : tree scope = lambda_scope.scope;
1633 1662640 : if (is_static_data_member_initialized_in_class (scope))
1634 : {
1635 158078 : if (!abi_version_at_least (20))
1636 158078 : scope = NULL_TREE;
1637 472635 : if (warn_abi && abi_version_crosses (20) && !processing_template_decl)
1638 : {
1639 18 : if (abi_version_at_least (20))
1640 18 : warning_at (location_of (closure), OPT_Wabi,
1641 : "the mangled name of %qT changed in "
1642 : "%<-fabi-version=20%> (GCC 15.1)", closure);
1643 : else
1644 0 : warning_at (location_of (closure), OPT_Wabi,
1645 : "the mangled name of %qT changes in "
1646 : "%<-fabi-version=20%> (GCC 15.1)", closure);
1647 : }
1648 : }
1649 :
1650 : /* An otherwise unattached class-scope lambda in a member template
1651 : should not have a mangling scope, as the mangling scope will not
1652 : correctly inherit on instantiation. */
1653 1662640 : tree ctx = TYPE_CONTEXT (closure);
1654 1662640 : if (scope
1655 1662640 : && ctx
1656 1661567 : && CLASS_TYPE_P (ctx)
1657 161679 : && ctx == TREE_TYPE (scope)
1658 1664838 : && current_template_depth > template_class_depth (ctx))
1659 : scope = NULL_TREE;
1660 :
1661 1662640 : LAMBDA_EXPR_EXTRA_SCOPE (lambda) = scope;
1662 1662640 : if (scope)
1663 1661470 : maybe_key_decl (scope, TYPE_NAME (closure));
1664 1662640 : }
1665 :
1666 : // Compare lambda template heads TMPL_A and TMPL_B, used for both
1667 : // templated lambdas, and template template parameters of said lambda.
1668 :
1669 : static bool
1670 33214 : compare_lambda_template_head (tree tmpl_a, tree tmpl_b)
1671 : {
1672 : // We only need one level of template parms
1673 33214 : tree inner_a = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_a));
1674 33214 : tree inner_b = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_b));
1675 :
1676 : // We only compare explicit template parms, ignoring trailing
1677 : // synthetic ones.
1678 33214 : int len_a = TREE_VEC_LENGTH (inner_a);
1679 33214 : int len_b = TREE_VEC_LENGTH (inner_b);
1680 :
1681 34133 : for (int ix = 0, len = MAX (len_a, len_b); ix != len; ix++)
1682 : {
1683 33261 : tree parm_a = NULL_TREE;
1684 33261 : if (ix < len_a)
1685 : {
1686 33249 : parm_a = TREE_VEC_ELT (inner_a, ix);
1687 33249 : if (parm_a == error_mark_node)
1688 : return false;
1689 33249 : parm_a = TREE_VALUE (parm_a);
1690 33249 : if (parm_a == error_mark_node)
1691 : return false;
1692 33249 : if (DECL_VIRTUAL_P (parm_a))
1693 16174 : parm_a = NULL_TREE;
1694 : }
1695 :
1696 33261 : tree parm_b = NULL_TREE;
1697 33261 : if (ix < len_b)
1698 : {
1699 33241 : parm_b = TREE_VEC_ELT (inner_b, ix);
1700 33241 : if (parm_b == error_mark_node)
1701 : return false;
1702 33241 : parm_b = TREE_VALUE (parm_b);
1703 33241 : if (parm_b == error_mark_node)
1704 : return false;
1705 33235 : if (DECL_VIRTUAL_P (parm_b))
1706 17361 : parm_b = NULL_TREE;
1707 : }
1708 :
1709 33255 : if (!parm_a && !parm_b)
1710 : // we're done
1711 : break;
1712 :
1713 17154 : if (!(parm_a && parm_b))
1714 : return false;
1715 :
1716 15826 : if (TREE_CODE (parm_a) != TREE_CODE (parm_b))
1717 : return false;
1718 :
1719 13919 : if (TREE_CODE (parm_a) == PARM_DECL)
1720 : {
1721 13021 : if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_a))
1722 13021 : != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_b)))
1723 : return false;
1724 :
1725 30 : if (!same_type_p (TREE_TYPE (parm_a), TREE_TYPE (parm_b)))
1726 : return false;
1727 : }
1728 : else
1729 : {
1730 898 : if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_a))
1731 898 : != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_b)))
1732 : return false;
1733 :
1734 898 : if (TREE_CODE (parm_a) != TEMPLATE_DECL)
1735 898 : gcc_checking_assert (TREE_CODE (parm_a) == TYPE_DECL);
1736 0 : else if (!compare_lambda_template_head (parm_a, parm_b))
1737 : return false;
1738 : }
1739 : }
1740 :
1741 : return true;
1742 : }
1743 :
1744 : // Compare lambda signatures FN_A and FN_B, they may be TEMPLATE_DECLs too.
1745 :
1746 : static bool
1747 261266 : compare_lambda_sig (tree fn_a, tree fn_b)
1748 : {
1749 261266 : if (TREE_CODE (fn_a) == TEMPLATE_DECL
1750 47374 : && TREE_CODE (fn_b) == TEMPLATE_DECL)
1751 : {
1752 33214 : if (!compare_lambda_template_head (fn_a, fn_b))
1753 : return false;
1754 16973 : fn_a = DECL_TEMPLATE_RESULT (fn_a);
1755 16973 : fn_b = DECL_TEMPLATE_RESULT (fn_b);
1756 : }
1757 228052 : else if (TREE_CODE (fn_a) == TEMPLATE_DECL
1758 213892 : || TREE_CODE (fn_b) == TEMPLATE_DECL)
1759 : return false;
1760 :
1761 230507 : if (fn_a == error_mark_node
1762 230505 : || fn_b == error_mark_node)
1763 : return false;
1764 :
1765 230503 : for (tree args_a = FUNCTION_FIRST_USER_PARMTYPE (fn_a),
1766 230503 : args_b = FUNCTION_FIRST_USER_PARMTYPE (fn_b);
1767 413540 : args_a || args_b;
1768 183037 : args_a = TREE_CHAIN (args_a), args_b = TREE_CHAIN (args_b))
1769 : {
1770 298168 : if (!args_a || !args_b)
1771 : return false;
1772 : // This check also deals with differing variadicness
1773 298163 : if (!same_type_p (TREE_VALUE (args_a), TREE_VALUE (args_b)))
1774 : return false;
1775 : }
1776 :
1777 : return true;
1778 : }
1779 :
1780 : // Record the per-scope discriminator of LAMBDA. If the extra scope
1781 : // is empty, we must use the empty scope counter, which might not be
1782 : // the live one.
1783 :
1784 : void
1785 1665857 : record_lambda_scope_discriminator (tree lambda)
1786 : {
1787 3330402 : auto *slot = (vec_safe_is_empty (lambda_scope_stack)
1788 1664545 : || LAMBDA_EXPR_EXTRA_SCOPE (lambda)
1789 3075 : ? &lambda_scope : lambda_scope_stack->begin ());
1790 1665857 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda) = slot->count++;
1791 1665857 : }
1792 :
1793 : // Record the per-scope per-signature discriminator of LAMBDA. If the
1794 : // extra scope is empty, we must use the empty scope counter, which
1795 : // might not be the live one.
1796 :
1797 : void
1798 1665845 : record_lambda_scope_sig_discriminator (tree lambda, tree fn)
1799 : {
1800 3330378 : auto *slot = (vec_safe_is_empty (lambda_scope_stack)
1801 1664533 : || LAMBDA_EXPR_EXTRA_SCOPE (lambda)
1802 3072 : ? &lambda_scope : lambda_scope_stack->begin ());
1803 1665845 : gcc_checking_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda) == slot->scope);
1804 :
1805 : // A linear search, we're not expecting this to be a big list, and
1806 : // this avoids needing a signature hash function.
1807 1665845 : lambda_sig_count *sig;
1808 1665845 : if (unsigned ix = vec_safe_length (slot->discriminators))
1809 385363 : for (sig = slot->discriminators->begin (); ix--; sig++)
1810 261266 : if (compare_lambda_sig (fn, sig->fn))
1811 115372 : goto found;
1812 1550473 : {
1813 1550473 : lambda_sig_count init = {fn, 0};
1814 1550473 : sig = vec_safe_push (slot->discriminators, init);
1815 : }
1816 1665845 : found:
1817 1665845 : LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda) = sig->count++;
1818 1665845 : }
1819 :
1820 : /* Push the proxies for any explicit captures in LAMBDA_EXPR.
1821 : If EARLY_P, we do not have the real operator() yet. */
1822 :
1823 : void
1824 3331667 : push_capture_proxies (tree lambda_expr, bool early_p)
1825 : {
1826 5632158 : for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
1827 2300491 : cap = TREE_CHAIN (cap))
1828 2300491 : build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap), early_p);
1829 3331667 : }
1830 :
1831 : tree
1832 1665810 : start_lambda_function (tree fco, tree lambda_expr)
1833 : {
1834 : /* Let the front end know that we are going to be defining this
1835 : function. */
1836 1665810 : start_preparsed_function (fco,
1837 : NULL_TREE,
1838 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1839 :
1840 1665810 : tree body = begin_function_body ();
1841 :
1842 : /* Push the proxies for any explicit captures. */
1843 1665810 : push_capture_proxies (lambda_expr);
1844 :
1845 1665810 : return body;
1846 : }
1847 :
1848 : /* Subroutine of prune_lambda_captures: CAP is a node in
1849 : LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
1850 : might optimize away the capture, or NULL_TREE if there is no such
1851 : variable. */
1852 :
1853 : static tree
1854 4562 : var_to_maybe_prune (tree cap)
1855 : {
1856 4562 : if (LAMBDA_CAPTURE_EXPLICIT_P (cap))
1857 : /* Don't prune explicit captures. */
1858 : return NULL_TREE;
1859 :
1860 4553 : tree mem = TREE_PURPOSE (cap);
1861 9106 : if (!DECL_P (mem) || !DECL_NORMAL_CAPTURE_P (mem))
1862 : /* Packs and init-captures aren't captures of constant vars. */
1863 : return NULL_TREE;
1864 :
1865 4553 : tree init = TREE_VALUE (cap);
1866 4553 : if (is_normal_capture_proxy (init))
1867 0 : init = DECL_CAPTURED_VARIABLE (init);
1868 4553 : if (decl_constant_var_p (init))
1869 1886 : return init;
1870 :
1871 : return NULL_TREE;
1872 : }
1873 :
1874 : /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
1875 : for constant variables are actually used in the lambda body.
1876 :
1877 : There will always be a DECL_EXPR for the capture proxy; remember it when we
1878 : see it, but replace it with any other use. */
1879 :
1880 : static tree
1881 74623 : mark_const_cap_r (tree *t, int *walk_subtrees, void *data)
1882 : {
1883 74623 : hash_map<tree,tree*> &const_vars = *(hash_map<tree,tree*>*)data;
1884 :
1885 74623 : tree var = NULL_TREE;
1886 74623 : if (TREE_CODE (*t) == DECL_EXPR)
1887 : {
1888 5662 : tree decl = DECL_EXPR_DECL (*t);
1889 5662 : if (is_constant_capture_proxy (decl))
1890 : {
1891 1898 : var = DECL_CAPTURED_VARIABLE (decl);
1892 1898 : *walk_subtrees = 0;
1893 : }
1894 : }
1895 68961 : else if (!location_wrapper_p (*t) /* is_capture_proxy dislikes them. */
1896 68961 : && is_constant_capture_proxy (*t))
1897 312 : var = DECL_CAPTURED_VARIABLE (*t);
1898 :
1899 74623 : if (var)
1900 : {
1901 2210 : tree *&slot = const_vars.get_or_insert (var);
1902 2210 : if (!slot || VAR_P (*t))
1903 2210 : slot = t;
1904 : }
1905 :
1906 74623 : return NULL_TREE;
1907 : }
1908 :
1909 : /* We're at the end of processing a lambda; go back and remove any captures of
1910 : constant variables for which we've folded away all uses. */
1911 :
1912 : static void
1913 1665810 : prune_lambda_captures (tree body)
1914 : {
1915 1665810 : tree lam = current_lambda_expr ();
1916 1665810 : if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam))
1917 : /* No uses were optimized away. */
1918 1664288 : return;
1919 1793 : if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
1920 : /* No default captures, and we don't prune explicit captures. */
1921 : return;
1922 : /* Don't bother pruning in a template, we'll prune at instantiation time. */
1923 1744 : if (dependent_type_p (TREE_TYPE (lam)))
1924 : return;
1925 :
1926 1522 : hash_map<tree,tree*> const_vars;
1927 :
1928 1522 : cp_walk_tree_without_duplicates (&body, mark_const_cap_r, &const_vars);
1929 :
1930 1522 : tree bind_expr = expr_single (DECL_SAVED_TREE (lambda_function (lam)));
1931 3056 : bool noexcept_p = (bind_expr
1932 1522 : && TREE_CODE (bind_expr) == MUST_NOT_THROW_EXPR);
1933 12 : if (noexcept_p)
1934 12 : bind_expr = expr_single (TREE_OPERAND (bind_expr, 0));
1935 :
1936 1522 : tree *fieldp = &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam));
1937 6084 : for (tree *capp = &LAMBDA_EXPR_CAPTURE_LIST (lam); *capp; )
1938 : {
1939 4562 : tree cap = *capp;
1940 4562 : if (tree var = var_to_maybe_prune (cap))
1941 : {
1942 1886 : tree **use = const_vars.get (var);
1943 1886 : if (TREE_CODE (**use) == DECL_EXPR)
1944 : {
1945 : /* All uses of this capture were folded away, leaving only the
1946 : proxy declaration. */
1947 :
1948 1574 : if (noexcept_p)
1949 : {
1950 : /* We didn't handle noexcept lambda captures correctly before
1951 : the fix for PR c++/119764. */
1952 33 : if (abi_version_crosses (21))
1953 3 : warning_at (location_of (lam), OPT_Wabi, "%qD is no longer"
1954 : " captured in noexcept lambda in ABI v21 "
1955 : "(GCC 16)", var);
1956 12 : if (!abi_version_at_least (21))
1957 0 : goto next;
1958 : }
1959 :
1960 : /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
1961 1574 : *capp = TREE_CHAIN (cap);
1962 :
1963 : /* And out of TYPE_FIELDS. */
1964 1574 : tree field = TREE_PURPOSE (cap);
1965 4091 : while (*fieldp != field)
1966 2517 : fieldp = &DECL_CHAIN (*fieldp);
1967 1574 : *fieldp = DECL_CHAIN (*fieldp);
1968 :
1969 : /* And out of the bindings for the function. */
1970 1574 : tree *blockp = &BLOCK_VARS (current_binding_level->blocks);
1971 3236 : while (*blockp != DECL_EXPR_DECL (**use))
1972 1662 : blockp = &DECL_CHAIN (*blockp);
1973 1574 : *blockp = DECL_CHAIN (*blockp);
1974 :
1975 : /* And maybe out of the vars declared in the containing
1976 : BIND_EXPR, if it's listed there. */
1977 1574 : tree *bindp = &BIND_EXPR_VARS (bind_expr);
1978 4148 : while (*bindp && *bindp != DECL_EXPR_DECL (**use))
1979 1000 : bindp = &DECL_CHAIN (*bindp);
1980 1574 : if (*bindp)
1981 523 : *bindp = DECL_CHAIN (*bindp);
1982 :
1983 : /* And remove the capture proxy declaration. */
1984 1574 : **use = void_node;
1985 1574 : continue;
1986 1574 : }
1987 : }
1988 :
1989 2988 : next:
1990 2988 : capp = &TREE_CHAIN (cap);
1991 : }
1992 1522 : }
1993 :
1994 : // Record the per-scope per-signature discriminator of LAMBDA. If the
1995 : // extra scope is empty, we must use the empty scope counter, which
1996 : // might not be the live one.
1997 :
1998 : void
1999 1665810 : finish_lambda_function (tree body)
2000 : {
2001 1665810 : finish_function_body (body);
2002 :
2003 1665810 : prune_lambda_captures (cur_stmt_list);
2004 :
2005 : /* Finish the function and generate code for it if necessary. */
2006 1665810 : tree fn = finish_function (/*inline_p=*/true);
2007 :
2008 : /* Only expand if the call op is not a template. */
2009 1665810 : if (!DECL_TEMPLATE_INFO (fn))
2010 355169 : expand_or_defer_fn (fn);
2011 1665810 : }
2012 :
2013 : #include "gt-cp-lambda.h"
|