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