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