2008-08-23 00:00:37 +08:00
|
|
|
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// We might split this into multiple files if it gets too unwieldy
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2009-04-14 02:03:33 +08:00
|
|
|
#include "Mangle.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-26 05:12:28 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-04 06:50:24 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-24 03:42:54 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::GenerateStaticCXXBlockVarDeclInit(const VarDecl &D,
|
|
|
|
llvm::GlobalVariable *GV) {
|
|
|
|
// FIXME: This should use __cxa_guard_{acquire,release}?
|
|
|
|
|
2008-08-23 00:00:37 +08:00
|
|
|
assert(!getContext().getLangOptions().ThreadsafeStatics &&
|
|
|
|
"thread safe statics are currently not supported!");
|
|
|
|
|
2009-04-14 02:03:33 +08:00
|
|
|
llvm::SmallString<256> GuardVName;
|
|
|
|
llvm::raw_svector_ostream GuardVOut(GuardVName);
|
|
|
|
mangleGuardVariable(&D, getContext(), GuardVOut);
|
|
|
|
|
2008-08-23 00:00:37 +08:00
|
|
|
// Create the guard variable.
|
|
|
|
llvm::GlobalValue *GuardV =
|
2009-07-09 03:05:04 +08:00
|
|
|
new llvm::GlobalVariable(CGM.getModule(), llvm::Type::Int64Ty, false,
|
2009-02-26 03:24:29 +08:00
|
|
|
GV->getLinkage(),
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Constant::getNullValue(llvm::Type::Int64Ty),
|
2009-07-09 03:05:04 +08:00
|
|
|
GuardVName.c_str());
|
2008-08-23 00:00:37 +08:00
|
|
|
|
|
|
|
// Load the first byte of the guard variable.
|
2009-07-30 06:16:19 +08:00
|
|
|
const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
|
2008-08-23 00:00:37 +08:00
|
|
|
llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
|
|
|
|
"tmp");
|
|
|
|
|
|
|
|
// Compare it against 0.
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
|
2008-08-23 00:00:37 +08:00
|
|
|
llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
|
|
|
|
|
2008-11-11 10:29:29 +08:00
|
|
|
llvm::BasicBlock *InitBlock = createBasicBlock("init");
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
|
2008-08-23 00:00:37 +08:00
|
|
|
|
|
|
|
// If the guard variable is 0, jump to the initializer code.
|
|
|
|
Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
|
|
|
|
|
|
|
|
EmitBlock(InitBlock);
|
|
|
|
|
|
|
|
const Expr *Init = D.getInit();
|
|
|
|
if (!hasAggregateLLVMType(Init->getType())) {
|
|
|
|
llvm::Value *V = EmitScalarExpr(Init);
|
|
|
|
Builder.CreateStore(V, GV, D.getType().isVolatileQualified());
|
|
|
|
} else if (Init->getType()->isAnyComplexType()) {
|
|
|
|
EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified());
|
|
|
|
} else {
|
|
|
|
EmitAggExpr(Init, GV, D.getType().isVolatileQualified());
|
|
|
|
}
|
|
|
|
|
2009-07-25 07:12:58 +08:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1),
|
2008-08-23 00:00:37 +08:00
|
|
|
Builder.CreateBitCast(GuardV, PtrTy));
|
|
|
|
|
|
|
|
EmitBlock(EndBlock);
|
|
|
|
}
|
|
|
|
|
2009-05-12 07:37:08 +08:00
|
|
|
RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
|
|
|
|
llvm::Value *Callee,
|
|
|
|
llvm::Value *This,
|
|
|
|
CallExpr::const_arg_iterator ArgBeg,
|
|
|
|
CallExpr::const_arg_iterator ArgEnd) {
|
2009-04-04 06:50:24 +08:00
|
|
|
assert(MD->isInstance() &&
|
|
|
|
"Trying to emit a member call expr on a static method!");
|
2009-05-12 07:37:08 +08:00
|
|
|
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
|
|
|
|
// Push the this ptr.
|
|
|
|
Args.push_back(std::make_pair(RValue::get(This),
|
|
|
|
MD->getThisType(getContext())));
|
|
|
|
|
|
|
|
// And the rest of the call args
|
|
|
|
EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
|
2009-04-04 06:50:24 +08:00
|
|
|
|
2009-05-12 07:37:08 +08:00
|
|
|
QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
|
|
|
|
return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
|
|
|
|
Callee, Args, MD);
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
|
|
|
|
const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
|
|
|
|
|
2009-04-09 04:31:57 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
|
2009-07-31 05:47:44 +08:00
|
|
|
|
2009-08-01 02:25:34 +08:00
|
|
|
if (MD->isVirtual()) {
|
2009-07-31 05:47:44 +08:00
|
|
|
ErrorUnsupported(CE, "virtual dispatch");
|
|
|
|
}
|
|
|
|
|
2009-04-04 06:50:24 +08:00
|
|
|
const llvm::Type *Ty =
|
2009-04-09 04:31:57 +08:00
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
|
|
|
|
FPT->isVariadic());
|
2009-05-13 05:21:08 +08:00
|
|
|
llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty);
|
2009-04-04 06:50:24 +08:00
|
|
|
|
2009-05-12 07:37:08 +08:00
|
|
|
llvm::Value *This;
|
2009-04-04 06:50:24 +08:00
|
|
|
|
|
|
|
if (ME->isArrow())
|
2009-05-12 07:37:08 +08:00
|
|
|
This = EmitScalarExpr(ME->getBase());
|
2009-04-04 06:50:24 +08:00
|
|
|
else {
|
|
|
|
LValue BaseLV = EmitLValue(ME->getBase());
|
2009-05-12 07:37:08 +08:00
|
|
|
This = BaseLV.getAddress();
|
2009-04-04 06:50:24 +08:00
|
|
|
}
|
|
|
|
|
2009-05-12 07:37:08 +08:00
|
|
|
return EmitCXXMemberCall(MD, Callee, This,
|
|
|
|
CE->arg_begin(), CE->arg_end());
|
2009-04-04 06:50:24 +08:00
|
|
|
}
|
2009-04-15 00:58:56 +08:00
|
|
|
|
2009-05-27 12:18:27 +08:00
|
|
|
RValue
|
|
|
|
CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
|
|
|
|
const CXXMethodDecl *MD) {
|
|
|
|
assert(MD->isInstance() &&
|
|
|
|
"Trying to emit a member call expr on a static method!");
|
|
|
|
|
|
|
|
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
|
|
|
|
const llvm::Type *Ty =
|
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
|
|
|
|
FPT->isVariadic());
|
|
|
|
llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty);
|
|
|
|
|
|
|
|
llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
|
|
|
|
|
|
|
|
return EmitCXXMemberCall(MD, Callee, This,
|
|
|
|
E->arg_begin() + 1, E->arg_end());
|
|
|
|
}
|
|
|
|
|
2009-04-15 00:58:56 +08:00
|
|
|
llvm::Value *CodeGenFunction::LoadCXXThis() {
|
|
|
|
assert(isa<CXXMethodDecl>(CurFuncDecl) &&
|
|
|
|
"Must be in a C++ member function decl to load 'this'");
|
|
|
|
assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
|
|
|
|
"Must be in a C++ member function decl to load 'this'");
|
|
|
|
|
|
|
|
// FIXME: What if we're inside a block?
|
2009-05-16 15:57:57 +08:00
|
|
|
// ans: See how CodeGenFunction::LoadObjCSelf() uses
|
|
|
|
// CodeGenFunction::BlockForwardSelf() for how to do this.
|
2009-04-15 00:58:56 +08:00
|
|
|
return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
|
|
|
|
}
|
2009-04-15 23:55:24 +08:00
|
|
|
|
2009-07-30 08:10:25 +08:00
|
|
|
static bool
|
|
|
|
GetNestedPaths(llvm::SmallVectorImpl<const CXXRecordDecl *> &NestedBasePaths,
|
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(),
|
|
|
|
e = ClassDecl->bases_end(); i != e; ++i) {
|
|
|
|
if (i->isVirtual())
|
|
|
|
continue;
|
|
|
|
const CXXRecordDecl *Base =
|
2009-08-05 05:58:42 +08:00
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
2009-07-30 08:10:25 +08:00
|
|
|
if (Base == BaseClassDecl) {
|
|
|
|
NestedBasePaths.push_back(BaseClassDecl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// BaseClassDecl not an immediate base of ClassDecl.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(),
|
|
|
|
e = ClassDecl->bases_end(); i != e; ++i) {
|
|
|
|
if (i->isVirtual())
|
|
|
|
continue;
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (GetNestedPaths(NestedBasePaths, Base, BaseClassDecl)) {
|
|
|
|
NestedBasePaths.push_back(Base);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-29 01:38:28 +08:00
|
|
|
llvm::Value *CodeGenFunction::AddressCXXOfBaseClass(llvm::Value *BaseValue,
|
2009-07-29 02:09:28 +08:00
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
2009-07-29 01:38:28 +08:00
|
|
|
if (ClassDecl == BaseClassDecl)
|
|
|
|
return BaseValue;
|
|
|
|
|
2009-07-30 08:10:25 +08:00
|
|
|
llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
llvm::SmallVector<const CXXRecordDecl *, 16> NestedBasePaths;
|
|
|
|
GetNestedPaths(NestedBasePaths, ClassDecl, BaseClassDecl);
|
|
|
|
assert(NestedBasePaths.size() > 0 &&
|
|
|
|
"AddressCXXOfBaseClass - inheritence path failed");
|
|
|
|
NestedBasePaths.push_back(ClassDecl);
|
|
|
|
uint64_t Offset = 0;
|
|
|
|
|
2009-07-29 01:38:28 +08:00
|
|
|
// Accessing a member of the base class. Must add delata to
|
|
|
|
// the load of 'this'.
|
2009-07-30 08:10:25 +08:00
|
|
|
for (unsigned i = NestedBasePaths.size()-1; i > 0; i--) {
|
|
|
|
const CXXRecordDecl *DerivedClass = NestedBasePaths[i];
|
|
|
|
const CXXRecordDecl *BaseClass = NestedBasePaths[i-1];
|
|
|
|
const ASTRecordLayout &Layout =
|
|
|
|
getContext().getASTRecordLayout(DerivedClass);
|
|
|
|
Offset += Layout.getBaseClassOffset(BaseClass) / 8;
|
|
|
|
}
|
2009-07-29 23:54:56 +08:00
|
|
|
llvm::Value *OffsetVal =
|
|
|
|
llvm::ConstantInt::get(
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().LongTy), Offset);
|
2009-07-29 01:38:28 +08:00
|
|
|
BaseValue = Builder.CreateBitCast(BaseValue, I8Ptr);
|
|
|
|
BaseValue = Builder.CreateGEP(BaseValue, OffsetVal, "add.ptr");
|
|
|
|
QualType BTy =
|
|
|
|
getContext().getCanonicalType(
|
2009-07-29 02:09:28 +08:00
|
|
|
getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl)));
|
2009-07-29 01:38:28 +08:00
|
|
|
const llvm::Type *BasePtr = ConvertType(BTy);
|
2009-07-30 06:16:19 +08:00
|
|
|
BasePtr = llvm::PointerType::getUnqual(BasePtr);
|
2009-07-29 01:38:28 +08:00
|
|
|
BaseValue = Builder.CreateBitCast(BaseValue, BasePtr);
|
|
|
|
return BaseValue;
|
|
|
|
}
|
|
|
|
|
2009-04-17 08:06:03 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
|
|
|
|
CXXCtorType Type,
|
|
|
|
llvm::Value *This,
|
|
|
|
CallExpr::const_arg_iterator ArgBeg,
|
|
|
|
CallExpr::const_arg_iterator ArgEnd) {
|
2009-05-12 07:37:08 +08:00
|
|
|
llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
|
|
|
|
|
|
|
|
EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
|
2009-04-17 08:06:03 +08:00
|
|
|
}
|
|
|
|
|
2009-05-30 05:03:38 +08:00
|
|
|
void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
|
|
|
|
CXXDtorType Type,
|
|
|
|
llvm::Value *This) {
|
|
|
|
llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
|
|
|
|
|
|
|
|
EmitCXXMemberCall(D, Callee, This, 0, 0);
|
|
|
|
}
|
|
|
|
|
2009-04-17 08:06:03 +08:00
|
|
|
void
|
2009-05-04 01:47:16 +08:00
|
|
|
CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
|
|
|
|
const CXXConstructExpr *E) {
|
2009-04-17 08:06:03 +08:00
|
|
|
assert(Dest && "Must have a destination!");
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD =
|
2009-07-30 05:53:49 +08:00
|
|
|
cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
|
2009-04-17 08:06:03 +08:00
|
|
|
if (RD->hasTrivialConstructor())
|
|
|
|
return;
|
2009-08-06 09:02:49 +08:00
|
|
|
|
|
|
|
// Code gen optimization to eliminate copy constructor and return
|
|
|
|
// its first argument instead.
|
2009-08-07 03:12:38 +08:00
|
|
|
if (E->isElidable()) {
|
2009-08-06 09:02:49 +08:00
|
|
|
CXXConstructExpr::const_arg_iterator i = E->arg_begin();
|
2009-08-07 03:12:38 +08:00
|
|
|
EmitAggExpr((*i), Dest, false);
|
|
|
|
return;
|
2009-08-06 09:02:49 +08:00
|
|
|
}
|
2009-04-17 08:06:03 +08:00
|
|
|
// Call the constructor.
|
|
|
|
EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
|
|
|
|
E->arg_begin(), E->arg_end());
|
|
|
|
}
|
|
|
|
|
2009-05-31 09:40:14 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
|
2009-06-01 04:21:44 +08:00
|
|
|
if (E->isArray()) {
|
|
|
|
ErrorUnsupported(E, "new[] expression");
|
2009-07-31 07:11:26 +08:00
|
|
|
return llvm::UndefValue::get(ConvertType(E->getType()));
|
2009-06-01 04:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QualType AllocType = E->getAllocatedType();
|
|
|
|
FunctionDecl *NewFD = E->getOperatorNew();
|
|
|
|
const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType();
|
|
|
|
|
|
|
|
CallArgList NewArgs;
|
|
|
|
|
|
|
|
// The allocation size is the first argument.
|
|
|
|
QualType SizeTy = getContext().getSizeType();
|
|
|
|
llvm::Value *AllocSize =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(ConvertType(SizeTy),
|
2009-06-01 04:21:44 +08:00
|
|
|
getContext().getTypeSize(AllocType) / 8);
|
|
|
|
|
|
|
|
NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy));
|
|
|
|
|
|
|
|
// Emit the rest of the arguments.
|
|
|
|
// FIXME: Ideally, this should just use EmitCallArgs.
|
|
|
|
CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin();
|
|
|
|
|
|
|
|
// First, use the types from the function type.
|
|
|
|
// We start at 1 here because the first argument (the allocation size)
|
|
|
|
// has already been emitted.
|
|
|
|
for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) {
|
|
|
|
QualType ArgType = NewFTy->getArgType(i);
|
|
|
|
|
|
|
|
assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
|
|
|
|
getTypePtr() ==
|
|
|
|
getContext().getCanonicalType(NewArg->getType()).getTypePtr() &&
|
|
|
|
"type mismatch in call argument!");
|
|
|
|
|
|
|
|
NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
|
|
|
|
ArgType));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either we've emitted all the call args, or we have a call to a
|
|
|
|
// variadic function.
|
|
|
|
assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) &&
|
|
|
|
"Extra arguments in non-variadic function!");
|
|
|
|
|
|
|
|
// If we still have any arguments, emit them using the type of the argument.
|
|
|
|
for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end();
|
|
|
|
NewArg != NewArgEnd; ++NewArg) {
|
|
|
|
QualType ArgType = NewArg->getType();
|
|
|
|
NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
|
|
|
|
ArgType));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the call to new.
|
|
|
|
RValue RV =
|
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs),
|
|
|
|
CGM.GetAddrOfFunction(GlobalDecl(NewFD)),
|
|
|
|
NewArgs, NewFD);
|
|
|
|
|
2009-06-01 05:53:59 +08:00
|
|
|
// If an allocation function is declared with an empty exception specification
|
|
|
|
// it returns null to indicate failure to allocate storage. [expr.new]p13.
|
|
|
|
// (We don't need to check for null when there's no new initializer and
|
|
|
|
// we're allocating a POD type).
|
|
|
|
bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
|
|
|
|
!(AllocType->isPODType() && !E->hasInitializer());
|
2009-06-01 04:21:44 +08:00
|
|
|
|
2009-06-01 08:05:16 +08:00
|
|
|
llvm::BasicBlock *NewNull = 0;
|
|
|
|
llvm::BasicBlock *NewNotNull = 0;
|
|
|
|
llvm::BasicBlock *NewEnd = 0;
|
|
|
|
|
|
|
|
llvm::Value *NewPtr = RV.getScalarVal();
|
|
|
|
|
2009-06-01 05:53:59 +08:00
|
|
|
if (NullCheckResult) {
|
2009-06-01 08:05:16 +08:00
|
|
|
NewNull = createBasicBlock("new.null");
|
|
|
|
NewNotNull = createBasicBlock("new.notnull");
|
|
|
|
NewEnd = createBasicBlock("new.end");
|
|
|
|
|
|
|
|
llvm::Value *IsNull =
|
|
|
|
Builder.CreateICmpEQ(NewPtr,
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Constant::getNullValue(NewPtr->getType()),
|
2009-06-01 08:05:16 +08:00
|
|
|
"isnull");
|
|
|
|
|
|
|
|
Builder.CreateCondBr(IsNull, NewNull, NewNotNull);
|
|
|
|
EmitBlock(NewNotNull);
|
2009-06-01 05:53:59 +08:00
|
|
|
}
|
|
|
|
|
2009-06-01 08:05:16 +08:00
|
|
|
NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType()));
|
2009-06-01 05:53:59 +08:00
|
|
|
|
2009-06-01 04:56:36 +08:00
|
|
|
if (AllocType->isPODType()) {
|
2009-06-01 08:26:14 +08:00
|
|
|
if (E->getNumConstructorArgs() > 0) {
|
2009-06-01 04:56:36 +08:00
|
|
|
assert(E->getNumConstructorArgs() == 1 &&
|
|
|
|
"Can only have one argument to initializer of POD type.");
|
|
|
|
|
|
|
|
const Expr *Init = E->getConstructorArg(0);
|
|
|
|
|
2009-06-01 05:07:58 +08:00
|
|
|
if (!hasAggregateLLVMType(AllocType))
|
2009-06-01 04:56:36 +08:00
|
|
|
Builder.CreateStore(EmitScalarExpr(Init), NewPtr);
|
2009-06-01 05:07:58 +08:00
|
|
|
else if (AllocType->isAnyComplexType())
|
|
|
|
EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified());
|
2009-06-01 05:12:26 +08:00
|
|
|
else
|
|
|
|
EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified());
|
2009-06-01 04:56:36 +08:00
|
|
|
}
|
2009-06-01 05:53:59 +08:00
|
|
|
} else {
|
|
|
|
// Call the constructor.
|
|
|
|
CXXConstructorDecl *Ctor = E->getConstructor();
|
2009-06-01 04:56:36 +08:00
|
|
|
|
2009-06-01 05:53:59 +08:00
|
|
|
EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr,
|
|
|
|
E->constructor_arg_begin(),
|
|
|
|
E->constructor_arg_end());
|
2009-06-01 04:21:44 +08:00
|
|
|
}
|
2009-06-01 05:53:59 +08:00
|
|
|
|
2009-06-01 08:05:16 +08:00
|
|
|
if (NullCheckResult) {
|
|
|
|
Builder.CreateBr(NewEnd);
|
|
|
|
EmitBlock(NewNull);
|
|
|
|
Builder.CreateBr(NewEnd);
|
|
|
|
EmitBlock(NewEnd);
|
|
|
|
|
|
|
|
llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
|
|
|
|
PHI->reserveOperandSpace(2);
|
|
|
|
PHI->addIncoming(NewPtr, NewNotNull);
|
2009-08-01 04:28:54 +08:00
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull);
|
2009-06-01 08:05:16 +08:00
|
|
|
|
|
|
|
NewPtr = PHI;
|
|
|
|
}
|
|
|
|
|
2009-06-01 05:53:59 +08:00
|
|
|
return NewPtr;
|
2009-05-31 09:40:14 +08:00
|
|
|
}
|
|
|
|
|
2009-04-17 09:58:57 +08:00
|
|
|
static bool canGenerateCXXstructor(const CXXRecordDecl *RD,
|
|
|
|
ASTContext &Context) {
|
2009-04-16 05:02:13 +08:00
|
|
|
// The class has base classes - we don't support that right now.
|
|
|
|
if (RD->getNumBases() > 0)
|
|
|
|
return false;
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
|
|
|
|
I != E; ++I) {
|
2009-04-16 05:02:13 +08:00
|
|
|
// We don't support ctors for fields that aren't POD.
|
|
|
|
if (!I->getType()->isPODType())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-15 23:55:24 +08:00
|
|
|
void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
|
2009-04-17 09:58:57 +08:00
|
|
|
if (!canGenerateCXXstructor(D->getParent(), getContext())) {
|
2009-04-16 05:02:13 +08:00
|
|
|
ErrorUnsupported(D, "C++ constructor", true);
|
|
|
|
return;
|
|
|
|
}
|
2009-04-15 23:55:24 +08:00
|
|
|
|
2009-05-05 12:44:02 +08:00
|
|
|
EmitGlobal(GlobalDecl(D, Ctor_Complete));
|
|
|
|
EmitGlobal(GlobalDecl(D, Ctor_Base));
|
2009-04-15 23:55:24 +08:00
|
|
|
}
|
2009-04-17 07:57:24 +08:00
|
|
|
|
2009-04-17 09:58:57 +08:00
|
|
|
void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
|
|
|
|
CXXCtorType Type) {
|
|
|
|
|
|
|
|
llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
|
|
|
|
|
|
|
|
CodeGenFunction(*this).GenerateCode(D, Fn);
|
|
|
|
|
|
|
|
SetFunctionDefinitionAttributes(D, Fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, Fn);
|
|
|
|
}
|
|
|
|
|
2009-04-17 07:57:24 +08:00
|
|
|
llvm::Function *
|
|
|
|
CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
|
|
|
|
CXXCtorType Type) {
|
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
|
|
|
|
|
|
|
|
const char *Name = getMangledCXXCtorName(D, Type);
|
2009-05-13 05:21:08 +08:00
|
|
|
return cast<llvm::Function>(
|
|
|
|
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
|
2009-04-17 07:57:24 +08:00
|
|
|
}
|
2009-04-17 09:58:57 +08:00
|
|
|
|
|
|
|
const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
|
|
|
|
CXXCtorType Type) {
|
|
|
|
llvm::SmallString<256> Name;
|
|
|
|
llvm::raw_svector_ostream Out(Name);
|
|
|
|
mangleCXXCtor(D, Type, Context, Out);
|
|
|
|
|
|
|
|
Name += '\0';
|
|
|
|
return UniqueMangledName(Name.begin(), Name.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
|
|
|
|
if (!canGenerateCXXstructor(D->getParent(), getContext())) {
|
|
|
|
ErrorUnsupported(D, "C++ destructor", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitCXXDestructor(D, Dtor_Complete);
|
|
|
|
EmitCXXDestructor(D, Dtor_Base);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
|
|
|
|
CXXDtorType Type) {
|
|
|
|
llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
|
|
|
|
|
|
|
|
CodeGenFunction(*this).GenerateCode(D, Fn);
|
|
|
|
|
|
|
|
SetFunctionDefinitionAttributes(D, Fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, Fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Function *
|
|
|
|
CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
|
|
|
|
CXXDtorType Type) {
|
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
|
|
|
|
|
|
|
|
const char *Name = getMangledCXXDtorName(D, Type);
|
2009-05-13 05:21:08 +08:00
|
|
|
return cast<llvm::Function>(
|
|
|
|
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
|
2009-04-17 09:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
|
|
|
|
CXXDtorType Type) {
|
|
|
|
llvm::SmallString<256> Name;
|
|
|
|
llvm::raw_svector_ostream Out(Name);
|
|
|
|
mangleCXXDtor(D, Type, Context, Out);
|
|
|
|
|
|
|
|
Name += '\0';
|
|
|
|
return UniqueMangledName(Name.begin(), Name.end());
|
|
|
|
}
|
2009-07-21 07:18:55 +08:00
|
|
|
|
2009-08-01 07:15:31 +08:00
|
|
|
llvm::Constant *CodeGenFunction::GenerateRtti(const CXXRecordDecl *RD) {
|
|
|
|
llvm::Type *Ptr8Ty;
|
|
|
|
Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
|
2009-08-05 04:06:48 +08:00
|
|
|
llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
|
2009-08-01 07:15:31 +08:00
|
|
|
|
|
|
|
if (!getContext().getLangOptions().Rtti)
|
2009-08-05 04:06:48 +08:00
|
|
|
return Rtti;
|
2009-08-01 07:15:31 +08:00
|
|
|
|
|
|
|
llvm::SmallString<256> OutName;
|
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
|
|
|
QualType ClassTy;
|
2009-08-08 02:05:12 +08:00
|
|
|
ClassTy = getContext().getTagDeclType(RD);
|
2009-08-01 07:15:31 +08:00
|
|
|
mangleCXXRtti(ClassTy, getContext(), Out);
|
|
|
|
const char *Name = OutName.c_str();
|
|
|
|
llvm::GlobalVariable::LinkageTypes linktype;
|
|
|
|
linktype = llvm::GlobalValue::WeakAnyLinkage;
|
|
|
|
std::vector<llvm::Constant *> info;
|
2009-08-07 05:49:36 +08:00
|
|
|
// assert (0 && "FIXME: implement rtti descriptor");
|
2009-08-01 07:15:31 +08:00
|
|
|
// FIXME: descriptor
|
|
|
|
info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
|
2009-08-07 05:49:36 +08:00
|
|
|
// assert (0 && "FIXME: implement rtti ts");
|
2009-08-01 07:15:31 +08:00
|
|
|
// FIXME: TS
|
|
|
|
info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
|
|
|
|
|
|
|
|
llvm::Constant *C;
|
|
|
|
llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
|
|
|
|
C = llvm::ConstantArray::get(type, info);
|
2009-08-05 04:06:48 +08:00
|
|
|
Rtti = new llvm::GlobalVariable(CGM.getModule(), type, true, linktype, C,
|
2009-08-01 07:15:31 +08:00
|
|
|
Name);
|
2009-08-05 04:06:48 +08:00
|
|
|
Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
|
|
|
|
return Rtti;
|
2009-08-01 07:15:31 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 23:50:11 +08:00
|
|
|
void CodeGenFunction::GenerateVtableForBase(const CXXRecordDecl *RD,
|
2009-08-07 05:49:36 +08:00
|
|
|
const CXXRecordDecl *Class,
|
|
|
|
llvm::Constant *rtti,
|
|
|
|
std::vector<llvm::Constant *> &methods,
|
2009-08-07 07:48:32 +08:00
|
|
|
bool isPrimary,
|
|
|
|
bool ForVirtualBase) {
|
2009-08-06 23:50:11 +08:00
|
|
|
typedef CXXRecordDecl::method_iterator meth_iter;
|
|
|
|
llvm::Type *Ptr8Ty;
|
|
|
|
Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
|
2009-08-07 05:49:36 +08:00
|
|
|
llvm::Constant *m = llvm::Constant::getNullValue(Ptr8Ty);
|
|
|
|
|
|
|
|
if (RD && !RD->isDynamicClass())
|
|
|
|
return;
|
2009-08-07 07:48:32 +08:00
|
|
|
|
|
|
|
if (RD && ForVirtualBase)
|
|
|
|
for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
|
|
|
|
++mi) {
|
|
|
|
if (mi->isVirtual()) {
|
|
|
|
// FIXME: vcall: offset for virtual base for this function
|
|
|
|
m = llvm::Constant::getNullValue(Ptr8Ty);
|
|
|
|
methods.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isPrimary && ForVirtualBase)
|
|
|
|
for (meth_iter mi = Class->method_begin(),
|
|
|
|
me = Class->method_end(); mi != me; ++mi) {
|
|
|
|
if (mi->isVirtual()) {
|
|
|
|
// FIXME: vcall: offset for virtual base for this function
|
|
|
|
m = llvm::Constant::getNullValue(Ptr8Ty);
|
|
|
|
methods.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 05:49:36 +08:00
|
|
|
if (RD) {
|
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Class);
|
|
|
|
int64_t BaseOffset = -(Layout.getBaseClassOffset(RD) / 8);
|
|
|
|
m = llvm::ConstantInt::get(llvm::Type::Int64Ty, BaseOffset);
|
|
|
|
m = llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
|
|
|
|
}
|
|
|
|
methods.push_back(m);
|
|
|
|
methods.push_back(rtti);
|
|
|
|
|
|
|
|
if (RD)
|
|
|
|
for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
|
|
|
|
++mi) {
|
|
|
|
if (mi->isVirtual()) {
|
|
|
|
m = CGM.GetAddrOfFunction(GlobalDecl(*mi));
|
|
|
|
m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
|
|
|
|
methods.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isPrimary)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// And add the virtuals for the class to the primary vtable.
|
2009-08-07 07:48:32 +08:00
|
|
|
for (meth_iter mi = Class->method_begin(), me = Class->method_end(); mi != me;
|
2009-08-06 23:50:11 +08:00
|
|
|
++mi) {
|
|
|
|
if (mi->isVirtual()) {
|
|
|
|
m = CGM.GetAddrOfFunction(GlobalDecl(*mi));
|
|
|
|
m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
|
|
|
|
methods.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-01 02:25:34 +08:00
|
|
|
llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
|
|
|
|
llvm::SmallString<256> OutName;
|
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
|
|
|
QualType ClassTy;
|
2009-08-08 02:05:12 +08:00
|
|
|
ClassTy = getContext().getTagDeclType(RD);
|
2009-08-01 02:25:34 +08:00
|
|
|
mangleCXXVtable(ClassTy, getContext(), Out);
|
|
|
|
const char *Name = OutName.c_str();
|
2009-08-01 05:43:43 +08:00
|
|
|
llvm::GlobalVariable::LinkageTypes linktype;
|
|
|
|
linktype = llvm::GlobalValue::WeakAnyLinkage;
|
|
|
|
std::vector<llvm::Constant *> methods;
|
2009-08-01 02:25:34 +08:00
|
|
|
typedef CXXRecordDecl::method_iterator meth_iter;
|
2009-08-01 05:43:43 +08:00
|
|
|
llvm::Type *Ptr8Ty;
|
|
|
|
Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
|
2009-08-06 06:37:18 +08:00
|
|
|
int64_t Offset = 0;
|
2009-08-07 05:49:36 +08:00
|
|
|
llvm::Constant *rtti = GenerateRtti(RD);
|
|
|
|
|
|
|
|
Offset += LLVMPointerWidth;
|
|
|
|
Offset += LLVMPointerWidth;
|
2009-08-06 06:37:18 +08:00
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2009-08-08 04:14:05 +08:00
|
|
|
const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
|
2009-08-05 05:58:42 +08:00
|
|
|
|
2009-08-08 05:54:03 +08:00
|
|
|
// The virtual base offsets come first.
|
|
|
|
for (CXXRecordDecl::reverse_base_class_const_iterator i = RD->vbases_rbegin(),
|
|
|
|
e = RD->vbases_rend(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
int64_t BaseOffset = Layout.getBaseClassOffset(Base) / 8;
|
|
|
|
llvm::Constant *m;
|
|
|
|
m = llvm::ConstantInt::get(llvm::Type::Int64Ty, BaseOffset);
|
|
|
|
m = llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
|
|
|
|
methods.push_back(m);
|
|
|
|
}
|
|
|
|
|
2009-08-07 02:05:22 +08:00
|
|
|
// The primary base comes first.
|
2009-08-08 03:00:50 +08:00
|
|
|
GenerateVtableForBase(PrimaryBase, RD, rtti, methods, true,
|
|
|
|
PrimaryBaseWasVirtual);
|
2009-08-05 05:58:42 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
if (i->isVirtual())
|
|
|
|
continue;
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
2009-08-06 06:37:18 +08:00
|
|
|
if (PrimaryBase != Base) {
|
2009-08-07 05:49:36 +08:00
|
|
|
GenerateVtableForBase(Base, RD, rtti, methods);
|
2009-08-01 05:43:43 +08:00
|
|
|
}
|
2009-08-01 02:25:34 +08:00
|
|
|
}
|
2009-08-07 02:05:22 +08:00
|
|
|
|
2009-08-06 23:50:11 +08:00
|
|
|
// FIXME: finish layout for virtual bases
|
|
|
|
// FIXME: audit indirect virtual bases
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(),
|
|
|
|
e = RD->vbases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (Base != PrimaryBase)
|
2009-08-07 07:48:32 +08:00
|
|
|
GenerateVtableForBase(Base, RD, rtti, methods, false, true);
|
2009-08-06 23:50:11 +08:00
|
|
|
}
|
2009-08-06 06:37:18 +08:00
|
|
|
|
2009-08-01 05:43:43 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
|
|
|
|
C = llvm::ConstantArray::get(type, methods);
|
|
|
|
llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
|
|
|
|
linktype, C, Name);
|
2009-08-01 02:25:34 +08:00
|
|
|
vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
|
|
|
|
vtable = Builder.CreateGEP(vtable,
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int64Ty,
|
2009-08-06 06:37:18 +08:00
|
|
|
Offset/8));
|
2009-08-01 02:25:34 +08:00
|
|
|
return vtable;
|
|
|
|
}
|
|
|
|
|
2009-08-08 07:51:33 +08:00
|
|
|
/// EmitClassMemberwiseCopy - This routine generates code to copy a class
|
|
|
|
/// object from SrcValue to DestValue. Copying can be either a bitwise copy
|
|
|
|
/// of via a copy constructor call.
|
|
|
|
void CodeGenFunction::EmitClassMemberwiseCopy(
|
|
|
|
llvm::Value *DestValue, llvm::Value *SrcValue,
|
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
|
|
|
// FIXME. Do bitwise copy of trivial copy constructors.
|
|
|
|
if (BaseClassDecl->hasTrivialCopyConstructor())
|
|
|
|
return;
|
|
|
|
if (CXXConstructorDecl *BaseCopyCtor =
|
2009-08-08 08:59:58 +08:00
|
|
|
BaseClassDecl->getCopyConstructor(getContext(), 0)) {
|
2009-08-08 07:51:33 +08:00
|
|
|
llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
|
|
|
|
Ctor_Complete);
|
|
|
|
|
2009-08-08 08:15:41 +08:00
|
|
|
llvm::Value *Dest = ClassDecl ?
|
|
|
|
AddressCXXOfBaseClass(DestValue, ClassDecl, BaseClassDecl) : DestValue;
|
2009-08-08 07:51:33 +08:00
|
|
|
|
|
|
|
CallArgList CallArgs;
|
|
|
|
// Push the this (Dest) ptr.
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(Dest),
|
|
|
|
BaseCopyCtor->getThisType(getContext())));
|
|
|
|
|
2009-08-08 08:15:41 +08:00
|
|
|
llvm::Value *Src = ClassDecl ?
|
|
|
|
AddressCXXOfBaseClass(SrcValue, ClassDecl, BaseClassDecl) : SrcValue;
|
2009-08-08 07:51:33 +08:00
|
|
|
// Push the Src ptr.
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(Src),
|
|
|
|
BaseCopyCtor->getThisType(getContext())));
|
|
|
|
QualType ResultType =
|
|
|
|
BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
|
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
|
|
|
|
Callee, CallArgs, BaseCopyCtor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-08 04:22:40 +08:00
|
|
|
/// EmitCopyCtorBody - This routine implicitly defines body of a copy
|
|
|
|
/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
|
|
|
|
/// The implicitly-defined copy constructor for class X performs a memberwise
|
|
|
|
/// copy of its subobjects. The order of copying is the same as the order
|
|
|
|
/// of initialization of bases and members in a user-defined constructor
|
|
|
|
/// Each subobject is copied in the manner appropriate to its type:
|
|
|
|
/// if the subobject is of class type, the copy constructor for the class is
|
|
|
|
/// used;
|
|
|
|
/// if the subobject is an array, each element is copied, in the manner
|
|
|
|
/// appropriate to the element type;
|
|
|
|
/// if the subobject is of scalar type, the built-in assignment operator is
|
|
|
|
/// used.
|
|
|
|
/// Virtual base class subobjects shall be copied only once by the
|
|
|
|
/// implicitly-defined copy constructor
|
|
|
|
|
2009-08-08 07:51:33 +08:00
|
|
|
void CodeGenFunction::EmitCopyCtorBody(const CXXConstructorDecl *CD,
|
|
|
|
const FunctionArgList &Args) {
|
2009-08-08 04:22:40 +08:00
|
|
|
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
|
|
|
|
assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
|
|
|
|
"EmitCopyCtorBody - copy constructor has definition already");
|
|
|
|
|
2009-08-08 08:15:41 +08:00
|
|
|
FunctionArgList::const_iterator i = Args.begin();
|
|
|
|
const VarDecl *ThisArg = i->first;
|
|
|
|
llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
|
|
|
|
llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
|
|
|
|
const VarDecl *SrcArg = (i+1)->first;
|
|
|
|
llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
|
|
|
|
llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
|
|
|
|
|
2009-08-08 04:22:40 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
|
|
|
|
Base != ClassDecl->bases_end(); ++Base) {
|
|
|
|
// FIXME. copy constrution of virtual base NYI
|
|
|
|
if (Base->isVirtual())
|
|
|
|
continue;
|
2009-08-08 07:51:33 +08:00
|
|
|
|
2009-08-08 04:22:40 +08:00
|
|
|
CXXRecordDecl *BaseClassDecl
|
|
|
|
= cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
|
2009-08-08 07:51:33 +08:00
|
|
|
EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl);
|
2009-08-08 04:22:40 +08:00
|
|
|
}
|
|
|
|
|
2009-08-08 08:15:41 +08:00
|
|
|
for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
|
|
|
|
FieldEnd = ClassDecl->field_end();
|
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
QualType FieldType = getContext().getCanonicalType((*Field)->getType());
|
|
|
|
|
|
|
|
// FIXME. How about copying arrays!
|
|
|
|
assert(!getContext().getAsArrayType(FieldType) &&
|
|
|
|
"FIXME. Copying arrays NYI");
|
|
|
|
|
|
|
|
if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
|
|
|
|
CXXRecordDecl *FieldClassDecl
|
|
|
|
= cast<CXXRecordDecl>(FieldClassType->getDecl());
|
|
|
|
LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
|
|
|
|
LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
|
|
|
|
EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
|
|
|
|
0 /*ClassDecl*/, FieldClassDecl);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// FIXME. Do a built-in assignment of scalar data members.
|
|
|
|
}
|
2009-08-08 04:22:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-21 07:18:55 +08:00
|
|
|
/// EmitCtorPrologue - This routine generates necessary code to initialize
|
|
|
|
/// base classes and non-static data members belonging to this constructor.
|
|
|
|
void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
|
2009-07-26 05:12:28 +08:00
|
|
|
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
|
2009-08-06 21:41:24 +08:00
|
|
|
// FIXME: Add vbase initialization
|
2009-08-01 02:25:34 +08:00
|
|
|
llvm::Value *LoadOfThis = 0;
|
2009-07-29 02:09:28 +08:00
|
|
|
|
2009-07-26 05:12:28 +08:00
|
|
|
for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
|
2009-07-21 07:18:55 +08:00
|
|
|
E = CD->init_end();
|
|
|
|
B != E; ++B) {
|
|
|
|
CXXBaseOrMemberInitializer *Member = (*B);
|
|
|
|
if (Member->isBaseInitializer()) {
|
2009-08-01 02:25:34 +08:00
|
|
|
LoadOfThis = LoadCXXThis();
|
2009-07-29 02:09:28 +08:00
|
|
|
Type *BaseType = Member->getBaseClass();
|
|
|
|
CXXRecordDecl *BaseClassDecl =
|
2009-07-30 05:53:49 +08:00
|
|
|
cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
|
2009-07-29 02:09:28 +08:00
|
|
|
llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
|
|
|
|
BaseClassDecl);
|
2009-07-26 05:12:28 +08:00
|
|
|
EmitCXXConstructorCall(Member->getConstructor(),
|
|
|
|
Ctor_Complete, V,
|
|
|
|
Member->const_arg_begin(),
|
|
|
|
Member->const_arg_end());
|
2009-07-31 06:28:39 +08:00
|
|
|
} else {
|
2009-07-21 07:18:55 +08:00
|
|
|
// non-static data member initilaizers.
|
|
|
|
FieldDecl *Field = Member->getMember();
|
|
|
|
QualType FieldType = getContext().getCanonicalType((Field)->getType());
|
|
|
|
assert(!getContext().getAsArrayType(FieldType)
|
|
|
|
&& "FIXME. Field arrays initialization unsupported");
|
2009-07-25 01:57:02 +08:00
|
|
|
|
2009-08-01 02:25:34 +08:00
|
|
|
LoadOfThis = LoadCXXThis();
|
2009-07-21 07:18:55 +08:00
|
|
|
LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
|
2009-07-30 05:53:49 +08:00
|
|
|
if (FieldType->getAs<RecordType>()) {
|
2009-07-25 01:57:02 +08:00
|
|
|
|
|
|
|
assert(Member->getConstructor() &&
|
|
|
|
"EmitCtorPrologue - no constructor to initialize member");
|
|
|
|
EmitCXXConstructorCall(Member->getConstructor(),
|
|
|
|
Ctor_Complete, LHS.getAddress(),
|
|
|
|
Member->const_arg_begin(),
|
|
|
|
Member->const_arg_end());
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-21 07:18:55 +08:00
|
|
|
|
|
|
|
assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
|
2009-07-25 01:57:02 +08:00
|
|
|
Expr *RhsExpr = *Member->arg_begin();
|
2009-07-21 07:18:55 +08:00
|
|
|
llvm::Value *RHS = EmitScalarExpr(RhsExpr, true);
|
|
|
|
if (LHS.isBitfield())
|
|
|
|
EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, FieldType, 0);
|
|
|
|
else
|
|
|
|
EmitStoreThroughLValue(RValue::get(RHS), LHS, FieldType);
|
|
|
|
}
|
|
|
|
}
|
2009-08-01 02:25:34 +08:00
|
|
|
|
|
|
|
// Initialize the vtable pointer
|
2009-08-06 06:59:44 +08:00
|
|
|
if (ClassDecl->isDynamicClass()) {
|
2009-08-01 02:25:34 +08:00
|
|
|
if (!LoadOfThis)
|
|
|
|
LoadOfThis = LoadCXXThis();
|
|
|
|
llvm::Value *VtableField;
|
|
|
|
llvm::Type *Ptr8Ty, *PtrPtr8Ty;
|
|
|
|
Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
|
|
|
|
PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
|
|
|
|
VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
|
|
|
|
llvm::Value *vtable = GenerateVtable(ClassDecl);
|
|
|
|
Builder.CreateStore(vtable, VtableField);
|
|
|
|
}
|
2009-07-21 07:18:55 +08:00
|
|
|
}
|
2009-07-31 01:49:11 +08:00
|
|
|
|
|
|
|
/// EmitDtorEpilogue - Emit all code that comes at the end of class's
|
|
|
|
/// destructor. This is to call destructors on members and base classes
|
|
|
|
/// in reverse order of their construction.
|
|
|
|
void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) {
|
|
|
|
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
|
|
|
|
assert(!ClassDecl->isPolymorphic() &&
|
|
|
|
"FIXME. polymorphic destruction not supported");
|
|
|
|
(void)ClassDecl; // prevent warning.
|
|
|
|
|
|
|
|
for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
|
|
|
|
*E = DD->destr_end(); B != E; ++B) {
|
|
|
|
uintptr_t BaseOrMember = (*B);
|
|
|
|
if (DD->isMemberToDestroy(BaseOrMember)) {
|
|
|
|
FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
|
|
|
|
QualType FieldType = getContext().getCanonicalType((FD)->getType());
|
|
|
|
assert(!getContext().getAsArrayType(FieldType)
|
|
|
|
&& "FIXME. Field arrays destruction unsupported");
|
|
|
|
const RecordType *RT = FieldType->getAs<RecordType>();
|
|
|
|
CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (FieldClassDecl->hasTrivialDestructor())
|
|
|
|
continue;
|
|
|
|
llvm::Value *LoadOfThis = LoadCXXThis();
|
|
|
|
LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
|
|
|
|
EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
|
|
|
|
Dtor_Complete, LHS.getAddress());
|
2009-07-31 06:28:39 +08:00
|
|
|
} else {
|
2009-07-31 01:49:11 +08:00
|
|
|
const RecordType *RT =
|
|
|
|
DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
|
|
|
|
CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (BaseClassDecl->hasTrivialDestructor())
|
|
|
|
continue;
|
|
|
|
llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
|
|
|
|
ClassDecl,BaseClassDecl);
|
|
|
|
EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
|
|
|
|
Dtor_Complete, V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|