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