Douglas Gregor | a8f32e0 | 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 | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 53 | assert(BaseType.isCanonical() && "Base type must be the canonical type"); |
Douglas Gregor | a8f32e0 | 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(); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame^] | 64 | ScratchAccess.clear(); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 65 | DetectedVirtual = 0; |
| 66 | } |
| 67 | |
| 68 | /// @brief Swaps the contents of this CXXBasePaths structure with the |
| 69 | /// contents of Other. |
| 70 | void CXXBasePaths::swap(CXXBasePaths &Other) { |
| 71 | std::swap(Origin, Other.Origin); |
| 72 | Paths.swap(Other.Paths); |
| 73 | ClassSubobjects.swap(Other.ClassSubobjects); |
| 74 | std::swap(FindAmbiguities, Other.FindAmbiguities); |
| 75 | std::swap(RecordPaths, Other.RecordPaths); |
| 76 | std::swap(DetectVirtual, Other.DetectVirtual); |
| 77 | std::swap(DetectedVirtual, Other.DetectedVirtual); |
| 78 | } |
| 79 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 80 | bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base) const { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 81 | CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 82 | /*DetectVirtual=*/false); |
| 83 | return isDerivedFrom(Base, Paths); |
| 84 | } |
| 85 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 86 | bool CXXRecordDecl::isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 87 | if (getCanonicalDecl() == Base->getCanonicalDecl()) |
| 88 | return false; |
| 89 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 90 | Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 91 | return lookupInBases(&FindBaseClass, Base->getCanonicalDecl(), Paths); |
| 92 | } |
| 93 | |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 94 | static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) { |
| 95 | // OpaqueTarget is a CXXRecordDecl*. |
| 96 | return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget; |
| 97 | } |
| 98 | |
| 99 | bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const { |
| 100 | return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl()); |
| 101 | } |
| 102 | |
| 103 | bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches, |
| 104 | void *OpaqueData, |
| 105 | bool AllowShortCircuit) const { |
| 106 | ASTContext &Context = getASTContext(); |
| 107 | llvm::SmallVector<const CXXRecordDecl*, 8> Queue; |
| 108 | |
| 109 | const CXXRecordDecl *Record = this; |
| 110 | bool AllMatches = true; |
| 111 | while (true) { |
| 112 | for (CXXRecordDecl::base_class_const_iterator |
| 113 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 114 | const RecordType *Ty = I->getType()->getAs<RecordType>(); |
| 115 | if (!Ty) { |
| 116 | if (AllowShortCircuit) return false; |
| 117 | AllMatches = false; |
| 118 | continue; |
| 119 | } |
| 120 | |
Anders Carlsson | ca910e8 | 2009-12-09 04:26:02 +0000 | [diff] [blame] | 121 | CXXRecordDecl *Base = |
| 122 | cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition(Context)); |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 123 | if (!Base) { |
| 124 | if (AllowShortCircuit) return false; |
| 125 | AllMatches = false; |
| 126 | continue; |
| 127 | } |
| 128 | |
Anders Carlsson | ca910e8 | 2009-12-09 04:26:02 +0000 | [diff] [blame] | 129 | Queue.push_back(Base); |
| 130 | if (!BaseMatches(Base, OpaqueData)) { |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 131 | if (AllowShortCircuit) return false; |
| 132 | AllMatches = false; |
| 133 | continue; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (Queue.empty()) break; |
| 138 | Record = Queue.back(); // not actually a queue. |
| 139 | Queue.pop_back(); |
| 140 | } |
| 141 | |
| 142 | return AllMatches; |
| 143 | } |
| 144 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 145 | bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches, |
| 146 | void *UserData, |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 147 | CXXBasePaths &Paths) const { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 148 | bool FoundPath = false; |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame^] | 149 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 150 | ASTContext &Context = getASTContext(); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 151 | for (base_class_const_iterator BaseSpec = bases_begin(), |
| 152 | BaseSpecEnd = bases_end(); BaseSpec != BaseSpecEnd; ++BaseSpec) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 153 | // Find the record of the base class subobjects for this type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 154 | QualType BaseType = Context.getCanonicalType(BaseSpec->getType()) |
| 155 | .getUnqualifiedType(); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 156 | |
| 157 | // C++ [temp.dep]p3: |
| 158 | // In the definition of a class template or a member of a class template, |
| 159 | // if a base class of the class template depends on a template-parameter, |
| 160 | // the base class scope is not examined during unqualified name lookup |
| 161 | // either at the point of definition of the class template or member or |
| 162 | // during an instantiation of the class tem- plate or member. |
| 163 | if (BaseType->isDependentType()) |
| 164 | continue; |
| 165 | |
| 166 | // Determine whether we need to visit this base class at all, |
| 167 | // updating the count of subobjects appropriately. |
| 168 | std::pair<bool, unsigned>& Subobjects = Paths.ClassSubobjects[BaseType]; |
| 169 | bool VisitBase = true; |
| 170 | bool SetVirtual = false; |
| 171 | if (BaseSpec->isVirtual()) { |
| 172 | VisitBase = !Subobjects.first; |
| 173 | Subobjects.first = true; |
| 174 | if (Paths.isDetectingVirtual() && Paths.DetectedVirtual == 0) { |
| 175 | // If this is the first virtual we find, remember it. If it turns out |
| 176 | // there is no base path here, we'll reset it later. |
| 177 | Paths.DetectedVirtual = BaseType->getAs<RecordType>(); |
| 178 | SetVirtual = true; |
| 179 | } |
| 180 | } else |
| 181 | ++Subobjects.second; |
| 182 | |
| 183 | if (Paths.isRecordingPaths()) { |
| 184 | // Add this base specifier to the current path. |
| 185 | CXXBasePathElement Element; |
| 186 | Element.Base = &*BaseSpec; |
| 187 | Element.Class = this; |
| 188 | if (BaseSpec->isVirtual()) |
| 189 | Element.SubobjectNumber = 0; |
| 190 | else |
| 191 | Element.SubobjectNumber = Subobjects.second; |
| 192 | Paths.ScratchPath.push_back(Element); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame^] | 193 | |
| 194 | // C++0x [class.access.base]p1 (paraphrased): |
| 195 | // The access of a member of a base class is the less permissive |
| 196 | // of its access within the base class and the access of the base |
| 197 | // class within the derived class. |
| 198 | // We're just calculating the access along the path, so we ignore |
| 199 | // the access specifiers of whatever decls we've found. |
| 200 | AccessSpecifier PathAccess = Paths.ScratchPath.Access; |
| 201 | Paths.ScratchAccess.push_back(PathAccess); |
| 202 | Paths.ScratchPath.Access |
| 203 | = std::max(PathAccess, BaseSpec->getAccessSpecifier()); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if (BaseMatches(BaseSpec, Paths.ScratchPath, UserData)) { |
| 207 | // We've found a path that terminates that this base. |
| 208 | FoundPath = true; |
| 209 | if (Paths.isRecordingPaths()) { |
| 210 | // We have a path. Make a copy of it before moving on. |
| 211 | Paths.Paths.push_back(Paths.ScratchPath); |
| 212 | } else if (!Paths.isFindingAmbiguities()) { |
| 213 | // We found a path and we don't care about ambiguities; |
| 214 | // return immediately. |
| 215 | return FoundPath; |
| 216 | } |
| 217 | } else if (VisitBase) { |
| 218 | CXXRecordDecl *BaseRecord |
| 219 | = cast<CXXRecordDecl>(BaseSpec->getType()->getAs<RecordType>() |
| 220 | ->getDecl()); |
| 221 | if (BaseRecord->lookupInBases(BaseMatches, UserData, Paths)) { |
| 222 | // C++ [class.member.lookup]p2: |
| 223 | // A member name f in one sub-object B hides a member name f in |
| 224 | // a sub-object A if A is a base class sub-object of B. Any |
| 225 | // declarations that are so hidden are eliminated from |
| 226 | // consideration. |
| 227 | |
| 228 | // There is a path to a base class that meets the criteria. If we're |
| 229 | // not collecting paths or finding ambiguities, we're done. |
| 230 | FoundPath = true; |
| 231 | if (!Paths.isFindingAmbiguities()) |
| 232 | return FoundPath; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Pop this base specifier off the current path (if we're |
| 237 | // collecting paths). |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame^] | 238 | if (Paths.isRecordingPaths()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 239 | Paths.ScratchPath.pop_back(); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame^] | 240 | Paths.ScratchPath.Access = Paths.ScratchAccess.back(); |
| 241 | Paths.ScratchAccess.pop_back(); |
| 242 | } |
| 243 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 244 | // If we set a virtual earlier, and this isn't a path, forget it again. |
| 245 | if (SetVirtual && !FoundPath) { |
| 246 | Paths.DetectedVirtual = 0; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return FoundPath; |
| 251 | } |
| 252 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 253 | bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 254 | CXXBasePath &Path, |
| 255 | void *BaseRecord) { |
| 256 | assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord && |
| 257 | "User data for FindBaseClass is not canonical!"); |
| 258 | return Specifier->getType()->getAs<RecordType>()->getDecl() |
| 259 | ->getCanonicalDecl() == BaseRecord; |
| 260 | } |
| 261 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 262 | bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 263 | CXXBasePath &Path, |
| 264 | void *Name) { |
| 265 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 266 | |
| 267 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 268 | for (Path.Decls = BaseRecord->lookup(N); |
| 269 | Path.Decls.first != Path.Decls.second; |
| 270 | ++Path.Decls.first) { |
| 271 | if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag)) |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | return false; |
| 276 | } |
| 277 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 278 | bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 279 | CXXBasePath &Path, |
| 280 | void *Name) { |
| 281 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 282 | |
| 283 | const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member; |
| 284 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 285 | for (Path.Decls = BaseRecord->lookup(N); |
| 286 | Path.Decls.first != Path.Decls.second; |
| 287 | ++Path.Decls.first) { |
| 288 | if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS)) |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 295 | bool CXXRecordDecl:: |
| 296 | FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, |
| 297 | CXXBasePath &Path, |
| 298 | void *Name) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 299 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 300 | |
| 301 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 302 | for (Path.Decls = BaseRecord->lookup(N); |
| 303 | Path.Decls.first != Path.Decls.second; |
| 304 | ++Path.Decls.first) { |
| 305 | // FIXME: Refactor the "is it a nested-name-specifier?" check |
| 306 | if (isa<TypedefDecl>(*Path.Decls.first) || |
| 307 | (*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag)) |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | return false; |
Mike Stump | 82109bd | 2009-10-06 23:38:59 +0000 | [diff] [blame] | 312 | } |