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