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 isSubroutineType() const;
131 bool isBasicType() 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 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 /// DITypedArray - This descriptor holds an array of nodes with type T.
170 template <typename T> class DITypedArray : public DIDescriptor {
172 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
173 unsigned getNumElements() const {
174 return DbgNode ? DbgNode->getNumOperands() : 0;
176 T getElement(unsigned Idx) const {
177 return getFieldAs<T>(Idx);
181 typedef DITypedArray<DIDescriptor> DIArray;
183 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
184 /// FIXME: it seems strange that this doesn't have either a reference to the
185 /// type/precision or a file/line pair for location info.
186 class DIEnumerator : public DIDescriptor {
187 friend class DIDescriptor;
188 void printInternal(raw_ostream &OS) const;
191 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
193 StringRef getName() const { return getStringField(1); }
194 int64_t getEnumValue() const { return getInt64Field(2); }
198 template <typename T> class DIRef;
199 typedef DIRef<DIScope> DIScopeRef;
200 typedef DIRef<DIType> DITypeRef;
201 typedef DITypedArray<DITypeRef> DITypeArray;
203 /// DIScope - A base class for various scopes.
205 /// Although, implementation-wise, DIScope is the parent class of most
206 /// other DIxxx classes, including DIType and its descendants, most of
207 /// DIScope's descendants are not a substitutable subtype of
208 /// DIScope. The DIDescriptor::isScope() method only is true for
209 /// DIScopes that are scopes in the strict lexical scope sense
210 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
211 class DIScope : public DIDescriptor {
213 friend class DIDescriptor;
214 void printInternal(raw_ostream &OS) const;
217 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
219 /// Gets the parent scope for this scope node or returns a
220 /// default constructed scope.
221 DIScopeRef getContext() const;
222 /// If the scope node has a name, return that, else return an empty string.
223 StringRef getName() const;
224 StringRef getFilename() const;
225 StringRef getDirectory() const;
227 /// Generate a reference to this DIScope. Uses the type identifier instead
228 /// of the actual MDNode if possible, to help type uniquing.
229 DIScopeRef getRef() const;
232 /// Represents reference to a DIDescriptor, abstracts over direct and
233 /// identifier-based metadata references.
234 template <typename T> class DIRef {
235 template <typename DescTy>
236 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
237 friend DIScopeRef DIScope::getContext() const;
238 friend DIScopeRef DIScope::getRef() const;
241 /// Val can be either a MDNode or a MDString, in the latter,
242 /// MDString specifies the type identifier.
244 explicit DIRef(const Value *V);
247 T resolve(const DITypeIdentifierMap &Map) const;
248 StringRef getName() const;
249 operator Value *() const { return const_cast<Value *>(Val); }
252 template <typename T>
253 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
257 if (const MDNode *MD = dyn_cast<MDNode>(Val))
260 const MDString *MS = cast<MDString>(Val);
261 // Find the corresponding MDNode.
262 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
263 assert(Iter != Map.end() && "Identifier not in the type map?");
264 assert(DIDescriptor(Iter->second).isType() &&
265 "MDNode in DITypeIdentifierMap should be a DIType.");
266 return T(Iter->second);
269 template <typename T> StringRef DIRef<T>::getName() const {
273 if (const MDNode *MD = dyn_cast<MDNode>(Val))
274 return T(MD).getName();
276 const MDString *MS = cast<MDString>(Val);
277 return MS->getString();
280 /// Specialize getFieldAs to handle fields that are references to DIScopes.
281 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
282 /// Specialize DIRef constructor for DIScopeRef.
283 template <> DIRef<DIScope>::DIRef(const Value *V);
285 /// Specialize getFieldAs to handle fields that are references to DITypes.
286 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
287 /// Specialize DIRef constructor for DITypeRef.
288 template <> DIRef<DIType>::DIRef(const Value *V);
290 /// DIType - This is a wrapper for a type.
291 /// FIXME: Types should be factored much better so that CV qualifiers and
292 /// others do not require a huge and empty descriptor full of zeros.
293 class DIType : public DIScope {
295 friend class DIDescriptor;
296 void printInternal(raw_ostream &OS) const;
299 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
300 operator DITypeRef () const {
302 "constructing DITypeRef from an MDNode that is not a type");
303 return DITypeRef(&*getRef());
306 /// Verify - Verify that a type descriptor is well formed.
309 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
310 StringRef getName() const { return getStringField(3); }
311 unsigned getLineNumber() const { return getUnsignedField(4); }
312 uint64_t getSizeInBits() const { return getUInt64Field(5); }
313 uint64_t getAlignInBits() const { return getUInt64Field(6); }
314 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
315 // carry this is just plain insane.
316 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
317 unsigned getFlags() const { return getUnsignedField(8); }
318 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
319 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
320 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
321 // isAppleBlock - Return true if this is the Apple Blocks extension.
322 bool isAppleBlockExtension() const {
323 return (getFlags() & FlagAppleBlock) != 0;
325 bool isBlockByrefStruct() const {
326 return (getFlags() & FlagBlockByrefStruct) != 0;
328 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
329 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
330 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
331 bool isObjcClassComplete() const {
332 return (getFlags() & FlagObjcClassComplete) != 0;
334 bool isVector() const { return (getFlags() & FlagVector) != 0; }
335 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
336 bool isLValueReference() const {
337 return (getFlags() & FlagLValueReference) != 0;
339 bool isRValueReference() const {
340 return (getFlags() & FlagRValueReference) != 0;
342 bool isValid() const { return DbgNode && isType(); }
344 /// replaceAllUsesWith - Replace all uses of debug info referenced by
346 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
347 void replaceAllUsesWith(MDNode *D);
350 /// DIBasicType - A basic type, like 'int' or 'float'.
351 class DIBasicType : public DIType {
353 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
355 unsigned getEncoding() const { return getUnsignedField(9); }
357 /// Verify - Verify that a basic type descriptor is well formed.
361 /// DIDerivedType - A simple derived type, like a const qualified type,
362 /// a typedef, a pointer or reference, et cetera. Or, a data member of
363 /// a class/struct/union.
364 class DIDerivedType : public DIType {
365 friend class DIDescriptor;
366 void printInternal(raw_ostream &OS) const;
369 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
371 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
373 /// getObjCProperty - Return property node, if this ivar is
374 /// associated with one.
375 MDNode *getObjCProperty() const;
377 DITypeRef getClassType() const {
378 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
379 return getFieldAs<DITypeRef>(10);
382 Constant *getConstant() const {
383 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
384 return getConstantField(10);
387 /// Verify - Verify that a derived type descriptor is well formed.
391 /// DICompositeType - This descriptor holds a type that can refer to multiple
392 /// other types, like a function or struct.
393 /// DICompositeType is derived from DIDerivedType because some
394 /// composite types (such as enums) can be derived from basic types
395 // FIXME: Make this derive from DIType directly & just store the
396 // base type in a single DIType field.
397 class DICompositeType : public DIDerivedType {
398 friend class DIDescriptor;
399 void printInternal(raw_ostream &OS) const;
400 void setArraysHelper(MDNode *Elements, MDNode *TParams);
403 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
405 DIArray getElements() const {
406 assert(!isSubroutineType() && "no elements for DISubroutineType");
407 return getFieldAs<DIArray>(10);
409 template <typename T>
410 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
411 assert((!TParams || DbgNode->getNumOperands() == 15) &&
412 "If you're setting the template parameters this should include a slot "
414 setArraysHelper(Elements, TParams);
416 unsigned getRunTimeLang() const { return getUnsignedField(11); }
417 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
418 void setContainingType(DICompositeType ContainingType);
419 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
420 MDString *getIdentifier() const;
422 /// Verify - Verify that a composite type descriptor is well formed.
426 class DISubroutineType : public DICompositeType {
428 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
429 DITypedArray<DITypeRef> getTypeArray() const {
430 return getFieldAs<DITypedArray<DITypeRef>>(10);
434 /// DIFile - This is a wrapper for a file.
435 class DIFile : public DIScope {
436 friend class DIDescriptor;
439 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
440 MDNode *getFileNode() const;
444 /// DICompileUnit - A wrapper for a compile unit.
445 class DICompileUnit : public DIScope {
446 friend class DIDescriptor;
447 void printInternal(raw_ostream &OS) const;
450 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
452 dwarf::SourceLanguage getLanguage() const {
453 return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
455 StringRef getProducer() const { return getStringField(3); }
457 bool isOptimized() const { return getUnsignedField(4) != 0; }
458 StringRef getFlags() const { return getStringField(5); }
459 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
461 DIArray getEnumTypes() const;
462 DIArray getRetainedTypes() const;
463 DIArray getSubprograms() const;
464 DIArray getGlobalVariables() const;
465 DIArray getImportedEntities() const;
467 StringRef getSplitDebugFilename() const { return getStringField(12); }
468 unsigned getEmissionKind() const { return getUnsignedField(13); }
470 /// Verify - Verify that a compile unit is well formed.
474 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
475 class DISubprogram : public DIScope {
476 friend class DIDescriptor;
477 void printInternal(raw_ostream &OS) const;
480 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
482 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
483 StringRef getName() const { return getStringField(3); }
484 StringRef getDisplayName() const { return getStringField(4); }
485 StringRef getLinkageName() const { return getStringField(5); }
486 unsigned getLineNumber() const { return getUnsignedField(6); }
487 DISubroutineType getType() const { return getFieldAs<DISubroutineType>(7); }
489 /// isLocalToUnit - Return true if this subprogram is local to the current
490 /// compile unit, like 'static' in C.
491 unsigned isLocalToUnit() const { return getUnsignedField(8); }
492 unsigned isDefinition() const { return getUnsignedField(9); }
494 unsigned getVirtuality() const { return getUnsignedField(10); }
495 unsigned getVirtualIndex() const { return getUnsignedField(11); }
497 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
499 unsigned getFlags() const { return getUnsignedField(13); }
501 unsigned isArtificial() const {
502 return (getUnsignedField(13) & FlagArtificial) != 0;
504 /// isPrivate - Return true if this subprogram has "private"
505 /// access specifier.
506 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
507 /// isProtected - Return true if this subprogram has "protected"
508 /// access specifier.
509 bool isProtected() const {
510 return (getUnsignedField(13) & FlagProtected) != 0;
512 /// isExplicit - Return true if this subprogram is marked as explicit.
513 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
514 /// isPrototyped - Return true if this subprogram is prototyped.
515 bool isPrototyped() const {
516 return (getUnsignedField(13) & FlagPrototyped) != 0;
519 /// Return true if this subprogram is a C++11 reference-qualified
520 /// non-static member function (void foo() &).
521 unsigned isLValueReference() const {
522 return (getUnsignedField(13) & FlagLValueReference) != 0;
525 /// Return true if this subprogram is a C++11
526 /// rvalue-reference-qualified non-static member function
528 unsigned isRValueReference() const {
529 return (getUnsignedField(13) & FlagRValueReference) != 0;
532 unsigned isOptimized() const;
534 /// Verify - Verify that a subprogram descriptor is well formed.
537 /// describes - Return true if this subprogram provides debugging
538 /// information for the function F.
539 bool describes(const Function *F);
541 Function *getFunction() const { return getFunctionField(15); }
542 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
543 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
544 DISubprogram getFunctionDeclaration() const {
545 return getFieldAs<DISubprogram>(17);
547 MDNode *getVariablesNodes() const;
548 DIArray getVariables() const;
550 /// getScopeLineNumber - Get the beginning of the scope of the
551 /// function, not necessarily where the name of the program
553 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
556 /// DILexicalBlock - This is a wrapper for a lexical block.
557 class DILexicalBlock : public DIScope {
559 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
560 DIScope getContext() const { return getFieldAs<DIScope>(2); }
561 unsigned getLineNumber() const { return getUnsignedField(3); }
562 unsigned getColumnNumber() const { return getUnsignedField(4); }
563 unsigned getDiscriminator() const { return getUnsignedField(5); }
567 /// DILexicalBlockFile - This is a wrapper for a lexical block with
568 /// a filename change.
569 class DILexicalBlockFile : public DIScope {
571 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
572 DIScope getContext() const {
573 if (getScope().isSubprogram())
575 return getScope().getContext();
577 unsigned getLineNumber() const { return getScope().getLineNumber(); }
578 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
579 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
583 /// DINameSpace - A wrapper for a C++ style name space.
584 class DINameSpace : public DIScope {
585 friend class DIDescriptor;
586 void printInternal(raw_ostream &OS) const;
589 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
590 DIScope getContext() const { return getFieldAs<DIScope>(2); }
591 StringRef getName() const { return getStringField(3); }
592 unsigned getLineNumber() const { return getUnsignedField(4); }
596 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
597 class DITemplateTypeParameter : public DIDescriptor {
599 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
602 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
603 StringRef getName() const { return getStringField(2); }
604 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
605 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
606 StringRef getDirectory() const {
607 return getFieldAs<DIFile>(4).getDirectory();
609 unsigned getLineNumber() const { return getUnsignedField(5); }
610 unsigned getColumnNumber() const { return getUnsignedField(6); }
614 /// DITemplateValueParameter - This is a wrapper for template value parameter.
615 class DITemplateValueParameter : public DIDescriptor {
617 explicit DITemplateValueParameter(const MDNode *N = nullptr)
620 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
621 StringRef getName() const { return getStringField(2); }
622 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
623 Value *getValue() const;
624 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
625 StringRef getDirectory() const {
626 return getFieldAs<DIFile>(5).getDirectory();
628 unsigned getLineNumber() const { return getUnsignedField(6); }
629 unsigned getColumnNumber() const { return getUnsignedField(7); }
633 /// DIGlobalVariable - This is a wrapper for a global variable.
634 class DIGlobalVariable : public DIDescriptor {
635 friend class DIDescriptor;
636 void printInternal(raw_ostream &OS) const;
639 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
641 DIScope getContext() const { return getFieldAs<DIScope>(2); }
642 StringRef getName() const { return getStringField(3); }
643 StringRef getDisplayName() const { return getStringField(4); }
644 StringRef getLinkageName() const { return getStringField(5); }
645 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
646 StringRef getDirectory() const {
647 return getFieldAs<DIFile>(6).getDirectory();
650 unsigned getLineNumber() const { return getUnsignedField(7); }
651 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
652 unsigned isLocalToUnit() const { return getUnsignedField(9); }
653 unsigned isDefinition() const { return getUnsignedField(10); }
655 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
656 Constant *getConstant() const { return getConstantField(11); }
657 DIDerivedType getStaticDataMemberDeclaration() const {
658 return getFieldAs<DIDerivedType>(12);
661 /// Verify - Verify that a global variable descriptor is well formed.
665 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
667 class DIVariable : public DIDescriptor {
668 friend class DIDescriptor;
669 void printInternal(raw_ostream &OS) const;
672 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
674 DIScope getContext() const { return getFieldAs<DIScope>(1); }
675 StringRef getName() const { return getStringField(2); }
676 DIFile getFile() const { return getFieldAs<DIFile>(3); }
677 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
678 unsigned getArgNumber() const {
679 unsigned L = getUnsignedField(4);
682 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
684 /// isArtificial - Return true if this variable is marked as "artificial".
685 bool isArtificial() const {
686 return (getUnsignedField(6) & FlagArtificial) != 0;
689 bool isObjectPointer() const {
690 return (getUnsignedField(6) & FlagObjectPointer) != 0;
693 /// \brief Return true if this variable is represented as a pointer.
694 bool isIndirect() const {
695 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
698 /// getInlinedAt - If this variable is inlined then return inline location.
699 MDNode *getInlinedAt() const;
701 /// Verify - Verify that a variable descriptor is well formed.
704 /// HasComplexAddr - Return true if the variable has a complex address.
705 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
707 /// \brief Return the size of this variable's complex address or
708 /// zero if there is none.
709 unsigned getNumAddrElements() const {
710 if (DbgNode->getNumOperands() < 9)
712 return getDescriptorField(8)->getNumOperands();
715 /// \brief return the Idx'th complex address element.
716 uint64_t getAddrElement(unsigned Idx) const;
718 /// isBlockByrefVariable - Return true if the variable was declared as
719 /// a "__block" variable (Apple Blocks).
720 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
721 return (getType().resolve(Map)).isBlockByrefStruct();
724 /// isInlinedFnArgument - Return true if this variable provides debugging
725 /// information for an inlined function arguments.
726 bool isInlinedFnArgument(const Function *CurFn);
728 /// isVariablePiece - Return whether this is a piece of an aggregate
730 bool isVariablePiece() const;
731 /// getPieceOffset - Return the offset of this piece in bytes.
732 uint64_t getPieceOffset() const;
733 /// getPieceSize - Return the size of this piece in bytes.
734 uint64_t getPieceSize() const;
736 /// Return the size reported by the variable's type.
737 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
739 void printExtendedName(raw_ostream &OS) const;
742 /// DILocation - This object holds location information. This object
743 /// is not associated with any DWARF tag.
744 class DILocation : public DIDescriptor {
746 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
748 unsigned getLineNumber() const { return getUnsignedField(0); }
749 unsigned getColumnNumber() const { return getUnsignedField(1); }
750 DIScope getScope() const { return getFieldAs<DIScope>(2); }
751 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
752 StringRef getFilename() const { return getScope().getFilename(); }
753 StringRef getDirectory() const { return getScope().getDirectory(); }
755 bool atSameLineAs(const DILocation &Other) const {
756 return (getLineNumber() == Other.getLineNumber() &&
757 getFilename() == Other.getFilename());
759 /// getDiscriminator - DWARF discriminators are used to distinguish
760 /// identical file locations for instructions that are on different
761 /// basic blocks. If two instructions are inside the same lexical block
762 /// and are in different basic blocks, we create a new lexical block
763 /// with identical location as the original but with a different
764 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
766 unsigned getDiscriminator() const {
767 // Since discriminators are associated with lexical blocks, make
768 // sure this location is a lexical block before retrieving its
770 return getScope().isLexicalBlock()
771 ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
774 unsigned computeNewDiscriminator(LLVMContext &Ctx);
775 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
778 class DIObjCProperty : public DIDescriptor {
779 friend class DIDescriptor;
780 void printInternal(raw_ostream &OS) const;
783 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
785 StringRef getObjCPropertyName() const { return getStringField(1); }
786 DIFile getFile() const { return getFieldAs<DIFile>(2); }
787 unsigned getLineNumber() const { return getUnsignedField(3); }
789 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
790 StringRef getObjCPropertySetterName() const { return getStringField(5); }
791 bool isReadOnlyObjCProperty() const {
792 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
794 bool isReadWriteObjCProperty() const {
795 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
797 bool isAssignObjCProperty() const {
798 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
800 bool isRetainObjCProperty() const {
801 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
803 bool isCopyObjCProperty() const {
804 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
806 bool isNonAtomicObjCProperty() const {
807 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
810 /// Objective-C doesn't have an ODR, so there is no benefit in storing
811 /// the type as a DITypeRef here.
812 DIType getType() const { return getFieldAs<DIType>(7); }
814 /// Verify - Verify that a derived type descriptor is well formed.
818 /// \brief An imported module (C++ using directive or similar).
819 class DIImportedEntity : public DIDescriptor {
820 friend class DIDescriptor;
821 void printInternal(raw_ostream &OS) const;
824 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
825 DIScope getContext() const { return getFieldAs<DIScope>(1); }
826 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
827 unsigned getLineNumber() const { return getUnsignedField(3); }
828 StringRef getName() const { return getStringField(4); }
832 /// getDISubprogram - Find subprogram that is enclosing this scope.
833 DISubprogram getDISubprogram(const MDNode *Scope);
835 /// getDICompositeType - Find underlying composite type.
836 DICompositeType getDICompositeType(DIType T);
838 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
839 /// to hold function specific information.
840 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
842 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
843 /// suitable to hold function specific information.
844 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
846 /// createInlinedVariable - Create a new inlined variable based on current
848 /// @param DV Current Variable.
849 /// @param InlinedScope Location at current variable is inlined.
850 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
851 LLVMContext &VMContext);
853 /// cleanseInlinedVariable - Remove inlined scope from the variable.
854 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
856 /// getEntireVariable - Remove OpPiece exprs from the variable.
857 DIVariable getEntireVariable(DIVariable DV);
859 /// Construct DITypeIdentifierMap by going through retained types of each CU.
860 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
862 /// Strip debug info in the module if it exists.
863 /// To do this, we remove all calls to the debugger intrinsics and any named
864 /// metadata for debugging. We also remove debug locations for instructions.
865 /// Return true if module is modified.
866 bool StripDebugInfo(Module &M);
868 /// Return Debug Info Metadata Version by checking module flags.
869 unsigned getDebugMetadataVersionFromModule(const Module &M);
871 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
872 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
873 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
874 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
875 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
877 class DebugInfoFinder {
879 DebugInfoFinder() : TypeMapInitialized(false) {}
881 /// processModule - Process entire module and collect debug info
883 void processModule(const Module &M);
885 /// processDeclare - Process DbgDeclareInst.
886 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
887 /// Process DbgValueInst.
888 void processValue(const Module &M, const DbgValueInst *DVI);
889 /// processLocation - Process DILocation.
890 void processLocation(const Module &M, DILocation Loc);
896 /// Initialize TypeIdentifierMap.
897 void InitializeTypeMap(const Module &M);
899 /// processType - Process DIType.
900 void processType(DIType DT);
902 /// processSubprogram - Process DISubprogram.
903 void processSubprogram(DISubprogram SP);
905 void processScope(DIScope Scope);
907 /// addCompileUnit - Add compile unit into CUs.
908 bool addCompileUnit(DICompileUnit CU);
910 /// addGlobalVariable - Add global variable into GVs.
911 bool addGlobalVariable(DIGlobalVariable DIG);
913 // addSubprogram - Add subprogram into SPs.
914 bool addSubprogram(DISubprogram SP);
916 /// addType - Add type into Tys.
917 bool addType(DIType DT);
919 bool addScope(DIScope Scope);
922 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
923 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
924 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
925 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
926 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
928 iterator_range<compile_unit_iterator> compile_units() const {
929 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
932 iterator_range<subprogram_iterator> subprograms() const {
933 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
936 iterator_range<global_variable_iterator> global_variables() const {
937 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
940 iterator_range<type_iterator> types() const {
941 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
944 iterator_range<scope_iterator> scopes() const {
945 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
948 unsigned compile_unit_count() const { return CUs.size(); }
949 unsigned global_variable_count() const { return GVs.size(); }
950 unsigned subprogram_count() const { return SPs.size(); }
951 unsigned type_count() const { return TYs.size(); }
952 unsigned scope_count() const { return Scopes.size(); }
955 SmallVector<DICompileUnit, 8> CUs; // Compile Units
956 SmallVector<DISubprogram, 8> SPs; // Subprograms
957 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
958 SmallVector<DIType, 8> TYs; // Types
959 SmallVector<DIScope, 8> Scopes; // Scopes
960 SmallPtrSet<MDNode *, 64> NodesSeen;
961 DITypeIdentifierMap TypeIdentifierMap;
962 /// Specify if TypeIdentifierMap is initialized.
963 bool TypeMapInitialized;
966 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
968 } // end namespace llvm