]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/DebugInfo.h
Merge ACPICA 20110413.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / 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_ANALYSIS_DEBUGINFO_H
18 #define LLVM_ANALYSIS_DEBUGINFO_H
19
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/SmallPtrSet.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 DIVariable;
44   class DIType;
45
46   /// DIDescriptor - A thin wraper around MDNode to access encoded debug info.
47   /// This should not be stored in a container, because underly MDNode may
48   /// change in certain situations.
49   class DIDescriptor {
50   public:
51     enum {
52       FlagPrivate          = 1 << 0,
53       FlagProtected        = 1 << 1,
54       FlagFwdDecl          = 1 << 2,
55       FlagAppleBlock       = 1 << 3,
56       FlagBlockByrefStruct = 1 << 4,
57       FlagVirtual          = 1 << 5,
58       FlagArtificial       = 1 << 6,
59       FlagExplicit         = 1 << 7,
60       FlagPrototyped       = 1 << 8
61     };
62   protected:
63     const MDNode *DbgNode;
64
65     StringRef getStringField(unsigned Elt) const;
66     unsigned getUnsignedField(unsigned Elt) const {
67       return (unsigned)getUInt64Field(Elt);
68     }
69     uint64_t getUInt64Field(unsigned Elt) const;
70     DIDescriptor getDescriptorField(unsigned Elt) const;
71
72     template <typename DescTy>
73     DescTy getFieldAs(unsigned Elt) const {
74       return DescTy(getDescriptorField(Elt));
75     }
76
77     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
78     Constant *getConstantField(unsigned Elt) const;
79     Function *getFunctionField(unsigned Elt) const;
80
81   public:
82     explicit DIDescriptor() : DbgNode(0) {}
83     explicit DIDescriptor(const MDNode *N) : DbgNode(N) {}
84     explicit DIDescriptor(const DIFile F);
85     explicit DIDescriptor(const DISubprogram F);
86     explicit DIDescriptor(const DILexicalBlock F);
87     explicit DIDescriptor(const DIVariable F);
88     explicit DIDescriptor(const DIType F);
89
90     bool Verify() const { return DbgNode != 0; }
91
92     operator MDNode *() const { return const_cast<MDNode*>(DbgNode); }
93     MDNode *operator ->() const { return const_cast<MDNode*>(DbgNode); }
94
95     unsigned getVersion() const {
96       return getUnsignedField(0) & LLVMDebugVersionMask;
97     }
98
99     unsigned getTag() const {
100       return getUnsignedField(0) & ~LLVMDebugVersionMask;
101     }
102
103     /// print - print descriptor.
104     void print(raw_ostream &OS) const;
105
106     /// dump - print descriptor to dbgs() with a newline.
107     void dump() const;
108
109     bool isDerivedType() const;
110     bool isCompositeType() const;
111     bool isBasicType() const;
112     bool isVariable() const;
113     bool isSubprogram() const;
114     bool isGlobalVariable() const;
115     bool isScope() const;
116     bool isFile() const;
117     bool isCompileUnit() const;
118     bool isNameSpace() const;
119     bool isLexicalBlock() const;
120     bool isSubrange() const;
121     bool isEnumerator() const;
122     bool isType() const;
123     bool isGlobal() const;
124     bool isUnspecifiedParameter() const;
125     bool isTemplateTypeParameter() const;
126     bool isTemplateValueParameter() const;
127   };
128
129   /// DISubrange - This is used to represent ranges, for array bounds.
130   class DISubrange : public DIDescriptor {
131   public:
132     explicit DISubrange(const MDNode *N = 0) : DIDescriptor(N) {}
133
134     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
135     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
136   };
137
138   /// DIArray - This descriptor holds an array of descriptors.
139   class DIArray : public DIDescriptor {
140   public:
141     explicit DIArray(const MDNode *N = 0)
142       : DIDescriptor(N) {}
143
144     unsigned getNumElements() const;
145     DIDescriptor getElement(unsigned Idx) const {
146       return getDescriptorField(Idx);
147     }
148   };
149
150   /// DIScope - A base class for various scopes.
151   class DIScope : public DIDescriptor {
152   public:
153     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
154     virtual ~DIScope() {}
155
156     StringRef getFilename() const;
157     StringRef getDirectory() const;
158   };
159
160   /// DICompileUnit - A wrapper for a compile unit.
161   class DICompileUnit : public DIScope {
162   public:
163     explicit DICompileUnit(const MDNode *N = 0) : DIScope(N) {}
164
165     unsigned getLanguage() const   { return getUnsignedField(2); }
166     StringRef getFilename() const  { return getStringField(3);   }
167     StringRef getDirectory() const { return getStringField(4);   }
168     StringRef getProducer() const  { return getStringField(5);   }
169
170     /// isMain - Each input file is encoded as a separate compile unit in LLVM
171     /// debugging information output. However, many target specific tool chains
172     /// prefer to encode only one compile unit in an object file. In this
173     /// situation, the LLVM code generator will include  debugging information
174     /// entities in the compile unit that is marked as main compile unit. The
175     /// code generator accepts maximum one main compile unit per module. If a
176     /// module does not contain any main compile unit then the code generator
177     /// will emit multiple compile units in the output object file.
178
179     bool isMain() const                { return getUnsignedField(6) != 0; }
180     bool isOptimized() const           { return getUnsignedField(7) != 0; }
181     StringRef getFlags() const       { return getStringField(8);   }
182     unsigned getRunTimeVersion() const { return getUnsignedField(9); }
183
184     /// Verify - Verify that a compile unit is well formed.
185     bool Verify() const;
186
187     /// print - print compile unit.
188     void print(raw_ostream &OS) const;
189
190     /// dump - print compile unit to dbgs() with a newline.
191     void dump() const;
192   };
193
194   /// DIFile - This is a wrapper for a file.
195   class DIFile : public DIScope {
196   public:
197     explicit DIFile(const MDNode *N = 0) : DIScope(N) {
198       if (DbgNode && !isFile())
199         DbgNode = 0;
200     }
201     StringRef getFilename() const  { return getStringField(1);   }
202     StringRef getDirectory() const { return getStringField(2);   }
203     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
204   };
205
206   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
207   /// FIXME: it seems strange that this doesn't have either a reference to the
208   /// type/precision or a file/line pair for location info.
209   class DIEnumerator : public DIDescriptor {
210   public:
211     explicit DIEnumerator(const MDNode *N = 0) : DIDescriptor(N) {}
212
213     StringRef getName() const        { return getStringField(1); }
214     uint64_t getEnumValue() const      { return getUInt64Field(2); }
215   };
216
217   /// DIType - This is a wrapper for a type.
218   /// FIXME: Types should be factored much better so that CV qualifiers and
219   /// others do not require a huge and empty descriptor full of zeros.
220   class DIType : public DIScope {
221   public:
222   protected:
223     // This ctor is used when the Tag has already been validated by a derived
224     // ctor.
225     DIType(const MDNode *N, bool, bool) : DIScope(N) {}
226
227   public:
228
229     /// Verify - Verify that a type descriptor is well formed.
230     bool Verify() const;
231   public:
232     explicit DIType(const MDNode *N);
233     explicit DIType() {}
234     virtual ~DIType() {}
235
236     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
237     StringRef getName() const           { return getStringField(2);     }
238     DICompileUnit getCompileUnit() const{ 
239      if (getVersion() == llvm::LLVMDebugVersion7)
240        return getFieldAs<DICompileUnit>(3);
241      
242      return getFieldAs<DIFile>(3).getCompileUnit();
243     }
244     DIFile getFile() const              { return getFieldAs<DIFile>(3); }
245     unsigned getLineNumber() const      { return getUnsignedField(4); }
246     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
247     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
248     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
249     // carry this is just plain insane.
250     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
251     unsigned getFlags() const           { return getUnsignedField(8); }
252     bool isPrivate() const {
253       return (getFlags() & FlagPrivate) != 0;
254     }
255     bool isProtected() const {
256       return (getFlags() & FlagProtected) != 0;
257     }
258     bool isForwardDecl() const {
259       return (getFlags() & FlagFwdDecl) != 0;
260     }
261     // isAppleBlock - Return true if this is the Apple Blocks extension.
262     bool isAppleBlockExtension() const {
263       return (getFlags() & FlagAppleBlock) != 0;
264     }
265     bool isBlockByrefStruct() const {
266       return (getFlags() & FlagBlockByrefStruct) != 0;
267     }
268     bool isVirtual() const {
269       return (getFlags() & FlagVirtual) != 0;
270     }
271     bool isArtificial() const {
272       return (getFlags() & FlagArtificial) != 0;
273     }
274     bool isValid() const {
275       return DbgNode && (isBasicType() || isDerivedType() || isCompositeType());
276     }
277     StringRef getDirectory() const  { 
278       if (getVersion() == llvm::LLVMDebugVersion7)
279         return getCompileUnit().getDirectory();
280
281       return getFieldAs<DIFile>(3).getDirectory();
282     }
283     StringRef getFilename() const  { 
284       if (getVersion() == llvm::LLVMDebugVersion7)
285         return getCompileUnit().getFilename();
286
287       return getFieldAs<DIFile>(3).getFilename();
288     }
289
290     /// replaceAllUsesWith - Replace all uses of debug info referenced by
291     /// this descriptor.
292     void replaceAllUsesWith(DIDescriptor &D);
293     void replaceAllUsesWith(MDNode *D);
294
295     /// print - print type.
296     void print(raw_ostream &OS) const;
297
298     /// dump - print type to dbgs() with a newline.
299     void dump() const;
300   };
301
302   /// DIBasicType - A basic type, like 'int' or 'float'.
303   class DIBasicType : public DIType {
304   public:
305     explicit DIBasicType(const MDNode *N = 0) : DIType(N) {}
306
307     unsigned getEncoding() const { return getUnsignedField(9); }
308
309     /// Verify - Verify that a basic type descriptor is well formed.
310     bool Verify() const;
311
312     /// print - print basic type.
313     void print(raw_ostream &OS) const;
314
315     /// dump - print basic type to dbgs() with a newline.
316     void dump() const;
317   };
318
319   /// DIDerivedType - A simple derived type, like a const qualified type,
320   /// a typedef, a pointer or reference, etc.
321   class DIDerivedType : public DIType {
322   protected:
323     explicit DIDerivedType(const MDNode *N, bool, bool)
324       : DIType(N, true, true) {}
325   public:
326     explicit DIDerivedType(const MDNode *N = 0)
327       : DIType(N, true, true) {}
328
329     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
330
331     /// getOriginalTypeSize - If this type is derived from a base type then
332     /// return base type size.
333     uint64_t getOriginalTypeSize() const;
334
335     /// Verify - Verify that a derived type descriptor is well formed.
336     bool Verify() const;
337
338     /// print - print derived type.
339     void print(raw_ostream &OS) const;
340
341     /// dump - print derived type to dbgs() with a newline.
342     void dump() const;
343   };
344
345   /// DICompositeType - This descriptor holds a type that can refer to multiple
346   /// other types, like a function or struct.
347   /// FIXME: Why is this a DIDerivedType??
348   class DICompositeType : public DIDerivedType {
349   public:
350     explicit DICompositeType(const MDNode *N = 0)
351       : DIDerivedType(N, true, true) {
352       if (N && !isCompositeType())
353         DbgNode = 0;
354     }
355
356     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
357     unsigned getRunTimeLang() const { return getUnsignedField(11); }
358     DICompositeType getContainingType() const {
359       return getFieldAs<DICompositeType>(12);
360     }
361     DIArray getTemplateParams() const { return getFieldAs<DIArray>(13); }
362
363     /// Verify - Verify that a composite type descriptor is well formed.
364     bool Verify() const;
365
366     /// print - print composite type.
367     void print(raw_ostream &OS) const;
368
369     /// dump - print composite type to dbgs() with a newline.
370     void dump() const;
371   };
372
373   /// DITemplateTypeParameter - This is a wrapper for template type parameter.
374   class DITemplateTypeParameter : public DIDescriptor {
375   public:
376     explicit DITemplateTypeParameter(const MDNode *N = 0) : DIDescriptor(N) {}
377
378     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
379     StringRef getName() const        { return getStringField(2); }
380     DIType getType() const           { return getFieldAs<DIType>(3); }
381     StringRef getFilename() const    { 
382       return getFieldAs<DIFile>(4).getFilename();
383     }
384     StringRef getDirectory() const   { 
385       return getFieldAs<DIFile>(4).getDirectory();
386     }
387     unsigned getLineNumber() const   { return getUnsignedField(5); }
388     unsigned getColumnNumber() const { return getUnsignedField(6); }
389   };
390
391   /// DITemplateValueParameter - This is a wrapper for template value parameter.
392   class DITemplateValueParameter : public DIDescriptor {
393   public:
394     explicit DITemplateValueParameter(const MDNode *N = 0) : DIDescriptor(N) {}
395
396     DIScope getContext() const       { return getFieldAs<DIScope>(1); }
397     StringRef getName() const        { return getStringField(2); }
398     DIType getType() const           { return getFieldAs<DIType>(3); }
399     uint64_t getValue() const         { return getUInt64Field(4); }
400     StringRef getFilename() const    { 
401       return getFieldAs<DIFile>(5).getFilename();
402     }
403     StringRef getDirectory() const   { 
404       return getFieldAs<DIFile>(5).getDirectory();
405     }
406     unsigned getLineNumber() const   { return getUnsignedField(6); }
407     unsigned getColumnNumber() const { return getUnsignedField(7); }
408   };
409
410   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
411   class DISubprogram : public DIScope {
412   public:
413     explicit DISubprogram(const MDNode *N = 0) : DIScope(N) {}
414
415     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
416     StringRef getName() const         { return getStringField(3); }
417     StringRef getDisplayName() const  { return getStringField(4); }
418     StringRef getLinkageName() const  { return getStringField(5); }
419     DICompileUnit getCompileUnit() const{ 
420       if (getVersion() == llvm::LLVMDebugVersion7)
421         return getFieldAs<DICompileUnit>(6);
422
423       return getFieldAs<DIFile>(6).getCompileUnit(); 
424     }
425     unsigned getLineNumber() const      { return getUnsignedField(7); }
426     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
427
428     /// getReturnTypeName - Subprogram return types are encoded either as
429     /// DIType or as DICompositeType.
430     StringRef getReturnTypeName() const {
431       DICompositeType DCT(getFieldAs<DICompositeType>(8));
432       if (DCT.Verify()) {
433         DIArray A = DCT.getTypeArray();
434         DIType T(A.getElement(0));
435         return T.getName();
436       }
437       DIType T(getFieldAs<DIType>(8));
438       return T.getName();
439     }
440
441     /// isLocalToUnit - Return true if this subprogram is local to the current
442     /// compile unit, like 'static' in C.
443     unsigned isLocalToUnit() const     { return getUnsignedField(9); }
444     unsigned isDefinition() const      { return getUnsignedField(10); }
445
446     unsigned getVirtuality() const { return getUnsignedField(11); }
447     unsigned getVirtualIndex() const { return getUnsignedField(12); }
448
449     DICompositeType getContainingType() const {
450       return getFieldAs<DICompositeType>(13);
451     }
452     unsigned isArtificial() const    { 
453       if (getVersion() <= llvm::LLVMDebugVersion8)
454         return getUnsignedField(14); 
455       return (getUnsignedField(14) & FlagArtificial) != 0;
456     }
457     /// isPrivate - Return true if this subprogram has "private"
458     /// access specifier.
459     bool isPrivate() const    { 
460       if (getVersion() <= llvm::LLVMDebugVersion8)
461         return false;
462       return (getUnsignedField(14) & FlagPrivate) != 0;
463     }
464     /// isProtected - Return true if this subprogram has "protected"
465     /// access specifier.
466     bool isProtected() const    { 
467       if (getVersion() <= llvm::LLVMDebugVersion8)
468         return false;
469       return (getUnsignedField(14) & FlagProtected) != 0;
470     }
471     /// isExplicit - Return true if this subprogram is marked as explicit.
472     bool isExplicit() const    { 
473       if (getVersion() <= llvm::LLVMDebugVersion8)
474         return false;
475       return (getUnsignedField(14) & FlagExplicit) != 0;
476     }
477     /// isPrototyped - Return true if this subprogram is prototyped.
478     bool isPrototyped() const    { 
479       if (getVersion() <= llvm::LLVMDebugVersion8)
480         return false;
481       return (getUnsignedField(14) & FlagPrototyped) != 0;
482     }
483
484     unsigned isOptimized() const;
485
486     StringRef getFilename() const    { 
487       if (getVersion() == llvm::LLVMDebugVersion7)
488         return getCompileUnit().getFilename();
489
490       return getFieldAs<DIFile>(6).getFilename(); 
491     }
492
493     StringRef getDirectory() const   { 
494       if (getVersion() == llvm::LLVMDebugVersion7)
495         return getCompileUnit().getFilename();
496
497       return getFieldAs<DIFile>(6).getDirectory(); 
498     }
499
500     /// Verify - Verify that a subprogram descriptor is well formed.
501     bool Verify() const;
502
503     /// print - print subprogram.
504     void print(raw_ostream &OS) const;
505
506     /// dump - print subprogram to dbgs() with a newline.
507     void dump() const;
508
509     /// describes - Return true if this subprogram provides debugging
510     /// information for the function F.
511     bool describes(const Function *F);
512
513     Function *getFunction() const { return getFunctionField(16); }
514   };
515
516   /// DIGlobalVariable - This is a wrapper for a global variable.
517   class DIGlobalVariable : public DIDescriptor {
518   public:
519     explicit DIGlobalVariable(const MDNode *N = 0) : DIDescriptor(N) {}
520
521     DIScope getContext() const          { return getFieldAs<DIScope>(2); }
522     StringRef getName() const         { return getStringField(3); }
523     StringRef getDisplayName() const  { return getStringField(4); }
524     StringRef getLinkageName() const  { return getStringField(5); }
525     DICompileUnit getCompileUnit() const{ 
526       if (getVersion() == llvm::LLVMDebugVersion7)
527         return getFieldAs<DICompileUnit>(6);
528
529       DIFile F = getFieldAs<DIFile>(6); 
530       return F.getCompileUnit();
531     }
532
533     unsigned getLineNumber() const      { return getUnsignedField(7); }
534     DIType getType() const              { return getFieldAs<DIType>(8); }
535     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
536     unsigned isDefinition() const       { return getUnsignedField(10); }
537
538     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
539     Constant *getConstant() const   { return getConstantField(11); }
540
541     /// Verify - Verify that a global variable descriptor is well formed.
542     bool Verify() const;
543
544     /// print - print global variable.
545     void print(raw_ostream &OS) const;
546
547     /// dump - print global variable to dbgs() with a newline.
548     void dump() const;
549   };
550
551   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
552   /// global etc).
553   class DIVariable : public DIDescriptor {
554   public:
555     explicit DIVariable(const MDNode *N = 0)
556       : DIDescriptor(N) {}
557
558     DIScope getContext() const          { return getFieldAs<DIScope>(1); }
559     StringRef getName() const           { return getStringField(2);     }
560     DICompileUnit getCompileUnit() const{ 
561       if (getVersion() == llvm::LLVMDebugVersion7)
562         return getFieldAs<DICompileUnit>(3);
563
564       DIFile F = getFieldAs<DIFile>(3); 
565       return F.getCompileUnit();
566     }
567     unsigned getLineNumber() const      { return getUnsignedField(4); }
568     DIType getType() const              { return getFieldAs<DIType>(5); }
569     
570     /// isArtificial - Return true if this variable is marked as "artificial".
571     bool isArtificial() const    { 
572       if (getVersion() <= llvm::LLVMDebugVersion8)
573         return false;
574       return (getUnsignedField(6) & FlagArtificial) != 0;
575     }
576
577
578     /// Verify - Verify that a variable descriptor is well formed.
579     bool Verify() const;
580
581     /// HasComplexAddr - Return true if the variable has a complex address.
582     bool hasComplexAddress() const {
583       return getNumAddrElements() > 0;
584     }
585
586     unsigned getNumAddrElements() const;
587     
588     uint64_t getAddrElement(unsigned Idx) const {
589       return getUInt64Field(Idx+6);
590     }
591
592     /// isBlockByrefVariable - Return true if the variable was declared as
593     /// a "__block" variable (Apple Blocks).
594     bool isBlockByrefVariable() const {
595       return getType().isBlockByrefStruct();
596     }
597
598     /// isInlinedFnArgument - Return trule if this variable provides debugging
599     /// information for an inlined function arguments.
600     bool isInlinedFnArgument(const Function *CurFn);
601
602     /// print - print variable.
603     void print(raw_ostream &OS) const;
604
605     /// dump - print variable to dbgs() with a newline.
606     void dump() const;
607   };
608
609   /// DILexicalBlock - This is a wrapper for a lexical block.
610   class DILexicalBlock : public DIScope {
611   public:
612     explicit DILexicalBlock(const MDNode *N = 0) : DIScope(N) {}
613     DIScope getContext() const       { return getFieldAs<DIScope>(1);      }
614     unsigned getLineNumber() const   { return getUnsignedField(2);         }
615     unsigned getColumnNumber() const { return getUnsignedField(3);         }
616     StringRef getDirectory() const {
617       StringRef dir = getFieldAs<DIFile>(4).getDirectory();
618       return !dir.empty() ? dir : getContext().getDirectory();
619     }
620     StringRef getFilename() const {
621       StringRef filename = getFieldAs<DIFile>(4).getFilename();
622       return !filename.empty() ? filename : getContext().getFilename();
623     }
624   };
625
626   /// DINameSpace - A wrapper for a C++ style name space.
627   class DINameSpace : public DIScope { 
628   public:
629     explicit DINameSpace(const MDNode *N = 0) : DIScope(N) {}
630     DIScope getContext() const     { return getFieldAs<DIScope>(1);      }
631     StringRef getName() const      { return getStringField(2);           }
632     StringRef getDirectory() const  { 
633       return getFieldAs<DIFile>(3).getDirectory();
634     }
635     StringRef getFilename() const  { 
636       return getFieldAs<DIFile>(3).getFilename();
637     }
638     DICompileUnit getCompileUnit() const{ 
639       if (getVersion() == llvm::LLVMDebugVersion7)
640         return getFieldAs<DICompileUnit>(3);
641
642       return getFieldAs<DIFile>(3).getCompileUnit(); 
643     }
644     unsigned getLineNumber() const { return getUnsignedField(4);         }
645     bool Verify() const;
646   };
647
648   /// DILocation - This object holds location information. This object
649   /// is not associated with any DWARF tag.
650   class DILocation : public DIDescriptor {
651   public:
652     explicit DILocation(const MDNode *N) : DIDescriptor(N) { }
653
654     unsigned getLineNumber() const     { return getUnsignedField(0); }
655     unsigned getColumnNumber() const   { return getUnsignedField(1); }
656     DIScope  getScope() const          { return getFieldAs<DIScope>(2); }
657     DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
658     StringRef getFilename() const    { return getScope().getFilename(); }
659     StringRef getDirectory() const   { return getScope().getDirectory(); }
660     bool Verify() const;
661   };
662
663   /// DIFactory - This object assists with the construction of the various
664   /// descriptors.
665   class DIFactory {
666     Module &M;
667     LLVMContext& VMContext;
668
669     Function *DeclareFn;     // llvm.dbg.declare
670     Function *ValueFn;       // llvm.dbg.value
671
672     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
673     void operator=(const DIFactory&); // DO NOT IMPLEMENT
674   public:
675     enum ComplexAddrKind { OpPlus=1, OpDeref };
676
677     explicit DIFactory(Module &m);
678
679     /// GetOrCreateArray - Create an descriptor for an array of descriptors.
680     /// This implicitly uniques the arrays created.
681     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
682
683     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
684     /// implicitly uniques the values returned.
685     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
686
687     /// CreateUnspecifiedParameter - Create unspeicified type descriptor
688     /// for a subroutine type.
689     DIDescriptor CreateUnspecifiedParameter();
690
691     /// CreateCompileUnit - Create a new descriptor for the specified compile
692     /// unit.
693     DICompileUnit CreateCompileUnit(unsigned LangID,
694                                     StringRef Filename,
695                                     StringRef Directory,
696                                     StringRef Producer,
697                                     bool isMain = false,
698                                     bool isOptimized = false,
699                                     StringRef Flags = "",
700                                     unsigned RunTimeVer = 0);
701
702     /// CreateFile -  Create a new descriptor for the specified file.
703     DIFile CreateFile(StringRef Filename, StringRef Directory,
704                       DICompileUnit CU);
705
706     /// CreateEnumerator - Create a single enumerator value.
707     DIEnumerator CreateEnumerator(StringRef Name, uint64_t Val);
708
709     /// CreateBasicType - Create a basic type like int, float, etc.
710     DIBasicType CreateBasicType(DIDescriptor Context, StringRef Name,
711                                 DIFile F, unsigned LineNumber,
712                                 uint64_t SizeInBits, uint64_t AlignInBits,
713                                 uint64_t OffsetInBits, unsigned Flags,
714                                 unsigned Encoding);
715
716     /// CreateBasicType - Create a basic type like int, float, etc.
717     DIBasicType CreateBasicTypeEx(DIDescriptor Context, StringRef Name,
718                                 DIFile F, unsigned LineNumber,
719                                 Constant *SizeInBits, Constant *AlignInBits,
720                                 Constant *OffsetInBits, unsigned Flags,
721                                 unsigned Encoding);
722
723     /// CreateDerivedType - Create a derived type like const qualified type,
724     /// pointer, typedef, etc.
725     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
726                                     StringRef Name,
727                                     DIFile F,
728                                     unsigned LineNumber,
729                                     uint64_t SizeInBits, uint64_t AlignInBits,
730                                     uint64_t OffsetInBits, unsigned Flags,
731                                     DIType DerivedFrom);
732
733     /// CreateDerivedType - Create a derived type like const qualified type,
734     /// pointer, typedef, etc.
735     DIDerivedType CreateDerivedTypeEx(unsigned Tag, DIDescriptor Context,
736                                       StringRef Name,
737                                       DIFile F,
738                                       unsigned LineNumber,
739                                       Constant *SizeInBits, 
740                                       Constant *AlignInBits,
741                                       Constant *OffsetInBits, unsigned Flags,
742                                       DIType DerivedFrom);
743
744     /// CreateCompositeType - Create a composite type like array, struct, etc.
745     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
746                                         StringRef Name,
747                                         DIFile F,
748                                         unsigned LineNumber,
749                                         uint64_t SizeInBits,
750                                         uint64_t AlignInBits,
751                                         uint64_t OffsetInBits, unsigned Flags,
752                                         DIType DerivedFrom,
753                                         DIArray Elements,
754                                         unsigned RunTimeLang = 0,
755                                         MDNode *ContainingType = 0);
756
757     /// CreateTemporaryType - Create a temporary forward-declared type.
758     DIType CreateTemporaryType();
759     DIType CreateTemporaryType(DIFile F);
760
761     /// CreateArtificialType - Create a new DIType with "artificial" flag set.
762     DIType CreateArtificialType(DIType Ty);
763
764     /// CreateCompositeType - Create a composite type like array, struct, etc.
765     DICompositeType CreateCompositeTypeEx(unsigned Tag, DIDescriptor Context,
766                                           StringRef Name,
767                                           DIFile F,
768                                           unsigned LineNumber,
769                                           Constant *SizeInBits,
770                                           Constant *AlignInBits,
771                                           Constant *OffsetInBits, 
772                                           unsigned Flags,
773                                           DIType DerivedFrom,
774                                           DIArray Elements,
775                                           unsigned RunTimeLang = 0,
776                                           MDNode *ContainingType = 0);
777
778     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
779     /// See comments in DISubprogram for descriptions of these fields.
780     DISubprogram CreateSubprogram(DIDescriptor Context, StringRef Name,
781                                   StringRef DisplayName,
782                                   StringRef LinkageName,
783                                   DIFile F, unsigned LineNo,
784                                   DIType Ty, bool isLocalToUnit,
785                                   bool isDefinition,
786                                   unsigned VK = 0,
787                                   unsigned VIndex = 0,
788                                   DIType ContainingType = DIType(),
789                                   unsigned Flags = 0,
790                                   bool isOptimized = false,
791                                   Function *Fn = 0);
792
793     /// CreateSubprogramDefinition - Create new subprogram descriptor for the
794     /// given declaration. 
795     DISubprogram CreateSubprogramDefinition(DISubprogram &SPDeclaration);
796
797     /// CreateGlobalVariable - Create a new descriptor for the specified global.
798     DIGlobalVariable
799     CreateGlobalVariable(DIDescriptor Context, StringRef Name,
800                          StringRef DisplayName,
801                          StringRef LinkageName,
802                          DIFile F,
803                          unsigned LineNo, DIType Ty, bool isLocalToUnit,
804                          bool isDefinition, llvm::GlobalVariable *GV);
805
806     /// CreateGlobalVariable - Create a new descriptor for the specified constant.
807     DIGlobalVariable
808     CreateGlobalVariable(DIDescriptor Context, StringRef Name,
809                          StringRef DisplayName,
810                          StringRef LinkageName,
811                          DIFile F,
812                          unsigned LineNo, DIType Ty, bool isLocalToUnit,
813                          bool isDefinition, llvm::Constant *C);
814
815     /// CreateVariable - Create a new descriptor for the specified variable.
816     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
817                               StringRef Name,
818                               DIFile F, unsigned LineNo,
819                               DIType Ty, bool AlwaysPreserve = false,
820                               unsigned Flags = 0);
821
822     /// CreateComplexVariable - Create a new descriptor for the specified
823     /// variable which has a complex address expression for its address.
824     DIVariable CreateComplexVariable(unsigned Tag, DIDescriptor Context,
825                                      StringRef Name, DIFile F, unsigned LineNo,
826                                      DIType Ty, Value *const *Addr,
827                                      unsigned NumAddr);
828
829     /// CreateLexicalBlock - This creates a descriptor for a lexical block
830     /// with the specified parent context.
831     DILexicalBlock CreateLexicalBlock(DIDescriptor Context, DIFile F,
832                                       unsigned Line = 0, unsigned Col = 0);
833
834     /// CreateNameSpace - This creates new descriptor for a namespace
835     /// with the specified parent context.
836     DINameSpace CreateNameSpace(DIDescriptor Context, StringRef Name,
837                                 DIFile F, unsigned LineNo);
838
839     /// CreateLocation - Creates a debug info location.
840     DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
841                               DIScope S, DILocation OrigLoc);
842
843     /// CreateLocation - Creates a debug info location.
844     DILocation CreateLocation(unsigned LineNo, unsigned ColumnNo,
845                               DIScope S, MDNode *OrigLoc = 0);
846
847     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
848     Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
849                                BasicBlock *InsertAtEnd);
850
851     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
852     Instruction *InsertDeclare(llvm::Value *Storage, DIVariable D,
853                                Instruction *InsertBefore);
854
855     /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
856     Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
857                                          DIVariable D, BasicBlock *InsertAtEnd);
858
859     /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
860     Instruction *InsertDbgValueIntrinsic(llvm::Value *V, uint64_t Offset,
861                                        DIVariable D, Instruction *InsertBefore);
862
863     // RecordType - Record DIType in a module such that it is not lost even if
864     // it is not referenced through debug info anchors.
865     void RecordType(DIType T);
866
867   private:
868     Constant *GetTagConstant(unsigned TAG);
869   };
870
871   /// getDISubprogram - Find subprogram that is enclosing this scope.
872   DISubprogram getDISubprogram(const MDNode *Scope);
873
874   /// getDICompositeType - Find underlying composite type.
875   DICompositeType getDICompositeType(DIType T);
876
877   /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
878   /// to hold function specific information.
879   NamedMDNode *getOrInsertFnSpecificMDNode(Module &M, StringRef Name);
880
881   /// getFnSpecificMDNode - Return a NameMDNode, if available, that is 
882   /// suitable to hold function specific information.
883   NamedMDNode *getFnSpecificMDNode(const Module &M, StringRef Name);
884
885   class DebugInfoFinder {
886   public:
887     /// processModule - Process entire module and collect debug info
888     /// anchors.
889     void processModule(Module &M);
890
891   private:
892     /// processType - Process DIType.
893     void processType(DIType DT);
894
895     /// processLexicalBlock - Process DILexicalBlock.
896     void processLexicalBlock(DILexicalBlock LB);
897
898     /// processSubprogram - Process DISubprogram.
899     void processSubprogram(DISubprogram SP);
900
901     /// processDeclare - Process DbgDeclareInst.
902     void processDeclare(DbgDeclareInst *DDI);
903
904     /// processLocation - Process DILocation.
905     void processLocation(DILocation Loc);
906
907     /// addCompileUnit - Add compile unit into CUs.
908     bool addCompileUnit(DICompileUnit CU);
909
910     /// addGlobalVariable - Add global variable into GVs.
911     bool addGlobalVariable(DIGlobalVariable DIG);
912
913     // addSubprogram - Add subprgoram into SPs.
914     bool addSubprogram(DISubprogram SP);
915
916     /// addType - Add type into Tys.
917     bool addType(DIType DT);
918
919   public:
920     typedef SmallVector<MDNode *, 8>::const_iterator iterator;
921     iterator compile_unit_begin()    const { return CUs.begin(); }
922     iterator compile_unit_end()      const { return CUs.end(); }
923     iterator subprogram_begin()      const { return SPs.begin(); }
924     iterator subprogram_end()        const { return SPs.end(); }
925     iterator global_variable_begin() const { return GVs.begin(); }
926     iterator global_variable_end()   const { return GVs.end(); }
927     iterator type_begin()            const { return TYs.begin(); }
928     iterator type_end()              const { return TYs.end(); }
929
930     unsigned compile_unit_count()    const { return CUs.size(); }
931     unsigned global_variable_count() const { return GVs.size(); }
932     unsigned subprogram_count()      const { return SPs.size(); }
933     unsigned type_count()            const { return TYs.size(); }
934
935   private:
936     SmallVector<MDNode *, 8> CUs;  // Compile Units
937     SmallVector<MDNode *, 8> SPs;  // Subprograms
938     SmallVector<MDNode *, 8> GVs;  // Global Variables;
939     SmallVector<MDNode *, 8> TYs;  // Types
940     SmallPtrSet<MDNode *, 64> NodesSeen;
941   };
942 } // end namespace llvm
943
944 #endif