Add a TODO and explain when we can get rid of the isMain field.
[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     // TODO: This can be removed when we remove the legacy debug information.
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 getFlags() const {
526       return getUnsignedField(14);
527     }
528
529     unsigned isArtificial() const    {
530       if (getVersion() <= llvm::LLVMDebugVersion8)
531         return getUnsignedField(14);
532       return (getUnsignedField(14) & FlagArtificial) != 0;
533     }
534     /// isPrivate - Return true if this subprogram has "private"
535     /// access specifier.
536     bool isPrivate() const    {
537       if (getVersion() <= llvm::LLVMDebugVersion8)
538         return false;
539       return (getUnsignedField(14) & FlagPrivate) != 0;
540     }
541     /// isProtected - Return true if this subprogram has "protected"
542     /// access specifier.
543     bool isProtected() const    {
544       if (getVersion() <= llvm::LLVMDebugVersion8)
545         return false;
546       return (getUnsignedField(14) & FlagProtected) != 0;
547     }
548     /// isExplicit - Return true if this subprogram is marked as explicit.
549     bool isExplicit() const    {
550       if (getVersion() <= llvm::LLVMDebugVersion8)
551         return false;
552       return (getUnsignedField(14) & FlagExplicit) != 0;
553     }
554     /// isPrototyped - Return true if this subprogram is prototyped.
555     bool isPrototyped() const    {
556       if (getVersion() <= llvm::LLVMDebugVersion8)
557         return false;
558       return (getUnsignedField(14) & FlagPrototyped) != 0;
559     }
560
561     unsigned isOptimized() const;
562
563     StringRef getFilename() const    {
564       if (getVersion() == llvm::LLVMDebugVersion7)
565         return getCompileUnit().getFilename();
566
567       return getFieldAs<DIFile>(6).getFilename();
568     }
569
570     StringRef getDirectory() const   {
571       if (getVersion() == llvm::LLVMDebugVersion7)
572         return getCompileUnit().getFilename();
573
574       return getFieldAs<DIFile>(6).getDirectory();
575     }
576
577     DIFile getFile() const {
578       return getFieldAs<DIFile>(6);
579     }
580
581     /// getScopeLineNumber - Get the beginning of the scope of the
582     /// function, not necessarily where the name of the program
583     /// starts.
584     unsigned getScopeLineNumber() const { return getUnsignedField(20); }
585
586     /// Verify - Verify that a subprogram descriptor is well formed.
587     bool Verify() const;
588
589     /// describes - Return true if this subprogram provides debugging
590     /// information for the function F.
591     bool describes(const Function *F);
592
593     Function *getFunction() const { return getFunctionField(16); }
594     void replaceFunction(Function *F) { replaceFunctionField(16, F); }
595     DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
596     DISubprogram getFunctionDeclaration() const {
597       return getFieldAs<DISubprogram>(18);
598     }
599     MDNode *getVariablesNodes() const;
600     DIArray getVariables() const;
601   };
602
603   /// DIGlobalVariable - This is a wrapper for a global variable.
604   class DIGlobalVariable : public DIDescriptor {
605     friend class DIDescriptor;
606     void printInternal(raw_ostream &OS) const;
607   public:
608     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
609
610     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
611     StringRef getName() const         { return getStringField(3); }
612     StringRef getDisplayName() const  { return getStringField(4); }
613     StringRef getLinkageName() const  { return getStringField(5); }
614     DICompileUnit getCompileUnit() const{
615       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
616       if (getVersion() == llvm::LLVMDebugVersion7)
617         return getFieldAs<DICompileUnit>(6);
618
619       DIFile F = getFieldAs<DIFile>(6);
620       return F.getCompileUnit();
621     }
622     StringRef getFilename() const {
623       if (getVersion() <= llvm::LLVMDebugVersion10)
624         return getContext().getFilename();
625       return getFieldAs<DIFile>(6).getFilename();
626     }
627     StringRef getDirectory() const {
628       if (getVersion() <= llvm::LLVMDebugVersion10)
629         return getContext().getDirectory();
630       return getFieldAs<DIFile>(6).getDirectory();
631
632     }
633
634     unsigned getLineNumber() const      { return getUnsignedField(7); }
635     DIType getType() const              { return getFieldAs<DIType>(8); }
636     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
637     unsigned isDefinition() const       { return getUnsignedField(10); }
638
639     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
640     Constant *getConstant() const   { return getConstantField(11); }
641     DIDerivedType getStaticDataMemberDeclaration() const {
642       return getFieldAs<DIDerivedType>(12);
643     }
644
645     /// Verify - Verify that a global variable descriptor is well formed.
646     bool Verify() const;
647   };
648
649   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
650   /// global etc).
651   class DIVariable : public DIDescriptor {
652     friend class DIDescriptor;
653     void printInternal(raw_ostream &OS) const;
654   public:
655     explicit DIVariable(const MDNode *N = 0)
656       : DIDescriptor(N) {}
657
658     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
659     StringRef getName() const           { return getStringField(2);     }
660     DICompileUnit getCompileUnit() const {
661       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
662       if (getVersion() == llvm::LLVMDebugVersion7)
663         return getFieldAs<DICompileUnit>(3);
664
665       DIFile F = getFieldAs<DIFile>(3);
666       return F.getCompileUnit();
667     }
668     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
669     unsigned getLineNumber() const      {
670       return (getUnsignedField(4) << 8) >> 8;
671     }
672     unsigned getArgNumber() const       {
673       unsigned L = getUnsignedField(4);
674       return L >> 24;
675     }
676     DIType getType() const              { return getFieldAs<DIType>(5); }
677
678     /// isArtificial - Return true if this variable is marked as "artificial".
679     bool isArtificial() const    {
680       if (getVersion() <= llvm::LLVMDebugVersion8)
681         return false;
682       return (getUnsignedField(6) & FlagArtificial) != 0;
683     }
684
685     bool isObjectPointer() const {
686       return (getUnsignedField(6) & FlagObjectPointer) != 0;
687     }
688
689     /// getInlinedAt - If this variable is inlined then return inline location.
690     MDNode *getInlinedAt() const;
691
692     /// Verify - Verify that a variable descriptor is well formed.
693     bool Verify() const;
694
695     /// HasComplexAddr - Return true if the variable has a complex address.
696     bool hasComplexAddress() const {
697       return getNumAddrElements() > 0;
698     }
699
700     unsigned getNumAddrElements() const;
701
702     uint64_t getAddrElement(unsigned Idx) const {
703       if (getVersion() <= llvm::LLVMDebugVersion8)
704         return getUInt64Field(Idx+6);
705       if (getVersion() == llvm::LLVMDebugVersion9)
706         return getUInt64Field(Idx+7);
707       return getUInt64Field(Idx+8);
708     }
709
710     /// isBlockByrefVariable - Return true if the variable was declared as
711     /// a "__block" variable (Apple Blocks).
712     bool isBlockByrefVariable() const {
713       return getType().isBlockByrefStruct();
714     }
715
716     /// isInlinedFnArgument - Return true if this variable provides debugging
717     /// information for an inlined function arguments.
718     bool isInlinedFnArgument(const Function *CurFn);
719
720     void printExtendedName(raw_ostream &OS) const;
721   };
722
723   /// DILexicalBlock - This is a wrapper for a lexical block.
724   class DILexicalBlock : public DIScope {
725   public:
726     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
727     DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
728     unsigned getLineNumber() const   { return getUnsignedField(2);         }
729     unsigned getColumnNumber() const { return getUnsignedField(3);         }
730     StringRef getDirectory() const {
731       StringRef dir = getFieldAs<DIFile>(4).getDirectory();
732       return !dir.empty() ? dir : getContext().getDirectory();
733     }
734     StringRef getFilename() const {
735       StringRef filename = getFieldAs<DIFile>(4).getFilename();
736       return !filename.empty() ? filename : getContext().getFilename();
737     }
738   };
739
740   /// DILexicalBlockFile - This is a wrapper for a lexical block with
741   /// a filename change.
742   class DILexicalBlockFile : public DIScope {
743   public:
744     explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
745     DIScope getContext() const { if (getScope().isSubprogram()) return getScope(); return getScope().getContext(); }
746     unsigned getLineNumber() const { return getScope().getLineNumber(); }
747     unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
748     StringRef getDirectory() const {
749       StringRef dir = getFieldAs<DIFile>(2).getDirectory();
750       return !dir.empty() ? dir : getContext().getDirectory();
751     }
752     StringRef getFilename() const {
753       StringRef filename = getFieldAs<DIFile>(2).getFilename();
754       assert(!filename.empty() && "Why'd you create this then?");
755       return filename;
756     }
757     DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
758   };
759
760   /// DINameSpace - A wrapper for a C++ style name space.
761   class DINameSpace : public DIScope {
762   public:
763     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
764     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
765     StringRef getName() const      { return getStringField(2);           }
766     StringRef getDirectory() const  {
767       return getFieldAs<DIFile>(3).getDirectory();
768     }
769     StringRef getFilename() const  {
770       return getFieldAs<DIFile>(3).getFilename();
771     }
772     DICompileUnit getCompileUnit() const{
773       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
774       if (getVersion() == llvm::LLVMDebugVersion7)
775         return getFieldAs<DICompileUnit>(3);
776
777       return getFieldAs<DIFile>(3).getCompileUnit();
778     }
779     unsigned getLineNumber() const { return getUnsignedField(4);         }
780     bool Verify() const;
781   };
782
783   /// DILocation - This object holds location information. This object
784   /// is not associated with any DWARF tag.
785   class DILocation : public DIDescriptor {
786   public:
787     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
788
789     unsigned getLineNumber() const     { return getUnsignedField(0); }
790     unsigned getColumnNumber() const   { return getUnsignedField(1); }
791     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
792     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
793     StringRef getFilename() const    { return getScope().getFilename(); }
794     StringRef getDirectory() const   { return getScope().getDirectory(); }
795     bool Verify() const;
796   };
797
798   class DIObjCProperty : public DIDescriptor {
799     friend class DIDescriptor;
800     void printInternal(raw_ostream &OS) const;
801   public:
802     explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) { }
803
804     StringRef getObjCPropertyName() const { return getStringField(1); }
805     DIFile getFile() const { return getFieldAs<DIFile>(2); }
806     unsigned getLineNumber() const { return getUnsignedField(3); }
807
808     StringRef getObjCPropertyGetterName() const {
809       return getStringField(4);
810     }
811     StringRef getObjCPropertySetterName() const {
812       return getStringField(5);
813     }
814     bool isReadOnlyObjCProperty() {
815       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
816     }
817     bool isReadWriteObjCProperty() {
818       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
819     }
820     bool isAssignObjCProperty() {
821       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
822     }
823     bool isRetainObjCProperty() {
824       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
825     }
826     bool isCopyObjCProperty() {
827       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
828     }
829     bool isNonAtomicObjCProperty() {
830       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
831     }
832
833     DIType getType() const { return getFieldAs<DIType>(7); }
834
835     /// Verify - Verify that a derived type descriptor is well formed.
836     bool Verify() const;
837   };
838
839   /// getDISubprogram - Find subprogram that is enclosing this scope.
840   DISubprogram getDISubprogram(const MDNode *Scope);
841
842   /// getDICompositeType - Find underlying composite type.
843   DICompositeType getDICompositeType(DIType T);
844
845   /// isSubprogramContext - Return true if Context is either a subprogram
846   /// or another context nested inside a subprogram.
847   bool isSubprogramContext(const MDNode *Context);
848
849   /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
850   /// to hold function specific information.
851   NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
852
853   /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
854   /// suitable to hold function specific information.
855   NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
856
857   /// createInlinedVariable - Create a new inlined variable based on current
858   /// variable.
859   /// @param DV            Current Variable.
860   /// @param InlinedScope  Location at current variable is inlined.
861   DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
862                                    LLVMContext &VMContext);
863
864   /// cleanseInlinedVariable - Remove inlined scope from the variable.
865   DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
866
867   class DebugInfoFinder {
868   public:
869     /// processModule - Process entire module and collect debug info
870     /// anchors.
871     void processModule(const Module &M);
872
873   private:
874     /// processType - Process DIType.
875     void processType(DIType DT);
876
877     /// processLexicalBlock - Process DILexicalBlock.
878     void processLexicalBlock(DILexicalBlock LB);
879
880     /// processSubprogram - Process DISubprogram.
881     void processSubprogram(DISubprogram SP);
882
883     /// processDeclare - Process DbgDeclareInst.
884     void processDeclare(const DbgDeclareInst *DDI);
885
886     /// processLocation - Process DILocation.
887     void processLocation(DILocation Loc);
888
889     /// addCompileUnit - Add compile unit into CUs.
890     bool addCompileUnit(DICompileUnit CU);
891
892     /// addGlobalVariable - Add global variable into GVs.
893     bool addGlobalVariable(DIGlobalVariable DIG);
894
895     // addSubprogram - Add subprogram into SPs.
896     bool addSubprogram(DISubprogram SP);
897
898     /// addType - Add type into Tys.
899     bool addType(DIType DT);
900
901   public:
902     typedef SmallVector<MDNode *, 8>::const_iterator iterator;
903     iterator compile_unit_begin()    const { return CUs.begin(); }
904     iterator compile_unit_end()      const { return CUs.end(); }
905     iterator subprogram_begin()      const { return SPs.begin(); }
906     iterator subprogram_end()        const { return SPs.end(); }
907     iterator global_variable_begin() const { return GVs.begin(); }
908     iterator global_variable_end()   const { return GVs.end(); }
909     iterator type_begin()            const { return TYs.begin(); }
910     iterator type_end()              const { return TYs.end(); }
911
912     unsigned compile_unit_count()    const { return CUs.size(); }
913     unsigned global_variable_count() const { return GVs.size(); }
914     unsigned subprogram_count()      const { return SPs.size(); }
915     unsigned type_count()            const { return TYs.size(); }
916
917   private:
918     SmallVector<MDNode *, 8> CUs;  // Compile Units
919     SmallVector<MDNode *, 8> SPs;  // Subprograms
920     SmallVector<MDNode *, 8> GVs;  // Global Variables;
921     SmallVector<MDNode *, 8> TYs;  // Types
922     SmallPtrSet<MDNode *, 64> NodesSeen;
923   };
924 } // end namespace llvm
925
926 #endif