Revamp how debugging information is emitted for debug info objects.
[oota-llvm.git] / include / llvm / Analysis / 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     };
65   protected:
66     const MDNode *DbgNode;
67
68     StringRef getStringField(unsigned Elt) const;
69     unsigned getUnsignedField(unsigned Elt) const {
70       return (unsigned)getUInt64Field(Elt);
71     }
72     uint64_t getUInt64Field(unsigned Elt) const;
73     DIDescriptor getDescriptorField(unsigned Elt) const;
74
75     template <typename DescTy>
76     DescTy getFieldAs(unsigned Elt) const {
77       return DescTy(getDescriptorField(Elt));
78     }
79
80     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
81     Constant *getConstantField(unsigned Elt) const;
82     Function *getFunctionField(unsigned Elt) const;
83
84   public:
85     explicit DIDescriptor() : DbgNode(0) {}
86     explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
87     explicit DIDescriptor(const DIFile F);
88     explicit DIDescriptor(const DISubprogram F);
89     explicit DIDescriptor(const DILexicalBlockFile F);
90     explicit DIDescriptor(const DILexicalBlock F);
91     explicit DIDescriptor(const DIVariable F);
92     explicit DIDescriptor(const DIType F);
93
94     bool Verify() const { return DbgNode != 0; }
95
96     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
97     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
98
99     unsigned getVersion() const {
100       return getUnsignedField(0) & LLVMDebugVersionMask;
101     }
102
103     unsigned getTag() const {
104       return getUnsignedField(0) & ~LLVMDebugVersionMask;
105     }
106
107     bool isDerivedType() const;
108     bool isCompositeType() const;
109     bool isBasicType() const;
110     bool isVariable() const;
111     bool isSubprogram() const;
112     bool isGlobalVariable() const;
113     bool isScope() const;
114     bool isFile() const;
115     bool isCompileUnit() const;
116     bool isNameSpace() const;
117     bool isLexicalBlockFile() const;
118     bool isLexicalBlock() const;
119     bool isSubrange() const;
120     bool isEnumerator() const;
121     bool isType() const;
122     bool isGlobal() const;
123     bool isUnspecifiedParameter() const;
124     bool isTemplateTypeParameter() const;
125     bool isTemplateValueParameter() const;
126     bool isObjCProperty() const;
127
128     /// print - print descriptor.
129     void print(raw_ostream &OS) const;
130
131     /// dump - print descriptor to dbgs() with a newline.
132     void dump() const;
133   };
134
135   /// DISubrange - This is used to represent ranges, for array bounds.
136   class DISubrange : public DIDescriptor {
137     friend class DIDescriptor;
138     void printInternal(raw_ostream &OS) const;
139   public:
140     explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
141
142     uint64_t getLo() const { return getUInt64Field(1); }
143     uint64_t getHi() const { return getUInt64Field(2); }
144   };
145
146   /// DIArray - This descriptor holds an array of descriptors.
147   class DIArray : public DIDescriptor {
148   public:
149     explicit DIArray(const MDNode *N = 0)
150       : DIDescriptor(N) {}
151
152     unsigned getNumElements() const;
153     DIDescriptor getElement(unsigned Idx) const {
154       return getDescriptorField(Idx);
155     }
156   };
157
158   /// DIScope - A base class for various scopes.
159   class DIScope : public DIDescriptor {
160     virtual void anchor();
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     virtual ~DIScope() {}
167
168     StringRef getFilename() const;
169     StringRef getDirectory() const;
170   };
171
172   /// DICompileUnit - A wrapper for a compile unit.
173   class DICompileUnit : public DIScope {
174     virtual void anchor();
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     virtual void anchor();
211     friend class DIDescriptor;
212     void printInternal(raw_ostream &OS) const {} // FIXME: Output something?
213   public:
214     explicit DIFile(const MDNode *N = 0) : DIScope(N) {
215       if (DbgNode && !isFile())
216         DbgNode = 0;
217     }
218     StringRef getFilename() const  { return getStringField(1);   }
219     StringRef getDirectory() const { return getStringField(2);   }
220     DICompileUnit getCompileUnit() const{ 
221       assert (getVersion() <= LLVMDebugVersion10  && "Invalid CompileUnit!");
222       return getFieldAs<DICompileUnit>(3); 
223     }
224   };
225
226   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
227   /// FIXME: it seems strange that this doesn't have either a reference to the
228   /// type/precision or a file/line pair for location info.
229   class DIEnumerator : public DIDescriptor {
230     friend class DIDescriptor;
231     void printInternal(raw_ostream &OS) const;
232   public:
233     explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
234
235     StringRef getName() const        { return getStringField(1); }
236     uint64_t getEnumValue() const      { return getUInt64Field(2); }
237   };
238
239   /// DIType - This is a wrapper for a type.
240   /// FIXME: Types should be factored much better so that CV qualifiers and
241   /// others do not require a huge and empty descriptor full of zeros.
242   class DIType : public DIScope {
243     virtual void anchor();
244   protected:
245     friend class DIDescriptor;
246     void printInternal(raw_ostream &OS) const;
247     // This ctor is used when the Tag has already been validated by a derived
248     // ctor.
249     DIType(const MDNode *N, bool, bool) : DIScope(N) {}
250   public:
251     /// Verify - Verify that a type descriptor is well formed.
252     bool Verify() const;
253     explicit DIType(const MDNode *N);
254     explicit DIType() {}
255     virtual ~DIType() {}
256
257     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
258     StringRef getName() const           { return getStringField(2);     }
259     DICompileUnit getCompileUnit() const{ 
260       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
261      if (getVersion() == llvm::LLVMDebugVersion7)
262        return getFieldAs<DICompileUnit>(3);
263      
264      return getFieldAs<DIFile>(3).getCompileUnit();
265     }
266     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
267     unsigned getLineNumber() const      { return getUnsignedField(4); }
268     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
269     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
270     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
271     // carry this is just plain insane.
272     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
273     unsigned getFlags() const           { return getUnsignedField(8); }
274     bool isPrivate() const {
275       return (getFlags() & FlagPrivate) != 0;
276     }
277     bool isProtected() const {
278       return (getFlags() & FlagProtected) != 0;
279     }
280     bool isForwardDecl() const {
281       return (getFlags() & FlagFwdDecl) != 0;
282     }
283     // isAppleBlock - Return true if this is the Apple Blocks extension.
284     bool isAppleBlockExtension() const {
285       return (getFlags() & FlagAppleBlock) != 0;
286     }
287     bool isBlockByrefStruct() const {
288       return (getFlags() & FlagBlockByrefStruct) != 0;
289     }
290     bool isVirtual() const {
291       return (getFlags() & FlagVirtual) != 0;
292     }
293     bool isArtificial() const {
294       return (getFlags() & FlagArtificial) != 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     virtual void anchor();
327   public:
328     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
329
330     unsigned getEncoding() const { return getUnsignedField(9); }
331
332     /// Verify - Verify that a basic type descriptor is well formed.
333     bool Verify() const;
334   };
335
336   /// DIDerivedType - A simple derived type, like a const qualified type,
337   /// a typedef, a pointer or reference, etc.
338   class DIDerivedType : public DIType {
339     virtual void anchor();
340     friend class DIDescriptor;
341     void printInternal(raw_ostream &OS) const;
342   protected:
343     explicit DIDerivedType(const MDNode *N, bool, bool)
344       : DIType(N, true, true) {}
345   public:
346     explicit DIDerivedType(const MDNode *N = 0)
347       : DIType(N, true, true) {}
348
349     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
350
351     /// getOriginalTypeSize - If this type is derived from a base type then
352     /// return base type size.
353     uint64_t getOriginalTypeSize() const;
354
355     /// getObjCProperty - Return property node, if this ivar is 
356     /// associated with one.
357     MDNode *getObjCProperty() const;
358
359     StringRef getObjCPropertyName() const { 
360       if (getVersion() > LLVMDebugVersion11)
361         return StringRef();
362       return getStringField(10); 
363     }
364     StringRef getObjCPropertyGetterName() const {
365       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
366       return getStringField(11);
367     }
368     StringRef getObjCPropertySetterName() const {
369       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
370       return getStringField(12);
371     }
372     bool isReadOnlyObjCProperty() {
373       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
374       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
375     }
376     bool isReadWriteObjCProperty() {
377       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
378       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
379     }
380     bool isAssignObjCProperty() {
381       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
382       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
383     }
384     bool isRetainObjCProperty() {
385       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
386       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
387     }
388     bool isCopyObjCProperty() {
389       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
390       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
391     }
392     bool isNonAtomicObjCProperty() {
393       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
394       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
395     }
396
397     /// Verify - Verify that a derived type descriptor is well formed.
398     bool Verify() const;
399   };
400
401   /// DICompositeType - This descriptor holds a type that can refer to multiple
402   /// other types, like a function or struct.
403   /// FIXME: Why is this a DIDerivedType??
404   class DICompositeType : public DIDerivedType {
405     virtual void anchor();
406     friend class DIDescriptor;
407     void printInternal(raw_ostream &OS) const;
408   public:
409     explicit DICompositeType(const MDNode *N = 0)
410       : DIDerivedType(N, true, true) {
411       if (N && !isCompositeType())
412         DbgNode = 0;
413     }
414
415     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
416     unsigned getRunTimeLang() const { return getUnsignedField(11); }
417     DICompositeType getContainingType() const {
418       return getFieldAs<DICompositeType>(12);
419     }
420     DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
421
422     /// Verify - Verify that a composite type descriptor is well formed.
423     bool Verify() const;
424   };
425
426   /// DITemplateTypeParameter - This is a wrapper for template type parameter.
427   class DITemplateTypeParameter : public DIDescriptor {
428   public:
429     explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
430
431     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
432     StringRef getName() const        { return getStringField(2); }
433     DIType getType() const           { return getFieldAs<DIType>(3); }
434     StringRef getFilename() const    { 
435       return getFieldAs<DIFile>(4).getFilename();
436     }
437     StringRef getDirectory() const   { 
438       return getFieldAs<DIFile>(4).getDirectory();
439     }
440     unsigned getLineNumber() const   { return getUnsignedField(5); }
441     unsigned getColumnNumber() const { return getUnsignedField(6); }
442   };
443
444   /// DITemplateValueParameter - This is a wrapper for template value parameter.
445   class DITemplateValueParameter : public DIDescriptor {
446   public:
447     explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
448
449     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
450     StringRef getName() const        { return getStringField(2); }
451     DIType getType() const           { return getFieldAs<DIType>(3); }
452     uint64_t getValue() const         { return getUInt64Field(4); }
453     StringRef getFilename() const    { 
454       return getFieldAs<DIFile>(5).getFilename();
455     }
456     StringRef getDirectory() const   { 
457       return getFieldAs<DIFile>(5).getDirectory();
458     }
459     unsigned getLineNumber() const   { return getUnsignedField(6); }
460     unsigned getColumnNumber() const { return getUnsignedField(7); }
461   };
462
463   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
464   class DISubprogram : public DIScope {
465     virtual void anchor();
466     friend class DIDescriptor;
467     void printInternal(raw_ostream &OS) const;
468   public:
469     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
470
471     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
472     StringRef getName() const         { return getStringField(3); }
473     StringRef getDisplayName() const  { return getStringField(4); }
474     StringRef getLinkageName() const  { return getStringField(5); }
475     DICompileUnit getCompileUnit() const{ 
476       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
477       if (getVersion() == llvm::LLVMDebugVersion7)
478         return getFieldAs<DICompileUnit>(6);
479
480       return getFieldAs<DIFile>(6).getCompileUnit(); 
481     }
482     unsigned getLineNumber() const      { return getUnsignedField(7); }
483     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
484
485     /// getReturnTypeName - Subprogram return types are encoded either as
486     /// DIType or as DICompositeType.
487     StringRef getReturnTypeName() const {
488       DICompositeType DCT(getFieldAs<DICompositeType>(8));
489       if (DCT.Verify()) {
490         DIArray A = DCT.getTypeArray();
491         DIType T(A.getElement(0));
492         return T.getName();
493       }
494       DIType T(getFieldAs<DIType>(8));
495       return T.getName();
496     }
497
498     /// isLocalToUnit - Return true if this subprogram is local to the current
499     /// compile unit, like 'static' in C.
500     unsigned isLocalToUnit() const     { return getUnsignedField(9); }
501     unsigned isDefinition() const      { return getUnsignedField(10); }
502
503     unsigned getVirtuality() const { return getUnsignedField(11); }
504     unsigned getVirtualIndex() const { return getUnsignedField(12); }
505
506     DICompositeType getContainingType() const {
507       return getFieldAs<DICompositeType>(13);
508     }
509
510     unsigned isArtificial() const    { 
511       if (getVersion() <= llvm::LLVMDebugVersion8)
512         return getUnsignedField(14); 
513       return (getUnsignedField(14) & FlagArtificial) != 0;
514     }
515     /// isPrivate - Return true if this subprogram has "private"
516     /// access specifier.
517     bool isPrivate() const    { 
518       if (getVersion() <= llvm::LLVMDebugVersion8)
519         return false;
520       return (getUnsignedField(14) & FlagPrivate) != 0;
521     }
522     /// isProtected - Return true if this subprogram has "protected"
523     /// access specifier.
524     bool isProtected() const    { 
525       if (getVersion() <= llvm::LLVMDebugVersion8)
526         return false;
527       return (getUnsignedField(14) & FlagProtected) != 0;
528     }
529     /// isExplicit - Return true if this subprogram is marked as explicit.
530     bool isExplicit() const    { 
531       if (getVersion() <= llvm::LLVMDebugVersion8)
532         return false;
533       return (getUnsignedField(14) & FlagExplicit) != 0;
534     }
535     /// isPrototyped - Return true if this subprogram is prototyped.
536     bool isPrototyped() const    { 
537       if (getVersion() <= llvm::LLVMDebugVersion8)
538         return false;
539       return (getUnsignedField(14) & FlagPrototyped) != 0;
540     }
541
542     unsigned isOptimized() const;
543
544     StringRef getFilename() const    { 
545       if (getVersion() == llvm::LLVMDebugVersion7)
546         return getCompileUnit().getFilename();
547
548       return getFieldAs<DIFile>(6).getFilename(); 
549     }
550
551     StringRef getDirectory() const   { 
552       if (getVersion() == llvm::LLVMDebugVersion7)
553         return getCompileUnit().getFilename();
554
555       return getFieldAs<DIFile>(6).getDirectory(); 
556     }
557
558     /// getScopeLineNumber - Get the beginning of the scope of the
559     /// function, not necessarily where the name of the program
560     /// starts.
561     unsigned getScopeLineNumber() const { return getUnsignedField(20); }
562
563     /// Verify - Verify that a subprogram descriptor is well formed.
564     bool Verify() const;
565
566     /// describes - Return true if this subprogram provides debugging
567     /// information for the function F.
568     bool describes(const Function *F);
569
570     Function *getFunction() const { return getFunctionField(16); }
571     DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
572     DISubprogram getFunctionDeclaration() const {
573       return getFieldAs<DISubprogram>(18);
574     }
575     MDNode *getVariablesNodes() const;
576     DIArray getVariables() const;
577   };
578
579   /// DIGlobalVariable - This is a wrapper for a global variable.
580   class DIGlobalVariable : public DIDescriptor {
581     friend class DIDescriptor;
582     void printInternal(raw_ostream &OS) const;
583   public:
584     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
585
586     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
587     StringRef getName() const         { return getStringField(3); }
588     StringRef getDisplayName() const  { return getStringField(4); }
589     StringRef getLinkageName() const  { return getStringField(5); }
590     DICompileUnit getCompileUnit() const{ 
591       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
592       if (getVersion() == llvm::LLVMDebugVersion7)
593         return getFieldAs<DICompileUnit>(6);
594
595       DIFile F = getFieldAs<DIFile>(6); 
596       return F.getCompileUnit();
597     }
598     StringRef getFilename() const {
599       if (getVersion() <= llvm::LLVMDebugVersion10)
600         return getContext().getFilename();
601       return getFieldAs<DIFile>(6).getFilename();
602     } 
603     StringRef getDirectory() const {
604       if (getVersion() <= llvm::LLVMDebugVersion10)
605         return getContext().getDirectory();
606       return getFieldAs<DIFile>(6).getDirectory();
607
608     } 
609
610     unsigned getLineNumber() const      { return getUnsignedField(7); }
611     DIType getType() const              { return getFieldAs<DIType>(8); }
612     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
613     unsigned isDefinition() const       { return getUnsignedField(10); }
614
615     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
616     Constant *getConstant() const   { return getConstantField(11); }
617
618     /// Verify - Verify that a global variable descriptor is well formed.
619     bool Verify() const;
620   };
621
622   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
623   /// global etc).
624   class DIVariable : public DIDescriptor {
625     friend class DIDescriptor;
626     void printInternal(raw_ostream &OS) const;
627   public:
628     explicit DIVariable(const MDNode *N = 0)
629       : DIDescriptor(N) {}
630
631     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
632     StringRef getName() const           { return getStringField(2);     }
633     DICompileUnit getCompileUnit() const { 
634       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
635       if (getVersion() == llvm::LLVMDebugVersion7)
636         return getFieldAs<DICompileUnit>(3);
637
638       DIFile F = getFieldAs<DIFile>(3); 
639       return F.getCompileUnit();
640     }
641     unsigned getLineNumber() const      { 
642       return (getUnsignedField(4) << 8) >> 8; 
643     }
644     unsigned getArgNumber() const       {
645       unsigned L = getUnsignedField(4); 
646       return L >> 24;
647     }
648     DIType getType() const              { return getFieldAs<DIType>(5); }
649     
650     /// isArtificial - Return true if this variable is marked as "artificial".
651     bool isArtificial() const    { 
652       if (getVersion() <= llvm::LLVMDebugVersion8)
653         return false;
654       return (getUnsignedField(6) & FlagArtificial) != 0;
655     }
656
657     /// getInlinedAt - If this variable is inlined then return inline location.
658     MDNode *getInlinedAt() const;
659
660     /// Verify - Verify that a variable descriptor is well formed.
661     bool Verify() const;
662
663     /// HasComplexAddr - Return true if the variable has a complex address.
664     bool hasComplexAddress() const {
665       return getNumAddrElements() > 0;
666     }
667
668     unsigned getNumAddrElements() const;
669     
670     uint64_t getAddrElement(unsigned Idx) const {
671       if (getVersion() <= llvm::LLVMDebugVersion8)
672         return getUInt64Field(Idx+6);
673       if (getVersion() == llvm::LLVMDebugVersion9)
674         return getUInt64Field(Idx+7);
675       return getUInt64Field(Idx+8);
676     }
677
678     /// isBlockByrefVariable - Return true if the variable was declared as
679     /// a "__block" variable (Apple Blocks).
680     bool isBlockByrefVariable() const {
681       return getType().isBlockByrefStruct();
682     }
683
684     /// isInlinedFnArgument - Return trule if this variable provides debugging
685     /// information for an inlined function arguments.
686     bool isInlinedFnArgument(const Function *CurFn);
687
688     void printExtendedName(raw_ostream &OS) const;
689   };
690
691   /// DILexicalBlock - This is a wrapper for a lexical block.
692   class DILexicalBlock : public DIScope {
693     virtual void anchor();
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     virtual void anchor();
713   public:
714     explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
715     DIScope getContext() const { return getScope().getContext(); }
716     unsigned getLineNumber() const { return getScope().getLineNumber(); }
717     unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
718     StringRef getDirectory() const {
719       StringRef dir = getFieldAs<DIFile>(2).getDirectory();
720       return !dir.empty() ? dir : getContext().getDirectory();
721     }
722     StringRef getFilename() const {
723       StringRef filename = getFieldAs<DIFile>(2).getFilename();
724       assert(!filename.empty() && "Why'd you create this then?");
725       return filename;
726     }
727     DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
728   };
729
730   /// DINameSpace - A wrapper for a C++ style name space.
731   class DINameSpace : public DIScope { 
732     virtual void anchor();
733   public:
734     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
735     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
736     StringRef getName() const      { return getStringField(2);           }
737     StringRef getDirectory() const  { 
738       return getFieldAs<DIFile>(3).getDirectory();
739     }
740     StringRef getFilename() const  { 
741       return getFieldAs<DIFile>(3).getFilename();
742     }
743     DICompileUnit getCompileUnit() const{ 
744       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
745       if (getVersion() == llvm::LLVMDebugVersion7)
746         return getFieldAs<DICompileUnit>(3);
747
748       return getFieldAs<DIFile>(3).getCompileUnit(); 
749     }
750     unsigned getLineNumber() const { return getUnsignedField(4);         }
751     bool Verify() const;
752   };
753
754   /// DILocation - This object holds location information. This object
755   /// is not associated with any DWARF tag.
756   class DILocation : public DIDescriptor {
757   public:
758     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
759
760     unsigned getLineNumber() const     { return getUnsignedField(0); }
761     unsigned getColumnNumber() const   { return getUnsignedField(1); }
762     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
763     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
764     StringRef getFilename() const    { return getScope().getFilename(); }
765     StringRef getDirectory() const   { return getScope().getDirectory(); }
766     bool Verify() const;
767   };
768
769   class DIObjCProperty : public DIDescriptor {
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(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(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