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