1 //===--- llvm/DIBuilder.h - Debug Information Builder -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a DIBuilder that is useful for creating debugging
11 // information entries in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_DIBUILDER_H
16 #define LLVM_DIBUILDER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/DataTypes.h"
32 class DICompositeType;
39 class DIGlobalVariable;
43 class DILexicalBlockFile;
46 class DITemplateTypeParameter;
47 class DITemplateValueParameter;
49 class DIImportedModule;
54 LLVMContext & VMContext;
57 MDNode *TempEnumTypes;
58 MDNode *TempRetainTypes;
59 MDNode *TempSubprograms;
62 Function *DeclareFn; // llvm.dbg.declare
63 Function *ValueFn; // llvm.dbg.value
65 SmallVector<Value *, 4> AllEnumTypes;
66 SmallVector<Value *, 4> AllRetainTypes;
67 SmallVector<Value *, 4> AllSubprograms;
68 SmallVector<Value *, 4> AllGVs;
70 DIBuilder(const DIBuilder &) LLVM_DELETED_FUNCTION;
71 void operator=(const DIBuilder &) LLVM_DELETED_FUNCTION;
74 explicit DIBuilder(Module &M);
75 const MDNode *getCU() { return TheCU; }
76 enum ComplexAddrKind { OpPlus=1, OpDeref };
78 /// finalize - Construct any deferred debug info descriptors.
81 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
82 /// information generated during this instance of compilation.
83 /// @param Lang Source programming language, eg. dwarf::DW_LANG_C99
84 /// @param File File name
85 /// @param Dir Directory
86 /// @param Producer String identify producer of debugging information.
87 /// Usuall this is a compiler version string.
88 /// @param isOptimized A boolean flag which indicates whether optimization
90 /// @param Flags This string lists command line options. This string is
91 /// directly embedded in debug info output which may be used
92 /// by a tool analyzing generated debugging information.
93 /// @param RV This indicates runtime version for languages like
95 /// @param SplitName The name of the file that we'll split debug info out
97 void createCompileUnit(unsigned Lang, StringRef File, StringRef Dir,
98 StringRef Producer, bool isOptimized,
99 StringRef Flags, unsigned RV,
100 StringRef SplitName = StringRef());
102 /// createFile - Create a file descriptor to hold debugging information
104 DIFile createFile(StringRef Filename, StringRef Directory);
106 /// createEnumerator - Create a single enumerator value.
107 DIEnumerator createEnumerator(StringRef Name, uint64_t Val);
109 /// createNullPtrType - Create C++0x nullptr type.
110 DIType createNullPtrType(StringRef Name);
112 /// createBasicType - Create debugging information entry for a basic
114 /// @param Name Type name.
115 /// @param SizeInBits Size of the type.
116 /// @param AlignInBits Type alignment.
117 /// @param Encoding DWARF encoding code, e.g. dwarf::DW_ATE_float.
118 DIBasicType createBasicType(StringRef Name, uint64_t SizeInBits,
119 uint64_t AlignInBits, unsigned Encoding);
121 /// createQualifiedType - Create debugging information entry for a qualified
122 /// type, e.g. 'const int'.
123 /// @param Tag Tag identifing type, e.g. dwarf::TAG_volatile_type
124 /// @param FromTy Base Type.
125 DIDerivedType createQualifiedType(unsigned Tag, DIType FromTy);
127 /// createPointerType - Create debugging information entry for a pointer.
128 /// @param PointeeTy Type pointed by this pointer.
129 /// @param SizeInBits Size.
130 /// @param AlignInBits Alignment. (optional)
131 /// @param Name Pointer type name. (optional)
133 createPointerType(DIType PointeeTy, uint64_t SizeInBits,
134 uint64_t AlignInBits = 0, StringRef Name = StringRef());
136 /// \brief Create debugging information entry for a pointer to member.
137 /// @param PointeeTy Type pointed to by this pointer.
138 /// @param Class Type for which this pointer points to members of.
139 DIDerivedType createMemberPointerType(DIType PointeeTy, DIType Class);
141 /// createReferenceType - Create debugging information entry for a c++
142 /// style reference or rvalue reference type.
143 DIDerivedType createReferenceType(unsigned Tag, DIType RTy);
145 /// createTypedef - Create debugging information entry for a typedef.
146 /// @param Ty Original type.
147 /// @param Name Typedef name.
148 /// @param File File where this type is defined.
149 /// @param LineNo Line number.
150 /// @param Context The surrounding context for the typedef.
151 DIDerivedType createTypedef(DIType Ty, StringRef Name, DIFile File,
152 unsigned LineNo, DIDescriptor Context);
154 /// createFriend - Create debugging information entry for a 'friend'.
155 DIType createFriend(DIType Ty, DIType FriendTy);
157 /// createInheritance - Create debugging information entry to establish
158 /// inheritance relationship between two types.
159 /// @param Ty Original type.
160 /// @param BaseTy Base type. Ty is inherits from base.
161 /// @param BaseOffset Base offset.
162 /// @param Flags Flags to describe inheritance attribute,
164 DIDerivedType createInheritance(DIType Ty, DIType BaseTy,
165 uint64_t BaseOffset, unsigned Flags);
167 /// createMemberType - Create debugging information entry for a member.
168 /// @param Scope Member scope.
169 /// @param Name Member name.
170 /// @param File File where this member is defined.
171 /// @param LineNo Line number.
172 /// @param SizeInBits Member size.
173 /// @param AlignInBits Member alignment.
174 /// @param OffsetInBits Member offset.
175 /// @param Flags Flags to encode member attribute, e.g. private
176 /// @param Ty Parent type.
178 createMemberType(DIDescriptor Scope, StringRef Name, DIFile File,
179 unsigned LineNo, uint64_t SizeInBits, uint64_t AlignInBits,
180 uint64_t OffsetInBits, unsigned Flags, DIType Ty);
182 /// createStaticMemberType - Create debugging information entry for a
183 /// C++ static data member.
184 /// @param Scope Member scope.
185 /// @param Name Member name.
186 /// @param File File where this member is declared.
187 /// @param LineNo Line number.
188 /// @param Ty Type of the static member.
189 /// @param Flags Flags to encode member attribute, e.g. private.
190 /// @param Val Const initializer of the member.
191 DIType createStaticMemberType(DIDescriptor Scope, StringRef Name,
192 DIFile File, unsigned LineNo, DIType Ty,
193 unsigned Flags, llvm::Value *Val);
195 /// createObjCIVar - Create debugging information entry for Objective-C
196 /// instance variable.
197 /// @param Name Member name.
198 /// @param File File where this member is defined.
199 /// @param LineNo Line number.
200 /// @param SizeInBits Member size.
201 /// @param AlignInBits Member alignment.
202 /// @param OffsetInBits Member offset.
203 /// @param Flags Flags to encode member attribute, e.g. private
204 /// @param Ty Parent type.
205 /// @param PropertyName Name of the Objective C property associated with
207 /// @param PropertyGetterName Name of the Objective C property getter
209 /// @param PropertySetterName Name of the Objective C property setter
211 /// @param PropertyAttributes Objective C property attributes.
212 DIType createObjCIVar(StringRef Name, DIFile File,
213 unsigned LineNo, uint64_t SizeInBits,
214 uint64_t AlignInBits, uint64_t OffsetInBits,
215 unsigned Flags, DIType Ty,
216 StringRef PropertyName = StringRef(),
217 StringRef PropertyGetterName = StringRef(),
218 StringRef PropertySetterName = StringRef(),
219 unsigned PropertyAttributes = 0);
221 /// createObjCIVar - Create debugging information entry for Objective-C
222 /// instance variable.
223 /// @param Name Member name.
224 /// @param File File where this member is defined.
225 /// @param LineNo Line number.
226 /// @param SizeInBits Member size.
227 /// @param AlignInBits Member alignment.
228 /// @param OffsetInBits Member offset.
229 /// @param Flags Flags to encode member attribute, e.g. private
230 /// @param Ty Parent type.
231 /// @param PropertyNode Property associated with this ivar.
232 DIType createObjCIVar(StringRef Name, DIFile File,
233 unsigned LineNo, uint64_t SizeInBits,
234 uint64_t AlignInBits, uint64_t OffsetInBits,
235 unsigned Flags, DIType Ty,
236 MDNode *PropertyNode);
238 /// createObjCProperty - Create debugging information entry for Objective-C
240 /// @param Name Property name.
241 /// @param File File where this property is defined.
242 /// @param LineNumber Line number.
243 /// @param GetterName Name of the Objective C property getter selector.
244 /// @param SetterName Name of the Objective C property setter selector.
245 /// @param PropertyAttributes Objective C property attributes.
247 DIObjCProperty createObjCProperty(StringRef Name,
248 DIFile File, unsigned LineNumber,
249 StringRef GetterName,
250 StringRef SetterName,
251 unsigned PropertyAttributes,
254 /// createClassType - Create debugging information entry for a class.
255 /// @param Scope Scope in which this class is defined.
256 /// @param Name class name.
257 /// @param File File where this member is defined.
258 /// @param LineNumber Line number.
259 /// @param SizeInBits Member size.
260 /// @param AlignInBits Member alignment.
261 /// @param OffsetInBits Member offset.
262 /// @param Flags Flags to encode member attribute, e.g. private
263 /// @param Elements class members.
264 /// @param VTableHolder Debug info of the base class that contains vtable
265 /// for this type. This is used in
266 /// DW_AT_containing_type. See DWARF documentation
268 /// @param TemplateParms Template type parameters.
269 DICompositeType createClassType(DIDescriptor Scope, StringRef Name,
270 DIFile File, unsigned LineNumber,
271 uint64_t SizeInBits, uint64_t AlignInBits,
272 uint64_t OffsetInBits, unsigned Flags,
273 DIType DerivedFrom, DIArray Elements,
274 MDNode *VTableHolder = 0,
275 MDNode *TemplateParms = 0);
277 /// createStructType - Create debugging information entry for a struct.
278 /// @param Scope Scope in which this struct is defined.
279 /// @param Name Struct name.
280 /// @param File File where this member is defined.
281 /// @param LineNumber Line number.
282 /// @param SizeInBits Member size.
283 /// @param AlignInBits Member alignment.
284 /// @param Flags Flags to encode member attribute, e.g. private
285 /// @param Elements Struct elements.
286 /// @param RunTimeLang Optional parameter, Objective-C runtime version.
287 DICompositeType createStructType(DIDescriptor Scope, StringRef Name,
288 DIFile File, unsigned LineNumber,
289 uint64_t SizeInBits, uint64_t AlignInBits,
290 unsigned Flags, DIType DerivedFrom,
291 DIArray Elements, unsigned RunTimeLang = 0,
292 MDNode *VTableHolder = 0);
294 /// createUnionType - Create debugging information entry for an union.
295 /// @param Scope Scope in which this union is defined.
296 /// @param Name Union name.
297 /// @param File File where this member is defined.
298 /// @param LineNumber Line number.
299 /// @param SizeInBits Member size.
300 /// @param AlignInBits Member alignment.
301 /// @param Flags Flags to encode member attribute, e.g. private
302 /// @param Elements Union elements.
303 /// @param RunTimeLang Optional parameter, Objective-C runtime version.
304 DICompositeType createUnionType(
305 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
306 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
307 DIArray Elements, unsigned RunTimeLang = 0);
309 /// createTemplateTypeParameter - Create debugging information for template
311 /// @param Scope Scope in which this type is defined.
312 /// @param Name Type parameter name.
313 /// @param Ty Parameter type.
314 /// @param File File where this type parameter is defined.
315 /// @param LineNo Line number.
316 /// @param ColumnNo Column Number.
317 DITemplateTypeParameter
318 createTemplateTypeParameter(DIDescriptor Scope, StringRef Name, DIType Ty,
319 MDNode *File = 0, unsigned LineNo = 0,
320 unsigned ColumnNo = 0);
322 /// createTemplateValueParameter - Create debugging information for template
324 /// @param Scope Scope in which this type is defined.
325 /// @param Name Value parameter name.
326 /// @param Ty Parameter type.
327 /// @param Value Constant parameter value.
328 /// @param File File where this type parameter is defined.
329 /// @param LineNo Line number.
330 /// @param ColumnNo Column Number.
331 DITemplateValueParameter
332 createTemplateValueParameter(DIDescriptor Scope, StringRef Name, DIType Ty,
334 MDNode *File = 0, unsigned LineNo = 0,
335 unsigned ColumnNo = 0);
337 /// createArrayType - Create debugging information entry for an array.
338 /// @param Size Array size.
339 /// @param AlignInBits Alignment.
340 /// @param Ty Element type.
341 /// @param Subscripts Subscripts.
342 DICompositeType createArrayType(uint64_t Size, uint64_t AlignInBits,
343 DIType Ty, DIArray Subscripts);
345 /// createVectorType - Create debugging information entry for a vector type.
346 /// @param Size Array size.
347 /// @param AlignInBits Alignment.
348 /// @param Ty Element type.
349 /// @param Subscripts Subscripts.
350 DIType createVectorType(uint64_t Size, uint64_t AlignInBits,
351 DIType Ty, DIArray Subscripts);
353 /// createEnumerationType - Create debugging information entry for an
355 /// @param Scope Scope in which this enumeration is defined.
356 /// @param Name Union name.
357 /// @param File File where this member is defined.
358 /// @param LineNumber Line number.
359 /// @param SizeInBits Member size.
360 /// @param AlignInBits Member alignment.
361 /// @param Elements Enumeration elements.
362 DICompositeType createEnumerationType(
363 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
364 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
367 /// createSubroutineType - Create subroutine type.
368 /// @param File File in which this subroutine is defined.
369 /// @param ParameterTypes An array of subroutine parameter types. This
370 /// includes return type at 0th index.
371 DICompositeType createSubroutineType(DIFile File, DIArray ParameterTypes);
373 /// createArtificialType - Create a new DIType with "artificial" flag set.
374 DIType createArtificialType(DIType Ty);
376 /// createObjectPointerType - Create a new DIType with the "object pointer"
378 DIType createObjectPointerType(DIType Ty);
380 /// createForwardDecl - Create a temporary forward-declared type.
381 DIType createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
382 DIFile F, unsigned Line, unsigned RuntimeLang = 0,
383 uint64_t SizeInBits = 0, uint64_t AlignInBits = 0);
385 /// retainType - Retain DIType in a module even if it is not referenced
386 /// through debug info anchors.
387 void retainType(DIType T);
389 /// createUnspecifiedParameter - Create unspeicified type descriptor
390 /// for a subroutine type.
391 DIDescriptor createUnspecifiedParameter();
393 /// getOrCreateArray - Get a DIArray, create one if required.
394 DIArray getOrCreateArray(ArrayRef<Value *> Elements);
396 /// getOrCreateSubrange - Create a descriptor for a value range. This
397 /// implicitly uniques the values returned.
398 DISubrange getOrCreateSubrange(int64_t Lo, int64_t Count);
400 /// createGlobalVariable - Create a new descriptor for the specified global.
401 /// @param Name Name of the variable.
402 /// @param File File where this variable is defined.
403 /// @param LineNo Line number.
404 /// @param Ty Variable Type.
405 /// @param isLocalToUnit Boolean flag indicate whether this variable is
406 /// externally visible or not.
407 /// @param Val llvm::Value of the variable.
409 createGlobalVariable(StringRef Name, DIFile File, unsigned LineNo,
410 DIType Ty, bool isLocalToUnit, llvm::Value *Val);
412 /// \brief Create a new descriptor for the specified global.
413 /// @param Name Name of the variable.
414 /// @param LinkageName Mangled variable name.
415 /// @param File File where this variable is defined.
416 /// @param LineNo Line number.
417 /// @param Ty Variable Type.
418 /// @param isLocalToUnit Boolean flag indicate whether this variable is
419 /// externally visible or not.
420 /// @param Val llvm::Value of the variable.
422 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile File,
423 unsigned LineNo, DIType Ty, bool isLocalToUnit,
426 /// createStaticVariable - Create a new descriptor for the specified
428 /// @param Context Variable scope.
429 /// @param Name Name of the variable.
430 /// @param LinkageName Mangled name of the variable.
431 /// @param File File where this variable is defined.
432 /// @param LineNo Line number.
433 /// @param Ty Variable Type.
434 /// @param isLocalToUnit Boolean flag indicate whether this variable is
435 /// externally visible or not.
436 /// @param Val llvm::Value of the variable.
437 /// @param Decl Reference to the corresponding declaration.
439 createStaticVariable(DIDescriptor Context, StringRef Name,
440 StringRef LinkageName, DIFile File, unsigned LineNo,
441 DIType Ty, bool isLocalToUnit, llvm::Value *Val,
442 MDNode *Decl = NULL);
445 /// createLocalVariable - Create a new descriptor for the specified
447 /// @param Tag Dwarf TAG. Usually DW_TAG_auto_variable or
448 /// DW_TAG_arg_variable.
449 /// @param Scope Variable scope.
450 /// @param Name Variable name.
451 /// @param File File where this variable is defined.
452 /// @param LineNo Line number.
453 /// @param Ty Variable Type
454 /// @param AlwaysPreserve Boolean. Set to true if debug info for this
455 /// variable should be preserved in optimized build.
456 /// @param Flags Flags, e.g. artificial variable.
457 /// @param ArgNo If this variable is an arugment then this argument's
458 /// number. 1 indicates 1st argument.
459 DIVariable createLocalVariable(unsigned Tag, DIDescriptor Scope,
461 DIFile File, unsigned LineNo,
462 DIType Ty, bool AlwaysPreserve = false,
467 /// createComplexVariable - Create a new descriptor for the specified
468 /// variable which has a complex address expression for its address.
469 /// @param Tag Dwarf TAG. Usually DW_TAG_auto_variable or
470 /// DW_TAG_arg_variable.
471 /// @param Scope Variable scope.
472 /// @param Name Variable name.
473 /// @param F File where this variable is defined.
474 /// @param LineNo Line number.
475 /// @param Ty Variable Type
476 /// @param Addr An array of complex address operations.
477 /// @param ArgNo If this variable is an arugment then this argument's
478 /// number. 1 indicates 1st argument.
479 DIVariable createComplexVariable(unsigned Tag, DIDescriptor Scope,
480 StringRef Name, DIFile F, unsigned LineNo,
481 DIType Ty, ArrayRef<Value *> Addr,
484 /// createFunction - Create a new descriptor for the specified subprogram.
485 /// See comments in DISubprogram for descriptions of these fields.
486 /// @param Scope Function scope.
487 /// @param Name Function name.
488 /// @param LinkageName Mangled function name.
489 /// @param File File where this variable is defined.
490 /// @param LineNo Line number.
491 /// @param Ty Function type.
492 /// @param isLocalToUnit True if this function is not externally visible..
493 /// @param isDefinition True if this is a function definition.
494 /// @param ScopeLine Set to the beginning of the scope this starts
495 /// @param Flags e.g. is this function prototyped or not.
496 /// This flags are used to emit dwarf attributes.
497 /// @param isOptimized True if optimization is ON.
498 /// @param Fn llvm::Function pointer.
499 /// @param TParam Function template parameters.
500 DISubprogram createFunction(DIDescriptor Scope, StringRef Name,
501 StringRef LinkageName,
502 DIFile File, unsigned LineNo,
503 DIType Ty, bool isLocalToUnit,
507 bool isOptimized = false,
512 /// createMethod - Create a new descriptor for the specified C++ method.
513 /// See comments in DISubprogram for descriptions of these fields.
514 /// @param Scope Function scope.
515 /// @param Name Function name.
516 /// @param LinkageName Mangled function name.
517 /// @param File File where this variable is defined.
518 /// @param LineNo Line number.
519 /// @param Ty Function type.
520 /// @param isLocalToUnit True if this function is not externally visible..
521 /// @param isDefinition True if this is a function definition.
522 /// @param Virtuality Attributes describing virtualness. e.g. pure
523 /// virtual function.
524 /// @param VTableIndex Index no of this method in virtual table.
525 /// @param VTableHolder Type that holds vtable.
526 /// @param Flags e.g. is this function prototyped or not.
527 /// This flags are used to emit dwarf attributes.
528 /// @param isOptimized True if optimization is ON.
529 /// @param Fn llvm::Function pointer.
530 /// @param TParam Function template parameters.
531 DISubprogram createMethod(DIDescriptor Scope, StringRef Name,
532 StringRef LinkageName,
533 DIFile File, unsigned LineNo,
534 DIType Ty, bool isLocalToUnit,
536 unsigned Virtuality = 0, unsigned VTableIndex = 0,
537 MDNode *VTableHolder = 0,
539 bool isOptimized = false,
543 /// createNameSpace - This creates new descriptor for a namespace
544 /// with the specified parent scope.
545 /// @param Scope Namespace scope
546 /// @param Name Name of this namespace
547 /// @param File Source file
548 /// @param LineNo Line number
549 DINameSpace createNameSpace(DIDescriptor Scope, StringRef Name,
550 DIFile File, unsigned LineNo);
553 /// createLexicalBlockFile - This creates a descriptor for a lexical
554 /// block with a new file attached. This merely extends the existing
555 /// lexical block as it crosses a file.
556 /// @param Scope Lexical block.
557 /// @param File Source file.
558 DILexicalBlockFile createLexicalBlockFile(DIDescriptor Scope,
561 /// createLexicalBlock - This creates a descriptor for a lexical block
562 /// with the specified parent context.
563 /// @param Scope Parent lexical scope.
564 /// @param File Source file
565 /// @param Line Line number
566 /// @param Col Column number
567 DILexicalBlock createLexicalBlock(DIDescriptor Scope, DIFile File,
568 unsigned Line, unsigned Col);
571 /// \brief Create a descriptor for an imported module.
572 /// @param NS The namespace being imported here
573 DIImportedModule createImportedModule(DINameSpace NS);
575 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
576 /// @param Storage llvm::Value of the variable
577 /// @param VarInfo Variable's debug info descriptor.
578 /// @param InsertAtEnd Location for the new intrinsic.
579 Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
580 BasicBlock *InsertAtEnd);
582 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
583 /// @param Storage llvm::Value of the variable
584 /// @param VarInfo Variable's debug info descriptor.
585 /// @param InsertBefore Location for the new intrinsic.
586 Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
587 Instruction *InsertBefore);
590 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
591 /// @param Val llvm::Value of the variable
592 /// @param Offset Offset
593 /// @param VarInfo Variable's debug info descriptor.
594 /// @param InsertAtEnd Location for the new intrinsic.
595 Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
597 BasicBlock *InsertAtEnd);
599 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
600 /// @param Val llvm::Value of the variable
601 /// @param Offset Offset
602 /// @param VarInfo Variable's debug info descriptor.
603 /// @param InsertBefore Location for the new intrinsic.
604 Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
606 Instruction *InsertBefore);
609 } // end namespace llvm