1//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This builds an AST and converts it to LLVM Code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/CodeGen/ModuleBuilder.h" 15#include "CodeGenModule.h" 16#include "clang/Frontend/CompileOptions.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/Expr.h" 20#include "clang/Basic/Diagnostic.h" 21#include "clang/Basic/TargetInfo.h"
| 1//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This builds an AST and converts it to LLVM Code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/CodeGen/ModuleBuilder.h" 15#include "CodeGenModule.h" 16#include "clang/Frontend/CompileOptions.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/Expr.h" 20#include "clang/Basic/Diagnostic.h" 21#include "clang/Basic/TargetInfo.h"
|
| 22#include "llvm/LLVMContext.h"
|
22#include "llvm/Module.h" 23#include "llvm/Target/TargetData.h" 24#include "llvm/Support/Compiler.h" 25#include "llvm/ADT/OwningPtr.h" 26using namespace clang; 27 28 29namespace { 30 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator { 31 Diagnostic &Diags; 32 llvm::OwningPtr<const llvm::TargetData> TD; 33 ASTContext *Ctx; 34 const CompileOptions CompileOpts; // Intentionally copied in. 35 protected: 36 llvm::OwningPtr<llvm::Module> M; 37 llvm::OwningPtr<CodeGen::CodeGenModule> Builder; 38 public: 39 CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
| 23#include "llvm/Module.h" 24#include "llvm/Target/TargetData.h" 25#include "llvm/Support/Compiler.h" 26#include "llvm/ADT/OwningPtr.h" 27using namespace clang; 28 29 30namespace { 31 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator { 32 Diagnostic &Diags; 33 llvm::OwningPtr<const llvm::TargetData> TD; 34 ASTContext *Ctx; 35 const CompileOptions CompileOpts; // Intentionally copied in. 36 protected: 37 llvm::OwningPtr<llvm::Module> M; 38 llvm::OwningPtr<CodeGen::CodeGenModule> Builder; 39 public: 40 CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
|
40 const CompileOptions &CO) 41 : Diags(diags), CompileOpts(CO), M(new llvm::Module(ModuleName)) {}
| 41 const CompileOptions &CO, llvm::LLVMContext& C) 42 : Diags(diags), CompileOpts(CO), M(new llvm::Module(ModuleName, C)) {}
|
42 43 virtual ~CodeGeneratorImpl() {} 44 45 virtual llvm::Module* GetModule() { 46 return M.get(); 47 } 48 49 virtual llvm::Module* ReleaseModule() { 50 return M.take(); 51 } 52 53 virtual void Initialize(ASTContext &Context) { 54 Ctx = &Context; 55 56 M->setTargetTriple(Ctx->Target.getTargetTriple()); 57 M->setDataLayout(Ctx->Target.getTargetDescription()); 58 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription())); 59 Builder.reset(new CodeGen::CodeGenModule(Context, CompileOpts, 60 *M, *TD, Diags)); 61 } 62 63 virtual void HandleTopLevelDecl(DeclGroupRef DG) { 64 // Make sure to emit all elements of a Decl. 65 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 66 Builder->EmitTopLevelDecl(*I); 67 } 68 69 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 70 /// to (e.g. struct, union, enum, class) is completed. This allows the 71 /// client hack on the type, which can occur at any point in the file 72 /// (because these can be defined in declspecs). 73 virtual void HandleTagDeclDefinition(TagDecl *D) { 74 Builder->UpdateCompletedType(D); 75 } 76 77 virtual void HandleTranslationUnit(ASTContext &Ctx) { 78 if (Diags.hasErrorOccurred()) { 79 M.reset(); 80 return; 81 } 82 83 if (Builder) 84 Builder->Release(); 85 }; 86 87 virtual void CompleteTentativeDefinition(VarDecl *D) { 88 if (Diags.hasErrorOccurred()) 89 return; 90 91 Builder->EmitTentativeDefinition(D); 92 } 93 }; 94} 95 96CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags, 97 const std::string& ModuleName,
| 43 44 virtual ~CodeGeneratorImpl() {} 45 46 virtual llvm::Module* GetModule() { 47 return M.get(); 48 } 49 50 virtual llvm::Module* ReleaseModule() { 51 return M.take(); 52 } 53 54 virtual void Initialize(ASTContext &Context) { 55 Ctx = &Context; 56 57 M->setTargetTriple(Ctx->Target.getTargetTriple()); 58 M->setDataLayout(Ctx->Target.getTargetDescription()); 59 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription())); 60 Builder.reset(new CodeGen::CodeGenModule(Context, CompileOpts, 61 *M, *TD, Diags)); 62 } 63 64 virtual void HandleTopLevelDecl(DeclGroupRef DG) { 65 // Make sure to emit all elements of a Decl. 66 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 67 Builder->EmitTopLevelDecl(*I); 68 } 69 70 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 71 /// to (e.g. struct, union, enum, class) is completed. This allows the 72 /// client hack on the type, which can occur at any point in the file 73 /// (because these can be defined in declspecs). 74 virtual void HandleTagDeclDefinition(TagDecl *D) { 75 Builder->UpdateCompletedType(D); 76 } 77 78 virtual void HandleTranslationUnit(ASTContext &Ctx) { 79 if (Diags.hasErrorOccurred()) { 80 M.reset(); 81 return; 82 } 83 84 if (Builder) 85 Builder->Release(); 86 }; 87 88 virtual void CompleteTentativeDefinition(VarDecl *D) { 89 if (Diags.hasErrorOccurred()) 90 return; 91 92 Builder->EmitTentativeDefinition(D); 93 } 94 }; 95} 96 97CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags, 98 const std::string& ModuleName,
|
98 const CompileOptions &CO) { 99 return new CodeGeneratorImpl(Diags, ModuleName, CO);
| 99 const CompileOptions &CO, 100 llvm::LLVMContext& C) { 101 return new CodeGeneratorImpl(Diags, ModuleName, CO, C);
|
100}
| 102}
|