1*67e74705SXin Li //===--- CGDebugInfo.h - DebugInfo for LLVM CodeGen -------------*- C++ -*-===// 2*67e74705SXin Li // 3*67e74705SXin Li // The LLVM Compiler Infrastructure 4*67e74705SXin Li // 5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source 6*67e74705SXin Li // License. See LICENSE.TXT for details. 7*67e74705SXin Li // 8*67e74705SXin Li //===----------------------------------------------------------------------===// 9*67e74705SXin Li // 10*67e74705SXin Li // This is the source-level debug info generator for llvm translation. 11*67e74705SXin Li // 12*67e74705SXin Li //===----------------------------------------------------------------------===// 13*67e74705SXin Li 14*67e74705SXin Li #ifndef LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 15*67e74705SXin Li #define LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 16*67e74705SXin Li 17*67e74705SXin Li #include "CGBuilder.h" 18*67e74705SXin Li #include "clang/AST/Expr.h" 19*67e74705SXin Li #include "clang/AST/ExternalASTSource.h" 20*67e74705SXin Li #include "clang/AST/Type.h" 21*67e74705SXin Li #include "clang/Basic/SourceLocation.h" 22*67e74705SXin Li #include "clang/Frontend/CodeGenOptions.h" 23*67e74705SXin Li #include "llvm/ADT/DenseMap.h" 24*67e74705SXin Li #include "llvm/ADT/Optional.h" 25*67e74705SXin Li #include "llvm/IR/DIBuilder.h" 26*67e74705SXin Li #include "llvm/IR/DebugInfo.h" 27*67e74705SXin Li #include "llvm/IR/ValueHandle.h" 28*67e74705SXin Li #include "llvm/Support/Allocator.h" 29*67e74705SXin Li 30*67e74705SXin Li namespace llvm { 31*67e74705SXin Li class MDNode; 32*67e74705SXin Li } 33*67e74705SXin Li 34*67e74705SXin Li namespace clang { 35*67e74705SXin Li class CXXMethodDecl; 36*67e74705SXin Li class ClassTemplateSpecializationDecl; 37*67e74705SXin Li class GlobalDecl; 38*67e74705SXin Li class ModuleMap; 39*67e74705SXin Li class ObjCInterfaceDecl; 40*67e74705SXin Li class ObjCIvarDecl; 41*67e74705SXin Li class UsingDecl; 42*67e74705SXin Li class VarDecl; 43*67e74705SXin Li 44*67e74705SXin Li namespace CodeGen { 45*67e74705SXin Li class CodeGenModule; 46*67e74705SXin Li class CodeGenFunction; 47*67e74705SXin Li class CGBlockInfo; 48*67e74705SXin Li 49*67e74705SXin Li /// This class gathers all debug information during compilation and is 50*67e74705SXin Li /// responsible for emitting to llvm globals or pass directly to the 51*67e74705SXin Li /// backend. 52*67e74705SXin Li class CGDebugInfo { 53*67e74705SXin Li friend class ApplyDebugLocation; 54*67e74705SXin Li friend class SaveAndRestoreLocation; 55*67e74705SXin Li CodeGenModule &CGM; 56*67e74705SXin Li const codegenoptions::DebugInfoKind DebugKind; 57*67e74705SXin Li bool DebugTypeExtRefs; 58*67e74705SXin Li llvm::DIBuilder DBuilder; 59*67e74705SXin Li llvm::DICompileUnit *TheCU = nullptr; 60*67e74705SXin Li ModuleMap *ClangModuleMap = nullptr; 61*67e74705SXin Li ExternalASTSource::ASTSourceDescriptor PCHDescriptor; 62*67e74705SXin Li SourceLocation CurLoc; 63*67e74705SXin Li llvm::DIType *VTablePtrType = nullptr; 64*67e74705SXin Li llvm::DIType *ClassTy = nullptr; 65*67e74705SXin Li llvm::DICompositeType *ObjTy = nullptr; 66*67e74705SXin Li llvm::DIType *SelTy = nullptr; 67*67e74705SXin Li #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 68*67e74705SXin Li llvm::DIType *SingletonId = nullptr; 69*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def" 70*67e74705SXin Li llvm::DIType *OCLEventDITy = nullptr; 71*67e74705SXin Li llvm::DIType *OCLClkEventDITy = nullptr; 72*67e74705SXin Li llvm::DIType *OCLQueueDITy = nullptr; 73*67e74705SXin Li llvm::DIType *OCLNDRangeDITy = nullptr; 74*67e74705SXin Li llvm::DIType *OCLReserveIDDITy = nullptr; 75*67e74705SXin Li 76*67e74705SXin Li /// Cache of previously constructed Types. 77*67e74705SXin Li llvm::DenseMap<const void *, llvm::TrackingMDRef> TypeCache; 78*67e74705SXin Li 79*67e74705SXin Li llvm::SmallDenseMap<llvm::StringRef, llvm::StringRef> DebugPrefixMap; 80*67e74705SXin Li 81*67e74705SXin Li struct ObjCInterfaceCacheEntry { 82*67e74705SXin Li const ObjCInterfaceType *Type; 83*67e74705SXin Li llvm::DIType *Decl; 84*67e74705SXin Li llvm::DIFile *Unit; ObjCInterfaceCacheEntryObjCInterfaceCacheEntry85*67e74705SXin Li ObjCInterfaceCacheEntry(const ObjCInterfaceType *Type, llvm::DIType *Decl, 86*67e74705SXin Li llvm::DIFile *Unit) 87*67e74705SXin Li : Type(Type), Decl(Decl), Unit(Unit) {} 88*67e74705SXin Li }; 89*67e74705SXin Li 90*67e74705SXin Li /// Cache of previously constructed interfaces which may change. 91*67e74705SXin Li llvm::SmallVector<ObjCInterfaceCacheEntry, 32> ObjCInterfaceCache; 92*67e74705SXin Li 93*67e74705SXin Li /// Cache of references to clang modules and precompiled headers. 94*67e74705SXin Li llvm::DenseMap<const Module *, llvm::TrackingMDRef> ModuleCache; 95*67e74705SXin Li 96*67e74705SXin Li /// List of interfaces we want to keep even if orphaned. 97*67e74705SXin Li std::vector<void *> RetainedTypes; 98*67e74705SXin Li 99*67e74705SXin Li /// Cache of forward declared types to RAUW at the end of 100*67e74705SXin Li /// compilation. 101*67e74705SXin Li std::vector<std::pair<const TagType *, llvm::TrackingMDRef>> ReplaceMap; 102*67e74705SXin Li 103*67e74705SXin Li /// Cache of replaceable forward declarations (functions and 104*67e74705SXin Li /// variables) to RAUW at the end of compilation. 105*67e74705SXin Li std::vector<std::pair<const DeclaratorDecl *, llvm::TrackingMDRef>> 106*67e74705SXin Li FwdDeclReplaceMap; 107*67e74705SXin Li 108*67e74705SXin Li /// Keep track of our current nested lexical block. 109*67e74705SXin Li std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack; 110*67e74705SXin Li llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap; 111*67e74705SXin Li /// Keep track of LexicalBlockStack counter at the beginning of a 112*67e74705SXin Li /// function. This is used to pop unbalanced regions at the end of a 113*67e74705SXin Li /// function. 114*67e74705SXin Li std::vector<unsigned> FnBeginRegionCount; 115*67e74705SXin Li 116*67e74705SXin Li /// This is a storage for names that are constructed on demand. For 117*67e74705SXin Li /// example, C++ destructors, C++ operators etc.. 118*67e74705SXin Li llvm::BumpPtrAllocator DebugInfoNames; 119*67e74705SXin Li StringRef CWDName; 120*67e74705SXin Li 121*67e74705SXin Li llvm::DenseMap<const char *, llvm::TrackingMDRef> DIFileCache; 122*67e74705SXin Li llvm::DenseMap<const FunctionDecl *, llvm::TrackingMDRef> SPCache; 123*67e74705SXin Li /// Cache declarations relevant to DW_TAG_imported_declarations (C++ 124*67e74705SXin Li /// using declarations) that aren't covered by other more specific caches. 125*67e74705SXin Li llvm::DenseMap<const Decl *, llvm::TrackingMDRef> DeclCache; 126*67e74705SXin Li llvm::DenseMap<const NamespaceDecl *, llvm::TrackingMDRef> NameSpaceCache; 127*67e74705SXin Li llvm::DenseMap<const NamespaceAliasDecl *, llvm::TrackingMDRef> 128*67e74705SXin Li NamespaceAliasCache; 129*67e74705SXin Li llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIDerivedType>> 130*67e74705SXin Li StaticDataMemberCache; 131*67e74705SXin Li 132*67e74705SXin Li /// Helper functions for getOrCreateType. 133*67e74705SXin Li /// @{ 134*67e74705SXin Li /// Currently the checksum of an interface includes the number of 135*67e74705SXin Li /// ivars and property accessors. 136*67e74705SXin Li llvm::DIType *CreateType(const BuiltinType *Ty); 137*67e74705SXin Li llvm::DIType *CreateType(const ComplexType *Ty); 138*67e74705SXin Li llvm::DIType *CreateQualifiedType(QualType Ty, llvm::DIFile *Fg); 139*67e74705SXin Li llvm::DIType *CreateType(const TypedefType *Ty, llvm::DIFile *Fg); 140*67e74705SXin Li llvm::DIType *CreateType(const TemplateSpecializationType *Ty, 141*67e74705SXin Li llvm::DIFile *Fg); 142*67e74705SXin Li llvm::DIType *CreateType(const ObjCObjectPointerType *Ty, llvm::DIFile *F); 143*67e74705SXin Li llvm::DIType *CreateType(const PointerType *Ty, llvm::DIFile *F); 144*67e74705SXin Li llvm::DIType *CreateType(const BlockPointerType *Ty, llvm::DIFile *F); 145*67e74705SXin Li llvm::DIType *CreateType(const FunctionType *Ty, llvm::DIFile *F); 146*67e74705SXin Li /// Get structure or union type. 147*67e74705SXin Li llvm::DIType *CreateType(const RecordType *Tyg); 148*67e74705SXin Li llvm::DIType *CreateTypeDefinition(const RecordType *Ty); 149*67e74705SXin Li llvm::DICompositeType *CreateLimitedType(const RecordType *Ty); 150*67e74705SXin Li void CollectContainingType(const CXXRecordDecl *RD, 151*67e74705SXin Li llvm::DICompositeType *CT); 152*67e74705SXin Li /// Get Objective-C interface type. 153*67e74705SXin Li llvm::DIType *CreateType(const ObjCInterfaceType *Ty, llvm::DIFile *F); 154*67e74705SXin Li llvm::DIType *CreateTypeDefinition(const ObjCInterfaceType *Ty, 155*67e74705SXin Li llvm::DIFile *F); 156*67e74705SXin Li /// Get Objective-C object type. 157*67e74705SXin Li llvm::DIType *CreateType(const ObjCObjectType *Ty, llvm::DIFile *F); 158*67e74705SXin Li llvm::DIType *CreateType(const VectorType *Ty, llvm::DIFile *F); 159*67e74705SXin Li llvm::DIType *CreateType(const ArrayType *Ty, llvm::DIFile *F); 160*67e74705SXin Li llvm::DIType *CreateType(const LValueReferenceType *Ty, llvm::DIFile *F); 161*67e74705SXin Li llvm::DIType *CreateType(const RValueReferenceType *Ty, llvm::DIFile *Unit); 162*67e74705SXin Li llvm::DIType *CreateType(const MemberPointerType *Ty, llvm::DIFile *F); 163*67e74705SXin Li llvm::DIType *CreateType(const AtomicType *Ty, llvm::DIFile *F); 164*67e74705SXin Li llvm::DIType *CreateType(const PipeType *Ty, llvm::DIFile *F); 165*67e74705SXin Li /// Get enumeration type. 166*67e74705SXin Li llvm::DIType *CreateEnumType(const EnumType *Ty); 167*67e74705SXin Li llvm::DIType *CreateTypeDefinition(const EnumType *Ty); 168*67e74705SXin Li /// Look up the completed type for a self pointer in the TypeCache and 169*67e74705SXin Li /// create a copy of it with the ObjectPointer and Artificial flags 170*67e74705SXin Li /// set. If the type is not cached, a new one is created. This should 171*67e74705SXin Li /// never happen though, since creating a type for the implicit self 172*67e74705SXin Li /// argument implies that we already parsed the interface definition 173*67e74705SXin Li /// and the ivar declarations in the implementation. 174*67e74705SXin Li llvm::DIType *CreateSelfType(const QualType &QualTy, llvm::DIType *Ty); 175*67e74705SXin Li /// @} 176*67e74705SXin Li 177*67e74705SXin Li /// Get the type from the cache or return null type if it doesn't 178*67e74705SXin Li /// exist. 179*67e74705SXin Li llvm::DIType *getTypeOrNull(const QualType); 180*67e74705SXin Li /// Return the debug type for a C++ method. 181*67e74705SXin Li /// \arg CXXMethodDecl is of FunctionType. This function type is 182*67e74705SXin Li /// not updated to include implicit \c this pointer. Use this routine 183*67e74705SXin Li /// to get a method type which includes \c this pointer. 184*67e74705SXin Li llvm::DISubroutineType *getOrCreateMethodType(const CXXMethodDecl *Method, 185*67e74705SXin Li llvm::DIFile *F); 186*67e74705SXin Li llvm::DISubroutineType * 187*67e74705SXin Li getOrCreateInstanceMethodType(QualType ThisPtr, const FunctionProtoType *Func, 188*67e74705SXin Li llvm::DIFile *Unit); 189*67e74705SXin Li llvm::DISubroutineType * 190*67e74705SXin Li getOrCreateFunctionType(const Decl *D, QualType FnType, llvm::DIFile *F); 191*67e74705SXin Li /// \return debug info descriptor for vtable. 192*67e74705SXin Li llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F); 193*67e74705SXin Li /// \return namespace descriptor for the given namespace decl. 194*67e74705SXin Li llvm::DINamespace *getOrCreateNameSpace(const NamespaceDecl *N); 195*67e74705SXin Li llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty, 196*67e74705SXin Li QualType PointeeTy, llvm::DIFile *F); 197*67e74705SXin Li llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache); 198*67e74705SXin Li 199*67e74705SXin Li /// A helper function to create a subprogram for a single member 200*67e74705SXin Li /// function GlobalDecl. 201*67e74705SXin Li llvm::DISubprogram *CreateCXXMemberFunction(const CXXMethodDecl *Method, 202*67e74705SXin Li llvm::DIFile *F, 203*67e74705SXin Li llvm::DIType *RecordTy); 204*67e74705SXin Li 205*67e74705SXin Li /// A helper function to collect debug info for C++ member 206*67e74705SXin Li /// functions. This is used while creating debug info entry for a 207*67e74705SXin Li /// Record. 208*67e74705SXin Li void CollectCXXMemberFunctions(const CXXRecordDecl *Decl, llvm::DIFile *F, 209*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &E, 210*67e74705SXin Li llvm::DIType *T); 211*67e74705SXin Li 212*67e74705SXin Li /// A helper function to collect debug info for C++ base 213*67e74705SXin Li /// classes. This is used while creating debug info entry for a 214*67e74705SXin Li /// Record. 215*67e74705SXin Li void CollectCXXBases(const CXXRecordDecl *Decl, llvm::DIFile *F, 216*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &EltTys, 217*67e74705SXin Li llvm::DIType *RecordTy); 218*67e74705SXin Li 219*67e74705SXin Li /// A helper function to collect template parameters. 220*67e74705SXin Li llvm::DINodeArray CollectTemplateParams(const TemplateParameterList *TPList, 221*67e74705SXin Li ArrayRef<TemplateArgument> TAList, 222*67e74705SXin Li llvm::DIFile *Unit); 223*67e74705SXin Li /// A helper function to collect debug info for function template 224*67e74705SXin Li /// parameters. 225*67e74705SXin Li llvm::DINodeArray CollectFunctionTemplateParams(const FunctionDecl *FD, 226*67e74705SXin Li llvm::DIFile *Unit); 227*67e74705SXin Li 228*67e74705SXin Li /// A helper function to collect debug info for template 229*67e74705SXin Li /// parameters. 230*67e74705SXin Li llvm::DINodeArray 231*67e74705SXin Li CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TS, 232*67e74705SXin Li llvm::DIFile *F); 233*67e74705SXin Li 234*67e74705SXin Li llvm::DIType *createFieldType(StringRef name, QualType type, 235*67e74705SXin Li SourceLocation loc, AccessSpecifier AS, 236*67e74705SXin Li uint64_t offsetInBits, llvm::DIFile *tunit, 237*67e74705SXin Li llvm::DIScope *scope, 238*67e74705SXin Li const RecordDecl *RD = nullptr); 239*67e74705SXin Li 240*67e74705SXin Li /// Create new bit field member. 241*67e74705SXin Li llvm::DIType *createBitFieldType(const FieldDecl *BitFieldDecl, 242*67e74705SXin Li llvm::DIScope *RecordTy, 243*67e74705SXin Li const RecordDecl *RD); 244*67e74705SXin Li 245*67e74705SXin Li /// Helpers for collecting fields of a record. 246*67e74705SXin Li /// @{ 247*67e74705SXin Li void CollectRecordLambdaFields(const CXXRecordDecl *CXXDecl, 248*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &E, 249*67e74705SXin Li llvm::DIType *RecordTy); 250*67e74705SXin Li llvm::DIDerivedType *CreateRecordStaticField(const VarDecl *Var, 251*67e74705SXin Li llvm::DIType *RecordTy, 252*67e74705SXin Li const RecordDecl *RD); 253*67e74705SXin Li void CollectRecordNormalField(const FieldDecl *Field, uint64_t OffsetInBits, 254*67e74705SXin Li llvm::DIFile *F, 255*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &E, 256*67e74705SXin Li llvm::DIType *RecordTy, const RecordDecl *RD); 257*67e74705SXin Li void CollectRecordFields(const RecordDecl *Decl, llvm::DIFile *F, 258*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &E, 259*67e74705SXin Li llvm::DICompositeType *RecordTy); 260*67e74705SXin Li 261*67e74705SXin Li /// If the C++ class has vtable info then insert appropriate debug 262*67e74705SXin Li /// info entry in EltTys vector. 263*67e74705SXin Li void CollectVTableInfo(const CXXRecordDecl *Decl, llvm::DIFile *F, 264*67e74705SXin Li SmallVectorImpl<llvm::Metadata *> &EltTys); 265*67e74705SXin Li /// @} 266*67e74705SXin Li 267*67e74705SXin Li /// Create a new lexical block node and push it on the stack. 268*67e74705SXin Li void CreateLexicalBlock(SourceLocation Loc); 269*67e74705SXin Li 270*67e74705SXin Li public: 271*67e74705SXin Li CGDebugInfo(CodeGenModule &CGM); 272*67e74705SXin Li ~CGDebugInfo(); 273*67e74705SXin Li 274*67e74705SXin Li void finalize(); 275*67e74705SXin Li 276*67e74705SXin Li /// Module debugging: Support for building PCMs. 277*67e74705SXin Li /// @{ 278*67e74705SXin Li /// Set the main CU's DwoId field to \p Signature. 279*67e74705SXin Li void setDwoId(uint64_t Signature); 280*67e74705SXin Li 281*67e74705SXin Li /// When generating debug information for a clang module or 282*67e74705SXin Li /// precompiled header, this module map will be used to determine 283*67e74705SXin Li /// the module of origin of each Decl. setModuleMap(ModuleMap & MMap)284*67e74705SXin Li void setModuleMap(ModuleMap &MMap) { ClangModuleMap = &MMap; } 285*67e74705SXin Li 286*67e74705SXin Li /// When generating debug information for a clang module or 287*67e74705SXin Li /// precompiled header, this module map will be used to determine 288*67e74705SXin Li /// the module of origin of each Decl. setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH)289*67e74705SXin Li void setPCHDescriptor(ExternalASTSource::ASTSourceDescriptor PCH) { 290*67e74705SXin Li PCHDescriptor = PCH; 291*67e74705SXin Li } 292*67e74705SXin Li /// @} 293*67e74705SXin Li 294*67e74705SXin Li /// Update the current source location. If \arg loc is invalid it is 295*67e74705SXin Li /// ignored. 296*67e74705SXin Li void setLocation(SourceLocation Loc); 297*67e74705SXin Li 298*67e74705SXin Li /// Emit metadata to indicate a change in line/column information in 299*67e74705SXin Li /// the source file. If the location is invalid, the previous 300*67e74705SXin Li /// location will be reused. 301*67e74705SXin Li void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc); 302*67e74705SXin Li 303*67e74705SXin Li /// Emit a call to llvm.dbg.function.start to indicate 304*67e74705SXin Li /// start of a new function. 305*67e74705SXin Li /// \param Loc The location of the function header. 306*67e74705SXin Li /// \param ScopeLoc The location of the function body. 307*67e74705SXin Li void EmitFunctionStart(GlobalDecl GD, SourceLocation Loc, 308*67e74705SXin Li SourceLocation ScopeLoc, QualType FnType, 309*67e74705SXin Li llvm::Function *Fn, CGBuilderTy &Builder); 310*67e74705SXin Li 311*67e74705SXin Li /// Emit debug info for a function declaration. 312*67e74705SXin Li void EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, QualType FnType); 313*67e74705SXin Li 314*67e74705SXin Li /// Constructs the debug code for exiting a function. 315*67e74705SXin Li void EmitFunctionEnd(CGBuilderTy &Builder); 316*67e74705SXin Li 317*67e74705SXin Li /// Emit metadata to indicate the beginning of a new lexical block 318*67e74705SXin Li /// and push the block onto the stack. 319*67e74705SXin Li void EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc); 320*67e74705SXin Li 321*67e74705SXin Li /// Emit metadata to indicate the end of a new lexical block and pop 322*67e74705SXin Li /// the current block. 323*67e74705SXin Li void EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc); 324*67e74705SXin Li 325*67e74705SXin Li /// Emit call to \c llvm.dbg.declare for an automatic variable 326*67e74705SXin Li /// declaration. 327*67e74705SXin Li void EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, 328*67e74705SXin Li CGBuilderTy &Builder); 329*67e74705SXin Li 330*67e74705SXin Li /// Emit call to \c llvm.dbg.declare for an imported variable 331*67e74705SXin Li /// declaration in a block. 332*67e74705SXin Li void EmitDeclareOfBlockDeclRefVariable(const VarDecl *variable, 333*67e74705SXin Li llvm::Value *storage, 334*67e74705SXin Li CGBuilderTy &Builder, 335*67e74705SXin Li const CGBlockInfo &blockInfo, 336*67e74705SXin Li llvm::Instruction *InsertPoint = nullptr); 337*67e74705SXin Li 338*67e74705SXin Li /// Emit call to \c llvm.dbg.declare for an argument variable 339*67e74705SXin Li /// declaration. 340*67e74705SXin Li void EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 341*67e74705SXin Li unsigned ArgNo, CGBuilderTy &Builder); 342*67e74705SXin Li 343*67e74705SXin Li /// Emit call to \c llvm.dbg.declare for the block-literal argument 344*67e74705SXin Li /// to a block invocation function. 345*67e74705SXin Li void EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, 346*67e74705SXin Li llvm::Value *Arg, unsigned ArgNo, 347*67e74705SXin Li llvm::Value *LocalAddr, 348*67e74705SXin Li CGBuilderTy &Builder); 349*67e74705SXin Li 350*67e74705SXin Li /// Emit information about a global variable. 351*67e74705SXin Li void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl); 352*67e74705SXin Li 353*67e74705SXin Li /// Emit global variable's debug info. 354*67e74705SXin Li void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init); 355*67e74705SXin Li 356*67e74705SXin Li /// Emit C++ using directive. 357*67e74705SXin Li void EmitUsingDirective(const UsingDirectiveDecl &UD); 358*67e74705SXin Li 359*67e74705SXin Li /// Emit the type explicitly casted to. 360*67e74705SXin Li void EmitExplicitCastType(QualType Ty); 361*67e74705SXin Li 362*67e74705SXin Li /// Emit C++ using declaration. 363*67e74705SXin Li void EmitUsingDecl(const UsingDecl &UD); 364*67e74705SXin Li 365*67e74705SXin Li /// Emit an @import declaration. 366*67e74705SXin Li void EmitImportDecl(const ImportDecl &ID); 367*67e74705SXin Li 368*67e74705SXin Li /// Emit C++ namespace alias. 369*67e74705SXin Li llvm::DIImportedEntity *EmitNamespaceAlias(const NamespaceAliasDecl &NA); 370*67e74705SXin Li 371*67e74705SXin Li /// Emit record type's standalone debug info. 372*67e74705SXin Li llvm::DIType *getOrCreateRecordType(QualType Ty, SourceLocation L); 373*67e74705SXin Li 374*67e74705SXin Li /// Emit an Objective-C interface type standalone debug info. 375*67e74705SXin Li llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc); 376*67e74705SXin Li 377*67e74705SXin Li /// Emit standalone debug info for a type. 378*67e74705SXin Li llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc); 379*67e74705SXin Li 380*67e74705SXin Li void completeType(const EnumDecl *ED); 381*67e74705SXin Li void completeType(const RecordDecl *RD); 382*67e74705SXin Li void completeRequiredType(const RecordDecl *RD); 383*67e74705SXin Li void completeClassData(const RecordDecl *RD); 384*67e74705SXin Li 385*67e74705SXin Li void completeTemplateDefinition(const ClassTemplateSpecializationDecl &SD); 386*67e74705SXin Li 387*67e74705SXin Li private: 388*67e74705SXin Li /// Emit call to llvm.dbg.declare for a variable declaration. 389*67e74705SXin Li void EmitDeclare(const VarDecl *decl, llvm::Value *AI, 390*67e74705SXin Li llvm::Optional<unsigned> ArgNo, CGBuilderTy &Builder); 391*67e74705SXin Li 392*67e74705SXin Li /// Build up structure info for the byref. See \a BuildByRefType. 393*67e74705SXin Li llvm::DIType *EmitTypeForVarWithBlocksAttr(const VarDecl *VD, 394*67e74705SXin Li uint64_t *OffSet); 395*67e74705SXin Li 396*67e74705SXin Li /// Get context info for the DeclContext of \p Decl. 397*67e74705SXin Li llvm::DIScope *getDeclContextDescriptor(const Decl *D); 398*67e74705SXin Li /// Get context info for a given DeclContext \p Decl. 399*67e74705SXin Li llvm::DIScope *getContextDescriptor(const Decl *Context, 400*67e74705SXin Li llvm::DIScope *Default); 401*67e74705SXin Li 402*67e74705SXin Li llvm::DIScope *getCurrentContextDescriptor(const Decl *Decl); 403*67e74705SXin Li 404*67e74705SXin Li /// Create a forward decl for a RecordType in a given context. 405*67e74705SXin Li llvm::DICompositeType *getOrCreateRecordFwdDecl(const RecordType *, 406*67e74705SXin Li llvm::DIScope *); 407*67e74705SXin Li 408*67e74705SXin Li /// Return current directory name. 409*67e74705SXin Li StringRef getCurrentDirname(); 410*67e74705SXin Li 411*67e74705SXin Li /// Create new compile unit. 412*67e74705SXin Li void CreateCompileUnit(); 413*67e74705SXin Li 414*67e74705SXin Li /// Remap a given path with the current debug prefix map 415*67e74705SXin Li std::string remapDIPath(StringRef) const; 416*67e74705SXin Li 417*67e74705SXin Li /// Get the file debug info descriptor for the input location. 418*67e74705SXin Li llvm::DIFile *getOrCreateFile(SourceLocation Loc); 419*67e74705SXin Li 420*67e74705SXin Li /// Get the file info for main compile unit. 421*67e74705SXin Li llvm::DIFile *getOrCreateMainFile(); 422*67e74705SXin Li 423*67e74705SXin Li /// Get the type from the cache or create a new type if necessary. 424*67e74705SXin Li llvm::DIType *getOrCreateType(QualType Ty, llvm::DIFile *Fg); 425*67e74705SXin Li 426*67e74705SXin Li /// Get a reference to a clang module. If \p CreateSkeletonCU is true, 427*67e74705SXin Li /// this also creates a split dwarf skeleton compile unit. 428*67e74705SXin Li llvm::DIModule * 429*67e74705SXin Li getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod, 430*67e74705SXin Li bool CreateSkeletonCU); 431*67e74705SXin Li 432*67e74705SXin Li /// DebugTypeExtRefs: If \p D originated in a clang module, return it. 433*67e74705SXin Li llvm::DIModule *getParentModuleOrNull(const Decl *D); 434*67e74705SXin Li 435*67e74705SXin Li /// Get the type from the cache or create a new partial type if 436*67e74705SXin Li /// necessary. 437*67e74705SXin Li llvm::DICompositeType *getOrCreateLimitedType(const RecordType *Ty, 438*67e74705SXin Li llvm::DIFile *F); 439*67e74705SXin Li 440*67e74705SXin Li /// Create type metadata for a source language type. 441*67e74705SXin Li llvm::DIType *CreateTypeNode(QualType Ty, llvm::DIFile *Fg); 442*67e74705SXin Li 443*67e74705SXin Li /// Create new member and increase Offset by FType's size. 444*67e74705SXin Li llvm::DIType *CreateMemberType(llvm::DIFile *Unit, QualType FType, 445*67e74705SXin Li StringRef Name, uint64_t *Offset); 446*67e74705SXin Li 447*67e74705SXin Li /// Retrieve the DIDescriptor, if any, for the canonical form of this 448*67e74705SXin Li /// declaration. 449*67e74705SXin Li llvm::DINode *getDeclarationOrDefinition(const Decl *D); 450*67e74705SXin Li 451*67e74705SXin Li /// \return debug info descriptor to describe method 452*67e74705SXin Li /// declaration for the given method definition. 453*67e74705SXin Li llvm::DISubprogram *getFunctionDeclaration(const Decl *D); 454*67e74705SXin Li 455*67e74705SXin Li /// \return debug info descriptor to describe in-class static data 456*67e74705SXin Li /// member declaration for the given out-of-class definition. If D 457*67e74705SXin Li /// is an out-of-class definition of a static data member of a 458*67e74705SXin Li /// class, find its corresponding in-class declaration. 459*67e74705SXin Li llvm::DIDerivedType * 460*67e74705SXin Li getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D); 461*67e74705SXin Li 462*67e74705SXin Li /// Create a subprogram describing the forward declaration 463*67e74705SXin Li /// represented in the given FunctionDecl. 464*67e74705SXin Li llvm::DISubprogram *getFunctionForwardDeclaration(const FunctionDecl *FD); 465*67e74705SXin Li 466*67e74705SXin Li /// Create a global variable describing the forward decalration 467*67e74705SXin Li /// represented in the given VarDecl. 468*67e74705SXin Li llvm::DIGlobalVariable * 469*67e74705SXin Li getGlobalVariableForwardDeclaration(const VarDecl *VD); 470*67e74705SXin Li 471*67e74705SXin Li /// \brief Return a global variable that represents one of the 472*67e74705SXin Li /// collection of global variables created for an anonmyous union. 473*67e74705SXin Li /// 474*67e74705SXin Li /// Recursively collect all of the member fields of a global 475*67e74705SXin Li /// anonymous decl and create static variables for them. The first 476*67e74705SXin Li /// time this is called it needs to be on a union and then from 477*67e74705SXin Li /// there we can have additional unnamed fields. 478*67e74705SXin Li llvm::DIGlobalVariable * 479*67e74705SXin Li CollectAnonRecordDecls(const RecordDecl *RD, llvm::DIFile *Unit, 480*67e74705SXin Li unsigned LineNo, StringRef LinkageName, 481*67e74705SXin Li llvm::GlobalVariable *Var, llvm::DIScope *DContext); 482*67e74705SXin Li 483*67e74705SXin Li /// Get function name for the given FunctionDecl. If the name is 484*67e74705SXin Li /// constructed on demand (e.g., C++ destructor) then the name is 485*67e74705SXin Li /// stored on the side. 486*67e74705SXin Li StringRef getFunctionName(const FunctionDecl *FD); 487*67e74705SXin Li 488*67e74705SXin Li /// Returns the unmangled name of an Objective-C method. 489*67e74705SXin Li /// This is the display name for the debugging info. 490*67e74705SXin Li StringRef getObjCMethodName(const ObjCMethodDecl *FD); 491*67e74705SXin Li 492*67e74705SXin Li /// Return selector name. This is used for debugging 493*67e74705SXin Li /// info. 494*67e74705SXin Li StringRef getSelectorName(Selector S); 495*67e74705SXin Li 496*67e74705SXin Li /// Get class name including template argument list. 497*67e74705SXin Li StringRef getClassName(const RecordDecl *RD); 498*67e74705SXin Li 499*67e74705SXin Li /// Get the vtable name for the given class. 500*67e74705SXin Li StringRef getVTableName(const CXXRecordDecl *Decl); 501*67e74705SXin Li 502*67e74705SXin Li /// Get line number for the location. If location is invalid 503*67e74705SXin Li /// then use current location. 504*67e74705SXin Li unsigned getLineNumber(SourceLocation Loc); 505*67e74705SXin Li 506*67e74705SXin Li /// Get column number for the location. If location is 507*67e74705SXin Li /// invalid then use current location. 508*67e74705SXin Li /// \param Force Assume DebugColumnInfo option is true. 509*67e74705SXin Li unsigned getColumnNumber(SourceLocation Loc, bool Force = false); 510*67e74705SXin Li 511*67e74705SXin Li /// Collect various properties of a FunctionDecl. 512*67e74705SXin Li /// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl. 513*67e74705SXin Li void collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, 514*67e74705SXin Li StringRef &Name, StringRef &LinkageName, 515*67e74705SXin Li llvm::DIScope *&FDContext, 516*67e74705SXin Li llvm::DINodeArray &TParamsArray, 517*67e74705SXin Li unsigned &Flags); 518*67e74705SXin Li 519*67e74705SXin Li /// Collect various properties of a VarDecl. 520*67e74705SXin Li void collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, 521*67e74705SXin Li unsigned &LineNo, QualType &T, StringRef &Name, 522*67e74705SXin Li StringRef &LinkageName, llvm::DIScope *&VDContext); 523*67e74705SXin Li 524*67e74705SXin Li /// Allocate a copy of \p A using the DebugInfoNames allocator 525*67e74705SXin Li /// and return a reference to it. If multiple arguments are given the strings 526*67e74705SXin Li /// are concatenated. 527*67e74705SXin Li StringRef internString(StringRef A, StringRef B = StringRef()) { 528*67e74705SXin Li char *Data = DebugInfoNames.Allocate<char>(A.size() + B.size()); 529*67e74705SXin Li if (!A.empty()) 530*67e74705SXin Li std::memcpy(Data, A.data(), A.size()); 531*67e74705SXin Li if (!B.empty()) 532*67e74705SXin Li std::memcpy(Data + A.size(), B.data(), B.size()); 533*67e74705SXin Li return StringRef(Data, A.size() + B.size()); 534*67e74705SXin Li } 535*67e74705SXin Li }; 536*67e74705SXin Li 537*67e74705SXin Li /// A scoped helper to set the current debug location to the specified 538*67e74705SXin Li /// location or preferred location of the specified Expr. 539*67e74705SXin Li class ApplyDebugLocation { 540*67e74705SXin Li private: 541*67e74705SXin Li void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false); 542*67e74705SXin Li ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty, 543*67e74705SXin Li SourceLocation TemporaryLocation); 544*67e74705SXin Li 545*67e74705SXin Li llvm::DebugLoc OriginalLocation; 546*67e74705SXin Li CodeGenFunction *CGF; 547*67e74705SXin Li 548*67e74705SXin Li public: 549*67e74705SXin Li /// Set the location to the (valid) TemporaryLocation. 550*67e74705SXin Li ApplyDebugLocation(CodeGenFunction &CGF, SourceLocation TemporaryLocation); 551*67e74705SXin Li ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E); 552*67e74705SXin Li ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc); ApplyDebugLocation(ApplyDebugLocation && Other)553*67e74705SXin Li ApplyDebugLocation(ApplyDebugLocation &&Other) : CGF(Other.CGF) { 554*67e74705SXin Li Other.CGF = nullptr; 555*67e74705SXin Li } 556*67e74705SXin Li 557*67e74705SXin Li ~ApplyDebugLocation(); 558*67e74705SXin Li 559*67e74705SXin Li /// \brief Apply TemporaryLocation if it is valid. Otherwise switch 560*67e74705SXin Li /// to an artificial debug location that has a valid scope, but no 561*67e74705SXin Li /// line information. 562*67e74705SXin Li /// 563*67e74705SXin Li /// Artificial locations are useful when emitting compiler-generated 564*67e74705SXin Li /// helper functions that have no source location associated with 565*67e74705SXin Li /// them. The DWARF specification allows the compiler to use the 566*67e74705SXin Li /// special line number 0 to indicate code that can not be 567*67e74705SXin Li /// attributed to any source location. Note that passing an empty 568*67e74705SXin Li /// SourceLocation to CGDebugInfo::setLocation() will result in the 569*67e74705SXin Li /// last valid location being reused. CreateArtificial(CodeGenFunction & CGF)570*67e74705SXin Li static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) { 571*67e74705SXin Li return ApplyDebugLocation(CGF, false, SourceLocation()); 572*67e74705SXin Li } 573*67e74705SXin Li /// \brief Apply TemporaryLocation if it is valid. Otherwise switch 574*67e74705SXin Li /// to an artificial debug location that has a valid scope, but no 575*67e74705SXin Li /// line information. 576*67e74705SXin Li static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction & CGF,SourceLocation TemporaryLocation)577*67e74705SXin Li CreateDefaultArtificial(CodeGenFunction &CGF, 578*67e74705SXin Li SourceLocation TemporaryLocation) { 579*67e74705SXin Li return ApplyDebugLocation(CGF, false, TemporaryLocation); 580*67e74705SXin Li } 581*67e74705SXin Li 582*67e74705SXin Li /// Set the IRBuilder to not attach debug locations. Note that 583*67e74705SXin Li /// passing an empty SourceLocation to \a CGDebugInfo::setLocation() 584*67e74705SXin Li /// will result in the last valid location being reused. Note that 585*67e74705SXin Li /// all instructions that do not have a location at the beginning of 586*67e74705SXin Li /// a function are counted towards to function prologue. CreateEmpty(CodeGenFunction & CGF)587*67e74705SXin Li static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) { 588*67e74705SXin Li return ApplyDebugLocation(CGF, true, SourceLocation()); 589*67e74705SXin Li } 590*67e74705SXin Li 591*67e74705SXin Li }; 592*67e74705SXin Li 593*67e74705SXin Li } // namespace CodeGen 594*67e74705SXin Li } // namespace clang 595*67e74705SXin Li 596*67e74705SXin Li #endif // LLVM_CLANG_LIB_CODEGEN_CGDEBUGINFO_H 597