2010-04-18 04:15:18 +08:00
|
|
|
//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
|
2009-10-12 06:13:54 +08: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 "CodeGenFunction.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.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"
|
2013-10-31 05:53:58 +08:00
|
|
|
#include "clang/CodeGen/CGFunctionInfo.h"
|
2010-08-06 04:39:18 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2009-11-27 03:32:45 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2010-02-28 00:18:19 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2010-02-13 18:38:52 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-02-11 16:02:13 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-05-07 01:27:27 +08:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2010-03-18 04:06:32 +08:00
|
|
|
#include <algorithm>
|
2009-11-13 13:46:16 +08:00
|
|
|
#include <cstdio>
|
2009-10-12 06:13:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2014-02-06 01:27:08 +08:00
|
|
|
CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
|
|
|
|
: CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
|
2011-09-26 09:56:30 +08:00
|
|
|
|
2010-03-24 01:17:29 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
|
2011-02-07 01:15:43 +08:00
|
|
|
const ThunkInfo &Thunk) {
|
2010-03-24 01:17:29 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
// Compute the mangled name.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Name;
|
2011-02-11 10:52:17 +08:00
|
|
|
llvm::raw_svector_ostream Out(Name);
|
2010-03-24 01:17:29 +08:00
|
|
|
if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
|
2010-08-31 15:33:07 +08:00
|
|
|
getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
|
2011-02-11 10:52:17 +08:00
|
|
|
Thunk.This, Out);
|
2010-03-24 01:17:29 +08:00
|
|
|
else
|
2011-02-11 10:52:17 +08:00
|
|
|
getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
|
|
|
|
Out.flush();
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
|
2013-12-09 12:29:47 +08:00
|
|
|
return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true,
|
|
|
|
/*DontDefer*/ true);
|
2010-03-24 01:17:29 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 07:46:35 +08:00
|
|
|
static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
|
|
|
|
const ThunkInfo &Thunk, llvm::Function *Fn) {
|
2011-01-30 03:39:23 +08:00
|
|
|
CGM.setGlobalVisibility(Fn, MD);
|
2010-08-05 07:46:35 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 15:12:35 +08: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-07 01:27:27 +08: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);
|
|
|
|
}
|
2013-10-30 19:55:43 +08:00
|
|
|
|
|
|
|
ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue,
|
|
|
|
Thunk.Return);
|
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
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>();
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResultType = FPT->getReturnType();
|
2011-05-07 01:27:27 +08:00
|
|
|
|
|
|
|
// Get the original function
|
2012-02-17 11:33:10 +08:00
|
|
|
assert(FnInfo.isVariadic());
|
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
|
2011-05-07 01:27:27 +08:00
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
|
|
|
llvm::Function *BaseFn = cast<llvm::Function>(Callee);
|
|
|
|
|
|
|
|
// Clone to thunk.
|
2012-09-19 21:13:52 +08:00
|
|
|
llvm::ValueToValueMapTy VMap;
|
|
|
|
llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap,
|
|
|
|
/*ModuleLevelChanges=*/false);
|
2011-05-07 01:27:27 +08:00
|
|
|
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);
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *AdjustedThisPtr =
|
|
|
|
CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
|
2011-05-07 01:27:27 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
|
|
|
|
const CGFunctionInfo &FnInfo) {
|
|
|
|
assert(!CurGD.getDecl() && "CurGD was already set!");
|
|
|
|
CurGD = GD;
|
|
|
|
|
|
|
|
// Build FunctionArgs.
|
2010-03-24 08:39:18 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
QualType ThisType = MD->getThisType(getContext());
|
2013-11-16 01:24:45 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
2013-07-01 04:40:16 +08:00
|
|
|
QualType ResultType =
|
2014-01-26 00:55:45 +08:00
|
|
|
CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getReturnType();
|
2010-03-24 08:39:18 +08:00
|
|
|
FunctionArgList FunctionArgs;
|
|
|
|
|
|
|
|
// Create the implicit 'this' parameter declaration.
|
2013-12-18 03:46:40 +08:00
|
|
|
CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
|
|
|
// Add the rest of the parameters.
|
|
|
|
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
|
2013-11-16 01:24:45 +08:00
|
|
|
E = MD->param_end();
|
|
|
|
I != E; ++I)
|
|
|
|
FunctionArgs.push_back(*I);
|
2012-10-25 18:18:50 +08:00
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Start defining the function.
|
2011-03-09 12:27:21 +08:00
|
|
|
StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
|
2014-04-11 07:21:53 +08:00
|
|
|
MD->getLocation(), SourceLocation());
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
|
2012-02-11 10:57:39 +08:00
|
|
|
CXXThisValue = CXXABIThisValue;
|
2013-11-16 01:24:45 +08:00
|
|
|
}
|
2010-08-31 15:33:07 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
void CodeGenFunction::EmitCallAndReturnForThunk(GlobalDecl GD,
|
|
|
|
llvm::Value *Callee,
|
|
|
|
const ThunkInfo *Thunk) {
|
|
|
|
assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
|
|
|
|
"Please use a new CGF for this thunk");
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
// Adjust the 'this' pointer if necessary
|
|
|
|
llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment(
|
|
|
|
*this, LoadCXXThis(), Thunk->This)
|
|
|
|
: LoadCXXThis();
|
2013-10-30 19:55:43 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Start building CallArgs.
|
2010-03-24 08:39:18 +08:00
|
|
|
CallArgList CallArgs;
|
2013-11-16 01:24:45 +08:00
|
|
|
QualType ThisType = MD->getThisType(getContext());
|
2011-05-03 01:57:46 +08:00
|
|
|
CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs);
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Add the rest of the arguments.
|
2010-03-24 08:39:18 +08:00
|
|
|
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
|
2013-11-16 01:24:45 +08:00
|
|
|
E = MD->param_end(); I != E; ++I)
|
|
|
|
EmitDelegateCallArg(CallArgs, *I, (*I)->getLocStart());
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
#ifndef NDEBUG
|
2012-07-07 14:41:13 +08:00
|
|
|
const CGFunctionInfo &CallFnInfo =
|
|
|
|
CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
|
2012-02-17 11:33:10 +08:00
|
|
|
RequiredArgs::forPrototypePlus(FPT, 1));
|
2013-11-16 01:24:45 +08:00
|
|
|
assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
|
|
|
|
CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
|
|
|
|
CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
|
2012-07-07 14:41:13 +08:00
|
|
|
assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
|
|
|
|
similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
|
2013-11-16 01:24:45 +08:00
|
|
|
CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
|
|
|
|
assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
|
|
|
|
for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
|
2011-03-09 15:12:35 +08:00
|
|
|
assert(similar(CallFnInfo.arg_begin()[i].info,
|
|
|
|
CallFnInfo.arg_begin()[i].type,
|
2013-11-16 01:24:45 +08:00
|
|
|
CurFnInfo->arg_begin()[i].info,
|
|
|
|
CurFnInfo->arg_begin()[i].type));
|
2011-03-09 12:27:21 +08:00
|
|
|
#endif
|
2013-11-16 01:24:45 +08:00
|
|
|
|
2010-05-20 13:54:35 +08:00
|
|
|
// Determine whether we have a return value slot to use.
|
2013-11-16 01:24:45 +08:00
|
|
|
QualType ResultType =
|
2014-01-26 00:55:45 +08:00
|
|
|
CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getReturnType();
|
2010-05-20 13:54:35 +08:00
|
|
|
ReturnValueSlot Slot;
|
|
|
|
if (!ResultType->isVoidType() &&
|
2013-11-16 01:24:45 +08:00
|
|
|
CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
|
2013-03-08 05:37:08 +08:00
|
|
|
!hasScalarEvaluationKind(CurFnInfo->getReturnType()))
|
2010-05-20 13:54:35 +08:00
|
|
|
Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
|
|
|
|
|
2010-03-24 08:39:18 +08:00
|
|
|
// Now emit our call.
|
2013-11-16 01:24:45 +08:00
|
|
|
RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Consider return adjustment if we have ThunkInfo.
|
|
|
|
if (Thunk && !Thunk->Return.isEmpty())
|
|
|
|
RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Emit return.
|
2010-05-20 13:54:35 +08:00
|
|
|
if (!ResultType->isVoidType() && Slot.isNull())
|
2011-02-08 16:22:06 +08:00
|
|
|
CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2012-07-31 08:33:55 +08:00
|
|
|
// Disable the final ARC autorelease.
|
|
|
|
AutoreleaseResult = false;
|
|
|
|
|
2010-03-24 08:39:18 +08:00
|
|
|
FinishFunction();
|
2013-11-16 01:24:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
|
|
|
|
const CGFunctionInfo &FnInfo,
|
|
|
|
GlobalDecl GD, const ThunkInfo &Thunk) {
|
|
|
|
StartThunk(Fn, GD, FnInfo);
|
|
|
|
|
|
|
|
// Get our callee.
|
|
|
|
llvm::Type *Ty =
|
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
|
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
|
|
|
|
|
|
|
// Make the call and return the result.
|
|
|
|
EmitCallAndReturnForThunk(GD, Callee, &Thunk);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
|
|
|
// Set the right linkage.
|
2013-06-06 01:49:37 +08:00
|
|
|
CGM.setFunctionLinkage(GD, Fn);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
|
|
|
// Set the right visibility.
|
2013-11-16 01:24:45 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2010-08-05 07:46:35 +08:00
|
|
|
setThunkVisibility(CGM, MD, Thunk, Fn);
|
2010-03-24 08:39:18 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
|
|
|
|
bool ForVTable) {
|
2012-02-17 11:33:10 +08:00
|
|
|
const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
|
2011-03-09 12:27:21 +08:00
|
|
|
|
|
|
|
// FIXME: re-use FnInfo in this computation.
|
2014-05-08 23:44:45 +08:00
|
|
|
llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
|
|
|
|
llvm::GlobalValue *Entry;
|
|
|
|
|
2010-03-24 02:18:41 +08:00
|
|
|
// Strip off a bitcast if we got one back.
|
2014-05-08 23:44:45 +08:00
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) {
|
2010-03-24 02:18:41 +08:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast);
|
2014-05-08 23:44:45 +08:00
|
|
|
Entry = cast<llvm::GlobalValue>(CE->getOperand(0));
|
|
|
|
} else {
|
|
|
|
Entry = cast<llvm::GlobalValue>(C);
|
2010-03-24 02:18:41 +08:00
|
|
|
}
|
2014-05-08 23:44:45 +08:00
|
|
|
|
2010-03-24 02:18:41 +08:00
|
|
|
// There's already a declaration with the same name, check if it has the same
|
|
|
|
// type or if we need to replace it.
|
2014-05-08 23:44:45 +08:00
|
|
|
if (Entry->getType()->getElementType() !=
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getTypes().GetFunctionTypeForVTable(GD)) {
|
2014-05-08 23:44:45 +08:00
|
|
|
llvm::GlobalValue *OldThunkFn = Entry;
|
|
|
|
|
2010-03-24 02:18:41 +08: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 18:55:15 +08:00
|
|
|
OldThunkFn->setName(StringRef());
|
2014-05-08 23:44:45 +08:00
|
|
|
Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk));
|
2010-03-24 02:18:41 +08:00
|
|
|
|
|
|
|
// If needed, replace the old thunk with a bitcast.
|
|
|
|
if (!OldThunkFn->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2010-03-24 08:35:44 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
|
2010-03-24 02:18:41 +08:00
|
|
|
OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old thunk.
|
|
|
|
OldThunkFn->eraseFromParent();
|
|
|
|
}
|
2010-03-24 08:39:18 +08:00
|
|
|
|
|
|
|
llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
|
2013-10-09 17:23:58 +08:00
|
|
|
bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
|
|
|
|
bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
|
2011-02-07 02:31:40 +08:00
|
|
|
|
|
|
|
if (!ThunkFn->isDeclaration()) {
|
2013-10-09 17:23:58 +08:00
|
|
|
if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
|
2011-02-07 02:31:40 +08:00
|
|
|
// There is already a thunk emitted for this function, do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-07 04:09:44 +08:00
|
|
|
// Change the linkage.
|
2013-06-06 01:49:37 +08:00
|
|
|
CGM.setFunctionLinkage(GD, ThunkFn);
|
2011-02-07 04:09:44 +08:00
|
|
|
return;
|
2011-02-07 02:31:40 +08:00
|
|
|
}
|
|
|
|
|
2012-09-22 04:39:32 +08:00
|
|
|
CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
|
|
|
|
|
2011-05-07 01:27:27 +08: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.
|
2013-12-08 00:12:52 +08:00
|
|
|
if (!UseAvailableExternallyLinkage) {
|
2011-05-07 01:27:27 +08:00
|
|
|
CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
|
2013-12-08 00:12:52 +08:00
|
|
|
CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable);
|
|
|
|
}
|
2011-05-07 01:27:27 +08:00
|
|
|
} else {
|
|
|
|
// Normal thunk body generation.
|
|
|
|
CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
|
2013-12-08 00:12:52 +08:00
|
|
|
CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable);
|
2011-05-07 01:27:27 +08:00
|
|
|
}
|
2011-02-07 02:31:40 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
|
|
|
|
const ThunkInfo &Thunk) {
|
|
|
|
// If the ABI has key functions, only the TU with the key function should emit
|
|
|
|
// the thunk. However, we can allow inlining of thunks if we emit them with
|
|
|
|
// available_externally linkage together with vtables when optimizations are
|
|
|
|
// enabled.
|
|
|
|
if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
|
|
|
|
!CGM.getCodeGenOpts().OptimizationLevel)
|
2011-02-07 02:31:40 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We can't emit thunks for member functions with incomplete types.
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2011-07-10 08:18:59 +08:00
|
|
|
if (!CGM.getTypes().isFuncTypeConvertible(
|
2013-10-12 04:46:27 +08:00
|
|
|
MD->getType()->castAs<FunctionType>()))
|
2011-02-07 02:31:40 +08:00
|
|
|
return;
|
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
emitThunk(GD, Thunk, /*ForVTable=*/true);
|
2010-03-24 00:36:50 +08:00
|
|
|
}
|
|
|
|
|
2010-03-23 12:59:02 +08:00
|
|
|
void CodeGenVTables::EmitThunks(GlobalDecl GD)
|
|
|
|
{
|
2010-03-24 00:36:50 +08: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;
|
|
|
|
|
2013-12-21 07:58:52 +08:00
|
|
|
const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
|
|
|
|
VTContext->getThunkInfo(GD);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2011-09-26 09:56:41 +08:00
|
|
|
if (!ThunkInfoVector)
|
2010-03-25 00:42:11 +08:00
|
|
|
return;
|
|
|
|
|
2011-09-26 09:56:41 +08:00
|
|
|
for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
|
2013-10-09 17:23:58 +08:00
|
|
|
emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false);
|
2010-03-23 12:59:02 +08:00
|
|
|
}
|
|
|
|
|
2010-03-25 23:26:28 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
|
2011-09-26 09:56:50 +08:00
|
|
|
const VTableComponent *Components,
|
2010-03-25 23:26:28 +08:00
|
|
|
unsigned NumComponents,
|
2011-09-26 09:56:50 +08:00
|
|
|
const VTableLayout::VTableThunkTy *VTableThunks,
|
|
|
|
unsigned NumVTableThunks) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant *, 64> Inits;
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PtrDiffTy =
|
2010-03-26 00:49:53 +08:00
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
QualType ClassType = CGM.getContext().getTagDeclType(RD);
|
|
|
|
llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
|
|
|
|
|
|
|
|
unsigned NextVTableThunkIndex = 0;
|
|
|
|
|
2012-10-17 06:56:05 +08:00
|
|
|
llvm::Constant *PureVirtualFn = 0, *DeletedVirtualFn = 0;
|
2010-03-29 13:40:50 +08:00
|
|
|
|
2010-03-25 23:26:28 +08:00
|
|
|
for (unsigned I = 0; I != NumComponents; ++I) {
|
2011-09-26 09:56:50 +08:00
|
|
|
VTableComponent Component = Components[I];
|
2010-03-26 00:49:53 +08:00
|
|
|
|
|
|
|
llvm::Constant *Init = 0;
|
|
|
|
|
|
|
|
switch (Component.getKind()) {
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_VCallOffset:
|
2011-04-02 09:14:48 +08:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy,
|
|
|
|
Component.getVCallOffset().getQuantity());
|
2010-03-26 00:49:53 +08:00
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_VBaseOffset:
|
2011-04-02 09:14:48 +08:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy,
|
|
|
|
Component.getVBaseOffset().getQuantity());
|
2010-03-26 00:49:53 +08:00
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_OffsetToTop:
|
2011-04-02 09:14:48 +08:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy,
|
|
|
|
Component.getOffsetToTop().getQuantity());
|
2010-03-26 00:49:53 +08:00
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_RTTI:
|
2010-03-26 00:49:53 +08:00
|
|
|
Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
|
|
|
|
break;
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
2010-03-26 00:49:53 +08:00
|
|
|
GlobalDecl GD;
|
|
|
|
|
|
|
|
// Get the right global decl.
|
|
|
|
switch (Component.getKind()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected vtable component kind");
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
2010-03-26 00:49:53 +08:00
|
|
|
GD = Component.getFunctionDecl();
|
|
|
|
break;
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
2010-03-26 00:49:53 +08:00
|
|
|
GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
|
|
|
|
break;
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_DeletingDtorPointer:
|
2010-03-26 00:49:53 +08:00
|
|
|
GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-03-29 13:40:50 +08:00
|
|
|
if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
|
|
|
|
// We have a pure virtual member function.
|
2012-07-18 03:17:58 +08:00
|
|
|
if (!PureVirtualFn) {
|
2012-09-14 09:19:01 +08:00
|
|
|
llvm::FunctionType *Ty =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
|
|
|
|
StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
|
|
|
|
PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
|
|
|
|
PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
|
2012-07-18 03:17:58 +08:00
|
|
|
CGM.Int8PtrTy);
|
2010-03-29 13:40:50 +08:00
|
|
|
}
|
|
|
|
Init = PureVirtualFn;
|
2012-10-17 06:56:05 +08:00
|
|
|
} else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
|
|
|
|
if (!DeletedVirtualFn) {
|
|
|
|
llvm::FunctionType *Ty =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
|
|
|
|
StringRef DeletedCallName =
|
|
|
|
CGM.getCXXABI().GetDeletedVirtualCallName();
|
|
|
|
DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
|
|
|
|
DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
|
|
|
|
CGM.Int8PtrTy);
|
|
|
|
}
|
|
|
|
Init = DeletedVirtualFn;
|
2010-03-26 00:49:53 +08:00
|
|
|
} else {
|
2010-03-29 13:40:50 +08:00
|
|
|
// Check if we should use a thunk.
|
2011-09-26 09:56:50 +08:00
|
|
|
if (NextVTableThunkIndex < NumVTableThunks &&
|
2010-03-29 13:40:50 +08:00
|
|
|
VTableThunks[NextVTableThunkIndex].first == I) {
|
|
|
|
const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
|
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
maybeEmitThunkForVTable(GD, Thunk);
|
2012-03-21 04:18:13 +08:00
|
|
|
Init = CGM.GetAddrOfThunk(GD, Thunk);
|
2011-02-07 02:31:40 +08:00
|
|
|
|
2010-03-29 13:40:50 +08:00
|
|
|
NextVTableThunkIndex++;
|
|
|
|
} else {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
|
2010-03-26 00:49:53 +08:00
|
|
|
|
2011-02-05 12:35:53 +08:00
|
|
|
Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
2010-03-29 13:40:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
|
2010-03-26 00:49:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-11 03:13:06 +08:00
|
|
|
case VTableComponent::CK_UnusedFunctionPointer:
|
2010-03-26 00:49:53 +08:00
|
|
|
Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
|
|
|
|
break;
|
|
|
|
};
|
2010-03-25 23:26:28 +08:00
|
|
|
|
|
|
|
Inits.push_back(Init);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
|
2011-06-22 17:24:39 +08:00
|
|
|
return llvm::ConstantArray::get(ArrayType, Inits);
|
2010-03-25 23:26:28 +08:00
|
|
|
}
|
|
|
|
|
2010-03-25 08:35:49 +08:00
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
|
2010-03-26 11:56:54 +08:00
|
|
|
const BaseSubobject &Base,
|
|
|
|
bool BaseIsVirtual,
|
2011-03-27 17:00:25 +08:00
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
2010-03-26 11:56:54 +08:00
|
|
|
VTableAddressPointsMapTy& AddressPoints) {
|
2013-08-22 23:23:05 +08:00
|
|
|
if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
|
|
|
|
DI->completeClassData(Base.getBase());
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<VTableLayout> VTLayout(
|
2013-12-21 07:58:52 +08:00
|
|
|
getItaniumVTableContext().createConstructionVTableLayout(
|
2013-11-05 23:54:58 +08:00
|
|
|
Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2010-03-26 00:49:53 +08:00
|
|
|
// Add the address points.
|
2011-09-26 09:57:04 +08:00
|
|
|
AddressPoints = VTLayout->getAddressPoints();
|
2010-03-25 23:26:28 +08:00
|
|
|
|
|
|
|
// Get the mangled construction vtable name.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> OutName;
|
2011-02-11 10:52:17 +08:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2013-10-03 14:26:13 +08:00
|
|
|
cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
|
|
|
|
.mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
|
|
|
|
Base.getBase(), Out);
|
2011-02-11 10:52:17 +08:00
|
|
|
Out.flush();
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name = OutName.str();
|
2010-03-25 23:26:28 +08:00
|
|
|
|
|
|
|
llvm::ArrayType *ArrayType =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2013-02-16 08:51:21 +08:00
|
|
|
// Construction vtable symbols are not part of the Itanium ABI, so we cannot
|
|
|
|
// guarantee that they actually will be available externally. Instead, when
|
|
|
|
// emitting an available_externally VTT, we provide references to an internal
|
|
|
|
// linkage construction vtable. The ABI only requires complete-object vtables
|
|
|
|
// to be the same for all instances of a type, not construction vtables.
|
|
|
|
if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
|
|
|
|
Linkage = llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
2010-03-25 23:26:28 +08:00
|
|
|
// Create the variable that will hold the construction vtable.
|
|
|
|
llvm::GlobalVariable *VTable =
|
2011-03-27 17:00:25 +08:00
|
|
|
CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
|
2014-02-08 08:41:16 +08:00
|
|
|
CGM.setGlobalVisibility(VTable, RD);
|
2011-03-27 17:00:25 +08:00
|
|
|
|
|
|
|
// V-tables are always unnamed_addr.
|
|
|
|
VTable->setUnnamedAddr(true);
|
2010-03-25 23:26:28 +08:00
|
|
|
|
|
|
|
// Create and set the initializer.
|
|
|
|
llvm::Constant *Init =
|
|
|
|
CreateVTableInitializer(Base.getBase(),
|
2011-09-26 09:57:04 +08:00
|
|
|
VTLayout->vtable_component_begin(),
|
|
|
|
VTLayout->getNumVTableComponents(),
|
|
|
|
VTLayout->vtable_thunk_begin(),
|
|
|
|
VTLayout->getNumVTableThunks());
|
2010-03-25 23:26:28 +08:00
|
|
|
VTable->setInitializer(Init);
|
|
|
|
|
2010-03-25 08:35:49 +08:00
|
|
|
return VTable;
|
|
|
|
}
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
/// Compute the required linkage of the v-table for the given class.
|
|
|
|
///
|
|
|
|
/// Note that we only call this at the end of the translation unit.
|
|
|
|
llvm::GlobalVariable::LinkageTypes
|
|
|
|
CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!RD->isExternallyVisible())
|
2013-01-26 06:31:03 +08:00
|
|
|
return llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
|
|
|
// We're at the end of the translation unit, so the current key
|
|
|
|
// function is fully correct.
|
|
|
|
if (const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD)) {
|
|
|
|
// If this class has a key function, use that to determine the
|
|
|
|
// linkage of the vtable.
|
|
|
|
const FunctionDecl *def = 0;
|
|
|
|
if (keyFunction->hasBody(def))
|
|
|
|
keyFunction = cast<CXXMethodDecl>(def);
|
|
|
|
|
|
|
|
switch (keyFunction->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
2013-09-04 05:05:13 +08:00
|
|
|
assert(def && "Should not have been asked to emit this");
|
2013-01-26 06:31:03 +08:00
|
|
|
if (keyFunction->isInlined())
|
|
|
|
return !Context.getLangOpts().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
|
|
|
|
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
return !Context.getLangOpts().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return !Context.getLangOpts().AppleKext ?
|
|
|
|
llvm::GlobalVariable::WeakODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
2013-09-04 05:05:13 +08:00
|
|
|
llvm_unreachable("Should not have been asked to emit this");
|
2013-01-26 06:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -fapple-kext mode does not support weak linkage, so we must use
|
|
|
|
// internal linkage.
|
|
|
|
if (Context.getLangOpts().AppleKext)
|
|
|
|
return llvm::Function::InternalLinkage;
|
|
|
|
|
|
|
|
switch (RD->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
return llvm::GlobalVariable::LinkOnceODRLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
2013-09-04 05:05:13 +08:00
|
|
|
llvm_unreachable("Should not have been asked to emit this");
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateSpecializationKind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a callback from Sema to tell us that it believes that a
|
|
|
|
/// particular v-table is required to be emitted in this translation
|
|
|
|
/// unit.
|
|
|
|
///
|
|
|
|
/// The reason we don't simply trust this callback is because Sema
|
|
|
|
/// will happily report that something is used even when it's used
|
|
|
|
/// only in code that we don't actually have to emit.
|
|
|
|
///
|
|
|
|
/// \param isRequired - if true, the v-table is mandatory, e.g.
|
|
|
|
/// because the translation unit defines the key function
|
|
|
|
void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) {
|
|
|
|
if (!isRequired) return;
|
|
|
|
|
|
|
|
VTables.GenerateClassData(theClass);
|
|
|
|
}
|
|
|
|
|
2010-03-29 11:38:52 +08:00
|
|
|
void
|
2013-01-26 06:31:03 +08:00
|
|
|
CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
|
2013-08-22 23:23:05 +08:00
|
|
|
if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
|
|
|
|
DI->completeClassData(RD);
|
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
if (RD->getNumVBases())
|
2013-09-27 22:48:01 +08:00
|
|
|
CGM.getCXXABI().emitVirtualInheritanceTables(RD);
|
|
|
|
|
|
|
|
CGM.getCXXABI().emitVTableDefinitions(*this, RD);
|
2010-03-29 11:38:52 +08:00
|
|
|
}
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
/// At this point in the translation unit, does it appear that can we
|
|
|
|
/// rely on the vtable being defined elsewhere in the program?
|
|
|
|
///
|
|
|
|
/// The response is really only definitive when called at the end of
|
|
|
|
/// the translation unit.
|
|
|
|
///
|
|
|
|
/// The only semantic restriction here is that the object file should
|
|
|
|
/// not contain a v-table definition when that v-table is defined
|
|
|
|
/// strongly elsewhere. Otherwise, we'd just like to avoid emitting
|
|
|
|
/// v-tables when unnecessary.
|
|
|
|
bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
|
2013-12-05 12:47:09 +08:00
|
|
|
assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
// If we have an explicit instantiation declaration (and not a
|
|
|
|
// definition), the v-table is defined elsewhere.
|
|
|
|
TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
|
|
|
|
if (TSK == TSK_ExplicitInstantiationDeclaration)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, if the class is an instantiated template, the
|
|
|
|
// v-table must be defined here.
|
|
|
|
if (TSK == TSK_ImplicitInstantiation ||
|
|
|
|
TSK == TSK_ExplicitInstantiationDefinition)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, if the class doesn't have a key function (possibly
|
|
|
|
// anymore), the v-table must be defined here.
|
|
|
|
const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
|
|
|
|
if (!keyFunction)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, if we don't have a definition of the key function, the
|
|
|
|
// v-table must be defined somewhere else.
|
|
|
|
return !keyFunction->hasBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given that we're currently at the end of the translation unit, and
|
|
|
|
/// we've emitted a reference to the v-table for this class, should
|
|
|
|
/// we define that v-table?
|
|
|
|
static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
return !CGM.getVTables().isVTableExternal(RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given that at some point we emitted a reference to one or more
|
|
|
|
/// v-tables, and that we are now at the end of the translation unit,
|
|
|
|
/// decide whether we should emit them.
|
|
|
|
void CodeGenModule::EmitDeferredVTables() {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Remember the size of DeferredVTables, because we're going to assume
|
|
|
|
// that this entire operation doesn't modify it.
|
|
|
|
size_t savedSize = DeferredVTables.size();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator;
|
|
|
|
for (const_iterator i = DeferredVTables.begin(),
|
|
|
|
e = DeferredVTables.end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *RD = *i;
|
|
|
|
if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
|
|
|
|
VTables.GenerateClassData(RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(savedSize == DeferredVTables.size() &&
|
|
|
|
"deferred extra v-tables during v-table emission?");
|
|
|
|
DeferredVTables.clear();
|
|
|
|
}
|