blob: 6e80ee7c28a1bb91af6cf823f097a6af1e803719 [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
Aaron Ballmane6f465e2014-03-14 21:38:48 +000038CXXBasePaths::decl_range CXXBasePaths::found_decls() {
Douglas Gregor36d1b142009-10-06 17:59:45 +000039 if (NumDeclsFound == 0)
40 ComputeDeclsFound();
Douglas Gregor36d1b142009-10-06 17:59:45 +000041
Aaron Ballmane6f465e2014-03-14 21:38:48 +000042 return decl_range(decl_iterator(DeclsFound),
43 decl_iterator(DeclsFound + NumDeclsFound));
Douglas Gregor36d1b142009-10-06 17:59:45 +000044}
45
46/// isAmbiguous - Determines whether the set of paths provided is
47/// ambiguous, i.e., there are two or more paths that refer to
48/// different base class subobjects of the same type. BaseType must be
49/// an unqualified, canonical class type.
Douglas Gregor27ac4292010-05-21 20:29:55 +000050bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
51 BaseType = BaseType.getUnqualifiedType();
Douglas Gregor36d1b142009-10-06 17:59:45 +000052 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
53 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
54}
55
56/// clear - Clear out all prior path information.
57void CXXBasePaths::clear() {
58 Paths.clear();
59 ClassSubobjects.clear();
60 ScratchPath.clear();
Craig Topper36250ad2014-05-12 05:36:57 +000061 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +000062}
63
64/// @brief Swaps the contents of this CXXBasePaths structure with the
65/// contents of Other.
66void CXXBasePaths::swap(CXXBasePaths &Other) {
67 std::swap(Origin, Other.Origin);
68 Paths.swap(Other.Paths);
69 ClassSubobjects.swap(Other.ClassSubobjects);
70 std::swap(FindAmbiguities, Other.FindAmbiguities);
71 std::swap(RecordPaths, Other.RecordPaths);
72 std::swap(DetectVirtual, Other.DetectVirtual);
73 std::swap(DetectedVirtual, Other.DetectedVirtual);
74}
75
John McCall388ef532011-01-28 22:02:36 +000076bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000077 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
78 /*DetectVirtual=*/false);
79 return isDerivedFrom(Base, Paths);
80}
81
John McCall388ef532011-01-28 22:02:36 +000082bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
83 CXXBasePaths &Paths) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000084 if (getCanonicalDecl() == Base->getCanonicalDecl())
85 return false;
86
John McCall84c16cf2009-11-12 03:15:40 +000087 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
John McCall388ef532011-01-28 22:02:36 +000088 return lookupInBases(&FindBaseClass,
89 const_cast<CXXRecordDecl*>(Base->getCanonicalDecl()),
90 Paths);
Douglas Gregor36d1b142009-10-06 17:59:45 +000091}
92
Jordan Rose55edf5ff2012-08-08 18:23:20 +000093bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
Anders Carlsson15722da2010-06-04 01:40:08 +000094 if (!getNumVBases())
95 return false;
96
Douglas Gregor3e637462010-03-03 04:38:46 +000097 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
98 /*DetectVirtual=*/false);
99
100 if (getCanonicalDecl() == Base->getCanonicalDecl())
101 return false;
102
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000103 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
104
105 const void *BasePtr = static_cast<const void*>(Base->getCanonicalDecl());
106 return lookupInBases(&FindVirtualBaseClass,
107 const_cast<void *>(BasePtr),
108 Paths);
Douglas Gregor3e637462010-03-03 04:38:46 +0000109}
110
John McCallddabf1a2009-12-08 07:42:38 +0000111static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) {
112 // OpaqueTarget is a CXXRecordDecl*.
113 return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget;
114}
115
116bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
David Greene94319262013-01-15 22:09:40 +0000117 return forallBases(BaseIsNot,
118 const_cast<CXXRecordDecl *>(Base->getCanonicalDecl()));
John McCallddabf1a2009-12-08 07:42:38 +0000119}
120
Richard Smithd80b2d52012-11-22 00:24:47 +0000121bool
122CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const {
123 assert(isDependentContext());
124
125 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
126 if (CurContext->Equals(this))
127 return true;
128
129 return false;
130}
131
John McCallddabf1a2009-12-08 07:42:38 +0000132bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches,
133 void *OpaqueData,
Douglas Gregordc974572012-11-10 07:24:09 +0000134 bool AllowShortCircuit) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000135 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCallddabf1a2009-12-08 07:42:38 +0000136
137 const CXXRecordDecl *Record = this;
138 bool AllMatches = true;
139 while (true) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000140 for (const auto &I : Record->bases()) {
141 const RecordType *Ty = I.getType()->getAs<RecordType>();
Douglas Gregordc974572012-11-10 07:24:09 +0000142 if (!Ty) {
John McCallddabf1a2009-12-08 07:42:38 +0000143 if (AllowShortCircuit) return false;
144 AllMatches = false;
145 continue;
146 }
147
Douglas Gregordc974572012-11-10 07:24:09 +0000148 CXXRecordDecl *Base =
149 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithd80b2d52012-11-22 00:24:47 +0000150 if (!Base ||
151 (Base->isDependentContext() &&
152 !Base->isCurrentInstantiation(Record))) {
John McCallddabf1a2009-12-08 07:42:38 +0000153 if (AllowShortCircuit) return false;
154 AllMatches = false;
155 continue;
156 }
157
Anders Carlssonf9812782009-12-09 04:26:02 +0000158 Queue.push_back(Base);
159 if (!BaseMatches(Base, OpaqueData)) {
John McCallddabf1a2009-12-08 07:42:38 +0000160 if (AllowShortCircuit) return false;
161 AllMatches = false;
162 continue;
163 }
164 }
165
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000166 if (Queue.empty())
167 break;
168 Record = Queue.pop_back_val(); // not actually a queue.
John McCallddabf1a2009-12-08 07:42:38 +0000169 }
170
171 return AllMatches;
172}
173
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000174bool CXXBasePaths::lookupInBases(ASTContext &Context,
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000175 const CXXRecordDecl *Record,
176 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
177 void *UserData) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000178 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000179
John McCall553c0792010-01-23 00:46:32 +0000180 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000181 AccessSpecifier AccessToHere = ScratchPath.Access;
182 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000183
Aaron Ballman574705e2014-03-13 15:41:46 +0000184 for (const auto &BaseSpec : Record->bases()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000185 // Find the record of the base class subobjects for this type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000186 QualType BaseType =
187 Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
188
Douglas Gregor36d1b142009-10-06 17:59:45 +0000189 // C++ [temp.dep]p3:
190 // In the definition of a class template or a member of a class template,
191 // if a base class of the class template depends on a template-parameter,
192 // the base class scope is not examined during unqualified name lookup
193 // either at the point of definition of the class template or member or
194 // during an instantiation of the class tem- plate or member.
195 if (BaseType->isDependentType())
196 continue;
197
198 // Determine whether we need to visit this base class at all,
199 // updating the count of subobjects appropriately.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000200 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000201 bool VisitBase = true;
202 bool SetVirtual = false;
Aaron Ballman574705e2014-03-13 15:41:46 +0000203 if (BaseSpec.isVirtual()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000204 VisitBase = !Subobjects.first;
205 Subobjects.first = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000206 if (isDetectingVirtual() && DetectedVirtual == nullptr) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000207 // If this is the first virtual we find, remember it. If it turns out
208 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000209 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000210 SetVirtual = true;
211 }
212 } else
213 ++Subobjects.second;
214
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000215 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000216 // Add this base specifier to the current path.
217 CXXBasePathElement Element;
Aaron Ballman574705e2014-03-13 15:41:46 +0000218 Element.Base = &BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000219 Element.Class = Record;
Aaron Ballman574705e2014-03-13 15:41:46 +0000220 if (BaseSpec.isVirtual())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000221 Element.SubobjectNumber = 0;
222 else
223 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000224 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000225
John McCall553c0792010-01-23 00:46:32 +0000226 // Calculate the "top-down" access to this base class.
227 // The spec actually describes this bottom-up, but top-down is
228 // equivalent because the definition works out as follows:
229 // 1. Write down the access along each step in the inheritance
230 // chain, followed by the access of the decl itself.
231 // For example, in
232 // class A { public: int foo; };
233 // class B : protected A {};
234 // class C : public B {};
235 // class D : private C {};
236 // we would write:
237 // private public protected public
238 // 2. If 'private' appears anywhere except far-left, access is denied.
239 // 3. Otherwise, overall access is determined by the most restrictive
240 // access in the sequence.
241 if (IsFirstStep)
Aaron Ballman574705e2014-03-13 15:41:46 +0000242 ScratchPath.Access = BaseSpec.getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000243 else
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000244 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
Aaron Ballman574705e2014-03-13 15:41:46 +0000245 BaseSpec.getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000246 }
John McCall6f891402010-02-09 00:57:12 +0000247
248 // Track whether there's a path involving this specific base.
249 bool FoundPathThroughBase = false;
250
Aaron Ballman574705e2014-03-13 15:41:46 +0000251 if (BaseMatches(&BaseSpec, ScratchPath, UserData)) {
John McCall553c0792010-01-23 00:46:32 +0000252 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000253 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000254 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000255 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000256 Paths.push_back(ScratchPath);
257 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000258 // We found a path and we don't care about ambiguities;
259 // return immediately.
260 return FoundPath;
261 }
262 } else if (VisitBase) {
263 CXXRecordDecl *BaseRecord
Aaron Ballman574705e2014-03-13 15:41:46 +0000264 = cast<CXXRecordDecl>(BaseSpec.getType()->castAs<RecordType>()
Douglas Gregor36d1b142009-10-06 17:59:45 +0000265 ->getDecl());
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000266 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000267 // C++ [class.member.lookup]p2:
268 // A member name f in one sub-object B hides a member name f in
269 // a sub-object A if A is a base class sub-object of B. Any
270 // declarations that are so hidden are eliminated from
271 // consideration.
272
273 // There is a path to a base class that meets the criteria. If we're
274 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000275 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000276 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000277 return FoundPath;
278 }
279 }
280
281 // Pop this base specifier off the current path (if we're
282 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000283 if (isRecordingPaths()) {
284 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000285 }
286
Douglas Gregor36d1b142009-10-06 17:59:45 +0000287 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000288 if (SetVirtual && !FoundPathThroughBase) {
Craig Topper36250ad2014-05-12 05:36:57 +0000289 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000290 }
291 }
John McCall553c0792010-01-23 00:46:32 +0000292
293 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000294 ScratchPath.Access = AccessToHere;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000295
296 return FoundPath;
297}
298
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000299bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches,
300 void *UserData,
301 CXXBasePaths &Paths) const {
Douglas Gregor3e637462010-03-03 04:38:46 +0000302 // If we didn't find anything, report that.
303 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData))
304 return false;
305
306 // If we're not recording paths or we won't ever find ambiguities,
307 // we're done.
308 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
309 return true;
310
311 // C++ [class.member.lookup]p6:
312 // When virtual base classes are used, a hidden declaration can be
313 // reached along a path through the sub-object lattice that does
314 // not pass through the hiding declaration. This is not an
315 // ambiguity. The identical use with nonvirtual base classes is an
316 // ambiguity; in that case there is no unique instance of the name
317 // that hides all the others.
318 //
319 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
320 // way to make it any faster.
321 for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end();
322 P != PEnd; /* increment in loop */) {
323 bool Hidden = false;
324
325 for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end();
326 PE != PEEnd && !Hidden; ++PE) {
327 if (PE->Base->isVirtual()) {
Craig Topper36250ad2014-05-12 05:36:57 +0000328 CXXRecordDecl *VBase = nullptr;
Douglas Gregor3e637462010-03-03 04:38:46 +0000329 if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>())
330 VBase = cast<CXXRecordDecl>(Record->getDecl());
331 if (!VBase)
332 break;
333
334 // The declaration(s) we found along this path were found in a
335 // subobject of a virtual base. Check whether this virtual
336 // base is a subobject of any other path; if so, then the
337 // declaration in this path are hidden by that patch.
338 for (CXXBasePaths::paths_iterator HidingP = Paths.begin(),
339 HidingPEnd = Paths.end();
340 HidingP != HidingPEnd;
341 ++HidingP) {
Craig Topper36250ad2014-05-12 05:36:57 +0000342 CXXRecordDecl *HidingClass = nullptr;
Douglas Gregor3e637462010-03-03 04:38:46 +0000343 if (const RecordType *Record
344 = HidingP->back().Base->getType()->getAs<RecordType>())
345 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
346 if (!HidingClass)
347 break;
348
349 if (HidingClass->isVirtuallyDerivedFrom(VBase)) {
350 Hidden = true;
351 break;
352 }
353 }
354 }
355 }
356
357 if (Hidden)
358 P = Paths.Paths.erase(P);
359 else
360 ++P;
361 }
362
363 return true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000364}
365
John McCall84c16cf2009-11-12 03:15:40 +0000366bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000367 CXXBasePath &Path,
368 void *BaseRecord) {
369 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
370 "User data for FindBaseClass is not canonical!");
Ted Kremenek28831752012-08-23 20:46:57 +0000371 return Specifier->getType()->castAs<RecordType>()->getDecl()
372 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000373}
374
Douglas Gregor3e637462010-03-03 04:38:46 +0000375bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
376 CXXBasePath &Path,
377 void *BaseRecord) {
378 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
379 "User data for FindBaseClass is not canonical!");
380 return Specifier->isVirtual() &&
Ted Kremenek28831752012-08-23 20:46:57 +0000381 Specifier->getType()->castAs<RecordType>()->getDecl()
382 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor3e637462010-03-03 04:38:46 +0000383}
384
John McCall84c16cf2009-11-12 03:15:40 +0000385bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000386 CXXBasePath &Path,
387 void *Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000388 RecordDecl *BaseRecord =
389 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000390
391 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
392 for (Path.Decls = BaseRecord->lookup(N);
David Blaikieff7d47a2012-12-19 00:45:41 +0000393 !Path.Decls.empty();
394 Path.Decls = Path.Decls.slice(1)) {
395 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000396 return true;
397 }
398
399 return false;
400}
401
John McCall84c16cf2009-11-12 03:15:40 +0000402bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000403 CXXBasePath &Path,
404 void *Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000405 RecordDecl *BaseRecord =
406 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000407
408 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
409 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
410 for (Path.Decls = BaseRecord->lookup(N);
David Blaikieff7d47a2012-12-19 00:45:41 +0000411 !Path.Decls.empty();
412 Path.Decls = Path.Decls.slice(1)) {
413 if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000414 return true;
415 }
416
417 return false;
418}
419
John McCall84c16cf2009-11-12 03:15:40 +0000420bool CXXRecordDecl::
421FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
422 CXXBasePath &Path,
423 void *Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000424 RecordDecl *BaseRecord =
425 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000426
427 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
428 for (Path.Decls = BaseRecord->lookup(N);
David Blaikieff7d47a2012-12-19 00:45:41 +0000429 !Path.Decls.empty();
430 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000431 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikieff7d47a2012-12-19 00:45:41 +0000432 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
433 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000434 return true;
435 }
436
437 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000438}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000439
440void OverridingMethods::add(unsigned OverriddenSubobject,
441 UniqueVirtualMethod Overriding) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000442 SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
Douglas Gregor4165bd62010-03-23 23:47:56 +0000443 = Overrides[OverriddenSubobject];
444 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
445 Overriding) == SubobjectOverrides.end())
446 SubobjectOverrides.push_back(Overriding);
447}
448
449void OverridingMethods::add(const OverridingMethods &Other) {
450 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
451 for (overriding_const_iterator M = I->second.begin(),
452 MEnd = I->second.end();
453 M != MEnd;
454 ++M)
455 add(I->first, *M);
456 }
457}
458
459void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
460 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
461 I->second.clear();
462 I->second.push_back(Overriding);
463 }
464}
465
466
467namespace {
468 class FinalOverriderCollector {
469 /// \brief The number of subobjects of a given class type that
470 /// occur within the class hierarchy.
471 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
472
473 /// \brief Overriders for each virtual base subobject.
474 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
475
476 CXXFinalOverriderMap FinalOverriders;
477
478 public:
479 ~FinalOverriderCollector();
480
481 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
482 const CXXRecordDecl *InVirtualSubobject,
483 CXXFinalOverriderMap &Overriders);
484 };
485}
486
487void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
488 bool VirtualBase,
489 const CXXRecordDecl *InVirtualSubobject,
490 CXXFinalOverriderMap &Overriders) {
491 unsigned SubobjectNumber = 0;
492 if (!VirtualBase)
493 SubobjectNumber
494 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
495
Aaron Ballman574705e2014-03-13 15:41:46 +0000496 for (const auto &Base : RD->bases()) {
497 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000498 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
499 if (!BaseDecl->isPolymorphic())
500 continue;
501
Aaron Ballman574705e2014-03-13 15:41:46 +0000502 if (Overriders.empty() && !Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000503 // There are no other overriders of virtual member functions,
504 // so let the base class fill in our overriders for us.
505 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
506 continue;
507 }
508
509 // Collect all of the overridders from the base class subobject
510 // and merge them into the set of overridders for this class.
511 // For virtual base classes, populate or use the cached virtual
512 // overrides so that we do not walk the virtual base class (and
513 // its base classes) more than once.
514 CXXFinalOverriderMap ComputedBaseOverriders;
515 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
Aaron Ballman574705e2014-03-13 15:41:46 +0000516 if (Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000517 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramerea388a22012-05-27 22:41:08 +0000518 BaseOverriders = MyVirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000519 if (!MyVirtualOverriders) {
520 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramerea388a22012-05-27 22:41:08 +0000521
522 // Collect may cause VirtualOverriders to reallocate, invalidating the
523 // MyVirtualOverriders reference. Set BaseOverriders to the right
524 // value now.
525 BaseOverriders = MyVirtualOverriders;
526
Douglas Gregor4165bd62010-03-23 23:47:56 +0000527 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
528 }
Douglas Gregor4165bd62010-03-23 23:47:56 +0000529 } else
530 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
531
532 // Merge the overriders from this base class into our own set of
533 // overriders.
534 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
535 OMEnd = BaseOverriders->end();
536 OM != OMEnd;
537 ++OM) {
538 const CXXMethodDecl *CanonOM
539 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl());
540 Overriders[CanonOM].add(OM->second);
541 }
542 }
543 }
544
Aaron Ballman2b124d12014-03-13 16:36:16 +0000545 for (auto *M : RD->methods()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000546 // We only care about virtual methods.
547 if (!M->isVirtual())
548 continue;
549
550 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl());
551
552 if (CanonM->begin_overridden_methods()
553 == CanonM->end_overridden_methods()) {
554 // This is a new virtual function that does not override any
555 // other virtual function. Add it to the map of virtual
556 // functions for which we are tracking overridders.
557
558 // C++ [class.virtual]p2:
559 // For convenience we say that any virtual function overrides itself.
560 Overriders[CanonM].add(SubobjectNumber,
561 UniqueVirtualMethod(CanonM, SubobjectNumber,
562 InVirtualSubobject));
563 continue;
564 }
565
566 // This virtual method overrides other virtual methods, so it does
567 // not add any new slots into the set of overriders. Instead, we
568 // replace entries in the set of overriders with the new
569 // overrider. To do so, we dig down to the original virtual
570 // functions using data recursion and update all of the methods it
571 // overrides.
572 typedef std::pair<CXXMethodDecl::method_iterator,
573 CXXMethodDecl::method_iterator> OverriddenMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000574 SmallVector<OverriddenMethods, 4> Stack;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000575 Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(),
576 CanonM->end_overridden_methods()));
577 while (!Stack.empty()) {
578 OverriddenMethods OverMethods = Stack.back();
579 Stack.pop_back();
580
581 for (; OverMethods.first != OverMethods.second; ++OverMethods.first) {
582 const CXXMethodDecl *CanonOM
583 = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl());
Anders Carlssona2f74f32010-06-03 01:00:02 +0000584
585 // C++ [class.virtual]p2:
586 // A virtual member function C::vf of a class object S is
587 // a final overrider unless the most derived class (1.8)
588 // of which S is a base class subobject (if any) declares
589 // or inherits another member function that overrides vf.
590 //
591 // Treating this object like the most derived class, we
592 // replace any overrides from base classes with this
593 // overriding virtual function.
594 Overriders[CanonOM].replaceAll(
595 UniqueVirtualMethod(CanonM, SubobjectNumber,
596 InVirtualSubobject));
597
Douglas Gregor4165bd62010-03-23 23:47:56 +0000598 if (CanonOM->begin_overridden_methods()
Anders Carlssona2f74f32010-06-03 01:00:02 +0000599 == CanonOM->end_overridden_methods())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000600 continue;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000601
602 // Continue recursion to the methods that this virtual method
603 // overrides.
604 Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(),
605 CanonOM->end_overridden_methods()));
606 }
607 }
Anders Carlssona2f74f32010-06-03 01:00:02 +0000608
609 // C++ [class.virtual]p2:
610 // For convenience we say that any virtual function overrides itself.
611 Overriders[CanonM].add(SubobjectNumber,
612 UniqueVirtualMethod(CanonM, SubobjectNumber,
613 InVirtualSubobject));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000614 }
615}
616
617FinalOverriderCollector::~FinalOverriderCollector() {
618 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
619 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
620 VO != VOEnd;
621 ++VO)
622 delete VO->second;
623}
624
625void
626CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
627 FinalOverriderCollector Collector;
Craig Topper36250ad2014-05-12 05:36:57 +0000628 Collector.Collect(this, false, nullptr, FinalOverriders);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000629
630 // Weed out any final overriders that come from virtual base class
631 // subobjects that were hidden by other subobjects along any path.
632 // This is the final-overrider variant of C++ [class.member.lookup]p10.
633 for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(),
634 OMEnd = FinalOverriders.end();
635 OM != OMEnd;
636 ++OM) {
637 for (OverridingMethods::iterator SO = OM->second.begin(),
638 SOEnd = OM->second.end();
639 SO != SOEnd;
640 ++SO) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000641 SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO->second;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000642 if (Overriding.size() < 2)
643 continue;
644
Craig Topper2341c0d2013-07-04 03:08:24 +0000645 for (SmallVectorImpl<UniqueVirtualMethod>::iterator
Douglas Gregor4165bd62010-03-23 23:47:56 +0000646 Pos = Overriding.begin(), PosEnd = Overriding.end();
647 Pos != PosEnd;
648 /* increment in loop */) {
649 if (!Pos->InVirtualSubobject) {
650 ++Pos;
651 continue;
652 }
653
654 // We have an overriding method in a virtual base class
655 // subobject (or non-virtual base class subobject thereof);
656 // determine whether there exists an other overriding method
657 // in a base class subobject that hides the virtual base class
658 // subobject.
659 bool Hidden = false;
Craig Topper2341c0d2013-07-04 03:08:24 +0000660 for (SmallVectorImpl<UniqueVirtualMethod>::iterator
Douglas Gregor4165bd62010-03-23 23:47:56 +0000661 OP = Overriding.begin(), OPEnd = Overriding.end();
662 OP != OPEnd && !Hidden;
663 ++OP) {
664 if (Pos == OP)
665 continue;
666
667 if (OP->Method->getParent()->isVirtuallyDerivedFrom(
668 const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject)))
669 Hidden = true;
670 }
671
672 if (Hidden) {
673 // The current overriding function is hidden by another
674 // overriding function; remove this one.
675 Pos = Overriding.erase(Pos);
676 PosEnd = Overriding.end();
677 } else {
678 ++Pos;
679 }
680 }
681 }
682 }
683}
Anders Carlsson4131f002010-11-24 22:50:27 +0000684
685static void
686AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
687 CXXIndirectPrimaryBaseSet& Bases) {
688 // If the record has a virtual primary base class, add it to our set.
689 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000690 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson4131f002010-11-24 22:50:27 +0000691 Bases.insert(Layout.getPrimaryBase());
692
Aaron Ballman574705e2014-03-13 15:41:46 +0000693 for (const auto &I : RD->bases()) {
694 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000695 "Cannot get indirect primary bases for class with dependent bases.");
696
697 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000698 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000699
700 // Only bases with virtual bases participate in computing the
701 // indirect primary virtual base classes.
702 if (BaseDecl->getNumVBases())
703 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
704 }
705
706}
707
708void
709CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
710 ASTContext &Context = getASTContext();
711
712 if (!getNumVBases())
713 return;
714
Aaron Ballman574705e2014-03-13 15:41:46 +0000715 for (const auto &I : bases()) {
716 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000717 "Cannot get indirect primary bases for class with dependent bases.");
718
719 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000720 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000721
722 // Only bases with virtual bases participate in computing the
723 // indirect primary virtual base classes.
724 if (BaseDecl->getNumVBases())
725 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
726 }
727}