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