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