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