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