ModuleBuilder.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/CodeGenOptions.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/ADT/OwningPtr.h"
  25. using namespace clang;
  26. namespace {
  27. class CodeGeneratorImpl : public CodeGenerator {
  28. DiagnosticsEngine &Diags;
  29. OwningPtr<const llvm::TargetData> TD;
  30. ASTContext *Ctx;
  31. const CodeGenOptions CodeGenOpts; // Intentionally copied in.
  32. protected:
  33. OwningPtr<llvm::Module> M;
  34. OwningPtr<CodeGen::CodeGenModule> Builder;
  35. public:
  36. CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
  37. const CodeGenOptions &CGO, llvm::LLVMContext& C)
  38. : Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
  39. virtual ~CodeGeneratorImpl() {}
  40. virtual llvm::Module* GetModule() {
  41. return M.get();
  42. }
  43. virtual llvm::Module* ReleaseModule() {
  44. return M.take();
  45. }
  46. virtual void Initialize(ASTContext &Context) {
  47. Ctx = &Context;
  48. M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
  49. M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
  50. TD.reset(new llvm::TargetData(Ctx->getTargetInfo().getTargetDescription()));
  51. Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
  52. *M, *TD, Diags));
  53. }
  54. virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
  55. // Make sure to emit all elements of a Decl.
  56. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
  57. Builder->EmitTopLevelDecl(*I);
  58. return true;
  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. // In C++, we may have member functions that need to be emitted at this
  67. // point.
  68. if (Ctx->getLangOptions().CPlusPlus && !D->isDependentContext()) {
  69. for (DeclContext::decl_iterator M = D->decls_begin(),
  70. MEnd = D->decls_end();
  71. M != MEnd; ++M)
  72. if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
  73. if (Method->doesThisDeclarationHaveABody() &&
  74. (Method->hasAttr<UsedAttr>() ||
  75. Method->hasAttr<ConstructorAttr>()))
  76. Builder->EmitTopLevelDecl(Method);
  77. }
  78. }
  79. virtual void HandleTranslationUnit(ASTContext &Ctx) {
  80. if (Diags.hasErrorOccurred()) {
  81. M.reset();
  82. return;
  83. }
  84. if (Builder)
  85. Builder->Release();
  86. }
  87. virtual void CompleteTentativeDefinition(VarDecl *D) {
  88. if (Diags.hasErrorOccurred())
  89. return;
  90. Builder->EmitTentativeDefinition(D);
  91. }
  92. virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
  93. if (Diags.hasErrorOccurred())
  94. return;
  95. Builder->EmitVTable(RD, DefinitionRequired);
  96. }
  97. };
  98. }
  99. void CodeGenerator::anchor() { }
  100. CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
  101. const std::string& ModuleName,
  102. const CodeGenOptions &CGO,
  103. llvm::LLVMContext& C) {
  104. return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
  105. }