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