lambda.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /* Perform the semantic phase of lambda parsing, i.e., the process of
  2. building tree structure, checking semantic consistency, and
  3. building RTL. These routines are used both during actual parsing
  4. and during the instantiation of template functions.
  5. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. GCC is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "options.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "stringpool.h"
  33. #include "hash-map.h"
  34. #include "is-a.h"
  35. #include "plugin-api.h"
  36. #include "tm.h"
  37. #include "hard-reg-set.h"
  38. #include "input.h"
  39. #include "function.h"
  40. #include "ipa-ref.h"
  41. #include "cgraph.h"
  42. #include "tree-iterator.h"
  43. #include "cp-tree.h"
  44. #include "toplev.h"
  45. /* Constructor for a lambda expression. */
  46. tree
  47. build_lambda_expr (void)
  48. {
  49. tree lambda = make_node (LAMBDA_EXPR);
  50. LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
  51. LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
  52. LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
  53. LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
  54. LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
  55. LAMBDA_EXPR_MUTABLE_P (lambda) = false;
  56. return lambda;
  57. }
  58. /* Create the closure object for a LAMBDA_EXPR. */
  59. tree
  60. build_lambda_object (tree lambda_expr)
  61. {
  62. /* Build aggregate constructor call.
  63. - cp_parser_braced_list
  64. - cp_parser_functional_cast */
  65. vec<constructor_elt, va_gc> *elts = NULL;
  66. tree node, expr, type;
  67. location_t saved_loc;
  68. if (processing_template_decl)
  69. return lambda_expr;
  70. /* Make sure any error messages refer to the lambda-introducer. */
  71. saved_loc = input_location;
  72. input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
  73. for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
  74. node;
  75. node = TREE_CHAIN (node))
  76. {
  77. tree field = TREE_PURPOSE (node);
  78. tree val = TREE_VALUE (node);
  79. if (field == error_mark_node)
  80. {
  81. expr = error_mark_node;
  82. goto out;
  83. }
  84. if (DECL_P (val))
  85. mark_used (val);
  86. /* Mere mortals can't copy arrays with aggregate initialization, so
  87. do some magic to make it work here. */
  88. if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
  89. val = build_array_copy (val);
  90. else if (DECL_NORMAL_CAPTURE_P (field)
  91. && !DECL_VLA_CAPTURE_P (field)
  92. && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
  93. {
  94. /* "the entities that are captured by copy are used to
  95. direct-initialize each corresponding non-static data
  96. member of the resulting closure object."
  97. There's normally no way to express direct-initialization
  98. from an element of a CONSTRUCTOR, so we build up a special
  99. TARGET_EXPR to bypass the usual copy-initialization. */
  100. val = force_rvalue (val, tf_warning_or_error);
  101. if (TREE_CODE (val) == TARGET_EXPR)
  102. TARGET_EXPR_DIRECT_INIT_P (val) = true;
  103. }
  104. CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
  105. }
  106. expr = build_constructor (init_list_type_node, elts);
  107. CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
  108. /* N2927: "[The closure] class type is not an aggregate."
  109. But we briefly treat it as an aggregate to make this simpler. */
  110. type = LAMBDA_EXPR_CLOSURE (lambda_expr);
  111. CLASSTYPE_NON_AGGREGATE (type) = 0;
  112. expr = finish_compound_literal (type, expr, tf_warning_or_error);
  113. CLASSTYPE_NON_AGGREGATE (type) = 1;
  114. out:
  115. input_location = saved_loc;
  116. return expr;
  117. }
  118. /* Return an initialized RECORD_TYPE for LAMBDA.
  119. LAMBDA must have its explicit captures already. */
  120. tree
  121. begin_lambda_type (tree lambda)
  122. {
  123. tree type;
  124. {
  125. /* Unique name. This is just like an unnamed class, but we cannot use
  126. make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */
  127. tree name;
  128. name = make_lambda_name ();
  129. /* Create the new RECORD_TYPE for this lambda. */
  130. type = xref_tag (/*tag_code=*/record_type,
  131. name,
  132. /*scope=*/ts_lambda,
  133. /*template_header_p=*/false);
  134. if (type == error_mark_node)
  135. return error_mark_node;
  136. }
  137. /* Designate it as a struct so that we can use aggregate initialization. */
  138. CLASSTYPE_DECLARED_CLASS (type) = false;
  139. /* Cross-reference the expression and the type. */
  140. LAMBDA_EXPR_CLOSURE (lambda) = type;
  141. CLASSTYPE_LAMBDA_EXPR (type) = lambda;
  142. /* Clear base types. */
  143. xref_basetypes (type, /*bases=*/NULL_TREE);
  144. /* Start the class. */
  145. type = begin_class_definition (type);
  146. return type;
  147. }
  148. /* Returns the type to use for the return type of the operator() of a
  149. closure class. */
  150. tree
  151. lambda_return_type (tree expr)
  152. {
  153. if (expr == NULL_TREE)
  154. return void_type_node;
  155. if (type_unknown_p (expr)
  156. || BRACE_ENCLOSED_INITIALIZER_P (expr))
  157. {
  158. cxx_incomplete_type_error (expr, TREE_TYPE (expr));
  159. return void_type_node;
  160. }
  161. gcc_checking_assert (!type_dependent_expression_p (expr));
  162. return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
  163. }
  164. /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
  165. closure type. */
  166. tree
  167. lambda_function (tree lambda)
  168. {
  169. tree type;
  170. if (TREE_CODE (lambda) == LAMBDA_EXPR)
  171. type = LAMBDA_EXPR_CLOSURE (lambda);
  172. else
  173. type = lambda;
  174. gcc_assert (LAMBDA_TYPE_P (type));
  175. /* Don't let debug_tree cause instantiation. */
  176. if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
  177. && !COMPLETE_OR_OPEN_TYPE_P (type))
  178. return NULL_TREE;
  179. lambda = lookup_member (type, ansi_opname (CALL_EXPR),
  180. /*protect=*/0, /*want_type=*/false,
  181. tf_warning_or_error);
  182. if (lambda)
  183. lambda = STRIP_TEMPLATE (get_first_fn (lambda));
  184. return lambda;
  185. }
  186. /* Returns the type to use for the FIELD_DECL corresponding to the
  187. capture of EXPR.
  188. The caller should add REFERENCE_TYPE for capture by reference. */
  189. tree
  190. lambda_capture_field_type (tree expr, bool explicit_init_p)
  191. {
  192. tree type;
  193. if (explicit_init_p)
  194. {
  195. type = make_auto ();
  196. type = do_auto_deduction (type, expr, type);
  197. }
  198. else
  199. type = non_reference (unlowered_expr_type (expr));
  200. if (type_dependent_expression_p (expr)
  201. && !is_this_parameter (tree_strip_nop_conversions (expr)))
  202. {
  203. type = cxx_make_type (DECLTYPE_TYPE);
  204. DECLTYPE_TYPE_EXPR (type) = expr;
  205. DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
  206. DECLTYPE_FOR_INIT_CAPTURE (type) = explicit_init_p;
  207. SET_TYPE_STRUCTURAL_EQUALITY (type);
  208. }
  209. return type;
  210. }
  211. /* Returns true iff DECL is a lambda capture proxy variable created by
  212. build_capture_proxy. */
  213. bool
  214. is_capture_proxy (tree decl)
  215. {
  216. return (VAR_P (decl)
  217. && DECL_HAS_VALUE_EXPR_P (decl)
  218. && !DECL_ANON_UNION_VAR_P (decl)
  219. && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
  220. }
  221. /* Returns true iff DECL is a capture proxy for a normal capture
  222. (i.e. without explicit initializer). */
  223. bool
  224. is_normal_capture_proxy (tree decl)
  225. {
  226. if (!is_capture_proxy (decl))
  227. /* It's not a capture proxy. */
  228. return false;
  229. if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
  230. /* VLA capture. */
  231. return true;
  232. /* It is a capture proxy, is it a normal capture? */
  233. tree val = DECL_VALUE_EXPR (decl);
  234. if (val == error_mark_node)
  235. return true;
  236. gcc_assert (TREE_CODE (val) == COMPONENT_REF);
  237. val = TREE_OPERAND (val, 1);
  238. return DECL_NORMAL_CAPTURE_P (val);
  239. }
  240. /* VAR is a capture proxy created by build_capture_proxy; add it to the
  241. current function, which is the operator() for the appropriate lambda. */
  242. void
  243. insert_capture_proxy (tree var)
  244. {
  245. cp_binding_level *b;
  246. tree stmt_list;
  247. /* Put the capture proxy in the extra body block so that it won't clash
  248. with a later local variable. */
  249. b = current_binding_level;
  250. for (;;)
  251. {
  252. cp_binding_level *n = b->level_chain;
  253. if (n->kind == sk_function_parms)
  254. break;
  255. b = n;
  256. }
  257. pushdecl_with_scope (var, b, false);
  258. /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
  259. var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
  260. stmt_list = (*stmt_list_stack)[1];
  261. gcc_assert (stmt_list);
  262. append_to_statement_list_force (var, &stmt_list);
  263. }
  264. /* We've just finished processing a lambda; if the containing scope is also
  265. a lambda, insert any capture proxies that were created while processing
  266. the nested lambda. */
  267. void
  268. insert_pending_capture_proxies (void)
  269. {
  270. tree lam;
  271. vec<tree, va_gc> *proxies;
  272. unsigned i;
  273. if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
  274. return;
  275. lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
  276. proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
  277. for (i = 0; i < vec_safe_length (proxies); ++i)
  278. {
  279. tree var = (*proxies)[i];
  280. insert_capture_proxy (var);
  281. }
  282. release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
  283. LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
  284. }
  285. /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
  286. return the type we want the proxy to have: the type of the field itself,
  287. with added const-qualification if the lambda isn't mutable and the
  288. capture is by value. */
  289. tree
  290. lambda_proxy_type (tree ref)
  291. {
  292. tree type;
  293. if (ref == error_mark_node)
  294. return error_mark_node;
  295. if (REFERENCE_REF_P (ref))
  296. ref = TREE_OPERAND (ref, 0);
  297. gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
  298. type = TREE_TYPE (ref);
  299. if (!type || WILDCARD_TYPE_P (non_reference (type)))
  300. {
  301. type = cxx_make_type (DECLTYPE_TYPE);
  302. DECLTYPE_TYPE_EXPR (type) = ref;
  303. DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
  304. SET_TYPE_STRUCTURAL_EQUALITY (type);
  305. }
  306. if (DECL_PACK_P (TREE_OPERAND (ref, 1)))
  307. type = make_pack_expansion (type);
  308. return type;
  309. }
  310. /* MEMBER is a capture field in a lambda closure class. Now that we're
  311. inside the operator(), build a placeholder var for future lookups and
  312. debugging. */
  313. tree
  314. build_capture_proxy (tree member)
  315. {
  316. tree var, object, fn, closure, name, lam, type;
  317. if (PACK_EXPANSION_P (member))
  318. member = PACK_EXPANSION_PATTERN (member);
  319. closure = DECL_CONTEXT (member);
  320. fn = lambda_function (closure);
  321. lam = CLASSTYPE_LAMBDA_EXPR (closure);
  322. /* The proxy variable forwards to the capture field. */
  323. object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
  324. object = finish_non_static_data_member (member, object, NULL_TREE);
  325. if (REFERENCE_REF_P (object))
  326. object = TREE_OPERAND (object, 0);
  327. /* Remove the __ inserted by add_capture. */
  328. name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
  329. type = lambda_proxy_type (object);
  330. if (DECL_VLA_CAPTURE_P (member))
  331. {
  332. /* Rebuild the VLA type from the pointer and maxindex. */
  333. tree field = next_initializable_field (TYPE_FIELDS (type));
  334. tree ptr = build_simple_component_ref (object, field);
  335. field = next_initializable_field (DECL_CHAIN (field));
  336. tree max = build_simple_component_ref (object, field);
  337. type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)),
  338. build_index_type (max));
  339. type = build_reference_type (type);
  340. REFERENCE_VLA_OK (type) = true;
  341. object = convert (type, ptr);
  342. }
  343. var = build_decl (input_location, VAR_DECL, name, type);
  344. SET_DECL_VALUE_EXPR (var, object);
  345. DECL_HAS_VALUE_EXPR_P (var) = 1;
  346. DECL_ARTIFICIAL (var) = 1;
  347. TREE_USED (var) = 1;
  348. DECL_CONTEXT (var) = fn;
  349. if (name == this_identifier)
  350. {
  351. gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
  352. LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
  353. }
  354. if (fn == current_function_decl)
  355. insert_capture_proxy (var);
  356. else
  357. vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var);
  358. return var;
  359. }
  360. /* Return a struct containing a pointer and a length for lambda capture of
  361. an array of runtime length. */
  362. static tree
  363. vla_capture_type (tree array_type)
  364. {
  365. static tree ptr_id, max_id;
  366. tree type = xref_tag (record_type, make_anon_name (), ts_current, false);
  367. xref_basetypes (type, NULL_TREE);
  368. type = begin_class_definition (type);
  369. if (!ptr_id)
  370. {
  371. ptr_id = get_identifier ("ptr");
  372. max_id = get_identifier ("max");
  373. }
  374. tree ptrtype = build_pointer_type (TREE_TYPE (array_type));
  375. tree field = build_decl (input_location, FIELD_DECL, ptr_id, ptrtype);
  376. finish_member_declaration (field);
  377. field = build_decl (input_location, FIELD_DECL, max_id, sizetype);
  378. finish_member_declaration (field);
  379. return finish_struct (type, NULL_TREE);
  380. }
  381. /* From an ID and INITIALIZER, create a capture (by reference if
  382. BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
  383. and return it. */
  384. tree
  385. add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p,
  386. bool explicit_init_p)
  387. {
  388. char *buf;
  389. tree type, member, name;
  390. bool vla = false;
  391. bool variadic = false;
  392. tree initializer = orig_init;
  393. if (PACK_EXPANSION_P (initializer))
  394. {
  395. initializer = PACK_EXPANSION_PATTERN (initializer);
  396. variadic = true;
  397. }
  398. if (TREE_CODE (initializer) == TREE_LIST)
  399. initializer = build_x_compound_expr_from_list (initializer, ELK_INIT,
  400. tf_warning_or_error);
  401. type = TREE_TYPE (initializer);
  402. if (type == error_mark_node)
  403. return error_mark_node;
  404. if (array_of_runtime_bound_p (type))
  405. {
  406. vla = true;
  407. if (!by_reference_p)
  408. error ("array of runtime bound cannot be captured by copy, "
  409. "only by reference");
  410. /* For a VLA, we capture the address of the first element and the
  411. maximum index, and then reconstruct the VLA for the proxy. */
  412. tree elt = cp_build_array_ref (input_location, initializer,
  413. integer_zero_node, tf_warning_or_error);
  414. initializer = build_constructor_va (init_list_type_node, 2,
  415. NULL_TREE, build_address (elt),
  416. NULL_TREE, array_type_nelts (type));
  417. type = vla_capture_type (type);
  418. }
  419. else if (!dependent_type_p (type)
  420. && variably_modified_type_p (type, NULL_TREE))
  421. {
  422. error ("capture of variable-size type %qT that is not an N3639 array "
  423. "of runtime bound", type);
  424. if (TREE_CODE (type) == ARRAY_TYPE
  425. && variably_modified_type_p (TREE_TYPE (type), NULL_TREE))
  426. inform (input_location, "because the array element type %qT has "
  427. "variable size", TREE_TYPE (type));
  428. type = error_mark_node;
  429. }
  430. else
  431. {
  432. type = lambda_capture_field_type (initializer, explicit_init_p);
  433. if (by_reference_p)
  434. {
  435. type = build_reference_type (type);
  436. if (!dependent_type_p (type) && !real_lvalue_p (initializer))
  437. error ("cannot capture %qE by reference", initializer);
  438. }
  439. else
  440. {
  441. /* Capture by copy requires a complete type. */
  442. type = complete_type (type);
  443. if (!dependent_type_p (type) && !COMPLETE_TYPE_P (type))
  444. {
  445. error ("capture by copy of incomplete type %qT", type);
  446. cxx_incomplete_type_inform (type);
  447. return error_mark_node;
  448. }
  449. }
  450. }
  451. /* Add __ to the beginning of the field name so that user code
  452. won't find the field with name lookup. We can't just leave the name
  453. unset because template instantiation uses the name to find
  454. instantiated fields. */
  455. buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
  456. buf[1] = buf[0] = '_';
  457. memcpy (buf + 2, IDENTIFIER_POINTER (id),
  458. IDENTIFIER_LENGTH (id) + 1);
  459. name = get_identifier (buf);
  460. /* If TREE_TYPE isn't set, we're still in the introducer, so check
  461. for duplicates. */
  462. if (!LAMBDA_EXPR_CLOSURE (lambda))
  463. {
  464. if (IDENTIFIER_MARKED (name))
  465. {
  466. pedwarn (input_location, 0,
  467. "already captured %qD in lambda expression", id);
  468. return NULL_TREE;
  469. }
  470. IDENTIFIER_MARKED (name) = true;
  471. }
  472. if (variadic)
  473. type = make_pack_expansion (type);
  474. /* Make member variable. */
  475. member = build_decl (input_location, FIELD_DECL, name, type);
  476. DECL_VLA_CAPTURE_P (member) = vla;
  477. if (!explicit_init_p)
  478. /* Normal captures are invisible to name lookup but uses are replaced
  479. with references to the capture field; we implement this by only
  480. really making them invisible in unevaluated context; see
  481. qualify_lookup. For now, let's make explicitly initialized captures
  482. always visible. */
  483. DECL_NORMAL_CAPTURE_P (member) = true;
  484. if (id == this_identifier)
  485. LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
  486. /* Add it to the appropriate closure class if we've started it. */
  487. if (current_class_type
  488. && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
  489. finish_member_declaration (member);
  490. tree listmem = member;
  491. if (variadic)
  492. {
  493. listmem = make_pack_expansion (member);
  494. initializer = orig_init;
  495. }
  496. LAMBDA_EXPR_CAPTURE_LIST (lambda)
  497. = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
  498. if (LAMBDA_EXPR_CLOSURE (lambda))
  499. return build_capture_proxy (member);
  500. /* For explicit captures we haven't started the function yet, so we wait
  501. and build the proxy from cp_parser_lambda_body. */
  502. return NULL_TREE;
  503. }
  504. /* Register all the capture members on the list CAPTURES, which is the
  505. LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
  506. void
  507. register_capture_members (tree captures)
  508. {
  509. if (captures == NULL_TREE)
  510. return;
  511. register_capture_members (TREE_CHAIN (captures));
  512. tree field = TREE_PURPOSE (captures);
  513. if (PACK_EXPANSION_P (field))
  514. field = PACK_EXPANSION_PATTERN (field);
  515. /* We set this in add_capture to avoid duplicates. */
  516. IDENTIFIER_MARKED (DECL_NAME (field)) = false;
  517. finish_member_declaration (field);
  518. }
  519. /* Similar to add_capture, except this works on a stack of nested lambdas.
  520. BY_REFERENCE_P in this case is derived from the default capture mode.
  521. Returns the capture for the lambda at the bottom of the stack. */
  522. tree
  523. add_default_capture (tree lambda_stack, tree id, tree initializer)
  524. {
  525. bool this_capture_p = (id == this_identifier);
  526. tree var = NULL_TREE;
  527. tree saved_class_type = current_class_type;
  528. tree node;
  529. for (node = lambda_stack;
  530. node;
  531. node = TREE_CHAIN (node))
  532. {
  533. tree lambda = TREE_VALUE (node);
  534. current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
  535. if (DECL_PACK_P (initializer))
  536. initializer = make_pack_expansion (initializer);
  537. var = add_capture (lambda,
  538. id,
  539. initializer,
  540. /*by_reference_p=*/
  541. (!this_capture_p
  542. && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
  543. == CPLD_REFERENCE)),
  544. /*explicit_init_p=*/false);
  545. initializer = convert_from_reference (var);
  546. }
  547. current_class_type = saved_class_type;
  548. return var;
  549. }
  550. /* Return the capture pertaining to a use of 'this' in LAMBDA, in the
  551. form of an INDIRECT_REF, possibly adding it through default
  552. capturing, if ADD_CAPTURE_P is false. */
  553. tree
  554. lambda_expr_this_capture (tree lambda, bool add_capture_p)
  555. {
  556. tree result;
  557. tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
  558. /* In unevaluated context this isn't an odr-use, so just return the
  559. nearest 'this'. */
  560. if (cp_unevaluated_operand)
  561. {
  562. /* In an NSDMI the fake 'this' pointer that we're using for
  563. parsing is in scope_chain. */
  564. if (LAMBDA_EXPR_EXTRA_SCOPE (lambda)
  565. && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (lambda)) == FIELD_DECL)
  566. return scope_chain->x_current_class_ptr;
  567. return lookup_name (this_identifier);
  568. }
  569. /* Try to default capture 'this' if we can. */
  570. if (!this_capture
  571. && (!add_capture_p
  572. || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE))
  573. {
  574. tree lambda_stack = NULL_TREE;
  575. tree init = NULL_TREE;
  576. /* If we are in a lambda function, we can move out until we hit:
  577. 1. a non-lambda function or NSDMI,
  578. 2. a lambda function capturing 'this', or
  579. 3. a non-default capturing lambda function. */
  580. for (tree tlambda = lambda; ;)
  581. {
  582. lambda_stack = tree_cons (NULL_TREE,
  583. tlambda,
  584. lambda_stack);
  585. if (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)
  586. && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)) == FIELD_DECL)
  587. {
  588. /* In an NSDMI, we don't have a function to look up the decl in,
  589. but the fake 'this' pointer that we're using for parsing is
  590. in scope_chain. */
  591. init = scope_chain->x_current_class_ptr;
  592. gcc_checking_assert
  593. (init && (TREE_TYPE (TREE_TYPE (init))
  594. == current_nonlambda_class_type ()));
  595. break;
  596. }
  597. tree closure_decl = TYPE_NAME (LAMBDA_EXPR_CLOSURE (tlambda));
  598. tree containing_function = decl_function_context (closure_decl);
  599. if (containing_function == NULL_TREE)
  600. /* We ran out of scopes; there's no 'this' to capture. */
  601. break;
  602. if (!LAMBDA_FUNCTION_P (containing_function))
  603. {
  604. /* We found a non-lambda function. */
  605. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function))
  606. /* First parameter is 'this'. */
  607. init = DECL_ARGUMENTS (containing_function);
  608. break;
  609. }
  610. tlambda
  611. = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
  612. if (LAMBDA_EXPR_THIS_CAPTURE (tlambda))
  613. {
  614. /* An outer lambda has already captured 'this'. */
  615. init = LAMBDA_EXPR_THIS_CAPTURE (tlambda);
  616. break;
  617. }
  618. if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE)
  619. /* An outer lambda won't let us capture 'this'. */
  620. break;
  621. }
  622. if (init)
  623. {
  624. if (add_capture_p)
  625. this_capture = add_default_capture (lambda_stack,
  626. /*id=*/this_identifier,
  627. init);
  628. else
  629. this_capture = init;
  630. }
  631. }
  632. if (!this_capture)
  633. {
  634. if (add_capture_p)
  635. error ("%<this%> was not captured for this lambda function");
  636. result = error_mark_node;
  637. }
  638. else
  639. {
  640. /* To make sure that current_class_ref is for the lambda. */
  641. gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
  642. == LAMBDA_EXPR_CLOSURE (lambda));
  643. result = this_capture;
  644. /* If 'this' is captured, each use of 'this' is transformed into an
  645. access to the corresponding unnamed data member of the closure
  646. type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
  647. ensures that the transformed expression is an rvalue. ] */
  648. result = rvalue (result);
  649. }
  650. return result;
  651. }
  652. /* We don't want to capture 'this' until we know we need it, i.e. after
  653. overload resolution has chosen a non-static member function. At that
  654. point we call this function to turn a dummy object into a use of the
  655. 'this' capture. */
  656. tree
  657. maybe_resolve_dummy (tree object, bool add_capture_p)
  658. {
  659. if (!is_dummy_object (object))
  660. return object;
  661. tree type = TYPE_MAIN_VARIANT (TREE_TYPE (object));
  662. gcc_assert (!TYPE_PTR_P (type));
  663. if (type != current_class_type
  664. && current_class_type
  665. && LAMBDA_TYPE_P (current_class_type)
  666. && lambda_function (current_class_type)
  667. && DERIVED_FROM_P (type, current_nonlambda_class_type ()))
  668. {
  669. /* In a lambda, need to go through 'this' capture. */
  670. tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
  671. tree cap = lambda_expr_this_capture (lam, add_capture_p);
  672. if (cap && cap != error_mark_node)
  673. object = build_x_indirect_ref (EXPR_LOCATION (object), cap,
  674. RO_NULL, tf_warning_or_error);
  675. }
  676. return object;
  677. }
  678. /* Returns the innermost non-lambda function. */
  679. tree
  680. current_nonlambda_function (void)
  681. {
  682. tree fn = current_function_decl;
  683. while (fn && LAMBDA_FUNCTION_P (fn))
  684. fn = decl_function_context (fn);
  685. return fn;
  686. }
  687. /* Returns the method basetype of the innermost non-lambda function, or
  688. NULL_TREE if none. */
  689. tree
  690. nonlambda_method_basetype (void)
  691. {
  692. tree fn, type;
  693. if (!current_class_ref)
  694. return NULL_TREE;
  695. type = current_class_type;
  696. if (!LAMBDA_TYPE_P (type))
  697. return type;
  698. /* Find the nearest enclosing non-lambda function. */
  699. fn = TYPE_NAME (type);
  700. do
  701. fn = decl_function_context (fn);
  702. while (fn && LAMBDA_FUNCTION_P (fn));
  703. if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
  704. return NULL_TREE;
  705. return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
  706. }
  707. /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with
  708. indicated FN and NARGS, but do not initialize the return type or any of the
  709. argument slots. */
  710. static tree
  711. prepare_op_call (tree fn, int nargs)
  712. {
  713. tree t;
  714. t = build_vl_exp (CALL_EXPR, nargs + 3);
  715. CALL_EXPR_FN (t) = fn;
  716. CALL_EXPR_STATIC_CHAIN (t) = NULL;
  717. return t;
  718. }
  719. /* If the closure TYPE has a static op(), also add a conversion to function
  720. pointer. */
  721. void
  722. maybe_add_lambda_conv_op (tree type)
  723. {
  724. bool nested = (cfun != NULL);
  725. bool nested_def = decl_function_context (TYPE_MAIN_DECL (type));
  726. tree callop = lambda_function (type);
  727. if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE)
  728. return;
  729. if (processing_template_decl)
  730. return;
  731. bool const generic_lambda_p
  732. = (DECL_TEMPLATE_INFO (callop)
  733. && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop);
  734. if (!generic_lambda_p && DECL_INITIAL (callop) == NULL_TREE)
  735. {
  736. /* If the op() wasn't instantiated due to errors, give up. */
  737. gcc_assert (errorcount || sorrycount);
  738. return;
  739. }
  740. /* Non-template conversion operators are defined directly with build_call_a
  741. and using DIRECT_ARGVEC for arguments (including 'this'). Templates are
  742. deferred and the CALL is built in-place. In the case of a deduced return
  743. call op, the decltype expression, DECLTYPE_CALL, used as a substitute for
  744. the return type is also built in-place. The arguments of DECLTYPE_CALL in
  745. the return expression may differ in flags from those in the body CALL. In
  746. particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in
  747. the body CALL, but not in DECLTYPE_CALL. */
  748. vec<tree, va_gc> *direct_argvec = 0;
  749. tree decltype_call = 0, call = 0;
  750. tree fn_result = TREE_TYPE (TREE_TYPE (callop));
  751. if (generic_lambda_p)
  752. {
  753. /* Prepare the dependent member call for the static member function
  754. '_FUN' and, potentially, prepare another call to be used in a decltype
  755. return expression for a deduced return call op to allow for simple
  756. implementation of the conversion operator. */
  757. tree instance = build_nop (type, null_pointer_node);
  758. tree objfn = build_min (COMPONENT_REF, NULL_TREE,
  759. instance, DECL_NAME (callop), NULL_TREE);
  760. int nargs = list_length (DECL_ARGUMENTS (callop)) - 1;
  761. call = prepare_op_call (objfn, nargs);
  762. if (type_uses_auto (fn_result))
  763. decltype_call = prepare_op_call (objfn, nargs);
  764. }
  765. else
  766. {
  767. direct_argvec = make_tree_vector ();
  768. direct_argvec->quick_push (build1 (NOP_EXPR,
  769. TREE_TYPE (DECL_ARGUMENTS (callop)),
  770. null_pointer_node));
  771. }
  772. /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to
  773. declare the static member function "_FUN" below. For each arg append to
  774. DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated
  775. call args (for the template case). If a parameter pack is found, expand
  776. it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */
  777. tree fn_args = NULL_TREE;
  778. {
  779. int ix = 0;
  780. tree src = DECL_CHAIN (DECL_ARGUMENTS (callop));
  781. tree tgt;
  782. while (src)
  783. {
  784. tree new_node = copy_node (src);
  785. if (!fn_args)
  786. fn_args = tgt = new_node;
  787. else
  788. {
  789. TREE_CHAIN (tgt) = new_node;
  790. tgt = new_node;
  791. }
  792. mark_exp_read (tgt);
  793. if (generic_lambda_p)
  794. {
  795. if (DECL_PACK_P (tgt))
  796. {
  797. tree a = make_pack_expansion (tgt);
  798. if (decltype_call)
  799. CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
  800. PACK_EXPANSION_LOCAL_P (a) = true;
  801. CALL_EXPR_ARG (call, ix) = a;
  802. }
  803. else
  804. {
  805. tree a = convert_from_reference (tgt);
  806. CALL_EXPR_ARG (call, ix) = a;
  807. if (decltype_call)
  808. CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
  809. }
  810. ++ix;
  811. }
  812. else
  813. vec_safe_push (direct_argvec, tgt);
  814. src = TREE_CHAIN (src);
  815. }
  816. }
  817. if (generic_lambda_p)
  818. {
  819. if (decltype_call)
  820. {
  821. ++processing_template_decl;
  822. fn_result = finish_decltype_type
  823. (decltype_call, /*id_expression_or_member_access_p=*/false,
  824. tf_warning_or_error);
  825. --processing_template_decl;
  826. }
  827. }
  828. else
  829. call = build_call_a (callop,
  830. direct_argvec->length (),
  831. direct_argvec->address ());
  832. CALL_FROM_THUNK_P (call) = 1;
  833. tree stattype = build_function_type (fn_result, FUNCTION_ARG_CHAIN (callop));
  834. /* First build up the conversion op. */
  835. tree rettype = build_pointer_type (stattype);
  836. tree name = mangle_conv_op_name_for_type (rettype);
  837. tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
  838. tree fntype = build_method_type_directly (thistype, rettype, void_list_node);
  839. tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
  840. tree fn = convfn;
  841. DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
  842. if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
  843. && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
  844. DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
  845. SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
  846. grokclassfn (type, fn, NO_SPECIAL);
  847. set_linkage_according_to_type (type, fn);
  848. rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
  849. DECL_IN_AGGR_P (fn) = 1;
  850. DECL_ARTIFICIAL (fn) = 1;
  851. DECL_NOT_REALLY_EXTERN (fn) = 1;
  852. DECL_DECLARED_INLINE_P (fn) = 1;
  853. DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
  854. if (nested_def)
  855. DECL_INTERFACE_KNOWN (fn) = 1;
  856. if (generic_lambda_p)
  857. fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
  858. add_method (type, fn, NULL_TREE);
  859. /* Generic thunk code fails for varargs; we'll complain in mark_used if
  860. the conversion op is used. */
  861. if (varargs_function_p (callop))
  862. {
  863. DECL_DELETED_FN (fn) = 1;
  864. return;
  865. }
  866. /* Now build up the thunk to be returned. */
  867. name = get_identifier ("_FUN");
  868. tree statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
  869. fn = statfn;
  870. DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
  871. if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
  872. && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
  873. DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
  874. grokclassfn (type, fn, NO_SPECIAL);
  875. set_linkage_according_to_type (type, fn);
  876. rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
  877. DECL_IN_AGGR_P (fn) = 1;
  878. DECL_ARTIFICIAL (fn) = 1;
  879. DECL_NOT_REALLY_EXTERN (fn) = 1;
  880. DECL_DECLARED_INLINE_P (fn) = 1;
  881. DECL_STATIC_FUNCTION_P (fn) = 1;
  882. DECL_ARGUMENTS (fn) = fn_args;
  883. for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg))
  884. {
  885. /* Avoid duplicate -Wshadow warnings. */
  886. DECL_NAME (arg) = NULL_TREE;
  887. DECL_CONTEXT (arg) = fn;
  888. }
  889. if (nested_def)
  890. DECL_INTERFACE_KNOWN (fn) = 1;
  891. if (generic_lambda_p)
  892. fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
  893. add_method (type, fn, NULL_TREE);
  894. if (nested)
  895. push_function_context ();
  896. else
  897. /* Still increment function_depth so that we don't GC in the
  898. middle of an expression. */
  899. ++function_depth;
  900. /* Generate the body of the thunk. */
  901. start_preparsed_function (statfn, NULL_TREE,
  902. SF_PRE_PARSED | SF_INCLASS_INLINE);
  903. if (DECL_ONE_ONLY (statfn))
  904. {
  905. /* Put the thunk in the same comdat group as the call op. */
  906. cgraph_node::get_create (statfn)->add_to_same_comdat_group
  907. (cgraph_node::get_create (callop));
  908. }
  909. tree body = begin_function_body ();
  910. tree compound_stmt = begin_compound_stmt (0);
  911. if (!generic_lambda_p)
  912. {
  913. set_flags_from_callee (call);
  914. if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
  915. call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
  916. }
  917. call = convert_from_reference (call);
  918. finish_return_stmt (call);
  919. finish_compound_stmt (compound_stmt);
  920. finish_function_body (body);
  921. fn = finish_function (/*inline*/2);
  922. if (!generic_lambda_p)
  923. expand_or_defer_fn (fn);
  924. /* Generate the body of the conversion op. */
  925. start_preparsed_function (convfn, NULL_TREE,
  926. SF_PRE_PARSED | SF_INCLASS_INLINE);
  927. body = begin_function_body ();
  928. compound_stmt = begin_compound_stmt (0);
  929. /* decl_needed_p needs to see that it's used. */
  930. TREE_USED (statfn) = 1;
  931. finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
  932. finish_compound_stmt (compound_stmt);
  933. finish_function_body (body);
  934. fn = finish_function (/*inline*/2);
  935. if (!generic_lambda_p)
  936. expand_or_defer_fn (fn);
  937. if (nested)
  938. pop_function_context ();
  939. else
  940. --function_depth;
  941. }
  942. /* Returns true iff VAL is a lambda-related declaration which should
  943. be ignored by unqualified lookup. */
  944. bool
  945. is_lambda_ignored_entity (tree val)
  946. {
  947. /* In unevaluated context, look past normal capture proxies. */
  948. if (cp_unevaluated_operand && is_normal_capture_proxy (val))
  949. return true;
  950. /* Always ignore lambda fields, their names are only for debugging. */
  951. if (TREE_CODE (val) == FIELD_DECL
  952. && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
  953. return true;
  954. /* None of the lookups that use qualify_lookup want the op() from the
  955. lambda; they want the one from the enclosing class. */
  956. if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
  957. return true;
  958. return false;
  959. }