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