2007-07-11 21:01:13 +04:00
|
|
|
//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This builds an AST and converts it to LLVM Code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
|
|
|
#include "CodeGenModule.h"
|
2009-03-26 08:00:52 +03:00
|
|
|
#include "clang/Frontend/CompileOptions.h"
|
2008-02-06 05:01:47 +03:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 08:54:23 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2008-02-06 05:01:47 +03:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-07-01 21:00:06 +04:00
|
|
|
#include "llvm/LLVMContext.h"
|
2008-02-06 05:01:47 +03:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-08-05 22:50:11 +04:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-03-26 08:00:52 +03:00
|
|
|
using namespace clang;
|
2008-08-05 22:50:11 +04:00
|
|
|
|
2008-01-12 10:05:38 +03:00
|
|
|
|
2008-02-06 05:01:47 +03:00
|
|
|
namespace {
|
2008-08-05 22:50:11 +04:00
|
|
|
class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
|
2008-02-06 05:01:47 +03:00
|
|
|
Diagnostic &Diags;
|
2008-08-05 22:50:11 +04:00
|
|
|
llvm::OwningPtr<const llvm::TargetData> TD;
|
2008-02-06 05:01:47 +03:00
|
|
|
ASTContext *Ctx;
|
2009-03-26 08:00:52 +03:00
|
|
|
const CompileOptions CompileOpts; // Intentionally copied in.
|
2008-02-06 05:01:47 +03:00
|
|
|
protected:
|
2008-08-05 22:50:11 +04:00
|
|
|
llvm::OwningPtr<llvm::Module> M;
|
|
|
|
llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
|
2008-02-06 05:01:47 +03:00
|
|
|
public:
|
2009-03-26 08:00:52 +03:00
|
|
|
CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
|
2009-07-02 03:14:14 +04:00
|
|
|
const CompileOptions &CO, llvm::LLVMContext& C)
|
2009-07-01 21:00:06 +04:00
|
|
|
: Diags(diags), CompileOpts(CO), M(new llvm::Module(ModuleName, C)) {}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-05 22:50:11 +04:00
|
|
|
virtual ~CodeGeneratorImpl() {}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-21 23:55:09 +04:00
|
|
|
virtual llvm::Module* GetModule() {
|
|
|
|
return M.get();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-07 23:47:41 +04:00
|
|
|
virtual llvm::Module* ReleaseModule() {
|
2008-08-05 22:50:11 +04:00
|
|
|
return M.take();
|
2008-02-06 05:01:47 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-02-06 05:01:47 +03:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-24 13:10:05 +04:00
|
|
|
M->setTargetTriple(Ctx->Target.getTriple().getTriple());
|
2008-02-06 05:01:47 +03:00
|
|
|
M->setDataLayout(Ctx->Target.getTargetDescription());
|
2008-08-05 22:50:11 +04:00
|
|
|
TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
|
2009-03-26 08:00:52 +03:00
|
|
|
Builder.reset(new CodeGen::CodeGenModule(Context, CompileOpts,
|
|
|
|
*M, *TD, Diags));
|
2008-02-06 05:01:47 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-29 20:50:03 +04:00
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef DG) {
|
2009-01-20 04:17:11 +03:00
|
|
|
// Make sure to emit all elements of a Decl.
|
2009-03-29 20:50:03 +04:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
Builder->EmitTopLevelDecl(*I);
|
2008-02-06 05:01:47 +03:00
|
|
|
}
|
2008-08-16 03:26:23 +04:00
|
|
|
|
2008-02-06 07:51:19 +03:00
|
|
|
/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
|
2009-03-29 20:50:03 +04: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 07:51:19 +03:00
|
|
|
virtual void HandleTagDeclDefinition(TagDecl *D) {
|
2008-02-06 08:08:19 +03:00
|
|
|
Builder->UpdateCompletedType(D);
|
2008-02-06 07:51:19 +03:00
|
|
|
}
|
2008-08-07 23:47:41 +04:00
|
|
|
|
2009-03-28 07:11:33 +03:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
2008-08-07 23:47:41 +04:00
|
|
|
if (Diags.hasErrorOccurred()) {
|
|
|
|
M.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Builder)
|
|
|
|
Builder->Release();
|
|
|
|
};
|
2009-04-21 21:11:58 +04:00
|
|
|
|
|
|
|
virtual void CompleteTentativeDefinition(VarDecl *D) {
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Builder->EmitTentativeDefinition(D);
|
|
|
|
}
|
2008-02-06 05:01:47 +03:00
|
|
|
};
|
2007-07-13 09:13:43 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
|
2008-08-05 22:50:11 +04:00
|
|
|
const std::string& ModuleName,
|
2009-07-01 21:00:06 +04:00
|
|
|
const CompileOptions &CO,
|
2009-07-02 03:14:14 +04:00
|
|
|
llvm::LLVMContext& C) {
|
2009-07-01 21:00:06 +04:00
|
|
|
return new CodeGeneratorImpl(Diags, ModuleName, CO, C);
|
2008-02-05 11:06:13 +03:00
|
|
|
}
|