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