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;
48 /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
49 /// This should not be stored in a container, because the underlying MDNode
50 /// may change in certain situations.
55 FlagProtected = 1 << 1,
57 FlagAppleBlock = 1 << 3,
58 FlagBlockByrefStruct = 1 << 4,
60 FlagArtificial = 1 << 6,
61 FlagExplicit = 1 << 7,
62 FlagPrototyped = 1 << 8,
63 FlagObjcClassComplete = 1 << 9
66 const MDNode *DbgNode;
68 StringRef getStringField(unsigned Elt) const;
69 unsigned getUnsignedField(unsigned Elt) const {
70 return (unsigned)getUInt64Field(Elt);
72 uint64_t getUInt64Field(unsigned Elt) const;
73 DIDescriptor getDescriptorField(unsigned Elt) const;
75 template <typename DescTy>
76 DescTy getFieldAs(unsigned Elt) const {
77 return DescTy(getDescriptorField(Elt));
80 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
81 Constant *getConstantField(unsigned Elt) const;
82 Function *getFunctionField(unsigned Elt) const;
85 explicit DIDescriptor() : DbgNode(0) {}
86 explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
87 explicit DIDescriptor(const DIFile F);
88 explicit DIDescriptor(const DISubprogram F);
89 explicit DIDescriptor(const DILexicalBlockFile F);
90 explicit DIDescriptor(const DILexicalBlock F);
91 explicit DIDescriptor(const DIVariable F);
92 explicit DIDescriptor(const DIType F);
94 bool Verify() const { return DbgNode != 0; }
96 operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
97 MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
99 unsigned getVersion() const {
100 return getUnsignedField(0) & LLVMDebugVersionMask;
103 unsigned getTag() const {
104 return getUnsignedField(0) & ~LLVMDebugVersionMask;
107 bool isDerivedType() const;
108 bool isCompositeType() const;
109 bool isBasicType() const;
110 bool isVariable() const;
111 bool isSubprogram() const;
112 bool isGlobalVariable() const;
113 bool isScope() const;
115 bool isCompileUnit() const;
116 bool isNameSpace() const;
117 bool isLexicalBlockFile() const;
118 bool isLexicalBlock() const;
119 bool isSubrange() const;
120 bool isEnumerator() const;
122 bool isGlobal() const;
123 bool isUnspecifiedParameter() const;
124 bool isTemplateTypeParameter() const;
125 bool isTemplateValueParameter() const;
126 bool isObjCProperty() const;
128 /// print - print descriptor.
129 void print(raw_ostream &OS) const;
131 /// dump - print descriptor to dbgs() with a newline.
135 /// DISubrange - This is used to represent ranges, for array bounds.
136 class DISubrange : public DIDescriptor {
137 friend class DIDescriptor;
138 void printInternal(raw_ostream &OS) const;
140 explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
142 uint64_t getLo() const { return getUInt64Field(1); }
143 uint64_t getHi() const { return getUInt64Field(2); }
146 /// DIArray - This descriptor holds an array of descriptors.
147 class DIArray : public DIDescriptor {
149 explicit DIArray(const MDNode *N = 0)
152 unsigned getNumElements() const;
153 DIDescriptor getElement(unsigned Idx) const {
154 return getDescriptorField(Idx);
158 /// DIScope - A base class for various scopes.
159 class DIScope : public DIDescriptor {
161 friend class DIDescriptor;
162 void printInternal(raw_ostream &OS) const;
164 explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
166 StringRef getFilename() const;
167 StringRef getDirectory() const;
170 /// DICompileUnit - A wrapper for a compile unit.
171 class DICompileUnit : public DIScope {
172 friend class DIDescriptor;
173 void printInternal(raw_ostream &OS) const;
175 explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
177 unsigned getLanguage() const { return getUnsignedField(2); }
178 StringRef getFilename() const { return getStringField(3); }
179 StringRef getDirectory() const { return getStringField(4); }
180 StringRef getProducer() const { return getStringField(5); }
182 /// isMain - Each input file is encoded as a separate compile unit in LLVM
183 /// debugging information output. However, many target specific tool chains
184 /// prefer to encode only one compile unit in an object file. In this
185 /// situation, the LLVM code generator will include debugging information
186 /// entities in the compile unit that is marked as main compile unit. The
187 /// code generator accepts maximum one main compile unit per module. If a
188 /// module does not contain any main compile unit then the code generator
189 /// will emit multiple compile units in the output object file.
191 bool isMain() const { return getUnsignedField(6) != 0; }
192 bool isOptimized() const { return getUnsignedField(7) != 0; }
193 StringRef getFlags() const { return getStringField(8); }
194 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
196 DIArray getEnumTypes() const;
197 DIArray getRetainedTypes() const;
198 DIArray getSubprograms() const;
199 DIArray getGlobalVariables() const;
201 /// Verify - Verify that a compile unit is well formed.
205 /// DIFile - This is a wrapper for a file.
206 class DIFile : public DIScope {
207 friend class DIDescriptor;
208 void printInternal(raw_ostream &OS) const {} // FIXME: Output something?
210 explicit DIFile(const MDNode *N = 0) : DIScope(N) {
211 if (DbgNode && !isFile())
214 StringRef getFilename() const { return getStringField(1); }
215 StringRef getDirectory() const { return getStringField(2); }
216 DICompileUnit getCompileUnit() const{
217 assert (getVersion() <= LLVMDebugVersion10 && "Invalid CompileUnit!");
218 return getFieldAs<DICompileUnit>(3);
222 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
223 /// FIXME: it seems strange that this doesn't have either a reference to the
224 /// type/precision or a file/line pair for location info.
225 class DIEnumerator : public DIDescriptor {
226 friend class DIDescriptor;
227 void printInternal(raw_ostream &OS) const;
229 explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
231 StringRef getName() const { return getStringField(1); }
232 uint64_t getEnumValue() const { return getUInt64Field(2); }
235 /// DIType - This is a wrapper for a type.
236 /// FIXME: Types should be factored much better so that CV qualifiers and
237 /// others do not require a huge and empty descriptor full of zeros.
238 class DIType : public DIScope {
240 friend class DIDescriptor;
241 void printInternal(raw_ostream &OS) const;
242 // This ctor is used when the Tag has already been validated by a derived
244 DIType(const MDNode *N, bool, bool) : DIScope(N) {}
246 /// Verify - Verify that a type descriptor is well formed.
248 explicit DIType(const MDNode *N);
251 DIScope getContext() const { return getFieldAs<DIScope>(1); }
252 StringRef getName() const { return getStringField(2); }
253 DICompileUnit getCompileUnit() const{
254 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
255 if (getVersion() == llvm::LLVMDebugVersion7)
256 return getFieldAs<DICompileUnit>(3);
258 return getFieldAs<DIFile>(3).getCompileUnit();
260 DIFile getFile() const { return getFieldAs<DIFile>(3); }
261 unsigned getLineNumber() const { return getUnsignedField(4); }
262 uint64_t getSizeInBits() const { return getUInt64Field(5); }
263 uint64_t getAlignInBits() const { return getUInt64Field(6); }
264 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
265 // carry this is just plain insane.
266 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
267 unsigned getFlags() const { return getUnsignedField(8); }
268 bool isPrivate() const {
269 return (getFlags() & FlagPrivate) != 0;
271 bool isProtected() const {
272 return (getFlags() & FlagProtected) != 0;
274 bool isForwardDecl() const {
275 return (getFlags() & FlagFwdDecl) != 0;
277 // isAppleBlock - Return true if this is the Apple Blocks extension.
278 bool isAppleBlockExtension() const {
279 return (getFlags() & FlagAppleBlock) != 0;
281 bool isBlockByrefStruct() const {
282 return (getFlags() & FlagBlockByrefStruct) != 0;
284 bool isVirtual() const {
285 return (getFlags() & FlagVirtual) != 0;
287 bool isArtificial() const {
288 return (getFlags() & FlagArtificial) != 0;
290 bool isObjcClassComplete() const {
291 return (getFlags() & FlagObjcClassComplete) != 0;
293 bool isValid() const {
294 return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
296 StringRef getDirectory() const {
297 if (getVersion() == llvm::LLVMDebugVersion7)
298 return getCompileUnit().getDirectory();
300 return getFieldAs<DIFile>(3).getDirectory();
302 StringRef getFilename() const {
303 if (getVersion() == llvm::LLVMDebugVersion7)
304 return getCompileUnit().getFilename();
306 return getFieldAs<DIFile>(3).getFilename();
309 /// isUnsignedDIType - Return true if type encoding is unsigned.
310 bool isUnsignedDIType();
312 /// replaceAllUsesWith - Replace all uses of debug info referenced by
314 void replaceAllUsesWith(DIDescriptor &D);
315 void replaceAllUsesWith(MDNode *D);
318 /// DIBasicType - A basic type, like 'int' or 'float'.
319 class DIBasicType : public DIType {
321 explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
323 unsigned getEncoding() const { return getUnsignedField(9); }
325 /// Verify - Verify that a basic type descriptor is well formed.
329 /// DIDerivedType - A simple derived type, like a const qualified type,
330 /// a typedef, a pointer or reference, etc.
331 class DIDerivedType : public DIType {
332 friend class DIDescriptor;
333 void printInternal(raw_ostream &OS) const;
335 explicit DIDerivedType(const MDNode *N, bool, bool)
336 : DIType(N, true, true) {}
338 explicit DIDerivedType(const MDNode *N = 0)
339 : DIType(N, true, true) {}
341 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
343 /// getOriginalTypeSize - If this type is derived from a base type then
344 /// return base type size.
345 uint64_t getOriginalTypeSize() const;
347 /// getObjCProperty - Return property node, if this ivar is
348 /// associated with one.
349 MDNode *getObjCProperty() const;
351 StringRef getObjCPropertyName() const {
352 if (getVersion() > LLVMDebugVersion11)
354 return getStringField(10);
356 StringRef getObjCPropertyGetterName() const {
357 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
358 return getStringField(11);
360 StringRef getObjCPropertySetterName() const {
361 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
362 return getStringField(12);
364 bool isReadOnlyObjCProperty() {
365 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
366 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
368 bool isReadWriteObjCProperty() {
369 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
370 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
372 bool isAssignObjCProperty() {
373 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
374 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
376 bool isRetainObjCProperty() {
377 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
378 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
380 bool isCopyObjCProperty() {
381 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
382 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
384 bool isNonAtomicObjCProperty() {
385 assert (getVersion() <= LLVMDebugVersion11 && "Invalid Request");
386 return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
389 /// Verify - Verify that a derived type descriptor is well formed.
393 /// DICompositeType - This descriptor holds a type that can refer to multiple
394 /// other types, like a function or struct.
395 /// FIXME: Why is this a DIDerivedType??
396 class DICompositeType : public DIDerivedType {
397 friend class DIDescriptor;
398 void printInternal(raw_ostream &OS) const;
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.
417 /// DITemplateTypeParameter - This is a wrapper for template type parameter.
418 class DITemplateTypeParameter : public DIDescriptor {
420 explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
422 DIScope getContext() const { return getFieldAs<DIScope>(1); }
423 StringRef getName() const { return getStringField(2); }
424 DIType getType() const { return getFieldAs<DIType>(3); }
425 StringRef getFilename() const {
426 return getFieldAs<DIFile>(4).getFilename();
428 StringRef getDirectory() const {
429 return getFieldAs<DIFile>(4).getDirectory();
431 unsigned getLineNumber() const { return getUnsignedField(5); }
432 unsigned getColumnNumber() const { return getUnsignedField(6); }
435 /// DITemplateValueParameter - This is a wrapper for template value parameter.
436 class DITemplateValueParameter : public DIDescriptor {
438 explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
440 DIScope getContext() const { return getFieldAs<DIScope>(1); }
441 StringRef getName() const { return getStringField(2); }
442 DIType getType() const { return getFieldAs<DIType>(3); }
443 uint64_t getValue() const { return getUInt64Field(4); }
444 StringRef getFilename() const {
445 return getFieldAs<DIFile>(5).getFilename();
447 StringRef getDirectory() const {
448 return getFieldAs<DIFile>(5).getDirectory();
450 unsigned getLineNumber() const { return getUnsignedField(6); }
451 unsigned getColumnNumber() const { return getUnsignedField(7); }
454 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
455 class DISubprogram : public DIScope {
456 friend class DIDescriptor;
457 void printInternal(raw_ostream &OS) const;
459 explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
461 DIScope getContext() const { return getFieldAs<DIScope>(2); }
462 StringRef getName() const { return getStringField(3); }
463 StringRef getDisplayName() const { return getStringField(4); }
464 StringRef getLinkageName() const { return getStringField(5); }
465 DICompileUnit getCompileUnit() const{
466 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
467 if (getVersion() == llvm::LLVMDebugVersion7)
468 return getFieldAs<DICompileUnit>(6);
470 return getFieldAs<DIFile>(6).getCompileUnit();
472 unsigned getLineNumber() const { return getUnsignedField(7); }
473 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
475 /// getReturnTypeName - Subprogram return types are encoded either as
476 /// DIType or as DICompositeType.
477 StringRef getReturnTypeName() const {
478 DICompositeType DCT(getFieldAs<DICompositeType>(8));
480 DIArray A = DCT.getTypeArray();
481 DIType T(A.getElement(0));
484 DIType T(getFieldAs<DIType>(8));
488 /// isLocalToUnit - Return true if this subprogram is local to the current
489 /// compile unit, like 'static' in C.
490 unsigned isLocalToUnit() const { return getUnsignedField(9); }
491 unsigned isDefinition() const { return getUnsignedField(10); }
493 unsigned getVirtuality() const { return getUnsignedField(11); }
494 unsigned getVirtualIndex() const { return getUnsignedField(12); }
496 DICompositeType getContainingType() const {
497 return getFieldAs<DICompositeType>(13);
500 unsigned isArtificial() const {
501 if (getVersion() <= llvm::LLVMDebugVersion8)
502 return getUnsignedField(14);
503 return (getUnsignedField(14) & FlagArtificial) != 0;
505 /// isPrivate - Return true if this subprogram has "private"
506 /// access specifier.
507 bool isPrivate() const {
508 if (getVersion() <= llvm::LLVMDebugVersion8)
510 return (getUnsignedField(14) & FlagPrivate) != 0;
512 /// isProtected - Return true if this subprogram has "protected"
513 /// access specifier.
514 bool isProtected() const {
515 if (getVersion() <= llvm::LLVMDebugVersion8)
517 return (getUnsignedField(14) & FlagProtected) != 0;
519 /// isExplicit - Return true if this subprogram is marked as explicit.
520 bool isExplicit() const {
521 if (getVersion() <= llvm::LLVMDebugVersion8)
523 return (getUnsignedField(14) & FlagExplicit) != 0;
525 /// isPrototyped - Return true if this subprogram is prototyped.
526 bool isPrototyped() const {
527 if (getVersion() <= llvm::LLVMDebugVersion8)
529 return (getUnsignedField(14) & FlagPrototyped) != 0;
532 unsigned isOptimized() const;
534 StringRef getFilename() const {
535 if (getVersion() == llvm::LLVMDebugVersion7)
536 return getCompileUnit().getFilename();
538 return getFieldAs<DIFile>(6).getFilename();
541 StringRef getDirectory() const {
542 if (getVersion() == llvm::LLVMDebugVersion7)
543 return getCompileUnit().getFilename();
545 return getFieldAs<DIFile>(6).getDirectory();
548 /// getScopeLineNumber - Get the beginning of the scope of the
549 /// function, not necessarily where the name of the program
551 unsigned getScopeLineNumber() const { return getUnsignedField(20); }
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(16); }
561 DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
562 DISubprogram getFunctionDeclaration() const {
563 return getFieldAs<DISubprogram>(18);
565 MDNode *getVariablesNodes() const;
566 DIArray getVariables() const;
569 /// DIGlobalVariable - This is a wrapper for a global variable.
570 class DIGlobalVariable : public DIDescriptor {
571 friend class DIDescriptor;
572 void printInternal(raw_ostream &OS) const;
574 explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
576 DIScope getContext() const { return getFieldAs<DIScope>(2); }
577 StringRef getName() const { return getStringField(3); }
578 StringRef getDisplayName() const { return getStringField(4); }
579 StringRef getLinkageName() const { return getStringField(5); }
580 DICompileUnit getCompileUnit() const{
581 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
582 if (getVersion() == llvm::LLVMDebugVersion7)
583 return getFieldAs<DICompileUnit>(6);
585 DIFile F = getFieldAs<DIFile>(6);
586 return F.getCompileUnit();
588 StringRef getFilename() const {
589 if (getVersion() <= llvm::LLVMDebugVersion10)
590 return getContext().getFilename();
591 return getFieldAs<DIFile>(6).getFilename();
593 StringRef getDirectory() const {
594 if (getVersion() <= llvm::LLVMDebugVersion10)
595 return getContext().getDirectory();
596 return getFieldAs<DIFile>(6).getDirectory();
600 unsigned getLineNumber() const { return getUnsignedField(7); }
601 DIType getType() const { return getFieldAs<DIType>(8); }
602 unsigned isLocalToUnit() const { return getUnsignedField(9); }
603 unsigned isDefinition() const { return getUnsignedField(10); }
605 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
606 Constant *getConstant() const { return getConstantField(11); }
608 /// Verify - Verify that a global variable descriptor is well formed.
612 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
614 class DIVariable : public DIDescriptor {
615 friend class DIDescriptor;
616 void printInternal(raw_ostream &OS) const;
618 explicit DIVariable(const MDNode *N = 0)
621 DIScope getContext() const { return getFieldAs<DIScope>(1); }
622 StringRef getName() const { return getStringField(2); }
623 DICompileUnit getCompileUnit() const {
624 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
625 if (getVersion() == llvm::LLVMDebugVersion7)
626 return getFieldAs<DICompileUnit>(3);
628 DIFile F = getFieldAs<DIFile>(3);
629 return F.getCompileUnit();
631 unsigned getLineNumber() const {
632 return (getUnsignedField(4) << 8) >> 8;
634 unsigned getArgNumber() const {
635 unsigned L = getUnsignedField(4);
638 DIType getType() const { return getFieldAs<DIType>(5); }
640 /// isArtificial - Return true if this variable is marked as "artificial".
641 bool isArtificial() const {
642 if (getVersion() <= llvm::LLVMDebugVersion8)
644 return (getUnsignedField(6) & FlagArtificial) != 0;
647 /// getInlinedAt - If this variable is inlined then return inline location.
648 MDNode *getInlinedAt() const;
650 /// Verify - Verify that a variable descriptor is well formed.
653 /// HasComplexAddr - Return true if the variable has a complex address.
654 bool hasComplexAddress() const {
655 return getNumAddrElements() > 0;
658 unsigned getNumAddrElements() const;
660 uint64_t getAddrElement(unsigned Idx) const {
661 if (getVersion() <= llvm::LLVMDebugVersion8)
662 return getUInt64Field(Idx+6);
663 if (getVersion() == llvm::LLVMDebugVersion9)
664 return getUInt64Field(Idx+7);
665 return getUInt64Field(Idx+8);
668 /// isBlockByrefVariable - Return true if the variable was declared as
669 /// a "__block" variable (Apple Blocks).
670 bool isBlockByrefVariable() const {
671 return getType().isBlockByrefStruct();
674 /// isInlinedFnArgument - Return trule if this variable provides debugging
675 /// information for an inlined function arguments.
676 bool isInlinedFnArgument(const Function *CurFn);
678 void printExtendedName(raw_ostream &OS) const;
681 /// DILexicalBlock - This is a wrapper for a lexical block.
682 class DILexicalBlock : public DIScope {
684 explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
685 DIScope getContext() const { return getFieldAs<DIScope>(1); }
686 unsigned getLineNumber() const { return getUnsignedField(2); }
687 unsigned getColumnNumber() const { return getUnsignedField(3); }
688 StringRef getDirectory() const {
689 StringRef dir = getFieldAs<DIFile>(4).getDirectory();
690 return !dir.empty() ? dir : getContext().getDirectory();
692 StringRef getFilename() const {
693 StringRef filename = getFieldAs<DIFile>(4).getFilename();
694 return !filename.empty() ? filename : getContext().getFilename();
698 /// DILexicalBlockFile - This is a wrapper for a lexical block with
699 /// a filename change.
700 class DILexicalBlockFile : public DIScope {
702 explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
703 DIScope getContext() const { return getScope().getContext(); }
704 unsigned getLineNumber() const { return getScope().getLineNumber(); }
705 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
706 StringRef getDirectory() const {
707 StringRef dir = getFieldAs<DIFile>(2).getDirectory();
708 return !dir.empty() ? dir : getContext().getDirectory();
710 StringRef getFilename() const {
711 StringRef filename = getFieldAs<DIFile>(2).getFilename();
712 assert(!filename.empty() && "Why'd you create this then?");
715 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
718 /// DINameSpace - A wrapper for a C++ style name space.
719 class DINameSpace : public DIScope {
721 explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
722 DIScope getContext() const { return getFieldAs<DIScope>(1); }
723 StringRef getName() const { return getStringField(2); }
724 StringRef getDirectory() const {
725 return getFieldAs<DIFile>(3).getDirectory();
727 StringRef getFilename() const {
728 return getFieldAs<DIFile>(3).getFilename();
730 DICompileUnit getCompileUnit() const{
731 assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
732 if (getVersion() == llvm::LLVMDebugVersion7)
733 return getFieldAs<DICompileUnit>(3);
735 return getFieldAs<DIFile>(3).getCompileUnit();
737 unsigned getLineNumber() const { return getUnsignedField(4); }
741 /// DILocation - This object holds location information. This object
742 /// is not associated with any DWARF tag.
743 class DILocation : public DIDescriptor {
745 explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
747 unsigned getLineNumber() const { return getUnsignedField(0); }
748 unsigned getColumnNumber() const { return getUnsignedField(1); }
749 DIScope getScope() const { return getFieldAs<DIScope>(2); }
750 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
751 StringRef getFilename() const { return getScope().getFilename(); }
752 StringRef getDirectory() const { return getScope().getDirectory(); }
756 class DIObjCProperty : public DIDescriptor {
757 friend class DIDescriptor;
758 void printInternal(raw_ostream &OS) const;
760 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) { }
762 StringRef getObjCPropertyName() const { return getStringField(1); }
763 DIFile getFile() const { return getFieldAs<DIFile>(2); }
764 unsigned getLineNumber() const { return getUnsignedField(3); }
766 StringRef getObjCPropertyGetterName() const {
767 return getStringField(4);
769 StringRef getObjCPropertySetterName() const {
770 return getStringField(5);
772 bool isReadOnlyObjCProperty() {
773 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
775 bool isReadWriteObjCProperty() {
776 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
778 bool isAssignObjCProperty() {
779 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
781 bool isRetainObjCProperty() {
782 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
784 bool isCopyObjCProperty() {
785 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
787 bool isNonAtomicObjCProperty() {
788 return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
791 DIType getType() const { return getFieldAs<DIType>(7); }
793 /// Verify - Verify that a derived type descriptor is well formed.
797 /// getDISubprogram - Find subprogram that is enclosing this scope.
798 DISubprogram getDISubprogram(const MDNode *Scope);
800 /// getDICompositeType - Find underlying composite type.
801 DICompositeType getDICompositeType(DIType T);
803 /// isSubprogramContext - Return true if Context is either a subprogram
804 /// or another context nested inside a subprogram.
805 bool isSubprogramContext(const MDNode *Context);
807 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
808 /// to hold function specific information.
809 NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
811 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
812 /// suitable to hold function specific information.
813 NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
815 /// createInlinedVariable - Create a new inlined variable based on current
817 /// @param DV Current Variable.
818 /// @param InlinedScope Location at current variable is inlined.
819 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
820 LLVMContext &VMContext);
822 /// cleanseInlinedVariable - Remove inlined scope from the variable.
823 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
825 class DebugInfoFinder {
827 /// processModule - Process entire module and collect debug info
829 void processModule(Module &M);
832 /// processType - Process DIType.
833 void processType(DIType DT);
835 /// processLexicalBlock - Process DILexicalBlock.
836 void processLexicalBlock(DILexicalBlock LB);
838 /// processSubprogram - Process DISubprogram.
839 void processSubprogram(DISubprogram SP);
841 /// processDeclare - Process DbgDeclareInst.
842 void processDeclare(DbgDeclareInst *DDI);
844 /// processLocation - Process DILocation.
845 void processLocation(DILocation Loc);
847 /// addCompileUnit - Add compile unit into CUs.
848 bool addCompileUnit(DICompileUnit CU);
850 /// addGlobalVariable - Add global variable into GVs.
851 bool addGlobalVariable(DIGlobalVariable DIG);
853 // addSubprogram - Add subprogram into SPs.
854 bool addSubprogram(DISubprogram SP);
856 /// addType - Add type into Tys.
857 bool addType(DIType DT);
860 typedef SmallVector<MDNode *, 8>::const_iterator iterator;
861 iterator compile_unit_begin() const { return CUs.begin(); }
862 iterator compile_unit_end() const { return CUs.end(); }
863 iterator subprogram_begin() const { return SPs.begin(); }
864 iterator subprogram_end() const { return SPs.end(); }
865 iterator global_variable_begin() const { return GVs.begin(); }
866 iterator global_variable_end() const { return GVs.end(); }
867 iterator type_begin() const { return TYs.begin(); }
868 iterator type_end() const { return TYs.end(); }
870 unsigned compile_unit_count() const { return CUs.size(); }
871 unsigned global_variable_count() const { return GVs.size(); }
872 unsigned subprogram_count() const { return SPs.size(); }
873 unsigned type_count() const { return TYs.size(); }
876 SmallVector<MDNode *, 8> CUs; // Compile Units
877 SmallVector<MDNode *, 8> SPs; // Subprograms
878 SmallVector<MDNode *, 8> GVs; // Global Variables;
879 SmallVector<MDNode *, 8> TYs; // Types
880 SmallPtrSet<MDNode *, 64> NodesSeen;
882 } // end namespace llvm