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