2009-11-24 13:51:11 +08:00
|
|
|
//===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
|
2009-09-23 06:53:17 +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 code generation of C++ expressions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-10-01 03:05:55 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2009-09-23 06:53:17 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2010-05-21 05:38:57 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2010-10-01 03:05:55 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2010-07-21 04:19:24 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-09-23 06:53:17 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
|
|
|
|
llvm::Value *Callee,
|
|
|
|
ReturnValueSlot ReturnValue,
|
|
|
|
llvm::Value *This,
|
2010-01-02 09:01:18 +08:00
|
|
|
llvm::Value *VTT,
|
2010-01-02 04:29:01 +08:00
|
|
|
CallExpr::const_arg_iterator ArgBeg,
|
|
|
|
CallExpr::const_arg_iterator ArgEnd) {
|
|
|
|
assert(MD->isInstance() &&
|
|
|
|
"Trying to emit a member call expr on a static method!");
|
|
|
|
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
|
|
|
|
// Push the this ptr.
|
|
|
|
Args.push_back(std::make_pair(RValue::get(This),
|
|
|
|
MD->getThisType(getContext())));
|
|
|
|
|
2010-01-02 09:01:18 +08:00
|
|
|
// If there is a VTT parameter, emit it.
|
|
|
|
if (VTT) {
|
|
|
|
QualType T = getContext().getPointerType(getContext().VoidPtrTy);
|
|
|
|
Args.push_back(std::make_pair(RValue::get(VTT), T));
|
|
|
|
}
|
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
// And the rest of the call args
|
|
|
|
EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
|
|
|
|
|
2010-02-06 05:31:56 +08:00
|
|
|
QualType ResultType = FPT->getResultType();
|
2011-03-03 05:36:49 +08:00
|
|
|
return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
|
|
|
|
FPT->getExtInfo()),
|
2010-03-31 04:24:48 +08:00
|
|
|
Callee, ReturnValue, Args, MD);
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2011-01-29 11:52:01 +08:00
|
|
|
static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
|
2011-01-29 13:04:11 +08:00
|
|
|
const Expr *E = Base;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
E = E->IgnoreParens();
|
|
|
|
if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
|
|
|
|
if (CE->getCastKind() == CK_DerivedToBase ||
|
|
|
|
CE->getCastKind() == CK_UncheckedDerivedToBase ||
|
|
|
|
CE->getCastKind() == CK_NoOp) {
|
|
|
|
E = CE->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType DerivedType = E->getType();
|
2011-01-29 11:52:01 +08:00
|
|
|
if (const PointerType *PTy = DerivedType->getAs<PointerType>())
|
|
|
|
DerivedType = PTy->getPointeeType();
|
|
|
|
|
|
|
|
return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
|
|
|
|
}
|
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
|
|
|
|
/// expr can be devirtualized.
|
2011-01-21 09:04:41 +08:00
|
|
|
static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
|
|
|
|
const Expr *Base,
|
2010-10-27 21:28:46 +08:00
|
|
|
const CXXMethodDecl *MD) {
|
|
|
|
|
2011-01-29 11:52:01 +08:00
|
|
|
// When building with -fapple-kext, all calls must go through the vtable since
|
|
|
|
// the kernel linker can do runtime patching of vtables.
|
2011-01-21 09:04:41 +08:00
|
|
|
if (Context.getLangOptions().AppleKext)
|
|
|
|
return false;
|
|
|
|
|
2011-01-29 11:52:01 +08:00
|
|
|
// If the most derived class is marked final, we know that no subclass can
|
|
|
|
// override this member function and so we can devirtualize it. For example:
|
|
|
|
//
|
|
|
|
// struct A { virtual void f(); }
|
|
|
|
// struct B final : A { };
|
|
|
|
//
|
|
|
|
// void f(B *b) {
|
|
|
|
// b->f();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base);
|
|
|
|
if (MostDerivedClassDecl->hasAttr<FinalAttr>())
|
|
|
|
return true;
|
|
|
|
|
2011-01-24 05:07:30 +08:00
|
|
|
// If the member function is marked 'final', we know that it can't be
|
2010-10-27 21:34:43 +08:00
|
|
|
// overridden and can therefore devirtualize it.
|
2011-01-25 00:26:15 +08:00
|
|
|
if (MD->hasAttr<FinalAttr>())
|
2010-10-27 21:28:46 +08:00
|
|
|
return true;
|
2010-10-27 21:34:43 +08:00
|
|
|
|
2011-01-24 05:07:30 +08:00
|
|
|
// Similarly, if the class itself is marked 'final' it can't be overridden
|
|
|
|
// and we can therefore devirtualize the member function call.
|
2011-01-25 00:26:15 +08:00
|
|
|
if (MD->getParent()->hasAttr<FinalAttr>())
|
2010-10-27 21:34:43 +08:00
|
|
|
return true;
|
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
|
|
|
|
// This is a record decl. We know the type and can devirtualize it.
|
|
|
|
return VD->getType()->isRecordType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can always devirtualize calls on temporary object expressions.
|
2010-02-01 04:58:15 +08:00
|
|
|
if (isa<CXXConstructExpr>(Base))
|
2010-01-02 04:29:01 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// And calls on bound temporaries.
|
|
|
|
if (isa<CXXBindTemporaryExpr>(Base))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if this is a call expr that returns a record type.
|
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
|
|
|
|
return CE->getCallReturnType()->isRecordType();
|
2010-10-27 21:28:46 +08:00
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
// We can't devirtualize the call.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-18 13:04:39 +08:00
|
|
|
// Note: This function also emit constructor calls to support a MSVC
|
|
|
|
// extensions allowing explicit constructor function call.
|
2010-01-02 04:29:01 +08:00
|
|
|
RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
|
|
|
|
ReturnValueSlot ReturnValue) {
|
|
|
|
if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens()))
|
|
|
|
return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
|
|
|
|
|
|
|
|
const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()->IgnoreParens());
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
|
|
|
|
|
2010-10-01 03:05:55 +08:00
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
2010-10-23 02:56:27 +08:00
|
|
|
if (DI && CGM.getCodeGenOpts().LimitDebugInfo
|
|
|
|
&& !isa<CallExpr>(ME->getBase())) {
|
2010-10-01 03:05:55 +08:00
|
|
|
QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType();
|
|
|
|
if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) {
|
|
|
|
DI->getOrCreateRecordType(PTy->getPointeeType(),
|
|
|
|
MD->getParent()->getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
if (MD->isStatic()) {
|
|
|
|
// The method is static, emit it as we would a regular call.
|
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
|
|
|
|
return EmitCall(getContext().getPointerType(MD->getType()), Callee,
|
|
|
|
ReturnValue, CE->arg_begin(), CE->arg_end());
|
|
|
|
}
|
|
|
|
|
2010-09-03 09:26:39 +08:00
|
|
|
// Compute the object pointer.
|
2010-01-02 04:29:01 +08:00
|
|
|
llvm::Value *This;
|
|
|
|
if (ME->isArrow())
|
|
|
|
This = EmitScalarExpr(ME->getBase());
|
2010-12-04 16:14:53 +08:00
|
|
|
else
|
|
|
|
This = EmitLValue(ME->getBase()).getAddress();
|
2010-01-02 04:29:01 +08:00
|
|
|
|
2010-09-03 09:26:39 +08:00
|
|
|
if (MD->isTrivial()) {
|
|
|
|
if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
|
2011-01-18 13:04:39 +08:00
|
|
|
if (isa<CXXConstructorDecl>(MD) &&
|
|
|
|
cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
|
|
|
|
return RValue::get(0);
|
|
|
|
|
|
|
|
if (MD->isCopyAssignmentOperator()) {
|
|
|
|
// We don't like to generate the trivial copy assignment operator when
|
|
|
|
// it isn't necessary; just produce the proper effect here.
|
|
|
|
llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
|
|
|
|
EmitAggregateCopy(This, RHS, CE->getType());
|
|
|
|
return RValue::get(This);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CXXConstructorDecl>(MD) &&
|
|
|
|
cast<CXXConstructorDecl>(MD)->isCopyConstructor()) {
|
|
|
|
llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
|
|
|
|
EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS,
|
|
|
|
CE->arg_begin(), CE->arg_end());
|
|
|
|
return RValue::get(This);
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown trivial member function");
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2010-09-03 09:26:39 +08:00
|
|
|
// Compute the function type we're calling.
|
2011-01-18 13:04:39 +08:00
|
|
|
const CGFunctionInfo *FInfo = 0;
|
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
|
|
|
|
Dtor_Complete);
|
|
|
|
else if (isa<CXXConstructorDecl>(MD))
|
|
|
|
FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD),
|
|
|
|
Ctor_Complete);
|
|
|
|
else
|
|
|
|
FInfo = &CGM.getTypes().getFunctionInfo(MD);
|
2010-09-03 09:26:39 +08:00
|
|
|
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
const llvm::Type *Ty
|
2011-01-18 13:04:39 +08:00
|
|
|
= CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
|
2010-09-03 09:26:39 +08:00
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
// C++ [class.virtual]p12:
|
|
|
|
// Explicit qualification with the scope operator (5.1) suppresses the
|
|
|
|
// virtual call mechanism.
|
|
|
|
//
|
|
|
|
// We also don't emit a virtual call if the base expression has a record type
|
|
|
|
// because then we know what the type is.
|
2011-01-21 01:19:02 +08:00
|
|
|
bool UseVirtualCall;
|
2011-01-21 09:04:41 +08:00
|
|
|
UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
|
|
|
|
&& !canDevirtualizeMemberFunctionCalls(getContext(),
|
|
|
|
ME->getBase(), MD);
|
2010-01-02 04:29:01 +08:00
|
|
|
llvm::Value *Callee;
|
2010-09-03 09:26:39 +08:00
|
|
|
if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
if (UseVirtualCall) {
|
|
|
|
Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
|
2010-01-02 04:29:01 +08:00
|
|
|
} else {
|
2011-02-02 07:22:34 +08:00
|
|
|
if (getContext().getLangOptions().AppleKext &&
|
|
|
|
MD->isVirtual() &&
|
|
|
|
ME->hasQualifier())
|
2011-02-04 03:27:17 +08:00
|
|
|
Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
|
2011-02-02 07:22:34 +08:00
|
|
|
else
|
|
|
|
Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
2011-01-18 13:04:39 +08:00
|
|
|
} else if (const CXXConstructorDecl *Ctor =
|
|
|
|
dyn_cast<CXXConstructorDecl>(MD)) {
|
|
|
|
Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
|
2010-09-03 09:26:39 +08:00
|
|
|
} else if (UseVirtualCall) {
|
2011-01-21 01:19:02 +08:00
|
|
|
Callee = BuildVirtualCall(MD, This, Ty);
|
2010-01-02 04:29:01 +08:00
|
|
|
} else {
|
2011-01-21 09:04:41 +08:00
|
|
|
if (getContext().getLangOptions().AppleKext &&
|
2011-01-29 07:42:29 +08:00
|
|
|
MD->isVirtual() &&
|
2011-01-21 09:04:41 +08:00
|
|
|
ME->hasQualifier())
|
2011-02-04 03:27:17 +08:00
|
|
|
Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
|
2011-01-21 09:04:41 +08:00
|
|
|
else
|
|
|
|
Callee = CGM.GetAddrOfFunction(MD, Ty);
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2010-01-02 09:01:18 +08:00
|
|
|
return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
|
2010-01-02 04:29:01 +08:00
|
|
|
CE->arg_begin(), CE->arg_end());
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue
|
|
|
|
CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
|
|
|
|
ReturnValueSlot ReturnValue) {
|
|
|
|
const BinaryOperator *BO =
|
|
|
|
cast<BinaryOperator>(E->getCallee()->IgnoreParens());
|
|
|
|
const Expr *BaseExpr = BO->getLHS();
|
|
|
|
const Expr *MemFnExpr = BO->getRHS();
|
|
|
|
|
|
|
|
const MemberPointerType *MPT =
|
|
|
|
MemFnExpr->getType()->getAs<MemberPointerType>();
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
const FunctionProtoType *FPT =
|
|
|
|
MPT->getPointeeType()->getAs<FunctionProtoType>();
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Get the member function pointer.
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
|
2010-01-02 04:29:01 +08:00
|
|
|
|
|
|
|
// Emit the 'this' pointer.
|
|
|
|
llvm::Value *This;
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (BO->getOpcode() == BO_PtrMemI)
|
2010-01-02 04:29:01 +08:00
|
|
|
This = EmitScalarExpr(BaseExpr);
|
|
|
|
else
|
|
|
|
This = EmitLValue(BaseExpr).getAddress();
|
|
|
|
|
2010-08-22 08:05:51 +08:00
|
|
|
// Ask the ABI to load the callee. Note that This is modified.
|
|
|
|
llvm::Value *Callee =
|
2011-02-08 16:22:06 +08:00
|
|
|
CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT);
|
2010-01-02 04:29:01 +08:00
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
|
|
|
|
QualType ThisType =
|
|
|
|
getContext().getPointerType(getContext().getTagDeclType(RD));
|
|
|
|
|
|
|
|
// Push the this ptr.
|
|
|
|
Args.push_back(std::make_pair(RValue::get(This), ThisType));
|
|
|
|
|
|
|
|
// And the rest of the call args
|
|
|
|
EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
|
2010-02-06 05:31:56 +08:00
|
|
|
const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>();
|
2011-03-03 05:36:49 +08:00
|
|
|
return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee,
|
|
|
|
ReturnValue, Args);
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RValue
|
|
|
|
CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
|
|
|
|
const CXXMethodDecl *MD,
|
|
|
|
ReturnValueSlot ReturnValue) {
|
|
|
|
assert(MD->isInstance() &&
|
|
|
|
"Trying to emit a member call expr on a static method!");
|
2010-12-04 16:14:53 +08:00
|
|
|
LValue LV = EmitLValue(E->getArg(0));
|
|
|
|
llvm::Value *This = LV.getAddress();
|
|
|
|
|
2010-09-28 06:37:28 +08:00
|
|
|
if (MD->isCopyAssignmentOperator()) {
|
2010-01-02 04:29:01 +08:00
|
|
|
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
|
|
|
|
if (ClassDecl->hasTrivialCopyAssignment()) {
|
|
|
|
assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
|
|
|
|
"EmitCXXOperatorMemberCallExpr - user declared copy assignment");
|
|
|
|
llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
|
|
|
|
QualType Ty = E->getType();
|
2010-06-16 06:44:06 +08:00
|
|
|
EmitAggregateCopy(This, Src, Ty);
|
2010-01-02 04:29:01 +08:00
|
|
|
return RValue::get(This);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
const llvm::Type *Ty =
|
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
|
|
|
|
FPT->isVariadic());
|
|
|
|
llvm::Value *Callee;
|
2011-01-21 01:19:02 +08:00
|
|
|
if (MD->isVirtual() &&
|
2011-01-21 09:04:41 +08:00
|
|
|
!canDevirtualizeMemberFunctionCalls(getContext(),
|
|
|
|
E->getArg(0), MD))
|
2010-01-02 04:29:01 +08:00
|
|
|
Callee = BuildVirtualCall(MD, This, Ty);
|
|
|
|
else
|
|
|
|
Callee = CGM.GetAddrOfFunction(MD, Ty);
|
|
|
|
|
2010-01-02 09:01:18 +08:00
|
|
|
return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
|
2010-01-02 04:29:01 +08:00
|
|
|
E->arg_begin() + 1, E->arg_end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-15 18:14:12 +08:00
|
|
|
CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
|
|
|
|
AggValueSlot Dest) {
|
|
|
|
assert(!Dest.isIgnored() && "Must have a destination!");
|
2010-01-02 04:29:01 +08:00
|
|
|
const CXXConstructorDecl *CD = E->getConstructor();
|
2010-08-23 00:15:35 +08:00
|
|
|
|
|
|
|
// If we require zero initialization before (or instead of) calling the
|
|
|
|
// constructor, as can be the case with a non-user-provided default
|
|
|
|
// constructor, emit the zero initialization now.
|
|
|
|
if (E->requiresZeroInitialization())
|
2010-09-15 18:14:12 +08:00
|
|
|
EmitNullInitialization(Dest.getAddr(), E->getType());
|
2010-08-23 00:15:35 +08:00
|
|
|
|
|
|
|
// If this is a call to a trivial default constructor, do nothing.
|
|
|
|
if (CD->isTrivial() && CD->isDefaultConstructor())
|
|
|
|
return;
|
|
|
|
|
2010-09-18 08:58:34 +08:00
|
|
|
// Elide the constructor if we're constructing from a temporary.
|
|
|
|
// The temporary check is required because Sema sets this on NRVO
|
|
|
|
// returns.
|
2010-01-02 04:29:01 +08:00
|
|
|
if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
|
2010-09-18 08:58:34 +08:00
|
|
|
assert(getContext().hasSameUnqualifiedType(E->getType(),
|
|
|
|
E->getArg(0)->getType()));
|
2010-09-15 18:14:12 +08:00
|
|
|
if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
|
|
|
|
EmitAggExpr(E->getArg(0), Dest);
|
2010-05-15 08:13:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
2010-08-23 00:15:35 +08:00
|
|
|
|
|
|
|
const ConstantArrayType *Array
|
|
|
|
= getContext().getAsConstantArrayType(E->getType());
|
2010-01-02 04:29:01 +08:00
|
|
|
if (Array) {
|
|
|
|
QualType BaseElementTy = getContext().getBaseElementType(Array);
|
|
|
|
const llvm::Type *BasePtr = ConvertType(BaseElementTy);
|
|
|
|
BasePtr = llvm::PointerType::getUnqual(BasePtr);
|
|
|
|
llvm::Value *BaseAddrPtr =
|
2010-09-15 18:14:12 +08:00
|
|
|
Builder.CreateBitCast(Dest.getAddr(), BasePtr);
|
2010-01-02 04:29:01 +08:00
|
|
|
|
|
|
|
EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
|
|
|
|
E->arg_begin(), E->arg_end());
|
|
|
|
}
|
2010-05-03 07:20:53 +08:00
|
|
|
else {
|
|
|
|
CXXCtorType Type =
|
|
|
|
(E->getConstructionKind() == CXXConstructExpr::CK_Complete)
|
|
|
|
? Ctor_Complete : Ctor_Base;
|
|
|
|
bool ForVirtualBase =
|
|
|
|
E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
|
|
|
|
|
2010-01-02 04:29:01 +08:00
|
|
|
// Call the constructor.
|
2010-09-15 18:14:12 +08:00
|
|
|
EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
|
2010-01-02 04:29:01 +08:00
|
|
|
E->arg_begin(), E->arg_end());
|
2010-05-03 07:20:53 +08:00
|
|
|
}
|
2010-01-02 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2010-11-14 05:53:34 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
|
|
|
|
llvm::Value *Src,
|
2010-12-03 01:02:11 +08:00
|
|
|
const Expr *Exp) {
|
2010-12-06 16:20:24 +08:00
|
|
|
if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
|
2010-11-14 05:53:34 +08:00
|
|
|
Exp = E->getSubExpr();
|
|
|
|
assert(isa<CXXConstructExpr>(Exp) &&
|
|
|
|
"EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
|
|
|
|
const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
|
|
|
|
const CXXConstructorDecl *CD = E->getConstructor();
|
|
|
|
RunCleanupsScope Scope(*this);
|
|
|
|
|
|
|
|
// If we require zero initialization before (or instead of) calling the
|
|
|
|
// constructor, as can be the case with a non-user-provided default
|
|
|
|
// constructor, emit the zero initialization now.
|
|
|
|
// FIXME. Do I still need this for a copy ctor synthesis?
|
|
|
|
if (E->requiresZeroInitialization())
|
|
|
|
EmitNullInitialization(Dest, E->getType());
|
|
|
|
|
2010-11-15 21:54:43 +08:00
|
|
|
assert(!getContext().getAsConstantArrayType(E->getType())
|
|
|
|
&& "EmitSynthesizedCXXCopyCtor - Copied-in Array");
|
2010-11-14 05:53:34 +08:00
|
|
|
EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src,
|
|
|
|
E->arg_begin(), E->arg_end());
|
|
|
|
}
|
|
|
|
|
2010-08-23 09:17:59 +08:00
|
|
|
/// Check whether the given operator new[] is the global placement
|
|
|
|
/// operator new[].
|
|
|
|
static bool IsPlacementOperatorNewArray(ASTContext &Ctx,
|
|
|
|
const FunctionDecl *Fn) {
|
|
|
|
// Must be in global scope. Note that allocation functions can't be
|
|
|
|
// declared in namespaces.
|
2010-08-31 08:36:30 +08:00
|
|
|
if (!Fn->getDeclContext()->getRedeclContext()->isFileContext())
|
2010-08-23 09:17:59 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Signature must be void *operator new[](size_t, void*).
|
|
|
|
// The size_t is common to all operator new[]s.
|
|
|
|
if (Fn->getNumParams() != 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType());
|
|
|
|
return (ParamType == Ctx.VoidPtrTy);
|
|
|
|
}
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
|
|
|
|
const CXXNewExpr *E) {
|
2009-12-14 04:04:38 +08:00
|
|
|
if (!E->isArray())
|
2010-01-27 03:44:24 +08:00
|
|
|
return CharUnits::Zero();
|
2009-12-14 04:04:38 +08:00
|
|
|
|
2009-12-14 04:34:34 +08:00
|
|
|
// No cookie is required if the new operator being used is
|
|
|
|
// ::operator new[](size_t, void*).
|
|
|
|
const FunctionDecl *OperatorNew = E->getOperatorNew();
|
2010-09-02 17:58:18 +08:00
|
|
|
if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew))
|
2010-08-23 09:17:59 +08:00
|
|
|
return CharUnits::Zero();
|
|
|
|
|
2011-01-27 17:37:56 +08:00
|
|
|
return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
|
2009-09-24 00:07:23 +08:00
|
|
|
}
|
|
|
|
|
2010-03-25 00:57:01 +08:00
|
|
|
static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context,
|
2010-07-21 02:45:57 +08:00
|
|
|
CodeGenFunction &CGF,
|
2009-09-24 00:07:23 +08:00
|
|
|
const CXXNewExpr *E,
|
2010-07-21 09:10:17 +08:00
|
|
|
llvm::Value *&NumElements,
|
|
|
|
llvm::Value *&SizeWithoutCookie) {
|
2010-08-26 23:23:38 +08:00
|
|
|
QualType ElemType = E->getAllocatedType();
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
const llvm::IntegerType *SizeTy =
|
|
|
|
cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType()));
|
2009-09-24 00:07:23 +08:00
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
|
|
|
|
|
2010-07-21 09:10:17 +08:00
|
|
|
if (!E->isArray()) {
|
|
|
|
SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
|
|
|
|
return SizeWithoutCookie;
|
|
|
|
}
|
2009-09-24 00:07:23 +08:00
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
// Figure out the cookie size.
|
|
|
|
CharUnits CookieSize = CalculateCookiePadding(CGF, E);
|
|
|
|
|
2009-09-24 00:07:23 +08:00
|
|
|
// Emit the array size expression.
|
2010-08-26 23:23:38 +08:00
|
|
|
// We multiply the size of all dimensions for NumElements.
|
|
|
|
// e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
|
2009-09-24 00:07:23 +08:00
|
|
|
NumElements = CGF.EmitScalarExpr(E->getArraySize());
|
2010-09-02 17:58:18 +08:00
|
|
|
assert(NumElements->getType() == SizeTy && "element count not a size_t");
|
|
|
|
|
|
|
|
uint64_t ArraySizeMultiplier = 1;
|
2010-08-26 23:23:38 +08:00
|
|
|
while (const ConstantArrayType *CAT
|
|
|
|
= CGF.getContext().getAsConstantArrayType(ElemType)) {
|
|
|
|
ElemType = CAT->getElementType();
|
2010-09-02 17:58:18 +08:00
|
|
|
ArraySizeMultiplier *= CAT->getSize().getZExtValue();
|
2010-08-26 23:23:38 +08:00
|
|
|
}
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
llvm::Value *Size;
|
2010-07-21 04:19:24 +08:00
|
|
|
|
2010-07-21 05:55:52 +08:00
|
|
|
// If someone is doing 'new int[42]' there is no need to do a dynamic check.
|
|
|
|
// Don't bloat the -O0 code.
|
|
|
|
if (llvm::ConstantInt *NumElementsC =
|
|
|
|
dyn_cast<llvm::ConstantInt>(NumElements)) {
|
|
|
|
llvm::APInt NEC = NumElementsC->getValue();
|
2010-09-02 17:58:18 +08:00
|
|
|
unsigned SizeWidth = NEC.getBitWidth();
|
|
|
|
|
|
|
|
// Determine if there is an overflow here by doing an extended multiply.
|
2010-12-07 16:25:34 +08:00
|
|
|
NEC = NEC.zext(SizeWidth*2);
|
2010-09-02 17:58:18 +08:00
|
|
|
llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity());
|
2010-07-21 05:55:52 +08:00
|
|
|
SC *= NEC;
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
if (!CookieSize.isZero()) {
|
|
|
|
// Save the current size without a cookie. We don't care if an
|
|
|
|
// overflow's already happened because SizeWithoutCookie isn't
|
|
|
|
// used if the allocator returns null or throws, as it should
|
|
|
|
// always do on an overflow.
|
2010-12-07 16:25:34 +08:00
|
|
|
llvm::APInt SWC = SC.trunc(SizeWidth);
|
2010-09-02 17:58:18 +08:00
|
|
|
SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC);
|
|
|
|
|
|
|
|
// Add the cookie size.
|
|
|
|
SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity());
|
|
|
|
}
|
2010-07-21 05:55:52 +08:00
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
if (SC.countLeadingZeros() >= SizeWidth) {
|
2010-12-07 16:25:34 +08:00
|
|
|
SC = SC.trunc(SizeWidth);
|
2010-09-02 17:58:18 +08:00
|
|
|
Size = llvm::ConstantInt::get(SizeTy, SC);
|
2010-07-21 05:55:52 +08:00
|
|
|
} else {
|
|
|
|
// On overflow, produce a -1 so operator new throws.
|
2010-09-02 17:58:18 +08:00
|
|
|
Size = llvm::Constant::getAllOnesValue(SizeTy);
|
2010-07-21 05:55:52 +08:00
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
// Scale NumElements while we're at it.
|
|
|
|
uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier;
|
|
|
|
NumElements = llvm::ConstantInt::get(SizeTy, N);
|
|
|
|
|
|
|
|
// Otherwise, we don't need to do an overflow-checked multiplication if
|
|
|
|
// we're multiplying by one.
|
|
|
|
} else if (TypeSize.isOne()) {
|
|
|
|
assert(ArraySizeMultiplier == 1);
|
|
|
|
|
|
|
|
Size = NumElements;
|
|
|
|
|
|
|
|
// If we need a cookie, add its size in with an overflow check.
|
|
|
|
// This is maybe a little paranoid.
|
|
|
|
if (!CookieSize.isZero()) {
|
|
|
|
SizeWithoutCookie = Size;
|
|
|
|
|
|
|
|
llvm::Value *CookieSizeV
|
|
|
|
= llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
|
|
|
|
|
|
|
|
const llvm::Type *Types[] = { SizeTy };
|
|
|
|
llvm::Value *UAddF
|
|
|
|
= CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
|
|
|
|
llvm::Value *AddRes
|
|
|
|
= CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV);
|
|
|
|
|
|
|
|
Size = CGF.Builder.CreateExtractValue(AddRes, 0);
|
|
|
|
llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
|
|
|
|
Size = CGF.Builder.CreateSelect(DidOverflow,
|
|
|
|
llvm::ConstantInt::get(SizeTy, -1),
|
|
|
|
Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise use the int.umul.with.overflow intrinsic.
|
2010-07-21 05:55:52 +08:00
|
|
|
} else {
|
2010-09-02 17:58:18 +08:00
|
|
|
llvm::Value *OutermostElementSize
|
|
|
|
= llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
|
|
|
|
|
|
|
|
llvm::Value *NumOutermostElements = NumElements;
|
|
|
|
|
|
|
|
// Scale NumElements by the array size multiplier. This might
|
|
|
|
// overflow, but only if the multiplication below also overflows,
|
|
|
|
// in which case this multiplication isn't used.
|
|
|
|
if (ArraySizeMultiplier != 1)
|
|
|
|
NumElements = CGF.Builder.CreateMul(NumElements,
|
|
|
|
llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier));
|
|
|
|
|
|
|
|
// The requested size of the outermost array is non-constant.
|
|
|
|
// Multiply that by the static size of the elements of that array;
|
|
|
|
// on unsigned overflow, set the size to -1 to trigger an
|
|
|
|
// exception from the allocation routine. This is sufficient to
|
|
|
|
// prevent buffer overruns from the allocator returning a
|
|
|
|
// seemingly valid pointer to insufficient space. This idea comes
|
|
|
|
// originally from MSVC, and GCC has an open bug requesting
|
|
|
|
// similar behavior:
|
|
|
|
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351
|
|
|
|
//
|
|
|
|
// This will not be sufficient for C++0x, which requires a
|
|
|
|
// specific exception class (std::bad_array_new_length).
|
|
|
|
// That will require ABI support that has not yet been specified.
|
|
|
|
const llvm::Type *Types[] = { SizeTy };
|
|
|
|
llvm::Value *UMulF
|
|
|
|
= CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1);
|
|
|
|
llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements,
|
|
|
|
OutermostElementSize);
|
|
|
|
|
|
|
|
// The overflow bit.
|
2010-07-21 05:55:52 +08:00
|
|
|
llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1);
|
2010-07-21 05:07:09 +08:00
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
// The result of the multiplication.
|
|
|
|
Size = CGF.Builder.CreateExtractValue(MulRes, 0);
|
|
|
|
|
|
|
|
// If we have a cookie, we need to add that size in, too.
|
|
|
|
if (!CookieSize.isZero()) {
|
|
|
|
SizeWithoutCookie = Size;
|
|
|
|
|
|
|
|
llvm::Value *CookieSizeV
|
|
|
|
= llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
|
|
|
|
llvm::Value *UAddF
|
|
|
|
= CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
|
|
|
|
llvm::Value *AddRes
|
|
|
|
= CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV);
|
|
|
|
|
|
|
|
Size = CGF.Builder.CreateExtractValue(AddRes, 0);
|
|
|
|
|
|
|
|
llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
|
2011-04-10 03:54:33 +08:00
|
|
|
DidOverflow = CGF.Builder.CreateOr(DidOverflow, AddDidOverflow);
|
2010-09-02 17:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Size = CGF.Builder.CreateSelect(DidOverflow,
|
|
|
|
llvm::ConstantInt::get(SizeTy, -1),
|
|
|
|
Size);
|
2010-07-21 05:55:52 +08:00
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
if (CookieSize.isZero())
|
|
|
|
SizeWithoutCookie = Size;
|
|
|
|
else
|
|
|
|
assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?");
|
|
|
|
|
2010-07-21 05:55:52 +08:00
|
|
|
return Size;
|
2009-09-24 00:07:23 +08:00
|
|
|
}
|
|
|
|
|
2010-06-26 02:26:07 +08:00
|
|
|
static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E,
|
|
|
|
llvm::Value *NewPtr) {
|
|
|
|
|
|
|
|
assert(E->getNumConstructorArgs() == 1 &&
|
|
|
|
"Can only have one argument to initializer of POD type.");
|
|
|
|
|
|
|
|
const Expr *Init = E->getConstructorArg(0);
|
|
|
|
QualType AllocType = E->getAllocatedType();
|
2010-08-21 10:24:36 +08:00
|
|
|
|
|
|
|
unsigned Alignment =
|
|
|
|
CGF.getContext().getTypeAlignInChars(AllocType).getQuantity();
|
2010-06-26 02:26:07 +08:00
|
|
|
if (!CGF.hasAggregateLLVMType(AllocType))
|
|
|
|
CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr,
|
2010-08-21 10:24:36 +08:00
|
|
|
AllocType.isVolatileQualified(), Alignment,
|
|
|
|
AllocType);
|
2010-06-26 02:26:07 +08:00
|
|
|
else if (AllocType->isAnyComplexType())
|
|
|
|
CGF.EmitComplexExprIntoAddr(Init, NewPtr,
|
|
|
|
AllocType.isVolatileQualified());
|
2010-09-15 18:14:12 +08:00
|
|
|
else {
|
|
|
|
AggValueSlot Slot
|
|
|
|
= AggValueSlot::forAddr(NewPtr, AllocType.isVolatileQualified(), true);
|
|
|
|
CGF.EmitAggExpr(Init, Slot);
|
|
|
|
}
|
2010-06-26 02:26:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements) {
|
2010-06-26 04:01:13 +08:00
|
|
|
// We have a POD type.
|
|
|
|
if (E->getNumConstructorArgs() == 0)
|
|
|
|
return;
|
|
|
|
|
2010-06-26 02:26:07 +08:00
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
|
|
|
|
|
|
|
// Create a temporary for the loop index and initialize it with 0.
|
|
|
|
llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
|
|
|
|
Builder.CreateStore(Zero, IndexPtr);
|
|
|
|
|
|
|
|
// Start the loop with a block that tests the condition.
|
|
|
|
llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
|
|
|
|
llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
|
|
|
|
|
|
|
|
EmitBlock(CondBlock);
|
|
|
|
|
|
|
|
llvm::BasicBlock *ForBody = createBasicBlock("for.body");
|
|
|
|
|
|
|
|
// Generate: if (loop-index < number-of-elements fall to the loop body,
|
|
|
|
// otherwise, go to the block after the for-loop.
|
|
|
|
llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
|
|
|
|
llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
|
|
|
|
// If the condition is true, execute the body.
|
|
|
|
Builder.CreateCondBr(IsLess, ForBody, AfterFor);
|
|
|
|
|
|
|
|
EmitBlock(ForBody);
|
|
|
|
|
|
|
|
llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
|
|
|
|
// Inside the loop body, emit the constructor call on the array element.
|
|
|
|
Counter = Builder.CreateLoad(IndexPtr);
|
|
|
|
llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter,
|
|
|
|
"arrayidx");
|
|
|
|
StoreAnyExprIntoOneUnit(*this, E, Address);
|
|
|
|
|
|
|
|
EmitBlock(ContinueBlock);
|
|
|
|
|
|
|
|
// Emit the increment of the loop counter.
|
|
|
|
llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
|
|
|
|
Counter = Builder.CreateLoad(IndexPtr);
|
|
|
|
NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
|
|
|
|
Builder.CreateStore(NextVal, IndexPtr);
|
|
|
|
|
|
|
|
// Finally, branch back up to the condition for the next iteration.
|
|
|
|
EmitBranch(CondBlock);
|
|
|
|
|
|
|
|
// Emit the fall-through block.
|
|
|
|
EmitBlock(AfterFor, true);
|
|
|
|
}
|
|
|
|
|
2010-07-21 09:10:17 +08:00
|
|
|
static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
|
|
|
|
llvm::Value *NewPtr, llvm::Value *Size) {
|
2011-02-08 16:22:06 +08:00
|
|
|
CGF.EmitCastToVoidPtr(NewPtr);
|
2011-01-19 09:58:38 +08:00
|
|
|
CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T);
|
2010-12-30 08:13:21 +08:00
|
|
|
CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size,
|
2011-01-19 09:58:38 +08:00
|
|
|
Alignment.getQuantity(), false);
|
2010-07-21 09:10:17 +08:00
|
|
|
}
|
|
|
|
|
2009-09-24 00:07:23 +08:00
|
|
|
static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
|
|
|
|
llvm::Value *NewPtr,
|
2010-07-21 09:10:17 +08:00
|
|
|
llvm::Value *NumElements,
|
|
|
|
llvm::Value *AllocSizeWithoutCookie) {
|
2009-11-25 02:43:52 +08:00
|
|
|
if (E->isArray()) {
|
2010-05-03 23:09:17 +08:00
|
|
|
if (CXXConstructorDecl *Ctor = E->getConstructor()) {
|
2010-07-21 09:10:17 +08:00
|
|
|
bool RequiresZeroInitialization = false;
|
|
|
|
if (Ctor->getParent()->hasTrivialConstructor()) {
|
|
|
|
// If new expression did not specify value-initialization, then there
|
|
|
|
// is no initialization.
|
|
|
|
if (!E->hasInitializer() || Ctor->getParent()->isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) {
|
2010-07-21 09:10:17 +08:00
|
|
|
// Optimization: since zero initialization will just set the memory
|
|
|
|
// to all zeroes, generate a single memset to do it in one shot.
|
|
|
|
EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
|
|
|
|
AllocSizeWithoutCookie);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RequiresZeroInitialization = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
|
|
|
|
E->constructor_arg_begin(),
|
|
|
|
E->constructor_arg_end(),
|
|
|
|
RequiresZeroInitialization);
|
2010-05-03 23:09:17 +08:00
|
|
|
return;
|
2010-07-21 09:10:17 +08:00
|
|
|
} else if (E->getNumConstructorArgs() == 1 &&
|
|
|
|
isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) {
|
|
|
|
// Optimization: since zero initialization will just set the memory
|
|
|
|
// to all zeroes, generate a single memset to do it in one shot.
|
|
|
|
EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
|
|
|
|
AllocSizeWithoutCookie);
|
|
|
|
return;
|
|
|
|
} else {
|
2010-06-26 02:26:07 +08:00
|
|
|
CGF.EmitNewArrayInitializer(E, NewPtr, NumElements);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-25 02:43:52 +08:00
|
|
|
}
|
2009-09-24 00:07:23 +08:00
|
|
|
|
2009-11-25 02:43:52 +08:00
|
|
|
if (CXXConstructorDecl *Ctor = E->getConstructor()) {
|
2010-07-08 14:14:04 +08:00
|
|
|
// Per C++ [expr.new]p15, if we have an initializer, then we're performing
|
|
|
|
// direct initialization. C++ [dcl.init]p5 requires that we
|
|
|
|
// zero-initialize storage if there are no user-declared constructors.
|
|
|
|
if (E->hasInitializer() &&
|
|
|
|
!Ctor->getParent()->hasUserDeclaredConstructor() &&
|
|
|
|
!Ctor->getParent()->isEmpty())
|
|
|
|
CGF.EmitNullInitialization(NewPtr, E->getAllocatedType());
|
|
|
|
|
2010-07-08 07:37:33 +08:00
|
|
|
CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
|
|
|
|
NewPtr, E->constructor_arg_begin(),
|
|
|
|
E->constructor_arg_end());
|
2009-09-24 00:07:23 +08:00
|
|
|
|
2009-11-25 02:43:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-06-26 04:01:13 +08:00
|
|
|
// We have a POD type.
|
|
|
|
if (E->getNumConstructorArgs() == 0)
|
|
|
|
return;
|
|
|
|
|
2010-06-26 02:26:07 +08:00
|
|
|
StoreAnyExprIntoOneUnit(CGF, E, NewPtr);
|
2009-09-24 00:07:23 +08:00
|
|
|
}
|
|
|
|
|
2010-09-14 15:57:04 +08:00
|
|
|
namespace {
|
|
|
|
/// A cleanup to call the given 'operator delete' function upon
|
|
|
|
/// abnormal exit from a new expression.
|
|
|
|
class CallDeleteDuringNew : public EHScopeStack::Cleanup {
|
|
|
|
size_t NumPlacementArgs;
|
|
|
|
const FunctionDecl *OperatorDelete;
|
|
|
|
llvm::Value *Ptr;
|
|
|
|
llvm::Value *AllocSize;
|
|
|
|
|
|
|
|
RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
static size_t getExtraSize(size_t NumPlacementArgs) {
|
|
|
|
return NumPlacementArgs * sizeof(RValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
CallDeleteDuringNew(size_t NumPlacementArgs,
|
|
|
|
const FunctionDecl *OperatorDelete,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
llvm::Value *AllocSize)
|
|
|
|
: NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
|
|
|
|
Ptr(Ptr), AllocSize(AllocSize) {}
|
|
|
|
|
|
|
|
void setPlacementArg(unsigned I, RValue Arg) {
|
|
|
|
assert(I < NumPlacementArgs && "index out of range");
|
|
|
|
getPlacementArgs()[I] = Arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= OperatorDelete->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
|
2010-09-15 05:45:42 +08:00
|
|
|
(FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
|
2010-09-14 15:57:04 +08:00
|
|
|
|
|
|
|
CallArgList DeleteArgs;
|
|
|
|
|
|
|
|
// The first argument is always a void*.
|
|
|
|
FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
|
|
|
|
DeleteArgs.push_back(std::make_pair(RValue::get(Ptr), *AI++));
|
|
|
|
|
|
|
|
// A member 'operator delete' can take an extra 'size_t' argument.
|
|
|
|
if (FPT->getNumArgs() == NumPlacementArgs + 2)
|
|
|
|
DeleteArgs.push_back(std::make_pair(RValue::get(AllocSize), *AI++));
|
|
|
|
|
|
|
|
// Pass the rest of the arguments, which must match exactly.
|
|
|
|
for (unsigned I = 0; I != NumPlacementArgs; ++I)
|
|
|
|
DeleteArgs.push_back(std::make_pair(getPlacementArgs()[I], *AI++));
|
|
|
|
|
|
|
|
// Call 'operator delete'.
|
2011-03-03 05:36:49 +08:00
|
|
|
CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
|
2010-09-14 15:57:04 +08:00
|
|
|
CGF.CGM.GetAddrOfFunction(OperatorDelete),
|
|
|
|
ReturnValueSlot(), DeleteArgs, OperatorDelete);
|
|
|
|
}
|
|
|
|
};
|
2010-09-17 08:50:28 +08:00
|
|
|
|
|
|
|
/// A cleanup to call the given 'operator delete' function upon
|
|
|
|
/// abnormal exit from a new expression when the new expression is
|
|
|
|
/// conditional.
|
|
|
|
class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
|
|
|
|
size_t NumPlacementArgs;
|
|
|
|
const FunctionDecl *OperatorDelete;
|
2011-01-28 18:53:53 +08:00
|
|
|
DominatingValue<RValue>::saved_type Ptr;
|
|
|
|
DominatingValue<RValue>::saved_type AllocSize;
|
2010-09-17 08:50:28 +08:00
|
|
|
|
2011-01-28 18:53:53 +08:00
|
|
|
DominatingValue<RValue>::saved_type *getPlacementArgs() {
|
|
|
|
return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
|
2010-09-17 08:50:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static size_t getExtraSize(size_t NumPlacementArgs) {
|
2011-01-28 18:53:53 +08:00
|
|
|
return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
|
2010-09-17 08:50:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
|
|
|
|
const FunctionDecl *OperatorDelete,
|
2011-01-28 18:53:53 +08:00
|
|
|
DominatingValue<RValue>::saved_type Ptr,
|
|
|
|
DominatingValue<RValue>::saved_type AllocSize)
|
2010-09-17 08:50:28 +08:00
|
|
|
: NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
|
|
|
|
Ptr(Ptr), AllocSize(AllocSize) {}
|
|
|
|
|
2011-01-28 18:53:53 +08:00
|
|
|
void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
|
2010-09-17 08:50:28 +08:00
|
|
|
assert(I < NumPlacementArgs && "index out of range");
|
|
|
|
getPlacementArgs()[I] = Arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
const FunctionProtoType *FPT
|
|
|
|
= OperatorDelete->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
|
|
|
|
(FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
|
|
|
|
|
|
|
|
CallArgList DeleteArgs;
|
|
|
|
|
|
|
|
// The first argument is always a void*.
|
|
|
|
FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
|
2011-01-28 18:53:53 +08:00
|
|
|
DeleteArgs.push_back(std::make_pair(Ptr.restore(CGF), *AI++));
|
2010-09-17 08:50:28 +08:00
|
|
|
|
|
|
|
// A member 'operator delete' can take an extra 'size_t' argument.
|
|
|
|
if (FPT->getNumArgs() == NumPlacementArgs + 2) {
|
2011-01-28 18:53:53 +08:00
|
|
|
RValue RV = AllocSize.restore(CGF);
|
2010-09-17 08:50:28 +08:00
|
|
|
DeleteArgs.push_back(std::make_pair(RV, *AI++));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the rest of the arguments, which must match exactly.
|
|
|
|
for (unsigned I = 0; I != NumPlacementArgs; ++I) {
|
2011-01-28 18:53:53 +08:00
|
|
|
RValue RV = getPlacementArgs()[I].restore(CGF);
|
2010-09-17 08:50:28 +08:00
|
|
|
DeleteArgs.push_back(std::make_pair(RV, *AI++));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call 'operator delete'.
|
2011-03-03 05:36:49 +08:00
|
|
|
CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
|
2010-09-17 08:50:28 +08:00
|
|
|
CGF.CGM.GetAddrOfFunction(OperatorDelete),
|
|
|
|
ReturnValueSlot(), DeleteArgs, OperatorDelete);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enter a cleanup to call 'operator delete' if the initializer in a
|
|
|
|
/// new-expression throws.
|
|
|
|
static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
|
|
|
|
const CXXNewExpr *E,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *AllocSize,
|
|
|
|
const CallArgList &NewArgs) {
|
|
|
|
// If we're not inside a conditional branch, then the cleanup will
|
|
|
|
// dominate and we can do the easier (and more efficient) thing.
|
|
|
|
if (!CGF.isInConditionalBranch()) {
|
|
|
|
CallDeleteDuringNew *Cleanup = CGF.EHStack
|
|
|
|
.pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
|
|
|
|
E->getNumPlacementArgs(),
|
|
|
|
E->getOperatorDelete(),
|
|
|
|
NewPtr, AllocSize);
|
|
|
|
for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
|
|
|
|
Cleanup->setPlacementArg(I, NewArgs[I+1].first);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need to save all this stuff.
|
2011-01-28 18:53:53 +08:00
|
|
|
DominatingValue<RValue>::saved_type SavedNewPtr =
|
|
|
|
DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
|
|
|
|
DominatingValue<RValue>::saved_type SavedAllocSize =
|
|
|
|
DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
|
2010-09-17 08:50:28 +08:00
|
|
|
|
|
|
|
CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
|
|
|
|
.pushCleanupWithExtra<CallDeleteDuringConditionalNew>(InactiveEHCleanup,
|
|
|
|
E->getNumPlacementArgs(),
|
|
|
|
E->getOperatorDelete(),
|
|
|
|
SavedNewPtr,
|
|
|
|
SavedAllocSize);
|
|
|
|
for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
|
2011-01-28 18:53:53 +08:00
|
|
|
Cleanup->setPlacementArg(I,
|
|
|
|
DominatingValue<RValue>::save(CGF, NewArgs[I+1].first));
|
2010-09-17 08:50:28 +08:00
|
|
|
|
|
|
|
CGF.ActivateCleanupBlock(CGF.EHStack.stable_begin());
|
2010-09-14 15:57:04 +08:00
|
|
|
}
|
|
|
|
|
2009-09-23 06:53:17 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
|
2011-03-07 11:12:35 +08:00
|
|
|
// The element type being allocated.
|
|
|
|
QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
// 1. Build a call to the allocation function.
|
|
|
|
FunctionDecl *allocator = E->getOperatorNew();
|
|
|
|
const FunctionProtoType *allocatorType =
|
|
|
|
allocator->getType()->castAs<FunctionProtoType>();
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
CallArgList allocatorArgs;
|
2009-09-23 06:53:17 +08:00
|
|
|
|
|
|
|
// The allocation size is the first argument.
|
2011-03-07 11:12:35 +08:00
|
|
|
QualType sizeType = getContext().getSizeType();
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
llvm::Value *numElements = 0;
|
|
|
|
llvm::Value *allocSizeWithoutCookie = 0;
|
|
|
|
llvm::Value *allocSize =
|
|
|
|
EmitCXXNewAllocSize(getContext(), *this, E, numElements,
|
|
|
|
allocSizeWithoutCookie);
|
2009-09-24 00:07:23 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
allocatorArgs.push_back(std::make_pair(RValue::get(allocSize), sizeType));
|
2009-09-23 06:53:17 +08:00
|
|
|
|
|
|
|
// Emit the rest of the arguments.
|
|
|
|
// FIXME: Ideally, this should just use EmitCallArgs.
|
2011-03-07 11:12:35 +08:00
|
|
|
CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin();
|
2009-09-23 06:53:17 +08:00
|
|
|
|
|
|
|
// First, use the types from the function type.
|
|
|
|
// We start at 1 here because the first argument (the allocation size)
|
|
|
|
// has already been emitted.
|
2011-03-07 11:12:35 +08:00
|
|
|
for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e;
|
|
|
|
++i, ++placementArg) {
|
|
|
|
QualType argType = allocatorType->getArgType(i);
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(),
|
|
|
|
placementArg->getType()) &&
|
2009-09-23 06:53:17 +08:00
|
|
|
"type mismatch in call argument!");
|
|
|
|
|
2011-03-12 04:59:21 +08:00
|
|
|
EmitCallArg(allocatorArgs, *placementArg, argType);
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Either we've emitted all the call args, or we have a call to a
|
|
|
|
// variadic function.
|
2011-03-07 11:12:35 +08:00
|
|
|
assert((placementArg == E->placement_arg_end() ||
|
|
|
|
allocatorType->isVariadic()) &&
|
|
|
|
"Extra arguments to non-variadic function!");
|
2009-09-23 06:53:17 +08:00
|
|
|
|
|
|
|
// If we still have any arguments, emit them using the type of the argument.
|
2011-03-07 11:12:35 +08:00
|
|
|
for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end();
|
|
|
|
placementArg != placementArgsEnd; ++placementArg) {
|
2011-03-12 04:59:21 +08:00
|
|
|
EmitCallArg(allocatorArgs, *placementArg, placementArg->getType());
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
// Emit the allocation call.
|
2009-09-23 06:53:17 +08:00
|
|
|
RValue RV =
|
2011-03-07 11:12:35 +08:00
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(allocatorArgs, allocatorType),
|
|
|
|
CGM.GetAddrOfFunction(allocator), ReturnValueSlot(),
|
|
|
|
allocatorArgs, allocator);
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
// Emit a null check on the allocation result if the allocation
|
|
|
|
// function is allowed to return null (because it has a non-throwing
|
|
|
|
// exception spec; for this part, we inline
|
|
|
|
// CXXNewExpr::shouldNullCheckAllocation()) and we have an
|
|
|
|
// interesting initializer.
|
2011-03-14 01:09:40 +08:00
|
|
|
bool nullCheck = allocatorType->isNothrow(getContext()) &&
|
2011-03-07 11:12:35 +08:00
|
|
|
!(allocType->isPODType() && !E->hasInitializer());
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
llvm::BasicBlock *nullCheckBB = 0;
|
|
|
|
llvm::BasicBlock *contBB = 0;
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
llvm::Value *allocation = RV.getScalarVal();
|
|
|
|
unsigned AS =
|
|
|
|
cast<llvm::PointerType>(allocation->getType())->getAddressSpace();
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 09:52:56 +08:00
|
|
|
// The null-check means that the initializer is conditionally
|
|
|
|
// evaluated.
|
|
|
|
ConditionalEvaluation conditional(*this);
|
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
if (nullCheck) {
|
|
|
|
conditional.begin(*this);
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
nullCheckBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
|
|
|
|
contBB = createBasicBlock("new.cont");
|
2011-03-07 09:52:56 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
|
|
|
|
Builder.CreateCondBr(isNull, contBB, notNullBB);
|
|
|
|
EmitBlock(notNullBB);
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
2010-01-27 03:44:24 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
assert((allocSize == allocSizeWithoutCookie) ==
|
2010-09-02 17:58:18 +08:00
|
|
|
CalculateCookiePadding(*this, E).isZero());
|
2011-03-07 11:12:35 +08:00
|
|
|
if (allocSize != allocSizeWithoutCookie) {
|
2010-09-02 17:58:18 +08:00
|
|
|
assert(E->isArray());
|
2011-03-07 11:12:35 +08:00
|
|
|
allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
|
|
|
|
numElements,
|
|
|
|
E, allocType);
|
2009-09-24 02:59:48 +08:00
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2010-09-14 15:57:04 +08:00
|
|
|
// If there's an operator delete, enter a cleanup to call it if an
|
|
|
|
// exception is thrown.
|
2011-03-07 11:12:35 +08:00
|
|
|
EHScopeStack::stable_iterator operatorDeleteCleanup;
|
2010-09-14 15:57:04 +08:00
|
|
|
if (E->getOperatorDelete()) {
|
2011-03-07 11:12:35 +08:00
|
|
|
EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
|
|
|
|
operatorDeleteCleanup = EHStack.stable_begin();
|
2010-09-14 15:57:04 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
const llvm::Type *elementPtrTy
|
|
|
|
= ConvertTypeForMem(allocType)->getPointerTo(AS);
|
|
|
|
llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
|
2010-09-14 15:57:04 +08:00
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
if (E->isArray()) {
|
2011-03-07 11:12:35 +08:00
|
|
|
EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
// NewPtr is a pointer to the base element type. If we're
|
|
|
|
// allocating an array of arrays, we'll need to cast back to the
|
|
|
|
// array pointer type.
|
2011-03-07 11:12:35 +08:00
|
|
|
const llvm::Type *resultType = ConvertTypeForMem(E->getType());
|
|
|
|
if (result->getType() != resultType)
|
|
|
|
result = Builder.CreateBitCast(result, resultType);
|
2010-09-02 17:58:18 +08:00
|
|
|
} else {
|
2011-03-07 11:12:35 +08:00
|
|
|
EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie);
|
2010-03-25 00:57:01 +08:00
|
|
|
}
|
2010-09-14 15:57:04 +08:00
|
|
|
|
|
|
|
// Deactivate the 'operator delete' cleanup if we finished
|
|
|
|
// initialization.
|
2011-03-07 11:12:35 +08:00
|
|
|
if (operatorDeleteCleanup.isValid())
|
|
|
|
DeactivateCleanupBlock(operatorDeleteCleanup);
|
2010-03-25 00:57:01 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
if (nullCheck) {
|
2011-03-07 09:52:56 +08:00
|
|
|
conditional.end(*this);
|
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
|
|
|
|
EmitBlock(contBB);
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
|
2011-03-07 11:12:35 +08:00
|
|
|
PHI->addIncoming(result, notNullBB);
|
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
|
|
|
|
nullCheckBB);
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
result = PHI;
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2011-03-07 11:12:35 +08:00
|
|
|
return result;
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 08:50:08 +08:00
|
|
|
void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
QualType DeleteTy) {
|
2010-09-02 17:58:18 +08:00
|
|
|
assert(DeleteFD->getOverloadedOperator() == OO_Delete);
|
|
|
|
|
2009-11-18 08:50:08 +08:00
|
|
|
const FunctionProtoType *DeleteFTy =
|
|
|
|
DeleteFD->getType()->getAs<FunctionProtoType>();
|
|
|
|
|
|
|
|
CallArgList DeleteArgs;
|
|
|
|
|
2009-12-14 04:04:38 +08:00
|
|
|
// Check if we need to pass the size to the delete operator.
|
|
|
|
llvm::Value *Size = 0;
|
|
|
|
QualType SizeTy;
|
2009-11-18 08:50:08 +08:00
|
|
|
if (DeleteFTy->getNumArgs() == 2) {
|
2009-12-14 04:04:38 +08:00
|
|
|
SizeTy = DeleteFTy->getArgType(1);
|
2010-01-27 03:59:28 +08:00
|
|
|
CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
|
|
|
|
Size = llvm::ConstantInt::get(ConvertType(SizeTy),
|
|
|
|
DeleteTypeSize.getQuantity());
|
2009-12-14 04:04:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QualType ArgTy = DeleteFTy->getArgType(0);
|
|
|
|
llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
|
|
|
|
DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
|
|
|
|
|
|
|
|
if (Size)
|
|
|
|
DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
|
2009-11-18 08:50:08 +08:00
|
|
|
|
|
|
|
// Emit the call to delete.
|
2011-03-03 05:36:49 +08:00
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
|
2009-12-25 03:25:24 +08:00
|
|
|
CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
|
2009-11-18 08:50:08 +08:00
|
|
|
DeleteArgs, DeleteFD);
|
|
|
|
}
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
namespace {
|
|
|
|
/// Calls the given 'operator delete' on a single object.
|
|
|
|
struct CallObjectDelete : EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *Ptr;
|
|
|
|
const FunctionDecl *OperatorDelete;
|
|
|
|
QualType ElementType;
|
|
|
|
|
|
|
|
CallObjectDelete(llvm::Value *Ptr,
|
|
|
|
const FunctionDecl *OperatorDelete,
|
|
|
|
QualType ElementType)
|
|
|
|
: Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the code for deleting a single object.
|
|
|
|
static void EmitObjectDelete(CodeGenFunction &CGF,
|
|
|
|
const FunctionDecl *OperatorDelete,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
QualType ElementType) {
|
|
|
|
// Find the destructor for the type, if applicable. If the
|
|
|
|
// destructor is virtual, we'll just emit the vcall and return.
|
|
|
|
const CXXDestructorDecl *Dtor = 0;
|
|
|
|
if (const RecordType *RT = ElementType->getAs<RecordType>()) {
|
|
|
|
CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (!RD->hasTrivialDestructor()) {
|
|
|
|
Dtor = RD->getDestructor();
|
|
|
|
|
|
|
|
if (Dtor->isVirtual()) {
|
|
|
|
const llvm::Type *Ty =
|
2010-09-03 09:26:39 +08:00
|
|
|
CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor,
|
|
|
|
Dtor_Complete),
|
2010-09-02 17:58:18 +08:00
|
|
|
/*isVariadic=*/false);
|
|
|
|
|
|
|
|
llvm::Value *Callee
|
|
|
|
= CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
|
|
|
|
CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
|
|
|
|
0, 0);
|
|
|
|
|
|
|
|
// The dtor took care of deleting the object.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we call delete even if the dtor throws.
|
2011-01-28 16:37:24 +08:00
|
|
|
// This doesn't have to a conditional cleanup because we're going
|
|
|
|
// to pop it off in a second.
|
2010-09-02 17:58:18 +08:00
|
|
|
CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
|
|
|
|
Ptr, OperatorDelete, ElementType);
|
|
|
|
|
|
|
|
if (Dtor)
|
|
|
|
CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
|
|
|
|
/*ForVirtualBase=*/false, Ptr);
|
|
|
|
|
|
|
|
CGF.PopCleanupBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Calls the given 'operator delete' on an array of objects.
|
|
|
|
struct CallArrayDelete : EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *Ptr;
|
|
|
|
const FunctionDecl *OperatorDelete;
|
|
|
|
llvm::Value *NumElements;
|
|
|
|
QualType ElementType;
|
|
|
|
CharUnits CookieSize;
|
|
|
|
|
|
|
|
CallArrayDelete(llvm::Value *Ptr,
|
|
|
|
const FunctionDecl *OperatorDelete,
|
|
|
|
llvm::Value *NumElements,
|
|
|
|
QualType ElementType,
|
|
|
|
CharUnits CookieSize)
|
|
|
|
: Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
|
|
|
|
ElementType(ElementType), CookieSize(CookieSize) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
const FunctionProtoType *DeleteFTy =
|
|
|
|
OperatorDelete->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
|
|
|
|
// Pass the pointer as the first argument.
|
|
|
|
QualType VoidPtrTy = DeleteFTy->getArgType(0);
|
|
|
|
llvm::Value *DeletePtr
|
|
|
|
= CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy));
|
|
|
|
|
|
|
|
// Pass the original requested size as the second argument.
|
|
|
|
if (DeleteFTy->getNumArgs() == 2) {
|
|
|
|
QualType size_t = DeleteFTy->getArgType(1);
|
|
|
|
const llvm::IntegerType *SizeTy
|
|
|
|
= cast<llvm::IntegerType>(CGF.ConvertType(size_t));
|
|
|
|
|
|
|
|
CharUnits ElementTypeSize =
|
|
|
|
CGF.CGM.getContext().getTypeSizeInChars(ElementType);
|
|
|
|
|
|
|
|
// The size of an element, multiplied by the number of elements.
|
|
|
|
llvm::Value *Size
|
|
|
|
= llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
|
|
|
|
Size = CGF.Builder.CreateMul(Size, NumElements);
|
|
|
|
|
|
|
|
// Plus the size of the cookie if applicable.
|
|
|
|
if (!CookieSize.isZero()) {
|
|
|
|
llvm::Value *CookieSizeV
|
|
|
|
= llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
|
|
|
|
Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
|
|
|
|
}
|
|
|
|
|
|
|
|
Args.push_back(std::make_pair(RValue::get(Size), size_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the call to delete.
|
2011-03-03 05:36:49 +08:00
|
|
|
CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
|
2010-09-02 17:58:18 +08:00
|
|
|
CGF.CGM.GetAddrOfFunction(OperatorDelete),
|
|
|
|
ReturnValueSlot(), Args, OperatorDelete);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the code for deleting an array of objects.
|
|
|
|
static void EmitArrayDelete(CodeGenFunction &CGF,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXDeleteExpr *E,
|
2010-09-02 17:58:18 +08:00
|
|
|
llvm::Value *Ptr,
|
|
|
|
QualType ElementType) {
|
|
|
|
llvm::Value *NumElements = 0;
|
|
|
|
llvm::Value *AllocatedPtr = 0;
|
|
|
|
CharUnits CookieSize;
|
2011-01-27 17:37:56 +08:00
|
|
|
CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, E, ElementType,
|
2010-09-02 17:58:18 +08:00
|
|
|
NumElements, AllocatedPtr, CookieSize);
|
|
|
|
|
|
|
|
assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
|
|
|
|
|
|
|
|
// Make sure that we call delete even if one of the dtors throws.
|
2011-01-27 17:37:56 +08:00
|
|
|
const FunctionDecl *OperatorDelete = E->getOperatorDelete();
|
2010-09-02 17:58:18 +08:00
|
|
|
CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
|
|
|
|
AllocatedPtr, OperatorDelete,
|
|
|
|
NumElements, ElementType,
|
|
|
|
CookieSize);
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
|
|
|
|
if (!RD->hasTrivialDestructor()) {
|
|
|
|
assert(NumElements && "ReadArrayCookie didn't find element count"
|
|
|
|
" for a class with destructor");
|
|
|
|
CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.PopCleanupBlock();
|
|
|
|
}
|
|
|
|
|
2009-09-23 06:53:17 +08:00
|
|
|
void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
|
2009-11-14 03:27:47 +08:00
|
|
|
|
2009-09-30 02:16:17 +08:00
|
|
|
// Get at the argument before we performed the implicit conversion
|
|
|
|
// to void*.
|
|
|
|
const Expr *Arg = E->getArgument();
|
|
|
|
while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (ICE->getCastKind() != CK_UserDefinedConversion &&
|
2009-09-30 02:16:17 +08:00
|
|
|
ICE->getType()->isVoidPointerType())
|
|
|
|
Arg = ICE->getSubExpr();
|
2009-10-01 13:49:51 +08:00
|
|
|
else
|
|
|
|
break;
|
2009-09-30 02:16:17 +08:00
|
|
|
}
|
2009-09-23 06:53:17 +08:00
|
|
|
|
2009-09-30 02:16:17 +08:00
|
|
|
llvm::Value *Ptr = EmitScalarExpr(Arg);
|
2009-09-23 06:53:17 +08:00
|
|
|
|
|
|
|
// Null check the pointer.
|
|
|
|
llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
|
|
|
|
llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
|
|
|
|
|
|
|
|
llvm::Value *IsNull =
|
|
|
|
Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
|
|
|
|
"isnull");
|
|
|
|
|
|
|
|
Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
|
|
|
|
EmitBlock(DeleteNotNull);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
// We might be deleting a pointer to array. If so, GEP down to the
|
|
|
|
// first non-array element.
|
|
|
|
// (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
|
|
|
|
QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
|
|
|
|
if (DeleteTy->isConstantArrayType()) {
|
|
|
|
llvm::Value *Zero = Builder.getInt32(0);
|
|
|
|
llvm::SmallVector<llvm::Value*,8> GEP;
|
|
|
|
|
|
|
|
GEP.push_back(Zero); // point at the outermost array
|
|
|
|
|
|
|
|
// For each layer of array type we're pointing at:
|
|
|
|
while (const ConstantArrayType *Arr
|
|
|
|
= getContext().getAsConstantArrayType(DeleteTy)) {
|
|
|
|
// 1. Unpeel the array type.
|
|
|
|
DeleteTy = Arr->getElementType();
|
|
|
|
|
|
|
|
// 2. GEP to the first element of the array.
|
|
|
|
GEP.push_back(Zero);
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
|
2009-09-23 06:53:17 +08:00
|
|
|
}
|
|
|
|
|
2010-09-03 01:38:50 +08:00
|
|
|
assert(ConvertTypeForMem(DeleteTy) ==
|
|
|
|
cast<llvm::PointerType>(Ptr->getType())->getElementType());
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
if (E->isArrayForm()) {
|
2011-01-27 17:37:56 +08:00
|
|
|
EmitArrayDelete(*this, E, Ptr, DeleteTy);
|
2010-09-02 17:58:18 +08:00
|
|
|
} else {
|
|
|
|
EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
|
|
|
|
}
|
2009-09-23 06:53:17 +08:00
|
|
|
|
|
|
|
EmitBlock(DeleteEnd);
|
|
|
|
}
|
2009-11-15 16:09:41 +08:00
|
|
|
|
2011-01-28 16:37:24 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
|
2009-11-15 16:09:41 +08:00
|
|
|
QualType Ty = E->getType();
|
|
|
|
const llvm::Type *LTy = ConvertType(Ty)->getPointerTo();
|
2009-12-11 10:46:30 +08:00
|
|
|
|
2009-12-17 15:09:17 +08:00
|
|
|
if (E->isTypeOperand()) {
|
|
|
|
llvm::Constant *TypeInfo =
|
|
|
|
CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
|
|
|
|
return Builder.CreateBitCast(TypeInfo, LTy);
|
|
|
|
}
|
|
|
|
|
2009-11-15 16:09:41 +08:00
|
|
|
Expr *subE = E->getExprOperand();
|
2009-11-18 06:33:00 +08:00
|
|
|
Ty = subE->getType();
|
|
|
|
CanQualType CanTy = CGM.getContext().getCanonicalType(Ty);
|
|
|
|
Ty = CanTy.getUnqualifiedType().getNonReferenceType();
|
2009-11-15 16:09:41 +08:00
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (RD->isPolymorphic()) {
|
|
|
|
// FIXME: if subE is an lvalue do
|
|
|
|
LValue Obj = EmitLValue(subE);
|
|
|
|
llvm::Value *This = Obj.getAddress();
|
2009-11-16 00:52:53 +08:00
|
|
|
// We need to do a zero check for *p, unless it has NonNullAttr.
|
|
|
|
// FIXME: PointerType->hasAttr<NonNullAttr>()
|
|
|
|
bool CanBeZero = false;
|
2009-11-17 08:45:21 +08:00
|
|
|
if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE->IgnoreParens()))
|
2010-08-25 19:45:40 +08:00
|
|
|
if (UO->getOpcode() == UO_Deref)
|
2009-11-16 00:52:53 +08:00
|
|
|
CanBeZero = true;
|
|
|
|
if (CanBeZero) {
|
|
|
|
llvm::BasicBlock *NonZeroBlock = createBasicBlock();
|
|
|
|
llvm::BasicBlock *ZeroBlock = createBasicBlock();
|
|
|
|
|
2010-10-27 02:44:08 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(This->getType());
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpNE(This, Zero),
|
2009-11-16 00:52:53 +08:00
|
|
|
NonZeroBlock, ZeroBlock);
|
|
|
|
EmitBlock(ZeroBlock);
|
|
|
|
/// Call __cxa_bad_typeid
|
2011-02-08 16:22:06 +08:00
|
|
|
const llvm::Type *ResultType = llvm::Type::getVoidTy(getLLVMContext());
|
2009-11-16 00:52:53 +08:00
|
|
|
const llvm::FunctionType *FTy;
|
|
|
|
FTy = llvm::FunctionType::get(ResultType, false);
|
|
|
|
llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
|
2009-11-16 14:50:58 +08:00
|
|
|
Builder.CreateCall(F)->setDoesNotReturn();
|
2009-11-16 00:52:53 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
EmitBlock(NonZeroBlock);
|
|
|
|
}
|
2010-10-27 02:44:08 +08:00
|
|
|
llvm::Value *V = GetVTablePtr(This, LTy->getPointerTo());
|
2009-11-15 16:09:41 +08:00
|
|
|
V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL);
|
|
|
|
V = Builder.CreateLoad(V);
|
|
|
|
return V;
|
2009-12-17 15:09:17 +08:00
|
|
|
}
|
2009-11-15 16:09:41 +08:00
|
|
|
}
|
2009-12-17 15:09:17 +08:00
|
|
|
return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(Ty), LTy);
|
2009-11-15 16:09:41 +08:00
|
|
|
}
|
2009-11-16 14:50:58 +08:00
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *V,
|
|
|
|
const CXXDynamicCastExpr *DCE) {
|
2009-12-17 15:09:17 +08:00
|
|
|
QualType SrcTy = DCE->getSubExpr()->getType();
|
|
|
|
QualType DestTy = DCE->getTypeAsWritten();
|
|
|
|
QualType InnerType = DestTy->getPointeeType();
|
|
|
|
|
2009-11-16 14:50:58 +08:00
|
|
|
const llvm::Type *LTy = ConvertType(DCE->getType());
|
2009-11-17 06:52:20 +08:00
|
|
|
|
2009-11-16 14:50:58 +08:00
|
|
|
bool CanBeZero = false;
|
|
|
|
bool ToVoid = false;
|
2009-11-17 06:52:20 +08:00
|
|
|
bool ThrowOnBad = false;
|
2009-12-17 15:09:17 +08:00
|
|
|
if (DestTy->isPointerType()) {
|
2009-11-16 14:50:58 +08:00
|
|
|
// FIXME: if PointerType->hasAttr<NonNullAttr>(), we don't set this
|
|
|
|
CanBeZero = true;
|
|
|
|
if (InnerType->isVoidType())
|
|
|
|
ToVoid = true;
|
|
|
|
} else {
|
|
|
|
LTy = LTy->getPointerTo();
|
2010-05-15 05:14:41 +08:00
|
|
|
|
|
|
|
// FIXME: What if exceptions are disabled?
|
2009-11-16 14:50:58 +08:00
|
|
|
ThrowOnBad = true;
|
|
|
|
}
|
|
|
|
|
2009-12-17 15:09:17 +08:00
|
|
|
if (SrcTy->isPointerType() || SrcTy->isReferenceType())
|
|
|
|
SrcTy = SrcTy->getPointeeType();
|
|
|
|
SrcTy = SrcTy.getUnqualifiedType();
|
|
|
|
|
2009-12-18 22:55:04 +08:00
|
|
|
if (DestTy->isPointerType() || DestTy->isReferenceType())
|
2009-12-17 15:09:17 +08:00
|
|
|
DestTy = DestTy->getPointeeType();
|
|
|
|
DestTy = DestTy.getUnqualifiedType();
|
2009-11-16 14:50:58 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *ContBlock = createBasicBlock();
|
|
|
|
llvm::BasicBlock *NullBlock = 0;
|
|
|
|
llvm::BasicBlock *NonZeroBlock = 0;
|
|
|
|
if (CanBeZero) {
|
|
|
|
NonZeroBlock = createBasicBlock();
|
|
|
|
NullBlock = createBasicBlock();
|
2009-12-17 15:09:17 +08:00
|
|
|
Builder.CreateCondBr(Builder.CreateIsNotNull(V), NonZeroBlock, NullBlock);
|
2009-11-16 14:50:58 +08:00
|
|
|
EmitBlock(NonZeroBlock);
|
|
|
|
}
|
|
|
|
|
2009-11-17 06:52:20 +08:00
|
|
|
llvm::BasicBlock *BadCastBlock = 0;
|
2009-11-16 14:50:58 +08:00
|
|
|
|
2009-12-17 15:09:17 +08:00
|
|
|
const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
|
2009-11-16 14:50:58 +08:00
|
|
|
|
2009-11-17 06:52:20 +08:00
|
|
|
// See if this is a dynamic_cast(void*)
|
|
|
|
if (ToVoid) {
|
|
|
|
llvm::Value *This = V;
|
2010-10-27 02:44:08 +08:00
|
|
|
V = GetVTablePtr(This, PtrDiffTy->getPointerTo());
|
2009-11-17 06:52:20 +08:00
|
|
|
V = Builder.CreateConstInBoundsGEP1_64(V, -2ULL);
|
|
|
|
V = Builder.CreateLoad(V, "offset to top");
|
2011-02-08 16:22:06 +08:00
|
|
|
This = EmitCastToVoidPtr(This);
|
2009-11-17 06:52:20 +08:00
|
|
|
V = Builder.CreateInBoundsGEP(This, V);
|
|
|
|
V = Builder.CreateBitCast(V, LTy);
|
|
|
|
} else {
|
|
|
|
/// Call __dynamic_cast
|
2011-02-08 16:22:06 +08:00
|
|
|
const llvm::Type *ResultType = Int8PtrTy;
|
2009-11-17 06:52:20 +08:00
|
|
|
const llvm::FunctionType *FTy;
|
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
2011-02-08 16:22:06 +08:00
|
|
|
ArgTys.push_back(Int8PtrTy);
|
|
|
|
ArgTys.push_back(Int8PtrTy);
|
|
|
|
ArgTys.push_back(Int8PtrTy);
|
2009-11-17 06:52:20 +08:00
|
|
|
ArgTys.push_back(PtrDiffTy);
|
|
|
|
FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
|
|
|
|
|
|
|
|
// FIXME: Calculate better hint.
|
|
|
|
llvm::Value *hint = llvm::ConstantInt::get(PtrDiffTy, -1ULL);
|
2009-12-17 15:09:17 +08:00
|
|
|
|
|
|
|
assert(SrcTy->isRecordType() && "Src type must be record type!");
|
|
|
|
assert(DestTy->isRecordType() && "Dest type must be record type!");
|
|
|
|
|
2009-12-24 06:04:40 +08:00
|
|
|
llvm::Value *SrcArg
|
|
|
|
= CGM.GetAddrOfRTTIDescriptor(SrcTy.getUnqualifiedType());
|
|
|
|
llvm::Value *DestArg
|
|
|
|
= CGM.GetAddrOfRTTIDescriptor(DestTy.getUnqualifiedType());
|
2009-12-17 15:09:17 +08:00
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
V = Builder.CreateBitCast(V, Int8PtrTy);
|
2009-11-17 06:52:20 +08:00
|
|
|
V = Builder.CreateCall4(CGM.CreateRuntimeFunction(FTy, "__dynamic_cast"),
|
2009-12-17 15:09:17 +08:00
|
|
|
V, SrcArg, DestArg, hint);
|
2009-11-17 06:52:20 +08:00
|
|
|
V = Builder.CreateBitCast(V, LTy);
|
|
|
|
|
|
|
|
if (ThrowOnBad) {
|
|
|
|
BadCastBlock = createBasicBlock();
|
2009-12-17 15:09:17 +08:00
|
|
|
Builder.CreateCondBr(Builder.CreateIsNotNull(V), ContBlock, BadCastBlock);
|
2009-11-17 06:52:20 +08:00
|
|
|
EmitBlock(BadCastBlock);
|
2010-05-15 05:14:41 +08:00
|
|
|
/// Invoke __cxa_bad_cast
|
2011-02-08 16:22:06 +08:00
|
|
|
ResultType = llvm::Type::getVoidTy(getLLVMContext());
|
2009-11-17 06:52:20 +08:00
|
|
|
const llvm::FunctionType *FBadTy;
|
2009-11-17 11:01:03 +08:00
|
|
|
FBadTy = llvm::FunctionType::get(ResultType, false);
|
2009-11-17 06:52:20 +08:00
|
|
|
llvm::Value *F = CGM.CreateRuntimeFunction(FBadTy, "__cxa_bad_cast");
|
2010-05-15 05:14:41 +08:00
|
|
|
if (llvm::BasicBlock *InvokeDest = getInvokeDest()) {
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
|
|
|
|
Builder.CreateInvoke(F, Cont, InvokeDest)->setDoesNotReturn();
|
|
|
|
EmitBlock(Cont);
|
|
|
|
} else {
|
|
|
|
// FIXME: Does this ever make sense?
|
|
|
|
Builder.CreateCall(F)->setDoesNotReturn();
|
|
|
|
}
|
2009-11-17 08:08:50 +08:00
|
|
|
Builder.CreateUnreachable();
|
2009-11-17 06:52:20 +08:00
|
|
|
}
|
2009-11-16 14:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CanBeZero) {
|
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
EmitBlock(NullBlock);
|
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
}
|
|
|
|
EmitBlock(ContBlock);
|
|
|
|
if (CanBeZero) {
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *PHI = Builder.CreatePHI(LTy, 2);
|
2009-11-16 14:50:58 +08:00
|
|
|
PHI->addIncoming(V, NonZeroBlock);
|
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(LTy), NullBlock);
|
|
|
|
V = PHI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|