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