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