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