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