| 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" |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 16 | #include "clang/AST/DeclTemplate.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
| Douglas Gregor | 18e1b52 | 2012-09-11 07:19:42 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SetVector.h" |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 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(); |
| David Blaikie | 8f2a7fe | 2015-08-18 23:56:00 +0000 | [diff] [blame] | 34 | DeclsFound = llvm::make_unique<NamedDecl *[]>(NumDeclsFound); |
| 35 | std::copy(Decls.begin(), Decls.end(), DeclsFound.get()); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| Aaron Ballman | e6f465e | 2014-03-14 21:38:48 +0000 | [diff] [blame] | 38 | CXXBasePaths::decl_range CXXBasePaths::found_decls() { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 39 | if (NumDeclsFound == 0) |
| 40 | ComputeDeclsFound(); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 41 | |
| David Blaikie | 8f2a7fe | 2015-08-18 23:56:00 +0000 | [diff] [blame] | 42 | return decl_range(decl_iterator(DeclsFound.get()), |
| 43 | decl_iterator(DeclsFound.get() + NumDeclsFound)); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /// isAmbiguous - Determines whether the set of paths provided is |
| 47 | /// ambiguous, i.e., there are two or more paths that refer to |
| 48 | /// different base class subobjects of the same type. BaseType must be |
| 49 | /// an unqualified, canonical class type. |
| Douglas Gregor | 27ac429 | 2010-05-21 20:29:55 +0000 | [diff] [blame] | 50 | bool CXXBasePaths::isAmbiguous(CanQualType BaseType) { |
| 51 | BaseType = BaseType.getUnqualifiedType(); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 52 | std::pair<bool, unsigned>& Subobjects = ClassSubobjects[BaseType]; |
| 53 | return Subobjects.second + (Subobjects.first? 1 : 0) > 1; |
| 54 | } |
| 55 | |
| 56 | /// clear - Clear out all prior path information. |
| 57 | void CXXBasePaths::clear() { |
| 58 | Paths.clear(); |
| 59 | ClassSubobjects.clear(); |
| 60 | ScratchPath.clear(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 61 | DetectedVirtual = nullptr; |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /// @brief Swaps the contents of this CXXBasePaths structure with the |
| 65 | /// contents of Other. |
| 66 | void CXXBasePaths::swap(CXXBasePaths &Other) { |
| 67 | std::swap(Origin, Other.Origin); |
| 68 | Paths.swap(Other.Paths); |
| 69 | ClassSubobjects.swap(Other.ClassSubobjects); |
| 70 | std::swap(FindAmbiguities, Other.FindAmbiguities); |
| 71 | std::swap(RecordPaths, Other.RecordPaths); |
| 72 | std::swap(DetectVirtual, Other.DetectVirtual); |
| 73 | std::swap(DetectedVirtual, Other.DetectedVirtual); |
| 74 | } |
| 75 | |
| John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 76 | bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 77 | CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false, |
| 78 | /*DetectVirtual=*/false); |
| 79 | return isDerivedFrom(Base, Paths); |
| 80 | } |
| 81 | |
| John McCall | 388ef53 | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 82 | bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base, |
| 83 | CXXBasePaths &Paths) const { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 84 | if (getCanonicalDecl() == Base->getCanonicalDecl()) |
| 85 | return false; |
| 86 | |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 87 | Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 88 | |
| 89 | const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl(); |
| Benjamin Kramer | 1d38be9 | 2015-07-25 15:27:04 +0000 | [diff] [blame] | 90 | // FIXME: Capturing 'this' is a workaround for name lookup bugs in GCC 4.7. |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 91 | return lookupInBases( |
| Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 92 | [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) { |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 93 | return FindBaseClass(Specifier, Path, BaseDecl); |
| 94 | }, |
| 95 | Paths); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| Jordan Rose | 55edf5ff | 2012-08-08 18:23:20 +0000 | [diff] [blame] | 98 | bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const { |
| Anders Carlsson | 15722da | 2010-06-04 01:40:08 +0000 | [diff] [blame] | 99 | if (!getNumVBases()) |
| 100 | return false; |
| 101 | |
| Douglas Gregor | 3e63746 | 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 | 55edf5ff | 2012-08-08 18:23:20 +0000 | [diff] [blame] | 108 | Paths.setOrigin(const_cast<CXXRecordDecl*>(this)); |
| 109 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 110 | const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl(); |
| Benjamin Kramer | 1d38be9 | 2015-07-25 15:27:04 +0000 | [diff] [blame] | 111 | // FIXME: Capturing 'this' is a workaround for name lookup bugs in GCC 4.7. |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 112 | return lookupInBases( |
| Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 113 | [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) { |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 114 | return FindVirtualBaseClass(Specifier, Path, BaseDecl); |
| 115 | }, |
| 116 | Paths); |
| John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const { |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 120 | const CXXRecordDecl *TargetDecl = Base->getCanonicalDecl(); |
| 121 | return forallBases([TargetDecl](const CXXRecordDecl *Base) { |
| 122 | return Base->getCanonicalDecl() != TargetDecl; |
| 123 | }); |
| John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| Richard Smith | d80b2d5 | 2012-11-22 00:24:47 +0000 | [diff] [blame] | 126 | bool |
| 127 | CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const { |
| 128 | assert(isDependentContext()); |
| 129 | |
| 130 | for (; !CurContext->isFileContext(); CurContext = CurContext->getParent()) |
| 131 | if (CurContext->Equals(this)) |
| 132 | return true; |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 137 | bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches, |
| 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 | } |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 161 | |
| 162 | Queue.push_back(Base); |
| 163 | if (!BaseMatches(Base)) { |
| 164 | if (AllowShortCircuit) return false; |
| 165 | AllMatches = false; |
| 166 | continue; |
| John McCall | ddabf1a | 2009-12-08 07:42:38 +0000 | [diff] [blame] | 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 | |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 178 | bool CXXBasePaths::lookupInBases(ASTContext &Context, |
| 179 | const CXXRecordDecl *Record, |
| 180 | CXXRecordDecl::BaseMatchesCallback BaseMatches, |
| 181 | bool LookupInDependent) { |
| 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. |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 199 | if (!LookupInDependent && BaseType->isDependentType()) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 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; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 210 | if (isDetectingVirtual() && DetectedVirtual == nullptr) { |
| 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 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 255 | if (BaseMatches(&BaseSpec, ScratchPath)) { |
| 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) { |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 267 | CXXRecordDecl *BaseRecord; |
| 268 | if (LookupInDependent) { |
| 269 | BaseRecord = nullptr; |
| 270 | const TemplateSpecializationType *TST = |
| 271 | BaseSpec.getType()->getAs<TemplateSpecializationType>(); |
| 272 | if (!TST) { |
| 273 | if (auto *RT = BaseSpec.getType()->getAs<RecordType>()) |
| 274 | BaseRecord = cast<CXXRecordDecl>(RT->getDecl()); |
| 275 | } else { |
| 276 | TemplateName TN = TST->getTemplateName(); |
| 277 | if (auto *TD = |
| 278 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl())) |
| 279 | BaseRecord = TD->getTemplatedDecl(); |
| 280 | } |
| 281 | } else { |
| 282 | BaseRecord = cast<CXXRecordDecl>( |
| 283 | BaseSpec.getType()->castAs<RecordType>()->getDecl()); |
| 284 | } |
| 285 | if (BaseRecord && |
| 286 | lookupInBases(Context, BaseRecord, BaseMatches, LookupInDependent)) { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 287 | // C++ [class.member.lookup]p2: |
| 288 | // A member name f in one sub-object B hides a member name f in |
| 289 | // a sub-object A if A is a base class sub-object of B. Any |
| 290 | // declarations that are so hidden are eliminated from |
| 291 | // consideration. |
| 292 | |
| 293 | // There is a path to a base class that meets the criteria. If we're |
| 294 | // not collecting paths or finding ambiguities, we're done. |
| John McCall | 6f89140 | 2010-02-09 00:57:12 +0000 | [diff] [blame] | 295 | FoundPath = FoundPathThroughBase = true; |
| Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 296 | if (!isFindingAmbiguities()) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 297 | return FoundPath; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Pop this base specifier off the current path (if we're |
| 302 | // collecting paths). |
| Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 303 | if (isRecordingPaths()) { |
| 304 | ScratchPath.pop_back(); |
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 307 | // 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] | 308 | if (SetVirtual && !FoundPathThroughBase) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 309 | DetectedVirtual = nullptr; |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| John McCall | 553c079 | 2010-01-23 00:46:32 +0000 | [diff] [blame] | 312 | |
| 313 | // Reset the scratch path access. |
| Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 314 | ScratchPath.Access = AccessToHere; |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 315 | |
| 316 | return FoundPath; |
| 317 | } |
| 318 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 319 | bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches, |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 320 | CXXBasePaths &Paths, |
| 321 | bool LookupInDependent) const { |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 322 | // If we didn't find anything, report that. |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 323 | if (!Paths.lookupInBases(getASTContext(), this, BaseMatches, |
| 324 | LookupInDependent)) |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 325 | return false; |
| 326 | |
| 327 | // If we're not recording paths or we won't ever find ambiguities, |
| 328 | // we're done. |
| 329 | if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities()) |
| 330 | return true; |
| 331 | |
| 332 | // C++ [class.member.lookup]p6: |
| 333 | // When virtual base classes are used, a hidden declaration can be |
| 334 | // reached along a path through the sub-object lattice that does |
| 335 | // not pass through the hiding declaration. This is not an |
| 336 | // ambiguity. The identical use with nonvirtual base classes is an |
| 337 | // ambiguity; in that case there is no unique instance of the name |
| 338 | // that hides all the others. |
| 339 | // |
| 340 | // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy |
| 341 | // way to make it any faster. |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 342 | Paths.Paths.remove_if([&Paths](const CXXBasePath &Path) { |
| 343 | for (const CXXBasePathElement &PE : Path) { |
| 344 | if (!PE.Base->isVirtual()) |
| 345 | continue; |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 346 | |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 347 | CXXRecordDecl *VBase = nullptr; |
| 348 | if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>()) |
| 349 | VBase = cast<CXXRecordDecl>(Record->getDecl()); |
| 350 | if (!VBase) |
| 351 | break; |
| 352 | |
| 353 | // The declaration(s) we found along this path were found in a |
| 354 | // subobject of a virtual base. Check whether this virtual |
| 355 | // base is a subobject of any other path; if so, then the |
| 356 | // declaration in this path are hidden by that patch. |
| 357 | for (const CXXBasePath &HidingP : Paths) { |
| 358 | CXXRecordDecl *HidingClass = nullptr; |
| 359 | if (const RecordType *Record = |
| 360 | HidingP.back().Base->getType()->getAs<RecordType>()) |
| 361 | HidingClass = cast<CXXRecordDecl>(Record->getDecl()); |
| 362 | if (!HidingClass) |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 363 | break; |
| 364 | |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 365 | if (HidingClass->isVirtuallyDerivedFrom(VBase)) |
| 366 | return true; |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 369 | return false; |
| 370 | }); |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 371 | |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 372 | return true; |
| Douglas Gregor | 0555f7e | 2010-03-03 02:18:00 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 375 | bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 376 | CXXBasePath &Path, |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 377 | const CXXRecordDecl *BaseRecord) { |
| Benjamin Kramer | 1d38be9 | 2015-07-25 15:27:04 +0000 | [diff] [blame] | 378 | assert(BaseRecord->getCanonicalDecl() == BaseRecord && |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 379 | "User data for FindBaseClass is not canonical!"); |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 380 | return Specifier->getType()->castAs<RecordType>()->getDecl() |
| 381 | ->getCanonicalDecl() == BaseRecord; |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 384 | bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, |
| 385 | CXXBasePath &Path, |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 386 | const CXXRecordDecl *BaseRecord) { |
| Benjamin Kramer | 1d38be9 | 2015-07-25 15:27:04 +0000 | [diff] [blame] | 387 | assert(BaseRecord->getCanonicalDecl() == BaseRecord && |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 388 | "User data for FindBaseClass is not canonical!"); |
| 389 | return Specifier->isVirtual() && |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 390 | Specifier->getType()->castAs<RecordType>()->getDecl() |
| 391 | ->getCanonicalDecl() == BaseRecord; |
| Douglas Gregor | 3e63746 | 2010-03-03 04:38:46 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 394 | bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier, |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 395 | CXXBasePath &Path, |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 396 | DeclarationName Name) { |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 397 | RecordDecl *BaseRecord = |
| 398 | Specifier->getType()->castAs<RecordType>()->getDecl(); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 399 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 400 | for (Path.Decls = BaseRecord->lookup(Name); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 401 | !Path.Decls.empty(); |
| 402 | Path.Decls = Path.Decls.slice(1)) { |
| 403 | if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag)) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
| 407 | return false; |
| 408 | } |
| 409 | |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 410 | static bool findOrdinaryMember(RecordDecl *BaseRecord, CXXBasePath &Path, |
| 411 | DeclarationName Name) { |
| 412 | const unsigned IDNS = clang::Decl::IDNS_Ordinary | clang::Decl::IDNS_Tag | |
| 413 | clang::Decl::IDNS_Member; |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 414 | for (Path.Decls = BaseRecord->lookup(Name); |
| 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 | } |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 420 | |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 424 | bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier, |
| 425 | CXXBasePath &Path, |
| 426 | DeclarationName Name) { |
| 427 | RecordDecl *BaseRecord = |
| 428 | Specifier->getType()->castAs<RecordType>()->getDecl(); |
| 429 | return findOrdinaryMember(BaseRecord, Path, Name); |
| 430 | } |
| 431 | |
| 432 | bool CXXRecordDecl::FindOrdinaryMemberInDependentClasses( |
| 433 | const CXXBaseSpecifier *Specifier, CXXBasePath &Path, |
| 434 | DeclarationName Name) { |
| 435 | const TemplateSpecializationType *TST = |
| 436 | Specifier->getType()->getAs<TemplateSpecializationType>(); |
| 437 | if (!TST) { |
| 438 | auto *RT = Specifier->getType()->getAs<RecordType>(); |
| 439 | if (!RT) |
| 440 | return false; |
| 441 | return findOrdinaryMember(RT->getDecl(), Path, Name); |
| 442 | } |
| 443 | TemplateName TN = TST->getTemplateName(); |
| 444 | const auto *TD = dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()); |
| 445 | if (!TD) |
| 446 | return false; |
| 447 | CXXRecordDecl *RD = TD->getTemplatedDecl(); |
| 448 | if (!RD) |
| 449 | return false; |
| 450 | return findOrdinaryMember(RD, Path, Name); |
| 451 | } |
| 452 | |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 453 | bool CXXRecordDecl::FindOMPReductionMember(const CXXBaseSpecifier *Specifier, |
| 454 | CXXBasePath &Path, |
| 455 | DeclarationName Name) { |
| 456 | RecordDecl *BaseRecord = |
| 457 | Specifier->getType()->castAs<RecordType>()->getDecl(); |
| 458 | |
| 459 | for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty(); |
| 460 | Path.Decls = Path.Decls.slice(1)) { |
| 461 | if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPReduction)) |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | return false; |
| 466 | } |
| 467 | |
| John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 468 | bool CXXRecordDecl:: |
| 469 | FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, |
| 470 | CXXBasePath &Path, |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 471 | DeclarationName Name) { |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 472 | RecordDecl *BaseRecord = |
| 473 | Specifier->getType()->castAs<RecordType>()->getDecl(); |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 474 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 475 | for (Path.Decls = BaseRecord->lookup(Name); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 476 | !Path.Decls.empty(); |
| 477 | Path.Decls = Path.Decls.slice(1)) { |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 478 | // FIXME: Refactor the "is it a nested-name-specifier?" check |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 479 | if (isa<TypedefNameDecl>(Path.Decls.front()) || |
| 480 | Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag)) |
| Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 481 | return true; |
| 482 | } |
| 483 | |
| 484 | return false; |
| Mike Stump | 512c5b7 | 2009-10-06 23:38:59 +0000 | [diff] [blame] | 485 | } |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 486 | |
| Alex Lorenz | 4e1377a | 2017-05-10 09:47:41 +0000 | [diff] [blame^] | 487 | std::vector<const NamedDecl *> CXXRecordDecl::lookupDependentName( |
| 488 | const DeclarationName &Name, |
| 489 | llvm::function_ref<bool(const NamedDecl *ND)> Filter) { |
| 490 | std::vector<const NamedDecl *> Results; |
| 491 | // Lookup in the class. |
| 492 | DeclContext::lookup_result DirectResult = lookup(Name); |
| 493 | if (!DirectResult.empty()) { |
| 494 | for (const NamedDecl *ND : DirectResult) { |
| 495 | if (Filter(ND)) |
| 496 | Results.push_back(ND); |
| 497 | } |
| 498 | return Results; |
| 499 | } |
| 500 | // Perform lookup into our base classes. |
| 501 | CXXBasePaths Paths; |
| 502 | Paths.setOrigin(this); |
| 503 | if (!lookupInBases( |
| 504 | [&](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) { |
| 505 | return CXXRecordDecl::FindOrdinaryMemberInDependentClasses( |
| 506 | Specifier, Path, Name); |
| 507 | }, |
| 508 | Paths, /*LookupInDependent=*/true)) |
| 509 | return Results; |
| 510 | for (const NamedDecl *ND : Paths.front().Decls) { |
| 511 | if (Filter(ND)) |
| 512 | Results.push_back(ND); |
| 513 | } |
| 514 | return Results; |
| 515 | } |
| 516 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 517 | void OverridingMethods::add(unsigned OverriddenSubobject, |
| 518 | UniqueVirtualMethod Overriding) { |
| Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 519 | SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 520 | = Overrides[OverriddenSubobject]; |
| 521 | if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(), |
| 522 | Overriding) == SubobjectOverrides.end()) |
| 523 | SubobjectOverrides.push_back(Overriding); |
| 524 | } |
| 525 | |
| 526 | void OverridingMethods::add(const OverridingMethods &Other) { |
| 527 | for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) { |
| 528 | for (overriding_const_iterator M = I->second.begin(), |
| 529 | MEnd = I->second.end(); |
| 530 | M != MEnd; |
| 531 | ++M) |
| 532 | add(I->first, *M); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) { |
| 537 | for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) { |
| 538 | I->second.clear(); |
| 539 | I->second.push_back(Overriding); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | namespace { |
| 545 | class FinalOverriderCollector { |
| 546 | /// \brief The number of subobjects of a given class type that |
| 547 | /// occur within the class hierarchy. |
| 548 | llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount; |
| 549 | |
| 550 | /// \brief Overriders for each virtual base subobject. |
| 551 | llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders; |
| 552 | |
| 553 | CXXFinalOverriderMap FinalOverriders; |
| 554 | |
| 555 | public: |
| 556 | ~FinalOverriderCollector(); |
| 557 | |
| 558 | void Collect(const CXXRecordDecl *RD, bool VirtualBase, |
| 559 | const CXXRecordDecl *InVirtualSubobject, |
| 560 | CXXFinalOverriderMap &Overriders); |
| 561 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 562 | } |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 563 | |
| 564 | void FinalOverriderCollector::Collect(const CXXRecordDecl *RD, |
| 565 | bool VirtualBase, |
| 566 | const CXXRecordDecl *InVirtualSubobject, |
| 567 | CXXFinalOverriderMap &Overriders) { |
| 568 | unsigned SubobjectNumber = 0; |
| 569 | if (!VirtualBase) |
| 570 | SubobjectNumber |
| 571 | = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())]; |
| 572 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 573 | for (const auto &Base : RD->bases()) { |
| 574 | if (const RecordType *RT = Base.getType()->getAs<RecordType>()) { |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 575 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 576 | if (!BaseDecl->isPolymorphic()) |
| 577 | continue; |
| 578 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 579 | if (Overriders.empty() && !Base.isVirtual()) { |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 580 | // There are no other overriders of virtual member functions, |
| 581 | // so let the base class fill in our overriders for us. |
| 582 | Collect(BaseDecl, false, InVirtualSubobject, Overriders); |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | // Collect all of the overridders from the base class subobject |
| 587 | // and merge them into the set of overridders for this class. |
| 588 | // For virtual base classes, populate or use the cached virtual |
| 589 | // overrides so that we do not walk the virtual base class (and |
| 590 | // its base classes) more than once. |
| 591 | CXXFinalOverriderMap ComputedBaseOverriders; |
| 592 | CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders; |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 593 | if (Base.isVirtual()) { |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 594 | CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl]; |
| Benjamin Kramer | ea388a2 | 2012-05-27 22:41:08 +0000 | [diff] [blame] | 595 | BaseOverriders = MyVirtualOverriders; |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 596 | if (!MyVirtualOverriders) { |
| 597 | MyVirtualOverriders = new CXXFinalOverriderMap; |
| Benjamin Kramer | ea388a2 | 2012-05-27 22:41:08 +0000 | [diff] [blame] | 598 | |
| 599 | // Collect may cause VirtualOverriders to reallocate, invalidating the |
| 600 | // MyVirtualOverriders reference. Set BaseOverriders to the right |
| 601 | // value now. |
| 602 | BaseOverriders = MyVirtualOverriders; |
| 603 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 604 | Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders); |
| 605 | } |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 606 | } else |
| 607 | Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders); |
| 608 | |
| 609 | // Merge the overriders from this base class into our own set of |
| 610 | // overriders. |
| 611 | for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(), |
| 612 | OMEnd = BaseOverriders->end(); |
| 613 | OM != OMEnd; |
| 614 | ++OM) { |
| 615 | const CXXMethodDecl *CanonOM |
| 616 | = cast<CXXMethodDecl>(OM->first->getCanonicalDecl()); |
| 617 | Overriders[CanonOM].add(OM->second); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 622 | for (auto *M : RD->methods()) { |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 623 | // We only care about virtual methods. |
| 624 | if (!M->isVirtual()) |
| 625 | continue; |
| 626 | |
| 627 | CXXMethodDecl *CanonM = cast<CXXMethodDecl>(M->getCanonicalDecl()); |
| 628 | |
| 629 | if (CanonM->begin_overridden_methods() |
| 630 | == CanonM->end_overridden_methods()) { |
| 631 | // This is a new virtual function that does not override any |
| 632 | // other virtual function. Add it to the map of virtual |
| 633 | // functions for which we are tracking overridders. |
| 634 | |
| 635 | // C++ [class.virtual]p2: |
| 636 | // For convenience we say that any virtual function overrides itself. |
| 637 | Overriders[CanonM].add(SubobjectNumber, |
| 638 | UniqueVirtualMethod(CanonM, SubobjectNumber, |
| 639 | InVirtualSubobject)); |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | // This virtual method overrides other virtual methods, so it does |
| 644 | // not add any new slots into the set of overriders. Instead, we |
| 645 | // replace entries in the set of overriders with the new |
| 646 | // overrider. To do so, we dig down to the original virtual |
| 647 | // functions using data recursion and update all of the methods it |
| 648 | // overrides. |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 649 | typedef llvm::iterator_range<CXXMethodDecl::method_iterator> |
| 650 | OverriddenMethods; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 651 | SmallVector<OverriddenMethods, 4> Stack; |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 652 | Stack.push_back(llvm::make_range(CanonM->begin_overridden_methods(), |
| 653 | CanonM->end_overridden_methods())); |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 654 | while (!Stack.empty()) { |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 655 | for (const CXXMethodDecl *OM : Stack.pop_back_val()) { |
| 656 | const CXXMethodDecl *CanonOM = OM->getCanonicalDecl(); |
| Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 657 | |
| 658 | // C++ [class.virtual]p2: |
| 659 | // A virtual member function C::vf of a class object S is |
| 660 | // a final overrider unless the most derived class (1.8) |
| 661 | // of which S is a base class subobject (if any) declares |
| 662 | // or inherits another member function that overrides vf. |
| 663 | // |
| 664 | // Treating this object like the most derived class, we |
| 665 | // replace any overrides from base classes with this |
| 666 | // overriding virtual function. |
| 667 | Overriders[CanonOM].replaceAll( |
| 668 | UniqueVirtualMethod(CanonM, SubobjectNumber, |
| 669 | InVirtualSubobject)); |
| 670 | |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 671 | if (CanonOM->begin_overridden_methods() |
| Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 672 | == CanonOM->end_overridden_methods()) |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 673 | continue; |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 674 | |
| 675 | // Continue recursion to the methods that this virtual method |
| 676 | // overrides. |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 677 | Stack.push_back(llvm::make_range(CanonOM->begin_overridden_methods(), |
| 678 | CanonOM->end_overridden_methods())); |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 681 | |
| 682 | // C++ [class.virtual]p2: |
| 683 | // For convenience we say that any virtual function overrides itself. |
| 684 | Overriders[CanonM].add(SubobjectNumber, |
| 685 | UniqueVirtualMethod(CanonM, SubobjectNumber, |
| 686 | InVirtualSubobject)); |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
| 690 | FinalOverriderCollector::~FinalOverriderCollector() { |
| 691 | for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator |
| 692 | VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end(); |
| 693 | VO != VOEnd; |
| 694 | ++VO) |
| 695 | delete VO->second; |
| 696 | } |
| 697 | |
| 698 | void |
| 699 | CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const { |
| 700 | FinalOverriderCollector Collector; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 701 | Collector.Collect(this, false, nullptr, FinalOverriders); |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 702 | |
| 703 | // Weed out any final overriders that come from virtual base class |
| 704 | // subobjects that were hidden by other subobjects along any path. |
| 705 | // This is the final-overrider variant of C++ [class.member.lookup]p10. |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 706 | for (auto &OM : FinalOverriders) { |
| 707 | for (auto &SO : OM.second) { |
| 708 | SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO.second; |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 709 | if (Overriding.size() < 2) |
| 710 | continue; |
| 711 | |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 712 | auto IsHidden = [&Overriding](const UniqueVirtualMethod &M) { |
| 713 | if (!M.InVirtualSubobject) |
| 714 | return false; |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 715 | |
| 716 | // We have an overriding method in a virtual base class |
| 717 | // subobject (or non-virtual base class subobject thereof); |
| 718 | // determine whether there exists an other overriding method |
| 719 | // in a base class subobject that hides the virtual base class |
| 720 | // subobject. |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 721 | for (const UniqueVirtualMethod &OP : Overriding) |
| 722 | if (&M != &OP && |
| 723 | OP.Method->getParent()->isVirtuallyDerivedFrom( |
| 724 | M.InVirtualSubobject)) |
| 725 | return true; |
| 726 | return false; |
| 727 | }; |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 728 | |
| Benjamin Kramer | ece036e | 2015-02-11 19:09:16 +0000 | [diff] [blame] | 729 | Overriding.erase( |
| 730 | std::remove_if(Overriding.begin(), Overriding.end(), IsHidden), |
| 731 | Overriding.end()); |
| Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | } |
| Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 735 | |
| 736 | static void |
| 737 | AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context, |
| 738 | CXXIndirectPrimaryBaseSet& Bases) { |
| 739 | // If the record has a virtual primary base class, add it to our set. |
| 740 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| Anders Carlsson | 7f95cd1 | 2010-11-24 23:12:57 +0000 | [diff] [blame] | 741 | if (Layout.isPrimaryBaseVirtual()) |
| Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 742 | Bases.insert(Layout.getPrimaryBase()); |
| 743 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 744 | for (const auto &I : RD->bases()) { |
| 745 | assert(!I.getType()->isDependentType() && |
| Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 746 | "Cannot get indirect primary bases for class with dependent bases."); |
| 747 | |
| 748 | const CXXRecordDecl *BaseDecl = |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 749 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
| Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 750 | |
| 751 | // Only bases with virtual bases participate in computing the |
| 752 | // indirect primary virtual base classes. |
| 753 | if (BaseDecl->getNumVBases()) |
| 754 | AddIndirectPrimaryBases(BaseDecl, Context, Bases); |
| 755 | } |
| 756 | |
| 757 | } |
| 758 | |
| 759 | void |
| 760 | CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const { |
| 761 | ASTContext &Context = getASTContext(); |
| 762 | |
| 763 | if (!getNumVBases()) |
| 764 | return; |
| 765 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 766 | for (const auto &I : bases()) { |
| 767 | assert(!I.getType()->isDependentType() && |
| Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 768 | "Cannot get indirect primary bases for class with dependent bases."); |
| 769 | |
| 770 | const CXXRecordDecl *BaseDecl = |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 771 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
| Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 772 | |
| 773 | // Only bases with virtual bases participate in computing the |
| 774 | // indirect primary virtual base classes. |
| 775 | if (BaseDecl->getNumVBases()) |
| 776 | AddIndirectPrimaryBases(BaseDecl, Context, Bases); |
| 777 | } |
| 778 | } |