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