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