blob: 9314cfde7c02ca75ca0c62b765d43726c843840c [file] [log] [blame]
Eugene Zelenkof71964a2017-11-30 22:33:48 +00001//===- CXXInheritance.cpp - C++ Inheritance -------------------------------===//
Douglas Gregor36d1b142009-10-06 17:59:45 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenkof71964a2017-11-30 22:33:48 +000013
Douglas Gregor36d1b142009-10-06 17:59:45 +000014#include "clang/AST/CXXInheritance.h"
Benjamin Kramer2ef30312012-07-04 18:45:14 +000015#include "clang/AST/ASTContext.h"
Eugene Zelenkof71964a2017-11-30 22:33:48 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000018#include "clang/AST/DeclCXX.h"
Alex Lorenz4e1377a2017-05-10 09:47:41 +000019#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/RecordLayout.h"
Eugene Zelenkof71964a2017-11-30 22:33:48 +000021#include "clang/AST/TemplateName.h"
22#include "clang/AST/Type.h"
23#include "clang/Basic/LLVM.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/STLExtras.h"
Douglas Gregor18e1b522012-09-11 07:19:42 +000026#include "llvm/ADT/SetVector.h"
Eugene Zelenkof71964a2017-11-30 22:33:48 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/iterator_range.h"
29#include "llvm/Support/Casting.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000030#include <algorithm>
Eugene Zelenkof71964a2017-11-30 22:33:48 +000031#include <utility>
32#include <cassert>
33#include <vector>
Douglas Gregor36d1b142009-10-06 17:59:45 +000034
35using namespace clang;
36
37/// \brief Computes the set of declarations referenced by these base
38/// paths.
39void CXXBasePaths::ComputeDeclsFound() {
40 assert(NumDeclsFound == 0 && !DeclsFound &&
41 "Already computed the set of declarations");
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000042
Eugene Zelenkof71964a2017-11-30 22:33:48 +000043 llvm::SetVector<NamedDecl *, SmallVector<NamedDecl *, 8>> Decls;
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000044 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
David Blaikieff7d47a2012-12-19 00:45:41 +000045 Decls.insert(Path->Decls.front());
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000046
Douglas Gregor36d1b142009-10-06 17:59:45 +000047 NumDeclsFound = Decls.size();
David Blaikie8f2a7fe2015-08-18 23:56:00 +000048 DeclsFound = llvm::make_unique<NamedDecl *[]>(NumDeclsFound);
49 std::copy(Decls.begin(), Decls.end(), DeclsFound.get());
Douglas Gregor36d1b142009-10-06 17:59:45 +000050}
51
Aaron Ballmane6f465e2014-03-14 21:38:48 +000052CXXBasePaths::decl_range CXXBasePaths::found_decls() {
Douglas Gregor36d1b142009-10-06 17:59:45 +000053 if (NumDeclsFound == 0)
54 ComputeDeclsFound();
Douglas Gregor36d1b142009-10-06 17:59:45 +000055
David Blaikie8f2a7fe2015-08-18 23:56:00 +000056 return decl_range(decl_iterator(DeclsFound.get()),
57 decl_iterator(DeclsFound.get() + NumDeclsFound));
Douglas Gregor36d1b142009-10-06 17:59:45 +000058}
59
60/// isAmbiguous - Determines whether the set of paths provided is
61/// ambiguous, i.e., there are two or more paths that refer to
62/// different base class subobjects of the same type. BaseType must be
63/// an unqualified, canonical class type.
Douglas Gregor27ac4292010-05-21 20:29:55 +000064bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
65 BaseType = BaseType.getUnqualifiedType();
Douglas Gregor36d1b142009-10-06 17:59:45 +000066 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
67 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
68}
69
70/// clear - Clear out all prior path information.
71void CXXBasePaths::clear() {
72 Paths.clear();
73 ClassSubobjects.clear();
Alex Lorenz6796c0b2017-05-18 18:06:07 +000074 VisitedDependentRecords.clear();
Douglas Gregor36d1b142009-10-06 17:59:45 +000075 ScratchPath.clear();
Craig Topper36250ad2014-05-12 05:36:57 +000076 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +000077}
78
79/// @brief Swaps the contents of this CXXBasePaths structure with the
80/// contents of Other.
81void CXXBasePaths::swap(CXXBasePaths &Other) {
82 std::swap(Origin, Other.Origin);
83 Paths.swap(Other.Paths);
84 ClassSubobjects.swap(Other.ClassSubobjects);
Alex Lorenz6796c0b2017-05-18 18:06:07 +000085 VisitedDependentRecords.swap(Other.VisitedDependentRecords);
Douglas Gregor36d1b142009-10-06 17:59:45 +000086 std::swap(FindAmbiguities, Other.FindAmbiguities);
87 std::swap(RecordPaths, Other.RecordPaths);
88 std::swap(DetectVirtual, Other.DetectVirtual);
89 std::swap(DetectedVirtual, Other.DetectedVirtual);
90}
91
John McCall388ef532011-01-28 22:02:36 +000092bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000093 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
94 /*DetectVirtual=*/false);
95 return isDerivedFrom(Base, Paths);
96}
97
John McCall388ef532011-01-28 22:02:36 +000098bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
99 CXXBasePaths &Paths) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000100 if (getCanonicalDecl() == Base->getCanonicalDecl())
101 return false;
102
John McCall84c16cf2009-11-12 03:15:40 +0000103 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000104
105 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000106 // FIXME: Capturing 'this' is a workaround for name lookup bugs in GCC 4.7.
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000107 return lookupInBases(
Malcolm Parsonsc6e45832017-01-13 18:55:32 +0000108 [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000109 return FindBaseClass(Specifier, Path, BaseDecl);
110 },
111 Paths);
Douglas Gregor36d1b142009-10-06 17:59:45 +0000112}
113
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000114bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
Anders Carlsson15722da2010-06-04 01:40:08 +0000115 if (!getNumVBases())
116 return false;
117
Douglas Gregor3e637462010-03-03 04:38:46 +0000118 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
119 /*DetectVirtual=*/false);
120
121 if (getCanonicalDecl() == Base->getCanonicalDecl())
122 return false;
123
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000124 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
125
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000126 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000127 // FIXME: Capturing 'this' is a workaround for name lookup bugs in GCC 4.7.
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000128 return lookupInBases(
Malcolm Parsonsc6e45832017-01-13 18:55:32 +0000129 [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000130 return FindVirtualBaseClass(Specifier, Path, BaseDecl);
131 },
132 Paths);
John McCallddabf1a2009-12-08 07:42:38 +0000133}
134
135bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000136 const CXXRecordDecl *TargetDecl = Base->getCanonicalDecl();
137 return forallBases([TargetDecl](const CXXRecordDecl *Base) {
138 return Base->getCanonicalDecl() != TargetDecl;
139 });
John McCallddabf1a2009-12-08 07:42:38 +0000140}
141
Richard Smithd80b2d52012-11-22 00:24:47 +0000142bool
143CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const {
144 assert(isDependentContext());
145
146 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
147 if (CurContext->Equals(this))
148 return true;
149
150 return false;
151}
152
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000153bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches,
Douglas Gregordc974572012-11-10 07:24:09 +0000154 bool AllowShortCircuit) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000155 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCallddabf1a2009-12-08 07:42:38 +0000156
157 const CXXRecordDecl *Record = this;
158 bool AllMatches = true;
159 while (true) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000160 for (const auto &I : Record->bases()) {
161 const RecordType *Ty = I.getType()->getAs<RecordType>();
Douglas Gregordc974572012-11-10 07:24:09 +0000162 if (!Ty) {
John McCallddabf1a2009-12-08 07:42:38 +0000163 if (AllowShortCircuit) return false;
164 AllMatches = false;
165 continue;
166 }
167
Douglas Gregordc974572012-11-10 07:24:09 +0000168 CXXRecordDecl *Base =
169 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithd80b2d52012-11-22 00:24:47 +0000170 if (!Base ||
171 (Base->isDependentContext() &&
172 !Base->isCurrentInstantiation(Record))) {
John McCallddabf1a2009-12-08 07:42:38 +0000173 if (AllowShortCircuit) return false;
174 AllMatches = false;
175 continue;
176 }
Faisal Vali683b0742016-05-19 02:28:21 +0000177
178 Queue.push_back(Base);
179 if (!BaseMatches(Base)) {
180 if (AllowShortCircuit) return false;
181 AllMatches = false;
182 continue;
John McCallddabf1a2009-12-08 07:42:38 +0000183 }
184 }
185
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000186 if (Queue.empty())
187 break;
188 Record = Queue.pop_back_val(); // not actually a queue.
John McCallddabf1a2009-12-08 07:42:38 +0000189 }
190
191 return AllMatches;
192}
193
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000194bool CXXBasePaths::lookupInBases(ASTContext &Context,
195 const CXXRecordDecl *Record,
196 CXXRecordDecl::BaseMatchesCallback BaseMatches,
197 bool LookupInDependent) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000198 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000199
John McCall553c0792010-01-23 00:46:32 +0000200 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000201 AccessSpecifier AccessToHere = ScratchPath.Access;
202 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000203
Aaron Ballman574705e2014-03-13 15:41:46 +0000204 for (const auto &BaseSpec : Record->bases()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000205 // Find the record of the base class subobjects for this type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000206 QualType BaseType =
207 Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
208
Douglas Gregor36d1b142009-10-06 17:59:45 +0000209 // C++ [temp.dep]p3:
210 // In the definition of a class template or a member of a class template,
211 // if a base class of the class template depends on a template-parameter,
212 // the base class scope is not examined during unqualified name lookup
213 // either at the point of definition of the class template or member or
214 // during an instantiation of the class tem- plate or member.
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000215 if (!LookupInDependent && BaseType->isDependentType())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000216 continue;
217
218 // Determine whether we need to visit this base class at all,
219 // updating the count of subobjects appropriately.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000220 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000221 bool VisitBase = true;
222 bool SetVirtual = false;
Aaron Ballman574705e2014-03-13 15:41:46 +0000223 if (BaseSpec.isVirtual()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000224 VisitBase = !Subobjects.first;
225 Subobjects.first = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000226 if (isDetectingVirtual() && DetectedVirtual == nullptr) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000227 // If this is the first virtual we find, remember it. If it turns out
228 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000229 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000230 SetVirtual = true;
231 }
232 } else
233 ++Subobjects.second;
234
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000235 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000236 // Add this base specifier to the current path.
237 CXXBasePathElement Element;
Aaron Ballman574705e2014-03-13 15:41:46 +0000238 Element.Base = &BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000239 Element.Class = Record;
Aaron Ballman574705e2014-03-13 15:41:46 +0000240 if (BaseSpec.isVirtual())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000241 Element.SubobjectNumber = 0;
242 else
243 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000244 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000245
John McCall553c0792010-01-23 00:46:32 +0000246 // Calculate the "top-down" access to this base class.
247 // The spec actually describes this bottom-up, but top-down is
248 // equivalent because the definition works out as follows:
249 // 1. Write down the access along each step in the inheritance
250 // chain, followed by the access of the decl itself.
251 // For example, in
252 // class A { public: int foo; };
253 // class B : protected A {};
254 // class C : public B {};
255 // class D : private C {};
256 // we would write:
257 // private public protected public
258 // 2. If 'private' appears anywhere except far-left, access is denied.
259 // 3. Otherwise, overall access is determined by the most restrictive
260 // access in the sequence.
261 if (IsFirstStep)
Aaron Ballman574705e2014-03-13 15:41:46 +0000262 ScratchPath.Access = BaseSpec.getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000263 else
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000264 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
Aaron Ballman574705e2014-03-13 15:41:46 +0000265 BaseSpec.getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000266 }
John McCall6f891402010-02-09 00:57:12 +0000267
268 // Track whether there's a path involving this specific base.
269 bool FoundPathThroughBase = false;
270
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000271 if (BaseMatches(&BaseSpec, ScratchPath)) {
John McCall553c0792010-01-23 00:46:32 +0000272 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000273 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000274 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000275 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000276 Paths.push_back(ScratchPath);
277 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000278 // We found a path and we don't care about ambiguities;
279 // return immediately.
280 return FoundPath;
281 }
282 } else if (VisitBase) {
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000283 CXXRecordDecl *BaseRecord;
284 if (LookupInDependent) {
285 BaseRecord = nullptr;
286 const TemplateSpecializationType *TST =
287 BaseSpec.getType()->getAs<TemplateSpecializationType>();
288 if (!TST) {
289 if (auto *RT = BaseSpec.getType()->getAs<RecordType>())
290 BaseRecord = cast<CXXRecordDecl>(RT->getDecl());
291 } else {
292 TemplateName TN = TST->getTemplateName();
293 if (auto *TD =
294 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()))
295 BaseRecord = TD->getTemplatedDecl();
296 }
Alex Lorenz6796c0b2017-05-18 18:06:07 +0000297 if (BaseRecord) {
298 if (!BaseRecord->hasDefinition() ||
299 VisitedDependentRecords.count(BaseRecord)) {
300 BaseRecord = nullptr;
301 } else {
302 VisitedDependentRecords.insert(BaseRecord);
303 }
304 }
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000305 } else {
306 BaseRecord = cast<CXXRecordDecl>(
307 BaseSpec.getType()->castAs<RecordType>()->getDecl());
308 }
309 if (BaseRecord &&
310 lookupInBases(Context, BaseRecord, BaseMatches, LookupInDependent)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000311 // C++ [class.member.lookup]p2:
312 // A member name f in one sub-object B hides a member name f in
313 // a sub-object A if A is a base class sub-object of B. Any
314 // declarations that are so hidden are eliminated from
315 // consideration.
316
317 // There is a path to a base class that meets the criteria. If we're
318 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000319 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000320 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000321 return FoundPath;
322 }
323 }
324
325 // Pop this base specifier off the current path (if we're
326 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000327 if (isRecordingPaths()) {
328 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000329 }
330
Douglas Gregor36d1b142009-10-06 17:59:45 +0000331 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000332 if (SetVirtual && !FoundPathThroughBase) {
Craig Topper36250ad2014-05-12 05:36:57 +0000333 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000334 }
335 }
John McCall553c0792010-01-23 00:46:32 +0000336
337 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000338 ScratchPath.Access = AccessToHere;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000339
340 return FoundPath;
341}
342
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000343bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches,
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000344 CXXBasePaths &Paths,
345 bool LookupInDependent) const {
Douglas Gregor3e637462010-03-03 04:38:46 +0000346 // If we didn't find anything, report that.
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000347 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches,
348 LookupInDependent))
Douglas Gregor3e637462010-03-03 04:38:46 +0000349 return false;
350
351 // If we're not recording paths or we won't ever find ambiguities,
352 // we're done.
353 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
354 return true;
355
356 // C++ [class.member.lookup]p6:
357 // When virtual base classes are used, a hidden declaration can be
358 // reached along a path through the sub-object lattice that does
359 // not pass through the hiding declaration. This is not an
360 // ambiguity. The identical use with nonvirtual base classes is an
361 // ambiguity; in that case there is no unique instance of the name
362 // that hides all the others.
363 //
364 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
365 // way to make it any faster.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000366 Paths.Paths.remove_if([&Paths](const CXXBasePath &Path) {
367 for (const CXXBasePathElement &PE : Path) {
368 if (!PE.Base->isVirtual())
369 continue;
Douglas Gregor3e637462010-03-03 04:38:46 +0000370
Benjamin Kramerece036e2015-02-11 19:09:16 +0000371 CXXRecordDecl *VBase = nullptr;
372 if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>())
373 VBase = cast<CXXRecordDecl>(Record->getDecl());
374 if (!VBase)
375 break;
376
377 // The declaration(s) we found along this path were found in a
378 // subobject of a virtual base. Check whether this virtual
379 // base is a subobject of any other path; if so, then the
380 // declaration in this path are hidden by that patch.
381 for (const CXXBasePath &HidingP : Paths) {
382 CXXRecordDecl *HidingClass = nullptr;
383 if (const RecordType *Record =
384 HidingP.back().Base->getType()->getAs<RecordType>())
385 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
386 if (!HidingClass)
Douglas Gregor3e637462010-03-03 04:38:46 +0000387 break;
388
Benjamin Kramerece036e2015-02-11 19:09:16 +0000389 if (HidingClass->isVirtuallyDerivedFrom(VBase))
390 return true;
Douglas Gregor3e637462010-03-03 04:38:46 +0000391 }
392 }
Benjamin Kramerece036e2015-02-11 19:09:16 +0000393 return false;
394 });
Douglas Gregor3e637462010-03-03 04:38:46 +0000395
Douglas Gregor3e637462010-03-03 04:38:46 +0000396 return true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000397}
398
John McCall84c16cf2009-11-12 03:15:40 +0000399bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000400 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000401 const CXXRecordDecl *BaseRecord) {
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000402 assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
Douglas Gregor36d1b142009-10-06 17:59:45 +0000403 "User data for FindBaseClass is not canonical!");
Ted Kremenek28831752012-08-23 20:46:57 +0000404 return Specifier->getType()->castAs<RecordType>()->getDecl()
405 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000406}
407
Douglas Gregor3e637462010-03-03 04:38:46 +0000408bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
409 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000410 const CXXRecordDecl *BaseRecord) {
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000411 assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
Douglas Gregor3e637462010-03-03 04:38:46 +0000412 "User data for FindBaseClass is not canonical!");
413 return Specifier->isVirtual() &&
Ted Kremenek28831752012-08-23 20:46:57 +0000414 Specifier->getType()->castAs<RecordType>()->getDecl()
415 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor3e637462010-03-03 04:38:46 +0000416}
417
John McCall84c16cf2009-11-12 03:15:40 +0000418bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000419 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000420 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000421 RecordDecl *BaseRecord =
422 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000423
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000424 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000425 !Path.Decls.empty();
426 Path.Decls = Path.Decls.slice(1)) {
427 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000428 return true;
429 }
430
431 return false;
432}
433
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000434static bool findOrdinaryMember(RecordDecl *BaseRecord, CXXBasePath &Path,
435 DeclarationName Name) {
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000436 const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag |
437 Decl::IDNS_Member;
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000438 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000439 !Path.Decls.empty();
440 Path.Decls = Path.Decls.slice(1)) {
441 if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000442 return true;
443 }
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000444
Douglas Gregor36d1b142009-10-06 17:59:45 +0000445 return false;
446}
447
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000448bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
449 CXXBasePath &Path,
450 DeclarationName Name) {
451 RecordDecl *BaseRecord =
452 Specifier->getType()->castAs<RecordType>()->getDecl();
453 return findOrdinaryMember(BaseRecord, Path, Name);
454}
455
456bool CXXRecordDecl::FindOrdinaryMemberInDependentClasses(
457 const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
458 DeclarationName Name) {
459 const TemplateSpecializationType *TST =
460 Specifier->getType()->getAs<TemplateSpecializationType>();
461 if (!TST) {
462 auto *RT = Specifier->getType()->getAs<RecordType>();
463 if (!RT)
464 return false;
465 return findOrdinaryMember(RT->getDecl(), Path, Name);
466 }
467 TemplateName TN = TST->getTemplateName();
468 const auto *TD = dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
469 if (!TD)
470 return false;
471 CXXRecordDecl *RD = TD->getTemplatedDecl();
472 if (!RD)
473 return false;
474 return findOrdinaryMember(RD, Path, Name);
475}
476
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000477bool CXXRecordDecl::FindOMPReductionMember(const CXXBaseSpecifier *Specifier,
478 CXXBasePath &Path,
479 DeclarationName Name) {
480 RecordDecl *BaseRecord =
481 Specifier->getType()->castAs<RecordType>()->getDecl();
482
483 for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty();
484 Path.Decls = Path.Decls.slice(1)) {
485 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPReduction))
486 return true;
487 }
488
489 return false;
490}
491
John McCall84c16cf2009-11-12 03:15:40 +0000492bool CXXRecordDecl::
493FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
494 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000495 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000496 RecordDecl *BaseRecord =
497 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000498
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000499 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000500 !Path.Decls.empty();
501 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000502 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikieff7d47a2012-12-19 00:45:41 +0000503 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
504 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000505 return true;
506 }
507
508 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000509}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000510
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000511std::vector<const NamedDecl *> CXXRecordDecl::lookupDependentName(
512 const DeclarationName &Name,
513 llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
514 std::vector<const NamedDecl *> Results;
515 // Lookup in the class.
516 DeclContext::lookup_result DirectResult = lookup(Name);
517 if (!DirectResult.empty()) {
518 for (const NamedDecl *ND : DirectResult) {
519 if (Filter(ND))
520 Results.push_back(ND);
521 }
522 return Results;
523 }
524 // Perform lookup into our base classes.
525 CXXBasePaths Paths;
526 Paths.setOrigin(this);
527 if (!lookupInBases(
528 [&](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
529 return CXXRecordDecl::FindOrdinaryMemberInDependentClasses(
530 Specifier, Path, Name);
531 },
532 Paths, /*LookupInDependent=*/true))
533 return Results;
534 for (const NamedDecl *ND : Paths.front().Decls) {
535 if (Filter(ND))
536 Results.push_back(ND);
537 }
538 return Results;
539}
540
Douglas Gregor4165bd62010-03-23 23:47:56 +0000541void OverridingMethods::add(unsigned OverriddenSubobject,
542 UniqueVirtualMethod Overriding) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000543 SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
Douglas Gregor4165bd62010-03-23 23:47:56 +0000544 = Overrides[OverriddenSubobject];
545 if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
546 Overriding) == SubobjectOverrides.end())
547 SubobjectOverrides.push_back(Overriding);
548}
549
550void OverridingMethods::add(const OverridingMethods &Other) {
551 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
552 for (overriding_const_iterator M = I->second.begin(),
553 MEnd = I->second.end();
554 M != MEnd;
555 ++M)
556 add(I->first, *M);
557 }
558}
559
560void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
561 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
562 I->second.clear();
563 I->second.push_back(Overriding);
564 }
565}
566
Douglas Gregor4165bd62010-03-23 23:47:56 +0000567namespace {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000568
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000569class FinalOverriderCollector {
570 /// \brief The number of subobjects of a given class type that
571 /// occur within the class hierarchy.
572 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000573
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000574 /// \brief Overriders for each virtual base subobject.
575 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000576
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000577 CXXFinalOverriderMap FinalOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000578
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000579public:
580 ~FinalOverriderCollector();
581
582 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
583 const CXXRecordDecl *InVirtualSubobject,
584 CXXFinalOverriderMap &Overriders);
585};
586
587} // namespace
Douglas Gregor4165bd62010-03-23 23:47:56 +0000588
589void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
590 bool VirtualBase,
591 const CXXRecordDecl *InVirtualSubobject,
592 CXXFinalOverriderMap &Overriders) {
593 unsigned SubobjectNumber = 0;
594 if (!VirtualBase)
595 SubobjectNumber
596 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
597
Aaron Ballman574705e2014-03-13 15:41:46 +0000598 for (const auto &Base : RD->bases()) {
599 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000600 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
601 if (!BaseDecl->isPolymorphic())
602 continue;
603
Aaron Ballman574705e2014-03-13 15:41:46 +0000604 if (Overriders.empty() && !Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000605 // There are no other overriders of virtual member functions,
606 // so let the base class fill in our overriders for us.
607 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
608 continue;
609 }
610
611 // Collect all of the overridders from the base class subobject
612 // and merge them into the set of overridders for this class.
613 // For virtual base classes, populate or use the cached virtual
614 // overrides so that we do not walk the virtual base class (and
615 // its base classes) more than once.
616 CXXFinalOverriderMap ComputedBaseOverriders;
617 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
Aaron Ballman574705e2014-03-13 15:41:46 +0000618 if (Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000619 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramerea388a22012-05-27 22:41:08 +0000620 BaseOverriders = MyVirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000621 if (!MyVirtualOverriders) {
622 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramerea388a22012-05-27 22:41:08 +0000623
624 // Collect may cause VirtualOverriders to reallocate, invalidating the
625 // MyVirtualOverriders reference. Set BaseOverriders to the right
626 // value now.
627 BaseOverriders = MyVirtualOverriders;
628
Douglas Gregor4165bd62010-03-23 23:47:56 +0000629 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
630 }
Douglas Gregor4165bd62010-03-23 23:47:56 +0000631 } else
632 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
633
634 // Merge the overriders from this base class into our own set of
635 // overriders.
636 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
637 OMEnd = BaseOverriders->end();
638 OM != OMEnd;
639 ++OM) {
George Burgess IV00f70bd2018-03-01 05:43:23 +0000640 const CXXMethodDecl *CanonOM = OM->first->getCanonicalDecl();
Douglas Gregor4165bd62010-03-23 23:47:56 +0000641 Overriders[CanonOM].add(OM->second);
642 }
643 }
644 }
645
Aaron Ballman2b124d12014-03-13 16:36:16 +0000646 for (auto *M : RD->methods()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000647 // We only care about virtual methods.
648 if (!M->isVirtual())
649 continue;
650
George Burgess IV00f70bd2018-03-01 05:43:23 +0000651 CXXMethodDecl *CanonM = M->getCanonicalDecl();
Benjamin Krameracfa3392017-12-17 23:52:45 +0000652 using OverriddenMethodsRange =
653 llvm::iterator_range<CXXMethodDecl::method_iterator>;
654 OverriddenMethodsRange OverriddenMethods = CanonM->overridden_methods();
Douglas Gregor4165bd62010-03-23 23:47:56 +0000655
Benjamin Krameracfa3392017-12-17 23:52:45 +0000656 if (OverriddenMethods.begin() == OverriddenMethods.end()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000657 // This is a new virtual function that does not override any
658 // other virtual function. Add it to the map of virtual
659 // functions for which we are tracking overridders.
660
661 // C++ [class.virtual]p2:
662 // For convenience we say that any virtual function overrides itself.
663 Overriders[CanonM].add(SubobjectNumber,
664 UniqueVirtualMethod(CanonM, SubobjectNumber,
665 InVirtualSubobject));
666 continue;
667 }
668
669 // This virtual method overrides other virtual methods, so it does
670 // not add any new slots into the set of overriders. Instead, we
671 // replace entries in the set of overriders with the new
672 // overrider. To do so, we dig down to the original virtual
673 // functions using data recursion and update all of the methods it
674 // overrides.
Benjamin Krameracfa3392017-12-17 23:52:45 +0000675 SmallVector<OverriddenMethodsRange, 4> Stack(1, OverriddenMethods);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000676 while (!Stack.empty()) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000677 for (const CXXMethodDecl *OM : Stack.pop_back_val()) {
678 const CXXMethodDecl *CanonOM = OM->getCanonicalDecl();
Anders Carlssona2f74f32010-06-03 01:00:02 +0000679
680 // C++ [class.virtual]p2:
681 // A virtual member function C::vf of a class object S is
682 // a final overrider unless the most derived class (1.8)
683 // of which S is a base class subobject (if any) declares
684 // or inherits another member function that overrides vf.
685 //
686 // Treating this object like the most derived class, we
687 // replace any overrides from base classes with this
688 // overriding virtual function.
689 Overriders[CanonOM].replaceAll(
690 UniqueVirtualMethod(CanonM, SubobjectNumber,
691 InVirtualSubobject));
692
Benjamin Krameracfa3392017-12-17 23:52:45 +0000693 auto OverriddenMethods = CanonOM->overridden_methods();
694 if (OverriddenMethods.begin() == OverriddenMethods.end())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000695 continue;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000696
697 // Continue recursion to the methods that this virtual method
698 // overrides.
Benjamin Krameracfa3392017-12-17 23:52:45 +0000699 Stack.push_back(OverriddenMethods);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000700 }
701 }
Anders Carlssona2f74f32010-06-03 01:00:02 +0000702
703 // C++ [class.virtual]p2:
704 // For convenience we say that any virtual function overrides itself.
705 Overriders[CanonM].add(SubobjectNumber,
706 UniqueVirtualMethod(CanonM, SubobjectNumber,
707 InVirtualSubobject));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000708 }
709}
710
711FinalOverriderCollector::~FinalOverriderCollector() {
712 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
713 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
714 VO != VOEnd;
715 ++VO)
716 delete VO->second;
717}
718
719void
720CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
721 FinalOverriderCollector Collector;
Craig Topper36250ad2014-05-12 05:36:57 +0000722 Collector.Collect(this, false, nullptr, FinalOverriders);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000723
724 // Weed out any final overriders that come from virtual base class
725 // subobjects that were hidden by other subobjects along any path.
726 // This is the final-overrider variant of C++ [class.member.lookup]p10.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000727 for (auto &OM : FinalOverriders) {
728 for (auto &SO : OM.second) {
729 SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO.second;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000730 if (Overriding.size() < 2)
731 continue;
732
Benjamin Kramerece036e2015-02-11 19:09:16 +0000733 auto IsHidden = [&Overriding](const UniqueVirtualMethod &M) {
734 if (!M.InVirtualSubobject)
735 return false;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000736
737 // We have an overriding method in a virtual base class
738 // subobject (or non-virtual base class subobject thereof);
739 // determine whether there exists an other overriding method
740 // in a base class subobject that hides the virtual base class
741 // subobject.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000742 for (const UniqueVirtualMethod &OP : Overriding)
743 if (&M != &OP &&
744 OP.Method->getParent()->isVirtuallyDerivedFrom(
745 M.InVirtualSubobject))
746 return true;
747 return false;
748 };
Douglas Gregor4165bd62010-03-23 23:47:56 +0000749
Benjamin Kramerece036e2015-02-11 19:09:16 +0000750 Overriding.erase(
751 std::remove_if(Overriding.begin(), Overriding.end(), IsHidden),
752 Overriding.end());
Douglas Gregor4165bd62010-03-23 23:47:56 +0000753 }
754 }
755}
Anders Carlsson4131f002010-11-24 22:50:27 +0000756
757static void
758AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
759 CXXIndirectPrimaryBaseSet& Bases) {
760 // If the record has a virtual primary base class, add it to our set.
761 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000762 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson4131f002010-11-24 22:50:27 +0000763 Bases.insert(Layout.getPrimaryBase());
764
Aaron Ballman574705e2014-03-13 15:41:46 +0000765 for (const auto &I : RD->bases()) {
766 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000767 "Cannot get indirect primary bases for class with dependent bases.");
768
769 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000770 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000771
772 // Only bases with virtual bases participate in computing the
773 // indirect primary virtual base classes.
774 if (BaseDecl->getNumVBases())
775 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
776 }
777
778}
779
780void
781CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
782 ASTContext &Context = getASTContext();
783
784 if (!getNumVBases())
785 return;
786
Aaron Ballman574705e2014-03-13 15:41:46 +0000787 for (const auto &I : bases()) {
788 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000789 "Cannot get indirect primary bases for class with dependent bases.");
790
791 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000792 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000793
794 // Only bases with virtual bases participate in computing the
795 // indirect primary virtual base classes.
796 if (BaseDecl->getNumVBases())
797 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
798 }
799}