ModuleBuilder.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "CGDebugInfo.h"
  15. #include "CodeGenModule.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 "clang/Frontend/CodeGenOptions.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/IR/DataLayout.h"
  24. #include "llvm/IR/LLVMContext.h"
  25. #include "llvm/IR/Module.h"
  26. #include <memory>
  27. using namespace clang;
  28. namespace {
  29. class CodeGeneratorImpl : public CodeGenerator {
  30. DiagnosticsEngine &Diags;
  31. std::unique_ptr<const llvm::DataLayout> TD;
  32. ASTContext *Ctx;
  33. const CodeGenOptions CodeGenOpts; // Intentionally copied in.
  34. protected:
  35. std::unique_ptr<llvm::Module> M;
  36. std::unique_ptr<CodeGen::CodeGenModule> Builder;
  37. public:
  38. CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
  39. const CodeGenOptions &CGO, llvm::LLVMContext& C)
  40. : Diags(diags), CodeGenOpts(CGO),
  41. M(new llvm::Module(ModuleName, C)) {}
  42. virtual ~CodeGeneratorImpl() {}
  43. llvm::Module* GetModule() override {
  44. return M.get();
  45. }
  46. llvm::Module *ReleaseModule() override { return M.release(); }
  47. void Initialize(ASTContext &Context) override {
  48. Ctx = &Context;
  49. M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
  50. M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
  51. TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
  52. Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
  53. Diags));
  54. for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
  55. HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
  56. }
  57. void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
  58. if (Diags.hasErrorOccurred())
  59. return;
  60. Builder->HandleCXXStaticMemberVarInstantiation(VD);
  61. }
  62. bool HandleTopLevelDecl(DeclGroupRef DG) override {
  63. if (Diags.hasErrorOccurred())
  64. return true;
  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. return true;
  69. }
  70. void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
  71. if (Diags.hasErrorOccurred())
  72. return;
  73. assert(D->doesThisDeclarationHaveABody());
  74. // We may have member functions that need to be emitted at this point.
  75. if (!D->isDependentContext() &&
  76. (D->hasAttr<UsedAttr>() || D->hasAttr<ConstructorAttr>() ||
  77. D->hasAttr<DLLExportAttr>())) {
  78. Builder->EmitTopLevelDecl(D);
  79. }
  80. }
  81. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  82. /// to (e.g. struct, union, enum, class) is completed. This allows the
  83. /// client hack on the type, which can occur at any point in the file
  84. /// (because these can be defined in declspecs).
  85. void HandleTagDeclDefinition(TagDecl *D) override {
  86. if (Diags.hasErrorOccurred())
  87. return;
  88. Builder->UpdateCompletedType(D);
  89. }
  90. void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
  91. if (Diags.hasErrorOccurred())
  92. return;
  93. if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
  94. if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
  95. DI->completeRequiredType(RD);
  96. }
  97. void HandleTranslationUnit(ASTContext &Ctx) override {
  98. if (Diags.hasErrorOccurred()) {
  99. if (Builder)
  100. Builder->clear();
  101. M.reset();
  102. return;
  103. }
  104. if (Builder)
  105. Builder->Release();
  106. }
  107. void CompleteTentativeDefinition(VarDecl *D) override {
  108. if (Diags.hasErrorOccurred())
  109. return;
  110. Builder->EmitTentativeDefinition(D);
  111. }
  112. void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
  113. if (Diags.hasErrorOccurred())
  114. return;
  115. Builder->EmitVTable(RD, DefinitionRequired);
  116. }
  117. void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
  118. Builder->AppendLinkerOptions(Opts);
  119. }
  120. void HandleDetectMismatch(llvm::StringRef Name,
  121. llvm::StringRef Value) override {
  122. Builder->AddDetectMismatch(Name, Value);
  123. }
  124. void HandleDependentLibrary(llvm::StringRef Lib) override {
  125. Builder->AddDependentLib(Lib);
  126. }
  127. };
  128. }
  129. void CodeGenerator::anchor() { }
  130. CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
  131. const std::string& ModuleName,
  132. const CodeGenOptions &CGO,
  133. const TargetOptions &/*TO*/,
  134. llvm::LLVMContext& C) {
  135. return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
  136. }