2007-05-24 06:29:05 +00:00
|
|
|
//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
2007-12-29 19:59:25 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-24 06:29:05 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This builds an AST and converts it to LLVM Code.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
2013-07-13 21:08:14 +00:00
|
|
|
#include "CGDebugInfo.h"
|
2014-01-07 11:51:46 +00:00
|
|
|
#include "CodeGenModule.h"
|
2008-02-06 02:01:47 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 04:54:23 +00:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
|
#include "clang/AST/Expr.h"
|
2008-02-06 02:01:47 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2016-04-08 16:52:00 +00:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-05-08 13:44:39 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-02 11:45:17 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
|
#include "llvm/IR/Module.h"
|
2014-03-09 11:36:40 +00:00
|
|
|
#include <memory>
|
2016-05-18 05:21:18 +00:00
|
|
|
|
2009-03-26 05:00:52 +00:00
|
|
|
using namespace clang;
|
2016-05-18 05:21:18 +00:00
|
|
|
using namespace CodeGen;
|
2008-08-05 18:50:11 +00:00
|
|
|
|
2008-02-06 02:01:47 +00:00
|
|
|
namespace {
|
2009-11-28 19:45:26 +00:00
|
|
|
class CodeGeneratorImpl : public CodeGenerator {
|
2011-09-25 23:23:43 +00:00
|
|
|
DiagnosticsEngine &Diags;
|
2008-02-06 02:01:47 +00:00
|
|
|
ASTContext *Ctx;
|
2015-06-30 02:26:03 +00:00
|
|
|
const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info.
|
|
|
|
|
const PreprocessorOptions &PreprocessorOpts; // Only used for debug info.
|
2009-11-12 17:24:48 +00:00
|
|
|
const CodeGenOptions CodeGenOpts; // Intentionally copied in.
|
2014-08-01 22:42:16 +00:00
|
|
|
|
|
|
|
|
unsigned HandlingTopLevelDecls;
|
2016-04-22 18:46:33 +00:00
|
|
|
|
|
|
|
|
/// Use this when emitting decls to block re-entrant decl emission. It will
|
|
|
|
|
/// emit all deferred decls on scope exit. Set EmitDeferred to false if decl
|
|
|
|
|
/// emission must be deferred longer, like at the end of a tag definition.
|
2014-08-01 22:42:16 +00:00
|
|
|
struct HandlingTopLevelDeclRAII {
|
|
|
|
|
CodeGeneratorImpl &Self;
|
2016-04-22 18:46:33 +00:00
|
|
|
bool EmitDeferred;
|
|
|
|
|
HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self,
|
|
|
|
|
bool EmitDeferred = true)
|
|
|
|
|
: Self(Self), EmitDeferred(EmitDeferred) {
|
2014-08-01 22:42:16 +00:00
|
|
|
++Self.HandlingTopLevelDecls;
|
|
|
|
|
}
|
|
|
|
|
~HandlingTopLevelDeclRAII() {
|
2016-04-22 18:46:33 +00:00
|
|
|
unsigned Level = --Self.HandlingTopLevelDecls;
|
|
|
|
|
if (Level == 0 && EmitDeferred)
|
2014-08-01 22:42:16 +00:00
|
|
|
Self.EmitDeferredDecls();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-04 18:41:51 +00:00
|
|
|
CoverageSourceInfo *CoverageInfo;
|
|
|
|
|
|
2008-02-06 02:01:47 +00:00
|
|
|
protected:
|
2014-03-07 20:03:18 +00:00
|
|
|
std::unique_ptr<llvm::Module> M;
|
|
|
|
|
std::unique_ptr<CodeGen::CodeGenModule> Builder;
|
|
|
|
|
|
2014-12-18 19:19:00 +00:00
|
|
|
private:
|
|
|
|
|
SmallVector<CXXMethodDecl *, 8> DeferredInlineMethodDefinitions;
|
|
|
|
|
|
2008-02-06 02:01:47 +00:00
|
|
|
public:
|
2016-05-18 05:21:18 +00:00
|
|
|
CodeGeneratorImpl(DiagnosticsEngine &diags, llvm::StringRef ModuleName,
|
2015-06-30 02:26:03 +00:00
|
|
|
const HeaderSearchOptions &HSO,
|
|
|
|
|
const PreprocessorOptions &PPO, const CodeGenOptions &CGO,
|
|
|
|
|
llvm::LLVMContext &C,
|
2014-08-04 18:41:51 +00:00
|
|
|
CoverageSourceInfo *CoverageInfo = nullptr)
|
2015-06-30 02:26:03 +00:00
|
|
|
: Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO),
|
|
|
|
|
PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
|
2016-03-13 21:05:23 +00:00
|
|
|
CoverageInfo(CoverageInfo), M(new llvm::Module(ModuleName, C)) {
|
|
|
|
|
C.setDiscardValueNames(CGO.DiscardValueNames);
|
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2015-04-11 02:00:23 +00:00
|
|
|
~CodeGeneratorImpl() override {
|
2014-12-19 23:35:11 +00:00
|
|
|
// There should normally not be any leftover inline method definitions.
|
|
|
|
|
assert(DeferredInlineMethodDefinitions.empty() ||
|
|
|
|
|
Diags.hasErrorOccurred());
|
2014-12-18 19:19:00 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2016-05-18 05:21:18 +00:00
|
|
|
CodeGenModule &CGM() {
|
|
|
|
|
return *Builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Module *GetModule() {
|
2008-10-21 19:55:09 +00:00
|
|
|
return M.get();
|
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2016-05-18 05:21:18 +00:00
|
|
|
llvm::Module *ReleaseModule() {
|
|
|
|
|
return M.release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Decl *GetDeclForMangledName(StringRef MangledName) {
|
2014-06-05 22:10:59 +00:00
|
|
|
GlobalDecl Result;
|
|
|
|
|
if (!Builder->lookupRepresentativeDecl(MangledName, Result))
|
|
|
|
|
return nullptr;
|
|
|
|
|
const Decl *D = Result.getCanonicalDecl().getDecl();
|
|
|
|
|
if (auto FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
|
if (FD->hasBody(FD))
|
|
|
|
|
return FD;
|
|
|
|
|
} else if (auto TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
|
if (auto Def = TD->getDefinition())
|
|
|
|
|
return Def;
|
|
|
|
|
}
|
|
|
|
|
return D;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 05:21:18 +00:00
|
|
|
llvm::Constant *GetAddrOfGlobal(GlobalDecl global, bool isForDefinition) {
|
|
|
|
|
return Builder->GetAddrOfGlobal(global, isForDefinition);
|
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2014-03-12 06:41:41 +00:00
|
|
|
void Initialize(ASTContext &Context) override {
|
2008-02-06 02:01:47 +00:00
|
|
|
Ctx = &Context;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2011-09-02 00:18:52 +00:00
|
|
|
M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
|
2016-03-04 19:00:41 +00:00
|
|
|
M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
|
2015-07-24 16:04:29 +00:00
|
|
|
Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts,
|
|
|
|
|
PreprocessorOpts, CodeGenOpts,
|
|
|
|
|
*M, Diags, CoverageInfo));
|
2013-08-08 00:17:41 +00:00
|
|
|
|
2016-01-16 00:31:22 +00:00
|
|
|
for (auto &&Lib : CodeGenOpts.DependentLibraries)
|
2016-03-02 17:28:48 +00:00
|
|
|
Builder->AddDependentLib(Lib);
|
2016-01-16 00:31:22 +00:00
|
|
|
for (auto &&Opt : CodeGenOpts.LinkerOptions)
|
2016-03-02 17:28:48 +00:00
|
|
|
Builder->AppendLinkerOptions(Opt);
|
2008-02-06 02:01:47 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2014-03-12 06:41:41 +00:00
|
|
|
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
2012-03-08 15:51:03 +00:00
|
|
|
Builder->HandleCXXStaticMemberVarInstantiation(VD);
|
2012-03-05 10:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 06:41:41 +00:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return true;
|
|
|
|
|
|
2014-08-01 22:42:16 +00:00
|
|
|
HandlingTopLevelDeclRAII HandlingDecl(*this);
|
|
|
|
|
|
2009-01-20 01:17:11 +00:00
|
|
|
// Make sure to emit all elements of a Decl.
|
2009-03-29 16:50:03 +00:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
|
Builder->EmitTopLevelDecl(*I);
|
2014-06-06 17:36:17 +00:00
|
|
|
|
2014-08-01 22:42:16 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EmitDeferredDecls() {
|
2014-08-13 21:15:09 +00:00
|
|
|
if (DeferredInlineMethodDefinitions.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
2014-08-01 20:39:36 +00:00
|
|
|
// Emit any deferred inline method definitions. Note that more deferred
|
|
|
|
|
// methods may be added during this loop, since ASTConsumer callbacks
|
|
|
|
|
// can be invoked if AST inspection results in declarations being added.
|
2014-08-13 21:15:09 +00:00
|
|
|
HandlingTopLevelDeclRAII HandlingDecl(*this);
|
|
|
|
|
for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I)
|
2014-08-01 20:39:36 +00:00
|
|
|
Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
|
2014-08-01 20:09:39 +00:00
|
|
|
DeferredInlineMethodDefinitions.clear();
|
2008-02-06 02:01:47 +00:00
|
|
|
}
|
2008-08-15 23:26:23 +00:00
|
|
|
|
2016-03-30 06:27:31 +00:00
|
|
|
void HandleInlineFunctionDefinition(FunctionDecl *D) override {
|
2014-05-23 20:37:38 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
assert(D->doesThisDeclarationHaveABody());
|
|
|
|
|
|
2016-03-30 06:27:31 +00:00
|
|
|
// Handle friend functions.
|
|
|
|
|
if (D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend)) {
|
|
|
|
|
if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()
|
|
|
|
|
&& !D->getLexicalDeclContext()->isDependentContext())
|
|
|
|
|
Builder->EmitTopLevelDecl(D);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, must be a method.
|
|
|
|
|
auto MD = cast<CXXMethodDecl>(D);
|
|
|
|
|
|
2014-06-06 17:36:17 +00:00
|
|
|
// We may want to emit this definition. However, that decision might be
|
|
|
|
|
// based on computing the linkage, and we have to defer that in case we
|
|
|
|
|
// are inside of something that will change the method's final linkage,
|
|
|
|
|
// e.g.
|
|
|
|
|
// typedef struct {
|
|
|
|
|
// void bar();
|
|
|
|
|
// void foo() { bar(); }
|
|
|
|
|
// } A;
|
2016-03-30 06:27:31 +00:00
|
|
|
DeferredInlineMethodDefinitions.push_back(MD);
|
2014-08-04 18:41:51 +00:00
|
|
|
|
2014-11-18 00:34:46 +00:00
|
|
|
// Provide some coverage mapping even for methods that aren't emitted.
|
|
|
|
|
// Don't do this for templated classes though, as they may not be
|
|
|
|
|
// instantiable.
|
2016-03-30 06:27:31 +00:00
|
|
|
if (!MD->getParent()->getDescribedClassTemplate())
|
|
|
|
|
Builder->AddDeferredUnusedCoverageMapping(MD);
|
2014-05-23 20:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2008-02-06 04:51:19 +00:00
|
|
|
/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
|
2009-03-29 16:50:03 +00:00
|
|
|
/// to (e.g. struct, union, enum, class) is completed. This allows the
|
|
|
|
|
/// client hack on the type, which can occur at any point in the file
|
|
|
|
|
/// (because these can be defined in declspecs).
|
2014-03-12 06:41:41 +00:00
|
|
|
void HandleTagDeclDefinition(TagDecl *D) override {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
2016-04-22 18:46:33 +00:00
|
|
|
// Don't allow re-entrant calls to CodeGen triggered by PCH
|
|
|
|
|
// deserialization to emit deferred decls.
|
|
|
|
|
HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false);
|
|
|
|
|
|
2008-02-06 05:08:19 +00:00
|
|
|
Builder->UpdateCompletedType(D);
|
2014-07-17 20:25:23 +00:00
|
|
|
|
|
|
|
|
// For MSVC compatibility, treat declarations of static data members with
|
|
|
|
|
// inline initializers as definitions.
|
2015-10-08 04:53:31 +00:00
|
|
|
if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) {
|
2014-07-17 20:25:23 +00:00
|
|
|
for (Decl *Member : D->decls()) {
|
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
|
|
|
|
|
if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
|
|
|
|
|
Ctx->DeclMustBeEmitted(VD)) {
|
|
|
|
|
Builder->EmitGlobal(VD);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-04 09:22:22 +00:00
|
|
|
// For OpenMP emit declare reduction functions, if required.
|
|
|
|
|
if (Ctx->getLangOpts().OpenMP) {
|
|
|
|
|
for (Decl *Member : D->decls()) {
|
|
|
|
|
if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) {
|
|
|
|
|
if (Ctx->DeclMustBeEmitted(DRD))
|
|
|
|
|
Builder->EmitGlobal(DRD);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-02-06 04:51:19 +00:00
|
|
|
}
|
2008-08-07 19:47:41 +00:00
|
|
|
|
2014-03-12 06:41:41 +00:00
|
|
|
void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
2016-04-22 18:46:33 +00:00
|
|
|
// Don't allow re-entrant calls to CodeGen triggered by PCH
|
|
|
|
|
// deserialization to emit deferred decls.
|
|
|
|
|
HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false);
|
|
|
|
|
|
2013-07-13 21:08:14 +00:00
|
|
|
if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
|
|
|
|
|
if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
|
2013-08-15 20:49:17 +00:00
|
|
|
DI->completeRequiredType(RD);
|
2013-07-13 21:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 06:41:41 +00:00
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override {
|
2016-01-28 23:29:02 +00:00
|
|
|
// Release the Builder when there is no error.
|
|
|
|
|
if (!Diags.hasErrorOccurred() && Builder)
|
|
|
|
|
Builder->Release();
|
|
|
|
|
|
|
|
|
|
// If there are errors before or when releasing the Builder, reset
|
|
|
|
|
// the module to stop here before invoking the backend.
|
2008-08-07 19:47:41 +00:00
|
|
|
if (Diags.hasErrorOccurred()) {
|
2013-12-09 14:59:08 +00:00
|
|
|
if (Builder)
|
|
|
|
|
Builder->clear();
|
2008-08-07 19:47:41 +00:00
|
|
|
M.reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-12-19 17:50:07 +00:00
|
|
|
}
|
2009-04-21 17:11:58 +00:00
|
|
|
|
2016-01-26 19:30:26 +00:00
|
|
|
void AssignInheritanceModel(CXXRecordDecl *RD) override {
|
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Builder->RefreshTypeCacheForClass(RD);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 06:41:41 +00:00
|
|
|
void CompleteTentativeDefinition(VarDecl *D) override {
|
2009-04-21 17:11:58 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Builder->EmitTentativeDefinition(D);
|
|
|
|
|
}
|
2010-05-13 16:44:06 +00:00
|
|
|
|
2015-01-15 04:07:35 +00:00
|
|
|
void HandleVTable(CXXRecordDecl *RD) override {
|
2010-05-13 16:44:06 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
2015-01-15 04:07:35 +00:00
|
|
|
Builder->EmitVTable(RD);
|
2010-05-13 16:44:06 +00:00
|
|
|
}
|
2008-02-06 02:01:47 +00:00
|
|
|
};
|
2015-06-22 23:07:51 +00:00
|
|
|
}
|
2007-05-24 06:29:05 +00:00
|
|
|
|
2011-12-20 02:48:34 +00:00
|
|
|
void CodeGenerator::anchor() { }
|
|
|
|
|
|
2016-05-18 05:21:18 +00:00
|
|
|
CodeGenModule &CodeGenerator::CGM() {
|
|
|
|
|
return static_cast<CodeGeneratorImpl*>(this)->CGM();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Module *CodeGenerator::GetModule() {
|
|
|
|
|
return static_cast<CodeGeneratorImpl*>(this)->GetModule();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Module *CodeGenerator::ReleaseModule() {
|
|
|
|
|
return static_cast<CodeGeneratorImpl*>(this)->ReleaseModule();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Decl *CodeGenerator::GetDeclForMangledName(llvm::StringRef name) {
|
|
|
|
|
return static_cast<CodeGeneratorImpl*>(this)->GetDeclForMangledName(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenerator::GetAddrOfGlobal(GlobalDecl global,
|
|
|
|
|
bool isForDefinition) {
|
|
|
|
|
return static_cast<CodeGeneratorImpl*>(this)
|
|
|
|
|
->GetAddrOfGlobal(global, isForDefinition);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-30 02:26:03 +00:00
|
|
|
CodeGenerator *clang::CreateLLVMCodeGen(
|
2016-05-18 05:21:18 +00:00
|
|
|
DiagnosticsEngine &Diags, llvm::StringRef ModuleName,
|
2015-06-30 02:26:03 +00:00
|
|
|
const HeaderSearchOptions &HeaderSearchOpts,
|
|
|
|
|
const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO,
|
|
|
|
|
llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) {
|
|
|
|
|
return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts,
|
|
|
|
|
PreprocessorOpts, CGO, C, CoverageInfo);
|
2007-05-24 06:29:05 +00:00
|
|
|
}
|