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 BaseOverriders = MyVirtualOverriders; 509 if (!MyVirtualOverriders) { 510 MyVirtualOverriders = new CXXFinalOverriderMap; 511 512 // Collect may cause VirtualOverriders to reallocate, invalidating the 513 // MyVirtualOverriders reference. Set BaseOverriders to the right 514 // value now. 515 BaseOverriders = MyVirtualOverriders; 516 517 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders); 518 } 519 } else 520 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders); 521 522 // Merge the overriders from this base class into our own set of 523 // overriders. 524 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(), 525 OMEnd = BaseOverriders->end(); 526 OM != OMEnd; 527 ++OM) { 528 const CXXMethodDecl *CanonOM 529 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl()); 530 Overriders[CanonOM].add(OM->second); 531 } 532 } 533 } 534 535 for (CXXRecordDecl::method_iterator M = RD->method_begin(), 536 MEnd = RD->method_end(); 537 M != MEnd; 538 ++M) { 539 // We only care about virtual methods. 540 if (!M->isVirtual()) 541 continue; 542 543 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl()); 544 545 if (CanonM->begin_overridden_methods() 546 == CanonM->end_overridden_methods()) { 547 // This is a new virtual function that does not override any 548 // other virtual function. Add it to the map of virtual 549 // functions for which we are tracking overridders. 550 551 // C++ [class.virtual]p2: 552 // For convenience we say that any virtual function overrides itself. 553 Overriders[CanonM].add(SubobjectNumber, 554 UniqueVirtualMethod(CanonM, SubobjectNumber, 555 InVirtualSubobject)); 556 continue; 557 } 558 559 // This virtual method overrides other virtual methods, so it does 560 // not add any new slots into the set of overriders. Instead, we 561 // replace entries in the set of overriders with the new 562 // overrider. To do so, we dig down to the original virtual 563 // functions using data recursion and update all of the methods it 564 // overrides. 565 typedef std::pair<CXXMethodDecl::method_iterator, 566 CXXMethodDecl::method_iterator> OverriddenMethods; 567 SmallVector<OverriddenMethods, 4> Stack; 568 Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(), 569 CanonM->end_overridden_methods())); 570 while (!Stack.empty()) { 571 OverriddenMethods OverMethods = Stack.back(); 572 Stack.pop_back(); 573 574 for (; OverMethods.first != OverMethods.second; ++OverMethods.first) { 575 const CXXMethodDecl *CanonOM 576 = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl()); 577 578 // C++ [class.virtual]p2: 579 // A virtual member function C::vf of a class object S is 580 // a final overrider unless the most derived class (1.8) 581 // of which S is a base class subobject (if any) declares 582 // or inherits another member function that overrides vf. 583 // 584 // Treating this object like the most derived class, we 585 // replace any overrides from base classes with this 586 // overriding virtual function. 587 Overriders[CanonOM].replaceAll( 588 UniqueVirtualMethod(CanonM, SubobjectNumber, 589 InVirtualSubobject)); 590 591 if (CanonOM->begin_overridden_methods() 592 == CanonOM->end_overridden_methods()) 593 continue; 594 595 // Continue recursion to the methods that this virtual method 596 // overrides. 597 Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(), 598 CanonOM->end_overridden_methods())); 599 } 600 } 601 602 // C++ [class.virtual]p2: 603 // For convenience we say that any virtual function overrides itself. 604 Overriders[CanonM].add(SubobjectNumber, 605 UniqueVirtualMethod(CanonM, SubobjectNumber, 606 InVirtualSubobject)); 607 } 608 } 609 610 FinalOverriderCollector::~FinalOverriderCollector() { 611 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator 612 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end(); 613 VO != VOEnd; 614 ++VO) 615 delete VO->second; 616 } 617 618 void 619 CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const { 620 FinalOverriderCollector Collector; 621 Collector.Collect(this, false, 0, FinalOverriders); 622 623 // Weed out any final overriders that come from virtual base class 624 // subobjects that were hidden by other subobjects along any path. 625 // This is the final-overrider variant of C++ [class.member.lookup]p10. 626 for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(), 627 OMEnd = FinalOverriders.end(); 628 OM != OMEnd; 629 ++OM) { 630 for (OverridingMethods::iterator SO = OM->second.begin(), 631 SOEnd = OM->second.end(); 632 SO != SOEnd; 633 ++SO) { 634 SmallVector<UniqueVirtualMethod, 4> &Overriding = SO->second; 635 if (Overriding.size() < 2) 636 continue; 637 638 for (SmallVector<UniqueVirtualMethod, 4>::iterator 639 Pos = Overriding.begin(), PosEnd = Overriding.end(); 640 Pos != PosEnd; 641 /* increment in loop */) { 642 if (!Pos->InVirtualSubobject) { 643 ++Pos; 644 continue; 645 } 646 647 // We have an overriding method in a virtual base class 648 // subobject (or non-virtual base class subobject thereof); 649 // determine whether there exists an other overriding method 650 // in a base class subobject that hides the virtual base class 651 // subobject. 652 bool Hidden = false; 653 for (SmallVector<UniqueVirtualMethod, 4>::iterator 654 OP = Overriding.begin(), OPEnd = Overriding.end(); 655 OP != OPEnd && !Hidden; 656 ++OP) { 657 if (Pos == OP) 658 continue; 659 660 if (OP->Method->getParent()->isVirtuallyDerivedFrom( 661 const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject))) 662 Hidden = true; 663 } 664 665 if (Hidden) { 666 // The current overriding function is hidden by another 667 // overriding function; remove this one. 668 Pos = Overriding.erase(Pos); 669 PosEnd = Overriding.end(); 670 } else { 671 ++Pos; 672 } 673 } 674 } 675 } 676 } 677 678 static void 679 AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context, 680 CXXIndirectPrimaryBaseSet& Bases) { 681 // If the record has a virtual primary base class, add it to our set. 682 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 683 if (Layout.isPrimaryBaseVirtual()) 684 Bases.insert(Layout.getPrimaryBase()); 685 686 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 687 E = RD->bases_end(); I != E; ++I) { 688 assert(!I->getType()->isDependentType() && 689 "Cannot get indirect primary bases for class with dependent bases."); 690 691 const CXXRecordDecl *BaseDecl = 692 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 693 694 // Only bases with virtual bases participate in computing the 695 // indirect primary virtual base classes. 696 if (BaseDecl->getNumVBases()) 697 AddIndirectPrimaryBases(BaseDecl, Context, Bases); 698 } 699 700 } 701 702 void 703 CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const { 704 ASTContext &Context = getASTContext(); 705 706 if (!getNumVBases()) 707 return; 708 709 for (CXXRecordDecl::base_class_const_iterator I = bases_begin(), 710 E = bases_end(); I != E; ++I) { 711 assert(!I->getType()->isDependentType() && 712 "Cannot get indirect primary bases for class with dependent bases."); 713 714 const CXXRecordDecl *BaseDecl = 715 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 716 717 // Only bases with virtual bases participate in computing the 718 // indirect primary virtual base classes. 719 if (BaseDecl->getNumVBases()) 720 AddIndirectPrimaryBases(BaseDecl, Context, Bases); 721 } 722 } 723 724