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