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