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