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