2007-06-02 12:16:21 +08:00
|
|
|
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-02 12:16:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Decl nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-30 18:30:31 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2007-10-17 08:52:43 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-12-22 22:23:30 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-08-25 09:38:19 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-04-19 12:17:09 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-05-08 13:58:21 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-11-13 01:24:48 +08:00
|
|
|
#include "clang/CodeGen/CodeGenOptions.h"
|
2007-10-17 08:52:43 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-12-12 15:38:43 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-03-04 11:23:46 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-06-14 04:44:40 +08:00
|
|
|
#include "llvm/Type.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
|
2007-06-09 09:20:56 +08:00
|
|
|
void CodeGenFunction::EmitDecl(const Decl &D) {
|
|
|
|
switch (D.getKind()) {
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::TranslationUnit:
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
|
|
|
case Decl::NonTypeTemplateParm:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::Field:
|
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCAtDefsField:
|
2007-10-09 05:37:32 +08:00
|
|
|
case Decl::ParmVar:
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ClassTemplate:
|
|
|
|
case Decl::FunctionTemplate:
|
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::ObjCProperty:
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::ObjCPropertyImpl:
|
|
|
|
case Decl::ObjCClass:
|
|
|
|
case Decl::ObjCForwardProtocol:
|
|
|
|
case Decl::FileScopeAsm:
|
|
|
|
case Decl::Friend:
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
case Decl::Block:
|
|
|
|
|
|
|
|
assert(0 && "Declaration not should not be in declstmts!");
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::Function: // void X();
|
2008-10-15 08:42:39 +08:00
|
|
|
case Decl::Record: // struct/union/class X;
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::Enum: // enum X;
|
2009-09-09 23:08:12 +08:00
|
|
|
case Decl::EnumConstant: // enum ? { X = ? }
|
2008-10-15 08:42:39 +08:00
|
|
|
case Decl::CXXRecord: // struct/union/class X; [C++]
|
2009-11-23 08:07:06 +08:00
|
|
|
case Decl::Using: // using X; [C++]
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
case Decl::UsingDirective: // using namespace X; [C++]
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::NamespaceAlias:
|
2009-12-04 01:26:31 +08:00
|
|
|
case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
|
2007-06-02 12:16:21 +08:00
|
|
|
// None of these decls require codegen support.
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-30 01:28:43 +08:00
|
|
|
case Decl::Var: {
|
|
|
|
const VarDecl &VD = cast<VarDecl>(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(VD.isBlockVarDecl() &&
|
2008-08-30 01:28:43 +08:00
|
|
|
"Should not see file-scope variables inside a function!");
|
|
|
|
return EmitBlockVarDecl(VD);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
case Decl::Typedef: { // typedef int X;
|
|
|
|
const TypedefDecl &TD = cast<TypedefDecl>(D);
|
|
|
|
QualType Ty = TD.getUnderlyingType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitBlockVarDecl - This method handles emission of any variable declaration
|
|
|
|
/// inside a function, including static vars etc.
|
2008-04-16 06:42:06 +08:00
|
|
|
void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
|
2009-06-30 10:34:44 +08:00
|
|
|
if (D.hasAttr<AsmLabelAttr>())
|
2009-03-31 04:32:06 +08:00
|
|
|
CGM.ErrorUnsupported(&D, "__asm__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
switch (D.getStorageClass()) {
|
2009-04-14 10:25:56 +08:00
|
|
|
case VarDecl::None:
|
|
|
|
case VarDecl::Auto:
|
|
|
|
case VarDecl::Register:
|
|
|
|
return EmitLocalBlockVarDecl(D);
|
2010-02-07 10:03:08 +08:00
|
|
|
case VarDecl::Static: {
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
|
|
|
llvm::GlobalValue::InternalLinkage;
|
|
|
|
|
|
|
|
// If this is a static declaration inside an inline function, it must have
|
|
|
|
// weak linkage so that the linker will merge multiple definitions of it.
|
|
|
|
if (getContext().getLangOptions().CPlusPlus) {
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
|
|
|
|
if (FD->isInlined())
|
|
|
|
Linkage = llvm::GlobalValue::WeakAnyLinkage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EmitStaticBlockVarDecl(D, Linkage);
|
|
|
|
}
|
2007-06-02 12:53:11 +08:00
|
|
|
case VarDecl::Extern:
|
2009-04-14 10:25:56 +08:00
|
|
|
case VarDecl::PrivateExtern:
|
2008-02-17 06:30:38 +08:00
|
|
|
// Don't emit it now, allow it to be emitted lazily on its first use.
|
|
|
|
return;
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
2009-04-14 10:25:56 +08:00
|
|
|
|
|
|
|
assert(0 && "Unknown storage class");
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
const char *Separator) {
|
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
2010-03-20 07:29:14 +08:00
|
|
|
if (CGF.getContext().getLangOptions().CPlusPlus) {
|
|
|
|
MangleBuffer Name;
|
|
|
|
CGM.getMangledName(Name, &D);
|
|
|
|
return Name.getString().str();
|
|
|
|
}
|
2009-12-05 16:22:11 +08:00
|
|
|
|
|
|
|
std::string ContextName;
|
2010-03-20 07:29:14 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
|
|
|
|
MangleBuffer Name;
|
|
|
|
CGM.getMangledName(Name, FD);
|
|
|
|
ContextName = Name.getString().str();
|
|
|
|
} else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
|
2009-12-05 16:22:11 +08:00
|
|
|
ContextName = CGF.CurFn->getName();
|
|
|
|
else
|
|
|
|
// FIXME: What about in a block??
|
|
|
|
assert(0 && "Unknown context for block var decl");
|
|
|
|
|
|
|
|
return ContextName + Separator + D.getNameAsString();
|
|
|
|
}
|
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenFunction::CreateStaticBlockVarDecl(const VarDecl &D,
|
|
|
|
const char *Separator,
|
2009-12-05 16:22:11 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
2009-02-26 03:24:29 +08:00
|
|
|
QualType Ty = D.getType();
|
|
|
|
assert(Ty->isConstantSizeType() && "VLAs can't be static");
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
std::string Name = GetStaticDeclName(*this, D, Separator);
|
2009-02-26 03:24:29 +08:00
|
|
|
|
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
|
2009-09-27 02:16:06 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), LTy,
|
|
|
|
Ty.isConstant(getContext()), Linkage,
|
|
|
|
CGM.EmitNullConstant(D.getType()), Name, 0,
|
|
|
|
D.isThreadSpecified(), Ty.getAddressSpace());
|
2010-01-28 01:10:57 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
2009-09-27 02:16:06 +08:00
|
|
|
return GV;
|
2009-02-26 03:24:29 +08:00
|
|
|
}
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
/// AddInitializerToGlobalBlockVarDecl - Add the initializer for 'D' to the
|
|
|
|
/// global variable that has already been created for it. If the initializer
|
|
|
|
/// has a different type than GV does, this may free GV and return a different
|
|
|
|
/// one. Otherwise it just returns GV.
|
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenFunction::AddInitializerToGlobalBlockVarDecl(const VarDecl &D,
|
|
|
|
llvm::GlobalVariable *GV) {
|
|
|
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
|
|
|
|
|
|
|
|
// If constant emission failed, then this should be a C++ static
|
|
|
|
// initializer.
|
|
|
|
if (!Init) {
|
|
|
|
if (!getContext().getLangOptions().CPlusPlus)
|
|
|
|
CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
|
2010-01-26 12:02:23 +08:00
|
|
|
else {
|
|
|
|
// Since we have a static initializer, this global variable can't
|
|
|
|
// be constant.
|
|
|
|
GV->setConstant(false);
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
EmitStaticCXXBlockVarDeclInit(D, GV);
|
2010-01-26 12:02:23 +08:00
|
|
|
}
|
2009-12-05 16:22:11 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The initializer may differ in type from the global. Rewrite
|
|
|
|
// the global to match the initializer. (We have to do this
|
|
|
|
// because some types, like unions, can't be completely represented
|
|
|
|
// in the LLVM type system.)
|
|
|
|
if (GV->getType() != Init->getType()) {
|
|
|
|
llvm::GlobalVariable *OldGV = GV;
|
|
|
|
|
|
|
|
GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
|
|
|
|
OldGV->isConstant(),
|
|
|
|
OldGV->getLinkage(), Init, "",
|
|
|
|
0, D.isThreadSpecified(),
|
|
|
|
D.getType().getAddressSpace());
|
|
|
|
|
|
|
|
// Steal the name of the old global
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
|
|
|
|
// Replace all uses of the old global with the new global
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
|
|
|
|
// Erase the old global, since it is no longer used.
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
GV->setInitializer(Init);
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2010-02-07 10:03:08 +08:00
|
|
|
void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D,
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
2009-02-26 03:45:19 +08:00
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-07 10:03:08 +08:00
|
|
|
llvm::GlobalVariable *GV = CreateStaticBlockVarDecl(D, ".", Linkage);
|
2009-02-26 03:45:19 +08:00
|
|
|
|
2009-02-26 04:08:33 +08:00
|
|
|
// Store into LocalDeclMap before generating initializer to handle
|
|
|
|
// circular references.
|
|
|
|
DMEntry = GV;
|
2010-04-20 02:15:02 +08:00
|
|
|
if (getContext().getLangOptions().CPlusPlus)
|
|
|
|
CGM.setStaticLocalDeclAddress(&D, GV);
|
2009-02-26 04:08:33 +08:00
|
|
|
|
2010-05-05 04:45:42 +08:00
|
|
|
// We can't have a VLA here, but we can have a pointer to a VLA,
|
|
|
|
// even though that doesn't really make any sense.
|
2009-04-20 11:54:15 +08:00
|
|
|
// Make sure to evaluate VLA bounds now so that we have them for later.
|
|
|
|
if (D.getType()->isVariablyModifiedType())
|
|
|
|
EmitVLASize(D.getType());
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
// If this value has an initializer, emit it.
|
|
|
|
if (D.getInit())
|
|
|
|
GV = AddInitializerToGlobalBlockVarDecl(D, GV);
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2010-03-11 07:59:59 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
|
|
|
|
2009-02-13 07:32:54 +08:00
|
|
|
// FIXME: Merge attribute handling.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
|
2008-04-19 12:17:09 +08:00
|
|
|
SourceManager &SM = CGM.getContext().getSourceManager();
|
|
|
|
llvm::Constant *Ann =
|
2009-09-09 23:08:12 +08:00
|
|
|
CGM.EmitAnnotateAttr(GV, AA,
|
2009-01-16 15:36:28 +08:00
|
|
|
SM.getInstantiationLineNumber(D.getLocation()));
|
2008-04-19 12:17:09 +08:00
|
|
|
CGM.AddAnnotation(Ann);
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const SectionAttr *SA = D.getAttr<SectionAttr>())
|
2009-02-13 07:32:54 +08:00
|
|
|
GV->setSection(SA->getName());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (D.hasAttr<UsedAttr>())
|
2009-02-14 06:08:43 +08:00
|
|
|
CGM.AddUsedGlobal(GV);
|
|
|
|
|
2009-02-26 04:08:33 +08:00
|
|
|
// We may have to cast the constant because of the initializer
|
|
|
|
// mismatch above.
|
|
|
|
//
|
|
|
|
// FIXME: It is really dangerous to store this in the map; if anyone
|
|
|
|
// RAUW's the GV uses of this constant will be invalid.
|
2008-06-08 09:23:18 +08:00
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
|
|
|
|
const llvm::Type *LPtrTy =
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::get(LTy, D.getType().getAddressSpace());
|
2009-07-30 02:54:39 +08:00
|
|
|
DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
|
2008-06-05 16:59:10 +08:00
|
|
|
|
|
|
|
// Emit global variable debug descriptor for static vars.
|
2009-02-13 16:11:52 +08:00
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
2009-02-20 08:19:45 +08:00
|
|
|
if (DI) {
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2008-06-05 16:59:10 +08:00
|
|
|
DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
|
|
|
|
}
|
2007-10-17 08:52:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-12 10:14:24 +08:00
|
|
|
unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
|
|
|
|
assert(ByRefValueInfo.count(VD) && "Did not find value!");
|
|
|
|
|
|
|
|
return ByRefValueInfo.find(VD)->second.second;
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
/// BuildByRefType - This routine changes a __block variable declared as T x
|
|
|
|
/// into:
|
|
|
|
///
|
|
|
|
/// struct {
|
|
|
|
/// void *__isa;
|
|
|
|
/// void *__forwarding;
|
|
|
|
/// int32_t __flags;
|
|
|
|
/// int32_t __size;
|
2009-09-22 10:12:52 +08:00
|
|
|
/// void *__copy_helper; // only if needed
|
|
|
|
/// void *__destroy_helper; // only if needed
|
|
|
|
/// char padding[X]; // only if needed
|
2009-03-04 11:23:46 +08:00
|
|
|
/// T x;
|
|
|
|
/// } x
|
|
|
|
///
|
2009-09-09 10:51:03 +08:00
|
|
|
const llvm::Type *CodeGenFunction::BuildByRefType(const ValueDecl *D) {
|
2009-09-12 10:14:24 +08:00
|
|
|
std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
|
|
|
|
if (Info.first)
|
|
|
|
return Info.first;
|
|
|
|
|
2009-09-09 10:51:03 +08:00
|
|
|
QualType Ty = D->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
std::vector<const llvm::Type *> Types;
|
2009-09-10 09:32:12 +08:00
|
|
|
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::PointerType *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
2009-09-10 09:32:12 +08:00
|
|
|
llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(VMContext);
|
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
// void *__isa;
|
|
|
|
Types.push_back(Int8PtrTy);
|
2009-09-12 10:14:24 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
// void *__forwarding;
|
|
|
|
Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
|
2009-09-12 10:14:24 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
// int32_t __flags;
|
|
|
|
Types.push_back(llvm::Type::getInt32Ty(VMContext));
|
|
|
|
|
|
|
|
// int32_t __size;
|
|
|
|
Types.push_back(llvm::Type::getInt32Ty(VMContext));
|
|
|
|
|
|
|
|
bool HasCopyAndDispose = BlockRequiresCopying(Ty);
|
|
|
|
if (HasCopyAndDispose) {
|
|
|
|
/// void *__copy_helper;
|
|
|
|
Types.push_back(Int8PtrTy);
|
|
|
|
|
|
|
|
/// void *__destroy_helper;
|
|
|
|
Types.push_back(Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Packed = false;
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits Align = getContext().getDeclAlign(D);
|
|
|
|
if (Align > CharUnits::fromQuantity(Target.getPointerAlign(0) / 8)) {
|
2009-09-12 10:44:18 +08:00
|
|
|
// We have to insert padding.
|
|
|
|
|
|
|
|
// The struct above has 2 32-bit integers.
|
|
|
|
unsigned CurrentOffsetInBytes = 4 * 2;
|
|
|
|
|
|
|
|
// And either 2 or 4 pointers.
|
|
|
|
CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
|
|
|
|
CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
|
|
|
|
|
|
|
|
// Align the offset.
|
|
|
|
unsigned AlignedOffsetInBytes =
|
2010-01-28 01:10:57 +08:00
|
|
|
llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
|
2009-09-12 10:44:18 +08:00
|
|
|
|
|
|
|
unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
|
2009-09-14 01:55:13 +08:00
|
|
|
if (NumPaddingBytes > 0) {
|
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
|
2009-10-21 08:42:55 +08:00
|
|
|
// FIXME: We need a sema error for alignment larger than the minimum of
|
|
|
|
// the maximal stack alignmint and the alignment of malloc on the system.
|
2009-09-14 01:55:13 +08:00
|
|
|
if (NumPaddingBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
2009-09-14 01:55:13 +08:00
|
|
|
Types.push_back(Ty);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
2009-09-14 01:55:13 +08:00
|
|
|
// We want a packed struct.
|
|
|
|
Packed = true;
|
|
|
|
}
|
2009-09-12 10:44:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// T x;
|
|
|
|
Types.push_back(ConvertType(Ty));
|
2009-09-10 09:32:12 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
const llvm::Type *T = llvm::StructType::get(VMContext, Types, Packed);
|
2009-09-10 09:32:12 +08:00
|
|
|
|
|
|
|
cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
|
|
|
|
CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
|
|
|
|
ByRefTypeHolder.get());
|
|
|
|
|
2009-09-12 10:14:24 +08:00
|
|
|
Info.first = ByRefTypeHolder.get();
|
2009-09-12 10:44:18 +08:00
|
|
|
|
|
|
|
Info.second = Types.size() - 1;
|
2009-09-12 10:14:24 +08:00
|
|
|
|
|
|
|
return Info.first;
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
|
|
|
|
/// variable declaration with auto, register, or no storage class specifier.
|
2008-05-08 13:58:21 +08:00
|
|
|
/// These turn into simple stack objects, or GlobalValues depending on target.
|
2008-04-16 06:42:06 +08:00
|
|
|
void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2009-06-30 10:34:44 +08:00
|
|
|
bool isByRef = D.hasAttr<BlocksAttr>();
|
2009-03-05 09:23:13 +08:00
|
|
|
bool needsDispose = false;
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits Align = CharUnits::Zero();
|
2009-12-05 16:22:11 +08:00
|
|
|
bool IsSimpleConstantInitializer = false;
|
2007-06-02 12:53:11 +08:00
|
|
|
|
|
|
|
llvm::Value *DeclPtr;
|
2008-02-15 20:20:59 +08:00
|
|
|
if (Ty->isConstantSizeType()) {
|
2008-05-08 13:58:21 +08:00
|
|
|
if (!Target.useGlobalsForAutomaticVariables()) {
|
2009-11-04 09:18:09 +08:00
|
|
|
|
2009-12-05 14:49:57 +08:00
|
|
|
// If this value is an array or struct, is POD, and if the initializer is
|
|
|
|
// a staticly determinable constant, try to optimize it.
|
2009-12-05 16:22:11 +08:00
|
|
|
if (D.getInit() && !isByRef &&
|
2009-12-05 14:49:57 +08:00
|
|
|
(Ty->isArrayType() || Ty->isRecordType()) &&
|
|
|
|
Ty->isPODType() &&
|
|
|
|
D.getInit()->isConstantInitializer(getContext())) {
|
|
|
|
// If this variable is marked 'const', emit the value as a global.
|
|
|
|
if (CGM.getCodeGenOpts().MergeAllConstants &&
|
|
|
|
Ty.isConstant(getContext())) {
|
2010-02-07 10:03:08 +08:00
|
|
|
EmitStaticBlockVarDecl(D, llvm::GlobalValue::InternalLinkage);
|
2009-11-04 09:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-12-05 16:22:11 +08:00
|
|
|
|
|
|
|
IsSimpleConstantInitializer = true;
|
2009-11-04 09:18:09 +08:00
|
|
|
}
|
|
|
|
|
2008-05-08 13:58:21 +08:00
|
|
|
// A normal fixed sized variable becomes an alloca in the entry block.
|
2009-03-04 12:25:14 +08:00
|
|
|
const llvm::Type *LTy = ConvertTypeForMem(Ty);
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef)
|
2009-09-09 10:51:03 +08:00
|
|
|
LTy = BuildByRefType(&D);
|
2009-03-22 08:24:14 +08:00
|
|
|
llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
|
2009-12-08 22:04:35 +08:00
|
|
|
Alloc->setName(D.getNameAsString());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-28 01:10:57 +08:00
|
|
|
Align = getContext().getDeclAlign(&D);
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef)
|
2010-01-28 01:10:57 +08:00
|
|
|
Align = std::max(Align,
|
|
|
|
CharUnits::fromQuantity(Target.getPointerAlign(0) / 8));
|
|
|
|
Alloc->setAlignment(Align.getQuantity());
|
2008-06-01 05:20:41 +08:00
|
|
|
DeclPtr = Alloc;
|
2008-05-08 13:58:21 +08:00
|
|
|
} else {
|
|
|
|
// Targets that don't support recursion emit locals as globals.
|
|
|
|
const char *Class =
|
|
|
|
D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
|
2009-09-09 23:08:12 +08:00
|
|
|
DeclPtr = CreateStaticBlockVarDecl(D, Class,
|
2009-02-26 03:45:19 +08:00
|
|
|
llvm::GlobalValue
|
|
|
|
::InternalLinkage);
|
2008-05-08 13:58:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
// FIXME: Can this happen?
|
2008-12-21 04:46:34 +08:00
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
2007-06-02 12:53:11 +08:00
|
|
|
} else {
|
2009-07-19 14:58:07 +08:00
|
|
|
EnsureInsertPoint();
|
|
|
|
|
2009-02-10 04:41:50 +08:00
|
|
|
if (!DidCallStackSave) {
|
2008-12-12 15:38:43 +08:00
|
|
|
// Save the stack.
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *LTy = llvm::Type::getInt8PtrTy(VMContext);
|
2008-12-12 15:38:43 +08:00
|
|
|
llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
|
|
|
|
llvm::Value *V = Builder.CreateCall(F);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
Builder.CreateStore(V, Stack);
|
2009-02-10 04:41:50 +08:00
|
|
|
|
|
|
|
DidCallStackSave = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-10 04:41:50 +08:00
|
|
|
{
|
|
|
|
// Push a cleanup block and restore the stack there.
|
2009-11-25 00:21:10 +08:00
|
|
|
DelayedCleanupBlock scope(*this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-10 04:41:50 +08:00
|
|
|
V = Builder.CreateLoad(Stack, "tmp");
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
|
|
|
|
Builder.CreateCall(F, V);
|
|
|
|
}
|
2008-12-12 15:38:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
// Get the element type.
|
2009-09-09 23:08:12 +08:00
|
|
|
const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
|
2008-12-12 15:38:43 +08:00
|
|
|
const llvm::Type *LElemPtrTy =
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
|
2008-12-12 15:38:43 +08:00
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
llvm::Value *VLASize = EmitVLASize(Ty);
|
2008-12-12 15:38:43 +08:00
|
|
|
|
2009-02-06 03:43:10 +08:00
|
|
|
// Downcast the VLA size expression
|
2009-08-14 05:57:51 +08:00
|
|
|
VLASize = Builder.CreateIntCast(VLASize, llvm::Type::getInt32Ty(VMContext),
|
|
|
|
false, "tmp");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
// Allocate memory for the array.
|
2009-09-27 02:16:06 +08:00
|
|
|
llvm::AllocaInst *VLA =
|
|
|
|
Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), VLASize, "vla");
|
2010-01-28 01:10:57 +08:00
|
|
|
VLA->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
2009-09-27 02:16:06 +08:00
|
|
|
|
2008-12-21 07:11:59 +08:00
|
|
|
DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
2008-12-21 07:11:59 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for local var declaration.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2009-07-19 15:03:11 +08:00
|
|
|
assert(HaveInsertPoint() && "Unexpected unreachable point!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2009-05-22 21:54:25 +08:00
|
|
|
if (Target.useGlobalsForAutomaticVariables()) {
|
|
|
|
DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
|
2009-03-04 11:23:46 +08:00
|
|
|
} else
|
|
|
|
DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
|
2008-05-30 18:30:31 +08:00
|
|
|
}
|
|
|
|
|
2007-07-12 08:39:48 +08:00
|
|
|
// If this local has an initializer, emit it now.
|
2009-07-19 14:58:07 +08:00
|
|
|
const Expr *Init = D.getInit();
|
|
|
|
|
|
|
|
// If we are at an unreachable point, we don't need to emit the initializer
|
|
|
|
// unless it contains a label.
|
|
|
|
if (!HaveInsertPoint()) {
|
|
|
|
if (!ContainsLabel(Init))
|
|
|
|
Init = 0;
|
|
|
|
else
|
|
|
|
EnsureInsertPoint();
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef) {
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::PointerType *PtrToInt8Ty = llvm::Type::getInt8PtrTy(VMContext);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
EnsureInsertPoint();
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
|
|
|
|
llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
|
|
|
|
llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
|
|
|
|
llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
|
|
|
|
llvm::Value *V;
|
|
|
|
int flag = 0;
|
|
|
|
int flags = 0;
|
|
|
|
|
2009-03-07 14:04:31 +08:00
|
|
|
needsDispose = true;
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
if (Ty->isBlockPointerType()) {
|
|
|
|
flag |= BLOCK_FIELD_IS_BLOCK;
|
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
|
|
|
} else if (BlockRequiresCopying(Ty)) {
|
|
|
|
flag |= BLOCK_FIELD_IS_OBJECT;
|
2009-03-06 12:53:30 +08:00
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
2009-03-07 14:04:31 +08:00
|
|
|
|
|
|
|
// FIXME: Someone double check this.
|
|
|
|
if (Ty.isObjCGCWeak())
|
|
|
|
flag |= BLOCK_FIELD_IS_WEAK;
|
2009-03-04 11:23:46 +08:00
|
|
|
|
|
|
|
int isa = 0;
|
|
|
|
if (flag&BLOCK_FIELD_IS_WEAK)
|
|
|
|
isa = 1;
|
2009-08-14 05:57:51 +08:00
|
|
|
V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), isa);
|
2009-03-06 09:33:24 +08:00
|
|
|
V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, isa_field);
|
|
|
|
|
2009-09-10 09:32:12 +08:00
|
|
|
Builder.CreateStore(DeclPtr, forwarding_field);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags);
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, flags_field);
|
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
const llvm::Type *V1;
|
|
|
|
V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
|
2009-08-14 05:57:51 +08:00
|
|
|
V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2010-01-26 21:48:07 +08:00
|
|
|
CGM.GetTargetTypeStoreSize(V1).getQuantity());
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, size_field);
|
|
|
|
|
|
|
|
if (flags & BLOCK_HAS_COPY_DISPOSE) {
|
2009-03-05 16:32:30 +08:00
|
|
|
BlockHasCopyDispose = true;
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
|
2010-01-28 01:10:57 +08:00
|
|
|
Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag,
|
|
|
|
Align.getQuantity()),
|
2009-03-06 14:12:24 +08:00
|
|
|
copy_helper);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
|
2009-06-06 07:26:36 +08:00
|
|
|
Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag,
|
2010-01-28 01:10:57 +08:00
|
|
|
Align.getQuantity()),
|
2009-03-06 12:53:30 +08:00
|
|
|
destroy_helper);
|
2007-08-26 13:13:54 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-02-08 07:51:38 +08:00
|
|
|
|
2010-03-13 05:40:43 +08:00
|
|
|
if (Init) {
|
|
|
|
llvm::Value *Loc = DeclPtr;
|
|
|
|
if (isByRef)
|
|
|
|
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
|
|
D.getNameAsString());
|
|
|
|
|
|
|
|
bool isVolatile =
|
|
|
|
getContext().getCanonicalType(D.getType()).isVolatileQualified();
|
|
|
|
|
|
|
|
// If the initializer was a simple constant initializer, we can optimize it
|
|
|
|
// in various ways.
|
|
|
|
if (IsSimpleConstantInitializer) {
|
|
|
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(),D.getType(),this);
|
|
|
|
assert(Init != 0 && "Wasn't a simple constant init?");
|
|
|
|
|
|
|
|
llvm::Value *AlignVal =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
|
|
|
Align.getQuantity());
|
|
|
|
const llvm::Type *IntPtr =
|
|
|
|
llvm::IntegerType::get(VMContext, LLVMPointerWidth);
|
|
|
|
llvm::Value *SizeVal =
|
|
|
|
llvm::ConstantInt::get(IntPtr,
|
|
|
|
getContext().getTypeSizeInChars(Ty).getQuantity());
|
|
|
|
|
|
|
|
const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
|
|
|
|
if (Loc->getType() != BP)
|
|
|
|
Loc = Builder.CreateBitCast(Loc, BP, "tmp");
|
|
|
|
|
2010-04-04 11:10:52 +08:00
|
|
|
llvm::Value *NotVolatile =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), 0);
|
|
|
|
|
2010-03-13 05:40:43 +08:00
|
|
|
// If the initializer is all zeros, codegen with memset.
|
|
|
|
if (isa<llvm::ConstantAggregateZero>(Init)) {
|
|
|
|
llvm::Value *Zero =
|
2010-04-04 11:10:52 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 0);
|
|
|
|
Builder.CreateCall5(CGM.getMemSetFn(Loc->getType(), SizeVal->getType()),
|
|
|
|
Loc, Zero, SizeVal, AlignVal, NotVolatile);
|
2010-03-13 05:40:43 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, create a temporary global with the initializer then
|
|
|
|
// memcpy from the global to the alloca.
|
|
|
|
std::string Name = GetStaticDeclName(*this, D, ".");
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), Init->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
Init, Name, 0, false, 0);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
|
|
|
|
|
|
|
llvm::Value *SrcPtr = GV;
|
|
|
|
if (SrcPtr->getType() != BP)
|
|
|
|
SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
|
2010-04-04 11:10:52 +08:00
|
|
|
|
|
|
|
Builder.CreateCall5(CGM.getMemCpyFn(Loc->getType(), SrcPtr->getType(),
|
|
|
|
SizeVal->getType()),
|
|
|
|
Loc, SrcPtr, SizeVal, AlignVal, NotVolatile);
|
2010-03-13 05:40:43 +08:00
|
|
|
}
|
|
|
|
} else if (Ty->isReferenceType()) {
|
|
|
|
RValue RV = EmitReferenceBindingToExpr(Init, /*IsInitializer=*/true);
|
|
|
|
EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Ty);
|
|
|
|
} else if (!hasAggregateLLVMType(Init->getType())) {
|
|
|
|
llvm::Value *V = EmitScalarExpr(Init);
|
|
|
|
EmitStoreOfScalar(V, Loc, isVolatile, D.getType());
|
|
|
|
} else if (Init->getType()->isAnyComplexType()) {
|
|
|
|
EmitComplexExprIntoAddr(Init, Loc, isVolatile);
|
|
|
|
} else {
|
|
|
|
EmitAggExpr(Init, Loc, isVolatile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-04 04:20:07 +08:00
|
|
|
// Handle CXX destruction of variables.
|
2009-08-04 04:51:29 +08:00
|
|
|
QualType DtorTy(Ty);
|
2009-11-10 06:08:55 +08:00
|
|
|
while (const ArrayType *Array = getContext().getAsArrayType(DtorTy))
|
2009-10-30 00:22:54 +08:00
|
|
|
DtorTy = getContext().getBaseElementType(Array);
|
2009-08-04 04:51:29 +08:00
|
|
|
if (const RecordType *RT = DtorTy->getAs<RecordType>())
|
2009-08-04 04:20:07 +08:00
|
|
|
if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
2010-05-04 08:26:07 +08:00
|
|
|
llvm::Value *Loc = DeclPtr;
|
|
|
|
if (isByRef)
|
|
|
|
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
|
|
D.getNameAsString());
|
|
|
|
|
2009-08-04 04:20:07 +08:00
|
|
|
if (!ClassDecl->hasTrivialDestructor()) {
|
|
|
|
const CXXDestructorDecl *D = ClassDecl->getDestructor(getContext());
|
|
|
|
assert(D && "EmitLocalBlockVarDecl - destructor is nul");
|
2009-10-30 00:22:54 +08:00
|
|
|
|
|
|
|
if (const ConstantArrayType *Array =
|
|
|
|
getContext().getAsConstantArrayType(Ty)) {
|
2009-12-09 11:35:49 +08:00
|
|
|
{
|
|
|
|
DelayedCleanupBlock Scope(*this);
|
|
|
|
QualType BaseElementTy = getContext().getBaseElementType(Array);
|
|
|
|
const llvm::Type *BasePtr = ConvertType(BaseElementTy);
|
|
|
|
BasePtr = llvm::PointerType::getUnqual(BasePtr);
|
|
|
|
llvm::Value *BaseAddrPtr =
|
2010-05-04 08:26:07 +08:00
|
|
|
Builder.CreateBitCast(Loc, BasePtr);
|
2009-12-09 11:35:49 +08:00
|
|
|
EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
|
2009-11-05 01:57:40 +08:00
|
|
|
|
2009-12-09 11:35:49 +08:00
|
|
|
// Make sure to jump to the exit block.
|
|
|
|
EmitBranch(Scope.getCleanupExitBlock());
|
|
|
|
}
|
|
|
|
if (Exceptions) {
|
|
|
|
EHCleanupBlock Cleanup(*this);
|
|
|
|
QualType BaseElementTy = getContext().getBaseElementType(Array);
|
|
|
|
const llvm::Type *BasePtr = ConvertType(BaseElementTy);
|
|
|
|
BasePtr = llvm::PointerType::getUnqual(BasePtr);
|
|
|
|
llvm::Value *BaseAddrPtr =
|
2010-05-04 08:26:07 +08:00
|
|
|
Builder.CreateBitCast(Loc, BasePtr);
|
2009-12-09 11:35:49 +08:00
|
|
|
EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
|
|
|
|
}
|
2009-11-05 01:57:40 +08:00
|
|
|
} else {
|
2009-12-09 11:35:49 +08:00
|
|
|
{
|
|
|
|
DelayedCleanupBlock Scope(*this);
|
2010-05-03 07:29:11 +08:00
|
|
|
EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false,
|
2010-05-04 08:26:07 +08:00
|
|
|
Loc);
|
2009-12-09 11:35:49 +08:00
|
|
|
|
|
|
|
// Make sure to jump to the exit block.
|
|
|
|
EmitBranch(Scope.getCleanupExitBlock());
|
|
|
|
}
|
|
|
|
if (Exceptions) {
|
|
|
|
EHCleanupBlock Cleanup(*this);
|
2010-05-03 07:29:11 +08:00
|
|
|
EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false,
|
2010-05-04 08:26:07 +08:00
|
|
|
Loc);
|
2009-12-09 11:35:49 +08:00
|
|
|
}
|
2009-11-05 01:57:40 +08:00
|
|
|
}
|
2009-08-04 04:20:07 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 07:51:38 +08:00
|
|
|
// Handle the cleanup attribute
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
|
2009-02-08 07:51:38 +08:00
|
|
|
const FunctionDecl *FD = CA->getFunctionDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-11 07:43:36 +08:00
|
|
|
llvm::Constant* F = CGM.GetAddrOfFunction(FD);
|
2009-02-08 07:51:38 +08:00
|
|
|
assert(F && "Could not find function!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-26 08:34:20 +08:00
|
|
|
const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
|
|
|
|
|
|
|
|
// In some cases, the type of the function argument will be different from
|
|
|
|
// the type of the pointer. An example of this is
|
|
|
|
// void f(void* arg);
|
|
|
|
// __attribute__((cleanup(f))) void *g;
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-04-26 08:34:20 +08:00
|
|
|
// To fix this we insert a bitcast here.
|
|
|
|
QualType ArgTy = Info.arg_begin()->type;
|
2009-12-09 11:35:49 +08:00
|
|
|
{
|
|
|
|
DelayedCleanupBlock scope(*this);
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(Builder.CreateBitCast(DeclPtr,
|
|
|
|
ConvertType(ArgTy))),
|
|
|
|
getContext().getPointerType(D.getType())));
|
2009-12-25 03:25:24 +08:00
|
|
|
EmitCall(Info, F, ReturnValueSlot(), Args);
|
2009-12-09 11:35:49 +08:00
|
|
|
}
|
|
|
|
if (Exceptions) {
|
|
|
|
EHCleanupBlock Cleanup(*this);
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(Builder.CreateBitCast(DeclPtr,
|
|
|
|
ConvertType(ArgTy))),
|
|
|
|
getContext().getPointerType(D.getType())));
|
2009-12-25 03:25:24 +08:00
|
|
|
EmitCall(Info, F, ReturnValueSlot(), Args);
|
2009-12-09 11:35:49 +08:00
|
|
|
}
|
2009-02-08 07:51:38 +08:00
|
|
|
}
|
2009-03-05 09:23:13 +08:00
|
|
|
|
2009-03-05 10:34:38 +08:00
|
|
|
if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
|
2009-12-09 11:35:49 +08:00
|
|
|
{
|
|
|
|
DelayedCleanupBlock scope(*this);
|
|
|
|
llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
|
|
|
|
V = Builder.CreateLoad(V);
|
|
|
|
BuildBlockRelease(V);
|
|
|
|
}
|
|
|
|
// FIXME: Turn this on and audit the codegen
|
|
|
|
if (0 && Exceptions) {
|
|
|
|
EHCleanupBlock Cleanup(*this);
|
|
|
|
llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
|
|
|
|
V = Builder.CreateLoad(V);
|
|
|
|
BuildBlockRelease(V);
|
|
|
|
}
|
2009-03-05 09:23:13 +08:00
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// Emit an alloca (or GlobalValue depending on target)
|
2008-05-08 13:58:21 +08:00
|
|
|
/// for the specified parameter and set up LocalDeclMap.
|
2008-08-16 11:19:19 +08:00
|
|
|
void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
|
|
|
|
// FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
|
2008-10-31 17:52:39 +08:00
|
|
|
assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
|
2008-08-16 11:19:19 +08:00
|
|
|
"Invalid argument to EmitParmDecl");
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2009-11-04 07:25:48 +08:00
|
|
|
CanQualType CTy = getContext().getCanonicalType(Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-14 04:44:40 +08:00
|
|
|
llvm::Value *DeclPtr;
|
2010-02-09 06:53:07 +08:00
|
|
|
// If this is an aggregate or variable sized value, reuse the input pointer.
|
|
|
|
if (!Ty->isConstantSizeType() ||
|
|
|
|
CodeGenFunction::hasAggregateLLVMType(Ty)) {
|
2007-06-14 04:44:40 +08:00
|
|
|
DeclPtr = Arg;
|
|
|
|
} else {
|
2010-02-09 06:53:07 +08:00
|
|
|
// Otherwise, create a temporary to hold the value.
|
2010-02-09 10:48:28 +08:00
|
|
|
DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr");
|
2010-02-09 06:53:07 +08:00
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
|
|
|
EmitStoreOfScalar(Arg, DeclPtr, CTy.isVolatileQualified(), Ty);
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
2010-02-09 06:53:07 +08:00
|
|
|
Arg->setName(D.getName());
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for param declaration.
|
2009-10-10 06:06:15 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
|
|
|
DI->setLocation(D.getLocation());
|
2008-11-10 14:08:34 +08:00
|
|
|
DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
|
2009-10-10 06:06:15 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|