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