1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
49 class DILexicalBlockFile;
55 /// Maps from type identifier to the actual MDNode.
56 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
58 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
59 /// This should not be stored in a container, because the underlying MDNode
60 /// may change in certain situations.
62 // Befriends DIRef so DIRef can befriend the protected member
63 // function: getFieldAs<DIRef>.
64 template <typename T> friend class DIRef;
67 /// The three accessibility flags are mutually exclusive and rolled
68 /// together in the first two bits.
70 FlagAccessibility = 1 << 0 | 1 << 1,
76 FlagAppleBlock = 1 << 3,
77 FlagBlockByrefStruct = 1 << 4,
79 FlagArtificial = 1 << 6,
80 FlagExplicit = 1 << 7,
81 FlagPrototyped = 1 << 8,
82 FlagObjcClassComplete = 1 << 9,
83 FlagObjectPointer = 1 << 10,
85 FlagStaticMember = 1 << 12,
86 FlagIndirectVariable = 1 << 13,
87 FlagLValueReference = 1 << 14,
88 FlagRValueReference = 1 << 15
92 const MDNode *DbgNode;
94 StringRef getStringField(unsigned Elt) const;
95 unsigned getUnsignedField(unsigned Elt) const {
96 return (unsigned)getUInt64Field(Elt);
98 uint64_t getUInt64Field(unsigned Elt) const;
99 int64_t getInt64Field(unsigned Elt) const;
100 DIDescriptor getDescriptorField(unsigned Elt) const;
102 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
103 return DescTy(getDescriptorField(Elt));
106 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
107 Constant *getConstantField(unsigned Elt) const;
108 Function *getFunctionField(unsigned Elt) const;
109 void replaceFunctionField(unsigned Elt, Function *F);
112 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
116 operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
117 MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
119 // An explicit operator bool so that we can do testing of DI values
121 // FIXME: This operator bool isn't actually protecting anything at the
122 // moment due to the conversion operator above making DIDescriptor nodes
123 // implicitly convertable to bool.
124 LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
126 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
127 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
129 uint16_t getTag() const {
130 return getUnsignedField(0) & ~LLVMDebugVersionMask;
133 bool isDerivedType() const;
134 bool isCompositeType() const;
135 bool isSubroutineType() const;
136 bool isBasicType() const;
137 bool isVariable() const;
138 bool isSubprogram() const;
139 bool isGlobalVariable() const;
140 bool isScope() const;
142 bool isCompileUnit() const;
143 bool isNameSpace() const;
144 bool isLexicalBlockFile() const;
145 bool isLexicalBlock() const;
146 bool isSubrange() const;
147 bool isEnumerator() const;
149 bool isTemplateTypeParameter() const;
150 bool isTemplateValueParameter() const;
151 bool isObjCProperty() const;
152 bool isImportedEntity() const;
153 bool isExpression() const;
155 /// print - print descriptor.
156 void print(raw_ostream &OS) const;
158 /// dump - print descriptor to dbgs() with a newline.
161 /// replaceAllUsesWith - Replace all uses of debug info referenced by
163 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
164 void replaceAllUsesWith(MDNode *D);
167 /// DISubrange - This is used to represent ranges, for array bounds.
168 class DISubrange : public DIDescriptor {
169 friend class DIDescriptor;
170 void printInternal(raw_ostream &OS) const;
173 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
175 int64_t getLo() const { return getInt64Field(1); }
176 int64_t getCount() const { return getInt64Field(2); }
180 /// DITypedArray - This descriptor holds an array of nodes with type T.
181 template <typename T> class DITypedArray : public DIDescriptor {
183 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
184 unsigned getNumElements() const {
185 return DbgNode ? DbgNode->getNumOperands() : 0;
187 T getElement(unsigned Idx) const {
188 return getFieldAs<T>(Idx);
192 typedef DITypedArray<DIDescriptor> DIArray;
194 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
195 /// FIXME: it seems strange that this doesn't have either a reference to the
196 /// type/precision or a file/line pair for location info.
197 class DIEnumerator : public DIDescriptor {
198 friend class DIDescriptor;
199 void printInternal(raw_ostream &OS) const;
202 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
204 StringRef getName() const { return getStringField(1); }
205 int64_t getEnumValue() const { return getInt64Field(2); }
209 template <typename T> class DIRef;
210 typedef DIRef<DIScope> DIScopeRef;
211 typedef DIRef<DIType> DITypeRef;
212 typedef DITypedArray<DITypeRef> DITypeArray;
214 /// DIScope - A base class for various scopes.
216 /// Although, implementation-wise, DIScope is the parent class of most
217 /// other DIxxx classes, including DIType and its descendants, most of
218 /// DIScope's descendants are not a substitutable subtype of
219 /// DIScope. The DIDescriptor::isScope() method only is true for
220 /// DIScopes that are scopes in the strict lexical scope sense
221 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
222 class DIScope : public DIDescriptor {
224 friend class DIDescriptor;
225 void printInternal(raw_ostream &OS) const;
228 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
230 /// Gets the parent scope for this scope node or returns a
231 /// default constructed scope.
232 DIScopeRef getContext() const;
233 /// If the scope node has a name, return that, else return an empty string.
234 StringRef getName() const;
235 StringRef getFilename() const;
236 StringRef getDirectory() const;
238 /// Generate a reference to this DIScope. Uses the type identifier instead
239 /// of the actual MDNode if possible, to help type uniquing.
240 DIScopeRef getRef() const;
243 /// Represents reference to a DIDescriptor, abstracts over direct and
244 /// identifier-based metadata references.
245 template <typename T> class DIRef {
246 template <typename DescTy>
247 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
248 friend DIScopeRef DIScope::getContext() const;
249 friend DIScopeRef DIScope::getRef() const;
252 /// Val can be either a MDNode or a MDString, in the latter,
253 /// MDString specifies the type identifier.
255 explicit DIRef(const Value *V);
258 T resolve(const DITypeIdentifierMap &Map) const;
259 StringRef getName() const;
260 operator Value *() const { return const_cast<Value *>(Val); }
263 template <typename T>
264 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
268 if (const MDNode *MD = dyn_cast<MDNode>(Val))
271 const MDString *MS = cast<MDString>(Val);
272 // Find the corresponding MDNode.
273 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
274 assert(Iter != Map.end() && "Identifier not in the type map?");
275 assert(DIDescriptor(Iter->second).isType() &&
276 "MDNode in DITypeIdentifierMap should be a DIType.");
277 return T(Iter->second);
280 template <typename T> StringRef DIRef<T>::getName() const {
284 if (const MDNode *MD = dyn_cast<MDNode>(Val))
285 return T(MD).getName();
287 const MDString *MS = cast<MDString>(Val);
288 return MS->getString();
291 /// Specialize getFieldAs to handle fields that are references to DIScopes.
292 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
293 /// Specialize DIRef constructor for DIScopeRef.
294 template <> DIRef<DIScope>::DIRef(const Value *V);
296 /// Specialize getFieldAs to handle fields that are references to DITypes.
297 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
298 /// Specialize DIRef constructor for DITypeRef.
299 template <> DIRef<DIType>::DIRef(const Value *V);
301 /// DIType - This is a wrapper for a type.
302 /// FIXME: Types should be factored much better so that CV qualifiers and
303 /// others do not require a huge and empty descriptor full of zeros.
304 class DIType : public DIScope {
306 friend class DIDescriptor;
307 void printInternal(raw_ostream &OS) const;
310 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
311 operator DITypeRef () const {
313 "constructing DITypeRef from an MDNode that is not a type");
314 return DITypeRef(&*getRef());
317 /// Verify - Verify that a type descriptor is well formed.
320 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
321 StringRef getName() const { return getStringField(3); }
322 unsigned getLineNumber() const { return getUnsignedField(4); }
323 uint64_t getSizeInBits() const { return getUInt64Field(5); }
324 uint64_t getAlignInBits() const { return getUInt64Field(6); }
325 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
326 // carry this is just plain insane.
327 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
328 unsigned getFlags() const { return getUnsignedField(8); }
329 bool isPrivate() const {
330 return (getFlags() & FlagAccessibility) == FlagPrivate;
332 bool isProtected() const {
333 return (getFlags() & FlagAccessibility) == FlagProtected;
335 bool isPublic() const {
336 return (getFlags() & FlagAccessibility) == FlagPublic;
338 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
339 // isAppleBlock - Return true if this is the Apple Blocks extension.
340 bool isAppleBlockExtension() const {
341 return (getFlags() & FlagAppleBlock) != 0;
343 bool isBlockByrefStruct() const {
344 return (getFlags() & FlagBlockByrefStruct) != 0;
346 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
347 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
348 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
349 bool isObjcClassComplete() const {
350 return (getFlags() & FlagObjcClassComplete) != 0;
352 bool isVector() const { return (getFlags() & FlagVector) != 0; }
353 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
354 bool isLValueReference() const {
355 return (getFlags() & FlagLValueReference) != 0;
357 bool isRValueReference() const {
358 return (getFlags() & FlagRValueReference) != 0;
360 bool isValid() const { return DbgNode && isType(); }
363 /// DIBasicType - A basic type, like 'int' or 'float'.
364 class DIBasicType : public DIType {
366 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
368 unsigned getEncoding() const { return getUnsignedField(9); }
370 /// Verify - Verify that a basic type descriptor is well formed.
374 /// DIDerivedType - A simple derived type, like a const qualified type,
375 /// a typedef, a pointer or reference, et cetera. Or, a data member of
376 /// a class/struct/union.
377 class DIDerivedType : public DIType {
378 friend class DIDescriptor;
379 void printInternal(raw_ostream &OS) const;
382 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
384 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
386 /// getObjCProperty - Return property node, if this ivar is
387 /// associated with one.
388 MDNode *getObjCProperty() const;
390 DITypeRef getClassType() const {
391 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
392 return getFieldAs<DITypeRef>(10);
395 Constant *getConstant() const {
396 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
397 return getConstantField(10);
400 /// Verify - Verify that a derived type descriptor is well formed.
404 /// DICompositeType - This descriptor holds a type that can refer to multiple
405 /// other types, like a function or struct.
406 /// DICompositeType is derived from DIDerivedType because some
407 /// composite types (such as enums) can be derived from basic types
408 // FIXME: Make this derive from DIType directly & just store the
409 // base type in a single DIType field.
410 class DICompositeType : public DIDerivedType {
411 friend class DIDescriptor;
412 void printInternal(raw_ostream &OS) const;
413 void setArraysHelper(MDNode *Elements, MDNode *TParams);
416 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
418 DIArray getElements() const {
419 assert(!isSubroutineType() && "no elements for DISubroutineType");
420 return getFieldAs<DIArray>(10);
422 template <typename T>
423 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
424 assert((!TParams || DbgNode->getNumOperands() == 15) &&
425 "If you're setting the template parameters this should include a slot "
427 setArraysHelper(Elements, TParams);
429 unsigned getRunTimeLang() const { return getUnsignedField(11); }
430 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
431 void setContainingType(DICompositeType ContainingType);
432 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
433 MDString *getIdentifier() const;
435 /// Verify - Verify that a composite type descriptor is well formed.
439 class DISubroutineType : public DICompositeType {
441 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
442 DITypedArray<DITypeRef> getTypeArray() const {
443 return getFieldAs<DITypedArray<DITypeRef>>(10);
447 /// DIFile - This is a wrapper for a file.
448 class DIFile : public DIScope {
449 friend class DIDescriptor;
452 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
453 MDNode *getFileNode() const;
457 /// DICompileUnit - A wrapper for a compile unit.
458 class DICompileUnit : public DIScope {
459 friend class DIDescriptor;
460 void printInternal(raw_ostream &OS) const;
463 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
465 dwarf::SourceLanguage getLanguage() const {
466 return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
468 StringRef getProducer() const { return getStringField(3); }
470 bool isOptimized() const { return getUnsignedField(4) != 0; }
471 StringRef getFlags() const { return getStringField(5); }
472 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
474 DIArray getEnumTypes() const;
475 DIArray getRetainedTypes() const;
476 DIArray getSubprograms() const;
477 DIArray getGlobalVariables() const;
478 DIArray getImportedEntities() const;
480 StringRef getSplitDebugFilename() const { return getStringField(12); }
481 unsigned getEmissionKind() const { return getUnsignedField(13); }
483 /// Verify - Verify that a compile unit is well formed.
487 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
488 class DISubprogram : public DIScope {
489 friend class DIDescriptor;
490 void printInternal(raw_ostream &OS) const;
493 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
495 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
496 StringRef getName() const { return getStringField(3); }
497 StringRef getDisplayName() const { return getStringField(4); }
498 StringRef getLinkageName() const { return getStringField(5); }
499 unsigned getLineNumber() const { return getUnsignedField(6); }
500 DISubroutineType getType() const { return getFieldAs<DISubroutineType>(7); }
502 /// isLocalToUnit - Return true if this subprogram is local to the current
503 /// compile unit, like 'static' in C.
504 unsigned isLocalToUnit() const { return getUnsignedField(8); }
505 unsigned isDefinition() const { return getUnsignedField(9); }
507 unsigned getVirtuality() const { return getUnsignedField(10); }
508 unsigned getVirtualIndex() const { return getUnsignedField(11); }
510 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
512 unsigned getFlags() const { return getUnsignedField(13); }
514 unsigned isArtificial() const {
515 return (getUnsignedField(13) & FlagArtificial) != 0;
517 /// isPrivate - Return true if this subprogram has "private"
518 /// access specifier.
519 bool isPrivate() const {
520 return (getFlags() & FlagAccessibility) == FlagPrivate;
522 /// isProtected - Return true if this subprogram has "protected"
523 /// access specifier.
524 bool isProtected() const {
525 return (getFlags() & FlagAccessibility) == FlagProtected;
527 /// isPublic - Return true if this subprogram has "public"
528 /// access specifier.
529 bool isPublic() const {
530 return (getFlags() & FlagAccessibility) == FlagPublic;
532 /// isExplicit - Return true if this subprogram is marked as explicit.
533 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
534 /// isPrototyped - Return true if this subprogram is prototyped.
535 bool isPrototyped() const {
536 return (getUnsignedField(13) & FlagPrototyped) != 0;
539 /// Return true if this subprogram is a C++11 reference-qualified
540 /// non-static member function (void foo() &).
541 unsigned isLValueReference() const {
542 return (getUnsignedField(13) & FlagLValueReference) != 0;
545 /// Return true if this subprogram is a C++11
546 /// rvalue-reference-qualified non-static member function
548 unsigned isRValueReference() const {
549 return (getUnsignedField(13) & FlagRValueReference) != 0;
552 unsigned isOptimized() const;
554 /// Verify - Verify that a subprogram descriptor is well formed.
557 /// describes - Return true if this subprogram provides debugging
558 /// information for the function F.
559 bool describes(const Function *F);
561 Function *getFunction() const { return getFunctionField(15); }
562 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
563 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
564 DISubprogram getFunctionDeclaration() const {
565 return getFieldAs<DISubprogram>(17);
567 MDNode *getVariablesNodes() const;
568 DIArray getVariables() const;
570 /// getScopeLineNumber - Get the beginning of the scope of the
571 /// function, not necessarily where the name of the program
573 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
576 /// DILexicalBlock - This is a wrapper for a lexical block.
577 class DILexicalBlock : public DIScope {
579 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
580 DIScope getContext() const { return getFieldAs<DIScope>(2); }
581 unsigned getLineNumber() const { return getUnsignedField(3); }
582 unsigned getColumnNumber() const { return getUnsignedField(4); }
586 /// DILexicalBlockFile - This is a wrapper for a lexical block with
587 /// a filename change.
588 class DILexicalBlockFile : public DIScope {
590 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
591 DIScope getContext() const {
592 if (getScope().isSubprogram())
594 return getScope().getContext();
596 unsigned getLineNumber() const { return getScope().getLineNumber(); }
597 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
598 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
599 unsigned getDiscriminator() const { return getUnsignedField(3); }
603 /// DINameSpace - A wrapper for a C++ style name space.
604 class DINameSpace : public DIScope {
605 friend class DIDescriptor;
606 void printInternal(raw_ostream &OS) const;
609 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
610 DIScope getContext() const { return getFieldAs<DIScope>(2); }
611 StringRef getName() const { return getStringField(3); }
612 unsigned getLineNumber() const { return getUnsignedField(4); }
616 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
617 class DITemplateTypeParameter : public DIDescriptor {
619 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
622 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
623 StringRef getName() const { return getStringField(2); }
624 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
625 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
626 StringRef getDirectory() const {
627 return getFieldAs<DIFile>(4).getDirectory();
629 unsigned getLineNumber() const { return getUnsignedField(5); }
630 unsigned getColumnNumber() const { return getUnsignedField(6); }
634 /// DITemplateValueParameter - This is a wrapper for template value parameter.
635 class DITemplateValueParameter : public DIDescriptor {
637 explicit DITemplateValueParameter(const MDNode *N = nullptr)
640 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
641 StringRef getName() const { return getStringField(2); }
642 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
643 Value *getValue() const;
644 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
645 StringRef getDirectory() const {
646 return getFieldAs<DIFile>(5).getDirectory();
648 unsigned getLineNumber() const { return getUnsignedField(6); }
649 unsigned getColumnNumber() const { return getUnsignedField(7); }
653 /// DIGlobalVariable - This is a wrapper for a global variable.
654 class DIGlobalVariable : public DIDescriptor {
655 friend class DIDescriptor;
656 void printInternal(raw_ostream &OS) const;
659 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
661 DIScope getContext() const { return getFieldAs<DIScope>(2); }
662 StringRef getName() const { return getStringField(3); }
663 StringRef getDisplayName() const { return getStringField(4); }
664 StringRef getLinkageName() const { return getStringField(5); }
665 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
666 StringRef getDirectory() const {
667 return getFieldAs<DIFile>(6).getDirectory();
670 unsigned getLineNumber() const { return getUnsignedField(7); }
671 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
672 unsigned isLocalToUnit() const { return getUnsignedField(9); }
673 unsigned isDefinition() const { return getUnsignedField(10); }
675 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
676 Constant *getConstant() const { return getConstantField(11); }
677 DIDerivedType getStaticDataMemberDeclaration() const {
678 return getFieldAs<DIDerivedType>(12);
681 /// Verify - Verify that a global variable descriptor is well formed.
685 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
687 class DIVariable : public DIDescriptor {
688 friend class DIDescriptor;
689 void printInternal(raw_ostream &OS) const;
692 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
694 DIScope getContext() const { return getFieldAs<DIScope>(1); }
695 StringRef getName() const { return getStringField(2); }
696 DIFile getFile() const { return getFieldAs<DIFile>(3); }
697 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
698 unsigned getArgNumber() const {
699 unsigned L = getUnsignedField(4);
702 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
704 /// isArtificial - Return true if this variable is marked as "artificial".
705 bool isArtificial() const {
706 return (getUnsignedField(6) & FlagArtificial) != 0;
709 bool isObjectPointer() const {
710 return (getUnsignedField(6) & FlagObjectPointer) != 0;
713 /// \brief Return true if this variable is represented as a pointer.
714 bool isIndirect() const {
715 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
718 /// getInlinedAt - If this variable is inlined then return inline location.
719 MDNode *getInlinedAt() const;
721 /// Verify - Verify that a variable descriptor is well formed.
724 /// isBlockByrefVariable - Return true if the variable was declared as
725 /// a "__block" variable (Apple Blocks).
726 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
727 return (getType().resolve(Map)).isBlockByrefStruct();
730 /// isInlinedFnArgument - Return true if this variable provides debugging
731 /// information for an inlined function arguments.
732 bool isInlinedFnArgument(const Function *CurFn);
734 /// Return the size reported by the variable's type.
735 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
737 void printExtendedName(raw_ostream &OS) const;
740 /// DIExpression - A complex location expression.
741 class DIExpression : public DIDescriptor {
742 friend class DIDescriptor;
743 void printInternal(raw_ostream &OS) const;
746 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
748 /// Verify - Verify that a variable descriptor is well formed.
751 /// \brief Return the number of elements in the complex expression.
752 unsigned getNumElements() const {
755 unsigned N = DbgNode->getNumOperands();
756 assert(N > 0 && "missing tag");
760 /// \brief return the Idx'th complex address element.
761 uint64_t getElement(unsigned Idx) const;
763 /// isVariablePiece - Return whether this is a piece of an aggregate
765 bool isVariablePiece() const;
766 /// getPieceOffset - Return the offset of this piece in bytes.
767 uint64_t getPieceOffset() const;
768 /// getPieceSize - Return the size of this piece in bytes.
769 uint64_t getPieceSize() const;
772 /// DILocation - This object holds location information. This object
773 /// is not associated with any DWARF tag.
774 class DILocation : public DIDescriptor {
776 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
778 unsigned getLineNumber() const { return getUnsignedField(0); }
779 unsigned getColumnNumber() const { return getUnsignedField(1); }
780 DIScope getScope() const { return getFieldAs<DIScope>(2); }
781 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
782 StringRef getFilename() const { return getScope().getFilename(); }
783 StringRef getDirectory() const { return getScope().getDirectory(); }
785 bool atSameLineAs(const DILocation &Other) const {
786 return (getLineNumber() == Other.getLineNumber() &&
787 getFilename() == Other.getFilename());
789 /// getDiscriminator - DWARF discriminators are used to distinguish
790 /// identical file locations for instructions that are on different
791 /// basic blocks. If two instructions are inside the same lexical block
792 /// and are in different basic blocks, we create a new lexical block
793 /// with identical location as the original but with a different
794 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
796 unsigned getDiscriminator() const {
797 // Since discriminators are associated with lexical blocks, make
798 // sure this location is a lexical block before retrieving its
800 return getScope().isLexicalBlockFile()
801 ? getFieldAs<DILexicalBlockFile>(2).getDiscriminator()
804 unsigned computeNewDiscriminator(LLVMContext &Ctx);
805 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
808 class DIObjCProperty : public DIDescriptor {
809 friend class DIDescriptor;
810 void printInternal(raw_ostream &OS) const;
813 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
815 StringRef getObjCPropertyName() const { return getStringField(1); }
816 DIFile getFile() const { return getFieldAs<DIFile>(2); }
817 unsigned getLineNumber() const { return getUnsignedField(3); }
819 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
820 StringRef getObjCPropertySetterName() const { return getStringField(5); }
821 bool isReadOnlyObjCProperty() const {
822 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
824 bool isReadWriteObjCProperty() const {
825 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
827 bool isAssignObjCProperty() const {
828 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
830 bool isRetainObjCProperty() const {
831 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
833 bool isCopyObjCProperty() const {
834 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
836 bool isNonAtomicObjCProperty() const {
837 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
840 /// Objective-C doesn't have an ODR, so there is no benefit in storing
841 /// the type as a DITypeRef here.
842 DIType getType() const { return getFieldAs<DIType>(7); }
844 /// Verify - Verify that a derived type descriptor is well formed.
848 /// \brief An imported module (C++ using directive or similar).
849 class DIImportedEntity : public DIDescriptor {
850 friend class DIDescriptor;
851 void printInternal(raw_ostream &OS) const;
854 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
855 DIScope getContext() const { return getFieldAs<DIScope>(1); }
856 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
857 unsigned getLineNumber() const { return getUnsignedField(3); }
858 StringRef getName() const { return getStringField(4); }
862 /// getDISubprogram - Find subprogram that is enclosing this scope.
863 DISubprogram getDISubprogram(const MDNode *Scope);
865 /// getDICompositeType - Find underlying composite type.
866 DICompositeType getDICompositeType(DIType T);
868 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
869 /// to hold function specific information.
870 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
872 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
873 /// suitable to hold function specific information.
874 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
876 /// createInlinedVariable - Create a new inlined variable based on current
878 /// @param DV Current Variable.
879 /// @param InlinedScope Location at current variable is inlined.
880 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
881 LLVMContext &VMContext);
883 /// cleanseInlinedVariable - Remove inlined scope from the variable.
884 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
886 /// Construct DITypeIdentifierMap by going through retained types of each CU.
887 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
889 /// Strip debug info in the module if it exists.
890 /// To do this, we remove all calls to the debugger intrinsics and any named
891 /// metadata for debugging. We also remove debug locations for instructions.
892 /// Return true if module is modified.
893 bool StripDebugInfo(Module &M);
895 /// Return Debug Info Metadata Version by checking module flags.
896 unsigned getDebugMetadataVersionFromModule(const Module &M);
898 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
899 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
900 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
901 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
902 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
904 class DebugInfoFinder {
906 DebugInfoFinder() : TypeMapInitialized(false) {}
908 /// processModule - Process entire module and collect debug info
910 void processModule(const Module &M);
912 /// processDeclare - Process DbgDeclareInst.
913 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
914 /// Process DbgValueInst.
915 void processValue(const Module &M, const DbgValueInst *DVI);
916 /// processLocation - Process DILocation.
917 void processLocation(const Module &M, DILocation Loc);
923 /// Initialize TypeIdentifierMap.
924 void InitializeTypeMap(const Module &M);
926 /// processType - Process DIType.
927 void processType(DIType DT);
929 /// processSubprogram - Process DISubprogram.
930 void processSubprogram(DISubprogram SP);
932 void processScope(DIScope Scope);
934 /// addCompileUnit - Add compile unit into CUs.
935 bool addCompileUnit(DICompileUnit CU);
937 /// addGlobalVariable - Add global variable into GVs.
938 bool addGlobalVariable(DIGlobalVariable DIG);
940 // addSubprogram - Add subprogram into SPs.
941 bool addSubprogram(DISubprogram SP);
943 /// addType - Add type into Tys.
944 bool addType(DIType DT);
946 bool addScope(DIScope Scope);
949 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
950 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
951 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
952 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
953 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
955 iterator_range<compile_unit_iterator> compile_units() const {
956 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
959 iterator_range<subprogram_iterator> subprograms() const {
960 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
963 iterator_range<global_variable_iterator> global_variables() const {
964 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
967 iterator_range<type_iterator> types() const {
968 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
971 iterator_range<scope_iterator> scopes() const {
972 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
975 unsigned compile_unit_count() const { return CUs.size(); }
976 unsigned global_variable_count() const { return GVs.size(); }
977 unsigned subprogram_count() const { return SPs.size(); }
978 unsigned type_count() const { return TYs.size(); }
979 unsigned scope_count() const { return Scopes.size(); }
982 SmallVector<DICompileUnit, 8> CUs; // Compile Units
983 SmallVector<DISubprogram, 8> SPs; // Subprograms
984 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
985 SmallVector<DIType, 8> TYs; // Types
986 SmallVector<DIScope, 8> Scopes; // Scopes
987 SmallPtrSet<MDNode *, 64> NodesSeen;
988 DITypeIdentifierMap TypeIdentifierMap;
989 /// Specify if TypeIdentifierMap is initialized.
990 bool TypeMapInitialized;
993 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
995 } // end namespace llvm