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