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