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