ModuleBuilder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. const Decl *GetDeclForMangledName(StringRef MangledName) override {
  47. GlobalDecl Result;
  48. if (!Builder->lookupRepresentativeDecl(MangledName, Result))
  49. return nullptr;
  50. const Decl *D = Result.getCanonicalDecl().getDecl();
  51. if (auto FD = dyn_cast<FunctionDecl>(D)) {
  52. if (FD->hasBody(FD))
  53. return FD;
  54. } else if (auto TD = dyn_cast<TagDecl>(D)) {
  55. if (auto Def = TD->getDefinition())
  56. return Def;
  57. }
  58. return D;
  59. }
  60. llvm::Module *ReleaseModule() override { return M.release(); }
  61. void Initialize(ASTContext &Context) override {
  62. Ctx = &Context;
  63. M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
  64. M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
  65. TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
  66. Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
  67. Diags));
  68. for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
  69. HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
  70. }
  71. void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
  72. if (Diags.hasErrorOccurred())
  73. return;
  74. Builder->HandleCXXStaticMemberVarInstantiation(VD);
  75. }
  76. bool HandleTopLevelDecl(DeclGroupRef DG) override {
  77. if (Diags.hasErrorOccurred())
  78. return true;
  79. // Make sure to emit all elements of a Decl.
  80. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
  81. Builder->EmitTopLevelDecl(*I);
  82. return true;
  83. }
  84. void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
  85. if (Diags.hasErrorOccurred())
  86. return;
  87. assert(D->doesThisDeclarationHaveABody());
  88. // We may have member functions that need to be emitted at this point.
  89. if (!D->isDependentContext() &&
  90. (D->hasAttr<UsedAttr>() || D->hasAttr<ConstructorAttr>() ||
  91. D->hasAttr<DLLExportAttr>())) {
  92. Builder->EmitTopLevelDecl(D);
  93. }
  94. }
  95. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  96. /// to (e.g. struct, union, enum, class) is completed. This allows the
  97. /// client hack on the type, which can occur at any point in the file
  98. /// (because these can be defined in declspecs).
  99. void HandleTagDeclDefinition(TagDecl *D) override {
  100. if (Diags.hasErrorOccurred())
  101. return;
  102. Builder->UpdateCompletedType(D);
  103. }
  104. void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
  105. if (Diags.hasErrorOccurred())
  106. return;
  107. if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
  108. if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
  109. DI->completeRequiredType(RD);
  110. }
  111. void HandleTranslationUnit(ASTContext &Ctx) override {
  112. if (Diags.hasErrorOccurred()) {
  113. if (Builder)
  114. Builder->clear();
  115. M.reset();
  116. return;
  117. }
  118. if (Builder)
  119. Builder->Release();
  120. }
  121. void CompleteTentativeDefinition(VarDecl *D) override {
  122. if (Diags.hasErrorOccurred())
  123. return;
  124. Builder->EmitTentativeDefinition(D);
  125. }
  126. void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
  127. if (Diags.hasErrorOccurred())
  128. return;
  129. Builder->EmitVTable(RD, DefinitionRequired);
  130. }
  131. void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
  132. Builder->AppendLinkerOptions(Opts);
  133. }
  134. void HandleDetectMismatch(llvm::StringRef Name,
  135. llvm::StringRef Value) override {
  136. Builder->AddDetectMismatch(Name, Value);
  137. }
  138. void HandleDependentLibrary(llvm::StringRef Lib) override {
  139. Builder->AddDependentLib(Lib);
  140. }
  141. };
  142. }
  143. void CodeGenerator::anchor() { }
  144. CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
  145. const std::string& ModuleName,
  146. const CodeGenOptions &CGO,
  147. const TargetOptions &/*TO*/,
  148. llvm::LLVMContext& C) {
  149. return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
  150. }