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;
69 FlagProtected = 1 << 1,
71 FlagAppleBlock = 1 << 3,
72 FlagBlockByrefStruct = 1 << 4,
74 FlagArtificial = 1 << 6,
75 FlagExplicit = 1 << 7,
76 FlagPrototyped = 1 << 8,
77 FlagObjcClassComplete = 1 << 9,
78 FlagObjectPointer = 1 << 10,
80 FlagStaticMember = 1 << 12,
81 FlagIndirectVariable = 1 << 13,
82 FlagLValueReference = 1 << 14,
83 FlagRValueReference = 1 << 15
87 const MDNode *DbgNode;
89 StringRef getStringField(unsigned Elt) const;
90 unsigned getUnsignedField(unsigned Elt) const {
91 return (unsigned)getUInt64Field(Elt);
93 uint64_t getUInt64Field(unsigned Elt) const;
94 int64_t getInt64Field(unsigned Elt) const;
95 DIDescriptor getDescriptorField(unsigned Elt) const;
97 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
98 return DescTy(getDescriptorField(Elt));
101 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
102 Constant *getConstantField(unsigned Elt) const;
103 Function *getFunctionField(unsigned Elt) const;
104 void replaceFunctionField(unsigned Elt, Function *F);
107 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
111 operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
112 MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
114 // An explicit operator bool so that we can do testing of DI values
116 // FIXME: This operator bool isn't actually protecting anything at the
117 // moment due to the conversion operator above making DIDescriptor nodes
118 // implicitly convertable to bool.
119 LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
121 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
122 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
124 uint16_t getTag() const {
125 return getUnsignedField(0) & ~LLVMDebugVersionMask;
128 bool isDerivedType() const;
129 bool isCompositeType() const;
130 bool isBasicType() const;
131 bool isTrivialType() const;
132 bool isVariable() const;
133 bool isSubprogram() const;
134 bool isGlobalVariable() const;
135 bool isScope() const;
137 bool isCompileUnit() const;
138 bool isNameSpace() const;
139 bool isLexicalBlockFile() const;
140 bool isLexicalBlock() const;
141 bool isSubrange() const;
142 bool isEnumerator() const;
144 bool isUnspecifiedParameter() const;
145 bool isTemplateTypeParameter() const;
146 bool isTemplateValueParameter() const;
147 bool isObjCProperty() const;
148 bool isImportedEntity() const;
150 /// print - print descriptor.
151 void print(raw_ostream &OS) const;
153 /// dump - print descriptor to dbgs() with a newline.
157 /// DISubrange - This is used to represent ranges, for array bounds.
158 class DISubrange : public DIDescriptor {
159 friend class DIDescriptor;
160 void printInternal(raw_ostream &OS) const;
163 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
165 int64_t getLo() const { return getInt64Field(1); }
166 int64_t getCount() const { return getInt64Field(2); }
170 /// DITypedArray - This descriptor holds an array of nodes with type T.
171 template <typename T> class DITypedArray : public DIDescriptor {
173 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
174 unsigned getNumElements() const {
175 return DbgNode ? DbgNode->getNumOperands() : 0;
177 T getElement(unsigned Idx) const {
178 return getFieldAs<T>(Idx);
182 typedef DITypedArray<DIDescriptor> DIArray;
184 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
185 /// FIXME: it seems strange that this doesn't have either a reference to the
186 /// type/precision or a file/line pair for location info.
187 class DIEnumerator : public DIDescriptor {
188 friend class DIDescriptor;
189 void printInternal(raw_ostream &OS) const;
192 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
194 StringRef getName() const { return getStringField(1); }
195 int64_t getEnumValue() const { return getInt64Field(2); }
199 template <typename T> class DIRef;
200 typedef DIRef<DIScope> DIScopeRef;
201 typedef DIRef<DIType> DITypeRef;
202 typedef DITypedArray<DITypeRef> DITypeArray;
204 /// DIScope - A base class for various scopes.
206 /// Although, implementation-wise, DIScope is the parent class of most
207 /// other DIxxx classes, including DIType and its descendants, most of
208 /// DIScope's descendants are not a substitutable subtype of
209 /// DIScope. The DIDescriptor::isScope() method only is true for
210 /// DIScopes that are scopes in the strict lexical scope sense
211 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
212 class DIScope : public DIDescriptor {
214 friend class DIDescriptor;
215 void printInternal(raw_ostream &OS) const;
218 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
220 /// Gets the parent scope for this scope node or returns a
221 /// default constructed scope.
222 DIScopeRef getContext() const;
223 /// If the scope node has a name, return that, else return an empty string.
224 StringRef getName() const;
225 StringRef getFilename() const;
226 StringRef getDirectory() const;
228 /// Generate a reference to this DIScope. Uses the type identifier instead
229 /// of the actual MDNode if possible, to help type uniquing.
230 DIScopeRef getRef() const;
233 /// Represents reference to a DIDescriptor, abstracts over direct and
234 /// identifier-based metadata references.
235 template <typename T> class DIRef {
236 template <typename DescTy>
237 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
238 friend DIScopeRef DIScope::getContext() const;
239 friend DIScopeRef DIScope::getRef() const;
242 /// Val can be either a MDNode or a MDString, in the latter,
243 /// MDString specifies the type identifier.
245 explicit DIRef(const Value *V);
248 T resolve(const DITypeIdentifierMap &Map) const;
249 StringRef getName() const;
250 operator Value *() const { return const_cast<Value *>(Val); }
253 template <typename T>
254 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
258 if (const MDNode *MD = dyn_cast<MDNode>(Val))
261 const MDString *MS = cast<MDString>(Val);
262 // Find the corresponding MDNode.
263 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
264 assert(Iter != Map.end() && "Identifier not in the type map?");
265 assert(DIDescriptor(Iter->second).isType() &&
266 "MDNode in DITypeIdentifierMap should be a DIType.");
267 return T(Iter->second);
270 template <typename T> StringRef DIRef<T>::getName() const {
274 if (const MDNode *MD = dyn_cast<MDNode>(Val))
275 return T(MD).getName();
277 const MDString *MS = cast<MDString>(Val);
278 return MS->getString();
281 /// Specialize getFieldAs to handle fields that are references to DIScopes.
282 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
283 /// Specialize DIRef constructor for DIScopeRef.
284 template <> DIRef<DIScope>::DIRef(const Value *V);
286 /// Specialize getFieldAs to handle fields that are references to DITypes.
287 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
288 /// Specialize DIRef constructor for DITypeRef.
289 template <> DIRef<DIType>::DIRef(const Value *V);
291 /// DIType - This is a wrapper for a type.
292 /// FIXME: Types should be factored much better so that CV qualifiers and
293 /// others do not require a huge and empty descriptor full of zeros.
294 class DIType : public DIScope {
296 friend class DIDescriptor;
297 void printInternal(raw_ostream &OS) const;
300 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
301 operator DITypeRef () const {
303 "constructing DITypeRef from an MDNode that is not a type");
304 return DITypeRef(&*getRef());
307 /// Verify - Verify that a type descriptor is well formed.
310 DIScopeRef getContext() const {
311 assert(!isTrivialType() && "no context for DITrivialType");
312 return getFieldAs<DIScopeRef>(2);
314 StringRef getName() const {
315 assert(!isTrivialType() && "no name for DITrivialType");
316 return getStringField(3);
318 unsigned getLineNumber() const {
319 assert(!isTrivialType() && "no line number for DITrivialType");
320 return getUnsignedField(4);
322 uint64_t getSizeInBits() const {
323 assert(!isTrivialType() && "no SizeInBits for DITrivialType");
324 return getUInt64Field(5);
326 uint64_t getAlignInBits() const {
327 assert(!isTrivialType() && "no AlignInBits for DITrivialType");
328 return getUInt64Field(6);
330 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
331 // carry this is just plain insane.
332 uint64_t getOffsetInBits() const {
333 assert(!isTrivialType() && "no OffsetInBits for DITrivialType");
334 return getUInt64Field(7);
336 unsigned getFlags() const {
337 assert(!isTrivialType() && "no flag for DITrivialType");
338 return getUnsignedField(8);
340 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
341 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
342 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
343 // isAppleBlock - Return true if this is the Apple Blocks extension.
344 bool isAppleBlockExtension() const {
345 return (getFlags() & FlagAppleBlock) != 0;
347 bool isBlockByrefStruct() const {
348 return (getFlags() & FlagBlockByrefStruct) != 0;
350 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
351 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
352 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
353 bool isObjcClassComplete() const {
354 return (getFlags() & FlagObjcClassComplete) != 0;
356 bool isVector() const { return (getFlags() & FlagVector) != 0; }
357 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
358 bool isLValueReference() const {
359 return (getFlags() & FlagLValueReference) != 0;
361 bool isRValueReference() const {
362 return (getFlags() & FlagRValueReference) != 0;
364 bool isValid() const { return DbgNode && isType(); }
366 /// replaceAllUsesWith - Replace all uses of debug info referenced by
368 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
369 void replaceAllUsesWith(MDNode *D);
372 class DITrivialType : public DIType {
374 explicit DITrivialType(const MDNode *N = nullptr) : DIType(N) {}
378 /// DIBasicType - A basic type, like 'int' or 'float'.
379 class DIBasicType : public DIType {
381 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
383 unsigned getEncoding() const { return getUnsignedField(9); }
385 /// Verify - Verify that a basic type descriptor is well formed.
389 /// DIDerivedType - A simple derived type, like a const qualified type,
390 /// a typedef, a pointer or reference, et cetera. Or, a data member of
391 /// a class/struct/union.
392 class DIDerivedType : public DIType {
393 friend class DIDescriptor;
394 void printInternal(raw_ostream &OS) const;
397 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
399 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
401 /// getObjCProperty - Return property node, if this ivar is
402 /// associated with one.
403 MDNode *getObjCProperty() const;
405 DITypeRef getClassType() const {
406 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
407 return getFieldAs<DITypeRef>(10);
410 Constant *getConstant() const {
411 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
412 return getConstantField(10);
415 /// Verify - Verify that a derived type descriptor is well formed.
419 /// DICompositeType - This descriptor holds a type that can refer to multiple
420 /// other types, like a function or struct.
421 /// DICompositeType is derived from DIDerivedType because some
422 /// composite types (such as enums) can be derived from basic types
423 // FIXME: Make this derive from DIType directly & just store the
424 // base type in a single DIType field.
425 class DICompositeType : public DIDerivedType {
426 friend class DIDescriptor;
427 void printInternal(raw_ostream &OS) const;
428 void setArraysHelper(MDNode *Elements, MDNode *TParams);
431 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
433 DIArray getElements() const { return getFieldAs<DIArray>(10); }
434 template <typename T>
435 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
436 assert((!TParams || DbgNode->getNumOperands() == 15) &&
437 "If you're setting the template parameters this should include a slot "
439 setArraysHelper(Elements, TParams);
441 unsigned getRunTimeLang() const { return getUnsignedField(11); }
442 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
443 void setContainingType(DICompositeType ContainingType);
444 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
445 MDString *getIdentifier() const;
447 /// Verify - Verify that a composite type descriptor is well formed.
451 /// DIFile - This is a wrapper for a file.
452 class DIFile : public DIScope {
453 friend class DIDescriptor;
456 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
457 MDNode *getFileNode() const;
461 /// DICompileUnit - A wrapper for a compile unit.
462 class DICompileUnit : public DIScope {
463 friend class DIDescriptor;
464 void printInternal(raw_ostream &OS) const;
467 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
469 dwarf::SourceLanguage getLanguage() const {
470 return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
472 StringRef getProducer() const { return getStringField(3); }
474 bool isOptimized() const { return getUnsignedField(4) != 0; }
475 StringRef getFlags() const { return getStringField(5); }
476 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
478 DIArray getEnumTypes() const;
479 DIArray getRetainedTypes() const;
480 DIArray getSubprograms() const;
481 DIArray getGlobalVariables() const;
482 DIArray getImportedEntities() const;
484 StringRef getSplitDebugFilename() const { return getStringField(12); }
485 unsigned getEmissionKind() const { return getUnsignedField(13); }
487 /// Verify - Verify that a compile unit is well formed.
491 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
492 class DISubprogram : public DIScope {
493 friend class DIDescriptor;
494 void printInternal(raw_ostream &OS) const;
497 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
499 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
500 StringRef getName() const { return getStringField(3); }
501 StringRef getDisplayName() const { return getStringField(4); }
502 StringRef getLinkageName() const { return getStringField(5); }
503 unsigned getLineNumber() const { return getUnsignedField(6); }
504 DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
506 /// isLocalToUnit - Return true if this subprogram is local to the current
507 /// compile unit, like 'static' in C.
508 unsigned isLocalToUnit() const { return getUnsignedField(8); }
509 unsigned isDefinition() const { return getUnsignedField(9); }
511 unsigned getVirtuality() const { return getUnsignedField(10); }
512 unsigned getVirtualIndex() const { return getUnsignedField(11); }
514 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
516 unsigned getFlags() const { return getUnsignedField(13); }
518 unsigned isArtificial() const {
519 return (getUnsignedField(13) & FlagArtificial) != 0;
521 /// isPrivate - Return true if this subprogram has "private"
522 /// access specifier.
523 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
524 /// isProtected - Return true if this subprogram has "protected"
525 /// access specifier.
526 bool isProtected() const {
527 return (getUnsignedField(13) & FlagProtected) != 0;
529 /// isExplicit - Return true if this subprogram is marked as explicit.
530 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
531 /// isPrototyped - Return true if this subprogram is prototyped.
532 bool isPrototyped() const {
533 return (getUnsignedField(13) & FlagPrototyped) != 0;
536 /// Return true if this subprogram is a C++11 reference-qualified
537 /// non-static member function (void foo() &).
538 unsigned isLValueReference() const {
539 return (getUnsignedField(13) & FlagLValueReference) != 0;
542 /// Return true if this subprogram is a C++11
543 /// rvalue-reference-qualified non-static member function
545 unsigned isRValueReference() const {
546 return (getUnsignedField(13) & FlagRValueReference) != 0;
549 unsigned isOptimized() const;
551 /// Verify - Verify that a subprogram descriptor is well formed.
554 /// describes - Return true if this subprogram provides debugging
555 /// information for the function F.
556 bool describes(const Function *F);
558 Function *getFunction() const { return getFunctionField(15); }
559 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
560 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
561 DISubprogram getFunctionDeclaration() const {
562 return getFieldAs<DISubprogram>(17);
564 MDNode *getVariablesNodes() const;
565 DIArray getVariables() const;
567 /// getScopeLineNumber - Get the beginning of the scope of the
568 /// function, not necessarily where the name of the program
570 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
573 /// DILexicalBlock - This is a wrapper for a lexical block.
574 class DILexicalBlock : public DIScope {
576 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
577 DIScope getContext() const { return getFieldAs<DIScope>(2); }
578 unsigned getLineNumber() const { return getUnsignedField(3); }
579 unsigned getColumnNumber() const { return getUnsignedField(4); }
580 unsigned getDiscriminator() const { return getUnsignedField(5); }
584 /// DILexicalBlockFile - This is a wrapper for a lexical block with
585 /// a filename change.
586 class DILexicalBlockFile : public DIScope {
588 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
589 DIScope getContext() const {
590 if (getScope().isSubprogram())
592 return getScope().getContext();
594 unsigned getLineNumber() const { return getScope().getLineNumber(); }
595 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
596 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
600 /// DINameSpace - A wrapper for a C++ style name space.
601 class DINameSpace : public DIScope {
602 friend class DIDescriptor;
603 void printInternal(raw_ostream &OS) const;
606 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
607 DIScope getContext() const { return getFieldAs<DIScope>(2); }
608 StringRef getName() const { return getStringField(3); }
609 unsigned getLineNumber() const { return getUnsignedField(4); }
613 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
614 class DITemplateTypeParameter : public DIDescriptor {
616 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
619 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
620 StringRef getName() const { return getStringField(2); }
621 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
622 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
623 StringRef getDirectory() const {
624 return getFieldAs<DIFile>(4).getDirectory();
626 unsigned getLineNumber() const { return getUnsignedField(5); }
627 unsigned getColumnNumber() const { return getUnsignedField(6); }
631 /// DITemplateValueParameter - This is a wrapper for template value parameter.
632 class DITemplateValueParameter : public DIDescriptor {
634 explicit DITemplateValueParameter(const MDNode *N = nullptr)
637 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
638 StringRef getName() const { return getStringField(2); }
639 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
640 Value *getValue() const;
641 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
642 StringRef getDirectory() const {
643 return getFieldAs<DIFile>(5).getDirectory();
645 unsigned getLineNumber() const { return getUnsignedField(6); }
646 unsigned getColumnNumber() const { return getUnsignedField(7); }
650 /// DIGlobalVariable - This is a wrapper for a global variable.
651 class DIGlobalVariable : public DIDescriptor {
652 friend class DIDescriptor;
653 void printInternal(raw_ostream &OS) const;
656 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
658 DIScope getContext() const { return getFieldAs<DIScope>(2); }
659 StringRef getName() const { return getStringField(3); }
660 StringRef getDisplayName() const { return getStringField(4); }
661 StringRef getLinkageName() const { return getStringField(5); }
662 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
663 StringRef getDirectory() const {
664 return getFieldAs<DIFile>(6).getDirectory();
667 unsigned getLineNumber() const { return getUnsignedField(7); }
668 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
669 unsigned isLocalToUnit() const { return getUnsignedField(9); }
670 unsigned isDefinition() const { return getUnsignedField(10); }
672 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
673 Constant *getConstant() const { return getConstantField(11); }
674 DIDerivedType getStaticDataMemberDeclaration() const {
675 return getFieldAs<DIDerivedType>(12);
678 /// Verify - Verify that a global variable descriptor is well formed.
682 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
684 class DIVariable : public DIDescriptor {
685 friend class DIDescriptor;
686 void printInternal(raw_ostream &OS) const;
689 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
691 DIScope getContext() const { return getFieldAs<DIScope>(1); }
692 StringRef getName() const { return getStringField(2); }
693 DIFile getFile() const { return getFieldAs<DIFile>(3); }
694 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
695 unsigned getArgNumber() const {
696 unsigned L = getUnsignedField(4);
699 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
701 /// isArtificial - Return true if this variable is marked as "artificial".
702 bool isArtificial() const {
703 return (getUnsignedField(6) & FlagArtificial) != 0;
706 bool isObjectPointer() const {
707 return (getUnsignedField(6) & FlagObjectPointer) != 0;
710 /// \brief Return true if this variable is represented as a pointer.
711 bool isIndirect() const {
712 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
715 /// getInlinedAt - If this variable is inlined then return inline location.
716 MDNode *getInlinedAt() const;
718 /// Verify - Verify that a variable descriptor is well formed.
721 /// HasComplexAddr - Return true if the variable has a complex address.
722 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
724 /// \brief Return the size of this variable's complex address or
725 /// zero if there is none.
726 unsigned getNumAddrElements() const {
727 if (DbgNode->getNumOperands() < 9)
729 return getDescriptorField(8)->getNumOperands();
732 /// \brief return the Idx'th complex address element.
733 uint64_t getAddrElement(unsigned Idx) const;
735 /// isBlockByrefVariable - Return true if the variable was declared as
736 /// a "__block" variable (Apple Blocks).
737 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
738 return (getType().resolve(Map)).isBlockByrefStruct();
741 /// isInlinedFnArgument - Return true if this variable provides debugging
742 /// information for an inlined function arguments.
743 bool isInlinedFnArgument(const Function *CurFn);
745 void printExtendedName(raw_ostream &OS) const;
748 /// DILocation - This object holds location information. This object
749 /// is not associated with any DWARF tag.
750 class DILocation : public DIDescriptor {
752 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
754 unsigned getLineNumber() const { return getUnsignedField(0); }
755 unsigned getColumnNumber() const { return getUnsignedField(1); }
756 DIScope getScope() const { return getFieldAs<DIScope>(2); }
757 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
758 StringRef getFilename() const { return getScope().getFilename(); }
759 StringRef getDirectory() const { return getScope().getDirectory(); }
761 bool atSameLineAs(const DILocation &Other) const {
762 return (getLineNumber() == Other.getLineNumber() &&
763 getFilename() == Other.getFilename());
765 /// getDiscriminator - DWARF discriminators are used to distinguish
766 /// identical file locations for instructions that are on different
767 /// basic blocks. If two instructions are inside the same lexical block
768 /// and are in different basic blocks, we create a new lexical block
769 /// with identical location as the original but with a different
770 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
772 unsigned getDiscriminator() const {
773 // Since discriminators are associated with lexical blocks, make
774 // sure this location is a lexical block before retrieving its
776 return getScope().isLexicalBlock()
777 ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
780 unsigned computeNewDiscriminator(LLVMContext &Ctx);
781 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
784 class DIObjCProperty : public DIDescriptor {
785 friend class DIDescriptor;
786 void printInternal(raw_ostream &OS) const;
789 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
791 StringRef getObjCPropertyName() const { return getStringField(1); }
792 DIFile getFile() const { return getFieldAs<DIFile>(2); }
793 unsigned getLineNumber() const { return getUnsignedField(3); }
795 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
796 StringRef getObjCPropertySetterName() const { return getStringField(5); }
797 bool isReadOnlyObjCProperty() const {
798 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
800 bool isReadWriteObjCProperty() const {
801 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
803 bool isAssignObjCProperty() const {
804 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
806 bool isRetainObjCProperty() const {
807 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
809 bool isCopyObjCProperty() const {
810 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
812 bool isNonAtomicObjCProperty() const {
813 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
816 /// Objective-C doesn't have an ODR, so there is no benefit in storing
817 /// the type as a DITypeRef here.
818 DIType getType() const { return getFieldAs<DIType>(7); }
820 /// Verify - Verify that a derived type descriptor is well formed.
824 /// \brief An imported module (C++ using directive or similar).
825 class DIImportedEntity : public DIDescriptor {
826 friend class DIDescriptor;
827 void printInternal(raw_ostream &OS) const;
830 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
831 DIScope getContext() const { return getFieldAs<DIScope>(1); }
832 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
833 unsigned getLineNumber() const { return getUnsignedField(3); }
834 StringRef getName() const { return getStringField(4); }
838 /// getDISubprogram - Find subprogram that is enclosing this scope.
839 DISubprogram getDISubprogram(const MDNode *Scope);
841 /// getDICompositeType - Find underlying composite type.
842 DICompositeType getDICompositeType(DIType T);
844 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
845 /// to hold function specific information.
846 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
848 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
849 /// suitable to hold function specific information.
850 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
852 /// createInlinedVariable - Create a new inlined variable based on current
854 /// @param DV Current Variable.
855 /// @param InlinedScope Location at current variable is inlined.
856 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
857 LLVMContext &VMContext);
859 /// cleanseInlinedVariable - Remove inlined scope from the variable.
860 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
862 /// Construct DITypeIdentifierMap by going through retained types of each CU.
863 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
865 /// Strip debug info in the module if it exists.
866 /// To do this, we remove all calls to the debugger intrinsics and any named
867 /// metadata for debugging. We also remove debug locations for instructions.
868 /// Return true if module is modified.
869 bool StripDebugInfo(Module &M);
871 /// Return Debug Info Metadata Version by checking module flags.
872 unsigned getDebugMetadataVersionFromModule(const Module &M);
874 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
875 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
876 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
877 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
878 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
880 class DebugInfoFinder {
882 DebugInfoFinder() : TypeMapInitialized(false) {}
884 /// processModule - Process entire module and collect debug info
886 void processModule(const Module &M);
888 /// processDeclare - Process DbgDeclareInst.
889 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
890 /// Process DbgValueInst.
891 void processValue(const Module &M, const DbgValueInst *DVI);
892 /// processLocation - Process DILocation.
893 void processLocation(const Module &M, DILocation Loc);
899 /// Initialize TypeIdentifierMap.
900 void InitializeTypeMap(const Module &M);
902 /// processType - Process DIType.
903 void processType(DIType DT);
905 /// processSubprogram - Process DISubprogram.
906 void processSubprogram(DISubprogram SP);
908 void processScope(DIScope Scope);
910 /// addCompileUnit - Add compile unit into CUs.
911 bool addCompileUnit(DICompileUnit CU);
913 /// addGlobalVariable - Add global variable into GVs.
914 bool addGlobalVariable(DIGlobalVariable DIG);
916 // addSubprogram - Add subprogram into SPs.
917 bool addSubprogram(DISubprogram SP);
919 /// addType - Add type into Tys.
920 bool addType(DIType DT);
922 bool addScope(DIScope Scope);
925 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
926 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
927 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
928 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
929 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
931 iterator_range<compile_unit_iterator> compile_units() const {
932 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
935 iterator_range<subprogram_iterator> subprograms() const {
936 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
939 iterator_range<global_variable_iterator> global_variables() const {
940 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
943 iterator_range<type_iterator> types() const {
944 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
947 iterator_range<scope_iterator> scopes() const {
948 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
951 unsigned compile_unit_count() const { return CUs.size(); }
952 unsigned global_variable_count() const { return GVs.size(); }
953 unsigned subprogram_count() const { return SPs.size(); }
954 unsigned type_count() const { return TYs.size(); }
955 unsigned scope_count() const { return Scopes.size(); }
958 SmallVector<DICompileUnit, 8> CUs; // Compile Units
959 SmallVector<DISubprogram, 8> SPs; // Subprograms
960 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
961 SmallVector<DIType, 8> TYs; // Types
962 SmallVector<DIScope, 8> Scopes; // Scopes
963 SmallPtrSet<MDNode *, 64> NodesSeen;
964 DITypeIdentifierMap TypeIdentifierMap;
965 /// Specify if TypeIdentifierMap is initialized.
966 bool TypeMapInitialized;
969 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
971 } // end namespace llvm