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" |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 46170f9 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 15 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
| 17 | #include <algorithm> |
| 18 | #include <set> |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | /// \brief Computes the set of declarations referenced by these base |
| 23 | /// paths. |
| 24 | void CXXBasePaths::ComputeDeclsFound() { |
| 25 | assert(NumDeclsFound == 0 && !DeclsFound && |
| 26 | "Already computed the set of declarations"); |
Benjamin Kramer | d0e49e5 | 2012-02-23 15:18:31 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 925d58c | 2012-08-23 05:05:18 +0000 | [diff] [blame^] | 28 | llvm::SmallPtrSet<NamedDecl *, 8> KnownDecls; |
Benjamin Kramer | d0e49e5 | 2012-02-23 15:18:31 +0000 | [diff] [blame] | 29 | SmallVector<NamedDecl *, 8> Decls; |
| 30 | for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path) |
Douglas Gregor | 925d58c | 2012-08-23 05:05:18 +0000 | [diff] [blame^] | 31 | if (KnownDecls.insert(*Path->Decls.first)) |
| 32 | Decls.push_back(*Path->Decls.first); |
Benjamin Kramer | d0e49e5 | 2012-02-23 15:18:31 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 34 | NumDeclsFound = Decls.size(); |
| 35 | DeclsFound = new NamedDecl * [NumDeclsFound]; |
| 36 | std::copy(Decls.begin(), Decls.end(), DeclsFound); |
| 37 | } |
| 38 | |
| 39 | CXXBasePaths::decl_iterator CXXBasePaths::found_decls_begin() { |
| 40 | if (NumDeclsFound == 0) |
| 41 | ComputeDeclsFound(); |
| 42 | return DeclsFound; |
| 43 | } |
| 44 | |
| 45 | CXXBasePaths::decl_iterator CXXBasePaths::found_decls_end() { |
| 46 | if (NumDeclsFound == 0) |
| 47 | ComputeDeclsFound(); |
| 48 | return DeclsFound + NumDeclsFound; |
| 49 | } |
| 50 | |
| 51 | /// isAmbiguous - Determines whether the set of paths provided is |
| 52 | /// ambiguous, i.e., there are two or more paths that refer to |
| 53 | /// different base class subobjects of the same type. BaseType must be |
| 54 | /// an unqualified, canonical class type. |
Douglas Gregor | e0d5fe2 | 2010-05-21 20:29:55 +0000 | [diff] [blame] | 55 | bool CXXBasePaths::isAmbiguous(CanQualType BaseType) { |
| 56 | BaseType = BaseType.getUnqualifiedType(); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 57 | std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; |
| 58 | return Subobjects.second + (Subobjects.first? 1 : 0) > 1; |
| 59 | } |
| 60 | |
| 61 | /// clear - Clear out all prior path information. |
| 62 | void CXXBasePaths::clear() { |
| 63 | Paths.clear(); |
| 64 | ClassSubobjects.clear(); |
| 65 | ScratchPath.clear(); |
| 66 | DetectedVirtual = 0; |
| 67 | } |
| 68 | |
| 69 | /// @brief Swaps the contents of this CXXBasePaths structure with the |
| 70 | /// contents of Other. |
| 71 | void CXXBasePaths::swap(CXXBasePaths &Other) { |
| 72 | std::swap(Origin, Other.Origin); |
| 73 | Paths.swap(Other.Paths); |
| 74 | ClassSubobjects.swap(Other.ClassSubobjects); |
| 75 | std::swap(FindAmbiguities, Other.FindAmbiguities); |
| 76 | std::swap(RecordPaths, Other.RecordPaths); |
| 77 | std::swap(DetectVirtual, Other.DetectVirtual); |
| 78 | std::swap(DetectedVirtual, Other.DetectedVirtual); |
| 79 | } |
| 80 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 81 | bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 82 | CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 83 | /*DetectVirtual=*/false); |
| 84 | return isDerivedFrom(Base, Paths); |
| 85 | } |
| 86 | |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 87 | bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base, |
| 88 | CXXBasePaths &Paths) const { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 89 | if (getCanonicalDecl() == Base->getCanonicalDecl()) |
| 90 | return false; |
| 91 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 92 | Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); |
John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 93 | return lookupInBases(&FindBaseClass, |
| 94 | const_cast<CXXRecordDecl*>(Base->getCanonicalDecl()), |
| 95 | Paths); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Jordan Rose | 2aa800a | 2012-08-08 18:23:20 +0000 | [diff] [blame] | 98 | bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const { |
Anders Carlsson | 1c4c397 | 2010-06-04 01:40:08 +0000 | [diff] [blame] | 99 | if (!getNumVBases()) |
| 100 | return false; |
| 101 | |
Douglas Gregor | 4e6ba4b | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 102 | CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 103 | /*DetectVirtual=*/false); |
| 104 | |
| 105 | if (getCanonicalDecl() == Base->getCanonicalDecl()) |
| 106 | return false; |
| 107 | |
Jordan Rose | 2aa800a | 2012-08-08 18:23:20 +0000 | [diff] [blame] | 108 | Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); |
| 109 | |
| 110 | const void *BasePtr = static_cast<const void*>(Base->getCanonicalDecl()); |
| 111 | return lookupInBases(&FindVirtualBaseClass, |
| 112 | const_cast<void *>(BasePtr), |
| 113 | Paths); |
Douglas Gregor | 4e6ba4b | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 116 | static bool BaseIsNot(const CXXRecordDecl *Base, void *OpaqueTarget) { |
| 117 | // OpaqueTarget is a CXXRecordDecl*. |
| 118 | return Base->getCanonicalDecl() != (const CXXRecordDecl*) OpaqueTarget; |
| 119 | } |
| 120 | |
| 121 | bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const { |
| 122 | return forallBases(BaseIsNot, (void*) Base->getCanonicalDecl()); |
| 123 | } |
| 124 | |
| 125 | bool CXXRecordDecl::forallBases(ForallBasesCallback *BaseMatches, |
| 126 | void *OpaqueData, |
| 127 | bool AllowShortCircuit) const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | SmallVector<const CXXRecordDecl*, 8> Queue; |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 129 | |
| 130 | const CXXRecordDecl *Record = this; |
| 131 | bool AllMatches = true; |
| 132 | while (true) { |
| 133 | for (CXXRecordDecl::base_class_const_iterator |
| 134 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 135 | const RecordType *Ty = I->getType()->getAs<RecordType>(); |
| 136 | if (!Ty) { |
| 137 | if (AllowShortCircuit) return false; |
| 138 | AllMatches = false; |
| 139 | continue; |
| 140 | } |
| 141 | |
Anders Carlsson | ca910e8 | 2009-12-09 04:26:02 +0000 | [diff] [blame] | 142 | CXXRecordDecl *Base = |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 143 | cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition()); |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 144 | if (!Base) { |
| 145 | if (AllowShortCircuit) return false; |
| 146 | AllMatches = false; |
| 147 | continue; |
| 148 | } |
| 149 | |
Anders Carlsson | ca910e8 | 2009-12-09 04:26:02 +0000 | [diff] [blame] | 150 | Queue.push_back(Base); |
| 151 | if (!BaseMatches(Base, OpaqueData)) { |
John McCall | e8174bc | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 152 | if (AllowShortCircuit) return false; |
| 153 | AllMatches = false; |
| 154 | continue; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (Queue.empty()) break; |
| 159 | Record = Queue.back(); // not actually a queue. |
| 160 | Queue.pop_back(); |
| 161 | } |
| 162 | |
| 163 | return AllMatches; |
| 164 | } |
| 165 | |
Jordan Rose | 2aa800a | 2012-08-08 18:23:20 +0000 | [diff] [blame] | 166 | bool CXXBasePaths::lookupInBases(ASTContext &Context, |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 167 | const CXXRecordDecl *Record, |
| 168 | CXXRecordDecl::BaseMatchesCallback *BaseMatches, |
| 169 | void *UserData) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 170 | bool FoundPath = false; |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 171 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 172 | // The access of the path down to this record. |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 173 | AccessSpecifier AccessToHere = ScratchPath.Access; |
| 174 | bool IsFirstStep = ScratchPath.empty(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 176 | for (CXXRecordDecl::base_class_const_iterator BaseSpec = Record->bases_begin(), |
| 177 | BaseSpecEnd = Record->bases_end(); |
| 178 | BaseSpec != BaseSpecEnd; |
| 179 | ++BaseSpec) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 180 | // Find the record of the base class subobjects for this type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 181 | QualType BaseType = Context.getCanonicalType(BaseSpec->getType()) |
| 182 | .getUnqualifiedType(); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 183 | |
| 184 | // C++ [temp.dep]p3: |
| 185 | // In the definition of a class template or a member of a class template, |
| 186 | // if a base class of the class template depends on a template-parameter, |
| 187 | // the base class scope is not examined during unqualified name lookup |
| 188 | // either at the point of definition of the class template or member or |
| 189 | // during an instantiation of the class tem- plate or member. |
| 190 | if (BaseType->isDependentType()) |
| 191 | continue; |
| 192 | |
| 193 | // Determine whether we need to visit this base class at all, |
| 194 | // updating the count of subobjects appropriately. |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 195 | std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 196 | bool VisitBase = true; |
| 197 | bool SetVirtual = false; |
| 198 | if (BaseSpec->isVirtual()) { |
| 199 | VisitBase = !Subobjects.first; |
| 200 | Subobjects.first = true; |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 201 | if (isDetectingVirtual() && DetectedVirtual == 0) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 202 | // If this is the first virtual we find, remember it. If it turns out |
| 203 | // there is no base path here, we'll reset it later. |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 204 | DetectedVirtual = BaseType->getAs<RecordType>(); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 205 | SetVirtual = true; |
| 206 | } |
| 207 | } else |
| 208 | ++Subobjects.second; |
| 209 | |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 210 | if (isRecordingPaths()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 211 | // Add this base specifier to the current path. |
| 212 | CXXBasePathElement Element; |
| 213 | Element.Base = &*BaseSpec; |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 214 | Element.Class = Record; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 215 | if (BaseSpec->isVirtual()) |
| 216 | Element.SubobjectNumber = 0; |
| 217 | else |
| 218 | Element.SubobjectNumber = Subobjects.second; |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 219 | ScratchPath.push_back(Element); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 220 | |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 221 | // Calculate the "top-down" access to this base class. |
| 222 | // The spec actually describes this bottom-up, but top-down is |
| 223 | // equivalent because the definition works out as follows: |
| 224 | // 1. Write down the access along each step in the inheritance |
| 225 | // chain, followed by the access of the decl itself. |
| 226 | // For example, in |
| 227 | // class A { public: int foo; }; |
| 228 | // class B : protected A {}; |
| 229 | // class C : public B {}; |
| 230 | // class D : private C {}; |
| 231 | // we would write: |
| 232 | // private public protected public |
| 233 | // 2. If 'private' appears anywhere except far-left, access is denied. |
| 234 | // 3. Otherwise, overall access is determined by the most restrictive |
| 235 | // access in the sequence. |
| 236 | if (IsFirstStep) |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 237 | ScratchPath.Access = BaseSpec->getAccessSpecifier(); |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 238 | else |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 239 | ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere, |
| 240 | BaseSpec->getAccessSpecifier()); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 241 | } |
John McCall | ed814cc | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 242 | |
| 243 | // Track whether there's a path involving this specific base. |
| 244 | bool FoundPathThroughBase = false; |
| 245 | |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 246 | if (BaseMatches(BaseSpec, ScratchPath, UserData)) { |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 247 | // We've found a path that terminates at this base. |
John McCall | ed814cc | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 248 | FoundPath = FoundPathThroughBase = true; |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 249 | if (isRecordingPaths()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 250 | // We have a path. Make a copy of it before moving on. |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 251 | Paths.push_back(ScratchPath); |
| 252 | } else if (!isFindingAmbiguities()) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 253 | // We found a path and we don't care about ambiguities; |
| 254 | // return immediately. |
| 255 | return FoundPath; |
| 256 | } |
| 257 | } else if (VisitBase) { |
| 258 | CXXRecordDecl *BaseRecord |
| 259 | = cast<CXXRecordDecl>(BaseSpec->getType()->getAs<RecordType>() |
| 260 | ->getDecl()); |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 261 | if (lookupInBases(Context, BaseRecord, BaseMatches, UserData)) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 262 | // C++ [class.member.lookup]p2: |
| 263 | // A member name f in one sub-object B hides a member name f in |
| 264 | // a sub-object A if A is a base class sub-object of B. Any |
| 265 | // declarations that are so hidden are eliminated from |
| 266 | // consideration. |
| 267 | |
| 268 | // There is a path to a base class that meets the criteria. If we're |
| 269 | // not collecting paths or finding ambiguities, we're done. |
John McCall | ed814cc | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 270 | FoundPath = FoundPathThroughBase = true; |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 271 | if (!isFindingAmbiguities()) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 272 | return FoundPath; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Pop this base specifier off the current path (if we're |
| 277 | // collecting paths). |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 278 | if (isRecordingPaths()) { |
| 279 | ScratchPath.pop_back(); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 282 | // If we set a virtual earlier, and this isn't a path, forget it again. |
John McCall | ed814cc | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 283 | if (SetVirtual && !FoundPathThroughBase) { |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 284 | DetectedVirtual = 0; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
John McCall | 92f8831 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 287 | |
| 288 | // Reset the scratch path access. |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 289 | ScratchPath.Access = AccessToHere; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 290 | |
| 291 | return FoundPath; |
| 292 | } |
| 293 | |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 294 | bool CXXRecordDecl::lookupInBases(BaseMatchesCallback *BaseMatches, |
| 295 | void *UserData, |
| 296 | CXXBasePaths &Paths) const { |
Douglas Gregor | 4e6ba4b | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 297 | // If we didn't find anything, report that. |
| 298 | if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, UserData)) |
| 299 | return false; |
| 300 | |
| 301 | // If we're not recording paths or we won't ever find ambiguities, |
| 302 | // we're done. |
| 303 | if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities()) |
| 304 | return true; |
| 305 | |
| 306 | // C++ [class.member.lookup]p6: |
| 307 | // When virtual base classes are used, a hidden declaration can be |
| 308 | // reached along a path through the sub-object lattice that does |
| 309 | // not pass through the hiding declaration. This is not an |
| 310 | // ambiguity. The identical use with nonvirtual base classes is an |
| 311 | // ambiguity; in that case there is no unique instance of the name |
| 312 | // that hides all the others. |
| 313 | // |
| 314 | // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy |
| 315 | // way to make it any faster. |
| 316 | for (CXXBasePaths::paths_iterator P = Paths.begin(), PEnd = Paths.end(); |
| 317 | P != PEnd; /* increment in loop */) { |
| 318 | bool Hidden = false; |
| 319 | |
| 320 | for (CXXBasePath::iterator PE = P->begin(), PEEnd = P->end(); |
| 321 | PE != PEEnd && !Hidden; ++PE) { |
| 322 | if (PE->Base->isVirtual()) { |
| 323 | CXXRecordDecl *VBase = 0; |
| 324 | if (const RecordType *Record = PE->Base->getType()->getAs<RecordType>()) |
| 325 | VBase = cast<CXXRecordDecl>(Record->getDecl()); |
| 326 | if (!VBase) |
| 327 | break; |
| 328 | |
| 329 | // The declaration(s) we found along this path were found in a |
| 330 | // subobject of a virtual base. Check whether this virtual |
| 331 | // base is a subobject of any other path; if so, then the |
| 332 | // declaration in this path are hidden by that patch. |
| 333 | for (CXXBasePaths::paths_iterator HidingP = Paths.begin(), |
| 334 | HidingPEnd = Paths.end(); |
| 335 | HidingP != HidingPEnd; |
| 336 | ++HidingP) { |
| 337 | CXXRecordDecl *HidingClass = 0; |
| 338 | if (const RecordType *Record |
| 339 | = HidingP->back().Base->getType()->getAs<RecordType>()) |
| 340 | HidingClass = cast<CXXRecordDecl>(Record->getDecl()); |
| 341 | if (!HidingClass) |
| 342 | break; |
| 343 | |
| 344 | if (HidingClass->isVirtuallyDerivedFrom(VBase)) { |
| 345 | Hidden = true; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (Hidden) |
| 353 | P = Paths.Paths.erase(P); |
| 354 | else |
| 355 | ++P; |
| 356 | } |
| 357 | |
| 358 | return true; |
Douglas Gregor | 89b7702 | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 359 | } |
| 360 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 361 | bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 362 | CXXBasePath &Path, |
| 363 | void *BaseRecord) { |
| 364 | assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord && |
| 365 | "User data for FindBaseClass is not canonical!"); |
| 366 | return Specifier->getType()->getAs<RecordType>()->getDecl() |
| 367 | ->getCanonicalDecl() == BaseRecord; |
| 368 | } |
| 369 | |
Douglas Gregor | 4e6ba4b | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 370 | bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, |
| 371 | CXXBasePath &Path, |
| 372 | void *BaseRecord) { |
| 373 | assert(((Decl *)BaseRecord)->getCanonicalDecl() == BaseRecord && |
| 374 | "User data for FindBaseClass is not canonical!"); |
| 375 | return Specifier->isVirtual() && |
| 376 | Specifier->getType()->getAs<RecordType>()->getDecl() |
| 377 | ->getCanonicalDecl() == BaseRecord; |
| 378 | } |
| 379 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 380 | bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 381 | CXXBasePath &Path, |
| 382 | void *Name) { |
| 383 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 384 | |
| 385 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 386 | for (Path.Decls = BaseRecord->lookup(N); |
| 387 | Path.Decls.first != Path.Decls.second; |
| 388 | ++Path.Decls.first) { |
| 389 | if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag)) |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | return false; |
| 394 | } |
| 395 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 396 | bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 397 | CXXBasePath &Path, |
| 398 | void *Name) { |
| 399 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 400 | |
| 401 | const unsigned IDNS = IDNS_Ordinary | IDNS_Tag | IDNS_Member; |
| 402 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 403 | for (Path.Decls = BaseRecord->lookup(N); |
| 404 | Path.Decls.first != Path.Decls.second; |
| 405 | ++Path.Decls.first) { |
| 406 | if ((*Path.Decls.first)->isInIdentifierNamespace(IDNS)) |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | return false; |
| 411 | } |
| 412 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 413 | bool CXXRecordDecl:: |
| 414 | FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, |
| 415 | CXXBasePath &Path, |
| 416 | void *Name) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 417 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 418 | |
| 419 | DeclarationName N = DeclarationName::getFromOpaquePtr(Name); |
| 420 | for (Path.Decls = BaseRecord->lookup(N); |
| 421 | Path.Decls.first != Path.Decls.second; |
| 422 | ++Path.Decls.first) { |
| 423 | // FIXME: Refactor the "is it a nested-name-specifier?" check |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 424 | if (isa<TypedefNameDecl>(*Path.Decls.first) || |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 425 | (*Path.Decls.first)->isInIdentifierNamespace(IDNS_Tag)) |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | return false; |
Mike Stump | 82109bd | 2009-10-06 23:38:59 +0000 | [diff] [blame] | 430 | } |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 431 | |
| 432 | void OverridingMethods::add(unsigned OverriddenSubobject, |
| 433 | UniqueVirtualMethod Overriding) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 434 | SmallVector<UniqueVirtualMethod, 4> &SubobjectOverrides |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 435 | = Overrides[OverriddenSubobject]; |
| 436 | if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(), |
| 437 | Overriding) == SubobjectOverrides.end()) |
| 438 | SubobjectOverrides.push_back(Overriding); |
| 439 | } |
| 440 | |
| 441 | void OverridingMethods::add(const OverridingMethods &Other) { |
| 442 | for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) { |
| 443 | for (overriding_const_iterator M = I->second.begin(), |
| 444 | MEnd = I->second.end(); |
| 445 | M != MEnd; |
| 446 | ++M) |
| 447 | add(I->first, *M); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) { |
| 452 | for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) { |
| 453 | I->second.clear(); |
| 454 | I->second.push_back(Overriding); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | |
| 459 | namespace { |
| 460 | class FinalOverriderCollector { |
| 461 | /// \brief The number of subobjects of a given class type that |
| 462 | /// occur within the class hierarchy. |
| 463 | llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount; |
| 464 | |
| 465 | /// \brief Overriders for each virtual base subobject. |
| 466 | llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders; |
| 467 | |
| 468 | CXXFinalOverriderMap FinalOverriders; |
| 469 | |
| 470 | public: |
| 471 | ~FinalOverriderCollector(); |
| 472 | |
| 473 | void Collect(const CXXRecordDecl *RD, bool VirtualBase, |
| 474 | const CXXRecordDecl *InVirtualSubobject, |
| 475 | CXXFinalOverriderMap &Overriders); |
| 476 | }; |
| 477 | } |
| 478 | |
| 479 | void FinalOverriderCollector::Collect(const CXXRecordDecl *RD, |
| 480 | bool VirtualBase, |
| 481 | const CXXRecordDecl *InVirtualSubobject, |
| 482 | CXXFinalOverriderMap &Overriders) { |
| 483 | unsigned SubobjectNumber = 0; |
| 484 | if (!VirtualBase) |
| 485 | SubobjectNumber |
| 486 | = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())]; |
| 487 | |
| 488 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 489 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { |
| 490 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
| 491 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 492 | if (!BaseDecl->isPolymorphic()) |
| 493 | continue; |
| 494 | |
| 495 | if (Overriders.empty() && !Base->isVirtual()) { |
| 496 | // There are no other overriders of virtual member functions, |
| 497 | // so let the base class fill in our overriders for us. |
| 498 | Collect(BaseDecl, false, InVirtualSubobject, Overriders); |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | // Collect all of the overridders from the base class subobject |
| 503 | // and merge them into the set of overridders for this class. |
| 504 | // For virtual base classes, populate or use the cached virtual |
| 505 | // overrides so that we do not walk the virtual base class (and |
| 506 | // its base classes) more than once. |
| 507 | CXXFinalOverriderMap ComputedBaseOverriders; |
| 508 | CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders; |
| 509 | if (Base->isVirtual()) { |
| 510 | CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl]; |
Benjamin Kramer | e0cf31d | 2012-05-27 22:41:08 +0000 | [diff] [blame] | 511 | BaseOverriders = MyVirtualOverriders; |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 512 | if (!MyVirtualOverriders) { |
| 513 | MyVirtualOverriders = new CXXFinalOverriderMap; |
Benjamin Kramer | e0cf31d | 2012-05-27 22:41:08 +0000 | [diff] [blame] | 514 | |
| 515 | // Collect may cause VirtualOverriders to reallocate, invalidating the |
| 516 | // MyVirtualOverriders reference. Set BaseOverriders to the right |
| 517 | // value now. |
| 518 | BaseOverriders = MyVirtualOverriders; |
| 519 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 520 | Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders); |
| 521 | } |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 522 | } else |
| 523 | Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders); |
| 524 | |
| 525 | // Merge the overriders from this base class into our own set of |
| 526 | // overriders. |
| 527 | for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(), |
| 528 | OMEnd = BaseOverriders->end(); |
| 529 | OM != OMEnd; |
| 530 | ++OM) { |
| 531 | const CXXMethodDecl *CanonOM |
| 532 | = cast<CXXMethodDecl>(OM->first->getCanonicalDecl()); |
| 533 | Overriders[CanonOM].add(OM->second); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | for (CXXRecordDecl::method_iterator M = RD->method_begin(), |
| 539 | MEnd = RD->method_end(); |
| 540 | M != MEnd; |
| 541 | ++M) { |
| 542 | // We only care about virtual methods. |
| 543 | if (!M->isVirtual()) |
| 544 | continue; |
| 545 | |
| 546 | CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl()); |
| 547 | |
| 548 | if (CanonM->begin_overridden_methods() |
| 549 | == CanonM->end_overridden_methods()) { |
| 550 | // This is a new virtual function that does not override any |
| 551 | // other virtual function. Add it to the map of virtual |
| 552 | // functions for which we are tracking overridders. |
| 553 | |
| 554 | // C++ [class.virtual]p2: |
| 555 | // For convenience we say that any virtual function overrides itself. |
| 556 | Overriders[CanonM].add(SubobjectNumber, |
| 557 | UniqueVirtualMethod(CanonM, SubobjectNumber, |
| 558 | InVirtualSubobject)); |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | // This virtual method overrides other virtual methods, so it does |
| 563 | // not add any new slots into the set of overriders. Instead, we |
| 564 | // replace entries in the set of overriders with the new |
| 565 | // overrider. To do so, we dig down to the original virtual |
| 566 | // functions using data recursion and update all of the methods it |
| 567 | // overrides. |
| 568 | typedef std::pair<CXXMethodDecl::method_iterator, |
| 569 | CXXMethodDecl::method_iterator> OverriddenMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 570 | SmallVector<OverriddenMethods, 4> Stack; |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 571 | Stack.push_back(std::make_pair(CanonM->begin_overridden_methods(), |
| 572 | CanonM->end_overridden_methods())); |
| 573 | while (!Stack.empty()) { |
| 574 | OverriddenMethods OverMethods = Stack.back(); |
| 575 | Stack.pop_back(); |
| 576 | |
| 577 | for (; OverMethods.first != OverMethods.second; ++OverMethods.first) { |
| 578 | const CXXMethodDecl *CanonOM |
| 579 | = cast<CXXMethodDecl>((*OverMethods.first)->getCanonicalDecl()); |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 580 | |
| 581 | // C++ [class.virtual]p2: |
| 582 | // A virtual member function C::vf of a class object S is |
| 583 | // a final overrider unless the most derived class (1.8) |
| 584 | // of which S is a base class subobject (if any) declares |
| 585 | // or inherits another member function that overrides vf. |
| 586 | // |
| 587 | // Treating this object like the most derived class, we |
| 588 | // replace any overrides from base classes with this |
| 589 | // overriding virtual function. |
| 590 | Overriders[CanonOM].replaceAll( |
| 591 | UniqueVirtualMethod(CanonM, SubobjectNumber, |
| 592 | InVirtualSubobject)); |
| 593 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 594 | if (CanonOM->begin_overridden_methods() |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 595 | == CanonOM->end_overridden_methods()) |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 596 | continue; |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 597 | |
| 598 | // Continue recursion to the methods that this virtual method |
| 599 | // overrides. |
| 600 | Stack.push_back(std::make_pair(CanonOM->begin_overridden_methods(), |
| 601 | CanonOM->end_overridden_methods())); |
| 602 | } |
| 603 | } |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 604 | |
| 605 | // C++ [class.virtual]p2: |
| 606 | // For convenience we say that any virtual function overrides itself. |
| 607 | Overriders[CanonM].add(SubobjectNumber, |
| 608 | UniqueVirtualMethod(CanonM, SubobjectNumber, |
| 609 | InVirtualSubobject)); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | FinalOverriderCollector::~FinalOverriderCollector() { |
| 614 | for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator |
| 615 | VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end(); |
| 616 | VO != VOEnd; |
| 617 | ++VO) |
| 618 | delete VO->second; |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const { |
| 623 | FinalOverriderCollector Collector; |
| 624 | Collector.Collect(this, false, 0, FinalOverriders); |
| 625 | |
| 626 | // Weed out any final overriders that come from virtual base class |
| 627 | // subobjects that were hidden by other subobjects along any path. |
| 628 | // This is the final-overrider variant of C++ [class.member.lookup]p10. |
| 629 | for (CXXFinalOverriderMap::iterator OM = FinalOverriders.begin(), |
| 630 | OMEnd = FinalOverriders.end(); |
| 631 | OM != OMEnd; |
| 632 | ++OM) { |
| 633 | for (OverridingMethods::iterator SO = OM->second.begin(), |
| 634 | SOEnd = OM->second.end(); |
| 635 | SO != SOEnd; |
| 636 | ++SO) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 637 | SmallVector<UniqueVirtualMethod, 4> &Overriding = SO->second; |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 638 | if (Overriding.size() < 2) |
| 639 | continue; |
| 640 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 641 | for (SmallVector<UniqueVirtualMethod, 4>::iterator |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 642 | Pos = Overriding.begin(), PosEnd = Overriding.end(); |
| 643 | Pos != PosEnd; |
| 644 | /* increment in loop */) { |
| 645 | if (!Pos->InVirtualSubobject) { |
| 646 | ++Pos; |
| 647 | continue; |
| 648 | } |
| 649 | |
| 650 | // We have an overriding method in a virtual base class |
| 651 | // subobject (or non-virtual base class subobject thereof); |
| 652 | // determine whether there exists an other overriding method |
| 653 | // in a base class subobject that hides the virtual base class |
| 654 | // subobject. |
| 655 | bool Hidden = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 656 | for (SmallVector<UniqueVirtualMethod, 4>::iterator |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 657 | OP = Overriding.begin(), OPEnd = Overriding.end(); |
| 658 | OP != OPEnd && !Hidden; |
| 659 | ++OP) { |
| 660 | if (Pos == OP) |
| 661 | continue; |
| 662 | |
| 663 | if (OP->Method->getParent()->isVirtuallyDerivedFrom( |
| 664 | const_cast<CXXRecordDecl *>(Pos->InVirtualSubobject))) |
| 665 | Hidden = true; |
| 666 | } |
| 667 | |
| 668 | if (Hidden) { |
| 669 | // The current overriding function is hidden by another |
| 670 | // overriding function; remove this one. |
| 671 | Pos = Overriding.erase(Pos); |
| 672 | PosEnd = Overriding.end(); |
| 673 | } else { |
| 674 | ++Pos; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
Anders Carlsson | 46170f9 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 680 | |
| 681 | static void |
| 682 | AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context, |
| 683 | CXXIndirectPrimaryBaseSet& Bases) { |
| 684 | // If the record has a virtual primary base class, add it to our set. |
| 685 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | c9e814b | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 686 | if (Layout.isPrimaryBaseVirtual()) |
Anders Carlsson | 46170f9 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 687 | Bases.insert(Layout.getPrimaryBase()); |
| 688 | |
| 689 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 690 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | 3a03765 | 2010-11-24 22:55:29 +0000 | [diff] [blame] | 691 | assert(!I->getType()->isDependentType() && |
Anders Carlsson | 46170f9 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 692 | "Cannot get indirect primary bases for class with dependent bases."); |
| 693 | |
| 694 | const CXXRecordDecl *BaseDecl = |
| 695 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 696 | |
| 697 | // Only bases with virtual bases participate in computing the |
| 698 | // indirect primary virtual base classes. |
| 699 | if (BaseDecl->getNumVBases()) |
| 700 | AddIndirectPrimaryBases(BaseDecl, Context, Bases); |
| 701 | } |
| 702 | |
| 703 | } |
| 704 | |
| 705 | void |
| 706 | CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const { |
| 707 | ASTContext &Context = getASTContext(); |
| 708 | |
| 709 | if (!getNumVBases()) |
| 710 | return; |
| 711 | |
| 712 | for (CXXRecordDecl::base_class_const_iterator I = bases_begin(), |
| 713 | E = bases_end(); I != E; ++I) { |
Anders Carlsson | 3a03765 | 2010-11-24 22:55:29 +0000 | [diff] [blame] | 714 | assert(!I->getType()->isDependentType() && |
Anders Carlsson | 46170f9 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 715 | "Cannot get indirect primary bases for class with dependent bases."); |
| 716 | |
| 717 | const CXXRecordDecl *BaseDecl = |
| 718 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 719 | |
| 720 | // Only bases with virtual bases participate in computing the |
| 721 | // indirect primary virtual base classes. |
| 722 | if (BaseDecl->getNumVBases()) |
| 723 | AddIndirectPrimaryBases(BaseDecl, Context, Bases); |
| 724 | } |
| 725 | } |
| 726 | |