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 1667905 : build_lambda_expr (void)
42 : {
43 1667905 : tree lambda = make_node (LAMBDA_EXPR);
44 1667905 : LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
45 1667905 : LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
46 1667905 : LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
47 1667905 : LAMBDA_EXPR_REGEN_INFO (lambda) = NULL_TREE;
48 1667905 : LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
49 1667905 : return lambda;
50 : }
51 :
52 : /* Create the closure object for a LAMBDA_EXPR. */
53 :
54 : tree
55 1669585 : build_lambda_object (tree lambda_expr)
56 : {
57 : /* Build aggregate constructor call.
58 : - cp_parser_braced_list
59 : - cp_parser_functional_cast */
60 1669585 : vec<constructor_elt, va_gc> *elts = NULL;
61 1669585 : tree node, expr, type;
62 :
63 1669585 : 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 1669570 : 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 405886 : location_t loc = LAMBDA_EXPR_LOCATION (lambda_expr);
74 405886 : iloc_sentinel il (loc);
75 :
76 405886 : for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
77 1017075 : node;
78 611189 : node = TREE_CHAIN (node))
79 : {
80 611189 : tree field = TREE_PURPOSE (node);
81 611189 : tree val = TREE_VALUE (node);
82 :
83 611189 : if (field == error_mark_node)
84 : {
85 0 : expr = error_mark_node;
86 0 : goto out;
87 : }
88 :
89 611189 : 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 611189 : if (DECL_P (val))
94 294748 : 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 611189 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
99 19 : val = build_array_copy (val);
100 611170 : else if (DECL_NORMAL_CAPTURE_P (field)
101 609888 : && !DECL_VLA_CAPTURE_P (field)
102 1221016 : && !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 282832 : val = force_rvalue (val, tf_warning_or_error);
112 282832 : if (TREE_CODE (val) == TARGET_EXPR)
113 1169 : TARGET_EXPR_DIRECT_INIT_P (val) = true;
114 : }
115 :
116 611189 : CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
117 : }
118 :
119 405886 : expr = build_constructor (init_list_type_node, elts);
120 405886 : 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 405886 : type = LAMBDA_EXPR_CLOSURE (lambda_expr);
125 405886 : CLASSTYPE_NON_AGGREGATE (type) = 0;
126 405886 : expr = finish_compound_literal (type, expr, tf_warning_or_error);
127 405886 : protected_set_expr_location (expr, loc);
128 405886 : CLASSTYPE_NON_AGGREGATE (type) = 1;
129 :
130 405886 : out:
131 405886 : return expr;
132 405886 : }
133 :
134 : /* Return an initialized RECORD_TYPE for LAMBDA.
135 : LAMBDA must have its explicit captures already. */
136 :
137 : tree
138 1667896 : begin_lambda_type (tree lambda)
139 : {
140 : /* Lambda names are nearly but not quite anonymous. */
141 1667896 : tree name = make_anon_name ();
142 1667896 : IDENTIFIER_LAMBDA_P (name) = true;
143 :
144 : /* Create the new RECORD_TYPE for this lambda. */
145 1667896 : tree type = xref_tag (/*tag_code=*/record_type, name);
146 1667896 : 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 1667896 : CLASSTYPE_DECLARED_CLASS (type) = false;
151 :
152 : /* Cross-reference the expression and the type. */
153 1667896 : LAMBDA_EXPR_CLOSURE (lambda) = type;
154 1667896 : 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 1667896 : if (cxx_dialect >= cxx17)
159 1663981 : CLASSTYPE_LITERAL_P (type) = true;
160 :
161 : /* Clear base types. */
162 1667896 : xref_basetypes (type, /*bases=*/NULL_TREE);
163 :
164 : /* Start the class. */
165 1667896 : type = begin_class_definition (type);
166 :
167 1667896 : 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 14887018 : lambda_function (tree lambda)
175 : {
176 14887018 : tree type;
177 14887018 : if (TREE_CODE (lambda) == LAMBDA_EXPR)
178 6399115 : type = LAMBDA_EXPR_CLOSURE (lambda);
179 : else
180 : type = lambda;
181 29774036 : gcc_assert (LAMBDA_TYPE_P (type));
182 : /* Don't let debug_tree cause instantiation. */
183 14887018 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
184 14887018 : && !COMPLETE_OR_OPEN_TYPE_P (type))
185 : return NULL_TREE;
186 14887018 : lambda = get_class_binding_direct (type, call_op_identifier);
187 14887018 : if (lambda)
188 14886971 : 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 2034054 : type_deducible_expression_p (tree expr)
197 : {
198 2034054 : if (!type_dependent_expression_p (expr))
199 : return true;
200 0 : if (BRACE_ENCLOSED_INITIALIZER_P (expr)
201 1276706 : || TREE_CODE (expr) == EXPR_PACK_EXPANSION)
202 : return false;
203 1276706 : tree t = non_reference (TREE_TYPE (expr));
204 1276706 : return (t && TREE_CODE (t) != TYPE_PACK_EXPANSION
205 533266 : && !WILDCARD_TYPE_P (t) && !LAMBDA_TYPE_P (t)
206 617800 : && !array_of_unknown_bound_p (t)
207 1894467 : && !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 2048552 : lambda_capture_field_type (tree expr, bool explicit_init_p,
217 : bool by_reference_p)
218 : {
219 2048552 : tree type;
220 2048552 : bool is_this = is_this_parameter (tree_strip_nop_conversions (expr));
221 :
222 2048552 : if (explicit_init_p)
223 : {
224 14498 : 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 14315 : tree auto_node = make_auto ();
234 14315 : type = auto_node;
235 14315 : 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 14315 : type = do_auto_deduction (type, expr, auto_node);
240 : }
241 : }
242 2034054 : else if (!type_deducible_expression_p (expr))
243 : {
244 658948 : type = cxx_make_type (DECLTYPE_TYPE);
245 658948 : DECLTYPE_TYPE_EXPR (type) = expr;
246 658948 : DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
247 658948 : DECLTYPE_FOR_REF_CAPTURE (type) = by_reference_p;
248 658948 : SET_TYPE_STRUCTURAL_EQUALITY (type);
249 : }
250 : else
251 : {
252 1375106 : STRIP_ANY_LOCATION_WRAPPER (expr);
253 :
254 1375106 : 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 1375106 : type = non_reference (unlowered_expr_type (expr));
265 :
266 1375106 : if ((by_reference_p && !is_this) || TREE_CODE (type) == FUNCTION_TYPE)
267 724018 : type = build_reference_type (type);
268 : }
269 :
270 2048552 : 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 5945484123 : is_capture_proxy (tree decl)
278 : {
279 : /* Location wrappers should be stripped or otherwise handled by the
280 : caller before using this predicate. */
281 5945484123 : gcc_checking_assert (!location_wrapper_p (decl));
282 :
283 5945484123 : return (VAR_P (decl)
284 1654512453 : && DECL_HAS_VALUE_EXPR_P (decl)
285 44401897 : && !DECL_ANON_UNION_VAR_P (decl)
286 44399107 : && !DECL_DECOMPOSITION_P (decl)
287 40196665 : && !DECL_FNAME_P (decl)
288 39814054 : && !(DECL_ARTIFICIAL (decl)
289 39812272 : && DECL_LANG_SPECIFIC (decl)
290 38463125 : && DECL_OMP_PRIVATIZED_MEMBER (decl))
291 6025073516 : && 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 5628297358 : is_normal_capture_proxy (tree decl)
299 : {
300 5628297358 : if (!is_capture_proxy (decl))
301 : /* It's not a capture proxy. */
302 : return false;
303 :
304 30114768 : return (DECL_LANG_SPECIFIC (decl)
305 30114768 : && 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 5552506 : strip_normal_capture_proxy (tree decl)
313 : {
314 5605514 : while (is_normal_capture_proxy (decl))
315 53008 : decl = DECL_CAPTURED_VARIABLE (decl);
316 5552506 : 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 2392240 : insert_capture_proxy (tree var)
335 : {
336 2392240 : if (is_normal_capture_proxy (var))
337 : {
338 2376801 : tree cap = DECL_CAPTURED_VARIABLE (var);
339 2376801 : if (CHECKING_P)
340 : {
341 2376801 : gcc_assert (!is_normal_capture_proxy (cap));
342 2376801 : tree old = retrieve_local_specialization (cap);
343 2376801 : if (old)
344 22013 : gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var));
345 : }
346 2376801 : 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 2392240 : pushdecl_outermost_localscope (var);
352 :
353 : /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
354 2392240 : 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 2392240 : unsigned index = 1 + use_eh_spec_block (current_function_decl);
359 2392240 : tree stmt_list = (*stmt_list_stack)[index];
360 2392240 : gcc_assert (stmt_list);
361 2392240 : append_to_statement_list_force (var, &stmt_list);
362 2392240 : }
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 1667896 : insert_pending_capture_proxies (void)
370 : {
371 1667896 : tree lam;
372 1667896 : vec<tree, va_gc> *proxies;
373 1667896 : unsigned i;
374 :
375 1789171 : if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
376 : return;
377 :
378 29155 : lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
379 29155 : proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
380 36549 : for (i = 0; i < vec_safe_length (proxies); ++i)
381 : {
382 7394 : tree var = (*proxies)[i];
383 7394 : insert_capture_proxy (var);
384 : }
385 29155 : release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
386 29155 : 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 2634068 : lambda_proxy_type (tree ref)
396 : {
397 2634068 : tree type;
398 2634068 : if (ref == error_mark_node)
399 : return error_mark_node;
400 2634050 : if (REFERENCE_REF_P (ref))
401 0 : ref = TREE_OPERAND (ref, 0);
402 2634050 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
403 2634050 : type = TREE_TYPE (ref);
404 2634050 : if (!type || WILDCARD_TYPE_P (non_reference (type)))
405 : {
406 838519 : type = cxx_make_type (DECLTYPE_TYPE);
407 838519 : DECLTYPE_TYPE_EXPR (type) = ref;
408 838519 : DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
409 838519 : SET_TYPE_STRUCTURAL_EQUALITY (type);
410 : }
411 2634050 : if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
412 3016 : 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 3382036 : build_capture_proxy (tree member, tree init, bool early_p)
423 : {
424 3382036 : tree var, object, fn, closure, name, lam, type;
425 :
426 3382036 : if (PACK_EXPANSION_P (member))
427 2998 : member = PACK_EXPANSION_PATTERN (member);
428 :
429 3382036 : closure = DECL_CONTEXT (member);
430 3382036 : fn = lambda_function (closure);
431 3382036 : lam = CLASSTYPE_LAMBDA_EXPR (closure);
432 :
433 3382036 : object = DECL_ARGUMENTS (fn);
434 : /* The proxy variable forwards to the capture field. */
435 3382036 : if (INDIRECT_TYPE_P (TREE_TYPE (object)))
436 3381820 : object = build_fold_indirect_ref (object);
437 3382036 : object = finish_non_static_data_member (member, object, NULL_TREE);
438 3382036 : if (REFERENCE_REF_P (object))
439 1306561 : object = TREE_OPERAND (object, 0);
440 :
441 : /* Remove the __ inserted by add_capture. */
442 3382036 : if (IDENTIFIER_POINTER (DECL_NAME (member))[2] == '_'
443 3382036 : && IDENTIFIER_POINTER (DECL_NAME (member))[3] == '.')
444 36 : name = get_identifier ("_");
445 : else
446 3382000 : name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
447 :
448 3382036 : 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 748378 : type = cp_build_qualified_type (TREE_TYPE (member), TYPE_QUAL_CONST);
454 : else
455 : {
456 2633658 : type = lambda_proxy_type (object);
457 2633658 : 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 3382036 : 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 3382036 : complete_type (type);
479 :
480 3382036 : var = build_decl (input_location, VAR_DECL, name, type);
481 3382036 : SET_DECL_VALUE_EXPR (var, object);
482 3382036 : DECL_HAS_VALUE_EXPR_P (var) = 1;
483 3382036 : DECL_ARTIFICIAL (var) = 1;
484 3382036 : TREE_USED (var) = 1;
485 3382036 : DECL_CONTEXT (var) = fn;
486 :
487 3382036 : if (DECL_NORMAL_CAPTURE_P (member))
488 : {
489 3351379 : 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 3351307 : if (PACK_EXPANSION_P (init))
498 2656 : init = PACK_EXPANSION_PATTERN (init);
499 : }
500 :
501 3351379 : init = strip_contract_const_wrapper (init);
502 :
503 3351379 : if (INDIRECT_REF_P (init))
504 716345 : init = TREE_OPERAND (init, 0);
505 3351379 : STRIP_NOPS (init);
506 :
507 3351379 : gcc_assert (VAR_P (init) || TREE_CODE (init) == PARM_DECL);
508 3351379 : init = strip_normal_capture_proxy (init);
509 3351379 : retrofit_lang_decl (var);
510 3351379 : DECL_CAPTURED_VARIABLE (var) = init;
511 : }
512 :
513 3382036 : if (name == this_identifier)
514 : {
515 748656 : if (early_p)
516 : return var;
517 402273 : gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
518 402273 : LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
519 : }
520 :
521 3035653 : if (early_p)
522 : {
523 805342 : gcc_checking_assert (current_binding_level->kind == sk_lambda);
524 : /* insert_capture_proxy below wouldn't push into the lambda scope. */
525 805342 : pushdecl (var);
526 : }
527 2230311 : else if (fn == current_function_decl)
528 2222917 : insert_capture_proxy (var);
529 : else
530 7394 : 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 1896449 : add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
567 : bool explicit_init_p, unsigned *name_independent_cnt)
568 : {
569 1896449 : char *buf;
570 1896449 : tree type, member, name;
571 1896449 : bool vla = false;
572 1896449 : bool variadic = false;
573 1896449 : tree initializer = orig_init;
574 :
575 1896449 : if (PACK_EXPANSION_P (initializer))
576 : {
577 1499 : initializer = PACK_EXPANSION_PATTERN (initializer);
578 : variadic = true;
579 : }
580 :
581 1896449 : if (TREE_CODE (initializer) == TREE_LIST
582 : /* A pack expansion might end up with multiple elements. */
583 1896449 : && !PACK_EXPANSION_P (TREE_VALUE (initializer)))
584 15 : initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
585 : tf_warning_or_error);
586 1896449 : type = TREE_TYPE (initializer);
587 1896449 : if (type == error_mark_node)
588 : return error_mark_node;
589 :
590 1896425 : 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 1896380 : else if (!dependent_type_p (type)
608 1896380 : && 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 1896362 : type = lambda_capture_field_type (initializer, explicit_init_p,
622 : by_reference_p);
623 1896362 : if (type == error_mark_node)
624 : return error_mark_node;
625 :
626 1896359 : 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 1896359 : if (dependent_type_p (type))
634 : ;
635 608314 : else if (id != this_identifier && by_reference_p)
636 : {
637 262434 : 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 345880 : type = complete_type (type);
647 345880 : 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 345874 : else if (!verify_type_context (input_location,
655 : TCTX_CAPTURE_BY_COPY, type))
656 0 : return error_mark_node;
657 : }
658 :
659 1896350 : if (cxx_dialect < cxx20 && !explicit_init_p)
660 : {
661 8164 : auto_diagnostic_group d;
662 8164 : tree stripped_init = tree_strip_any_location_wrapper (initializer);
663 4240 : if (DECL_DECOMPOSITION_P (stripped_init)
664 12404 : && 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 8164 : }
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 1896395 : 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 1896347 : buf = XALLOCAVEC (char, IDENTIFIER_LENGTH (id) + 3);
691 1896347 : buf[1] = buf[0] = '_';
692 1896347 : memcpy (buf + 2, IDENTIFIER_POINTER (id),
693 1896347 : IDENTIFIER_LENGTH (id) + 1);
694 1896347 : name = get_identifier (buf);
695 : }
696 :
697 1896395 : if (variadic)
698 : {
699 1499 : type = make_pack_expansion (type);
700 1499 : 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 1896395 : member = build_decl (input_location, FIELD_DECL, name, type);
714 1896395 : DECL_VLA_CAPTURE_P (member) = vla;
715 :
716 1896395 : 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 1881900 : DECL_NORMAL_CAPTURE_P (member) = true;
723 :
724 1896395 : if (id == this_identifier)
725 367884 : LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
726 :
727 : /* Add it to the appropriate closure class if we've started it. */
728 1896395 : if (current_class_type
729 1896395 : && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
730 : {
731 1078593 : 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 1078589 : finish_member_declaration (member);
746 : }
747 :
748 1896391 : tree listmem = member;
749 1896391 : if (variadic)
750 : {
751 1499 : listmem = make_pack_expansion (member);
752 1499 : initializer = orig_init;
753 : }
754 1896391 : LAMBDA_EXPR_CAPTURE_LIST (lambda)
755 1896391 : = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
756 :
757 1896391 : if (LAMBDA_EXPR_CLOSURE (lambda))
758 1078589 : 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 817802 : LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda)) = true;
762 817802 : 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 2819621 : register_capture_members (tree captures)
770 : {
771 2819621 : if (captures == NULL_TREE)
772 : return;
773 :
774 1151725 : register_capture_members (TREE_CHAIN (captures));
775 :
776 1151725 : tree field = TREE_PURPOSE (captures);
777 1151725 : if (PACK_EXPANSION_P (field))
778 1499 : field = PACK_EXPANSION_PATTERN (field);
779 :
780 1151725 : 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 1071217 : add_default_capture (tree lambda_stack, tree id, tree initializer)
789 : {
790 1071217 : bool this_capture_p = (id == this_identifier);
791 1071217 : tree var = NULL_TREE;
792 1071217 : tree saved_class_type = current_class_type;
793 :
794 1071217 : for (tree node = lambda_stack;
795 2149834 : node;
796 1078617 : node = TREE_CHAIN (node))
797 : {
798 1078617 : tree lambda = TREE_VALUE (node);
799 :
800 1078617 : current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
801 1078617 : if (DECL_PACK_P (initializer))
802 0 : initializer = make_pack_expansion (initializer);
803 2157234 : var = add_capture (lambda,
804 : id,
805 : initializer,
806 : /*by_reference_p=*/
807 : (this_capture_p
808 1022727 : || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
809 1022727 : == CPLD_REFERENCE)),
810 : /*explicit_init_p=*/false, NULL);
811 1078617 : initializer = convert_from_reference (var);
812 :
813 : /* Warn about deprecated implicit capture of this via [=]. */
814 1078617 : if (cxx_dialect >= cxx20
815 1072385 : && this_capture_p
816 1134250 : && 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 1071217 : current_class_type = saved_class_type;
828 :
829 1071217 : 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 1144930 : lambda_expr_this_capture (tree lambda, int add_capture_p)
839 : {
840 1144930 : tree result;
841 :
842 1144930 : tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
843 1144930 : if (this_capture)
844 1026419 : 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 : A typeid operand's own probe is capture-transparent
852 : ([expr.prim.lambda.capture]/7). */
853 1144930 : if (cp_unevaluated_operand > cp_unevaluated_typeid_cutoff)
854 136 : add_capture_p = false;
855 :
856 : /* If we captured 'this' but don't have a capture proxy yet, look up the
857 : captured 'this' again. */
858 1144930 : if (this_capture && TREE_CODE (this_capture) == FIELD_DECL)
859 : {
860 0 : gcc_assert (!add_capture_p);
861 : this_capture = NULL_TREE;
862 : }
863 :
864 : /* Try to default capture 'this' if we can. */
865 : if (!this_capture)
866 : {
867 : tree lambda_stack = NULL_TREE;
868 121535 : tree init = NULL_TREE;
869 : bool saw_complete = false;
870 :
871 : /* If we are in a lambda function, we can move out until we hit:
872 : 1. a non-lambda function or NSDMI,
873 : 2. a lambda function capturing 'this', or
874 : 3. a non-default capturing lambda function. */
875 : for (tree tlambda = lambda; ;)
876 : {
877 121535 : if (add_capture_p
878 177444 : && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
879 : /* tlambda won't let us capture 'this'. */
880 : break;
881 :
882 121519 : if (add_capture_p)
883 55893 : lambda_stack = tree_cons (NULL_TREE,
884 : tlambda,
885 : lambda_stack);
886 :
887 121519 : tree closure = LAMBDA_EXPR_CLOSURE (tlambda);
888 121519 : if (COMPLETE_TYPE_P (closure))
889 : /* We're instantiating a generic lambda op(), the containing
890 : scope may be gone. */
891 8577 : saw_complete = true;
892 :
893 121519 : tree containing_function
894 121519 : = decl_function_context (TYPE_NAME (closure));
895 :
896 121519 : tree ex = LAMBDA_EXPR_EXTRA_SCOPE (tlambda);
897 121519 : if (ex && TREE_CODE (ex) == FIELD_DECL)
898 : {
899 : /* Lambda in an NSDMI. We don't have a function to look up
900 : 'this' in, but we can find (or rebuild) the fake one from
901 : inject_this_parameter. */
902 63 : if (!containing_function && !saw_complete)
903 : /* If we're parsing a lambda in a non-local class,
904 : we can find the fake 'this' in scope_chain. */
905 45 : init = scope_chain->x_current_class_ptr;
906 : else
907 : /* Otherwise it's either gone or buried in
908 : function_context_stack, so make another. */
909 18 : init = build_this_parm (NULL_TREE, DECL_CONTEXT (ex),
910 : TYPE_UNQUALIFIED);
911 63 : gcc_checking_assert
912 : (init && (TREE_TYPE (TREE_TYPE (init))
913 : == current_nonlambda_class_type ()));
914 : break;
915 : }
916 :
917 121456 : if (containing_function == NULL_TREE)
918 : /* We ran out of scopes; there's no 'this' to capture. */
919 : break;
920 :
921 120933 : if (!LAMBDA_FUNCTION_P (containing_function))
922 : {
923 : /* We found a non-lambda function.
924 : There is no this pointer in xobj member functions. */
925 114675 : if (DECL_IOBJ_MEMBER_FUNCTION_P (containing_function))
926 : /* First parameter is 'this'. */
927 103521 : init = DECL_ARGUMENTS (containing_function);
928 : break;
929 : }
930 :
931 3048 : tlambda
932 3048 : = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
933 :
934 3048 : if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
935 : {
936 : /* An outer lambda has already captured 'this'. */
937 : init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
938 : break;
939 : }
940 : }
941 :
942 107357 : if (init)
943 : {
944 103608 : if (add_capture_p)
945 55861 : this_capture = add_default_capture (lambda_stack,
946 : /*id=*/this_identifier,
947 : init);
948 : else
949 : this_capture = init;
950 : }
951 : }
952 :
953 1144930 : if (cp_unevaluated_operand)
954 : result = this_capture;
955 1144782 : else if (!this_capture)
956 : {
957 14885 : if (add_capture_p == 1)
958 : {
959 16 : error ("%<this%> was not captured for this lambda function");
960 16 : result = error_mark_node;
961 : }
962 : else
963 : result = NULL_TREE;
964 : }
965 : else
966 : {
967 : /* To make sure that current_class_ref is for the lambda. */
968 1129897 : gcc_assert (!current_class_ref
969 : || (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
970 : == LAMBDA_EXPR_CLOSURE (lambda)));
971 :
972 1129897 : result = this_capture;
973 :
974 : /* If 'this' is captured, each use of 'this' is transformed into an
975 : access to the corresponding unnamed data member of the closure
976 : type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
977 : ensures that the transformed expression is an rvalue. ] */
978 1129897 : result = rvalue (result);
979 : }
980 :
981 1130061 : gcc_checking_assert (!result || result == error_mark_node
982 : || TYPE_PTR_P (TREE_TYPE (result)));
983 :
984 1144930 : return result;
985 : }
986 :
987 : /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
988 :
989 : tree
990 84514083 : current_lambda_expr (void)
991 : {
992 84514083 : tree type = current_class_type;
993 151929228 : while (type && !LAMBDA_TYPE_P (type))
994 30474815 : type = decl_type_context (TYPE_NAME (type));
995 84514083 : if (type)
996 6630246 : return CLASSTYPE_LAMBDA_EXPR (type);
997 : else
998 : return NULL_TREE;
999 : }
1000 :
1001 : /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
1002 : object. NULL otherwise.. */
1003 :
1004 : static tree
1005 254503506 : resolvable_dummy_lambda (tree object)
1006 : {
1007 254503506 : if (!is_dummy_object (object))
1008 : return NULL_TREE;
1009 :
1010 12575118 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
1011 12575118 : gcc_assert (!TYPE_PTR_P (type));
1012 :
1013 12575118 : tree fn;
1014 12575118 : if (type != current_class_type
1015 8871599 : && current_class_type
1016 10291541 : && LAMBDA_TYPE_P (current_class_type)
1017 482073 : && (fn = lambda_function (current_class_type))
1018 : /* Even dummy lambdas have an operator() since P2036, but the
1019 : dummy operator() doesn't have this set. */
1020 482062 : && DECL_LAMBDA_FUNCTION_P (fn)
1021 13057177 : && DERIVED_FROM_P (type, nonlambda_method_basetype()))
1022 463406 : return CLASSTYPE_LAMBDA_EXPR (current_class_type);
1023 :
1024 : return NULL_TREE;
1025 : }
1026 :
1027 : /* We don't want to capture 'this' until we know we need it, i.e. after
1028 : overload resolution has chosen a non-static member function. At that
1029 : point we call this function to turn a dummy object into a use of the
1030 : 'this' capture. */
1031 :
1032 : tree
1033 152852814 : maybe_resolve_dummy (tree object, bool add_capture_p)
1034 : {
1035 152852814 : if (tree lam = resolvable_dummy_lambda (object))
1036 415029 : if (tree cap = lambda_expr_this_capture (lam, add_capture_p))
1037 415029 : if (cap != error_mark_node)
1038 415023 : object = build_fold_indirect_ref (cap);
1039 :
1040 152852814 : return object;
1041 : }
1042 :
1043 : /* When parsing a generic lambda containing an argument-dependent
1044 : member function call we defer overload resolution to instantiation
1045 : time. But we have to know now whether to capture this or not.
1046 : Do that if FNS contains any non-static fns as per
1047 : [expr.prim.lambda.capture]/7.1. */
1048 :
1049 : void
1050 101650692 : maybe_generic_this_capture (tree object, tree fns)
1051 : {
1052 101650692 : if (tree lam = resolvable_dummy_lambda (object))
1053 48377 : if (!LAMBDA_EXPR_THIS_CAPTURE (lam))
1054 : {
1055 : /* We've not yet captured, so look at the function set of
1056 : interest. */
1057 4417 : if (BASELINK_P (fns))
1058 3334 : fns = BASELINK_FUNCTIONS (fns);
1059 4417 : bool id_expr = TREE_CODE (fns) == TEMPLATE_ID_EXPR;
1060 4417 : if (id_expr)
1061 1089 : fns = TREE_OPERAND (fns, 0);
1062 :
1063 4426 : for (lkp_iterator iter (fns); iter; ++iter)
1064 3337 : if (((!id_expr && TREE_CODE (*iter) != USING_DECL)
1065 1092 : || TREE_CODE (*iter) == TEMPLATE_DECL)
1066 8849 : && DECL_OBJECT_MEMBER_FUNCTION_P (*iter))
1067 : {
1068 : /* Found a non-static member. Capture this. */
1069 4417 : lambda_expr_this_capture (lam, /*maybe*/-1);
1070 4417 : break;
1071 : }
1072 : }
1073 101650692 : }
1074 :
1075 : /* Returns the innermost non-lambda function. If ONLY_SKIP_CONSTEVAL_BLOCK_P,
1076 : we only skip lambda functions that represent consteval blocks. */
1077 :
1078 : tree
1079 299187 : current_nonlambda_function (bool only_skip_consteval_block_p/*=false*/)
1080 : {
1081 299187 : tree fn = current_function_decl;
1082 299187 : tree lam;
1083 606439 : while (fn && LAMBDA_FUNCTION_P (fn)
1084 300154 : && (!only_skip_consteval_block_p
1085 : /* Only keep going if FN represents a consteval block. */
1086 943 : || ((lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fn)))
1087 943 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))))
1088 21 : fn = decl_function_context (fn);
1089 299187 : return fn;
1090 : }
1091 :
1092 : /* Returns the method basetype of the innermost non-lambda function, including
1093 : a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
1094 :
1095 : tree
1096 482469 : nonlambda_method_basetype (void)
1097 : {
1098 482469 : tree type = current_class_type;
1099 964633 : if (!type || !LAMBDA_TYPE_P (type))
1100 398 : return current_class_ref ? type : NULL_TREE;
1101 :
1102 488223 : while (true)
1103 : {
1104 485147 : tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1105 485147 : tree ex = LAMBDA_EXPR_EXTRA_SCOPE (lam);
1106 485147 : if (ex && TREE_CODE (ex) == FIELD_DECL)
1107 : /* Lambda in an NSDMI. */
1108 30 : return DECL_CONTEXT (ex);
1109 :
1110 485117 : tree fn = TYPE_CONTEXT (type);
1111 485117 : if (!fn || TREE_CODE (fn) != FUNCTION_DECL
1112 966471 : || !DECL_OBJECT_MEMBER_FUNCTION_P (fn))
1113 : /* No enclosing non-lambda method. */
1114 : return NULL_TREE;
1115 472995 : if (!LAMBDA_FUNCTION_P (fn))
1116 : /* Found an enclosing non-lambda method. */
1117 466756 : return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1118 3076 : type = DECL_CONTEXT (fn);
1119 3076 : }
1120 : }
1121 :
1122 : /* Like current_scope, but looking through lambdas. If ONLY_SKIP_CLOSURES_P,
1123 : only look through closure types. */
1124 :
1125 : tree
1126 48227518 : current_nonlambda_scope (bool only_skip_closures_p/*=false*/)
1127 : {
1128 48227518 : tree scope = current_scope ();
1129 48343810 : for (;;)
1130 : {
1131 48459904 : if (!only_skip_closures_p
1132 47857577 : && TREE_CODE (scope) == FUNCTION_DECL
1133 90196253 : && LAMBDA_FUNCTION_P (scope))
1134 : {
1135 116094 : scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope));
1136 116094 : continue;
1137 : }
1138 55006457 : else if (LAMBDA_TYPE_P (scope))
1139 : {
1140 198 : scope = CP_TYPE_CONTEXT (scope);
1141 198 : continue;
1142 : }
1143 48227518 : break;
1144 : }
1145 48227518 : return scope;
1146 : }
1147 :
1148 : /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
1149 : indicated FN and NARGS, but do not initialize the return type or any of the
1150 : argument slots. */
1151 :
1152 : static tree
1153 56868 : prepare_op_call (tree fn, int nargs)
1154 : {
1155 56868 : tree t;
1156 :
1157 56868 : t = build_vl_exp (CALL_EXPR, nargs + 3);
1158 56868 : CALL_EXPR_FN (t) = fn;
1159 56868 : CALL_EXPR_STATIC_CHAIN (t) = NULL;
1160 :
1161 56868 : return t;
1162 : }
1163 :
1164 : /* Return true iff CALLOP is the op() for a generic lambda. */
1165 :
1166 : bool
1167 645480566 : generic_lambda_fn_p (tree callop)
1168 : {
1169 698757445 : return (callop && LAMBDA_FUNCTION_P (callop)
1170 7289912 : && DECL_TEMPLATE_INFO (callop)
1171 652687546 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
1172 : }
1173 :
1174 : /* If the closure TYPE has a static op(), also add a conversion to function
1175 : pointer. */
1176 :
1177 : void
1178 1667849 : maybe_add_lambda_conv_op (tree type)
1179 : {
1180 1667849 : bool nested = (cfun != NULL);
1181 1667849 : bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
1182 1667849 : tree callop = lambda_function (type);
1183 1667849 : tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1184 :
1185 1667849 : if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE
1186 522620 : || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE
1187 : /* CWG2561 ...and no explicit object parameter. */
1188 2178109 : || DECL_XOBJ_MEMBER_FUNCTION_P (callop))
1189 1557246 : return;
1190 :
1191 510058 : if (processing_template_decl)
1192 : return;
1193 :
1194 111498 : bool const generic_lambda_p = generic_lambda_fn_p (callop);
1195 :
1196 111498 : if (!generic_lambda_p && undeduced_auto_decl (callop))
1197 : {
1198 : /* If the op() wasn't deduced due to errors, give up. */
1199 46 : gcc_assert (errorcount || sorrycount);
1200 : return;
1201 : }
1202 :
1203 : /* Non-generic non-capturing lambdas only have a conversion function to
1204 : pointer to function when the trailing requires-clause's constraints are
1205 : satisfied. */
1206 111452 : if (!generic_lambda_p && !constraints_satisfied_p (callop))
1207 : return;
1208 :
1209 : /* Non-template conversion operators are defined directly with build_call_a
1210 : and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1211 : deferred and the CALL is built in-place. In the case of a deduced return
1212 : call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1213 : the return type is also built in-place. The arguments of DECLTYPE_CALL in
1214 : the return expression may differ in flags from those in the body CALL. In
1215 : particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1216 : the body CALL, but not in DECLTYPE_CALL. */
1217 :
1218 111434 : vec<tree, va_gc> *direct_argvec = 0;
1219 111434 : tree decltype_call = 0, call = 0;
1220 111434 : tree optype = TREE_TYPE (callop);
1221 111434 : tree fn_result = TREE_TYPE (optype);
1222 :
1223 111434 : tree thisarg = NULL_TREE;
1224 111434 : if (TREE_CODE (optype) == METHOD_TYPE)
1225 110845 : thisarg = build_int_cst (TREE_TYPE (DECL_ARGUMENTS (callop)), 0);
1226 111434 : if (generic_lambda_p)
1227 : {
1228 28566 : ++processing_template_decl;
1229 :
1230 : /* Prepare the dependent member call for the static member function
1231 : '_FUN' and, potentially, prepare another call to be used in a decltype
1232 : return expression for a deduced return call op to allow for simple
1233 : implementation of the conversion operator. */
1234 :
1235 28566 : tree objfn;
1236 28566 : int nargs = list_length (DECL_ARGUMENTS (callop));
1237 28566 : if (thisarg)
1238 : {
1239 28522 : tree instance = cp_build_fold_indirect_ref (thisarg);
1240 28522 : objfn = lookup_template_function (DECL_NAME (callop),
1241 28522 : DECL_TI_ARGS (callop));
1242 28522 : objfn = build_min (COMPONENT_REF, NULL_TREE,
1243 : instance, objfn, NULL_TREE);
1244 28522 : --nargs;
1245 28522 : call = prepare_op_call (objfn, nargs);
1246 : }
1247 : else
1248 : objfn = callop;
1249 :
1250 28566 : if (type_uses_auto (fn_result))
1251 28346 : decltype_call = prepare_op_call (objfn, nargs);
1252 : }
1253 82868 : else if (thisarg)
1254 : {
1255 82323 : direct_argvec = make_tree_vector ();
1256 82323 : direct_argvec->quick_push (thisarg);
1257 : }
1258 :
1259 : /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1260 : declare the static member function "_FUN" below. For each arg append to
1261 : DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1262 : call args (for the template case). If a parameter pack is found, expand
1263 : it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1264 :
1265 111434 : tree fn_args = NULL_TREE;
1266 111434 : {
1267 111434 : int ix = 0;
1268 111434 : tree src = FUNCTION_FIRST_USER_PARM (callop);
1269 111434 : tree tgt = NULL;
1270 :
1271 111434 : if (!thisarg && !decltype_call)
1272 111434 : src = NULL_TREE;
1273 223354 : while (src)
1274 : {
1275 111920 : tree new_node = copy_node (src);
1276 : /* We set DECL_CONTEXT of NEW_NODE to the statfn below.
1277 : Notice this is creating a recursive type! */
1278 :
1279 : /* Clear TREE_ADDRESSABLE on thunk arguments. */
1280 111920 : TREE_ADDRESSABLE (new_node) = 0;
1281 :
1282 111920 : if (!fn_args)
1283 111920 : fn_args = tgt = new_node;
1284 : else
1285 : {
1286 37131 : TREE_CHAIN (tgt) = new_node;
1287 37131 : tgt = new_node;
1288 : }
1289 :
1290 111920 : mark_exp_read (tgt);
1291 :
1292 111920 : if (generic_lambda_p)
1293 : {
1294 52548 : tree a = tgt;
1295 52548 : if (thisarg)
1296 : {
1297 52504 : if (DECL_PACK_P (tgt))
1298 : {
1299 640 : a = make_pack_expansion (a);
1300 640 : PACK_EXPANSION_LOCAL_P (a) = true;
1301 : }
1302 52504 : CALL_EXPR_ARG (call, ix) = a;
1303 : }
1304 :
1305 52548 : if (decltype_call)
1306 : {
1307 : /* Avoid capturing variables in this context. */
1308 52241 : ++cp_unevaluated_operand;
1309 52241 : CALL_EXPR_ARG (decltype_call, ix) = forward_parm (tgt);
1310 52241 : --cp_unevaluated_operand;
1311 : }
1312 :
1313 52548 : ++ix;
1314 : }
1315 : else
1316 59372 : vec_safe_push (direct_argvec, tgt);
1317 :
1318 111920 : src = TREE_CHAIN (src);
1319 : }
1320 : }
1321 :
1322 111434 : if (generic_lambda_p)
1323 : {
1324 28566 : if (decltype_call)
1325 : {
1326 28346 : fn_result = finish_decltype_type
1327 28346 : (decltype_call, /*id_expression_or_member_access_p=*/false,
1328 : tf_warning_or_error);
1329 : }
1330 : }
1331 82868 : else if (thisarg)
1332 : {
1333 : /* Don't warn on deprecated or unavailable lambda declarations, unless
1334 : the lambda is actually called. */
1335 82323 : auto du = make_temp_override (deprecated_state,
1336 82323 : UNAVAILABLE_DEPRECATED_SUPPRESS);
1337 82323 : call = build_call_a (callop, direct_argvec->length (),
1338 : direct_argvec->address ());
1339 82323 : }
1340 :
1341 111434 : if (thisarg)
1342 : {
1343 110845 : CALL_FROM_THUNK_P (call) = 1;
1344 110845 : SET_EXPR_LOCATION (call, UNKNOWN_LOCATION);
1345 : }
1346 :
1347 111434 : tree stattype
1348 111434 : = cp_build_function_type (fn_result,
1349 111434 : FUNCTION_FIRST_USER_PARMTYPE (callop));
1350 111434 : stattype = cp_build_type_attribute_variant (stattype,
1351 111434 : TYPE_ATTRIBUTES (optype));
1352 111434 : if (flag_noexcept_type
1353 111434 : && TYPE_NOTHROW_P (TREE_TYPE (callop)))
1354 118 : stattype = build_exception_variant (stattype, noexcept_true_spec);
1355 :
1356 111434 : if (generic_lambda_p)
1357 28566 : --processing_template_decl;
1358 :
1359 : /* First build up the conversion op. */
1360 :
1361 111434 : tree rettype = build_pointer_type (stattype);
1362 111434 : tree name = make_conv_op_name (rettype);
1363 111434 : tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1364 111434 : tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
1365 : /* DR 1722: The conversion function should be noexcept. */
1366 111434 : fntype = build_exception_variant (fntype, noexcept_true_spec);
1367 111434 : tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
1368 111434 : SET_DECL_LANGUAGE (convfn, lang_cplusplus);
1369 111434 : tree fn = convfn;
1370 111434 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1371 111434 : SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
1372 111434 : grokclassfn (type, fn, NO_SPECIAL);
1373 111434 : set_linkage_according_to_type (type, fn);
1374 111434 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1375 111434 : DECL_IN_AGGR_P (fn) = 1;
1376 111434 : DECL_ARTIFICIAL (fn) = 1;
1377 111434 : DECL_NOT_REALLY_EXTERN (fn) = 1;
1378 111434 : DECL_DECLARED_INLINE_P (fn) = 1;
1379 111434 : DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop);
1380 222868 : if (DECL_IMMEDIATE_FUNCTION_P (callop))
1381 5328 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
1382 111434 : DECL_ARGUMENTS (fn) = build_this_parm (fn, fntype, TYPE_QUAL_CONST);
1383 :
1384 111434 : if (nested_def)
1385 100051 : DECL_INTERFACE_KNOWN (fn) = 1;
1386 :
1387 111434 : if (generic_lambda_p)
1388 28566 : fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1389 :
1390 111434 : add_method (type, fn, false);
1391 :
1392 111434 : if (thisarg == NULL_TREE)
1393 : {
1394 : /* For static lambda, just return operator(). */
1395 589 : if (nested)
1396 170 : push_function_context ();
1397 : else
1398 : /* Still increment function_depth so that we don't GC in the
1399 : middle of an expression. */
1400 419 : ++function_depth;
1401 :
1402 : /* Generate the body of the conversion op. */
1403 :
1404 589 : start_preparsed_function (convfn, NULL_TREE,
1405 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1406 589 : tree body = begin_function_body ();
1407 589 : tree compound_stmt = begin_compound_stmt (0);
1408 :
1409 : /* decl_needed_p needs to see that it's used. */
1410 589 : TREE_USED (callop) = 1;
1411 589 : finish_return_stmt (decay_conversion (callop, tf_warning_or_error));
1412 :
1413 589 : finish_compound_stmt (compound_stmt);
1414 589 : finish_function_body (body);
1415 :
1416 589 : fn = finish_function (/*inline_p=*/true);
1417 589 : if (!generic_lambda_p)
1418 545 : expand_or_defer_fn (fn);
1419 :
1420 589 : if (nested)
1421 170 : pop_function_context ();
1422 : else
1423 419 : --function_depth;
1424 : return;
1425 : }
1426 :
1427 : /* Generic thunk code fails for varargs; we'll complain in mark_used if
1428 : the conversion op is used. */
1429 110845 : if (varargs_function_p (callop))
1430 : {
1431 242 : DECL_DELETED_FN (fn) = 1;
1432 242 : return;
1433 : }
1434 :
1435 : /* Now build up the thunk to be returned. */
1436 :
1437 110603 : tree statfn = build_lang_decl (FUNCTION_DECL, fun_identifier, stattype);
1438 110603 : SET_DECL_LANGUAGE (statfn, lang_cplusplus);
1439 110603 : fn = statfn;
1440 110603 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1441 110603 : grokclassfn (type, fn, NO_SPECIAL);
1442 110603 : set_linkage_according_to_type (type, fn);
1443 110603 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1444 110603 : DECL_IN_AGGR_P (fn) = 1;
1445 110603 : DECL_ARTIFICIAL (fn) = 1;
1446 110603 : DECL_NOT_REALLY_EXTERN (fn) = 1;
1447 110603 : DECL_DECLARED_INLINE_P (fn) = 1;
1448 110603 : DECL_STATIC_FUNCTION_P (fn) = 1;
1449 110603 : DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop);
1450 221206 : if (DECL_IMMEDIATE_FUNCTION_P (callop))
1451 4812 : SET_DECL_IMMEDIATE_FUNCTION_P (fn);
1452 110603 : DECL_ARGUMENTS (fn) = fn_args;
1453 222253 : for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
1454 : {
1455 : /* Avoid duplicate -Wshadow warnings. */
1456 111650 : DECL_NAME (arg) = NULL_TREE;
1457 111650 : DECL_CONTEXT (arg) = fn;
1458 : }
1459 110603 : if (nested_def)
1460 99649 : DECL_INTERFACE_KNOWN (fn) = 1;
1461 :
1462 110603 : if (generic_lambda_p)
1463 28510 : fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1464 :
1465 110603 : if (flag_sanitize & SANITIZE_NULL)
1466 : /* Don't UBsan this function; we're deliberately calling op() with a null
1467 : object argument. */
1468 37 : add_no_sanitize_value (fn, SANITIZE_UNDEFINED);
1469 :
1470 110603 : add_method (type, fn, false);
1471 :
1472 110603 : if (nested)
1473 99727 : push_function_context ();
1474 : else
1475 : /* Still increment function_depth so that we don't GC in the
1476 : middle of an expression. */
1477 10876 : ++function_depth;
1478 :
1479 : /* Generate the body of the thunk. */
1480 :
1481 110603 : start_preparsed_function (statfn, NULL_TREE,
1482 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1483 110603 : tree body = begin_function_body ();
1484 110603 : tree compound_stmt = begin_compound_stmt (0);
1485 110603 : if (!generic_lambda_p)
1486 : {
1487 82093 : set_flags_from_callee (call);
1488 82093 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1489 3204 : call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1490 : }
1491 110603 : call = convert_from_reference (call);
1492 110603 : finish_return_stmt (call);
1493 :
1494 110603 : finish_compound_stmt (compound_stmt);
1495 110603 : finish_function_body (body);
1496 :
1497 110603 : fn = finish_function (/*inline_p=*/true);
1498 110603 : if (!generic_lambda_p)
1499 82093 : expand_or_defer_fn (fn);
1500 :
1501 : /* Generate the body of the conversion op. */
1502 :
1503 110603 : start_preparsed_function (convfn, NULL_TREE,
1504 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1505 110603 : body = begin_function_body ();
1506 110603 : compound_stmt = begin_compound_stmt (0);
1507 :
1508 : /* decl_needed_p needs to see that it's used. */
1509 110603 : TREE_USED (statfn) = 1;
1510 110603 : finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1511 :
1512 110603 : finish_compound_stmt (compound_stmt);
1513 110603 : finish_function_body (body);
1514 :
1515 110603 : fn = finish_function (/*inline_p=*/true);
1516 110603 : if (!generic_lambda_p)
1517 82093 : expand_or_defer_fn (fn);
1518 :
1519 110603 : if (nested)
1520 99727 : pop_function_context ();
1521 : else
1522 10876 : --function_depth;
1523 : }
1524 :
1525 : /* True if FN is the static function "_FUN" that gets returned from the lambda
1526 : conversion operator. */
1527 :
1528 : bool
1529 1303296 : lambda_static_thunk_p (tree fn)
1530 : {
1531 1303296 : return (fn && TREE_CODE (fn) == FUNCTION_DECL
1532 1303296 : && DECL_ARTIFICIAL (fn)
1533 127921 : && DECL_STATIC_FUNCTION_P (fn)
1534 1553666 : && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn)));
1535 : }
1536 :
1537 : bool
1538 207038324 : call_from_lambda_thunk_p (tree call)
1539 : {
1540 207038324 : return (CALL_FROM_THUNK_P (call)
1541 207038324 : && lambda_static_thunk_p (current_function_decl));
1542 : }
1543 :
1544 : /* Returns true iff VAL is a lambda-related declaration which should
1545 : be ignored by unqualified lookup. */
1546 :
1547 : bool
1548 3779246296 : is_lambda_ignored_entity (tree val)
1549 : {
1550 : /* Look past normal, non-VLA capture proxies. */
1551 3779246296 : if (is_normal_capture_proxy (val)
1552 3779246296 : && !variably_modified_type_p (TREE_TYPE (val), NULL_TREE))
1553 : return true;
1554 :
1555 : /* Always ignore lambda fields, their names are only for debugging. */
1556 3770856917 : if (TREE_CODE (val) == FIELD_DECL
1557 3770856917 : && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1558 : return true;
1559 :
1560 : /* None of the lookups that use qualify_lookup want the op() from the
1561 : lambda; they want the one from the enclosing class. */
1562 3770856890 : if (tree fns = maybe_get_fns (val))
1563 1260692328 : if (LAMBDA_FUNCTION_P (OVL_FIRST (fns)))
1564 612677 : return true;
1565 :
1566 : return false;
1567 : }
1568 :
1569 : /* Lambdas that appear in variable initializer or default argument
1570 : scope get that in their mangling, so we need to record it. Also,
1571 : multiple lambdas in the same scope may need a mangling
1572 : discriminator. In ABI <= 17, there is a single per-scope sequence
1573 : number. In ABI >= 18, there are per-scope per-signature sequence
1574 : numbers. */
1575 : struct GTY(()) lambda_sig_count
1576 : {
1577 : tree fn; // The lambda fn whose sig this is.
1578 : unsigned count;
1579 : };
1580 : struct GTY(()) lambda_discriminator
1581 : {
1582 : tree scope;
1583 : unsigned nesting; // Inside a function, VAR_DECLs get the function
1584 : // as scope. This counts that nesting.
1585 : unsigned count; // The per-scope counter.
1586 : vec<lambda_sig_count, va_gc> *discriminators; // Per-signature counters
1587 : };
1588 : // The current scope.
1589 : static GTY(()) lambda_discriminator lambda_scope;
1590 : // Stack of previous scopes.
1591 : static GTY(()) vec<lambda_discriminator, va_gc> *lambda_scope_stack;
1592 :
1593 : // Push DECL as lambda extra scope, also new discriminator counters.
1594 :
1595 : void
1596 342995287 : start_lambda_scope (tree decl)
1597 : {
1598 342995287 : gcc_checking_assert (decl);
1599 342995287 : if (current_function_decl && VAR_P (decl))
1600 : // If we're inside a function, we ignore variable scope. Don't push.
1601 51885827 : lambda_scope.nesting++;
1602 : else
1603 : {
1604 291109460 : vec_safe_push (lambda_scope_stack, lambda_scope);
1605 291109460 : lambda_scope.scope = decl;
1606 291109460 : lambda_scope.nesting = 0;
1607 291109460 : lambda_scope.count = 0;
1608 291109460 : lambda_scope.discriminators = nullptr;
1609 : }
1610 342995287 : }
1611 :
1612 : // Pop from the current lambda extra scope.
1613 :
1614 : void
1615 342992437 : finish_lambda_scope (void)
1616 : {
1617 342992437 : if (!lambda_scope.nesting--)
1618 : {
1619 291106610 : lambda_scope = lambda_scope_stack->last ();
1620 291106610 : lambda_scope_stack->pop ();
1621 : }
1622 342992437 : }
1623 :
1624 : // Record the current lambda scope into LAMBDA
1625 :
1626 : void
1627 1664679 : record_lambda_scope (tree lambda)
1628 : {
1629 1664679 : tree closure = LAMBDA_EXPR_CLOSURE (lambda);
1630 1664679 : gcc_checking_assert (closure);
1631 :
1632 : /* Before ABI v20, lambdas in static data member initializers did not
1633 : get a dedicated lambda scope. */
1634 1664679 : tree scope = lambda_scope.scope;
1635 1664679 : if (is_static_data_member_initialized_in_class (scope))
1636 : {
1637 158185 : if (!abi_version_at_least (20))
1638 158185 : scope = NULL_TREE;
1639 472956 : if (warn_abi && abi_version_crosses (20) && !processing_template_decl)
1640 : {
1641 18 : if (abi_version_at_least (20))
1642 18 : warning_at (location_of (closure), OPT_Wabi,
1643 : "the mangled name of %qT changed in "
1644 : "%<-fabi-version=20%> (GCC 15.1)", closure);
1645 : else
1646 0 : warning_at (location_of (closure), OPT_Wabi,
1647 : "the mangled name of %qT changes in "
1648 : "%<-fabi-version=20%> (GCC 15.1)", closure);
1649 : }
1650 : }
1651 :
1652 : /* An otherwise unattached class-scope lambda in a member template
1653 : should not have a mangling scope, as the mangling scope will not
1654 : correctly inherit on instantiation. */
1655 1664679 : tree ctx = TYPE_CONTEXT (closure);
1656 1664679 : if (scope
1657 1664679 : && ctx
1658 1663559 : && CLASS_TYPE_P (ctx)
1659 161790 : && ctx == TREE_TYPE (scope)
1660 1666881 : && current_template_depth > template_class_depth (ctx))
1661 : scope = NULL_TREE;
1662 :
1663 1664679 : LAMBDA_EXPR_EXTRA_SCOPE (lambda) = scope;
1664 1664679 : if (scope)
1665 1663462 : maybe_key_decl (scope, TYPE_NAME (closure));
1666 1664679 : }
1667 :
1668 : // Compare lambda template heads TMPL_A and TMPL_B, used for both
1669 : // templated lambdas, and template template parameters of said lambda.
1670 :
1671 : static bool
1672 33252 : compare_lambda_template_head (tree tmpl_a, tree tmpl_b)
1673 : {
1674 : // We only need one level of template parms
1675 33252 : tree inner_a = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_a));
1676 33252 : tree inner_b = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_b));
1677 :
1678 : // We only compare explicit template parms, ignoring trailing
1679 : // synthetic ones.
1680 33252 : int len_a = TREE_VEC_LENGTH (inner_a);
1681 33252 : int len_b = TREE_VEC_LENGTH (inner_b);
1682 :
1683 34171 : for (int ix = 0, len = MAX (len_a, len_b); ix != len; ix++)
1684 : {
1685 33299 : tree parm_a = NULL_TREE;
1686 33299 : if (ix < len_a)
1687 : {
1688 33287 : parm_a = TREE_VEC_ELT (inner_a, ix);
1689 33287 : if (parm_a == error_mark_node)
1690 : return false;
1691 33287 : parm_a = TREE_VALUE (parm_a);
1692 33287 : if (parm_a == error_mark_node)
1693 : return false;
1694 33287 : if (DECL_VIRTUAL_P (parm_a))
1695 16188 : parm_a = NULL_TREE;
1696 : }
1697 :
1698 33299 : tree parm_b = NULL_TREE;
1699 33299 : if (ix < len_b)
1700 : {
1701 33279 : parm_b = TREE_VEC_ELT (inner_b, ix);
1702 33279 : if (parm_b == error_mark_node)
1703 : return false;
1704 33279 : parm_b = TREE_VALUE (parm_b);
1705 33279 : if (parm_b == error_mark_node)
1706 : return false;
1707 33273 : if (DECL_VIRTUAL_P (parm_b))
1708 17378 : parm_b = NULL_TREE;
1709 : }
1710 :
1711 33293 : if (!parm_a && !parm_b)
1712 : // we're done
1713 : break;
1714 :
1715 17178 : if (!(parm_a && parm_b))
1716 : return false;
1717 :
1718 15847 : if (TREE_CODE (parm_a) != TREE_CODE (parm_b))
1719 : return false;
1720 :
1721 13933 : if (TREE_CODE (parm_a) == PARM_DECL)
1722 : {
1723 13035 : if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_a))
1724 13035 : != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_b)))
1725 : return false;
1726 :
1727 30 : if (!same_type_p (TREE_TYPE (parm_a), TREE_TYPE (parm_b)))
1728 : return false;
1729 : }
1730 : else
1731 : {
1732 898 : if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_a))
1733 898 : != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_b)))
1734 : return false;
1735 :
1736 898 : if (TREE_CODE (parm_a) != TEMPLATE_DECL)
1737 898 : gcc_checking_assert (TREE_CODE (parm_a) == TYPE_DECL);
1738 0 : else if (!compare_lambda_template_head (parm_a, parm_b))
1739 : return false;
1740 : }
1741 : }
1742 :
1743 : return true;
1744 : }
1745 :
1746 : // Compare lambda signatures FN_A and FN_B, they may be TEMPLATE_DECLs too.
1747 :
1748 : static bool
1749 261584 : compare_lambda_sig (tree fn_a, tree fn_b)
1750 : {
1751 261584 : if (TREE_CODE (fn_a) == TEMPLATE_DECL
1752 47432 : && TREE_CODE (fn_b) == TEMPLATE_DECL)
1753 : {
1754 33252 : if (!compare_lambda_template_head (fn_a, fn_b))
1755 : return false;
1756 16987 : fn_a = DECL_TEMPLATE_RESULT (fn_a);
1757 16987 : fn_b = DECL_TEMPLATE_RESULT (fn_b);
1758 : }
1759 228332 : else if (TREE_CODE (fn_a) == TEMPLATE_DECL
1760 214152 : || TREE_CODE (fn_b) == TEMPLATE_DECL)
1761 : return false;
1762 :
1763 230778 : if (fn_a == error_mark_node
1764 230776 : || fn_b == error_mark_node)
1765 : return false;
1766 :
1767 230774 : for (tree args_a = FUNCTION_FIRST_USER_PARMTYPE (fn_a),
1768 230774 : args_b = FUNCTION_FIRST_USER_PARMTYPE (fn_b);
1769 414060 : args_a || args_b;
1770 183286 : args_a = TREE_CHAIN (args_a), args_b = TREE_CHAIN (args_b))
1771 : {
1772 298532 : if (!args_a || !args_b)
1773 : return false;
1774 : // This check also deals with differing variadicness
1775 298527 : if (!same_type_p (TREE_VALUE (args_a), TREE_VALUE (args_b)))
1776 : return false;
1777 : }
1778 :
1779 : return true;
1780 : }
1781 :
1782 : // Record the per-scope discriminator of LAMBDA. If the extra scope
1783 : // is empty, we must use the empty scope counter, which might not be
1784 : // the live one.
1785 :
1786 : void
1787 1667896 : record_lambda_scope_discriminator (tree lambda)
1788 : {
1789 3334433 : auto *slot = (vec_safe_is_empty (lambda_scope_stack)
1790 1666537 : || LAMBDA_EXPR_EXTRA_SCOPE (lambda)
1791 3075 : ? &lambda_scope : lambda_scope_stack->begin ());
1792 1667896 : LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda) = slot->count++;
1793 1667896 : }
1794 :
1795 : // Record the per-scope per-signature discriminator of LAMBDA. If the
1796 : // extra scope is empty, we must use the empty scope counter, which
1797 : // might not be the live one.
1798 :
1799 : void
1800 1667884 : record_lambda_scope_sig_discriminator (tree lambda, tree fn)
1801 : {
1802 3334409 : auto *slot = (vec_safe_is_empty (lambda_scope_stack)
1803 1666525 : || LAMBDA_EXPR_EXTRA_SCOPE (lambda)
1804 3072 : ? &lambda_scope : lambda_scope_stack->begin ());
1805 1667884 : gcc_checking_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda) == slot->scope);
1806 :
1807 : // A linear search, we're not expecting this to be a big list, and
1808 : // this avoids needing a signature hash function.
1809 1667884 : lambda_sig_count *sig;
1810 1667884 : if (unsigned ix = vec_safe_length (slot->discriminators))
1811 385831 : for (sig = slot->discriminators->begin (); ix--; sig++)
1812 261584 : if (compare_lambda_sig (fn, sig->fn))
1813 115528 : goto found;
1814 1552356 : {
1815 1552356 : lambda_sig_count init = {fn, 0};
1816 1552356 : sig = vec_safe_push (slot->discriminators, init);
1817 : }
1818 1667884 : found:
1819 1667884 : LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda) = sig->count++;
1820 1667884 : }
1821 :
1822 : /* Push the proxies for any explicit captures in LAMBDA_EXPR.
1823 : If EARLY_P, we do not have the real operator() yet. */
1824 :
1825 : void
1826 3335745 : push_capture_proxies (tree lambda_expr, bool early_p)
1827 : {
1828 5639192 : for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
1829 2303447 : cap = TREE_CHAIN (cap))
1830 2303447 : build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap), early_p);
1831 3335745 : }
1832 :
1833 : tree
1834 1667849 : start_lambda_function (tree fco, tree lambda_expr)
1835 : {
1836 : /* Let the front end know that we are going to be defining this
1837 : function. */
1838 1667849 : start_preparsed_function (fco,
1839 : NULL_TREE,
1840 : SF_PRE_PARSED | SF_INCLASS_INLINE);
1841 :
1842 1667849 : tree body = begin_function_body ();
1843 :
1844 : /* Push the proxies for any explicit captures. */
1845 1667849 : push_capture_proxies (lambda_expr);
1846 :
1847 1667849 : return body;
1848 : }
1849 :
1850 : /* Subroutine of prune_lambda_captures: CAP is a node in
1851 : LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
1852 : might optimize away the capture, or NULL_TREE if there is no such
1853 : variable. */
1854 :
1855 : static tree
1856 4562 : var_to_maybe_prune (tree cap)
1857 : {
1858 4562 : if (LAMBDA_CAPTURE_EXPLICIT_P (cap))
1859 : /* Don't prune explicit captures. */
1860 : return NULL_TREE;
1861 :
1862 4553 : tree mem = TREE_PURPOSE (cap);
1863 9106 : if (!DECL_P (mem) || !DECL_NORMAL_CAPTURE_P (mem))
1864 : /* Packs and init-captures aren't captures of constant vars. */
1865 : return NULL_TREE;
1866 :
1867 4553 : tree init = TREE_VALUE (cap);
1868 4553 : if (is_normal_capture_proxy (init))
1869 0 : init = DECL_CAPTURED_VARIABLE (init);
1870 4553 : if (decl_constant_var_p (init))
1871 1886 : return init;
1872 :
1873 : return NULL_TREE;
1874 : }
1875 :
1876 : /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
1877 : for constant variables are actually used in the lambda body.
1878 :
1879 : There will always be a DECL_EXPR for the capture proxy; remember it when we
1880 : see it, but replace it with any other use. */
1881 :
1882 : static tree
1883 74623 : mark_const_cap_r (tree *t, int *walk_subtrees, void *data)
1884 : {
1885 74623 : hash_map<tree,tree*> &const_vars = *(hash_map<tree,tree*>*)data;
1886 :
1887 74623 : tree var = NULL_TREE;
1888 74623 : if (TREE_CODE (*t) == DECL_EXPR)
1889 : {
1890 5662 : tree decl = DECL_EXPR_DECL (*t);
1891 5662 : if (is_constant_capture_proxy (decl))
1892 : {
1893 1898 : var = DECL_CAPTURED_VARIABLE (decl);
1894 1898 : *walk_subtrees = 0;
1895 : }
1896 : }
1897 68961 : else if (!location_wrapper_p (*t) /* is_capture_proxy dislikes them. */
1898 68961 : && is_constant_capture_proxy (*t))
1899 312 : var = DECL_CAPTURED_VARIABLE (*t);
1900 :
1901 74623 : if (var)
1902 : {
1903 2210 : tree *&slot = const_vars.get_or_insert (var);
1904 2210 : if (!slot || VAR_P (*t))
1905 2210 : slot = t;
1906 : }
1907 :
1908 74623 : return NULL_TREE;
1909 : }
1910 :
1911 : /* We're at the end of processing a lambda; go back and remove any captures of
1912 : constant variables for which we've folded away all uses. */
1913 :
1914 : static void
1915 1667849 : prune_lambda_captures (tree body)
1916 : {
1917 1667849 : tree lam = current_lambda_expr ();
1918 1667849 : if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam))
1919 : /* No uses were optimized away. */
1920 1666327 : return;
1921 1796 : if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
1922 : /* No default captures, and we don't prune explicit captures. */
1923 : return;
1924 : /* Don't bother pruning in a template, we'll prune at instantiation time. */
1925 1747 : if (dependent_type_p (TREE_TYPE (lam)))
1926 : return;
1927 :
1928 1522 : hash_map<tree,tree*> const_vars;
1929 :
1930 1522 : cp_walk_tree_without_duplicates (&body, mark_const_cap_r, &const_vars);
1931 :
1932 1522 : tree bind_expr = expr_single (DECL_SAVED_TREE (lambda_function (lam)));
1933 3056 : bool noexcept_p = (bind_expr
1934 1522 : && TREE_CODE (bind_expr) == MUST_NOT_THROW_EXPR);
1935 12 : if (noexcept_p)
1936 12 : bind_expr = expr_single (TREE_OPERAND (bind_expr, 0));
1937 :
1938 1522 : tree *fieldp = &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam));
1939 6084 : for (tree *capp = &LAMBDA_EXPR_CAPTURE_LIST (lam); *capp; )
1940 : {
1941 4562 : tree cap = *capp;
1942 4562 : if (tree var = var_to_maybe_prune (cap))
1943 : {
1944 1886 : tree **use = const_vars.get (var);
1945 1886 : if (TREE_CODE (**use) == DECL_EXPR)
1946 : {
1947 : /* All uses of this capture were folded away, leaving only the
1948 : proxy declaration. */
1949 :
1950 1574 : if (noexcept_p)
1951 : {
1952 : /* We didn't handle noexcept lambda captures correctly before
1953 : the fix for PR c++/119764. */
1954 33 : if (abi_version_crosses (21))
1955 3 : warning_at (location_of (lam), OPT_Wabi, "%qD is no longer"
1956 : " captured in noexcept lambda in ABI v21 "
1957 : "(GCC 16)", var);
1958 12 : if (!abi_version_at_least (21))
1959 0 : goto next;
1960 : }
1961 :
1962 : /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
1963 1574 : *capp = TREE_CHAIN (cap);
1964 :
1965 : /* And out of TYPE_FIELDS. */
1966 1574 : tree field = TREE_PURPOSE (cap);
1967 4091 : while (*fieldp != field)
1968 2517 : fieldp = &DECL_CHAIN (*fieldp);
1969 1574 : *fieldp = DECL_CHAIN (*fieldp);
1970 :
1971 : /* And out of the bindings for the function. */
1972 1574 : tree *blockp = &BLOCK_VARS (current_binding_level->blocks);
1973 3236 : while (*blockp != DECL_EXPR_DECL (**use))
1974 1662 : blockp = &DECL_CHAIN (*blockp);
1975 1574 : *blockp = DECL_CHAIN (*blockp);
1976 :
1977 : /* And maybe out of the vars declared in the containing
1978 : BIND_EXPR, if it's listed there. */
1979 1574 : tree *bindp = &BIND_EXPR_VARS (bind_expr);
1980 4148 : while (*bindp && *bindp != DECL_EXPR_DECL (**use))
1981 1000 : bindp = &DECL_CHAIN (*bindp);
1982 1574 : if (*bindp)
1983 523 : *bindp = DECL_CHAIN (*bindp);
1984 :
1985 : /* And remove the capture proxy declaration. */
1986 1574 : **use = void_node;
1987 1574 : continue;
1988 1574 : }
1989 : }
1990 :
1991 2988 : next:
1992 2988 : capp = &TREE_CHAIN (cap);
1993 : }
1994 1522 : }
1995 :
1996 : // Record the per-scope per-signature discriminator of LAMBDA. If the
1997 : // extra scope is empty, we must use the empty scope counter, which
1998 : // might not be the live one.
1999 :
2000 : void
2001 1667849 : finish_lambda_function (tree body)
2002 : {
2003 1667849 : finish_function_body (body);
2004 :
2005 1667849 : prune_lambda_captures (cur_stmt_list);
2006 :
2007 : /* Finish the function and generate code for it if necessary. */
2008 1667849 : tree fn = finish_function (/*inline_p=*/true);
2009 :
2010 : /* Only expand if the call op is not a template. */
2011 1667849 : if (!DECL_TEMPLATE_INFO (fn))
2012 355660 : expand_or_defer_fn (fn);
2013 1667849 : }
2014 :
2015 : #include "gt-cp-lambda.h"
|