1 //===------ CXXInheritance.cpp - C++ Inheritance ----------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file provides routines that help analyzing C++ inheritance hierarchies. 11 // 12 //===----------------------------------------------------------------------===// 13 #include "clang/AST/CXXInheritance.h" 14 #include "clang/AST/RecordLayout.h" 15 #include "clang/AST/DeclCXX.h" 16 #include <algorithm> 17 #include <set> 18 19 using namespace clang; 20 21 /// \brief Computes the set of declarations referenced by these base 22 /// paths. 23 void CXXBasePaths::ComputeDeclsFound() { 24 assert(NumDeclsFound == 0 && !DeclsFound && 25 "Already computed the set of declarations"); 26 27 SmallVector<NamedDecl *, 8> Decls; 28 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path) 29 Decls.push_back(*Path->Decls.first); 30 31 // Eliminate duplicated decls. 32 llvm::array_pod_sort(Decls.begin(), Decls.end()); 33 Decls.erase(std::unique(Decls.begin(), Decls.end()), Decls.end()); 34 35 NumDeclsFound = Decls.size(); 36 DeclsFound = new NamedDecl * [NumDeclsFound]; 37 std::copy(Decls.begin(), Decls.end(), DeclsFound); 38 } 39 40 CXXBasePaths::decl_iterator CXXBasePaths::found_decls_begin() { 41 if (NumDeclsFound == 0) 42 ComputeDeclsFound(); 43 return DeclsFound; 44 } 45 46 CXXBasePaths::decl_iterator CXXBasePaths::found_decls_end() { 47 if (NumDeclsFound == 0) 48 ComputeDeclsFound(); 49 return DeclsFound + NumDeclsFound; 50 } 51 52 /// isAmbiguous - Determines whether the set of paths provided is 53 /// ambiguous, i.e., there are two or more paths that refer to 54 /// different base class subobjects of the same type. BaseType must be 55 /// an unqualified, canonical class type. 56 bool CXXBasePaths::isAmbiguous(CanQualType BaseType) { 57 BaseType = BaseType.getUnqualifiedType(); 58 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; 59 return Subobjects.second + (Subobjects.first? 1 : 0) > 1; 60 } 61 62 /// clear - Clear out all prior path information. 63 void CXXBasePaths::clear() { 64 Paths.clear(); 65 ClassSubobjects.clear(); 66 ScratchPath.clear(); 67 DetectedVirtual = 0; 68 } 69 70 /// @brief Swaps the contents of this CXXBasePaths structure with the 71 /// contents of Other. 72 void CXXBasePaths::swap(CXXBasePaths &Other) { 73 std::swap(Origin, Other.Origin); 74 Paths.swap(Other.Paths); 75 ClassSubobjects.swap(Other.ClassSubobjects); 76 std::swap(FindAmbiguities, Other.FindAmbiguities); 77 std::swap(RecordPaths, Other.RecordPaths); 78 std::swap(DetectVirtual, Other.DetectVirtual); 79 std::swap(DetectedVirtual, Other.DetectedVirtual); 80 } 81 82 bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const { 83 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, 84 /*DetectVirtual=*/false); 85 return isDerivedFrom(Base, Paths); 86 } 87 88 bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base, 89 CXXBasePaths &Paths) const { 90 if (getCanonicalDecl() == Base->getCanonicalDecl()) 91 return false; 92 93 Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); 94 return lookupInBases(&FindBaseClass, 95 const_cast<CXXRecordDecl*>(Base->getCanonicalDecl()), 96 Paths); 97 } 98 99 bool CXXRecordDecl::isVirtuallyDerivedFrom(CXXRecordDecl *Base) const { 100 if (!getNumVBases()) 101 return false; 102 103 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, 104 /*DetectVirtual=*/false); 105 106 if (getCanonicalDecl() == Base->getCanonicalDecl()) 107 return false; 108 109 Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); 110 return lookupInBases(&FindVirtualBaseClass, Base->getCanonicalDecl(), Paths); 111 } 112 113 static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) { 114 // OpaqueTarget is a CXXRecordDecl*. 115 return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget; 116 } 117 118 bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const { 119 return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl()); 120 } 121 122 bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches, 123 void *OpaqueData, 124 bool AllowShortCircuit) const { 125 SmallVector<const CXXRecordDecl*, 8> Queue; 126 127 const CXXRecordDecl *Record = this; 128 bool AllMatches = true; 129 while (true) { 130 for (CXXRecordDecl::base_class_const_iterator 131 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 132 const RecordType *Ty = I->getType()->getAs<RecordType>(); 133 if (!Ty) { 134 if (AllowShortCircuit) return false; 135 AllMatches = false; 136 continue; 137 } 138 139 CXXRecordDecl *Base = 140 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition()); 141 if (!Base) { 142 if (AllowShortCircuit) return false; 143 AllMatches = false; 144 continue; 145 } 146 147 Queue.push_back(Base); 148 if (!BaseMatches(Base, OpaqueData)) { 149 if (AllowShortCircuit) return false; 150 AllMatches = false; 151 continue; 152 } 153 } 154 155 if (Queue.empty()) break; 156 Record = Queue.back(); // not actually a queue. 157 Queue.pop_back(); 158 } 159 160 return AllMatches; 161 } 162 163 bool CXXBasePaths::lookupInBases(ASTContext &Context, 164 const CXXRecordDecl *Record, 165 CXXRecordDecl::BaseMatchesCallback *BaseMatches, 166 void *UserData) { 167 bool FoundPath = false; 168 169 // The access of the path down to this record. 170 AccessSpecifier AccessToHere = ScratchPath.Access; 171 bool IsFirstStep = ScratchPath.empty(); 172 173 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(), 174 BaseSpecEnd = Record->bases_end(); 175 BaseSpec != BaseSpecEnd; 176 ++BaseSpec) { 177 // Find the record of the base class subobjects for this type. 178 QualType BaseType = Context.getCanonicalType(BaseSpec->getType()) 179 .getUnqualifiedType(); 180 181 // C++ [temp.dep]p3: 182 // In the definition of a class template or a member of a class template, 183 // if a base class of the class template depends on a template-parameter, 184 // the base class scope is not examined during unqualified name lookup 185 // either at the point of definition of the class template or member or 186 // during an instantiation of the class tem- plate or member. 187 if (BaseType->isDependentType()) 188 continue; 189 190 // Determine whether we need to visit this base class at all, 191 // updating the count of subobjects appropriately. 192 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; 193 bool VisitBase = true; 194 bool SetVirtual = false; 195 if (BaseSpec->isVirtual()) { 196 VisitBase = !Subobjects.first; 197 Subobjects.first = true; 198 if (isDetectingVirtual() && DetectedVirtual == 0) { 199 // If this is the first virtual we find, remember it. If it turns out 200 // there is no base path here, we'll reset it later. 201 DetectedVirtual = BaseType->getAs<RecordType>(); 202 SetVirtual = true; 203 } 204 } else 205 ++Subobjects.second; 206 207 if (isRecordingPaths()) { 208 // Add this base specifier to the current path. 209 CXXBasePathElement Element; 210 Element.Base = &*BaseSpec; 211 Element.Class = Record; 212 if (BaseSpec->isVirtual()) 213 Element.SubobjectNumber = 0; 214 else 215 Element.SubobjectNumber = Subobjects.second; 216 ScratchPath.push_back(Element); 217 218 // Calculate the "top-down" access to this base class. 219 // The spec actually describes this bottom-up, but top-down is 220 // equivalent because the definition works out as follows: 221 // 1. Write down the access along each step in the inheritance 222 // chain, followed by the access of the decl itself. 223 // For example, in 224 // class A { public: int foo; }; 225 // class B : protected A {}; 226 // class C : public B {}; 227 // class D : private C {}; 228 // we would write: 229 // private public protected public 230 // 2. If 'private' appears anywhere except far-left, access is denied. 231 // 3. Otherwise, overall access is determined by the most restrictive 232 // access in the sequence. 233 if (IsFirstStep) 234 ScratchPath.Access = BaseSpec->getAccessSpecifier(); 235 else 236 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere, 237 BaseSpec->getAccessSpecifier()); 238 } 239 240 // Track whether there's a path involving this specific base. 241 bool FoundPathThroughBase = false; 242 243 if (BaseMatches(BaseSpec, ScratchPath, UserData)) { 244 // We've found a path that terminates at this base. 245 FoundPath = FoundPathThroughBase = true; 246 if (isRecordingPaths()) { 247 // We have a path. Make a copy of it before moving on. 248 Paths.push_back(ScratchPath); 249 } else if (!isFindingAmbiguities()) { 250 // We found a path and we don't care about ambiguities; 251 // return immediately. 252 return FoundPath; 253 } 254 } else if (VisitBase) { 255 CXXRecordDecl *BaseRecord 256 = cast<CXXRecordDecl>(BaseSpec->getType()->getAs<RecordType>() 257 ->getDecl()); 258 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) { 259 // C++ [class.member.lookup]p2: 260 // A member name f in one sub-object B hides a member name f in 261 // a sub-object A if A is a base class sub-object of B. Any 262 // declarations that are so hidden are eliminated from 263 // consideration. 264 265 // There is a path to a base class that meets the criteria. If we're 266 // not collecting paths or finding ambiguities, we're done. 267 FoundPath = FoundPathThroughBase = true; 268 if (!isFindingAmbiguities()) 269 return FoundPath; 270 } 271 } 272 273 // Pop this base specifier off the current path (if we're 274 // collecting paths). 275 if (isRecordingPaths()) { 276 ScratchPath.pop_back(); 277 } 278 279 // If we set a virtual earlier, and this isn't a path, forget it again. 280 if (SetVirtual && !FoundPathThroughBase) { 281 DetectedVirtual = 0; 282 } 283 } 284 285 // Reset the scratch path access. 286 ScratchPath.Access = AccessToHere; 287 288 return FoundPath; 289 } 290 291 bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches, 292 void *UserData, 293 CXXBasePaths &Paths) const { 294 // If we didn't find anything, report that. 295 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData)) 296 return false; 297 298 // If we're not recording paths or we won't ever find ambiguities, 299 // we're done. 300 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities()) 301 return true; 302 303 // C++ [class.member.lookup]p6: 304 // When virtual base classes are used, a hidden declaration can be 305 // reached along a path through the sub-object lattice that does 306 // not pass through the hiding declaration. This is not an 307 // ambiguity. The identical use with nonvirtual base classes is an 308 // ambiguity; in that case there is no unique instance of the name 309 // that hides all the others. 310 // 311 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy 312 // way to make it any faster. 313 for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end(); 314 P != PEnd; /* increment in loop */) { 315 bool Hidden = false; 316 317 for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end(); 318 PE != PEEnd && !Hidden; ++PE) { 319 if (PE->Base->isVirtual()) { 320 CXXRecordDecl *VBase = 0; 321 if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>()) 322 VBase = cast<CXXRecordDecl>(Record->getDecl()); 323 if (!VBase) 324 break; 325 326 // The declaration(s) we found along this path were found in a 327 // subobject of a virtual base. Check whether this virtual 328 // base is a subobject of any other path; if so, then the 329 // declaration in this path are hidden by that patch. 330 for (CXXBasePaths::paths_iterator HidingP = Paths.begin(), 331 HidingPEnd = Paths.end(); 332 HidingP != HidingPEnd; 333 ++HidingP) { 334 CXXRecordDecl *HidingClass = 0; 335 if (const RecordType *Record 336 = HidingP->back().Base->getType()->getAs<RecordType>()) 337 HidingClass = cast<CXXRecordDecl>(Record->getDecl()); 338 if (!HidingClass) 339 break; 340 341 if (HidingClass->isVirtuallyDerivedFrom(VBase)) { 342 Hidden = true; 343 break; 344 } 345 } 346 } 347 } 348 349 if (Hidden) 350 P = Paths.Paths.erase(P); 351 else 352 ++P; 353 } 354 355 return true; 356 } 357 358 bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier, 359 CXXBasePath &Path, 360 void *BaseRecord) { 361 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord && 362 "User data for FindBaseClass is not canonical!"); 363 return Specifier->getType()->getAs<RecordType>()->getDecl() 364 ->getCanonicalDecl() == BaseRecord; 365 } 366 367 bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, 368 CXXBasePath &Path, 369 void *BaseRecord) { 370 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord && 371 "User data for FindBaseClass is not canonical!"); 372 return Specifier->isVirtual() && 373 Specifier->getType()->getAs<RecordType>()->getDecl() 374 ->getCanonicalDecl() == BaseRecord; 375 } 376 377 bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier, 378 CXXBasePath &Path, 379 void *Name) { 380 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); 381 382 DeclarationName N = DeclarationName::getFromOpaquePtr(Name); 383 for (Path.Decls = BaseRecord->lookup(N); 384 Path.Decls.first != Path.Decls.second; 385 ++Path.Decls.first) { 386 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag)) 387 return true; 388 } 389 390 return false; 391 } 392 393 bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier, 394 CXXBasePath &Path, 395 void *Name) { 396 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); 397 398 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member; 399 DeclarationName N = DeclarationName::getFromOpaquePtr(Name); 400 for (Path.Decls = BaseRecord->lookup(N); 401 Path.Decls.first != Path.Decls.second; 402 ++Path.Decls.first) { 403 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS)) 404 return true; 405 } 406 407 return false; 408 } 409 410 bool CXXRecordDecl:: 411 FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, 412 CXXBasePath &Path, 413 void *Name) { 414 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); 415 416 DeclarationName N = DeclarationName::getFromOpaquePtr(Name); 417 for (Path.Decls = BaseRecord->lookup(N); 418 Path.Decls.first != Path.Decls.second; 419 ++Path.Decls.first) { 420 // FIXME: Refactor the "is it a nested-name-specifier?" check 421 if (isa<TypedefNameDecl>(*Path.Decls.first) || 422 (*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag)) 423 return true; 424 } 425 426 return false; 427 } 428 429 void OverridingMethods::add(unsigned OverriddenSubobject, 430 UniqueVirtualMethod Overriding) { 431 SmallVector<UniqueVirtualMethod, 4> &SubobjectOverrides 432 = Overrides[OverriddenSubobject]; 433 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(), 434 Overriding) == SubobjectOverrides.end()) 435 SubobjectOverrides.push_back(Overriding); 436 } 437 438 void OverridingMethods::add(const OverridingMethods &Other) { 439 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) { 440 for (overriding_const_iterator M = I->second.begin(), 441 MEnd = I->second.end(); 442 M != MEnd; 443 ++M) 444 add(I->first, *M); 445 } 446 } 447 448 void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) { 449 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) { 450 I->second.clear(); 451 I->second.push_back(Overriding); 452 } 453 } 454 455 456 namespace { 457 class FinalOverriderCollector { 458 /// \brief The number of subobjects of a given class type that 459 /// occur within the class hierarchy. 460 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount; 461 462 /// \brief Overriders for each virtual base subobject. 463 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders; 464 465 CXXFinalOverriderMap FinalOverriders; 466 467 public: 468 ~FinalOverriderCollector(); 469 470 void Collect(const CXXRecordDecl *RD, bool VirtualBase, 471 const CXXRecordDecl *InVirtualSubobject, 472 CXXFinalOverriderMap &Overriders); 473 }; 474 } 475 476 void FinalOverriderCollector::Collect(const CXXRecordDecl *RD, 477 bool VirtualBase, 478 const CXXRecordDecl *InVirtualSubobject, 479 CXXFinalOverriderMap &Overriders) { 480 unsigned SubobjectNumber = 0; 481 if (!VirtualBase) 482 SubobjectNumber 483 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())]; 484 485 for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), 486 BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { 487 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { 488 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); 489 if (!BaseDecl->isPolymorphic()) 490 continue; 491 492 if (Overriders.empty() && !Base->isVirtual()) { 493 // There are no other overriders of virtual member functions, 494 // so let the base class fill in our overriders for us. 495 Collect(BaseDecl, false, InVirtualSubobject, Overriders); 496 continue; 497 } 498 499 // Collect all of the overridders from the base class subobject 500 // and merge them into the set of overridders for this class. 501 // For virtual base classes, populate or use the cached virtual 502 // overrides so that we do not walk the virtual base class (and 503 // its base classes) more than once. 504 CXXFinalOverriderMap ComputedBaseOverriders; 505 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders; 506 if (Base->isVirtual()) { 507 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl]; 508 if (!MyVirtualOverriders) { 509 MyVirtualOverriders = new CXXFinalOverriderMap; 510 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders); 511 } 512 513 BaseOverriders = MyVirtualOverriders; 514 } else 515 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders); 516 517 // Merge the overriders from this base class into our own set of 518 // overriders. 519 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(), 520 OMEnd = BaseOverriders->end(); 521 OM != OMEnd; 522 ++OM) { 523 const CXXMethodDecl *CanonOM 524 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl()); 525 Overriders[CanonOM].add(OM->second); 526 } 527 } 528 } 529 530 for (CXXRecordDecl::method_iterator M = RD->method_begin(), 531 MEnd = RD->method_end(); 532 M != MEnd; 533 ++M) { 534 // We only care about virtual methods. 535 if (!M->isVirtual()) 536 continue; 537 538 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl()); 539 540 if (CanonM->begin_overridden_methods() 541 == CanonM->end_overridden_methods()) { 542 // This is a new virtual function that does not override any 543 // other virtual function. Add it to the map of virtual 544 // functions for which we are tracking overridders. 545 546 // C++ [class.virtual]p2: 547 // For convenience we say that any virtual function overrides itself. 548 Overriders[CanonM].add(SubobjectNumber, 549 UniqueVirtualMethod(CanonM, SubobjectNumber, 550 InVirtualSubobject)); 551 continue; 552 } 553 554 // This virtual method overrides other virtual methods, so it does 555 // not add any new slots into the set of overriders. Instead, we 556 // replace entries in the set of overriders with the new 557 // overrider. To do so, we dig down to the original virtual 558 // functions using data recursion and update all of the methods it 559 // overrides. 560 typedef std::pair<CXXMethodDecl::method_iterator, 561 CXXMethodDecl::method_iterator> OverriddenMethods; 562 SmallVector<OverriddenMethods, 4> Stack; 563 Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(), 564 CanonM->end_overridden_methods())); 565 while (!Stack.empty()) { 566 OverriddenMethods OverMethods = Stack.back(); 567 Stack.pop_back(); 568 569 for (; OverMethods.first != OverMethods.second; ++OverMethods.first) { 570 const CXXMethodDecl *CanonOM 571 = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl()); 572 573 // C++ [class.virtual]p2: 574 // A virtual member function C::vf of a class object S is 575 // a final overrider unless the most derived class (1.8) 576 // of which S is a base class subobject (if any) declares 577 // or inherits another member function that overrides vf. 578 // 579 // Treating this object like the most derived class, we 580 // replace any overrides from base classes with this 581 // overriding virtual function. 582 Overriders[CanonOM].replaceAll( 583 UniqueVirtualMethod(CanonM, SubobjectNumber, 584 InVirtualSubobject)); 585 586 if (CanonOM->begin_overridden_methods() 587 == CanonOM->end_overridden_methods()) 588 continue; 589 590 // Continue recursion to the methods that this virtual method 591 // overrides. 592 Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(), 593 CanonOM->end_overridden_methods())); 594 } 595 } 596 597 // C++ [class.virtual]p2: 598 // For convenience we say that any virtual function overrides itself. 599 Overriders[CanonM].add(SubobjectNumber, 600 UniqueVirtualMethod(CanonM, SubobjectNumber, 601 InVirtualSubobject)); 602 } 603 } 604 605 FinalOverriderCollector::~FinalOverriderCollector() { 606 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator 607 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end(); 608 VO != VOEnd; 609 ++VO) 610 delete VO->second; 611 } 612 613 void 614 CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const { 615 FinalOverriderCollector Collector; 616 Collector.Collect(this, false, 0, FinalOverriders); 617 618 // Weed out any final overriders that come from virtual base class 619 // subobjects that were hidden by other subobjects along any path. 620 // This is the final-overrider variant of C++ [class.member.lookup]p10. 621 for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(), 622 OMEnd = FinalOverriders.end(); 623 OM != OMEnd; 624 ++OM) { 625 for (OverridingMethods::iterator SO = OM->second.begin(), 626 SOEnd = OM->second.end(); 627 SO != SOEnd; 628 ++SO) { 629 SmallVector<UniqueVirtualMethod, 4> &Overriding = SO->second; 630 if (Overriding.size() < 2) 631 continue; 632 633 for (SmallVector<UniqueVirtualMethod, 4>::iterator 634 Pos = Overriding.begin(), PosEnd = Overriding.end(); 635 Pos != PosEnd; 636 /* increment in loop */) { 637 if (!Pos->InVirtualSubobject) { 638 ++Pos; 639 continue; 640 } 641 642 // We have an overriding method in a virtual base class 643 // subobject (or non-virtual base class subobject thereof); 644 // determine whether there exists an other overriding method 645 // in a base class subobject that hides the virtual base class 646 // subobject. 647 bool Hidden = false; 648 for (SmallVector<UniqueVirtualMethod, 4>::iterator 649 OP = Overriding.begin(), OPEnd = Overriding.end(); 650 OP != OPEnd && !Hidden; 651 ++OP) { 652 if (Pos == OP) 653 continue; 654 655 if (OP->Method->getParent()->isVirtuallyDerivedFrom( 656 const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject))) 657 Hidden = true; 658 } 659 660 if (Hidden) { 661 // The current overriding function is hidden by another 662 // overriding function; remove this one. 663 Pos = Overriding.erase(Pos); 664 PosEnd = Overriding.end(); 665 } else { 666 ++Pos; 667 } 668 } 669 } 670 } 671 } 672 673 static void 674 AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context, 675 CXXIndirectPrimaryBaseSet& Bases) { 676 // If the record has a virtual primary base class, add it to our set. 677 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 678 if (Layout.isPrimaryBaseVirtual()) 679 Bases.insert(Layout.getPrimaryBase()); 680 681 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 682 E = RD->bases_end(); I != E; ++I) { 683 assert(!I->getType()->isDependentType() && 684 "Cannot get indirect primary bases for class with dependent bases."); 685 686 const CXXRecordDecl *BaseDecl = 687 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 688 689 // Only bases with virtual bases participate in computing the 690 // indirect primary virtual base classes. 691 if (BaseDecl->getNumVBases()) 692 AddIndirectPrimaryBases(BaseDecl, Context, Bases); 693 } 694 695 } 696 697 void 698 CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const { 699 ASTContext &Context = getASTContext(); 700 701 if (!getNumVBases()) 702 return; 703 704 for (CXXRecordDecl::base_class_const_iterator I = bases_begin(), 705 E = bases_end(); I != E; ++I) { 706 assert(!I->getType()->isDependentType() && 707 "Cannot get indirect primary bases for class with dependent bases."); 708 709 const CXXRecordDecl *BaseDecl = 710 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 711 712 // Only bases with virtual bases participate in computing the 713 // indirect primary virtual base classes. 714 if (BaseDecl->getNumVBases()) 715 AddIndirectPrimaryBases(BaseDecl, Context, Bases); 716 } 717 } 718 719