blob: 0e0b35d92adfb2d9af9ba1b0e5e8593721982944 [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"
Douglas Gregora8f32e02009-10-06 17:59:45 +000015#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/RecordLayout.h"
Douglas Gregora13cdf42012-09-11 07:19:42 +000017#include "llvm/ADT/SetVector.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000018#include <algorithm>
19#include <set>
20
21using namespace clang;
22
23/// \brief Computes the set of declarations referenced by these base
24/// paths.
25void CXXBasePaths::ComputeDeclsFound() {
26 assert(NumDeclsFound == 0 && !DeclsFound &&
27 "Already computed the set of declarations");
Benjamin Kramerd0e49e52012-02-23 15:18:31 +000028
Douglas Gregora13cdf42012-09-11 07:19:42 +000029 llvm::SetVector<NamedDecl *, SmallVector<NamedDecl *, 8> > Decls;
Benjamin Kramerd0e49e52012-02-23 15:18:31 +000030 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
David Blaikie3bc93e32012-12-19 00:45:41 +000031 Decls.insert(Path->Decls.front());
Benjamin Kramerd0e49e52012-02-23 15:18:31 +000032
Douglas Gregora8f32e02009-10-06 17:59:45 +000033 NumDeclsFound = Decls.size();
34 DeclsFound = new NamedDecl * [NumDeclsFound];
35 std::copy(Decls.begin(), Decls.end(), DeclsFound);
36}
37
38CXXBasePaths::decl_iterator CXXBasePaths::found_decls_begin() {
39 if (NumDeclsFound == 0)
40 ComputeDeclsFound();
41 return DeclsFound;
42}
43
44CXXBasePaths::decl_iterator CXXBasePaths::found_decls_end() {
45 if (NumDeclsFound == 0)
46 ComputeDeclsFound();
47 return DeclsFound + NumDeclsFound;
48}
49
50/// isAmbiguous - Determines whether the set of paths provided is
51/// ambiguous, i.e., there are two or more paths that refer to
52/// different base class subobjects of the same type. BaseType must be
53/// an unqualified, canonical class type.
Douglas Gregore0d5fe22010-05-21 20:29:55 +000054bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
55 BaseType = BaseType.getUnqualifiedType();
Douglas Gregora8f32e02009-10-06 17:59:45 +000056 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
57 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
58}
59
60/// clear - Clear out all prior path information.
61void CXXBasePaths::clear() {
62 Paths.clear();
63 ClassSubobjects.clear();
64 ScratchPath.clear();
65 DetectedVirtual = 0;
66}
67
68/// @brief Swaps the contents of this CXXBasePaths structure with the
69/// contents of Other.
70void CXXBasePaths::swap(CXXBasePaths &Other) {
71 std::swap(Origin, Other.Origin);
72 Paths.swap(Other.Paths);
73 ClassSubobjects.swap(Other.ClassSubobjects);
74 std::swap(FindAmbiguities, Other.FindAmbiguities);
75 std::swap(RecordPaths, Other.RecordPaths);
76 std::swap(DetectVirtual, Other.DetectVirtual);
77 std::swap(DetectedVirtual, Other.DetectedVirtual);
78}
79
John McCalld89d30f2011-01-28 22:02:36 +000080bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregora8f32e02009-10-06 17:59:45 +000081 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
82 /*DetectVirtual=*/false);
83 return isDerivedFrom(Base, Paths);
84}
85
John McCalld89d30f2011-01-28 22:02:36 +000086bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
87 CXXBasePaths &Paths) const {
Douglas Gregora8f32e02009-10-06 17:59:45 +000088 if (getCanonicalDecl() == Base->getCanonicalDecl())
89 return false;
90
John McCallaf8e6ed2009-11-12 03:15:40 +000091 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
John McCalld89d30f2011-01-28 22:02:36 +000092 return lookupInBases(&FindBaseClass,
93 const_cast<CXXRecordDecl*>(Base->getCanonicalDecl()),
94 Paths);
Douglas Gregora8f32e02009-10-06 17:59:45 +000095}
96
Jordan Rose2aa800a2012-08-08 18:23:20 +000097bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
Anders Carlsson1c4c3972010-06-04 01:40:08 +000098 if (!getNumVBases())
99 return false;
100
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000101 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
102 /*DetectVirtual=*/false);
103
104 if (getCanonicalDecl() == Base->getCanonicalDecl())
105 return false;
106
Jordan Rose2aa800a2012-08-08 18:23:20 +0000107 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
108
109 const void *BasePtr = static_cast<const void*>(Base->getCanonicalDecl());
110 return lookupInBases(&FindVirtualBaseClass,
111 const_cast<void *>(BasePtr),
112 Paths);
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000113}
114
John McCalle8174bc2009-12-08 07:42:38 +0000115static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) {
116 // OpaqueTarget is a CXXRecordDecl*.
117 return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget;
118}
119
120bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
David Greene3f7b3a72013-01-15 22:09:40 +0000121 return forallBases(BaseIsNot,
122 const_cast<CXXRecordDecl *>(Base->getCanonicalDecl()));
John McCalle8174bc2009-12-08 07:42:38 +0000123}
124
Richard Smithf62c6902012-11-22 00:24:47 +0000125bool
126CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const {
127 assert(isDependentContext());
128
129 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
130 if (CurContext->Equals(this))
131 return true;
132
133 return false;
134}
135
John McCalle8174bc2009-12-08 07:42:38 +0000136bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches,
137 void *OpaqueData,
Douglas Gregor229d47a2012-11-10 07:24:09 +0000138 bool AllowShortCircuit) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000139 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCalle8174bc2009-12-08 07:42:38 +0000140
141 const CXXRecordDecl *Record = this;
142 bool AllMatches = true;
143 while (true) {
144 for (CXXRecordDecl::base_class_const_iterator
145 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
Douglas Gregor229d47a2012-11-10 07:24:09 +0000146 const RecordType *Ty = I->getType()->getAs<RecordType>();
147 if (!Ty) {
John McCalle8174bc2009-12-08 07:42:38 +0000148 if (AllowShortCircuit) return false;
149 AllMatches = false;
150 continue;
151 }
152
Douglas Gregor229d47a2012-11-10 07:24:09 +0000153 CXXRecordDecl *Base =
154 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithf62c6902012-11-22 00:24:47 +0000155 if (!Base ||
156 (Base->isDependentContext() &&
157 !Base->isCurrentInstantiation(Record))) {
John McCalle8174bc2009-12-08 07:42:38 +0000158 if (AllowShortCircuit) return false;
159 AllMatches = false;
160 continue;
161 }
162
Anders Carlssonca910e82009-12-09 04:26:02 +0000163 Queue.push_back(Base);
164 if (!BaseMatches(Base, OpaqueData)) {
John McCalle8174bc2009-12-08 07:42:38 +0000165 if (AllowShortCircuit) return false;
166 AllMatches = false;
167 continue;
168 }
169 }
170
171 if (Queue.empty()) break;
172 Record = Queue.back(); // not actually a queue.
173 Queue.pop_back();
174 }
175
176 return AllMatches;
177}
178
Jordan Rose2aa800a2012-08-08 18:23:20 +0000179bool CXXBasePaths::lookupInBases(ASTContext &Context,
Douglas Gregor89b77022010-03-03 02:18:00 +0000180 const CXXRecordDecl *Record,
181 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
182 void *UserData) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000183 bool FoundPath = false;
John McCall46460a62010-01-20 21:53:11 +0000184
John McCall92f88312010-01-23 00:46:32 +0000185 // The access of the path down to this record.
Douglas Gregor89b77022010-03-03 02:18:00 +0000186 AccessSpecifier AccessToHere = ScratchPath.Access;
187 bool IsFirstStep = ScratchPath.empty();
John McCall92f88312010-01-23 00:46:32 +0000188
Douglas Gregor89b77022010-03-03 02:18:00 +0000189 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(),
190 BaseSpecEnd = Record->bases_end();
191 BaseSpec != BaseSpecEnd;
192 ++BaseSpec) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000193 // Find the record of the base class subobjects for this type.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000194 QualType BaseType = Context.getCanonicalType(BaseSpec->getType())
195 .getUnqualifiedType();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000196
197 // C++ [temp.dep]p3:
198 // In the definition of a class template or a member of a class template,
199 // if a base class of the class template depends on a template-parameter,
200 // the base class scope is not examined during unqualified name lookup
201 // either at the point of definition of the class template or member or
202 // during an instantiation of the class tem- plate or member.
203 if (BaseType->isDependentType())
204 continue;
205
206 // Determine whether we need to visit this base class at all,
207 // updating the count of subobjects appropriately.
Douglas Gregor89b77022010-03-03 02:18:00 +0000208 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregora8f32e02009-10-06 17:59:45 +0000209 bool VisitBase = true;
210 bool SetVirtual = false;
211 if (BaseSpec->isVirtual()) {
212 VisitBase = !Subobjects.first;
213 Subobjects.first = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000214 if (isDetectingVirtual() && DetectedVirtual == 0) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000215 // If this is the first virtual we find, remember it. If it turns out
216 // there is no base path here, we'll reset it later.
Douglas Gregor89b77022010-03-03 02:18:00 +0000217 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000218 SetVirtual = true;
219 }
220 } else
221 ++Subobjects.second;
222
Douglas Gregor89b77022010-03-03 02:18:00 +0000223 if (isRecordingPaths()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000224 // Add this base specifier to the current path.
225 CXXBasePathElement Element;
226 Element.Base = &*BaseSpec;
Douglas Gregor89b77022010-03-03 02:18:00 +0000227 Element.Class = Record;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000228 if (BaseSpec->isVirtual())
229 Element.SubobjectNumber = 0;
230 else
231 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor89b77022010-03-03 02:18:00 +0000232 ScratchPath.push_back(Element);
John McCall46460a62010-01-20 21:53:11 +0000233
John McCall92f88312010-01-23 00:46:32 +0000234 // Calculate the "top-down" access to this base class.
235 // The spec actually describes this bottom-up, but top-down is
236 // equivalent because the definition works out as follows:
237 // 1. Write down the access along each step in the inheritance
238 // chain, followed by the access of the decl itself.
239 // For example, in
240 // class A { public: int foo; };
241 // class B : protected A {};
242 // class C : public B {};
243 // class D : private C {};
244 // we would write:
245 // private public protected public
246 // 2. If 'private' appears anywhere except far-left, access is denied.
247 // 3. Otherwise, overall access is determined by the most restrictive
248 // access in the sequence.
249 if (IsFirstStep)
Douglas Gregor89b77022010-03-03 02:18:00 +0000250 ScratchPath.Access = BaseSpec->getAccessSpecifier();
John McCall92f88312010-01-23 00:46:32 +0000251 else
Douglas Gregor89b77022010-03-03 02:18:00 +0000252 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
253 BaseSpec->getAccessSpecifier());
Douglas Gregora8f32e02009-10-06 17:59:45 +0000254 }
John McCalled814cc2010-02-09 00:57:12 +0000255
256 // Track whether there's a path involving this specific base.
257 bool FoundPathThroughBase = false;
258
Douglas Gregor89b77022010-03-03 02:18:00 +0000259 if (BaseMatches(BaseSpec, ScratchPath, UserData)) {
John McCall92f88312010-01-23 00:46:32 +0000260 // We've found a path that terminates at this base.
John McCalled814cc2010-02-09 00:57:12 +0000261 FoundPath = FoundPathThroughBase = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000262 if (isRecordingPaths()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000263 // We have a path. Make a copy of it before moving on.
Douglas Gregor89b77022010-03-03 02:18:00 +0000264 Paths.push_back(ScratchPath);
265 } else if (!isFindingAmbiguities()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000266 // We found a path and we don't care about ambiguities;
267 // return immediately.
268 return FoundPath;
269 }
270 } else if (VisitBase) {
271 CXXRecordDecl *BaseRecord
Ted Kremenekcca17802012-09-12 06:50:29 +0000272 = cast<CXXRecordDecl>(BaseSpec->getType()->castAs<RecordType>()
Douglas Gregora8f32e02009-10-06 17:59:45 +0000273 ->getDecl());
Douglas Gregor89b77022010-03-03 02:18:00 +0000274 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000275 // C++ [class.member.lookup]p2:
276 // A member name f in one sub-object B hides a member name f in
277 // a sub-object A if A is a base class sub-object of B. Any
278 // declarations that are so hidden are eliminated from
279 // consideration.
280
281 // There is a path to a base class that meets the criteria. If we're
282 // not collecting paths or finding ambiguities, we're done.
John McCalled814cc2010-02-09 00:57:12 +0000283 FoundPath = FoundPathThroughBase = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000284 if (!isFindingAmbiguities())
Douglas Gregora8f32e02009-10-06 17:59:45 +0000285 return FoundPath;
286 }
287 }
288
289 // Pop this base specifier off the current path (if we're
290 // collecting paths).
Douglas Gregor89b77022010-03-03 02:18:00 +0000291 if (isRecordingPaths()) {
292 ScratchPath.pop_back();
John McCall46460a62010-01-20 21:53:11 +0000293 }
294
Douglas Gregora8f32e02009-10-06 17:59:45 +0000295 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCalled814cc2010-02-09 00:57:12 +0000296 if (SetVirtual && !FoundPathThroughBase) {
Douglas Gregor89b77022010-03-03 02:18:00 +0000297 DetectedVirtual = 0;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000298 }
299 }
John McCall92f88312010-01-23 00:46:32 +0000300
301 // Reset the scratch path access.
Douglas Gregor89b77022010-03-03 02:18:00 +0000302 ScratchPath.Access = AccessToHere;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000303
304 return FoundPath;
305}
306
Douglas Gregor89b77022010-03-03 02:18:00 +0000307bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches,
308 void *UserData,
309 CXXBasePaths &Paths) const {
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000310 // If we didn't find anything, report that.
311 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData))
312 return false;
313
314 // If we're not recording paths or we won't ever find ambiguities,
315 // we're done.
316 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
317 return true;
318
319 // C++ [class.member.lookup]p6:
320 // When virtual base classes are used, a hidden declaration can be
321 // reached along a path through the sub-object lattice that does
322 // not pass through the hiding declaration. This is not an
323 // ambiguity. The identical use with nonvirtual base classes is an
324 // ambiguity; in that case there is no unique instance of the name
325 // that hides all the others.
326 //
327 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
328 // way to make it any faster.
329 for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end();
330 P != PEnd; /* increment in loop */) {
331 bool Hidden = false;
332
333 for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end();
334 PE != PEEnd && !Hidden; ++PE) {
335 if (PE->Base->isVirtual()) {
336 CXXRecordDecl *VBase = 0;
337 if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>())
338 VBase = cast<CXXRecordDecl>(Record->getDecl());
339 if (!VBase)
340 break;
341
342 // The declaration(s) we found along this path were found in a
343 // subobject of a virtual base. Check whether this virtual
344 // base is a subobject of any other path; if so, then the
345 // declaration in this path are hidden by that patch.
346 for (CXXBasePaths::paths_iterator HidingP = Paths.begin(),
347 HidingPEnd = Paths.end();
348 HidingP != HidingPEnd;
349 ++HidingP) {
350 CXXRecordDecl *HidingClass = 0;
351 if (const RecordType *Record
352 = HidingP->back().Base->getType()->getAs<RecordType>())
353 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
354 if (!HidingClass)
355 break;
356
357 if (HidingClass->isVirtuallyDerivedFrom(VBase)) {
358 Hidden = true;
359 break;
360 }
361 }
362 }
363 }
364
365 if (Hidden)
366 P = Paths.Paths.erase(P);
367 else
368 ++P;
369 }
370
371 return true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000372}
373
John McCallaf8e6ed2009-11-12 03:15:40 +0000374bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000375 CXXBasePath &Path,
376 void *BaseRecord) {
377 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
378 "User data for FindBaseClass is not canonical!");
Ted Kremenek890f0f12012-08-23 20:46:57 +0000379 return Specifier->getType()->castAs<RecordType>()->getDecl()
380 ->getCanonicalDecl() == BaseRecord;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000381}
382
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000383bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
384 CXXBasePath &Path,
385 void *BaseRecord) {
386 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
387 "User data for FindBaseClass is not canonical!");
388 return Specifier->isVirtual() &&
Ted Kremenek890f0f12012-08-23 20:46:57 +0000389 Specifier->getType()->castAs<RecordType>()->getDecl()
390 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000391}
392
John McCallaf8e6ed2009-11-12 03:15:40 +0000393bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000394 CXXBasePath &Path,
395 void *Name) {
Ted Kremenek890f0f12012-08-23 20:46:57 +0000396 RecordDecl *BaseRecord =
397 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000398
399 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
400 for (Path.Decls = BaseRecord->lookup(N);
David Blaikie3bc93e32012-12-19 00:45:41 +0000401 !Path.Decls.empty();
402 Path.Decls = Path.Decls.slice(1)) {
403 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregora8f32e02009-10-06 17:59:45 +0000404 return true;
405 }
406
407 return false;
408}
409
John McCallaf8e6ed2009-11-12 03:15:40 +0000410bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000411 CXXBasePath &Path,
412 void *Name) {
Ted Kremenek890f0f12012-08-23 20:46:57 +0000413 RecordDecl *BaseRecord =
414 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000415
416 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
417 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
418 for (Path.Decls = BaseRecord->lookup(N);
David Blaikie3bc93e32012-12-19 00:45:41 +0000419 !Path.Decls.empty();
420 Path.Decls = Path.Decls.slice(1)) {
421 if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
Douglas Gregora8f32e02009-10-06 17:59:45 +0000422 return true;
423 }
424
425 return false;
426}
427
John McCallaf8e6ed2009-11-12 03:15:40 +0000428bool CXXRecordDecl::
429FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
430 CXXBasePath &Path,
431 void *Name) {
Ted Kremenek890f0f12012-08-23 20:46:57 +0000432 RecordDecl *BaseRecord =
433 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000434
435 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
436 for (Path.Decls = BaseRecord->lookup(N);
David Blaikie3bc93e32012-12-19 00:45:41 +0000437 !Path.Decls.empty();
438 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000439 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikie3bc93e32012-12-19 00:45:41 +0000440 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
441 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregora8f32e02009-10-06 17:59:45 +0000442 return true;
443 }
444
445 return false;
Mike Stump82109bd2009-10-06 23:38:59 +0000446}
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000447
448void OverridingMethods::add(unsigned OverriddenSubobject,
449 UniqueVirtualMethod Overriding) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000450 SmallVector<UniqueVirtualMethod, 4> &SubobjectOverrides
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000451 = Overrides[OverriddenSubobject];
452 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
453 Overriding) == SubobjectOverrides.end())
454 SubobjectOverrides.push_back(Overriding);
455}
456
457void OverridingMethods::add(const OverridingMethods &Other) {
458 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
459 for (overriding_const_iterator M = I->second.begin(),
460 MEnd = I->second.end();
461 M != MEnd;
462 ++M)
463 add(I->first, *M);
464 }
465}
466
467void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
468 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
469 I->second.clear();
470 I->second.push_back(Overriding);
471 }
472}
473
474
475namespace {
476 class FinalOverriderCollector {
477 /// \brief The number of subobjects of a given class type that
478 /// occur within the class hierarchy.
479 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
480
481 /// \brief Overriders for each virtual base subobject.
482 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
483
484 CXXFinalOverriderMap FinalOverriders;
485
486 public:
487 ~FinalOverriderCollector();
488
489 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
490 const CXXRecordDecl *InVirtualSubobject,
491 CXXFinalOverriderMap &Overriders);
492 };
493}
494
495void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
496 bool VirtualBase,
497 const CXXRecordDecl *InVirtualSubobject,
498 CXXFinalOverriderMap &Overriders) {
499 unsigned SubobjectNumber = 0;
500 if (!VirtualBase)
501 SubobjectNumber
502 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
503
504 for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(),
505 BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) {
506 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) {
507 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
508 if (!BaseDecl->isPolymorphic())
509 continue;
510
511 if (Overriders.empty() && !Base->isVirtual()) {
512 // There are no other overriders of virtual member functions,
513 // so let the base class fill in our overriders for us.
514 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
515 continue;
516 }
517
518 // Collect all of the overridders from the base class subobject
519 // and merge them into the set of overridders for this class.
520 // For virtual base classes, populate or use the cached virtual
521 // overrides so that we do not walk the virtual base class (and
522 // its base classes) more than once.
523 CXXFinalOverriderMap ComputedBaseOverriders;
524 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
525 if (Base->isVirtual()) {
526 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramere0cf31d2012-05-27 22:41:08 +0000527 BaseOverriders = MyVirtualOverriders;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000528 if (!MyVirtualOverriders) {
529 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramere0cf31d2012-05-27 22:41:08 +0000530
531 // Collect may cause VirtualOverriders to reallocate, invalidating the
532 // MyVirtualOverriders reference. Set BaseOverriders to the right
533 // value now.
534 BaseOverriders = MyVirtualOverriders;
535
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000536 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
537 }
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000538 } else
539 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
540
541 // Merge the overriders from this base class into our own set of
542 // overriders.
543 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
544 OMEnd = BaseOverriders->end();
545 OM != OMEnd;
546 ++OM) {
547 const CXXMethodDecl *CanonOM
548 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl());
549 Overriders[CanonOM].add(OM->second);
550 }
551 }
552 }
553
554 for (CXXRecordDecl::method_iterator M = RD->method_begin(),
555 MEnd = RD->method_end();
556 M != MEnd;
557 ++M) {
558 // We only care about virtual methods.
559 if (!M->isVirtual())
560 continue;
561
562 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl());
563
564 if (CanonM->begin_overridden_methods()
565 == CanonM->end_overridden_methods()) {
566 // This is a new virtual function that does not override any
567 // other virtual function. Add it to the map of virtual
568 // functions for which we are tracking overridders.
569
570 // C++ [class.virtual]p2:
571 // For convenience we say that any virtual function overrides itself.
572 Overriders[CanonM].add(SubobjectNumber,
573 UniqueVirtualMethod(CanonM, SubobjectNumber,
574 InVirtualSubobject));
575 continue;
576 }
577
578 // This virtual method overrides other virtual methods, so it does
579 // not add any new slots into the set of overriders. Instead, we
580 // replace entries in the set of overriders with the new
581 // overrider. To do so, we dig down to the original virtual
582 // functions using data recursion and update all of the methods it
583 // overrides.
584 typedef std::pair<CXXMethodDecl::method_iterator,
585 CXXMethodDecl::method_iterator> OverriddenMethods;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000586 SmallVector<OverriddenMethods, 4> Stack;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000587 Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(),
588 CanonM->end_overridden_methods()));
589 while (!Stack.empty()) {
590 OverriddenMethods OverMethods = Stack.back();
591 Stack.pop_back();
592
593 for (; OverMethods.first != OverMethods.second; ++OverMethods.first) {
594 const CXXMethodDecl *CanonOM
595 = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl());
Anders Carlssonffdb2d22010-06-03 01:00:02 +0000596
597 // C++ [class.virtual]p2:
598 // A virtual member function C::vf of a class object S is
599 // a final overrider unless the most derived class (1.8)
600 // of which S is a base class subobject (if any) declares
601 // or inherits another member function that overrides vf.
602 //
603 // Treating this object like the most derived class, we
604 // replace any overrides from base classes with this
605 // overriding virtual function.
606 Overriders[CanonOM].replaceAll(
607 UniqueVirtualMethod(CanonM, SubobjectNumber,
608 InVirtualSubobject));
609
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000610 if (CanonOM->begin_overridden_methods()
Anders Carlssonffdb2d22010-06-03 01:00:02 +0000611 == CanonOM->end_overridden_methods())
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000612 continue;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000613
614 // Continue recursion to the methods that this virtual method
615 // overrides.
616 Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(),
617 CanonOM->end_overridden_methods()));
618 }
619 }
Anders Carlssonffdb2d22010-06-03 01:00:02 +0000620
621 // C++ [class.virtual]p2:
622 // For convenience we say that any virtual function overrides itself.
623 Overriders[CanonM].add(SubobjectNumber,
624 UniqueVirtualMethod(CanonM, SubobjectNumber,
625 InVirtualSubobject));
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000626 }
627}
628
629FinalOverriderCollector::~FinalOverriderCollector() {
630 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
631 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
632 VO != VOEnd;
633 ++VO)
634 delete VO->second;
635}
636
637void
638CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
639 FinalOverriderCollector Collector;
640 Collector.Collect(this, false, 0, FinalOverriders);
641
642 // Weed out any final overriders that come from virtual base class
643 // subobjects that were hidden by other subobjects along any path.
644 // This is the final-overrider variant of C++ [class.member.lookup]p10.
645 for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(),
646 OMEnd = FinalOverriders.end();
647 OM != OMEnd;
648 ++OM) {
649 for (OverridingMethods::iterator SO = OM->second.begin(),
650 SOEnd = OM->second.end();
651 SO != SOEnd;
652 ++SO) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000653 SmallVector<UniqueVirtualMethod, 4> &Overriding = SO->second;
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000654 if (Overriding.size() < 2)
655 continue;
656
Chris Lattner5f9e2722011-07-23 10:55:15 +0000657 for (SmallVector<UniqueVirtualMethod, 4>::iterator
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000658 Pos = Overriding.begin(), PosEnd = Overriding.end();
659 Pos != PosEnd;
660 /* increment in loop */) {
661 if (!Pos->InVirtualSubobject) {
662 ++Pos;
663 continue;
664 }
665
666 // We have an overriding method in a virtual base class
667 // subobject (or non-virtual base class subobject thereof);
668 // determine whether there exists an other overriding method
669 // in a base class subobject that hides the virtual base class
670 // subobject.
671 bool Hidden = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000672 for (SmallVector<UniqueVirtualMethod, 4>::iterator
Douglas Gregor7b2fc9d2010-03-23 23:47:56 +0000673 OP = Overriding.begin(), OPEnd = Overriding.end();
674 OP != OPEnd && !Hidden;
675 ++OP) {
676 if (Pos == OP)
677 continue;
678
679 if (OP->Method->getParent()->isVirtuallyDerivedFrom(
680 const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject)))
681 Hidden = true;
682 }
683
684 if (Hidden) {
685 // The current overriding function is hidden by another
686 // overriding function; remove this one.
687 Pos = Overriding.erase(Pos);
688 PosEnd = Overriding.end();
689 } else {
690 ++Pos;
691 }
692 }
693 }
694 }
695}
Anders Carlsson46170f92010-11-24 22:50:27 +0000696
697static void
698AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
699 CXXIndirectPrimaryBaseSet& Bases) {
700 // If the record has a virtual primary base class, add it to our set.
701 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000702 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson46170f92010-11-24 22:50:27 +0000703 Bases.insert(Layout.getPrimaryBase());
704
705 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
706 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson3a037652010-11-24 22:55:29 +0000707 assert(!I->getType()->isDependentType() &&
Anders Carlsson46170f92010-11-24 22:50:27 +0000708 "Cannot get indirect primary bases for class with dependent bases.");
709
710 const CXXRecordDecl *BaseDecl =
Ted Kremenek890f0f12012-08-23 20:46:57 +0000711 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlsson46170f92010-11-24 22:50:27 +0000712
713 // Only bases with virtual bases participate in computing the
714 // indirect primary virtual base classes.
715 if (BaseDecl->getNumVBases())
716 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
717 }
718
719}
720
721void
722CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
723 ASTContext &Context = getASTContext();
724
725 if (!getNumVBases())
726 return;
727
728 for (CXXRecordDecl::base_class_const_iterator I = bases_begin(),
729 E = bases_end(); I != E; ++I) {
Anders Carlsson3a037652010-11-24 22:55:29 +0000730 assert(!I->getType()->isDependentType() &&
Anders Carlsson46170f92010-11-24 22:50:27 +0000731 "Cannot get indirect primary bases for class with dependent bases.");
732
733 const CXXRecordDecl *BaseDecl =
Ted Kremenekcca17802012-09-12 06:50:29 +0000734 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlsson46170f92010-11-24 22:50:27 +0000735
736 // Only bases with virtual bases participate in computing the
737 // indirect primary virtual base classes.
738 if (BaseDecl->getNumVBases())
739 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
740 }
741}