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