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 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 = nullptr) : 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 = nullptr) : 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 = nullptr) : 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 = nullptr) : 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 = nullptr) : 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(LLVMContext &VMContext, 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 = nullptr) : 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 = nullptr) : 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 = nullptr) : 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 = nullptr) : 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 = nullptr) : DIScope(N) {}
430 dwarf::SourceLanguage getLanguage() const {
431 return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
433 StringRef getProducer() const { return getStringField(3); }
435 bool isOptimized() const { return getUnsignedField(4) != 0; }
436 StringRef getFlags() const { return getStringField(5); }
437 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
439 DIArray getEnumTypes() const;
440 DIArray getRetainedTypes() const;
441 DIArray getSubprograms() const;
442 DIArray getGlobalVariables() const;
443 DIArray getImportedEntities() const;
445 StringRef getSplitDebugFilename() const { return getStringField(12); }
446 unsigned getEmissionKind() const { return getUnsignedField(13); }
448 /// Verify - Verify that a compile unit is well formed.
452 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
453 class DISubprogram : public DIScope {
454 friend class DIDescriptor;
455 void printInternal(raw_ostream &OS) const;
458 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
460 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
461 StringRef getName() const { return getStringField(3); }
462 StringRef getDisplayName() const { return getStringField(4); }
463 StringRef getLinkageName() const { return getStringField(5); }
464 unsigned getLineNumber() const { return getUnsignedField(6); }
465 DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
467 /// isLocalToUnit - Return true if this subprogram is local to the current
468 /// compile unit, like 'static' in C.
469 unsigned isLocalToUnit() const { return getUnsignedField(8); }
470 unsigned isDefinition() const { return getUnsignedField(9); }
472 unsigned getVirtuality() const { return getUnsignedField(10); }
473 unsigned getVirtualIndex() const { return getUnsignedField(11); }
475 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
477 unsigned getFlags() const { return getUnsignedField(13); }
479 unsigned isArtificial() const {
480 return (getUnsignedField(13) & FlagArtificial) != 0;
482 /// isPrivate - Return true if this subprogram has "private"
483 /// access specifier.
484 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
485 /// isProtected - Return true if this subprogram has "protected"
486 /// access specifier.
487 bool isProtected() const {
488 return (getUnsignedField(13) & FlagProtected) != 0;
490 /// isExplicit - Return true if this subprogram is marked as explicit.
491 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
492 /// isPrototyped - Return true if this subprogram is prototyped.
493 bool isPrototyped() const {
494 return (getUnsignedField(13) & FlagPrototyped) != 0;
497 /// Return true if this subprogram is a C++11 reference-qualified
498 /// non-static member function (void foo() &).
499 unsigned isLValueReference() const {
500 return (getUnsignedField(13) & FlagLValueReference) != 0;
503 /// Return true if this subprogram is a C++11
504 /// rvalue-reference-qualified non-static member function
506 unsigned isRValueReference() const {
507 return (getUnsignedField(13) & FlagRValueReference) != 0;
510 unsigned isOptimized() const;
512 /// Verify - Verify that a subprogram descriptor is well formed.
515 /// describes - Return true if this subprogram provides debugging
516 /// information for the function F.
517 bool describes(const Function *F);
519 Function *getFunction() const { return getFunctionField(15); }
520 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
521 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
522 DISubprogram getFunctionDeclaration() const {
523 return getFieldAs<DISubprogram>(17);
525 MDNode *getVariablesNodes() const;
526 DIArray getVariables() const;
528 /// getScopeLineNumber - Get the beginning of the scope of the
529 /// function, not necessarily where the name of the program
531 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
534 /// DILexicalBlock - This is a wrapper for a lexical block.
535 class DILexicalBlock : public DIScope {
537 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
538 DIScope getContext() const { return getFieldAs<DIScope>(2); }
539 unsigned getLineNumber() const { return getUnsignedField(3); }
540 unsigned getColumnNumber() const { return getUnsignedField(4); }
541 unsigned getDiscriminator() const { return getUnsignedField(5); }
545 /// DILexicalBlockFile - This is a wrapper for a lexical block with
546 /// a filename change.
547 class DILexicalBlockFile : public DIScope {
549 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
550 DIScope getContext() const {
551 if (getScope().isSubprogram())
553 return getScope().getContext();
555 unsigned getLineNumber() const { return getScope().getLineNumber(); }
556 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
557 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
561 /// DINameSpace - A wrapper for a C++ style name space.
562 class DINameSpace : public DIScope {
563 friend class DIDescriptor;
564 void printInternal(raw_ostream &OS) const;
567 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
568 DIScope getContext() const { return getFieldAs<DIScope>(2); }
569 StringRef getName() const { return getStringField(3); }
570 unsigned getLineNumber() const { return getUnsignedField(4); }
574 /// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
575 class DIUnspecifiedParameter : public DIDescriptor {
577 explicit DIUnspecifiedParameter(const MDNode *N = nullptr)
582 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
583 class DITemplateTypeParameter : public DIDescriptor {
585 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
588 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
589 StringRef getName() const { return getStringField(2); }
590 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
591 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
592 StringRef getDirectory() const {
593 return getFieldAs<DIFile>(4).getDirectory();
595 unsigned getLineNumber() const { return getUnsignedField(5); }
596 unsigned getColumnNumber() const { return getUnsignedField(6); }
600 /// DITemplateValueParameter - This is a wrapper for template value parameter.
601 class DITemplateValueParameter : public DIDescriptor {
603 explicit DITemplateValueParameter(const MDNode *N = nullptr)
606 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
607 StringRef getName() const { return getStringField(2); }
608 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
609 Value *getValue() const;
610 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
611 StringRef getDirectory() const {
612 return getFieldAs<DIFile>(5).getDirectory();
614 unsigned getLineNumber() const { return getUnsignedField(6); }
615 unsigned getColumnNumber() const { return getUnsignedField(7); }
619 /// DIGlobalVariable - This is a wrapper for a global variable.
620 class DIGlobalVariable : public DIDescriptor {
621 friend class DIDescriptor;
622 void printInternal(raw_ostream &OS) const;
625 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
627 DIScope getContext() const { return getFieldAs<DIScope>(2); }
628 StringRef getName() const { return getStringField(3); }
629 StringRef getDisplayName() const { return getStringField(4); }
630 StringRef getLinkageName() const { return getStringField(5); }
631 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
632 StringRef getDirectory() const {
633 return getFieldAs<DIFile>(6).getDirectory();
636 unsigned getLineNumber() const { return getUnsignedField(7); }
637 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
638 unsigned isLocalToUnit() const { return getUnsignedField(9); }
639 unsigned isDefinition() const { return getUnsignedField(10); }
641 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
642 Constant *getConstant() const { return getConstantField(11); }
643 DIDerivedType getStaticDataMemberDeclaration() const {
644 return getFieldAs<DIDerivedType>(12);
647 /// Verify - Verify that a global variable descriptor is well formed.
651 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
653 class DIVariable : public DIDescriptor {
654 friend class DIDescriptor;
655 void printInternal(raw_ostream &OS) const;
658 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
660 DIScope getContext() const { return getFieldAs<DIScope>(1); }
661 StringRef getName() const { return getStringField(2); }
662 DIFile getFile() const { return getFieldAs<DIFile>(3); }
663 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
664 unsigned getArgNumber() const {
665 unsigned L = getUnsignedField(4);
668 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
670 /// isArtificial - Return true if this variable is marked as "artificial".
671 bool isArtificial() const {
672 return (getUnsignedField(6) & FlagArtificial) != 0;
675 bool isObjectPointer() const {
676 return (getUnsignedField(6) & FlagObjectPointer) != 0;
679 /// \brief Return true if this variable is represented as a pointer.
680 bool isIndirect() const {
681 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
684 /// getInlinedAt - If this variable is inlined then return inline location.
685 MDNode *getInlinedAt() const;
687 /// Verify - Verify that a variable descriptor is well formed.
690 /// HasComplexAddr - Return true if the variable has a complex address.
691 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
693 /// \brief Return the size of this variable's complex address or
694 /// zero if there is none.
695 unsigned getNumAddrElements() const {
696 if (DbgNode->getNumOperands() < 9)
698 return getDescriptorField(8)->getNumOperands();
701 /// \brief return the Idx'th complex address element.
702 uint64_t getAddrElement(unsigned Idx) const;
704 /// isBlockByrefVariable - Return true if the variable was declared as
705 /// a "__block" variable (Apple Blocks).
706 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
707 return (getType().resolve(Map)).isBlockByrefStruct();
710 /// isInlinedFnArgument - Return true if this variable provides debugging
711 /// information for an inlined function arguments.
712 bool isInlinedFnArgument(const Function *CurFn);
714 void printExtendedName(raw_ostream &OS) const;
717 /// DILocation - This object holds location information. This object
718 /// is not associated with any DWARF tag.
719 class DILocation : public DIDescriptor {
721 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
723 unsigned getLineNumber() const { return getUnsignedField(0); }
724 unsigned getColumnNumber() const { return getUnsignedField(1); }
725 DIScope getScope() const { return getFieldAs<DIScope>(2); }
726 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
727 StringRef getFilename() const { return getScope().getFilename(); }
728 StringRef getDirectory() const { return getScope().getDirectory(); }
730 bool atSameLineAs(const DILocation &Other) const {
731 return (getLineNumber() == Other.getLineNumber() &&
732 getFilename() == Other.getFilename());
734 /// getDiscriminator - DWARF discriminators are used to distinguish
735 /// identical file locations for instructions that are on different
736 /// basic blocks. If two instructions are inside the same lexical block
737 /// and are in different basic blocks, we create a new lexical block
738 /// with identical location as the original but with a different
739 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
741 unsigned getDiscriminator() const {
742 // Since discriminators are associated with lexical blocks, make
743 // sure this location is a lexical block before retrieving its
745 return getScope().isLexicalBlock()
746 ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
749 unsigned computeNewDiscriminator(LLVMContext &Ctx);
750 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
753 class DIObjCProperty : public DIDescriptor {
754 friend class DIDescriptor;
755 void printInternal(raw_ostream &OS) const;
758 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
760 StringRef getObjCPropertyName() const { return getStringField(1); }
761 DIFile getFile() const { return getFieldAs<DIFile>(2); }
762 unsigned getLineNumber() const { return getUnsignedField(3); }
764 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
765 StringRef getObjCPropertySetterName() const { return getStringField(5); }
766 bool isReadOnlyObjCProperty() const {
767 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
769 bool isReadWriteObjCProperty() const {
770 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
772 bool isAssignObjCProperty() const {
773 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
775 bool isRetainObjCProperty() const {
776 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
778 bool isCopyObjCProperty() const {
779 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
781 bool isNonAtomicObjCProperty() const {
782 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
785 /// Objective-C doesn't have an ODR, so there is no benefit in storing
786 /// the type as a DITypeRef here.
787 DIType getType() const { return getFieldAs<DIType>(7); }
789 /// Verify - Verify that a derived type descriptor is well formed.
793 /// \brief An imported module (C++ using directive or similar).
794 class DIImportedEntity : public DIDescriptor {
795 friend class DIDescriptor;
796 void printInternal(raw_ostream &OS) const;
799 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
800 DIScope getContext() const { return getFieldAs<DIScope>(1); }
801 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
802 unsigned getLineNumber() const { return getUnsignedField(3); }
803 StringRef getName() const { return getStringField(4); }
807 /// getDISubprogram - Find subprogram that is enclosing this scope.
808 DISubprogram getDISubprogram(const MDNode *Scope);
810 /// getDICompositeType - Find underlying composite type.
811 DICompositeType getDICompositeType(DIType T);
813 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
814 /// to hold function specific information.
815 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
817 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
818 /// suitable to hold function specific information.
819 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
821 /// createInlinedVariable - Create a new inlined variable based on current
823 /// @param DV Current Variable.
824 /// @param InlinedScope Location at current variable is inlined.
825 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
826 LLVMContext &VMContext);
828 /// cleanseInlinedVariable - Remove inlined scope from the variable.
829 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
831 /// Construct DITypeIdentifierMap by going through retained types of each CU.
832 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
834 /// Strip debug info in the module if it exists.
835 /// To do this, we remove all calls to the debugger intrinsics and any named
836 /// metadata for debugging. We also remove debug locations for instructions.
837 /// Return true if module is modified.
838 bool StripDebugInfo(Module &M);
840 /// Return Debug Info Metadata Version by checking module flags.
841 unsigned getDebugMetadataVersionFromModule(const Module &M);
843 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
844 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
845 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
846 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
847 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
849 class DebugInfoFinder {
851 DebugInfoFinder() : TypeMapInitialized(false) {}
853 /// processModule - Process entire module and collect debug info
855 void processModule(const Module &M);
857 /// processDeclare - Process DbgDeclareInst.
858 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
859 /// Process DbgValueInst.
860 void processValue(const Module &M, const DbgValueInst *DVI);
861 /// processLocation - Process DILocation.
862 void processLocation(const Module &M, DILocation Loc);
868 /// Initialize TypeIdentifierMap.
869 void InitializeTypeMap(const Module &M);
871 /// processType - Process DIType.
872 void processType(DIType DT);
874 /// processSubprogram - Process DISubprogram.
875 void processSubprogram(DISubprogram SP);
877 void processScope(DIScope Scope);
879 /// addCompileUnit - Add compile unit into CUs.
880 bool addCompileUnit(DICompileUnit CU);
882 /// addGlobalVariable - Add global variable into GVs.
883 bool addGlobalVariable(DIGlobalVariable DIG);
885 // addSubprogram - Add subprogram into SPs.
886 bool addSubprogram(DISubprogram SP);
888 /// addType - Add type into Tys.
889 bool addType(DIType DT);
891 bool addScope(DIScope Scope);
894 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
895 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
896 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
897 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
898 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
900 iterator_range<compile_unit_iterator> compile_units() const {
901 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
904 iterator_range<subprogram_iterator> subprograms() const {
905 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
908 iterator_range<global_variable_iterator> global_variables() const {
909 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
912 iterator_range<type_iterator> types() const {
913 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
916 iterator_range<scope_iterator> scopes() const {
917 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
920 unsigned compile_unit_count() const { return CUs.size(); }
921 unsigned global_variable_count() const { return GVs.size(); }
922 unsigned subprogram_count() const { return SPs.size(); }
923 unsigned type_count() const { return TYs.size(); }
924 unsigned scope_count() const { return Scopes.size(); }
927 SmallVector<DICompileUnit, 8> CUs; // Compile Units
928 SmallVector<DISubprogram, 8> SPs; // Subprograms
929 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
930 SmallVector<DIType, 8> TYs; // Types
931 SmallVector<DIScope, 8> Scopes; // Scopes
932 SmallPtrSet<MDNode *, 64> NodesSeen;
933 DITypeIdentifierMap TypeIdentifierMap;
934 /// Specify if TypeIdentifierMap is initialized.
935 bool TypeMapInitialized;
938 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
940 } // end namespace llvm