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