PR target/66563
[official-gcc.git] / gcc / cp / lambda.c
blob0ac96afef31a8de04a8ef77235d9e6c2e94ed5a9
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-2015 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 "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "plugin-api.h"
33 #include "tm.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "ipa-ref.h"
37 #include "cgraph.h"
38 #include "tree-iterator.h"
39 #include "cp-tree.h"
40 #include "toplev.h"
42 /* Constructor for a lambda expression. */
44 tree
45 build_lambda_expr (void)
47 tree lambda = make_node (LAMBDA_EXPR);
48 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
49 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
50 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
51 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
52 LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
53 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
54 return lambda;
57 /* Create the closure object for a LAMBDA_EXPR. */
59 tree
60 build_lambda_object (tree lambda_expr)
62 /* Build aggregate constructor call.
63 - cp_parser_braced_list
64 - cp_parser_functional_cast */
65 vec<constructor_elt, va_gc> *elts = NULL;
66 tree node, expr, type;
67 location_t saved_loc;
69 if (processing_template_decl)
70 return lambda_expr;
72 /* Make sure any error messages refer to the lambda-introducer. */
73 saved_loc = input_location;
74 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
76 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
77 node;
78 node = TREE_CHAIN (node))
80 tree field = TREE_PURPOSE (node);
81 tree val = TREE_VALUE (node);
83 if (field == error_mark_node)
85 expr = error_mark_node;
86 goto out;
89 if (DECL_P (val))
90 mark_used (val);
92 /* Mere mortals can't copy arrays with aggregate initialization, so
93 do some magic to make it work here. */
94 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
95 val = build_array_copy (val);
96 else if (DECL_NORMAL_CAPTURE_P (field)
97 && !DECL_VLA_CAPTURE_P (field)
98 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
100 /* "the entities that are captured by copy are used to
101 direct-initialize each corresponding non-static data
102 member of the resulting closure object."
104 There's normally no way to express direct-initialization
105 from an element of a CONSTRUCTOR, so we build up a special
106 TARGET_EXPR to bypass the usual copy-initialization. */
107 val = force_rvalue (val, tf_warning_or_error);
108 if (TREE_CODE (val) == TARGET_EXPR)
109 TARGET_EXPR_DIRECT_INIT_P (val) = true;
112 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
115 expr = build_constructor (init_list_type_node, elts);
116 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
118 /* N2927: "[The closure] class type is not an aggregate."
119 But we briefly treat it as an aggregate to make this simpler. */
120 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
121 CLASSTYPE_NON_AGGREGATE (type) = 0;
122 expr = finish_compound_literal (type, expr, tf_warning_or_error);
123 CLASSTYPE_NON_AGGREGATE (type) = 1;
125 out:
126 input_location = saved_loc;
127 return expr;
130 /* Return an initialized RECORD_TYPE for LAMBDA.
131 LAMBDA must have its explicit captures already. */
133 tree
134 begin_lambda_type (tree lambda)
136 tree type;
139 /* Unique name. This is just like an unnamed class, but we cannot use
140 make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */
141 tree name;
142 name = make_lambda_name ();
144 /* Create the new RECORD_TYPE for this lambda. */
145 type = xref_tag (/*tag_code=*/record_type,
146 name,
147 /*scope=*/ts_lambda,
148 /*template_header_p=*/false);
149 if (type == error_mark_node)
150 return error_mark_node;
153 /* Designate it as a struct so that we can use aggregate initialization. */
154 CLASSTYPE_DECLARED_CLASS (type) = false;
156 /* Cross-reference the expression and the type. */
157 LAMBDA_EXPR_CLOSURE (lambda) = type;
158 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
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 void_type_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, ansi_opname (CALL_EXPR),
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.
213 The caller should add REFERENCE_TYPE for capture by reference. */
215 tree
216 lambda_capture_field_type (tree expr, bool explicit_init_p)
218 tree type;
219 if (explicit_init_p)
221 type = make_auto ();
222 type = do_auto_deduction (type, expr, type);
224 else
225 type = non_reference (unlowered_expr_type (expr));
226 if (type_dependent_expression_p (expr)
227 && !is_this_parameter (tree_strip_nop_conversions (expr)))
229 type = cxx_make_type (DECLTYPE_TYPE);
230 DECLTYPE_TYPE_EXPR (type) = expr;
231 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
232 DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p;
233 SET_TYPE_STRUCTURAL_EQUALITY (type);
235 return type;
238 /* Returns true iff DECL is a lambda capture proxy variable created by
239 build_capture_proxy. */
241 bool
242 is_capture_proxy (tree decl)
244 return (VAR_P (decl)
245 && DECL_HAS_VALUE_EXPR_P (decl)
246 && !DECL_ANON_UNION_VAR_P (decl)
247 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
250 /* Returns true iff DECL is a capture proxy for a normal capture
251 (i.e. without explicit initializer). */
253 bool
254 is_normal_capture_proxy (tree decl)
256 if (!is_capture_proxy (decl))
257 /* It's not a capture proxy. */
258 return false;
260 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
261 /* VLA capture. */
262 return true;
264 /* It is a capture proxy, is it a normal capture? */
265 tree val = DECL_VALUE_EXPR (decl);
266 if (val == error_mark_node)
267 return true;
269 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
270 val = TREE_OPERAND (val, 1);
271 return DECL_NORMAL_CAPTURE_P (val);
274 /* VAR is a capture proxy created by build_capture_proxy; add it to the
275 current function, which is the operator() for the appropriate lambda. */
277 void
278 insert_capture_proxy (tree var)
280 cp_binding_level *b;
281 tree stmt_list;
283 /* Put the capture proxy in the extra body block so that it won't clash
284 with a later local variable. */
285 b = current_binding_level;
286 for (;;)
288 cp_binding_level *n = b->level_chain;
289 if (n->kind == sk_function_parms)
290 break;
291 b = n;
293 pushdecl_with_scope (var, b, false);
295 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
296 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
297 stmt_list = (*stmt_list_stack)[1];
298 gcc_assert (stmt_list);
299 append_to_statement_list_force (var, &stmt_list);
302 /* We've just finished processing a lambda; if the containing scope is also
303 a lambda, insert any capture proxies that were created while processing
304 the nested lambda. */
306 void
307 insert_pending_capture_proxies (void)
309 tree lam;
310 vec<tree, va_gc> *proxies;
311 unsigned i;
313 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
314 return;
316 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
317 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
318 for (i = 0; i < vec_safe_length (proxies); ++i)
320 tree var = (*proxies)[i];
321 insert_capture_proxy (var);
323 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
324 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
327 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
328 return the type we want the proxy to have: the type of the field itself,
329 with added const-qualification if the lambda isn't mutable and the
330 capture is by value. */
332 tree
333 lambda_proxy_type (tree ref)
335 tree type;
336 if (ref == error_mark_node)
337 return error_mark_node;
338 if (REFERENCE_REF_P (ref))
339 ref = TREE_OPERAND (ref, 0);
340 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
341 type = TREE_TYPE (ref);
342 if (!type || WILDCARD_TYPE_P (non_reference (type)))
344 type = cxx_make_type (DECLTYPE_TYPE);
345 DECLTYPE_TYPE_EXPR (type) = ref;
346 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
347 SET_TYPE_STRUCTURAL_EQUALITY (type);
349 if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
350 type = make_pack_expansion (type);
351 return type;
354 /* MEMBER is a capture field in a lambda closure class. Now that we're
355 inside the operator(), build a placeholder var for future lookups and
356 debugging. */
358 tree
359 build_capture_proxy (tree member)
361 tree var, object, fn, closure, name, lam, type;
363 if (PACK_EXPANSION_P (member))
364 member = PACK_EXPANSION_PATTERN (member);
366 closure = DECL_CONTEXT (member);
367 fn = lambda_function (closure);
368 lam = CLASSTYPE_LAMBDA_EXPR (closure);
370 /* The proxy variable forwards to the capture field. */
371 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
372 object = finish_non_static_data_member (member, object, NULL_TREE);
373 if (REFERENCE_REF_P (object))
374 object = TREE_OPERAND (object, 0);
376 /* Remove the __ inserted by add_capture. */
377 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
379 type = lambda_proxy_type (object);
381 if (DECL_VLA_CAPTURE_P (member))
383 /* Rebuild the VLA type from the pointer and maxindex. */
384 tree field = next_initializable_field (TYPE_FIELDS (type));
385 tree ptr = build_simple_component_ref (object, field);
386 field = next_initializable_field (DECL_CHAIN (field));
387 tree max = build_simple_component_ref (object, field);
388 type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
389 build_index_type (max));
390 type = build_reference_type (type);
391 REFERENCE_VLA_OK (type) = true;
392 object = convert (type, ptr);
395 var = build_decl (input_location, VAR_DECL, name, type);
396 SET_DECL_VALUE_EXPR (var, object);
397 DECL_HAS_VALUE_EXPR_P (var) = 1;
398 DECL_ARTIFICIAL (var) = 1;
399 TREE_USED (var) = 1;
400 DECL_CONTEXT (var) = fn;
402 if (name == this_identifier)
404 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
405 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
408 if (fn == current_function_decl)
409 insert_capture_proxy (var);
410 else
411 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
413 return var;
416 /* Return a struct containing a pointer and a length for lambda capture of
417 an array of runtime length. */
419 static tree
420 vla_capture_type (tree array_type)
422 static tree ptr_id, max_id;
423 tree type = xref_tag (record_type, make_anon_name (), ts_current, false);
424 xref_basetypes (type, NULL_TREE);
425 type = begin_class_definition (type);
426 if (!ptr_id)
428 ptr_id = get_identifier ("ptr");
429 max_id = get_identifier ("max");
431 tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
432 tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
433 finish_member_declaration (field);
434 field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
435 finish_member_declaration (field);
436 return finish_struct (type, NULL_TREE);
439 /* From an ID and INITIALIZER, create a capture (by reference if
440 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
441 and return it. */
443 tree
444 add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
445 bool explicit_init_p)
447 char *buf;
448 tree type, member, name;
449 bool vla = false;
450 bool variadic = false;
451 tree initializer = orig_init;
453 if (PACK_EXPANSION_P (initializer))
455 initializer = PACK_EXPANSION_PATTERN (initializer);
456 variadic = true;
459 if (TREE_CODE (initializer) == TREE_LIST)
460 initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
461 tf_warning_or_error);
462 type = TREE_TYPE (initializer);
463 if (type == error_mark_node)
464 return error_mark_node;
466 if (array_of_runtime_bound_p (type))
468 vla = true;
469 if (!by_reference_p)
470 error ("array of runtime bound cannot be captured by copy, "
471 "only by reference");
473 /* For a VLA, we capture the address of the first element and the
474 maximum index, and then reconstruct the VLA for the proxy. */
475 tree elt = cp_build_array_ref (input_location, initializer,
476 integer_zero_node, tf_warning_or_error);
477 initializer = build_constructor_va (init_list_type_node, 2,
478 NULL_TREE, build_address (elt),
479 NULL_TREE, array_type_nelts (type));
480 type = vla_capture_type (type);
482 else if (!dependent_type_p (type)
483 && variably_modified_type_p (type, NULL_TREE))
485 error ("capture of variable-size type %qT that is not an N3639 array "
486 "of runtime bound", type);
487 if (TREE_CODE (type) == ARRAY_TYPE
488 && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
489 inform (input_location, "because the array element type %qT has "
490 "variable size", TREE_TYPE (type));
491 type = error_mark_node;
493 else
495 type = lambda_capture_field_type (initializer, explicit_init_p);
496 if (by_reference_p)
498 type = build_reference_type (type);
499 if (!dependent_type_p (type) && !real_lvalue_p (initializer))
500 error ("cannot capture %qE by reference", initializer);
502 else
504 /* Capture by copy requires a complete type. */
505 type = complete_type (type);
506 if (!dependent_type_p (type) && !COMPLETE_TYPE_P (type))
508 error ("capture by copy of incomplete type %qT", type);
509 cxx_incomplete_type_inform (type);
510 return error_mark_node;
515 /* Add __ to the beginning of the field name so that user code
516 won't find the field with name lookup. We can't just leave the name
517 unset because template instantiation uses the name to find
518 instantiated fields. */
519 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
520 buf[1] = buf[0] = '_';
521 memcpy (buf + 2, IDENTIFIER_POINTER (id),
522 IDENTIFIER_LENGTH (id) + 1);
523 name = get_identifier (buf);
525 /* If TREE_TYPE isn't set, we're still in the introducer, so check
526 for duplicates. */
527 if (!LAMBDA_EXPR_CLOSURE (lambda))
529 if (IDENTIFIER_MARKED (name))
531 pedwarn (input_location, 0,
532 "already captured %qD in lambda expression", id);
533 return NULL_TREE;
535 IDENTIFIER_MARKED (name) = true;
538 if (variadic)
539 type = make_pack_expansion (type);
541 /* Make member variable. */
542 member = build_decl (input_location, FIELD_DECL, name, type);
543 DECL_VLA_CAPTURE_P (member) = vla;
545 if (!explicit_init_p)
546 /* Normal captures are invisible to name lookup but uses are replaced
547 with references to the capture field; we implement this by only
548 really making them invisible in unevaluated context; see
549 qualify_lookup. For now, let's make explicitly initialized captures
550 always visible. */
551 DECL_NORMAL_CAPTURE_P (member) = true;
553 if (id == this_identifier)
554 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
556 /* Add it to the appropriate closure class if we've started it. */
557 if (current_class_type
558 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
559 finish_member_declaration (member);
561 tree listmem = member;
562 if (variadic)
564 listmem = make_pack_expansion (member);
565 initializer = orig_init;
567 LAMBDA_EXPR_CAPTURE_LIST (lambda)
568 = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
570 if (LAMBDA_EXPR_CLOSURE (lambda))
571 return build_capture_proxy (member);
572 /* For explicit captures we haven't started the function yet, so we wait
573 and build the proxy from cp_parser_lambda_body. */
574 return NULL_TREE;
577 /* Register all the capture members on the list CAPTURES, which is the
578 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
580 void
581 register_capture_members (tree captures)
583 if (captures == NULL_TREE)
584 return;
586 register_capture_members (TREE_CHAIN (captures));
588 tree field = TREE_PURPOSE (captures);
589 if (PACK_EXPANSION_P (field))
590 field = PACK_EXPANSION_PATTERN (field);
592 /* We set this in add_capture to avoid duplicates. */
593 IDENTIFIER_MARKED (DECL_NAME (field)) = false;
594 finish_member_declaration (field);
597 /* Similar to add_capture, except this works on a stack of nested lambdas.
598 BY_REFERENCE_P in this case is derived from the default capture mode.
599 Returns the capture for the lambda at the bottom of the stack. */
601 tree
602 add_default_capture (tree lambda_stack, tree id, tree initializer)
604 bool this_capture_p = (id == this_identifier);
606 tree var = NULL_TREE;
608 tree saved_class_type = current_class_type;
610 tree node;
612 for (node = lambda_stack;
613 node;
614 node = TREE_CHAIN (node))
616 tree lambda = TREE_VALUE (node);
618 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
619 if (DECL_PACK_P (initializer))
620 initializer = make_pack_expansion (initializer);
621 var = add_capture (lambda,
623 initializer,
624 /*by_reference_p=*/
625 (!this_capture_p
626 && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
627 == CPLD_REFERENCE)),
628 /*explicit_init_p=*/false);
629 initializer = convert_from_reference (var);
632 current_class_type = saved_class_type;
634 return var;
637 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
638 form of an INDIRECT_REF, possibly adding it through default
639 capturing, if ADD_CAPTURE_P is true. */
641 tree
642 lambda_expr_this_capture (tree lambda, bool add_capture_p)
644 tree result;
646 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
648 /* In unevaluated context this isn't an odr-use, so don't capture. */
649 if (cp_unevaluated_operand)
650 add_capture_p = false;
652 /* Try to default capture 'this' if we can. */
653 if (!this_capture
654 && (!add_capture_p
655 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE))
657 tree lambda_stack = NULL_TREE;
658 tree init = NULL_TREE;
660 /* If we are in a lambda function, we can move out until we hit:
661 1. a non-lambda function or NSDMI,
662 2. a lambda function capturing 'this', or
663 3. a non-default capturing lambda function. */
664 for (tree tlambda = lambda; ;)
666 lambda_stack = tree_cons (NULL_TREE,
667 tlambda,
668 lambda_stack);
670 if (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)
671 && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)) == FIELD_DECL)
673 /* In an NSDMI, we don't have a function to look up the decl in,
674 but the fake 'this' pointer that we're using for parsing is
675 in scope_chain. */
676 init = scope_chain->x_current_class_ptr;
677 gcc_checking_assert
678 (init && (TREE_TYPE (TREE_TYPE (init))
679 == current_nonlambda_class_type ()));
680 break;
683 tree closure_decl = TYPE_NAME (LAMBDA_EXPR_CLOSURE (tlambda));
684 tree containing_function = decl_function_context (closure_decl);
686 if (containing_function == NULL_TREE)
687 /* We ran out of scopes; there's no 'this' to capture. */
688 break;
690 if (!LAMBDA_FUNCTION_P (containing_function))
692 /* We found a non-lambda function. */
693 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function))
694 /* First parameter is 'this'. */
695 init = DECL_ARGUMENTS (containing_function);
696 break;
699 tlambda
700 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
702 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
704 /* An outer lambda has already captured 'this'. */
705 init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
706 break;
709 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
710 /* An outer lambda won't let us capture 'this'. */
711 break;
714 if (init)
716 if (add_capture_p)
717 this_capture = add_default_capture (lambda_stack,
718 /*id=*/this_identifier,
719 init);
720 else
721 this_capture = init;
725 if (cp_unevaluated_operand)
726 result = this_capture;
727 else if (!this_capture)
729 if (add_capture_p)
731 error ("%<this%> was not captured for this lambda function");
732 result = error_mark_node;
734 else
735 result = NULL_TREE;
737 else
739 /* To make sure that current_class_ref is for the lambda. */
740 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
741 == LAMBDA_EXPR_CLOSURE (lambda));
743 result = this_capture;
745 /* If 'this' is captured, each use of 'this' is transformed into an
746 access to the corresponding unnamed data member of the closure
747 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
748 ensures that the transformed expression is an rvalue. ] */
749 result = rvalue (result);
752 return result;
755 /* We don't want to capture 'this' until we know we need it, i.e. after
756 overload resolution has chosen a non-static member function. At that
757 point we call this function to turn a dummy object into a use of the
758 'this' capture. */
760 tree
761 maybe_resolve_dummy (tree object, bool add_capture_p)
763 if (!is_dummy_object (object))
764 return object;
766 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
767 gcc_assert (!TYPE_PTR_P (type));
769 if (type != current_class_type
770 && current_class_type
771 && LAMBDA_TYPE_P (current_class_type)
772 && lambda_function (current_class_type)
773 && DERIVED_FROM_P (type, current_nonlambda_class_type ()))
775 /* In a lambda, need to go through 'this' capture. */
776 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
777 tree cap = lambda_expr_this_capture (lam, add_capture_p);
778 if (cap && cap != error_mark_node)
779 object = build_x_indirect_ref (EXPR_LOCATION (object), cap,
780 RO_NULL, tf_warning_or_error);
783 return object;
786 /* Returns the innermost non-lambda function. */
788 tree
789 current_nonlambda_function (void)
791 tree fn = current_function_decl;
792 while (fn && LAMBDA_FUNCTION_P (fn))
793 fn = decl_function_context (fn);
794 return fn;
797 /* Returns the method basetype of the innermost non-lambda function, or
798 NULL_TREE if none. */
800 tree
801 nonlambda_method_basetype (void)
803 tree fn, type;
804 if (!current_class_ref)
805 return NULL_TREE;
807 type = current_class_type;
808 if (!LAMBDA_TYPE_P (type))
809 return type;
811 /* Find the nearest enclosing non-lambda function. */
812 fn = TYPE_NAME (type);
814 fn = decl_function_context (fn);
815 while (fn && LAMBDA_FUNCTION_P (fn));
817 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
818 return NULL_TREE;
820 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
823 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
824 indicated FN and NARGS, but do not initialize the return type or any of the
825 argument slots. */
827 static tree
828 prepare_op_call (tree fn, int nargs)
830 tree t;
832 t = build_vl_exp (CALL_EXPR, nargs + 3);
833 CALL_EXPR_FN (t) = fn;
834 CALL_EXPR_STATIC_CHAIN (t) = NULL;
836 return t;
839 /* If the closure TYPE has a static op(), also add a conversion to function
840 pointer. */
842 void
843 maybe_add_lambda_conv_op (tree type)
845 bool nested = (cfun != NULL);
846 bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
847 tree callop = lambda_function (type);
849 if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE)
850 return;
852 if (processing_template_decl)
853 return;
855 bool const generic_lambda_p
856 = (DECL_TEMPLATE_INFO (callop)
857 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop);
859 if (!generic_lambda_p && DECL_INITIAL (callop) == NULL_TREE)
861 /* If the op() wasn't instantiated due to errors, give up. */
862 gcc_assert (errorcount || sorrycount);
863 return;
866 /* Non-template conversion operators are defined directly with build_call_a
867 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
868 deferred and the CALL is built in-place. In the case of a deduced return
869 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
870 the return type is also built in-place. The arguments of DECLTYPE_CALL in
871 the return expression may differ in flags from those in the body CALL. In
872 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
873 the body CALL, but not in DECLTYPE_CALL. */
875 vec<tree, va_gc> *direct_argvec = 0;
876 tree decltype_call = 0, call = 0;
877 tree fn_result = TREE_TYPE (TREE_TYPE (callop));
879 if (generic_lambda_p)
881 /* Prepare the dependent member call for the static member function
882 '_FUN' and, potentially, prepare another call to be used in a decltype
883 return expression for a deduced return call op to allow for simple
884 implementation of the conversion operator. */
886 tree instance = build_nop (type, null_pointer_node);
887 tree objfn = build_min (COMPONENT_REF, NULL_TREE,
888 instance, DECL_NAME (callop), NULL_TREE);
889 int nargs = list_length (DECL_ARGUMENTS (callop)) - 1;
891 call = prepare_op_call (objfn, nargs);
892 if (type_uses_auto (fn_result))
893 decltype_call = prepare_op_call (objfn, nargs);
895 else
897 direct_argvec = make_tree_vector ();
898 direct_argvec->quick_push (build1 (NOP_EXPR,
899 TREE_TYPE (DECL_ARGUMENTS (callop)),
900 null_pointer_node));
903 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
904 declare the static member function "_FUN" below. For each arg append to
905 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
906 call args (for the template case). If a parameter pack is found, expand
907 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
909 tree fn_args = NULL_TREE;
911 int ix = 0;
912 tree src = DECL_CHAIN (DECL_ARGUMENTS (callop));
913 tree tgt;
915 while (src)
917 tree new_node = copy_node (src);
919 if (!fn_args)
920 fn_args = tgt = new_node;
921 else
923 TREE_CHAIN (tgt) = new_node;
924 tgt = new_node;
927 mark_exp_read (tgt);
929 if (generic_lambda_p)
931 if (DECL_PACK_P (tgt))
933 tree a = make_pack_expansion (tgt);
934 if (decltype_call)
935 CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
936 PACK_EXPANSION_LOCAL_P (a) = true;
937 CALL_EXPR_ARG (call, ix) = a;
939 else
941 tree a = convert_from_reference (tgt);
942 CALL_EXPR_ARG (call, ix) = a;
943 if (decltype_call)
944 CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
946 ++ix;
948 else
949 vec_safe_push (direct_argvec, tgt);
951 src = TREE_CHAIN (src);
956 if (generic_lambda_p)
958 if (decltype_call)
960 ++processing_template_decl;
961 fn_result = finish_decltype_type
962 (decltype_call, /*id_expression_or_member_access_p=*/false,
963 tf_warning_or_error);
964 --processing_template_decl;
967 else
968 call = build_call_a (callop,
969 direct_argvec->length (),
970 direct_argvec->address ());
972 CALL_FROM_THUNK_P (call) = 1;
974 tree stattype = build_function_type (fn_result, FUNCTION_ARG_CHAIN (callop));
976 /* First build up the conversion op. */
978 tree rettype = build_pointer_type (stattype);
979 tree name = mangle_conv_op_name_for_type (rettype);
980 tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
981 tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
982 tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
983 tree fn = convfn;
984 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
986 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
987 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
988 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
990 SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
991 grokclassfn (type, fn, NO_SPECIAL);
992 set_linkage_according_to_type (type, fn);
993 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
994 DECL_IN_AGGR_P (fn) = 1;
995 DECL_ARTIFICIAL (fn) = 1;
996 DECL_NOT_REALLY_EXTERN (fn) = 1;
997 DECL_DECLARED_INLINE_P (fn) = 1;
998 DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
999 if (nested_def)
1000 DECL_INTERFACE_KNOWN (fn) = 1;
1002 if (generic_lambda_p)
1003 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1005 add_method (type, fn, NULL_TREE);
1007 /* Generic thunk code fails for varargs; we'll complain in mark_used if
1008 the conversion op is used. */
1009 if (varargs_function_p (callop))
1011 DECL_DELETED_FN (fn) = 1;
1012 return;
1015 /* Now build up the thunk to be returned. */
1017 name = get_identifier ("_FUN");
1018 tree statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
1019 fn = statfn;
1020 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
1021 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1022 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1023 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1024 grokclassfn (type, fn, NO_SPECIAL);
1025 set_linkage_according_to_type (type, fn);
1026 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1027 DECL_IN_AGGR_P (fn) = 1;
1028 DECL_ARTIFICIAL (fn) = 1;
1029 DECL_NOT_REALLY_EXTERN (fn) = 1;
1030 DECL_DECLARED_INLINE_P (fn) = 1;
1031 DECL_STATIC_FUNCTION_P (fn) = 1;
1032 DECL_ARGUMENTS (fn) = fn_args;
1033 for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
1035 /* Avoid duplicate -Wshadow warnings. */
1036 DECL_NAME (arg) = NULL_TREE;
1037 DECL_CONTEXT (arg) = fn;
1039 if (nested_def)
1040 DECL_INTERFACE_KNOWN (fn) = 1;
1042 if (generic_lambda_p)
1043 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1045 add_method (type, fn, NULL_TREE);
1047 if (nested)
1048 push_function_context ();
1049 else
1050 /* Still increment function_depth so that we don't GC in the
1051 middle of an expression. */
1052 ++function_depth;
1054 /* Generate the body of the thunk. */
1056 start_preparsed_function (statfn, NULL_TREE,
1057 SF_PRE_PARSED | SF_INCLASS_INLINE);
1058 if (DECL_ONE_ONLY (statfn))
1060 /* Put the thunk in the same comdat group as the call op. */
1061 cgraph_node::get_create (statfn)->add_to_same_comdat_group
1062 (cgraph_node::get_create (callop));
1064 tree body = begin_function_body ();
1065 tree compound_stmt = begin_compound_stmt (0);
1066 if (!generic_lambda_p)
1068 set_flags_from_callee (call);
1069 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1070 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1072 call = convert_from_reference (call);
1073 finish_return_stmt (call);
1075 finish_compound_stmt (compound_stmt);
1076 finish_function_body (body);
1078 fn = finish_function (/*inline*/2);
1079 if (!generic_lambda_p)
1080 expand_or_defer_fn (fn);
1082 /* Generate the body of the conversion op. */
1084 start_preparsed_function (convfn, NULL_TREE,
1085 SF_PRE_PARSED | SF_INCLASS_INLINE);
1086 body = begin_function_body ();
1087 compound_stmt = begin_compound_stmt (0);
1089 /* decl_needed_p needs to see that it's used. */
1090 TREE_USED (statfn) = 1;
1091 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1093 finish_compound_stmt (compound_stmt);
1094 finish_function_body (body);
1096 fn = finish_function (/*inline*/2);
1097 if (!generic_lambda_p)
1098 expand_or_defer_fn (fn);
1100 if (nested)
1101 pop_function_context ();
1102 else
1103 --function_depth;
1106 /* Returns true iff VAL is a lambda-related declaration which should
1107 be ignored by unqualified lookup. */
1109 bool
1110 is_lambda_ignored_entity (tree val)
1112 /* In unevaluated context, look past normal capture proxies. */
1113 if (cp_unevaluated_operand && is_normal_capture_proxy (val))
1114 return true;
1116 /* Always ignore lambda fields, their names are only for debugging. */
1117 if (TREE_CODE (val) == FIELD_DECL
1118 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1119 return true;
1121 /* None of the lookups that use qualify_lookup want the op() from the
1122 lambda; they want the one from the enclosing class. */
1123 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
1124 return true;
1126 return false;