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