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