blob: 0377bd324cb65fe0f21bbd94c915cfdb3661652b [file] [log] [blame]
Eugene Zelenkof71964a2017-11-30 22:33:48 +00001//===- CXXInheritance.cpp - C++ Inheritance -------------------------------===//
Douglas Gregor36d1b142009-10-06 17:59:45 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor36d1b142009-10-06 17:59:45 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file provides routines that help analyzing C++ inheritance hierarchies.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenkof71964a2017-11-30 22:33:48 +000012
Douglas Gregor36d1b142009-10-06 17:59:45 +000013#include "clang/AST/CXXInheritance.h"
Benjamin Kramer2ef30312012-07-04 18:45:14 +000014#include "clang/AST/ASTContext.h"
Eugene Zelenkof71964a2017-11-30 22:33:48 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclBase.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000017#include "clang/AST/DeclCXX.h"
Alex Lorenz4e1377a2017-05-10 09:47:41 +000018#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/RecordLayout.h"
Eugene Zelenkof71964a2017-11-30 22:33:48 +000020#include "clang/AST/TemplateName.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/LLVM.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/STLExtras.h"
Douglas Gregor18e1b522012-09-11 07:19:42 +000025#include "llvm/ADT/SetVector.h"
Eugene Zelenkof71964a2017-11-30 22:33:48 +000026#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/Support/Casting.h"
Douglas Gregor36d1b142009-10-06 17:59:45 +000029#include <algorithm>
Eugene Zelenkof71964a2017-11-30 22:33:48 +000030#include <utility>
31#include <cassert>
32#include <vector>
Douglas Gregor36d1b142009-10-06 17:59:45 +000033
34using namespace clang;
35
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000036/// Computes the set of declarations referenced by these base
Douglas Gregor36d1b142009-10-06 17:59:45 +000037/// paths.
38void CXXBasePaths::ComputeDeclsFound() {
39 assert(NumDeclsFound == 0 && !DeclsFound &&
40 "Already computed the set of declarations");
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000041
Benjamin Kramer1b2bc602018-07-20 20:13:08 +000042 llvm::SmallSetVector<NamedDecl *, 8> Decls;
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000043 for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
David Blaikieff7d47a2012-12-19 00:45:41 +000044 Decls.insert(Path->Decls.front());
Benjamin Kramer91c6b6a2012-02-23 15:18:31 +000045
Douglas Gregor36d1b142009-10-06 17:59:45 +000046 NumDeclsFound = Decls.size();
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +000047 DeclsFound = std::make_unique<NamedDecl *[]>(NumDeclsFound);
David Blaikie8f2a7fe2015-08-18 23:56:00 +000048 std::copy(Decls.begin(), Decls.end(), DeclsFound.get());
Douglas Gregor36d1b142009-10-06 17:59:45 +000049}
50
Aaron Ballmane6f465e2014-03-14 21:38:48 +000051CXXBasePaths::decl_range CXXBasePaths::found_decls() {
Douglas Gregor36d1b142009-10-06 17:59:45 +000052 if (NumDeclsFound == 0)
53 ComputeDeclsFound();
Douglas Gregor36d1b142009-10-06 17:59:45 +000054
David Blaikie8f2a7fe2015-08-18 23:56:00 +000055 return decl_range(decl_iterator(DeclsFound.get()),
56 decl_iterator(DeclsFound.get() + NumDeclsFound));
Douglas Gregor36d1b142009-10-06 17:59:45 +000057}
58
59/// isAmbiguous - Determines whether the set of paths provided is
60/// ambiguous, i.e., there are two or more paths that refer to
61/// different base class subobjects of the same type. BaseType must be
62/// an unqualified, canonical class type.
Douglas Gregor27ac4292010-05-21 20:29:55 +000063bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
64 BaseType = BaseType.getUnqualifiedType();
Benjamin Kramer1b2bc602018-07-20 20:13:08 +000065 IsVirtBaseAndNumberNonVirtBases Subobjects = ClassSubobjects[BaseType];
66 return Subobjects.NumberOfNonVirtBases + (Subobjects.IsVirtBase ? 1 : 0) > 1;
Douglas Gregor36d1b142009-10-06 17:59:45 +000067}
68
69/// clear - Clear out all prior path information.
70void CXXBasePaths::clear() {
71 Paths.clear();
72 ClassSubobjects.clear();
Alex Lorenz6796c0b2017-05-18 18:06:07 +000073 VisitedDependentRecords.clear();
Douglas Gregor36d1b142009-10-06 17:59:45 +000074 ScratchPath.clear();
Craig Topper36250ad2014-05-12 05:36:57 +000075 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +000076}
77
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000078/// Swaps the contents of this CXXBasePaths structure with the
Douglas Gregor36d1b142009-10-06 17:59:45 +000079/// contents of Other.
80void CXXBasePaths::swap(CXXBasePaths &Other) {
81 std::swap(Origin, Other.Origin);
82 Paths.swap(Other.Paths);
83 ClassSubobjects.swap(Other.ClassSubobjects);
Alex Lorenz6796c0b2017-05-18 18:06:07 +000084 VisitedDependentRecords.swap(Other.VisitedDependentRecords);
Douglas Gregor36d1b142009-10-06 17:59:45 +000085 std::swap(FindAmbiguities, Other.FindAmbiguities);
86 std::swap(RecordPaths, Other.RecordPaths);
87 std::swap(DetectVirtual, Other.DetectVirtual);
88 std::swap(DetectedVirtual, Other.DetectedVirtual);
89}
90
John McCall388ef532011-01-28 22:02:36 +000091bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000092 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
93 /*DetectVirtual=*/false);
94 return isDerivedFrom(Base, Paths);
95}
96
John McCall388ef532011-01-28 22:02:36 +000097bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
98 CXXBasePaths &Paths) const {
Douglas Gregor36d1b142009-10-06 17:59:45 +000099 if (getCanonicalDecl() == Base->getCanonicalDecl())
100 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000101
John McCall84c16cf2009-11-12 03:15:40 +0000102 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000103
104 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
105 return lookupInBases(
Malcolm Parsonsc6e45832017-01-13 18:55:32 +0000106 [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000107 return FindBaseClass(Specifier, Path, BaseDecl);
108 },
109 Paths);
Douglas Gregor36d1b142009-10-06 17:59:45 +0000110}
111
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000112bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
Anders Carlsson15722da2010-06-04 01:40:08 +0000113 if (!getNumVBases())
114 return false;
115
Douglas Gregor3e637462010-03-03 04:38:46 +0000116 CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
117 /*DetectVirtual=*/false);
118
119 if (getCanonicalDecl() == Base->getCanonicalDecl())
120 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000121
Jordan Rose55edf5ff2012-08-08 18:23:20 +0000122 Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
123
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000124 const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
125 return lookupInBases(
Malcolm Parsonsc6e45832017-01-13 18:55:32 +0000126 [BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000127 return FindVirtualBaseClass(Specifier, Path, BaseDecl);
128 },
129 Paths);
John McCallddabf1a2009-12-08 07:42:38 +0000130}
131
132bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000133 const CXXRecordDecl *TargetDecl = Base->getCanonicalDecl();
134 return forallBases([TargetDecl](const CXXRecordDecl *Base) {
135 return Base->getCanonicalDecl() != TargetDecl;
136 });
John McCallddabf1a2009-12-08 07:42:38 +0000137}
138
Richard Smithd80b2d52012-11-22 00:24:47 +0000139bool
140CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const {
141 assert(isDependentContext());
142
143 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
144 if (CurContext->Equals(this))
145 return true;
146
147 return false;
148}
149
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000150bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches,
Douglas Gregordc974572012-11-10 07:24:09 +0000151 bool AllowShortCircuit) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000152 SmallVector<const CXXRecordDecl*, 8> Queue;
John McCallddabf1a2009-12-08 07:42:38 +0000153
154 const CXXRecordDecl *Record = this;
155 bool AllMatches = true;
156 while (true) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000157 for (const auto &I : Record->bases()) {
158 const RecordType *Ty = I.getType()->getAs<RecordType>();
Douglas Gregordc974572012-11-10 07:24:09 +0000159 if (!Ty) {
John McCallddabf1a2009-12-08 07:42:38 +0000160 if (AllowShortCircuit) return false;
161 AllMatches = false;
162 continue;
163 }
164
Fangrui Song6907ce22018-07-30 19:24:48 +0000165 CXXRecordDecl *Base =
Douglas Gregordc974572012-11-10 07:24:09 +0000166 cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
Richard Smithd80b2d52012-11-22 00:24:47 +0000167 if (!Base ||
168 (Base->isDependentContext() &&
169 !Base->isCurrentInstantiation(Record))) {
John McCallddabf1a2009-12-08 07:42:38 +0000170 if (AllowShortCircuit) return false;
171 AllMatches = false;
172 continue;
173 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000174
Faisal Vali683b0742016-05-19 02:28:21 +0000175 Queue.push_back(Base);
176 if (!BaseMatches(Base)) {
177 if (AllowShortCircuit) return false;
178 AllMatches = false;
179 continue;
John McCallddabf1a2009-12-08 07:42:38 +0000180 }
181 }
182
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000183 if (Queue.empty())
184 break;
185 Record = Queue.pop_back_val(); // not actually a queue.
John McCallddabf1a2009-12-08 07:42:38 +0000186 }
187
188 return AllMatches;
189}
190
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000191bool CXXBasePaths::lookupInBases(ASTContext &Context,
192 const CXXRecordDecl *Record,
193 CXXRecordDecl::BaseMatchesCallback BaseMatches,
194 bool LookupInDependent) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000195 bool FoundPath = false;
John McCall401982f2010-01-20 21:53:11 +0000196
John McCall553c0792010-01-23 00:46:32 +0000197 // The access of the path down to this record.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000198 AccessSpecifier AccessToHere = ScratchPath.Access;
199 bool IsFirstStep = ScratchPath.empty();
John McCall553c0792010-01-23 00:46:32 +0000200
Aaron Ballman574705e2014-03-13 15:41:46 +0000201 for (const auto &BaseSpec : Record->bases()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000202 // Find the record of the base class subobjects for this type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000203 QualType BaseType =
204 Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
205
Douglas Gregor36d1b142009-10-06 17:59:45 +0000206 // C++ [temp.dep]p3:
207 // In the definition of a class template or a member of a class template,
208 // if a base class of the class template depends on a template-parameter,
Fangrui Song6907ce22018-07-30 19:24:48 +0000209 // the base class scope is not examined during unqualified name lookup
210 // either at the point of definition of the class template or member or
Douglas Gregor36d1b142009-10-06 17:59:45 +0000211 // during an instantiation of the class tem- plate or member.
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000212 if (!LookupInDependent && BaseType->isDependentType())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000213 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +0000214
Douglas Gregor36d1b142009-10-06 17:59:45 +0000215 // Determine whether we need to visit this base class at all,
216 // updating the count of subobjects appropriately.
Benjamin Kramer1b2bc602018-07-20 20:13:08 +0000217 IsVirtBaseAndNumberNonVirtBases &Subobjects = ClassSubobjects[BaseType];
Douglas Gregor36d1b142009-10-06 17:59:45 +0000218 bool VisitBase = true;
219 bool SetVirtual = false;
Aaron Ballman574705e2014-03-13 15:41:46 +0000220 if (BaseSpec.isVirtual()) {
Benjamin Kramer1b2bc602018-07-20 20:13:08 +0000221 VisitBase = !Subobjects.IsVirtBase;
222 Subobjects.IsVirtBase = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000223 if (isDetectingVirtual() && DetectedVirtual == nullptr) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000224 // If this is the first virtual we find, remember it. If it turns out
225 // there is no base path here, we'll reset it later.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000226 DetectedVirtual = BaseType->getAs<RecordType>();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000227 SetVirtual = true;
228 }
Benjamin Kramer1b2bc602018-07-20 20:13:08 +0000229 } else {
230 ++Subobjects.NumberOfNonVirtBases;
231 }
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000232 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000233 // Add this base specifier to the current path.
234 CXXBasePathElement Element;
Aaron Ballman574705e2014-03-13 15:41:46 +0000235 Element.Base = &BaseSpec;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000236 Element.Class = Record;
Aaron Ballman574705e2014-03-13 15:41:46 +0000237 if (BaseSpec.isVirtual())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000238 Element.SubobjectNumber = 0;
239 else
Benjamin Kramer1b2bc602018-07-20 20:13:08 +0000240 Element.SubobjectNumber = Subobjects.NumberOfNonVirtBases;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000241 ScratchPath.push_back(Element);
John McCall401982f2010-01-20 21:53:11 +0000242
John McCall553c0792010-01-23 00:46:32 +0000243 // Calculate the "top-down" access to this base class.
244 // The spec actually describes this bottom-up, but top-down is
245 // equivalent because the definition works out as follows:
246 // 1. Write down the access along each step in the inheritance
247 // chain, followed by the access of the decl itself.
248 // For example, in
249 // class A { public: int foo; };
250 // class B : protected A {};
251 // class C : public B {};
252 // class D : private C {};
253 // we would write:
254 // private public protected public
255 // 2. If 'private' appears anywhere except far-left, access is denied.
256 // 3. Otherwise, overall access is determined by the most restrictive
257 // access in the sequence.
258 if (IsFirstStep)
Aaron Ballman574705e2014-03-13 15:41:46 +0000259 ScratchPath.Access = BaseSpec.getAccessSpecifier();
John McCall553c0792010-01-23 00:46:32 +0000260 else
Fangrui Song6907ce22018-07-30 19:24:48 +0000261 ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
Aaron Ballman574705e2014-03-13 15:41:46 +0000262 BaseSpec.getAccessSpecifier());
Douglas Gregor36d1b142009-10-06 17:59:45 +0000263 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000264
John McCall6f891402010-02-09 00:57:12 +0000265 // Track whether there's a path involving this specific base.
266 bool FoundPathThroughBase = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000267
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000268 if (BaseMatches(&BaseSpec, ScratchPath)) {
John McCall553c0792010-01-23 00:46:32 +0000269 // We've found a path that terminates at this base.
John McCall6f891402010-02-09 00:57:12 +0000270 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000271 if (isRecordingPaths()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000272 // We have a path. Make a copy of it before moving on.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000273 Paths.push_back(ScratchPath);
274 } else if (!isFindingAmbiguities()) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000275 // We found a path and we don't care about ambiguities;
276 // return immediately.
277 return FoundPath;
278 }
279 } else if (VisitBase) {
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000280 CXXRecordDecl *BaseRecord;
281 if (LookupInDependent) {
282 BaseRecord = nullptr;
283 const TemplateSpecializationType *TST =
284 BaseSpec.getType()->getAs<TemplateSpecializationType>();
285 if (!TST) {
286 if (auto *RT = BaseSpec.getType()->getAs<RecordType>())
287 BaseRecord = cast<CXXRecordDecl>(RT->getDecl());
288 } else {
289 TemplateName TN = TST->getTemplateName();
290 if (auto *TD =
291 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()))
292 BaseRecord = TD->getTemplatedDecl();
293 }
Alex Lorenz6796c0b2017-05-18 18:06:07 +0000294 if (BaseRecord) {
295 if (!BaseRecord->hasDefinition() ||
296 VisitedDependentRecords.count(BaseRecord)) {
297 BaseRecord = nullptr;
298 } else {
299 VisitedDependentRecords.insert(BaseRecord);
300 }
301 }
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000302 } else {
303 BaseRecord = cast<CXXRecordDecl>(
304 BaseSpec.getType()->castAs<RecordType>()->getDecl());
305 }
306 if (BaseRecord &&
307 lookupInBases(Context, BaseRecord, BaseMatches, LookupInDependent)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000308 // C++ [class.member.lookup]p2:
309 // A member name f in one sub-object B hides a member name f in
310 // a sub-object A if A is a base class sub-object of B. Any
311 // declarations that are so hidden are eliminated from
312 // consideration.
Fangrui Song6907ce22018-07-30 19:24:48 +0000313
314 // There is a path to a base class that meets the criteria. If we're
Douglas Gregor36d1b142009-10-06 17:59:45 +0000315 // not collecting paths or finding ambiguities, we're done.
John McCall6f891402010-02-09 00:57:12 +0000316 FoundPath = FoundPathThroughBase = true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000317 if (!isFindingAmbiguities())
Douglas Gregor36d1b142009-10-06 17:59:45 +0000318 return FoundPath;
319 }
320 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000321
Douglas Gregor36d1b142009-10-06 17:59:45 +0000322 // Pop this base specifier off the current path (if we're
323 // collecting paths).
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000324 if (isRecordingPaths()) {
325 ScratchPath.pop_back();
John McCall401982f2010-01-20 21:53:11 +0000326 }
327
Douglas Gregor36d1b142009-10-06 17:59:45 +0000328 // If we set a virtual earlier, and this isn't a path, forget it again.
John McCall6f891402010-02-09 00:57:12 +0000329 if (SetVirtual && !FoundPathThroughBase) {
Craig Topper36250ad2014-05-12 05:36:57 +0000330 DetectedVirtual = nullptr;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000331 }
332 }
John McCall553c0792010-01-23 00:46:32 +0000333
334 // Reset the scratch path access.
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000335 ScratchPath.Access = AccessToHere;
Fangrui Song6907ce22018-07-30 19:24:48 +0000336
Douglas Gregor36d1b142009-10-06 17:59:45 +0000337 return FoundPath;
338}
339
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000340bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches,
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000341 CXXBasePaths &Paths,
342 bool LookupInDependent) const {
Douglas Gregor3e637462010-03-03 04:38:46 +0000343 // If we didn't find anything, report that.
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000344 if (!Paths.lookupInBases(getASTContext(), this, BaseMatches,
345 LookupInDependent))
Douglas Gregor3e637462010-03-03 04:38:46 +0000346 return false;
347
348 // If we're not recording paths or we won't ever find ambiguities,
349 // we're done.
350 if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
351 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000352
Douglas Gregor3e637462010-03-03 04:38:46 +0000353 // C++ [class.member.lookup]p6:
354 // When virtual base classes are used, a hidden declaration can be
355 // reached along a path through the sub-object lattice that does
356 // not pass through the hiding declaration. This is not an
357 // ambiguity. The identical use with nonvirtual base classes is an
358 // ambiguity; in that case there is no unique instance of the name
359 // that hides all the others.
360 //
361 // FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
362 // way to make it any faster.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000363 Paths.Paths.remove_if([&Paths](const CXXBasePath &Path) {
364 for (const CXXBasePathElement &PE : Path) {
365 if (!PE.Base->isVirtual())
366 continue;
Douglas Gregor3e637462010-03-03 04:38:46 +0000367
Benjamin Kramerece036e2015-02-11 19:09:16 +0000368 CXXRecordDecl *VBase = nullptr;
369 if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>())
370 VBase = cast<CXXRecordDecl>(Record->getDecl());
371 if (!VBase)
372 break;
373
374 // The declaration(s) we found along this path were found in a
375 // subobject of a virtual base. Check whether this virtual
376 // base is a subobject of any other path; if so, then the
377 // declaration in this path are hidden by that patch.
378 for (const CXXBasePath &HidingP : Paths) {
379 CXXRecordDecl *HidingClass = nullptr;
380 if (const RecordType *Record =
381 HidingP.back().Base->getType()->getAs<RecordType>())
382 HidingClass = cast<CXXRecordDecl>(Record->getDecl());
383 if (!HidingClass)
Douglas Gregor3e637462010-03-03 04:38:46 +0000384 break;
385
Benjamin Kramerece036e2015-02-11 19:09:16 +0000386 if (HidingClass->isVirtuallyDerivedFrom(VBase))
387 return true;
Douglas Gregor3e637462010-03-03 04:38:46 +0000388 }
389 }
Benjamin Kramerece036e2015-02-11 19:09:16 +0000390 return false;
391 });
Douglas Gregor3e637462010-03-03 04:38:46 +0000392
Douglas Gregor3e637462010-03-03 04:38:46 +0000393 return true;
Douglas Gregor0555f7e2010-03-03 02:18:00 +0000394}
395
Fangrui Song6907ce22018-07-30 19:24:48 +0000396bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000397 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000398 const CXXRecordDecl *BaseRecord) {
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000399 assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
Douglas Gregor36d1b142009-10-06 17:59:45 +0000400 "User data for FindBaseClass is not canonical!");
Ted Kremenek28831752012-08-23 20:46:57 +0000401 return Specifier->getType()->castAs<RecordType>()->getDecl()
402 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor36d1b142009-10-06 17:59:45 +0000403}
404
Fangrui Song6907ce22018-07-30 19:24:48 +0000405bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
Douglas Gregor3e637462010-03-03 04:38:46 +0000406 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000407 const CXXRecordDecl *BaseRecord) {
Benjamin Kramer1d38be92015-07-25 15:27:04 +0000408 assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
Douglas Gregor3e637462010-03-03 04:38:46 +0000409 "User data for FindBaseClass is not canonical!");
410 return Specifier->isVirtual() &&
Ted Kremenek28831752012-08-23 20:46:57 +0000411 Specifier->getType()->castAs<RecordType>()->getDecl()
412 ->getCanonicalDecl() == BaseRecord;
Douglas Gregor3e637462010-03-03 04:38:46 +0000413}
414
Fangrui Song6907ce22018-07-30 19:24:48 +0000415bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
Douglas Gregor36d1b142009-10-06 17:59:45 +0000416 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000417 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000418 RecordDecl *BaseRecord =
419 Specifier->getType()->castAs<RecordType>()->getDecl();
Douglas Gregor36d1b142009-10-06 17:59:45 +0000420
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000421 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000422 !Path.Decls.empty();
423 Path.Decls = Path.Decls.slice(1)) {
424 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000425 return true;
426 }
427
428 return false;
429}
430
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000431static bool findOrdinaryMember(RecordDecl *BaseRecord, CXXBasePath &Path,
432 DeclarationName Name) {
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000433 const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag |
434 Decl::IDNS_Member;
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000435 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000436 !Path.Decls.empty();
437 Path.Decls = Path.Decls.slice(1)) {
438 if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000439 return true;
440 }
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000441
Douglas Gregor36d1b142009-10-06 17:59:45 +0000442 return false;
443}
444
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000445bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
446 CXXBasePath &Path,
447 DeclarationName Name) {
448 RecordDecl *BaseRecord =
449 Specifier->getType()->castAs<RecordType>()->getDecl();
450 return findOrdinaryMember(BaseRecord, Path, Name);
451}
452
453bool CXXRecordDecl::FindOrdinaryMemberInDependentClasses(
454 const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
455 DeclarationName Name) {
456 const TemplateSpecializationType *TST =
457 Specifier->getType()->getAs<TemplateSpecializationType>();
458 if (!TST) {
459 auto *RT = Specifier->getType()->getAs<RecordType>();
460 if (!RT)
461 return false;
462 return findOrdinaryMember(RT->getDecl(), Path, Name);
463 }
464 TemplateName TN = TST->getTemplateName();
465 const auto *TD = dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
466 if (!TD)
467 return false;
468 CXXRecordDecl *RD = TD->getTemplatedDecl();
469 if (!RD)
470 return false;
471 return findOrdinaryMember(RD, Path, Name);
472}
473
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000474bool CXXRecordDecl::FindOMPReductionMember(const CXXBaseSpecifier *Specifier,
475 CXXBasePath &Path,
476 DeclarationName Name) {
477 RecordDecl *BaseRecord =
478 Specifier->getType()->castAs<RecordType>()->getDecl();
479
480 for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty();
481 Path.Decls = Path.Decls.slice(1)) {
482 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPReduction))
483 return true;
484 }
485
486 return false;
487}
488
Michael Kruse251e1482019-02-01 20:25:04 +0000489bool CXXRecordDecl::FindOMPMapperMember(const CXXBaseSpecifier *Specifier,
490 CXXBasePath &Path,
491 DeclarationName Name) {
492 RecordDecl *BaseRecord =
493 Specifier->getType()->castAs<RecordType>()->getDecl();
494
495 for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty();
496 Path.Decls = Path.Decls.slice(1)) {
497 if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPMapper))
498 return true;
499 }
500
501 return false;
502}
503
John McCall84c16cf2009-11-12 03:15:40 +0000504bool CXXRecordDecl::
Fangrui Song6907ce22018-07-30 19:24:48 +0000505FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
John McCall84c16cf2009-11-12 03:15:40 +0000506 CXXBasePath &Path,
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000507 DeclarationName Name) {
Ted Kremenek28831752012-08-23 20:46:57 +0000508 RecordDecl *BaseRecord =
509 Specifier->getType()->castAs<RecordType>()->getDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +0000510
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000511 for (Path.Decls = BaseRecord->lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +0000512 !Path.Decls.empty();
513 Path.Decls = Path.Decls.slice(1)) {
Douglas Gregor36d1b142009-10-06 17:59:45 +0000514 // FIXME: Refactor the "is it a nested-name-specifier?" check
David Blaikieff7d47a2012-12-19 00:45:41 +0000515 if (isa<TypedefNameDecl>(Path.Decls.front()) ||
516 Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
Douglas Gregor36d1b142009-10-06 17:59:45 +0000517 return true;
518 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000519
Douglas Gregor36d1b142009-10-06 17:59:45 +0000520 return false;
Mike Stump512c5b72009-10-06 23:38:59 +0000521}
Douglas Gregor4165bd62010-03-23 23:47:56 +0000522
Alex Lorenz4e1377a2017-05-10 09:47:41 +0000523std::vector<const NamedDecl *> CXXRecordDecl::lookupDependentName(
524 const DeclarationName &Name,
525 llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
526 std::vector<const NamedDecl *> Results;
527 // Lookup in the class.
528 DeclContext::lookup_result DirectResult = lookup(Name);
529 if (!DirectResult.empty()) {
530 for (const NamedDecl *ND : DirectResult) {
531 if (Filter(ND))
532 Results.push_back(ND);
533 }
534 return Results;
535 }
536 // Perform lookup into our base classes.
537 CXXBasePaths Paths;
538 Paths.setOrigin(this);
539 if (!lookupInBases(
540 [&](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
541 return CXXRecordDecl::FindOrdinaryMemberInDependentClasses(
542 Specifier, Path, Name);
543 },
544 Paths, /*LookupInDependent=*/true))
545 return Results;
546 for (const NamedDecl *ND : Paths.front().Decls) {
547 if (Filter(ND))
548 Results.push_back(ND);
549 }
550 return Results;
551}
552
Fangrui Song6907ce22018-07-30 19:24:48 +0000553void OverridingMethods::add(unsigned OverriddenSubobject,
Douglas Gregor4165bd62010-03-23 23:47:56 +0000554 UniqueVirtualMethod Overriding) {
Craig Topper2341c0d2013-07-04 03:08:24 +0000555 SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
Douglas Gregor4165bd62010-03-23 23:47:56 +0000556 = Overrides[OverriddenSubobject];
Fangrui Song75e74e02019-03-31 08:48:19 +0000557 if (llvm::find(SubobjectOverrides, Overriding) == SubobjectOverrides.end())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000558 SubobjectOverrides.push_back(Overriding);
559}
560
561void OverridingMethods::add(const OverridingMethods &Other) {
562 for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000563 for (overriding_const_iterator M = I->second.begin(),
Douglas Gregor4165bd62010-03-23 23:47:56 +0000564 MEnd = I->second.end();
Fangrui Song6907ce22018-07-30 19:24:48 +0000565 M != MEnd;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000566 ++M)
567 add(I->first, *M);
568 }
569}
570
571void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
572 for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
573 I->second.clear();
574 I->second.push_back(Overriding);
575 }
576}
577
Douglas Gregor4165bd62010-03-23 23:47:56 +0000578namespace {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000579
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000580class FinalOverriderCollector {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000581 /// The number of subobjects of a given class type that
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000582 /// occur within the class hierarchy.
583 llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000584
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000585 /// Overriders for each virtual base subobject.
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000586 llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000587
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000588 CXXFinalOverriderMap FinalOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000589
Eugene Zelenkof71964a2017-11-30 22:33:48 +0000590public:
591 ~FinalOverriderCollector();
592
593 void Collect(const CXXRecordDecl *RD, bool VirtualBase,
594 const CXXRecordDecl *InVirtualSubobject,
595 CXXFinalOverriderMap &Overriders);
596};
597
598} // namespace
Douglas Gregor4165bd62010-03-23 23:47:56 +0000599
Fangrui Song6907ce22018-07-30 19:24:48 +0000600void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
Douglas Gregor4165bd62010-03-23 23:47:56 +0000601 bool VirtualBase,
602 const CXXRecordDecl *InVirtualSubobject,
603 CXXFinalOverriderMap &Overriders) {
604 unsigned SubobjectNumber = 0;
605 if (!VirtualBase)
606 SubobjectNumber
607 = ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
608
Aaron Ballman574705e2014-03-13 15:41:46 +0000609 for (const auto &Base : RD->bases()) {
610 if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000611 const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
612 if (!BaseDecl->isPolymorphic())
613 continue;
614
Aaron Ballman574705e2014-03-13 15:41:46 +0000615 if (Overriders.empty() && !Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000616 // There are no other overriders of virtual member functions,
617 // so let the base class fill in our overriders for us.
618 Collect(BaseDecl, false, InVirtualSubobject, Overriders);
619 continue;
620 }
621
622 // Collect all of the overridders from the base class subobject
623 // and merge them into the set of overridders for this class.
624 // For virtual base classes, populate or use the cached virtual
625 // overrides so that we do not walk the virtual base class (and
626 // its base classes) more than once.
627 CXXFinalOverriderMap ComputedBaseOverriders;
628 CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
Aaron Ballman574705e2014-03-13 15:41:46 +0000629 if (Base.isVirtual()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000630 CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
Benjamin Kramerea388a22012-05-27 22:41:08 +0000631 BaseOverriders = MyVirtualOverriders;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000632 if (!MyVirtualOverriders) {
633 MyVirtualOverriders = new CXXFinalOverriderMap;
Benjamin Kramerea388a22012-05-27 22:41:08 +0000634
635 // Collect may cause VirtualOverriders to reallocate, invalidating the
636 // MyVirtualOverriders reference. Set BaseOverriders to the right
637 // value now.
638 BaseOverriders = MyVirtualOverriders;
639
Douglas Gregor4165bd62010-03-23 23:47:56 +0000640 Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
641 }
Douglas Gregor4165bd62010-03-23 23:47:56 +0000642 } else
643 Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
644
645 // Merge the overriders from this base class into our own set of
646 // overriders.
Fangrui Song6907ce22018-07-30 19:24:48 +0000647 for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
Douglas Gregor4165bd62010-03-23 23:47:56 +0000648 OMEnd = BaseOverriders->end();
649 OM != OMEnd;
650 ++OM) {
George Burgess IV00f70bd2018-03-01 05:43:23 +0000651 const CXXMethodDecl *CanonOM = OM->first->getCanonicalDecl();
Douglas Gregor4165bd62010-03-23 23:47:56 +0000652 Overriders[CanonOM].add(OM->second);
653 }
654 }
655 }
656
Aaron Ballman2b124d12014-03-13 16:36:16 +0000657 for (auto *M : RD->methods()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000658 // We only care about virtual methods.
659 if (!M->isVirtual())
660 continue;
661
George Burgess IV00f70bd2018-03-01 05:43:23 +0000662 CXXMethodDecl *CanonM = M->getCanonicalDecl();
Benjamin Krameracfa3392017-12-17 23:52:45 +0000663 using OverriddenMethodsRange =
664 llvm::iterator_range<CXXMethodDecl::method_iterator>;
665 OverriddenMethodsRange OverriddenMethods = CanonM->overridden_methods();
Douglas Gregor4165bd62010-03-23 23:47:56 +0000666
Benjamin Krameracfa3392017-12-17 23:52:45 +0000667 if (OverriddenMethods.begin() == OverriddenMethods.end()) {
Douglas Gregor4165bd62010-03-23 23:47:56 +0000668 // This is a new virtual function that does not override any
669 // other virtual function. Add it to the map of virtual
Fangrui Song6907ce22018-07-30 19:24:48 +0000670 // functions for which we are tracking overridders.
Douglas Gregor4165bd62010-03-23 23:47:56 +0000671
672 // C++ [class.virtual]p2:
673 // For convenience we say that any virtual function overrides itself.
674 Overriders[CanonM].add(SubobjectNumber,
675 UniqueVirtualMethod(CanonM, SubobjectNumber,
676 InVirtualSubobject));
677 continue;
678 }
679
680 // This virtual method overrides other virtual methods, so it does
681 // not add any new slots into the set of overriders. Instead, we
682 // replace entries in the set of overriders with the new
683 // overrider. To do so, we dig down to the original virtual
684 // functions using data recursion and update all of the methods it
685 // overrides.
Benjamin Krameracfa3392017-12-17 23:52:45 +0000686 SmallVector<OverriddenMethodsRange, 4> Stack(1, OverriddenMethods);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000687 while (!Stack.empty()) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000688 for (const CXXMethodDecl *OM : Stack.pop_back_val()) {
689 const CXXMethodDecl *CanonOM = OM->getCanonicalDecl();
Anders Carlssona2f74f32010-06-03 01:00:02 +0000690
691 // C++ [class.virtual]p2:
692 // A virtual member function C::vf of a class object S is
693 // a final overrider unless the most derived class (1.8)
694 // of which S is a base class subobject (if any) declares
695 // or inherits another member function that overrides vf.
696 //
697 // Treating this object like the most derived class, we
698 // replace any overrides from base classes with this
699 // overriding virtual function.
700 Overriders[CanonOM].replaceAll(
701 UniqueVirtualMethod(CanonM, SubobjectNumber,
702 InVirtualSubobject));
703
Benjamin Krameracfa3392017-12-17 23:52:45 +0000704 auto OverriddenMethods = CanonOM->overridden_methods();
705 if (OverriddenMethods.begin() == OverriddenMethods.end())
Douglas Gregor4165bd62010-03-23 23:47:56 +0000706 continue;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000707
708 // Continue recursion to the methods that this virtual method
709 // overrides.
Benjamin Krameracfa3392017-12-17 23:52:45 +0000710 Stack.push_back(OverriddenMethods);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000711 }
712 }
Anders Carlssona2f74f32010-06-03 01:00:02 +0000713
714 // C++ [class.virtual]p2:
715 // For convenience we say that any virtual function overrides itself.
716 Overriders[CanonM].add(SubobjectNumber,
717 UniqueVirtualMethod(CanonM, SubobjectNumber,
718 InVirtualSubobject));
Douglas Gregor4165bd62010-03-23 23:47:56 +0000719 }
720}
721
722FinalOverriderCollector::~FinalOverriderCollector() {
723 for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
724 VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
Fangrui Song6907ce22018-07-30 19:24:48 +0000725 VO != VOEnd;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000726 ++VO)
727 delete VO->second;
728}
729
Fangrui Song6907ce22018-07-30 19:24:48 +0000730void
Douglas Gregor4165bd62010-03-23 23:47:56 +0000731CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
732 FinalOverriderCollector Collector;
Craig Topper36250ad2014-05-12 05:36:57 +0000733 Collector.Collect(this, false, nullptr, FinalOverriders);
Douglas Gregor4165bd62010-03-23 23:47:56 +0000734
735 // Weed out any final overriders that come from virtual base class
736 // subobjects that were hidden by other subobjects along any path.
737 // This is the final-overrider variant of C++ [class.member.lookup]p10.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000738 for (auto &OM : FinalOverriders) {
739 for (auto &SO : OM.second) {
740 SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO.second;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000741 if (Overriding.size() < 2)
742 continue;
743
Benjamin Kramerece036e2015-02-11 19:09:16 +0000744 auto IsHidden = [&Overriding](const UniqueVirtualMethod &M) {
745 if (!M.InVirtualSubobject)
746 return false;
Douglas Gregor4165bd62010-03-23 23:47:56 +0000747
748 // We have an overriding method in a virtual base class
749 // subobject (or non-virtual base class subobject thereof);
750 // determine whether there exists an other overriding method
751 // in a base class subobject that hides the virtual base class
752 // subobject.
Benjamin Kramerece036e2015-02-11 19:09:16 +0000753 for (const UniqueVirtualMethod &OP : Overriding)
754 if (&M != &OP &&
755 OP.Method->getParent()->isVirtuallyDerivedFrom(
756 M.InVirtualSubobject))
757 return true;
758 return false;
759 };
Douglas Gregor4165bd62010-03-23 23:47:56 +0000760
Richard Smithaade5fb2020-01-31 17:05:27 -0800761 // FIXME: IsHidden reads from Overriding from the middle of a remove_if
762 // over the same sequence! Is this guaranteed to work?
Benjamin Kramerece036e2015-02-11 19:09:16 +0000763 Overriding.erase(
764 std::remove_if(Overriding.begin(), Overriding.end(), IsHidden),
765 Overriding.end());
Douglas Gregor4165bd62010-03-23 23:47:56 +0000766 }
767 }
768}
Anders Carlsson4131f002010-11-24 22:50:27 +0000769
Fangrui Song6907ce22018-07-30 19:24:48 +0000770static void
Anders Carlsson4131f002010-11-24 22:50:27 +0000771AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
772 CXXIndirectPrimaryBaseSet& Bases) {
773 // If the record has a virtual primary base class, add it to our set.
774 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000775 if (Layout.isPrimaryBaseVirtual())
Anders Carlsson4131f002010-11-24 22:50:27 +0000776 Bases.insert(Layout.getPrimaryBase());
777
Aaron Ballman574705e2014-03-13 15:41:46 +0000778 for (const auto &I : RD->bases()) {
779 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000780 "Cannot get indirect primary bases for class with dependent bases.");
781
782 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000783 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000784
785 // Only bases with virtual bases participate in computing the
786 // indirect primary virtual base classes.
787 if (BaseDecl->getNumVBases())
788 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
789 }
790
791}
792
Fangrui Song6907ce22018-07-30 19:24:48 +0000793void
Anders Carlsson4131f002010-11-24 22:50:27 +0000794CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
795 ASTContext &Context = getASTContext();
796
797 if (!getNumVBases())
798 return;
799
Aaron Ballman574705e2014-03-13 15:41:46 +0000800 for (const auto &I : bases()) {
801 assert(!I.getType()->isDependentType() &&
Anders Carlsson4131f002010-11-24 22:50:27 +0000802 "Cannot get indirect primary bases for class with dependent bases.");
803
804 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000805 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson4131f002010-11-24 22:50:27 +0000806
807 // Only bases with virtual bases participate in computing the
808 // indirect primary virtual base classes.
809 if (BaseDecl->getNumVBases())
810 AddIndirectPrimaryBases(BaseDecl, Context, Bases);
811 }
812}