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