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