A typedef's context is not the same as type's context. It is the context of typedef...
[oota-llvm.git] / include / llvm / Analysis / DIBuilder.h
1 //===--- llvm/Analysis/DIBuilder.h - Debug Information Builder --*- 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 DIBuilder that is useful for creating debugging 
11 // information entries in LLVM IR form.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_DIBUILDER_H
16 #define LLVM_ANALYSIS_DIBUILDER_H
17
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21
22 namespace llvm {
23   class BasicBlock;
24   class Instruction;
25   class Function;
26   class Module;
27   class Value;
28   class LLVMContext;
29   class MDNode;
30   class StringRef;
31   class DIDescriptor;
32   class DIFile;
33   class DIEnumerator;
34   class DIType;
35   class DIArray;
36   class DIGlobalVariable;
37   class DINameSpace;
38   class DIVariable;
39   class DISubrange;
40   class DILexicalBlock;
41   class DISubprogram;
42   class DITemplateTypeParameter;
43   class DITemplateValueParameter;
44
45   class DIBuilder {
46     private:
47     Module &M;
48     LLVMContext & VMContext;
49     MDNode *TheCU;
50
51     Function *DeclareFn;     // llvm.dbg.declare
52     Function *ValueFn;       // llvm.dbg.value
53
54     DIBuilder(const DIBuilder &);       // DO NOT IMPLEMENT
55     void operator=(const DIBuilder &);  // DO NOT IMPLEMENT
56
57     public:
58     explicit DIBuilder(Module &M);
59     const MDNode *getCU() { return TheCU; }
60     enum ComplexAddrKind { OpPlus=1, OpDeref };
61
62     /// createCompileUnit - A CompileUnit provides an anchor for all debugging
63     /// information generated during this instance of compilation.
64     /// @param Lang     Source programming language, eg. dwarf::DW_LANG_C99
65     /// @param File     File name
66     /// @param Dir      Directory
67     /// @param Producer String identify producer of debugging information. 
68     ///                 Usuall this is a compiler version string.
69     /// @param isOptimized A boolean flag which indicates whether optimization
70     ///                    is ON or not.
71     /// @param Flags    This string lists command line options. This string is 
72     ///                 directly embedded in debug info output which may be used
73     ///                 by a tool analyzing generated debugging information.
74     /// @param RV       This indicates runtime version for languages like 
75     ///                 Objective-C.
76     void createCompileUnit(unsigned Lang, StringRef File, StringRef Dir, 
77                            StringRef Producer,
78                            bool isOptimized, StringRef Flags, unsigned RV);
79
80     /// createFile - Create a file descriptor to hold debugging information
81     /// for a file.
82     DIFile createFile(StringRef Filename, StringRef Directory);
83                            
84     /// createEnumerator - Create a single enumerator value.
85     DIEnumerator createEnumerator(StringRef Name, uint64_t Val);
86
87     /// createBasicType - Create debugging information entry for a basic 
88     /// type.
89     /// @param Name        Type name.
90     /// @param SizeInBits  Size of the type.
91     /// @param AlignInBits Type alignment.
92     /// @param Encoding    DWARF encoding code, e.g. dwarf::DW_ATE_float.
93     DIType createBasicType(StringRef Name, uint64_t SizeInBits, 
94                            uint64_t AlignInBits, unsigned Encoding);
95
96     /// createQualifiedType - Create debugging information entry for a qualified
97     /// type, e.g. 'const int'.
98     /// @param Tag         Tag identifing type, e.g. dwarf::TAG_volatile_type
99     /// @param FromTy      Base Type.
100     DIType createQualifiedType(unsigned Tag, DIType FromTy);
101
102     /// createPointerType - Create debugging information entry for a pointer.
103     /// @param PointeeTy   Type pointed by this pointer.
104     /// @param SizeInBits  Size.
105     /// @param AlignInBits Alignment. (optional)
106     /// @param Name        Pointer type name. (optional)
107     DIType createPointerType(DIType PointeeTy, uint64_t SizeInBits,
108                              uint64_t AlignInBits = 0, 
109                              StringRef Name = StringRef());
110
111     /// createReferenceType - Create debugging information entry for a c++
112     /// style reference.
113     DIType createReferenceType(DIType RTy);
114
115     /// createTypedef - Create debugging information entry for a typedef.
116     /// @param Ty          Original type.
117     /// @param Name        Typedef name.
118     /// @param File        File where this type is defined.
119     /// @param LineNo      Line number.
120     /// @param Context     The surrounding context for the typedef.
121     DIType createTypedef(DIType Ty, StringRef Name, DIFile File, 
122                          unsigned LineNo, DIDescriptor Context);
123
124     /// createFriend - Create debugging information entry for a 'friend'.
125     DIType createFriend(DIType Ty, DIType FriendTy);
126
127     /// createInheritance - Create debugging information entry to establish
128     /// inheritance relationship between two types.
129     /// @param Ty           Original type.
130     /// @param BaseTy       Base type. Ty is inherits from base.
131     /// @param BaseOffset   Base offset.
132     /// @param Flags        Flags to describe inheritance attribute, 
133     ///                     e.g. private
134     DIType createInheritance(DIType Ty, DIType BaseTy, uint64_t BaseOffset,
135                              unsigned Flags);
136
137     /// createMemberType - Create debugging information entry for a member.
138     /// @param Name         Member name.
139     /// @param File         File where this member is defined.
140     /// @param LineNo       Line number.
141     /// @param SizeInBits   Member size.
142     /// @param AlignInBits  Member alignment.
143     /// @param OffsetInBits Member offset.
144     /// @param Flags        Flags to encode member attribute, e.g. private
145     /// @param Ty           Parent type.
146     DIType createMemberType(StringRef Name, DIFile File,
147                             unsigned LineNo, uint64_t SizeInBits, 
148                             uint64_t AlignInBits, uint64_t OffsetInBits, 
149                             unsigned Flags, DIType Ty);
150
151     /// createObjCIVar - Create debugging information entry for Objective-C
152     /// instance variable.
153     /// @param Name         Member name.
154     /// @param File         File where this member is defined.
155     /// @param LineNo       Line number.
156     /// @param SizeInBits   Member size.
157     /// @param AlignInBits  Member alignment.
158     /// @param OffsetInBits Member offset.
159     /// @param Flags        Flags to encode member attribute, e.g. private
160     /// @param Ty           Parent type.
161     /// @param PropertyName Name of the Objective C property assoicated with
162     ///                     this ivar.
163     /// @param GetterName   Name of the Objective C property getter selector.
164     /// @param SetterName   Name of the Objective C property setter selector.
165     /// @param PropertyAttributes Objective C property attributes.
166     DIType createObjCIVar(StringRef Name, DIFile File,
167                           unsigned LineNo, uint64_t SizeInBits, 
168                           uint64_t AlignInBits, uint64_t OffsetInBits, 
169                           unsigned Flags, DIType Ty,
170                           StringRef PropertyName = StringRef(),
171                           StringRef PropertyGetterName = StringRef(),
172                           StringRef PropertySetterName = StringRef(),
173                           unsigned PropertyAttributes = 0);
174
175     /// createClassType - Create debugging information entry for a class.
176     /// @param Scope        Scope in which this class is defined.
177     /// @param Name         class name.
178     /// @param File         File where this member is defined.
179     /// @param LineNo       Line number.
180     /// @param SizeInBits   Member size.
181     /// @param AlignInBits  Member alignment.
182     /// @param OffsetInBits Member offset.
183     /// @param Flags        Flags to encode member attribute, e.g. private
184     /// @param Elements     class members.
185     /// @param VTableHolder Debug info of the base class that contains vtable
186     ///                     for this type. This is used in 
187     ///                     DW_AT_containing_type. See DWARF documentation
188     ///                     for more info.
189     /// @param TemplateParms Template type parameters.
190     DIType createClassType(DIDescriptor Scope, StringRef Name, DIFile File,
191                            unsigned LineNumber, uint64_t SizeInBits,
192                            uint64_t AlignInBits, uint64_t OffsetInBits,
193                            unsigned Flags, DIType DerivedFrom, 
194                            DIArray Elements, MDNode *VTableHolder = 0,
195                            MDNode *TemplateParms = 0);
196
197     /// createStructType - Create debugging information entry for a struct.
198     /// @param Scope        Scope in which this struct is defined.
199     /// @param Name         Struct name.
200     /// @param File         File where this member is defined.
201     /// @param LineNo       Line number.
202     /// @param SizeInBits   Member size.
203     /// @param AlignInBits  Member alignment.
204     /// @param Flags        Flags to encode member attribute, e.g. private
205     /// @param Elements     Struct elements.
206     /// @param RunTimeLang  Optional parameter, Objective-C runtime version.
207     DIType createStructType(DIDescriptor Scope, StringRef Name, DIFile File,
208                             unsigned LineNumber, uint64_t SizeInBits,
209                             uint64_t AlignInBits, unsigned Flags,
210                             DIArray Elements, unsigned RunTimeLang = 0);
211
212     /// createUnionType - Create debugging information entry for an union.
213     /// @param Scope        Scope in which this union is defined.
214     /// @param Name         Union name.
215     /// @param File         File where this member is defined.
216     /// @param LineNo       Line number.
217     /// @param SizeInBits   Member size.
218     /// @param AlignInBits  Member alignment.
219     /// @param Flags        Flags to encode member attribute, e.g. private
220     /// @param Elements     Union elements.
221     /// @param RunTimeLang  Optional parameter, Objective-C runtime version.
222     DIType createUnionType(DIDescriptor Scope, StringRef Name, DIFile File,
223                            unsigned LineNumber, uint64_t SizeInBits,
224                            uint64_t AlignInBits, unsigned Flags,
225                            DIArray Elements, unsigned RunTimeLang = 0);
226
227     /// createTemplateTypeParameter - Create debugging information for template
228     /// type parameter.
229     /// @param Scope        Scope in which this type is defined.
230     /// @param Name         Type parameter name.
231     /// @param Ty           Parameter type.
232     /// @param File         File where this type parameter is defined.
233     /// @param LineNo       Line number.
234     /// @param ColumnNo     Column Number.
235     DITemplateTypeParameter
236     createTemplateTypeParameter(DIDescriptor Scope, StringRef Name, DIType Ty,
237                                 MDNode *File = 0, unsigned LineNo = 0,
238                                 unsigned ColumnNo = 0);
239
240     /// createTemplateValueParameter - Create debugging information for template
241     /// value parameter.
242     /// @param Scope        Scope in which this type is defined.
243     /// @param Name         Value parameter name.
244     /// @param Ty           Parameter type.
245     /// @param Value        Constant parameter value.
246     /// @param File         File where this type parameter is defined.
247     /// @param LineNo       Line number.
248     /// @param ColumnNo     Column Number.
249     DITemplateValueParameter
250     createTemplateValueParameter(DIDescriptor Scope, StringRef Name, DIType Ty,
251                                  uint64_t Value,
252                                  MDNode *File = 0, unsigned LineNo = 0,
253                                  unsigned ColumnNo = 0);
254
255     /// createArrayType - Create debugging information entry for an array.
256     /// @param Size         Array size.
257     /// @param AlignInBits  Alignment.
258     /// @param Ty           Element type.
259     /// @param Subscripts   Subscripts.
260     DIType createArrayType(uint64_t Size, uint64_t AlignInBits, 
261                            DIType Ty, DIArray Subscripts);
262
263     /// createVectorType - Create debugging information entry for a vector type.
264     /// @param Size         Array size.
265     /// @param AlignInBits  Alignment.
266     /// @param Ty           Element type.
267     /// @param Subscripts   Subscripts.
268     DIType createVectorType(uint64_t Size, uint64_t AlignInBits, 
269                             DIType Ty, DIArray Subscripts);
270
271     /// createEnumerationType - Create debugging information entry for an 
272     /// enumeration.
273     /// @param Scope        Scope in which this enumeration is defined.
274     /// @param Name         Union name.
275     /// @param File         File where this member is defined.
276     /// @param LineNo       Line number.
277     /// @param SizeInBits   Member size.
278     /// @param AlignInBits  Member alignment.
279     /// @param Elements     Enumeration elements.
280     DIType createEnumerationType(DIDescriptor Scope, StringRef Name, 
281                                  DIFile File, unsigned LineNumber, 
282                                  uint64_t SizeInBits, 
283                                  uint64_t AlignInBits, DIArray Elements);
284
285     /// createSubroutineType - Create subroutine type.
286     /// @param File          File in which this subroutine is defined.
287     /// @param ParamterTypes An array of subroutine parameter types. This
288     ///                      includes return type at 0th index.
289     DIType createSubroutineType(DIFile File, DIArray ParameterTypes);
290
291     /// createArtificialType - Create a new DIType with "artificial" flag set.
292     DIType createArtificialType(DIType Ty);
293
294     /// createTemporaryType - Create a temporary forward-declared type.
295     DIType createTemporaryType();
296     DIType createTemporaryType(DIFile F);
297
298     /// retainType - Retain DIType in a module even if it is not referenced 
299     /// through debug info anchors.
300     void retainType(DIType T);
301
302     /// createUnspecifiedParameter - Create unspeicified type descriptor
303     /// for a subroutine type.
304     DIDescriptor createUnspecifiedParameter();
305
306     /// getOrCreateArray - Get a DIArray, create one if required.
307     DIArray getOrCreateArray(ArrayRef<Value *> Elements);
308
309     /// getOrCreateSubrange - Create a descriptor for a value range.  This
310     /// implicitly uniques the values returned.
311     DISubrange getOrCreateSubrange(int64_t Lo, int64_t Hi);
312
313     /// createGlobalVariable - Create a new descriptor for the specified global.
314     /// @param Name        Name of the variable.
315     /// @param File        File where this variable is defined.
316     /// @param LineNo      Line number.
317     /// @param Ty          Variable Type.
318     /// @param isLocalToUnit Boolean flag indicate whether this variable is
319     ///                      externally visible or not.
320     /// @param Val         llvm::Value of the variable.
321     DIGlobalVariable
322     createGlobalVariable(StringRef Name, DIFile File, unsigned LineNo,
323                          DIType Ty, bool isLocalToUnit, llvm::Value *Val);
324
325
326     /// createStaticVariable - Create a new descriptor for the specified 
327     /// variable.
328     /// @param Conext      Variable scope. 
329     /// @param Name        Name of the variable.
330     /// @param LinakgeName Mangled  name of the variable.
331     /// @param File        File where this variable is defined.
332     /// @param LineNo      Line number.
333     /// @param Ty          Variable Type.
334     /// @param isLocalToUnit Boolean flag indicate whether this variable is
335     ///                      externally visible or not.
336     /// @param Val         llvm::Value of the variable.
337     DIGlobalVariable
338     createStaticVariable(DIDescriptor Context, StringRef Name, 
339                          StringRef LinkageName, DIFile File, unsigned LineNo, 
340                          DIType Ty, bool isLocalToUnit, llvm::Value *Val);
341
342
343     /// createLocalVariable - Create a new descriptor for the specified 
344     /// local variable.
345     /// @param Tag         Dwarf TAG. Usually DW_TAG_auto_variable or
346     ///                    DW_TAG_arg_variable.
347     /// @param Scope       Variable scope.
348     /// @param Name        Variable name.
349     /// @param File        File where this variable is defined.
350     /// @param LineNo      Line number.
351     /// @param Ty          Variable Type
352     /// @param AlwaysPreserve Boolean. Set to true if debug info for this
353     ///                       variable should be preserved in optimized build.
354     /// @param Flags          Flags, e.g. artificial variable.
355     /// @param ArgNo       If this variable is an arugment then this argument's
356     ///                    number. 1 indicates 1st argument.
357     DIVariable createLocalVariable(unsigned Tag, DIDescriptor Scope,
358                                    StringRef Name,
359                                    DIFile File, unsigned LineNo,
360                                    DIType Ty, bool AlwaysPreserve = false,
361                                    unsigned Flags = 0,
362                                    unsigned ArgNo = 0);
363
364
365     /// createComplexVariable - Create a new descriptor for the specified
366     /// variable which has a complex address expression for its address.
367     /// @param Tag         Dwarf TAG. Usually DW_TAG_auto_variable or
368     ///                    DW_TAG_arg_variable.
369     /// @param Scope       Variable scope.
370     /// @param Name        Variable name.
371     /// @param File        File where this variable is defined.
372     /// @param LineNo      Line number.
373     /// @param Ty          Variable Type
374     /// @param Addr        An array of complex address operations.
375     /// @param ArgNo       If this variable is an arugment then this argument's
376     ///                    number. 1 indicates 1st argument.
377     DIVariable createComplexVariable(unsigned Tag, DIDescriptor Scope,
378                                      StringRef Name, DIFile F, unsigned LineNo,
379                                      DIType Ty, ArrayRef<Value *> Addr,
380                                      unsigned ArgNo = 0);
381
382     /// createFunction - Create a new descriptor for the specified subprogram.
383     /// See comments in DISubprogram for descriptions of these fields.
384     /// @param Scope         Function scope.
385     /// @param Name          Function name.
386     /// @param LinkageName   Mangled function name.
387     /// @param File          File where this variable is defined.
388     /// @param LineNo        Line number.
389     /// @param Ty            Function type.
390     /// @param isLocalToUnit True if this function is not externally visible..
391     /// @param isDefinition  True if this is a function definition.
392     /// @param Flags         e.g. is this function prototyped or not.
393     ///                      This flags are used to emit dwarf attributes.
394     /// @param isOptimized   True if optimization is ON.
395     /// @param Fn            llvm::Function pointer.
396     /// @param TParam        Function template parameters.
397     DISubprogram createFunction(DIDescriptor Scope, StringRef Name,
398                                 StringRef LinkageName,
399                                 DIFile File, unsigned LineNo,
400                                 DIType Ty, bool isLocalToUnit,
401                                 bool isDefinition,
402                                 unsigned Flags = 0,
403                                 bool isOptimized = false,
404                                 Function *Fn = 0,
405                                 MDNode *TParam = 0,
406                                 MDNode *Decl = 0);
407
408     /// createMethod - Create a new descriptor for the specified C++ method.
409     /// See comments in DISubprogram for descriptions of these fields.
410     /// @param Scope         Function scope.
411     /// @param Name          Function name.
412     /// @param LinkageName   Mangled function name.
413     /// @param File          File where this variable is defined.
414     /// @param LineNo        Line number.
415     /// @param Ty            Function type.
416     /// @param isLocalToUnit True if this function is not externally visible..
417     /// @param isDefinition  True if this is a function definition.
418     /// @param Virtuality    Attributes describing virtualness. e.g. pure 
419     ///                      virtual function.
420     /// @param VTableIndex   Index no of this method in virtual table.
421     /// @param VTableHolder  Type that holds vtable.
422     /// @param Flags         e.g. is this function prototyped or not.
423     ///                      This flags are used to emit dwarf attributes.
424     /// @param isOptimized   True if optimization is ON.
425     /// @param Fn            llvm::Function pointer.
426     /// @param TParam        Function template parameters.
427     DISubprogram createMethod(DIDescriptor Scope, StringRef Name,
428                               StringRef LinkageName,
429                               DIFile File, unsigned LineNo,
430                               DIType Ty, bool isLocalToUnit,
431                               bool isDefinition,
432                               unsigned Virtuality = 0, unsigned VTableIndex = 0,
433                               MDNode *VTableHolder = 0,
434                               unsigned Flags = 0,
435                               bool isOptimized = false,
436                               Function *Fn = 0,
437                               MDNode *TParam = 0);
438
439     /// createNameSpace - This creates new descriptor for a namespace
440     /// with the specified parent scope.
441     /// @param Scope       Namespace scope
442     /// @param Name        Name of this namespace
443     /// @param File        Source file
444     /// @param LineNo      Line number
445     DINameSpace createNameSpace(DIDescriptor Scope, StringRef Name,
446                                 DIFile File, unsigned LineNo);
447
448
449     /// createLexicalBlock - This creates a descriptor for a lexical block
450     /// with the specified parent context.
451     /// @param Scope       Parent lexical scope.
452     /// @param File        Source file
453     /// @param Line        Line number
454     /// @param Col         Column number
455     DILexicalBlock createLexicalBlock(DIDescriptor Scope, DIFile File,
456                                       unsigned Line, unsigned Col);
457
458     /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
459     /// @param Storage     llvm::Value of the variable
460     /// @param VarInfo     Variable's debug info descriptor.
461     /// @param InsertAtEnd Location for the new intrinsic.
462     Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
463                                BasicBlock *InsertAtEnd);
464
465     /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
466     /// @param Storage      llvm::Value of the variable
467     /// @param VarInfo      Variable's debug info descriptor.
468     /// @param InsertBefore Location for the new intrinsic.
469     Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
470                                Instruction *InsertBefore);
471
472
473     /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
474     /// @param Val          llvm::Value of the variable
475     /// @param Offset       Offset
476     /// @param VarInfo      Variable's debug info descriptor.
477     /// @param InsertAtEnd Location for the new intrinsic.
478     Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
479                                          DIVariable VarInfo, 
480                                          BasicBlock *InsertAtEnd);
481     
482     /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
483     /// @param Val          llvm::Value of the variable
484     /// @param Offset       Offset
485     /// @param VarInfo      Variable's debug info descriptor.
486     /// @param InsertBefore Location for the new intrinsic.
487     Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
488                                          DIVariable VarInfo, 
489                                          Instruction *InsertBefore);
490
491   };
492 } // end namespace llvm
493
494 #endif