1*0b57cec5SDimitry Andric //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This builds an AST and converts it to LLVM Code. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "clang/CodeGen/ModuleBuilder.h" 14*0b57cec5SDimitry Andric #include "CGDebugInfo.h" 15*0b57cec5SDimitry Andric #include "CodeGenModule.h" 16*0b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 17*0b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h" 18*0b57cec5SDimitry Andric #include "clang/AST/Expr.h" 19*0b57cec5SDimitry Andric #include "clang/Basic/CodeGenOptions.h" 20*0b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h" 21*0b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 23*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 24*0b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 25*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 26*0b57cec5SDimitry Andric #include <memory> 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric using namespace clang; 29*0b57cec5SDimitry Andric using namespace CodeGen; 30*0b57cec5SDimitry Andric 31*0b57cec5SDimitry Andric namespace { 32*0b57cec5SDimitry Andric class CodeGeneratorImpl : public CodeGenerator { 33*0b57cec5SDimitry Andric DiagnosticsEngine &Diags; 34*0b57cec5SDimitry Andric ASTContext *Ctx; 35*0b57cec5SDimitry Andric const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info. 36*0b57cec5SDimitry Andric const PreprocessorOptions &PreprocessorOpts; // Only used for debug info. 37*0b57cec5SDimitry Andric const CodeGenOptions CodeGenOpts; // Intentionally copied in. 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric unsigned HandlingTopLevelDecls; 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric /// Use this when emitting decls to block re-entrant decl emission. It will 42*0b57cec5SDimitry Andric /// emit all deferred decls on scope exit. Set EmitDeferred to false if decl 43*0b57cec5SDimitry Andric /// emission must be deferred longer, like at the end of a tag definition. 44*0b57cec5SDimitry Andric struct HandlingTopLevelDeclRAII { 45*0b57cec5SDimitry Andric CodeGeneratorImpl &Self; 46*0b57cec5SDimitry Andric bool EmitDeferred; 47*0b57cec5SDimitry Andric HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self, 48*0b57cec5SDimitry Andric bool EmitDeferred = true) 49*0b57cec5SDimitry Andric : Self(Self), EmitDeferred(EmitDeferred) { 50*0b57cec5SDimitry Andric ++Self.HandlingTopLevelDecls; 51*0b57cec5SDimitry Andric } 52*0b57cec5SDimitry Andric ~HandlingTopLevelDeclRAII() { 53*0b57cec5SDimitry Andric unsigned Level = --Self.HandlingTopLevelDecls; 54*0b57cec5SDimitry Andric if (Level == 0 && EmitDeferred) 55*0b57cec5SDimitry Andric Self.EmitDeferredDecls(); 56*0b57cec5SDimitry Andric } 57*0b57cec5SDimitry Andric }; 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric CoverageSourceInfo *CoverageInfo; 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric protected: 62*0b57cec5SDimitry Andric std::unique_ptr<llvm::Module> M; 63*0b57cec5SDimitry Andric std::unique_ptr<CodeGen::CodeGenModule> Builder; 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric private: 66*0b57cec5SDimitry Andric SmallVector<FunctionDecl *, 8> DeferredInlineMemberFuncDefs; 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric public: 69*0b57cec5SDimitry Andric CodeGeneratorImpl(DiagnosticsEngine &diags, llvm::StringRef ModuleName, 70*0b57cec5SDimitry Andric const HeaderSearchOptions &HSO, 71*0b57cec5SDimitry Andric const PreprocessorOptions &PPO, const CodeGenOptions &CGO, 72*0b57cec5SDimitry Andric llvm::LLVMContext &C, 73*0b57cec5SDimitry Andric CoverageSourceInfo *CoverageInfo = nullptr) 74*0b57cec5SDimitry Andric : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO), 75*0b57cec5SDimitry Andric PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0), 76*0b57cec5SDimitry Andric CoverageInfo(CoverageInfo), M(new llvm::Module(ModuleName, C)) { 77*0b57cec5SDimitry Andric C.setDiscardValueNames(CGO.DiscardValueNames); 78*0b57cec5SDimitry Andric } 79*0b57cec5SDimitry Andric 80*0b57cec5SDimitry Andric ~CodeGeneratorImpl() override { 81*0b57cec5SDimitry Andric // There should normally not be any leftover inline method definitions. 82*0b57cec5SDimitry Andric assert(DeferredInlineMemberFuncDefs.empty() || 83*0b57cec5SDimitry Andric Diags.hasErrorOccurred()); 84*0b57cec5SDimitry Andric } 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric CodeGenModule &CGM() { 87*0b57cec5SDimitry Andric return *Builder; 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric llvm::Module *GetModule() { 91*0b57cec5SDimitry Andric return M.get(); 92*0b57cec5SDimitry Andric } 93*0b57cec5SDimitry Andric 94*0b57cec5SDimitry Andric CGDebugInfo *getCGDebugInfo() { 95*0b57cec5SDimitry Andric return Builder->getModuleDebugInfo(); 96*0b57cec5SDimitry Andric } 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric llvm::Module *ReleaseModule() { 99*0b57cec5SDimitry Andric return M.release(); 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric const Decl *GetDeclForMangledName(StringRef MangledName) { 103*0b57cec5SDimitry Andric GlobalDecl Result; 104*0b57cec5SDimitry Andric if (!Builder->lookupRepresentativeDecl(MangledName, Result)) 105*0b57cec5SDimitry Andric return nullptr; 106*0b57cec5SDimitry Andric const Decl *D = Result.getCanonicalDecl().getDecl(); 107*0b57cec5SDimitry Andric if (auto FD = dyn_cast<FunctionDecl>(D)) { 108*0b57cec5SDimitry Andric if (FD->hasBody(FD)) 109*0b57cec5SDimitry Andric return FD; 110*0b57cec5SDimitry Andric } else if (auto TD = dyn_cast<TagDecl>(D)) { 111*0b57cec5SDimitry Andric if (auto Def = TD->getDefinition()) 112*0b57cec5SDimitry Andric return Def; 113*0b57cec5SDimitry Andric } 114*0b57cec5SDimitry Andric return D; 115*0b57cec5SDimitry Andric } 116*0b57cec5SDimitry Andric 117*0b57cec5SDimitry Andric llvm::Constant *GetAddrOfGlobal(GlobalDecl global, bool isForDefinition) { 118*0b57cec5SDimitry Andric return Builder->GetAddrOfGlobal(global, ForDefinition_t(isForDefinition)); 119*0b57cec5SDimitry Andric } 120*0b57cec5SDimitry Andric 121*0b57cec5SDimitry Andric llvm::Module *StartModule(llvm::StringRef ModuleName, 122*0b57cec5SDimitry Andric llvm::LLVMContext &C) { 123*0b57cec5SDimitry Andric assert(!M && "Replacing existing Module?"); 124*0b57cec5SDimitry Andric M.reset(new llvm::Module(ModuleName, C)); 125*0b57cec5SDimitry Andric Initialize(*Ctx); 126*0b57cec5SDimitry Andric return M.get(); 127*0b57cec5SDimitry Andric } 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric void Initialize(ASTContext &Context) override { 130*0b57cec5SDimitry Andric Ctx = &Context; 131*0b57cec5SDimitry Andric 132*0b57cec5SDimitry Andric M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); 133*0b57cec5SDimitry Andric M->setDataLayout(Ctx->getTargetInfo().getDataLayout()); 134*0b57cec5SDimitry Andric const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion(); 135*0b57cec5SDimitry Andric if (!SDKVersion.empty()) 136*0b57cec5SDimitry Andric M->setSDKVersion(SDKVersion); 137*0b57cec5SDimitry Andric Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts, 138*0b57cec5SDimitry Andric PreprocessorOpts, CodeGenOpts, 139*0b57cec5SDimitry Andric *M, Diags, CoverageInfo)); 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric for (auto &&Lib : CodeGenOpts.DependentLibraries) 142*0b57cec5SDimitry Andric Builder->AddDependentLib(Lib); 143*0b57cec5SDimitry Andric for (auto &&Opt : CodeGenOpts.LinkerOptions) 144*0b57cec5SDimitry Andric Builder->AppendLinkerOptions(Opt); 145*0b57cec5SDimitry Andric } 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { 148*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 149*0b57cec5SDimitry Andric return; 150*0b57cec5SDimitry Andric 151*0b57cec5SDimitry Andric Builder->HandleCXXStaticMemberVarInstantiation(VD); 152*0b57cec5SDimitry Andric } 153*0b57cec5SDimitry Andric 154*0b57cec5SDimitry Andric bool HandleTopLevelDecl(DeclGroupRef DG) override { 155*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 156*0b57cec5SDimitry Andric return true; 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric HandlingTopLevelDeclRAII HandlingDecl(*this); 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric // Make sure to emit all elements of a Decl. 161*0b57cec5SDimitry Andric for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) 162*0b57cec5SDimitry Andric Builder->EmitTopLevelDecl(*I); 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric return true; 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric void EmitDeferredDecls() { 168*0b57cec5SDimitry Andric if (DeferredInlineMemberFuncDefs.empty()) 169*0b57cec5SDimitry Andric return; 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric // Emit any deferred inline method definitions. Note that more deferred 172*0b57cec5SDimitry Andric // methods may be added during this loop, since ASTConsumer callbacks 173*0b57cec5SDimitry Andric // can be invoked if AST inspection results in declarations being added. 174*0b57cec5SDimitry Andric HandlingTopLevelDeclRAII HandlingDecl(*this); 175*0b57cec5SDimitry Andric for (unsigned I = 0; I != DeferredInlineMemberFuncDefs.size(); ++I) 176*0b57cec5SDimitry Andric Builder->EmitTopLevelDecl(DeferredInlineMemberFuncDefs[I]); 177*0b57cec5SDimitry Andric DeferredInlineMemberFuncDefs.clear(); 178*0b57cec5SDimitry Andric } 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric void HandleInlineFunctionDefinition(FunctionDecl *D) override { 181*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 182*0b57cec5SDimitry Andric return; 183*0b57cec5SDimitry Andric 184*0b57cec5SDimitry Andric assert(D->doesThisDeclarationHaveABody()); 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric // We may want to emit this definition. However, that decision might be 187*0b57cec5SDimitry Andric // based on computing the linkage, and we have to defer that in case we 188*0b57cec5SDimitry Andric // are inside of something that will change the method's final linkage, 189*0b57cec5SDimitry Andric // e.g. 190*0b57cec5SDimitry Andric // typedef struct { 191*0b57cec5SDimitry Andric // void bar(); 192*0b57cec5SDimitry Andric // void foo() { bar(); } 193*0b57cec5SDimitry Andric // } A; 194*0b57cec5SDimitry Andric DeferredInlineMemberFuncDefs.push_back(D); 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric // Provide some coverage mapping even for methods that aren't emitted. 197*0b57cec5SDimitry Andric // Don't do this for templated classes though, as they may not be 198*0b57cec5SDimitry Andric // instantiable. 199*0b57cec5SDimitry Andric if (!D->getLexicalDeclContext()->isDependentContext()) 200*0b57cec5SDimitry Andric Builder->AddDeferredUnusedCoverageMapping(D); 201*0b57cec5SDimitry Andric } 202*0b57cec5SDimitry Andric 203*0b57cec5SDimitry Andric /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl 204*0b57cec5SDimitry Andric /// to (e.g. struct, union, enum, class) is completed. This allows the 205*0b57cec5SDimitry Andric /// client hack on the type, which can occur at any point in the file 206*0b57cec5SDimitry Andric /// (because these can be defined in declspecs). 207*0b57cec5SDimitry Andric void HandleTagDeclDefinition(TagDecl *D) override { 208*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 209*0b57cec5SDimitry Andric return; 210*0b57cec5SDimitry Andric 211*0b57cec5SDimitry Andric // Don't allow re-entrant calls to CodeGen triggered by PCH 212*0b57cec5SDimitry Andric // deserialization to emit deferred decls. 213*0b57cec5SDimitry Andric HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false); 214*0b57cec5SDimitry Andric 215*0b57cec5SDimitry Andric Builder->UpdateCompletedType(D); 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric // For MSVC compatibility, treat declarations of static data members with 218*0b57cec5SDimitry Andric // inline initializers as definitions. 219*0b57cec5SDimitry Andric if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) { 220*0b57cec5SDimitry Andric for (Decl *Member : D->decls()) { 221*0b57cec5SDimitry Andric if (VarDecl *VD = dyn_cast<VarDecl>(Member)) { 222*0b57cec5SDimitry Andric if (Ctx->isMSStaticDataMemberInlineDefinition(VD) && 223*0b57cec5SDimitry Andric Ctx->DeclMustBeEmitted(VD)) { 224*0b57cec5SDimitry Andric Builder->EmitGlobal(VD); 225*0b57cec5SDimitry Andric } 226*0b57cec5SDimitry Andric } 227*0b57cec5SDimitry Andric } 228*0b57cec5SDimitry Andric } 229*0b57cec5SDimitry Andric // For OpenMP emit declare reduction functions, if required. 230*0b57cec5SDimitry Andric if (Ctx->getLangOpts().OpenMP) { 231*0b57cec5SDimitry Andric for (Decl *Member : D->decls()) { 232*0b57cec5SDimitry Andric if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) { 233*0b57cec5SDimitry Andric if (Ctx->DeclMustBeEmitted(DRD)) 234*0b57cec5SDimitry Andric Builder->EmitGlobal(DRD); 235*0b57cec5SDimitry Andric } 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric } 238*0b57cec5SDimitry Andric } 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric void HandleTagDeclRequiredDefinition(const TagDecl *D) override { 241*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 242*0b57cec5SDimitry Andric return; 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric // Don't allow re-entrant calls to CodeGen triggered by PCH 245*0b57cec5SDimitry Andric // deserialization to emit deferred decls. 246*0b57cec5SDimitry Andric HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false); 247*0b57cec5SDimitry Andric 248*0b57cec5SDimitry Andric if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo()) 249*0b57cec5SDimitry Andric if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) 250*0b57cec5SDimitry Andric DI->completeRequiredType(RD); 251*0b57cec5SDimitry Andric } 252*0b57cec5SDimitry Andric 253*0b57cec5SDimitry Andric void HandleTranslationUnit(ASTContext &Ctx) override { 254*0b57cec5SDimitry Andric // Release the Builder when there is no error. 255*0b57cec5SDimitry Andric if (!Diags.hasErrorOccurred() && Builder) 256*0b57cec5SDimitry Andric Builder->Release(); 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric // If there are errors before or when releasing the Builder, reset 259*0b57cec5SDimitry Andric // the module to stop here before invoking the backend. 260*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) { 261*0b57cec5SDimitry Andric if (Builder) 262*0b57cec5SDimitry Andric Builder->clear(); 263*0b57cec5SDimitry Andric M.reset(); 264*0b57cec5SDimitry Andric return; 265*0b57cec5SDimitry Andric } 266*0b57cec5SDimitry Andric } 267*0b57cec5SDimitry Andric 268*0b57cec5SDimitry Andric void AssignInheritanceModel(CXXRecordDecl *RD) override { 269*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 270*0b57cec5SDimitry Andric return; 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric Builder->RefreshTypeCacheForClass(RD); 273*0b57cec5SDimitry Andric } 274*0b57cec5SDimitry Andric 275*0b57cec5SDimitry Andric void CompleteTentativeDefinition(VarDecl *D) override { 276*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 277*0b57cec5SDimitry Andric return; 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric Builder->EmitTentativeDefinition(D); 280*0b57cec5SDimitry Andric } 281*0b57cec5SDimitry Andric 282*0b57cec5SDimitry Andric void HandleVTable(CXXRecordDecl *RD) override { 283*0b57cec5SDimitry Andric if (Diags.hasErrorOccurred()) 284*0b57cec5SDimitry Andric return; 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric Builder->EmitVTable(RD); 287*0b57cec5SDimitry Andric } 288*0b57cec5SDimitry Andric }; 289*0b57cec5SDimitry Andric } 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric void CodeGenerator::anchor() { } 292*0b57cec5SDimitry Andric 293*0b57cec5SDimitry Andric CodeGenModule &CodeGenerator::CGM() { 294*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this)->CGM(); 295*0b57cec5SDimitry Andric } 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric llvm::Module *CodeGenerator::GetModule() { 298*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this)->GetModule(); 299*0b57cec5SDimitry Andric } 300*0b57cec5SDimitry Andric 301*0b57cec5SDimitry Andric llvm::Module *CodeGenerator::ReleaseModule() { 302*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this)->ReleaseModule(); 303*0b57cec5SDimitry Andric } 304*0b57cec5SDimitry Andric 305*0b57cec5SDimitry Andric CGDebugInfo *CodeGenerator::getCGDebugInfo() { 306*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this)->getCGDebugInfo(); 307*0b57cec5SDimitry Andric } 308*0b57cec5SDimitry Andric 309*0b57cec5SDimitry Andric const Decl *CodeGenerator::GetDeclForMangledName(llvm::StringRef name) { 310*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this)->GetDeclForMangledName(name); 311*0b57cec5SDimitry Andric } 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric llvm::Constant *CodeGenerator::GetAddrOfGlobal(GlobalDecl global, 314*0b57cec5SDimitry Andric bool isForDefinition) { 315*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this) 316*0b57cec5SDimitry Andric ->GetAddrOfGlobal(global, isForDefinition); 317*0b57cec5SDimitry Andric } 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric llvm::Module *CodeGenerator::StartModule(llvm::StringRef ModuleName, 320*0b57cec5SDimitry Andric llvm::LLVMContext &C) { 321*0b57cec5SDimitry Andric return static_cast<CodeGeneratorImpl*>(this)->StartModule(ModuleName, C); 322*0b57cec5SDimitry Andric } 323*0b57cec5SDimitry Andric 324*0b57cec5SDimitry Andric CodeGenerator *clang::CreateLLVMCodeGen( 325*0b57cec5SDimitry Andric DiagnosticsEngine &Diags, llvm::StringRef ModuleName, 326*0b57cec5SDimitry Andric const HeaderSearchOptions &HeaderSearchOpts, 327*0b57cec5SDimitry Andric const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, 328*0b57cec5SDimitry Andric llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) { 329*0b57cec5SDimitry Andric return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts, 330*0b57cec5SDimitry Andric PreprocessorOpts, CGO, C, CoverageInfo); 331*0b57cec5SDimitry Andric } 332