2009-10-12 06:13:54 +08:00
|
|
|
//===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation of virtual tables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "CodeGenFunction.h"
|
2009-11-28 04:47:55 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-10-12 06:13:54 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-11-27 03:32:45 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2009-11-13 13:46:16 +08:00
|
|
|
#include <cstdio>
|
2009-10-12 06:13:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-11-28 06:21:51 +08:00
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class VtableBuilder {
|
2009-10-12 06:13:54 +08:00
|
|
|
public:
|
|
|
|
/// Index_t - Vtable index type.
|
|
|
|
typedef uint64_t Index_t;
|
|
|
|
private:
|
|
|
|
std::vector<llvm::Constant *> &methods;
|
|
|
|
std::vector<llvm::Constant *> submethods;
|
|
|
|
llvm::Type *Ptr8Ty;
|
|
|
|
/// Class - The most derived class that this vtable is being built for.
|
|
|
|
const CXXRecordDecl *Class;
|
2009-11-13 09:54:23 +08:00
|
|
|
/// LayoutClass - The most derived class used for virtual base layout
|
|
|
|
/// information.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
2009-11-13 10:13:54 +08:00
|
|
|
/// LayoutOffset - The offset for Class in LayoutClass.
|
|
|
|
uint64_t LayoutOffset;
|
2009-10-12 06:13:54 +08:00
|
|
|
/// BLayout - Layout for the most derived class that this vtable is being
|
|
|
|
/// built for.
|
|
|
|
const ASTRecordLayout &BLayout;
|
|
|
|
llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
|
|
|
|
llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
|
|
|
|
llvm::Constant *rtti;
|
|
|
|
llvm::LLVMContext &VMContext;
|
|
|
|
CodeGenModule &CGM; // Per-module state.
|
|
|
|
/// Index - Maps a method decl into a vtable index. Useful for virtual
|
|
|
|
/// dispatch codegen.
|
2009-11-14 01:08:56 +08:00
|
|
|
llvm::DenseMap<GlobalDecl, Index_t> Index;
|
|
|
|
llvm::DenseMap<GlobalDecl, Index_t> VCall;
|
|
|
|
llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
|
2009-11-07 07:27:42 +08:00
|
|
|
// This is the offset to the nearest virtual base
|
2009-11-14 01:08:56 +08:00
|
|
|
llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
|
2009-10-12 06:13:54 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
|
2009-10-28 07:46:47 +08:00
|
|
|
|
2009-11-27 03:54:33 +08:00
|
|
|
/// PureVirtualFunction - Points to __cxa_pure_virtual.
|
|
|
|
llvm::Constant *PureVirtualFn;
|
|
|
|
|
2009-11-26 11:25:13 +08:00
|
|
|
/// Thunk - Represents a single thunk.
|
|
|
|
struct Thunk {
|
|
|
|
Thunk()
|
|
|
|
: Index(0) { }
|
|
|
|
|
|
|
|
Thunk(uint64_t Index, const ThunkAdjustment &Adjustment)
|
|
|
|
: Index(Index), Adjustment(Adjustment) { }
|
|
|
|
|
|
|
|
/// Index - The index in the vtable.
|
|
|
|
uint64_t Index;
|
|
|
|
|
2009-11-27 03:32:45 +08:00
|
|
|
/// Adjustment - The thunk adjustment.
|
2009-11-26 11:25:13 +08:00
|
|
|
ThunkAdjustment Adjustment;
|
|
|
|
};
|
2009-11-27 03:32:45 +08:00
|
|
|
|
|
|
|
/// Thunks - The thunks in a vtable.
|
2009-11-26 11:25:13 +08:00
|
|
|
typedef llvm::DenseMap<GlobalDecl, Thunk> ThunksMapTy;
|
|
|
|
ThunksMapTy Thunks;
|
2009-11-27 03:32:45 +08:00
|
|
|
|
|
|
|
/// CovariantThunk - Represents a single covariant thunk.
|
|
|
|
struct CovariantThunk {
|
|
|
|
CovariantThunk()
|
|
|
|
: Index(0) { }
|
|
|
|
|
|
|
|
CovariantThunk(uint64_t Index, const ThunkAdjustment &ThisAdjustment,
|
|
|
|
const ThunkAdjustment &ReturnAdjustment,
|
|
|
|
CanQualType ReturnType)
|
|
|
|
: Index(Index), Adjustment(ThisAdjustment, ReturnAdjustment),
|
|
|
|
ReturnType(ReturnType) { }
|
|
|
|
|
|
|
|
// Index - The index in the vtable.
|
|
|
|
uint64_t Index;
|
|
|
|
|
|
|
|
/// Adjustment - The covariant thunk adjustment.
|
|
|
|
CovariantThunkAdjustment Adjustment;
|
|
|
|
|
|
|
|
/// ReturnType - The return type of the function.
|
|
|
|
CanQualType ReturnType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// CovariantThunks - The covariant thunks in a vtable.
|
|
|
|
typedef llvm::DenseMap<GlobalDecl, CovariantThunk> CovariantThunksMapTy;
|
|
|
|
CovariantThunksMapTy CovariantThunks;
|
2009-11-26 11:25:13 +08:00
|
|
|
|
2009-11-27 03:32:45 +08:00
|
|
|
/// PureVirtualMethods - Pure virtual methods.
|
|
|
|
typedef llvm::DenseSet<GlobalDecl> PureVirtualMethodsSetTy;
|
|
|
|
PureVirtualMethodsSetTy PureVirtualMethods;
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
std::vector<Index_t> VCalls;
|
2009-11-13 04:47:57 +08:00
|
|
|
|
|
|
|
typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
|
2009-11-20 04:52:19 +08:00
|
|
|
// subAddressPoints - Used to hold the AddressPoints (offsets) into the built
|
|
|
|
// vtable for use in computing the initializers for the VTT.
|
|
|
|
llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
|
2009-11-13 04:47:57 +08:00
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
typedef CXXRecordDecl::method_iterator method_iter;
|
|
|
|
const bool Extern;
|
|
|
|
const uint32_t LLVMPointerWidth;
|
|
|
|
Index_t extra;
|
2009-10-15 10:04:03 +08:00
|
|
|
typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
|
2009-11-20 04:52:19 +08:00
|
|
|
static llvm::DenseMap<CtorVtable_t, int64_t>&
|
|
|
|
AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
|
|
|
|
const CXXRecordDecl *c) {
|
|
|
|
CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l];
|
|
|
|
if (oref == 0)
|
|
|
|
oref = new CodeGenModule::AddrMap_t;
|
|
|
|
|
|
|
|
llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
|
|
|
|
if (ref == 0)
|
|
|
|
ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
|
|
|
|
return *ref;
|
|
|
|
}
|
2009-11-27 03:54:33 +08:00
|
|
|
|
|
|
|
/// getPureVirtualFn - Return the __cxa_pure_virtual function.
|
|
|
|
llvm::Constant* getPureVirtualFn() {
|
|
|
|
if (!PureVirtualFn) {
|
|
|
|
const llvm::FunctionType *Ty =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return PureVirtualFn;
|
|
|
|
}
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
public:
|
2009-11-13 10:13:54 +08:00
|
|
|
VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c,
|
|
|
|
const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
|
|
|
|
: methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo),
|
2009-11-13 09:54:23 +08:00
|
|
|
BLayout(cgm.getContext().getASTRecordLayout(l)),
|
2009-12-03 02:57:08 +08:00
|
|
|
rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()),
|
2009-11-27 03:54:33 +08:00
|
|
|
CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)),
|
2009-11-19 08:49:05 +08:00
|
|
|
Extern(!l->isInAnonymousNamespace()),
|
2009-11-27 03:54:33 +08:00
|
|
|
LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
|
2009-10-12 06:13:54 +08:00
|
|
|
Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
|
|
|
|
}
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; }
|
2009-10-12 06:13:54 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
|
|
|
|
{ return VBIndex; }
|
|
|
|
|
|
|
|
llvm::Constant *wrap(Index_t i) {
|
|
|
|
llvm::Constant *m;
|
|
|
|
m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
|
|
|
|
return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *wrap(llvm::Constant *m) {
|
|
|
|
return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
|
|
|
|
}
|
|
|
|
|
2009-12-03 03:50:41 +08:00
|
|
|
#define D1(x)
|
|
|
|
//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
|
2009-11-01 04:06:59 +08:00
|
|
|
|
|
|
|
void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
|
2009-10-14 06:54:56 +08:00
|
|
|
bool updateVBIndex, Index_t current_vbindex) {
|
2009-10-12 06:13:54 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
2009-10-14 06:54:56 +08:00
|
|
|
Index_t next_vbindex = current_vbindex;
|
2009-10-12 06:13:54 +08:00
|
|
|
if (i->isVirtual() && !SeenVBase.count(Base)) {
|
|
|
|
SeenVBase.insert(Base);
|
2009-10-14 06:54:56 +08:00
|
|
|
if (updateVBIndex) {
|
2009-11-01 04:06:59 +08:00
|
|
|
next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
|
2009-10-14 06:54:56 +08:00
|
|
|
- 3*LLVMPointerWidth/8);
|
|
|
|
VBIndex[Base] = next_vbindex;
|
|
|
|
}
|
2009-11-01 04:06:59 +08:00
|
|
|
int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
|
|
|
|
VCalls.push_back((0?700:0) + BaseOffset);
|
|
|
|
D1(printf(" vbase for %s at %d delta %d most derived %s\n",
|
|
|
|
Base->getNameAsCString(),
|
|
|
|
(int)-VCalls.size()-3, (int)BaseOffset,
|
|
|
|
Class->getNameAsCString()));
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
2009-10-14 06:54:56 +08:00
|
|
|
// We also record offsets for non-virtual bases to closest enclosing
|
|
|
|
// virtual base. We do this so that we don't have to search
|
|
|
|
// for the nearst virtual base class when generating thunks.
|
|
|
|
if (updateVBIndex && VBIndex.count(Base) == 0)
|
|
|
|
VBIndex[Base] = next_vbindex;
|
2009-11-01 04:06:59 +08:00
|
|
|
GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartNewTable() {
|
|
|
|
SeenVBase.clear();
|
|
|
|
}
|
|
|
|
|
2009-10-15 17:30:16 +08:00
|
|
|
Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
|
|
|
|
Index_t Offset = 0) {
|
|
|
|
|
|
|
|
if (B == D)
|
|
|
|
return Offset;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
|
|
|
|
e = D->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
int64_t BaseOffset = 0;
|
|
|
|
if (!i->isVirtual())
|
|
|
|
BaseOffset = Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
int64_t o = getNVOffset_1(Base, B, BaseOffset);
|
|
|
|
if (o >= 0)
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getNVOffset - Returns the non-virtual offset for the given (B) base of the
|
|
|
|
/// derived class D.
|
|
|
|
Index_t getNVOffset(QualType qB, QualType qD) {
|
2009-11-04 03:03:17 +08:00
|
|
|
qD = qD->getPointeeType();
|
|
|
|
qB = qB->getPointeeType();
|
2009-10-15 17:30:16 +08:00
|
|
|
CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
|
|
|
|
CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
|
|
|
|
int64_t o = getNVOffset_1(D, B);
|
|
|
|
if (o >= 0)
|
|
|
|
return o;
|
|
|
|
|
|
|
|
assert(false && "FIXME: non-virtual base not found");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
/// getVbaseOffset - Returns the index into the vtable for the virtual base
|
|
|
|
/// offset for the given (B) virtual base of the derived class D.
|
|
|
|
Index_t getVbaseOffset(QualType qB, QualType qD) {
|
2009-11-04 03:03:17 +08:00
|
|
|
qD = qD->getPointeeType();
|
|
|
|
qB = qB->getPointeeType();
|
2009-10-12 06:13:54 +08:00
|
|
|
CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
|
|
|
|
CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
|
|
|
|
if (D != Class)
|
2009-11-30 09:19:33 +08:00
|
|
|
return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
|
2009-10-12 06:13:54 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
|
|
|
|
i = VBIndex.find(B);
|
|
|
|
if (i != VBIndex.end())
|
|
|
|
return i->second;
|
|
|
|
|
2009-10-14 06:54:56 +08:00
|
|
|
assert(false && "FIXME: Base not found");
|
2009-10-12 06:13:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
bool OverrideMethod(GlobalDecl GD, llvm::Constant *m,
|
2009-10-15 17:30:16 +08:00
|
|
|
bool MorallyVirtual, Index_t OverrideOffset,
|
2009-11-07 07:27:42 +08:00
|
|
|
Index_t Offset, int64_t CurrentVBaseOffset) {
|
2009-11-14 01:08:56 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
2009-10-28 08:35:46 +08:00
|
|
|
const bool isPure = MD->isPure();
|
2009-10-12 06:13:54 +08:00
|
|
|
typedef CXXMethodDecl::method_iterator meth_iter;
|
2009-10-15 17:30:16 +08:00
|
|
|
// FIXME: Should OverrideOffset's be Offset?
|
2009-10-12 06:13:54 +08:00
|
|
|
|
|
|
|
// FIXME: Don't like the nested loops. For very large inheritance
|
|
|
|
// heirarchies we could have a table on the side with the final overridder
|
|
|
|
// and just replace each instance of an overridden method once. Would be
|
|
|
|
// nice to measure the cost/benefit on real code.
|
|
|
|
|
|
|
|
for (meth_iter mi = MD->begin_overridden_methods(),
|
|
|
|
e = MD->end_overridden_methods();
|
|
|
|
mi != e; ++mi) {
|
2009-11-14 01:08:56 +08:00
|
|
|
GlobalDecl OGD;
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
const CXXMethodDecl *OMD = *mi;
|
2009-11-14 01:08:56 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
|
|
|
|
OGD = GlobalDecl(DD, GD.getDtorType());
|
|
|
|
else
|
|
|
|
OGD = OMD;
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
llvm::Constant *om;
|
2009-11-14 01:08:56 +08:00
|
|
|
om = WrapAddrOf(OGD);
|
2009-10-12 06:13:54 +08:00
|
|
|
om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
|
|
|
|
|
|
|
|
for (Index_t i = 0, e = submethods.size();
|
|
|
|
i != e; ++i) {
|
|
|
|
// FIXME: begin_overridden_methods might be too lax, covariance */
|
|
|
|
if (submethods[i] != om)
|
|
|
|
continue;
|
|
|
|
QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
|
|
|
|
CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
|
|
|
|
QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
|
|
|
|
CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
|
2009-11-27 03:32:45 +08:00
|
|
|
ThunkAdjustment ReturnAdjustment;
|
2009-10-12 06:13:54 +08:00
|
|
|
if (oret != ret) {
|
|
|
|
// FIXME: calculate offsets for covariance
|
2009-11-27 03:32:45 +08:00
|
|
|
CovariantThunksMapTy::iterator i = CovariantThunks.find(OMD);
|
|
|
|
if (i != CovariantThunks.end()) {
|
|
|
|
oret = i->second.ReturnType;
|
|
|
|
CovariantThunks.erase(i);
|
2009-10-13 18:55:21 +08:00
|
|
|
}
|
2009-10-15 17:30:16 +08:00
|
|
|
// FIXME: Double check oret
|
|
|
|
Index_t nv = getNVOffset(oret, ret)/8;
|
2009-11-27 03:32:45 +08:00
|
|
|
ReturnAdjustment = ThunkAdjustment(nv, getVbaseOffset(oret, ret));
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
2009-11-14 01:08:56 +08:00
|
|
|
Index[GD] = i;
|
2009-10-12 06:13:54 +08:00
|
|
|
submethods[i] = m;
|
2009-10-28 08:35:46 +08:00
|
|
|
if (isPure)
|
2009-11-27 03:32:45 +08:00
|
|
|
PureVirtualMethods.insert(GD);
|
|
|
|
PureVirtualMethods.erase(OGD);
|
2009-11-14 01:08:56 +08:00
|
|
|
Thunks.erase(OGD);
|
|
|
|
if (MorallyVirtual || VCall.count(OGD)) {
|
|
|
|
Index_t &idx = VCall[OGD];
|
2009-10-12 06:13:54 +08:00
|
|
|
if (idx == 0) {
|
2009-11-14 01:08:56 +08:00
|
|
|
NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
|
|
|
|
VCallOffset[GD] = OverrideOffset/8;
|
2009-10-12 06:13:54 +08:00
|
|
|
idx = VCalls.size()+1;
|
|
|
|
VCalls.push_back(0);
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
|
2009-11-14 07:45:53 +08:00
|
|
|
MD->getNameAsString().c_str(), (int)-idx-3,
|
|
|
|
(int)VCalls[idx-1], Class->getNameAsCString()));
|
2009-10-12 06:13:54 +08:00
|
|
|
} else {
|
2009-11-14 01:08:56 +08:00
|
|
|
NonVirtualOffset[GD] = NonVirtualOffset[OGD];
|
|
|
|
VCallOffset[GD] = VCallOffset[OGD];
|
|
|
|
VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
|
2009-11-14 07:45:53 +08:00
|
|
|
MD->getNameAsString().c_str(), (int)-idx-3,
|
|
|
|
(int)VCalls[idx-1], Class->getNameAsCString()));
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
2009-11-14 01:08:56 +08:00
|
|
|
VCall[GD] = idx;
|
2009-11-26 11:25:13 +08:00
|
|
|
int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
|
|
|
|
int64_t VirtualAdjustment =
|
|
|
|
-((idx + extra + 2) * LLVMPointerWidth / 8);
|
|
|
|
|
2009-11-07 07:27:42 +08:00
|
|
|
// Optimize out virtual adjustments of 0.
|
|
|
|
if (VCalls[idx-1] == 0)
|
2009-11-26 11:25:13 +08:00
|
|
|
VirtualAdjustment = 0;
|
|
|
|
|
|
|
|
ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
|
|
|
|
VirtualAdjustment);
|
|
|
|
|
2009-10-15 10:04:03 +08:00
|
|
|
// FIXME: Do we always have to build a covariant thunk to save oret,
|
|
|
|
// which is the containing virtual base class?
|
2009-11-27 03:32:45 +08:00
|
|
|
if (!ReturnAdjustment.isEmpty()) {
|
|
|
|
CovariantThunks[GD] =
|
|
|
|
CovariantThunk(i, ThisAdjustment, ReturnAdjustment, oret);
|
|
|
|
} else if (!isPure && !ThisAdjustment.isEmpty())
|
2009-11-26 11:25:13 +08:00
|
|
|
Thunks[GD] = Thunk(i, ThisAdjustment);
|
2009-10-12 06:13:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-10-14 06:54:56 +08:00
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
// FIXME: finish off
|
2009-11-26 11:25:13 +08:00
|
|
|
int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
|
2009-11-07 07:27:42 +08:00
|
|
|
|
2009-11-27 03:32:45 +08:00
|
|
|
if (NonVirtualAdjustment || !ReturnAdjustment.isEmpty()) {
|
2009-11-26 11:25:13 +08:00
|
|
|
ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
|
2009-10-14 06:54:56 +08:00
|
|
|
|
2009-11-27 03:32:45 +08:00
|
|
|
if (!ReturnAdjustment.isEmpty()) {
|
|
|
|
CovariantThunks[GD] =
|
|
|
|
CovariantThunk(i, ThisAdjustment, ReturnAdjustment, oret);
|
|
|
|
} else if (!isPure)
|
2009-11-26 11:25:13 +08:00
|
|
|
Thunks[GD] = Thunk(i, ThisAdjustment);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallThunks() {
|
2009-11-26 11:25:13 +08:00
|
|
|
for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end();
|
2009-10-12 06:13:54 +08:00
|
|
|
i != e; ++i) {
|
2009-11-14 01:08:56 +08:00
|
|
|
GlobalDecl GD = i->first;
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2009-11-26 11:25:13 +08:00
|
|
|
assert(!MD->isPure() && "Can't thunk pure virtual methods!");
|
|
|
|
|
|
|
|
const Thunk& Thunk = i->second;
|
|
|
|
assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
|
|
|
|
|
|
|
|
submethods[Thunk.Index] = CGM.BuildThunk(MD, Extern, Thunk.Adjustment);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
Thunks.clear();
|
2009-11-26 11:25:13 +08:00
|
|
|
|
2009-11-27 03:32:45 +08:00
|
|
|
for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(),
|
|
|
|
e = CovariantThunks.end(); i != e; ++i) {
|
2009-11-14 01:08:56 +08:00
|
|
|
GlobalDecl GD = i->first;
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2009-10-28 08:35:46 +08:00
|
|
|
if (MD->isPure())
|
|
|
|
continue;
|
2009-11-26 11:09:37 +08:00
|
|
|
|
2009-11-27 03:32:45 +08:00
|
|
|
const CovariantThunk &Thunk = i->second;
|
|
|
|
assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
|
|
|
|
submethods[Thunk.Index] =
|
|
|
|
CGM.BuildCovariantThunk(MD, Extern, Thunk.Adjustment);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
CovariantThunks.clear();
|
2009-11-27 03:32:45 +08:00
|
|
|
|
|
|
|
for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(),
|
|
|
|
e = PureVirtualMethods.end(); i != e; ++i) {
|
|
|
|
GlobalDecl GD = *i;
|
2009-11-27 03:54:33 +08:00
|
|
|
submethods[Index[GD]] = getPureVirtualFn();
|
2009-10-28 07:46:47 +08:00
|
|
|
}
|
2009-11-27 03:32:45 +08:00
|
|
|
PureVirtualMethods.clear();
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
llvm::Constant *WrapAddrOf(GlobalDecl GD) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
2009-10-28 07:36:26 +08:00
|
|
|
if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
|
2009-11-14 01:08:56 +08:00
|
|
|
return wrap(CGM.GetAddrOfCXXDestructor(Dtor, GD.getDtorType()));
|
2009-10-28 07:36:26 +08:00
|
|
|
|
2009-11-24 13:08:52 +08:00
|
|
|
const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
|
2009-10-28 07:36:26 +08:00
|
|
|
|
|
|
|
return wrap(CGM.GetAddrOfFunction(MD, Ty));
|
|
|
|
}
|
|
|
|
|
2009-11-07 07:27:42 +08:00
|
|
|
void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
|
|
|
|
int64_t CurrentVBaseOffset) {
|
2009-10-15 10:04:03 +08:00
|
|
|
for (Path_t::reverse_iterator i = Path->rbegin(),
|
2009-10-12 06:13:54 +08:00
|
|
|
e = Path->rend(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *RD = i->first;
|
2009-10-15 17:30:16 +08:00
|
|
|
int64_t OverrideOffset = i->second;
|
2009-10-12 06:13:54 +08:00
|
|
|
for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
|
|
|
|
++mi) {
|
2009-11-14 01:08:56 +08:00
|
|
|
const CXXMethodDecl *MD = *mi;
|
|
|
|
|
|
|
|
if (!MD->isVirtual())
|
2009-10-12 06:13:54 +08:00
|
|
|
continue;
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
// Override both the complete and the deleting destructor.
|
|
|
|
GlobalDecl CompDtor(DD, Dtor_Complete);
|
|
|
|
OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual,
|
|
|
|
OverrideOffset, Offset, CurrentVBaseOffset);
|
|
|
|
|
|
|
|
GlobalDecl DeletingDtor(DD, Dtor_Deleting);
|
|
|
|
OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual,
|
|
|
|
OverrideOffset, Offset, CurrentVBaseOffset);
|
|
|
|
} else {
|
|
|
|
OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset,
|
|
|
|
Offset, CurrentVBaseOffset);
|
|
|
|
}
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
|
2009-11-30 09:19:33 +08:00
|
|
|
int64_t CurrentVBaseOffset) {
|
2009-11-14 01:08:56 +08:00
|
|
|
llvm::Constant *m = WrapAddrOf(GD);
|
2009-10-28 07:36:26 +08:00
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
// If we can find a previously allocated slot for this, reuse it.
|
2009-11-14 01:08:56 +08:00
|
|
|
if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset,
|
2009-11-07 07:27:42 +08:00
|
|
|
CurrentVBaseOffset))
|
2009-10-12 06:13:54 +08:00
|
|
|
return;
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
// else allocate a new slot.
|
2009-11-14 01:08:56 +08:00
|
|
|
Index[GD] = submethods.size();
|
2009-10-12 06:13:54 +08:00
|
|
|
submethods.push_back(m);
|
2009-11-14 07:45:53 +08:00
|
|
|
D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
|
|
|
|
(int)Index[GD]));
|
2009-10-28 08:35:46 +08:00
|
|
|
if (MD->isPure())
|
2009-11-27 03:32:45 +08:00
|
|
|
PureVirtualMethods.insert(GD);
|
2009-10-12 06:13:54 +08:00
|
|
|
if (MorallyVirtual) {
|
2009-11-14 01:08:56 +08:00
|
|
|
VCallOffset[GD] = Offset/8;
|
|
|
|
Index_t &idx = VCall[GD];
|
2009-10-12 06:13:54 +08:00
|
|
|
// Allocate the first one, after that, we reuse the previous one.
|
|
|
|
if (idx == 0) {
|
2009-11-14 01:08:56 +08:00
|
|
|
NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
|
2009-10-12 06:13:54 +08:00
|
|
|
idx = VCalls.size()+1;
|
|
|
|
VCalls.push_back(0);
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf(" vcall for %s at %d with delta %d\n",
|
2009-11-14 07:45:53 +08:00
|
|
|
MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
|
2009-11-30 09:19:33 +08:00
|
|
|
Index_t Offset, int64_t CurrentVBaseOffset) {
|
2009-10-12 06:13:54 +08:00
|
|
|
for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
|
2009-11-14 01:08:56 +08:00
|
|
|
++mi) {
|
|
|
|
const CXXMethodDecl *MD = *mi;
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
// For destructors, add both the complete and the deleting destructor
|
|
|
|
// to the vtable.
|
|
|
|
AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
|
2009-11-30 09:19:33 +08:00
|
|
|
CurrentVBaseOffset);
|
2009-11-14 01:08:56 +08:00
|
|
|
AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
|
2009-11-07 07:27:42 +08:00
|
|
|
CurrentVBaseOffset);
|
2009-11-30 09:19:33 +08:00
|
|
|
} else
|
|
|
|
AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
|
2009-11-14 01:08:56 +08:00
|
|
|
}
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
|
|
|
|
const CXXRecordDecl *PrimaryBase,
|
|
|
|
bool PrimaryBaseWasVirtual, bool MorallyVirtual,
|
2009-11-07 07:27:42 +08:00
|
|
|
int64_t Offset, int64_t CurrentVBaseOffset,
|
|
|
|
Path_t *Path) {
|
2009-10-15 10:04:03 +08:00
|
|
|
Path->push_back(std::make_pair(RD, Offset));
|
2009-10-12 06:13:54 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
if (i->isVirtual())
|
|
|
|
continue;
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
|
|
|
|
uint64_t o = Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
StartNewTable();
|
2009-11-13 10:13:54 +08:00
|
|
|
GenerateVtableForBase(Base, o, MorallyVirtual, false,
|
2009-11-07 07:27:42 +08:00
|
|
|
CurrentVBaseOffset, Path);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 10:04:03 +08:00
|
|
|
Path->pop_back();
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
|
2009-10-15 02:14:51 +08:00
|
|
|
// #define D(X) do { X; } while (0)
|
|
|
|
#define D(X)
|
|
|
|
|
|
|
|
void insertVCalls(int InsertionPoint) {
|
|
|
|
llvm::Constant *e = 0;
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf("============= combining vbase/vcall\n"));
|
2009-10-15 02:14:51 +08:00
|
|
|
D(VCalls.insert(VCalls.begin(), 673));
|
|
|
|
D(VCalls.push_back(672));
|
2009-10-15 17:30:16 +08:00
|
|
|
methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
|
2009-10-15 02:14:51 +08:00
|
|
|
// The vcalls come first...
|
|
|
|
for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
|
|
|
|
e = VCalls.rend();
|
|
|
|
i != e; ++i)
|
|
|
|
methods[InsertionPoint++] = wrap((0?600:0) + *i);
|
|
|
|
VCalls.clear();
|
2009-11-10 10:30:51 +08:00
|
|
|
VCall.clear();
|
2009-10-15 02:14:51 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 07:13:20 +08:00
|
|
|
void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
|
|
|
|
Index_t AddressPoint) {
|
|
|
|
D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString(),
|
|
|
|
LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
|
2009-11-20 04:52:19 +08:00
|
|
|
subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
|
2009-11-14 07:13:20 +08:00
|
|
|
|
|
|
|
// Now also add the address point for all our primary bases.
|
|
|
|
while (1) {
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
RD = Layout.getPrimaryBase();
|
|
|
|
const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
// FIXME: Double check this.
|
|
|
|
if (RD == 0)
|
|
|
|
break;
|
|
|
|
if (PrimaryBaseWasVirtual &&
|
|
|
|
BLayout.getVBaseClassOffset(RD) != Offset)
|
|
|
|
break;
|
|
|
|
D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString(),
|
|
|
|
LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
|
2009-11-20 04:52:19 +08:00
|
|
|
subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
|
2009-11-14 07:13:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-01 04:06:59 +08:00
|
|
|
Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
|
|
|
|
const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
|
|
|
|
bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
|
2009-11-07 07:27:42 +08:00
|
|
|
int64_t CurrentVBaseOffset,
|
2009-11-01 04:06:59 +08:00
|
|
|
Path_t *Path) {
|
2009-10-15 10:04:03 +08:00
|
|
|
bool alloc = false;
|
|
|
|
if (Path == 0) {
|
|
|
|
alloc = true;
|
|
|
|
Path = new Path_t;
|
|
|
|
}
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
StartNewTable();
|
|
|
|
extra = 0;
|
2009-10-15 02:14:51 +08:00
|
|
|
bool DeferVCalls = MorallyVirtual || ForVirtualBase;
|
|
|
|
int VCallInsertionPoint = methods.size();
|
|
|
|
if (!DeferVCalls) {
|
|
|
|
insertVCalls(VCallInsertionPoint);
|
2009-10-15 17:30:16 +08:00
|
|
|
} else
|
|
|
|
// FIXME: just for extra, or for all uses of VCalls.size post this?
|
|
|
|
extra = -VCalls.size();
|
2009-10-12 06:13:54 +08:00
|
|
|
|
2009-11-13 10:13:54 +08:00
|
|
|
methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
|
2009-10-12 06:13:54 +08:00
|
|
|
methods.push_back(rtti);
|
|
|
|
Index_t AddressPoint = methods.size();
|
|
|
|
|
|
|
|
InstallThunks();
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf("============= combining methods\n"));
|
2009-10-12 06:13:54 +08:00
|
|
|
methods.insert(methods.end(), submethods.begin(), submethods.end());
|
|
|
|
submethods.clear();
|
|
|
|
|
|
|
|
// and then the non-virtual bases.
|
|
|
|
NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
|
2009-11-07 07:27:42 +08:00
|
|
|
MorallyVirtual, Offset, CurrentVBaseOffset, Path);
|
2009-10-15 02:14:51 +08:00
|
|
|
|
|
|
|
if (ForVirtualBase) {
|
2009-11-13 04:47:57 +08:00
|
|
|
// FIXME: We're adding to VCalls in callers, we need to do the overrides
|
|
|
|
// in the inner part, so that we know the complete set of vcalls during
|
|
|
|
// the build and don't have to insert into methods. Saving out the
|
|
|
|
// AddressPoint here, would need to be fixed, if we didn't do that. Also
|
|
|
|
// retroactively adding vcalls for overrides later wind up in the wrong
|
|
|
|
// place, the vcall slot has to be alloted during the walk of the base
|
|
|
|
// when the function is first introduces.
|
2009-10-15 02:14:51 +08:00
|
|
|
AddressPoint += VCalls.size();
|
2009-11-13 04:47:57 +08:00
|
|
|
insertVCalls(VCallInsertionPoint);
|
2009-10-15 02:14:51 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 07:13:20 +08:00
|
|
|
AddAddressPoints(RD, Offset, AddressPoint);
|
2009-11-13 04:47:57 +08:00
|
|
|
|
2009-10-15 10:04:03 +08:00
|
|
|
if (alloc) {
|
|
|
|
delete Path;
|
|
|
|
}
|
2009-10-12 06:13:54 +08:00
|
|
|
return AddressPoint;
|
|
|
|
}
|
|
|
|
|
2009-11-01 04:06:59 +08:00
|
|
|
void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
|
|
|
|
bool updateVBIndex, Index_t current_vbindex,
|
2009-11-30 09:19:33 +08:00
|
|
|
int64_t CurrentVBaseOffset) {
|
2009-11-01 04:06:59 +08:00
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
|
|
|
|
// vtables are composed from the chain of primaries.
|
|
|
|
if (PrimaryBase) {
|
|
|
|
D1(printf(" doing primaries for %s most derived %s\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString()));
|
|
|
|
|
2009-11-07 07:27:42 +08:00
|
|
|
int BaseCurrentVBaseOffset = CurrentVBaseOffset;
|
|
|
|
if (PrimaryBaseWasVirtual)
|
|
|
|
BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
|
2009-11-01 04:06:59 +08:00
|
|
|
if (!PrimaryBaseWasVirtual)
|
|
|
|
Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
|
2009-11-30 09:19:33 +08:00
|
|
|
updateVBIndex, current_vbindex, BaseCurrentVBaseOffset);
|
2009-11-01 04:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
D1(printf(" doing vcall entries for %s most derived %s\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString()));
|
|
|
|
|
|
|
|
// And add the virtuals for the class to the primary vtable.
|
2009-11-30 09:19:33 +08:00
|
|
|
AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
|
2009-11-01 04:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
|
|
|
|
bool updateVBIndex, Index_t current_vbindex,
|
2009-11-07 07:27:42 +08:00
|
|
|
bool RDisVirtualBase, int64_t CurrentVBaseOffset,
|
2009-11-30 09:19:33 +08:00
|
|
|
bool bottom) {
|
2009-10-12 06:13:54 +08:00
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
|
|
|
|
// vtables are composed from the chain of primaries.
|
|
|
|
if (PrimaryBase) {
|
2009-11-07 07:27:42 +08:00
|
|
|
int BaseCurrentVBaseOffset = CurrentVBaseOffset;
|
|
|
|
if (PrimaryBaseWasVirtual) {
|
2009-10-12 06:13:54 +08:00
|
|
|
IndirectPrimary.insert(PrimaryBase);
|
2009-11-07 07:27:42 +08:00
|
|
|
BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
}
|
2009-11-01 04:06:59 +08:00
|
|
|
|
|
|
|
D1(printf(" doing primaries for %s most derived %s\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString()));
|
|
|
|
|
|
|
|
VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
|
2009-11-07 07:27:42 +08:00
|
|
|
updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
|
2009-11-30 09:19:33 +08:00
|
|
|
BaseCurrentVBaseOffset, false);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf(" doing vbase entries for %s most derived %s\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString()));
|
|
|
|
GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
|
|
|
|
|
|
|
|
if (RDisVirtualBase || bottom) {
|
|
|
|
Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
|
2009-11-30 09:19:33 +08:00
|
|
|
CurrentVBaseOffset);
|
2009-11-01 04:06:59 +08:00
|
|
|
}
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
|
2009-11-13 10:13:54 +08:00
|
|
|
int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
|
|
|
|
bool MorallyVirtual = false,
|
2009-10-12 06:13:54 +08:00
|
|
|
bool ForVirtualBase = false,
|
2009-11-07 07:27:42 +08:00
|
|
|
int CurrentVBaseOffset = 0,
|
2009-10-15 10:04:03 +08:00
|
|
|
Path_t *Path = 0) {
|
2009-10-12 06:13:54 +08:00
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return 0;
|
|
|
|
|
2009-11-13 10:35:38 +08:00
|
|
|
// Construction vtable don't need parts that have no virtual bases and
|
|
|
|
// aren't morally virtual.
|
|
|
|
if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
|
|
|
|
extra = 0;
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf("building entries for base %s most derived %s\n",
|
|
|
|
RD->getNameAsCString(), Class->getNameAsCString()));
|
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
if (ForVirtualBase)
|
2009-11-01 04:06:59 +08:00
|
|
|
extra = VCalls.size();
|
2009-10-12 06:13:54 +08:00
|
|
|
|
2009-11-01 04:06:59 +08:00
|
|
|
VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
|
2009-11-07 07:27:42 +08:00
|
|
|
CurrentVBaseOffset, true);
|
2009-10-12 06:13:54 +08:00
|
|
|
|
|
|
|
if (Path)
|
2009-11-07 07:27:42 +08:00
|
|
|
OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
|
2009-10-12 06:13:54 +08:00
|
|
|
|
2009-11-01 04:06:59 +08:00
|
|
|
return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
|
2009-11-07 07:27:42 +08:00
|
|
|
Offset, ForVirtualBase, CurrentVBaseOffset, Path);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateVtableForVBases(const CXXRecordDecl *RD,
|
|
|
|
int64_t Offset = 0,
|
2009-10-15 10:04:03 +08:00
|
|
|
Path_t *Path = 0) {
|
2009-10-12 06:13:54 +08:00
|
|
|
bool alloc = false;
|
|
|
|
if (Path == 0) {
|
|
|
|
alloc = true;
|
2009-10-15 10:04:03 +08:00
|
|
|
Path = new Path_t;
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
// FIXME: We also need to override using all paths to a virtual base,
|
|
|
|
// right now, we just process the first path
|
|
|
|
Path->push_back(std::make_pair(RD, Offset));
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (i->isVirtual() && !IndirectPrimary.count(Base)) {
|
|
|
|
// Mark it so we don't output it twice.
|
|
|
|
IndirectPrimary.insert(Base);
|
|
|
|
StartNewTable();
|
2009-10-15 02:14:51 +08:00
|
|
|
VCall.clear();
|
2009-10-12 06:13:54 +08:00
|
|
|
int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
|
2009-11-07 07:27:42 +08:00
|
|
|
int64_t CurrentVBaseOffset = BaseOffset;
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf("vtable %s virtual base %s\n",
|
|
|
|
Class->getNameAsCString(), Base->getNameAsCString()));
|
2009-11-13 10:13:54 +08:00
|
|
|
GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
|
2009-11-07 07:27:42 +08:00
|
|
|
Path);
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
2009-11-13 04:47:57 +08:00
|
|
|
int64_t BaseOffset;
|
2009-10-12 06:13:54 +08:00
|
|
|
if (i->isVirtual())
|
|
|
|
BaseOffset = BLayout.getVBaseClassOffset(Base);
|
2009-11-13 04:47:57 +08:00
|
|
|
else {
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
BaseOffset = Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
}
|
|
|
|
|
2009-10-15 10:04:03 +08:00
|
|
|
if (Base->getNumVBases()) {
|
2009-10-12 06:13:54 +08:00
|
|
|
GenerateVtableForVBases(Base, BaseOffset, Path);
|
2009-10-15 10:04:03 +08:00
|
|
|
}
|
2009-10-12 06:13:54 +08:00
|
|
|
}
|
|
|
|
Path->pop_back();
|
|
|
|
if (alloc)
|
|
|
|
delete Path;
|
|
|
|
}
|
|
|
|
};
|
2009-11-28 06:21:51 +08:00
|
|
|
}
|
|
|
|
|
2009-11-28 04:47:55 +08:00
|
|
|
/// TypeConversionRequiresAdjustment - Returns whether conversion from a
|
|
|
|
/// derived type to a base type requires adjustment.
|
|
|
|
static bool
|
|
|
|
TypeConversionRequiresAdjustment(ASTContext &Ctx,
|
|
|
|
const CXXRecordDecl *DerivedDecl,
|
|
|
|
const CXXRecordDecl *BaseDecl) {
|
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/true);
|
|
|
|
if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
|
|
|
|
isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
|
|
|
|
assert(false && "Class must be derived from the passed in base class!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-28 11:03:52 +08:00
|
|
|
// If we found a virtual base we always want to require adjustment.
|
|
|
|
if (Paths.getDetectedVirtual())
|
|
|
|
return true;
|
|
|
|
|
2009-11-28 04:47:55 +08:00
|
|
|
const CXXBasePath &Path = Paths.front();
|
|
|
|
|
2009-11-28 11:03:52 +08:00
|
|
|
for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
|
2009-11-28 04:47:55 +08:00
|
|
|
const CXXBasePathElement &Element = Path[Start];
|
|
|
|
|
|
|
|
// Check the base class offset.
|
|
|
|
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
|
|
|
|
|
|
|
|
const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
|
|
|
|
const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
|
|
|
|
|
|
|
|
if (Layout.getBaseClassOffset(Base) != 0) {
|
|
|
|
// This requires an adjustment.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
TypeConversionRequiresAdjustment(ASTContext &Ctx,
|
|
|
|
QualType DerivedType, QualType BaseType) {
|
|
|
|
// Canonicalize the types.
|
|
|
|
QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
|
|
|
|
QualType CanBaseType = Ctx.getCanonicalType(BaseType);
|
|
|
|
|
|
|
|
assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
|
|
|
|
"Types must have same type class!");
|
|
|
|
|
|
|
|
if (CanDerivedType == CanBaseType) {
|
|
|
|
// No adjustment needed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) {
|
|
|
|
CanDerivedType = RT->getPointeeType();
|
|
|
|
CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType();
|
|
|
|
} else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) {
|
|
|
|
CanDerivedType = PT->getPointeeType();
|
|
|
|
CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType();
|
|
|
|
} else {
|
|
|
|
assert(false && "Unexpected return type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CanDerivedType == CanBaseType) {
|
|
|
|
// No adjustment needed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *DerivedDecl =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
|
|
|
|
|
|
|
|
return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
int64_t CurrentIndex = 0;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
|
|
|
if (PrimaryBase) {
|
2009-12-01 03:43:26 +08:00
|
|
|
assert(PrimaryBase->isDefinition() &&
|
|
|
|
"Should have the definition decl of the primary base!");
|
2009-11-28 04:47:55 +08:00
|
|
|
|
|
|
|
// Since the record decl shares its vtable pointer with the primary base
|
|
|
|
// we need to start counting at the end of the primary base's vtable.
|
|
|
|
CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXDestructorDecl *ImplicitVirtualDtor = 0;
|
|
|
|
|
|
|
|
for (CXXRecordDecl::method_iterator i = RD->method_begin(),
|
|
|
|
e = RD->method_end(); i != e; ++i) {
|
|
|
|
const CXXMethodDecl *MD = *i;
|
|
|
|
|
|
|
|
// We only want virtual methods.
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool ShouldAddEntryForMethod = true;
|
|
|
|
|
|
|
|
// Check if this method overrides a method in the primary base.
|
|
|
|
for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
|
|
|
|
e = MD->end_overridden_methods(); i != e; ++i) {
|
|
|
|
const CXXMethodDecl *OverriddenMD = *i;
|
|
|
|
const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
|
|
|
|
assert(OverriddenMD->isCanonicalDecl() &&
|
|
|
|
"Should have the canonical decl of the overridden RD!");
|
|
|
|
|
|
|
|
if (OverriddenRD == PrimaryBase) {
|
|
|
|
// Check if converting from the return type of the method to the
|
|
|
|
// return type of the overridden method requires conversion.
|
|
|
|
QualType ReturnType =
|
|
|
|
MD->getType()->getAs<FunctionType>()->getResultType();
|
|
|
|
QualType OverriddenReturnType =
|
|
|
|
OverriddenMD->getType()->getAs<FunctionType>()->getResultType();
|
|
|
|
|
|
|
|
if (!TypeConversionRequiresAdjustment(CGM.getContext(),
|
|
|
|
ReturnType, OverriddenReturnType)) {
|
|
|
|
// This index is shared between the index in the vtable of the primary
|
|
|
|
// base class.
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
const CXXDestructorDecl *OverriddenDD =
|
|
|
|
cast<CXXDestructorDecl>(OverriddenMD);
|
|
|
|
|
|
|
|
// Add both the complete and deleting entries.
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
|
|
|
|
getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
|
|
|
|
getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
|
|
|
|
} else {
|
|
|
|
MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to add an entry for this method.
|
|
|
|
ShouldAddEntryForMethod = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ShouldAddEntryForMethod)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
if (MD->isImplicit()) {
|
|
|
|
assert(!ImplicitVirtualDtor &&
|
|
|
|
"Did already see an implicit virtual dtor!");
|
|
|
|
ImplicitVirtualDtor = DD;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the complete dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
|
|
|
|
|
|
|
|
// Add the deleting dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
|
|
|
|
} else {
|
|
|
|
// Add the entry.
|
|
|
|
MethodVtableIndices[MD] = CurrentIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImplicitVirtualDtor) {
|
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// If a class has an implicitly-defined virtual destructor,
|
|
|
|
// its entries come after the declared virtual function pointers.
|
|
|
|
|
|
|
|
// Add the complete dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
|
|
|
|
CurrentIndex++;
|
|
|
|
|
|
|
|
// Add the deleting dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
|
|
|
|
CurrentIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumVirtualFunctionPointers[RD] = CurrentIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
|
|
|
|
NumVirtualFunctionPointers.find(RD);
|
|
|
|
if (I != NumVirtualFunctionPointers.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
ComputeMethodVtableIndices(RD);
|
|
|
|
|
|
|
|
I = NumVirtualFunctionPointers.find(RD);
|
|
|
|
assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
|
2009-11-14 01:08:56 +08:00
|
|
|
MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
|
2009-10-12 06:13:54 +08:00
|
|
|
if (I != MethodVtableIndices.end())
|
|
|
|
return I->second;
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
|
2009-11-28 04:47:55 +08:00
|
|
|
|
|
|
|
ComputeMethodVtableIndices(RD);
|
|
|
|
|
2009-11-14 01:08:56 +08:00
|
|
|
I = MethodVtableIndices.find(GD);
|
2009-10-12 06:13:54 +08:00
|
|
|
assert(I != MethodVtableIndices.end() && "Did not find index!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *VBase) {
|
|
|
|
ClassPairTy ClassPair(RD, VBase);
|
|
|
|
|
|
|
|
VirtualBaseClassIndiciesTy::iterator I =
|
|
|
|
VirtualBaseClassIndicies.find(ClassPair);
|
|
|
|
if (I != VirtualBaseClassIndicies.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
std::vector<llvm::Constant *> methods;
|
|
|
|
// FIXME: This seems expensive. Can we do a partial job to get
|
|
|
|
// just this data.
|
2009-11-13 10:13:54 +08:00
|
|
|
VtableBuilder b(methods, RD, RD, 0, CGM);
|
2009-11-01 04:06:59 +08:00
|
|
|
D1(printf("vtable %s\n", RD->getNameAsCString()));
|
2009-10-12 06:13:54 +08:00
|
|
|
b.GenerateVtableForBase(RD);
|
|
|
|
b.GenerateVtableForVBases(RD);
|
|
|
|
|
|
|
|
for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
|
|
|
|
b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
|
|
|
|
// Insert all types.
|
|
|
|
ClassPairTy ClassPair(RD, I->first);
|
|
|
|
|
|
|
|
VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
I = VirtualBaseClassIndicies.find(ClassPair);
|
|
|
|
assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
|
|
|
|
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2009-11-13 04:47:57 +08:00
|
|
|
llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
|
|
|
|
const CXXRecordDecl *RD,
|
2009-11-12 04:26:26 +08:00
|
|
|
uint64_t Offset) {
|
2009-10-12 06:13:54 +08:00
|
|
|
llvm::SmallString<256> OutName;
|
2009-11-13 04:47:57 +08:00
|
|
|
if (LayoutClass != RD)
|
2009-11-21 17:06:22 +08:00
|
|
|
getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
|
2009-11-12 04:26:26 +08:00
|
|
|
else
|
2009-11-21 17:06:22 +08:00
|
|
|
getMangleContext().mangleCXXVtable(RD, OutName);
|
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-10-12 06:57:54 +08:00
|
|
|
|
2009-10-12 06:13:54 +08:00
|
|
|
std::vector<llvm::Constant *> methods;
|
|
|
|
llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
|
|
|
|
int64_t AddressPoint;
|
|
|
|
|
2009-11-19 12:04:36 +08:00
|
|
|
llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
|
2009-11-20 04:52:19 +08:00
|
|
|
if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
|
2009-11-19 12:04:36 +08:00
|
|
|
AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
|
|
|
|
Offset)];
|
2009-11-20 04:52:19 +08:00
|
|
|
// FIXME: We can never have 0 address point. Do this for now so gepping
|
|
|
|
// retains the same structure. Later, we'll just assert.
|
|
|
|
if (AddressPoint == 0)
|
|
|
|
AddressPoint = 1;
|
|
|
|
} else {
|
2009-11-19 12:04:36 +08:00
|
|
|
VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
|
2009-10-12 06:13:54 +08:00
|
|
|
|
2009-11-19 12:04:36 +08:00
|
|
|
D1(printf("vtable %s\n", RD->getNameAsCString()));
|
|
|
|
// First comes the vtables for all the non-virtual bases...
|
|
|
|
AddressPoint = b.GenerateVtableForBase(RD, Offset);
|
2009-10-12 06:13:54 +08:00
|
|
|
|
2009-11-19 12:04:36 +08:00
|
|
|
// then the vtables for all the virtual bases.
|
|
|
|
b.GenerateVtableForVBases(RD, Offset);
|
2009-10-12 06:13:54 +08:00
|
|
|
|
2009-11-19 12:04:36 +08:00
|
|
|
bool CreateDefinition = true;
|
|
|
|
if (LayoutClass != RD)
|
|
|
|
CreateDefinition = true;
|
|
|
|
else {
|
2009-12-01 07:41:22 +08:00
|
|
|
const ASTRecordLayout &Layout =
|
|
|
|
getContext().getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
|
2009-11-19 12:04:36 +08:00
|
|
|
if (!KeyFunction->getBody()) {
|
|
|
|
// If there is a KeyFunction, and it isn't defined, just build a
|
|
|
|
// reference to the vtable.
|
|
|
|
CreateDefinition = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-13 04:47:57 +08:00
|
|
|
|
2009-11-19 12:04:36 +08:00
|
|
|
llvm::Constant *C = 0;
|
|
|
|
llvm::Type *type = Ptr8Ty;
|
|
|
|
llvm::GlobalVariable::LinkageTypes linktype
|
|
|
|
= llvm::GlobalValue::ExternalLinkage;
|
|
|
|
if (CreateDefinition) {
|
|
|
|
llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size());
|
|
|
|
C = llvm::ConstantArray::get(ntype, methods);
|
|
|
|
linktype = llvm::GlobalValue::LinkOnceODRLinkage;
|
|
|
|
if (LayoutClass->isInAnonymousNamespace())
|
|
|
|
linktype = llvm::GlobalValue::InternalLinkage;
|
|
|
|
type = ntype;
|
|
|
|
}
|
|
|
|
llvm::GlobalVariable *OGV = GV;
|
|
|
|
GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
|
|
|
|
if (OGV) {
|
|
|
|
GV->takeName(OGV);
|
|
|
|
llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
|
|
|
|
OGV->getType());
|
|
|
|
OGV->replaceAllUsesWith(NewPtr);
|
|
|
|
OGV->eraseFromParent();
|
|
|
|
}
|
|
|
|
bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
|
|
|
|
if (Hidden)
|
|
|
|
GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
|
|
|
|
}
|
2009-11-18 12:00:48 +08:00
|
|
|
llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
|
2009-11-10 15:44:33 +08:00
|
|
|
llvm::Constant *AddressPointC;
|
|
|
|
uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
|
|
|
|
AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
|
|
|
|
AddressPoint*LLVMPointerWidth/8);
|
2009-11-13 04:47:57 +08:00
|
|
|
vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
|
|
|
|
1);
|
2009-11-10 15:44:33 +08:00
|
|
|
|
2009-11-20 04:52:19 +08:00
|
|
|
assert(vtable->getType() == Ptr8Ty);
|
2009-10-12 06:13:54 +08:00
|
|
|
return vtable;
|
|
|
|
}
|
2009-11-10 10:30:51 +08:00
|
|
|
|
2009-12-03 03:07:44 +08:00
|
|
|
namespace {
|
2009-11-10 10:30:51 +08:00
|
|
|
class VTTBuilder {
|
|
|
|
/// Inits - The list of values built for the VTT.
|
|
|
|
std::vector<llvm::Constant *> &Inits;
|
|
|
|
/// Class - The most derived class that this vtable is being built for.
|
|
|
|
const CXXRecordDecl *Class;
|
|
|
|
CodeGenModule &CGM; // Per-module state.
|
2009-11-11 08:35:07 +08:00
|
|
|
llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
|
2009-11-11 11:08:24 +08:00
|
|
|
/// BLayout - Layout for the most derived class that this vtable is being
|
|
|
|
/// built for.
|
|
|
|
const ASTRecordLayout &BLayout;
|
2009-11-13 09:54:23 +08:00
|
|
|
CodeGenModule::AddrMap_t &AddressPoints;
|
2009-11-13 04:47:57 +08:00
|
|
|
// vtbl - A pointer to the vtable for Class.
|
|
|
|
llvm::Constant *ClassVtbl;
|
|
|
|
llvm::LLVMContext &VMContext;
|
2009-11-11 08:35:07 +08:00
|
|
|
|
2009-11-13 06:56:32 +08:00
|
|
|
/// BuildVtablePtr - Build up a referene to the given secondary vtable
|
2009-11-13 09:54:23 +08:00
|
|
|
llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
|
|
|
|
const CXXRecordDecl *VtblClass,
|
|
|
|
const CXXRecordDecl *RD,
|
2009-11-13 06:56:32 +08:00
|
|
|
uint64_t Offset) {
|
|
|
|
int64_t AddressPoint;
|
2009-11-13 09:54:23 +08:00
|
|
|
AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
|
2009-11-13 07:36:21 +08:00
|
|
|
// FIXME: We can never have 0 address point. Do this for now so gepping
|
2009-11-20 04:52:19 +08:00
|
|
|
// retains the same structure. Later we'll just assert.
|
2009-11-13 07:36:21 +08:00
|
|
|
if (AddressPoint == 0)
|
|
|
|
AddressPoint = 1;
|
2009-11-13 09:54:23 +08:00
|
|
|
D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
|
|
|
|
RD->getNameAsCString(), VtblClass->getNameAsCString(),
|
|
|
|
Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
|
2009-11-13 06:56:32 +08:00
|
|
|
uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
|
|
|
|
llvm::Constant *init;
|
|
|
|
init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
|
|
|
|
AddressPoint*LLVMPointerWidth/8);
|
|
|
|
init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2009-11-13 04:47:57 +08:00
|
|
|
/// Secondary - Add the secondary vtable pointers to Inits. Offset is the
|
|
|
|
/// current offset in bits to the object we're working on.
|
2009-11-13 06:56:32 +08:00
|
|
|
void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
|
2009-11-13 09:54:23 +08:00
|
|
|
const CXXRecordDecl *VtblClass, uint64_t Offset=0,
|
|
|
|
bool MorallyVirtual=false) {
|
2009-11-11 08:35:07 +08:00
|
|
|
if (RD->getNumVBases() == 0 && ! MorallyVirtual)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
bool NonVirtualPrimaryBase;
|
|
|
|
NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
|
|
|
|
bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
|
2009-11-11 11:08:24 +08:00
|
|
|
uint64_t BaseOffset;
|
|
|
|
if (!i->isVirtual()) {
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
BaseOffset = Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
} else
|
|
|
|
BaseOffset = BLayout.getVBaseClassOffset(Base);
|
2009-11-13 07:36:21 +08:00
|
|
|
llvm::Constant *subvtbl = vtbl;
|
2009-11-13 09:54:23 +08:00
|
|
|
const CXXRecordDecl *subVtblClass = VtblClass;
|
2009-11-11 08:35:07 +08:00
|
|
|
if ((Base->getNumVBases() || BaseMorallyVirtual)
|
|
|
|
&& !NonVirtualPrimaryBase) {
|
|
|
|
// FIXME: Slightly too many of these for __ZTT8test8_B2
|
2009-11-13 06:56:32 +08:00
|
|
|
llvm::Constant *init;
|
2009-11-13 07:36:21 +08:00
|
|
|
if (BaseMorallyVirtual)
|
2009-11-13 09:54:23 +08:00
|
|
|
init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
|
2009-11-13 07:36:21 +08:00
|
|
|
else {
|
2009-11-13 06:56:32 +08:00
|
|
|
init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
|
2009-11-13 07:36:21 +08:00
|
|
|
subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
|
2009-11-13 09:54:23 +08:00
|
|
|
subVtblClass = Base;
|
2009-11-13 07:36:21 +08:00
|
|
|
}
|
2009-11-13 06:56:32 +08:00
|
|
|
Inits.push_back(init);
|
2009-11-11 08:35:07 +08:00
|
|
|
}
|
2009-11-13 09:54:23 +08:00
|
|
|
Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
|
2009-11-11 08:35:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-13 04:47:57 +08:00
|
|
|
/// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
|
|
|
|
/// currnet object we're working on.
|
|
|
|
void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
|
2009-11-11 08:35:07 +08:00
|
|
|
if (RD->getNumVBases() == 0 && !MorallyVirtual)
|
|
|
|
return;
|
|
|
|
|
2009-11-13 04:47:57 +08:00
|
|
|
llvm::Constant *init;
|
2009-11-13 09:54:23 +08:00
|
|
|
const CXXRecordDecl *VtblClass;
|
|
|
|
|
2009-11-11 08:35:07 +08:00
|
|
|
// First comes the primary virtual table pointer...
|
2009-11-13 09:54:23 +08:00
|
|
|
if (MorallyVirtual) {
|
|
|
|
init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
|
|
|
|
VtblClass = Class;
|
|
|
|
} else {
|
2009-11-13 04:47:57 +08:00
|
|
|
init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
|
2009-11-13 09:54:23 +08:00
|
|
|
VtblClass = RD;
|
|
|
|
}
|
2009-11-13 06:56:32 +08:00
|
|
|
llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
|
2009-11-13 04:47:57 +08:00
|
|
|
Inits.push_back(init);
|
2009-11-11 08:35:07 +08:00
|
|
|
|
|
|
|
// then the secondary VTTs....
|
2009-11-13 04:47:57 +08:00
|
|
|
SecondaryVTTs(RD, Offset, MorallyVirtual);
|
2009-11-11 08:35:07 +08:00
|
|
|
|
|
|
|
// and last the secondary vtable pointers.
|
2009-11-13 09:54:23 +08:00
|
|
|
Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
|
2009-11-11 08:35:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
|
|
|
|
/// built from each direct non-virtual proper base that requires a VTT in
|
|
|
|
/// declaration order.
|
2009-11-13 04:47:57 +08:00
|
|
|
void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
|
|
|
|
bool MorallyVirtual=false) {
|
2009-11-11 08:35:07 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (i->isVirtual())
|
|
|
|
continue;
|
2009-11-13 04:47:57 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
|
|
|
|
BuildVTT(Base, BaseOffset, MorallyVirtual);
|
2009-11-11 08:35:07 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-10 10:30:51 +08:00
|
|
|
|
2009-11-11 08:35:07 +08:00
|
|
|
/// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
|
|
|
|
/// graph preorder.
|
|
|
|
void VirtualVTTs(const CXXRecordDecl *RD) {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (i->isVirtual() && !SeenVBase.count(Base)) {
|
|
|
|
SeenVBase.insert(Base);
|
2009-11-13 04:47:57 +08:00
|
|
|
uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
|
|
|
|
BuildVTT(Base, BaseOffset, true);
|
2009-11-11 08:35:07 +08:00
|
|
|
}
|
|
|
|
VirtualVTTs(Base);
|
|
|
|
}
|
|
|
|
}
|
2009-11-10 10:30:51 +08:00
|
|
|
public:
|
|
|
|
VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
|
2009-11-11 11:08:24 +08:00
|
|
|
CodeGenModule &cgm)
|
|
|
|
: Inits(inits), Class(c), CGM(cgm),
|
2009-11-13 04:47:57 +08:00
|
|
|
BLayout(cgm.getContext().getASTRecordLayout(c)),
|
2009-11-13 09:54:23 +08:00
|
|
|
AddressPoints(*cgm.AddressPoints[c]),
|
2009-11-13 04:47:57 +08:00
|
|
|
VMContext(cgm.getModule().getContext()) {
|
2009-11-10 15:44:33 +08:00
|
|
|
|
2009-11-11 08:35:07 +08:00
|
|
|
// First comes the primary virtual table pointer for the complete class...
|
2009-11-13 04:47:57 +08:00
|
|
|
ClassVtbl = CGM.getVtableInfo().getVtable(Class);
|
|
|
|
Inits.push_back(ClassVtbl);
|
|
|
|
ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
|
|
|
|
|
2009-11-11 08:35:07 +08:00
|
|
|
// then the secondary VTTs...
|
|
|
|
SecondaryVTTs(Class);
|
|
|
|
|
|
|
|
// then the secondary vtable pointers...
|
2009-11-13 09:54:23 +08:00
|
|
|
Secondary(Class, ClassVtbl, Class);
|
2009-11-11 08:35:07 +08:00
|
|
|
|
|
|
|
// and last, the virtual VTTs.
|
|
|
|
VirtualVTTs(Class);
|
2009-11-10 10:30:51 +08:00
|
|
|
}
|
|
|
|
};
|
2009-12-03 03:07:44 +08:00
|
|
|
}
|
2009-11-10 10:30:51 +08:00
|
|
|
|
2009-11-10 15:44:33 +08:00
|
|
|
llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
|
2009-11-11 03:13:04 +08:00
|
|
|
// Only classes that have virtual bases need a VTT.
|
|
|
|
if (RD->getNumVBases() == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-10 10:30:51 +08:00
|
|
|
llvm::SmallString<256> OutName;
|
2009-11-21 17:06:22 +08:00
|
|
|
getMangleContext().mangleCXXVTT(RD, OutName);
|
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-11-10 10:30:51 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable::LinkageTypes linktype;
|
|
|
|
linktype = llvm::GlobalValue::LinkOnceODRLinkage;
|
2009-11-19 12:04:36 +08:00
|
|
|
if (RD->isInAnonymousNamespace())
|
|
|
|
linktype = llvm::GlobalValue::InternalLinkage;
|
2009-11-10 10:30:51 +08:00
|
|
|
std::vector<llvm::Constant *> inits;
|
|
|
|
llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
|
|
|
|
|
|
|
|
D1(printf("vtt %s\n", RD->getNameAsCString()));
|
|
|
|
|
2009-11-11 08:35:07 +08:00
|
|
|
VTTBuilder b(inits, RD, *this);
|
|
|
|
|
2009-11-10 10:30:51 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
|
|
|
|
C = llvm::ConstantArray::get(type, inits);
|
2009-11-18 12:00:48 +08:00
|
|
|
llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
|
2009-11-19 12:04:36 +08:00
|
|
|
linktype, C, Name);
|
2009-11-18 12:00:48 +08:00
|
|
|
bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
|
|
|
|
if (Hidden)
|
|
|
|
vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
|
|
|
|
return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
|
2009-11-10 10:30:51 +08:00
|
|
|
}
|
2009-11-10 15:44:33 +08:00
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
|
|
|
|
Vtables[RD] = CGM.GenerateVtable(RD, RD);
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.GenerateRTTI(RD);
|
2009-11-19 09:08:19 +08:00
|
|
|
CGM.GenerateVTT(RD);
|
|
|
|
}
|
|
|
|
|
2009-11-12 04:26:26 +08:00
|
|
|
llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
|
2009-11-10 15:44:33 +08:00
|
|
|
llvm::Constant *&vtbl = Vtables[RD];
|
|
|
|
if (vtbl)
|
|
|
|
return vtbl;
|
2009-11-13 04:47:57 +08:00
|
|
|
vtbl = CGM.GenerateVtable(RD, RD);
|
2009-11-19 12:04:36 +08:00
|
|
|
|
|
|
|
bool CreateDefinition = true;
|
2009-12-01 07:41:22 +08:00
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
|
2009-11-19 12:04:36 +08:00
|
|
|
if (!KeyFunction->getBody()) {
|
|
|
|
// If there is a KeyFunction, and it isn't defined, just build a
|
|
|
|
// reference to the vtable.
|
|
|
|
CreateDefinition = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CreateDefinition) {
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.GenerateRTTI(RD);
|
2009-11-19 12:04:36 +08:00
|
|
|
CGM.GenerateVTT(RD);
|
|
|
|
}
|
2009-11-10 15:44:33 +08:00
|
|
|
return vtbl;
|
|
|
|
}
|
2009-11-12 04:26:26 +08:00
|
|
|
|
2009-11-13 04:47:57 +08:00
|
|
|
llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
|
|
|
|
const CXXRecordDecl *RD,
|
2009-11-12 04:26:26 +08:00
|
|
|
uint64_t Offset) {
|
2009-11-13 04:47:57 +08:00
|
|
|
return CGM.GenerateVtable(LayoutClass, RD, Offset);
|
2009-11-12 04:26:26 +08:00
|
|
|
}
|
2009-12-01 07:41:22 +08:00
|
|
|
|
|
|
|
void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
const CXXRecordDecl *RD = MD->getParent();
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Get the key function.
|
|
|
|
const CXXMethodDecl *KeyFunction = Layout.getKeyFunction();
|
|
|
|
|
|
|
|
if (!KeyFunction) {
|
|
|
|
// If there's no key function, we don't want to emit the vtable here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have the key function.
|
|
|
|
if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the key function is a destructor, we only want to emit the vtable once,
|
|
|
|
// so do it for the complete destructor.
|
|
|
|
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Emit the data.
|
|
|
|
GenerateClassData(RD);
|
|
|
|
}
|
|
|
|
|