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