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