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