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