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