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