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