2017-12-01 06:33:48 +08:00
|
|
|
//===- CXXInheritance.cpp - C++ Inheritance -------------------------------===//
|
2009-10-07 01:59:45 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-10-07 01:59:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides routines that help analyzing C++ inheritance hierarchies.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-01 06:33:48 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2012-07-05 02:45:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2017-12-01 06:33:48 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
2009-10-07 01:59:45 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2017-05-10 17:47:41 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2017-12-01 06:33:48 +08:00
|
|
|
#include "clang/AST/TemplateName.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-09-11 15:19:42 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2017-12-01 06:33:48 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2009-10-07 01:59:45 +08:00
|
|
|
#include <algorithm>
|
2017-12-01 06:33:48 +08:00
|
|
|
#include <utility>
|
|
|
|
#include <cassert>
|
|
|
|
#include <vector>
|
2009-10-07 01:59:45 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Computes the set of declarations referenced by these base
|
2009-10-07 01:59:45 +08:00
|
|
|
/// paths.
|
|
|
|
void CXXBasePaths::ComputeDeclsFound() {
|
|
|
|
assert(NumDeclsFound == 0 && !DeclsFound &&
|
|
|
|
"Already computed the set of declarations");
|
2012-02-23 23:18:31 +08:00
|
|
|
|
2018-07-21 04:13:08 +08:00
|
|
|
llvm::SmallSetVector<NamedDecl *, 8> Decls;
|
2012-02-23 23:18:31 +08:00
|
|
|
for (paths_iterator Path = begin(), PathEnd = end(); Path != PathEnd; ++Path)
|
2012-12-19 08:45:41 +08:00
|
|
|
Decls.insert(Path->Decls.front());
|
2012-02-23 23:18:31 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
NumDeclsFound = Decls.size();
|
2015-08-19 07:56:00 +08:00
|
|
|
DeclsFound = llvm::make_unique<NamedDecl *[]>(NumDeclsFound);
|
|
|
|
std::copy(Decls.begin(), Decls.end(), DeclsFound.get());
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 05:38:48 +08:00
|
|
|
CXXBasePaths::decl_range CXXBasePaths::found_decls() {
|
2009-10-07 01:59:45 +08:00
|
|
|
if (NumDeclsFound == 0)
|
|
|
|
ComputeDeclsFound();
|
|
|
|
|
2015-08-19 07:56:00 +08:00
|
|
|
return decl_range(decl_iterator(DeclsFound.get()),
|
|
|
|
decl_iterator(DeclsFound.get() + NumDeclsFound));
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isAmbiguous - Determines whether the set of paths provided is
|
|
|
|
/// ambiguous, i.e., there are two or more paths that refer to
|
|
|
|
/// different base class subobjects of the same type. BaseType must be
|
|
|
|
/// an unqualified, canonical class type.
|
2010-05-22 04:29:55 +08:00
|
|
|
bool CXXBasePaths::isAmbiguous(CanQualType BaseType) {
|
|
|
|
BaseType = BaseType.getUnqualifiedType();
|
2018-07-21 04:13:08 +08:00
|
|
|
IsVirtBaseAndNumberNonVirtBases Subobjects = ClassSubobjects[BaseType];
|
|
|
|
return Subobjects.NumberOfNonVirtBases + (Subobjects.IsVirtBase ? 1 : 0) > 1;
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// clear - Clear out all prior path information.
|
|
|
|
void CXXBasePaths::clear() {
|
|
|
|
Paths.clear();
|
|
|
|
ClassSubobjects.clear();
|
2017-05-19 02:06:07 +08:00
|
|
|
VisitedDependentRecords.clear();
|
2009-10-07 01:59:45 +08:00
|
|
|
ScratchPath.clear();
|
2014-05-12 13:36:57 +08:00
|
|
|
DetectedVirtual = nullptr;
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Swaps the contents of this CXXBasePaths structure with the
|
2009-10-07 01:59:45 +08:00
|
|
|
/// contents of Other.
|
|
|
|
void CXXBasePaths::swap(CXXBasePaths &Other) {
|
|
|
|
std::swap(Origin, Other.Origin);
|
|
|
|
Paths.swap(Other.Paths);
|
|
|
|
ClassSubobjects.swap(Other.ClassSubobjects);
|
2017-05-19 02:06:07 +08:00
|
|
|
VisitedDependentRecords.swap(Other.VisitedDependentRecords);
|
2009-10-07 01:59:45 +08:00
|
|
|
std::swap(FindAmbiguities, Other.FindAmbiguities);
|
|
|
|
std::swap(RecordPaths, Other.RecordPaths);
|
|
|
|
std::swap(DetectVirtual, Other.DetectVirtual);
|
|
|
|
std::swap(DetectedVirtual, Other.DetectedVirtual);
|
|
|
|
}
|
|
|
|
|
2011-01-29 06:02:36 +08:00
|
|
|
bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base) const {
|
2009-10-07 01:59:45 +08:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
|
|
|
|
/*DetectVirtual=*/false);
|
|
|
|
return isDerivedFrom(Base, Paths);
|
|
|
|
}
|
|
|
|
|
2011-01-29 06:02:36 +08:00
|
|
|
bool CXXRecordDecl::isDerivedFrom(const CXXRecordDecl *Base,
|
|
|
|
CXXBasePaths &Paths) const {
|
2009-10-07 01:59:45 +08:00
|
|
|
if (getCanonicalDecl() == Base->getCanonicalDecl())
|
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-11-12 11:15:40 +08:00
|
|
|
Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
|
2015-07-25 23:07:25 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
|
|
|
|
return lookupInBases(
|
2017-01-14 02:55:32 +08:00
|
|
|
[BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
|
2015-07-25 23:07:25 +08:00
|
|
|
return FindBaseClass(Specifier, Path, BaseDecl);
|
|
|
|
},
|
|
|
|
Paths);
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:20 +08:00
|
|
|
bool CXXRecordDecl::isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const {
|
2010-06-04 09:40:08 +08:00
|
|
|
if (!getNumVBases())
|
|
|
|
return false;
|
|
|
|
|
2010-03-03 12:38:46 +08:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/false,
|
|
|
|
/*DetectVirtual=*/false);
|
|
|
|
|
|
|
|
if (getCanonicalDecl() == Base->getCanonicalDecl())
|
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-08-09 02:23:20 +08:00
|
|
|
Paths.setOrigin(const_cast<CXXRecordDecl*>(this));
|
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = Base->getCanonicalDecl();
|
|
|
|
return lookupInBases(
|
2017-01-14 02:55:32 +08:00
|
|
|
[BaseDecl](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
|
2015-07-25 23:07:25 +08:00
|
|
|
return FindVirtualBaseClass(Specifier, Path, BaseDecl);
|
|
|
|
},
|
|
|
|
Paths);
|
2009-12-08 15:42:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CXXRecordDecl::isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const {
|
2015-07-25 23:07:25 +08:00
|
|
|
const CXXRecordDecl *TargetDecl = Base->getCanonicalDecl();
|
|
|
|
return forallBases([TargetDecl](const CXXRecordDecl *Base) {
|
|
|
|
return Base->getCanonicalDecl() != TargetDecl;
|
|
|
|
});
|
2009-12-08 15:42:38 +08:00
|
|
|
}
|
|
|
|
|
2012-11-22 08:24:47 +08:00
|
|
|
bool
|
|
|
|
CXXRecordDecl::isCurrentInstantiation(const DeclContext *CurContext) const {
|
|
|
|
assert(isDependentContext());
|
|
|
|
|
|
|
|
for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
|
|
|
|
if (CurContext->Equals(this))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
bool CXXRecordDecl::forallBases(ForallBasesCallback BaseMatches,
|
2012-11-10 15:24:09 +08:00
|
|
|
bool AllowShortCircuit) const {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const CXXRecordDecl*, 8> Queue;
|
2009-12-08 15:42:38 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *Record = this;
|
|
|
|
bool AllMatches = true;
|
|
|
|
while (true) {
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : Record->bases()) {
|
|
|
|
const RecordType *Ty = I.getType()->getAs<RecordType>();
|
2012-11-10 15:24:09 +08:00
|
|
|
if (!Ty) {
|
2009-12-08 15:42:38 +08:00
|
|
|
if (AllowShortCircuit) return false;
|
|
|
|
AllMatches = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
CXXRecordDecl *Base =
|
2012-11-10 15:24:09 +08:00
|
|
|
cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition());
|
2012-11-22 08:24:47 +08:00
|
|
|
if (!Base ||
|
|
|
|
(Base->isDependentContext() &&
|
|
|
|
!Base->isCurrentInstantiation(Record))) {
|
2009-12-08 15:42:38 +08:00
|
|
|
if (AllowShortCircuit) return false;
|
|
|
|
AllMatches = false;
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2016-05-19 10:28:21 +08:00
|
|
|
Queue.push_back(Base);
|
|
|
|
if (!BaseMatches(Base)) {
|
|
|
|
if (AllowShortCircuit) return false;
|
|
|
|
AllMatches = false;
|
|
|
|
continue;
|
2009-12-08 15:42:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-24 00:11:15 +08:00
|
|
|
if (Queue.empty())
|
|
|
|
break;
|
|
|
|
Record = Queue.pop_back_val(); // not actually a queue.
|
2009-12-08 15:42:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return AllMatches;
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:47:41 +08:00
|
|
|
bool CXXBasePaths::lookupInBases(ASTContext &Context,
|
|
|
|
const CXXRecordDecl *Record,
|
|
|
|
CXXRecordDecl::BaseMatchesCallback BaseMatches,
|
|
|
|
bool LookupInDependent) {
|
2009-10-07 01:59:45 +08:00
|
|
|
bool FoundPath = false;
|
2010-01-21 05:53:11 +08:00
|
|
|
|
2010-01-23 08:46:32 +08:00
|
|
|
// The access of the path down to this record.
|
2010-03-03 10:18:00 +08:00
|
|
|
AccessSpecifier AccessToHere = ScratchPath.Access;
|
|
|
|
bool IsFirstStep = ScratchPath.empty();
|
2010-01-23 08:46:32 +08:00
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &BaseSpec : Record->bases()) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// Find the record of the base class subobjects for this type.
|
2014-03-13 23:41:46 +08:00
|
|
|
QualType BaseType =
|
|
|
|
Context.getCanonicalType(BaseSpec.getType()).getUnqualifiedType();
|
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
// C++ [temp.dep]p3:
|
|
|
|
// In the definition of a class template or a member of a class template,
|
|
|
|
// if a base class of the class template depends on a template-parameter,
|
2018-07-31 03:24:48 +08:00
|
|
|
// the base class scope is not examined during unqualified name lookup
|
|
|
|
// either at the point of definition of the class template or member or
|
2009-10-07 01:59:45 +08:00
|
|
|
// during an instantiation of the class tem- plate or member.
|
2017-05-10 17:47:41 +08:00
|
|
|
if (!LookupInDependent && BaseType->isDependentType())
|
2009-10-07 01:59:45 +08:00
|
|
|
continue;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
// Determine whether we need to visit this base class at all,
|
|
|
|
// updating the count of subobjects appropriately.
|
2018-07-21 04:13:08 +08:00
|
|
|
IsVirtBaseAndNumberNonVirtBases &Subobjects = ClassSubobjects[BaseType];
|
2009-10-07 01:59:45 +08:00
|
|
|
bool VisitBase = true;
|
|
|
|
bool SetVirtual = false;
|
2014-03-13 23:41:46 +08:00
|
|
|
if (BaseSpec.isVirtual()) {
|
2018-07-21 04:13:08 +08:00
|
|
|
VisitBase = !Subobjects.IsVirtBase;
|
|
|
|
Subobjects.IsVirtBase = true;
|
2014-05-12 13:36:57 +08:00
|
|
|
if (isDetectingVirtual() && DetectedVirtual == nullptr) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// If this is the first virtual we find, remember it. If it turns out
|
|
|
|
// there is no base path here, we'll reset it later.
|
2010-03-03 10:18:00 +08:00
|
|
|
DetectedVirtual = BaseType->getAs<RecordType>();
|
2009-10-07 01:59:45 +08:00
|
|
|
SetVirtual = true;
|
|
|
|
}
|
2018-07-21 04:13:08 +08:00
|
|
|
} else {
|
|
|
|
++Subobjects.NumberOfNonVirtBases;
|
|
|
|
}
|
2010-03-03 10:18:00 +08:00
|
|
|
if (isRecordingPaths()) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// Add this base specifier to the current path.
|
|
|
|
CXXBasePathElement Element;
|
2014-03-13 23:41:46 +08:00
|
|
|
Element.Base = &BaseSpec;
|
2010-03-03 10:18:00 +08:00
|
|
|
Element.Class = Record;
|
2014-03-13 23:41:46 +08:00
|
|
|
if (BaseSpec.isVirtual())
|
2009-10-07 01:59:45 +08:00
|
|
|
Element.SubobjectNumber = 0;
|
|
|
|
else
|
2018-07-21 04:13:08 +08:00
|
|
|
Element.SubobjectNumber = Subobjects.NumberOfNonVirtBases;
|
2010-03-03 10:18:00 +08:00
|
|
|
ScratchPath.push_back(Element);
|
2010-01-21 05:53:11 +08:00
|
|
|
|
2010-01-23 08:46:32 +08:00
|
|
|
// Calculate the "top-down" access to this base class.
|
|
|
|
// The spec actually describes this bottom-up, but top-down is
|
|
|
|
// equivalent because the definition works out as follows:
|
|
|
|
// 1. Write down the access along each step in the inheritance
|
|
|
|
// chain, followed by the access of the decl itself.
|
|
|
|
// For example, in
|
|
|
|
// class A { public: int foo; };
|
|
|
|
// class B : protected A {};
|
|
|
|
// class C : public B {};
|
|
|
|
// class D : private C {};
|
|
|
|
// we would write:
|
|
|
|
// private public protected public
|
|
|
|
// 2. If 'private' appears anywhere except far-left, access is denied.
|
|
|
|
// 3. Otherwise, overall access is determined by the most restrictive
|
|
|
|
// access in the sequence.
|
|
|
|
if (IsFirstStep)
|
2014-03-13 23:41:46 +08:00
|
|
|
ScratchPath.Access = BaseSpec.getAccessSpecifier();
|
2010-01-23 08:46:32 +08:00
|
|
|
else
|
2018-07-31 03:24:48 +08:00
|
|
|
ScratchPath.Access = CXXRecordDecl::MergeAccess(AccessToHere,
|
2014-03-13 23:41:46 +08:00
|
|
|
BaseSpec.getAccessSpecifier());
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-02-09 08:57:12 +08:00
|
|
|
// Track whether there's a path involving this specific base.
|
|
|
|
bool FoundPathThroughBase = false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
if (BaseMatches(&BaseSpec, ScratchPath)) {
|
2010-01-23 08:46:32 +08:00
|
|
|
// We've found a path that terminates at this base.
|
2010-02-09 08:57:12 +08:00
|
|
|
FoundPath = FoundPathThroughBase = true;
|
2010-03-03 10:18:00 +08:00
|
|
|
if (isRecordingPaths()) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// We have a path. Make a copy of it before moving on.
|
2010-03-03 10:18:00 +08:00
|
|
|
Paths.push_back(ScratchPath);
|
|
|
|
} else if (!isFindingAmbiguities()) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// We found a path and we don't care about ambiguities;
|
|
|
|
// return immediately.
|
|
|
|
return FoundPath;
|
|
|
|
}
|
|
|
|
} else if (VisitBase) {
|
2017-05-10 17:47:41 +08:00
|
|
|
CXXRecordDecl *BaseRecord;
|
|
|
|
if (LookupInDependent) {
|
|
|
|
BaseRecord = nullptr;
|
|
|
|
const TemplateSpecializationType *TST =
|
|
|
|
BaseSpec.getType()->getAs<TemplateSpecializationType>();
|
|
|
|
if (!TST) {
|
|
|
|
if (auto *RT = BaseSpec.getType()->getAs<RecordType>())
|
|
|
|
BaseRecord = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
} else {
|
|
|
|
TemplateName TN = TST->getTemplateName();
|
|
|
|
if (auto *TD =
|
|
|
|
dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()))
|
|
|
|
BaseRecord = TD->getTemplatedDecl();
|
|
|
|
}
|
2017-05-19 02:06:07 +08:00
|
|
|
if (BaseRecord) {
|
|
|
|
if (!BaseRecord->hasDefinition() ||
|
|
|
|
VisitedDependentRecords.count(BaseRecord)) {
|
|
|
|
BaseRecord = nullptr;
|
|
|
|
} else {
|
|
|
|
VisitedDependentRecords.insert(BaseRecord);
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 17:47:41 +08:00
|
|
|
} else {
|
|
|
|
BaseRecord = cast<CXXRecordDecl>(
|
|
|
|
BaseSpec.getType()->castAs<RecordType>()->getDecl());
|
|
|
|
}
|
|
|
|
if (BaseRecord &&
|
|
|
|
lookupInBases(Context, BaseRecord, BaseMatches, LookupInDependent)) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// C++ [class.member.lookup]p2:
|
|
|
|
// A member name f in one sub-object B hides a member name f in
|
|
|
|
// a sub-object A if A is a base class sub-object of B. Any
|
|
|
|
// declarations that are so hidden are eliminated from
|
|
|
|
// consideration.
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
// There is a path to a base class that meets the criteria. If we're
|
2009-10-07 01:59:45 +08:00
|
|
|
// not collecting paths or finding ambiguities, we're done.
|
2010-02-09 08:57:12 +08:00
|
|
|
FoundPath = FoundPathThroughBase = true;
|
2010-03-03 10:18:00 +08:00
|
|
|
if (!isFindingAmbiguities())
|
2009-10-07 01:59:45 +08:00
|
|
|
return FoundPath;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
// Pop this base specifier off the current path (if we're
|
|
|
|
// collecting paths).
|
2010-03-03 10:18:00 +08:00
|
|
|
if (isRecordingPaths()) {
|
|
|
|
ScratchPath.pop_back();
|
2010-01-21 05:53:11 +08:00
|
|
|
}
|
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
// If we set a virtual earlier, and this isn't a path, forget it again.
|
2010-02-09 08:57:12 +08:00
|
|
|
if (SetVirtual && !FoundPathThroughBase) {
|
2014-05-12 13:36:57 +08:00
|
|
|
DetectedVirtual = nullptr;
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-23 08:46:32 +08:00
|
|
|
|
|
|
|
// Reset the scratch path access.
|
2010-03-03 10:18:00 +08:00
|
|
|
ScratchPath.Access = AccessToHere;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
return FoundPath;
|
|
|
|
}
|
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches,
|
2017-05-10 17:47:41 +08:00
|
|
|
CXXBasePaths &Paths,
|
|
|
|
bool LookupInDependent) const {
|
2010-03-03 12:38:46 +08:00
|
|
|
// If we didn't find anything, report that.
|
2017-05-10 17:47:41 +08:00
|
|
|
if (!Paths.lookupInBases(getASTContext(), this, BaseMatches,
|
|
|
|
LookupInDependent))
|
2010-03-03 12:38:46 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we're not recording paths or we won't ever find ambiguities,
|
|
|
|
// we're done.
|
|
|
|
if (!Paths.isRecordingPaths() || !Paths.isFindingAmbiguities())
|
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-03-03 12:38:46 +08:00
|
|
|
// C++ [class.member.lookup]p6:
|
|
|
|
// When virtual base classes are used, a hidden declaration can be
|
|
|
|
// reached along a path through the sub-object lattice that does
|
|
|
|
// not pass through the hiding declaration. This is not an
|
|
|
|
// ambiguity. The identical use with nonvirtual base classes is an
|
|
|
|
// ambiguity; in that case there is no unique instance of the name
|
|
|
|
// that hides all the others.
|
|
|
|
//
|
|
|
|
// FIXME: This is an O(N^2) algorithm, but DPG doesn't see an easy
|
|
|
|
// way to make it any faster.
|
2015-02-12 03:09:16 +08:00
|
|
|
Paths.Paths.remove_if([&Paths](const CXXBasePath &Path) {
|
|
|
|
for (const CXXBasePathElement &PE : Path) {
|
|
|
|
if (!PE.Base->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CXXRecordDecl *VBase = nullptr;
|
|
|
|
if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>())
|
|
|
|
VBase = cast<CXXRecordDecl>(Record->getDecl());
|
|
|
|
if (!VBase)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// The declaration(s) we found along this path were found in a
|
|
|
|
// subobject of a virtual base. Check whether this virtual
|
|
|
|
// base is a subobject of any other path; if so, then the
|
|
|
|
// declaration in this path are hidden by that patch.
|
|
|
|
for (const CXXBasePath &HidingP : Paths) {
|
|
|
|
CXXRecordDecl *HidingClass = nullptr;
|
|
|
|
if (const RecordType *Record =
|
|
|
|
HidingP.back().Base->getType()->getAs<RecordType>())
|
|
|
|
HidingClass = cast<CXXRecordDecl>(Record->getDecl());
|
|
|
|
if (!HidingClass)
|
2010-03-03 12:38:46 +08:00
|
|
|
break;
|
|
|
|
|
2015-02-12 03:09:16 +08:00
|
|
|
if (HidingClass->isVirtuallyDerivedFrom(VBase))
|
|
|
|
return true;
|
2010-03-03 12:38:46 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 03:09:16 +08:00
|
|
|
return false;
|
|
|
|
});
|
2010-03-03 12:38:46 +08:00
|
|
|
|
|
|
|
return true;
|
2010-03-03 10:18:00 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
|
2009-10-07 01:59:45 +08:00
|
|
|
CXXBasePath &Path,
|
2015-07-25 23:07:25 +08:00
|
|
|
const CXXRecordDecl *BaseRecord) {
|
2015-07-25 23:27:04 +08:00
|
|
|
assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
|
2009-10-07 01:59:45 +08:00
|
|
|
"User data for FindBaseClass is not canonical!");
|
2012-08-24 04:46:57 +08:00
|
|
|
return Specifier->getType()->castAs<RecordType>()->getDecl()
|
|
|
|
->getCanonicalDecl() == BaseRecord;
|
2009-10-07 01:59:45 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
|
2010-03-03 12:38:46 +08:00
|
|
|
CXXBasePath &Path,
|
2015-07-25 23:07:25 +08:00
|
|
|
const CXXRecordDecl *BaseRecord) {
|
2015-07-25 23:27:04 +08:00
|
|
|
assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
|
2010-03-03 12:38:46 +08:00
|
|
|
"User data for FindBaseClass is not canonical!");
|
|
|
|
return Specifier->isVirtual() &&
|
2012-08-24 04:46:57 +08:00
|
|
|
Specifier->getType()->castAs<RecordType>()->getDecl()
|
|
|
|
->getCanonicalDecl() == BaseRecord;
|
2010-03-03 12:38:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
bool CXXRecordDecl::FindTagMember(const CXXBaseSpecifier *Specifier,
|
2009-10-07 01:59:45 +08:00
|
|
|
CXXBasePath &Path,
|
2015-07-25 23:07:25 +08:00
|
|
|
DeclarationName Name) {
|
2012-08-24 04:46:57 +08:00
|
|
|
RecordDecl *BaseRecord =
|
|
|
|
Specifier->getType()->castAs<RecordType>()->getDecl();
|
2009-10-07 01:59:45 +08:00
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
for (Path.Decls = BaseRecord->lookup(Name);
|
2012-12-19 08:45:41 +08:00
|
|
|
!Path.Decls.empty();
|
|
|
|
Path.Decls = Path.Decls.slice(1)) {
|
|
|
|
if (Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
|
2009-10-07 01:59:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:47:41 +08:00
|
|
|
static bool findOrdinaryMember(RecordDecl *BaseRecord, CXXBasePath &Path,
|
|
|
|
DeclarationName Name) {
|
2017-12-01 06:33:48 +08:00
|
|
|
const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag |
|
|
|
|
Decl::IDNS_Member;
|
2015-07-25 23:07:25 +08:00
|
|
|
for (Path.Decls = BaseRecord->lookup(Name);
|
2012-12-19 08:45:41 +08:00
|
|
|
!Path.Decls.empty();
|
|
|
|
Path.Decls = Path.Decls.slice(1)) {
|
|
|
|
if (Path.Decls.front()->isInIdentifierNamespace(IDNS))
|
2009-10-07 01:59:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-10 17:47:41 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:47:41 +08:00
|
|
|
bool CXXRecordDecl::FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
|
|
|
|
CXXBasePath &Path,
|
|
|
|
DeclarationName Name) {
|
|
|
|
RecordDecl *BaseRecord =
|
|
|
|
Specifier->getType()->castAs<RecordType>()->getDecl();
|
|
|
|
return findOrdinaryMember(BaseRecord, Path, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXXRecordDecl::FindOrdinaryMemberInDependentClasses(
|
|
|
|
const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
|
|
|
|
DeclarationName Name) {
|
|
|
|
const TemplateSpecializationType *TST =
|
|
|
|
Specifier->getType()->getAs<TemplateSpecializationType>();
|
|
|
|
if (!TST) {
|
|
|
|
auto *RT = Specifier->getType()->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return false;
|
|
|
|
return findOrdinaryMember(RT->getDecl(), Path, Name);
|
|
|
|
}
|
|
|
|
TemplateName TN = TST->getTemplateName();
|
|
|
|
const auto *TD = dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
|
|
|
|
if (!TD)
|
|
|
|
return false;
|
|
|
|
CXXRecordDecl *RD = TD->getTemplatedDecl();
|
|
|
|
if (!RD)
|
|
|
|
return false;
|
|
|
|
return findOrdinaryMember(RD, Path, Name);
|
|
|
|
}
|
|
|
|
|
2016-03-03 13:21:39 +08:00
|
|
|
bool CXXRecordDecl::FindOMPReductionMember(const CXXBaseSpecifier *Specifier,
|
|
|
|
CXXBasePath &Path,
|
|
|
|
DeclarationName Name) {
|
|
|
|
RecordDecl *BaseRecord =
|
|
|
|
Specifier->getType()->castAs<RecordType>()->getDecl();
|
|
|
|
|
|
|
|
for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty();
|
|
|
|
Path.Decls = Path.Decls.slice(1)) {
|
|
|
|
if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPReduction))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:25:04 +08:00
|
|
|
bool CXXRecordDecl::FindOMPMapperMember(const CXXBaseSpecifier *Specifier,
|
|
|
|
CXXBasePath &Path,
|
|
|
|
DeclarationName Name) {
|
|
|
|
RecordDecl *BaseRecord =
|
|
|
|
Specifier->getType()->castAs<RecordType>()->getDecl();
|
|
|
|
|
|
|
|
for (Path.Decls = BaseRecord->lookup(Name); !Path.Decls.empty();
|
|
|
|
Path.Decls = Path.Decls.slice(1)) {
|
|
|
|
if (Path.Decls.front()->isInIdentifierNamespace(IDNS_OMPMapper))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-12 11:15:40 +08:00
|
|
|
bool CXXRecordDecl::
|
2018-07-31 03:24:48 +08:00
|
|
|
FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
|
2009-11-12 11:15:40 +08:00
|
|
|
CXXBasePath &Path,
|
2015-07-25 23:07:25 +08:00
|
|
|
DeclarationName Name) {
|
2012-08-24 04:46:57 +08:00
|
|
|
RecordDecl *BaseRecord =
|
|
|
|
Specifier->getType()->castAs<RecordType>()->getDecl();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
for (Path.Decls = BaseRecord->lookup(Name);
|
2012-12-19 08:45:41 +08:00
|
|
|
!Path.Decls.empty();
|
|
|
|
Path.Decls = Path.Decls.slice(1)) {
|
2009-10-07 01:59:45 +08:00
|
|
|
// FIXME: Refactor the "is it a nested-name-specifier?" check
|
2012-12-19 08:45:41 +08:00
|
|
|
if (isa<TypedefNameDecl>(Path.Decls.front()) ||
|
|
|
|
Path.Decls.front()->isInIdentifierNamespace(IDNS_Tag))
|
2009-10-07 01:59:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
return false;
|
2009-10-07 07:38:59 +08:00
|
|
|
}
|
2010-03-24 07:47:56 +08:00
|
|
|
|
2017-05-10 17:47:41 +08:00
|
|
|
std::vector<const NamedDecl *> CXXRecordDecl::lookupDependentName(
|
|
|
|
const DeclarationName &Name,
|
|
|
|
llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
|
|
|
|
std::vector<const NamedDecl *> Results;
|
|
|
|
// Lookup in the class.
|
|
|
|
DeclContext::lookup_result DirectResult = lookup(Name);
|
|
|
|
if (!DirectResult.empty()) {
|
|
|
|
for (const NamedDecl *ND : DirectResult) {
|
|
|
|
if (Filter(ND))
|
|
|
|
Results.push_back(ND);
|
|
|
|
}
|
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
// Perform lookup into our base classes.
|
|
|
|
CXXBasePaths Paths;
|
|
|
|
Paths.setOrigin(this);
|
|
|
|
if (!lookupInBases(
|
|
|
|
[&](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
|
|
|
|
return CXXRecordDecl::FindOrdinaryMemberInDependentClasses(
|
|
|
|
Specifier, Path, Name);
|
|
|
|
},
|
|
|
|
Paths, /*LookupInDependent=*/true))
|
|
|
|
return Results;
|
|
|
|
for (const NamedDecl *ND : Paths.front().Decls) {
|
|
|
|
if (Filter(ND))
|
|
|
|
Results.push_back(ND);
|
|
|
|
}
|
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void OverridingMethods::add(unsigned OverriddenSubobject,
|
2010-03-24 07:47:56 +08:00
|
|
|
UniqueVirtualMethod Overriding) {
|
2013-07-04 11:08:24 +08:00
|
|
|
SmallVectorImpl<UniqueVirtualMethod> &SubobjectOverrides
|
2010-03-24 07:47:56 +08:00
|
|
|
= Overrides[OverriddenSubobject];
|
2018-07-31 03:24:48 +08:00
|
|
|
if (std::find(SubobjectOverrides.begin(), SubobjectOverrides.end(),
|
2010-03-24 07:47:56 +08:00
|
|
|
Overriding) == SubobjectOverrides.end())
|
|
|
|
SubobjectOverrides.push_back(Overriding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverridingMethods::add(const OverridingMethods &Other) {
|
|
|
|
for (const_iterator I = Other.begin(), IE = Other.end(); I != IE; ++I) {
|
2018-07-31 03:24:48 +08:00
|
|
|
for (overriding_const_iterator M = I->second.begin(),
|
2010-03-24 07:47:56 +08:00
|
|
|
MEnd = I->second.end();
|
2018-07-31 03:24:48 +08:00
|
|
|
M != MEnd;
|
2010-03-24 07:47:56 +08:00
|
|
|
++M)
|
|
|
|
add(I->first, *M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverridingMethods::replaceAll(UniqueVirtualMethod Overriding) {
|
|
|
|
for (iterator I = begin(), IEnd = end(); I != IEnd; ++I) {
|
|
|
|
I->second.clear();
|
|
|
|
I->second.push_back(Overriding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-12-01 06:33:48 +08:00
|
|
|
class FinalOverriderCollector {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The number of subobjects of a given class type that
|
2017-12-01 06:33:48 +08:00
|
|
|
/// occur within the class hierarchy.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCount;
|
2010-03-24 07:47:56 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Overriders for each virtual base subobject.
|
2017-12-01 06:33:48 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *> VirtualOverriders;
|
2010-03-24 07:47:56 +08:00
|
|
|
|
2017-12-01 06:33:48 +08:00
|
|
|
CXXFinalOverriderMap FinalOverriders;
|
2010-03-24 07:47:56 +08:00
|
|
|
|
2017-12-01 06:33:48 +08:00
|
|
|
public:
|
|
|
|
~FinalOverriderCollector();
|
|
|
|
|
|
|
|
void Collect(const CXXRecordDecl *RD, bool VirtualBase,
|
|
|
|
const CXXRecordDecl *InVirtualSubobject,
|
|
|
|
CXXFinalOverriderMap &Overriders);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
2010-03-24 07:47:56 +08:00
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void FinalOverriderCollector::Collect(const CXXRecordDecl *RD,
|
2010-03-24 07:47:56 +08:00
|
|
|
bool VirtualBase,
|
|
|
|
const CXXRecordDecl *InVirtualSubobject,
|
|
|
|
CXXFinalOverriderMap &Overriders) {
|
|
|
|
unsigned SubobjectNumber = 0;
|
|
|
|
if (!VirtualBase)
|
|
|
|
SubobjectNumber
|
|
|
|
= ++SubobjectCount[cast<CXXRecordDecl>(RD->getCanonicalDecl())];
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &Base : RD->bases()) {
|
|
|
|
if (const RecordType *RT = Base.getType()->getAs<RecordType>()) {
|
2010-03-24 07:47:56 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (!BaseDecl->isPolymorphic())
|
|
|
|
continue;
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
if (Overriders.empty() && !Base.isVirtual()) {
|
2010-03-24 07:47:56 +08:00
|
|
|
// There are no other overriders of virtual member functions,
|
|
|
|
// so let the base class fill in our overriders for us.
|
|
|
|
Collect(BaseDecl, false, InVirtualSubobject, Overriders);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all of the overridders from the base class subobject
|
|
|
|
// and merge them into the set of overridders for this class.
|
|
|
|
// For virtual base classes, populate or use the cached virtual
|
|
|
|
// overrides so that we do not walk the virtual base class (and
|
|
|
|
// its base classes) more than once.
|
|
|
|
CXXFinalOverriderMap ComputedBaseOverriders;
|
|
|
|
CXXFinalOverriderMap *BaseOverriders = &ComputedBaseOverriders;
|
2014-03-13 23:41:46 +08:00
|
|
|
if (Base.isVirtual()) {
|
2010-03-24 07:47:56 +08:00
|
|
|
CXXFinalOverriderMap *&MyVirtualOverriders = VirtualOverriders[BaseDecl];
|
2012-05-28 06:41:08 +08:00
|
|
|
BaseOverriders = MyVirtualOverriders;
|
2010-03-24 07:47:56 +08:00
|
|
|
if (!MyVirtualOverriders) {
|
|
|
|
MyVirtualOverriders = new CXXFinalOverriderMap;
|
2012-05-28 06:41:08 +08:00
|
|
|
|
|
|
|
// Collect may cause VirtualOverriders to reallocate, invalidating the
|
|
|
|
// MyVirtualOverriders reference. Set BaseOverriders to the right
|
|
|
|
// value now.
|
|
|
|
BaseOverriders = MyVirtualOverriders;
|
|
|
|
|
2010-03-24 07:47:56 +08:00
|
|
|
Collect(BaseDecl, true, BaseDecl, *MyVirtualOverriders);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
Collect(BaseDecl, false, InVirtualSubobject, ComputedBaseOverriders);
|
|
|
|
|
|
|
|
// Merge the overriders from this base class into our own set of
|
|
|
|
// overriders.
|
2018-07-31 03:24:48 +08:00
|
|
|
for (CXXFinalOverriderMap::iterator OM = BaseOverriders->begin(),
|
2010-03-24 07:47:56 +08:00
|
|
|
OMEnd = BaseOverriders->end();
|
|
|
|
OM != OMEnd;
|
|
|
|
++OM) {
|
2018-03-01 13:43:23 +08:00
|
|
|
const CXXMethodDecl *CanonOM = OM->first->getCanonicalDecl();
|
2010-03-24 07:47:56 +08:00
|
|
|
Overriders[CanonOM].add(OM->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 00:36:16 +08:00
|
|
|
for (auto *M : RD->methods()) {
|
2010-03-24 07:47:56 +08:00
|
|
|
// We only care about virtual methods.
|
|
|
|
if (!M->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
2018-03-01 13:43:23 +08:00
|
|
|
CXXMethodDecl *CanonM = M->getCanonicalDecl();
|
2017-12-18 07:52:45 +08:00
|
|
|
using OverriddenMethodsRange =
|
|
|
|
llvm::iterator_range<CXXMethodDecl::method_iterator>;
|
|
|
|
OverriddenMethodsRange OverriddenMethods = CanonM->overridden_methods();
|
2010-03-24 07:47:56 +08:00
|
|
|
|
2017-12-18 07:52:45 +08:00
|
|
|
if (OverriddenMethods.begin() == OverriddenMethods.end()) {
|
2010-03-24 07:47:56 +08:00
|
|
|
// This is a new virtual function that does not override any
|
|
|
|
// other virtual function. Add it to the map of virtual
|
2018-07-31 03:24:48 +08:00
|
|
|
// functions for which we are tracking overridders.
|
2010-03-24 07:47:56 +08:00
|
|
|
|
|
|
|
// C++ [class.virtual]p2:
|
|
|
|
// For convenience we say that any virtual function overrides itself.
|
|
|
|
Overriders[CanonM].add(SubobjectNumber,
|
|
|
|
UniqueVirtualMethod(CanonM, SubobjectNumber,
|
|
|
|
InVirtualSubobject));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This virtual method overrides other virtual methods, so it does
|
|
|
|
// not add any new slots into the set of overriders. Instead, we
|
|
|
|
// replace entries in the set of overriders with the new
|
|
|
|
// overrider. To do so, we dig down to the original virtual
|
|
|
|
// functions using data recursion and update all of the methods it
|
|
|
|
// overrides.
|
2017-12-18 07:52:45 +08:00
|
|
|
SmallVector<OverriddenMethodsRange, 4> Stack(1, OverriddenMethods);
|
2010-03-24 07:47:56 +08:00
|
|
|
while (!Stack.empty()) {
|
2015-02-07 01:25:10 +08:00
|
|
|
for (const CXXMethodDecl *OM : Stack.pop_back_val()) {
|
|
|
|
const CXXMethodDecl *CanonOM = OM->getCanonicalDecl();
|
2010-06-03 09:00:02 +08:00
|
|
|
|
|
|
|
// C++ [class.virtual]p2:
|
|
|
|
// A virtual member function C::vf of a class object S is
|
|
|
|
// a final overrider unless the most derived class (1.8)
|
|
|
|
// of which S is a base class subobject (if any) declares
|
|
|
|
// or inherits another member function that overrides vf.
|
|
|
|
//
|
|
|
|
// Treating this object like the most derived class, we
|
|
|
|
// replace any overrides from base classes with this
|
|
|
|
// overriding virtual function.
|
|
|
|
Overriders[CanonOM].replaceAll(
|
|
|
|
UniqueVirtualMethod(CanonM, SubobjectNumber,
|
|
|
|
InVirtualSubobject));
|
|
|
|
|
2017-12-18 07:52:45 +08:00
|
|
|
auto OverriddenMethods = CanonOM->overridden_methods();
|
|
|
|
if (OverriddenMethods.begin() == OverriddenMethods.end())
|
2010-03-24 07:47:56 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Continue recursion to the methods that this virtual method
|
|
|
|
// overrides.
|
2017-12-18 07:52:45 +08:00
|
|
|
Stack.push_back(OverriddenMethods);
|
2010-03-24 07:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-03 09:00:02 +08:00
|
|
|
|
|
|
|
// C++ [class.virtual]p2:
|
|
|
|
// For convenience we say that any virtual function overrides itself.
|
|
|
|
Overriders[CanonM].add(SubobjectNumber,
|
|
|
|
UniqueVirtualMethod(CanonM, SubobjectNumber,
|
|
|
|
InVirtualSubobject));
|
2010-03-24 07:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FinalOverriderCollector::~FinalOverriderCollector() {
|
|
|
|
for (llvm::DenseMap<const CXXRecordDecl *, CXXFinalOverriderMap *>::iterator
|
|
|
|
VO = VirtualOverriders.begin(), VOEnd = VirtualOverriders.end();
|
2018-07-31 03:24:48 +08:00
|
|
|
VO != VOEnd;
|
2010-03-24 07:47:56 +08:00
|
|
|
++VO)
|
|
|
|
delete VO->second;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void
|
2010-03-24 07:47:56 +08:00
|
|
|
CXXRecordDecl::getFinalOverriders(CXXFinalOverriderMap &FinalOverriders) const {
|
|
|
|
FinalOverriderCollector Collector;
|
2014-05-12 13:36:57 +08:00
|
|
|
Collector.Collect(this, false, nullptr, FinalOverriders);
|
2010-03-24 07:47:56 +08:00
|
|
|
|
|
|
|
// Weed out any final overriders that come from virtual base class
|
|
|
|
// subobjects that were hidden by other subobjects along any path.
|
|
|
|
// This is the final-overrider variant of C++ [class.member.lookup]p10.
|
2015-02-12 03:09:16 +08:00
|
|
|
for (auto &OM : FinalOverriders) {
|
|
|
|
for (auto &SO : OM.second) {
|
|
|
|
SmallVectorImpl<UniqueVirtualMethod> &Overriding = SO.second;
|
2010-03-24 07:47:56 +08:00
|
|
|
if (Overriding.size() < 2)
|
|
|
|
continue;
|
|
|
|
|
2015-02-12 03:09:16 +08:00
|
|
|
auto IsHidden = [&Overriding](const UniqueVirtualMethod &M) {
|
|
|
|
if (!M.InVirtualSubobject)
|
|
|
|
return false;
|
2010-03-24 07:47:56 +08:00
|
|
|
|
|
|
|
// We have an overriding method in a virtual base class
|
|
|
|
// subobject (or non-virtual base class subobject thereof);
|
|
|
|
// determine whether there exists an other overriding method
|
|
|
|
// in a base class subobject that hides the virtual base class
|
|
|
|
// subobject.
|
2015-02-12 03:09:16 +08:00
|
|
|
for (const UniqueVirtualMethod &OP : Overriding)
|
|
|
|
if (&M != &OP &&
|
|
|
|
OP.Method->getParent()->isVirtuallyDerivedFrom(
|
|
|
|
M.InVirtualSubobject))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Overriding.erase(
|
|
|
|
std::remove_if(Overriding.begin(), Overriding.end(), IsHidden),
|
|
|
|
Overriding.end());
|
2010-03-24 07:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-25 06:50:27 +08:00
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
static void
|
2010-11-25 06:50:27 +08:00
|
|
|
AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
|
|
|
|
CXXIndirectPrimaryBaseSet& Bases) {
|
|
|
|
// If the record has a virtual primary base class, add it to our set.
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2010-11-25 07:12:57 +08:00
|
|
|
if (Layout.isPrimaryBaseVirtual())
|
2010-11-25 06:50:27 +08:00
|
|
|
Bases.insert(Layout.getPrimaryBase());
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : RD->bases()) {
|
|
|
|
assert(!I.getType()->isDependentType() &&
|
2010-11-25 06:50:27 +08:00
|
|
|
"Cannot get indirect primary bases for class with dependent bases.");
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
2014-03-13 23:41:46 +08:00
|
|
|
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
|
2010-11-25 06:50:27 +08:00
|
|
|
|
|
|
|
// Only bases with virtual bases participate in computing the
|
|
|
|
// indirect primary virtual base classes.
|
|
|
|
if (BaseDecl->getNumVBases())
|
|
|
|
AddIndirectPrimaryBases(BaseDecl, Context, Bases);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void
|
2010-11-25 06:50:27 +08:00
|
|
|
CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
|
|
|
|
ASTContext &Context = getASTContext();
|
|
|
|
|
|
|
|
if (!getNumVBases())
|
|
|
|
return;
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : bases()) {
|
|
|
|
assert(!I.getType()->isDependentType() &&
|
2010-11-25 06:50:27 +08:00
|
|
|
"Cannot get indirect primary bases for class with dependent bases.");
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
2014-03-13 23:41:46 +08:00
|
|
|
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
|
2010-11-25 06:50:27 +08:00
|
|
|
|
|
|
|
// Only bases with virtual bases participate in computing the
|
|
|
|
// indirect primary virtual base classes.
|
|
|
|
if (BaseDecl->getNumVBases())
|
|
|
|
AddIndirectPrimaryBases(BaseDecl, Context, Bases);
|
|
|
|
}
|
|
|
|
}
|