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