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