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"
|
2011-10-06 22:29:37 +04:00
|
|
|
#include "CGCUDARuntime.h"
|
2010-08-31 11:33:07 +04:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-13 04:59:25 +04:00
|
|
|
#include "CGObjCRuntime.h"
|
2011-09-20 01:14:35 +04:00
|
|
|
#include "CGOpenCLRuntime.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"
|
2011-10-27 00:41:06 +04:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2011-12-19 18:41:01 +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;
|
|
|
|
|
2011-09-10 02:41:49 +04:00
|
|
|
static const char AnnotationSection[] = "llvm.metadata";
|
|
|
|
|
2010-08-23 01:01:12 +04:00
|
|
|
static CGCXXABI &createCXXABI(CodeGenModule &CGM) {
|
2011-09-02 04:18:52 +04:00
|
|
|
switch (CGM.getContext().getTargetInfo().getCXXABI()) {
|
2010-08-23 01:01:12 +04:00
|
|
|
case CXXABI_ARM: return *CreateARMCXXABI(CGM);
|
|
|
|
case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM);
|
|
|
|
case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("invalid C++ ABI kind");
|
|
|
|
}
|
|
|
|
|
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,
|
2011-09-26 03:23:43 +04:00
|
|
|
DiagnosticsEngine &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)),
|
2011-06-21 22:54:39 +04:00
|
|
|
Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI, CGO),
|
2010-10-15 03:06:10 +04:00
|
|
|
TBAA(0),
|
2011-10-06 22:29:37 +04:00
|
|
|
VTables(*this), ObjCRuntime(0), OpenCLRuntime(0), CUDARuntime(0),
|
|
|
|
DebugInfo(0), ARCData(0), RRData(0), CFConstantStringClassRef(0),
|
|
|
|
ConstantStringClassRef(0), NSConstantStringType(0),
|
2010-07-16 04:00:15 +04:00
|
|
|
VMContext(M.getContext()),
|
|
|
|
NSConcreteGlobalBlock(0), NSConcreteStackBlock(0),
|
2011-02-08 11:22:06 +03:00
|
|
|
BlockObjectAssign(0), BlockObjectDispose(0),
|
|
|
|
BlockDescriptorType(0), GenericBlockLiteralType(0) {
|
2012-02-07 04:39:47 +04:00
|
|
|
|
|
|
|
// Initialize the type cache.
|
|
|
|
llvm::LLVMContext &LLVMContext = M.getContext();
|
|
|
|
VoidTy = llvm::Type::getVoidTy(LLVMContext);
|
|
|
|
Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
|
|
|
|
Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
|
|
|
|
Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
|
|
|
|
Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
|
|
|
|
FloatTy = llvm::Type::getFloatTy(LLVMContext);
|
|
|
|
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
|
|
|
|
PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
|
|
|
|
PointerAlignInBytes =
|
|
|
|
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
|
|
|
|
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
|
|
|
|
IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
|
|
|
|
Int8PtrTy = Int8Ty->getPointerTo(0);
|
|
|
|
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
|
|
|
|
|
2011-03-23 00:21:24 +03:00
|
|
|
if (Features.ObjC1)
|
2011-09-20 01:14:35 +04:00
|
|
|
createObjCRuntime();
|
|
|
|
if (Features.OpenCL)
|
|
|
|
createOpenCLRuntime();
|
2011-10-06 22:29:37 +04:00
|
|
|
if (Features.CUDA)
|
|
|
|
createCUDARuntime();
|
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();
|
2008-03-01 11:45:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenModule::~CodeGenModule() {
|
2011-07-28 00:29:46 +04:00
|
|
|
delete ObjCRuntime;
|
2011-09-20 01:14:35 +04:00
|
|
|
delete OpenCLRuntime;
|
2011-10-06 22:29:37 +04:00
|
|
|
delete CUDARuntime;
|
2011-10-08 09:28:26 +04:00
|
|
|
delete TheTargetCodeGenInfo;
|
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)
|
2011-07-28 00:29:46 +04:00
|
|
|
ObjCRuntime = CreateGNUObjCRuntime(*this);
|
2010-01-23 05:40:42 +03:00
|
|
|
else
|
2011-07-28 00:29:46 +04:00
|
|
|
ObjCRuntime = CreateMacObjCRuntime(*this);
|
2010-01-23 05:40:42 +03:00
|
|
|
}
|
|
|
|
|
2011-09-20 01:14:35 +04:00
|
|
|
void CodeGenModule::createOpenCLRuntime() {
|
|
|
|
OpenCLRuntime = new CGOpenCLRuntime(*this);
|
|
|
|
}
|
|
|
|
|
2011-10-06 22:29:37 +04:00
|
|
|
void CodeGenModule::createCUDARuntime() {
|
|
|
|
CUDARuntime = CreateNVCUDARuntime(*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();
|
2011-07-28 00:29:46 +04:00
|
|
|
if (ObjCRuntime)
|
|
|
|
if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
|
2008-08-11 22:12:00 +04:00
|
|
|
AddGlobalCtor(ObjCInitFunction);
|
2008-08-01 04:01:51 +04:00
|
|
|
EmitCtorList(GlobalCtors, "llvm.global_ctors");
|
|
|
|
EmitCtorList(GlobalDtors, "llvm.global_dtors");
|
2011-09-10 02:41:49 +04:00
|
|
|
EmitGlobalAnnotations();
|
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();
|
2011-08-17 00:58:22 +04:00
|
|
|
|
|
|
|
if (DebugInfo)
|
|
|
|
DebugInfo->finalize();
|
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-09-02 04:18:52 +04:00
|
|
|
return getContext().getTargetInfo().getTriple().isOSDarwin();
|
2010-03-06 03:35:14 +03:00
|
|
|
}
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
void CodeGenModule::Error(SourceLocation loc, StringRef error) {
|
2011-09-26 03:23:43 +04:00
|
|
|
unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, error);
|
2011-03-18 05:56:14 +03:00
|
|
|
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;
|
2011-09-26 03:23:43 +04:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::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;
|
2011-09-26 03:23:43 +04:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::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
|
|
|
}
|
|
|
|
|
2011-06-25 01:55:10 +04:00
|
|
|
llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
|
|
|
|
return llvm::ConstantInt::get(SizeTy, size.getQuantity());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
|
2010-06-22 20:05:32 +04:00
|
|
|
const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()];
|
2010-06-22 20:05:32 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-05 06:13:05 +04:00
|
|
|
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);
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
Str = StringRef(Name, Length);
|
2010-06-22 20:05:32 +04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
|
2010-03-20 02:29:14 +03:00
|
|
|
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 =
|
2012-02-07 04:39:47 +04:00
|
|
|
llvm::StructType::get(Int32Ty, llvm::PointerType::getUnqual(CtorFTy), NULL);
|
2008-08-01 04:01:51 +04:00
|
|
|
|
|
|
|
// Construct the constructor and destructor arrays.
|
2012-02-07 02:16:34 +04:00
|
|
|
SmallVector<llvm::Constant*, 8> Ctors;
|
2008-08-01 04:01:51 +04:00
|
|
|
for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
|
2012-02-07 02:16:34 +04:00
|
|
|
llvm::Constant *S[] = {
|
|
|
|
llvm::ConstantInt::get(Int32Ty, I->second, false),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2011-09-19 22:05:26 +04:00
|
|
|
|
|
|
|
// Note that Apple's kernel linker doesn't support symbol
|
|
|
|
// coalescing, so we need to avoid linkonce and weak linkages there.
|
|
|
|
// Normally, this means we just map to internal, but for explicit
|
|
|
|
// instantiations we'll map to external.
|
|
|
|
|
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
|
2011-09-19 22:05:26 +04:00
|
|
|
: llvm::Function::ExternalLinkage;
|
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
|
|
|
}
|
|
|
|
|
2011-10-02 05:16:38 +04:00
|
|
|
/// Determines whether the language options require us to model
|
|
|
|
/// unwind exceptions. We treat -fexceptions as mandating this
|
|
|
|
/// except under the fragile ObjC ABI with only ObjC exceptions
|
|
|
|
/// enabled. This means, for example, that C with -fexceptions
|
|
|
|
/// enables this.
|
|
|
|
static bool hasUnwindExceptions(const LangOptions &Features) {
|
|
|
|
// If exceptions are completely disabled, obviously this is false.
|
|
|
|
if (!Features.Exceptions) return false;
|
|
|
|
|
|
|
|
// If C++ exceptions are enabled, this is true.
|
|
|
|
if (Features.CXXExceptions) return true;
|
|
|
|
|
|
|
|
// If ObjC exceptions are enabled, this depends on the ABI.
|
|
|
|
if (Features.ObjCExceptions) {
|
|
|
|
if (!Features.ObjCNonFragileABI) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2011-10-02 05:16:38 +04:00
|
|
|
if (!hasUnwindExceptions(Features))
|
2009-09-09 19:08:12 +04:00
|
|
|
F->addFnAttr(llvm::Attribute::NoUnwind);
|
2008-10-28 03:17:57 +03:00
|
|
|
|
2011-08-23 03:55:33 +04:00
|
|
|
if (D->hasAttr<NakedAttr>()) {
|
|
|
|
// Naked implies noinline: we should not be inlining such functions.
|
2010-09-29 22:20:25 +04:00
|
|
|
F->addFnAttr(llvm::Attribute::Naked);
|
2011-08-23 03:55:33 +04:00
|
|
|
F->addFnAttr(llvm::Attribute::NoInline);
|
|
|
|
}
|
2010-09-29 22:20:25 +04:00
|
|
|
|
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-08-23 03:55:33 +04:00
|
|
|
// (noinline wins over always_inline, and we can't specify both in IR)
|
|
|
|
if (D->hasAttr<AlwaysInlineAttr>() &&
|
|
|
|
!F->hasFnAttr(llvm::Attribute::NoInline))
|
|
|
|
F->addFnAttr(llvm::Attribute::AlwaysInline);
|
|
|
|
|
2011-01-11 03:26:26 +03:00
|
|
|
if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
|
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
|
2011-09-13 21:21:33 +04:00
|
|
|
if (Features.getStackProtector() == LangOptions::SSPOn)
|
2009-11-16 19:56:03 +03:00
|
|
|
F->addFnAttr(llvm::Attribute::StackProtect);
|
2011-09-13 21:21:33 +04:00
|
|
|
else if (Features.getStackProtector() == LangOptions::SSPReq)
|
2009-11-16 19:56:03 +03:00
|
|
|
F->addFnAttr(llvm::Attribute::StackProtectReq);
|
|
|
|
|
2012-02-02 15:49:28 +04:00
|
|
|
if (Features.AddressSanitizer) {
|
|
|
|
// When AddressSanitizer is enabled, set AddressSafety attribute
|
|
|
|
// unless __attribute__((no_address_safety_analysis)) is used.
|
|
|
|
if (!D->hasAttr<NoAddressSafetyAnalysisAttr>())
|
|
|
|
F->addFnAttr(llvm::Attribute::AddressSafety);
|
|
|
|
}
|
|
|
|
|
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-04-01 02:37:52 +04:00
|
|
|
// Convert LLVMUsed to what ConstantArray needs.
|
2012-02-07 02:16:34 +04:00
|
|
|
SmallVector<llvm::Constant*, 8> UsedArray;
|
2009-04-01 02:37:52 +04:00
|
|
|
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]),
|
2012-02-07 02:16:34 +04:00
|
|
|
Int8PtrTy);
|
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;
|
2012-02-07 02:16:34 +04:00
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, 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
|
2011-07-18 09:26:13 +04:00
|
|
|
// 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.
|
2011-07-23 14:55:15 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-09-10 02:41:49 +04:00
|
|
|
void CodeGenModule::EmitGlobalAnnotations() {
|
|
|
|
if (Annotations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a new global variable for the ConstantStruct in the Module.
|
|
|
|
llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
|
|
|
|
Annotations[0]->getType(), Annotations.size()), Annotations);
|
|
|
|
llvm::GlobalValue *gv = new llvm::GlobalVariable(getModule(),
|
|
|
|
Array->getType(), false, llvm::GlobalValue::AppendingLinkage, Array,
|
|
|
|
"llvm.global.annotations");
|
|
|
|
gv->setSection(AnnotationSection);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationString(llvm::StringRef Str) {
|
|
|
|
llvm::StringMap<llvm::Constant*>::iterator i = AnnotationStrings.find(Str);
|
|
|
|
if (i != AnnotationStrings.end())
|
|
|
|
return i->second;
|
|
|
|
|
|
|
|
// Not found yet, create a new global.
|
2012-02-05 06:30:40 +04:00
|
|
|
llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
|
2011-09-10 02:41:49 +04:00
|
|
|
llvm::GlobalValue *gv = new llvm::GlobalVariable(getModule(), s->getType(),
|
|
|
|
true, llvm::GlobalValue::PrivateLinkage, s, ".str");
|
|
|
|
gv->setSection(AnnotationSection);
|
|
|
|
gv->setUnnamedAddr(true);
|
|
|
|
AnnotationStrings[Str] = gv;
|
|
|
|
return gv;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
|
|
|
|
SourceManager &SM = getContext().getSourceManager();
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
|
|
if (PLoc.isValid())
|
|
|
|
return EmitAnnotationString(PLoc.getFilename());
|
|
|
|
return EmitAnnotationString(SM.getBufferName(Loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
|
|
|
|
SourceManager &SM = getContext().getSourceManager();
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(L);
|
|
|
|
unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
|
|
|
|
SM.getExpansionLineNumber(L);
|
|
|
|
return llvm::ConstantInt::get(Int32Ty, LineNo);
|
|
|
|
}
|
|
|
|
|
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,
|
2011-09-10 02:41:49 +04:00
|
|
|
SourceLocation L) {
|
|
|
|
// Get the globals for file name, annotation, and the line number.
|
|
|
|
llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
|
|
|
|
*UnitGV = EmitAnnotationUnit(L),
|
|
|
|
*LineNoCst = EmitAnnotationLineNo(L);
|
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] = {
|
2011-09-10 02:41:49 +04:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
|
|
|
|
LineNoCst
|
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
|
|
|
}
|
|
|
|
|
2011-09-10 02:41:49 +04:00
|
|
|
void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
|
|
|
|
llvm::GlobalValue *GV) {
|
|
|
|
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
|
|
|
|
// Get the struct elements for these annotations.
|
|
|
|
for (specific_attr_iterator<AnnotateAttr>
|
|
|
|
ai = D->specific_attr_begin<AnnotateAttr>(),
|
|
|
|
ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai)
|
|
|
|
Annotations.push_back(EmitAnnotateAttr(GV, *ai, D->getLocation()));
|
|
|
|
}
|
|
|
|
|
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?");
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
|
2010-03-04 21:17:24 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2011-10-06 22:29:46 +04:00
|
|
|
// If this is CUDA, be selective about which declarations we emit.
|
|
|
|
if (Features.CUDA) {
|
|
|
|
if (CodeGenOpts.CUDAIsDevice) {
|
|
|
|
if (!Global->hasAttr<CUDADeviceAttr>() &&
|
|
|
|
!Global->hasAttr<CUDAGlobalAttr>() &&
|
|
|
|
!Global->hasAttr<CUDAConstantAttr>() &&
|
|
|
|
!Global->hasAttr<CUDASharedAttr>())
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!Global->hasAttr<CUDAHostAttr>() && (
|
|
|
|
Global->hasAttr<CUDADeviceAttr>() ||
|
|
|
|
Global->hasAttr<CUDAConstantAttr>() ||
|
|
|
|
Global->hasAttr<CUDASharedAttr>()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2009-02-14 00:18:01 +03:00
|
|
|
// Forward declarations are emitted lazily on first use.
|
2011-07-18 09:26:13 +04:00
|
|
|
if (!FD->doesThisDeclarationHaveABody()) {
|
|
|
|
if (!FD->doesDeclarationForceExternallyVisibleDefinition())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const FunctionDecl *InlineDefinition = 0;
|
|
|
|
FD->getBody(InlineDefinition);
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2011-07-18 09:26:13 +04:00
|
|
|
llvm::StringMap<GlobalDecl>::iterator DDI =
|
|
|
|
DeferredDecls.find(MangledName);
|
|
|
|
if (DDI != DeferredDecls.end())
|
|
|
|
DeferredDecls.erase(DDI);
|
|
|
|
EmitGlobalDefinition(InlineDefinition);
|
2009-02-14 00:18:01 +03:00
|
|
|
return;
|
2011-07-18 09:26:13 +04:00
|
|
|
}
|
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-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.
|
2011-07-23 14:55:15 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-27 00:41:06 +04:00
|
|
|
namespace {
|
|
|
|
struct FunctionIsDirectlyRecursive :
|
|
|
|
public RecursiveASTVisitor<FunctionIsDirectlyRecursive> {
|
|
|
|
const StringRef Name;
|
2011-12-19 18:41:01 +04:00
|
|
|
const Builtin::Context &BI;
|
2011-10-27 00:41:06 +04:00
|
|
|
bool Result;
|
2011-12-19 18:41:01 +04:00
|
|
|
FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) :
|
|
|
|
Name(N), BI(C), Result(false) {
|
2011-10-27 00:41:06 +04:00
|
|
|
}
|
|
|
|
typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base;
|
|
|
|
|
|
|
|
bool TraverseCallExpr(CallExpr *E) {
|
2011-12-19 18:41:01 +04:00
|
|
|
const FunctionDecl *FD = E->getDirectCallee();
|
|
|
|
if (!FD)
|
2011-10-27 00:41:06 +04:00
|
|
|
return true;
|
2011-12-19 18:41:01 +04:00
|
|
|
AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
|
|
|
|
if (Attr && Name == Attr->getLabel()) {
|
|
|
|
Result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned BuiltinID = FD->getBuiltinID();
|
|
|
|
if (!BuiltinID)
|
2011-10-27 00:41:06 +04:00
|
|
|
return true;
|
2012-01-18 07:41:19 +04:00
|
|
|
StringRef BuiltinName = BI.GetName(BuiltinID);
|
|
|
|
if (BuiltinName.startswith("__builtin_") &&
|
|
|
|
Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
|
2011-10-27 00:41:06 +04:00
|
|
|
Result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-19 18:41:01 +04:00
|
|
|
// isTriviallyRecursive - Check if this function calls another
|
|
|
|
// decl that, because of the asm attribute or the other decl being a builtin,
|
|
|
|
// ends up pointing to itself.
|
2011-10-27 00:41:06 +04:00
|
|
|
bool
|
2011-12-19 18:41:01 +04:00
|
|
|
CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
|
|
|
|
StringRef Name;
|
|
|
|
if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
|
2012-01-18 05:50:13 +04:00
|
|
|
// asm labels are a special kind of mangling we have to support.
|
2011-12-19 18:41:01 +04:00
|
|
|
AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
|
|
|
|
if (!Attr)
|
|
|
|
return false;
|
|
|
|
Name = Attr->getLabel();
|
|
|
|
} else {
|
|
|
|
Name = FD->getName();
|
|
|
|
}
|
2011-10-27 00:41:06 +04:00
|
|
|
|
2011-12-19 18:41:01 +04:00
|
|
|
FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
|
|
|
|
Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD));
|
2011-10-27 00:41:06 +04:00
|
|
|
return Walker.Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CodeGenModule::shouldEmitFunction(const FunctionDecl *F) {
|
|
|
|
if (getFunctionLinkage(F) != llvm::Function::AvailableExternallyLinkage)
|
|
|
|
return true;
|
2011-10-29 00:43:56 +04:00
|
|
|
if (CodeGenOpts.OptimizationLevel == 0 &&
|
|
|
|
!F->hasAttr<AlwaysInlineAttr>())
|
2011-10-27 00:41:06 +04:00
|
|
|
return false;
|
|
|
|
// PR9614. Avoid cases where the source code is lying to us. An available
|
|
|
|
// externally function should have an equivalent function somewhere else,
|
|
|
|
// but a function that calls itself is clearly not equivalent to the real
|
|
|
|
// implementation.
|
|
|
|
// This happens in glibc's btowc and in some configure checks.
|
2011-12-19 18:41:01 +04:00
|
|
|
return !isTriviallyRecursive(F);
|
2011-10-27 00:41:06 +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.
|
2011-10-27 00:41:06 +04:00
|
|
|
if (!shouldEmitFunction(Function))
|
2010-07-13 10:02:28 +04:00
|
|
|
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
|
|
|
|
2011-09-23 09:06:16 +04:00
|
|
|
llvm_unreachable("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 *
|
2011-07-23 14:55:15 +04:00
|
|
|
CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName,
|
2011-07-18 08:24:23 +04:00
|
|
|
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.
|
2011-07-09 21:41:47 +04:00
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
|
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
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *FTy;
|
2010-04-28 04:00:30 +04:00
|
|
|
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
|
|
|
}
|
2012-01-14 20:38:05 +04:00
|
|
|
FD = FD->getPreviousDecl();
|
2010-12-15 07:00:32 +03:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2010-04-28 04:00:30 +04:00
|
|
|
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-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty,
|
2011-02-05 07:35:53 +03:00
|
|
|
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
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
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 *
|
2011-07-18 08:24:23 +04:00
|
|
|
CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef Name,
|
2011-06-16 03:02:42 +04:00
|
|
|
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 *
|
2011-07-23 14:55:15 +04:00
|
|
|
CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
|
2011-07-18 08:24:23 +04:00
|
|
|
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 *
|
2011-07-23 14:55:15 +04:00
|
|
|
CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name,
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty,
|
2011-01-29 21:20:20 +03:00
|
|
|
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,
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty) {
|
2009-03-23 00:03:39 +03:00
|
|
|
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
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
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
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
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 *
|
2011-07-18 08:24:23 +04:00
|
|
|
CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
|
2011-07-23 14:55:15 +04:00
|
|
|
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.
|
2011-07-23 14:55:15 +04:00
|
|
|
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
|
|
|
}
|
2012-01-21 01:50:17 +04:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateSpecializationKind!");
|
2010-01-06 23:27:16 +03:00
|
|
|
}
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
CharUnits CodeGenModule::GetTargetTypeStoreSize(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();
|
2012-02-14 02:16:19 +04:00
|
|
|
CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
|
|
|
|
bool NeedsGlobalCtor = false;
|
|
|
|
bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2012-01-14 08:30:29 +04:00
|
|
|
const VarDecl *InitDecl;
|
|
|
|
const Expr *InitExpr = D->getAnyInitializer(InitDecl);
|
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 {
|
2012-01-14 08:30:29 +04:00
|
|
|
Init = EmitConstantInit(*InitDecl);
|
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();
|
2012-01-14 08:30:29 +04:00
|
|
|
|
2009-08-09 03:24:23 +04:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
Init = EmitNullConstant(T);
|
2012-02-14 02:16:19 +04:00
|
|
|
NeedsGlobalCtor = 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
|
2012-02-14 02:16:19 +04:00
|
|
|
// initializer position (just in case this entry was delayed) if we
|
|
|
|
// also don't need to register a destructor.
|
|
|
|
if (getLangOptions().CPlusPlus && !NeedsGlobalDtor)
|
2010-07-16 03:40:35 +04:00
|
|
|
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
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
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.
|
2011-07-23 14:55:15 +04:00
|
|
|
Entry->setName(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
|
|
|
}
|
|
|
|
|
2011-09-10 02:41:49 +04:00
|
|
|
if (D->hasAttr<AnnotateAttr>())
|
|
|
|
AddGlobalAnnotations(D, GV);
|
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);
|
2012-02-14 02:16:19 +04:00
|
|
|
if (!NeedsGlobalCtor && !NeedsGlobalDtor &&
|
|
|
|
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.
|
2012-02-14 02:16:19 +04:00
|
|
|
if (NeedsGlobalCtor || NeedsGlobalDtor)
|
|
|
|
EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
|
2010-11-06 12:44:32 +03:00
|
|
|
|
2010-10-27 20:21:54 +04:00
|
|
|
// Emit global variable debug information.
|
2011-10-14 01:45:18 +04:00
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
2010-10-27 20:21:54 +04:00
|
|
|
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
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *NewRetTy = NewFn->getReturnType();
|
2011-07-23 14:55:15 +04:00
|
|
|
SmallVector<llvm::Value*, 4> ArgList;
|
2009-05-05 10:16:31 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-08-03 04:43:55 +04:00
|
|
|
// Get the attribute list.
|
|
|
|
llvm::SmallVector<llvm::AttributeWithIndex, 8> AttrVec;
|
|
|
|
llvm::AttrListPtr AttrList = CI->getAttributes();
|
|
|
|
|
|
|
|
// Get any return attributes.
|
|
|
|
llvm::Attributes RAttrs = AttrList.getRetAttributes();
|
|
|
|
|
|
|
|
// Add the return attributes.
|
|
|
|
if (RAttrs)
|
|
|
|
AttrVec.push_back(llvm::AttributeWithIndex::get(0, RAttrs));
|
|
|
|
|
2009-05-05 10:16:31 +04:00
|
|
|
// 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;
|
|
|
|
}
|
2011-08-03 04:43:55 +04:00
|
|
|
|
|
|
|
// Add any parameter attributes.
|
|
|
|
if (llvm::Attributes PAttrs = AttrList.getParamAttributes(ArgNo + 1))
|
|
|
|
AttrVec.push_back(llvm::AttributeWithIndex::get(ArgNo + 1, PAttrs));
|
2009-05-05 10:16:31 +04:00
|
|
|
}
|
|
|
|
if (DontTransform)
|
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-08-03 04:43:55 +04:00
|
|
|
if (llvm::Attributes FnAttrs = AttrList.getFnAttributes())
|
|
|
|
AttrVec.push_back(llvm::AttributeWithIndex::get(~0, FnAttrs));
|
|
|
|
|
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);
|
2011-07-15 12:37:34 +04:00
|
|
|
llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList, "", CI);
|
2009-05-05 10:16:31 +04:00
|
|
|
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);
|
2011-08-03 04:43:55 +04:00
|
|
|
NewCall->setAttributes(llvm::AttrListPtr::get(AttrVec.begin(),
|
|
|
|
AttrVec.end()));
|
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();
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *Ty = getTypes().GetFunctionType(FI, variadic);
|
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.
|
2011-07-23 14:55:15 +04:00
|
|
|
OldFn->setName(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());
|
2011-09-10 02:41:49 +04:00
|
|
|
if (D->hasAttr<AnnotateAttr>())
|
|
|
|
AddGlobalAnnotations(D, Fn);
|
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?");
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
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
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-14 21:45:50 +04:00
|
|
|
llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
|
2011-07-23 21:14:25 +04:00
|
|
|
ArrayRef<llvm::Type*> Tys) {
|
2011-07-12 18:06:48 +04:00
|
|
|
return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
|
2011-07-14 21:45:50 +04:00
|
|
|
Tys);
|
2007-12-18 03:25:38 +03:00
|
|
|
}
|
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) {
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef String = Literal->getString();
|
2010-08-17 16:54:38 +04:00
|
|
|
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.
|
2011-07-23 14:55:15 +04:00
|
|
|
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.
|
2012-02-05 06:13:05 +04:00
|
|
|
SmallString<128> AsBytes;
|
2009-07-24 03:41:22 +04:00
|
|
|
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;
|
2011-07-23 14:55:15 +04:00
|
|
|
return Map.GetOrCreateValue(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)
|
|
|
|
{
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef String = Literal->getString();
|
2011-05-13 22:13:10 +04:00
|
|
|
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
|
|
|
|
2012-02-07 04:39:47 +04:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
|
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) {
|
2011-07-18 08:24:23 +04:00
|
|
|
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 =
|
2011-07-21 18:31:17 +04:00
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
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
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::StructType *STy =
|
2008-11-15 21:54:24 +03:00
|
|
|
cast<llvm::StructType>(getTypes().ConvertType(CFTy));
|
|
|
|
|
2011-10-15 16:20:02 +04:00
|
|
|
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.
|
2011-07-18 08:24:23 +04:00
|
|
|
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.
|
2012-02-05 06:30:40 +04:00
|
|
|
llvm::Constant *C = llvm::ConstantDataArray::getString(VMContext,
|
|
|
|
Entry.getKey());
|
2009-04-03 04:57:44 +04:00
|
|
|
|
2009-07-16 09:03:48 +04:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
2012-01-10 12:46:39 +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;
|
2012-01-10 12:46:39 +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
|
|
|
|
2012-01-10 12:46:39 +04:00
|
|
|
// Note: -fwritable-strings doesn't make the backing store strings of
|
|
|
|
// CFStrings writable. (See <rdar://problem/10657500>)
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::GlobalVariable *GV =
|
2012-01-10 12:46:39 +04:00
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
|
|
|
|
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
|
|
|
}
|
2011-07-21 18:31:17 +04:00
|
|
|
Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
2009-08-16 09:55:31 +04:00
|
|
|
|
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_");
|
2011-09-02 04:18:52 +04:00
|
|
|
if (const char *Sect = getContext().getTargetInfo().getCFStringSection())
|
2009-04-01 03:42:16 +04:00
|
|
|
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
|
|
|
|
2011-08-09 19:54:21 +04:00
|
|
|
static RecordDecl *
|
|
|
|
CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
|
|
|
|
DeclContext *DC, IdentifierInfo *Id) {
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (Ctx.getLangOptions().CPlusPlus)
|
|
|
|
return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
|
|
|
|
else
|
|
|
|
return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2012-02-07 02:47:00 +04:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
|
2010-04-23 21:41:07 +04:00
|
|
|
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);
|
2011-07-18 08:24:23 +04:00
|
|
|
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.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2011-05-18 02:21:16 +04:00
|
|
|
ConstantStringClassRef =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, PTy);
|
|
|
|
} else {
|
2011-05-18 02:46:11 +04:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "_NSConstantStringClassReference"
|
|
|
|
: "_" + StringClass + "ClassReference";
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
|
2011-05-18 02:46:11 +04:00
|
|
|
GV = CreateRuntimeVariable(PTy, str);
|
2011-05-18 02:21:16 +04:00
|
|
|
// Decay array -> ptr
|
|
|
|
ConstantStringClassRef =
|
2011-07-21 18:31:17 +04:00
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
2010-10-19 21:19:29 +04:00
|
|
|
}
|
2010-04-23 21:41:07 +04:00
|
|
|
}
|
2011-08-09 19:54:21 +04:00
|
|
|
|
|
|
|
if (!NSConstantStringType) {
|
|
|
|
// Construct the type for a constant NSString.
|
|
|
|
RecordDecl *D = CreateRecordDecl(Context, TTK_Struct,
|
|
|
|
Context.getTranslationUnitDecl(),
|
|
|
|
&Context.Idents.get("__builtin_NSString"));
|
|
|
|
D->startDefinition();
|
|
|
|
|
|
|
|
QualType FieldTypes[3];
|
|
|
|
|
|
|
|
// const int *isa;
|
|
|
|
FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst());
|
|
|
|
// const char *str;
|
|
|
|
FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst());
|
|
|
|
// unsigned int length;
|
|
|
|
FieldTypes[2] = Context.UnsignedIntTy;
|
|
|
|
|
|
|
|
// Create fields
|
|
|
|
for (unsigned i = 0; i < 3; ++i) {
|
|
|
|
FieldDecl *Field = FieldDecl::Create(Context, D,
|
|
|
|
SourceLocation(),
|
|
|
|
SourceLocation(), 0,
|
|
|
|
FieldTypes[i], /*TInfo=*/0,
|
|
|
|
/*BitWidth=*/0,
|
|
|
|
/*Mutable=*/false,
|
|
|
|
/*HasInit=*/false);
|
|
|
|
Field->setAccess(AS_public);
|
|
|
|
D->addDecl(Field);
|
|
|
|
}
|
|
|
|
|
|
|
|
D->completeDefinition();
|
|
|
|
QualType NSTy = Context.getTagDeclType(D);
|
|
|
|
NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy));
|
|
|
|
}
|
2010-04-23 21:41:07 +04:00
|
|
|
|
2011-10-15 16:20:02 +04:00
|
|
|
llvm::Constant *Fields[3];
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
// Class pointer.
|
2010-10-19 21:19:29 +04:00
|
|
|
Fields[0] = ConstantStringClassRef;
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
// String pointer.
|
2012-02-05 06:30:40 +04:00
|
|
|
llvm::Constant *C =
|
|
|
|
llvm::ConstantDataArray::getString(VMContext, Entry.getKey());
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
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());
|
2011-07-21 18:31:17 +04:00
|
|
|
Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
2010-04-23 21:41:07 +04:00
|
|
|
|
|
|
|
// String length.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
2010-04-23 21:41:07 +04:00
|
|
|
Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
|
|
|
|
|
|
|
|
// The struct.
|
2011-08-09 19:54:21 +04:00
|
|
|
C = llvm::ConstantStruct::get(NSConstantStringType, Fields);
|
2010-04-23 21:41:07 +04:00
|
|
|
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
|
2011-09-02 04:18:52 +04:00
|
|
|
? getContext().getTargetInfo().getNSStringNonFragileABISection()
|
|
|
|
: getContext().getTargetInfo().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
|
|
|
}
|
|
|
|
|
2011-08-09 21:23:49 +04:00
|
|
|
QualType CodeGenModule::getObjCFastEnumerationStateType() {
|
|
|
|
if (ObjCFastEnumerationStateType.isNull()) {
|
|
|
|
RecordDecl *D = CreateRecordDecl(Context, TTK_Struct,
|
|
|
|
Context.getTranslationUnitDecl(),
|
|
|
|
&Context.Idents.get("__objcFastEnumerationState"));
|
|
|
|
D->startDefinition();
|
|
|
|
|
|
|
|
QualType FieldTypes[] = {
|
|
|
|
Context.UnsignedLongTy,
|
|
|
|
Context.getPointerType(Context.getObjCIdType()),
|
|
|
|
Context.getPointerType(Context.UnsignedLongTy),
|
|
|
|
Context.getConstantArrayType(Context.UnsignedLongTy,
|
|
|
|
llvm::APInt(32, 5), ArrayType::Normal, 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 4; ++i) {
|
|
|
|
FieldDecl *Field = FieldDecl::Create(Context,
|
|
|
|
D,
|
|
|
|
SourceLocation(),
|
|
|
|
SourceLocation(), 0,
|
|
|
|
FieldTypes[i], /*TInfo=*/0,
|
|
|
|
/*BitWidth=*/0,
|
|
|
|
/*Mutable=*/false,
|
|
|
|
/*HasInit=*/false);
|
|
|
|
Field->setAccess(AS_public);
|
|
|
|
D->addDecl(Field);
|
|
|
|
}
|
|
|
|
|
|
|
|
D->completeDefinition();
|
|
|
|
ObjCFastEnumerationStateType = Context.getTagDeclType(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjCFastEnumerationStateType;
|
|
|
|
}
|
|
|
|
|
2011-11-01 06:23:42 +04:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
|
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
|
|
|
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
2012-02-07 02:47:00 +04:00
|
|
|
if (E->getCharByteWidth() == 1) {
|
|
|
|
SmallString<64> Str(E->getString());
|
|
|
|
|
|
|
|
// Resize the string to the right size, which is indicated by its type.
|
|
|
|
const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
|
|
|
|
Str.resize(CAT->getSize().getZExtValue());
|
|
|
|
return llvm::ConstantDataArray::getString(VMContext, Str, false);
|
|
|
|
}
|
2012-02-05 06:30:40 +04:00
|
|
|
|
|
|
|
llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
|
|
|
|
llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
2012-02-07 02:52:04 +04:00
|
|
|
|
|
|
|
// Wide strings have either 2-byte or 4-byte elements.
|
|
|
|
if (ElemTy->getPrimitiveSizeInBits() == 16) {
|
|
|
|
SmallVector<uint16_t, 32> Elements;
|
|
|
|
Elements.reserve(NumElements);
|
|
|
|
|
|
|
|
for(unsigned i = 0, e = E->getLength(); i != e; ++i)
|
|
|
|
Elements.push_back(E->getCodeUnit(i));
|
|
|
|
Elements.resize(NumElements);
|
|
|
|
return llvm::ConstantDataArray::get(VMContext, Elements);
|
2012-02-05 06:30:40 +04:00
|
|
|
}
|
|
|
|
|
2012-02-07 02:52:04 +04:00
|
|
|
assert(ElemTy->getPrimitiveSizeInBits() == 32);
|
|
|
|
SmallVector<uint32_t, 32> Elements;
|
|
|
|
Elements.reserve(NumElements);
|
|
|
|
|
|
|
|
for(unsigned i = 0, e = E->getLength(); i != e; ++i)
|
|
|
|
Elements.push_back(E->getCodeUnit(i));
|
|
|
|
Elements.resize(NumElements);
|
|
|
|
return llvm::ConstantDataArray::get(VMContext, Elements);
|
2011-11-01 06:23:42 +04:00
|
|
|
}
|
|
|
|
|
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) {
|
2011-08-04 05:03:22 +04:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(S->getType());
|
2011-11-01 06:23:42 +04:00
|
|
|
if (S->isAscii() || S->isUTF8()) {
|
2012-02-07 02:47:00 +04:00
|
|
|
SmallString<64> Str(S->getString());
|
|
|
|
|
|
|
|
// Resize the string to the right size, which is indicated by its type.
|
|
|
|
const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType());
|
|
|
|
Str.resize(CAT->getSize().getZExtValue());
|
|
|
|
return GetAddrOfConstantString(Str, /*GlobalName*/ 0, Align.getQuantity());
|
2009-11-16 08:55:46 +03:00
|
|
|
}
|
2011-11-01 06:23:42 +04:00
|
|
|
|
2012-02-07 02:47:00 +04:00
|
|
|
// FIXME: the following does not memoize wide strings.
|
2011-11-01 06:23:42 +04:00
|
|
|
llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(),C->getType(),
|
|
|
|
!Features.WritableStrings,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
C,".str");
|
2012-01-18 16:11:32 +04:00
|
|
|
|
2011-11-01 06:23:42 +04:00
|
|
|
GV->setAlignment(Align.getQuantity());
|
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
return GV;
|
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-08-04 05:03:22 +04:00
|
|
|
static llvm::GlobalVariable *GenerateStringLiteral(StringRef str,
|
2007-11-28 08:34:05 +03:00
|
|
|
bool constant,
|
2008-10-18 01:56:50 +04:00
|
|
|
CodeGenModule &CGM,
|
2011-08-04 05:03:22 +04:00
|
|
|
const char *GlobalName,
|
|
|
|
unsigned Alignment) {
|
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 =
|
2012-02-05 06:30:40 +04:00
|
|
|
llvm::ConstantDataArray::getString(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-08-04 05:03:22 +04:00
|
|
|
GV->setAlignment(Alignment);
|
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-07-23 14:55:15 +04:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantString(StringRef Str,
|
2011-08-04 05:03:22 +04:00
|
|
|
const char *GlobalName,
|
|
|
|
unsigned Alignment) {
|
2009-04-01 03:42:16 +04:00
|
|
|
// 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.
|
2012-02-07 02:47:00 +04:00
|
|
|
if (Features.WritableStrings)
|
2011-08-04 05:03:22 +04:00
|
|
|
return GenerateStringLiteral(Str, false, *this, GlobalName, Alignment);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-08-04 05:03:22 +04:00
|
|
|
llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
|
2011-03-05 16:45:23 +03:00
|
|
|
ConstantStringMap.GetOrCreateValue(Str);
|
2007-11-28 08:34:05 +03:00
|
|
|
|
2011-08-04 05:03:22 +04:00
|
|
|
if (llvm::GlobalVariable *GV = Entry.getValue()) {
|
|
|
|
if (Alignment > GV->getAlignment()) {
|
|
|
|
GV->setAlignment(Alignment);
|
|
|
|
}
|
|
|
|
return GV;
|
|
|
|
}
|
2007-11-28 08:34:05 +03:00
|
|
|
|
|
|
|
// Create a global variable for this.
|
2012-02-07 02:47:00 +04:00
|
|
|
llvm::GlobalVariable *GV = GenerateStringLiteral(Str, true, *this, GlobalName,
|
|
|
|
Alignment);
|
2011-08-04 05:03:22 +04:00
|
|
|
Entry.setValue(GV);
|
|
|
|
return GV;
|
2007-11-28 08:34:05 +03:00
|
|
|
}
|
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,
|
2011-08-04 05:03:22 +04:00
|
|
|
const char *GlobalName,
|
|
|
|
unsigned Alignment) {
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef StrWithNull(Str.c_str(), Str.size() + 1);
|
2011-08-04 05:03:22 +04:00
|
|
|
return GetAddrOfConstantString(StrWithNull, GlobalName, Alignment);
|
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) {
|
2011-07-22 06:08:32 +04:00
|
|
|
const ObjCInterfaceDecl *iface = impl->getClassInterface();
|
|
|
|
for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
|
2011-03-22 10:05:39 +03:00
|
|
|
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(),
|
2011-08-17 23:25:08 +04:00
|
|
|
cxxSelector, getContext().VoidTy, 0, D,
|
|
|
|
/*isInstance=*/true, /*isVariadic=*/false,
|
|
|
|
/*isSynthesized=*/true, /*isImplicitlyDeclared=*/true,
|
|
|
|
/*isDefined=*/false, ObjCMethodDecl::Required);
|
2011-03-22 10:05:39 +03:00
|
|
|
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(),
|
2011-10-03 10:36:36 +04:00
|
|
|
D->getLocation(),
|
|
|
|
cxxSelector,
|
2010-04-29 01:28:56 +04:00
|
|
|
getContext().getObjCIdType(), 0,
|
2011-08-17 23:25:08 +04:00
|
|
|
D, /*isInstance=*/true,
|
|
|
|
/*isVariadic=*/false,
|
|
|
|
/*isSynthesized=*/true,
|
|
|
|
/*isImplicitlyDeclared=*/true,
|
|
|
|
/*isDefined=*/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:
|
2011-12-03 03:23:56 +04:00
|
|
|
case Decl::Import:
|
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.
|
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
|
|
|
|
2012-01-02 01:23:57 +04:00
|
|
|
case Decl::ObjCProtocol: {
|
|
|
|
ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(D);
|
|
|
|
if (Proto->isThisDeclarationADefinition())
|
|
|
|
ObjCRuntime->GenerateProtocol(Proto);
|
2008-08-16 03:26:23 +04:00
|
|
|
break;
|
2012-01-02 01:23:57 +04:00
|
|
|
}
|
|
|
|
|
2008-08-16 03:26:23 +04:00
|
|
|
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.
|
2011-07-28 00:29:46 +04:00
|
|
|
ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
|
2008-08-16 03:26:23 +04:00
|
|
|
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);
|
2011-07-28 00:29:46 +04:00
|
|
|
ObjCRuntime->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:
|
2012-01-31 22:59:20 +04:00
|
|
|
ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
|
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);
|
2011-07-23 14:55:15 +04:00
|
|
|
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);
|
2011-07-24 00:04:25 +04:00
|
|
|
else if (*--S.end() == '\n')
|
|
|
|
getModule().setModuleInlineAsm(S + AsmString.str());
|
2008-08-16 03:26:23 +04:00
|
|
|
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);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
|
2010-07-07 03:57:41 +04:00
|
|
|
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
|
2011-07-23 14:55:15 +04:00
|
|
|
for (llvm::DenseMap<GlobalDecl,StringRef>::iterator
|
2010-07-07 03:57:41 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|