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