ModuleBuilder.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "clang/AST/ASTConsumer.h"
  16. #include "clang/AST/ASTContext.h"
  17. #include "clang/AST/Decl.h"
  18. using namespace clang;
  19. //===----------------------------------------------------------------------===//
  20. // LLVM Emitter
  21. #include "clang/Basic/Diagnostic.h"
  22. #include "clang/Basic/TargetInfo.h"
  23. #include "clang/CodeGen/ModuleBuilder.h"
  24. #include "llvm/Module.h"
  25. #include "llvm/Target/TargetData.h"
  26. #include "llvm/Target/TargetMachine.h"
  27. namespace {
  28. class CodeGenerator : public ASTConsumer {
  29. Diagnostic &Diags;
  30. const llvm::TargetData *TD;
  31. ASTContext *Ctx;
  32. const LangOptions &Features;
  33. protected:
  34. llvm::Module *&M;
  35. CodeGen::CodeGenModule *Builder;
  36. public:
  37. CodeGenerator(Diagnostic &diags, const LangOptions &LO,
  38. llvm::Module *&DestModule)
  39. : Diags(diags), Features(LO), M(DestModule) {}
  40. ~CodeGenerator() {
  41. delete Builder;
  42. }
  43. virtual void Initialize(ASTContext &Context) {
  44. Ctx = &Context;
  45. M->setTargetTriple(Ctx->Target.getTargetTriple());
  46. M->setDataLayout(Ctx->Target.getTargetDescription());
  47. TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
  48. Builder = new CodeGen::CodeGenModule(Context, Features, *M, *TD, Diags);
  49. }
  50. virtual void HandleTopLevelDecl(Decl *D) {
  51. // If an error occurred, stop code generation, but continue parsing and
  52. // semantic analysis (to ensure all warnings and errors are emitted).
  53. if (Diags.hasErrorOccurred())
  54. return;
  55. if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  56. Builder->EmitFunction(FD);
  57. } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
  58. Builder->EmitGlobalVarDeclarator(FVD);
  59. } else if (isa<ObjCClassDecl>(D) || isa<ObjCCategoryDecl>(D)) {
  60. // Forward declaration. Only used for type checking.
  61. } else if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)){
  62. Builder->EmitObjCMethod(OMD);
  63. } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
  64. if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx)
  65. Builder->WarnUnsupported(LSD, "linkage spec");
  66. // FIXME: implement C++ linkage, C linkage works mostly by C
  67. // language reuse already.
  68. } else if (FileScopeAsmDecl *AD = dyn_cast<FileScopeAsmDecl>(D)) {
  69. std::string AsmString(AD->getAsmString()->getStrData(),
  70. AD->getAsmString()->getByteLength());
  71. const std::string &S = Builder->getModule().getModuleInlineAsm();
  72. if (S.empty())
  73. Builder->getModule().setModuleInlineAsm(AsmString);
  74. else
  75. Builder->getModule().setModuleInlineAsm(S + '\n' + AsmString);
  76. } else {
  77. assert(isa<TypeDecl>(D) && "Unknown top level decl");
  78. // TODO: handle debug info?
  79. }
  80. }
  81. /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
  82. /// (e.g. struct, union, enum, class) is completed. This allows the client to
  83. /// hack on the type, which can occur at any point in the file (because these
  84. /// can be defined in declspecs).
  85. virtual void HandleTagDeclDefinition(TagDecl *D) {
  86. Builder->UpdateCompletedType(D);
  87. }
  88. };
  89. }
  90. ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags,
  91. const LangOptions &Features,
  92. llvm::Module *&DestModule) {
  93. return new CodeGenerator(Diags, Features, DestModule);
  94. }