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 "CGDebugInfo.h" 16 #include "CodeGenModule.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/Basic/Diagnostic.h" 21 #include "clang/Basic/TargetInfo.h" 22 #include "clang/Frontend/CodeGenOptions.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 #include <memory> 28 using namespace clang; 29 30 namespace { 31 class CodeGeneratorImpl : public CodeGenerator { 32 DiagnosticsEngine &Diags; 33 std::unique_ptr<const llvm::DataLayout> TD; 34 ASTContext *Ctx; 35 const CodeGenOptions CodeGenOpts; // Intentionally copied in. 36 protected: 37 std::unique_ptr<llvm::Module> M; 38 std::unique_ptr<CodeGen::CodeGenModule> Builder; 39 40 public: 41 CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName, 42 const CodeGenOptions &CGO, llvm::LLVMContext& C) 43 : Diags(diags), CodeGenOpts(CGO), 44 M(new llvm::Module(ModuleName, C)) {} 45 46 virtual ~CodeGeneratorImpl() {} 47 48 llvm::Module* GetModule() override { 49 return M.get(); 50 } 51 52 llvm::Module *ReleaseModule() override { return M.release(); } 53 54 void Initialize(ASTContext &Context) override { 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 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i) 64 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]); 65 } 66 67 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { 68 if (Diags.hasErrorOccurred()) 69 return; 70 71 Builder->HandleCXXStaticMemberVarInstantiation(VD); 72 } 73 74 bool HandleTopLevelDecl(DeclGroupRef DG) override { 75 if (Diags.hasErrorOccurred()) 76 return true; 77 78 // Make sure to emit all elements of a Decl. 79 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 80 Builder->EmitTopLevelDecl(*I); 81 return true; 82 } 83 84 void HandleInlineMethodDefinition(CXXMethodDecl *D) override { 85 if (Diags.hasErrorOccurred()) 86 return; 87 88 assert(D->doesThisDeclarationHaveABody()); 89 90 // We may have member functions that need to be emitted at this point. 91 if (!D->isDependentContext() && 92 (D->hasAttr<UsedAttr>() || D->hasAttr<ConstructorAttr>() || 93 D->hasAttr<DLLExportAttr>())) { 94 Builder->EmitTopLevelDecl(D); 95 } 96 } 97 98 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 99 /// to (e.g. struct, union, enum, class) is completed. This allows the 100 /// client hack on the type, which can occur at any point in the file 101 /// (because these can be defined in declspecs). 102 void HandleTagDeclDefinition(TagDecl *D) override { 103 if (Diags.hasErrorOccurred()) 104 return; 105 106 Builder->UpdateCompletedType(D); 107 } 108 109 void HandleTagDeclRequiredDefinition(const TagDecl *D) override { 110 if (Diags.hasErrorOccurred()) 111 return; 112 113 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo()) 114 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) 115 DI->completeRequiredType(RD); 116 } 117 118 void HandleTranslationUnit(ASTContext &Ctx) override { 119 if (Diags.hasErrorOccurred()) { 120 if (Builder) 121 Builder->clear(); 122 M.reset(); 123 return; 124 } 125 126 if (Builder) 127 Builder->Release(); 128 } 129 130 void CompleteTentativeDefinition(VarDecl *D) override { 131 if (Diags.hasErrorOccurred()) 132 return; 133 134 Builder->EmitTentativeDefinition(D); 135 } 136 137 void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override { 138 if (Diags.hasErrorOccurred()) 139 return; 140 141 Builder->EmitVTable(RD, DefinitionRequired); 142 } 143 144 void HandleLinkerOptionPragma(llvm::StringRef Opts) override { 145 Builder->AppendLinkerOptions(Opts); 146 } 147 148 void HandleDetectMismatch(llvm::StringRef Name, 149 llvm::StringRef Value) override { 150 Builder->AddDetectMismatch(Name, Value); 151 } 152 153 void HandleDependentLibrary(llvm::StringRef Lib) override { 154 Builder->AddDependentLib(Lib); 155 } 156 }; 157 } 158 159 void CodeGenerator::anchor() { } 160 161 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags, 162 const std::string& ModuleName, 163 const CodeGenOptions &CGO, 164 const TargetOptions &/*TO*/, 165 llvm::LLVMContext& C) { 166 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C); 167 } 168