1 //===--- llvm/Analysis/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_ANALYSIS_DEBUGINFO_H
18 #define LLVM_ANALYSIS_DEBUGINFO_H
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/Dwarf.h"
43 class DILexicalBlockFile;
47 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
48 /// This should not be stored in a container, because underly MDNode may
49 /// change in certain situations.
54 FlagProtected = 1 << 1,
56 FlagAppleBlock = 1 << 3,
57 FlagBlockByrefStruct = 1 << 4,
59 FlagArtificial = 1 << 6,
60 FlagExplicit = 1 << 7,
61 FlagPrototyped = 1 << 8,
62 FlagObjcClassComplete = 1 << 9
65 const MDNode *DbgNode;
67 StringRef getStringField(unsigned Elt) const;
68 unsigned getUnsignedField(unsigned Elt) const {
69 return (unsigned)getUInt64Field(Elt);
71 uint64_t getUInt64Field(unsigned Elt) const;
72 DIDescriptor getDescriptorField(unsigned Elt) const;
74 template <typename DescTy>
75 DescTy getFieldAs(unsigned Elt) const {
76 return DescTy(getDescriptorField(Elt));
79 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
80 Constant *getConstantField(unsigned Elt) const;
81 Function *getFunctionField(unsigned Elt) const;
84 explicit DIDescriptor() : DbgNode(0) {}
85 explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
86 explicit DIDescriptor(const DIFile F);
87 explicit DIDescriptor(const DISubprogram F);
88 explicit DIDescriptor(const DILexicalBlockFile F);
89 explicit DIDescriptor(const DILexicalBlock F);
90 explicit DIDescriptor(const DIVariable F);
91 explicit DIDescriptor(const DIType F);
93 bool Verify() const { return DbgNode != 0; }
95 operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
96 MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
98 unsigned getVersion() const {
99 return getUnsignedField(0) & LLVMDebugVersionMask;
102 unsigned getTag() const {
103 return getUnsignedField(0) & ~LLVMDebugVersionMask;
106 /// print - print descriptor.
107 void print(raw_ostream &OS) const;
109 /// dump - print descriptor to dbgs() with a newline.
112 bool isDerivedType() const;
113 bool isCompositeType() const;
114 bool isBasicType() const;
115 bool isVariable() const;
116 bool isSubprogram() const;
117 bool isGlobalVariable() const;
118 bool isScope() const;
120 bool isCompileUnit() const;
121 bool isNameSpace() const;
122 bool isLexicalBlockFile() const;
123 bool isLexicalBlock() const;
124 bool isSubrange() const;
125 bool isEnumerator() const;
127 bool isGlobal() const;
128 bool isUnspecifiedParameter() const;
129 bool isTemplateTypeParameter() const;
130 bool isTemplateValueParameter() const;
133 /// DISubrange - This is used to represent ranges, for array bounds.
134 class DISubrange : public DIDescriptor {
136 explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
138 uint64_t getLo() const { return getUInt64Field(1); }
139 uint64_t getHi() const { return getUInt64Field(2); }
142 /// DIArray - This descriptor holds an array of descriptors.
143 class DIArray : public DIDescriptor {
145 explicit DIArray(const MDNode *N = 0)
148 unsigned getNumElements() const;
149 DIDescriptor getElement(unsigned Idx) const {
150 return getDescriptorField(Idx);
154 /// DIScope - A base class for various scopes.
155 class DIScope : public DIDescriptor {
156 virtual void anchor();
158 explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
159 virtual ~DIScope() {}
161 StringRef getFilename() const;
162 StringRef getDirectory() const;
165 /// DICompileUnit - A wrapper for a compile unit.
166 class DICompileUnit : public DIScope {
167 virtual void anchor();
169 explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
171 unsigned getLanguage() const { return getUnsignedField(2); }
172 StringRef getFilename() const { return getStringField(3); }
173 StringRef getDirectory() const { return getStringField(4); }
174 StringRef getProducer() const { return getStringField(5); }
176 /// isMain - Each input file is encoded as a separate compile unit in LLVM
177 /// debugging information output. However, many target specific tool chains
178 /// prefer to encode only one compile unit in an object file. In this
179 /// situation, the LLVM code generator will include debugging information
180 /// entities in the compile unit that is marked as main compile unit. The
181 /// code generator accepts maximum one main compile unit per module. If a
182 /// module does not contain any main compile unit then the code generator
183 /// will emit multiple compile units in the output object file.
185 bool isMain() const { return getUnsignedField(6) != 0; }
186 bool isOptimized() const { return getUnsignedField(7) != 0; }
187 StringRef getFlags() const { return getStringField(8); }
188 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
190 DIArray getEnumTypes() const;
191 DIArray getRetainedTypes() const;
192 DIArray getSubprograms() const;
193 DIArray getGlobalVariables() const;
195 /// Verify - Verify that a compile unit is well formed.
198 /// print - print compile unit.
199 void print(raw_ostream &OS) const;
201 /// dump - print compile unit to dbgs() with a newline.
205 /// DIFile - This is a wrapper for a file.
206 class DIFile : public DIScope {
207 virtual void anchor();
209 explicit DIFile(const MDNode *N = 0) : DIScope(N) {
210 if (DbgNode && !isFile())
213 StringRef getFilename() const { return getStringField(1); }
214 StringRef getDirectory() const { return getStringField(2); }
215 DICompileUnit getCompileUnit() const{
216 assert (getVersion() <= LLVMDebugVersion10 && "Invalid CompileUnit!");
217 return getFieldAs<DICompileUnit>(3);
221 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
222 /// FIXME: it seems strange that this doesn't have either a reference to the
223 /// type/precision or a file/line pair for location info.
224 class DIEnumerator : public DIDescriptor {
226 explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
228 StringRef getName() const { return getStringField(1); }
229 uint64_t getEnumValue() const { return getUInt64Field(2); }
232 /// DIType - This is a wrapper for a type.
233 /// FIXME: Types should be factored much better so that CV qualifiers and
234 /// others do not require a huge and empty descriptor full of zeros.
235 class DIType : public DIScope {
236 virtual void anchor();
238 // This ctor is used when the Tag has already been validated by a derived
240 DIType(const MDNode *N, bool, bool) : DIScope(N) {}
244 /// Verify - Verify that a type descriptor is well formed.
246 explicit DIType(const MDNode *N);
250 DIScope getContext() const { return getFieldAs<DIScope>(1); }
251 StringRef getName() const { return getStringField(2); }
252 DICompileUnit getCompileUnit() const{
253 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
254 if (getVersion() == llvm::LLVMDebugVersion7)
255 return getFieldAs<DICompileUnit>(3);
257 return getFieldAs<DIFile>(3).getCompileUnit();
259 DIFile getFile() const { return getFieldAs<DIFile>(3); }
260 unsigned getLineNumber() const { return getUnsignedField(4); }
261 uint64_t getSizeInBits() const { return getUInt64Field(5); }
262 uint64_t getAlignInBits() const { return getUInt64Field(6); }
263 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
264 // carry this is just plain insane.
265 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
266 unsigned getFlags() const { return getUnsignedField(8); }
267 bool isPrivate() const {
268 return (getFlags() & FlagPrivate) != 0;
270 bool isProtected() const {
271 return (getFlags() & FlagProtected) != 0;
273 bool isForwardDecl() const {
274 return (getFlags() & FlagFwdDecl) != 0;
276 // isAppleBlock - Return true if this is the Apple Blocks extension.
277 bool isAppleBlockExtension() const {
278 return (getFlags() & FlagAppleBlock) != 0;
280 bool isBlockByrefStruct() const {
281 return (getFlags() & FlagBlockByrefStruct) != 0;
283 bool isVirtual() const {
284 return (getFlags() & FlagVirtual) != 0;
286 bool isArtificial() const {
287 return (getFlags() & FlagArtificial) != 0;
289 bool isObjcClassComplete() const {
290 return (getFlags() & FlagObjcClassComplete) != 0;
292 bool isValid() const {
293 return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
295 StringRef getDirectory() const {
296 if (getVersion() == llvm::LLVMDebugVersion7)
297 return getCompileUnit().getDirectory();
299 return getFieldAs<DIFile>(3).getDirectory();
301 StringRef getFilename() const {
302 if (getVersion() == llvm::LLVMDebugVersion7)
303 return getCompileUnit().getFilename();
305 return getFieldAs<DIFile>(3).getFilename();
308 /// isUnsignedDIType - Return true if type encoding is unsigned.
309 bool isUnsignedDIType();
311 /// replaceAllUsesWith - Replace all uses of debug info referenced by
313 void replaceAllUsesWith(DIDescriptor &D);
314 void replaceAllUsesWith(MDNode *D);
316 /// print - print type.
317 void print(raw_ostream &OS) const;
319 /// dump - print type to dbgs() with a newline.
323 /// DIBasicType - A basic type, like 'int' or 'float'.
324 class DIBasicType : public DIType {
325 virtual void anchor();
327 explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
329 unsigned getEncoding() const { return getUnsignedField(9); }
331 /// Verify - Verify that a basic type descriptor is well formed.
334 /// print - print basic type.
335 void print(raw_ostream &OS) const;
337 /// dump - print basic type to dbgs() with a newline.
341 /// DIDerivedType - A simple derived type, like a const qualified type,
342 /// a typedef, a pointer or reference, etc.
343 class DIDerivedType : public DIType {
344 virtual void anchor();
346 explicit DIDerivedType(const MDNode *N, bool, bool)
347 : DIType(N, true, true) {}
349 explicit DIDerivedType(const MDNode *N = 0)
350 : DIType(N, true, true) {}
352 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
354 /// getOriginalTypeSize - If this type is derived from a base type then
355 /// return base type size.
356 uint64_t getOriginalTypeSize() const;
358 StringRef getObjCPropertyName() const { return getStringField(10); }
359 StringRef getObjCPropertyGetterName() const {
360 return getStringField(11);
362 StringRef getObjCPropertySetterName() const {
363 return getStringField(12);
365 bool isReadOnlyObjCProperty() {
366 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
368 bool isReadWriteObjCProperty() {
369 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
371 bool isAssignObjCProperty() {
372 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
374 bool isRetainObjCProperty() {
375 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
377 bool isCopyObjCProperty() {
378 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
380 bool isNonAtomicObjCProperty() {
381 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
384 /// Verify - Verify that a derived type descriptor is well formed.
387 /// print - print derived type.
388 void print(raw_ostream &OS) const;
390 /// dump - print derived type to dbgs() with a newline.
394 /// DICompositeType - This descriptor holds a type that can refer to multiple
395 /// other types, like a function or struct.
396 /// FIXME: Why is this a DIDerivedType??
397 class DICompositeType : public DIDerivedType {
398 virtual void anchor();
400 explicit DICompositeType(const MDNode *N = 0)
401 : DIDerivedType(N, true, true) {
402 if (N && !isCompositeType())
406 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
407 unsigned getRunTimeLang() const { return getUnsignedField(11); }
408 DICompositeType getContainingType() const {
409 return getFieldAs<DICompositeType>(12);
411 DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
413 /// Verify - Verify that a composite type descriptor is well formed.
416 /// print - print composite type.
417 void print(raw_ostream &OS) const;
419 /// dump - print composite type to dbgs() with a newline.
423 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
424 class DITemplateTypeParameter : public DIDescriptor {
426 explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
428 DIScope getContext() const { return getFieldAs<DIScope>(1); }
429 StringRef getName() const { return getStringField(2); }
430 DIType getType() const { return getFieldAs<DIType>(3); }
431 StringRef getFilename() const {
432 return getFieldAs<DIFile>(4).getFilename();
434 StringRef getDirectory() const {
435 return getFieldAs<DIFile>(4).getDirectory();
437 unsigned getLineNumber() const { return getUnsignedField(5); }
438 unsigned getColumnNumber() const { return getUnsignedField(6); }
441 /// DITemplateValueParameter - This is a wrapper for template value parameter.
442 class DITemplateValueParameter : public DIDescriptor {
444 explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
446 DIScope getContext() const { return getFieldAs<DIScope>(1); }
447 StringRef getName() const { return getStringField(2); }
448 DIType getType() const { return getFieldAs<DIType>(3); }
449 uint64_t getValue() const { return getUInt64Field(4); }
450 StringRef getFilename() const {
451 return getFieldAs<DIFile>(5).getFilename();
453 StringRef getDirectory() const {
454 return getFieldAs<DIFile>(5).getDirectory();
456 unsigned getLineNumber() const { return getUnsignedField(6); }
457 unsigned getColumnNumber() const { return getUnsignedField(7); }
460 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
461 class DISubprogram : public DIScope {
462 virtual void anchor();
464 explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
466 DIScope getContext() const { return getFieldAs<DIScope>(2); }
467 StringRef getName() const { return getStringField(3); }
468 StringRef getDisplayName() const { return getStringField(4); }
469 StringRef getLinkageName() const { return getStringField(5); }
470 DICompileUnit getCompileUnit() const{
471 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
472 if (getVersion() == llvm::LLVMDebugVersion7)
473 return getFieldAs<DICompileUnit>(6);
475 return getFieldAs<DIFile>(6).getCompileUnit();
477 unsigned getLineNumber() const { return getUnsignedField(7); }
478 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
480 /// getReturnTypeName - Subprogram return types are encoded either as
481 /// DIType or as DICompositeType.
482 StringRef getReturnTypeName() const {
483 DICompositeType DCT(getFieldAs<DICompositeType>(8));
485 DIArray A = DCT.getTypeArray();
486 DIType T(A.getElement(0));
489 DIType T(getFieldAs<DIType>(8));
493 /// isLocalToUnit - Return true if this subprogram is local to the current
494 /// compile unit, like 'static' in C.
495 unsigned isLocalToUnit() const { return getUnsignedField(9); }
496 unsigned isDefinition() const { return getUnsignedField(10); }
498 unsigned getVirtuality() const { return getUnsignedField(11); }
499 unsigned getVirtualIndex() const { return getUnsignedField(12); }
501 DICompositeType getContainingType() const {
502 return getFieldAs<DICompositeType>(13);
504 unsigned isArtificial() const {
505 if (getVersion() <= llvm::LLVMDebugVersion8)
506 return getUnsignedField(14);
507 return (getUnsignedField(14) & FlagArtificial) != 0;
509 /// isPrivate - Return true if this subprogram has "private"
510 /// access specifier.
511 bool isPrivate() const {
512 if (getVersion() <= llvm::LLVMDebugVersion8)
514 return (getUnsignedField(14) & FlagPrivate) != 0;
516 /// isProtected - Return true if this subprogram has "protected"
517 /// access specifier.
518 bool isProtected() const {
519 if (getVersion() <= llvm::LLVMDebugVersion8)
521 return (getUnsignedField(14) & FlagProtected) != 0;
523 /// isExplicit - Return true if this subprogram is marked as explicit.
524 bool isExplicit() const {
525 if (getVersion() <= llvm::LLVMDebugVersion8)
527 return (getUnsignedField(14) & FlagExplicit) != 0;
529 /// isPrototyped - Return true if this subprogram is prototyped.
530 bool isPrototyped() const {
531 if (getVersion() <= llvm::LLVMDebugVersion8)
533 return (getUnsignedField(14) & FlagPrototyped) != 0;
536 unsigned isOptimized() const;
538 StringRef getFilename() const {
539 if (getVersion() == llvm::LLVMDebugVersion7)
540 return getCompileUnit().getFilename();
542 return getFieldAs<DIFile>(6).getFilename();
545 StringRef getDirectory() const {
546 if (getVersion() == llvm::LLVMDebugVersion7)
547 return getCompileUnit().getFilename();
549 return getFieldAs<DIFile>(6).getDirectory();
552 /// Verify - Verify that a subprogram descriptor is well formed.
555 /// print - print subprogram.
556 void print(raw_ostream &OS) const;
558 /// dump - print subprogram to dbgs() with a newline.
561 /// describes - Return true if this subprogram provides debugging
562 /// information for the function F.
563 bool describes(const Function *F);
565 Function *getFunction() const { return getFunctionField(16); }
566 DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
567 DISubprogram getFunctionDeclaration() const {
568 return getFieldAs<DISubprogram>(18);
570 MDNode *getVariablesNodes() const;
571 DIArray getVariables() const;
574 /// DIGlobalVariable - This is a wrapper for a global variable.
575 class DIGlobalVariable : public DIDescriptor {
577 explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
579 DIScope getContext() const { return getFieldAs<DIScope>(2); }
580 StringRef getName() const { return getStringField(3); }
581 StringRef getDisplayName() const { return getStringField(4); }
582 StringRef getLinkageName() const { return getStringField(5); }
583 DICompileUnit getCompileUnit() const{
584 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
585 if (getVersion() == llvm::LLVMDebugVersion7)
586 return getFieldAs<DICompileUnit>(6);
588 DIFile F = getFieldAs<DIFile>(6);
589 return F.getCompileUnit();
591 StringRef getFilename() const {
592 if (getVersion() <= llvm::LLVMDebugVersion10)
593 return getContext().getFilename();
594 return getFieldAs<DIFile>(6).getFilename();
596 StringRef getDirectory() const {
597 if (getVersion() <= llvm::LLVMDebugVersion10)
598 return getContext().getDirectory();
599 return getFieldAs<DIFile>(6).getDirectory();
603 unsigned getLineNumber() const { return getUnsignedField(7); }
604 DIType getType() const { return getFieldAs<DIType>(8); }
605 unsigned isLocalToUnit() const { return getUnsignedField(9); }
606 unsigned isDefinition() const { return getUnsignedField(10); }
608 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
609 Constant *getConstant() const { return getConstantField(11); }
611 /// Verify - Verify that a global variable descriptor is well formed.
614 /// print - print global variable.
615 void print(raw_ostream &OS) const;
617 /// dump - print global variable to dbgs() with a newline.
621 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
623 class DIVariable : public DIDescriptor {
625 explicit DIVariable(const MDNode *N = 0)
628 DIScope getContext() const { return getFieldAs<DIScope>(1); }
629 StringRef getName() const { return getStringField(2); }
630 DICompileUnit getCompileUnit() const {
631 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
632 if (getVersion() == llvm::LLVMDebugVersion7)
633 return getFieldAs<DICompileUnit>(3);
635 DIFile F = getFieldAs<DIFile>(3);
636 return F.getCompileUnit();
638 unsigned getLineNumber() const {
639 return (getUnsignedField(4) << 8) >> 8;
641 unsigned getArgNumber() const {
642 unsigned L = getUnsignedField(4);
645 DIType getType() const { return getFieldAs<DIType>(5); }
647 /// isArtificial - Return true if this variable is marked as "artificial".
648 bool isArtificial() const {
649 if (getVersion() <= llvm::LLVMDebugVersion8)
651 return (getUnsignedField(6) & FlagArtificial) != 0;
654 /// getInlinedAt - If this variable is inlined then return inline location.
655 MDNode *getInlinedAt() const;
657 /// Verify - Verify that a variable descriptor is well formed.
660 /// HasComplexAddr - Return true if the variable has a complex address.
661 bool hasComplexAddress() const {
662 return getNumAddrElements() > 0;
665 unsigned getNumAddrElements() const;
667 uint64_t getAddrElement(unsigned Idx) const {
668 if (getVersion() <= llvm::LLVMDebugVersion8)
669 return getUInt64Field(Idx+6);
670 if (getVersion() == llvm::LLVMDebugVersion9)
671 return getUInt64Field(Idx+7);
672 return getUInt64Field(Idx+8);
675 /// isBlockByrefVariable - Return true if the variable was declared as
676 /// a "__block" variable (Apple Blocks).
677 bool isBlockByrefVariable() const {
678 return getType().isBlockByrefStruct();
681 /// isInlinedFnArgument - Return trule if this variable provides debugging
682 /// information for an inlined function arguments.
683 bool isInlinedFnArgument(const Function *CurFn);
685 /// print - print variable.
686 void print(raw_ostream &OS) const;
688 void printExtendedName(raw_ostream &OS) const;
690 /// dump - print variable to dbgs() with a newline.
694 /// DILexicalBlock - This is a wrapper for a lexical block.
695 class DILexicalBlock : public DIScope {
696 virtual void anchor();
698 explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
699 DIScope getContext() const { return getFieldAs<DIScope>(1); }
700 unsigned getLineNumber() const { return getUnsignedField(2); }
701 unsigned getColumnNumber() const { return getUnsignedField(3); }
702 StringRef getDirectory() const {
703 StringRef dir = getFieldAs<DIFile>(4).getDirectory();
704 return !dir.empty() ? dir : getContext().getDirectory();
706 StringRef getFilename() const {
707 StringRef filename = getFieldAs<DIFile>(4).getFilename();
708 return !filename.empty() ? filename : getContext().getFilename();
712 /// DILexicalBlockFile - This is a wrapper for a lexical block with
713 /// a filename change.
714 class DILexicalBlockFile : public DIScope {
715 virtual void anchor();
717 explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
718 DIScope getContext() const { return getScope().getContext(); }
719 unsigned getLineNumber() const { return getScope().getLineNumber(); }
720 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
721 StringRef getDirectory() const {
722 StringRef dir = getFieldAs<DIFile>(2).getDirectory();
723 return !dir.empty() ? dir : getContext().getDirectory();
725 StringRef getFilename() const {
726 StringRef filename = getFieldAs<DIFile>(2).getFilename();
727 assert(!filename.empty() && "Why'd you create this then?");
730 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
733 /// DINameSpace - A wrapper for a C++ style name space.
734 class DINameSpace : public DIScope {
735 virtual void anchor();
737 explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
738 DIScope getContext() const { return getFieldAs<DIScope>(1); }
739 StringRef getName() const { return getStringField(2); }
740 StringRef getDirectory() const {
741 return getFieldAs<DIFile>(3).getDirectory();
743 StringRef getFilename() const {
744 return getFieldAs<DIFile>(3).getFilename();
746 DICompileUnit getCompileUnit() const{
747 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
748 if (getVersion() == llvm::LLVMDebugVersion7)
749 return getFieldAs<DICompileUnit>(3);
751 return getFieldAs<DIFile>(3).getCompileUnit();
753 unsigned getLineNumber() const { return getUnsignedField(4); }
757 /// DILocation - This object holds location information. This object
758 /// is not associated with any DWARF tag.
759 class DILocation : public DIDescriptor {
761 explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
763 unsigned getLineNumber() const { return getUnsignedField(0); }
764 unsigned getColumnNumber() const { return getUnsignedField(1); }
765 DIScope getScope() const { return getFieldAs<DIScope>(2); }
766 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
767 StringRef getFilename() const { return getScope().getFilename(); }
768 StringRef getDirectory() const { return getScope().getDirectory(); }
772 /// getDISubprogram - Find subprogram that is enclosing this scope.
773 DISubprogram getDISubprogram(const MDNode *Scope);
775 /// getDICompositeType - Find underlying composite type.
776 DICompositeType getDICompositeType(DIType T);
778 /// isSubprogramContext - Return true if Context is either a subprogram
779 /// or another context nested inside a subprogram.
780 bool isSubprogramContext(const MDNode *Context);
782 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
783 /// to hold function specific information.
784 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
786 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
787 /// suitable to hold function specific information.
788 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
790 /// createInlinedVariable - Create a new inlined variable based on current
792 /// @param DV Current Variable.
793 /// @param InlinedScope Location at current variable is inlined.
794 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
795 LLVMContext &VMContext);
797 /// cleanseInlinedVariable - Remove inlined scope from the variable.
798 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
800 class DebugInfoFinder {
802 /// processModule - Process entire module and collect debug info
804 void processModule(Module &M);
807 /// processType - Process DIType.
808 void processType(DIType DT);
810 /// processLexicalBlock - Process DILexicalBlock.
811 void processLexicalBlock(DILexicalBlock LB);
813 /// processSubprogram - Process DISubprogram.
814 void processSubprogram(DISubprogram SP);
816 /// processDeclare - Process DbgDeclareInst.
817 void processDeclare(DbgDeclareInst *DDI);
819 /// processLocation - Process DILocation.
820 void processLocation(DILocation Loc);
822 /// addCompileUnit - Add compile unit into CUs.
823 bool addCompileUnit(DICompileUnit CU);
825 /// addGlobalVariable - Add global variable into GVs.
826 bool addGlobalVariable(DIGlobalVariable DIG);
828 // addSubprogram - Add subprogram into SPs.
829 bool addSubprogram(DISubprogram SP);
831 /// addType - Add type into Tys.
832 bool addType(DIType DT);
835 typedef SmallVector<MDNode *, 8>::const_iterator iterator;
836 iterator compile_unit_begin() const { return CUs.begin(); }
837 iterator compile_unit_end() const { return CUs.end(); }
838 iterator subprogram_begin() const { return SPs.begin(); }
839 iterator subprogram_end() const { return SPs.end(); }
840 iterator global_variable_begin() const { return GVs.begin(); }
841 iterator global_variable_end() const { return GVs.end(); }
842 iterator type_begin() const { return TYs.begin(); }
843 iterator type_end() const { return TYs.end(); }
845 unsigned compile_unit_count() const { return CUs.size(); }
846 unsigned global_variable_count() const { return GVs.size(); }
847 unsigned subprogram_count() const { return SPs.size(); }
848 unsigned type_count() const { return TYs.size(); }
851 SmallVector<MDNode *, 8> CUs; // Compile Units
852 SmallVector<MDNode *, 8> SPs; // Subprograms
853 SmallVector<MDNode *, 8> GVs; // Global Variables;
854 SmallVector<MDNode *, 8> TYs; // Types
855 SmallPtrSet<MDNode *, 64> NodesSeen;
857 } // end namespace llvm