2008-08-22 16:00:37 +00: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-13 18:03:33 +00:00
|
|
|
#include "Mangle.h"
|
2008-08-22 16:00:37 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-03 22:50:24 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-23 19:42:54 +00:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-08-22 16:00:37 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-02-25 19:24:29 +00:00
|
|
|
void
|
|
|
|
CodeGenFunction::GenerateStaticCXXBlockVarDeclInit(const VarDecl &D,
|
|
|
|
llvm::GlobalVariable *GV) {
|
|
|
|
// FIXME: This should use __cxa_guard_{acquire,release}?
|
|
|
|
|
2008-08-22 16:00:37 +00:00
|
|
|
assert(!getContext().getLangOptions().ThreadsafeStatics &&
|
|
|
|
"thread safe statics are currently not supported!");
|
|
|
|
|
2009-04-13 18:03:33 +00:00
|
|
|
llvm::SmallString<256> GuardVName;
|
|
|
|
llvm::raw_svector_ostream GuardVOut(GuardVName);
|
|
|
|
mangleGuardVariable(&D, getContext(), GuardVOut);
|
|
|
|
|
2008-08-22 16:00:37 +00:00
|
|
|
// Create the guard variable.
|
|
|
|
llvm::GlobalValue *GuardV =
|
|
|
|
new llvm::GlobalVariable(llvm::Type::Int64Ty, false,
|
2009-02-25 19:24:29 +00:00
|
|
|
GV->getLinkage(),
|
2008-08-22 16:00:37 +00:00
|
|
|
llvm::Constant::getNullValue(llvm::Type::Int64Ty),
|
2009-04-13 18:03:33 +00:00
|
|
|
GuardVName.c_str(),
|
2008-08-22 16:00:37 +00:00
|
|
|
&CGM.getModule());
|
|
|
|
|
|
|
|
// Load the first byte of the guard variable.
|
2009-02-25 19:24:29 +00:00
|
|
|
const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0);
|
2008-08-22 16:00:37 +00: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 02:29:29 +00:00
|
|
|
llvm::BasicBlock *InitBlock = createBasicBlock("init");
|
2008-11-13 01:38:36 +00:00
|
|
|
llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
|
2008-08-22 16:00:37 +00: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-04-03 22:50:24 +00:00
|
|
|
RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
|
|
|
|
const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
|
|
|
|
assert(MD->isInstance() &&
|
|
|
|
"Trying to emit a member call expr on a static method!");
|
|
|
|
|
2009-04-08 20:31:57 +00:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
|
2009-04-03 22:50:24 +00:00
|
|
|
const llvm::Type *Ty =
|
2009-04-08 20:31:57 +00:00
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
|
|
|
|
FPT->isVariadic());
|
2009-04-03 22:50:24 +00:00
|
|
|
llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
|
|
|
|
|
|
|
|
llvm::Value *BaseValue = 0;
|
|
|
|
|
|
|
|
// There's a deref operator node added in Sema::BuildCallToMemberFunction
|
|
|
|
// that's giving the wrong type for -> call exprs so we just ignore them
|
|
|
|
// for now.
|
|
|
|
if (ME->isArrow())
|
|
|
|
return EmitUnsupportedRValue(CE, "C++ member call expr");
|
|
|
|
else {
|
|
|
|
LValue BaseLV = EmitLValue(ME->getBase());
|
|
|
|
BaseValue = BaseLV.getAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
|
|
|
|
// Push the 'this' pointer.
|
|
|
|
Args.push_back(std::make_pair(RValue::get(BaseValue),
|
|
|
|
MD->getThisType(getContext())));
|
|
|
|
|
2009-04-08 23:13:16 +00:00
|
|
|
EmitCallArgs(Args, FPT, CE->arg_begin(), CE->arg_end());
|
2009-04-03 22:50:24 +00:00
|
|
|
|
|
|
|
QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
|
2009-04-08 20:31:57 +00:00
|
|
|
return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
|
|
|
|
Callee, Args, MD);
|
2009-04-03 22:50:24 +00:00
|
|
|
}
|
2009-04-14 16:58:56 +00: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");
|
|
|
|
}
|