ModuleBuilder.cpp 5.2 KB

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