blob: a97d6a22e7b3fb10e64ea4fd4d2e97ff3f7e9f37 [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>
Douglas Gregor36d1b142009-10-06 17:59:45 +000019
20using namespace clang;
21
22/// \brief Computes the set of declarations referenced by these base
23/// paths.
24void CXXBasePaths::ComputeDeclsFound() {
25 assert(NumDeclsFound == 0 && !DeclsFound &&
26 "Already computed the set of declarations");
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000027
Douglas Gregor18e1b522012-09-11 07:19:42 +000028 llvm::SetVector<NamedDecl *, SmallVector<NamedDecl *, 8> > Decls;
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000029 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
David Blaikieff7d47a2012-12-19 00:45:41 +000030 Decls.insert(Path->Decls.front());
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000031
Douglas Gregor36d1b142009-10-06 17:59:45 +000032 NumDeclsFound = Decls.size();
David Blaikie8f2a7fe2015-08-18 23:56:00 +000033 DeclsFound = llvm::make_unique<NamedDecl *[]>(NumDeclsFound);
34 std::copy(Decls.begin(), Decls.end(), DeclsFound.get());
Douglas Gregor36d1b142009-10-06 17:59:45 +000035}
36
Aaron Ballmane6f465e2014-03-14 21:38:48 +000037CXXBasePaths::decl_range CXXBasePaths::found_decls() {
Douglas Gregor36d1b142009-10-06 17:59:45 +000038 if (NumDeclsFound == 0)
39 ComputeDeclsFound();
Douglas Gregor36d1b142009-10-06 17:59:45 +000040
David Blaikie8f2a7fe2015-08-18 23:56:00 +000041 return decl_range(decl_iterator(DeclsFound.get()),
42 decl_iterator(DeclsFound.get() + NumDeclsFound));
Douglas Gregor36d1b142009-10-06 17:59:45 +000043}
44
45/// isAmbiguous - Determines whether the set of paths provided is
46/// ambiguous, i.e., there are two or more paths that refer to
47/// different base class subobjects of the same type. BaseType must be
48/// an unqualified, canonical class type.
Douglas Gregor27ac4292010-05-21 20:29:55 +000049bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
50 BaseType = BaseType.getUnqualifiedType();
Douglas Gregor36d1b142009-10-06 17:59:45 +000051 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
52 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
53}
54
55/// clear - Clear out all prior path information.
56void CXXBasePaths::clear() {
57 Paths.clear();
58 ClassSubobjects.clear();
59 ScratchPath.clear();
Craig Topper36250ad2014-05-12 05:36:57 +000060 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +000061}
62
63/// @brief Swaps the contents of this CXXBasePaths structure with the
64/// contents of Other.
65void CXXBasePaths::swap(CXXBasePaths &Other) {
66 std::swap(Origin, Other.Origin);
67 Paths.swap(Other.Paths);
68 ClassSubobjects.swap(Other.ClassSubobjects);
69 std::swap(FindAmbiguities, Other.FindAmbiguities);
70 std::swap(RecordPaths, Other.RecordPaths);
71 std::swap(DetectVirtual, Other.DetectVirtual);
72 std::swap(DetectedVirtual, Other.DetectedVirtual);
73}
74
John McCall388ef532011-01-28 22:02:36 +000075bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000076 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
77 /*DetectVirtual=*/false);
78 return isDerivedFrom(Base, Paths);
79}
80
John McCall388ef532011-01-28 22:02:36 +000081bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
82 CXXBasePaths &Paths) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000083 if (getCanonicalDecl() == Base->getCanonicalDecl())
84 return false;
85
John McCall84c16cf2009-11-12 03:15:40 +000086 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +000087
88 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
Benjamin Kramer1d38be92015-07-25 15:27:04 +000089 // FIXME: Capturing 'this' is a workaround for name lookup bugs in GCC 4.7.
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +000090 return lookupInBases(
Benjamin Kramer1d38be92015-07-25 15:27:04 +000091 [this, BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +000092 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();
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000110 // FIXME: Capturing 'this' is a workaround for name lookup bugs in GCC 4.7.
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000111 return lookupInBases(
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000112 [this, BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000113 return FindVirtualBaseClass(Specifier, Path, BaseDecl);
114 },
115 Paths);
John McCallddabf1a2009-12-08 07:42:38 +0000116}
117
118bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000119 const CXXRecordDecl *TargetDecl = Base->getCanonicalDecl();
120 return forallBases([TargetDecl](const CXXRecordDecl *Base) {
121 return Base->getCanonicalDecl() != TargetDecl;
122 });
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
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000136bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches,
Douglas Gregordc974572012-11-10 07:24:09 +0000137 bool AllowShortCircuit) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000138 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCallddabf1a2009-12-08 07:42:38 +0000139
140 const CXXRecordDecl *Record = this;
141 bool AllMatches = true;
142 while (true) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000143 for (const auto &I : Record->bases()) {
144 const RecordType *Ty = I.getType()->getAs<RecordType>();
Douglas Gregordc974572012-11-10 07:24:09 +0000145 if (!Ty) {
John McCallddabf1a2009-12-08 07:42:38 +0000146 if (AllowShortCircuit) return false;
147 AllMatches = false;
148 continue;
149 }
150
Douglas Gregordc974572012-11-10 07:24:09 +0000151 CXXRecordDecl *Base =
152 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithd80b2d52012-11-22 00:24:47 +0000153 if (!Base ||
154 (Base->isDependentContext() &&
155 !Base->isCurrentInstantiation(Record))) {
John McCallddabf1a2009-12-08 07:42:38 +0000156 if (AllowShortCircuit) return false;
157 AllMatches = false;
158 continue;
159 }
Faisal Vali683b0742016-05-19 02:28:21 +0000160
161 Queue.push_back(Base);
162 if (!BaseMatches(Base)) {
163 if (AllowShortCircuit) return false;
164 AllMatches = false;
165 continue;
John McCallddabf1a2009-12-08 07:42:38 +0000166 }
167 }
168
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000169 if (Queue.empty())
170 break;
171 Record = Queue.pop_back_val(); // not actually a queue.
John McCallddabf1a2009-12-08 07:42:38 +0000172 }
173
174 return AllMatches;
175}
176
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000177bool CXXBasePaths::lookupInBases(
178 ASTContext &Context, const CXXRecordDecl *Record,
179 CXXRecordDecl::BaseMatchesCallback BaseMatches) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000180 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000181
John McCall553c0792010-01-23 00:46:32 +0000182 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000183 AccessSpecifier AccessToHere = ScratchPath.Access;
184 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000185
Aaron Ballman574705e2014-03-13 15:41:46 +0000186 for (const auto &BaseSpec : Record->bases()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000187 // Find the record of the base class subobjects for this type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000188 QualType BaseType =
189 Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
190
Douglas Gregor36d1b142009-10-06 17:59:45 +0000191 // C++ [temp.dep]p3:
192 // In the definition of a class template or a member of a class template,
193 // if a base class of the class template depends on a template-parameter,
194 // the base class scope is not examined during unqualified name lookup
195 // either at the point of definition of the class template or member or
196 // during an instantiation of the class tem- plate or member.
197 if (BaseType->isDependentType())
198 continue;
199
200 // Determine whether we need to visit this base class at all,
201 // updating the count of subobjects appropriately.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000202 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000203 bool VisitBase = true;
204 bool SetVirtual = false;
Aaron Ballman574705e2014-03-13 15:41:46 +0000205 if (BaseSpec.isVirtual()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000206 VisitBase = !Subobjects.first;
207 Subobjects.first = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000208 if (isDetectingVirtual() && DetectedVirtual == nullptr) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000209 // If this is the first virtual we find, remember it. If it turns out
210 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000211 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000212 SetVirtual = true;
213 }
214 } else
215 ++Subobjects.second;
216
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000217 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000218 // Add this base specifier to the current path.
219 CXXBasePathElement Element;
Aaron Ballman574705e2014-03-13 15:41:46 +0000220 Element.Base = &BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000221 Element.Class = Record;
Aaron Ballman574705e2014-03-13 15:41:46 +0000222 if (BaseSpec.isVirtual())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000223 Element.SubobjectNumber = 0;
224 else
225 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000226 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000227
John McCall553c0792010-01-23 00:46:32 +0000228 // Calculate the "top-down" access to this base class.
229 // The spec actually describes this bottom-up, but top-down is
230 // equivalent because the definition works out as follows:
231 // 1. Write down the access along each step in the inheritance
232 // chain, followed by the access of the decl itself.
233 // For example, in
234 // class A { public: int foo; };
235 // class B : protected A {};
236 // class C : public B {};
237 // class D : private C {};
238 // we would write:
239 // private public protected public
240 // 2. If 'private' appears anywhere except far-left, access is denied.
241 // 3. Otherwise, overall access is determined by the most restrictive
242 // access in the sequence.
243 if (IsFirstStep)
Aaron Ballman574705e2014-03-13 15:41:46 +0000244 ScratchPath.Access = BaseSpec.getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000245 else
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000246 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
Aaron Ballman574705e2014-03-13 15:41:46 +0000247 BaseSpec.getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000248 }
John McCall6f891402010-02-09 00:57:12 +0000249
250 // Track whether there's a path involving this specific base.
251 bool FoundPathThroughBase = false;
252
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000253 if (BaseMatches(&BaseSpec, ScratchPath)) {
John McCall553c0792010-01-23 00:46:32 +0000254 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000255 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000256 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000257 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000258 Paths.push_back(ScratchPath);
259 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000260 // We found a path and we don't care about ambiguities;
261 // return immediately.
262 return FoundPath;
263 }
264 } else if (VisitBase) {
265 CXXRecordDecl *BaseRecord
Aaron Ballman574705e2014-03-13 15:41:46 +0000266 = cast<CXXRecordDecl>(BaseSpec.getType()->castAs<RecordType>()
Douglas Gregor36d1b142009-10-06 17:59:45 +0000267 ->getDecl());
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000268 if (lookupInBases(Context, BaseRecord, BaseMatches)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000269 // C++ [class.member.lookup]p2:
270 // A member name f in one sub-object B hides a member name f in
271 // a sub-object A if A is a base class sub-object of B. Any
272 // declarations that are so hidden are eliminated from
273 // consideration.
274
275 // There is a path to a base class that meets the criteria. If we're
276 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000277 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000278 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000279 return FoundPath;
280 }
281 }
282
283 // Pop this base specifier off the current path (if we're
284 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000285 if (isRecordingPaths()) {
286 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000287 }
288
Douglas Gregor36d1b142009-10-06 17:59:45 +0000289 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000290 if (SetVirtual && !FoundPathThroughBase) {
Craig Topper36250ad2014-05-12 05:36:57 +0000291 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000292 }
293 }
John McCall553c0792010-01-23 00:46:32 +0000294
295 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000296 ScratchPath.Access = AccessToHere;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000297
298 return FoundPath;
299}
300
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000301bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches,
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000302 CXXBasePaths &Paths) const {
Douglas Gregor3e637462010-03-03 04:38:46 +0000303 // If we didn't find anything, report that.
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000304 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches))
Douglas Gregor3e637462010-03-03 04:38:46 +0000305 return false;
306
307 // If we're not recording paths or we won't ever find ambiguities,
308 // we're done.
309 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
310 return true;
311
312 // C++ [class.member.lookup]p6:
313 // When virtual base classes are used, a hidden declaration can be
314 // reached along a path through the sub-object lattice that does
315 // not pass through the hiding declaration. This is not an
316 // ambiguity. The identical use with nonvirtual base classes is an
317 // ambiguity; in that case there is no unique instance of the name
318 // that hides all the others.
319 //
320 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
321 // way to make it any faster.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000322 Paths.Paths.remove_if([&Paths](const CXXBasePath &Path) {
323 for (const CXXBasePathElement &PE : Path) {
324 if (!PE.Base->isVirtual())
325 continue;
Douglas Gregor3e637462010-03-03 04:38:46 +0000326
Benjamin Kramerece036e2015-02-11 19:09:16 +0000327 CXXRecordDecl *VBase = nullptr;
328 if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>())
329 VBase = cast<CXXRecordDecl>(Record->getDecl());
330 if (!VBase)
331 break;
332
333 // The declaration(s) we found along this path were found in a
334 // subobject of a virtual base. Check whether this virtual
335 // base is a subobject of any other path; if so, then the
336 // declaration in this path are hidden by that patch.
337 for (const CXXBasePath &HidingP : Paths) {
338 CXXRecordDecl *HidingClass = nullptr;
339 if (const RecordType *Record =
340 HidingP.back().Base->getType()->getAs<RecordType>())
341 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
342 if (!HidingClass)
Douglas Gregor3e637462010-03-03 04:38:46 +0000343 break;
344
Benjamin Kramerece036e2015-02-11 19:09:16 +0000345 if (HidingClass->isVirtuallyDerivedFrom(VBase))
346 return true;
Douglas Gregor3e637462010-03-03 04:38:46 +0000347 }
348 }
Benjamin Kramerece036e2015-02-11 19:09:16 +0000349 return false;
350 });
Douglas Gregor3e637462010-03-03 04:38:46 +0000351
Douglas Gregor3e637462010-03-03 04:38:46 +0000352 return true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000353}
354
John McCall84c16cf2009-11-12 03:15:40 +0000355bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000356 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000357 const CXXRecordDecl *BaseRecord) {
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000358 assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
Douglas Gregor36d1b142009-10-06 17:59:45 +0000359 "User data for FindBaseClass is not canonical!");
Ted Kremenek28831752012-08-23 20:46:57 +0000360 return Specifier->getType()->castAs<RecordType>()->getDecl()
361 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000362}
363
Douglas Gregor3e637462010-03-03 04:38:46 +0000364bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
365 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000366 const CXXRecordDecl *BaseRecord) {
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000367 assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
Douglas Gregor3e637462010-03-03 04:38:46 +0000368 "User data for FindBaseClass is not canonical!");
369 return Specifier->isVirtual() &&
Ted Kremenek28831752012-08-23 20:46:57 +0000370 Specifier->getType()->castAs<RecordType>()->getDecl()
371 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor3e637462010-03-03 04:38:46 +0000372}
373
John McCall84c16cf2009-11-12 03:15:40 +0000374bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000375 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000376 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000377 RecordDecl *BaseRecord =
378 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000379
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000380 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000381 !Path.Decls.empty();
382 Path.Decls = Path.Decls.slice(1)) {
383 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000384 return true;
385 }
386
387 return false;
388}
389
John McCall84c16cf2009-11-12 03:15:40 +0000390bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000391 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000392 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000393 RecordDecl *BaseRecord =
394 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000395
396 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000397 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000398 !Path.Decls.empty();
399 Path.Decls = Path.Decls.slice(1)) {
400 if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000401 return true;
402 }
403
404 return false;
405}
406
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000407bool CXXRecordDecl::FindOMPReductionMember(const CXXBaseSpecifier *Specifier,
408 CXXBasePath &Path,
409 DeclarationName Name) {
410 RecordDecl *BaseRecord =
411 Specifier->getType()->castAs<RecordType>()->getDecl();
412
413 for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty();
414 Path.Decls = Path.Decls.slice(1)) {
415 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPReduction))
416 return true;
417 }
418
419 return false;
420}
421
John McCall84c16cf2009-11-12 03:15:40 +0000422bool CXXRecordDecl::
423FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
424 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000425 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000426 RecordDecl *BaseRecord =
427 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000428
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000429 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000430 !Path.Decls.empty();
431 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000432 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikieff7d47a2012-12-19 00:45:41 +0000433 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
434 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000435 return true;
436 }
437
438 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000439}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000440
441void OverridingMethods::add(unsigned OverriddenSubobject,
442 UniqueVirtualMethod Overriding) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000443 SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
Douglas Gregor4165bd62010-03-23 23:47:56 +0000444 = Overrides[OverriddenSubobject];
445 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
446 Overriding) == SubobjectOverrides.end())
447 SubobjectOverrides.push_back(Overriding);
448}
449
450void OverridingMethods::add(const OverridingMethods &Other) {
451 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
452 for (overriding_const_iterator M = I->second.begin(),
453 MEnd = I->second.end();
454 M != MEnd;
455 ++M)
456 add(I->first, *M);
457 }
458}
459
460void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
461 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
462 I->second.clear();
463 I->second.push_back(Overriding);
464 }
465}
466
467
468namespace {
469 class FinalOverriderCollector {
470 /// \brief The number of subobjects of a given class type that
471 /// occur within the class hierarchy.
472 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
473
474 /// \brief Overriders for each virtual base subobject.
475 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
476
477 CXXFinalOverriderMap FinalOverriders;
478
479 public:
480 ~FinalOverriderCollector();
481
482 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
483 const CXXRecordDecl *InVirtualSubobject,
484 CXXFinalOverriderMap &Overriders);
485 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000486}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000487
488void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
489 bool VirtualBase,
490 const CXXRecordDecl *InVirtualSubobject,
491 CXXFinalOverriderMap &Overriders) {
492 unsigned SubobjectNumber = 0;
493 if (!VirtualBase)
494 SubobjectNumber
495 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
496
Aaron Ballman574705e2014-03-13 15:41:46 +0000497 for (const auto &Base : RD->bases()) {
498 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000499 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
500 if (!BaseDecl->isPolymorphic())
501 continue;
502
Aaron Ballman574705e2014-03-13 15:41:46 +0000503 if (Overriders.empty() && !Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000504 // There are no other overriders of virtual member functions,
505 // so let the base class fill in our overriders for us.
506 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
507 continue;
508 }
509
510 // Collect all of the overridders from the base class subobject
511 // and merge them into the set of overridders for this class.
512 // For virtual base classes, populate or use the cached virtual
513 // overrides so that we do not walk the virtual base class (and
514 // its base classes) more than once.
515 CXXFinalOverriderMap ComputedBaseOverriders;
516 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
Aaron Ballman574705e2014-03-13 15:41:46 +0000517 if (Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000518 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramerea388a22012-05-27 22:41:08 +0000519 BaseOverriders = MyVirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000520 if (!MyVirtualOverriders) {
521 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramerea388a22012-05-27 22:41:08 +0000522
523 // Collect may cause VirtualOverriders to reallocate, invalidating the
524 // MyVirtualOverriders reference. Set BaseOverriders to the right
525 // value now.
526 BaseOverriders = MyVirtualOverriders;
527
Douglas Gregor4165bd62010-03-23 23:47:56 +0000528 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
529 }
Douglas Gregor4165bd62010-03-23 23:47:56 +0000530 } else
531 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
532
533 // Merge the overriders from this base class into our own set of
534 // overriders.
535 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
536 OMEnd = BaseOverriders->end();
537 OM != OMEnd;
538 ++OM) {
539 const CXXMethodDecl *CanonOM
540 = cast<CXXMethodDecl>(OM->first->getCanonicalDecl());
541 Overriders[CanonOM].add(OM->second);
542 }
543 }
544 }
545
Aaron Ballman2b124d12014-03-13 16:36:16 +0000546 for (auto *M : RD->methods()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000547 // We only care about virtual methods.
548 if (!M->isVirtual())
549 continue;
550
551 CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl());
552
553 if (CanonM->begin_overridden_methods()
554 == CanonM->end_overridden_methods()) {
555 // This is a new virtual function that does not override any
556 // other virtual function. Add it to the map of virtual
557 // functions for which we are tracking overridders.
558
559 // C++ [class.virtual]p2:
560 // For convenience we say that any virtual function overrides itself.
561 Overriders[CanonM].add(SubobjectNumber,
562 UniqueVirtualMethod(CanonM, SubobjectNumber,
563 InVirtualSubobject));
564 continue;
565 }
566
567 // This virtual method overrides other virtual methods, so it does
568 // not add any new slots into the set of overriders. Instead, we
569 // replace entries in the set of overriders with the new
570 // overrider. To do so, we dig down to the original virtual
571 // functions using data recursion and update all of the methods it
572 // overrides.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000573 typedef llvm::iterator_range<CXXMethodDecl::method_iterator>
574 OverriddenMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000575 SmallVector<OverriddenMethods, 4> Stack;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000576 Stack.push_back(llvm::make_range(CanonM->begin_overridden_methods(),
577 CanonM->end_overridden_methods()));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000578 while (!Stack.empty()) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000579 for (const CXXMethodDecl *OM : Stack.pop_back_val()) {
580 const CXXMethodDecl *CanonOM = OM->getCanonicalDecl();
Anders Carlssona2f74f32010-06-03 01:00:02 +0000581
582 // C++ [class.virtual]p2:
583 // A virtual member function C::vf of a class object S is
584 // a final overrider unless the most derived class (1.8)
585 // of which S is a base class subobject (if any) declares
586 // or inherits another member function that overrides vf.
587 //
588 // Treating this object like the most derived class, we
589 // replace any overrides from base classes with this
590 // overriding virtual function.
591 Overriders[CanonOM].replaceAll(
592 UniqueVirtualMethod(CanonM, SubobjectNumber,
593 InVirtualSubobject));
594
Douglas Gregor4165bd62010-03-23 23:47:56 +0000595 if (CanonOM->begin_overridden_methods()
Anders Carlssona2f74f32010-06-03 01:00:02 +0000596 == CanonOM->end_overridden_methods())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000597 continue;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000598
599 // Continue recursion to the methods that this virtual method
600 // overrides.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000601 Stack.push_back(llvm::make_range(CanonOM->begin_overridden_methods(),
602 CanonOM->end_overridden_methods()));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000603 }
604 }
Anders Carlssona2f74f32010-06-03 01:00:02 +0000605
606 // C++ [class.virtual]p2:
607 // For convenience we say that any virtual function overrides itself.
608 Overriders[CanonM].add(SubobjectNumber,
609 UniqueVirtualMethod(CanonM, SubobjectNumber,
610 InVirtualSubobject));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000611 }
612}
613
614FinalOverriderCollector::~FinalOverriderCollector() {
615 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
616 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
617 VO != VOEnd;
618 ++VO)
619 delete VO->second;
620}
621
622void
623CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
624 FinalOverriderCollector Collector;
Craig Topper36250ad2014-05-12 05:36:57 +0000625 Collector.Collect(this, false, nullptr, FinalOverriders);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000626
627 // Weed out any final overriders that come from virtual base class
628 // subobjects that were hidden by other subobjects along any path.
629 // This is the final-overrider variant of C++ [class.member.lookup]p10.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000630 for (auto &OM : FinalOverriders) {
631 for (auto &SO : OM.second) {
632 SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO.second;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000633 if (Overriding.size() < 2)
634 continue;
635
Benjamin Kramerece036e2015-02-11 19:09:16 +0000636 auto IsHidden = [&Overriding](const UniqueVirtualMethod &M) {
637 if (!M.InVirtualSubobject)
638 return false;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000639
640 // We have an overriding method in a virtual base class
641 // subobject (or non-virtual base class subobject thereof);
642 // determine whether there exists an other overriding method
643 // in a base class subobject that hides the virtual base class
644 // subobject.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000645 for (const UniqueVirtualMethod &OP : Overriding)
646 if (&M != &OP &&
647 OP.Method->getParent()->isVirtuallyDerivedFrom(
648 M.InVirtualSubobject))
649 return true;
650 return false;
651 };
Douglas Gregor4165bd62010-03-23 23:47:56 +0000652
Benjamin Kramerece036e2015-02-11 19:09:16 +0000653 Overriding.erase(
654 std::remove_if(Overriding.begin(), Overriding.end(), IsHidden),
655 Overriding.end());
Douglas Gregor4165bd62010-03-23 23:47:56 +0000656 }
657 }
658}
Anders Carlsson4131f002010-11-24 22:50:27 +0000659
660static void
661AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
662 CXXIndirectPrimaryBaseSet& Bases) {
663 // If the record has a virtual primary base class, add it to our set.
664 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000665 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson4131f002010-11-24 22:50:27 +0000666 Bases.insert(Layout.getPrimaryBase());
667
Aaron Ballman574705e2014-03-13 15:41:46 +0000668 for (const auto &I : RD->bases()) {
669 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000670 "Cannot get indirect primary bases for class with dependent bases.");
671
672 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000673 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000674
675 // Only bases with virtual bases participate in computing the
676 // indirect primary virtual base classes.
677 if (BaseDecl->getNumVBases())
678 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
679 }
680
681}
682
683void
684CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
685 ASTContext &Context = getASTContext();
686
687 if (!getNumVBases())
688 return;
689
Aaron Ballman574705e2014-03-13 15:41:46 +0000690 for (const auto &I : bases()) {
691 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000692 "Cannot get indirect primary bases for class with dependent bases.");
693
694 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000695 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000696
697 // Only bases with virtual bases participate in computing the
698 // indirect primary virtual base classes.
699 if (BaseDecl->getNumVBases())
700 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
701 }
702}