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 const Decl *GetDeclForMangledName(StringRef MangledName) override { 53 GlobalDecl Result; 54 if (!Builder->lookupRepresentativeDecl(MangledName, Result)) 55 return nullptr; 56 const Decl *D = Result.getCanonicalDecl().getDecl(); 57 if (auto FD = dyn_cast<FunctionDecl>(D)) { 58 if (FD->hasBody(FD)) 59 return FD; 60 } else if (auto TD = dyn_cast<TagDecl>(D)) { 61 if (auto Def = TD->getDefinition()) 62 return Def; 63 } 64 return D; 65 } 66 67 llvm::Module *ReleaseModule() override { return M.release(); } 68 69 void Initialize(ASTContext &Context) override { 70 Ctx = &Context; 71 72 M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); 73 M->setDataLayout(Ctx->getTargetInfo().getTargetDescription()); 74 TD.reset(new llvm::DataLayout(Ctx->getTargetInfo().getTargetDescription())); 75 Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts, *M, *TD, 76 Diags)); 77 78 for (size_t i = 0, e = CodeGenOpts.DependentLibraries.size(); i < e; ++i) 79 HandleDependentLibrary(CodeGenOpts.DependentLibraries[i]); 80 } 81 82 void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { 83 if (Diags.hasErrorOccurred()) 84 return; 85 86 Builder->HandleCXXStaticMemberVarInstantiation(VD); 87 } 88 89 bool HandleTopLevelDecl(DeclGroupRef DG) override { 90 if (Diags.hasErrorOccurred()) 91 return true; 92 93 // Make sure to emit all elements of a Decl. 94 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 95 Builder->EmitTopLevelDecl(*I); 96 97 // Emit any deferred inline method definitions. Note that more deferred 98 // methods may be added during this loop, since ASTConsumer callbacks 99 // can be invoked if AST inspection results in declarations being added. 100 for (unsigned I = 0; I != DeferredInlineMethodDefinitions.size(); ++I) 101 Builder->EmitTopLevelDecl(DeferredInlineMethodDefinitions[I]); 102 DeferredInlineMethodDefinitions.clear(); 103 104 return true; 105 } 106 107 void HandleInlineMethodDefinition(CXXMethodDecl *D) override { 108 if (Diags.hasErrorOccurred()) 109 return; 110 111 assert(D->doesThisDeclarationHaveABody()); 112 113 // We may want to emit this definition. However, that decision might be 114 // based on computing the linkage, and we have to defer that in case we 115 // are inside of something that will change the method's final linkage, 116 // e.g. 117 // typedef struct { 118 // void bar(); 119 // void foo() { bar(); } 120 // } A; 121 DeferredInlineMethodDefinitions.push_back(D); 122 } 123 124 /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 125 /// to (e.g. struct, union, enum, class) is completed. This allows the 126 /// client hack on the type, which can occur at any point in the file 127 /// (because these can be defined in declspecs). 128 void HandleTagDeclDefinition(TagDecl *D) override { 129 if (Diags.hasErrorOccurred()) 130 return; 131 132 Builder->UpdateCompletedType(D); 133 134 // For MSVC compatibility, treat declarations of static data members with 135 // inline initializers as definitions. 136 if (Ctx->getLangOpts().MSVCCompat) { 137 for (Decl *Member : D->decls()) { 138 if (VarDecl *VD = dyn_cast<VarDecl>(Member)) { 139 if (Ctx->isMSStaticDataMemberInlineDefinition(VD) && 140 Ctx->DeclMustBeEmitted(VD)) { 141 Builder->EmitGlobal(VD); 142 } 143 } 144 } 145 } 146 } 147 148 void HandleTagDeclRequiredDefinition(const TagDecl *D) override { 149 if (Diags.hasErrorOccurred()) 150 return; 151 152 if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo()) 153 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) 154 DI->completeRequiredType(RD); 155 } 156 157 void HandleTranslationUnit(ASTContext &Ctx) override { 158 if (Diags.hasErrorOccurred()) { 159 if (Builder) 160 Builder->clear(); 161 M.reset(); 162 return; 163 } 164 165 if (Builder) 166 Builder->Release(); 167 } 168 169 void CompleteTentativeDefinition(VarDecl *D) override { 170 if (Diags.hasErrorOccurred()) 171 return; 172 173 Builder->EmitTentativeDefinition(D); 174 } 175 176 void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) override { 177 if (Diags.hasErrorOccurred()) 178 return; 179 180 Builder->EmitVTable(RD, DefinitionRequired); 181 } 182 183 void HandleLinkerOptionPragma(llvm::StringRef Opts) override { 184 Builder->AppendLinkerOptions(Opts); 185 } 186 187 void HandleDetectMismatch(llvm::StringRef Name, 188 llvm::StringRef Value) override { 189 Builder->AddDetectMismatch(Name, Value); 190 } 191 192 void HandleDependentLibrary(llvm::StringRef Lib) override { 193 Builder->AddDependentLib(Lib); 194 } 195 196 private: 197 std::vector<CXXMethodDecl *> DeferredInlineMethodDefinitions; 198 }; 199 } 200 201 void CodeGenerator::anchor() { } 202 203 CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags, 204 const std::string& ModuleName, 205 const CodeGenOptions &CGO, 206 const TargetOptions &/*TO*/, 207 llvm::LLVMContext& C) { 208 return new CodeGeneratorImpl(Diags, ModuleName, CGO, C); 209 } 210