ModuleBuilder.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. unsigned HandlingTopLevelDecls;
  35. struct HandlingTopLevelDeclRAII {
  36. CodeGeneratorImpl &Self;
  37. HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self) : Self(Self) {
  38. ++Self.HandlingTopLevelDecls;
  39. }
  40. ~HandlingTopLevelDeclRAII() {
  41. if (--Self.HandlingTopLevelDecls == 0)
  42. Self.EmitDeferredDecls();
  43. }
  44. };
  45. protected:
  46. std::unique_ptr<llvm::Module> M;
  47. std::unique_ptr<CodeGen::CodeGenModule> Builder;
  48. public:
  49. CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
  50. const CodeGenOptions &CGO, llvm::LLVMContext& C)
  51. : Diags(diags), CodeGenOpts(CGO), HandlingTopLevelDecls(0),
  52. M(new llvm::Module(ModuleName, C)) {}
  53. virtual ~CodeGeneratorImpl() {}
  54. llvm::Module* GetModule() override {
  55. return M.get();
  56. }
  57. const Decl *GetDeclForMangledName(StringRef MangledName) override {
  58. GlobalDecl Result;
  59. if (!Builder->lookupRepresentativeDecl(MangledName, Result))
  60. return nullptr;
  61. const Decl *D = Result.getCanonicalDecl().getDecl();
  62. if (auto FD = dyn_cast<FunctionDecl>(D)) {
  63. if (FD->hasBody(FD))
  64. return FD;
  65. } else if (auto TD = dyn_cast<TagDecl>(D)) {
  66. if (auto Def = TD->getDefinition())
  67. return Def;
  68. }
  69. return D;
  70. }
  71. llvm::Module *ReleaseModule() override { return M.release(); }
  72. void Initialize(ASTContext &Context) override {
  73. Ctx = &Context;
  74. M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
  75. M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
  76. TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription()));
  77. Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD,
  78. Diags));
  79. for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i)
  80. HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]);
  81. }
  82. void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
  83. if (Diags.hasErrorOccurred())
  84. return;
  85. Builder->HandleCXXStaticMemberVarInstantiation(VD);
  86. }
  87. bool HandleTopLevelDecl(DeclGroupRef DG) override {
  88. if (Diags.hasErrorOccurred())
  89. return true;
  90. HandlingTopLevelDeclRAII HandlingDecl(*this);
  91. // Make sure to emit all elements of a Decl.
  92. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
  93. Builder->EmitTopLevelDecl(*I);
  94. return true;
  95. }
  96. void EmitDeferredDecls() {
  97. // Emit any deferred inline method definitions. Note that more deferred
  98. // methods may be added during this loop, since ASTConsumer callbacks
  99. // can be invoked if AST inspection results in declarations being added.
  100. for (unsigned I = 0; I < DeferredInlineMethodDefinitions.size(); ++I)
  101. Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]);
  102. DeferredInlineMethodDefinitions.clear();
  103. }
  104. void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
  105. if (Diags.hasErrorOccurred())
  106. return;
  107. assert(D->doesThisDeclarationHaveABody());
  108. // We may want to emit this definition. However, that decision might be
  109. // based on computing the linkage, and we have to defer that in case we
  110. // are inside of something that will change the method's final linkage,
  111. // e.g.
  112. // typedef struct {
  113. // void bar();
  114. // void foo() { bar(); }
  115. // } A;
  116. DeferredInlineMethodDefinitions.push_back(D);
  117. }
  118. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  119. /// to (e.g. struct, union, enum, class) is completed. This allows the
  120. /// client hack on the type, which can occur at any point in the file
  121. /// (because these can be defined in declspecs).
  122. void HandleTagDeclDefinition(TagDecl *D) override {
  123. if (Diags.hasErrorOccurred())
  124. return;
  125. Builder->UpdateCompletedType(D);
  126. // For MSVC compatibility, treat declarations of static data members with
  127. // inline initializers as definitions.
  128. if (Ctx->getLangOpts().MSVCCompat) {
  129. for (Decl *Member : D->decls()) {
  130. if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
  131. if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
  132. Ctx->DeclMustBeEmitted(VD)) {
  133. Builder->EmitGlobal(VD);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
  140. if (Diags.hasErrorOccurred())
  141. return;
  142. if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
  143. if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
  144. DI->completeRequiredType(RD);
  145. }
  146. void HandleTranslationUnit(ASTContext &Ctx) override {
  147. if (Diags.hasErrorOccurred()) {
  148. if (Builder)
  149. Builder->clear();
  150. M.reset();
  151. return;
  152. }
  153. if (Builder)
  154. Builder->Release();
  155. }
  156. void CompleteTentativeDefinition(VarDecl *D) override {
  157. if (Diags.hasErrorOccurred())
  158. return;
  159. Builder->EmitTentativeDefinition(D);
  160. }
  161. void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
  162. if (Diags.hasErrorOccurred())
  163. return;
  164. Builder->EmitVTable(RD, DefinitionRequired);
  165. }
  166. void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
  167. Builder->AppendLinkerOptions(Opts);
  168. }
  169. void HandleDetectMismatch(llvm::StringRef Name,
  170. llvm::StringRef Value) override {
  171. Builder->AddDetectMismatch(Name, Value);
  172. }
  173. void HandleDependentLibrary(llvm::StringRef Lib) override {
  174. Builder->AddDependentLib(Lib);
  175. }
  176. private:
  177. std::vector<CXXMethodDecl *> DeferredInlineMethodDefinitions;
  178. };
  179. }
  180. void CodeGenerator::anchor() { }
  181. CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
  182. const std::string& ModuleName,
  183. const CodeGenOptions &CGO,
  184. const TargetOptions &/*TO*/,
  185. llvm::LLVMContext& C) {
  186. return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
  187. }