CXXInheritance.cpp 26 KB

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