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 /// DIArray - This descriptor holds an array of descriptors.
171 class DIArray : public DIDescriptor {
173 explicit DIArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
175 unsigned getNumElements() const;
176 DIDescriptor getElement(unsigned Idx) const {
177 return getDescriptorField(Idx);
181 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
182 /// FIXME: it seems strange that this doesn't have either a reference to the
183 /// type/precision or a file/line pair for location info.
184 class DIEnumerator : public DIDescriptor {
185 friend class DIDescriptor;
186 void printInternal(raw_ostream &OS) const;
189 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
191 StringRef getName() const { return getStringField(1); }
192 int64_t getEnumValue() const { return getInt64Field(2); }
196 template <typename T> class DIRef;
197 typedef DIRef<DIScope> DIScopeRef;
198 typedef DIRef<DIType> DITypeRef;
200 /// DIScope - A base class for various scopes.
202 /// Although, implementation-wise, DIScope is the parent class of most
203 /// other DIxxx classes, including DIType and its descendants, most of
204 /// DIScope's descendants are not a substitutable subtype of
205 /// DIScope. The DIDescriptor::isScope() method only is true for
206 /// DIScopes that are scopes in the strict lexical scope sense
207 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
208 class DIScope : public DIDescriptor {
210 friend class DIDescriptor;
211 void printInternal(raw_ostream &OS) const;
214 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
216 /// Gets the parent scope for this scope node or returns a
217 /// default constructed scope.
218 DIScopeRef getContext() const;
219 /// If the scope node has a name, return that, else return an empty string.
220 StringRef getName() const;
221 StringRef getFilename() const;
222 StringRef getDirectory() const;
224 /// Generate a reference to this DIScope. Uses the type identifier instead
225 /// of the actual MDNode if possible, to help type uniquing.
226 DIScopeRef getRef() const;
229 /// Represents reference to a DIDescriptor, abstracts over direct and
230 /// identifier-based metadata references.
231 template <typename T> class DIRef {
232 template <typename DescTy>
233 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
234 friend DIScopeRef DIScope::getContext() const;
235 friend DIScopeRef DIScope::getRef() const;
238 /// Val can be either a MDNode or a MDString, in the latter,
239 /// MDString specifies the type identifier.
241 explicit DIRef(const Value *V);
244 T resolve(const DITypeIdentifierMap &Map) const;
245 StringRef getName() const;
246 operator Value *() const { return const_cast<Value *>(Val); }
249 template <typename T>
250 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
254 if (const MDNode *MD = dyn_cast<MDNode>(Val))
257 const MDString *MS = cast<MDString>(Val);
258 // Find the corresponding MDNode.
259 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
260 assert(Iter != Map.end() && "Identifier not in the type map?");
261 assert(DIDescriptor(Iter->second).isType() &&
262 "MDNode in DITypeIdentifierMap should be a DIType.");
263 return T(Iter->second);
266 template <typename T> StringRef DIRef<T>::getName() const {
270 if (const MDNode *MD = dyn_cast<MDNode>(Val))
271 return T(MD).getName();
273 const MDString *MS = cast<MDString>(Val);
274 return MS->getString();
277 /// Specialize getFieldAs to handle fields that are references to DIScopes.
278 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
279 /// Specialize DIRef constructor for DIScopeRef.
280 template <> DIRef<DIScope>::DIRef(const Value *V);
282 /// Specialize getFieldAs to handle fields that are references to DITypes.
283 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
284 /// Specialize DIRef constructor for DITypeRef.
285 template <> DIRef<DIType>::DIRef(const Value *V);
287 /// DIType - This is a wrapper for a type.
288 /// FIXME: Types should be factored much better so that CV qualifiers and
289 /// others do not require a huge and empty descriptor full of zeros.
290 class DIType : public DIScope {
292 friend class DIDescriptor;
293 void printInternal(raw_ostream &OS) const;
296 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
297 operator DITypeRef () const {
299 "constructing DITypeRef from an MDNode that is not a type");
300 return DITypeRef(&*getRef());
303 /// Verify - Verify that a type descriptor is well formed.
306 DIScopeRef getContext() const {
307 assert(!isTrivialType() && "no context for DITrivialType");
308 return getFieldAs<DIScopeRef>(2);
310 StringRef getName() const {
311 assert(!isTrivialType() && "no name for DITrivialType");
312 return getStringField(3);
314 unsigned getLineNumber() const {
315 assert(!isTrivialType() && "no line number for DITrivialType");
316 return getUnsignedField(4);
318 uint64_t getSizeInBits() const {
319 assert(!isTrivialType() && "no SizeInBits for DITrivialType");
320 return getUInt64Field(5);
322 uint64_t getAlignInBits() const {
323 assert(!isTrivialType() && "no AlignInBits for DITrivialType");
324 return getUInt64Field(6);
326 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
327 // carry this is just plain insane.
328 uint64_t getOffsetInBits() const {
329 assert(!isTrivialType() && "no OffsetInBits for DITrivialType");
330 return getUInt64Field(7);
332 unsigned getFlags() const {
333 assert(!isTrivialType() && "no flag for DITrivialType");
334 return getUnsignedField(8);
336 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
337 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
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(); }
362 /// replaceAllUsesWith - Replace all uses of debug info referenced by
364 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
365 void replaceAllUsesWith(MDNode *D);
368 class DITrivialType : public DIType {
370 explicit DITrivialType(const MDNode *N = nullptr) : DIType(N) {}
374 /// DIBasicType - A basic type, like 'int' or 'float'.
375 class DIBasicType : public DIType {
377 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
379 unsigned getEncoding() const { return getUnsignedField(9); }
381 /// Verify - Verify that a basic type descriptor is well formed.
385 /// DIDerivedType - A simple derived type, like a const qualified type,
386 /// a typedef, a pointer or reference, et cetera. Or, a data member of
387 /// a class/struct/union.
388 class DIDerivedType : public DIType {
389 friend class DIDescriptor;
390 void printInternal(raw_ostream &OS) const;
393 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
395 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
397 /// getObjCProperty - Return property node, if this ivar is
398 /// associated with one.
399 MDNode *getObjCProperty() const;
401 DITypeRef getClassType() const {
402 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
403 return getFieldAs<DITypeRef>(10);
406 Constant *getConstant() const {
407 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
408 return getConstantField(10);
411 /// Verify - Verify that a derived type descriptor is well formed.
415 /// DICompositeType - This descriptor holds a type that can refer to multiple
416 /// other types, like a function or struct.
417 /// DICompositeType is derived from DIDerivedType because some
418 /// composite types (such as enums) can be derived from basic types
419 // FIXME: Make this derive from DIType directly & just store the
420 // base type in a single DIType field.
421 class DICompositeType : public DIDerivedType {
422 friend class DIDescriptor;
423 void printInternal(raw_ostream &OS) const;
426 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
428 DIArray getElements() const { return getFieldAs<DIArray>(10); }
429 void setArrays(DIArray Elements, DIArray TParams = DIArray());
430 unsigned getRunTimeLang() const { return getUnsignedField(11); }
431 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
432 void setContainingType(DICompositeType ContainingType);
433 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
434 MDString *getIdentifier() const;
436 /// Verify - Verify that a composite type descriptor is well formed.
440 /// DIFile - This is a wrapper for a file.
441 class DIFile : public DIScope {
442 friend class DIDescriptor;
445 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
446 MDNode *getFileNode() const;
450 /// DICompileUnit - A wrapper for a compile unit.
451 class DICompileUnit : public DIScope {
452 friend class DIDescriptor;
453 void printInternal(raw_ostream &OS) const;
456 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
458 dwarf::SourceLanguage getLanguage() const {
459 return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
461 StringRef getProducer() const { return getStringField(3); }
463 bool isOptimized() const { return getUnsignedField(4) != 0; }
464 StringRef getFlags() const { return getStringField(5); }
465 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
467 DIArray getEnumTypes() const;
468 DIArray getRetainedTypes() const;
469 DIArray getSubprograms() const;
470 DIArray getGlobalVariables() const;
471 DIArray getImportedEntities() const;
473 StringRef getSplitDebugFilename() const { return getStringField(12); }
474 unsigned getEmissionKind() const { return getUnsignedField(13); }
476 /// Verify - Verify that a compile unit is well formed.
480 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
481 class DISubprogram : public DIScope {
482 friend class DIDescriptor;
483 void printInternal(raw_ostream &OS) const;
486 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
488 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
489 StringRef getName() const { return getStringField(3); }
490 StringRef getDisplayName() const { return getStringField(4); }
491 StringRef getLinkageName() const { return getStringField(5); }
492 unsigned getLineNumber() const { return getUnsignedField(6); }
493 DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
495 /// isLocalToUnit - Return true if this subprogram is local to the current
496 /// compile unit, like 'static' in C.
497 unsigned isLocalToUnit() const { return getUnsignedField(8); }
498 unsigned isDefinition() const { return getUnsignedField(9); }
500 unsigned getVirtuality() const { return getUnsignedField(10); }
501 unsigned getVirtualIndex() const { return getUnsignedField(11); }
503 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
505 unsigned getFlags() const { return getUnsignedField(13); }
507 unsigned isArtificial() const {
508 return (getUnsignedField(13) & FlagArtificial) != 0;
510 /// isPrivate - Return true if this subprogram has "private"
511 /// access specifier.
512 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
513 /// isProtected - Return true if this subprogram has "protected"
514 /// access specifier.
515 bool isProtected() const {
516 return (getUnsignedField(13) & FlagProtected) != 0;
518 /// isExplicit - Return true if this subprogram is marked as explicit.
519 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
520 /// isPrototyped - Return true if this subprogram is prototyped.
521 bool isPrototyped() const {
522 return (getUnsignedField(13) & FlagPrototyped) != 0;
525 /// Return true if this subprogram is a C++11 reference-qualified
526 /// non-static member function (void foo() &).
527 unsigned isLValueReference() const {
528 return (getUnsignedField(13) & FlagLValueReference) != 0;
531 /// Return true if this subprogram is a C++11
532 /// rvalue-reference-qualified non-static member function
534 unsigned isRValueReference() const {
535 return (getUnsignedField(13) & FlagRValueReference) != 0;
538 unsigned isOptimized() const;
540 /// Verify - Verify that a subprogram descriptor is well formed.
543 /// describes - Return true if this subprogram provides debugging
544 /// information for the function F.
545 bool describes(const Function *F);
547 Function *getFunction() const { return getFunctionField(15); }
548 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
549 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
550 DISubprogram getFunctionDeclaration() const {
551 return getFieldAs<DISubprogram>(17);
553 MDNode *getVariablesNodes() const;
554 DIArray getVariables() const;
556 /// getScopeLineNumber - Get the beginning of the scope of the
557 /// function, not necessarily where the name of the program
559 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
562 /// DILexicalBlock - This is a wrapper for a lexical block.
563 class DILexicalBlock : public DIScope {
565 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
566 DIScope getContext() const { return getFieldAs<DIScope>(2); }
567 unsigned getLineNumber() const { return getUnsignedField(3); }
568 unsigned getColumnNumber() const { return getUnsignedField(4); }
569 unsigned getDiscriminator() const { return getUnsignedField(5); }
573 /// DILexicalBlockFile - This is a wrapper for a lexical block with
574 /// a filename change.
575 class DILexicalBlockFile : public DIScope {
577 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
578 DIScope getContext() const {
579 if (getScope().isSubprogram())
581 return getScope().getContext();
583 unsigned getLineNumber() const { return getScope().getLineNumber(); }
584 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
585 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
589 /// DINameSpace - A wrapper for a C++ style name space.
590 class DINameSpace : public DIScope {
591 friend class DIDescriptor;
592 void printInternal(raw_ostream &OS) const;
595 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
596 DIScope getContext() const { return getFieldAs<DIScope>(2); }
597 StringRef getName() const { return getStringField(3); }
598 unsigned getLineNumber() const { return getUnsignedField(4); }
602 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
603 class DITemplateTypeParameter : public DIDescriptor {
605 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
608 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
609 StringRef getName() const { return getStringField(2); }
610 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
611 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
612 StringRef getDirectory() const {
613 return getFieldAs<DIFile>(4).getDirectory();
615 unsigned getLineNumber() const { return getUnsignedField(5); }
616 unsigned getColumnNumber() const { return getUnsignedField(6); }
620 /// DITemplateValueParameter - This is a wrapper for template value parameter.
621 class DITemplateValueParameter : public DIDescriptor {
623 explicit DITemplateValueParameter(const MDNode *N = nullptr)
626 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
627 StringRef getName() const { return getStringField(2); }
628 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
629 Value *getValue() const;
630 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
631 StringRef getDirectory() const {
632 return getFieldAs<DIFile>(5).getDirectory();
634 unsigned getLineNumber() const { return getUnsignedField(6); }
635 unsigned getColumnNumber() const { return getUnsignedField(7); }
639 /// DIGlobalVariable - This is a wrapper for a global variable.
640 class DIGlobalVariable : public DIDescriptor {
641 friend class DIDescriptor;
642 void printInternal(raw_ostream &OS) const;
645 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
647 DIScope getContext() const { return getFieldAs<DIScope>(2); }
648 StringRef getName() const { return getStringField(3); }
649 StringRef getDisplayName() const { return getStringField(4); }
650 StringRef getLinkageName() const { return getStringField(5); }
651 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
652 StringRef getDirectory() const {
653 return getFieldAs<DIFile>(6).getDirectory();
656 unsigned getLineNumber() const { return getUnsignedField(7); }
657 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
658 unsigned isLocalToUnit() const { return getUnsignedField(9); }
659 unsigned isDefinition() const { return getUnsignedField(10); }
661 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
662 Constant *getConstant() const { return getConstantField(11); }
663 DIDerivedType getStaticDataMemberDeclaration() const {
664 return getFieldAs<DIDerivedType>(12);
667 /// Verify - Verify that a global variable descriptor is well formed.
671 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
673 class DIVariable : public DIDescriptor {
674 friend class DIDescriptor;
675 void printInternal(raw_ostream &OS) const;
678 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
680 DIScope getContext() const { return getFieldAs<DIScope>(1); }
681 StringRef getName() const { return getStringField(2); }
682 DIFile getFile() const { return getFieldAs<DIFile>(3); }
683 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
684 unsigned getArgNumber() const {
685 unsigned L = getUnsignedField(4);
688 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
690 /// isArtificial - Return true if this variable is marked as "artificial".
691 bool isArtificial() const {
692 return (getUnsignedField(6) & FlagArtificial) != 0;
695 bool isObjectPointer() const {
696 return (getUnsignedField(6) & FlagObjectPointer) != 0;
699 /// \brief Return true if this variable is represented as a pointer.
700 bool isIndirect() const {
701 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
704 /// getInlinedAt - If this variable is inlined then return inline location.
705 MDNode *getInlinedAt() const;
707 /// Verify - Verify that a variable descriptor is well formed.
710 /// HasComplexAddr - Return true if the variable has a complex address.
711 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
713 /// \brief Return the size of this variable's complex address or
714 /// zero if there is none.
715 unsigned getNumAddrElements() const {
716 if (DbgNode->getNumOperands() < 9)
718 return getDescriptorField(8)->getNumOperands();
721 /// \brief return the Idx'th complex address element.
722 uint64_t getAddrElement(unsigned Idx) const;
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 void printExtendedName(raw_ostream &OS) const;
737 /// DILocation - This object holds location information. This object
738 /// is not associated with any DWARF tag.
739 class DILocation : public DIDescriptor {
741 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
743 unsigned getLineNumber() const { return getUnsignedField(0); }
744 unsigned getColumnNumber() const { return getUnsignedField(1); }
745 DIScope getScope() const { return getFieldAs<DIScope>(2); }
746 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
747 StringRef getFilename() const { return getScope().getFilename(); }
748 StringRef getDirectory() const { return getScope().getDirectory(); }
750 bool atSameLineAs(const DILocation &Other) const {
751 return (getLineNumber() == Other.getLineNumber() &&
752 getFilename() == Other.getFilename());
754 /// getDiscriminator - DWARF discriminators are used to distinguish
755 /// identical file locations for instructions that are on different
756 /// basic blocks. If two instructions are inside the same lexical block
757 /// and are in different basic blocks, we create a new lexical block
758 /// with identical location as the original but with a different
759 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
761 unsigned getDiscriminator() const {
762 // Since discriminators are associated with lexical blocks, make
763 // sure this location is a lexical block before retrieving its
765 return getScope().isLexicalBlock()
766 ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
769 unsigned computeNewDiscriminator(LLVMContext &Ctx);
770 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
773 class DIObjCProperty : public DIDescriptor {
774 friend class DIDescriptor;
775 void printInternal(raw_ostream &OS) const;
778 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
780 StringRef getObjCPropertyName() const { return getStringField(1); }
781 DIFile getFile() const { return getFieldAs<DIFile>(2); }
782 unsigned getLineNumber() const { return getUnsignedField(3); }
784 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
785 StringRef getObjCPropertySetterName() const { return getStringField(5); }
786 bool isReadOnlyObjCProperty() const {
787 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
789 bool isReadWriteObjCProperty() const {
790 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
792 bool isAssignObjCProperty() const {
793 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
795 bool isRetainObjCProperty() const {
796 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
798 bool isCopyObjCProperty() const {
799 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
801 bool isNonAtomicObjCProperty() const {
802 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
805 /// Objective-C doesn't have an ODR, so there is no benefit in storing
806 /// the type as a DITypeRef here.
807 DIType getType() const { return getFieldAs<DIType>(7); }
809 /// Verify - Verify that a derived type descriptor is well formed.
813 /// \brief An imported module (C++ using directive or similar).
814 class DIImportedEntity : public DIDescriptor {
815 friend class DIDescriptor;
816 void printInternal(raw_ostream &OS) const;
819 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
820 DIScope getContext() const { return getFieldAs<DIScope>(1); }
821 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
822 unsigned getLineNumber() const { return getUnsignedField(3); }
823 StringRef getName() const { return getStringField(4); }
827 /// getDISubprogram - Find subprogram that is enclosing this scope.
828 DISubprogram getDISubprogram(const MDNode *Scope);
830 /// getDICompositeType - Find underlying composite type.
831 DICompositeType getDICompositeType(DIType T);
833 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
834 /// to hold function specific information.
835 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
837 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
838 /// suitable to hold function specific information.
839 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
841 /// createInlinedVariable - Create a new inlined variable based on current
843 /// @param DV Current Variable.
844 /// @param InlinedScope Location at current variable is inlined.
845 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
846 LLVMContext &VMContext);
848 /// cleanseInlinedVariable - Remove inlined scope from the variable.
849 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
851 /// Construct DITypeIdentifierMap by going through retained types of each CU.
852 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
854 /// Strip debug info in the module if it exists.
855 /// To do this, we remove all calls to the debugger intrinsics and any named
856 /// metadata for debugging. We also remove debug locations for instructions.
857 /// Return true if module is modified.
858 bool StripDebugInfo(Module &M);
860 /// Return Debug Info Metadata Version by checking module flags.
861 unsigned getDebugMetadataVersionFromModule(const Module &M);
863 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
864 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
865 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
866 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
867 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
869 class DebugInfoFinder {
871 DebugInfoFinder() : TypeMapInitialized(false) {}
873 /// processModule - Process entire module and collect debug info
875 void processModule(const Module &M);
877 /// processDeclare - Process DbgDeclareInst.
878 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
879 /// Process DbgValueInst.
880 void processValue(const Module &M, const DbgValueInst *DVI);
881 /// processLocation - Process DILocation.
882 void processLocation(const Module &M, DILocation Loc);
888 /// Initialize TypeIdentifierMap.
889 void InitializeTypeMap(const Module &M);
891 /// processType - Process DIType.
892 void processType(DIType DT);
894 /// processSubprogram - Process DISubprogram.
895 void processSubprogram(DISubprogram SP);
897 void processScope(DIScope Scope);
899 /// addCompileUnit - Add compile unit into CUs.
900 bool addCompileUnit(DICompileUnit CU);
902 /// addGlobalVariable - Add global variable into GVs.
903 bool addGlobalVariable(DIGlobalVariable DIG);
905 // addSubprogram - Add subprogram into SPs.
906 bool addSubprogram(DISubprogram SP);
908 /// addType - Add type into Tys.
909 bool addType(DIType DT);
911 bool addScope(DIScope Scope);
914 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
915 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
916 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
917 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
918 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
920 iterator_range<compile_unit_iterator> compile_units() const {
921 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
924 iterator_range<subprogram_iterator> subprograms() const {
925 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
928 iterator_range<global_variable_iterator> global_variables() const {
929 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
932 iterator_range<type_iterator> types() const {
933 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
936 iterator_range<scope_iterator> scopes() const {
937 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
940 unsigned compile_unit_count() const { return CUs.size(); }
941 unsigned global_variable_count() const { return GVs.size(); }
942 unsigned subprogram_count() const { return SPs.size(); }
943 unsigned type_count() const { return TYs.size(); }
944 unsigned scope_count() const { return Scopes.size(); }
947 SmallVector<DICompileUnit, 8> CUs; // Compile Units
948 SmallVector<DISubprogram, 8> SPs; // Subprograms
949 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
950 SmallVector<DIType, 8> TYs; // Types
951 SmallVector<DIScope, 8> Scopes; // Scopes
952 SmallPtrSet<MDNode *, 64> NodesSeen;
953 DITypeIdentifierMap TypeIdentifierMap;
954 /// Specify if TypeIdentifierMap is initialized.
955 bool TypeMapInitialized;
958 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
960 } // end namespace llvm