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