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