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 = 0) : 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 != 0; }
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 isVariable() const;
132 bool isSubprogram() const;
133 bool isGlobalVariable() const;
134 bool isScope() const;
136 bool isCompileUnit() const;
137 bool isNameSpace() const;
138 bool isLexicalBlockFile() const;
139 bool isLexicalBlock() const;
140 bool isSubrange() const;
141 bool isEnumerator() const;
143 bool isUnspecifiedParameter() const;
144 bool isTemplateTypeParameter() const;
145 bool isTemplateValueParameter() const;
146 bool isObjCProperty() const;
147 bool isImportedEntity() const;
149 /// print - print descriptor.
150 void print(raw_ostream &OS) const;
152 /// dump - print descriptor to dbgs() with a newline.
156 /// DISubrange - This is used to represent ranges, for array bounds.
157 class DISubrange : public DIDescriptor {
158 friend class DIDescriptor;
159 void printInternal(raw_ostream &OS) const;
162 explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
164 int64_t getLo() const { return getInt64Field(1); }
165 int64_t getCount() const { return getInt64Field(2); }
169 /// DIArray - This descriptor holds an array of descriptors.
170 class DIArray : public DIDescriptor {
172 explicit DIArray(const MDNode *N = 0) : DIDescriptor(N) {}
174 unsigned getNumElements() const;
175 DIDescriptor getElement(unsigned Idx) const {
176 return getDescriptorField(Idx);
180 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
181 /// FIXME: it seems strange that this doesn't have either a reference to the
182 /// type/precision or a file/line pair for location info.
183 class DIEnumerator : public DIDescriptor {
184 friend class DIDescriptor;
185 void printInternal(raw_ostream &OS) const;
188 explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
190 StringRef getName() const { return getStringField(1); }
191 int64_t getEnumValue() const { return getInt64Field(2); }
195 template <typename T> class DIRef;
196 typedef DIRef<DIScope> DIScopeRef;
197 typedef DIRef<DIType> DITypeRef;
199 /// DIScope - A base class for various scopes.
201 /// Although, implementation-wise, DIScope is the parent class of most
202 /// other DIxxx classes, including DIType and its descendants, most of
203 /// DIScope's descendants are not a substitutable subtype of
204 /// DIScope. The DIDescriptor::isScope() method only is true for
205 /// DIScopes that are scopes in the strict lexical scope sense
206 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
207 class DIScope : public DIDescriptor {
209 friend class DIDescriptor;
210 void printInternal(raw_ostream &OS) const;
213 explicit DIScope(const MDNode *N = 0) : DIDescriptor(N) {}
215 /// Gets the parent scope for this scope node or returns a
216 /// default constructed scope.
217 DIScopeRef getContext() const;
218 /// If the scope node has a name, return that, else return an empty string.
219 StringRef getName() const;
220 StringRef getFilename() const;
221 StringRef getDirectory() const;
223 /// Generate a reference to this DIScope. Uses the type identifier instead
224 /// of the actual MDNode if possible, to help type uniquing.
225 DIScopeRef getRef() const;
228 /// Represents reference to a DIDescriptor, abstracts over direct and
229 /// identifier-based metadata references.
230 template <typename T> class DIRef {
231 template <typename DescTy>
232 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
233 friend DIScopeRef DIScope::getContext() const;
234 friend DIScopeRef DIScope::getRef() const;
237 /// Val can be either a MDNode or a MDString, in the latter,
238 /// MDString specifies the type identifier.
240 explicit DIRef(const Value *V);
243 T resolve(const DITypeIdentifierMap &Map) const;
244 StringRef getName() const;
245 operator Value *() const { return const_cast<Value *>(Val); }
248 template <typename T>
249 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
253 if (const MDNode *MD = dyn_cast<MDNode>(Val))
256 const MDString *MS = cast<MDString>(Val);
257 // Find the corresponding MDNode.
258 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
259 assert(Iter != Map.end() && "Identifier not in the type map?");
260 assert(DIDescriptor(Iter->second).isType() &&
261 "MDNode in DITypeIdentifierMap should be a DIType.");
262 return T(Iter->second);
265 template <typename T> StringRef DIRef<T>::getName() const {
269 if (const MDNode *MD = dyn_cast<MDNode>(Val))
270 return T(MD).getName();
272 const MDString *MS = cast<MDString>(Val);
273 return MS->getString();
276 /// Specialize getFieldAs to handle fields that are references to DIScopes.
277 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
278 /// Specialize DIRef constructor for DIScopeRef.
279 template <> DIRef<DIScope>::DIRef(const Value *V);
281 /// Specialize getFieldAs to handle fields that are references to DITypes.
282 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
283 /// Specialize DIRef constructor for DITypeRef.
284 template <> DIRef<DIType>::DIRef(const Value *V);
286 /// DIType - This is a wrapper for a type.
287 /// FIXME: Types should be factored much better so that CV qualifiers and
288 /// others do not require a huge and empty descriptor full of zeros.
289 class DIType : public DIScope {
291 friend class DIDescriptor;
292 void printInternal(raw_ostream &OS) const;
295 explicit DIType(const MDNode *N = 0) : DIScope(N) {}
296 operator DITypeRef () const {
298 "constructing DITypeRef from an MDNode that is not a type");
299 return DITypeRef(&*getRef());
302 /// Verify - Verify that a type descriptor is well formed.
305 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
306 StringRef getName() const { return getStringField(3); }
307 unsigned getLineNumber() const { return getUnsignedField(4); }
308 uint64_t getSizeInBits() const { return getUInt64Field(5); }
309 uint64_t getAlignInBits() const { return getUInt64Field(6); }
310 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
311 // carry this is just plain insane.
312 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
313 unsigned getFlags() const { return getUnsignedField(8); }
314 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
315 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
316 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
317 // isAppleBlock - Return true if this is the Apple Blocks extension.
318 bool isAppleBlockExtension() const {
319 return (getFlags() & FlagAppleBlock) != 0;
321 bool isBlockByrefStruct() const {
322 return (getFlags() & FlagBlockByrefStruct) != 0;
324 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
325 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
326 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
327 bool isObjcClassComplete() const {
328 return (getFlags() & FlagObjcClassComplete) != 0;
330 bool isVector() const { return (getFlags() & FlagVector) != 0; }
331 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
332 bool isLValueReference() const {
333 return (getFlags() & FlagLValueReference) != 0;
335 bool isRValueReference() const {
336 return (getFlags() & FlagRValueReference) != 0;
338 bool isValid() const { return DbgNode && isType(); }
340 /// replaceAllUsesWith - Replace all uses of debug info referenced by
342 void replaceAllUsesWith(DIDescriptor &D);
343 void replaceAllUsesWith(MDNode *D);
346 /// DIBasicType - A basic type, like 'int' or 'float'.
347 class DIBasicType : public DIType {
349 explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
351 unsigned getEncoding() const { return getUnsignedField(9); }
353 /// Verify - Verify that a basic type descriptor is well formed.
357 /// DIDerivedType - A simple derived type, like a const qualified type,
358 /// a typedef, a pointer or reference, et cetera. Or, a data member of
359 /// a class/struct/union.
360 class DIDerivedType : public DIType {
361 friend class DIDescriptor;
362 void printInternal(raw_ostream &OS) const;
365 explicit DIDerivedType(const MDNode *N = 0) : DIType(N) {}
367 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
369 /// getObjCProperty - Return property node, if this ivar is
370 /// associated with one.
371 MDNode *getObjCProperty() const;
373 DITypeRef getClassType() const {
374 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
375 return getFieldAs<DITypeRef>(10);
378 Constant *getConstant() const {
379 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
380 return getConstantField(10);
383 /// Verify - Verify that a derived type descriptor is well formed.
387 /// DICompositeType - This descriptor holds a type that can refer to multiple
388 /// other types, like a function or struct.
389 /// DICompositeType is derived from DIDerivedType because some
390 /// composite types (such as enums) can be derived from basic types
391 // FIXME: Make this derive from DIType directly & just store the
392 // base type in a single DIType field.
393 class DICompositeType : public DIDerivedType {
394 friend class DIDescriptor;
395 void printInternal(raw_ostream &OS) const;
398 explicit DICompositeType(const MDNode *N = 0) : DIDerivedType(N) {}
400 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
401 void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
402 unsigned getRunTimeLang() const { return getUnsignedField(11); }
403 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
404 void setContainingType(DICompositeType ContainingType);
405 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
406 MDString *getIdentifier() const;
408 /// Verify - Verify that a composite type descriptor is well formed.
412 /// DIFile - This is a wrapper for a file.
413 class DIFile : public DIScope {
414 friend class DIDescriptor;
417 explicit DIFile(const MDNode *N = 0) : DIScope(N) {}
418 MDNode *getFileNode() const;
422 /// DICompileUnit - A wrapper for a compile unit.
423 class DICompileUnit : public DIScope {
424 friend class DIDescriptor;
425 void printInternal(raw_ostream &OS) const;
428 explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
430 unsigned getLanguage() const { return getUnsignedField(2); }
431 StringRef getProducer() const { return getStringField(3); }
433 bool isOptimized() const { return getUnsignedField(4) != 0; }
434 StringRef getFlags() const { return getStringField(5); }
435 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
437 DIArray getEnumTypes() const;
438 DIArray getRetainedTypes() const;
439 DIArray getSubprograms() const;
440 DIArray getGlobalVariables() const;
441 DIArray getImportedEntities() const;
443 StringRef getSplitDebugFilename() const { return getStringField(12); }
444 unsigned getEmissionKind() const { return getUnsignedField(13); }
446 /// Verify - Verify that a compile unit is well formed.
450 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
451 class DISubprogram : public DIScope {
452 friend class DIDescriptor;
453 void printInternal(raw_ostream &OS) const;
456 explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
458 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
459 StringRef getName() const { return getStringField(3); }
460 StringRef getDisplayName() const { return getStringField(4); }
461 StringRef getLinkageName() const { return getStringField(5); }
462 unsigned getLineNumber() const { return getUnsignedField(6); }
463 DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
465 /// isLocalToUnit - Return true if this subprogram is local to the current
466 /// compile unit, like 'static' in C.
467 unsigned isLocalToUnit() const { return getUnsignedField(8); }
468 unsigned isDefinition() const { return getUnsignedField(9); }
470 unsigned getVirtuality() const { return getUnsignedField(10); }
471 unsigned getVirtualIndex() const { return getUnsignedField(11); }
473 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
475 unsigned getFlags() const { return getUnsignedField(13); }
477 unsigned isArtificial() const {
478 return (getUnsignedField(13) & FlagArtificial) != 0;
480 /// isPrivate - Return true if this subprogram has "private"
481 /// access specifier.
482 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
483 /// isProtected - Return true if this subprogram has "protected"
484 /// access specifier.
485 bool isProtected() const {
486 return (getUnsignedField(13) & FlagProtected) != 0;
488 /// isExplicit - Return true if this subprogram is marked as explicit.
489 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
490 /// isPrototyped - Return true if this subprogram is prototyped.
491 bool isPrototyped() const {
492 return (getUnsignedField(13) & FlagPrototyped) != 0;
495 /// Return true if this subprogram is a C++11 reference-qualified
496 /// non-static member function (void foo() &).
497 unsigned isLValueReference() const {
498 return (getUnsignedField(13) & FlagLValueReference) != 0;
501 /// Return true if this subprogram is a C++11
502 /// rvalue-reference-qualified non-static member function
504 unsigned isRValueReference() const {
505 return (getUnsignedField(13) & FlagRValueReference) != 0;
508 unsigned isOptimized() const;
510 /// Verify - Verify that a subprogram descriptor is well formed.
513 /// describes - Return true if this subprogram provides debugging
514 /// information for the function F.
515 bool describes(const Function *F);
517 Function *getFunction() const { return getFunctionField(15); }
518 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
519 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
520 DISubprogram getFunctionDeclaration() const {
521 return getFieldAs<DISubprogram>(17);
523 MDNode *getVariablesNodes() const;
524 DIArray getVariables() const;
526 /// getScopeLineNumber - Get the beginning of the scope of the
527 /// function, not necessarily where the name of the program
529 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
532 /// DILexicalBlock - This is a wrapper for a lexical block.
533 class DILexicalBlock : public DIScope {
535 explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
536 DIScope getContext() const { return getFieldAs<DIScope>(2); }
537 unsigned getLineNumber() const { return getUnsignedField(3); }
538 unsigned getColumnNumber() const { return getUnsignedField(4); }
539 unsigned getDiscriminator() const { return getUnsignedField(5); }
543 /// DILexicalBlockFile - This is a wrapper for a lexical block with
544 /// a filename change.
545 class DILexicalBlockFile : public DIScope {
547 explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
548 DIScope getContext() const {
549 if (getScope().isSubprogram())
551 return getScope().getContext();
553 unsigned getLineNumber() const { return getScope().getLineNumber(); }
554 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
555 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
559 /// DINameSpace - A wrapper for a C++ style name space.
560 class DINameSpace : public DIScope {
561 friend class DIDescriptor;
562 void printInternal(raw_ostream &OS) const;
565 explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
566 DIScope getContext() const { return getFieldAs<DIScope>(2); }
567 StringRef getName() const { return getStringField(3); }
568 unsigned getLineNumber() const { return getUnsignedField(4); }
572 /// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
573 class DIUnspecifiedParameter : public DIDescriptor {
575 explicit DIUnspecifiedParameter(const MDNode *N = 0) : DIDescriptor(N) {}
579 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
580 class DITemplateTypeParameter : public DIDescriptor {
582 explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
584 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
585 StringRef getName() const { return getStringField(2); }
586 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
587 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
588 StringRef getDirectory() const {
589 return getFieldAs<DIFile>(4).getDirectory();
591 unsigned getLineNumber() const { return getUnsignedField(5); }
592 unsigned getColumnNumber() const { return getUnsignedField(6); }
596 /// DITemplateValueParameter - This is a wrapper for template value parameter.
597 class DITemplateValueParameter : public DIDescriptor {
599 explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
601 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
602 StringRef getName() const { return getStringField(2); }
603 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
604 Value *getValue() const;
605 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
606 StringRef getDirectory() const {
607 return getFieldAs<DIFile>(5).getDirectory();
609 unsigned getLineNumber() const { return getUnsignedField(6); }
610 unsigned getColumnNumber() const { return getUnsignedField(7); }
614 /// DIGlobalVariable - This is a wrapper for a global variable.
615 class DIGlobalVariable : public DIDescriptor {
616 friend class DIDescriptor;
617 void printInternal(raw_ostream &OS) const;
620 explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
622 DIScope getContext() const { return getFieldAs<DIScope>(2); }
623 StringRef getName() const { return getStringField(3); }
624 StringRef getDisplayName() const { return getStringField(4); }
625 StringRef getLinkageName() const { return getStringField(5); }
626 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
627 StringRef getDirectory() const {
628 return getFieldAs<DIFile>(6).getDirectory();
631 unsigned getLineNumber() const { return getUnsignedField(7); }
632 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
633 unsigned isLocalToUnit() const { return getUnsignedField(9); }
634 unsigned isDefinition() const { return getUnsignedField(10); }
636 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
637 Constant *getConstant() const { return getConstantField(11); }
638 DIDerivedType getStaticDataMemberDeclaration() const {
639 return getFieldAs<DIDerivedType>(12);
642 /// Verify - Verify that a global variable descriptor is well formed.
646 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
648 class DIVariable : public DIDescriptor {
649 friend class DIDescriptor;
650 void printInternal(raw_ostream &OS) const;
653 explicit DIVariable(const MDNode *N = 0) : DIDescriptor(N) {}
655 DIScope getContext() const { return getFieldAs<DIScope>(1); }
656 StringRef getName() const { return getStringField(2); }
657 DIFile getFile() const { return getFieldAs<DIFile>(3); }
658 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
659 unsigned getArgNumber() const {
660 unsigned L = getUnsignedField(4);
663 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
665 /// isArtificial - Return true if this variable is marked as "artificial".
666 bool isArtificial() const {
667 return (getUnsignedField(6) & FlagArtificial) != 0;
670 bool isObjectPointer() const {
671 return (getUnsignedField(6) & FlagObjectPointer) != 0;
674 /// \brief Return true if this variable is represented as a pointer.
675 bool isIndirect() const {
676 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
679 /// getInlinedAt - If this variable is inlined then return inline location.
680 MDNode *getInlinedAt() const;
682 /// Verify - Verify that a variable descriptor is well formed.
685 /// HasComplexAddr - Return true if the variable has a complex address.
686 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
688 unsigned getNumAddrElements() const;
690 uint64_t getAddrElement(unsigned Idx) const {
691 return getUInt64Field(Idx + 8);
694 /// isBlockByrefVariable - Return true if the variable was declared as
695 /// a "__block" variable (Apple Blocks).
696 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
697 return (getType().resolve(Map)).isBlockByrefStruct();
700 /// isInlinedFnArgument - Return true if this variable provides debugging
701 /// information for an inlined function arguments.
702 bool isInlinedFnArgument(const Function *CurFn);
704 void printExtendedName(raw_ostream &OS) const;
707 /// DILocation - This object holds location information. This object
708 /// is not associated with any DWARF tag.
709 class DILocation : public DIDescriptor {
711 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
713 unsigned getLineNumber() const { return getUnsignedField(0); }
714 unsigned getColumnNumber() const { return getUnsignedField(1); }
715 DIScope getScope() const { return getFieldAs<DIScope>(2); }
716 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
717 StringRef getFilename() const { return getScope().getFilename(); }
718 StringRef getDirectory() const { return getScope().getDirectory(); }
720 bool atSameLineAs(const DILocation &Other) const {
721 return (getLineNumber() == Other.getLineNumber() &&
722 getFilename() == Other.getFilename());
724 /// getDiscriminator - DWARF discriminators are used to distinguish
725 /// identical file locations for instructions that are on different
726 /// basic blocks. If two instructions are inside the same lexical block
727 /// and are in different basic blocks, we create a new lexical block
728 /// with identical location as the original but with a different
729 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
731 unsigned getDiscriminator() const {
732 // Since discriminators are associated with lexical blocks, make
733 // sure this location is a lexical block before retrieving its
735 return getScope().isLexicalBlock()
736 ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
739 unsigned computeNewDiscriminator(LLVMContext &Ctx);
740 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
743 class DIObjCProperty : public DIDescriptor {
744 friend class DIDescriptor;
745 void printInternal(raw_ostream &OS) const;
748 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
750 StringRef getObjCPropertyName() const { return getStringField(1); }
751 DIFile getFile() const { return getFieldAs<DIFile>(2); }
752 unsigned getLineNumber() const { return getUnsignedField(3); }
754 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
755 StringRef getObjCPropertySetterName() const { return getStringField(5); }
756 bool isReadOnlyObjCProperty() const {
757 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
759 bool isReadWriteObjCProperty() const {
760 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
762 bool isAssignObjCProperty() const {
763 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
765 bool isRetainObjCProperty() const {
766 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
768 bool isCopyObjCProperty() const {
769 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
771 bool isNonAtomicObjCProperty() const {
772 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
775 /// Objective-C doesn't have an ODR, so there is no benefit in storing
776 /// the type as a DITypeRef here.
777 DIType getType() const { return getFieldAs<DIType>(7); }
779 /// Verify - Verify that a derived type descriptor is well formed.
783 /// \brief An imported module (C++ using directive or similar).
784 class DIImportedEntity : public DIDescriptor {
785 friend class DIDescriptor;
786 void printInternal(raw_ostream &OS) const;
789 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
790 DIScope getContext() const { return getFieldAs<DIScope>(1); }
791 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
792 unsigned getLineNumber() const { return getUnsignedField(3); }
793 StringRef getName() const { return getStringField(4); }
797 /// getDISubprogram - Find subprogram that is enclosing this scope.
798 DISubprogram getDISubprogram(const MDNode *Scope);
800 /// getDICompositeType - Find underlying composite type.
801 DICompositeType getDICompositeType(DIType T);
803 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
804 /// to hold function specific information.
805 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
807 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
808 /// suitable to hold function specific information.
809 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
811 /// createInlinedVariable - Create a new inlined variable based on current
813 /// @param DV Current Variable.
814 /// @param InlinedScope Location at current variable is inlined.
815 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
816 LLVMContext &VMContext);
818 /// cleanseInlinedVariable - Remove inlined scope from the variable.
819 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
821 /// Construct DITypeIdentifierMap by going through retained types of each CU.
822 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
824 /// Strip debug info in the module if it exists.
825 /// To do this, we remove all calls to the debugger intrinsics and any named
826 /// metadata for debugging. We also remove debug locations for instructions.
827 /// Return true if module is modified.
828 bool StripDebugInfo(Module &M);
830 /// Return Debug Info Metadata Version by checking module flags.
831 unsigned getDebugMetadataVersionFromModule(const Module &M);
833 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
834 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
835 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
836 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
837 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
839 class DebugInfoFinder {
841 DebugInfoFinder() : TypeMapInitialized(false) {}
843 /// processModule - Process entire module and collect debug info
845 void processModule(const Module &M);
847 /// processDeclare - Process DbgDeclareInst.
848 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
849 /// Process DbgValueInst.
850 void processValue(const Module &M, const DbgValueInst *DVI);
851 /// processLocation - Process DILocation.
852 void processLocation(const Module &M, DILocation Loc);
858 /// Initialize TypeIdentifierMap.
859 void InitializeTypeMap(const Module &M);
861 /// processType - Process DIType.
862 void processType(DIType DT);
864 /// processSubprogram - Process DISubprogram.
865 void processSubprogram(DISubprogram SP);
867 void processScope(DIScope Scope);
869 /// addCompileUnit - Add compile unit into CUs.
870 bool addCompileUnit(DICompileUnit CU);
872 /// addGlobalVariable - Add global variable into GVs.
873 bool addGlobalVariable(DIGlobalVariable DIG);
875 // addSubprogram - Add subprogram into SPs.
876 bool addSubprogram(DISubprogram SP);
878 /// addType - Add type into Tys.
879 bool addType(DIType DT);
881 bool addScope(DIScope Scope);
884 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
885 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
886 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
887 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
888 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
890 iterator_range<compile_unit_iterator> compile_units() const {
891 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
894 iterator_range<subprogram_iterator> subprograms() const {
895 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
898 iterator_range<global_variable_iterator> global_variables() const {
899 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
902 iterator_range<type_iterator> types() const {
903 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
906 iterator_range<scope_iterator> scopes() const {
907 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
910 unsigned compile_unit_count() const { return CUs.size(); }
911 unsigned global_variable_count() const { return GVs.size(); }
912 unsigned subprogram_count() const { return SPs.size(); }
913 unsigned type_count() const { return TYs.size(); }
914 unsigned scope_count() const { return Scopes.size(); }
917 SmallVector<DICompileUnit, 8> CUs; // Compile Units
918 SmallVector<DISubprogram, 8> SPs; // Subprograms
919 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
920 SmallVector<DIType, 8> TYs; // Types
921 SmallVector<DIScope, 8> Scopes; // Scopes
922 SmallPtrSet<MDNode *, 64> NodesSeen;
923 DITypeIdentifierMap TypeIdentifierMap;
924 /// Specify if TypeIdentifierMap is initialized.
925 bool TypeMapInitialized;
927 } // end namespace llvm