DebugInfo is a lightweight APIs and consumers are expected to use light objects direc...
[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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_DEBUGINFO_H
16 #define LLVM_SUPPORT_DEBUGINFO_H
17
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
21
22 namespace llvm {
23   class BasicBlock;
24   class Constant;
25   class Function;
26   class GlobalVariable;
27   class Module;
28   class Type;
29   class Value;
30   class DbgStopPointInst;
31   class DbgDeclareInst;
32   class Instruction;
33   
34   class DIDescriptor {
35   public:
36     enum {
37       Version7    = 7 << 16,     // Current version of debug information.
38       Version6    = 6 << 16,     // Constant for version 6.
39       Version5    = 5 << 16,     // Constant for version 5.
40       Version4    = 4 << 16,     // Constant for version 4.
41       VersionMask = 0xffff0000   // Mask for version number.
42     };
43     
44   protected:    
45     GlobalVariable *GV;
46     
47     /// DIDescriptor constructor.  If the specified GV is non-null, this checks
48     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
49     /// not, the debug info is corrupt and we ignore it.
50     DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
51     
52     std::string getStringField(unsigned Elt) const;
53     unsigned getUnsignedField(unsigned Elt) const {
54       return (unsigned)getUInt64Field(Elt);
55     }
56     uint64_t getUInt64Field(unsigned Elt) const;
57     DIDescriptor getDescriptorField(unsigned Elt) const;
58     
59     template <typename DescTy>
60     DescTy getFieldAs(unsigned Elt) const {
61       return DescTy(getDescriptorField(Elt).getGV());
62     }
63   
64     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
65     
66   public:
67     explicit DIDescriptor() : GV(0) {}
68     explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
69
70     bool isNull() const { return GV == 0; }
71
72     GlobalVariable *getGV() const { return GV; }
73
74     unsigned getVersion() const {
75       return getUnsignedField(0) & VersionMask;
76     }
77     
78     unsigned getTag() const {
79       return getUnsignedField(0) & ~VersionMask;
80     }
81   };
82   
83   /// DIAnchor - A wrapper for various anchor descriptors.
84   class DIAnchor : public DIDescriptor {
85   public:
86     explicit DIAnchor(GlobalVariable *GV = 0);
87     
88     unsigned getAnchorTag() const { return getUnsignedField(1); }
89   };
90
91   /// DISubrange - This is used to represent ranges, for array bounds.
92   class DISubrange : public DIDescriptor {
93   public:
94     explicit DISubrange(GlobalVariable *GV = 0);
95     
96     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
97     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
98   };
99   
100   /// DIArray - This descriptor holds an array of descriptors.
101   class DIArray : public DIDescriptor {
102   public:
103     explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
104     
105     unsigned getNumElements() const;
106     DIDescriptor getElement(unsigned Idx) const {
107       return getDescriptorField(Idx);
108     }
109   };
110   
111   /// DICompileUnit - A wrapper for a compile unit.
112   class DICompileUnit : public DIDescriptor {
113   public:
114     explicit DICompileUnit(GlobalVariable *GV = 0);
115     
116     unsigned getLanguage() const     { return getUnsignedField(2); }
117     std::string getFilename() const  { return getStringField(3); }
118     std::string getDirectory() const { return getStringField(4); }
119     std::string getProducer() const  { return getStringField(5); }
120   };
121
122   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
123   /// FIXME: it seems strange that this doesn't have either a reference to the
124   /// type/precision or a file/line pair for location info.
125   class DIEnumerator : public DIDescriptor {
126   public:
127     explicit DIEnumerator(GlobalVariable *GV = 0);
128     
129     std::string getName() const  { return getStringField(1); }
130     uint64_t getEnumValue() const { return getUInt64Field(2); }
131   };
132   
133   /// DIType - This is a wrapper for a type.
134   /// FIXME: Types should be factored much better so that CV qualifiers and
135   /// others do not require a huge and empty descriptor full of zeros.
136   class DIType : public DIDescriptor {
137   protected:
138     DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
139     // This ctor is used when the Tag has already been validated by a derived
140     // ctor.
141     DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
142
143   public:
144     /// isDerivedType - Return true if the specified tag is legal for
145     /// DIDerivedType.
146     static bool isDerivedType(unsigned TAG);
147
148     /// isCompositeType - Return true if the specified tag is legal for
149     /// DICompositeType.
150     static bool isCompositeType(unsigned TAG);
151
152     /// isBasicType - Return true if the specified tag is legal for
153     /// DIBasicType.
154     static bool isBasicType(unsigned TAG) {
155       return TAG == dwarf::DW_TAG_base_type;
156     }
157
158   public:
159     explicit DIType(GlobalVariable *GV);
160     explicit DIType() {}
161     virtual ~DIType() {}
162
163     DIDescriptor getContext() const     { return getDescriptorField(1); }
164     std::string getName() const         { return getStringField(2); }
165     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
166     unsigned getLineNumber() const      { return getUnsignedField(4); }
167     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
168     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
169     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
170     // carry this is just plain insane.
171     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
172     unsigned getFlags() const           { return getUnsignedField(8); }
173
174     virtual std::string getFilename() const { 
175       assert (0 && "Invalid DIDescriptor");
176       return "";
177     }
178
179     virtual std::string getDirectory() const { 
180       assert (0 && "Invalid DIDescriptor");
181       return "";
182     }
183   };
184   
185   /// DIBasicType - A basic type, like 'int' or 'float'.
186   class DIBasicType : public DIType {
187   public:
188     explicit DIBasicType(GlobalVariable *GV);
189     
190     unsigned getEncoding() const { return getUnsignedField(9); }
191     std::string getFilename() const { return getStringField(10); }
192     std::string getDirectory() const { return getStringField(11); }
193   };
194   
195   /// DIDerivedType - A simple derived type, like a const qualified type,
196   /// a typedef, a pointer or reference, etc.
197   class DIDerivedType : public DIType {
198   protected:
199     explicit DIDerivedType(GlobalVariable *GV, bool, bool)
200       : DIType(GV, true, true) {}
201   public:
202     explicit DIDerivedType(GlobalVariable *GV);
203     
204     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
205     std::string getFilename() const { return getStringField(10); }
206     std::string getDirectory() const { return getStringField(11); }
207   };
208
209   
210   /// DICompositeType - This descriptor holds a type that can refer to multiple
211   /// other types, like a function or struct.
212   /// FIXME: Why is this a DIDerivedType??
213   class DICompositeType : public DIDerivedType {
214   public:
215     explicit DICompositeType(GlobalVariable *GV);
216     
217     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
218     std::string getFilename() const { return getStringField(11); }
219     std::string getDirectory() const { return getStringField(12); }
220   };
221   
222   /// DIGlobal - This is a common class for global variables and subprograms.
223   class DIGlobal : public DIDescriptor {
224   protected:
225     explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
226       : DIDescriptor(GV, RequiredTag) {}
227
228     /// isSubprogram - Return true if the specified tag is legal for
229     /// DISubprogram.
230     static bool isSubprogram(unsigned TAG) {
231       return TAG == dwarf::DW_TAG_subprogram;
232     }
233
234     /// isGlobalVariable - Return true if the specified tag is legal for
235     /// DIGlobalVariable.
236     static bool isGlobalVariable(unsigned TAG) {
237       return TAG == dwarf::DW_TAG_variable;
238     }
239
240   public:
241     virtual ~DIGlobal() {}
242
243     DIDescriptor getContext() const     { return getDescriptorField(2); }
244     std::string getName() const         { return getStringField(3); }
245     std::string getDisplayName() const  { return getStringField(4); }
246     std::string getLinkageName() const  { return getStringField(5); }
247     
248     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
249     unsigned getLineNumber() const      { return getUnsignedField(7); }
250     DIType getType() const              { return getFieldAs<DIType>(8); }
251     
252     /// isLocalToUnit - Return true if this subprogram is local to the current
253     /// compile unit, like 'static' in C.
254     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
255     unsigned isDefinition() const       { return getUnsignedField(10); }
256
257     virtual std::string getFilename() const { 
258       assert (0 && "Invalid DIDescriptor");
259       return "";
260     }
261
262     virtual std::string getDirectory() const { 
263       assert (0 && "Invalid DIDescriptor");
264       return "";
265     }
266   };
267   
268   
269   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
270   class DISubprogram : public DIGlobal {
271   public:
272     explicit DISubprogram(GlobalVariable *GV = 0);
273     std::string getFilename() const { return getStringField(11); }
274     std::string getDirectory() const { return getStringField(12); }
275     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
276   };
277   
278   /// DIGlobalVariable - This is a wrapper for a global variable.
279   class DIGlobalVariable : public DIGlobal {
280   public:
281     explicit DIGlobalVariable(GlobalVariable *GV = 0);
282     
283     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
284     std::string getFilename() const { return getStringField(12); }
285     std::string getDirectory() const { return getStringField(13); }
286   };
287   
288   
289   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
290   /// global etc).
291   class DIVariable : public DIDescriptor {
292   public:
293     explicit DIVariable(GlobalVariable *GV = 0);
294     
295     DIDescriptor getContext() const { return getDescriptorField(1); }
296     std::string getName() const { return getStringField(2); }
297     
298     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
299     unsigned getLineNumber() const      { return getUnsignedField(4); }
300     DIType getType() const              { return getFieldAs<DIType>(5); }
301     std::string getFilename() const { return getStringField(6); }
302     std::string getDirectory() const { return getStringField(7); }
303     
304     /// isVariable - Return true if the specified tag is legal for DIVariable.
305     static bool isVariable(unsigned Tag);
306   };
307   
308   
309   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
310   class DIBlock : public DIDescriptor {
311   public:
312     explicit DIBlock(GlobalVariable *GV = 0);
313     
314     DIDescriptor getContext() const { return getDescriptorField(1); }
315   };
316   
317   /// DIFactory - This object assists with the construction of the various
318   /// descriptors.
319   class DIFactory {
320     Module &M;
321     // Cached values for uniquing and faster lookups.
322     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
323     const Type *EmptyStructPtr; // "{}*".
324     Function *StopPointFn;   // llvm.dbg.stoppoint
325     Function *FuncStartFn;   // llvm.dbg.func.start
326     Function *RegionStartFn; // llvm.dbg.region.start
327     Function *RegionEndFn;   // llvm.dbg.region.end
328     Function *DeclareFn;     // llvm.dbg.declare
329     StringMap<Constant*> StringCache;
330     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
331     
332     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
333     void operator=(const DIFactory&); // DO NOT IMPLEMENT
334   public:
335     explicit DIFactory(Module &m);
336     
337     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
338     /// creating a new one if there isn't already one in the module.
339     DIAnchor GetOrCreateCompileUnitAnchor();
340
341     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
342     /// creating a new one if there isn't already one in the module.
343     DIAnchor GetOrCreateSubprogramAnchor();
344
345     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
346     /// creating a new one if there isn't already one in the module.
347     DIAnchor GetOrCreateGlobalVariableAnchor();
348
349     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
350     /// This implicitly uniques the arrays created.
351     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
352
353     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
354     /// implicitly uniques the values returned.
355     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
356     
357     
358     /// CreateCompileUnit - Create a new descriptor for the specified compile
359     /// unit.
360     DICompileUnit CreateCompileUnit(unsigned LangID,
361                                     const std::string &Filename,
362                                     const std::string &Directory,
363                                     const std::string &Producer);
364
365     /// CreateEnumerator - Create a single enumerator value.
366     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
367     
368     /// CreateBasicType - Create a basic type like int, float, etc.
369     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
370                                 DICompileUnit CompileUnit, unsigned LineNumber,
371                                 uint64_t SizeInBits, uint64_t AlignInBits,
372                                 uint64_t OffsetInBits, unsigned Flags,
373                                 unsigned Encoding,
374                                 const std::string *FileName = 0,
375                                 const std::string *Directory = 0);
376  
377     /// CreateDerivedType - Create a derived type like const qualified type,
378     /// pointer, typedef, etc.
379     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
380                                     const std::string &Name,
381                                     DICompileUnit CompileUnit,
382                                     unsigned LineNumber,
383                                     uint64_t SizeInBits, uint64_t AlignInBits,
384                                     uint64_t OffsetInBits, unsigned Flags,
385                                     DIType DerivedFrom,
386                                     const std::string *FileName = 0,
387                                     const std::string *Directory = 0);
388
389     /// CreateCompositeType - Create a composite type like array, struct, etc.
390     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
391                                         const std::string &Name,
392                                         DICompileUnit CompileUnit,
393                                         unsigned LineNumber,
394                                         uint64_t SizeInBits,
395                                         uint64_t AlignInBits,
396                                         uint64_t OffsetInBits, unsigned Flags,
397                                         DIType DerivedFrom,
398                                         DIArray Elements,
399                                         const std::string *FileName = 0,
400                                         const std::string *Directory = 0);
401     
402     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
403     /// See comments in DISubprogram for descriptions of these fields.
404     DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
405                                   const std::string &DisplayName,
406                                   const std::string &LinkageName,
407                                   DICompileUnit CompileUnit, unsigned LineNo,
408                                   DIType Type, bool isLocalToUnit,
409                                   bool isDefinition,
410                                   const std::string *FileName = 0,
411                                   const std::string *Directory = 0);
412
413     /// CreateGlobalVariable - Create a new descriptor for the specified global.
414     DIGlobalVariable
415     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
416                          const std::string &DisplayName,
417                          const std::string &LinkageName, 
418                          DICompileUnit CompileUnit,
419                          unsigned LineNo, DIType Type, bool isLocalToUnit,
420                          bool isDefinition, llvm::GlobalVariable *GV,
421                          const std::string *FileName = 0,
422                          const std::string *Directory = 0);
423     
424     
425     /// CreateVariable - Create a new descriptor for the specified variable.
426     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
427                               const std::string &Name,
428                               DICompileUnit CompileUnit, unsigned LineNo,
429                               DIType Type,
430                               const std::string *FileName = 0,
431                               const std::string *Directory = 0);
432     
433     /// CreateBlock - This creates a descriptor for a lexical block with the
434     /// specified parent context.
435     DIBlock CreateBlock(DIDescriptor Context);
436     
437     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
438     /// inserting it at the end of the specified basic block.
439     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
440                          BasicBlock *BB);
441     
442     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
443     /// mark the start of the specified subprogram.
444     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
445
446     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
447     /// mark the start of a region for the specified scoping descriptor.
448     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
449     
450     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
451     /// mark the end of a region for the specified scoping descriptor.
452     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
453     
454     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
455     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
456     
457   private:
458     Constant *GetTagConstant(unsigned TAG);
459     Constant *GetStringConstant(const std::string &String);
460     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
461     
462     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
463     Constant *getCastToEmpty(DIDescriptor D);
464   };
465   
466   /// Finds the stoppoint coressponding to this instruction, that is the
467   /// stoppoint that dominates this instruction 
468   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
469
470   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
471   /// instruction in this Basic Block, and returns the stoppoint for it.
472   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
473
474   /// Finds the dbg.declare intrinsic corresponding to this value if any.
475   /// It looks through pointer casts too.
476   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
477 } // end namespace llvm
478
479 #endif