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