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