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