Basic array support.
[oota-llvm.git] / include / llvm / CodeGen / MachineDebugInfo.h
1 //===-- llvm/CodeGen/MachineDebugInfo.h -------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collect debug information for a module.  This information should be in a
11 // neutral form that can be used by different debugging schemes.
12 //
13 // The organization of information is primarily clustered around the source
14 // compile units.  The main exception is source line correspondence where
15 // inlining may interleave code from various compile units.
16 //
17 // The following information can be retrieved from the MachineDebugInfo.
18 //
19 //  -- Source directories - Directories are uniqued based on their canonical
20 //     string and assigned a sequential numeric ID (base 1.)
21 //  -- Source files - Files are also uniqued based on their name and directory
22 //     ID.  A file ID is sequential number (base 1.)
23 //  -- Source line coorespondence - A vector of file ID, line#, column# triples.
24 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
25 //     corresponding to each entry in the source line list.  This allows a debug
26 //     emitter to generate labels referenced by debug information tables.
27 //
28 //===----------------------------------------------------------------------===//
29
30 #ifndef LLVM_CODEGEN_MACHINEDEBUGINFO_H
31 #define LLVM_CODEGEN_MACHINEDEBUGINFO_H
32
33 #include "llvm/Support/Dwarf.h"
34 #include "llvm/Support/DataTypes.h"
35 #include "llvm/ADT/UniqueVector.h"
36 #include "llvm/GlobalValue.h"
37 #include "llvm/Pass.h"
38 #include "llvm/User.h"
39
40 #include <string>
41 #include <set>
42
43 namespace llvm {
44
45 //===----------------------------------------------------------------------===//
46 // Forward declarations.
47 class Constant;
48 class DebugInfoDesc;
49 class GlobalVariable;
50 class Module;
51 class PointerType;
52 class StructType;
53
54 //===----------------------------------------------------------------------===//
55 // Debug info constants.
56
57 enum {
58   LLVMDebugVersion = 1,                 // Current version of debug information.
59   DIInvalid = ~0U,                      // Invalid result indicator.
60   
61   // DebugInfoDesc type identifying tags.
62   DI_TAG_anchor = 0,
63   DI_TAG_compile_unit,
64   DI_TAG_global_variable,
65   DI_TAG_subprogram,
66   DI_TAG_basictype,
67   DI_TAG_typedef,
68   DI_TAG_pointer,
69   DI_TAG_reference,
70   DI_TAG_array,
71   DI_TAG_struct,
72   DI_TAG_union,
73   DI_TAG_enum,
74   DI_TAG_subrange,
75   DI_TAG_const,
76   DI_TAG_volatile,
77   DI_TAG_restrict
78 };
79
80 //===----------------------------------------------------------------------===//
81 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
82 /// the supplied DebugInfoDesc.
83 class DIVisitor {
84 public:
85   DIVisitor() {}
86   virtual ~DIVisitor() {}
87
88   /// ApplyToFields - Target the visitor to each field of the debug information
89   /// descriptor.
90   void ApplyToFields(DebugInfoDesc *DD);
91   
92   /// Apply - Subclasses override each of these methods to perform the
93   /// appropriate action for the type of field.
94   virtual void Apply(int &Field) = 0;
95   virtual void Apply(unsigned &Field) = 0;
96   virtual void Apply(int64_t &Field) = 0;
97   virtual void Apply(uint64_t &Field) = 0;
98   virtual void Apply(bool &Field) = 0;
99   virtual void Apply(std::string &Field) = 0;
100   virtual void Apply(DebugInfoDesc *&Field) = 0;
101   virtual void Apply(GlobalVariable *&Field) = 0;
102   virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
103 };
104
105 //===----------------------------------------------------------------------===//
106 /// DebugInfoDesc - This class is the base class for debug info descriptors.
107 ///
108 class DebugInfoDesc {
109 private:
110   unsigned Tag;                         // Content indicator.  Dwarf values are
111                                         // used but that does not limit use to
112                                         // Dwarf writers.
113   
114 protected:
115   DebugInfoDesc(unsigned T) : Tag(T) {}
116   
117 public:
118   virtual ~DebugInfoDesc() {}
119
120   // Accessors
121   unsigned getTag()          const { return Tag; }
122   
123   /// TagFromGlobal - Returns the Tag number from a debug info descriptor
124   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
125   static unsigned TagFromGlobal(GlobalVariable *GV);
126
127   /// DescFactory - Create an instance of debug info descriptor based on Tag.
128   /// Return NULL if not a recognized Tag.
129   static DebugInfoDesc *DescFactory(unsigned Tag);
130   
131   /// getLinkage - get linkage appropriate for this type of descriptor.
132   ///
133   virtual GlobalValue::LinkageTypes getLinkage() const;
134     
135   //===--------------------------------------------------------------------===//
136   // Subclasses should supply the following static methods.
137   
138   // Implement isa/cast/dyncast.
139   static bool classof(const DebugInfoDesc *) { return true; }
140   
141   //===--------------------------------------------------------------------===//
142   // Subclasses should supply the following virtual methods.
143   
144   /// ApplyToFields - Target the vistor to the fields of the descriptor.
145   ///
146   virtual void ApplyToFields(DIVisitor *Visitor);
147
148   /// getDescString - Return a string used to compose global names and labels.
149   ///
150   virtual const char *getDescString() const = 0;
151   
152   /// getTypeString - Return a string used to label this descriptor's type.
153   ///
154   virtual const char *getTypeString() const = 0;
155   
156 #ifndef NDEBUG
157   virtual void dump() = 0;
158 #endif
159 };
160
161 //===----------------------------------------------------------------------===//
162 /// AnchorDesc - Descriptors of this class act as markers for identifying
163 /// descriptors of certain groups.
164 class AnchorDesc : public DebugInfoDesc {
165 private:  
166   std::string Name;                     // Anchor type string.
167   
168 public:
169   AnchorDesc()
170   : DebugInfoDesc(DI_TAG_anchor)
171   , Name("")
172   {}
173   AnchorDesc(const std::string &N)
174   : DebugInfoDesc(DI_TAG_anchor)
175   , Name(N)
176   {}
177   
178   // Accessors
179   const std::string &getName() const { return Name; }
180
181   // Implement isa/cast/dyncast.
182   static bool classof(const AnchorDesc *) { return true; }
183   static bool classof(const DebugInfoDesc *D) {
184     return D->getTag() == DI_TAG_anchor;
185   }
186
187   /// getLinkage - get linkage appropriate for this type of descriptor.
188   ///
189   virtual GlobalValue::LinkageTypes getLinkage() const;
190
191   /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
192   ///
193   virtual void ApplyToFields(DIVisitor *Visitor);
194
195   /// getDescString - Return a string used to compose global names and labels.
196   ///
197   virtual const char *getDescString() const;
198     
199   /// getTypeString - Return a string used to label this descriptor's type.
200   ///
201   virtual const char *getTypeString() const;
202     
203 #ifndef NDEBUG
204   virtual void dump();
205 #endif
206 };
207
208 //===----------------------------------------------------------------------===//
209 /// AnchoredDesc - This class manages anchors for a variety of top level
210 /// descriptors.
211 class AnchoredDesc : public DebugInfoDesc {
212 private:  
213   AnchorDesc *Anchor;                   // Anchor for all descriptors of the
214                                         // same type.
215
216 protected:
217
218   AnchoredDesc(unsigned T);
219
220 public:  
221   // Accessors.
222   AnchorDesc *getAnchor() const { return Anchor; }
223   void setAnchor(AnchorDesc *A) { Anchor = A; }
224
225   //===--------------------------------------------------------------------===//
226   // Subclasses should supply the following virtual methods.
227   
228   /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
229   ///
230   virtual void ApplyToFields(DIVisitor *Visitor);
231 };
232
233 //===----------------------------------------------------------------------===//
234 /// CompileUnitDesc - This class packages debug information associated with a 
235 /// source/header file.
236 class CompileUnitDesc : public AnchoredDesc {
237 private:  
238   unsigned DebugVersion;                // LLVM debug version when produced.
239   unsigned Language;                    // Language number (ex. DW_LANG_C89.)
240   std::string FileName;                 // Source file name.
241   std::string Directory;                // Source file directory.
242   std::string Producer;                 // Compiler string.
243   
244 public:
245   CompileUnitDesc();
246   
247   // Accessors
248   unsigned getDebugVersion()              const { return DebugVersion; }
249   unsigned getLanguage()                  const { return Language; }
250   const std::string &getFileName()        const { return FileName; }
251   const std::string &getDirectory()       const { return Directory; }
252   const std::string &getProducer()        const { return Producer; }
253   void setLanguage(unsigned L)                  { Language = L; }
254   void setFileName(const std::string &FN)       { FileName = FN; }
255   void setDirectory(const std::string &D)       { Directory = D; }
256   void setProducer(const std::string &P)        { Producer = P; }
257   
258   // FIXME - Need translation unit getter/setter.
259
260   // Implement isa/cast/dyncast.
261   static bool classof(const CompileUnitDesc *) { return true; }
262   static bool classof(const DebugInfoDesc *D) {
263     return D->getTag() == DI_TAG_compile_unit;
264   }
265   
266   /// DebugVersionFromGlobal - Returns the version number from a compile unit
267   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
268   static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
269   
270   /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
271   ///
272   virtual void ApplyToFields(DIVisitor *Visitor);
273
274   /// getDescString - Return a string used to compose global names and labels.
275   ///
276   virtual const char *getDescString() const;
277     
278   /// getTypeString - Return a string used to label this descriptor's type.
279   ///
280   virtual const char *getTypeString() const;
281   
282   /// getAnchorString - Return a string used to label this descriptor's anchor.
283   ///
284   virtual const char *getAnchorString() const;
285     
286 #ifndef NDEBUG
287   virtual void dump();
288 #endif
289 };
290
291 //===----------------------------------------------------------------------===//
292 /// TypeDesc - This class packages debug information associated with a type.
293 ///
294 class TypeDesc : public DebugInfoDesc {
295 private:
296   DebugInfoDesc *Context;               // Context debug descriptor.
297   std::string Name;                     // Type name (may be empty.)
298   CompileUnitDesc *File;                // Declared compile unit (may be NULL.)
299   int Line;                             // Declared line# (may be zero.)
300   uint64_t Size;                        // Type size (may be zero.)
301
302 protected:
303   TypeDesc(unsigned T);
304
305 public:
306   // Accessors
307   DebugInfoDesc *getContext()                const { return Context; }
308   const std::string &getName()               const { return Name; }
309   CompileUnitDesc *getFile()                 const { return File; }
310   int getLine()                              const { return Line; }
311   uint64_t getSize()                         const { return Size; }
312   void setContext(DebugInfoDesc *C)                { Context = C; }
313   void setName(const std::string &N)               { Name = N; }
314   void setFile(CompileUnitDesc *U)                 { File = U; }
315   void setLine(int L)                              { Line = L; }
316   void setSize(uint64_t S)                         { Size = S; }
317   
318   /// ApplyToFields - Target the visitor to the fields of the  TypeDesc.
319   ///
320   virtual void ApplyToFields(DIVisitor *Visitor);
321
322   /// getDescString - Return a string used to compose global names and labels.
323   ///
324   virtual const char *getDescString() const;
325
326   /// getTypeString - Return a string used to label this descriptor's type.
327   ///
328   virtual const char *getTypeString() const;
329   
330 #ifndef NDEBUG
331   virtual void dump();
332 #endif
333 };
334
335 //===----------------------------------------------------------------------===//
336 /// BasicTypeDesc - This class packages debug information associated with a
337 /// basic type (eg. int, bool, double.)
338 class BasicTypeDesc : public TypeDesc {
339 private:
340   unsigned Encoding;                    // Type encoding.
341   
342 public:
343   BasicTypeDesc();
344   
345   // Accessors
346   unsigned getEncoding()                     const { return Encoding; }
347   void setEncoding(unsigned E)                     { Encoding = E; }
348
349   // Implement isa/cast/dyncast.
350   static bool classof(const BasicTypeDesc *) { return true; }
351   static bool classof(const DebugInfoDesc *D) {
352     return D->getTag() == DI_TAG_basictype;
353   }
354   
355   /// ApplyToFields - Target the visitor to the fields of the  BasicTypeDesc.
356   ///
357   virtual void ApplyToFields(DIVisitor *Visitor);
358
359   /// getDescString - Return a string used to compose global names and labels.
360   ///
361   virtual const char *getDescString() const;
362
363   /// getTypeString - Return a string used to label this descriptor's type.
364   ///
365   virtual const char *getTypeString() const;
366
367 #ifndef NDEBUG
368   virtual void dump();
369 #endif
370 };
371
372
373 //===----------------------------------------------------------------------===//
374 /// DerivedTypeDesc - This class packages debug information associated with a
375 /// derived types (eg., typedef, pointer, reference.)
376 class DerivedTypeDesc : public TypeDesc {
377 private:
378   TypeDesc *FromType;                   // Type derived from.
379
380 public:
381   DerivedTypeDesc(unsigned T);
382   
383   // Accessors
384   TypeDesc *getFromType()                    const { return FromType; }
385   void setFromType(TypeDesc *F)                    { FromType = F; }
386
387   // Implement isa/cast/dyncast.
388   static bool classof(const DerivedTypeDesc *) { return true; }
389   static bool classof(const DebugInfoDesc *D) {
390     unsigned T =  D->getTag();
391     switch (T) {
392     case DI_TAG_typedef:
393     case DI_TAG_pointer:
394     case DI_TAG_reference:
395     case DI_TAG_const:
396     case DI_TAG_volatile:
397     case DI_TAG_restrict:
398       return true;
399     default: break;
400     }
401     return false;
402   }
403   
404   /// ApplyToFields - Target the visitor to the fields of the  DerivedTypeDesc.
405   ///
406   virtual void ApplyToFields(DIVisitor *Visitor);
407
408   /// getDescString - Return a string used to compose global names and labels.
409   ///
410   virtual const char *getDescString() const;
411
412   /// getTypeString - Return a string used to label this descriptor's type.
413   ///
414   virtual const char *getTypeString() const;
415
416 #ifndef NDEBUG
417   virtual void dump();
418 #endif
419 };
420
421 //===----------------------------------------------------------------------===//
422 /// CompositeTypeDesc - This class packages debug information associated with a
423 /// array/struct types (eg., arrays, struct, union, enums.)
424 class CompositeTypeDesc : public DerivedTypeDesc {
425 private:
426   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
427
428 public:
429   CompositeTypeDesc(unsigned T);
430   
431   // Accessors
432   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
433
434   // Implement isa/cast/dyncast.
435   static bool classof(const CompositeTypeDesc *) { return true; }
436   static bool classof(const DebugInfoDesc *D) {
437     unsigned T =  D->getTag();
438     switch (T) {
439     case DI_TAG_array:
440     case DI_TAG_struct:
441     case DI_TAG_union:
442     case DI_TAG_enum:
443       return true;
444     default: break;
445     }
446     return false;
447   }
448   
449   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
450   ///
451   virtual void ApplyToFields(DIVisitor *Visitor);
452
453   /// getDescString - Return a string used to compose global names and labels.
454   ///
455   virtual const char *getDescString() const;
456
457   /// getTypeString - Return a string used to label this descriptor's type.
458   ///
459   virtual const char *getTypeString() const;
460
461 #ifndef NDEBUG
462   virtual void dump();
463 #endif
464 };
465
466 //===----------------------------------------------------------------------===//
467 /// SubrangeDesc - This class packages debug information associated with integer
468 /// value ranges.
469 class SubrangeDesc : public DebugInfoDesc {
470 private:
471   int64_t Lo;                           // Low value of range
472   int64_t Hi;                           // High value of range
473
474 public:
475   SubrangeDesc();
476   
477   // Accessors
478   int64_t getLo()                            const { return Lo; }
479   int64_t getHi()                            const { return Hi; }
480   void setLo(int64_t L)                            { Lo = L; }
481   void setHi(int64_t H)                            { Hi = H; }
482
483   // Implement isa/cast/dyncast.
484   static bool classof(const SubrangeDesc *) { return true; }
485   static bool classof(const DebugInfoDesc *D) {
486     return D->getTag() == DI_TAG_subrange;
487   }
488   
489   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
490   ///
491   virtual void ApplyToFields(DIVisitor *Visitor);
492
493   /// getDescString - Return a string used to compose global names and labels.
494   ///
495   virtual const char *getDescString() const;
496     
497   /// getTypeString - Return a string used to label this descriptor's type.
498   ///
499   virtual const char *getTypeString() const;
500
501 #ifndef NDEBUG
502   virtual void dump();
503 #endif
504 };
505
506 //===----------------------------------------------------------------------===//
507 /// GlobalDesc - This class is the base descriptor for global functions and
508 /// variables.
509 class GlobalDesc : public AnchoredDesc {
510 private:
511   DebugInfoDesc *Context;               // Context debug descriptor.
512   std::string Name;                     // Global name.
513   TypeDesc *TyDesc;                     // Type debug descriptor.
514   bool IsStatic;                        // Is the global a static.
515   bool IsDefinition;                    // Is the global defined in context.
516   
517 protected:
518   GlobalDesc(unsigned T);
519
520 public:
521   // Accessors
522   DebugInfoDesc *getContext()                const { return Context; }
523   const std::string &getName()               const { return Name; }
524   TypeDesc *getTypeDesc()                    const { return TyDesc; }
525   bool isStatic()                            const { return IsStatic; }
526   bool isDefinition()                        const { return IsDefinition; }
527   void setContext(DebugInfoDesc *C)                { Context = C; }
528   void setName(const std::string &N)               { Name = N; }
529   void setTypeDesc(TypeDesc *T)                    { TyDesc = T; }
530   void setIsStatic(bool IS)                        { IsStatic = IS; }
531   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
532
533   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
534   ///
535   virtual void ApplyToFields(DIVisitor *Visitor);
536 };
537
538 //===----------------------------------------------------------------------===//
539 /// GlobalVariableDesc - This class packages debug information associated with a
540 /// GlobalVariable.
541 class GlobalVariableDesc : public GlobalDesc {
542 private:
543   GlobalVariable *Global;               // llvm global.
544   unsigned Line;                        // Source line number.
545   
546 public:
547   GlobalVariableDesc();
548
549   // Accessors.
550   GlobalVariable *getGlobalVariable()        const { return Global; }
551   unsigned getLine()                         const { return Line; }
552   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
553   void setLine(unsigned L)                         { Line = L; }
554  
555   // Implement isa/cast/dyncast.
556   static bool classof(const GlobalVariableDesc *) { return true; }
557   static bool classof(const DebugInfoDesc *D) {
558     return D->getTag() == DI_TAG_global_variable; 
559   }
560   
561   /// ApplyToFields - Target the visitor to the fields of the
562   /// GlobalVariableDesc.
563   virtual void ApplyToFields(DIVisitor *Visitor);
564
565   /// getDescString - Return a string used to compose global names and labels.
566   ///
567   virtual const char *getDescString() const;
568
569   /// getTypeString - Return a string used to label this descriptor's type.
570   ///
571   virtual const char *getTypeString() const;
572   
573   /// getAnchorString - Return a string used to label this descriptor's anchor.
574   ///
575   virtual const char *getAnchorString() const;
576     
577 #ifndef NDEBUG
578   virtual void dump();
579 #endif
580 };
581
582 //===----------------------------------------------------------------------===//
583 /// SubprogramDesc - This class packages debug information associated with a
584 /// subprogram/function.
585 class SubprogramDesc : public GlobalDesc {
586 private:
587   // FIXME - Other attributes
588   
589 public:
590   SubprogramDesc();
591   
592   // Accessors
593   // FIXME - Other getters/setters.
594   
595   // Implement isa/cast/dyncast.
596   static bool classof(const SubprogramDesc *) { return true; }
597   static bool classof(const DebugInfoDesc *D) {
598     return D->getTag() == DI_TAG_subprogram;
599   }
600   
601   /// ApplyToFields - Target the visitor to the fields of the  SubprogramDesc.
602   ///
603   virtual void ApplyToFields(DIVisitor *Visitor);
604
605   /// getDescString - Return a string used to compose global names and labels.
606   ///
607   virtual const char *getDescString() const;
608
609   /// getTypeString - Return a string used to label this descriptor's type.
610   ///
611   virtual const char *getTypeString() const;
612   
613   /// getAnchorString - Return a string used to label this descriptor's anchor.
614   ///
615   virtual const char *getAnchorString() const;
616     
617 #ifndef NDEBUG
618   virtual void dump();
619 #endif
620 };
621
622 //===----------------------------------------------------------------------===//
623 /// DIDeserializer - This class is responsible for casting GlobalVariables
624 /// into DebugInfoDesc objects.
625 class DIDeserializer {
626 private:
627   unsigned DebugVersion;                // Version of debug information in use.
628   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
629                                         // Previously defined gloabls.
630   
631 public:
632   DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
633   ~DIDeserializer() {}
634   
635   // Accessors
636   unsigned getDebugVersion() const { return DebugVersion; }
637   
638   /// Deserialize - Reconstitute a GlobalVariable into it's component
639   /// DebugInfoDesc objects.
640   DebugInfoDesc *Deserialize(Value *V);
641   DebugInfoDesc *Deserialize(GlobalVariable *GV);
642 };
643
644 //===----------------------------------------------------------------------===//
645 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
646 /// into GlobalVariables.
647 class DISerializer {
648 private:
649   Module *M;                            // Definition space module.
650   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
651   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
652   std::map<unsigned, StructType *> TagTypes;
653                                         // Types per Tag.  Created lazily.
654   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
655                                         // Previously defined descriptors.
656   std::map<const std::string, Constant *> StringCache;
657                                         // Previously defined strings.
658                                           
659 public:
660   DISerializer()
661   : M(NULL)
662   , StrPtrTy(NULL)
663   , EmptyStructPtrTy(NULL)
664   , TagTypes()
665   , DescGlobals()
666   , StringCache()
667   {}
668   ~DISerializer() {}
669   
670   // Accessors
671   Module *getModule()        const { return M; };
672   void setModule(Module *module)  { M = module; }
673
674   /// getStrPtrType - Return a "sbyte *" type.
675   ///
676   const PointerType *getStrPtrType();
677   
678   /// getEmptyStructPtrType - Return a "{ }*" type.
679   ///
680   const PointerType *getEmptyStructPtrType();
681   
682   /// getTagType - Return the type describing the specified descriptor (via
683   /// tag.)
684   const StructType *getTagType(DebugInfoDesc *DD);
685   
686   /// getString - Construct the string as constant string global.
687   ///
688   Constant *getString(const std::string &String);
689   
690   /// Serialize - Recursively cast the specified descriptor into a
691   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
692   GlobalVariable *Serialize(DebugInfoDesc *DD);
693 };
694
695 //===----------------------------------------------------------------------===//
696 /// DIVerifier - This class is responsible for verifying the given network of
697 /// GlobalVariables are valid as DebugInfoDesc objects.
698 class DIVerifier {
699 private:
700   unsigned DebugVersion;                // Version of debug information in use.
701   std::set<GlobalVariable *> Visited;   // Tracks visits during recursion.
702   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
703
704   /// markVisited - Return true if the GlobalVariable hase been "seen" before.
705   /// Mark markVisited otherwise.
706   bool markVisited(GlobalVariable *GV);
707   
708 public:
709   DIVerifier() : DebugVersion(LLVMDebugVersion) {}
710   ~DIVerifier() {}
711   
712   /// Verify - Return true if the GlobalVariable appears to be a valid
713   /// serialization of a DebugInfoDesc.
714   bool Verify(Value *V);
715   bool Verify(GlobalVariable *GV);
716 };
717
718 //===----------------------------------------------------------------------===//
719 /// SourceLineInfo - This class is used to record source line correspondence.
720 ///
721 class SourceLineInfo {
722 private:
723   unsigned Line;                        // Source line number.
724   unsigned Column;                      // Source column.
725   unsigned SourceID;                    // Source ID number.
726
727 public:
728   SourceLineInfo(unsigned L, unsigned C, unsigned S)
729   : Line(L), Column(C), SourceID(S) {}
730   
731   // Accessors
732   unsigned getLine()     const { return Line; }
733   unsigned getColumn()   const { return Column; }
734   unsigned getSourceID() const { return SourceID; }
735 };
736
737 //===----------------------------------------------------------------------===//
738 /// SourceFileInfo - This class is used to track source information.
739 ///
740 class SourceFileInfo {
741 private:
742   unsigned DirectoryID;                 // Directory ID number.
743   std::string Name;                     // File name (not including directory.)
744   
745 public:
746   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
747             
748   // Accessors
749   unsigned getDirectoryID()    const { return DirectoryID; }
750   const std::string &getName() const { return Name; }
751
752   /// operator== - Used by UniqueVector to locate entry.
753   ///
754   bool operator==(const SourceFileInfo &SI) const {
755     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
756   }
757
758   /// operator< - Used by UniqueVector to locate entry.
759   ///
760   bool operator<(const SourceFileInfo &SI) const {
761     return getDirectoryID() < SI.getDirectoryID() ||
762           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
763   }
764 };
765
766 //===----------------------------------------------------------------------===//
767 /// MachineDebugInfo - This class contains debug information specific to a
768 /// module.  Queries can be made by different debugging schemes and reformated
769 /// for specific use.
770 ///
771 class MachineDebugInfo : public ImmutablePass {
772 private:
773   // Use the same serializer/deserializer/verifier for the module.
774   DIDeserializer DR;
775
776   // CompileUnits - Uniquing vector for compile units.
777   UniqueVector<CompileUnitDesc *> CompileUnits;
778   
779   // Directories - Uniquing vector for directories.
780   UniqueVector<std::string> Directories;
781                                          
782   // SourceFiles - Uniquing vector for source files.
783   UniqueVector<SourceFileInfo> SourceFiles;
784
785   // Lines - List of of source line correspondence.
786   std::vector<SourceLineInfo *> Lines;
787
788 public:
789   MachineDebugInfo();
790   ~MachineDebugInfo();
791   
792   /// doInitialization - Initialize the debug state for a new module.
793   ///
794   bool doInitialization();
795   
796   /// doFinalization - Tear down the debug state after completion of a module.
797   ///
798   bool doFinalization();
799   
800   /// getDescFor - Convert a Value to a debug information descriptor.
801   ///
802   // FIXME - use new Value type when available.
803   DebugInfoDesc *getDescFor(Value *V);
804   
805   /// Verify - Verify that a Value is debug information descriptor.
806   ///
807   bool Verify(Value *V);
808   
809   /// AnalyzeModule - Scan the module for global debug information.
810   ///
811   void AnalyzeModule(Module &M);
812   
813   /// hasInfo - Returns true if valid debug info is present.
814   ///
815   bool hasInfo() const { return !CompileUnits.empty(); }
816   
817   /// RecordLabel - Records location information and associates it with a
818   /// debug label.  Returns a unique label ID used to generate a label and 
819   /// provide correspondence to the source line list.
820   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
821     Lines.push_back(new SourceLineInfo(Line, Column, Source));
822     return Lines.size();
823   }
824   
825   /// RecordSource - Register a source file with debug info. Returns an source
826   /// ID.
827   unsigned RecordSource(const std::string &Directory,
828                         const std::string &Source) {
829     unsigned DirectoryID = Directories.insert(Directory);
830     return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
831   }
832   unsigned RecordSource(const CompileUnitDesc *CompileUnit) {
833     return RecordSource(CompileUnit->getDirectory(),
834                         CompileUnit->getFileName());
835   }
836   
837   /// getDirectories - Return the UniqueVector of std::string representing
838   /// directories.
839   const UniqueVector<std::string> &getDirectories() const {
840     return Directories;
841   }
842   
843   /// getSourceFiles - Return the UniqueVector of source files. 
844   ///
845   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
846     return SourceFiles;
847   }
848   
849   /// getSourceLines - Return a vector of source lines.  Vector index + 1
850   /// equals label ID.
851   const std::vector<SourceLineInfo *> &getSourceLines() const {
852     return Lines;
853   }
854   
855   /// SetupCompileUnits - Set up the unique vector of compile units.
856   ///
857   void SetupCompileUnits(Module &M);
858
859   /// getCompileUnits - Return a vector of debug compile units.
860   ///
861   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
862   
863   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
864   /// named GlobalVariable.
865   std::vector<GlobalVariable*>
866   getGlobalVariablesUsing(Module &M, const std::string &RootName);
867
868   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
869   ///
870   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
871     T Desc;
872     std::vector<GlobalVariable *> Globals =
873                              getGlobalVariablesUsing(M, Desc.getAnchorString());
874     std::vector<T *> AnchoredDescs;
875     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
876       AnchoredDescs.push_back(cast<T>(DR.Deserialize(Globals[i])));
877     }
878
879     return AnchoredDescs;
880   }
881
882 }; // End class MachineDebugInfo
883
884 } // End llvm namespace
885
886 #endif