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 tree type = current_class_type; 1006 if (!type || !LAMBDA_TYPE_P (type)) 1007 return current_class_ref ? type : NULL_TREE; 1008 1009 while (true) 1010 { 1011 tree lam = CLASSTYPE_LAMBDA_EXPR (type); 1012 tree ex = LAMBDA_EXPR_EXTRA_SCOPE (lam); 1013 if (ex && TREE_CODE (ex) == FIELD_DECL) 1014 /* Lambda in an NSDMI. */ 1015 return DECL_CONTEXT (ex); 1016 1017 tree fn = TYPE_CONTEXT (type); 1018 if (!fn || TREE_CODE (fn) != FUNCTION_DECL 1019 || !DECL_IOBJ_MEMBER_FUNCTION_P (fn)) 1020 /* No enclosing non-lambda method. */ 1021 return NULL_TREE; 1022 if (!LAMBDA_FUNCTION_P (fn)) 1023 /* Found an enclosing non-lambda method. */ 1024 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn)); 1025 type = DECL_CONTEXT (fn); 1026 } 1027 } 1028 1029 /* Like current_scope, but looking through lambdas. */ 1030 1031 tree 1032 current_nonlambda_scope (void) 1033 { 1034 tree scope = current_scope (); 1035 for (;;) 1036 { 1037 if (TREE_CODE (scope) == FUNCTION_DECL 1038 && LAMBDA_FUNCTION_P (scope)) 1039 { 1040 scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope)); 1041 continue; 1042 } 1043 else if (LAMBDA_TYPE_P (scope)) 1044 { 1045 scope = CP_TYPE_CONTEXT (scope); 1046 continue; 1047 } 1048 break; 1049 } 1050 return scope; 1051 } 1052 1053 /* Helper function for maybe_add_lambda_conv_op; build a CALL_EXPR with 1054 indicated FN and NARGS, but do not initialize the return type or any of the 1055 argument slots. */ 1056 1057 static tree 1058 prepare_op_call (tree fn, int nargs) 1059 { 1060 tree t; 1061 1062 t = build_vl_exp (CALL_EXPR, nargs + 3); 1063 CALL_EXPR_FN (t) = fn; 1064 CALL_EXPR_STATIC_CHAIN (t) = NULL; 1065 1066 return t; 1067 } 1068 1069 /* Return true iff CALLOP is the op() for a generic lambda. */ 1070 1071 bool 1072 generic_lambda_fn_p (tree callop) 1073 { 1074 return (LAMBDA_FUNCTION_P (callop) 1075 && DECL_TEMPLATE_INFO (callop) 1076 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop))); 1077 } 1078 1079 /* If the closure TYPE has a static op(), also add a conversion to function 1080 pointer. */ 1081 1082 void 1083 maybe_add_lambda_conv_op (tree type) 1084 { 1085 bool nested = (cfun != NULL); 1086 bool nested_def = decl_function_context (TYPE_MAIN_DECL (type)); 1087 tree callop = lambda_function (type); 1088 tree lam = CLASSTYPE_LAMBDA_EXPR (type); 1089 1090 if (LAMBDA_EXPR_CAPTURE_LIST (lam) != NULL_TREE 1091 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) != CPLD_NONE) 1092 return; 1093 1094 if (processing_template_decl) 1095 return; 1096 1097 bool const generic_lambda_p = generic_lambda_fn_p (callop); 1098 1099 if (!generic_lambda_p && undeduced_auto_decl (callop)) 1100 { 1101 /* If the op() wasn't deduced due to errors, give up. */ 1102 gcc_assert (errorcount || sorrycount); 1103 return; 1104 } 1105 1106 /* Non-generic non-capturing lambdas only have a conversion function to 1107 pointer to function when the trailing requires-clause's constraints are 1108 satisfied. */ 1109 if (!generic_lambda_p && !constraints_satisfied_p (callop)) 1110 return; 1111 1112 /* Non-template conversion operators are defined directly with build_call_a 1113 and using DIRECT_ARGVEC for arguments (including 'this'). Templates are 1114 deferred and the CALL is built in-place. In the case of a deduced return 1115 call op, the decltype expression, DECLTYPE_CALL, used as a substitute for 1116 the return type is also built in-place. The arguments of DECLTYPE_CALL in 1117 the return expression may differ in flags from those in the body CALL. In 1118 particular, parameter pack expansions are marked PACK_EXPANSION_LOCAL_P in 1119 the body CALL, but not in DECLTYPE_CALL. */ 1120 1121 vec<tree, va_gc> *direct_argvec = 0; 1122 tree decltype_call = 0, call = 0; 1123 tree optype = TREE_TYPE (callop); 1124 tree fn_result = TREE_TYPE (optype); 1125 1126 tree thisarg = NULL_TREE; 1127 if (TREE_CODE (optype) == METHOD_TYPE) 1128 thisarg = build_int_cst (TREE_TYPE (DECL_ARGUMENTS (callop)), 0); 1129 if (generic_lambda_p) 1130 { 1131 ++processing_template_decl; 1132 1133 /* Prepare the dependent member call for the static member function 1134 '_FUN' and, potentially, prepare another call to be used in a decltype 1135 return expression for a deduced return call op to allow for simple 1136 implementation of the conversion operator. */ 1137 1138 tree objfn; 1139 int nargs = list_length (DECL_ARGUMENTS (callop)); 1140 if (thisarg) 1141 { 1142 tree instance = cp_build_fold_indirect_ref (thisarg); 1143 objfn = lookup_template_function (DECL_NAME (callop), 1144 DECL_TI_ARGS (callop)); 1145 objfn = build_min (COMPONENT_REF, NULL_TREE, 1146 instance, objfn, NULL_TREE); 1147 --nargs; 1148 call = prepare_op_call (objfn, nargs); 1149 } 1150 else 1151 objfn = callop; 1152 1153 if (type_uses_auto (fn_result)) 1154 decltype_call = prepare_op_call (objfn, nargs); 1155 } 1156 else if (thisarg) 1157 { 1158 direct_argvec = make_tree_vector (); 1159 direct_argvec->quick_push (thisarg); 1160 } 1161 1162 /* Copy CALLOP's argument list (as per 'copy_list') as FN_ARGS in order to 1163 declare the static member function "_FUN" below. For each arg append to 1164 DIRECT_ARGVEC (for the non-template case) or populate the pre-allocated 1165 call args (for the template case). If a parameter pack is found, expand 1166 it, flagging it as PACK_EXPANSION_LOCAL_P for the body call. */ 1167 1168 tree fn_args = NULL_TREE; 1169 { 1170 int ix = 0; 1171 tree src = FUNCTION_FIRST_USER_PARM (callop); 1172 tree tgt = NULL; 1173 1174 if (!thisarg && !decltype_call) 1175 src = NULL_TREE; 1176 while (src) 1177 { 1178 tree new_node = copy_node (src); 1179 /* We set DECL_CONTEXT of NEW_NODE to the statfn below. 1180 Notice this is creating a recursive type! */ 1181 1182 /* Clear TREE_ADDRESSABLE on thunk arguments. */ 1183 TREE_ADDRESSABLE (new_node) = 0; 1184 1185 if (!fn_args) 1186 fn_args = tgt = new_node; 1187 else 1188 { 1189 TREE_CHAIN (tgt) = new_node; 1190 tgt = new_node; 1191 } 1192 1193 mark_exp_read (tgt); 1194 1195 if (generic_lambda_p) 1196 { 1197 tree a = tgt; 1198 if (thisarg) 1199 { 1200 if (DECL_PACK_P (tgt)) 1201 { 1202 a = make_pack_expansion (a); 1203 PACK_EXPANSION_LOCAL_P (a) = true; 1204 } 1205 CALL_EXPR_ARG (call, ix) = a; 1206 } 1207 1208 if (decltype_call) 1209 { 1210 /* Avoid capturing variables in this context. */ 1211 ++cp_unevaluated_operand; 1212 CALL_EXPR_ARG (decltype_call, ix) = forward_parm (tgt); 1213 --cp_unevaluated_operand; 1214 } 1215 1216 ++ix; 1217 } 1218 else 1219 vec_safe_push (direct_argvec, tgt); 1220 1221 src = TREE_CHAIN (src); 1222 } 1223 } 1224 1225 if (generic_lambda_p) 1226 { 1227 if (decltype_call) 1228 { 1229 fn_result = finish_decltype_type 1230 (decltype_call, /*id_expression_or_member_access_p=*/false, 1231 tf_warning_or_error); 1232 } 1233 } 1234 else if (thisarg) 1235 { 1236 /* Don't warn on deprecated or unavailable lambda declarations, unless 1237 the lambda is actually called. */ 1238 auto du = make_temp_override (deprecated_state, 1239 UNAVAILABLE_DEPRECATED_SUPPRESS); 1240 call = build_call_a (callop, direct_argvec->length (), 1241 direct_argvec->address ()); 1242 } 1243 1244 if (thisarg) 1245 { 1246 CALL_FROM_THUNK_P (call) = 1; 1247 SET_EXPR_LOCATION (call, UNKNOWN_LOCATION); 1248 } 1249 1250 tree stattype 1251 = build_function_type (fn_result, FUNCTION_FIRST_USER_PARMTYPE (callop)); 1252 stattype = (cp_build_type_attribute_variant 1253 (stattype, TYPE_ATTRIBUTES (optype))); 1254 if (flag_noexcept_type 1255 && TYPE_NOTHROW_P (TREE_TYPE (callop))) 1256 stattype = build_exception_variant (stattype, noexcept_true_spec); 1257 1258 if (generic_lambda_p) 1259 --processing_template_decl; 1260 1261 /* First build up the conversion op. */ 1262 1263 tree rettype = build_pointer_type (stattype); 1264 tree name = make_conv_op_name (rettype); 1265 tree thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST); 1266 tree fntype = build_method_type_directly (thistype, rettype, void_list_node); 1267 /* DR 1722: The conversion function should be noexcept. */ 1268 fntype = build_exception_variant (fntype, noexcept_true_spec); 1269 tree convfn = build_lang_decl (FUNCTION_DECL, name, fntype); 1270 SET_DECL_LANGUAGE (convfn, lang_cplusplus); 1271 tree fn = convfn; 1272 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop); 1273 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY); 1274 grokclassfn (type, fn, NO_SPECIAL); 1275 set_linkage_according_to_type (type, fn); 1276 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof); 1277 DECL_IN_AGGR_P (fn) = 1; 1278 DECL_ARTIFICIAL (fn) = 1; 1279 DECL_NOT_REALLY_EXTERN (fn) = 1; 1280 DECL_DECLARED_INLINE_P (fn) = 1; 1281 DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop); 1282 if (DECL_IMMEDIATE_FUNCTION_P (callop)) 1283 SET_DECL_IMMEDIATE_FUNCTION_P (fn); 1284 DECL_ARGUMENTS (fn) = build_this_parm (fn, fntype, TYPE_QUAL_CONST); 1285 1286 if (nested_def) 1287 DECL_INTERFACE_KNOWN (fn) = 1; 1288 1289 if (generic_lambda_p) 1290 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop)); 1291 1292 add_method (type, fn, false); 1293 1294 if (thisarg == NULL_TREE) 1295 { 1296 /* For static lambda, just return operator(). */ 1297 if (nested) 1298 push_function_context (); 1299 else 1300 /* Still increment function_depth so that we don't GC in the 1301 middle of an expression. */ 1302 ++function_depth; 1303 1304 /* Generate the body of the conversion op. */ 1305 1306 start_preparsed_function (convfn, NULL_TREE, 1307 SF_PRE_PARSED | SF_INCLASS_INLINE); 1308 tree body = begin_function_body (); 1309 tree compound_stmt = begin_compound_stmt (0); 1310 1311 /* decl_needed_p needs to see that it's used. */ 1312 TREE_USED (callop) = 1; 1313 finish_return_stmt (decay_conversion (callop, tf_warning_or_error)); 1314 1315 finish_compound_stmt (compound_stmt); 1316 finish_function_body (body); 1317 1318 fn = finish_function (/*inline_p=*/true); 1319 if (!generic_lambda_p) 1320 expand_or_defer_fn (fn); 1321 1322 if (nested) 1323 pop_function_context (); 1324 else 1325 --function_depth; 1326 return; 1327 } 1328 1329 /* Generic thunk code fails for varargs; we'll complain in mark_used if 1330 the conversion op is used. */ 1331 if (varargs_function_p (callop)) 1332 { 1333 DECL_DELETED_FN (fn) = 1; 1334 return; 1335 } 1336 1337 /* Now build up the thunk to be returned. */ 1338 1339 tree statfn = build_lang_decl (FUNCTION_DECL, fun_identifier, stattype); 1340 SET_DECL_LANGUAGE (statfn, lang_cplusplus); 1341 fn = statfn; 1342 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop); 1343 grokclassfn (type, fn, NO_SPECIAL); 1344 set_linkage_according_to_type (type, fn); 1345 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof); 1346 DECL_IN_AGGR_P (fn) = 1; 1347 DECL_ARTIFICIAL (fn) = 1; 1348 DECL_NOT_REALLY_EXTERN (fn) = 1; 1349 DECL_DECLARED_INLINE_P (fn) = 1; 1350 DECL_STATIC_FUNCTION_P (fn) = 1; 1351 DECL_DECLARED_CONSTEXPR_P (fn) = DECL_DECLARED_CONSTEXPR_P (callop); 1352 if (DECL_IMMEDIATE_FUNCTION_P (callop)) 1353 SET_DECL_IMMEDIATE_FUNCTION_P (fn); 1354 DECL_ARGUMENTS (fn) = fn_args; 1355 for (tree arg = fn_args; arg; arg = DECL_CHAIN (arg)) 1356 { 1357 /* Avoid duplicate -Wshadow warnings. */ 1358 DECL_NAME (arg) = NULL_TREE; 1359 DECL_CONTEXT (arg) = fn; 1360 } 1361 if (nested_def) 1362 DECL_INTERFACE_KNOWN (fn) = 1; 1363 1364 if (generic_lambda_p) 1365 fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop)); 1366 1367 if (flag_sanitize & SANITIZE_NULL) 1368 /* Don't UBsan this function; we're deliberately calling op() with a null 1369 object argument. */ 1370 add_no_sanitize_value (fn, SANITIZE_UNDEFINED); 1371 1372 add_method (type, fn, false); 1373 1374 if (nested) 1375 push_function_context (); 1376 else 1377 /* Still increment function_depth so that we don't GC in the 1378 middle of an expression. */ 1379 ++function_depth; 1380 1381 /* Generate the body of the thunk. */ 1382 1383 start_preparsed_function (statfn, NULL_TREE, 1384 SF_PRE_PARSED | SF_INCLASS_INLINE); 1385 tree body = begin_function_body (); 1386 tree compound_stmt = begin_compound_stmt (0); 1387 if (!generic_lambda_p) 1388 { 1389 set_flags_from_callee (call); 1390 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call))) 1391 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error); 1392 } 1393 call = convert_from_reference (call); 1394 finish_return_stmt (call); 1395 1396 finish_compound_stmt (compound_stmt); 1397 finish_function_body (body); 1398 1399 fn = finish_function (/*inline_p=*/true); 1400 if (!generic_lambda_p) 1401 expand_or_defer_fn (fn); 1402 1403 /* Generate the body of the conversion op. */ 1404 1405 start_preparsed_function (convfn, NULL_TREE, 1406 SF_PRE_PARSED | SF_INCLASS_INLINE); 1407 body = begin_function_body (); 1408 compound_stmt = begin_compound_stmt (0); 1409 1410 /* decl_needed_p needs to see that it's used. */ 1411 TREE_USED (statfn) = 1; 1412 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error)); 1413 1414 finish_compound_stmt (compound_stmt); 1415 finish_function_body (body); 1416 1417 fn = finish_function (/*inline_p=*/true); 1418 if (!generic_lambda_p) 1419 expand_or_defer_fn (fn); 1420 1421 if (nested) 1422 pop_function_context (); 1423 else 1424 --function_depth; 1425 } 1426 1427 /* True if FN is the static function "_FUN" that gets returned from the lambda 1428 conversion operator. */ 1429 1430 bool 1431 lambda_static_thunk_p (tree fn) 1432 { 1433 return (fn && TREE_CODE (fn) == FUNCTION_DECL 1434 && DECL_ARTIFICIAL (fn) 1435 && DECL_STATIC_FUNCTION_P (fn) 1436 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fn))); 1437 } 1438 1439 bool 1440 call_from_lambda_thunk_p (tree call) 1441 { 1442 return (CALL_FROM_THUNK_P (call) 1443 && lambda_static_thunk_p (current_function_decl)); 1444 } 1445 1446 /* Returns true iff VAL is a lambda-related declaration which should 1447 be ignored by unqualified lookup. */ 1448 1449 bool 1450 is_lambda_ignored_entity (tree val) 1451 { 1452 /* Look past normal, non-VLA capture proxies. */ 1453 if (is_normal_capture_proxy (val) 1454 && !variably_modified_type_p (TREE_TYPE (val), NULL_TREE)) 1455 return true; 1456 1457 /* Always ignore lambda fields, their names are only for debugging. */ 1458 if (TREE_CODE (val) == FIELD_DECL 1459 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val))) 1460 return true; 1461 1462 /* None of the lookups that use qualify_lookup want the op() from the 1463 lambda; they want the one from the enclosing class. */ 1464 if (tree fns = maybe_get_fns (val)) 1465 if (LAMBDA_FUNCTION_P (OVL_FIRST (fns))) 1466 return true; 1467 1468 return false; 1469 } 1470 1471 /* Lambdas that appear in variable initializer or default argument 1472 scope get that in their mangling, so we need to record it. Also, 1473 multiple lambdas in the same scope may need a mangling 1474 discriminator. In ABI <= 17, there is a single per-scope sequence 1475 number. In ABI >= 18, there are per-scope per-signature sequence 1476 numbers. */ 1477 struct GTY(()) lambda_sig_count 1478 { 1479 tree fn; // The lambda fn whose sig this is. 1480 unsigned count; 1481 }; 1482 struct GTY(()) lambda_discriminator 1483 { 1484 tree scope; 1485 unsigned nesting; // Inside a function, VAR_DECLs get the function 1486 // as scope. This counts that nesting. 1487 unsigned count; // The per-scope counter. 1488 vec<lambda_sig_count, va_gc> *discriminators; // Per-signature counters 1489 }; 1490 // The current scope. 1491 static GTY(()) lambda_discriminator lambda_scope; 1492 // Stack of previous scopes. 1493 static GTY(()) vec<lambda_discriminator, va_gc> *lambda_scope_stack; 1494 1495 // Push DECL as lambda extra scope, also new discriminator counters. 1496 1497 void 1498 start_lambda_scope (tree decl) 1499 { 1500 gcc_checking_assert (decl); 1501 if (current_function_decl && VAR_P (decl)) 1502 // If we're inside a function, we ignore variable scope. Don't push. 1503 lambda_scope.nesting++; 1504 else 1505 { 1506 vec_safe_push (lambda_scope_stack, lambda_scope); 1507 lambda_scope.scope = decl; 1508 lambda_scope.nesting = 0; 1509 lambda_scope.count = 0; 1510 lambda_scope.discriminators = nullptr; 1511 } 1512 } 1513 1514 // Pop from the current lambda extra scope. 1515 1516 void 1517 finish_lambda_scope (void) 1518 { 1519 if (!lambda_scope.nesting--) 1520 { 1521 lambda_scope = lambda_scope_stack->last (); 1522 lambda_scope_stack->pop (); 1523 } 1524 } 1525 1526 // Record the current lambda scope into LAMBDA 1527 1528 void 1529 record_lambda_scope (tree lambda) 1530 { 1531 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope.scope; 1532 if (lambda_scope.scope) 1533 { 1534 tree closure = LAMBDA_EXPR_CLOSURE (lambda); 1535 gcc_checking_assert (closure); 1536 maybe_key_decl (lambda_scope.scope, TYPE_NAME (closure)); 1537 } 1538 } 1539 1540 // Compare lambda template heads TMPL_A and TMPL_B, used for both 1541 // templated lambdas, and template template parameters of said lambda. 1542 1543 static bool 1544 compare_lambda_template_head (tree tmpl_a, tree tmpl_b) 1545 { 1546 // We only need one level of template parms 1547 tree inner_a = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_a)); 1548 tree inner_b = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl_b)); 1549 1550 // We only compare explicit template parms, ignoring trailing 1551 // synthetic ones. 1552 int len_a = TREE_VEC_LENGTH (inner_a); 1553 int len_b = TREE_VEC_LENGTH (inner_b); 1554 1555 for (int ix = 0, len = MAX (len_a, len_b); ix != len; ix++) 1556 { 1557 tree parm_a = NULL_TREE; 1558 if (ix < len_a) 1559 { 1560 parm_a = TREE_VEC_ELT (inner_a, ix); 1561 if (parm_a == error_mark_node) 1562 return false; 1563 parm_a = TREE_VALUE (parm_a); 1564 if (parm_a == error_mark_node) 1565 return false; 1566 if (DECL_VIRTUAL_P (parm_a)) 1567 parm_a = NULL_TREE; 1568 } 1569 1570 tree parm_b = NULL_TREE; 1571 if (ix < len_b) 1572 { 1573 parm_b = TREE_VEC_ELT (inner_b, ix); 1574 if (parm_b == error_mark_node) 1575 return false; 1576 parm_b = TREE_VALUE (parm_b); 1577 if (parm_b == error_mark_node) 1578 return false; 1579 if (DECL_VIRTUAL_P (parm_b)) 1580 parm_b = NULL_TREE; 1581 } 1582 1583 if (!parm_a && !parm_b) 1584 // we're done 1585 break; 1586 1587 if (!(parm_a && parm_b)) 1588 return false; 1589 1590 if (TREE_CODE (parm_a) != TREE_CODE (parm_b)) 1591 return false; 1592 1593 if (TREE_CODE (parm_a) == PARM_DECL) 1594 { 1595 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_a)) 1596 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm_b))) 1597 return false; 1598 1599 if (!same_type_p (TREE_TYPE (parm_a), TREE_TYPE (parm_b))) 1600 return false; 1601 } 1602 else 1603 { 1604 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_a)) 1605 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm_b))) 1606 return false; 1607 1608 if (TREE_CODE (parm_a) != TEMPLATE_DECL) 1609 gcc_checking_assert (TREE_CODE (parm_a) == TYPE_DECL); 1610 else if (!compare_lambda_template_head (parm_a, parm_b)) 1611 return false; 1612 } 1613 } 1614 1615 return true; 1616 } 1617 1618 // Compare lambda signatures FN_A and FN_B, they may be TEMPLATE_DECLs too. 1619 1620 static bool 1621 compare_lambda_sig (tree fn_a, tree fn_b) 1622 { 1623 if (TREE_CODE (fn_a) == TEMPLATE_DECL 1624 && TREE_CODE (fn_b) == TEMPLATE_DECL) 1625 { 1626 if (!compare_lambda_template_head (fn_a, fn_b)) 1627 return false; 1628 fn_a = DECL_TEMPLATE_RESULT (fn_a); 1629 fn_b = DECL_TEMPLATE_RESULT (fn_b); 1630 } 1631 else if (TREE_CODE (fn_a) == TEMPLATE_DECL 1632 || TREE_CODE (fn_b) == TEMPLATE_DECL) 1633 return false; 1634 1635 if (fn_a == error_mark_node 1636 || fn_b == error_mark_node) 1637 return false; 1638 1639 for (tree args_a = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn_a))), 1640 args_b = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn_b))); 1641 args_a || args_b; 1642 args_a = TREE_CHAIN (args_a), args_b = TREE_CHAIN (args_b)) 1643 { 1644 if (!args_a || !args_b) 1645 return false; 1646 // This check also deals with differing variadicness 1647 if (!same_type_p (TREE_VALUE (args_a), TREE_VALUE (args_b))) 1648 return false; 1649 } 1650 1651 return true; 1652 } 1653 1654 // Record the per-scope discriminator of LAMBDA. If the extra scope 1655 // is empty, we must use the empty scope counter, which might not be 1656 // the live one. 1657 1658 void 1659 record_lambda_scope_discriminator (tree lambda) 1660 { 1661 auto *slot = (vec_safe_is_empty (lambda_scope_stack) 1662 || LAMBDA_EXPR_EXTRA_SCOPE (lambda) 1663 ? &lambda_scope : lambda_scope_stack->begin ()); 1664 LAMBDA_EXPR_SCOPE_ONLY_DISCRIMINATOR (lambda) = slot->count++; 1665 } 1666 1667 // Record the per-scope per-signature discriminator of LAMBDA. If the 1668 // extra scope is empty, we must use the empty scope counter, which 1669 // might not be the live one. 1670 1671 void 1672 record_lambda_scope_sig_discriminator (tree lambda, tree fn) 1673 { 1674 auto *slot = (vec_safe_is_empty (lambda_scope_stack) 1675 || LAMBDA_EXPR_EXTRA_SCOPE (lambda) 1676 ? &lambda_scope : lambda_scope_stack->begin ()); 1677 gcc_checking_assert (LAMBDA_EXPR_EXTRA_SCOPE (lambda) == slot->scope); 1678 1679 // A linear search, we're not expecting this to be a big list, and 1680 // this avoids needing a signature hash function. 1681 lambda_sig_count *sig; 1682 if (unsigned ix = vec_safe_length (slot->discriminators)) 1683 for (sig = slot->discriminators->begin (); ix--; sig++) 1684 if (compare_lambda_sig (fn, sig->fn)) 1685 goto found; 1686 { 1687 lambda_sig_count init = {fn, 0}; 1688 sig = vec_safe_push (slot->discriminators, init); 1689 } 1690 found: 1691 LAMBDA_EXPR_SCOPE_SIG_DISCRIMINATOR (lambda) = sig->count++; 1692 } 1693 1694 tree 1695 start_lambda_function (tree fco, tree lambda_expr) 1696 { 1697 /* Let the front end know that we are going to be defining this 1698 function. */ 1699 start_preparsed_function (fco, 1700 NULL_TREE, 1701 SF_PRE_PARSED | SF_INCLASS_INLINE); 1702 1703 tree body = begin_function_body (); 1704 1705 /* Push the proxies for any explicit captures. */ 1706 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap; 1707 cap = TREE_CHAIN (cap)) 1708 build_capture_proxy (TREE_PURPOSE (cap), TREE_VALUE (cap)); 1709 1710 return body; 1711 } 1712 1713 /* Subroutine of prune_lambda_captures: CAP is a node in 1714 LAMBDA_EXPR_CAPTURE_LIST. Return the variable it captures for which we 1715 might optimize away the capture, or NULL_TREE if there is no such 1716 variable. */ 1717 1718 static tree 1719 var_to_maybe_prune (tree cap) 1720 { 1721 if (LAMBDA_CAPTURE_EXPLICIT_P (cap)) 1722 /* Don't prune explicit captures. */ 1723 return NULL_TREE; 1724 1725 tree mem = TREE_PURPOSE (cap); 1726 if (!DECL_P (mem) || !DECL_NORMAL_CAPTURE_P (mem)) 1727 /* Packs and init-captures aren't captures of constant vars. */ 1728 return NULL_TREE; 1729 1730 tree init = TREE_VALUE (cap); 1731 if (is_normal_capture_proxy (init)) 1732 init = DECL_CAPTURED_VARIABLE (init); 1733 if (decl_constant_var_p (init)) 1734 return init; 1735 1736 return NULL_TREE; 1737 } 1738 1739 /* walk_tree helper for prune_lambda_captures: Remember which capture proxies 1740 for constant variables are actually used in the lambda body. 1741 1742 There will always be a DECL_EXPR for the capture proxy; remember it when we 1743 see it, but replace it with any other use. */ 1744 1745 static tree 1746 mark_const_cap_r (tree *t, int *walk_subtrees, void *data) 1747 { 1748 hash_map<tree,tree*> &const_vars = *(hash_map<tree,tree*>*)data; 1749 1750 tree var = NULL_TREE; 1751 if (TREE_CODE (*t) == DECL_EXPR) 1752 { 1753 tree decl = DECL_EXPR_DECL (*t); 1754 if (is_constant_capture_proxy (decl)) 1755 { 1756 var = DECL_CAPTURED_VARIABLE (decl); 1757 *walk_subtrees = 0; 1758 } 1759 } 1760 else if (!location_wrapper_p (*t) /* is_capture_proxy dislikes them. */ 1761 && is_constant_capture_proxy (*t)) 1762 var = DECL_CAPTURED_VARIABLE (*t); 1763 1764 if (var) 1765 { 1766 tree *&slot = const_vars.get_or_insert (var); 1767 if (!slot || VAR_P (*t)) 1768 slot = t; 1769 } 1770 1771 return NULL_TREE; 1772 } 1773 1774 /* We're at the end of processing a lambda; go back and remove any captures of 1775 constant variables for which we've folded away all uses. */ 1776 1777 static void 1778 prune_lambda_captures (tree body) 1779 { 1780 tree lam = current_lambda_expr (); 1781 if (!LAMBDA_EXPR_CAPTURE_OPTIMIZED (lam)) 1782 /* No uses were optimized away. */ 1783 return; 1784 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE) 1785 /* No default captures, and we don't prune explicit captures. */ 1786 return; 1787 /* Don't bother pruning in a template, we'll prune at instantiation time. */ 1788 if (dependent_type_p (TREE_TYPE (lam))) 1789 return; 1790 1791 hash_map<tree,tree*> const_vars; 1792 1793 cp_walk_tree_without_duplicates (&body, mark_const_cap_r, &const_vars); 1794 1795 tree *fieldp = &TYPE_FIELDS (LAMBDA_EXPR_CLOSURE (lam)); 1796 for (tree *capp = &LAMBDA_EXPR_CAPTURE_LIST (lam); *capp; ) 1797 { 1798 tree cap = *capp; 1799 if (tree var = var_to_maybe_prune (cap)) 1800 { 1801 tree **use = const_vars.get (var); 1802 if (use && TREE_CODE (**use) == DECL_EXPR) 1803 { 1804 /* All uses of this capture were folded away, leaving only the 1805 proxy declaration. */ 1806 1807 /* Splice the capture out of LAMBDA_EXPR_CAPTURE_LIST. */ 1808 *capp = TREE_CHAIN (cap); 1809 1810 /* And out of TYPE_FIELDS. */ 1811 tree field = TREE_PURPOSE (cap); 1812 while (*fieldp != field) 1813 fieldp = &DECL_CHAIN (*fieldp); 1814 *fieldp = DECL_CHAIN (*fieldp); 1815 1816 /* And remove the capture proxy declaration. */ 1817 **use = void_node; 1818 continue; 1819 } 1820 } 1821 1822 capp = &TREE_CHAIN (cap); 1823 } 1824 } 1825 1826 // Record the per-scope per-signature discriminator of LAMBDA. If the 1827 // extra scope is empty, we must use the empty scope counter, which 1828 // might not be the live one. 1829 1830 void 1831 finish_lambda_function (tree body) 1832 { 1833 finish_function_body (body); 1834 1835 prune_lambda_captures (body); 1836 1837 /* Finish the function and generate code for it if necessary. */ 1838 tree fn = finish_function (/*inline_p=*/true); 1839 1840 /* Only expand if the call op is not a template. */ 1841 if (!DECL_TEMPLATE_INFO (fn)) 1842 expand_or_defer_fn (fn); 1843 } 1844 1845 #include "gt-cp-lambda.h" 1846