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"
|
|
|
|
#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 =
|
|
|
|
new llvm::GlobalVariable(llvm::Type::Int64Ty, false,
|
2009-02-26 03:24:29 +08:00
|
|
|
GV->getLinkage(),
|
2008-08-23 00:00:37 +08:00
|
|
|
llvm::Constant::getNullValue(llvm::Type::Int64Ty),
|
2009-04-14 02:03:33 +08:00
|
|
|
GuardVName.c_str(),
|
2008-08-23 00:00:37 +08:00
|
|
|
&CGM.getModule());
|
|
|
|
|
|
|
|
// Load the first byte of the guard variable.
|
2009-02-26 03:24:29 +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.
|
|
|
|
llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1),
|
|
|
|
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-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-04-04 06:50:24 +08:00
|
|
|
llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
|
|
|
|
|
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
|
|
|
|
|
|
|
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?
|
|
|
|
return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
|
|
|
|
}
|
2009-04-15 23:55:24 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 =
|
|
|
|
cast<CXXRecordDecl>(E->getType()->getAsRecordType()->getDecl());
|
|
|
|
if (RD->hasTrivialConstructor())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Call the constructor.
|
|
|
|
EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
|
|
|
|
E->arg_begin(), E->arg_end());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (CXXRecordDecl::field_iterator I = RD->field_begin(Context),
|
|
|
|
E = RD->field_end(Context); I != E; ++I) {
|
|
|
|
// 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-12 07:37:08 +08:00
|
|
|
|
2009-04-17 07:57:24 +08:00
|
|
|
return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, D));
|
|
|
|
}
|
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);
|
|
|
|
return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, D));
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|