ModuleBuilder.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // Emit any deferred inline method definitions.
  83. for (CXXMethodDecl *MD : DeferredInlineMethodDefinitions)
  84. Builder->EmitTopLevelDecl(MD);
  85. DeferredInlineMethodDefinitions.clear();
  86. return true;
  87. }
  88. void HandleInlineMethodDefinition(CXXMethodDecl *D) override {
  89. if (Diags.hasErrorOccurred())
  90. return;
  91. assert(D->doesThisDeclarationHaveABody());
  92. // We may want to emit this definition. However, that decision might be
  93. // based on computing the linkage, and we have to defer that in case we
  94. // are inside of something that will change the method's final linkage,
  95. // e.g.
  96. // typedef struct {
  97. // void bar();
  98. // void foo() { bar(); }
  99. // } A;
  100. DeferredInlineMethodDefinitions.push_back(D);
  101. }
  102. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  103. /// to (e.g. struct, union, enum, class) is completed. This allows the
  104. /// client hack on the type, which can occur at any point in the file
  105. /// (because these can be defined in declspecs).
  106. void HandleTagDeclDefinition(TagDecl *D) override {
  107. if (Diags.hasErrorOccurred())
  108. return;
  109. Builder->UpdateCompletedType(D);
  110. // For MSVC compatibility, treat declarations of static data members with
  111. // inline initializers as definitions.
  112. if (Ctx->getLangOpts().MSVCCompat) {
  113. for (Decl *Member : D->decls()) {
  114. if (VarDecl *VD = dyn_cast<VarDecl>(Member)) {
  115. if (Ctx->isMSStaticDataMemberInlineDefinition(VD) &&
  116. Ctx->DeclMustBeEmitted(VD)) {
  117. Builder->EmitGlobal(VD);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
  124. if (Diags.hasErrorOccurred())
  125. return;
  126. if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo())
  127. if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
  128. DI->completeRequiredType(RD);
  129. }
  130. void HandleTranslationUnit(ASTContext &Ctx) override {
  131. if (Diags.hasErrorOccurred()) {
  132. if (Builder)
  133. Builder->clear();
  134. M.reset();
  135. return;
  136. }
  137. if (Builder)
  138. Builder->Release();
  139. }
  140. void CompleteTentativeDefinition(VarDecl *D) override {
  141. if (Diags.hasErrorOccurred())
  142. return;
  143. Builder->EmitTentativeDefinition(D);
  144. }
  145. void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override {
  146. if (Diags.hasErrorOccurred())
  147. return;
  148. Builder->EmitVTable(RD, DefinitionRequired);
  149. }
  150. void HandleLinkerOptionPragma(llvm::StringRef Opts) override {
  151. Builder->AppendLinkerOptions(Opts);
  152. }
  153. void HandleDetectMismatch(llvm::StringRef Name,
  154. llvm::StringRef Value) override {
  155. Builder->AddDetectMismatch(Name, Value);
  156. }
  157. void HandleDependentLibrary(llvm::StringRef Lib) override {
  158. Builder->AddDependentLib(Lib);
  159. }
  160. private:
  161. std::vector<CXXMethodDecl *> DeferredInlineMethodDefinitions;
  162. };
  163. }
  164. void CodeGenerator::anchor() { }
  165. CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
  166. const std::string& ModuleName,
  167. const CodeGenOptions &CGO,
  168. const TargetOptions &/*TO*/,
  169. llvm::LLVMContext& C) {
  170. return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
  171. }