annotate gcc/cp/lambda.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Perform the semantic phase of lambda parsing, i.e., the process of
kono
parents:
diff changeset
2 building tree structure, checking semantic consistency, and
kono
parents:
diff changeset
3 building RTL. These routines are used both during actual parsing
kono
parents:
diff changeset
4 and during the instantiation of template functions.
kono
parents:
diff changeset
5
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 This file is part of GCC.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 GCC is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
11 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
12 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
13 any later version.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 GCC is distributed in the hope that it will be useful, but
kono
parents:
diff changeset
16 WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kono
parents:
diff changeset
18 General Public License for more details.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
21 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
22 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 #include "config.h"
kono
parents:
diff changeset
25 #include "system.h"
kono
parents:
diff changeset
26 #include "coretypes.h"
kono
parents:
diff changeset
27 #include "cp-tree.h"
kono
parents:
diff changeset
28 #include "stringpool.h"
kono
parents:
diff changeset
29 #include "cgraph.h"
kono
parents:
diff changeset
30 #include "tree-iterator.h"
kono
parents:
diff changeset
31 #include "toplev.h"
kono
parents:
diff changeset
32 #include "gimplify.h"
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 /* Constructor for a lambda expression. */
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 tree
kono
parents:
diff changeset
37 build_lambda_expr (void)
kono
parents:
diff changeset
38 {
kono
parents:
diff changeset
39 tree lambda = make_node (LAMBDA_EXPR);
kono
parents:
diff changeset
40 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
kono
parents:
diff changeset
41 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
kono
parents:
diff changeset
42 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
kono
parents:
diff changeset
43 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
kono
parents:
diff changeset
44 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
kono
parents:
diff changeset
45 return lambda;
kono
parents:
diff changeset
46 }
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 /* Create the closure object for a LAMBDA_EXPR. */
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 tree
kono
parents:
diff changeset
51 build_lambda_object (tree lambda_expr)
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 /* Build aggregate constructor call.
kono
parents:
diff changeset
54 - cp_parser_braced_list
kono
parents:
diff changeset
55 - cp_parser_functional_cast */
kono
parents:
diff changeset
56 vec<constructor_elt, va_gc> *elts = NULL;
kono
parents:
diff changeset
57 tree node, expr, type;
kono
parents:
diff changeset
58 location_t saved_loc;
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 if (processing_template_decl || lambda_expr == error_mark_node)
kono
parents:
diff changeset
61 return lambda_expr;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 /* Make sure any error messages refer to the lambda-introducer. */
kono
parents:
diff changeset
64 saved_loc = input_location;
kono
parents:
diff changeset
65 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
kono
parents:
diff changeset
68 node;
kono
parents:
diff changeset
69 node = TREE_CHAIN (node))
kono
parents:
diff changeset
70 {
kono
parents:
diff changeset
71 tree field = TREE_PURPOSE (node);
kono
parents:
diff changeset
72 tree val = TREE_VALUE (node);
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 if (field == error_mark_node)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 expr = error_mark_node;
kono
parents:
diff changeset
77 goto out;
kono
parents:
diff changeset
78 }
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 if (TREE_CODE (val) == TREE_LIST)
kono
parents:
diff changeset
81 val = build_x_compound_expr_from_list (val, ELK_INIT,
kono
parents:
diff changeset
82 tf_warning_or_error);
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 if (DECL_P (val))
kono
parents:
diff changeset
85 mark_used (val);
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 /* Mere mortals can't copy arrays with aggregate initialization, so
kono
parents:
diff changeset
88 do some magic to make it work here. */
kono
parents:
diff changeset
89 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
kono
parents:
diff changeset
90 val = build_array_copy (val);
kono
parents:
diff changeset
91 else if (DECL_NORMAL_CAPTURE_P (field)
kono
parents:
diff changeset
92 && !DECL_VLA_CAPTURE_P (field)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
93 && !TYPE_REF_P (TREE_TYPE (field)))
111
kono
parents:
diff changeset
94 {
kono
parents:
diff changeset
95 /* "the entities that are captured by copy are used to
kono
parents:
diff changeset
96 direct-initialize each corresponding non-static data
kono
parents:
diff changeset
97 member of the resulting closure object."
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 There's normally no way to express direct-initialization
kono
parents:
diff changeset
100 from an element of a CONSTRUCTOR, so we build up a special
kono
parents:
diff changeset
101 TARGET_EXPR to bypass the usual copy-initialization. */
kono
parents:
diff changeset
102 val = force_rvalue (val, tf_warning_or_error);
kono
parents:
diff changeset
103 if (TREE_CODE (val) == TARGET_EXPR)
kono
parents:
diff changeset
104 TARGET_EXPR_DIRECT_INIT_P (val) = true;
kono
parents:
diff changeset
105 }
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 expr = build_constructor (init_list_type_node, elts);
kono
parents:
diff changeset
111 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 /* N2927: "[The closure] class type is not an aggregate."
kono
parents:
diff changeset
114 But we briefly treat it as an aggregate to make this simpler. */
kono
parents:
diff changeset
115 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
kono
parents:
diff changeset
116 CLASSTYPE_NON_AGGREGATE (type) = 0;
kono
parents:
diff changeset
117 expr = finish_compound_literal (type, expr, tf_warning_or_error);
kono
parents:
diff changeset
118 CLASSTYPE_NON_AGGREGATE (type) = 1;
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 out:
kono
parents:
diff changeset
121 input_location = saved_loc;
kono
parents:
diff changeset
122 return expr;
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Return an initialized RECORD_TYPE for LAMBDA.
kono
parents:
diff changeset
126 LAMBDA must have its explicit captures already. */
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 tree
kono
parents:
diff changeset
129 begin_lambda_type (tree lambda)
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 tree type;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 {
kono
parents:
diff changeset
134 /* Unique name. This is just like an unnamed class, but we cannot use
kono
parents:
diff changeset
135 make_anon_name because of certain checks against TYPE_UNNAMED_P. */
kono
parents:
diff changeset
136 tree name;
kono
parents:
diff changeset
137 name = make_lambda_name ();
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 /* Create the new RECORD_TYPE for this lambda. */
kono
parents:
diff changeset
140 type = xref_tag (/*tag_code=*/record_type,
kono
parents:
diff changeset
141 name,
kono
parents:
diff changeset
142 /*scope=*/ts_lambda,
kono
parents:
diff changeset
143 /*template_header_p=*/false);
kono
parents:
diff changeset
144 if (type == error_mark_node)
kono
parents:
diff changeset
145 return error_mark_node;
kono
parents:
diff changeset
146 }
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 /* Designate it as a struct so that we can use aggregate initialization. */
kono
parents:
diff changeset
149 CLASSTYPE_DECLARED_CLASS (type) = false;
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 /* Cross-reference the expression and the type. */
kono
parents:
diff changeset
152 LAMBDA_EXPR_CLOSURE (lambda) = type;
kono
parents:
diff changeset
153 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 /* In C++17, assume the closure is literal; we'll clear the flag later if
kono
parents:
diff changeset
156 necessary. */
kono
parents:
diff changeset
157 if (cxx_dialect >= cxx17)
kono
parents:
diff changeset
158 CLASSTYPE_LITERAL_P (type) = true;
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 /* Clear base types. */
kono
parents:
diff changeset
161 xref_basetypes (type, /*bases=*/NULL_TREE);
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 /* Start the class. */
kono
parents:
diff changeset
164 type = begin_class_definition (type);
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 return type;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 /* Returns the type to use for the return type of the operator() of a
kono
parents:
diff changeset
170 closure class. */
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 tree
kono
parents:
diff changeset
173 lambda_return_type (tree expr)
kono
parents:
diff changeset
174 {
kono
parents:
diff changeset
175 if (expr == NULL_TREE)
kono
parents:
diff changeset
176 return void_type_node;
kono
parents:
diff changeset
177 if (type_unknown_p (expr)
kono
parents:
diff changeset
178 || BRACE_ENCLOSED_INITIALIZER_P (expr))
kono
parents:
diff changeset
179 {
kono
parents:
diff changeset
180 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
kono
parents:
diff changeset
181 return error_mark_node;
kono
parents:
diff changeset
182 }
kono
parents:
diff changeset
183 gcc_checking_assert (!type_dependent_expression_p (expr));
kono
parents:
diff changeset
184 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
kono
parents:
diff changeset
188 closure type. */
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 tree
kono
parents:
diff changeset
191 lambda_function (tree lambda)
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 tree type;
kono
parents:
diff changeset
194 if (TREE_CODE (lambda) == LAMBDA_EXPR)
kono
parents:
diff changeset
195 type = LAMBDA_EXPR_CLOSURE (lambda);
kono
parents:
diff changeset
196 else
kono
parents:
diff changeset
197 type = lambda;
kono
parents:
diff changeset
198 gcc_assert (LAMBDA_TYPE_P (type));
kono
parents:
diff changeset
199 /* Don't let debug_tree cause instantiation. */
kono
parents:
diff changeset
200 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
kono
parents:
diff changeset
201 && !COMPLETE_OR_OPEN_TYPE_P (type))
kono
parents:
diff changeset
202 return NULL_TREE;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
203 lambda = lookup_member (type, call_op_identifier,
111
kono
parents:
diff changeset
204 /*protect=*/0, /*want_type=*/false,
kono
parents:
diff changeset
205 tf_warning_or_error);
kono
parents:
diff changeset
206 if (lambda)
kono
parents:
diff changeset
207 lambda = STRIP_TEMPLATE (get_first_fn (lambda));
kono
parents:
diff changeset
208 return lambda;
kono
parents:
diff changeset
209 }
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 /* Returns the type to use for the FIELD_DECL corresponding to the
kono
parents:
diff changeset
212 capture of EXPR. EXPLICIT_INIT_P indicates whether this is a
kono
parents:
diff changeset
213 C++14 init capture, and BY_REFERENCE_P indicates whether we're
kono
parents:
diff changeset
214 capturing by reference. */
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 tree
kono
parents:
diff changeset
217 lambda_capture_field_type (tree expr, bool explicit_init_p,
kono
parents:
diff changeset
218 bool by_reference_p)
kono
parents:
diff changeset
219 {
kono
parents:
diff changeset
220 tree type;
kono
parents:
diff changeset
221 bool is_this = is_this_parameter (tree_strip_nop_conversions (expr));
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 if (!is_this && type_dependent_expression_p (expr))
kono
parents:
diff changeset
224 {
kono
parents:
diff changeset
225 type = cxx_make_type (DECLTYPE_TYPE);
kono
parents:
diff changeset
226 DECLTYPE_TYPE_EXPR (type) = expr;
kono
parents:
diff changeset
227 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
kono
parents:
diff changeset
228 DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p;
kono
parents:
diff changeset
229 DECLTYPE_FOR_REF_CAPTURE (type) = by_reference_p;
kono
parents:
diff changeset
230 SET_TYPE_STRUCTURAL_EQUALITY (type);
kono
parents:
diff changeset
231 }
kono
parents:
diff changeset
232 else if (!is_this && explicit_init_p)
kono
parents:
diff changeset
233 {
kono
parents:
diff changeset
234 tree auto_node = make_auto ();
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 type = auto_node;
kono
parents:
diff changeset
237 if (by_reference_p)
kono
parents:
diff changeset
238 /* Add the reference now, so deduction doesn't lose
kono
parents:
diff changeset
239 outermost CV qualifiers of EXPR. */
kono
parents:
diff changeset
240 type = build_reference_type (type);
kono
parents:
diff changeset
241 type = do_auto_deduction (type, expr, auto_node);
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243 else
kono
parents:
diff changeset
244 {
kono
parents:
diff changeset
245 type = non_reference (unlowered_expr_type (expr));
kono
parents:
diff changeset
246
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
247 if (!is_this
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
248 && (by_reference_p || TREE_CODE (type) == FUNCTION_TYPE))
111
kono
parents:
diff changeset
249 type = build_reference_type (type);
kono
parents:
diff changeset
250 }
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 return type;
kono
parents:
diff changeset
253 }
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 /* Returns true iff DECL is a lambda capture proxy variable created by
kono
parents:
diff changeset
256 build_capture_proxy. */
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 bool
kono
parents:
diff changeset
259 is_capture_proxy (tree decl)
kono
parents:
diff changeset
260 {
kono
parents:
diff changeset
261 return (VAR_P (decl)
kono
parents:
diff changeset
262 && DECL_HAS_VALUE_EXPR_P (decl)
kono
parents:
diff changeset
263 && !DECL_ANON_UNION_VAR_P (decl)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
264 && !DECL_DECOMPOSITION_P (decl)
111
kono
parents:
diff changeset
265 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
kono
parents:
diff changeset
266 }
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 /* Returns true iff DECL is a capture proxy for a normal capture
kono
parents:
diff changeset
269 (i.e. without explicit initializer). */
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 bool
kono
parents:
diff changeset
272 is_normal_capture_proxy (tree decl)
kono
parents:
diff changeset
273 {
kono
parents:
diff changeset
274 if (!is_capture_proxy (decl))
kono
parents:
diff changeset
275 /* It's not a capture proxy. */
kono
parents:
diff changeset
276 return false;
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
kono
parents:
diff changeset
279 /* VLA capture. */
kono
parents:
diff changeset
280 return true;
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 /* It is a capture proxy, is it a normal capture? */
kono
parents:
diff changeset
283 tree val = DECL_VALUE_EXPR (decl);
kono
parents:
diff changeset
284 if (val == error_mark_node)
kono
parents:
diff changeset
285 return true;
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 if (TREE_CODE (val) == ADDR_EXPR)
kono
parents:
diff changeset
288 val = TREE_OPERAND (val, 0);
kono
parents:
diff changeset
289 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
kono
parents:
diff changeset
290 val = TREE_OPERAND (val, 1);
kono
parents:
diff changeset
291 return DECL_NORMAL_CAPTURE_P (val);
kono
parents:
diff changeset
292 }
kono
parents:
diff changeset
293
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
294 /* Returns true iff DECL is a capture proxy for a normal capture
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
295 of a constant variable. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
296
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
297 bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
298 is_constant_capture_proxy (tree decl)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
299 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
300 if (is_normal_capture_proxy (decl))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
301 return decl_constant_var_p (DECL_CAPTURED_VARIABLE (decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
302 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
303 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
304
111
kono
parents:
diff changeset
305 /* VAR is a capture proxy created by build_capture_proxy; add it to the
kono
parents:
diff changeset
306 current function, which is the operator() for the appropriate lambda. */
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 void
kono
parents:
diff changeset
309 insert_capture_proxy (tree var)
kono
parents:
diff changeset
310 {
kono
parents:
diff changeset
311 if (is_normal_capture_proxy (var))
kono
parents:
diff changeset
312 {
kono
parents:
diff changeset
313 tree cap = DECL_CAPTURED_VARIABLE (var);
kono
parents:
diff changeset
314 if (CHECKING_P)
kono
parents:
diff changeset
315 {
kono
parents:
diff changeset
316 gcc_assert (!is_normal_capture_proxy (cap));
kono
parents:
diff changeset
317 tree old = retrieve_local_specialization (cap);
kono
parents:
diff changeset
318 if (old)
kono
parents:
diff changeset
319 gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var));
kono
parents:
diff changeset
320 }
kono
parents:
diff changeset
321 register_local_specialization (var, cap);
kono
parents:
diff changeset
322 }
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 /* Put the capture proxy in the extra body block so that it won't clash
kono
parents:
diff changeset
325 with a later local variable. */
kono
parents:
diff changeset
326 pushdecl_outermost_localscope (var);
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
kono
parents:
diff changeset
329 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
kono
parents:
diff changeset
330 tree stmt_list = (*stmt_list_stack)[1];
kono
parents:
diff changeset
331 gcc_assert (stmt_list);
kono
parents:
diff changeset
332 append_to_statement_list_force (var, &stmt_list);
kono
parents:
diff changeset
333 }
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 /* We've just finished processing a lambda; if the containing scope is also
kono
parents:
diff changeset
336 a lambda, insert any capture proxies that were created while processing
kono
parents:
diff changeset
337 the nested lambda. */
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 void
kono
parents:
diff changeset
340 insert_pending_capture_proxies (void)
kono
parents:
diff changeset
341 {
kono
parents:
diff changeset
342 tree lam;
kono
parents:
diff changeset
343 vec<tree, va_gc> *proxies;
kono
parents:
diff changeset
344 unsigned i;
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
kono
parents:
diff changeset
347 return;
kono
parents:
diff changeset
348
kono
parents:
diff changeset
349 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
kono
parents:
diff changeset
350 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
kono
parents:
diff changeset
351 for (i = 0; i < vec_safe_length (proxies); ++i)
kono
parents:
diff changeset
352 {
kono
parents:
diff changeset
353 tree var = (*proxies)[i];
kono
parents:
diff changeset
354 insert_capture_proxy (var);
kono
parents:
diff changeset
355 }
kono
parents:
diff changeset
356 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
kono
parents:
diff changeset
357 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
kono
parents:
diff changeset
358 }
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
kono
parents:
diff changeset
361 return the type we want the proxy to have: the type of the field itself,
kono
parents:
diff changeset
362 with added const-qualification if the lambda isn't mutable and the
kono
parents:
diff changeset
363 capture is by value. */
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 tree
kono
parents:
diff changeset
366 lambda_proxy_type (tree ref)
kono
parents:
diff changeset
367 {
kono
parents:
diff changeset
368 tree type;
kono
parents:
diff changeset
369 if (ref == error_mark_node)
kono
parents:
diff changeset
370 return error_mark_node;
kono
parents:
diff changeset
371 if (REFERENCE_REF_P (ref))
kono
parents:
diff changeset
372 ref = TREE_OPERAND (ref, 0);
kono
parents:
diff changeset
373 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
kono
parents:
diff changeset
374 type = TREE_TYPE (ref);
kono
parents:
diff changeset
375 if (!type || WILDCARD_TYPE_P (non_reference (type)))
kono
parents:
diff changeset
376 {
kono
parents:
diff changeset
377 type = cxx_make_type (DECLTYPE_TYPE);
kono
parents:
diff changeset
378 DECLTYPE_TYPE_EXPR (type) = ref;
kono
parents:
diff changeset
379 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
kono
parents:
diff changeset
380 SET_TYPE_STRUCTURAL_EQUALITY (type);
kono
parents:
diff changeset
381 }
kono
parents:
diff changeset
382 if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
kono
parents:
diff changeset
383 type = make_pack_expansion (type);
kono
parents:
diff changeset
384 return type;
kono
parents:
diff changeset
385 }
kono
parents:
diff changeset
386
kono
parents:
diff changeset
387 /* MEMBER is a capture field in a lambda closure class. Now that we're
kono
parents:
diff changeset
388 inside the operator(), build a placeholder var for future lookups and
kono
parents:
diff changeset
389 debugging. */
kono
parents:
diff changeset
390
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
391 static tree
111
kono
parents:
diff changeset
392 build_capture_proxy (tree member, tree init)
kono
parents:
diff changeset
393 {
kono
parents:
diff changeset
394 tree var, object, fn, closure, name, lam, type;
kono
parents:
diff changeset
395
kono
parents:
diff changeset
396 if (PACK_EXPANSION_P (member))
kono
parents:
diff changeset
397 member = PACK_EXPANSION_PATTERN (member);
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 closure = DECL_CONTEXT (member);
kono
parents:
diff changeset
400 fn = lambda_function (closure);
kono
parents:
diff changeset
401 lam = CLASSTYPE_LAMBDA_EXPR (closure);
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 /* The proxy variable forwards to the capture field. */
kono
parents:
diff changeset
404 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
kono
parents:
diff changeset
405 object = finish_non_static_data_member (member, object, NULL_TREE);
kono
parents:
diff changeset
406 if (REFERENCE_REF_P (object))
kono
parents:
diff changeset
407 object = TREE_OPERAND (object, 0);
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 /* Remove the __ inserted by add_capture. */
kono
parents:
diff changeset
410 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 type = lambda_proxy_type (object);
kono
parents:
diff changeset
413
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
414 if (name == this_identifier && !INDIRECT_TYPE_P (type))
111
kono
parents:
diff changeset
415 {
kono
parents:
diff changeset
416 type = build_pointer_type (type);
kono
parents:
diff changeset
417 type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
kono
parents:
diff changeset
418 object = build_fold_addr_expr_with_type (object, type);
kono
parents:
diff changeset
419 }
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 if (DECL_VLA_CAPTURE_P (member))
kono
parents:
diff changeset
422 {
kono
parents:
diff changeset
423 /* Rebuild the VLA type from the pointer and maxindex. */
kono
parents:
diff changeset
424 tree field = next_initializable_field (TYPE_FIELDS (type));
kono
parents:
diff changeset
425 tree ptr = build_simple_component_ref (object, field);
kono
parents:
diff changeset
426 field = next_initializable_field (DECL_CHAIN (field));
kono
parents:
diff changeset
427 tree max = build_simple_component_ref (object, field);
kono
parents:
diff changeset
428 type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
kono
parents:
diff changeset
429 build_index_type (max));
kono
parents:
diff changeset
430 type = build_reference_type (type);
kono
parents:
diff changeset
431 REFERENCE_VLA_OK (type) = true;
kono
parents:
diff changeset
432 object = convert (type, ptr);
kono
parents:
diff changeset
433 }
kono
parents:
diff changeset
434
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
435 complete_type (type);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
436
111
kono
parents:
diff changeset
437 var = build_decl (input_location, VAR_DECL, name, type);
kono
parents:
diff changeset
438 SET_DECL_VALUE_EXPR (var, object);
kono
parents:
diff changeset
439 DECL_HAS_VALUE_EXPR_P (var) = 1;
kono
parents:
diff changeset
440 DECL_ARTIFICIAL (var) = 1;
kono
parents:
diff changeset
441 TREE_USED (var) = 1;
kono
parents:
diff changeset
442 DECL_CONTEXT (var) = fn;
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 if (DECL_NORMAL_CAPTURE_P (member))
kono
parents:
diff changeset
445 {
kono
parents:
diff changeset
446 if (DECL_VLA_CAPTURE_P (member))
kono
parents:
diff changeset
447 {
kono
parents:
diff changeset
448 init = CONSTRUCTOR_ELT (init, 0)->value;
kono
parents:
diff changeset
449 init = TREE_OPERAND (init, 0); // Strip ADDR_EXPR.
kono
parents:
diff changeset
450 init = TREE_OPERAND (init, 0); // Strip ARRAY_REF.
kono
parents:
diff changeset
451 }
kono
parents:
diff changeset
452 else
kono
parents:
diff changeset
453 {
kono
parents:
diff changeset
454 if (PACK_EXPANSION_P (init))
kono
parents:
diff changeset
455 init = PACK_EXPANSION_PATTERN (init);
kono
parents:
diff changeset
456 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
457
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
458 if (INDIRECT_REF_P (init))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
459 init = TREE_OPERAND (init, 0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
460 STRIP_NOPS (init);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
461
111
kono
parents:
diff changeset
462 gcc_assert (VAR_P (init) || TREE_CODE (init) == PARM_DECL);
kono
parents:
diff changeset
463 while (is_normal_capture_proxy (init))
kono
parents:
diff changeset
464 init = DECL_CAPTURED_VARIABLE (init);
kono
parents:
diff changeset
465 retrofit_lang_decl (var);
kono
parents:
diff changeset
466 DECL_CAPTURED_VARIABLE (var) = init;
kono
parents:
diff changeset
467 }
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 if (name == this_identifier)
kono
parents:
diff changeset
470 {
kono
parents:
diff changeset
471 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
kono
parents:
diff changeset
472 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
kono
parents:
diff changeset
473 }
kono
parents:
diff changeset
474
kono
parents:
diff changeset
475 if (fn == current_function_decl)
kono
parents:
diff changeset
476 insert_capture_proxy (var);
kono
parents:
diff changeset
477 else
kono
parents:
diff changeset
478 vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 return var;
kono
parents:
diff changeset
481 }
kono
parents:
diff changeset
482
kono
parents:
diff changeset
483 static GTY(()) tree ptr_id;
kono
parents:
diff changeset
484 static GTY(()) tree max_id;
kono
parents:
diff changeset
485
kono
parents:
diff changeset
486 /* Return a struct containing a pointer and a length for lambda capture of
kono
parents:
diff changeset
487 an array of runtime length. */
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 static tree
kono
parents:
diff changeset
490 vla_capture_type (tree array_type)
kono
parents:
diff changeset
491 {
kono
parents:
diff changeset
492 tree type = xref_tag (record_type, make_anon_name (), ts_current, false);
kono
parents:
diff changeset
493 xref_basetypes (type, NULL_TREE);
kono
parents:
diff changeset
494 type = begin_class_definition (type);
kono
parents:
diff changeset
495 if (!ptr_id)
kono
parents:
diff changeset
496 {
kono
parents:
diff changeset
497 ptr_id = get_identifier ("ptr");
kono
parents:
diff changeset
498 max_id = get_identifier ("max");
kono
parents:
diff changeset
499 }
kono
parents:
diff changeset
500 tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
kono
parents:
diff changeset
501 tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
kono
parents:
diff changeset
502 finish_member_declaration (field);
kono
parents:
diff changeset
503 field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
kono
parents:
diff changeset
504 finish_member_declaration (field);
kono
parents:
diff changeset
505 return finish_struct (type, NULL_TREE);
kono
parents:
diff changeset
506 }
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 /* From an ID and INITIALIZER, create a capture (by reference if
kono
parents:
diff changeset
509 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
kono
parents:
diff changeset
510 and return it. If ID is `this', BY_REFERENCE_P says whether
kono
parents:
diff changeset
511 `*this' is captured by reference. */
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 tree
kono
parents:
diff changeset
514 add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
kono
parents:
diff changeset
515 bool explicit_init_p)
kono
parents:
diff changeset
516 {
kono
parents:
diff changeset
517 char *buf;
kono
parents:
diff changeset
518 tree type, member, name;
kono
parents:
diff changeset
519 bool vla = false;
kono
parents:
diff changeset
520 bool variadic = false;
kono
parents:
diff changeset
521 tree initializer = orig_init;
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 if (PACK_EXPANSION_P (initializer))
kono
parents:
diff changeset
524 {
kono
parents:
diff changeset
525 initializer = PACK_EXPANSION_PATTERN (initializer);
kono
parents:
diff changeset
526 variadic = true;
kono
parents:
diff changeset
527 }
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 if (TREE_CODE (initializer) == TREE_LIST
kono
parents:
diff changeset
530 /* A pack expansion might end up with multiple elements. */
kono
parents:
diff changeset
531 && !PACK_EXPANSION_P (TREE_VALUE (initializer)))
kono
parents:
diff changeset
532 initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
kono
parents:
diff changeset
533 tf_warning_or_error);
kono
parents:
diff changeset
534 type = TREE_TYPE (initializer);
kono
parents:
diff changeset
535 if (type == error_mark_node)
kono
parents:
diff changeset
536 return error_mark_node;
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 if (array_of_runtime_bound_p (type))
kono
parents:
diff changeset
539 {
kono
parents:
diff changeset
540 vla = true;
kono
parents:
diff changeset
541 if (!by_reference_p)
kono
parents:
diff changeset
542 error ("array of runtime bound cannot be captured by copy, "
kono
parents:
diff changeset
543 "only by reference");
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 /* For a VLA, we capture the address of the first element and the
kono
parents:
diff changeset
546 maximum index, and then reconstruct the VLA for the proxy. */
kono
parents:
diff changeset
547 tree elt = cp_build_array_ref (input_location, initializer,
kono
parents:
diff changeset
548 integer_zero_node, tf_warning_or_error);
kono
parents:
diff changeset
549 initializer = build_constructor_va (init_list_type_node, 2,
kono
parents:
diff changeset
550 NULL_TREE, build_address (elt),
kono
parents:
diff changeset
551 NULL_TREE, array_type_nelts (type));
kono
parents:
diff changeset
552 type = vla_capture_type (type);
kono
parents:
diff changeset
553 }
kono
parents:
diff changeset
554 else if (!dependent_type_p (type)
kono
parents:
diff changeset
555 && variably_modified_type_p (type, NULL_TREE))
kono
parents:
diff changeset
556 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
557 sorry ("capture of variably-modified type %qT that is not an N3639 array "
111
kono
parents:
diff changeset
558 "of runtime bound", type);
kono
parents:
diff changeset
559 if (TREE_CODE (type) == ARRAY_TYPE
kono
parents:
diff changeset
560 && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
kono
parents:
diff changeset
561 inform (input_location, "because the array element type %qT has "
kono
parents:
diff changeset
562 "variable size", TREE_TYPE (type));
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
563 return error_mark_node;
111
kono
parents:
diff changeset
564 }
kono
parents:
diff changeset
565 else
kono
parents:
diff changeset
566 {
kono
parents:
diff changeset
567 type = lambda_capture_field_type (initializer, explicit_init_p,
kono
parents:
diff changeset
568 by_reference_p);
kono
parents:
diff changeset
569 if (type == error_mark_node)
kono
parents:
diff changeset
570 return error_mark_node;
kono
parents:
diff changeset
571
kono
parents:
diff changeset
572 if (id == this_identifier && !by_reference_p)
kono
parents:
diff changeset
573 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
574 gcc_assert (INDIRECT_TYPE_P (type));
111
kono
parents:
diff changeset
575 type = TREE_TYPE (type);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
576 initializer = cp_build_fold_indirect_ref (initializer);
111
kono
parents:
diff changeset
577 }
kono
parents:
diff changeset
578
kono
parents:
diff changeset
579 if (dependent_type_p (type))
kono
parents:
diff changeset
580 ;
kono
parents:
diff changeset
581 else if (id != this_identifier && by_reference_p)
kono
parents:
diff changeset
582 {
kono
parents:
diff changeset
583 if (!lvalue_p (initializer))
kono
parents:
diff changeset
584 {
kono
parents:
diff changeset
585 error ("cannot capture %qE by reference", initializer);
kono
parents:
diff changeset
586 return error_mark_node;
kono
parents:
diff changeset
587 }
kono
parents:
diff changeset
588 }
kono
parents:
diff changeset
589 else
kono
parents:
diff changeset
590 {
kono
parents:
diff changeset
591 /* Capture by copy requires a complete type. */
kono
parents:
diff changeset
592 type = complete_type (type);
kono
parents:
diff changeset
593 if (!COMPLETE_TYPE_P (type))
kono
parents:
diff changeset
594 {
kono
parents:
diff changeset
595 error ("capture by copy of incomplete type %qT", type);
kono
parents:
diff changeset
596 cxx_incomplete_type_inform (type);
kono
parents:
diff changeset
597 return error_mark_node;
kono
parents:
diff changeset
598 }
kono
parents:
diff changeset
599 }
kono
parents:
diff changeset
600 }
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 /* Add __ to the beginning of the field name so that user code
kono
parents:
diff changeset
603 won't find the field with name lookup. We can't just leave the name
kono
parents:
diff changeset
604 unset because template instantiation uses the name to find
kono
parents:
diff changeset
605 instantiated fields. */
kono
parents:
diff changeset
606 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
kono
parents:
diff changeset
607 buf[1] = buf[0] = '_';
kono
parents:
diff changeset
608 memcpy (buf + 2, IDENTIFIER_POINTER (id),
kono
parents:
diff changeset
609 IDENTIFIER_LENGTH (id) + 1);
kono
parents:
diff changeset
610 name = get_identifier (buf);
kono
parents:
diff changeset
611
kono
parents:
diff changeset
612 /* If TREE_TYPE isn't set, we're still in the introducer, so check
kono
parents:
diff changeset
613 for duplicates. */
kono
parents:
diff changeset
614 if (!LAMBDA_EXPR_CLOSURE (lambda))
kono
parents:
diff changeset
615 {
kono
parents:
diff changeset
616 if (IDENTIFIER_MARKED (name))
kono
parents:
diff changeset
617 {
kono
parents:
diff changeset
618 pedwarn (input_location, 0,
kono
parents:
diff changeset
619 "already captured %qD in lambda expression", id);
kono
parents:
diff changeset
620 return NULL_TREE;
kono
parents:
diff changeset
621 }
kono
parents:
diff changeset
622 IDENTIFIER_MARKED (name) = true;
kono
parents:
diff changeset
623 }
kono
parents:
diff changeset
624
kono
parents:
diff changeset
625 if (variadic)
kono
parents:
diff changeset
626 type = make_pack_expansion (type);
kono
parents:
diff changeset
627
kono
parents:
diff changeset
628 /* Make member variable. */
kono
parents:
diff changeset
629 member = build_decl (input_location, FIELD_DECL, name, type);
kono
parents:
diff changeset
630 DECL_VLA_CAPTURE_P (member) = vla;
kono
parents:
diff changeset
631
kono
parents:
diff changeset
632 if (!explicit_init_p)
kono
parents:
diff changeset
633 /* Normal captures are invisible to name lookup but uses are replaced
kono
parents:
diff changeset
634 with references to the capture field; we implement this by only
kono
parents:
diff changeset
635 really making them invisible in unevaluated context; see
kono
parents:
diff changeset
636 qualify_lookup. For now, let's make explicitly initialized captures
kono
parents:
diff changeset
637 always visible. */
kono
parents:
diff changeset
638 DECL_NORMAL_CAPTURE_P (member) = true;
kono
parents:
diff changeset
639
kono
parents:
diff changeset
640 if (id == this_identifier)
kono
parents:
diff changeset
641 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 /* Add it to the appropriate closure class if we've started it. */
kono
parents:
diff changeset
644 if (current_class_type
kono
parents:
diff changeset
645 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
kono
parents:
diff changeset
646 {
kono
parents:
diff changeset
647 if (COMPLETE_TYPE_P (current_class_type))
kono
parents:
diff changeset
648 internal_error ("trying to capture %qD in instantiation of "
kono
parents:
diff changeset
649 "generic lambda", id);
kono
parents:
diff changeset
650 finish_member_declaration (member);
kono
parents:
diff changeset
651 }
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 tree listmem = member;
kono
parents:
diff changeset
654 if (variadic)
kono
parents:
diff changeset
655 {
kono
parents:
diff changeset
656 listmem = make_pack_expansion (member);
kono
parents:
diff changeset
657 initializer = orig_init;
kono
parents:
diff changeset
658 }
kono
parents:
diff changeset
659 LAMBDA_EXPR_CAPTURE_LIST (lambda)
kono
parents:
diff changeset
660 = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
kono
parents:
diff changeset
661
kono
parents:
diff changeset
662 if (LAMBDA_EXPR_CLOSURE (lambda))
kono
parents:
diff changeset
663 return build_capture_proxy (member, initializer);
kono
parents:
diff changeset
664 /* For explicit captures we haven't started the function yet, so we wait
kono
parents:
diff changeset
665 and build the proxy from cp_parser_lambda_body. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
666 LAMBDA_CAPTURE_EXPLICIT_P (LAMBDA_EXPR_CAPTURE_LIST (lambda)) = true;
111
kono
parents:
diff changeset
667 return NULL_TREE;
kono
parents:
diff changeset
668 }
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 /* Register all the capture members on the list CAPTURES, which is the
kono
parents:
diff changeset
671 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 void
kono
parents:
diff changeset
674 register_capture_members (tree captures)
kono
parents:
diff changeset
675 {
kono
parents:
diff changeset
676 if (captures == NULL_TREE)
kono
parents:
diff changeset
677 return;
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 register_capture_members (TREE_CHAIN (captures));
kono
parents:
diff changeset
680
kono
parents:
diff changeset
681 tree field = TREE_PURPOSE (captures);
kono
parents:
diff changeset
682 if (PACK_EXPANSION_P (field))
kono
parents:
diff changeset
683 field = PACK_EXPANSION_PATTERN (field);
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 /* We set this in add_capture to avoid duplicates. */
kono
parents:
diff changeset
686 IDENTIFIER_MARKED (DECL_NAME (field)) = false;
kono
parents:
diff changeset
687 finish_member_declaration (field);
kono
parents:
diff changeset
688 }
kono
parents:
diff changeset
689
kono
parents:
diff changeset
690 /* Similar to add_capture, except this works on a stack of nested lambdas.
kono
parents:
diff changeset
691 BY_REFERENCE_P in this case is derived from the default capture mode.
kono
parents:
diff changeset
692 Returns the capture for the lambda at the bottom of the stack. */
kono
parents:
diff changeset
693
kono
parents:
diff changeset
694 tree
kono
parents:
diff changeset
695 add_default_capture (tree lambda_stack, tree id, tree initializer)
kono
parents:
diff changeset
696 {
kono
parents:
diff changeset
697 bool this_capture_p = (id == this_identifier);
kono
parents:
diff changeset
698 tree var = NULL_TREE;
kono
parents:
diff changeset
699 tree saved_class_type = current_class_type;
kono
parents:
diff changeset
700
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
701 for (tree node = lambda_stack;
111
kono
parents:
diff changeset
702 node;
kono
parents:
diff changeset
703 node = TREE_CHAIN (node))
kono
parents:
diff changeset
704 {
kono
parents:
diff changeset
705 tree lambda = TREE_VALUE (node);
kono
parents:
diff changeset
706
kono
parents:
diff changeset
707 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
kono
parents:
diff changeset
708 if (DECL_PACK_P (initializer))
kono
parents:
diff changeset
709 initializer = make_pack_expansion (initializer);
kono
parents:
diff changeset
710 var = add_capture (lambda,
kono
parents:
diff changeset
711 id,
kono
parents:
diff changeset
712 initializer,
kono
parents:
diff changeset
713 /*by_reference_p=*/
kono
parents:
diff changeset
714 (this_capture_p
kono
parents:
diff changeset
715 || (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
kono
parents:
diff changeset
716 == CPLD_REFERENCE)),
kono
parents:
diff changeset
717 /*explicit_init_p=*/false);
kono
parents:
diff changeset
718 initializer = convert_from_reference (var);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
719
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
720 /* Warn about deprecated implicit capture of this via [=]. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
721 if (cxx_dialect >= cxx2a
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
722 && this_capture_p
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
723 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_COPY
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
724 && !in_system_header_at (LAMBDA_EXPR_LOCATION (lambda)))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
725 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
726 if (warning_at (LAMBDA_EXPR_LOCATION (lambda), OPT_Wdeprecated,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
727 "implicit capture of %qE via %<[=]%> is deprecated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
728 "in C++20", this_identifier))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
729 inform (LAMBDA_EXPR_LOCATION (lambda), "add explicit %<this%> or "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
730 "%<*this%> capture");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
731 }
111
kono
parents:
diff changeset
732 }
kono
parents:
diff changeset
733
kono
parents:
diff changeset
734 current_class_type = saved_class_type;
kono
parents:
diff changeset
735
kono
parents:
diff changeset
736 return var;
kono
parents:
diff changeset
737 }
kono
parents:
diff changeset
738
kono
parents:
diff changeset
739 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
kono
parents:
diff changeset
740 form of an INDIRECT_REF, possibly adding it through default
kono
parents:
diff changeset
741 capturing, if ADD_CAPTURE_P is true. */
kono
parents:
diff changeset
742
kono
parents:
diff changeset
743 tree
kono
parents:
diff changeset
744 lambda_expr_this_capture (tree lambda, bool add_capture_p)
kono
parents:
diff changeset
745 {
kono
parents:
diff changeset
746 tree result;
kono
parents:
diff changeset
747
kono
parents:
diff changeset
748 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
kono
parents:
diff changeset
749
kono
parents:
diff changeset
750 /* In unevaluated context this isn't an odr-use, so don't capture. */
kono
parents:
diff changeset
751 if (cp_unevaluated_operand)
kono
parents:
diff changeset
752 add_capture_p = false;
kono
parents:
diff changeset
753
kono
parents:
diff changeset
754 /* Try to default capture 'this' if we can. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
755 if (!this_capture)
111
kono
parents:
diff changeset
756 {
kono
parents:
diff changeset
757 tree lambda_stack = NULL_TREE;
kono
parents:
diff changeset
758 tree init = NULL_TREE;
kono
parents:
diff changeset
759
kono
parents:
diff changeset
760 /* If we are in a lambda function, we can move out until we hit:
kono
parents:
diff changeset
761 1. a non-lambda function or NSDMI,
kono
parents:
diff changeset
762 2. a lambda function capturing 'this', or
kono
parents:
diff changeset
763 3. a non-default capturing lambda function. */
kono
parents:
diff changeset
764 for (tree tlambda = lambda; ;)
kono
parents:
diff changeset
765 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
766 if (add_capture_p
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
767 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
768 /* tlambda won't let us capture 'this'. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
769 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
770
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
771 if (add_capture_p)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
772 lambda_stack = tree_cons (NULL_TREE,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
773 tlambda,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
774 lambda_stack);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
775
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
776 tree closure = LAMBDA_EXPR_CLOSURE (tlambda);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
777 tree containing_function
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
778 = decl_function_context (TYPE_NAME (closure));
111
kono
parents:
diff changeset
779
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
780 tree ex = LAMBDA_EXPR_EXTRA_SCOPE (tlambda);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
781 if (ex && TREE_CODE (ex) == FIELD_DECL)
111
kono
parents:
diff changeset
782 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
783 /* Lambda in an NSDMI. We don't have a function to look up
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
784 'this' in, but we can find (or rebuild) the fake one from
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
785 inject_this_parameter. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
786 if (!containing_function && !COMPLETE_TYPE_P (closure))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
787 /* If we're parsing a lambda in a non-local class,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
788 we can find the fake 'this' in scope_chain. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
789 init = scope_chain->x_current_class_ptr;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
790 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
791 /* Otherwise it's either gone or buried in
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
792 function_context_stack, so make another. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
793 init = build_this_parm (NULL_TREE, DECL_CONTEXT (ex),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
794 TYPE_UNQUALIFIED);
111
kono
parents:
diff changeset
795 gcc_checking_assert
kono
parents:
diff changeset
796 (init && (TREE_TYPE (TREE_TYPE (init))
kono
parents:
diff changeset
797 == current_nonlambda_class_type ()));
kono
parents:
diff changeset
798 break;
kono
parents:
diff changeset
799 }
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 if (containing_function == NULL_TREE)
kono
parents:
diff changeset
802 /* We ran out of scopes; there's no 'this' to capture. */
kono
parents:
diff changeset
803 break;
kono
parents:
diff changeset
804
kono
parents:
diff changeset
805 if (!LAMBDA_FUNCTION_P (containing_function))
kono
parents:
diff changeset
806 {
kono
parents:
diff changeset
807 /* We found a non-lambda function. */
kono
parents:
diff changeset
808 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function))
kono
parents:
diff changeset
809 /* First parameter is 'this'. */
kono
parents:
diff changeset
810 init = DECL_ARGUMENTS (containing_function);
kono
parents:
diff changeset
811 break;
kono
parents:
diff changeset
812 }
kono
parents:
diff changeset
813
kono
parents:
diff changeset
814 tlambda
kono
parents:
diff changeset
815 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
kono
parents:
diff changeset
816
kono
parents:
diff changeset
817 if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
kono
parents:
diff changeset
818 {
kono
parents:
diff changeset
819 /* An outer lambda has already captured 'this'. */
kono
parents:
diff changeset
820 init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
kono
parents:
diff changeset
821 break;
kono
parents:
diff changeset
822 }
kono
parents:
diff changeset
823 }
kono
parents:
diff changeset
824
kono
parents:
diff changeset
825 if (init)
kono
parents:
diff changeset
826 {
kono
parents:
diff changeset
827 if (add_capture_p)
kono
parents:
diff changeset
828 this_capture = add_default_capture (lambda_stack,
kono
parents:
diff changeset
829 /*id=*/this_identifier,
kono
parents:
diff changeset
830 init);
kono
parents:
diff changeset
831 else
kono
parents:
diff changeset
832 this_capture = init;
kono
parents:
diff changeset
833 }
kono
parents:
diff changeset
834 }
kono
parents:
diff changeset
835
kono
parents:
diff changeset
836 if (cp_unevaluated_operand)
kono
parents:
diff changeset
837 result = this_capture;
kono
parents:
diff changeset
838 else if (!this_capture)
kono
parents:
diff changeset
839 {
kono
parents:
diff changeset
840 if (add_capture_p)
kono
parents:
diff changeset
841 {
kono
parents:
diff changeset
842 error ("%<this%> was not captured for this lambda function");
kono
parents:
diff changeset
843 result = error_mark_node;
kono
parents:
diff changeset
844 }
kono
parents:
diff changeset
845 else
kono
parents:
diff changeset
846 result = NULL_TREE;
kono
parents:
diff changeset
847 }
kono
parents:
diff changeset
848 else
kono
parents:
diff changeset
849 {
kono
parents:
diff changeset
850 /* To make sure that current_class_ref is for the lambda. */
kono
parents:
diff changeset
851 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
kono
parents:
diff changeset
852 == LAMBDA_EXPR_CLOSURE (lambda));
kono
parents:
diff changeset
853
kono
parents:
diff changeset
854 result = this_capture;
kono
parents:
diff changeset
855
kono
parents:
diff changeset
856 /* If 'this' is captured, each use of 'this' is transformed into an
kono
parents:
diff changeset
857 access to the corresponding unnamed data member of the closure
kono
parents:
diff changeset
858 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
kono
parents:
diff changeset
859 ensures that the transformed expression is an rvalue. ] */
kono
parents:
diff changeset
860 result = rvalue (result);
kono
parents:
diff changeset
861 }
kono
parents:
diff changeset
862
kono
parents:
diff changeset
863 return result;
kono
parents:
diff changeset
864 }
kono
parents:
diff changeset
865
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
866 /* Return the innermost LAMBDA_EXPR we're currently in, if any. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
867
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
868 tree
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
869 current_lambda_expr (void)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
870 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
871 tree type = current_class_type;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
872 while (type && !LAMBDA_TYPE_P (type))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
873 type = decl_type_context (TYPE_NAME (type));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
874 if (type)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
875 return CLASSTYPE_LAMBDA_EXPR (type);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
876 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
877 return NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
878 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
879
111
kono
parents:
diff changeset
880 /* Return the current LAMBDA_EXPR, if this is a resolvable dummy
kono
parents:
diff changeset
881 object. NULL otherwise.. */
kono
parents:
diff changeset
882
kono
parents:
diff changeset
883 static tree
kono
parents:
diff changeset
884 resolvable_dummy_lambda (tree object)
kono
parents:
diff changeset
885 {
kono
parents:
diff changeset
886 if (!is_dummy_object (object))
kono
parents:
diff changeset
887 return NULL_TREE;
kono
parents:
diff changeset
888
kono
parents:
diff changeset
889 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
kono
parents:
diff changeset
890 gcc_assert (!TYPE_PTR_P (type));
kono
parents:
diff changeset
891
kono
parents:
diff changeset
892 if (type != current_class_type
kono
parents:
diff changeset
893 && current_class_type
kono
parents:
diff changeset
894 && LAMBDA_TYPE_P (current_class_type)
kono
parents:
diff changeset
895 && lambda_function (current_class_type)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
896 && DERIVED_FROM_P (type, nonlambda_method_basetype()))
111
kono
parents:
diff changeset
897 return CLASSTYPE_LAMBDA_EXPR (current_class_type);
kono
parents:
diff changeset
898
kono
parents:
diff changeset
899 return NULL_TREE;
kono
parents:
diff changeset
900 }
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 /* We don't want to capture 'this' until we know we need it, i.e. after
kono
parents:
diff changeset
903 overload resolution has chosen a non-static member function. At that
kono
parents:
diff changeset
904 point we call this function to turn a dummy object into a use of the
kono
parents:
diff changeset
905 'this' capture. */
kono
parents:
diff changeset
906
kono
parents:
diff changeset
907 tree
kono
parents:
diff changeset
908 maybe_resolve_dummy (tree object, bool add_capture_p)
kono
parents:
diff changeset
909 {
kono
parents:
diff changeset
910 if (tree lam = resolvable_dummy_lambda (object))
kono
parents:
diff changeset
911 if (tree cap = lambda_expr_this_capture (lam, add_capture_p))
kono
parents:
diff changeset
912 if (cap != error_mark_node)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
913 object = build_fold_indirect_ref (cap);
111
kono
parents:
diff changeset
914
kono
parents:
diff changeset
915 return object;
kono
parents:
diff changeset
916 }
kono
parents:
diff changeset
917
kono
parents:
diff changeset
918 /* When parsing a generic lambda containing an argument-dependent
kono
parents:
diff changeset
919 member function call we defer overload resolution to instantiation
kono
parents:
diff changeset
920 time. But we have to know now whether to capture this or not.
kono
parents:
diff changeset
921 Do that if FNS contains any non-static fns.
kono
parents:
diff changeset
922 The std doesn't anticipate this case, but I expect this to be the
kono
parents:
diff changeset
923 outcome of discussion. */
kono
parents:
diff changeset
924
kono
parents:
diff changeset
925 void
kono
parents:
diff changeset
926 maybe_generic_this_capture (tree object, tree fns)
kono
parents:
diff changeset
927 {
kono
parents:
diff changeset
928 if (tree lam = resolvable_dummy_lambda (object))
kono
parents:
diff changeset
929 if (!LAMBDA_EXPR_THIS_CAPTURE (lam))
kono
parents:
diff changeset
930 {
kono
parents:
diff changeset
931 /* We've not yet captured, so look at the function set of
kono
parents:
diff changeset
932 interest. */
kono
parents:
diff changeset
933 if (BASELINK_P (fns))
kono
parents:
diff changeset
934 fns = BASELINK_FUNCTIONS (fns);
kono
parents:
diff changeset
935 bool id_expr = TREE_CODE (fns) == TEMPLATE_ID_EXPR;
kono
parents:
diff changeset
936 if (id_expr)
kono
parents:
diff changeset
937 fns = TREE_OPERAND (fns, 0);
kono
parents:
diff changeset
938
kono
parents:
diff changeset
939 for (lkp_iterator iter (fns); iter; ++iter)
kono
parents:
diff changeset
940 if ((!id_expr || TREE_CODE (*iter) == TEMPLATE_DECL)
kono
parents:
diff changeset
941 && DECL_NONSTATIC_MEMBER_FUNCTION_P (*iter))
kono
parents:
diff changeset
942 {
kono
parents:
diff changeset
943 /* Found a non-static member. Capture this. */
kono
parents:
diff changeset
944 lambda_expr_this_capture (lam, true);
kono
parents:
diff changeset
945 break;
kono
parents:
diff changeset
946 }
kono
parents:
diff changeset
947 }
kono
parents:
diff changeset
948 }
kono
parents:
diff changeset
949
kono
parents:
diff changeset
950 /* Returns the innermost non-lambda function. */
kono
parents:
diff changeset
951
kono
parents:
diff changeset
952 tree
kono
parents:
diff changeset
953 current_nonlambda_function (void)
kono
parents:
diff changeset
954 {
kono
parents:
diff changeset
955 tree fn = current_function_decl;
kono
parents:
diff changeset
956 while (fn && LAMBDA_FUNCTION_P (fn))
kono
parents:
diff changeset
957 fn = decl_function_context (fn);
kono
parents:
diff changeset
958 return fn;
kono
parents:
diff changeset
959 }
kono
parents:
diff changeset
960
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
961 /* Returns the method basetype of the innermost non-lambda function, including
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
962 a hypothetical constructor if inside an NSDMI, or NULL_TREE if none. */
111
kono
parents:
diff changeset
963
kono
parents:
diff changeset
964 tree
kono
parents:
diff changeset
965 nonlambda_method_basetype (void)
kono
parents:
diff changeset
966 {
kono
parents:
diff changeset
967 if (!current_class_ref)
kono
parents:
diff changeset
968 return NULL_TREE;
kono
parents:
diff changeset
969
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
970 tree type = current_class_type;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
971 if (!type || !LAMBDA_TYPE_P (type))
111
kono
parents:
diff changeset
972 return type;
kono
parents:
diff changeset
973
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
974 while (true)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
975 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
976 tree lam = CLASSTYPE_LAMBDA_EXPR (type);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
977 tree ex = LAMBDA_EXPR_EXTRA_SCOPE (lam);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
978 if (ex && TREE_CODE (ex) == FIELD_DECL)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
979 /* Lambda in an NSDMI. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
980 return DECL_CONTEXT (ex);
111
kono
parents:
diff changeset
981
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
982 tree fn = TYPE_CONTEXT (type);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
983 if (!fn || TREE_CODE (fn) != FUNCTION_DECL
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
984 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
985 /* No enclosing non-lambda method. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
986 return NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
987 if (!LAMBDA_FUNCTION_P (fn))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
988 /* Found an enclosing non-lambda method. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
989 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
990 type = DECL_CONTEXT (fn);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
991 }
111
kono
parents:
diff changeset
992 }
kono
parents:
diff changeset
993
kono
parents:
diff changeset
994 /* Like current_scope, but looking through lambdas. */
kono
parents:
diff changeset
995
kono
parents:
diff changeset
996 tree
kono
parents:
diff changeset
997 current_nonlambda_scope (void)
kono
parents:
diff changeset
998 {
kono
parents:
diff changeset
999 tree scope = current_scope ();
kono
parents:
diff changeset
1000 for (;;)
kono
parents:
diff changeset
1001 {
kono
parents:
diff changeset
1002 if (TREE_CODE (scope) == FUNCTION_DECL
kono
parents:
diff changeset
1003 && LAMBDA_FUNCTION_P (scope))
kono
parents:
diff changeset
1004 {
kono
parents:
diff changeset
1005 scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope));
kono
parents:
diff changeset
1006 continue;
kono
parents:
diff changeset
1007 }
kono
parents:
diff changeset
1008 else if (LAMBDA_TYPE_P (scope))
kono
parents:
diff changeset
1009 {
kono
parents:
diff changeset
1010 scope = CP_TYPE_CONTEXT (scope);
kono
parents:
diff changeset
1011 continue;
kono
parents:
diff changeset
1012 }
kono
parents:
diff changeset
1013 break;
kono
parents:
diff changeset
1014 }
kono
parents:
diff changeset
1015 return scope;
kono
parents:
diff changeset
1016 }
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
kono
parents:
diff changeset
1019 indicated FN and NARGS, but do not initialize the return type or any of the
kono
parents:
diff changeset
1020 argument slots. */
kono
parents:
diff changeset
1021
kono
parents:
diff changeset
1022 static tree
kono
parents:
diff changeset
1023 prepare_op_call (tree fn, int nargs)
kono
parents:
diff changeset
1024 {
kono
parents:
diff changeset
1025 tree t;
kono
parents:
diff changeset
1026
kono
parents:
diff changeset
1027 t = build_vl_exp (CALL_EXPR, nargs + 3);
kono
parents:
diff changeset
1028 CALL_EXPR_FN (t) = fn;
kono
parents:
diff changeset
1029 CALL_EXPR_STATIC_CHAIN (t) = NULL;
kono
parents:
diff changeset
1030
kono
parents:
diff changeset
1031 return t;
kono
parents:
diff changeset
1032 }
kono
parents:
diff changeset
1033
kono
parents:
diff changeset
1034 /* Return true iff CALLOP is the op() for a generic lambda. */
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 bool
kono
parents:
diff changeset
1037 generic_lambda_fn_p (tree callop)
kono
parents:
diff changeset
1038 {
kono
parents:
diff changeset
1039 return (LAMBDA_FUNCTION_P (callop)
kono
parents:
diff changeset
1040 && DECL_TEMPLATE_INFO (callop)
kono
parents:
diff changeset
1041 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
kono
parents:
diff changeset
1042 }
kono
parents:
diff changeset
1043
kono
parents:
diff changeset
1044 /* If the closure TYPE has a static op(), also add a conversion to function
kono
parents:
diff changeset
1045 pointer. */
kono
parents:
diff changeset
1046
kono
parents:
diff changeset
1047 void
kono
parents:
diff changeset
1048 maybe_add_lambda_conv_op (tree type)
kono
parents:
diff changeset
1049 {
kono
parents:
diff changeset
1050 bool nested = (cfun != NULL);
kono
parents:
diff changeset
1051 bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
kono
parents:
diff changeset
1052 tree callop = lambda_function (type);
kono
parents:
diff changeset
1053 tree lam = CLASSTYPE_LAMBDA_EXPR (type);
kono
parents:
diff changeset
1054
kono
parents:
diff changeset
1055 if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE
kono
parents:
diff changeset
1056 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE)
kono
parents:
diff changeset
1057 return;
kono
parents:
diff changeset
1058
kono
parents:
diff changeset
1059 if (processing_template_decl)
kono
parents:
diff changeset
1060 return;
kono
parents:
diff changeset
1061
kono
parents:
diff changeset
1062 bool const generic_lambda_p = generic_lambda_fn_p (callop);
kono
parents:
diff changeset
1063
kono
parents:
diff changeset
1064 if (!generic_lambda_p && DECL_INITIAL (callop) == NULL_TREE)
kono
parents:
diff changeset
1065 {
kono
parents:
diff changeset
1066 /* If the op() wasn't instantiated due to errors, give up. */
kono
parents:
diff changeset
1067 gcc_assert (errorcount || sorrycount);
kono
parents:
diff changeset
1068 return;
kono
parents:
diff changeset
1069 }
kono
parents:
diff changeset
1070
kono
parents:
diff changeset
1071 /* Non-template conversion operators are defined directly with build_call_a
kono
parents:
diff changeset
1072 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
kono
parents:
diff changeset
1073 deferred and the CALL is built in-place. In the case of a deduced return
kono
parents:
diff changeset
1074 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
kono
parents:
diff changeset
1075 the return type is also built in-place. The arguments of DECLTYPE_CALL in
kono
parents:
diff changeset
1076 the return expression may differ in flags from those in the body CALL. In
kono
parents:
diff changeset
1077 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
kono
parents:
diff changeset
1078 the body CALL, but not in DECLTYPE_CALL. */
kono
parents:
diff changeset
1079
kono
parents:
diff changeset
1080 vec<tree, va_gc> *direct_argvec = 0;
kono
parents:
diff changeset
1081 tree decltype_call = 0, call = 0;
kono
parents:
diff changeset
1082 tree optype = TREE_TYPE (callop);
kono
parents:
diff changeset
1083 tree fn_result = TREE_TYPE (optype);
kono
parents:
diff changeset
1084
kono
parents:
diff changeset
1085 tree thisarg = build_nop (TREE_TYPE (DECL_ARGUMENTS (callop)),
kono
parents:
diff changeset
1086 null_pointer_node);
kono
parents:
diff changeset
1087 if (generic_lambda_p)
kono
parents:
diff changeset
1088 {
kono
parents:
diff changeset
1089 ++processing_template_decl;
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 /* Prepare the dependent member call for the static member function
kono
parents:
diff changeset
1092 '_FUN' and, potentially, prepare another call to be used in a decltype
kono
parents:
diff changeset
1093 return expression for a deduced return call op to allow for simple
kono
parents:
diff changeset
1094 implementation of the conversion operator. */
kono
parents:
diff changeset
1095
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1096 tree instance = cp_build_fold_indirect_ref (thisarg);
111
kono
parents:
diff changeset
1097 tree objfn = build_min (COMPONENT_REF, NULL_TREE,
kono
parents:
diff changeset
1098 instance, DECL_NAME (callop), NULL_TREE);
kono
parents:
diff changeset
1099 int nargs = list_length (DECL_ARGUMENTS (callop)) - 1;
kono
parents:
diff changeset
1100
kono
parents:
diff changeset
1101 call = prepare_op_call (objfn, nargs);
kono
parents:
diff changeset
1102 if (type_uses_auto (fn_result))
kono
parents:
diff changeset
1103 decltype_call = prepare_op_call (objfn, nargs);
kono
parents:
diff changeset
1104 }
kono
parents:
diff changeset
1105 else
kono
parents:
diff changeset
1106 {
kono
parents:
diff changeset
1107 direct_argvec = make_tree_vector ();
kono
parents:
diff changeset
1108 direct_argvec->quick_push (thisarg);
kono
parents:
diff changeset
1109 }
kono
parents:
diff changeset
1110
kono
parents:
diff changeset
1111 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
kono
parents:
diff changeset
1112 declare the static member function "_FUN" below. For each arg append to
kono
parents:
diff changeset
1113 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
kono
parents:
diff changeset
1114 call args (for the template case). If a parameter pack is found, expand
kono
parents:
diff changeset
1115 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
kono
parents:
diff changeset
1116
kono
parents:
diff changeset
1117 tree fn_args = NULL_TREE;
kono
parents:
diff changeset
1118 {
kono
parents:
diff changeset
1119 int ix = 0;
kono
parents:
diff changeset
1120 tree src = DECL_CHAIN (DECL_ARGUMENTS (callop));
kono
parents:
diff changeset
1121 tree tgt = NULL;
kono
parents:
diff changeset
1122
kono
parents:
diff changeset
1123 while (src)
kono
parents:
diff changeset
1124 {
kono
parents:
diff changeset
1125 tree new_node = copy_node (src);
kono
parents:
diff changeset
1126
kono
parents:
diff changeset
1127 if (!fn_args)
kono
parents:
diff changeset
1128 fn_args = tgt = new_node;
kono
parents:
diff changeset
1129 else
kono
parents:
diff changeset
1130 {
kono
parents:
diff changeset
1131 TREE_CHAIN (tgt) = new_node;
kono
parents:
diff changeset
1132 tgt = new_node;
kono
parents:
diff changeset
1133 }
kono
parents:
diff changeset
1134
kono
parents:
diff changeset
1135 mark_exp_read (tgt);
kono
parents:
diff changeset
1136
kono
parents:
diff changeset
1137 if (generic_lambda_p)
kono
parents:
diff changeset
1138 {
kono
parents:
diff changeset
1139 /* Avoid capturing variables in this context. */
kono
parents:
diff changeset
1140 ++cp_unevaluated_operand;
kono
parents:
diff changeset
1141 tree a = forward_parm (tgt);
kono
parents:
diff changeset
1142 --cp_unevaluated_operand;
kono
parents:
diff changeset
1143
kono
parents:
diff changeset
1144 CALL_EXPR_ARG (call, ix) = a;
kono
parents:
diff changeset
1145 if (decltype_call)
kono
parents:
diff changeset
1146 CALL_EXPR_ARG (decltype_call, ix) = unshare_expr (a);
kono
parents:
diff changeset
1147
kono
parents:
diff changeset
1148 if (PACK_EXPANSION_P (a))
kono
parents:
diff changeset
1149 /* Set this after unsharing so it's not in decltype_call. */
kono
parents:
diff changeset
1150 PACK_EXPANSION_LOCAL_P (a) = true;
kono
parents:
diff changeset
1151
kono
parents:
diff changeset
1152 ++ix;
kono
parents:
diff changeset
1153 }
kono
parents:
diff changeset
1154 else
kono
parents:
diff changeset
1155 vec_safe_push (direct_argvec, tgt);
kono
parents:
diff changeset
1156
kono
parents:
diff changeset
1157 src = TREE_CHAIN (src);
kono
parents:
diff changeset
1158 }
kono
parents:
diff changeset
1159 }
kono
parents:
diff changeset
1160
kono
parents:
diff changeset
1161 if (generic_lambda_p)
kono
parents:
diff changeset
1162 {
kono
parents:
diff changeset
1163 if (decltype_call)
kono
parents:
diff changeset
1164 {
kono
parents:
diff changeset
1165 fn_result = finish_decltype_type
kono
parents:
diff changeset
1166 (decltype_call, /*id_expression_or_member_access_p=*/false,
kono
parents:
diff changeset
1167 tf_warning_or_error);
kono
parents:
diff changeset
1168 }
kono
parents:
diff changeset
1169 }
kono
parents:
diff changeset
1170 else
kono
parents:
diff changeset
1171 call = build_call_a (callop,
kono
parents:
diff changeset
1172 direct_argvec->length (),
kono
parents:
diff changeset
1173 direct_argvec->address ());
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 CALL_FROM_THUNK_P (call) = 1;
kono
parents:
diff changeset
1176 SET_EXPR_LOCATION (call, UNKNOWN_LOCATION);
kono
parents:
diff changeset
1177
kono
parents:
diff changeset
1178 tree stattype = build_function_type (fn_result, FUNCTION_ARG_CHAIN (callop));
kono
parents:
diff changeset
1179 stattype = (cp_build_type_attribute_variant
kono
parents:
diff changeset
1180 (stattype, TYPE_ATTRIBUTES (optype)));
kono
parents:
diff changeset
1181 if (flag_noexcept_type
kono
parents:
diff changeset
1182 && TYPE_NOTHROW_P (TREE_TYPE (callop)))
kono
parents:
diff changeset
1183 stattype = build_exception_variant (stattype, noexcept_true_spec);
kono
parents:
diff changeset
1184
kono
parents:
diff changeset
1185 if (generic_lambda_p)
kono
parents:
diff changeset
1186 --processing_template_decl;
kono
parents:
diff changeset
1187
kono
parents:
diff changeset
1188 /* First build up the conversion op. */
kono
parents:
diff changeset
1189
kono
parents:
diff changeset
1190 tree rettype = build_pointer_type (stattype);
kono
parents:
diff changeset
1191 tree name = make_conv_op_name (rettype);
kono
parents:
diff changeset
1192 tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
kono
parents:
diff changeset
1193 tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
kono
parents:
diff changeset
1194 tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1195 SET_DECL_LANGUAGE (convfn, lang_cplusplus);
111
kono
parents:
diff changeset
1196 tree fn = convfn;
kono
parents:
diff changeset
1197 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
kono
parents:
diff changeset
1198 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
kono
parents:
diff changeset
1199 grokclassfn (type, fn, NO_SPECIAL);
kono
parents:
diff changeset
1200 set_linkage_according_to_type (type, fn);
kono
parents:
diff changeset
1201 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
kono
parents:
diff changeset
1202 DECL_IN_AGGR_P (fn) = 1;
kono
parents:
diff changeset
1203 DECL_ARTIFICIAL (fn) = 1;
kono
parents:
diff changeset
1204 DECL_NOT_REALLY_EXTERN (fn) = 1;
kono
parents:
diff changeset
1205 DECL_DECLARED_INLINE_P (fn) = 1;
kono
parents:
diff changeset
1206 DECL_ARGUMENTS (fn) = build_this_parm (fn, fntype, TYPE_QUAL_CONST);
kono
parents:
diff changeset
1207
kono
parents:
diff changeset
1208 if (nested_def)
kono
parents:
diff changeset
1209 DECL_INTERFACE_KNOWN (fn) = 1;
kono
parents:
diff changeset
1210
kono
parents:
diff changeset
1211 if (generic_lambda_p)
kono
parents:
diff changeset
1212 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
kono
parents:
diff changeset
1213
kono
parents:
diff changeset
1214 add_method (type, fn, false);
kono
parents:
diff changeset
1215
kono
parents:
diff changeset
1216 /* Generic thunk code fails for varargs; we'll complain in mark_used if
kono
parents:
diff changeset
1217 the conversion op is used. */
kono
parents:
diff changeset
1218 if (varargs_function_p (callop))
kono
parents:
diff changeset
1219 {
kono
parents:
diff changeset
1220 DECL_DELETED_FN (fn) = 1;
kono
parents:
diff changeset
1221 return;
kono
parents:
diff changeset
1222 }
kono
parents:
diff changeset
1223
kono
parents:
diff changeset
1224 /* Now build up the thunk to be returned. */
kono
parents:
diff changeset
1225
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1226 tree statfn = build_lang_decl (FUNCTION_DECL, fun_identifier, stattype);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1227 SET_DECL_LANGUAGE (statfn, lang_cplusplus);
111
kono
parents:
diff changeset
1228 fn = statfn;
kono
parents:
diff changeset
1229 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
kono
parents:
diff changeset
1230 grokclassfn (type, fn, NO_SPECIAL);
kono
parents:
diff changeset
1231 set_linkage_according_to_type (type, fn);
kono
parents:
diff changeset
1232 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
kono
parents:
diff changeset
1233 DECL_IN_AGGR_P (fn) = 1;
kono
parents:
diff changeset
1234 DECL_ARTIFICIAL (fn) = 1;
kono
parents:
diff changeset
1235 DECL_NOT_REALLY_EXTERN (fn) = 1;
kono
parents:
diff changeset
1236 DECL_DECLARED_INLINE_P (fn) = 1;
kono
parents:
diff changeset
1237 DECL_STATIC_FUNCTION_P (fn) = 1;
kono
parents:
diff changeset
1238 DECL_ARGUMENTS (fn) = fn_args;
kono
parents:
diff changeset
1239 for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
kono
parents:
diff changeset
1240 {
kono
parents:
diff changeset
1241 /* Avoid duplicate -Wshadow warnings. */
kono
parents:
diff changeset
1242 DECL_NAME (arg) = NULL_TREE;
kono
parents:
diff changeset
1243 DECL_CONTEXT (arg) = fn;
kono
parents:
diff changeset
1244 }
kono
parents:
diff changeset
1245 if (nested_def)
kono
parents:
diff changeset
1246 DECL_INTERFACE_KNOWN (fn) = 1;
kono
parents:
diff changeset
1247
kono
parents:
diff changeset
1248 if (generic_lambda_p)
kono
parents:
diff changeset
1249 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
kono
parents:
diff changeset
1250
kono
parents:
diff changeset
1251 if (flag_sanitize & SANITIZE_NULL)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1252 /* Don't UBsan this function; we're deliberately calling op() with a null
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1253 object argument. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1254 add_no_sanitize_value (fn, SANITIZE_UNDEFINED);
111
kono
parents:
diff changeset
1255
kono
parents:
diff changeset
1256 add_method (type, fn, false);
kono
parents:
diff changeset
1257
kono
parents:
diff changeset
1258 if (nested)
kono
parents:
diff changeset
1259 push_function_context ();
kono
parents:
diff changeset
1260 else
kono
parents:
diff changeset
1261 /* Still increment function_depth so that we don't GC in the
kono
parents:
diff changeset
1262 middle of an expression. */
kono
parents:
diff changeset
1263 ++function_depth;
kono
parents:
diff changeset
1264
kono
parents:
diff changeset
1265 /* Generate the body of the thunk. */
kono
parents:
diff changeset
1266
kono
parents:
diff changeset
1267 start_preparsed_function (statfn, NULL_TREE,
kono
parents:
diff changeset
1268 SF_PRE_PARSED | SF_INCLASS_INLINE);
kono
parents:
diff changeset
1269 if (DECL_ONE_ONLY (statfn))
kono
parents:
diff changeset
1270 {
kono
parents:
diff changeset
1271 /* Put the thunk in the same comdat group as the call op. */
kono
parents:
diff changeset
1272 cgraph_node::get_create (statfn)->add_to_same_comdat_group
kono
parents:
diff changeset
1273 (cgraph_node::get_create (callop));
kono
parents:
diff changeset
1274 }
kono
parents:
diff changeset
1275 tree body = begin_function_body ();
kono
parents:
diff changeset
1276 tree compound_stmt = begin_compound_stmt (0);
kono
parents:
diff changeset
1277 if (!generic_lambda_p)
kono
parents:
diff changeset
1278 {
kono
parents:
diff changeset
1279 set_flags_from_callee (call);
kono
parents:
diff changeset
1280 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
kono
parents:
diff changeset
1281 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
kono
parents:
diff changeset
1282 }
kono
parents:
diff changeset
1283 call = convert_from_reference (call);
kono
parents:
diff changeset
1284 finish_return_stmt (call);
kono
parents:
diff changeset
1285
kono
parents:
diff changeset
1286 finish_compound_stmt (compound_stmt);
kono
parents:
diff changeset
1287 finish_function_body (body);
kono
parents:
diff changeset
1288
kono
parents:
diff changeset
1289 fn = finish_function (/*inline_p=*/true);
kono
parents:
diff changeset
1290 if (!generic_lambda_p)
kono
parents:
diff changeset
1291 expand_or_defer_fn (fn);
kono
parents:
diff changeset
1292
kono
parents:
diff changeset
1293 /* Generate the body of the conversion op. */
kono
parents:
diff changeset
1294
kono
parents:
diff changeset
1295 start_preparsed_function (convfn, NULL_TREE,
kono
parents:
diff changeset
1296 SF_PRE_PARSED | SF_INCLASS_INLINE);
kono
parents:
diff changeset
1297 body = begin_function_body ();
kono
parents:
diff changeset
1298 compound_stmt = begin_compound_stmt (0);
kono
parents:
diff changeset
1299
kono
parents:
diff changeset
1300 /* decl_needed_p needs to see that it's used. */
kono
parents:
diff changeset
1301 TREE_USED (statfn) = 1;
kono
parents:
diff changeset
1302 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
kono
parents:
diff changeset
1303
kono
parents:
diff changeset
1304 finish_compound_stmt (compound_stmt);
kono
parents:
diff changeset
1305 finish_function_body (body);
kono
parents:
diff changeset
1306
kono
parents:
diff changeset
1307 fn = finish_function (/*inline_p=*/true);
kono
parents:
diff changeset
1308 if (!generic_lambda_p)
kono
parents:
diff changeset
1309 expand_or_defer_fn (fn);
kono
parents:
diff changeset
1310
kono
parents:
diff changeset
1311 if (nested)
kono
parents:
diff changeset
1312 pop_function_context ();
kono
parents:
diff changeset
1313 else
kono
parents:
diff changeset
1314 --function_depth;
kono
parents:
diff changeset
1315 }
kono
parents:
diff changeset
1316
kono
parents:
diff changeset
1317 /* True if FN is the static function "_FUN" that gets returned from the lambda
kono
parents:
diff changeset
1318 conversion operator. */
kono
parents:
diff changeset
1319
kono
parents:
diff changeset
1320 bool
kono
parents:
diff changeset
1321 lambda_static_thunk_p (tree fn)
kono
parents:
diff changeset
1322 {
kono
parents:
diff changeset
1323 return (fn && TREE_CODE (fn) == FUNCTION_DECL
kono
parents:
diff changeset
1324 && DECL_ARTIFICIAL (fn)
kono
parents:
diff changeset
1325 && DECL_STATIC_FUNCTION_P (fn)
kono
parents:
diff changeset
1326 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn)));
kono
parents:
diff changeset
1327 }
kono
parents:
diff changeset
1328
kono
parents:
diff changeset
1329 /* Returns true iff VAL is a lambda-related declaration which should
kono
parents:
diff changeset
1330 be ignored by unqualified lookup. */
kono
parents:
diff changeset
1331
kono
parents:
diff changeset
1332 bool
kono
parents:
diff changeset
1333 is_lambda_ignored_entity (tree val)
kono
parents:
diff changeset
1334 {
kono
parents:
diff changeset
1335 /* Look past normal capture proxies. */
kono
parents:
diff changeset
1336 if (is_normal_capture_proxy (val))
kono
parents:
diff changeset
1337 return true;
kono
parents:
diff changeset
1338
kono
parents:
diff changeset
1339 /* Always ignore lambda fields, their names are only for debugging. */
kono
parents:
diff changeset
1340 if (TREE_CODE (val) == FIELD_DECL
kono
parents:
diff changeset
1341 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
kono
parents:
diff changeset
1342 return true;
kono
parents:
diff changeset
1343
kono
parents:
diff changeset
1344 /* None of the lookups that use qualify_lookup want the op() from the
kono
parents:
diff changeset
1345 lambda; they want the one from the enclosing class. */
kono
parents:
diff changeset
1346 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
kono
parents:
diff changeset
1347 return true;
kono
parents:
diff changeset
1348
kono
parents:
diff changeset
1349 return false;
kono
parents:
diff changeset
1350 }
kono
parents:
diff changeset
1351
kono
parents:
diff changeset
1352 /* Lambdas that appear in variable initializer or default argument scope
kono
parents:
diff changeset
1353 get that in their mangling, so we need to record it. We might as well
kono
parents:
diff changeset
1354 use the count for function and namespace scopes as well. */
kono
parents:
diff changeset
1355 static GTY(()) tree lambda_scope;
kono
parents:
diff changeset
1356 static GTY(()) int lambda_count;
kono
parents:
diff changeset
1357 struct GTY(()) tree_int
kono
parents:
diff changeset
1358 {
kono
parents:
diff changeset
1359 tree t;
kono
parents:
diff changeset
1360 int i;
kono
parents:
diff changeset
1361 };
kono
parents:
diff changeset
1362 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
kono
parents:
diff changeset
1363
kono
parents:
diff changeset
1364 void
kono
parents:
diff changeset
1365 start_lambda_scope (tree decl)
kono
parents:
diff changeset
1366 {
kono
parents:
diff changeset
1367 tree_int ti;
kono
parents:
diff changeset
1368 gcc_assert (decl);
kono
parents:
diff changeset
1369 /* Once we're inside a function, we ignore variable scope and just push
kono
parents:
diff changeset
1370 the function again so that popping works properly. */
kono
parents:
diff changeset
1371 if (current_function_decl && TREE_CODE (decl) == VAR_DECL)
kono
parents:
diff changeset
1372 decl = current_function_decl;
kono
parents:
diff changeset
1373 ti.t = lambda_scope;
kono
parents:
diff changeset
1374 ti.i = lambda_count;
kono
parents:
diff changeset
1375 vec_safe_push (lambda_scope_stack, ti);
kono
parents:
diff changeset
1376 if (lambda_scope != decl)
kono
parents:
diff changeset
1377 {
kono
parents:
diff changeset
1378 /* Don't reset the count if we're still in the same function. */
kono
parents:
diff changeset
1379 lambda_scope = decl;
kono
parents:
diff changeset
1380 lambda_count = 0;
kono
parents:
diff changeset
1381 }
kono
parents:
diff changeset
1382 }
kono
parents:
diff changeset
1383
kono
parents:
diff changeset
1384 void
kono
parents:
diff changeset
1385 record_lambda_scope (tree lambda)
kono
parents:
diff changeset
1386 {
kono
parents:
diff changeset
1387 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
kono
parents:
diff changeset
1388 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
kono
parents:
diff changeset
1389 }
kono
parents:
diff changeset
1390
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1391 /* This lambda is an instantiation of a lambda in a template default argument
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1392 that got no LAMBDA_EXPR_EXTRA_SCOPE, so this shouldn't either. But we do
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1393 need to use and increment the global count to avoid collisions. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1394
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1395 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1396 record_null_lambda_scope (tree lambda)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1397 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1398 if (vec_safe_is_empty (lambda_scope_stack))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1399 record_lambda_scope (lambda);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1400 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1401 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1402 tree_int *p = lambda_scope_stack->begin();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1403 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = p->t;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1404 LAMBDA_EXPR_DISCRIMINATOR (lambda) = p->i++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1405 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1406 gcc_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda) == NULL_TREE);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1407 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1408
111
kono
parents:
diff changeset
1409 void
kono
parents:
diff changeset
1410 finish_lambda_scope (void)
kono
parents:
diff changeset
1411 {
kono
parents:
diff changeset
1412 tree_int *p = &lambda_scope_stack->last ();
kono
parents:
diff changeset
1413 if (lambda_scope != p->t)
kono
parents:
diff changeset
1414 {
kono
parents:
diff changeset
1415 lambda_scope = p->t;
kono
parents:
diff changeset
1416 lambda_count = p->i;
kono
parents:
diff changeset
1417 }
kono
parents:
diff changeset
1418 lambda_scope_stack->pop ();
kono
parents:
diff changeset
1419 }
kono
parents:
diff changeset
1420
kono
parents:
diff changeset
1421 tree
kono
parents:
diff changeset
1422 start_lambda_function (tree fco, tree lambda_expr)
kono
parents:
diff changeset
1423 {
kono
parents:
diff changeset
1424 /* Let the front end know that we are going to be defining this
kono
parents:
diff changeset
1425 function. */
kono
parents:
diff changeset
1426 start_preparsed_function (fco,
kono
parents:
diff changeset
1427 NULL_TREE,
kono
parents:
diff changeset
1428 SF_PRE_PARSED | SF_INCLASS_INLINE);
kono
parents:
diff changeset
1429
kono
parents:
diff changeset
1430 tree body = begin_function_body ();
kono
parents:
diff changeset
1431
kono
parents:
diff changeset
1432 /* Push the proxies for any explicit captures. */
kono
parents:
diff changeset
1433 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
kono
parents:
diff changeset
1434 cap = TREE_CHAIN (cap))
kono
parents:
diff changeset
1435 build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap));
kono
parents:
diff changeset
1436
kono
parents:
diff changeset
1437 return body;
kono
parents:
diff changeset
1438 }
kono
parents:
diff changeset
1439
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1440 /* Subroutine of prune_lambda_captures: CAP is a node in
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1441 LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1442 might optimize away the capture, or NULL_TREE if there is no such
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1443 variable. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1444
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1445 static tree
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1446 var_to_maybe_prune (tree cap)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1447 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1448 if (LAMBDA_CAPTURE_EXPLICIT_P (cap))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1449 /* Don't prune explicit captures. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1450 return NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1451
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1452 tree mem = TREE_PURPOSE (cap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1453 if (!DECL_P (mem) || !DECL_NORMAL_CAPTURE_P (mem))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1454 /* Packs and init-captures aren't captures of constant vars. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1455 return NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1456
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1457 tree init = TREE_VALUE (cap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1458 if (is_normal_capture_proxy (init))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1459 init = DECL_CAPTURED_VARIABLE (init);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1460 if (decl_constant_var_p (init))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1461 return init;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1462
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1463 return NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1464 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1465
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1466 /* walk_tree helper for prune_lambda_captures: Remember which capture proxies
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1467 for constant variables are actually used in the lambda body.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1468
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1469 There will always be a DECL_EXPR for the capture proxy; remember it when we
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1470 see it, but replace it with any other use. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1471
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1472 static tree
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1473 mark_const_cap_r (tree *t, int *walk_subtrees, void *data)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1474 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1475 hash_map<tree,tree*> &const_vars = *(hash_map<tree,tree*>*)data;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1476
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1477 tree var = NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1478 if (TREE_CODE (*t) == DECL_EXPR)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1479 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1480 tree decl = DECL_EXPR_DECL (*t);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1481 if (is_constant_capture_proxy (decl))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1482 var = DECL_CAPTURED_VARIABLE (decl);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1483 *walk_subtrees = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1484 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1485 else if (is_constant_capture_proxy (*t))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1486 var = DECL_CAPTURED_VARIABLE (*t);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1487
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1488 if (var)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1489 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1490 tree *&slot = const_vars.get_or_insert (var);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1491 if (!slot || VAR_P (*t))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1492 slot = t;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1493 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1494
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1495 return NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1496 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1497
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1498 /* We're at the end of processing a lambda; go back and remove any captures of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1499 constant variables for which we've folded away all uses. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1500
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1501 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1502 prune_lambda_captures (tree body)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1503 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1504 tree lam = current_lambda_expr ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1505 if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1506 /* No uses were optimized away. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1507 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1508 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1509 /* No default captures, and we don't prune explicit captures. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1510 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1511
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1512 hash_map<tree,tree*> const_vars;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1513
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1514 cp_walk_tree_without_duplicates (&body, mark_const_cap_r, &const_vars);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1515
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1516 tree *fieldp = &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1517 for (tree *capp = &LAMBDA_EXPR_CAPTURE_LIST (lam); *capp; )
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1518 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1519 tree cap = *capp;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1520 if (tree var = var_to_maybe_prune (cap))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1521 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1522 tree **use = const_vars.get (var);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1523 if (use && TREE_CODE (**use) == DECL_EXPR)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1524 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1525 /* All uses of this capture were folded away, leaving only the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1526 proxy declaration. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1527
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1528 /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1529 *capp = TREE_CHAIN (cap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1530
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1531 /* And out of TYPE_FIELDS. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1532 tree field = TREE_PURPOSE (cap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1533 while (*fieldp != field)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1534 fieldp = &DECL_CHAIN (*fieldp);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1535 *fieldp = DECL_CHAIN (*fieldp);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1536
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1537 /* And remove the capture proxy declaration. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1538 **use = void_node;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1539 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1540 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1541 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1542
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1543 capp = &TREE_CHAIN (cap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1544 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1545 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1546
111
kono
parents:
diff changeset
1547 void
kono
parents:
diff changeset
1548 finish_lambda_function (tree body)
kono
parents:
diff changeset
1549 {
kono
parents:
diff changeset
1550 finish_function_body (body);
kono
parents:
diff changeset
1551
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1552 prune_lambda_captures (body);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1553
111
kono
parents:
diff changeset
1554 /* Finish the function and generate code for it if necessary. */
kono
parents:
diff changeset
1555 tree fn = finish_function (/*inline_p=*/true);
kono
parents:
diff changeset
1556
kono
parents:
diff changeset
1557 /* Only expand if the call op is not a template. */
kono
parents:
diff changeset
1558 if (!DECL_TEMPLATE_INFO (fn))
kono
parents:
diff changeset
1559 expand_or_defer_fn (fn);
kono
parents:
diff changeset
1560 }
kono
parents:
diff changeset
1561
kono
parents:
diff changeset
1562 #include "gt-cp-lambda.h"