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