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