blob: 247f3753f08d2aeba978c9836d404b58b199f6bb [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"
Benjamin Kramerd4f51982012-07-04 18:45:14 +000014#include "clang/AST/ASTContext.h"
Anders Carlsson46170f92010-11-24 22:50:27 +000015#include "clang/AST/RecordLayout.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/DeclCXX.h"
17#include <algorithm>
18#include <set>
19
20using namespace clang;
21
22/// \brief Computes the set of declarations referenced by these base
23/// paths.
24void CXXBasePaths::ComputeDeclsFound() {
25 assert(NumDeclsFound == 0 && !DeclsFound &&
26 "Already computed the set of declarations");
Benjamin Kramerd0e49e52012-02-23 15:18:31 +000027
Douglas Gregor925d58c2012-08-23 05:05:18 +000028 llvm::SmallPtrSet<NamedDecl *, 8> KnownDecls;
Benjamin Kramerd0e49e52012-02-23 15:18:31 +000029 SmallVector<NamedDecl *, 8> Decls;
30 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
Douglas Gregor925d58c2012-08-23 05:05:18 +000031 if (KnownDecls.insert(*Path->Decls.first))
32 Decls.push_back(*Path->Decls.first);
Benjamin Kramerd0e49e52012-02-23 15:18:31 +000033
Douglas Gregora8f32e02009-10-06 17:59:45 +000034 NumDeclsFound = Decls.size();
35 DeclsFound = new NamedDecl * [NumDeclsFound];
36 std::copy(Decls.begin(), Decls.end(), DeclsFound);
37}
38
39CXXBasePaths::decl_iterator CXXBasePaths::found_decls_begin() {
40 if (NumDeclsFound == 0)
41 ComputeDeclsFound();
42 return DeclsFound;
43}
44
45CXXBasePaths::decl_iterator CXXBasePaths::found_decls_end() {
46 if (NumDeclsFound == 0)
47 ComputeDeclsFound();
48 return DeclsFound + NumDeclsFound;
49}
50
51/// isAmbiguous - Determines whether the set of paths provided is
52/// ambiguous, i.e., there are two or more paths that refer to
53/// different base class subobjects of the same type. BaseType must be
54/// an unqualified, canonical class type.
Douglas Gregore0d5fe22010-05-21 20:29:55 +000055bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
56 BaseType = BaseType.getUnqualifiedType();
Douglas Gregora8f32e02009-10-06 17:59:45 +000057 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
58 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
59}
60
61/// clear - Clear out all prior path information.
62void CXXBasePaths::clear() {
63 Paths.clear();
64 ClassSubobjects.clear();
65 ScratchPath.clear();
66 DetectedVirtual = 0;
67}
68
69/// @brief Swaps the contents of this CXXBasePaths structure with the
70/// contents of Other.
71void CXXBasePaths::swap(CXXBasePaths &Other) {
72 std::swap(Origin, Other.Origin);
73 Paths.swap(Other.Paths);
74 ClassSubobjects.swap(Other.ClassSubobjects);
75 std::swap(FindAmbiguities, Other.FindAmbiguities);
76 std::swap(RecordPaths, Other.RecordPaths);
77 std::swap(DetectVirtual, Other.DetectVirtual);
78 std::swap(DetectedVirtual, Other.DetectedVirtual);
79}
80
John McCalld89d30f2011-01-28 22:02:36 +000081bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregora8f32e02009-10-06 17:59:45 +000082 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
83 /*DetectVirtual=*/false);
84 return isDerivedFrom(Base, Paths);
85}
86
John McCalld89d30f2011-01-28 22:02:36 +000087bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
88 CXXBasePaths &Paths) const {
Douglas Gregora8f32e02009-10-06 17:59:45 +000089 if (getCanonicalDecl() == Base->getCanonicalDecl())
90 return false;
91
John McCallaf8e6ed2009-11-12 03:15:40 +000092 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
John McCalld89d30f2011-01-28 22:02:36 +000093 return lookupInBases(&FindBaseClass,
94 const_cast<CXXRecordDecl*>(Base->getCanonicalDecl()),
95 Paths);
Douglas Gregora8f32e02009-10-06 17:59:45 +000096}
97
Jordan Rose2aa800a2012-08-08 18:23:20 +000098bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
Anders Carlsson1c4c3972010-06-04 01:40:08 +000099 if (!getNumVBases())
100 return false;
101
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000102 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
103 /*DetectVirtual=*/false);
104
105 if (getCanonicalDecl() == Base->getCanonicalDecl())
106 return false;
107
Jordan Rose2aa800a2012-08-08 18:23:20 +0000108 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
109
110 const void *BasePtr = static_cast<const void*>(Base->getCanonicalDecl());
111 return lookupInBases(&FindVirtualBaseClass,
112 const_cast<void *>(BasePtr),
113 Paths);
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000114}
115
John McCalle8174bc2009-12-08 07:42:38 +0000116static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) {
117 // OpaqueTarget is a CXXRecordDecl*.
118 return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget;
119}
120
121bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
122 return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl());
123}
124
125bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches,
126 void *OpaqueData,
127 bool AllowShortCircuit) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCalle8174bc2009-12-08 07:42:38 +0000129
130 const CXXRecordDecl *Record = this;
131 bool AllMatches = true;
132 while (true) {
133 for (CXXRecordDecl::base_class_const_iterator
134 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
135 const RecordType *Ty = I->getType()->getAs<RecordType>();
136 if (!Ty) {
137 if (AllowShortCircuit) return false;
138 AllMatches = false;
139 continue;
140 }
141
Anders Carlssonca910e82009-12-09 04:26:02 +0000142 CXXRecordDecl *Base =
Douglas Gregor952b0172010-02-11 01:04:33 +0000143 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
John McCalle8174bc2009-12-08 07:42:38 +0000144 if (!Base) {
145 if (AllowShortCircuit) return false;
146 AllMatches = false;
147 continue;
148 }
149
Anders Carlssonca910e82009-12-09 04:26:02 +0000150 Queue.push_back(Base);
151 if (!BaseMatches(Base, OpaqueData)) {
John McCalle8174bc2009-12-08 07:42:38 +0000152 if (AllowShortCircuit) return false;
153 AllMatches = false;
154 continue;
155 }
156 }
157
158 if (Queue.empty()) break;
159 Record = Queue.back(); // not actually a queue.
160 Queue.pop_back();
161 }
162
163 return AllMatches;
164}
165
Jordan Rose2aa800a2012-08-08 18:23:20 +0000166bool CXXBasePaths::lookupInBases(ASTContext &Context,
Douglas Gregor89b77022010-03-03 02:18:00 +0000167 const CXXRecordDecl *Record,
168 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
169 void *UserData) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000170 bool FoundPath = false;
John McCall46460a62010-01-20 21:53:11 +0000171
John McCall92f88312010-01-23 00:46:32 +0000172 // The access of the path down to this record.
Douglas Gregor89b77022010-03-03 02:18:00 +0000173 AccessSpecifier AccessToHere = ScratchPath.Access;
174 bool IsFirstStep = ScratchPath.empty();
John McCall92f88312010-01-23 00:46:32 +0000175
Douglas Gregor89b77022010-03-03 02:18:00 +0000176 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(),
177 BaseSpecEnd = Record->bases_end();
178 BaseSpec != BaseSpecEnd;
179 ++BaseSpec) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000180 // Find the record of the base class subobjects for this type.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000181 QualType BaseType = Context.getCanonicalType(BaseSpec->getType())
182 .getUnqualifiedType();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000183
184 // C++ [temp.dep]p3:
185 // In the definition of a class template or a member of a class template,
186 // if a base class of the class template depends on a template-parameter,
187 // the base class scope is not examined during unqualified name lookup
188 // either at the point of definition of the class template or member or
189 // during an instantiation of the class tem- plate or member.
190 if (BaseType->isDependentType())
191 continue;
192
193 // Determine whether we need to visit this base class at all,
194 // updating the count of subobjects appropriately.
Douglas Gregor89b77022010-03-03 02:18:00 +0000195 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregora8f32e02009-10-06 17:59:45 +0000196 bool VisitBase = true;
197 bool SetVirtual = false;
198 if (BaseSpec->isVirtual()) {
199 VisitBase = !Subobjects.first;
200 Subobjects.first = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000201 if (isDetectingVirtual() && DetectedVirtual == 0) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000202 // If this is the first virtual we find, remember it. If it turns out
203 // there is no base path here, we'll reset it later.
Douglas Gregor89b77022010-03-03 02:18:00 +0000204 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000205 SetVirtual = true;
206 }
207 } else
208 ++Subobjects.second;
209
Douglas Gregor89b77022010-03-03 02:18:00 +0000210 if (isRecordingPaths()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000211 // Add this base specifier to the current path.
212 CXXBasePathElement Element;
213 Element.Base = &*BaseSpec;
Douglas Gregor89b77022010-03-03 02:18:00 +0000214 Element.Class = Record;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000215 if (BaseSpec->isVirtual())
216 Element.SubobjectNumber = 0;
217 else
218 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor89b77022010-03-03 02:18:00 +0000219 ScratchPath.push_back(Element);
John McCall46460a62010-01-20 21:53:11 +0000220
John McCall92f88312010-01-23 00:46:32 +0000221 // Calculate the "top-down" access to this base class.
222 // The spec actually describes this bottom-up, but top-down is
223 // equivalent because the definition works out as follows:
224 // 1. Write down the access along each step in the inheritance
225 // chain, followed by the access of the decl itself.
226 // For example, in
227 // class A { public: int foo; };
228 // class B : protected A {};
229 // class C : public B {};
230 // class D : private C {};
231 // we would write:
232 // private public protected public
233 // 2. If 'private' appears anywhere except far-left, access is denied.
234 // 3. Otherwise, overall access is determined by the most restrictive
235 // access in the sequence.
236 if (IsFirstStep)
Douglas Gregor89b77022010-03-03 02:18:00 +0000237 ScratchPath.Access = BaseSpec->getAccessSpecifier();
John McCall92f88312010-01-23 00:46:32 +0000238 else
Douglas Gregor89b77022010-03-03 02:18:00 +0000239 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
240 BaseSpec->getAccessSpecifier());
Douglas Gregora8f32e02009-10-06 17:59:45 +0000241 }
John McCalled814cc2010-02-09 00:57:12 +0000242
243 // Track whether there's a path involving this specific base.
244 bool FoundPathThroughBase = false;
245
Douglas Gregor89b77022010-03-03 02:18:00 +0000246 if (BaseMatches(BaseSpec, ScratchPath, UserData)) {
John McCall92f88312010-01-23 00:46:32 +0000247 // We've found a path that terminates at this base.
John McCalled814cc2010-02-09 00:57:12 +0000248 FoundPath = FoundPathThroughBase = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000249 if (isRecordingPaths()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000250 // We have a path. Make a copy of it before moving on.
Douglas Gregor89b77022010-03-03 02:18:00 +0000251 Paths.push_back(ScratchPath);
252 } else if (!isFindingAmbiguities()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000253 // We found a path and we don't care about ambiguities;
254 // return immediately.
255 return FoundPath;
256 }
257 } else if (VisitBase) {
258 CXXRecordDecl *BaseRecord
259 = cast<CXXRecordDecl>(BaseSpec->getType()->getAs<RecordType>()
260 ->getDecl());
Douglas Gregor89b77022010-03-03 02:18:00 +0000261 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000262 // C++ [class.member.lookup]p2:
263 // A member name f in one sub-object B hides a member name f in
264 // a sub-object A if A is a base class sub-object of B. Any
265 // declarations that are so hidden are eliminated from
266 // consideration.
267
268 // There is a path to a base class that meets the criteria. If we're
269 // not collecting paths or finding ambiguities, we're done.
John McCalled814cc2010-02-09 00:57:12 +0000270 FoundPath = FoundPathThroughBase = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000271 if (!isFindingAmbiguities())
Douglas Gregora8f32e02009-10-06 17:59:45 +0000272 return FoundPath;
273 }
274 }
275
276 // Pop this base specifier off the current path (if we're
277 // collecting paths).
Douglas Gregor89b77022010-03-03 02:18:00 +0000278 if (isRecordingPaths()) {
279 ScratchPath.pop_back();
John McCall46460a62010-01-20 21:53:11 +0000280 }
281
Douglas Gregora8f32e02009-10-06 17:59:45 +0000282 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCalled814cc2010-02-09 00:57:12 +0000283 if (SetVirtual && !FoundPathThroughBase) {
Douglas Gregor89b77022010-03-03 02:18:00 +0000284 DetectedVirtual = 0;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000285 }
286 }
John McCall92f88312010-01-23 00:46:32 +0000287
288 // Reset the scratch path access.
Douglas Gregor89b77022010-03-03 02:18:00 +0000289 ScratchPath.Access = AccessToHere;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000290
291 return FoundPath;
292}
293
Douglas Gregor89b77022010-03-03 02:18:00 +0000294bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches,
295 void *UserData,
296 CXXBasePaths &Paths) const {
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000297 // If we didn't find anything, report that.
298 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData))
299 return false;
300
301 // If we're not recording paths or we won't ever find ambiguities,
302 // we're done.
303 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
304 return true;
305
306 // C++ [class.member.lookup]p6:
307 // When virtual base classes are used, a hidden declaration can be
308 // reached along a path through the sub-object lattice that does
309 // not pass through the hiding declaration. This is not an
310 // ambiguity. The identical use with nonvirtual base classes is an
311 // ambiguity; in that case there is no unique instance of the name
312 // that hides all the others.
313 //
314 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
315 // way to make it any faster.
316 for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end();
317 P != PEnd; /* increment in loop */) {
318 bool Hidden = false;
319
320 for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end();
321 PE != PEEnd && !Hidden; ++PE) {
322 if (PE->Base->isVirtual()) {
323 CXXRecordDecl *VBase = 0;
324 if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>())
325 VBase = cast<CXXRecordDecl>(Record->getDecl());
326 if (!VBase)
327 break;
328
329 // The declaration(s) we found along this path were found in a
330 // subobject of a virtual base. Check whether this virtual
331 // base is a subobject of any other path; if so, then the
332 // declaration in this path are hidden by that patch.
333 for (CXXBasePaths::paths_iterator HidingP = Paths.begin(),
334 HidingPEnd = Paths.end();
335 HidingP != HidingPEnd;
336 ++HidingP) {
337 CXXRecordDecl *HidingClass = 0;
338 if (const RecordType *Record
339 = HidingP->back().Base->getType()->getAs<RecordType>())
340 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
341 if (!HidingClass)
342 break;
343
344 if (HidingClass->isVirtuallyDerivedFrom(VBase)) {
345 Hidden = true;
346 break;
347 }
348 }
349 }
350 }
351
352 if (Hidden)
353 P = Paths.Paths.erase(P);
354 else
355 ++P;
356 }
357
358 return true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000359}
360
John McCallaf8e6ed2009-11-12 03:15:40 +0000361bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000362 CXXBasePath &Path,
363 void *BaseRecord) {
364 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
365 "User data for FindBaseClass is not canonical!");
366 return Specifier->getType()->getAs<RecordType>()->getDecl()
367 ->getCanonicalDecl() == BaseRecord;
368}
369
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000370bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
371 CXXBasePath &Path,
372 void *BaseRecord) {
373 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
374 "User data for FindBaseClass is not canonical!");
375 return Specifier->isVirtual() &&
376 Specifier->getType()->getAs<RecordType>()->getDecl()
377 ->getCanonicalDecl() == BaseRecord;
378}
379
John McCallaf8e6ed2009-11-12 03:15:40 +0000380bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000381 CXXBasePath &Path,
382 void *Name) {
383 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
384
385 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
386 for (Path.Decls = BaseRecord->lookup(N);
387 Path.Decls.first != Path.Decls.second;
388 ++Path.Decls.first) {
389 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag))
390 return true;
391 }
392
393 return false;
394}
395
John McCallaf8e6ed2009-11-12 03:15:40 +0000396bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000397 CXXBasePath &Path,
398 void *Name) {
399 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
400
401 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
402 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
403 for (Path.Decls = BaseRecord->lookup(N);
404 Path.Decls.first != Path.Decls.second;
405 ++Path.Decls.first) {
406 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS))
407 return true;
408 }
409
410 return false;
411}
412
John McCallaf8e6ed2009-11-12 03:15:40 +0000413bool CXXRecordDecl::
414FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
415 CXXBasePath &Path,
416 void *Name) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000417 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
418
419 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
420 for (Path.Decls = BaseRecord->lookup(N);
421 Path.Decls.first != Path.Decls.second;
422 ++Path.Decls.first) {
423 // FIXME: Refactor the "is it a nested-name-specifier?" check
Richard Smith162e1c12011-04-15 14:24:37 +0000424 if (isa<TypedefNameDecl>(*Path.Decls.first) ||
Douglas Gregora8f32e02009-10-06 17:59:45 +0000425 (*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag))
426 return true;
427 }
428
429 return false;
Mike Stump82109bd2009-10-06 23:38:59 +0000430}
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000431
432void OverridingMethods::add(unsigned OverriddenSubobject,
433 UniqueVirtualMethod Overriding) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000434 SmallVector<UniqueVirtualMethod, 4> &SubobjectOverrides
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000435 = Overrides[OverriddenSubobject];
436 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
437 Overriding) == SubobjectOverrides.end())
438 SubobjectOverrides.push_back(Overriding);
439}
440
441void OverridingMethods::add(const OverridingMethods &Other) {
442 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
443 for (overriding_const_iterator M = I->second.begin(),
444 MEnd = I->second.end();
445 M != MEnd;
446 ++M)
447 add(I->first, *M);
448 }
449}
450
451void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
452 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
453 I->second.clear();
454 I->second.push_back(Overriding);
455 }
456}
457
458
459namespace {
460 class FinalOverriderCollector {
461 /// \brief The number of subobjects of a given class type that
462 /// occur within the class hierarchy.
463 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
464
465 /// \brief Overriders for each virtual base subobject.
466 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
467
468 CXXFinalOverriderMap FinalOverriders;
469
470 public:
471 ~FinalOverriderCollector();
472
473 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
474 const CXXRecordDecl *InVirtualSubobject,
475 CXXFinalOverriderMap &Overriders);
476 };
477}
478
479void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
480 bool VirtualBase,
481 const CXXRecordDecl *InVirtualSubobject,
482 CXXFinalOverriderMap &Overriders) {
483 unsigned SubobjectNumber = 0;
484 if (!VirtualBase)
485 SubobjectNumber
486 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
487
488 for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(),
489 BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) {
490 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
491 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
492 if (!BaseDecl->isPolymorphic())
493 continue;
494
495 if (Overriders.empty() && !Base->isVirtual()) {
496 // There are no other overriders of virtual member functions,
497 // so let the base class fill in our overriders for us.
498 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
499 continue;
500 }
501
502 // Collect all of the overridders from the base class subobject
503 // and merge them into the set of overridders for this class.
504 // For virtual base classes, populate or use the cached virtual
505 // overrides so that we do not walk the virtual base class (and
506 // its base classes) more than once.
507 CXXFinalOverriderMap ComputedBaseOverriders;
508 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
509 if (Base->isVirtual()) {
510 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramere0cf31d2012-05-27 22:41:08 +0000511 BaseOverriders = MyVirtualOverriders;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000512 if (!MyVirtualOverriders) {
513 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramere0cf31d2012-05-27 22:41:08 +0000514
515 // Collect may cause VirtualOverriders to reallocate, invalidating the
516 // MyVirtualOverriders reference. Set BaseOverriders to the right
517 // value now.
518 BaseOverriders = MyVirtualOverriders;
519
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000520 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
521 }
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000522 } else
523 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
524
525 // Merge the overriders from this base class into our own set of
526 // overriders.
527 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
528 OMEnd = BaseOverriders->end();
529 OM != OMEnd;
530 ++OM) {
531 const CXXMethodDecl *CanonOM
532 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl());
533 Overriders[CanonOM].add(OM->second);
534 }
535 }
536 }
537
538 for (CXXRecordDecl::method_iterator M = RD->method_begin(),
539 MEnd = RD->method_end();
540 M != MEnd;
541 ++M) {
542 // We only care about virtual methods.
543 if (!M->isVirtual())
544 continue;
545
546 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl());
547
548 if (CanonM->begin_overridden_methods()
549 == CanonM->end_overridden_methods()) {
550 // This is a new virtual function that does not override any
551 // other virtual function. Add it to the map of virtual
552 // functions for which we are tracking overridders.
553
554 // C++ [class.virtual]p2:
555 // For convenience we say that any virtual function overrides itself.
556 Overriders[CanonM].add(SubobjectNumber,
557 UniqueVirtualMethod(CanonM, SubobjectNumber,
558 InVirtualSubobject));
559 continue;
560 }
561
562 // This virtual method overrides other virtual methods, so it does
563 // not add any new slots into the set of overriders. Instead, we
564 // replace entries in the set of overriders with the new
565 // overrider. To do so, we dig down to the original virtual
566 // functions using data recursion and update all of the methods it
567 // overrides.
568 typedef std::pair<CXXMethodDecl::method_iterator,
569 CXXMethodDecl::method_iterator> OverriddenMethods;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000570 SmallVector<OverriddenMethods, 4> Stack;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000571 Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(),
572 CanonM->end_overridden_methods()));
573 while (!Stack.empty()) {
574 OverriddenMethods OverMethods = Stack.back();
575 Stack.pop_back();
576
577 for (; OverMethods.first != OverMethods.second; ++OverMethods.first) {
578 const CXXMethodDecl *CanonOM
579 = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl());
Anders Carlssonffdb2d22010-06-03 01:00:02 +0000580
581 // C++ [class.virtual]p2:
582 // A virtual member function C::vf of a class object S is
583 // a final overrider unless the most derived class (1.8)
584 // of which S is a base class subobject (if any) declares
585 // or inherits another member function that overrides vf.
586 //
587 // Treating this object like the most derived class, we
588 // replace any overrides from base classes with this
589 // overriding virtual function.
590 Overriders[CanonOM].replaceAll(
591 UniqueVirtualMethod(CanonM, SubobjectNumber,
592 InVirtualSubobject));
593
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000594 if (CanonOM->begin_overridden_methods()
Anders Carlssonffdb2d22010-06-03 01:00:02 +0000595 == CanonOM->end_overridden_methods())
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000596 continue;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000597
598 // Continue recursion to the methods that this virtual method
599 // overrides.
600 Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(),
601 CanonOM->end_overridden_methods()));
602 }
603 }
Anders Carlssonffdb2d22010-06-03 01:00:02 +0000604
605 // C++ [class.virtual]p2:
606 // For convenience we say that any virtual function overrides itself.
607 Overriders[CanonM].add(SubobjectNumber,
608 UniqueVirtualMethod(CanonM, SubobjectNumber,
609 InVirtualSubobject));
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000610 }
611}
612
613FinalOverriderCollector::~FinalOverriderCollector() {
614 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
615 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
616 VO != VOEnd;
617 ++VO)
618 delete VO->second;
619}
620
621void
622CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
623 FinalOverriderCollector Collector;
624 Collector.Collect(this, false, 0, FinalOverriders);
625
626 // Weed out any final overriders that come from virtual base class
627 // subobjects that were hidden by other subobjects along any path.
628 // This is the final-overrider variant of C++ [class.member.lookup]p10.
629 for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(),
630 OMEnd = FinalOverriders.end();
631 OM != OMEnd;
632 ++OM) {
633 for (OverridingMethods::iterator SO = OM->second.begin(),
634 SOEnd = OM->second.end();
635 SO != SOEnd;
636 ++SO) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000637 SmallVector<UniqueVirtualMethod, 4> &Overriding = SO->second;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000638 if (Overriding.size() < 2)
639 continue;
640
Chris Lattner5f9e2722011-07-23 10:55:15 +0000641 for (SmallVector<UniqueVirtualMethod, 4>::iterator
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000642 Pos = Overriding.begin(), PosEnd = Overriding.end();
643 Pos != PosEnd;
644 /* increment in loop */) {
645 if (!Pos->InVirtualSubobject) {
646 ++Pos;
647 continue;
648 }
649
650 // We have an overriding method in a virtual base class
651 // subobject (or non-virtual base class subobject thereof);
652 // determine whether there exists an other overriding method
653 // in a base class subobject that hides the virtual base class
654 // subobject.
655 bool Hidden = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000656 for (SmallVector<UniqueVirtualMethod, 4>::iterator
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000657 OP = Overriding.begin(), OPEnd = Overriding.end();
658 OP != OPEnd && !Hidden;
659 ++OP) {
660 if (Pos == OP)
661 continue;
662
663 if (OP->Method->getParent()->isVirtuallyDerivedFrom(
664 const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject)))
665 Hidden = true;
666 }
667
668 if (Hidden) {
669 // The current overriding function is hidden by another
670 // overriding function; remove this one.
671 Pos = Overriding.erase(Pos);
672 PosEnd = Overriding.end();
673 } else {
674 ++Pos;
675 }
676 }
677 }
678 }
679}
Anders Carlsson46170f92010-11-24 22:50:27 +0000680
681static void
682AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
683 CXXIndirectPrimaryBaseSet& Bases) {
684 // If the record has a virtual primary base class, add it to our set.
685 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000686 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson46170f92010-11-24 22:50:27 +0000687 Bases.insert(Layout.getPrimaryBase());
688
689 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
690 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson3a037652010-11-24 22:55:29 +0000691 assert(!I->getType()->isDependentType() &&
Anders Carlsson46170f92010-11-24 22:50:27 +0000692 "Cannot get indirect primary bases for class with dependent bases.");
693
694 const CXXRecordDecl *BaseDecl =
695 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
696
697 // Only bases with virtual bases participate in computing the
698 // indirect primary virtual base classes.
699 if (BaseDecl->getNumVBases())
700 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
701 }
702
703}
704
705void
706CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
707 ASTContext &Context = getASTContext();
708
709 if (!getNumVBases())
710 return;
711
712 for (CXXRecordDecl::base_class_const_iterator I = bases_begin(),
713 E = bases_end(); I != E; ++I) {
Anders Carlsson3a037652010-11-24 22:55:29 +0000714 assert(!I->getType()->isDependentType() &&
Anders Carlsson46170f92010-11-24 22:50:27 +0000715 "Cannot get indirect primary bases for class with dependent bases.");
716
717 const CXXRecordDecl *BaseDecl =
718 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
719
720 // Only bases with virtual bases participate in computing the
721 // indirect primary virtual base classes.
722 if (BaseDecl->getNumVBases())
723 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
724 }
725}
726