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"
|
2009-03-26 13:00:52 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2007-05-28 09:07:47 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2008-09-09 05:33:45 +08:00
|
|
|
#include "CGCall.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2009-02-13 08:10:09 +08:00
|
|
|
#include "Mangle.h"
|
2010-01-10 20:58:08 +08:00
|
|
|
#include "TargetInfo.h"
|
2009-11-13 01:24:48 +08:00
|
|
|
#include "clang/CodeGen/CodeGenOptions.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-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-11-05 00:51:42 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-12-01 07:41:22 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2007-12-02 15:19:18 +08:00
|
|
|
#include "clang/Basic/Diagnostic.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"
|
2009-04-01 23:50:34 +08:00
|
|
|
#include "clang/Basic/ConvertUTF.h"
|
2008-03-09 11:09:36 +08:00
|
|
|
#include "llvm/CallingConv.h"
|
2007-08-31 12:31:45 +08:00
|
|
|
#include "llvm/Module.h"
|
2007-06-23 02:48:09 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-12-29 05:44:41 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2008-06-01 22:13:53 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-11-07 17:22:46 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-05-28 09:07:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
|
2009-11-13 01:24:48 +08:00
|
|
|
CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
|
2007-12-02 09:40:18 +08:00
|
|
|
llvm::Module &M, const llvm::TargetData &TD,
|
2009-03-26 13:00:52 +08:00
|
|
|
Diagnostic &diags)
|
|
|
|
: BlockModule(C, M, TD, Types, *this), Context(C),
|
2009-11-13 01:24:48 +08:00
|
|
|
Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M),
|
2010-01-10 20:58:08 +08:00
|
|
|
TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags),
|
|
|
|
Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()),
|
|
|
|
MangleCtx(C), VtableInfo(*this), Runtime(0),
|
2009-07-15 07:10:40 +08:00
|
|
|
MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0),
|
|
|
|
VMContext(M.getContext()) {
|
2008-08-12 02:12:00 +08:00
|
|
|
|
2009-03-21 15:12:05 +08:00
|
|
|
if (!Features.ObjC1)
|
|
|
|
Runtime = 0;
|
|
|
|
else if (!Features.NeXTRuntime)
|
|
|
|
Runtime = CreateGNUObjCRuntime(*this);
|
|
|
|
else if (Features.ObjCNonFragileABI)
|
|
|
|
Runtime = CreateMacNonFragileABIObjCRuntime(*this);
|
|
|
|
else
|
|
|
|
Runtime = CreateMacObjCRuntime(*this);
|
2008-05-08 16:54:20 +08:00
|
|
|
|
|
|
|
// If debug info generation is enabled, create the CGDebugInfo object.
|
2009-12-07 02:00:51 +08:00
|
|
|
DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0;
|
2008-03-01 16:45:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenModule::~CodeGenModule() {
|
2008-08-06 02:50:11 +08:00
|
|
|
delete Runtime;
|
|
|
|
delete DebugInfo;
|
|
|
|
}
|
|
|
|
|
2010-01-23 10:40:42 +08:00
|
|
|
void CodeGenModule::createObjCRuntime() {
|
|
|
|
if (!Features.NeXTRuntime)
|
|
|
|
Runtime = CreateGNUObjCRuntime(*this);
|
|
|
|
else if (Features.ObjCNonFragileABI)
|
|
|
|
Runtime = CreateMacNonFragileABIObjCRuntime(*this);
|
|
|
|
else
|
|
|
|
Runtime = CreateMacObjCRuntime(*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();
|
2008-08-12 02:12:00 +08:00
|
|
|
if (Runtime)
|
|
|
|
if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
|
|
|
|
AddGlobalCtor(ObjCInitFunction);
|
2008-08-01 08:01:51 +08:00
|
|
|
EmitCtorList(GlobalCtors, "llvm.global_ctors");
|
|
|
|
EmitCtorList(GlobalDtors, "llvm.global_dtors");
|
2008-04-19 07:43:57 +08:00
|
|
|
EmitAnnotations();
|
2009-02-14 04:29:50 +08:00
|
|
|
EmitLLVMUsed();
|
2008-10-01 08:49:24 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::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;
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::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
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
LangOptions::VisibilityMode
|
2009-04-14 14:00:08 +08:00
|
|
|
CodeGenModule::getDeclVisibilityMode(const Decl *D) const {
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
|
|
|
if (VD->getStorageClass() == VarDecl::PrivateExtern)
|
|
|
|
return LangOptions::Hidden;
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) {
|
2009-04-14 14:00:08 +08:00
|
|
|
switch (attr->getVisibility()) {
|
|
|
|
default: assert(0 && "Unknown visibility!");
|
2009-09-09 23:08:12 +08:00
|
|
|
case VisibilityAttr::DefaultVisibility:
|
2009-04-14 14:00:08 +08:00
|
|
|
return LangOptions::Default;
|
|
|
|
case VisibilityAttr::HiddenVisibility:
|
|
|
|
return LangOptions::Hidden;
|
|
|
|
case VisibilityAttr::ProtectedVisibility:
|
|
|
|
return LangOptions::Protected;
|
|
|
|
}
|
2009-04-11 04:26:50 +08:00
|
|
|
}
|
2009-04-07 13:48:37 +08:00
|
|
|
|
2010-02-07 09:44:36 +08:00
|
|
|
// This decl should have the same visibility as its parent.
|
|
|
|
if (const DeclContext *DC = D->getDeclContext())
|
|
|
|
return getDeclVisibilityMode(cast<Decl>(DC));
|
|
|
|
|
2009-04-14 14:00:08 +08:00
|
|
|
return getLangOptions().getVisibilityMode();
|
2008-05-22 08:50:06 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
|
2009-04-14 14:00:08 +08:00
|
|
|
const Decl *D) const {
|
|
|
|
// 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
|
|
|
|
2009-04-14 14:00:08 +08:00
|
|
|
switch (getDeclVisibilityMode(D)) {
|
2009-04-03 11:28:57 +08:00
|
|
|
default: assert(0 && "Unknown visibility!");
|
2009-04-14 14:00:08 +08:00
|
|
|
case LangOptions::Default:
|
|
|
|
return GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
|
|
|
case LangOptions::Hidden:
|
|
|
|
return GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
|
|
|
case LangOptions::Protected:
|
|
|
|
return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
|
2009-04-03 11:28:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-05 12:44:02 +08:00
|
|
|
const char *CodeGenModule::getMangledName(const GlobalDecl &GD) {
|
2009-09-11 07:38:47 +08:00
|
|
|
const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-05 12:44:02 +08:00
|
|
|
if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND))
|
|
|
|
return getMangledCXXCtorName(D, GD.getCtorType());
|
|
|
|
if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND))
|
|
|
|
return getMangledCXXDtorName(D, GD.getDtorType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-05 12:44:02 +08:00
|
|
|
return getMangledName(ND);
|
|
|
|
}
|
|
|
|
|
2009-02-13 08:10:09 +08:00
|
|
|
/// \brief Retrieves the mangled name for the given declaration.
|
|
|
|
///
|
|
|
|
/// If the given declaration requires a mangled name, returns an
|
2009-03-21 14:31:09 +08:00
|
|
|
/// const char* containing the mangled name. Otherwise, returns
|
|
|
|
/// the unmangled name.
|
2009-02-13 08:10:09 +08:00
|
|
|
///
|
2009-02-19 07:53:56 +08:00
|
|
|
const char *CodeGenModule::getMangledName(const NamedDecl *ND) {
|
2009-11-21 17:14:52 +08:00
|
|
|
if (!getMangleContext().shouldMangleDeclName(ND)) {
|
2009-03-21 14:31:09 +08:00
|
|
|
assert(ND->getIdentifier() && "Attempt to mangle unnamed decl.");
|
2009-03-21 15:12:05 +08:00
|
|
|
return ND->getNameAsCString();
|
2009-03-21 14:31:09 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 07:53:56 +08:00
|
|
|
llvm::SmallString<256> Name;
|
2009-11-21 17:14:52 +08:00
|
|
|
getMangleContext().mangleName(ND, Name);
|
2009-02-19 07:53:56 +08:00
|
|
|
Name += '\0';
|
2009-04-15 23:55:24 +08:00
|
|
|
return UniqueMangledName(Name.begin(), Name.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *CodeGenModule::UniqueMangledName(const char *NameStart,
|
|
|
|
const char *NameEnd) {
|
|
|
|
assert(*(NameEnd - 1) == '\0' && "Mangled name must be null terminated!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-15 23:55:24 +08:00
|
|
|
return MangledNames.GetOrCreateValue(NameStart, NameEnd).getKeyData();
|
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()*.
|
|
|
|
llvm::FunctionType* CtorFTy =
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
2008-08-01 08:01:51 +08:00
|
|
|
std::vector<const llvm::Type*>(),
|
|
|
|
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 ()* }.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::StructType* CtorStructTy =
|
|
|
|
llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext),
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::getUnqual(CtorFTy), NULL);
|
2008-08-01 08:01:51 +08:00
|
|
|
|
|
|
|
// Construct the constructor and destructor arrays.
|
|
|
|
std::vector<llvm::Constant*> Ctors;
|
|
|
|
for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
|
|
|
|
std::vector<llvm::Constant*> S;
|
2009-09-09 23:08:12 +08:00
|
|
|
S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2009-08-14 05:57:51 +08:00
|
|
|
I->second, false));
|
2009-07-30 02:54:39 +08:00
|
|
|
S.push_back(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 07:43:57 +08:00
|
|
|
void CodeGenModule::EmitAnnotations() {
|
|
|
|
if (Annotations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a new global variable for the ConstantStruct in the Module.
|
|
|
|
llvm::Constant *Array =
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(),
|
2008-04-19 07:43:57 +08:00
|
|
|
Annotations.size()),
|
|
|
|
Annotations);
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalValue *gv =
|
|
|
|
new llvm::GlobalVariable(TheModule, Array->getType(), false,
|
|
|
|
llvm::GlobalValue::AppendingLinkage, Array,
|
2009-07-09 03:05:04 +08:00
|
|
|
"llvm.global.annotations");
|
2008-04-19 07:43:57 +08:00
|
|
|
gv->setSection("llvm.metadata");
|
|
|
|
}
|
|
|
|
|
2009-04-15 00:44:36 +08:00
|
|
|
static CodeGenModule::GVALinkage
|
2009-09-09 23:08:12 +08:00
|
|
|
GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD,
|
2009-06-19 00:11:24 +08:00
|
|
|
const LangOptions &Features) {
|
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
|
|
|
CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal;
|
2009-10-01 08:25:31 +08:00
|
|
|
|
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
|
|
|
Linkage L = FD->getLinkage();
|
|
|
|
if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus &&
|
|
|
|
FD->getType()->getLinkage() == UniqueExternalLinkage)
|
|
|
|
L = UniqueExternalLinkage;
|
2009-10-28 07:26:40 +08:00
|
|
|
|
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
|
|
|
switch (L) {
|
|
|
|
case NoLinkage:
|
|
|
|
case InternalLinkage:
|
|
|
|
case UniqueExternalLinkage:
|
|
|
|
return CodeGenModule::GVA_Internal;
|
2009-12-11 06:25:34 +08:00
|
|
|
|
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
|
|
|
case ExternalLinkage:
|
|
|
|
switch (FD->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
External = CodeGenModule::GVA_StrongExternal;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
// FIXME: explicit instantiation definitions should use weak linkage
|
2009-12-11 06:25:34 +08:00
|
|
|
return CodeGenModule::GVA_StrongExternal;
|
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
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
case TSK_ImplicitInstantiation:
|
2009-12-11 06:25:34 +08:00
|
|
|
External = CodeGenModule::GVA_TemplateInstantiation;
|
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
|
|
|
break;
|
|
|
|
}
|
2009-12-11 06:25:34 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-28 05:11:48 +08:00
|
|
|
if (!FD->isInlined())
|
2009-06-30 06:39:32 +08:00
|
|
|
return External;
|
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
|
|
|
|
2009-09-13 15:46:26 +08:00
|
|
|
if (!Features.CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
|
|
|
|
// GNU or C99 inline semantics. Determine whether this symbol should be
|
|
|
|
// externally visible.
|
|
|
|
if (FD->isInlineDefinitionExternallyVisible())
|
|
|
|
return External;
|
2009-04-15 04:25:53 +08:00
|
|
|
|
2009-09-13 15:46:26 +08:00
|
|
|
// C99 inline semantics, where the symbol is not externally visible.
|
2009-04-24 02:22:55 +08:00
|
|
|
return CodeGenModule::GVA_C99Inline;
|
2009-09-13 15:46:26 +08:00
|
|
|
}
|
2009-04-24 02:22:55 +08:00
|
|
|
|
2009-10-28 07:26:40 +08:00
|
|
|
// C++0x [temp.explicit]p9:
|
|
|
|
// [ Note: The intent is that an inline function that is the subject of
|
|
|
|
// an explicit instantiation declaration will still be implicitly
|
|
|
|
// instantiated when used so that the body can be considered for
|
|
|
|
// inlining, but that no out-of-line copy of the inline function would be
|
|
|
|
// generated in the translation unit. -- end note ]
|
|
|
|
if (FD->getTemplateSpecializationKind()
|
|
|
|
== TSK_ExplicitInstantiationDeclaration)
|
|
|
|
return CodeGenModule::GVA_C99Inline;
|
|
|
|
|
2009-09-13 15:46:26 +08:00
|
|
|
return CodeGenModule::GVA_CXXInline;
|
2009-04-14 16:05:55 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 09:32:20 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes
|
|
|
|
CodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
|
2009-06-19 00:11:24 +08:00
|
|
|
GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2009-04-14 15:19:20 +08:00
|
|
|
if (Linkage == GVA_Internal) {
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::InternalLinkage;
|
2009-06-30 10:34:44 +08:00
|
|
|
} else if (D->hasAttr<DLLExportAttr>()) {
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::DLLExportLinkage;
|
2009-06-30 10:34:44 +08:00
|
|
|
} else if (D->hasAttr<WeakAttr>()) {
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::WeakAnyLinkage;
|
2009-04-15 04:25:53 +08:00
|
|
|
} else if (Linkage == GVA_C99Inline) {
|
|
|
|
// In C99 mode, 'inline' functions are guaranteed to have a strong
|
|
|
|
// definition somewhere else, so we can use available_externally linkage.
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::AvailableExternallyLinkage;
|
2009-06-30 06:39:32 +08:00
|
|
|
} else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) {
|
2009-04-15 00:44:36 +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.
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::LinkOnceODRLinkage;
|
2009-04-14 16:05:55 +08:00
|
|
|
} else {
|
2009-04-15 04:25:53 +08:00
|
|
|
assert(Linkage == GVA_StrongExternal);
|
|
|
|
// Otherwise, we have strong external linkage.
|
2010-02-19 09:32:20 +08:00
|
|
|
return llvm::Function::ExternalLinkage;
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
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) {
|
|
|
|
GV->setLinkage(getFunctionLinkage(D));
|
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;
|
2009-09-12 08:59:20 +08:00
|
|
|
ConstructAttributeList(Info, D, AttributeList, CallingConv);
|
2008-09-26 05:02:23 +08:00
|
|
|
F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
|
2009-09-12 08:59:20 +08:00
|
|
|
AttributeList.size()));
|
|
|
|
F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
|
|
|
|
llvm::Function *F) {
|
2009-03-02 12:58:03 +08:00
|
|
|
if (!Features.Exceptions && !Features.ObjCNonFragileABI)
|
2009-09-09 23:08:12 +08:00
|
|
|
F->addFnAttr(llvm::Attribute::NoUnwind);
|
2008-10-28 08:17:57 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (D->hasAttr<AlwaysInlineAttr>())
|
2008-10-28 08:17:57 +08:00
|
|
|
F->addFnAttr(llvm::Attribute::AlwaysInline);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-27 06:31:08 +08:00
|
|
|
if (D->hasAttr<NoInlineAttr>())
|
2009-02-20 03:22:11 +08:00
|
|
|
F->addFnAttr(llvm::Attribute::NoInline);
|
2009-10-06 05:58:44 +08:00
|
|
|
|
2009-11-17 00:56:03 +08:00
|
|
|
if (Features.getStackProtectorMode() == LangOptions::SSPOn)
|
|
|
|
F->addFnAttr(llvm::Attribute::StackProtect);
|
|
|
|
else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
|
|
|
|
F->addFnAttr(llvm::Attribute::StackProtectReq);
|
|
|
|
|
2009-11-21 16:43:09 +08:00
|
|
|
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) {
|
|
|
|
unsigned width = Context.Target.getCharWidth();
|
|
|
|
F->setAlignment(AA->getAlignment() / width);
|
|
|
|
while ((AA = AA->getNext<AlignedAttr>()))
|
|
|
|
F->setAlignment(std::max(F->getAlignment(), AA->getAlignment() / width));
|
|
|
|
}
|
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) {
|
|
|
|
setGlobalVisibility(GV, D);
|
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
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)
|
2010-02-06 10:44:09 +08:00
|
|
|
SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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>() ||
|
2009-06-30 10:34:44 +08:00
|
|
|
FD->hasAttr<WeakImportAttr>()) {
|
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);
|
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());
|
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-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 06:37:52 +08:00
|
|
|
// Convert LLVMUsed to what ConstantArray needs.
|
|
|
|
std::vector<llvm::Constant*> UsedArray;
|
|
|
|
UsedArray.resize(LLVMUsed.size());
|
|
|
|
for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) {
|
2009-09-09 23:08:12 +08:00
|
|
|
UsedArray[i] =
|
|
|
|
llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]),
|
2009-07-15 07:10:40 +08:00
|
|
|
i8PTy);
|
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;
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// for a static function, iterate until no changes are made.
|
|
|
|
while (!DeferredDeclsToEmit.empty()) {
|
2009-05-05 12:44:02 +08:00
|
|
|
GlobalDecl D = DeferredDeclsToEmit.back();
|
2009-03-21 17:44:56 +08:00
|
|
|
DeferredDeclsToEmit.pop_back();
|
|
|
|
|
|
|
|
// The mangled name for the decl must have been emitted in GlobalDeclMap.
|
|
|
|
// Look it up to see if it was defined with a stronger definition (e.g. an
|
|
|
|
// extern inline function with a strong function redefinition). If so,
|
|
|
|
// just ignore the deferred decl.
|
|
|
|
llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)];
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the
|
2008-04-19 12:17:09 +08:00
|
|
|
/// annotation information for a given GlobalValue. The annotation struct is
|
2009-09-09 23:08:12 +08:00
|
|
|
/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the
|
|
|
|
/// GlobalValue being annotated. The second field is the constant string
|
|
|
|
/// created from the AnnotateAttr's annotation. The third field is a constant
|
2008-04-19 12:17:09 +08:00
|
|
|
/// string containing the name of the translation unit. The fourth field is
|
|
|
|
/// the line number in the file of the annotated value declaration.
|
|
|
|
///
|
|
|
|
/// FIXME: this does not unique the annotation string constants, as llvm-gcc
|
|
|
|
/// appears to.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
|
2008-04-19 12:17:09 +08:00
|
|
|
const AnnotateAttr *AA,
|
|
|
|
unsigned LineNo) {
|
|
|
|
llvm::Module *M = &getModule();
|
|
|
|
|
|
|
|
// get [N x i8] constants for the annotation string, and the filename string
|
|
|
|
// which are the 2nd and 3rd elements of the global annotation structure.
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext);
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *anno = llvm::ConstantArray::get(VMContext,
|
2009-08-14 05:57:51 +08:00
|
|
|
AA->getAnnotation(), true);
|
|
|
|
llvm::Constant *unit = llvm::ConstantArray::get(VMContext,
|
|
|
|
M->getModuleIdentifier(),
|
2008-04-19 12:17:09 +08:00
|
|
|
true);
|
|
|
|
|
|
|
|
// Get the two global values corresponding to the ConstantArrays we just
|
|
|
|
// created to hold the bytes of the strings.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalValue *annoGV =
|
2009-07-16 13:03:48 +08:00
|
|
|
new llvm::GlobalVariable(*M, anno->getType(), false,
|
|
|
|
llvm::GlobalValue::PrivateLinkage, anno,
|
|
|
|
GV->getName());
|
2008-04-19 12:17:09 +08:00
|
|
|
// translation unit name string, emitted into the llvm.metadata section.
|
|
|
|
llvm::GlobalValue *unitGV =
|
2009-07-16 13:03:48 +08:00
|
|
|
new llvm::GlobalVariable(*M, unit->getType(), false,
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalValue::PrivateLinkage, unit,
|
2009-07-16 13:03:48 +08:00
|
|
|
".str");
|
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] = {
|
2009-07-30 02:54:39 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, SBP),
|
|
|
|
llvm::ConstantExpr::getBitCast(annoGV, SBP),
|
|
|
|
llvm::ConstantExpr::getBitCast(unitGV, SBP),
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo)
|
2008-04-19 12:17:09 +08:00
|
|
|
};
|
2009-08-06 07:18:46 +08:00
|
|
|
return llvm::ConstantStruct::get(VMContext, Fields, 4, false);
|
2008-04-19 12:17:09 +08:00
|
|
|
}
|
|
|
|
|
2009-02-14 05:18:01 +08:00
|
|
|
bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
|
2009-02-14 06:08:43 +08:00
|
|
|
// Never defer when EmitAllDecls is specified or the decl has
|
|
|
|
// attribute used.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>())
|
2009-02-14 05:18:01 +08:00
|
|
|
return false;
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2009-02-14 05:18:01 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
|
|
|
|
// Constructors and destructors should never be deferred.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (FD->hasAttr<ConstructorAttr>() ||
|
2009-06-30 10:34:44 +08:00
|
|
|
FD->hasAttr<DestructorAttr>())
|
2009-02-14 05:18:01 +08:00
|
|
|
return false;
|
|
|
|
|
2009-12-08 11:56:49 +08:00
|
|
|
// The key function for a class must never be deferred.
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Global)) {
|
|
|
|
const CXXRecordDecl *RD = MD->getParent();
|
|
|
|
if (MD->isOutOfLine() && RD->isDynamicClass()) {
|
|
|
|
const CXXMethodDecl *KeyFunction = getContext().getKeyFunction(RD);
|
2010-01-06 03:06:31 +08:00
|
|
|
if (KeyFunction &&
|
|
|
|
KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl())
|
2009-12-08 11:56:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-19 00:11:24 +08:00
|
|
|
GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-14 14:44:48 +08:00
|
|
|
// static, static inline, always_inline, and extern inline functions can
|
2009-04-15 00:44:36 +08:00
|
|
|
// always be deferred. Normal inline functions can be deferred in C99/C++.
|
2009-04-15 04:25:53 +08:00
|
|
|
if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
|
2009-12-25 13:29:40 +08:00
|
|
|
Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
|
2009-04-14 14:44:48 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
2009-02-14 05:18:01 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-14 14:44:48 +08:00
|
|
|
const VarDecl *VD = cast<VarDecl>(Global);
|
|
|
|
assert(VD->isFileVarDecl() && "Invalid decl");
|
2009-04-22 01:11:58 +08:00
|
|
|
|
2009-10-09 01:28:59 +08:00
|
|
|
// We never want to defer structs that have non-trivial constructors or
|
|
|
|
// destructors.
|
|
|
|
|
|
|
|
// FIXME: Handle references.
|
|
|
|
if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
|
|
if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 05:29:40 +08:00
|
|
|
// Static data may be deferred, but out-of-line static data members
|
|
|
|
// cannot be.
|
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
|
|
|
Linkage L = VD->getLinkage();
|
|
|
|
if (L == ExternalLinkage && getContext().getLangOptions().CPlusPlus &&
|
|
|
|
VD->getType()->getLinkage() == UniqueExternalLinkage)
|
|
|
|
L = UniqueExternalLinkage;
|
|
|
|
|
|
|
|
switch (L) {
|
|
|
|
case NoLinkage:
|
|
|
|
case InternalLinkage:
|
|
|
|
case UniqueExternalLinkage:
|
2009-11-06 02:03:03 +08:00
|
|
|
// Initializer has side effects?
|
|
|
|
if (VD->getInit() && VD->getInit()->HasSideEffects(Context))
|
|
|
|
return false;
|
|
|
|
return !(VD->isStaticDataMember() && VD->isOutOfLine());
|
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
|
|
|
|
|
|
|
case ExternalLinkage:
|
|
|
|
break;
|
2009-11-06 02:03:03 +08:00
|
|
|
}
|
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
|
|
|
|
2009-11-06 02:03:03 +08:00
|
|
|
return false;
|
2009-02-14 05:18:01 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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>())
|
2009-03-23 05:47:11 +08:00
|
|
|
return EmitAliasDefinition(Global);
|
2008-09-09 07:44:31 +08:00
|
|
|
|
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.
|
|
|
|
if (!FD->isThisDeclarationADefinition())
|
|
|
|
return;
|
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.
|
2009-02-14 05:18:01 +08:00
|
|
|
if (MayDeferGeneration(Global)) {
|
2009-03-21 17:44:56 +08:00
|
|
|
// If the value has already been used, add it directly to the
|
|
|
|
// DeferredDeclsToEmit list.
|
2009-05-05 12:44:02 +08:00
|
|
|
const char *MangledName = getMangledName(GD);
|
2009-03-21 17:44:56 +08:00
|
|
|
if (GlobalDeclMap.count(MangledName))
|
2009-05-05 12:44:02 +08:00
|
|
|
DeferredDeclsToEmit.push_back(GD);
|
2009-03-21 17:44:56 +08:00
|
|
|
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.
|
2009-05-05 12:44:02 +08:00
|
|
|
DeferredDecls[MangledName] = GD;
|
2009-03-21 17:44:56 +08:00
|
|
|
}
|
2008-07-30 07:18:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise emit the definition.
|
2009-05-05 12:44:02 +08:00
|
|
|
EmitGlobalDefinition(GD);
|
2008-04-20 14:29:50 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-10-27 22:32:27 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo((ValueDecl *)D, D->getLocation(),
|
|
|
|
Context.getSourceManager(),
|
|
|
|
"Generating code for declaration");
|
|
|
|
|
2009-12-07 06:01:30 +08:00
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
|
2009-12-01 07:41:22 +08:00
|
|
|
getVtableInfo().MaybeEmitVtable(GD);
|
2009-12-07 06:01:30 +08:00
|
|
|
if (MD->isVirtual() && MD->isOutOfLine() &&
|
|
|
|
(!isa<CXXDestructorDecl>(D) || GD.getDtorType() != Dtor_Base)) {
|
2009-12-08 07:56:34 +08:00
|
|
|
if (isa<CXXDestructorDecl>(D)) {
|
|
|
|
GlobalDecl CanonGD(cast<CXXDestructorDecl>(D->getCanonicalDecl()),
|
|
|
|
GD.getDtorType());
|
|
|
|
BuildThunksForVirtual(CanonGD);
|
|
|
|
} else {
|
|
|
|
BuildThunksForVirtual(MD->getCanonicalDecl());
|
|
|
|
}
|
2009-12-07 06:01:30 +08:00
|
|
|
}
|
|
|
|
}
|
2009-12-01 07:41:22 +08:00
|
|
|
|
2009-05-05 12:44:02 +08:00
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
|
|
|
|
EmitCXXConstructor(CD, GD.getCtorType());
|
2009-05-30 05:03:38 +08:00
|
|
|
else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D))
|
|
|
|
EmitCXXDestructor(DD, GD.getDtorType());
|
2009-05-13 05:21:08 +08:00
|
|
|
else if (isa<FunctionDecl>(D))
|
|
|
|
EmitGlobalFunctionDefinition(GD);
|
2009-05-05 12:44:02 +08:00
|
|
|
else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
2008-07-30 07:18:29 +08:00
|
|
|
EmitGlobalVarDefinition(VD);
|
2009-05-05 12:44:02 +08:00
|
|
|
else {
|
2008-07-30 07:18:29 +08:00
|
|
|
assert(0 && "Invalid argument to EmitGlobalDefinition()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName,
|
|
|
|
const llvm::Type *Ty,
|
2009-05-13 05:21:08 +08:00
|
|
|
GlobalDecl D) {
|
2009-03-21 17:25:43 +08:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
|
|
|
llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
|
|
|
|
if (Entry) {
|
|
|
|
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.
|
2009-07-30 06:16:19 +08:00
|
|
|
const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, PTy);
|
2009-03-21 17:25:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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;
|
|
|
|
if (!isa<llvm::FunctionType>(Ty)) {
|
|
|
|
Ty = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
|
|
|
std::vector<const llvm::Type*>(), false);
|
|
|
|
IsIncompleteFunction = true;
|
|
|
|
}
|
|
|
|
llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty),
|
|
|
|
llvm::Function::ExternalLinkage,
|
|
|
|
"", &getModule());
|
|
|
|
F->setName(MangledName);
|
|
|
|
if (D.getDecl())
|
2010-02-06 10:44:09 +08:00
|
|
|
SetFunctionAttributes(D, F, IsIncompleteFunction);
|
2009-11-09 13:07:37 +08:00
|
|
|
Entry = F;
|
|
|
|
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::DenseMap<const char*, GlobalDecl>::iterator DDI =
|
2009-05-13 04:58:15 +08:00
|
|
|
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-05-13 05:21:08 +08:00
|
|
|
} else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) {
|
2009-05-13 05:02:27 +08:00
|
|
|
// If this the first reference to a C++ inline function in a class, queue up
|
|
|
|
// the deferred function body for emission. These are not seen as
|
|
|
|
// top-level declarations.
|
2009-05-13 05:21:08 +08:00
|
|
|
if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD))
|
|
|
|
DeferredDeclsToEmit.push_back(D);
|
2009-07-31 07:22:00 +08:00
|
|
|
// A called constructor which has no definition or declaration need be
|
|
|
|
// synthesized.
|
|
|
|
else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
|
2010-03-03 05:28:26 +08:00
|
|
|
if (CD->isImplicit()) {
|
|
|
|
assert (CD->isUsed());
|
2009-11-26 15:40:08 +08:00
|
|
|
DeferredDeclsToEmit.push_back(D);
|
2010-03-03 05:28:26 +08:00
|
|
|
}
|
2009-11-26 15:40:08 +08:00
|
|
|
} else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
|
2010-03-03 05:28:26 +08:00
|
|
|
if (DD->isImplicit()) {
|
|
|
|
assert (DD->isUsed());
|
2009-11-26 15:40:08 +08:00
|
|
|
DeferredDeclsToEmit.push_back(D);
|
2010-03-03 05:28:26 +08:00
|
|
|
}
|
2009-11-26 15:40:08 +08:00
|
|
|
} else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
2010-03-03 05:28:26 +08:00
|
|
|
if (MD->isCopyAssignment() && MD->isImplicit()) {
|
|
|
|
assert (MD->isUsed());
|
2009-07-31 07:22:00 +08:00
|
|
|
DeferredDeclsToEmit.push_back(D);
|
2010-03-03 05:28:26 +08:00
|
|
|
}
|
2009-07-31 07:22:00 +08:00
|
|
|
}
|
2009-03-21 17:44:56 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
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,
|
2009-03-23 05:03:39 +08:00
|
|
|
const llvm::Type *Ty) {
|
|
|
|
// 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());
|
2009-09-05 03:04:08 +08:00
|
|
|
return GetOrCreateLLVMFunction(getMangledName(GD), Ty, GD);
|
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 *
|
|
|
|
CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
|
|
|
|
const char *Name) {
|
|
|
|
// Convert Name to be a uniqued string from the IdentifierInfo table.
|
2009-10-19 04:26:12 +08:00
|
|
|
Name = getContext().Idents.get(Name).getNameStart();
|
2009-05-13 05:21:08 +08:00
|
|
|
return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl());
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2009-12-12 05:23:03 +08:00
|
|
|
static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) {
|
2010-02-09 05:46:50 +08:00
|
|
|
if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType())
|
2009-12-12 05:23:03 +08:00
|
|
|
return false;
|
|
|
|
if (Context.getLangOptions().CPlusPlus &&
|
|
|
|
Context.getBaseElementType(D->getType())->getAs<RecordType>()) {
|
|
|
|
// FIXME: We should do something fancier here!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
llvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName,
|
|
|
|
const llvm::PointerType*Ty,
|
|
|
|
const VarDecl *D) {
|
2008-08-06 07:31:02 +08:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2009-03-21 16:06:59 +08:00
|
|
|
llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
|
2009-03-21 16:03:33 +08:00
|
|
|
if (Entry) {
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::DenseMap<const char*, GlobalDecl>::iterator DDI =
|
2009-03-21 17:44:56 +08:00
|
|
|
DeferredDecls.find(MangledName);
|
|
|
|
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
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
|
2009-03-21 16:03:33 +08:00
|
|
|
llvm::GlobalValue::ExternalLinkage,
|
2009-09-09 23:08:12 +08:00
|
|
|
0, "", 0,
|
2009-04-20 05:05:03 +08:00
|
|
|
false, Ty->getAddressSpace());
|
2009-03-22 08:12:30 +08:00
|
|
|
GV->setName(MangledName);
|
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.
|
2009-12-12 05:23:03 +08:00
|
|
|
GV->setConstant(DeclIsConstantGlobal(Context, D));
|
2009-01-13 10:25:00 +08:00
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
// FIXME: Merge with other attribute handling code.
|
|
|
|
if (D->getStorageClass() == VarDecl::PrivateExtern)
|
2009-04-14 14:00:08 +08:00
|
|
|
GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
2009-01-13 10:25:00 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (D->hasAttr<WeakAttr>() ||
|
2009-06-30 10:34:44 +08:00
|
|
|
D->hasAttr<WeakImportAttr>())
|
2009-03-23 05:03:39 +08:00
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
|
2009-04-20 05:05:03 +08:00
|
|
|
|
|
|
|
GV->setThreadLocal(D->isThreadSpecified());
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
return Entry = GV;
|
|
|
|
}
|
2009-02-21 08:24:10 +08:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
/// then it will be greated with the specified type instead of whatever the
|
|
|
|
/// normal requested type would be.
|
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
|
|
|
|
const llvm::Type *Ty) {
|
|
|
|
assert(D->hasGlobalStorage() && "Not a global variable");
|
|
|
|
QualType ASTTy = D->getType();
|
|
|
|
if (Ty == 0)
|
|
|
|
Ty = getTypes().ConvertTypeForMem(ASTTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const llvm::PointerType *PTy =
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::get(Ty, ASTTy.getAddressSpace());
|
2009-03-23 05:03:39 +08:00
|
|
|
return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D);
|
|
|
|
}
|
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 *
|
|
|
|
CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty,
|
|
|
|
const char *Name) {
|
|
|
|
// Convert Name to be a uniqued string from the IdentifierInfo table.
|
2009-10-19 04:26:12 +08:00
|
|
|
Name = getContext().Idents.get(Name).getNameStart();
|
2009-07-30 06:16:19 +08:00
|
|
|
return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0);
|
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.
|
|
|
|
const char *MangledName = getMangledName(D);
|
|
|
|
if (GlobalDeclMap.count(MangledName) == 0) {
|
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);
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:27:16 +08:00
|
|
|
llvm::GlobalVariable::LinkageTypes
|
|
|
|
CodeGenModule::getVtableLinkage(const CXXRecordDecl *RD) {
|
2010-01-07 06:00:56 +08:00
|
|
|
if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
|
|
|
|
return llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *KeyFunction
|
|
|
|
= RD->getASTContext().getKeyFunction(RD)) {
|
|
|
|
// If this class has a key function, use that to determine the linkage of
|
|
|
|
// the vtable.
|
2010-01-07 04:27:16 +08:00
|
|
|
const FunctionDecl *Def = 0;
|
|
|
|
if (KeyFunction->getBody(Def))
|
|
|
|
KeyFunction = cast<CXXMethodDecl>(Def);
|
2010-01-07 06:00:56 +08:00
|
|
|
|
2010-01-07 04:27:16 +08:00
|
|
|
switch (KeyFunction->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
if (KeyFunction->isInlined())
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
|
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
// FIXME: Use available_externally linkage. However, this currently
|
|
|
|
// breaks LLVM's build due to undefined symbols.
|
|
|
|
// return llvm::GlobalVariable::AvailableExternallyLinkage;
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
}
|
2010-01-07 06:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (RD->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
|
|
|
// FIXME: Use available_externally linkage. However, this currently
|
|
|
|
// breaks LLVM's build due to undefined symbols.
|
|
|
|
// return llvm::GlobalVariable::AvailableExternallyLinkage;
|
2010-01-07 04:27:16 +08:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Silence GCC warning.
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
}
|
|
|
|
|
2009-10-15 05:29:40 +08:00
|
|
|
static CodeGenModule::GVALinkage
|
|
|
|
GetLinkageForVariable(ASTContext &Context, const VarDecl *VD) {
|
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
|
|
|
// If this is a static data member, compute the kind of template
|
|
|
|
// specialization. Otherwise, this variable is not part of a
|
|
|
|
// template.
|
|
|
|
TemplateSpecializationKind TSK = TSK_Undeclared;
|
|
|
|
if (VD->isStaticDataMember())
|
|
|
|
TSK = VD->getTemplateSpecializationKind();
|
|
|
|
|
|
|
|
Linkage L = VD->getLinkage();
|
|
|
|
if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus &&
|
|
|
|
VD->getType()->getLinkage() == UniqueExternalLinkage)
|
|
|
|
L = UniqueExternalLinkage;
|
|
|
|
|
|
|
|
switch (L) {
|
|
|
|
case NoLinkage:
|
|
|
|
case InternalLinkage:
|
|
|
|
case UniqueExternalLinkage:
|
2009-10-15 05:29:40 +08:00
|
|
|
return CodeGenModule::GVA_Internal;
|
|
|
|
|
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
|
|
|
case ExternalLinkage:
|
|
|
|
switch (TSK) {
|
2009-10-15 05:29:40 +08:00
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
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
|
|
|
|
|
|
|
// FIXME: ExplicitInstantiationDefinition should be weak!
|
2009-10-15 05:29:40 +08:00
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return CodeGenModule::GVA_StrongExternal;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
2009-12-12 13:05:38 +08:00
|
|
|
llvm_unreachable("Variable should not be instantiated");
|
2009-10-15 05:29:40 +08:00
|
|
|
// Fall through to treat this like any other instantiation.
|
|
|
|
|
|
|
|
case TSK_ImplicitInstantiation:
|
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
|
|
|
return CodeGenModule::GVA_TemplateInstantiation;
|
2009-10-15 05:29:40 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-26 10:52:12 +08:00
|
|
|
|
2009-10-15 05:29:40 +08:00
|
|
|
return CodeGenModule::GVA_StrongExternal;
|
|
|
|
}
|
|
|
|
|
2010-01-26 21:48:07 +08:00
|
|
|
CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const {
|
|
|
|
return CharUnits::fromQuantity(
|
|
|
|
TheTargetData.getTypeStoreSizeInBits(Ty) / Context.getCharWidth());
|
|
|
|
}
|
|
|
|
|
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();
|
2010-01-08 08:50:11 +08:00
|
|
|
bool NonConstInit = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-02 04:16:42 +08:00
|
|
|
const Expr *InitExpr = D->getAnyInitializer();
|
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 {
|
2010-01-27 01:43:42 +08:00
|
|
|
Init = EmitConstantExpr(InitExpr, D->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-20 09:18:21 +08:00
|
|
|
if (!Init) {
|
2010-01-27 01:43:42 +08:00
|
|
|
QualType T = InitExpr->getType();
|
2009-08-09 07:24:23 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
2010-01-08 08:50:11 +08:00
|
|
|
EmitCXXGlobalVarDeclInitFunc(D);
|
2009-08-09 07:24:23 +08:00
|
|
|
Init = EmitNullConstant(T);
|
2010-01-08 08:50:11 +08:00
|
|
|
NonConstInit = true;
|
2009-08-09 07:24:23 +08:00
|
|
|
} else {
|
|
|
|
ErrorUnsupported(D, "static initializer");
|
|
|
|
Init = llvm::UndefValue::get(getTypes().ConvertType(T));
|
|
|
|
}
|
2009-02-20 09:18:21 +08:00
|
|
|
}
|
2007-07-14 08:23:28 +08:00
|
|
|
}
|
2007-10-27 00:31:40 +08:00
|
|
|
|
2009-03-21 16:13:05 +08:00
|
|
|
const 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 ||
|
|
|
|
GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// Remove the old entry from GlobalDeclMap so that we'll create a new one.
|
|
|
|
GlobalDeclMap.erase(getMangledName(D));
|
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
|
|
|
GV->takeName(cast<llvm::GlobalValue>(Entry));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) {
|
2008-04-19 12:17:09 +08:00
|
|
|
SourceManager &SM = Context.getSourceManager();
|
|
|
|
AddAnnotation(EmitAnnotateAttr(GV, AA,
|
2009-01-16 15:36:28 +08:00
|
|
|
SM.getInstantiationLineNumber(D->getLocation())));
|
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.
|
|
|
|
GV->setConstant(false);
|
2010-01-08 08:50:11 +08:00
|
|
|
if (!NonConstInit && DeclIsConstantGlobal(Context, D))
|
2009-08-05 13:20:29 +08:00
|
|
|
GV->setConstant(true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-28 01:10:57 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
|
2008-05-29 19:10:27 +08:00
|
|
|
|
2007-07-13 13:13:43 +08:00
|
|
|
// Set the llvm linkage type as appropriate.
|
2009-10-15 05:29:40 +08:00
|
|
|
GVALinkage Linkage = GetLinkageForVariable(getContext(), D);
|
2009-10-15 05:36:34 +08:00
|
|
|
if (Linkage == GVA_Internal)
|
2008-05-04 09:44:26 +08:00
|
|
|
GV->setLinkage(llvm::Function::InternalLinkage);
|
2009-06-30 10:34:44 +08:00
|
|
|
else if (D->hasAttr<DLLImportAttr>())
|
2008-03-03 11:28:21 +08:00
|
|
|
GV->setLinkage(llvm::Function::DLLImportLinkage);
|
2009-06-30 10:34:44 +08:00
|
|
|
else if (D->hasAttr<DLLExportAttr>())
|
2008-03-03 11:28:21 +08:00
|
|
|
GV->setLinkage(llvm::Function::DLLExportLinkage);
|
2009-08-05 13:20:29 +08:00
|
|
|
else if (D->hasAttr<WeakAttr>()) {
|
|
|
|
if (GV->isConstant())
|
|
|
|
GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage);
|
|
|
|
else
|
|
|
|
GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
|
2009-10-15 05:29:40 +08:00
|
|
|
} else if (Linkage == GVA_TemplateInstantiation)
|
|
|
|
GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
|
2009-11-30 16:40:34 +08:00
|
|
|
else if (!getLangOptions().CPlusPlus && !CodeGenOpts.NoCommon &&
|
2009-08-05 12:56:58 +08:00
|
|
|
!D->hasExternalStorage() && !D->getInit() &&
|
2009-08-05 13:20:29 +08:00
|
|
|
!D->getAttr<SectionAttr>()) {
|
2009-04-14 14:00:08 +08:00
|
|
|
GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
|
2009-08-05 13:20:29 +08:00
|
|
|
// common vars aren't constant even if declared const.
|
|
|
|
GV->setConstant(false);
|
|
|
|
} else
|
2009-04-14 14:00:08 +08:00
|
|
|
GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
|
2009-04-11 04:26:50 +08:00
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
SetCommonAttributes(D, GV);
|
2009-04-14 14:00:08 +08:00
|
|
|
|
2008-06-05 16:59:10 +08:00
|
|
|
// Emit global variable debug information.
|
2009-03-21 16:13:05 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D->getLocation());
|
2008-06-05 16:59:10 +08:00
|
|
|
DI->EmitGlobalVariable(GV, D);
|
|
|
|
}
|
2007-07-13 13:13:43 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2009-05-05 14:16:31 +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.
|
|
|
|
llvm::Function *OldFn = dyn_cast<llvm::Function>(Old);
|
|
|
|
if (OldFn == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-05 14:16:31 +08:00
|
|
|
const llvm::Type *NewRetTy = NewFn->getReturnType();
|
|
|
|
llvm::SmallVector<llvm::Value*, 4> ArgList;
|
|
|
|
|
|
|
|
for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end();
|
|
|
|
UI != E; ) {
|
|
|
|
// TODO: Do invokes ever occur in C code? If so, we should handle them too.
|
2009-06-05 00:47:43 +08:00
|
|
|
unsigned OpNo = UI.getOperandNo();
|
2009-05-05 14:16:31 +08:00
|
|
|
llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*UI++);
|
2009-06-05 00:47:43 +08:00
|
|
|
if (!CI || OpNo != 0) continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-05 14:16:31 +08:00
|
|
|
// If the return types don't match exactly, and if the call isn't dead, then
|
|
|
|
// we can't transform this call.
|
|
|
|
if (CI->getType() != NewRetTy && !CI->use_empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the function was passed too few arguments, don't transform. If extra
|
|
|
|
// arguments were passed, we silently drop them. If any of the types
|
|
|
|
// mismatch, we don't transform.
|
|
|
|
unsigned ArgNo = 0;
|
|
|
|
bool DontTransform = false;
|
|
|
|
for (llvm::Function::arg_iterator AI = NewFn->arg_begin(),
|
|
|
|
E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) {
|
|
|
|
if (CI->getNumOperands()-1 == ArgNo ||
|
|
|
|
CI->getOperand(ArgNo+1)->getType() != AI->getType()) {
|
|
|
|
DontTransform = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DontTransform)
|
|
|
|
continue;
|
2009-09-09 23:08:12 +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.
|
|
|
|
ArgList.append(CI->op_begin()+1, CI->op_begin()+1+ArgNo);
|
|
|
|
llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(),
|
|
|
|
ArgList.end(), "", CI);
|
|
|
|
ArgList.clear();
|
2009-10-05 21:47:21 +08:00
|
|
|
if (!NewCall->getType()->isVoidTy())
|
2009-05-05 14:16:31 +08:00
|
|
|
NewCall->takeName(CI);
|
|
|
|
NewCall->setAttributes(CI->getAttributes());
|
2009-09-12 08:59:20 +08:00
|
|
|
NewCall->setCallingConv(CI->getCallingConv());
|
2009-05-05 14:16:31 +08:00
|
|
|
|
|
|
|
// Finally, remove the old call, replacing any uses with the new one.
|
2009-10-14 13:49:21 +08:00
|
|
|
if (!CI->use_empty())
|
|
|
|
CI->replaceAllUsesWith(NewCall);
|
2009-10-14 01:02:04 +08:00
|
|
|
|
2009-10-15 01:03:29 +08:00
|
|
|
// Copy any custom metadata attached with CI.
|
2009-12-29 15:49:13 +08:00
|
|
|
if (llvm::MDNode *DbgNode = CI->getMetadata("dbg"))
|
|
|
|
NewCall->setMetadata("dbg", DbgNode);
|
2009-05-05 14:16:31 +08:00
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2010-02-23 08:48:20 +08:00
|
|
|
const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD);
|
2009-02-19 15:15:39 +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
|
|
|
|
// (e.g. "int f(int x)"). Start by making a new function of the
|
|
|
|
// correct type, RAUW, then steal the name.
|
2009-03-21 16:53:37 +08:00
|
|
|
GlobalDeclMap.erase(getMangledName(D));
|
2009-05-13 05:21:08 +08:00
|
|
|
llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
|
2009-05-05 14:16:31 +08:00
|
|
|
NewFn->takeName(OldFn);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-05 14:16:31 +08:00
|
|
|
// If this is an implementation of a function without a prototype, try to
|
|
|
|
// replace any existing uses of the function (which may be calls) with uses
|
|
|
|
// of the new function
|
2009-05-13 04:58:15 +08:00
|
|
|
if (D->getType()->isFunctionNoProtoType()) {
|
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
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
llvm::Function *Fn = cast<llvm::Function>(Entry);
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2008-09-09 07:44:31 +08:00
|
|
|
CodeGenFunction(*this).GenerateCode(D, Fn);
|
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());
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
void CodeGenModule::EmitAliasDefinition(const ValueDecl *D) {
|
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?");
|
|
|
|
|
|
|
|
const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
// Unique the name through the identifier table.
|
2010-02-11 13:28:37 +08:00
|
|
|
const char *AliaseeName =
|
|
|
|
getContext().Idents.get(AA->getAliasee()).getNameStart();
|
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))
|
2009-05-13 05:21:08 +08:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, GlobalDecl());
|
2009-03-23 05:47:11 +08:00
|
|
|
else
|
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AliaseeName,
|
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
|
|
|
// See if there is already something with the alias' name in the module.
|
|
|
|
const char *MangledName = getMangledName(D);
|
|
|
|
llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
if (Entry && !Entry->isDeclaration()) {
|
|
|
|
// 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.
|
|
|
|
GA->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
if (Entry) {
|
|
|
|
// 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.
|
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();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
// Now we know that there is no conflict, set the name.
|
|
|
|
Entry = GA;
|
|
|
|
GA->setName(MangledName);
|
|
|
|
|
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.
|
2009-06-30 10:35:26 +08:00
|
|
|
if (FD->getBody())
|
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>() ||
|
2009-06-30 10:34:44 +08:00
|
|
|
D->hasAttr<WeakImportAttr>()) {
|
2009-04-14 16:05:55 +08:00
|
|
|
GA->setLinkage(llvm::Function::WeakAnyLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCommonAttributes(D, GA);
|
2009-03-23 05:47:11 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 05:56:56 +08:00
|
|
|
/// getBuiltinLibFunction - Given a builtin id for a function like
|
|
|
|
/// "__builtin_fabsf", return a Function* for "fabsf".
|
2009-09-14 12:33:21 +08:00
|
|
|
llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
|
|
|
|
unsigned BuiltinID) {
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
llvm-svn: 64504
2009-02-14 07:20:09 +08:00
|
|
|
assert((Context.BuiltinInfo.isLibFunction(BuiltinID) ||
|
2009-09-09 23:08:12 +08:00
|
|
|
Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) &&
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
llvm-svn: 64504
2009-02-14 07:20:09 +08:00
|
|
|
"isn't a lib fn");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
llvm-svn: 64504
2009-02-14 07:20:09 +08:00
|
|
|
// Get the name, skip over the __builtin_ prefix (if necessary).
|
|
|
|
const char *Name = Context.BuiltinInfo.GetName(BuiltinID);
|
|
|
|
if (Context.BuiltinInfo.isLibFunction(BuiltinID))
|
|
|
|
Name += 10;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const llvm::FunctionType *Ty =
|
2009-12-09 11:05:59 +08:00
|
|
|
cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
|
2007-08-31 12:31:45 +08:00
|
|
|
|
2009-03-23 05:56:56 +08:00
|
|
|
// Unique the name through the identifier table.
|
2009-10-19 04:26:12 +08:00
|
|
|
Name = getContext().Idents.get(Name).getNameStart();
|
2009-09-14 12:33:21 +08:00
|
|
|
return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD));
|
2007-08-31 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
2007-12-18 08:25:38 +08:00
|
|
|
llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys,
|
|
|
|
unsigned NumTys) {
|
|
|
|
return llvm::Intrinsic::getDeclaration(&getModule(),
|
|
|
|
(llvm::Intrinsic::ID)IID, Tys, NumTys);
|
|
|
|
}
|
2007-08-31 12:31:45 +08:00
|
|
|
|
2007-06-23 02:48:09 +08:00
|
|
|
llvm::Function *CodeGenModule::getMemCpyFn() {
|
|
|
|
if (MemCpyFn) return MemCpyFn;
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext);
|
2008-11-22 00:43:15 +08:00
|
|
|
return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1);
|
2007-06-23 02:48:09 +08:00
|
|
|
}
|
2007-08-21 08:21:21 +08:00
|
|
|
|
2008-05-26 20:59:39 +08:00
|
|
|
llvm::Function *CodeGenModule::getMemMoveFn() {
|
|
|
|
if (MemMoveFn) return MemMoveFn;
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext);
|
2008-11-22 00:43:15 +08:00
|
|
|
return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1);
|
2008-05-26 20:59:39 +08:00
|
|
|
}
|
|
|
|
|
2008-02-20 06:01:01 +08:00
|
|
|
llvm::Function *CodeGenModule::getMemSetFn() {
|
|
|
|
if (MemSetFn) return MemSetFn;
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext);
|
2008-11-22 00:43:15 +08:00
|
|
|
return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1);
|
2008-02-20 06:01:01 +08:00
|
|
|
}
|
2007-12-18 08:25:38 +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) {
|
2009-09-22 11:27:52 +08:00
|
|
|
unsigned NumBytes = Literal->getByteLength();
|
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;
|
|
|
|
return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(),
|
|
|
|
StringLength));
|
|
|
|
}
|
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
// Otherwise, convert the UTF8 literals into a byte string.
|
|
|
|
llvm::SmallVector<UTF16, 128> ToBuf(NumBytes);
|
|
|
|
const UTF8 *FromPtr = (UTF8 *)Literal->getStrData();
|
|
|
|
UTF16 *ToPtr = &ToBuf[0];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
|
2009-07-24 06:52:48 +08:00
|
|
|
&ToPtr, ToPtr + NumBytes,
|
|
|
|
strictConversion);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
// Check for conversion failure.
|
|
|
|
if (Result != conversionOK) {
|
|
|
|
// FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove
|
|
|
|
// this duplicate code.
|
|
|
|
assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed");
|
2009-09-22 18:03:52 +08:00
|
|
|
StringLength = NumBytes;
|
|
|
|
return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(),
|
|
|
|
StringLength));
|
2009-04-01 23:50:34 +08:00
|
|
|
}
|
2009-07-24 06:52:48 +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
|
|
|
|
|
|
|
// Render the UTF-16 string into a byte array and convert to the target byte
|
|
|
|
// order.
|
|
|
|
//
|
|
|
|
// FIXME: This isn't something we should need to do here.
|
|
|
|
llvm::SmallString<128> AsBytes;
|
|
|
|
AsBytes.reserve(StringLength * 2);
|
|
|
|
for (unsigned i = 0; i != StringLength; ++i) {
|
|
|
|
unsigned short Val = ToBuf[i];
|
|
|
|
if (TargetIsLSB) {
|
|
|
|
AsBytes.push_back(Val & 0xFF);
|
|
|
|
AsBytes.push_back(Val >> 8);
|
|
|
|
} else {
|
|
|
|
AsBytes.push_back(Val >> 8);
|
|
|
|
AsBytes.push_back(Val & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
2009-08-04 05:47:08 +08:00
|
|
|
// Append one extra null character, the second is automatically added by our
|
|
|
|
// caller.
|
|
|
|
AsBytes.push_back(0);
|
2009-07-24 07:41:22 +08:00
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
IsUTF16 = true;
|
2009-07-24 07:41:22 +08:00
|
|
|
return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size()));
|
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,
|
2009-07-24 07:41:22 +08:00
|
|
|
getTargetData().isLittleEndian(),
|
|
|
|
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
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Constant *Zero =
|
|
|
|
llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext));
|
2008-08-24 02:37:06 +08:00
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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) {
|
|
|
|
const 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
|
|
|
|
CFConstantStringClassRef =
|
2009-07-30 02:54:39 +08:00
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
|
2007-08-21 08:21:21 +08:00
|
|
|
}
|
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
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const llvm::StructType *STy =
|
2008-11-16 02:54:24 +08:00
|
|
|
cast<llvm::StructType>(getTypes().ConvertType(CFTy));
|
|
|
|
|
2009-08-16 13:55:31 +08:00
|
|
|
std::vector<llvm::Constant*> Fields(4);
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Class pointer.
|
2009-08-16 13:55:31 +08:00
|
|
|
Fields[0] = CFConstantStringClassRef;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Flags.
|
2008-08-24 02:37:06 +08:00
|
|
|
const 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.
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str());
|
2009-04-03 08:57:44 +08:00
|
|
|
|
2009-07-16 13:03:48 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
2009-10-14 13:55:45 +08:00
|
|
|
bool isConstant;
|
2009-04-03 08:57:44 +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;
|
2009-10-14 13:55:45 +08:00
|
|
|
// Note: -fwritable-strings doesn't make unicode CFStrings writable, but
|
|
|
|
// does make plain ascii ones writable.
|
|
|
|
isConstant = true;
|
2009-04-03 08:57:44 +08:00
|
|
|
} else {
|
2009-07-16 13:03:48 +08:00
|
|
|
Linkage = llvm::GlobalValue::PrivateLinkage;
|
2009-10-14 13:55:45 +08:00
|
|
|
isConstant = !Features.WritableStrings;
|
2009-04-03 08:57:44 +08:00
|
|
|
}
|
2009-10-14 13:55:45 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
2009-10-14 13:55:45 +08:00
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C,
|
|
|
|
".str");
|
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());
|
2009-04-03 08:57:44 +08:00
|
|
|
}
|
2009-08-16 13:55:31 +08:00
|
|
|
Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2);
|
|
|
|
|
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_");
|
2009-04-01 07:42:16 +08:00
|
|
|
if (const char *Sect = getContext().Target.getCFStringSection())
|
|
|
|
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
|
|
|
|
2008-08-14 07:20:05 +08:00
|
|
|
/// GetStringForStringLiteral - Return the appropriate bytes for a
|
2008-08-11 04:25:57 +08:00
|
|
|
/// string literal, properly padded to match the literal type.
|
2008-08-14 07:20:05 +08:00
|
|
|
std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) {
|
2008-08-11 04:25:57 +08:00
|
|
|
const char *StrData = E->getStrData();
|
|
|
|
unsigned Len = E->getByteLength();
|
|
|
|
|
|
|
|
const ConstantArrayType *CAT =
|
|
|
|
getContext().getAsConstantArrayType(E->getType());
|
|
|
|
assert(CAT && "String isn't pointer or array!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-27 07:01:51 +08:00
|
|
|
// Resize the string to the right size.
|
2008-08-11 04:25:57 +08:00
|
|
|
std::string Str(StrData, StrData+Len);
|
|
|
|
uint64_t RealLen = CAT->getSize().getZExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-27 07:01:51 +08:00
|
|
|
if (E->isWide())
|
|
|
|
RealLen *= getContext().Target.getWCharWidth()/8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-11 04:25:57 +08:00
|
|
|
Str.resize(RealLen, '\0');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-11 04:25:57 +08:00
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
// FIXME: This can be more efficient.
|
2009-11-16 13:55:46 +08:00
|
|
|
// FIXME: We shouldn't need to bitcast the constant in the wide string case.
|
|
|
|
llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S));
|
|
|
|
if (S->isWide()) {
|
|
|
|
llvm::Type *DestTy =
|
|
|
|
llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType()));
|
|
|
|
C = llvm::ConstantExpr::getBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
return C;
|
2008-08-14 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.
|
2009-09-09 23:08:12 +08:00
|
|
|
static llvm::Constant *GenerateStringLiteral(const std::string &str,
|
2007-11-28 13:34:05 +08:00
|
|
|
bool constant,
|
2008-10-18 05:56:50 +08:00
|
|
|
CodeGenModule &CGM,
|
|
|
|
const char *GlobalName) {
|
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 =
|
|
|
|
llvm::ConstantArray::get(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
|
2009-09-09 23:08:12 +08:00
|
|
|
return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant,
|
2009-07-16 13:03:48 +08:00
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
C, GlobalName);
|
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.
|
2008-10-18 05:56:50 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str,
|
|
|
|
const char *GlobalName) {
|
2009-04-01 07:42:16 +08:00
|
|
|
bool IsConstant = !Features.WritableStrings;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Don't share any string literals if strings aren't constant.
|
|
|
|
if (!IsConstant)
|
2008-10-18 05:56:50 +08:00
|
|
|
return GenerateStringLiteral(str, false, *this, GlobalName);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::StringMapEntry<llvm::Constant *> &Entry =
|
2009-07-16 13:03:48 +08:00
|
|
|
ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
|
2007-11-28 13:34:05 +08:00
|
|
|
|
|
|
|
if (Entry.getValue())
|
2009-02-25 06:18:39 +08:00
|
|
|
return Entry.getValue();
|
2007-11-28 13:34:05 +08:00
|
|
|
|
|
|
|
// Create a global variable for this.
|
2008-10-18 05:56:50 +08:00
|
|
|
llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName);
|
2007-11-28 13:34:05 +08:00
|
|
|
Entry.setValue(C);
|
|
|
|
return C;
|
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
|
|
|
|
/// GetAddrOfConstantCString - Returns a pointer to a character
|
|
|
|
/// array containing the literal and a terminating '\-'
|
|
|
|
/// character. The result has pointer to array type.
|
2008-10-18 05:56:50 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str,
|
|
|
|
const char *GlobalName){
|
2008-12-10 03:10:54 +08:00
|
|
|
return GetAddrOfConstantString(str + '\0', GlobalName);
|
2008-08-14 07:20:05 +08:00
|
|
|
}
|
2008-08-16 07:26:23 +08:00
|
|
|
|
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) {
|
2008-08-26 16:29:31 +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
|
|
|
|
// been overridden. Note that ::isSynthesized is not the method
|
|
|
|
// we want, that just indicates if the decl came from a
|
|
|
|
// property. What we want to know is if the method is defined in
|
|
|
|
// this implementation.
|
2009-06-30 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2009-04-02 13:55:18 +08:00
|
|
|
I != E; ++I)
|
|
|
|
EmitTopLevelDecl(*I);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-11 07:43:36 +08:00
|
|
|
EmitGlobal(cast<FunctionDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Var:
|
2009-09-11 07:43:36 +08:00
|
|
|
EmitGlobal(cast<VarDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
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-09-03 07:49:23 +08:00
|
|
|
case Decl::UsingDirective:
|
2009-06-30 04:59:39 +08:00
|
|
|
case Decl::ClassTemplate:
|
|
|
|
case Decl::FunctionTemplate:
|
2009-09-24 03:19:16 +08:00
|
|
|
case Decl::NamespaceAlias:
|
2009-06-20 08:51:54 +08:00
|
|
|
break;
|
2009-04-15 23:55:24 +08:00
|
|
|
case Decl::CXXConstructor:
|
2009-11-24 13:16:24 +08:00
|
|
|
// Skip function templates
|
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate())
|
|
|
|
return;
|
|
|
|
|
2009-04-15 23:55:24 +08:00
|
|
|
EmitCXXConstructors(cast<CXXConstructorDecl>(D));
|
|
|
|
break;
|
2009-04-17 09:58:57 +08:00
|
|
|
case Decl::CXXDestructor:
|
|
|
|
EmitCXXDestructors(cast<CXXDestructorDecl>(D));
|
|
|
|
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.
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::ObjCClass:
|
|
|
|
case Decl::ObjCForwardProtocol:
|
2009-03-22 02:06:45 +08:00
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCInterface:
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2009-04-01 10:36:43 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::ObjCProtocol:
|
2009-03-22 02:06:45 +08:00
|
|
|
Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
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.
|
2008-08-16 07:26:23 +08:00
|
|
|
Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
case Decl::ObjCImplementation: {
|
|
|
|
ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D);
|
|
|
|
EmitObjCPropertyImplementations(OMD);
|
|
|
|
Runtime->GenerateClass(OMD);
|
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:
|
2009-01-08 09:10:55 +08:00
|
|
|
// compatibility-alias is a directive and has no code gen.
|
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);
|
2009-12-11 21:33:18 +08:00
|
|
|
llvm::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);
|
|
|
|
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
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|