blob: 8615df4e790149e7e759d1ead453165435c6aa56 [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"
14#include "clang/AST/DeclCXX.h"
15#include <algorithm>
16#include <set>
17
18using namespace clang;
19
20/// \brief Computes the set of declarations referenced by these base
21/// paths.
22void CXXBasePaths::ComputeDeclsFound() {
23 assert(NumDeclsFound == 0 && !DeclsFound &&
24 "Already computed the set of declarations");
25
26 std::set<NamedDecl *> Decls;
27 for (CXXBasePaths::paths_iterator Path = begin(), PathEnd = end();
28 Path != PathEnd; ++Path)
29 Decls.insert(*Path->Decls.first);
30
31 NumDeclsFound = Decls.size();
32 DeclsFound = new NamedDecl * [NumDeclsFound];
33 std::copy(Decls.begin(), Decls.end(), DeclsFound);
34}
35
36CXXBasePaths::decl_iterator CXXBasePaths::found_decls_begin() {
37 if (NumDeclsFound == 0)
38 ComputeDeclsFound();
39 return DeclsFound;
40}
41
42CXXBasePaths::decl_iterator CXXBasePaths::found_decls_end() {
43 if (NumDeclsFound == 0)
44 ComputeDeclsFound();
45 return DeclsFound + NumDeclsFound;
46}
47
48/// isAmbiguous - Determines whether the set of paths provided is
49/// ambiguous, i.e., there are two or more paths that refer to
50/// different base class subobjects of the same type. BaseType must be
51/// an unqualified, canonical class type.
52bool CXXBasePaths::isAmbiguous(QualType BaseType) {
John McCallb692a092009-10-22 20:10:53 +000053 assert(BaseType.isCanonical() && "Base type must be the canonical type");
Douglas Gregor36d1b142009-10-06 17:59:45 +000054 assert(BaseType.hasQualifiers() == 0 && "Base type must be unqualified");
55 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
56 return Subobjects.second + (Subobjects.first? 1 : 0) > 1;
57}
58
59/// clear - Clear out all prior path information.
60void CXXBasePaths::clear() {
61 Paths.clear();
62 ClassSubobjects.clear();
63 ScratchPath.clear();
64 DetectedVirtual = 0;
65}
66
67/// @brief Swaps the contents of this CXXBasePaths structure with the
68/// contents of Other.
69void CXXBasePaths::swap(CXXBasePaths &Other) {
70 std::swap(Origin, Other.Origin);
71 Paths.swap(Other.Paths);
72 ClassSubobjects.swap(Other.ClassSubobjects);
73 std::swap(FindAmbiguities, Other.FindAmbiguities);
74 std::swap(RecordPaths, Other.RecordPaths);
75 std::swap(DetectVirtual, Other.DetectVirtual);
76 std::swap(DetectedVirtual, Other.DetectedVirtual);
77}
78
John McCall84c16cf2009-11-12 03:15:40 +000079bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000080 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
81 /*DetectVirtual=*/false);
82 return isDerivedFrom(Base, Paths);
83}
84
John McCall84c16cf2009-11-12 03:15:40 +000085bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000086 if (getCanonicalDecl() == Base->getCanonicalDecl())
87 return false;
88
John McCall84c16cf2009-11-12 03:15:40 +000089 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
Douglas Gregor36d1b142009-10-06 17:59:45 +000090 return lookupInBases(&FindBaseClass, Base->getCanonicalDecl(), Paths);
91}
92
John McCallddabf1a2009-12-08 07:42:38 +000093static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) {
94 // OpaqueTarget is a CXXRecordDecl*.
95 return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget;
96}
97
98bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
99 return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl());
100}
101
102bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches,
103 void *OpaqueData,
104 bool AllowShortCircuit) const {
John McCallddabf1a2009-12-08 07:42:38 +0000105 llvm::SmallVector<const CXXRecordDecl*, 8> Queue;
106
107 const CXXRecordDecl *Record = this;
108 bool AllMatches = true;
109 while (true) {
110 for (CXXRecordDecl::base_class_const_iterator
111 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
112 const RecordType *Ty = I->getType()->getAs<RecordType>();
113 if (!Ty) {
114 if (AllowShortCircuit) return false;
115 AllMatches = false;
116 continue;
117 }
118
Anders Carlssonf9812782009-12-09 04:26:02 +0000119 CXXRecordDecl *Base =
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000120 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
John McCallddabf1a2009-12-08 07:42:38 +0000121 if (!Base) {
122 if (AllowShortCircuit) return false;
123 AllMatches = false;
124 continue;
125 }
126
Anders Carlssonf9812782009-12-09 04:26:02 +0000127 Queue.push_back(Base);
128 if (!BaseMatches(Base, OpaqueData)) {
John McCallddabf1a2009-12-08 07:42:38 +0000129 if (AllowShortCircuit) return false;
130 AllMatches = false;
131 continue;
132 }
133 }
134
135 if (Queue.empty()) break;
136 Record = Queue.back(); // not actually a queue.
137 Queue.pop_back();
138 }
139
140 return AllMatches;
141}
142
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000143bool CXXBasePaths::lookupInBases(ASTContext &Context,
144 const CXXRecordDecl *Record,
145 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
146 void *UserData) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000147 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000148
John McCall553c0792010-01-23 00:46:32 +0000149 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000150 AccessSpecifier AccessToHere = ScratchPath.Access;
151 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000152
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000153 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(),
154 BaseSpecEnd = Record->bases_end();
155 BaseSpec != BaseSpecEnd;
156 ++BaseSpec) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000157 // Find the record of the base class subobjects for this type.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000158 QualType BaseType = Context.getCanonicalType(BaseSpec->getType())
159 .getUnqualifiedType();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000160
161 // C++ [temp.dep]p3:
162 // In the definition of a class template or a member of a class template,
163 // if a base class of the class template depends on a template-parameter,
164 // the base class scope is not examined during unqualified name lookup
165 // either at the point of definition of the class template or member or
166 // during an instantiation of the class tem- plate or member.
167 if (BaseType->isDependentType())
168 continue;
169
170 // Determine whether we need to visit this base class at all,
171 // updating the count of subobjects appropriately.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000172 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000173 bool VisitBase = true;
174 bool SetVirtual = false;
175 if (BaseSpec->isVirtual()) {
176 VisitBase = !Subobjects.first;
177 Subobjects.first = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000178 if (isDetectingVirtual() && DetectedVirtual == 0) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000179 // If this is the first virtual we find, remember it. If it turns out
180 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000181 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000182 SetVirtual = true;
183 }
184 } else
185 ++Subobjects.second;
186
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000187 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000188 // Add this base specifier to the current path.
189 CXXBasePathElement Element;
190 Element.Base = &*BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000191 Element.Class = Record;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000192 if (BaseSpec->isVirtual())
193 Element.SubobjectNumber = 0;
194 else
195 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000196 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000197
John McCall553c0792010-01-23 00:46:32 +0000198 // Calculate the "top-down" access to this base class.
199 // The spec actually describes this bottom-up, but top-down is
200 // equivalent because the definition works out as follows:
201 // 1. Write down the access along each step in the inheritance
202 // chain, followed by the access of the decl itself.
203 // For example, in
204 // class A { public: int foo; };
205 // class B : protected A {};
206 // class C : public B {};
207 // class D : private C {};
208 // we would write:
209 // private public protected public
210 // 2. If 'private' appears anywhere except far-left, access is denied.
211 // 3. Otherwise, overall access is determined by the most restrictive
212 // access in the sequence.
213 if (IsFirstStep)
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000214 ScratchPath.Access = BaseSpec->getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000215 else
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000216 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
217 BaseSpec->getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000218 }
John McCall6f891402010-02-09 00:57:12 +0000219
220 // Track whether there's a path involving this specific base.
221 bool FoundPathThroughBase = false;
222
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000223 if (BaseMatches(BaseSpec, ScratchPath, UserData)) {
John McCall553c0792010-01-23 00:46:32 +0000224 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000225 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000226 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000227 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000228 Paths.push_back(ScratchPath);
229 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000230 // We found a path and we don't care about ambiguities;
231 // return immediately.
232 return FoundPath;
233 }
234 } else if (VisitBase) {
235 CXXRecordDecl *BaseRecord
236 = cast<CXXRecordDecl>(BaseSpec->getType()->getAs<RecordType>()
237 ->getDecl());
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000238 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000239 // C++ [class.member.lookup]p2:
240 // A member name f in one sub-object B hides a member name f in
241 // a sub-object A if A is a base class sub-object of B. Any
242 // declarations that are so hidden are eliminated from
243 // consideration.
244
245 // There is a path to a base class that meets the criteria. If we're
246 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000247 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000248 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000249 return FoundPath;
250 }
251 }
252
253 // Pop this base specifier off the current path (if we're
254 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000255 if (isRecordingPaths()) {
256 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000257 }
258
Douglas Gregor36d1b142009-10-06 17:59:45 +0000259 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000260 if (SetVirtual && !FoundPathThroughBase) {
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000261 DetectedVirtual = 0;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000262 }
263 }
John McCall553c0792010-01-23 00:46:32 +0000264
265 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000266 ScratchPath.Access = AccessToHere;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000267
268 return FoundPath;
269}
270
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000271bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches,
272 void *UserData,
273 CXXBasePaths &Paths) const {
274 return Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData);
275}
276
John McCall84c16cf2009-11-12 03:15:40 +0000277bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000278 CXXBasePath &Path,
279 void *BaseRecord) {
280 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
281 "User data for FindBaseClass is not canonical!");
282 return Specifier->getType()->getAs<RecordType>()->getDecl()
283 ->getCanonicalDecl() == BaseRecord;
284}
285
John McCall84c16cf2009-11-12 03:15:40 +0000286bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000287 CXXBasePath &Path,
288 void *Name) {
289 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
290
291 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
292 for (Path.Decls = BaseRecord->lookup(N);
293 Path.Decls.first != Path.Decls.second;
294 ++Path.Decls.first) {
295 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag))
296 return true;
297 }
298
299 return false;
300}
301
John McCall84c16cf2009-11-12 03:15:40 +0000302bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000303 CXXBasePath &Path,
304 void *Name) {
305 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
306
307 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
308 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
309 for (Path.Decls = BaseRecord->lookup(N);
310 Path.Decls.first != Path.Decls.second;
311 ++Path.Decls.first) {
312 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS))
313 return true;
314 }
315
316 return false;
317}
318
John McCall84c16cf2009-11-12 03:15:40 +0000319bool CXXRecordDecl::
320FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
321 CXXBasePath &Path,
322 void *Name) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000323 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
324
325 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
326 for (Path.Decls = BaseRecord->lookup(N);
327 Path.Decls.first != Path.Decls.second;
328 ++Path.Decls.first) {
329 // FIXME: Refactor the "is it a nested-name-specifier?" check
330 if (isa<TypedefDecl>(*Path.Decls.first) ||
331 (*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag))
332 return true;
333 }
334
335 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000336}