add getUnderlyingObjectSize()
[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     };
68   protected:
69     const MDNode *DbgNode;
70
71     StringRef getStringField(unsigned Elt) const;
72     unsigned getUnsignedField(unsigned Elt) const {
73       return (unsigned)getUInt64Field(Elt);
74     }
75     uint64_t getUInt64Field(unsigned Elt) const;
76     int64_t getInt64Field(unsigned Elt) const;
77     DIDescriptor getDescriptorField(unsigned Elt) const;
78
79     template <typename DescTy>
80     DescTy getFieldAs(unsigned Elt) const {
81       return DescTy(getDescriptorField(Elt));
82     }
83
84     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
85     Constant *getConstantField(unsigned Elt) const;
86     Function *getFunctionField(unsigned Elt) const;
87     void replaceFunctionField(unsigned Elt, Function *F);
88
89   public:
90     explicit DIDescriptor() : DbgNode(0) {}
91     explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
92     explicit DIDescriptor(const DIFile F);
93     explicit DIDescriptor(const DISubprogram F);
94     explicit DIDescriptor(const DILexicalBlockFile F);
95     explicit DIDescriptor(const DILexicalBlock F);
96     explicit DIDescriptor(const DIVariable F);
97     explicit DIDescriptor(const DIType F);
98
99     bool Verify() const { return DbgNode != 0; }
100
101     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
102     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
103
104     unsigned getVersion() const {
105       return getUnsignedField(0) & LLVMDebugVersionMask;
106     }
107
108     unsigned getTag() const {
109       return getUnsignedField(0) & ~LLVMDebugVersionMask;
110     }
111
112     bool isDerivedType() const;
113     bool isCompositeType() const;
114     bool isBasicType() const;
115     bool isVariable() const;
116     bool isSubprogram() const;
117     bool isGlobalVariable() const;
118     bool isScope() const;
119     bool isFile() const;
120     bool isCompileUnit() const;
121     bool isNameSpace() const;
122     bool isLexicalBlockFile() const;
123     bool isLexicalBlock() const;
124     bool isSubrange() const;
125     bool isEnumerator() const;
126     bool isType() const;
127     bool isGlobal() const;
128     bool isUnspecifiedParameter() const;
129     bool isTemplateTypeParameter() const;
130     bool isTemplateValueParameter() const;
131     bool isObjCProperty() const;
132
133     /// print - print descriptor.
134     void print(raw_ostream &OS) const;
135
136     /// dump - print descriptor to dbgs() with a newline.
137     void dump() const;
138   };
139
140   /// DISubrange - This is used to represent ranges, for array bounds.
141   class DISubrange : public DIDescriptor {
142     friend class DIDescriptor;
143     void printInternal(raw_ostream &OS) const;
144   public:
145     explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
146
147     int64_t getLo() const { return getInt64Field(1); }
148     int64_t  getCount() const { return getInt64Field(2); }
149   };
150
151   /// DIArray - This descriptor holds an array of descriptors.
152   class DIArray : public DIDescriptor {
153   public:
154     explicit DIArray(const MDNode *N = 0)
155       : DIDescriptor(N) {}
156
157     unsigned getNumElements() const;
158     DIDescriptor getElement(unsigned Idx) const {
159       return getDescriptorField(Idx);
160     }
161   };
162
163   /// DIScope - A base class for various scopes.
164   class DIScope : public DIDescriptor {
165   protected:
166     friend class DIDescriptor;
167     void printInternal(raw_ostream &OS) const;
168   public:
169     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
170
171     StringRef getFilename() const;
172     StringRef getDirectory() const;
173   };
174
175   /// DICompileUnit - A wrapper for a compile unit.
176   class DICompileUnit : public DIScope {
177     friend class DIDescriptor;
178     void printInternal(raw_ostream &OS) const;
179   public:
180     explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
181
182     unsigned getLanguage() const   { return getUnsignedField(2); }
183     StringRef getFilename() const  { return getStringField(3);   }
184     StringRef getDirectory() const { return getStringField(4);   }
185     StringRef getProducer() const  { return getStringField(5);   }
186
187     /// isMain - Each input file is encoded as a separate compile unit in LLVM
188     /// debugging information output. However, many target specific tool chains
189     /// prefer to encode only one compile unit in an object file. In this
190     /// situation, the LLVM code generator will include debugging information
191     /// entities in the compile unit that is marked as main compile unit. The
192     /// code generator accepts maximum one main compile unit per module. If a
193     /// module does not contain any main compile unit then the code generator
194     /// will emit multiple compile units in the output object file.
195     // TODO: This can be removed when we remove the legacy debug information.
196     bool isMain() const                { return getUnsignedField(6) != 0; }
197     bool isOptimized() const           { return getUnsignedField(7) != 0; }
198     StringRef getFlags() const       { return getStringField(8);   }
199     unsigned getRunTimeVersion() const { return getUnsignedField(9); }
200
201     DIArray getEnumTypes() const;
202     DIArray getRetainedTypes() const;
203     DIArray getSubprograms() const;
204     DIArray getGlobalVariables() const;
205
206     StringRef getSplitDebugFilename() const { return getStringField(14); }
207
208     /// Verify - Verify that a compile unit is well formed.
209     bool Verify() const;
210   };
211
212   /// DIFile - This is a wrapper for a file.
213   class DIFile : public DIScope {
214     friend class DIDescriptor;
215     void printInternal(raw_ostream &OS) const {} // FIXME: Output something?
216   public:
217     explicit DIFile(const MDNode *N = 0) : DIScope(N) {
218       if (DbgNode && !isFile())
219         DbgNode = 0;
220     }
221     StringRef getFilename() const  { return getStringField(1);   }
222     StringRef getDirectory() const { return getStringField(2);   }
223     DICompileUnit getCompileUnit() const{
224       assert (getVersion() <= LLVMDebugVersion10  && "Invalid CompileUnit!");
225       return getFieldAs<DICompileUnit>(3);
226     }
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     uint64_t getEnumValue() const      { return getUInt64Field(2); }
240   };
241
242   /// DIType - This is a wrapper for a type.
243   /// FIXME: Types should be factored much better so that CV qualifiers and
244   /// others do not require a huge and empty descriptor full of zeros.
245   class DIType : public DIScope {
246   protected:
247     friend class DIDescriptor;
248     void printInternal(raw_ostream &OS) const;
249     // This ctor is used when the Tag has already been validated by a derived
250     // ctor.
251     DIType(const MDNode *N, bool, bool) : DIScope(N) {}
252   public:
253     /// Verify - Verify that a type descriptor is well formed.
254     bool Verify() const;
255     explicit DIType(const MDNode *N);
256     explicit DIType() {}
257
258     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
259     StringRef getName() const           { return getStringField(2);     }
260     DICompileUnit getCompileUnit() const{
261       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
262      if (getVersion() == llvm::LLVMDebugVersion7)
263        return getFieldAs<DICompileUnit>(3);
264
265      return getFieldAs<DIFile>(3).getCompileUnit();
266     }
267     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
268     unsigned getLineNumber() const      { return getUnsignedField(4); }
269     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
270     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
271     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
272     // carry this is just plain insane.
273     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
274     unsigned getFlags() const           { return getUnsignedField(8); }
275     bool isPrivate() const {
276       return (getFlags() & FlagPrivate) != 0;
277     }
278     bool isProtected() const {
279       return (getFlags() & FlagProtected) != 0;
280     }
281     bool isForwardDecl() const {
282       return (getFlags() & FlagFwdDecl) != 0;
283     }
284     // isAppleBlock - Return true if this is the Apple Blocks extension.
285     bool isAppleBlockExtension() const {
286       return (getFlags() & FlagAppleBlock) != 0;
287     }
288     bool isBlockByrefStruct() const {
289       return (getFlags() & FlagBlockByrefStruct) != 0;
290     }
291     bool isVirtual() const {
292       return (getFlags() & FlagVirtual) != 0;
293     }
294     bool isArtificial() const {
295       return (getFlags() & FlagArtificial) != 0;
296     }
297     bool isObjectPointer() const {
298       return (getFlags() & FlagObjectPointer) != 0;
299     }
300     bool isObjcClassComplete() const {
301       return (getFlags() & FlagObjcClassComplete) != 0;
302     }
303     bool isVector() const {
304       return (getFlags() & FlagVector) != 0;
305     }
306     bool isStaticMember() const {
307       return (getFlags() & FlagStaticMember) != 0;
308     }
309     bool isValid() const {
310       return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
311     }
312     StringRef getDirectory() const  {
313       if (getVersion() == llvm::LLVMDebugVersion7)
314         return getCompileUnit().getDirectory();
315
316       return getFieldAs<DIFile>(3).getDirectory();
317     }
318     StringRef getFilename() const  {
319       if (getVersion() == llvm::LLVMDebugVersion7)
320         return getCompileUnit().getFilename();
321
322       return getFieldAs<DIFile>(3).getFilename();
323     }
324
325     /// isUnsignedDIType - Return true if type encoding is unsigned.
326     bool isUnsignedDIType();
327
328     /// replaceAllUsesWith - Replace all uses of debug info referenced by
329     /// this descriptor.
330     void replaceAllUsesWith(DIDescriptor &D);
331     void replaceAllUsesWith(MDNode *D);
332   };
333
334   /// DIBasicType - A basic type, like 'int' or 'float'.
335   class DIBasicType : public DIType {
336   public:
337     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
338
339     unsigned getEncoding() const { return getUnsignedField(9); }
340
341     /// Verify - Verify that a basic type descriptor is well formed.
342     bool Verify() const;
343   };
344
345   /// DIDerivedType - A simple derived type, like a const qualified type,
346   /// a typedef, a pointer or reference, et cetera.  Or, a data member of
347   /// a class/struct/union.
348   class DIDerivedType : public DIType {
349     friend class DIDescriptor;
350     void printInternal(raw_ostream &OS) const;
351   protected:
352     explicit DIDerivedType(const MDNode *N, bool, bool)
353       : DIType(N, true, true) {}
354   public:
355     explicit DIDerivedType(const MDNode *N = 0)
356       : DIType(N, true, true) {}
357
358     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
359
360     /// getOriginalTypeSize - If this type is derived from a base type then
361     /// return base type size.
362     uint64_t getOriginalTypeSize() const;
363
364     /// getObjCProperty - Return property node, if this ivar is
365     /// associated with one.
366     MDNode *getObjCProperty() const;
367
368     DIType getClassType() const {
369       assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
370       return getFieldAs<DIType>(10);
371     }
372
373     Constant *getConstant() const {
374       assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
375       return getConstantField(10);
376     }
377
378     StringRef getObjCPropertyName() const {
379       if (getVersion() > LLVMDebugVersion11)
380         return StringRef();
381       return getStringField(10);
382     }
383     StringRef getObjCPropertyGetterName() const {
384       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
385       return getStringField(11);
386     }
387     StringRef getObjCPropertySetterName() const {
388       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
389       return getStringField(12);
390     }
391     bool isReadOnlyObjCProperty() {
392       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
393       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
394     }
395     bool isReadWriteObjCProperty() {
396       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
397       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
398     }
399     bool isAssignObjCProperty() {
400       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
401       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
402     }
403     bool isRetainObjCProperty() {
404       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
405       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
406     }
407     bool isCopyObjCProperty() {
408       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
409       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
410     }
411     bool isNonAtomicObjCProperty() {
412       assert (getVersion() <= LLVMDebugVersion11  && "Invalid Request");
413       return (getUnsignedField(13) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
414     }
415
416     /// Verify - Verify that a derived type descriptor is well formed.
417     bool Verify() const;
418   };
419
420   /// DICompositeType - This descriptor holds a type that can refer to multiple
421   /// other types, like a function or struct.
422   /// FIXME: Why is this a DIDerivedType??
423   class DICompositeType : public DIDerivedType {
424     friend class DIDescriptor;
425     void printInternal(raw_ostream &OS) const;
426   public:
427     explicit DICompositeType(const MDNode *N = 0)
428       : DIDerivedType(N, true, true) {
429       if (N && !isCompositeType())
430         DbgNode = 0;
431     }
432
433     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
434     unsigned getRunTimeLang() const { return getUnsignedField(11); }
435     DICompositeType getContainingType() const {
436       return getFieldAs<DICompositeType>(12);
437     }
438     DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
439
440     /// Verify - Verify that a composite type descriptor is well formed.
441     bool Verify() const;
442   };
443
444   /// DITemplateTypeParameter - This is a wrapper for template type parameter.
445   class DITemplateTypeParameter : public DIDescriptor {
446   public:
447     explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
448
449     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
450     StringRef getName() const        { return getStringField(2); }
451     DIType getType() const           { return getFieldAs<DIType>(3); }
452     StringRef getFilename() const    {
453       return getFieldAs<DIFile>(4).getFilename();
454     }
455     StringRef getDirectory() const   {
456       return getFieldAs<DIFile>(4).getDirectory();
457     }
458     unsigned getLineNumber() const   { return getUnsignedField(5); }
459     unsigned getColumnNumber() const { return getUnsignedField(6); }
460   };
461
462   /// DITemplateValueParameter - This is a wrapper for template value parameter.
463   class DITemplateValueParameter : public DIDescriptor {
464   public:
465     explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
466
467     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
468     StringRef getName() const        { return getStringField(2); }
469     DIType getType() const           { return getFieldAs<DIType>(3); }
470     uint64_t getValue() const         { return getUInt64Field(4); }
471     StringRef getFilename() const    {
472       return getFieldAs<DIFile>(5).getFilename();
473     }
474     StringRef getDirectory() const   {
475       return getFieldAs<DIFile>(5).getDirectory();
476     }
477     unsigned getLineNumber() const   { return getUnsignedField(6); }
478     unsigned getColumnNumber() const { return getUnsignedField(7); }
479   };
480
481   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
482   class DISubprogram : public DIScope {
483     friend class DIDescriptor;
484     void printInternal(raw_ostream &OS) const;
485   public:
486     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
487
488     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
489     StringRef getName() const         { return getStringField(3); }
490     StringRef getDisplayName() const  { return getStringField(4); }
491     StringRef getLinkageName() const  { return getStringField(5); }
492     DICompileUnit getCompileUnit() const{
493       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
494       if (getVersion() == llvm::LLVMDebugVersion7)
495         return getFieldAs<DICompileUnit>(6);
496
497       return getFieldAs<DIFile>(6).getCompileUnit();
498     }
499     unsigned getLineNumber() const      { return getUnsignedField(7); }
500     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
501
502     /// getReturnTypeName - Subprogram return types are encoded either as
503     /// DIType or as DICompositeType.
504     StringRef getReturnTypeName() const {
505       DICompositeType DCT(getFieldAs<DICompositeType>(8));
506       if (DCT.Verify()) {
507         DIArray A = DCT.getTypeArray();
508         DIType T(A.getElement(0));
509         return T.getName();
510       }
511       DIType T(getFieldAs<DIType>(8));
512       return T.getName();
513     }
514
515     /// isLocalToUnit - Return true if this subprogram is local to the current
516     /// compile unit, like 'static' in C.
517     unsigned isLocalToUnit() const     { return getUnsignedField(9); }
518     unsigned isDefinition() const      { return getUnsignedField(10); }
519
520     unsigned getVirtuality() const { return getUnsignedField(11); }
521     unsigned getVirtualIndex() const { return getUnsignedField(12); }
522
523     DICompositeType getContainingType() const {
524       return getFieldAs<DICompositeType>(13);
525     }
526
527     unsigned getFlags() const {
528       return getUnsignedField(14);
529     }
530
531     unsigned isArtificial() const    {
532       if (getVersion() <= llvm::LLVMDebugVersion8)
533         return getUnsignedField(14);
534       return (getUnsignedField(14) & FlagArtificial) != 0;
535     }
536     /// isPrivate - Return true if this subprogram has "private"
537     /// access specifier.
538     bool isPrivate() const    {
539       if (getVersion() <= llvm::LLVMDebugVersion8)
540         return false;
541       return (getUnsignedField(14) & FlagPrivate) != 0;
542     }
543     /// isProtected - Return true if this subprogram has "protected"
544     /// access specifier.
545     bool isProtected() const    {
546       if (getVersion() <= llvm::LLVMDebugVersion8)
547         return false;
548       return (getUnsignedField(14) & FlagProtected) != 0;
549     }
550     /// isExplicit - Return true if this subprogram is marked as explicit.
551     bool isExplicit() const    {
552       if (getVersion() <= llvm::LLVMDebugVersion8)
553         return false;
554       return (getUnsignedField(14) & FlagExplicit) != 0;
555     }
556     /// isPrototyped - Return true if this subprogram is prototyped.
557     bool isPrototyped() const    {
558       if (getVersion() <= llvm::LLVMDebugVersion8)
559         return false;
560       return (getUnsignedField(14) & FlagPrototyped) != 0;
561     }
562
563     unsigned isOptimized() const;
564
565     StringRef getFilename() const    {
566       if (getVersion() == llvm::LLVMDebugVersion7)
567         return getCompileUnit().getFilename();
568
569       return getFieldAs<DIFile>(6).getFilename();
570     }
571
572     StringRef getDirectory() const   {
573       if (getVersion() == llvm::LLVMDebugVersion7)
574         return getCompileUnit().getFilename();
575
576       return getFieldAs<DIFile>(6).getDirectory();
577     }
578
579     DIFile getFile() const {
580       return getFieldAs<DIFile>(6);
581     }
582
583     /// getScopeLineNumber - Get the beginning of the scope of the
584     /// function, not necessarily where the name of the program
585     /// starts.
586     unsigned getScopeLineNumber() const { return getUnsignedField(20); }
587
588     /// Verify - Verify that a subprogram descriptor is well formed.
589     bool Verify() const;
590
591     /// describes - Return true if this subprogram provides debugging
592     /// information for the function F.
593     bool describes(const Function *F);
594
595     Function *getFunction() const { return getFunctionField(16); }
596     void replaceFunction(Function *F) { replaceFunctionField(16, F); }
597     DIArray getTemplateParams() const { return getFieldAs<DIArray>(17); }
598     DISubprogram getFunctionDeclaration() const {
599       return getFieldAs<DISubprogram>(18);
600     }
601     MDNode *getVariablesNodes() const;
602     DIArray getVariables() const;
603   };
604
605   /// DIGlobalVariable - This is a wrapper for a global variable.
606   class DIGlobalVariable : public DIDescriptor {
607     friend class DIDescriptor;
608     void printInternal(raw_ostream &OS) const;
609   public:
610     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
611
612     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
613     StringRef getName() const         { return getStringField(3); }
614     StringRef getDisplayName() const  { return getStringField(4); }
615     StringRef getLinkageName() const  { return getStringField(5); }
616     DICompileUnit getCompileUnit() const{
617       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
618       if (getVersion() == llvm::LLVMDebugVersion7)
619         return getFieldAs<DICompileUnit>(6);
620
621       DIFile F = getFieldAs<DIFile>(6);
622       return F.getCompileUnit();
623     }
624     StringRef getFilename() const {
625       if (getVersion() <= llvm::LLVMDebugVersion10)
626         return getContext().getFilename();
627       return getFieldAs<DIFile>(6).getFilename();
628     }
629     StringRef getDirectory() const {
630       if (getVersion() <= llvm::LLVMDebugVersion10)
631         return getContext().getDirectory();
632       return getFieldAs<DIFile>(6).getDirectory();
633
634     }
635
636     unsigned getLineNumber() const      { return getUnsignedField(7); }
637     DIType getType() const              { return getFieldAs<DIType>(8); }
638     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
639     unsigned isDefinition() const       { return getUnsignedField(10); }
640
641     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
642     Constant *getConstant() const   { return getConstantField(11); }
643     DIDerivedType getStaticDataMemberDeclaration() const {
644       return getFieldAs<DIDerivedType>(12);
645     }
646
647     /// Verify - Verify that a global variable descriptor is well formed.
648     bool Verify() const;
649   };
650
651   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
652   /// global etc).
653   class DIVariable : public DIDescriptor {
654     friend class DIDescriptor;
655     void printInternal(raw_ostream &OS) const;
656   public:
657     explicit DIVariable(const MDNode *N = 0)
658       : DIDescriptor(N) {}
659
660     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
661     StringRef getName() const           { return getStringField(2);     }
662     DICompileUnit getCompileUnit() const {
663       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
664       if (getVersion() == llvm::LLVMDebugVersion7)
665         return getFieldAs<DICompileUnit>(3);
666
667       DIFile F = getFieldAs<DIFile>(3);
668       return F.getCompileUnit();
669     }
670     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
671     unsigned getLineNumber() const      {
672       return (getUnsignedField(4) << 8) >> 8;
673     }
674     unsigned getArgNumber() const       {
675       unsigned L = getUnsignedField(4);
676       return L >> 24;
677     }
678     DIType getType() const              { return getFieldAs<DIType>(5); }
679
680     /// isArtificial - Return true if this variable is marked as "artificial".
681     bool isArtificial() const    {
682       if (getVersion() <= llvm::LLVMDebugVersion8)
683         return false;
684       return (getUnsignedField(6) & FlagArtificial) != 0;
685     }
686
687     bool isObjectPointer() const {
688       return (getUnsignedField(6) & FlagObjectPointer) != 0;
689     }
690
691     /// getInlinedAt - If this variable is inlined then return inline location.
692     MDNode *getInlinedAt() const;
693
694     /// Verify - Verify that a variable descriptor is well formed.
695     bool Verify() const;
696
697     /// HasComplexAddr - Return true if the variable has a complex address.
698     bool hasComplexAddress() const {
699       return getNumAddrElements() > 0;
700     }
701
702     unsigned getNumAddrElements() const;
703
704     uint64_t getAddrElement(unsigned Idx) const {
705       if (getVersion() <= llvm::LLVMDebugVersion8)
706         return getUInt64Field(Idx+6);
707       if (getVersion() == llvm::LLVMDebugVersion9)
708         return getUInt64Field(Idx+7);
709       return getUInt64Field(Idx+8);
710     }
711
712     /// isBlockByrefVariable - Return true if the variable was declared as
713     /// a "__block" variable (Apple Blocks).
714     bool isBlockByrefVariable() const {
715       return getType().isBlockByrefStruct();
716     }
717
718     /// isInlinedFnArgument - Return true if this variable provides debugging
719     /// information for an inlined function arguments.
720     bool isInlinedFnArgument(const Function *CurFn);
721
722     void printExtendedName(raw_ostream &OS) const;
723   };
724
725   /// DILexicalBlock - This is a wrapper for a lexical block.
726   class DILexicalBlock : public DIScope {
727   public:
728     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
729     DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
730     unsigned getLineNumber() const   { return getUnsignedField(2);         }
731     unsigned getColumnNumber() const { return getUnsignedField(3);         }
732     StringRef getDirectory() const {
733       StringRef dir = getFieldAs<DIFile>(4).getDirectory();
734       return !dir.empty() ? dir : getContext().getDirectory();
735     }
736     StringRef getFilename() const {
737       StringRef filename = getFieldAs<DIFile>(4).getFilename();
738       return !filename.empty() ? filename : getContext().getFilename();
739     }
740   };
741
742   /// DILexicalBlockFile - This is a wrapper for a lexical block with
743   /// a filename change.
744   class DILexicalBlockFile : public DIScope {
745   public:
746     explicit DILexicalBlockFile(const MDNode *N = 0) : DIScope(N) {}
747     DIScope getContext() const { if (getScope().isSubprogram()) return getScope(); return getScope().getContext(); }
748     unsigned getLineNumber() const { return getScope().getLineNumber(); }
749     unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
750     StringRef getDirectory() const {
751       StringRef dir = getFieldAs<DIFile>(2).getDirectory();
752       return !dir.empty() ? dir : getContext().getDirectory();
753     }
754     StringRef getFilename() const {
755       StringRef filename = getFieldAs<DIFile>(2).getFilename();
756       assert(!filename.empty() && "Why'd you create this then?");
757       return filename;
758     }
759     DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(1); }
760   };
761
762   /// DINameSpace - A wrapper for a C++ style name space.
763   class DINameSpace : public DIScope {
764   public:
765     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
766     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
767     StringRef getName() const      { return getStringField(2);           }
768     StringRef getDirectory() const  {
769       return getFieldAs<DIFile>(3).getDirectory();
770     }
771     StringRef getFilename() const  {
772       return getFieldAs<DIFile>(3).getFilename();
773     }
774     DICompileUnit getCompileUnit() const{
775       assert (getVersion() <= LLVMDebugVersion10 && "Invalid getCompileUnit!");
776       if (getVersion() == llvm::LLVMDebugVersion7)
777         return getFieldAs<DICompileUnit>(3);
778
779       return getFieldAs<DIFile>(3).getCompileUnit();
780     }
781     unsigned getLineNumber() const { return getUnsignedField(4);         }
782     bool Verify() const;
783   };
784
785   /// DILocation - This object holds location information. This object
786   /// is not associated with any DWARF tag.
787   class DILocation : public DIDescriptor {
788   public:
789     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
790
791     unsigned getLineNumber() const     { return getUnsignedField(0); }
792     unsigned getColumnNumber() const   { return getUnsignedField(1); }
793     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
794     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
795     StringRef getFilename() const    { return getScope().getFilename(); }
796     StringRef getDirectory() const   { return getScope().getDirectory(); }
797     bool Verify() const;
798   };
799
800   class DIObjCProperty : public DIDescriptor {
801     friend class DIDescriptor;
802     void printInternal(raw_ostream &OS) const;
803   public:
804     explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) { }
805
806     StringRef getObjCPropertyName() const { return getStringField(1); }
807     DIFile getFile() const { return getFieldAs<DIFile>(2); }
808     unsigned getLineNumber() const { return getUnsignedField(3); }
809
810     StringRef getObjCPropertyGetterName() const {
811       return getStringField(4);
812     }
813     StringRef getObjCPropertySetterName() const {
814       return getStringField(5);
815     }
816     bool isReadOnlyObjCProperty() {
817       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
818     }
819     bool isReadWriteObjCProperty() {
820       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
821     }
822     bool isAssignObjCProperty() {
823       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_assign) != 0;
824     }
825     bool isRetainObjCProperty() {
826       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_retain) != 0;
827     }
828     bool isCopyObjCProperty() {
829       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_copy) != 0;
830     }
831     bool isNonAtomicObjCProperty() {
832       return (getUnsignedField(6) & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
833     }
834
835     DIType getType() const { return getFieldAs<DIType>(7); }
836
837     /// Verify - Verify that a derived type descriptor is well formed.
838     bool Verify() const;
839   };
840
841   /// getDISubprogram - Find subprogram that is enclosing this scope.
842   DISubprogram getDISubprogram(const MDNode *Scope);
843
844   /// getDICompositeType - Find underlying composite type.
845   DICompositeType getDICompositeType(DIType T);
846
847   /// isSubprogramContext - Return true if Context is either a subprogram
848   /// or another context nested inside a subprogram.
849   bool isSubprogramContext(const MDNode *Context);
850
851   /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
852   /// to hold function specific information.
853   NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, DISubprogram SP);
854
855   /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
856   /// suitable to hold function specific information.
857   NamedMDNode *getFnSpecificMDNode(const Module &M, DISubprogram SP);
858
859   /// createInlinedVariable - Create a new inlined variable based on current
860   /// variable.
861   /// @param DV            Current Variable.
862   /// @param InlinedScope  Location at current variable is inlined.
863   DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
864                                    LLVMContext &VMContext);
865
866   /// cleanseInlinedVariable - Remove inlined scope from the variable.
867   DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
868
869   class DebugInfoFinder {
870   public:
871     /// processModule - Process entire module and collect debug info
872     /// anchors.
873     void processModule(const Module &M);
874
875   private:
876     /// processType - Process DIType.
877     void processType(DIType DT);
878
879     /// processLexicalBlock - Process DILexicalBlock.
880     void processLexicalBlock(DILexicalBlock LB);
881
882     /// processSubprogram - Process DISubprogram.
883     void processSubprogram(DISubprogram SP);
884
885     /// processDeclare - Process DbgDeclareInst.
886     void processDeclare(const DbgDeclareInst *DDI);
887
888     /// processLocation - Process DILocation.
889     void processLocation(DILocation Loc);
890
891     /// addCompileUnit - Add compile unit into CUs.
892     bool addCompileUnit(DICompileUnit CU);
893
894     /// addGlobalVariable - Add global variable into GVs.
895     bool addGlobalVariable(DIGlobalVariable DIG);
896
897     // addSubprogram - Add subprogram into SPs.
898     bool addSubprogram(DISubprogram SP);
899
900     /// addType - Add type into Tys.
901     bool addType(DIType DT);
902
903   public:
904     typedef SmallVector<MDNode *, 8>::const_iterator iterator;
905     iterator compile_unit_begin()    const { return CUs.begin(); }
906     iterator compile_unit_end()      const { return CUs.end(); }
907     iterator subprogram_begin()      const { return SPs.begin(); }
908     iterator subprogram_end()        const { return SPs.end(); }
909     iterator global_variable_begin() const { return GVs.begin(); }
910     iterator global_variable_end()   const { return GVs.end(); }
911     iterator type_begin()            const { return TYs.begin(); }
912     iterator type_end()              const { return TYs.end(); }
913
914     unsigned compile_unit_count()    const { return CUs.size(); }
915     unsigned global_variable_count() const { return GVs.size(); }
916     unsigned subprogram_count()      const { return SPs.size(); }
917     unsigned type_count()            const { return TYs.size(); }
918
919   private:
920     SmallVector<MDNode *, 8> CUs;  // Compile Units
921     SmallVector<MDNode *, 8> SPs;  // Subprograms
922     SmallVector<MDNode *, 8> GVs;  // Global Variables;
923     SmallVector<MDNode *, 8> TYs;  // Types
924     SmallPtrSet<MDNode *, 64> NodesSeen;
925   };
926 } // end namespace llvm
927
928 #endif