Add 'getInt64Field()' method to get the signed integer instead of unsigned.
[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   };
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     StringRef getObjCPropertyName() const {
358       if (getVersion() > LLVMDebugVersion11)
359         return StringRef();
360       return getStringField(10);
361     }
362     StringRef getObjCPropertyGetterName() const {
363       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
364       return getStringField(11);
365     }
366     StringRef getObjCPropertySetterName() const {
367       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
368       return getStringField(12);
369     }
370     bool isReadOnlyObjCProperty() {
371       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
372       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
373     }
374     bool isReadWriteObjCProperty() {
375       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
376       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
377     }
378     bool isAssignObjCProperty() {
379       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
380       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
381     }
382     bool isRetainObjCProperty() {
383       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
384       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
385     }
386     bool isCopyObjCProperty() {
387       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
388       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
389     }
390     bool isNonAtomicObjCProperty() {
391       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
392       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
393     }
394
395     /// Verify - Verify that a derived type descriptor is well formed.
396     bool Verify() const;
397   };
398
399   /// DICompositeType - This descriptor holds a type that can refer to multiple
400   /// other types, like a function or struct.
401   /// FIXME: Why is this a DIDerivedType??
402   class DICompositeType : public DIDerivedType {
403     friend class DIDescriptor;
404     void printInternal(raw_ostream &OS) const;
405   public:
406     explicit DICompositeType(const MDNode *N = 0)
407       : DIDerivedType(N, true, true) {
408       if (N && !isCompositeType())
409         DbgNode = 0;
410     }
411
412     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
413     unsigned getRunTimeLang() const { return getUnsignedField(11); }
414     DICompositeType getContainingType() const {
415       return getFieldAs<DICompositeType>(12);
416     }
417     DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
418
419     /// Verify - Verify that a composite type descriptor is well formed.
420     bool Verify() const;
421   };
422
423   /// DITemplateTypeParameter - This is a wrapper for template type parameter.
424   class DITemplateTypeParameter : public DIDescriptor {
425   public:
426     explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
427
428     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
429     StringRef getName() const        { return getStringField(2); }
430     DIType getType() const           { return getFieldAs<DIType>(3); }
431     StringRef getFilename() const    {
432       return getFieldAs<DIFile>(4).getFilename();
433     }
434     StringRef getDirectory() const   {
435       return getFieldAs<DIFile>(4).getDirectory();
436     }
437     unsigned getLineNumber() const   { return getUnsignedField(5); }
438     unsigned getColumnNumber() const { return getUnsignedField(6); }
439   };
440
441   /// DITemplateValueParameter - This is a wrapper for template value parameter.
442   class DITemplateValueParameter : public DIDescriptor {
443   public:
444     explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
445
446     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
447     StringRef getName() const        { return getStringField(2); }
448     DIType getType() const           { return getFieldAs<DIType>(3); }
449     uint64_t getValue() const         { return getUInt64Field(4); }
450     StringRef getFilename() const    {
451       return getFieldAs<DIFile>(5).getFilename();
452     }
453     StringRef getDirectory() const   {
454       return getFieldAs<DIFile>(5).getDirectory();
455     }
456     unsigned getLineNumber() const   { return getUnsignedField(6); }
457     unsigned getColumnNumber() const { return getUnsignedField(7); }
458   };
459
460   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
461   class DISubprogram : public DIScope {
462     friend class DIDescriptor;
463     void printInternal(raw_ostream &OS) const;
464   public:
465     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
466
467     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
468     StringRef getName() const         { return getStringField(3); }
469     StringRef getDisplayName() const  { return getStringField(4); }
470     StringRef getLinkageName() const  { return getStringField(5); }
471     DICompileUnit getCompileUnit() const{
472       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
473       if (getVersion() == llvm::LLVMDebugVersion7)
474         return getFieldAs<DICompileUnit>(6);
475
476       return getFieldAs<DIFile>(6).getCompileUnit();
477     }
478     unsigned getLineNumber() const      { return getUnsignedField(7); }
479     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
480
481     /// getReturnTypeName - Subprogram return types are encoded either as
482     /// DIType or as DICompositeType.
483     StringRef getReturnTypeName() const {
484       DICompositeType DCT(getFieldAs<DICompositeType>(8));
485       if (DCT.Verify()) {
486         DIArray A = DCT.getTypeArray();
487         DIType T(A.getElement(0));
488         return T.getName();
489       }
490       DIType T(getFieldAs<DIType>(8));
491       return T.getName();
492     }
493
494     /// isLocalToUnit - Return true if this subprogram is local to the current
495     /// compile unit, like 'static' in C.
496     unsigned isLocalToUnit() const     { return getUnsignedField(9); }
497     unsigned isDefinition() const      { return getUnsignedField(10); }
498
499     unsigned getVirtuality() const { return getUnsignedField(11); }
500     unsigned getVirtualIndex() const { return getUnsignedField(12); }
501
502     DICompositeType getContainingType() const {
503       return getFieldAs<DICompositeType>(13);
504     }
505
506     unsigned isArtificial() const    {
507       if (getVersion() <= llvm::LLVMDebugVersion8)
508         return getUnsignedField(14);
509       return (getUnsignedField(14) & FlagArtificial) != 0;
510     }
511     /// isPrivate - Return true if this subprogram has "private"
512     /// access specifier.
513     bool isPrivate() const    {
514       if (getVersion() <= llvm::LLVMDebugVersion8)
515         return false;
516       return (getUnsignedField(14) & FlagPrivate) != 0;
517     }
518     /// isProtected - Return true if this subprogram has "protected"
519     /// access specifier.
520     bool isProtected() const    {
521       if (getVersion() <= llvm::LLVMDebugVersion8)
522         return false;
523       return (getUnsignedField(14) & FlagProtected) != 0;
524     }
525     /// isExplicit - Return true if this subprogram is marked as explicit.
526     bool isExplicit() const    {
527       if (getVersion() <= llvm::LLVMDebugVersion8)
528         return false;
529       return (getUnsignedField(14) & FlagExplicit) != 0;
530     }
531     /// isPrototyped - Return true if this subprogram is prototyped.
532     bool isPrototyped() const    {
533       if (getVersion() <= llvm::LLVMDebugVersion8)
534         return false;
535       return (getUnsignedField(14) & FlagPrototyped) != 0;
536     }
537
538     unsigned isOptimized() const;
539
540     StringRef getFilename() const    {
541       if (getVersion() == llvm::LLVMDebugVersion7)
542         return getCompileUnit().getFilename();
543
544       return getFieldAs<DIFile>(6).getFilename();
545     }
546
547     StringRef getDirectory() const   {
548       if (getVersion() == llvm::LLVMDebugVersion7)
549         return getCompileUnit().getFilename();
550
551       return getFieldAs<DIFile>(6).getDirectory();
552     }
553
554     /// getScopeLineNumber - Get the beginning of the scope of the
555     /// function, not necessarily where the name of the program
556     /// starts.
557     unsigned getScopeLineNumber() const { return getUnsignedField(20); }
558
559     /// Verify - Verify that a subprogram descriptor is well formed.
560     bool Verify() const;
561
562     /// describes - Return true if this subprogram provides debugging
563     /// information for the function F.
564     bool describes(const Function *F);
565
566     Function *getFunction() const { return getFunctionField(16); }
567     void replaceFunction(Function *F) { replaceFunctionField(16, F); }
568     DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
569     DISubprogram getFunctionDeclaration() const {
570       return getFieldAs<DISubprogram>(18);
571     }
572     MDNode *getVariablesNodes() const;
573     DIArray getVariables() const;
574   };
575
576   /// DIGlobalVariable - This is a wrapper for a global variable.
577   class DIGlobalVariable : public DIDescriptor {
578     friend class DIDescriptor;
579     void printInternal(raw_ostream &OS) const;
580   public:
581     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
582
583     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
584     StringRef getName() const         { return getStringField(3); }
585     StringRef getDisplayName() const  { return getStringField(4); }
586     StringRef getLinkageName() const  { return getStringField(5); }
587     DICompileUnit getCompileUnit() const{
588       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
589       if (getVersion() == llvm::LLVMDebugVersion7)
590         return getFieldAs<DICompileUnit>(6);
591
592       DIFile F = getFieldAs<DIFile>(6);
593       return F.getCompileUnit();
594     }
595     StringRef getFilename() const {
596       if (getVersion() <= llvm::LLVMDebugVersion10)
597         return getContext().getFilename();
598       return getFieldAs<DIFile>(6).getFilename();
599     }
600     StringRef getDirectory() const {
601       if (getVersion() <= llvm::LLVMDebugVersion10)
602         return getContext().getDirectory();
603       return getFieldAs<DIFile>(6).getDirectory();
604
605     }
606
607     unsigned getLineNumber() const      { return getUnsignedField(7); }
608     DIType getType() const              { return getFieldAs<DIType>(8); }
609     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
610     unsigned isDefinition() const       { return getUnsignedField(10); }
611
612     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
613     Constant *getConstant() const   { return getConstantField(11); }
614
615     /// Verify - Verify that a global variable descriptor is well formed.
616     bool Verify() const;
617   };
618
619   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
620   /// global etc).
621   class DIVariable : public DIDescriptor {
622     friend class DIDescriptor;
623     void printInternal(raw_ostream &OS) const;
624   public:
625     explicit DIVariable(const MDNode *N = 0)
626       : DIDescriptor(N) {}
627
628     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
629     StringRef getName() const           { return getStringField(2);     }
630     DICompileUnit getCompileUnit() const {
631       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
632       if (getVersion() == llvm::LLVMDebugVersion7)
633         return getFieldAs<DICompileUnit>(3);
634
635       DIFile F = getFieldAs<DIFile>(3);
636       return F.getCompileUnit();
637     }
638     unsigned getLineNumber() const      {
639       return (getUnsignedField(4) << 8) >> 8;
640     }
641     unsigned getArgNumber() const       {
642       unsigned L = getUnsignedField(4);
643       return L >> 24;
644     }
645     DIType getType() const              { return getFieldAs<DIType>(5); }
646
647     /// isArtificial - Return true if this variable is marked as "artificial".
648     bool isArtificial() const    {
649       if (getVersion() <= llvm::LLVMDebugVersion8)
650         return false;
651       return (getUnsignedField(6) & FlagArtificial) != 0;
652     }
653
654     bool isObjectPointer() const {
655       return (getUnsignedField(6) & FlagObjectPointer) != 0;
656     }
657
658     /// getInlinedAt - If this variable is inlined then return inline location.
659     MDNode *getInlinedAt() const;
660
661     /// Verify - Verify that a variable descriptor is well formed.
662     bool Verify() const;
663
664     /// HasComplexAddr - Return true if the variable has a complex address.
665     bool hasComplexAddress() const {
666       return getNumAddrElements() > 0;
667     }
668
669     unsigned getNumAddrElements() const;
670
671     uint64_t getAddrElement(unsigned Idx) const {
672       if (getVersion() <= llvm::LLVMDebugVersion8)
673         return getUInt64Field(Idx+6);
674       if (getVersion() == llvm::LLVMDebugVersion9)
675         return getUInt64Field(Idx+7);
676       return getUInt64Field(Idx+8);
677     }
678
679     /// isBlockByrefVariable - Return true if the variable was declared as
680     /// a "__block" variable (Apple Blocks).
681     bool isBlockByrefVariable() const {
682       return getType().isBlockByrefStruct();
683     }
684
685     /// isInlinedFnArgument - Return trule if this variable provides debugging
686     /// information for an inlined function arguments.
687     bool isInlinedFnArgument(const Function *CurFn);
688
689     void printExtendedName(raw_ostream &OS) const;
690   };
691
692   /// DILexicalBlock - This is a wrapper for a lexical block.
693   class DILexicalBlock : public DIScope {
694   public:
695     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
696     DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
697     unsigned getLineNumber() const   { return getUnsignedField(2);         }
698     unsigned getColumnNumber() const { return getUnsignedField(3);         }
699     StringRef getDirectory() const {
700       StringRef dir = getFieldAs<DIFile>(4).getDirectory();
701       return !dir.empty() ? dir : getContext().getDirectory();
702     }
703     StringRef getFilename() const {
704       StringRef filename = getFieldAs<DIFile>(4).getFilename();
705       return !filename.empty() ? filename : getContext().getFilename();
706     }
707   };
708
709   /// DILexicalBlockFile - This is a wrapper for a lexical block with
710   /// a filename change.
711   class DILexicalBlockFile : public DIScope {
712   public:
713     explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
714     DIScope getContext() const { return getScope().getContext(); }
715     unsigned getLineNumber() const { return getScope().getLineNumber(); }
716     unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
717     StringRef getDirectory() const {
718       StringRef dir = getFieldAs<DIFile>(2).getDirectory();
719       return !dir.empty() ? dir : getContext().getDirectory();
720     }
721     StringRef getFilename() const {
722       StringRef filename = getFieldAs<DIFile>(2).getFilename();
723       assert(!filename.empty() && "Why'd you create this then?");
724       return filename;
725     }
726     DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
727   };
728
729   /// DINameSpace - A wrapper for a C++ style name space.
730   class DINameSpace : public DIScope {
731   public:
732     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
733     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
734     StringRef getName() const      { return getStringField(2);           }
735     StringRef getDirectory() const  {
736       return getFieldAs<DIFile>(3).getDirectory();
737     }
738     StringRef getFilename() const  {
739       return getFieldAs<DIFile>(3).getFilename();
740     }
741     DICompileUnit getCompileUnit() const{
742       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
743       if (getVersion() == llvm::LLVMDebugVersion7)
744         return getFieldAs<DICompileUnit>(3);
745
746       return getFieldAs<DIFile>(3).getCompileUnit();
747     }
748     unsigned getLineNumber() const { return getUnsignedField(4);         }
749     bool Verify() const;
750   };
751
752   /// DILocation - This object holds location information. This object
753   /// is not associated with any DWARF tag.
754   class DILocation : public DIDescriptor {
755   public:
756     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
757
758     unsigned getLineNumber() const     { return getUnsignedField(0); }
759     unsigned getColumnNumber() const   { return getUnsignedField(1); }
760     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
761     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
762     StringRef getFilename() const    { return getScope().getFilename(); }
763     StringRef getDirectory() const   { return getScope().getDirectory(); }
764     bool Verify() const;
765   };
766
767   class DIObjCProperty : public DIDescriptor {
768     friend class DIDescriptor;
769     void printInternal(raw_ostream &OS) const;
770   public:
771     explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) { }
772
773     StringRef getObjCPropertyName() const { return getStringField(1); }
774     DIFile getFile() const { return getFieldAs<DIFile>(2); }
775     unsigned getLineNumber() const { return getUnsignedField(3); }
776
777     StringRef getObjCPropertyGetterName() const {
778       return getStringField(4);
779     }
780     StringRef getObjCPropertySetterName() const {
781       return getStringField(5);
782     }
783     bool isReadOnlyObjCProperty() {
784       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
785     }
786     bool isReadWriteObjCProperty() {
787       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
788     }
789     bool isAssignObjCProperty() {
790       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
791     }
792     bool isRetainObjCProperty() {
793       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
794     }
795     bool isCopyObjCProperty() {
796       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
797     }
798     bool isNonAtomicObjCProperty() {
799       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
800     }
801
802     DIType getType() const { return getFieldAs<DIType>(7); }
803
804     /// Verify - Verify that a derived type descriptor is well formed.
805     bool Verify() const;
806   };
807
808   /// getDISubprogram - Find subprogram that is enclosing this scope.
809   DISubprogram getDISubprogram(const MDNode *Scope);
810
811   /// getDICompositeType - Find underlying composite type.
812   DICompositeType getDICompositeType(DIType T);
813
814   /// isSubprogramContext - Return true if Context is either a subprogram
815   /// or another context nested inside a subprogram.
816   bool isSubprogramContext(const MDNode *Context);
817
818   /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
819   /// to hold function specific information.
820   NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
821
822   /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
823   /// suitable to hold function specific information.
824   NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
825
826   /// createInlinedVariable - Create a new inlined variable based on current
827   /// variable.
828   /// @param DV            Current Variable.
829   /// @param InlinedScope  Location at current variable is inlined.
830   DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
831                                    LLVMContext &VMContext);
832
833   /// cleanseInlinedVariable - Remove inlined scope from the variable.
834   DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
835
836   class DebugInfoFinder {
837   public:
838     /// processModule - Process entire module and collect debug info
839     /// anchors.
840     void processModule(const Module &M);
841
842   private:
843     /// processType - Process DIType.
844     void processType(DIType DT);
845
846     /// processLexicalBlock - Process DILexicalBlock.
847     void processLexicalBlock(DILexicalBlock LB);
848
849     /// processSubprogram - Process DISubprogram.
850     void processSubprogram(DISubprogram SP);
851
852     /// processDeclare - Process DbgDeclareInst.
853     void processDeclare(const DbgDeclareInst *DDI);
854
855     /// processLocation - Process DILocation.
856     void processLocation(DILocation Loc);
857
858     /// addCompileUnit - Add compile unit into CUs.
859     bool addCompileUnit(DICompileUnit CU);
860
861     /// addGlobalVariable - Add global variable into GVs.
862     bool addGlobalVariable(DIGlobalVariable DIG);
863
864     // addSubprogram - Add subprogram into SPs.
865     bool addSubprogram(DISubprogram SP);
866
867     /// addType - Add type into Tys.
868     bool addType(DIType DT);
869
870   public:
871     typedef SmallVector<MDNode *, 8>::const_iterator iterator;
872     iterator compile_unit_begin()    const { return CUs.begin(); }
873     iterator compile_unit_end()      const { return CUs.end(); }
874     iterator subprogram_begin()      const { return SPs.begin(); }
875     iterator subprogram_end()        const { return SPs.end(); }
876     iterator global_variable_begin() const { return GVs.begin(); }
877     iterator global_variable_end()   const { return GVs.end(); }
878     iterator type_begin()            const { return TYs.begin(); }
879     iterator type_end()              const { return TYs.end(); }
880
881     unsigned compile_unit_count()    const { return CUs.size(); }
882     unsigned global_variable_count() const { return GVs.size(); }
883     unsigned subprogram_count()      const { return SPs.size(); }
884     unsigned type_count()            const { return TYs.size(); }
885
886   private:
887     SmallVector<MDNode *, 8> CUs;  // Compile Units
888     SmallVector<MDNode *, 8> SPs;  // Subprograms
889     SmallVector<MDNode *, 8> GVs;  // Global Variables;
890     SmallVector<MDNode *, 8> TYs;  // Types
891     SmallPtrSet<MDNode *, 64> NodesSeen;
892   };
893 } // end namespace llvm
894
895 #endif