blob: c8529196c842d87fee174721208594c86d12ca06 [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));
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +000088
89 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
90 return lookupInBases(
91 [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
92 return FindBaseClass(Specifier, Path, BaseDecl);
93 },
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
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000109 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
110 return lookupInBases(
111 [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
112 return FindVirtualBaseClass(Specifier, Path, BaseDecl);
113 },
114 Paths);
John McCallddabf1a2009-12-08 07:42:38 +0000115}
116
117bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000118 const CXXRecordDecl *TargetDecl = Base->getCanonicalDecl();
119 return forallBases([TargetDecl](const CXXRecordDecl *Base) {
120 return Base->getCanonicalDecl() != TargetDecl;
121 });
John McCallddabf1a2009-12-08 07:42:38 +0000122}
123
Richard Smithd80b2d52012-11-22 00:24:47 +0000124bool
125CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const {
126 assert(isDependentContext());
127
128 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
129 if (CurContext->Equals(this))
130 return true;
131
132 return false;
133}
134
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000135bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches,
Douglas Gregordc974572012-11-10 07:24:09 +0000136 bool AllowShortCircuit) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000137 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCallddabf1a2009-12-08 07:42:38 +0000138
139 const CXXRecordDecl *Record = this;
140 bool AllMatches = true;
141 while (true) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000142 for (const auto &I : Record->bases()) {
143 const RecordType *Ty = I.getType()->getAs<RecordType>();
Douglas Gregordc974572012-11-10 07:24:09 +0000144 if (!Ty) {
John McCallddabf1a2009-12-08 07:42:38 +0000145 if (AllowShortCircuit) return false;
146 AllMatches = false;
147 continue;
148 }
149
Douglas Gregordc974572012-11-10 07:24:09 +0000150 CXXRecordDecl *Base =
151 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithd80b2d52012-11-22 00:24:47 +0000152 if (!Base ||
153 (Base->isDependentContext() &&
154 !Base->isCurrentInstantiation(Record))) {
John McCallddabf1a2009-12-08 07:42:38 +0000155 if (AllowShortCircuit) return false;
156 AllMatches = false;
157 continue;
158 }
159
Anders Carlssonf9812782009-12-09 04:26:02 +0000160 Queue.push_back(Base);
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000161 if (!BaseMatches(Base)) {
John McCallddabf1a2009-12-08 07:42:38 +0000162 if (AllowShortCircuit) return false;
163 AllMatches = false;
164 continue;
165 }
166 }
167
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000168 if (Queue.empty())
169 break;
170 Record = Queue.pop_back_val(); // not actually a queue.
John McCallddabf1a2009-12-08 07:42:38 +0000171 }
172
173 return AllMatches;
174}
175
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000176bool CXXBasePaths::lookupInBases(
177 ASTContext &Context, const CXXRecordDecl *Record,
178 CXXRecordDecl::BaseMatchesCallback BaseMatches) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000179 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000180
John McCall553c0792010-01-23 00:46:32 +0000181 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000182 AccessSpecifier AccessToHere = ScratchPath.Access;
183 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000184
Aaron Ballman574705e2014-03-13 15:41:46 +0000185 for (const auto &BaseSpec : Record->bases()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000186 // Find the record of the base class subobjects for this type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000187 QualType BaseType =
188 Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
189
Douglas Gregor36d1b142009-10-06 17:59:45 +0000190 // C++ [temp.dep]p3:
191 // In the definition of a class template or a member of a class template,
192 // if a base class of the class template depends on a template-parameter,
193 // the base class scope is not examined during unqualified name lookup
194 // either at the point of definition of the class template or member or
195 // during an instantiation of the class tem- plate or member.
196 if (BaseType->isDependentType())
197 continue;
198
199 // Determine whether we need to visit this base class at all,
200 // updating the count of subobjects appropriately.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000201 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000202 bool VisitBase = true;
203 bool SetVirtual = false;
Aaron Ballman574705e2014-03-13 15:41:46 +0000204 if (BaseSpec.isVirtual()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000205 VisitBase = !Subobjects.first;
206 Subobjects.first = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000207 if (isDetectingVirtual() && DetectedVirtual == nullptr) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000208 // If this is the first virtual we find, remember it. If it turns out
209 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000210 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000211 SetVirtual = true;
212 }
213 } else
214 ++Subobjects.second;
215
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000216 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000217 // Add this base specifier to the current path.
218 CXXBasePathElement Element;
Aaron Ballman574705e2014-03-13 15:41:46 +0000219 Element.Base = &BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000220 Element.Class = Record;
Aaron Ballman574705e2014-03-13 15:41:46 +0000221 if (BaseSpec.isVirtual())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000222 Element.SubobjectNumber = 0;
223 else
224 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000225 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000226
John McCall553c0792010-01-23 00:46:32 +0000227 // Calculate the "top-down" access to this base class.
228 // The spec actually describes this bottom-up, but top-down is
229 // equivalent because the definition works out as follows:
230 // 1. Write down the access along each step in the inheritance
231 // chain, followed by the access of the decl itself.
232 // For example, in
233 // class A { public: int foo; };
234 // class B : protected A {};
235 // class C : public B {};
236 // class D : private C {};
237 // we would write:
238 // private public protected public
239 // 2. If 'private' appears anywhere except far-left, access is denied.
240 // 3. Otherwise, overall access is determined by the most restrictive
241 // access in the sequence.
242 if (IsFirstStep)
Aaron Ballman574705e2014-03-13 15:41:46 +0000243 ScratchPath.Access = BaseSpec.getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000244 else
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000245 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
Aaron Ballman574705e2014-03-13 15:41:46 +0000246 BaseSpec.getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000247 }
John McCall6f891402010-02-09 00:57:12 +0000248
249 // Track whether there's a path involving this specific base.
250 bool FoundPathThroughBase = false;
251
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000252 if (BaseMatches(&BaseSpec, ScratchPath)) {
John McCall553c0792010-01-23 00:46:32 +0000253 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000254 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000255 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000256 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000257 Paths.push_back(ScratchPath);
258 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000259 // We found a path and we don't care about ambiguities;
260 // return immediately.
261 return FoundPath;
262 }
263 } else if (VisitBase) {
264 CXXRecordDecl *BaseRecord
Aaron Ballman574705e2014-03-13 15:41:46 +0000265 = cast<CXXRecordDecl>(BaseSpec.getType()->castAs<RecordType>()
Douglas Gregor36d1b142009-10-06 17:59:45 +0000266 ->getDecl());
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000267 if (lookupInBases(Context, BaseRecord, BaseMatches)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000268 // C++ [class.member.lookup]p2:
269 // A member name f in one sub-object B hides a member name f in
270 // a sub-object A if A is a base class sub-object of B. Any
271 // declarations that are so hidden are eliminated from
272 // consideration.
273
274 // There is a path to a base class that meets the criteria. If we're
275 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000276 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000277 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000278 return FoundPath;
279 }
280 }
281
282 // Pop this base specifier off the current path (if we're
283 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000284 if (isRecordingPaths()) {
285 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000286 }
287
Douglas Gregor36d1b142009-10-06 17:59:45 +0000288 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000289 if (SetVirtual && !FoundPathThroughBase) {
Craig Topper36250ad2014-05-12 05:36:57 +0000290 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000291 }
292 }
John McCall553c0792010-01-23 00:46:32 +0000293
294 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000295 ScratchPath.Access = AccessToHere;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000296
297 return FoundPath;
298}
299
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000300bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches,
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000301 CXXBasePaths &Paths) const {
Douglas Gregor3e637462010-03-03 04:38:46 +0000302 // If we didn't find anything, report that.
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000303 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches))
Douglas Gregor3e637462010-03-03 04:38:46 +0000304 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.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000321 Paths.Paths.remove_if([&Paths](const CXXBasePath &Path) {
322 for (const CXXBasePathElement &PE : Path) {
323 if (!PE.Base->isVirtual())
324 continue;
Douglas Gregor3e637462010-03-03 04:38:46 +0000325
Benjamin Kramerece036e2015-02-11 19:09:16 +0000326 CXXRecordDecl *VBase = nullptr;
327 if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>())
328 VBase = cast<CXXRecordDecl>(Record->getDecl());
329 if (!VBase)
330 break;
331
332 // The declaration(s) we found along this path were found in a
333 // subobject of a virtual base. Check whether this virtual
334 // base is a subobject of any other path; if so, then the
335 // declaration in this path are hidden by that patch.
336 for (const CXXBasePath &HidingP : Paths) {
337 CXXRecordDecl *HidingClass = nullptr;
338 if (const RecordType *Record =
339 HidingP.back().Base->getType()->getAs<RecordType>())
340 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
341 if (!HidingClass)
Douglas Gregor3e637462010-03-03 04:38:46 +0000342 break;
343
Benjamin Kramerece036e2015-02-11 19:09:16 +0000344 if (HidingClass->isVirtuallyDerivedFrom(VBase))
345 return true;
Douglas Gregor3e637462010-03-03 04:38:46 +0000346 }
347 }
Benjamin Kramerece036e2015-02-11 19:09:16 +0000348 return false;
349 });
Douglas Gregor3e637462010-03-03 04:38:46 +0000350
Douglas Gregor3e637462010-03-03 04:38:46 +0000351 return true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000352}
353
John McCall84c16cf2009-11-12 03:15:40 +0000354bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000355 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000356 const CXXRecordDecl *BaseRecord) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000357 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
358 "User data for FindBaseClass is not canonical!");
Ted Kremenek28831752012-08-23 20:46:57 +0000359 return Specifier->getType()->castAs<RecordType>()->getDecl()
360 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000361}
362
Douglas Gregor3e637462010-03-03 04:38:46 +0000363bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
364 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000365 const CXXRecordDecl *BaseRecord) {
Douglas Gregor3e637462010-03-03 04:38:46 +0000366 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
367 "User data for FindBaseClass is not canonical!");
368 return Specifier->isVirtual() &&
Ted Kremenek28831752012-08-23 20:46:57 +0000369 Specifier->getType()->castAs<RecordType>()->getDecl()
370 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor3e637462010-03-03 04:38:46 +0000371}
372
John McCall84c16cf2009-11-12 03:15:40 +0000373bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000374 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000375 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000376 RecordDecl *BaseRecord =
377 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000378
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000379 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000380 !Path.Decls.empty();
381 Path.Decls = Path.Decls.slice(1)) {
382 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000383 return true;
384 }
385
386 return false;
387}
388
John McCall84c16cf2009-11-12 03:15:40 +0000389bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000390 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000391 DeclarationName 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 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000396 for (Path.Decls = BaseRecord->lookup(Name);
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))
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::
407FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
408 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000409 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000410 RecordDecl *BaseRecord =
411 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000412
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000413 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000414 !Path.Decls.empty();
415 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000416 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikieff7d47a2012-12-19 00:45:41 +0000417 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
418 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000419 return true;
420 }
421
422 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000423}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000424
425void OverridingMethods::add(unsigned OverriddenSubobject,
426 UniqueVirtualMethod Overriding) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000427 SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
Douglas Gregor4165bd62010-03-23 23:47:56 +0000428 = Overrides[OverriddenSubobject];
429 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
430 Overriding) == SubobjectOverrides.end())
431 SubobjectOverrides.push_back(Overriding);
432}
433
434void OverridingMethods::add(const OverridingMethods &Other) {
435 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
436 for (overriding_const_iterator M = I->second.begin(),
437 MEnd = I->second.end();
438 M != MEnd;
439 ++M)
440 add(I->first, *M);
441 }
442}
443
444void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
445 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
446 I->second.clear();
447 I->second.push_back(Overriding);
448 }
449}
450
451
452namespace {
453 class FinalOverriderCollector {
454 /// \brief The number of subobjects of a given class type that
455 /// occur within the class hierarchy.
456 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
457
458 /// \brief Overriders for each virtual base subobject.
459 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
460
461 CXXFinalOverriderMap FinalOverriders;
462
463 public:
464 ~FinalOverriderCollector();
465
466 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
467 const CXXRecordDecl *InVirtualSubobject,
468 CXXFinalOverriderMap &Overriders);
469 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000470}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000471
472void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
473 bool VirtualBase,
474 const CXXRecordDecl *InVirtualSubobject,
475 CXXFinalOverriderMap &Overriders) {
476 unsigned SubobjectNumber = 0;
477 if (!VirtualBase)
478 SubobjectNumber
479 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
480
Aaron Ballman574705e2014-03-13 15:41:46 +0000481 for (const auto &Base : RD->bases()) {
482 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000483 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
484 if (!BaseDecl->isPolymorphic())
485 continue;
486
Aaron Ballman574705e2014-03-13 15:41:46 +0000487 if (Overriders.empty() && !Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000488 // There are no other overriders of virtual member functions,
489 // so let the base class fill in our overriders for us.
490 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
491 continue;
492 }
493
494 // Collect all of the overridders from the base class subobject
495 // and merge them into the set of overridders for this class.
496 // For virtual base classes, populate or use the cached virtual
497 // overrides so that we do not walk the virtual base class (and
498 // its base classes) more than once.
499 CXXFinalOverriderMap ComputedBaseOverriders;
500 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
Aaron Ballman574705e2014-03-13 15:41:46 +0000501 if (Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000502 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramerea388a22012-05-27 22:41:08 +0000503 BaseOverriders = MyVirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000504 if (!MyVirtualOverriders) {
505 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramerea388a22012-05-27 22:41:08 +0000506
507 // Collect may cause VirtualOverriders to reallocate, invalidating the
508 // MyVirtualOverriders reference. Set BaseOverriders to the right
509 // value now.
510 BaseOverriders = MyVirtualOverriders;
511
Douglas Gregor4165bd62010-03-23 23:47:56 +0000512 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
513 }
Douglas Gregor4165bd62010-03-23 23:47:56 +0000514 } else
515 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
516
517 // Merge the overriders from this base class into our own set of
518 // overriders.
519 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
520 OMEnd = BaseOverriders->end();
521 OM != OMEnd;
522 ++OM) {
523 const CXXMethodDecl *CanonOM
524 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl());
525 Overriders[CanonOM].add(OM->second);
526 }
527 }
528 }
529
Aaron Ballman2b124d12014-03-13 16:36:16 +0000530 for (auto *M : RD->methods()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000531 // We only care about virtual methods.
532 if (!M->isVirtual())
533 continue;
534
535 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl());
536
537 if (CanonM->begin_overridden_methods()
538 == CanonM->end_overridden_methods()) {
539 // This is a new virtual function that does not override any
540 // other virtual function. Add it to the map of virtual
541 // functions for which we are tracking overridders.
542
543 // C++ [class.virtual]p2:
544 // For convenience we say that any virtual function overrides itself.
545 Overriders[CanonM].add(SubobjectNumber,
546 UniqueVirtualMethod(CanonM, SubobjectNumber,
547 InVirtualSubobject));
548 continue;
549 }
550
551 // This virtual method overrides other virtual methods, so it does
552 // not add any new slots into the set of overriders. Instead, we
553 // replace entries in the set of overriders with the new
554 // overrider. To do so, we dig down to the original virtual
555 // functions using data recursion and update all of the methods it
556 // overrides.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000557 typedef llvm::iterator_range<CXXMethodDecl::method_iterator>
558 OverriddenMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 SmallVector<OverriddenMethods, 4> Stack;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000560 Stack.push_back(llvm::make_range(CanonM->begin_overridden_methods(),
561 CanonM->end_overridden_methods()));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000562 while (!Stack.empty()) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000563 for (const CXXMethodDecl *OM : Stack.pop_back_val()) {
564 const CXXMethodDecl *CanonOM = OM->getCanonicalDecl();
Anders Carlssona2f74f32010-06-03 01:00:02 +0000565
566 // C++ [class.virtual]p2:
567 // A virtual member function C::vf of a class object S is
568 // a final overrider unless the most derived class (1.8)
569 // of which S is a base class subobject (if any) declares
570 // or inherits another member function that overrides vf.
571 //
572 // Treating this object like the most derived class, we
573 // replace any overrides from base classes with this
574 // overriding virtual function.
575 Overriders[CanonOM].replaceAll(
576 UniqueVirtualMethod(CanonM, SubobjectNumber,
577 InVirtualSubobject));
578
Douglas Gregor4165bd62010-03-23 23:47:56 +0000579 if (CanonOM->begin_overridden_methods()
Anders Carlssona2f74f32010-06-03 01:00:02 +0000580 == CanonOM->end_overridden_methods())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000581 continue;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000582
583 // Continue recursion to the methods that this virtual method
584 // overrides.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000585 Stack.push_back(llvm::make_range(CanonOM->begin_overridden_methods(),
586 CanonOM->end_overridden_methods()));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000587 }
588 }
Anders Carlssona2f74f32010-06-03 01:00:02 +0000589
590 // C++ [class.virtual]p2:
591 // For convenience we say that any virtual function overrides itself.
592 Overriders[CanonM].add(SubobjectNumber,
593 UniqueVirtualMethod(CanonM, SubobjectNumber,
594 InVirtualSubobject));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000595 }
596}
597
598FinalOverriderCollector::~FinalOverriderCollector() {
599 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
600 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
601 VO != VOEnd;
602 ++VO)
603 delete VO->second;
604}
605
606void
607CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
608 FinalOverriderCollector Collector;
Craig Topper36250ad2014-05-12 05:36:57 +0000609 Collector.Collect(this, false, nullptr, FinalOverriders);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000610
611 // Weed out any final overriders that come from virtual base class
612 // subobjects that were hidden by other subobjects along any path.
613 // This is the final-overrider variant of C++ [class.member.lookup]p10.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000614 for (auto &OM : FinalOverriders) {
615 for (auto &SO : OM.second) {
616 SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO.second;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000617 if (Overriding.size() < 2)
618 continue;
619
Benjamin Kramerece036e2015-02-11 19:09:16 +0000620 auto IsHidden = [&Overriding](const UniqueVirtualMethod &M) {
621 if (!M.InVirtualSubobject)
622 return false;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000623
624 // We have an overriding method in a virtual base class
625 // subobject (or non-virtual base class subobject thereof);
626 // determine whether there exists an other overriding method
627 // in a base class subobject that hides the virtual base class
628 // subobject.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000629 for (const UniqueVirtualMethod &OP : Overriding)
630 if (&M != &OP &&
631 OP.Method->getParent()->isVirtuallyDerivedFrom(
632 M.InVirtualSubobject))
633 return true;
634 return false;
635 };
Douglas Gregor4165bd62010-03-23 23:47:56 +0000636
Benjamin Kramerece036e2015-02-11 19:09:16 +0000637 Overriding.erase(
638 std::remove_if(Overriding.begin(), Overriding.end(), IsHidden),
639 Overriding.end());
Douglas Gregor4165bd62010-03-23 23:47:56 +0000640 }
641 }
642}
Anders Carlsson4131f002010-11-24 22:50:27 +0000643
644static void
645AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
646 CXXIndirectPrimaryBaseSet& Bases) {
647 // If the record has a virtual primary base class, add it to our set.
648 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000649 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson4131f002010-11-24 22:50:27 +0000650 Bases.insert(Layout.getPrimaryBase());
651
Aaron Ballman574705e2014-03-13 15:41:46 +0000652 for (const auto &I : RD->bases()) {
653 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000654 "Cannot get indirect primary bases for class with dependent bases.");
655
656 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000657 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000658
659 // Only bases with virtual bases participate in computing the
660 // indirect primary virtual base classes.
661 if (BaseDecl->getNumVBases())
662 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
663 }
664
665}
666
667void
668CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
669 ASTContext &Context = getASTContext();
670
671 if (!getNumVBases())
672 return;
673
Aaron Ballman574705e2014-03-13 15:41:46 +0000674 for (const auto &I : bases()) {
675 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000676 "Cannot get indirect primary bases for class with dependent bases.");
677
678 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000679 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000680
681 // Only bases with virtual bases participate in computing the
682 // indirect primary virtual base classes.
683 if (BaseDecl->getNumVBases())
684 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
685 }
686}