1 /* Language-dependent node constructors for parse phase of GNU compiler. 2 Copyright (C) 1987-2022 Free Software Foundation, Inc. 3 Hacked by Michael Tiemann (tiemann (at) cygnus.com) 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "tree.h" 25 #include "cp-tree.h" 26 #include "gimple-expr.h" 27 #include "cgraph.h" 28 #include "stor-layout.h" 29 #include "print-tree.h" 30 #include "tree-iterator.h" 31 #include "tree-inline.h" 32 #include "debug.h" 33 #include "convert.h" 34 #include "gimplify.h" 35 #include "stringpool.h" 36 #include "attribs.h" 37 #include "flags.h" 38 #include "selftest.h" 39 40 static tree bot_manip (tree *, int *, void *); 41 static tree bot_replace (tree *, int *, void *); 42 static hashval_t list_hash_pieces (tree, tree, tree); 43 static tree build_target_expr (tree, tree, tsubst_flags_t); 44 static tree count_trees_r (tree *, int *, void *); 45 static tree verify_stmt_tree_r (tree *, int *, void *); 46 47 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *); 48 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *); 49 50 /* If REF is an lvalue, returns the kind of lvalue that REF is. 51 Otherwise, returns clk_none. */ 52 53 cp_lvalue_kind 54 lvalue_kind (const_tree ref) 55 { 56 cp_lvalue_kind op1_lvalue_kind = clk_none; 57 cp_lvalue_kind op2_lvalue_kind = clk_none; 58 59 /* Expressions of reference type are sometimes wrapped in 60 INDIRECT_REFs. INDIRECT_REFs are just internal compiler 61 representation, not part of the language, so we have to look 62 through them. */ 63 if (REFERENCE_REF_P (ref)) 64 return lvalue_kind (TREE_OPERAND (ref, 0)); 65 66 if (TREE_TYPE (ref) 67 && TYPE_REF_P (TREE_TYPE (ref))) 68 { 69 /* unnamed rvalue references are rvalues */ 70 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref)) 71 && TREE_CODE (ref) != PARM_DECL 72 && !VAR_P (ref) 73 && TREE_CODE (ref) != COMPONENT_REF 74 /* Functions are always lvalues. */ 75 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE) 76 { 77 op1_lvalue_kind = clk_rvalueref; 78 if (implicit_rvalue_p (ref)) 79 op1_lvalue_kind |= clk_implicit_rval; 80 return op1_lvalue_kind; 81 } 82 83 /* lvalue references and named rvalue references are lvalues. */ 84 return clk_ordinary; 85 } 86 87 if (ref == current_class_ptr) 88 return clk_none; 89 90 /* Expressions with cv void type are prvalues. */ 91 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref))) 92 return clk_none; 93 94 switch (TREE_CODE (ref)) 95 { 96 case SAVE_EXPR: 97 return clk_none; 98 99 /* preincrements and predecrements are valid lvals, provided 100 what they refer to are valid lvals. */ 101 case PREINCREMENT_EXPR: 102 case PREDECREMENT_EXPR: 103 case TRY_CATCH_EXPR: 104 case REALPART_EXPR: 105 case IMAGPART_EXPR: 106 case VIEW_CONVERT_EXPR: 107 return lvalue_kind (TREE_OPERAND (ref, 0)); 108 109 case ARRAY_REF: 110 { 111 tree op1 = TREE_OPERAND (ref, 0); 112 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE) 113 { 114 op1_lvalue_kind = lvalue_kind (op1); 115 if (op1_lvalue_kind == clk_class) 116 /* in the case of an array operand, the result is an lvalue if 117 that operand is an lvalue and an xvalue otherwise */ 118 op1_lvalue_kind = clk_rvalueref; 119 return op1_lvalue_kind; 120 } 121 else 122 return clk_ordinary; 123 } 124 125 case MEMBER_REF: 126 case DOTSTAR_EXPR: 127 if (TREE_CODE (ref) == MEMBER_REF) 128 op1_lvalue_kind = clk_ordinary; 129 else 130 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0)); 131 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) 132 op1_lvalue_kind = clk_none; 133 else if (op1_lvalue_kind == clk_class) 134 /* The result of a .* expression whose second operand is a pointer to a 135 data member is an lvalue if the first operand is an lvalue and an 136 xvalue otherwise. */ 137 op1_lvalue_kind = clk_rvalueref; 138 return op1_lvalue_kind; 139 140 case COMPONENT_REF: 141 if (BASELINK_P (TREE_OPERAND (ref, 1))) 142 { 143 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1)); 144 145 /* For static member function recurse on the BASELINK, we can get 146 here e.g. from reference_binding. If BASELINK_FUNCTIONS is 147 OVERLOAD, the overload is resolved first if possible through 148 resolve_address_of_overloaded_function. */ 149 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn)) 150 return lvalue_kind (TREE_OPERAND (ref, 1)); 151 } 152 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0)); 153 if (op1_lvalue_kind == clk_class) 154 /* If E1 is an lvalue, then E1.E2 is an lvalue; 155 otherwise E1.E2 is an xvalue. */ 156 op1_lvalue_kind = clk_rvalueref; 157 158 /* Look at the member designator. */ 159 if (!op1_lvalue_kind) 160 ; 161 else if (is_overloaded_fn (TREE_OPERAND (ref, 1))) 162 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some 163 situations. If we're seeing a COMPONENT_REF, it's a non-static 164 member, so it isn't an lvalue. */ 165 op1_lvalue_kind = clk_none; 166 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL) 167 /* This can be IDENTIFIER_NODE in a template. */; 168 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1))) 169 { 170 /* Clear the ordinary bit. If this object was a class 171 rvalue we want to preserve that information. */ 172 op1_lvalue_kind &= ~clk_ordinary; 173 /* The lvalue is for a bitfield. */ 174 op1_lvalue_kind |= clk_bitfield; 175 } 176 else if (DECL_PACKED (TREE_OPERAND (ref, 1))) 177 op1_lvalue_kind |= clk_packed; 178 179 return op1_lvalue_kind; 180 181 case STRING_CST: 182 case COMPOUND_LITERAL_EXPR: 183 return clk_ordinary; 184 185 case CONST_DECL: 186 /* CONST_DECL without TREE_STATIC are enumeration values and 187 thus not lvalues. With TREE_STATIC they are used by ObjC++ 188 in objc_build_string_object and need to be considered as 189 lvalues. */ 190 if (! TREE_STATIC (ref)) 191 return clk_none; 192 /* FALLTHRU */ 193 case VAR_DECL: 194 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref)) 195 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref))); 196 197 if (TREE_READONLY (ref) && ! TREE_STATIC (ref) 198 && DECL_LANG_SPECIFIC (ref) 199 && DECL_IN_AGGR_P (ref)) 200 return clk_none; 201 /* FALLTHRU */ 202 case INDIRECT_REF: 203 case ARROW_EXPR: 204 case PARM_DECL: 205 case RESULT_DECL: 206 case PLACEHOLDER_EXPR: 207 return clk_ordinary; 208 209 /* A scope ref in a template, left as SCOPE_REF to support later 210 access checking. */ 211 case SCOPE_REF: 212 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref))); 213 { 214 tree op = TREE_OPERAND (ref, 1); 215 if (TREE_CODE (op) == FIELD_DECL) 216 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary); 217 else 218 return lvalue_kind (op); 219 } 220 221 case MAX_EXPR: 222 case MIN_EXPR: 223 /* Disallow <? and >? as lvalues if either argument side-effects. */ 224 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0)) 225 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1))) 226 return clk_none; 227 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0)); 228 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)); 229 break; 230 231 case COND_EXPR: 232 if (processing_template_decl) 233 { 234 /* Within templates, a REFERENCE_TYPE will indicate whether 235 the COND_EXPR result is an ordinary lvalue or rvalueref. 236 Since REFERENCE_TYPEs are handled above, if we reach this 237 point, we know we got a plain rvalue. Unless we have a 238 type-dependent expr, that is, but we shouldn't be testing 239 lvalueness if we can't even tell the types yet! */ 240 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref))); 241 goto default_; 242 } 243 { 244 tree op1 = TREE_OPERAND (ref, 1); 245 if (!op1) op1 = TREE_OPERAND (ref, 0); 246 tree op2 = TREE_OPERAND (ref, 2); 247 op1_lvalue_kind = lvalue_kind (op1); 248 op2_lvalue_kind = lvalue_kind (op2); 249 if (!op1_lvalue_kind != !op2_lvalue_kind) 250 { 251 /* The second or the third operand (but not both) is a 252 throw-expression; the result is of the type 253 and value category of the other. */ 254 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR) 255 op2_lvalue_kind = op1_lvalue_kind; 256 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR) 257 op1_lvalue_kind = op2_lvalue_kind; 258 } 259 } 260 break; 261 262 case MODOP_EXPR: 263 /* We expect to see unlowered MODOP_EXPRs only during 264 template processing. */ 265 gcc_assert (processing_template_decl); 266 return clk_ordinary; 267 268 case MODIFY_EXPR: 269 case TYPEID_EXPR: 270 return clk_ordinary; 271 272 case COMPOUND_EXPR: 273 return lvalue_kind (TREE_OPERAND (ref, 1)); 274 275 case TARGET_EXPR: 276 return clk_class; 277 278 case VA_ARG_EXPR: 279 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none); 280 281 case CALL_EXPR: 282 /* We can see calls outside of TARGET_EXPR in templates. */ 283 if (CLASS_TYPE_P (TREE_TYPE (ref))) 284 return clk_class; 285 return clk_none; 286 287 case FUNCTION_DECL: 288 /* All functions (except non-static-member functions) are 289 lvalues. */ 290 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref) 291 ? clk_none : clk_ordinary); 292 293 case BASELINK: 294 /* We now represent a reference to a single static member function 295 with a BASELINK. */ 296 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns 297 its argument unmodified and we assign it to a const_tree. */ 298 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref))); 299 300 case NON_DEPENDENT_EXPR: 301 case PAREN_EXPR: 302 return lvalue_kind (TREE_OPERAND (ref, 0)); 303 304 case TEMPLATE_PARM_INDEX: 305 if (CLASS_TYPE_P (TREE_TYPE (ref))) 306 /* A template parameter object is an lvalue. */ 307 return clk_ordinary; 308 return clk_none; 309 310 default: 311 default_: 312 if (!TREE_TYPE (ref)) 313 return clk_none; 314 if (CLASS_TYPE_P (TREE_TYPE (ref)) 315 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE) 316 return clk_class; 317 return clk_none; 318 } 319 320 /* If one operand is not an lvalue at all, then this expression is 321 not an lvalue. */ 322 if (!op1_lvalue_kind || !op2_lvalue_kind) 323 return clk_none; 324 325 /* Otherwise, it's an lvalue, and it has all the odd properties 326 contributed by either operand. */ 327 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind; 328 /* It's not an ordinary lvalue if it involves any other kind. */ 329 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none) 330 op1_lvalue_kind &= ~clk_ordinary; 331 /* It can't be both a pseudo-lvalue and a non-addressable lvalue. 332 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */ 333 if ((op1_lvalue_kind & (clk_rvalueref|clk_class)) 334 && (op1_lvalue_kind & (clk_bitfield|clk_packed))) 335 op1_lvalue_kind = clk_none; 336 return op1_lvalue_kind; 337 } 338 339 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */ 340 341 cp_lvalue_kind 342 real_lvalue_p (const_tree ref) 343 { 344 cp_lvalue_kind kind = lvalue_kind (ref); 345 if (kind & (clk_rvalueref|clk_class)) 346 return clk_none; 347 else 348 return kind; 349 } 350 351 /* c-common wants us to return bool. */ 352 353 bool 354 lvalue_p (const_tree t) 355 { 356 return real_lvalue_p (t); 357 } 358 359 /* This differs from lvalue_p in that xvalues are included. */ 360 361 bool 362 glvalue_p (const_tree ref) 363 { 364 cp_lvalue_kind kind = lvalue_kind (ref); 365 if (kind & clk_class) 366 return false; 367 else 368 return (kind != clk_none); 369 } 370 371 /* This differs from glvalue_p in that class prvalues are included. */ 372 373 bool 374 obvalue_p (const_tree ref) 375 { 376 return (lvalue_kind (ref) != clk_none); 377 } 378 379 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue 380 reference), false otherwise. */ 381 382 bool 383 xvalue_p (const_tree ref) 384 { 385 return (lvalue_kind (ref) == clk_rvalueref); 386 } 387 388 /* True if REF is a bit-field. */ 389 390 bool 391 bitfield_p (const_tree ref) 392 { 393 return (lvalue_kind (ref) & clk_bitfield); 394 } 395 396 /* C++-specific version of stabilize_reference. */ 397 398 tree 399 cp_stabilize_reference (tree ref) 400 { 401 STRIP_ANY_LOCATION_WRAPPER (ref); 402 switch (TREE_CODE (ref)) 403 { 404 case NON_DEPENDENT_EXPR: 405 /* We aren't actually evaluating this. */ 406 return ref; 407 408 /* We need to treat specially anything stabilize_reference doesn't 409 handle specifically. */ 410 case VAR_DECL: 411 case PARM_DECL: 412 case RESULT_DECL: 413 CASE_CONVERT: 414 case FLOAT_EXPR: 415 case FIX_TRUNC_EXPR: 416 case INDIRECT_REF: 417 case COMPONENT_REF: 418 case BIT_FIELD_REF: 419 case ARRAY_REF: 420 case ARRAY_RANGE_REF: 421 case ERROR_MARK: 422 break; 423 default: 424 cp_lvalue_kind kind = lvalue_kind (ref); 425 if ((kind & ~clk_class) != clk_none) 426 { 427 tree type = unlowered_expr_type (ref); 428 bool rval = !!(kind & clk_rvalueref); 429 type = cp_build_reference_type (type, rval); 430 /* This inhibits warnings in, eg, cxx_mark_addressable 431 (c++/60955). */ 432 warning_sentinel s (extra_warnings); 433 ref = build_static_cast (input_location, type, ref, 434 tf_error); 435 } 436 } 437 438 return stabilize_reference (ref); 439 } 440 441 /* Test whether DECL is a builtin that may appear in a 442 constant-expression. */ 443 444 bool 445 builtin_valid_in_constant_expr_p (const_tree decl) 446 { 447 STRIP_ANY_LOCATION_WRAPPER (decl); 448 if (TREE_CODE (decl) != FUNCTION_DECL) 449 /* Not a function. */ 450 return false; 451 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL) 452 { 453 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND)) 454 switch (DECL_FE_FUNCTION_CODE (decl)) 455 { 456 case CP_BUILT_IN_IS_CONSTANT_EVALUATED: 457 case CP_BUILT_IN_SOURCE_LOCATION: 458 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER: 459 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS: 460 return true; 461 default: 462 break; 463 } 464 /* Not a built-in. */ 465 return false; 466 } 467 switch (DECL_FUNCTION_CODE (decl)) 468 { 469 /* These always have constant results like the corresponding 470 macros/symbol. */ 471 case BUILT_IN_FILE: 472 case BUILT_IN_FUNCTION: 473 case BUILT_IN_LINE: 474 475 /* The following built-ins are valid in constant expressions 476 when their arguments are. */ 477 case BUILT_IN_ADD_OVERFLOW_P: 478 case BUILT_IN_SUB_OVERFLOW_P: 479 case BUILT_IN_MUL_OVERFLOW_P: 480 481 /* These have constant results even if their operands are 482 non-constant. */ 483 case BUILT_IN_CONSTANT_P: 484 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: 485 return true; 486 default: 487 return false; 488 } 489 } 490 491 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */ 492 493 static tree 494 build_target_expr (tree decl, tree value, tsubst_flags_t complain) 495 { 496 tree t; 497 tree type = TREE_TYPE (decl); 498 499 value = mark_rvalue_use (value); 500 501 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value)) 502 || TREE_TYPE (decl) == TREE_TYPE (value) 503 /* On ARM ctors return 'this'. */ 504 || (TYPE_PTR_P (TREE_TYPE (value)) 505 && TREE_CODE (value) == CALL_EXPR) 506 || useless_type_conversion_p (TREE_TYPE (decl), 507 TREE_TYPE (value))); 508 509 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor 510 moving a constant aggregate into .rodata. */ 511 if (CP_TYPE_CONST_NON_VOLATILE_P (type) 512 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 513 && !VOID_TYPE_P (TREE_TYPE (value)) 514 && reduced_constant_expression_p (value)) 515 TREE_READONLY (decl) = true; 516 517 if (complain & tf_no_cleanup) 518 /* The caller is building a new-expr and does not need a cleanup. */ 519 t = NULL_TREE; 520 else 521 { 522 t = cxx_maybe_build_cleanup (decl, complain); 523 if (t == error_mark_node) 524 return error_mark_node; 525 } 526 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE); 527 if (location_t eloc = cp_expr_location (value)) 528 SET_EXPR_LOCATION (t, eloc); 529 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not 530 ignore the TARGET_EXPR. If there really turn out to be no 531 side-effects, then the optimizer should be able to get rid of 532 whatever code is generated anyhow. */ 533 TREE_SIDE_EFFECTS (t) = 1; 534 535 return t; 536 } 537 538 /* Return an undeclared local temporary of type TYPE for use in building a 539 TARGET_EXPR. */ 540 541 tree 542 build_local_temp (tree type) 543 { 544 tree slot = build_decl (input_location, 545 VAR_DECL, NULL_TREE, type); 546 DECL_ARTIFICIAL (slot) = 1; 547 DECL_IGNORED_P (slot) = 1; 548 DECL_CONTEXT (slot) = current_function_decl; 549 layout_decl (slot, 0); 550 return slot; 551 } 552 553 /* Return whether DECL is such a local temporary (or one from 554 create_tmp_var_raw). */ 555 556 bool 557 is_local_temp (tree decl) 558 { 559 return (VAR_P (decl) && DECL_ARTIFICIAL (decl) 560 && !TREE_STATIC (decl)); 561 } 562 563 /* Set various status flags when building an AGGR_INIT_EXPR object T. */ 564 565 static void 566 process_aggr_init_operands (tree t) 567 { 568 bool side_effects; 569 570 side_effects = TREE_SIDE_EFFECTS (t); 571 if (!side_effects) 572 { 573 int i, n; 574 n = TREE_OPERAND_LENGTH (t); 575 for (i = 1; i < n; i++) 576 { 577 tree op = TREE_OPERAND (t, i); 578 if (op && TREE_SIDE_EFFECTS (op)) 579 { 580 side_effects = 1; 581 break; 582 } 583 } 584 } 585 TREE_SIDE_EFFECTS (t) = side_effects; 586 } 587 588 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE, 589 FN, and SLOT. NARGS is the number of call arguments which are specified 590 as a tree array ARGS. */ 591 592 static tree 593 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs, 594 tree *args) 595 { 596 tree t; 597 int i; 598 599 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3); 600 TREE_TYPE (t) = return_type; 601 AGGR_INIT_EXPR_FN (t) = fn; 602 AGGR_INIT_EXPR_SLOT (t) = slot; 603 for (i = 0; i < nargs; i++) 604 AGGR_INIT_EXPR_ARG (t, i) = args[i]; 605 process_aggr_init_operands (t); 606 return t; 607 } 608 609 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its 610 target. TYPE is the type to be initialized. 611 612 Build an AGGR_INIT_EXPR to represent the initialization. This function 613 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used 614 to initialize another object, whereas a TARGET_EXPR can either 615 initialize another object or create its own temporary object, and as a 616 result building up a TARGET_EXPR requires that the type's destructor be 617 callable. */ 618 619 tree 620 build_aggr_init_expr (tree type, tree init) 621 { 622 tree fn; 623 tree slot; 624 tree rval; 625 int is_ctor; 626 627 gcc_assert (!VOID_TYPE_P (type)); 628 629 /* Don't build AGGR_INIT_EXPR in a template. */ 630 if (processing_template_decl) 631 return init; 632 633 fn = cp_get_callee (init); 634 if (fn == NULL_TREE) 635 return convert (type, init); 636 637 is_ctor = (TREE_CODE (fn) == ADDR_EXPR 638 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL 639 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0))); 640 641 /* We split the CALL_EXPR into its function and its arguments here. 642 Then, in expand_expr, we put them back together. The reason for 643 this is that this expression might be a default argument 644 expression. In that case, we need a new temporary every time the 645 expression is used. That's what break_out_target_exprs does; it 646 replaces every AGGR_INIT_EXPR with a copy that uses a fresh 647 temporary slot. Then, expand_expr builds up a call-expression 648 using the new slot. */ 649 650 /* If we don't need to use a constructor to create an object of this 651 type, don't mess with AGGR_INIT_EXPR. */ 652 if (is_ctor || TREE_ADDRESSABLE (type)) 653 { 654 slot = build_local_temp (type); 655 656 if (TREE_CODE (init) == CALL_EXPR) 657 { 658 rval = build_aggr_init_array (void_type_node, fn, slot, 659 call_expr_nargs (init), 660 CALL_EXPR_ARGP (init)); 661 AGGR_INIT_FROM_THUNK_P (rval) 662 = CALL_FROM_THUNK_P (init); 663 } 664 else 665 { 666 rval = build_aggr_init_array (void_type_node, fn, slot, 667 aggr_init_expr_nargs (init), 668 AGGR_INIT_EXPR_ARGP (init)); 669 AGGR_INIT_FROM_THUNK_P (rval) 670 = AGGR_INIT_FROM_THUNK_P (init); 671 } 672 TREE_SIDE_EFFECTS (rval) = 1; 673 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor; 674 TREE_NOTHROW (rval) = TREE_NOTHROW (init); 675 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init); 676 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init); 677 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init); 678 } 679 else 680 rval = init; 681 682 return rval; 683 } 684 685 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its 686 target. TYPE is the type that this initialization should appear to 687 have. 688 689 Build an encapsulation of the initialization to perform 690 and return it so that it can be processed by language-independent 691 and language-specific expression expanders. */ 692 693 tree 694 build_cplus_new (tree type, tree init, tsubst_flags_t complain) 695 { 696 /* This function should cope with what build_special_member_call 697 can produce. When performing parenthesized aggregate initialization, 698 it can produce a { }. */ 699 if (BRACE_ENCLOSED_INITIALIZER_P (init)) 700 { 701 gcc_assert (cxx_dialect >= cxx20); 702 return finish_compound_literal (type, init, complain); 703 } 704 705 tree rval = build_aggr_init_expr (type, init); 706 tree slot; 707 708 if (init == error_mark_node) 709 return error_mark_node; 710 711 if (!complete_type_or_maybe_complain (type, init, complain)) 712 return error_mark_node; 713 714 /* Make sure that we're not trying to create an instance of an 715 abstract class. */ 716 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain)) 717 return error_mark_node; 718 719 if (TREE_CODE (rval) == AGGR_INIT_EXPR) 720 slot = AGGR_INIT_EXPR_SLOT (rval); 721 else if (TREE_CODE (rval) == CALL_EXPR 722 || TREE_CODE (rval) == CONSTRUCTOR) 723 slot = build_local_temp (type); 724 else 725 return rval; 726 727 rval = build_target_expr (slot, rval, complain); 728 729 if (rval != error_mark_node) 730 TARGET_EXPR_IMPLICIT_P (rval) = 1; 731 732 return rval; 733 } 734 735 /* Subroutine of build_vec_init_expr: Build up a single element 736 intialization as a proxy for the full array initialization to get things 737 marked as used and any appropriate diagnostics. 738 739 This used to be necessary because we were deferring building the actual 740 constructor calls until gimplification time; now we only do it to set 741 VEC_INIT_EXPR_IS_CONSTEXPR. 742 743 We assume that init is either NULL_TREE, {}, void_type_node (indicating 744 value-initialization), or another array to copy. */ 745 746 static tree 747 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain) 748 { 749 tree inner_type = strip_array_types (type); 750 751 if (integer_zerop (array_type_nelts_total (type)) 752 || !CLASS_TYPE_P (inner_type)) 753 /* No interesting initialization to do. */ 754 return integer_zero_node; 755 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) 756 { 757 /* Even if init has initializers for some array elements, 758 we're interested in the {}-init of trailing elements. */ 759 if (CP_AGGREGATE_TYPE_P (inner_type)) 760 { 761 tree empty = build_constructor (init_list_type_node, nullptr); 762 return digest_init (inner_type, empty, complain); 763 } 764 else 765 /* It's equivalent to value-init. */ 766 init = void_type_node; 767 } 768 if (init == void_type_node) 769 return build_value_init (inner_type, complain); 770 771 releasing_vec argvec; 772 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)) 773 { 774 gcc_assert (same_type_ignoring_top_level_qualifiers_p 775 (type, TREE_TYPE (init))); 776 tree init_type = strip_array_types (TREE_TYPE (init)); 777 tree dummy = build_dummy_object (init_type); 778 if (!lvalue_p (init)) 779 dummy = move (dummy); 780 argvec->quick_push (dummy); 781 } 782 init = build_special_member_call (NULL_TREE, complete_ctor_identifier, 783 &argvec, inner_type, LOOKUP_NORMAL, 784 complain); 785 786 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But 787 we don't want one here because we aren't creating a temporary. */ 788 if (TREE_CODE (init) == TARGET_EXPR) 789 init = TARGET_EXPR_INITIAL (init); 790 791 return init; 792 } 793 794 /* Return a TARGET_EXPR which expresses the initialization of an array to 795 be named later, either default-initialization or copy-initialization 796 from another array of the same type. */ 797 798 tree 799 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain) 800 { 801 if (tree vi = get_vec_init_expr (init)) 802 return vi; 803 804 tree elt_init; 805 if (init && TREE_CODE (init) == CONSTRUCTOR 806 && !BRACE_ENCLOSED_INITIALIZER_P (init)) 807 /* We built any needed constructor calls in digest_init. */ 808 elt_init = init; 809 else 810 elt_init = build_vec_init_elt (type, init, complain); 811 812 bool value_init = false; 813 if (init == void_type_node) 814 { 815 value_init = true; 816 init = NULL_TREE; 817 } 818 819 tree slot = build_local_temp (type); 820 init = build2 (VEC_INIT_EXPR, type, slot, init); 821 TREE_SIDE_EFFECTS (init) = true; 822 SET_EXPR_LOCATION (init, input_location); 823 824 if (cxx_dialect >= cxx11) 825 { 826 bool cx = potential_constant_expression (elt_init); 827 if (BRACE_ENCLOSED_INITIALIZER_P (init)) 828 cx &= potential_constant_expression (init); 829 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx; 830 } 831 VEC_INIT_EXPR_VALUE_INIT (init) = value_init; 832 833 return init; 834 } 835 836 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE 837 means VEC_INIT_EXPR_SLOT). */ 838 839 tree 840 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain, 841 vec<tree,va_gc> **flags) 842 { 843 iloc_sentinel ils = EXPR_LOCATION (vec_init); 844 845 if (!target) 846 target = VEC_INIT_EXPR_SLOT (vec_init); 847 tree init = VEC_INIT_EXPR_INIT (vec_init); 848 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE); 849 return build_vec_init (target, NULL_TREE, init, 850 VEC_INIT_EXPR_VALUE_INIT (vec_init), 851 from_array, complain, flags); 852 } 853 854 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context 855 that requires a constant expression. */ 856 857 void 858 diagnose_non_constexpr_vec_init (tree expr) 859 { 860 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr)); 861 tree init, elt_init; 862 if (VEC_INIT_EXPR_VALUE_INIT (expr)) 863 init = void_type_node; 864 else 865 init = VEC_INIT_EXPR_INIT (expr); 866 867 elt_init = build_vec_init_elt (type, init, tf_warning_or_error); 868 require_potential_constant_expression (elt_init); 869 } 870 871 tree 872 build_array_copy (tree init) 873 { 874 return get_target_expr (build_vec_init_expr 875 (TREE_TYPE (init), init, tf_warning_or_error)); 876 } 877 878 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the 879 indicated TYPE. */ 880 881 tree 882 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain) 883 { 884 gcc_assert (!VOID_TYPE_P (type)); 885 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init))); 886 887 if (TREE_CODE (init) == TARGET_EXPR 888 || init == error_mark_node) 889 return init; 890 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type) 891 && TREE_CODE (init) != COND_EXPR 892 && TREE_CODE (init) != CONSTRUCTOR 893 && TREE_CODE (init) != VA_ARG_EXPR 894 && TREE_CODE (init) != CALL_EXPR) 895 /* We need to build up a copy constructor call. COND_EXPR is a special 896 case because we already have copies on the arms and we don't want 897 another one here. A CONSTRUCTOR is aggregate initialization, which 898 is handled separately. A VA_ARG_EXPR is magic creation of an 899 aggregate; there's no additional work to be done. A CALL_EXPR 900 already creates a prvalue. */ 901 return force_rvalue (init, complain); 902 903 return force_target_expr (type, init, complain); 904 } 905 906 /* Like the above function, but without the checking. This function should 907 only be used by code which is deliberately trying to subvert the type 908 system, such as call_builtin_trap. Or build_over_call, to avoid 909 infinite recursion. */ 910 911 tree 912 force_target_expr (tree type, tree init, tsubst_flags_t complain) 913 { 914 tree slot; 915 916 gcc_assert (!VOID_TYPE_P (type)); 917 918 slot = build_local_temp (type); 919 return build_target_expr (slot, init, complain); 920 } 921 922 /* Like build_target_expr_with_type, but use the type of INIT. */ 923 924 tree 925 get_target_expr_sfinae (tree init, tsubst_flags_t complain) 926 { 927 if (TREE_CODE (init) == AGGR_INIT_EXPR) 928 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain); 929 else if (TREE_CODE (init) == VEC_INIT_EXPR) 930 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain); 931 else 932 { 933 init = convert_bitfield_to_declared_type (init); 934 return build_target_expr_with_type (init, TREE_TYPE (init), complain); 935 } 936 } 937 938 tree 939 get_target_expr (tree init) 940 { 941 return get_target_expr_sfinae (init, tf_warning_or_error); 942 } 943 944 /* If EXPR is a bitfield reference, convert it to the declared type of 945 the bitfield, and return the resulting expression. Otherwise, 946 return EXPR itself. */ 947 948 tree 949 convert_bitfield_to_declared_type (tree expr) 950 { 951 tree bitfield_type; 952 953 bitfield_type = is_bitfield_expr_with_lowered_type (expr); 954 if (bitfield_type) 955 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), 956 expr); 957 return expr; 958 } 959 960 /* EXPR is being used in an rvalue context. Return a version of EXPR 961 that is marked as an rvalue. */ 962 963 tree 964 rvalue (tree expr) 965 { 966 tree type; 967 968 if (error_operand_p (expr)) 969 return expr; 970 971 expr = mark_rvalue_use (expr); 972 973 /* [expr.type]: "If a prvalue initially has the type "cv T", where T is a 974 cv-unqualified non-class, non-array type, the type of the expression is 975 adjusted to T prior to any further analysis. */ 976 type = TREE_TYPE (expr); 977 if (!CLASS_TYPE_P (type) && TREE_CODE (type) != ARRAY_TYPE 978 && cv_qualified_p (type)) 979 type = cv_unqualified (type); 980 981 /* We need to do this for rvalue refs as well to get the right answer 982 from decltype; see c++/36628. */ 983 if (!processing_template_decl && glvalue_p (expr)) 984 { 985 /* But don't use this function for class lvalues; use move (to treat an 986 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */ 987 gcc_checking_assert (!CLASS_TYPE_P (type)); 988 expr = build1 (NON_LVALUE_EXPR, type, expr); 989 } 990 else if (type != TREE_TYPE (expr)) 991 expr = build_nop (type, expr); 992 993 return expr; 994 } 995 996 997 struct cplus_array_info 999 { 1000 tree type; 1001 tree domain; 1002 }; 1003 1004 struct cplus_array_hasher : ggc_ptr_hash<tree_node> 1005 { 1006 typedef cplus_array_info *compare_type; 1007 1008 static hashval_t hash (tree t); 1009 static bool equal (tree, cplus_array_info *); 1010 }; 1011 1012 /* Hash an ARRAY_TYPE. K is really of type `tree'. */ 1013 1014 hashval_t 1015 cplus_array_hasher::hash (tree t) 1016 { 1017 hashval_t hash; 1018 1019 hash = TYPE_UID (TREE_TYPE (t)); 1020 if (TYPE_DOMAIN (t)) 1021 hash ^= TYPE_UID (TYPE_DOMAIN (t)); 1022 return hash; 1023 } 1024 1025 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really 1026 of type `cplus_array_info*'. */ 1027 1028 bool 1029 cplus_array_hasher::equal (tree t1, cplus_array_info *t2) 1030 { 1031 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain); 1032 } 1033 1034 /* Hash table containing dependent array types, which are unsuitable for 1035 the language-independent type hash table. */ 1036 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab; 1037 1038 /* Build an ARRAY_TYPE without laying it out. */ 1039 1040 static tree 1041 build_min_array_type (tree elt_type, tree index_type) 1042 { 1043 tree t = cxx_make_type (ARRAY_TYPE); 1044 TREE_TYPE (t) = elt_type; 1045 TYPE_DOMAIN (t) = index_type; 1046 return t; 1047 } 1048 1049 /* Set TYPE_CANONICAL like build_array_type_1, but using 1050 build_cplus_array_type. */ 1051 1052 static void 1053 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep) 1054 { 1055 /* Set the canonical type for this new node. */ 1056 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type) 1057 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))) 1058 SET_TYPE_STRUCTURAL_EQUALITY (t); 1059 else if (TYPE_CANONICAL (elt_type) != elt_type 1060 || (index_type && TYPE_CANONICAL (index_type) != index_type)) 1061 TYPE_CANONICAL (t) 1062 = build_cplus_array_type (TYPE_CANONICAL (elt_type), 1063 index_type 1064 ? TYPE_CANONICAL (index_type) : index_type, 1065 dep); 1066 else 1067 TYPE_CANONICAL (t) = t; 1068 } 1069 1070 /* Like build_array_type, but handle special C++ semantics: an array of a 1071 variant element type is a variant of the array of the main variant of 1072 the element type. IS_DEPENDENT is -ve if we should determine the 1073 dependency. Otherwise its bool value indicates dependency. */ 1074 1075 tree 1076 build_cplus_array_type (tree elt_type, tree index_type, int dependent) 1077 { 1078 tree t; 1079 1080 if (elt_type == error_mark_node || index_type == error_mark_node) 1081 return error_mark_node; 1082 1083 if (dependent < 0) 1084 dependent = (uses_template_parms (elt_type) 1085 || (index_type && uses_template_parms (index_type))); 1086 1087 if (elt_type != TYPE_MAIN_VARIANT (elt_type)) 1088 /* Start with an array of the TYPE_MAIN_VARIANT. */ 1089 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type), 1090 index_type, dependent); 1091 else if (dependent) 1092 { 1093 /* Since type_hash_canon calls layout_type, we need to use our own 1094 hash table. */ 1095 cplus_array_info cai; 1096 hashval_t hash; 1097 1098 if (cplus_array_htab == NULL) 1099 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61); 1100 1101 hash = TYPE_UID (elt_type); 1102 if (index_type) 1103 hash ^= TYPE_UID (index_type); 1104 cai.type = elt_type; 1105 cai.domain = index_type; 1106 1107 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT); 1108 if (*e) 1109 /* We have found the type: we're done. */ 1110 return (tree) *e; 1111 else 1112 { 1113 /* Build a new array type. */ 1114 t = build_min_array_type (elt_type, index_type); 1115 1116 /* Store it in the hash table. */ 1117 *e = t; 1118 1119 /* Set the canonical type for this new node. */ 1120 set_array_type_canon (t, elt_type, index_type, dependent); 1121 1122 /* Mark it as dependent now, this saves time later. */ 1123 TYPE_DEPENDENT_P_VALID (t) = true; 1124 TYPE_DEPENDENT_P (t) = true; 1125 } 1126 } 1127 else 1128 { 1129 bool typeless_storage = is_byte_access_type (elt_type); 1130 t = build_array_type (elt_type, index_type, typeless_storage); 1131 1132 /* Mark as non-dependenty now, this will save time later. */ 1133 TYPE_DEPENDENT_P_VALID (t) = true; 1134 } 1135 1136 /* Now check whether we already have this array variant. */ 1137 if (elt_type != TYPE_MAIN_VARIANT (elt_type)) 1138 { 1139 tree m = t; 1140 for (t = m; t; t = TYPE_NEXT_VARIANT (t)) 1141 if (TREE_TYPE (t) == elt_type 1142 && TYPE_NAME (t) == NULL_TREE 1143 && TYPE_ATTRIBUTES (t) == NULL_TREE) 1144 break; 1145 if (!t) 1146 { 1147 t = build_min_array_type (elt_type, index_type); 1148 /* Mark dependency now, this saves time later. */ 1149 TYPE_DEPENDENT_P_VALID (t) = true; 1150 TYPE_DEPENDENT_P (t) = dependent; 1151 set_array_type_canon (t, elt_type, index_type, dependent); 1152 if (!dependent) 1153 { 1154 layout_type (t); 1155 /* Make sure sizes are shared with the main variant. 1156 layout_type can't be called after setting TYPE_NEXT_VARIANT, 1157 as it will overwrite alignment etc. of all variants. */ 1158 TYPE_SIZE (t) = TYPE_SIZE (m); 1159 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m); 1160 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m); 1161 } 1162 1163 TYPE_MAIN_VARIANT (t) = m; 1164 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m); 1165 TYPE_NEXT_VARIANT (m) = t; 1166 } 1167 } 1168 1169 /* Avoid spurious warnings with VLAs (c++/54583). */ 1170 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t))) 1171 suppress_warning (TYPE_SIZE (t), OPT_Wunused); 1172 1173 /* Push these needs up to the ARRAY_TYPE so that initialization takes 1174 place more easily. */ 1175 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t) 1176 = TYPE_NEEDS_CONSTRUCTING (elt_type)); 1177 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 1178 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type)); 1179 1180 if (!dependent && t == TYPE_MAIN_VARIANT (t) 1181 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type)) 1182 { 1183 /* The element type has been completed since the last time we saw 1184 this array type; update the layout and 'tor flags for any variants 1185 that need it. */ 1186 layout_type (t); 1187 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) 1188 { 1189 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor; 1190 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor; 1191 } 1192 } 1193 1194 return t; 1195 } 1196 1197 /* Return an ARRAY_TYPE with element type ELT and length N. */ 1198 1199 tree 1200 build_array_of_n_type (tree elt, int n) 1201 { 1202 return build_cplus_array_type (elt, build_index_type (size_int (n - 1))); 1203 } 1204 1205 /* True iff T is an array of unknown bound. */ 1206 1207 bool 1208 array_of_unknown_bound_p (const_tree t) 1209 { 1210 return (TREE_CODE (t) == ARRAY_TYPE 1211 && !TYPE_DOMAIN (t)); 1212 } 1213 1214 /* True iff T is an N3639 array of runtime bound (VLA). These were approved 1215 for C++14 but then removed. This should only be used for N3639 1216 specifically; code wondering more generally if something is a VLA should use 1217 vla_type_p. */ 1218 1219 bool 1220 array_of_runtime_bound_p (tree t) 1221 { 1222 if (!t || TREE_CODE (t) != ARRAY_TYPE) 1223 return false; 1224 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE)) 1225 return false; 1226 tree dom = TYPE_DOMAIN (t); 1227 if (!dom) 1228 return false; 1229 tree max = TYPE_MAX_VALUE (dom); 1230 return (!potential_rvalue_constant_expression (max) 1231 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max))); 1232 } 1233 1234 /* True iff T is a variable length array. */ 1235 1236 bool 1237 vla_type_p (tree t) 1238 { 1239 for (; t && TREE_CODE (t) == ARRAY_TYPE; 1240 t = TREE_TYPE (t)) 1241 if (tree dom = TYPE_DOMAIN (t)) 1242 { 1243 tree max = TYPE_MAX_VALUE (dom); 1244 if (!potential_rvalue_constant_expression (max) 1245 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max))) 1246 return true; 1247 } 1248 return false; 1249 } 1250 1251 1252 /* Return a reference type node of MODE referring to TO_TYPE. If MODE 1253 is VOIDmode the standard pointer mode will be picked. If RVAL is 1254 true, return an rvalue reference type, otherwise return an lvalue 1255 reference type. If a type node exists, reuse it, otherwise create 1256 a new one. */ 1257 tree 1258 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval) 1259 { 1260 tree lvalue_ref, t; 1261 1262 if (to_type == error_mark_node) 1263 return error_mark_node; 1264 1265 if (TYPE_REF_P (to_type)) 1266 { 1267 rval = rval && TYPE_REF_IS_RVALUE (to_type); 1268 to_type = TREE_TYPE (to_type); 1269 } 1270 1271 lvalue_ref = build_reference_type_for_mode (to_type, mode, false); 1272 1273 if (!rval) 1274 return lvalue_ref; 1275 1276 /* This code to create rvalue reference types is based on and tied 1277 to the code creating lvalue reference types in the middle-end 1278 functions build_reference_type_for_mode and build_reference_type. 1279 1280 It works by putting the rvalue reference type nodes after the 1281 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so 1282 they will effectively be ignored by the middle end. */ 1283 1284 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); ) 1285 if (TYPE_REF_IS_RVALUE (t)) 1286 return t; 1287 1288 t = build_distinct_type_copy (lvalue_ref); 1289 1290 TYPE_REF_IS_RVALUE (t) = true; 1291 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref); 1292 TYPE_NEXT_REF_TO (lvalue_ref) = t; 1293 1294 if (TYPE_STRUCTURAL_EQUALITY_P (to_type)) 1295 SET_TYPE_STRUCTURAL_EQUALITY (t); 1296 else if (TYPE_CANONICAL (to_type) != to_type) 1297 TYPE_CANONICAL (t) 1298 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval); 1299 else 1300 TYPE_CANONICAL (t) = t; 1301 1302 layout_type (t); 1303 1304 return t; 1305 1306 } 1307 1308 /* Return a reference type node referring to TO_TYPE. If RVAL is 1309 true, return an rvalue reference type, otherwise return an lvalue 1310 reference type. If a type node exists, reuse it, otherwise create 1311 a new one. */ 1312 tree 1313 cp_build_reference_type (tree to_type, bool rval) 1314 { 1315 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval); 1316 } 1317 1318 /* Returns EXPR cast to rvalue reference type, like std::move. */ 1319 1320 tree 1321 move (tree expr) 1322 { 1323 tree type = TREE_TYPE (expr); 1324 gcc_assert (!TYPE_REF_P (type)); 1325 if (xvalue_p (expr)) 1326 return expr; 1327 type = cp_build_reference_type (type, /*rval*/true); 1328 return build_static_cast (input_location, type, expr, 1329 tf_warning_or_error); 1330 } 1331 1332 /* Used by the C++ front end to build qualified array types. However, 1333 the C version of this function does not properly maintain canonical 1334 types (which are not used in C). */ 1335 tree 1336 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */, 1337 size_t /* orig_qual_indirect */) 1338 { 1339 return cp_build_qualified_type (type, type_quals); 1340 } 1341 1342 1343 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles 1345 arrays correctly. In particular, if TYPE is an array of T's, and 1346 TYPE_QUALS is non-empty, returns an array of qualified T's. 1347 1348 FLAGS determines how to deal with ill-formed qualifications. If 1349 tf_ignore_bad_quals is set, then bad qualifications are dropped 1350 (this is permitted if TYPE was introduced via a typedef or template 1351 type parameter). If bad qualifications are dropped and tf_warning 1352 is set, then a warning is issued for non-const qualifications. If 1353 tf_ignore_bad_quals is not set and tf_error is not set, we 1354 return error_mark_node. Otherwise, we issue an error, and ignore 1355 the qualifications. 1356 1357 Qualification of a reference type is valid when the reference came 1358 via a typedef or template type argument. [dcl.ref] No such 1359 dispensation is provided for qualifying a function type. [dcl.fct] 1360 DR 295 queries this and the proposed resolution brings it into line 1361 with qualifying a reference. We implement the DR. We also behave 1362 in a similar manner for restricting non-pointer types. */ 1363 1364 tree 1365 cp_build_qualified_type_real (tree type, 1366 int type_quals, 1367 tsubst_flags_t complain) 1368 { 1369 tree result; 1370 int bad_quals = TYPE_UNQUALIFIED; 1371 1372 if (type == error_mark_node) 1373 return type; 1374 1375 if (type_quals == cp_type_quals (type)) 1376 return type; 1377 1378 if (TREE_CODE (type) == ARRAY_TYPE) 1379 { 1380 /* In C++, the qualification really applies to the array element 1381 type. Obtain the appropriately qualified element type. */ 1382 tree t; 1383 tree element_type 1384 = cp_build_qualified_type_real (TREE_TYPE (type), 1385 type_quals, 1386 complain); 1387 1388 if (element_type == error_mark_node) 1389 return error_mark_node; 1390 1391 /* See if we already have an identically qualified type. Tests 1392 should be equivalent to those in check_qualified_type. */ 1393 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 1394 if (TREE_TYPE (t) == element_type 1395 && TYPE_NAME (t) == TYPE_NAME (type) 1396 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) 1397 && attribute_list_equal (TYPE_ATTRIBUTES (t), 1398 TYPE_ATTRIBUTES (type))) 1399 break; 1400 1401 if (!t) 1402 { 1403 /* If we already know the dependentness, tell the array type 1404 constructor. This is important for module streaming, as we cannot 1405 dynamically determine that on read in. */ 1406 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type), 1407 TYPE_DEPENDENT_P_VALID (type) 1408 ? int (TYPE_DEPENDENT_P (type)) : -1); 1409 1410 /* Keep the typedef name. */ 1411 if (TYPE_NAME (t) != TYPE_NAME (type)) 1412 { 1413 t = build_variant_type_copy (t); 1414 TYPE_NAME (t) = TYPE_NAME (type); 1415 SET_TYPE_ALIGN (t, TYPE_ALIGN (type)); 1416 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type); 1417 } 1418 } 1419 1420 /* Even if we already had this variant, we update 1421 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case 1422 they changed since the variant was originally created. 1423 1424 This seems hokey; if there is some way to use a previous 1425 variant *without* coming through here, 1426 TYPE_NEEDS_CONSTRUCTING will never be updated. */ 1427 TYPE_NEEDS_CONSTRUCTING (t) 1428 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type)); 1429 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 1430 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type)); 1431 return t; 1432 } 1433 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION) 1434 { 1435 tree t = PACK_EXPANSION_PATTERN (type); 1436 1437 t = cp_build_qualified_type_real (t, type_quals, complain); 1438 return make_pack_expansion (t, complain); 1439 } 1440 1441 /* A reference or method type shall not be cv-qualified. 1442 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295 1443 (in CD1) we always ignore extra cv-quals on functions. */ 1444 1445 /* [dcl.ref/1] Cv-qualified references are ill-formed except when 1446 the cv-qualifiers are introduced through the use of a typedef-name 1447 ([dcl.typedef], [temp.param]) or decltype-specifier 1448 ([dcl.type.decltype]),in which case the cv-qualifiers are 1449 ignored. */ 1450 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE) 1451 && (TYPE_REF_P (type) 1452 || FUNC_OR_METHOD_TYPE_P (type))) 1453 { 1454 if (TYPE_REF_P (type) 1455 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type))) 1456 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); 1457 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); 1458 } 1459 1460 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */ 1461 if (TREE_CODE (type) == FUNCTION_TYPE) 1462 type_quals |= type_memfn_quals (type); 1463 1464 /* A restrict-qualified type must be a pointer (or reference) 1465 to object or incomplete type. */ 1466 if ((type_quals & TYPE_QUAL_RESTRICT) 1467 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 1468 && TREE_CODE (type) != TYPENAME_TYPE 1469 && !INDIRECT_TYPE_P (type)) 1470 { 1471 bad_quals |= TYPE_QUAL_RESTRICT; 1472 type_quals &= ~TYPE_QUAL_RESTRICT; 1473 } 1474 1475 if (bad_quals == TYPE_UNQUALIFIED 1476 || (complain & tf_ignore_bad_quals)) 1477 /*OK*/; 1478 else if (!(complain & tf_error)) 1479 return error_mark_node; 1480 else 1481 { 1482 tree bad_type = build_qualified_type (ptr_type_node, bad_quals); 1483 error ("%qV qualifiers cannot be applied to %qT", 1484 bad_type, type); 1485 } 1486 1487 /* Retrieve (or create) the appropriately qualified variant. */ 1488 result = build_qualified_type (type, type_quals); 1489 1490 return result; 1491 } 1492 1493 /* Return TYPE with const and volatile removed. */ 1494 1495 tree 1496 cv_unqualified (tree type) 1497 { 1498 int quals; 1499 1500 if (type == error_mark_node) 1501 return type; 1502 1503 quals = cp_type_quals (type); 1504 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE); 1505 return cp_build_qualified_type (type, quals); 1506 } 1507 1508 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes 1509 from ATTRIBS that affect type identity, and no others. If any are not 1510 applied, set *remove_attributes to true. */ 1511 1512 static tree 1513 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes) 1514 { 1515 tree first_ident = NULL_TREE; 1516 tree new_attribs = NULL_TREE; 1517 tree *p = &new_attribs; 1518 1519 if (OVERLOAD_TYPE_P (result)) 1520 { 1521 /* On classes and enums all attributes are ingrained. */ 1522 gcc_assert (attribs == TYPE_ATTRIBUTES (result)); 1523 return result; 1524 } 1525 1526 for (tree a = attribs; a; a = TREE_CHAIN (a)) 1527 { 1528 const attribute_spec *as 1529 = lookup_attribute_spec (get_attribute_name (a)); 1530 if (as && as->affects_type_identity) 1531 { 1532 if (!first_ident) 1533 first_ident = a; 1534 else if (first_ident == error_mark_node) 1535 { 1536 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE); 1537 p = &TREE_CHAIN (*p); 1538 } 1539 } 1540 else if (first_ident && first_ident != error_mark_node) 1541 { 1542 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2)) 1543 { 1544 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE); 1545 p = &TREE_CHAIN (*p); 1546 } 1547 first_ident = error_mark_node; 1548 } 1549 } 1550 if (first_ident != error_mark_node) 1551 new_attribs = first_ident; 1552 1553 if (first_ident == attribs) 1554 /* All attributes affected type identity. */; 1555 else 1556 *remove_attributes = true; 1557 1558 return cp_build_type_attribute_variant (result, new_attribs); 1559 } 1560 1561 /* Builds a qualified variant of T that is either not a typedef variant 1562 (the default behavior) or not a typedef variant of a user-facing type 1563 (if FLAGS contains STF_USER_FACING). 1564 1565 E.g. consider the following declarations: 1566 typedef const int ConstInt; 1567 typedef ConstInt* PtrConstInt; 1568 If T is PtrConstInt, this function returns a type representing 1569 const int*. 1570 In other words, if T is a typedef, the function returns the underlying type. 1571 The cv-qualification and attributes of the type returned match the 1572 input type. 1573 They will always be compatible types. 1574 The returned type is built so that all of its subtypes 1575 recursively have their typedefs stripped as well. 1576 1577 This is different from just returning TYPE_CANONICAL (T) 1578 Because of several reasons: 1579 * If T is a type that needs structural equality 1580 its TYPE_CANONICAL (T) will be NULL. 1581 * TYPE_CANONICAL (T) desn't carry type attributes 1582 and loses template parameter names. 1583 1584 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't 1585 affect type identity, and set the referent to true if any were 1586 stripped. */ 1587 1588 tree 1589 strip_typedefs (tree t, bool *remove_attributes /* = NULL */, 1590 unsigned int flags /* = 0 */) 1591 { 1592 tree result = NULL, type = NULL, t0 = NULL; 1593 1594 if (!t || t == error_mark_node) 1595 return t; 1596 1597 if (TREE_CODE (t) == TREE_LIST) 1598 { 1599 bool changed = false; 1600 releasing_vec vec; 1601 tree r = t; 1602 for (; t; t = TREE_CHAIN (t)) 1603 { 1604 gcc_assert (!TREE_PURPOSE (t)); 1605 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes, flags); 1606 if (elt != TREE_VALUE (t)) 1607 changed = true; 1608 vec_safe_push (vec, elt); 1609 } 1610 if (changed) 1611 r = build_tree_list_vec (vec); 1612 return r; 1613 } 1614 1615 gcc_assert (TYPE_P (t)); 1616 1617 if (t == TYPE_CANONICAL (t)) 1618 return t; 1619 1620 if (!(flags & STF_STRIP_DEPENDENT) 1621 && dependent_alias_template_spec_p (t, nt_opaque)) 1622 /* DR 1558: However, if the template-id is dependent, subsequent 1623 template argument substitution still applies to the template-id. */ 1624 return t; 1625 1626 switch (TREE_CODE (t)) 1627 { 1628 case POINTER_TYPE: 1629 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags); 1630 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false); 1631 break; 1632 case REFERENCE_TYPE: 1633 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags); 1634 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t)); 1635 break; 1636 case OFFSET_TYPE: 1637 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags); 1638 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags); 1639 result = build_offset_type (t0, type); 1640 break; 1641 case RECORD_TYPE: 1642 if (TYPE_PTRMEMFUNC_P (t)) 1643 { 1644 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), 1645 remove_attributes, flags); 1646 result = build_ptrmemfunc_type (t0); 1647 } 1648 break; 1649 case ARRAY_TYPE: 1650 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags); 1651 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags); 1652 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t) 1653 || !dependent_type_p (t)); 1654 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t)); 1655 break; 1656 case FUNCTION_TYPE: 1657 case METHOD_TYPE: 1658 { 1659 tree arg_types = NULL, arg_node, arg_node2, arg_type; 1660 bool changed; 1661 1662 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places 1663 around the compiler (e.g. cp_parser_late_parsing_default_args), we 1664 can't expect that re-hashing a function type will find a previous 1665 equivalent type, so try to reuse the input type if nothing has 1666 changed. If the type is itself a variant, that will change. */ 1667 bool is_variant = typedef_variant_p (t); 1668 if (remove_attributes 1669 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t))) 1670 is_variant = true; 1671 1672 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags); 1673 tree canon_spec = (flag_noexcept_type 1674 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t)) 1675 : NULL_TREE); 1676 changed = (type != TREE_TYPE (t) || is_variant 1677 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec); 1678 1679 for (arg_node = TYPE_ARG_TYPES (t); 1680 arg_node; 1681 arg_node = TREE_CHAIN (arg_node)) 1682 { 1683 if (arg_node == void_list_node) 1684 break; 1685 arg_type = strip_typedefs (TREE_VALUE (arg_node), 1686 remove_attributes, flags); 1687 gcc_assert (arg_type); 1688 if (arg_type == TREE_VALUE (arg_node) && !changed) 1689 continue; 1690 1691 if (!changed) 1692 { 1693 changed = true; 1694 for (arg_node2 = TYPE_ARG_TYPES (t); 1695 arg_node2 != arg_node; 1696 arg_node2 = TREE_CHAIN (arg_node2)) 1697 arg_types 1698 = tree_cons (TREE_PURPOSE (arg_node2), 1699 TREE_VALUE (arg_node2), arg_types); 1700 } 1701 1702 arg_types 1703 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types); 1704 } 1705 1706 if (!changed) 1707 return t; 1708 1709 if (arg_types) 1710 arg_types = nreverse (arg_types); 1711 1712 /* A list of parameters not ending with an ellipsis 1713 must end with void_list_node. */ 1714 if (arg_node) 1715 arg_types = chainon (arg_types, void_list_node); 1716 1717 if (TREE_CODE (t) == METHOD_TYPE) 1718 { 1719 tree class_type = TREE_TYPE (TREE_VALUE (arg_types)); 1720 gcc_assert (class_type); 1721 result = 1722 build_method_type_directly (class_type, type, 1723 TREE_CHAIN (arg_types)); 1724 } 1725 else 1726 { 1727 result = build_function_type (type, arg_types); 1728 result = apply_memfn_quals (result, type_memfn_quals (t)); 1729 } 1730 1731 result = build_cp_fntype_variant (result, 1732 type_memfn_rqual (t), canon_spec, 1733 TYPE_HAS_LATE_RETURN_TYPE (t)); 1734 } 1735 break; 1736 case TYPENAME_TYPE: 1737 { 1738 bool changed = false; 1739 tree fullname = TYPENAME_TYPE_FULLNAME (t); 1740 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR 1741 && TREE_OPERAND (fullname, 1)) 1742 { 1743 tree args = TREE_OPERAND (fullname, 1); 1744 tree new_args = copy_node (args); 1745 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i) 1746 { 1747 tree arg = TREE_VEC_ELT (args, i); 1748 tree strip_arg; 1749 if (TYPE_P (arg)) 1750 strip_arg = strip_typedefs (arg, remove_attributes, flags); 1751 else 1752 strip_arg = strip_typedefs_expr (arg, remove_attributes, 1753 flags); 1754 TREE_VEC_ELT (new_args, i) = strip_arg; 1755 if (strip_arg != arg) 1756 changed = true; 1757 } 1758 if (changed) 1759 { 1760 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args) 1761 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args); 1762 fullname 1763 = lookup_template_function (TREE_OPERAND (fullname, 0), 1764 new_args); 1765 } 1766 else 1767 ggc_free (new_args); 1768 } 1769 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags); 1770 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t)) 1771 return t; 1772 tree name = fullname; 1773 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) 1774 name = TREE_OPERAND (fullname, 0); 1775 /* Use build_typename_type rather than make_typename_type because we 1776 don't want to resolve it here, just strip typedefs. */ 1777 result = build_typename_type (ctx, name, fullname, typename_type); 1778 } 1779 break; 1780 case DECLTYPE_TYPE: 1781 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t), 1782 remove_attributes, flags); 1783 if (result == DECLTYPE_TYPE_EXPR (t)) 1784 result = NULL_TREE; 1785 else 1786 result = (finish_decltype_type 1787 (result, 1788 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), 1789 tf_none)); 1790 break; 1791 case UNDERLYING_TYPE: 1792 type = strip_typedefs (UNDERLYING_TYPE_TYPE (t), 1793 remove_attributes, flags); 1794 result = finish_underlying_type (type); 1795 break; 1796 case TYPE_PACK_EXPANSION: 1797 { 1798 tree pat = PACK_EXPANSION_PATTERN (t); 1799 if (TYPE_P (pat)) 1800 { 1801 type = strip_typedefs (pat, remove_attributes, flags); 1802 if (type != pat) 1803 { 1804 result = build_distinct_type_copy (t); 1805 PACK_EXPANSION_PATTERN (result) = type; 1806 } 1807 } 1808 } 1809 break; 1810 default: 1811 break; 1812 } 1813 1814 if (!result) 1815 { 1816 if (typedef_variant_p (t)) 1817 { 1818 if ((flags & STF_USER_VISIBLE) 1819 && !user_facing_original_type_p (t)) 1820 return t; 1821 /* If T is a non-template alias or typedef, we can assume that 1822 instantiating its definition will hit any substitution failure, 1823 so we don't need to retain it here as well. */ 1824 if (!alias_template_specialization_p (t, nt_opaque)) 1825 flags |= STF_STRIP_DEPENDENT; 1826 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)), 1827 remove_attributes, flags); 1828 } 1829 else 1830 result = TYPE_MAIN_VARIANT (t); 1831 } 1832 /*gcc_assert (!typedef_variant_p (result) 1833 || dependent_alias_template_spec_p (result, nt_opaque) 1834 || ((flags & STF_USER_VISIBLE) 1835 && !user_facing_original_type_p (result)));*/ 1836 1837 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t)) 1838 /* If RESULT is complete and T isn't, it's likely the case that T 1839 is a variant of RESULT which hasn't been updated yet. Skip the 1840 attribute handling. */; 1841 else 1842 { 1843 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result) 1844 || TYPE_ALIGN (t) != TYPE_ALIGN (result)) 1845 { 1846 gcc_assert (TYPE_USER_ALIGN (t)); 1847 if (remove_attributes) 1848 *remove_attributes = true; 1849 else 1850 { 1851 if (TYPE_ALIGN (t) == TYPE_ALIGN (result)) 1852 result = build_variant_type_copy (result); 1853 else 1854 result = build_aligned_type (result, TYPE_ALIGN (t)); 1855 TYPE_USER_ALIGN (result) = true; 1856 } 1857 } 1858 1859 if (TYPE_ATTRIBUTES (t)) 1860 { 1861 if (remove_attributes) 1862 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t), 1863 remove_attributes); 1864 else 1865 result = cp_build_type_attribute_variant (result, 1866 TYPE_ATTRIBUTES (t)); 1867 } 1868 } 1869 1870 return cp_build_qualified_type (result, cp_type_quals (t)); 1871 } 1872 1873 /* Like strip_typedefs above, but works on expressions, so that in 1874 1875 template<class T> struct A 1876 { 1877 typedef T TT; 1878 B<sizeof(TT)> b; 1879 }; 1880 1881 sizeof(TT) is replaced by sizeof(T). */ 1882 1883 tree 1884 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags) 1885 { 1886 unsigned i,n; 1887 tree r, type, *ops; 1888 enum tree_code code; 1889 1890 if (t == NULL_TREE || t == error_mark_node) 1891 return t; 1892 1893 STRIP_ANY_LOCATION_WRAPPER (t); 1894 1895 if (DECL_P (t) || CONSTANT_CLASS_P (t)) 1896 return t; 1897 1898 /* Some expressions have type operands, so let's handle types here rather 1899 than check TYPE_P in multiple places below. */ 1900 if (TYPE_P (t)) 1901 return strip_typedefs (t, remove_attributes, flags); 1902 1903 code = TREE_CODE (t); 1904 switch (code) 1905 { 1906 case IDENTIFIER_NODE: 1907 case TEMPLATE_PARM_INDEX: 1908 case OVERLOAD: 1909 case BASELINK: 1910 case ARGUMENT_PACK_SELECT: 1911 return t; 1912 1913 case TRAIT_EXPR: 1914 { 1915 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), 1916 remove_attributes, flags); 1917 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), 1918 remove_attributes, flags); 1919 if (type1 == TRAIT_EXPR_TYPE1 (t) 1920 && type2 == TRAIT_EXPR_TYPE2 (t)) 1921 return t; 1922 r = copy_node (t); 1923 TRAIT_EXPR_TYPE1 (r) = type1; 1924 TRAIT_EXPR_TYPE2 (r) = type2; 1925 return r; 1926 } 1927 1928 case TREE_LIST: 1929 { 1930 releasing_vec vec; 1931 bool changed = false; 1932 tree it; 1933 for (it = t; it; it = TREE_CHAIN (it)) 1934 { 1935 tree val = strip_typedefs_expr (TREE_VALUE (it), 1936 remove_attributes, flags); 1937 vec_safe_push (vec, val); 1938 if (val != TREE_VALUE (it)) 1939 changed = true; 1940 gcc_assert (TREE_PURPOSE (it) == NULL_TREE); 1941 } 1942 if (changed) 1943 { 1944 r = NULL_TREE; 1945 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it) 1946 r = tree_cons (NULL_TREE, it, r); 1947 } 1948 else 1949 r = t; 1950 return r; 1951 } 1952 1953 case TREE_VEC: 1954 { 1955 bool changed = false; 1956 releasing_vec vec; 1957 n = TREE_VEC_LENGTH (t); 1958 vec_safe_reserve (vec, n); 1959 for (i = 0; i < n; ++i) 1960 { 1961 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i), 1962 remove_attributes, flags); 1963 vec->quick_push (op); 1964 if (op != TREE_VEC_ELT (t, i)) 1965 changed = true; 1966 } 1967 if (changed) 1968 { 1969 r = copy_node (t); 1970 for (i = 0; i < n; ++i) 1971 TREE_VEC_ELT (r, i) = (*vec)[i]; 1972 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r) 1973 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t); 1974 } 1975 else 1976 r = t; 1977 return r; 1978 } 1979 1980 case CONSTRUCTOR: 1981 { 1982 bool changed = false; 1983 vec<constructor_elt, va_gc> *vec 1984 = vec_safe_copy (CONSTRUCTOR_ELTS (t)); 1985 n = CONSTRUCTOR_NELTS (t); 1986 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags); 1987 for (i = 0; i < n; ++i) 1988 { 1989 constructor_elt *e = &(*vec)[i]; 1990 tree op = strip_typedefs_expr (e->value, remove_attributes, flags); 1991 if (op != e->value) 1992 { 1993 changed = true; 1994 e->value = op; 1995 } 1996 gcc_checking_assert 1997 (e->index == strip_typedefs_expr (e->index, remove_attributes, 1998 flags)); 1999 } 2000 2001 if (!changed && type == TREE_TYPE (t)) 2002 { 2003 vec_free (vec); 2004 return t; 2005 } 2006 else 2007 { 2008 r = copy_node (t); 2009 TREE_TYPE (r) = type; 2010 CONSTRUCTOR_ELTS (r) = vec; 2011 return r; 2012 } 2013 } 2014 2015 case LAMBDA_EXPR: 2016 case STMT_EXPR: 2017 return t; 2018 2019 default: 2020 break; 2021 } 2022 2023 gcc_assert (EXPR_P (t)); 2024 2025 n = cp_tree_operand_length (t); 2026 ops = XALLOCAVEC (tree, n); 2027 type = TREE_TYPE (t); 2028 2029 switch (code) 2030 { 2031 CASE_CONVERT: 2032 case IMPLICIT_CONV_EXPR: 2033 case DYNAMIC_CAST_EXPR: 2034 case STATIC_CAST_EXPR: 2035 case CONST_CAST_EXPR: 2036 case REINTERPRET_CAST_EXPR: 2037 case CAST_EXPR: 2038 case NEW_EXPR: 2039 type = strip_typedefs (type, remove_attributes, flags); 2040 /* fallthrough */ 2041 2042 default: 2043 for (i = 0; i < n; ++i) 2044 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), 2045 remove_attributes, flags); 2046 break; 2047 } 2048 2049 /* If nothing changed, return t. */ 2050 for (i = 0; i < n; ++i) 2051 if (ops[i] != TREE_OPERAND (t, i)) 2052 break; 2053 if (i == n && type == TREE_TYPE (t)) 2054 return t; 2055 2056 r = copy_node (t); 2057 TREE_TYPE (r) = type; 2058 for (i = 0; i < n; ++i) 2059 TREE_OPERAND (r, i) = ops[i]; 2060 return r; 2061 } 2062 2063 /* Makes a copy of BINFO and TYPE, which is to be inherited into a 2064 graph dominated by T. If BINFO is NULL, TYPE is a dependent base, 2065 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy. 2066 VIRT indicates whether TYPE is inherited virtually or not. 2067 IGO_PREV points at the previous binfo of the inheritance graph 2068 order chain. The newly copied binfo's TREE_CHAIN forms this 2069 ordering. 2070 2071 The CLASSTYPE_VBASECLASSES vector of T is constructed in the 2072 correct order. That is in the order the bases themselves should be 2073 constructed in. 2074 2075 The BINFO_INHERITANCE of a virtual base class points to the binfo 2076 of the most derived type. ??? We could probably change this so that 2077 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence 2078 remove a field. They currently can only differ for primary virtual 2079 virtual bases. */ 2080 2081 tree 2082 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt) 2083 { 2084 tree new_binfo; 2085 2086 if (virt) 2087 { 2088 /* See if we've already made this virtual base. */ 2089 new_binfo = binfo_for_vbase (type, t); 2090 if (new_binfo) 2091 return new_binfo; 2092 } 2093 2094 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0); 2095 BINFO_TYPE (new_binfo) = type; 2096 2097 /* Chain it into the inheritance graph. */ 2098 TREE_CHAIN (*igo_prev) = new_binfo; 2099 *igo_prev = new_binfo; 2100 2101 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo)) 2102 { 2103 int ix; 2104 tree base_binfo; 2105 2106 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type)); 2107 2108 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo); 2109 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo); 2110 2111 /* We do not need to copy the accesses, as they are read only. */ 2112 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo); 2113 2114 /* Recursively copy base binfos of BINFO. */ 2115 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 2116 { 2117 tree new_base_binfo; 2118 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo), 2119 t, igo_prev, 2120 BINFO_VIRTUAL_P (base_binfo)); 2121 2122 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo)) 2123 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo; 2124 BINFO_BASE_APPEND (new_binfo, new_base_binfo); 2125 } 2126 } 2127 else 2128 BINFO_DEPENDENT_BASE_P (new_binfo) = 1; 2129 2130 if (virt) 2131 { 2132 /* Push it onto the list after any virtual bases it contains 2133 will have been pushed. */ 2134 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo); 2135 BINFO_VIRTUAL_P (new_binfo) = 1; 2136 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t); 2137 } 2138 2139 return new_binfo; 2140 } 2141 2142 /* Hashing of lists so that we don't make duplicates. 2144 The entry point is `list_hash_canon'. */ 2145 2146 struct list_proxy 2147 { 2148 tree purpose; 2149 tree value; 2150 tree chain; 2151 }; 2152 2153 struct list_hasher : ggc_ptr_hash<tree_node> 2154 { 2155 typedef list_proxy *compare_type; 2156 2157 static hashval_t hash (tree); 2158 static bool equal (tree, list_proxy *); 2159 }; 2160 2161 /* Now here is the hash table. When recording a list, it is added 2162 to the slot whose index is the hash code mod the table size. 2163 Note that the hash table is used for several kinds of lists. 2164 While all these live in the same table, they are completely independent, 2165 and the hash code is computed differently for each of these. */ 2166 2167 static GTY (()) hash_table<list_hasher> *list_hash_table; 2168 2169 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy 2170 for a node we are thinking about adding). */ 2171 2172 bool 2173 list_hasher::equal (tree t, list_proxy *proxy) 2174 { 2175 return (TREE_VALUE (t) == proxy->value 2176 && TREE_PURPOSE (t) == proxy->purpose 2177 && TREE_CHAIN (t) == proxy->chain); 2178 } 2179 2180 /* Compute a hash code for a list (chain of TREE_LIST nodes 2181 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the 2182 TREE_COMMON slots), by adding the hash codes of the individual entries. */ 2183 2184 static hashval_t 2185 list_hash_pieces (tree purpose, tree value, tree chain) 2186 { 2187 hashval_t hashcode = 0; 2188 2189 if (chain) 2190 hashcode += TREE_HASH (chain); 2191 2192 if (value) 2193 hashcode += TREE_HASH (value); 2194 else 2195 hashcode += 1007; 2196 if (purpose) 2197 hashcode += TREE_HASH (purpose); 2198 else 2199 hashcode += 1009; 2200 return hashcode; 2201 } 2202 2203 /* Hash an already existing TREE_LIST. */ 2204 2205 hashval_t 2206 list_hasher::hash (tree t) 2207 { 2208 return list_hash_pieces (TREE_PURPOSE (t), 2209 TREE_VALUE (t), 2210 TREE_CHAIN (t)); 2211 } 2212 2213 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical 2214 object for an identical list if one already exists. Otherwise, build a 2215 new one, and record it as the canonical object. */ 2216 2217 tree 2218 hash_tree_cons (tree purpose, tree value, tree chain) 2219 { 2220 int hashcode = 0; 2221 tree *slot; 2222 struct list_proxy proxy; 2223 2224 /* Hash the list node. */ 2225 hashcode = list_hash_pieces (purpose, value, chain); 2226 /* Create a proxy for the TREE_LIST we would like to create. We 2227 don't actually create it so as to avoid creating garbage. */ 2228 proxy.purpose = purpose; 2229 proxy.value = value; 2230 proxy.chain = chain; 2231 /* See if it is already in the table. */ 2232 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT); 2233 /* If not, create a new node. */ 2234 if (!*slot) 2235 *slot = tree_cons (purpose, value, chain); 2236 return (tree) *slot; 2237 } 2238 2239 /* Constructor for hashed lists. */ 2240 2241 tree 2242 hash_tree_chain (tree value, tree chain) 2243 { 2244 return hash_tree_cons (NULL_TREE, value, chain); 2245 } 2246 2247 void 2249 debug_binfo (tree elem) 2250 { 2251 HOST_WIDE_INT n; 2252 tree virtuals; 2253 2254 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC 2255 "\nvtable type:\n", 2256 TYPE_NAME_STRING (BINFO_TYPE (elem)), 2257 TREE_INT_CST_LOW (BINFO_OFFSET (elem))); 2258 debug_tree (BINFO_TYPE (elem)); 2259 if (BINFO_VTABLE (elem)) 2260 fprintf (stderr, "vtable decl \"%s\"\n", 2261 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem)))); 2262 else 2263 fprintf (stderr, "no vtable decl yet\n"); 2264 fprintf (stderr, "virtuals:\n"); 2265 virtuals = BINFO_VIRTUALS (elem); 2266 n = 0; 2267 2268 while (virtuals) 2269 { 2270 tree fndecl = TREE_VALUE (virtuals); 2271 fprintf (stderr, "%s [%ld =? %ld]\n", 2272 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)), 2273 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl))); 2274 ++n; 2275 virtuals = TREE_CHAIN (virtuals); 2276 } 2277 } 2278 2279 /* Build a representation for the qualified name SCOPE::NAME. TYPE is 2280 the type of the result expression, if known, or NULL_TREE if the 2281 resulting expression is type-dependent. If TEMPLATE_P is true, 2282 NAME is known to be a template because the user explicitly used the 2283 "template" keyword after the "::". 2284 2285 All SCOPE_REFs should be built by use of this function. */ 2286 2287 tree 2288 build_qualified_name (tree type, tree scope, tree name, bool template_p) 2289 { 2290 tree t; 2291 if (type == error_mark_node 2292 || scope == error_mark_node 2293 || name == error_mark_node) 2294 return error_mark_node; 2295 gcc_assert (TREE_CODE (name) != SCOPE_REF); 2296 t = build2 (SCOPE_REF, type, scope, name); 2297 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p; 2298 PTRMEM_OK_P (t) = true; 2299 if (type) 2300 t = convert_from_reference (t); 2301 return t; 2302 } 2303 2304 /* Like check_qualified_type, but also check ref-qualifier, exception 2305 specification, and whether the return type was specified after the 2306 parameters. */ 2307 2308 static bool 2309 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals, 2310 cp_ref_qualifier rqual, tree raises, bool late) 2311 { 2312 return (TYPE_QUALS (cand) == type_quals 2313 && check_base_type (cand, base) 2314 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand), 2315 ce_exact) 2316 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late 2317 && type_memfn_rqual (cand) == rqual); 2318 } 2319 2320 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */ 2321 2322 tree 2323 build_ref_qualified_type (tree type, cp_ref_qualifier rqual) 2324 { 2325 tree raises = TYPE_RAISES_EXCEPTIONS (type); 2326 bool late = TYPE_HAS_LATE_RETURN_TYPE (type); 2327 return build_cp_fntype_variant (type, rqual, raises, late); 2328 } 2329 2330 tree 2331 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL) 2332 { 2333 /* Stored in an unsigned short, but we're limited to the number of 2334 modules anyway. */ 2335 gcc_checking_assert (clusters <= (unsigned short)(~0)); 2336 size_t length = (offsetof (tree_binding_vec, vec) 2337 + clusters * sizeof (binding_cluster)); 2338 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT); 2339 TREE_SET_CODE (vec, BINDING_VECTOR); 2340 BINDING_VECTOR_NAME (vec) = name; 2341 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters; 2342 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0; 2343 2344 return vec; 2345 } 2346 2347 /* Make a raw overload node containing FN. */ 2348 2349 tree 2350 ovl_make (tree fn, tree next) 2351 { 2352 tree result = make_node (OVERLOAD); 2353 2354 if (TREE_CODE (fn) == OVERLOAD) 2355 OVL_NESTED_P (result) = true; 2356 2357 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL 2358 ? unknown_type_node : TREE_TYPE (fn)); 2359 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next)) 2360 OVL_DEDUP_P (result) = true; 2361 OVL_FUNCTION (result) = fn; 2362 OVL_CHAIN (result) = next; 2363 return result; 2364 } 2365 2366 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is > 2367 zero if this is a using-decl. It is > 1 if we're exporting the 2368 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl 2369 cannot be both using and hidden.) We keep the hidden decls first, 2370 but remaining ones are unordered. */ 2371 2372 tree 2373 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden) 2374 { 2375 tree result = maybe_ovl; 2376 tree insert_after = NULL_TREE; 2377 2378 /* Skip hidden. */ 2379 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD 2380 && OVL_HIDDEN_P (maybe_ovl); 2381 maybe_ovl = OVL_CHAIN (maybe_ovl)) 2382 { 2383 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl)); 2384 insert_after = maybe_ovl; 2385 } 2386 2387 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL) 2388 { 2389 maybe_ovl = ovl_make (fn, maybe_ovl); 2390 2391 if (using_or_hidden < 0) 2392 OVL_HIDDEN_P (maybe_ovl) = true; 2393 if (using_or_hidden > 0) 2394 { 2395 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true; 2396 if (using_or_hidden > 1) 2397 OVL_EXPORT_P (maybe_ovl) = true; 2398 } 2399 } 2400 else 2401 maybe_ovl = fn; 2402 2403 if (insert_after) 2404 { 2405 OVL_CHAIN (insert_after) = maybe_ovl; 2406 TREE_TYPE (insert_after) = unknown_type_node; 2407 } 2408 else 2409 result = maybe_ovl; 2410 2411 return result; 2412 } 2413 2414 /* Skip any hidden names at the beginning of OVL. */ 2415 2416 tree 2417 ovl_skip_hidden (tree ovl) 2418 { 2419 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl)) 2420 ovl = OVL_CHAIN (ovl); 2421 2422 return ovl; 2423 } 2424 2425 /* NODE is an OVL_HIDDEN_P node that is now revealed. */ 2426 2427 tree 2428 ovl_iterator::reveal_node (tree overload, tree node) 2429 { 2430 /* We cannot have returned NODE as part of a lookup overload, so we 2431 don't have to worry about preserving that. */ 2432 2433 OVL_HIDDEN_P (node) = false; 2434 if (tree chain = OVL_CHAIN (node)) 2435 if (TREE_CODE (chain) == OVERLOAD) 2436 { 2437 if (OVL_HIDDEN_P (chain)) 2438 { 2439 /* The node needs moving, and the simplest way is to remove it 2440 and reinsert. */ 2441 overload = remove_node (overload, node); 2442 overload = ovl_insert (OVL_FUNCTION (node), overload); 2443 } 2444 else if (OVL_DEDUP_P (chain)) 2445 OVL_DEDUP_P (node) = true; 2446 } 2447 return overload; 2448 } 2449 2450 /* NODE is on the overloads of OVL. Remove it. 2451 The removed node is unaltered and may continue to be iterated 2452 from (i.e. it is safe to remove a node from an overload one is 2453 currently iterating over). */ 2454 2455 tree 2456 ovl_iterator::remove_node (tree overload, tree node) 2457 { 2458 tree *slot = &overload; 2459 while (*slot != node) 2460 { 2461 tree probe = *slot; 2462 gcc_checking_assert (!OVL_LOOKUP_P (probe)); 2463 2464 slot = &OVL_CHAIN (probe); 2465 } 2466 2467 /* Stitch out NODE. We don't have to worry about now making a 2468 singleton overload (and consequently maybe setting its type), 2469 because all uses of this function will be followed by inserting a 2470 new node that must follow the place we've cut this out from. */ 2471 if (TREE_CODE (node) != OVERLOAD) 2472 /* Cloned inherited ctors don't mark themselves as via_using. */ 2473 *slot = NULL_TREE; 2474 else 2475 *slot = OVL_CHAIN (node); 2476 2477 return overload; 2478 } 2479 2480 /* Mark or unmark a lookup set. */ 2481 2482 void 2483 lookup_mark (tree ovl, bool val) 2484 { 2485 for (lkp_iterator iter (ovl); iter; ++iter) 2486 { 2487 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val); 2488 LOOKUP_SEEN_P (*iter) = val; 2489 } 2490 } 2491 2492 /* Add a set of new FNS into a lookup. */ 2493 2494 tree 2495 lookup_add (tree fns, tree lookup) 2496 { 2497 if (fns == error_mark_node || lookup == error_mark_node) 2498 return error_mark_node; 2499 2500 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL) 2501 { 2502 lookup = ovl_make (fns, lookup); 2503 OVL_LOOKUP_P (lookup) = true; 2504 } 2505 else 2506 lookup = fns; 2507 2508 return lookup; 2509 } 2510 2511 /* FNS is a new overload set, add them to LOOKUP, if they are not 2512 already present there. */ 2513 2514 tree 2515 lookup_maybe_add (tree fns, tree lookup, bool deduping) 2516 { 2517 if (deduping) 2518 for (tree next, probe = fns; probe; probe = next) 2519 { 2520 tree fn = probe; 2521 next = NULL_TREE; 2522 2523 if (TREE_CODE (probe) == OVERLOAD) 2524 { 2525 fn = OVL_FUNCTION (probe); 2526 next = OVL_CHAIN (probe); 2527 } 2528 2529 if (!LOOKUP_SEEN_P (fn)) 2530 LOOKUP_SEEN_P (fn) = true; 2531 else 2532 { 2533 /* This function was already seen. Insert all the 2534 predecessors onto the lookup. */ 2535 for (; fns != probe; fns = OVL_CHAIN (fns)) 2536 { 2537 lookup = lookup_add (OVL_FUNCTION (fns), lookup); 2538 /* Propagate OVL_USING, but OVL_HIDDEN & 2539 OVL_DEDUP_P don't matter. */ 2540 if (OVL_USING_P (fns)) 2541 OVL_USING_P (lookup) = true; 2542 } 2543 2544 /* And now skip this function. */ 2545 fns = next; 2546 } 2547 } 2548 2549 if (fns) 2550 /* We ended in a set of new functions. Add them all in one go. */ 2551 lookup = lookup_add (fns, lookup); 2552 2553 return lookup; 2554 } 2555 2556 /* Returns nonzero if X is an expression for a (possibly overloaded) 2557 function. If "f" is a function or function template, "f", "c->f", 2558 "c.f", "C::f", and "f<int>" will all be considered possibly 2559 overloaded functions. Returns 2 if the function is actually 2560 overloaded, i.e., if it is impossible to know the type of the 2561 function without performing overload resolution. */ 2562 2563 int 2564 is_overloaded_fn (tree x) 2565 { 2566 STRIP_ANY_LOCATION_WRAPPER (x); 2567 2568 /* A baselink is also considered an overloaded function. */ 2569 if (TREE_CODE (x) == OFFSET_REF 2570 || TREE_CODE (x) == COMPONENT_REF) 2571 x = TREE_OPERAND (x, 1); 2572 x = MAYBE_BASELINK_FUNCTIONS (x); 2573 if (TREE_CODE (x) == TEMPLATE_ID_EXPR) 2574 x = TREE_OPERAND (x, 0); 2575 2576 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x)) 2577 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x))) 2578 return 2; 2579 2580 return OVL_P (x); 2581 } 2582 2583 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name 2584 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return 2585 NULL_TREE. */ 2586 2587 tree 2588 dependent_name (tree x) 2589 { 2590 /* FIXME a dependent name must be unqualified, but this function doesn't 2591 distinguish between qualified and unqualified identifiers. */ 2592 if (identifier_p (x)) 2593 return x; 2594 if (TREE_CODE (x) == TEMPLATE_ID_EXPR) 2595 x = TREE_OPERAND (x, 0); 2596 if (OVL_P (x)) 2597 return OVL_NAME (x); 2598 return NULL_TREE; 2599 } 2600 2601 /* Like dependent_name, but instead takes a CALL_EXPR and also checks 2602 its dependence. */ 2603 2604 tree 2605 call_expr_dependent_name (tree x) 2606 { 2607 if (TREE_TYPE (x) != NULL_TREE) 2608 /* X isn't dependent, so its callee isn't a dependent name. */ 2609 return NULL_TREE; 2610 return dependent_name (CALL_EXPR_FN (x)); 2611 } 2612 2613 /* Returns true iff X is an expression for an overloaded function 2614 whose type cannot be known without performing overload 2615 resolution. */ 2616 2617 bool 2618 really_overloaded_fn (tree x) 2619 { 2620 return is_overloaded_fn (x) == 2; 2621 } 2622 2623 /* Get the overload set FROM refers to. Returns NULL if it's not an 2624 overload set. */ 2625 2626 tree 2627 maybe_get_fns (tree from) 2628 { 2629 STRIP_ANY_LOCATION_WRAPPER (from); 2630 2631 /* A baselink is also considered an overloaded function. */ 2632 if (TREE_CODE (from) == OFFSET_REF 2633 || TREE_CODE (from) == COMPONENT_REF) 2634 from = TREE_OPERAND (from, 1); 2635 if (BASELINK_P (from)) 2636 from = BASELINK_FUNCTIONS (from); 2637 if (TREE_CODE (from) == TEMPLATE_ID_EXPR) 2638 from = TREE_OPERAND (from, 0); 2639 2640 if (OVL_P (from)) 2641 return from; 2642 2643 return NULL; 2644 } 2645 2646 /* FROM refers to an overload set. Return that set (or die). */ 2647 2648 tree 2649 get_fns (tree from) 2650 { 2651 tree res = maybe_get_fns (from); 2652 2653 gcc_assert (res); 2654 return res; 2655 } 2656 2657 /* Return the first function of the overload set FROM refers to. */ 2658 2659 tree 2660 get_first_fn (tree from) 2661 { 2662 return OVL_FIRST (get_fns (from)); 2663 } 2664 2665 /* Return the scope where the overloaded functions OVL were found. */ 2666 2667 tree 2668 ovl_scope (tree ovl) 2669 { 2670 if (TREE_CODE (ovl) == OFFSET_REF 2671 || TREE_CODE (ovl) == COMPONENT_REF) 2672 ovl = TREE_OPERAND (ovl, 1); 2673 if (TREE_CODE (ovl) == BASELINK) 2674 return BINFO_TYPE (BASELINK_BINFO (ovl)); 2675 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR) 2676 ovl = TREE_OPERAND (ovl, 0); 2677 /* Skip using-declarations. */ 2678 lkp_iterator iter (ovl); 2679 do 2680 ovl = *iter; 2681 while (iter.using_p () && ++iter); 2682 2683 return CP_DECL_CONTEXT (ovl); 2684 } 2685 2686 #define PRINT_RING_SIZE 4 2688 2689 static const char * 2690 cxx_printable_name_internal (tree decl, int v, bool translate) 2691 { 2692 static unsigned int uid_ring[PRINT_RING_SIZE]; 2693 static char *print_ring[PRINT_RING_SIZE]; 2694 static bool trans_ring[PRINT_RING_SIZE]; 2695 static int ring_counter; 2696 int i; 2697 2698 /* Only cache functions. */ 2699 if (v < 2 2700 || TREE_CODE (decl) != FUNCTION_DECL 2701 || DECL_LANG_SPECIFIC (decl) == 0) 2702 return lang_decl_name (decl, v, translate); 2703 2704 /* See if this print name is lying around. */ 2705 for (i = 0; i < PRINT_RING_SIZE; i++) 2706 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i]) 2707 /* yes, so return it. */ 2708 return print_ring[i]; 2709 2710 if (++ring_counter == PRINT_RING_SIZE) 2711 ring_counter = 0; 2712 2713 if (current_function_decl != NULL_TREE) 2714 { 2715 /* There may be both translated and untranslated versions of the 2716 name cached. */ 2717 for (i = 0; i < 2; i++) 2718 { 2719 if (uid_ring[ring_counter] == DECL_UID (current_function_decl)) 2720 ring_counter += 1; 2721 if (ring_counter == PRINT_RING_SIZE) 2722 ring_counter = 0; 2723 } 2724 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl)); 2725 } 2726 2727 free (print_ring[ring_counter]); 2728 2729 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate)); 2730 uid_ring[ring_counter] = DECL_UID (decl); 2731 trans_ring[ring_counter] = translate; 2732 return print_ring[ring_counter]; 2733 } 2734 2735 const char * 2736 cxx_printable_name (tree decl, int v) 2737 { 2738 return cxx_printable_name_internal (decl, v, false); 2739 } 2740 2741 const char * 2742 cxx_printable_name_translate (tree decl, int v) 2743 { 2744 return cxx_printable_name_internal (decl, v, true); 2745 } 2746 2747 /* Return the canonical version of exception-specification RAISES for a C++17 2749 function type, for use in type comparison and building TYPE_CANONICAL. */ 2750 2751 tree 2752 canonical_eh_spec (tree raises) 2753 { 2754 if (raises == NULL_TREE) 2755 return raises; 2756 else if (DEFERRED_NOEXCEPT_SPEC_P (raises) 2757 || UNPARSED_NOEXCEPT_SPEC_P (raises) 2758 || uses_template_parms (raises) 2759 || uses_template_parms (TREE_PURPOSE (raises))) 2760 /* Keep a dependent or deferred exception specification. */ 2761 return raises; 2762 else if (nothrow_spec_p (raises)) 2763 /* throw() -> noexcept. */ 2764 return noexcept_true_spec; 2765 else 2766 /* For C++17 type matching, anything else -> nothing. */ 2767 return NULL_TREE; 2768 } 2769 2770 tree 2771 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual, 2772 tree raises, bool late) 2773 { 2774 cp_cv_quals type_quals = TYPE_QUALS (type); 2775 2776 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late)) 2777 return type; 2778 2779 tree v = TYPE_MAIN_VARIANT (type); 2780 for (; v; v = TYPE_NEXT_VARIANT (v)) 2781 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late)) 2782 return v; 2783 2784 /* Need to build a new variant. */ 2785 v = build_variant_type_copy (type); 2786 if (!TYPE_DEPENDENT_P (v)) 2787 /* We no longer know that it's not type-dependent. */ 2788 TYPE_DEPENDENT_P_VALID (v) = false; 2789 TYPE_RAISES_EXCEPTIONS (v) = raises; 2790 TYPE_HAS_LATE_RETURN_TYPE (v) = late; 2791 switch (rqual) 2792 { 2793 case REF_QUAL_RVALUE: 2794 FUNCTION_RVALUE_QUALIFIED (v) = 1; 2795 FUNCTION_REF_QUALIFIED (v) = 1; 2796 break; 2797 case REF_QUAL_LVALUE: 2798 FUNCTION_RVALUE_QUALIFIED (v) = 0; 2799 FUNCTION_REF_QUALIFIED (v) = 1; 2800 break; 2801 default: 2802 FUNCTION_REF_QUALIFIED (v) = 0; 2803 break; 2804 } 2805 2806 /* Canonicalize the exception specification. */ 2807 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE; 2808 2809 if (TYPE_STRUCTURAL_EQUALITY_P (type)) 2810 /* Propagate structural equality. */ 2811 SET_TYPE_STRUCTURAL_EQUALITY (v); 2812 else if (TYPE_CANONICAL (type) != type || cr != raises || late) 2813 /* Build the underlying canonical type, since it is different 2814 from TYPE. */ 2815 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type), 2816 rqual, cr, false); 2817 else 2818 /* T is its own canonical type. */ 2819 TYPE_CANONICAL (v) = v; 2820 2821 return v; 2822 } 2823 2824 /* TYPE is a function or method type with a deferred exception 2825 specification that has been parsed to RAISES. Fixup all the type 2826 variants that are affected in place. Via decltype &| noexcept 2827 tricks, the unparsed spec could have escaped into the type system. 2828 The general case is hard to fixup canonical types for. */ 2829 2830 void 2831 fixup_deferred_exception_variants (tree type, tree raises) 2832 { 2833 tree original = TYPE_RAISES_EXCEPTIONS (type); 2834 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE; 2835 2836 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original)); 2837 2838 /* Though sucky, this walk will process the canonical variants 2839 first. */ 2840 tree prev = NULL_TREE; 2841 for (tree variant = TYPE_MAIN_VARIANT (type); 2842 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant)) 2843 if (TYPE_RAISES_EXCEPTIONS (variant) == original) 2844 { 2845 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type)); 2846 2847 if (!TYPE_STRUCTURAL_EQUALITY_P (variant)) 2848 { 2849 cp_cv_quals var_quals = TYPE_QUALS (variant); 2850 cp_ref_qualifier rqual = type_memfn_rqual (variant); 2851 2852 /* If VARIANT would become a dup (cp_check_qualified_type-wise) 2853 of an existing variant in the variant list of TYPE after its 2854 exception specification has been parsed, elide it. Otherwise, 2855 build_cp_fntype_variant could use it, leading to "canonical 2856 types differ for identical types." */ 2857 tree v = TYPE_MAIN_VARIANT (type); 2858 for (; v; v = TYPE_NEXT_VARIANT (v)) 2859 if (cp_check_qualified_type (v, variant, var_quals, 2860 rqual, cr, false)) 2861 { 2862 /* The main variant will not match V, so PREV will never 2863 be null. */ 2864 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant); 2865 break; 2866 } 2867 TYPE_RAISES_EXCEPTIONS (variant) = raises; 2868 2869 if (!v) 2870 v = build_cp_fntype_variant (TYPE_CANONICAL (variant), 2871 rqual, cr, false); 2872 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v); 2873 } 2874 else 2875 TYPE_RAISES_EXCEPTIONS (variant) = raises; 2876 2877 if (!TYPE_DEPENDENT_P (variant)) 2878 /* We no longer know that it's not type-dependent. */ 2879 TYPE_DEPENDENT_P_VALID (variant) = false; 2880 } 2881 } 2882 2883 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions 2884 listed in RAISES. */ 2885 2886 tree 2887 build_exception_variant (tree type, tree raises) 2888 { 2889 cp_ref_qualifier rqual = type_memfn_rqual (type); 2890 bool late = TYPE_HAS_LATE_RETURN_TYPE (type); 2891 return build_cp_fntype_variant (type, rqual, raises, late); 2892 } 2893 2894 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new 2895 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template 2896 arguments. */ 2897 2898 tree 2899 bind_template_template_parm (tree t, tree newargs) 2900 { 2901 tree decl = TYPE_NAME (t); 2902 tree t2; 2903 2904 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM); 2905 decl = build_decl (input_location, 2906 TYPE_DECL, DECL_NAME (decl), NULL_TREE); 2907 SET_DECL_TEMPLATE_PARM_P (decl); 2908 2909 /* These nodes have to be created to reflect new TYPE_DECL and template 2910 arguments. */ 2911 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t)); 2912 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl; 2913 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2) 2914 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs); 2915 2916 TREE_TYPE (decl) = t2; 2917 TYPE_NAME (t2) = decl; 2918 TYPE_STUB_DECL (t2) = decl; 2919 TYPE_SIZE (t2) = 0; 2920 SET_TYPE_STRUCTURAL_EQUALITY (t2); 2921 2922 return t2; 2923 } 2924 2925 /* Called from count_trees via walk_tree. */ 2926 2927 static tree 2928 count_trees_r (tree *tp, int *walk_subtrees, void *data) 2929 { 2930 ++*((int *) data); 2931 2932 if (TYPE_P (*tp)) 2933 *walk_subtrees = 0; 2934 2935 return NULL_TREE; 2936 } 2937 2938 /* Debugging function for measuring the rough complexity of a tree 2939 representation. */ 2940 2941 int 2942 count_trees (tree t) 2943 { 2944 int n_trees = 0; 2945 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees); 2946 return n_trees; 2947 } 2948 2949 /* Called from verify_stmt_tree via walk_tree. */ 2950 2951 static tree 2952 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data) 2953 { 2954 tree t = *tp; 2955 hash_table<nofree_ptr_hash <tree_node> > *statements 2956 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data); 2957 tree_node **slot; 2958 2959 if (!STATEMENT_CODE_P (TREE_CODE (t))) 2960 return NULL_TREE; 2961 2962 /* If this statement is already present in the hash table, then 2963 there is a circularity in the statement tree. */ 2964 gcc_assert (!statements->find (t)); 2965 2966 slot = statements->find_slot (t, INSERT); 2967 *slot = t; 2968 2969 return NULL_TREE; 2970 } 2971 2972 /* Debugging function to check that the statement T has not been 2973 corrupted. For now, this function simply checks that T contains no 2974 circularities. */ 2975 2976 void 2977 verify_stmt_tree (tree t) 2978 { 2979 hash_table<nofree_ptr_hash <tree_node> > statements (37); 2980 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL); 2981 } 2982 2983 /* Check if the type T depends on a type with no linkage and if so, 2984 return it. If RELAXED_P then do not consider a class type declared 2985 within a vague-linkage function to have no linkage. Remember: 2986 no-linkage is not the same as internal-linkage*/ 2987 2988 tree 2989 no_linkage_check (tree t, bool relaxed_p) 2990 { 2991 tree r; 2992 2993 /* Lambda types that don't have mangling scope have no linkage. We 2994 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because 2995 when we get here from pushtag none of the lambda information is 2996 set up yet, so we want to assume that the lambda has linkage and 2997 fix it up later if not. We need to check this even in templates so 2998 that we properly handle a lambda-expression in the signature. */ 2999 if (LAMBDA_TYPE_P (t) 3000 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node) 3001 { 3002 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t); 3003 if (!extra) 3004 return t; 3005 } 3006 3007 /* Otherwise there's no point in checking linkage on template functions; we 3008 can't know their complete types. */ 3009 if (processing_template_decl) 3010 return NULL_TREE; 3011 3012 switch (TREE_CODE (t)) 3013 { 3014 case RECORD_TYPE: 3015 if (TYPE_PTRMEMFUNC_P (t)) 3016 goto ptrmem; 3017 /* Fall through. */ 3018 case UNION_TYPE: 3019 if (!CLASS_TYPE_P (t)) 3020 return NULL_TREE; 3021 /* Fall through. */ 3022 case ENUMERAL_TYPE: 3023 /* Only treat unnamed types as having no linkage if they're at 3024 namespace scope. This is core issue 966. */ 3025 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t)) 3026 return t; 3027 3028 for (r = CP_TYPE_CONTEXT (t); ; ) 3029 { 3030 /* If we're a nested type of a !TREE_PUBLIC class, we might not 3031 have linkage, or we might just be in an anonymous namespace. 3032 If we're in a TREE_PUBLIC class, we have linkage. */ 3033 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r))) 3034 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p); 3035 else if (TREE_CODE (r) == FUNCTION_DECL) 3036 { 3037 if (!relaxed_p || !vague_linkage_p (r)) 3038 return t; 3039 else 3040 r = CP_DECL_CONTEXT (r); 3041 } 3042 else 3043 break; 3044 } 3045 3046 return NULL_TREE; 3047 3048 case ARRAY_TYPE: 3049 case POINTER_TYPE: 3050 case REFERENCE_TYPE: 3051 case VECTOR_TYPE: 3052 return no_linkage_check (TREE_TYPE (t), relaxed_p); 3053 3054 case OFFSET_TYPE: 3055 ptrmem: 3056 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t), 3057 relaxed_p); 3058 if (r) 3059 return r; 3060 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p); 3061 3062 case METHOD_TYPE: 3063 case FUNCTION_TYPE: 3064 { 3065 tree parm = TYPE_ARG_TYPES (t); 3066 if (TREE_CODE (t) == METHOD_TYPE) 3067 /* The 'this' pointer isn't interesting; a method has the same 3068 linkage (or lack thereof) as its enclosing class. */ 3069 parm = TREE_CHAIN (parm); 3070 for (; 3071 parm && parm != void_list_node; 3072 parm = TREE_CHAIN (parm)) 3073 { 3074 r = no_linkage_check (TREE_VALUE (parm), relaxed_p); 3075 if (r) 3076 return r; 3077 } 3078 return no_linkage_check (TREE_TYPE (t), relaxed_p); 3079 } 3080 3081 default: 3082 return NULL_TREE; 3083 } 3084 } 3085 3086 extern int depth_reached; 3087 3088 void 3089 cxx_print_statistics (void) 3090 { 3091 print_template_statistics (); 3092 if (GATHER_STATISTICS) 3093 fprintf (stderr, "maximum template instantiation depth reached: %d\n", 3094 depth_reached); 3095 } 3096 3097 /* Return, as an INTEGER_CST node, the number of elements for TYPE 3098 (which is an ARRAY_TYPE). This counts only elements of the top 3099 array. */ 3100 3101 tree 3102 array_type_nelts_top (tree type) 3103 { 3104 return fold_build2_loc (input_location, 3105 PLUS_EXPR, sizetype, 3106 array_type_nelts (type), 3107 size_one_node); 3108 } 3109 3110 /* Return, as an INTEGER_CST node, the number of elements for TYPE 3111 (which is an ARRAY_TYPE). This one is a recursive count of all 3112 ARRAY_TYPEs that are clumped together. */ 3113 3114 tree 3115 array_type_nelts_total (tree type) 3116 { 3117 tree sz = array_type_nelts_top (type); 3118 type = TREE_TYPE (type); 3119 while (TREE_CODE (type) == ARRAY_TYPE) 3120 { 3121 tree n = array_type_nelts_top (type); 3122 sz = fold_build2_loc (input_location, 3123 MULT_EXPR, sizetype, sz, n); 3124 type = TREE_TYPE (type); 3125 } 3126 return sz; 3127 } 3128 3129 /* Return true if FNDECL is std::source_location::current () method. */ 3130 3131 bool 3132 source_location_current_p (tree fndecl) 3133 { 3134 gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL 3135 && DECL_IMMEDIATE_FUNCTION_P (fndecl)); 3136 if (DECL_NAME (fndecl) == NULL_TREE 3137 || TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE 3138 || TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != RECORD_TYPE 3139 || DECL_CONTEXT (fndecl) != TREE_TYPE (TREE_TYPE (fndecl)) 3140 || !id_equal (DECL_NAME (fndecl), "current")) 3141 return false; 3142 3143 tree source_location = DECL_CONTEXT (fndecl); 3144 if (TYPE_NAME (source_location) == NULL_TREE 3145 || TREE_CODE (TYPE_NAME (source_location)) != TYPE_DECL 3146 || TYPE_IDENTIFIER (source_location) == NULL_TREE 3147 || !id_equal (TYPE_IDENTIFIER (source_location), 3148 "source_location") 3149 || !decl_in_std_namespace_p (TYPE_NAME (source_location))) 3150 return false; 3151 3152 return true; 3153 } 3154 3155 struct bot_data 3156 { 3157 splay_tree target_remap; 3158 bool clear_location; 3159 }; 3160 3161 /* Called from break_out_target_exprs via mapcar. */ 3162 3163 static tree 3164 bot_manip (tree* tp, int* walk_subtrees, void* data_) 3165 { 3166 bot_data &data = *(bot_data*)data_; 3167 splay_tree target_remap = data.target_remap; 3168 tree t = *tp; 3169 3170 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t)) 3171 { 3172 /* There can't be any TARGET_EXPRs or their slot variables below this 3173 point. But we must make a copy, in case subsequent processing 3174 alters any part of it. For example, during gimplification a cast 3175 of the form (T) &X::f (where "f" is a member function) will lead 3176 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */ 3177 *walk_subtrees = 0; 3178 *tp = unshare_expr (t); 3179 return NULL_TREE; 3180 } 3181 if (TREE_CODE (t) == TARGET_EXPR) 3182 { 3183 tree u; 3184 3185 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR) 3186 { 3187 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1), 3188 tf_warning_or_error); 3189 if (u == error_mark_node) 3190 return u; 3191 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1))) 3192 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true; 3193 } 3194 else 3195 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1), 3196 tf_warning_or_error); 3197 3198 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t); 3199 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t); 3200 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t); 3201 3202 /* Map the old variable to the new one. */ 3203 splay_tree_insert (target_remap, 3204 (splay_tree_key) TREE_OPERAND (t, 0), 3205 (splay_tree_value) TREE_OPERAND (u, 0)); 3206 3207 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1), 3208 data.clear_location); 3209 if (TREE_OPERAND (u, 1) == error_mark_node) 3210 return error_mark_node; 3211 3212 /* Replace the old expression with the new version. */ 3213 *tp = u; 3214 /* We don't have to go below this point; the recursive call to 3215 break_out_target_exprs will have handled anything below this 3216 point. */ 3217 *walk_subtrees = 0; 3218 return NULL_TREE; 3219 } 3220 if (TREE_CODE (*tp) == SAVE_EXPR) 3221 { 3222 t = *tp; 3223 splay_tree_node n = splay_tree_lookup (target_remap, 3224 (splay_tree_key) t); 3225 if (n) 3226 { 3227 *tp = (tree)n->value; 3228 *walk_subtrees = 0; 3229 } 3230 else 3231 { 3232 copy_tree_r (tp, walk_subtrees, NULL); 3233 splay_tree_insert (target_remap, 3234 (splay_tree_key)t, 3235 (splay_tree_value)*tp); 3236 /* Make sure we don't remap an already-remapped SAVE_EXPR. */ 3237 splay_tree_insert (target_remap, 3238 (splay_tree_key)*tp, 3239 (splay_tree_value)*tp); 3240 } 3241 return NULL_TREE; 3242 } 3243 if (TREE_CODE (*tp) == DECL_EXPR 3244 && VAR_P (DECL_EXPR_DECL (*tp)) 3245 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp)) 3246 && !TREE_STATIC (DECL_EXPR_DECL (*tp))) 3247 { 3248 tree t; 3249 splay_tree_node n 3250 = splay_tree_lookup (target_remap, 3251 (splay_tree_key) DECL_EXPR_DECL (*tp)); 3252 if (n) 3253 t = (tree) n->value; 3254 else 3255 { 3256 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp))); 3257 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp)); 3258 splay_tree_insert (target_remap, 3259 (splay_tree_key) DECL_EXPR_DECL (*tp), 3260 (splay_tree_value) t); 3261 } 3262 copy_tree_r (tp, walk_subtrees, NULL); 3263 DECL_EXPR_DECL (*tp) = t; 3264 if (data.clear_location && EXPR_HAS_LOCATION (*tp)) 3265 SET_EXPR_LOCATION (*tp, input_location); 3266 return NULL_TREE; 3267 } 3268 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp)) 3269 { 3270 copy_tree_r (tp, walk_subtrees, NULL); 3271 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p)) 3272 { 3273 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p)); 3274 tree t = create_temporary_var (TREE_TYPE (*p)); 3275 DECL_INITIAL (t) = DECL_INITIAL (*p); 3276 DECL_CHAIN (t) = DECL_CHAIN (*p); 3277 splay_tree_insert (target_remap, (splay_tree_key) *p, 3278 (splay_tree_value) t); 3279 *p = t; 3280 } 3281 if (data.clear_location && EXPR_HAS_LOCATION (*tp)) 3282 SET_EXPR_LOCATION (*tp, input_location); 3283 return NULL_TREE; 3284 } 3285 3286 /* Make a copy of this node. */ 3287 t = copy_tree_r (tp, walk_subtrees, NULL); 3288 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR) 3289 if (!processing_template_decl) 3290 set_flags_from_callee (*tp); 3291 if (data.clear_location && EXPR_HAS_LOCATION (*tp)) 3292 SET_EXPR_LOCATION (*tp, input_location); 3293 return t; 3294 } 3295 3296 /* Replace all remapped VAR_DECLs in T with their new equivalents. 3297 DATA is really a splay-tree mapping old variables to new 3298 variables. */ 3299 3300 static tree 3301 bot_replace (tree* t, int* /*walk_subtrees*/, void* data_) 3302 { 3303 bot_data &data = *(bot_data*)data_; 3304 splay_tree target_remap = data.target_remap; 3305 3306 if (VAR_P (*t)) 3307 { 3308 splay_tree_node n = splay_tree_lookup (target_remap, 3309 (splay_tree_key) *t); 3310 if (n) 3311 *t = (tree) n->value; 3312 } 3313 else if (TREE_CODE (*t) == PARM_DECL 3314 && DECL_NAME (*t) == this_identifier 3315 && !DECL_CONTEXT (*t)) 3316 { 3317 /* In an NSDMI we need to replace the 'this' parameter we used for 3318 parsing with the real one for this function. */ 3319 *t = current_class_ptr; 3320 } 3321 else if (TREE_CODE (*t) == CONVERT_EXPR 3322 && CONVERT_EXPR_VBASE_PATH (*t)) 3323 { 3324 /* In an NSDMI build_base_path defers building conversions to morally 3325 virtual bases, and we handle it here. */ 3326 tree basetype = TREE_TYPE (*t); 3327 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype, 3328 /*check_access=*/false, /*nonnull=*/true, 3329 tf_warning_or_error); 3330 } 3331 3332 return NULL_TREE; 3333 } 3334 3335 /* When we parse a default argument expression, we may create 3336 temporary variables via TARGET_EXPRs. When we actually use the 3337 default-argument expression, we make a copy of the expression 3338 and replace the temporaries with appropriate local versions. 3339 3340 If CLEAR_LOCATION is true, override any EXPR_LOCATION with 3341 input_location. */ 3342 3343 tree 3344 break_out_target_exprs (tree t, bool clear_location /* = false */) 3345 { 3346 static int target_remap_count; 3347 static splay_tree target_remap; 3348 3349 /* We shouldn't be called on templated trees, nor do we want to 3350 produce them. */ 3351 gcc_checking_assert (!processing_template_decl); 3352 3353 if (!target_remap_count++) 3354 target_remap = splay_tree_new (splay_tree_compare_pointers, 3355 /*splay_tree_delete_key_fn=*/NULL, 3356 /*splay_tree_delete_value_fn=*/NULL); 3357 bot_data data = { target_remap, clear_location }; 3358 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node) 3359 t = error_mark_node; 3360 cp_walk_tree (&t, bot_replace, &data, NULL); 3361 3362 if (!--target_remap_count) 3363 { 3364 splay_tree_delete (target_remap); 3365 target_remap = NULL; 3366 } 3367 3368 return t; 3369 } 3370 3371 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX, 3372 which we expect to have type TYPE. */ 3373 3374 tree 3375 build_ctor_subob_ref (tree index, tree type, tree obj) 3376 { 3377 if (index == NULL_TREE) 3378 /* Can't refer to a particular member of a vector. */ 3379 obj = NULL_TREE; 3380 else if (TREE_CODE (index) == INTEGER_CST) 3381 obj = cp_build_array_ref (input_location, obj, index, tf_none); 3382 else 3383 obj = build_class_member_access_expr (obj, index, NULL_TREE, 3384 /*reference*/false, tf_none); 3385 if (obj) 3386 { 3387 tree objtype = TREE_TYPE (obj); 3388 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype)) 3389 { 3390 /* When the destination object refers to a flexible array member 3391 verify that it matches the type of the source object except 3392 for its domain and qualifiers. */ 3393 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type), 3394 TYPE_MAIN_VARIANT (objtype), 3395 COMPARE_REDECLARATION)); 3396 } 3397 else 3398 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype)); 3399 } 3400 3401 return obj; 3402 } 3403 3404 struct replace_placeholders_t 3405 { 3406 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */ 3407 tree exp; /* The outermost exp. */ 3408 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */ 3409 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */ 3410 }; 3411 3412 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and 3413 build up subexpressions as we go deeper. */ 3414 3415 static tree 3416 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_) 3417 { 3418 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_); 3419 tree obj = d->obj; 3420 3421 if (TYPE_P (*t) || TREE_CONSTANT (*t)) 3422 { 3423 *walk_subtrees = false; 3424 return NULL_TREE; 3425 } 3426 3427 switch (TREE_CODE (*t)) 3428 { 3429 case PLACEHOLDER_EXPR: 3430 { 3431 tree x = obj; 3432 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t), 3433 TREE_TYPE (x)); 3434 x = TREE_OPERAND (x, 0)) 3435 gcc_assert (handled_component_p (x)); 3436 *t = unshare_expr (x); 3437 *walk_subtrees = false; 3438 d->seen = true; 3439 } 3440 break; 3441 3442 case CONSTRUCTOR: 3443 { 3444 constructor_elt *ce; 3445 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t); 3446 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors 3447 other than the d->exp one, those have PLACEHOLDER_EXPRs 3448 related to another object. */ 3449 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t) 3450 && *t != d->exp) 3451 || d->pset->add (*t)) 3452 { 3453 *walk_subtrees = false; 3454 return NULL_TREE; 3455 } 3456 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i) 3457 { 3458 tree *valp = &ce->value; 3459 tree type = TREE_TYPE (*valp); 3460 tree subob = obj; 3461 3462 /* Elements with RANGE_EXPR index shouldn't have any 3463 placeholders in them. */ 3464 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR) 3465 continue; 3466 3467 if (TREE_CODE (*valp) == CONSTRUCTOR 3468 && AGGREGATE_TYPE_P (type)) 3469 { 3470 /* If we're looking at the initializer for OBJ, then build 3471 a sub-object reference. If we're looking at an 3472 initializer for another object, just pass OBJ down. */ 3473 if (same_type_ignoring_top_level_qualifiers_p 3474 (TREE_TYPE (*t), TREE_TYPE (obj))) 3475 subob = build_ctor_subob_ref (ce->index, type, obj); 3476 if (TREE_CODE (*valp) == TARGET_EXPR) 3477 valp = &TARGET_EXPR_INITIAL (*valp); 3478 } 3479 d->obj = subob; 3480 cp_walk_tree (valp, replace_placeholders_r, data_, NULL); 3481 d->obj = obj; 3482 } 3483 *walk_subtrees = false; 3484 break; 3485 } 3486 3487 default: 3488 if (d->pset->add (*t)) 3489 *walk_subtrees = false; 3490 break; 3491 } 3492 3493 return NULL_TREE; 3494 } 3495 3496 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if 3497 a PLACEHOLDER_EXPR has been encountered. */ 3498 3499 tree 3500 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/) 3501 { 3502 /* This is only relevant for C++14. */ 3503 if (cxx_dialect < cxx14) 3504 return exp; 3505 3506 /* If the object isn't a (member of a) class, do nothing. */ 3507 tree op0 = obj; 3508 while (handled_component_p (op0)) 3509 op0 = TREE_OPERAND (op0, 0); 3510 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0)))) 3511 return exp; 3512 3513 tree *tp = &exp; 3514 if (TREE_CODE (exp) == TARGET_EXPR) 3515 tp = &TARGET_EXPR_INITIAL (exp); 3516 hash_set<tree> pset; 3517 replace_placeholders_t data = { obj, *tp, false, &pset }; 3518 cp_walk_tree (tp, replace_placeholders_r, &data, NULL); 3519 if (seen_p) 3520 *seen_p = data.seen; 3521 return exp; 3522 } 3523 3524 /* Callback function for find_placeholders. */ 3525 3526 static tree 3527 find_placeholders_r (tree *t, int *walk_subtrees, void *) 3528 { 3529 if (TYPE_P (*t) || TREE_CONSTANT (*t)) 3530 { 3531 *walk_subtrees = false; 3532 return NULL_TREE; 3533 } 3534 3535 switch (TREE_CODE (*t)) 3536 { 3537 case PLACEHOLDER_EXPR: 3538 return *t; 3539 3540 case CONSTRUCTOR: 3541 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)) 3542 *walk_subtrees = false; 3543 break; 3544 3545 default: 3546 break; 3547 } 3548 3549 return NULL_TREE; 3550 } 3551 3552 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into 3553 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */ 3554 3555 bool 3556 find_placeholders (tree exp) 3557 { 3558 /* This is only relevant for C++14. */ 3559 if (cxx_dialect < cxx14) 3560 return false; 3561 3562 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL); 3563 } 3564 3565 /* Similar to `build_nt', but for template definitions of dependent 3566 expressions */ 3567 3568 tree 3569 build_min_nt_loc (location_t loc, enum tree_code code, ...) 3570 { 3571 tree t; 3572 int length; 3573 int i; 3574 va_list p; 3575 3576 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 3577 3578 va_start (p, code); 3579 3580 t = make_node (code); 3581 SET_EXPR_LOCATION (t, loc); 3582 length = TREE_CODE_LENGTH (code); 3583 3584 for (i = 0; i < length; i++) 3585 TREE_OPERAND (t, i) = va_arg (p, tree); 3586 3587 va_end (p); 3588 return t; 3589 } 3590 3591 /* Similar to `build', but for template definitions. */ 3592 3593 tree 3594 build_min (enum tree_code code, tree tt, ...) 3595 { 3596 tree t; 3597 int length; 3598 int i; 3599 va_list p; 3600 3601 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 3602 3603 va_start (p, tt); 3604 3605 t = make_node (code); 3606 length = TREE_CODE_LENGTH (code); 3607 TREE_TYPE (t) = tt; 3608 3609 for (i = 0; i < length; i++) 3610 { 3611 tree x = va_arg (p, tree); 3612 TREE_OPERAND (t, i) = x; 3613 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x)) 3614 TREE_SIDE_EFFECTS (t) = 1; 3615 } 3616 3617 va_end (p); 3618 3619 return t; 3620 } 3621 3622 /* Similar to `build', but for template definitions of non-dependent 3623 expressions. NON_DEP is the non-dependent expression that has been 3624 built. */ 3625 3626 tree 3627 build_min_non_dep (enum tree_code code, tree non_dep, ...) 3628 { 3629 tree t; 3630 int length; 3631 int i; 3632 va_list p; 3633 3634 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 3635 3636 va_start (p, non_dep); 3637 3638 if (REFERENCE_REF_P (non_dep)) 3639 non_dep = TREE_OPERAND (non_dep, 0); 3640 3641 t = make_node (code); 3642 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep)); 3643 length = TREE_CODE_LENGTH (code); 3644 TREE_TYPE (t) = unlowered_expr_type (non_dep); 3645 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep); 3646 3647 for (i = 0; i < length; i++) 3648 TREE_OPERAND (t, i) = va_arg (p, tree); 3649 3650 va_end (p); 3651 return convert_from_reference (t); 3652 } 3653 3654 /* Similar to build_min_nt, but call expressions */ 3655 3656 tree 3657 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args) 3658 { 3659 tree ret, t; 3660 unsigned int ix; 3661 3662 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3); 3663 CALL_EXPR_FN (ret) = fn; 3664 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE; 3665 FOR_EACH_VEC_SAFE_ELT (args, ix, t) 3666 CALL_EXPR_ARG (ret, ix) = t; 3667 3668 return ret; 3669 } 3670 3671 /* Similar to `build_min_nt_call_vec', but for template definitions of 3672 non-dependent expressions. NON_DEP is the non-dependent expression 3673 that has been built. */ 3674 3675 tree 3676 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec) 3677 { 3678 tree t = build_min_nt_call_vec (fn, argvec); 3679 if (REFERENCE_REF_P (non_dep)) 3680 non_dep = TREE_OPERAND (non_dep, 0); 3681 TREE_TYPE (t) = TREE_TYPE (non_dep); 3682 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep); 3683 return convert_from_reference (t); 3684 } 3685 3686 /* Similar to build_min_non_dep, but for expressions that have been resolved to 3687 a call to an operator overload. OP is the operator that has been 3688 overloaded. NON_DEP is the non-dependent expression that's been built, 3689 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is 3690 the overload that NON_DEP is calling. */ 3691 3692 tree 3693 build_min_non_dep_op_overload (enum tree_code op, 3694 tree non_dep, 3695 tree overload, ...) 3696 { 3697 va_list p; 3698 int nargs, expected_nargs; 3699 tree fn, call; 3700 3701 non_dep = extract_call_expr (non_dep); 3702 3703 nargs = call_expr_nargs (non_dep); 3704 3705 expected_nargs = cp_tree_code_length (op); 3706 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE) 3707 expected_nargs -= 1; 3708 if ((op == POSTINCREMENT_EXPR 3709 || op == POSTDECREMENT_EXPR) 3710 /* With -fpermissive non_dep could be operator++(). */ 3711 && (!flag_permissive || nargs != expected_nargs)) 3712 expected_nargs += 1; 3713 gcc_assert (nargs == expected_nargs); 3714 3715 releasing_vec args; 3716 va_start (p, overload); 3717 3718 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE) 3719 { 3720 fn = overload; 3721 for (int i = 0; i < nargs; i++) 3722 { 3723 tree arg = va_arg (p, tree); 3724 vec_safe_push (args, arg); 3725 } 3726 } 3727 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE) 3728 { 3729 tree object = va_arg (p, tree); 3730 tree binfo = TYPE_BINFO (TREE_TYPE (object)); 3731 tree method = build_baselink (binfo, binfo, overload, NULL_TREE); 3732 fn = build_min (COMPONENT_REF, TREE_TYPE (overload), 3733 object, method, NULL_TREE); 3734 for (int i = 0; i < nargs; i++) 3735 { 3736 tree arg = va_arg (p, tree); 3737 vec_safe_push (args, arg); 3738 } 3739 } 3740 else 3741 gcc_unreachable (); 3742 3743 va_end (p); 3744 call = build_min_non_dep_call_vec (non_dep, fn, args); 3745 3746 tree call_expr = extract_call_expr (call); 3747 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep); 3748 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true; 3749 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep); 3750 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep); 3751 3752 return call; 3753 } 3754 3755 /* Similar to above build_min_non_dep_op_overload, but arguments 3756 are taken from ARGS vector. */ 3757 3758 tree 3759 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object, 3760 vec<tree, va_gc> *args) 3761 { 3762 non_dep = extract_call_expr (non_dep); 3763 3764 unsigned int nargs = call_expr_nargs (non_dep); 3765 gcc_assert (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE); 3766 tree binfo = TYPE_BINFO (TREE_TYPE (object)); 3767 tree method = build_baselink (binfo, binfo, overload, NULL_TREE); 3768 tree fn = build_min (COMPONENT_REF, TREE_TYPE (overload), 3769 object, method, NULL_TREE); 3770 gcc_assert (vec_safe_length (args) == nargs); 3771 3772 tree call = build_min_non_dep_call_vec (non_dep, fn, args); 3773 3774 tree call_expr = extract_call_expr (call); 3775 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep); 3776 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true; 3777 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep); 3778 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep); 3779 3780 return call; 3781 } 3782 3783 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */ 3784 3785 vec<tree, va_gc> * 3786 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx) 3787 { 3788 unsigned len = vec_safe_length (old_vec); 3789 gcc_assert (idx <= len); 3790 3791 vec<tree, va_gc> *new_vec = NULL; 3792 vec_alloc (new_vec, len + 1); 3793 3794 unsigned i; 3795 for (i = 0; i < len; ++i) 3796 { 3797 if (i == idx) 3798 new_vec->quick_push (elt); 3799 new_vec->quick_push ((*old_vec)[i]); 3800 } 3801 if (i == idx) 3802 new_vec->quick_push (elt); 3803 3804 return new_vec; 3805 } 3806 3807 tree 3808 get_type_decl (tree t) 3809 { 3810 if (TREE_CODE (t) == TYPE_DECL) 3811 return t; 3812 if (TYPE_P (t)) 3813 return TYPE_STUB_DECL (t); 3814 gcc_assert (t == error_mark_node); 3815 return t; 3816 } 3817 3818 /* Returns the namespace that contains DECL, whether directly or 3819 indirectly. */ 3820 3821 tree 3822 decl_namespace_context (tree decl) 3823 { 3824 while (1) 3825 { 3826 if (TREE_CODE (decl) == NAMESPACE_DECL) 3827 return decl; 3828 else if (TYPE_P (decl)) 3829 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl)); 3830 else 3831 decl = CP_DECL_CONTEXT (decl); 3832 } 3833 } 3834 3835 /* Returns true if decl is within an anonymous namespace, however deeply 3836 nested, or false otherwise. */ 3837 3838 bool 3839 decl_anon_ns_mem_p (const_tree decl) 3840 { 3841 while (TREE_CODE (decl) != NAMESPACE_DECL) 3842 { 3843 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */ 3844 if (TYPE_P (decl)) 3845 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl)); 3846 3847 decl = CP_DECL_CONTEXT (decl); 3848 } 3849 return !TREE_PUBLIC (decl); 3850 } 3851 3852 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs. 3853 Return whether their CALL_EXPR_FNs are equivalent. */ 3854 3855 static bool 3856 called_fns_equal (tree t1, tree t2) 3857 { 3858 /* Core 1321: dependent names are equivalent even if the overload sets 3859 are different. But do compare explicit template arguments. */ 3860 tree name1 = call_expr_dependent_name (t1); 3861 tree name2 = call_expr_dependent_name (t2); 3862 t1 = CALL_EXPR_FN (t1); 3863 t2 = CALL_EXPR_FN (t2); 3864 if (name1 || name2) 3865 { 3866 tree targs1 = NULL_TREE, targs2 = NULL_TREE; 3867 3868 if (name1 != name2) 3869 return false; 3870 3871 /* FIXME dependent_name currently returns an unqualified name regardless 3872 of whether the function was named with a qualified- or unqualified-id. 3873 Until that's fixed, check that we aren't looking at overload sets from 3874 different scopes. */ 3875 if (is_overloaded_fn (t1) && is_overloaded_fn (t2) 3876 && (DECL_CONTEXT (get_first_fn (t1)) 3877 != DECL_CONTEXT (get_first_fn (t2)))) 3878 return false; 3879 3880 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR) 3881 targs1 = TREE_OPERAND (t1, 1); 3882 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR) 3883 targs2 = TREE_OPERAND (t2, 1); 3884 return cp_tree_equal (targs1, targs2); 3885 } 3886 else 3887 return cp_tree_equal (t1, t2); 3888 } 3889 3890 /* Return truthvalue of whether T1 is the same tree structure as T2. 3891 Return 1 if they are the same. Return 0 if they are different. */ 3892 3893 bool 3894 cp_tree_equal (tree t1, tree t2) 3895 { 3896 enum tree_code code1, code2; 3897 3898 if (t1 == t2) 3899 return true; 3900 if (!t1 || !t2) 3901 return false; 3902 3903 code1 = TREE_CODE (t1); 3904 code2 = TREE_CODE (t2); 3905 3906 if (code1 != code2) 3907 return false; 3908 3909 if (CONSTANT_CLASS_P (t1) 3910 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) 3911 return false; 3912 3913 switch (code1) 3914 { 3915 case VOID_CST: 3916 /* There's only a single VOID_CST node, so we should never reach 3917 here. */ 3918 gcc_unreachable (); 3919 3920 case INTEGER_CST: 3921 return tree_int_cst_equal (t1, t2); 3922 3923 case REAL_CST: 3924 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2)); 3925 3926 case STRING_CST: 3927 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) 3928 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), 3929 TREE_STRING_LENGTH (t1)); 3930 3931 case FIXED_CST: 3932 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), 3933 TREE_FIXED_CST (t2)); 3934 3935 case COMPLEX_CST: 3936 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) 3937 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); 3938 3939 case VECTOR_CST: 3940 return operand_equal_p (t1, t2, OEP_ONLY_CONST); 3941 3942 case CONSTRUCTOR: 3943 /* We need to do this when determining whether or not two 3944 non-type pointer to member function template arguments 3945 are the same. */ 3946 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) 3947 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2)) 3948 return false; 3949 { 3950 tree field, value; 3951 unsigned int i; 3952 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value) 3953 { 3954 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i); 3955 if (!cp_tree_equal (field, elt2->index) 3956 || !cp_tree_equal (value, elt2->value)) 3957 return false; 3958 } 3959 } 3960 return true; 3961 3962 case TREE_LIST: 3963 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) 3964 return false; 3965 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) 3966 return false; 3967 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); 3968 3969 case SAVE_EXPR: 3970 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 3971 3972 case CALL_EXPR: 3973 { 3974 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2)) 3975 return false; 3976 3977 if (!called_fns_equal (t1, t2)) 3978 return false; 3979 3980 call_expr_arg_iterator iter1, iter2; 3981 init_call_expr_arg_iterator (t1, &iter1); 3982 init_call_expr_arg_iterator (t2, &iter2); 3983 if (iter1.n != iter2.n) 3984 return false; 3985 3986 while (more_call_expr_args_p (&iter1)) 3987 { 3988 tree arg1 = next_call_expr_arg (&iter1); 3989 tree arg2 = next_call_expr_arg (&iter2); 3990 3991 gcc_checking_assert (arg1 && arg2); 3992 if (!cp_tree_equal (arg1, arg2)) 3993 return false; 3994 } 3995 3996 return true; 3997 } 3998 3999 case TARGET_EXPR: 4000 { 4001 tree o1 = TREE_OPERAND (t1, 0); 4002 tree o2 = TREE_OPERAND (t2, 0); 4003 4004 /* Special case: if either target is an unallocated VAR_DECL, 4005 it means that it's going to be unified with whatever the 4006 TARGET_EXPR is really supposed to initialize, so treat it 4007 as being equivalent to anything. */ 4008 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE 4009 && !DECL_RTL_SET_P (o1)) 4010 /*Nop*/; 4011 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE 4012 && !DECL_RTL_SET_P (o2)) 4013 /*Nop*/; 4014 else if (!cp_tree_equal (o1, o2)) 4015 return false; 4016 4017 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); 4018 } 4019 4020 case PARM_DECL: 4021 /* For comparing uses of parameters in late-specified return types 4022 with an out-of-class definition of the function, but can also come 4023 up for expressions that involve 'this' in a member function 4024 template. */ 4025 4026 if (comparing_specializations 4027 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2)) 4028 /* When comparing hash table entries, only an exact match is 4029 good enough; we don't want to replace 'this' with the 4030 version from another function. But be more flexible 4031 with parameters with identical contexts. */ 4032 return false; 4033 4034 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) 4035 { 4036 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2)) 4037 return false; 4038 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2)) 4039 return false; 4040 if (DECL_ARTIFICIAL (t1) 4041 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2) 4042 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2))) 4043 return true; 4044 } 4045 return false; 4046 4047 case VAR_DECL: 4048 case CONST_DECL: 4049 case FIELD_DECL: 4050 case FUNCTION_DECL: 4051 case TEMPLATE_DECL: 4052 case IDENTIFIER_NODE: 4053 case SSA_NAME: 4054 case USING_DECL: 4055 case DEFERRED_PARSE: 4056 return false; 4057 4058 case BASELINK: 4059 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2) 4060 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2) 4061 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2) 4062 && cp_tree_equal (BASELINK_FUNCTIONS (t1), 4063 BASELINK_FUNCTIONS (t2))); 4064 4065 case TEMPLATE_PARM_INDEX: 4066 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2) 4067 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2) 4068 && (TEMPLATE_PARM_PARAMETER_PACK (t1) 4069 == TEMPLATE_PARM_PARAMETER_PACK (t2)) 4070 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)), 4071 TREE_TYPE (TEMPLATE_PARM_DECL (t2)))); 4072 4073 case TEMPLATE_ID_EXPR: 4074 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))) 4075 return false; 4076 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1))) 4077 return false; 4078 return true; 4079 4080 case CONSTRAINT_INFO: 4081 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1), 4082 CI_ASSOCIATED_CONSTRAINTS (t2)); 4083 4084 case CHECK_CONSTR: 4085 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2) 4086 && comp_template_args (CHECK_CONSTR_ARGS (t1), 4087 CHECK_CONSTR_ARGS (t2))); 4088 4089 case TREE_VEC: 4090 /* These are template args. Really we should be getting the 4091 caller to do this as it knows it to be true. */ 4092 if (!comp_template_args (t1, t2, NULL, NULL, false)) 4093 return false; 4094 return true; 4095 4096 case SIZEOF_EXPR: 4097 case ALIGNOF_EXPR: 4098 { 4099 tree o1 = TREE_OPERAND (t1, 0); 4100 tree o2 = TREE_OPERAND (t2, 0); 4101 4102 if (code1 == SIZEOF_EXPR) 4103 { 4104 if (SIZEOF_EXPR_TYPE_P (t1)) 4105 o1 = TREE_TYPE (o1); 4106 if (SIZEOF_EXPR_TYPE_P (t2)) 4107 o2 = TREE_TYPE (o2); 4108 } 4109 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2)) 4110 return false; 4111 4112 if (TREE_CODE (o1) != TREE_CODE (o2)) 4113 return false; 4114 4115 if (ARGUMENT_PACK_P (o1)) 4116 return template_args_equal (o1, o2); 4117 else if (TYPE_P (o1)) 4118 return same_type_p (o1, o2); 4119 else 4120 return cp_tree_equal (o1, o2); 4121 } 4122 4123 case MODOP_EXPR: 4124 { 4125 tree t1_op1, t2_op1; 4126 4127 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))) 4128 return false; 4129 4130 t1_op1 = TREE_OPERAND (t1, 1); 4131 t2_op1 = TREE_OPERAND (t2, 1); 4132 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1)) 4133 return false; 4134 4135 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2)); 4136 } 4137 4138 case PTRMEM_CST: 4139 /* Two pointer-to-members are the same if they point to the same 4140 field or function in the same class. */ 4141 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2)) 4142 return false; 4143 4144 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)); 4145 4146 case OVERLOAD: 4147 { 4148 /* Two overloads. Must be exactly the same set of decls. */ 4149 lkp_iterator first (t1); 4150 lkp_iterator second (t2); 4151 4152 for (; first && second; ++first, ++second) 4153 if (*first != *second) 4154 return false; 4155 return !(first || second); 4156 } 4157 4158 case TRAIT_EXPR: 4159 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2)) 4160 return false; 4161 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2)) 4162 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2)); 4163 4164 case NON_LVALUE_EXPR: 4165 case VIEW_CONVERT_EXPR: 4166 /* Used for location wrappers with possibly NULL types. */ 4167 if (!TREE_TYPE (t1) || !TREE_TYPE (t2)) 4168 { 4169 if (TREE_TYPE (t1) || TREE_TYPE (t2)) 4170 return false; 4171 break; 4172 } 4173 /* FALLTHROUGH */ 4174 4175 case CAST_EXPR: 4176 case STATIC_CAST_EXPR: 4177 case REINTERPRET_CAST_EXPR: 4178 case CONST_CAST_EXPR: 4179 case DYNAMIC_CAST_EXPR: 4180 case IMPLICIT_CONV_EXPR: 4181 case NEW_EXPR: 4182 case BIT_CAST_EXPR: 4183 CASE_CONVERT: 4184 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) 4185 return false; 4186 /* Now compare operands as usual. */ 4187 break; 4188 4189 case DEFERRED_NOEXCEPT: 4190 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1), 4191 DEFERRED_NOEXCEPT_PATTERN (t2)) 4192 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1), 4193 DEFERRED_NOEXCEPT_ARGS (t2))); 4194 4195 case LAMBDA_EXPR: 4196 /* Two lambda-expressions are never considered equivalent. */ 4197 return false; 4198 4199 case TYPE_ARGUMENT_PACK: 4200 case NONTYPE_ARGUMENT_PACK: 4201 { 4202 tree p1 = ARGUMENT_PACK_ARGS (t1); 4203 tree p2 = ARGUMENT_PACK_ARGS (t2); 4204 int len = TREE_VEC_LENGTH (p1); 4205 if (TREE_VEC_LENGTH (p2) != len) 4206 return false; 4207 4208 for (int ix = 0; ix != len; ix++) 4209 if (!template_args_equal (TREE_VEC_ELT (p1, ix), 4210 TREE_VEC_ELT (p2, ix))) 4211 return false; 4212 return true; 4213 } 4214 4215 case EXPR_PACK_EXPANSION: 4216 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1), 4217 PACK_EXPANSION_PATTERN (t2))) 4218 return false; 4219 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1), 4220 PACK_EXPANSION_EXTRA_ARGS (t2))) 4221 return false; 4222 return true; 4223 4224 default: 4225 break; 4226 } 4227 4228 switch (TREE_CODE_CLASS (code1)) 4229 { 4230 case tcc_unary: 4231 case tcc_binary: 4232 case tcc_comparison: 4233 case tcc_expression: 4234 case tcc_vl_exp: 4235 case tcc_reference: 4236 case tcc_statement: 4237 { 4238 int n = cp_tree_operand_length (t1); 4239 if (TREE_CODE_CLASS (code1) == tcc_vl_exp 4240 && n != TREE_OPERAND_LENGTH (t2)) 4241 return false; 4242 4243 for (int i = 0; i < n; ++i) 4244 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) 4245 return false; 4246 4247 return true; 4248 } 4249 4250 case tcc_type: 4251 return same_type_p (t1, t2); 4252 4253 default: 4254 gcc_unreachable (); 4255 } 4256 4257 /* We can get here with --disable-checking. */ 4258 return false; 4259 } 4260 4261 /* The type of ARG when used as an lvalue. */ 4262 4263 tree 4264 lvalue_type (tree arg) 4265 { 4266 tree type = TREE_TYPE (arg); 4267 return type; 4268 } 4269 4270 /* The type of ARG for printing error messages; denote lvalues with 4271 reference types. */ 4272 4273 tree 4274 error_type (tree arg) 4275 { 4276 tree type = TREE_TYPE (arg); 4277 4278 if (TREE_CODE (type) == ARRAY_TYPE) 4279 ; 4280 else if (TREE_CODE (type) == ERROR_MARK) 4281 ; 4282 else if (lvalue_p (arg)) 4283 type = build_reference_type (lvalue_type (arg)); 4284 else if (MAYBE_CLASS_TYPE_P (type)) 4285 type = lvalue_type (arg); 4286 4287 return type; 4288 } 4289 4290 /* Does FUNCTION use a variable-length argument list? */ 4291 4292 int 4293 varargs_function_p (const_tree function) 4294 { 4295 return stdarg_p (TREE_TYPE (function)); 4296 } 4297 4298 /* Returns 1 if decl is a member of a class. */ 4299 4300 int 4301 member_p (const_tree decl) 4302 { 4303 const_tree const ctx = DECL_CONTEXT (decl); 4304 return (ctx && TYPE_P (ctx)); 4305 } 4306 4307 /* Create a placeholder for member access where we don't actually have an 4308 object that the access is against. For a general declval<T> equivalent, 4309 use build_stub_object instead. */ 4310 4311 tree 4312 build_dummy_object (tree type) 4313 { 4314 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node); 4315 return cp_build_fold_indirect_ref (decl); 4316 } 4317 4318 /* We've gotten a reference to a member of TYPE. Return *this if appropriate, 4319 or a dummy object otherwise. If BINFOP is non-0, it is filled with the 4320 binfo path from current_class_type to TYPE, or 0. */ 4321 4322 tree 4323 maybe_dummy_object (tree type, tree* binfop) 4324 { 4325 tree decl, context; 4326 tree binfo; 4327 tree current = current_nonlambda_class_type (); 4328 4329 if (current 4330 && (binfo = lookup_base (current, type, ba_any, NULL, 4331 tf_warning_or_error))) 4332 context = current; 4333 else 4334 { 4335 /* Reference from a nested class member function. */ 4336 context = type; 4337 binfo = TYPE_BINFO (type); 4338 } 4339 4340 if (binfop) 4341 *binfop = binfo; 4342 4343 /* current_class_ref might not correspond to current_class_type if 4344 we're in tsubst_default_argument or a lambda-declarator; in either 4345 case, we want to use current_class_ref if it matches CONTEXT. */ 4346 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE; 4347 if (ctype 4348 && same_type_ignoring_top_level_qualifiers_p (ctype, context)) 4349 decl = current_class_ref; 4350 else 4351 { 4352 /* Return a dummy object whose cv-quals are consistent with (the 4353 non-lambda) 'this' if available. */ 4354 if (ctype) 4355 { 4356 int quals = TYPE_UNQUALIFIED; 4357 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype)) 4358 { 4359 if (tree cap = lambda_expr_this_capture (lambda, false)) 4360 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap))); 4361 } 4362 else 4363 quals = cp_type_quals (ctype); 4364 context = cp_build_qualified_type (context, quals); 4365 } 4366 decl = build_dummy_object (context); 4367 } 4368 4369 return decl; 4370 } 4371 4372 /* Returns 1 if OB is a placeholder object, or a pointer to one. */ 4373 4374 bool 4375 is_dummy_object (const_tree ob) 4376 { 4377 if (INDIRECT_REF_P (ob)) 4378 ob = TREE_OPERAND (ob, 0); 4379 return (TREE_CODE (ob) == CONVERT_EXPR 4380 && TREE_OPERAND (ob, 0) == void_node); 4381 } 4382 4383 /* Returns true if TYPE is char, unsigned char, or std::byte. */ 4384 4385 bool 4386 is_byte_access_type (tree type) 4387 { 4388 type = TYPE_MAIN_VARIANT (type); 4389 if (type == char_type_node 4390 || type == unsigned_char_type_node) 4391 return true; 4392 4393 return (TREE_CODE (type) == ENUMERAL_TYPE 4394 && TYPE_CONTEXT (type) == std_node 4395 && !strcmp ("byte", TYPE_NAME_STRING (type))); 4396 } 4397 4398 /* Returns true if TYPE is unsigned char or std::byte. */ 4399 4400 bool 4401 is_byte_access_type_not_plain_char (tree type) 4402 { 4403 type = TYPE_MAIN_VARIANT (type); 4404 if (type == char_type_node) 4405 return false; 4406 4407 return is_byte_access_type (type); 4408 } 4409 4410 /* Returns 1 iff type T is something we want to treat as a scalar type for 4411 the purpose of deciding whether it is trivial/POD/standard-layout. */ 4412 4413 bool 4414 scalarish_type_p (const_tree t) 4415 { 4416 if (t == error_mark_node) 4417 return 1; 4418 4419 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t)); 4420 } 4421 4422 /* Returns true iff T requires non-trivial default initialization. */ 4423 4424 bool 4425 type_has_nontrivial_default_init (const_tree t) 4426 { 4427 t = strip_array_types (CONST_CAST_TREE (t)); 4428 4429 if (CLASS_TYPE_P (t)) 4430 return TYPE_HAS_COMPLEX_DFLT (t); 4431 else 4432 return 0; 4433 } 4434 4435 /* Track classes with only deleted copy/move constructors so that we can warn 4436 if they are used in call/return by value. */ 4437 4438 static GTY(()) hash_set<tree>* deleted_copy_types; 4439 static void 4440 remember_deleted_copy (const_tree t) 4441 { 4442 if (!deleted_copy_types) 4443 deleted_copy_types = hash_set<tree>::create_ggc(37); 4444 deleted_copy_types->add (CONST_CAST_TREE (t)); 4445 } 4446 void 4447 maybe_warn_parm_abi (tree t, location_t loc) 4448 { 4449 if (!deleted_copy_types 4450 || !deleted_copy_types->contains (t)) 4451 return; 4452 4453 if ((flag_abi_version == 12 || warn_abi_version == 12) 4454 && classtype_has_non_deleted_move_ctor (t)) 4455 { 4456 bool w; 4457 auto_diagnostic_group d; 4458 if (flag_abi_version > 12) 4459 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes " 4460 "the calling convention for %qT, which was " 4461 "accidentally changed in 8.1", t); 4462 else 4463 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) " 4464 "accidentally changes the calling convention for %qT", 4465 t); 4466 if (w) 4467 inform (location_of (t), " declared here"); 4468 return; 4469 } 4470 4471 auto_diagnostic_group d; 4472 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in " 4473 "%<-fabi-version=13%> (GCC 8.2)", t)) 4474 inform (location_of (t), " because all of its copy and move " 4475 "constructors are deleted"); 4476 } 4477 4478 /* Returns true iff copying an object of type T (including via move 4479 constructor) is non-trivial. That is, T has no non-trivial copy 4480 constructors and no non-trivial move constructors, and not all copy/move 4481 constructors are deleted. This function implements the ABI notion of 4482 non-trivial copy, which has diverged from the one in the standard. */ 4483 4484 bool 4485 type_has_nontrivial_copy_init (const_tree type) 4486 { 4487 tree t = strip_array_types (CONST_CAST_TREE (type)); 4488 4489 if (CLASS_TYPE_P (t)) 4490 { 4491 gcc_assert (COMPLETE_TYPE_P (t)); 4492 4493 if (TYPE_HAS_COMPLEX_COPY_CTOR (t) 4494 || TYPE_HAS_COMPLEX_MOVE_CTOR (t)) 4495 /* Nontrivial. */ 4496 return true; 4497 4498 if (cxx_dialect < cxx11) 4499 /* No deleted functions before C++11. */ 4500 return false; 4501 4502 /* Before ABI v12 we did a bitwise copy of types with only deleted 4503 copy/move constructors. */ 4504 if (!abi_version_at_least (12) 4505 && !(warn_abi && abi_version_crosses (12))) 4506 return false; 4507 4508 bool saw_copy = false; 4509 bool saw_non_deleted = false; 4510 bool saw_non_deleted_move = false; 4511 4512 if (CLASSTYPE_LAZY_MOVE_CTOR (t)) 4513 saw_copy = saw_non_deleted = true; 4514 else if (CLASSTYPE_LAZY_COPY_CTOR (t)) 4515 { 4516 saw_copy = true; 4517 if (classtype_has_move_assign_or_move_ctor_p (t, true)) 4518 /* [class.copy]/8 If the class definition declares a move 4519 constructor or move assignment operator, the implicitly declared 4520 copy constructor is defined as deleted.... */; 4521 else 4522 /* Any other reason the implicitly-declared function would be 4523 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be 4524 set. */ 4525 saw_non_deleted = true; 4526 } 4527 4528 if (!saw_non_deleted) 4529 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter) 4530 { 4531 tree fn = *iter; 4532 if (copy_fn_p (fn)) 4533 { 4534 saw_copy = true; 4535 if (!DECL_DELETED_FN (fn)) 4536 { 4537 /* Not deleted, therefore trivial. */ 4538 saw_non_deleted = true; 4539 break; 4540 } 4541 } 4542 else if (move_fn_p (fn)) 4543 if (!DECL_DELETED_FN (fn)) 4544 saw_non_deleted_move = true; 4545 } 4546 4547 gcc_assert (saw_copy); 4548 4549 /* ABI v12 buggily ignored move constructors. */ 4550 bool v11nontriv = false; 4551 bool v12nontriv = !saw_non_deleted; 4552 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move; 4553 bool nontriv = (abi_version_at_least (13) ? v13nontriv 4554 : flag_abi_version == 12 ? v12nontriv 4555 : v11nontriv); 4556 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv 4557 : warn_abi_version == 12 ? v12nontriv 4558 : v11nontriv); 4559 if (nontriv != warn_nontriv) 4560 remember_deleted_copy (t); 4561 4562 return nontriv; 4563 } 4564 else 4565 return 0; 4566 } 4567 4568 /* Returns 1 iff type T is a trivially copyable type, as defined in 4569 [basic.types] and [class]. */ 4570 4571 bool 4572 trivially_copyable_p (const_tree t) 4573 { 4574 t = strip_array_types (CONST_CAST_TREE (t)); 4575 4576 if (CLASS_TYPE_P (t)) 4577 return ((!TYPE_HAS_COPY_CTOR (t) 4578 || !TYPE_HAS_COMPLEX_COPY_CTOR (t)) 4579 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t) 4580 && (!TYPE_HAS_COPY_ASSIGN (t) 4581 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t)) 4582 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) 4583 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t)); 4584 else 4585 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */ 4586 return scalarish_type_p (t); 4587 } 4588 4589 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and 4590 [class]. */ 4591 4592 bool 4593 trivial_type_p (const_tree t) 4594 { 4595 t = strip_array_types (CONST_CAST_TREE (t)); 4596 4597 if (CLASS_TYPE_P (t)) 4598 return (TYPE_HAS_TRIVIAL_DFLT (t) 4599 && trivially_copyable_p (t)); 4600 else 4601 return scalarish_type_p (t); 4602 } 4603 4604 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */ 4605 4606 bool 4607 pod_type_p (const_tree t) 4608 { 4609 /* This CONST_CAST is okay because strip_array_types returns its 4610 argument unmodified and we assign it to a const_tree. */ 4611 t = strip_array_types (CONST_CAST_TREE(t)); 4612 4613 if (!CLASS_TYPE_P (t)) 4614 return scalarish_type_p (t); 4615 else if (cxx_dialect > cxx98) 4616 /* [class]/10: A POD struct is a class that is both a trivial class and a 4617 standard-layout class, and has no non-static data members of type 4618 non-POD struct, non-POD union (or array of such types). 4619 4620 We don't need to check individual members because if a member is 4621 non-std-layout or non-trivial, the class will be too. */ 4622 return (std_layout_type_p (t) && trivial_type_p (t)); 4623 else 4624 /* The C++98 definition of POD is different. */ 4625 return !CLASSTYPE_NON_LAYOUT_POD_P (t); 4626 } 4627 4628 /* Returns true iff T is POD for the purpose of layout, as defined in the 4629 C++ ABI. */ 4630 4631 bool 4632 layout_pod_type_p (const_tree t) 4633 { 4634 t = strip_array_types (CONST_CAST_TREE (t)); 4635 4636 if (CLASS_TYPE_P (t)) 4637 return !CLASSTYPE_NON_LAYOUT_POD_P (t); 4638 else 4639 return scalarish_type_p (t); 4640 } 4641 4642 /* Returns true iff T is a standard-layout type, as defined in 4643 [basic.types]. */ 4644 4645 bool 4646 std_layout_type_p (const_tree t) 4647 { 4648 t = strip_array_types (CONST_CAST_TREE (t)); 4649 4650 if (CLASS_TYPE_P (t)) 4651 return !CLASSTYPE_NON_STD_LAYOUT (t); 4652 else 4653 return scalarish_type_p (t); 4654 } 4655 4656 static bool record_has_unique_obj_representations (const_tree, const_tree); 4657 4658 /* Returns true iff T satisfies std::has_unique_object_representations<T>, 4659 as defined in [meta.unary.prop]. */ 4660 4661 bool 4662 type_has_unique_obj_representations (const_tree t) 4663 { 4664 bool ret; 4665 4666 t = strip_array_types (CONST_CAST_TREE (t)); 4667 4668 if (!trivially_copyable_p (t)) 4669 return false; 4670 4671 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t)) 4672 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t); 4673 4674 switch (TREE_CODE (t)) 4675 { 4676 case INTEGER_TYPE: 4677 case POINTER_TYPE: 4678 case REFERENCE_TYPE: 4679 /* If some backend has any paddings in these types, we should add 4680 a target hook for this and handle it there. */ 4681 return true; 4682 4683 case BOOLEAN_TYPE: 4684 /* For bool values other than 0 and 1 should only appear with 4685 undefined behavior. */ 4686 return true; 4687 4688 case ENUMERAL_TYPE: 4689 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t)); 4690 4691 case REAL_TYPE: 4692 /* XFmode certainly contains padding on x86, which the CPU doesn't store 4693 when storing long double values, so for that we have to return false. 4694 Other kinds of floating point values are questionable due to +.0/-.0 4695 and NaNs, let's play safe for now. */ 4696 return false; 4697 4698 case FIXED_POINT_TYPE: 4699 return false; 4700 4701 case OFFSET_TYPE: 4702 return true; 4703 4704 case COMPLEX_TYPE: 4705 case VECTOR_TYPE: 4706 return type_has_unique_obj_representations (TREE_TYPE (t)); 4707 4708 case RECORD_TYPE: 4709 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t)); 4710 if (CLASS_TYPE_P (t)) 4711 { 4712 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1; 4713 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret; 4714 } 4715 return ret; 4716 4717 case UNION_TYPE: 4718 ret = true; 4719 bool any_fields; 4720 any_fields = false; 4721 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 4722 if (TREE_CODE (field) == FIELD_DECL) 4723 { 4724 any_fields = true; 4725 if (!type_has_unique_obj_representations (TREE_TYPE (field)) 4726 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1) 4727 { 4728 ret = false; 4729 break; 4730 } 4731 } 4732 if (!any_fields && !integer_zerop (TYPE_SIZE (t))) 4733 ret = false; 4734 if (CLASS_TYPE_P (t)) 4735 { 4736 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1; 4737 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret; 4738 } 4739 return ret; 4740 4741 case NULLPTR_TYPE: 4742 return false; 4743 4744 case ERROR_MARK: 4745 return false; 4746 4747 default: 4748 gcc_unreachable (); 4749 } 4750 } 4751 4752 /* Helper function for type_has_unique_obj_representations. */ 4753 4754 static bool 4755 record_has_unique_obj_representations (const_tree t, const_tree sz) 4756 { 4757 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 4758 if (TREE_CODE (field) != FIELD_DECL) 4759 ; 4760 /* For bases, can't use type_has_unique_obj_representations here, as in 4761 struct S { int i : 24; S (); }; 4762 struct T : public S { int j : 8; T (); }; 4763 S doesn't have unique obj representations, but T does. */ 4764 else if (DECL_FIELD_IS_BASE (field)) 4765 { 4766 if (!record_has_unique_obj_representations (TREE_TYPE (field), 4767 DECL_SIZE (field))) 4768 return false; 4769 } 4770 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field)) 4771 { 4772 tree btype = DECL_BIT_FIELD_TYPE (field); 4773 if (!type_has_unique_obj_representations (btype)) 4774 return false; 4775 } 4776 else if (!type_has_unique_obj_representations (TREE_TYPE (field))) 4777 return false; 4778 4779 offset_int cur = 0; 4780 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 4781 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field)) 4782 { 4783 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field)); 4784 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field)); 4785 fld = fld * BITS_PER_UNIT + bitpos; 4786 if (cur != fld) 4787 return false; 4788 if (DECL_SIZE (field)) 4789 { 4790 offset_int size = wi::to_offset (DECL_SIZE (field)); 4791 cur += size; 4792 } 4793 } 4794 if (cur != wi::to_offset (sz)) 4795 return false; 4796 4797 return true; 4798 } 4799 4800 /* Nonzero iff type T is a class template implicit specialization. */ 4801 4802 bool 4803 class_tmpl_impl_spec_p (const_tree t) 4804 { 4805 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t); 4806 } 4807 4808 /* Returns 1 iff zero initialization of type T means actually storing 4809 zeros in it. */ 4810 4811 int 4812 zero_init_p (const_tree t) 4813 { 4814 /* This CONST_CAST is okay because strip_array_types returns its 4815 argument unmodified and we assign it to a const_tree. */ 4816 t = strip_array_types (CONST_CAST_TREE(t)); 4817 4818 if (t == error_mark_node) 4819 return 1; 4820 4821 /* NULL pointers to data members are initialized with -1. */ 4822 if (TYPE_PTRDATAMEM_P (t)) 4823 return 0; 4824 4825 /* Classes that contain types that can't be zero-initialized, cannot 4826 be zero-initialized themselves. */ 4827 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t)) 4828 return 0; 4829 4830 return 1; 4831 } 4832 4833 /* Returns true if the expression or initializer T is the result of 4834 zero-initialization for its type, taking pointers to members 4835 into consideration. */ 4836 4837 bool 4838 zero_init_expr_p (tree t) 4839 { 4840 tree type = TREE_TYPE (t); 4841 if (!type || uses_template_parms (type)) 4842 return false; 4843 if (TYPE_PTRMEM_P (type)) 4844 return null_member_pointer_value_p (t); 4845 if (TREE_CODE (t) == CONSTRUCTOR) 4846 { 4847 if (COMPOUND_LITERAL_P (t) 4848 || BRACE_ENCLOSED_INITIALIZER_P (t)) 4849 /* Undigested, conversions might change the zeroness. */ 4850 return false; 4851 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t)) 4852 { 4853 if (TREE_CODE (type) == UNION_TYPE 4854 && elt.index != first_field (type)) 4855 return false; 4856 if (!zero_init_expr_p (elt.value)) 4857 return false; 4858 } 4859 return true; 4860 } 4861 if (zero_init_p (type)) 4862 return initializer_zerop (t); 4863 return false; 4864 } 4865 4866 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a 4867 non-type template parameter. If EXPLAIN, explain why not. */ 4868 4869 bool 4870 structural_type_p (tree t, bool explain) 4871 { 4872 /* A structural type is one of the following: */ 4873 4874 /* a scalar type, or */ 4875 if (SCALAR_TYPE_P (t)) 4876 return true; 4877 /* an lvalue reference type, or */ 4878 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t)) 4879 return true; 4880 /* a literal class type with the following properties: 4881 - all base classes and non-static data members are public and non-mutable 4882 and 4883 - the types of all bases classes and non-static data members are 4884 structural types or (possibly multi-dimensional) array thereof. */ 4885 if (!CLASS_TYPE_P (t)) 4886 return false; 4887 if (!literal_type_p (t)) 4888 { 4889 if (explain) 4890 explain_non_literal_class (t); 4891 return false; 4892 } 4893 for (tree m = next_initializable_field (TYPE_FIELDS (t)); m; 4894 m = next_initializable_field (DECL_CHAIN (m))) 4895 { 4896 if (TREE_PRIVATE (m) || TREE_PROTECTED (m)) 4897 { 4898 if (explain) 4899 { 4900 if (DECL_FIELD_IS_BASE (m)) 4901 inform (location_of (m), "base class %qT is not public", 4902 TREE_TYPE (m)); 4903 else 4904 inform (location_of (m), "%qD is not public", m); 4905 } 4906 return false; 4907 } 4908 if (DECL_MUTABLE_P (m)) 4909 { 4910 if (explain) 4911 inform (location_of (m), "%qD is mutable", m); 4912 return false; 4913 } 4914 tree mtype = strip_array_types (TREE_TYPE (m)); 4915 if (!structural_type_p (mtype)) 4916 { 4917 if (explain) 4918 { 4919 inform (location_of (m), "%qD has a non-structural type", m); 4920 structural_type_p (mtype, true); 4921 } 4922 return false; 4923 } 4924 } 4925 return true; 4926 } 4927 4928 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU 4929 warn_unused_result attribute. */ 4930 4931 static tree 4932 handle_nodiscard_attribute (tree *node, tree name, tree args, 4933 int /*flags*/, bool *no_add_attrs) 4934 { 4935 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST) 4936 { 4937 error ("%qE attribute argument must be a string constant", name); 4938 *no_add_attrs = true; 4939 } 4940 if (TREE_CODE (*node) == FUNCTION_DECL) 4941 { 4942 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))) 4943 && !DECL_CONSTRUCTOR_P (*node)) 4944 warning_at (DECL_SOURCE_LOCATION (*node), 4945 OPT_Wattributes, "%qE attribute applied to %qD with void " 4946 "return type", name, *node); 4947 } 4948 else if (OVERLOAD_TYPE_P (*node)) 4949 /* OK */; 4950 else 4951 { 4952 warning (OPT_Wattributes, "%qE attribute can only be applied to " 4953 "functions or to class or enumeration types", name); 4954 *no_add_attrs = true; 4955 } 4956 return NULL_TREE; 4957 } 4958 4959 /* Handle a C++20 "no_unique_address" attribute; arguments as in 4960 struct attribute_spec.handler. */ 4961 static tree 4962 handle_no_unique_addr_attribute (tree* node, 4963 tree name, 4964 tree /*args*/, 4965 int /*flags*/, 4966 bool* no_add_attrs) 4967 { 4968 if (TREE_CODE (*node) != FIELD_DECL) 4969 { 4970 warning (OPT_Wattributes, "%qE attribute can only be applied to " 4971 "non-static data members", name); 4972 *no_add_attrs = true; 4973 } 4974 else if (DECL_C_BIT_FIELD (*node)) 4975 { 4976 warning (OPT_Wattributes, "%qE attribute cannot be applied to " 4977 "a bit-field", name); 4978 *no_add_attrs = true; 4979 } 4980 4981 return NULL_TREE; 4982 } 4983 4984 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU 4985 hot/cold attributes. */ 4986 4987 static tree 4988 handle_likeliness_attribute (tree *node, tree name, tree args, 4989 int flags, bool *no_add_attrs) 4990 { 4991 *no_add_attrs = true; 4992 if (TREE_CODE (*node) == LABEL_DECL 4993 || TREE_CODE (*node) == FUNCTION_DECL) 4994 { 4995 if (args) 4996 warning (OPT_Wattributes, "%qE attribute takes no arguments", name); 4997 tree bname = (is_attribute_p ("likely", name) 4998 ? get_identifier ("hot") : get_identifier ("cold")); 4999 if (TREE_CODE (*node) == FUNCTION_DECL) 5000 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to " 5001 "functions; treating as %<[[gnu::%E]]%>", name, bname); 5002 tree battr = build_tree_list (bname, NULL_TREE); 5003 decl_attributes (node, battr, flags); 5004 return NULL_TREE; 5005 } 5006 else 5007 return error_mark_node; 5008 } 5009 5010 /* Table of valid C++ attributes. */ 5011 const struct attribute_spec cxx_attribute_table[] = 5012 { 5013 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, 5014 affects_type_identity, handler, exclude } */ 5015 { "init_priority", 1, 1, true, false, false, false, 5016 handle_init_priority_attribute, NULL }, 5017 { "abi_tag", 1, -1, false, false, false, true, 5018 handle_abi_tag_attribute, NULL }, 5019 { NULL, 0, 0, false, false, false, false, NULL, NULL } 5020 }; 5021 5022 /* Table of C++ standard attributes. */ 5023 const struct attribute_spec std_attribute_table[] = 5024 { 5025 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, 5026 affects_type_identity, handler, exclude } */ 5027 { "maybe_unused", 0, 0, false, false, false, false, 5028 handle_unused_attribute, NULL }, 5029 { "nodiscard", 0, 1, false, false, false, false, 5030 handle_nodiscard_attribute, NULL }, 5031 { "no_unique_address", 0, 0, true, false, false, false, 5032 handle_no_unique_addr_attribute, NULL }, 5033 { "likely", 0, 0, false, false, false, false, 5034 handle_likeliness_attribute, attr_cold_hot_exclusions }, 5035 { "unlikely", 0, 0, false, false, false, false, 5036 handle_likeliness_attribute, attr_cold_hot_exclusions }, 5037 { "noreturn", 0, 0, true, false, false, false, 5038 handle_noreturn_attribute, attr_noreturn_exclusions }, 5039 { NULL, 0, 0, false, false, false, false, NULL, NULL } 5040 }; 5041 5042 /* Handle an "init_priority" attribute; arguments as in 5043 struct attribute_spec.handler. */ 5044 static tree 5045 handle_init_priority_attribute (tree* node, 5046 tree name, 5047 tree args, 5048 int /*flags*/, 5049 bool* no_add_attrs) 5050 { 5051 tree initp_expr = TREE_VALUE (args); 5052 tree decl = *node; 5053 tree type = TREE_TYPE (decl); 5054 int pri; 5055 5056 STRIP_NOPS (initp_expr); 5057 initp_expr = default_conversion (initp_expr); 5058 if (initp_expr) 5059 initp_expr = maybe_constant_value (initp_expr); 5060 5061 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST) 5062 { 5063 error ("requested %<init_priority%> is not an integer constant"); 5064 cxx_constant_value (initp_expr); 5065 *no_add_attrs = true; 5066 return NULL_TREE; 5067 } 5068 5069 pri = TREE_INT_CST_LOW (initp_expr); 5070 5071 type = strip_array_types (type); 5072 5073 if (decl == NULL_TREE 5074 || !VAR_P (decl) 5075 || !TREE_STATIC (decl) 5076 || DECL_EXTERNAL (decl) 5077 || (TREE_CODE (type) != RECORD_TYPE 5078 && TREE_CODE (type) != UNION_TYPE) 5079 /* Static objects in functions are initialized the 5080 first time control passes through that 5081 function. This is not precise enough to pin down an 5082 init_priority value, so don't allow it. */ 5083 || current_function_decl) 5084 { 5085 error ("can only use %qE attribute on file-scope definitions " 5086 "of objects of class type", name); 5087 *no_add_attrs = true; 5088 return NULL_TREE; 5089 } 5090 5091 if (pri > MAX_INIT_PRIORITY || pri <= 0) 5092 { 5093 error ("requested %<init_priority%> %i is out of range [0, %i]", 5094 pri, MAX_INIT_PRIORITY); 5095 *no_add_attrs = true; 5096 return NULL_TREE; 5097 } 5098 5099 /* Check for init_priorities that are reserved for 5100 language and runtime support implementations.*/ 5101 if (pri <= MAX_RESERVED_INIT_PRIORITY) 5102 { 5103 warning 5104 (0, "requested %<init_priority%> %i is reserved for internal use", 5105 pri); 5106 } 5107 5108 if (SUPPORTS_INIT_PRIORITY) 5109 { 5110 SET_DECL_INIT_PRIORITY (decl, pri); 5111 DECL_HAS_INIT_PRIORITY_P (decl) = 1; 5112 return NULL_TREE; 5113 } 5114 else 5115 { 5116 error ("%qE attribute is not supported on this platform", name); 5117 *no_add_attrs = true; 5118 return NULL_TREE; 5119 } 5120 } 5121 5122 /* DECL is being redeclared; the old declaration had the abi tags in OLD, 5123 and the new one has the tags in NEW_. Give an error if there are tags 5124 in NEW_ that weren't in OLD. */ 5125 5126 bool 5127 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_) 5128 { 5129 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST) 5130 old = TREE_VALUE (old); 5131 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST) 5132 new_ = TREE_VALUE (new_); 5133 bool err = false; 5134 for (const_tree t = new_; t; t = TREE_CHAIN (t)) 5135 { 5136 tree str = TREE_VALUE (t); 5137 for (const_tree in = old; in; in = TREE_CHAIN (in)) 5138 { 5139 tree ostr = TREE_VALUE (in); 5140 if (cp_tree_equal (str, ostr)) 5141 goto found; 5142 } 5143 error ("redeclaration of %qD adds abi tag %qE", decl, str); 5144 err = true; 5145 found:; 5146 } 5147 if (err) 5148 { 5149 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here"); 5150 return false; 5151 } 5152 return true; 5153 } 5154 5155 /* The abi_tag attribute with the name NAME was given ARGS. If they are 5156 ill-formed, give an error and return false; otherwise, return true. */ 5157 5158 bool 5159 check_abi_tag_args (tree args, tree name) 5160 { 5161 if (!args) 5162 { 5163 error ("the %qE attribute requires arguments", name); 5164 return false; 5165 } 5166 for (tree arg = args; arg; arg = TREE_CHAIN (arg)) 5167 { 5168 tree elt = TREE_VALUE (arg); 5169 if (TREE_CODE (elt) != STRING_CST 5170 || (!same_type_ignoring_top_level_qualifiers_p 5171 (strip_array_types (TREE_TYPE (elt)), 5172 char_type_node))) 5173 { 5174 error ("arguments to the %qE attribute must be narrow string " 5175 "literals", name); 5176 return false; 5177 } 5178 const char *begin = TREE_STRING_POINTER (elt); 5179 const char *end = begin + TREE_STRING_LENGTH (elt); 5180 for (const char *p = begin; p != end; ++p) 5181 { 5182 char c = *p; 5183 if (p == begin) 5184 { 5185 if (!ISALPHA (c) && c != '_') 5186 { 5187 error ("arguments to the %qE attribute must contain valid " 5188 "identifiers", name); 5189 inform (input_location, "%<%c%> is not a valid first " 5190 "character for an identifier", c); 5191 return false; 5192 } 5193 } 5194 else if (p == end - 1) 5195 gcc_assert (c == 0); 5196 else 5197 { 5198 if (!ISALNUM (c) && c != '_') 5199 { 5200 error ("arguments to the %qE attribute must contain valid " 5201 "identifiers", name); 5202 inform (input_location, "%<%c%> is not a valid character " 5203 "in an identifier", c); 5204 return false; 5205 } 5206 } 5207 } 5208 } 5209 return true; 5210 } 5211 5212 /* Handle an "abi_tag" attribute; arguments as in 5213 struct attribute_spec.handler. */ 5214 5215 static tree 5216 handle_abi_tag_attribute (tree* node, tree name, tree args, 5217 int flags, bool* no_add_attrs) 5218 { 5219 if (!check_abi_tag_args (args, name)) 5220 goto fail; 5221 5222 if (TYPE_P (*node)) 5223 { 5224 if (!OVERLOAD_TYPE_P (*node)) 5225 { 5226 error ("%qE attribute applied to non-class, non-enum type %qT", 5227 name, *node); 5228 goto fail; 5229 } 5230 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE)) 5231 { 5232 error ("%qE attribute applied to %qT after its definition", 5233 name, *node); 5234 goto fail; 5235 } 5236 else if (CLASS_TYPE_P (*node) 5237 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node)) 5238 { 5239 warning (OPT_Wattributes, "ignoring %qE attribute applied to " 5240 "template instantiation %qT", name, *node); 5241 goto fail; 5242 } 5243 else if (CLASS_TYPE_P (*node) 5244 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node)) 5245 { 5246 warning (OPT_Wattributes, "ignoring %qE attribute applied to " 5247 "template specialization %qT", name, *node); 5248 goto fail; 5249 } 5250 5251 tree attributes = TYPE_ATTRIBUTES (*node); 5252 tree decl = TYPE_NAME (*node); 5253 5254 /* Make sure all declarations have the same abi tags. */ 5255 if (DECL_SOURCE_LOCATION (decl) != input_location) 5256 { 5257 if (!check_abi_tag_redeclaration (decl, 5258 lookup_attribute ("abi_tag", 5259 attributes), 5260 args)) 5261 goto fail; 5262 } 5263 } 5264 else 5265 { 5266 if (!VAR_OR_FUNCTION_DECL_P (*node)) 5267 { 5268 error ("%qE attribute applied to non-function, non-variable %qD", 5269 name, *node); 5270 goto fail; 5271 } 5272 else if (DECL_LANGUAGE (*node) == lang_c) 5273 { 5274 error ("%qE attribute applied to extern \"C\" declaration %qD", 5275 name, *node); 5276 goto fail; 5277 } 5278 } 5279 5280 return NULL_TREE; 5281 5282 fail: 5283 *no_add_attrs = true; 5284 return NULL_TREE; 5285 } 5286 5287 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the 5288 thing pointed to by the constant. */ 5289 5290 tree 5291 make_ptrmem_cst (tree type, tree member) 5292 { 5293 tree ptrmem_cst = make_node (PTRMEM_CST); 5294 TREE_TYPE (ptrmem_cst) = type; 5295 PTRMEM_CST_MEMBER (ptrmem_cst) = member; 5296 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location; 5297 return ptrmem_cst; 5298 } 5299 5300 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May 5301 return an existing type if an appropriate type already exists. */ 5302 5303 tree 5304 cp_build_type_attribute_variant (tree type, tree attributes) 5305 { 5306 tree new_type; 5307 5308 new_type = build_type_attribute_variant (type, attributes); 5309 if (FUNC_OR_METHOD_TYPE_P (new_type)) 5310 gcc_checking_assert (cxx_type_hash_eq (type, new_type)); 5311 5312 /* Making a new main variant of a class type is broken. */ 5313 gcc_assert (!CLASS_TYPE_P (type) || new_type == type); 5314 5315 return new_type; 5316 } 5317 5318 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes. 5319 Called only after doing all language independent checks. */ 5320 5321 bool 5322 cxx_type_hash_eq (const_tree typea, const_tree typeb) 5323 { 5324 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea)); 5325 5326 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb)) 5327 return false; 5328 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb)) 5329 return false; 5330 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea), 5331 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact); 5332 } 5333 5334 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For 5335 C++, these are the exception-specifier and ref-qualifier. */ 5336 5337 tree 5338 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb) 5339 { 5340 tree type = CONST_CAST_TREE (typea); 5341 if (FUNC_OR_METHOD_TYPE_P (type)) 5342 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb), 5343 TYPE_RAISES_EXCEPTIONS (typeb), 5344 TYPE_HAS_LATE_RETURN_TYPE (typeb)); 5345 return type; 5346 } 5347 5348 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order 5349 traversal. Called from walk_tree. */ 5350 5351 tree 5352 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func, 5353 void *data, hash_set<tree> *pset) 5354 { 5355 enum tree_code code = TREE_CODE (*tp); 5356 tree result; 5357 5358 #define WALK_SUBTREE(NODE) \ 5359 do \ 5360 { \ 5361 result = cp_walk_tree (&(NODE), func, data, pset); \ 5362 if (result) goto out; \ 5363 } \ 5364 while (0) 5365 5366 if (TYPE_P (*tp)) 5367 { 5368 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of 5369 the argument, so don't look through typedefs, but do walk into 5370 template arguments for alias templates (and non-typedefed classes). 5371 5372 If *WALK_SUBTREES_P > 1, we're interested in type identity or 5373 equivalence, so look through typedefs, ignoring template arguments for 5374 alias templates, and walk into template args of classes. 5375 5376 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2 5377 when that's the behavior the walk_tree_fn wants. */ 5378 if (*walk_subtrees_p == 1 && typedef_variant_p (*tp)) 5379 { 5380 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (*tp)) 5381 WALK_SUBTREE (TI_ARGS (ti)); 5382 *walk_subtrees_p = 0; 5383 return NULL_TREE; 5384 } 5385 5386 if (tree ti = TYPE_TEMPLATE_INFO (*tp)) 5387 WALK_SUBTREE (TI_ARGS (ti)); 5388 } 5389 5390 /* Not one of the easy cases. We must explicitly go through the 5391 children. */ 5392 result = NULL_TREE; 5393 switch (code) 5394 { 5395 case TEMPLATE_TYPE_PARM: 5396 if (template_placeholder_p (*tp)) 5397 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (*tp)); 5398 /* Fall through. */ 5399 case DEFERRED_PARSE: 5400 case TEMPLATE_TEMPLATE_PARM: 5401 case BOUND_TEMPLATE_TEMPLATE_PARM: 5402 case UNBOUND_CLASS_TEMPLATE: 5403 case TEMPLATE_PARM_INDEX: 5404 case TYPEOF_TYPE: 5405 case UNDERLYING_TYPE: 5406 /* None of these have subtrees other than those already walked 5407 above. */ 5408 *walk_subtrees_p = 0; 5409 break; 5410 5411 case TYPENAME_TYPE: 5412 WALK_SUBTREE (TYPE_CONTEXT (*tp)); 5413 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (*tp)); 5414 *walk_subtrees_p = 0; 5415 break; 5416 5417 case BASELINK: 5418 if (BASELINK_QUALIFIED_P (*tp)) 5419 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp))); 5420 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp)); 5421 *walk_subtrees_p = 0; 5422 break; 5423 5424 case PTRMEM_CST: 5425 WALK_SUBTREE (TREE_TYPE (*tp)); 5426 *walk_subtrees_p = 0; 5427 break; 5428 5429 case TREE_LIST: 5430 WALK_SUBTREE (TREE_PURPOSE (*tp)); 5431 break; 5432 5433 case OVERLOAD: 5434 WALK_SUBTREE (OVL_FUNCTION (*tp)); 5435 WALK_SUBTREE (OVL_CHAIN (*tp)); 5436 *walk_subtrees_p = 0; 5437 break; 5438 5439 case USING_DECL: 5440 WALK_SUBTREE (DECL_NAME (*tp)); 5441 WALK_SUBTREE (USING_DECL_SCOPE (*tp)); 5442 WALK_SUBTREE (USING_DECL_DECLS (*tp)); 5443 *walk_subtrees_p = 0; 5444 break; 5445 5446 case RECORD_TYPE: 5447 if (TYPE_PTRMEMFUNC_P (*tp)) 5448 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp)); 5449 break; 5450 5451 case TYPE_ARGUMENT_PACK: 5452 case NONTYPE_ARGUMENT_PACK: 5453 { 5454 tree args = ARGUMENT_PACK_ARGS (*tp); 5455 int i, len = TREE_VEC_LENGTH (args); 5456 for (i = 0; i < len; i++) 5457 WALK_SUBTREE (TREE_VEC_ELT (args, i)); 5458 } 5459 break; 5460 5461 case TYPE_PACK_EXPANSION: 5462 WALK_SUBTREE (TREE_TYPE (*tp)); 5463 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp)); 5464 *walk_subtrees_p = 0; 5465 break; 5466 5467 case EXPR_PACK_EXPANSION: 5468 WALK_SUBTREE (TREE_OPERAND (*tp, 0)); 5469 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp)); 5470 *walk_subtrees_p = 0; 5471 break; 5472 5473 case CAST_EXPR: 5474 case REINTERPRET_CAST_EXPR: 5475 case STATIC_CAST_EXPR: 5476 case CONST_CAST_EXPR: 5477 case DYNAMIC_CAST_EXPR: 5478 case IMPLICIT_CONV_EXPR: 5479 case BIT_CAST_EXPR: 5480 if (TREE_TYPE (*tp)) 5481 WALK_SUBTREE (TREE_TYPE (*tp)); 5482 break; 5483 5484 case CONSTRUCTOR: 5485 if (COMPOUND_LITERAL_P (*tp)) 5486 WALK_SUBTREE (TREE_TYPE (*tp)); 5487 break; 5488 5489 case TRAIT_EXPR: 5490 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp)); 5491 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp)); 5492 *walk_subtrees_p = 0; 5493 break; 5494 5495 case DECLTYPE_TYPE: 5496 ++cp_unevaluated_operand; 5497 /* We can't use WALK_SUBTREE here because of the goto. */ 5498 result = cp_walk_tree (&DECLTYPE_TYPE_EXPR (*tp), func, data, pset); 5499 --cp_unevaluated_operand; 5500 *walk_subtrees_p = 0; 5501 break; 5502 5503 case ALIGNOF_EXPR: 5504 case SIZEOF_EXPR: 5505 case NOEXCEPT_EXPR: 5506 ++cp_unevaluated_operand; 5507 result = cp_walk_tree (&TREE_OPERAND (*tp, 0), func, data, pset); 5508 --cp_unevaluated_operand; 5509 *walk_subtrees_p = 0; 5510 break; 5511 5512 case REQUIRES_EXPR: 5513 { 5514 cp_unevaluated u; 5515 for (tree parm = REQUIRES_EXPR_PARMS (*tp); parm; parm = DECL_CHAIN (parm)) 5516 /* Walk the types of each parameter, but not the parameter itself, 5517 since doing so would cause false positives in the unexpanded pack 5518 checker if the requires-expr introduces a function parameter pack, 5519 e.g. requires (Ts... ts) { }. */ 5520 WALK_SUBTREE (TREE_TYPE (parm)); 5521 WALK_SUBTREE (REQUIRES_EXPR_REQS (*tp)); 5522 *walk_subtrees_p = 0; 5523 break; 5524 } 5525 5526 case DECL_EXPR: 5527 /* User variables should be mentioned in BIND_EXPR_VARS 5528 and their initializers and sizes walked when walking 5529 the containing BIND_EXPR. Compiler temporaries are 5530 handled here. And also normal variables in templates, 5531 since do_poplevel doesn't build a BIND_EXPR then. */ 5532 if (VAR_P (TREE_OPERAND (*tp, 0)) 5533 && (processing_template_decl 5534 || (DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0)) 5535 && !TREE_STATIC (TREE_OPERAND (*tp, 0))))) 5536 { 5537 tree decl = TREE_OPERAND (*tp, 0); 5538 WALK_SUBTREE (DECL_INITIAL (decl)); 5539 WALK_SUBTREE (DECL_SIZE (decl)); 5540 WALK_SUBTREE (DECL_SIZE_UNIT (decl)); 5541 } 5542 break; 5543 5544 case LAMBDA_EXPR: 5545 /* Don't walk into the body of the lambda, but the capture initializers 5546 are part of the enclosing context. */ 5547 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (*tp); cap; 5548 cap = TREE_CHAIN (cap)) 5549 WALK_SUBTREE (TREE_VALUE (cap)); 5550 break; 5551 5552 case CO_YIELD_EXPR: 5553 if (TREE_OPERAND (*tp, 1)) 5554 /* Operand 1 is the tree for the relevant co_await which has any 5555 interesting sub-trees. */ 5556 WALK_SUBTREE (TREE_OPERAND (*tp, 1)); 5557 break; 5558 5559 case CO_AWAIT_EXPR: 5560 if (TREE_OPERAND (*tp, 1)) 5561 /* Operand 1 is frame variable. */ 5562 WALK_SUBTREE (TREE_OPERAND (*tp, 1)); 5563 if (TREE_OPERAND (*tp, 2)) 5564 /* Operand 2 has the initialiser, and we need to walk any subtrees 5565 there. */ 5566 WALK_SUBTREE (TREE_OPERAND (*tp, 2)); 5567 break; 5568 5569 case CO_RETURN_EXPR: 5570 if (TREE_OPERAND (*tp, 0)) 5571 { 5572 if (VOID_TYPE_P (TREE_OPERAND (*tp, 0))) 5573 /* For void expressions, operand 1 is a trivial call, and any 5574 interesting subtrees will be part of operand 0. */ 5575 WALK_SUBTREE (TREE_OPERAND (*tp, 0)); 5576 else if (TREE_OPERAND (*tp, 1)) 5577 /* Interesting sub-trees will be in the return_value () call 5578 arguments. */ 5579 WALK_SUBTREE (TREE_OPERAND (*tp, 1)); 5580 } 5581 break; 5582 5583 case STATIC_ASSERT: 5584 WALK_SUBTREE (STATIC_ASSERT_CONDITION (*tp)); 5585 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (*tp)); 5586 break; 5587 5588 default: 5589 return NULL_TREE; 5590 } 5591 5592 /* We didn't find what we were looking for. */ 5593 out: 5594 return result; 5595 5596 #undef WALK_SUBTREE 5597 } 5598 5599 /* Like save_expr, but for C++. */ 5600 5601 tree 5602 cp_save_expr (tree expr) 5603 { 5604 /* There is no reason to create a SAVE_EXPR within a template; if 5605 needed, we can create the SAVE_EXPR when instantiating the 5606 template. Furthermore, the middle-end cannot handle C++-specific 5607 tree codes. */ 5608 if (processing_template_decl) 5609 return expr; 5610 5611 /* TARGET_EXPRs are only expanded once. */ 5612 if (TREE_CODE (expr) == TARGET_EXPR) 5613 return expr; 5614 5615 return save_expr (expr); 5616 } 5617 5618 /* Initialize tree.cc. */ 5619 5620 void 5621 init_tree (void) 5622 { 5623 list_hash_table = hash_table<list_hasher>::create_ggc (61); 5624 register_scoped_attributes (std_attribute_table, NULL); 5625 } 5626 5627 /* Returns the kind of special function that DECL (a FUNCTION_DECL) 5628 is. Note that sfk_none is zero, so this function can be used as a 5629 predicate to test whether or not DECL is a special function. */ 5630 5631 special_function_kind 5632 special_function_p (const_tree decl) 5633 { 5634 /* Rather than doing all this stuff with magic names, we should 5635 probably have a field of type `special_function_kind' in 5636 DECL_LANG_SPECIFIC. */ 5637 if (DECL_INHERITED_CTOR (decl)) 5638 return sfk_inheriting_constructor; 5639 if (DECL_COPY_CONSTRUCTOR_P (decl)) 5640 return sfk_copy_constructor; 5641 if (DECL_MOVE_CONSTRUCTOR_P (decl)) 5642 return sfk_move_constructor; 5643 if (DECL_CONSTRUCTOR_P (decl)) 5644 return sfk_constructor; 5645 if (DECL_ASSIGNMENT_OPERATOR_P (decl) 5646 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR)) 5647 { 5648 if (copy_fn_p (decl)) 5649 return sfk_copy_assignment; 5650 if (move_fn_p (decl)) 5651 return sfk_move_assignment; 5652 } 5653 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)) 5654 return sfk_destructor; 5655 if (DECL_COMPLETE_DESTRUCTOR_P (decl)) 5656 return sfk_complete_destructor; 5657 if (DECL_BASE_DESTRUCTOR_P (decl)) 5658 return sfk_base_destructor; 5659 if (DECL_DELETING_DESTRUCTOR_P (decl)) 5660 return sfk_deleting_destructor; 5661 if (DECL_CONV_FN_P (decl)) 5662 return sfk_conversion; 5663 if (deduction_guide_p (decl)) 5664 return sfk_deduction_guide; 5665 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR 5666 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR) 5667 return sfk_comparison; 5668 5669 return sfk_none; 5670 } 5671 5672 /* As above, but only if DECL is a special member function as per 11.3.3 5673 [special]: default/copy/move ctor, copy/move assignment, or destructor. */ 5674 5675 special_function_kind 5676 special_memfn_p (const_tree decl) 5677 { 5678 switch (special_function_kind sfk = special_function_p (decl)) 5679 { 5680 case sfk_constructor: 5681 if (!default_ctor_p (decl)) 5682 break; 5683 gcc_fallthrough(); 5684 case sfk_copy_constructor: 5685 case sfk_copy_assignment: 5686 case sfk_move_assignment: 5687 case sfk_move_constructor: 5688 case sfk_destructor: 5689 return sfk; 5690 5691 default: 5692 break; 5693 } 5694 return sfk_none; 5695 } 5696 5697 /* Returns nonzero if TYPE is a character type, including wchar_t. */ 5698 5699 int 5700 char_type_p (tree type) 5701 { 5702 return (same_type_p (type, char_type_node) 5703 || same_type_p (type, unsigned_char_type_node) 5704 || same_type_p (type, signed_char_type_node) 5705 || same_type_p (type, char8_type_node) 5706 || same_type_p (type, char16_type_node) 5707 || same_type_p (type, char32_type_node) 5708 || same_type_p (type, wchar_type_node)); 5709 } 5710 5711 /* Returns the kind of linkage associated with the indicated DECL. Th 5712 value returned is as specified by the language standard; it is 5713 independent of implementation details regarding template 5714 instantiation, etc. For example, it is possible that a declaration 5715 to which this function assigns external linkage would not show up 5716 as a global symbol when you run `nm' on the resulting object file. */ 5717 5718 linkage_kind 5719 decl_linkage (tree decl) 5720 { 5721 /* This function doesn't attempt to calculate the linkage from first 5722 principles as given in [basic.link]. Instead, it makes use of 5723 the fact that we have already set TREE_PUBLIC appropriately, and 5724 then handles a few special cases. Ideally, we would calculate 5725 linkage first, and then transform that into a concrete 5726 implementation. */ 5727 5728 /* Things that don't have names have no linkage. */ 5729 if (!DECL_NAME (decl)) 5730 return lk_none; 5731 5732 /* Fields have no linkage. */ 5733 if (TREE_CODE (decl) == FIELD_DECL) 5734 return lk_none; 5735 5736 /* Things in local scope do not have linkage. */ 5737 if (decl_function_context (decl)) 5738 return lk_none; 5739 5740 /* Things that are TREE_PUBLIC have external linkage. */ 5741 if (TREE_PUBLIC (decl)) 5742 return lk_external; 5743 5744 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants, 5745 check one of the "clones" for the real linkage. */ 5746 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl) 5747 && DECL_CHAIN (decl) 5748 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl))) 5749 return decl_linkage (DECL_CHAIN (decl)); 5750 5751 if (TREE_CODE (decl) == NAMESPACE_DECL) 5752 return lk_external; 5753 5754 /* Linkage of a CONST_DECL depends on the linkage of the enumeration 5755 type. */ 5756 if (TREE_CODE (decl) == CONST_DECL) 5757 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl))); 5758 5759 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but 5760 are considered to have external linkage for language purposes, as do 5761 template instantiations on targets without weak symbols. DECLs really 5762 meant to have internal linkage have DECL_THIS_STATIC set. */ 5763 if (TREE_CODE (decl) == TYPE_DECL) 5764 return lk_external; 5765 if (VAR_OR_FUNCTION_DECL_P (decl)) 5766 { 5767 if (!DECL_THIS_STATIC (decl)) 5768 return lk_external; 5769 5770 /* Static data members and static member functions from classes 5771 in anonymous namespace also don't have TREE_PUBLIC set. */ 5772 if (DECL_CLASS_CONTEXT (decl)) 5773 return lk_external; 5774 } 5775 5776 /* Everything else has internal linkage. */ 5777 return lk_internal; 5778 } 5779 5780 /* Returns the storage duration of the object or reference associated with 5781 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */ 5782 5783 duration_kind 5784 decl_storage_duration (tree decl) 5785 { 5786 if (TREE_CODE (decl) == PARM_DECL) 5787 return dk_auto; 5788 if (TREE_CODE (decl) == FUNCTION_DECL) 5789 return dk_static; 5790 gcc_assert (VAR_P (decl)); 5791 if (!TREE_STATIC (decl) 5792 && !DECL_EXTERNAL (decl)) 5793 return dk_auto; 5794 if (CP_DECL_THREAD_LOCAL_P (decl)) 5795 return dk_thread; 5796 return dk_static; 5797 } 5798 5799 /* EXP is an expression that we want to pre-evaluate. Returns (in 5801 *INITP) an expression that will perform the pre-evaluation. The 5802 value returned by this function is a side-effect free expression 5803 equivalent to the pre-evaluated expression. Callers must ensure 5804 that *INITP is evaluated before EXP. */ 5805 5806 tree 5807 stabilize_expr (tree exp, tree* initp) 5808 { 5809 tree init_expr; 5810 5811 if (!TREE_SIDE_EFFECTS (exp)) 5812 init_expr = NULL_TREE; 5813 else if (VOID_TYPE_P (TREE_TYPE (exp))) 5814 { 5815 init_expr = exp; 5816 exp = void_node; 5817 } 5818 /* There are no expressions with REFERENCE_TYPE, but there can be call 5819 arguments with such a type; just treat it as a pointer. */ 5820 else if (TYPE_REF_P (TREE_TYPE (exp)) 5821 || SCALAR_TYPE_P (TREE_TYPE (exp)) 5822 || !glvalue_p (exp)) 5823 { 5824 init_expr = get_target_expr (exp); 5825 exp = TARGET_EXPR_SLOT (init_expr); 5826 if (CLASS_TYPE_P (TREE_TYPE (exp))) 5827 exp = move (exp); 5828 else 5829 exp = rvalue (exp); 5830 } 5831 else 5832 { 5833 bool xval = !lvalue_p (exp); 5834 exp = cp_build_addr_expr (exp, tf_warning_or_error); 5835 init_expr = get_target_expr (exp); 5836 exp = TARGET_EXPR_SLOT (init_expr); 5837 exp = cp_build_fold_indirect_ref (exp); 5838 if (xval) 5839 exp = move (exp); 5840 } 5841 *initp = init_expr; 5842 5843 gcc_assert (!TREE_SIDE_EFFECTS (exp)); 5844 return exp; 5845 } 5846 5847 /* Add NEW_EXPR, an expression whose value we don't care about, after the 5848 similar expression ORIG. */ 5849 5850 tree 5851 add_stmt_to_compound (tree orig, tree new_expr) 5852 { 5853 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr)) 5854 return orig; 5855 if (!orig || !TREE_SIDE_EFFECTS (orig)) 5856 return new_expr; 5857 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr); 5858 } 5859 5860 /* Like stabilize_expr, but for a call whose arguments we want to 5861 pre-evaluate. CALL is modified in place to use the pre-evaluated 5862 arguments, while, upon return, *INITP contains an expression to 5863 compute the arguments. */ 5864 5865 void 5866 stabilize_call (tree call, tree *initp) 5867 { 5868 tree inits = NULL_TREE; 5869 int i; 5870 int nargs = call_expr_nargs (call); 5871 5872 if (call == error_mark_node || processing_template_decl) 5873 { 5874 *initp = NULL_TREE; 5875 return; 5876 } 5877 5878 gcc_assert (TREE_CODE (call) == CALL_EXPR); 5879 5880 for (i = 0; i < nargs; i++) 5881 { 5882 tree init; 5883 CALL_EXPR_ARG (call, i) = 5884 stabilize_expr (CALL_EXPR_ARG (call, i), &init); 5885 inits = add_stmt_to_compound (inits, init); 5886 } 5887 5888 *initp = inits; 5889 } 5890 5891 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want 5892 to pre-evaluate. CALL is modified in place to use the pre-evaluated 5893 arguments, while, upon return, *INITP contains an expression to 5894 compute the arguments. */ 5895 5896 static void 5897 stabilize_aggr_init (tree call, tree *initp) 5898 { 5899 tree inits = NULL_TREE; 5900 int i; 5901 int nargs = aggr_init_expr_nargs (call); 5902 5903 if (call == error_mark_node) 5904 return; 5905 5906 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR); 5907 5908 for (i = 0; i < nargs; i++) 5909 { 5910 tree init; 5911 AGGR_INIT_EXPR_ARG (call, i) = 5912 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init); 5913 inits = add_stmt_to_compound (inits, init); 5914 } 5915 5916 *initp = inits; 5917 } 5918 5919 /* Like stabilize_expr, but for an initialization. 5920 5921 If the initialization is for an object of class type, this function 5922 takes care not to introduce additional temporaries. 5923 5924 Returns TRUE iff the expression was successfully pre-evaluated, 5925 i.e., if INIT is now side-effect free, except for, possibly, a 5926 single call to a constructor. */ 5927 5928 bool 5929 stabilize_init (tree init, tree *initp) 5930 { 5931 tree t = init; 5932 5933 *initp = NULL_TREE; 5934 5935 if (t == error_mark_node || processing_template_decl) 5936 return true; 5937 5938 if (TREE_CODE (t) == INIT_EXPR) 5939 t = TREE_OPERAND (t, 1); 5940 if (TREE_CODE (t) == TARGET_EXPR) 5941 t = TARGET_EXPR_INITIAL (t); 5942 5943 /* If the RHS can be stabilized without breaking copy elision, stabilize 5944 it. We specifically don't stabilize class prvalues here because that 5945 would mean an extra copy, but they might be stabilized below. */ 5946 if (TREE_CODE (init) == INIT_EXPR 5947 && TREE_CODE (t) != CONSTRUCTOR 5948 && TREE_CODE (t) != AGGR_INIT_EXPR 5949 && (SCALAR_TYPE_P (TREE_TYPE (t)) 5950 || glvalue_p (t))) 5951 { 5952 TREE_OPERAND (init, 1) = stabilize_expr (t, initp); 5953 return true; 5954 } 5955 5956 if (TREE_CODE (t) == COMPOUND_EXPR 5957 && TREE_CODE (init) == INIT_EXPR) 5958 { 5959 tree last = expr_last (t); 5960 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */ 5961 if (!TREE_SIDE_EFFECTS (last)) 5962 { 5963 *initp = t; 5964 TREE_OPERAND (init, 1) = last; 5965 return true; 5966 } 5967 } 5968 5969 if (TREE_CODE (t) == CONSTRUCTOR) 5970 { 5971 /* Aggregate initialization: stabilize each of the field 5972 initializers. */ 5973 unsigned i; 5974 constructor_elt *ce; 5975 bool good = true; 5976 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); 5977 for (i = 0; vec_safe_iterate (v, i, &ce); ++i) 5978 { 5979 tree type = TREE_TYPE (ce->value); 5980 tree subinit; 5981 if (TYPE_REF_P (type) 5982 || SCALAR_TYPE_P (type)) 5983 ce->value = stabilize_expr (ce->value, &subinit); 5984 else if (!stabilize_init (ce->value, &subinit)) 5985 good = false; 5986 *initp = add_stmt_to_compound (*initp, subinit); 5987 } 5988 return good; 5989 } 5990 5991 if (TREE_CODE (t) == CALL_EXPR) 5992 { 5993 stabilize_call (t, initp); 5994 return true; 5995 } 5996 5997 if (TREE_CODE (t) == AGGR_INIT_EXPR) 5998 { 5999 stabilize_aggr_init (t, initp); 6000 return true; 6001 } 6002 6003 /* The initialization is being performed via a bitwise copy -- and 6004 the item copied may have side effects. */ 6005 return !TREE_SIDE_EFFECTS (init); 6006 } 6007 6008 /* Returns true if a cast to TYPE may appear in an integral constant 6009 expression. */ 6010 6011 bool 6012 cast_valid_in_integral_constant_expression_p (tree type) 6013 { 6014 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type) 6015 || cxx_dialect >= cxx11 6016 || dependent_type_p (type) 6017 || type == error_mark_node); 6018 } 6019 6020 /* Return true if we need to fix linkage information of DECL. */ 6021 6022 static bool 6023 cp_fix_function_decl_p (tree decl) 6024 { 6025 /* Skip if DECL is not externally visible. */ 6026 if (!TREE_PUBLIC (decl)) 6027 return false; 6028 6029 /* We need to fix DECL if it a appears to be exported but with no 6030 function body. Thunks do not have CFGs and we may need to 6031 handle them specially later. */ 6032 if (!gimple_has_body_p (decl) 6033 && !DECL_THUNK_P (decl) 6034 && !DECL_EXTERNAL (decl)) 6035 { 6036 struct cgraph_node *node = cgraph_node::get (decl); 6037 6038 /* Don't fix same_body aliases. Although they don't have their own 6039 CFG, they share it with what they alias to. */ 6040 if (!node || !node->alias || !node->num_references ()) 6041 return true; 6042 } 6043 6044 return false; 6045 } 6046 6047 /* Clean the C++ specific parts of the tree T. */ 6048 6049 void 6050 cp_free_lang_data (tree t) 6051 { 6052 if (FUNC_OR_METHOD_TYPE_P (t)) 6053 { 6054 /* Default args are not interesting anymore. */ 6055 tree argtypes = TYPE_ARG_TYPES (t); 6056 while (argtypes) 6057 { 6058 TREE_PURPOSE (argtypes) = 0; 6059 argtypes = TREE_CHAIN (argtypes); 6060 } 6061 } 6062 else if (TREE_CODE (t) == FUNCTION_DECL 6063 && cp_fix_function_decl_p (t)) 6064 { 6065 /* If T is used in this translation unit at all, the definition 6066 must exist somewhere else since we have decided to not emit it 6067 in this TU. So make it an external reference. */ 6068 DECL_EXTERNAL (t) = 1; 6069 TREE_STATIC (t) = 0; 6070 } 6071 if (TREE_CODE (t) == NAMESPACE_DECL) 6072 /* We do not need the leftover chaining of namespaces from the 6073 binding level. */ 6074 DECL_CHAIN (t) = NULL_TREE; 6075 } 6076 6077 /* Stub for c-common. Please keep in sync with c-decl.cc. 6078 FIXME: If address space support is target specific, then this 6079 should be a C target hook. But currently this is not possible, 6080 because this function is called via REGISTER_TARGET_PRAGMAS. */ 6081 void 6082 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/) 6083 { 6084 } 6085 6086 /* Return the number of operands in T that we care about for things like 6087 mangling. */ 6088 6089 int 6090 cp_tree_operand_length (const_tree t) 6091 { 6092 enum tree_code code = TREE_CODE (t); 6093 6094 if (TREE_CODE_CLASS (code) == tcc_vl_exp) 6095 return VL_EXP_OPERAND_LENGTH (t); 6096 6097 return cp_tree_code_length (code); 6098 } 6099 6100 /* Like cp_tree_operand_length, but takes a tree_code CODE. */ 6101 6102 int 6103 cp_tree_code_length (enum tree_code code) 6104 { 6105 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 6106 6107 switch (code) 6108 { 6109 case PREINCREMENT_EXPR: 6110 case PREDECREMENT_EXPR: 6111 case POSTINCREMENT_EXPR: 6112 case POSTDECREMENT_EXPR: 6113 return 1; 6114 6115 case ARRAY_REF: 6116 return 2; 6117 6118 case EXPR_PACK_EXPANSION: 6119 return 1; 6120 6121 default: 6122 return TREE_CODE_LENGTH (code); 6123 } 6124 } 6125 6126 /* Like EXPR_LOCATION, but also handle some tcc_exceptional that have 6127 locations. */ 6128 6129 location_t 6130 cp_expr_location (const_tree t_) 6131 { 6132 tree t = CONST_CAST_TREE (t_); 6133 if (t == NULL_TREE) 6134 return UNKNOWN_LOCATION; 6135 switch (TREE_CODE (t)) 6136 { 6137 case LAMBDA_EXPR: 6138 return LAMBDA_EXPR_LOCATION (t); 6139 case STATIC_ASSERT: 6140 return STATIC_ASSERT_SOURCE_LOCATION (t); 6141 case TRAIT_EXPR: 6142 return TRAIT_EXPR_LOCATION (t); 6143 case PTRMEM_CST: 6144 return PTRMEM_CST_LOCATION (t); 6145 default: 6146 return EXPR_LOCATION (t); 6147 } 6148 } 6149 6150 /* Implement -Wzero_as_null_pointer_constant. Return true if the 6151 conditions for the warning hold, false otherwise. */ 6152 bool 6153 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc) 6154 { 6155 if (c_inhibit_evaluation_warnings == 0 6156 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr))) 6157 { 6158 warning_at (loc, OPT_Wzero_as_null_pointer_constant, 6159 "zero as null pointer constant"); 6160 return true; 6161 } 6162 return false; 6163 } 6164 6165 /* Release memory we no longer need after parsing. */ 6167 void 6168 cp_tree_c_finish_parsing () 6169 { 6170 if (previous_class_level) 6171 invalidate_class_lookup_cache (); 6172 deleted_copy_types = NULL; 6173 } 6174 6175 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007) 6177 /* Complain that some language-specific thing hanging off a tree 6178 node has been accessed improperly. */ 6179 6180 void 6181 lang_check_failed (const char* file, int line, const char* function) 6182 { 6183 internal_error ("%<lang_*%> check: failed in %s, at %s:%d", 6184 function, trim_filename (file), line); 6185 } 6186 #endif /* ENABLE_TREE_CHECKING */ 6187 6188 #if CHECKING_P 6189 6190 namespace selftest { 6191 6192 /* Verify that lvalue_kind () works, for various expressions, 6193 and that location wrappers don't affect the results. */ 6194 6195 static void 6196 test_lvalue_kind () 6197 { 6198 location_t loc = BUILTINS_LOCATION; 6199 6200 /* Verify constants and parameters, without and with 6201 location wrappers. */ 6202 tree int_cst = build_int_cst (integer_type_node, 42); 6203 ASSERT_EQ (clk_none, lvalue_kind (int_cst)); 6204 6205 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc); 6206 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst)); 6207 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst)); 6208 6209 tree string_lit = build_string (4, "foo"); 6210 TREE_TYPE (string_lit) = char_array_type_node; 6211 string_lit = fix_string_type (string_lit); 6212 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit)); 6213 6214 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc); 6215 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit)); 6216 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit)); 6217 6218 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, 6219 get_identifier ("some_parm"), 6220 integer_type_node); 6221 ASSERT_EQ (clk_ordinary, lvalue_kind (parm)); 6222 6223 tree wrapped_parm = maybe_wrap_with_location (parm, loc); 6224 ASSERT_TRUE (location_wrapper_p (wrapped_parm)); 6225 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm)); 6226 6227 /* Verify that lvalue_kind of std::move on a parm isn't 6228 affected by location wrappers. */ 6229 tree rvalue_ref_of_parm = move (parm); 6230 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm)); 6231 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm); 6232 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm)); 6233 6234 /* Verify lvalue_p. */ 6235 ASSERT_FALSE (lvalue_p (int_cst)); 6236 ASSERT_FALSE (lvalue_p (wrapped_int_cst)); 6237 ASSERT_TRUE (lvalue_p (parm)); 6238 ASSERT_TRUE (lvalue_p (wrapped_parm)); 6239 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm)); 6240 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm)); 6241 } 6242 6243 /* Run all of the selftests within this file. */ 6244 6245 void 6246 cp_tree_cc_tests () 6247 { 6248 test_lvalue_kind (); 6249 } 6250 6251 } // namespace selftest 6252 6253 #endif /* #if CHECKING_P */ 6254 6255 6256 #include "gt-cp-tree.h" 6257