Newer
Older
//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This coordinates the per-module state used while generating code.
//
//===----------------------------------------------------------------------===//
#include "CodeGenModule.h"
#include "CGDebugInfo.h"
#include "CGObjCRuntime.h"
#include "Mangle.h"
#include "TargetInfo.h"
Chandler Carruth
committed
#include "clang/Frontend/CodeGenOptions.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
Anders Carlsson
committed
#include "clang/AST/RecordLayout.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/ConvertUTF.h"
Nate Begeman
committed
#include "llvm/CallingConv.h"
Chris Lattner
committed
#include "llvm/Module.h"
#include "llvm/Intrinsics.h"
#include "llvm/LLVMContext.h"
#include "llvm/ADT/Triple.h"
Anton Korobeynikov
committed
#include "llvm/Target/TargetData.h"
#include "llvm/Support/CallSite.h"
using namespace clang;
using namespace CodeGen;
Chandler Carruth
committed
CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
llvm::Module &M, const llvm::TargetData &TD,
Diagnostic &diags)
: BlockModule(C, M, TD, Types, *this), Context(C),
Chandler Carruth
committed
Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M),
TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags),
Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()),
VTables(*this), Runtime(0), ABI(0),
CFConstantStringClassRef(0),
VMContext(M.getContext()) {
if (!Features.ObjC1)
Runtime = 0;
else if (!Features.NeXTRuntime)
Runtime = CreateGNUObjCRuntime(*this);
else if (Features.ObjCNonFragileABI)
Runtime = CreateMacNonFragileABIObjCRuntime(*this);
else
Runtime = CreateMacObjCRuntime(*this);
if (!Features.CPlusPlus)
ABI = 0;
else createCXXABI();
// If debug info generation is enabled, create the CGDebugInfo object.
DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0;
}
CodeGenModule::~CodeGenModule() {
Ted Kremenek
committed
delete Runtime;
delete ABI;
Ted Kremenek
committed
delete DebugInfo;
}
David Chisnall
committed
void CodeGenModule::createObjCRuntime() {
if (!Features.NeXTRuntime)
Runtime = CreateGNUObjCRuntime(*this);
else if (Features.ObjCNonFragileABI)
Runtime = CreateMacNonFragileABIObjCRuntime(*this);
else
Runtime = CreateMacObjCRuntime(*this);
}
void CodeGenModule::createCXXABI() {
if (Context.Target.getCXXABI() == "microsoft")
ABI = CreateMicrosoftCXXABI(*this);
else
ABI = CreateItaniumCXXABI(*this);
Ted Kremenek
committed
void CodeGenModule::Release() {
EmitDeferred();
Eli Friedman
committed
EmitCXXGlobalInitFunc();
if (Runtime)
if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
AddGlobalCtor(ObjCInitFunction);
EmitCtorList(GlobalCtors, "llvm.global_ctors");
EmitCtorList(GlobalDtors, "llvm.global_dtors");
EmitAnnotations();
EmitLLVMUsed();
bool CodeGenModule::isTargetDarwin() const {
return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin;
}
/// ErrorUnsupported - Print out an error that codegen doesn't support the
/// specified stmt yet.
void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
bool OmitOnError) {
if (OmitOnError && getDiags().hasErrorOccurred())
return;
unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
"cannot compile this %0 yet");
std::string Msg = Type;
Chris Lattner
committed
getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
<< Msg << S->getSourceRange();
/// ErrorUnsupported - Print out an error that codegen doesn't support the
/// specified decl yet.
void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type,
bool OmitOnError) {
if (OmitOnError && getDiags().hasErrorOccurred())
return;
unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error,
"cannot compile this %0 yet");
std::string Msg = Type;
Chris Lattner
committed
getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
CodeGenModule::getDeclVisibilityMode(const Decl *D) const {
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
if (VD->getStorageClass() == VarDecl::PrivateExtern)
return LangOptions::Hidden;
Argyrios Kyrtzidis
committed
if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) {
switch (attr->getVisibility()) {
default: assert(0 && "Unknown visibility!");
case VisibilityAttr::DefaultVisibility:
return LangOptions::Default;
case VisibilityAttr::HiddenVisibility:
return LangOptions::Hidden;
case VisibilityAttr::ProtectedVisibility:
return LangOptions::Protected;
}
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
if (getLangOptions().CPlusPlus) {
// Entities subject to an explicit instantiation declaration get default
// visibility.
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
if (Function->getTemplateSpecializationKind()
== TSK_ExplicitInstantiationDeclaration)
return LangOptions::Default;
} else if (const ClassTemplateSpecializationDecl *ClassSpec
= dyn_cast<ClassTemplateSpecializationDecl>(D)) {
if (ClassSpec->getSpecializationKind()
== TSK_ExplicitInstantiationDeclaration)
return LangOptions::Default;
} else if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
if (Record->getTemplateSpecializationKind()
== TSK_ExplicitInstantiationDeclaration)
return LangOptions::Default;
} else if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
if (Var->isStaticDataMember() &&
(Var->getTemplateSpecializationKind()
== TSK_ExplicitInstantiationDeclaration))
return LangOptions::Default;
}
// If -fvisibility-inlines-hidden was provided, then inline C++ member
// functions get "hidden" visibility by default.
if (getLangOptions().InlineVisibilityHidden)
if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
if (Method->isInlined())
return LangOptions::Hidden;
}
Anders Carlsson
committed
// This decl should have the same visibility as its parent.
if (const DeclContext *DC = D->getDeclContext())
return getDeclVisibilityMode(cast<Decl>(DC));
return getLangOptions().getVisibilityMode();
void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
const Decl *D) const {
// Internal definitions always have default visibility.
Chris Lattner
committed
if (GV->hasLocalLinkage()) {
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Loading
Loading full blame...