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