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