compiler: give error for non-int arguments to make
[official-gcc.git] / gcc / cp / lambda.c
blobff8236ad316c453ff2f2f7866f69718e1ce016db
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.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
8 This file is part of GCC.
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.
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.
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/>. */
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"
34 /* Constructor for a lambda expression. */
36 tree
37 build_lambda_expr (void)
39 tree lambda = make_node (LAMBDA_EXPR);
40 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
41 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
42 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
43 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
44 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
45 return lambda;
48 /* Create the closure object for a LAMBDA_EXPR. */
50 tree
51 build_lambda_object (tree lambda_expr)
53 /* Build aggregate constructor call.
54 - cp_parser_braced_list
55 - cp_parser_functional_cast */
56 vec<constructor_elt, va_gc> *elts = NULL;
57 tree node, expr, type;
58 location_t saved_loc;
60 if (processing_template_decl || lambda_expr == error_mark_node)
61 return lambda_expr;
63 /* Make sure any error messages refer to the lambda-introducer. */
64 saved_loc = input_location;
65 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
67 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
68 node;
69 node = TREE_CHAIN (node))
71 tree field = TREE_PURPOSE (node);
72 tree val = TREE_VALUE (node);
74 if (field == error_mark_node)
76 expr = error_mark_node;
77 goto out;
80 if (TREE_CODE (val) == TREE_LIST)
81 val = build_x_compound_expr_from_list (val, ELK_INIT,
82 tf_warning_or_error);
84 if (DECL_P (val))
85 mark_used (val);
87 /* Mere mortals can't copy arrays with aggregate initialization, so
88 do some magic to make it work here. */
89 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
90 val = build_array_copy (val);
91 else if (DECL_NORMAL_CAPTURE_P (field)
92 && !DECL_VLA_CAPTURE_P (field)
93 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
95 /* "the entities that are captured by copy are used to
96 direct-initialize each corresponding non-static data
97 member of the resulting closure object."
99 There's normally no way to express direct-initialization
100 from an element of a CONSTRUCTOR, so we build up a special
101 TARGET_EXPR to bypass the usual copy-initialization. */
102 val = force_rvalue (val, tf_warning_or_error);
103 if (TREE_CODE (val) == TARGET_EXPR)
104 TARGET_EXPR_DIRECT_INIT_P (val) = true;
107 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
110 expr = build_constructor (init_list_type_node, elts);
111 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
113 /* N2927: "[The closure] class type is not an aggregate."
114 But we briefly treat it as an aggregate to make this simpler. */
115 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
116 CLASSTYPE_NON_AGGREGATE (type) = 0;
117 expr = finish_compound_literal (type, expr, tf_warning_or_error);
118 CLASSTYPE_NON_AGGREGATE (type) = 1;
120 out:
121 input_location = saved_loc;
122 return expr;
125 /* Return an initialized RECORD_TYPE for LAMBDA.
126 LAMBDA must have its explicit captures already. */
128 tree
129 begin_lambda_type (tree lambda)
131 tree type;
134 /* Unique name. This is just like an unnamed class, but we cannot use
135 make_anon_name because of certain checks against TYPE_UNNAMED_P. */
136 tree name;
137 name = make_lambda_name ();
139 /* Create the new RECORD_TYPE for this lambda. */
140 type = xref_tag (/*tag_code=*/record_type,
141 name,
142 /*scope=*/ts_lambda,
143 /*template_header_p=*/false);
144 if (type == error_mark_node)
145 return error_mark_node;
148 /* Designate it as a struct so that we can use aggregate initialization. */
149 CLASSTYPE_DECLARED_CLASS (type) = false;
151 /* Cross-reference the expression and the type. */
152 LAMBDA_EXPR_CLOSURE (lambda) = type;
153 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
155 /* In C++17, assume the closure is literal; we'll clear the flag later if
156 necessary. */
157 if (cxx_dialect >= cxx17)
158 CLASSTYPE_LITERAL_P (type) = true;
160 /* Clear base types. */
161 xref_basetypes (type, /*bases=*/NULL_TREE);
163 /* Start the class. */
164 type = begin_class_definition (type);
166 return type;
169 /* Returns the type to use for the return type of the operator() of a
170 closure class. */
172 tree
173 lambda_return_type (tree expr)
175 if (expr == NULL_TREE)
176 return void_type_node;
177 if (type_unknown_p (expr)
178 || BRACE_ENCLOSED_INITIALIZER_P (expr))
180 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
181 return error_mark_node;
183 gcc_checking_assert (!type_dependent_expression_p (expr));
184 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
187 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
188 closure type. */
190 tree
191 lambda_function (tree lambda)
193 tree type;
194 if (TREE_CODE (lambda) == LAMBDA_EXPR)
195 type = LAMBDA_EXPR_CLOSURE (lambda);
196 else
197 type = lambda;
198 gcc_assert (LAMBDA_TYPE_P (type));
199 /* Don't let debug_tree cause instantiation. */
200 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
201 && !COMPLETE_OR_OPEN_TYPE_P (type))
202 return NULL_TREE;
203 lambda = lookup_member (type, call_op_identifier,
204 /*protect=*/0, /*want_type=*/false,
205 tf_warning_or_error);
206 if (lambda)
207 lambda = STRIP_TEMPLATE (get_first_fn (lambda));
208 return lambda;
211 /* Returns the type to use for the FIELD_DECL corresponding to the
212 capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
213 C++14 init capture, and BY_REFERENCE_P indicates whether we're
214 capturing by reference. */
216 tree
217 lambda_capture_field_type (tree expr, bool explicit_init_p,
218 bool by_reference_p)
220 tree type;
221 bool is_this = is_this_parameter (tree_strip_nop_conversions (expr));
223 if (!is_this && type_dependent_expression_p (expr))
225 type = cxx_make_type (DECLTYPE_TYPE);
226 DECLTYPE_TYPE_EXPR (type) = expr;
227 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
228 DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p;
229 DECLTYPE_FOR_REF_CAPTURE (type) = by_reference_p;
230 SET_TYPE_STRUCTURAL_EQUALITY (type);
232 else if (!is_this && explicit_init_p)
234 tree auto_node = make_auto ();
236 type = auto_node;
237 if (by_reference_p)
238 /* Add the reference now, so deduction doesn't lose
239 outermost CV qualifiers of EXPR. */
240 type = build_reference_type (type);
241 type = do_auto_deduction (type, expr, auto_node);
243 else
245 type = non_reference (unlowered_expr_type (expr));
247 if (!is_this
248 && (by_reference_p || TREE_CODE (type) == FUNCTION_TYPE))
249 type = build_reference_type (type);
252 return type;
255 /* Returns true iff DECL is a lambda capture proxy variable created by
256 build_capture_proxy. */
258 bool
259 is_capture_proxy (tree decl)
261 return (VAR_P (decl)
262 && DECL_HAS_VALUE_EXPR_P (decl)
263 && !DECL_ANON_UNION_VAR_P (decl)
264 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
267 /* Returns true iff DECL is a capture proxy for a normal capture
268 (i.e. without explicit initializer). */
270 bool
271 is_normal_capture_proxy (tree decl)
273 if (!is_capture_proxy (decl))
274 /* It's not a capture proxy. */
275 return false;
277 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
278 /* VLA capture. */
279 return true;
281 /* It is a capture proxy, is it a normal capture? */
282 tree val = DECL_VALUE_EXPR (decl);
283 if (val == error_mark_node)
284 return true;
286 if (TREE_CODE (val) == ADDR_EXPR)
287 val = TREE_OPERAND (val, 0);
288 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
289 val = TREE_OPERAND (val, 1);
290 return DECL_NORMAL_CAPTURE_P (val);
293 /* Returns true iff DECL is a capture proxy for which we can use
294 DECL_CAPTURED_VARIABLE. In effect, this is a normal proxy other than a
295 nested capture of a function parameter pack. */
297 bool
298 is_capture_proxy_with_ref (tree var)
300 return (is_normal_capture_proxy (var) && DECL_LANG_SPECIFIC (var)
301 && DECL_CAPTURED_VARIABLE (var));
304 /* VAR is a capture proxy created by build_capture_proxy; add it to the
305 current function, which is the operator() for the appropriate lambda. */
307 void
308 insert_capture_proxy (tree var)
310 if (is_capture_proxy_with_ref (var))
312 tree cap = DECL_CAPTURED_VARIABLE (var);
313 if (CHECKING_P)
315 gcc_assert (!is_normal_capture_proxy (cap));
316 tree old = retrieve_local_specialization (cap);
317 if (old)
318 gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var));
320 register_local_specialization (var, cap);
323 /* Put the capture proxy in the extra body block so that it won't clash
324 with a later local variable. */
325 pushdecl_outermost_localscope (var);
327 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
328 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
329 tree stmt_list = (*stmt_list_stack)[1];
330 gcc_assert (stmt_list);
331 append_to_statement_list_force (var, &stmt_list);
334 /* We've just finished processing a lambda; if the containing scope is also
335 a lambda, insert any capture proxies that were created while processing
336 the nested lambda. */
338 void
339 insert_pending_capture_proxies (void)
341 tree lam;
342 vec<tree, va_gc> *proxies;
343 unsigned i;
345 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
346 return;
348 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
349 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
350 for (i = 0; i < vec_safe_length (proxies); ++i)
352 tree var = (*proxies)[i];
353 insert_capture_proxy (var);
355 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
356 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
359 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
360 return the type we want the proxy to have: the type of the field itself,
361 with added const-qualification if the lambda isn't mutable and the
362 capture is by value. */
364 tree
365 lambda_proxy_type (tree ref)
367 tree type;
368 if (ref == error_mark_node)
369 return error_mark_node;
370 if (REFERENCE_REF_P (ref))
371 ref = TREE_OPERAND (ref, 0);
372 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
373 type = TREE_TYPE (ref);
374 if (!type || WILDCARD_TYPE_P (non_reference (type)))
376 type = cxx_make_type (DECLTYPE_TYPE);
377 DECLTYPE_TYPE_EXPR (type) = ref;
378 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
379 SET_TYPE_STRUCTURAL_EQUALITY (type);
381 if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
382 type = make_pack_expansion (type);
383 return type;
386 /* MEMBER is a capture field in a lambda closure class. Now that we're
387 inside the operator(), build a placeholder var for future lookups and
388 debugging. */
390 tree
391 build_capture_proxy (tree member, tree init)
393 tree var, object, fn, closure, name, lam, type;
395 if (PACK_EXPANSION_P (member))
396 member = PACK_EXPANSION_PATTERN (member);
398 closure = DECL_CONTEXT (member);
399 fn = lambda_function (closure);
400 lam = CLASSTYPE_LAMBDA_EXPR (closure);
402 /* The proxy variable forwards to the capture field. */
403 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
404 object = finish_non_static_data_member (member, object, NULL_TREE);
405 if (REFERENCE_REF_P (object))
406 object = TREE_OPERAND (object, 0);
408 /* Remove the __ inserted by add_capture. */
409 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
411 type = lambda_proxy_type (object);
413 if (name == this_identifier && !POINTER_TYPE_P (type))
415 type = build_pointer_type (type);
416 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
417 object = build_fold_addr_expr_with_type (object, type);
420 if (DECL_VLA_CAPTURE_P (member))
422 /* Rebuild the VLA type from the pointer and maxindex. */
423 tree field = next_initializable_field (TYPE_FIELDS (type));
424 tree ptr = build_simple_component_ref (object, field);
425 field = next_initializable_field (DECL_CHAIN (field));
426 tree max = build_simple_component_ref (object, field);
427 type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
428 build_index_type (max));
429 type = build_reference_type (type);
430 REFERENCE_VLA_OK (type) = true;
431 object = convert (type, ptr);
434 var = build_decl (input_location, VAR_DECL, name, type);
435 SET_DECL_VALUE_EXPR (var, object);
436 DECL_HAS_VALUE_EXPR_P (var) = 1;
437 DECL_ARTIFICIAL (var) = 1;
438 TREE_USED (var) = 1;
439 DECL_CONTEXT (var) = fn;
441 if (DECL_NORMAL_CAPTURE_P (member))
443 if (DECL_VLA_CAPTURE_P (member))
445 init = CONSTRUCTOR_ELT (init, 0)->value;
446 init = TREE_OPERAND (init, 0); // Strip ADDR_EXPR.
447 init = TREE_OPERAND (init, 0); // Strip ARRAY_REF.
449 else
451 if (PACK_EXPANSION_P (init))
452 init = PACK_EXPANSION_PATTERN (init);
453 if (TREE_CODE (init) == INDIRECT_REF)
454 init = TREE_OPERAND (init, 0);
455 STRIP_NOPS (init);
458 if (TREE_CODE (init) == COMPONENT_REF)
459 /* We're capturing a capture of a function parameter pack, and have
460 lost track of the original variable. It's not important to have
461 DECL_CAPTURED_VARIABLE in this case, since a function parameter pack
462 isn't a constant variable, so don't bother trying to set it. */;
463 else
465 gcc_assert (VAR_P (init) || TREE_CODE (init) == PARM_DECL);
466 while (is_normal_capture_proxy (init))
467 init = DECL_CAPTURED_VARIABLE (init);
468 retrofit_lang_decl (var);
469 DECL_CAPTURED_VARIABLE (var) = init;
473 if (name == this_identifier)
475 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
476 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
479 if (fn == current_function_decl)
480 insert_capture_proxy (var);
481 else
482 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
484 return var;
487 static GTY(()) tree ptr_id;
488 static GTY(()) tree max_id;
490 /* Return a struct containing a pointer and a length for lambda capture of
491 an array of runtime length. */
493 static tree
494 vla_capture_type (tree array_type)
496 tree type = xref_tag (record_type, make_anon_name (), ts_current, false);
497 xref_basetypes (type, NULL_TREE);
498 type = begin_class_definition (type);
499 if (!ptr_id)
501 ptr_id = get_identifier ("ptr");
502 max_id = get_identifier ("max");
504 tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
505 tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
506 finish_member_declaration (field);
507 field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
508 finish_member_declaration (field);
509 return finish_struct (type, NULL_TREE);
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. */
517 tree
518 add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
519 bool explicit_init_p)
521 char *buf;
522 tree type, member, name;
523 bool vla = false;
524 bool variadic = false;
525 tree initializer = orig_init;
527 if (PACK_EXPANSION_P (initializer))
529 initializer = PACK_EXPANSION_PATTERN (initializer);
530 variadic = true;
533 if (TREE_CODE (initializer) == TREE_LIST
534 /* A pack expansion might end up with multiple elements. */
535 && !PACK_EXPANSION_P (TREE_VALUE (initializer)))
536 initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
537 tf_warning_or_error);
538 type = TREE_TYPE (initializer);
539 if (type == error_mark_node)
540 return error_mark_node;
542 if (array_of_runtime_bound_p (type))
544 vla = true;
545 if (!by_reference_p)
546 error ("array of runtime bound cannot be captured by copy, "
547 "only by reference");
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 tree elt = cp_build_array_ref (input_location, initializer,
552 integer_zero_node, tf_warning_or_error);
553 initializer = build_constructor_va (init_list_type_node, 2,
554 NULL_TREE, build_address (elt),
555 NULL_TREE, array_type_nelts (type));
556 type = vla_capture_type (type);
558 else if (!dependent_type_p (type)
559 && variably_modified_type_p (type, NULL_TREE))
561 error ("capture of variable-size type %qT that is not an N3639 array "
562 "of runtime bound", type);
563 if (TREE_CODE (type) == ARRAY_TYPE
564 && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
565 inform (input_location, "because the array element type %qT has "
566 "variable size", TREE_TYPE (type));
567 type = error_mark_node;
569 else
571 type = lambda_capture_field_type (initializer, explicit_init_p,
572 by_reference_p);
573 if (type == error_mark_node)
574 return error_mark_node;
576 if (id == this_identifier && !by_reference_p)
578 gcc_assert (POINTER_TYPE_P (type));
579 type = TREE_TYPE (type);
580 initializer = cp_build_fold_indirect_ref (initializer);
583 if (dependent_type_p (type))
585 else if (id != this_identifier && by_reference_p)
587 if (!lvalue_p (initializer))
589 error ("cannot capture %qE by reference", initializer);
590 return error_mark_node;
593 else
595 /* Capture by copy requires a complete type. */
596 type = complete_type (type);
597 if (!COMPLETE_TYPE_P (type))
599 error ("capture by copy of incomplete type %qT", type);
600 cxx_incomplete_type_inform (type);
601 return error_mark_node;
606 /* Add __ to the beginning of the field name so that user code
607 won't find the field with name lookup. We can't just leave the name
608 unset because template instantiation uses the name to find
609 instantiated fields. */
610 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
611 buf[1] = buf[0] = '_';
612 memcpy (buf + 2, IDENTIFIER_POINTER (id),
613 IDENTIFIER_LENGTH (id) + 1);
614 name = get_identifier (buf);
616 /* If TREE_TYPE isn't set, we're still in the introducer, so check
617 for duplicates. */
618 if (!LAMBDA_EXPR_CLOSURE (lambda))
620 if (IDENTIFIER_MARKED (name))
622 pedwarn (input_location, 0,
623 "already captured %qD in lambda expression", id);
624 return NULL_TREE;
626 IDENTIFIER_MARKED (name) = true;
629 if (variadic)
630 type = make_pack_expansion (type);
632 /* Make member variable. */
633 member = build_decl (input_location, FIELD_DECL, name, type);
634 DECL_VLA_CAPTURE_P (member) = vla;
636 if (!explicit_init_p)
637 /* Normal captures are invisible to name lookup but uses are replaced
638 with references to the capture field; we implement this by only
639 really making them invisible in unevaluated context; see
640 qualify_lookup. For now, let's make explicitly initialized captures
641 always visible. */
642 DECL_NORMAL_CAPTURE_P (member) = true;
644 if (id == this_identifier)
645 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
647 /* Add it to the appropriate closure class if we've started it. */
648 if (current_class_type
649 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
651 if (COMPLETE_TYPE_P (current_class_type))
652 internal_error ("trying to capture %qD in instantiation of "
653 "generic lambda", id);
654 finish_member_declaration (member);
657 tree listmem = member;
658 if (variadic)
660 listmem = make_pack_expansion (member);
661 initializer = orig_init;
663 LAMBDA_EXPR_CAPTURE_LIST (lambda)
664 = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
666 if (LAMBDA_EXPR_CLOSURE (lambda))
667 return build_capture_proxy (member, initializer);
668 /* For explicit captures we haven't started the function yet, so we wait
669 and build the proxy from cp_parser_lambda_body. */
670 return NULL_TREE;
673 /* Register all the capture members on the list CAPTURES, which is the
674 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
676 void
677 register_capture_members (tree captures)
679 if (captures == NULL_TREE)
680 return;
682 register_capture_members (TREE_CHAIN (captures));
684 tree field = TREE_PURPOSE (captures);
685 if (PACK_EXPANSION_P (field))
686 field = PACK_EXPANSION_PATTERN (field);
688 /* We set this in add_capture to avoid duplicates. */
689 IDENTIFIER_MARKED (DECL_NAME (field)) = false;
690 finish_member_declaration (field);
693 /* Similar to add_capture, except this works on a stack of nested lambdas.
694 BY_REFERENCE_P in this case is derived from the default capture mode.
695 Returns the capture for the lambda at the bottom of the stack. */
697 tree
698 add_default_capture (tree lambda_stack, tree id, tree initializer)
700 bool this_capture_p = (id == this_identifier);
702 tree var = NULL_TREE;
704 tree saved_class_type = current_class_type;
706 tree node;
708 for (node = lambda_stack;
709 node;
710 node = TREE_CHAIN (node))
712 tree lambda = TREE_VALUE (node);
714 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
715 if (DECL_PACK_P (initializer))
716 initializer = make_pack_expansion (initializer);
717 var = add_capture (lambda,
719 initializer,
720 /*by_reference_p=*/
721 (this_capture_p
722 || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
723 == CPLD_REFERENCE)),
724 /*explicit_init_p=*/false);
725 initializer = convert_from_reference (var);
728 current_class_type = saved_class_type;
730 return var;
733 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
734 form of an INDIRECT_REF, possibly adding it through default
735 capturing, if ADD_CAPTURE_P is true. */
737 tree
738 lambda_expr_this_capture (tree lambda, bool add_capture_p)
740 tree result;
742 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
744 /* In unevaluated context this isn't an odr-use, so don't capture. */
745 if (cp_unevaluated_operand)
746 add_capture_p = false;
748 /* Try to default capture 'this' if we can. */
749 if (!this_capture
750 && (!add_capture_p
751 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE))
753 tree lambda_stack = NULL_TREE;
754 tree init = NULL_TREE;
756 /* If we are in a lambda function, we can move out until we hit:
757 1. a non-lambda function or NSDMI,
758 2. a lambda function capturing 'this', or
759 3. a non-default capturing lambda function. */
760 for (tree tlambda = lambda; ;)
762 lambda_stack = tree_cons (NULL_TREE,
763 tlambda,
764 lambda_stack);
766 if (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)
767 && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)) == FIELD_DECL)
769 /* In an NSDMI, we don't have a function to look up the decl in,
770 but the fake 'this' pointer that we're using for parsing is
771 in scope_chain. */
772 init = scope_chain->x_current_class_ptr;
773 gcc_checking_assert
774 (init && (TREE_TYPE (TREE_TYPE (init))
775 == current_nonlambda_class_type ()));
776 break;
779 tree closure_decl = TYPE_NAME (LAMBDA_EXPR_CLOSURE (tlambda));
780 tree containing_function = decl_function_context (closure_decl);
782 if (containing_function == NULL_TREE)
783 /* We ran out of scopes; there's no 'this' to capture. */
784 break;
786 if (!LAMBDA_FUNCTION_P (containing_function))
788 /* We found a non-lambda function. */
789 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function))
790 /* First parameter is 'this'. */
791 init = DECL_ARGUMENTS (containing_function);
792 break;
795 tlambda
796 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
798 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
800 /* An outer lambda has already captured 'this'. */
801 init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
802 break;
805 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
806 /* An outer lambda won't let us capture 'this'. */
807 break;
810 if (init)
812 if (add_capture_p)
813 this_capture = add_default_capture (lambda_stack,
814 /*id=*/this_identifier,
815 init);
816 else
817 this_capture = init;
821 if (cp_unevaluated_operand)
822 result = this_capture;
823 else if (!this_capture)
825 if (add_capture_p)
827 error ("%<this%> was not captured for this lambda function");
828 result = error_mark_node;
830 else
831 result = NULL_TREE;
833 else
835 /* To make sure that current_class_ref is for the lambda. */
836 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
837 == LAMBDA_EXPR_CLOSURE (lambda));
839 result = this_capture;
841 /* If 'this' is captured, each use of 'this' is transformed into an
842 access to the corresponding unnamed data member of the closure
843 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
844 ensures that the transformed expression is an rvalue. ] */
845 result = rvalue (result);
848 return result;
851 /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
852 object. NULL otherwise.. */
854 static tree
855 resolvable_dummy_lambda (tree object)
857 if (!is_dummy_object (object))
858 return NULL_TREE;
860 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
861 gcc_assert (!TYPE_PTR_P (type));
863 if (type != current_class_type
864 && current_class_type
865 && LAMBDA_TYPE_P (current_class_type)
866 && lambda_function (current_class_type)
867 && DERIVED_FROM_P (type, current_nonlambda_class_type ()))
868 return CLASSTYPE_LAMBDA_EXPR (current_class_type);
870 return NULL_TREE;
873 /* We don't want to capture 'this' until we know we need it, i.e. after
874 overload resolution has chosen a non-static member function. At that
875 point we call this function to turn a dummy object into a use of the
876 'this' capture. */
878 tree
879 maybe_resolve_dummy (tree object, bool add_capture_p)
881 if (tree lam = resolvable_dummy_lambda (object))
882 if (tree cap = lambda_expr_this_capture (lam, add_capture_p))
883 if (cap != error_mark_node)
884 object = build_fold_indirect_ref (cap);
886 return object;
889 /* When parsing a generic lambda containing an argument-dependent
890 member function call we defer overload resolution to instantiation
891 time. But we have to know now whether to capture this or not.
892 Do that if FNS contains any non-static fns.
893 The std doesn't anticipate this case, but I expect this to be the
894 outcome of discussion. */
896 void
897 maybe_generic_this_capture (tree object, tree fns)
899 if (tree lam = resolvable_dummy_lambda (object))
900 if (!LAMBDA_EXPR_THIS_CAPTURE (lam))
902 /* We've not yet captured, so look at the function set of
903 interest. */
904 if (BASELINK_P (fns))
905 fns = BASELINK_FUNCTIONS (fns);
906 bool id_expr = TREE_CODE (fns) == TEMPLATE_ID_EXPR;
907 if (id_expr)
908 fns = TREE_OPERAND (fns, 0);
910 for (lkp_iterator iter (fns); iter; ++iter)
911 if ((!id_expr || TREE_CODE (*iter) == TEMPLATE_DECL)
912 && DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter))
914 /* Found a non-static member. Capture this. */
915 lambda_expr_this_capture (lam, true);
916 break;
921 /* Returns the innermost non-lambda function. */
923 tree
924 current_nonlambda_function (void)
926 tree fn = current_function_decl;
927 while (fn && LAMBDA_FUNCTION_P (fn))
928 fn = decl_function_context (fn);
929 return fn;
932 /* Returns the method basetype of the innermost non-lambda function, or
933 NULL_TREE if none. */
935 tree
936 nonlambda_method_basetype (void)
938 tree fn, type;
939 if (!current_class_ref)
940 return NULL_TREE;
942 type = current_class_type;
943 if (!type || !LAMBDA_TYPE_P (type))
944 return type;
946 /* Find the nearest enclosing non-lambda function. */
947 fn = TYPE_NAME (type);
949 fn = decl_function_context (fn);
950 while (fn && LAMBDA_FUNCTION_P (fn));
952 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
953 return NULL_TREE;
955 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
958 /* Like current_scope, but looking through lambdas. */
960 tree
961 current_nonlambda_scope (void)
963 tree scope = current_scope ();
964 for (;;)
966 if (TREE_CODE (scope) == FUNCTION_DECL
967 && LAMBDA_FUNCTION_P (scope))
969 scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope));
970 continue;
972 else if (LAMBDA_TYPE_P (scope))
974 scope = CP_TYPE_CONTEXT (scope);
975 continue;
977 break;
979 return scope;
982 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
983 indicated FN and NARGS, but do not initialize the return type or any of the
984 argument slots. */
986 static tree
987 prepare_op_call (tree fn, int nargs)
989 tree t;
991 t = build_vl_exp (CALL_EXPR, nargs + 3);
992 CALL_EXPR_FN (t) = fn;
993 CALL_EXPR_STATIC_CHAIN (t) = NULL;
995 return t;
998 /* Return true iff CALLOP is the op() for a generic lambda. */
1000 bool
1001 generic_lambda_fn_p (tree callop)
1003 return (LAMBDA_FUNCTION_P (callop)
1004 && DECL_TEMPLATE_INFO (callop)
1005 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
1008 /* If the closure TYPE has a static op(), also add a conversion to function
1009 pointer. */
1011 void
1012 maybe_add_lambda_conv_op (tree type)
1014 bool nested = (cfun != NULL);
1015 bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
1016 tree callop = lambda_function (type);
1017 tree lam = CLASSTYPE_LAMBDA_EXPR (type);
1019 if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE
1020 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE)
1021 return;
1023 if (processing_template_decl)
1024 return;
1026 bool const generic_lambda_p = generic_lambda_fn_p (callop);
1028 if (!generic_lambda_p && DECL_INITIAL (callop) == NULL_TREE)
1030 /* If the op() wasn't instantiated due to errors, give up. */
1031 gcc_assert (errorcount || sorrycount);
1032 return;
1035 /* Non-template conversion operators are defined directly with build_call_a
1036 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
1037 deferred and the CALL is built in-place. In the case of a deduced return
1038 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
1039 the return type is also built in-place. The arguments of DECLTYPE_CALL in
1040 the return expression may differ in flags from those in the body CALL. In
1041 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
1042 the body CALL, but not in DECLTYPE_CALL. */
1044 vec<tree, va_gc> *direct_argvec = 0;
1045 tree decltype_call = 0, call = 0;
1046 tree optype = TREE_TYPE (callop);
1047 tree fn_result = TREE_TYPE (optype);
1049 tree thisarg = build_nop (TREE_TYPE (DECL_ARGUMENTS (callop)),
1050 null_pointer_node);
1051 if (generic_lambda_p)
1053 ++processing_template_decl;
1055 /* Prepare the dependent member call for the static member function
1056 '_FUN' and, potentially, prepare another call to be used in a decltype
1057 return expression for a deduced return call op to allow for simple
1058 implementation of the conversion operator. */
1060 tree instance = cp_build_fold_indirect_ref (thisarg);
1061 tree objfn = build_min (COMPONENT_REF, NULL_TREE,
1062 instance, DECL_NAME (callop), NULL_TREE);
1063 int nargs = list_length (DECL_ARGUMENTS (callop)) - 1;
1065 call = prepare_op_call (objfn, nargs);
1066 if (type_uses_auto (fn_result))
1067 decltype_call = prepare_op_call (objfn, nargs);
1069 else
1071 direct_argvec = make_tree_vector ();
1072 direct_argvec->quick_push (thisarg);
1075 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1076 declare the static member function "_FUN" below. For each arg append to
1077 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1078 call args (for the template case). If a parameter pack is found, expand
1079 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1081 tree fn_args = NULL_TREE;
1083 int ix = 0;
1084 tree src = DECL_CHAIN (DECL_ARGUMENTS (callop));
1085 tree tgt = NULL;
1087 while (src)
1089 tree new_node = copy_node (src);
1091 if (!fn_args)
1092 fn_args = tgt = new_node;
1093 else
1095 TREE_CHAIN (tgt) = new_node;
1096 tgt = new_node;
1099 mark_exp_read (tgt);
1101 if (generic_lambda_p)
1103 /* Avoid capturing variables in this context. */
1104 ++cp_unevaluated_operand;
1105 tree a = forward_parm (tgt);
1106 --cp_unevaluated_operand;
1108 CALL_EXPR_ARG (call, ix) = a;
1109 if (decltype_call)
1110 CALL_EXPR_ARG (decltype_call, ix) = unshare_expr (a);
1112 if (PACK_EXPANSION_P (a))
1113 /* Set this after unsharing so it's not in decltype_call. */
1114 PACK_EXPANSION_LOCAL_P (a) = true;
1116 ++ix;
1118 else
1119 vec_safe_push (direct_argvec, tgt);
1121 src = TREE_CHAIN (src);
1125 if (generic_lambda_p)
1127 if (decltype_call)
1129 fn_result = finish_decltype_type
1130 (decltype_call, /*id_expression_or_member_access_p=*/false,
1131 tf_warning_or_error);
1134 else
1135 call = build_call_a (callop,
1136 direct_argvec->length (),
1137 direct_argvec->address ());
1139 CALL_FROM_THUNK_P (call) = 1;
1140 SET_EXPR_LOCATION (call, UNKNOWN_LOCATION);
1142 tree stattype = build_function_type (fn_result, FUNCTION_ARG_CHAIN (callop));
1143 stattype = (cp_build_type_attribute_variant
1144 (stattype, TYPE_ATTRIBUTES (optype)));
1145 if (flag_noexcept_type
1146 && TYPE_NOTHROW_P (TREE_TYPE (callop)))
1147 stattype = build_exception_variant (stattype, noexcept_true_spec);
1149 if (generic_lambda_p)
1150 --processing_template_decl;
1152 /* First build up the conversion op. */
1154 tree rettype = build_pointer_type (stattype);
1155 tree name = make_conv_op_name (rettype);
1156 tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1157 tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
1158 tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
1159 tree fn = convfn;
1160 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1161 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
1162 grokclassfn (type, fn, NO_SPECIAL);
1163 set_linkage_according_to_type (type, fn);
1164 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1165 DECL_IN_AGGR_P (fn) = 1;
1166 DECL_ARTIFICIAL (fn) = 1;
1167 DECL_NOT_REALLY_EXTERN (fn) = 1;
1168 DECL_DECLARED_INLINE_P (fn) = 1;
1169 DECL_ARGUMENTS (fn) = build_this_parm (fn, fntype, TYPE_QUAL_CONST);
1171 if (nested_def)
1172 DECL_INTERFACE_KNOWN (fn) = 1;
1174 if (generic_lambda_p)
1175 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1177 add_method (type, fn, false);
1179 /* Generic thunk code fails for varargs; we'll complain in mark_used if
1180 the conversion op is used. */
1181 if (varargs_function_p (callop))
1183 DECL_DELETED_FN (fn) = 1;
1184 return;
1187 /* Now build up the thunk to be returned. */
1189 name = get_identifier ("_FUN");
1190 tree statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
1191 fn = statfn;
1192 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1193 grokclassfn (type, fn, NO_SPECIAL);
1194 set_linkage_according_to_type (type, fn);
1195 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
1196 DECL_IN_AGGR_P (fn) = 1;
1197 DECL_ARTIFICIAL (fn) = 1;
1198 DECL_NOT_REALLY_EXTERN (fn) = 1;
1199 DECL_DECLARED_INLINE_P (fn) = 1;
1200 DECL_STATIC_FUNCTION_P (fn) = 1;
1201 DECL_ARGUMENTS (fn) = fn_args;
1202 for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
1204 /* Avoid duplicate -Wshadow warnings. */
1205 DECL_NAME (arg) = NULL_TREE;
1206 DECL_CONTEXT (arg) = fn;
1208 if (nested_def)
1209 DECL_INTERFACE_KNOWN (fn) = 1;
1211 if (generic_lambda_p)
1212 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1214 if (flag_sanitize & SANITIZE_NULL)
1215 /* Don't UBsan this function; we're deliberately calling op() with a null
1216 object argument. */
1217 add_no_sanitize_value (fn, SANITIZE_UNDEFINED);
1219 add_method (type, fn, false);
1221 if (nested)
1222 push_function_context ();
1223 else
1224 /* Still increment function_depth so that we don't GC in the
1225 middle of an expression. */
1226 ++function_depth;
1228 /* Generate the body of the thunk. */
1230 start_preparsed_function (statfn, NULL_TREE,
1231 SF_PRE_PARSED | SF_INCLASS_INLINE);
1232 if (DECL_ONE_ONLY (statfn))
1234 /* Put the thunk in the same comdat group as the call op. */
1235 cgraph_node::get_create (statfn)->add_to_same_comdat_group
1236 (cgraph_node::get_create (callop));
1238 tree body = begin_function_body ();
1239 tree compound_stmt = begin_compound_stmt (0);
1240 if (!generic_lambda_p)
1242 set_flags_from_callee (call);
1243 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1244 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1246 call = convert_from_reference (call);
1247 finish_return_stmt (call);
1249 finish_compound_stmt (compound_stmt);
1250 finish_function_body (body);
1252 fn = finish_function (/*inline_p=*/true);
1253 if (!generic_lambda_p)
1254 expand_or_defer_fn (fn);
1256 /* Generate the body of the conversion op. */
1258 start_preparsed_function (convfn, NULL_TREE,
1259 SF_PRE_PARSED | SF_INCLASS_INLINE);
1260 body = begin_function_body ();
1261 compound_stmt = begin_compound_stmt (0);
1263 /* decl_needed_p needs to see that it's used. */
1264 TREE_USED (statfn) = 1;
1265 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1267 finish_compound_stmt (compound_stmt);
1268 finish_function_body (body);
1270 fn = finish_function (/*inline_p=*/true);
1271 if (!generic_lambda_p)
1272 expand_or_defer_fn (fn);
1274 if (nested)
1275 pop_function_context ();
1276 else
1277 --function_depth;
1280 /* True if FN is the static function "_FUN" that gets returned from the lambda
1281 conversion operator. */
1283 bool
1284 lambda_static_thunk_p (tree fn)
1286 return (fn && TREE_CODE (fn) == FUNCTION_DECL
1287 && DECL_ARTIFICIAL (fn)
1288 && DECL_STATIC_FUNCTION_P (fn)
1289 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn)));
1292 /* Returns true iff VAL is a lambda-related declaration which should
1293 be ignored by unqualified lookup. */
1295 bool
1296 is_lambda_ignored_entity (tree val)
1298 /* Look past normal capture proxies. */
1299 if (is_normal_capture_proxy (val))
1300 return true;
1302 /* Always ignore lambda fields, their names are only for debugging. */
1303 if (TREE_CODE (val) == FIELD_DECL
1304 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1305 return true;
1307 /* None of the lookups that use qualify_lookup want the op() from the
1308 lambda; they want the one from the enclosing class. */
1309 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
1310 return true;
1312 return false;
1315 /* Lambdas that appear in variable initializer or default argument scope
1316 get that in their mangling, so we need to record it. We might as well
1317 use the count for function and namespace scopes as well. */
1318 static GTY(()) tree lambda_scope;
1319 static GTY(()) int lambda_count;
1320 struct GTY(()) tree_int
1322 tree t;
1323 int i;
1325 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
1327 void
1328 start_lambda_scope (tree decl)
1330 tree_int ti;
1331 gcc_assert (decl);
1332 /* Once we're inside a function, we ignore variable scope and just push
1333 the function again so that popping works properly. */
1334 if (current_function_decl && TREE_CODE (decl) == VAR_DECL)
1335 decl = current_function_decl;
1336 ti.t = lambda_scope;
1337 ti.i = lambda_count;
1338 vec_safe_push (lambda_scope_stack, ti);
1339 if (lambda_scope != decl)
1341 /* Don't reset the count if we're still in the same function. */
1342 lambda_scope = decl;
1343 lambda_count = 0;
1347 void
1348 record_lambda_scope (tree lambda)
1350 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
1351 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
1354 void
1355 finish_lambda_scope (void)
1357 tree_int *p = &lambda_scope_stack->last ();
1358 if (lambda_scope != p->t)
1360 lambda_scope = p->t;
1361 lambda_count = p->i;
1363 lambda_scope_stack->pop ();
1366 tree
1367 start_lambda_function (tree fco, tree lambda_expr)
1369 /* Let the front end know that we are going to be defining this
1370 function. */
1371 start_preparsed_function (fco,
1372 NULL_TREE,
1373 SF_PRE_PARSED | SF_INCLASS_INLINE);
1375 tree body = begin_function_body ();
1377 /* Push the proxies for any explicit captures. */
1378 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
1379 cap = TREE_CHAIN (cap))
1380 build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap));
1382 return body;
1385 void
1386 finish_lambda_function (tree body)
1388 finish_function_body (body);
1390 /* Finish the function and generate code for it if necessary. */
1391 tree fn = finish_function (/*inline_p=*/true);
1393 /* Only expand if the call op is not a template. */
1394 if (!DECL_TEMPLATE_INFO (fn))
1395 expand_or_defer_fn (fn);
1398 #include "gt-cp-lambda.h"