1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_IR_DEBUGINFO_H
18 #define LLVM_IR_DEBUGINFO_H
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Dwarf.h"
49 class DILexicalBlockFile;
55 /// Maps from type identifier to the actual MDNode.
56 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
58 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
59 /// This should not be stored in a container, because the underlying MDNode
60 /// may change in certain situations.
62 // Befriends DIRef so DIRef can befriend the protected member
63 // function: getFieldAs<DIRef>.
64 template <typename T> friend class DIRef;
69 FlagProtected = 1 << 1,
71 FlagAppleBlock = 1 << 3,
72 FlagBlockByrefStruct = 1 << 4,
74 FlagArtificial = 1 << 6,
75 FlagExplicit = 1 << 7,
76 FlagPrototyped = 1 << 8,
77 FlagObjcClassComplete = 1 << 9,
78 FlagObjectPointer = 1 << 10,
80 FlagStaticMember = 1 << 12,
81 FlagIndirectVariable = 1 << 13,
82 FlagLValueReference = 1 << 14,
83 FlagRValueReference = 1 << 15
87 const MDNode *DbgNode;
89 StringRef getStringField(unsigned Elt) const;
90 unsigned getUnsignedField(unsigned Elt) const {
91 return (unsigned)getUInt64Field(Elt);
93 uint64_t getUInt64Field(unsigned Elt) const;
94 int64_t getInt64Field(unsigned Elt) const;
95 DIDescriptor getDescriptorField(unsigned Elt) const;
97 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
98 return DescTy(getDescriptorField(Elt));
101 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
102 Constant *getConstantField(unsigned Elt) const;
103 Function *getFunctionField(unsigned Elt) const;
104 void replaceFunctionField(unsigned Elt, Function *F);
107 explicit DIDescriptor(const MDNode *N = 0) : DbgNode(N) {}
111 operator MDNode *() const { return const_cast<MDNode *>(DbgNode); }
112 MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); }
114 // An explicit operator bool so that we can do testing of DI values
116 // FIXME: This operator bool isn't actually protecting anything at the
117 // moment due to the conversion operator above making DIDescriptor nodes
118 // implicitly convertable to bool.
119 LLVM_EXPLICIT operator bool() const { return DbgNode != 0; }
121 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
122 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
124 uint16_t getTag() const {
125 return getUnsignedField(0) & ~LLVMDebugVersionMask;
128 bool isDerivedType() const;
129 bool isCompositeType() const;
130 bool isBasicType() const;
131 bool isVariable() const;
132 bool isSubprogram() const;
133 bool isGlobalVariable() const;
134 bool isScope() const;
136 bool isCompileUnit() const;
137 bool isNameSpace() const;
138 bool isLexicalBlockFile() const;
139 bool isLexicalBlock() const;
140 bool isSubrange() const;
141 bool isEnumerator() const;
143 bool isUnspecifiedParameter() const;
144 bool isTemplateTypeParameter() const;
145 bool isTemplateValueParameter() const;
146 bool isObjCProperty() const;
147 bool isImportedEntity() const;
149 /// print - print descriptor.
150 void print(raw_ostream &OS) const;
152 /// dump - print descriptor to dbgs() with a newline.
156 /// DISubrange - This is used to represent ranges, for array bounds.
157 class DISubrange : public DIDescriptor {
158 friend class DIDescriptor;
159 void printInternal(raw_ostream &OS) const;
162 explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
164 int64_t getLo() const { return getInt64Field(1); }
165 int64_t getCount() const { return getInt64Field(2); }
169 /// DIArray - This descriptor holds an array of descriptors.
170 class DIArray : public DIDescriptor {
172 explicit DIArray(const MDNode *N = 0) : DIDescriptor(N) {}
174 unsigned getNumElements() const;
175 DIDescriptor getElement(unsigned Idx) const {
176 return getDescriptorField(Idx);
180 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
181 /// FIXME: it seems strange that this doesn't have either a reference to the
182 /// type/precision or a file/line pair for location info.
183 class DIEnumerator : public DIDescriptor {
184 friend class DIDescriptor;
185 void printInternal(raw_ostream &OS) const;
188 explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
190 StringRef getName() const { return getStringField(1); }
191 int64_t getEnumValue() const { return getInt64Field(2); }
195 template <typename T> class DIRef;
196 typedef DIRef<DIScope> DIScopeRef;
197 typedef DIRef<DIType> DITypeRef;
199 /// DIScope - A base class for various scopes.
200 class DIScope : public DIDescriptor {
202 friend class DIDescriptor;
203 void printInternal(raw_ostream &OS) const;
206 explicit DIScope(const MDNode *N = 0) : DIDescriptor(N) {}
208 /// Gets the parent scope for this scope node or returns a
209 /// default constructed scope.
210 DIScopeRef getContext() const;
211 /// If the scope node has a name, return that, else return an empty string.
212 StringRef getName() const;
213 StringRef getFilename() const;
214 StringRef getDirectory() const;
216 /// Generate a reference to this DIScope. Uses the type identifier instead
217 /// of the actual MDNode if possible, to help type uniquing.
218 DIScopeRef getRef() const;
221 /// Represents reference to a DIDescriptor, abstracts over direct and
222 /// identifier-based metadata references.
223 template <typename T> class DIRef {
224 template <typename DescTy>
225 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
226 friend DIScopeRef DIScope::getContext() const;
227 friend DIScopeRef DIScope::getRef() const;
230 /// Val can be either a MDNode or a MDString, in the latter,
231 /// MDString specifies the type identifier.
233 explicit DIRef(const Value *V);
236 T resolve(const DITypeIdentifierMap &Map) const;
237 StringRef getName() const;
238 operator Value *() const { return const_cast<Value *>(Val); }
241 template <typename T>
242 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
246 if (const MDNode *MD = dyn_cast<MDNode>(Val))
249 const MDString *MS = cast<MDString>(Val);
250 // Find the corresponding MDNode.
251 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
252 assert(Iter != Map.end() && "Identifier not in the type map?");
253 assert(DIDescriptor(Iter->second).isType() &&
254 "MDNode in DITypeIdentifierMap should be a DIType.");
255 return T(Iter->second);
258 template <typename T> StringRef DIRef<T>::getName() const {
262 if (const MDNode *MD = dyn_cast<MDNode>(Val))
263 return T(MD).getName();
265 const MDString *MS = cast<MDString>(Val);
266 return MS->getString();
269 /// Specialize getFieldAs to handle fields that are references to DIScopes.
270 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
271 /// Specialize DIRef constructor for DIScopeRef.
272 template <> DIRef<DIScope>::DIRef(const Value *V);
274 /// Specialize getFieldAs to handle fields that are references to DITypes.
275 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
276 /// Specialize DIRef constructor for DITypeRef.
277 template <> DIRef<DIType>::DIRef(const Value *V);
279 /// DIType - This is a wrapper for a type.
280 /// FIXME: Types should be factored much better so that CV qualifiers and
281 /// others do not require a huge and empty descriptor full of zeros.
282 class DIType : public DIScope {
284 friend class DIDescriptor;
285 void printInternal(raw_ostream &OS) const;
288 explicit DIType(const MDNode *N = 0) : DIScope(N) {}
289 operator DITypeRef () const {
291 "constructing DITypeRef from an MDNode that is not a type");
292 return DITypeRef(&*getRef());
295 /// Verify - Verify that a type descriptor is well formed.
298 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
299 StringRef getName() const { return getStringField(3); }
300 unsigned getLineNumber() const { return getUnsignedField(4); }
301 uint64_t getSizeInBits() const { return getUInt64Field(5); }
302 uint64_t getAlignInBits() const { return getUInt64Field(6); }
303 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
304 // carry this is just plain insane.
305 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
306 unsigned getFlags() const { return getUnsignedField(8); }
307 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
308 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
309 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
310 // isAppleBlock - Return true if this is the Apple Blocks extension.
311 bool isAppleBlockExtension() const {
312 return (getFlags() & FlagAppleBlock) != 0;
314 bool isBlockByrefStruct() const {
315 return (getFlags() & FlagBlockByrefStruct) != 0;
317 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
318 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
319 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
320 bool isObjcClassComplete() const {
321 return (getFlags() & FlagObjcClassComplete) != 0;
323 bool isVector() const { return (getFlags() & FlagVector) != 0; }
324 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
325 bool isLValueReference() const {
326 return (getFlags() & FlagLValueReference) != 0;
328 bool isRValueReference() const {
329 return (getFlags() & FlagRValueReference) != 0;
331 bool isValid() const { return DbgNode && isType(); }
333 /// replaceAllUsesWith - Replace all uses of debug info referenced by
335 void replaceAllUsesWith(DIDescriptor &D);
336 void replaceAllUsesWith(MDNode *D);
339 /// DIBasicType - A basic type, like 'int' or 'float'.
340 class DIBasicType : public DIType {
342 explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
344 unsigned getEncoding() const { return getUnsignedField(9); }
346 /// Verify - Verify that a basic type descriptor is well formed.
350 /// DIDerivedType - A simple derived type, like a const qualified type,
351 /// a typedef, a pointer or reference, et cetera. Or, a data member of
352 /// a class/struct/union.
353 class DIDerivedType : public DIType {
354 friend class DIDescriptor;
355 void printInternal(raw_ostream &OS) const;
358 explicit DIDerivedType(const MDNode *N = 0) : DIType(N) {}
360 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
362 /// getObjCProperty - Return property node, if this ivar is
363 /// associated with one.
364 MDNode *getObjCProperty() const;
366 DITypeRef getClassType() const {
367 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
368 return getFieldAs<DITypeRef>(10);
371 Constant *getConstant() const {
372 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
373 return getConstantField(10);
376 /// Verify - Verify that a derived type descriptor is well formed.
380 /// DICompositeType - This descriptor holds a type that can refer to multiple
381 /// other types, like a function or struct.
382 /// DICompositeType is derived from DIDerivedType because some
383 /// composite types (such as enums) can be derived from basic types
384 // FIXME: Make this derive from DIType directly & just store the
385 // base type in a single DIType field.
386 class DICompositeType : public DIDerivedType {
387 friend class DIDescriptor;
388 void printInternal(raw_ostream &OS) const;
391 explicit DICompositeType(const MDNode *N = 0) : DIDerivedType(N) {}
393 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
394 void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
395 unsigned getRunTimeLang() const { return getUnsignedField(11); }
396 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
397 void setContainingType(DICompositeType ContainingType);
398 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
399 MDString *getIdentifier() const;
401 /// Verify - Verify that a composite type descriptor is well formed.
405 /// DIFile - This is a wrapper for a file.
406 class DIFile : public DIScope {
407 friend class DIDescriptor;
410 explicit DIFile(const MDNode *N = 0) : DIScope(N) {}
411 MDNode *getFileNode() const;
415 /// DICompileUnit - A wrapper for a compile unit.
416 class DICompileUnit : public DIScope {
417 friend class DIDescriptor;
418 void printInternal(raw_ostream &OS) const;
421 explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
423 unsigned getLanguage() const { return getUnsignedField(2); }
424 StringRef getProducer() const { return getStringField(3); }
426 bool isOptimized() const { return getUnsignedField(4) != 0; }
427 StringRef getFlags() const { return getStringField(5); }
428 unsigned getRunTimeVersion() const { return getUnsignedField(6); }
430 DIArray getEnumTypes() const;
431 DIArray getRetainedTypes() const;
432 DIArray getSubprograms() const;
433 DIArray getGlobalVariables() const;
434 DIArray getImportedEntities() const;
436 StringRef getSplitDebugFilename() const { return getStringField(12); }
437 unsigned getEmissionKind() const { return getUnsignedField(13); }
439 /// Verify - Verify that a compile unit is well formed.
443 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
444 class DISubprogram : public DIScope {
445 friend class DIDescriptor;
446 void printInternal(raw_ostream &OS) const;
449 explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
451 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
452 StringRef getName() const { return getStringField(3); }
453 StringRef getDisplayName() const { return getStringField(4); }
454 StringRef getLinkageName() const { return getStringField(5); }
455 unsigned getLineNumber() const { return getUnsignedField(6); }
456 DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
458 /// isLocalToUnit - Return true if this subprogram is local to the current
459 /// compile unit, like 'static' in C.
460 unsigned isLocalToUnit() const { return getUnsignedField(8); }
461 unsigned isDefinition() const { return getUnsignedField(9); }
463 unsigned getVirtuality() const { return getUnsignedField(10); }
464 unsigned getVirtualIndex() const { return getUnsignedField(11); }
466 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(12); }
468 unsigned getFlags() const { return getUnsignedField(13); }
470 unsigned isArtificial() const {
471 return (getUnsignedField(13) & FlagArtificial) != 0;
473 /// isPrivate - Return true if this subprogram has "private"
474 /// access specifier.
475 bool isPrivate() const { return (getUnsignedField(13) & FlagPrivate) != 0; }
476 /// isProtected - Return true if this subprogram has "protected"
477 /// access specifier.
478 bool isProtected() const {
479 return (getUnsignedField(13) & FlagProtected) != 0;
481 /// isExplicit - Return true if this subprogram is marked as explicit.
482 bool isExplicit() const { return (getUnsignedField(13) & FlagExplicit) != 0; }
483 /// isPrototyped - Return true if this subprogram is prototyped.
484 bool isPrototyped() const {
485 return (getUnsignedField(13) & FlagPrototyped) != 0;
488 /// Return true if this subprogram is a C++11 reference-qualified
489 /// non-static member function (void foo() &).
490 unsigned isLValueReference() const {
491 return (getUnsignedField(13) & FlagLValueReference) != 0;
494 /// Return true if this subprogram is a C++11
495 /// rvalue-reference-qualified non-static member function
497 unsigned isRValueReference() const {
498 return (getUnsignedField(13) & FlagRValueReference) != 0;
501 unsigned isOptimized() const;
503 /// Verify - Verify that a subprogram descriptor is well formed.
506 /// describes - Return true if this subprogram provides debugging
507 /// information for the function F.
508 bool describes(const Function *F);
510 Function *getFunction() const { return getFunctionField(15); }
511 void replaceFunction(Function *F) { replaceFunctionField(15, F); }
512 DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
513 DISubprogram getFunctionDeclaration() const {
514 return getFieldAs<DISubprogram>(17);
516 MDNode *getVariablesNodes() const;
517 DIArray getVariables() const;
519 /// getScopeLineNumber - Get the beginning of the scope of the
520 /// function, not necessarily where the name of the program
522 unsigned getScopeLineNumber() const { return getUnsignedField(19); }
525 /// DILexicalBlock - This is a wrapper for a lexical block.
526 class DILexicalBlock : public DIScope {
528 explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
529 DIScope getContext() const { return getFieldAs<DIScope>(2); }
530 unsigned getLineNumber() const { return getUnsignedField(3); }
531 unsigned getColumnNumber() const { return getUnsignedField(4); }
532 unsigned getDiscriminator() const { return getUnsignedField(5); }
536 /// DILexicalBlockFile - This is a wrapper for a lexical block with
537 /// a filename change.
538 class DILexicalBlockFile : public DIScope {
540 explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
541 DIScope getContext() const {
542 if (getScope().isSubprogram())
544 return getScope().getContext();
546 unsigned getLineNumber() const { return getScope().getLineNumber(); }
547 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
548 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
552 /// DINameSpace - A wrapper for a C++ style name space.
553 class DINameSpace : public DIScope {
554 friend class DIDescriptor;
555 void printInternal(raw_ostream &OS) const;
558 explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
559 DIScope getContext() const { return getFieldAs<DIScope>(2); }
560 StringRef getName() const { return getStringField(3); }
561 unsigned getLineNumber() const { return getUnsignedField(4); }
565 /// DIUnspecifiedParameter - This is a wrapper for unspecified parameters.
566 class DIUnspecifiedParameter : public DIDescriptor {
568 explicit DIUnspecifiedParameter(const MDNode *N = 0) : DIDescriptor(N) {}
572 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
573 class DITemplateTypeParameter : public DIDescriptor {
575 explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
577 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
578 StringRef getName() const { return getStringField(2); }
579 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
580 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
581 StringRef getDirectory() const {
582 return getFieldAs<DIFile>(4).getDirectory();
584 unsigned getLineNumber() const { return getUnsignedField(5); }
585 unsigned getColumnNumber() const { return getUnsignedField(6); }
589 /// DITemplateValueParameter - This is a wrapper for template value parameter.
590 class DITemplateValueParameter : public DIDescriptor {
592 explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
594 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
595 StringRef getName() const { return getStringField(2); }
596 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
597 Value *getValue() const;
598 StringRef getFilename() const { return getFieldAs<DIFile>(5).getFilename(); }
599 StringRef getDirectory() const {
600 return getFieldAs<DIFile>(5).getDirectory();
602 unsigned getLineNumber() const { return getUnsignedField(6); }
603 unsigned getColumnNumber() const { return getUnsignedField(7); }
607 /// DIGlobalVariable - This is a wrapper for a global variable.
608 class DIGlobalVariable : public DIDescriptor {
609 friend class DIDescriptor;
610 void printInternal(raw_ostream &OS) const;
613 explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
615 // FIXME: use DIScopeRef for LTO type uniqueing.
616 DIScope getContext() const { return getFieldAs<DIScope>(2); }
617 StringRef getName() const { return getStringField(3); }
618 StringRef getDisplayName() const { return getStringField(4); }
619 StringRef getLinkageName() const { return getStringField(5); }
620 StringRef getFilename() const { return getFieldAs<DIFile>(6).getFilename(); }
621 StringRef getDirectory() const {
622 return getFieldAs<DIFile>(6).getDirectory();
625 unsigned getLineNumber() const { return getUnsignedField(7); }
626 DITypeRef getType() const { return getFieldAs<DITypeRef>(8); }
627 unsigned isLocalToUnit() const { return getUnsignedField(9); }
628 unsigned isDefinition() const { return getUnsignedField(10); }
630 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
631 Constant *getConstant() const { return getConstantField(11); }
632 DIDerivedType getStaticDataMemberDeclaration() const {
633 return getFieldAs<DIDerivedType>(12);
636 /// Verify - Verify that a global variable descriptor is well formed.
640 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
642 class DIVariable : public DIDescriptor {
643 friend class DIDescriptor;
644 void printInternal(raw_ostream &OS) const;
647 explicit DIVariable(const MDNode *N = 0) : DIDescriptor(N) {}
649 // FIXME: use DIScopeRef for LTO type uniqueing.
650 DIScope getContext() const { return getFieldAs<DIScope>(1); }
651 StringRef getName() const { return getStringField(2); }
652 DIFile getFile() const { return getFieldAs<DIFile>(3); }
653 unsigned getLineNumber() const { return (getUnsignedField(4) << 8) >> 8; }
654 unsigned getArgNumber() const {
655 unsigned L = getUnsignedField(4);
658 DITypeRef getType() const { return getFieldAs<DITypeRef>(5); }
660 /// isArtificial - Return true if this variable is marked as "artificial".
661 bool isArtificial() const {
662 return (getUnsignedField(6) & FlagArtificial) != 0;
665 bool isObjectPointer() const {
666 return (getUnsignedField(6) & FlagObjectPointer) != 0;
669 /// \brief Return true if this variable is represented as a pointer.
670 bool isIndirect() const {
671 return (getUnsignedField(6) & FlagIndirectVariable) != 0;
674 /// getInlinedAt - If this variable is inlined then return inline location.
675 MDNode *getInlinedAt() const;
677 /// Verify - Verify that a variable descriptor is well formed.
680 /// HasComplexAddr - Return true if the variable has a complex address.
681 bool hasComplexAddress() const { return getNumAddrElements() > 0; }
683 unsigned getNumAddrElements() const;
685 uint64_t getAddrElement(unsigned Idx) const {
686 return getUInt64Field(Idx + 8);
689 /// isBlockByrefVariable - Return true if the variable was declared as
690 /// a "__block" variable (Apple Blocks).
691 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
692 return (getType().resolve(Map)).isBlockByrefStruct();
695 /// isInlinedFnArgument - Return true if this variable provides debugging
696 /// information for an inlined function arguments.
697 bool isInlinedFnArgument(const Function *CurFn);
699 void printExtendedName(raw_ostream &OS) const;
702 /// DILocation - This object holds location information. This object
703 /// is not associated with any DWARF tag.
704 class DILocation : public DIDescriptor {
706 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
708 unsigned getLineNumber() const { return getUnsignedField(0); }
709 unsigned getColumnNumber() const { return getUnsignedField(1); }
710 DIScope getScope() const { return getFieldAs<DIScope>(2); }
711 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
712 StringRef getFilename() const { return getScope().getFilename(); }
713 StringRef getDirectory() const { return getScope().getDirectory(); }
715 bool atSameLineAs(const DILocation &Other) const {
716 return (getLineNumber() == Other.getLineNumber() &&
717 getFilename() == Other.getFilename());
719 /// getDiscriminator - DWARF discriminators are used to distinguish
720 /// identical file locations for instructions that are on different
721 /// basic blocks. If two instructions are inside the same lexical block
722 /// and are in different basic blocks, we create a new lexical block
723 /// with identical location as the original but with a different
724 /// discriminator value (lib/Transforms/Util/AddDiscriminators.cpp
726 unsigned getDiscriminator() const {
727 // Since discriminators are associated with lexical blocks, make
728 // sure this location is a lexical block before retrieving its
730 return getScope().isLexicalBlock()
731 ? getFieldAs<DILexicalBlock>(2).getDiscriminator()
734 unsigned computeNewDiscriminator(LLVMContext &Ctx);
735 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlock NewScope);
738 class DIObjCProperty : public DIDescriptor {
739 friend class DIDescriptor;
740 void printInternal(raw_ostream &OS) const;
743 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
745 StringRef getObjCPropertyName() const { return getStringField(1); }
746 DIFile getFile() const { return getFieldAs<DIFile>(2); }
747 unsigned getLineNumber() const { return getUnsignedField(3); }
749 StringRef getObjCPropertyGetterName() const { return getStringField(4); }
750 StringRef getObjCPropertySetterName() const { return getStringField(5); }
751 bool isReadOnlyObjCProperty() const {
752 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
754 bool isReadWriteObjCProperty() const {
755 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
757 bool isAssignObjCProperty() const {
758 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
760 bool isRetainObjCProperty() const {
761 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
763 bool isCopyObjCProperty() const {
764 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
766 bool isNonAtomicObjCProperty() const {
767 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
770 /// Objective-C doesn't have an ODR, so there is no benefit in storing
771 /// the type as a DITypeRef here.
772 DIType getType() const { return getFieldAs<DIType>(7); }
774 /// Verify - Verify that a derived type descriptor is well formed.
778 /// \brief An imported module (C++ using directive or similar).
779 class DIImportedEntity : public DIDescriptor {
780 friend class DIDescriptor;
781 void printInternal(raw_ostream &OS) const;
784 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
785 DIScope getContext() const { return getFieldAs<DIScope>(1); }
786 DIDescriptor getEntity() const { return getFieldAs<DIDescriptor>(2); }
787 unsigned getLineNumber() const { return getUnsignedField(3); }
788 StringRef getName() const { return getStringField(4); }
792 /// getDISubprogram - Find subprogram that is enclosing this scope.
793 DISubprogram getDISubprogram(const MDNode *Scope);
795 /// getDICompositeType - Find underlying composite type.
796 DICompositeType getDICompositeType(DIType T);
798 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
799 /// to hold function specific information.
800 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
802 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
803 /// suitable to hold function specific information.
804 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
806 /// createInlinedVariable - Create a new inlined variable based on current
808 /// @param DV Current Variable.
809 /// @param InlinedScope Location at current variable is inlined.
810 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
811 LLVMContext &VMContext);
813 /// cleanseInlinedVariable - Remove inlined scope from the variable.
814 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
816 /// Construct DITypeIdentifierMap by going through retained types of each CU.
817 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
819 /// Strip debug info in the module if it exists.
820 /// To do this, we remove all calls to the debugger intrinsics and any named
821 /// metadata for debugging. We also remove debug locations for instructions.
822 /// Return true if module is modified.
823 bool StripDebugInfo(Module &M);
825 /// Return Debug Info Metadata Version by checking module flags.
826 unsigned getDebugMetadataVersionFromModule(const Module &M);
828 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
829 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
830 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
831 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
832 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
834 class DebugInfoFinder {
836 DebugInfoFinder() : TypeMapInitialized(false) {}
838 /// processModule - Process entire module and collect debug info
840 void processModule(const Module &M);
842 /// processDeclare - Process DbgDeclareInst.
843 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
844 /// Process DbgValueInst.
845 void processValue(const Module &M, const DbgValueInst *DVI);
846 /// processLocation - Process DILocation.
847 void processLocation(const Module &M, DILocation Loc);
853 /// Initialize TypeIdentifierMap.
854 void InitializeTypeMap(const Module &M);
856 /// processType - Process DIType.
857 void processType(DIType DT);
859 /// processSubprogram - Process DISubprogram.
860 void processSubprogram(DISubprogram SP);
862 void processScope(DIScope Scope);
864 /// addCompileUnit - Add compile unit into CUs.
865 bool addCompileUnit(DICompileUnit CU);
867 /// addGlobalVariable - Add global variable into GVs.
868 bool addGlobalVariable(DIGlobalVariable DIG);
870 // addSubprogram - Add subprogram into SPs.
871 bool addSubprogram(DISubprogram SP);
873 /// addType - Add type into Tys.
874 bool addType(DIType DT);
876 bool addScope(DIScope Scope);
879 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
880 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
881 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
882 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
883 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
885 iterator_range<compile_unit_iterator> compile_units() const {
886 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
889 iterator_range<subprogram_iterator> subprograms() const {
890 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
893 iterator_range<global_variable_iterator> global_variables() const {
894 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
897 iterator_range<type_iterator> types() const {
898 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
901 iterator_range<scope_iterator> scopes() const {
902 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
905 unsigned compile_unit_count() const { return CUs.size(); }
906 unsigned global_variable_count() const { return GVs.size(); }
907 unsigned subprogram_count() const { return SPs.size(); }
908 unsigned type_count() const { return TYs.size(); }
909 unsigned scope_count() const { return Scopes.size(); }
912 SmallVector<DICompileUnit, 8> CUs; // Compile Units
913 SmallVector<DISubprogram, 8> SPs; // Subprograms
914 SmallVector<DIGlobalVariable, 8> GVs; // Global Variables;
915 SmallVector<DIType, 8> TYs; // Types
916 SmallVector<DIScope, 8> Scopes; // Scopes
917 SmallPtrSet<MDNode *, 64> NodesSeen;
918 DITypeIdentifierMap TypeIdentifierMap;
919 /// Specify if TypeIdentifierMap is initialized.
920 bool TypeMapInitialized;
922 } // end namespace llvm