blob: 70f8ee4bca5e496303b59fc1c3331bdc93436f93 [file] [log] [blame]
Douglas Gregora8f32e02009-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 McCall467b27b2009-10-22 20:10:53 +000053 assert(BaseType.isCanonical() && "Base type must be the canonical type");
Douglas Gregora8f32e02009-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 McCallaf8e6ed2009-11-12 03:15:40 +000079bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base) const {
Douglas Gregora8f32e02009-10-06 17:59:45 +000080 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
81 /*DetectVirtual=*/false);
82 return isDerivedFrom(Base, Paths);
83}
84
John McCallaf8e6ed2009-11-12 03:15:40 +000085bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const {
Douglas Gregora8f32e02009-10-06 17:59:45 +000086 if (getCanonicalDecl() == Base->getCanonicalDecl())
87 return false;
88
John McCallaf8e6ed2009-11-12 03:15:40 +000089 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
Douglas Gregora8f32e02009-10-06 17:59:45 +000090 return lookupInBases(&FindBaseClass, Base->getCanonicalDecl(), Paths);
91}
92
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +000093bool CXXRecordDecl::isVirtuallyDerivedFrom(CXXRecordDecl *Base) const {
94 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
95 /*DetectVirtual=*/false);
96
97 if (getCanonicalDecl() == Base->getCanonicalDecl())
98 return false;
99
100 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
101 return lookupInBases(&FindVirtualBaseClass, Base->getCanonicalDecl(), Paths);
102}
103
John McCalle8174bc2009-12-08 07:42:38 +0000104static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) {
105 // OpaqueTarget is a CXXRecordDecl*.
106 return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget;
107}
108
109bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
110 return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl());
111}
112
113bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches,
114 void *OpaqueData,
115 bool AllowShortCircuit) const {
John McCalle8174bc2009-12-08 07:42:38 +0000116 llvm::SmallVector<const CXXRecordDecl*, 8> Queue;
117
118 const CXXRecordDecl *Record = this;
119 bool AllMatches = true;
120 while (true) {
121 for (CXXRecordDecl::base_class_const_iterator
122 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
123 const RecordType *Ty = I->getType()->getAs<RecordType>();
124 if (!Ty) {
125 if (AllowShortCircuit) return false;
126 AllMatches = false;
127 continue;
128 }
129
Anders Carlssonca910e82009-12-09 04:26:02 +0000130 CXXRecordDecl *Base =
Douglas Gregor952b0172010-02-11 01:04:33 +0000131 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
John McCalle8174bc2009-12-08 07:42:38 +0000132 if (!Base) {
133 if (AllowShortCircuit) return false;
134 AllMatches = false;
135 continue;
136 }
137
Anders Carlssonca910e82009-12-09 04:26:02 +0000138 Queue.push_back(Base);
139 if (!BaseMatches(Base, OpaqueData)) {
John McCalle8174bc2009-12-08 07:42:38 +0000140 if (AllowShortCircuit) return false;
141 AllMatches = false;
142 continue;
143 }
144 }
145
146 if (Queue.empty()) break;
147 Record = Queue.back(); // not actually a queue.
148 Queue.pop_back();
149 }
150
151 return AllMatches;
152}
153
Douglas Gregor89b77022010-03-03 02:18:00 +0000154bool CXXBasePaths::lookupInBases(ASTContext &Context,
155 const CXXRecordDecl *Record,
156 CXXRecordDecl::BaseMatchesCallback *BaseMatches,
157 void *UserData) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000158 bool FoundPath = false;
John McCall46460a62010-01-20 21:53:11 +0000159
John McCall92f88312010-01-23 00:46:32 +0000160 // The access of the path down to this record.
Douglas Gregor89b77022010-03-03 02:18:00 +0000161 AccessSpecifier AccessToHere = ScratchPath.Access;
162 bool IsFirstStep = ScratchPath.empty();
John McCall92f88312010-01-23 00:46:32 +0000163
Douglas Gregor89b77022010-03-03 02:18:00 +0000164 for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(),
165 BaseSpecEnd = Record->bases_end();
166 BaseSpec != BaseSpecEnd;
167 ++BaseSpec) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000168 // Find the record of the base class subobjects for this type.
Douglas Gregora4923eb2009-11-16 21:35:15 +0000169 QualType BaseType = Context.getCanonicalType(BaseSpec->getType())
170 .getUnqualifiedType();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000171
172 // C++ [temp.dep]p3:
173 // In the definition of a class template or a member of a class template,
174 // if a base class of the class template depends on a template-parameter,
175 // the base class scope is not examined during unqualified name lookup
176 // either at the point of definition of the class template or member or
177 // during an instantiation of the class tem- plate or member.
178 if (BaseType->isDependentType())
179 continue;
180
181 // Determine whether we need to visit this base class at all,
182 // updating the count of subobjects appropriately.
Douglas Gregor89b77022010-03-03 02:18:00 +0000183 std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType];
Douglas Gregora8f32e02009-10-06 17:59:45 +0000184 bool VisitBase = true;
185 bool SetVirtual = false;
186 if (BaseSpec->isVirtual()) {
187 VisitBase = !Subobjects.first;
188 Subobjects.first = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000189 if (isDetectingVirtual() && DetectedVirtual == 0) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000190 // If this is the first virtual we find, remember it. If it turns out
191 // there is no base path here, we'll reset it later.
Douglas Gregor89b77022010-03-03 02:18:00 +0000192 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregora8f32e02009-10-06 17:59:45 +0000193 SetVirtual = true;
194 }
195 } else
196 ++Subobjects.second;
197
Douglas Gregor89b77022010-03-03 02:18:00 +0000198 if (isRecordingPaths()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000199 // Add this base specifier to the current path.
200 CXXBasePathElement Element;
201 Element.Base = &*BaseSpec;
Douglas Gregor89b77022010-03-03 02:18:00 +0000202 Element.Class = Record;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000203 if (BaseSpec->isVirtual())
204 Element.SubobjectNumber = 0;
205 else
206 Element.SubobjectNumber = Subobjects.second;
Douglas Gregor89b77022010-03-03 02:18:00 +0000207 ScratchPath.push_back(Element);
John McCall46460a62010-01-20 21:53:11 +0000208
John McCall92f88312010-01-23 00:46:32 +0000209 // Calculate the "top-down" access to this base class.
210 // The spec actually describes this bottom-up, but top-down is
211 // equivalent because the definition works out as follows:
212 // 1. Write down the access along each step in the inheritance
213 // chain, followed by the access of the decl itself.
214 // For example, in
215 // class A { public: int foo; };
216 // class B : protected A {};
217 // class C : public B {};
218 // class D : private C {};
219 // we would write:
220 // private public protected public
221 // 2. If 'private' appears anywhere except far-left, access is denied.
222 // 3. Otherwise, overall access is determined by the most restrictive
223 // access in the sequence.
224 if (IsFirstStep)
Douglas Gregor89b77022010-03-03 02:18:00 +0000225 ScratchPath.Access = BaseSpec->getAccessSpecifier();
John McCall92f88312010-01-23 00:46:32 +0000226 else
Douglas Gregor89b77022010-03-03 02:18:00 +0000227 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
228 BaseSpec->getAccessSpecifier());
Douglas Gregora8f32e02009-10-06 17:59:45 +0000229 }
John McCalled814cc2010-02-09 00:57:12 +0000230
231 // Track whether there's a path involving this specific base.
232 bool FoundPathThroughBase = false;
233
Douglas Gregor89b77022010-03-03 02:18:00 +0000234 if (BaseMatches(BaseSpec, ScratchPath, UserData)) {
John McCall92f88312010-01-23 00:46:32 +0000235 // We've found a path that terminates at this base.
John McCalled814cc2010-02-09 00:57:12 +0000236 FoundPath = FoundPathThroughBase = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000237 if (isRecordingPaths()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000238 // We have a path. Make a copy of it before moving on.
Douglas Gregor89b77022010-03-03 02:18:00 +0000239 Paths.push_back(ScratchPath);
240 } else if (!isFindingAmbiguities()) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000241 // We found a path and we don't care about ambiguities;
242 // return immediately.
243 return FoundPath;
244 }
245 } else if (VisitBase) {
246 CXXRecordDecl *BaseRecord
247 = cast<CXXRecordDecl>(BaseSpec->getType()->getAs<RecordType>()
248 ->getDecl());
Douglas Gregor89b77022010-03-03 02:18:00 +0000249 if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000250 // C++ [class.member.lookup]p2:
251 // A member name f in one sub-object B hides a member name f in
252 // a sub-object A if A is a base class sub-object of B. Any
253 // declarations that are so hidden are eliminated from
254 // consideration.
255
256 // There is a path to a base class that meets the criteria. If we're
257 // not collecting paths or finding ambiguities, we're done.
John McCalled814cc2010-02-09 00:57:12 +0000258 FoundPath = FoundPathThroughBase = true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000259 if (!isFindingAmbiguities())
Douglas Gregora8f32e02009-10-06 17:59:45 +0000260 return FoundPath;
261 }
262 }
263
264 // Pop this base specifier off the current path (if we're
265 // collecting paths).
Douglas Gregor89b77022010-03-03 02:18:00 +0000266 if (isRecordingPaths()) {
267 ScratchPath.pop_back();
John McCall46460a62010-01-20 21:53:11 +0000268 }
269
Douglas Gregora8f32e02009-10-06 17:59:45 +0000270 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCalled814cc2010-02-09 00:57:12 +0000271 if (SetVirtual && !FoundPathThroughBase) {
Douglas Gregor89b77022010-03-03 02:18:00 +0000272 DetectedVirtual = 0;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000273 }
274 }
John McCall92f88312010-01-23 00:46:32 +0000275
276 // Reset the scratch path access.
Douglas Gregor89b77022010-03-03 02:18:00 +0000277 ScratchPath.Access = AccessToHere;
Douglas Gregora8f32e02009-10-06 17:59:45 +0000278
279 return FoundPath;
280}
281
Douglas Gregor89b77022010-03-03 02:18:00 +0000282bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches,
283 void *UserData,
284 CXXBasePaths &Paths) const {
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000285 // If we didn't find anything, report that.
286 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData))
287 return false;
288
289 // If we're not recording paths or we won't ever find ambiguities,
290 // we're done.
291 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
292 return true;
293
294 // C++ [class.member.lookup]p6:
295 // When virtual base classes are used, a hidden declaration can be
296 // reached along a path through the sub-object lattice that does
297 // not pass through the hiding declaration. This is not an
298 // ambiguity. The identical use with nonvirtual base classes is an
299 // ambiguity; in that case there is no unique instance of the name
300 // that hides all the others.
301 //
302 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
303 // way to make it any faster.
304 for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end();
305 P != PEnd; /* increment in loop */) {
306 bool Hidden = false;
307
308 for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end();
309 PE != PEEnd && !Hidden; ++PE) {
310 if (PE->Base->isVirtual()) {
311 CXXRecordDecl *VBase = 0;
312 if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>())
313 VBase = cast<CXXRecordDecl>(Record->getDecl());
314 if (!VBase)
315 break;
316
317 // The declaration(s) we found along this path were found in a
318 // subobject of a virtual base. Check whether this virtual
319 // base is a subobject of any other path; if so, then the
320 // declaration in this path are hidden by that patch.
321 for (CXXBasePaths::paths_iterator HidingP = Paths.begin(),
322 HidingPEnd = Paths.end();
323 HidingP != HidingPEnd;
324 ++HidingP) {
325 CXXRecordDecl *HidingClass = 0;
326 if (const RecordType *Record
327 = HidingP->back().Base->getType()->getAs<RecordType>())
328 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
329 if (!HidingClass)
330 break;
331
332 if (HidingClass->isVirtuallyDerivedFrom(VBase)) {
333 Hidden = true;
334 break;
335 }
336 }
337 }
338 }
339
340 if (Hidden)
341 P = Paths.Paths.erase(P);
342 else
343 ++P;
344 }
345
346 return true;
Douglas Gregor89b77022010-03-03 02:18:00 +0000347}
348
John McCallaf8e6ed2009-11-12 03:15:40 +0000349bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000350 CXXBasePath &Path,
351 void *BaseRecord) {
352 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
353 "User data for FindBaseClass is not canonical!");
354 return Specifier->getType()->getAs<RecordType>()->getDecl()
355 ->getCanonicalDecl() == BaseRecord;
356}
357
Douglas Gregor4e6ba4b2010-03-03 04:38:46 +0000358bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
359 CXXBasePath &Path,
360 void *BaseRecord) {
361 assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord &&
362 "User data for FindBaseClass is not canonical!");
363 return Specifier->isVirtual() &&
364 Specifier->getType()->getAs<RecordType>()->getDecl()
365 ->getCanonicalDecl() == BaseRecord;
366}
367
John McCallaf8e6ed2009-11-12 03:15:40 +0000368bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000369 CXXBasePath &Path,
370 void *Name) {
371 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
372
373 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
374 for (Path.Decls = BaseRecord->lookup(N);
375 Path.Decls.first != Path.Decls.second;
376 ++Path.Decls.first) {
377 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag))
378 return true;
379 }
380
381 return false;
382}
383
John McCallaf8e6ed2009-11-12 03:15:40 +0000384bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
Douglas Gregora8f32e02009-10-06 17:59:45 +0000385 CXXBasePath &Path,
386 void *Name) {
387 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
388
389 const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member;
390 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
391 for (Path.Decls = BaseRecord->lookup(N);
392 Path.Decls.first != Path.Decls.second;
393 ++Path.Decls.first) {
394 if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS))
395 return true;
396 }
397
398 return false;
399}
400
John McCallaf8e6ed2009-11-12 03:15:40 +0000401bool CXXRecordDecl::
402FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
403 CXXBasePath &Path,
404 void *Name) {
Douglas Gregora8f32e02009-10-06 17:59:45 +0000405 RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
406
407 DeclarationName N = DeclarationName::getFromOpaquePtr(Name);
408 for (Path.Decls = BaseRecord->lookup(N);
409 Path.Decls.first != Path.Decls.second;
410 ++Path.Decls.first) {
411 // FIXME: Refactor the "is it a nested-name-specifier?" check
412 if (isa<TypedefDecl>(*Path.Decls.first) ||
413 (*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag))
414 return true;
415 }
416
417 return false;
Mike Stump82109bd2009-10-06 23:38:59 +0000418}