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