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"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
|
|
|
|
#include "llvm/ADT/OwningPtr.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"
|
2009-03-26 05:00:52 +00:00
|
|
|
using namespace clang;
|
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;
|
2012-10-08 16:25:52 +00:00
|
|
|
OwningPtr<const llvm::DataLayout> TD;
|
2008-02-06 02:01:47 +00:00
|
|
|
ASTContext *Ctx;
|
2009-11-12 17:24:48 +00:00
|
|
|
const CodeGenOptions CodeGenOpts; // Intentionally copied in.
|
2008-02-06 02:01:47 +00:00
|
|
|
protected:
|
2012-02-05 02:12:40 +00:00
|
|
|
OwningPtr<llvm::Module> M;
|
|
|
|
|
OwningPtr<CodeGen::CodeGenModule> Builder;
|
2008-02-06 02:01:47 +00:00
|
|
|
public:
|
2011-09-25 23:23:43 +00:00
|
|
|
CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
|
2013-04-16 22:48:20 +00:00
|
|
|
const CodeGenOptions &CGO, llvm::LLVMContext& C)
|
|
|
|
|
: Diags(diags), CodeGenOpts(CGO),
|
2013-02-14 08:09:20 +00:00
|
|
|
M(new llvm::Module(ModuleName, C)) {}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-08-05 18:50:11 +00:00
|
|
|
virtual ~CodeGeneratorImpl() {}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-10-21 19:55:09 +00:00
|
|
|
virtual llvm::Module* GetModule() {
|
|
|
|
|
return M.get();
|
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-08-07 19:47:41 +00:00
|
|
|
virtual llvm::Module* ReleaseModule() {
|
2008-08-05 18:50:11 +00:00
|
|
|
return M.take();
|
2008-02-06 02:01:47 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2008-02-06 02:01:47 +00:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
|
Ctx = &Context;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2011-09-02 00:18:52 +00:00
|
|
|
M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
|
|
|
|
|
M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
|
2012-10-08 16:25:52 +00:00
|
|
|
TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
|
2013-04-16 22:48:20 +00:00
|
|
|
Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
|
|
|
|
|
Diags));
|
2013-08-08 00:17:41 +00:00
|
|
|
|
|
|
|
|
for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
|
|
|
|
|
HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
|
2008-02-06 02:01:47 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2012-03-08 15:51:03 +00:00
|
|
|
virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
|
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
|
|
|
}
|
|
|
|
|
|
2011-11-18 00:26:59 +00:00
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return true;
|
|
|
|
|
|
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);
|
2011-11-18 00:26:59 +00:00
|
|
|
return true;
|
2008-02-06 02:01:47 +00:00
|
|
|
}
|
2008-08-15 23:26:23 +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).
|
2008-02-06 04:51:19 +00:00
|
|
|
virtual void HandleTagDeclDefinition(TagDecl *D) {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
2008-02-06 05:08:19 +00:00
|
|
|
Builder->UpdateCompletedType(D);
|
2011-02-15 18:11:42 +00:00
|
|
|
|
|
|
|
|
// In C++, we may have member functions that need to be emitted at this
|
|
|
|
|
// point.
|
2012-03-11 07:00:24 +00:00
|
|
|
if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
|
2011-02-15 18:11:42 +00:00
|
|
|
for (DeclContext::decl_iterator M = D->decls_begin(),
|
|
|
|
|
MEnd = D->decls_end();
|
|
|
|
|
M != MEnd; ++M)
|
|
|
|
|
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
|
2011-05-06 20:44:56 +00:00
|
|
|
if (Method->doesThisDeclarationHaveABody() &&
|
2011-02-19 21:54:50 +00:00
|
|
|
(Method->hasAttr<UsedAttr>() ||
|
|
|
|
|
Method->hasAttr<ConstructorAttr>()))
|
2011-02-15 18:11:42 +00:00
|
|
|
Builder->EmitTopLevelDecl(Method);
|
|
|
|
|
}
|
2008-02-06 04:51:19 +00:00
|
|
|
}
|
2008-08-07 19:47:41 +00:00
|
|
|
|
2013-07-13 21:08:14 +00:00
|
|
|
virtual void HandleTagDeclRequiredDefinition(const TagDecl *D) LLVM_OVERRIDE {
|
2013-08-19 21:02:26 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2009-03-28 04:11:33 +00:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Builder)
|
|
|
|
|
Builder->Release();
|
2009-12-19 17:50:07 +00:00
|
|
|
}
|
2009-04-21 17:11:58 +00:00
|
|
|
|
|
|
|
|
virtual void CompleteTentativeDefinition(VarDecl *D) {
|
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Builder->EmitTentativeDefinition(D);
|
|
|
|
|
}
|
2010-05-13 16:44:06 +00:00
|
|
|
|
|
|
|
|
virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
|
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Builder->EmitVTable(RD, DefinitionRequired);
|
|
|
|
|
}
|
2013-05-08 13:44:39 +00:00
|
|
|
|
|
|
|
|
virtual void HandleLinkerOptionPragma(llvm::StringRef Opts) {
|
|
|
|
|
Builder->AppendLinkerOptions(Opts);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-04 02:07:14 +00:00
|
|
|
virtual void HandleDetectMismatch(llvm::StringRef Name,
|
|
|
|
|
llvm::StringRef Value) {
|
|
|
|
|
Builder->AddDetectMismatch(Name, Value);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-08 13:44:39 +00:00
|
|
|
virtual void HandleDependentLibrary(llvm::StringRef Lib) {
|
|
|
|
|
Builder->AddDependentLib(Lib);
|
|
|
|
|
}
|
2008-02-06 02:01:47 +00:00
|
|
|
};
|
2007-05-24 06:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-20 02:48:34 +00:00
|
|
|
void CodeGenerator::anchor() { }
|
|
|
|
|
|
2011-09-25 23:23:43 +00:00
|
|
|
CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
|
2008-08-05 18:50:11 +00:00
|
|
|
const std::string& ModuleName,
|
2009-11-12 17:24:48 +00:00
|
|
|
const CodeGenOptions &CGO,
|
2013-04-16 22:48:20 +00:00
|
|
|
const TargetOptions &/*TO*/,
|
2009-07-01 23:14:14 +00:00
|
|
|
llvm::LLVMContext& C) {
|
2013-04-16 22:48:20 +00:00
|
|
|
return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
|
2007-05-24 06:29:05 +00:00
|
|
|
}
|