ModuleBuilder.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "clang/CodeGen/ModuleBuilder.h"
  14. #include "CodeGenModule.h"
  15. #include "clang/Frontend/CompileOptions.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/DeclObjC.h"
  18. #include "clang/AST/Expr.h"
  19. #include "clang/Basic/Diagnostic.h"
  20. #include "clang/Basic/TargetInfo.h"
  21. #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"
  26. using namespace clang;
  27. namespace {
  28. class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
  29. Diagnostic &Diags;
  30. llvm::OwningPtr<const llvm::TargetData> TD;
  31. ASTContext *Ctx;
  32. const CompileOptions CompileOpts; // Intentionally copied in.
  33. protected:
  34. llvm::OwningPtr<llvm::Module> M;
  35. llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
  36. public:
  37. CodeGeneratorImpl(Diagnostic &diags, const std::string& ModuleName,
  38. const CompileOptions &CO, const llvm::LLVMContext& C)
  39. : Diags(diags), CompileOpts(CO), M(new llvm::Module(ModuleName, C)) {}
  40. virtual ~CodeGeneratorImpl() {}
  41. virtual llvm::Module* GetModule() {
  42. return M.get();
  43. }
  44. virtual llvm::Module* ReleaseModule() {
  45. return M.take();
  46. }
  47. virtual void Initialize(ASTContext &Context) {
  48. Ctx = &Context;
  49. M->setTargetTriple(Ctx->Target.getTargetTriple());
  50. M->setDataLayout(Ctx->Target.getTargetDescription());
  51. TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
  52. Builder.reset(new CodeGen::CodeGenModule(Context, CompileOpts,
  53. *M, *TD, Diags));
  54. }
  55. virtual void HandleTopLevelDecl(DeclGroupRef DG) {
  56. // Make sure to emit all elements of a Decl.
  57. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
  58. Builder->EmitTopLevelDecl(*I);
  59. }
  60. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  61. /// to (e.g. struct, union, enum, class) is completed. This allows the
  62. /// client hack on the type, which can occur at any point in the file
  63. /// (because these can be defined in declspecs).
  64. virtual void HandleTagDeclDefinition(TagDecl *D) {
  65. Builder->UpdateCompletedType(D);
  66. }
  67. virtual void HandleTranslationUnit(ASTContext &Ctx) {
  68. if (Diags.hasErrorOccurred()) {
  69. M.reset();
  70. return;
  71. }
  72. if (Builder)
  73. Builder->Release();
  74. };
  75. virtual void CompleteTentativeDefinition(VarDecl *D) {
  76. if (Diags.hasErrorOccurred())
  77. return;
  78. Builder->EmitTentativeDefinition(D);
  79. }
  80. };
  81. }
  82. CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags,
  83. const std::string& ModuleName,
  84. const CompileOptions &CO,
  85. const llvm::LLVMContext& C) {
  86. return new CodeGeneratorImpl(Diags, ModuleName, CO, C);
  87. }