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/ASTContext.h" 17 #include "clang/AST/DeclObjC.h" 18 #include "clang/AST/Expr.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 "llvm/Module.h" 27 #include "llvm/Target/TargetData.h" 28 #include "llvm/Support/Compiler.h" 29 #include "llvm/ADT/OwningPtr.h" 30 31 32 namespace { 33 class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator { 34 Diagnostic &Diags; 35 llvm::OwningPtr<const llvm::TargetData> TD; 36 ASTContext *Ctx; 37 const LangOptions &Features; 38 bool GenerateDebugInfo; 39 protected: 40 llvm::OwningPtr<llvm::Module> M; 41 llvm::OwningPtr<CodeGen::CodeGenModule> Builder; 42 public: 43 CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO, 44 const std::string& ModuleName, 45 bool DebugInfoFlag) 46 : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag), 47 M(new llvm::Module(ModuleName)) {} 48 49 virtual ~CodeGeneratorImpl() {} 50 51 virtual llvm::Module* ReleaseModule() { 52 return M.take(); 53 } 54 55 virtual void Initialize(ASTContext &Context) { 56 Ctx = &Context; 57 58 M->setTargetTriple(Ctx->Target.getTargetTriple()); 59 M->setDataLayout(Ctx->Target.getTargetDescription()); 60 TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription())); 61 Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD, 62 Diags, GenerateDebugInfo)); 63 } 64 65 virtual void HandleTopLevelDecl(Decl *D) { 66 // Make sure to emit all elements of a ScopedDecl. 67 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { 68 for (; SD; SD = SD->getNextDeclarator()) 69 Builder->EmitTopLevelDecl(SD); 70 } else { 71 Builder->EmitTopLevelDecl(D); 72 } 73 } 74 75 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 76 /// (e.g. struct, union, enum, class) is completed. This allows the client to 77 /// hack on the type, which can occur at any point in the file (because these 78 /// can be defined in declspecs). 79 virtual void HandleTagDeclDefinition(TagDecl *D) { 80 Builder->UpdateCompletedType(D); 81 } 82 83 virtual void HandleTranslationUnit(TranslationUnit& TU) { 84 if (Diags.hasErrorOccurred()) { 85 M.reset(); 86 return; 87 } 88 89 if (Builder) 90 Builder->Release(); 91 }; 92 }; 93 } 94 95 CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags, 96 const LangOptions &Features, 97 const std::string& ModuleName, 98 bool GenerateDebugInfo) { 99 return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo); 100 } 101