2011-09-26 09:57:12 +08:00
|
|
|
//===--- VTableBuilder.cpp - C++ vtable layout builder --------------------===//
|
|
|
|
//
|
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
|
2011-09-26 09:57:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with generation of the layout of virtual tables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/VTableBuilder.h"
|
2012-07-05 02:45:14 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2015-05-02 05:35:45 +08:00
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
2011-09-26 09:57:12 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
|
|
|
#include "clang/AST/RecordLayout.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2015-05-05 02:47:54 +08:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
2014-01-04 07:42:00 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2011-09-26 09:57:12 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-09-26 09:57:12 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
#define DUMP_OVERRIDERS 0
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// BaseOffset - Represents an offset from a derived class to a direct or
|
|
|
|
/// indirect base class.
|
|
|
|
struct BaseOffset {
|
|
|
|
/// DerivedClass - The derived class.
|
|
|
|
const CXXRecordDecl *DerivedClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// VirtualBase - If the path from the derived class to the base class
|
2013-05-08 16:09:21 +08:00
|
|
|
/// involves virtual base classes, this holds the declaration of the last
|
|
|
|
/// virtual base in this path (i.e. closest to the base class).
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXRecordDecl *VirtualBase;
|
|
|
|
|
|
|
|
/// NonVirtualOffset - The offset from the derived class to the base class.
|
2018-07-31 03:24:48 +08:00
|
|
|
/// (Or the offset from the virtual base class to the base class, if the
|
2011-09-26 09:57:12 +08:00
|
|
|
/// path from the derived class to the base class involves a virtual base
|
|
|
|
/// class.
|
|
|
|
CharUnits NonVirtualOffset;
|
2014-05-12 13:36:57 +08:00
|
|
|
|
|
|
|
BaseOffset() : DerivedClass(nullptr), VirtualBase(nullptr),
|
|
|
|
NonVirtualOffset(CharUnits::Zero()) { }
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseOffset(const CXXRecordDecl *DerivedClass,
|
|
|
|
const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset)
|
2018-07-31 03:24:48 +08:00
|
|
|
: DerivedClass(DerivedClass), VirtualBase(VirtualBase),
|
2011-09-26 09:57:12 +08:00
|
|
|
NonVirtualOffset(NonVirtualOffset) { }
|
|
|
|
|
|
|
|
bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// FinalOverriders - Contains the final overrider member functions for all
|
|
|
|
/// member functions in the base subobjects of a class.
|
|
|
|
class FinalOverriders {
|
|
|
|
public:
|
|
|
|
/// OverriderInfo - Information about a final overrider.
|
|
|
|
struct OverriderInfo {
|
|
|
|
/// Method - The method decl of the overrider.
|
|
|
|
const CXXMethodDecl *Method;
|
|
|
|
|
2014-11-17 23:11:05 +08:00
|
|
|
/// VirtualBase - The virtual base class subobject of this overrider.
|
2014-04-23 01:32:02 +08:00
|
|
|
/// Note that this records the closest derived virtual base class subobject.
|
|
|
|
const CXXRecordDecl *VirtualBase;
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
/// Offset - the base offset of the overrider's parent in the layout class.
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits Offset;
|
2014-05-12 13:36:57 +08:00
|
|
|
|
|
|
|
OverriderInfo() : Method(nullptr), VirtualBase(nullptr),
|
|
|
|
Offset(CharUnits::Zero()) { }
|
2011-09-26 09:57:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// MostDerivedClass - The most derived class for which the final overriders
|
|
|
|
/// are stored.
|
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// MostDerivedClassOffset - If we're building final overriders for a
|
2011-09-26 09:57:12 +08:00
|
|
|
/// construction vtable, this holds the offset from the layout class to the
|
|
|
|
/// most derived class.
|
|
|
|
const CharUnits MostDerivedClassOffset;
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
/// LayoutClass - The class we're using for layout information. Will be
|
2011-09-26 09:57:12 +08:00
|
|
|
/// different than the most derived class if the final overriders are for a
|
2018-07-31 03:24:48 +08:00
|
|
|
/// construction vtable.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
ASTContext &Context;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// MostDerivedClassLayout - the AST record layout of the most derived class.
|
|
|
|
const ASTRecordLayout &MostDerivedClassLayout;
|
|
|
|
|
|
|
|
/// MethodBaseOffsetPairTy - Uniquely identifies a member function
|
|
|
|
/// in a base subobject.
|
|
|
|
typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<MethodBaseOffsetPairTy,
|
|
|
|
OverriderInfo> OverridersMapTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// OverridersMap - The final overriders for all virtual member functions of
|
2011-09-26 09:57:12 +08:00
|
|
|
/// all the base subobjects of the most derived class.
|
|
|
|
OverridersMapTy OverridersMap;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented
|
|
|
|
/// as a record decl and a subobject number) and its offsets in the most
|
|
|
|
/// derived class as well as the layout class.
|
2018-07-31 03:24:48 +08:00
|
|
|
typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits> SubobjectOffsetMapTy;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
|
|
|
|
/// given base.
|
|
|
|
void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
|
|
|
|
CharUnits OffsetInLayoutClass,
|
|
|
|
SubobjectOffsetMapTy &SubobjectOffsets,
|
|
|
|
SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
|
|
|
|
SubobjectCountMapTy &SubobjectCounts);
|
|
|
|
|
|
|
|
typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// dump - dump the final overriders for a base subobject, and all its direct
|
|
|
|
/// and indirect base subobjects.
|
|
|
|
void dump(raw_ostream &Out, BaseSubobject Base,
|
|
|
|
VisitedVirtualBasesSetTy& VisitedVirtualBases);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
public:
|
|
|
|
FinalOverriders(const CXXRecordDecl *MostDerivedClass,
|
|
|
|
CharUnits MostDerivedClassOffset,
|
|
|
|
const CXXRecordDecl *LayoutClass);
|
|
|
|
|
|
|
|
/// getOverrider - Get the final overrider for the given method declaration in
|
2018-07-31 03:24:48 +08:00
|
|
|
/// the subobject with the given base offset.
|
|
|
|
OverriderInfo getOverrider(const CXXMethodDecl *MD,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits BaseOffset) const {
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Did not find overrider!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// dump - dump the final overriders.
|
|
|
|
void dump() {
|
|
|
|
VisitedVirtualBasesSetTy VisitedVirtualBases;
|
2018-07-31 03:24:48 +08:00
|
|
|
dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()),
|
2011-09-26 09:57:12 +08:00
|
|
|
VisitedVirtualBases);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
|
|
|
|
CharUnits MostDerivedClassOffset,
|
|
|
|
const CXXRecordDecl *LayoutClass)
|
2018-07-31 03:24:48 +08:00
|
|
|
: MostDerivedClass(MostDerivedClass),
|
2011-09-26 09:57:12 +08:00
|
|
|
MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
|
|
|
|
Context(MostDerivedClass->getASTContext()),
|
|
|
|
MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
|
|
|
|
|
|
|
|
// Compute base offsets.
|
|
|
|
SubobjectOffsetMapTy SubobjectOffsets;
|
|
|
|
SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
|
|
|
|
SubobjectCountMapTy SubobjectCounts;
|
2018-07-31 03:24:48 +08:00
|
|
|
ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
|
2011-09-26 09:57:12 +08:00
|
|
|
/*IsVirtual=*/false,
|
2018-07-31 03:24:48 +08:00
|
|
|
MostDerivedClassOffset,
|
|
|
|
SubobjectOffsets, SubobjectLayoutClassOffsets,
|
2011-09-26 09:57:12 +08:00
|
|
|
SubobjectCounts);
|
|
|
|
|
2012-07-23 16:59:39 +08:00
|
|
|
// Get the final overriders.
|
2011-09-26 09:57:12 +08:00
|
|
|
CXXFinalOverriderMap FinalOverriders;
|
|
|
|
MostDerivedClass->getFinalOverriders(FinalOverriders);
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &Overrider : FinalOverriders) {
|
|
|
|
const CXXMethodDecl *MD = Overrider.first;
|
|
|
|
const OverridingMethods &Methods = Overrider.second;
|
|
|
|
|
|
|
|
for (const auto &M : Methods) {
|
|
|
|
unsigned SubobjectNumber = M.first;
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(SubobjectOffsets.count(std::make_pair(MD->getParent(),
|
2011-09-26 09:57:12 +08:00
|
|
|
SubobjectNumber)) &&
|
|
|
|
"Did not find subobject offset!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(),
|
|
|
|
SubobjectNumber)];
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
assert(M.second.size() == 1 && "Final overrider is not unique!");
|
|
|
|
const UniqueVirtualMethod &Method = M.second.front();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *OverriderRD = Method.Method->getParent();
|
|
|
|
assert(SubobjectLayoutClassOffsets.count(
|
|
|
|
std::make_pair(OverriderRD, Method.Subobject))
|
|
|
|
&& "Did not find subobject offset!");
|
|
|
|
CharUnits OverriderOffset =
|
2018-07-31 03:24:48 +08:00
|
|
|
SubobjectLayoutClassOffsets[std::make_pair(OverriderRD,
|
2011-09-26 09:57:12 +08:00
|
|
|
Method.Subobject)];
|
|
|
|
|
|
|
|
OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)];
|
|
|
|
assert(!Overrider.Method && "Overrider should not exist yet!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Overrider.Offset = OverriderOffset;
|
|
|
|
Overrider.Method = Method.Method;
|
2014-04-23 01:32:02 +08:00
|
|
|
Overrider.VirtualBase = Method.InVirtualSubobject;
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DUMP_OVERRIDERS
|
|
|
|
// And dump them (for now).
|
|
|
|
dump();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
static BaseOffset ComputeBaseOffset(const ASTContext &Context,
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXRecordDecl *DerivedRD,
|
|
|
|
const CXXBasePath &Path) {
|
|
|
|
CharUnits NonVirtualOffset = CharUnits::Zero();
|
|
|
|
|
|
|
|
unsigned NonVirtualStart = 0;
|
2014-05-12 13:36:57 +08:00
|
|
|
const CXXRecordDecl *VirtualBase = nullptr;
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// First, look for the virtual base class.
|
2013-05-08 16:09:21 +08:00
|
|
|
for (int I = Path.size(), E = 0; I != E; --I) {
|
|
|
|
const CXXBasePathElement &Element = Path[I - 1];
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (Element.Base->isVirtual()) {
|
2013-05-08 16:09:21 +08:00
|
|
|
NonVirtualStart = I;
|
2011-09-26 09:57:12 +08:00
|
|
|
QualType VBaseType = Element.Base->getType();
|
2013-07-03 00:00:40 +08:00
|
|
|
VirtualBase = VBaseType->getAsCXXRecordDecl();
|
2013-05-08 16:09:21 +08:00
|
|
|
break;
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Now compute the non-virtual offset.
|
|
|
|
for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
|
|
|
|
const CXXBasePathElement &Element = Path[I];
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check the base class offset.
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
|
|
|
|
|
2013-07-03 00:00:40 +08:00
|
|
|
const CXXRecordDecl *Base = Element.Base->getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
NonVirtualOffset += Layout.getBaseClassOffset(Base);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// FIXME: This should probably use CharUnits or something. Maybe we should
|
2018-07-31 03:24:48 +08:00
|
|
|
// even change the base offsets in ASTRecordLayout to be specified in
|
2011-09-26 09:57:12 +08:00
|
|
|
// CharUnits.
|
|
|
|
return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
static BaseOffset ComputeBaseOffset(const ASTContext &Context,
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXRecordDecl *BaseRD,
|
|
|
|
const CXXRecordDecl *DerivedRD) {
|
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/false);
|
2013-02-04 02:55:34 +08:00
|
|
|
|
|
|
|
if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
|
2011-09-26 09:57:12 +08:00
|
|
|
llvm_unreachable("Class must be derived from the passed in base class!");
|
|
|
|
|
|
|
|
return ComputeBaseOffset(Context, DerivedRD, Paths.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
static BaseOffset
|
2018-07-31 03:24:48 +08:00
|
|
|
ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXMethodDecl *DerivedMD,
|
|
|
|
const CXXMethodDecl *BaseMD) {
|
|
|
|
const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
|
|
|
|
const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Canonicalize the return types.
|
2014-01-26 00:55:45 +08:00
|
|
|
CanQualType CanDerivedReturnType =
|
|
|
|
Context.getCanonicalType(DerivedFT->getReturnType());
|
|
|
|
CanQualType CanBaseReturnType =
|
|
|
|
Context.getCanonicalType(BaseFT->getReturnType());
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(CanDerivedReturnType->getTypeClass() ==
|
|
|
|
CanBaseReturnType->getTypeClass() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Types must have same type class!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (CanDerivedReturnType == CanBaseReturnType) {
|
|
|
|
// No adjustment needed.
|
|
|
|
return BaseOffset();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (isa<ReferenceType>(CanDerivedReturnType)) {
|
2018-07-31 03:24:48 +08:00
|
|
|
CanDerivedReturnType =
|
2011-09-26 09:57:12 +08:00
|
|
|
CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
|
2018-07-31 03:24:48 +08:00
|
|
|
CanBaseReturnType =
|
2011-09-26 09:57:12 +08:00
|
|
|
CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
|
|
|
|
} else if (isa<PointerType>(CanDerivedReturnType)) {
|
2018-07-31 03:24:48 +08:00
|
|
|
CanDerivedReturnType =
|
2011-09-26 09:57:12 +08:00
|
|
|
CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
|
2018-07-31 03:24:48 +08:00
|
|
|
CanBaseReturnType =
|
2011-09-26 09:57:12 +08:00
|
|
|
CanBaseReturnType->getAs<PointerType>()->getPointeeType();
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected return type!");
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// We need to compare unqualified types here; consider
|
|
|
|
// const T *Base::foo();
|
|
|
|
// T *Derived::foo();
|
2018-07-31 03:24:48 +08:00
|
|
|
if (CanDerivedReturnType.getUnqualifiedType() ==
|
2011-09-26 09:57:12 +08:00
|
|
|
CanBaseReturnType.getUnqualifiedType()) {
|
|
|
|
// No adjustment needed.
|
|
|
|
return BaseOffset();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *DerivedRD =
|
2011-09-26 09:57:12 +08:00
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *BaseRD =
|
2011-09-26 09:57:12 +08:00
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
|
|
|
|
|
|
|
|
return ComputeBaseOffset(Context, BaseRD, DerivedRD);
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void
|
2011-09-26 09:57:12 +08:00
|
|
|
FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
|
|
|
|
CharUnits OffsetInLayoutClass,
|
|
|
|
SubobjectOffsetMapTy &SubobjectOffsets,
|
|
|
|
SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
|
|
|
|
SubobjectCountMapTy &SubobjectCounts) {
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
unsigned SubobjectNumber = 0;
|
|
|
|
if (!IsVirtual)
|
|
|
|
SubobjectNumber = ++SubobjectCounts[RD];
|
|
|
|
|
|
|
|
// Set up the subobject to offset mapping.
|
|
|
|
assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber))
|
|
|
|
&& "Subobject offset already exists!");
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber))
|
2011-09-26 09:57:12 +08:00
|
|
|
&& "Subobject offset already exists!");
|
|
|
|
|
|
|
|
SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset();
|
|
|
|
SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] =
|
|
|
|
OffsetInLayoutClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Traverse our bases.
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
CharUnits BaseOffset;
|
|
|
|
CharUnits BaseOffsetInLayoutClass;
|
2014-03-26 16:22:48 +08:00
|
|
|
if (B.isVirtual()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if we've visited this virtual base before.
|
|
|
|
if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
2018-07-31 03:24:48 +08:00
|
|
|
BaseOffsetInLayoutClass =
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
} else {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
CharUnits Offset = Layout.getBaseClassOffset(BaseDecl);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseOffset = Base.getBaseOffset() + Offset;
|
|
|
|
BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset),
|
|
|
|
B.isVirtual(), BaseOffsetInLayoutClass,
|
|
|
|
SubobjectOffsets, SubobjectLayoutClassOffsets,
|
2011-09-26 09:57:12 +08:00
|
|
|
SubobjectCounts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base,
|
|
|
|
VisitedVirtualBasesSetTy &VisitedVirtualBases) {
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Ignore bases that don't have any virtual member functions.
|
|
|
|
if (!BaseDecl->isPolymorphic())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CharUnits BaseOffset;
|
2014-03-26 16:22:48 +08:00
|
|
|
if (B.isVirtual()) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!VisitedVirtualBases.insert(BaseDecl).second) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// We've visited this base before.
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
} else {
|
|
|
|
BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases);
|
|
|
|
}
|
|
|
|
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << "Final overriders for (";
|
|
|
|
RD->printQualifiedName(Out);
|
|
|
|
Out << ", ";
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << Base.getBaseOffset().getQuantity() << ")\n";
|
|
|
|
|
|
|
|
// Now dump the overriders for this base subobject.
|
2014-03-14 00:36:16 +08:00
|
|
|
for (const auto *MD : RD->methods()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
2015-02-26 03:17:48 +08:00
|
|
|
MD = MD->getCanonicalDecl();
|
2015-02-25 18:50:06 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
|
|
|
|
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << " ";
|
|
|
|
MD->printQualifiedName(Out);
|
|
|
|
Out << " - (";
|
|
|
|
Overrider.Method->printQualifiedName(Out);
|
2013-06-05 14:40:07 +08:00
|
|
|
Out << ", " << Overrider.Offset.getQuantity() << ')';
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
BaseOffset Offset;
|
|
|
|
if (!Overrider.Method->isPure())
|
|
|
|
Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
|
|
|
|
|
|
|
|
if (!Offset.isEmpty()) {
|
|
|
|
Out << " [ret-adj: ";
|
2014-01-04 02:42:48 +08:00
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
Offset.VirtualBase->printQualifiedName(Out);
|
|
|
|
Out << " vbase, ";
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << "\n";
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
|
|
|
|
struct VCallOffsetMap {
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Offsets - Keeps track of methods and their offsets.
|
|
|
|
// FIXME: This should be a real map and not a vector.
|
|
|
|
SmallVector<MethodAndOffsetPairTy, 16> Offsets;
|
|
|
|
|
|
|
|
/// MethodsCanShareVCallOffset - Returns whether two virtual member functions
|
|
|
|
/// can share the same vcall offset.
|
|
|
|
static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
|
|
|
|
const CXXMethodDecl *RHS);
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
|
|
|
|
/// add was successful, or false if there was already a member function with
|
|
|
|
/// the same signature in the map.
|
|
|
|
bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
|
|
|
|
/// vtable address point) for the given virtual member function.
|
|
|
|
CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// empty - Return whether the offset map is empty or not.
|
|
|
|
bool empty() const { return Offsets.empty(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
|
|
|
|
const CXXMethodDecl *RHS) {
|
2012-03-21 14:57:19 +08:00
|
|
|
const FunctionProtoType *LT =
|
|
|
|
cast<FunctionProtoType>(LHS->getType().getCanonicalType());
|
|
|
|
const FunctionProtoType *RT =
|
|
|
|
cast<FunctionProtoType>(RHS->getType().getCanonicalType());
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Fast-path matches in the canonical types.
|
|
|
|
if (LT == RT) return true;
|
|
|
|
|
|
|
|
// Force the signatures to match. We can't rely on the overrides
|
|
|
|
// list here because there isn't necessarily an inheritance
|
|
|
|
// relationship between the two methods.
|
2019-01-28 19:37:49 +08:00
|
|
|
if (LT->getMethodQuals() != RT->getMethodQuals())
|
2011-09-26 09:57:12 +08:00
|
|
|
return false;
|
2015-07-26 01:10:49 +08:00
|
|
|
return LT->getParamTypes() == RT->getParamTypes();
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
|
|
|
|
const CXXMethodDecl *RHS) {
|
|
|
|
assert(LHS->isVirtual() && "LHS must be virtual!");
|
|
|
|
assert(RHS->isVirtual() && "LHS must be virtual!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// A destructor can share a vcall offset with another destructor.
|
|
|
|
if (isa<CXXDestructorDecl>(LHS))
|
|
|
|
return isa<CXXDestructorDecl>(RHS);
|
|
|
|
|
|
|
|
// FIXME: We need to check more things here.
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// The methods must have the same name.
|
|
|
|
DeclarationName LHSName = LHS->getDeclName();
|
|
|
|
DeclarationName RHSName = RHS->getDeclName();
|
|
|
|
if (LHSName != RHSName)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// And the same signatures.
|
|
|
|
return HasSameVirtualSignature(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits OffsetOffset) {
|
|
|
|
// Check if we can reuse an offset.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &OffsetPair : Offsets) {
|
|
|
|
if (MethodsCanShareVCallOffset(OffsetPair.first, MD))
|
2011-09-26 09:57:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Add the offset.
|
|
|
|
Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
|
|
|
|
// Look for an offset.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &OffsetPair : Offsets) {
|
|
|
|
if (MethodsCanShareVCallOffset(OffsetPair.first, MD))
|
|
|
|
return OffsetPair.second;
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
llvm_unreachable("Should always find a vcall offset offset!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
|
|
|
|
class VCallAndVBaseOffsetBuilder {
|
|
|
|
public:
|
2018-07-31 03:24:48 +08:00
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
|
2011-09-26 09:57:12 +08:00
|
|
|
VBaseOffsetOffsetsMapTy;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// MostDerivedClass - The most derived class for which we're building vcall
|
|
|
|
/// and vbase offsets.
|
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// LayoutClass - The class we're using for layout information. Will be
|
2011-09-26 09:57:12 +08:00
|
|
|
/// different than the most derived class if we're building a construction
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Context - The ASTContext which we will use for layout information.
|
|
|
|
ASTContext &Context;
|
|
|
|
|
|
|
|
/// Components - vcall and vbase offset components
|
|
|
|
typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy;
|
|
|
|
VTableComponentVectorTy Components;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// VisitedVirtualBases - Visited virtual bases.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// VCallOffsets - Keeps track of vcall offsets.
|
|
|
|
VCallOffsetMap VCallOffsets;
|
|
|
|
|
|
|
|
|
|
|
|
/// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
|
|
|
|
/// relative to the address point.
|
|
|
|
VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// FinalOverriders - The final overriders of the most derived class.
|
|
|
|
/// (Can be null when we're not building a vtable of the most derived class).
|
|
|
|
const FinalOverriders *Overriders;
|
|
|
|
|
|
|
|
/// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
|
|
|
|
/// given base subobject.
|
|
|
|
void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
|
|
|
|
CharUnits RealBaseOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// AddVCallOffsets - Add vcall offsets for the given base subobject.
|
|
|
|
void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// AddVBaseOffsets - Add vbase offsets for the given class.
|
2018-07-31 03:24:48 +08:00
|
|
|
void AddVBaseOffsets(const CXXRecordDecl *Base,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits OffsetInLayoutClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
|
|
|
|
/// chars, relative to the vtable address point.
|
|
|
|
CharUnits getCurrentOffsetOffset() const;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
public:
|
|
|
|
VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
|
|
|
|
const CXXRecordDecl *LayoutClass,
|
|
|
|
const FinalOverriders *Overriders,
|
|
|
|
BaseSubobject Base, bool BaseIsVirtual,
|
|
|
|
CharUnits OffsetInLayoutClass)
|
2018-07-31 03:24:48 +08:00
|
|
|
: MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
|
2011-09-26 09:57:12 +08:00
|
|
|
Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Add vcall and vbase offsets.
|
|
|
|
AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Methods for iterating over the components.
|
|
|
|
typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
|
|
|
|
const_iterator components_begin() const { return Components.rbegin(); }
|
|
|
|
const_iterator components_end() const { return Components.rend(); }
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
|
|
|
|
const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
|
|
|
|
return VBaseOffsetOffsets;
|
|
|
|
}
|
|
|
|
};
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
void
|
2011-09-26 09:57:12 +08:00
|
|
|
VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
|
|
|
|
bool BaseIsVirtual,
|
|
|
|
CharUnits RealBaseOffset) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// ..in classes sharing a virtual table with a primary base class, the vcall
|
|
|
|
// and vbase offsets added by the derived class all come before the vcall
|
|
|
|
// and vbase offsets required by the base class, so that the latter may be
|
|
|
|
// laid out as required by the base class without regard to additions from
|
|
|
|
// the derived class(es).
|
|
|
|
|
|
|
|
// (Since we're emitting the vcall and vbase offsets in reverse order, we'll
|
|
|
|
// emit them for the primary base first).
|
|
|
|
if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
|
|
|
bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
|
|
|
|
|
|
|
|
CharUnits PrimaryBaseOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Get the base offset of the primary base.
|
|
|
|
if (PrimaryBaseIsVirtual) {
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary vbase should have a zero offset!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
PrimaryBaseOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
} else {
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary base should have a zero offset!");
|
|
|
|
|
|
|
|
PrimaryBaseOffset = Base.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddVCallAndVBaseOffsets(
|
|
|
|
BaseSubobject(PrimaryBase,PrimaryBaseOffset),
|
|
|
|
PrimaryBaseIsVirtual, RealBaseOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddVBaseOffsets(Base.getBase(), RealBaseOffset);
|
|
|
|
|
|
|
|
// We only want to add vcall offsets for virtual bases.
|
|
|
|
if (BaseIsVirtual)
|
|
|
|
AddVCallOffsets(Base, RealBaseOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
|
2018-07-31 03:24:48 +08:00
|
|
|
// OffsetIndex is the index of this vcall or vbase offset, relative to the
|
2011-09-26 09:57:12 +08:00
|
|
|
// vtable address point. (We subtract 3 to account for the information just
|
|
|
|
// above the address point, the RTTI info, the offset to top, and the
|
|
|
|
// vcall offset itself).
|
|
|
|
int64_t OffsetIndex = -(int64_t)(3 + Components.size());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
CharUnits PointerWidth =
|
2011-09-26 09:57:12 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
|
|
|
CharUnits OffsetOffset = PointerWidth * OffsetIndex;
|
|
|
|
return OffsetOffset;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits VBaseOffset) {
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
|
|
|
// Handle the primary base first.
|
|
|
|
// We only want to add vcall offsets if the base is non-virtual; a virtual
|
|
|
|
// primary base will have its vcall and vbase offsets emitted already.
|
|
|
|
if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
|
|
|
|
// Get the base offset of the primary base.
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary base should have a zero offset!");
|
|
|
|
|
|
|
|
AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
|
|
|
|
VBaseOffset);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Add the vcall offsets.
|
2014-03-14 00:36:16 +08:00
|
|
|
for (const auto *MD : RD->methods()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
2015-02-26 03:17:48 +08:00
|
|
|
MD = MD->getCanonicalDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
CharUnits OffsetOffset = getCurrentOffsetOffset();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Don't add a vcall offset if we already have one for this member function
|
|
|
|
// signature.
|
|
|
|
if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CharUnits Offset = CharUnits::Zero();
|
|
|
|
|
|
|
|
if (Overriders) {
|
|
|
|
// Get the final overrider.
|
2018-07-31 03:24:48 +08:00
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
2011-09-26 09:57:12 +08:00
|
|
|
Overriders->getOverrider(MD, Base.getBaseOffset());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// The vcall offset is the offset from the virtual base to the object
|
2011-09-26 09:57:12 +08:00
|
|
|
/// where the function was overridden.
|
|
|
|
Offset = Overrider.Offset - VBaseOffset;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Components.push_back(
|
|
|
|
VTableComponent::MakeVCallOffset(Offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
// And iterate over all non-virtual bases (ignoring the primary base).
|
2018-07-31 03:24:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
2014-03-26 16:22:48 +08:00
|
|
|
if (B.isVirtual())
|
2011-09-26 09:57:12 +08:00
|
|
|
continue;
|
|
|
|
|
2014-03-26 16:22:48 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
if (BaseDecl == PrimaryBase)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the base offset of this base.
|
2018-07-31 03:24:48 +08:00
|
|
|
CharUnits BaseOffset = Base.getBaseOffset() +
|
2011-09-26 09:57:12 +08:00
|
|
|
Layout.getBaseClassOffset(BaseDecl);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
|
2011-09-26 09:57:12 +08:00
|
|
|
VBaseOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void
|
2011-09-26 09:57:12 +08:00
|
|
|
VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
|
|
|
|
CharUnits OffsetInLayoutClass) {
|
2018-07-31 03:24:48 +08:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
2011-09-26 09:57:12 +08:00
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
// Add vbase offsets.
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Check if this is a virtual base that we haven't visited before.
|
2014-11-19 15:49:47 +08:00
|
|
|
if (B.isVirtual() && VisitedVirtualBases.insert(BaseDecl).second) {
|
2018-07-31 03:24:48 +08:00
|
|
|
CharUnits Offset =
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
|
|
|
|
|
|
|
|
// Add the vbase offset offset.
|
|
|
|
assert(!VBaseOffsetOffsets.count(BaseDecl) &&
|
|
|
|
"vbase offset offset already exists!");
|
|
|
|
|
|
|
|
CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
|
|
|
|
VBaseOffsetOffsets.insert(
|
|
|
|
std::make_pair(BaseDecl, VBaseOffsetOffset));
|
|
|
|
|
|
|
|
Components.push_back(
|
|
|
|
VTableComponent::MakeVBaseOffset(Offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the base class looking for more vbase offsets.
|
|
|
|
AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
/// ItaniumVTableBuilder - Class for building vtable layout information.
|
|
|
|
class ItaniumVTableBuilder {
|
2011-09-26 09:57:12 +08:00
|
|
|
public:
|
2018-07-31 03:24:48 +08:00
|
|
|
/// PrimaryBasesSetVectorTy - A set vector of direct and indirect
|
2011-09-26 09:57:12 +08:00
|
|
|
/// primary bases.
|
2018-07-31 03:24:48 +08:00
|
|
|
typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
|
2011-09-26 09:57:12 +08:00
|
|
|
PrimaryBasesSetVectorTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
|
2011-09-26 09:57:12 +08:00
|
|
|
VBaseOffsetOffsetsMapTy;
|
2016-12-14 04:40:39 +08:00
|
|
|
|
|
|
|
typedef VTableLayout::AddressPointsMapTy AddressPointsMapTy;
|
2011-09-26 09:57:12 +08:00
|
|
|
|
2013-06-05 22:05:50 +08:00
|
|
|
typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
private:
|
|
|
|
/// VTables - Global vtable information.
|
2013-10-09 19:33:51 +08:00
|
|
|
ItaniumVTableContext &VTables;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// MostDerivedClass - The most derived class for which we're building this
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
|
|
|
|
|
|
|
/// MostDerivedClassOffset - If we're building a construction vtable, this
|
|
|
|
/// holds the offset from the layout class to the most derived class.
|
|
|
|
const CharUnits MostDerivedClassOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
|
2011-09-26 09:57:12 +08:00
|
|
|
/// base. (This only makes sense when building a construction vtable).
|
|
|
|
bool MostDerivedClassIsVirtual;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// LayoutClass - The class we're using for layout information. Will be
|
2011-09-26 09:57:12 +08:00
|
|
|
/// different than the most derived class if we're building a construction
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Context - The ASTContext which we will use for layout information.
|
|
|
|
ASTContext &Context;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// FinalOverriders - The final overriders of the most derived class.
|
|
|
|
const FinalOverriders Overriders;
|
|
|
|
|
|
|
|
/// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
|
|
|
|
/// bases in this vtable.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
|
|
|
|
|
|
|
|
/// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
|
|
|
|
/// the most derived class.
|
|
|
|
VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
|
2016-12-14 04:40:39 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Components - The components of the vtable being built.
|
|
|
|
SmallVector<VTableComponent, 64> Components;
|
|
|
|
|
|
|
|
/// AddressPoints - Address points for the vtable being built.
|
|
|
|
AddressPointsMapTy AddressPoints;
|
|
|
|
|
|
|
|
/// MethodInfo - Contains information about a method in a vtable.
|
|
|
|
/// (Used for computing 'this' pointer adjustment thunks.
|
|
|
|
struct MethodInfo {
|
|
|
|
/// BaseOffset - The base offset of this method.
|
|
|
|
const CharUnits BaseOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// BaseOffsetInLayoutClass - The base offset in the layout class of this
|
|
|
|
/// method.
|
|
|
|
const CharUnits BaseOffsetInLayoutClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// VTableIndex - The index in the vtable that this method has.
|
|
|
|
/// (For destructors, this is the index of the complete destructor).
|
|
|
|
const uint64_t VTableIndex;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
|
2011-09-26 09:57:12 +08:00
|
|
|
uint64_t VTableIndex)
|
2018-07-31 03:24:48 +08:00
|
|
|
: BaseOffset(BaseOffset),
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
|
|
|
|
VTableIndex(VTableIndex) { }
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
MethodInfo()
|
|
|
|
: BaseOffset(CharUnits::Zero()),
|
|
|
|
BaseOffsetInLayoutClass(CharUnits::Zero()),
|
2011-09-26 09:57:12 +08:00
|
|
|
VTableIndex(0) { }
|
2019-01-21 05:19:56 +08:00
|
|
|
|
|
|
|
MethodInfo(MethodInfo const&) = default;
|
2011-09-26 09:57:12 +08:00
|
|
|
};
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// MethodInfoMap - The information for all methods in the vtable we're
|
|
|
|
/// currently building.
|
|
|
|
MethodInfoMapTy MethodInfoMap;
|
2013-06-05 22:05:50 +08:00
|
|
|
|
|
|
|
/// MethodVTableIndices - Contains the index (relative to the vtable address
|
|
|
|
/// point) where the function pointer for a virtual function is stored.
|
|
|
|
MethodVTableIndicesTy MethodVTableIndices;
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// VTableThunks - The thunks by vtable index in the vtable currently being
|
2011-09-26 09:57:12 +08:00
|
|
|
/// built.
|
|
|
|
VTableThunksMapTy VTableThunks;
|
|
|
|
|
|
|
|
typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
|
|
|
|
typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Thunks - A map that contains all the thunks needed for all methods in the
|
|
|
|
/// most derived class for which the vtable is currently being built.
|
|
|
|
ThunksMapTy Thunks;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// AddThunk - Add a thunk for the given method.
|
|
|
|
void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
|
|
|
|
/// part of the vtable we're currently building.
|
|
|
|
void ComputeThisAdjustments();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
|
|
|
|
|
|
|
|
/// PrimaryVirtualBases - All known virtual bases who are a primary base of
|
|
|
|
/// some other base.
|
|
|
|
VisitedVirtualBasesSetTy PrimaryVirtualBases;
|
|
|
|
|
|
|
|
/// ComputeReturnAdjustment - Compute the return adjustment given a return
|
|
|
|
/// adjustment base offset.
|
|
|
|
ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
|
|
|
|
/// the 'this' pointer from the base subobject to the derived subobject.
|
|
|
|
BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
|
|
|
|
BaseSubobject Derived) const;
|
|
|
|
|
|
|
|
/// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
|
|
|
|
/// given virtual member function, its offset in the layout class and its
|
|
|
|
/// final overrider.
|
2018-07-31 03:24:48 +08:00
|
|
|
ThisAdjustment
|
|
|
|
ComputeThisAdjustment(const CXXMethodDecl *MD,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits BaseOffsetInLayoutClass,
|
|
|
|
FinalOverriders::OverriderInfo Overrider);
|
|
|
|
|
|
|
|
/// AddMethod - Add a single virtual member function to the vtable
|
|
|
|
/// components vector.
|
|
|
|
void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
|
|
|
|
|
|
|
|
/// IsOverriderUsed - Returns whether the overrider will ever be used in this
|
2018-07-31 03:24:48 +08:00
|
|
|
/// part of the vtable.
|
2011-09-26 09:57:12 +08:00
|
|
|
///
|
|
|
|
/// Itanium C++ ABI 2.5.2:
|
|
|
|
///
|
|
|
|
/// struct A { virtual void f(); };
|
|
|
|
/// struct B : virtual public A { int i; };
|
|
|
|
/// struct C : virtual public A { int j; };
|
|
|
|
/// struct D : public B, public C {};
|
|
|
|
///
|
|
|
|
/// When B and C are declared, A is a primary base in each case, so although
|
|
|
|
/// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
|
|
|
|
/// adjustment is required and no thunk is generated. However, inside D
|
|
|
|
/// objects, A is no longer a primary base of C, so if we allowed calls to
|
|
|
|
/// C::f() to use the copy of A's vtable in the C subobject, we would need
|
2018-07-31 03:24:48 +08:00
|
|
|
/// to adjust this from C* to B::A*, which would require a third-party
|
|
|
|
/// thunk. Since we require that a call to C::f() first convert to A*,
|
|
|
|
/// C-in-D's copy of A's vtable is never referenced, so this is not
|
2011-09-26 09:57:12 +08:00
|
|
|
/// necessary.
|
|
|
|
bool IsOverriderUsed(const CXXMethodDecl *Overrider,
|
|
|
|
CharUnits BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
CharUnits FirstBaseOffsetInLayoutClass) const;
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// AddMethods - Add the methods of this base subobject and all its
|
|
|
|
/// primary bases to the vtable components vector.
|
|
|
|
void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
CharUnits FirstBaseOffsetInLayoutClass,
|
|
|
|
PrimaryBasesSetVectorTy &PrimaryBases);
|
|
|
|
|
|
|
|
// LayoutVTable - Layout the vtable for the given base class, including its
|
|
|
|
// secondary vtables and any vtables for virtual bases.
|
|
|
|
void LayoutVTable();
|
|
|
|
|
|
|
|
/// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
|
|
|
|
/// given base subobject, as well as all its secondary vtables.
|
|
|
|
///
|
|
|
|
/// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
|
|
|
|
/// or a direct or indirect base of a virtual base.
|
|
|
|
///
|
|
|
|
/// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
|
2018-07-31 03:24:48 +08:00
|
|
|
/// in the layout class.
|
2011-09-26 09:57:12 +08:00
|
|
|
void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
|
|
|
|
bool BaseIsMorallyVirtual,
|
|
|
|
bool BaseIsVirtualInLayoutClass,
|
|
|
|
CharUnits OffsetInLayoutClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// LayoutSecondaryVTables - Layout the secondary vtables for the given base
|
|
|
|
/// subobject.
|
|
|
|
///
|
|
|
|
/// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
|
|
|
|
/// or a direct or indirect base of a virtual base.
|
|
|
|
void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
|
|
|
|
CharUnits OffsetInLayoutClass);
|
|
|
|
|
|
|
|
/// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
|
|
|
|
/// class hierarchy.
|
2018-07-31 03:24:48 +08:00
|
|
|
void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits OffsetInLayoutClass,
|
|
|
|
VisitedVirtualBasesSetTy &VBases);
|
|
|
|
|
|
|
|
/// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
|
|
|
|
/// given base (excluding any primary bases).
|
2018-07-31 03:24:48 +08:00
|
|
|
void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
|
2011-09-26 09:57:12 +08:00
|
|
|
VisitedVirtualBasesSetTy &VBases);
|
|
|
|
|
|
|
|
/// isBuildingConstructionVTable - Return whether this vtable builder is
|
|
|
|
/// building a construction vtable.
|
2018-07-31 03:24:48 +08:00
|
|
|
bool isBuildingConstructorVTable() const {
|
2011-09-26 09:57:12 +08:00
|
|
|
return MostDerivedClass != LayoutClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-12-14 04:40:39 +08:00
|
|
|
/// Component indices of the first component of each of the vtables in the
|
|
|
|
/// vtable group.
|
|
|
|
SmallVector<size_t, 4> VTableIndices;
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
ItaniumVTableBuilder(ItaniumVTableContext &VTables,
|
|
|
|
const CXXRecordDecl *MostDerivedClass,
|
|
|
|
CharUnits MostDerivedClassOffset,
|
|
|
|
bool MostDerivedClassIsVirtual,
|
|
|
|
const CXXRecordDecl *LayoutClass)
|
|
|
|
: VTables(VTables), MostDerivedClass(MostDerivedClass),
|
|
|
|
MostDerivedClassOffset(MostDerivedClassOffset),
|
|
|
|
MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
|
|
|
|
LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
|
|
|
|
Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
|
2013-10-09 17:23:58 +08:00
|
|
|
assert(!Context.getTargetInfo().getCXXABI().isMicrosoft());
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
LayoutVTable();
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Context.getLangOpts().DumpVTableLayouts)
|
2013-11-09 05:28:00 +08:00
|
|
|
dumpLayout(llvm::outs());
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t getNumThunks() const {
|
|
|
|
return Thunks.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThunksMapTy::const_iterator thunks_begin() const {
|
|
|
|
return Thunks.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThunksMapTy::const_iterator thunks_end() const {
|
|
|
|
return Thunks.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
|
|
|
|
return VBaseOffsetOffsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AddressPointsMapTy &getAddressPoints() const {
|
|
|
|
return AddressPoints;
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:05:50 +08:00
|
|
|
MethodVTableIndicesTy::const_iterator vtable_indices_begin() const {
|
|
|
|
return MethodVTableIndices.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
MethodVTableIndicesTy::const_iterator vtable_indices_end() const {
|
|
|
|
return MethodVTableIndices.end();
|
|
|
|
}
|
|
|
|
|
2016-12-14 04:40:39 +08:00
|
|
|
ArrayRef<VTableComponent> vtable_components() const { return Components; }
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
AddressPointsMapTy::const_iterator address_points_begin() const {
|
|
|
|
return AddressPoints.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressPointsMapTy::const_iterator address_points_end() const {
|
|
|
|
return AddressPoints.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
|
|
|
|
return VTableThunks.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
VTableThunksMapTy::const_iterator vtable_thunks_end() const {
|
|
|
|
return VTableThunks.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dumpLayout - Dump the vtable layout.
|
|
|
|
void dumpLayout(raw_ostream&);
|
|
|
|
};
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::AddThunk(const CXXMethodDecl *MD,
|
|
|
|
const ThunkInfo &Thunk) {
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(!isBuildingConstructorVTable() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Can't add thunks for construction vtable");
|
|
|
|
|
2013-07-06 03:34:19 +08:00
|
|
|
SmallVectorImpl<ThunkInfo> &ThunksVector = Thunks[MD];
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if we have this thunk already.
|
2019-03-31 16:48:19 +08:00
|
|
|
if (llvm::find(ThunksVector, Thunk) != ThunksVector.end())
|
2011-09-26 09:57:12 +08:00
|
|
|
return;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
ThunksVector.push_back(Thunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
/// Visit all the methods overridden by the given method recursively,
|
|
|
|
/// in a depth-first pre-order. The Visitor's visitor method returns a bool
|
|
|
|
/// indicating whether to continue the recursion for the given overridden
|
|
|
|
/// method (i.e. returning false stops the iteration).
|
|
|
|
template <class VisitorTy>
|
|
|
|
static void
|
|
|
|
visitAllOverriddenMethods(const CXXMethodDecl *MD, VisitorTy &Visitor) {
|
2011-09-26 09:57:12 +08:00
|
|
|
assert(MD->isVirtual() && "Method is not virtual!");
|
|
|
|
|
2017-12-18 07:52:45 +08:00
|
|
|
for (const CXXMethodDecl *OverriddenMD : MD->overridden_methods()) {
|
2015-07-26 00:31:30 +08:00
|
|
|
if (!Visitor(OverriddenMD))
|
2013-07-30 17:46:19 +08:00
|
|
|
continue;
|
|
|
|
visitAllOverriddenMethods(OverriddenMD, Visitor);
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
|
|
|
|
/// the overridden methods that the function decl overrides.
|
|
|
|
static void
|
|
|
|
ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
|
|
|
|
OverriddenMethodsSetTy& OverriddenMethods) {
|
2015-07-26 00:31:30 +08:00
|
|
|
auto OverriddenMethodsCollector = [&](const CXXMethodDecl *MD) {
|
|
|
|
// Don't recurse on this method if we've already collected it.
|
|
|
|
return OverriddenMethods.insert(MD).second;
|
|
|
|
};
|
|
|
|
visitAllOverriddenMethods(MD, OverriddenMethodsCollector);
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::ComputeThisAdjustments() {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Now go through the method info map and see if any of the methods need
|
|
|
|
// 'this' pointer adjustments.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &MI : MethodInfoMap) {
|
|
|
|
const CXXMethodDecl *MD = MI.first;
|
|
|
|
const MethodInfo &MethodInfo = MI.second;
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Ignore adjustments for unused function pointers.
|
|
|
|
uint64_t VTableIndex = MethodInfo.VTableIndex;
|
2018-07-31 03:24:48 +08:00
|
|
|
if (Components[VTableIndex].getKind() ==
|
2011-09-26 09:57:12 +08:00
|
|
|
VTableComponent::CK_UnusedFunctionPointer)
|
|
|
|
continue;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Get the final overrider for this method.
|
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
|
|
|
Overriders.getOverrider(MD, MethodInfo.BaseOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if we need an adjustment at all.
|
|
|
|
if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
|
|
|
|
// When a return thunk is needed by a derived class that overrides a
|
2018-07-31 03:24:48 +08:00
|
|
|
// virtual base, gcc uses a virtual 'this' adjustment as well.
|
2011-09-26 09:57:12 +08:00
|
|
|
// While the thunk itself might be needed by vtables in subclasses or
|
|
|
|
// in construction vtables, there doesn't seem to be a reason for using
|
|
|
|
// the thunk in this vtable. Still, we do so to match gcc.
|
|
|
|
if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThisAdjustment ThisAdjustment =
|
|
|
|
ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
|
|
|
|
|
|
|
|
if (ThisAdjustment.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add it.
|
|
|
|
VTableThunks[VTableIndex].This = ThisAdjustment;
|
|
|
|
|
2013-09-27 22:48:01 +08:00
|
|
|
if (isa<CXXDestructorDecl>(MD)) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Add an adjustment for the deleting destructor as well.
|
|
|
|
VTableThunks[VTableIndex + 1].This = ThisAdjustment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear the method info map.
|
|
|
|
MethodInfoMap.clear();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (isBuildingConstructorVTable()) {
|
|
|
|
// We don't need to store thunk information for construction vtables.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &TI : VTableThunks) {
|
|
|
|
const VTableComponent &Component = Components[TI.first];
|
|
|
|
const ThunkInfo &Thunk = TI.second;
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXMethodDecl *MD;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
switch (Component.getKind()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected vtable component kind!");
|
|
|
|
case VTableComponent::CK_FunctionPointer:
|
|
|
|
MD = Component.getFunctionDecl();
|
|
|
|
break;
|
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
|
|
|
MD = Component.getDestructorDecl();
|
|
|
|
break;
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer:
|
|
|
|
// We've already added the thunk when we saw the complete dtor pointer.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MD->getParent() == MostDerivedClass)
|
|
|
|
AddThunk(MD, Thunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
ReturnAdjustment
|
|
|
|
ItaniumVTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
|
2011-09-26 09:57:12 +08:00
|
|
|
ReturnAdjustment Adjustment;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!Offset.isEmpty()) {
|
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
// Get the virtual base offset offset.
|
|
|
|
if (Offset.DerivedClass == MostDerivedClass) {
|
|
|
|
// We can get the offset offset directly from our map.
|
2013-10-30 19:55:43 +08:00
|
|
|
Adjustment.Virtual.Itanium.VBaseOffsetOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity();
|
|
|
|
} else {
|
2013-10-30 19:55:43 +08:00
|
|
|
Adjustment.Virtual.Itanium.VBaseOffsetOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
|
|
|
|
Offset.VirtualBase).getQuantity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
return Adjustment;
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
BaseOffset ItaniumVTableBuilder::ComputeThisAdjustmentBaseOffset(
|
|
|
|
BaseSubobject Base, BaseSubobject Derived) const {
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXRecordDecl *BaseRD = Base.getBase();
|
|
|
|
const CXXRecordDecl *DerivedRD = Derived.getBase();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/true,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/true);
|
|
|
|
|
2013-02-04 02:55:34 +08:00
|
|
|
if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
|
2011-09-26 09:57:12 +08:00
|
|
|
llvm_unreachable("Class must be derived from the passed in base class!");
|
|
|
|
|
|
|
|
// We have to go through all the paths, and see which one leads us to the
|
|
|
|
// right base subobject.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXBasePath &Path : Paths) {
|
|
|
|
BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, Path);
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
// If we have a virtual base class, the non-virtual offset is relative
|
|
|
|
// to the virtual base class offset.
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
/// Get the virtual base offset, relative to the most derived class
|
2011-09-26 09:57:12 +08:00
|
|
|
/// layout.
|
2018-07-31 03:24:48 +08:00
|
|
|
OffsetToBaseSubobject +=
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
|
|
|
|
} else {
|
2018-07-31 03:24:48 +08:00
|
|
|
// Otherwise, the non-virtual offset is relative to the derived class
|
2011-09-26 09:57:12 +08:00
|
|
|
// offset.
|
|
|
|
OffsetToBaseSubobject += Derived.getBaseOffset();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if this path gives us the right base subobject.
|
|
|
|
if (OffsetToBaseSubobject == Base.getBaseOffset()) {
|
|
|
|
// Since we're going from the base class _to_ the derived class, we'll
|
|
|
|
// invert the non-virtual offset here.
|
|
|
|
Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
|
|
|
|
return Offset;
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
return BaseOffset();
|
|
|
|
}
|
2013-10-09 19:33:51 +08:00
|
|
|
|
|
|
|
ThisAdjustment ItaniumVTableBuilder::ComputeThisAdjustment(
|
|
|
|
const CXXMethodDecl *MD, CharUnits BaseOffsetInLayoutClass,
|
|
|
|
FinalOverriders::OverriderInfo Overrider) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Ignore adjustments for pure virtual member functions.
|
|
|
|
if (Overrider.Method->isPure())
|
|
|
|
return ThisAdjustment();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
BaseSubobject OverriddenBaseSubobject(MD->getParent(),
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseOffsetInLayoutClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
|
|
|
|
Overrider.Offset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Compute the adjustment offset.
|
|
|
|
BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
|
|
|
|
OverriderBaseSubobject);
|
|
|
|
if (Offset.isEmpty())
|
|
|
|
return ThisAdjustment();
|
|
|
|
|
|
|
|
ThisAdjustment Adjustment;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
// Get the vcall offset map for this virtual base.
|
|
|
|
VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
|
|
|
|
|
|
|
|
if (VCallOffsets.empty()) {
|
|
|
|
// We don't have vcall offsets for this virtual base, go ahead and
|
|
|
|
// build them.
|
|
|
|
VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*Overriders=*/nullptr,
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseSubobject(Offset.VirtualBase,
|
|
|
|
CharUnits::Zero()),
|
|
|
|
/*BaseIsVirtual=*/true,
|
|
|
|
/*OffsetInLayoutClass=*/
|
|
|
|
CharUnits::Zero());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
VCallOffsets = Builder.getVCallOffsets();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
Adjustment.Virtual.Itanium.VCallOffsetOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the non-virtual part of the adjustment.
|
|
|
|
Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
return Adjustment;
|
|
|
|
}
|
2013-10-09 19:33:51 +08:00
|
|
|
|
|
|
|
void ItaniumVTableBuilder::AddMethod(const CXXMethodDecl *MD,
|
|
|
|
ReturnAdjustment ReturnAdjustment) {
|
2011-09-26 09:57:12 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(ReturnAdjustment.isEmpty() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Destructor can't have return adjustment!");
|
|
|
|
|
2013-09-27 22:48:01 +08:00
|
|
|
// Add both the complete destructor and the deleting destructor.
|
|
|
|
Components.push_back(VTableComponent::MakeCompleteDtor(DD));
|
|
|
|
Components.push_back(VTableComponent::MakeDeletingDtor(DD));
|
2011-09-26 09:57:12 +08:00
|
|
|
} else {
|
|
|
|
// Add the return adjustment if necessary.
|
|
|
|
if (!ReturnAdjustment.isEmpty())
|
|
|
|
VTableThunks[Components.size()].Return = ReturnAdjustment;
|
|
|
|
|
|
|
|
// Add the function.
|
|
|
|
Components.push_back(VTableComponent::MakeFunction(MD));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// OverridesIndirectMethodInBase - Return whether the given member function
|
2018-07-31 03:24:48 +08:00
|
|
|
/// overrides any methods in the set of given bases.
|
2011-09-26 09:57:12 +08:00
|
|
|
/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
|
|
|
|
/// For example, if we have:
|
|
|
|
///
|
|
|
|
/// struct A { virtual void f(); }
|
|
|
|
/// struct B : A { virtual void f(); }
|
|
|
|
/// struct C : B { virtual void f(); }
|
|
|
|
///
|
2018-07-31 03:24:48 +08:00
|
|
|
/// OverridesIndirectMethodInBase will return true if given C::f as the method
|
2011-09-26 09:57:12 +08:00
|
|
|
/// and { A } as the set of bases.
|
2013-10-09 19:33:51 +08:00
|
|
|
static bool OverridesIndirectMethodInBases(
|
|
|
|
const CXXMethodDecl *MD,
|
|
|
|
ItaniumVTableBuilder::PrimaryBasesSetVectorTy &Bases) {
|
2011-09-26 09:57:12 +08:00
|
|
|
if (Bases.count(MD->getParent()))
|
|
|
|
return true;
|
2017-12-18 07:52:45 +08:00
|
|
|
|
|
|
|
for (const CXXMethodDecl *OverriddenMD : MD->overridden_methods()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check "indirect overriders".
|
|
|
|
if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
bool ItaniumVTableBuilder::IsOverriderUsed(
|
|
|
|
const CXXMethodDecl *Overrider, CharUnits BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
CharUnits FirstBaseOffsetInLayoutClass) const {
|
2011-09-26 09:57:12 +08:00
|
|
|
// If the base and the first base in the primary base chain have the same
|
|
|
|
// offsets, then this overrider will be used.
|
|
|
|
if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We know now that Base (or a direct or indirect base of it) is a primary
|
2018-07-31 03:24:48 +08:00
|
|
|
// base in part of the class hierarchy, but not a primary base in the most
|
2011-09-26 09:57:12 +08:00
|
|
|
// derived class.
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// If the overrider is the first base in the primary base chain, we know
|
|
|
|
// that the overrider will be used.
|
|
|
|
if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
|
|
|
|
return true;
|
2013-10-09 19:33:51 +08:00
|
|
|
|
|
|
|
ItaniumVTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
|
|
|
|
PrimaryBases.insert(RD);
|
|
|
|
|
|
|
|
// Now traverse the base chain, starting with the first base, until we find
|
|
|
|
// the base that is no longer a primary base.
|
|
|
|
while (true) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!PrimaryBase)
|
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (Layout.isPrimaryBaseVirtual()) {
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary base should always be at offset 0!");
|
|
|
|
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
// Now check if this is the primary base that is not a primary base in the
|
|
|
|
// most derived class.
|
|
|
|
if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
|
|
|
|
FirstBaseOffsetInLayoutClass) {
|
|
|
|
// We found it, stop walking the chain.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary base should always be at offset 0!");
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!PrimaryBases.insert(PrimaryBase))
|
|
|
|
llvm_unreachable("Found a duplicate primary base!");
|
|
|
|
|
|
|
|
RD = PrimaryBase;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// If the final overrider is an override of one of the primary bases,
|
|
|
|
// then we know that it will be used.
|
|
|
|
return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
|
|
|
|
}
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy;
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
/// FindNearestOverriddenMethod - Given a method, returns the overridden method
|
|
|
|
/// from the nearest base. Returns null if no method was found.
|
2013-07-30 17:46:19 +08:00
|
|
|
/// The Bases are expected to be sorted in a base-to-derived order.
|
|
|
|
static const CXXMethodDecl *
|
2011-09-26 09:57:12 +08:00
|
|
|
FindNearestOverriddenMethod(const CXXMethodDecl *MD,
|
2013-07-30 17:46:19 +08:00
|
|
|
BasesSetVectorTy &Bases) {
|
2011-09-26 09:57:12 +08:00
|
|
|
OverriddenMethodsSetTy OverriddenMethods;
|
|
|
|
ComputeAllOverriddenMethods(MD, OverriddenMethods);
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXRecordDecl *PrimaryBase :
|
|
|
|
llvm::make_range(Bases.rbegin(), Bases.rend())) {
|
2013-07-30 17:46:19 +08:00
|
|
|
// Now check the overridden methods.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXMethodDecl *OverriddenMD : OverriddenMethods) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// We found our overridden method.
|
|
|
|
if (OverriddenMD->getParent() == PrimaryBase)
|
|
|
|
return OverriddenMD;
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 13:36:57 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2013-10-09 19:33:51 +08:00
|
|
|
}
|
2011-09-26 09:57:12 +08:00
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::AddMethods(
|
|
|
|
BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
CharUnits FirstBaseOffsetInLayoutClass,
|
|
|
|
PrimaryBasesSetVectorTy &PrimaryBases) {
|
2013-06-05 22:05:50 +08:00
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// The order of the virtual function pointers in a virtual table is the
|
|
|
|
// order of declaration of the corresponding member functions in the class.
|
|
|
|
//
|
|
|
|
// There is an entry for any virtual function declared in a class,
|
|
|
|
// whether it is a new function or overrides a base class function,
|
|
|
|
// unless it overrides a function from the primary base, and conversion
|
|
|
|
// between their return types does not require an adjustment.
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
|
|
|
CharUnits PrimaryBaseOffset;
|
|
|
|
CharUnits PrimaryBaseOffsetInLayoutClass;
|
|
|
|
if (Layout.isPrimaryBaseVirtual()) {
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary vbase should have a zero offset!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
PrimaryBaseOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
PrimaryBaseOffsetInLayoutClass =
|
|
|
|
LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
} else {
|
2012-07-05 02:45:14 +08:00
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Primary base should have a zero offset!");
|
|
|
|
|
|
|
|
PrimaryBaseOffset = Base.getBaseOffset();
|
|
|
|
PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
|
2018-07-31 03:24:48 +08:00
|
|
|
PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
|
2011-09-26 09:57:12 +08:00
|
|
|
FirstBaseOffsetInLayoutClass, PrimaryBases);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!PrimaryBases.insert(PrimaryBase))
|
|
|
|
llvm_unreachable("Found a duplicate primary base!");
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:36:57 +08:00
|
|
|
const CXXDestructorDecl *ImplicitVirtualDtor = nullptr;
|
2013-06-05 22:05:50 +08:00
|
|
|
|
|
|
|
typedef llvm::SmallVector<const CXXMethodDecl *, 8> NewVirtualFunctionsTy;
|
|
|
|
NewVirtualFunctionsTy NewVirtualFunctions;
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Now go through all virtual member functions and add them.
|
2014-03-14 00:36:16 +08:00
|
|
|
for (const auto *MD : RD->methods()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
2015-02-26 03:17:48 +08:00
|
|
|
MD = MD->getCanonicalDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Get the final overrider.
|
2018-07-31 03:24:48 +08:00
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
2011-09-26 09:57:12 +08:00
|
|
|
Overriders.getOverrider(MD, Base.getBaseOffset());
|
|
|
|
|
|
|
|
// Check if this virtual member function overrides a method in a primary
|
|
|
|
// base. If this is the case, and the return type doesn't require adjustment
|
|
|
|
// then we can just use the member function from the primary base.
|
2018-07-31 03:24:48 +08:00
|
|
|
if (const CXXMethodDecl *OverriddenMD =
|
2011-09-26 09:57:12 +08:00
|
|
|
FindNearestOverriddenMethod(MD, PrimaryBases)) {
|
2018-07-31 03:24:48 +08:00
|
|
|
if (ComputeReturnAdjustmentBaseOffset(Context, MD,
|
2011-09-26 09:57:12 +08:00
|
|
|
OverriddenMD).isEmpty()) {
|
|
|
|
// Replace the method info of the overridden method with our own
|
|
|
|
// method.
|
2018-07-31 03:24:48 +08:00
|
|
|
assert(MethodInfoMap.count(OverriddenMD) &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Did not find the overridden method!");
|
|
|
|
MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
|
|
|
|
OverriddenMethodInfo.VTableIndex);
|
|
|
|
|
|
|
|
assert(!MethodInfoMap.count(MD) &&
|
|
|
|
"Should not have method info for this method yet!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
|
|
|
|
MethodInfoMap.erase(OverriddenMD);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// If the overridden method exists in a virtual base class or a direct
|
|
|
|
// or indirect base class of a virtual base class, we need to emit a
|
|
|
|
// thunk if we ever have a class hierarchy where the base class is not
|
|
|
|
// a primary base in the complete object.
|
|
|
|
if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
|
|
|
|
// Compute the this adjustment.
|
|
|
|
ThisAdjustment ThisAdjustment =
|
|
|
|
ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
|
|
|
|
Overrider);
|
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
if (ThisAdjustment.Virtual.Itanium.VCallOffsetOffset &&
|
2011-09-26 09:57:12 +08:00
|
|
|
Overrider.Method->getParent() == MostDerivedClass) {
|
|
|
|
|
|
|
|
// There's no return adjustment from OverriddenMD and MD,
|
|
|
|
// but that doesn't mean there isn't one between MD and
|
|
|
|
// the final overrider.
|
|
|
|
BaseOffset ReturnAdjustmentOffset =
|
|
|
|
ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
|
2018-07-31 03:24:48 +08:00
|
|
|
ReturnAdjustment ReturnAdjustment =
|
2011-09-26 09:57:12 +08:00
|
|
|
ComputeReturnAdjustment(ReturnAdjustmentOffset);
|
|
|
|
|
|
|
|
// This is a virtual thunk for the most derived class, add it.
|
2018-07-31 03:24:48 +08:00
|
|
|
AddThunk(Overrider.Method,
|
2011-09-26 09:57:12 +08:00
|
|
|
ThunkInfo(ThisAdjustment, ReturnAdjustment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:05:50 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
if (MD->isImplicit()) {
|
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// If a class has an implicitly-defined virtual destructor,
|
|
|
|
// its entries come after the declared virtual function pointers.
|
|
|
|
|
|
|
|
assert(!ImplicitVirtualDtor &&
|
|
|
|
"Did already see an implicit virtual dtor!");
|
|
|
|
ImplicitVirtualDtor = DD;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NewVirtualFunctions.push_back(MD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImplicitVirtualDtor)
|
|
|
|
NewVirtualFunctions.push_back(ImplicitVirtualDtor);
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXMethodDecl *MD : NewVirtualFunctions) {
|
2013-06-05 22:05:50 +08:00
|
|
|
// Get the final overrider.
|
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
|
|
|
Overriders.getOverrider(MD, Base.getBaseOffset());
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Insert the method info for this method.
|
|
|
|
MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
|
|
|
|
Components.size());
|
|
|
|
|
|
|
|
assert(!MethodInfoMap.count(MD) &&
|
|
|
|
"Should not have method info for this method yet!");
|
|
|
|
MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
|
|
|
|
|
|
|
|
// Check if this overrider is going to be used.
|
|
|
|
const CXXMethodDecl *OverriderMD = Overrider.Method;
|
|
|
|
if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
|
2018-07-31 03:24:48 +08:00
|
|
|
FirstBaseInPrimaryBaseChain,
|
2011-09-26 09:57:12 +08:00
|
|
|
FirstBaseOffsetInLayoutClass)) {
|
|
|
|
Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-05 22:05:50 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if this overrider needs a return adjustment.
|
|
|
|
// We don't want to do this for pure virtual member functions.
|
|
|
|
BaseOffset ReturnAdjustmentOffset;
|
|
|
|
if (!OverriderMD->isPure()) {
|
2018-07-31 03:24:48 +08:00
|
|
|
ReturnAdjustmentOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
ReturnAdjustment ReturnAdjustment =
|
2011-09-26 09:57:12 +08:00
|
|
|
ComputeReturnAdjustment(ReturnAdjustmentOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
AddMethod(Overrider.Method, ReturnAdjustment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::LayoutVTable() {
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
|
|
|
|
CharUnits::Zero()),
|
|
|
|
/*BaseIsMorallyVirtual=*/false,
|
|
|
|
MostDerivedClassIsVirtual,
|
|
|
|
MostDerivedClassOffset);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
VisitedVirtualBasesSetTy VBases;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Determine the primary virtual bases.
|
2018-07-31 03:24:48 +08:00
|
|
|
DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
|
2011-09-26 09:57:12 +08:00
|
|
|
VBases);
|
|
|
|
VBases.clear();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
|
|
|
|
|
|
|
|
// -fapple-kext adds an extra entry at end of vtbl.
|
2012-03-11 15:00:24 +08:00
|
|
|
bool IsAppleKext = Context.getLangOpts().AppleKext;
|
2011-09-26 09:57:12 +08:00
|
|
|
if (IsAppleKext)
|
|
|
|
Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero()));
|
|
|
|
}
|
2013-10-09 19:33:51 +08:00
|
|
|
|
|
|
|
void ItaniumVTableBuilder::LayoutPrimaryAndSecondaryVTables(
|
|
|
|
BaseSubobject Base, bool BaseIsMorallyVirtual,
|
|
|
|
bool BaseIsVirtualInLayoutClass, CharUnits OffsetInLayoutClass) {
|
2011-09-26 09:57:12 +08:00
|
|
|
assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
|
|
|
|
|
2016-12-14 04:40:39 +08:00
|
|
|
unsigned VTableIndex = Components.size();
|
|
|
|
VTableIndices.push_back(VTableIndex);
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Add vcall and vbase offsets for this vtable.
|
|
|
|
VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
|
2018-07-31 03:24:48 +08:00
|
|
|
Base, BaseIsVirtualInLayoutClass,
|
2011-09-26 09:57:12 +08:00
|
|
|
OffsetInLayoutClass);
|
|
|
|
Components.append(Builder.components_begin(), Builder.components_end());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if we need to add these vcall offsets.
|
|
|
|
if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
|
|
|
|
VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (VCallOffsets.empty())
|
|
|
|
VCallOffsets = Builder.getVCallOffsets();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're laying out the most derived class we want to keep track of the
|
|
|
|
// virtual base class offset offsets.
|
|
|
|
if (Base.getBase() == MostDerivedClass)
|
|
|
|
VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
|
|
|
|
|
2013-09-27 22:48:01 +08:00
|
|
|
// Add the offset to top.
|
|
|
|
CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
|
|
|
|
Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
|
2013-01-21 21:02:41 +08:00
|
|
|
|
2013-09-27 22:48:01 +08:00
|
|
|
// Next, add the RTTI.
|
|
|
|
Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
|
2013-01-21 21:02:41 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
uint64_t AddressPoint = Components.size();
|
|
|
|
|
|
|
|
// Now go through all virtual member functions and add them.
|
|
|
|
PrimaryBasesSetVectorTy PrimaryBases;
|
|
|
|
AddMethods(Base, OffsetInLayoutClass,
|
2018-07-31 03:24:48 +08:00
|
|
|
Base.getBase(), OffsetInLayoutClass,
|
2011-09-26 09:57:12 +08:00
|
|
|
PrimaryBases);
|
|
|
|
|
2013-06-05 22:05:50 +08:00
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
if (RD == MostDerivedClass) {
|
|
|
|
assert(MethodVTableIndices.empty());
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : MethodInfoMap) {
|
|
|
|
const CXXMethodDecl *MD = I.first;
|
|
|
|
const MethodInfo &MI = I.second;
|
2013-06-05 22:05:50 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
2013-09-27 22:48:01 +08:00
|
|
|
MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)]
|
|
|
|
= MI.VTableIndex - AddressPoint;
|
|
|
|
MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)]
|
|
|
|
= MI.VTableIndex + 1 - AddressPoint;
|
2013-06-05 22:05:50 +08:00
|
|
|
} else {
|
|
|
|
MethodVTableIndices[MD] = MI.VTableIndex - AddressPoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Compute 'this' pointer adjustments.
|
|
|
|
ComputeThisAdjustments();
|
|
|
|
|
|
|
|
// Add all address points.
|
|
|
|
while (true) {
|
2016-12-14 04:40:39 +08:00
|
|
|
AddressPoints.insert(
|
|
|
|
std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
|
|
|
|
VTableLayout::AddressPointLocation{
|
|
|
|
unsigned(VTableIndices.size() - 1),
|
|
|
|
unsigned(AddressPoint - VTableIndex)}));
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!PrimaryBase)
|
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (Layout.isPrimaryBaseVirtual()) {
|
|
|
|
// Check if this virtual primary base is a primary base in the layout
|
|
|
|
// class. If it's not, we don't want to add it.
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
|
|
|
|
OffsetInLayoutClass) {
|
|
|
|
// We don't want to add this class (or any of its primary bases).
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RD = PrimaryBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layout secondary vtables.
|
|
|
|
LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void
|
|
|
|
ItaniumVTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
|
|
|
|
bool BaseIsMorallyVirtual,
|
|
|
|
CharUnits OffsetInLayoutClass) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Itanium C++ ABI 2.5.2:
|
2018-07-31 03:24:48 +08:00
|
|
|
// Following the primary virtual table of a derived class are secondary
|
2011-09-26 09:57:12 +08:00
|
|
|
// virtual tables for each of its proper base classes, except any primary
|
|
|
|
// base(s) with which it shares its primary virtual table.
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Ignore virtual bases, we'll emit them later.
|
2014-03-26 16:22:48 +08:00
|
|
|
if (B.isVirtual())
|
2011-09-26 09:57:12 +08:00
|
|
|
continue;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-03-26 16:22:48 +08:00
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Ignore bases that don't have a vtable.
|
|
|
|
if (!BaseDecl->isDynamicClass())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isBuildingConstructorVTable()) {
|
|
|
|
// Itanium C++ ABI 2.6.4:
|
|
|
|
// Some of the base class subobjects may not need construction virtual
|
|
|
|
// tables, which will therefore not be present in the construction
|
|
|
|
// virtual table group, even though the subobject virtual tables are
|
|
|
|
// present in the main virtual table group for the complete object.
|
|
|
|
if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the base offset of this base.
|
|
|
|
CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
CharUnits BaseOffsetInLayoutClass =
|
2011-09-26 09:57:12 +08:00
|
|
|
OffsetInLayoutClass + RelativeBaseOffset;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
// Don't emit a secondary vtable for a primary base. We might however want
|
2011-09-26 09:57:12 +08:00
|
|
|
// to emit secondary vtables for other bases of this base.
|
|
|
|
if (BaseDecl == PrimaryBase) {
|
|
|
|
LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
|
|
|
|
BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layout the primary vtable (and any secondary vtables) for this base.
|
|
|
|
LayoutPrimaryAndSecondaryVTables(
|
|
|
|
BaseSubobject(BaseDecl, BaseOffset),
|
|
|
|
BaseIsMorallyVirtual,
|
|
|
|
/*BaseIsVirtualInLayoutClass=*/false,
|
|
|
|
BaseOffsetInLayoutClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::DeterminePrimaryVirtualBases(
|
|
|
|
const CXXRecordDecl *RD, CharUnits OffsetInLayoutClass,
|
|
|
|
VisitedVirtualBasesSetTy &VBases) {
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Check if this base has a primary base.
|
|
|
|
if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
|
|
|
|
|
|
|
// Check if it's virtual.
|
|
|
|
if (Layout.isPrimaryBaseVirtual()) {
|
|
|
|
bool IsPrimaryVirtualBase = true;
|
|
|
|
|
|
|
|
if (isBuildingConstructorVTable()) {
|
|
|
|
// Check if the base is actually a primary base in the class we use for
|
|
|
|
// layout.
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
CharUnits PrimaryBaseOffsetInLayoutClass =
|
|
|
|
LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
// We know that the base is not a primary base in the layout class if
|
2011-09-26 09:57:12 +08:00
|
|
|
// the base offsets are different.
|
|
|
|
if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
|
|
|
|
IsPrimaryVirtualBase = false;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (IsPrimaryVirtualBase)
|
|
|
|
PrimaryVirtualBases.insert(PrimaryBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse bases, looking for more primary virtual bases.
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
CharUnits BaseOffsetInLayoutClass;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-03-26 16:22:48 +08:00
|
|
|
if (B.isVirtual()) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!VBases.insert(BaseDecl).second)
|
2011-09-26 09:57:12 +08:00
|
|
|
continue;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
BaseOffsetInLayoutClass =
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
} else {
|
2018-07-31 03:24:48 +08:00
|
|
|
BaseOffsetInLayoutClass =
|
2011-09-26 09:57:12 +08:00
|
|
|
OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::LayoutVTablesForVirtualBases(
|
|
|
|
const CXXRecordDecl *RD, VisitedVirtualBasesSetTy &VBases) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// Then come the virtual base virtual tables, also in inheritance graph
|
|
|
|
// order, and again excluding primary bases (which share virtual tables with
|
|
|
|
// the classes for which they are primary).
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
|
|
|
const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Check if this base needs a vtable. (If it's virtual, not a primary base
|
|
|
|
// of some other class, and we haven't visited it before).
|
2014-11-19 15:49:47 +08:00
|
|
|
if (B.isVirtual() && BaseDecl->isDynamicClass() &&
|
|
|
|
!PrimaryVirtualBases.count(BaseDecl) &&
|
|
|
|
VBases.insert(BaseDecl).second) {
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
CharUnits BaseOffset =
|
2011-09-26 09:57:12 +08:00
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
2018-07-31 03:24:48 +08:00
|
|
|
CharUnits BaseOffsetInLayoutClass =
|
2011-09-26 09:57:12 +08:00
|
|
|
LayoutClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
|
|
|
|
LayoutPrimaryAndSecondaryVTables(
|
|
|
|
BaseSubobject(BaseDecl, BaseOffset),
|
|
|
|
/*BaseIsMorallyVirtual=*/true,
|
|
|
|
/*BaseIsVirtualInLayoutClass=*/true,
|
|
|
|
BaseOffsetInLayoutClass);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// We only need to check the base for virtual base vtables if it actually
|
|
|
|
// has virtual bases.
|
|
|
|
if (BaseDecl->getNumVBases())
|
|
|
|
LayoutVTablesForVirtualBases(BaseDecl, VBases);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dumpLayout - Dump the vtable layout.
|
2013-10-09 19:33:51 +08:00
|
|
|
void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
|
2013-06-28 23:42:28 +08:00
|
|
|
// FIXME: write more tests that actually use the dumpLayout output to prevent
|
2013-10-09 19:33:51 +08:00
|
|
|
// ItaniumVTableBuilder regressions.
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
if (isBuildingConstructorVTable()) {
|
|
|
|
Out << "Construction vtable for ('";
|
2014-01-04 02:42:48 +08:00
|
|
|
MostDerivedClass->printQualifiedName(Out);
|
|
|
|
Out << "', ";
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << MostDerivedClassOffset.getQuantity() << ") in '";
|
2014-01-04 02:42:48 +08:00
|
|
|
LayoutClass->printQualifiedName(Out);
|
2011-09-26 09:57:12 +08:00
|
|
|
} else {
|
|
|
|
Out << "Vtable for '";
|
2014-01-04 02:42:48 +08:00
|
|
|
MostDerivedClass->printQualifiedName(Out);
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
Out << "' (" << Components.size() << " entries).\n";
|
|
|
|
|
|
|
|
// Iterate through the address points and insert them into a new map where
|
|
|
|
// they are keyed by the index and not the base object.
|
|
|
|
// Since an address point can be shared by multiple subobjects, we use an
|
|
|
|
// STL multimap.
|
|
|
|
std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &AP : AddressPoints) {
|
|
|
|
const BaseSubobject &Base = AP.first;
|
2016-12-14 04:40:39 +08:00
|
|
|
uint64_t Index =
|
|
|
|
VTableIndices[AP.second.VTableIndex] + AP.second.AddressPointIndex;
|
2015-07-26 01:10:49 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
AddressPointsByIndex.insert(std::make_pair(Index, Base));
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
for (unsigned I = 0, E = Components.size(); I != E; ++I) {
|
|
|
|
uint64_t Index = I;
|
|
|
|
|
|
|
|
Out << llvm::format("%4d | ", I);
|
|
|
|
|
|
|
|
const VTableComponent &Component = Components[I];
|
|
|
|
|
|
|
|
// Dump the component.
|
|
|
|
switch (Component.getKind()) {
|
|
|
|
|
|
|
|
case VTableComponent::CK_VCallOffset:
|
|
|
|
Out << "vcall_offset ("
|
2018-07-31 03:24:48 +08:00
|
|
|
<< Component.getVCallOffset().getQuantity()
|
2011-09-26 09:57:12 +08:00
|
|
|
<< ")";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VTableComponent::CK_VBaseOffset:
|
|
|
|
Out << "vbase_offset ("
|
|
|
|
<< Component.getVBaseOffset().getQuantity()
|
|
|
|
<< ")";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VTableComponent::CK_OffsetToTop:
|
|
|
|
Out << "offset_to_top ("
|
|
|
|
<< Component.getOffsetToTop().getQuantity()
|
|
|
|
<< ")";
|
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
case VTableComponent::CK_RTTI:
|
2014-01-04 02:42:48 +08:00
|
|
|
Component.getRTTIDecl()->printQualifiedName(Out);
|
|
|
|
Out << " RTTI";
|
2011-09-26 09:57:12 +08:00
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
case VTableComponent::CK_FunctionPointer: {
|
|
|
|
const CXXMethodDecl *MD = Component.getFunctionDecl();
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
std::string Str =
|
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
2011-09-26 09:57:12 +08:00
|
|
|
MD);
|
|
|
|
Out << Str;
|
|
|
|
if (MD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
|
2012-10-17 04:25:33 +08:00
|
|
|
if (MD->isDeleted())
|
|
|
|
Out << " [deleted]";
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
ThunkInfo Thunk = VTableThunks.lookup(I);
|
|
|
|
if (!Thunk.isEmpty()) {
|
|
|
|
// If this function pointer has a return adjustment, dump it.
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
|
|
|
Out << "\n [return adjustment: ";
|
|
|
|
Out << Thunk.Return.NonVirtual << " non-virtual";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-10-30 19:55:43 +08:00
|
|
|
if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << " vbase offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this function pointer has a 'this' pointer adjustment, dump it.
|
|
|
|
if (!Thunk.This.isEmpty()) {
|
|
|
|
Out << "\n [this adjustment: ";
|
|
|
|
Out << Thunk.This.NonVirtual << " non-virtual";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << " vcall offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << ']';
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
2011-09-26 09:57:12 +08:00
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
2018-07-31 03:24:48 +08:00
|
|
|
bool IsComplete =
|
2011-09-26 09:57:12 +08:00
|
|
|
Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXDestructorDecl *DD = Component.getDestructorDecl();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-01-04 02:42:48 +08:00
|
|
|
DD->printQualifiedName(Out);
|
2011-09-26 09:57:12 +08:00
|
|
|
if (IsComplete)
|
|
|
|
Out << "() [complete]";
|
|
|
|
else
|
|
|
|
Out << "() [deleting]";
|
|
|
|
|
|
|
|
if (DD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
|
|
|
|
ThunkInfo Thunk = VTableThunks.lookup(I);
|
|
|
|
if (!Thunk.isEmpty()) {
|
|
|
|
// If this destructor has a 'this' pointer adjustment, dump it.
|
|
|
|
if (!Thunk.This.isEmpty()) {
|
|
|
|
Out << "\n [this adjustment: ";
|
|
|
|
Out << Thunk.This.NonVirtual << " non-virtual";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << " vcall offset offset";
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << ']';
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
|
|
|
}
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VTableComponent::CK_UnusedFunctionPointer: {
|
|
|
|
const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
std::string Str =
|
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
2011-09-26 09:57:12 +08:00
|
|
|
MD);
|
|
|
|
Out << "[unused] " << Str;
|
|
|
|
if (MD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Dump the next address point.
|
|
|
|
uint64_t NextIndex = Index + 1;
|
|
|
|
if (AddressPointsByIndex.count(NextIndex)) {
|
|
|
|
if (AddressPointsByIndex.count(NextIndex) == 1) {
|
2018-07-31 03:24:48 +08:00
|
|
|
const BaseSubobject &Base =
|
2011-09-26 09:57:12 +08:00
|
|
|
AddressPointsByIndex.find(NextIndex)->second;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << " -- (";
|
|
|
|
Base.getBase()->printQualifiedName(Out);
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << ", " << Base.getBaseOffset().getQuantity();
|
|
|
|
Out << ") vtable address --\n";
|
|
|
|
} else {
|
|
|
|
CharUnits BaseOffset =
|
|
|
|
AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// We store the class names in a set to get a stable order.
|
|
|
|
std::set<std::string> ClassNames;
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I :
|
|
|
|
llvm::make_range(AddressPointsByIndex.equal_range(NextIndex))) {
|
|
|
|
assert(I.second.getBaseOffset() == BaseOffset &&
|
2011-09-26 09:57:12 +08:00
|
|
|
"Invalid base offset!");
|
2015-07-26 01:10:49 +08:00
|
|
|
const CXXRecordDecl *RD = I.second.getBase();
|
2011-09-26 09:57:12 +08:00
|
|
|
ClassNames.insert(RD->getQualifiedNameAsString());
|
|
|
|
}
|
2015-07-26 01:10:49 +08:00
|
|
|
|
|
|
|
for (const std::string &Name : ClassNames) {
|
|
|
|
Out << " -- (" << Name;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (isBuildingConstructorVTable())
|
|
|
|
return;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (MostDerivedClass->getNumVBases()) {
|
|
|
|
// We store the virtual base class names and their offsets in a map to get
|
|
|
|
// a stable order.
|
|
|
|
|
|
|
|
std::map<std::string, CharUnits> ClassNamesAndOffsets;
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : VBaseOffsetOffsets) {
|
|
|
|
std::string ClassName = I.first->getQualifiedNameAsString();
|
|
|
|
CharUnits OffsetOffset = I.second;
|
|
|
|
ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << "Virtual base offset offsets for '";
|
2014-01-04 02:42:48 +08:00
|
|
|
MostDerivedClass->printQualifiedName(Out);
|
|
|
|
Out << "' (";
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << ClassNamesAndOffsets.size();
|
|
|
|
Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : ClassNamesAndOffsets)
|
|
|
|
Out << " " << I.first << " | " << I.second.getQuantity() << '\n';
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
Out << "\n";
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (!Thunks.empty()) {
|
|
|
|
// We store the method names in a map to get a stable order.
|
|
|
|
std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
|
2015-07-26 01:10:49 +08:00
|
|
|
|
|
|
|
for (const auto &I : Thunks) {
|
|
|
|
const CXXMethodDecl *MD = I.first;
|
2018-07-31 03:24:48 +08:00
|
|
|
std::string MethodName =
|
2011-09-26 09:57:12 +08:00
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
|
|
|
MD);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
|
|
|
|
}
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : MethodNamesAndDecls) {
|
|
|
|
const std::string &MethodName = I.first;
|
|
|
|
const CXXMethodDecl *MD = I.second;
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
ThunkInfoVectorTy ThunksVector = Thunks[MD];
|
2018-09-27 06:16:28 +08:00
|
|
|
llvm::sort(ThunksVector, [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
|
2014-05-12 13:36:57 +08:00
|
|
|
assert(LHS.Method == nullptr && RHS.Method == nullptr);
|
2014-03-04 04:26:46 +08:00
|
|
|
return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
|
2014-03-01 22:48:57 +08:00
|
|
|
});
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
|
|
|
|
Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
|
|
|
|
const ThunkInfo &Thunk = ThunksVector[I];
|
|
|
|
|
|
|
|
Out << llvm::format("%4d | ", I);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// If this function pointer has a return pointer adjustment, dump it.
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
2013-06-28 23:42:28 +08:00
|
|
|
Out << "return adjustment: " << Thunk.Return.NonVirtual;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << " non-virtual";
|
2013-10-30 19:55:43 +08:00
|
|
|
if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << " vbase offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Thunk.This.isEmpty())
|
|
|
|
Out << "\n ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this function pointer has a 'this' pointer adjustment, dump it.
|
|
|
|
if (!Thunk.This.isEmpty()) {
|
|
|
|
Out << "this adjustment: ";
|
|
|
|
Out << Thunk.This.NonVirtual << " non-virtual";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << " vcall offset offset";
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << '\n';
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the vtable indices for all the member functions.
|
|
|
|
// Store them in a map keyed by the index so we'll get a sorted table.
|
|
|
|
std::map<uint64_t, std::string> IndicesMap;
|
|
|
|
|
2014-03-14 00:36:16 +08:00
|
|
|
for (const auto *MD : MostDerivedClass->methods()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// We only want virtual member functions.
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
2015-02-26 03:17:48 +08:00
|
|
|
MD = MD->getCanonicalDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
std::string MethodName =
|
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
|
|
|
MD);
|
|
|
|
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
2013-09-27 22:48:01 +08:00
|
|
|
GlobalDecl GD(DD, Dtor_Complete);
|
|
|
|
assert(MethodVTableIndices.count(GD));
|
|
|
|
uint64_t VTableIndex = MethodVTableIndices[GD];
|
|
|
|
IndicesMap[VTableIndex] = MethodName + " [complete]";
|
|
|
|
IndicesMap[VTableIndex + 1] = MethodName + " [deleting]";
|
2011-09-26 09:57:12 +08:00
|
|
|
} else {
|
2013-06-05 22:05:50 +08:00
|
|
|
assert(MethodVTableIndices.count(MD));
|
|
|
|
IndicesMap[MethodVTableIndices[MD]] = MethodName;
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the vtable indices for all the member functions.
|
|
|
|
if (!IndicesMap.empty()) {
|
|
|
|
Out << "VTable indices for '";
|
2014-01-04 02:42:48 +08:00
|
|
|
MostDerivedClass->printQualifiedName(Out);
|
2011-09-26 09:57:12 +08:00
|
|
|
Out << "' (" << IndicesMap.size() << " entries).\n";
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : IndicesMap) {
|
|
|
|
uint64_t VTableIndex = I.first;
|
|
|
|
const std::string &MethodName = I.second;
|
2011-09-26 09:57:12 +08:00
|
|
|
|
2013-06-05 22:05:50 +08:00
|
|
|
Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName
|
2012-03-10 10:06:27 +08:00
|
|
|
<< '\n';
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
|
|
|
}
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-09-26 09:57:12 +08:00
|
|
|
|
2016-12-14 04:40:39 +08:00
|
|
|
VTableLayout::VTableLayout(ArrayRef<size_t> VTableIndices,
|
|
|
|
ArrayRef<VTableComponent> VTableComponents,
|
|
|
|
ArrayRef<VTableThunkTy> VTableThunks,
|
|
|
|
const AddressPointsMapTy &AddressPoints)
|
|
|
|
: VTableComponents(VTableComponents), VTableThunks(VTableThunks),
|
|
|
|
AddressPoints(AddressPoints) {
|
|
|
|
if (VTableIndices.size() <= 1)
|
|
|
|
assert(VTableIndices.size() == 1 && VTableIndices[0] == 0);
|
|
|
|
else
|
|
|
|
this->VTableIndices = OwningArrayRef<size_t>(VTableIndices);
|
|
|
|
|
2018-10-01 05:41:11 +08:00
|
|
|
llvm::sort(this->VTableThunks, [](const VTableLayout::VTableThunkTy &LHS,
|
|
|
|
const VTableLayout::VTableThunkTy &RHS) {
|
|
|
|
assert((LHS.first != RHS.first || LHS.second == RHS.second) &&
|
|
|
|
"Different thunks should have unique indices!");
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
});
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
VTableLayout::~VTableLayout() { }
|
2011-09-26 09:57:12 +08:00
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
ItaniumVTableContext::ItaniumVTableContext(ASTContext &Context)
|
2013-12-21 07:58:52 +08:00
|
|
|
: VTableContextBase(/*MS=*/false) {}
|
2013-01-21 21:02:41 +08:00
|
|
|
|
2016-10-11 00:26:24 +08:00
|
|
|
ItaniumVTableContext::~ItaniumVTableContext() {}
|
2011-09-26 09:57:12 +08:00
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
uint64_t ItaniumVTableContext::getMethodVTableIndex(GlobalDecl GD) {
|
2018-05-18 02:12:18 +08:00
|
|
|
GD = GD.getCanonicalDecl();
|
2011-09-26 09:57:12 +08:00
|
|
|
MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
|
|
|
|
if (I != MethodVTableIndices.end())
|
|
|
|
return I->second;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
computeVTableRelatedInformation(RD);
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
I = MethodVTableIndices.find(GD);
|
|
|
|
assert(I != MethodVTableIndices.end() && "Did not find index!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
CharUnits
|
|
|
|
ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *VBase) {
|
2011-09-26 09:57:12 +08:00
|
|
|
ClassPairTy ClassPair(RD, VBase);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
VirtualBaseClassOffsetOffsetsMapTy::iterator I =
|
2011-09-26 09:57:12 +08:00
|
|
|
VirtualBaseClassOffsetOffsets.find(ClassPair);
|
|
|
|
if (I != VirtualBaseClassOffsetOffsets.end())
|
|
|
|
return I->second;
|
2014-05-12 13:36:57 +08:00
|
|
|
|
2019-07-16 12:46:31 +08:00
|
|
|
VCallAndVBaseOffsetBuilder Builder(RD, RD, /*Overriders=*/nullptr,
|
2011-09-26 09:57:12 +08:00
|
|
|
BaseSubobject(RD, CharUnits::Zero()),
|
|
|
|
/*BaseIsVirtual=*/false,
|
|
|
|
/*OffsetInLayoutClass=*/CharUnits::Zero());
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : Builder.getVBaseOffsetOffsets()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Insert all types.
|
2015-07-26 01:10:49 +08:00
|
|
|
ClassPairTy ClassPair(RD, I.first);
|
|
|
|
|
|
|
|
VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second));
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
I = VirtualBaseClassOffsetOffsets.find(ClassPair);
|
|
|
|
assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:24 +08:00
|
|
|
static std::unique_ptr<VTableLayout>
|
|
|
|
CreateVTableLayout(const ItaniumVTableBuilder &Builder) {
|
2011-09-26 09:57:12 +08:00
|
|
|
SmallVector<VTableLayout::VTableThunkTy, 1>
|
|
|
|
VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
|
|
|
|
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<VTableLayout>(
|
2016-12-14 04:40:39 +08:00
|
|
|
Builder.VTableIndices, Builder.vtable_components(), VTableThunks,
|
|
|
|
Builder.getAddressPoints());
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
void
|
|
|
|
ItaniumVTableContext::computeVTableRelatedInformation(const CXXRecordDecl *RD) {
|
2016-10-11 00:26:24 +08:00
|
|
|
std::unique_ptr<const VTableLayout> &Entry = VTableLayouts[RD];
|
2011-09-26 09:57:12 +08:00
|
|
|
|
|
|
|
// Check if we've computed this information before.
|
|
|
|
if (Entry)
|
|
|
|
return;
|
|
|
|
|
2013-10-09 19:33:51 +08:00
|
|
|
ItaniumVTableBuilder Builder(*this, RD, CharUnits::Zero(),
|
|
|
|
/*MostDerivedClassIsVirtual=*/0, RD);
|
2011-09-26 09:57:12 +08:00
|
|
|
Entry = CreateVTableLayout(Builder);
|
|
|
|
|
2013-06-05 22:05:50 +08:00
|
|
|
MethodVTableIndices.insert(Builder.vtable_indices_begin(),
|
|
|
|
Builder.vtable_indices_end());
|
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
// Add the known thunks.
|
|
|
|
Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
|
|
|
|
|
|
|
|
// If we don't have the vbase information for this class, insert it.
|
|
|
|
// getVirtualBaseOffsetOffset will compute it separately without computing
|
|
|
|
// the rest of the vtable related information.
|
|
|
|
if (!RD->getNumVBases())
|
|
|
|
return;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-07-03 00:00:40 +08:00
|
|
|
const CXXRecordDecl *VBase =
|
|
|
|
RD->vbases_begin()->getType()->getAsCXXRecordDecl();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-09-26 09:57:12 +08:00
|
|
|
if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
|
|
|
|
return;
|
2013-10-09 19:33:51 +08:00
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : Builder.getVBaseOffsetOffsets()) {
|
2011-09-26 09:57:12 +08:00
|
|
|
// Insert all types.
|
2015-07-26 01:10:49 +08:00
|
|
|
ClassPairTy ClassPair(RD, I.first);
|
|
|
|
|
|
|
|
VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second));
|
2011-09-26 09:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:24 +08:00
|
|
|
std::unique_ptr<VTableLayout>
|
|
|
|
ItaniumVTableContext::createConstructionVTableLayout(
|
2013-10-09 19:33:51 +08:00
|
|
|
const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset,
|
|
|
|
bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass) {
|
|
|
|
ItaniumVTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset,
|
|
|
|
MostDerivedClassIsVirtual, LayoutClass);
|
2011-09-26 09:57:12 +08:00
|
|
|
return CreateVTableLayout(Builder);
|
|
|
|
}
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Vtables in the Microsoft ABI are different from the Itanium ABI.
|
|
|
|
//
|
|
|
|
// The main differences are:
|
|
|
|
// 1. Separate vftable and vbtable.
|
|
|
|
//
|
|
|
|
// 2. Each subobject with a vfptr gets its own vftable rather than an address
|
|
|
|
// point in a single vtable shared between all the subobjects.
|
|
|
|
// Each vftable is represented by a separate section and virtual calls
|
|
|
|
// must be done using the vftable which has a slot for the function to be
|
|
|
|
// called.
|
|
|
|
//
|
|
|
|
// 3. Virtual method definitions expect their 'this' parameter to point to the
|
|
|
|
// first vfptr whose table provides a compatible overridden method. In many
|
|
|
|
// cases, this permits the original vf-table entry to directly call
|
|
|
|
// the method instead of passing through a thunk.
|
2014-11-14 22:10:15 +08:00
|
|
|
// See example before VFTableBuilder::ComputeThisOffset below.
|
2013-07-30 17:46:19 +08:00
|
|
|
//
|
|
|
|
// A compatible overridden method is one which does not have a non-trivial
|
|
|
|
// covariant-return adjustment.
|
|
|
|
//
|
|
|
|
// The first vfptr is the one with the lowest offset in the complete-object
|
|
|
|
// layout of the defining class, and the method definition will subtract
|
|
|
|
// that constant offset from the parameter value to get the real 'this'
|
|
|
|
// value. Therefore, if the offset isn't really constant (e.g. if a virtual
|
|
|
|
// function defined in a virtual base is overridden in a more derived
|
|
|
|
// virtual base and these bases have a reverse order in the complete
|
|
|
|
// object), the vf-table may require a this-adjustment thunk.
|
|
|
|
//
|
|
|
|
// 4. vftables do not contain new entries for overrides that merely require
|
|
|
|
// this-adjustment. Together with #3, this keeps vf-tables smaller and
|
|
|
|
// eliminates the need for this-adjustment thunks in many cases, at the cost
|
|
|
|
// of often requiring redundant work to adjust the "this" pointer.
|
|
|
|
//
|
|
|
|
// 5. Instead of VTT and constructor vtables, vbtables and vtordisps are used.
|
|
|
|
// Vtordisps are emitted into the class layout if a class has
|
|
|
|
// a) a user-defined ctor/dtor
|
|
|
|
// and
|
|
|
|
// b) a method overriding a method in a virtual base.
|
2014-11-14 22:10:15 +08:00
|
|
|
//
|
|
|
|
// To get a better understanding of this code,
|
|
|
|
// you might want to see examples in test/CodeGenCXX/microsoft-abi-vtables-*.cpp
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
class VFTableBuilder {
|
|
|
|
public:
|
|
|
|
typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation>
|
|
|
|
MethodVFTableLocationsTy;
|
|
|
|
|
2014-03-21 04:38:34 +08:00
|
|
|
typedef llvm::iterator_range<MethodVFTableLocationsTy::const_iterator>
|
|
|
|
method_locations_range;
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
private:
|
2013-11-05 23:54:58 +08:00
|
|
|
/// VTables - Global vtable information.
|
|
|
|
MicrosoftVTableContext &VTables;
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
/// Context - The ASTContext which we will use for layout information.
|
|
|
|
ASTContext &Context;
|
|
|
|
|
|
|
|
/// MostDerivedClass - The most derived class for which we're building this
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
|
|
|
|
|
|
|
const ASTRecordLayout &MostDerivedClassLayout;
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfo &WhichVFPtr;
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
/// FinalOverriders - The final overriders of the most derived class.
|
|
|
|
const FinalOverriders Overriders;
|
|
|
|
|
|
|
|
/// Components - The components of the vftable being built.
|
|
|
|
SmallVector<VTableComponent, 64> Components;
|
|
|
|
|
|
|
|
MethodVFTableLocationsTy MethodVFTableLocations;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Does this class have an RTTI component?
|
2016-02-12 01:49:28 +08:00
|
|
|
bool HasRTTIComponent = false;
|
2014-07-02 05:10:07 +08:00
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
/// MethodInfo - Contains information about a method in a vtable.
|
|
|
|
/// (Used for computing 'this' pointer adjustment thunks.
|
|
|
|
struct MethodInfo {
|
|
|
|
/// VBTableIndex - The nonzero index in the vbtable that
|
|
|
|
/// this method's base has, or zero.
|
|
|
|
const uint64_t VBTableIndex;
|
|
|
|
|
|
|
|
/// VFTableIndex - The index in the vftable that this method has.
|
|
|
|
const uint64_t VFTableIndex;
|
|
|
|
|
|
|
|
/// Shadowed - Indicates if this vftable slot is shadowed by
|
|
|
|
/// a slot for a covariant-return override. If so, it shouldn't be printed
|
|
|
|
/// or used for vcalls in the most derived class.
|
|
|
|
bool Shadowed;
|
|
|
|
|
2014-08-10 19:40:51 +08:00
|
|
|
/// UsesExtraSlot - Indicates if this vftable slot was created because
|
|
|
|
/// any of the overridden slots required a return adjusting thunk.
|
|
|
|
bool UsesExtraSlot;
|
|
|
|
|
|
|
|
MethodInfo(uint64_t VBTableIndex, uint64_t VFTableIndex,
|
|
|
|
bool UsesExtraSlot = false)
|
2013-10-22 22:50:20 +08:00
|
|
|
: VBTableIndex(VBTableIndex), VFTableIndex(VFTableIndex),
|
2014-08-10 19:40:51 +08:00
|
|
|
Shadowed(false), UsesExtraSlot(UsesExtraSlot) {}
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2014-08-10 19:40:51 +08:00
|
|
|
MethodInfo()
|
|
|
|
: VBTableIndex(0), VFTableIndex(0), Shadowed(false),
|
|
|
|
UsesExtraSlot(false) {}
|
2013-07-30 17:46:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
|
|
|
|
|
|
|
|
/// MethodInfoMap - The information for all methods in the vftable we're
|
|
|
|
/// currently building.
|
|
|
|
MethodInfoMapTy MethodInfoMap;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
|
|
|
|
|
|
|
|
/// VTableThunks - The thunks by vftable index in the vftable currently being
|
|
|
|
/// built.
|
|
|
|
VTableThunksMapTy VTableThunks;
|
|
|
|
|
|
|
|
typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
|
|
|
|
typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
|
|
|
|
|
|
|
|
/// Thunks - A map that contains all the thunks needed for all methods in the
|
|
|
|
/// most derived class for which the vftable is currently being built.
|
|
|
|
ThunksMapTy Thunks;
|
|
|
|
|
|
|
|
/// AddThunk - Add a thunk for the given method.
|
|
|
|
void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
|
|
|
|
SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
|
|
|
|
|
|
|
|
// Check if we have this thunk already.
|
2019-03-31 16:48:19 +08:00
|
|
|
if (llvm::find(ThunksVector, Thunk) != ThunksVector.end())
|
2013-07-30 17:46:19 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ThunksVector.push_back(Thunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ComputeThisOffset - Returns the 'this' argument offset for the given
|
2014-03-05 02:17:38 +08:00
|
|
|
/// method, relative to the beginning of the MostDerivedClass.
|
|
|
|
CharUnits ComputeThisOffset(FinalOverriders::OverriderInfo Overrider);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
void CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider,
|
|
|
|
CharUnits ThisOffset, ThisAdjustment &TA);
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
/// AddMethod - Add a single virtual member function to the vftable
|
|
|
|
/// components vector.
|
2013-10-09 17:23:58 +08:00
|
|
|
void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) {
|
2014-03-07 17:34:59 +08:00
|
|
|
if (!TI.isEmpty()) {
|
|
|
|
VTableThunks[Components.size()] = TI;
|
|
|
|
AddThunk(MD, TI);
|
|
|
|
}
|
2013-07-30 17:46:19 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
2013-10-09 17:23:58 +08:00
|
|
|
assert(TI.Return.isEmpty() &&
|
2013-07-30 17:46:19 +08:00
|
|
|
"Destructor can't have return adjustment!");
|
|
|
|
Components.push_back(VTableComponent::MakeDeletingDtor(DD));
|
|
|
|
} else {
|
|
|
|
Components.push_back(VTableComponent::MakeFunction(MD));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddMethods - Add the methods of this base subobject and the relevant
|
|
|
|
/// subbases to the vftable we're currently laying out.
|
|
|
|
void AddMethods(BaseSubobject Base, unsigned BaseDepth,
|
|
|
|
const CXXRecordDecl *LastVBase,
|
|
|
|
BasesSetVectorTy &VisitedBases);
|
|
|
|
|
|
|
|
void LayoutVFTable() {
|
2014-09-23 04:39:37 +08:00
|
|
|
// RTTI data goes before all other entries.
|
|
|
|
if (HasRTTIComponent)
|
|
|
|
Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
BasesSetVectorTy VisitedBases;
|
2014-05-12 13:36:57 +08:00
|
|
|
AddMethods(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 0, nullptr,
|
2013-07-30 17:46:19 +08:00
|
|
|
VisitedBases);
|
2014-09-26 16:07:55 +08:00
|
|
|
assert((HasRTTIComponent ? Components.size() - 1 : Components.size()) &&
|
|
|
|
"vftable can't be empty");
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
assert(MethodVFTableLocations.empty());
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : MethodInfoMap) {
|
|
|
|
const CXXMethodDecl *MD = I.first;
|
|
|
|
const MethodInfo &MI = I.second;
|
2018-05-18 02:12:18 +08:00
|
|
|
assert(MD == MD->getCanonicalDecl());
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
// Skip the methods that the MostDerivedClass didn't override
|
|
|
|
// and the entries shadowed by return adjusting thunks.
|
|
|
|
if (MD->getParent() != MostDerivedClass || MI.Shadowed)
|
|
|
|
continue;
|
2014-02-28 03:40:09 +08:00
|
|
|
MethodVFTableLocation Loc(MI.VBTableIndex, WhichVFPtr.getVBaseWithVPtr(),
|
|
|
|
WhichVFPtr.NonVirtualOffset, MI.VFTableIndex);
|
2013-07-30 17:46:19 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
MethodVFTableLocations[GlobalDecl(DD, Dtor_Deleting)] = Loc;
|
|
|
|
} else {
|
|
|
|
MethodVFTableLocations[MD] = Loc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-11-05 23:54:58 +08:00
|
|
|
VFTableBuilder(MicrosoftVTableContext &VTables,
|
2016-10-11 00:26:29 +08:00
|
|
|
const CXXRecordDecl *MostDerivedClass, const VPtrInfo &Which)
|
2013-11-05 23:54:58 +08:00
|
|
|
: VTables(VTables),
|
|
|
|
Context(MostDerivedClass->getASTContext()),
|
2013-07-30 17:46:19 +08:00
|
|
|
MostDerivedClass(MostDerivedClass),
|
|
|
|
MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)),
|
2016-10-11 00:26:29 +08:00
|
|
|
WhichVFPtr(Which),
|
2013-07-30 17:46:19 +08:00
|
|
|
Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) {
|
2016-06-30 02:29:21 +08:00
|
|
|
// Provide the RTTI component if RTTIData is enabled. If the vftable would
|
|
|
|
// be available externally, we should not provide the RTTI componenent. It
|
|
|
|
// is currently impossible to get available externally vftables with either
|
|
|
|
// dllimport or extern template instantiations, but eventually we may add a
|
|
|
|
// flag to support additional devirtualization that needs this.
|
2016-02-12 01:49:28 +08:00
|
|
|
if (Context.getLangOpts().RTTIData)
|
2016-06-30 02:29:21 +08:00
|
|
|
HasRTTIComponent = true;
|
2014-07-02 04:30:31 +08:00
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
LayoutVFTable();
|
|
|
|
|
|
|
|
if (Context.getLangOpts().DumpVTableLayouts)
|
2013-11-09 05:28:00 +08:00
|
|
|
dumpLayout(llvm::outs());
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t getNumThunks() const { return Thunks.size(); }
|
|
|
|
|
|
|
|
ThunksMapTy::const_iterator thunks_begin() const { return Thunks.begin(); }
|
|
|
|
|
|
|
|
ThunksMapTy::const_iterator thunks_end() const { return Thunks.end(); }
|
|
|
|
|
2014-03-21 04:38:34 +08:00
|
|
|
method_locations_range vtable_locations() const {
|
|
|
|
return method_locations_range(MethodVFTableLocations.begin(),
|
|
|
|
MethodVFTableLocations.end());
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 04:40:39 +08:00
|
|
|
ArrayRef<VTableComponent> vtable_components() const { return Components; }
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
|
|
|
|
return VTableThunks.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
VTableThunksMapTy::const_iterator vtable_thunks_end() const {
|
|
|
|
return VTableThunks.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpLayout(raw_ostream &);
|
|
|
|
};
|
|
|
|
|
2015-03-23 20:31:05 +08:00
|
|
|
} // end namespace
|
|
|
|
|
2014-11-14 22:10:15 +08:00
|
|
|
// Let's study one class hierarchy as an example:
|
|
|
|
// struct A {
|
|
|
|
// virtual void f();
|
|
|
|
// int x;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct B : virtual A {
|
|
|
|
// virtual void f();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// Record layouts:
|
|
|
|
// struct A:
|
2014-11-14 22:16:34 +08:00
|
|
|
// 0 | (A vftable pointer)
|
2014-11-14 22:10:15 +08:00
|
|
|
// 4 | int x
|
|
|
|
//
|
|
|
|
// struct B:
|
2014-11-14 22:16:34 +08:00
|
|
|
// 0 | (B vbtable pointer)
|
|
|
|
// 4 | struct A (virtual base)
|
|
|
|
// 4 | (A vftable pointer)
|
2014-11-14 22:10:15 +08:00
|
|
|
// 8 | int x
|
|
|
|
//
|
|
|
|
// Let's assume we have a pointer to the A part of an object of dynamic type B:
|
|
|
|
// B b;
|
|
|
|
// A *a = (A*)&b;
|
|
|
|
// a->f();
|
|
|
|
//
|
|
|
|
// In this hierarchy, f() belongs to the vftable of A, so B::f() expects
|
|
|
|
// "this" parameter to point at the A subobject, which is B+4.
|
|
|
|
// In the B::f() prologue, it adjusts "this" back to B by subtracting 4,
|
2014-11-30 07:57:35 +08:00
|
|
|
// performed as a *static* adjustment.
|
2014-11-14 22:10:15 +08:00
|
|
|
//
|
|
|
|
// Interesting thing happens when we alter the relative placement of A and B
|
|
|
|
// subobjects in a class:
|
|
|
|
// struct C : virtual B { };
|
|
|
|
//
|
|
|
|
// C c;
|
|
|
|
// A *a = (A*)&c;
|
|
|
|
// a->f();
|
|
|
|
//
|
|
|
|
// Respective record layout is:
|
|
|
|
// 0 | (C vbtable pointer)
|
|
|
|
// 4 | struct A (virtual base)
|
|
|
|
// 4 | (A vftable pointer)
|
|
|
|
// 8 | int x
|
|
|
|
// 12 | struct B (virtual base)
|
|
|
|
// 12 | (B vbtable pointer)
|
|
|
|
//
|
|
|
|
// The final overrider of f() in class C is still B::f(), so B+4 should be
|
|
|
|
// passed as "this" to that code. However, "a" points at B-8, so the respective
|
|
|
|
// vftable entry should hold a thunk that adds 12 to the "this" argument before
|
|
|
|
// performing a tail call to B::f().
|
|
|
|
//
|
|
|
|
// With this example in mind, we can now calculate the 'this' argument offset
|
|
|
|
// for the given method, relative to the beginning of the MostDerivedClass.
|
2013-07-30 17:46:19 +08:00
|
|
|
CharUnits
|
2014-03-05 02:17:38 +08:00
|
|
|
VFTableBuilder::ComputeThisOffset(FinalOverriders::OverriderInfo Overrider) {
|
2015-07-26 00:31:30 +08:00
|
|
|
BasesSetVectorTy Bases;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Find the set of least derived bases that define the given method.
|
|
|
|
OverriddenMethodsSetTy VisitedOverriddenMethods;
|
|
|
|
auto InitialOverriddenDefinitionCollector = [&](
|
|
|
|
const CXXMethodDecl *OverriddenMD) {
|
|
|
|
if (OverriddenMD->size_overridden_methods() == 0)
|
|
|
|
Bases.insert(OverriddenMD->getParent());
|
|
|
|
// Don't recurse on this method if we've already collected it.
|
|
|
|
return VisitedOverriddenMethods.insert(OverriddenMD).second;
|
|
|
|
};
|
|
|
|
visitAllOverriddenMethods(Overrider.Method,
|
|
|
|
InitialOverriddenDefinitionCollector);
|
|
|
|
}
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2014-03-07 17:34:59 +08:00
|
|
|
// If there are no overrides then 'this' is located
|
|
|
|
// in the base that defines the method.
|
2015-07-26 00:31:30 +08:00
|
|
|
if (Bases.size() == 0)
|
2014-03-07 17:34:59 +08:00
|
|
|
return Overrider.Offset;
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
CXXBasePaths Paths;
|
2015-07-25 23:07:25 +08:00
|
|
|
Overrider.Method->getParent()->lookupInBases(
|
2015-07-26 00:31:30 +08:00
|
|
|
[&Bases](const CXXBaseSpecifier *Specifier, CXXBasePath &) {
|
|
|
|
return Bases.count(Specifier->getType()->getAsCXXRecordDecl());
|
2015-07-25 23:07:25 +08:00
|
|
|
},
|
|
|
|
Paths);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
// This will hold the smallest this offset among overridees of MD.
|
|
|
|
// This implies that an offset of a non-virtual base will dominate an offset
|
|
|
|
// of a virtual base to potentially reduce the number of thunks required
|
|
|
|
// in the derived classes that inherit this method.
|
|
|
|
CharUnits Ret;
|
|
|
|
bool First = true;
|
|
|
|
|
2014-04-18 06:01:48 +08:00
|
|
|
const ASTRecordLayout &OverriderRDLayout =
|
|
|
|
Context.getASTRecordLayout(Overrider.Method->getParent());
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXBasePath &Path : Paths) {
|
2014-03-05 02:17:38 +08:00
|
|
|
CharUnits ThisOffset = Overrider.Offset;
|
2013-11-06 14:24:31 +08:00
|
|
|
CharUnits LastVBaseOffset;
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2015-05-17 07:49:53 +08:00
|
|
|
// For each path from the overrider to the parents of the overridden
|
|
|
|
// methods, traverse the path, calculating the this offset in the most
|
|
|
|
// derived class.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXBasePathElement &Element : Path) {
|
2013-07-30 17:46:19 +08:00
|
|
|
QualType CurTy = Element.Base->getType();
|
|
|
|
const CXXRecordDecl *PrevRD = Element.Class,
|
|
|
|
*CurRD = CurTy->getAsCXXRecordDecl();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(PrevRD);
|
|
|
|
|
|
|
|
if (Element.Base->isVirtual()) {
|
2014-04-18 06:01:48 +08:00
|
|
|
// The interesting things begin when you have virtual inheritance.
|
|
|
|
// The final overrider will use a static adjustment equal to the offset
|
|
|
|
// of the vbase in the final overrider class.
|
|
|
|
// For example, if the final overrider is in a vbase B of the most
|
|
|
|
// derived class and it overrides a method of the B's own vbase A,
|
|
|
|
// it uses A* as "this". In its prologue, it can cast A* to B* with
|
|
|
|
// a static offset. This offset is used regardless of the actual
|
|
|
|
// offset of A from B in the most derived class, requiring an
|
|
|
|
// this-adjusting thunk in the vftable if A and B are laid out
|
|
|
|
// differently in the most derived class.
|
|
|
|
LastVBaseOffset = ThisOffset =
|
|
|
|
Overrider.Offset + OverriderRDLayout.getVBaseClassOffset(CurRD);
|
2013-07-30 17:46:19 +08:00
|
|
|
} else {
|
|
|
|
ThisOffset += Layout.getBaseClassOffset(CurRD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:17:38 +08:00
|
|
|
if (isa<CXXDestructorDecl>(Overrider.Method)) {
|
2013-11-06 14:24:31 +08:00
|
|
|
if (LastVBaseOffset.isZero()) {
|
|
|
|
// If a "Base" class has at least one non-virtual base with a virtual
|
|
|
|
// destructor, the "Base" virtual destructor will take the address
|
|
|
|
// of the "Base" subobject as the "this" argument.
|
2014-03-05 02:17:38 +08:00
|
|
|
ThisOffset = Overrider.Offset;
|
2013-11-06 14:24:31 +08:00
|
|
|
} else {
|
|
|
|
// A virtual destructor of a virtual base takes the address of the
|
|
|
|
// virtual base subobject as the "this" argument.
|
2014-03-05 02:17:38 +08:00
|
|
|
ThisOffset = LastVBaseOffset;
|
2013-11-06 14:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 02:24:06 +08:00
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
if (Ret > ThisOffset || First) {
|
|
|
|
First = false;
|
|
|
|
Ret = ThisOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!First && "Method not found in the given subobject?");
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:11:05 +08:00
|
|
|
// Things are getting even more complex when the "this" adjustment has to
|
|
|
|
// use a dynamic offset instead of a static one, or even two dynamic offsets.
|
|
|
|
// This is sometimes required when a virtual call happens in the middle of
|
|
|
|
// a non-most-derived class construction or destruction.
|
|
|
|
//
|
|
|
|
// Let's take a look at the following example:
|
|
|
|
// struct A {
|
|
|
|
// virtual void f();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// void foo(A *a) { a->f(); } // Knows nothing about siblings of A.
|
|
|
|
//
|
|
|
|
// struct B : virtual A {
|
|
|
|
// virtual void f();
|
|
|
|
// B() {
|
|
|
|
// foo(this);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct C : virtual B {
|
|
|
|
// virtual void f();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// Record layouts for these classes are:
|
|
|
|
// struct A
|
|
|
|
// 0 | (A vftable pointer)
|
|
|
|
//
|
|
|
|
// struct B
|
|
|
|
// 0 | (B vbtable pointer)
|
|
|
|
// 4 | (vtordisp for vbase A)
|
|
|
|
// 8 | struct A (virtual base)
|
|
|
|
// 8 | (A vftable pointer)
|
|
|
|
//
|
|
|
|
// struct C
|
|
|
|
// 0 | (C vbtable pointer)
|
|
|
|
// 4 | (vtordisp for vbase A)
|
|
|
|
// 8 | struct A (virtual base) // A precedes B!
|
|
|
|
// 8 | (A vftable pointer)
|
|
|
|
// 12 | struct B (virtual base)
|
|
|
|
// 12 | (B vbtable pointer)
|
|
|
|
//
|
|
|
|
// When one creates an object of type C, the C constructor:
|
|
|
|
// - initializes all the vbptrs, then
|
|
|
|
// - calls the A subobject constructor
|
|
|
|
// (initializes A's vfptr with an address of A vftable), then
|
|
|
|
// - calls the B subobject constructor
|
|
|
|
// (initializes A's vfptr with an address of B vftable and vtordisp for A),
|
|
|
|
// that in turn calls foo(), then
|
|
|
|
// - initializes A's vfptr with an address of C vftable and zeroes out the
|
|
|
|
// vtordisp
|
|
|
|
// FIXME: if a structor knows it belongs to MDC, why doesn't it use a vftable
|
|
|
|
// without vtordisp thunks?
|
2014-11-17 23:53:50 +08:00
|
|
|
// FIXME: how are vtordisp handled in the presence of nooverride/final?
|
2014-11-17 23:11:05 +08:00
|
|
|
//
|
|
|
|
// When foo() is called, an object with a layout of class C has a vftable
|
|
|
|
// referencing B::f() that assumes a B layout, so the "this" adjustments are
|
|
|
|
// incorrect, unless an extra adjustment is done. This adjustment is called
|
|
|
|
// "vtordisp adjustment". Vtordisp basically holds the difference between the
|
|
|
|
// actual location of a vbase in the layout class and the location assumed by
|
|
|
|
// the vftable of the class being constructed/destructed. Vtordisp is only
|
|
|
|
// needed if "this" escapes a
|
|
|
|
// structor (or we can't prove otherwise).
|
|
|
|
// [i.e. vtordisp is a dynamic adjustment for a static adjustment, which is an
|
|
|
|
// estimation of a dynamic adjustment]
|
|
|
|
//
|
|
|
|
// foo() gets a pointer to the A vbase and doesn't know anything about B or C,
|
|
|
|
// so it just passes that pointer as "this" in a virtual call.
|
|
|
|
// If there was no vtordisp, that would just dispatch to B::f().
|
|
|
|
// However, B::f() assumes B+8 is passed as "this",
|
|
|
|
// yet the pointer foo() passes along is B-4 (i.e. C+8).
|
|
|
|
// An extra adjustment is needed, so we emit a thunk into the B vftable.
|
|
|
|
// This vtordisp thunk subtracts the value of vtordisp
|
|
|
|
// from the "this" argument (-12) before making a tailcall to B::f().
|
|
|
|
//
|
|
|
|
// Let's consider an even more complex example:
|
|
|
|
// struct D : virtual B, virtual C {
|
|
|
|
// D() {
|
|
|
|
// foo(this);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct D
|
|
|
|
// 0 | (D vbtable pointer)
|
|
|
|
// 4 | (vtordisp for vbase A)
|
|
|
|
// 8 | struct A (virtual base) // A precedes both B and C!
|
|
|
|
// 8 | (A vftable pointer)
|
|
|
|
// 12 | struct B (virtual base) // B precedes C!
|
|
|
|
// 12 | (B vbtable pointer)
|
|
|
|
// 16 | struct C (virtual base)
|
|
|
|
// 16 | (C vbtable pointer)
|
|
|
|
//
|
|
|
|
// When D::D() calls foo(), we find ourselves in a thunk that should tailcall
|
|
|
|
// to C::f(), which assumes C+8 as its "this" parameter. This time, foo()
|
|
|
|
// passes along A, which is C-8. The A vtordisp holds
|
|
|
|
// "D.vbptr[index_of_A] - offset_of_A_in_D"
|
|
|
|
// and we statically know offset_of_A_in_D, so can get a pointer to D.
|
|
|
|
// When we know it, we can make an extra vbtable lookup to locate the C vbase
|
|
|
|
// and one extra static adjustment to calculate the expected value of C+8.
|
2013-11-06 14:24:31 +08:00
|
|
|
void VFTableBuilder::CalculateVtordispAdjustment(
|
|
|
|
FinalOverriders::OverriderInfo Overrider, CharUnits ThisOffset,
|
|
|
|
ThisAdjustment &TA) {
|
|
|
|
const ASTRecordLayout::VBaseOffsetsMapTy &VBaseMap =
|
|
|
|
MostDerivedClassLayout.getVBaseOffsetsMap();
|
|
|
|
const ASTRecordLayout::VBaseOffsetsMapTy::const_iterator &VBaseMapEntry =
|
2014-02-28 03:40:09 +08:00
|
|
|
VBaseMap.find(WhichVFPtr.getVBaseWithVPtr());
|
2013-11-06 14:24:31 +08:00
|
|
|
assert(VBaseMapEntry != VBaseMap.end());
|
|
|
|
|
2014-04-23 01:32:02 +08:00
|
|
|
// If there's no vtordisp or the final overrider is defined in the same vbase
|
|
|
|
// as the initial declaration, we don't need any vtordisp adjustment.
|
|
|
|
if (!VBaseMapEntry->second.hasVtorDisp() ||
|
|
|
|
Overrider.VirtualBase == WhichVFPtr.getVBaseWithVPtr())
|
2014-04-17 19:01:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// OK, now we know we need to use a vtordisp thunk.
|
2013-11-06 14:24:31 +08:00
|
|
|
// The implicit vtordisp field is located right before the vbase.
|
2014-11-17 23:53:50 +08:00
|
|
|
CharUnits OffsetOfVBaseWithVFPtr = VBaseMapEntry->second.VBaseOffset;
|
2013-11-06 14:24:31 +08:00
|
|
|
TA.Virtual.Microsoft.VtordispOffset =
|
2014-11-17 23:53:50 +08:00
|
|
|
(OffsetOfVBaseWithVFPtr - WhichVFPtr.FullOffsetInMDC).getQuantity() - 4;
|
2013-11-06 14:24:31 +08:00
|
|
|
|
2014-04-17 19:01:41 +08:00
|
|
|
// A simple vtordisp thunk will suffice if the final overrider is defined
|
|
|
|
// in either the most derived class or its non-virtual base.
|
2014-04-23 01:32:02 +08:00
|
|
|
if (Overrider.Method->getParent() == MostDerivedClass ||
|
|
|
|
!Overrider.VirtualBase)
|
2013-11-06 14:24:31 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise, we need to do use the dynamic offset of the final overrider
|
|
|
|
// in order to get "this" adjustment right.
|
|
|
|
TA.Virtual.Microsoft.VBPtrOffset =
|
2014-11-17 23:53:50 +08:00
|
|
|
(OffsetOfVBaseWithVFPtr + WhichVFPtr.NonVirtualOffset -
|
2013-11-06 14:24:31 +08:00
|
|
|
MostDerivedClassLayout.getVBPtrOffset()).getQuantity();
|
|
|
|
TA.Virtual.Microsoft.VBOffsetOffset =
|
|
|
|
Context.getTypeSizeInChars(Context.IntTy).getQuantity() *
|
2014-04-23 01:32:02 +08:00
|
|
|
VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase);
|
2013-11-06 14:24:31 +08:00
|
|
|
|
|
|
|
TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity();
|
|
|
|
}
|
|
|
|
|
2013-10-06 23:31:37 +08:00
|
|
|
static void GroupNewVirtualOverloads(
|
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
SmallVector<const CXXMethodDecl *, 10> &VirtualMethods) {
|
|
|
|
// Put the virtual methods into VirtualMethods in the proper order:
|
|
|
|
// 1) Group overloads by declaration name. New groups are added to the
|
|
|
|
// vftable in the order of their first declarations in this class
|
2015-11-19 08:03:54 +08:00
|
|
|
// (including overrides, non-virtual methods and any other named decl that
|
|
|
|
// might be nested within the class).
|
2013-10-06 23:31:37 +08:00
|
|
|
// 2) In each group, new overloads appear in the reverse order of declaration.
|
|
|
|
typedef SmallVector<const CXXMethodDecl *, 1> MethodGroup;
|
|
|
|
SmallVector<MethodGroup, 10> Groups;
|
|
|
|
typedef llvm::DenseMap<DeclarationName, unsigned> VisitedGroupIndicesTy;
|
|
|
|
VisitedGroupIndicesTy VisitedGroupIndices;
|
2015-11-19 08:03:54 +08:00
|
|
|
for (const auto *D : RD->decls()) {
|
|
|
|
const auto *ND = dyn_cast<NamedDecl>(D);
|
|
|
|
if (!ND)
|
|
|
|
continue;
|
2013-10-06 23:31:37 +08:00
|
|
|
VisitedGroupIndicesTy::iterator J;
|
|
|
|
bool Inserted;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(J, Inserted) = VisitedGroupIndices.insert(
|
2015-11-19 08:03:54 +08:00
|
|
|
std::make_pair(ND->getDeclName(), Groups.size()));
|
2013-10-06 23:31:37 +08:00
|
|
|
if (Inserted)
|
2014-02-20 06:06:10 +08:00
|
|
|
Groups.push_back(MethodGroup());
|
2015-11-19 08:03:54 +08:00
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(ND))
|
|
|
|
if (MD->isVirtual())
|
|
|
|
Groups[J->second].push_back(MD->getCanonicalDecl());
|
2013-10-06 23:31:37 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const MethodGroup &Group : Groups)
|
|
|
|
VirtualMethods.append(Group.rbegin(), Group.rend());
|
2013-10-06 23:31:37 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 16:12:53 +08:00
|
|
|
static bool isDirectVBase(const CXXRecordDecl *Base, const CXXRecordDecl *RD) {
|
|
|
|
for (const auto &B : RD->bases()) {
|
|
|
|
if (B.isVirtual() && B.getType()->getAsCXXRecordDecl() == Base)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
void VFTableBuilder::AddMethods(BaseSubobject Base, unsigned BaseDepth,
|
|
|
|
const CXXRecordDecl *LastVBase,
|
|
|
|
BasesSetVectorTy &VisitedBases) {
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
if (!RD->isPolymorphic())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// See if this class expands a vftable of the base we look at, which is either
|
2015-05-17 07:49:53 +08:00
|
|
|
// the one defined by the vfptr base path or the primary base of the current
|
|
|
|
// class.
|
2014-05-12 13:36:57 +08:00
|
|
|
const CXXRecordDecl *NextBase = nullptr, *NextLastVBase = LastVBase;
|
2013-07-30 17:46:19 +08:00
|
|
|
CharUnits NextBaseOffset;
|
2016-07-20 22:40:25 +08:00
|
|
|
if (BaseDepth < WhichVFPtr.PathToIntroducingObject.size()) {
|
|
|
|
NextBase = WhichVFPtr.PathToIntroducingObject[BaseDepth];
|
2014-03-26 16:12:53 +08:00
|
|
|
if (isDirectVBase(NextBase, RD)) {
|
2013-07-30 17:46:19 +08:00
|
|
|
NextLastVBase = NextBase;
|
|
|
|
NextBaseOffset = MostDerivedClassLayout.getVBaseClassOffset(NextBase);
|
|
|
|
} else {
|
|
|
|
NextBaseOffset =
|
|
|
|
Base.getBaseOffset() + Layout.getBaseClassOffset(NextBase);
|
|
|
|
}
|
|
|
|
} else if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
|
|
|
assert(!Layout.isPrimaryBaseVirtual() &&
|
|
|
|
"No primary virtual bases in this ABI");
|
|
|
|
NextBase = PrimaryBase;
|
|
|
|
NextBaseOffset = Base.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NextBase) {
|
|
|
|
AddMethods(BaseSubobject(NextBase, NextBaseOffset), BaseDepth + 1,
|
|
|
|
NextLastVBase, VisitedBases);
|
|
|
|
if (!VisitedBases.insert(NextBase))
|
|
|
|
llvm_unreachable("Found a duplicate primary base!");
|
|
|
|
}
|
|
|
|
|
2013-10-06 23:31:37 +08:00
|
|
|
SmallVector<const CXXMethodDecl*, 10> VirtualMethods;
|
|
|
|
// Put virtual methods in the proper order.
|
|
|
|
GroupNewVirtualOverloads(RD, VirtualMethods);
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
// Now go through all virtual member functions and add them to the current
|
|
|
|
// vftable. This is done by
|
|
|
|
// - replacing overridden methods in their existing slots, as long as they
|
|
|
|
// don't require return adjustment; calculating This adjustment if needed.
|
|
|
|
// - adding new slots for methods of the current base not present in any
|
|
|
|
// sub-bases;
|
|
|
|
// - adding new slots for methods that require Return adjustment.
|
|
|
|
// We keep track of the methods visited in the sub-bases in MethodInfoMap.
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXMethodDecl *MD : VirtualMethods) {
|
2014-11-17 23:53:50 +08:00
|
|
|
FinalOverriders::OverriderInfo FinalOverrider =
|
2013-07-30 17:46:19 +08:00
|
|
|
Overriders.getOverrider(MD, Base.getBaseOffset());
|
2014-11-17 23:53:50 +08:00
|
|
|
const CXXMethodDecl *FinalOverriderMD = FinalOverrider.Method;
|
2014-03-07 17:34:59 +08:00
|
|
|
const CXXMethodDecl *OverriddenMD =
|
|
|
|
FindNearestOverriddenMethod(MD, VisitedBases);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2014-03-07 17:34:59 +08:00
|
|
|
ThisAdjustment ThisAdjustmentOffset;
|
2014-08-10 19:40:51 +08:00
|
|
|
bool ReturnAdjustingThunk = false, ForceReturnAdjustmentMangling = false;
|
2014-11-17 23:53:50 +08:00
|
|
|
CharUnits ThisOffset = ComputeThisOffset(FinalOverrider);
|
2014-03-07 17:34:59 +08:00
|
|
|
ThisAdjustmentOffset.NonVirtual =
|
|
|
|
(ThisOffset - WhichVFPtr.FullOffsetInMDC).getQuantity();
|
2014-11-17 23:53:50 +08:00
|
|
|
if ((OverriddenMD || FinalOverriderMD != MD) &&
|
2014-03-07 17:34:59 +08:00
|
|
|
WhichVFPtr.getVBaseWithVPtr())
|
2014-11-17 23:53:50 +08:00
|
|
|
CalculateVtordispAdjustment(FinalOverrider, ThisOffset,
|
|
|
|
ThisAdjustmentOffset);
|
2014-03-07 17:34:59 +08:00
|
|
|
|
2017-08-30 01:40:04 +08:00
|
|
|
unsigned VBIndex =
|
|
|
|
LastVBase ? VTables.getVBTableIndex(MostDerivedClass, LastVBase) : 0;
|
|
|
|
|
2014-03-07 17:34:59 +08:00
|
|
|
if (OverriddenMD) {
|
2015-05-17 07:49:53 +08:00
|
|
|
// If MD overrides anything in this vftable, we need to update the
|
|
|
|
// entries.
|
2013-07-30 17:46:19 +08:00
|
|
|
MethodInfoMapTy::iterator OverriddenMDIterator =
|
|
|
|
MethodInfoMap.find(OverriddenMD);
|
|
|
|
|
|
|
|
// If the overridden method went to a different vftable, skip it.
|
|
|
|
if (OverriddenMDIterator == MethodInfoMap.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MethodInfo &OverriddenMethodInfo = OverriddenMDIterator->second;
|
|
|
|
|
2017-08-30 01:40:04 +08:00
|
|
|
VBIndex = OverriddenMethodInfo.VBTableIndex;
|
|
|
|
|
2014-08-10 19:40:51 +08:00
|
|
|
// Let's check if the overrider requires any return adjustments.
|
|
|
|
// We must create a new slot if the MD's return type is not trivially
|
|
|
|
// convertible to the OverriddenMD's one.
|
|
|
|
// Once a chain of method overrides adds a return adjusting vftable slot,
|
|
|
|
// all subsequent overrides will also use an extra method slot.
|
|
|
|
ReturnAdjustingThunk = !ComputeReturnAdjustmentBaseOffset(
|
|
|
|
Context, MD, OverriddenMD).isEmpty() ||
|
|
|
|
OverriddenMethodInfo.UsesExtraSlot;
|
|
|
|
|
|
|
|
if (!ReturnAdjustingThunk) {
|
2013-07-30 17:46:19 +08:00
|
|
|
// No return adjustment needed - just replace the overridden method info
|
|
|
|
// with the current info.
|
2017-08-30 01:40:04 +08:00
|
|
|
MethodInfo MI(VBIndex, OverriddenMethodInfo.VFTableIndex);
|
2013-07-30 17:46:19 +08:00
|
|
|
MethodInfoMap.erase(OverriddenMDIterator);
|
|
|
|
|
|
|
|
assert(!MethodInfoMap.count(MD) &&
|
|
|
|
"Should not have method info for this method yet!");
|
|
|
|
MethodInfoMap.insert(std::make_pair(MD, MI));
|
|
|
|
continue;
|
2013-12-28 04:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case we need a return adjustment, we'll add a new slot for
|
2018-04-06 23:14:32 +08:00
|
|
|
// the overrider. Mark the overridden method as shadowed by the new slot.
|
2013-12-28 04:29:16 +08:00
|
|
|
OverriddenMethodInfo.Shadowed = true;
|
2014-03-07 17:34:59 +08:00
|
|
|
|
|
|
|
// Force a special name mangling for a return-adjusting thunk
|
|
|
|
// unless the method is the final overrider without this adjustment.
|
2014-08-10 19:40:51 +08:00
|
|
|
ForceReturnAdjustmentMangling =
|
2014-11-17 23:53:50 +08:00
|
|
|
!(MD == FinalOverriderMD && ThisAdjustmentOffset.isEmpty());
|
2014-02-28 03:40:09 +08:00
|
|
|
} else if (Base.getBaseOffset() != WhichVFPtr.FullOffsetInMDC ||
|
2013-07-30 17:46:19 +08:00
|
|
|
MD->size_overridden_methods()) {
|
|
|
|
// Skip methods that don't belong to the vftable of the current class,
|
|
|
|
// e.g. each method that wasn't seen in any of the visited sub-bases
|
|
|
|
// but overrides multiple methods of other sub-bases.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, MD is a method not seen in any of the sub-bases or
|
|
|
|
// it requires return adjustment. Insert the method info for this method.
|
2014-07-02 05:10:07 +08:00
|
|
|
MethodInfo MI(VBIndex,
|
2014-08-10 19:40:51 +08:00
|
|
|
HasRTTIComponent ? Components.size() - 1 : Components.size(),
|
|
|
|
ReturnAdjustingThunk);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
assert(!MethodInfoMap.count(MD) &&
|
|
|
|
"Should not have method info for this method yet!");
|
|
|
|
MethodInfoMap.insert(std::make_pair(MD, MI));
|
|
|
|
|
|
|
|
// Check if this overrider needs a return adjustment.
|
|
|
|
// We don't want to do this for pure virtual member functions.
|
|
|
|
BaseOffset ReturnAdjustmentOffset;
|
|
|
|
ReturnAdjustment ReturnAdjustment;
|
2014-11-17 23:53:50 +08:00
|
|
|
if (!FinalOverriderMD->isPure()) {
|
2013-07-30 17:46:19 +08:00
|
|
|
ReturnAdjustmentOffset =
|
2014-11-17 23:53:50 +08:00
|
|
|
ComputeReturnAdjustmentBaseOffset(Context, FinalOverriderMD, MD);
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
if (!ReturnAdjustmentOffset.isEmpty()) {
|
2014-08-10 19:40:51 +08:00
|
|
|
ForceReturnAdjustmentMangling = true;
|
2013-07-30 17:46:19 +08:00
|
|
|
ReturnAdjustment.NonVirtual =
|
|
|
|
ReturnAdjustmentOffset.NonVirtualOffset.getQuantity();
|
|
|
|
if (ReturnAdjustmentOffset.VirtualBase) {
|
2013-10-30 19:55:43 +08:00
|
|
|
const ASTRecordLayout &DerivedLayout =
|
|
|
|
Context.getASTRecordLayout(ReturnAdjustmentOffset.DerivedClass);
|
|
|
|
ReturnAdjustment.Virtual.Microsoft.VBPtrOffset =
|
|
|
|
DerivedLayout.getVBPtrOffset().getQuantity();
|
|
|
|
ReturnAdjustment.Virtual.Microsoft.VBIndex =
|
2013-11-05 23:54:58 +08:00
|
|
|
VTables.getVBTableIndex(ReturnAdjustmentOffset.DerivedClass,
|
|
|
|
ReturnAdjustmentOffset.VirtualBase);
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:53:50 +08:00
|
|
|
AddMethod(FinalOverriderMD,
|
2014-08-10 19:40:51 +08:00
|
|
|
ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment,
|
|
|
|
ForceReturnAdjustmentMangling ? MD : nullptr));
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
static void PrintBasePath(const VPtrInfo::BasePath &Path, raw_ostream &Out) {
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXRecordDecl *Elem :
|
|
|
|
llvm::make_range(Path.rbegin(), Path.rend())) {
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << "'";
|
2015-07-26 01:10:49 +08:00
|
|
|
Elem->printQualifiedName(Out);
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << "' in ";
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-30 19:55:43 +08:00
|
|
|
static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out,
|
|
|
|
bool ContinueFirstLine) {
|
|
|
|
const ReturnAdjustment &R = TI.Return;
|
|
|
|
bool Multiline = false;
|
2014-03-07 17:34:59 +08:00
|
|
|
const char *LinePrefix = "\n ";
|
|
|
|
if (!R.isEmpty() || TI.Method) {
|
2013-10-30 19:55:43 +08:00
|
|
|
if (!ContinueFirstLine)
|
|
|
|
Out << LinePrefix;
|
2014-03-07 17:34:59 +08:00
|
|
|
Out << "[return adjustment (to type '"
|
|
|
|
<< TI.Method->getReturnType().getCanonicalType().getAsString()
|
|
|
|
<< "'): ";
|
2013-10-30 19:55:43 +08:00
|
|
|
if (R.Virtual.Microsoft.VBPtrOffset)
|
|
|
|
Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", ";
|
|
|
|
if (R.Virtual.Microsoft.VBIndex)
|
|
|
|
Out << "vbase #" << R.Virtual.Microsoft.VBIndex << ", ";
|
|
|
|
Out << R.NonVirtual << " non-virtual]";
|
|
|
|
Multiline = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ThisAdjustment &T = TI.This;
|
|
|
|
if (!T.isEmpty()) {
|
|
|
|
if (Multiline || !ContinueFirstLine)
|
|
|
|
Out << LinePrefix;
|
|
|
|
Out << "[this adjustment: ";
|
2013-11-06 14:24:31 +08:00
|
|
|
if (!TI.This.Virtual.isEmpty()) {
|
|
|
|
assert(T.Virtual.Microsoft.VtordispOffset < 0);
|
|
|
|
Out << "vtordisp at " << T.Virtual.Microsoft.VtordispOffset << ", ";
|
|
|
|
if (T.Virtual.Microsoft.VBPtrOffset) {
|
|
|
|
Out << "vbptr at " << T.Virtual.Microsoft.VBPtrOffset
|
2014-03-07 17:34:59 +08:00
|
|
|
<< " to the left,";
|
2013-11-06 14:24:31 +08:00
|
|
|
assert(T.Virtual.Microsoft.VBOffsetOffset > 0);
|
|
|
|
Out << LinePrefix << " vboffset at "
|
|
|
|
<< T.Virtual.Microsoft.VBOffsetOffset << " in the vbtable, ";
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 19:55:43 +08:00
|
|
|
Out << T.NonVirtual << " non-virtual]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
void VFTableBuilder::dumpLayout(raw_ostream &Out) {
|
|
|
|
Out << "VFTable for ";
|
2016-07-20 22:40:25 +08:00
|
|
|
PrintBasePath(WhichVFPtr.PathToIntroducingObject, Out);
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << "'";
|
|
|
|
MostDerivedClass->printQualifiedName(Out);
|
2014-03-05 21:54:07 +08:00
|
|
|
Out << "' (" << Components.size()
|
|
|
|
<< (Components.size() == 1 ? " entry" : " entries") << ").\n";
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0, E = Components.size(); I != E; ++I) {
|
|
|
|
Out << llvm::format("%4d | ", I);
|
|
|
|
|
|
|
|
const VTableComponent &Component = Components[I];
|
|
|
|
|
|
|
|
// Dump the component.
|
|
|
|
switch (Component.getKind()) {
|
|
|
|
case VTableComponent::CK_RTTI:
|
2014-01-04 02:42:48 +08:00
|
|
|
Component.getRTTIDecl()->printQualifiedName(Out);
|
|
|
|
Out << " RTTI";
|
2013-07-30 17:46:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VTableComponent::CK_FunctionPointer: {
|
|
|
|
const CXXMethodDecl *MD = Component.getFunctionDecl();
|
|
|
|
|
2013-12-28 03:43:59 +08:00
|
|
|
// FIXME: Figure out how to print the real thunk type, since they can
|
|
|
|
// differ in the return type.
|
2013-07-30 17:46:19 +08:00
|
|
|
std::string Str = PredefinedExpr::ComputeName(
|
|
|
|
PredefinedExpr::PrettyFunctionNoVirtual, MD);
|
|
|
|
Out << Str;
|
|
|
|
if (MD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
|
2014-09-12 12:38:08 +08:00
|
|
|
if (MD->isDeleted())
|
2013-07-30 17:46:19 +08:00
|
|
|
Out << " [deleted]";
|
|
|
|
|
|
|
|
ThunkInfo Thunk = VTableThunks.lookup(I);
|
2013-10-30 19:55:43 +08:00
|
|
|
if (!Thunk.isEmpty())
|
|
|
|
dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
|
|
|
const CXXDestructorDecl *DD = Component.getDestructorDecl();
|
|
|
|
|
2014-01-04 02:42:48 +08:00
|
|
|
DD->printQualifiedName(Out);
|
2013-07-30 17:46:19 +08:00
|
|
|
Out << "() [scalar deleting]";
|
|
|
|
|
|
|
|
if (DD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
|
|
|
|
ThunkInfo Thunk = VTableThunks.lookup(I);
|
|
|
|
if (!Thunk.isEmpty()) {
|
|
|
|
assert(Thunk.Return.isEmpty() &&
|
|
|
|
"No return adjustment needed for destructors!");
|
2013-10-30 19:55:43 +08:00
|
|
|
dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
DiagnosticsEngine &Diags = Context.getDiagnostics();
|
|
|
|
unsigned DiagID = Diags.getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error,
|
|
|
|
"Unexpected vftable component type %0 for component number %1");
|
|
|
|
Diags.Report(MostDerivedClass->getLocation(), DiagID)
|
|
|
|
<< I << Component.getKind();
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
|
|
|
|
|
|
|
if (!Thunks.empty()) {
|
|
|
|
// We store the method names in a map to get a stable order.
|
|
|
|
std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : Thunks) {
|
|
|
|
const CXXMethodDecl *MD = I.first;
|
2013-07-30 17:46:19 +08:00
|
|
|
std::string MethodName = PredefinedExpr::ComputeName(
|
|
|
|
PredefinedExpr::PrettyFunctionNoVirtual, MD);
|
|
|
|
|
|
|
|
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
|
|
|
|
}
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &MethodNameAndDecl : MethodNamesAndDecls) {
|
|
|
|
const std::string &MethodName = MethodNameAndDecl.first;
|
|
|
|
const CXXMethodDecl *MD = MethodNameAndDecl.second;
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
ThunkInfoVectorTy ThunksVector = Thunks[MD];
|
2019-04-24 22:43:05 +08:00
|
|
|
llvm::stable_sort(ThunksVector, [](const ThunkInfo &LHS,
|
|
|
|
const ThunkInfo &RHS) {
|
2014-03-01 22:48:57 +08:00
|
|
|
// Keep different thunks with the same adjustments in the order they
|
|
|
|
// were put into the vector.
|
2014-03-04 04:26:46 +08:00
|
|
|
return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
|
2014-03-01 22:48:57 +08:00
|
|
|
});
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
|
|
|
|
Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
|
|
|
|
const ThunkInfo &Thunk = ThunksVector[I];
|
|
|
|
|
|
|
|
Out << llvm::format("%4d | ", I);
|
2013-10-30 19:55:43 +08:00
|
|
|
dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/true);
|
2013-07-30 17:46:19 +08:00
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
}
|
2014-03-20 21:42:14 +08:00
|
|
|
|
|
|
|
Out.flush();
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
static bool setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *, 4> &A,
|
2014-08-27 14:28:16 +08:00
|
|
|
ArrayRef<const CXXRecordDecl *> B) {
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const CXXRecordDecl *Decl : B) {
|
|
|
|
if (A.count(Decl))
|
2014-01-04 07:42:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
static bool rebucketPaths(VPtrInfoVector &Paths);
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
/// Produces MSVC-compatible vbtable data. The symbols produced by this
|
|
|
|
/// algorithm match those produced by MSVC 2012 and newer, which is different
|
|
|
|
/// from MSVC 2010.
|
2014-01-03 08:14:35 +08:00
|
|
|
///
|
|
|
|
/// MSVC 2012 appears to minimize the vbtable names using the following
|
|
|
|
/// algorithm. First, walk the class hierarchy in the usual order, depth first,
|
|
|
|
/// left to right, to find all of the subobjects which contain a vbptr field.
|
|
|
|
/// Visiting each class node yields a list of inheritance paths to vbptrs. Each
|
|
|
|
/// record with a vbptr creates an initially empty path.
|
|
|
|
///
|
|
|
|
/// To combine paths from child nodes, the paths are compared to check for
|
|
|
|
/// ambiguity. Paths are "ambiguous" if multiple paths have the same set of
|
|
|
|
/// components in the same order. Each group of ambiguous paths is extended by
|
|
|
|
/// appending the class of the base from which it came. If the current class
|
|
|
|
/// node produced an ambiguous path, its path is extended with the current class.
|
|
|
|
/// After extending paths, MSVC again checks for ambiguity, and extends any
|
|
|
|
/// ambiguous path which wasn't already extended. Because each node yields an
|
|
|
|
/// unambiguous set of paths, MSVC doesn't need to extend any path more than once
|
|
|
|
/// to produce an unambiguous set of paths.
|
|
|
|
///
|
|
|
|
/// TODO: Presumably vftables use the same algorithm.
|
2014-02-28 03:40:09 +08:00
|
|
|
void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
|
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
VPtrInfoVector &Paths) {
|
2014-01-04 07:42:00 +08:00
|
|
|
assert(Paths.empty());
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
// Base case: this subobject has its own vptr.
|
|
|
|
if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr())
|
2019-08-15 07:04:18 +08:00
|
|
|
Paths.push_back(std::make_unique<VPtrInfo>(RD));
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
// Recursive case: get all the vbtables from our bases and remove anything
|
2014-09-23 07:14:46 +08:00
|
|
|
// that shares a virtual base.
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen;
|
2014-09-23 07:14:46 +08:00
|
|
|
for (const auto &B : RD->bases()) {
|
2014-03-26 16:22:48 +08:00
|
|
|
const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
|
|
|
|
if (B.isVirtual() && VBasesSeen.count(Base))
|
2014-01-04 07:42:00 +08:00
|
|
|
continue;
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
if (!Base->isDynamicClass())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const VPtrInfoVector &BasePaths =
|
|
|
|
ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base);
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2016-10-11 00:26:29 +08:00
|
|
|
for (const std::unique_ptr<VPtrInfo> &BaseInfo : BasePaths) {
|
2014-01-04 07:42:00 +08:00
|
|
|
// Don't include the path if it goes through a virtual base that we've
|
|
|
|
// already included.
|
2014-02-28 03:40:09 +08:00
|
|
|
if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases))
|
2014-01-04 07:42:00 +08:00
|
|
|
continue;
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
// Copy the path and adjust it as necessary.
|
2019-08-15 07:04:18 +08:00
|
|
|
auto P = std::make_unique<VPtrInfo>(*BaseInfo);
|
2014-01-04 07:42:00 +08:00
|
|
|
|
|
|
|
// We mangle Base into the path if the path would've been ambiguous and it
|
|
|
|
// wasn't already extended with Base.
|
|
|
|
if (P->MangledPath.empty() || P->MangledPath.back() != Base)
|
|
|
|
P->NextBaseToMangle = Base;
|
|
|
|
|
2014-04-18 06:47:52 +08:00
|
|
|
// Keep track of which vtable the derived class is going to extend with
|
|
|
|
// new methods or bases. We append to either the vftable of our primary
|
|
|
|
// base, or the first non-virtual base that has a vbtable.
|
2016-07-20 22:40:25 +08:00
|
|
|
if (P->ObjectWithVPtr == Base &&
|
2014-04-18 06:47:52 +08:00
|
|
|
Base == (ForVBTables ? Layout.getBaseSharingVBPtr()
|
2014-02-28 03:40:09 +08:00
|
|
|
: Layout.getPrimaryBase()))
|
2016-07-20 22:40:25 +08:00
|
|
|
P->ObjectWithVPtr = RD;
|
2014-04-18 06:47:52 +08:00
|
|
|
|
|
|
|
// Keep track of the full adjustment from the MDC to this vtable. The
|
|
|
|
// adjustment is captured by an optional vbase and a non-virtual offset.
|
2014-03-26 16:22:48 +08:00
|
|
|
if (B.isVirtual())
|
2014-01-04 07:42:00 +08:00
|
|
|
P->ContainingVBases.push_back(Base);
|
|
|
|
else if (P->ContainingVBases.empty())
|
|
|
|
P->NonVirtualOffset += Layout.getBaseClassOffset(Base);
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
// Update the full offset in the MDC.
|
|
|
|
P->FullOffsetInMDC = P->NonVirtualOffset;
|
|
|
|
if (const CXXRecordDecl *VB = P->getVBaseWithVPtr())
|
|
|
|
P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB);
|
|
|
|
|
2016-10-11 00:26:29 +08:00
|
|
|
Paths.push_back(std::move(P));
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
2014-03-31 19:01:51 +08:00
|
|
|
if (B.isVirtual())
|
|
|
|
VBasesSeen.insert(Base);
|
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
// After visiting any direct base, we've transitively visited all of its
|
|
|
|
// morally virtual bases.
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &VB : Base->vbases())
|
|
|
|
VBasesSeen.insert(VB.getType()->getAsCXXRecordDecl());
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
// Sort the paths into buckets, and if any of them are ambiguous, extend all
|
|
|
|
// paths in ambiguous buckets.
|
|
|
|
bool Changed = true;
|
|
|
|
while (Changed)
|
|
|
|
Changed = rebucketPaths(Paths);
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:29 +08:00
|
|
|
static bool extendPath(VPtrInfo &P) {
|
|
|
|
if (P.NextBaseToMangle) {
|
|
|
|
P.MangledPath.push_back(P.NextBaseToMangle);
|
|
|
|
P.NextBaseToMangle = nullptr;// Prevent the path from being extended twice.
|
2014-01-04 07:42:00 +08:00
|
|
|
return true;
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
2014-01-04 07:42:00 +08:00
|
|
|
return false;
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
static bool rebucketPaths(VPtrInfoVector &Paths) {
|
2014-01-03 08:14:35 +08:00
|
|
|
// What we're essentially doing here is bucketing together ambiguous paths.
|
|
|
|
// Any bucket with more than one path in it gets extended by NextBase, which
|
|
|
|
// is usually the direct base of the inherited the vbptr. This code uses a
|
|
|
|
// sorted vector to implement a multiset to form the buckets. Note that the
|
|
|
|
// ordering is based on pointers, but it doesn't change our output order. The
|
|
|
|
// current algorithm is designed to match MSVC 2012's names.
|
2016-10-11 03:26:22 +08:00
|
|
|
llvm::SmallVector<std::reference_wrapper<VPtrInfo>, 2> PathsSorted;
|
|
|
|
PathsSorted.reserve(Paths.size());
|
|
|
|
for (auto& P : Paths)
|
|
|
|
PathsSorted.push_back(*P);
|
2018-09-27 06:16:28 +08:00
|
|
|
llvm::sort(PathsSorted, [](const VPtrInfo &LHS, const VPtrInfo &RHS) {
|
2016-10-11 00:26:29 +08:00
|
|
|
return LHS.MangledPath < RHS.MangledPath;
|
2014-03-08 05:35:40 +08:00
|
|
|
});
|
2014-01-04 07:42:00 +08:00
|
|
|
bool Changed = false;
|
2014-01-03 08:14:35 +08:00
|
|
|
for (size_t I = 0, E = PathsSorted.size(); I != E;) {
|
|
|
|
// Scan forward to find the end of the bucket.
|
|
|
|
size_t BucketStart = I;
|
|
|
|
do {
|
|
|
|
++I;
|
2016-10-11 00:26:29 +08:00
|
|
|
} while (I != E &&
|
|
|
|
PathsSorted[BucketStart].get().MangledPath ==
|
|
|
|
PathsSorted[I].get().MangledPath);
|
2014-01-03 08:14:35 +08:00
|
|
|
|
|
|
|
// If this bucket has multiple paths, extend them all.
|
|
|
|
if (I - BucketStart > 1) {
|
|
|
|
for (size_t II = BucketStart; II != I; ++II)
|
2014-01-04 07:42:00 +08:00
|
|
|
Changed |= extendPath(PathsSorted[II]);
|
|
|
|
assert(Changed && "no paths were extended to fix ambiguity");
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 07:42:00 +08:00
|
|
|
return Changed;
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:36 +08:00
|
|
|
MicrosoftVTableContext::~MicrosoftVTableContext() {}
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
namespace {
|
|
|
|
typedef llvm::SetVector<BaseSubobject, std::vector<BaseSubobject>,
|
|
|
|
llvm::DenseSet<BaseSubobject>> FullPathTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This recursive function finds all paths from a subobject centered at
|
2016-07-20 22:40:25 +08:00
|
|
|
// (RD, Offset) to the subobject located at IntroducingObject.
|
2015-05-05 02:47:54 +08:00
|
|
|
static void findPathsToSubobject(ASTContext &Context,
|
|
|
|
const ASTRecordLayout &MostDerivedLayout,
|
|
|
|
const CXXRecordDecl *RD, CharUnits Offset,
|
2016-07-20 22:40:25 +08:00
|
|
|
BaseSubobject IntroducingObject,
|
2015-05-05 02:47:54 +08:00
|
|
|
FullPathTy &FullPath,
|
|
|
|
std::list<FullPathTy> &Paths) {
|
2016-07-20 22:40:25 +08:00
|
|
|
if (BaseSubobject(RD, Offset) == IntroducingObject) {
|
2015-05-05 02:47:54 +08:00
|
|
|
Paths.push_back(FullPath);
|
|
|
|
return;
|
2014-09-23 07:14:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
for (const CXXBaseSpecifier &BS : RD->bases()) {
|
|
|
|
const CXXRecordDecl *Base = BS.getType()->getAsCXXRecordDecl();
|
|
|
|
CharUnits NewOffset = BS.isVirtual()
|
|
|
|
? MostDerivedLayout.getVBaseClassOffset(Base)
|
|
|
|
: Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
FullPath.insert(BaseSubobject(Base, NewOffset));
|
|
|
|
findPathsToSubobject(Context, MostDerivedLayout, Base, NewOffset,
|
2016-07-20 22:40:25 +08:00
|
|
|
IntroducingObject, FullPath, Paths);
|
2015-05-01 01:15:48 +08:00
|
|
|
FullPath.pop_back();
|
2015-05-05 02:47:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-23 07:14:46 +08:00
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
// Return the paths which are not subsets of other paths.
|
|
|
|
static void removeRedundantPaths(std::list<FullPathTy> &FullPaths) {
|
|
|
|
FullPaths.remove_if([&](const FullPathTy &SpecificPath) {
|
|
|
|
for (const FullPathTy &OtherPath : FullPaths) {
|
|
|
|
if (&SpecificPath == &OtherPath)
|
|
|
|
continue;
|
2018-10-21 01:53:42 +08:00
|
|
|
if (llvm::all_of(SpecificPath, [&](const BaseSubobject &BSO) {
|
|
|
|
return OtherPath.count(BSO) != 0;
|
|
|
|
})) {
|
2015-05-05 02:47:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2015-05-01 01:15:48 +08:00
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
static CharUnits getOffsetOfFullPath(ASTContext &Context,
|
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
const FullPathTy &FullPath) {
|
|
|
|
const ASTRecordLayout &MostDerivedLayout =
|
|
|
|
Context.getASTRecordLayout(RD);
|
|
|
|
CharUnits Offset = CharUnits::fromQuantity(-1);
|
|
|
|
for (const BaseSubobject &BSO : FullPath) {
|
|
|
|
const CXXRecordDecl *Base = BSO.getBase();
|
|
|
|
// The first entry in the path is always the most derived record, skip it.
|
|
|
|
if (Base == RD) {
|
|
|
|
assert(Offset.getQuantity() == -1);
|
|
|
|
Offset = CharUnits::Zero();
|
2015-05-01 01:15:48 +08:00
|
|
|
continue;
|
2015-05-05 02:47:54 +08:00
|
|
|
}
|
|
|
|
assert(Offset.getQuantity() != -1);
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
// While we know which base has to be traversed, we don't know if that base
|
|
|
|
// was a virtual base.
|
|
|
|
const CXXBaseSpecifier *BaseBS = std::find_if(
|
|
|
|
RD->bases_begin(), RD->bases_end(), [&](const CXXBaseSpecifier &BS) {
|
|
|
|
return BS.getType()->getAsCXXRecordDecl() == Base;
|
|
|
|
});
|
|
|
|
Offset = BaseBS->isVirtual() ? MostDerivedLayout.getVBaseClassOffset(Base)
|
|
|
|
: Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
RD = Base;
|
|
|
|
}
|
|
|
|
return Offset;
|
|
|
|
}
|
2015-05-01 01:15:48 +08:00
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
// We want to select the path which introduces the most covariant overrides. If
|
|
|
|
// two paths introduce overrides which the other path doesn't contain, issue a
|
|
|
|
// diagnostic.
|
|
|
|
static const FullPathTy *selectBestPath(ASTContext &Context,
|
2016-10-11 00:26:29 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
const VPtrInfo &Info,
|
2015-05-05 02:47:54 +08:00
|
|
|
std::list<FullPathTy> &FullPaths) {
|
2015-05-05 09:39:20 +08:00
|
|
|
// Handle some easy cases first.
|
|
|
|
if (FullPaths.empty())
|
|
|
|
return nullptr;
|
|
|
|
if (FullPaths.size() == 1)
|
|
|
|
return &FullPaths.front();
|
|
|
|
|
2015-05-05 02:47:54 +08:00
|
|
|
const FullPathTy *BestPath = nullptr;
|
|
|
|
typedef std::set<const CXXMethodDecl *> OverriderSetTy;
|
|
|
|
OverriderSetTy LastOverrides;
|
|
|
|
for (const FullPathTy &SpecificPath : FullPaths) {
|
2015-05-05 09:39:20 +08:00
|
|
|
assert(!SpecificPath.empty());
|
2015-05-05 02:47:54 +08:00
|
|
|
OverriderSetTy CurrentOverrides;
|
|
|
|
const CXXRecordDecl *TopLevelRD = SpecificPath.begin()->getBase();
|
|
|
|
// Find the distance from the start of the path to the subobject with the
|
|
|
|
// VPtr.
|
|
|
|
CharUnits BaseOffset =
|
|
|
|
getOffsetOfFullPath(Context, TopLevelRD, SpecificPath);
|
|
|
|
FinalOverriders Overriders(TopLevelRD, CharUnits::Zero(), TopLevelRD);
|
2016-10-11 00:26:29 +08:00
|
|
|
for (const CXXMethodDecl *MD : Info.IntroducingObject->methods()) {
|
2015-05-05 02:47:54 +08:00
|
|
|
if (!MD->isVirtual())
|
2015-04-28 01:19:49 +08:00
|
|
|
continue;
|
2015-05-05 02:47:54 +08:00
|
|
|
FinalOverriders::OverriderInfo OI =
|
|
|
|
Overriders.getOverrider(MD->getCanonicalDecl(), BaseOffset);
|
2015-05-05 09:39:20 +08:00
|
|
|
const CXXMethodDecl *OverridingMethod = OI.Method;
|
2015-05-05 02:47:54 +08:00
|
|
|
// Only overriders which have a return adjustment introduce problematic
|
|
|
|
// thunks.
|
2015-05-05 09:39:20 +08:00
|
|
|
if (ComputeReturnAdjustmentBaseOffset(Context, OverridingMethod, MD)
|
|
|
|
.isEmpty())
|
2015-05-02 05:35:41 +08:00
|
|
|
continue;
|
2015-05-05 02:47:54 +08:00
|
|
|
// It's possible that the overrider isn't in this path. If so, skip it
|
|
|
|
// because this path didn't introduce it.
|
2015-05-05 09:39:20 +08:00
|
|
|
const CXXRecordDecl *OverridingParent = OverridingMethod->getParent();
|
2018-10-21 01:53:42 +08:00
|
|
|
if (llvm::none_of(SpecificPath, [&](const BaseSubobject &BSO) {
|
|
|
|
return BSO.getBase() == OverridingParent;
|
|
|
|
}))
|
2015-05-01 01:15:48 +08:00
|
|
|
continue;
|
2015-05-05 09:39:20 +08:00
|
|
|
CurrentOverrides.insert(OverridingMethod);
|
2015-05-05 02:47:54 +08:00
|
|
|
}
|
|
|
|
OverriderSetTy NewOverrides =
|
|
|
|
llvm::set_difference(CurrentOverrides, LastOverrides);
|
|
|
|
if (NewOverrides.empty())
|
|
|
|
continue;
|
|
|
|
OverriderSetTy MissingOverrides =
|
|
|
|
llvm::set_difference(LastOverrides, CurrentOverrides);
|
|
|
|
if (MissingOverrides.empty()) {
|
|
|
|
// This path is a strict improvement over the last path, let's use it.
|
|
|
|
BestPath = &SpecificPath;
|
|
|
|
std::swap(CurrentOverrides, LastOverrides);
|
|
|
|
} else {
|
|
|
|
// This path introduces an overrider with a conflicting covariant thunk.
|
|
|
|
DiagnosticsEngine &Diags = Context.getDiagnostics();
|
|
|
|
const CXXMethodDecl *CovariantMD = *NewOverrides.begin();
|
|
|
|
const CXXMethodDecl *ConflictMD = *MissingOverrides.begin();
|
|
|
|
Diags.Report(RD->getLocation(), diag::err_vftable_ambiguous_component)
|
|
|
|
<< RD;
|
|
|
|
Diags.Report(CovariantMD->getLocation(), diag::note_covariant_thunk)
|
|
|
|
<< CovariantMD;
|
|
|
|
Diags.Report(ConflictMD->getLocation(), diag::note_covariant_thunk)
|
|
|
|
<< ConflictMD;
|
2014-09-23 07:14:46 +08:00
|
|
|
}
|
2015-05-01 01:15:48 +08:00
|
|
|
}
|
2015-05-05 09:39:20 +08:00
|
|
|
// Go with the path that introduced the most covariant overrides. If there is
|
|
|
|
// no such path, pick the first path.
|
|
|
|
return BestPath ? BestPath : &FullPaths.front();
|
2014-09-23 07:14:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void computeFullPathsForVFTables(ASTContext &Context,
|
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
VPtrInfoVector &Paths) {
|
|
|
|
const ASTRecordLayout &MostDerivedLayout = Context.getASTRecordLayout(RD);
|
2015-05-05 02:47:54 +08:00
|
|
|
FullPathTy FullPath;
|
|
|
|
std::list<FullPathTy> FullPaths;
|
2016-10-11 00:26:29 +08:00
|
|
|
for (const std::unique_ptr<VPtrInfo>& Info : Paths) {
|
2015-05-05 02:47:54 +08:00
|
|
|
findPathsToSubobject(
|
|
|
|
Context, MostDerivedLayout, RD, CharUnits::Zero(),
|
2016-07-20 22:40:25 +08:00
|
|
|
BaseSubobject(Info->IntroducingObject, Info->FullOffsetInMDC), FullPath,
|
2015-05-05 02:47:54 +08:00
|
|
|
FullPaths);
|
2014-09-23 07:14:46 +08:00
|
|
|
FullPath.clear();
|
2015-05-05 02:47:54 +08:00
|
|
|
removeRedundantPaths(FullPaths);
|
2016-07-20 22:40:25 +08:00
|
|
|
Info->PathToIntroducingObject.clear();
|
2015-05-05 02:47:54 +08:00
|
|
|
if (const FullPathTy *BestPath =
|
2016-10-11 00:26:29 +08:00
|
|
|
selectBestPath(Context, RD, *Info, FullPaths))
|
2015-05-05 02:47:54 +08:00
|
|
|
for (const BaseSubobject &BSO : *BestPath)
|
2016-07-20 22:40:25 +08:00
|
|
|
Info->PathToIntroducingObject.push_back(BSO.getBase());
|
2015-05-05 02:47:54 +08:00
|
|
|
FullPaths.clear();
|
2014-09-23 07:14:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 04:00:39 +08:00
|
|
|
static bool vfptrIsEarlierInMDC(const ASTRecordLayout &Layout,
|
|
|
|
const MethodVFTableLocation &LHS,
|
|
|
|
const MethodVFTableLocation &RHS) {
|
2018-03-29 02:23:35 +08:00
|
|
|
CharUnits L = LHS.VFPtrOffset;
|
|
|
|
CharUnits R = RHS.VFPtrOffset;
|
|
|
|
if (LHS.VBase)
|
|
|
|
L += Layout.getVBaseClassOffset(LHS.VBase);
|
|
|
|
if (RHS.VBase)
|
|
|
|
R += Layout.getVBaseClassOffset(RHS.VBase);
|
|
|
|
return L < R;
|
|
|
|
}
|
|
|
|
|
2013-11-05 23:54:58 +08:00
|
|
|
void MicrosoftVTableContext::computeVTableRelatedInformation(
|
2013-07-30 17:46:19 +08:00
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
assert(RD->isDynamicClass());
|
|
|
|
|
|
|
|
// Check if we've computed this information before.
|
|
|
|
if (VFPtrLocations.count(RD))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap;
|
|
|
|
|
2016-10-11 00:26:29 +08:00
|
|
|
{
|
2019-08-15 07:04:18 +08:00
|
|
|
auto VFPtrs = std::make_unique<VPtrInfoVector>();
|
2018-04-03 04:00:39 +08:00
|
|
|
computeVTablePaths(/*ForVBTables=*/false, RD, *VFPtrs);
|
|
|
|
computeFullPathsForVFTables(Context, RD, *VFPtrs);
|
2016-10-11 00:26:29 +08:00
|
|
|
VFPtrLocations[RD] = std::move(VFPtrs);
|
|
|
|
}
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
MethodVFTableLocationsTy NewMethodLocations;
|
2018-04-03 04:00:39 +08:00
|
|
|
for (const std::unique_ptr<VPtrInfo> &VFPtr : *VFPtrLocations[RD]) {
|
2016-10-11 00:26:29 +08:00
|
|
|
VFTableBuilder Builder(*this, RD, *VFPtr);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
VFTableIdTy id(RD, VFPtr->FullOffsetInMDC);
|
2013-07-30 17:46:19 +08:00
|
|
|
assert(VFTableLayouts.count(id) == 0);
|
|
|
|
SmallVector<VTableLayout::VTableThunkTy, 1> VTableThunks(
|
|
|
|
Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
|
2019-08-15 07:04:18 +08:00
|
|
|
VFTableLayouts[id] = std::make_unique<VTableLayout>(
|
2016-12-14 04:40:39 +08:00
|
|
|
ArrayRef<size_t>{0}, Builder.vtable_components(), VTableThunks,
|
|
|
|
EmptyAddressPointsMap);
|
2013-07-30 17:46:19 +08:00
|
|
|
Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
|
2014-03-21 04:38:34 +08:00
|
|
|
|
2018-03-29 02:23:35 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &Loc : Builder.vtable_locations()) {
|
2018-03-29 02:23:35 +08:00
|
|
|
auto Insert = NewMethodLocations.insert(Loc);
|
|
|
|
if (!Insert.second) {
|
|
|
|
const MethodVFTableLocation &NewLoc = Loc.second;
|
|
|
|
MethodVFTableLocation &OldLoc = Insert.first->second;
|
|
|
|
if (vfptrIsEarlierInMDC(Layout, NewLoc, OldLoc))
|
|
|
|
OldLoc = NewLoc;
|
|
|
|
}
|
2014-03-21 04:38:34 +08:00
|
|
|
}
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MethodVFTableLocations.insert(NewMethodLocations.begin(),
|
|
|
|
NewMethodLocations.end());
|
|
|
|
if (Context.getLangOpts().DumpVTableLayouts)
|
2013-11-09 05:28:00 +08:00
|
|
|
dumpMethodLocations(RD, NewMethodLocations, llvm::outs());
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 23:54:58 +08:00
|
|
|
void MicrosoftVTableContext::dumpMethodLocations(
|
2013-07-30 17:46:19 +08:00
|
|
|
const CXXRecordDecl *RD, const MethodVFTableLocationsTy &NewMethods,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
// Compute the vtable indices for all the member functions.
|
|
|
|
// Store them in a map keyed by the location so we'll get a sorted table.
|
|
|
|
std::map<MethodVFTableLocation, std::string> IndicesMap;
|
|
|
|
bool HasNonzeroOffset = false;
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : NewMethods) {
|
|
|
|
const CXXMethodDecl *MD = cast<const CXXMethodDecl>(I.first.getDecl());
|
2013-07-30 17:46:19 +08:00
|
|
|
assert(MD->isVirtual());
|
|
|
|
|
|
|
|
std::string MethodName = PredefinedExpr::ComputeName(
|
|
|
|
PredefinedExpr::PrettyFunctionNoVirtual, MD);
|
|
|
|
|
|
|
|
if (isa<CXXDestructorDecl>(MD)) {
|
2015-07-26 01:10:49 +08:00
|
|
|
IndicesMap[I.second] = MethodName + " [scalar deleting]";
|
2013-07-30 17:46:19 +08:00
|
|
|
} else {
|
2015-07-26 01:10:49 +08:00
|
|
|
IndicesMap[I.second] = MethodName;
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
if (!I.second.VFPtrOffset.isZero() || I.second.VBTableIndex != 0)
|
2013-07-30 17:46:19 +08:00
|
|
|
HasNonzeroOffset = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the vtable indices for all the member functions.
|
|
|
|
if (!IndicesMap.empty()) {
|
|
|
|
Out << "VFTable indices for ";
|
2014-01-04 02:42:48 +08:00
|
|
|
Out << "'";
|
|
|
|
RD->printQualifiedName(Out);
|
2014-03-05 21:54:07 +08:00
|
|
|
Out << "' (" << IndicesMap.size()
|
|
|
|
<< (IndicesMap.size() == 1 ? " entry" : " entries") << ").\n";
|
2013-07-30 17:46:19 +08:00
|
|
|
|
|
|
|
CharUnits LastVFPtrOffset = CharUnits::fromQuantity(-1);
|
|
|
|
uint64_t LastVBIndex = 0;
|
2015-07-26 01:10:49 +08:00
|
|
|
for (const auto &I : IndicesMap) {
|
|
|
|
CharUnits VFPtrOffset = I.first.VFPtrOffset;
|
|
|
|
uint64_t VBIndex = I.first.VBTableIndex;
|
2013-07-30 17:46:19 +08:00
|
|
|
if (HasNonzeroOffset &&
|
|
|
|
(VFPtrOffset != LastVFPtrOffset || VBIndex != LastVBIndex)) {
|
|
|
|
assert(VBIndex > LastVBIndex || VFPtrOffset > LastVFPtrOffset);
|
|
|
|
Out << " -- accessible via ";
|
|
|
|
if (VBIndex)
|
|
|
|
Out << "vbtable index " << VBIndex << ", ";
|
|
|
|
Out << "vfptr at offset " << VFPtrOffset.getQuantity() << " --\n";
|
|
|
|
LastVFPtrOffset = VFPtrOffset;
|
|
|
|
LastVBIndex = VBIndex;
|
|
|
|
}
|
|
|
|
|
2015-07-26 01:10:49 +08:00
|
|
|
uint64_t VTableIndex = I.first.Index;
|
|
|
|
const std::string &MethodName = I.second;
|
2013-07-30 17:46:19 +08:00
|
|
|
Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName << '\n';
|
|
|
|
}
|
|
|
|
Out << '\n';
|
|
|
|
}
|
2014-03-20 21:42:14 +08:00
|
|
|
|
|
|
|
Out.flush();
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:36 +08:00
|
|
|
const VirtualBaseInfo &MicrosoftVTableContext::computeVBTableRelatedInformation(
|
2013-11-05 23:54:58 +08:00
|
|
|
const CXXRecordDecl *RD) {
|
2014-01-04 07:42:00 +08:00
|
|
|
VirtualBaseInfo *VBI;
|
2013-11-05 23:54:58 +08:00
|
|
|
|
2014-01-04 07:42:00 +08:00
|
|
|
{
|
|
|
|
// Get or create a VBI for RD. Don't hold a reference to the DenseMap cell,
|
|
|
|
// as it may be modified and rehashed under us.
|
2016-10-11 00:26:36 +08:00
|
|
|
std::unique_ptr<VirtualBaseInfo> &Entry = VBaseInfo[RD];
|
2014-01-04 07:42:00 +08:00
|
|
|
if (Entry)
|
2016-10-11 00:26:36 +08:00
|
|
|
return *Entry;
|
2019-08-15 07:04:18 +08:00
|
|
|
Entry = std::make_unique<VirtualBaseInfo>();
|
2016-10-11 00:26:36 +08:00
|
|
|
VBI = Entry.get();
|
2014-01-04 07:42:00 +08:00
|
|
|
}
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
computeVTablePaths(/*ForVBTables=*/true, RD, VBI->VBPtrPaths);
|
2013-11-05 23:54:58 +08:00
|
|
|
|
2013-11-08 19:45:35 +08:00
|
|
|
// First, see if the Derived class shared the vbptr with a non-virtual base.
|
2014-01-03 08:14:35 +08:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2013-11-08 19:45:35 +08:00
|
|
|
if (const CXXRecordDecl *VBPtrBase = Layout.getBaseSharingVBPtr()) {
|
2014-01-03 08:14:35 +08:00
|
|
|
// If the Derived class shares the vbptr with a non-virtual base, the shared
|
|
|
|
// virtual bases come first so that the layout is the same.
|
2016-10-11 00:26:36 +08:00
|
|
|
const VirtualBaseInfo &BaseInfo =
|
2014-01-03 08:14:35 +08:00
|
|
|
computeVBTableRelatedInformation(VBPtrBase);
|
2016-10-11 00:26:36 +08:00
|
|
|
VBI->VBTableIndices.insert(BaseInfo.VBTableIndices.begin(),
|
|
|
|
BaseInfo.VBTableIndices.end());
|
2013-11-05 23:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New vbases are added to the end of the vbtable.
|
|
|
|
// Skip the self entry and vbases visited in the non-virtual base, if any.
|
2014-01-04 07:42:00 +08:00
|
|
|
unsigned VBTableIndex = 1 + VBI->VBTableIndices.size();
|
2014-03-26 16:22:48 +08:00
|
|
|
for (const auto &VB : RD->vbases()) {
|
|
|
|
const CXXRecordDecl *CurVBase = VB.getType()->getAsCXXRecordDecl();
|
2014-01-04 07:42:00 +08:00
|
|
|
if (!VBI->VBTableIndices.count(CurVBase))
|
|
|
|
VBI->VBTableIndices[CurVBase] = VBTableIndex++;
|
2013-11-05 23:54:58 +08:00
|
|
|
}
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2016-10-11 00:26:36 +08:00
|
|
|
return *VBI;
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MicrosoftVTableContext::getVBTableIndex(const CXXRecordDecl *Derived,
|
|
|
|
const CXXRecordDecl *VBase) {
|
2016-10-11 00:26:36 +08:00
|
|
|
const VirtualBaseInfo &VBInfo = computeVBTableRelatedInformation(Derived);
|
|
|
|
assert(VBInfo.VBTableIndices.count(VBase));
|
|
|
|
return VBInfo.VBTableIndices.find(VBase)->second;
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfoVector &
|
2014-01-03 08:14:35 +08:00
|
|
|
MicrosoftVTableContext::enumerateVBTables(const CXXRecordDecl *RD) {
|
2016-10-11 00:26:36 +08:00
|
|
|
return computeVBTableRelatedInformation(RD).VBPtrPaths;
|
2013-11-05 23:54:58 +08:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfoVector &
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) {
|
2013-07-30 17:46:19 +08:00
|
|
|
computeVTableRelatedInformation(RD);
|
|
|
|
|
|
|
|
assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations");
|
2018-04-03 04:00:39 +08:00
|
|
|
return *VFPtrLocations[RD];
|
2013-07-30 17:46:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const VTableLayout &
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext::getVFTableLayout(const CXXRecordDecl *RD,
|
|
|
|
CharUnits VFPtrOffset) {
|
2013-07-30 17:46:19 +08:00
|
|
|
computeVTableRelatedInformation(RD);
|
|
|
|
|
|
|
|
VFTableIdTy id(RD, VFPtrOffset);
|
|
|
|
assert(VFTableLayouts.count(id) && "Couldn't find a VFTable at this offset");
|
|
|
|
return *VFTableLayouts[id];
|
|
|
|
}
|
|
|
|
|
2018-04-03 04:00:39 +08:00
|
|
|
MethodVFTableLocation
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext::getMethodVFTableLocation(GlobalDecl GD) {
|
2013-07-30 17:46:19 +08:00
|
|
|
assert(cast<CXXMethodDecl>(GD.getDecl())->isVirtual() &&
|
|
|
|
"Only use this method for virtual methods or dtors");
|
|
|
|
if (isa<CXXDestructorDecl>(GD.getDecl()))
|
|
|
|
assert(GD.getDtorType() == Dtor_Deleting);
|
|
|
|
|
2018-05-18 02:12:18 +08:00
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
|
2013-07-30 17:46:19 +08:00
|
|
|
MethodVFTableLocationsTy::iterator I = MethodVFTableLocations.find(GD);
|
|
|
|
if (I != MethodVFTableLocations.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
|
|
|
|
|
|
|
|
computeVTableRelatedInformation(RD);
|
|
|
|
|
|
|
|
I = MethodVFTableLocations.find(GD);
|
|
|
|
assert(I != MethodVFTableLocations.end() && "Did not find index!");
|
|
|
|
return I->second;
|
|
|
|
}
|