2007-07-11 21:01:13 +04:00
|
|
|
//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-module state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenModule.h"
|
2009-03-26 08:00:52 +03:00
|
|
|
#include "CGDebugInfo.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "CodeGenFunction.h"
|
2010-10-15 03:06:10 +04:00
|
|
|
#include "CodeGenTBAA.h"
|
2008-09-09 01:33:45 +04:00
|
|
|
#include "CGCall.h"
|
2010-08-31 11:33:07 +04:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-13 04:59:25 +04:00
|
|
|
#include "CGObjCRuntime.h"
|
2010-01-10 15:58:08 +03:00
|
|
|
#include "TargetInfo.h"
|
2010-06-16 03:19:56 +04:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-01-26 16:48:07 +03:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2008-08-11 09:35:13 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-11-04 19:51:42 +03:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2010-06-21 22:41:26 +04:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2011-01-13 21:57:25 +03:00
|
|
|
#include "clang/AST/Mangle.h"
|
2009-12-01 02:41:22 +03:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-06-14 05:54:56 +04:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2007-12-02 10:19:18 +03:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-04-19 08:17:09 +04:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-04-01 19:50:34 +04:00
|
|
|
#include "clang/Basic/ConvertUTF.h"
|
2008-03-09 06:09:36 +03:00
|
|
|
#include "llvm/CallingConv.h"
|
2007-08-31 08:31:45 +04:00
|
|
|
#include "llvm/Module.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-12-29 00:44:41 +03:00
|
|
|
#include "llvm/LLVMContext.h"
|
2010-03-06 03:35:14 +03:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2011-02-11 02:59:36 +03:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2008-06-01 18:13:53 +04:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2010-04-10 06:56:12 +04:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-11-07 12:22:46 +03:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-08-23 01:01:12 +04:00
|
|
|
static CGCXXABI &createCXXABI(CodeGenModule &CGM) {
|
|
|
|
switch (CGM.getContext().Target.getCXXABI()) {
|
|
|
|
case CXXABI_ARM: return *CreateARMCXXABI(CGM);
|
|
|
|
case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM);
|
|
|
|
case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("invalid C++ ABI kind");
|
|
|
|
return *CreateItaniumCXXABI(CGM);
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2009-11-12 20:24:48 +03:00
|
|
|
CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
|
2010-03-04 07:29:44 +03:00
|
|
|
llvm::Module &M, const llvm::TargetData &TD,
|
|
|
|
Diagnostic &diags)
|
2011-02-08 11:22:06 +03:00
|
|
|
: Context(C), Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M),
|
2010-03-04 07:29:44 +03:00
|
|
|
TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags),
|
2010-08-23 01:01:12 +04:00
|
|
|
ABI(createCXXABI(*this)),
|
|
|
|
Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI),
|
2010-10-15 03:06:10 +04:00
|
|
|
TBAA(0),
|
2011-06-16 03:02:42 +04:00
|
|
|
VTables(*this), Runtime(0), DebugInfo(0), ARCData(0), RRData(0),
|
2010-10-19 21:19:29 +04:00
|
|
|
CFConstantStringClassRef(0), ConstantStringClassRef(0),
|
2010-07-16 04:00:15 +04:00
|
|
|
VMContext(M.getContext()),
|
2010-07-16 04:00:19 +04:00
|
|
|
NSConcreteGlobalBlockDecl(0), NSConcreteStackBlockDecl(0),
|
2010-07-16 04:00:15 +04:00
|
|
|
NSConcreteGlobalBlock(0), NSConcreteStackBlock(0),
|
2010-07-16 04:00:19 +04:00
|
|
|
BlockObjectAssignDecl(0), BlockObjectDisposeDecl(0),
|
2011-02-08 11:22:06 +03:00
|
|
|
BlockObjectAssign(0), BlockObjectDispose(0),
|
|
|
|
BlockDescriptorType(0), GenericBlockLiteralType(0) {
|
2011-03-23 00:21:24 +03:00
|
|
|
if (Features.ObjC1)
|
|
|
|
createObjCRuntime();
|
2008-05-08 12:54:20 +04:00
|
|
|
|
2010-10-15 03:06:10 +04:00
|
|
|
// Enable TBAA unless it's suppressed.
|
|
|
|
if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)
|
2010-10-16 00:23:12 +04:00
|
|
|
TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(),
|
|
|
|
ABI.getMangleContext());
|
2010-10-15 03:06:10 +04:00
|
|
|
|
2011-04-22 03:44:07 +04:00
|
|
|
// If debug info or coverage generation is enabled, create the CGDebugInfo
|
|
|
|
// object.
|
|
|
|
if (CodeGenOpts.DebugInfo || CodeGenOpts.EmitGcovArcs ||
|
|
|
|
CodeGenOpts.EmitGcovNotes)
|
|
|
|
DebugInfo = new CGDebugInfo(*this);
|
2011-02-08 11:22:06 +03:00
|
|
|
|
|
|
|
Block.GlobalUniqueCount = 0;
|
2011-02-15 12:22:45 +03:00
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
if (C.getLangOptions().ObjCAutoRefCount)
|
|
|
|
ARCData = new ARCEntrypoints();
|
|
|
|
RRData = new RREntrypoints();
|
|
|
|
|
2011-02-15 12:22:45 +03:00
|
|
|
// Initialize the type cache.
|
|
|
|
llvm::LLVMContext &LLVMContext = M.getContext();
|
2011-05-15 05:53:33 +04:00
|
|
|
VoidTy = llvm::Type::getVoidTy(LLVMContext);
|
|
|
|
Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
|
|
|
|
Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
|
|
|
|
Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
|
2011-02-15 12:22:45 +03:00
|
|
|
PointerWidthInBits = C.Target.getPointerWidth(0);
|
2011-02-22 09:44:22 +03:00
|
|
|
PointerAlignInBytes =
|
|
|
|
C.toCharUnitsFromBits(C.Target.getPointerAlign(0)).getQuantity();
|
2011-02-15 12:22:45 +03:00
|
|
|
IntTy = llvm::IntegerType::get(LLVMContext, C.Target.getIntWidth());
|
|
|
|
IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
|
|
|
|
Int8PtrTy = Int8Ty->getPointerTo(0);
|
|
|
|
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
|
2008-03-01 11:45:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenModule::~CodeGenModule() {
|
2008-08-05 22:50:11 +04:00
|
|
|
delete Runtime;
|
2010-08-23 01:01:12 +04:00
|
|
|
delete &ABI;
|
2010-10-15 22:04:46 +04:00
|
|
|
delete TBAA;
|
2008-08-05 22:50:11 +04:00
|
|
|
delete DebugInfo;
|
2011-06-16 03:02:42 +04:00
|
|
|
delete ARCData;
|
|
|
|
delete RRData;
|
2008-08-05 22:50:11 +04:00
|
|
|
}
|
|
|
|
|
2010-01-23 05:40:42 +03:00
|
|
|
void CodeGenModule::createObjCRuntime() {
|
|
|
|
if (!Features.NeXTRuntime)
|
|
|
|
Runtime = CreateGNUObjCRuntime(*this);
|
|
|
|
else
|
|
|
|
Runtime = CreateMacObjCRuntime(*this);
|
|
|
|
}
|
|
|
|
|
2008-08-05 22:50:11 +04:00
|
|
|
void CodeGenModule::Release() {
|
2009-03-23 00:21:57 +03:00
|
|
|
EmitDeferred();
|
2010-01-08 03:50:11 +03:00
|
|
|
EmitCXXGlobalInitFunc();
|
2010-03-20 07:15:41 +03:00
|
|
|
EmitCXXGlobalDtorFunc();
|
2008-08-11 22:12:00 +04:00
|
|
|
if (Runtime)
|
|
|
|
if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
|
|
|
|
AddGlobalCtor(ObjCInitFunction);
|
2008-08-01 04:01:51 +04:00
|
|
|
EmitCtorList(GlobalCtors, "llvm.global_ctors");
|
|
|
|
EmitCtorList(GlobalDtors, "llvm.global_dtors");
|
2008-04-19 03:43:57 +04:00
|
|
|
EmitAnnotations();
|
2009-02-13 23:29:50 +03:00
|
|
|
EmitLLVMUsed();
|
2010-07-07 03:57:41 +04:00
|
|
|
|
2010-09-16 10:16:50 +04:00
|
|
|
SimplifyPersonality();
|
|
|
|
|
2010-07-07 03:57:41 +04:00
|
|
|
if (getCodeGenOpts().EmitDeclMetadata)
|
|
|
|
EmitDeclMetadata();
|
2011-05-05 00:46:58 +04:00
|
|
|
|
|
|
|
if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
|
2011-05-05 04:08:20 +04:00
|
|
|
EmitCoverageFile();
|
2008-10-01 04:49:24 +04:00
|
|
|
}
|
|
|
|
|
2011-03-23 19:29:39 +03:00
|
|
|
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
|
|
|
|
// Make sure that this type is translated.
|
|
|
|
Types.UpdateCompletedType(TD);
|
|
|
|
if (DebugInfo)
|
|
|
|
DebugInfo->UpdateCompletedType(TD);
|
|
|
|
}
|
|
|
|
|
2010-10-15 03:06:10 +04:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
|
|
|
return 0;
|
|
|
|
return TBAA->getTBAAInfo(QTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
|
|
|
|
llvm::MDNode *TBAAInfo) {
|
|
|
|
Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
|
|
|
|
}
|
|
|
|
|
2010-03-06 03:35:14 +03:00
|
|
|
bool CodeGenModule::isTargetDarwin() const {
|
2011-04-20 01:43:27 +04:00
|
|
|
return getContext().Target.getTriple().isOSDarwin();
|
2010-03-06 03:35:14 +03:00
|
|
|
}
|
|
|
|
|
2011-03-18 05:56:14 +03:00
|
|
|
void CodeGenModule::Error(SourceLocation loc, llvm::StringRef error) {
|
|
|
|
unsigned diagID = getDiags().getCustomDiagID(Diagnostic::Error, error);
|
|
|
|
getDiags().Report(Context.getFullLoc(loc), diagID);
|
|
|
|
}
|
|
|
|
|
2008-08-16 04:56:44 +04:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 10:19:18 +03:00
|
|
|
/// specified stmt yet.
|
2008-09-04 07:43:08 +04:00
|
|
|
void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
if (OmitOnError && getDiags().hasErrorOccurred())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
|
2009-02-06 22:18:03 +03:00
|
|
|
"cannot compile this %0 yet");
|
2007-12-02 10:19:18 +03:00
|
|
|
std::string Msg = Type;
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59502 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 10:04:44 +03:00
|
|
|
getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
|
|
|
|
<< Msg << S->getSourceRange();
|
2007-12-02 10:19:18 +03:00
|
|
|
}
|
2007-12-02 09:27:33 +03:00
|
|
|
|
2008-08-16 04:56:44 +04:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2008-01-12 10:05:38 +03:00
|
|
|
/// specified decl yet.
|
2008-09-04 07:43:08 +04:00
|
|
|
void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
if (OmitOnError && getDiags().hasErrorOccurred())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
|
2009-02-06 22:18:03 +03:00
|
|
|
"cannot compile this %0 yet");
|
2008-01-12 10:05:38 +03:00
|
|
|
std::string Msg = Type;
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59502 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 10:04:44 +03:00
|
|
|
getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
|
2008-01-12 10:05:38 +03:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
|
2011-01-29 22:39:23 +03:00
|
|
|
const NamedDecl *D) const {
|
2009-04-14 10:00:08 +04:00
|
|
|
// Internal definitions always have default visibility.
|
2009-04-14 09:27:13 +04:00
|
|
|
if (GV->hasLocalLinkage()) {
|
2009-04-11 00:26:50 +04:00
|
|
|
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
2009-04-07 09:48:37 +04:00
|
|
|
return;
|
2009-04-11 00:26:50 +04:00
|
|
|
}
|
2009-04-07 09:48:37 +04:00
|
|
|
|
2010-10-30 15:50:40 +04:00
|
|
|
// Set visibility for definitions.
|
|
|
|
NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility();
|
2011-06-17 00:14:50 +04:00
|
|
|
if (LV.visibilityExplicit() || !GV->hasAvailableExternallyLinkage())
|
|
|
|
GV->setVisibility(GetLLVMVisibility(LV.visibility()));
|
2009-04-03 07:28:57 +04:00
|
|
|
}
|
|
|
|
|
2010-08-04 12:34:44 +04:00
|
|
|
/// Set the symbol visibility of type information (vtable and RTTI)
|
|
|
|
/// associated with the given type.
|
|
|
|
void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV,
|
|
|
|
const CXXRecordDecl *RD,
|
2011-01-29 23:24:48 +03:00
|
|
|
TypeVisibilityKind TVK) const {
|
2011-01-29 22:39:23 +03:00
|
|
|
setGlobalVisibility(GV, RD);
|
2010-08-04 12:34:44 +04:00
|
|
|
|
2010-08-13 03:36:15 +04:00
|
|
|
if (!CodeGenOpts.HiddenWeakVTables)
|
|
|
|
return;
|
|
|
|
|
2011-01-29 23:36:11 +03:00
|
|
|
// We never want to drop the visibility for RTTI names.
|
|
|
|
if (TVK == TVK_ForRTTIName)
|
|
|
|
return;
|
|
|
|
|
2010-08-04 12:34:44 +04:00
|
|
|
// We want to drop the visibility to hidden for weak type symbols.
|
|
|
|
// This isn't possible if there might be unresolved references
|
|
|
|
// elsewhere that rely on this symbol being visible.
|
|
|
|
|
2010-08-06 00:39:18 +04:00
|
|
|
// This should be kept roughly in sync with setThunkVisibility
|
|
|
|
// in CGVTables.cpp.
|
|
|
|
|
2010-08-04 12:34:44 +04:00
|
|
|
// Preconditions.
|
2011-01-24 03:46:19 +03:00
|
|
|
if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage ||
|
2010-08-04 12:34:44 +04:00
|
|
|
GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't override an explicit visibility attribute.
|
2011-03-26 15:10:19 +03:00
|
|
|
if (RD->getExplicitVisibility())
|
2010-08-04 12:34:44 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (RD->getTemplateSpecializationKind()) {
|
|
|
|
// We have to disable the optimization if this is an EI definition
|
|
|
|
// because there might be EI declarations in other shared objects.
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
return;
|
|
|
|
|
2010-08-06 00:39:18 +04:00
|
|
|
// Every use of a non-template class's type information has to emit it.
|
2010-08-04 12:34:44 +04:00
|
|
|
case TSK_Undeclared:
|
|
|
|
break;
|
|
|
|
|
2010-08-06 00:39:18 +04:00
|
|
|
// In theory, implicit instantiations can ignore the possibility of
|
|
|
|
// an explicit instantiation declaration because there necessarily
|
|
|
|
// must be an EI definition somewhere with default visibility. In
|
|
|
|
// practice, it's possible to have an explicit instantiation for
|
|
|
|
// an arbitrary template class, and linkers aren't necessarily able
|
|
|
|
// to deal with mixed-visibility symbols.
|
|
|
|
case TSK_ExplicitSpecialization:
|
2010-08-04 12:34:44 +04:00
|
|
|
case TSK_ImplicitInstantiation:
|
2010-08-13 03:36:15 +04:00
|
|
|
if (!CodeGenOpts.HiddenWeakTemplateVTables)
|
2010-08-06 00:39:18 +04:00
|
|
|
return;
|
2010-08-04 12:34:44 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a key function, there may be translation units
|
|
|
|
// that don't have the key function's definition. But ignore
|
|
|
|
// this if we're emitting RTTI under -fno-rtti.
|
2011-01-29 23:24:48 +03:00
|
|
|
if (!(TVK != TVK_ForRTTI) || Features.RTTI) {
|
2010-08-04 12:34:44 +04:00
|
|
|
if (Context.getKeyFunction(RD))
|
|
|
|
return;
|
2011-01-29 23:24:48 +03:00
|
|
|
}
|
2010-08-04 12:34:44 +04:00
|
|
|
|
|
|
|
// Otherwise, drop the visibility to hidden.
|
|
|
|
GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
2011-01-12 00:44:37 +03:00
|
|
|
GV->setUnnamedAddr(true);
|
2010-08-04 12:34:44 +04:00
|
|
|
}
|
|
|
|
|
2010-06-22 20:05:32 +04:00
|
|
|
llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
|
|
|
|
const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()];
|
|
|
|
if (!Str.empty())
|
|
|
|
return Str;
|
|
|
|
|
2010-08-31 11:33:07 +04:00
|
|
|
if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
|
2010-06-22 20:05:32 +04:00
|
|
|
IdentifierInfo *II = ND->getIdentifier();
|
|
|
|
assert(II && "Attempt to mangle unnamed decl.");
|
|
|
|
|
|
|
|
Str = II->getName();
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallString<256> Buffer;
|
2011-02-11 02:59:36 +03:00
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
2010-06-22 20:05:32 +04:00
|
|
|
if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND))
|
2011-02-11 02:59:36 +03:00
|
|
|
getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out);
|
2010-06-22 20:05:32 +04:00
|
|
|
else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND))
|
2011-02-11 02:59:36 +03:00
|
|
|
getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out);
|
2010-06-22 20:05:32 +04:00
|
|
|
else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND))
|
2011-02-11 02:59:36 +03:00
|
|
|
getCXXABI().getMangleContext().mangleBlock(BD, Out);
|
2010-06-22 20:05:32 +04:00
|
|
|
else
|
2011-02-11 02:59:36 +03:00
|
|
|
getCXXABI().getMangleContext().mangleName(ND, Out);
|
2010-06-22 20:05:32 +04:00
|
|
|
|
|
|
|
// Allocate space for the mangled name.
|
2011-02-11 02:59:36 +03:00
|
|
|
Out.flush();
|
2010-06-22 20:05:32 +04:00
|
|
|
size_t Length = Buffer.size();
|
|
|
|
char *Name = MangledNamesAllocator.Allocate<char>(Length);
|
|
|
|
std::copy(Buffer.begin(), Buffer.end(), Name);
|
|
|
|
|
|
|
|
Str = llvm::StringRef(Name, Length);
|
|
|
|
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
2011-01-13 21:57:25 +03:00
|
|
|
void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
|
|
|
|
const BlockDecl *BD) {
|
|
|
|
MangleContext &MangleCtx = getCXXABI().getMangleContext();
|
|
|
|
const Decl *D = GD.getDecl();
|
2011-02-11 02:59:36 +03:00
|
|
|
llvm::raw_svector_ostream Out(Buffer.getBuffer());
|
2011-01-13 21:57:25 +03:00
|
|
|
if (D == 0)
|
2011-02-11 02:59:36 +03:00
|
|
|
MangleCtx.mangleGlobalBlock(BD, Out);
|
2011-01-13 21:57:25 +03:00
|
|
|
else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
|
2011-02-11 02:59:36 +03:00
|
|
|
MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
|
2011-01-13 21:57:25 +03:00
|
|
|
else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D))
|
2011-02-11 02:59:36 +03:00
|
|
|
MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
|
2011-01-13 21:57:25 +03:00
|
|
|
else
|
2011-02-11 02:59:36 +03:00
|
|
|
MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
|
2010-06-09 06:36:32 +04:00
|
|
|
}
|
|
|
|
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) {
|
|
|
|
return getModule().getNamedValue(Name);
|
2009-02-13 03:10:09 +03:00
|
|
|
}
|
|
|
|
|
2008-03-14 20:18:18 +03:00
|
|
|
/// AddGlobalCtor - Add a function to the list that will be called before
|
|
|
|
/// main() runs.
|
2008-08-01 04:01:51 +04:00
|
|
|
void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
|
2009-01-13 05:25:00 +03:00
|
|
|
// FIXME: Type coercion of void()* types.
|
2008-08-01 04:01:51 +04:00
|
|
|
GlobalCtors.push_back(std::make_pair(Ctor, Priority));
|
2008-03-14 20:18:18 +03:00
|
|
|
}
|
|
|
|
|
2008-08-01 04:01:51 +04:00
|
|
|
/// AddGlobalDtor - Add a function to the list that will be called
|
|
|
|
/// when the module is unloaded.
|
|
|
|
void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
|
2009-01-13 05:25:00 +03:00
|
|
|
// FIXME: Type coercion of void()* types.
|
2008-08-01 04:01:51 +04:00
|
|
|
GlobalDtors.push_back(std::make_pair(Dtor, Priority));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
|
|
|
|
// Ctor function type is void()*.
|
2011-05-15 05:53:33 +04:00
|
|
|
llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
|
2009-07-30 02:16:19 +04:00
|
|
|
llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
|
2008-08-01 04:01:51 +04:00
|
|
|
|
|
|
|
// Get the type of a ctor entry, { i32, void ()* }.
|
2011-06-20 08:01:35 +04:00
|
|
|
llvm::StructType *CtorStructTy =
|
2011-06-19 02:49:11 +04:00
|
|
|
llvm::StructType::get(llvm::Type::getInt32Ty(VMContext),
|
2009-07-30 02:16:19 +04:00
|
|
|
llvm::PointerType::getUnqual(CtorFTy), NULL);
|
2008-08-01 04:01:51 +04:00
|
|
|
|
|
|
|
// Construct the constructor and destructor arrays.
|
|
|
|
std::vector<llvm::Constant*> Ctors;
|
|
|
|
for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
|
|
|
|
std::vector<llvm::Constant*> S;
|
2009-09-09 19:08:12 +04:00
|
|
|
S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2009-08-14 01:57:51 +04:00
|
|
|
I->second, false));
|
2009-07-29 22:54:39 +04:00
|
|
|
S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy));
|
2009-07-28 02:29:56 +04:00
|
|
|
Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
|
2008-08-01 04:01:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ctors.empty()) {
|
2009-07-30 02:16:19 +04:00
|
|
|
llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
|
2009-07-08 23:05:04 +04:00
|
|
|
new llvm::GlobalVariable(TheModule, AT, false,
|
2008-03-19 08:24:56 +03:00
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
2009-07-28 22:33:04 +04:00
|
|
|
llvm::ConstantArray::get(AT, Ctors),
|
2009-07-08 23:05:04 +04:00
|
|
|
GlobalName);
|
2008-03-14 20:18:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 03:43:57 +04:00
|
|
|
void CodeGenModule::EmitAnnotations() {
|
|
|
|
if (Annotations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a new global variable for the ConstantStruct in the Module.
|
|
|
|
llvm::Constant *Array =
|
2009-07-30 02:16:19 +04:00
|
|
|
llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
|
2008-04-19 03:43:57 +04:00
|
|
|
Annotations.size()),
|
|
|
|
Annotations);
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::GlobalValue *gv =
|
|
|
|
new llvm::GlobalVariable(TheModule, Array->getType(), false,
|
|
|
|
llvm::GlobalValue::AppendingLinkage, Array,
|
2009-07-08 23:05:04 +04:00
|
|
|
"llvm.global.annotations");
|
2008-04-19 03:43:57 +04:00
|
|
|
gv->setSection("llvm.metadata");
|
|
|
|
}
|
|
|
|
|
2010-02-19 04:32:20 +03:00
|
|
|
llvm::GlobalValue::LinkageTypes
|
|
|
|
CodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
|
2010-07-29 22:15:58 +04:00
|
|
|
GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
|
2009-04-14 12:05:55 +04:00
|
|
|
|
2010-06-30 20:58:07 +04:00
|
|
|
if (Linkage == GVA_Internal)
|
2010-02-19 04:32:20 +03:00
|
|
|
return llvm::Function::InternalLinkage;
|
2010-06-30 20:58:07 +04:00
|
|
|
|
|
|
|
if (D->hasAttr<DLLExportAttr>())
|
2010-02-19 04:32:20 +03:00
|
|
|
return llvm::Function::DLLExportLinkage;
|
2010-06-30 20:58:07 +04:00
|
|
|
|
|
|
|
if (D->hasAttr<WeakAttr>())
|
2010-02-19 04:32:20 +03:00
|
|
|
return llvm::Function::WeakAnyLinkage;
|
2010-06-30 20:58:07 +04:00
|
|
|
|
|
|
|
// In C99 mode, 'inline' functions are guaranteed to have a strong
|
|
|
|
// definition somewhere else, so we can use available_externally linkage.
|
|
|
|
if (Linkage == GVA_C99Inline)
|
2011-02-04 03:08:13 +03:00
|
|
|
return llvm::Function::AvailableExternallyLinkage;
|
2010-06-30 20:58:07 +04:00
|
|
|
|
|
|
|
// In C++, the compiler has to emit a definition in every translation unit
|
|
|
|
// that references the function. We should use linkonce_odr because
|
|
|
|
// a) if all references in this translation unit are optimized away, we
|
|
|
|
// don't need to codegen it. b) if the function persists, it needs to be
|
|
|
|
// merged with other definitions. c) C++ has the ODR, so we know the
|
|
|
|
// definition is dependable.
|
|
|
|
if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
|
2011-02-04 03:01:24 +03:00
|
|
|
return !Context.getLangOptions().AppleKext
|
|
|
|
? llvm::Function::LinkOnceODRLinkage
|
|
|
|
: llvm::Function::InternalLinkage;
|
2010-06-30 20:58:07 +04:00
|
|
|
|
|
|
|
// An explicit instantiation of a template has weak linkage, since
|
|
|
|
// explicit instantiations can occur in multiple translation units
|
|
|
|
// and must all be equivalent. However, we are not allowed to
|
|
|
|
// throw away these explicit instantiations.
|
|
|
|
if (Linkage == GVA_ExplicitTemplateInstantiation)
|
2011-02-04 03:01:24 +03:00
|
|
|
return !Context.getLangOptions().AppleKext
|
|
|
|
? llvm::Function::WeakODRLinkage
|
|
|
|
: llvm::Function::InternalLinkage;
|
2010-06-30 20:58:07 +04:00
|
|
|
|
|
|
|
// Otherwise, we have strong external linkage.
|
|
|
|
assert(Linkage == GVA_StrongExternal);
|
|
|
|
return llvm::Function::ExternalLinkage;
|
2010-02-19 04:32:20 +03:00
|
|
|
}
|
2008-06-08 19:45:52 +04:00
|
|
|
|
2010-02-19 04:32:20 +03:00
|
|
|
|
|
|
|
/// SetFunctionDefinitionAttributes - Set attributes for a global.
|
|
|
|
///
|
|
|
|
/// FIXME: This is currently only done for aliases and functions, but not for
|
|
|
|
/// variables (these details are set in EmitGlobalVarDefinition for variables).
|
|
|
|
void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D,
|
|
|
|
llvm::GlobalValue *GV) {
|
2009-04-14 12:05:55 +04:00
|
|
|
SetCommonAttributes(D, GV);
|
2008-06-08 19:45:52 +04:00
|
|
|
}
|
|
|
|
|
2009-04-14 11:08:30 +04:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
|
2009-09-09 19:08:12 +04:00
|
|
|
const CGFunctionInfo &Info,
|
2009-04-14 11:08:30 +04:00
|
|
|
llvm::Function *F) {
|
2009-09-12 04:59:20 +04:00
|
|
|
unsigned CallingConv;
|
2008-09-26 01:02:23 +04:00
|
|
|
AttributeListType AttributeList;
|
2009-09-12 04:59:20 +04:00
|
|
|
ConstructAttributeList(Info, D, AttributeList, CallingConv);
|
2008-09-26 01:02:23 +04:00
|
|
|
F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
|
2009-09-12 04:59:20 +04:00
|
|
|
AttributeList.size()));
|
|
|
|
F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
|
2008-09-05 03:41:35 +04:00
|
|
|
}
|
|
|
|
|
2009-04-14 12:05:55 +04:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
|
|
|
|
llvm::Function *F) {
|
2011-05-25 07:44:55 +04:00
|
|
|
if (CodeGenOpts.UnwindTables)
|
|
|
|
F->setHasUWTable();
|
|
|
|
|
2009-03-02 07:58:03 +03:00
|
|
|
if (!Features.Exceptions && !Features.ObjCNonFragileABI)
|
2009-09-09 19:08:12 +04:00
|
|
|
F->addFnAttr(llvm::Attribute::NoUnwind);
|
2008-10-28 03:17:57 +03:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (D->hasAttr<AlwaysInlineAttr>())
|
2008-10-28 03:17:57 +03:00
|
|
|
F->addFnAttr(llvm::Attribute::AlwaysInline);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-09-29 22:20:25 +04:00
|
|
|
if (D->hasAttr<NakedAttr>())
|
|
|
|
F->addFnAttr(llvm::Attribute::Naked);
|
|
|
|
|
2009-08-27 02:31:08 +04:00
|
|
|
if (D->hasAttr<NoInlineAttr>())
|
2009-02-19 22:22:11 +03:00
|
|
|
F->addFnAttr(llvm::Attribute::NoInline);
|
2009-10-06 01:58:44 +04:00
|
|
|
|
2011-01-11 03:26:26 +03:00
|
|
|
if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
|
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
|
2009-11-16 19:56:03 +03:00
|
|
|
if (Features.getStackProtectorMode() == LangOptions::SSPOn)
|
|
|
|
F->addFnAttr(llvm::Attribute::StackProtect);
|
|
|
|
else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
|
|
|
|
F->addFnAttr(llvm::Attribute::StackProtectReq);
|
|
|
|
|
2010-08-19 03:23:40 +04:00
|
|
|
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
|
|
|
|
if (alignment)
|
|
|
|
F->setAlignment(alignment);
|
|
|
|
|
2009-10-06 02:49:20 +04:00
|
|
|
// C++ ABI requires 2-byte alignment for member functions.
|
2009-10-06 03:08:21 +04:00
|
|
|
if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
|
|
|
|
F->setAlignment(2);
|
2008-09-05 03:41:35 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
void CodeGenModule::SetCommonAttributes(const Decl *D,
|
2009-04-14 12:05:55 +04:00
|
|
|
llvm::GlobalValue *GV) {
|
2011-01-29 22:41:00 +03:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
setGlobalVisibility(GV, ND);
|
2010-10-23 01:05:15 +04:00
|
|
|
else
|
|
|
|
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
2009-04-14 12:05:55 +04:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (D->hasAttr<UsedAttr>())
|
2009-04-14 12:05:55 +04:00
|
|
|
AddUsedGlobal(GV);
|
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
|
2009-04-14 12:05:55 +04:00
|
|
|
GV->setSection(SA->getName());
|
2010-01-10 15:58:08 +03:00
|
|
|
|
|
|
|
getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this);
|
2009-04-14 12:05:55 +04:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:48:04 +04:00
|
|
|
void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
|
|
|
|
llvm::Function *F,
|
|
|
|
const CGFunctionInfo &FI) {
|
|
|
|
SetLLVMFunctionAttributes(D, FI, F);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, F);
|
2009-04-14 12:05:55 +04:00
|
|
|
|
|
|
|
F->setLinkage(llvm::Function::InternalLinkage);
|
|
|
|
|
2009-04-17 04:48:04 +04:00
|
|
|
SetCommonAttributes(D, F);
|
2008-09-05 03:41:35 +04:00
|
|
|
}
|
|
|
|
|
2010-02-06 05:44:09 +03:00
|
|
|
void CodeGenModule::SetFunctionAttributes(GlobalDecl GD,
|
2009-05-26 05:22:57 +04:00
|
|
|
llvm::Function *F,
|
|
|
|
bool IsIncompleteFunction) {
|
2011-04-06 16:29:04 +04:00
|
|
|
if (unsigned IID = F->getIntrinsicID()) {
|
|
|
|
// If this is an intrinsic function, set the function's attributes
|
|
|
|
// to the intrinsic's attributes.
|
|
|
|
F->setAttributes(llvm::Intrinsic::getAttributes((llvm::Intrinsic::ID)IID));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-06 05:44:09 +03:00
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
|
|
|
|
|
2009-05-26 05:22:57 +04:00
|
|
|
if (!IsIncompleteFunction)
|
2010-02-06 05:44:09 +03:00
|
|
|
SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-14 12:05:55 +04:00
|
|
|
// Only a few attributes are set on declarations; these may later be
|
|
|
|
// overridden by a definition.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (FD->hasAttr<DLLImportAttr>()) {
|
2009-04-14 12:05:55 +04:00
|
|
|
F->setLinkage(llvm::Function::DLLImportLinkage);
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (FD->hasAttr<WeakAttr>() ||
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@128127 91177308-0d34-0410-b5e6-96231b3b80d8
2011-03-23 03:50:03 +03:00
|
|
|
FD->isWeakImported()) {
|
2009-04-14 12:05:55 +04:00
|
|
|
// "extern_weak" is overloaded in LLVM; we probably should have
|
2009-09-09 19:08:12 +04:00
|
|
|
// separate linkage types for this.
|
2009-04-14 12:05:55 +04:00
|
|
|
F->setLinkage(llvm::Function::ExternalWeakLinkage);
|
|
|
|
} else {
|
2009-09-09 19:08:12 +04:00
|
|
|
F->setLinkage(llvm::Function::ExternalLinkage);
|
2010-10-30 15:50:40 +04:00
|
|
|
|
|
|
|
NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility();
|
|
|
|
if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) {
|
|
|
|
F->setVisibility(GetLLVMVisibility(LV.visibility()));
|
|
|
|
}
|
2009-04-14 12:05:55 +04:00
|
|
|
}
|
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
|
2009-04-14 12:05:55 +04:00
|
|
|
F->setSection(SA->getName());
|
2008-09-09 03:44:31 +04:00
|
|
|
}
|
|
|
|
|
2009-02-13 23:29:50 +03:00
|
|
|
void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
|
2009-09-09 19:08:12 +04:00
|
|
|
assert(!GV->isDeclaration() &&
|
2009-02-13 23:29:50 +03:00
|
|
|
"Only globals with definition can force usage.");
|
2009-04-01 02:37:52 +04:00
|
|
|
LLVMUsed.push_back(GV);
|
2009-02-13 23:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitLLVMUsed() {
|
|
|
|
// Don't create llvm.used if there is no need.
|
2009-07-18 03:57:13 +04:00
|
|
|
if (LLVMUsed.empty())
|
2009-02-13 23:29:50 +03:00
|
|
|
return;
|
|
|
|
|
2009-10-13 14:07:13 +04:00
|
|
|
const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-01 02:37:52 +04:00
|
|
|
// Convert LLVMUsed to what ConstantArray needs.
|
|
|
|
std::vector<llvm::Constant*> UsedArray;
|
|
|
|
UsedArray.resize(LLVMUsed.size());
|
|
|
|
for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
|
2009-09-09 19:08:12 +04:00
|
|
|
UsedArray[i] =
|
|
|
|
llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]),
|
2009-07-15 03:10:40 +04:00
|
|
|
i8PTy);
|
2009-04-01 02:37:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-24 01:47:46 +04:00
|
|
|
if (UsedArray.empty())
|
|
|
|
return;
|
2009-07-30 02:16:19 +04:00
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), ATy, false,
|
2009-02-13 23:29:50 +03:00
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
2009-07-28 22:33:04 +04:00
|
|
|
llvm::ConstantArray::get(ATy, UsedArray),
|
2009-07-08 23:05:04 +04:00
|
|
|
"llvm.used");
|
2009-02-13 23:29:50 +03:00
|
|
|
|
|
|
|
GV->setSection("llvm.metadata");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitDeferred() {
|
2009-03-21 12:44:56 +03:00
|
|
|
// Emit code for any potentially referenced deferred decls. Since a
|
|
|
|
// previously unused static decl may become used during the generation of code
|
|
|
|
// for a static function, iterate until no changes are made.
|
2010-03-10 05:19:29 +03:00
|
|
|
|
2010-04-18 00:15:18 +04:00
|
|
|
while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) {
|
|
|
|
if (!DeferredVTables.empty()) {
|
|
|
|
const CXXRecordDecl *RD = DeferredVTables.back();
|
|
|
|
DeferredVTables.pop_back();
|
|
|
|
getVTables().GenerateClassData(getVTableLinkage(RD), RD);
|
2010-03-10 05:19:29 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-05-05 08:44:02 +04:00
|
|
|
GlobalDecl D = DeferredDeclsToEmit.back();
|
2009-03-21 12:44:56 +03:00
|
|
|
DeferredDeclsToEmit.pop_back();
|
|
|
|
|
2010-05-27 05:45:30 +04:00
|
|
|
// Check to see if we've already emitted this. This is necessary
|
|
|
|
// for a couple of reasons: first, decls can end up in the
|
|
|
|
// deferred-decls queue multiple times, and second, decls can end
|
|
|
|
// up with definitions in unusual ways (e.g. by an extern inline
|
|
|
|
// function acquiring a strong function redefinition). Just
|
|
|
|
// ignore these cases.
|
|
|
|
//
|
|
|
|
// TODO: That said, looking this up multiple times is very wasteful.
|
2010-06-22 20:16:50 +04:00
|
|
|
llvm::StringRef Name = getMangledName(D);
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::GlobalValue *CGRef = GetGlobalValue(Name);
|
2009-03-21 12:44:56 +03:00
|
|
|
assert(CGRef && "Deferred decl wasn't referenced?");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:44:56 +03:00
|
|
|
if (!CGRef->isDeclaration())
|
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-05-27 05:45:30 +04:00
|
|
|
// GlobalAlias::isDeclaration() defers to the aliasee, but for our
|
|
|
|
// purposes an alias counts as a definition.
|
|
|
|
if (isa<llvm::GlobalAlias>(CGRef))
|
|
|
|
continue;
|
|
|
|
|
2009-03-21 12:44:56 +03:00
|
|
|
// Otherwise, emit the definition and move on to the next one.
|
|
|
|
EmitGlobalDefinition(D);
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
|
2008-04-19 08:17:09 +04:00
|
|
|
/// annotation information for a given GlobalValue. The annotation struct is
|
2009-09-09 19:08:12 +04:00
|
|
|
/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
|
|
|
|
/// GlobalValue being annotated. The second field is the constant string
|
|
|
|
/// created from the AnnotateAttr's annotation. The third field is a constant
|
2008-04-19 08:17:09 +04:00
|
|
|
/// string containing the name of the translation unit. The fourth field is
|
|
|
|
/// the line number in the file of the annotated value declaration.
|
|
|
|
///
|
|
|
|
/// FIXME: this does not unique the annotation string constants, as llvm-gcc
|
|
|
|
/// appears to.
|
|
|
|
///
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
|
2008-04-19 08:17:09 +04:00
|
|
|
const AnnotateAttr *AA,
|
|
|
|
unsigned LineNo) {
|
|
|
|
llvm::Module *M = &getModule();
|
|
|
|
|
|
|
|
// get [N x i8] constants for the annotation string, and the filename string
|
|
|
|
// which are the 2nd and 3rd elements of the global annotation structure.
|
2009-10-13 14:07:13 +04:00
|
|
|
const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext);
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *anno = llvm::ConstantArray::get(VMContext,
|
2009-08-14 01:57:51 +04:00
|
|
|
AA->getAnnotation(), true);
|
|
|
|
llvm::Constant *unit = llvm::ConstantArray::get(VMContext,
|
|
|
|
M->getModuleIdentifier(),
|
2008-04-19 08:17:09 +04:00
|
|
|
true);
|
|
|
|
|
|
|
|
// Get the two global values corresponding to the ConstantArrays we just
|
|
|
|
// created to hold the bytes of the strings.
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::GlobalValue *annoGV =
|
2009-07-16 09:03:48 +04:00
|
|
|
new llvm::GlobalVariable(*M, anno->getType(), false,
|
|
|
|
llvm::GlobalValue::PrivateLinkage, anno,
|
|
|
|
GV->getName());
|
2008-04-19 08:17:09 +04:00
|
|
|
// translation unit name string, emitted into the llvm.metadata section.
|
|
|
|
llvm::GlobalValue *unitGV =
|
2009-07-16 09:03:48 +04:00
|
|
|
new llvm::GlobalVariable(*M, unit->getType(), false,
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::GlobalValue::PrivateLinkage, unit,
|
2009-07-16 09:03:48 +04:00
|
|
|
".str");
|
2011-01-18 01:22:52 +03:00
|
|
|
unitGV->setUnnamedAddr(true);
|
2008-04-19 08:17:09 +04:00
|
|
|
|
2009-04-15 02:41:13 +04:00
|
|
|
// Create the ConstantStruct for the global annotation.
|
2008-04-19 08:17:09 +04:00
|
|
|
llvm::Constant *Fields[4] = {
|
2009-07-29 22:54:39 +04:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, SBP),
|
|
|
|
llvm::ConstantExpr::getBitCast(annoGV, SBP),
|
|
|
|
llvm::ConstantExpr::getBitCast(unitGV, SBP),
|
2009-08-14 01:57:51 +04:00
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo)
|
2008-04-19 08:17:09 +04:00
|
|
|
};
|
2011-06-20 08:01:35 +04:00
|
|
|
return llvm::ConstantStruct::getAnon(Fields);
|
2008-04-19 08:17:09 +04:00
|
|
|
}
|
|
|
|
|
2010-07-28 02:37:14 +04:00
|
|
|
bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
|
2010-07-29 22:15:58 +04:00
|
|
|
// Never defer when EmitAllDecls is specified.
|
|
|
|
if (Features.EmitAllDecls)
|
2010-07-28 02:37:14 +04:00
|
|
|
return false;
|
When a function or variable somehow depends on a type or declaration
that is in an anonymous namespace, give that function or variable
internal linkage.
This change models an oddity of the C++ standard, where names declared
in an anonymous namespace have external linkage but, because anonymous
namespace are really "uniquely-named" namespaces, the names cannot be
referenced from other translation units. That means that they have
external linkage for semantic analysis, but the only sensible
implementation for code generation is to give them internal
linkage. We now model this notion via the UniqueExternalLinkage
linkage type. There are several changes here:
- Extended NamedDecl::getLinkage() to produce UniqueExternalLinkage
when the declaration is in an anonymous namespace.
- Added Type::getLinkage() to determine the linkage of a type, which
is defined as the minimum linkage of the types (when we're dealing
with a compound type that is not a struct/class/union).
- Extended NamedDecl::getLinkage() to consider the linkage of the
template arguments and template parameters of function template
specializations and class template specializations.
- Taught code generation to rely on NamedDecl::getLinkage() when
determining the linkage of variables and functions, also
considering the linkage of the types of those variables and
functions (C++ only). Map UniqueExternalLinkage to internal
linkage, taking out the explicit checks for
isInAnonymousNamespace().
This fixes much of PR5792, which, as discovered by Anders Carlsson, is
actually the reason behind the pass-manager assertion that causes the
majority of clang-on-clang regression test failures. With this fix,
Clang-built-Clang+LLVM passes 88% of its regression tests (up from
67%). The specific numbers are:
LLVM:
Expected Passes : 4006
Expected Failures : 32
Unsupported Tests : 40
Unexpected Failures: 736
Clang:
Expected Passes : 1903
Expected Failures : 14
Unexpected Failures: 75
Overall:
Expected Passes : 5909
Expected Failures : 46
Unsupported Tests : 40
Unexpected Failures: 811
Still to do:
- Improve testing
- Check whether we should allow the presence of types with
InternalLinkage (in addition to UniqueExternalLinkage) given
variables/functions internal linkage in C++, as mentioned in
PR5792.
- Determine how expensive the getLinkage() calls are in practice;
consider caching the result in NamedDecl.
- Assess the feasibility of Chris's idea in comment #1 of PR5792.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95216 91177308-0d34-0410-b5e6-96231b3b80d8
2010-02-03 12:33:45 +03:00
|
|
|
|
2010-07-30 00:08:05 +04:00
|
|
|
return !getContext().DeclMustBeEmitted(Global);
|
2009-02-14 00:18:01 +03:00
|
|
|
}
|
|
|
|
|
2010-03-04 21:17:24 +03:00
|
|
|
llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
|
|
|
|
const AliasAttr *AA = VD->getAttr<AliasAttr>();
|
|
|
|
assert(AA && "No alias?");
|
|
|
|
|
|
|
|
const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
|
|
|
|
|
|
|
|
// See if there is already something with the target's name in the module.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
|
2010-03-04 21:17:24 +03:00
|
|
|
|
|
|
|
llvm::Constant *Aliasee;
|
|
|
|
if (isa<llvm::FunctionType>(DeclTy))
|
2011-02-05 07:35:53 +03:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(),
|
|
|
|
/*ForVTable=*/false);
|
2010-03-04 21:17:24 +03:00
|
|
|
else
|
2010-03-20 02:29:14 +03:00
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
|
2010-03-04 21:17:24 +03:00
|
|
|
llvm::PointerType::getUnqual(DeclTy), 0);
|
|
|
|
if (!Entry) {
|
|
|
|
llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
|
|
|
|
F->setLinkage(llvm::Function::ExternalWeakLinkage);
|
|
|
|
WeakRefReferences.insert(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Aliasee;
|
|
|
|
}
|
|
|
|
|
2009-05-13 01:21:08 +04:00
|
|
|
void CodeGenModule::EmitGlobal(GlobalDecl GD) {
|
2009-09-11 03:38:47 +04:00
|
|
|
const ValueDecl *Global = cast<ValueDecl>(GD.getDecl());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-04 21:17:24 +03:00
|
|
|
// Weak references don't produce any output by themselves.
|
|
|
|
if (Global->hasAttr<WeakRefAttr>())
|
|
|
|
return;
|
|
|
|
|
2009-03-23 00:47:11 +03:00
|
|
|
// If this is an alias definition (which otherwise looks like a declaration)
|
|
|
|
// emit it now.
|
2009-06-30 06:34:44 +04:00
|
|
|
if (Global->hasAttr<AliasAttr>())
|
2010-03-20 02:29:14 +03:00
|
|
|
return EmitAliasDefinition(GD);
|
2008-09-09 03:44:31 +04:00
|
|
|
|
2009-03-21 12:44:56 +03:00
|
|
|
// Ignore declarations, they will be emitted on their first use.
|
2009-03-19 11:27:24 +03:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
|
2010-07-16 04:00:19 +04:00
|
|
|
if (FD->getIdentifier()) {
|
|
|
|
llvm::StringRef Name = FD->getName();
|
|
|
|
if (Name == "_Block_object_assign") {
|
|
|
|
BlockObjectAssignDecl = FD;
|
|
|
|
} else if (Name == "_Block_object_dispose") {
|
|
|
|
BlockObjectDisposeDecl = FD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-14 00:18:01 +03:00
|
|
|
// Forward declarations are emitted lazily on first use.
|
2011-05-07 00:44:56 +04:00
|
|
|
if (!FD->doesThisDeclarationHaveABody())
|
2009-02-14 00:18:01 +03:00
|
|
|
return;
|
2009-02-13 23:29:50 +03:00
|
|
|
} else {
|
|
|
|
const VarDecl *VD = cast<VarDecl>(Global);
|
2008-07-30 03:18:29 +04:00
|
|
|
assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
|
|
|
|
|
2010-07-16 04:00:19 +04:00
|
|
|
if (VD->getIdentifier()) {
|
|
|
|
llvm::StringRef Name = VD->getName();
|
|
|
|
if (Name == "_NSConcreteGlobalBlock") {
|
|
|
|
NSConcreteGlobalBlockDecl = VD;
|
|
|
|
} else if (Name == "_NSConcreteStackBlock") {
|
|
|
|
NSConcreteStackBlockDecl = VD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-06 08:15:45 +03:00
|
|
|
if (VD->isThisDeclarationADefinition() != VarDecl::Definition)
|
2009-02-14 00:18:01 +03:00
|
|
|
return;
|
2008-04-20 10:29:50 +04:00
|
|
|
}
|
|
|
|
|
2009-03-21 12:44:56 +03:00
|
|
|
// Defer code generation when possible if this is a static definition, inline
|
|
|
|
// function etc. These we only want to emit if they are used.
|
2010-04-13 21:39:09 +04:00
|
|
|
if (!MayDeferGeneration(Global)) {
|
|
|
|
// Emit the definition if it can't be deferred.
|
|
|
|
EmitGlobalDefinition(GD);
|
2008-07-30 03:18:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-16 03:40:35 +04:00
|
|
|
|
|
|
|
// If we're deferring emission of a C++ variable with an
|
|
|
|
// initializer, remember the order in which it appeared in the file.
|
|
|
|
if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) &&
|
|
|
|
cast<VarDecl>(Global)->hasInit()) {
|
|
|
|
DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
|
|
|
|
CXXGlobalInits.push_back(0);
|
|
|
|
}
|
2010-04-13 21:39:09 +04:00
|
|
|
|
|
|
|
// If the value has already been used, add it directly to the
|
|
|
|
// DeferredDeclsToEmit list.
|
2010-06-22 20:16:50 +04:00
|
|
|
llvm::StringRef MangledName = getMangledName(GD);
|
2010-04-13 21:39:09 +04:00
|
|
|
if (GetGlobalValue(MangledName))
|
|
|
|
DeferredDeclsToEmit.push_back(GD);
|
|
|
|
else {
|
|
|
|
// Otherwise, remember that we saw a deferred decl with this name. The
|
|
|
|
// first use of the mangled name will cause it to move into
|
|
|
|
// DeferredDeclsToEmit.
|
|
|
|
DeferredDecls[MangledName] = GD;
|
|
|
|
}
|
2008-04-20 10:29:50 +04:00
|
|
|
}
|
|
|
|
|
2009-05-13 01:21:08 +04:00
|
|
|
void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) {
|
2009-09-11 03:38:47 +04:00
|
|
|
const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-19 20:39:44 +04:00
|
|
|
PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
|
2009-10-27 17:32:27 +03:00
|
|
|
Context.getSourceManager(),
|
|
|
|
"Generating code for declaration");
|
|
|
|
|
2010-07-13 10:02:28 +04:00
|
|
|
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
// At -O0, don't generate IR for functions with available_externally
|
|
|
|
// linkage.
|
2010-07-16 02:58:18 +04:00
|
|
|
if (CodeGenOpts.OptimizationLevel == 0 &&
|
|
|
|
!Function->hasAttr<AlwaysInlineAttr>() &&
|
2010-07-13 10:02:28 +04:00
|
|
|
getFunctionLinkage(Function)
|
|
|
|
== llvm::Function::AvailableExternallyLinkage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
|
2011-05-06 21:27:27 +04:00
|
|
|
// Make sure to emit the definition(s) before we emit the thunks.
|
|
|
|
// This is necessary for the generation of certain thunks.
|
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method))
|
|
|
|
EmitCXXConstructor(CD, GD.getCtorType());
|
|
|
|
else if (const CXXDestructorDecl *DD =dyn_cast<CXXDestructorDecl>(Method))
|
|
|
|
EmitCXXDestructor(DD, GD.getDtorType());
|
|
|
|
else
|
|
|
|
EmitGlobalFunctionDefinition(GD);
|
|
|
|
|
2010-07-13 10:02:28 +04:00
|
|
|
if (Method->isVirtual())
|
|
|
|
getVTables().EmitThunks(GD);
|
2010-03-23 07:31:31 +03:00
|
|
|
|
2011-05-06 21:27:27 +04:00
|
|
|
return;
|
2010-07-13 10:02:28 +04:00
|
|
|
}
|
2010-04-13 21:57:11 +04:00
|
|
|
|
|
|
|
return EmitGlobalFunctionDefinition(GD);
|
2010-07-13 10:02:28 +04:00
|
|
|
}
|
2010-04-13 21:57:11 +04:00
|
|
|
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
|
|
|
return EmitGlobalVarDefinition(VD);
|
2010-04-13 21:39:09 +04:00
|
|
|
|
|
|
|
assert(0 && "Invalid argument to EmitGlobalDefinition()");
|
2008-07-30 03:18:29 +04:00
|
|
|
}
|
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
|
|
|
|
/// module, create and return an llvm Function with the specified type. If there
|
|
|
|
/// is something in the module with the specified name, return it potentially
|
|
|
|
/// bitcasted to the right type.
|
|
|
|
///
|
|
|
|
/// If D is non-null, it specifies a decl that correspond to this. This is used
|
|
|
|
/// to set the attributes on the function when it is first created.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName,
|
|
|
|
const llvm::Type *Ty,
|
2011-06-16 03:02:42 +04:00
|
|
|
GlobalDecl D, bool ForVTable,
|
|
|
|
llvm::Attributes ExtraAttrs) {
|
2009-03-21 12:25:43 +03:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2009-03-21 12:25:43 +03:00
|
|
|
if (Entry) {
|
2010-03-04 21:17:24 +03:00
|
|
|
if (WeakRefReferences.count(Entry)) {
|
|
|
|
const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl());
|
|
|
|
if (FD && !FD->hasAttr<WeakAttr>())
|
2010-03-23 07:31:31 +03:00
|
|
|
Entry->setLinkage(llvm::Function::ExternalLinkage);
|
2010-03-04 21:17:24 +03:00
|
|
|
|
|
|
|
WeakRefReferences.erase(Entry);
|
|
|
|
}
|
|
|
|
|
2009-03-21 12:25:43 +03:00
|
|
|
if (Entry->getType()->getElementType() == Ty)
|
|
|
|
return Entry;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:25:43 +03:00
|
|
|
// Make sure the result is of the correct type.
|
2009-07-30 02:16:19 +04:00
|
|
|
const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, PTy);
|
2009-03-21 12:25:43 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-09 08:07:37 +03:00
|
|
|
// This function doesn't have a complete type (for example, the return
|
|
|
|
// type is an incomplete struct). Use a fake type instead, and make
|
|
|
|
// sure not to try to set attributes.
|
|
|
|
bool IsIncompleteFunction = false;
|
2010-04-28 04:00:30 +04:00
|
|
|
|
|
|
|
const llvm::FunctionType *FTy;
|
|
|
|
if (isa<llvm::FunctionType>(Ty)) {
|
|
|
|
FTy = cast<llvm::FunctionType>(Ty);
|
|
|
|
} else {
|
2011-05-15 05:53:33 +04:00
|
|
|
FTy = llvm::FunctionType::get(VoidTy, false);
|
2009-11-09 08:07:37 +03:00
|
|
|
IsIncompleteFunction = true;
|
|
|
|
}
|
2010-06-30 23:14:05 +04:00
|
|
|
|
2010-04-28 04:00:30 +04:00
|
|
|
llvm::Function *F = llvm::Function::Create(FTy,
|
2009-11-09 08:07:37 +03:00
|
|
|
llvm::Function::ExternalLinkage,
|
2010-03-20 02:29:14 +03:00
|
|
|
MangledName, &getModule());
|
|
|
|
assert(F->getName() == MangledName && "name was uniqued!");
|
2009-11-09 08:07:37 +03:00
|
|
|
if (D.getDecl())
|
2010-02-06 05:44:09 +03:00
|
|
|
SetFunctionAttributes(D, F, IsIncompleteFunction);
|
2011-06-16 03:02:42 +04:00
|
|
|
if (ExtraAttrs != llvm::Attribute::None)
|
|
|
|
F->addFnAttr(ExtraAttrs);
|
2009-11-09 08:07:37 +03:00
|
|
|
|
2009-03-21 12:44:56 +03:00
|
|
|
// This is the first use or definition of a mangled name. If there is a
|
|
|
|
// deferred decl with this name, remember that we need to emit it at the end
|
|
|
|
// of the file.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
|
2009-03-21 12:44:56 +03:00
|
|
|
if (DDI != DeferredDecls.end()) {
|
|
|
|
// Move the potentially referenced deferred decl to the DeferredDeclsToEmit
|
|
|
|
// list, and remove it from DeferredDecls (since we don't need it anymore).
|
|
|
|
DeferredDeclsToEmit.push_back(DDI->second);
|
|
|
|
DeferredDecls.erase(DDI);
|
2010-12-15 07:00:32 +03:00
|
|
|
|
|
|
|
// Otherwise, there are cases we have to worry about where we're
|
|
|
|
// using a declaration for which we must emit a definition but where
|
|
|
|
// we might not find a top-level definition:
|
|
|
|
// - member functions defined inline in their classes
|
|
|
|
// - friend functions defined inline in some class
|
|
|
|
// - special member functions with implicit definitions
|
|
|
|
// If we ever change our AST traversal to walk into class methods,
|
|
|
|
// this will be unnecessary.
|
2011-02-05 07:35:53 +03:00
|
|
|
//
|
|
|
|
// We also don't emit a definition for a function if it's going to be an entry
|
|
|
|
// in a vtable, unless it's already marked as used.
|
2011-02-03 09:30:58 +03:00
|
|
|
} else if (getLangOptions().CPlusPlus && D.getDecl()) {
|
2010-12-15 07:00:32 +03:00
|
|
|
// Look for a declaration that's lexically in a record.
|
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl());
|
|
|
|
do {
|
|
|
|
if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
|
2011-02-05 07:35:53 +03:00
|
|
|
if (FD->isImplicit() && !ForVTable) {
|
2010-12-15 07:00:32 +03:00
|
|
|
assert(FD->isUsed() && "Sema didn't mark implicit function as used!");
|
2011-02-09 05:03:05 +03:00
|
|
|
DeferredDeclsToEmit.push_back(D.getWithDecl(FD));
|
2010-12-15 07:00:32 +03:00
|
|
|
break;
|
2011-05-07 00:44:56 +04:00
|
|
|
} else if (FD->doesThisDeclarationHaveABody()) {
|
2011-02-09 05:03:05 +03:00
|
|
|
DeferredDeclsToEmit.push_back(D.getWithDecl(FD));
|
2010-12-15 07:00:32 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-03-03 00:28:26 +03:00
|
|
|
}
|
2010-12-15 07:00:32 +03:00
|
|
|
FD = FD->getPreviousDeclaration();
|
|
|
|
} while (FD);
|
2009-03-21 12:44:56 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-28 04:00:30 +04:00
|
|
|
// Make sure the result is of the requested type.
|
|
|
|
if (!IsIncompleteFunction) {
|
|
|
|
assert(F->getType()->getElementType() == Ty);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
|
|
|
return llvm::ConstantExpr::getBitCast(F, PTy);
|
2009-03-21 12:25:43 +03:00
|
|
|
}
|
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
/// GetAddrOfFunction - Return the address of the given function. If Ty is
|
|
|
|
/// non-null, then this function will use the specified type if it has to
|
|
|
|
/// create it (this occurs when we see a definition of the function).
|
2009-05-13 01:21:08 +04:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
|
2011-02-05 07:35:53 +03:00
|
|
|
const llvm::Type *Ty,
|
|
|
|
bool ForVTable) {
|
2009-03-23 00:03:39 +03:00
|
|
|
// If there was no specific requested type, just convert it now.
|
|
|
|
if (!Ty)
|
2009-09-11 03:38:47 +04:00
|
|
|
Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
|
2010-06-30 23:14:05 +04:00
|
|
|
|
2010-06-22 20:16:50 +04:00
|
|
|
llvm::StringRef MangledName = getMangledName(GD);
|
2011-02-05 07:35:53 +03:00
|
|
|
return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable);
|
2009-03-23 00:03:39 +03:00
|
|
|
}
|
2008-05-30 23:50:47 +04:00
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
/// CreateRuntimeFunction - Create a new runtime function with the specified
|
|
|
|
/// type and name.
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::StringRef Name,
|
|
|
|
llvm::Attributes ExtraAttrs) {
|
|
|
|
return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
|
|
|
|
ExtraAttrs);
|
2009-03-23 00:03:39 +03:00
|
|
|
}
|
2008-07-30 03:18:29 +04:00
|
|
|
|
2011-05-08 02:06:45 +04:00
|
|
|
static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D,
|
|
|
|
bool ConstantInit) {
|
2010-02-09 00:46:50 +03:00
|
|
|
if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType())
|
2009-12-12 00:23:03 +03:00
|
|
|
return false;
|
2011-05-08 02:06:45 +04:00
|
|
|
|
|
|
|
if (Context.getLangOptions().CPlusPlus) {
|
|
|
|
if (const RecordType *Record
|
|
|
|
= Context.getBaseElementType(D->getType())->getAs<RecordType>())
|
2011-05-13 05:05:07 +04:00
|
|
|
return ConstantInit &&
|
|
|
|
cast<CXXRecordDecl>(Record->getDecl())->isPOD() &&
|
|
|
|
!cast<CXXRecordDecl>(Record->getDecl())->hasMutableFields();
|
2009-12-12 00:23:03 +03:00
|
|
|
}
|
2011-05-08 02:06:45 +04:00
|
|
|
|
2009-12-12 00:23:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
|
|
|
|
/// create and return an llvm GlobalVariable with the specified type. If there
|
|
|
|
/// is something in the module with the specified name, return it potentially
|
|
|
|
/// bitcasted to the right type.
|
|
|
|
///
|
|
|
|
/// If D is non-null, it specifies a decl that correspond to this. This is used
|
|
|
|
/// to set the attributes on the global when it is first created.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName,
|
|
|
|
const llvm::PointerType *Ty,
|
2011-01-19 00:07:57 +03:00
|
|
|
const VarDecl *D,
|
|
|
|
bool UnnamedAddr) {
|
2008-08-06 03:31:02 +04:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2009-03-21 11:03:33 +03:00
|
|
|
if (Entry) {
|
2010-03-04 21:17:24 +03:00
|
|
|
if (WeakRefReferences.count(Entry)) {
|
|
|
|
if (D && !D->hasAttr<WeakAttr>())
|
2010-03-23 07:31:31 +03:00
|
|
|
Entry->setLinkage(llvm::Function::ExternalLinkage);
|
2010-03-04 21:17:24 +03:00
|
|
|
|
|
|
|
WeakRefReferences.erase(Entry);
|
|
|
|
}
|
|
|
|
|
2011-01-19 00:07:57 +03:00
|
|
|
if (UnnamedAddr)
|
|
|
|
Entry->setUnnamedAddr(true);
|
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
if (Entry->getType() == Ty)
|
2009-03-21 12:16:30 +03:00
|
|
|
return Entry;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 11:03:33 +03:00
|
|
|
// Make sure the result is of the correct type.
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, Ty);
|
2009-03-21 11:03:33 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:44:56 +03:00
|
|
|
// This is the first use or definition of a mangled name. If there is a
|
|
|
|
// deferred decl with this name, remember that we need to emit it at the end
|
|
|
|
// of the file.
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
|
2009-03-21 12:44:56 +03:00
|
|
|
if (DDI != DeferredDecls.end()) {
|
|
|
|
// Move the potentially referenced deferred decl to the DeferredDeclsToEmit
|
|
|
|
// list, and remove it from DeferredDecls (since we don't need it anymore).
|
|
|
|
DeferredDeclsToEmit.push_back(DDI->second);
|
|
|
|
DeferredDecls.erase(DDI);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
|
2009-03-21 11:03:33 +03:00
|
|
|
llvm::GlobalValue::ExternalLinkage,
|
2010-03-20 02:29:14 +03:00
|
|
|
0, MangledName, 0,
|
2009-04-20 01:05:03 +04:00
|
|
|
false, Ty->getAddressSpace());
|
2009-01-13 05:25:00 +03:00
|
|
|
|
2009-03-21 11:03:33 +03:00
|
|
|
// Handle things which are present even on external declarations.
|
2009-03-23 00:03:39 +03:00
|
|
|
if (D) {
|
2009-05-16 11:57:57 +04:00
|
|
|
// FIXME: This code is overly simple and should be merged with other global
|
|
|
|
// handling.
|
2011-05-08 02:06:45 +04:00
|
|
|
GV->setConstant(DeclIsConstantGlobal(Context, D, false));
|
2009-01-13 05:25:00 +03:00
|
|
|
|
2010-10-30 02:22:43 +04:00
|
|
|
// Set linkage and visibility in case we never see a definition.
|
2010-10-30 15:50:40 +04:00
|
|
|
NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility();
|
|
|
|
if (LV.linkage() != ExternalLinkage) {
|
2011-02-19 05:53:41 +03:00
|
|
|
// Don't set internal linkage on declarations.
|
2010-10-30 02:22:43 +04:00
|
|
|
} else {
|
|
|
|
if (D->hasAttr<DLLImportAttr>())
|
|
|
|
GV->setLinkage(llvm::GlobalValue::DLLImportLinkage);
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@128127 91177308-0d34-0410-b5e6-96231b3b80d8
2011-03-23 03:50:03 +03:00
|
|
|
else if (D->hasAttr<WeakAttr>() || D->isWeakImported())
|
2010-10-30 02:22:43 +04:00
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
|
|
|
|
|
2010-10-30 15:50:40 +04:00
|
|
|
// Set visibility on a declaration only if it's explicit.
|
|
|
|
if (LV.visibilityExplicit())
|
|
|
|
GV->setVisibility(GetLLVMVisibility(LV.visibility()));
|
2010-10-30 02:22:43 +04:00
|
|
|
}
|
2009-04-20 01:05:03 +04:00
|
|
|
|
|
|
|
GV->setThreadLocal(D->isThreadSpecified());
|
2009-03-23 00:03:39 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-20 02:29:14 +03:00
|
|
|
return GV;
|
2009-03-23 00:03:39 +03:00
|
|
|
}
|
2009-02-21 03:24:10 +03:00
|
|
|
|
|
|
|
|
2011-01-29 21:20:20 +03:00
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenModule::CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name,
|
|
|
|
const llvm::Type *Ty,
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
|
|
|
llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
|
|
|
|
llvm::GlobalVariable *OldGV = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (GV) {
|
|
|
|
// Check if the variable has the right type.
|
|
|
|
if (GV->getType()->getElementType() == Ty)
|
|
|
|
return GV;
|
|
|
|
|
|
|
|
// Because C++ name mangling, the only way we can end up with an already
|
|
|
|
// existing global with the same name is if it has been declared extern "C".
|
2011-01-29 21:25:07 +03:00
|
|
|
assert(GV->isDeclaration() && "Declaration has wrong type!");
|
2011-01-29 21:20:20 +03:00
|
|
|
OldGV = GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new variable.
|
|
|
|
GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
|
|
|
|
Linkage, 0, Name);
|
|
|
|
|
|
|
|
if (OldGV) {
|
|
|
|
// Replace occurrences of the old variable if needed.
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
|
|
|
|
if (!OldGV->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
|
|
|
|
/// given global variable. If Ty is non-null and if the global doesn't exist,
|
|
|
|
/// then it will be greated with the specified type instead of whatever the
|
|
|
|
/// normal requested type would be.
|
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
|
|
|
|
const llvm::Type *Ty) {
|
|
|
|
assert(D->hasGlobalStorage() && "Not a global variable");
|
|
|
|
QualType ASTTy = D->getType();
|
|
|
|
if (Ty == 0)
|
|
|
|
Ty = getTypes().ConvertTypeForMem(ASTTy);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
const llvm::PointerType *PTy =
|
2011-03-19 01:38:29 +03:00
|
|
|
llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
|
2010-03-20 02:29:14 +03:00
|
|
|
|
2010-06-22 20:16:50 +04:00
|
|
|
llvm::StringRef MangledName = getMangledName(D);
|
2010-03-20 02:29:14 +03:00
|
|
|
return GetOrCreateLLVMGlobal(MangledName, PTy, D);
|
2009-03-23 00:03:39 +03:00
|
|
|
}
|
2009-03-04 20:31:19 +03:00
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
/// CreateRuntimeVariable - Create a new runtime global variable with the
|
|
|
|
/// specified type and name.
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
|
2010-03-20 02:29:14 +03:00
|
|
|
llvm::StringRef Name) {
|
2011-04-07 12:22:57 +04:00
|
|
|
return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0,
|
2011-01-19 00:07:57 +03:00
|
|
|
true);
|
2008-07-30 03:18:29 +04:00
|
|
|
}
|
|
|
|
|
2009-04-16 02:08:45 +04:00
|
|
|
void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
|
|
|
|
assert(!D->getInit() && "Cannot emit definite definitions here!");
|
|
|
|
|
2009-04-21 23:28:58 +04:00
|
|
|
if (MayDeferGeneration(D)) {
|
|
|
|
// If we have not seen a reference to this variable yet, place it
|
|
|
|
// into the deferred declarations table to be emitted if needed
|
|
|
|
// later.
|
2010-06-22 20:16:50 +04:00
|
|
|
llvm::StringRef MangledName = getMangledName(D);
|
2010-03-20 02:29:14 +03:00
|
|
|
if (!GetGlobalValue(MangledName)) {
|
2009-09-11 03:43:36 +04:00
|
|
|
DeferredDecls[MangledName] = D;
|
2009-04-16 02:08:45 +04:00
|
|
|
return;
|
2009-04-21 23:28:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The tentative definition is the only definition.
|
2009-04-16 02:08:45 +04:00
|
|
|
EmitGlobalVarDefinition(D);
|
|
|
|
}
|
|
|
|
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103718 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-13 20:44:06 +04:00
|
|
|
void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) {
|
|
|
|
if (DefinitionRequired)
|
|
|
|
getVTables().GenerateClassData(getVTableLinkage(Class), Class);
|
|
|
|
}
|
|
|
|
|
2010-01-06 23:27:16 +03:00
|
|
|
llvm::GlobalVariable::LinkageTypes
|
2010-04-18 00:15:18 +04:00
|
|
|
CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
|
2011-06-11 01:53:06 +04:00
|
|
|
if (RD->getLinkage() != ExternalLinkage)
|
2010-01-07 01:00:56 +03:00
|
|
|
return llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *KeyFunction
|
|
|
|
= RD->getASTContext().getKeyFunction(RD)) {
|
|
|
|
// If this class has a key function, use that to determine the linkage of
|
|
|
|
// the vtable.
|
2010-01-06 23:27:16 +03:00
|
|
|
const FunctionDecl *Def = 0;
|
2010-07-07 15:31:19 +04:00
|
|
|
if (KeyFunction->hasBody(Def))
|
2010-01-06 23:27:16 +03:00
|
|
|
KeyFunction = cast<CXXMethodDecl>(Def);
|
2010-01-07 01:00:56 +03:00
|
|
|
|
2010-01-06 23:27:16 +03:00
|
|
|
switch (KeyFunction->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
2011-01-30 23:45:54 +03:00
|
|
|
// When compiling with optimizations turned on, we emit all vtables,
|
|
|
|
// even if the key function is not defined in the current translation
|
|
|
|
// unit. If this is the case, use available_externally linkage.
|
|
|
|
if (!Def && CodeGenOpts.OptimizationLevel)
|
|
|
|
return llvm::GlobalVariable::AvailableExternallyLinkage;
|
|
|
|
|
2010-01-06 23:27:16 +03:00
|
|
|
if (KeyFunction->isInlined())
|
2011-02-04 03:01:24 +03:00
|
|
|
return !Context.getLangOptions().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
2010-01-06 23:27:16 +03:00
|
|
|
|
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ImplicitInstantiation:
|
2011-02-04 03:01:24 +03:00
|
|
|
return !Context.getLangOptions().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
2011-01-24 03:46:19 +03:00
|
|
|
|
2010-01-06 23:27:16 +03:00
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
2011-02-04 03:01:24 +03:00
|
|
|
return !Context.getLangOptions().AppleKext ?
|
|
|
|
llvm::GlobalVariable::WeakODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
2011-01-24 03:46:19 +03:00
|
|
|
|
2010-01-06 23:27:16 +03:00
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
// FIXME: Use available_externally linkage. However, this currently
|
|
|
|
// breaks LLVM's build due to undefined symbols.
|
|
|
|
// return llvm::GlobalVariable::AvailableExternallyLinkage;
|
2011-02-04 03:01:24 +03:00
|
|
|
return !Context.getLangOptions().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
2010-01-06 23:27:16 +03:00
|
|
|
}
|
2010-01-07 01:00:56 +03:00
|
|
|
}
|
|
|
|
|
2011-02-04 03:32:39 +03:00
|
|
|
if (Context.getLangOptions().AppleKext)
|
|
|
|
return llvm::Function::InternalLinkage;
|
|
|
|
|
2010-01-07 01:00:56 +03:00
|
|
|
switch (RD->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
// FIXME: Use available_externally linkage. However, this currently
|
|
|
|
// breaks LLVM's build due to undefined symbols.
|
|
|
|
// return llvm::GlobalVariable::AvailableExternallyLinkage;
|
2011-02-04 03:01:24 +03:00
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
2011-02-04 03:32:39 +03:00
|
|
|
return llvm::GlobalVariable::LinkOnceODRLinkage;
|
2011-02-04 03:01:24 +03:00
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
2011-02-04 03:32:39 +03:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
2010-01-06 23:27:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Silence GCC warning.
|
2011-02-04 03:32:39 +03:00
|
|
|
return llvm::GlobalVariable::LinkOnceODRLinkage;
|
2010-01-06 23:27:16 +03:00
|
|
|
}
|
|
|
|
|
2010-01-26 16:48:07 +03:00
|
|
|
CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const {
|
2011-01-18 05:01:14 +03:00
|
|
|
return Context.toCharUnitsFromBits(
|
|
|
|
TheTargetData.getTypeStoreSizeInBits(Ty));
|
2010-01-26 16:48:07 +03:00
|
|
|
}
|
|
|
|
|
2008-07-30 03:18:29 +04:00
|
|
|
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
|
2007-07-14 04:23:28 +04:00
|
|
|
llvm::Constant *Init = 0;
|
2008-05-30 23:50:47 +04:00
|
|
|
QualType ASTTy = D->getType();
|
2010-01-08 03:50:11 +03:00
|
|
|
bool NonConstInit = false;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-02-01 23:16:42 +03:00
|
|
|
const Expr *InitExpr = D->getAnyInitializer();
|
2010-01-26 20:43:42 +03:00
|
|
|
|
|
|
|
if (!InitExpr) {
|
2008-05-31 00:39:54 +04:00
|
|
|
// This is a tentative definition; tentative definitions are
|
2009-04-16 02:08:45 +04:00
|
|
|
// implicitly initialized with { 0 }.
|
|
|
|
//
|
|
|
|
// Note that tentative definitions are only emitted at the end of
|
|
|
|
// a translation unit, so they should never have incomplete
|
|
|
|
// type. In addition, EmitTentativeDefinition makes sure that we
|
|
|
|
// never attempt to emit a tentative definition if a real one
|
|
|
|
// exists. A use may still exists, however, so we still may need
|
|
|
|
// to do a RAUW.
|
|
|
|
assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
|
2009-08-03 01:18:22 +04:00
|
|
|
Init = EmitNullConstant(D->getType());
|
2008-05-30 23:50:47 +04:00
|
|
|
} else {
|
2010-05-06 00:15:55 +04:00
|
|
|
Init = EmitConstantExpr(InitExpr, D->getType());
|
2009-02-20 04:18:21 +03:00
|
|
|
if (!Init) {
|
2010-01-26 20:43:42 +03:00
|
|
|
QualType T = InitExpr->getType();
|
2010-05-06 00:15:55 +04:00
|
|
|
if (D->getType()->isReferenceType())
|
|
|
|
T = D->getType();
|
|
|
|
|
2009-08-09 03:24:23 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
Init = EmitNullConstant(T);
|
2010-01-08 03:50:11 +03:00
|
|
|
NonConstInit = true;
|
2009-08-09 03:24:23 +04:00
|
|
|
} else {
|
|
|
|
ErrorUnsupported(D, "static initializer");
|
|
|
|
Init = llvm::UndefValue::get(getTypes().ConvertType(T));
|
|
|
|
}
|
2010-07-16 03:40:35 +04:00
|
|
|
} else {
|
|
|
|
// We don't need an initializer, so remove the entry for the delayed
|
|
|
|
// initializer position (just in case this entry was delayed).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
DelayedCXXInitPosition.erase(D);
|
2009-02-20 04:18:21 +03:00
|
|
|
}
|
2007-07-14 04:23:28 +04:00
|
|
|
}
|
2007-10-26 20:31:40 +04:00
|
|
|
|
2009-03-21 11:13:05 +03:00
|
|
|
const llvm::Type* InitType = Init->getType();
|
2009-03-21 12:16:30 +03:00
|
|
|
llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:16:30 +03:00
|
|
|
// Strip off a bitcast if we got one back.
|
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
|
2009-07-16 20:48:25 +04:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast ||
|
|
|
|
// all zero index gep.
|
|
|
|
CE->getOpcode() == llvm::Instruction::GetElementPtr);
|
2009-03-21 12:16:30 +03:00
|
|
|
Entry = CE->getOperand(0);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:16:30 +03:00
|
|
|
// Entry is now either a Function or GlobalVariable.
|
|
|
|
llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:16:30 +03:00
|
|
|
// We have a definition after a declaration with the wrong type.
|
|
|
|
// We must make a new GlobalVariable* and update everything that used OldGV
|
|
|
|
// (a declaration or tentative definition) with the new GlobalVariable*
|
|
|
|
// (which will be a definition).
|
|
|
|
//
|
|
|
|
// This happens if there is a prototype for a global (e.g.
|
|
|
|
// "extern int x[];") and then a definition of a different type (e.g.
|
|
|
|
// "int x[10];"). This also happens when an initializer has a different type
|
|
|
|
// from the type of the global (this happens with unions).
|
|
|
|
if (GV == 0 ||
|
|
|
|
GV->getType()->getElementType() != InitType ||
|
2011-03-19 01:38:29 +03:00
|
|
|
GV->getType()->getAddressSpace() !=
|
|
|
|
getContext().getTargetAddressSpace(ASTTy)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-20 02:29:14 +03:00
|
|
|
// Move the old entry aside so that we'll create a new one.
|
|
|
|
Entry->setName(llvm::StringRef());
|
2009-02-19 08:36:41 +03:00
|
|
|
|
2009-03-21 12:16:30 +03:00
|
|
|
// Make a new global with the correct type, this is now guaranteed to work.
|
|
|
|
GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
|
2009-03-21 12:25:43 +03:00
|
|
|
|
2008-05-30 23:50:47 +04:00
|
|
|
// Replace all uses of the old global with the new global
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2009-07-29 22:54:39 +04:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, Entry->getType());
|
2009-03-21 12:16:30 +03:00
|
|
|
Entry->replaceAllUsesWith(NewPtrForOldDecl);
|
2008-05-30 23:50:47 +04:00
|
|
|
|
|
|
|
// Erase the old global, since it is no longer used.
|
2009-03-21 12:16:30 +03:00
|
|
|
cast<llvm::GlobalValue>(Entry)->eraseFromParent();
|
2008-05-30 23:50:47 +04:00
|
|
|
}
|
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
|
2008-04-19 08:17:09 +04:00
|
|
|
SourceManager &SM = Context.getSourceManager();
|
|
|
|
AddAnnotation(EmitAnnotateAttr(GV, AA,
|
2009-01-16 10:36:28 +03:00
|
|
|
SM.getInstantiationLineNumber(D->getLocation())));
|
2008-04-19 08:17:09 +04:00
|
|
|
}
|
|
|
|
|
2007-07-13 09:13:43 +04:00
|
|
|
GV->setInitializer(Init);
|
2009-08-05 09:20:29 +04:00
|
|
|
|
|
|
|
// If it is safe to mark the global 'constant', do so now.
|
|
|
|
GV->setConstant(false);
|
2011-05-08 02:06:45 +04:00
|
|
|
if (!NonConstInit && DeclIsConstantGlobal(Context, D, true))
|
2009-08-05 09:20:29 +04:00
|
|
|
GV->setConstant(true);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-01-27 20:10:57 +03:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
|
2010-10-27 20:21:54 +04:00
|
|
|
|
2007-07-13 09:13:43 +04:00
|
|
|
// Set the llvm linkage type as appropriate.
|
2010-10-27 20:21:54 +04:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
|
|
|
GetLLVMLinkageVarDefinition(D, GV);
|
|
|
|
GV->setLinkage(Linkage);
|
|
|
|
if (Linkage == llvm::GlobalVariable::CommonLinkage)
|
|
|
|
// common vars aren't constant even if declared const.
|
|
|
|
GV->setConstant(false);
|
|
|
|
|
|
|
|
SetCommonAttributes(D, GV);
|
|
|
|
|
2010-11-06 12:44:32 +03:00
|
|
|
// Emit the initializer function if necessary.
|
|
|
|
if (NonConstInit)
|
|
|
|
EmitCXXGlobalVarDeclInitFunc(D, GV);
|
|
|
|
|
2010-10-27 20:21:54 +04:00
|
|
|
// Emit global variable debug information.
|
2011-03-07 21:45:56 +03:00
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo()) {
|
2010-10-27 20:21:54 +04:00
|
|
|
DI->setLocation(D->getLocation());
|
|
|
|
DI->EmitGlobalVariable(GV, D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalValue::LinkageTypes
|
|
|
|
CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D,
|
|
|
|
llvm::GlobalVariable *GV) {
|
2010-07-29 22:15:58 +04:00
|
|
|
GVALinkage Linkage = getContext().GetGVALinkageForVariable(D);
|
2009-10-15 01:36:34 +04:00
|
|
|
if (Linkage == GVA_Internal)
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::Function::InternalLinkage;
|
2009-06-30 06:34:44 +04:00
|
|
|
else if (D->hasAttr<DLLImportAttr>())
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::Function::DLLImportLinkage;
|
2009-06-30 06:34:44 +04:00
|
|
|
else if (D->hasAttr<DLLExportAttr>())
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::Function::DLLExportLinkage;
|
2009-08-05 09:20:29 +04:00
|
|
|
else if (D->hasAttr<WeakAttr>()) {
|
|
|
|
if (GV->isConstant())
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
2009-08-05 09:20:29 +04:00
|
|
|
else
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::GlobalVariable::WeakAnyLinkage;
|
2010-03-13 21:23:07 +03:00
|
|
|
} else if (Linkage == GVA_TemplateInstantiation ||
|
|
|
|
Linkage == GVA_ExplicitTemplateInstantiation)
|
2011-04-12 05:46:54 +04:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
2010-12-02 05:45:55 +03:00
|
|
|
else if (!getLangOptions().CPlusPlus &&
|
|
|
|
((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) ||
|
|
|
|
D->getAttr<CommonAttr>()) &&
|
2009-08-05 08:56:58 +04:00
|
|
|
!D->hasExternalStorage() && !D->getInit() &&
|
2011-06-20 21:50:03 +04:00
|
|
|
!D->getAttr<SectionAttr>() && !D->isThreadSpecified() &&
|
|
|
|
!D->getAttr<WeakImportAttr>()) {
|
2010-08-08 05:37:14 +04:00
|
|
|
// Thread local vars aren't considered common linkage.
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::GlobalVariable::CommonLinkage;
|
2008-06-05 12:59:10 +04:00
|
|
|
}
|
2010-10-27 20:21:54 +04:00
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
2007-07-13 09:13:43 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2009-05-05 10:16:31 +04:00
|
|
|
/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
|
|
|
|
/// implement a function with no prototype, e.g. "int foo() {}". If there are
|
|
|
|
/// existing call uses of the old function in the module, this adjusts them to
|
|
|
|
/// call the new function directly.
|
|
|
|
///
|
|
|
|
/// This is not just a cleanup: the always_inline pass requires direct calls to
|
|
|
|
/// functions to be able to inline them. If there is a bitcast in the way, it
|
|
|
|
/// won't inline them. Instcombine normally deletes these calls, but it isn't
|
|
|
|
/// run at -O0.
|
|
|
|
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
|
|
|
|
llvm::Function *NewFn) {
|
|
|
|
// If we're redefining a global as a function, don't transform it.
|
|
|
|
llvm::Function *OldFn = dyn_cast<llvm::Function>(Old);
|
|
|
|
if (OldFn == 0) return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-05 10:16:31 +04:00
|
|
|
const llvm::Type *NewRetTy = NewFn->getReturnType();
|
|
|
|
llvm::SmallVector<llvm::Value*, 4> ArgList;
|
|
|
|
|
|
|
|
for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end();
|
2010-04-10 15:02:40 +04:00
|
|
|
UI != E; ) {
|
2009-05-05 10:16:31 +04:00
|
|
|
// TODO: Do invokes ever occur in C code? If so, we should handle them too.
|
2010-04-10 15:02:40 +04:00
|
|
|
llvm::Value::use_iterator I = UI++; // Increment before the CI is erased.
|
|
|
|
llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I);
|
2010-07-28 13:19:33 +04:00
|
|
|
if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I)
|
2010-04-10 07:45:50 +04:00
|
|
|
llvm::CallSite CS(CI);
|
2010-04-10 15:02:40 +04:00
|
|
|
if (!CI || !CS.isCallee(I)) continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-05 10:16:31 +04:00
|
|
|
// If the return types don't match exactly, and if the call isn't dead, then
|
|
|
|
// we can't transform this call.
|
|
|
|
if (CI->getType() != NewRetTy && !CI->use_empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the function was passed too few arguments, don't transform. If extra
|
|
|
|
// arguments were passed, we silently drop them. If any of the types
|
|
|
|
// mismatch, we don't transform.
|
|
|
|
unsigned ArgNo = 0;
|
|
|
|
bool DontTransform = false;
|
|
|
|
for (llvm::Function::arg_iterator AI = NewFn->arg_begin(),
|
|
|
|
E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) {
|
2010-04-10 07:45:50 +04:00
|
|
|
if (CS.arg_size() == ArgNo ||
|
|
|
|
CS.getArgument(ArgNo)->getType() != AI->getType()) {
|
2009-05-05 10:16:31 +04:00
|
|
|
DontTransform = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DontTransform)
|
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-05 10:16:31 +04:00
|
|
|
// Okay, we can transform this. Create the new call instruction and copy
|
|
|
|
// over the required information.
|
2010-04-10 06:56:12 +04:00
|
|
|
ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo);
|
2009-05-05 10:16:31 +04:00
|
|
|
llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(),
|
|
|
|
ArgList.end(), "", CI);
|
|
|
|
ArgList.clear();
|
2009-10-05 17:47:21 +04:00
|
|
|
if (!NewCall->getType()->isVoidTy())
|
2009-05-05 10:16:31 +04:00
|
|
|
NewCall->takeName(CI);
|
|
|
|
NewCall->setAttributes(CI->getAttributes());
|
2009-09-12 04:59:20 +04:00
|
|
|
NewCall->setCallingConv(CI->getCallingConv());
|
2009-05-05 10:16:31 +04:00
|
|
|
|
|
|
|
// Finally, remove the old call, replacing any uses with the new one.
|
2009-10-14 09:49:21 +04:00
|
|
|
if (!CI->use_empty())
|
|
|
|
CI->replaceAllUsesWith(NewCall);
|
2009-10-13 21:02:04 +04:00
|
|
|
|
2010-04-01 10:31:43 +04:00
|
|
|
// Copy debug location attached to CI.
|
|
|
|
if (!CI->getDebugLoc().isUnknown())
|
|
|
|
NewCall->setDebugLoc(CI->getDebugLoc());
|
2009-05-05 10:16:31 +04:00
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-30 03:18:29 +04:00
|
|
|
|
2009-05-13 01:21:08 +04:00
|
|
|
void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) {
|
|
|
|
const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl());
|
2011-03-09 07:27:21 +03:00
|
|
|
|
2011-03-09 11:12:35 +03:00
|
|
|
// Compute the function info and LLVM type.
|
2011-03-09 07:27:21 +03:00
|
|
|
const CGFunctionInfo &FI = getTypes().getFunctionInfo(GD);
|
2011-03-09 11:12:35 +03:00
|
|
|
bool variadic = false;
|
|
|
|
if (const FunctionProtoType *fpt = D->getType()->getAs<FunctionProtoType>())
|
|
|
|
variadic = fpt->isVariadic();
|
|
|
|
const llvm::FunctionType *Ty = getTypes().GetFunctionType(FI, variadic, false);
|
2011-03-09 07:27:21 +03:00
|
|
|
|
2009-05-13 00:58:15 +04:00
|
|
|
// Get or create the prototype for the function.
|
2009-05-13 01:21:08 +04:00
|
|
|
llvm::Constant *Entry = GetAddrOfFunction(GD, Ty);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:25:43 +03:00
|
|
|
// Strip off a bitcast if we got one back.
|
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
|
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast);
|
|
|
|
Entry = CE->getOperand(0);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
|
2009-03-21 12:25:43 +03:00
|
|
|
if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) {
|
2009-05-05 10:16:31 +04:00
|
|
|
llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-10 02:53:08 +03:00
|
|
|
// If the types mismatch then we have to rewrite the definition.
|
2009-05-05 10:16:31 +04:00
|
|
|
assert(OldFn->isDeclaration() &&
|
2009-03-21 12:25:43 +03:00
|
|
|
"Shouldn't replace non-declaration");
|
2009-03-21 11:53:37 +03:00
|
|
|
|
2009-03-21 11:38:50 +03:00
|
|
|
// F is the Function* for the one with the wrong type, we must make a new
|
|
|
|
// Function* and update everything that used F (a declaration) with the new
|
|
|
|
// Function* (which will be a definition).
|
|
|
|
//
|
|
|
|
// This happens if there is a prototype for a function
|
|
|
|
// (e.g. "int f()") and then a definition of a different type
|
2010-03-20 02:29:14 +03:00
|
|
|
// (e.g. "int f(int x)"). Move the old function aside so that it
|
|
|
|
// doesn't interfere with GetAddrOfFunction.
|
|
|
|
OldFn->setName(llvm::StringRef());
|
2009-05-13 01:21:08 +04:00
|
|
|
llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-05 10:16:31 +04:00
|
|
|
// If this is an implementation of a function without a prototype, try to
|
|
|
|
// replace any existing uses of the function (which may be calls) with uses
|
|
|
|
// of the new function
|
2009-05-13 00:58:15 +04:00
|
|
|
if (D->getType()->isFunctionNoProtoType()) {
|
2009-05-05 10:16:31 +04:00
|
|
|
ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn);
|
2009-05-13 00:58:15 +04:00
|
|
|
OldFn->removeDeadConstantUsers();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 11:38:50 +03:00
|
|
|
// Replace uses of F with the Function we will endow with a body.
|
2009-05-05 10:16:31 +04:00
|
|
|
if (!Entry->use_empty()) {
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2009-07-29 22:54:39 +04:00
|
|
|
llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
|
2009-05-05 10:16:31 +04:00
|
|
|
Entry->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 11:38:50 +03:00
|
|
|
// Ok, delete the old function now, which is dead.
|
2009-05-05 10:16:31 +04:00
|
|
|
OldFn->eraseFromParent();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-21 12:25:43 +03:00
|
|
|
Entry = NewFn;
|
2008-07-30 03:18:29 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-11-03 00:04:24 +03:00
|
|
|
// We need to set linkage and visibility on the function before
|
|
|
|
// generating code for it because various parts of IR generation
|
|
|
|
// want to propagate this information down (e.g. to local static
|
|
|
|
// declarations).
|
2009-03-21 12:25:43 +03:00
|
|
|
llvm::Function *Fn = cast<llvm::Function>(Entry);
|
2010-05-25 08:30:21 +04:00
|
|
|
setFunctionLinkage(D, Fn);
|
2008-07-30 03:18:29 +04:00
|
|
|
|
2010-11-03 00:04:24 +03:00
|
|
|
// FIXME: this is redundant with part of SetFunctionDefinitionAttributes
|
2011-01-29 22:39:23 +03:00
|
|
|
setGlobalVisibility(Fn, D);
|
2010-11-03 00:04:24 +03:00
|
|
|
|
2011-03-09 07:27:21 +03:00
|
|
|
CodeGenFunction(*this).GenerateCode(D, Fn, FI);
|
2008-08-11 21:36:14 +04:00
|
|
|
|
2009-04-14 12:05:55 +04:00
|
|
|
SetFunctionDefinitionAttributes(D, Fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, Fn);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-30 06:34:44 +04:00
|
|
|
if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
|
2008-09-09 03:44:31 +04:00
|
|
|
AddGlobalCtor(Fn, CA->getPriority());
|
2009-06-30 06:34:44 +04:00
|
|
|
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
|
2008-09-09 03:44:31 +04:00
|
|
|
AddGlobalDtor(Fn, DA->getPriority());
|
2008-07-30 03:18:29 +04:00
|
|
|
}
|
|
|
|
|
2010-03-20 02:29:14 +03:00
|
|
|
void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
|
|
|
|
const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
|
2009-06-30 06:34:44 +04:00
|
|
|
const AliasAttr *AA = D->getAttr<AliasAttr>();
|
2009-03-23 00:47:11 +03:00
|
|
|
assert(AA && "Not an alias?");
|
|
|
|
|
2010-06-22 20:16:50 +04:00
|
|
|
llvm::StringRef MangledName = getMangledName(GD);
|
2010-03-20 02:29:14 +03:00
|
|
|
|
|
|
|
// If there is a definition in the module, then it wins over the alias.
|
|
|
|
// This is dubious, but allow it to be safe. Just ignore the alias.
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (Entry && !Entry->isDeclaration())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-03-20 02:29:14 +03:00
|
|
|
const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
|
2009-03-23 00:47:11 +03:00
|
|
|
|
|
|
|
// Create a reference to the named value. This ensures that it is emitted
|
|
|
|
// if a deferred decl.
|
|
|
|
llvm::Constant *Aliasee;
|
|
|
|
if (isa<llvm::FunctionType>(DeclTy))
|
2011-02-05 07:35:53 +03:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(),
|
|
|
|
/*ForVTable=*/false);
|
2009-03-23 00:47:11 +03:00
|
|
|
else
|
2010-03-20 02:29:14 +03:00
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
|
2009-07-30 02:16:19 +04:00
|
|
|
llvm::PointerType::getUnqual(DeclTy), 0);
|
2009-03-23 00:47:11 +03:00
|
|
|
|
|
|
|
// Create the new alias itself, but don't set a name yet.
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::GlobalValue *GA =
|
2009-03-23 00:47:11 +03:00
|
|
|
new llvm::GlobalAlias(Aliasee->getType(),
|
|
|
|
llvm::Function::ExternalLinkage,
|
|
|
|
"", Aliasee, &getModule());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-23 00:47:11 +03:00
|
|
|
if (Entry) {
|
2010-03-20 02:29:14 +03:00
|
|
|
assert(Entry->isDeclaration());
|
|
|
|
|
2009-03-23 00:47:11 +03:00
|
|
|
// If there is a declaration in the module, then we had an extern followed
|
|
|
|
// by the alias, as in:
|
|
|
|
// extern int test6();
|
|
|
|
// ...
|
|
|
|
// int test6() __attribute__((alias("test7")));
|
|
|
|
//
|
|
|
|
// Remove it and replace uses of it with the alias.
|
2010-03-20 02:29:14 +03:00
|
|
|
GA->takeName(Entry);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-29 22:54:39 +04:00
|
|
|
Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
|
2009-03-23 00:47:11 +03:00
|
|
|
Entry->getType()));
|
|
|
|
Entry->eraseFromParent();
|
2010-03-20 02:29:14 +03:00
|
|
|
} else {
|
2010-06-22 20:16:50 +04:00
|
|
|
GA->setName(MangledName);
|
2009-03-23 00:47:11 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-14 12:05:55 +04:00
|
|
|
// Set attributes which are particular to an alias; this is a
|
|
|
|
// specialization of the attributes which may be set on a global
|
|
|
|
// variable/function.
|
2009-06-30 06:34:44 +04:00
|
|
|
if (D->hasAttr<DLLExportAttr>()) {
|
2009-04-14 12:05:55 +04:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
// The dllexport attribute is ignored for undefined symbols.
|
2010-07-07 15:31:19 +04:00
|
|
|
if (FD->hasBody())
|
2009-04-14 12:05:55 +04:00
|
|
|
GA->setLinkage(llvm::Function::DLLExportLinkage);
|
|
|
|
} else {
|
|
|
|
GA->setLinkage(llvm::Function::DLLExportLinkage);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (D->hasAttr<WeakAttr>() ||
|
2010-02-24 01:00:30 +03:00
|
|
|
D->hasAttr<WeakRefAttr>() ||
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@128127 91177308-0d34-0410-b5e6-96231b3b80d8
2011-03-23 03:50:03 +03:00
|
|
|
D->isWeakImported()) {
|
2009-04-14 12:05:55 +04:00
|
|
|
GA->setLinkage(llvm::Function::WeakAnyLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCommonAttributes(D, GA);
|
2009-03-23 00:47:11 +03:00
|
|
|
}
|
|
|
|
|
2009-03-23 00:56:56 +03:00
|
|
|
/// getBuiltinLibFunction - Given a builtin id for a function like
|
|
|
|
/// "__builtin_fabsf", return a Function* for "fabsf".
|
2009-09-14 08:33:21 +04:00
|
|
|
llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
|
|
|
|
unsigned BuiltinID) {
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
|
2009-09-09 19:08:12 +04:00
|
|
|
Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
"isn't a lib fn");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-14 02:20:09 +03:00
|
|
|
// Get the name, skip over the __builtin_ prefix (if necessary).
|
2011-05-14 01:12:10 +04:00
|
|
|
llvm::StringRef Name;
|
|
|
|
GlobalDecl D(FD);
|
|
|
|
|
|
|
|
// If the builtin has been declared explicitly with an assembler label,
|
|
|
|
// use the mangled name. This differs from the plain label on platforms
|
|
|
|
// that prefix labels.
|
2011-05-14 02:58:37 +04:00
|
|
|
if (FD->hasAttr<AsmLabelAttr>())
|
2011-05-14 01:12:10 +04:00
|
|
|
Name = getMangledName(D);
|
|
|
|
else if (Context.BuiltinInfo.isLibFunction(BuiltinID))
|
|
|
|
Name = Context.BuiltinInfo.GetName(BuiltinID) + 10;
|
|
|
|
else
|
|
|
|
Name = Context.BuiltinInfo.GetName(BuiltinID);
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
const llvm::FunctionType *Ty =
|
2009-12-09 06:05:59 +03:00
|
|
|
cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
|
2007-08-31 08:31:45 +04:00
|
|
|
|
2011-05-14 01:12:10 +04:00
|
|
|
return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false);
|
2007-08-31 08:31:45 +04:00
|
|
|
}
|
|
|
|
|
2007-12-18 03:25:38 +03:00
|
|
|
llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
|
|
|
|
unsigned NumTys) {
|
|
|
|
return llvm::Intrinsic::getDeclaration(&getModule(),
|
|
|
|
(llvm::Intrinsic::ID)IID, Tys, NumTys);
|
|
|
|
}
|
2007-08-31 08:31:45 +04:00
|
|
|
|
2009-07-24 02:52:48 +04:00
|
|
|
static llvm::StringMapEntry<llvm::Constant*> &
|
|
|
|
GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map,
|
|
|
|
const StringLiteral *Literal,
|
2009-07-24 03:41:22 +04:00
|
|
|
bool TargetIsLSB,
|
2009-07-24 02:52:48 +04:00
|
|
|
bool &IsUTF16,
|
|
|
|
unsigned &StringLength) {
|
2010-08-17 16:54:38 +04:00
|
|
|
llvm::StringRef String = Literal->getString();
|
|
|
|
unsigned NumBytes = String.size();
|
2009-07-24 02:52:48 +04:00
|
|
|
|
2009-09-22 14:03:52 +04:00
|
|
|
// Check for simple case.
|
|
|
|
if (!Literal->containsNonAsciiOrNull()) {
|
|
|
|
StringLength = NumBytes;
|
2010-08-17 16:54:38 +04:00
|
|
|
return Map.GetOrCreateValue(String);
|
2009-09-22 14:03:52 +04:00
|
|
|
}
|
|
|
|
|
2009-07-24 02:52:48 +04:00
|
|
|
// Otherwise, convert the UTF8 literals into a byte string.
|
|
|
|
llvm::SmallVector<UTF16, 128> ToBuf(NumBytes);
|
2010-08-17 16:54:38 +04:00
|
|
|
const UTF8 *FromPtr = (UTF8 *)String.data();
|
2009-07-24 02:52:48 +04:00
|
|
|
UTF16 *ToPtr = &ToBuf[0];
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-09-07 23:57:04 +04:00
|
|
|
(void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
|
|
|
|
&ToPtr, ToPtr + NumBytes,
|
|
|
|
strictConversion);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-24 03:41:22 +04:00
|
|
|
// ConvertUTF8toUTF16 returns the length in ToPtr.
|
2009-07-24 02:52:48 +04:00
|
|
|
StringLength = ToPtr - &ToBuf[0];
|
2009-07-24 03:41:22 +04:00
|
|
|
|
|
|
|
// Render the UTF-16 string into a byte array and convert to the target byte
|
|
|
|
// order.
|
|
|
|
//
|
|
|
|
// FIXME: This isn't something we should need to do here.
|
|
|
|
llvm::SmallString<128> AsBytes;
|
|
|
|
AsBytes.reserve(StringLength * 2);
|
|
|
|
for (unsigned i = 0; i != StringLength; ++i) {
|
|
|
|
unsigned short Val = ToBuf[i];
|
|
|
|
if (TargetIsLSB) {
|
|
|
|
AsBytes.push_back(Val & 0xFF);
|
|
|
|
AsBytes.push_back(Val >> 8);
|
|
|
|
} else {
|
|
|
|
AsBytes.push_back(Val >> 8);
|
|
|
|
AsBytes.push_back(Val & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
2009-08-04 01:47:08 +04:00
|
|
|
// Append one extra null character, the second is automatically added by our
|
|
|
|
// caller.
|
|
|
|
AsBytes.push_back(0);
|
2009-07-24 03:41:22 +04:00
|
|
|
|
2009-07-24 02:52:48 +04:00
|
|
|
IsUTF16 = true;
|
2009-07-24 03:41:22 +04:00
|
|
|
return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size()));
|
2009-07-24 02:52:48 +04:00
|
|
|
}
|
|
|
|
|
2011-05-13 22:13:10 +04:00
|
|
|
static llvm::StringMapEntry<llvm::Constant*> &
|
|
|
|
GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map,
|
|
|
|
const StringLiteral *Literal,
|
|
|
|
unsigned &StringLength)
|
|
|
|
{
|
|
|
|
llvm::StringRef String = Literal->getString();
|
|
|
|
StringLength = String.size();
|
|
|
|
return Map.GetOrCreateValue(String);
|
|
|
|
}
|
|
|
|
|
2009-07-24 02:52:48 +04:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
|
|
|
|
unsigned StringLength = 0;
|
|
|
|
bool isUTF16 = false;
|
|
|
|
llvm::StringMapEntry<llvm::Constant*> &Entry =
|
2009-09-09 19:08:12 +04:00
|
|
|
GetConstantCFStringEntry(CFConstantStringMap, Literal,
|
2009-07-24 03:41:22 +04:00
|
|
|
getTargetData().isLittleEndian(),
|
|
|
|
isUTF16, StringLength);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-24 02:52:48 +04:00
|
|
|
if (llvm::Constant *C = Entry.getValue())
|
|
|
|
return C;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-14 01:57:51 +04:00
|
|
|
llvm::Constant *Zero =
|
|
|
|
llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
|
2008-08-23 22:37:06 +04:00
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-16 20:48:25 +04:00
|
|
|
// If we don't already have it, get __CFConstantStringClassReference.
|
2007-08-21 04:21:21 +04:00
|
|
|
if (!CFConstantStringClassRef) {
|
|
|
|
const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
|
2009-07-30 02:16:19 +04:00
|
|
|
Ty = llvm::ArrayType::get(Ty, 0);
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *GV = CreateRuntimeVariable(Ty,
|
2009-07-16 20:48:25 +04:00
|
|
|
"__CFConstantStringClassReference");
|
2008-08-23 22:37:06 +04:00
|
|
|
// Decay array -> ptr
|
|
|
|
CFConstantStringClassRef =
|
2009-07-29 22:54:39 +04:00
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
|
2007-08-21 04:21:21 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-11-15 21:54:24 +03:00
|
|
|
QualType CFTy = getContext().getCFConstantStringType();
|
2008-08-23 22:37:06 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
const llvm::StructType *STy =
|
2008-11-15 21:54:24 +03:00
|
|
|
cast<llvm::StructType>(getTypes().ConvertType(CFTy));
|
|
|
|
|
2009-08-16 09:55:31 +04:00
|
|
|
std::vector<llvm::Constant*> Fields(4);
|
2008-12-11 19:49:14 +03:00
|
|
|
|
2007-08-21 04:21:21 +04:00
|
|
|
// Class pointer.
|
2009-08-16 09:55:31 +04:00
|
|
|
Fields[0] = CFConstantStringClassRef;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-21 04:21:21 +04:00
|
|
|
// Flags.
|
2008-08-23 22:37:06 +04:00
|
|
|
const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
2009-09-09 19:08:12 +04:00
|
|
|
Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
|
2009-08-16 09:55:31 +04:00
|
|
|
llvm::ConstantInt::get(Ty, 0x07C8);
|
|
|
|
|
2007-08-21 04:21:21 +04:00
|
|
|
// String pointer.
|
2009-08-14 01:57:51 +04:00
|
|
|
llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
|
2009-04-03 04:57:44 +04:00
|
|
|
|
2009-07-16 09:03:48 +04:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
2009-10-14 09:55:45 +04:00
|
|
|
bool isConstant;
|
2009-04-03 04:57:44 +04:00
|
|
|
if (isUTF16) {
|
2009-10-14 09:55:45 +04:00
|
|
|
// FIXME: why do utf strings get "_" labels instead of "L" labels?
|
2009-07-16 09:03:48 +04:00
|
|
|
Linkage = llvm::GlobalValue::InternalLinkage;
|
2009-10-14 09:55:45 +04:00
|
|
|
// Note: -fwritable-strings doesn't make unicode CFStrings writable, but
|
|
|
|
// does make plain ascii ones writable.
|
|
|
|
isConstant = true;
|
2009-04-03 04:57:44 +04:00
|
|
|
} else {
|
2011-03-14 20:55:00 +03:00
|
|
|
// FIXME: With OS X ld 123.2 (xcode 4) and LTO we would get a linker error
|
|
|
|
// when using private linkage. It is not clear if this is a bug in ld
|
|
|
|
// or a reasonable new restriction.
|
2011-03-15 00:08:19 +03:00
|
|
|
Linkage = llvm::GlobalValue::LinkerPrivateLinkage;
|
2009-10-14 09:55:45 +04:00
|
|
|
isConstant = !Features.WritableStrings;
|
2009-04-03 04:57:44 +04:00
|
|
|
}
|
2009-10-14 09:55:45 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::GlobalVariable *GV =
|
2009-10-14 09:55:45 +04:00
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
|
|
|
|
".str");
|
2011-01-17 19:31:00 +03:00
|
|
|
GV->setUnnamedAddr(true);
|
2009-04-03 04:57:44 +04:00
|
|
|
if (isUTF16) {
|
2010-01-26 21:46:23 +03:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2011-04-13 03:30:52 +04:00
|
|
|
} else {
|
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2009-04-03 04:57:44 +04:00
|
|
|
}
|
2009-08-16 09:55:31 +04:00
|
|
|
Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
|
|
|
|
|
2007-08-21 04:21:21 +04:00
|
|
|
// String length.
|
|
|
|
Ty = getTypes().ConvertType(getContext().LongTy);
|
2009-08-16 09:55:31 +04:00
|
|
|
Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-08-21 04:21:21 +04:00
|
|
|
// The struct.
|
2009-07-28 02:29:56 +04:00
|
|
|
C = llvm::ConstantStruct::get(STy, Fields);
|
2009-09-09 19:08:12 +04:00
|
|
|
GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage, C,
|
2009-07-16 09:03:48 +04:00
|
|
|
"_unnamed_cfstring_");
|
2009-04-01 03:42:16 +04:00
|
|
|
if (const char *Sect = getContext().Target.getCFStringSection())
|
|
|
|
GV->setSection(Sect);
|
2009-07-24 02:52:48 +04:00
|
|
|
Entry.setValue(GV);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-11-01 03:41:52 +03:00
|
|
|
return GV;
|
2007-08-21 04:21:21 +04:00
|
|
|
}
|
2007-11-28 08:34:05 +03:00
|
|
|
|
2010-04-23 00:26:39 +04:00
|
|
|
llvm::Constant *
|
2010-10-19 21:19:29 +04:00
|
|
|
CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
|
2010-04-23 21:41:07 +04:00
|
|
|
unsigned StringLength = 0;
|
|
|
|
llvm::StringMapEntry<llvm::Constant*> &Entry =
|
2011-05-13 22:13:10 +04:00
|
|
|
GetConstantStringEntry(CFConstantStringMap, Literal, StringLength);
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
if (llvm::Constant *C = Entry.getValue())
|
|
|
|
return C;
|
|
|
|
|
|
|
|
llvm::Constant *Zero =
|
|
|
|
llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
|
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
|
|
|
|
2010-04-24 02:33:39 +04:00
|
|
|
// If we don't already have it, get _NSConstantStringClassReference.
|
2010-10-19 21:19:29 +04:00
|
|
|
if (!ConstantStringClassRef) {
|
|
|
|
std::string StringClass(getLangOptions().ObjCConstantStringClass);
|
2010-04-23 21:41:07 +04:00
|
|
|
const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
|
2010-10-19 21:19:29 +04:00
|
|
|
llvm::Constant *GV;
|
2011-05-18 02:21:16 +04:00
|
|
|
if (Features.ObjCNonFragileABI) {
|
2011-05-18 02:46:11 +04:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "OBJC_CLASS_$_NSConstantString"
|
|
|
|
: "OBJC_CLASS_$_" + StringClass;
|
2011-05-18 02:21:16 +04:00
|
|
|
GV = getObjCRuntime().GetClassGlobal(str);
|
|
|
|
// Make sure the result is of the correct type.
|
|
|
|
const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
|
|
|
ConstantStringClassRef =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, PTy);
|
|
|
|
} else {
|
2011-05-18 02:46:11 +04:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "_NSConstantStringClassReference"
|
|
|
|
: "_" + StringClass + "ClassReference";
|
|
|
|
const llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
|
|
|
|
GV = CreateRuntimeVariable(PTy, str);
|
2011-05-18 02:21:16 +04:00
|
|
|
// Decay array -> ptr
|
|
|
|
ConstantStringClassRef =
|
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
|
2010-10-19 21:19:29 +04:00
|
|
|
}
|
2010-04-23 21:41:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QualType NSTy = getContext().getNSConstantStringType();
|
|
|
|
|
|
|
|
const llvm::StructType *STy =
|
|
|
|
cast<llvm::StructType>(getTypes().ConvertType(NSTy));
|
|
|
|
|
|
|
|
std::vector<llvm::Constant*> Fields(3);
|
|
|
|
|
|
|
|
// Class pointer.
|
2010-10-19 21:19:29 +04:00
|
|
|
Fields[0] = ConstantStringClassRef;
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
// String pointer.
|
|
|
|
llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
|
|
|
|
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
|
|
|
bool isConstant;
|
2011-05-13 22:13:10 +04:00
|
|
|
Linkage = llvm::GlobalValue::PrivateLinkage;
|
|
|
|
isConstant = !Features.WritableStrings;
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
|
|
|
|
".str");
|
2011-01-18 01:11:21 +03:00
|
|
|
GV->setUnnamedAddr(true);
|
2011-05-13 22:13:10 +04:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2010-04-23 21:41:07 +04:00
|
|
|
Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
|
|
|
|
|
|
|
|
// String length.
|
|
|
|
const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
|
|
|
Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
|
|
|
|
|
|
|
|
// The struct.
|
|
|
|
C = llvm::ConstantStruct::get(STy, Fields);
|
|
|
|
GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage, C,
|
|
|
|
"_unnamed_nsstring_");
|
|
|
|
// FIXME. Fix section.
|
2010-04-24 02:33:39 +04:00
|
|
|
if (const char *Sect =
|
|
|
|
Features.ObjCNonFragileABI
|
|
|
|
? getContext().Target.getNSStringNonFragileABISection()
|
|
|
|
: getContext().Target.getNSStringSection())
|
2010-04-23 21:41:07 +04:00
|
|
|
GV->setSection(Sect);
|
|
|
|
Entry.setValue(GV);
|
|
|
|
|
|
|
|
return GV;
|
2010-04-23 00:26:39 +04:00
|
|
|
}
|
|
|
|
|
2008-08-14 03:20:05 +04:00
|
|
|
/// GetStringForStringLiteral - Return the appropriate bytes for a
|
2008-08-11 00:25:57 +04:00
|
|
|
/// string literal, properly padded to match the literal type.
|
2008-08-14 03:20:05 +04:00
|
|
|
std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
|
2011-01-29 20:53:12 +03:00
|
|
|
const ASTContext &Context = getContext();
|
2008-08-11 00:25:57 +04:00
|
|
|
const ConstantArrayType *CAT =
|
2011-01-29 20:53:12 +03:00
|
|
|
Context.getAsConstantArrayType(E->getType());
|
2008-08-11 00:25:57 +04:00
|
|
|
assert(CAT && "String isn't pointer or array!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-27 02:01:51 +03:00
|
|
|
// Resize the string to the right size.
|
2008-08-11 00:25:57 +04:00
|
|
|
uint64_t RealLen = CAT->getSize().getZExtValue();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-27 02:01:51 +03:00
|
|
|
if (E->isWide())
|
2011-01-29 20:53:12 +03:00
|
|
|
RealLen *= Context.Target.getWCharWidth() / Context.getCharWidth();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-17 16:54:38 +04:00
|
|
|
std::string Str = E->getString().str();
|
2008-08-11 00:25:57 +04:00
|
|
|
Str.resize(RealLen, '\0');
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-11 00:25:57 +04:00
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
2008-08-14 03:20:05 +04:00
|
|
|
/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
|
|
|
|
/// constant array for the given string literal.
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
|
|
|
|
// FIXME: This can be more efficient.
|
2009-11-16 08:55:46 +03:00
|
|
|
// FIXME: We shouldn't need to bitcast the constant in the wide string case.
|
|
|
|
llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S));
|
|
|
|
if (S->isWide()) {
|
|
|
|
llvm::Type *DestTy =
|
|
|
|
llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType()));
|
|
|
|
C = llvm::ConstantExpr::getBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
return C;
|
2008-08-14 03:20:05 +04:00
|
|
|
}
|
|
|
|
|
2009-02-25 01:18:39 +03:00
|
|
|
/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
|
|
|
|
/// array for the given ObjCEncodeExpr node.
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
|
|
|
|
std::string Str;
|
|
|
|
getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
2009-03-07 23:17:55 +03:00
|
|
|
|
|
|
|
return GetAddrOfConstantCString(Str);
|
2009-02-25 01:18:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-11 03:02:17 +03:00
|
|
|
/// GenerateWritableString -- Creates storage for a string literal.
|
2011-03-05 16:45:23 +03:00
|
|
|
static llvm::Constant *GenerateStringLiteral(llvm::StringRef str,
|
2007-11-28 08:34:05 +03:00
|
|
|
bool constant,
|
2008-10-18 01:56:50 +04:00
|
|
|
CodeGenModule &CGM,
|
|
|
|
const char *GlobalName) {
|
2008-08-14 03:20:05 +04:00
|
|
|
// Create Constant for this string literal. Don't add a '\0'.
|
2009-08-14 01:57:51 +04:00
|
|
|
llvm::Constant *C =
|
|
|
|
llvm::ConstantArray::get(CGM.getLLVMContext(), str, false);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-11-28 08:34:05 +03:00
|
|
|
// Create a global variable for this string
|
2011-01-11 01:34:03 +03:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
C, GlobalName);
|
2011-05-28 02:13:20 +04:00
|
|
|
GV->setAlignment(1);
|
2011-01-11 01:34:03 +03:00
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
return GV;
|
2007-11-28 08:34:05 +03:00
|
|
|
}
|
|
|
|
|
2008-08-14 03:20:05 +04:00
|
|
|
/// GetAddrOfConstantString - Returns a pointer to a character array
|
|
|
|
/// containing the literal. This contents are exactly that of the
|
|
|
|
/// given string, i.e. it will not be null terminated automatically;
|
|
|
|
/// see GetAddrOfConstantCString. Note that whether the result is
|
|
|
|
/// actually a pointer to an LLVM constant depends on
|
|
|
|
/// Feature.WriteableStrings.
|
|
|
|
///
|
|
|
|
/// The result has pointer to array type.
|
2011-03-05 16:45:23 +03:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantString(llvm::StringRef Str,
|
2008-10-18 01:56:50 +04:00
|
|
|
const char *GlobalName) {
|
2009-04-01 03:42:16 +04:00
|
|
|
bool IsConstant = !Features.WritableStrings;
|
|
|
|
|
|
|
|
// Get the default prefix if a name wasn't specified.
|
|
|
|
if (!GlobalName)
|
2009-07-16 09:03:48 +04:00
|
|
|
GlobalName = ".str";
|
2009-04-01 03:42:16 +04:00
|
|
|
|
|
|
|
// Don't share any string literals if strings aren't constant.
|
|
|
|
if (!IsConstant)
|
2011-03-05 16:45:23 +03:00
|
|
|
return GenerateStringLiteral(Str, false, *this, GlobalName);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
llvm::StringMapEntry<llvm::Constant *> &Entry =
|
2011-03-05 16:45:23 +03:00
|
|
|
ConstantStringMap.GetOrCreateValue(Str);
|
2007-11-28 08:34:05 +03:00
|
|
|
|
|
|
|
if (Entry.getValue())
|
2009-02-25 01:18:39 +03:00
|
|
|
return Entry.getValue();
|
2007-11-28 08:34:05 +03:00
|
|
|
|
|
|
|
// Create a global variable for this.
|
2011-03-05 16:45:23 +03:00
|
|
|
llvm::Constant *C = GenerateStringLiteral(Str, true, *this, GlobalName);
|
2007-11-28 08:34:05 +03:00
|
|
|
Entry.setValue(C);
|
|
|
|
return C;
|
|
|
|
}
|
2008-08-14 03:20:05 +04:00
|
|
|
|
|
|
|
/// GetAddrOfConstantCString - Returns a pointer to a character
|
2011-03-05 16:45:23 +03:00
|
|
|
/// array containing the literal and a terminating '\0'
|
2008-08-14 03:20:05 +04:00
|
|
|
/// character. The result has pointer to array type.
|
2011-03-05 16:45:23 +03:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &Str,
|
2008-10-18 01:56:50 +04:00
|
|
|
const char *GlobalName){
|
2011-03-05 16:45:23 +03:00
|
|
|
llvm::StringRef StrWithNull(Str.c_str(), Str.size() + 1);
|
|
|
|
return GetAddrOfConstantString(StrWithNull, GlobalName);
|
2008-08-14 03:20:05 +04:00
|
|
|
}
|
2008-08-16 03:26:23 +04:00
|
|
|
|
2008-08-26 12:29:31 +04:00
|
|
|
/// EmitObjCPropertyImplementations - Emit information for synthesized
|
|
|
|
/// properties for an implementation.
|
2009-09-09 19:08:12 +04:00
|
|
|
void CodeGenModule::EmitObjCPropertyImplementations(const
|
2008-08-26 12:29:31 +04:00
|
|
|
ObjCImplementationDecl *D) {
|
2009-09-09 19:08:12 +04:00
|
|
|
for (ObjCImplementationDecl::propimpl_iterator
|
2009-06-30 06:36:12 +04:00
|
|
|
i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
|
2008-08-26 12:29:31 +04:00
|
|
|
ObjCPropertyImplDecl *PID = *i;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-26 12:29:31 +04:00
|
|
|
// Dynamic is just for type-checking.
|
|
|
|
if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
|
|
|
|
ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
|
|
|
|
|
|
|
// Determine which methods need to be implemented, some may have
|
|
|
|
// been overridden. Note that ::isSynthesized is not the method
|
|
|
|
// we want, that just indicates if the decl came from a
|
|
|
|
// property. What we want to know is if the method is defined in
|
|
|
|
// this implementation.
|
2009-06-30 06:36:12 +04:00
|
|
|
if (!D->getInstanceMethod(PD->getGetterName()))
|
2008-12-09 23:23:04 +03:00
|
|
|
CodeGenFunction(*this).GenerateObjCGetter(
|
|
|
|
const_cast<ObjCImplementationDecl *>(D), PID);
|
2008-08-26 12:29:31 +04:00
|
|
|
if (!PD->isReadOnly() &&
|
2009-06-30 06:36:12 +04:00
|
|
|
!D->getInstanceMethod(PD->getSetterName()))
|
2008-12-09 23:23:04 +03:00
|
|
|
CodeGenFunction(*this).GenerateObjCSetter(
|
|
|
|
const_cast<ObjCImplementationDecl *>(D), PID);
|
2008-08-26 12:29:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 10:05:39 +03:00
|
|
|
static bool needsDestructMethod(ObjCImplementationDecl *impl) {
|
|
|
|
ObjCInterfaceDecl *iface
|
|
|
|
= const_cast<ObjCInterfaceDecl*>(impl->getClassInterface());
|
|
|
|
for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
|
|
|
|
ivar; ivar = ivar->getNextIvar())
|
|
|
|
if (ivar->getType().isDestructedType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-29 01:28:56 +04:00
|
|
|
/// EmitObjCIvarInitializations - Emit information for ivar initialization
|
|
|
|
/// for an implementation.
|
|
|
|
void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
|
2011-03-22 10:05:39 +03:00
|
|
|
// We might need a .cxx_destruct even if we don't have any ivar initializers.
|
|
|
|
if (needsDestructMethod(D)) {
|
|
|
|
IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
|
|
|
|
Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
|
|
|
|
ObjCMethodDecl *DTORMethod =
|
|
|
|
ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(),
|
|
|
|
cxxSelector, getContext().VoidTy, 0, D, true,
|
|
|
|
false, true, false, ObjCMethodDecl::Required);
|
|
|
|
D->addInstanceMethod(DTORMethod);
|
|
|
|
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
|
2011-06-16 03:02:42 +04:00
|
|
|
D->setHasCXXStructors(true);
|
2011-03-22 10:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the implementation doesn't have any ivar initializers, we don't need
|
|
|
|
// a .cxx_construct.
|
2011-03-17 17:19:08 +03:00
|
|
|
if (D->getNumIvarInitializers() == 0)
|
2010-04-29 01:28:56 +04:00
|
|
|
return;
|
|
|
|
|
2011-03-22 10:05:39 +03:00
|
|
|
IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
|
|
|
|
Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
|
2010-04-29 01:28:56 +04:00
|
|
|
// The constructor returns 'self'.
|
|
|
|
ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
|
|
|
|
D->getLocation(),
|
|
|
|
D->getLocation(), cxxSelector,
|
|
|
|
getContext().getObjCIdType(), 0,
|
2011-03-22 10:05:39 +03:00
|
|
|
D, true, false, true, false,
|
2010-04-29 01:28:56 +04:00
|
|
|
ObjCMethodDecl::Required);
|
|
|
|
D->addInstanceMethod(CTORMethod);
|
|
|
|
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
|
2011-06-16 03:02:42 +04:00
|
|
|
D->setHasCXXStructors(true);
|
2010-04-29 01:28:56 +04:00
|
|
|
}
|
|
|
|
|
2009-04-02 09:55:18 +04:00
|
|
|
/// EmitNamespace - Emit all declarations in a namespace.
|
2009-04-01 04:58:25 +04:00
|
|
|
void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
|
2009-06-30 06:36:12 +04:00
|
|
|
for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
|
2009-04-01 04:58:25 +04:00
|
|
|
I != E; ++I)
|
|
|
|
EmitTopLevelDecl(*I);
|
|
|
|
}
|
|
|
|
|
2009-04-02 09:55:18 +04:00
|
|
|
// EmitLinkageSpec - Emit all declarations in a linkage spec.
|
|
|
|
void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
|
2009-08-02 00:48:04 +04:00
|
|
|
if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
|
|
|
|
LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
|
2009-04-02 09:55:18 +04:00
|
|
|
ErrorUnsupported(LSD, "linkage spec");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-30 06:36:12 +04:00
|
|
|
for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end();
|
2009-04-02 09:55:18 +04:00
|
|
|
I != E; ++I)
|
|
|
|
EmitTopLevelDecl(*I);
|
|
|
|
}
|
|
|
|
|
2008-08-16 03:26:23 +04:00
|
|
|
/// EmitTopLevelDecl - Emit code for a single top level declaration.
|
|
|
|
void CodeGenModule::EmitTopLevelDecl(Decl *D) {
|
|
|
|
// If an error has occurred, stop code generation, but continue
|
|
|
|
// parsing and semantic analysis (to ensure all warnings and errors
|
|
|
|
// are emitted).
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
2009-06-29 21:30:29 +04:00
|
|
|
// Ignore dependent declarations.
|
|
|
|
if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-16 03:26:23 +04:00
|
|
|
switch (D->getKind()) {
|
2009-08-25 17:14:46 +04:00
|
|
|
case Decl::CXXConversion:
|
2009-04-05 00:47:02 +04:00
|
|
|
case Decl::CXXMethod:
|
2008-08-16 03:26:23 +04:00
|
|
|
case Decl::Function:
|
2009-06-29 21:30:29 +04:00
|
|
|
// Skip function templates
|
2011-04-23 02:18:13 +04:00
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
|
|
|
|
cast<FunctionDecl>(D)->isLateTemplateParsed())
|
2009-06-29 21:30:29 +04:00
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-11 03:43:36 +04:00
|
|
|
EmitGlobal(cast<FunctionDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2008-08-16 03:26:23 +04:00
|
|
|
case Decl::Var:
|
2009-09-11 03:43:36 +04:00
|
|
|
EmitGlobal(cast<VarDecl>(D));
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
|
|
|
|
2011-04-12 05:01:22 +04:00
|
|
|
// Indirect fields from global anonymous structs and unions can be
|
|
|
|
// ignored; only the actual variable requires IR gen support.
|
|
|
|
case Decl::IndirectField:
|
|
|
|
break;
|
|
|
|
|
2009-04-15 19:55:24 +04:00
|
|
|
// C++ Decls
|
2008-08-16 03:26:23 +04:00
|
|
|
case Decl::Namespace:
|
2009-04-01 04:58:25 +04:00
|
|
|
EmitNamespace(cast<NamespaceDecl>(D));
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
2009-06-20 04:51:54 +04:00
|
|
|
// No code generation needed.
|
2009-11-17 12:33:40 +03:00
|
|
|
case Decl::UsingShadow:
|
2009-06-20 04:51:54 +04:00
|
|
|
case Decl::Using:
|
2009-09-03 03:49:23 +04:00
|
|
|
case Decl::UsingDirective:
|
2009-06-30 00:59:39 +04:00
|
|
|
case Decl::ClassTemplate:
|
|
|
|
case Decl::FunctionTemplate:
|
2011-05-06 01:57:07 +04:00
|
|
|
case Decl::TypeAliasTemplate:
|
2009-09-23 23:19:16 +04:00
|
|
|
case Decl::NamespaceAlias:
|
2011-06-05 09:04:23 +04:00
|
|
|
case Decl::Block:
|
2009-06-20 04:51:54 +04:00
|
|
|
break;
|
2009-04-15 19:55:24 +04:00
|
|
|
case Decl::CXXConstructor:
|
2009-11-24 08:16:24 +03:00
|
|
|
// Skip function templates
|
2011-04-23 02:18:13 +04:00
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
|
|
|
|
cast<FunctionDecl>(D)->isLateTemplateParsed())
|
2009-11-24 08:16:24 +03:00
|
|
|
return;
|
|
|
|
|
2009-04-15 19:55:24 +04:00
|
|
|
EmitCXXConstructors(cast<CXXConstructorDecl>(D));
|
|
|
|
break;
|
2009-04-17 05:58:57 +04:00
|
|
|
case Decl::CXXDestructor:
|
2011-04-23 02:18:13 +04:00
|
|
|
if (cast<FunctionDecl>(D)->isLateTemplateParsed())
|
|
|
|
return;
|
2009-04-17 05:58:57 +04:00
|
|
|
EmitCXXDestructors(cast<CXXDestructorDecl>(D));
|
|
|
|
break;
|
2009-06-12 01:22:55 +04:00
|
|
|
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
2009-04-15 19:55:24 +04:00
|
|
|
// Objective-C Decls
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-19 01:33:24 +03:00
|
|
|
// Forward declarations, no (immediate) code generation.
|
2008-08-16 03:26:23 +04:00
|
|
|
case Decl::ObjCClass:
|
|
|
|
case Decl::ObjCForwardProtocol:
|
2009-03-21 21:06:45 +03:00
|
|
|
case Decl::ObjCInterface:
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
2010-08-23 22:51:39 +04:00
|
|
|
|
2011-04-09 11:11:53 +04:00
|
|
|
case Decl::ObjCCategory: {
|
|
|
|
ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
|
|
|
|
if (CD->IsClassExtension() && CD->hasSynthBitfield())
|
|
|
|
Context.ResetObjCLayout(CD->getClassInterface());
|
|
|
|
break;
|
|
|
|
}
|
2009-04-01 06:36:43 +04:00
|
|
|
|
2008-08-16 03:26:23 +04:00
|
|
|
case Decl::ObjCProtocol:
|
2009-03-21 21:06:45 +03:00
|
|
|
Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::ObjCCategoryImpl:
|
2008-08-26 12:29:31 +04:00
|
|
|
// Categories have properties but don't support synthesize so we
|
|
|
|
// can ignore them here.
|
2008-08-16 03:26:23 +04:00
|
|
|
Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2008-08-26 12:29:31 +04:00
|
|
|
case Decl::ObjCImplementation: {
|
|
|
|
ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
|
2010-08-23 22:51:39 +04:00
|
|
|
if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield())
|
|
|
|
Context.ResetObjCLayout(OMD->getClassInterface());
|
2008-08-26 12:29:31 +04:00
|
|
|
EmitObjCPropertyImplementations(OMD);
|
2010-04-29 01:28:56 +04:00
|
|
|
EmitObjCIvarInitializations(OMD);
|
2008-08-26 12:29:31 +04:00
|
|
|
Runtime->GenerateClass(OMD);
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2008-08-16 03:26:23 +04:00
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
|
|
|
|
// If this is not a prototype, emit the body.
|
2009-06-30 06:35:26 +04:00
|
|
|
if (OMD->getBody())
|
2008-08-16 03:26:23 +04:00
|
|
|
CodeGenFunction(*this).GenerateObjCMethod(OMD);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
case Decl::ObjCCompatibleAlias:
|
2009-01-08 04:10:55 +03:00
|
|
|
// compatibility-alias is a directive and has no code gen.
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
|
|
|
|
2009-04-02 09:55:18 +04:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
EmitLinkageSpec(cast<LinkageSpecDecl>(D));
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::FileScopeAsm: {
|
|
|
|
FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
|
2009-12-11 16:33:18 +03:00
|
|
|
llvm::StringRef AsmString = AD->getAsmString()->getString();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-16 03:26:23 +04:00
|
|
|
const std::string &S = getModule().getModuleInlineAsm();
|
|
|
|
if (S.empty())
|
|
|
|
getModule().setModuleInlineAsm(AsmString);
|
|
|
|
else
|
2009-12-11 16:33:18 +03:00
|
|
|
getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
default:
|
2009-05-16 11:57:57 +04:00
|
|
|
// Make sure we handled everything we should, every other kind is a
|
|
|
|
// non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
|
|
|
|
// function. Need to recode Decl::Kind to do that easily.
|
2008-08-16 03:26:23 +04:00
|
|
|
assert(isa<TypeDecl>(D) && "Unsupported decl kind");
|
|
|
|
}
|
|
|
|
}
|
2010-07-07 03:57:41 +04:00
|
|
|
|
|
|
|
/// Turns the given pointer into a constant.
|
|
|
|
static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
|
|
|
|
const void *Ptr) {
|
|
|
|
uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
|
|
|
|
const llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
|
|
|
|
return llvm::ConstantInt::get(i64, PtrInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
|
|
|
|
llvm::NamedMDNode *&GlobalMetadata,
|
|
|
|
GlobalDecl D,
|
|
|
|
llvm::GlobalValue *Addr) {
|
|
|
|
if (!GlobalMetadata)
|
|
|
|
GlobalMetadata =
|
|
|
|
CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
|
|
|
|
|
|
|
|
// TODO: should we report variant information for ctors/dtors?
|
|
|
|
llvm::Value *Ops[] = {
|
|
|
|
Addr,
|
|
|
|
GetPointerConstant(CGM.getLLVMContext(), D.getDecl())
|
|
|
|
};
|
2011-04-21 23:59:12 +04:00
|
|
|
GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
|
2010-07-07 03:57:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits metadata nodes associating all the global values in the
|
|
|
|
/// current module with the Decls they came from. This is useful for
|
|
|
|
/// projects using IR gen as a subroutine.
|
|
|
|
///
|
|
|
|
/// Since there's currently no way to associate an MDNode directly
|
|
|
|
/// with an llvm::GlobalValue, we create a global named metadata
|
|
|
|
/// with the name 'clang.global.decl.ptrs'.
|
|
|
|
void CodeGenModule::EmitDeclMetadata() {
|
|
|
|
llvm::NamedMDNode *GlobalMetadata = 0;
|
|
|
|
|
|
|
|
// StaticLocalDeclMap
|
|
|
|
for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator
|
|
|
|
I = MangledDeclNames.begin(), E = MangledDeclNames.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
llvm::GlobalValue *Addr = getModule().getNamedValue(I->second);
|
|
|
|
EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits metadata nodes for all the local variables in the current
|
|
|
|
/// function.
|
|
|
|
void CodeGenFunction::EmitDeclMetadata() {
|
|
|
|
if (LocalDeclMap.empty()) return;
|
|
|
|
|
|
|
|
llvm::LLVMContext &Context = getLLVMContext();
|
|
|
|
|
|
|
|
// Find the unique metadata ID for this name.
|
|
|
|
unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
|
|
|
|
|
|
|
|
llvm::NamedMDNode *GlobalMetadata = 0;
|
|
|
|
|
|
|
|
for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator
|
|
|
|
I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) {
|
|
|
|
const Decl *D = I->first;
|
|
|
|
llvm::Value *Addr = I->second;
|
|
|
|
|
|
|
|
if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
|
|
|
|
llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
|
2011-04-21 23:59:12 +04:00
|
|
|
Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr));
|
2010-07-07 03:57:41 +04:00
|
|
|
} else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
|
|
|
|
GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
|
|
|
|
EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-16 04:00:15 +04:00
|
|
|
|
2011-05-05 04:08:20 +04:00
|
|
|
void CodeGenModule::EmitCoverageFile() {
|
|
|
|
if (!getCodeGenOpts().CoverageFile.empty()) {
|
2011-05-05 00:46:58 +04:00
|
|
|
if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) {
|
|
|
|
llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
|
|
|
|
llvm::LLVMContext &Ctx = TheModule.getContext();
|
2011-05-05 04:08:20 +04:00
|
|
|
llvm::MDString *CoverageFile =
|
|
|
|
llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile);
|
2011-05-05 00:46:58 +04:00
|
|
|
for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
|
|
|
|
llvm::MDNode *CU = CUNode->getOperand(i);
|
2011-05-05 04:08:20 +04:00
|
|
|
llvm::Value *node[] = { CoverageFile, CU };
|
2011-05-05 00:46:58 +04:00
|
|
|
llvm::MDNode *N = llvm::MDNode::get(Ctx, node);
|
|
|
|
GCov->addOperand(N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-16 04:00:15 +04:00
|
|
|
///@name Custom Runtime Function Interfaces
|
|
|
|
///@{
|
|
|
|
//
|
|
|
|
// FIXME: These can be eliminated once we can have clients just get the required
|
|
|
|
// AST nodes from the builtin tables.
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getBlockObjectDispose() {
|
|
|
|
if (BlockObjectDispose)
|
|
|
|
return BlockObjectDispose;
|
|
|
|
|
2010-07-16 04:00:19 +04:00
|
|
|
// If we saw an explicit decl, use that.
|
|
|
|
if (BlockObjectDisposeDecl) {
|
|
|
|
return BlockObjectDispose = GetAddrOfFunction(
|
|
|
|
BlockObjectDisposeDecl,
|
|
|
|
getTypes().GetFunctionType(BlockObjectDisposeDecl));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise construct the function by hand.
|
2011-05-15 05:53:33 +04:00
|
|
|
const llvm::Type *args[] = { Int8PtrTy, Int32Ty };
|
|
|
|
const llvm::FunctionType *fty
|
|
|
|
= llvm::FunctionType::get(VoidTy, args, false);
|
2010-07-16 04:00:15 +04:00
|
|
|
return BlockObjectDispose =
|
2011-05-15 05:53:33 +04:00
|
|
|
CreateRuntimeFunction(fty, "_Block_object_dispose");
|
2010-07-16 04:00:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getBlockObjectAssign() {
|
|
|
|
if (BlockObjectAssign)
|
|
|
|
return BlockObjectAssign;
|
|
|
|
|
2010-07-16 04:00:19 +04:00
|
|
|
// If we saw an explicit decl, use that.
|
|
|
|
if (BlockObjectAssignDecl) {
|
|
|
|
return BlockObjectAssign = GetAddrOfFunction(
|
|
|
|
BlockObjectAssignDecl,
|
|
|
|
getTypes().GetFunctionType(BlockObjectAssignDecl));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise construct the function by hand.
|
2011-05-15 05:53:33 +04:00
|
|
|
const llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
|
|
|
|
const llvm::FunctionType *fty
|
|
|
|
= llvm::FunctionType::get(VoidTy, args, false);
|
2010-07-16 04:00:15 +04:00
|
|
|
return BlockObjectAssign =
|
2011-05-15 05:53:33 +04:00
|
|
|
CreateRuntimeFunction(fty, "_Block_object_assign");
|
2010-07-16 04:00:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
|
|
|
|
if (NSConcreteGlobalBlock)
|
|
|
|
return NSConcreteGlobalBlock;
|
2010-07-16 04:00:19 +04:00
|
|
|
|
|
|
|
// If we saw an explicit decl, use that.
|
|
|
|
if (NSConcreteGlobalBlockDecl) {
|
|
|
|
return NSConcreteGlobalBlock = GetAddrOfGlobalVar(
|
|
|
|
NSConcreteGlobalBlockDecl,
|
|
|
|
getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise construct the variable by hand.
|
2011-02-08 11:22:06 +03:00
|
|
|
return NSConcreteGlobalBlock =
|
|
|
|
CreateRuntimeVariable(Int8PtrTy, "_NSConcreteGlobalBlock");
|
2010-07-16 04:00:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
|
|
|
|
if (NSConcreteStackBlock)
|
|
|
|
return NSConcreteStackBlock;
|
2010-07-16 04:00:19 +04:00
|
|
|
|
|
|
|
// If we saw an explicit decl, use that.
|
|
|
|
if (NSConcreteStackBlockDecl) {
|
|
|
|
return NSConcreteStackBlock = GetAddrOfGlobalVar(
|
|
|
|
NSConcreteStackBlockDecl,
|
|
|
|
getTypes().ConvertType(NSConcreteStackBlockDecl->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise construct the variable by hand.
|
2011-02-08 11:22:06 +03:00
|
|
|
return NSConcreteStackBlock =
|
|
|
|
CreateRuntimeVariable(Int8PtrTy, "_NSConcreteStackBlock");
|
2010-07-16 04:00:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
///@}
|