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 #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/IR/DataLayout.h" 24 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/IR/Module.h" 26 using namespace clang; 27 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 43 virtual ~CodeGeneratorImpl() {} 44 45 virtual llvm::Module* GetModule() { 46 return M.get(); 47 } 48 49 virtual llvm::Module* ReleaseModule() { 50 return M.take(); 51 } 52 53 virtual void Initialize(ASTContext &Context) { 54 Ctx = &Context; 55 56 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); 57 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription()); 58 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription())); 59 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD, 60 Diags)); 61 } 62 63 virtual void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { 64 Builder->HandleCXXStaticMemberVarInstantiation(VD); 65 } 66 67 virtual bool HandleTopLevelDecl(DeclGroupRef DG) { 68 // Make sure to emit all elements of a Decl. 69 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 70 Builder->EmitTopLevelDecl(*I); 71 return true; 72 } 73 74 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 75 /// to (e.g. struct, union, enum, class) is completed. This allows the 76 /// client hack on the type, which can occur at any point in the file 77 /// (because these can be defined in declspecs). 78 virtual void HandleTagDeclDefinition(TagDecl *D) { 79 Builder->UpdateCompletedType(D); 80 81 // In C++, we may have member functions that need to be emitted at this 82 // point. 83 if (Ctx->getLangOpts().CPlusPlus && !D->isDependentContext()) { 84 for (DeclContext::decl_iterator M = D->decls_begin(), 85 MEnd = D->decls_end(); 86 M != MEnd; ++M) 87 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M)) 88 if (Method->doesThisDeclarationHaveABody() && 89 (Method->hasAttr<UsedAttr>() || 90 Method->hasAttr<ConstructorAttr>())) 91 Builder->EmitTopLevelDecl(Method); 92 } 93 } 94 95 virtual void HandleTranslationUnit(ASTContext &Ctx) { 96 if (Diags.hasErrorOccurred()) { 97 M.reset(); 98 return; 99 } 100 101 if (Builder) 102 Builder->Release(); 103 } 104 105 virtual void CompleteTentativeDefinition(VarDecl *D) { 106 if (Diags.hasErrorOccurred()) 107 return; 108 109 Builder->EmitTentativeDefinition(D); 110 } 111 112 virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) { 113 if (Diags.hasErrorOccurred()) 114 return; 115 116 Builder->EmitVTable(RD, DefinitionRequired); 117 } 118 }; 119 } 120 121 void CodeGenerator::anchor() { } 122 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 } 130