Encode member accessibility.
[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   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     /// Verify - Verify that a compile unit is well formed.
114     bool Verify() const;
115   };
116
117   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
118   /// FIXME: it seems strange that this doesn't have either a reference to the
119   /// type/precision or a file/line pair for location info.
120   class DIEnumerator : public DIDescriptor {
121   public:
122     explicit DIEnumerator(GlobalVariable *GV = 0);
123
124     std::string getName() const  { return getStringField(1); }
125     uint64_t getEnumValue() const { return getUInt64Field(2); }
126   };
127
128   /// DIType - This is a wrapper for a type.
129   /// FIXME: Types should be factored much better so that CV qualifiers and
130   /// others do not require a huge and empty descriptor full of zeros.
131   class DIType : public DIDescriptor {
132   public:
133     enum {
134       FlagPrivate   = 1 << 0,
135       FlagProtected = 1 << 1,
136       FlagFwdDecl   = 1 << 2
137     };
138
139   protected:
140     DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
141     // This ctor is used when the Tag has already been validated by a derived
142     // ctor.
143     DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
144
145   public:
146     /// isDerivedType - Return true if the specified tag is legal for
147     /// DIDerivedType.
148     static bool isDerivedType(unsigned TAG);
149
150     /// isCompositeType - Return true if the specified tag is legal for
151     /// DICompositeType.
152     static bool isCompositeType(unsigned TAG);
153
154     /// isBasicType - Return true if the specified tag is legal for
155     /// DIBasicType.
156     static bool isBasicType(unsigned TAG) {
157       return TAG == dwarf::DW_TAG_base_type;
158     }
159
160     /// Verify - Verify that a type descriptor is well formed.
161     bool Verify() const;
162   public:
163     explicit DIType(GlobalVariable *GV);
164     explicit DIType() {}
165     virtual ~DIType() {}
166
167     DIDescriptor getContext() const     { return getDescriptorField(1); }
168     std::string getName() const         { return getStringField(2); }
169     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
170     unsigned getLineNumber() const      { return getUnsignedField(4); }
171     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
172     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
173     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
174     // carry this is just plain insane.
175     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
176     unsigned getFlags() const           { return getUnsignedField(8); }
177     bool isPrivate() const              { return (getFlags() & FlagPrivate) != 0; }
178     bool isProtected() const            { return (getFlags() & FlagProtected) != 0; }
179     bool isForwardDecl() const          { return (getFlags() & FlagFwdDecl) != 0; }
180
181     virtual std::string getFilename() const { 
182       assert (0 && "Invalid DIDescriptor");
183       return "";
184     }
185
186     virtual std::string getDirectory() const { 
187       assert (0 && "Invalid DIDescriptor");
188       return "";
189     }
190   };
191
192   /// DIBasicType - A basic type, like 'int' or 'float'.
193   class DIBasicType : public DIType {
194   public:
195     explicit DIBasicType(GlobalVariable *GV);
196
197     unsigned getEncoding() const { return getUnsignedField(9); }
198     std::string getFilename() const { return getStringField(10); }
199     std::string getDirectory() const { return getStringField(11); }
200   };
201
202   /// DIDerivedType - A simple derived type, like a const qualified type,
203   /// a typedef, a pointer or reference, etc.
204   class DIDerivedType : public DIType {
205   protected:
206     explicit DIDerivedType(GlobalVariable *GV, bool, bool)
207       : DIType(GV, true, true) {}
208   public:
209     explicit DIDerivedType(GlobalVariable *GV);
210
211     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
212     std::string getFilename() const { return getStringField(10); }
213     std::string getDirectory() const { return getStringField(11); }
214   };
215
216   /// DICompositeType - This descriptor holds a type that can refer to multiple
217   /// other types, like a function or struct.
218   /// FIXME: Why is this a DIDerivedType??
219   class DICompositeType : public DIDerivedType {
220   public:
221     explicit DICompositeType(GlobalVariable *GV);
222
223     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
224     std::string getFilename() const { return getStringField(11); }
225     std::string getDirectory() const { return getStringField(12); }
226
227     /// Verify - Verify that a composite type descriptor is well formed.
228     bool Verify() const;
229   };
230
231   /// DIGlobal - This is a common class for global variables and subprograms.
232   class DIGlobal : public DIDescriptor {
233   protected:
234     explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
235       : DIDescriptor(GV, RequiredTag) {}
236
237     /// isSubprogram - Return true if the specified tag is legal for
238     /// DISubprogram.
239     static bool isSubprogram(unsigned TAG) {
240       return TAG == dwarf::DW_TAG_subprogram;
241     }
242
243     /// isGlobalVariable - Return true if the specified tag is legal for
244     /// DIGlobalVariable.
245     static bool isGlobalVariable(unsigned TAG) {
246       return TAG == dwarf::DW_TAG_variable;
247     }
248
249   public:
250     virtual ~DIGlobal() {}
251
252     DIDescriptor getContext() const     { return getDescriptorField(2); }
253     std::string getName() const         { return getStringField(3); }
254     std::string getDisplayName() const  { return getStringField(4); }
255     std::string getLinkageName() const  { return getStringField(5); }
256     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
257     unsigned getLineNumber() const      { return getUnsignedField(7); }
258     DIType getType() const              { return getFieldAs<DIType>(8); }
259
260     /// isLocalToUnit - Return true if this subprogram is local to the current
261     /// compile unit, like 'static' in C.
262     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
263     unsigned isDefinition() const       { return getUnsignedField(10); }
264
265     virtual std::string getFilename() const { 
266       assert (0 && "Invalid DIDescriptor");
267       return "";
268     }
269
270     virtual std::string getDirectory() const { 
271       assert (0 && "Invalid DIDescriptor");
272       return "";
273     }
274   };
275
276   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
277   class DISubprogram : public DIGlobal {
278   public:
279     explicit DISubprogram(GlobalVariable *GV = 0);
280     std::string getFilename() const { return getStringField(11); }
281     std::string getDirectory() const { return getStringField(12); }
282     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
283
284     /// Verify - Verify that a subprogram descriptor is well formed.
285     bool Verify() const;
286   };
287
288   /// DIGlobalVariable - This is a wrapper for a global variable.
289   class DIGlobalVariable : public DIGlobal {
290   public:
291     explicit DIGlobalVariable(GlobalVariable *GV = 0);
292
293     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
294     std::string getFilename() const { return getStringField(12); }
295     std::string getDirectory() const { return getStringField(13); }
296
297     /// Verify - Verify that a global variable descriptor is well formed.
298     bool Verify() const;
299   };
300
301   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
302   /// global etc).
303   class DIVariable : public DIDescriptor {
304   public:
305     explicit DIVariable(GlobalVariable *GV = 0);
306
307     DIDescriptor getContext() const { return getDescriptorField(1); }
308     std::string getName() const { return getStringField(2); }
309     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
310     unsigned getLineNumber() const      { return getUnsignedField(4); }
311     DIType getType() const              { return getFieldAs<DIType>(5); }
312     std::string getFilename() const { return getStringField(6); }
313     std::string getDirectory() const { return getStringField(7); }
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
322   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
323   class DIBlock : public DIDescriptor {
324   public:
325     explicit DIBlock(GlobalVariable *GV = 0);
326     
327     DIDescriptor getContext() const { return getDescriptorField(1); }
328   };
329
330   /// DIFactory - This object assists with the construction of the various
331   /// descriptors.
332   class DIFactory {
333     Module &M;
334     // Cached values for uniquing and faster lookups.
335     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
336     const Type *EmptyStructPtr; // "{}*".
337     Function *StopPointFn;   // llvm.dbg.stoppoint
338     Function *FuncStartFn;   // llvm.dbg.func.start
339     Function *RegionStartFn; // llvm.dbg.region.start
340     Function *RegionEndFn;   // llvm.dbg.region.end
341     Function *DeclareFn;     // llvm.dbg.declare
342     StringMap<Constant*> StringCache;
343     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
344
345     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
346     void operator=(const DIFactory&); // DO NOT IMPLEMENT
347   public:
348     explicit DIFactory(Module &m);
349
350     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
351     /// creating a new one if there isn't already one in the module.
352     DIAnchor GetOrCreateCompileUnitAnchor();
353
354     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
355     /// creating a new one if there isn't already one in the module.
356     DIAnchor GetOrCreateSubprogramAnchor();
357
358     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
359     /// creating a new one if there isn't already one in the module.
360     DIAnchor GetOrCreateGlobalVariableAnchor();
361
362     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
363     /// This implicitly uniques the arrays created.
364     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
365
366     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
367     /// implicitly uniques the values returned.
368     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
369
370     /// CreateCompileUnit - Create a new descriptor for the specified compile
371     /// unit.
372     DICompileUnit CreateCompileUnit(unsigned LangID,
373                                     const std::string &Filename,
374                                     const std::string &Directory,
375                                     const std::string &Producer);
376
377     /// CreateEnumerator - Create a single enumerator value.
378     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
379
380     /// CreateBasicType - Create a basic type like int, float, etc.
381     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
382                                 DICompileUnit CompileUnit, unsigned LineNumber,
383                                 uint64_t SizeInBits, uint64_t AlignInBits,
384                                 uint64_t OffsetInBits, unsigned Flags,
385                                 unsigned Encoding,
386                                 const std::string *FileName = 0,
387                                 const std::string *Directory = 0);
388
389     /// CreateDerivedType - Create a derived type like const qualified type,
390     /// pointer, typedef, etc.
391     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
392                                     const std::string &Name,
393                                     DICompileUnit CompileUnit,
394                                     unsigned LineNumber,
395                                     uint64_t SizeInBits, uint64_t AlignInBits,
396                                     uint64_t OffsetInBits, unsigned Flags,
397                                     DIType DerivedFrom,
398                                     const std::string *FileName = 0,
399                                     const std::string *Directory = 0);
400
401     /// CreateCompositeType - Create a composite type like array, struct, etc.
402     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
403                                         const std::string &Name,
404                                         DICompileUnit CompileUnit,
405                                         unsigned LineNumber,
406                                         uint64_t SizeInBits,
407                                         uint64_t AlignInBits,
408                                         uint64_t OffsetInBits, unsigned Flags,
409                                         DIType DerivedFrom,
410                                         DIArray Elements,
411                                         const std::string *FileName = 0,
412                                         const std::string *Directory = 0);
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                                   const std::string *FileName = 0,
423                                   const std::string *Directory = 0);
424
425     /// CreateGlobalVariable - Create a new descriptor for the specified global.
426     DIGlobalVariable
427     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
428                          const std::string &DisplayName,
429                          const std::string &LinkageName, 
430                          DICompileUnit CompileUnit,
431                          unsigned LineNo, DIType Type, bool isLocalToUnit,
432                          bool isDefinition, llvm::GlobalVariable *GV,
433                          const std::string *FileName = 0,
434                          const std::string *Directory = 0);
435
436     /// CreateVariable - Create a new descriptor for the specified variable.
437     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
438                               const std::string &Name,
439                               DICompileUnit CompileUnit, unsigned LineNo,
440                               DIType Type,
441                               const std::string *FileName = 0,
442                               const std::string *Directory = 0);
443
444     /// CreateBlock - This creates a descriptor for a lexical block with the
445     /// specified parent context.
446     DIBlock CreateBlock(DIDescriptor Context);
447
448     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
449     /// inserting it at the end of the specified basic block.
450     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
451                          BasicBlock *BB);
452
453     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
454     /// mark the start of the specified subprogram.
455     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
456
457     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
458     /// mark the start of a region for the specified scoping descriptor.
459     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
460
461     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
462     /// mark the end of a region for the specified scoping descriptor.
463     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
464
465     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
466     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
467
468   private:
469     Constant *GetTagConstant(unsigned TAG);
470     Constant *GetStringConstant(const std::string &String);
471     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
472
473     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
474     Constant *getCastToEmpty(DIDescriptor D);
475   };
476
477   /// Finds the stoppoint coressponding to this instruction, that is the
478   /// stoppoint that dominates this instruction 
479   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
480
481   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
482   /// instruction in this Basic Block, and returns the stoppoint for it.
483   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
484
485   /// Finds the dbg.declare intrinsic corresponding to this value if any.
486   /// It looks through pointer casts too.
487   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
488 } // end namespace llvm
489
490 #endif