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;
67 /// The three accessibility flags are mutually exclusive and rolled
68 /// together in the first two bits.
70 FlagAccessibility = 1 << 0 | 1 << 1,
76 FlagAppleBlock = 1 << 3,
77 FlagBlockByrefStruct = 1 << 4,
79 FlagArtificial = 1 << 6,
80 FlagExplicit = 1 << 7,
81 FlagPrototyped = 1 << 8,
82 FlagObjcClassComplete = 1 << 9,
83 FlagObjectPointer = 1 << 10,
85 FlagStaticMember = 1 << 12,
86 FlagIndirectVariable = 1 << 13,
87 FlagLValueReference = 1 << 14,
88 FlagRValueReference = 1 << 15
92 const MDNode *DbgNode;
94 StringRef getStringField(unsigned Elt) const;
95 unsigned getUnsignedField(unsigned Elt) const {
96 return (unsigned)getUInt64Field(Elt);
98 uint64_t getUInt64Field(unsigned Elt) const;
99 int64_t getInt64Field(unsigned Elt) const;
100 DIDescriptor getDescriptorField(unsigned Elt) const;
102 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
103 return DescTy(getDescriptorField(Elt));
106 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
107 Constant *getConstantField(unsigned Elt) const;
108 Function *getFunctionField(unsigned Elt) const;
109 void replaceFunctionField(unsigned Elt, Function *F);
112 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
116 operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
117 MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
119 // An explicit operator bool so that we can do testing of DI values
121 // FIXME: This operator bool isn't actually protecting anything at the
122 // moment due to the conversion operator above making DIDescriptor nodes
123 // implicitly convertable to bool.
124 LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
126 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
127 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
129 uint16_t getTag() const {
130 return getUnsignedField(0) & ~LLVMDebugVersionMask;
133 bool isDerivedType() const;
134 bool isCompositeType() const;
135 bool isSubroutineType() const;
136 bool isBasicType() const;
137 bool isVariable() const;
138 bool isSubprogram() const;
139 bool isGlobalVariable() const;
140 bool isScope() const;
142 bool isCompileUnit() const;
143 bool isNameSpace() const;
144 bool isLexicalBlockFile() const;
145 bool isLexicalBlock() const;
146 bool isSubrange() const;
147 bool isEnumerator() const;
149 bool isTemplateTypeParameter() const;
150 bool isTemplateValueParameter() const;
151 bool isObjCProperty() const;
152 bool isImportedEntity() const;
154 /// print - print descriptor.
155 void print(raw_ostream &OS) const;
157 /// dump - print descriptor to dbgs() with a newline.
161 /// DISubrange - This is used to represent ranges, for array bounds.
162 class DISubrange : public DIDescriptor {
163 friend class DIDescriptor;
164 void printInternal(raw_ostream &OS) const;
167 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
169 int64_t getLo() const { return getInt64Field(1); }
170 int64_t getCount() const { return getInt64Field(2); }
174 /// DITypedArray - This descriptor holds an array of nodes with type T.
175 template <typename T> class DITypedArray : public DIDescriptor {
177 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
178 unsigned getNumElements() const {
179 return DbgNode ? DbgNode->getNumOperands() : 0;
181 T getElement(unsigned Idx) const {
182 return getFieldAs<T>(Idx);
186 typedef DITypedArray<DIDescriptor> DIArray;
188 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
189 /// FIXME: it seems strange that this doesn't have either a reference to the
190 /// type/precision or a file/line pair for location info.
191 class DIEnumerator : public DIDescriptor {
192 friend class DIDescriptor;
193 void printInternal(raw_ostream &OS) const;
196 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
198 StringRef getName() const { return getStringField(1); }
199 int64_t getEnumValue() const { return getInt64Field(2); }
203 template <typename T> class DIRef;
204 typedef DIRef<DIScope> DIScopeRef;
205 typedef DIRef<DIType> DITypeRef;
206 typedef DITypedArray<DITypeRef> DITypeArray;
208 /// DIScope - A base class for various scopes.
210 /// Although, implementation-wise, DIScope is the parent class of most
211 /// other DIxxx classes, including DIType and its descendants, most of
212 /// DIScope's descendants are not a substitutable subtype of
213 /// DIScope. The DIDescriptor::isScope() method only is true for
214 /// DIScopes that are scopes in the strict lexical scope sense
215 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
216 class DIScope : public DIDescriptor {
218 friend class DIDescriptor;
219 void printInternal(raw_ostream &OS) const;
222 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
224 /// Gets the parent scope for this scope node or returns a
225 /// default constructed scope.
226 DIScopeRef getContext() const;
227 /// If the scope node has a name, return that, else return an empty string.
228 StringRef getName() const;
229 StringRef getFilename() const;
230 StringRef getDirectory() const;
232 /// Generate a reference to this DIScope. Uses the type identifier instead
233 /// of the actual MDNode if possible, to help type uniquing.
234 DIScopeRef getRef() const;
237 /// Represents reference to a DIDescriptor, abstracts over direct and
238 /// identifier-based metadata references.
239 template <typename T> class DIRef {
240 template <typename DescTy>
241 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
242 friend DIScopeRef DIScope::getContext() const;
243 friend DIScopeRef DIScope::getRef() const;
246 /// Val can be either a MDNode or a MDString, in the latter,
247 /// MDString specifies the type identifier.
249 explicit DIRef(const Value *V);
252 T resolve(const DITypeIdentifierMap &Map) const;
253 StringRef getName() const;
254 operator Value *() const { return const_cast<Value *>(Val); }
257 template <typename T>
258 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
262 if (const MDNode *MD = dyn_cast<MDNode>(Val))
265 const MDString *MS = cast<MDString>(Val);
266 // Find the corresponding MDNode.
267 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
268 assert(Iter != Map.end() && "Identifier not in the type map?");
269 assert(DIDescriptor(Iter->second).isType() &&
270 "MDNode in DITypeIdentifierMap should be a DIType.");
271 return T(Iter->second);
274 template <typename T> StringRef DIRef<T>::getName() const {
278 if (const MDNode *MD = dyn_cast<MDNode>(Val))
279 return T(MD).getName();
281 const MDString *MS = cast<MDString>(Val);
282 return MS->getString();
285 /// Specialize getFieldAs to handle fields that are references to DIScopes.
286 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
287 /// Specialize DIRef constructor for DIScopeRef.
288 template <> DIRef<DIScope>::DIRef(const Value *V);
290 /// Specialize getFieldAs to handle fields that are references to DITypes.
291 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
292 /// Specialize DIRef constructor for DITypeRef.
293 template <> DIRef<DIType>::DIRef(const Value *V);
295 /// DIType - This is a wrapper for a type.
296 /// FIXME: Types should be factored much better so that CV qualifiers and
297 /// others do not require a huge and empty descriptor full of zeros.
298 class DIType : public DIScope {
300 friend class DIDescriptor;
301 void printInternal(raw_ostream &OS) const;
304 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
305 operator DITypeRef () const {
307 "constructing DITypeRef from an MDNode that is not a type");
308 return DITypeRef(&*getRef());
311 /// Verify - Verify that a type descriptor is well formed.
314 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
315 StringRef getName() const { return getStringField(3); }
316 unsigned getLineNumber() const { return getUnsignedField(4); }
317 uint64_t getSizeInBits() const { return getUInt64Field(5); }
318 uint64_t getAlignInBits() const { return getUInt64Field(6); }
319 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
320 // carry this is just plain insane.
321 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
322 unsigned getFlags() const { return getUnsignedField(8); }
323 bool isPrivate() const {
324 return (getFlags() & FlagAccessibility) == FlagPrivate;
326 bool isProtected() const {
327 return (getFlags() & FlagAccessibility) == FlagProtected;
329 bool isPublic() const {
330 return (getFlags() & FlagAccessibility) == FlagPublic;
332 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
333 // isAppleBlock - Return true if this is the Apple Blocks extension.
334 bool isAppleBlockExtension() const {
335 return (getFlags() & FlagAppleBlock) != 0;
337 bool isBlockByrefStruct() const {
338 return (getFlags() & FlagBlockByrefStruct) != 0;
340 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
341 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
342 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
343 bool isObjcClassComplete() const {
344 return (getFlags() & FlagObjcClassComplete) != 0;
346 bool isVector() const { return (getFlags() & FlagVector) != 0; }
347 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
348 bool isLValueReference() const {
349 return (getFlags() & FlagLValueReference) != 0;
351 bool isRValueReference() const {
352 return (getFlags() & FlagRValueReference) != 0;
354 bool isValid() const { return DbgNode && isType(); }
356 /// replaceAllUsesWith - Replace all uses of debug info referenced by
358 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
359 void replaceAllUsesWith(MDNode *D);
362 /// DIBasicType - A basic type, like 'int' or 'float'.
363 class DIBasicType : public DIType {
365 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
367 unsigned getEncoding() const { return getUnsignedField(9); }
369 /// Verify - Verify that a basic type descriptor is well formed.
373 /// DIDerivedType - A simple derived type, like a const qualified type,
374 /// a typedef, a pointer or reference, et cetera. Or, a data member of
375 /// a class/struct/union.
376 class DIDerivedType : public DIType {
377 friend class DIDescriptor;
378 void printInternal(raw_ostream &OS) const;
381 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
383 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
385 /// getObjCProperty - Return property node, if this ivar is
386 /// associated with one.
387 MDNode *getObjCProperty() const;
389 DITypeRef getClassType() const {
390 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
391 return getFieldAs<DITypeRef>(10);
394 Constant *getConstant() const {
395 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
396 return getConstantField(10);
399 /// Verify - Verify that a derived type descriptor is well formed.
403 /// DICompositeType - This descriptor holds a type that can refer to multiple
404 /// other types, like a function or struct.
405 /// DICompositeType is derived from DIDerivedType because some
406 /// composite types (such as enums) can be derived from basic types
407 // FIXME: Make this derive from DIType directly & just store the
408 // base type in a single DIType field.
409 class DICompositeType : public DIDerivedType {
410 friend class DIDescriptor;
411 void printInternal(raw_ostream &OS) const;
412 void setArraysHelper(MDNode *Elements, MDNode *TParams);
415 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
417 DIArray getElements() const {
418 assert(!isSubroutineType() && "no elements for DISubroutineType");
419 return getFieldAs<DIArray>(10);
421 template <typename T>
422 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
423 assert((!TParams || DbgNode->getNumOperands() == 15) &&
424 "If you're setting the template parameters this should include a slot "
426 setArraysHelper(Elements, TParams);
428 unsigned getRunTimeLang() const { return getUnsignedField(11); }
429 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
430 void setContainingType(DICompositeType ContainingType);
431 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
432 MDString *getIdentifier() const;
434 /// Verify - Verify that a composite type descriptor is well formed.
438 class DISubroutineType : public DICompositeType {
440 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
441 DITypedArray<DITypeRef> getTypeArray() const {
442 return getFieldAs<DITypedArray<DITypeRef>>(10);
446 /// DIFile - This is a wrapper for a file.
447 class DIFile : public DIScope {
448 friend class DIDescriptor;
451 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
452 MDNode *getFileNode() const;
456 /// DICompileUnit - A wrapper for a compile unit.
457 class DICompileUnit : public DIScope {
458 friend class DIDescriptor;
459 void printInternal(raw_ostream &OS) const;
462 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
464 dwarf::SourceLanguage getLanguage() const {
465 return static_cast<dwarf::SourceLanguage>(getUnsignedField(2));
467 StringRef getProducer() const { return getStringField(3); }
469 bool isOptimized() const { return getUnsignedField(4) != 0; }
470 StringRef getFlags() const { return getStringField(5); }
471 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
473 DIArray getEnumTypes() const;
474 DIArray getRetainedTypes() const;
475 DIArray getSubprograms() const;
476 DIArray getGlobalVariables() const;
477 DIArray getImportedEntities() const;
479 StringRef getSplitDebugFilename() const { return getStringField(12); }
480 unsigned getEmissionKind() const { return getUnsignedField(13); }
482 /// Verify - Verify that a compile unit is well formed.
486 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
487 class DISubprogram : public DIScope {
488 friend class DIDescriptor;
489 void printInternal(raw_ostream &OS) const;
492 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
494 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
495 StringRef getName() const { return getStringField(3); }
496 StringRef getDisplayName() const { return getStringField(4); }
497 StringRef getLinkageName() const { return getStringField(5); }
498 unsigned getLineNumber() const { return getUnsignedField(6); }
499 DISubroutineType getType() const { return getFieldAs<DISubroutineType>(7); }
501 /// isLocalToUnit - Return true if this subprogram is local to the current
502 /// compile unit, like 'static' in C.
503 unsigned isLocalToUnit() const { return getUnsignedField(8); }
504 unsigned isDefinition() const { return getUnsignedField(9); }
506 unsigned getVirtuality() const { return getUnsignedField(10); }
507 unsigned getVirtualIndex() const { return getUnsignedField(11); }
509 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
511 unsigned getFlags() const { return getUnsignedField(13); }
513 unsigned isArtificial() const {
514 return (getUnsignedField(13) & FlagArtificial) != 0;
516 /// isPrivate - Return true if this subprogram has "private"
517 /// access specifier.
518 bool isPrivate() const {
519 return (getFlags() & FlagAccessibility) == FlagPrivate;
521 /// isProtected - Return true if this subprogram has "protected"
522 /// access specifier.
523 bool isProtected() const {
524 return (getFlags() & FlagAccessibility) == FlagProtected;
526 /// isPublic - Return true if this subprogram has "public"
527 /// access specifier.
528 bool isPublic() const {
529 return (getFlags() & FlagAccessibility) == FlagPublic;
531 /// isExplicit - Return true if this subprogram is marked as explicit.
532 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
533 /// isPrototyped - Return true if this subprogram is prototyped.
534 bool isPrototyped() const {
535 return (getUnsignedField(13) & FlagPrototyped) != 0;
538 /// Return true if this subprogram is a C++11 reference-qualified
539 /// non-static member function (void foo() &).
540 unsigned isLValueReference() const {
541 return (getUnsignedField(13) & FlagLValueReference) != 0;
544 /// Return true if this subprogram is a C++11
545 /// rvalue-reference-qualified non-static member function
547 unsigned isRValueReference() const {
548 return (getUnsignedField(13) & FlagRValueReference) != 0;
551 unsigned isOptimized() const;
553 /// Verify - Verify that a subprogram descriptor is well formed.
556 /// describes - Return true if this subprogram provides debugging
557 /// information for the function F.
558 bool describes(const Function *F);
560 Function *getFunction() const { return getFunctionField(15); }
561 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
562 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
563 DISubprogram getFunctionDeclaration() const {
564 return getFieldAs<DISubprogram>(17);
566 MDNode *getVariablesNodes() const;
567 DIArray getVariables() const;
569 /// getScopeLineNumber - Get the beginning of the scope of the
570 /// function, not necessarily where the name of the program
572 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
575 /// DILexicalBlock - This is a wrapper for a lexical block.
576 class DILexicalBlock : public DIScope {
578 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
579 DIScope getContext() const { return getFieldAs<DIScope>(2); }
580 unsigned getLineNumber() const { return getUnsignedField(3); }
581 unsigned getColumnNumber() const { return getUnsignedField(4); }
585 /// DILexicalBlockFile - This is a wrapper for a lexical block with
586 /// a filename change.
587 class DILexicalBlockFile : public DIScope {
589 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
590 DIScope getContext() const {
591 if (getScope().isSubprogram())
593 return getScope().getContext();
595 unsigned getLineNumber() const { return getScope().getLineNumber(); }
596 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
597 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
598 unsigned getDiscriminator() const { return getUnsignedField(3); }
602 /// DINameSpace - A wrapper for a C++ style name space.
603 class DINameSpace : public DIScope {
604 friend class DIDescriptor;
605 void printInternal(raw_ostream &OS) const;
608 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
609 DIScope getContext() const { return getFieldAs<DIScope>(2); }
610 StringRef getName() const { return getStringField(3); }
611 unsigned getLineNumber() const { return getUnsignedField(4); }
615 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
616 class DITemplateTypeParameter : public DIDescriptor {
618 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
621 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
622 StringRef getName() const { return getStringField(2); }
623 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
624 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
625 StringRef getDirectory() const {
626 return getFieldAs<DIFile>(4).getDirectory();
628 unsigned getLineNumber() const { return getUnsignedField(5); }
629 unsigned getColumnNumber() const { return getUnsignedField(6); }
633 /// DITemplateValueParameter - This is a wrapper for template value parameter.
634 class DITemplateValueParameter : public DIDescriptor {
636 explicit DITemplateValueParameter(const MDNode *N = nullptr)
639 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
640 StringRef getName() const { return getStringField(2); }
641 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
642 Value *getValue() const;
643 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
644 StringRef getDirectory() const {
645 return getFieldAs<DIFile>(5).getDirectory();
647 unsigned getLineNumber() const { return getUnsignedField(6); }
648 unsigned getColumnNumber() const { return getUnsignedField(7); }
652 /// DIGlobalVariable - This is a wrapper for a global variable.
653 class DIGlobalVariable : public DIDescriptor {
654 friend class DIDescriptor;
655 void printInternal(raw_ostream &OS) const;
658 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
660 DIScope getContext() const { return getFieldAs<DIScope>(2); }
661 StringRef getName() const { return getStringField(3); }
662 StringRef getDisplayName() const { return getStringField(4); }
663 StringRef getLinkageName() const { return getStringField(5); }
664 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
665 StringRef getDirectory() const {
666 return getFieldAs<DIFile>(6).getDirectory();
669 unsigned getLineNumber() const { return getUnsignedField(7); }
670 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
671 unsigned isLocalToUnit() const { return getUnsignedField(9); }
672 unsigned isDefinition() const { return getUnsignedField(10); }
674 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
675 Constant *getConstant() const { return getConstantField(11); }
676 DIDerivedType getStaticDataMemberDeclaration() const {
677 return getFieldAs<DIDerivedType>(12);
680 /// Verify - Verify that a global variable descriptor is well formed.
684 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
686 class DIVariable : public DIDescriptor {
687 friend class DIDescriptor;
688 void printInternal(raw_ostream &OS) const;
691 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
693 DIScope getContext() const { return getFieldAs<DIScope>(1); }
694 StringRef getName() const { return getStringField(2); }
695 DIFile getFile() const { return getFieldAs<DIFile>(3); }
696 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
697 unsigned getArgNumber() const {
698 unsigned L = getUnsignedField(4);
701 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
703 /// isArtificial - Return true if this variable is marked as "artificial".
704 bool isArtificial() const {
705 return (getUnsignedField(6) & FlagArtificial) != 0;
708 bool isObjectPointer() const {
709 return (getUnsignedField(6) & FlagObjectPointer) != 0;
712 /// \brief Return true if this variable is represented as a pointer.
713 bool isIndirect() const {
714 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
717 /// getInlinedAt - If this variable is inlined then return inline location.
718 MDNode *getInlinedAt() const;
720 /// Verify - Verify that a variable descriptor is well formed.
723 /// HasComplexAddr - Return true if the variable has a complex address.
724 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
726 /// \brief Return the size of this variable's complex address or
727 /// zero if there is none.
728 unsigned getNumAddrElements() const {
729 if (DbgNode->getNumOperands() < 9)
731 return getDescriptorField(8)->getNumOperands();
734 /// \brief return the Idx'th complex address element.
735 uint64_t getAddrElement(unsigned Idx) const;
737 /// isBlockByrefVariable - Return true if the variable was declared as
738 /// a "__block" variable (Apple Blocks).
739 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
740 return (getType().resolve(Map)).isBlockByrefStruct();
743 /// isInlinedFnArgument - Return true if this variable provides debugging
744 /// information for an inlined function arguments.
745 bool isInlinedFnArgument(const Function *CurFn);
747 /// isVariablePiece - Return whether this is a piece of an aggregate
749 bool isVariablePiece() const;
750 /// getPieceOffset - Return the offset of this piece in bytes.
751 uint64_t getPieceOffset() const;
752 /// getPieceSize - Return the size of this piece in bytes.
753 uint64_t getPieceSize() const;
755 /// Return the size reported by the variable's type.
756 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
758 void printExtendedName(raw_ostream &OS) const;
761 /// DILocation - This object holds location information. This object
762 /// is not associated with any DWARF tag.
763 class DILocation : public DIDescriptor {
765 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
767 unsigned getLineNumber() const { return getUnsignedField(0); }
768 unsigned getColumnNumber() const { return getUnsignedField(1); }
769 DIScope getScope() const { return getFieldAs<DIScope>(2); }
770 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
771 StringRef getFilename() const { return getScope().getFilename(); }
772 StringRef getDirectory() const { return getScope().getDirectory(); }
774 bool atSameLineAs(const DILocation &Other) const {
775 return (getLineNumber() == Other.getLineNumber() &&
776 getFilename() == Other.getFilename());
778 /// getDiscriminator - DWARF discriminators are used to distinguish
779 /// identical file locations for instructions that are on different
780 /// basic blocks. If two instructions are inside the same lexical block
781 /// and are in different basic blocks, we create a new lexical block
782 /// with identical location as the original but with a different
783 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
785 unsigned getDiscriminator() const {
786 // Since discriminators are associated with lexical blocks, make
787 // sure this location is a lexical block before retrieving its
789 return getScope().isLexicalBlockFile()
790 ? getFieldAs<DILexicalBlockFile>(2).getDiscriminator()
793 unsigned computeNewDiscriminator(LLVMContext &Ctx);
794 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
797 class DIObjCProperty : public DIDescriptor {
798 friend class DIDescriptor;
799 void printInternal(raw_ostream &OS) const;
802 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
804 StringRef getObjCPropertyName() const { return getStringField(1); }
805 DIFile getFile() const { return getFieldAs<DIFile>(2); }
806 unsigned getLineNumber() const { return getUnsignedField(3); }
808 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
809 StringRef getObjCPropertySetterName() const { return getStringField(5); }
810 bool isReadOnlyObjCProperty() const {
811 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
813 bool isReadWriteObjCProperty() const {
814 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
816 bool isAssignObjCProperty() const {
817 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
819 bool isRetainObjCProperty() const {
820 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
822 bool isCopyObjCProperty() const {
823 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
825 bool isNonAtomicObjCProperty() const {
826 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
829 /// Objective-C doesn't have an ODR, so there is no benefit in storing
830 /// the type as a DITypeRef here.
831 DIType getType() const { return getFieldAs<DIType>(7); }
833 /// Verify - Verify that a derived type descriptor is well formed.
837 /// \brief An imported module (C++ using directive or similar).
838 class DIImportedEntity : public DIDescriptor {
839 friend class DIDescriptor;
840 void printInternal(raw_ostream &OS) const;
843 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
844 DIScope getContext() const { return getFieldAs<DIScope>(1); }
845 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
846 unsigned getLineNumber() const { return getUnsignedField(3); }
847 StringRef getName() const { return getStringField(4); }
851 /// getDISubprogram - Find subprogram that is enclosing this scope.
852 DISubprogram getDISubprogram(const MDNode *Scope);
854 /// getDICompositeType - Find underlying composite type.
855 DICompositeType getDICompositeType(DIType T);
857 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
858 /// to hold function specific information.
859 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
861 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
862 /// suitable to hold function specific information.
863 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
865 /// createInlinedVariable - Create a new inlined variable based on current
867 /// @param DV Current Variable.
868 /// @param InlinedScope Location at current variable is inlined.
869 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
870 LLVMContext &VMContext);
872 /// cleanseInlinedVariable - Remove inlined scope from the variable.
873 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
875 /// getEntireVariable - Remove OpPiece exprs from the variable.
876 DIVariable getEntireVariable(DIVariable DV);
878 /// Construct DITypeIdentifierMap by going through retained types of each CU.
879 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
881 /// Strip debug info in the module if it exists.
882 /// To do this, we remove all calls to the debugger intrinsics and any named
883 /// metadata for debugging. We also remove debug locations for instructions.
884 /// Return true if module is modified.
885 bool StripDebugInfo(Module &M);
887 /// Return Debug Info Metadata Version by checking module flags.
888 unsigned getDebugMetadataVersionFromModule(const Module &M);
890 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
891 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
892 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
893 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
894 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
896 class DebugInfoFinder {
898 DebugInfoFinder() : TypeMapInitialized(false) {}
900 /// processModule - Process entire module and collect debug info
902 void processModule(const Module &M);
904 /// processDeclare - Process DbgDeclareInst.
905 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
906 /// Process DbgValueInst.
907 void processValue(const Module &M, const DbgValueInst *DVI);
908 /// processLocation - Process DILocation.
909 void processLocation(const Module &M, DILocation Loc);
915 /// Initialize TypeIdentifierMap.
916 void InitializeTypeMap(const Module &M);
918 /// processType - Process DIType.
919 void processType(DIType DT);
921 /// processSubprogram - Process DISubprogram.
922 void processSubprogram(DISubprogram SP);
924 void processScope(DIScope Scope);
926 /// addCompileUnit - Add compile unit into CUs.
927 bool addCompileUnit(DICompileUnit CU);
929 /// addGlobalVariable - Add global variable into GVs.
930 bool addGlobalVariable(DIGlobalVariable DIG);
932 // addSubprogram - Add subprogram into SPs.
933 bool addSubprogram(DISubprogram SP);
935 /// addType - Add type into Tys.
936 bool addType(DIType DT);
938 bool addScope(DIScope Scope);
941 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
942 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
943 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
944 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
945 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
947 iterator_range<compile_unit_iterator> compile_units() const {
948 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
951 iterator_range<subprogram_iterator> subprograms() const {
952 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
955 iterator_range<global_variable_iterator> global_variables() const {
956 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
959 iterator_range<type_iterator> types() const {
960 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
963 iterator_range<scope_iterator> scopes() const {
964 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
967 unsigned compile_unit_count() const { return CUs.size(); }
968 unsigned global_variable_count() const { return GVs.size(); }
969 unsigned subprogram_count() const { return SPs.size(); }
970 unsigned type_count() const { return TYs.size(); }
971 unsigned scope_count() const { return Scopes.size(); }
974 SmallVector<DICompileUnit, 8> CUs; // Compile Units
975 SmallVector<DISubprogram, 8> SPs; // Subprograms
976 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
977 SmallVector<DIType, 8> TYs; // Types
978 SmallVector<DIScope, 8> Scopes; // Scopes
979 SmallPtrSet<MDNode *, 64> NodesSeen;
980 DITypeIdentifierMap TypeIdentifierMap;
981 /// Specify if TypeIdentifierMap is initialized.
982 bool TypeMapInitialized;
985 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
987 } // end namespace llvm