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