1 //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- 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 // Declarations for metadata specific to debug info. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_IR_DEBUGINFOMETADATA_H 14 #define LLVM_IR_DEBUGINFOMETADATA_H 15 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/BitmaskEnum.h" 18 #include "llvm/ADT/None.h" 19 #include "llvm/ADT/Optional.h" 20 #include "llvm/ADT/PointerUnion.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/iterator_range.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/Metadata.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/Discriminator.h" 30 #include <cassert> 31 #include <climits> 32 #include <cstddef> 33 #include <cstdint> 34 #include <iterator> 35 #include <vector> 36 37 // Helper macros for defining get() overrides. 38 #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__ 39 #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS 40 #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \ 41 static CLASS *getDistinct(LLVMContext &Context, \ 42 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 43 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \ 44 } \ 45 static Temp##CLASS getTemporary(LLVMContext &Context, \ 46 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 47 return Temp##CLASS( \ 48 getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \ 49 } 50 #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \ 51 static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 52 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \ 53 } \ 54 static CLASS *getIfExists(LLVMContext &Context, \ 55 DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \ 56 return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \ 57 /* ShouldCreate */ false); \ 58 } \ 59 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) 60 61 namespace llvm { 62 63 namespace dwarf { 64 enum Tag : uint16_t; 65 } 66 67 extern cl::opt<bool> EnableFSDiscriminator; 68 69 class DITypeRefArray { 70 const MDTuple *N = nullptr; 71 72 public: 73 DITypeRefArray() = default; 74 DITypeRefArray(const MDTuple *N) : N(N) {} 75 76 explicit operator bool() const { return get(); } 77 explicit operator MDTuple *() const { return get(); } 78 79 MDTuple *get() const { return const_cast<MDTuple *>(N); } 80 MDTuple *operator->() const { return get(); } 81 MDTuple &operator*() const { return *get(); } 82 83 // FIXME: Fix callers and remove condition on N. 84 unsigned size() const { return N ? N->getNumOperands() : 0u; } 85 DIType *operator[](unsigned I) const { 86 return cast_or_null<DIType>(N->getOperand(I)); 87 } 88 89 class iterator { 90 MDNode::op_iterator I = nullptr; 91 92 public: 93 using iterator_category = std::input_iterator_tag; 94 using value_type = DIType *; 95 using difference_type = std::ptrdiff_t; 96 using pointer = void; 97 using reference = DIType *; 98 99 iterator() = default; 100 explicit iterator(MDNode::op_iterator I) : I(I) {} 101 102 DIType *operator*() const { return cast_or_null<DIType>(*I); } 103 104 iterator &operator++() { 105 ++I; 106 return *this; 107 } 108 109 iterator operator++(int) { 110 iterator Temp(*this); 111 ++I; 112 return Temp; 113 } 114 115 bool operator==(const iterator &X) const { return I == X.I; } 116 bool operator!=(const iterator &X) const { return I != X.I; } 117 }; 118 119 // FIXME: Fix callers and remove condition on N. 120 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); } 121 iterator end() const { return N ? iterator(N->op_end()) : iterator(); } 122 }; 123 124 /// Tagged DWARF-like metadata node. 125 /// 126 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*, 127 /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's 128 /// potentially used for non-DWARF output. 129 class DINode : public MDNode { 130 friend class LLVMContextImpl; 131 friend class MDNode; 132 133 protected: 134 DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 135 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None) 136 : MDNode(C, ID, Storage, Ops1, Ops2) { 137 assert(Tag < 1u << 16); 138 SubclassData16 = Tag; 139 } 140 ~DINode() = default; 141 142 template <class Ty> Ty *getOperandAs(unsigned I) const { 143 return cast_or_null<Ty>(getOperand(I)); 144 } 145 146 StringRef getStringOperand(unsigned I) const { 147 if (auto *S = getOperandAs<MDString>(I)) 148 return S->getString(); 149 return StringRef(); 150 } 151 152 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) { 153 if (S.empty()) 154 return nullptr; 155 return MDString::get(Context, S); 156 } 157 158 /// Allow subclasses to mutate the tag. 159 void setTag(unsigned Tag) { SubclassData16 = Tag; } 160 161 public: 162 dwarf::Tag getTag() const; 163 164 /// Debug info flags. 165 /// 166 /// The three accessibility flags are mutually exclusive and rolled together 167 /// in the first two bits. 168 enum DIFlags : uint32_t { 169 #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID, 170 #define DI_FLAG_LARGEST_NEEDED 171 #include "llvm/IR/DebugInfoFlags.def" 172 FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic, 173 FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance | 174 FlagVirtualInheritance, 175 LLVM_MARK_AS_BITMASK_ENUM(FlagLargest) 176 }; 177 178 static DIFlags getFlag(StringRef Flag); 179 static StringRef getFlagString(DIFlags Flag); 180 181 /// Split up a flags bitfield. 182 /// 183 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns 184 /// any remaining (unrecognized) bits. 185 static DIFlags splitFlags(DIFlags Flags, 186 SmallVectorImpl<DIFlags> &SplitFlags); 187 188 static bool classof(const Metadata *MD) { 189 switch (MD->getMetadataID()) { 190 default: 191 return false; 192 case GenericDINodeKind: 193 case DISubrangeKind: 194 case DIEnumeratorKind: 195 case DIBasicTypeKind: 196 case DIStringTypeKind: 197 case DIDerivedTypeKind: 198 case DICompositeTypeKind: 199 case DISubroutineTypeKind: 200 case DIFileKind: 201 case DICompileUnitKind: 202 case DISubprogramKind: 203 case DILexicalBlockKind: 204 case DILexicalBlockFileKind: 205 case DINamespaceKind: 206 case DICommonBlockKind: 207 case DITemplateTypeParameterKind: 208 case DITemplateValueParameterKind: 209 case DIGlobalVariableKind: 210 case DILocalVariableKind: 211 case DILabelKind: 212 case DIObjCPropertyKind: 213 case DIImportedEntityKind: 214 case DIModuleKind: 215 case DIGenericSubrangeKind: 216 return true; 217 } 218 } 219 }; 220 221 /// Generic tagged DWARF-like metadata node. 222 /// 223 /// An un-specialized DWARF-like metadata node. The first operand is a 224 /// (possibly empty) null-separated \a MDString header that contains arbitrary 225 /// fields. The remaining operands are \a dwarf_operands(), and are pointers 226 /// to other metadata. 227 class GenericDINode : public DINode { 228 friend class LLVMContextImpl; 229 friend class MDNode; 230 231 GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash, 232 unsigned Tag, ArrayRef<Metadata *> Ops1, 233 ArrayRef<Metadata *> Ops2) 234 : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) { 235 setHash(Hash); 236 } 237 ~GenericDINode() { dropAllReferences(); } 238 239 void setHash(unsigned Hash) { SubclassData32 = Hash; } 240 void recalculateHash(); 241 242 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag, 243 StringRef Header, ArrayRef<Metadata *> DwarfOps, 244 StorageType Storage, bool ShouldCreate = true) { 245 return getImpl(Context, Tag, getCanonicalMDString(Context, Header), 246 DwarfOps, Storage, ShouldCreate); 247 } 248 249 static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag, 250 MDString *Header, ArrayRef<Metadata *> DwarfOps, 251 StorageType Storage, bool ShouldCreate = true); 252 253 TempGenericDINode cloneImpl() const { 254 return getTemporary(getContext(), getTag(), getHeader(), 255 SmallVector<Metadata *, 4>(dwarf_operands())); 256 } 257 258 public: 259 unsigned getHash() const { return SubclassData32; } 260 261 DEFINE_MDNODE_GET(GenericDINode, 262 (unsigned Tag, StringRef Header, 263 ArrayRef<Metadata *> DwarfOps), 264 (Tag, Header, DwarfOps)) 265 DEFINE_MDNODE_GET(GenericDINode, 266 (unsigned Tag, MDString *Header, 267 ArrayRef<Metadata *> DwarfOps), 268 (Tag, Header, DwarfOps)) 269 270 /// Return a (temporary) clone of this. 271 TempGenericDINode clone() const { return cloneImpl(); } 272 273 dwarf::Tag getTag() const; 274 StringRef getHeader() const { return getStringOperand(0); } 275 MDString *getRawHeader() const { return getOperandAs<MDString>(0); } 276 277 op_iterator dwarf_op_begin() const { return op_begin() + 1; } 278 op_iterator dwarf_op_end() const { return op_end(); } 279 op_range dwarf_operands() const { 280 return op_range(dwarf_op_begin(), dwarf_op_end()); 281 } 282 283 unsigned getNumDwarfOperands() const { return getNumOperands() - 1; } 284 const MDOperand &getDwarfOperand(unsigned I) const { 285 return getOperand(I + 1); 286 } 287 void replaceDwarfOperandWith(unsigned I, Metadata *New) { 288 replaceOperandWith(I + 1, New); 289 } 290 291 static bool classof(const Metadata *MD) { 292 return MD->getMetadataID() == GenericDINodeKind; 293 } 294 }; 295 296 /// Array subrange. 297 /// 298 /// TODO: Merge into node for DW_TAG_array_type, which should have a custom 299 /// type. 300 class DISubrange : public DINode { 301 friend class LLVMContextImpl; 302 friend class MDNode; 303 304 DISubrange(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops); 305 306 ~DISubrange() = default; 307 308 static DISubrange *getImpl(LLVMContext &Context, int64_t Count, 309 int64_t LowerBound, StorageType Storage, 310 bool ShouldCreate = true); 311 312 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode, 313 int64_t LowerBound, StorageType Storage, 314 bool ShouldCreate = true); 315 316 static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode, 317 Metadata *LowerBound, Metadata *UpperBound, 318 Metadata *Stride, StorageType Storage, 319 bool ShouldCreate = true); 320 321 TempDISubrange cloneImpl() const { 322 return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(), 323 getRawUpperBound(), getRawStride()); 324 } 325 326 public: 327 DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0), 328 (Count, LowerBound)) 329 330 DEFINE_MDNODE_GET(DISubrange, (Metadata * CountNode, int64_t LowerBound = 0), 331 (CountNode, LowerBound)) 332 333 DEFINE_MDNODE_GET(DISubrange, 334 (Metadata * CountNode, Metadata *LowerBound, 335 Metadata *UpperBound, Metadata *Stride), 336 (CountNode, LowerBound, UpperBound, Stride)) 337 338 TempDISubrange clone() const { return cloneImpl(); } 339 340 Metadata *getRawCountNode() const { return getOperand(0).get(); } 341 342 Metadata *getRawLowerBound() const { return getOperand(1).get(); } 343 344 Metadata *getRawUpperBound() const { return getOperand(2).get(); } 345 346 Metadata *getRawStride() const { return getOperand(3).get(); } 347 348 typedef PointerUnion<ConstantInt *, DIVariable *, DIExpression *> BoundType; 349 350 BoundType getCount() const; 351 352 BoundType getLowerBound() const; 353 354 BoundType getUpperBound() const; 355 356 BoundType getStride() const; 357 358 static bool classof(const Metadata *MD) { 359 return MD->getMetadataID() == DISubrangeKind; 360 } 361 }; 362 363 class DIGenericSubrange : public DINode { 364 friend class LLVMContextImpl; 365 friend class MDNode; 366 367 DIGenericSubrange(LLVMContext &C, StorageType Storage, 368 ArrayRef<Metadata *> Ops); 369 370 ~DIGenericSubrange() = default; 371 372 static DIGenericSubrange *getImpl(LLVMContext &Context, Metadata *CountNode, 373 Metadata *LowerBound, Metadata *UpperBound, 374 Metadata *Stride, StorageType Storage, 375 bool ShouldCreate = true); 376 377 TempDIGenericSubrange cloneImpl() const { 378 return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(), 379 getRawUpperBound(), getRawStride()); 380 } 381 382 public: 383 DEFINE_MDNODE_GET(DIGenericSubrange, 384 (Metadata * CountNode, Metadata *LowerBound, 385 Metadata *UpperBound, Metadata *Stride), 386 (CountNode, LowerBound, UpperBound, Stride)) 387 388 TempDIGenericSubrange clone() const { return cloneImpl(); } 389 390 Metadata *getRawCountNode() const { return getOperand(0).get(); } 391 Metadata *getRawLowerBound() const { return getOperand(1).get(); } 392 Metadata *getRawUpperBound() const { return getOperand(2).get(); } 393 Metadata *getRawStride() const { return getOperand(3).get(); } 394 395 using BoundType = PointerUnion<DIVariable *, DIExpression *>; 396 397 BoundType getCount() const; 398 BoundType getLowerBound() const; 399 BoundType getUpperBound() const; 400 BoundType getStride() const; 401 402 static bool classof(const Metadata *MD) { 403 return MD->getMetadataID() == DIGenericSubrangeKind; 404 } 405 }; 406 407 /// Enumeration value. 408 /// 409 /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no 410 /// longer creates a type cycle. 411 class DIEnumerator : public DINode { 412 friend class LLVMContextImpl; 413 friend class MDNode; 414 415 APInt Value; 416 DIEnumerator(LLVMContext &C, StorageType Storage, const APInt &Value, 417 bool IsUnsigned, ArrayRef<Metadata *> Ops); 418 DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value, 419 bool IsUnsigned, ArrayRef<Metadata *> Ops) 420 : DIEnumerator(C, Storage, APInt(64, Value, !IsUnsigned), IsUnsigned, 421 Ops) {} 422 ~DIEnumerator() = default; 423 424 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value, 425 bool IsUnsigned, StringRef Name, 426 StorageType Storage, bool ShouldCreate = true) { 427 return getImpl(Context, Value, IsUnsigned, 428 getCanonicalMDString(Context, Name), Storage, ShouldCreate); 429 } 430 static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value, 431 bool IsUnsigned, MDString *Name, 432 StorageType Storage, bool ShouldCreate = true); 433 434 TempDIEnumerator cloneImpl() const { 435 return getTemporary(getContext(), getValue(), isUnsigned(), getName()); 436 } 437 438 public: 439 DEFINE_MDNODE_GET(DIEnumerator, 440 (int64_t Value, bool IsUnsigned, StringRef Name), 441 (APInt(64, Value, !IsUnsigned), IsUnsigned, Name)) 442 DEFINE_MDNODE_GET(DIEnumerator, 443 (int64_t Value, bool IsUnsigned, MDString *Name), 444 (APInt(64, Value, !IsUnsigned), IsUnsigned, Name)) 445 DEFINE_MDNODE_GET(DIEnumerator, 446 (APInt Value, bool IsUnsigned, StringRef Name), 447 (Value, IsUnsigned, Name)) 448 DEFINE_MDNODE_GET(DIEnumerator, 449 (APInt Value, bool IsUnsigned, MDString *Name), 450 (Value, IsUnsigned, Name)) 451 452 TempDIEnumerator clone() const { return cloneImpl(); } 453 454 const APInt &getValue() const { return Value; } 455 bool isUnsigned() const { return SubclassData32; } 456 StringRef getName() const { return getStringOperand(0); } 457 458 MDString *getRawName() const { return getOperandAs<MDString>(0); } 459 460 static bool classof(const Metadata *MD) { 461 return MD->getMetadataID() == DIEnumeratorKind; 462 } 463 }; 464 465 /// Base class for scope-like contexts. 466 /// 467 /// Base class for lexical scopes and types (which are also declaration 468 /// contexts). 469 /// 470 /// TODO: Separate the concepts of declaration contexts and lexical scopes. 471 class DIScope : public DINode { 472 protected: 473 DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 474 ArrayRef<Metadata *> Ops) 475 : DINode(C, ID, Storage, Tag, Ops) {} 476 ~DIScope() = default; 477 478 public: 479 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 480 481 inline StringRef getFilename() const; 482 inline StringRef getDirectory() const; 483 inline Optional<StringRef> getSource() const; 484 485 StringRef getName() const; 486 DIScope *getScope() const; 487 488 /// Return the raw underlying file. 489 /// 490 /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it 491 /// \em is the file). If \c this is an \a DIFile, we need to return \c this. 492 /// Otherwise, return the first operand, which is where all other subclasses 493 /// store their file pointer. 494 Metadata *getRawFile() const { 495 return isa<DIFile>(this) ? const_cast<DIScope *>(this) 496 : static_cast<Metadata *>(getOperand(0)); 497 } 498 499 static bool classof(const Metadata *MD) { 500 switch (MD->getMetadataID()) { 501 default: 502 return false; 503 case DIBasicTypeKind: 504 case DIStringTypeKind: 505 case DIDerivedTypeKind: 506 case DICompositeTypeKind: 507 case DISubroutineTypeKind: 508 case DIFileKind: 509 case DICompileUnitKind: 510 case DISubprogramKind: 511 case DILexicalBlockKind: 512 case DILexicalBlockFileKind: 513 case DINamespaceKind: 514 case DICommonBlockKind: 515 case DIModuleKind: 516 return true; 517 } 518 } 519 }; 520 521 /// File. 522 /// 523 /// TODO: Merge with directory/file node (including users). 524 /// TODO: Canonicalize paths on creation. 525 class DIFile : public DIScope { 526 friend class LLVMContextImpl; 527 friend class MDNode; 528 529 public: 530 /// Which algorithm (e.g. MD5) a checksum was generated with. 531 /// 532 /// The encoding is explicit because it is used directly in Bitcode. The 533 /// value 0 is reserved to indicate the absence of a checksum in Bitcode. 534 enum ChecksumKind { 535 // The first variant was originally CSK_None, encoded as 0. The new 536 // internal representation removes the need for this by wrapping the 537 // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0 538 // encoding is reserved. 539 CSK_MD5 = 1, 540 CSK_SHA1 = 2, 541 CSK_SHA256 = 3, 542 CSK_Last = CSK_SHA256 // Should be last enumeration. 543 }; 544 545 /// A single checksum, represented by a \a Kind and a \a Value (a string). 546 template <typename T> struct ChecksumInfo { 547 /// The kind of checksum which \a Value encodes. 548 ChecksumKind Kind; 549 /// The string value of the checksum. 550 T Value; 551 552 ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) {} 553 ~ChecksumInfo() = default; 554 bool operator==(const ChecksumInfo<T> &X) const { 555 return Kind == X.Kind && Value == X.Value; 556 } 557 bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); } 558 StringRef getKindAsString() const { return getChecksumKindAsString(Kind); } 559 }; 560 561 private: 562 Optional<ChecksumInfo<MDString *>> Checksum; 563 Optional<MDString *> Source; 564 565 DIFile(LLVMContext &C, StorageType Storage, 566 Optional<ChecksumInfo<MDString *>> CS, Optional<MDString *> Src, 567 ArrayRef<Metadata *> Ops); 568 ~DIFile() = default; 569 570 static DIFile *getImpl(LLVMContext &Context, StringRef Filename, 571 StringRef Directory, 572 Optional<ChecksumInfo<StringRef>> CS, 573 Optional<StringRef> Source, StorageType Storage, 574 bool ShouldCreate = true) { 575 Optional<ChecksumInfo<MDString *>> MDChecksum; 576 if (CS) 577 MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value)); 578 return getImpl( 579 Context, getCanonicalMDString(Context, Filename), 580 getCanonicalMDString(Context, Directory), MDChecksum, 581 Source ? Optional<MDString *>(getCanonicalMDString(Context, *Source)) 582 : None, 583 Storage, ShouldCreate); 584 } 585 static DIFile *getImpl(LLVMContext &Context, MDString *Filename, 586 MDString *Directory, 587 Optional<ChecksumInfo<MDString *>> CS, 588 Optional<MDString *> Source, StorageType Storage, 589 bool ShouldCreate = true); 590 591 TempDIFile cloneImpl() const { 592 return getTemporary(getContext(), getFilename(), getDirectory(), 593 getChecksum(), getSource()); 594 } 595 596 public: 597 DEFINE_MDNODE_GET(DIFile, 598 (StringRef Filename, StringRef Directory, 599 Optional<ChecksumInfo<StringRef>> CS = None, 600 Optional<StringRef> Source = None), 601 (Filename, Directory, CS, Source)) 602 DEFINE_MDNODE_GET(DIFile, 603 (MDString * Filename, MDString *Directory, 604 Optional<ChecksumInfo<MDString *>> CS = None, 605 Optional<MDString *> Source = None), 606 (Filename, Directory, CS, Source)) 607 608 TempDIFile clone() const { return cloneImpl(); } 609 610 StringRef getFilename() const { return getStringOperand(0); } 611 StringRef getDirectory() const { return getStringOperand(1); } 612 Optional<ChecksumInfo<StringRef>> getChecksum() const { 613 Optional<ChecksumInfo<StringRef>> StringRefChecksum; 614 if (Checksum) 615 StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString()); 616 return StringRefChecksum; 617 } 618 Optional<StringRef> getSource() const { 619 return Source ? Optional<StringRef>((*Source)->getString()) : None; 620 } 621 622 MDString *getRawFilename() const { return getOperandAs<MDString>(0); } 623 MDString *getRawDirectory() const { return getOperandAs<MDString>(1); } 624 Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; } 625 Optional<MDString *> getRawSource() const { return Source; } 626 627 static StringRef getChecksumKindAsString(ChecksumKind CSKind); 628 static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr); 629 630 static bool classof(const Metadata *MD) { 631 return MD->getMetadataID() == DIFileKind; 632 } 633 }; 634 635 StringRef DIScope::getFilename() const { 636 if (auto *F = getFile()) 637 return F->getFilename(); 638 return ""; 639 } 640 641 StringRef DIScope::getDirectory() const { 642 if (auto *F = getFile()) 643 return F->getDirectory(); 644 return ""; 645 } 646 647 Optional<StringRef> DIScope::getSource() const { 648 if (auto *F = getFile()) 649 return F->getSource(); 650 return None; 651 } 652 653 /// Base class for types. 654 /// 655 /// TODO: Remove the hardcoded name and context, since many types don't use 656 /// them. 657 /// TODO: Split up flags. 658 class DIType : public DIScope { 659 unsigned Line; 660 DIFlags Flags; 661 uint64_t SizeInBits; 662 uint64_t OffsetInBits; 663 uint32_t AlignInBits; 664 665 protected: 666 DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 667 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, 668 uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops) 669 : DIScope(C, ID, Storage, Tag, Ops) { 670 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags); 671 } 672 ~DIType() = default; 673 674 void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, 675 uint64_t OffsetInBits, DIFlags Flags) { 676 this->Line = Line; 677 this->Flags = Flags; 678 this->SizeInBits = SizeInBits; 679 this->AlignInBits = AlignInBits; 680 this->OffsetInBits = OffsetInBits; 681 } 682 683 /// Change fields in place. 684 void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits, 685 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) { 686 assert(isDistinct() && "Only distinct nodes can mutate"); 687 setTag(Tag); 688 init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags); 689 } 690 691 public: 692 TempDIType clone() const { 693 return TempDIType(cast<DIType>(MDNode::clone().release())); 694 } 695 696 unsigned getLine() const { return Line; } 697 uint64_t getSizeInBits() const { return SizeInBits; } 698 uint32_t getAlignInBits() const { return AlignInBits; } 699 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; } 700 uint64_t getOffsetInBits() const { return OffsetInBits; } 701 DIFlags getFlags() const { return Flags; } 702 703 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 704 StringRef getName() const { return getStringOperand(2); } 705 706 Metadata *getRawScope() const { return getOperand(1); } 707 MDString *getRawName() const { return getOperandAs<MDString>(2); } 708 709 /// Returns a new temporary DIType with updated Flags 710 TempDIType cloneWithFlags(DIFlags NewFlags) const { 711 auto NewTy = clone(); 712 NewTy->Flags = NewFlags; 713 return NewTy; 714 } 715 716 bool isPrivate() const { 717 return (getFlags() & FlagAccessibility) == FlagPrivate; 718 } 719 bool isProtected() const { 720 return (getFlags() & FlagAccessibility) == FlagProtected; 721 } 722 bool isPublic() const { 723 return (getFlags() & FlagAccessibility) == FlagPublic; 724 } 725 bool isForwardDecl() const { return getFlags() & FlagFwdDecl; } 726 bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; } 727 bool isVirtual() const { return getFlags() & FlagVirtual; } 728 bool isArtificial() const { return getFlags() & FlagArtificial; } 729 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; } 730 bool isObjcClassComplete() const { 731 return getFlags() & FlagObjcClassComplete; 732 } 733 bool isVector() const { return getFlags() & FlagVector; } 734 bool isBitField() const { return getFlags() & FlagBitField; } 735 bool isStaticMember() const { return getFlags() & FlagStaticMember; } 736 bool isLValueReference() const { return getFlags() & FlagLValueReference; } 737 bool isRValueReference() const { return getFlags() & FlagRValueReference; } 738 bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; } 739 bool isTypePassByReference() const { 740 return getFlags() & FlagTypePassByReference; 741 } 742 bool isBigEndian() const { return getFlags() & FlagBigEndian; } 743 bool isLittleEndian() const { return getFlags() & FlagLittleEndian; } 744 bool getExportSymbols() const { return getFlags() & FlagExportSymbols; } 745 746 static bool classof(const Metadata *MD) { 747 switch (MD->getMetadataID()) { 748 default: 749 return false; 750 case DIBasicTypeKind: 751 case DIStringTypeKind: 752 case DIDerivedTypeKind: 753 case DICompositeTypeKind: 754 case DISubroutineTypeKind: 755 return true; 756 } 757 } 758 }; 759 760 /// Basic type, like 'int' or 'float'. 761 /// 762 /// TODO: Split out DW_TAG_unspecified_type. 763 /// TODO: Drop unused accessors. 764 class DIBasicType : public DIType { 765 friend class LLVMContextImpl; 766 friend class MDNode; 767 768 unsigned Encoding; 769 770 DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag, 771 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding, 772 DIFlags Flags, ArrayRef<Metadata *> Ops) 773 : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0, 774 Flags, Ops), 775 Encoding(Encoding) {} 776 ~DIBasicType() = default; 777 778 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag, 779 StringRef Name, uint64_t SizeInBits, 780 uint32_t AlignInBits, unsigned Encoding, 781 DIFlags Flags, StorageType Storage, 782 bool ShouldCreate = true) { 783 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), 784 SizeInBits, AlignInBits, Encoding, Flags, Storage, 785 ShouldCreate); 786 } 787 static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag, 788 MDString *Name, uint64_t SizeInBits, 789 uint32_t AlignInBits, unsigned Encoding, 790 DIFlags Flags, StorageType Storage, 791 bool ShouldCreate = true); 792 793 TempDIBasicType cloneImpl() const { 794 return getTemporary(getContext(), getTag(), getName(), getSizeInBits(), 795 getAlignInBits(), getEncoding(), getFlags()); 796 } 797 798 public: 799 DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name), 800 (Tag, Name, 0, 0, 0, FlagZero)) 801 DEFINE_MDNODE_GET(DIBasicType, 802 (unsigned Tag, StringRef Name, uint64_t SizeInBits), 803 (Tag, Name, SizeInBits, 0, 0, FlagZero)) 804 DEFINE_MDNODE_GET(DIBasicType, 805 (unsigned Tag, MDString *Name, uint64_t SizeInBits), 806 (Tag, Name, SizeInBits, 0, 0, FlagZero)) 807 DEFINE_MDNODE_GET(DIBasicType, 808 (unsigned Tag, StringRef Name, uint64_t SizeInBits, 809 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags), 810 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags)) 811 DEFINE_MDNODE_GET(DIBasicType, 812 (unsigned Tag, MDString *Name, uint64_t SizeInBits, 813 uint32_t AlignInBits, unsigned Encoding, DIFlags Flags), 814 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags)) 815 816 TempDIBasicType clone() const { return cloneImpl(); } 817 818 unsigned getEncoding() const { return Encoding; } 819 820 enum class Signedness { Signed, Unsigned }; 821 822 /// Return the signedness of this type, or None if this type is neither 823 /// signed nor unsigned. 824 Optional<Signedness> getSignedness() const; 825 826 static bool classof(const Metadata *MD) { 827 return MD->getMetadataID() == DIBasicTypeKind; 828 } 829 }; 830 831 /// String type, Fortran CHARACTER(n) 832 class DIStringType : public DIType { 833 friend class LLVMContextImpl; 834 friend class MDNode; 835 836 unsigned Encoding; 837 838 DIStringType(LLVMContext &C, StorageType Storage, unsigned Tag, 839 uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding, 840 ArrayRef<Metadata *> Ops) 841 : DIType(C, DIStringTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0, 842 FlagZero, Ops), 843 Encoding(Encoding) {} 844 ~DIStringType() = default; 845 846 static DIStringType *getImpl(LLVMContext &Context, unsigned Tag, 847 StringRef Name, Metadata *StringLength, 848 Metadata *StrLenExp, Metadata *StrLocationExp, 849 uint64_t SizeInBits, uint32_t AlignInBits, 850 unsigned Encoding, StorageType Storage, 851 bool ShouldCreate = true) { 852 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), 853 StringLength, StrLenExp, StrLocationExp, SizeInBits, 854 AlignInBits, Encoding, Storage, ShouldCreate); 855 } 856 static DIStringType *getImpl(LLVMContext &Context, unsigned Tag, 857 MDString *Name, Metadata *StringLength, 858 Metadata *StrLenExp, Metadata *StrLocationExp, 859 uint64_t SizeInBits, uint32_t AlignInBits, 860 unsigned Encoding, StorageType Storage, 861 bool ShouldCreate = true); 862 863 TempDIStringType cloneImpl() const { 864 return getTemporary(getContext(), getTag(), getRawName(), 865 getRawStringLength(), getRawStringLengthExp(), 866 getRawStringLocationExp(), getSizeInBits(), 867 getAlignInBits(), getEncoding()); 868 } 869 870 public: 871 DEFINE_MDNODE_GET(DIStringType, 872 (unsigned Tag, StringRef Name, uint64_t SizeInBits, 873 uint32_t AlignInBits), 874 (Tag, Name, nullptr, nullptr, nullptr, SizeInBits, 875 AlignInBits, 0)) 876 DEFINE_MDNODE_GET(DIStringType, 877 (unsigned Tag, MDString *Name, Metadata *StringLength, 878 Metadata *StringLengthExp, Metadata *StringLocationExp, 879 uint64_t SizeInBits, uint32_t AlignInBits, 880 unsigned Encoding), 881 (Tag, Name, StringLength, StringLengthExp, 882 StringLocationExp, SizeInBits, AlignInBits, Encoding)) 883 DEFINE_MDNODE_GET(DIStringType, 884 (unsigned Tag, StringRef Name, Metadata *StringLength, 885 Metadata *StringLengthExp, Metadata *StringLocationExp, 886 uint64_t SizeInBits, uint32_t AlignInBits, 887 unsigned Encoding), 888 (Tag, Name, StringLength, StringLengthExp, 889 StringLocationExp, SizeInBits, AlignInBits, Encoding)) 890 891 TempDIStringType clone() const { return cloneImpl(); } 892 893 static bool classof(const Metadata *MD) { 894 return MD->getMetadataID() == DIStringTypeKind; 895 } 896 897 DIVariable *getStringLength() const { 898 return cast_or_null<DIVariable>(getRawStringLength()); 899 } 900 901 DIExpression *getStringLengthExp() const { 902 return cast_or_null<DIExpression>(getRawStringLengthExp()); 903 } 904 905 DIExpression *getStringLocationExp() const { 906 return cast_or_null<DIExpression>(getRawStringLocationExp()); 907 } 908 909 unsigned getEncoding() const { return Encoding; } 910 911 Metadata *getRawStringLength() const { return getOperand(3); } 912 913 Metadata *getRawStringLengthExp() const { return getOperand(4); } 914 915 Metadata *getRawStringLocationExp() const { return getOperand(5); } 916 }; 917 918 /// Derived types. 919 /// 920 /// This includes qualified types, pointers, references, friends, typedefs, and 921 /// class members. 922 /// 923 /// TODO: Split out members (inheritance, fields, methods, etc.). 924 class DIDerivedType : public DIType { 925 friend class LLVMContextImpl; 926 friend class MDNode; 927 928 /// The DWARF address space of the memory pointed to or referenced by a 929 /// pointer or reference type respectively. 930 Optional<unsigned> DWARFAddressSpace; 931 932 DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag, 933 unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits, 934 uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace, 935 DIFlags Flags, ArrayRef<Metadata *> Ops) 936 : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits, 937 AlignInBits, OffsetInBits, Flags, Ops), 938 DWARFAddressSpace(DWARFAddressSpace) {} 939 ~DIDerivedType() = default; 940 941 static DIDerivedType * 942 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File, 943 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 944 uint32_t AlignInBits, uint64_t OffsetInBits, 945 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, 946 Metadata *ExtraData, DINodeArray Annotations, StorageType Storage, 947 bool ShouldCreate = true) { 948 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File, 949 Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits, 950 DWARFAddressSpace, Flags, ExtraData, Annotations.get(), 951 Storage, ShouldCreate); 952 } 953 static DIDerivedType * 954 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 955 unsigned Line, Metadata *Scope, Metadata *BaseType, 956 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 957 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, 958 Metadata *ExtraData, Metadata *Annotations, StorageType Storage, 959 bool ShouldCreate = true); 960 961 TempDIDerivedType cloneImpl() const { 962 return getTemporary( 963 getContext(), getTag(), getName(), getFile(), getLine(), getScope(), 964 getBaseType(), getSizeInBits(), getAlignInBits(), getOffsetInBits(), 965 getDWARFAddressSpace(), getFlags(), getExtraData(), getAnnotations()); 966 } 967 968 public: 969 DEFINE_MDNODE_GET( 970 DIDerivedType, 971 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 972 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 973 uint32_t AlignInBits, uint64_t OffsetInBits, 974 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, 975 Metadata *ExtraData = nullptr, Metadata *Annotations = nullptr), 976 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 977 OffsetInBits, DWARFAddressSpace, Flags, ExtraData, Annotations)) 978 DEFINE_MDNODE_GET(DIDerivedType, 979 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line, 980 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 981 uint32_t AlignInBits, uint64_t OffsetInBits, 982 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, 983 Metadata *ExtraData = nullptr, 984 DINodeArray Annotations = nullptr), 985 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, 986 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags, 987 ExtraData, Annotations)) 988 989 TempDIDerivedType clone() const { return cloneImpl(); } 990 991 /// Get the base type this is derived from. 992 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); } 993 Metadata *getRawBaseType() const { return getOperand(3); } 994 995 /// \returns The DWARF address space of the memory pointed to or referenced by 996 /// a pointer or reference type respectively. 997 Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; } 998 999 /// Get extra data associated with this derived type. 1000 /// 1001 /// Class type for pointer-to-members, objective-c property node for ivars, 1002 /// global constant wrapper for static members, or virtual base pointer offset 1003 /// for inheritance. 1004 /// 1005 /// TODO: Separate out types that need this extra operand: pointer-to-member 1006 /// types and member fields (static members and ivars). 1007 Metadata *getExtraData() const { return getRawExtraData(); } 1008 Metadata *getRawExtraData() const { return getOperand(4); } 1009 1010 /// Get annotations associated with this derived type. 1011 DINodeArray getAnnotations() const { 1012 return cast_or_null<MDTuple>(getRawAnnotations()); 1013 } 1014 Metadata *getRawAnnotations() const { return getOperand(5); } 1015 1016 /// Get casted version of extra data. 1017 /// @{ 1018 DIType *getClassType() const; 1019 1020 DIObjCProperty *getObjCProperty() const { 1021 return dyn_cast_or_null<DIObjCProperty>(getExtraData()); 1022 } 1023 1024 uint32_t getVBPtrOffset() const; 1025 1026 Constant *getStorageOffsetInBits() const; 1027 1028 Constant *getConstant() const; 1029 1030 Constant *getDiscriminantValue() const; 1031 /// @} 1032 1033 static bool classof(const Metadata *MD) { 1034 return MD->getMetadataID() == DIDerivedTypeKind; 1035 } 1036 }; 1037 1038 /// Composite types. 1039 /// 1040 /// TODO: Detach from DerivedTypeBase (split out MDEnumType?). 1041 /// TODO: Create a custom, unrelated node for DW_TAG_array_type. 1042 class DICompositeType : public DIType { 1043 friend class LLVMContextImpl; 1044 friend class MDNode; 1045 1046 unsigned RuntimeLang; 1047 1048 DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag, 1049 unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits, 1050 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1051 ArrayRef<Metadata *> Ops) 1052 : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits, 1053 AlignInBits, OffsetInBits, Flags, Ops), 1054 RuntimeLang(RuntimeLang) {} 1055 ~DICompositeType() = default; 1056 1057 /// Change fields in place. 1058 void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang, 1059 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 1060 DIFlags Flags) { 1061 assert(isDistinct() && "Only distinct nodes can mutate"); 1062 assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate"); 1063 this->RuntimeLang = RuntimeLang; 1064 DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags); 1065 } 1066 1067 static DICompositeType * 1068 getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File, 1069 unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 1070 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1071 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder, 1072 DITemplateParameterArray TemplateParams, StringRef Identifier, 1073 DIDerivedType *Discriminator, Metadata *DataLocation, 1074 Metadata *Associated, Metadata *Allocated, Metadata *Rank, 1075 DINodeArray Annotations, StorageType Storage, 1076 bool ShouldCreate = true) { 1077 return getImpl( 1078 Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope, 1079 BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(), 1080 RuntimeLang, VTableHolder, TemplateParams.get(), 1081 getCanonicalMDString(Context, Identifier), Discriminator, DataLocation, 1082 Associated, Allocated, Rank, Annotations.get(), Storage, ShouldCreate); 1083 } 1084 static DICompositeType * 1085 getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 1086 unsigned Line, Metadata *Scope, Metadata *BaseType, 1087 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 1088 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, 1089 Metadata *VTableHolder, Metadata *TemplateParams, 1090 MDString *Identifier, Metadata *Discriminator, Metadata *DataLocation, 1091 Metadata *Associated, Metadata *Allocated, Metadata *Rank, 1092 Metadata *Annotations, StorageType Storage, bool ShouldCreate = true); 1093 1094 TempDICompositeType cloneImpl() const { 1095 return getTemporary( 1096 getContext(), getTag(), getName(), getFile(), getLine(), getScope(), 1097 getBaseType(), getSizeInBits(), getAlignInBits(), getOffsetInBits(), 1098 getFlags(), getElements(), getRuntimeLang(), getVTableHolder(), 1099 getTemplateParams(), getIdentifier(), getDiscriminator(), 1100 getRawDataLocation(), getRawAssociated(), getRawAllocated(), 1101 getRawRank(), getAnnotations()); 1102 } 1103 1104 public: 1105 DEFINE_MDNODE_GET( 1106 DICompositeType, 1107 (unsigned Tag, StringRef Name, DIFile *File, unsigned Line, 1108 DIScope *Scope, DIType *BaseType, uint64_t SizeInBits, 1109 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1110 DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder, 1111 DITemplateParameterArray TemplateParams = nullptr, 1112 StringRef Identifier = "", DIDerivedType *Discriminator = nullptr, 1113 Metadata *DataLocation = nullptr, Metadata *Associated = nullptr, 1114 Metadata *Allocated = nullptr, Metadata *Rank = nullptr, 1115 DINodeArray Annotations = nullptr), 1116 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1117 OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams, 1118 Identifier, Discriminator, DataLocation, Associated, Allocated, Rank, 1119 Annotations)) 1120 DEFINE_MDNODE_GET( 1121 DICompositeType, 1122 (unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 1123 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 1124 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 1125 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, 1126 Metadata *TemplateParams = nullptr, MDString *Identifier = nullptr, 1127 Metadata *Discriminator = nullptr, Metadata *DataLocation = nullptr, 1128 Metadata *Associated = nullptr, Metadata *Allocated = nullptr, 1129 Metadata *Rank = nullptr, Metadata *Annotations = nullptr), 1130 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits, 1131 OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams, 1132 Identifier, Discriminator, DataLocation, Associated, Allocated, Rank, 1133 Annotations)) 1134 1135 TempDICompositeType clone() const { return cloneImpl(); } 1136 1137 /// Get a DICompositeType with the given ODR identifier. 1138 /// 1139 /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped 1140 /// DICompositeType for the given ODR \c Identifier. If none exists, creates 1141 /// a new node. 1142 /// 1143 /// Else, returns \c nullptr. 1144 static DICompositeType * 1145 getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, 1146 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, 1147 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, 1148 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, 1149 unsigned RuntimeLang, Metadata *VTableHolder, 1150 Metadata *TemplateParams, Metadata *Discriminator, 1151 Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, 1152 Metadata *Rank, Metadata *Annotations); 1153 static DICompositeType *getODRTypeIfExists(LLVMContext &Context, 1154 MDString &Identifier); 1155 1156 /// Build a DICompositeType with the given ODR identifier. 1157 /// 1158 /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If 1159 /// it doesn't exist, creates a new one. If it does exist and \a 1160 /// isForwardDecl(), and the new arguments would be a definition, mutates the 1161 /// the type in place. In either case, returns the type. 1162 /// 1163 /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns 1164 /// nullptr. 1165 static DICompositeType * 1166 buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, 1167 MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, 1168 Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, 1169 uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, 1170 unsigned RuntimeLang, Metadata *VTableHolder, 1171 Metadata *TemplateParams, Metadata *Discriminator, 1172 Metadata *DataLocation, Metadata *Associated, 1173 Metadata *Allocated, Metadata *Rank, Metadata *Annotations); 1174 1175 DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); } 1176 DINodeArray getElements() const { 1177 return cast_or_null<MDTuple>(getRawElements()); 1178 } 1179 DIType *getVTableHolder() const { 1180 return cast_or_null<DIType>(getRawVTableHolder()); 1181 } 1182 DITemplateParameterArray getTemplateParams() const { 1183 return cast_or_null<MDTuple>(getRawTemplateParams()); 1184 } 1185 StringRef getIdentifier() const { return getStringOperand(7); } 1186 unsigned getRuntimeLang() const { return RuntimeLang; } 1187 1188 Metadata *getRawBaseType() const { return getOperand(3); } 1189 Metadata *getRawElements() const { return getOperand(4); } 1190 Metadata *getRawVTableHolder() const { return getOperand(5); } 1191 Metadata *getRawTemplateParams() const { return getOperand(6); } 1192 MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); } 1193 Metadata *getRawDiscriminator() const { return getOperand(8); } 1194 DIDerivedType *getDiscriminator() const { 1195 return getOperandAs<DIDerivedType>(8); 1196 } 1197 Metadata *getRawDataLocation() const { return getOperand(9); } 1198 DIVariable *getDataLocation() const { 1199 return dyn_cast_or_null<DIVariable>(getRawDataLocation()); 1200 } 1201 DIExpression *getDataLocationExp() const { 1202 return dyn_cast_or_null<DIExpression>(getRawDataLocation()); 1203 } 1204 Metadata *getRawAssociated() const { return getOperand(10); } 1205 DIVariable *getAssociated() const { 1206 return dyn_cast_or_null<DIVariable>(getRawAssociated()); 1207 } 1208 DIExpression *getAssociatedExp() const { 1209 return dyn_cast_or_null<DIExpression>(getRawAssociated()); 1210 } 1211 Metadata *getRawAllocated() const { return getOperand(11); } 1212 DIVariable *getAllocated() const { 1213 return dyn_cast_or_null<DIVariable>(getRawAllocated()); 1214 } 1215 DIExpression *getAllocatedExp() const { 1216 return dyn_cast_or_null<DIExpression>(getRawAllocated()); 1217 } 1218 Metadata *getRawRank() const { return getOperand(12); } 1219 ConstantInt *getRankConst() const { 1220 if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank())) 1221 return dyn_cast_or_null<ConstantInt>(MD->getValue()); 1222 return nullptr; 1223 } 1224 DIExpression *getRankExp() const { 1225 return dyn_cast_or_null<DIExpression>(getRawRank()); 1226 } 1227 1228 Metadata *getRawAnnotations() const { return getOperand(13); } 1229 DINodeArray getAnnotations() const { 1230 return cast_or_null<MDTuple>(getRawAnnotations()); 1231 } 1232 1233 /// Replace operands. 1234 /// 1235 /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision 1236 /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track 1237 /// of its movement if necessary. 1238 /// @{ 1239 void replaceElements(DINodeArray Elements) { 1240 #ifndef NDEBUG 1241 for (DINode *Op : getElements()) 1242 assert(is_contained(Elements->operands(), Op) && 1243 "Lost a member during member list replacement"); 1244 #endif 1245 replaceOperandWith(4, Elements.get()); 1246 } 1247 1248 void replaceVTableHolder(DIType *VTableHolder) { 1249 replaceOperandWith(5, VTableHolder); 1250 } 1251 1252 void replaceTemplateParams(DITemplateParameterArray TemplateParams) { 1253 replaceOperandWith(6, TemplateParams.get()); 1254 } 1255 /// @} 1256 1257 static bool classof(const Metadata *MD) { 1258 return MD->getMetadataID() == DICompositeTypeKind; 1259 } 1260 }; 1261 1262 /// Type array for a subprogram. 1263 /// 1264 /// TODO: Fold the array of types in directly as operands. 1265 class DISubroutineType : public DIType { 1266 friend class LLVMContextImpl; 1267 friend class MDNode; 1268 1269 /// The calling convention used with DW_AT_calling_convention. Actually of 1270 /// type dwarf::CallingConvention. 1271 uint8_t CC; 1272 1273 DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags, 1274 uint8_t CC, ArrayRef<Metadata *> Ops); 1275 ~DISubroutineType() = default; 1276 1277 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags, 1278 uint8_t CC, DITypeRefArray TypeArray, 1279 StorageType Storage, 1280 bool ShouldCreate = true) { 1281 return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate); 1282 } 1283 static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags, 1284 uint8_t CC, Metadata *TypeArray, 1285 StorageType Storage, 1286 bool ShouldCreate = true); 1287 1288 TempDISubroutineType cloneImpl() const { 1289 return getTemporary(getContext(), getFlags(), getCC(), getTypeArray()); 1290 } 1291 1292 public: 1293 DEFINE_MDNODE_GET(DISubroutineType, 1294 (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray), 1295 (Flags, CC, TypeArray)) 1296 DEFINE_MDNODE_GET(DISubroutineType, 1297 (DIFlags Flags, uint8_t CC, Metadata *TypeArray), 1298 (Flags, CC, TypeArray)) 1299 1300 TempDISubroutineType clone() const { return cloneImpl(); } 1301 // Returns a new temporary DISubroutineType with updated CC 1302 TempDISubroutineType cloneWithCC(uint8_t CC) const { 1303 auto NewTy = clone(); 1304 NewTy->CC = CC; 1305 return NewTy; 1306 } 1307 1308 uint8_t getCC() const { return CC; } 1309 1310 DITypeRefArray getTypeArray() const { 1311 return cast_or_null<MDTuple>(getRawTypeArray()); 1312 } 1313 1314 Metadata *getRawTypeArray() const { return getOperand(3); } 1315 1316 static bool classof(const Metadata *MD) { 1317 return MD->getMetadataID() == DISubroutineTypeKind; 1318 } 1319 }; 1320 1321 /// Compile unit. 1322 class DICompileUnit : public DIScope { 1323 friend class LLVMContextImpl; 1324 friend class MDNode; 1325 1326 public: 1327 enum DebugEmissionKind : unsigned { 1328 NoDebug = 0, 1329 FullDebug, 1330 LineTablesOnly, 1331 DebugDirectivesOnly, 1332 LastEmissionKind = DebugDirectivesOnly 1333 }; 1334 1335 enum class DebugNameTableKind : unsigned { 1336 Default = 0, 1337 GNU = 1, 1338 None = 2, 1339 LastDebugNameTableKind = None 1340 }; 1341 1342 static Optional<DebugEmissionKind> getEmissionKind(StringRef Str); 1343 static const char *emissionKindString(DebugEmissionKind EK); 1344 static Optional<DebugNameTableKind> getNameTableKind(StringRef Str); 1345 static const char *nameTableKindString(DebugNameTableKind PK); 1346 1347 private: 1348 unsigned SourceLanguage; 1349 bool IsOptimized; 1350 unsigned RuntimeVersion; 1351 unsigned EmissionKind; 1352 uint64_t DWOId; 1353 bool SplitDebugInlining; 1354 bool DebugInfoForProfiling; 1355 unsigned NameTableKind; 1356 bool RangesBaseAddress; 1357 1358 DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage, 1359 bool IsOptimized, unsigned RuntimeVersion, 1360 unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining, 1361 bool DebugInfoForProfiling, unsigned NameTableKind, 1362 bool RangesBaseAddress, ArrayRef<Metadata *> Ops); 1363 ~DICompileUnit() = default; 1364 1365 static DICompileUnit * 1366 getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File, 1367 StringRef Producer, bool IsOptimized, StringRef Flags, 1368 unsigned RuntimeVersion, StringRef SplitDebugFilename, 1369 unsigned EmissionKind, DICompositeTypeArray EnumTypes, 1370 DIScopeArray RetainedTypes, 1371 DIGlobalVariableExpressionArray GlobalVariables, 1372 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros, 1373 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, 1374 unsigned NameTableKind, bool RangesBaseAddress, StringRef SysRoot, 1375 StringRef SDK, StorageType Storage, bool ShouldCreate = true) { 1376 return getImpl( 1377 Context, SourceLanguage, File, getCanonicalMDString(Context, Producer), 1378 IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion, 1379 getCanonicalMDString(Context, SplitDebugFilename), EmissionKind, 1380 EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(), 1381 ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining, 1382 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, 1383 getCanonicalMDString(Context, SysRoot), 1384 getCanonicalMDString(Context, SDK), Storage, ShouldCreate); 1385 } 1386 static DICompileUnit * 1387 getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File, 1388 MDString *Producer, bool IsOptimized, MDString *Flags, 1389 unsigned RuntimeVersion, MDString *SplitDebugFilename, 1390 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, 1391 Metadata *GlobalVariables, Metadata *ImportedEntities, 1392 Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining, 1393 bool DebugInfoForProfiling, unsigned NameTableKind, 1394 bool RangesBaseAddress, MDString *SysRoot, MDString *SDK, 1395 StorageType Storage, bool ShouldCreate = true); 1396 1397 TempDICompileUnit cloneImpl() const { 1398 return getTemporary( 1399 getContext(), getSourceLanguage(), getFile(), getProducer(), 1400 isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(), 1401 getEmissionKind(), getEnumTypes(), getRetainedTypes(), 1402 getGlobalVariables(), getImportedEntities(), getMacros(), DWOId, 1403 getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(), 1404 getRangesBaseAddress(), getSysRoot(), getSDK()); 1405 } 1406 1407 public: 1408 static void get() = delete; 1409 static void getIfExists() = delete; 1410 1411 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY( 1412 DICompileUnit, 1413 (unsigned SourceLanguage, DIFile *File, StringRef Producer, 1414 bool IsOptimized, StringRef Flags, unsigned RuntimeVersion, 1415 StringRef SplitDebugFilename, DebugEmissionKind EmissionKind, 1416 DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes, 1417 DIGlobalVariableExpressionArray GlobalVariables, 1418 DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros, 1419 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, 1420 DebugNameTableKind NameTableKind, bool RangesBaseAddress, 1421 StringRef SysRoot, StringRef SDK), 1422 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion, 1423 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, 1424 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining, 1425 DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress, 1426 SysRoot, SDK)) 1427 DEFINE_MDNODE_GET_DISTINCT_TEMPORARY( 1428 DICompileUnit, 1429 (unsigned SourceLanguage, Metadata *File, MDString *Producer, 1430 bool IsOptimized, MDString *Flags, unsigned RuntimeVersion, 1431 MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes, 1432 Metadata *RetainedTypes, Metadata *GlobalVariables, 1433 Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId, 1434 bool SplitDebugInlining, bool DebugInfoForProfiling, 1435 unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot, 1436 MDString *SDK), 1437 (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion, 1438 SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes, 1439 GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining, 1440 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, SysRoot, SDK)) 1441 1442 TempDICompileUnit clone() const { return cloneImpl(); } 1443 1444 unsigned getSourceLanguage() const { return SourceLanguage; } 1445 bool isOptimized() const { return IsOptimized; } 1446 unsigned getRuntimeVersion() const { return RuntimeVersion; } 1447 DebugEmissionKind getEmissionKind() const { 1448 return (DebugEmissionKind)EmissionKind; 1449 } 1450 bool isDebugDirectivesOnly() const { 1451 return EmissionKind == DebugDirectivesOnly; 1452 } 1453 bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; } 1454 DebugNameTableKind getNameTableKind() const { 1455 return (DebugNameTableKind)NameTableKind; 1456 } 1457 bool getRangesBaseAddress() const { return RangesBaseAddress; } 1458 StringRef getProducer() const { return getStringOperand(1); } 1459 StringRef getFlags() const { return getStringOperand(2); } 1460 StringRef getSplitDebugFilename() const { return getStringOperand(3); } 1461 DICompositeTypeArray getEnumTypes() const { 1462 return cast_or_null<MDTuple>(getRawEnumTypes()); 1463 } 1464 DIScopeArray getRetainedTypes() const { 1465 return cast_or_null<MDTuple>(getRawRetainedTypes()); 1466 } 1467 DIGlobalVariableExpressionArray getGlobalVariables() const { 1468 return cast_or_null<MDTuple>(getRawGlobalVariables()); 1469 } 1470 DIImportedEntityArray getImportedEntities() const { 1471 return cast_or_null<MDTuple>(getRawImportedEntities()); 1472 } 1473 DIMacroNodeArray getMacros() const { 1474 return cast_or_null<MDTuple>(getRawMacros()); 1475 } 1476 uint64_t getDWOId() const { return DWOId; } 1477 void setDWOId(uint64_t DwoId) { DWOId = DwoId; } 1478 bool getSplitDebugInlining() const { return SplitDebugInlining; } 1479 void setSplitDebugInlining(bool SplitDebugInlining) { 1480 this->SplitDebugInlining = SplitDebugInlining; 1481 } 1482 StringRef getSysRoot() const { return getStringOperand(9); } 1483 StringRef getSDK() const { return getStringOperand(10); } 1484 1485 MDString *getRawProducer() const { return getOperandAs<MDString>(1); } 1486 MDString *getRawFlags() const { return getOperandAs<MDString>(2); } 1487 MDString *getRawSplitDebugFilename() const { 1488 return getOperandAs<MDString>(3); 1489 } 1490 Metadata *getRawEnumTypes() const { return getOperand(4); } 1491 Metadata *getRawRetainedTypes() const { return getOperand(5); } 1492 Metadata *getRawGlobalVariables() const { return getOperand(6); } 1493 Metadata *getRawImportedEntities() const { return getOperand(7); } 1494 Metadata *getRawMacros() const { return getOperand(8); } 1495 MDString *getRawSysRoot() const { return getOperandAs<MDString>(9); } 1496 MDString *getRawSDK() const { return getOperandAs<MDString>(10); } 1497 1498 /// Replace arrays. 1499 /// 1500 /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and 1501 /// deleted on a uniquing collision. In practice, uniquing collisions on \a 1502 /// DICompileUnit should be fairly rare. 1503 /// @{ 1504 void replaceEnumTypes(DICompositeTypeArray N) { 1505 replaceOperandWith(4, N.get()); 1506 } 1507 void replaceRetainedTypes(DITypeArray N) { replaceOperandWith(5, N.get()); } 1508 void replaceGlobalVariables(DIGlobalVariableExpressionArray N) { 1509 replaceOperandWith(6, N.get()); 1510 } 1511 void replaceImportedEntities(DIImportedEntityArray N) { 1512 replaceOperandWith(7, N.get()); 1513 } 1514 void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); } 1515 /// @} 1516 1517 static bool classof(const Metadata *MD) { 1518 return MD->getMetadataID() == DICompileUnitKind; 1519 } 1520 }; 1521 1522 /// A scope for locals. 1523 /// 1524 /// A legal scope for lexical blocks, local variables, and debug info 1525 /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a 1526 /// DILexicalBlockFile. 1527 class DILocalScope : public DIScope { 1528 protected: 1529 DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag, 1530 ArrayRef<Metadata *> Ops) 1531 : DIScope(C, ID, Storage, Tag, Ops) {} 1532 ~DILocalScope() = default; 1533 1534 public: 1535 /// Get the subprogram for this scope. 1536 /// 1537 /// Return this if it's an \a DISubprogram; otherwise, look up the scope 1538 /// chain. 1539 DISubprogram *getSubprogram() const; 1540 1541 /// Get the first non DILexicalBlockFile scope of this scope. 1542 /// 1543 /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the 1544 /// scope chain. 1545 DILocalScope *getNonLexicalBlockFileScope() const; 1546 1547 static bool classof(const Metadata *MD) { 1548 return MD->getMetadataID() == DISubprogramKind || 1549 MD->getMetadataID() == DILexicalBlockKind || 1550 MD->getMetadataID() == DILexicalBlockFileKind; 1551 } 1552 }; 1553 1554 /// Debug location. 1555 /// 1556 /// A debug location in source code, used for debug info and otherwise. 1557 class DILocation : public MDNode { 1558 friend class LLVMContextImpl; 1559 friend class MDNode; 1560 1561 DILocation(LLVMContext &C, StorageType Storage, unsigned Line, 1562 unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode); 1563 ~DILocation() { dropAllReferences(); } 1564 1565 static DILocation *getImpl(LLVMContext &Context, unsigned Line, 1566 unsigned Column, Metadata *Scope, 1567 Metadata *InlinedAt, bool ImplicitCode, 1568 StorageType Storage, bool ShouldCreate = true); 1569 static DILocation *getImpl(LLVMContext &Context, unsigned Line, 1570 unsigned Column, DILocalScope *Scope, 1571 DILocation *InlinedAt, bool ImplicitCode, 1572 StorageType Storage, bool ShouldCreate = true) { 1573 return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope), 1574 static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage, 1575 ShouldCreate); 1576 } 1577 1578 TempDILocation cloneImpl() const { 1579 // Get the raw scope/inlinedAt since it is possible to invoke this on 1580 // a DILocation containing temporary metadata. 1581 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(), 1582 getRawInlinedAt(), isImplicitCode()); 1583 } 1584 1585 public: 1586 // Disallow replacing operands. 1587 void replaceOperandWith(unsigned I, Metadata *New) = delete; 1588 1589 DEFINE_MDNODE_GET(DILocation, 1590 (unsigned Line, unsigned Column, Metadata *Scope, 1591 Metadata *InlinedAt = nullptr, bool ImplicitCode = false), 1592 (Line, Column, Scope, InlinedAt, ImplicitCode)) 1593 DEFINE_MDNODE_GET(DILocation, 1594 (unsigned Line, unsigned Column, DILocalScope *Scope, 1595 DILocation *InlinedAt = nullptr, 1596 bool ImplicitCode = false), 1597 (Line, Column, Scope, InlinedAt, ImplicitCode)) 1598 1599 /// Return a (temporary) clone of this. 1600 TempDILocation clone() const { return cloneImpl(); } 1601 1602 unsigned getLine() const { return SubclassData32; } 1603 unsigned getColumn() const { return SubclassData16; } 1604 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); } 1605 1606 DILocation *getInlinedAt() const { 1607 return cast_or_null<DILocation>(getRawInlinedAt()); 1608 } 1609 1610 /// Check if the location corresponds to an implicit code. 1611 /// When the ImplicitCode flag is true, it means that the Instruction 1612 /// with this DILocation has been added by the front-end but it hasn't been 1613 /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing 1614 /// bracket). It's useful for code coverage to not show a counter on "empty" 1615 /// lines. 1616 bool isImplicitCode() const { return SubclassData1; } 1617 void setImplicitCode(bool ImplicitCode) { SubclassData1 = ImplicitCode; } 1618 1619 DIFile *getFile() const { return getScope()->getFile(); } 1620 StringRef getFilename() const { return getScope()->getFilename(); } 1621 StringRef getDirectory() const { return getScope()->getDirectory(); } 1622 Optional<StringRef> getSource() const { return getScope()->getSource(); } 1623 1624 /// Get the scope where this is inlined. 1625 /// 1626 /// Walk through \a getInlinedAt() and return \a getScope() from the deepest 1627 /// location. 1628 DILocalScope *getInlinedAtScope() const { 1629 if (auto *IA = getInlinedAt()) 1630 return IA->getInlinedAtScope(); 1631 return getScope(); 1632 } 1633 1634 /// Get the DWARF discriminator. 1635 /// 1636 /// DWARF discriminators distinguish identical file locations between 1637 /// instructions that are on different basic blocks. 1638 /// 1639 /// There are 3 components stored in discriminator, from lower bits: 1640 /// 1641 /// Base discriminator: assigned by AddDiscriminators pass to identify IRs 1642 /// that are defined by the same source line, but 1643 /// different basic blocks. 1644 /// Duplication factor: assigned by optimizations that will scale down 1645 /// the execution frequency of the original IR. 1646 /// Copy Identifier: assigned by optimizations that clones the IR. 1647 /// Each copy of the IR will be assigned an identifier. 1648 /// 1649 /// Encoding: 1650 /// 1651 /// The above 3 components are encoded into a 32bit unsigned integer in 1652 /// order. If the lowest bit is 1, the current component is empty, and the 1653 /// next component will start in the next bit. Otherwise, the current 1654 /// component is non-empty, and its content starts in the next bit. The 1655 /// value of each components is either 5 bit or 12 bit: if the 7th bit 1656 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the 1657 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to 1658 /// represent the component. Thus, the number of bits used for a component 1659 /// is either 0 (if it and all the next components are empty); 1 - if it is 1660 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both 1661 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last 1662 /// component is also capped at 0x1ff, even in the case when both first 1663 /// components are 0, and we'd technically have 29 bits available. 1664 /// 1665 /// For precise control over the data being encoded in the discriminator, 1666 /// use encodeDiscriminator/decodeDiscriminator. 1667 1668 inline unsigned getDiscriminator() const; 1669 1670 // For the regular discriminator, it stands for all empty components if all 1671 // the lowest 3 bits are non-zero and all higher 29 bits are unused(zero by 1672 // default). Here we fully leverage the higher 29 bits for pseudo probe use. 1673 // This is the format: 1674 // [2:0] - 0x7 1675 // [31:3] - pseudo probe fields guaranteed to be non-zero as a whole 1676 // So if the lower 3 bits is non-zero and the others has at least one 1677 // non-zero bit, it guarantees to be a pseudo probe discriminator 1678 inline static bool isPseudoProbeDiscriminator(unsigned Discriminator) { 1679 return ((Discriminator & 0x7) == 0x7) && (Discriminator & 0xFFFFFFF8); 1680 } 1681 1682 /// Returns a new DILocation with updated \p Discriminator. 1683 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const; 1684 1685 /// Returns a new DILocation with updated base discriminator \p BD. Only the 1686 /// base discriminator is set in the new DILocation, the other encoded values 1687 /// are elided. 1688 /// If the discriminator cannot be encoded, the function returns None. 1689 inline Optional<const DILocation *> 1690 cloneWithBaseDiscriminator(unsigned BD) const; 1691 1692 /// Returns the duplication factor stored in the discriminator, or 1 if no 1693 /// duplication factor (or 0) is encoded. 1694 inline unsigned getDuplicationFactor() const; 1695 1696 /// Returns the copy identifier stored in the discriminator. 1697 inline unsigned getCopyIdentifier() const; 1698 1699 /// Returns the base discriminator stored in the discriminator. 1700 inline unsigned getBaseDiscriminator() const; 1701 1702 /// Returns a new DILocation with duplication factor \p DF * current 1703 /// duplication factor encoded in the discriminator. The current duplication 1704 /// factor is as defined by getDuplicationFactor(). 1705 /// Returns None if encoding failed. 1706 inline Optional<const DILocation *> 1707 cloneByMultiplyingDuplicationFactor(unsigned DF) const; 1708 1709 /// When two instructions are combined into a single instruction we also 1710 /// need to combine the original locations into a single location. 1711 /// 1712 /// When the locations are the same we can use either location. When they 1713 /// differ, we need a third location which is distinct from either. If they 1714 /// have the same file/line but have a different discriminator we could 1715 /// create a location with a new discriminator. If they are from different 1716 /// files/lines the location is ambiguous and can't be represented in a line 1717 /// entry. In this case, if \p GenerateLocation is true, we will set the 1718 /// merged debug location as line 0 of the nearest common scope where the two 1719 /// locations are inlined from. 1720 /// 1721 /// \p GenerateLocation: Whether the merged location can be generated when 1722 /// \p LocA and \p LocB differ. 1723 static const DILocation *getMergedLocation(const DILocation *LocA, 1724 const DILocation *LocB); 1725 1726 /// Try to combine the vector of locations passed as input in a single one. 1727 /// This function applies getMergedLocation() repeatedly left-to-right. 1728 /// 1729 /// \p Locs: The locations to be merged. 1730 static const DILocation * 1731 getMergedLocations(ArrayRef<const DILocation *> Locs); 1732 1733 /// Return the masked discriminator value for an input discrimnator value D 1734 /// (i.e. zero out the (B+1)-th and above bits for D (B is 0-base). 1735 // Example: an input of (0x1FF, 7) returns 0xFF. 1736 static unsigned getMaskedDiscriminator(unsigned D, unsigned B) { 1737 return (D & getN1Bits(B)); 1738 } 1739 1740 /// Return the bits used for base discriminators. 1741 static unsigned getBaseDiscriminatorBits() { return getBaseFSBitEnd(); } 1742 1743 /// Returns the base discriminator for a given encoded discriminator \p D. 1744 static unsigned 1745 getBaseDiscriminatorFromDiscriminator(unsigned D, 1746 bool IsFSDiscriminator = false) { 1747 if (IsFSDiscriminator) 1748 return getMaskedDiscriminator(D, getBaseDiscriminatorBits()); 1749 return getUnsignedFromPrefixEncoding(D); 1750 } 1751 1752 /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor 1753 /// have certain special case behavior (e.g. treating empty duplication factor 1754 /// as the value '1'). 1755 /// This API, in conjunction with cloneWithDiscriminator, may be used to 1756 /// encode the raw values provided. 1757 /// 1758 /// \p BD: base discriminator 1759 /// \p DF: duplication factor 1760 /// \p CI: copy index 1761 /// 1762 /// The return is None if the values cannot be encoded in 32 bits - for 1763 /// example, values for BD or DF larger than 12 bits. Otherwise, the return is 1764 /// the encoded value. 1765 static Optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF, 1766 unsigned CI); 1767 1768 /// Raw decoder for values in an encoded discriminator D. 1769 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF, 1770 unsigned &CI); 1771 1772 /// Returns the duplication factor for a given encoded discriminator \p D, or 1773 /// 1 if no value or 0 is encoded. 1774 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) { 1775 if (EnableFSDiscriminator) 1776 return 1; 1777 D = getNextComponentInDiscriminator(D); 1778 unsigned Ret = getUnsignedFromPrefixEncoding(D); 1779 if (Ret == 0) 1780 return 1; 1781 return Ret; 1782 } 1783 1784 /// Returns the copy identifier for a given encoded discriminator \p D. 1785 static unsigned getCopyIdentifierFromDiscriminator(unsigned D) { 1786 return getUnsignedFromPrefixEncoding( 1787 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D))); 1788 } 1789 1790 Metadata *getRawScope() const { return getOperand(0); } 1791 Metadata *getRawInlinedAt() const { 1792 if (getNumOperands() == 2) 1793 return getOperand(1); 1794 return nullptr; 1795 } 1796 1797 static bool classof(const Metadata *MD) { 1798 return MD->getMetadataID() == DILocationKind; 1799 } 1800 }; 1801 1802 /// Subprogram description. 1803 class DISubprogram : public DILocalScope { 1804 friend class LLVMContextImpl; 1805 friend class MDNode; 1806 1807 unsigned Line; 1808 unsigned ScopeLine; 1809 unsigned VirtualIndex; 1810 1811 /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue 1812 /// of method overrides from secondary bases by this amount. It may be 1813 /// negative. 1814 int ThisAdjustment; 1815 1816 public: 1817 /// Debug info subprogram flags. 1818 enum DISPFlags : uint32_t { 1819 #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID, 1820 #define DISP_FLAG_LARGEST_NEEDED 1821 #include "llvm/IR/DebugInfoFlags.def" 1822 SPFlagNonvirtual = SPFlagZero, 1823 SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual, 1824 LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest) 1825 }; 1826 1827 static DISPFlags getFlag(StringRef Flag); 1828 static StringRef getFlagString(DISPFlags Flag); 1829 1830 /// Split up a flags bitfield for easier printing. 1831 /// 1832 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns 1833 /// any remaining (unrecognized) bits. 1834 static DISPFlags splitFlags(DISPFlags Flags, 1835 SmallVectorImpl<DISPFlags> &SplitFlags); 1836 1837 // Helper for converting old bitfields to new flags word. 1838 static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition, 1839 bool IsOptimized, 1840 unsigned Virtuality = SPFlagNonvirtual, 1841 bool IsMainSubprogram = false); 1842 1843 private: 1844 DIFlags Flags; 1845 DISPFlags SPFlags; 1846 1847 DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line, 1848 unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment, 1849 DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops); 1850 ~DISubprogram() = default; 1851 1852 static DISubprogram * 1853 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name, 1854 StringRef LinkageName, DIFile *File, unsigned Line, 1855 DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType, 1856 unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags, 1857 DISPFlags SPFlags, DICompileUnit *Unit, 1858 DITemplateParameterArray TemplateParams, DISubprogram *Declaration, 1859 DINodeArray RetainedNodes, DITypeArray ThrownTypes, 1860 DINodeArray Annotations, StringRef TargetFuncName, 1861 StorageType Storage, bool ShouldCreate = true) { 1862 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), 1863 getCanonicalMDString(Context, LinkageName), File, Line, Type, 1864 ScopeLine, ContainingType, VirtualIndex, ThisAdjustment, 1865 Flags, SPFlags, Unit, TemplateParams.get(), Declaration, 1866 RetainedNodes.get(), ThrownTypes.get(), Annotations.get(), 1867 getCanonicalMDString(Context, TargetFuncName), 1868 Storage, ShouldCreate); 1869 } 1870 static DISubprogram * 1871 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 1872 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 1873 unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex, 1874 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, 1875 Metadata *TemplateParams, Metadata *Declaration, 1876 Metadata *RetainedNodes, Metadata *ThrownTypes, Metadata *Annotations, 1877 MDString *TargetFuncName, StorageType Storage, 1878 bool ShouldCreate = true); 1879 1880 TempDISubprogram cloneImpl() const { 1881 return getTemporary(getContext(), getScope(), getName(), getLinkageName(), 1882 getFile(), getLine(), getType(), getScopeLine(), 1883 getContainingType(), getVirtualIndex(), 1884 getThisAdjustment(), getFlags(), getSPFlags(), 1885 getUnit(), getTemplateParams(), getDeclaration(), 1886 getRetainedNodes(), getThrownTypes(), getAnnotations(), 1887 getTargetFuncName()); 1888 } 1889 1890 public: 1891 DEFINE_MDNODE_GET( 1892 DISubprogram, 1893 (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File, 1894 unsigned Line, DISubroutineType *Type, unsigned ScopeLine, 1895 DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment, 1896 DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit, 1897 DITemplateParameterArray TemplateParams = nullptr, 1898 DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr, 1899 DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr, 1900 StringRef TargetFuncName = ""), 1901 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType, 1902 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams, 1903 Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName)) 1904 1905 DEFINE_MDNODE_GET( 1906 DISubprogram, 1907 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File, 1908 unsigned Line, Metadata *Type, unsigned ScopeLine, 1909 Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment, 1910 DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, 1911 Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr, 1912 Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr, 1913 Metadata *Annotations = nullptr, MDString *TargetFuncName = nullptr), 1914 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType, 1915 VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams, 1916 Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName)) 1917 1918 TempDISubprogram clone() const { return cloneImpl(); } 1919 1920 /// Returns a new temporary DISubprogram with updated Flags 1921 TempDISubprogram cloneWithFlags(DIFlags NewFlags) const { 1922 auto NewSP = clone(); 1923 NewSP->Flags = NewFlags; 1924 return NewSP; 1925 } 1926 1927 public: 1928 unsigned getLine() const { return Line; } 1929 unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; } 1930 unsigned getVirtualIndex() const { return VirtualIndex; } 1931 int getThisAdjustment() const { return ThisAdjustment; } 1932 unsigned getScopeLine() const { return ScopeLine; } 1933 void setScopeLine(unsigned L) { 1934 assert(isDistinct()); 1935 ScopeLine = L; 1936 } 1937 DIFlags getFlags() const { return Flags; } 1938 DISPFlags getSPFlags() const { return SPFlags; } 1939 bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; } 1940 bool isDefinition() const { return getSPFlags() & SPFlagDefinition; } 1941 bool isOptimized() const { return getSPFlags() & SPFlagOptimized; } 1942 bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; } 1943 1944 bool isArtificial() const { return getFlags() & FlagArtificial; } 1945 bool isPrivate() const { 1946 return (getFlags() & FlagAccessibility) == FlagPrivate; 1947 } 1948 bool isProtected() const { 1949 return (getFlags() & FlagAccessibility) == FlagProtected; 1950 } 1951 bool isPublic() const { 1952 return (getFlags() & FlagAccessibility) == FlagPublic; 1953 } 1954 bool isExplicit() const { return getFlags() & FlagExplicit; } 1955 bool isPrototyped() const { return getFlags() & FlagPrototyped; } 1956 bool areAllCallsDescribed() const { 1957 return getFlags() & FlagAllCallsDescribed; 1958 } 1959 bool isPure() const { return getSPFlags() & SPFlagPure; } 1960 bool isElemental() const { return getSPFlags() & SPFlagElemental; } 1961 bool isRecursive() const { return getSPFlags() & SPFlagRecursive; } 1962 bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; } 1963 1964 /// Check if this is deleted member function. 1965 /// 1966 /// Return true if this subprogram is a C++11 special 1967 /// member function declared deleted. 1968 bool isDeleted() const { return getSPFlags() & SPFlagDeleted; } 1969 1970 /// Check if this is reference-qualified. 1971 /// 1972 /// Return true if this subprogram is a C++11 reference-qualified non-static 1973 /// member function (void foo() &). 1974 bool isLValueReference() const { return getFlags() & FlagLValueReference; } 1975 1976 /// Check if this is rvalue-reference-qualified. 1977 /// 1978 /// Return true if this subprogram is a C++11 rvalue-reference-qualified 1979 /// non-static member function (void foo() &&). 1980 bool isRValueReference() const { return getFlags() & FlagRValueReference; } 1981 1982 /// Check if this is marked as noreturn. 1983 /// 1984 /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn 1985 bool isNoReturn() const { return getFlags() & FlagNoReturn; } 1986 1987 // Check if this routine is a compiler-generated thunk. 1988 // 1989 // Returns true if this subprogram is a thunk generated by the compiler. 1990 bool isThunk() const { return getFlags() & FlagThunk; } 1991 1992 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 1993 1994 StringRef getName() const { return getStringOperand(2); } 1995 StringRef getLinkageName() const { return getStringOperand(3); } 1996 /// Only used by clients of CloneFunction, and only right after the cloning. 1997 void replaceLinkageName(MDString *LN) { replaceOperandWith(3, LN); } 1998 1999 DISubroutineType *getType() const { 2000 return cast_or_null<DISubroutineType>(getRawType()); 2001 } 2002 DIType *getContainingType() const { 2003 return cast_or_null<DIType>(getRawContainingType()); 2004 } 2005 void replaceType(DISubroutineType *Ty) { 2006 assert(isDistinct() && "Only distinct nodes can mutate"); 2007 replaceOperandWith(4, Ty); 2008 } 2009 2010 DICompileUnit *getUnit() const { 2011 return cast_or_null<DICompileUnit>(getRawUnit()); 2012 } 2013 void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); } 2014 DITemplateParameterArray getTemplateParams() const { 2015 return cast_or_null<MDTuple>(getRawTemplateParams()); 2016 } 2017 DISubprogram *getDeclaration() const { 2018 return cast_or_null<DISubprogram>(getRawDeclaration()); 2019 } 2020 DINodeArray getRetainedNodes() const { 2021 return cast_or_null<MDTuple>(getRawRetainedNodes()); 2022 } 2023 DITypeArray getThrownTypes() const { 2024 return cast_or_null<MDTuple>(getRawThrownTypes()); 2025 } 2026 DINodeArray getAnnotations() const { 2027 return cast_or_null<MDTuple>(getRawAnnotations()); 2028 } 2029 StringRef getTargetFuncName() const { 2030 return (getRawTargetFuncName()) ? getStringOperand(12) : StringRef(); 2031 } 2032 2033 Metadata *getRawScope() const { return getOperand(1); } 2034 MDString *getRawName() const { return getOperandAs<MDString>(2); } 2035 MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); } 2036 Metadata *getRawType() const { return getOperand(4); } 2037 Metadata *getRawUnit() const { return getOperand(5); } 2038 Metadata *getRawDeclaration() const { return getOperand(6); } 2039 Metadata *getRawRetainedNodes() const { return getOperand(7); } 2040 Metadata *getRawContainingType() const { 2041 return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr; 2042 } 2043 Metadata *getRawTemplateParams() const { 2044 return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr; 2045 } 2046 Metadata *getRawThrownTypes() const { 2047 return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr; 2048 } 2049 Metadata *getRawAnnotations() const { 2050 return getNumOperands() > 11 ? getOperandAs<Metadata>(11) : nullptr; 2051 } 2052 MDString *getRawTargetFuncName() const { 2053 return getNumOperands() > 12 ? getOperandAs<MDString>(12) : nullptr; 2054 } 2055 2056 void replaceRawLinkageName(MDString *LinkageName) { 2057 replaceOperandWith(3, LinkageName); 2058 } 2059 2060 /// Check if this subprogram describes the given function. 2061 /// 2062 /// FIXME: Should this be looking through bitcasts? 2063 bool describes(const Function *F) const; 2064 2065 static bool classof(const Metadata *MD) { 2066 return MD->getMetadataID() == DISubprogramKind; 2067 } 2068 }; 2069 2070 class DILexicalBlockBase : public DILocalScope { 2071 protected: 2072 DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage, 2073 ArrayRef<Metadata *> Ops); 2074 ~DILexicalBlockBase() = default; 2075 2076 public: 2077 DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); } 2078 2079 Metadata *getRawScope() const { return getOperand(1); } 2080 2081 static bool classof(const Metadata *MD) { 2082 return MD->getMetadataID() == DILexicalBlockKind || 2083 MD->getMetadataID() == DILexicalBlockFileKind; 2084 } 2085 }; 2086 2087 class DILexicalBlock : public DILexicalBlockBase { 2088 friend class LLVMContextImpl; 2089 friend class MDNode; 2090 2091 unsigned Line; 2092 uint16_t Column; 2093 2094 DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line, 2095 unsigned Column, ArrayRef<Metadata *> Ops) 2096 : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line), 2097 Column(Column) { 2098 assert(Column < (1u << 16) && "Expected 16-bit column"); 2099 } 2100 ~DILexicalBlock() = default; 2101 2102 static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope, 2103 DIFile *File, unsigned Line, unsigned Column, 2104 StorageType Storage, 2105 bool ShouldCreate = true) { 2106 return getImpl(Context, static_cast<Metadata *>(Scope), 2107 static_cast<Metadata *>(File), Line, Column, Storage, 2108 ShouldCreate); 2109 } 2110 2111 static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope, 2112 Metadata *File, unsigned Line, unsigned Column, 2113 StorageType Storage, bool ShouldCreate = true); 2114 2115 TempDILexicalBlock cloneImpl() const { 2116 return getTemporary(getContext(), getScope(), getFile(), getLine(), 2117 getColumn()); 2118 } 2119 2120 public: 2121 DEFINE_MDNODE_GET(DILexicalBlock, 2122 (DILocalScope * Scope, DIFile *File, unsigned Line, 2123 unsigned Column), 2124 (Scope, File, Line, Column)) 2125 DEFINE_MDNODE_GET(DILexicalBlock, 2126 (Metadata * Scope, Metadata *File, unsigned Line, 2127 unsigned Column), 2128 (Scope, File, Line, Column)) 2129 2130 TempDILexicalBlock clone() const { return cloneImpl(); } 2131 2132 unsigned getLine() const { return Line; } 2133 unsigned getColumn() const { return Column; } 2134 2135 static bool classof(const Metadata *MD) { 2136 return MD->getMetadataID() == DILexicalBlockKind; 2137 } 2138 }; 2139 2140 class DILexicalBlockFile : public DILexicalBlockBase { 2141 friend class LLVMContextImpl; 2142 friend class MDNode; 2143 2144 unsigned Discriminator; 2145 2146 DILexicalBlockFile(LLVMContext &C, StorageType Storage, 2147 unsigned Discriminator, ArrayRef<Metadata *> Ops) 2148 : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops), 2149 Discriminator(Discriminator) {} 2150 ~DILexicalBlockFile() = default; 2151 2152 static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope, 2153 DIFile *File, unsigned Discriminator, 2154 StorageType Storage, 2155 bool ShouldCreate = true) { 2156 return getImpl(Context, static_cast<Metadata *>(Scope), 2157 static_cast<Metadata *>(File), Discriminator, Storage, 2158 ShouldCreate); 2159 } 2160 2161 static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope, 2162 Metadata *File, unsigned Discriminator, 2163 StorageType Storage, 2164 bool ShouldCreate = true); 2165 2166 TempDILexicalBlockFile cloneImpl() const { 2167 return getTemporary(getContext(), getScope(), getFile(), 2168 getDiscriminator()); 2169 } 2170 2171 public: 2172 DEFINE_MDNODE_GET(DILexicalBlockFile, 2173 (DILocalScope * Scope, DIFile *File, 2174 unsigned Discriminator), 2175 (Scope, File, Discriminator)) 2176 DEFINE_MDNODE_GET(DILexicalBlockFile, 2177 (Metadata * Scope, Metadata *File, unsigned Discriminator), 2178 (Scope, File, Discriminator)) 2179 2180 TempDILexicalBlockFile clone() const { return cloneImpl(); } 2181 unsigned getDiscriminator() const { return Discriminator; } 2182 2183 static bool classof(const Metadata *MD) { 2184 return MD->getMetadataID() == DILexicalBlockFileKind; 2185 } 2186 }; 2187 2188 unsigned DILocation::getDiscriminator() const { 2189 if (auto *F = dyn_cast<DILexicalBlockFile>(getScope())) 2190 return F->getDiscriminator(); 2191 return 0; 2192 } 2193 2194 const DILocation * 2195 DILocation::cloneWithDiscriminator(unsigned Discriminator) const { 2196 DIScope *Scope = getScope(); 2197 // Skip all parent DILexicalBlockFile that already have a discriminator 2198 // assigned. We do not want to have nested DILexicalBlockFiles that have 2199 // mutliple discriminators because only the leaf DILexicalBlockFile's 2200 // dominator will be used. 2201 for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope); 2202 LBF && LBF->getDiscriminator() != 0; 2203 LBF = dyn_cast<DILexicalBlockFile>(Scope)) 2204 Scope = LBF->getScope(); 2205 DILexicalBlockFile *NewScope = 2206 DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator); 2207 return DILocation::get(getContext(), getLine(), getColumn(), NewScope, 2208 getInlinedAt()); 2209 } 2210 2211 unsigned DILocation::getBaseDiscriminator() const { 2212 return getBaseDiscriminatorFromDiscriminator(getDiscriminator(), 2213 EnableFSDiscriminator); 2214 } 2215 2216 unsigned DILocation::getDuplicationFactor() const { 2217 return getDuplicationFactorFromDiscriminator(getDiscriminator()); 2218 } 2219 2220 unsigned DILocation::getCopyIdentifier() const { 2221 return getCopyIdentifierFromDiscriminator(getDiscriminator()); 2222 } 2223 2224 Optional<const DILocation *> 2225 DILocation::cloneWithBaseDiscriminator(unsigned D) const { 2226 unsigned BD, DF, CI; 2227 2228 if (EnableFSDiscriminator) { 2229 BD = getBaseDiscriminator(); 2230 if (D == BD) 2231 return this; 2232 return cloneWithDiscriminator(D); 2233 } 2234 2235 decodeDiscriminator(getDiscriminator(), BD, DF, CI); 2236 if (D == BD) 2237 return this; 2238 if (Optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI)) 2239 return cloneWithDiscriminator(*Encoded); 2240 return None; 2241 } 2242 2243 Optional<const DILocation *> 2244 DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF) const { 2245 assert(!EnableFSDiscriminator && "FSDiscriminator should not call this."); 2246 2247 DF *= getDuplicationFactor(); 2248 if (DF <= 1) 2249 return this; 2250 2251 unsigned BD = getBaseDiscriminator(); 2252 unsigned CI = getCopyIdentifier(); 2253 if (Optional<unsigned> D = encodeDiscriminator(BD, DF, CI)) 2254 return cloneWithDiscriminator(*D); 2255 return None; 2256 } 2257 2258 class DINamespace : public DIScope { 2259 friend class LLVMContextImpl; 2260 friend class MDNode; 2261 2262 unsigned ExportSymbols : 1; 2263 2264 DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols, 2265 ArrayRef<Metadata *> Ops); 2266 ~DINamespace() = default; 2267 2268 static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope, 2269 StringRef Name, bool ExportSymbols, 2270 StorageType Storage, bool ShouldCreate = true) { 2271 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), 2272 ExportSymbols, Storage, ShouldCreate); 2273 } 2274 static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope, 2275 MDString *Name, bool ExportSymbols, 2276 StorageType Storage, bool ShouldCreate = true); 2277 2278 TempDINamespace cloneImpl() const { 2279 return getTemporary(getContext(), getScope(), getName(), 2280 getExportSymbols()); 2281 } 2282 2283 public: 2284 DEFINE_MDNODE_GET(DINamespace, 2285 (DIScope * Scope, StringRef Name, bool ExportSymbols), 2286 (Scope, Name, ExportSymbols)) 2287 DEFINE_MDNODE_GET(DINamespace, 2288 (Metadata * Scope, MDString *Name, bool ExportSymbols), 2289 (Scope, Name, ExportSymbols)) 2290 2291 TempDINamespace clone() const { return cloneImpl(); } 2292 2293 bool getExportSymbols() const { return ExportSymbols; } 2294 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 2295 StringRef getName() const { return getStringOperand(2); } 2296 2297 Metadata *getRawScope() const { return getOperand(1); } 2298 MDString *getRawName() const { return getOperandAs<MDString>(2); } 2299 2300 static bool classof(const Metadata *MD) { 2301 return MD->getMetadataID() == DINamespaceKind; 2302 } 2303 }; 2304 2305 /// Represents a module in the programming language, for example, a Clang 2306 /// module, or a Fortran module. 2307 class DIModule : public DIScope { 2308 friend class LLVMContextImpl; 2309 friend class MDNode; 2310 unsigned LineNo; 2311 bool IsDecl; 2312 2313 DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo, 2314 bool IsDecl, ArrayRef<Metadata *> Ops); 2315 ~DIModule() = default; 2316 2317 static DIModule *getImpl(LLVMContext &Context, DIFile *File, DIScope *Scope, 2318 StringRef Name, StringRef ConfigurationMacros, 2319 StringRef IncludePath, StringRef APINotesFile, 2320 unsigned LineNo, bool IsDecl, StorageType Storage, 2321 bool ShouldCreate = true) { 2322 return getImpl(Context, File, Scope, getCanonicalMDString(Context, Name), 2323 getCanonicalMDString(Context, ConfigurationMacros), 2324 getCanonicalMDString(Context, IncludePath), 2325 getCanonicalMDString(Context, APINotesFile), LineNo, IsDecl, 2326 Storage, ShouldCreate); 2327 } 2328 static DIModule *getImpl(LLVMContext &Context, Metadata *File, 2329 Metadata *Scope, MDString *Name, 2330 MDString *ConfigurationMacros, MDString *IncludePath, 2331 MDString *APINotesFile, unsigned LineNo, bool IsDecl, 2332 StorageType Storage, bool ShouldCreate = true); 2333 2334 TempDIModule cloneImpl() const { 2335 return getTemporary(getContext(), getFile(), getScope(), getName(), 2336 getConfigurationMacros(), getIncludePath(), 2337 getAPINotesFile(), getLineNo(), getIsDecl()); 2338 } 2339 2340 public: 2341 DEFINE_MDNODE_GET(DIModule, 2342 (DIFile * File, DIScope *Scope, StringRef Name, 2343 StringRef ConfigurationMacros, StringRef IncludePath, 2344 StringRef APINotesFile, unsigned LineNo, 2345 bool IsDecl = false), 2346 (File, Scope, Name, ConfigurationMacros, IncludePath, 2347 APINotesFile, LineNo, IsDecl)) 2348 DEFINE_MDNODE_GET(DIModule, 2349 (Metadata * File, Metadata *Scope, MDString *Name, 2350 MDString *ConfigurationMacros, MDString *IncludePath, 2351 MDString *APINotesFile, unsigned LineNo, 2352 bool IsDecl = false), 2353 (File, Scope, Name, ConfigurationMacros, IncludePath, 2354 APINotesFile, LineNo, IsDecl)) 2355 2356 TempDIModule clone() const { return cloneImpl(); } 2357 2358 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 2359 StringRef getName() const { return getStringOperand(2); } 2360 StringRef getConfigurationMacros() const { return getStringOperand(3); } 2361 StringRef getIncludePath() const { return getStringOperand(4); } 2362 StringRef getAPINotesFile() const { return getStringOperand(5); } 2363 unsigned getLineNo() const { return LineNo; } 2364 bool getIsDecl() const { return IsDecl; } 2365 2366 Metadata *getRawScope() const { return getOperand(1); } 2367 MDString *getRawName() const { return getOperandAs<MDString>(2); } 2368 MDString *getRawConfigurationMacros() const { 2369 return getOperandAs<MDString>(3); 2370 } 2371 MDString *getRawIncludePath() const { return getOperandAs<MDString>(4); } 2372 MDString *getRawAPINotesFile() const { return getOperandAs<MDString>(5); } 2373 2374 static bool classof(const Metadata *MD) { 2375 return MD->getMetadataID() == DIModuleKind; 2376 } 2377 }; 2378 2379 /// Base class for template parameters. 2380 class DITemplateParameter : public DINode { 2381 protected: 2382 bool IsDefault; 2383 2384 DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage, 2385 unsigned Tag, bool IsDefault, ArrayRef<Metadata *> Ops) 2386 : DINode(Context, ID, Storage, Tag, Ops), IsDefault(IsDefault) {} 2387 ~DITemplateParameter() = default; 2388 2389 public: 2390 StringRef getName() const { return getStringOperand(0); } 2391 DIType *getType() const { return cast_or_null<DIType>(getRawType()); } 2392 2393 MDString *getRawName() const { return getOperandAs<MDString>(0); } 2394 Metadata *getRawType() const { return getOperand(1); } 2395 bool isDefault() const { return IsDefault; } 2396 2397 static bool classof(const Metadata *MD) { 2398 return MD->getMetadataID() == DITemplateTypeParameterKind || 2399 MD->getMetadataID() == DITemplateValueParameterKind; 2400 } 2401 }; 2402 2403 class DITemplateTypeParameter : public DITemplateParameter { 2404 friend class LLVMContextImpl; 2405 friend class MDNode; 2406 2407 DITemplateTypeParameter(LLVMContext &Context, StorageType Storage, 2408 bool IsDefault, ArrayRef<Metadata *> Ops); 2409 ~DITemplateTypeParameter() = default; 2410 2411 static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name, 2412 DIType *Type, bool IsDefault, 2413 StorageType Storage, 2414 bool ShouldCreate = true) { 2415 return getImpl(Context, getCanonicalMDString(Context, Name), Type, 2416 IsDefault, Storage, ShouldCreate); 2417 } 2418 static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name, 2419 Metadata *Type, bool IsDefault, 2420 StorageType Storage, 2421 bool ShouldCreate = true); 2422 2423 TempDITemplateTypeParameter cloneImpl() const { 2424 return getTemporary(getContext(), getName(), getType(), isDefault()); 2425 } 2426 2427 public: 2428 DEFINE_MDNODE_GET(DITemplateTypeParameter, 2429 (StringRef Name, DIType *Type, bool IsDefault), 2430 (Name, Type, IsDefault)) 2431 DEFINE_MDNODE_GET(DITemplateTypeParameter, 2432 (MDString * Name, Metadata *Type, bool IsDefault), 2433 (Name, Type, IsDefault)) 2434 2435 TempDITemplateTypeParameter clone() const { return cloneImpl(); } 2436 2437 static bool classof(const Metadata *MD) { 2438 return MD->getMetadataID() == DITemplateTypeParameterKind; 2439 } 2440 }; 2441 2442 class DITemplateValueParameter : public DITemplateParameter { 2443 friend class LLVMContextImpl; 2444 friend class MDNode; 2445 2446 DITemplateValueParameter(LLVMContext &Context, StorageType Storage, 2447 unsigned Tag, bool IsDefault, 2448 ArrayRef<Metadata *> Ops) 2449 : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag, 2450 IsDefault, Ops) {} 2451 ~DITemplateValueParameter() = default; 2452 2453 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag, 2454 StringRef Name, DIType *Type, 2455 bool IsDefault, Metadata *Value, 2456 StorageType Storage, 2457 bool ShouldCreate = true) { 2458 return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type, 2459 IsDefault, Value, Storage, ShouldCreate); 2460 } 2461 static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag, 2462 MDString *Name, Metadata *Type, 2463 bool IsDefault, Metadata *Value, 2464 StorageType Storage, 2465 bool ShouldCreate = true); 2466 2467 TempDITemplateValueParameter cloneImpl() const { 2468 return getTemporary(getContext(), getTag(), getName(), getType(), 2469 isDefault(), getValue()); 2470 } 2471 2472 public: 2473 DEFINE_MDNODE_GET(DITemplateValueParameter, 2474 (unsigned Tag, StringRef Name, DIType *Type, bool IsDefault, 2475 Metadata *Value), 2476 (Tag, Name, Type, IsDefault, Value)) 2477 DEFINE_MDNODE_GET(DITemplateValueParameter, 2478 (unsigned Tag, MDString *Name, Metadata *Type, 2479 bool IsDefault, Metadata *Value), 2480 (Tag, Name, Type, IsDefault, Value)) 2481 2482 TempDITemplateValueParameter clone() const { return cloneImpl(); } 2483 2484 Metadata *getValue() const { return getOperand(2); } 2485 2486 static bool classof(const Metadata *MD) { 2487 return MD->getMetadataID() == DITemplateValueParameterKind; 2488 } 2489 }; 2490 2491 /// Base class for variables. 2492 class DIVariable : public DINode { 2493 unsigned Line; 2494 uint32_t AlignInBits; 2495 2496 protected: 2497 DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, signed Line, 2498 ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0); 2499 ~DIVariable() = default; 2500 2501 public: 2502 unsigned getLine() const { return Line; } 2503 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 2504 StringRef getName() const { return getStringOperand(1); } 2505 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 2506 DIType *getType() const { return cast_or_null<DIType>(getRawType()); } 2507 uint32_t getAlignInBits() const { return AlignInBits; } 2508 uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; } 2509 /// Determines the size of the variable's type. 2510 Optional<uint64_t> getSizeInBits() const; 2511 2512 /// Return the signedness of this variable's type, or None if this type is 2513 /// neither signed nor unsigned. 2514 Optional<DIBasicType::Signedness> getSignedness() const { 2515 if (auto *BT = dyn_cast<DIBasicType>(getType())) 2516 return BT->getSignedness(); 2517 return None; 2518 } 2519 2520 StringRef getFilename() const { 2521 if (auto *F = getFile()) 2522 return F->getFilename(); 2523 return ""; 2524 } 2525 2526 StringRef getDirectory() const { 2527 if (auto *F = getFile()) 2528 return F->getDirectory(); 2529 return ""; 2530 } 2531 2532 Optional<StringRef> getSource() const { 2533 if (auto *F = getFile()) 2534 return F->getSource(); 2535 return None; 2536 } 2537 2538 Metadata *getRawScope() const { return getOperand(0); } 2539 MDString *getRawName() const { return getOperandAs<MDString>(1); } 2540 Metadata *getRawFile() const { return getOperand(2); } 2541 Metadata *getRawType() const { return getOperand(3); } 2542 2543 static bool classof(const Metadata *MD) { 2544 return MD->getMetadataID() == DILocalVariableKind || 2545 MD->getMetadataID() == DIGlobalVariableKind; 2546 } 2547 }; 2548 2549 /// DWARF expression. 2550 /// 2551 /// This is (almost) a DWARF expression that modifies the location of a 2552 /// variable, or the location of a single piece of a variable, or (when using 2553 /// DW_OP_stack_value) is the constant variable value. 2554 /// 2555 /// TODO: Co-allocate the expression elements. 2556 /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary 2557 /// storage types. 2558 class DIExpression : public MDNode { 2559 friend class LLVMContextImpl; 2560 friend class MDNode; 2561 2562 std::vector<uint64_t> Elements; 2563 2564 DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements) 2565 : MDNode(C, DIExpressionKind, Storage, None), 2566 Elements(Elements.begin(), Elements.end()) {} 2567 ~DIExpression() = default; 2568 2569 static DIExpression *getImpl(LLVMContext &Context, 2570 ArrayRef<uint64_t> Elements, StorageType Storage, 2571 bool ShouldCreate = true); 2572 2573 TempDIExpression cloneImpl() const { 2574 return getTemporary(getContext(), getElements()); 2575 } 2576 2577 public: 2578 DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements)) 2579 2580 TempDIExpression clone() const { return cloneImpl(); } 2581 2582 ArrayRef<uint64_t> getElements() const { return Elements; } 2583 2584 unsigned getNumElements() const { return Elements.size(); } 2585 2586 uint64_t getElement(unsigned I) const { 2587 assert(I < Elements.size() && "Index out of range"); 2588 return Elements[I]; 2589 } 2590 2591 enum SignedOrUnsignedConstant { SignedConstant, UnsignedConstant }; 2592 /// Determine whether this represents a constant value, if so 2593 // return it's sign information. 2594 llvm::Optional<SignedOrUnsignedConstant> isConstant() const; 2595 2596 /// Return the number of unique location operands referred to (via 2597 /// DW_OP_LLVM_arg) in this expression; this is not necessarily the number of 2598 /// instances of DW_OP_LLVM_arg within the expression. 2599 /// For example, for the expression: 2600 /// (DW_OP_LLVM_arg 0, DW_OP_LLVM_arg 1, DW_OP_plus, 2601 /// DW_OP_LLVM_arg 0, DW_OP_mul) 2602 /// This function would return 2, as there are two unique location operands 2603 /// (0 and 1). 2604 uint64_t getNumLocationOperands() const; 2605 2606 using element_iterator = ArrayRef<uint64_t>::iterator; 2607 2608 element_iterator elements_begin() const { return getElements().begin(); } 2609 element_iterator elements_end() const { return getElements().end(); } 2610 2611 /// A lightweight wrapper around an expression operand. 2612 /// 2613 /// TODO: Store arguments directly and change \a DIExpression to store a 2614 /// range of these. 2615 class ExprOperand { 2616 const uint64_t *Op = nullptr; 2617 2618 public: 2619 ExprOperand() = default; 2620 explicit ExprOperand(const uint64_t *Op) : Op(Op) {} 2621 2622 const uint64_t *get() const { return Op; } 2623 2624 /// Get the operand code. 2625 uint64_t getOp() const { return *Op; } 2626 2627 /// Get an argument to the operand. 2628 /// 2629 /// Never returns the operand itself. 2630 uint64_t getArg(unsigned I) const { return Op[I + 1]; } 2631 2632 unsigned getNumArgs() const { return getSize() - 1; } 2633 2634 /// Return the size of the operand. 2635 /// 2636 /// Return the number of elements in the operand (1 + args). 2637 unsigned getSize() const; 2638 2639 /// Append the elements of this operand to \p V. 2640 void appendToVector(SmallVectorImpl<uint64_t> &V) const { 2641 V.append(get(), get() + getSize()); 2642 } 2643 }; 2644 2645 /// An iterator for expression operands. 2646 class expr_op_iterator { 2647 ExprOperand Op; 2648 2649 public: 2650 using iterator_category = std::input_iterator_tag; 2651 using value_type = ExprOperand; 2652 using difference_type = std::ptrdiff_t; 2653 using pointer = value_type *; 2654 using reference = value_type &; 2655 2656 expr_op_iterator() = default; 2657 explicit expr_op_iterator(element_iterator I) : Op(I) {} 2658 2659 element_iterator getBase() const { return Op.get(); } 2660 const ExprOperand &operator*() const { return Op; } 2661 const ExprOperand *operator->() const { return &Op; } 2662 2663 expr_op_iterator &operator++() { 2664 increment(); 2665 return *this; 2666 } 2667 expr_op_iterator operator++(int) { 2668 expr_op_iterator T(*this); 2669 increment(); 2670 return T; 2671 } 2672 2673 /// Get the next iterator. 2674 /// 2675 /// \a std::next() doesn't work because this is technically an 2676 /// input_iterator, but it's a perfectly valid operation. This is an 2677 /// accessor to provide the same functionality. 2678 expr_op_iterator getNext() const { return ++expr_op_iterator(*this); } 2679 2680 bool operator==(const expr_op_iterator &X) const { 2681 return getBase() == X.getBase(); 2682 } 2683 bool operator!=(const expr_op_iterator &X) const { 2684 return getBase() != X.getBase(); 2685 } 2686 2687 private: 2688 void increment() { Op = ExprOperand(getBase() + Op.getSize()); } 2689 }; 2690 2691 /// Visit the elements via ExprOperand wrappers. 2692 /// 2693 /// These range iterators visit elements through \a ExprOperand wrappers. 2694 /// This is not guaranteed to be a valid range unless \a isValid() gives \c 2695 /// true. 2696 /// 2697 /// \pre \a isValid() gives \c true. 2698 /// @{ 2699 expr_op_iterator expr_op_begin() const { 2700 return expr_op_iterator(elements_begin()); 2701 } 2702 expr_op_iterator expr_op_end() const { 2703 return expr_op_iterator(elements_end()); 2704 } 2705 iterator_range<expr_op_iterator> expr_ops() const { 2706 return {expr_op_begin(), expr_op_end()}; 2707 } 2708 /// @} 2709 2710 bool isValid() const; 2711 2712 static bool classof(const Metadata *MD) { 2713 return MD->getMetadataID() == DIExpressionKind; 2714 } 2715 2716 /// Return whether the first element a DW_OP_deref. 2717 bool startsWithDeref() const; 2718 2719 /// Holds the characteristics of one fragment of a larger variable. 2720 struct FragmentInfo { 2721 uint64_t SizeInBits; 2722 uint64_t OffsetInBits; 2723 }; 2724 2725 /// Retrieve the details of this fragment expression. 2726 static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start, 2727 expr_op_iterator End); 2728 2729 /// Retrieve the details of this fragment expression. 2730 Optional<FragmentInfo> getFragmentInfo() const { 2731 return getFragmentInfo(expr_op_begin(), expr_op_end()); 2732 } 2733 2734 /// Return whether this is a piece of an aggregate variable. 2735 bool isFragment() const { return getFragmentInfo().has_value(); } 2736 2737 /// Return whether this is an implicit location description. 2738 bool isImplicit() const; 2739 2740 /// Return whether the location is computed on the expression stack, meaning 2741 /// it cannot be a simple register location. 2742 bool isComplex() const; 2743 2744 /// Append \p Ops with operations to apply the \p Offset. 2745 static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset); 2746 2747 /// If this is a constant offset, extract it. If there is no expression, 2748 /// return true with an offset of zero. 2749 bool extractIfOffset(int64_t &Offset) const; 2750 2751 /// Returns true iff this DIExpression contains at least one instance of 2752 /// `DW_OP_LLVM_arg, n` for all n in [0, N). 2753 bool hasAllLocationOps(unsigned N) const; 2754 2755 /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF 2756 /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address 2757 /// Space>. 2758 static const DIExpression *extractAddressClass(const DIExpression *Expr, 2759 unsigned &AddrClass); 2760 2761 /// Used for DIExpression::prepend. 2762 enum PrependOps : uint8_t { 2763 ApplyOffset = 0, 2764 DerefBefore = 1 << 0, 2765 DerefAfter = 1 << 1, 2766 StackValue = 1 << 2, 2767 EntryValue = 1 << 3 2768 }; 2769 2770 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it 2771 /// into a stack value or/and an entry value. 2772 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags, 2773 int64_t Offset = 0); 2774 2775 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a 2776 /// stack value. 2777 static DIExpression *prependOpcodes(const DIExpression *Expr, 2778 SmallVectorImpl<uint64_t> &Ops, 2779 bool StackValue = false, 2780 bool EntryValue = false); 2781 2782 /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the 2783 /// returned expression is a stack value only if \p DIExpr is a stack value. 2784 /// If \p DIExpr describes a fragment, the returned expression will describe 2785 /// the same fragment. 2786 static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops); 2787 2788 /// Convert \p DIExpr into a stack value if it isn't one already by appending 2789 /// DW_OP_deref if needed, and appending \p Ops to the resulting expression. 2790 /// If \p DIExpr describes a fragment, the returned expression will describe 2791 /// the same fragment. 2792 static DIExpression *appendToStack(const DIExpression *Expr, 2793 ArrayRef<uint64_t> Ops); 2794 2795 /// Create a copy of \p Expr by appending the given list of \p Ops to each 2796 /// instance of the operand `DW_OP_LLVM_arg, \p ArgNo`. This is used to 2797 /// modify a specific location used by \p Expr, such as when salvaging that 2798 /// location. 2799 static DIExpression *appendOpsToArg(const DIExpression *Expr, 2800 ArrayRef<uint64_t> Ops, unsigned ArgNo, 2801 bool StackValue = false); 2802 2803 /// Create a copy of \p Expr with each instance of 2804 /// `DW_OP_LLVM_arg, \p OldArg` replaced with `DW_OP_LLVM_arg, \p NewArg`, 2805 /// and each instance of `DW_OP_LLVM_arg, Arg` with `DW_OP_LLVM_arg, Arg - 1` 2806 /// for all Arg > \p OldArg. 2807 /// This is used when replacing one of the operands of a debug value list 2808 /// with another operand in the same list and deleting the old operand. 2809 static DIExpression *replaceArg(const DIExpression *Expr, uint64_t OldArg, 2810 uint64_t NewArg); 2811 2812 /// Create a DIExpression to describe one part of an aggregate variable that 2813 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation 2814 /// will be appended to the elements of \c Expr. If \c Expr already contains 2815 /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset 2816 /// into the existing fragment. 2817 /// 2818 /// \param OffsetInBits Offset of the piece in bits. 2819 /// \param SizeInBits Size of the piece in bits. 2820 /// \return Creating a fragment expression may fail if \c Expr 2821 /// contains arithmetic operations that would be 2822 /// truncated. 2823 static Optional<DIExpression *> 2824 createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, 2825 unsigned SizeInBits); 2826 2827 /// Determine the relative position of the fragments passed in. 2828 /// Returns -1 if this is entirely before Other, 0 if this and Other overlap, 2829 /// 1 if this is entirely after Other. 2830 static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) { 2831 uint64_t l1 = A.OffsetInBits; 2832 uint64_t l2 = B.OffsetInBits; 2833 uint64_t r1 = l1 + A.SizeInBits; 2834 uint64_t r2 = l2 + B.SizeInBits; 2835 if (r1 <= l2) 2836 return -1; 2837 else if (r2 <= l1) 2838 return 1; 2839 else 2840 return 0; 2841 } 2842 2843 using ExtOps = std::array<uint64_t, 6>; 2844 2845 /// Returns the ops for a zero- or sign-extension in a DIExpression. 2846 static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed); 2847 2848 /// Append a zero- or sign-extension to \p Expr. Converts the expression to a 2849 /// stack value if it isn't one already. 2850 static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize, 2851 unsigned ToSize, bool Signed); 2852 2853 /// Check if fragments overlap between a pair of FragmentInfos. 2854 static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) { 2855 return fragmentCmp(A, B) == 0; 2856 } 2857 2858 /// Determine the relative position of the fragments described by this 2859 /// DIExpression and \p Other. Calls static fragmentCmp implementation. 2860 int fragmentCmp(const DIExpression *Other) const { 2861 auto Fragment1 = *getFragmentInfo(); 2862 auto Fragment2 = *Other->getFragmentInfo(); 2863 return fragmentCmp(Fragment1, Fragment2); 2864 } 2865 2866 /// Check if fragments overlap between this DIExpression and \p Other. 2867 bool fragmentsOverlap(const DIExpression *Other) const { 2868 if (!isFragment() || !Other->isFragment()) 2869 return true; 2870 return fragmentCmp(Other) == 0; 2871 } 2872 2873 /// Check if the expression consists of exactly one entry value operand. 2874 /// (This is the only configuration of entry values that is supported.) 2875 bool isEntryValue() const; 2876 2877 /// Try to shorten an expression with an initial constant operand. 2878 /// Returns a new expression and constant on success, or the original 2879 /// expression and constant on failure. 2880 std::pair<DIExpression *, const ConstantInt *> 2881 constantFold(const ConstantInt *CI); 2882 }; 2883 2884 inline bool operator==(const DIExpression::FragmentInfo &A, 2885 const DIExpression::FragmentInfo &B) { 2886 return std::tie(A.SizeInBits, A.OffsetInBits) == 2887 std::tie(B.SizeInBits, B.OffsetInBits); 2888 } 2889 2890 inline bool operator<(const DIExpression::FragmentInfo &A, 2891 const DIExpression::FragmentInfo &B) { 2892 return std::tie(A.SizeInBits, A.OffsetInBits) < 2893 std::tie(B.SizeInBits, B.OffsetInBits); 2894 } 2895 2896 template <> struct DenseMapInfo<DIExpression::FragmentInfo> { 2897 using FragInfo = DIExpression::FragmentInfo; 2898 static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max(); 2899 2900 static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; } 2901 2902 static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; } 2903 2904 static unsigned getHashValue(const FragInfo &Frag) { 2905 return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff); 2906 } 2907 2908 static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; } 2909 }; 2910 2911 /// Global variables. 2912 /// 2913 /// TODO: Remove DisplayName. It's always equal to Name. 2914 class DIGlobalVariable : public DIVariable { 2915 friend class LLVMContextImpl; 2916 friend class MDNode; 2917 2918 bool IsLocalToUnit; 2919 bool IsDefinition; 2920 2921 DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line, 2922 bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits, 2923 ArrayRef<Metadata *> Ops) 2924 : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits), 2925 IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {} 2926 ~DIGlobalVariable() = default; 2927 2928 static DIGlobalVariable * 2929 getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name, 2930 StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type, 2931 bool IsLocalToUnit, bool IsDefinition, 2932 DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams, 2933 uint32_t AlignInBits, DINodeArray Annotations, StorageType Storage, 2934 bool ShouldCreate = true) { 2935 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), 2936 getCanonicalMDString(Context, LinkageName), File, Line, Type, 2937 IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration, 2938 cast_or_null<Metadata>(TemplateParams), AlignInBits, 2939 Annotations.get(), Storage, ShouldCreate); 2940 } 2941 static DIGlobalVariable * 2942 getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 2943 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 2944 bool IsLocalToUnit, bool IsDefinition, 2945 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 2946 uint32_t AlignInBits, Metadata *Annotations, StorageType Storage, 2947 bool ShouldCreate = true); 2948 2949 TempDIGlobalVariable cloneImpl() const { 2950 return getTemporary(getContext(), getScope(), getName(), getLinkageName(), 2951 getFile(), getLine(), getType(), isLocalToUnit(), 2952 isDefinition(), getStaticDataMemberDeclaration(), 2953 getTemplateParams(), getAlignInBits(), 2954 getAnnotations()); 2955 } 2956 2957 public: 2958 DEFINE_MDNODE_GET( 2959 DIGlobalVariable, 2960 (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File, 2961 unsigned Line, DIType *Type, bool IsLocalToUnit, bool IsDefinition, 2962 DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams, 2963 uint32_t AlignInBits, DINodeArray Annotations), 2964 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition, 2965 StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations)) 2966 DEFINE_MDNODE_GET( 2967 DIGlobalVariable, 2968 (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File, 2969 unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition, 2970 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 2971 uint32_t AlignInBits, Metadata *Annotations), 2972 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition, 2973 StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations)) 2974 2975 TempDIGlobalVariable clone() const { return cloneImpl(); } 2976 2977 bool isLocalToUnit() const { return IsLocalToUnit; } 2978 bool isDefinition() const { return IsDefinition; } 2979 StringRef getDisplayName() const { return getStringOperand(4); } 2980 StringRef getLinkageName() const { return getStringOperand(5); } 2981 DIDerivedType *getStaticDataMemberDeclaration() const { 2982 return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration()); 2983 } 2984 DINodeArray getAnnotations() const { 2985 return cast_or_null<MDTuple>(getRawAnnotations()); 2986 } 2987 2988 MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); } 2989 Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); } 2990 Metadata *getRawTemplateParams() const { return getOperand(7); } 2991 MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); } 2992 Metadata *getRawAnnotations() const { return getOperand(8); } 2993 2994 static bool classof(const Metadata *MD) { 2995 return MD->getMetadataID() == DIGlobalVariableKind; 2996 } 2997 }; 2998 2999 class DICommonBlock : public DIScope { 3000 unsigned LineNo; 3001 3002 friend class LLVMContextImpl; 3003 friend class MDNode; 3004 3005 DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo, 3006 ArrayRef<Metadata *> Ops); 3007 3008 static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope, 3009 DIGlobalVariable *Decl, StringRef Name, 3010 DIFile *File, unsigned LineNo, 3011 StorageType Storage, bool ShouldCreate = true) { 3012 return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name), 3013 File, LineNo, Storage, ShouldCreate); 3014 } 3015 static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope, 3016 Metadata *Decl, MDString *Name, Metadata *File, 3017 unsigned LineNo, StorageType Storage, 3018 bool ShouldCreate = true); 3019 3020 TempDICommonBlock cloneImpl() const { 3021 return getTemporary(getContext(), getScope(), getDecl(), getName(), 3022 getFile(), getLineNo()); 3023 } 3024 3025 public: 3026 DEFINE_MDNODE_GET(DICommonBlock, 3027 (DIScope * Scope, DIGlobalVariable *Decl, StringRef Name, 3028 DIFile *File, unsigned LineNo), 3029 (Scope, Decl, Name, File, LineNo)) 3030 DEFINE_MDNODE_GET(DICommonBlock, 3031 (Metadata * Scope, Metadata *Decl, MDString *Name, 3032 Metadata *File, unsigned LineNo), 3033 (Scope, Decl, Name, File, LineNo)) 3034 3035 TempDICommonBlock clone() const { return cloneImpl(); } 3036 3037 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 3038 DIGlobalVariable *getDecl() const { 3039 return cast_or_null<DIGlobalVariable>(getRawDecl()); 3040 } 3041 StringRef getName() const { return getStringOperand(2); } 3042 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3043 unsigned getLineNo() const { return LineNo; } 3044 3045 Metadata *getRawScope() const { return getOperand(0); } 3046 Metadata *getRawDecl() const { return getOperand(1); } 3047 MDString *getRawName() const { return getOperandAs<MDString>(2); } 3048 Metadata *getRawFile() const { return getOperand(3); } 3049 3050 static bool classof(const Metadata *MD) { 3051 return MD->getMetadataID() == DICommonBlockKind; 3052 } 3053 }; 3054 3055 /// Local variable. 3056 /// 3057 /// TODO: Split up flags. 3058 class DILocalVariable : public DIVariable { 3059 friend class LLVMContextImpl; 3060 friend class MDNode; 3061 3062 unsigned Arg : 16; 3063 DIFlags Flags; 3064 3065 DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line, 3066 unsigned Arg, DIFlags Flags, uint32_t AlignInBits, 3067 ArrayRef<Metadata *> Ops) 3068 : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits), 3069 Arg(Arg), Flags(Flags) { 3070 assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range"); 3071 } 3072 ~DILocalVariable() = default; 3073 3074 static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope, 3075 StringRef Name, DIFile *File, unsigned Line, 3076 DIType *Type, unsigned Arg, DIFlags Flags, 3077 uint32_t AlignInBits, DINodeArray Annotations, 3078 StorageType Storage, 3079 bool ShouldCreate = true) { 3080 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File, 3081 Line, Type, Arg, Flags, AlignInBits, Annotations.get(), 3082 Storage, ShouldCreate); 3083 } 3084 static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope, 3085 MDString *Name, Metadata *File, unsigned Line, 3086 Metadata *Type, unsigned Arg, DIFlags Flags, 3087 uint32_t AlignInBits, Metadata *Annotations, 3088 StorageType Storage, 3089 bool ShouldCreate = true); 3090 3091 TempDILocalVariable cloneImpl() const { 3092 return getTemporary(getContext(), getScope(), getName(), getFile(), 3093 getLine(), getType(), getArg(), getFlags(), 3094 getAlignInBits(), getAnnotations()); 3095 } 3096 3097 public: 3098 DEFINE_MDNODE_GET(DILocalVariable, 3099 (DILocalScope * Scope, StringRef Name, DIFile *File, 3100 unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags, 3101 uint32_t AlignInBits, DINodeArray Annotations), 3102 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits, 3103 Annotations)) 3104 DEFINE_MDNODE_GET(DILocalVariable, 3105 (Metadata * Scope, MDString *Name, Metadata *File, 3106 unsigned Line, Metadata *Type, unsigned Arg, DIFlags Flags, 3107 uint32_t AlignInBits, Metadata *Annotations), 3108 (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits, 3109 Annotations)) 3110 3111 TempDILocalVariable clone() const { return cloneImpl(); } 3112 3113 /// Get the local scope for this variable. 3114 /// 3115 /// Variables must be defined in a local scope. 3116 DILocalScope *getScope() const { 3117 return cast<DILocalScope>(DIVariable::getScope()); 3118 } 3119 3120 bool isParameter() const { return Arg; } 3121 unsigned getArg() const { return Arg; } 3122 DIFlags getFlags() const { return Flags; } 3123 3124 DINodeArray getAnnotations() const { 3125 return cast_or_null<MDTuple>(getRawAnnotations()); 3126 } 3127 Metadata *getRawAnnotations() const { return getOperand(4); } 3128 3129 bool isArtificial() const { return getFlags() & FlagArtificial; } 3130 bool isObjectPointer() const { return getFlags() & FlagObjectPointer; } 3131 3132 /// Check that a location is valid for this variable. 3133 /// 3134 /// Check that \c DL exists, is in the same subprogram, and has the same 3135 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment 3136 /// to a \a DbgInfoIntrinsic.) 3137 bool isValidLocationForIntrinsic(const DILocation *DL) const { 3138 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram(); 3139 } 3140 3141 static bool classof(const Metadata *MD) { 3142 return MD->getMetadataID() == DILocalVariableKind; 3143 } 3144 }; 3145 3146 /// Label. 3147 /// 3148 class DILabel : public DINode { 3149 friend class LLVMContextImpl; 3150 friend class MDNode; 3151 3152 unsigned Line; 3153 3154 DILabel(LLVMContext &C, StorageType Storage, unsigned Line, 3155 ArrayRef<Metadata *> Ops); 3156 ~DILabel() = default; 3157 3158 static DILabel *getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name, 3159 DIFile *File, unsigned Line, StorageType Storage, 3160 bool ShouldCreate = true) { 3161 return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File, 3162 Line, Storage, ShouldCreate); 3163 } 3164 static DILabel *getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 3165 Metadata *File, unsigned Line, StorageType Storage, 3166 bool ShouldCreate = true); 3167 3168 TempDILabel cloneImpl() const { 3169 return getTemporary(getContext(), getScope(), getName(), getFile(), 3170 getLine()); 3171 } 3172 3173 public: 3174 DEFINE_MDNODE_GET(DILabel, 3175 (DILocalScope * Scope, StringRef Name, DIFile *File, 3176 unsigned Line), 3177 (Scope, Name, File, Line)) 3178 DEFINE_MDNODE_GET(DILabel, 3179 (Metadata * Scope, MDString *Name, Metadata *File, 3180 unsigned Line), 3181 (Scope, Name, File, Line)) 3182 3183 TempDILabel clone() const { return cloneImpl(); } 3184 3185 /// Get the local scope for this label. 3186 /// 3187 /// Labels must be defined in a local scope. 3188 DILocalScope *getScope() const { 3189 return cast_or_null<DILocalScope>(getRawScope()); 3190 } 3191 unsigned getLine() const { return Line; } 3192 StringRef getName() const { return getStringOperand(1); } 3193 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3194 3195 Metadata *getRawScope() const { return getOperand(0); } 3196 MDString *getRawName() const { return getOperandAs<MDString>(1); } 3197 Metadata *getRawFile() const { return getOperand(2); } 3198 3199 /// Check that a location is valid for this label. 3200 /// 3201 /// Check that \c DL exists, is in the same subprogram, and has the same 3202 /// inlined-at location as \c this. (Otherwise, it's not a valid attachment 3203 /// to a \a DbgInfoIntrinsic.) 3204 bool isValidLocationForIntrinsic(const DILocation *DL) const { 3205 return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram(); 3206 } 3207 3208 static bool classof(const Metadata *MD) { 3209 return MD->getMetadataID() == DILabelKind; 3210 } 3211 }; 3212 3213 class DIObjCProperty : public DINode { 3214 friend class LLVMContextImpl; 3215 friend class MDNode; 3216 3217 unsigned Line; 3218 unsigned Attributes; 3219 3220 DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line, 3221 unsigned Attributes, ArrayRef<Metadata *> Ops); 3222 ~DIObjCProperty() = default; 3223 3224 static DIObjCProperty * 3225 getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line, 3226 StringRef GetterName, StringRef SetterName, unsigned Attributes, 3227 DIType *Type, StorageType Storage, bool ShouldCreate = true) { 3228 return getImpl(Context, getCanonicalMDString(Context, Name), File, Line, 3229 getCanonicalMDString(Context, GetterName), 3230 getCanonicalMDString(Context, SetterName), Attributes, Type, 3231 Storage, ShouldCreate); 3232 } 3233 static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name, 3234 Metadata *File, unsigned Line, 3235 MDString *GetterName, MDString *SetterName, 3236 unsigned Attributes, Metadata *Type, 3237 StorageType Storage, bool ShouldCreate = true); 3238 3239 TempDIObjCProperty cloneImpl() const { 3240 return getTemporary(getContext(), getName(), getFile(), getLine(), 3241 getGetterName(), getSetterName(), getAttributes(), 3242 getType()); 3243 } 3244 3245 public: 3246 DEFINE_MDNODE_GET(DIObjCProperty, 3247 (StringRef Name, DIFile *File, unsigned Line, 3248 StringRef GetterName, StringRef SetterName, 3249 unsigned Attributes, DIType *Type), 3250 (Name, File, Line, GetterName, SetterName, Attributes, 3251 Type)) 3252 DEFINE_MDNODE_GET(DIObjCProperty, 3253 (MDString * Name, Metadata *File, unsigned Line, 3254 MDString *GetterName, MDString *SetterName, 3255 unsigned Attributes, Metadata *Type), 3256 (Name, File, Line, GetterName, SetterName, Attributes, 3257 Type)) 3258 3259 TempDIObjCProperty clone() const { return cloneImpl(); } 3260 3261 unsigned getLine() const { return Line; } 3262 unsigned getAttributes() const { return Attributes; } 3263 StringRef getName() const { return getStringOperand(0); } 3264 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3265 StringRef getGetterName() const { return getStringOperand(2); } 3266 StringRef getSetterName() const { return getStringOperand(3); } 3267 DIType *getType() const { return cast_or_null<DIType>(getRawType()); } 3268 3269 StringRef getFilename() const { 3270 if (auto *F = getFile()) 3271 return F->getFilename(); 3272 return ""; 3273 } 3274 3275 StringRef getDirectory() const { 3276 if (auto *F = getFile()) 3277 return F->getDirectory(); 3278 return ""; 3279 } 3280 3281 MDString *getRawName() const { return getOperandAs<MDString>(0); } 3282 Metadata *getRawFile() const { return getOperand(1); } 3283 MDString *getRawGetterName() const { return getOperandAs<MDString>(2); } 3284 MDString *getRawSetterName() const { return getOperandAs<MDString>(3); } 3285 Metadata *getRawType() const { return getOperand(4); } 3286 3287 static bool classof(const Metadata *MD) { 3288 return MD->getMetadataID() == DIObjCPropertyKind; 3289 } 3290 }; 3291 3292 /// An imported module (C++ using directive or similar). 3293 class DIImportedEntity : public DINode { 3294 friend class LLVMContextImpl; 3295 friend class MDNode; 3296 3297 unsigned Line; 3298 3299 DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag, 3300 unsigned Line, ArrayRef<Metadata *> Ops) 3301 : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {} 3302 ~DIImportedEntity() = default; 3303 3304 static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag, 3305 DIScope *Scope, DINode *Entity, DIFile *File, 3306 unsigned Line, StringRef Name, 3307 DINodeArray Elements, StorageType Storage, 3308 bool ShouldCreate = true) { 3309 return getImpl(Context, Tag, Scope, Entity, File, Line, 3310 getCanonicalMDString(Context, Name), Elements.get(), Storage, 3311 ShouldCreate); 3312 } 3313 static DIImportedEntity * 3314 getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity, 3315 Metadata *File, unsigned Line, MDString *Name, Metadata *Elements, 3316 StorageType Storage, bool ShouldCreate = true); 3317 3318 TempDIImportedEntity cloneImpl() const { 3319 return getTemporary(getContext(), getTag(), getScope(), getEntity(), 3320 getFile(), getLine(), getName(), getElements()); 3321 } 3322 3323 public: 3324 DEFINE_MDNODE_GET(DIImportedEntity, 3325 (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File, 3326 unsigned Line, StringRef Name = "", 3327 DINodeArray Elements = nullptr), 3328 (Tag, Scope, Entity, File, Line, Name, Elements)) 3329 DEFINE_MDNODE_GET(DIImportedEntity, 3330 (unsigned Tag, Metadata *Scope, Metadata *Entity, 3331 Metadata *File, unsigned Line, MDString *Name, 3332 Metadata *Elements = nullptr), 3333 (Tag, Scope, Entity, File, Line, Name, Elements)) 3334 3335 TempDIImportedEntity clone() const { return cloneImpl(); } 3336 3337 unsigned getLine() const { return Line; } 3338 DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); } 3339 DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); } 3340 StringRef getName() const { return getStringOperand(2); } 3341 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3342 DINodeArray getElements() const { 3343 return cast_or_null<MDTuple>(getRawElements()); 3344 } 3345 3346 Metadata *getRawScope() const { return getOperand(0); } 3347 Metadata *getRawEntity() const { return getOperand(1); } 3348 MDString *getRawName() const { return getOperandAs<MDString>(2); } 3349 Metadata *getRawFile() const { return getOperand(3); } 3350 Metadata *getRawElements() const { return getOperand(4); } 3351 3352 static bool classof(const Metadata *MD) { 3353 return MD->getMetadataID() == DIImportedEntityKind; 3354 } 3355 }; 3356 3357 /// A pair of DIGlobalVariable and DIExpression. 3358 class DIGlobalVariableExpression : public MDNode { 3359 friend class LLVMContextImpl; 3360 friend class MDNode; 3361 3362 DIGlobalVariableExpression(LLVMContext &C, StorageType Storage, 3363 ArrayRef<Metadata *> Ops) 3364 : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {} 3365 ~DIGlobalVariableExpression() = default; 3366 3367 static DIGlobalVariableExpression * 3368 getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression, 3369 StorageType Storage, bool ShouldCreate = true); 3370 3371 TempDIGlobalVariableExpression cloneImpl() const { 3372 return getTemporary(getContext(), getVariable(), getExpression()); 3373 } 3374 3375 public: 3376 DEFINE_MDNODE_GET(DIGlobalVariableExpression, 3377 (Metadata * Variable, Metadata *Expression), 3378 (Variable, Expression)) 3379 3380 TempDIGlobalVariableExpression clone() const { return cloneImpl(); } 3381 3382 Metadata *getRawVariable() const { return getOperand(0); } 3383 3384 DIGlobalVariable *getVariable() const { 3385 return cast_or_null<DIGlobalVariable>(getRawVariable()); 3386 } 3387 3388 Metadata *getRawExpression() const { return getOperand(1); } 3389 3390 DIExpression *getExpression() const { 3391 return cast<DIExpression>(getRawExpression()); 3392 } 3393 3394 static bool classof(const Metadata *MD) { 3395 return MD->getMetadataID() == DIGlobalVariableExpressionKind; 3396 } 3397 }; 3398 3399 /// Macro Info DWARF-like metadata node. 3400 /// 3401 /// A metadata node with a DWARF macro info (i.e., a constant named 3402 /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a 3403 /// DIMacroNode 3404 /// because it's potentially used for non-DWARF output. 3405 class DIMacroNode : public MDNode { 3406 friend class LLVMContextImpl; 3407 friend class MDNode; 3408 3409 protected: 3410 DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType, 3411 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None) 3412 : MDNode(C, ID, Storage, Ops1, Ops2) { 3413 assert(MIType < 1u << 16); 3414 SubclassData16 = MIType; 3415 } 3416 ~DIMacroNode() = default; 3417 3418 template <class Ty> Ty *getOperandAs(unsigned I) const { 3419 return cast_or_null<Ty>(getOperand(I)); 3420 } 3421 3422 StringRef getStringOperand(unsigned I) const { 3423 if (auto *S = getOperandAs<MDString>(I)) 3424 return S->getString(); 3425 return StringRef(); 3426 } 3427 3428 static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) { 3429 if (S.empty()) 3430 return nullptr; 3431 return MDString::get(Context, S); 3432 } 3433 3434 public: 3435 unsigned getMacinfoType() const { return SubclassData16; } 3436 3437 static bool classof(const Metadata *MD) { 3438 switch (MD->getMetadataID()) { 3439 default: 3440 return false; 3441 case DIMacroKind: 3442 case DIMacroFileKind: 3443 return true; 3444 } 3445 } 3446 }; 3447 3448 class DIMacro : public DIMacroNode { 3449 friend class LLVMContextImpl; 3450 friend class MDNode; 3451 3452 unsigned Line; 3453 3454 DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line, 3455 ArrayRef<Metadata *> Ops) 3456 : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {} 3457 ~DIMacro() = default; 3458 3459 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line, 3460 StringRef Name, StringRef Value, StorageType Storage, 3461 bool ShouldCreate = true) { 3462 return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name), 3463 getCanonicalMDString(Context, Value), Storage, ShouldCreate); 3464 } 3465 static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line, 3466 MDString *Name, MDString *Value, StorageType Storage, 3467 bool ShouldCreate = true); 3468 3469 TempDIMacro cloneImpl() const { 3470 return getTemporary(getContext(), getMacinfoType(), getLine(), getName(), 3471 getValue()); 3472 } 3473 3474 public: 3475 DEFINE_MDNODE_GET(DIMacro, 3476 (unsigned MIType, unsigned Line, StringRef Name, 3477 StringRef Value = ""), 3478 (MIType, Line, Name, Value)) 3479 DEFINE_MDNODE_GET(DIMacro, 3480 (unsigned MIType, unsigned Line, MDString *Name, 3481 MDString *Value), 3482 (MIType, Line, Name, Value)) 3483 3484 TempDIMacro clone() const { return cloneImpl(); } 3485 3486 unsigned getLine() const { return Line; } 3487 3488 StringRef getName() const { return getStringOperand(0); } 3489 StringRef getValue() const { return getStringOperand(1); } 3490 3491 MDString *getRawName() const { return getOperandAs<MDString>(0); } 3492 MDString *getRawValue() const { return getOperandAs<MDString>(1); } 3493 3494 static bool classof(const Metadata *MD) { 3495 return MD->getMetadataID() == DIMacroKind; 3496 } 3497 }; 3498 3499 class DIMacroFile : public DIMacroNode { 3500 friend class LLVMContextImpl; 3501 friend class MDNode; 3502 3503 unsigned Line; 3504 3505 DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType, 3506 unsigned Line, ArrayRef<Metadata *> Ops) 3507 : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {} 3508 ~DIMacroFile() = default; 3509 3510 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType, 3511 unsigned Line, DIFile *File, 3512 DIMacroNodeArray Elements, StorageType Storage, 3513 bool ShouldCreate = true) { 3514 return getImpl(Context, MIType, Line, static_cast<Metadata *>(File), 3515 Elements.get(), Storage, ShouldCreate); 3516 } 3517 3518 static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType, 3519 unsigned Line, Metadata *File, Metadata *Elements, 3520 StorageType Storage, bool ShouldCreate = true); 3521 3522 TempDIMacroFile cloneImpl() const { 3523 return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(), 3524 getElements()); 3525 } 3526 3527 public: 3528 DEFINE_MDNODE_GET(DIMacroFile, 3529 (unsigned MIType, unsigned Line, DIFile *File, 3530 DIMacroNodeArray Elements), 3531 (MIType, Line, File, Elements)) 3532 DEFINE_MDNODE_GET(DIMacroFile, 3533 (unsigned MIType, unsigned Line, Metadata *File, 3534 Metadata *Elements), 3535 (MIType, Line, File, Elements)) 3536 3537 TempDIMacroFile clone() const { return cloneImpl(); } 3538 3539 void replaceElements(DIMacroNodeArray Elements) { 3540 #ifndef NDEBUG 3541 for (DIMacroNode *Op : getElements()) 3542 assert(is_contained(Elements->operands(), Op) && 3543 "Lost a macro node during macro node list replacement"); 3544 #endif 3545 replaceOperandWith(1, Elements.get()); 3546 } 3547 3548 unsigned getLine() const { return Line; } 3549 DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); } 3550 3551 DIMacroNodeArray getElements() const { 3552 return cast_or_null<MDTuple>(getRawElements()); 3553 } 3554 3555 Metadata *getRawFile() const { return getOperand(0); } 3556 Metadata *getRawElements() const { return getOperand(1); } 3557 3558 static bool classof(const Metadata *MD) { 3559 return MD->getMetadataID() == DIMacroFileKind; 3560 } 3561 }; 3562 3563 /// List of ValueAsMetadata, to be used as an argument to a dbg.value 3564 /// intrinsic. 3565 class DIArgList : public MDNode { 3566 friend class LLVMContextImpl; 3567 friend class MDNode; 3568 using iterator = SmallVectorImpl<ValueAsMetadata *>::iterator; 3569 3570 SmallVector<ValueAsMetadata *, 4> Args; 3571 3572 DIArgList(LLVMContext &C, StorageType Storage, 3573 ArrayRef<ValueAsMetadata *> Args) 3574 : MDNode(C, DIArgListKind, Storage, None), 3575 Args(Args.begin(), Args.end()) { 3576 track(); 3577 } 3578 ~DIArgList() { untrack(); } 3579 3580 static DIArgList *getImpl(LLVMContext &Context, 3581 ArrayRef<ValueAsMetadata *> Args, 3582 StorageType Storage, bool ShouldCreate = true); 3583 3584 TempDIArgList cloneImpl() const { 3585 return getTemporary(getContext(), getArgs()); 3586 } 3587 3588 void track(); 3589 void untrack(); 3590 void dropAllReferences(); 3591 3592 public: 3593 DEFINE_MDNODE_GET(DIArgList, (ArrayRef<ValueAsMetadata *> Args), (Args)) 3594 3595 TempDIArgList clone() const { return cloneImpl(); } 3596 3597 ArrayRef<ValueAsMetadata *> getArgs() const { return Args; } 3598 3599 iterator args_begin() { return Args.begin(); } 3600 iterator args_end() { return Args.end(); } 3601 3602 static bool classof(const Metadata *MD) { 3603 return MD->getMetadataID() == DIArgListKind; 3604 } 3605 3606 void handleChangedOperand(void *Ref, Metadata *New); 3607 }; 3608 3609 /// Identifies a unique instance of a variable. 3610 /// 3611 /// Storage for identifying a potentially inlined instance of a variable, 3612 /// or a fragment thereof. This guarantees that exactly one variable instance 3613 /// may be identified by this class, even when that variable is a fragment of 3614 /// an aggregate variable and/or there is another inlined instance of the same 3615 /// source code variable nearby. 3616 /// This class does not necessarily uniquely identify that variable: it is 3617 /// possible that a DebugVariable with different parameters may point to the 3618 /// same variable instance, but not that one DebugVariable points to multiple 3619 /// variable instances. 3620 class DebugVariable { 3621 using FragmentInfo = DIExpression::FragmentInfo; 3622 3623 const DILocalVariable *Variable; 3624 Optional<FragmentInfo> Fragment; 3625 const DILocation *InlinedAt; 3626 3627 /// Fragment that will overlap all other fragments. Used as default when 3628 /// caller demands a fragment. 3629 static const FragmentInfo DefaultFragment; 3630 3631 public: 3632 DebugVariable(const DILocalVariable *Var, Optional<FragmentInfo> FragmentInfo, 3633 const DILocation *InlinedAt) 3634 : Variable(Var), Fragment(FragmentInfo), InlinedAt(InlinedAt) {} 3635 3636 DebugVariable(const DILocalVariable *Var, const DIExpression *DIExpr, 3637 const DILocation *InlinedAt) 3638 : Variable(Var), 3639 Fragment(DIExpr ? DIExpr->getFragmentInfo() : NoneType()), 3640 InlinedAt(InlinedAt) {} 3641 3642 const DILocalVariable *getVariable() const { return Variable; } 3643 Optional<FragmentInfo> getFragment() const { return Fragment; } 3644 const DILocation *getInlinedAt() const { return InlinedAt; } 3645 3646 FragmentInfo getFragmentOrDefault() const { 3647 return Fragment.value_or(DefaultFragment); 3648 } 3649 3650 static bool isDefaultFragment(const FragmentInfo F) { 3651 return F == DefaultFragment; 3652 } 3653 3654 bool operator==(const DebugVariable &Other) const { 3655 return std::tie(Variable, Fragment, InlinedAt) == 3656 std::tie(Other.Variable, Other.Fragment, Other.InlinedAt); 3657 } 3658 3659 bool operator<(const DebugVariable &Other) const { 3660 return std::tie(Variable, Fragment, InlinedAt) < 3661 std::tie(Other.Variable, Other.Fragment, Other.InlinedAt); 3662 } 3663 }; 3664 3665 template <> struct DenseMapInfo<DebugVariable> { 3666 using FragmentInfo = DIExpression::FragmentInfo; 3667 3668 /// Empty key: no key should be generated that has no DILocalVariable. 3669 static inline DebugVariable getEmptyKey() { 3670 return DebugVariable(nullptr, NoneType(), nullptr); 3671 } 3672 3673 /// Difference in tombstone is that the Optional is meaningful. 3674 static inline DebugVariable getTombstoneKey() { 3675 return DebugVariable(nullptr, {{0, 0}}, nullptr); 3676 } 3677 3678 static unsigned getHashValue(const DebugVariable &D) { 3679 unsigned HV = 0; 3680 const Optional<FragmentInfo> Fragment = D.getFragment(); 3681 if (Fragment) 3682 HV = DenseMapInfo<FragmentInfo>::getHashValue(*Fragment); 3683 3684 return hash_combine(D.getVariable(), HV, D.getInlinedAt()); 3685 } 3686 3687 static bool isEqual(const DebugVariable &A, const DebugVariable &B) { 3688 return A == B; 3689 } 3690 }; 3691 3692 } // end namespace llvm 3693 3694 #undef DEFINE_MDNODE_GET_UNPACK_IMPL 3695 #undef DEFINE_MDNODE_GET_UNPACK 3696 #undef DEFINE_MDNODE_GET 3697 3698 #endif // LLVM_IR_DEBUGINFOMETADATA_H 3699