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