PR sanitizer/80403
[official-gcc.git] / gcc / cp / lambda.c
blob46ab30f0a76df8f14c68bb87a26ccc9f47de953c
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-2017 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"
33 #include "cp-cilkplus.h"
35 /* Constructor for a lambda expression. */
37 tree
38 build_lambda_expr (void)
40 tree lambda = make_node (LAMBDA_EXPR);
41 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
42 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
43 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
44 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
45 LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
46 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
47 return lambda;
50 /* Create the closure object for a LAMBDA_EXPR. */
52 tree
53 build_lambda_object (tree lambda_expr)
55 /* Build aggregate constructor call.
56 - cp_parser_braced_list
57 - cp_parser_functional_cast */
58 vec<constructor_elt, va_gc> *elts = NULL;
59 tree node, expr, type;
60 location_t saved_loc;
62 if (processing_template_decl)
63 return lambda_expr;
65 /* Make sure any error messages refer to the lambda-introducer. */
66 saved_loc = input_location;
67 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
69 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
70 node;
71 node = TREE_CHAIN (node))
73 tree field = TREE_PURPOSE (node);
74 tree val = TREE_VALUE (node);
76 if (field == error_mark_node)
78 expr = error_mark_node;
79 goto out;
82 if (TREE_CODE (val) == TREE_LIST)
83 val = build_x_compound_expr_from_list (val, ELK_INIT,
84 tf_warning_or_error);
86 if (DECL_P (val))
87 mark_used (val);
89 /* Mere mortals can't copy arrays with aggregate initialization, so
90 do some magic to make it work here. */
91 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
92 val = build_array_copy (val);
93 else if (DECL_NORMAL_CAPTURE_P (field)
94 && !DECL_VLA_CAPTURE_P (field)
95 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
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."
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 val = force_rvalue (val, tf_warning_or_error);
105 if (TREE_CODE (val) == TARGET_EXPR)
106 TARGET_EXPR_DIRECT_INIT_P (val) = true;
109 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
112 expr = build_constructor (init_list_type_node, elts);
113 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
115 /* N2927: "[The closure] class type is not an aggregate."
116 But we briefly treat it as an aggregate to make this simpler. */
117 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
118 CLASSTYPE_NON_AGGREGATE (type) = 0;
119 expr = finish_compound_literal (type, expr, tf_warning_or_error);
120 CLASSTYPE_NON_AGGREGATE (type) = 1;
122 out:
123 input_location = saved_loc;
124 return expr;
127 /* Return an initialized RECORD_TYPE for LAMBDA.
128 LAMBDA must have its explicit captures already. */
130 tree
131 begin_lambda_type (tree lambda)
133 tree type;
136 /* Unique name. This is just like an unnamed class, but we cannot use
137 make_anon_name because of certain checks against TYPE_UNNAMED_P. */
138 tree name;
139 name = make_lambda_name ();
141 /* Create the new RECORD_TYPE for this lambda. */
142 type = xref_tag (/*tag_code=*/record_type,
143 name,
144 /*scope=*/ts_lambda,
145 /*template_header_p=*/false);
146 if (type == error_mark_node)
147 return error_mark_node;
150 /* Designate it as a struct so that we can use aggregate initialization. */
151 CLASSTYPE_DECLARED_CLASS (type) = false;
153 /* Cross-reference the expression and the type. */
154 LAMBDA_EXPR_CLOSURE (lambda) = type;
155 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
157 /* In C++17, assume the closure is literal; we'll clear the flag later if
158 necessary. */
159 if (cxx_dialect >= cxx1z)
160 CLASSTYPE_LITERAL_P (type) = true;
162 /* Clear base types. */
163 xref_basetypes (type, /*bases=*/NULL_TREE);
165 /* Start the class. */
166 type = begin_class_definition (type);
168 return type;
171 /* Returns the type to use for the return type of the operator() of a
172 closure class. */
174 tree
175 lambda_return_type (tree expr)
177 if (expr == NULL_TREE)
178 return void_type_node;
179 if (type_unknown_p (expr)
180 || BRACE_ENCLOSED_INITIALIZER_P (expr))
182 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
183 return error_mark_node;
185 gcc_checking_assert (!type_dependent_expression_p (expr));
186 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
189 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
190 closure type. */
192 tree
193 lambda_function (tree lambda)
195 tree type;
196 if (TREE_CODE (lambda) == LAMBDA_EXPR)
197 type = LAMBDA_EXPR_CLOSURE (lambda);
198 else
199 type = lambda;
200 gcc_assert (LAMBDA_TYPE_P (type));
201 /* Don't let debug_tree cause instantiation. */
202 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
203 && !COMPLETE_OR_OPEN_TYPE_P (type))
204 return NULL_TREE;
205 lambda = lookup_member (type, cp_operator_id (CALL_EXPR),
206 /*protect=*/0, /*want_type=*/false,
207 tf_warning_or_error);
208 if (lambda)
209 lambda = STRIP_TEMPLATE (get_first_fn (lambda));
210 return lambda;
213 /* Returns the type to use for the FIELD_DECL corresponding to the
214 capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
215 C++14 init capture, and BY_REFERENCE_P indicates whether we're
216 capturing by reference. */
218 tree
219 lambda_capture_field_type (tree expr, bool explicit_init_p,
220 bool by_reference_p)
222 tree type;
223 bool is_this = is_this_parameter (tree_strip_nop_conversions (expr));
225 if (!is_this && type_dependent_expression_p (expr))
227 type = cxx_make_type (DECLTYPE_TYPE);
228 DECLTYPE_TYPE_EXPR (type) = expr;
229 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
230 DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p;
231 DECLTYPE_FOR_REF_CAPTURE (type) = by_reference_p;
232 SET_TYPE_STRUCTURAL_EQUALITY (type);
234 else if (!is_this && explicit_init_p)
236 tree auto_node = make_auto ();
238 type = auto_node;
239 if (by_reference_p)
240 /* Add the reference now, so deduction doesn't lose
241 outermost CV qualifiers of EXPR. */
242 type = build_reference_type (type);
243 type = do_auto_deduction (type, expr, auto_node);
245 else
247 type = non_reference (unlowered_expr_type (expr));
249 if (!is_this && by_reference_p)
250 type = build_reference_type (type);
253 return type;
256 /* Returns true iff DECL is a lambda capture proxy variable created by
257 build_capture_proxy. */
259 bool
260 is_capture_proxy (tree decl)
262 return (VAR_P (decl)
263 && DECL_HAS_VALUE_EXPR_P (decl)
264 && !DECL_ANON_UNION_VAR_P (decl)
265 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
268 /* Returns true iff DECL is a capture proxy for a normal capture
269 (i.e. without explicit initializer). */
271 bool
272 is_normal_capture_proxy (tree decl)
274 if (!is_capture_proxy (decl))
275 /* It's not a capture proxy. */
276 return false;
278 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
279 /* VLA capture. */
280 return true;
282 /* It is a capture proxy, is it a normal capture? */
283 tree val = DECL_VALUE_EXPR (decl);
284 if (val == error_mark_node)
285 return true;
287 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
288 val = TREE_OPERAND (val, 1);
289 return DECL_NORMAL_CAPTURE_P (val);
292 /* VAR is a capture proxy created by build_capture_proxy; add it to the
293 current function, which is the operator() for the appropriate lambda. */
295 void
296 insert_capture_proxy (tree var)
298 cp_binding_level *b;
299 tree stmt_list;
301 /* Put the capture proxy in the extra body block so that it won't clash
302 with a later local variable. */
303 b = current_binding_level;
304 for (;;)
306 cp_binding_level *n = b->level_chain;
307 if (n->kind == sk_function_parms)
308 break;
309 b = n;
311 pushdecl_with_scope (var, b, false);
313 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
314 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
315 stmt_list = (*stmt_list_stack)[1];
316 gcc_assert (stmt_list);
317 append_to_statement_list_force (var, &stmt_list);
320 /* We've just finished processing a lambda; if the containing scope is also
321 a lambda, insert any capture proxies that were created while processing
322 the nested lambda. */
324 void
325 insert_pending_capture_proxies (void)
327 tree lam;
328 vec<tree, va_gc> *proxies;
329 unsigned i;
331 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
332 return;
334 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
335 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
336 for (i = 0; i < vec_safe_length (proxies); ++i)
338 tree var = (*proxies)[i];
339 insert_capture_proxy (var);
341 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
342 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
345 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
346 return the type we want the proxy to have: the type of the field itself,
347 with added const-qualification if the lambda isn't mutable and the
348 capture is by value. */
350 tree
351 lambda_proxy_type (tree ref)
353 tree type;
354 if (ref == error_mark_node)
355 return error_mark_node;
356 if (REFERENCE_REF_P (ref))
357 ref = TREE_OPERAND (ref, 0);
358 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
359 type = TREE_TYPE (ref);
360 if (!type || WILDCARD_TYPE_P (non_reference (type)))
362 type = cxx_make_type (DECLTYPE_TYPE);
363 DECLTYPE_TYPE_EXPR (type) = ref;
364 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
365 SET_TYPE_STRUCTURAL_EQUALITY (type);
367 if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
368 type = make_pack_expansion (type);
369 return type;
372 /* MEMBER is a capture field in a lambda closure class. Now that we're
373 inside the operator(), build a placeholder var for future lookups and
374 debugging. */
376 tree
377 build_capture_proxy (tree member)
379 tree var, object, fn, closure, name, lam, type;
381 if (PACK_EXPANSION_P (member))
382 member = PACK_EXPANSION_PATTERN (member);
384 closure = DECL_CONTEXT (member);
385 fn = lambda_function (closure);
386 lam = CLASSTYPE_LAMBDA_EXPR (closure);
388 /* The proxy variable forwards to the capture field. */
389 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
390 object = finish_non_static_data_member (member, object, NULL_TREE);
391 if (REFERENCE_REF_P (object))
392 object = TREE_OPERAND (object, 0);
394 /* Remove the __ inserted by add_capture. */
395 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
397 type = lambda_proxy_type (object);
399 if (name == this_identifier && !POINTER_TYPE_P (type))
401 type = build_pointer_type (type);
402 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
403 object = build_fold_addr_expr_with_type (object, type);
406 if (DECL_VLA_CAPTURE_P (member))
408 /* Rebuild the VLA type from the pointer and maxindex. */
409 tree field = next_initializable_field (TYPE_FIELDS (type));
410 tree ptr = build_simple_component_ref (object, field);
411 field = next_initializable_field (DECL_CHAIN (field));
412 tree max = build_simple_component_ref (object, field);
413 type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
414 build_index_type (max));
415 type = build_reference_type (type);
416 REFERENCE_VLA_OK (type) = true;
417 object = convert (type, ptr);
420 var = build_decl (input_location, VAR_DECL, name, type);
421 SET_DECL_VALUE_EXPR (var, object);
422 DECL_HAS_VALUE_EXPR_P (var) = 1;
423 DECL_ARTIFICIAL (var) = 1;
424 TREE_USED (var) = 1;
425 DECL_CONTEXT (var) = fn;
427 if (name == this_identifier)
429 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
430 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
433 if (fn == current_function_decl)
434 insert_capture_proxy (var);
435 else
436 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
438 return var;
441 /* Return a struct containing a pointer and a length for lambda capture of
442 an array of runtime length. */
444 static tree
445 vla_capture_type (tree array_type)
447 static tree ptr_id, max_id;
448 tree type = xref_tag (record_type, make_anon_name (), ts_current, false);
449 xref_basetypes (type, NULL_TREE);
450 type = begin_class_definition (type);
451 if (!ptr_id)
453 ptr_id = get_identifier ("ptr");
454 max_id = get_identifier ("max");
456 tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
457 tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
458 finish_member_declaration (field);
459 field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
460 finish_member_declaration (field);
461 return finish_struct (type, NULL_TREE);
464 /* From an ID and INITIALIZER, create a capture (by reference if
465 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
466 and return it. If ID is `this', BY_REFERENCE_P says whether
467 `*this' is captured by reference. */
469 tree
470 add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
471 bool explicit_init_p)
473 char *buf;
474 tree type, member, name;
475 bool vla = false;
476 bool variadic = false;
477 tree initializer = orig_init;
479 if (PACK_EXPANSION_P (initializer))
481 initializer = PACK_EXPANSION_PATTERN (initializer);
482 variadic = true;
485 if (TREE_CODE (initializer) == TREE_LIST
486 /* A pack expansion might end up with multiple elements. */
487 && !PACK_EXPANSION_P (TREE_VALUE (initializer)))
488 initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
489 tf_warning_or_error);
490 type = TREE_TYPE (initializer);
491 if (type == error_mark_node)
492 return error_mark_node;
494 if (array_of_runtime_bound_p (type))
496 vla = true;
497 if (!by_reference_p)
498 error ("array of runtime bound cannot be captured by copy, "
499 "only by reference");
501 /* For a VLA, we capture the address of the first element and the
502 maximum index, and then reconstruct the VLA for the proxy. */
503 tree elt = cp_build_array_ref (input_location, initializer,
504 integer_zero_node, tf_warning_or_error);
505 initializer = build_constructor_va (init_list_type_node, 2,
506 NULL_TREE, build_address (elt),
507 NULL_TREE, array_type_nelts (type));
508 type = vla_capture_type (type);
510 else if (!dependent_type_p (type)
511 && variably_modified_type_p (type, NULL_TREE))
513 error ("capture of variable-size type %qT that is not an N3639 array "
514 "of runtime bound", type);
515 if (TREE_CODE (type) == ARRAY_TYPE
516 && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
517 inform (input_location, "because the array element type %qT has "
518 "variable size", TREE_TYPE (type));
519 type = error_mark_node;
521 else
523 type = lambda_capture_field_type (initializer, explicit_init_p,
524 by_reference_p);
525 if (type == error_mark_node)
526 return error_mark_node;
528 if (id == this_identifier && !by_reference_p)
530 gcc_assert (POINTER_TYPE_P (type));
531 type = TREE_TYPE (type);
532 initializer = cp_build_indirect_ref (initializer, RO_NULL,
533 tf_warning_or_error);
536 if (dependent_type_p (type))
538 else if (id != this_identifier && by_reference_p)
540 if (!lvalue_p (initializer))
541 error ("cannot capture %qE by reference", initializer);
543 else
545 /* Capture by copy requires a complete type. */
546 type = complete_type (type);
547 if (!COMPLETE_TYPE_P (type))
549 error ("capture by copy of incomplete type %qT", type);
550 cxx_incomplete_type_inform (type);
551 return error_mark_node;
556 /* Add __ to the beginning of the field name so that user code
557 won't find the field with name lookup. We can't just leave the name
558 unset because template instantiation uses the name to find
559 instantiated fields. */
560 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
561 buf[1] = buf[0] = '_';
562 memcpy (buf + 2, IDENTIFIER_POINTER (id),
563 IDENTIFIER_LENGTH (id) + 1);
564 name = get_identifier (buf);
566 /* If TREE_TYPE isn't set, we're still in the introducer, so check
567 for duplicates. */
568 if (!LAMBDA_EXPR_CLOSURE (lambda))
570 if (IDENTIFIER_MARKED (name))
572 pedwarn (input_location, 0,
573 "already captured %qD in lambda expression", id);
574 return NULL_TREE;
576 IDENTIFIER_MARKED (name) = true;
579 if (variadic)
580 type = make_pack_expansion (type);
582 /* Make member variable. */
583 member = build_decl (input_location, FIELD_DECL, name, type);
584 DECL_VLA_CAPTURE_P (member) = vla;
586 if (!explicit_init_p)
587 /* Normal captures are invisible to name lookup but uses are replaced
588 with references to the capture field; we implement this by only
589 really making them invisible in unevaluated context; see
590 qualify_lookup. For now, let's make explicitly initialized captures
591 always visible. */
592 DECL_NORMAL_CAPTURE_P (member) = true;
594 if (id == this_identifier)
595 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
597 /* Add it to the appropriate closure class if we've started it. */
598 if (current_class_type
599 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
600 finish_member_declaration (member);
602 tree listmem = member;
603 if (variadic)
605 listmem = make_pack_expansion (member);
606 initializer = orig_init;
608 LAMBDA_EXPR_CAPTURE_LIST (lambda)
609 = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
611 if (LAMBDA_EXPR_CLOSURE (lambda))
612 return build_capture_proxy (member);
613 /* For explicit captures we haven't started the function yet, so we wait
614 and build the proxy from cp_parser_lambda_body. */
615 return NULL_TREE;
618 /* Register all the capture members on the list CAPTURES, which is the
619 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
621 void
622 register_capture_members (tree captures)
624 if (captures == NULL_TREE)
625 return;
627 register_capture_members (TREE_CHAIN (captures));
629 tree field = TREE_PURPOSE (captures);
630 if (PACK_EXPANSION_P (field))
631 field = PACK_EXPANSION_PATTERN (field);
633 /* We set this in add_capture to avoid duplicates. */
634 IDENTIFIER_MARKED (DECL_NAME (field)) = false;
635 finish_member_declaration (field);
638 /* Similar to add_capture, except this works on a stack of nested lambdas.
639 BY_REFERENCE_P in this case is derived from the default capture mode.
640 Returns the capture for the lambda at the bottom of the stack. */
642 tree
643 add_default_capture (tree lambda_stack, tree id, tree initializer)
645 bool this_capture_p = (id == this_identifier);
647 tree var = NULL_TREE;
649 tree saved_class_type = current_class_type;
651 tree node;
653 for (node = lambda_stack;
654 node;
655 node = TREE_CHAIN (node))
657 tree lambda = TREE_VALUE (node);
659 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
660 if (DECL_PACK_P (initializer))
661 initializer = make_pack_expansion (initializer);
662 var = add_capture (lambda,
664 initializer,
665 /*by_reference_p=*/
666 (this_capture_p
667 || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
668 == CPLD_REFERENCE)),
669 /*explicit_init_p=*/false);
670 initializer = convert_from_reference (var);
673 current_class_type = saved_class_type;
675 return var;
678 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
679 form of an INDIRECT_REF, possibly adding it through default
680 capturing, if ADD_CAPTURE_P is true. */
682 tree
683 lambda_expr_this_capture (tree lambda, bool add_capture_p)
685 tree result;
687 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
689 /* In unevaluated context this isn't an odr-use, so don't capture. */
690 if (cp_unevaluated_operand)
691 add_capture_p = false;
693 /* Try to default capture 'this' if we can. */
694 if (!this_capture
695 && (!add_capture_p
696 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE))
698 tree lambda_stack = NULL_TREE;
699 tree init = NULL_TREE;
701 /* If we are in a lambda function, we can move out until we hit:
702 1. a non-lambda function or NSDMI,
703 2. a lambda function capturing 'this', or
704 3. a non-default capturing lambda function. */
705 for (tree tlambda = lambda; ;)
707 lambda_stack = tree_cons (NULL_TREE,
708 tlambda,
709 lambda_stack);
711 if (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)
712 && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)) == FIELD_DECL)
714 /* In an NSDMI, we don't have a function to look up the decl in,
715 but the fake 'this' pointer that we're using for parsing is
716 in scope_chain. */
717 init = scope_chain->x_current_class_ptr;
718 gcc_checking_assert
719 (init && (TREE_TYPE (TREE_TYPE (init))
720 == current_nonlambda_class_type ()));
721 break;
724 tree closure_decl = TYPE_NAME (LAMBDA_EXPR_CLOSURE (tlambda));
725 tree containing_function = decl_function_context (closure_decl);
727 if (containing_function == NULL_TREE)
728 /* We ran out of scopes; there's no 'this' to capture. */
729 break;
731 if (!LAMBDA_FUNCTION_P (containing_function))
733 /* We found a non-lambda function. */
734 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function))
735 /* First parameter is 'this'. */
736 init = DECL_ARGUMENTS (containing_function);
737 break;
740 tlambda
741 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
743 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
745 /* An outer lambda has already captured 'this'. */
746 init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
747 break;
750 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
751 /* An outer lambda won't let us capture 'this'. */
752 break;
755 if (init)
757 if (add_capture_p)
758 this_capture = add_default_capture (lambda_stack,
759 /*id=*/this_identifier,
760 init);
761 else
762 this_capture = init;
766 if (cp_unevaluated_operand)
767 result = this_capture;
768 else if (!this_capture)
770 if (add_capture_p)
772 error ("%<this%> was not captured for this lambda function");
773 result = error_mark_node;
775 else
776 result = NULL_TREE;
778 else
780 /* To make sure that current_class_ref is for the lambda. */
781 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
782 == LAMBDA_EXPR_CLOSURE (lambda));
784 result = this_capture;
786 /* If 'this' is captured, each use of 'this' is transformed into an
787 access to the corresponding unnamed data member of the closure
788 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
789 ensures that the transformed expression is an rvalue. ] */
790 result = rvalue (result);
793 return result;
796 /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
797 object. NULL otherwise.. */
799 static tree
800 resolvable_dummy_lambda (tree object)
802 if (!is_dummy_object (object))
803 return NULL_TREE;
805 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
806 gcc_assert (!TYPE_PTR_P (type));
808 if (type != current_class_type
809 && current_class_type
810 && LAMBDA_TYPE_P (current_class_type)
811 && lambda_function (current_class_type)
812 && DERIVED_FROM_P (type, current_nonlambda_class_type ()))
813 return CLASSTYPE_LAMBDA_EXPR (current_class_type);
815 return NULL_TREE;
818 /* We don't want to capture 'this' until we know we need it, i.e. after
819 overload resolution has chosen a non-static member function. At that
820 point we call this function to turn a dummy object into a use of the
821 'this' capture. */
823 tree
824 maybe_resolve_dummy (tree object, bool add_capture_p)
826 if (tree lam = resolvable_dummy_lambda (object))
827 if (tree cap = lambda_expr_this_capture (lam, add_capture_p))
828 if (cap != error_mark_node)
829 object = build_x_indirect_ref (EXPR_LOCATION (object), cap,
830 RO_NULL, tf_warning_or_error);
832 return object;
835 /* When parsing a generic lambda containing an argument-dependent
836 member function call we defer overload resolution to instantiation
837 time. But we have to know now whether to capture this or not.
838 Do that if FNS contains any non-static fns.
839 The std doesn't anticipate this case, but I expect this to be the
840 outcome of discussion. */
842 void
843 maybe_generic_this_capture (tree object, tree fns)
845 if (tree lam = resolvable_dummy_lambda (object))
846 if (!LAMBDA_EXPR_THIS_CAPTURE (lam))
848 /* We've not yet captured, so look at the function set of
849 interest. */
850 if (BASELINK_P (fns))
851 fns = BASELINK_FUNCTIONS (fns);
852 bool id_expr = TREE_CODE (fns) == TEMPLATE_ID_EXPR;
853 if (id_expr)
854 fns = TREE_OPERAND (fns, 0);
855 for (; fns; fns = OVL_NEXT (fns))
857 tree fn = OVL_CURRENT (fns);
859 if ((!id_expr || TREE_CODE (fn) == TEMPLATE_DECL)
860 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
862 /* Found a non-static member. Capture this. */
863 lambda_expr_this_capture (lam, true);
864 break;
870 /* Returns the innermost non-lambda function. */
872 tree
873 current_nonlambda_function (void)
875 tree fn = current_function_decl;
876 while (fn && LAMBDA_FUNCTION_P (fn))
877 fn = decl_function_context (fn);
878 return fn;
881 /* Returns the method basetype of the innermost non-lambda function, or
882 NULL_TREE if none. */
884 tree
885 nonlambda_method_basetype (void)
887 tree fn, type;
888 if (!current_class_ref)
889 return NULL_TREE;
891 type = current_class_type;
892 if (!LAMBDA_TYPE_P (type))
893 return type;
895 /* Find the nearest enclosing non-lambda function. */
896 fn = TYPE_NAME (type);
898 fn = decl_function_context (fn);
899 while (fn && LAMBDA_FUNCTION_P (fn));
901 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
902 return NULL_TREE;
904 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
907 /* Like current_scope, but looking through lambdas. */
909 tree
910 current_nonlambda_scope (void)
912 tree scope = current_scope ();
913 for (;;)
915 if (TREE_CODE (scope) == FUNCTION_DECL
916 && LAMBDA_FUNCTION_P (scope))
918 scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope));
919 continue;
921 else if (LAMBDA_TYPE_P (scope))
923 scope = CP_TYPE_CONTEXT (scope);
924 continue;
926 break;
928 return scope;
931 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
932 indicated FN and NARGS, but do not initialize the return type or any of the
933 argument slots. */
935 static tree
936 prepare_op_call (tree fn, int nargs)
938 tree t;
940 t = build_vl_exp (CALL_EXPR, nargs + 3);
941 CALL_EXPR_FN (t) = fn;
942 CALL_EXPR_STATIC_CHAIN (t) = NULL;
944 return t;
947 /* Return true iff CALLOP is the op() for a generic lambda. */
949 bool
950 generic_lambda_fn_p (tree callop)
952 return (LAMBDA_FUNCTION_P (callop)
953 && DECL_TEMPLATE_INFO (callop)
954 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
957 /* If the closure TYPE has a static op(), also add a conversion to function
958 pointer. */
960 void
961 maybe_add_lambda_conv_op (tree type)
963 bool nested = (cfun != NULL);
964 bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
965 tree callop = lambda_function (type);
966 tree lam = CLASSTYPE_LAMBDA_EXPR (type);
968 if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE
969 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE)
970 return;
972 if (processing_template_decl)
973 return;
975 bool const generic_lambda_p = generic_lambda_fn_p (callop);
977 if (!generic_lambda_p && DECL_INITIAL (callop) == NULL_TREE)
979 /* If the op() wasn't instantiated due to errors, give up. */
980 gcc_assert (errorcount || sorrycount);
981 return;
984 /* Non-template conversion operators are defined directly with build_call_a
985 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
986 deferred and the CALL is built in-place. In the case of a deduced return
987 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
988 the return type is also built in-place. The arguments of DECLTYPE_CALL in
989 the return expression may differ in flags from those in the body CALL. In
990 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
991 the body CALL, but not in DECLTYPE_CALL. */
993 vec<tree, va_gc> *direct_argvec = 0;
994 tree decltype_call = 0, call = 0;
995 tree optype = TREE_TYPE (callop);
996 tree fn_result = TREE_TYPE (optype);
998 tree thisarg = build_nop (TREE_TYPE (DECL_ARGUMENTS (callop)),
999 null_pointer_node);
1000 if (generic_lambda_p)
1002 /* Prepare the dependent member call for the static member function
1003 '_FUN' and, potentially, prepare another call to be used in a decltype
1004 return expression for a deduced return call op to allow for simple
1005 implementation of the conversion operator. */
1007 tree instance = cp_build_indirect_ref (thisarg, RO_NULL,
1008 tf_warning_or_error);
1009 tree objfn = build_min (COMPONENT_REF, NULL_TREE,
1010 instance, DECL_NAME (callop), NULL_TREE);
1011 int nargs = list_length (DECL_ARGUMENTS (callop)) - 1;
1013 call = prepare_op_call (objfn, nargs);
1014 if (type_uses_auto (fn_result))
1015 decltype_call = prepare_op_call (objfn, nargs);
1017 else
1019 direct_argvec = make_tree_vector ();
1020 direct_argvec->quick_push (thisarg);
1023 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
1024 declare the static member function "_FUN" below. For each arg append to
1025 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
1026 call args (for the template case). If a parameter pack is found, expand
1027 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
1029 tree fn_args = NULL_TREE;
1031 int ix = 0;
1032 tree src = DECL_CHAIN (DECL_ARGUMENTS (callop));
1033 tree tgt = NULL;
1035 while (src)
1037 tree new_node = copy_node (src);
1039 if (!fn_args)
1040 fn_args = tgt = new_node;
1041 else
1043 TREE_CHAIN (tgt) = new_node;
1044 tgt = new_node;
1047 mark_exp_read (tgt);
1049 if (generic_lambda_p)
1051 ++processing_template_decl;
1052 tree a = forward_parm (tgt);
1053 --processing_template_decl;
1055 CALL_EXPR_ARG (call, ix) = a;
1056 if (decltype_call)
1057 CALL_EXPR_ARG (decltype_call, ix) = unshare_expr (a);
1059 if (PACK_EXPANSION_P (a))
1060 /* Set this after unsharing so it's not in decltype_call. */
1061 PACK_EXPANSION_LOCAL_P (a) = true;
1063 ++ix;
1065 else
1066 vec_safe_push (direct_argvec, tgt);
1068 src = TREE_CHAIN (src);
1073 if (generic_lambda_p)
1075 if (decltype_call)
1077 ++processing_template_decl;
1078 fn_result = finish_decltype_type
1079 (decltype_call, /*id_expression_or_member_access_p=*/false,
1080 tf_warning_or_error);
1081 --processing_template_decl;
1084 else
1085 call = build_call_a (callop,
1086 direct_argvec->length (),
1087 direct_argvec->address ());
1089 CALL_FROM_THUNK_P (call) = 1;
1090 SET_EXPR_LOCATION (call, UNKNOWN_LOCATION);
1092 tree stattype = build_function_type (fn_result, FUNCTION_ARG_CHAIN (callop));
1093 stattype = (cp_build_type_attribute_variant
1094 (stattype, TYPE_ATTRIBUTES (optype)));
1095 if (flag_noexcept_type
1096 && TYPE_NOTHROW_P (TREE_TYPE (callop)))
1097 stattype = build_exception_variant (stattype, noexcept_true_spec);
1099 /* First build up the conversion op. */
1101 tree rettype = build_pointer_type (stattype);
1102 tree name = mangle_conv_op_name_for_type (rettype);
1103 tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1104 tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
1105 tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
1106 tree fn = convfn;
1107 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1108 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
1109 SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
1110 grokclassfn (type, fn, NO_SPECIAL);
1111 set_linkage_according_to_type (type, fn);
1112 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1113 DECL_IN_AGGR_P (fn) = 1;
1114 DECL_ARTIFICIAL (fn) = 1;
1115 DECL_NOT_REALLY_EXTERN (fn) = 1;
1116 DECL_DECLARED_INLINE_P (fn) = 1;
1117 DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
1118 if (nested_def)
1119 DECL_INTERFACE_KNOWN (fn) = 1;
1121 if (generic_lambda_p)
1122 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1124 add_method (type, fn, NULL_TREE);
1126 /* Generic thunk code fails for varargs; we'll complain in mark_used if
1127 the conversion op is used. */
1128 if (varargs_function_p (callop))
1130 DECL_DELETED_FN (fn) = 1;
1131 return;
1134 /* Now build up the thunk to be returned. */
1136 name = get_identifier ("_FUN");
1137 tree statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
1138 fn = statfn;
1139 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1140 grokclassfn (type, fn, NO_SPECIAL);
1141 set_linkage_according_to_type (type, fn);
1142 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1143 DECL_IN_AGGR_P (fn) = 1;
1144 DECL_ARTIFICIAL (fn) = 1;
1145 DECL_NOT_REALLY_EXTERN (fn) = 1;
1146 DECL_DECLARED_INLINE_P (fn) = 1;
1147 DECL_STATIC_FUNCTION_P (fn) = 1;
1148 DECL_ARGUMENTS (fn) = fn_args;
1149 for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
1151 /* Avoid duplicate -Wshadow warnings. */
1152 DECL_NAME (arg) = NULL_TREE;
1153 DECL_CONTEXT (arg) = fn;
1155 if (nested_def)
1156 DECL_INTERFACE_KNOWN (fn) = 1;
1158 if (generic_lambda_p)
1159 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1161 if (flag_sanitize & SANITIZE_NULL)
1163 /* Don't UBsan this function; we're deliberately calling op() with a null
1164 object argument. */
1165 tree attrs = build_tree_list (get_identifier ("no_sanitize_undefined"),
1166 NULL_TREE);
1167 cplus_decl_attributes (&fn, attrs, 0);
1170 add_method (type, fn, NULL_TREE);
1172 if (nested)
1173 push_function_context ();
1174 else
1175 /* Still increment function_depth so that we don't GC in the
1176 middle of an expression. */
1177 ++function_depth;
1179 /* Generate the body of the thunk. */
1181 start_preparsed_function (statfn, NULL_TREE,
1182 SF_PRE_PARSED | SF_INCLASS_INLINE);
1183 if (DECL_ONE_ONLY (statfn))
1185 /* Put the thunk in the same comdat group as the call op. */
1186 cgraph_node::get_create (statfn)->add_to_same_comdat_group
1187 (cgraph_node::get_create (callop));
1189 tree body = begin_function_body ();
1190 tree compound_stmt = begin_compound_stmt (0);
1191 if (!generic_lambda_p)
1193 set_flags_from_callee (call);
1194 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1195 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1197 call = convert_from_reference (call);
1198 finish_return_stmt (call);
1200 finish_compound_stmt (compound_stmt);
1201 finish_function_body (body);
1203 fn = finish_function (/*inline*/2);
1204 if (!generic_lambda_p)
1205 expand_or_defer_fn (fn);
1207 /* Generate the body of the conversion op. */
1209 start_preparsed_function (convfn, NULL_TREE,
1210 SF_PRE_PARSED | SF_INCLASS_INLINE);
1211 body = begin_function_body ();
1212 compound_stmt = begin_compound_stmt (0);
1214 /* decl_needed_p needs to see that it's used. */
1215 TREE_USED (statfn) = 1;
1216 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1218 finish_compound_stmt (compound_stmt);
1219 finish_function_body (body);
1221 fn = finish_function (/*inline*/2);
1222 if (!generic_lambda_p)
1223 expand_or_defer_fn (fn);
1225 if (nested)
1226 pop_function_context ();
1227 else
1228 --function_depth;
1231 /* True if FN is the static function "_FUN" that gets returned from the lambda
1232 conversion operator. */
1234 bool
1235 lambda_static_thunk_p (tree fn)
1237 return (fn && TREE_CODE (fn) == FUNCTION_DECL
1238 && DECL_ARTIFICIAL (fn)
1239 && DECL_STATIC_FUNCTION_P (fn)
1240 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn)));
1243 /* Returns true iff VAL is a lambda-related declaration which should
1244 be ignored by unqualified lookup. */
1246 bool
1247 is_lambda_ignored_entity (tree val)
1249 /* In unevaluated context, look past normal capture proxies. */
1250 if (cp_unevaluated_operand && is_normal_capture_proxy (val))
1251 return true;
1253 /* Always ignore lambda fields, their names are only for debugging. */
1254 if (TREE_CODE (val) == FIELD_DECL
1255 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1256 return true;
1258 /* None of the lookups that use qualify_lookup want the op() from the
1259 lambda; they want the one from the enclosing class. */
1260 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
1261 return true;
1263 return false;