2014-01-30 Alangi Derick <alangiderick@gmail.com>
[official-gcc.git] / gcc / cp / lambda.c
blob8bb820d0c3b0367dca12b2751d911ba39f3810ff
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-2014 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 "tree.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "tree-iterator.h"
31 #include "cp-tree.h"
32 #include "toplev.h"
33 #include "vec.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 (DECL_P (val))
83 mark_used (val);
85 /* Mere mortals can't copy arrays with aggregate initialization, so
86 do some magic to make it work here. */
87 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
88 val = build_array_copy (val);
89 else if (DECL_NORMAL_CAPTURE_P (field)
90 && !DECL_VLA_CAPTURE_P (field)
91 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
93 /* "the entities that are captured by copy are used to
94 direct-initialize each corresponding non-static data
95 member of the resulting closure object."
97 There's normally no way to express direct-initialization
98 from an element of a CONSTRUCTOR, so we build up a special
99 TARGET_EXPR to bypass the usual copy-initialization. */
100 val = force_rvalue (val, tf_warning_or_error);
101 if (TREE_CODE (val) == TARGET_EXPR)
102 TARGET_EXPR_DIRECT_INIT_P (val) = true;
105 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
108 expr = build_constructor (init_list_type_node, elts);
109 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
111 /* N2927: "[The closure] class type is not an aggregate."
112 But we briefly treat it as an aggregate to make this simpler. */
113 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
114 CLASSTYPE_NON_AGGREGATE (type) = 0;
115 expr = finish_compound_literal (type, expr, tf_warning_or_error);
116 CLASSTYPE_NON_AGGREGATE (type) = 1;
118 out:
119 input_location = saved_loc;
120 return expr;
123 /* Return an initialized RECORD_TYPE for LAMBDA.
124 LAMBDA must have its explicit captures already. */
126 tree
127 begin_lambda_type (tree lambda)
129 tree type;
132 /* Unique name. This is just like an unnamed class, but we cannot use
133 make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */
134 tree name;
135 name = make_lambda_name ();
137 /* Create the new RECORD_TYPE for this lambda. */
138 type = xref_tag (/*tag_code=*/record_type,
139 name,
140 /*scope=*/ts_lambda,
141 /*template_header_p=*/false);
142 if (type == error_mark_node)
143 return error_mark_node;
146 /* Designate it as a struct so that we can use aggregate initialization. */
147 CLASSTYPE_DECLARED_CLASS (type) = false;
149 /* Cross-reference the expression and the type. */
150 LAMBDA_EXPR_CLOSURE (lambda) = type;
151 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
153 /* Clear base types. */
154 xref_basetypes (type, /*bases=*/NULL_TREE);
156 /* Start the class. */
157 type = begin_class_definition (type);
159 return type;
162 /* Returns the type to use for the return type of the operator() of a
163 closure class. */
165 tree
166 lambda_return_type (tree expr)
168 if (expr == NULL_TREE)
169 return void_type_node;
170 if (type_unknown_p (expr)
171 || BRACE_ENCLOSED_INITIALIZER_P (expr))
173 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
174 return void_type_node;
176 gcc_checking_assert (!type_dependent_expression_p (expr));
177 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
180 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
181 closure type. */
183 tree
184 lambda_function (tree lambda)
186 tree type;
187 if (TREE_CODE (lambda) == LAMBDA_EXPR)
188 type = LAMBDA_EXPR_CLOSURE (lambda);
189 else
190 type = lambda;
191 gcc_assert (LAMBDA_TYPE_P (type));
192 /* Don't let debug_tree cause instantiation. */
193 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
194 && !COMPLETE_OR_OPEN_TYPE_P (type))
195 return NULL_TREE;
196 lambda = lookup_member (type, ansi_opname (CALL_EXPR),
197 /*protect=*/0, /*want_type=*/false,
198 tf_warning_or_error);
199 if (lambda)
200 lambda = STRIP_TEMPLATE (get_first_fn (lambda));
201 return lambda;
204 /* Returns the type to use for the FIELD_DECL corresponding to the
205 capture of EXPR.
206 The caller should add REFERENCE_TYPE for capture by reference. */
208 tree
209 lambda_capture_field_type (tree expr, bool explicit_init_p)
211 tree type;
212 if (explicit_init_p)
214 type = make_auto ();
215 type = do_auto_deduction (type, expr, type);
217 else
218 type = non_reference (unlowered_expr_type (expr));
219 if (!type || WILDCARD_TYPE_P (type) || type_uses_auto (type)
220 || DECL_PACK_P (expr))
222 type = cxx_make_type (DECLTYPE_TYPE);
223 DECLTYPE_TYPE_EXPR (type) = expr;
224 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
225 DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p;
226 SET_TYPE_STRUCTURAL_EQUALITY (type);
228 return type;
231 /* Returns true iff DECL is a lambda capture proxy variable created by
232 build_capture_proxy. */
234 bool
235 is_capture_proxy (tree decl)
237 return (VAR_P (decl)
238 && DECL_HAS_VALUE_EXPR_P (decl)
239 && !DECL_ANON_UNION_VAR_P (decl)
240 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
243 /* Returns true iff DECL is a capture proxy for a normal capture
244 (i.e. without explicit initializer). */
246 bool
247 is_normal_capture_proxy (tree decl)
249 if (!is_capture_proxy (decl))
250 /* It's not a capture proxy. */
251 return false;
253 /* It is a capture proxy, is it a normal capture? */
254 tree val = DECL_VALUE_EXPR (decl);
255 if (val == error_mark_node)
256 return true;
258 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
259 val = TREE_OPERAND (val, 1);
260 return DECL_NORMAL_CAPTURE_P (val);
263 /* VAR is a capture proxy created by build_capture_proxy; add it to the
264 current function, which is the operator() for the appropriate lambda. */
266 void
267 insert_capture_proxy (tree var)
269 cp_binding_level *b;
270 tree stmt_list;
272 /* Put the capture proxy in the extra body block so that it won't clash
273 with a later local variable. */
274 b = current_binding_level;
275 for (;;)
277 cp_binding_level *n = b->level_chain;
278 if (n->kind == sk_function_parms)
279 break;
280 b = n;
282 pushdecl_with_scope (var, b, false);
284 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
285 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
286 stmt_list = (*stmt_list_stack)[1];
287 gcc_assert (stmt_list);
288 append_to_statement_list_force (var, &stmt_list);
291 /* We've just finished processing a lambda; if the containing scope is also
292 a lambda, insert any capture proxies that were created while processing
293 the nested lambda. */
295 void
296 insert_pending_capture_proxies (void)
298 tree lam;
299 vec<tree, va_gc> *proxies;
300 unsigned i;
302 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
303 return;
305 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
306 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
307 for (i = 0; i < vec_safe_length (proxies); ++i)
309 tree var = (*proxies)[i];
310 insert_capture_proxy (var);
312 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
313 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
316 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
317 return the type we want the proxy to have: the type of the field itself,
318 with added const-qualification if the lambda isn't mutable and the
319 capture is by value. */
321 tree
322 lambda_proxy_type (tree ref)
324 tree type;
325 if (ref == error_mark_node)
326 return error_mark_node;
327 if (REFERENCE_REF_P (ref))
328 ref = TREE_OPERAND (ref, 0);
329 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
330 type = TREE_TYPE (ref);
331 if (!type || WILDCARD_TYPE_P (non_reference (type)))
333 type = cxx_make_type (DECLTYPE_TYPE);
334 DECLTYPE_TYPE_EXPR (type) = ref;
335 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
336 SET_TYPE_STRUCTURAL_EQUALITY (type);
338 if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
339 type = make_pack_expansion (type);
340 return type;
343 /* MEMBER is a capture field in a lambda closure class. Now that we're
344 inside the operator(), build a placeholder var for future lookups and
345 debugging. */
347 tree
348 build_capture_proxy (tree member)
350 tree var, object, fn, closure, name, lam, type;
352 if (PACK_EXPANSION_P (member))
353 member = PACK_EXPANSION_PATTERN (member);
355 closure = DECL_CONTEXT (member);
356 fn = lambda_function (closure);
357 lam = CLASSTYPE_LAMBDA_EXPR (closure);
359 /* The proxy variable forwards to the capture field. */
360 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
361 object = finish_non_static_data_member (member, object, NULL_TREE);
362 if (REFERENCE_REF_P (object))
363 object = TREE_OPERAND (object, 0);
365 /* Remove the __ inserted by add_capture. */
366 if (DECL_NORMAL_CAPTURE_P (member))
367 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
368 else
369 name = DECL_NAME (member);
371 type = lambda_proxy_type (object);
373 if (DECL_VLA_CAPTURE_P (member))
375 /* Rebuild the VLA type from the pointer and maxindex. */
376 tree field = next_initializable_field (TYPE_FIELDS (type));
377 tree ptr = build_simple_component_ref (object, field);
378 field = next_initializable_field (DECL_CHAIN (field));
379 tree max = build_simple_component_ref (object, field);
380 type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
381 build_index_type (max));
382 type = build_reference_type (type);
383 REFERENCE_VLA_OK (type) = true;
384 object = convert (type, ptr);
387 var = build_decl (input_location, VAR_DECL, name, type);
388 SET_DECL_VALUE_EXPR (var, object);
389 DECL_HAS_VALUE_EXPR_P (var) = 1;
390 DECL_ARTIFICIAL (var) = 1;
391 TREE_USED (var) = 1;
392 DECL_CONTEXT (var) = fn;
394 if (name == this_identifier)
396 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
397 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
400 if (fn == current_function_decl)
401 insert_capture_proxy (var);
402 else
403 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
405 return var;
408 /* Return a struct containing a pointer and a length for lambda capture of
409 an array of runtime length. */
411 static tree
412 vla_capture_type (tree array_type)
414 static tree ptr_id, max_id;
415 tree type = xref_tag (record_type, make_anon_name (), ts_current, false);
416 xref_basetypes (type, NULL_TREE);
417 type = begin_class_definition (type);
418 if (!ptr_id)
420 ptr_id = get_identifier ("ptr");
421 max_id = get_identifier ("max");
423 tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
424 tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
425 finish_member_declaration (field);
426 field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
427 finish_member_declaration (field);
428 return finish_struct (type, NULL_TREE);
431 /* From an ID and INITIALIZER, create a capture (by reference if
432 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
433 and return it. */
435 tree
436 add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
437 bool explicit_init_p)
439 char *buf;
440 tree type, member, name;
441 bool vla = false;
442 bool variadic = false;
443 tree initializer = orig_init;
445 if (PACK_EXPANSION_P (initializer))
447 initializer = PACK_EXPANSION_PATTERN (initializer);
448 variadic = true;
451 if (TREE_CODE (initializer) == TREE_LIST)
452 initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
453 tf_warning_or_error);
454 type = lambda_capture_field_type (initializer, explicit_init_p);
455 if (array_of_runtime_bound_p (type))
457 vla = true;
458 if (!by_reference_p)
459 error ("array of runtime bound cannot be captured by copy, "
460 "only by reference");
462 /* For a VLA, we capture the address of the first element and the
463 maximum index, and then reconstruct the VLA for the proxy. */
464 tree elt = cp_build_array_ref (input_location, initializer,
465 integer_zero_node, tf_warning_or_error);
466 initializer = build_constructor_va (init_list_type_node, 2,
467 NULL_TREE, build_address (elt),
468 NULL_TREE, array_type_nelts (type));
469 type = vla_capture_type (type);
471 else if (variably_modified_type_p (type, NULL_TREE))
473 error ("capture of variable-size type %qT that is not a C++1y array "
474 "of runtime bound", type);
475 if (TREE_CODE (type) == ARRAY_TYPE
476 && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
477 inform (input_location, "because the array element type %qT has "
478 "variable size", TREE_TYPE (type));
479 type = error_mark_node;
481 else if (by_reference_p)
483 type = build_reference_type (type);
484 if (!real_lvalue_p (initializer))
485 error ("cannot capture %qE by reference", initializer);
487 else
488 /* Capture by copy requires a complete type. */
489 type = complete_type (type);
491 /* Add __ to the beginning of the field name so that user code
492 won't find the field with name lookup. We can't just leave the name
493 unset because template instantiation uses the name to find
494 instantiated fields. */
495 if (!explicit_init_p)
497 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
498 buf[1] = buf[0] = '_';
499 memcpy (buf + 2, IDENTIFIER_POINTER (id),
500 IDENTIFIER_LENGTH (id) + 1);
501 name = get_identifier (buf);
503 else
504 /* But captures with explicit initializers are named. */
505 name = id;
507 /* If TREE_TYPE isn't set, we're still in the introducer, so check
508 for duplicates. */
509 if (!LAMBDA_EXPR_CLOSURE (lambda))
511 if (IDENTIFIER_MARKED (name))
513 pedwarn (input_location, 0,
514 "already captured %qD in lambda expression", id);
515 return NULL_TREE;
517 IDENTIFIER_MARKED (name) = true;
520 if (variadic)
521 type = make_pack_expansion (type);
523 /* Make member variable. */
524 member = build_decl (input_location, FIELD_DECL, name, type);
525 DECL_VLA_CAPTURE_P (member) = vla;
527 if (!explicit_init_p)
528 /* Normal captures are invisible to name lookup but uses are replaced
529 with references to the capture field; we implement this by only
530 really making them invisible in unevaluated context; see
531 qualify_lookup. For now, let's make explicitly initialized captures
532 always visible. */
533 DECL_NORMAL_CAPTURE_P (member) = true;
535 if (id == this_identifier)
536 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
538 /* Add it to the appropriate closure class if we've started it. */
539 if (current_class_type
540 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
541 finish_member_declaration (member);
543 tree listmem = member;
544 if (variadic)
546 listmem = make_pack_expansion (member);
547 initializer = orig_init;
549 LAMBDA_EXPR_CAPTURE_LIST (lambda)
550 = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
552 if (LAMBDA_EXPR_CLOSURE (lambda))
553 return build_capture_proxy (member);
554 /* For explicit captures we haven't started the function yet, so we wait
555 and build the proxy from cp_parser_lambda_body. */
556 return NULL_TREE;
559 /* Register all the capture members on the list CAPTURES, which is the
560 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
562 void
563 register_capture_members (tree captures)
565 if (captures == NULL_TREE)
566 return;
568 register_capture_members (TREE_CHAIN (captures));
570 tree field = TREE_PURPOSE (captures);
571 if (PACK_EXPANSION_P (field))
572 field = PACK_EXPANSION_PATTERN (field);
574 /* We set this in add_capture to avoid duplicates. */
575 IDENTIFIER_MARKED (DECL_NAME (field)) = false;
576 finish_member_declaration (field);
579 /* Similar to add_capture, except this works on a stack of nested lambdas.
580 BY_REFERENCE_P in this case is derived from the default capture mode.
581 Returns the capture for the lambda at the bottom of the stack. */
583 tree
584 add_default_capture (tree lambda_stack, tree id, tree initializer)
586 bool this_capture_p = (id == this_identifier);
588 tree var = NULL_TREE;
590 tree saved_class_type = current_class_type;
592 tree node;
594 for (node = lambda_stack;
595 node;
596 node = TREE_CHAIN (node))
598 tree lambda = TREE_VALUE (node);
600 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
601 if (DECL_PACK_P (initializer))
602 initializer = make_pack_expansion (initializer);
603 var = add_capture (lambda,
605 initializer,
606 /*by_reference_p=*/
607 (!this_capture_p
608 && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
609 == CPLD_REFERENCE)),
610 /*explicit_init_p=*/false);
611 initializer = convert_from_reference (var);
614 current_class_type = saved_class_type;
616 return var;
619 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the form of an
620 INDIRECT_REF, possibly adding it through default capturing. */
622 tree
623 lambda_expr_this_capture (tree lambda)
625 tree result;
627 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
629 /* In unevaluated context this isn't an odr-use, so just return the
630 nearest 'this'. */
631 if (cp_unevaluated_operand)
633 /* In an NSDMI the fake 'this' pointer that we're using for
634 parsing is in scope_chain. */
635 if (LAMBDA_EXPR_EXTRA_SCOPE (lambda)
636 && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (lambda)) == FIELD_DECL)
637 return scope_chain->x_current_class_ptr;
638 return lookup_name (this_identifier);
641 /* Try to default capture 'this' if we can. */
642 if (!this_capture
643 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE)
645 tree lambda_stack = NULL_TREE;
646 tree init = NULL_TREE;
648 /* If we are in a lambda function, we can move out until we hit:
649 1. a non-lambda function or NSDMI,
650 2. a lambda function capturing 'this', or
651 3. a non-default capturing lambda function. */
652 for (tree tlambda = lambda; ;)
654 lambda_stack = tree_cons (NULL_TREE,
655 tlambda,
656 lambda_stack);
658 if (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)
659 && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)) == FIELD_DECL)
661 /* In an NSDMI, we don't have a function to look up the decl in,
662 but the fake 'this' pointer that we're using for parsing is
663 in scope_chain. */
664 init = scope_chain->x_current_class_ptr;
665 gcc_checking_assert
666 (init && (TREE_TYPE (TREE_TYPE (init))
667 == current_nonlambda_class_type ()));
668 break;
671 tree closure_decl = TYPE_NAME (LAMBDA_EXPR_CLOSURE (tlambda));
672 tree containing_function = decl_function_context (closure_decl);
674 if (containing_function == NULL_TREE)
675 /* We ran out of scopes; there's no 'this' to capture. */
676 break;
678 if (!LAMBDA_FUNCTION_P (containing_function))
680 /* We found a non-lambda function. */
681 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function))
682 /* First parameter is 'this'. */
683 init = DECL_ARGUMENTS (containing_function);
684 break;
687 tlambda
688 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
690 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
692 /* An outer lambda has already captured 'this'. */
693 init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
694 break;
697 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
698 /* An outer lambda won't let us capture 'this'. */
699 break;
702 if (init)
703 this_capture = add_default_capture (lambda_stack,
704 /*id=*/this_identifier,
705 init);
708 if (!this_capture)
710 error ("%<this%> was not captured for this lambda function");
711 result = error_mark_node;
713 else
715 /* To make sure that current_class_ref is for the lambda. */
716 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
717 == LAMBDA_EXPR_CLOSURE (lambda));
719 result = this_capture;
721 /* If 'this' is captured, each use of 'this' is transformed into an
722 access to the corresponding unnamed data member of the closure
723 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
724 ensures that the transformed expression is an rvalue. ] */
725 result = rvalue (result);
728 return result;
731 /* We don't want to capture 'this' until we know we need it, i.e. after
732 overload resolution has chosen a non-static member function. At that
733 point we call this function to turn a dummy object into a use of the
734 'this' capture. */
736 tree
737 maybe_resolve_dummy (tree object)
739 if (!is_dummy_object (object))
740 return object;
742 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
743 gcc_assert (!TYPE_PTR_P (type));
745 if (type != current_class_type
746 && current_class_type
747 && LAMBDA_TYPE_P (current_class_type)
748 && DERIVED_FROM_P (type, current_nonlambda_class_type ()))
750 /* In a lambda, need to go through 'this' capture. */
751 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
752 tree cap = lambda_expr_this_capture (lam);
753 object = build_x_indirect_ref (EXPR_LOCATION (object), cap,
754 RO_NULL, tf_warning_or_error);
757 return object;
760 /* Returns the method basetype of the innermost non-lambda function, or
761 NULL_TREE if none. */
763 tree
764 nonlambda_method_basetype (void)
766 tree fn, type;
767 if (!current_class_ref)
768 return NULL_TREE;
770 type = current_class_type;
771 if (!LAMBDA_TYPE_P (type))
772 return type;
774 /* Find the nearest enclosing non-lambda function. */
775 fn = TYPE_NAME (type);
777 fn = decl_function_context (fn);
778 while (fn && LAMBDA_FUNCTION_P (fn));
780 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
781 return NULL_TREE;
783 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
786 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
787 indicated FN and NARGS, but do not initialize the return type or any of the
788 argument slots. */
790 static tree
791 prepare_op_call (tree fn, int nargs)
793 tree t;
795 t = build_vl_exp (CALL_EXPR, nargs + 3);
796 CALL_EXPR_FN (t) = fn;
797 CALL_EXPR_STATIC_CHAIN (t) = NULL;
799 return t;
802 /* If the closure TYPE has a static op(), also add a conversion to function
803 pointer. */
805 void
806 maybe_add_lambda_conv_op (tree type)
808 bool nested = (current_function_decl != NULL_TREE);
809 tree callop = lambda_function (type);
811 if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE)
812 return;
814 if (processing_template_decl)
815 return;
817 bool const generic_lambda_p
818 = (DECL_TEMPLATE_INFO (callop)
819 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop);
821 if (!generic_lambda_p && DECL_INITIAL (callop) == NULL_TREE)
823 /* If the op() wasn't instantiated due to errors, give up. */
824 gcc_assert (errorcount || sorrycount);
825 return;
828 /* Non-template conversion operators are defined directly with build_call_a
829 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
830 deferred and the CALL is built in-place. In the case of a deduced return
831 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
832 the return type is also built in-place. The arguments of DECLTYPE_CALL in
833 the return expression may differ in flags from those in the body CALL. In
834 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
835 the body CALL, but not in DECLTYPE_CALL. */
837 vec<tree, va_gc> *direct_argvec = 0;
838 tree decltype_call = 0, call = 0;
839 tree fn_result = TREE_TYPE (TREE_TYPE (callop));
841 if (generic_lambda_p)
843 /* Prepare the dependent member call for the static member function
844 '_FUN' and, potentially, prepare another call to be used in a decltype
845 return expression for a deduced return call op to allow for simple
846 implementation of the conversion operator. */
848 tree instance = build_nop (type, null_pointer_node);
849 tree objfn = build_min (COMPONENT_REF, NULL_TREE,
850 instance, DECL_NAME (callop), NULL_TREE);
851 int nargs = list_length (DECL_ARGUMENTS (callop)) - 1;
853 call = prepare_op_call (objfn, nargs);
854 if (type_uses_auto (fn_result))
855 decltype_call = prepare_op_call (objfn, nargs);
857 else
859 direct_argvec = make_tree_vector ();
860 direct_argvec->quick_push (build1 (NOP_EXPR,
861 TREE_TYPE (DECL_ARGUMENTS (callop)),
862 null_pointer_node));
865 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
866 declare the static member function "_FUN" below. For each arg append to
867 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
868 call args (for the template case). If a parameter pack is found, expand
869 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
871 tree fn_args = NULL_TREE;
873 int ix = 0;
874 tree src = DECL_CHAIN (DECL_ARGUMENTS (callop));
875 tree tgt;
877 while (src)
879 tree new_node = copy_node (src);
881 if (!fn_args)
882 fn_args = tgt = new_node;
883 else
885 TREE_CHAIN (tgt) = new_node;
886 tgt = new_node;
889 mark_exp_read (tgt);
891 if (generic_lambda_p)
893 if (DECL_PACK_P (tgt))
895 tree a = make_pack_expansion (tgt);
896 if (decltype_call)
897 CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
898 PACK_EXPANSION_LOCAL_P (a) = true;
899 CALL_EXPR_ARG (call, ix) = a;
901 else
903 tree a = convert_from_reference (tgt);
904 CALL_EXPR_ARG (call, ix) = a;
905 if (decltype_call)
906 CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
908 ++ix;
910 else
911 vec_safe_push (direct_argvec, tgt);
913 src = TREE_CHAIN (src);
918 if (generic_lambda_p)
920 if (decltype_call)
922 ++processing_template_decl;
923 fn_result = finish_decltype_type
924 (decltype_call, /*id_expression_or_member_access_p=*/false,
925 tf_warning_or_error);
926 --processing_template_decl;
929 else
930 call = build_call_a (callop,
931 direct_argvec->length (),
932 direct_argvec->address ());
934 CALL_FROM_THUNK_P (call) = 1;
936 tree stattype = build_function_type (fn_result, FUNCTION_ARG_CHAIN (callop));
938 /* First build up the conversion op. */
940 tree rettype = build_pointer_type (stattype);
941 tree name = mangle_conv_op_name_for_type (rettype);
942 tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
943 tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
944 tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
945 tree fn = convfn;
946 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
948 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
949 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
950 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
952 SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
953 grokclassfn (type, fn, NO_SPECIAL);
954 set_linkage_according_to_type (type, fn);
955 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
956 DECL_IN_AGGR_P (fn) = 1;
957 DECL_ARTIFICIAL (fn) = 1;
958 DECL_NOT_REALLY_EXTERN (fn) = 1;
959 DECL_DECLARED_INLINE_P (fn) = 1;
960 DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
961 if (nested)
962 DECL_INTERFACE_KNOWN (fn) = 1;
964 if (generic_lambda_p)
965 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
967 add_method (type, fn, NULL_TREE);
969 /* Generic thunk code fails for varargs; we'll complain in mark_used if
970 the conversion op is used. */
971 if (varargs_function_p (callop))
973 DECL_DELETED_FN (STRIP_TEMPLATE (fn)) = 1;
974 return;
977 /* Now build up the thunk to be returned. */
979 name = get_identifier ("_FUN");
980 tree statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
981 fn = statfn;
982 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
983 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
984 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
985 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
986 grokclassfn (type, fn, NO_SPECIAL);
987 set_linkage_according_to_type (type, fn);
988 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
989 DECL_IN_AGGR_P (fn) = 1;
990 DECL_ARTIFICIAL (fn) = 1;
991 DECL_NOT_REALLY_EXTERN (fn) = 1;
992 DECL_DECLARED_INLINE_P (fn) = 1;
993 DECL_STATIC_FUNCTION_P (fn) = 1;
994 DECL_ARGUMENTS (fn) = fn_args;
995 for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
997 /* Avoid duplicate -Wshadow warnings. */
998 DECL_NAME (arg) = NULL_TREE;
999 DECL_CONTEXT (arg) = fn;
1001 if (nested)
1002 DECL_INTERFACE_KNOWN (fn) = 1;
1004 if (generic_lambda_p)
1005 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
1007 add_method (type, fn, NULL_TREE);
1009 if (nested)
1010 push_function_context ();
1011 else
1012 /* Still increment function_depth so that we don't GC in the
1013 middle of an expression. */
1014 ++function_depth;
1016 /* Generate the body of the thunk. */
1018 start_preparsed_function (statfn, NULL_TREE,
1019 SF_PRE_PARSED | SF_INCLASS_INLINE);
1020 if (DECL_ONE_ONLY (statfn))
1022 /* Put the thunk in the same comdat group as the call op. */
1023 symtab_add_to_same_comdat_group
1024 (cgraph_get_create_node (statfn),
1025 cgraph_get_create_node (callop));
1027 tree body = begin_function_body ();
1028 tree compound_stmt = begin_compound_stmt (0);
1029 if (!generic_lambda_p)
1031 set_flags_from_callee (call);
1032 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
1033 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
1035 call = convert_from_reference (call);
1036 finish_return_stmt (call);
1038 finish_compound_stmt (compound_stmt);
1039 finish_function_body (body);
1041 fn = finish_function (/*inline*/2);
1042 if (!generic_lambda_p)
1043 expand_or_defer_fn (fn);
1045 /* Generate the body of the conversion op. */
1047 start_preparsed_function (convfn, NULL_TREE,
1048 SF_PRE_PARSED | SF_INCLASS_INLINE);
1049 body = begin_function_body ();
1050 compound_stmt = begin_compound_stmt (0);
1052 /* decl_needed_p needs to see that it's used. */
1053 TREE_USED (statfn) = 1;
1054 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
1056 finish_compound_stmt (compound_stmt);
1057 finish_function_body (body);
1059 fn = finish_function (/*inline*/2);
1060 if (!generic_lambda_p)
1061 expand_or_defer_fn (fn);
1063 if (nested)
1064 pop_function_context ();
1065 else
1066 --function_depth;
1069 /* Returns true iff VAL is a lambda-related declaration which should
1070 be ignored by unqualified lookup. */
1072 bool
1073 is_lambda_ignored_entity (tree val)
1075 /* In unevaluated context, look past normal capture proxies. */
1076 if (cp_unevaluated_operand && is_normal_capture_proxy (val))
1077 return true;
1079 /* Always ignore lambda fields, their names are only for debugging. */
1080 if (TREE_CODE (val) == FIELD_DECL
1081 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
1082 return true;
1084 /* None of the lookups that use qualify_lookup want the op() from the
1085 lambda; they want the one from the enclosing class. */
1086 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
1087 return true;
1089 return false;