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