2010-04-18 00:15:18 +04:00
|
|
|
//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
|
2009-10-12 02:13:54 +04:00
|
|
|
//
|
|
|
|
// 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"
|
2010-08-31 11:33:07 +04:00
|
|
|
#include "CGCXXABI.h"
|
2009-11-27 23:47:55 +03:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-10-12 02:13:54 +04:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2010-08-06 00:39:18 +04:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2009-11-26 22:32:45 +03:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2010-02-27 19:18:19 +03:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2010-02-13 13:38:52 +03:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-02-11 11:02:13 +03:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-05-06 21:27:27 +04:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2010-03-17 23:06:32 +03:00
|
|
|
#include <algorithm>
|
2009-11-13 08:46:16 +03:00
|
|
|
#include <cstdio>
|
2009-10-12 02:13:54 +04:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2011-09-26 05:56:30 +04:00
|
|
|
CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
|
|
|
|
: CGM(CGM), VTContext(CGM.getContext()) { }
|
|
|
|
|
2010-10-11 07:25:57 +04:00
|
|
|
bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
|
|
|
|
assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
|
|
|
|
|
|
|
|
TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
|
|
|
|
if (TSK == TSK_ExplicitInstantiationDeclaration)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
|
|
|
|
if (!KeyFunction)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Itanium C++ ABI, 5.2.6 Instantiated Templates:
|
|
|
|
// An instantiation of a class template requires:
|
|
|
|
// - In the object where instantiated, the virtual table...
|
|
|
|
if (TSK == TSK_ImplicitInstantiation ||
|
|
|
|
TSK == TSK_ExplicitInstantiationDefinition)
|
|
|
|
return true;
|
|
|
|
|
2011-01-30 23:45:54 +03:00
|
|
|
// If we're building with optimization, we always emit VTables since that
|
|
|
|
// allows for virtual function calls to be devirtualized.
|
|
|
|
// (We don't want to do this in -fapple-kext mode however).
|
|
|
|
if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOptions().AppleKext)
|
|
|
|
return true;
|
|
|
|
|
2010-10-11 07:25:57 +04:00
|
|
|
return KeyFunction->hasBody();
|
|
|
|
}
|
|
|
|
|
2010-03-23 20:17:29 +03:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
|
2011-02-06 20:15:43 +03:00
|
|
|
const ThunkInfo &Thunk) {
|
2010-03-23 20:17:29 +03:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
// Compute the mangled name.
|
|
|
|
llvm::SmallString<256> Name;
|
2011-02-11 05:52:17 +03:00
|
|
|
llvm::raw_svector_ostream Out(Name);
|
2010-03-23 20:17:29 +03:00
|
|
|
if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
|
2010-08-31 11:33:07 +04:00
|
|
|
getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
|
2011-02-11 05:52:17 +03:00
|
|
|
Thunk.This, Out);
|
2010-03-23 20:17:29 +03:00
|
|
|
else
|
2011-02-11 05:52:17 +03:00
|
|
|
getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
|
|
|
|
Out.flush();
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
|
2011-02-06 20:15:43 +03:00
|
|
|
return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
|
2010-03-23 20:17:29 +03:00
|
|
|
}
|
|
|
|
|
2010-03-24 03:39:18 +03:00
|
|
|
static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
int64_t NonVirtualAdjustment,
|
|
|
|
int64_t VirtualAdjustment) {
|
|
|
|
if (!NonVirtualAdjustment && !VirtualAdjustment)
|
|
|
|
return Ptr;
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Int8PtrTy =
|
2010-03-24 03:39:18 +03:00
|
|
|
llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
|
|
|
|
llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
|
|
|
|
|
|
|
|
if (NonVirtualAdjustment) {
|
|
|
|
// Do the non-virtual adjustment.
|
|
|
|
V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VirtualAdjustment) {
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *PtrDiffTy =
|
2010-03-24 03:39:18 +03:00
|
|
|
CGF.ConvertType(CGF.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
// Do the virtual adjustment.
|
|
|
|
llvm::Value *VTablePtrPtr =
|
|
|
|
CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
|
|
|
|
|
|
|
|
llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
|
|
|
|
|
|
|
|
llvm::Value *OffsetPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
|
|
|
|
|
|
|
|
OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
|
|
|
|
|
|
|
|
// Load the adjustment offset from the vtable.
|
|
|
|
llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
|
|
|
|
|
|
|
|
// Adjust our pointer.
|
|
|
|
V = CGF.Builder.CreateInBoundsGEP(V, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast back to the original type.
|
|
|
|
return CGF.Builder.CreateBitCast(V, Ptr->getType());
|
|
|
|
}
|
|
|
|
|
2010-08-05 03:46:35 +04:00
|
|
|
static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
|
|
|
|
const ThunkInfo &Thunk, llvm::Function *Fn) {
|
2011-01-29 22:39:23 +03:00
|
|
|
CGM.setGlobalVisibility(Fn, MD);
|
2010-08-05 03:46:35 +04:00
|
|
|
|
2010-08-13 03:36:15 +04:00
|
|
|
if (!CGM.getCodeGenOpts().HiddenWeakVTables)
|
|
|
|
return;
|
|
|
|
|
2010-08-05 03:46:35 +04:00
|
|
|
// If the thunk has weak/linkonce linkage, but the function must be
|
|
|
|
// emitted in every translation unit that references it, then we can
|
|
|
|
// emit its thunks with hidden visibility, since its thunks must be
|
|
|
|
// emitted when the function is.
|
|
|
|
|
2010-08-06 00:39:18 +04:00
|
|
|
// This follows CodeGenModule::setTypeVisibility; see the comments
|
|
|
|
// there for explanation.
|
2010-08-05 03:46:35 +04:00
|
|
|
|
|
|
|
if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
|
|
|
|
Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
|
|
|
|
Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
|
|
|
|
return;
|
|
|
|
|
2011-03-26 15:10:19 +03:00
|
|
|
if (MD->getExplicitVisibility())
|
2010-08-05 03:46:35 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (MD->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case TSK_Undeclared:
|
|
|
|
break;
|
|
|
|
|
2010-08-06 00:39:18 +04:00
|
|
|
case TSK_ExplicitSpecialization:
|
2010-08-05 03:46:35 +04:00
|
|
|
case TSK_ImplicitInstantiation:
|
2010-08-13 03:36:15 +04:00
|
|
|
if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
|
2010-08-06 00:39:18 +04:00
|
|
|
return;
|
2010-08-05 03:46:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's an explicit definition, and that definition is
|
|
|
|
// out-of-line, then we can't assume that all users will have a
|
|
|
|
// definition to emit.
|
|
|
|
const FunctionDecl *Def = 0;
|
|
|
|
if (MD->hasBody(Def) && Def->isOutOfLine())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
|
|
|
}
|
|
|
|
|
2011-03-09 10:12:35 +03:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
|
|
|
|
const ABIArgInfo &infoR, CanQualType typeR) {
|
|
|
|
return (infoL.getKind() == infoR.getKind() &&
|
|
|
|
(typeL == typeR ||
|
|
|
|
(isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
|
|
|
|
(isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-06 21:27:27 +04:00
|
|
|
static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
|
|
|
|
QualType ResultType, RValue RV,
|
|
|
|
const ThunkInfo &Thunk) {
|
|
|
|
// Emit the return adjustment.
|
|
|
|
bool NullCheckValue = !ResultType->isReferenceType();
|
|
|
|
|
|
|
|
llvm::BasicBlock *AdjustNull = 0;
|
|
|
|
llvm::BasicBlock *AdjustNotNull = 0;
|
|
|
|
llvm::BasicBlock *AdjustEnd = 0;
|
|
|
|
|
|
|
|
llvm::Value *ReturnValue = RV.getScalarVal();
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
AdjustNull = CGF.createBasicBlock("adjust.null");
|
|
|
|
AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
|
|
|
|
AdjustEnd = CGF.createBasicBlock("adjust.end");
|
|
|
|
|
|
|
|
llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
|
|
|
|
CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
|
|
|
|
CGF.EmitBlock(AdjustNotNull);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
|
|
|
|
Thunk.Return.NonVirtual,
|
|
|
|
Thunk.Return.VBaseOffsetOffset);
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
CGF.Builder.CreateBr(AdjustEnd);
|
|
|
|
CGF.EmitBlock(AdjustNull);
|
|
|
|
CGF.Builder.CreateBr(AdjustEnd);
|
|
|
|
CGF.EmitBlock(AdjustEnd);
|
|
|
|
|
|
|
|
llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
|
|
|
|
PHI->addIncoming(ReturnValue, AdjustNotNull);
|
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
|
|
|
|
AdjustNull);
|
|
|
|
ReturnValue = PHI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RValue::get(ReturnValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function does roughly the same thing as GenerateThunk, but in a
|
|
|
|
// very different way, so that va_start and va_end work correctly.
|
|
|
|
// FIXME: This function assumes "this" is the first non-sret LLVM argument of
|
|
|
|
// a function, and that there is an alloca built in the entry block
|
|
|
|
// for all accesses to "this".
|
|
|
|
// FIXME: This function assumes there is only one "ret" statement per function.
|
|
|
|
// FIXME: Cloning isn't correct in the presence of indirect goto!
|
|
|
|
// FIXME: This implementation of thunks bloats codesize by duplicating the
|
|
|
|
// function definition. There are alternatives:
|
|
|
|
// 1. Add some sort of stub support to LLVM for cases where we can
|
|
|
|
// do a this adjustment, then a sibcall.
|
|
|
|
// 2. We could transform the definition to take a va_list instead of an
|
|
|
|
// actual variable argument list, then have the thunks (including a
|
|
|
|
// no-op thunk for the regular definition) call va_start/va_end.
|
|
|
|
// There's a bit of per-call overhead for this solution, but it's
|
|
|
|
// better for codesize if the definition is long.
|
|
|
|
void CodeGenFunction::GenerateVarArgsThunk(
|
|
|
|
llvm::Function *Fn,
|
|
|
|
const CGFunctionInfo &FnInfo,
|
|
|
|
GlobalDecl GD, const ThunkInfo &Thunk) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
QualType ResultType = FPT->getResultType();
|
|
|
|
|
|
|
|
// Get the original function
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty =
|
2011-05-06 21:27:27 +04:00
|
|
|
CGM.getTypes().GetFunctionType(FnInfo, /*IsVariadic*/true);
|
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
|
|
|
llvm::Function *BaseFn = cast<llvm::Function>(Callee);
|
|
|
|
|
|
|
|
// Clone to thunk.
|
|
|
|
llvm::Function *NewFn = llvm::CloneFunction(BaseFn);
|
|
|
|
CGM.getModule().getFunctionList().push_back(NewFn);
|
|
|
|
Fn->replaceAllUsesWith(NewFn);
|
|
|
|
NewFn->takeName(Fn);
|
|
|
|
Fn->eraseFromParent();
|
|
|
|
Fn = NewFn;
|
|
|
|
|
|
|
|
// "Initialize" CGF (minimally).
|
|
|
|
CurFn = Fn;
|
|
|
|
|
|
|
|
// Get the "this" value
|
|
|
|
llvm::Function::arg_iterator AI = Fn->arg_begin();
|
|
|
|
if (CGM.ReturnTypeUsesSRet(FnInfo))
|
|
|
|
++AI;
|
|
|
|
|
|
|
|
// Find the first store of "this", which will be to the alloca associated
|
|
|
|
// with "this".
|
|
|
|
llvm::Value *ThisPtr = &*AI;
|
|
|
|
llvm::BasicBlock *EntryBB = Fn->begin();
|
|
|
|
llvm::Instruction *ThisStore = 0;
|
|
|
|
for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
|
|
|
|
I != E; I++) {
|
|
|
|
if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
|
|
|
|
ThisStore = cast<llvm::StoreInst>(I);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(ThisStore && "Store of this should be in entry block?");
|
|
|
|
// Adjust "this", if necessary.
|
|
|
|
Builder.SetInsertPoint(ThisStore);
|
|
|
|
llvm::Value *AdjustedThisPtr =
|
|
|
|
PerformTypeAdjustment(*this, ThisPtr,
|
|
|
|
Thunk.This.NonVirtual,
|
|
|
|
Thunk.This.VCallOffsetOffset);
|
|
|
|
ThisStore->setOperand(0, AdjustedThisPtr);
|
|
|
|
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
|
|
|
// Fix up the returned value, if necessary.
|
|
|
|
for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
|
|
|
|
llvm::Instruction *T = I->getTerminator();
|
|
|
|
if (isa<llvm::ReturnInst>(T)) {
|
|
|
|
RValue RV = RValue::get(T->getOperand(0));
|
|
|
|
T->eraseFromParent();
|
|
|
|
Builder.SetInsertPoint(&*I);
|
|
|
|
RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
|
|
|
|
Builder.CreateRet(RV.getScalarVal());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-09 07:27:21 +03:00
|
|
|
void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
|
|
|
|
const CGFunctionInfo &FnInfo,
|
|
|
|
GlobalDecl GD, const ThunkInfo &Thunk) {
|
2010-03-24 03:39:18 +03:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
QualType ResultType = FPT->getResultType();
|
|
|
|
QualType ThisType = MD->getThisType(getContext());
|
|
|
|
|
|
|
|
FunctionArgList FunctionArgs;
|
|
|
|
|
|
|
|
// FIXME: It would be nice if more of this code could be shared with
|
|
|
|
// CodeGenFunction::GenerateCode.
|
|
|
|
|
|
|
|
// Create the implicit 'this' parameter declaration.
|
2010-08-31 11:33:07 +04:00
|
|
|
CurGD = GD;
|
|
|
|
CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
// Add the rest of the parameters.
|
|
|
|
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
|
|
|
|
E = MD->param_end(); I != E; ++I) {
|
|
|
|
ParmVarDecl *Param = *I;
|
|
|
|
|
2011-03-09 07:27:21 +03:00
|
|
|
FunctionArgs.push_back(Param);
|
2010-03-24 03:39:18 +03:00
|
|
|
}
|
2011-03-03 00:36:49 +03:00
|
|
|
|
2011-03-09 07:27:21 +03:00
|
|
|
StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
|
|
|
|
SourceLocation());
|
2010-03-24 03:39:18 +03:00
|
|
|
|
2010-08-31 11:33:07 +04:00
|
|
|
CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
|
|
|
|
|
2010-03-24 03:39:18 +03:00
|
|
|
// Adjust the 'this' pointer if necessary.
|
|
|
|
llvm::Value *AdjustedThisPtr =
|
|
|
|
PerformTypeAdjustment(*this, LoadCXXThis(),
|
|
|
|
Thunk.This.NonVirtual,
|
|
|
|
Thunk.This.VCallOffsetOffset);
|
|
|
|
|
|
|
|
CallArgList CallArgs;
|
|
|
|
|
|
|
|
// Add our adjusted 'this' pointer.
|
2011-05-02 21:57:46 +04:00
|
|
|
CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
// Add the rest of the parameters.
|
|
|
|
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
|
|
|
|
E = MD->param_end(); I != E; ++I) {
|
2011-03-11 23:59:21 +03:00
|
|
|
ParmVarDecl *param = *I;
|
|
|
|
EmitDelegateCallArg(CallArgs, param);
|
2010-03-24 03:39:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get our callee.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty =
|
2010-08-31 11:33:07 +04:00
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
|
2010-03-24 03:39:18 +03:00
|
|
|
FPT->isVariadic());
|
2011-02-06 20:15:43 +03:00
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
2011-03-09 07:27:21 +03:00
|
|
|
#ifndef NDEBUG
|
|
|
|
const CGFunctionInfo &CallFnInfo =
|
2011-08-09 22:16:09 +04:00
|
|
|
CGM.getTypes().getFunctionInfo(ResultType, CallArgs, FPT->getExtInfo());
|
2011-03-09 10:12:35 +03:00
|
|
|
assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
|
|
|
|
CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
|
|
|
|
CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
|
|
|
|
assert(similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
|
|
|
|
FnInfo.getReturnInfo(), FnInfo.getReturnType()));
|
|
|
|
assert(CallFnInfo.arg_size() == FnInfo.arg_size());
|
|
|
|
for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
|
|
|
|
assert(similar(CallFnInfo.arg_begin()[i].info,
|
|
|
|
CallFnInfo.arg_begin()[i].type,
|
|
|
|
FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
|
2011-03-09 07:27:21 +03:00
|
|
|
#endif
|
2010-03-24 03:39:18 +03:00
|
|
|
|
2010-05-20 09:54:35 +04:00
|
|
|
// Determine whether we have a return value slot to use.
|
|
|
|
ReturnValueSlot Slot;
|
|
|
|
if (!ResultType->isVoidType() &&
|
|
|
|
FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
|
|
|
|
hasAggregateLLVMType(CurFnInfo->getReturnType()))
|
|
|
|
Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
|
|
|
|
|
2010-03-24 03:39:18 +03:00
|
|
|
// Now emit our call.
|
2010-05-20 09:54:35 +04:00
|
|
|
RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
2011-05-06 21:27:27 +04:00
|
|
|
if (!Thunk.Return.isEmpty())
|
|
|
|
RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
2010-05-20 09:54:35 +04:00
|
|
|
if (!ResultType->isVoidType() && Slot.isNull())
|
2011-02-08 11:22:06 +03:00
|
|
|
CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
|
|
|
|
// Set the right linkage.
|
2010-05-25 08:30:21 +04:00
|
|
|
CGM.setFunctionLinkage(MD, Fn);
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
// Set the right visibility.
|
2010-08-05 03:46:35 +04:00
|
|
|
setThunkVisibility(CGM, MD, Thunk, Fn);
|
2010-03-24 03:39:18 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 21:31:40 +03:00
|
|
|
void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
|
|
|
|
bool UseAvailableExternallyLinkage)
|
2010-03-23 19:36:50 +03:00
|
|
|
{
|
2011-03-09 07:27:21 +03:00
|
|
|
const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(GD);
|
|
|
|
|
|
|
|
// FIXME: re-use FnInfo in this computation.
|
2011-02-06 20:15:43 +03:00
|
|
|
llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
|
2010-03-23 21:18:41 +03:00
|
|
|
|
|
|
|
// Strip off a bitcast if we got one back.
|
2010-03-24 03:35:44 +03:00
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
|
2010-03-23 21:18:41 +03:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast);
|
2010-03-24 03:35:44 +03:00
|
|
|
Entry = CE->getOperand(0);
|
2010-03-23 21:18:41 +03:00
|
|
|
}
|
2010-03-23 20:17:29 +03:00
|
|
|
|
2010-03-23 21:18:41 +03:00
|
|
|
// There's already a declaration with the same name, check if it has the same
|
|
|
|
// type or if we need to replace it.
|
2010-03-24 03:35:44 +03:00
|
|
|
if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
|
2010-08-31 11:33:07 +04:00
|
|
|
CGM.getTypes().GetFunctionTypeForVTable(GD)) {
|
2010-03-24 03:35:44 +03:00
|
|
|
llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
|
2010-03-23 21:18:41 +03:00
|
|
|
|
|
|
|
// If the types mismatch then we have to rewrite the definition.
|
|
|
|
assert(OldThunkFn->isDeclaration() &&
|
|
|
|
"Shouldn't replace non-declaration");
|
|
|
|
|
|
|
|
// Remove the name from the old thunk function and get a new thunk.
|
2011-07-23 14:55:15 +04:00
|
|
|
OldThunkFn->setName(StringRef());
|
2011-02-06 20:15:43 +03:00
|
|
|
Entry = CGM.GetAddrOfThunk(GD, Thunk);
|
2010-03-23 21:18:41 +03:00
|
|
|
|
|
|
|
// If needed, replace the old thunk with a bitcast.
|
|
|
|
if (!OldThunkFn->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2010-03-24 03:35:44 +03:00
|
|
|
llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
|
2010-03-23 21:18:41 +03:00
|
|
|
OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old thunk.
|
|
|
|
OldThunkFn->eraseFromParent();
|
|
|
|
}
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
|
2011-02-06 21:31:40 +03:00
|
|
|
|
|
|
|
if (!ThunkFn->isDeclaration()) {
|
|
|
|
if (UseAvailableExternallyLinkage) {
|
|
|
|
// There is already a thunk emitted for this function, do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-06 23:09:44 +03:00
|
|
|
// If a function has a body, it should have available_externally linkage.
|
|
|
|
assert(ThunkFn->hasAvailableExternallyLinkage() &&
|
|
|
|
"Function should have available_externally linkage!");
|
|
|
|
|
|
|
|
// Change the linkage.
|
|
|
|
CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
|
|
|
|
return;
|
2011-02-06 21:31:40 +03:00
|
|
|
}
|
|
|
|
|
2011-05-06 21:27:27 +04:00
|
|
|
if (ThunkFn->isVarArg()) {
|
|
|
|
// Varargs thunks are special; we can't just generate a call because
|
|
|
|
// we can't copy the varargs. Our implementation is rather
|
|
|
|
// expensive/sucky at the moment, so don't generate the thunk unless
|
|
|
|
// we have to.
|
|
|
|
// FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
|
|
|
|
if (!UseAvailableExternallyLinkage)
|
|
|
|
CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
|
|
|
|
} else {
|
|
|
|
// Normal thunk body generation.
|
|
|
|
CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
|
|
|
|
}
|
2011-02-06 21:31:40 +03:00
|
|
|
|
|
|
|
if (UseAvailableExternallyLinkage)
|
|
|
|
ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
|
|
|
|
const ThunkInfo &Thunk) {
|
|
|
|
// We only want to do this when building with optimizations.
|
|
|
|
if (!CGM.getCodeGenOpts().OptimizationLevel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We can't emit thunks for member functions with incomplete types.
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2011-07-10 04:18:59 +04:00
|
|
|
if (!CGM.getTypes().isFuncTypeConvertible(
|
|
|
|
cast<FunctionType>(MD->getType().getTypePtr())))
|
2011-02-06 21:31:40 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
|
2010-03-23 19:36:50 +03:00
|
|
|
}
|
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
void CodeGenVTables::EmitThunks(GlobalDecl GD)
|
|
|
|
{
|
2010-03-23 19:36:50 +03:00
|
|
|
const CXXMethodDecl *MD =
|
|
|
|
cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
|
|
|
|
|
|
|
|
// We don't need to generate thunks for the base destructor.
|
|
|
|
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
|
|
|
|
return;
|
|
|
|
|
2011-09-26 05:56:41 +04:00
|
|
|
const VTableContext::ThunkInfoVectorTy *ThunkInfoVector =
|
|
|
|
VTContext.getThunkInfo(MD);
|
|
|
|
if (!ThunkInfoVector)
|
2010-03-24 19:42:11 +03:00
|
|
|
return;
|
|
|
|
|
2011-09-26 05:56:41 +04:00
|
|
|
for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
|
|
|
|
EmitThunk(GD, (*ThunkInfoVector)[I],
|
|
|
|
/*UseAvailableExternallyLinkage=*/false);
|
2010-03-23 07:59:02 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 18:26:28 +03:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
|
2011-09-26 05:56:50 +04:00
|
|
|
const VTableComponent *Components,
|
2010-03-25 18:26:28 +03:00
|
|
|
unsigned NumComponents,
|
2011-09-26 05:56:50 +04:00
|
|
|
const VTableLayout::VTableThunkTy *VTableThunks,
|
|
|
|
unsigned NumVTableThunks) {
|
2011-07-23 14:55:15 +04:00
|
|
|
SmallVector<llvm::Constant *, 64> Inits;
|
2010-03-25 18:26:28 +03:00
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
2010-03-25 18:26:28 +03:00
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *PtrDiffTy =
|
2010-03-25 19:49:53 +03:00
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
QualType ClassType = CGM.getContext().getTagDeclType(RD);
|
|
|
|
llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
|
|
|
|
|
|
|
|
unsigned NextVTableThunkIndex = 0;
|
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
llvm::Constant* PureVirtualFn = 0;
|
|
|
|
|
2010-03-25 18:26:28 +03:00
|
|
|
for (unsigned I = 0; I != NumComponents; ++I) {
|
2011-09-26 05:56:50 +04:00
|
|
|
VTableComponent Component = Components[I];
|
2010-03-25 19:49:53 +03:00
|
|
|
|
|
|
|
llvm::Constant *Init = 0;
|
|
|
|
|
|
|
|
switch (Component.getKind()) {
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_VCallOffset:
|
2011-04-02 05:14:48 +04:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy,
|
|
|
|
Component.getVCallOffset().getQuantity());
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_VBaseOffset:
|
2011-04-02 05:14:48 +04:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy,
|
|
|
|
Component.getVBaseOffset().getQuantity());
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_OffsetToTop:
|
2011-04-02 05:14:48 +04:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy,
|
|
|
|
Component.getOffsetToTop().getQuantity());
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_RTTI:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
2010-03-25 19:49:53 +03:00
|
|
|
GlobalDecl GD;
|
|
|
|
|
|
|
|
// Get the right global decl.
|
|
|
|
switch (Component.getKind()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected vtable component kind");
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
GD = Component.getFunctionDecl();
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_DeletingDtorPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
|
|
|
|
// We have a pure virtual member function.
|
|
|
|
if (!PureVirtualFn) {
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *Ty =
|
2010-03-29 09:40:50 +04:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
PureVirtualFn =
|
|
|
|
CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
|
|
|
|
PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
|
|
|
|
Int8PtrTy);
|
|
|
|
}
|
2010-03-29 05:04:16 +04:00
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
Init = PureVirtualFn;
|
2010-03-25 19:49:53 +03:00
|
|
|
} else {
|
2010-03-29 09:40:50 +04:00
|
|
|
// Check if we should use a thunk.
|
2011-09-26 05:56:50 +04:00
|
|
|
if (NextVTableThunkIndex < NumVTableThunks &&
|
2010-03-29 09:40:50 +04:00
|
|
|
VTableThunks[NextVTableThunkIndex].first == I) {
|
|
|
|
const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
|
|
|
|
|
2011-02-06 20:15:43 +03:00
|
|
|
Init = CGM.GetAddrOfThunk(GD, Thunk);
|
2011-02-06 21:31:40 +03:00
|
|
|
MaybeEmitThunkAvailableExternally(GD, Thunk);
|
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
NextVTableThunkIndex++;
|
|
|
|
} else {
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
|
2010-03-25 19:49:53 +03:00
|
|
|
|
2011-02-05 07:35:53 +03:00
|
|
|
Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
2010-03-29 09:40:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
|
2010-03-25 19:49:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_UnusedFunctionPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
|
|
|
|
break;
|
|
|
|
};
|
2010-03-25 18:26:28 +03:00
|
|
|
|
|
|
|
Inits.push_back(Init);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
|
2011-06-22 13:24:39 +04:00
|
|
|
return llvm::ConstantArray::get(ArrayType, Inits);
|
2010-03-25 18:26:28 +03:00
|
|
|
}
|
|
|
|
|
2010-03-30 07:35:35 +04:00
|
|
|
llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
|
2011-09-26 05:56:36 +04:00
|
|
|
llvm::GlobalVariable *&VTable = VTables[RD];
|
|
|
|
if (VTable)
|
|
|
|
return VTable;
|
|
|
|
|
|
|
|
// We may need to generate a definition for this vtable.
|
|
|
|
if (ShouldEmitVTableInThisTU(RD))
|
|
|
|
CGM.DeferredVTables.push_back(RD);
|
|
|
|
|
2010-03-24 06:57:14 +03:00
|
|
|
llvm::SmallString<256> OutName;
|
2011-02-11 05:52:17 +03:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
|
|
|
CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
|
|
|
|
Out.flush();
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef Name = OutName.str();
|
2010-03-24 06:57:14 +03:00
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
2010-03-24 19:42:11 +03:00
|
|
|
llvm::ArrayType *ArrayType =
|
2011-09-26 05:56:50 +04:00
|
|
|
llvm::ArrayType::get(Int8PtrTy,
|
|
|
|
VTContext.getVTableLayout(RD).getNumVTableComponents());
|
2010-03-29 07:38:52 +04:00
|
|
|
|
2011-09-26 05:56:36 +04:00
|
|
|
VTable =
|
2011-01-29 21:25:07 +03:00
|
|
|
CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
|
|
|
|
llvm::GlobalValue::ExternalLinkage);
|
2011-09-26 05:56:36 +04:00
|
|
|
VTable->setUnnamedAddr(true);
|
|
|
|
return VTable;
|
2009-11-10 10:44:33 +03:00
|
|
|
}
|
2009-11-11 23:26:26 +03:00
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
void
|
|
|
|
CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
|
|
|
const CXXRecordDecl *RD) {
|
2011-09-26 05:56:50 +04:00
|
|
|
const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
|
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
// Create and set the initializer.
|
|
|
|
llvm::Constant *Init =
|
2011-09-26 05:56:50 +04:00
|
|
|
CreateVTableInitializer(RD,
|
|
|
|
VTLayout.vtable_component_begin(),
|
|
|
|
VTLayout.getNumVTableComponents(),
|
|
|
|
VTLayout.vtable_thunk_begin(),
|
|
|
|
VTLayout.getNumVTableThunks());
|
2010-03-29 07:38:52 +04:00
|
|
|
VTable->setInitializer(Init);
|
2010-03-29 09:40:50 +04:00
|
|
|
|
|
|
|
// Set the correct linkage.
|
|
|
|
VTable->setLinkage(Linkage);
|
2010-06-15 03:41:45 +04:00
|
|
|
|
|
|
|
// Set the right visibility.
|
2011-01-29 23:24:48 +03:00
|
|
|
CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
|
2010-03-29 07:38:52 +04:00
|
|
|
}
|
|
|
|
|
2010-03-25 03:35:49 +03:00
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
|
2010-03-26 06:56:54 +03:00
|
|
|
const BaseSubobject &Base,
|
|
|
|
bool BaseIsVirtual,
|
2011-03-27 13:00:25 +04:00
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
2010-03-26 06:56:54 +03:00
|
|
|
VTableAddressPointsMapTy& AddressPoints) {
|
2011-09-26 05:57:04 +04:00
|
|
|
llvm::OwningPtr<VTableLayout> VTLayout(
|
|
|
|
VTContext.createConstructionVTableLayout(Base.getBase(),
|
|
|
|
Base.getBaseOffset(),
|
|
|
|
BaseIsVirtual, RD));
|
2010-03-25 18:26:28 +03:00
|
|
|
|
2010-03-25 19:49:53 +03:00
|
|
|
// Add the address points.
|
2011-09-26 05:57:04 +04:00
|
|
|
AddressPoints = VTLayout->getAddressPoints();
|
2010-03-25 18:26:28 +03:00
|
|
|
|
|
|
|
// Get the mangled construction vtable name.
|
|
|
|
llvm::SmallString<256> OutName;
|
2011-02-11 05:52:17 +03:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2010-08-31 11:33:07 +04:00
|
|
|
CGM.getCXXABI().getMangleContext().
|
2011-03-24 04:21:01 +03:00
|
|
|
mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
|
|
|
|
Out);
|
2011-02-11 05:52:17 +03:00
|
|
|
Out.flush();
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef Name = OutName.str();
|
2010-03-25 18:26:28 +03:00
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
2010-03-25 18:26:28 +03:00
|
|
|
llvm::ArrayType *ArrayType =
|
2011-09-26 05:57:04 +04:00
|
|
|
llvm::ArrayType::get(Int8PtrTy, VTLayout->getNumVTableComponents());
|
2010-03-25 18:26:28 +03:00
|
|
|
|
|
|
|
// Create the variable that will hold the construction vtable.
|
|
|
|
llvm::GlobalVariable *VTable =
|
2011-03-27 13:00:25 +04:00
|
|
|
CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
|
|
|
|
CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
|
|
|
|
|
|
|
|
// V-tables are always unnamed_addr.
|
|
|
|
VTable->setUnnamedAddr(true);
|
2010-03-25 18:26:28 +03:00
|
|
|
|
|
|
|
// Create and set the initializer.
|
|
|
|
llvm::Constant *Init =
|
|
|
|
CreateVTableInitializer(Base.getBase(),
|
2011-09-26 05:57:04 +04:00
|
|
|
VTLayout->vtable_component_begin(),
|
|
|
|
VTLayout->getNumVTableComponents(),
|
|
|
|
VTLayout->vtable_thunk_begin(),
|
|
|
|
VTLayout->getNumVTableThunks());
|
2010-03-25 18:26:28 +03:00
|
|
|
VTable->setInitializer(Init);
|
|
|
|
|
2010-03-25 03:35:49 +03:00
|
|
|
return VTable;
|
|
|
|
}
|
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
void
|
|
|
|
CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
|
|
|
|
const CXXRecordDecl *RD) {
|
2011-09-26 05:56:36 +04:00
|
|
|
llvm::GlobalVariable *VTable = GetAddrOfVTable(RD);
|
|
|
|
if (VTable->hasInitializer())
|
2010-03-29 07:38:52 +04:00
|
|
|
return;
|
|
|
|
|
2010-03-30 07:35:35 +04:00
|
|
|
EmitVTableDefinition(VTable, Linkage, RD);
|
|
|
|
|
2011-01-29 22:16:51 +03:00
|
|
|
if (RD->getNumVBases()) {
|
|
|
|
llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
|
|
|
|
EmitVTTDefinition(VTT, Linkage, RD);
|
|
|
|
}
|
2010-04-08 19:52:03 +04:00
|
|
|
|
|
|
|
// If this is the magic class __cxxabiv1::__fundamental_type_info,
|
|
|
|
// we will emit the typeinfo for the fundamental types. This is the
|
|
|
|
// same behaviour as GCC.
|
|
|
|
const DeclContext *DC = RD->getDeclContext();
|
|
|
|
if (RD->getIdentifier() &&
|
|
|
|
RD->getIdentifier()->isStr("__fundamental_type_info") &&
|
|
|
|
isa<NamespaceDecl>(DC) &&
|
|
|
|
cast<NamespaceDecl>(DC)->getIdentifier() &&
|
|
|
|
cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
|
|
|
|
DC->getParent()->isTranslationUnit())
|
|
|
|
CGM.EmitFundamentalRTTIDescriptors();
|
2010-03-29 07:38:52 +04:00
|
|
|
}
|