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