1 /* Language-dependent node constructors for parse phase of GNU compiler. 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 4 Free Software Foundation, Inc. 5 Hacked by Michael Tiemann (tiemann@cygnus.com) 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3, or (at your option) 12 any later version. 13 14 GCC is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with GCC; see the file COPYING3. If not see 21 <http://www.gnu.org/licenses/>. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "tm.h" 27 #include "tree.h" 28 #include "cp-tree.h" 29 #include "flags.h" 30 #include "real.h" 31 #include "rtl.h" 32 #include "toplev.h" 33 #include "insn-config.h" 34 #include "integrate.h" 35 #include "tree-inline.h" 36 #include "debug.h" 37 #include "target.h" 38 #include "convert.h" 39 #include "tree-flow.h" 40 #include "cgraph.h" 41 42 static tree bot_manip (tree *, int *, void *); 43 static tree bot_replace (tree *, int *, void *); 44 static int list_hash_eq (const void *, const void *); 45 static hashval_t list_hash_pieces (tree, tree, tree); 46 static hashval_t list_hash (const void *); 47 static cp_lvalue_kind lvalue_p_1 (const_tree); 48 static tree build_target_expr (tree, tree); 49 static tree count_trees_r (tree *, int *, void *); 50 static tree verify_stmt_tree_r (tree *, int *, void *); 51 static tree build_local_temp (tree); 52 53 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *); 54 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *); 55 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *); 56 57 /* If REF is an lvalue, returns the kind of lvalue that REF is. 58 Otherwise, returns clk_none. */ 59 60 static cp_lvalue_kind 61 lvalue_p_1 (const_tree ref) 62 { 63 cp_lvalue_kind op1_lvalue_kind = clk_none; 64 cp_lvalue_kind op2_lvalue_kind = clk_none; 65 66 /* Expressions of reference type are sometimes wrapped in 67 INDIRECT_REFs. INDIRECT_REFs are just internal compiler 68 representation, not part of the language, so we have to look 69 through them. */ 70 if (TREE_CODE (ref) == INDIRECT_REF 71 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) 72 == REFERENCE_TYPE) 73 return lvalue_p_1 (TREE_OPERAND (ref, 0)); 74 75 if (TREE_TYPE (ref) 76 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE) 77 { 78 /* unnamed rvalue references are rvalues */ 79 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref)) 80 && TREE_CODE (ref) != PARM_DECL 81 && TREE_CODE (ref) != VAR_DECL 82 && TREE_CODE (ref) != COMPONENT_REF) 83 return clk_rvalueref; 84 85 /* lvalue references and named rvalue references are lvalues. */ 86 return clk_ordinary; 87 } 88 89 if (ref == current_class_ptr) 90 return clk_none; 91 92 switch (TREE_CODE (ref)) 93 { 94 case SAVE_EXPR: 95 return clk_none; 96 /* preincrements and predecrements are valid lvals, provided 97 what they refer to are valid lvals. */ 98 case PREINCREMENT_EXPR: 99 case PREDECREMENT_EXPR: 100 case TRY_CATCH_EXPR: 101 case WITH_CLEANUP_EXPR: 102 case REALPART_EXPR: 103 case IMAGPART_EXPR: 104 return lvalue_p_1 (TREE_OPERAND (ref, 0)); 105 106 case COMPONENT_REF: 107 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0)); 108 /* Look at the member designator. */ 109 if (!op1_lvalue_kind) 110 ; 111 else if (is_overloaded_fn (TREE_OPERAND (ref, 1))) 112 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some 113 situations. If we're seeing a COMPONENT_REF, it's a non-static 114 member, so it isn't an lvalue. */ 115 op1_lvalue_kind = clk_none; 116 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL) 117 /* This can be IDENTIFIER_NODE in a template. */; 118 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1))) 119 { 120 /* Clear the ordinary bit. If this object was a class 121 rvalue we want to preserve that information. */ 122 op1_lvalue_kind &= ~clk_ordinary; 123 /* The lvalue is for a bitfield. */ 124 op1_lvalue_kind |= clk_bitfield; 125 } 126 else if (DECL_PACKED (TREE_OPERAND (ref, 1))) 127 op1_lvalue_kind |= clk_packed; 128 129 return op1_lvalue_kind; 130 131 case STRING_CST: 132 case COMPOUND_LITERAL_EXPR: 133 return clk_ordinary; 134 135 case CONST_DECL: 136 /* CONST_DECL without TREE_STATIC are enumeration values and 137 thus not lvalues. With TREE_STATIC they are used by ObjC++ 138 in objc_build_string_object and need to be considered as 139 lvalues. */ 140 if (! TREE_STATIC (ref)) 141 return clk_none; 142 case VAR_DECL: 143 if (TREE_READONLY (ref) && ! TREE_STATIC (ref) 144 && DECL_LANG_SPECIFIC (ref) 145 && DECL_IN_AGGR_P (ref)) 146 return clk_none; 147 case INDIRECT_REF: 148 case ARRAY_REF: 149 case PARM_DECL: 150 case RESULT_DECL: 151 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE) 152 return clk_ordinary; 153 break; 154 155 /* A scope ref in a template, left as SCOPE_REF to support later 156 access checking. */ 157 case SCOPE_REF: 158 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE(ref))); 159 return lvalue_p_1 (TREE_OPERAND (ref, 1)); 160 161 case MAX_EXPR: 162 case MIN_EXPR: 163 /* Disallow <? and >? as lvalues if either argument side-effects. */ 164 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0)) 165 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1))) 166 return clk_none; 167 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0)); 168 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1)); 169 break; 170 171 case COND_EXPR: 172 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1) 173 ? TREE_OPERAND (ref, 1) 174 : TREE_OPERAND (ref, 0)); 175 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2)); 176 break; 177 178 case MODIFY_EXPR: 179 return clk_ordinary; 180 181 case COMPOUND_EXPR: 182 return lvalue_p_1 (TREE_OPERAND (ref, 1)); 183 184 case TARGET_EXPR: 185 return clk_class; 186 187 case VA_ARG_EXPR: 188 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none); 189 190 case CALL_EXPR: 191 /* Any class-valued call would be wrapped in a TARGET_EXPR. */ 192 return clk_none; 193 194 case FUNCTION_DECL: 195 /* All functions (except non-static-member functions) are 196 lvalues. */ 197 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref) 198 ? clk_none : clk_ordinary); 199 200 case BASELINK: 201 /* We now represent a reference to a single static member function 202 with a BASELINK. */ 203 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns 204 its argument unmodified and we assign it to a const_tree. */ 205 return lvalue_p_1 (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref))); 206 207 case NON_DEPENDENT_EXPR: 208 /* We must consider NON_DEPENDENT_EXPRs to be lvalues so that 209 things like "&E" where "E" is an expression with a 210 non-dependent type work. It is safe to be lenient because an 211 error will be issued when the template is instantiated if "E" 212 is not an lvalue. */ 213 return clk_ordinary; 214 215 default: 216 break; 217 } 218 219 /* If one operand is not an lvalue at all, then this expression is 220 not an lvalue. */ 221 if (!op1_lvalue_kind || !op2_lvalue_kind) 222 return clk_none; 223 224 /* Otherwise, it's an lvalue, and it has all the odd properties 225 contributed by either operand. */ 226 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind; 227 /* It's not an ordinary lvalue if it involves any other kind. */ 228 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none) 229 op1_lvalue_kind &= ~clk_ordinary; 230 /* It can't be both a pseudo-lvalue and a non-addressable lvalue. 231 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */ 232 if ((op1_lvalue_kind & (clk_rvalueref|clk_class)) 233 && (op1_lvalue_kind & (clk_bitfield|clk_packed))) 234 op1_lvalue_kind = clk_none; 235 return op1_lvalue_kind; 236 } 237 238 /* Returns the kind of lvalue that REF is, in the sense of 239 [basic.lval]. This function should really be named lvalue_p; it 240 computes the C++ definition of lvalue. */ 241 242 cp_lvalue_kind 243 real_lvalue_p (tree ref) 244 { 245 cp_lvalue_kind kind = lvalue_p_1 (ref); 246 if (kind & (clk_rvalueref|clk_class)) 247 return clk_none; 248 else 249 return kind; 250 } 251 252 /* This differs from real_lvalue_p in that class rvalues are considered 253 lvalues. */ 254 255 bool 256 lvalue_p (const_tree ref) 257 { 258 return (lvalue_p_1 (ref) != clk_none); 259 } 260 261 /* This differs from real_lvalue_p in that rvalues formed by dereferencing 262 rvalue references are considered rvalues. */ 263 264 bool 265 lvalue_or_rvalue_with_address_p (const_tree ref) 266 { 267 cp_lvalue_kind kind = lvalue_p_1 (ref); 268 if (kind & clk_class) 269 return false; 270 else 271 return (kind != clk_none); 272 } 273 274 /* Test whether DECL is a builtin that may appear in a 275 constant-expression. */ 276 277 bool 278 builtin_valid_in_constant_expr_p (const_tree decl) 279 { 280 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing 281 in constant-expressions. We may want to add other builtins later. */ 282 return DECL_IS_BUILTIN_CONSTANT_P (decl); 283 } 284 285 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */ 286 287 static tree 288 build_target_expr (tree decl, tree value) 289 { 290 tree t; 291 292 #ifdef ENABLE_CHECKING 293 gcc_assert (VOID_TYPE_P (TREE_TYPE (value)) 294 || TREE_TYPE (decl) == TREE_TYPE (value) 295 || useless_type_conversion_p (TREE_TYPE (decl), 296 TREE_TYPE (value))); 297 #endif 298 299 t = build4 (TARGET_EXPR, TREE_TYPE (decl), decl, value, 300 cxx_maybe_build_cleanup (decl), NULL_TREE); 301 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not 302 ignore the TARGET_EXPR. If there really turn out to be no 303 side-effects, then the optimizer should be able to get rid of 304 whatever code is generated anyhow. */ 305 TREE_SIDE_EFFECTS (t) = 1; 306 307 return t; 308 } 309 310 /* Return an undeclared local temporary of type TYPE for use in building a 311 TARGET_EXPR. */ 312 313 static tree 314 build_local_temp (tree type) 315 { 316 tree slot = build_decl (input_location, 317 VAR_DECL, NULL_TREE, type); 318 DECL_ARTIFICIAL (slot) = 1; 319 DECL_IGNORED_P (slot) = 1; 320 DECL_CONTEXT (slot) = current_function_decl; 321 layout_decl (slot, 0); 322 return slot; 323 } 324 325 /* Set various status flags when building an AGGR_INIT_EXPR object T. */ 326 327 static void 328 process_aggr_init_operands (tree t) 329 { 330 bool side_effects; 331 332 side_effects = TREE_SIDE_EFFECTS (t); 333 if (!side_effects) 334 { 335 int i, n; 336 n = TREE_OPERAND_LENGTH (t); 337 for (i = 1; i < n; i++) 338 { 339 tree op = TREE_OPERAND (t, i); 340 if (op && TREE_SIDE_EFFECTS (op)) 341 { 342 side_effects = 1; 343 break; 344 } 345 } 346 } 347 TREE_SIDE_EFFECTS (t) = side_effects; 348 } 349 350 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE, 351 FN, and SLOT. NARGS is the number of call arguments which are specified 352 as a tree array ARGS. */ 353 354 static tree 355 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs, 356 tree *args) 357 { 358 tree t; 359 int i; 360 361 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3); 362 TREE_TYPE (t) = return_type; 363 AGGR_INIT_EXPR_FN (t) = fn; 364 AGGR_INIT_EXPR_SLOT (t) = slot; 365 for (i = 0; i < nargs; i++) 366 AGGR_INIT_EXPR_ARG (t, i) = args[i]; 367 process_aggr_init_operands (t); 368 return t; 369 } 370 371 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its 372 target. TYPE is the type to be initialized. 373 374 Build an AGGR_INIT_EXPR to represent the initialization. This function 375 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used 376 to initialize another object, whereas a TARGET_EXPR can either 377 initialize another object or create its own temporary object, and as a 378 result building up a TARGET_EXPR requires that the type's destructor be 379 callable. */ 380 381 tree 382 build_aggr_init_expr (tree type, tree init) 383 { 384 tree fn; 385 tree slot; 386 tree rval; 387 int is_ctor; 388 389 /* Make sure that we're not trying to create an instance of an 390 abstract class. */ 391 abstract_virtuals_error (NULL_TREE, type); 392 393 if (TREE_CODE (init) == CALL_EXPR) 394 fn = CALL_EXPR_FN (init); 395 else if (TREE_CODE (init) == AGGR_INIT_EXPR) 396 fn = AGGR_INIT_EXPR_FN (init); 397 else 398 return convert (type, init); 399 400 is_ctor = (TREE_CODE (fn) == ADDR_EXPR 401 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL 402 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0))); 403 404 /* We split the CALL_EXPR into its function and its arguments here. 405 Then, in expand_expr, we put them back together. The reason for 406 this is that this expression might be a default argument 407 expression. In that case, we need a new temporary every time the 408 expression is used. That's what break_out_target_exprs does; it 409 replaces every AGGR_INIT_EXPR with a copy that uses a fresh 410 temporary slot. Then, expand_expr builds up a call-expression 411 using the new slot. */ 412 413 /* If we don't need to use a constructor to create an object of this 414 type, don't mess with AGGR_INIT_EXPR. */ 415 if (is_ctor || TREE_ADDRESSABLE (type)) 416 { 417 slot = build_local_temp (type); 418 419 if (TREE_CODE(init) == CALL_EXPR) 420 rval = build_aggr_init_array (void_type_node, fn, slot, 421 call_expr_nargs (init), 422 CALL_EXPR_ARGP (init)); 423 else 424 rval = build_aggr_init_array (void_type_node, fn, slot, 425 aggr_init_expr_nargs (init), 426 AGGR_INIT_EXPR_ARGP (init)); 427 TREE_SIDE_EFFECTS (rval) = 1; 428 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor; 429 } 430 else 431 rval = init; 432 433 return rval; 434 } 435 436 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its 437 target. TYPE is the type that this initialization should appear to 438 have. 439 440 Build an encapsulation of the initialization to perform 441 and return it so that it can be processed by language-independent 442 and language-specific expression expanders. */ 443 444 tree 445 build_cplus_new (tree type, tree init) 446 { 447 tree rval = build_aggr_init_expr (type, init); 448 tree slot; 449 450 if (TREE_CODE (rval) == AGGR_INIT_EXPR) 451 slot = AGGR_INIT_EXPR_SLOT (rval); 452 else if (TREE_CODE (rval) == CALL_EXPR) 453 slot = build_local_temp (type); 454 else 455 return rval; 456 457 rval = build_target_expr (slot, rval); 458 TARGET_EXPR_IMPLICIT_P (rval) = 1; 459 460 return rval; 461 } 462 463 /* Return a TARGET_EXPR which expresses the direct-initialization of one 464 array from another. */ 465 466 tree 467 build_array_copy (tree init) 468 { 469 tree type = TREE_TYPE (init); 470 tree slot = build_local_temp (type); 471 init = build2 (VEC_INIT_EXPR, type, slot, init); 472 SET_EXPR_LOCATION (init, input_location); 473 init = build_target_expr (slot, init); 474 TARGET_EXPR_IMPLICIT_P (init) = 1; 475 476 return init; 477 } 478 479 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the 480 indicated TYPE. */ 481 482 tree 483 build_target_expr_with_type (tree init, tree type) 484 { 485 gcc_assert (!VOID_TYPE_P (type)); 486 487 if (TREE_CODE (init) == TARGET_EXPR) 488 return init; 489 else if (CLASS_TYPE_P (type) && !TYPE_HAS_TRIVIAL_INIT_REF (type) 490 && !VOID_TYPE_P (TREE_TYPE (init)) 491 && TREE_CODE (init) != COND_EXPR 492 && TREE_CODE (init) != CONSTRUCTOR 493 && TREE_CODE (init) != VA_ARG_EXPR) 494 /* We need to build up a copy constructor call. A void initializer 495 means we're being called from bot_manip. COND_EXPR is a special 496 case because we already have copies on the arms and we don't want 497 another one here. A CONSTRUCTOR is aggregate initialization, which 498 is handled separately. A VA_ARG_EXPR is magic creation of an 499 aggregate; there's no additional work to be done. */ 500 return force_rvalue (init); 501 502 return force_target_expr (type, init); 503 } 504 505 /* Like the above function, but without the checking. This function should 506 only be used by code which is deliberately trying to subvert the type 507 system, such as call_builtin_trap. */ 508 509 tree 510 force_target_expr (tree type, tree init) 511 { 512 tree slot; 513 514 gcc_assert (!VOID_TYPE_P (type)); 515 516 slot = build_local_temp (type); 517 return build_target_expr (slot, init); 518 } 519 520 /* Like build_target_expr_with_type, but use the type of INIT. */ 521 522 tree 523 get_target_expr (tree init) 524 { 525 if (TREE_CODE (init) == AGGR_INIT_EXPR) 526 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init); 527 else 528 return build_target_expr_with_type (init, TREE_TYPE (init)); 529 } 530 531 /* If EXPR is a bitfield reference, convert it to the declared type of 532 the bitfield, and return the resulting expression. Otherwise, 533 return EXPR itself. */ 534 535 tree 536 convert_bitfield_to_declared_type (tree expr) 537 { 538 tree bitfield_type; 539 540 bitfield_type = is_bitfield_expr_with_lowered_type (expr); 541 if (bitfield_type) 542 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), 543 expr); 544 return expr; 545 } 546 547 /* EXPR is being used in an rvalue context. Return a version of EXPR 548 that is marked as an rvalue. */ 549 550 tree 551 rvalue (tree expr) 552 { 553 tree type; 554 555 if (error_operand_p (expr)) 556 return expr; 557 558 /* [basic.lval] 559 560 Non-class rvalues always have cv-unqualified types. */ 561 type = TREE_TYPE (expr); 562 if (!CLASS_TYPE_P (type) && cv_qualified_p (type)) 563 type = cv_unqualified (type); 564 565 /* We need to do this for rvalue refs as well to get the right answer 566 from decltype; see c++/36628. */ 567 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr)) 568 expr = build1 (NON_LVALUE_EXPR, type, expr); 569 else if (type != TREE_TYPE (expr)) 570 expr = build_nop (type, expr); 571 572 return expr; 573 } 574 575 576 /* Hash an ARRAY_TYPE. K is really of type `tree'. */ 577 578 static hashval_t 579 cplus_array_hash (const void* k) 580 { 581 hashval_t hash; 582 const_tree const t = (const_tree) k; 583 584 hash = TYPE_UID (TREE_TYPE (t)); 585 if (TYPE_DOMAIN (t)) 586 hash ^= TYPE_UID (TYPE_DOMAIN (t)); 587 return hash; 588 } 589 590 typedef struct cplus_array_info { 591 tree type; 592 tree domain; 593 } cplus_array_info; 594 595 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really 596 of type `cplus_array_info*'. */ 597 598 static int 599 cplus_array_compare (const void * k1, const void * k2) 600 { 601 const_tree const t1 = (const_tree) k1; 602 const cplus_array_info *const t2 = (const cplus_array_info*) k2; 603 604 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain); 605 } 606 607 /* Hash table containing dependent array types, which are unsuitable for 608 the language-independent type hash table. */ 609 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab; 610 611 /* Like build_array_type, but handle special C++ semantics. */ 612 613 tree 614 build_cplus_array_type (tree elt_type, tree index_type) 615 { 616 tree t; 617 618 if (elt_type == error_mark_node || index_type == error_mark_node) 619 return error_mark_node; 620 621 if (processing_template_decl 622 && (dependent_type_p (elt_type) 623 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type))))) 624 { 625 void **e; 626 cplus_array_info cai; 627 hashval_t hash; 628 629 if (cplus_array_htab == NULL) 630 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash, 631 &cplus_array_compare, NULL); 632 633 hash = TYPE_UID (elt_type); 634 if (index_type) 635 hash ^= TYPE_UID (index_type); 636 cai.type = elt_type; 637 cai.domain = index_type; 638 639 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT); 640 if (*e) 641 /* We have found the type: we're done. */ 642 return (tree) *e; 643 else 644 { 645 /* Build a new array type. */ 646 t = cxx_make_type (ARRAY_TYPE); 647 TREE_TYPE (t) = elt_type; 648 TYPE_DOMAIN (t) = index_type; 649 650 /* Store it in the hash table. */ 651 *e = t; 652 653 /* Set the canonical type for this new node. */ 654 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type) 655 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))) 656 SET_TYPE_STRUCTURAL_EQUALITY (t); 657 else if (TYPE_CANONICAL (elt_type) != elt_type 658 || (index_type 659 && TYPE_CANONICAL (index_type) != index_type)) 660 TYPE_CANONICAL (t) 661 = build_cplus_array_type 662 (TYPE_CANONICAL (elt_type), 663 index_type ? TYPE_CANONICAL (index_type) : index_type); 664 else 665 TYPE_CANONICAL (t) = t; 666 } 667 } 668 else 669 t = build_array_type (elt_type, index_type); 670 671 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the 672 element type as well, so fix it up if needed. */ 673 if (elt_type != TYPE_MAIN_VARIANT (elt_type)) 674 { 675 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type), 676 index_type); 677 if (TYPE_MAIN_VARIANT (t) != m) 678 { 679 TYPE_MAIN_VARIANT (t) = m; 680 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m); 681 TYPE_NEXT_VARIANT (m) = t; 682 } 683 } 684 685 /* Push these needs up so that initialization takes place 686 more easily. */ 687 TYPE_NEEDS_CONSTRUCTING (t) 688 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type)); 689 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 690 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type)); 691 return t; 692 } 693 694 /* Return an ARRAY_TYPE with element type ELT and length N. */ 695 696 tree 697 build_array_of_n_type (tree elt, int n) 698 { 699 return build_cplus_array_type (elt, build_index_type (size_int (n - 1))); 700 } 701 702 /* Return a reference type node referring to TO_TYPE. If RVAL is 703 true, return an rvalue reference type, otherwise return an lvalue 704 reference type. If a type node exists, reuse it, otherwise create 705 a new one. */ 706 tree 707 cp_build_reference_type (tree to_type, bool rval) 708 { 709 tree lvalue_ref, t; 710 lvalue_ref = build_reference_type (to_type); 711 if (!rval) 712 return lvalue_ref; 713 714 /* This code to create rvalue reference types is based on and tied 715 to the code creating lvalue reference types in the middle-end 716 functions build_reference_type_for_mode and build_reference_type. 717 718 It works by putting the rvalue reference type nodes after the 719 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so 720 they will effectively be ignored by the middle end. */ 721 722 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); ) 723 if (TYPE_REF_IS_RVALUE (t)) 724 return t; 725 726 t = build_distinct_type_copy (lvalue_ref); 727 728 TYPE_REF_IS_RVALUE (t) = true; 729 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref); 730 TYPE_NEXT_REF_TO (lvalue_ref) = t; 731 732 if (TYPE_STRUCTURAL_EQUALITY_P (to_type)) 733 SET_TYPE_STRUCTURAL_EQUALITY (t); 734 else if (TYPE_CANONICAL (to_type) != to_type) 735 TYPE_CANONICAL (t) 736 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval); 737 else 738 TYPE_CANONICAL (t) = t; 739 740 layout_type (t); 741 742 return t; 743 744 } 745 746 /* Returns EXPR cast to rvalue reference type, like std::move. */ 747 748 tree 749 move (tree expr) 750 { 751 tree type = TREE_TYPE (expr); 752 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE); 753 type = cp_build_reference_type (type, /*rval*/true); 754 return build_static_cast (type, expr, tf_warning_or_error); 755 } 756 757 /* Used by the C++ front end to build qualified array types. However, 758 the C version of this function does not properly maintain canonical 759 types (which are not used in C). */ 760 tree 761 c_build_qualified_type (tree type, int type_quals) 762 { 763 return cp_build_qualified_type (type, type_quals); 764 } 765 766 767 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles 768 arrays correctly. In particular, if TYPE is an array of T's, and 769 TYPE_QUALS is non-empty, returns an array of qualified T's. 770 771 FLAGS determines how to deal with ill-formed qualifications. If 772 tf_ignore_bad_quals is set, then bad qualifications are dropped 773 (this is permitted if TYPE was introduced via a typedef or template 774 type parameter). If bad qualifications are dropped and tf_warning 775 is set, then a warning is issued for non-const qualifications. If 776 tf_ignore_bad_quals is not set and tf_error is not set, we 777 return error_mark_node. Otherwise, we issue an error, and ignore 778 the qualifications. 779 780 Qualification of a reference type is valid when the reference came 781 via a typedef or template type argument. [dcl.ref] No such 782 dispensation is provided for qualifying a function type. [dcl.fct] 783 DR 295 queries this and the proposed resolution brings it into line 784 with qualifying a reference. We implement the DR. We also behave 785 in a similar manner for restricting non-pointer types. */ 786 787 tree 788 cp_build_qualified_type_real (tree type, 789 int type_quals, 790 tsubst_flags_t complain) 791 { 792 tree result; 793 int bad_quals = TYPE_UNQUALIFIED; 794 795 if (type == error_mark_node) 796 return type; 797 798 if (type_quals == cp_type_quals (type)) 799 return type; 800 801 if (TREE_CODE (type) == ARRAY_TYPE) 802 { 803 /* In C++, the qualification really applies to the array element 804 type. Obtain the appropriately qualified element type. */ 805 tree t; 806 tree element_type 807 = cp_build_qualified_type_real (TREE_TYPE (type), 808 type_quals, 809 complain); 810 811 if (element_type == error_mark_node) 812 return error_mark_node; 813 814 /* See if we already have an identically qualified type. Tests 815 should be equivalent to those in check_qualified_type. */ 816 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 817 if (TREE_TYPE (t) == element_type 818 && TYPE_NAME (t) == TYPE_NAME (type) 819 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type) 820 && attribute_list_equal (TYPE_ATTRIBUTES (t), 821 TYPE_ATTRIBUTES (type))) 822 break; 823 824 if (!t) 825 { 826 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type)); 827 828 /* Keep the typedef name. */ 829 if (TYPE_NAME (t) != TYPE_NAME (type)) 830 { 831 t = build_variant_type_copy (t); 832 TYPE_NAME (t) = TYPE_NAME (type); 833 } 834 } 835 836 /* Even if we already had this variant, we update 837 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case 838 they changed since the variant was originally created. 839 840 This seems hokey; if there is some way to use a previous 841 variant *without* coming through here, 842 TYPE_NEEDS_CONSTRUCTING will never be updated. */ 843 TYPE_NEEDS_CONSTRUCTING (t) 844 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type)); 845 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 846 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type)); 847 return t; 848 } 849 else if (TYPE_PTRMEMFUNC_P (type)) 850 { 851 /* For a pointer-to-member type, we can't just return a 852 cv-qualified version of the RECORD_TYPE. If we do, we 853 haven't changed the field that contains the actual pointer to 854 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */ 855 tree t; 856 857 t = TYPE_PTRMEMFUNC_FN_TYPE (type); 858 t = cp_build_qualified_type_real (t, type_quals, complain); 859 return build_ptrmemfunc_type (t); 860 } 861 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION) 862 { 863 tree t = PACK_EXPANSION_PATTERN (type); 864 865 t = cp_build_qualified_type_real (t, type_quals, complain); 866 return make_pack_expansion (t); 867 } 868 869 /* A reference or method type shall not be cv-qualified. 870 [dcl.ref], [dcl.fct] */ 871 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE) 872 && (TREE_CODE (type) == REFERENCE_TYPE 873 || TREE_CODE (type) == METHOD_TYPE)) 874 { 875 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); 876 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); 877 } 878 879 /* A restrict-qualified type must be a pointer (or reference) 880 to object or incomplete type. */ 881 if ((type_quals & TYPE_QUAL_RESTRICT) 882 && TREE_CODE (type) != TEMPLATE_TYPE_PARM 883 && TREE_CODE (type) != TYPENAME_TYPE 884 && !POINTER_TYPE_P (type)) 885 { 886 bad_quals |= TYPE_QUAL_RESTRICT; 887 type_quals &= ~TYPE_QUAL_RESTRICT; 888 } 889 890 if (bad_quals == TYPE_UNQUALIFIED) 891 /*OK*/; 892 else if (!(complain & (tf_error | tf_ignore_bad_quals))) 893 return error_mark_node; 894 else 895 { 896 if (complain & tf_ignore_bad_quals) 897 /* We're not going to warn about constifying things that can't 898 be constified. */ 899 bad_quals &= ~TYPE_QUAL_CONST; 900 if (bad_quals) 901 { 902 tree bad_type = build_qualified_type (ptr_type_node, bad_quals); 903 904 if (!(complain & tf_ignore_bad_quals)) 905 error ("%qV qualifiers cannot be applied to %qT", 906 bad_type, type); 907 } 908 } 909 910 /* Retrieve (or create) the appropriately qualified variant. */ 911 result = build_qualified_type (type, type_quals); 912 913 /* If this was a pointer-to-method type, and we just made a copy, 914 then we need to unshare the record that holds the cached 915 pointer-to-member-function type, because these will be distinct 916 between the unqualified and qualified types. */ 917 if (result != type 918 && TREE_CODE (type) == POINTER_TYPE 919 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE 920 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type)) 921 TYPE_LANG_SPECIFIC (result) = NULL; 922 923 /* We may also have ended up building a new copy of the canonical 924 type of a pointer-to-method type, which could have the same 925 sharing problem described above. */ 926 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type) 927 && TREE_CODE (type) == POINTER_TYPE 928 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE 929 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) 930 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type)))) 931 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL; 932 933 return result; 934 } 935 936 /* Return TYPE with const and volatile removed. */ 937 938 tree 939 cv_unqualified (tree type) 940 { 941 int quals = TYPE_QUALS (type); 942 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE); 943 return cp_build_qualified_type (type, quals); 944 } 945 946 /* Builds a qualified variant of T that is not a typedef variant. 947 E.g. consider the following declarations: 948 typedef const int ConstInt; 949 typedef ConstInt* PtrConstInt; 950 If T is PtrConstInt, this function returns a type representing 951 const int*. 952 In other words, if T is a typedef, the function returns the underlying type. 953 The cv-qualification and attributes of the type returned match the 954 input type. 955 They will always be compatible types. 956 The returned type is built so that all of its subtypes 957 recursively have their typedefs stripped as well. 958 959 This is different from just returning TYPE_CANONICAL (T) 960 Because of several reasons: 961 * If T is a type that needs structural equality 962 its TYPE_CANONICAL (T) will be NULL. 963 * TYPE_CANONICAL (T) desn't carry type attributes 964 and looses template parameter names. */ 965 966 tree 967 strip_typedefs (tree t) 968 { 969 tree result = NULL, type = NULL, t0 = NULL; 970 971 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t)) 972 return t; 973 974 gcc_assert (TYPE_P (t)); 975 976 switch (TREE_CODE (t)) 977 { 978 case POINTER_TYPE: 979 type = strip_typedefs (TREE_TYPE (t)); 980 result = build_pointer_type (type); 981 break; 982 case REFERENCE_TYPE: 983 type = strip_typedefs (TREE_TYPE (t)); 984 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); 985 break; 986 case OFFSET_TYPE: 987 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t)); 988 type = strip_typedefs (TREE_TYPE (t)); 989 result = build_offset_type (t0, type); 990 break; 991 case RECORD_TYPE: 992 if (TYPE_PTRMEMFUNC_P (t)) 993 { 994 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t)); 995 result = build_ptrmemfunc_type (t0); 996 } 997 break; 998 case ARRAY_TYPE: 999 type = strip_typedefs (TREE_TYPE (t)); 1000 t0 = strip_typedefs (TYPE_DOMAIN (t));; 1001 result = build_cplus_array_type (type, t0); 1002 break; 1003 case FUNCTION_TYPE: 1004 case METHOD_TYPE: 1005 { 1006 tree arg_types = NULL, arg_node, arg_type; 1007 for (arg_node = TYPE_ARG_TYPES (t); 1008 arg_node; 1009 arg_node = TREE_CHAIN (arg_node)) 1010 { 1011 if (arg_node == void_list_node) 1012 break; 1013 arg_type = strip_typedefs (TREE_VALUE (arg_node)); 1014 gcc_assert (arg_type); 1015 1016 arg_types = 1017 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types); 1018 } 1019 1020 if (arg_types) 1021 arg_types = nreverse (arg_types); 1022 1023 /* A list of parameters not ending with an ellipsis 1024 must end with void_list_node. */ 1025 if (arg_node) 1026 arg_types = chainon (arg_types, void_list_node); 1027 1028 type = strip_typedefs (TREE_TYPE (t)); 1029 if (TREE_CODE (t) == METHOD_TYPE) 1030 { 1031 tree class_type = TREE_TYPE (TREE_VALUE (arg_types)); 1032 gcc_assert (class_type); 1033 result = 1034 build_method_type_directly (class_type, type, 1035 TREE_CHAIN (arg_types)); 1036 } 1037 else 1038 result = build_function_type (type, 1039 arg_types); 1040 1041 if (TYPE_RAISES_EXCEPTIONS (t)) 1042 result = build_exception_variant (result, 1043 TYPE_RAISES_EXCEPTIONS (t)); 1044 } 1045 break; 1046 case TYPENAME_TYPE: 1047 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)), 1048 TYPENAME_TYPE_FULLNAME (t), 1049 typename_type, tf_none); 1050 break; 1051 default: 1052 break; 1053 } 1054 1055 if (!result) 1056 result = TYPE_MAIN_VARIANT (t); 1057 if (TYPE_ATTRIBUTES (t)) 1058 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t)); 1059 return cp_build_qualified_type (result, cp_type_quals (t)); 1060 } 1061 1062 /* Returns true iff TYPE is a type variant created for a typedef. */ 1063 1064 bool 1065 typedef_variant_p (tree type) 1066 { 1067 return is_typedef_decl (TYPE_NAME (type)); 1068 } 1069 1070 /* Setup a TYPE_DECL node as a typedef representation. 1071 See comments of set_underlying_type in c-common.c. */ 1072 1073 void 1074 cp_set_underlying_type (tree t) 1075 { 1076 set_underlying_type (t); 1077 /* If T is a template type parm, make it require structural equality. 1078 This is useful when comparing two template type parms, 1079 because it forces the comparison of the template parameters of their 1080 decls. */ 1081 if (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM) 1082 SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (t)); 1083 } 1084 1085 1086 /* Makes a copy of BINFO and TYPE, which is to be inherited into a 1087 graph dominated by T. If BINFO is NULL, TYPE is a dependent base, 1088 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy. 1089 VIRT indicates whether TYPE is inherited virtually or not. 1090 IGO_PREV points at the previous binfo of the inheritance graph 1091 order chain. The newly copied binfo's TREE_CHAIN forms this 1092 ordering. 1093 1094 The CLASSTYPE_VBASECLASSES vector of T is constructed in the 1095 correct order. That is in the order the bases themselves should be 1096 constructed in. 1097 1098 The BINFO_INHERITANCE of a virtual base class points to the binfo 1099 of the most derived type. ??? We could probably change this so that 1100 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence 1101 remove a field. They currently can only differ for primary virtual 1102 virtual bases. */ 1103 1104 tree 1105 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt) 1106 { 1107 tree new_binfo; 1108 1109 if (virt) 1110 { 1111 /* See if we've already made this virtual base. */ 1112 new_binfo = binfo_for_vbase (type, t); 1113 if (new_binfo) 1114 return new_binfo; 1115 } 1116 1117 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0); 1118 BINFO_TYPE (new_binfo) = type; 1119 1120 /* Chain it into the inheritance graph. */ 1121 TREE_CHAIN (*igo_prev) = new_binfo; 1122 *igo_prev = new_binfo; 1123 1124 if (binfo) 1125 { 1126 int ix; 1127 tree base_binfo; 1128 1129 gcc_assert (!BINFO_DEPENDENT_BASE_P (binfo)); 1130 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type)); 1131 1132 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo); 1133 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo); 1134 1135 /* We do not need to copy the accesses, as they are read only. */ 1136 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo); 1137 1138 /* Recursively copy base binfos of BINFO. */ 1139 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 1140 { 1141 tree new_base_binfo; 1142 1143 gcc_assert (!BINFO_DEPENDENT_BASE_P (base_binfo)); 1144 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo), 1145 t, igo_prev, 1146 BINFO_VIRTUAL_P (base_binfo)); 1147 1148 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo)) 1149 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo; 1150 BINFO_BASE_APPEND (new_binfo, new_base_binfo); 1151 } 1152 } 1153 else 1154 BINFO_DEPENDENT_BASE_P (new_binfo) = 1; 1155 1156 if (virt) 1157 { 1158 /* Push it onto the list after any virtual bases it contains 1159 will have been pushed. */ 1160 VEC_quick_push (tree, CLASSTYPE_VBASECLASSES (t), new_binfo); 1161 BINFO_VIRTUAL_P (new_binfo) = 1; 1162 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t); 1163 } 1164 1165 return new_binfo; 1166 } 1167 1168 /* Hashing of lists so that we don't make duplicates. 1169 The entry point is `list_hash_canon'. */ 1170 1171 /* Now here is the hash table. When recording a list, it is added 1172 to the slot whose index is the hash code mod the table size. 1173 Note that the hash table is used for several kinds of lists. 1174 While all these live in the same table, they are completely independent, 1175 and the hash code is computed differently for each of these. */ 1176 1177 static GTY ((param_is (union tree_node))) htab_t list_hash_table; 1178 1179 struct list_proxy 1180 { 1181 tree purpose; 1182 tree value; 1183 tree chain; 1184 }; 1185 1186 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy 1187 for a node we are thinking about adding). */ 1188 1189 static int 1190 list_hash_eq (const void* entry, const void* data) 1191 { 1192 const_tree const t = (const_tree) entry; 1193 const struct list_proxy *const proxy = (const struct list_proxy *) data; 1194 1195 return (TREE_VALUE (t) == proxy->value 1196 && TREE_PURPOSE (t) == proxy->purpose 1197 && TREE_CHAIN (t) == proxy->chain); 1198 } 1199 1200 /* Compute a hash code for a list (chain of TREE_LIST nodes 1201 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the 1202 TREE_COMMON slots), by adding the hash codes of the individual entries. */ 1203 1204 static hashval_t 1205 list_hash_pieces (tree purpose, tree value, tree chain) 1206 { 1207 hashval_t hashcode = 0; 1208 1209 if (chain) 1210 hashcode += TREE_HASH (chain); 1211 1212 if (value) 1213 hashcode += TREE_HASH (value); 1214 else 1215 hashcode += 1007; 1216 if (purpose) 1217 hashcode += TREE_HASH (purpose); 1218 else 1219 hashcode += 1009; 1220 return hashcode; 1221 } 1222 1223 /* Hash an already existing TREE_LIST. */ 1224 1225 static hashval_t 1226 list_hash (const void* p) 1227 { 1228 const_tree const t = (const_tree) p; 1229 return list_hash_pieces (TREE_PURPOSE (t), 1230 TREE_VALUE (t), 1231 TREE_CHAIN (t)); 1232 } 1233 1234 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical 1235 object for an identical list if one already exists. Otherwise, build a 1236 new one, and record it as the canonical object. */ 1237 1238 tree 1239 hash_tree_cons (tree purpose, tree value, tree chain) 1240 { 1241 int hashcode = 0; 1242 void **slot; 1243 struct list_proxy proxy; 1244 1245 /* Hash the list node. */ 1246 hashcode = list_hash_pieces (purpose, value, chain); 1247 /* Create a proxy for the TREE_LIST we would like to create. We 1248 don't actually create it so as to avoid creating garbage. */ 1249 proxy.purpose = purpose; 1250 proxy.value = value; 1251 proxy.chain = chain; 1252 /* See if it is already in the table. */ 1253 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode, 1254 INSERT); 1255 /* If not, create a new node. */ 1256 if (!*slot) 1257 *slot = tree_cons (purpose, value, chain); 1258 return (tree) *slot; 1259 } 1260 1261 /* Constructor for hashed lists. */ 1262 1263 tree 1264 hash_tree_chain (tree value, tree chain) 1265 { 1266 return hash_tree_cons (NULL_TREE, value, chain); 1267 } 1268 1269 void 1270 debug_binfo (tree elem) 1271 { 1272 HOST_WIDE_INT n; 1273 tree virtuals; 1274 1275 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC 1276 "\nvtable type:\n", 1277 TYPE_NAME_STRING (BINFO_TYPE (elem)), 1278 TREE_INT_CST_LOW (BINFO_OFFSET (elem))); 1279 debug_tree (BINFO_TYPE (elem)); 1280 if (BINFO_VTABLE (elem)) 1281 fprintf (stderr, "vtable decl \"%s\"\n", 1282 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem)))); 1283 else 1284 fprintf (stderr, "no vtable decl yet\n"); 1285 fprintf (stderr, "virtuals:\n"); 1286 virtuals = BINFO_VIRTUALS (elem); 1287 n = 0; 1288 1289 while (virtuals) 1290 { 1291 tree fndecl = TREE_VALUE (virtuals); 1292 fprintf (stderr, "%s [%ld =? %ld]\n", 1293 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)), 1294 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl))); 1295 ++n; 1296 virtuals = TREE_CHAIN (virtuals); 1297 } 1298 } 1299 1300 /* Build a representation for the qualified name SCOPE::NAME. TYPE is 1301 the type of the result expression, if known, or NULL_TREE if the 1302 resulting expression is type-dependent. If TEMPLATE_P is true, 1303 NAME is known to be a template because the user explicitly used the 1304 "template" keyword after the "::". 1305 1306 All SCOPE_REFs should be built by use of this function. */ 1307 1308 tree 1309 build_qualified_name (tree type, tree scope, tree name, bool template_p) 1310 { 1311 tree t; 1312 if (type == error_mark_node 1313 || scope == error_mark_node 1314 || name == error_mark_node) 1315 return error_mark_node; 1316 t = build2 (SCOPE_REF, type, scope, name); 1317 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p; 1318 if (type) 1319 t = convert_from_reference (t); 1320 return t; 1321 } 1322 1323 /* Returns nonzero if X is an expression for a (possibly overloaded) 1324 function. If "f" is a function or function template, "f", "c->f", 1325 "c.f", "C::f", and "f<int>" will all be considered possibly 1326 overloaded functions. Returns 2 if the function is actually 1327 overloaded, i.e., if it is impossible to know the type of the 1328 function without performing overload resolution. */ 1329 1330 int 1331 is_overloaded_fn (tree x) 1332 { 1333 /* A baselink is also considered an overloaded function. */ 1334 if (TREE_CODE (x) == OFFSET_REF 1335 || TREE_CODE (x) == COMPONENT_REF) 1336 x = TREE_OPERAND (x, 1); 1337 if (BASELINK_P (x)) 1338 x = BASELINK_FUNCTIONS (x); 1339 if (TREE_CODE (x) == TEMPLATE_ID_EXPR) 1340 x = TREE_OPERAND (x, 0); 1341 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x)) 1342 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x))) 1343 return 2; 1344 return (TREE_CODE (x) == FUNCTION_DECL 1345 || TREE_CODE (x) == OVERLOAD); 1346 } 1347 1348 /* Returns true iff X is an expression for an overloaded function 1349 whose type cannot be known without performing overload 1350 resolution. */ 1351 1352 bool 1353 really_overloaded_fn (tree x) 1354 { 1355 return is_overloaded_fn (x) == 2; 1356 } 1357 1358 tree 1359 get_first_fn (tree from) 1360 { 1361 gcc_assert (is_overloaded_fn (from)); 1362 /* A baselink is also considered an overloaded function. */ 1363 if (TREE_CODE (from) == OFFSET_REF 1364 || TREE_CODE (from) == COMPONENT_REF) 1365 from = TREE_OPERAND (from, 1); 1366 if (BASELINK_P (from)) 1367 from = BASELINK_FUNCTIONS (from); 1368 if (TREE_CODE (from) == TEMPLATE_ID_EXPR) 1369 from = TREE_OPERAND (from, 0); 1370 return OVL_CURRENT (from); 1371 } 1372 1373 /* Return a new OVL node, concatenating it with the old one. */ 1374 1375 tree 1376 ovl_cons (tree decl, tree chain) 1377 { 1378 tree result = make_node (OVERLOAD); 1379 TREE_TYPE (result) = unknown_type_node; 1380 OVL_FUNCTION (result) = decl; 1381 TREE_CHAIN (result) = chain; 1382 1383 return result; 1384 } 1385 1386 /* Build a new overloaded function. If this is the first one, 1387 just return it; otherwise, ovl_cons the _DECLs */ 1388 1389 tree 1390 build_overload (tree decl, tree chain) 1391 { 1392 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL) 1393 return decl; 1394 if (chain && TREE_CODE (chain) != OVERLOAD) 1395 chain = ovl_cons (chain, NULL_TREE); 1396 return ovl_cons (decl, chain); 1397 } 1398 1399 1400 #define PRINT_RING_SIZE 4 1401 1402 static const char * 1403 cxx_printable_name_internal (tree decl, int v, bool translate) 1404 { 1405 static unsigned int uid_ring[PRINT_RING_SIZE]; 1406 static char *print_ring[PRINT_RING_SIZE]; 1407 static bool trans_ring[PRINT_RING_SIZE]; 1408 static int ring_counter; 1409 int i; 1410 1411 /* Only cache functions. */ 1412 if (v < 2 1413 || TREE_CODE (decl) != FUNCTION_DECL 1414 || DECL_LANG_SPECIFIC (decl) == 0) 1415 return lang_decl_name (decl, v, translate); 1416 1417 /* See if this print name is lying around. */ 1418 for (i = 0; i < PRINT_RING_SIZE; i++) 1419 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i]) 1420 /* yes, so return it. */ 1421 return print_ring[i]; 1422 1423 if (++ring_counter == PRINT_RING_SIZE) 1424 ring_counter = 0; 1425 1426 if (current_function_decl != NULL_TREE) 1427 { 1428 /* There may be both translated and untranslated versions of the 1429 name cached. */ 1430 for (i = 0; i < 2; i++) 1431 { 1432 if (uid_ring[ring_counter] == DECL_UID (current_function_decl)) 1433 ring_counter += 1; 1434 if (ring_counter == PRINT_RING_SIZE) 1435 ring_counter = 0; 1436 } 1437 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl)); 1438 } 1439 1440 if (print_ring[ring_counter]) 1441 free (print_ring[ring_counter]); 1442 1443 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate)); 1444 uid_ring[ring_counter] = DECL_UID (decl); 1445 trans_ring[ring_counter] = translate; 1446 return print_ring[ring_counter]; 1447 } 1448 1449 const char * 1450 cxx_printable_name (tree decl, int v) 1451 { 1452 return cxx_printable_name_internal (decl, v, false); 1453 } 1454 1455 const char * 1456 cxx_printable_name_translate (tree decl, int v) 1457 { 1458 return cxx_printable_name_internal (decl, v, true); 1459 } 1460 1461 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions 1462 listed in RAISES. */ 1463 1464 tree 1465 build_exception_variant (tree type, tree raises) 1466 { 1467 tree v = TYPE_MAIN_VARIANT (type); 1468 int type_quals = TYPE_QUALS (type); 1469 1470 for (; v; v = TYPE_NEXT_VARIANT (v)) 1471 if (check_qualified_type (v, type, type_quals) 1472 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1)) 1473 return v; 1474 1475 /* Need to build a new variant. */ 1476 v = build_variant_type_copy (type); 1477 TYPE_RAISES_EXCEPTIONS (v) = raises; 1478 return v; 1479 } 1480 1481 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new 1482 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template 1483 arguments. */ 1484 1485 tree 1486 bind_template_template_parm (tree t, tree newargs) 1487 { 1488 tree decl = TYPE_NAME (t); 1489 tree t2; 1490 1491 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM); 1492 decl = build_decl (input_location, 1493 TYPE_DECL, DECL_NAME (decl), NULL_TREE); 1494 1495 /* These nodes have to be created to reflect new TYPE_DECL and template 1496 arguments. */ 1497 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t)); 1498 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl; 1499 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2) 1500 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs); 1501 1502 TREE_TYPE (decl) = t2; 1503 TYPE_NAME (t2) = decl; 1504 TYPE_STUB_DECL (t2) = decl; 1505 TYPE_SIZE (t2) = 0; 1506 SET_TYPE_STRUCTURAL_EQUALITY (t2); 1507 1508 return t2; 1509 } 1510 1511 /* Called from count_trees via walk_tree. */ 1512 1513 static tree 1514 count_trees_r (tree *tp, int *walk_subtrees, void *data) 1515 { 1516 ++*((int *) data); 1517 1518 if (TYPE_P (*tp)) 1519 *walk_subtrees = 0; 1520 1521 return NULL_TREE; 1522 } 1523 1524 /* Debugging function for measuring the rough complexity of a tree 1525 representation. */ 1526 1527 int 1528 count_trees (tree t) 1529 { 1530 int n_trees = 0; 1531 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees); 1532 return n_trees; 1533 } 1534 1535 /* Called from verify_stmt_tree via walk_tree. */ 1536 1537 static tree 1538 verify_stmt_tree_r (tree* tp, 1539 int* walk_subtrees ATTRIBUTE_UNUSED , 1540 void* data) 1541 { 1542 tree t = *tp; 1543 htab_t *statements = (htab_t *) data; 1544 void **slot; 1545 1546 if (!STATEMENT_CODE_P (TREE_CODE (t))) 1547 return NULL_TREE; 1548 1549 /* If this statement is already present in the hash table, then 1550 there is a circularity in the statement tree. */ 1551 gcc_assert (!htab_find (*statements, t)); 1552 1553 slot = htab_find_slot (*statements, t, INSERT); 1554 *slot = t; 1555 1556 return NULL_TREE; 1557 } 1558 1559 /* Debugging function to check that the statement T has not been 1560 corrupted. For now, this function simply checks that T contains no 1561 circularities. */ 1562 1563 void 1564 verify_stmt_tree (tree t) 1565 { 1566 htab_t statements; 1567 statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 1568 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL); 1569 htab_delete (statements); 1570 } 1571 1572 /* Check if the type T depends on a type with no linkage and if so, return 1573 it. If RELAXED_P then do not consider a class type declared within 1574 a vague-linkage function to have no linkage. */ 1575 1576 tree 1577 no_linkage_check (tree t, bool relaxed_p) 1578 { 1579 tree r; 1580 1581 /* There's no point in checking linkage on template functions; we 1582 can't know their complete types. */ 1583 if (processing_template_decl) 1584 return NULL_TREE; 1585 1586 switch (TREE_CODE (t)) 1587 { 1588 case RECORD_TYPE: 1589 if (TYPE_PTRMEMFUNC_P (t)) 1590 goto ptrmem; 1591 /* Lambda types that don't have mangling scope have no linkage. We 1592 check CLASSTYPE_LAMBDA_EXPR here rather than LAMBDA_TYPE_P because 1593 when we get here from pushtag none of the lambda information is 1594 set up yet, so we want to assume that the lambda has linkage and 1595 fix it up later if not. */ 1596 if (CLASSTYPE_LAMBDA_EXPR (t) 1597 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE) 1598 return t; 1599 /* Fall through. */ 1600 case UNION_TYPE: 1601 if (!CLASS_TYPE_P (t)) 1602 return NULL_TREE; 1603 /* Fall through. */ 1604 case ENUMERAL_TYPE: 1605 /* Only treat anonymous types as having no linkage if they're at 1606 namespace scope. This is core issue 966. */ 1607 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t)) 1608 return t; 1609 1610 for (r = CP_TYPE_CONTEXT (t); ; ) 1611 { 1612 /* If we're a nested type of a !TREE_PUBLIC class, we might not 1613 have linkage, or we might just be in an anonymous namespace. 1614 If we're in a TREE_PUBLIC class, we have linkage. */ 1615 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r))) 1616 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p); 1617 else if (TREE_CODE (r) == FUNCTION_DECL) 1618 { 1619 if (!relaxed_p || !vague_linkage_p (r)) 1620 return t; 1621 else 1622 r = CP_DECL_CONTEXT (r); 1623 } 1624 else 1625 break; 1626 } 1627 1628 return NULL_TREE; 1629 1630 case ARRAY_TYPE: 1631 case POINTER_TYPE: 1632 case REFERENCE_TYPE: 1633 return no_linkage_check (TREE_TYPE (t), relaxed_p); 1634 1635 case OFFSET_TYPE: 1636 ptrmem: 1637 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t), 1638 relaxed_p); 1639 if (r) 1640 return r; 1641 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p); 1642 1643 case METHOD_TYPE: 1644 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p); 1645 if (r) 1646 return r; 1647 /* Fall through. */ 1648 case FUNCTION_TYPE: 1649 { 1650 tree parm; 1651 for (parm = TYPE_ARG_TYPES (t); 1652 parm && parm != void_list_node; 1653 parm = TREE_CHAIN (parm)) 1654 { 1655 r = no_linkage_check (TREE_VALUE (parm), relaxed_p); 1656 if (r) 1657 return r; 1658 } 1659 return no_linkage_check (TREE_TYPE (t), relaxed_p); 1660 } 1661 1662 default: 1663 return NULL_TREE; 1664 } 1665 } 1666 1667 #ifdef GATHER_STATISTICS 1668 extern int depth_reached; 1669 #endif 1670 1671 void 1672 cxx_print_statistics (void) 1673 { 1674 print_search_statistics (); 1675 print_class_statistics (); 1676 #ifdef GATHER_STATISTICS 1677 fprintf (stderr, "maximum template instantiation depth reached: %d\n", 1678 depth_reached); 1679 #endif 1680 } 1681 1682 /* Return, as an INTEGER_CST node, the number of elements for TYPE 1683 (which is an ARRAY_TYPE). This counts only elements of the top 1684 array. */ 1685 1686 tree 1687 array_type_nelts_top (tree type) 1688 { 1689 return fold_build2_loc (input_location, 1690 PLUS_EXPR, sizetype, 1691 array_type_nelts (type), 1692 size_one_node); 1693 } 1694 1695 /* Return, as an INTEGER_CST node, the number of elements for TYPE 1696 (which is an ARRAY_TYPE). This one is a recursive count of all 1697 ARRAY_TYPEs that are clumped together. */ 1698 1699 tree 1700 array_type_nelts_total (tree type) 1701 { 1702 tree sz = array_type_nelts_top (type); 1703 type = TREE_TYPE (type); 1704 while (TREE_CODE (type) == ARRAY_TYPE) 1705 { 1706 tree n = array_type_nelts_top (type); 1707 sz = fold_build2_loc (input_location, 1708 MULT_EXPR, sizetype, sz, n); 1709 type = TREE_TYPE (type); 1710 } 1711 return sz; 1712 } 1713 1714 /* Called from break_out_target_exprs via mapcar. */ 1715 1716 static tree 1717 bot_manip (tree* tp, int* walk_subtrees, void* data) 1718 { 1719 splay_tree target_remap = ((splay_tree) data); 1720 tree t = *tp; 1721 1722 if (!TYPE_P (t) && TREE_CONSTANT (t)) 1723 { 1724 /* There can't be any TARGET_EXPRs or their slot variables below 1725 this point. We used to check !TREE_SIDE_EFFECTS, but then we 1726 failed to copy an ADDR_EXPR of the slot VAR_DECL. */ 1727 *walk_subtrees = 0; 1728 return NULL_TREE; 1729 } 1730 if (TREE_CODE (t) == TARGET_EXPR) 1731 { 1732 tree u; 1733 1734 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR) 1735 { 1736 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1)); 1737 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1))) 1738 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true; 1739 } 1740 else 1741 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t)); 1742 1743 /* Map the old variable to the new one. */ 1744 splay_tree_insert (target_remap, 1745 (splay_tree_key) TREE_OPERAND (t, 0), 1746 (splay_tree_value) TREE_OPERAND (u, 0)); 1747 1748 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1)); 1749 1750 /* Replace the old expression with the new version. */ 1751 *tp = u; 1752 /* We don't have to go below this point; the recursive call to 1753 break_out_target_exprs will have handled anything below this 1754 point. */ 1755 *walk_subtrees = 0; 1756 return NULL_TREE; 1757 } 1758 1759 /* Make a copy of this node. */ 1760 return copy_tree_r (tp, walk_subtrees, NULL); 1761 } 1762 1763 /* Replace all remapped VAR_DECLs in T with their new equivalents. 1764 DATA is really a splay-tree mapping old variables to new 1765 variables. */ 1766 1767 static tree 1768 bot_replace (tree* t, 1769 int* walk_subtrees ATTRIBUTE_UNUSED , 1770 void* data) 1771 { 1772 splay_tree target_remap = ((splay_tree) data); 1773 1774 if (TREE_CODE (*t) == VAR_DECL) 1775 { 1776 splay_tree_node n = splay_tree_lookup (target_remap, 1777 (splay_tree_key) *t); 1778 if (n) 1779 *t = (tree) n->value; 1780 } 1781 1782 return NULL_TREE; 1783 } 1784 1785 /* When we parse a default argument expression, we may create 1786 temporary variables via TARGET_EXPRs. When we actually use the 1787 default-argument expression, we make a copy of the expression, but 1788 we must replace the temporaries with appropriate local versions. */ 1789 1790 tree 1791 break_out_target_exprs (tree t) 1792 { 1793 static int target_remap_count; 1794 static splay_tree target_remap; 1795 1796 if (!target_remap_count++) 1797 target_remap = splay_tree_new (splay_tree_compare_pointers, 1798 /*splay_tree_delete_key_fn=*/NULL, 1799 /*splay_tree_delete_value_fn=*/NULL); 1800 cp_walk_tree (&t, bot_manip, target_remap, NULL); 1801 cp_walk_tree (&t, bot_replace, target_remap, NULL); 1802 1803 if (!--target_remap_count) 1804 { 1805 splay_tree_delete (target_remap); 1806 target_remap = NULL; 1807 } 1808 1809 return t; 1810 } 1811 1812 /* Similar to `build_nt', but for template definitions of dependent 1813 expressions */ 1814 1815 tree 1816 build_min_nt (enum tree_code code, ...) 1817 { 1818 tree t; 1819 int length; 1820 int i; 1821 va_list p; 1822 1823 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 1824 1825 va_start (p, code); 1826 1827 t = make_node (code); 1828 length = TREE_CODE_LENGTH (code); 1829 1830 for (i = 0; i < length; i++) 1831 { 1832 tree x = va_arg (p, tree); 1833 TREE_OPERAND (t, i) = x; 1834 } 1835 1836 va_end (p); 1837 return t; 1838 } 1839 1840 1841 /* Similar to `build', but for template definitions. */ 1842 1843 tree 1844 build_min (enum tree_code code, tree tt, ...) 1845 { 1846 tree t; 1847 int length; 1848 int i; 1849 va_list p; 1850 1851 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 1852 1853 va_start (p, tt); 1854 1855 t = make_node (code); 1856 length = TREE_CODE_LENGTH (code); 1857 TREE_TYPE (t) = tt; 1858 1859 for (i = 0; i < length; i++) 1860 { 1861 tree x = va_arg (p, tree); 1862 TREE_OPERAND (t, i) = x; 1863 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x)) 1864 TREE_SIDE_EFFECTS (t) = 1; 1865 } 1866 1867 va_end (p); 1868 return t; 1869 } 1870 1871 /* Similar to `build', but for template definitions of non-dependent 1872 expressions. NON_DEP is the non-dependent expression that has been 1873 built. */ 1874 1875 tree 1876 build_min_non_dep (enum tree_code code, tree non_dep, ...) 1877 { 1878 tree t; 1879 int length; 1880 int i; 1881 va_list p; 1882 1883 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); 1884 1885 va_start (p, non_dep); 1886 1887 t = make_node (code); 1888 length = TREE_CODE_LENGTH (code); 1889 TREE_TYPE (t) = TREE_TYPE (non_dep); 1890 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep); 1891 1892 for (i = 0; i < length; i++) 1893 { 1894 tree x = va_arg (p, tree); 1895 TREE_OPERAND (t, i) = x; 1896 } 1897 1898 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR) 1899 /* This should not be considered a COMPOUND_EXPR, because it 1900 resolves to an overload. */ 1901 COMPOUND_EXPR_OVERLOADED (t) = 1; 1902 1903 va_end (p); 1904 return t; 1905 } 1906 1907 /* Similar to `build_call_list', but for template definitions of non-dependent 1908 expressions. NON_DEP is the non-dependent expression that has been 1909 built. */ 1910 1911 tree 1912 build_min_non_dep_call_vec (tree non_dep, tree fn, VEC(tree,gc) *argvec) 1913 { 1914 tree t = build_nt_call_vec (fn, argvec); 1915 TREE_TYPE (t) = TREE_TYPE (non_dep); 1916 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep); 1917 return t; 1918 } 1919 1920 tree 1921 get_type_decl (tree t) 1922 { 1923 if (TREE_CODE (t) == TYPE_DECL) 1924 return t; 1925 if (TYPE_P (t)) 1926 return TYPE_STUB_DECL (t); 1927 gcc_assert (t == error_mark_node); 1928 return t; 1929 } 1930 1931 /* Returns the namespace that contains DECL, whether directly or 1932 indirectly. */ 1933 1934 tree 1935 decl_namespace_context (tree decl) 1936 { 1937 while (1) 1938 { 1939 if (TREE_CODE (decl) == NAMESPACE_DECL) 1940 return decl; 1941 else if (TYPE_P (decl)) 1942 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl)); 1943 else 1944 decl = CP_DECL_CONTEXT (decl); 1945 } 1946 } 1947 1948 /* Returns true if decl is within an anonymous namespace, however deeply 1949 nested, or false otherwise. */ 1950 1951 bool 1952 decl_anon_ns_mem_p (const_tree decl) 1953 { 1954 while (1) 1955 { 1956 if (decl == NULL_TREE || decl == error_mark_node) 1957 return false; 1958 if (TREE_CODE (decl) == NAMESPACE_DECL 1959 && DECL_NAME (decl) == NULL_TREE) 1960 return true; 1961 /* Classes and namespaces inside anonymous namespaces have 1962 TREE_PUBLIC == 0, so we can shortcut the search. */ 1963 else if (TYPE_P (decl)) 1964 return (TREE_PUBLIC (TYPE_NAME (decl)) == 0); 1965 else if (TREE_CODE (decl) == NAMESPACE_DECL) 1966 return (TREE_PUBLIC (decl) == 0); 1967 else 1968 decl = DECL_CONTEXT (decl); 1969 } 1970 } 1971 1972 /* Return truthvalue of whether T1 is the same tree structure as T2. 1973 Return 1 if they are the same. Return 0 if they are different. */ 1974 1975 bool 1976 cp_tree_equal (tree t1, tree t2) 1977 { 1978 enum tree_code code1, code2; 1979 1980 if (t1 == t2) 1981 return true; 1982 if (!t1 || !t2) 1983 return false; 1984 1985 for (code1 = TREE_CODE (t1); 1986 CONVERT_EXPR_CODE_P (code1) 1987 || code1 == NON_LVALUE_EXPR; 1988 code1 = TREE_CODE (t1)) 1989 t1 = TREE_OPERAND (t1, 0); 1990 for (code2 = TREE_CODE (t2); 1991 CONVERT_EXPR_CODE_P (code2) 1992 || code1 == NON_LVALUE_EXPR; 1993 code2 = TREE_CODE (t2)) 1994 t2 = TREE_OPERAND (t2, 0); 1995 1996 /* They might have become equal now. */ 1997 if (t1 == t2) 1998 return true; 1999 2000 if (code1 != code2) 2001 return false; 2002 2003 switch (code1) 2004 { 2005 case INTEGER_CST: 2006 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2) 2007 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2); 2008 2009 case REAL_CST: 2010 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2)); 2011 2012 case STRING_CST: 2013 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2) 2014 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), 2015 TREE_STRING_LENGTH (t1)); 2016 2017 case FIXED_CST: 2018 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), 2019 TREE_FIXED_CST (t2)); 2020 2021 case COMPLEX_CST: 2022 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2)) 2023 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2)); 2024 2025 case CONSTRUCTOR: 2026 /* We need to do this when determining whether or not two 2027 non-type pointer to member function template arguments 2028 are the same. */ 2029 if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) 2030 /* The first operand is RTL. */ 2031 && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0))) 2032 return false; 2033 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); 2034 2035 case TREE_LIST: 2036 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))) 2037 return false; 2038 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2))) 2039 return false; 2040 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2)); 2041 2042 case SAVE_EXPR: 2043 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 2044 2045 case CALL_EXPR: 2046 { 2047 tree arg1, arg2; 2048 call_expr_arg_iterator iter1, iter2; 2049 if (!cp_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2))) 2050 return false; 2051 for (arg1 = first_call_expr_arg (t1, &iter1), 2052 arg2 = first_call_expr_arg (t2, &iter2); 2053 arg1 && arg2; 2054 arg1 = next_call_expr_arg (&iter1), 2055 arg2 = next_call_expr_arg (&iter2)) 2056 if (!cp_tree_equal (arg1, arg2)) 2057 return false; 2058 if (arg1 || arg2) 2059 return false; 2060 return true; 2061 } 2062 2063 case TARGET_EXPR: 2064 { 2065 tree o1 = TREE_OPERAND (t1, 0); 2066 tree o2 = TREE_OPERAND (t2, 0); 2067 2068 /* Special case: if either target is an unallocated VAR_DECL, 2069 it means that it's going to be unified with whatever the 2070 TARGET_EXPR is really supposed to initialize, so treat it 2071 as being equivalent to anything. */ 2072 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE 2073 && !DECL_RTL_SET_P (o1)) 2074 /*Nop*/; 2075 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE 2076 && !DECL_RTL_SET_P (o2)) 2077 /*Nop*/; 2078 else if (!cp_tree_equal (o1, o2)) 2079 return false; 2080 2081 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)); 2082 } 2083 2084 case WITH_CLEANUP_EXPR: 2085 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))) 2086 return false; 2087 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1)); 2088 2089 case COMPONENT_REF: 2090 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1)) 2091 return false; 2092 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); 2093 2094 case PARM_DECL: 2095 /* For comparing uses of parameters in late-specified return types 2096 with an out-of-class definition of the function, but can also come 2097 up for expressions that involve 'this' in a member function 2098 template. */ 2099 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))) 2100 { 2101 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2)) 2102 return false; 2103 if (DECL_ARTIFICIAL (t1) 2104 || DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)) 2105 return true; 2106 } 2107 return false; 2108 2109 case VAR_DECL: 2110 case CONST_DECL: 2111 case FUNCTION_DECL: 2112 case TEMPLATE_DECL: 2113 case IDENTIFIER_NODE: 2114 case SSA_NAME: 2115 return false; 2116 2117 case BASELINK: 2118 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2) 2119 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2) 2120 && cp_tree_equal (BASELINK_FUNCTIONS (t1), 2121 BASELINK_FUNCTIONS (t2))); 2122 2123 case TEMPLATE_PARM_INDEX: 2124 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2) 2125 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2) 2126 && (TEMPLATE_PARM_PARAMETER_PACK (t1) 2127 == TEMPLATE_PARM_PARAMETER_PACK (t2)) 2128 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)), 2129 TREE_TYPE (TEMPLATE_PARM_DECL (t2)))); 2130 2131 case TEMPLATE_ID_EXPR: 2132 { 2133 unsigned ix; 2134 tree vec1, vec2; 2135 2136 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))) 2137 return false; 2138 vec1 = TREE_OPERAND (t1, 1); 2139 vec2 = TREE_OPERAND (t2, 1); 2140 2141 if (!vec1 || !vec2) 2142 return !vec1 && !vec2; 2143 2144 if (TREE_VEC_LENGTH (vec1) != TREE_VEC_LENGTH (vec2)) 2145 return false; 2146 2147 for (ix = TREE_VEC_LENGTH (vec1); ix--;) 2148 if (!cp_tree_equal (TREE_VEC_ELT (vec1, ix), 2149 TREE_VEC_ELT (vec2, ix))) 2150 return false; 2151 2152 return true; 2153 } 2154 2155 case SIZEOF_EXPR: 2156 case ALIGNOF_EXPR: 2157 { 2158 tree o1 = TREE_OPERAND (t1, 0); 2159 tree o2 = TREE_OPERAND (t2, 0); 2160 2161 if (TREE_CODE (o1) != TREE_CODE (o2)) 2162 return false; 2163 if (TYPE_P (o1)) 2164 return same_type_p (o1, o2); 2165 else 2166 return cp_tree_equal (o1, o2); 2167 } 2168 2169 case MODOP_EXPR: 2170 { 2171 tree t1_op1, t2_op1; 2172 2173 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))) 2174 return false; 2175 2176 t1_op1 = TREE_OPERAND (t1, 1); 2177 t2_op1 = TREE_OPERAND (t2, 1); 2178 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1)) 2179 return false; 2180 2181 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2)); 2182 } 2183 2184 case PTRMEM_CST: 2185 /* Two pointer-to-members are the same if they point to the same 2186 field or function in the same class. */ 2187 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2)) 2188 return false; 2189 2190 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)); 2191 2192 case OVERLOAD: 2193 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2)) 2194 return false; 2195 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2)); 2196 2197 case TRAIT_EXPR: 2198 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2)) 2199 return false; 2200 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2)) 2201 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2)); 2202 2203 default: 2204 break; 2205 } 2206 2207 switch (TREE_CODE_CLASS (code1)) 2208 { 2209 case tcc_unary: 2210 case tcc_binary: 2211 case tcc_comparison: 2212 case tcc_expression: 2213 case tcc_vl_exp: 2214 case tcc_reference: 2215 case tcc_statement: 2216 { 2217 int i, n; 2218 2219 n = TREE_OPERAND_LENGTH (t1); 2220 if (TREE_CODE_CLASS (code1) == tcc_vl_exp 2221 && n != TREE_OPERAND_LENGTH (t2)) 2222 return false; 2223 2224 for (i = 0; i < n; ++i) 2225 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i))) 2226 return false; 2227 2228 return true; 2229 } 2230 2231 case tcc_type: 2232 return same_type_p (t1, t2); 2233 default: 2234 gcc_unreachable (); 2235 } 2236 /* We can get here with --disable-checking. */ 2237 return false; 2238 } 2239 2240 /* The type of ARG when used as an lvalue. */ 2241 2242 tree 2243 lvalue_type (tree arg) 2244 { 2245 tree type = TREE_TYPE (arg); 2246 return type; 2247 } 2248 2249 /* The type of ARG for printing error messages; denote lvalues with 2250 reference types. */ 2251 2252 tree 2253 error_type (tree arg) 2254 { 2255 tree type = TREE_TYPE (arg); 2256 2257 if (TREE_CODE (type) == ARRAY_TYPE) 2258 ; 2259 else if (TREE_CODE (type) == ERROR_MARK) 2260 ; 2261 else if (real_lvalue_p (arg)) 2262 type = build_reference_type (lvalue_type (arg)); 2263 else if (MAYBE_CLASS_TYPE_P (type)) 2264 type = lvalue_type (arg); 2265 2266 return type; 2267 } 2268 2269 /* Does FUNCTION use a variable-length argument list? */ 2270 2271 int 2272 varargs_function_p (const_tree function) 2273 { 2274 const_tree parm = TYPE_ARG_TYPES (TREE_TYPE (function)); 2275 for (; parm; parm = TREE_CHAIN (parm)) 2276 if (TREE_VALUE (parm) == void_type_node) 2277 return 0; 2278 return 1; 2279 } 2280 2281 /* Returns 1 if decl is a member of a class. */ 2282 2283 int 2284 member_p (const_tree decl) 2285 { 2286 const_tree const ctx = DECL_CONTEXT (decl); 2287 return (ctx && TYPE_P (ctx)); 2288 } 2289 2290 /* Create a placeholder for member access where we don't actually have an 2291 object that the access is against. */ 2292 2293 tree 2294 build_dummy_object (tree type) 2295 { 2296 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node); 2297 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error); 2298 } 2299 2300 /* We've gotten a reference to a member of TYPE. Return *this if appropriate, 2301 or a dummy object otherwise. If BINFOP is non-0, it is filled with the 2302 binfo path from current_class_type to TYPE, or 0. */ 2303 2304 tree 2305 maybe_dummy_object (tree type, tree* binfop) 2306 { 2307 tree decl, context; 2308 tree binfo; 2309 2310 if (current_class_type 2311 && (binfo = lookup_base (current_class_type, type, 2312 ba_unique | ba_quiet, NULL))) 2313 context = current_class_type; 2314 else 2315 { 2316 /* Reference from a nested class member function. */ 2317 context = type; 2318 binfo = TYPE_BINFO (type); 2319 } 2320 2321 if (binfop) 2322 *binfop = binfo; 2323 2324 if (current_class_ref && context == current_class_type 2325 /* Kludge: Make sure that current_class_type is actually 2326 correct. It might not be if we're in the middle of 2327 tsubst_default_argument. */ 2328 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)), 2329 current_class_type)) 2330 decl = current_class_ref; 2331 else if (current_class_ref && LAMBDA_TYPE_P (current_class_type) 2332 && context == nonlambda_method_basetype ()) 2333 /* In a lambda, need to go through 'this' capture. */ 2334 decl = (cp_build_indirect_ref 2335 ((lambda_expr_this_capture 2336 (CLASSTYPE_LAMBDA_EXPR (current_class_type))), 2337 RO_NULL, tf_warning_or_error)); 2338 else 2339 decl = build_dummy_object (context); 2340 2341 return decl; 2342 } 2343 2344 /* Returns 1 if OB is a placeholder object, or a pointer to one. */ 2345 2346 int 2347 is_dummy_object (const_tree ob) 2348 { 2349 if (TREE_CODE (ob) == INDIRECT_REF) 2350 ob = TREE_OPERAND (ob, 0); 2351 return (TREE_CODE (ob) == NOP_EXPR 2352 && TREE_OPERAND (ob, 0) == void_zero_node); 2353 } 2354 2355 /* Returns 1 iff type T is something we want to treat as a scalar type for 2356 the purpose of deciding whether it is trivial/POD/standard-layout. */ 2357 2358 static bool 2359 scalarish_type_p (const_tree t) 2360 { 2361 if (t == error_mark_node) 2362 return 1; 2363 2364 return (SCALAR_TYPE_P (t) 2365 || TREE_CODE (t) == VECTOR_TYPE); 2366 } 2367 2368 /* Returns true iff T requires non-trivial default initialization. */ 2369 2370 bool 2371 type_has_nontrivial_default_init (const_tree t) 2372 { 2373 t = strip_array_types (CONST_CAST_TREE (t)); 2374 2375 if (CLASS_TYPE_P (t)) 2376 return TYPE_HAS_COMPLEX_DFLT (t); 2377 else 2378 return 0; 2379 } 2380 2381 /* Returns true iff copying an object of type T is non-trivial. */ 2382 2383 bool 2384 type_has_nontrivial_copy_init (const_tree t) 2385 { 2386 t = strip_array_types (CONST_CAST_TREE (t)); 2387 2388 if (CLASS_TYPE_P (t)) 2389 return TYPE_HAS_COMPLEX_INIT_REF (t); 2390 else 2391 return 0; 2392 } 2393 2394 /* Returns 1 iff type T is a trivial type, as defined in [basic.types]. */ 2395 2396 bool 2397 trivial_type_p (const_tree t) 2398 { 2399 t = strip_array_types (CONST_CAST_TREE (t)); 2400 2401 if (CLASS_TYPE_P (t)) 2402 return (TYPE_HAS_TRIVIAL_DFLT (t) 2403 && TYPE_HAS_TRIVIAL_INIT_REF (t) 2404 && TYPE_HAS_TRIVIAL_ASSIGN_REF (t) 2405 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t)); 2406 else 2407 return scalarish_type_p (t); 2408 } 2409 2410 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */ 2411 2412 bool 2413 pod_type_p (const_tree t) 2414 { 2415 /* This CONST_CAST is okay because strip_array_types returns its 2416 argument unmodified and we assign it to a const_tree. */ 2417 t = strip_array_types (CONST_CAST_TREE(t)); 2418 2419 if (!CLASS_TYPE_P (t)) 2420 return scalarish_type_p (t); 2421 else if (cxx_dialect > cxx98) 2422 /* [class]/10: A POD struct is a class that is both a trivial class and a 2423 standard-layout class, and has no non-static data members of type 2424 non-POD struct, non-POD union (or array of such types). 2425 2426 We don't need to check individual members because if a member is 2427 non-std-layout or non-trivial, the class will be too. */ 2428 return (std_layout_type_p (t) && trivial_type_p (t)); 2429 else 2430 /* The C++98 definition of POD is different. */ 2431 return !CLASSTYPE_NON_LAYOUT_POD_P (t); 2432 } 2433 2434 /* Returns true iff T is POD for the purpose of layout, as defined in the 2435 C++ ABI. */ 2436 2437 bool 2438 layout_pod_type_p (const_tree t) 2439 { 2440 t = strip_array_types (CONST_CAST_TREE (t)); 2441 2442 if (CLASS_TYPE_P (t)) 2443 return !CLASSTYPE_NON_LAYOUT_POD_P (t); 2444 else 2445 return scalarish_type_p (t); 2446 } 2447 2448 /* Returns true iff T is a standard-layout type, as defined in 2449 [basic.types]. */ 2450 2451 bool 2452 std_layout_type_p (const_tree t) 2453 { 2454 t = strip_array_types (CONST_CAST_TREE (t)); 2455 2456 if (CLASS_TYPE_P (t)) 2457 return !CLASSTYPE_NON_STD_LAYOUT (t); 2458 else 2459 return scalarish_type_p (t); 2460 } 2461 2462 /* Nonzero iff type T is a class template implicit specialization. */ 2463 2464 bool 2465 class_tmpl_impl_spec_p (const_tree t) 2466 { 2467 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t); 2468 } 2469 2470 /* Returns 1 iff zero initialization of type T means actually storing 2471 zeros in it. */ 2472 2473 int 2474 zero_init_p (const_tree t) 2475 { 2476 /* This CONST_CAST is okay because strip_array_types returns its 2477 argument unmodified and we assign it to a const_tree. */ 2478 t = strip_array_types (CONST_CAST_TREE(t)); 2479 2480 if (t == error_mark_node) 2481 return 1; 2482 2483 /* NULL pointers to data members are initialized with -1. */ 2484 if (TYPE_PTRMEM_P (t)) 2485 return 0; 2486 2487 /* Classes that contain types that can't be zero-initialized, cannot 2488 be zero-initialized themselves. */ 2489 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t)) 2490 return 0; 2491 2492 return 1; 2493 } 2494 2495 /* Table of valid C++ attributes. */ 2496 const struct attribute_spec cxx_attribute_table[] = 2497 { 2498 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ 2499 { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute }, 2500 { "com_interface", 0, 0, false, false, false, handle_com_interface_attribute }, 2501 { "init_priority", 1, 1, true, false, false, handle_init_priority_attribute }, 2502 { NULL, 0, 0, false, false, false, NULL } 2503 }; 2504 2505 /* Handle a "java_interface" attribute; arguments as in 2506 struct attribute_spec.handler. */ 2507 static tree 2508 handle_java_interface_attribute (tree* node, 2509 tree name, 2510 tree args ATTRIBUTE_UNUSED , 2511 int flags, 2512 bool* no_add_attrs) 2513 { 2514 if (DECL_P (*node) 2515 || !CLASS_TYPE_P (*node) 2516 || !TYPE_FOR_JAVA (*node)) 2517 { 2518 error ("%qE attribute can only be applied to Java class definitions", 2519 name); 2520 *no_add_attrs = true; 2521 return NULL_TREE; 2522 } 2523 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) 2524 *node = build_variant_type_copy (*node); 2525 TYPE_JAVA_INTERFACE (*node) = 1; 2526 2527 return NULL_TREE; 2528 } 2529 2530 /* Handle a "com_interface" attribute; arguments as in 2531 struct attribute_spec.handler. */ 2532 static tree 2533 handle_com_interface_attribute (tree* node, 2534 tree name, 2535 tree args ATTRIBUTE_UNUSED , 2536 int flags ATTRIBUTE_UNUSED , 2537 bool* no_add_attrs) 2538 { 2539 static int warned; 2540 2541 *no_add_attrs = true; 2542 2543 if (DECL_P (*node) 2544 || !CLASS_TYPE_P (*node) 2545 || *node != TYPE_MAIN_VARIANT (*node)) 2546 { 2547 warning (OPT_Wattributes, "%qE attribute can only be applied " 2548 "to class definitions", name); 2549 return NULL_TREE; 2550 } 2551 2552 if (!warned++) 2553 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default", 2554 name); 2555 2556 return NULL_TREE; 2557 } 2558 2559 /* Handle an "init_priority" attribute; arguments as in 2560 struct attribute_spec.handler. */ 2561 static tree 2562 handle_init_priority_attribute (tree* node, 2563 tree name, 2564 tree args, 2565 int flags ATTRIBUTE_UNUSED , 2566 bool* no_add_attrs) 2567 { 2568 tree initp_expr = TREE_VALUE (args); 2569 tree decl = *node; 2570 tree type = TREE_TYPE (decl); 2571 int pri; 2572 2573 STRIP_NOPS (initp_expr); 2574 2575 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST) 2576 { 2577 error ("requested init_priority is not an integer constant"); 2578 *no_add_attrs = true; 2579 return NULL_TREE; 2580 } 2581 2582 pri = TREE_INT_CST_LOW (initp_expr); 2583 2584 type = strip_array_types (type); 2585 2586 if (decl == NULL_TREE 2587 || TREE_CODE (decl) != VAR_DECL 2588 || !TREE_STATIC (decl) 2589 || DECL_EXTERNAL (decl) 2590 || (TREE_CODE (type) != RECORD_TYPE 2591 && TREE_CODE (type) != UNION_TYPE) 2592 /* Static objects in functions are initialized the 2593 first time control passes through that 2594 function. This is not precise enough to pin down an 2595 init_priority value, so don't allow it. */ 2596 || current_function_decl) 2597 { 2598 error ("can only use %qE attribute on file-scope definitions " 2599 "of objects of class type", name); 2600 *no_add_attrs = true; 2601 return NULL_TREE; 2602 } 2603 2604 if (pri > MAX_INIT_PRIORITY || pri <= 0) 2605 { 2606 error ("requested init_priority is out of range"); 2607 *no_add_attrs = true; 2608 return NULL_TREE; 2609 } 2610 2611 /* Check for init_priorities that are reserved for 2612 language and runtime support implementations.*/ 2613 if (pri <= MAX_RESERVED_INIT_PRIORITY) 2614 { 2615 warning 2616 (0, "requested init_priority is reserved for internal use"); 2617 } 2618 2619 if (SUPPORTS_INIT_PRIORITY) 2620 { 2621 SET_DECL_INIT_PRIORITY (decl, pri); 2622 DECL_HAS_INIT_PRIORITY_P (decl) = 1; 2623 return NULL_TREE; 2624 } 2625 else 2626 { 2627 error ("%qE attribute is not supported on this platform", name); 2628 *no_add_attrs = true; 2629 return NULL_TREE; 2630 } 2631 } 2632 2633 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the 2634 thing pointed to by the constant. */ 2635 2636 tree 2637 make_ptrmem_cst (tree type, tree member) 2638 { 2639 tree ptrmem_cst = make_node (PTRMEM_CST); 2640 TREE_TYPE (ptrmem_cst) = type; 2641 PTRMEM_CST_MEMBER (ptrmem_cst) = member; 2642 return ptrmem_cst; 2643 } 2644 2645 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May 2646 return an existing type if an appropriate type already exists. */ 2647 2648 tree 2649 cp_build_type_attribute_variant (tree type, tree attributes) 2650 { 2651 tree new_type; 2652 2653 new_type = build_type_attribute_variant (type, attributes); 2654 if ((TREE_CODE (new_type) == FUNCTION_TYPE 2655 || TREE_CODE (new_type) == METHOD_TYPE) 2656 && (TYPE_RAISES_EXCEPTIONS (new_type) 2657 != TYPE_RAISES_EXCEPTIONS (type))) 2658 new_type = build_exception_variant (new_type, 2659 TYPE_RAISES_EXCEPTIONS (type)); 2660 2661 /* Making a new main variant of a class type is broken. */ 2662 gcc_assert (!CLASS_TYPE_P (type) || new_type == type); 2663 2664 return new_type; 2665 } 2666 2667 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes. 2668 Called only after doing all language independent checks. Only 2669 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already 2670 compared in type_hash_eq. */ 2671 2672 bool 2673 cxx_type_hash_eq (const_tree typea, const_tree typeb) 2674 { 2675 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE); 2676 2677 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea), 2678 TYPE_RAISES_EXCEPTIONS (typeb), 1); 2679 } 2680 2681 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order 2682 traversal. Called from walk_tree. */ 2683 2684 tree 2685 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func, 2686 void *data, struct pointer_set_t *pset) 2687 { 2688 enum tree_code code = TREE_CODE (*tp); 2689 tree result; 2690 2691 #define WALK_SUBTREE(NODE) \ 2692 do \ 2693 { \ 2694 result = cp_walk_tree (&(NODE), func, data, pset); \ 2695 if (result) goto out; \ 2696 } \ 2697 while (0) 2698 2699 /* Not one of the easy cases. We must explicitly go through the 2700 children. */ 2701 result = NULL_TREE; 2702 switch (code) 2703 { 2704 case DEFAULT_ARG: 2705 case TEMPLATE_TEMPLATE_PARM: 2706 case BOUND_TEMPLATE_TEMPLATE_PARM: 2707 case UNBOUND_CLASS_TEMPLATE: 2708 case TEMPLATE_PARM_INDEX: 2709 case TEMPLATE_TYPE_PARM: 2710 case TYPENAME_TYPE: 2711 case TYPEOF_TYPE: 2712 /* None of these have subtrees other than those already walked 2713 above. */ 2714 *walk_subtrees_p = 0; 2715 break; 2716 2717 case BASELINK: 2718 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp)); 2719 *walk_subtrees_p = 0; 2720 break; 2721 2722 case PTRMEM_CST: 2723 WALK_SUBTREE (TREE_TYPE (*tp)); 2724 *walk_subtrees_p = 0; 2725 break; 2726 2727 case TREE_LIST: 2728 WALK_SUBTREE (TREE_PURPOSE (*tp)); 2729 break; 2730 2731 case OVERLOAD: 2732 WALK_SUBTREE (OVL_FUNCTION (*tp)); 2733 WALK_SUBTREE (OVL_CHAIN (*tp)); 2734 *walk_subtrees_p = 0; 2735 break; 2736 2737 case USING_DECL: 2738 WALK_SUBTREE (DECL_NAME (*tp)); 2739 WALK_SUBTREE (USING_DECL_SCOPE (*tp)); 2740 WALK_SUBTREE (USING_DECL_DECLS (*tp)); 2741 *walk_subtrees_p = 0; 2742 break; 2743 2744 case RECORD_TYPE: 2745 if (TYPE_PTRMEMFUNC_P (*tp)) 2746 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp)); 2747 break; 2748 2749 case TYPE_ARGUMENT_PACK: 2750 case NONTYPE_ARGUMENT_PACK: 2751 { 2752 tree args = ARGUMENT_PACK_ARGS (*tp); 2753 int i, len = TREE_VEC_LENGTH (args); 2754 for (i = 0; i < len; i++) 2755 WALK_SUBTREE (TREE_VEC_ELT (args, i)); 2756 } 2757 break; 2758 2759 case TYPE_PACK_EXPANSION: 2760 WALK_SUBTREE (TREE_TYPE (*tp)); 2761 *walk_subtrees_p = 0; 2762 break; 2763 2764 case EXPR_PACK_EXPANSION: 2765 WALK_SUBTREE (TREE_OPERAND (*tp, 0)); 2766 *walk_subtrees_p = 0; 2767 break; 2768 2769 case CAST_EXPR: 2770 case REINTERPRET_CAST_EXPR: 2771 case STATIC_CAST_EXPR: 2772 case CONST_CAST_EXPR: 2773 case DYNAMIC_CAST_EXPR: 2774 if (TREE_TYPE (*tp)) 2775 WALK_SUBTREE (TREE_TYPE (*tp)); 2776 2777 { 2778 int i; 2779 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i) 2780 WALK_SUBTREE (TREE_OPERAND (*tp, i)); 2781 } 2782 *walk_subtrees_p = 0; 2783 break; 2784 2785 case TRAIT_EXPR: 2786 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp)); 2787 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp)); 2788 *walk_subtrees_p = 0; 2789 break; 2790 2791 case DECLTYPE_TYPE: 2792 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp)); 2793 *walk_subtrees_p = 0; 2794 break; 2795 2796 2797 default: 2798 return NULL_TREE; 2799 } 2800 2801 /* We didn't find what we were looking for. */ 2802 out: 2803 return result; 2804 2805 #undef WALK_SUBTREE 2806 } 2807 2808 /* Like save_expr, but for C++. */ 2809 2810 tree 2811 cp_save_expr (tree expr) 2812 { 2813 /* There is no reason to create a SAVE_EXPR within a template; if 2814 needed, we can create the SAVE_EXPR when instantiating the 2815 template. Furthermore, the middle-end cannot handle C++-specific 2816 tree codes. */ 2817 if (processing_template_decl) 2818 return expr; 2819 return save_expr (expr); 2820 } 2821 2822 /* Initialize tree.c. */ 2823 2824 void 2825 init_tree (void) 2826 { 2827 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL); 2828 } 2829 2830 /* Returns the kind of special function that DECL (a FUNCTION_DECL) 2831 is. Note that sfk_none is zero, so this function can be used as a 2832 predicate to test whether or not DECL is a special function. */ 2833 2834 special_function_kind 2835 special_function_p (const_tree decl) 2836 { 2837 /* Rather than doing all this stuff with magic names, we should 2838 probably have a field of type `special_function_kind' in 2839 DECL_LANG_SPECIFIC. */ 2840 if (DECL_COPY_CONSTRUCTOR_P (decl)) 2841 return sfk_copy_constructor; 2842 if (DECL_MOVE_CONSTRUCTOR_P (decl)) 2843 return sfk_move_constructor; 2844 if (DECL_CONSTRUCTOR_P (decl)) 2845 return sfk_constructor; 2846 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) 2847 return sfk_assignment_operator; 2848 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)) 2849 return sfk_destructor; 2850 if (DECL_COMPLETE_DESTRUCTOR_P (decl)) 2851 return sfk_complete_destructor; 2852 if (DECL_BASE_DESTRUCTOR_P (decl)) 2853 return sfk_base_destructor; 2854 if (DECL_DELETING_DESTRUCTOR_P (decl)) 2855 return sfk_deleting_destructor; 2856 if (DECL_CONV_FN_P (decl)) 2857 return sfk_conversion; 2858 2859 return sfk_none; 2860 } 2861 2862 /* Returns nonzero if TYPE is a character type, including wchar_t. */ 2863 2864 int 2865 char_type_p (tree type) 2866 { 2867 return (same_type_p (type, char_type_node) 2868 || same_type_p (type, unsigned_char_type_node) 2869 || same_type_p (type, signed_char_type_node) 2870 || same_type_p (type, char16_type_node) 2871 || same_type_p (type, char32_type_node) 2872 || same_type_p (type, wchar_type_node)); 2873 } 2874 2875 /* Returns the kind of linkage associated with the indicated DECL. Th 2876 value returned is as specified by the language standard; it is 2877 independent of implementation details regarding template 2878 instantiation, etc. For example, it is possible that a declaration 2879 to which this function assigns external linkage would not show up 2880 as a global symbol when you run `nm' on the resulting object file. */ 2881 2882 linkage_kind 2883 decl_linkage (tree decl) 2884 { 2885 /* This function doesn't attempt to calculate the linkage from first 2886 principles as given in [basic.link]. Instead, it makes use of 2887 the fact that we have already set TREE_PUBLIC appropriately, and 2888 then handles a few special cases. Ideally, we would calculate 2889 linkage first, and then transform that into a concrete 2890 implementation. */ 2891 2892 /* Things that don't have names have no linkage. */ 2893 if (!DECL_NAME (decl)) 2894 return lk_none; 2895 2896 /* Fields have no linkage. */ 2897 if (TREE_CODE (decl) == FIELD_DECL) 2898 return lk_none; 2899 2900 /* Things that are TREE_PUBLIC have external linkage. */ 2901 if (TREE_PUBLIC (decl)) 2902 return lk_external; 2903 2904 if (TREE_CODE (decl) == NAMESPACE_DECL) 2905 return lk_external; 2906 2907 /* Linkage of a CONST_DECL depends on the linkage of the enumeration 2908 type. */ 2909 if (TREE_CODE (decl) == CONST_DECL) 2910 return decl_linkage (TYPE_NAME (TREE_TYPE (decl))); 2911 2912 /* Some things that are not TREE_PUBLIC have external linkage, too. 2913 For example, on targets that don't have weak symbols, we make all 2914 template instantiations have internal linkage (in the object 2915 file), but the symbols should still be treated as having external 2916 linkage from the point of view of the language. */ 2917 if ((TREE_CODE (decl) == FUNCTION_DECL 2918 || TREE_CODE (decl) == VAR_DECL) 2919 && DECL_COMDAT (decl)) 2920 return lk_external; 2921 2922 /* Things in local scope do not have linkage, if they don't have 2923 TREE_PUBLIC set. */ 2924 if (decl_function_context (decl)) 2925 return lk_none; 2926 2927 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but 2928 are considered to have external linkage for language purposes. DECLs 2929 really meant to have internal linkage have DECL_THIS_STATIC set. */ 2930 if (TREE_CODE (decl) == TYPE_DECL) 2931 return lk_external; 2932 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 2933 { 2934 if (!DECL_THIS_STATIC (decl)) 2935 return lk_external; 2936 2937 /* Static data members and static member functions from classes 2938 in anonymous namespace also don't have TREE_PUBLIC set. */ 2939 if (DECL_CLASS_CONTEXT (decl)) 2940 return lk_external; 2941 } 2942 2943 /* Everything else has internal linkage. */ 2944 return lk_internal; 2945 } 2946 2947 /* EXP is an expression that we want to pre-evaluate. Returns (in 2948 *INITP) an expression that will perform the pre-evaluation. The 2949 value returned by this function is a side-effect free expression 2950 equivalent to the pre-evaluated expression. Callers must ensure 2951 that *INITP is evaluated before EXP. */ 2952 2953 tree 2954 stabilize_expr (tree exp, tree* initp) 2955 { 2956 tree init_expr; 2957 2958 if (!TREE_SIDE_EFFECTS (exp)) 2959 init_expr = NULL_TREE; 2960 else if (!real_lvalue_p (exp) 2961 || (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)) 2962 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (exp)))) 2963 { 2964 init_expr = get_target_expr (exp); 2965 exp = TARGET_EXPR_SLOT (init_expr); 2966 } 2967 else 2968 { 2969 exp = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error); 2970 init_expr = get_target_expr (exp); 2971 exp = TARGET_EXPR_SLOT (init_expr); 2972 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error); 2973 } 2974 *initp = init_expr; 2975 2976 gcc_assert (!TREE_SIDE_EFFECTS (exp)); 2977 return exp; 2978 } 2979 2980 /* Add NEW_EXPR, an expression whose value we don't care about, after the 2981 similar expression ORIG. */ 2982 2983 tree 2984 add_stmt_to_compound (tree orig, tree new_expr) 2985 { 2986 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr)) 2987 return orig; 2988 if (!orig || !TREE_SIDE_EFFECTS (orig)) 2989 return new_expr; 2990 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr); 2991 } 2992 2993 /* Like stabilize_expr, but for a call whose arguments we want to 2994 pre-evaluate. CALL is modified in place to use the pre-evaluated 2995 arguments, while, upon return, *INITP contains an expression to 2996 compute the arguments. */ 2997 2998 void 2999 stabilize_call (tree call, tree *initp) 3000 { 3001 tree inits = NULL_TREE; 3002 int i; 3003 int nargs = call_expr_nargs (call); 3004 3005 if (call == error_mark_node || processing_template_decl) 3006 { 3007 *initp = NULL_TREE; 3008 return; 3009 } 3010 3011 gcc_assert (TREE_CODE (call) == CALL_EXPR); 3012 3013 for (i = 0; i < nargs; i++) 3014 { 3015 tree init; 3016 CALL_EXPR_ARG (call, i) = 3017 stabilize_expr (CALL_EXPR_ARG (call, i), &init); 3018 inits = add_stmt_to_compound (inits, init); 3019 } 3020 3021 *initp = inits; 3022 } 3023 3024 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want 3025 to pre-evaluate. CALL is modified in place to use the pre-evaluated 3026 arguments, while, upon return, *INITP contains an expression to 3027 compute the arguments. */ 3028 3029 void 3030 stabilize_aggr_init (tree call, tree *initp) 3031 { 3032 tree inits = NULL_TREE; 3033 int i; 3034 int nargs = aggr_init_expr_nargs (call); 3035 3036 if (call == error_mark_node) 3037 return; 3038 3039 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR); 3040 3041 for (i = 0; i < nargs; i++) 3042 { 3043 tree init; 3044 AGGR_INIT_EXPR_ARG (call, i) = 3045 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init); 3046 inits = add_stmt_to_compound (inits, init); 3047 } 3048 3049 *initp = inits; 3050 } 3051 3052 /* Like stabilize_expr, but for an initialization. 3053 3054 If the initialization is for an object of class type, this function 3055 takes care not to introduce additional temporaries. 3056 3057 Returns TRUE iff the expression was successfully pre-evaluated, 3058 i.e., if INIT is now side-effect free, except for, possible, a 3059 single call to a constructor. */ 3060 3061 bool 3062 stabilize_init (tree init, tree *initp) 3063 { 3064 tree t = init; 3065 3066 *initp = NULL_TREE; 3067 3068 if (t == error_mark_node || processing_template_decl) 3069 return true; 3070 3071 if (TREE_CODE (t) == INIT_EXPR 3072 && TREE_CODE (TREE_OPERAND (t, 1)) != TARGET_EXPR 3073 && TREE_CODE (TREE_OPERAND (t, 1)) != AGGR_INIT_EXPR) 3074 { 3075 TREE_OPERAND (t, 1) = stabilize_expr (TREE_OPERAND (t, 1), initp); 3076 return true; 3077 } 3078 3079 if (TREE_CODE (t) == INIT_EXPR) 3080 t = TREE_OPERAND (t, 1); 3081 if (TREE_CODE (t) == TARGET_EXPR) 3082 t = TARGET_EXPR_INITIAL (t); 3083 if (TREE_CODE (t) == COMPOUND_EXPR) 3084 t = expr_last (t); 3085 if (TREE_CODE (t) == CONSTRUCTOR 3086 && EMPTY_CONSTRUCTOR_P (t)) 3087 /* Default-initialization. */ 3088 return true; 3089 3090 /* If the initializer is a COND_EXPR, we can't preevaluate 3091 anything. */ 3092 if (TREE_CODE (t) == COND_EXPR) 3093 return false; 3094 3095 if (TREE_CODE (t) == CALL_EXPR) 3096 { 3097 stabilize_call (t, initp); 3098 return true; 3099 } 3100 3101 if (TREE_CODE (t) == AGGR_INIT_EXPR) 3102 { 3103 stabilize_aggr_init (t, initp); 3104 return true; 3105 } 3106 3107 /* The initialization is being performed via a bitwise copy -- and 3108 the item copied may have side effects. */ 3109 return TREE_SIDE_EFFECTS (init); 3110 } 3111 3112 /* Like "fold", but should be used whenever we might be processing the 3113 body of a template. */ 3114 3115 tree 3116 fold_if_not_in_template (tree expr) 3117 { 3118 /* In the body of a template, there is never any need to call 3119 "fold". We will call fold later when actually instantiating the 3120 template. Integral constant expressions in templates will be 3121 evaluated via fold_non_dependent_expr, as necessary. */ 3122 if (processing_template_decl) 3123 return expr; 3124 3125 /* Fold C++ front-end specific tree codes. */ 3126 if (TREE_CODE (expr) == UNARY_PLUS_EXPR) 3127 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0)); 3128 3129 return fold (expr); 3130 } 3131 3132 /* Returns true if a cast to TYPE may appear in an integral constant 3133 expression. */ 3134 3135 bool 3136 cast_valid_in_integral_constant_expression_p (tree type) 3137 { 3138 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type) 3139 || dependent_type_p (type) 3140 || type == error_mark_node); 3141 } 3142 3143 /* Return true if we need to fix linkage information of DECL. */ 3144 3145 static bool 3146 cp_fix_function_decl_p (tree decl) 3147 { 3148 /* Skip if DECL is not externally visible. */ 3149 if (!TREE_PUBLIC (decl)) 3150 return false; 3151 3152 /* We need to fix DECL if it a appears to be exported but with no 3153 function body. Thunks do not have CFGs and we may need to 3154 handle them specially later. */ 3155 if (!gimple_has_body_p (decl) 3156 && !DECL_THUNK_P (decl) 3157 && !DECL_EXTERNAL (decl)) 3158 { 3159 struct cgraph_node *node = cgraph_get_node (decl); 3160 3161 /* Don't fix same_body aliases. Although they don't have their own 3162 CFG, they share it with what they alias to. */ 3163 if (!node 3164 || node->decl == decl 3165 || !node->same_body) 3166 return true; 3167 } 3168 3169 return false; 3170 } 3171 3172 /* Clean the C++ specific parts of the tree T. */ 3173 3174 void 3175 cp_free_lang_data (tree t) 3176 { 3177 if (TREE_CODE (t) == METHOD_TYPE 3178 || TREE_CODE (t) == FUNCTION_TYPE) 3179 { 3180 /* Default args are not interesting anymore. */ 3181 tree argtypes = TYPE_ARG_TYPES (t); 3182 while (argtypes) 3183 { 3184 TREE_PURPOSE (argtypes) = 0; 3185 argtypes = TREE_CHAIN (argtypes); 3186 } 3187 } 3188 else if (TREE_CODE (t) == FUNCTION_DECL 3189 && cp_fix_function_decl_p (t)) 3190 { 3191 /* If T is used in this translation unit at all, the definition 3192 must exist somewhere else since we have decided to not emit it 3193 in this TU. So make it an external reference. */ 3194 DECL_EXTERNAL (t) = 1; 3195 TREE_STATIC (t) = 0; 3196 } 3197 if (CP_AGGREGATE_TYPE_P (t) 3198 && TYPE_NAME (t)) 3199 { 3200 tree name = TYPE_NAME (t); 3201 if (TREE_CODE (name) == TYPE_DECL) 3202 name = DECL_NAME (name); 3203 /* Drop anonymous names. */ 3204 if (name != NULL_TREE 3205 && ANON_AGGRNAME_P (name)) 3206 TYPE_NAME (t) = NULL_TREE; 3207 } 3208 } 3209 3210 3211 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007) 3212 /* Complain that some language-specific thing hanging off a tree 3213 node has been accessed improperly. */ 3214 3215 void 3216 lang_check_failed (const char* file, int line, const char* function) 3217 { 3218 internal_error ("lang_* check: failed in %s, at %s:%d", 3219 function, trim_filename (file), line); 3220 } 3221 #endif /* ENABLE_TREE_CHECKING */ 3222 3223 #include "gt-cp-tree.h" 3224