1/* Breadth-first and depth-first routines for 2 searching multiple-inheritance lattice for GNU C++. 3 Copyright (C) 1987-2015 Free Software Foundation, Inc. 4 Contributed by Michael Tiemann (tiemann@cygnus.com) 5 6This file is part of GCC. 7 8GCC is free software; you can redistribute it and/or modify 9it under the terms of the GNU General Public License as published by 10the Free Software Foundation; either version 3, or (at your option) 11any later version. 12 13GCC is distributed in the hope that it will be useful, 14but WITHOUT ANY WARRANTY; without even the implied warranty of 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16GNU General Public License for more details. 17 18You should have received a copy of the GNU General Public License 19along with GCC; see the file COPYING3. If not see 20<http://www.gnu.org/licenses/>. */ 21 22/* High-level class interface. */ 23 24#include "config.h" 25#include "system.h" 26#include "coretypes.h" 27#include "tm.h" 28#include "hash-set.h" 29#include "machmode.h" 30#include "vec.h" 31#include "double-int.h" 32#include "input.h" 33#include "alias.h" 34#include "symtab.h" 35#include "wide-int.h" 36#include "inchash.h" 37#include "tree.h" 38#include "cp-tree.h" 39#include "intl.h" 40#include "flags.h" 41#include "toplev.h" 42#include "target.h" 43 44static int is_subobject_of_p (tree, tree); 45static tree dfs_lookup_base (tree, void *); 46static tree dfs_dcast_hint_pre (tree, void *); 47static tree dfs_dcast_hint_post (tree, void *); 48static tree dfs_debug_mark (tree, void *); 49static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *), 50 tree (*post_fn) (tree, void *), void *data); 51static void dfs_unmark_r (tree); 52static int check_hidden_convs (tree, int, int, tree, tree, tree); 53static tree split_conversions (tree, tree, tree, tree); 54static int lookup_conversions_r (tree, int, int, 55 tree, tree, tree, tree, tree *, tree *); 56static int look_for_overrides_r (tree, tree); 57static tree lookup_field_r (tree, void *); 58static tree dfs_accessible_post (tree, void *); 59static tree dfs_walk_once_accessible_r (tree, bool, bool, 60 tree (*pre_fn) (tree, void *), 61 tree (*post_fn) (tree, void *), 62 void *data); 63static tree dfs_walk_once_accessible (tree, bool, 64 tree (*pre_fn) (tree, void *), 65 tree (*post_fn) (tree, void *), 66 void *data); 67static tree dfs_access_in_type (tree, void *); 68static access_kind access_in_type (tree, tree); 69static int protected_accessible_p (tree, tree, tree); 70static int friend_accessible_p (tree, tree, tree); 71static tree dfs_get_pure_virtuals (tree, void *); 72 73 74/* Variables for gathering statistics. */ 75static int n_fields_searched; 76static int n_calls_lookup_field, n_calls_lookup_field_1; 77static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1; 78static int n_calls_get_base_type; 79static int n_outer_fields_searched; 80static int n_contexts_saved; 81 82 83/* Data for lookup_base and its workers. */ 84 85struct lookup_base_data_s 86{ 87 tree t; /* type being searched. */ 88 tree base; /* The base type we're looking for. */ 89 tree binfo; /* Found binfo. */ 90 bool via_virtual; /* Found via a virtual path. */ 91 bool ambiguous; /* Found multiply ambiguous */ 92 bool repeated_base; /* Whether there are repeated bases in the 93 hierarchy. */ 94 bool want_any; /* Whether we want any matching binfo. */ 95}; 96 97/* Worker function for lookup_base. See if we've found the desired 98 base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S). */ 99 100static tree 101dfs_lookup_base (tree binfo, void *data_) 102{ 103 struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_; 104 105 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base)) 106 { 107 if (!data->binfo) 108 { 109 data->binfo = binfo; 110 data->via_virtual 111 = binfo_via_virtual (data->binfo, data->t) != NULL_TREE; 112 113 if (!data->repeated_base) 114 /* If there are no repeated bases, we can stop now. */ 115 return binfo; 116 117 if (data->want_any && !data->via_virtual) 118 /* If this is a non-virtual base, then we can't do 119 better. */ 120 return binfo; 121 122 return dfs_skip_bases; 123 } 124 else 125 { 126 gcc_assert (binfo != data->binfo); 127 128 /* We've found more than one matching binfo. */ 129 if (!data->want_any) 130 { 131 /* This is immediately ambiguous. */ 132 data->binfo = NULL_TREE; 133 data->ambiguous = true; 134 return error_mark_node; 135 } 136 137 /* Prefer one via a non-virtual path. */ 138 if (!binfo_via_virtual (binfo, data->t)) 139 { 140 data->binfo = binfo; 141 data->via_virtual = false; 142 return binfo; 143 } 144 145 /* There must be repeated bases, otherwise we'd have stopped 146 on the first base we found. */ 147 return dfs_skip_bases; 148 } 149 } 150 151 return NULL_TREE; 152} 153 154/* Returns true if type BASE is accessible in T. (BASE is known to be 155 a (possibly non-proper) base class of T.) If CONSIDER_LOCAL_P is 156 true, consider any special access of the current scope, or access 157 bestowed by friendship. */ 158 159bool 160accessible_base_p (tree t, tree base, bool consider_local_p) 161{ 162 tree decl; 163 164 /* [class.access.base] 165 166 A base class is said to be accessible if an invented public 167 member of the base class is accessible. 168 169 If BASE is a non-proper base, this condition is trivially 170 true. */ 171 if (same_type_p (t, base)) 172 return true; 173 /* Rather than inventing a public member, we use the implicit 174 public typedef created in the scope of every class. */ 175 decl = TYPE_FIELDS (base); 176 while (!DECL_SELF_REFERENCE_P (decl)) 177 decl = DECL_CHAIN (decl); 178 while (ANON_AGGR_TYPE_P (t)) 179 t = TYPE_CONTEXT (t); 180 return accessible_p (t, decl, consider_local_p); 181} 182 183/* Lookup BASE in the hierarchy dominated by T. Do access checking as 184 ACCESS specifies. Return the binfo we discover. If KIND_PTR is 185 non-NULL, fill with information about what kind of base we 186 discovered. 187 188 If the base is inaccessible, or ambiguous, then error_mark_node is 189 returned. If the tf_error bit of COMPLAIN is not set, no error 190 is issued. */ 191 192tree 193lookup_base (tree t, tree base, base_access access, 194 base_kind *kind_ptr, tsubst_flags_t complain) 195{ 196 tree binfo; 197 tree t_binfo; 198 base_kind bk; 199 200 /* "Nothing" is definitely not derived from Base. */ 201 if (t == NULL_TREE) 202 { 203 if (kind_ptr) 204 *kind_ptr = bk_not_base; 205 return NULL_TREE; 206 } 207 208 if (t == error_mark_node || base == error_mark_node) 209 { 210 if (kind_ptr) 211 *kind_ptr = bk_not_base; 212 return error_mark_node; 213 } 214 gcc_assert (TYPE_P (base)); 215 216 if (!TYPE_P (t)) 217 { 218 t_binfo = t; 219 t = BINFO_TYPE (t); 220 } 221 else 222 { 223 t = complete_type (TYPE_MAIN_VARIANT (t)); 224 t_binfo = TYPE_BINFO (t); 225 } 226 227 base = TYPE_MAIN_VARIANT (base); 228 229 /* If BASE is incomplete, it can't be a base of T--and instantiating it 230 might cause an error. */ 231 if (t_binfo && CLASS_TYPE_P (base) && COMPLETE_OR_OPEN_TYPE_P (base)) 232 { 233 struct lookup_base_data_s data; 234 235 data.t = t; 236 data.base = base; 237 data.binfo = NULL_TREE; 238 data.ambiguous = data.via_virtual = false; 239 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t); 240 data.want_any = access == ba_any; 241 242 dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data); 243 binfo = data.binfo; 244 245 if (!binfo) 246 bk = data.ambiguous ? bk_ambig : bk_not_base; 247 else if (binfo == t_binfo) 248 bk = bk_same_type; 249 else if (data.via_virtual) 250 bk = bk_via_virtual; 251 else 252 bk = bk_proper_base; 253 } 254 else 255 { 256 binfo = NULL_TREE; 257 bk = bk_not_base; 258 } 259 260 /* Check that the base is unambiguous and accessible. */ 261 if (access != ba_any) 262 switch (bk) 263 { 264 case bk_not_base: 265 break; 266 267 case bk_ambig: 268 if (complain & tf_error) 269 error ("%qT is an ambiguous base of %qT", base, t); 270 binfo = error_mark_node; 271 break; 272 273 default: 274 if ((access & ba_check_bit) 275 /* If BASE is incomplete, then BASE and TYPE are probably 276 the same, in which case BASE is accessible. If they 277 are not the same, then TYPE is invalid. In that case, 278 there's no need to issue another error here, and 279 there's no implicit typedef to use in the code that 280 follows, so we skip the check. */ 281 && COMPLETE_TYPE_P (base) 282 && !accessible_base_p (t, base, !(access & ba_ignore_scope))) 283 { 284 if (complain & tf_error) 285 error ("%qT is an inaccessible base of %qT", base, t); 286 binfo = error_mark_node; 287 bk = bk_inaccessible; 288 } 289 break; 290 } 291 292 if (kind_ptr) 293 *kind_ptr = bk; 294 295 return binfo; 296} 297 298/* Data for dcast_base_hint walker. */ 299 300struct dcast_data_s 301{ 302 tree subtype; /* The base type we're looking for. */ 303 int virt_depth; /* Number of virtual bases encountered from most 304 derived. */ 305 tree offset; /* Best hint offset discovered so far. */ 306 bool repeated_base; /* Whether there are repeated bases in the 307 hierarchy. */ 308}; 309 310/* Worker for dcast_base_hint. Search for the base type being cast 311 from. */ 312 313static tree 314dfs_dcast_hint_pre (tree binfo, void *data_) 315{ 316 struct dcast_data_s *data = (struct dcast_data_s *) data_; 317 318 if (BINFO_VIRTUAL_P (binfo)) 319 data->virt_depth++; 320 321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype)) 322 { 323 if (data->virt_depth) 324 { 325 data->offset = ssize_int (-1); 326 return data->offset; 327 } 328 if (data->offset) 329 data->offset = ssize_int (-3); 330 else 331 data->offset = BINFO_OFFSET (binfo); 332 333 return data->repeated_base ? dfs_skip_bases : data->offset; 334 } 335 336 return NULL_TREE; 337} 338 339/* Worker for dcast_base_hint. Track the virtual depth. */ 340 341static tree 342dfs_dcast_hint_post (tree binfo, void *data_) 343{ 344 struct dcast_data_s *data = (struct dcast_data_s *) data_; 345 346 if (BINFO_VIRTUAL_P (binfo)) 347 data->virt_depth--; 348 349 return NULL_TREE; 350} 351 352/* The dynamic cast runtime needs a hint about how the static SUBTYPE type 353 started from is related to the required TARGET type, in order to optimize 354 the inheritance graph search. This information is independent of the 355 current context, and ignores private paths, hence get_base_distance is 356 inappropriate. Return a TREE specifying the base offset, BOFF. 357 BOFF >= 0, there is only one public non-virtual SUBTYPE base at offset BOFF, 358 and there are no public virtual SUBTYPE bases. 359 BOFF == -1, SUBTYPE occurs as multiple public virtual or non-virtual bases. 360 BOFF == -2, SUBTYPE is not a public base. 361 BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases. */ 362 363tree 364dcast_base_hint (tree subtype, tree target) 365{ 366 struct dcast_data_s data; 367 368 data.subtype = subtype; 369 data.virt_depth = 0; 370 data.offset = NULL_TREE; 371 data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target); 372 373 dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false, 374 dfs_dcast_hint_pre, dfs_dcast_hint_post, &data); 375 return data.offset ? data.offset : ssize_int (-2); 376} 377 378/* Search for a member with name NAME in a multiple inheritance 379 lattice specified by TYPE. If it does not exist, return NULL_TREE. 380 If the member is ambiguously referenced, return `error_mark_node'. 381 Otherwise, return a DECL with the indicated name. If WANT_TYPE is 382 true, type declarations are preferred. */ 383 384/* Do a 1-level search for NAME as a member of TYPE. The caller must 385 figure out whether it can access this field. (Since it is only one 386 level, this is reasonable.) */ 387 388tree 389lookup_field_1 (tree type, tree name, bool want_type) 390{ 391 tree field; 392 393 gcc_assert (identifier_p (name)); 394 395 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM 396 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM 397 || TREE_CODE (type) == TYPENAME_TYPE) 398 /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and 399 BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all; 400 instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX. (Miraculously, 401 the code often worked even when we treated the index as a list 402 of fields!) 403 The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME. */ 404 return NULL_TREE; 405 406 if (CLASSTYPE_SORTED_FIELDS (type)) 407 { 408 tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0]; 409 int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len; 410 int i; 411 412 while (lo < hi) 413 { 414 i = (lo + hi) / 2; 415 416 if (GATHER_STATISTICS) 417 n_fields_searched++; 418 419 if (DECL_NAME (fields[i]) > name) 420 hi = i; 421 else if (DECL_NAME (fields[i]) < name) 422 lo = i + 1; 423 else 424 { 425 field = NULL_TREE; 426 427 /* We might have a nested class and a field with the 428 same name; we sorted them appropriately via 429 field_decl_cmp, so just look for the first or last 430 field with this name. */ 431 if (want_type) 432 { 433 do 434 field = fields[i--]; 435 while (i >= lo && DECL_NAME (fields[i]) == name); 436 if (!DECL_DECLARES_TYPE_P (field)) 437 field = NULL_TREE; 438 } 439 else 440 { 441 do 442 field = fields[i++]; 443 while (i < hi && DECL_NAME (fields[i]) == name); 444 } 445 446 if (field) 447 { 448 field = strip_using_decl (field); 449 if (is_overloaded_fn (field)) 450 field = NULL_TREE; 451 } 452 453 return field; 454 } 455 } 456 return NULL_TREE; 457 } 458 459 field = TYPE_FIELDS (type); 460 461 if (GATHER_STATISTICS) 462 n_calls_lookup_field_1++; 463 464 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 465 { 466 tree decl = field; 467 468 if (GATHER_STATISTICS) 469 n_fields_searched++; 470 471 gcc_assert (DECL_P (field)); 472 if (DECL_NAME (field) == NULL_TREE 473 && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 474 { 475 tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type); 476 if (temp) 477 return temp; 478 } 479 480 if (TREE_CODE (decl) == USING_DECL 481 && DECL_NAME (decl) == name) 482 { 483 decl = strip_using_decl (decl); 484 if (is_overloaded_fn (decl)) 485 continue; 486 } 487 488 if (DECL_NAME (decl) == name 489 && (!want_type || DECL_DECLARES_TYPE_P (decl))) 490 return decl; 491 } 492 /* Not found. */ 493 if (name == vptr_identifier) 494 { 495 /* Give the user what s/he thinks s/he wants. */ 496 if (TYPE_POLYMORPHIC_P (type)) 497 return TYPE_VFIELD (type); 498 } 499 return NULL_TREE; 500} 501 502/* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or 503 NAMESPACE_DECL corresponding to the innermost non-block scope. */ 504 505tree 506current_scope (void) 507{ 508 /* There are a number of cases we need to be aware of here: 509 current_class_type current_function_decl 510 global NULL NULL 511 fn-local NULL SET 512 class-local SET NULL 513 class->fn SET SET 514 fn->class SET SET 515 516 Those last two make life interesting. If we're in a function which is 517 itself inside a class, we need decls to go into the fn's decls (our 518 second case below). But if we're in a class and the class itself is 519 inside a function, we need decls to go into the decls for the class. To 520 achieve this last goal, we must see if, when both current_class_ptr and 521 current_function_decl are set, the class was declared inside that 522 function. If so, we know to put the decls into the class's scope. */ 523 if (current_function_decl && current_class_type 524 && ((DECL_FUNCTION_MEMBER_P (current_function_decl) 525 && same_type_p (DECL_CONTEXT (current_function_decl), 526 current_class_type)) 527 || (DECL_FRIEND_CONTEXT (current_function_decl) 528 && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl), 529 current_class_type)))) 530 return current_function_decl; 531 if (current_class_type) 532 return current_class_type; 533 if (current_function_decl) 534 return current_function_decl; 535 return current_namespace; 536} 537 538/* Returns nonzero if we are currently in a function scope. Note 539 that this function returns zero if we are within a local class, but 540 not within a member function body of the local class. */ 541 542int 543at_function_scope_p (void) 544{ 545 tree cs = current_scope (); 546 /* Also check cfun to make sure that we're really compiling 547 this function (as opposed to having set current_function_decl 548 for access checking or some such). */ 549 return (cs && TREE_CODE (cs) == FUNCTION_DECL 550 && cfun && cfun->decl == current_function_decl); 551} 552 553/* Returns true if the innermost active scope is a class scope. */ 554 555bool 556at_class_scope_p (void) 557{ 558 tree cs = current_scope (); 559 return cs && TYPE_P (cs); 560} 561 562/* Returns true if the innermost active scope is a namespace scope. */ 563 564bool 565at_namespace_scope_p (void) 566{ 567 tree cs = current_scope (); 568 return cs && TREE_CODE (cs) == NAMESPACE_DECL; 569} 570 571/* Return the scope of DECL, as appropriate when doing name-lookup. */ 572 573tree 574context_for_name_lookup (tree decl) 575{ 576 /* [class.union] 577 578 For the purposes of name lookup, after the anonymous union 579 definition, the members of the anonymous union are considered to 580 have been defined in the scope in which the anonymous union is 581 declared. */ 582 tree context = DECL_CONTEXT (decl); 583 584 while (context && TYPE_P (context) 585 && (ANON_AGGR_TYPE_P (context) || UNSCOPED_ENUM_P (context))) 586 context = TYPE_CONTEXT (context); 587 if (!context) 588 context = global_namespace; 589 590 return context; 591} 592 593/* The accessibility routines use BINFO_ACCESS for scratch space 594 during the computation of the accessibility of some declaration. */ 595 596#define BINFO_ACCESS(NODE) \ 597 ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE))) 598 599/* Set the access associated with NODE to ACCESS. */ 600 601#define SET_BINFO_ACCESS(NODE, ACCESS) \ 602 ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \ 603 (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0)) 604 605/* Called from access_in_type via dfs_walk. Calculate the access to 606 DATA (which is really a DECL) in BINFO. */ 607 608static tree 609dfs_access_in_type (tree binfo, void *data) 610{ 611 tree decl = (tree) data; 612 tree type = BINFO_TYPE (binfo); 613 access_kind access = ak_none; 614 615 if (context_for_name_lookup (decl) == type) 616 { 617 /* If we have descended to the scope of DECL, just note the 618 appropriate access. */ 619 if (TREE_PRIVATE (decl)) 620 access = ak_private; 621 else if (TREE_PROTECTED (decl)) 622 access = ak_protected; 623 else 624 access = ak_public; 625 } 626 else 627 { 628 /* First, check for an access-declaration that gives us more 629 access to the DECL. */ 630 if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl)) 631 { 632 tree decl_access = purpose_member (type, DECL_ACCESS (decl)); 633 634 if (decl_access) 635 { 636 decl_access = TREE_VALUE (decl_access); 637 638 if (decl_access == access_public_node) 639 access = ak_public; 640 else if (decl_access == access_protected_node) 641 access = ak_protected; 642 else if (decl_access == access_private_node) 643 access = ak_private; 644 else 645 gcc_unreachable (); 646 } 647 } 648 649 if (!access) 650 { 651 int i; 652 tree base_binfo; 653 vec<tree, va_gc> *accesses; 654 655 /* Otherwise, scan our baseclasses, and pick the most favorable 656 access. */ 657 accesses = BINFO_BASE_ACCESSES (binfo); 658 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 659 { 660 tree base_access = (*accesses)[i]; 661 access_kind base_access_now = BINFO_ACCESS (base_binfo); 662 663 if (base_access_now == ak_none || base_access_now == ak_private) 664 /* If it was not accessible in the base, or only 665 accessible as a private member, we can't access it 666 all. */ 667 base_access_now = ak_none; 668 else if (base_access == access_protected_node) 669 /* Public and protected members in the base become 670 protected here. */ 671 base_access_now = ak_protected; 672 else if (base_access == access_private_node) 673 /* Public and protected members in the base become 674 private here. */ 675 base_access_now = ak_private; 676 677 /* See if the new access, via this base, gives more 678 access than our previous best access. */ 679 if (base_access_now != ak_none 680 && (access == ak_none || base_access_now < access)) 681 { 682 access = base_access_now; 683 684 /* If the new access is public, we can't do better. */ 685 if (access == ak_public) 686 break; 687 } 688 } 689 } 690 } 691 692 /* Note the access to DECL in TYPE. */ 693 SET_BINFO_ACCESS (binfo, access); 694 695 return NULL_TREE; 696} 697 698/* Return the access to DECL in TYPE. */ 699 700static access_kind 701access_in_type (tree type, tree decl) 702{ 703 tree binfo = TYPE_BINFO (type); 704 705 /* We must take into account 706 707 [class.paths] 708 709 If a name can be reached by several paths through a multiple 710 inheritance graph, the access is that of the path that gives 711 most access. 712 713 The algorithm we use is to make a post-order depth-first traversal 714 of the base-class hierarchy. As we come up the tree, we annotate 715 each node with the most lenient access. */ 716 dfs_walk_once (binfo, NULL, dfs_access_in_type, decl); 717 718 return BINFO_ACCESS (binfo); 719} 720 721/* Returns nonzero if it is OK to access DECL through an object 722 indicated by BINFO in the context of DERIVED. */ 723 724static int 725protected_accessible_p (tree decl, tree derived, tree binfo) 726{ 727 access_kind access; 728 729 /* We're checking this clause from [class.access.base] 730 731 m as a member of N is protected, and the reference occurs in a 732 member or friend of class N, or in a member or friend of a 733 class P derived from N, where m as a member of P is public, private 734 or protected. 735 736 Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N. */ 737 738 /* If DERIVED isn't derived from N, then it can't be a P. */ 739 if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived)) 740 return 0; 741 742 access = access_in_type (derived, decl); 743 744 /* If m is inaccessible in DERIVED, then it's not a P. */ 745 if (access == ak_none) 746 return 0; 747 748 /* [class.protected] 749 750 When a friend or a member function of a derived class references 751 a protected nonstatic member of a base class, an access check 752 applies in addition to those described earlier in clause 753 _class.access_) Except when forming a pointer to member 754 (_expr.unary.op_), the access must be through a pointer to, 755 reference to, or object of the derived class itself (or any class 756 derived from that class) (_expr.ref_). If the access is to form 757 a pointer to member, the nested-name-specifier shall name the 758 derived class (or any class derived from that class). */ 759 if (DECL_NONSTATIC_MEMBER_P (decl)) 760 { 761 /* We can tell through what the reference is occurring by 762 chasing BINFO up to the root. */ 763 tree t = binfo; 764 while (BINFO_INHERITANCE_CHAIN (t)) 765 t = BINFO_INHERITANCE_CHAIN (t); 766 767 if (!DERIVED_FROM_P (derived, BINFO_TYPE (t))) 768 return 0; 769 } 770 771 return 1; 772} 773 774/* Returns nonzero if SCOPE is a friend of a type which would be able 775 to access DECL through the object indicated by BINFO. */ 776 777static int 778friend_accessible_p (tree scope, tree decl, tree binfo) 779{ 780 tree befriending_classes; 781 tree t; 782 783 if (!scope) 784 return 0; 785 786 if (DECL_DECLARES_FUNCTION_P (scope)) 787 befriending_classes = DECL_BEFRIENDING_CLASSES (scope); 788 else if (TYPE_P (scope)) 789 befriending_classes = CLASSTYPE_BEFRIENDING_CLASSES (scope); 790 else 791 return 0; 792 793 for (t = befriending_classes; t; t = TREE_CHAIN (t)) 794 if (protected_accessible_p (decl, TREE_VALUE (t), binfo)) 795 return 1; 796 797 /* Nested classes have the same access as their enclosing types, as 798 per DR 45 (this is a change from the standard). */ 799 if (TYPE_P (scope)) 800 for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t)) 801 if (protected_accessible_p (decl, t, binfo)) 802 return 1; 803 804 if (DECL_DECLARES_FUNCTION_P (scope)) 805 { 806 /* Perhaps this SCOPE is a member of a class which is a 807 friend. */ 808 if (DECL_CLASS_SCOPE_P (scope) 809 && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo)) 810 return 1; 811 812 /* Or an instantiation of something which is a friend. */ 813 if (DECL_TEMPLATE_INFO (scope)) 814 { 815 int ret; 816 /* Increment processing_template_decl to make sure that 817 dependent_type_p works correctly. */ 818 ++processing_template_decl; 819 ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo); 820 --processing_template_decl; 821 return ret; 822 } 823 } 824 825 return 0; 826} 827 828/* Called via dfs_walk_once_accessible from accessible_p */ 829 830static tree 831dfs_accessible_post (tree binfo, void * /*data*/) 832{ 833 if (BINFO_ACCESS (binfo) != ak_none) 834 { 835 tree scope = current_scope (); 836 if (scope && TREE_CODE (scope) != NAMESPACE_DECL 837 && is_friend (BINFO_TYPE (binfo), scope)) 838 return binfo; 839 } 840 841 return NULL_TREE; 842} 843 844/* Like accessible_p below, but within a template returns true iff DECL is 845 accessible in TYPE to all possible instantiations of the template. */ 846 847int 848accessible_in_template_p (tree type, tree decl) 849{ 850 int save_ptd = processing_template_decl; 851 processing_template_decl = 0; 852 int val = accessible_p (type, decl, false); 853 processing_template_decl = save_ptd; 854 return val; 855} 856 857/* DECL is a declaration from a base class of TYPE, which was the 858 class used to name DECL. Return nonzero if, in the current 859 context, DECL is accessible. If TYPE is actually a BINFO node, 860 then we can tell in what context the access is occurring by looking 861 at the most derived class along the path indicated by BINFO. If 862 CONSIDER_LOCAL is true, do consider special access the current 863 scope or friendship thereof we might have. */ 864 865int 866accessible_p (tree type, tree decl, bool consider_local_p) 867{ 868 tree binfo; 869 tree scope; 870 access_kind access; 871 872 /* Nonzero if it's OK to access DECL if it has protected 873 accessibility in TYPE. */ 874 int protected_ok = 0; 875 876 /* If this declaration is in a block or namespace scope, there's no 877 access control. */ 878 if (!TYPE_P (context_for_name_lookup (decl))) 879 return 1; 880 881 /* There is no need to perform access checks inside a thunk. */ 882 scope = current_scope (); 883 if (scope && DECL_THUNK_P (scope)) 884 return 1; 885 886 /* In a template declaration, we cannot be sure whether the 887 particular specialization that is instantiated will be a friend 888 or not. Therefore, all access checks are deferred until 889 instantiation. However, PROCESSING_TEMPLATE_DECL is set in the 890 parameter list for a template (because we may see dependent types 891 in default arguments for template parameters), and access 892 checking should be performed in the outermost parameter list. */ 893 if (processing_template_decl 894 && (!processing_template_parmlist || processing_template_decl > 1)) 895 return 1; 896 897 if (!TYPE_P (type)) 898 { 899 binfo = type; 900 type = BINFO_TYPE (type); 901 } 902 else 903 binfo = TYPE_BINFO (type); 904 905 /* [class.access.base] 906 907 A member m is accessible when named in class N if 908 909 --m as a member of N is public, or 910 911 --m as a member of N is private, and the reference occurs in a 912 member or friend of class N, or 913 914 --m as a member of N is protected, and the reference occurs in a 915 member or friend of class N, or in a member or friend of a 916 class P derived from N, where m as a member of P is private or 917 protected, or 918 919 --there exists a base class B of N that is accessible at the point 920 of reference, and m is accessible when named in class B. 921 922 We walk the base class hierarchy, checking these conditions. */ 923 924 if (consider_local_p) 925 { 926 /* Figure out where the reference is occurring. Check to see if 927 DECL is private or protected in this scope, since that will 928 determine whether protected access is allowed. */ 929 tree ct = current_nonlambda_class_type (); 930 if (ct) 931 protected_ok = protected_accessible_p (decl, 932 ct, 933 binfo); 934 935 /* Now, loop through the classes of which we are a friend. */ 936 if (!protected_ok) 937 protected_ok = friend_accessible_p (scope, decl, binfo); 938 } 939 940 /* Standardize the binfo that access_in_type will use. We don't 941 need to know what path was chosen from this point onwards. */ 942 binfo = TYPE_BINFO (type); 943 944 /* Compute the accessibility of DECL in the class hierarchy 945 dominated by type. */ 946 access = access_in_type (type, decl); 947 if (access == ak_public 948 || (access == ak_protected && protected_ok)) 949 return 1; 950 951 if (!consider_local_p) 952 return 0; 953 954 /* Walk the hierarchy again, looking for a base class that allows 955 access. */ 956 return dfs_walk_once_accessible (binfo, /*friends=*/true, 957 NULL, dfs_accessible_post, NULL) 958 != NULL_TREE; 959} 960 961struct lookup_field_info { 962 /* The type in which we're looking. */ 963 tree type; 964 /* The name of the field for which we're looking. */ 965 tree name; 966 /* If non-NULL, the current result of the lookup. */ 967 tree rval; 968 /* The path to RVAL. */ 969 tree rval_binfo; 970 /* If non-NULL, the lookup was ambiguous, and this is a list of the 971 candidates. */ 972 tree ambiguous; 973 /* If nonzero, we are looking for types, not data members. */ 974 int want_type; 975 /* If something went wrong, a message indicating what. */ 976 const char *errstr; 977}; 978 979/* Nonzero for a class member means that it is shared between all objects 980 of that class. 981 982 [class.member.lookup]:If the resulting set of declarations are not all 983 from sub-objects of the same type, or the set has a nonstatic member 984 and includes members from distinct sub-objects, there is an ambiguity 985 and the program is ill-formed. 986 987 This function checks that T contains no nonstatic members. */ 988 989int 990shared_member_p (tree t) 991{ 992 if (VAR_P (t) || TREE_CODE (t) == TYPE_DECL \ 993 || TREE_CODE (t) == CONST_DECL) 994 return 1; 995 if (is_overloaded_fn (t)) 996 { 997 t = get_fns (t); 998 for (; t; t = OVL_NEXT (t)) 999 { 1000 tree fn = OVL_CURRENT (t); 1001 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 1002 return 0; 1003 } 1004 return 1; 1005 } 1006 return 0; 1007} 1008 1009/* Routine to see if the sub-object denoted by the binfo PARENT can be 1010 found as a base class and sub-object of the object denoted by 1011 BINFO. */ 1012 1013static int 1014is_subobject_of_p (tree parent, tree binfo) 1015{ 1016 tree probe; 1017 1018 for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 1019 { 1020 if (probe == binfo) 1021 return 1; 1022 if (BINFO_VIRTUAL_P (probe)) 1023 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo)) 1024 != NULL_TREE); 1025 } 1026 return 0; 1027} 1028 1029/* DATA is really a struct lookup_field_info. Look for a field with 1030 the name indicated there in BINFO. If this function returns a 1031 non-NULL value it is the result of the lookup. Called from 1032 lookup_field via breadth_first_search. */ 1033 1034static tree 1035lookup_field_r (tree binfo, void *data) 1036{ 1037 struct lookup_field_info *lfi = (struct lookup_field_info *) data; 1038 tree type = BINFO_TYPE (binfo); 1039 tree nval = NULL_TREE; 1040 1041 /* If this is a dependent base, don't look in it. */ 1042 if (BINFO_DEPENDENT_BASE_P (binfo)) 1043 return NULL_TREE; 1044 1045 /* If this base class is hidden by the best-known value so far, we 1046 don't need to look. */ 1047 if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo 1048 && !BINFO_VIRTUAL_P (binfo)) 1049 return dfs_skip_bases; 1050 1051 /* First, look for a function. There can't be a function and a data 1052 member with the same name, and if there's a function and a type 1053 with the same name, the type is hidden by the function. */ 1054 if (!lfi->want_type) 1055 nval = lookup_fnfields_slot (type, lfi->name); 1056 1057 if (!nval) 1058 /* Look for a data member or type. */ 1059 nval = lookup_field_1 (type, lfi->name, lfi->want_type); 1060 1061 /* If there is no declaration with the indicated name in this type, 1062 then there's nothing to do. */ 1063 if (!nval) 1064 goto done; 1065 1066 /* If we're looking up a type (as with an elaborated type specifier) 1067 we ignore all non-types we find. */ 1068 if (lfi->want_type && !DECL_DECLARES_TYPE_P (nval)) 1069 { 1070 if (lfi->name == TYPE_IDENTIFIER (type)) 1071 { 1072 /* If the aggregate has no user defined constructors, we allow 1073 it to have fields with the same name as the enclosing type. 1074 If we are looking for that name, find the corresponding 1075 TYPE_DECL. */ 1076 for (nval = TREE_CHAIN (nval); nval; nval = TREE_CHAIN (nval)) 1077 if (DECL_NAME (nval) == lfi->name 1078 && TREE_CODE (nval) == TYPE_DECL) 1079 break; 1080 } 1081 else 1082 nval = NULL_TREE; 1083 if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL) 1084 { 1085 binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type), 1086 lfi->name); 1087 if (e != NULL) 1088 nval = TYPE_MAIN_DECL (e->type); 1089 else 1090 goto done; 1091 } 1092 } 1093 1094 /* If the lookup already found a match, and the new value doesn't 1095 hide the old one, we might have an ambiguity. */ 1096 if (lfi->rval_binfo 1097 && !is_subobject_of_p (lfi->rval_binfo, binfo)) 1098 1099 { 1100 if (nval == lfi->rval && shared_member_p (nval)) 1101 /* The two things are really the same. */ 1102 ; 1103 else if (is_subobject_of_p (binfo, lfi->rval_binfo)) 1104 /* The previous value hides the new one. */ 1105 ; 1106 else 1107 { 1108 /* We have a real ambiguity. We keep a chain of all the 1109 candidates. */ 1110 if (!lfi->ambiguous && lfi->rval) 1111 { 1112 /* This is the first time we noticed an ambiguity. Add 1113 what we previously thought was a reasonable candidate 1114 to the list. */ 1115 lfi->ambiguous = tree_cons (NULL_TREE, lfi->rval, NULL_TREE); 1116 TREE_TYPE (lfi->ambiguous) = error_mark_node; 1117 } 1118 1119 /* Add the new value. */ 1120 lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous); 1121 TREE_TYPE (lfi->ambiguous) = error_mark_node; 1122 lfi->errstr = G_("request for member %qD is ambiguous"); 1123 } 1124 } 1125 else 1126 { 1127 lfi->rval = nval; 1128 lfi->rval_binfo = binfo; 1129 } 1130 1131 done: 1132 /* Don't look for constructors or destructors in base classes. */ 1133 if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name)) 1134 return dfs_skip_bases; 1135 return NULL_TREE; 1136} 1137 1138/* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO, 1139 BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO, 1140 FUNCTIONS, and OPTYPE respectively. */ 1141 1142tree 1143build_baselink (tree binfo, tree access_binfo, tree functions, tree optype) 1144{ 1145 tree baselink; 1146 1147 gcc_assert (TREE_CODE (functions) == FUNCTION_DECL 1148 || TREE_CODE (functions) == TEMPLATE_DECL 1149 || TREE_CODE (functions) == TEMPLATE_ID_EXPR 1150 || TREE_CODE (functions) == OVERLOAD); 1151 gcc_assert (!optype || TYPE_P (optype)); 1152 gcc_assert (TREE_TYPE (functions)); 1153 1154 baselink = make_node (BASELINK); 1155 TREE_TYPE (baselink) = TREE_TYPE (functions); 1156 BASELINK_BINFO (baselink) = binfo; 1157 BASELINK_ACCESS_BINFO (baselink) = access_binfo; 1158 BASELINK_FUNCTIONS (baselink) = functions; 1159 BASELINK_OPTYPE (baselink) = optype; 1160 1161 return baselink; 1162} 1163 1164/* Look for a member named NAME in an inheritance lattice dominated by 1165 XBASETYPE. If PROTECT is 0 or two, we do not check access. If it 1166 is 1, we enforce accessibility. If PROTECT is zero, then, for an 1167 ambiguous lookup, we return NULL. If PROTECT is 1, we issue error 1168 messages about inaccessible or ambiguous lookup. If PROTECT is 2, 1169 we return a TREE_LIST whose TREE_TYPE is error_mark_node and whose 1170 TREE_VALUEs are the list of ambiguous candidates. 1171 1172 WANT_TYPE is 1 when we should only return TYPE_DECLs. 1173 1174 If nothing can be found return NULL_TREE and do not issue an error. */ 1175 1176tree 1177lookup_member (tree xbasetype, tree name, int protect, bool want_type, 1178 tsubst_flags_t complain) 1179{ 1180 tree rval, rval_binfo = NULL_TREE; 1181 tree type = NULL_TREE, basetype_path = NULL_TREE; 1182 struct lookup_field_info lfi; 1183 1184 /* rval_binfo is the binfo associated with the found member, note, 1185 this can be set with useful information, even when rval is not 1186 set, because it must deal with ALL members, not just non-function 1187 members. It is used for ambiguity checking and the hidden 1188 checks. Whereas rval is only set if a proper (not hidden) 1189 non-function member is found. */ 1190 1191 const char *errstr = 0; 1192 1193 if (name == error_mark_node 1194 || xbasetype == NULL_TREE 1195 || xbasetype == error_mark_node) 1196 return NULL_TREE; 1197 1198 gcc_assert (identifier_p (name)); 1199 1200 if (TREE_CODE (xbasetype) == TREE_BINFO) 1201 { 1202 type = BINFO_TYPE (xbasetype); 1203 basetype_path = xbasetype; 1204 } 1205 else 1206 { 1207 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype))) 1208 return NULL_TREE; 1209 type = xbasetype; 1210 xbasetype = NULL_TREE; 1211 } 1212 1213 type = complete_type (type); 1214 if (!basetype_path) 1215 basetype_path = TYPE_BINFO (type); 1216 1217 if (!basetype_path) 1218 return NULL_TREE; 1219 1220 if (GATHER_STATISTICS) 1221 n_calls_lookup_field++; 1222 1223 memset (&lfi, 0, sizeof (lfi)); 1224 lfi.type = type; 1225 lfi.name = name; 1226 lfi.want_type = want_type; 1227 dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi); 1228 rval = lfi.rval; 1229 rval_binfo = lfi.rval_binfo; 1230 if (rval_binfo) 1231 type = BINFO_TYPE (rval_binfo); 1232 errstr = lfi.errstr; 1233 1234 /* If we are not interested in ambiguities, don't report them; 1235 just return NULL_TREE. */ 1236 if (!protect && lfi.ambiguous) 1237 return NULL_TREE; 1238 1239 if (protect == 2) 1240 { 1241 if (lfi.ambiguous) 1242 return lfi.ambiguous; 1243 else 1244 protect = 0; 1245 } 1246 1247 /* [class.access] 1248 1249 In the case of overloaded function names, access control is 1250 applied to the function selected by overloaded resolution. 1251 1252 We cannot check here, even if RVAL is only a single non-static 1253 member function, since we do not know what the "this" pointer 1254 will be. For: 1255 1256 class A { protected: void f(); }; 1257 class B : public A { 1258 void g(A *p) { 1259 f(); // OK 1260 p->f(); // Not OK. 1261 } 1262 }; 1263 1264 only the first call to "f" is valid. However, if the function is 1265 static, we can check. */ 1266 if (rval && protect 1267 && !really_overloaded_fn (rval)) 1268 { 1269 tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval; 1270 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) 1271 && !perform_or_defer_access_check (basetype_path, decl, decl, 1272 complain)) 1273 rval = error_mark_node; 1274 } 1275 1276 if (errstr && protect) 1277 { 1278 if (complain & tf_error) 1279 { 1280 error (errstr, name, type); 1281 if (lfi.ambiguous) 1282 print_candidates (lfi.ambiguous); 1283 } 1284 rval = error_mark_node; 1285 } 1286 1287 if (rval && is_overloaded_fn (rval)) 1288 rval = build_baselink (rval_binfo, basetype_path, rval, 1289 (IDENTIFIER_TYPENAME_P (name) 1290 ? TREE_TYPE (name): NULL_TREE)); 1291 return rval; 1292} 1293 1294/* Like lookup_member, except that if we find a function member we 1295 return NULL_TREE. */ 1296 1297tree 1298lookup_field (tree xbasetype, tree name, int protect, bool want_type) 1299{ 1300 tree rval = lookup_member (xbasetype, name, protect, want_type, 1301 tf_warning_or_error); 1302 1303 /* Ignore functions, but propagate the ambiguity list. */ 1304 if (!error_operand_p (rval) 1305 && (rval && BASELINK_P (rval))) 1306 return NULL_TREE; 1307 1308 return rval; 1309} 1310 1311/* Like lookup_member, except that if we find a non-function member we 1312 return NULL_TREE. */ 1313 1314tree 1315lookup_fnfields (tree xbasetype, tree name, int protect) 1316{ 1317 tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false, 1318 tf_warning_or_error); 1319 1320 /* Ignore non-functions, but propagate the ambiguity list. */ 1321 if (!error_operand_p (rval) 1322 && (rval && !BASELINK_P (rval))) 1323 return NULL_TREE; 1324 1325 return rval; 1326} 1327 1328/* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE 1329 corresponding to "operator TYPE ()", or -1 if there is no such 1330 operator. Only CLASS_TYPE itself is searched; this routine does 1331 not scan the base classes of CLASS_TYPE. */ 1332 1333static int 1334lookup_conversion_operator (tree class_type, tree type) 1335{ 1336 int tpl_slot = -1; 1337 1338 if (TYPE_HAS_CONVERSION (class_type)) 1339 { 1340 int i; 1341 tree fn; 1342 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_type); 1343 1344 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 1345 vec_safe_iterate (methods, i, &fn); ++i) 1346 { 1347 /* All the conversion operators come near the beginning of 1348 the class. Therefore, if FN is not a conversion 1349 operator, there is no matching conversion operator in 1350 CLASS_TYPE. */ 1351 fn = OVL_CURRENT (fn); 1352 if (!DECL_CONV_FN_P (fn)) 1353 break; 1354 1355 if (TREE_CODE (fn) == TEMPLATE_DECL) 1356 /* All the templated conversion functions are on the same 1357 slot, so remember it. */ 1358 tpl_slot = i; 1359 else if (same_type_p (DECL_CONV_FN_TYPE (fn), type)) 1360 return i; 1361 } 1362 } 1363 1364 return tpl_slot; 1365} 1366 1367/* TYPE is a class type. Return the index of the fields within 1368 the method vector with name NAME, or -1 if no such field exists. 1369 Does not lazily declare implicitly-declared member functions. */ 1370 1371static int 1372lookup_fnfields_idx_nolazy (tree type, tree name) 1373{ 1374 vec<tree, va_gc> *method_vec; 1375 tree fn; 1376 tree tmp; 1377 size_t i; 1378 1379 if (!CLASS_TYPE_P (type)) 1380 return -1; 1381 1382 method_vec = CLASSTYPE_METHOD_VEC (type); 1383 if (!method_vec) 1384 return -1; 1385 1386 if (GATHER_STATISTICS) 1387 n_calls_lookup_fnfields_1++; 1388 1389 /* Constructors are first... */ 1390 if (name == ctor_identifier) 1391 { 1392 fn = CLASSTYPE_CONSTRUCTORS (type); 1393 return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1; 1394 } 1395 /* and destructors are second. */ 1396 if (name == dtor_identifier) 1397 { 1398 fn = CLASSTYPE_DESTRUCTORS (type); 1399 return fn ? CLASSTYPE_DESTRUCTOR_SLOT : -1; 1400 } 1401 if (IDENTIFIER_TYPENAME_P (name)) 1402 return lookup_conversion_operator (type, TREE_TYPE (name)); 1403 1404 /* Skip the conversion operators. */ 1405 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 1406 vec_safe_iterate (method_vec, i, &fn); 1407 ++i) 1408 if (!DECL_CONV_FN_P (OVL_CURRENT (fn))) 1409 break; 1410 1411 /* If the type is complete, use binary search. */ 1412 if (COMPLETE_TYPE_P (type)) 1413 { 1414 int lo; 1415 int hi; 1416 1417 lo = i; 1418 hi = method_vec->length (); 1419 while (lo < hi) 1420 { 1421 i = (lo + hi) / 2; 1422 1423 if (GATHER_STATISTICS) 1424 n_outer_fields_searched++; 1425 1426 tmp = (*method_vec)[i]; 1427 tmp = DECL_NAME (OVL_CURRENT (tmp)); 1428 if (tmp > name) 1429 hi = i; 1430 else if (tmp < name) 1431 lo = i + 1; 1432 else 1433 return i; 1434 } 1435 } 1436 else 1437 for (; vec_safe_iterate (method_vec, i, &fn); ++i) 1438 { 1439 if (GATHER_STATISTICS) 1440 n_outer_fields_searched++; 1441 if (DECL_NAME (OVL_CURRENT (fn)) == name) 1442 return i; 1443 } 1444 1445 return -1; 1446} 1447 1448/* TYPE is a class type. Return the index of the fields within 1449 the method vector with name NAME, or -1 if no such field exists. */ 1450 1451int 1452lookup_fnfields_1 (tree type, tree name) 1453{ 1454 if (!CLASS_TYPE_P (type)) 1455 return -1; 1456 1457 if (COMPLETE_TYPE_P (type)) 1458 { 1459 if ((name == ctor_identifier 1460 || name == base_ctor_identifier 1461 || name == complete_ctor_identifier)) 1462 { 1463 if (CLASSTYPE_LAZY_DEFAULT_CTOR (type)) 1464 lazily_declare_fn (sfk_constructor, type); 1465 if (CLASSTYPE_LAZY_COPY_CTOR (type)) 1466 lazily_declare_fn (sfk_copy_constructor, type); 1467 if (CLASSTYPE_LAZY_MOVE_CTOR (type)) 1468 lazily_declare_fn (sfk_move_constructor, type); 1469 } 1470 else if (name == ansi_assopname (NOP_EXPR)) 1471 { 1472 if (CLASSTYPE_LAZY_COPY_ASSIGN (type)) 1473 lazily_declare_fn (sfk_copy_assignment, type); 1474 if (CLASSTYPE_LAZY_MOVE_ASSIGN (type)) 1475 lazily_declare_fn (sfk_move_assignment, type); 1476 } 1477 else if ((name == dtor_identifier 1478 || name == base_dtor_identifier 1479 || name == complete_dtor_identifier 1480 || name == deleting_dtor_identifier) 1481 && CLASSTYPE_LAZY_DESTRUCTOR (type)) 1482 lazily_declare_fn (sfk_destructor, type); 1483 } 1484 1485 return lookup_fnfields_idx_nolazy (type, name); 1486} 1487 1488/* TYPE is a class type. Return the field within the method vector with 1489 name NAME, or NULL_TREE if no such field exists. */ 1490 1491tree 1492lookup_fnfields_slot (tree type, tree name) 1493{ 1494 int ix = lookup_fnfields_1 (complete_type (type), name); 1495 if (ix < 0) 1496 return NULL_TREE; 1497 return (*CLASSTYPE_METHOD_VEC (type))[ix]; 1498} 1499 1500/* As above, but avoid lazily declaring functions. */ 1501 1502tree 1503lookup_fnfields_slot_nolazy (tree type, tree name) 1504{ 1505 int ix = lookup_fnfields_idx_nolazy (complete_type (type), name); 1506 if (ix < 0) 1507 return NULL_TREE; 1508 return (*CLASSTYPE_METHOD_VEC (type))[ix]; 1509} 1510 1511/* Like lookup_fnfields_1, except that the name is extracted from 1512 FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL. */ 1513 1514int 1515class_method_index_for_fn (tree class_type, tree function) 1516{ 1517 gcc_assert (DECL_DECLARES_FUNCTION_P (function)); 1518 1519 return lookup_fnfields_1 (class_type, 1520 DECL_CONSTRUCTOR_P (function) ? ctor_identifier : 1521 DECL_DESTRUCTOR_P (function) ? dtor_identifier : 1522 DECL_NAME (function)); 1523} 1524 1525 1526/* DECL is the result of a qualified name lookup. QUALIFYING_SCOPE is 1527 the class or namespace used to qualify the name. CONTEXT_CLASS is 1528 the class corresponding to the object in which DECL will be used. 1529 Return a possibly modified version of DECL that takes into account 1530 the CONTEXT_CLASS. 1531 1532 In particular, consider an expression like `B::m' in the context of 1533 a derived class `D'. If `B::m' has been resolved to a BASELINK, 1534 then the most derived class indicated by the BASELINK_BINFO will be 1535 `B', not `D'. This function makes that adjustment. */ 1536 1537tree 1538adjust_result_of_qualified_name_lookup (tree decl, 1539 tree qualifying_scope, 1540 tree context_class) 1541{ 1542 if (context_class && context_class != error_mark_node 1543 && CLASS_TYPE_P (context_class) 1544 && CLASS_TYPE_P (qualifying_scope) 1545 && DERIVED_FROM_P (qualifying_scope, context_class) 1546 && BASELINK_P (decl)) 1547 { 1548 tree base; 1549 1550 /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS. 1551 Because we do not yet know which function will be chosen by 1552 overload resolution, we cannot yet check either accessibility 1553 or ambiguity -- in either case, the choice of a static member 1554 function might make the usage valid. */ 1555 base = lookup_base (context_class, qualifying_scope, 1556 ba_unique, NULL, tf_none); 1557 if (base && base != error_mark_node) 1558 { 1559 BASELINK_ACCESS_BINFO (decl) = base; 1560 BASELINK_BINFO (decl) 1561 = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)), 1562 ba_unique, NULL, tf_none); 1563 } 1564 } 1565 1566 if (BASELINK_P (decl)) 1567 BASELINK_QUALIFIED_P (decl) = true; 1568 1569 return decl; 1570} 1571 1572 1573/* Walk the class hierarchy within BINFO, in a depth-first traversal. 1574 PRE_FN is called in preorder, while POST_FN is called in postorder. 1575 If PRE_FN returns DFS_SKIP_BASES, child binfos will not be 1576 walked. If PRE_FN or POST_FN returns a different non-NULL value, 1577 that value is immediately returned and the walk is terminated. One 1578 of PRE_FN and POST_FN can be NULL. At each node, PRE_FN and 1579 POST_FN are passed the binfo to examine and the caller's DATA 1580 value. All paths are walked, thus virtual and morally virtual 1581 binfos can be multiply walked. */ 1582 1583tree 1584dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *), 1585 tree (*post_fn) (tree, void *), void *data) 1586{ 1587 tree rval; 1588 unsigned ix; 1589 tree base_binfo; 1590 1591 /* Call the pre-order walking function. */ 1592 if (pre_fn) 1593 { 1594 rval = pre_fn (binfo, data); 1595 if (rval) 1596 { 1597 if (rval == dfs_skip_bases) 1598 goto skip_bases; 1599 return rval; 1600 } 1601 } 1602 1603 /* Find the next child binfo to walk. */ 1604 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 1605 { 1606 rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data); 1607 if (rval) 1608 return rval; 1609 } 1610 1611 skip_bases: 1612 /* Call the post-order walking function. */ 1613 if (post_fn) 1614 { 1615 rval = post_fn (binfo, data); 1616 gcc_assert (rval != dfs_skip_bases); 1617 return rval; 1618 } 1619 1620 return NULL_TREE; 1621} 1622 1623/* Worker for dfs_walk_once. This behaves as dfs_walk_all, except 1624 that binfos are walked at most once. */ 1625 1626static tree 1627dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *), 1628 tree (*post_fn) (tree, void *), void *data) 1629{ 1630 tree rval; 1631 unsigned ix; 1632 tree base_binfo; 1633 1634 /* Call the pre-order walking function. */ 1635 if (pre_fn) 1636 { 1637 rval = pre_fn (binfo, data); 1638 if (rval) 1639 { 1640 if (rval == dfs_skip_bases) 1641 goto skip_bases; 1642 1643 return rval; 1644 } 1645 } 1646 1647 /* Find the next child binfo to walk. */ 1648 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 1649 { 1650 if (BINFO_VIRTUAL_P (base_binfo)) 1651 { 1652 if (BINFO_MARKED (base_binfo)) 1653 continue; 1654 BINFO_MARKED (base_binfo) = 1; 1655 } 1656 1657 rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data); 1658 if (rval) 1659 return rval; 1660 } 1661 1662 skip_bases: 1663 /* Call the post-order walking function. */ 1664 if (post_fn) 1665 { 1666 rval = post_fn (binfo, data); 1667 gcc_assert (rval != dfs_skip_bases); 1668 return rval; 1669 } 1670 1671 return NULL_TREE; 1672} 1673 1674/* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of 1675 BINFO. */ 1676 1677static void 1678dfs_unmark_r (tree binfo) 1679{ 1680 unsigned ix; 1681 tree base_binfo; 1682 1683 /* Process the basetypes. */ 1684 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 1685 { 1686 if (BINFO_VIRTUAL_P (base_binfo)) 1687 { 1688 if (!BINFO_MARKED (base_binfo)) 1689 continue; 1690 BINFO_MARKED (base_binfo) = 0; 1691 } 1692 /* Only walk, if it can contain more virtual bases. */ 1693 if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo))) 1694 dfs_unmark_r (base_binfo); 1695 } 1696} 1697 1698/* Like dfs_walk_all, except that binfos are not multiply walked. For 1699 non-diamond shaped hierarchies this is the same as dfs_walk_all. 1700 For diamond shaped hierarchies we must mark the virtual bases, to 1701 avoid multiple walks. */ 1702 1703tree 1704dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *), 1705 tree (*post_fn) (tree, void *), void *data) 1706{ 1707 static int active = 0; /* We must not be called recursively. */ 1708 tree rval; 1709 1710 gcc_assert (pre_fn || post_fn); 1711 gcc_assert (!active); 1712 active++; 1713 1714 if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo))) 1715 /* We are not diamond shaped, and therefore cannot encounter the 1716 same binfo twice. */ 1717 rval = dfs_walk_all (binfo, pre_fn, post_fn, data); 1718 else 1719 { 1720 rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data); 1721 if (!BINFO_INHERITANCE_CHAIN (binfo)) 1722 { 1723 /* We are at the top of the hierarchy, and can use the 1724 CLASSTYPE_VBASECLASSES list for unmarking the virtual 1725 bases. */ 1726 vec<tree, va_gc> *vbases; 1727 unsigned ix; 1728 tree base_binfo; 1729 1730 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0; 1731 vec_safe_iterate (vbases, ix, &base_binfo); ix++) 1732 BINFO_MARKED (base_binfo) = 0; 1733 } 1734 else 1735 dfs_unmark_r (binfo); 1736 } 1737 1738 active--; 1739 1740 return rval; 1741} 1742 1743/* Worker function for dfs_walk_once_accessible. Behaves like 1744 dfs_walk_once_r, except (a) FRIENDS_P is true if special 1745 access given by the current context should be considered, (b) ONCE 1746 indicates whether bases should be marked during traversal. */ 1747 1748static tree 1749dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once, 1750 tree (*pre_fn) (tree, void *), 1751 tree (*post_fn) (tree, void *), void *data) 1752{ 1753 tree rval = NULL_TREE; 1754 unsigned ix; 1755 tree base_binfo; 1756 1757 /* Call the pre-order walking function. */ 1758 if (pre_fn) 1759 { 1760 rval = pre_fn (binfo, data); 1761 if (rval) 1762 { 1763 if (rval == dfs_skip_bases) 1764 goto skip_bases; 1765 1766 return rval; 1767 } 1768 } 1769 1770 /* Find the next child binfo to walk. */ 1771 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 1772 { 1773 bool mark = once && BINFO_VIRTUAL_P (base_binfo); 1774 1775 if (mark && BINFO_MARKED (base_binfo)) 1776 continue; 1777 1778 /* If the base is inherited via private or protected 1779 inheritance, then we can't see it, unless we are a friend of 1780 the current binfo. */ 1781 if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node) 1782 { 1783 tree scope; 1784 if (!friends_p) 1785 continue; 1786 scope = current_scope (); 1787 if (!scope 1788 || TREE_CODE (scope) == NAMESPACE_DECL 1789 || !is_friend (BINFO_TYPE (binfo), scope)) 1790 continue; 1791 } 1792 1793 if (mark) 1794 BINFO_MARKED (base_binfo) = 1; 1795 1796 rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once, 1797 pre_fn, post_fn, data); 1798 if (rval) 1799 return rval; 1800 } 1801 1802 skip_bases: 1803 /* Call the post-order walking function. */ 1804 if (post_fn) 1805 { 1806 rval = post_fn (binfo, data); 1807 gcc_assert (rval != dfs_skip_bases); 1808 return rval; 1809 } 1810 1811 return NULL_TREE; 1812} 1813 1814/* Like dfs_walk_once except that only accessible bases are walked. 1815 FRIENDS_P indicates whether friendship of the local context 1816 should be considered when determining accessibility. */ 1817 1818static tree 1819dfs_walk_once_accessible (tree binfo, bool friends_p, 1820 tree (*pre_fn) (tree, void *), 1821 tree (*post_fn) (tree, void *), void *data) 1822{ 1823 bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)); 1824 tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped, 1825 pre_fn, post_fn, data); 1826 1827 if (diamond_shaped) 1828 { 1829 if (!BINFO_INHERITANCE_CHAIN (binfo)) 1830 { 1831 /* We are at the top of the hierarchy, and can use the 1832 CLASSTYPE_VBASECLASSES list for unmarking the virtual 1833 bases. */ 1834 vec<tree, va_gc> *vbases; 1835 unsigned ix; 1836 tree base_binfo; 1837 1838 for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0; 1839 vec_safe_iterate (vbases, ix, &base_binfo); ix++) 1840 BINFO_MARKED (base_binfo) = 0; 1841 } 1842 else 1843 dfs_unmark_r (binfo); 1844 } 1845 return rval; 1846} 1847 1848/* Check that virtual overrider OVERRIDER is acceptable for base function 1849 BASEFN. Issue diagnostic, and return zero, if unacceptable. */ 1850 1851static int 1852check_final_overrider (tree overrider, tree basefn) 1853{ 1854 tree over_type = TREE_TYPE (overrider); 1855 tree base_type = TREE_TYPE (basefn); 1856 tree over_return = fndecl_declared_return_type (overrider); 1857 tree base_return = fndecl_declared_return_type (basefn); 1858 tree over_throw, base_throw; 1859 1860 int fail = 0; 1861 1862 if (DECL_INVALID_OVERRIDER_P (overrider)) 1863 return 0; 1864 1865 if (same_type_p (base_return, over_return)) 1866 /* OK */; 1867 else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return)) 1868 || (TREE_CODE (base_return) == TREE_CODE (over_return) 1869 && POINTER_TYPE_P (base_return))) 1870 { 1871 /* Potentially covariant. */ 1872 unsigned base_quals, over_quals; 1873 1874 fail = !POINTER_TYPE_P (base_return); 1875 if (!fail) 1876 { 1877 fail = cp_type_quals (base_return) != cp_type_quals (over_return); 1878 1879 base_return = TREE_TYPE (base_return); 1880 over_return = TREE_TYPE (over_return); 1881 } 1882 base_quals = cp_type_quals (base_return); 1883 over_quals = cp_type_quals (over_return); 1884 1885 if ((base_quals & over_quals) != over_quals) 1886 fail = 1; 1887 1888 if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return)) 1889 { 1890 /* Strictly speaking, the standard requires the return type to be 1891 complete even if it only differs in cv-quals, but that seems 1892 like a bug in the wording. */ 1893 if (!same_type_ignoring_top_level_qualifiers_p (base_return, 1894 over_return)) 1895 { 1896 tree binfo = lookup_base (over_return, base_return, 1897 ba_check, NULL, tf_none); 1898 1899 if (!binfo || binfo == error_mark_node) 1900 fail = 1; 1901 } 1902 } 1903 else if (can_convert_standard (TREE_TYPE (base_type), 1904 TREE_TYPE (over_type), 1905 tf_warning_or_error)) 1906 /* GNU extension, allow trivial pointer conversions such as 1907 converting to void *, or qualification conversion. */ 1908 { 1909 if (pedwarn (DECL_SOURCE_LOCATION (overrider), 0, 1910 "invalid covariant return type for %q#D", overrider)) 1911 inform (DECL_SOURCE_LOCATION (basefn), 1912 " overriding %q+#D", basefn); 1913 } 1914 else 1915 fail = 2; 1916 } 1917 else 1918 fail = 2; 1919 if (!fail) 1920 /* OK */; 1921 else 1922 { 1923 if (fail == 1) 1924 { 1925 error ("invalid covariant return type for %q+#D", overrider); 1926 error (" overriding %q+#D", basefn); 1927 } 1928 else 1929 { 1930 error ("conflicting return type specified for %q+#D", overrider); 1931 error (" overriding %q+#D", basefn); 1932 } 1933 DECL_INVALID_OVERRIDER_P (overrider) = 1; 1934 return 0; 1935 } 1936 1937 /* Check throw specifier is at least as strict. */ 1938 maybe_instantiate_noexcept (basefn); 1939 maybe_instantiate_noexcept (overrider); 1940 base_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (basefn)); 1941 over_throw = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (overrider)); 1942 1943 if (!comp_except_specs (base_throw, over_throw, ce_derived)) 1944 { 1945 error ("looser throw specifier for %q+#F", overrider); 1946 error (" overriding %q+#F", basefn); 1947 DECL_INVALID_OVERRIDER_P (overrider) = 1; 1948 return 0; 1949 } 1950 1951 /* Check for conflicting type attributes. */ 1952 if (!comp_type_attributes (over_type, base_type)) 1953 { 1954 error ("conflicting type attributes specified for %q+#D", overrider); 1955 error (" overriding %q+#D", basefn); 1956 DECL_INVALID_OVERRIDER_P (overrider) = 1; 1957 return 0; 1958 } 1959 1960 if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider)) 1961 { 1962 if (DECL_DELETED_FN (overrider)) 1963 { 1964 error ("deleted function %q+D", overrider); 1965 error ("overriding non-deleted function %q+D", basefn); 1966 maybe_explain_implicit_delete (overrider); 1967 } 1968 else 1969 { 1970 error ("non-deleted function %q+D", overrider); 1971 error ("overriding deleted function %q+D", basefn); 1972 } 1973 return 0; 1974 } 1975 if (DECL_FINAL_P (basefn)) 1976 { 1977 error ("virtual function %q+D", overrider); 1978 error ("overriding final function %q+D", basefn); 1979 return 0; 1980 } 1981 return 1; 1982} 1983 1984/* Given a class TYPE, and a function decl FNDECL, look for 1985 virtual functions in TYPE's hierarchy which FNDECL overrides. 1986 We do not look in TYPE itself, only its bases. 1987 1988 Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we 1989 find that it overrides anything. 1990 1991 We check that every function which is overridden, is correctly 1992 overridden. */ 1993 1994int 1995look_for_overrides (tree type, tree fndecl) 1996{ 1997 tree binfo = TYPE_BINFO (type); 1998 tree base_binfo; 1999 int ix; 2000 int found = 0; 2001 2002 /* A constructor for a class T does not override a function T 2003 in a base class. */ 2004 if (DECL_CONSTRUCTOR_P (fndecl)) 2005 return 0; 2006 2007 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 2008 { 2009 tree basetype = BINFO_TYPE (base_binfo); 2010 2011 if (TYPE_POLYMORPHIC_P (basetype)) 2012 found += look_for_overrides_r (basetype, fndecl); 2013 } 2014 return found; 2015} 2016 2017/* Look in TYPE for virtual functions with the same signature as 2018 FNDECL. */ 2019 2020tree 2021look_for_overrides_here (tree type, tree fndecl) 2022{ 2023 int ix; 2024 2025 /* If there are no methods in TYPE (meaning that only implicitly 2026 declared methods will ever be provided for TYPE), then there are 2027 no virtual functions. */ 2028 if (!CLASSTYPE_METHOD_VEC (type)) 2029 return NULL_TREE; 2030 2031 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl)) 2032 ix = CLASSTYPE_DESTRUCTOR_SLOT; 2033 else 2034 ix = lookup_fnfields_1 (type, DECL_NAME (fndecl)); 2035 if (ix >= 0) 2036 { 2037 tree fns = (*CLASSTYPE_METHOD_VEC (type))[ix]; 2038 2039 for (; fns; fns = OVL_NEXT (fns)) 2040 { 2041 tree fn = OVL_CURRENT (fns); 2042 2043 if (!DECL_VIRTUAL_P (fn)) 2044 /* Not a virtual. */; 2045 else if (DECL_CONTEXT (fn) != type) 2046 /* Introduced with a using declaration. */; 2047 else if (DECL_STATIC_FUNCTION_P (fndecl)) 2048 { 2049 tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn)); 2050 tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 2051 if (compparms (TREE_CHAIN (btypes), dtypes)) 2052 return fn; 2053 } 2054 else if (same_signature_p (fndecl, fn)) 2055 return fn; 2056 } 2057 } 2058 return NULL_TREE; 2059} 2060 2061/* Look in TYPE for virtual functions overridden by FNDECL. Check both 2062 TYPE itself and its bases. */ 2063 2064static int 2065look_for_overrides_r (tree type, tree fndecl) 2066{ 2067 tree fn = look_for_overrides_here (type, fndecl); 2068 if (fn) 2069 { 2070 if (DECL_STATIC_FUNCTION_P (fndecl)) 2071 { 2072 /* A static member function cannot match an inherited 2073 virtual member function. */ 2074 error ("%q+#D cannot be declared", fndecl); 2075 error (" since %q+#D declared in base class", fn); 2076 } 2077 else 2078 { 2079 /* It's definitely virtual, even if not explicitly set. */ 2080 DECL_VIRTUAL_P (fndecl) = 1; 2081 check_final_overrider (fndecl, fn); 2082 } 2083 return 1; 2084 } 2085 2086 /* We failed to find one declared in this class. Look in its bases. */ 2087 return look_for_overrides (type, fndecl); 2088} 2089 2090/* Called via dfs_walk from dfs_get_pure_virtuals. */ 2091 2092static tree 2093dfs_get_pure_virtuals (tree binfo, void *data) 2094{ 2095 tree type = (tree) data; 2096 2097 /* We're not interested in primary base classes; the derived class 2098 of which they are a primary base will contain the information we 2099 need. */ 2100 if (!BINFO_PRIMARY_P (binfo)) 2101 { 2102 tree virtuals; 2103 2104 for (virtuals = BINFO_VIRTUALS (binfo); 2105 virtuals; 2106 virtuals = TREE_CHAIN (virtuals)) 2107 if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals))) 2108 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (type), BV_FN (virtuals)); 2109 } 2110 2111 return NULL_TREE; 2112} 2113 2114/* Set CLASSTYPE_PURE_VIRTUALS for TYPE. */ 2115 2116void 2117get_pure_virtuals (tree type) 2118{ 2119 /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there 2120 is going to be overridden. */ 2121 CLASSTYPE_PURE_VIRTUALS (type) = NULL; 2122 /* Now, run through all the bases which are not primary bases, and 2123 collect the pure virtual functions. We look at the vtable in 2124 each class to determine what pure virtual functions are present. 2125 (A primary base is not interesting because the derived class of 2126 which it is a primary base will contain vtable entries for the 2127 pure virtuals in the base class. */ 2128 dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type); 2129} 2130 2131/* Debug info for C++ classes can get very large; try to avoid 2132 emitting it everywhere. 2133 2134 Note that this optimization wins even when the target supports 2135 BINCL (if only slightly), and reduces the amount of work for the 2136 linker. */ 2137 2138void 2139maybe_suppress_debug_info (tree t) 2140{ 2141 if (write_symbols == NO_DEBUG) 2142 return; 2143 2144 /* We might have set this earlier in cp_finish_decl. */ 2145 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0; 2146 2147 /* Always emit the information for each class every time. */ 2148 if (flag_emit_class_debug_always) 2149 return; 2150 2151 /* If we already know how we're handling this class, handle debug info 2152 the same way. */ 2153 if (CLASSTYPE_INTERFACE_KNOWN (t)) 2154 { 2155 if (CLASSTYPE_INTERFACE_ONLY (t)) 2156 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1; 2157 /* else don't set it. */ 2158 } 2159 /* If the class has a vtable, write out the debug info along with 2160 the vtable. */ 2161 else if (TYPE_CONTAINS_VPTR_P (t)) 2162 TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1; 2163 2164 /* Otherwise, just emit the debug info normally. */ 2165} 2166 2167/* Note that we want debugging information for a base class of a class 2168 whose vtable is being emitted. Normally, this would happen because 2169 calling the constructor for a derived class implies calling the 2170 constructors for all bases, which involve initializing the 2171 appropriate vptr with the vtable for the base class; but in the 2172 presence of optimization, this initialization may be optimized 2173 away, so we tell finish_vtable_vardecl that we want the debugging 2174 information anyway. */ 2175 2176static tree 2177dfs_debug_mark (tree binfo, void * /*data*/) 2178{ 2179 tree t = BINFO_TYPE (binfo); 2180 2181 if (CLASSTYPE_DEBUG_REQUESTED (t)) 2182 return dfs_skip_bases; 2183 2184 CLASSTYPE_DEBUG_REQUESTED (t) = 1; 2185 2186 return NULL_TREE; 2187} 2188 2189/* Write out the debugging information for TYPE, whose vtable is being 2190 emitted. Also walk through our bases and note that we want to 2191 write out information for them. This avoids the problem of not 2192 writing any debug info for intermediate basetypes whose 2193 constructors, and thus the references to their vtables, and thus 2194 the vtables themselves, were optimized away. */ 2195 2196void 2197note_debug_info_needed (tree type) 2198{ 2199 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type))) 2200 { 2201 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)) = 0; 2202 rest_of_type_compilation (type, toplevel_bindings_p ()); 2203 } 2204 2205 dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0); 2206} 2207 2208void 2209print_search_statistics (void) 2210{ 2211 if (! GATHER_STATISTICS) 2212 { 2213 fprintf (stderr, "no search statistics\n"); 2214 return; 2215 } 2216 2217 fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n", 2218 n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1); 2219 fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n", 2220 n_outer_fields_searched, n_calls_lookup_fnfields); 2221 fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type); 2222} 2223 2224void 2225reinit_search_statistics (void) 2226{ 2227 n_fields_searched = 0; 2228 n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0; 2229 n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0; 2230 n_calls_get_base_type = 0; 2231 n_outer_fields_searched = 0; 2232 n_contexts_saved = 0; 2233} 2234 2235/* Helper for lookup_conversions_r. TO_TYPE is the type converted to 2236 by a conversion op in base BINFO. VIRTUAL_DEPTH is nonzero if 2237 BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual 2238 bases have been encountered already in the tree walk. PARENT_CONVS 2239 is the list of lists of conversion functions that could hide CONV 2240 and OTHER_CONVS is the list of lists of conversion functions that 2241 could hide or be hidden by CONV, should virtualness be involved in 2242 the hierarchy. Merely checking the conversion op's name is not 2243 enough because two conversion operators to the same type can have 2244 different names. Return nonzero if we are visible. */ 2245 2246static int 2247check_hidden_convs (tree binfo, int virtual_depth, int virtualness, 2248 tree to_type, tree parent_convs, tree other_convs) 2249{ 2250 tree level, probe; 2251 2252 /* See if we are hidden by a parent conversion. */ 2253 for (level = parent_convs; level; level = TREE_CHAIN (level)) 2254 for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe)) 2255 if (same_type_p (to_type, TREE_TYPE (probe))) 2256 return 0; 2257 2258 if (virtual_depth || virtualness) 2259 { 2260 /* In a virtual hierarchy, we could be hidden, or could hide a 2261 conversion function on the other_convs list. */ 2262 for (level = other_convs; level; level = TREE_CHAIN (level)) 2263 { 2264 int we_hide_them; 2265 int they_hide_us; 2266 tree *prev, other; 2267 2268 if (!(virtual_depth || TREE_STATIC (level))) 2269 /* Neither is morally virtual, so cannot hide each other. */ 2270 continue; 2271 2272 if (!TREE_VALUE (level)) 2273 /* They evaporated away already. */ 2274 continue; 2275 2276 they_hide_us = (virtual_depth 2277 && original_binfo (binfo, TREE_PURPOSE (level))); 2278 we_hide_them = (!they_hide_us && TREE_STATIC (level) 2279 && original_binfo (TREE_PURPOSE (level), binfo)); 2280 2281 if (!(we_hide_them || they_hide_us)) 2282 /* Neither is within the other, so no hiding can occur. */ 2283 continue; 2284 2285 for (prev = &TREE_VALUE (level), other = *prev; other;) 2286 { 2287 if (same_type_p (to_type, TREE_TYPE (other))) 2288 { 2289 if (they_hide_us) 2290 /* We are hidden. */ 2291 return 0; 2292 2293 if (we_hide_them) 2294 { 2295 /* We hide the other one. */ 2296 other = TREE_CHAIN (other); 2297 *prev = other; 2298 continue; 2299 } 2300 } 2301 prev = &TREE_CHAIN (other); 2302 other = *prev; 2303 } 2304 } 2305 } 2306 return 1; 2307} 2308 2309/* Helper for lookup_conversions_r. PARENT_CONVS is a list of lists 2310 of conversion functions, the first slot will be for the current 2311 binfo, if MY_CONVS is non-NULL. CHILD_CONVS is the list of lists 2312 of conversion functions from children of the current binfo, 2313 concatenated with conversions from elsewhere in the hierarchy -- 2314 that list begins with OTHER_CONVS. Return a single list of lists 2315 containing only conversions from the current binfo and its 2316 children. */ 2317 2318static tree 2319split_conversions (tree my_convs, tree parent_convs, 2320 tree child_convs, tree other_convs) 2321{ 2322 tree t; 2323 tree prev; 2324 2325 /* Remove the original other_convs portion from child_convs. */ 2326 for (prev = NULL, t = child_convs; 2327 t != other_convs; prev = t, t = TREE_CHAIN (t)) 2328 continue; 2329 2330 if (prev) 2331 TREE_CHAIN (prev) = NULL_TREE; 2332 else 2333 child_convs = NULL_TREE; 2334 2335 /* Attach the child convs to any we had at this level. */ 2336 if (my_convs) 2337 { 2338 my_convs = parent_convs; 2339 TREE_CHAIN (my_convs) = child_convs; 2340 } 2341 else 2342 my_convs = child_convs; 2343 2344 return my_convs; 2345} 2346 2347/* Worker for lookup_conversions. Lookup conversion functions in 2348 BINFO and its children. VIRTUAL_DEPTH is nonzero, if BINFO is in 2349 a morally virtual base, and VIRTUALNESS is nonzero, if we've 2350 encountered virtual bases already in the tree walk. PARENT_CONVS & 2351 PARENT_TPL_CONVS are lists of list of conversions within parent 2352 binfos. OTHER_CONVS and OTHER_TPL_CONVS are conversions found 2353 elsewhere in the tree. Return the conversions found within this 2354 portion of the graph in CONVS and TPL_CONVS. Return nonzero is we 2355 encountered virtualness. We keep template and non-template 2356 conversions separate, to avoid unnecessary type comparisons. 2357 2358 The located conversion functions are held in lists of lists. The 2359 TREE_VALUE of the outer list is the list of conversion functions 2360 found in a particular binfo. The TREE_PURPOSE of both the outer 2361 and inner lists is the binfo at which those conversions were 2362 found. TREE_STATIC is set for those lists within of morally 2363 virtual binfos. The TREE_VALUE of the inner list is the conversion 2364 function or overload itself. The TREE_TYPE of each inner list node 2365 is the converted-to type. */ 2366 2367static int 2368lookup_conversions_r (tree binfo, 2369 int virtual_depth, int virtualness, 2370 tree parent_convs, tree parent_tpl_convs, 2371 tree other_convs, tree other_tpl_convs, 2372 tree *convs, tree *tpl_convs) 2373{ 2374 int my_virtualness = 0; 2375 tree my_convs = NULL_TREE; 2376 tree my_tpl_convs = NULL_TREE; 2377 tree child_convs = NULL_TREE; 2378 tree child_tpl_convs = NULL_TREE; 2379 unsigned i; 2380 tree base_binfo; 2381 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo)); 2382 tree conv; 2383 2384 /* If we have no conversion operators, then don't look. */ 2385 if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo))) 2386 { 2387 *convs = *tpl_convs = NULL_TREE; 2388 2389 return 0; 2390 } 2391 2392 if (BINFO_VIRTUAL_P (binfo)) 2393 virtual_depth++; 2394 2395 /* First, locate the unhidden ones at this level. */ 2396 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 2397 vec_safe_iterate (method_vec, i, &conv); 2398 ++i) 2399 { 2400 tree cur = OVL_CURRENT (conv); 2401 2402 if (!DECL_CONV_FN_P (cur)) 2403 break; 2404 2405 if (TREE_CODE (cur) == TEMPLATE_DECL) 2406 { 2407 /* Only template conversions can be overloaded, and we must 2408 flatten them out and check each one individually. */ 2409 tree tpls; 2410 2411 for (tpls = conv; tpls; tpls = OVL_NEXT (tpls)) 2412 { 2413 tree tpl = OVL_CURRENT (tpls); 2414 tree type = DECL_CONV_FN_TYPE (tpl); 2415 2416 if (check_hidden_convs (binfo, virtual_depth, virtualness, 2417 type, parent_tpl_convs, other_tpl_convs)) 2418 { 2419 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs); 2420 TREE_TYPE (my_tpl_convs) = type; 2421 if (virtual_depth) 2422 { 2423 TREE_STATIC (my_tpl_convs) = 1; 2424 my_virtualness = 1; 2425 } 2426 } 2427 } 2428 } 2429 else 2430 { 2431 tree name = DECL_NAME (cur); 2432 2433 if (!IDENTIFIER_MARKED (name)) 2434 { 2435 tree type = DECL_CONV_FN_TYPE (cur); 2436 if (type_uses_auto (type)) 2437 { 2438 mark_used (cur); 2439 type = DECL_CONV_FN_TYPE (cur); 2440 } 2441 2442 if (check_hidden_convs (binfo, virtual_depth, virtualness, 2443 type, parent_convs, other_convs)) 2444 { 2445 my_convs = tree_cons (binfo, conv, my_convs); 2446 TREE_TYPE (my_convs) = type; 2447 if (virtual_depth) 2448 { 2449 TREE_STATIC (my_convs) = 1; 2450 my_virtualness = 1; 2451 } 2452 IDENTIFIER_MARKED (name) = 1; 2453 } 2454 } 2455 } 2456 } 2457 2458 if (my_convs) 2459 { 2460 parent_convs = tree_cons (binfo, my_convs, parent_convs); 2461 if (virtual_depth) 2462 TREE_STATIC (parent_convs) = 1; 2463 } 2464 2465 if (my_tpl_convs) 2466 { 2467 parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs); 2468 if (virtual_depth) 2469 TREE_STATIC (parent_tpl_convs) = 1; 2470 } 2471 2472 child_convs = other_convs; 2473 child_tpl_convs = other_tpl_convs; 2474 2475 /* Now iterate over each base, looking for more conversions. */ 2476 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 2477 { 2478 tree base_convs, base_tpl_convs; 2479 unsigned base_virtualness; 2480 2481 base_virtualness = lookup_conversions_r (base_binfo, 2482 virtual_depth, virtualness, 2483 parent_convs, parent_tpl_convs, 2484 child_convs, child_tpl_convs, 2485 &base_convs, &base_tpl_convs); 2486 if (base_virtualness) 2487 my_virtualness = virtualness = 1; 2488 child_convs = chainon (base_convs, child_convs); 2489 child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs); 2490 } 2491 2492 /* Unmark the conversions found at this level */ 2493 for (conv = my_convs; conv; conv = TREE_CHAIN (conv)) 2494 IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0; 2495 2496 *convs = split_conversions (my_convs, parent_convs, 2497 child_convs, other_convs); 2498 *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs, 2499 child_tpl_convs, other_tpl_convs); 2500 2501 return my_virtualness; 2502} 2503 2504/* Return a TREE_LIST containing all the non-hidden user-defined 2505 conversion functions for TYPE (and its base-classes). The 2506 TREE_VALUE of each node is the FUNCTION_DECL of the conversion 2507 function. The TREE_PURPOSE is the BINFO from which the conversion 2508 functions in this node were selected. This function is effectively 2509 performing a set of member lookups as lookup_fnfield does, but 2510 using the type being converted to as the unique key, rather than the 2511 field name. */ 2512 2513tree 2514lookup_conversions (tree type) 2515{ 2516 tree convs, tpl_convs; 2517 tree list = NULL_TREE; 2518 2519 complete_type (type); 2520 if (!CLASS_TYPE_P (type) || !TYPE_BINFO (type)) 2521 return NULL_TREE; 2522 2523 lookup_conversions_r (TYPE_BINFO (type), 0, 0, 2524 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, 2525 &convs, &tpl_convs); 2526 2527 /* Flatten the list-of-lists */ 2528 for (; convs; convs = TREE_CHAIN (convs)) 2529 { 2530 tree probe, next; 2531 2532 for (probe = TREE_VALUE (convs); probe; probe = next) 2533 { 2534 next = TREE_CHAIN (probe); 2535 2536 TREE_CHAIN (probe) = list; 2537 list = probe; 2538 } 2539 } 2540 2541 for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs)) 2542 { 2543 tree probe, next; 2544 2545 for (probe = TREE_VALUE (tpl_convs); probe; probe = next) 2546 { 2547 next = TREE_CHAIN (probe); 2548 2549 TREE_CHAIN (probe) = list; 2550 list = probe; 2551 } 2552 } 2553 2554 return list; 2555} 2556 2557/* Returns the binfo of the first direct or indirect virtual base derived 2558 from BINFO, or NULL if binfo is not via virtual. */ 2559 2560tree 2561binfo_from_vbase (tree binfo) 2562{ 2563 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo)) 2564 { 2565 if (BINFO_VIRTUAL_P (binfo)) 2566 return binfo; 2567 } 2568 return NULL_TREE; 2569} 2570 2571/* Returns the binfo of the first direct or indirect virtual base derived 2572 from BINFO up to the TREE_TYPE, LIMIT, or NULL if binfo is not 2573 via virtual. */ 2574 2575tree 2576binfo_via_virtual (tree binfo, tree limit) 2577{ 2578 if (limit && !CLASSTYPE_VBASECLASSES (limit)) 2579 /* LIMIT has no virtual bases, so BINFO cannot be via one. */ 2580 return NULL_TREE; 2581 2582 for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit); 2583 binfo = BINFO_INHERITANCE_CHAIN (binfo)) 2584 { 2585 if (BINFO_VIRTUAL_P (binfo)) 2586 return binfo; 2587 } 2588 return NULL_TREE; 2589} 2590 2591/* BINFO is a base binfo in the complete type BINFO_TYPE (HERE). 2592 Find the equivalent binfo within whatever graph HERE is located. 2593 This is the inverse of original_binfo. */ 2594 2595tree 2596copied_binfo (tree binfo, tree here) 2597{ 2598 tree result = NULL_TREE; 2599 2600 if (BINFO_VIRTUAL_P (binfo)) 2601 { 2602 tree t; 2603 2604 for (t = here; BINFO_INHERITANCE_CHAIN (t); 2605 t = BINFO_INHERITANCE_CHAIN (t)) 2606 continue; 2607 2608 result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t)); 2609 } 2610 else if (BINFO_INHERITANCE_CHAIN (binfo)) 2611 { 2612 tree cbinfo; 2613 tree base_binfo; 2614 int ix; 2615 2616 cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here); 2617 for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++) 2618 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo))) 2619 { 2620 result = base_binfo; 2621 break; 2622 } 2623 } 2624 else 2625 { 2626 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo))); 2627 result = here; 2628 } 2629 2630 gcc_assert (result); 2631 return result; 2632} 2633 2634tree 2635binfo_for_vbase (tree base, tree t) 2636{ 2637 unsigned ix; 2638 tree binfo; 2639 vec<tree, va_gc> *vbases; 2640 2641 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0; 2642 vec_safe_iterate (vbases, ix, &binfo); ix++) 2643 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base)) 2644 return binfo; 2645 return NULL; 2646} 2647 2648/* BINFO is some base binfo of HERE, within some other 2649 hierarchy. Return the equivalent binfo, but in the hierarchy 2650 dominated by HERE. This is the inverse of copied_binfo. If BINFO 2651 is not a base binfo of HERE, returns NULL_TREE. */ 2652 2653tree 2654original_binfo (tree binfo, tree here) 2655{ 2656 tree result = NULL; 2657 2658 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here))) 2659 result = here; 2660 else if (BINFO_VIRTUAL_P (binfo)) 2661 result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here)) 2662 ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here)) 2663 : NULL_TREE); 2664 else if (BINFO_INHERITANCE_CHAIN (binfo)) 2665 { 2666 tree base_binfos; 2667 2668 base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here); 2669 if (base_binfos) 2670 { 2671 int ix; 2672 tree base_binfo; 2673 2674 for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++) 2675 if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), 2676 BINFO_TYPE (binfo))) 2677 { 2678 result = base_binfo; 2679 break; 2680 } 2681 } 2682 } 2683 2684 return result; 2685} 2686 2687