blob: 0f277b134acd1034c7a046ae9bc9c05e4d4c3678 [file] [log] [blame]
Douglas Gregor36d1b142009-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 Kramer2ef30312012-07-04 18:45:14 +000014#include "clang/AST/ASTContext.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000015#include "clang/AST/DeclCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/RecordLayout.h"
Douglas Gregor18e1b522012-09-11 07:19:42 +000017#include "llvm/ADT/SetVector.h"
Douglas Gregor36d1b142009-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 Kramer91c6b6a2012-02-23 15:18:31 +000028
Douglas Gregor18e1b522012-09-11 07:19:42 +000029 llvm::SetVector<NamedDecl *, SmallVector<NamedDecl *, 8> > Decls;
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000030 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
David Blaikieff7d47a2012-12-19 00:45:41 +000031 Decls.insert(Path->Decls.front());
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000032
Douglas Gregor36d1b142009-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 Gregor27ac4292010-05-21 20:29:55 +000054bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
55 BaseType = BaseType.getUnqualifiedType();
Douglas Gregor36d1b142009-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 McCall388ef532011-01-28 22:02:36 +000080bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000081 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
82 /*DetectVirtual=*/false);
83 return isDerivedFrom(Base, Paths);
84}
85
John McCall388ef532011-01-28 22:02:36 +000086bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
87 CXXBasePaths &Paths) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000088 if (getCanonicalDecl() == Base->getCanonicalDecl())
89 return false;
90
John McCall84c16cf2009-11-12 03:15:40 +000091 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
John McCall388ef532011-01-28 22:02:36 +000092 return lookupInBases(&FindBaseClass,
93 const_cast<CXXRecordDecl*>(Base->getCanonicalDecl()),
94 Paths);
Douglas Gregor36d1b142009-10-06 17:59:45 +000095}
96
Jordan Rose55edf5ff2012-08-08 18:23:20 +000097bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
Anders Carlsson15722da2010-06-04 01:40:08 +000098 if (!getNumVBases())
99 return false;
100
Douglas Gregor3e637462010-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 Rose55edf5ff2012-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 Gregor3e637462010-03-03 04:38:46 +0000113}
114
John McCallddabf1a2009-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 Greene94319262013-01-15 22:09:40 +0000121 return forallBases(BaseIsNot,
122 const_cast<CXXRecordDecl *>(Base->getCanonicalDecl()));
John McCallddabf1a2009-12-08 07:42:38 +0000123}
124
Richard Smithd80b2d52012-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 McCallddabf1a2009-12-08 07:42:38 +0000136bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches,
137 void *OpaqueData,
Douglas Gregordc974572012-11-10 07:24:09 +0000138 bool AllowShortCircuit) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000139 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCallddabf1a2009-12-08 07:42:38 +0000140
141 const CXXRecordDecl *Record = this;
142 bool AllMatches = true;
143 while (true) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000144 for (const auto &I : Record->bases()) {
145 const RecordType *Ty = I.getType()->getAs<RecordType>();
Douglas Gregordc974572012-11-10 07:24:09 +0000146 if (!Ty) {
John McCallddabf1a2009-12-08 07:42:38 +0000147 if (AllowShortCircuit) return false;
148 AllMatches = false;
149 continue;
150 }
151
Douglas Gregordc974572012-11-10 07:24:09 +0000152 CXXRecordDecl *Base =
153 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithd80b2d52012-11-22 00:24:47 +0000154 if (!Base ||
155 (Base->isDependentContext() &&
156 !Base->isCurrentInstantiation(Record))) {
John McCallddabf1a2009-12-08 07:42:38 +0000157 if (AllowShortCircuit) return false;
158 AllMatches = false;
159 continue;
160 }
161
Anders Carlssonf9812782009-12-09 04:26:02 +0000162 Queue.push_back(Base);
163 if (!BaseMatches(Base, OpaqueData)) {
John McCallddabf1a2009-12-08 07:42:38 +0000164 if (AllowShortCircuit) return false;
165 AllMatches = false;
166 continue;
167 }
168 }
169
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000170 if (Queue.empty())
171 break;
172 Record = Queue.pop_back_val(); // not actually a queue.
John McCallddabf1a2009-12-08 07:42:38 +0000173 }
174
175 return AllMatches;
176}
177
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000178bool CXXBasePaths::lookupInBases(ASTContext &Context,
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000179 const CXXRecordDecl *Record,
180 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
181 void *UserData) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000182 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000183
John McCall553c0792010-01-23 00:46:32 +0000184 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000185 AccessSpecifier AccessToHere = ScratchPath.Access;
186 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000187
Aaron Ballman574705e2014-03-13 15:41:46 +0000188 for (const auto &BaseSpec : Record->bases()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000189 // Find the record of the base class subobjects for this type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000190 QualType BaseType =
191 Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
192
Douglas Gregor36d1b142009-10-06 17:59:45 +0000193 // C++ [temp.dep]p3:
194 // In the definition of a class template or a member of a class template,
195 // if a base class of the class template depends on a template-parameter,
196 // the base class scope is not examined during unqualified name lookup
197 // either at the point of definition of the class template or member or
198 // during an instantiation of the class tem- plate or member.
199 if (BaseType->isDependentType())
200 continue;
201
202 // Determine whether we need to visit this base class at all,
203 // updating the count of subobjects appropriately.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000204 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000205 bool VisitBase = true;
206 bool SetVirtual = false;
Aaron Ballman574705e2014-03-13 15:41:46 +0000207 if (BaseSpec.isVirtual()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000208 VisitBase = !Subobjects.first;
209 Subobjects.first = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000210 if (isDetectingVirtual() && DetectedVirtual == 0) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000211 // If this is the first virtual we find, remember it. If it turns out
212 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000213 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000214 SetVirtual = true;
215 }
216 } else
217 ++Subobjects.second;
218
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000219 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000220 // Add this base specifier to the current path.
221 CXXBasePathElement Element;
Aaron Ballman574705e2014-03-13 15:41:46 +0000222 Element.Base = &BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000223 Element.Class = Record;
Aaron Ballman574705e2014-03-13 15:41:46 +0000224 if (BaseSpec.isVirtual())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000225 Element.SubobjectNumber = 0;
226 else
227 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000228 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000229
John McCall553c0792010-01-23 00:46:32 +0000230 // Calculate the "top-down" access to this base class.
231 // The spec actually describes this bottom-up, but top-down is
232 // equivalent because the definition works out as follows:
233 // 1. Write down the access along each step in the inheritance
234 // chain, followed by the access of the decl itself.
235 // For example, in
236 // class A { public: int foo; };
237 // class B : protected A {};
238 // class C : public B {};
239 // class D : private C {};
240 // we would write:
241 // private public protected public
242 // 2. If 'private' appears anywhere except far-left, access is denied.
243 // 3. Otherwise, overall access is determined by the most restrictive
244 // access in the sequence.
245 if (IsFirstStep)
Aaron Ballman574705e2014-03-13 15:41:46 +0000246 ScratchPath.Access = BaseSpec.getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000247 else
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000248 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
Aaron Ballman574705e2014-03-13 15:41:46 +0000249 BaseSpec.getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000250 }
John McCall6f891402010-02-09 00:57:12 +0000251
252 // Track whether there's a path involving this specific base.
253 bool FoundPathThroughBase = false;
254
Aaron Ballman574705e2014-03-13 15:41:46 +0000255 if (BaseMatches(&BaseSpec, ScratchPath, UserData)) {
John McCall553c0792010-01-23 00:46:32 +0000256 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000257 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000258 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000259 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000260 Paths.push_back(ScratchPath);
261 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000262 // We found a path and we don't care about ambiguities;
263 // return immediately.
264 return FoundPath;
265 }
266 } else if (VisitBase) {
267 CXXRecordDecl *BaseRecord
Aaron Ballman574705e2014-03-13 15:41:46 +0000268 = cast<CXXRecordDecl>(BaseSpec.getType()->castAs<RecordType>()
Douglas Gregor36d1b142009-10-06 17:59:45 +0000269 ->getDecl());
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000270 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000271 // C++ [class.member.lookup]p2:
272 // A member name f in one sub-object B hides a member name f in
273 // a sub-object A if A is a base class sub-object of B. Any
274 // declarations that are so hidden are eliminated from
275 // consideration.
276
277 // There is a path to a base class that meets the criteria. If we're
278 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000279 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000280 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000281 return FoundPath;
282 }
283 }
284
285 // Pop this base specifier off the current path (if we're
286 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000287 if (isRecordingPaths()) {
288 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000289 }
290
Douglas Gregor36d1b142009-10-06 17:59:45 +0000291 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000292 if (SetVirtual && !FoundPathThroughBase) {
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000293 DetectedVirtual = 0;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000294 }
295 }
John McCall553c0792010-01-23 00:46:32 +0000296
297 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000298 ScratchPath.Access = AccessToHere;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000299
300 return FoundPath;
301}
302
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000303bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches,
304 void *UserData,
305 CXXBasePaths &Paths) const {
Douglas Gregor3e637462010-03-03 04:38:46 +0000306 // If we didn't find anything, report that.
307 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData))
308 return false;
309
310 // If we're not recording paths or we won't ever find ambiguities,
311 // we're done.
312 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
313 return true;
314
315 // C++ [class.member.lookup]p6:
316 // When virtual base classes are used, a hidden declaration can be
317 // reached along a path through the sub-object lattice that does
318 // not pass through the hiding declaration. This is not an
319 // ambiguity. The identical use with nonvirtual base classes is an
320 // ambiguity; in that case there is no unique instance of the name
321 // that hides all the others.
322 //
323 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
324 // way to make it any faster.
325 for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end();
326 P != PEnd; /* increment in loop */) {
327 bool Hidden = false;
328
329 for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end();
330 PE != PEEnd && !Hidden; ++PE) {
331 if (PE->Base->isVirtual()) {
332 CXXRecordDecl *VBase = 0;
333 if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>())
334 VBase = cast<CXXRecordDecl>(Record->getDecl());
335 if (!VBase)
336 break;
337
338 // The declaration(s) we found along this path were found in a
339 // subobject of a virtual base. Check whether this virtual
340 // base is a subobject of any other path; if so, then the
341 // declaration in this path are hidden by that patch.
342 for (CXXBasePaths::paths_iterator HidingP = Paths.begin(),
343 HidingPEnd = Paths.end();
344 HidingP != HidingPEnd;
345 ++HidingP) {
346 CXXRecordDecl *HidingClass = 0;
347 if (const RecordType *Record
348 = HidingP->back().Base->getType()->getAs<RecordType>())
349 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
350 if (!HidingClass)
351 break;
352
353 if (HidingClass->isVirtuallyDerivedFrom(VBase)) {
354 Hidden = true;
355 break;
356 }
357 }
358 }
359 }
360
361 if (Hidden)
362 P = Paths.Paths.erase(P);
363 else
364 ++P;
365 }
366
367 return true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000368}
369
John McCall84c16cf2009-11-12 03:15:40 +0000370bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000371 CXXBasePath &Path,
372 void *BaseRecord) {
373 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
374 "User data for FindBaseClass is not canonical!");
Ted Kremenek28831752012-08-23 20:46:57 +0000375 return Specifier->getType()->castAs<RecordType>()->getDecl()
376 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000377}
378
Douglas Gregor3e637462010-03-03 04:38:46 +0000379bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
380 CXXBasePath &Path,
381 void *BaseRecord) {
382 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
383 "User data for FindBaseClass is not canonical!");
384 return Specifier->isVirtual() &&
Ted Kremenek28831752012-08-23 20:46:57 +0000385 Specifier->getType()->castAs<RecordType>()->getDecl()
386 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor3e637462010-03-03 04:38:46 +0000387}
388
John McCall84c16cf2009-11-12 03:15:40 +0000389bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000390 CXXBasePath &Path,
391 void *Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000392 RecordDecl *BaseRecord =
393 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000394
395 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
396 for (Path.Decls = BaseRecord->lookup(N);
David Blaikieff7d47a2012-12-19 00:45:41 +0000397 !Path.Decls.empty();
398 Path.Decls = Path.Decls.slice(1)) {
399 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000400 return true;
401 }
402
403 return false;
404}
405
John McCall84c16cf2009-11-12 03:15:40 +0000406bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000407 CXXBasePath &Path,
408 void *Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000409 RecordDecl *BaseRecord =
410 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000411
412 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
413 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
414 for (Path.Decls = BaseRecord->lookup(N);
David Blaikieff7d47a2012-12-19 00:45:41 +0000415 !Path.Decls.empty();
416 Path.Decls = Path.Decls.slice(1)) {
417 if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000418 return true;
419 }
420
421 return false;
422}
423
John McCall84c16cf2009-11-12 03:15:40 +0000424bool CXXRecordDecl::
425FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
426 CXXBasePath &Path,
427 void *Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000428 RecordDecl *BaseRecord =
429 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000430
431 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
432 for (Path.Decls = BaseRecord->lookup(N);
David Blaikieff7d47a2012-12-19 00:45:41 +0000433 !Path.Decls.empty();
434 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000435 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikieff7d47a2012-12-19 00:45:41 +0000436 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
437 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000438 return true;
439 }
440
441 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000442}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000443
444void OverridingMethods::add(unsigned OverriddenSubobject,
445 UniqueVirtualMethod Overriding) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000446 SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
Douglas Gregor4165bd62010-03-23 23:47:56 +0000447 = Overrides[OverriddenSubobject];
448 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
449 Overriding) == SubobjectOverrides.end())
450 SubobjectOverrides.push_back(Overriding);
451}
452
453void OverridingMethods::add(const OverridingMethods &Other) {
454 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
455 for (overriding_const_iterator M = I->second.begin(),
456 MEnd = I->second.end();
457 M != MEnd;
458 ++M)
459 add(I->first, *M);
460 }
461}
462
463void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
464 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
465 I->second.clear();
466 I->second.push_back(Overriding);
467 }
468}
469
470
471namespace {
472 class FinalOverriderCollector {
473 /// \brief The number of subobjects of a given class type that
474 /// occur within the class hierarchy.
475 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
476
477 /// \brief Overriders for each virtual base subobject.
478 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
479
480 CXXFinalOverriderMap FinalOverriders;
481
482 public:
483 ~FinalOverriderCollector();
484
485 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
486 const CXXRecordDecl *InVirtualSubobject,
487 CXXFinalOverriderMap &Overriders);
488 };
489}
490
491void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
492 bool VirtualBase,
493 const CXXRecordDecl *InVirtualSubobject,
494 CXXFinalOverriderMap &Overriders) {
495 unsigned SubobjectNumber = 0;
496 if (!VirtualBase)
497 SubobjectNumber
498 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
499
Aaron Ballman574705e2014-03-13 15:41:46 +0000500 for (const auto &Base : RD->bases()) {
501 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000502 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
503 if (!BaseDecl->isPolymorphic())
504 continue;
505
Aaron Ballman574705e2014-03-13 15:41:46 +0000506 if (Overriders.empty() && !Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000507 // There are no other overriders of virtual member functions,
508 // so let the base class fill in our overriders for us.
509 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
510 continue;
511 }
512
513 // Collect all of the overridders from the base class subobject
514 // and merge them into the set of overridders for this class.
515 // For virtual base classes, populate or use the cached virtual
516 // overrides so that we do not walk the virtual base class (and
517 // its base classes) more than once.
518 CXXFinalOverriderMap ComputedBaseOverriders;
519 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
Aaron Ballman574705e2014-03-13 15:41:46 +0000520 if (Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000521 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramerea388a22012-05-27 22:41:08 +0000522 BaseOverriders = MyVirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000523 if (!MyVirtualOverriders) {
524 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramerea388a22012-05-27 22:41:08 +0000525
526 // Collect may cause VirtualOverriders to reallocate, invalidating the
527 // MyVirtualOverriders reference. Set BaseOverriders to the right
528 // value now.
529 BaseOverriders = MyVirtualOverriders;
530
Douglas Gregor4165bd62010-03-23 23:47:56 +0000531 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
532 }
Douglas Gregor4165bd62010-03-23 23:47:56 +0000533 } else
534 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
535
536 // Merge the overriders from this base class into our own set of
537 // overriders.
538 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
539 OMEnd = BaseOverriders->end();
540 OM != OMEnd;
541 ++OM) {
542 const CXXMethodDecl *CanonOM
543 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl());
544 Overriders[CanonOM].add(OM->second);
545 }
546 }
547 }
548
549 for (CXXRecordDecl::method_iterator M = RD->method_begin(),
550 MEnd = RD->method_end();
551 M != MEnd;
552 ++M) {
553 // We only care about virtual methods.
554 if (!M->isVirtual())
555 continue;
556
557 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl());
558
559 if (CanonM->begin_overridden_methods()
560 == CanonM->end_overridden_methods()) {
561 // This is a new virtual function that does not override any
562 // other virtual function. Add it to the map of virtual
563 // functions for which we are tracking overridders.
564
565 // C++ [class.virtual]p2:
566 // For convenience we say that any virtual function overrides itself.
567 Overriders[CanonM].add(SubobjectNumber,
568 UniqueVirtualMethod(CanonM, SubobjectNumber,
569 InVirtualSubobject));
570 continue;
571 }
572
573 // This virtual method overrides other virtual methods, so it does
574 // not add any new slots into the set of overriders. Instead, we
575 // replace entries in the set of overriders with the new
576 // overrider. To do so, we dig down to the original virtual
577 // functions using data recursion and update all of the methods it
578 // overrides.
579 typedef std::pair<CXXMethodDecl::method_iterator,
580 CXXMethodDecl::method_iterator> OverriddenMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000581 SmallVector<OverriddenMethods, 4> Stack;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000582 Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(),
583 CanonM->end_overridden_methods()));
584 while (!Stack.empty()) {
585 OverriddenMethods OverMethods = Stack.back();
586 Stack.pop_back();
587
588 for (; OverMethods.first != OverMethods.second; ++OverMethods.first) {
589 const CXXMethodDecl *CanonOM
590 = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl());
Anders Carlssona2f74f32010-06-03 01:00:02 +0000591
592 // C++ [class.virtual]p2:
593 // A virtual member function C::vf of a class object S is
594 // a final overrider unless the most derived class (1.8)
595 // of which S is a base class subobject (if any) declares
596 // or inherits another member function that overrides vf.
597 //
598 // Treating this object like the most derived class, we
599 // replace any overrides from base classes with this
600 // overriding virtual function.
601 Overriders[CanonOM].replaceAll(
602 UniqueVirtualMethod(CanonM, SubobjectNumber,
603 InVirtualSubobject));
604
Douglas Gregor4165bd62010-03-23 23:47:56 +0000605 if (CanonOM->begin_overridden_methods()
Anders Carlssona2f74f32010-06-03 01:00:02 +0000606 == CanonOM->end_overridden_methods())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000607 continue;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000608
609 // Continue recursion to the methods that this virtual method
610 // overrides.
611 Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(),
612 CanonOM->end_overridden_methods()));
613 }
614 }
Anders Carlssona2f74f32010-06-03 01:00:02 +0000615
616 // C++ [class.virtual]p2:
617 // For convenience we say that any virtual function overrides itself.
618 Overriders[CanonM].add(SubobjectNumber,
619 UniqueVirtualMethod(CanonM, SubobjectNumber,
620 InVirtualSubobject));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000621 }
622}
623
624FinalOverriderCollector::~FinalOverriderCollector() {
625 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
626 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
627 VO != VOEnd;
628 ++VO)
629 delete VO->second;
630}
631
632void
633CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
634 FinalOverriderCollector Collector;
635 Collector.Collect(this, false, 0, FinalOverriders);
636
637 // Weed out any final overriders that come from virtual base class
638 // subobjects that were hidden by other subobjects along any path.
639 // This is the final-overrider variant of C++ [class.member.lookup]p10.
640 for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(),
641 OMEnd = FinalOverriders.end();
642 OM != OMEnd;
643 ++OM) {
644 for (OverridingMethods::iterator SO = OM->second.begin(),
645 SOEnd = OM->second.end();
646 SO != SOEnd;
647 ++SO) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000648 SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO->second;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000649 if (Overriding.size() < 2)
650 continue;
651
Craig Topper2341c0d2013-07-04 03:08:24 +0000652 for (SmallVectorImpl<UniqueVirtualMethod>::iterator
Douglas Gregor4165bd62010-03-23 23:47:56 +0000653 Pos = Overriding.begin(), PosEnd = Overriding.end();
654 Pos != PosEnd;
655 /* increment in loop */) {
656 if (!Pos->InVirtualSubobject) {
657 ++Pos;
658 continue;
659 }
660
661 // We have an overriding method in a virtual base class
662 // subobject (or non-virtual base class subobject thereof);
663 // determine whether there exists an other overriding method
664 // in a base class subobject that hides the virtual base class
665 // subobject.
666 bool Hidden = false;
Craig Topper2341c0d2013-07-04 03:08:24 +0000667 for (SmallVectorImpl<UniqueVirtualMethod>::iterator
Douglas Gregor4165bd62010-03-23 23:47:56 +0000668 OP = Overriding.begin(), OPEnd = Overriding.end();
669 OP != OPEnd && !Hidden;
670 ++OP) {
671 if (Pos == OP)
672 continue;
673
674 if (OP->Method->getParent()->isVirtuallyDerivedFrom(
675 const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject)))
676 Hidden = true;
677 }
678
679 if (Hidden) {
680 // The current overriding function is hidden by another
681 // overriding function; remove this one.
682 Pos = Overriding.erase(Pos);
683 PosEnd = Overriding.end();
684 } else {
685 ++Pos;
686 }
687 }
688 }
689 }
690}
Anders Carlsson4131f002010-11-24 22:50:27 +0000691
692static void
693AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
694 CXXIndirectPrimaryBaseSet& Bases) {
695 // If the record has a virtual primary base class, add it to our set.
696 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000697 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson4131f002010-11-24 22:50:27 +0000698 Bases.insert(Layout.getPrimaryBase());
699
Aaron Ballman574705e2014-03-13 15:41:46 +0000700 for (const auto &I : RD->bases()) {
701 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000702 "Cannot get indirect primary bases for class with dependent bases.");
703
704 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000705 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000706
707 // Only bases with virtual bases participate in computing the
708 // indirect primary virtual base classes.
709 if (BaseDecl->getNumVBases())
710 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
711 }
712
713}
714
715void
716CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
717 ASTContext &Context = getASTContext();
718
719 if (!getNumVBases())
720 return;
721
Aaron Ballman574705e2014-03-13 15:41:46 +0000722 for (const auto &I : bases()) {
723 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000724 "Cannot get indirect primary bases for class with dependent bases.");
725
726 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000727 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000728
729 // Only bases with virtual bases participate in computing the
730 // indirect primary virtual base classes.
731 if (BaseDecl->getNumVBases())
732 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
733 }
734}