2007-05-28 09:07:47 +08:00
|
|
|
//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-28 09:07:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-module state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenModule.h"
|
2011-10-07 02:29:37 +08:00
|
|
|
#include "CGCUDARuntime.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGCall.h"
|
|
|
|
#include "CGDebugInfo.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2011-09-20 05:14:35 +08:00
|
|
|
#include "CGOpenCLRuntime.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenTBAA.h"
|
2010-01-10 20:58:08 +08:00
|
|
|
#include "TargetInfo.h"
|
2007-06-16 08:16:26 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-01-26 21:48:07 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2008-11-05 00:51:42 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-06-22 02:41:26 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2009-12-01 07:41:22 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2011-10-27 04:41:06 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2011-12-19 22:41:01 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2013-02-09 06:30:41 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2007-12-02 15:19:18 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2013-01-15 01:21:00 +08:00
|
|
|
#include "clang/Basic/Module.h"
|
2008-04-19 12:17:09 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-06-23 02:48:09 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2012-02-26 04:51:20 +08:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2010-03-06 08:35:14 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2010-04-10 10:56:12 +08:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2013-01-30 20:06:08 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2009-11-07 17:22:46 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2013-01-30 20:06:08 +08:00
|
|
|
|
2007-05-28 09:07:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
static const char AnnotationSection[] = "llvm.metadata";
|
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
static CGCXXABI &createCXXABI(CodeGenModule &CGM) {
|
2013-04-17 06:48:15 +08:00
|
|
|
switch (CGM.getTarget().getCXXABI().getKind()) {
|
2013-01-31 20:13:10 +08:00
|
|
|
case TargetCXXABI::GenericAArch64:
|
2013-01-26 07:36:14 +08:00
|
|
|
case TargetCXXABI::GenericARM:
|
|
|
|
case TargetCXXABI::iOS:
|
|
|
|
case TargetCXXABI::GenericItanium:
|
|
|
|
return *CreateItaniumCXXABI(CGM);
|
|
|
|
case TargetCXXABI::Microsoft:
|
|
|
|
return *CreateMicrosoftCXXABI(CGM);
|
2010-08-23 05:01:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("invalid C++ ABI kind");
|
|
|
|
}
|
|
|
|
|
2007-05-28 09:07:47 +08:00
|
|
|
|
2009-11-13 01:24:48 +08:00
|
|
|
CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
|
2013-04-17 06:48:20 +08:00
|
|
|
llvm::Module &M, const llvm::DataLayout &TD,
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &diags)
|
2013-04-17 06:48:15 +08:00
|
|
|
: Context(C), LangOpts(C.getLangOpts()), CodeGenOpts(CGO), TheModule(M),
|
|
|
|
Diags(diags), TheDataLayout(TD), Target(C.getTargetInfo()),
|
|
|
|
ABI(createCXXABI(*this)), VMContext(M.getContext()), TBAA(0),
|
|
|
|
TheTargetCodeGenInfo(0), Types(*this), VTables(*this),
|
|
|
|
ObjCRuntime(0), OpenCLRuntime(0), CUDARuntime(0),
|
2012-02-16 08:57:37 +08:00
|
|
|
DebugInfo(0), ARCData(0), NoObjCARCExceptionsMetadata(0),
|
|
|
|
RRData(0), CFConstantStringClassRef(0),
|
2011-10-07 02:29:37 +08:00
|
|
|
ConstantStringClassRef(0), NSConstantStringType(0),
|
2010-07-16 08:00:15 +08:00
|
|
|
NSConcreteGlobalBlock(0), NSConcreteStackBlock(0),
|
2011-02-08 16:22:06 +08:00
|
|
|
BlockObjectAssign(0), BlockObjectDispose(0),
|
2013-01-18 19:30:38 +08:00
|
|
|
BlockDescriptorType(0), GenericBlockLiteralType(0),
|
2013-03-23 14:43:35 +08:00
|
|
|
LifetimeStartFn(0), LifetimeEndFn(0),
|
2013-01-18 19:30:38 +08:00
|
|
|
SanitizerBlacklist(CGO.SanitizerBlacklistFile),
|
|
|
|
SanOpts(SanitizerBlacklist.isIn(M) ?
|
|
|
|
SanitizerOptions::Disabled : LangOpts.Sanitize) {
|
|
|
|
|
2012-02-07 08:39:47 +08: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);
|
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.ObjC1)
|
2011-09-20 05:14:35 +08:00
|
|
|
createObjCRuntime();
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.OpenCL)
|
2011-09-20 05:14:35 +08:00
|
|
|
createOpenCLRuntime();
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.CUDA)
|
2011-10-07 02:29:37 +08:00
|
|
|
createCUDARuntime();
|
2008-05-08 16:54:20 +08:00
|
|
|
|
2012-04-24 14:57:01 +08:00
|
|
|
// Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
|
2013-01-18 19:30:38 +08:00
|
|
|
if (SanOpts.Thread ||
|
2012-04-24 14:57:01 +08:00
|
|
|
(!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
|
|
|
|
TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),
|
2010-10-16 04:23:12 +08:00
|
|
|
ABI.getMangleContext());
|
2010-10-15 07:06:10 +08:00
|
|
|
|
2011-04-22 07:44:07 +08:00
|
|
|
// If debug info or coverage generation is enabled, create the CGDebugInfo
|
|
|
|
// object.
|
2012-10-24 04:05:01 +08:00
|
|
|
if (CodeGenOpts.getDebugInfo() != CodeGenOptions::NoDebugInfo ||
|
2012-04-27 15:24:20 +08:00
|
|
|
CodeGenOpts.EmitGcovArcs ||
|
2011-04-22 07:44:07 +08:00
|
|
|
CodeGenOpts.EmitGcovNotes)
|
|
|
|
DebugInfo = new CGDebugInfo(*this);
|
2011-02-08 16:22:06 +08:00
|
|
|
|
|
|
|
Block.GlobalUniqueCount = 0;
|
2011-02-15 17:22:45 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (C.getLangOpts().ObjCAutoRefCount)
|
2011-06-16 07:02:42 +08:00
|
|
|
ARCData = new ARCEntrypoints();
|
|
|
|
RRData = new RREntrypoints();
|
2008-03-01 16:45:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenModule::~CodeGenModule() {
|
2011-07-28 04:29:46 +08:00
|
|
|
delete ObjCRuntime;
|
2011-09-20 05:14:35 +08:00
|
|
|
delete OpenCLRuntime;
|
2011-10-07 02:29:37 +08:00
|
|
|
delete CUDARuntime;
|
2011-10-08 13:28:26 +08:00
|
|
|
delete TheTargetCodeGenInfo;
|
2010-08-23 05:01:12 +08:00
|
|
|
delete &ABI;
|
2010-10-16 02:04:46 +08:00
|
|
|
delete TBAA;
|
2008-08-06 02:50:11 +08:00
|
|
|
delete DebugInfo;
|
2011-06-16 07:02:42 +08:00
|
|
|
delete ARCData;
|
|
|
|
delete RRData;
|
2008-08-06 02:50:11 +08:00
|
|
|
}
|
|
|
|
|
2010-01-23 10:40:42 +08:00
|
|
|
void CodeGenModule::createObjCRuntime() {
|
2012-06-20 14:18:46 +08:00
|
|
|
// This is just isGNUFamily(), but we want to force implementors of
|
|
|
|
// new ABIs to decide how best to do this.
|
|
|
|
switch (LangOpts.ObjCRuntime.getKind()) {
|
2012-07-04 04:49:52 +08:00
|
|
|
case ObjCRuntime::GNUstep:
|
|
|
|
case ObjCRuntime::GCC:
|
2012-07-12 10:07:58 +08:00
|
|
|
case ObjCRuntime::ObjFW:
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime = CreateGNUObjCRuntime(*this);
|
2012-06-20 14:18:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ObjCRuntime::FragileMacOSX:
|
|
|
|
case ObjCRuntime::MacOSX:
|
|
|
|
case ObjCRuntime::iOS:
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime = CreateMacObjCRuntime(*this);
|
2012-06-20 14:18:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad runtime kind");
|
2010-01-23 10:40:42 +08:00
|
|
|
}
|
|
|
|
|
2011-09-20 05:14:35 +08:00
|
|
|
void CodeGenModule::createOpenCLRuntime() {
|
|
|
|
OpenCLRuntime = new CGOpenCLRuntime(*this);
|
|
|
|
}
|
|
|
|
|
2011-10-07 02:29:37 +08:00
|
|
|
void CodeGenModule::createCUDARuntime() {
|
|
|
|
CUDARuntime = CreateNVCUDARuntime(*this);
|
|
|
|
}
|
|
|
|
|
2008-08-06 02:50:11 +08:00
|
|
|
void CodeGenModule::Release() {
|
2009-03-23 05:21:57 +08:00
|
|
|
EmitDeferred();
|
2010-01-08 08:50:11 +08:00
|
|
|
EmitCXXGlobalInitFunc();
|
2010-03-20 12:15:41 +08:00
|
|
|
EmitCXXGlobalDtorFunc();
|
2013-04-20 00:42:07 +08:00
|
|
|
EmitCXXThreadLocalInitFunc();
|
2011-07-28 04:29:46 +08:00
|
|
|
if (ObjCRuntime)
|
|
|
|
if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
|
2008-08-12 02:12:00 +08:00
|
|
|
AddGlobalCtor(ObjCInitFunction);
|
2008-08-01 08:01:51 +08:00
|
|
|
EmitCtorList(GlobalCtors, "llvm.global_ctors");
|
|
|
|
EmitCtorList(GlobalDtors, "llvm.global_dtors");
|
2011-09-10 06:41:49 +08:00
|
|
|
EmitGlobalAnnotations();
|
2013-04-06 13:00:46 +08:00
|
|
|
EmitStaticExternCAliases();
|
2009-02-14 04:29:50 +08:00
|
|
|
EmitLLVMUsed();
|
2013-01-16 09:23:41 +08:00
|
|
|
|
2013-05-08 21:44:39 +08:00
|
|
|
if (CodeGenOpts.Autolink &&
|
|
|
|
(Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
|
2013-01-16 09:23:41 +08:00
|
|
|
EmitModuleLinkOptions();
|
|
|
|
}
|
2013-06-19 09:46:49 +08:00
|
|
|
if (CodeGenOpts.DwarfVersion)
|
|
|
|
// We actually want the latest version when there are conflicts.
|
|
|
|
// We can change from Warning to Latest if such mode is supported.
|
|
|
|
getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version",
|
|
|
|
CodeGenOpts.DwarfVersion);
|
Switch autolinking metadata format over to actual linker options, e.g.,
!0 = metadata !{metadata !"-lautolink"}
!1 = metadata !{metadata !"-framework", metadata !"autolink_framework"}
referenced from llvm.module.linkoptions, e.g.,
!llvm.module.linkoptions = !{!0, !1, !2, !3}
This conceptually moves the logic for figuring out the syntax the
linker will accept from LLVM into Clang. Moreover, it makes it easier
to support MSVC's
#pragma comment(linker, "some option")
in the future, should anyone care to do so.
llvm-svn: 172441
2013-01-15 02:28:43 +08:00
|
|
|
|
2010-09-16 14:16:50 +08:00
|
|
|
SimplifyPersonality();
|
|
|
|
|
2010-07-07 07:57:41 +08:00
|
|
|
if (getCodeGenOpts().EmitDeclMetadata)
|
|
|
|
EmitDeclMetadata();
|
2011-05-05 04:46:58 +08:00
|
|
|
|
|
|
|
if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
|
2011-05-05 08:08:20 +08:00
|
|
|
EmitCoverageFile();
|
2011-08-17 04:58:22 +08:00
|
|
|
|
|
|
|
if (DebugInfo)
|
|
|
|
DebugInfo->finalize();
|
2008-10-01 08:49:24 +08:00
|
|
|
}
|
|
|
|
|
2011-03-24 00:29:39 +08:00
|
|
|
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
|
|
|
|
// Make sure that this type is translated.
|
|
|
|
Types.UpdateCompletedType(TD);
|
|
|
|
}
|
|
|
|
|
2010-10-15 07:06:10 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
|
|
|
return 0;
|
|
|
|
return TBAA->getTBAAInfo(QTy);
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:03:51 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() {
|
|
|
|
if (!TBAA)
|
|
|
|
return 0;
|
|
|
|
return TBAA->getTBAAInfoForVTablePtr();
|
|
|
|
}
|
|
|
|
|
2012-09-29 05:58:29 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
|
|
|
return 0;
|
|
|
|
return TBAA->getTBAAStructInfo(QTy);
|
|
|
|
}
|
|
|
|
|
2013-04-05 05:53:22 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAStructTypeInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
|
|
|
return 0;
|
|
|
|
return TBAA->getTBAAStructTypeInfo(QTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy,
|
|
|
|
llvm::MDNode *AccessN,
|
|
|
|
uint64_t O) {
|
|
|
|
if (!TBAA)
|
|
|
|
return 0;
|
|
|
|
return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O);
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:02:56 +08:00
|
|
|
/// Decorate the instruction with a TBAA tag. For scalar TBAA, the tag
|
|
|
|
/// is the same as the type. For struct-path aware TBAA, the tag
|
|
|
|
/// is different from the type: base type, access type and offset.
|
|
|
|
/// When ConvertTypeToTag is true, we create a tag based on the scalar type.
|
2010-10-15 07:06:10 +08:00
|
|
|
void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
|
2013-04-12 07:02:56 +08:00
|
|
|
llvm::MDNode *TBAAInfo,
|
|
|
|
bool ConvertTypeToTag) {
|
|
|
|
if (ConvertTypeToTag && TBAA && CodeGenOpts.StructPathTBAA)
|
|
|
|
Inst->setMetadata(llvm::LLVMContext::MD_tbaa,
|
|
|
|
TBAA->getTBAAScalarTagInfo(TBAAInfo));
|
|
|
|
else
|
|
|
|
Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
|
2010-10-15 07:06:10 +08:00
|
|
|
}
|
|
|
|
|
2012-03-31 03:44:53 +08:00
|
|
|
void CodeGenModule::Error(SourceLocation loc, StringRef error) {
|
|
|
|
unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, error);
|
2011-03-18 10:56:14 +08:00
|
|
|
getDiags().Report(Context.getFullLoc(loc), diagID);
|
|
|
|
}
|
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 15:19:18 +08:00
|
|
|
/// specified stmt yet.
|
2008-09-04 11:43:08 +08:00
|
|
|
void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
if (OmitOnError && getDiags().hasErrorOccurred())
|
|
|
|
return;
|
2011-09-26 07:23:43 +08:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
|
2009-02-07 03:18:03 +08:00
|
|
|
"cannot compile this %0 yet");
|
2007-12-02 15:19:18 +08: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?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
|
|
|
|
<< Msg << S->getSourceRange();
|
2007-12-02 15:19:18 +08:00
|
|
|
}
|
2007-12-02 14:27:33 +08:00
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2008-01-12 15:05:38 +08:00
|
|
|
/// specified decl yet.
|
2008-09-04 11:43:08 +08:00
|
|
|
void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
if (OmitOnError && getDiags().hasErrorOccurred())
|
|
|
|
return;
|
2011-09-26 07:23:43 +08:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
|
2009-02-07 03:18:03 +08:00
|
|
|
"cannot compile this %0 yet");
|
2008-01-12 15:05:38 +08: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?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
|
2008-01-12 15:05:38 +08:00
|
|
|
}
|
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
|
|
|
|
return llvm::ConstantInt::get(SizeTy, size.getQuantity());
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
|
2011-01-30 03:39:23 +08:00
|
|
|
const NamedDecl *D) const {
|
2009-04-14 14:00:08 +08:00
|
|
|
// Internal definitions always have default visibility.
|
2009-04-14 13:27:13 +08:00
|
|
|
if (GV->hasLocalLinkage()) {
|
2009-04-11 04:26:50 +08:00
|
|
|
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
2009-04-07 13:48:37 +08:00
|
|
|
return;
|
2009-04-11 04:26:50 +08:00
|
|
|
}
|
2009-04-07 13:48:37 +08:00
|
|
|
|
2010-10-30 19:50:40 +08:00
|
|
|
// Set visibility for definitions.
|
2013-02-27 10:15:29 +08:00
|
|
|
LinkageInfo LV = D->getLinkageAndVisibility();
|
2013-02-27 10:56:45 +08:00
|
|
|
if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage())
|
|
|
|
GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
|
2009-04-03 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
2012-06-28 16:01:44 +08:00
|
|
|
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
|
|
|
|
return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
|
|
|
|
.Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
|
|
|
|
.Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
|
|
|
|
.Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
|
|
|
|
.Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(
|
|
|
|
CodeGenOptions::TLSModel M) {
|
|
|
|
switch (M) {
|
|
|
|
case CodeGenOptions::GeneralDynamicTLSModel:
|
|
|
|
return llvm::GlobalVariable::GeneralDynamicTLSModel;
|
|
|
|
case CodeGenOptions::LocalDynamicTLSModel:
|
|
|
|
return llvm::GlobalVariable::LocalDynamicTLSModel;
|
|
|
|
case CodeGenOptions::InitialExecTLSModel:
|
|
|
|
return llvm::GlobalVariable::InitialExecTLSModel;
|
|
|
|
case CodeGenOptions::LocalExecTLSModel:
|
|
|
|
return llvm::GlobalVariable::LocalExecTLSModel;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid TLS model!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::setTLSMode(llvm::GlobalVariable *GV,
|
|
|
|
const VarDecl &D) const {
|
2013-04-13 10:43:54 +08:00
|
|
|
assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
|
2012-06-28 16:01:44 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable::ThreadLocalMode TLM;
|
2012-10-24 04:05:01 +08:00
|
|
|
TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel());
|
2012-06-28 16:01:44 +08:00
|
|
|
|
|
|
|
// Override the TLS model if it is explicitly specified.
|
|
|
|
if (D.hasAttr<TLSModelAttr>()) {
|
|
|
|
const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>();
|
|
|
|
TLM = GetLLVMTLSModel(Attr->getModel());
|
|
|
|
}
|
|
|
|
|
|
|
|
GV->setThreadLocalMode(TLM);
|
|
|
|
}
|
|
|
|
|
2010-08-04 16:34:44 +08: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-30 04:24:48 +08:00
|
|
|
TypeVisibilityKind TVK) const {
|
2011-01-30 03:39:23 +08:00
|
|
|
setGlobalVisibility(GV, RD);
|
2010-08-04 16:34:44 +08:00
|
|
|
|
2010-08-13 07:36:15 +08:00
|
|
|
if (!CodeGenOpts.HiddenWeakVTables)
|
|
|
|
return;
|
|
|
|
|
2011-01-30 04:36:11 +08:00
|
|
|
// We never want to drop the visibility for RTTI names.
|
|
|
|
if (TVK == TVK_ForRTTIName)
|
|
|
|
return;
|
|
|
|
|
2010-08-04 16:34:44 +08: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 04:39:18 +08:00
|
|
|
// This should be kept roughly in sync with setThunkVisibility
|
|
|
|
// in CGVTables.cpp.
|
|
|
|
|
2010-08-04 16:34:44 +08:00
|
|
|
// Preconditions.
|
2011-01-24 08:46:19 +08:00
|
|
|
if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage ||
|
2010-08-04 16:34:44 +08:00
|
|
|
GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't override an explicit visibility attribute.
|
2013-02-20 09:54:26 +08:00
|
|
|
if (RD->getExplicitVisibility(NamedDecl::VisibilityForType))
|
2010-08-04 16:34:44 +08: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 04:39:18 +08:00
|
|
|
// Every use of a non-template class's type information has to emit it.
|
2010-08-04 16:34:44 +08:00
|
|
|
case TSK_Undeclared:
|
|
|
|
break;
|
|
|
|
|
2010-08-06 04:39:18 +08: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 16:34:44 +08:00
|
|
|
case TSK_ImplicitInstantiation:
|
2012-10-24 22:11:55 +08:00
|
|
|
return;
|
2010-08-04 16:34:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!(TVK != TVK_ForRTTI) || LangOpts.RTTI) {
|
2013-01-26 06:31:03 +08:00
|
|
|
// FIXME: what should we do if we "lose" the key function during
|
|
|
|
// the emission of the file?
|
|
|
|
if (Context.getCurrentKeyFunction(RD))
|
2010-08-04 16:34:44 +08:00
|
|
|
return;
|
2011-01-30 04:24:48 +08:00
|
|
|
}
|
2010-08-04 16:34:44 +08:00
|
|
|
|
|
|
|
// Otherwise, drop the visibility to hidden.
|
|
|
|
GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
2011-01-12 05:44:37 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
2010-08-04 16:34:44 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
|
2010-06-23 00:05:32 +08:00
|
|
|
const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()];
|
2010-06-23 00:05:32 +08:00
|
|
|
if (!Str.empty())
|
|
|
|
return Str;
|
|
|
|
|
2010-08-31 15:33:07 +08:00
|
|
|
if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
|
2010-06-23 00:05:32 +08:00
|
|
|
IdentifierInfo *II = ND->getIdentifier();
|
|
|
|
assert(II && "Attempt to mangle unnamed decl.");
|
|
|
|
|
|
|
|
Str = II->getName();
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Buffer;
|
2011-02-11 07:59:36 +08:00
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
2010-06-23 00:05:32 +08:00
|
|
|
if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND))
|
2011-02-11 07:59:36 +08:00
|
|
|
getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out);
|
2010-06-23 00:05:32 +08:00
|
|
|
else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND))
|
2011-02-11 07:59:36 +08:00
|
|
|
getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out);
|
2010-06-23 00:05:32 +08:00
|
|
|
else
|
2011-02-11 07:59:36 +08:00
|
|
|
getCXXABI().getMangleContext().mangleName(ND, Out);
|
2010-06-23 00:05:32 +08:00
|
|
|
|
|
|
|
// Allocate space for the mangled name.
|
2011-02-11 07:59:36 +08:00
|
|
|
Out.flush();
|
2010-06-23 00:05:32 +08:00
|
|
|
size_t Length = Buffer.size();
|
|
|
|
char *Name = MangledNamesAllocator.Allocate<char>(Length);
|
|
|
|
std::copy(Buffer.begin(), Buffer.end(), Name);
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
Str = StringRef(Name, Length);
|
2010-06-23 00:05:32 +08:00
|
|
|
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
2011-01-14 02:57:25 +08:00
|
|
|
void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
|
|
|
|
const BlockDecl *BD) {
|
|
|
|
MangleContext &MangleCtx = getCXXABI().getMangleContext();
|
|
|
|
const Decl *D = GD.getDecl();
|
2011-02-11 07:59:36 +08:00
|
|
|
llvm::raw_svector_ostream Out(Buffer.getBuffer());
|
2011-01-14 02:57:25 +08:00
|
|
|
if (D == 0)
|
2012-06-27 00:06:38 +08:00
|
|
|
MangleCtx.mangleGlobalBlock(BD,
|
|
|
|
dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
|
2011-02-11 07:59:36 +08:00
|
|
|
MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D))
|
2011-02-11 07:59:36 +08:00
|
|
|
MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
else
|
2011-02-11 07:59:36 +08:00
|
|
|
MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
|
2010-06-09 10:36:32 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
|
2010-03-20 07:29:14 +08:00
|
|
|
return getModule().getNamedValue(Name);
|
2009-02-13 08:10:09 +08:00
|
|
|
}
|
|
|
|
|
2008-03-15 01:18:18 +08:00
|
|
|
/// AddGlobalCtor - Add a function to the list that will be called before
|
|
|
|
/// main() runs.
|
2008-08-01 08:01:51 +08:00
|
|
|
void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) {
|
2009-01-13 10:25:00 +08:00
|
|
|
// FIXME: Type coercion of void()* types.
|
2008-08-01 08:01:51 +08:00
|
|
|
GlobalCtors.push_back(std::make_pair(Ctor, Priority));
|
2008-03-15 01:18:18 +08:00
|
|
|
}
|
|
|
|
|
2008-08-01 08:01:51 +08: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 10:25:00 +08:00
|
|
|
// FIXME: Type coercion of void()* types.
|
2008-08-01 08:01:51 +08: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 09:53:33 +08:00
|
|
|
llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
|
2008-08-01 08:01:51 +08:00
|
|
|
|
|
|
|
// Get the type of a ctor entry, { i32, void ()* }.
|
2011-06-20 12:01:35 +08:00
|
|
|
llvm::StructType *CtorStructTy =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::StructType::get(Int32Ty, llvm::PointerType::getUnqual(CtorFTy), NULL);
|
2008-08-01 08:01:51 +08:00
|
|
|
|
|
|
|
// Construct the constructor and destructor arrays.
|
2012-02-07 06:16:34 +08:00
|
|
|
SmallVector<llvm::Constant*, 8> Ctors;
|
2008-08-01 08:01:51 +08:00
|
|
|
for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
|
2012-02-07 06:16:34 +08:00
|
|
|
llvm::Constant *S[] = {
|
|
|
|
llvm::ConstantInt::get(Int32Ty, I->second, false),
|
|
|
|
llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)
|
|
|
|
};
|
2009-07-28 06:29:56 +08:00
|
|
|
Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
|
2008-08-01 08:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ctors.empty()) {
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
|
2009-07-09 03:05:04 +08:00
|
|
|
new llvm::GlobalVariable(TheModule, AT, false,
|
2008-03-19 13:24:56 +08:00
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
2009-07-29 02:33:04 +08:00
|
|
|
llvm::ConstantArray::get(AT, Ctors),
|
2009-07-09 03:05:04 +08:00
|
|
|
GlobalName);
|
2008-03-15 01:18:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-19 09:32:20 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes
|
2013-06-06 01:49:37 +08:00
|
|
|
CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
|
2013-07-02 04:53:07 +08:00
|
|
|
const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl());
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
|
|
|
|
if (isa<CXXDestructorDecl>(D) &&
|
|
|
|
getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
|
|
|
|
GD.getDtorType()))
|
|
|
|
return llvm::Function::LinkOnceODRLinkage;
|
|
|
|
|
2010-07-30 02:15:58 +08:00
|
|
|
GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2010-07-01 00:58:07 +08:00
|
|
|
if (Linkage == GVA_Internal)
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::InternalLinkage;
|
2010-07-01 00:58:07 +08:00
|
|
|
|
|
|
|
if (D->hasAttr<DLLExportAttr>())
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::DLLExportLinkage;
|
2010-07-01 00:58:07 +08:00
|
|
|
|
|
|
|
if (D->hasAttr<WeakAttr>())
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::WeakAnyLinkage;
|
2010-07-01 00:58:07 +08: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 08:08:13 +08:00
|
|
|
return llvm::Function::AvailableExternallyLinkage;
|
2011-09-20 02:05:26 +08: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-07-01 00:58:07 +08: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)
|
2012-03-11 15:00:24 +08:00
|
|
|
return !Context.getLangOpts().AppleKext
|
2011-02-04 08:01:24 +08:00
|
|
|
? llvm::Function::LinkOnceODRLinkage
|
|
|
|
: llvm::Function::InternalLinkage;
|
2010-07-01 00:58:07 +08: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)
|
2012-03-11 15:00:24 +08:00
|
|
|
return !Context.getLangOpts().AppleKext
|
2011-02-04 08:01:24 +08:00
|
|
|
? llvm::Function::WeakODRLinkage
|
2011-09-20 02:05:26 +08:00
|
|
|
: llvm::Function::ExternalLinkage;
|
2010-07-01 00:58:07 +08:00
|
|
|
|
|
|
|
// Otherwise, we have strong external linkage.
|
|
|
|
assert(Linkage == GVA_StrongExternal);
|
|
|
|
return llvm::Function::ExternalLinkage;
|
2010-02-19 09:32:20 +08:00
|
|
|
}
|
2008-06-08 23:45:52 +08:00
|
|
|
|
2010-02-19 09:32:20 +08: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 16:05:55 +08:00
|
|
|
SetCommonAttributes(D, GV);
|
2008-06-08 23:45:52 +08:00
|
|
|
}
|
|
|
|
|
2009-04-14 15:08:30 +08:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
|
2009-09-09 23:08:12 +08:00
|
|
|
const CGFunctionInfo &Info,
|
2009-04-14 15:08:30 +08:00
|
|
|
llvm::Function *F) {
|
2009-09-12 08:59:20 +08:00
|
|
|
unsigned CallingConv;
|
2008-09-26 05:02:23 +08:00
|
|
|
AttributeListType AttributeList;
|
2013-02-22 08:13:35 +08:00
|
|
|
ConstructAttributeList(Info, D, AttributeList, CallingConv, false);
|
2012-12-08 07:17:26 +08:00
|
|
|
F->setAttributes(llvm::AttributeSet::get(getLLVMContext(), AttributeList));
|
2009-09-12 08:59:20 +08:00
|
|
|
F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2011-10-02 09:16:38 +08: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.
|
2012-03-11 15:00:24 +08:00
|
|
|
static bool hasUnwindExceptions(const LangOptions &LangOpts) {
|
2011-10-02 09:16:38 +08:00
|
|
|
// If exceptions are completely disabled, obviously this is false.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!LangOpts.Exceptions) return false;
|
2011-10-02 09:16:38 +08:00
|
|
|
|
|
|
|
// If C++ exceptions are enabled, this is true.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.CXXExceptions) return true;
|
2011-10-02 09:16:38 +08:00
|
|
|
|
|
|
|
// If ObjC exceptions are enabled, this depends on the ABI.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.ObjCExceptions) {
|
2012-07-04 04:49:52 +08:00
|
|
|
return LangOpts.ObjCRuntime.hasUnwindExceptions();
|
2011-10-02 09:16:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
|
|
|
|
llvm::Function *F) {
|
2013-05-11 20:45:37 +08:00
|
|
|
llvm::AttrBuilder B;
|
|
|
|
|
2011-05-25 11:44:55 +08:00
|
|
|
if (CodeGenOpts.UnwindTables)
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::UWTable);
|
2011-05-25 11:44:55 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!hasUnwindExceptions(LangOpts))
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::NoUnwind);
|
2008-10-28 08:17:57 +08:00
|
|
|
|
2011-08-23 07:55:33 +08:00
|
|
|
if (D->hasAttr<NakedAttr>()) {
|
|
|
|
// Naked implies noinline: we should not be inlining such functions.
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::Naked);
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
|
|
|
} else if (D->hasAttr<NoInlineAttr>()) {
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
|
|
|
} else if ((D->hasAttr<AlwaysInlineAttr>() ||
|
|
|
|
D->hasAttr<ForceInlineAttr>()) &&
|
|
|
|
!F->getAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::Attribute::NoInline)) {
|
|
|
|
// (noinline wins over always_inline, and we can't specify both in IR)
|
|
|
|
B.addAttribute(llvm::Attribute::AlwaysInline);
|
2011-08-23 07:55:33 +08:00
|
|
|
}
|
2010-09-30 02:20:25 +08:00
|
|
|
|
2013-05-25 04:18:15 +08:00
|
|
|
if (D->hasAttr<ColdAttr>()) {
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::OptimizeForSize);
|
2013-05-25 04:18:15 +08:00
|
|
|
B.addAttribute(llvm::Attribute::Cold);
|
|
|
|
}
|
2012-05-13 05:10:52 +08:00
|
|
|
|
2012-11-02 07:55:47 +08:00
|
|
|
if (D->hasAttr<MinSizeAttr>())
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::MinSize);
|
2012-09-29 06:46:07 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.getStackProtector() == LangOptions::SSPOn)
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::StackProtect);
|
2012-03-11 15:00:24 +08:00
|
|
|
else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::StackProtectReq);
|
2013-01-18 19:30:38 +08:00
|
|
|
|
2013-03-06 18:54:18 +08:00
|
|
|
// Add sanitizer attributes if function is not blacklisted.
|
|
|
|
if (!SanitizerBlacklist.isIn(*F)) {
|
|
|
|
// When AddressSanitizer is enabled, set SanitizeAddress attribute
|
|
|
|
// unless __attribute__((no_sanitize_address)) is used.
|
|
|
|
if (SanOpts.Address && !D->hasAttr<NoSanitizeAddressAttr>())
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::SanitizeAddress);
|
2013-03-06 18:54:18 +08:00
|
|
|
// Same for ThreadSanitizer and __attribute__((no_sanitize_thread))
|
|
|
|
if (SanOpts.Thread && !D->hasAttr<NoSanitizeThreadAttr>()) {
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::SanitizeThread);
|
2013-03-06 18:54:18 +08:00
|
|
|
}
|
|
|
|
// Same for MemorySanitizer and __attribute__((no_sanitize_memory))
|
|
|
|
if (SanOpts.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::SanitizeMemory);
|
2013-03-06 18:54:18 +08:00
|
|
|
}
|
2012-02-02 19:49:28 +08:00
|
|
|
|
2013-05-11 20:45:37 +08:00
|
|
|
F->addAttributes(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::AttributeSet::get(
|
|
|
|
F->getContext(), llvm::AttributeSet::FunctionIndex, B));
|
|
|
|
|
|
|
|
if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
|
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D))
|
|
|
|
if (MD->isVirtual())
|
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
|
|
|
|
if (alignment)
|
|
|
|
F->setAlignment(alignment);
|
|
|
|
|
2009-10-06 06:49:20 +08:00
|
|
|
// C++ ABI requires 2-byte alignment for member functions.
|
2009-10-06 07:08:21 +08:00
|
|
|
if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
|
|
|
|
F->setAlignment(2);
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::SetCommonAttributes(const Decl *D,
|
2009-04-14 16:05:55 +08:00
|
|
|
llvm::GlobalValue *GV) {
|
2011-01-30 03:41:00 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
setGlobalVisibility(GV, ND);
|
2010-10-23 05:05:15 +08:00
|
|
|
else
|
|
|
|
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (D->hasAttr<UsedAttr>())
|
2009-04-14 16:05:55 +08:00
|
|
|
AddUsedGlobal(GV);
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
|
2009-04-14 16:05:55 +08:00
|
|
|
GV->setSection(SA->getName());
|
2010-01-10 20:58:08 +08:00
|
|
|
|
2013-03-19 23:03:47 +08:00
|
|
|
// Alias cannot have attributes. Filter them here.
|
|
|
|
if (!isa<llvm::GlobalAlias>(GV))
|
|
|
|
getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this);
|
2009-04-14 16:05:55 +08:00
|
|
|
}
|
|
|
|
|
2009-04-17 08:48:04 +08:00
|
|
|
void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
|
|
|
|
llvm::Function *F,
|
|
|
|
const CGFunctionInfo &FI) {
|
|
|
|
SetLLVMFunctionAttributes(D, FI, F);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, F);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
|
|
|
F->setLinkage(llvm::Function::InternalLinkage);
|
|
|
|
|
2009-04-17 08:48:04 +08:00
|
|
|
SetCommonAttributes(D, F);
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2010-02-06 10:44:09 +08:00
|
|
|
void CodeGenModule::SetFunctionAttributes(GlobalDecl GD,
|
2009-05-26 09:22:57 +08:00
|
|
|
llvm::Function *F,
|
|
|
|
bool IsIncompleteFunction) {
|
2011-04-06 20:29:04 +08:00
|
|
|
if (unsigned IID = F->getIntrinsicID()) {
|
|
|
|
// If this is an intrinsic function, set the function's attributes
|
|
|
|
// to the intrinsic's attributes.
|
2012-10-15 12:47:45 +08:00
|
|
|
F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(),
|
|
|
|
(llvm::Intrinsic::ID)IID));
|
2011-04-06 20:29:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-06 10:44:09 +08:00
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
|
|
|
|
|
2009-05-26 09:22:57 +08:00
|
|
|
if (!IsIncompleteFunction)
|
2012-02-17 11:33:10 +08:00
|
|
|
SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-07-01 04:40:16 +08:00
|
|
|
if (getCXXABI().HasThisReturn(GD)) {
|
|
|
|
assert(!F->arg_empty() &&
|
|
|
|
F->arg_begin()->getType()
|
|
|
|
->canLosslesslyBitCastTo(F->getReturnType()) &&
|
|
|
|
"unexpected this return");
|
|
|
|
F->addAttribute(1, llvm::Attribute::Returned);
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
// Only a few attributes are set on declarations; these may later be
|
|
|
|
// overridden by a definition.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (FD->hasAttr<DLLImportAttr>()) {
|
2009-04-14 16:05:55 +08:00
|
|
|
F->setLinkage(llvm::Function::DLLImportLinkage);
|
2009-09-09 23:08:12 +08: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.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
FD->isWeakImported()) {
|
2009-04-14 16:05:55 +08:00
|
|
|
// "extern_weak" is overloaded in LLVM; we probably should have
|
2009-09-09 23:08:12 +08:00
|
|
|
// separate linkage types for this.
|
2009-04-14 16:05:55 +08:00
|
|
|
F->setLinkage(llvm::Function::ExternalWeakLinkage);
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
F->setLinkage(llvm::Function::ExternalLinkage);
|
2010-10-30 19:50:40 +08:00
|
|
|
|
2013-02-27 10:15:29 +08:00
|
|
|
LinkageInfo LV = FD->getLinkageAndVisibility();
|
2013-02-27 10:56:45 +08:00
|
|
|
if (LV.getLinkage() == ExternalLinkage && LV.isVisibilityExplicit()) {
|
|
|
|
F->setVisibility(GetLLVMVisibility(LV.getVisibility()));
|
2010-10-30 19:50:40 +08:00
|
|
|
}
|
2009-04-14 16:05:55 +08:00
|
|
|
}
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
|
2009-04-14 16:05:55 +08:00
|
|
|
F->setSection(SA->getName());
|
2013-07-22 07:12:18 +08:00
|
|
|
|
|
|
|
// A replaceable global allocation function does not act like a builtin by
|
|
|
|
// default, only if it is invoked by a new-expression or delete-expression.
|
|
|
|
if (FD->isReplaceableGlobalAllocationFunction())
|
|
|
|
F->addAttribute(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::Attribute::NoBuiltin);
|
2008-09-09 07:44:31 +08:00
|
|
|
}
|
|
|
|
|
2009-02-14 04:29:50 +08:00
|
|
|
void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!GV->isDeclaration() &&
|
2009-02-14 04:29:50 +08:00
|
|
|
"Only globals with definition can force usage.");
|
2009-04-01 06:37:52 +08:00
|
|
|
LLVMUsed.push_back(GV);
|
2009-02-14 04:29:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitLLVMUsed() {
|
|
|
|
// Don't create llvm.used if there is no need.
|
2009-07-18 07:57:13 +08:00
|
|
|
if (LLVMUsed.empty())
|
2009-02-14 04:29:50 +08:00
|
|
|
return;
|
|
|
|
|
2009-04-01 06:37:52 +08:00
|
|
|
// Convert LLVMUsed to what ConstantArray needs.
|
2012-02-07 06:16:34 +08:00
|
|
|
SmallVector<llvm::Constant*, 8> UsedArray;
|
2009-04-01 06:37:52 +08:00
|
|
|
UsedArray.resize(LLVMUsed.size());
|
|
|
|
for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
|
2009-09-09 23:08:12 +08:00
|
|
|
UsedArray[i] =
|
|
|
|
llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]),
|
2012-02-07 06:16:34 +08:00
|
|
|
Int8PtrTy);
|
2009-04-01 06:37:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 05:47:46 +08:00
|
|
|
if (UsedArray.empty())
|
|
|
|
return;
|
2012-02-07 06:16:34 +08:00
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), ATy, false,
|
2009-02-14 04:29:50 +08:00
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
2009-07-29 02:33:04 +08:00
|
|
|
llvm::ConstantArray::get(ATy, UsedArray),
|
2009-07-09 03:05:04 +08:00
|
|
|
"llvm.used");
|
2009-02-14 04:29:50 +08:00
|
|
|
|
|
|
|
GV->setSection("llvm.metadata");
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:44:39 +08:00
|
|
|
void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
|
|
|
|
llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
|
|
|
|
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
|
|
|
|
}
|
|
|
|
|
2013-06-04 10:07:14 +08:00
|
|
|
void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
|
|
|
|
llvm::SmallString<32> Opt;
|
|
|
|
getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
|
|
|
|
llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
|
|
|
|
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:44:39 +08:00
|
|
|
void CodeGenModule::AddDependentLib(StringRef Lib) {
|
|
|
|
llvm::SmallString<24> Opt;
|
|
|
|
getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
|
|
|
|
llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
|
|
|
|
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
|
|
|
|
}
|
|
|
|
|
2013-01-15 04:53:57 +08:00
|
|
|
/// \brief Add link options implied by the given module, including modules
|
|
|
|
/// it depends on, using a postorder walk.
|
2013-05-08 21:44:39 +08:00
|
|
|
static void addLinkOptionsPostorder(CodeGenModule &CGM,
|
2013-01-15 04:53:57 +08:00
|
|
|
Module *Mod,
|
2013-01-17 09:35:06 +08:00
|
|
|
SmallVectorImpl<llvm::Value *> &Metadata,
|
2013-01-15 04:53:57 +08:00
|
|
|
llvm::SmallPtrSet<Module *, 16> &Visited) {
|
|
|
|
// Import this module's parent.
|
|
|
|
if (Mod->Parent && Visited.insert(Mod->Parent)) {
|
2013-05-08 21:44:39 +08:00
|
|
|
addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Import this module's dependencies.
|
|
|
|
for (unsigned I = Mod->Imports.size(); I > 0; --I) {
|
|
|
|
if (Visited.insert(Mod->Imports[I-1]))
|
2013-05-08 21:44:39 +08:00
|
|
|
addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add linker options to link against the libraries/frameworks
|
|
|
|
// described by this module.
|
2013-05-08 21:44:39 +08:00
|
|
|
llvm::LLVMContext &Context = CGM.getLLVMContext();
|
2013-01-15 04:53:57 +08:00
|
|
|
for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
|
2013-05-08 21:44:39 +08:00
|
|
|
// Link against a framework. Frameworks are currently Darwin only, so we
|
|
|
|
// don't to ask TargetCodeGenInfo for the spelling of the linker option.
|
2013-01-15 04:53:57 +08:00
|
|
|
if (Mod->LinkLibraries[I-1].IsFramework) {
|
|
|
|
llvm::Value *Args[2] = {
|
|
|
|
llvm::MDString::get(Context, "-framework"),
|
|
|
|
llvm::MDString::get(Context, Mod->LinkLibraries[I-1].Library)
|
|
|
|
};
|
|
|
|
|
|
|
|
Metadata.push_back(llvm::MDNode::get(Context, Args));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link against a library.
|
2013-05-08 21:44:39 +08:00
|
|
|
llvm::SmallString<24> Opt;
|
|
|
|
CGM.getTargetCodeGenInfo().getDependentLibraryOption(
|
|
|
|
Mod->LinkLibraries[I-1].Library, Opt);
|
|
|
|
llvm::Value *OptString = llvm::MDString::get(Context, Opt);
|
2013-01-15 04:53:57 +08:00
|
|
|
Metadata.push_back(llvm::MDNode::get(Context, OptString));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitModuleLinkOptions() {
|
|
|
|
// Collect the set of all of the modules we want to visit to emit link
|
|
|
|
// options, which is essentially the imported modules and all of their
|
|
|
|
// non-explicit child modules.
|
|
|
|
llvm::SetVector<clang::Module *> LinkModules;
|
|
|
|
llvm::SmallPtrSet<clang::Module *, 16> Visited;
|
|
|
|
SmallVector<clang::Module *, 16> Stack;
|
|
|
|
|
|
|
|
// Seed the stack with imported modules.
|
|
|
|
for (llvm::SetVector<clang::Module *>::iterator M = ImportedModules.begin(),
|
|
|
|
MEnd = ImportedModules.end();
|
|
|
|
M != MEnd; ++M) {
|
|
|
|
if (Visited.insert(*M))
|
|
|
|
Stack.push_back(*M);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all of the modules to import, making a little effort to prune
|
|
|
|
// non-leaf modules.
|
|
|
|
while (!Stack.empty()) {
|
|
|
|
clang::Module *Mod = Stack.back();
|
|
|
|
Stack.pop_back();
|
|
|
|
|
|
|
|
bool AnyChildren = false;
|
|
|
|
|
|
|
|
// Visit the submodules of this module.
|
|
|
|
for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
|
|
|
|
SubEnd = Mod->submodule_end();
|
|
|
|
Sub != SubEnd; ++Sub) {
|
|
|
|
// Skip explicit children; they need to be explicitly imported to be
|
|
|
|
// linked against.
|
|
|
|
if ((*Sub)->IsExplicit)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Visited.insert(*Sub)) {
|
|
|
|
Stack.push_back(*Sub);
|
|
|
|
AnyChildren = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find any children, so add this module to the list of
|
|
|
|
// modules to link against.
|
|
|
|
if (!AnyChildren) {
|
|
|
|
LinkModules.insert(Mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add link options for all of the imported modules in reverse topological
|
2013-05-08 21:44:39 +08:00
|
|
|
// order. We don't do anything to try to order import link flags with respect
|
|
|
|
// to linker options inserted by things like #pragma comment().
|
2013-01-17 09:35:06 +08:00
|
|
|
SmallVector<llvm::Value *, 16> MetadataArgs;
|
2013-01-15 04:53:57 +08:00
|
|
|
Visited.clear();
|
|
|
|
for (llvm::SetVector<clang::Module *>::iterator M = LinkModules.begin(),
|
|
|
|
MEnd = LinkModules.end();
|
|
|
|
M != MEnd; ++M) {
|
|
|
|
if (Visited.insert(*M))
|
2013-05-08 21:44:39 +08:00
|
|
|
addLinkOptionsPostorder(*this, *M, MetadataArgs, Visited);
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
2013-01-17 09:35:06 +08:00
|
|
|
std::reverse(MetadataArgs.begin(), MetadataArgs.end());
|
2013-05-08 21:44:39 +08:00
|
|
|
LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
|
2013-01-15 04:53:57 +08:00
|
|
|
|
2013-01-17 09:35:06 +08:00
|
|
|
// Add the linker options metadata flag.
|
|
|
|
getModule().addModuleFlag(llvm::Module::AppendUnique, "Linker Options",
|
2013-05-08 21:44:39 +08:00
|
|
|
llvm::MDNode::get(getLLVMContext(),
|
|
|
|
LinkerOptionsMetadata));
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
|
|
|
|
2009-02-14 04:29:50 +08:00
|
|
|
void CodeGenModule::EmitDeferred() {
|
2009-03-21 17:44:56 +08: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 13:26:13 +08:00
|
|
|
// for a static function, iterate until no changes are made.
|
2010-03-10 10:19:29 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
while (true) {
|
2010-04-18 04:15:18 +08:00
|
|
|
if (!DeferredVTables.empty()) {
|
2013-01-26 06:31:03 +08:00
|
|
|
EmitDeferredVTables();
|
|
|
|
|
|
|
|
// Emitting a v-table doesn't directly cause more v-tables to
|
|
|
|
// become deferred, although it can cause functions to be
|
|
|
|
// emitted that then need those v-tables.
|
|
|
|
assert(DeferredVTables.empty());
|
2010-03-10 10:19:29 +08:00
|
|
|
}
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
// Stop if we're out of both deferred v-tables and deferred declarations.
|
|
|
|
if (DeferredDeclsToEmit.empty()) break;
|
|
|
|
|
2009-05-05 12:44:02 +08:00
|
|
|
GlobalDecl D = DeferredDeclsToEmit.back();
|
2009-03-21 17:44:56 +08:00
|
|
|
DeferredDeclsToEmit.pop_back();
|
|
|
|
|
2010-05-27 09:45:30 +08: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 18:55:15 +08:00
|
|
|
StringRef Name = getMangledName(D);
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *CGRef = GetGlobalValue(Name);
|
2009-03-21 17:44:56 +08:00
|
|
|
assert(CGRef && "Deferred decl wasn't referenced?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:44:56 +08:00
|
|
|
if (!CGRef->isDeclaration())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-27 09:45:30 +08: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 17:44:56 +08:00
|
|
|
// Otherwise, emit the definition and move on to the next one.
|
|
|
|
EmitGlobalDefinition(D);
|
|
|
|
}
|
2007-05-28 09:07:47 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2011-09-10 06:41:49 +08: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);
|
|
|
|
}
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
|
2011-09-10 06:41:49 +08:00
|
|
|
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 10:30:40 +08:00
|
|
|
llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
|
2011-09-10 06:41:49 +08: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 23:08:12 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
|
2008-04-19 12:17:09 +08:00
|
|
|
const AnnotateAttr *AA,
|
2011-09-10 06:41:49 +08: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 12:17:09 +08:00
|
|
|
|
2009-04-15 06:41:13 +08:00
|
|
|
// Create the ConstantStruct for the global annotation.
|
2008-04-19 12:17:09 +08:00
|
|
|
llvm::Constant *Fields[4] = {
|
2011-09-10 06:41:49 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
|
|
|
|
LineNoCst
|
2008-04-19 12:17:09 +08:00
|
|
|
};
|
2011-06-20 12:01:35 +08:00
|
|
|
return llvm::ConstantStruct::getAnon(Fields);
|
2008-04-19 12:17:09 +08:00
|
|
|
}
|
|
|
|
|
2011-09-10 06:41:49 +08: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 06:37:14 +08:00
|
|
|
bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
|
2010-07-30 02:15:58 +08:00
|
|
|
// Never defer when EmitAllDecls is specified.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.EmitAllDecls)
|
2010-07-28 06:37:14 +08: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.
llvm-svn: 95216
2010-02-03 17:33:45 +08:00
|
|
|
|
2010-07-30 04:08:05 +08:00
|
|
|
return !getContext().DeclMustBeEmitted(Global);
|
2009-02-14 05:18:01 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 18:13:44 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor(
|
|
|
|
const CXXUuidofExpr* E) {
|
|
|
|
// Sema has verified that IIDSource has a __declspec(uuid()), and that its
|
|
|
|
// well-formed.
|
|
|
|
StringRef Uuid;
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
Uuid = CXXUuidofExpr::GetUuidAttrOfType(E->getTypeOperand())->getGuid();
|
|
|
|
else {
|
|
|
|
// Special case: __uuidof(0) means an all-zero GUID.
|
|
|
|
Expr *Op = E->getExprOperand();
|
|
|
|
if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
|
|
|
|
Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
|
|
|
|
else
|
|
|
|
Uuid = "00000000-0000-0000-0000-000000000000";
|
|
|
|
}
|
|
|
|
std::string Name = "__uuid_" + Uuid.str();
|
|
|
|
|
|
|
|
// Look for an existing global.
|
|
|
|
if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
|
|
|
|
return GV;
|
|
|
|
|
|
|
|
llvm::Constant *Init = EmitUuidofInitializer(Uuid, E->getType());
|
|
|
|
assert(Init && "failed to initialize as constant");
|
|
|
|
|
|
|
|
// GUIDs are assumed to be 16 bytes, spread over 4-2-2-8 bytes. However, the
|
|
|
|
// first field is declared as "long", which for many targets is 8 bytes.
|
|
|
|
// Those architectures are not supported. (With the MS abi, long is always 4
|
|
|
|
// bytes.)
|
|
|
|
llvm::Type *GuidType = getTypes().ConvertType(E->getType());
|
|
|
|
if (Init->getType() != GuidType) {
|
|
|
|
DiagnosticsEngine &Diags = getDiags();
|
|
|
|
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
|
|
|
|
"__uuidof codegen is not supported on this architecture");
|
|
|
|
Diags.Report(E->getExprLoc(), DiagID) << E->getSourceRange();
|
|
|
|
Init = llvm::UndefValue::get(GuidType);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *GV = new llvm::GlobalVariable(getModule(), GuidType,
|
|
|
|
/*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, Init, Name);
|
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2010-03-05 02:17:24 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
|
|
|
|
const AliasAttr *AA = VD->getAttr<AliasAttr>();
|
|
|
|
assert(AA && "No alias?");
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
|
2010-03-05 02:17:24 +08:00
|
|
|
|
|
|
|
// See if there is already something with the target's name in the module.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
|
2012-10-17 01:45:27 +08:00
|
|
|
if (Entry) {
|
|
|
|
unsigned AS = getContext().getTargetAddressSpace(VD->getType());
|
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
|
|
|
|
}
|
2010-03-05 02:17:24 +08:00
|
|
|
|
|
|
|
llvm::Constant *Aliasee;
|
|
|
|
if (isa<llvm::FunctionType>(DeclTy))
|
2012-10-06 07:12:53 +08:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
|
|
|
|
GlobalDecl(cast<FunctionDecl>(VD)),
|
2011-02-05 12:35:53 +08:00
|
|
|
/*ForVTable=*/false);
|
2010-03-05 02:17:24 +08:00
|
|
|
else
|
2010-03-20 07:29:14 +08:00
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
|
2010-03-05 02:17:24 +08:00
|
|
|
llvm::PointerType::getUnqual(DeclTy), 0);
|
2012-10-17 01:45:27 +08:00
|
|
|
|
|
|
|
llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee);
|
|
|
|
F->setLinkage(llvm::Function::ExternalWeakLinkage);
|
|
|
|
WeakRefReferences.insert(F);
|
2010-03-05 02:17:24 +08:00
|
|
|
|
|
|
|
return Aliasee;
|
|
|
|
}
|
|
|
|
|
2009-05-13 05:21:08 +08:00
|
|
|
void CodeGenModule::EmitGlobal(GlobalDecl GD) {
|
2009-09-11 07:38:47 +08:00
|
|
|
const ValueDecl *Global = cast<ValueDecl>(GD.getDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-05 02:17:24 +08:00
|
|
|
// Weak references don't produce any output by themselves.
|
|
|
|
if (Global->hasAttr<WeakRefAttr>())
|
|
|
|
return;
|
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
// If this is an alias definition (which otherwise looks like a declaration)
|
|
|
|
// emit it now.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (Global->hasAttr<AliasAttr>())
|
2010-03-20 07:29:14 +08:00
|
|
|
return EmitAliasDefinition(GD);
|
2008-09-09 07:44:31 +08:00
|
|
|
|
2011-10-07 02:29:46 +08:00
|
|
|
// If this is CUDA, be selective about which declarations we emit.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.CUDA) {
|
2011-10-07 02:29:46 +08:00
|
|
|
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 17:44:56 +08:00
|
|
|
// Ignore declarations, they will be emitted on their first use.
|
2009-03-19 16:27:24 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
|
2009-02-14 05:18:01 +08:00
|
|
|
// Forward declarations are emitted lazily on first use.
|
2011-07-18 13:26:13 +08:00
|
|
|
if (!FD->doesThisDeclarationHaveABody()) {
|
|
|
|
if (!FD->doesDeclarationForceExternallyVisibleDefinition())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const FunctionDecl *InlineDefinition = 0;
|
|
|
|
FD->getBody(InlineDefinition);
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2012-03-25 02:22:12 +08:00
|
|
|
DeferredDecls.erase(MangledName);
|
2011-07-18 13:26:13 +08:00
|
|
|
EmitGlobalDefinition(InlineDefinition);
|
2009-02-14 05:18:01 +08:00
|
|
|
return;
|
2011-07-18 13:26:13 +08:00
|
|
|
}
|
2009-02-14 04:29:50 +08:00
|
|
|
} else {
|
|
|
|
const VarDecl *VD = cast<VarDecl>(Global);
|
2008-07-30 07:18:29 +08:00
|
|
|
assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
|
|
|
|
|
2010-02-06 13:15:45 +08:00
|
|
|
if (VD->isThisDeclarationADefinition() != VarDecl::Definition)
|
2009-02-14 05:18:01 +08:00
|
|
|
return;
|
2008-04-20 14:29:50 +08:00
|
|
|
}
|
|
|
|
|
2009-03-21 17:44:56 +08: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-14 01:39:09 +08:00
|
|
|
if (!MayDeferGeneration(Global)) {
|
|
|
|
// Emit the definition if it can't be deferred.
|
|
|
|
EmitGlobalDefinition(GD);
|
2008-07-30 07:18:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-07-16 07:40:35 +08:00
|
|
|
|
|
|
|
// If we're deferring emission of a C++ variable with an
|
|
|
|
// initializer, remember the order in which it appeared in the file.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
|
2010-07-16 07:40:35 +08:00
|
|
|
cast<VarDecl>(Global)->hasInit()) {
|
|
|
|
DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
|
|
|
|
CXXGlobalInits.push_back(0);
|
|
|
|
}
|
2010-04-14 01:39:09 +08:00
|
|
|
|
|
|
|
// If the value has already been used, add it directly to the
|
|
|
|
// DeferredDeclsToEmit list.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2010-04-14 01:39:09 +08: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 14:29:50 +08:00
|
|
|
}
|
|
|
|
|
2011-10-27 04:41:06 +08:00
|
|
|
namespace {
|
|
|
|
struct FunctionIsDirectlyRecursive :
|
|
|
|
public RecursiveASTVisitor<FunctionIsDirectlyRecursive> {
|
|
|
|
const StringRef Name;
|
2011-12-19 22:41:01 +08:00
|
|
|
const Builtin::Context &BI;
|
2011-10-27 04:41:06 +08:00
|
|
|
bool Result;
|
2011-12-19 22:41:01 +08:00
|
|
|
FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) :
|
|
|
|
Name(N), BI(C), Result(false) {
|
2011-10-27 04:41:06 +08:00
|
|
|
}
|
|
|
|
typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base;
|
|
|
|
|
|
|
|
bool TraverseCallExpr(CallExpr *E) {
|
2011-12-19 22:41:01 +08:00
|
|
|
const FunctionDecl *FD = E->getDirectCallee();
|
|
|
|
if (!FD)
|
2011-10-27 04:41:06 +08:00
|
|
|
return true;
|
2011-12-19 22:41:01 +08:00
|
|
|
AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
|
|
|
|
if (Attr && Name == Attr->getLabel()) {
|
|
|
|
Result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned BuiltinID = FD->getBuiltinID();
|
|
|
|
if (!BuiltinID)
|
2011-10-27 04:41:06 +08:00
|
|
|
return true;
|
2012-01-18 11:41:19 +08:00
|
|
|
StringRef BuiltinName = BI.GetName(BuiltinID);
|
|
|
|
if (BuiltinName.startswith("__builtin_") &&
|
|
|
|
Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
|
2011-10-27 04:41:06 +08:00
|
|
|
Result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-19 22:41:01 +08: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 04:41:06 +08:00
|
|
|
bool
|
2011-12-19 22:41:01 +08:00
|
|
|
CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
|
|
|
|
StringRef Name;
|
|
|
|
if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
|
2012-01-18 09:50:13 +08:00
|
|
|
// asm labels are a special kind of mangling we have to support.
|
2011-12-19 22:41:01 +08:00
|
|
|
AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
|
|
|
|
if (!Attr)
|
|
|
|
return false;
|
|
|
|
Name = Attr->getLabel();
|
|
|
|
} else {
|
|
|
|
Name = FD->getName();
|
|
|
|
}
|
2011-10-27 04:41:06 +08:00
|
|
|
|
2011-12-19 22:41:01 +08:00
|
|
|
FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
|
|
|
|
Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD));
|
2011-10-27 04:41:06 +08:00
|
|
|
return Walker.Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-06-06 01:49:37 +08:00
|
|
|
CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
|
|
|
|
if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
|
2011-10-27 04:41:06 +08:00
|
|
|
return true;
|
2013-06-06 01:49:37 +08:00
|
|
|
const FunctionDecl *F = cast<FunctionDecl>(GD.getDecl());
|
2011-10-29 04:43:56 +08:00
|
|
|
if (CodeGenOpts.OptimizationLevel == 0 &&
|
2012-06-18 15:00:48 +08:00
|
|
|
!F->hasAttr<AlwaysInlineAttr>() && !F->hasAttr<ForceInlineAttr>())
|
2011-10-27 04:41:06 +08: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 22:41:01 +08:00
|
|
|
return !isTriviallyRecursive(F);
|
2011-10-27 04:41:06 +08:00
|
|
|
}
|
|
|
|
|
2013-05-10 07:16:27 +08:00
|
|
|
/// If the type for the method's class was generated by
|
|
|
|
/// CGDebugInfo::createContextChain(), the cache contains only a
|
|
|
|
/// limited DIType without any declarations. Since EmitFunctionStart()
|
|
|
|
/// needs to find the canonical declaration for each method, we need
|
|
|
|
/// to construct the complete type prior to emitting the method.
|
|
|
|
void CodeGenModule::CompleteDIClassType(const CXXMethodDecl* D) {
|
|
|
|
if (!D->isInstance())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
|
|
|
|
const PointerType *ThisPtr =
|
|
|
|
cast<PointerType>(D->getThisType(getContext()));
|
|
|
|
DI->getOrCreateRecordType(ThisPtr->getPointeeType(), D->getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 05:21:08 +08:00
|
|
|
void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) {
|
2009-09-11 07:38:47 +08:00
|
|
|
const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-20 00:39:44 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
|
2009-10-27 22:32:27 +08:00
|
|
|
Context.getSourceManager(),
|
|
|
|
"Generating code for declaration");
|
|
|
|
|
2013-06-06 01:49:37 +08:00
|
|
|
if (isa<FunctionDecl>(D)) {
|
2010-07-13 14:02:28 +08:00
|
|
|
// At -O0, don't generate IR for functions with available_externally
|
|
|
|
// linkage.
|
2013-06-06 01:49:37 +08:00
|
|
|
if (!shouldEmitFunction(GD))
|
2010-07-13 14:02:28 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
|
2013-05-10 07:16:27 +08:00
|
|
|
CompleteDIClassType(Method);
|
2011-05-07 01:27:27 +08: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 14:02:28 +08:00
|
|
|
if (Method->isVirtual())
|
|
|
|
getVTables().EmitThunks(GD);
|
2010-03-23 12:31:31 +08:00
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
return;
|
2010-07-13 14:02:28 +08:00
|
|
|
}
|
2010-04-14 01:57:11 +08:00
|
|
|
|
|
|
|
return EmitGlobalFunctionDefinition(GD);
|
2010-07-13 14:02:28 +08:00
|
|
|
}
|
2010-04-14 01:57:11 +08:00
|
|
|
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
|
|
|
return EmitGlobalVarDefinition(VD);
|
2010-04-14 01:39:09 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08: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 07:29:14 +08:00
|
|
|
llvm::Constant *
|
2011-07-23 18:55:15 +08:00
|
|
|
CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty,
|
2013-07-22 21:07:10 +08:00
|
|
|
GlobalDecl GD, bool ForVTable,
|
2013-01-31 08:30:05 +08:00
|
|
|
llvm::AttributeSet ExtraAttrs) {
|
2013-07-22 21:07:10 +08:00
|
|
|
const Decl *D = GD.getDecl();
|
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2009-03-21 17:25:43 +08:00
|
|
|
if (Entry) {
|
2012-08-22 23:37:55 +08:00
|
|
|
if (WeakRefReferences.erase(Entry)) {
|
2013-07-22 21:07:10 +08:00
|
|
|
const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
|
2010-03-05 02:17:24 +08:00
|
|
|
if (FD && !FD->hasAttr<WeakAttr>())
|
2010-03-23 12:31:31 +08:00
|
|
|
Entry->setLinkage(llvm::Function::ExternalLinkage);
|
2010-03-05 02:17:24 +08:00
|
|
|
}
|
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
if (Entry->getType()->getElementType() == Ty)
|
|
|
|
return Entry;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
// Make sure the result is of the correct type.
|
2011-07-10 01:41:47 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
|
2009-03-21 17:25:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
// All MSVC dtors other than the base dtor are linkonce_odr and delegate to
|
|
|
|
// each other bottoming out with the base dtor. Therefore we emit non-base
|
|
|
|
// dtors on usage, even if there is no dtor definition in the TU.
|
|
|
|
if (D && isa<CXXDestructorDecl>(D) &&
|
|
|
|
getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
|
|
|
|
GD.getDtorType()))
|
|
|
|
DeferredDeclsToEmit.push_back(GD);
|
|
|
|
|
2009-11-09 13:07:37 +08: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 08:00:30 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy;
|
2010-04-28 08:00:30 +08:00
|
|
|
if (isa<llvm::FunctionType>(Ty)) {
|
|
|
|
FTy = cast<llvm::FunctionType>(Ty);
|
|
|
|
} else {
|
2011-05-15 09:53:33 +08:00
|
|
|
FTy = llvm::FunctionType::get(VoidTy, false);
|
2009-11-09 13:07:37 +08:00
|
|
|
IsIncompleteFunction = true;
|
|
|
|
}
|
2010-07-01 03:14:05 +08:00
|
|
|
|
2010-04-28 08:00:30 +08:00
|
|
|
llvm::Function *F = llvm::Function::Create(FTy,
|
2009-11-09 13:07:37 +08:00
|
|
|
llvm::Function::ExternalLinkage,
|
2010-03-20 07:29:14 +08:00
|
|
|
MangledName, &getModule());
|
|
|
|
assert(F->getName() == MangledName && "name was uniqued!");
|
2013-07-22 21:07:10 +08:00
|
|
|
if (D)
|
|
|
|
SetFunctionAttributes(GD, F, IsIncompleteFunction);
|
2013-01-31 08:30:05 +08:00
|
|
|
if (ExtraAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex)) {
|
|
|
|
llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeSet::FunctionIndex);
|
2013-01-23 08:21:06 +08:00
|
|
|
F->addAttributes(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::AttributeSet::get(VMContext,
|
|
|
|
llvm::AttributeSet::FunctionIndex,
|
|
|
|
B));
|
|
|
|
}
|
2009-11-09 13:07:37 +08:00
|
|
|
|
2009-03-21 17:44:56 +08: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 07:29:14 +08:00
|
|
|
llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
|
2009-03-21 17:44:56 +08: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 12:00:32 +08: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 12:35:53 +08: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.
|
2013-07-22 21:07:10 +08:00
|
|
|
} else if (getLangOpts().CPlusPlus && D) {
|
2010-12-15 12:00:32 +08:00
|
|
|
// Look for a declaration that's lexically in a record.
|
2013-07-22 21:07:10 +08:00
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(D);
|
2012-07-03 05:05:30 +08:00
|
|
|
FD = FD->getMostRecentDecl();
|
2010-12-15 12:00:32 +08:00
|
|
|
do {
|
|
|
|
if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
|
2011-02-05 12:35:53 +08:00
|
|
|
if (FD->isImplicit() && !ForVTable) {
|
2010-12-15 12:00:32 +08:00
|
|
|
assert(FD->isUsed() && "Sema didn't mark implicit function as used!");
|
2013-07-22 21:07:10 +08:00
|
|
|
DeferredDeclsToEmit.push_back(GD.getWithDecl(FD));
|
2010-12-15 12:00:32 +08:00
|
|
|
break;
|
2011-05-07 04:44:56 +08:00
|
|
|
} else if (FD->doesThisDeclarationHaveABody()) {
|
2013-07-22 21:07:10 +08:00
|
|
|
DeferredDeclsToEmit.push_back(GD.getWithDecl(FD));
|
2010-12-15 12:00:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-03-03 05:28:26 +08:00
|
|
|
}
|
2012-01-15 00:38:05 +08:00
|
|
|
FD = FD->getPreviousDecl();
|
2010-12-15 12:00:32 +08:00
|
|
|
} while (FD);
|
2009-03-21 17:44:56 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-28 08:00:30 +08:00
|
|
|
// Make sure the result is of the requested type.
|
|
|
|
if (!IsIncompleteFunction) {
|
|
|
|
assert(F->getType()->getElementType() == Ty);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2010-04-28 08:00:30 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(F, PTy);
|
2009-03-21 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08: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 05:21:08 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty,
|
2011-02-05 12:35:53 +08:00
|
|
|
bool ForVTable) {
|
2009-03-23 05:03:39 +08:00
|
|
|
// If there was no specific requested type, just convert it now.
|
|
|
|
if (!Ty)
|
2009-09-11 07:38:47 +08:00
|
|
|
Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
|
2010-07-01 03:14:05 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2011-02-05 12:35:53 +08:00
|
|
|
return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable);
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2008-05-31 03:50:47 +08:00
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// CreateRuntimeFunction - Create a new runtime function with the specified
|
|
|
|
/// type and name.
|
|
|
|
llvm::Constant *
|
2011-07-18 12:24:23 +08:00
|
|
|
CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name,
|
2013-01-31 08:30:05 +08:00
|
|
|
llvm::AttributeSet ExtraAttrs) {
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::Constant *C
|
|
|
|
= GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
|
|
|
|
ExtraAttrs);
|
|
|
|
if (llvm::Function *F = dyn_cast<llvm::Function>(C))
|
|
|
|
if (F->empty())
|
|
|
|
F->setCallingConv(getRuntimeCC());
|
|
|
|
return C;
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2012-02-17 14:48:11 +08:00
|
|
|
/// isTypeConstant - Determine whether an object of this type can be emitted
|
|
|
|
/// as a constant.
|
|
|
|
///
|
|
|
|
/// If ExcludeCtor is true, the duration when the object's constructor runs
|
|
|
|
/// will not be considered. The caller will need to verify that the object is
|
|
|
|
/// not written to during its construction.
|
|
|
|
bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
|
|
|
|
if (!Ty.isConstant(Context) && !Ty->isReferenceType())
|
2009-12-12 05:23:03 +08:00
|
|
|
return false;
|
2012-02-17 14:48:11 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Context.getLangOpts().CPlusPlus) {
|
2012-02-17 14:48:11 +08:00
|
|
|
if (const CXXRecordDecl *Record
|
|
|
|
= Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
|
|
|
|
return ExcludeCtor && !Record->hasMutableFields() &&
|
|
|
|
Record->hasTrivialDestructor();
|
2009-12-12 05:23:03 +08:00
|
|
|
}
|
2012-02-17 14:48:11 +08:00
|
|
|
|
2009-12-12 05:23:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08: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 07:29:14 +08:00
|
|
|
llvm::Constant *
|
2011-07-23 18:55:15 +08:00
|
|
|
CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *Ty,
|
2011-01-19 05:07:57 +08:00
|
|
|
const VarDecl *D,
|
|
|
|
bool UnnamedAddr) {
|
2008-08-06 07:31:02 +08:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2009-03-21 16:03:33 +08:00
|
|
|
if (Entry) {
|
2012-08-22 23:37:55 +08:00
|
|
|
if (WeakRefReferences.erase(Entry)) {
|
2010-03-05 02:17:24 +08:00
|
|
|
if (D && !D->hasAttr<WeakAttr>())
|
2010-03-23 12:31:31 +08:00
|
|
|
Entry->setLinkage(llvm::Function::ExternalLinkage);
|
2010-03-05 02:17:24 +08:00
|
|
|
}
|
|
|
|
|
2011-01-19 05:07:57 +08:00
|
|
|
if (UnnamedAddr)
|
|
|
|
Entry->setUnnamedAddr(true);
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
if (Entry->getType() == Ty)
|
2009-03-21 17:16:30 +08:00
|
|
|
return Entry;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 16:03:33 +08:00
|
|
|
// Make sure the result is of the correct type.
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, Ty);
|
2009-03-21 16:03:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:44:56 +08: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 07:29:14 +08:00
|
|
|
llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName);
|
2009-03-21 17:44:56 +08: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 23:08:12 +08:00
|
|
|
|
2012-05-21 05:08:35 +08:00
|
|
|
unsigned AddrSpace = GetGlobalVarAddressSpace(D, Ty->getAddressSpace());
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
|
2009-03-21 16:03:33 +08:00
|
|
|
llvm::GlobalValue::ExternalLinkage,
|
2010-03-20 07:29:14 +08:00
|
|
|
0, MangledName, 0,
|
2012-06-23 19:51:46 +08:00
|
|
|
llvm::GlobalVariable::NotThreadLocal, AddrSpace);
|
2009-01-13 10:25:00 +08:00
|
|
|
|
2009-03-21 16:03:33 +08:00
|
|
|
// Handle things which are present even on external declarations.
|
2009-03-23 05:03:39 +08:00
|
|
|
if (D) {
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: This code is overly simple and should be merged with other global
|
|
|
|
// handling.
|
2012-02-17 14:48:11 +08:00
|
|
|
GV->setConstant(isTypeConstant(D->getType(), false));
|
2009-01-13 10:25:00 +08:00
|
|
|
|
2010-10-30 06:22:43 +08:00
|
|
|
// Set linkage and visibility in case we never see a definition.
|
2013-02-27 10:15:29 +08:00
|
|
|
LinkageInfo LV = D->getLinkageAndVisibility();
|
2013-02-27 10:56:45 +08:00
|
|
|
if (LV.getLinkage() != ExternalLinkage) {
|
2011-02-19 10:53:41 +08:00
|
|
|
// Don't set internal linkage on declarations.
|
2010-10-30 06:22:43 +08: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.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
else if (D->hasAttr<WeakAttr>() || D->isWeakImported())
|
2010-10-30 06:22:43 +08:00
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
|
|
|
|
|
2010-10-30 19:50:40 +08:00
|
|
|
// Set visibility on a declaration only if it's explicit.
|
2013-02-27 10:56:45 +08:00
|
|
|
if (LV.isVisibilityExplicit())
|
|
|
|
GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
|
2010-10-30 06:22:43 +08:00
|
|
|
}
|
2009-04-20 05:05:03 +08:00
|
|
|
|
2013-04-20 00:42:07 +08:00
|
|
|
if (D->getTLSKind()) {
|
2013-04-22 16:06:17 +08:00
|
|
|
if (D->getTLSKind() == VarDecl::TLS_Dynamic)
|
|
|
|
CXXThreadLocals.push_back(std::make_pair(D, GV));
|
2012-06-28 16:01:44 +08:00
|
|
|
setTLSMode(GV, *D);
|
2013-04-20 00:42:07 +08:00
|
|
|
}
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-05-21 05:08:35 +08:00
|
|
|
if (AddrSpace != Ty->getAddressSpace())
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Ty);
|
|
|
|
else
|
|
|
|
return GV;
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-02-21 08:24:10 +08:00
|
|
|
|
|
|
|
|
2011-01-30 02:20:20 +08:00
|
|
|
llvm::GlobalVariable *
|
2011-07-23 18:55:15 +08:00
|
|
|
CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty,
|
2011-01-30 02:20:20 +08: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".
|
2012-10-11 18:13:44 +08:00
|
|
|
assert(GV->isDeclaration() && "Declaration has wrong type!");
|
2011-01-30 02:20:20 +08: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 05:03:39 +08: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,
|
2012-04-17 07:55:04 +08:00
|
|
|
/// then it will be created with the specified type instead of whatever the
|
2009-03-23 05:03:39 +08:00
|
|
|
/// normal requested type would be.
|
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty) {
|
2009-03-23 05:03:39 +08:00
|
|
|
assert(D->hasGlobalStorage() && "Not a global variable");
|
|
|
|
QualType ASTTy = D->getType();
|
|
|
|
if (Ty == 0)
|
|
|
|
Ty = getTypes().ConvertTypeForMem(ASTTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *PTy =
|
2011-03-19 06:38:29 +08:00
|
|
|
llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
|
2010-03-20 07:29:14 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(D);
|
2010-03-20 07:29:14 +08:00
|
|
|
return GetOrCreateLLVMGlobal(MangledName, PTy, D);
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-03-05 01:31:19 +08:00
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// CreateRuntimeVariable - Create a new runtime global variable with the
|
|
|
|
/// specified type and name.
|
|
|
|
llvm::Constant *
|
2011-07-18 12:24:23 +08:00
|
|
|
CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name) {
|
2011-04-07 16:22:57 +08:00
|
|
|
return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0,
|
2011-01-19 05:07:57 +08:00
|
|
|
true);
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2009-04-16 06:08:45 +08:00
|
|
|
void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
|
|
|
|
assert(!D->getInit() && "Cannot emit definite definitions here!");
|
|
|
|
|
2009-04-22 03:28:58 +08: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 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(D);
|
2010-03-20 07:29:14 +08:00
|
|
|
if (!GetGlobalValue(MangledName)) {
|
2009-09-11 07:43:36 +08:00
|
|
|
DeferredDecls[MangledName] = D;
|
2009-04-16 06:08:45 +08:00
|
|
|
return;
|
2009-04-22 03:28:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The tentative definition is the only definition.
|
2009-04-16 06:08:45 +08:00
|
|
|
EmitGlobalVarDefinition(D);
|
|
|
|
}
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
|
2011-01-18 10:01:14 +08:00
|
|
|
return Context.toCharUnitsFromBits(
|
2012-10-09 00:25:52 +08:00
|
|
|
TheDataLayout.getTypeStoreSizeInBits(Ty));
|
2010-01-26 21:48:07 +08:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:08:35 +08:00
|
|
|
unsigned CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D,
|
|
|
|
unsigned AddrSpace) {
|
|
|
|
if (LangOpts.CUDA && CodeGenOpts.CUDAIsDevice) {
|
|
|
|
if (D->hasAttr<CUDAConstantAttr>())
|
|
|
|
AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_constant);
|
|
|
|
else if (D->hasAttr<CUDASharedAttr>())
|
|
|
|
AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_shared);
|
|
|
|
else
|
|
|
|
AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddrSpace;
|
|
|
|
}
|
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
template<typename SomeDecl>
|
|
|
|
void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
|
|
|
|
llvm::GlobalValue *GV) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Must have 'used' attribute, or else inline assembly can't rely on
|
|
|
|
// the name existing.
|
|
|
|
if (!D->template hasAttr<UsedAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Must have internal linkage and an ordinary name.
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
|
2013-04-06 13:00:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Must be in an extern "C" context. Entities declared directly within
|
|
|
|
// a record are not extern "C" even if the record is in such a context.
|
2013-05-06 04:15:21 +08:00
|
|
|
const SomeDecl *First = D->getFirstDeclaration();
|
|
|
|
if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
|
2013-04-06 13:00:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// OK, this is an internal linkage entity inside an extern "C" linkage
|
|
|
|
// specification. Make a note of that so we can give it the "expected"
|
|
|
|
// mangled name if nothing else is using that name.
|
2013-04-06 15:07:44 +08:00
|
|
|
std::pair<StaticExternCMap::iterator, bool> R =
|
|
|
|
StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
|
2013-04-06 13:00:46 +08:00
|
|
|
|
|
|
|
// If we have multiple internal linkage entities with the same name
|
|
|
|
// in extern "C" regions, none of them gets that name.
|
2013-04-06 15:07:44 +08:00
|
|
|
if (!R.second)
|
|
|
|
R.first->second = 0;
|
2013-04-06 13:00:46 +08:00
|
|
|
}
|
|
|
|
|
2008-07-30 07:18:29 +08:00
|
|
|
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
|
2007-07-14 08:23:28 +08:00
|
|
|
llvm::Constant *Init = 0;
|
2008-05-31 03:50:47 +08:00
|
|
|
QualType ASTTy = D->getType();
|
2012-02-14 06:16:19 +08:00
|
|
|
CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
|
|
|
|
bool NeedsGlobalCtor = false;
|
|
|
|
bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
const VarDecl *InitDecl;
|
|
|
|
const Expr *InitExpr = D->getAnyInitializer(InitDecl);
|
2012-02-26 04:51:20 +08:00
|
|
|
|
2010-01-27 01:43:42 +08:00
|
|
|
if (!InitExpr) {
|
2008-05-31 04:39:54 +08:00
|
|
|
// This is a tentative definition; tentative definitions are
|
2009-04-16 06:08:45 +08: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 05:18:22 +08:00
|
|
|
Init = EmitNullConstant(D->getType());
|
2008-05-31 03:50:47 +08:00
|
|
|
} else {
|
2013-06-13 06:31:48 +08:00
|
|
|
initializedGlobalDecl = GlobalDecl(D);
|
|
|
|
Init = EmitConstantInit(*InitDecl);
|
2012-02-26 04:51:20 +08:00
|
|
|
|
2009-02-20 09:18:21 +08:00
|
|
|
if (!Init) {
|
2010-01-27 01:43:42 +08:00
|
|
|
QualType T = InitExpr->getType();
|
2010-05-06 04:15:55 +08:00
|
|
|
if (D->getType()->isReferenceType())
|
|
|
|
T = D->getType();
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus) {
|
2009-08-09 07:24:23 +08:00
|
|
|
Init = EmitNullConstant(T);
|
2012-02-14 06:16:19 +08:00
|
|
|
NeedsGlobalCtor = true;
|
2009-08-09 07:24:23 +08:00
|
|
|
} else {
|
|
|
|
ErrorUnsupported(D, "static initializer");
|
|
|
|
Init = llvm::UndefValue::get(getTypes().ConvertType(T));
|
|
|
|
}
|
2010-07-16 07:40:35 +08:00
|
|
|
} else {
|
|
|
|
// We don't need an initializer, so remove the entry for the delayed
|
2012-02-14 06:16:19 +08:00
|
|
|
// initializer position (just in case this entry was delayed) if we
|
|
|
|
// also don't need to register a destructor.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
|
2010-07-16 07:40:35 +08:00
|
|
|
DelayedCXXInitPosition.erase(D);
|
2009-02-20 09:18:21 +08:00
|
|
|
}
|
2007-07-14 08:23:28 +08:00
|
|
|
}
|
2007-10-27 00:31:40 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type* InitType = Init->getType();
|
2009-03-21 17:16:30 +08:00
|
|
|
llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// Strip off a bitcast if we got one back.
|
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
|
2009-07-17 00:48:25 +08:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast ||
|
|
|
|
// all zero index gep.
|
|
|
|
CE->getOpcode() == llvm::Instruction::GetElementPtr);
|
2009-03-21 17:16:30 +08:00
|
|
|
Entry = CE->getOperand(0);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// Entry is now either a Function or GlobalVariable.
|
|
|
|
llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08: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 06:38:29 +08:00
|
|
|
GV->getType()->getAddressSpace() !=
|
2012-05-21 05:08:35 +08:00
|
|
|
GetGlobalVarAddressSpace(D, getContext().getTargetAddressSpace(ASTTy))) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
// Move the old entry aside so that we'll create a new one.
|
2011-07-23 18:55:15 +08:00
|
|
|
Entry->setName(StringRef());
|
2009-02-19 13:36:41 +08:00
|
|
|
|
2009-03-21 17:16:30 +08: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 17:25:43 +08:00
|
|
|
|
2008-05-31 03:50:47 +08:00
|
|
|
// Replace all uses of the old global with the new global
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2009-07-30 02:54:39 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, Entry->getType());
|
2009-03-21 17:16:30 +08:00
|
|
|
Entry->replaceAllUsesWith(NewPtrForOldDecl);
|
2008-05-31 03:50:47 +08:00
|
|
|
|
|
|
|
// Erase the old global, since it is no longer used.
|
2009-03-21 17:16:30 +08:00
|
|
|
cast<llvm::GlobalValue>(Entry)->eraseFromParent();
|
2008-05-31 03:50:47 +08:00
|
|
|
}
|
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
MaybeHandleStaticInExternC(D, GV);
|
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
if (D->hasAttr<AnnotateAttr>())
|
|
|
|
AddGlobalAnnotations(D, GV);
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2007-07-13 13:13:43 +08:00
|
|
|
GV->setInitializer(Init);
|
2009-08-05 13:20:29 +08:00
|
|
|
|
|
|
|
// If it is safe to mark the global 'constant', do so now.
|
2012-02-17 14:48:11 +08:00
|
|
|
GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
|
|
|
|
isTypeConstant(D->getType(), true));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-28 01:10:57 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
|
2012-02-17 14:48:11 +08:00
|
|
|
|
2007-07-13 13:13:43 +08:00
|
|
|
// Set the llvm linkage type as appropriate.
|
2010-10-28 00:21:54 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
2013-07-02 04:22:57 +08:00
|
|
|
GetLLVMLinkageVarDefinition(D, GV->isConstant());
|
2010-10-28 00:21:54 +08:00
|
|
|
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 17:44:32 +08:00
|
|
|
// Emit the initializer function if necessary.
|
2012-02-14 06:16:19 +08:00
|
|
|
if (NeedsGlobalCtor || NeedsGlobalDtor)
|
|
|
|
EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
|
2010-11-06 17:44:32 +08:00
|
|
|
|
2012-08-21 14:53:28 +08:00
|
|
|
// If we are compiling with ASan, add metadata indicating dynamically
|
|
|
|
// initialized globals.
|
2013-01-18 19:30:38 +08:00
|
|
|
if (SanOpts.Address && NeedsGlobalCtor) {
|
2012-08-21 14:53:28 +08:00
|
|
|
llvm::Module &M = getModule();
|
|
|
|
|
|
|
|
llvm::NamedMDNode *DynamicInitializers =
|
|
|
|
M.getOrInsertNamedMetadata("llvm.asan.dynamically_initialized_globals");
|
|
|
|
llvm::Value *GlobalToAdd[] = { GV };
|
|
|
|
llvm::MDNode *ThisGlobal = llvm::MDNode::get(VMContext, GlobalToAdd);
|
|
|
|
DynamicInitializers->addOperand(ThisGlobal);
|
|
|
|
}
|
|
|
|
|
2010-10-28 00:21:54 +08:00
|
|
|
// Emit global variable debug information.
|
2011-10-14 05:45:18 +08:00
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
2012-10-24 04:05:01 +08:00
|
|
|
if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
|
2012-05-04 15:39:27 +08:00
|
|
|
DI->EmitGlobalVariable(GV, D);
|
2010-10-28 00:21:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalValue::LinkageTypes
|
2013-07-02 04:22:57 +08:00
|
|
|
CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D, bool isConstant) {
|
2010-07-30 02:15:58 +08:00
|
|
|
GVALinkage Linkage = getContext().GetGVALinkageForVariable(D);
|
2009-10-15 05:36:34 +08:00
|
|
|
if (Linkage == GVA_Internal)
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::Function::InternalLinkage;
|
2009-06-30 10:34:44 +08:00
|
|
|
else if (D->hasAttr<DLLImportAttr>())
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::Function::DLLImportLinkage;
|
2009-06-30 10:34:44 +08:00
|
|
|
else if (D->hasAttr<DLLExportAttr>())
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::Function::DLLExportLinkage;
|
2013-05-20 22:02:37 +08:00
|
|
|
else if (D->hasAttr<SelectAnyAttr>()) {
|
|
|
|
// selectany symbols are externally visible, so use weak instead of
|
|
|
|
// linkonce. MSVC optimizes away references to const selectany globals, so
|
|
|
|
// all definitions should be the same and ODR linkage should be used.
|
|
|
|
// http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
} else if (D->hasAttr<WeakAttr>()) {
|
2013-07-02 04:22:57 +08:00
|
|
|
if (isConstant)
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
2009-08-05 13:20:29 +08:00
|
|
|
else
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::WeakAnyLinkage;
|
2010-03-14 02:23:07 +08:00
|
|
|
} else if (Linkage == GVA_TemplateInstantiation ||
|
|
|
|
Linkage == GVA_ExplicitTemplateInstantiation)
|
2011-04-12 09:46:54 +08:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
2012-03-11 15:00:24 +08:00
|
|
|
else if (!getLangOpts().CPlusPlus &&
|
2010-12-02 10:45:55 +08:00
|
|
|
((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) ||
|
|
|
|
D->getAttr<CommonAttr>()) &&
|
2009-08-05 12:56:58 +08:00
|
|
|
!D->hasExternalStorage() && !D->getInit() &&
|
2013-04-13 10:43:54 +08:00
|
|
|
!D->getAttr<SectionAttr>() && !D->getTLSKind() &&
|
2011-06-21 01:50:03 +08:00
|
|
|
!D->getAttr<WeakImportAttr>()) {
|
2010-08-08 09:37:14 +08:00
|
|
|
// Thread local vars aren't considered common linkage.
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::CommonLinkage;
|
2013-05-03 03:18:03 +08:00
|
|
|
} else if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
|
|
|
|
getTarget().getTriple().isMacOSX())
|
|
|
|
// On Darwin, the backing variable for a C++11 thread_local variable always
|
|
|
|
// has internal linkage; all accesses should just be calls to the
|
|
|
|
// Itanium-specified entry point, which has the normal linkage of the
|
|
|
|
// variable.
|
|
|
|
return llvm::GlobalValue::InternalLinkage;
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
2007-07-13 13:13:43 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
/// Replace the uses of a function that was declared with a non-proto type.
|
|
|
|
/// We want to silently drop extra arguments from call sites
|
|
|
|
static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
|
|
|
|
llvm::Function *newFn) {
|
|
|
|
// Fast path.
|
|
|
|
if (old->use_empty()) return;
|
|
|
|
|
|
|
|
llvm::Type *newRetTy = newFn->getReturnType();
|
|
|
|
SmallVector<llvm::Value*, 4> newArgs;
|
|
|
|
|
|
|
|
for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
|
|
|
|
ui != ue; ) {
|
|
|
|
llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
|
|
|
|
llvm::User *user = *use;
|
|
|
|
|
|
|
|
// Recognize and replace uses of bitcasts. Most calls to
|
|
|
|
// unprototyped functions will use bitcasts.
|
|
|
|
if (llvm::ConstantExpr *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
|
|
|
|
if (bitcast->getOpcode() == llvm::Instruction::BitCast)
|
|
|
|
replaceUsesOfNonProtoConstant(bitcast, newFn);
|
2009-05-05 14:16:31 +08:00
|
|
|
continue;
|
2012-12-13 06:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recognize calls to the function.
|
|
|
|
llvm::CallSite callSite(user);
|
|
|
|
if (!callSite) continue;
|
|
|
|
if (!callSite.isCallee(use)) continue;
|
2009-05-05 14:16:31 +08:00
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
// If the return types don't match exactly, then we can't
|
|
|
|
// transform this call unless it's dead.
|
|
|
|
if (callSite->getType() != newRetTy && !callSite->use_empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the call site's attribute list.
|
2013-01-27 10:46:53 +08:00
|
|
|
SmallVector<llvm::AttributeSet, 8> newAttrs;
|
2012-12-13 06:21:47 +08:00
|
|
|
llvm::AttributeSet oldAttrs = callSite.getAttributes();
|
|
|
|
|
|
|
|
// Collect any return attributes from the call.
|
2013-01-22 05:57:40 +08:00
|
|
|
if (oldAttrs.hasAttributes(llvm::AttributeSet::ReturnIndex))
|
2013-01-22 06:45:00 +08:00
|
|
|
newAttrs.push_back(
|
2013-01-27 10:46:53 +08:00
|
|
|
llvm::AttributeSet::get(newFn->getContext(),
|
|
|
|
oldAttrs.getRetAttributes()));
|
2012-12-13 06:21:47 +08:00
|
|
|
|
|
|
|
// If the function was passed too few arguments, don't transform.
|
|
|
|
unsigned newNumArgs = newFn->arg_size();
|
|
|
|
if (callSite.arg_size() < newNumArgs) continue;
|
|
|
|
|
|
|
|
// 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(),
|
|
|
|
ae = newFn->arg_end(); ai != ae; ++ai, ++argNo) {
|
|
|
|
if (callSite.getArgument(argNo)->getType() != ai->getType()) {
|
|
|
|
dontTransform = true;
|
2009-05-05 14:16:31 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-08-03 08:43:55 +08:00
|
|
|
|
|
|
|
// Add any parameter attributes.
|
2013-01-23 14:15:10 +08:00
|
|
|
if (oldAttrs.hasAttributes(argNo + 1))
|
|
|
|
newAttrs.
|
2013-01-27 10:46:53 +08:00
|
|
|
push_back(llvm::
|
|
|
|
AttributeSet::get(newFn->getContext(),
|
|
|
|
oldAttrs.getParamAttributes(argNo + 1)));
|
2009-05-05 14:16:31 +08:00
|
|
|
}
|
2012-12-13 06:21:47 +08:00
|
|
|
if (dontTransform)
|
2009-05-05 14:16:31 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-19 05:26:07 +08:00
|
|
|
if (oldAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex))
|
2013-01-27 10:46:53 +08:00
|
|
|
newAttrs.push_back(llvm::AttributeSet::get(newFn->getContext(),
|
|
|
|
oldAttrs.getFnAttributes()));
|
2011-08-03 08:43:55 +08:00
|
|
|
|
2009-05-05 14:16:31 +08:00
|
|
|
// Okay, we can transform this. Create the new call instruction and copy
|
|
|
|
// over the required information.
|
2012-12-13 06:21:47 +08:00
|
|
|
newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo);
|
|
|
|
|
|
|
|
llvm::CallSite newCall;
|
|
|
|
if (callSite.isCall()) {
|
|
|
|
newCall = llvm::CallInst::Create(newFn, newArgs, "",
|
|
|
|
callSite.getInstruction());
|
|
|
|
} else {
|
|
|
|
llvm::InvokeInst *oldInvoke =
|
|
|
|
cast<llvm::InvokeInst>(callSite.getInstruction());
|
|
|
|
newCall = llvm::InvokeInst::Create(newFn,
|
|
|
|
oldInvoke->getNormalDest(),
|
|
|
|
oldInvoke->getUnwindDest(),
|
|
|
|
newArgs, "",
|
|
|
|
callSite.getInstruction());
|
|
|
|
}
|
|
|
|
newArgs.clear(); // for the next iteration
|
|
|
|
|
|
|
|
if (!newCall->getType()->isVoidTy())
|
|
|
|
newCall->takeName(callSite.getInstruction());
|
|
|
|
newCall.setAttributes(
|
|
|
|
llvm::AttributeSet::get(newFn->getContext(), newAttrs));
|
|
|
|
newCall.setCallingConv(callSite.getCallingConv());
|
2009-05-05 14:16:31 +08:00
|
|
|
|
|
|
|
// Finally, remove the old call, replacing any uses with the new one.
|
2012-12-13 06:21:47 +08:00
|
|
|
if (!callSite->use_empty())
|
|
|
|
callSite->replaceAllUsesWith(newCall.getInstruction());
|
2009-10-14 01:02:04 +08:00
|
|
|
|
2010-04-01 14:31:43 +08:00
|
|
|
// Copy debug location attached to CI.
|
2012-12-13 06:21:47 +08:00
|
|
|
if (!callSite->getDebugLoc().isUnknown())
|
|
|
|
newCall->setDebugLoc(callSite->getDebugLoc());
|
|
|
|
callSite->eraseFromParent();
|
2009-05-05 14:16:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 06:21:47 +08: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.
|
|
|
|
if (!isa<llvm::Function>(Old)) return;
|
|
|
|
|
|
|
|
replaceUsesOfNonProtoConstant(Old, NewFn);
|
|
|
|
}
|
|
|
|
|
2012-03-08 23:51:03 +08:00
|
|
|
void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
|
|
|
|
TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
|
|
|
|
// If we have a definition, this might be a deferred decl. If the
|
|
|
|
// instantiation is explicit, make sure we emit it at the end.
|
|
|
|
if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
|
|
|
|
GetAddrOfGlobalVar(VD);
|
2013-02-24 08:05:01 +08:00
|
|
|
|
|
|
|
EmitTopLevelDecl(VD);
|
2012-03-05 18:54:55 +08:00
|
|
|
}
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2009-05-13 05:21:08 +08:00
|
|
|
void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) {
|
|
|
|
const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl());
|
2011-03-09 12:27:21 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
// Compute the function info and LLVM type.
|
2012-02-17 11:33:10 +08:00
|
|
|
const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
|
|
|
|
llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
|
2011-03-09 12:27:21 +08:00
|
|
|
|
2009-05-13 04:58:15 +08:00
|
|
|
// Get or create the prototype for the function.
|
2009-05-13 05:21:08 +08:00
|
|
|
llvm::Constant *Entry = GetAddrOfFunction(GD, Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:25:43 +08: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 23:08:12 +08:00
|
|
|
|
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) {
|
2009-05-05 14:16:31 +08:00
|
|
|
llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 07:53:08 +08:00
|
|
|
// If the types mismatch then we have to rewrite the definition.
|
2009-05-05 14:16:31 +08:00
|
|
|
assert(OldFn->isDeclaration() &&
|
2009-03-21 17:25:43 +08:00
|
|
|
"Shouldn't replace non-declaration");
|
2009-03-21 16:53:37 +08:00
|
|
|
|
2009-03-21 16:38:50 +08: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 07:29:14 +08:00
|
|
|
// (e.g. "int f(int x)"). Move the old function aside so that it
|
|
|
|
// doesn't interfere with GetAddrOfFunction.
|
2011-07-23 18:55:15 +08:00
|
|
|
OldFn->setName(StringRef());
|
2009-05-13 05:21:08 +08:00
|
|
|
llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
// This might be an implementation of a function without a
|
|
|
|
// prototype, in which case, try to do special replacement of
|
|
|
|
// calls which match the new prototype. The really key thing here
|
|
|
|
// is that we also potentially drop arguments from the call site
|
|
|
|
// so as to make a direct call, which makes the inliner happier
|
|
|
|
// and suppresses a number of optimizer warnings (!) about
|
|
|
|
// dropping arguments.
|
|
|
|
if (!OldFn->use_empty()) {
|
2009-05-05 14:16:31 +08:00
|
|
|
ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn);
|
2009-05-13 04:58:15 +08:00
|
|
|
OldFn->removeDeadConstantUsers();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 16:38:50 +08:00
|
|
|
// Replace uses of F with the Function we will endow with a body.
|
2009-05-05 14:16:31 +08:00
|
|
|
if (!Entry->use_empty()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2009-07-30 02:54:39 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(NewFn, Entry->getType());
|
2009-05-05 14:16:31 +08:00
|
|
|
Entry->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 16:38:50 +08:00
|
|
|
// Ok, delete the old function now, which is dead.
|
2009-05-05 14:16:31 +08:00
|
|
|
OldFn->eraseFromParent();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
Entry = NewFn;
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-03 05:04:24 +08: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 17:25:43 +08:00
|
|
|
llvm::Function *Fn = cast<llvm::Function>(Entry);
|
2013-06-06 01:49:37 +08:00
|
|
|
setFunctionLinkage(GD, Fn);
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2010-11-03 05:04:24 +08:00
|
|
|
// FIXME: this is redundant with part of SetFunctionDefinitionAttributes
|
2011-01-30 03:39:23 +08:00
|
|
|
setGlobalVisibility(Fn, D);
|
2010-11-03 05:04:24 +08:00
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
MaybeHandleStaticInExternC(D, Fn);
|
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
CodeGenFunction(*this).GenerateCode(D, Fn, FI);
|
2008-08-12 01:36:14 +08:00
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
SetFunctionDefinitionAttributes(D, Fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, Fn);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
|
2008-09-09 07:44:31 +08:00
|
|
|
AddGlobalCtor(Fn, CA->getPriority());
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
|
2008-09-09 07:44:31 +08:00
|
|
|
AddGlobalDtor(Fn, DA->getPriority());
|
2011-09-10 06:41:49 +08:00
|
|
|
if (D->hasAttr<AnnotateAttr>())
|
|
|
|
AddGlobalAnnotations(D, Fn);
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
|
|
|
|
const ValueDecl *D = cast<ValueDecl>(GD.getDecl());
|
2009-06-30 10:34:44 +08:00
|
|
|
const AliasAttr *AA = D->getAttr<AliasAttr>();
|
2009-03-23 05:47:11 +08:00
|
|
|
assert(AA && "Not an alias?");
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2010-03-20 07:29:14 +08: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 23:08:12 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
|
2009-03-23 05:47:11 +08: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))
|
2012-10-06 07:12:53 +08:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
|
2011-02-05 12:35:53 +08:00
|
|
|
/*ForVTable=*/false);
|
2009-03-23 05:47:11 +08:00
|
|
|
else
|
2010-03-20 07:29:14 +08:00
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::getUnqual(DeclTy), 0);
|
2009-03-23 05:47:11 +08:00
|
|
|
|
|
|
|
// Create the new alias itself, but don't set a name yet.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalValue *GA =
|
2009-03-23 05:47:11 +08:00
|
|
|
new llvm::GlobalAlias(Aliasee->getType(),
|
|
|
|
llvm::Function::ExternalLinkage,
|
|
|
|
"", Aliasee, &getModule());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
if (Entry) {
|
2010-03-20 07:29:14 +08:00
|
|
|
assert(Entry->isDeclaration());
|
|
|
|
|
2009-03-23 05:47:11 +08: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 07:29:14 +08:00
|
|
|
GA->takeName(Entry);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 02:54:39 +08:00
|
|
|
Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
|
2009-03-23 05:47:11 +08:00
|
|
|
Entry->getType()));
|
|
|
|
Entry->eraseFromParent();
|
2010-03-20 07:29:14 +08:00
|
|
|
} else {
|
2010-06-23 00:16:50 +08:00
|
|
|
GA->setName(MangledName);
|
2009-03-23 05:47:11 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-14 16:05:55 +08: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 10:34:44 +08:00
|
|
|
if (D->hasAttr<DLLExportAttr>()) {
|
2009-04-14 16:05:55 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
// The dllexport attribute is ignored for undefined symbols.
|
2010-07-07 19:31:19 +08:00
|
|
|
if (FD->hasBody())
|
2009-04-14 16:05:55 +08:00
|
|
|
GA->setLinkage(llvm::Function::DLLExportLinkage);
|
|
|
|
} else {
|
|
|
|
GA->setLinkage(llvm::Function::DLLExportLinkage);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (D->hasAttr<WeakAttr>() ||
|
2010-02-24 06:00:30 +08: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.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
D->isWeakImported()) {
|
2009-04-14 16:05:55 +08:00
|
|
|
GA->setLinkage(llvm::Function::WeakAnyLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCommonAttributes(D, GA);
|
2009-03-23 05:47:11 +08:00
|
|
|
}
|
|
|
|
|
2011-07-15 01:45:50 +08:00
|
|
|
llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
|
2011-07-24 01:14:25 +08:00
|
|
|
ArrayRef<llvm::Type*> Tys) {
|
2011-07-12 22:06:48 +08:00
|
|
|
return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
|
2011-07-15 01:45:50 +08:00
|
|
|
Tys);
|
2007-12-18 08:25:38 +08:00
|
|
|
}
|
2007-08-31 12:31:45 +08:00
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
static llvm::StringMapEntry<llvm::Constant*> &
|
|
|
|
GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map,
|
|
|
|
const StringLiteral *Literal,
|
2009-07-24 07:41:22 +08:00
|
|
|
bool TargetIsLSB,
|
2009-07-24 06:52:48 +08:00
|
|
|
bool &IsUTF16,
|
|
|
|
unsigned &StringLength) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef String = Literal->getString();
|
2010-08-17 20:54:38 +08:00
|
|
|
unsigned NumBytes = String.size();
|
2009-07-24 06:52:48 +08:00
|
|
|
|
2009-09-22 18:03:52 +08:00
|
|
|
// Check for simple case.
|
|
|
|
if (!Literal->containsNonAsciiOrNull()) {
|
|
|
|
StringLength = NumBytes;
|
2010-08-17 20:54:38 +08:00
|
|
|
return Map.GetOrCreateValue(String);
|
2009-09-22 18:03:52 +08:00
|
|
|
}
|
|
|
|
|
2012-03-30 08:26:17 +08:00
|
|
|
// Otherwise, convert the UTF8 literals into a string of shorts.
|
|
|
|
IsUTF16 = true;
|
|
|
|
|
|
|
|
SmallVector<UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
|
2012-09-06 23:59:27 +08:00
|
|
|
const UTF8 *FromPtr = (const UTF8 *)String.data();
|
2009-07-24 06:52:48 +08:00
|
|
|
UTF16 *ToPtr = &ToBuf[0];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-09-08 03:57:04 +08:00
|
|
|
(void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
|
|
|
|
&ToPtr, ToPtr + NumBytes,
|
|
|
|
strictConversion);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 07:41:22 +08:00
|
|
|
// ConvertUTF8toUTF16 returns the length in ToPtr.
|
2009-07-24 06:52:48 +08:00
|
|
|
StringLength = ToPtr - &ToBuf[0];
|
2009-07-24 07:41:22 +08:00
|
|
|
|
2012-03-30 08:26:17 +08:00
|
|
|
// Add an explicit null.
|
|
|
|
*ToPtr = 0;
|
|
|
|
return Map.
|
|
|
|
GetOrCreateValue(StringRef(reinterpret_cast<const char *>(ToBuf.data()),
|
|
|
|
(StringLength + 1) * 2));
|
2009-07-24 06:52:48 +08:00
|
|
|
}
|
|
|
|
|
2011-05-14 02:13:10 +08:00
|
|
|
static llvm::StringMapEntry<llvm::Constant*> &
|
|
|
|
GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map,
|
2012-03-30 06:12:09 +08:00
|
|
|
const StringLiteral *Literal,
|
|
|
|
unsigned &StringLength) {
|
|
|
|
StringRef String = Literal->getString();
|
|
|
|
StringLength = String.size();
|
|
|
|
return Map.GetOrCreateValue(String);
|
2011-05-14 02:13:10 +08:00
|
|
|
}
|
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
|
|
|
|
unsigned StringLength = 0;
|
|
|
|
bool isUTF16 = false;
|
|
|
|
llvm::StringMapEntry<llvm::Constant*> &Entry =
|
2009-09-09 23:08:12 +08:00
|
|
|
GetConstantCFStringEntry(CFConstantStringMap, Literal,
|
2012-10-09 00:25:52 +08:00
|
|
|
getDataLayout().isLittleEndian(),
|
2009-07-24 07:41:22 +08:00
|
|
|
isUTF16, StringLength);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
if (llvm::Constant *C = Entry.getValue())
|
|
|
|
return C;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
|
2008-08-24 02:37:06 +08:00
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
2013-04-16 23:25:39 +08:00
|
|
|
llvm::Value *V;
|
|
|
|
|
2009-07-17 00:48:25 +08:00
|
|
|
// If we don't already have it, get __CFConstantStringClassReference.
|
2007-08-21 08:21:21 +08:00
|
|
|
if (!CFConstantStringClassRef) {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
|
2009-07-30 06:16:19 +08:00
|
|
|
Ty = llvm::ArrayType::get(Ty, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *GV = CreateRuntimeVariable(Ty,
|
2009-07-17 00:48:25 +08:00
|
|
|
"__CFConstantStringClassReference");
|
2008-08-24 02:37:06 +08:00
|
|
|
// Decay array -> ptr
|
2013-04-16 23:25:39 +08:00
|
|
|
V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
|
|
|
CFConstantStringClassRef = V;
|
2007-08-21 08:21:21 +08:00
|
|
|
}
|
2013-04-16 23:25:39 +08:00
|
|
|
else
|
|
|
|
V = CFConstantStringClassRef;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-16 02:54:24 +08:00
|
|
|
QualType CFTy = getContext().getCFConstantStringType();
|
2008-08-24 02:37:06 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *STy =
|
2008-11-16 02:54:24 +08:00
|
|
|
cast<llvm::StructType>(getTypes().ConvertType(CFTy));
|
|
|
|
|
2011-10-15 20:20:02 +08:00
|
|
|
llvm::Constant *Fields[4];
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Class pointer.
|
2013-04-16 23:25:39 +08:00
|
|
|
Fields[0] = cast<llvm::ConstantExpr>(V);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Flags.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
|
2009-08-16 13:55:31 +08:00
|
|
|
llvm::ConstantInt::get(Ty, 0x07C8);
|
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// String pointer.
|
2012-03-30 08:26:17 +08:00
|
|
|
llvm::Constant *C = 0;
|
|
|
|
if (isUTF16) {
|
|
|
|
ArrayRef<uint16_t> Arr =
|
2013-01-16 06:09:41 +08:00
|
|
|
llvm::makeArrayRef<uint16_t>(reinterpret_cast<uint16_t*>(
|
|
|
|
const_cast<char *>(Entry.getKey().data())),
|
2012-03-30 08:26:17 +08:00
|
|
|
Entry.getKey().size() / 2);
|
|
|
|
C = llvm::ConstantDataArray::get(VMContext, Arr);
|
|
|
|
} else {
|
|
|
|
C = llvm::ConstantDataArray::getString(VMContext, Entry.getKey());
|
|
|
|
}
|
2009-04-03 08:57:44 +08:00
|
|
|
|
2009-07-16 13:03:48 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
2012-01-10 16:46:39 +08:00
|
|
|
if (isUTF16)
|
2009-10-14 13:55:45 +08:00
|
|
|
// FIXME: why do utf strings get "_" labels instead of "L" labels?
|
2009-07-16 13:03:48 +08:00
|
|
|
Linkage = llvm::GlobalValue::InternalLinkage;
|
2012-01-10 16:46:39 +08:00
|
|
|
else
|
2011-03-15 01:55:00 +08: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 05:08:19 +08:00
|
|
|
Linkage = llvm::GlobalValue::LinkerPrivateLinkage;
|
2009-10-14 13:55:45 +08:00
|
|
|
|
2012-01-10 16:46:39 +08:00
|
|
|
// Note: -fwritable-strings doesn't make the backing store strings of
|
|
|
|
// CFStrings writable. (See <rdar://problem/10657500>)
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
2012-01-10 16:46:39 +08:00
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
|
|
|
|
Linkage, C, ".str");
|
2011-01-18 00:31:00 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
2013-05-07 00:23:57 +08:00
|
|
|
// Don't enforce the target's minimum global alignment, since the only use
|
|
|
|
// of the string is via this class initializer.
|
2009-04-03 08:57:44 +08:00
|
|
|
if (isUTF16) {
|
2010-01-27 02:46:23 +08:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2011-04-13 07:30:52 +08:00
|
|
|
} else {
|
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2009-04-03 08:57:44 +08:00
|
|
|
}
|
2012-03-30 08:26:17 +08:00
|
|
|
|
|
|
|
// String.
|
2011-07-21 22:31:17 +08:00
|
|
|
Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
2009-08-16 13:55:31 +08:00
|
|
|
|
2012-03-30 08:26:17 +08:00
|
|
|
if (isUTF16)
|
|
|
|
// Cast the UTF16 string to the correct type.
|
|
|
|
Fields[2] = llvm::ConstantExpr::getBitCast(Fields[2], Int8PtrTy);
|
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// String length.
|
|
|
|
Ty = getTypes().ConvertType(getContext().LongTy);
|
2009-08-16 13:55:31 +08:00
|
|
|
Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// The struct.
|
2009-07-28 06:29:56 +08:00
|
|
|
C = llvm::ConstantStruct::get(STy, Fields);
|
2009-09-09 23:08:12 +08:00
|
|
|
GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage, C,
|
2009-07-16 13:03:48 +08:00
|
|
|
"_unnamed_cfstring_");
|
2013-04-17 06:48:15 +08:00
|
|
|
if (const char *Sect = getTarget().getCFStringSection())
|
2009-04-01 07:42:16 +08:00
|
|
|
GV->setSection(Sect);
|
2009-07-24 06:52:48 +08:00
|
|
|
Entry.setValue(GV);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-01 08:41:52 +08:00
|
|
|
return GV;
|
2007-08-21 08:21:21 +08:00
|
|
|
}
|
2007-11-28 13:34:05 +08:00
|
|
|
|
2011-08-09 23:54:21 +08:00
|
|
|
static RecordDecl *
|
|
|
|
CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
|
|
|
|
DeclContext *DC, IdentifierInfo *Id) {
|
|
|
|
SourceLocation Loc;
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().CPlusPlus)
|
2011-08-09 23:54:21 +08:00
|
|
|
return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
|
|
|
|
else
|
|
|
|
return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
|
|
|
|
}
|
|
|
|
|
2010-04-23 04:26:39 +08:00
|
|
|
llvm::Constant *
|
2010-10-20 01:19:29 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
|
2010-04-24 01:41:07 +08:00
|
|
|
unsigned StringLength = 0;
|
|
|
|
llvm::StringMapEntry<llvm::Constant*> &Entry =
|
2011-05-14 02:13:10 +08:00
|
|
|
GetConstantStringEntry(CFConstantStringMap, Literal, StringLength);
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
if (llvm::Constant *C = Entry.getValue())
|
|
|
|
return C;
|
|
|
|
|
2012-02-07 06:47:00 +08:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
|
2010-04-24 01:41:07 +08:00
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
2013-04-16 23:25:39 +08:00
|
|
|
llvm::Value *V;
|
2010-04-24 06:33:39 +08:00
|
|
|
// If we don't already have it, get _NSConstantStringClassReference.
|
2010-10-20 01:19:29 +08:00
|
|
|
if (!ConstantStringClassRef) {
|
2012-03-11 15:00:24 +08:00
|
|
|
std::string StringClass(getLangOpts().ObjCConstantStringClass);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
|
2010-10-20 01:19:29 +08:00
|
|
|
llvm::Constant *GV;
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNonFragile()) {
|
2011-05-18 06:46:11 +08:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "OBJC_CLASS_$_NSConstantString"
|
|
|
|
: "OBJC_CLASS_$_" + StringClass;
|
2011-05-18 06:21:16 +08:00
|
|
|
GV = getObjCRuntime().GetClassGlobal(str);
|
|
|
|
// Make sure the result is of the correct type.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2013-04-16 23:25:39 +08:00
|
|
|
V = llvm::ConstantExpr::getBitCast(GV, PTy);
|
|
|
|
ConstantStringClassRef = V;
|
2011-05-18 06:21:16 +08:00
|
|
|
} else {
|
2011-05-18 06:46:11 +08:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "_NSConstantStringClassReference"
|
|
|
|
: "_" + StringClass + "ClassReference";
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
|
2011-05-18 06:46:11 +08:00
|
|
|
GV = CreateRuntimeVariable(PTy, str);
|
2011-05-18 06:21:16 +08:00
|
|
|
// Decay array -> ptr
|
2013-04-16 23:25:39 +08:00
|
|
|
V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
|
|
|
ConstantStringClassRef = V;
|
2010-10-20 01:19:29 +08:00
|
|
|
}
|
2010-04-24 01:41:07 +08:00
|
|
|
}
|
2013-04-16 23:25:39 +08:00
|
|
|
else
|
|
|
|
V = ConstantStringClassRef;
|
2011-08-09 23:54:21 +08: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,
|
2012-06-10 11:12:00 +08:00
|
|
|
ICIS_NoInit);
|
2011-08-09 23:54:21 +08:00
|
|
|
Field->setAccess(AS_public);
|
|
|
|
D->addDecl(Field);
|
|
|
|
}
|
|
|
|
|
|
|
|
D->completeDefinition();
|
|
|
|
QualType NSTy = Context.getTagDeclType(D);
|
|
|
|
NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy));
|
|
|
|
}
|
2010-04-24 01:41:07 +08:00
|
|
|
|
2011-10-15 20:20:02 +08:00
|
|
|
llvm::Constant *Fields[3];
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
// Class pointer.
|
2013-04-16 23:25:39 +08:00
|
|
|
Fields[0] = cast<llvm::ConstantExpr>(V);
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
// String pointer.
|
2012-02-05 10:30:40 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
llvm::ConstantDataArray::getString(VMContext, Entry.getKey());
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
|
|
|
bool isConstant;
|
2011-05-14 02:13:10 +08:00
|
|
|
Linkage = llvm::GlobalValue::PrivateLinkage;
|
2012-03-11 15:00:24 +08:00
|
|
|
isConstant = !LangOpts.WritableStrings;
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
|
|
|
|
".str");
|
2011-01-18 06:11:21 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
2013-05-07 00:23:57 +08:00
|
|
|
// Don't enforce the target's minimum global alignment, since the only use
|
|
|
|
// of the string is via this class initializer.
|
2011-05-14 02:13:10 +08:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2011-07-21 22:31:17 +08:00
|
|
|
Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
// String length.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
2010-04-24 01:41:07 +08:00
|
|
|
Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
|
|
|
|
|
|
|
|
// The struct.
|
2011-08-09 23:54:21 +08:00
|
|
|
C = llvm::ConstantStruct::get(NSConstantStringType, Fields);
|
2010-04-24 01:41:07 +08:00
|
|
|
GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage, C,
|
|
|
|
"_unnamed_nsstring_");
|
|
|
|
// FIXME. Fix section.
|
2010-04-24 06:33:39 +08:00
|
|
|
if (const char *Sect =
|
2012-06-20 14:18:46 +08:00
|
|
|
LangOpts.ObjCRuntime.isNonFragile()
|
2013-04-17 06:48:15 +08:00
|
|
|
? getTarget().getNSStringNonFragileABISection()
|
|
|
|
: getTarget().getNSStringSection())
|
2010-04-24 01:41:07 +08:00
|
|
|
GV->setSection(Sect);
|
|
|
|
Entry.setValue(GV);
|
|
|
|
|
|
|
|
return GV;
|
2010-04-23 04:26:39 +08:00
|
|
|
}
|
|
|
|
|
2011-08-10 01:23:49 +08: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,
|
2012-06-10 11:12:00 +08:00
|
|
|
ICIS_NoInit);
|
2011-08-10 01:23:49 +08:00
|
|
|
Field->setAccess(AS_public);
|
|
|
|
D->addDecl(Field);
|
|
|
|
}
|
|
|
|
|
|
|
|
D->completeDefinition();
|
|
|
|
ObjCFastEnumerationStateType = Context.getTagDeclType(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjCFastEnumerationStateType;
|
|
|
|
}
|
|
|
|
|
2011-11-01 10:23:42 +08: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 06:47:00 +08: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 10:30:40 +08:00
|
|
|
|
|
|
|
llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
|
|
|
|
llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
2012-02-07 06:52:04 +08: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 10:30:40 +08:00
|
|
|
}
|
|
|
|
|
2012-02-07 06:52:04 +08: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 10:23:42 +08:00
|
|
|
}
|
|
|
|
|
2008-08-14 07:20:05 +08:00
|
|
|
/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
|
|
|
|
/// constant array for the given string literal.
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) {
|
2013-05-07 00:23:57 +08:00
|
|
|
CharUnits Align = getContext().getAlignOfGlobalVarInChars(S->getType());
|
2011-11-01 10:23:42 +08:00
|
|
|
if (S->isAscii() || S->isUTF8()) {
|
2012-02-07 06:47:00 +08: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 13:55:46 +08:00
|
|
|
}
|
2011-11-01 10:23:42 +08:00
|
|
|
|
2012-02-07 06:47:00 +08:00
|
|
|
// FIXME: the following does not memoize wide strings.
|
2011-11-01 10:23:42 +08:00
|
|
|
llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(),C->getType(),
|
2012-03-11 15:00:24 +08:00
|
|
|
!LangOpts.WritableStrings,
|
2011-11-01 10:23:42 +08:00
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
C,".str");
|
2012-01-18 20:11:32 +08:00
|
|
|
|
2011-11-01 10:23:42 +08:00
|
|
|
GV->setAlignment(Align.getQuantity());
|
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
return GV;
|
2008-08-14 07:20:05 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08: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-08 04:17:55 +08:00
|
|
|
|
|
|
|
return GetAddrOfConstantCString(Str);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-11 08:02:17 +08:00
|
|
|
/// GenerateWritableString -- Creates storage for a string literal.
|
2011-08-04 09:03:22 +08:00
|
|
|
static llvm::GlobalVariable *GenerateStringLiteral(StringRef str,
|
2007-11-28 13:34:05 +08:00
|
|
|
bool constant,
|
2008-10-18 05:56:50 +08:00
|
|
|
CodeGenModule &CGM,
|
2011-08-04 09:03:22 +08:00
|
|
|
const char *GlobalName,
|
|
|
|
unsigned Alignment) {
|
2008-08-14 07:20:05 +08:00
|
|
|
// Create Constant for this string literal. Don't add a '\0'.
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Constant *C =
|
2012-02-05 10:30:40 +08:00
|
|
|
llvm::ConstantDataArray::getString(CGM.getLLVMContext(), str, false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-28 13:34:05 +08:00
|
|
|
// Create a global variable for this string
|
2011-01-11 06:34:03 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
C, GlobalName);
|
2011-08-04 09:03:22 +08:00
|
|
|
GV->setAlignment(Alignment);
|
2011-01-11 06:34:03 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
return GV;
|
2007-11-28 13:34:05 +08:00
|
|
|
}
|
|
|
|
|
2008-08-14 07:20:05 +08: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 18:55:15 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantString(StringRef Str,
|
2011-08-04 09:03:22 +08:00
|
|
|
const char *GlobalName,
|
|
|
|
unsigned Alignment) {
|
2009-04-01 07:42:16 +08:00
|
|
|
// Get the default prefix if a name wasn't specified.
|
|
|
|
if (!GlobalName)
|
2009-07-16 13:03:48 +08:00
|
|
|
GlobalName = ".str";
|
2009-04-01 07:42:16 +08:00
|
|
|
|
2013-05-07 00:23:57 +08:00
|
|
|
if (Alignment == 0)
|
|
|
|
Alignment = getContext().getAlignOfGlobalVarInChars(getContext().CharTy)
|
|
|
|
.getQuantity();
|
|
|
|
|
2009-04-01 07:42:16 +08:00
|
|
|
// Don't share any string literals if strings aren't constant.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.WritableStrings)
|
2011-08-04 09:03:22 +08:00
|
|
|
return GenerateStringLiteral(Str, false, *this, GlobalName, Alignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-04 09:03:22 +08:00
|
|
|
llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
|
2011-03-05 21:45:23 +08:00
|
|
|
ConstantStringMap.GetOrCreateValue(Str);
|
2007-11-28 13:34:05 +08:00
|
|
|
|
2011-08-04 09:03:22 +08:00
|
|
|
if (llvm::GlobalVariable *GV = Entry.getValue()) {
|
|
|
|
if (Alignment > GV->getAlignment()) {
|
|
|
|
GV->setAlignment(Alignment);
|
|
|
|
}
|
|
|
|
return GV;
|
|
|
|
}
|
2007-11-28 13:34:05 +08:00
|
|
|
|
|
|
|
// Create a global variable for this.
|
2012-02-07 06:47:00 +08:00
|
|
|
llvm::GlobalVariable *GV = GenerateStringLiteral(Str, true, *this, GlobalName,
|
|
|
|
Alignment);
|
2011-08-04 09:03:22 +08:00
|
|
|
Entry.setValue(GV);
|
|
|
|
return GV;
|
2007-11-28 13:34:05 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
|
|
|
|
/// GetAddrOfConstantCString - Returns a pointer to a character
|
2011-03-05 21:45:23 +08:00
|
|
|
/// array containing the literal and a terminating '\0'
|
2008-08-14 07:20:05 +08:00
|
|
|
/// character. The result has pointer to array type.
|
2011-03-05 21:45:23 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &Str,
|
2011-08-04 09:03:22 +08:00
|
|
|
const char *GlobalName,
|
|
|
|
unsigned Alignment) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef StrWithNull(Str.c_str(), Str.size() + 1);
|
2011-08-04 09:03:22 +08:00
|
|
|
return GetAddrOfConstantString(StrWithNull, GlobalName, Alignment);
|
2008-08-14 07:20:05 +08:00
|
|
|
}
|
2008-08-16 07:26:23 +08:00
|
|
|
|
2013-06-05 08:46:14 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfGlobalTemporary(
|
2013-06-14 11:07:01 +08:00
|
|
|
const MaterializeTemporaryExpr *E, const Expr *Init) {
|
2013-06-05 08:46:14 +08:00
|
|
|
assert((E->getStorageDuration() == SD_Static ||
|
|
|
|
E->getStorageDuration() == SD_Thread) && "not a global temporary");
|
|
|
|
const VarDecl *VD = cast<VarDecl>(E->getExtendingDecl());
|
|
|
|
|
|
|
|
// If we're not materializing a subobject of the temporary, keep the
|
|
|
|
// cv-qualifiers from the type of the MaterializeTemporaryExpr.
|
2013-06-14 11:07:01 +08:00
|
|
|
QualType MaterializedType = Init->getType();
|
|
|
|
if (Init == E->GetTemporaryExpr())
|
|
|
|
MaterializedType = E->getType();
|
2013-06-05 08:46:14 +08:00
|
|
|
|
|
|
|
llvm::Constant *&Slot = MaterializedGlobalTemporaryMap[E];
|
|
|
|
if (Slot)
|
|
|
|
return Slot;
|
|
|
|
|
|
|
|
// FIXME: If an externally-visible declaration extends multiple temporaries,
|
|
|
|
// we need to give each temporary the same name in every translation unit (and
|
|
|
|
// we also need to make the temporaries externally-visible).
|
|
|
|
SmallString<256> Name;
|
|
|
|
llvm::raw_svector_ostream Out(Name);
|
|
|
|
getCXXABI().getMangleContext().mangleReferenceTemporary(VD, Out);
|
|
|
|
Out.flush();
|
|
|
|
|
|
|
|
APValue *Value = 0;
|
|
|
|
if (E->getStorageDuration() == SD_Static) {
|
2013-06-14 11:07:01 +08:00
|
|
|
// We might have a cached constant initializer for this temporary. Note
|
|
|
|
// that this might have a different value from the value computed by
|
|
|
|
// evaluating the initializer if the surrounding constant expression
|
|
|
|
// modifies the temporary.
|
2013-06-05 08:46:14 +08:00
|
|
|
Value = getContext().getMaterializedTemporaryValue(E, false);
|
|
|
|
if (Value && Value->isUninit())
|
|
|
|
Value = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-14 11:07:01 +08:00
|
|
|
// Try evaluating it now, it might have a constant initializer.
|
|
|
|
Expr::EvalResult EvalResult;
|
|
|
|
if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
|
|
|
|
!EvalResult.hasSideEffects())
|
|
|
|
Value = &EvalResult.Val;
|
|
|
|
|
|
|
|
llvm::Constant *InitialValue = 0;
|
|
|
|
bool Constant = false;
|
|
|
|
llvm::Type *Type;
|
2013-06-05 08:46:14 +08:00
|
|
|
if (Value) {
|
|
|
|
// The temporary has a constant initializer, use it.
|
2013-06-14 11:07:01 +08:00
|
|
|
InitialValue = EmitConstantValue(*Value, MaterializedType, 0);
|
|
|
|
Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
|
|
|
|
Type = InitialValue->getType();
|
2013-06-05 08:46:14 +08:00
|
|
|
} else {
|
2013-06-14 11:07:01 +08:00
|
|
|
// No initializer, the initialization will be provided when we
|
2013-06-05 08:46:14 +08:00
|
|
|
// initialize the declaration which performed lifetime extension.
|
2013-06-14 11:07:01 +08:00
|
|
|
Type = getTypes().ConvertTypeForMem(MaterializedType);
|
2013-06-05 08:46:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a global variable for this lifetime-extended temporary.
|
|
|
|
llvm::GlobalVariable *GV =
|
2013-06-14 11:07:01 +08:00
|
|
|
new llvm::GlobalVariable(getModule(), Type, Constant,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
InitialValue, Name.c_str());
|
2013-06-05 08:46:14 +08:00
|
|
|
GV->setAlignment(
|
2013-06-14 11:07:01 +08:00
|
|
|
getContext().getTypeAlignInChars(MaterializedType).getQuantity());
|
2013-06-05 08:46:14 +08:00
|
|
|
if (VD->getTLSKind())
|
|
|
|
setTLSMode(GV, *VD);
|
|
|
|
Slot = GV;
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
/// EmitObjCPropertyImplementations - Emit information for synthesized
|
|
|
|
/// properties for an implementation.
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::EmitObjCPropertyImplementations(const
|
2008-08-26 16:29:31 +08:00
|
|
|
ObjCImplementationDecl *D) {
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCImplementationDecl::propimpl_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyImplDecl *PID = *i;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-26 16:29:31 +08: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
|
2012-10-11 00:42:25 +08:00
|
|
|
// been overridden. Note that ::isPropertyAccessor is not the method
|
2008-08-26 16:29:31 +08:00
|
|
|
// 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 10:36:12 +08:00
|
|
|
if (!D->getInstanceMethod(PD->getGetterName()))
|
2008-12-10 04:23:04 +08:00
|
|
|
CodeGenFunction(*this).GenerateObjCGetter(
|
|
|
|
const_cast<ObjCImplementationDecl *>(D), PID);
|
2008-08-26 16:29:31 +08:00
|
|
|
if (!PD->isReadOnly() &&
|
2009-06-30 10:36:12 +08:00
|
|
|
!D->getInstanceMethod(PD->getSetterName()))
|
2008-12-10 04:23:04 +08:00
|
|
|
CodeGenFunction(*this).GenerateObjCSetter(
|
|
|
|
const_cast<ObjCImplementationDecl *>(D), PID);
|
2008-08-26 16:29:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:05:39 +08:00
|
|
|
static bool needsDestructMethod(ObjCImplementationDecl *impl) {
|
2011-07-22 10:08:32 +08:00
|
|
|
const ObjCInterfaceDecl *iface = impl->getClassInterface();
|
|
|
|
for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
|
2011-03-22 15:05:39 +08:00
|
|
|
ivar; ivar = ivar->getNextIvar())
|
|
|
|
if (ivar->getType().isDestructedType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-29 05:28:56 +08:00
|
|
|
/// EmitObjCIvarInitializations - Emit information for ivar initialization
|
|
|
|
/// for an implementation.
|
|
|
|
void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
|
2011-03-22 15:05:39 +08: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-18 03:25:08 +08:00
|
|
|
cxxSelector, getContext().VoidTy, 0, D,
|
|
|
|
/*isInstance=*/true, /*isVariadic=*/false,
|
2012-10-11 00:42:25 +08:00
|
|
|
/*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isDefined=*/false, ObjCMethodDecl::Required);
|
2011-03-22 15:05:39 +08:00
|
|
|
D->addInstanceMethod(DTORMethod);
|
|
|
|
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
|
2012-10-17 12:53:31 +08:00
|
|
|
D->setHasDestructors(true);
|
2011-03-22 15:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the implementation doesn't have any ivar initializers, we don't need
|
|
|
|
// a .cxx_construct.
|
2011-03-17 22:19:08 +08:00
|
|
|
if (D->getNumIvarInitializers() == 0)
|
2010-04-29 05:28:56 +08:00
|
|
|
return;
|
|
|
|
|
2011-03-22 15:05:39 +08:00
|
|
|
IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
|
|
|
|
Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
|
2010-04-29 05:28:56 +08:00
|
|
|
// The constructor returns 'self'.
|
|
|
|
ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
|
|
|
|
D->getLocation(),
|
2011-10-03 14:36:36 +08:00
|
|
|
D->getLocation(),
|
|
|
|
cxxSelector,
|
2010-04-29 05:28:56 +08:00
|
|
|
getContext().getObjCIdType(), 0,
|
2011-08-18 03:25:08 +08:00
|
|
|
D, /*isInstance=*/true,
|
|
|
|
/*isVariadic=*/false,
|
2012-10-11 00:42:25 +08:00
|
|
|
/*isPropertyAccessor=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isImplicitlyDeclared=*/true,
|
|
|
|
/*isDefined=*/false,
|
2010-04-29 05:28:56 +08:00
|
|
|
ObjCMethodDecl::Required);
|
|
|
|
D->addInstanceMethod(CTORMethod);
|
|
|
|
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
|
2012-10-17 12:53:31 +08:00
|
|
|
D->setHasNonZeroConstructors(true);
|
2010-04-29 05:28:56 +08:00
|
|
|
}
|
|
|
|
|
2009-04-02 13:55:18 +08:00
|
|
|
/// EmitNamespace - Emit all declarations in a namespace.
|
2009-04-01 08:58:25 +08:00
|
|
|
void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
|
2009-04-01 08:58:25 +08:00
|
|
|
I != E; ++I)
|
|
|
|
EmitTopLevelDecl(*I);
|
|
|
|
}
|
|
|
|
|
2009-04-02 13:55:18 +08:00
|
|
|
// EmitLinkageSpec - Emit all declarations in a linkage spec.
|
|
|
|
void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
|
2009-08-02 04:48:04 +08:00
|
|
|
if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
|
|
|
|
LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
|
2009-04-02 13:55:18 +08:00
|
|
|
ErrorUnsupported(LSD, "linkage spec");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end();
|
2012-10-27 04:22:11 +08:00
|
|
|
I != E; ++I) {
|
2012-10-27 06:20:25 +08:00
|
|
|
// Meta-data for ObjC class includes references to implemented methods.
|
|
|
|
// Generate class's method definitions first.
|
2012-10-27 04:22:11 +08:00
|
|
|
if (ObjCImplDecl *OID = dyn_cast<ObjCImplDecl>(*I)) {
|
|
|
|
for (ObjCContainerDecl::method_iterator M = OID->meth_begin(),
|
|
|
|
MEnd = OID->meth_end();
|
|
|
|
M != MEnd; ++M)
|
|
|
|
EmitTopLevelDecl(*M);
|
|
|
|
}
|
2009-04-02 13:55:18 +08:00
|
|
|
EmitTopLevelDecl(*I);
|
2012-10-27 04:22:11 +08:00
|
|
|
}
|
2009-04-02 13:55:18 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 07:26:23 +08: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-30 01:30:29 +08:00
|
|
|
// Ignore dependent declarations.
|
|
|
|
if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
switch (D->getKind()) {
|
2009-08-25 21:14:46 +08:00
|
|
|
case Decl::CXXConversion:
|
2009-04-05 04:47:02 +08:00
|
|
|
case Decl::CXXMethod:
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Function:
|
2009-06-30 01:30:29 +08:00
|
|
|
// Skip function templates
|
2011-04-23 06:18:13 +08:00
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
|
|
|
|
cast<FunctionDecl>(D)->isLateTemplateParsed())
|
2009-06-30 01:30:29 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-11 07:43:36 +08:00
|
|
|
EmitGlobal(cast<FunctionDecl>(D));
|
|
|
|
break;
|
2013-08-06 09:03:05 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Var:
|
2013-08-06 09:03:05 +08:00
|
|
|
// Skip variable templates
|
|
|
|
if (cast<VarDecl>(D)->getDescribedVarTemplate())
|
|
|
|
return;
|
|
|
|
case Decl::VarTemplateSpecialization:
|
2009-09-11 07:43:36 +08:00
|
|
|
EmitGlobal(cast<VarDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
2011-04-12 09:01:22 +08: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 23:55:24 +08:00
|
|
|
// C++ Decls
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Namespace:
|
2009-04-01 08:58:25 +08:00
|
|
|
EmitNamespace(cast<NamespaceDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2009-06-20 08:51:54 +08:00
|
|
|
// No code generation needed.
|
2009-11-17 17:33:40 +08:00
|
|
|
case Decl::UsingShadow:
|
2009-06-20 08:51:54 +08:00
|
|
|
case Decl::Using:
|
2009-06-30 04:59:39 +08:00
|
|
|
case Decl::ClassTemplate:
|
2013-08-06 09:03:05 +08:00
|
|
|
case Decl::VarTemplate:
|
|
|
|
case Decl::VarTemplatePartialSpecialization:
|
2009-06-30 04:59:39 +08:00
|
|
|
case Decl::FunctionTemplate:
|
2011-05-06 05:57:07 +08:00
|
|
|
case Decl::TypeAliasTemplate:
|
2011-06-05 13:04:23 +08:00
|
|
|
case Decl::Block:
|
2013-02-23 01:15:32 +08:00
|
|
|
case Decl::Empty:
|
2009-06-20 08:51:54 +08:00
|
|
|
break;
|
2013-05-21 06:50:41 +08:00
|
|
|
case Decl::NamespaceAlias:
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
|
|
|
|
return;
|
2013-04-26 13:41:06 +08:00
|
|
|
case Decl::UsingDirective: // using namespace X; [C++]
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
|
|
|
|
return;
|
2009-04-15 23:55:24 +08:00
|
|
|
case Decl::CXXConstructor:
|
2009-11-24 13:16:24 +08:00
|
|
|
// Skip function templates
|
2011-04-23 06:18:13 +08:00
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
|
|
|
|
cast<FunctionDecl>(D)->isLateTemplateParsed())
|
2009-11-24 13:16:24 +08:00
|
|
|
return;
|
|
|
|
|
2013-08-05 01:30:04 +08:00
|
|
|
getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
|
2009-04-15 23:55:24 +08:00
|
|
|
break;
|
2009-04-17 09:58:57 +08:00
|
|
|
case Decl::CXXDestructor:
|
2011-04-23 06:18:13 +08:00
|
|
|
if (cast<FunctionDecl>(D)->isLateTemplateParsed())
|
|
|
|
return;
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
|
2009-04-17 09:58:57 +08:00
|
|
|
break;
|
2009-06-12 05:22:55 +08:00
|
|
|
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
2009-04-15 23:55:24 +08:00
|
|
|
// Objective-C Decls
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 06:33:24 +08:00
|
|
|
// Forward declarations, no (immediate) code generation.
|
2009-03-22 02:06:45 +08:00
|
|
|
case Decl::ObjCInterface:
|
2012-07-20 06:22:55 +08:00
|
|
|
case Decl::ObjCCategory:
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2009-04-01 10:36:43 +08:00
|
|
|
|
2012-01-02 05:23:57 +08:00
|
|
|
case Decl::ObjCProtocol: {
|
|
|
|
ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(D);
|
|
|
|
if (Proto->isThisDeclarationADefinition())
|
|
|
|
ObjCRuntime->GenerateProtocol(Proto);
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2012-01-02 05:23:57 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::ObjCCategoryImpl:
|
2008-08-26 16:29:31 +08:00
|
|
|
// Categories have properties but don't support synthesize so we
|
|
|
|
// can ignore them here.
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
case Decl::ObjCImplementation: {
|
|
|
|
ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
|
|
|
|
EmitObjCPropertyImplementations(OMD);
|
2010-04-29 05:28:56 +08:00
|
|
|
EmitObjCIvarInitializations(OMD);
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime->GenerateClass(OMD);
|
2012-04-11 13:56:05 +08:00
|
|
|
// Emit global variable debug information.
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
2012-12-04 02:28:12 +08:00
|
|
|
if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
|
|
|
|
DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
|
|
|
|
OMD->getClassInterface()), OMD->getLocation());
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D);
|
|
|
|
// If this is not a prototype, emit the body.
|
2009-06-30 10:35:26 +08:00
|
|
|
if (OMD->getBody())
|
2008-08-16 07:26:23 +08:00
|
|
|
CodeGenFunction(*this).GenerateObjCMethod(OMD);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
case Decl::ObjCCompatibleAlias:
|
2012-02-01 02:59:20 +08:00
|
|
|
ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
2009-04-02 13:55:18 +08:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
EmitLinkageSpec(cast<LinkageSpecDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::FileScopeAsm: {
|
|
|
|
FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D);
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef AsmString = AD->getAsmString()->getString();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
const std::string &S = getModule().getModuleInlineAsm();
|
|
|
|
if (S.empty())
|
|
|
|
getModule().setModuleInlineAsm(AsmString);
|
2012-08-10 18:57:52 +08:00
|
|
|
else if (S.end()[-1] == '\n')
|
2011-07-24 04:04:25 +08:00
|
|
|
getModule().setModuleInlineAsm(S + AsmString.str());
|
2008-08-16 07:26:23 +08:00
|
|
|
else
|
2009-12-11 21:33:18 +08:00
|
|
|
getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-15 01:21:00 +08:00
|
|
|
case Decl::Import: {
|
|
|
|
ImportDecl *Import = cast<ImportDecl>(D);
|
|
|
|
|
|
|
|
// Ignore import declarations that come from imported modules.
|
|
|
|
if (clang::Module *Owner = Import->getOwningModule()) {
|
|
|
|
if (getLangOpts().CurrentModule.empty() ||
|
|
|
|
Owner->getTopLevelModule()->Name == getLangOpts().CurrentModule)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-15 04:53:57 +08:00
|
|
|
ImportedModules.insert(Import->getImportedModule());
|
2013-01-15 01:21:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
default:
|
2009-05-16 15:57:57 +08: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 07:26:23 +08:00
|
|
|
assert(isa<TypeDecl>(D) && "Unsupported decl kind");
|
|
|
|
}
|
|
|
|
}
|
2010-07-07 07:57:41 +08: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 12:24:23 +08:00
|
|
|
llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
|
2010-07-07 07:57:41 +08: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-22 03:59:12 +08:00
|
|
|
GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
|
2010-07-07 07:57:41 +08:00
|
|
|
}
|
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
/// For each function which is declared within an extern "C" region and marked
|
|
|
|
/// as 'used', but has internal linkage, create an alias from the unmangled
|
|
|
|
/// name to the mangled name if possible. People expect to be able to refer
|
|
|
|
/// to such functions with an unmangled name from inline assembly within the
|
|
|
|
/// same translation unit.
|
|
|
|
void CodeGenModule::EmitStaticExternCAliases() {
|
2013-04-13 09:28:18 +08:00
|
|
|
for (StaticExternCMap::iterator I = StaticExternCValues.begin(),
|
|
|
|
E = StaticExternCValues.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
IdentifierInfo *Name = I->first;
|
|
|
|
llvm::GlobalValue *Val = I->second;
|
2013-04-06 15:07:44 +08:00
|
|
|
if (Val && !getModule().getNamedValue(Name->getName()))
|
|
|
|
AddUsedGlobal(new llvm::GlobalAlias(Val->getType(), Val->getLinkage(),
|
|
|
|
Name->getName(), Val, &getModule()));
|
|
|
|
}
|
2013-04-06 13:00:46 +08:00
|
|
|
}
|
|
|
|
|
2010-07-07 07:57:41 +08: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 18:55:15 +08:00
|
|
|
for (llvm::DenseMap<GlobalDecl,StringRef>::iterator
|
2010-07-07 07:57:41 +08: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-22 03:59:12 +08:00
|
|
|
Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr));
|
2010-07-07 07:57:41 +08: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 08:00:15 +08:00
|
|
|
|
2011-05-05 08:08:20 +08:00
|
|
|
void CodeGenModule::EmitCoverageFile() {
|
|
|
|
if (!getCodeGenOpts().CoverageFile.empty()) {
|
2011-05-05 04:46:58 +08: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 08:08:20 +08:00
|
|
|
llvm::MDString *CoverageFile =
|
|
|
|
llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile);
|
2011-05-05 04:46:58 +08:00
|
|
|
for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
|
|
|
|
llvm::MDNode *CU = CUNode->getOperand(i);
|
2011-05-05 08:08:20 +08:00
|
|
|
llvm::Value *node[] = { CoverageFile, CU };
|
2011-05-05 04:46:58 +08:00
|
|
|
llvm::MDNode *N = llvm::MDNode::get(Ctx, node);
|
|
|
|
GCov->addOperand(N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 18:13:44 +08:00
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid,
|
|
|
|
QualType GuidType) {
|
|
|
|
// Sema has checked that all uuid strings are of the form
|
|
|
|
// "12345678-1234-1234-1234-1234567890ab".
|
|
|
|
assert(Uuid.size() == 36);
|
|
|
|
const char *Uuidstr = Uuid.data();
|
|
|
|
for (int i = 0; i < 36; ++i) {
|
|
|
|
if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuidstr[i] == '-');
|
2013-02-09 06:30:41 +08:00
|
|
|
else assert(isHexDigit(Uuidstr[i]));
|
2012-10-11 18:13:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::APInt Field0(32, StringRef(Uuidstr , 8), 16);
|
|
|
|
llvm::APInt Field1(16, StringRef(Uuidstr + 9, 4), 16);
|
|
|
|
llvm::APInt Field2(16, StringRef(Uuidstr + 14, 4), 16);
|
2012-10-17 08:34:34 +08:00
|
|
|
static const int Field3ValueOffsets[] = { 19, 21, 24, 26, 28, 30, 32, 34 };
|
2012-10-11 18:13:44 +08:00
|
|
|
|
|
|
|
APValue InitStruct(APValue::UninitStruct(), /*NumBases=*/0, /*NumFields=*/4);
|
|
|
|
InitStruct.getStructField(0) = APValue(llvm::APSInt(Field0));
|
|
|
|
InitStruct.getStructField(1) = APValue(llvm::APSInt(Field1));
|
|
|
|
InitStruct.getStructField(2) = APValue(llvm::APSInt(Field2));
|
|
|
|
APValue& Arr = InitStruct.getStructField(3);
|
|
|
|
Arr = APValue(APValue::UninitArray(), 8, 8);
|
|
|
|
for (int t = 0; t < 8; ++t)
|
2012-10-14 05:56:05 +08:00
|
|
|
Arr.getArrayInitializedElt(t) = APValue(llvm::APSInt(
|
|
|
|
llvm::APInt(8, StringRef(Uuidstr + Field3ValueOffsets[t], 2), 16)));
|
2012-10-11 18:13:44 +08:00
|
|
|
|
|
|
|
return EmitConstantValue(InitStruct, GuidType);
|
|
|
|
}
|