Debug Info: In DIBuilder, the derived-from field of a DW_TAG_pointer_type
[oota-llvm.git] / include / llvm / DebugInfo.h
1 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
13 // DWARF information.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_DEBUGINFO_H
18 #define LLVM_DEBUGINFO_H
19
20 #include "llvm/Support/Casting.h"
21 #include "llvm/ADT/DenseMap.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/Dwarf.h"
27
28 namespace llvm {
29   class BasicBlock;
30   class Constant;
31   class Function;
32   class GlobalVariable;
33   class Module;
34   class Type;
35   class Value;
36   class DbgDeclareInst;
37   class DbgValueInst;
38   class Instruction;
39   class MDNode;
40   class MDString;
41   class NamedMDNode;
42   class LLVMContext;
43   class raw_ostream;
44
45   class DIFile;
46   class DISubprogram;
47   class DILexicalBlock;
48   class DILexicalBlockFile;
49   class DIVariable;
50   class DIType;
51   class DIScope;
52   class DIObjCProperty;
53
54   /// Maps from type identifier to the actual MDNode.
55   typedef DenseMap<const MDString *, MDNode*> DITypeIdentifierMap;
56
57   /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
58   /// This should not be stored in a container, because the underlying MDNode
59   /// may change in certain situations.
60   class DIDescriptor {
61     // Befriends DIRef so DIRef can befriend the protected member
62     // function: getFieldAs<DIRef>.
63     template <typename T>
64     friend class DIRef;
65   public:
66     enum {
67       FlagPrivate            = 1 << 0,
68       FlagProtected          = 1 << 1,
69       FlagFwdDecl            = 1 << 2,
70       FlagAppleBlock         = 1 << 3,
71       FlagBlockByrefStruct   = 1 << 4,
72       FlagVirtual            = 1 << 5,
73       FlagArtificial         = 1 << 6,
74       FlagExplicit           = 1 << 7,
75       FlagPrototyped         = 1 << 8,
76       FlagObjcClassComplete  = 1 << 9,
77       FlagObjectPointer      = 1 << 10,
78       FlagVector             = 1 << 11,
79       FlagStaticMember       = 1 << 12,
80       FlagIndirectVariable   = 1 << 13
81     };
82   protected:
83     const MDNode *DbgNode;
84
85     StringRef getStringField(unsigned Elt) const;
86     unsigned getUnsignedField(unsigned Elt) const {
87       return (unsigned)getUInt64Field(Elt);
88     }
89     uint64_t getUInt64Field(unsigned Elt) const;
90     int64_t getInt64Field(unsigned Elt) const;
91     DIDescriptor getDescriptorField(unsigned Elt) const;
92
93     template <typename DescTy>
94     DescTy getFieldAs(unsigned Elt) const {
95       return DescTy(getDescriptorField(Elt));
96     }
97
98     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
99     Constant *getConstantField(unsigned Elt) const;
100     Function *getFunctionField(unsigned Elt) const;
101     void replaceFunctionField(unsigned Elt, Function *F);
102
103   public:
104     explicit DIDescriptor(const MDNode *N = 0) : DbgNode(N) {}
105
106     bool Verify() const;
107
108     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
109     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
110
111     // An explicit operator bool so that we can do testing of DI values
112     // easily.
113     // FIXME: This operator bool isn't actually protecting anything at the
114     // moment due to the conversion operator above making DIDescriptor nodes
115     // implicitly convertable to bool.
116     LLVM_EXPLICIT operator bool() const { return DbgNode != 0; }
117
118     bool operator==(DIDescriptor Other) const {
119       return DbgNode == Other.DbgNode;
120     }
121     bool operator!=(DIDescriptor Other) const {
122       return !operator==(Other);
123     }
124
125     uint16_t getTag() const {
126       return getUnsignedField(0) & ~LLVMDebugVersionMask;
127     }
128
129     bool isDerivedType() const;
130     bool isCompositeType() const;
131     bool isBasicType() const;
132     bool isVariable() const;
133     bool isSubprogram() const;
134     bool isGlobalVariable() const;
135     bool isScope() const;
136     bool isFile() const;
137     bool isCompileUnit() const;
138     bool isNameSpace() const;
139     bool isLexicalBlockFile() const;
140     bool isLexicalBlock() const;
141     bool isSubrange() const;
142     bool isEnumerator() const;
143     bool isType() const;
144     bool isUnspecifiedParameter() const;
145     bool isTemplateTypeParameter() const;
146     bool isTemplateValueParameter() const;
147     bool isObjCProperty() const;
148     bool isImportedEntity() const;
149
150     /// print - print descriptor.
151     void print(raw_ostream &OS) const;
152
153     /// dump - print descriptor to dbgs() with a newline.
154     void dump() const;
155   };
156
157   /// DISubrange - This is used to represent ranges, for array bounds.
158   class DISubrange : public DIDescriptor {
159     friend class DIDescriptor;
160     void printInternal(raw_ostream &OS) const;
161   public:
162     explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
163
164     int64_t getLo() const { return getInt64Field(1); }
165     int64_t  getCount() const { return getInt64Field(2); }
166     bool Verify() const;
167   };
168
169   /// DIArray - This descriptor holds an array of descriptors.
170   class DIArray : public DIDescriptor {
171   public:
172     explicit DIArray(const MDNode *N = 0) : DIDescriptor(N) {}
173
174     unsigned getNumElements() const;
175     DIDescriptor getElement(unsigned Idx) const {
176       return getDescriptorField(Idx);
177     }
178   };
179
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;
186   public:
187     explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
188
189     StringRef getName() const        { return getStringField(1); }
190     int64_t getEnumValue() const      { return getInt64Field(2); }
191     bool Verify() const;
192   };
193
194   template <typename T>
195   class DIRef;
196   typedef DIRef<DIScope> DIScopeRef;
197   typedef DIRef<DIType> DITypeRef;
198
199   /// DIScope - A base class for various scopes.
200   class DIScope : public DIDescriptor {
201   protected:
202     friend class DIDescriptor;
203     void printInternal(raw_ostream &OS) const;
204   public:
205     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
206
207     /// Gets the parent scope for this scope node or returns a
208     /// default constructed scope.
209     DIScopeRef getContext() const;
210     /// If the scope node has a name, return that, else return an empty string.
211     StringRef getName() const;
212     StringRef getFilename() const;
213     StringRef getDirectory() const;
214
215     /// Generate a reference to this DIScope. Uses the type identifier instead
216     /// of the actual MDNode if possible, to help type uniquing.
217     DIScopeRef getRef() const;
218   };
219
220   /// Represents reference to a DIDescriptor, abstracts over direct and
221   /// identifier-based metadata references.
222   template <typename T>
223   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;
228
229     /// Val can be either a MDNode or a MDString, in the latter,
230     /// MDString specifies the type identifier.
231     const Value *Val;
232     explicit DIRef(const Value *V);
233   public:
234     T resolve(const DITypeIdentifierMap &Map) const {
235       if (!Val)
236         return T();
237
238       if (const MDNode *MD = dyn_cast<MDNode>(Val))
239         return T(MD);
240
241       const MDString *MS = cast<MDString>(Val);
242       // Find the corresponding MDNode.
243       DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
244       assert(Iter != Map.end() && "Identifier not in the type map?");
245       assert(DIDescriptor(Iter->second).isType() &&
246              "MDNode in DITypeIdentifierMap should be a DIType.");
247       return T(Iter->second);
248     }
249     StringRef getName() const {
250       if (!Val)
251         return StringRef();
252
253       if (const MDNode *MD = dyn_cast<MDNode>(Val))
254         return T(MD).getName();
255
256       const MDString *MS = cast<MDString>(Val);
257       return MS->getString();
258     }
259     operator Value *() const { return const_cast<Value*>(Val); }
260   };
261
262   /// Specialize getFieldAs to handle fields that are references to DIScopes.
263   template <>
264   DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
265   /// Specialize DIRef constructor for DIScopeRef.
266   template <>
267   DIRef<DIScope>::DIRef(const Value *V);
268
269   /// Specialize getFieldAs to handle fields that are references to DITypes.
270   template <>
271   DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
272   /// Specialize DIRef constructor for DITypeRef.
273   template <>
274   DIRef<DIType>::DIRef(const Value *V);
275
276   /// DIType - This is a wrapper for a type.
277   /// FIXME: Types should be factored much better so that CV qualifiers and
278   /// others do not require a huge and empty descriptor full of zeros.
279   class DIType : public DIScope {
280   protected:
281     friend class DIDescriptor;
282     void printInternal(raw_ostream &OS) const;
283
284   public:
285     DIType(const MDNode *N = 0) : DIScope(N) {}
286
287     /// Verify - Verify that a type descriptor is well formed.
288     bool Verify() const;
289
290     DIScopeRef getContext() const       { return getFieldAs<DIScopeRef>(2); }
291     StringRef getName() const           { return getStringField(3);     }
292     unsigned getLineNumber() const      { return getUnsignedField(4); }
293     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
294     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
295     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
296     // carry this is just plain insane.
297     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
298     unsigned getFlags() const           { return getUnsignedField(8); }
299     bool isPrivate() const {
300       return (getFlags() & FlagPrivate) != 0;
301     }
302     bool isProtected() const {
303       return (getFlags() & FlagProtected) != 0;
304     }
305     bool isForwardDecl() const {
306       return (getFlags() & FlagFwdDecl) != 0;
307     }
308     // isAppleBlock - Return true if this is the Apple Blocks extension.
309     bool isAppleBlockExtension() const {
310       return (getFlags() & FlagAppleBlock) != 0;
311     }
312     bool isBlockByrefStruct() const {
313       return (getFlags() & FlagBlockByrefStruct) != 0;
314     }
315     bool isVirtual() const {
316       return (getFlags() & FlagVirtual) != 0;
317     }
318     bool isArtificial() const {
319       return (getFlags() & FlagArtificial) != 0;
320     }
321     bool isObjectPointer() const {
322       return (getFlags() & FlagObjectPointer) != 0;
323     }
324     bool isObjcClassComplete() const {
325       return (getFlags() & FlagObjcClassComplete) != 0;
326     }
327     bool isVector() const {
328       return (getFlags() & FlagVector) != 0;
329     }
330     bool isStaticMember() const {
331       return (getFlags() & FlagStaticMember) != 0;
332     }
333     bool isValid() const {
334       return DbgNode && isType();
335     }
336
337     /// replaceAllUsesWith - Replace all uses of debug info referenced by
338     /// this descriptor.
339     void replaceAllUsesWith(DIDescriptor &D);
340     void replaceAllUsesWith(MDNode *D);
341   };
342
343   /// DIBasicType - A basic type, like 'int' or 'float'.
344   class DIBasicType : public DIType {
345   public:
346     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
347
348     unsigned getEncoding() const { return getUnsignedField(9); }
349
350     /// Verify - Verify that a basic type descriptor is well formed.
351     bool Verify() const;
352   };
353
354   /// DIDerivedType - A simple derived type, like a const qualified type,
355   /// a typedef, a pointer or reference, et cetera.  Or, a data member of
356   /// a class/struct/union.
357   class DIDerivedType : public DIType {
358     friend class DIDescriptor;
359     void printInternal(raw_ostream &OS) const;
360
361   public:
362     explicit DIDerivedType(const MDNode *N = 0) : DIType(N) {}
363
364     DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(9); }
365
366     /// getObjCProperty - Return property node, if this ivar is
367     /// associated with one.
368     MDNode *getObjCProperty() const;
369
370     DITypeRef getClassType() const {
371       assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
372       return getFieldAs<DITypeRef>(10);
373     }
374
375     Constant *getConstant() const {
376       assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
377       return getConstantField(10);
378     }
379
380     /// Verify - Verify that a derived type descriptor is well formed.
381     bool Verify() const;
382   };
383
384   /// DICompositeType - This descriptor holds a type that can refer to multiple
385   /// other types, like a function or struct.
386   /// DICompositeType is derived from DIDerivedType because some
387   /// composite types (such as enums) can be derived from basic types
388   // FIXME: Make this derive from DIType directly & just store the
389   // base type in a single DIType field.
390   class DICompositeType : public DIDerivedType {
391     friend class DIDescriptor;
392     void printInternal(raw_ostream &OS) const;
393   public:
394     explicit DICompositeType(const MDNode *N = 0) : DIDerivedType(N) {}
395
396     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
397     void setTypeArray(DIArray Elements, DIArray TParams = DIArray());
398     void addMember(DIDescriptor D);
399     unsigned getRunTimeLang() const { return getUnsignedField(11); }
400     DITypeRef getContainingType() const {
401       return getFieldAs<DITypeRef>(12);
402     }
403     void setContainingType(DICompositeType ContainingType);
404     DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
405     MDString *getIdentifier() const;
406
407     /// Verify - Verify that a composite type descriptor is well formed.
408     bool Verify() const;
409   };
410
411   /// DIFile - This is a wrapper for a file.
412   class DIFile : public DIScope {
413     friend class DIDescriptor;
414   public:
415     explicit DIFile(const MDNode *N = 0) : DIScope(N) {}
416     MDNode *getFileNode() const;
417     bool Verify() const;
418   };
419
420   /// DICompileUnit - A wrapper for a compile unit.
421   class DICompileUnit : public DIScope {
422     friend class DIDescriptor;
423     void printInternal(raw_ostream &OS) const;
424   public:
425     explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
426
427     unsigned getLanguage() const { return getUnsignedField(2); }
428     StringRef getProducer() const { return getStringField(3); }
429
430     bool isOptimized() const { return getUnsignedField(4) != 0; }
431     StringRef getFlags() const { return getStringField(5); }
432     unsigned getRunTimeVersion() const { return getUnsignedField(6); }
433
434     DIArray getEnumTypes() const;
435     DIArray getRetainedTypes() const;
436     DIArray getSubprograms() const;
437     DIArray getGlobalVariables() const;
438     DIArray getImportedEntities() const;
439
440     StringRef getSplitDebugFilename() const { return getStringField(12); }
441
442     /// Verify - Verify that a compile unit is well formed.
443     bool Verify() const;
444   };
445
446   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
447   class DISubprogram : public DIScope {
448     friend class DIDescriptor;
449     void printInternal(raw_ostream &OS) const;
450   public:
451     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
452
453     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
454     StringRef getName() const         { return getStringField(3); }
455     StringRef getDisplayName() const  { return getStringField(4); }
456     StringRef getLinkageName() const  { return getStringField(5); }
457     unsigned getLineNumber() const      { return getUnsignedField(6); }
458     DICompositeType getType() const { return getFieldAs<DICompositeType>(7); }
459
460     /// isLocalToUnit - Return true if this subprogram is local to the current
461     /// compile unit, like 'static' in C.
462     unsigned isLocalToUnit() const     { return getUnsignedField(8); }
463     unsigned isDefinition() const      { return getUnsignedField(9); }
464
465     unsigned getVirtuality() const { return getUnsignedField(10); }
466     unsigned getVirtualIndex() const { return getUnsignedField(11); }
467
468     DITypeRef getContainingType() const {
469       return getFieldAs<DITypeRef>(12);
470     }
471
472     unsigned getFlags() const {
473       return getUnsignedField(13);
474     }
475
476     unsigned isArtificial() const    {
477       return (getUnsignedField(13) & FlagArtificial) != 0;
478     }
479     /// isPrivate - Return true if this subprogram has "private"
480     /// access specifier.
481     bool isPrivate() const    {
482       return (getUnsignedField(13) & FlagPrivate) != 0;
483     }
484     /// isProtected - Return true if this subprogram has "protected"
485     /// access specifier.
486     bool isProtected() const    {
487       return (getUnsignedField(13) & FlagProtected) != 0;
488     }
489     /// isExplicit - Return true if this subprogram is marked as explicit.
490     bool isExplicit() const    {
491       return (getUnsignedField(13) & FlagExplicit) != 0;
492     }
493     /// isPrototyped - Return true if this subprogram is prototyped.
494     bool isPrototyped() const    {
495       return (getUnsignedField(13) & FlagPrototyped) != 0;
496     }
497
498     unsigned isOptimized() const;
499
500     /// Verify - Verify that a subprogram descriptor is well formed.
501     bool Verify() const;
502
503     /// describes - Return true if this subprogram provides debugging
504     /// information for the function F.
505     bool describes(const Function *F);
506
507     Function *getFunction() const { return getFunctionField(15); }
508     void replaceFunction(Function *F) { replaceFunctionField(15, F); }
509     DIArray getTemplateParams() const { return getFieldAs<DIArray>(16); }
510     DISubprogram getFunctionDeclaration() const {
511       return getFieldAs<DISubprogram>(17);
512     }
513     MDNode *getVariablesNodes() const;
514     DIArray getVariables() const;
515
516     /// getScopeLineNumber - Get the beginning of the scope of the
517     /// function, not necessarily where the name of the program
518     /// starts.
519     unsigned getScopeLineNumber() const { return getUnsignedField(19); }
520   };
521
522   /// DILexicalBlock - This is a wrapper for a lexical block.
523   class DILexicalBlock : public DIScope {
524   public:
525     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
526     DIScope getContext() const       { return getFieldAs<DIScope>(2);      }
527     unsigned getLineNumber() const   { return getUnsignedField(3);         }
528     unsigned getColumnNumber() const { return getUnsignedField(4);         }
529     bool Verify() const;
530   };
531
532   /// DILexicalBlockFile - This is a wrapper for a lexical block with
533   /// a filename change.
534   class DILexicalBlockFile : public DIScope {
535   public:
536     explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
537     DIScope getContext() const {
538       if (getScope().isSubprogram())
539         return getScope();
540       return getScope().getContext();
541     }
542     unsigned getLineNumber() const { return getScope().getLineNumber(); }
543     unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
544     DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
545     bool Verify() const;
546   };
547
548   /// DINameSpace - A wrapper for a C++ style name space.
549   class DINameSpace : public DIScope {
550     friend class DIDescriptor;
551     void printInternal(raw_ostream &OS) const;
552   public:
553     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
554     DIScope getContext() const     { return getFieldAs<DIScope>(2);      }
555     StringRef getName() const      { return getStringField(3);           }
556     unsigned getLineNumber() const { return getUnsignedField(4);         }
557     bool Verify() const;
558   };
559
560   /// DITemplateTypeParameter - This is a wrapper for template type parameter.
561   class DITemplateTypeParameter : public DIDescriptor {
562   public:
563     explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
564
565     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
566     StringRef getName() const        { return getStringField(2); }
567     DIType getType() const           { return getFieldAs<DIType>(3); }
568     StringRef getFilename() const    {
569       return getFieldAs<DIFile>(4).getFilename();
570     }
571     StringRef getDirectory() const   {
572       return getFieldAs<DIFile>(4).getDirectory();
573     }
574     unsigned getLineNumber() const   { return getUnsignedField(5); }
575     unsigned getColumnNumber() const { return getUnsignedField(6); }
576     bool Verify() const;
577   };
578
579   /// DITemplateValueParameter - This is a wrapper for template value parameter.
580   class DITemplateValueParameter : public DIDescriptor {
581   public:
582     explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
583
584     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
585     StringRef getName() const        { return getStringField(2); }
586     DIType getType() const           { return getFieldAs<DIType>(3); }
587     Value *getValue() const;
588     StringRef getFilename() const    {
589       return getFieldAs<DIFile>(5).getFilename();
590     }
591     StringRef getDirectory() const   {
592       return getFieldAs<DIFile>(5).getDirectory();
593     }
594     unsigned getLineNumber() const   { return getUnsignedField(6); }
595     unsigned getColumnNumber() const { return getUnsignedField(7); }
596     bool Verify() const;
597   };
598
599   /// DIGlobalVariable - This is a wrapper for a global variable.
600   class DIGlobalVariable : public DIDescriptor {
601     friend class DIDescriptor;
602     void printInternal(raw_ostream &OS) const;
603   public:
604     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
605
606     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
607     StringRef getName() const         { return getStringField(3); }
608     StringRef getDisplayName() const  { return getStringField(4); }
609     StringRef getLinkageName() const  { return getStringField(5); }
610     StringRef getFilename() const {
611       return getFieldAs<DIFile>(6).getFilename();
612     }
613     StringRef getDirectory() const {
614       return getFieldAs<DIFile>(6).getDirectory();
615
616     }
617
618     unsigned getLineNumber() const      { return getUnsignedField(7); }
619     DIType getType() const              { return getFieldAs<DIType>(8); }
620     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
621     unsigned isDefinition() const       { return getUnsignedField(10); }
622
623     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
624     Constant *getConstant() const   { return getConstantField(11); }
625     DIDerivedType getStaticDataMemberDeclaration() const {
626       return getFieldAs<DIDerivedType>(12);
627     }
628
629     /// Verify - Verify that a global variable descriptor is well formed.
630     bool Verify() const;
631   };
632
633   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
634   /// global etc).
635   class DIVariable : public DIDescriptor {
636     friend class DIDescriptor;
637     void printInternal(raw_ostream &OS) const;
638   public:
639     explicit DIVariable(const MDNode *N = 0) : DIDescriptor(N) {}
640
641     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
642     StringRef getName() const           { return getStringField(2);     }
643     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
644     unsigned getLineNumber() const      {
645       return (getUnsignedField(4) << 8) >> 8;
646     }
647     unsigned getArgNumber() const       {
648       unsigned L = getUnsignedField(4);
649       return L >> 24;
650     }
651     DIType getType() const              { return getFieldAs<DIType>(5); }
652
653     /// isArtificial - Return true if this variable is marked as "artificial".
654     bool isArtificial() const    {
655       return (getUnsignedField(6) & FlagArtificial) != 0;
656     }
657
658     bool isObjectPointer() const {
659       return (getUnsignedField(6) & FlagObjectPointer) != 0;
660     }
661
662     /// \brief Return true if this variable is represented as a pointer.
663     bool isIndirect() const {
664       return (getUnsignedField(6) & FlagIndirectVariable) != 0;
665     }
666
667     /// getInlinedAt - If this variable is inlined then return inline location.
668     MDNode *getInlinedAt() const;
669
670     /// Verify - Verify that a variable descriptor is well formed.
671     bool Verify() const;
672
673     /// HasComplexAddr - Return true if the variable has a complex address.
674     bool hasComplexAddress() const {
675       return getNumAddrElements() > 0;
676     }
677
678     unsigned getNumAddrElements() const;
679
680     uint64_t getAddrElement(unsigned Idx) const {
681       return getUInt64Field(Idx+8);
682     }
683
684     /// isBlockByrefVariable - Return true if the variable was declared as
685     /// a "__block" variable (Apple Blocks).
686     bool isBlockByrefVariable() const {
687       return getType().isBlockByrefStruct();
688     }
689
690     /// isInlinedFnArgument - Return true if this variable provides debugging
691     /// information for an inlined function arguments.
692     bool isInlinedFnArgument(const Function *CurFn);
693
694     void printExtendedName(raw_ostream &OS) const;
695   };
696
697   /// DILocation - This object holds location information. This object
698   /// is not associated with any DWARF tag.
699   class DILocation : public DIDescriptor {
700   public:
701     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
702
703     unsigned getLineNumber() const     { return getUnsignedField(0); }
704     unsigned getColumnNumber() const   { return getUnsignedField(1); }
705     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
706     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
707     StringRef getFilename() const    { return getScope().getFilename(); }
708     StringRef getDirectory() const   { return getScope().getDirectory(); }
709     bool Verify() const;
710   };
711
712   class DIObjCProperty : public DIDescriptor {
713     friend class DIDescriptor;
714     void printInternal(raw_ostream &OS) const;
715   public:
716     explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) { }
717
718     StringRef getObjCPropertyName() const { return getStringField(1); }
719     DIFile getFile() const { return getFieldAs<DIFile>(2); }
720     unsigned getLineNumber() const { return getUnsignedField(3); }
721
722     StringRef getObjCPropertyGetterName() const {
723       return getStringField(4);
724     }
725     StringRef getObjCPropertySetterName() const {
726       return getStringField(5);
727     }
728     bool isReadOnlyObjCProperty() const {
729       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
730     }
731     bool isReadWriteObjCProperty() const {
732       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
733     }
734     bool isAssignObjCProperty() const {
735       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
736     }
737     bool isRetainObjCProperty() const {
738       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
739     }
740     bool isCopyObjCProperty() const {
741       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
742     }
743     bool isNonAtomicObjCProperty() const {
744       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
745     }
746
747     DIType getType() const { return getFieldAs<DIType>(7); }
748
749     /// Verify - Verify that a derived type descriptor is well formed.
750     bool Verify() const;
751   };
752
753   /// \brief An imported module (C++ using directive or similar).
754   class DIImportedEntity : public DIDescriptor {
755     friend class DIDescriptor;
756     void printInternal(raw_ostream &OS) const;
757   public:
758     explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) { }
759     DIScope getContext() const { return getFieldAs<DIScope>(1); }
760     DIDescriptor getEntity() const { return getFieldAs<DIDescriptor>(2); }
761     unsigned getLineNumber() const { return getUnsignedField(3); }
762     StringRef getName() const { return getStringField(4); }
763     bool Verify() const;
764   };
765
766   /// getDISubprogram - Find subprogram that is enclosing this scope.
767   DISubprogram getDISubprogram(const MDNode *Scope);
768
769   /// getDICompositeType - Find underlying composite type.
770   DICompositeType getDICompositeType(DIType T);
771
772   /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
773   /// to hold function specific information.
774   NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
775
776   /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
777   /// suitable to hold function specific information.
778   NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
779
780   /// createInlinedVariable - Create a new inlined variable based on current
781   /// variable.
782   /// @param DV            Current Variable.
783   /// @param InlinedScope  Location at current variable is inlined.
784   DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
785                                    LLVMContext &VMContext);
786
787   /// cleanseInlinedVariable - Remove inlined scope from the variable.
788   DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
789
790   /// Construct DITypeIdentifierMap by going through retained types of each CU.
791   DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
792
793   /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
794   /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
795   /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
796   /// DbgValueInst and DbgLoc attached to instructions. processModule will go
797   /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
798   /// used by the CUs.
799   class DebugInfoFinder {
800   public:
801     /// processModule - Process entire module and collect debug info
802     /// anchors.
803     void processModule(const Module &M);
804
805     /// processDeclare - Process DbgDeclareInst.
806     void processDeclare(const DbgDeclareInst *DDI);
807     /// Process DbgValueInst.
808     void processValue(const DbgValueInst *DVI);
809     /// processLocation - Process DILocation.
810     void processLocation(DILocation Loc);
811
812     /// Clear all lists.
813     void reset();
814   private:
815     /// processType - Process DIType.
816     void processType(DIType DT);
817
818     /// processLexicalBlock - Process DILexicalBlock.
819     void processLexicalBlock(DILexicalBlock LB);
820
821     /// processSubprogram - Process DISubprogram.
822     void processSubprogram(DISubprogram SP);
823
824     void processScope(DIScope Scope);
825
826     /// addCompileUnit - Add compile unit into CUs.
827     bool addCompileUnit(DICompileUnit CU);
828
829     /// addGlobalVariable - Add global variable into GVs.
830     bool addGlobalVariable(DIGlobalVariable DIG);
831
832     // addSubprogram - Add subprogram into SPs.
833     bool addSubprogram(DISubprogram SP);
834
835     /// addType - Add type into Tys.
836     bool addType(DIType DT);
837
838     bool addScope(DIScope Scope);
839
840   public:
841     typedef SmallVectorImpl<MDNode *>::const_iterator iterator;
842     iterator compile_unit_begin()    const { return CUs.begin(); }
843     iterator compile_unit_end()      const { return CUs.end(); }
844     iterator subprogram_begin()      const { return SPs.begin(); }
845     iterator subprogram_end()        const { return SPs.end(); }
846     iterator global_variable_begin() const { return GVs.begin(); }
847     iterator global_variable_end()   const { return GVs.end(); }
848     iterator type_begin()            const { return TYs.begin(); }
849     iterator type_end()              const { return TYs.end(); }
850     iterator scope_begin()           const { return Scopes.begin(); }
851     iterator scope_end()             const { return Scopes.end(); }
852
853     unsigned compile_unit_count()    const { return CUs.size(); }
854     unsigned global_variable_count() const { return GVs.size(); }
855     unsigned subprogram_count()      const { return SPs.size(); }
856     unsigned type_count()            const { return TYs.size(); }
857     unsigned scope_count()           const { return Scopes.size(); }
858
859   private:
860     SmallVector<MDNode *, 8> CUs;  // Compile Units
861     SmallVector<MDNode *, 8> SPs;  // Subprograms
862     SmallVector<MDNode *, 8> GVs;  // Global Variables;
863     SmallVector<MDNode *, 8> TYs;  // Types
864     SmallVector<MDNode *, 8> Scopes; // Scopes
865     SmallPtrSet<MDNode *, 64> NodesSeen;
866     DITypeIdentifierMap TypeIdentifierMap;
867   };
868 } // end namespace llvm
869
870 #endif