ModuleBuilder.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
  55. Builder->HandleCXXStaticMemberVarInstantiation(VD);
  56. }
  57. virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
  58. // Make sure to emit all elements of a Decl.
  59. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
  60. Builder->EmitTopLevelDecl(*I);
  61. return true;
  62. }
  63. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  64. /// to (e.g. struct, union, enum, class) is completed. This allows the
  65. /// client hack on the type, which can occur at any point in the file
  66. /// (because these can be defined in declspecs).
  67. virtual void HandleTagDeclDefinition(TagDecl *D) {
  68. Builder->UpdateCompletedType(D);
  69. // In C++, we may have member functions that need to be emitted at this
  70. // point.
  71. if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) {
  72. for (DeclContext::decl_iterator M = D->decls_begin(),
  73. MEnd = D->decls_end();
  74. M != MEnd; ++M)
  75. if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
  76. if (Method->doesThisDeclarationHaveABody() &&
  77. (Method->hasAttr<UsedAttr>() ||
  78. Method->hasAttr<ConstructorAttr>()))
  79. Builder->EmitTopLevelDecl(Method);
  80. }
  81. }
  82. virtual void HandleTranslationUnit(ASTContext &Ctx) {
  83. if (Diags.hasErrorOccurred()) {
  84. M.reset();
  85. return;
  86. }
  87. if (Builder)
  88. Builder->Release();
  89. }
  90. virtual void CompleteTentativeDefinition(VarDecl *D) {
  91. if (Diags.hasErrorOccurred())
  92. return;
  93. Builder->EmitTentativeDefinition(D);
  94. }
  95. virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
  96. if (Diags.hasErrorOccurred())
  97. return;
  98. Builder->EmitVTable(RD, DefinitionRequired);
  99. }
  100. };
  101. }
  102. void CodeGenerator::anchor() { }
  103. CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
  104. const std::string& ModuleName,
  105. const CodeGenOptions &CGO,
  106. llvm::LLVMContext& C) {
  107. return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
  108. }