Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1 | //===------ 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 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | /// \brief Computes the set of declarations referenced by these base |
| 21 | /// paths. |
| 22 | void 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 | |
| 36 | CXXBasePaths::decl_iterator CXXBasePaths::found_decls_begin() { |
| 37 | if (NumDeclsFound == 0) |
| 38 | ComputeDeclsFound(); |
| 39 | return DeclsFound; |
| 40 | } |
| 41 | |
| 42 | CXXBasePaths::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. |
| 52 | bool CXXBasePaths::isAmbiguous(QualType BaseType) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 53 | assert(BaseType.isCanonical() && "Base type must be the canonical type"); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 54 | 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. |
| 60 | void 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. |
| 69 | void 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 McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 79 | bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base) const { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 80 | CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 81 | /*DetectVirtual=*/false); |
| 82 | return isDerivedFrom(Base, Paths); |
| 83 | } |
| 84 | |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 85 | bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 86 | if (getCanonicalDecl() == Base->getCanonicalDecl()) |
| 87 | return false; |
| 88 | |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 89 | Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 90 | return lookupInBases(&FindBaseClass, Base->getCanonicalDecl(), Paths); |
| 91 | } |
| 92 | |
John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 93 | static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) { |
| 94 | // OpaqueTarget is a CXXRecordDecl*. |
| 95 | return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget; |
| 96 | } |
| 97 | |
| 98 | bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const { |
| 99 | return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl()); |
| 100 | } |
| 101 | |
| 102 | bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches, |
| 103 | void *OpaqueData, |
| 104 | bool AllowShortCircuit) const { |
John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 105 | 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 Carlsson | f981278 | 2009-12-09 04:26:02 +0000 | [diff] [blame] | 119 | CXXRecordDecl *Base = |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 120 | cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition()); |
John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 121 | if (!Base) { |
| 122 | if (AllowShortCircuit) return false; |
| 123 | AllMatches = false; |
| 124 | continue; |
| 125 | } |
| 126 | |
Anders Carlsson | f981278 | 2009-12-09 04:26:02 +0000 | [diff] [blame] | 127 | Queue.push_back(Base); |
| 128 | if (!BaseMatches(Base, OpaqueData)) { |
John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 129 | 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 Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 143 | bool CXXBasePaths::lookupInBases(ASTContext &Context, |
| 144 | const CXXRecordDecl *Record, |
| 145 | CXXRecordDecl::BaseMatchesCallback *BaseMatches, |
| 146 | void *UserData) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 147 | bool FoundPath = false; |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 148 | |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 149 | // The access of the path down to this record. |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 150 | AccessSpecifier AccessToHere = ScratchPath.Access; |
| 151 | bool IsFirstStep = ScratchPath.empty(); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 153 | for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(), |
| 154 | BaseSpecEnd = Record->bases_end(); |
| 155 | BaseSpec != BaseSpecEnd; |
| 156 | ++BaseSpec) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 157 | // Find the record of the base class subobjects for this type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 158 | QualType BaseType = Context.getCanonicalType(BaseSpec->getType()) |
| 159 | .getUnqualifiedType(); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 160 | |
| 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 Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 172 | std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 173 | bool VisitBase = true; |
| 174 | bool SetVirtual = false; |
| 175 | if (BaseSpec->isVirtual()) { |
| 176 | VisitBase = !Subobjects.first; |
| 177 | Subobjects.first = true; |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 178 | if (isDetectingVirtual() && DetectedVirtual == 0) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 179 | // 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 Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 181 | DetectedVirtual = BaseType->getAs<RecordType>(); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 182 | SetVirtual = true; |
| 183 | } |
| 184 | } else |
| 185 | ++Subobjects.second; |
| 186 | |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 187 | if (isRecordingPaths()) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 188 | // Add this base specifier to the current path. |
| 189 | CXXBasePathElement Element; |
| 190 | Element.Base = &*BaseSpec; |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 191 | Element.Class = Record; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 192 | if (BaseSpec->isVirtual()) |
| 193 | Element.SubobjectNumber = 0; |
| 194 | else |
| 195 | Element.SubobjectNumber = Subobjects.second; |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 196 | ScratchPath.push_back(Element); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 197 | |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 198 | // 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 Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 214 | ScratchPath.Access = BaseSpec->getAccessSpecifier(); |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 215 | else |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 216 | ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere, |
| 217 | BaseSpec->getAccessSpecifier()); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 218 | } |
John McCall | 6f89140 | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 219 | |
| 220 | // Track whether there's a path involving this specific base. |
| 221 | bool FoundPathThroughBase = false; |
| 222 | |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 223 | if (BaseMatches(BaseSpec, ScratchPath, UserData)) { |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 224 | // We've found a path that terminates at this base. |
John McCall | 6f89140 | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 225 | FoundPath = FoundPathThroughBase = true; |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 226 | if (isRecordingPaths()) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 227 | // We have a path. Make a copy of it before moving on. |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 228 | Paths.push_back(ScratchPath); |
| 229 | } else if (!isFindingAmbiguities()) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 230 | // 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 Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 238 | if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 239 | // 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 McCall | 6f89140 | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 247 | FoundPath = FoundPathThroughBase = true; |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 248 | if (!isFindingAmbiguities()) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 249 | return FoundPath; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Pop this base specifier off the current path (if we're |
| 254 | // collecting paths). |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 255 | if (isRecordingPaths()) { |
| 256 | ScratchPath.pop_back(); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 259 | // If we set a virtual earlier, and this isn't a path, forget it again. |
John McCall | 6f89140 | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 260 | if (SetVirtual && !FoundPathThroughBase) { |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 261 | DetectedVirtual = 0; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 264 | |
| 265 | // Reset the scratch path access. |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 266 | ScratchPath.Access = AccessToHere; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 267 | |
| 268 | return FoundPath; |
| 269 | } |
| 270 | |
Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame^] | 271 | bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches, |
| 272 | void *UserData, |
| 273 | CXXBasePaths &Paths) const { |
| 274 | return Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData); |
| 275 | } |
| 276 | |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 277 | bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 278 | 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 McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 286 | bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 287 | 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 McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 302 | bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 303 | 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 McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 319 | bool CXXRecordDecl:: |
| 320 | FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, |
| 321 | CXXBasePath &Path, |
| 322 | void *Name) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 323 | 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 Stump | 512c5b7 | 2009-10-06 23:38:59 +0000 | [diff] [blame] | 336 | } |