1 //===- DIBuilder.h - Debug Information Builder ------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines a DIBuilder that is useful for creating debugging 10 // information entries in LLVM IR form. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_IR_DIBUILDER_H 15 #define LLVM_IR_DIBUILDER_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/MapVector.h" 20 #include "llvm/ADT/SetVector.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/BinaryFormat/Dwarf.h" 24 #include "llvm/IR/DebugInfoMetadata.h" 25 #include "llvm/IR/TrackingMDRef.h" 26 #include "llvm/Support/Casting.h" 27 #include <algorithm> 28 #include <cstdint> 29 #include <optional> 30 31 namespace llvm { 32 33 class BasicBlock; 34 class Constant; 35 class Function; 36 class Instruction; 37 class LLVMContext; 38 class Module; 39 class Value; 40 class DbgAssignIntrinsic; 41 class DbgRecord; 42 43 using DbgInstPtr = PointerUnion<Instruction *, DbgRecord *>; 44 45 class DIBuilder { 46 Module &M; 47 LLVMContext &VMContext; 48 49 DICompileUnit *CUNode; ///< The one compile unit created by this DIBuiler. 50 Function *DeclareFn; ///< llvm.dbg.declare 51 Function *ValueFn; ///< llvm.dbg.value 52 Function *LabelFn; ///< llvm.dbg.label 53 Function *AssignFn; ///< llvm.dbg.assign 54 55 SmallVector<TrackingMDNodeRef, 4> AllEnumTypes; 56 /// Track the RetainTypes, since they can be updated later on. 57 SmallVector<TrackingMDNodeRef, 4> AllRetainTypes; 58 SmallVector<DISubprogram *, 4> AllSubprograms; 59 SmallVector<Metadata *, 4> AllGVs; 60 SmallVector<TrackingMDNodeRef, 4> ImportedModules; 61 /// Map Macro parent (which can be DIMacroFile or nullptr) to a list of 62 /// Metadata all of type DIMacroNode. 63 /// DIMacroNode's with nullptr parent are DICompileUnit direct children. 64 MapVector<MDNode *, SetVector<Metadata *>> AllMacrosPerParent; 65 66 /// Track nodes that may be unresolved. 67 SmallVector<TrackingMDNodeRef, 4> UnresolvedNodes; 68 bool AllowUnresolvedNodes; 69 70 /// Each subprogram's preserved local variables, labels and imported 71 /// entities. 72 /// 73 /// Do not use a std::vector. Some versions of libc++ apparently copy 74 /// instead of move on grow operations, and TrackingMDRef is expensive to 75 /// copy. 76 DenseMap<DISubprogram *, SmallVector<TrackingMDNodeRef, 4>> 77 SubprogramTrackedNodes; 78 79 SmallVectorImpl<TrackingMDNodeRef> & getImportTrackingVector(const DIScope * S)80 getImportTrackingVector(const DIScope *S) { 81 return isa_and_nonnull<DILocalScope>(S) 82 ? getSubprogramNodesTrackingVector(S) 83 : ImportedModules; 84 } 85 SmallVectorImpl<TrackingMDNodeRef> & getSubprogramNodesTrackingVector(const DIScope * S)86 getSubprogramNodesTrackingVector(const DIScope *S) { 87 return SubprogramTrackedNodes[cast<DILocalScope>(S)->getSubprogram()]; 88 } 89 90 /// Create a temporary. 91 /// 92 /// Create an \a temporary node and track it in \a UnresolvedNodes. 93 void trackIfUnresolved(MDNode *N); 94 95 /// Internal helper for insertDeclare. 96 DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, 97 DIExpression *Expr, const DILocation *DL, 98 BasicBlock *InsertBB, Instruction *InsertBefore); 99 100 /// Internal helper for insertLabel. 101 DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL, 102 BasicBlock *InsertBB, Instruction *InsertBefore); 103 104 /// Internal helper. Track metadata if untracked and insert \p DPV. 105 void insertDPValue(DPValue *DPV, BasicBlock *InsertBB, 106 Instruction *InsertBefore, bool InsertAtHead = false); 107 108 /// Internal helper with common code used by insertDbg{Value,Addr}Intrinsic. 109 Instruction *insertDbgIntrinsic(llvm::Function *Intrinsic, llvm::Value *Val, 110 DILocalVariable *VarInfo, 111 DIExpression *Expr, const DILocation *DL, 112 BasicBlock *InsertBB, 113 Instruction *InsertBefore); 114 115 /// Internal helper for insertDbgValueIntrinsic. 116 DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, 117 DILocalVariable *VarInfo, 118 DIExpression *Expr, const DILocation *DL, 119 BasicBlock *InsertBB, 120 Instruction *InsertBefore); 121 122 public: 123 /// Construct a builder for a module. 124 /// 125 /// If \c AllowUnresolved, collect unresolved nodes attached to the module 126 /// in order to resolve cycles during \a finalize(). 127 /// 128 /// If \p CU is given a value other than nullptr, then set \p CUNode to CU. 129 explicit DIBuilder(Module &M, bool AllowUnresolved = true, 130 DICompileUnit *CU = nullptr); 131 DIBuilder(const DIBuilder &) = delete; 132 DIBuilder &operator=(const DIBuilder &) = delete; 133 134 /// Construct any deferred debug info descriptors. 135 void finalize(); 136 137 /// Finalize a specific subprogram - no new variables may be added to this 138 /// subprogram afterwards. 139 void finalizeSubprogram(DISubprogram *SP); 140 141 /// A CompileUnit provides an anchor for all debugging 142 /// information generated during this instance of compilation. 143 /// \param Lang Source programming language, eg. dwarf::DW_LANG_C99 144 /// \param File File info. 145 /// \param Producer Identify the producer of debugging information 146 /// and code. Usually this is a compiler 147 /// version string. 148 /// \param isOptimized A boolean flag which indicates whether optimization 149 /// is enabled or not. 150 /// \param Flags This string lists command line options. This 151 /// string is directly embedded in debug info 152 /// output which may be used by a tool 153 /// analyzing generated debugging information. 154 /// \param RV This indicates runtime version for languages like 155 /// Objective-C. 156 /// \param SplitName The name of the file that we'll split debug info 157 /// out into. 158 /// \param Kind The kind of debug information to generate. 159 /// \param DWOId The DWOId if this is a split skeleton compile unit. 160 /// \param SplitDebugInlining Whether to emit inline debug info. 161 /// \param DebugInfoForProfiling Whether to emit extra debug info for 162 /// profile collection. 163 /// \param NameTableKind Whether to emit .debug_gnu_pubnames, 164 /// .debug_pubnames, or no pubnames at all. 165 /// \param SysRoot The clang system root (value of -isysroot). 166 /// \param SDK The SDK name. On Darwin, this is the last component 167 /// of the sysroot. 168 DICompileUnit * 169 createCompileUnit(unsigned Lang, DIFile *File, StringRef Producer, 170 bool isOptimized, StringRef Flags, unsigned RV, 171 StringRef SplitName = StringRef(), 172 DICompileUnit::DebugEmissionKind Kind = 173 DICompileUnit::DebugEmissionKind::FullDebug, 174 uint64_t DWOId = 0, bool SplitDebugInlining = true, 175 bool DebugInfoForProfiling = false, 176 DICompileUnit::DebugNameTableKind NameTableKind = 177 DICompileUnit::DebugNameTableKind::Default, 178 bool RangesBaseAddress = false, StringRef SysRoot = {}, 179 StringRef SDK = {}); 180 181 /// Create a file descriptor to hold debugging information for a file. 182 /// \param Filename File name. 183 /// \param Directory Directory. 184 /// \param Checksum Optional checksum kind (e.g. CSK_MD5, CSK_SHA1, etc.) 185 /// and value. 186 /// \param Source Optional source text. 187 DIFile *createFile( 188 StringRef Filename, StringRef Directory, 189 std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = std::nullopt, 190 std::optional<StringRef> Source = std::nullopt); 191 192 /// Create debugging information entry for a macro. 193 /// \param Parent Macro parent (could be nullptr). 194 /// \param Line Source line number where the macro is defined. 195 /// \param MacroType DW_MACINFO_define or DW_MACINFO_undef. 196 /// \param Name Macro name. 197 /// \param Value Macro value. 198 DIMacro *createMacro(DIMacroFile *Parent, unsigned Line, unsigned MacroType, 199 StringRef Name, StringRef Value = StringRef()); 200 201 /// Create debugging information temporary entry for a macro file. 202 /// List of macro node direct children will be calculated by DIBuilder, 203 /// using the \p Parent relationship. 204 /// \param Parent Macro file parent (could be nullptr). 205 /// \param Line Source line number where the macro file is included. 206 /// \param File File descriptor containing the name of the macro file. 207 DIMacroFile *createTempMacroFile(DIMacroFile *Parent, unsigned Line, 208 DIFile *File); 209 210 /// Create a single enumerator value. 211 DIEnumerator *createEnumerator(StringRef Name, const APSInt &Value); 212 DIEnumerator *createEnumerator(StringRef Name, uint64_t Val, 213 bool IsUnsigned = false); 214 215 /// Create a DWARF unspecified type. 216 DIBasicType *createUnspecifiedType(StringRef Name); 217 218 /// Create C++11 nullptr type. 219 DIBasicType *createNullPtrType(); 220 221 /// Create debugging information entry for a basic 222 /// type. 223 /// \param Name Type name. 224 /// \param SizeInBits Size of the type. 225 /// \param Encoding DWARF encoding code, e.g., dwarf::DW_ATE_float. 226 /// \param Flags Optional DWARF attributes, e.g., DW_AT_endianity. 227 DIBasicType *createBasicType(StringRef Name, uint64_t SizeInBits, 228 unsigned Encoding, 229 DINode::DIFlags Flags = DINode::FlagZero); 230 231 /// Create debugging information entry for a string 232 /// type. 233 /// \param Name Type name. 234 /// \param SizeInBits Size of the type. 235 DIStringType *createStringType(StringRef Name, uint64_t SizeInBits); 236 237 /// Create debugging information entry for Fortran 238 /// assumed length string type. 239 /// \param Name Type name. 240 /// \param StringLength String length expressed as DIVariable *. 241 /// \param StrLocationExp Optional memory location of the string. 242 DIStringType *createStringType(StringRef Name, DIVariable *StringLength, 243 DIExpression *StrLocationExp = nullptr); 244 245 /// Create debugging information entry for Fortran 246 /// assumed length string type. 247 /// \param Name Type name. 248 /// \param StringLengthExp String length expressed in DIExpression form. 249 /// \param StrLocationExp Optional memory location of the string. 250 DIStringType *createStringType(StringRef Name, 251 DIExpression *StringLengthExp, 252 DIExpression *StrLocationExp = nullptr); 253 254 /// Create debugging information entry for a qualified 255 /// type, e.g. 'const int'. 256 /// \param Tag Tag identifing type, e.g. dwarf::TAG_volatile_type 257 /// \param FromTy Base Type. 258 DIDerivedType *createQualifiedType(unsigned Tag, DIType *FromTy); 259 260 /// Create debugging information entry for a pointer. 261 /// \param PointeeTy Type pointed by this pointer. 262 /// \param SizeInBits Size. 263 /// \param AlignInBits Alignment. (optional) 264 /// \param DWARFAddressSpace DWARF address space. (optional) 265 /// \param Name Pointer type name. (optional) 266 /// \param Annotations Member annotations. 267 DIDerivedType * 268 createPointerType(DIType *PointeeTy, uint64_t SizeInBits, 269 uint32_t AlignInBits = 0, 270 std::optional<unsigned> DWARFAddressSpace = std::nullopt, 271 StringRef Name = "", DINodeArray Annotations = nullptr); 272 273 /// Create debugging information entry for a pointer to member. 274 /// \param PointeeTy Type pointed to by this pointer. 275 /// \param SizeInBits Size. 276 /// \param AlignInBits Alignment. (optional) 277 /// \param Class Type for which this pointer points to members of. 278 DIDerivedType * 279 createMemberPointerType(DIType *PointeeTy, DIType *Class, 280 uint64_t SizeInBits, uint32_t AlignInBits = 0, 281 DINode::DIFlags Flags = DINode::FlagZero); 282 283 /// Create debugging information entry for a c++ 284 /// style reference or rvalue reference type. 285 DIDerivedType *createReferenceType( 286 unsigned Tag, DIType *RTy, uint64_t SizeInBits = 0, 287 uint32_t AlignInBits = 0, 288 std::optional<unsigned> DWARFAddressSpace = std::nullopt); 289 290 /// Create debugging information entry for a typedef. 291 /// \param Ty Original type. 292 /// \param Name Typedef name. 293 /// \param File File where this type is defined. 294 /// \param LineNo Line number. 295 /// \param Context The surrounding context for the typedef. 296 /// \param AlignInBits Alignment. (optional) 297 /// \param Flags Flags to describe inheritance attribute, e.g. private 298 /// \param Annotations Annotations. (optional) 299 DIDerivedType *createTypedef(DIType *Ty, StringRef Name, DIFile *File, 300 unsigned LineNo, DIScope *Context, 301 uint32_t AlignInBits = 0, 302 DINode::DIFlags Flags = DINode::FlagZero, 303 DINodeArray Annotations = nullptr); 304 305 /// Create debugging information entry for a 'friend'. 306 DIDerivedType *createFriend(DIType *Ty, DIType *FriendTy); 307 308 /// Create debugging information entry to establish 309 /// inheritance relationship between two types. 310 /// \param Ty Original type. 311 /// \param BaseTy Base type. Ty is inherits from base. 312 /// \param BaseOffset Base offset. 313 /// \param VBPtrOffset Virtual base pointer offset. 314 /// \param Flags Flags to describe inheritance attribute, 315 /// e.g. private 316 DIDerivedType *createInheritance(DIType *Ty, DIType *BaseTy, 317 uint64_t BaseOffset, uint32_t VBPtrOffset, 318 DINode::DIFlags Flags); 319 320 /// Create debugging information entry for a member. 321 /// \param Scope Member scope. 322 /// \param Name Member name. 323 /// \param File File where this member is defined. 324 /// \param LineNo Line number. 325 /// \param SizeInBits Member size. 326 /// \param AlignInBits Member alignment. 327 /// \param OffsetInBits Member offset. 328 /// \param Flags Flags to encode member attribute, e.g. private 329 /// \param Ty Parent type. 330 /// \param Annotations Member annotations. 331 DIDerivedType *createMemberType(DIScope *Scope, StringRef Name, 332 DIFile *File, unsigned LineNo, 333 uint64_t SizeInBits, uint32_t AlignInBits, 334 uint64_t OffsetInBits, 335 DINode::DIFlags Flags, DIType *Ty, 336 DINodeArray Annotations = nullptr); 337 338 /// Create debugging information entry for a variant. A variant 339 /// normally should be a member of a variant part. 340 /// \param Scope Member scope. 341 /// \param Name Member name. 342 /// \param File File where this member is defined. 343 /// \param LineNo Line number. 344 /// \param SizeInBits Member size. 345 /// \param AlignInBits Member alignment. 346 /// \param OffsetInBits Member offset. 347 /// \param Flags Flags to encode member attribute, e.g. private 348 /// \param Discriminant The discriminant for this branch; null for 349 /// the default branch 350 /// \param Ty Parent type. 351 DIDerivedType *createVariantMemberType(DIScope *Scope, StringRef Name, 352 DIFile *File, unsigned LineNo, 353 uint64_t SizeInBits, 354 uint32_t AlignInBits, 355 uint64_t OffsetInBits, 356 Constant *Discriminant, 357 DINode::DIFlags Flags, DIType *Ty); 358 359 /// Create debugging information entry for a bit field member. 360 /// \param Scope Member scope. 361 /// \param Name Member name. 362 /// \param File File where this member is defined. 363 /// \param LineNo Line number. 364 /// \param SizeInBits Member size. 365 /// \param OffsetInBits Member offset. 366 /// \param StorageOffsetInBits Member storage offset. 367 /// \param Flags Flags to encode member attribute. 368 /// \param Ty Parent type. 369 /// \param Annotations Member annotations. 370 DIDerivedType *createBitFieldMemberType(DIScope *Scope, StringRef Name, 371 DIFile *File, unsigned LineNo, 372 uint64_t SizeInBits, 373 uint64_t OffsetInBits, 374 uint64_t StorageOffsetInBits, 375 DINode::DIFlags Flags, DIType *Ty, 376 DINodeArray Annotations = nullptr); 377 378 /// Create debugging information entry for a 379 /// C++ static data member. 380 /// \param Scope Member scope. 381 /// \param Name Member name. 382 /// \param File File where this member is declared. 383 /// \param LineNo Line number. 384 /// \param Ty Type of the static member. 385 /// \param Flags Flags to encode member attribute, e.g. private. 386 /// \param Val Const initializer of the member. 387 /// \param Tag DWARF tag of the static member. 388 /// \param AlignInBits Member alignment. 389 DIDerivedType *createStaticMemberType(DIScope *Scope, StringRef Name, 390 DIFile *File, unsigned LineNo, 391 DIType *Ty, DINode::DIFlags Flags, 392 Constant *Val, unsigned Tag, 393 uint32_t AlignInBits = 0); 394 395 /// Create debugging information entry for Objective-C 396 /// instance variable. 397 /// \param Name Member name. 398 /// \param File File where this member is defined. 399 /// \param LineNo Line number. 400 /// \param SizeInBits Member size. 401 /// \param AlignInBits Member alignment. 402 /// \param OffsetInBits Member offset. 403 /// \param Flags Flags to encode member attribute, e.g. private 404 /// \param Ty Parent type. 405 /// \param PropertyNode Property associated with this ivar. 406 DIDerivedType *createObjCIVar(StringRef Name, DIFile *File, unsigned LineNo, 407 uint64_t SizeInBits, uint32_t AlignInBits, 408 uint64_t OffsetInBits, DINode::DIFlags Flags, 409 DIType *Ty, MDNode *PropertyNode); 410 411 /// Create debugging information entry for Objective-C 412 /// property. 413 /// \param Name Property name. 414 /// \param File File where this property is defined. 415 /// \param LineNumber Line number. 416 /// \param GetterName Name of the Objective C property getter selector. 417 /// \param SetterName Name of the Objective C property setter selector. 418 /// \param PropertyAttributes Objective C property attributes. 419 /// \param Ty Type. 420 DIObjCProperty *createObjCProperty(StringRef Name, DIFile *File, 421 unsigned LineNumber, 422 StringRef GetterName, 423 StringRef SetterName, 424 unsigned PropertyAttributes, DIType *Ty); 425 426 /// Create debugging information entry for a class. 427 /// \param Scope Scope in which this class is defined. 428 /// \param Name class name. 429 /// \param File File where this member is defined. 430 /// \param LineNumber Line number. 431 /// \param SizeInBits Member size. 432 /// \param AlignInBits Member alignment. 433 /// \param OffsetInBits Member offset. 434 /// \param Flags Flags to encode member attribute, e.g. private 435 /// \param Elements class members. 436 /// \param RunTimeLang Optional parameter, Objective-C runtime version. 437 /// \param VTableHolder Debug info of the base class that contains vtable 438 /// for this type. This is used in 439 /// DW_AT_containing_type. See DWARF documentation 440 /// for more info. 441 /// \param TemplateParms Template type parameters. 442 /// \param UniqueIdentifier A unique identifier for the class. 443 DICompositeType *createClassType( 444 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 445 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 446 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements, 447 unsigned RunTimeLang = 0, DIType *VTableHolder = nullptr, 448 MDNode *TemplateParms = nullptr, StringRef UniqueIdentifier = ""); 449 450 /// Create debugging information entry for a struct. 451 /// \param Scope Scope in which this struct is defined. 452 /// \param Name Struct name. 453 /// \param File File where this member is defined. 454 /// \param LineNumber Line number. 455 /// \param SizeInBits Member size. 456 /// \param AlignInBits Member alignment. 457 /// \param Flags Flags to encode member attribute, e.g. private 458 /// \param Elements Struct elements. 459 /// \param RunTimeLang Optional parameter, Objective-C runtime version. 460 /// \param UniqueIdentifier A unique identifier for the struct. 461 DICompositeType *createStructType( 462 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 463 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags, 464 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang = 0, 465 DIType *VTableHolder = nullptr, StringRef UniqueIdentifier = ""); 466 467 /// Create debugging information entry for an union. 468 /// \param Scope Scope in which this union is defined. 469 /// \param Name Union name. 470 /// \param File File where this member is defined. 471 /// \param LineNumber Line number. 472 /// \param SizeInBits Member size. 473 /// \param AlignInBits Member alignment. 474 /// \param Flags Flags to encode member attribute, e.g. private 475 /// \param Elements Union elements. 476 /// \param RunTimeLang Optional parameter, Objective-C runtime version. 477 /// \param UniqueIdentifier A unique identifier for the union. 478 DICompositeType *createUnionType(DIScope *Scope, StringRef Name, 479 DIFile *File, unsigned LineNumber, 480 uint64_t SizeInBits, uint32_t AlignInBits, 481 DINode::DIFlags Flags, 482 DINodeArray Elements, 483 unsigned RunTimeLang = 0, 484 StringRef UniqueIdentifier = ""); 485 486 /// Create debugging information entry for a variant part. A 487 /// variant part normally has a discriminator (though this is not 488 /// required) and a number of variant children. 489 /// \param Scope Scope in which this union is defined. 490 /// \param Name Union name. 491 /// \param File File where this member is defined. 492 /// \param LineNumber Line number. 493 /// \param SizeInBits Member size. 494 /// \param AlignInBits Member alignment. 495 /// \param Flags Flags to encode member attribute, e.g. private 496 /// \param Discriminator Discriminant member 497 /// \param Elements Variant elements. 498 /// \param UniqueIdentifier A unique identifier for the union. 499 DICompositeType *createVariantPart(DIScope *Scope, StringRef Name, 500 DIFile *File, unsigned LineNumber, 501 uint64_t SizeInBits, uint32_t AlignInBits, 502 DINode::DIFlags Flags, 503 DIDerivedType *Discriminator, 504 DINodeArray Elements, 505 StringRef UniqueIdentifier = ""); 506 507 /// Create debugging information for template 508 /// type parameter. 509 /// \param Scope Scope in which this type is defined. 510 /// \param Name Type parameter name. 511 /// \param Ty Parameter type. 512 /// \param IsDefault Parameter is default or not 513 DITemplateTypeParameter *createTemplateTypeParameter(DIScope *Scope, 514 StringRef Name, 515 DIType *Ty, 516 bool IsDefault); 517 518 /// Create debugging information for template 519 /// value parameter. 520 /// \param Scope Scope in which this type is defined. 521 /// \param Name Value parameter name. 522 /// \param Ty Parameter type. 523 /// \param IsDefault Parameter is default or not 524 /// \param Val Constant parameter value. 525 DITemplateValueParameter * 526 createTemplateValueParameter(DIScope *Scope, StringRef Name, DIType *Ty, 527 bool IsDefault, Constant *Val); 528 529 /// Create debugging information for a template template parameter. 530 /// \param Scope Scope in which this type is defined. 531 /// \param Name Value parameter name. 532 /// \param Ty Parameter type. 533 /// \param Val The fully qualified name of the template. 534 /// \param IsDefault Parameter is default or not. 535 DITemplateValueParameter * 536 createTemplateTemplateParameter(DIScope *Scope, StringRef Name, DIType *Ty, 537 StringRef Val, bool IsDefault = false); 538 539 /// Create debugging information for a template parameter pack. 540 /// \param Scope Scope in which this type is defined. 541 /// \param Name Value parameter name. 542 /// \param Ty Parameter type. 543 /// \param Val An array of types in the pack. 544 DITemplateValueParameter *createTemplateParameterPack(DIScope *Scope, 545 StringRef Name, 546 DIType *Ty, 547 DINodeArray Val); 548 549 /// Create debugging information entry for an array. 550 /// \param Size Array size. 551 /// \param AlignInBits Alignment. 552 /// \param Ty Element type. 553 /// \param Subscripts Subscripts. 554 /// \param DataLocation The location of the raw data of a descriptor-based 555 /// Fortran array, either a DIExpression* or 556 /// a DIVariable*. 557 /// \param Associated The associated attribute of a descriptor-based 558 /// Fortran array, either a DIExpression* or 559 /// a DIVariable*. 560 /// \param Allocated The allocated attribute of a descriptor-based 561 /// Fortran array, either a DIExpression* or 562 /// a DIVariable*. 563 /// \param Rank The rank attribute of a descriptor-based 564 /// Fortran array, either a DIExpression* or 565 /// a DIVariable*. 566 DICompositeType *createArrayType( 567 uint64_t Size, uint32_t AlignInBits, DIType *Ty, DINodeArray Subscripts, 568 PointerUnion<DIExpression *, DIVariable *> DataLocation = nullptr, 569 PointerUnion<DIExpression *, DIVariable *> Associated = nullptr, 570 PointerUnion<DIExpression *, DIVariable *> Allocated = nullptr, 571 PointerUnion<DIExpression *, DIVariable *> Rank = nullptr); 572 573 /// Create debugging information entry for a vector type. 574 /// \param Size Array size. 575 /// \param AlignInBits Alignment. 576 /// \param Ty Element type. 577 /// \param Subscripts Subscripts. 578 DICompositeType *createVectorType(uint64_t Size, uint32_t AlignInBits, 579 DIType *Ty, DINodeArray Subscripts); 580 581 /// Create debugging information entry for an 582 /// enumeration. 583 /// \param Scope Scope in which this enumeration is defined. 584 /// \param Name Union name. 585 /// \param File File where this member is defined. 586 /// \param LineNumber Line number. 587 /// \param SizeInBits Member size. 588 /// \param AlignInBits Member alignment. 589 /// \param Elements Enumeration elements. 590 /// \param UnderlyingType Underlying type of a C++11/ObjC fixed enum. 591 /// \param RunTimeLang Optional parameter, Objective-C runtime version. 592 /// \param UniqueIdentifier A unique identifier for the enum. 593 /// \param IsScoped Boolean flag indicate if this is C++11/ObjC 'enum 594 /// class'. 595 DICompositeType *createEnumerationType( 596 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber, 597 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements, 598 DIType *UnderlyingType, unsigned RunTimeLang = 0, 599 StringRef UniqueIdentifier = "", bool IsScoped = false); 600 /// Create debugging information entry for a set. 601 /// \param Scope Scope in which this set is defined. 602 /// \param Name Set name. 603 /// \param File File where this set is defined. 604 /// \param LineNo Line number. 605 /// \param SizeInBits Set size. 606 /// \param AlignInBits Set alignment. 607 /// \param Ty Base type of the set. 608 DIDerivedType *createSetType(DIScope *Scope, StringRef Name, DIFile *File, 609 unsigned LineNo, uint64_t SizeInBits, 610 uint32_t AlignInBits, DIType *Ty); 611 612 /// Create subroutine type. 613 /// \param ParameterTypes An array of subroutine parameter types. This 614 /// includes return type at 0th index. 615 /// \param Flags E.g.: LValueReference. 616 /// These flags are used to emit dwarf attributes. 617 /// \param CC Calling convention, e.g. dwarf::DW_CC_normal 618 DISubroutineType * 619 createSubroutineType(DITypeRefArray ParameterTypes, 620 DINode::DIFlags Flags = DINode::FlagZero, 621 unsigned CC = 0); 622 623 /// Create a distinct clone of \p SP with FlagArtificial set. 624 static DISubprogram *createArtificialSubprogram(DISubprogram *SP); 625 626 /// Create a uniqued clone of \p Ty with FlagArtificial set. 627 static DIType *createArtificialType(DIType *Ty); 628 629 /// Create a uniqued clone of \p Ty with FlagObjectPointer and 630 /// FlagArtificial set. 631 static DIType *createObjectPointerType(DIType *Ty); 632 633 /// Create a permanent forward-declared type. 634 DICompositeType *createForwardDecl(unsigned Tag, StringRef Name, 635 DIScope *Scope, DIFile *F, unsigned Line, 636 unsigned RuntimeLang = 0, 637 uint64_t SizeInBits = 0, 638 uint32_t AlignInBits = 0, 639 StringRef UniqueIdentifier = ""); 640 641 /// Create a temporary forward-declared type. 642 DICompositeType *createReplaceableCompositeType( 643 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line, 644 unsigned RuntimeLang = 0, uint64_t SizeInBits = 0, 645 uint32_t AlignInBits = 0, DINode::DIFlags Flags = DINode::FlagFwdDecl, 646 StringRef UniqueIdentifier = "", DINodeArray Annotations = nullptr); 647 648 /// Retain DIScope* in a module even if it is not referenced 649 /// through debug info anchors. 650 void retainType(DIScope *T); 651 652 /// Create unspecified parameter type 653 /// for a subroutine type. 654 DIBasicType *createUnspecifiedParameter(); 655 656 /// Get a DINodeArray, create one if required. 657 DINodeArray getOrCreateArray(ArrayRef<Metadata *> Elements); 658 659 /// Get a DIMacroNodeArray, create one if required. 660 DIMacroNodeArray getOrCreateMacroArray(ArrayRef<Metadata *> Elements); 661 662 /// Get a DITypeRefArray, create one if required. 663 DITypeRefArray getOrCreateTypeArray(ArrayRef<Metadata *> Elements); 664 665 /// Create a descriptor for a value range. This 666 /// implicitly uniques the values returned. 667 DISubrange *getOrCreateSubrange(int64_t Lo, int64_t Count); 668 DISubrange *getOrCreateSubrange(int64_t Lo, Metadata *CountNode); 669 DISubrange *getOrCreateSubrange(Metadata *Count, Metadata *LowerBound, 670 Metadata *UpperBound, Metadata *Stride); 671 672 DIGenericSubrange * 673 getOrCreateGenericSubrange(DIGenericSubrange::BoundType Count, 674 DIGenericSubrange::BoundType LowerBound, 675 DIGenericSubrange::BoundType UpperBound, 676 DIGenericSubrange::BoundType Stride); 677 678 /// Create a new descriptor for the specified variable. 679 /// \param Context Variable scope. 680 /// \param Name Name of the variable. 681 /// \param LinkageName Mangled name of the variable. 682 /// \param File File where this variable is defined. 683 /// \param LineNo Line number. 684 /// \param Ty Variable Type. 685 /// \param IsLocalToUnit Boolean flag indicate whether this variable is 686 /// externally visible or not. 687 /// \param Expr The location of the global relative to the attached 688 /// GlobalVariable. 689 /// \param Decl Reference to the corresponding declaration. 690 /// \param AlignInBits Variable alignment(or 0 if no alignment attr was 691 /// specified) 692 DIGlobalVariableExpression *createGlobalVariableExpression( 693 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File, 694 unsigned LineNo, DIType *Ty, bool IsLocalToUnit, bool isDefined = true, 695 DIExpression *Expr = nullptr, MDNode *Decl = nullptr, 696 MDTuple *TemplateParams = nullptr, uint32_t AlignInBits = 0, 697 DINodeArray Annotations = nullptr); 698 699 /// Identical to createGlobalVariable 700 /// except that the resulting DbgNode is temporary and meant to be RAUWed. 701 DIGlobalVariable *createTempGlobalVariableFwdDecl( 702 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File, 703 unsigned LineNo, DIType *Ty, bool IsLocalToUnit, MDNode *Decl = nullptr, 704 MDTuple *TemplateParams = nullptr, uint32_t AlignInBits = 0); 705 706 /// Create a new descriptor for an auto variable. This is a local variable 707 /// that is not a subprogram parameter. 708 /// 709 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually 710 /// leads to a \a DISubprogram. 711 /// 712 /// If \c AlwaysPreserve, this variable will be referenced from its 713 /// containing subprogram, and will survive some optimizations. 714 DILocalVariable * 715 createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File, 716 unsigned LineNo, DIType *Ty, bool AlwaysPreserve = false, 717 DINode::DIFlags Flags = DINode::FlagZero, 718 uint32_t AlignInBits = 0); 719 720 /// Create a new descriptor for an label. 721 /// 722 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually 723 /// leads to a \a DISubprogram. 724 DILabel * 725 createLabel(DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo, 726 bool AlwaysPreserve = false); 727 728 /// Create a new descriptor for a parameter variable. 729 /// 730 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually 731 /// leads to a \a DISubprogram. 732 /// 733 /// \c ArgNo is the index (starting from \c 1) of this variable in the 734 /// subprogram parameters. \c ArgNo should not conflict with other 735 /// parameters of the same subprogram. 736 /// 737 /// If \c AlwaysPreserve, this variable will be referenced from its 738 /// containing subprogram, and will survive some optimizations. 739 DILocalVariable * 740 createParameterVariable(DIScope *Scope, StringRef Name, unsigned ArgNo, 741 DIFile *File, unsigned LineNo, DIType *Ty, 742 bool AlwaysPreserve = false, 743 DINode::DIFlags Flags = DINode::FlagZero, 744 DINodeArray Annotations = nullptr); 745 746 /// Create a new descriptor for the specified 747 /// variable which has a complex address expression for its address. 748 /// \param Addr An array of complex address operations. 749 DIExpression *createExpression(ArrayRef<uint64_t> Addr = std::nullopt); 750 751 /// Create an expression for a variable that does not have an address, but 752 /// does have a constant value. createConstantValueExpression(uint64_t Val)753 DIExpression *createConstantValueExpression(uint64_t Val) { 754 return DIExpression::get( 755 VMContext, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_stack_value}); 756 } 757 758 /// Create a new descriptor for the specified subprogram. 759 /// See comments in DISubprogram* for descriptions of these fields. 760 /// \param Scope Function scope. 761 /// \param Name Function name. 762 /// \param LinkageName Mangled function name. 763 /// \param File File where this variable is defined. 764 /// \param LineNo Line number. 765 /// \param Ty Function type. 766 /// \param ScopeLine Set to the beginning of the scope this starts 767 /// \param Flags e.g. is this function prototyped or not. 768 /// These flags are used to emit dwarf attributes. 769 /// \param SPFlags Additional flags specific to subprograms. 770 /// \param TParams Function template parameters. 771 /// \param ThrownTypes Exception types this function may throw. 772 /// \param Annotations Attribute Annotations. 773 /// \param TargetFuncName The name of the target function if this is 774 /// a trampoline. 775 DISubprogram * 776 createFunction(DIScope *Scope, StringRef Name, StringRef LinkageName, 777 DIFile *File, unsigned LineNo, DISubroutineType *Ty, 778 unsigned ScopeLine, DINode::DIFlags Flags = DINode::FlagZero, 779 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero, 780 DITemplateParameterArray TParams = nullptr, 781 DISubprogram *Decl = nullptr, 782 DITypeArray ThrownTypes = nullptr, 783 DINodeArray Annotations = nullptr, 784 StringRef TargetFuncName = ""); 785 786 /// Identical to createFunction, 787 /// except that the resulting DbgNode is meant to be RAUWed. 788 DISubprogram *createTempFunctionFwdDecl( 789 DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File, 790 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, 791 DINode::DIFlags Flags = DINode::FlagZero, 792 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero, 793 DITemplateParameterArray TParams = nullptr, 794 DISubprogram *Decl = nullptr, DITypeArray ThrownTypes = nullptr); 795 796 /// Create a new descriptor for the specified C++ method. 797 /// See comments in \a DISubprogram* for descriptions of these fields. 798 /// \param Scope Function scope. 799 /// \param Name Function name. 800 /// \param LinkageName Mangled function name. 801 /// \param File File where this variable is defined. 802 /// \param LineNo Line number. 803 /// \param Ty Function type. 804 /// \param VTableIndex Index no of this method in virtual table, or -1u if 805 /// unrepresentable. 806 /// \param ThisAdjustment 807 /// MS ABI-specific adjustment of 'this' that occurs 808 /// in the prologue. 809 /// \param VTableHolder Type that holds vtable. 810 /// \param Flags e.g. is this function prototyped or not. 811 /// This flags are used to emit dwarf attributes. 812 /// \param SPFlags Additional flags specific to subprograms. 813 /// \param TParams Function template parameters. 814 /// \param ThrownTypes Exception types this function may throw. 815 DISubprogram * 816 createMethod(DIScope *Scope, StringRef Name, StringRef LinkageName, 817 DIFile *File, unsigned LineNo, DISubroutineType *Ty, 818 unsigned VTableIndex = 0, int ThisAdjustment = 0, 819 DIType *VTableHolder = nullptr, 820 DINode::DIFlags Flags = DINode::FlagZero, 821 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero, 822 DITemplateParameterArray TParams = nullptr, 823 DITypeArray ThrownTypes = nullptr); 824 825 /// Create common block entry for a Fortran common block. 826 /// \param Scope Scope of this common block. 827 /// \param decl Global variable declaration. 828 /// \param Name The name of this common block. 829 /// \param File The file this common block is defined. 830 /// \param LineNo Line number. 831 DICommonBlock *createCommonBlock(DIScope *Scope, DIGlobalVariable *decl, 832 StringRef Name, DIFile *File, 833 unsigned LineNo); 834 835 /// This creates new descriptor for a namespace with the specified 836 /// parent scope. 837 /// \param Scope Namespace scope 838 /// \param Name Name of this namespace 839 /// \param ExportSymbols True for C++ inline namespaces. 840 DINamespace *createNameSpace(DIScope *Scope, StringRef Name, 841 bool ExportSymbols); 842 843 /// This creates new descriptor for a module with the specified 844 /// parent scope. 845 /// \param Scope Parent scope 846 /// \param Name Name of this module 847 /// \param ConfigurationMacros 848 /// A space-separated shell-quoted list of -D macro 849 /// definitions as they would appear on a command line. 850 /// \param IncludePath The path to the module map file. 851 /// \param APINotesFile The path to an API notes file for this module. 852 /// \param File Source file of the module. 853 /// Used for Fortran modules. 854 /// \param LineNo Source line number of the module. 855 /// Used for Fortran modules. 856 /// \param IsDecl This is a module declaration; default to false; 857 /// when set to true, only Scope and Name are required 858 /// as this entry is just a hint for the debugger to find 859 /// the corresponding definition in the global scope. 860 DIModule *createModule(DIScope *Scope, StringRef Name, 861 StringRef ConfigurationMacros, StringRef IncludePath, 862 StringRef APINotesFile = {}, DIFile *File = nullptr, 863 unsigned LineNo = 0, bool IsDecl = false); 864 865 /// This creates a descriptor for a lexical block with a new file 866 /// attached. This merely extends the existing 867 /// lexical block as it crosses a file. 868 /// \param Scope Lexical block. 869 /// \param File Source file. 870 /// \param Discriminator DWARF path discriminator value. 871 DILexicalBlockFile *createLexicalBlockFile(DIScope *Scope, DIFile *File, 872 unsigned Discriminator = 0); 873 874 /// This creates a descriptor for a lexical block with the 875 /// specified parent context. 876 /// \param Scope Parent lexical scope. 877 /// \param File Source file. 878 /// \param Line Line number. 879 /// \param Col Column number. 880 DILexicalBlock *createLexicalBlock(DIScope *Scope, DIFile *File, 881 unsigned Line, unsigned Col); 882 883 /// Create a descriptor for an imported module. 884 /// \param Context The scope this module is imported into 885 /// \param NS The namespace being imported here. 886 /// \param File File where the declaration is located. 887 /// \param Line Line number of the declaration. 888 /// \param Elements Renamed elements. 889 DIImportedEntity *createImportedModule(DIScope *Context, DINamespace *NS, 890 DIFile *File, unsigned Line, 891 DINodeArray Elements = nullptr); 892 893 /// Create a descriptor for an imported module. 894 /// \param Context The scope this module is imported into. 895 /// \param NS An aliased namespace. 896 /// \param File File where the declaration is located. 897 /// \param Line Line number of the declaration. 898 /// \param Elements Renamed elements. 899 DIImportedEntity *createImportedModule(DIScope *Context, 900 DIImportedEntity *NS, DIFile *File, 901 unsigned Line, 902 DINodeArray Elements = nullptr); 903 904 /// Create a descriptor for an imported module. 905 /// \param Context The scope this module is imported into. 906 /// \param M The module being imported here 907 /// \param File File where the declaration is located. 908 /// \param Line Line number of the declaration. 909 /// \param Elements Renamed elements. 910 DIImportedEntity *createImportedModule(DIScope *Context, DIModule *M, 911 DIFile *File, unsigned Line, 912 DINodeArray Elements = nullptr); 913 914 /// Create a descriptor for an imported function. 915 /// \param Context The scope this module is imported into. 916 /// \param Decl The declaration (or definition) of a function, type, or 917 /// variable. 918 /// \param File File where the declaration is located. 919 /// \param Line Line number of the declaration. 920 /// \param Elements Renamed elements. 921 DIImportedEntity *createImportedDeclaration(DIScope *Context, DINode *Decl, 922 DIFile *File, unsigned Line, 923 StringRef Name = "", 924 DINodeArray Elements = nullptr); 925 926 /// Insert a new llvm.dbg.declare intrinsic call. 927 /// \param Storage llvm::Value of the variable 928 /// \param VarInfo Variable's debug info descriptor. 929 /// \param Expr A complex location expression. 930 /// \param DL Debug info location. 931 /// \param InsertAtEnd Location for the new intrinsic. 932 DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, 933 DIExpression *Expr, const DILocation *DL, 934 BasicBlock *InsertAtEnd); 935 936 /// Insert a new llvm.dbg.assign intrinsic call. 937 /// \param LinkedInstr Instruction with a DIAssignID to link with the new 938 /// intrinsic. The intrinsic will be inserted after 939 /// this instruction. 940 /// \param Val The value component of this dbg.assign. 941 /// \param SrcVar Variable's debug info descriptor. 942 /// \param ValExpr A complex location expression to modify \p Val. 943 /// \param Addr The address component (store destination). 944 /// \param AddrExpr A complex location expression to modify \p Addr. 945 /// NOTE: \p ValExpr carries the FragInfo for the 946 /// variable. 947 /// \param DL Debug info location, usually: (line: 0, 948 /// column: 0, scope: var-decl-scope). See 949 /// getDebugValueLoc. 950 DbgInstPtr insertDbgAssign(Instruction *LinkedInstr, Value *Val, 951 DILocalVariable *SrcVar, DIExpression *ValExpr, 952 Value *Addr, DIExpression *AddrExpr, 953 const DILocation *DL); 954 955 /// Insert a new llvm.dbg.declare intrinsic call. 956 /// \param Storage llvm::Value of the variable 957 /// \param VarInfo Variable's debug info descriptor. 958 /// \param Expr A complex location expression. 959 /// \param DL Debug info location. 960 /// \param InsertBefore Location for the new intrinsic. 961 DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, 962 DIExpression *Expr, const DILocation *DL, 963 Instruction *InsertBefore); 964 965 /// Insert a new llvm.dbg.label intrinsic call. 966 /// \param LabelInfo Label's debug info descriptor. 967 /// \param DL Debug info location. 968 /// \param InsertBefore Location for the new intrinsic. 969 DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL, 970 Instruction *InsertBefore); 971 972 /// Insert a new llvm.dbg.label intrinsic call. 973 /// \param LabelInfo Label's debug info descriptor. 974 /// \param DL Debug info location. 975 /// \param InsertAtEnd Location for the new intrinsic. 976 DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL, 977 BasicBlock *InsertAtEnd); 978 979 /// Insert a new llvm.dbg.value intrinsic call. 980 /// \param Val llvm::Value of the variable 981 /// \param VarInfo Variable's debug info descriptor. 982 /// \param Expr A complex location expression. 983 /// \param DL Debug info location. 984 /// \param InsertAtEnd Location for the new intrinsic. 985 DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, 986 DILocalVariable *VarInfo, 987 DIExpression *Expr, const DILocation *DL, 988 BasicBlock *InsertAtEnd); 989 990 /// Insert a new llvm.dbg.value intrinsic call. 991 /// \param Val llvm::Value of the variable 992 /// \param VarInfo Variable's debug info descriptor. 993 /// \param Expr A complex location expression. 994 /// \param DL Debug info location. 995 /// \param InsertBefore Location for the new intrinsic. 996 DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, 997 DILocalVariable *VarInfo, 998 DIExpression *Expr, const DILocation *DL, 999 Instruction *InsertBefore); 1000 1001 /// Replace the vtable holder in the given type. 1002 /// 1003 /// If this creates a self reference, it may orphan some unresolved cycles 1004 /// in the operands of \c T, so \a DIBuilder needs to track that. 1005 void replaceVTableHolder(DICompositeType *&T, 1006 DIType *VTableHolder); 1007 1008 /// Replace arrays on a composite type. 1009 /// 1010 /// If \c T is resolved, but the arrays aren't -- which can happen if \c T 1011 /// has a self-reference -- \a DIBuilder needs to track the array to 1012 /// resolve cycles. 1013 void replaceArrays(DICompositeType *&T, DINodeArray Elements, 1014 DINodeArray TParams = DINodeArray()); 1015 1016 /// Replace a temporary node. 1017 /// 1018 /// Call \a MDNode::replaceAllUsesWith() on \c N, replacing it with \c 1019 /// Replacement. 1020 /// 1021 /// If \c Replacement is the same as \c N.get(), instead call \a 1022 /// MDNode::replaceWithUniqued(). In this case, the uniqued node could 1023 /// have a different address, so we return the final address. 1024 template <class NodeTy> replaceTemporary(TempMDNode && N,NodeTy * Replacement)1025 NodeTy *replaceTemporary(TempMDNode &&N, NodeTy *Replacement) { 1026 if (N.get() == Replacement) 1027 return cast<NodeTy>(MDNode::replaceWithUniqued(std::move(N))); 1028 1029 N->replaceAllUsesWith(Replacement); 1030 return Replacement; 1031 } 1032 }; 1033 1034 // Create wrappers for C Binding types (see CBindingWrapping.h). 1035 DEFINE_ISA_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef) 1036 1037 } // end namespace llvm 1038 1039 #endif // LLVM_IR_DIBUILDER_H 1040