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