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