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