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