blob: e0238567f2512e828091a7f7463d40a868cc4410 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- DIBuilder.h - Debug Information Builder ------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a DIBuilder that is useful for creating debugging
10// information entries in LLVM IR form.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_DIBUILDER_H
15#define LLVM_IR_DIBUILDER_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/MapVector.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/IR/DebugInfo.h"
25#include "llvm/IR/DebugInfoMetadata.h"
26#include "llvm/IR/TrackingMDRef.h"
27#include "llvm/Support/Casting.h"
28#include <algorithm>
29#include <cstdint>
30
31namespace llvm {
32
33 class BasicBlock;
34 class Constant;
35 class Function;
36 class Instruction;
37 class LLVMContext;
38 class Module;
39 class Value;
40
41 class DIBuilder {
42 Module &M;
43 LLVMContext &VMContext;
44
45 DICompileUnit *CUNode; ///< The one compile unit created by this DIBuiler.
46 Function *DeclareFn; ///< llvm.dbg.declare
47 Function *ValueFn; ///< llvm.dbg.value
Andrew Scullcdfcccc2018-10-05 20:58:37 +010048 Function *LabelFn; ///< llvm.dbg.label
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049
50 SmallVector<Metadata *, 4> AllEnumTypes;
51 /// Track the RetainTypes, since they can be updated later on.
52 SmallVector<TrackingMDNodeRef, 4> AllRetainTypes;
53 SmallVector<Metadata *, 4> AllSubprograms;
54 SmallVector<Metadata *, 4> AllGVs;
55 SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
56 /// Map Macro parent (which can be DIMacroFile or nullptr) to a list of
57 /// Metadata all of type DIMacroNode.
58 /// DIMacroNode's with nullptr parent are DICompileUnit direct children.
59 MapVector<MDNode *, SetVector<Metadata *>> AllMacrosPerParent;
60
61 /// Track nodes that may be unresolved.
62 SmallVector<TrackingMDNodeRef, 4> UnresolvedNodes;
63 bool AllowUnresolvedNodes;
64
65 /// Each subprogram's preserved local variables.
66 ///
67 /// Do not use a std::vector. Some versions of libc++ apparently copy
68 /// instead of move on grow operations, and TrackingMDRef is expensive to
69 /// copy.
70 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> PreservedVariables;
71
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// Each subprogram's preserved labels.
73 DenseMap<MDNode *, SmallVector<TrackingMDNodeRef, 1>> PreservedLabels;
74
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 /// Create a temporary.
76 ///
77 /// Create an \a temporary node and track it in \a UnresolvedNodes.
78 void trackIfUnresolved(MDNode *N);
79
80 /// Internal helper for insertDeclare.
81 Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
82 DIExpression *Expr, const DILocation *DL,
83 BasicBlock *InsertBB, Instruction *InsertBefore);
84
Andrew Scullcdfcccc2018-10-05 20:58:37 +010085 /// Internal helper for insertLabel.
86 Instruction *insertLabel(DILabel *LabelInfo, const DILocation *DL,
87 BasicBlock *InsertBB, Instruction *InsertBefore);
88
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 /// Internal helper for insertDbgValueIntrinsic.
90 Instruction *
91 insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo,
92 DIExpression *Expr, const DILocation *DL,
93 BasicBlock *InsertBB, Instruction *InsertBefore);
94
95 public:
96 /// Construct a builder for a module.
97 ///
98 /// If \c AllowUnresolved, collect unresolved nodes attached to the module
99 /// in order to resolve cycles during \a finalize().
100 ///
101 /// If \p CU is given a value other than nullptr, then set \p CUNode to CU.
102 explicit DIBuilder(Module &M, bool AllowUnresolved = true,
103 DICompileUnit *CU = nullptr);
104 DIBuilder(const DIBuilder &) = delete;
105 DIBuilder &operator=(const DIBuilder &) = delete;
106
107 /// Construct any deferred debug info descriptors.
108 void finalize();
109
110 /// Finalize a specific subprogram - no new variables may be added to this
111 /// subprogram afterwards.
112 void finalizeSubprogram(DISubprogram *SP);
113
114 /// A CompileUnit provides an anchor for all debugging
115 /// information generated during this instance of compilation.
116 /// \param Lang Source programming language, eg. dwarf::DW_LANG_C99
117 /// \param File File info.
118 /// \param Producer Identify the producer of debugging information
119 /// and code. Usually this is a compiler
120 /// version string.
121 /// \param isOptimized A boolean flag which indicates whether optimization
122 /// is enabled or not.
123 /// \param Flags This string lists command line options. This
124 /// string is directly embedded in debug info
125 /// output which may be used by a tool
126 /// analyzing generated debugging information.
127 /// \param RV This indicates runtime version for languages like
128 /// Objective-C.
129 /// \param SplitName The name of the file that we'll split debug info
130 /// out into.
131 /// \param Kind The kind of debug information to generate.
132 /// \param DWOId The DWOId if this is a split skeleton compile unit.
133 /// \param SplitDebugInlining Whether to emit inline debug info.
134 /// \param DebugInfoForProfiling Whether to emit extra debug info for
135 /// profile collection.
Andrew Scull0372a572018-11-16 15:47:06 +0000136 /// \param NameTableKind Whether to emit .debug_gnu_pubnames,
137 /// .debug_pubnames, or no pubnames at all.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200138 /// \param SysRoot The clang system root (value of -isysroot).
139 /// \param SDK The SDK name. On Darwin, this is the last component
140 /// of the sysroot.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100141 DICompileUnit *
142 createCompileUnit(unsigned Lang, DIFile *File, StringRef Producer,
143 bool isOptimized, StringRef Flags, unsigned RV,
144 StringRef SplitName = StringRef(),
145 DICompileUnit::DebugEmissionKind Kind =
146 DICompileUnit::DebugEmissionKind::FullDebug,
147 uint64_t DWOId = 0, bool SplitDebugInlining = true,
148 bool DebugInfoForProfiling = false,
Andrew Scull0372a572018-11-16 15:47:06 +0000149 DICompileUnit::DebugNameTableKind NameTableKind =
Andrew Walbran16937d02019-10-22 13:54:20 +0100150 DICompileUnit::DebugNameTableKind::Default,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200151 bool RangesBaseAddress = false, StringRef SysRoot = {},
152 StringRef SDK = {});
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100153
154 /// Create a file descriptor to hold debugging information for a file.
155 /// \param Filename File name.
156 /// \param Directory Directory.
157 /// \param Checksum Optional checksum kind (e.g. CSK_MD5, CSK_SHA1, etc.)
158 /// and value.
159 /// \param Source Optional source text.
160 DIFile *
161 createFile(StringRef Filename, StringRef Directory,
162 Optional<DIFile::ChecksumInfo<StringRef>> Checksum = None,
163 Optional<StringRef> Source = None);
164
165 /// Create debugging information entry for a macro.
166 /// \param Parent Macro parent (could be nullptr).
167 /// \param Line Source line number where the macro is defined.
168 /// \param MacroType DW_MACINFO_define or DW_MACINFO_undef.
169 /// \param Name Macro name.
170 /// \param Value Macro value.
171 DIMacro *createMacro(DIMacroFile *Parent, unsigned Line, unsigned MacroType,
172 StringRef Name, StringRef Value = StringRef());
173
174 /// Create debugging information temporary entry for a macro file.
175 /// List of macro node direct children will be calculated by DIBuilder,
176 /// using the \p Parent relationship.
177 /// \param Parent Macro file parent (could be nullptr).
178 /// \param Line Source line number where the macro file is included.
179 /// \param File File descriptor containing the name of the macro file.
180 DIMacroFile *createTempMacroFile(DIMacroFile *Parent, unsigned Line,
181 DIFile *File);
182
183 /// Create a single enumerator value.
184 DIEnumerator *createEnumerator(StringRef Name, int64_t Val, bool IsUnsigned = false);
185
186 /// Create a DWARF unspecified type.
187 DIBasicType *createUnspecifiedType(StringRef Name);
188
189 /// Create C++11 nullptr type.
190 DIBasicType *createNullPtrType();
191
192 /// Create debugging information entry for a basic
193 /// type.
194 /// \param Name Type name.
195 /// \param SizeInBits Size of the type.
Andrew Scull0372a572018-11-16 15:47:06 +0000196 /// \param Encoding DWARF encoding code, e.g., dwarf::DW_ATE_float.
197 /// \param Flags Optional DWARF attributes, e.g., DW_AT_endianity.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100198 DIBasicType *createBasicType(StringRef Name, uint64_t SizeInBits,
Andrew Scull0372a572018-11-16 15:47:06 +0000199 unsigned Encoding,
200 DINode::DIFlags Flags = DINode::FlagZero);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100201
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200202 /// Create debugging information entry for a string
203 /// type.
204 /// \param Name Type name.
205 /// \param SizeInBits Size of the type.
206 DIStringType *createStringType(StringRef Name, uint64_t SizeInBits);
207
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 /// Create debugging information entry for a qualified
209 /// type, e.g. 'const int'.
210 /// \param Tag Tag identifing type, e.g. dwarf::TAG_volatile_type
211 /// \param FromTy Base Type.
212 DIDerivedType *createQualifiedType(unsigned Tag, DIType *FromTy);
213
214 /// Create debugging information entry for a pointer.
215 /// \param PointeeTy Type pointed by this pointer.
216 /// \param SizeInBits Size.
217 /// \param AlignInBits Alignment. (optional)
218 /// \param DWARFAddressSpace DWARF address space. (optional)
219 /// \param Name Pointer type name. (optional)
220 DIDerivedType *createPointerType(DIType *PointeeTy, uint64_t SizeInBits,
221 uint32_t AlignInBits = 0,
222 Optional<unsigned> DWARFAddressSpace =
223 None,
224 StringRef Name = "");
225
226 /// Create debugging information entry for a pointer to member.
227 /// \param PointeeTy Type pointed to by this pointer.
228 /// \param SizeInBits Size.
229 /// \param AlignInBits Alignment. (optional)
230 /// \param Class Type for which this pointer points to members of.
231 DIDerivedType *
232 createMemberPointerType(DIType *PointeeTy, DIType *Class,
233 uint64_t SizeInBits, uint32_t AlignInBits = 0,
234 DINode::DIFlags Flags = DINode::FlagZero);
235
236 /// Create debugging information entry for a c++
237 /// style reference or rvalue reference type.
238 DIDerivedType *createReferenceType(unsigned Tag, DIType *RTy,
239 uint64_t SizeInBits = 0,
240 uint32_t AlignInBits = 0,
241 Optional<unsigned> DWARFAddressSpace =
242 None);
243
244 /// Create debugging information entry for a typedef.
245 /// \param Ty Original type.
246 /// \param Name Typedef name.
247 /// \param File File where this type is defined.
248 /// \param LineNo Line number.
249 /// \param Context The surrounding context for the typedef.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200250 /// \param AlignInBits Alignment. (optional)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100251 DIDerivedType *createTypedef(DIType *Ty, StringRef Name, DIFile *File,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200252 unsigned LineNo, DIScope *Context,
253 uint32_t AlignInBits = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100254
255 /// Create debugging information entry for a 'friend'.
256 DIDerivedType *createFriend(DIType *Ty, DIType *FriendTy);
257
258 /// Create debugging information entry to establish
259 /// inheritance relationship between two types.
260 /// \param Ty Original type.
261 /// \param BaseTy Base type. Ty is inherits from base.
262 /// \param BaseOffset Base offset.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100263 /// \param VBPtrOffset Virtual base pointer offset.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100264 /// \param Flags Flags to describe inheritance attribute,
265 /// e.g. private
266 DIDerivedType *createInheritance(DIType *Ty, DIType *BaseTy,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100267 uint64_t BaseOffset, uint32_t VBPtrOffset,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100268 DINode::DIFlags Flags);
269
270 /// Create debugging information entry for a member.
271 /// \param Scope Member scope.
272 /// \param Name Member name.
273 /// \param File File where this member is defined.
274 /// \param LineNo Line number.
275 /// \param SizeInBits Member size.
276 /// \param AlignInBits Member alignment.
277 /// \param OffsetInBits Member offset.
278 /// \param Flags Flags to encode member attribute, e.g. private
279 /// \param Ty Parent type.
280 DIDerivedType *createMemberType(DIScope *Scope, StringRef Name,
281 DIFile *File, unsigned LineNo,
282 uint64_t SizeInBits,
283 uint32_t AlignInBits,
284 uint64_t OffsetInBits,
285 DINode::DIFlags Flags, DIType *Ty);
286
287 /// Create debugging information entry for a variant. A variant
288 /// normally should be a member of a variant part.
289 /// \param Scope Member scope.
290 /// \param Name Member name.
291 /// \param File File where this member is defined.
292 /// \param LineNo Line number.
293 /// \param SizeInBits Member size.
294 /// \param AlignInBits Member alignment.
295 /// \param OffsetInBits Member offset.
296 /// \param Flags Flags to encode member attribute, e.g. private
297 /// \param Discriminant The discriminant for this branch; null for
298 /// the default branch
299 /// \param Ty Parent type.
300 DIDerivedType *createVariantMemberType(DIScope *Scope, StringRef Name,
301 DIFile *File, unsigned LineNo,
302 uint64_t SizeInBits,
303 uint32_t AlignInBits,
304 uint64_t OffsetInBits,
305 Constant *Discriminant,
306 DINode::DIFlags Flags, DIType *Ty);
307
308 /// Create debugging information entry for a bit field member.
309 /// \param Scope Member scope.
310 /// \param Name Member name.
311 /// \param File File where this member is defined.
312 /// \param LineNo Line number.
313 /// \param SizeInBits Member size.
314 /// \param OffsetInBits Member offset.
315 /// \param StorageOffsetInBits Member storage offset.
316 /// \param Flags Flags to encode member attribute.
317 /// \param Ty Parent type.
318 DIDerivedType *createBitFieldMemberType(
319 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo,
320 uint64_t SizeInBits, uint64_t OffsetInBits,
321 uint64_t StorageOffsetInBits, DINode::DIFlags Flags, DIType *Ty);
322
323 /// Create debugging information entry for a
324 /// C++ static data member.
325 /// \param Scope Member scope.
326 /// \param Name Member name.
327 /// \param File File where this member is declared.
328 /// \param LineNo Line number.
329 /// \param Ty Type of the static member.
330 /// \param Flags Flags to encode member attribute, e.g. private.
331 /// \param Val Const initializer of the member.
332 /// \param AlignInBits Member alignment.
333 DIDerivedType *createStaticMemberType(DIScope *Scope, StringRef Name,
334 DIFile *File, unsigned LineNo,
335 DIType *Ty, DINode::DIFlags Flags,
336 Constant *Val,
337 uint32_t AlignInBits = 0);
338
339 /// Create debugging information entry for Objective-C
340 /// instance variable.
341 /// \param Name Member name.
342 /// \param File File where this member is defined.
343 /// \param LineNo Line number.
344 /// \param SizeInBits Member size.
345 /// \param AlignInBits Member alignment.
346 /// \param OffsetInBits Member offset.
347 /// \param Flags Flags to encode member attribute, e.g. private
348 /// \param Ty Parent type.
349 /// \param PropertyNode Property associated with this ivar.
350 DIDerivedType *createObjCIVar(StringRef Name, DIFile *File, unsigned LineNo,
351 uint64_t SizeInBits, uint32_t AlignInBits,
352 uint64_t OffsetInBits, DINode::DIFlags Flags,
353 DIType *Ty, MDNode *PropertyNode);
354
355 /// Create debugging information entry for Objective-C
356 /// property.
357 /// \param Name Property name.
358 /// \param File File where this property is defined.
359 /// \param LineNumber Line number.
360 /// \param GetterName Name of the Objective C property getter selector.
361 /// \param SetterName Name of the Objective C property setter selector.
362 /// \param PropertyAttributes Objective C property attributes.
363 /// \param Ty Type.
364 DIObjCProperty *createObjCProperty(StringRef Name, DIFile *File,
365 unsigned LineNumber,
366 StringRef GetterName,
367 StringRef SetterName,
368 unsigned PropertyAttributes, DIType *Ty);
369
370 /// Create debugging information entry for a class.
371 /// \param Scope Scope in which this class is defined.
372 /// \param Name class name.
373 /// \param File File where this member is defined.
374 /// \param LineNumber Line number.
375 /// \param SizeInBits Member size.
376 /// \param AlignInBits Member alignment.
377 /// \param OffsetInBits Member offset.
378 /// \param Flags Flags to encode member attribute, e.g. private
379 /// \param Elements class members.
380 /// \param VTableHolder Debug info of the base class that contains vtable
381 /// for this type. This is used in
382 /// DW_AT_containing_type. See DWARF documentation
383 /// for more info.
384 /// \param TemplateParms Template type parameters.
385 /// \param UniqueIdentifier A unique identifier for the class.
386 DICompositeType *createClassType(
387 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
388 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
389 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
390 DIType *VTableHolder = nullptr, MDNode *TemplateParms = nullptr,
391 StringRef UniqueIdentifier = "");
392
393 /// Create debugging information entry for a struct.
394 /// \param Scope Scope in which this struct is defined.
395 /// \param Name Struct name.
396 /// \param File File where this member is defined.
397 /// \param LineNumber Line number.
398 /// \param SizeInBits Member size.
399 /// \param AlignInBits Member alignment.
400 /// \param Flags Flags to encode member attribute, e.g. private
401 /// \param Elements Struct elements.
402 /// \param RunTimeLang Optional parameter, Objective-C runtime version.
403 /// \param UniqueIdentifier A unique identifier for the struct.
404 DICompositeType *createStructType(
405 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
406 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
407 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang = 0,
408 DIType *VTableHolder = nullptr, StringRef UniqueIdentifier = "");
409
410 /// Create debugging information entry for an union.
411 /// \param Scope Scope in which this union is defined.
412 /// \param Name Union name.
413 /// \param File File where this member is defined.
414 /// \param LineNumber Line number.
415 /// \param SizeInBits Member size.
416 /// \param AlignInBits Member alignment.
417 /// \param Flags Flags to encode member attribute, e.g. private
418 /// \param Elements Union elements.
419 /// \param RunTimeLang Optional parameter, Objective-C runtime version.
420 /// \param UniqueIdentifier A unique identifier for the union.
421 DICompositeType *createUnionType(DIScope *Scope, StringRef Name,
422 DIFile *File, unsigned LineNumber,
423 uint64_t SizeInBits, uint32_t AlignInBits,
424 DINode::DIFlags Flags,
425 DINodeArray Elements,
426 unsigned RunTimeLang = 0,
427 StringRef UniqueIdentifier = "");
428
429 /// Create debugging information entry for a variant part. A
430 /// variant part normally has a discriminator (though this is not
431 /// required) and a number of variant children.
432 /// \param Scope Scope in which this union is defined.
433 /// \param Name Union name.
434 /// \param File File where this member is defined.
435 /// \param LineNumber Line number.
436 /// \param SizeInBits Member size.
437 /// \param AlignInBits Member alignment.
438 /// \param Flags Flags to encode member attribute, e.g. private
439 /// \param Discriminator Discriminant member
440 /// \param Elements Variant elements.
441 /// \param UniqueIdentifier A unique identifier for the union.
442 DICompositeType *createVariantPart(DIScope *Scope, StringRef Name,
443 DIFile *File, unsigned LineNumber,
444 uint64_t SizeInBits, uint32_t AlignInBits,
445 DINode::DIFlags Flags,
446 DIDerivedType *Discriminator,
447 DINodeArray Elements,
448 StringRef UniqueIdentifier = "");
449
450 /// Create debugging information for template
451 /// type parameter.
452 /// \param Scope Scope in which this type is defined.
453 /// \param Name Type parameter name.
454 /// \param Ty Parameter type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200455 /// \param IsDefault Parameter is default or not
456 DITemplateTypeParameter *createTemplateTypeParameter(DIScope *Scope,
457 StringRef Name,
458 DIType *Ty,
459 bool IsDefault);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100460
461 /// Create debugging information for template
462 /// value parameter.
463 /// \param Scope Scope in which this type is defined.
464 /// \param Name Value parameter name.
465 /// \param Ty Parameter type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200466 /// \param IsDefault Parameter is default or not
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100467 /// \param Val Constant parameter value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200468 DITemplateValueParameter *
469 createTemplateValueParameter(DIScope *Scope, StringRef Name, DIType *Ty,
470 bool IsDefault, Constant *Val);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100471
472 /// Create debugging information for a template template parameter.
473 /// \param Scope Scope in which this type is defined.
474 /// \param Name Value parameter name.
475 /// \param Ty Parameter type.
476 /// \param Val The fully qualified name of the template.
477 DITemplateValueParameter *createTemplateTemplateParameter(DIScope *Scope,
478 StringRef Name,
479 DIType *Ty,
480 StringRef Val);
481
482 /// Create debugging information for a template parameter pack.
483 /// \param Scope Scope in which this type is defined.
484 /// \param Name Value parameter name.
485 /// \param Ty Parameter type.
486 /// \param Val An array of types in the pack.
487 DITemplateValueParameter *createTemplateParameterPack(DIScope *Scope,
488 StringRef Name,
489 DIType *Ty,
490 DINodeArray Val);
491
492 /// Create debugging information entry for an array.
493 /// \param Size Array size.
494 /// \param AlignInBits Alignment.
495 /// \param Ty Element type.
496 /// \param Subscripts Subscripts.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200497 /// \param DataLocation The location of the raw data of a descriptor-based
498 /// Fortran array, either a DIExpression* or
499 /// a DIVariable*.
500 /// \param Associated The associated attribute of a descriptor-based
501 /// Fortran array, either a DIExpression* or
502 /// a DIVariable*.
503 /// \param Allocated The allocated attribute of a descriptor-based
504 /// Fortran array, either a DIExpression* or
505 /// a DIVariable*.
506 /// \param Rank The rank attribute of a descriptor-based
507 /// Fortran array, either a DIExpression* or
508 /// a DIVariable*.
509 DICompositeType *createArrayType(
510 uint64_t Size, uint32_t AlignInBits, DIType *Ty, DINodeArray Subscripts,
511 PointerUnion<DIExpression *, DIVariable *> DataLocation = nullptr,
512 PointerUnion<DIExpression *, DIVariable *> Associated = nullptr,
513 PointerUnion<DIExpression *, DIVariable *> Allocated = nullptr,
514 PointerUnion<DIExpression *, DIVariable *> Rank = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100515
516 /// Create debugging information entry for a vector type.
517 /// \param Size Array size.
518 /// \param AlignInBits Alignment.
519 /// \param Ty Element type.
520 /// \param Subscripts Subscripts.
521 DICompositeType *createVectorType(uint64_t Size, uint32_t AlignInBits,
522 DIType *Ty, DINodeArray Subscripts);
523
524 /// Create debugging information entry for an
525 /// enumeration.
526 /// \param Scope Scope in which this enumeration is defined.
527 /// \param Name Union name.
528 /// \param File File where this member is defined.
529 /// \param LineNumber Line number.
530 /// \param SizeInBits Member size.
531 /// \param AlignInBits Member alignment.
532 /// \param Elements Enumeration elements.
533 /// \param UnderlyingType Underlying type of a C++11/ObjC fixed enum.
534 /// \param UniqueIdentifier A unique identifier for the enum.
Andrew Walbran16937d02019-10-22 13:54:20 +0100535 /// \param IsScoped Boolean flag indicate if this is C++11/ObjC 'enum class'.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100536 DICompositeType *createEnumerationType(
537 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
538 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
Andrew Walbran16937d02019-10-22 13:54:20 +0100539 DIType *UnderlyingType, StringRef UniqueIdentifier = "", bool IsScoped = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100540
541 /// Create subroutine type.
542 /// \param ParameterTypes An array of subroutine parameter types. This
543 /// includes return type at 0th index.
544 /// \param Flags E.g.: LValueReference.
545 /// These flags are used to emit dwarf attributes.
546 /// \param CC Calling convention, e.g. dwarf::DW_CC_normal
547 DISubroutineType *
548 createSubroutineType(DITypeRefArray ParameterTypes,
549 DINode::DIFlags Flags = DINode::FlagZero,
550 unsigned CC = 0);
551
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100552 /// Create a distinct clone of \p SP with FlagArtificial set.
553 static DISubprogram *createArtificialSubprogram(DISubprogram *SP);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100554
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100555 /// Create a uniqued clone of \p Ty with FlagArtificial set.
556 static DIType *createArtificialType(DIType *Ty);
557
558 /// Create a uniqued clone of \p Ty with FlagObjectPointer and
559 /// FlagArtificial set.
560 static DIType *createObjectPointerType(DIType *Ty);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100561
562 /// Create a permanent forward-declared type.
563 DICompositeType *createForwardDecl(unsigned Tag, StringRef Name,
564 DIScope *Scope, DIFile *F, unsigned Line,
565 unsigned RuntimeLang = 0,
566 uint64_t SizeInBits = 0,
567 uint32_t AlignInBits = 0,
568 StringRef UniqueIdentifier = "");
569
570 /// Create a temporary forward-declared type.
571 DICompositeType *createReplaceableCompositeType(
572 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
573 unsigned RuntimeLang = 0, uint64_t SizeInBits = 0,
574 uint32_t AlignInBits = 0, DINode::DIFlags Flags = DINode::FlagFwdDecl,
575 StringRef UniqueIdentifier = "");
576
577 /// Retain DIScope* in a module even if it is not referenced
578 /// through debug info anchors.
579 void retainType(DIScope *T);
580
581 /// Create unspecified parameter type
582 /// for a subroutine type.
583 DIBasicType *createUnspecifiedParameter();
584
585 /// Get a DINodeArray, create one if required.
586 DINodeArray getOrCreateArray(ArrayRef<Metadata *> Elements);
587
588 /// Get a DIMacroNodeArray, create one if required.
589 DIMacroNodeArray getOrCreateMacroArray(ArrayRef<Metadata *> Elements);
590
591 /// Get a DITypeRefArray, create one if required.
592 DITypeRefArray getOrCreateTypeArray(ArrayRef<Metadata *> Elements);
593
594 /// Create a descriptor for a value range. This
595 /// implicitly uniques the values returned.
596 DISubrange *getOrCreateSubrange(int64_t Lo, int64_t Count);
597 DISubrange *getOrCreateSubrange(int64_t Lo, Metadata *CountNode);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200598 DISubrange *getOrCreateSubrange(Metadata *Count, Metadata *LowerBound,
599 Metadata *UpperBound, Metadata *Stride);
600
601 DIGenericSubrange *
602 getOrCreateGenericSubrange(DIGenericSubrange::BoundType Count,
603 DIGenericSubrange::BoundType LowerBound,
604 DIGenericSubrange::BoundType UpperBound,
605 DIGenericSubrange::BoundType Stride);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100606
607 /// Create a new descriptor for the specified variable.
608 /// \param Context Variable scope.
609 /// \param Name Name of the variable.
610 /// \param LinkageName Mangled name of the variable.
611 /// \param File File where this variable is defined.
612 /// \param LineNo Line number.
613 /// \param Ty Variable Type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200614 /// \param IsLocalToUnit Boolean flag indicate whether this variable is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100615 /// externally visible or not.
616 /// \param Expr The location of the global relative to the attached
617 /// GlobalVariable.
618 /// \param Decl Reference to the corresponding declaration.
619 /// \param AlignInBits Variable alignment(or 0 if no alignment attr was
620 /// specified)
621 DIGlobalVariableExpression *createGlobalVariableExpression(
622 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200623 unsigned LineNo, DIType *Ty, bool IsLocalToUnit, bool isDefined = true,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100624 DIExpression *Expr = nullptr, MDNode *Decl = nullptr,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200625 MDTuple *TemplateParams = nullptr, uint32_t AlignInBits = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100626
627 /// Identical to createGlobalVariable
628 /// except that the resulting DbgNode is temporary and meant to be RAUWed.
629 DIGlobalVariable *createTempGlobalVariableFwdDecl(
630 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200631 unsigned LineNo, DIType *Ty, bool IsLocalToUnit, MDNode *Decl = nullptr,
632 MDTuple *TemplateParams= nullptr, uint32_t AlignInBits = 0);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100633
634 /// Create a new descriptor for an auto variable. This is a local variable
635 /// that is not a subprogram parameter.
636 ///
637 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
638 /// leads to a \a DISubprogram.
639 ///
640 /// If \c AlwaysPreserve, this variable will be referenced from its
641 /// containing subprogram, and will survive some optimizations.
642 DILocalVariable *
643 createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File,
644 unsigned LineNo, DIType *Ty, bool AlwaysPreserve = false,
645 DINode::DIFlags Flags = DINode::FlagZero,
646 uint32_t AlignInBits = 0);
647
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100648 /// Create a new descriptor for an label.
649 ///
650 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
651 /// leads to a \a DISubprogram.
652 DILabel *
653 createLabel(DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo,
654 bool AlwaysPreserve = false);
655
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100656 /// Create a new descriptor for a parameter variable.
657 ///
658 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
659 /// leads to a \a DISubprogram.
660 ///
661 /// \c ArgNo is the index (starting from \c 1) of this variable in the
662 /// subprogram parameters. \c ArgNo should not conflict with other
663 /// parameters of the same subprogram.
664 ///
665 /// If \c AlwaysPreserve, this variable will be referenced from its
666 /// containing subprogram, and will survive some optimizations.
667 DILocalVariable *
668 createParameterVariable(DIScope *Scope, StringRef Name, unsigned ArgNo,
669 DIFile *File, unsigned LineNo, DIType *Ty,
670 bool AlwaysPreserve = false,
671 DINode::DIFlags Flags = DINode::FlagZero);
672
673 /// Create a new descriptor for the specified
674 /// variable which has a complex address expression for its address.
675 /// \param Addr An array of complex address operations.
676 DIExpression *createExpression(ArrayRef<uint64_t> Addr = None);
677 DIExpression *createExpression(ArrayRef<int64_t> Addr);
678
679 /// Create an expression for a variable that does not have an address, but
680 /// does have a constant value.
681 DIExpression *createConstantValueExpression(uint64_t Val) {
682 return DIExpression::get(
683 VMContext, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_stack_value});
684 }
685
686 /// Create a new descriptor for the specified subprogram.
687 /// See comments in DISubprogram* for descriptions of these fields.
688 /// \param Scope Function scope.
689 /// \param Name Function name.
690 /// \param LinkageName Mangled function name.
691 /// \param File File where this variable is defined.
692 /// \param LineNo Line number.
693 /// \param Ty Function type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100694 /// \param ScopeLine Set to the beginning of the scope this starts
695 /// \param Flags e.g. is this function prototyped or not.
696 /// These flags are used to emit dwarf attributes.
Andrew Walbran16937d02019-10-22 13:54:20 +0100697 /// \param SPFlags Additional flags specific to subprograms.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100698 /// \param TParams Function template parameters.
699 /// \param ThrownTypes Exception types this function may throw.
Andrew Walbran16937d02019-10-22 13:54:20 +0100700 DISubprogram *
701 createFunction(DIScope *Scope, StringRef Name, StringRef LinkageName,
702 DIFile *File, unsigned LineNo, DISubroutineType *Ty,
703 unsigned ScopeLine, DINode::DIFlags Flags = DINode::FlagZero,
704 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero,
705 DITemplateParameterArray TParams = nullptr,
706 DISubprogram *Decl = nullptr,
707 DITypeArray ThrownTypes = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100708
709 /// Identical to createFunction,
710 /// except that the resulting DbgNode is meant to be RAUWed.
711 DISubprogram *createTempFunctionFwdDecl(
712 DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File,
Andrew Walbran16937d02019-10-22 13:54:20 +0100713 unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine,
714 DINode::DIFlags Flags = DINode::FlagZero,
715 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100716 DITemplateParameterArray TParams = nullptr,
717 DISubprogram *Decl = nullptr, DITypeArray ThrownTypes = nullptr);
718
719 /// Create a new descriptor for the specified C++ method.
720 /// See comments in \a DISubprogram* for descriptions of these fields.
721 /// \param Scope Function scope.
722 /// \param Name Function name.
723 /// \param LinkageName Mangled function name.
724 /// \param File File where this variable is defined.
725 /// \param LineNo Line number.
726 /// \param Ty Function type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100727 /// \param VTableIndex Index no of this method in virtual table, or -1u if
728 /// unrepresentable.
729 /// \param ThisAdjustment
730 /// MS ABI-specific adjustment of 'this' that occurs
731 /// in the prologue.
732 /// \param VTableHolder Type that holds vtable.
733 /// \param Flags e.g. is this function prototyped or not.
734 /// This flags are used to emit dwarf attributes.
Andrew Walbran16937d02019-10-22 13:54:20 +0100735 /// \param SPFlags Additional flags specific to subprograms.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100736 /// \param TParams Function template parameters.
737 /// \param ThrownTypes Exception types this function may throw.
Andrew Walbran16937d02019-10-22 13:54:20 +0100738 DISubprogram *
739 createMethod(DIScope *Scope, StringRef Name, StringRef LinkageName,
740 DIFile *File, unsigned LineNo, DISubroutineType *Ty,
741 unsigned VTableIndex = 0, int ThisAdjustment = 0,
742 DIType *VTableHolder = nullptr,
743 DINode::DIFlags Flags = DINode::FlagZero,
744 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagZero,
745 DITemplateParameterArray TParams = nullptr,
746 DITypeArray ThrownTypes = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100747
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100748 /// Create common block entry for a Fortran common block.
749 /// \param Scope Scope of this common block.
750 /// \param decl Global variable declaration.
751 /// \param Name The name of this common block.
752 /// \param File The file this common block is defined.
753 /// \param LineNo Line number.
754 DICommonBlock *createCommonBlock(DIScope *Scope, DIGlobalVariable *decl,
755 StringRef Name, DIFile *File,
756 unsigned LineNo);
757
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100758 /// This creates new descriptor for a namespace with the specified
759 /// parent scope.
760 /// \param Scope Namespace scope
761 /// \param Name Name of this namespace
762 /// \param ExportSymbols True for C++ inline namespaces.
763 DINamespace *createNameSpace(DIScope *Scope, StringRef Name,
764 bool ExportSymbols);
765
766 /// This creates new descriptor for a module with the specified
767 /// parent scope.
768 /// \param Scope Parent scope
769 /// \param Name Name of this module
770 /// \param ConfigurationMacros
771 /// A space-separated shell-quoted list of -D macro
772 /// definitions as they would appear on a command line.
773 /// \param IncludePath The path to the module map file.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200774 /// \param APINotesFile The path to an API notes file for this module.
775 /// \param File Source file of the module.
776 /// Used for Fortran modules.
777 /// \param LineNo Source line number of the module.
778 /// Used for Fortran modules.
779 /// \param IsDecl This is a module declaration; default to false;
780 /// when set to true, only Scope and Name are required
781 /// as this entry is just a hint for the debugger to find
782 /// the corresponding definition in the global scope.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100783 DIModule *createModule(DIScope *Scope, StringRef Name,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200784 StringRef ConfigurationMacros, StringRef IncludePath,
785 StringRef APINotesFile = {}, DIFile *File = nullptr,
786 unsigned LineNo = 0, bool IsDecl = false);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100787
788 /// This creates a descriptor for a lexical block with a new file
789 /// attached. This merely extends the existing
790 /// lexical block as it crosses a file.
791 /// \param Scope Lexical block.
792 /// \param File Source file.
793 /// \param Discriminator DWARF path discriminator value.
794 DILexicalBlockFile *createLexicalBlockFile(DIScope *Scope, DIFile *File,
795 unsigned Discriminator = 0);
796
797 /// This creates a descriptor for a lexical block with the
798 /// specified parent context.
799 /// \param Scope Parent lexical scope.
800 /// \param File Source file.
801 /// \param Line Line number.
802 /// \param Col Column number.
803 DILexicalBlock *createLexicalBlock(DIScope *Scope, DIFile *File,
804 unsigned Line, unsigned Col);
805
806 /// Create a descriptor for an imported module.
807 /// \param Context The scope this module is imported into
808 /// \param NS The namespace being imported here.
809 /// \param File File where the declaration is located.
810 /// \param Line Line number of the declaration.
811 DIImportedEntity *createImportedModule(DIScope *Context, DINamespace *NS,
812 DIFile *File, unsigned Line);
813
814 /// Create a descriptor for an imported module.
815 /// \param Context The scope this module is imported into.
816 /// \param NS An aliased namespace.
817 /// \param File File where the declaration is located.
818 /// \param Line Line number of the declaration.
819 DIImportedEntity *createImportedModule(DIScope *Context,
820 DIImportedEntity *NS, DIFile *File,
821 unsigned Line);
822
823 /// Create a descriptor for an imported module.
824 /// \param Context The scope this module is imported into.
825 /// \param M The module being imported here
826 /// \param File File where the declaration is located.
827 /// \param Line Line number of the declaration.
828 DIImportedEntity *createImportedModule(DIScope *Context, DIModule *M,
829 DIFile *File, unsigned Line);
830
831 /// Create a descriptor for an imported function.
832 /// \param Context The scope this module is imported into.
833 /// \param Decl The declaration (or definition) of a function, type, or
834 /// variable.
835 /// \param File File where the declaration is located.
836 /// \param Line Line number of the declaration.
837 DIImportedEntity *createImportedDeclaration(DIScope *Context, DINode *Decl,
838 DIFile *File, unsigned Line,
839 StringRef Name = "");
840
841 /// Insert a new llvm.dbg.declare intrinsic call.
842 /// \param Storage llvm::Value of the variable
843 /// \param VarInfo Variable's debug info descriptor.
844 /// \param Expr A complex location expression.
845 /// \param DL Debug info location.
846 /// \param InsertAtEnd Location for the new intrinsic.
847 Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
848 DIExpression *Expr, const DILocation *DL,
849 BasicBlock *InsertAtEnd);
850
851 /// Insert a new llvm.dbg.declare intrinsic call.
852 /// \param Storage llvm::Value of the variable
853 /// \param VarInfo Variable's debug info descriptor.
854 /// \param Expr A complex location expression.
855 /// \param DL Debug info location.
856 /// \param InsertBefore Location for the new intrinsic.
857 Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
858 DIExpression *Expr, const DILocation *DL,
859 Instruction *InsertBefore);
860
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100861 /// Insert a new llvm.dbg.label intrinsic call.
862 /// \param LabelInfo Label's debug info descriptor.
863 /// \param DL Debug info location.
864 /// \param InsertBefore Location for the new intrinsic.
865 Instruction *insertLabel(DILabel *LabelInfo, const DILocation *DL,
866 Instruction *InsertBefore);
867
868 /// Insert a new llvm.dbg.label intrinsic call.
869 /// \param LabelInfo Label's debug info descriptor.
870 /// \param DL Debug info location.
871 /// \param InsertAtEnd Location for the new intrinsic.
872 Instruction *insertLabel(DILabel *LabelInfo, const DILocation *DL,
873 BasicBlock *InsertAtEnd);
874
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100875 /// Insert a new llvm.dbg.value intrinsic call.
876 /// \param Val llvm::Value of the variable
877 /// \param VarInfo Variable's debug info descriptor.
878 /// \param Expr A complex location expression.
879 /// \param DL Debug info location.
880 /// \param InsertAtEnd Location for the new intrinsic.
881 Instruction *insertDbgValueIntrinsic(llvm::Value *Val,
882 DILocalVariable *VarInfo,
883 DIExpression *Expr,
884 const DILocation *DL,
885 BasicBlock *InsertAtEnd);
886
887 /// Insert a new llvm.dbg.value intrinsic call.
888 /// \param Val llvm::Value of the variable
889 /// \param VarInfo Variable's debug info descriptor.
890 /// \param Expr A complex location expression.
891 /// \param DL Debug info location.
892 /// \param InsertBefore Location for the new intrinsic.
893 Instruction *insertDbgValueIntrinsic(llvm::Value *Val,
894 DILocalVariable *VarInfo,
895 DIExpression *Expr,
896 const DILocation *DL,
897 Instruction *InsertBefore);
898
899 /// Replace the vtable holder in the given type.
900 ///
901 /// If this creates a self reference, it may orphan some unresolved cycles
902 /// in the operands of \c T, so \a DIBuilder needs to track that.
903 void replaceVTableHolder(DICompositeType *&T,
904 DIType *VTableHolder);
905
906 /// Replace arrays on a composite type.
907 ///
908 /// If \c T is resolved, but the arrays aren't -- which can happen if \c T
909 /// has a self-reference -- \a DIBuilder needs to track the array to
910 /// resolve cycles.
911 void replaceArrays(DICompositeType *&T, DINodeArray Elements,
912 DINodeArray TParams = DINodeArray());
913
914 /// Replace a temporary node.
915 ///
916 /// Call \a MDNode::replaceAllUsesWith() on \c N, replacing it with \c
917 /// Replacement.
918 ///
919 /// If \c Replacement is the same as \c N.get(), instead call \a
920 /// MDNode::replaceWithUniqued(). In this case, the uniqued node could
921 /// have a different address, so we return the final address.
922 template <class NodeTy>
923 NodeTy *replaceTemporary(TempMDNode &&N, NodeTy *Replacement) {
924 if (N.get() == Replacement)
925 return cast<NodeTy>(MDNode::replaceWithUniqued(std::move(N)));
926
927 N->replaceAllUsesWith(Replacement);
928 return Replacement;
929 }
930 };
931
932 // Create wrappers for C Binding types (see CBindingWrapping.h).
933 DEFINE_ISA_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
934
935} // end namespace llvm
936
937#endif // LLVM_IR_DIBUILDER_H