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