blob: aa8a8ec19ac09b5e1c57914e47c8454edb4d4c59 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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_IR_DIBUILDER_H
16#define LLVM_IR_DIBUILDER_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/IR/DebugInfo.h"
26#include "llvm/IR/DebugInfoMetadata.h"
27#include "llvm/IR/TrackingMDRef.h"
28#include "llvm/Support/Casting.h"
29#include <algorithm>
30#include <cstdint>
31
32namespace llvm {
33
34 class BasicBlock;
35 class Constant;
36 class Function;
37 class Instruction;
38 class LLVMContext;
39 class Module;
40 class Value;
41
42 class DIBuilder {
43 Module &M;
44 LLVMContext &VMContext;
45
46 DICompileUnit *CUNode; ///< The one compile unit created by this DIBuiler.
47 Function *DeclareFn; ///< llvm.dbg.declare
48 Function *ValueFn; ///< llvm.dbg.value
49
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
72 /// Create a temporary.
73 ///
74 /// Create an \a temporary node and track it in \a UnresolvedNodes.
75 void trackIfUnresolved(MDNode *N);
76
77 /// Internal helper for insertDeclare.
78 Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
79 DIExpression *Expr, const DILocation *DL,
80 BasicBlock *InsertBB, Instruction *InsertBefore);
81
82 /// Internal helper for insertDbgValueIntrinsic.
83 Instruction *
84 insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo,
85 DIExpression *Expr, const DILocation *DL,
86 BasicBlock *InsertBB, Instruction *InsertBefore);
87
88 public:
89 /// Construct a builder for a module.
90 ///
91 /// If \c AllowUnresolved, collect unresolved nodes attached to the module
92 /// in order to resolve cycles during \a finalize().
93 ///
94 /// If \p CU is given a value other than nullptr, then set \p CUNode to CU.
95 explicit DIBuilder(Module &M, bool AllowUnresolved = true,
96 DICompileUnit *CU = nullptr);
97 DIBuilder(const DIBuilder &) = delete;
98 DIBuilder &operator=(const DIBuilder &) = delete;
99
100 /// Construct any deferred debug info descriptors.
101 void finalize();
102
103 /// Finalize a specific subprogram - no new variables may be added to this
104 /// subprogram afterwards.
105 void finalizeSubprogram(DISubprogram *SP);
106
107 /// A CompileUnit provides an anchor for all debugging
108 /// information generated during this instance of compilation.
109 /// \param Lang Source programming language, eg. dwarf::DW_LANG_C99
110 /// \param File File info.
111 /// \param Producer Identify the producer of debugging information
112 /// and code. Usually this is a compiler
113 /// version string.
114 /// \param isOptimized A boolean flag which indicates whether optimization
115 /// is enabled or not.
116 /// \param Flags This string lists command line options. This
117 /// string is directly embedded in debug info
118 /// output which may be used by a tool
119 /// analyzing generated debugging information.
120 /// \param RV This indicates runtime version for languages like
121 /// Objective-C.
122 /// \param SplitName The name of the file that we'll split debug info
123 /// out into.
124 /// \param Kind The kind of debug information to generate.
125 /// \param DWOId The DWOId if this is a split skeleton compile unit.
126 /// \param SplitDebugInlining Whether to emit inline debug info.
127 /// \param DebugInfoForProfiling Whether to emit extra debug info for
128 /// profile collection.
129 /// \param GnuPubnames Whether to emit .debug_gnu_pubnames section instead
130 /// of .debug_pubnames.
131 DICompileUnit *
132 createCompileUnit(unsigned Lang, DIFile *File, StringRef Producer,
133 bool isOptimized, StringRef Flags, unsigned RV,
134 StringRef SplitName = StringRef(),
135 DICompileUnit::DebugEmissionKind Kind =
136 DICompileUnit::DebugEmissionKind::FullDebug,
137 uint64_t DWOId = 0, bool SplitDebugInlining = true,
138 bool DebugInfoForProfiling = false,
139 bool GnuPubnames = false);
140
141 /// Create a file descriptor to hold debugging information for a file.
142 /// \param Filename File name.
143 /// \param Directory Directory.
144 /// \param Checksum Optional checksum kind (e.g. CSK_MD5, CSK_SHA1, etc.)
145 /// and value.
146 /// \param Source Optional source text.
147 DIFile *
148 createFile(StringRef Filename, StringRef Directory,
149 Optional<DIFile::ChecksumInfo<StringRef>> Checksum = None,
150 Optional<StringRef> Source = None);
151
152 /// Create debugging information entry for a macro.
153 /// \param Parent Macro parent (could be nullptr).
154 /// \param Line Source line number where the macro is defined.
155 /// \param MacroType DW_MACINFO_define or DW_MACINFO_undef.
156 /// \param Name Macro name.
157 /// \param Value Macro value.
158 DIMacro *createMacro(DIMacroFile *Parent, unsigned Line, unsigned MacroType,
159 StringRef Name, StringRef Value = StringRef());
160
161 /// Create debugging information temporary entry for a macro file.
162 /// List of macro node direct children will be calculated by DIBuilder,
163 /// using the \p Parent relationship.
164 /// \param Parent Macro file parent (could be nullptr).
165 /// \param Line Source line number where the macro file is included.
166 /// \param File File descriptor containing the name of the macro file.
167 DIMacroFile *createTempMacroFile(DIMacroFile *Parent, unsigned Line,
168 DIFile *File);
169
170 /// Create a single enumerator value.
171 DIEnumerator *createEnumerator(StringRef Name, int64_t Val, bool IsUnsigned = false);
172
173 /// Create a DWARF unspecified type.
174 DIBasicType *createUnspecifiedType(StringRef Name);
175
176 /// Create C++11 nullptr type.
177 DIBasicType *createNullPtrType();
178
179 /// Create debugging information entry for a basic
180 /// type.
181 /// \param Name Type name.
182 /// \param SizeInBits Size of the type.
183 /// \param Encoding DWARF encoding code, e.g. dwarf::DW_ATE_float.
184 DIBasicType *createBasicType(StringRef Name, uint64_t SizeInBits,
185 unsigned Encoding);
186
187 /// Create debugging information entry for a qualified
188 /// type, e.g. 'const int'.
189 /// \param Tag Tag identifing type, e.g. dwarf::TAG_volatile_type
190 /// \param FromTy Base Type.
191 DIDerivedType *createQualifiedType(unsigned Tag, DIType *FromTy);
192
193 /// Create debugging information entry for a pointer.
194 /// \param PointeeTy Type pointed by this pointer.
195 /// \param SizeInBits Size.
196 /// \param AlignInBits Alignment. (optional)
197 /// \param DWARFAddressSpace DWARF address space. (optional)
198 /// \param Name Pointer type name. (optional)
199 DIDerivedType *createPointerType(DIType *PointeeTy, uint64_t SizeInBits,
200 uint32_t AlignInBits = 0,
201 Optional<unsigned> DWARFAddressSpace =
202 None,
203 StringRef Name = "");
204
205 /// Create debugging information entry for a pointer to member.
206 /// \param PointeeTy Type pointed to by this pointer.
207 /// \param SizeInBits Size.
208 /// \param AlignInBits Alignment. (optional)
209 /// \param Class Type for which this pointer points to members of.
210 DIDerivedType *
211 createMemberPointerType(DIType *PointeeTy, DIType *Class,
212 uint64_t SizeInBits, uint32_t AlignInBits = 0,
213 DINode::DIFlags Flags = DINode::FlagZero);
214
215 /// Create debugging information entry for a c++
216 /// style reference or rvalue reference type.
217 DIDerivedType *createReferenceType(unsigned Tag, DIType *RTy,
218 uint64_t SizeInBits = 0,
219 uint32_t AlignInBits = 0,
220 Optional<unsigned> DWARFAddressSpace =
221 None);
222
223 /// Create debugging information entry for a typedef.
224 /// \param Ty Original type.
225 /// \param Name Typedef name.
226 /// \param File File where this type is defined.
227 /// \param LineNo Line number.
228 /// \param Context The surrounding context for the typedef.
229 DIDerivedType *createTypedef(DIType *Ty, StringRef Name, DIFile *File,
230 unsigned LineNo, DIScope *Context);
231
232 /// Create debugging information entry for a 'friend'.
233 DIDerivedType *createFriend(DIType *Ty, DIType *FriendTy);
234
235 /// Create debugging information entry to establish
236 /// inheritance relationship between two types.
237 /// \param Ty Original type.
238 /// \param BaseTy Base type. Ty is inherits from base.
239 /// \param BaseOffset Base offset.
240 /// \param Flags Flags to describe inheritance attribute,
241 /// e.g. private
242 DIDerivedType *createInheritance(DIType *Ty, DIType *BaseTy,
243 uint64_t BaseOffset,
244 DINode::DIFlags Flags);
245
246 /// Create debugging information entry for a member.
247 /// \param Scope Member scope.
248 /// \param Name Member name.
249 /// \param File File where this member is defined.
250 /// \param LineNo Line number.
251 /// \param SizeInBits Member size.
252 /// \param AlignInBits Member alignment.
253 /// \param OffsetInBits Member offset.
254 /// \param Flags Flags to encode member attribute, e.g. private
255 /// \param Ty Parent type.
256 DIDerivedType *createMemberType(DIScope *Scope, StringRef Name,
257 DIFile *File, unsigned LineNo,
258 uint64_t SizeInBits,
259 uint32_t AlignInBits,
260 uint64_t OffsetInBits,
261 DINode::DIFlags Flags, DIType *Ty);
262
263 /// Create debugging information entry for a variant. A variant
264 /// normally should be a member of a variant part.
265 /// \param Scope Member scope.
266 /// \param Name Member name.
267 /// \param File File where this member is defined.
268 /// \param LineNo Line number.
269 /// \param SizeInBits Member size.
270 /// \param AlignInBits Member alignment.
271 /// \param OffsetInBits Member offset.
272 /// \param Flags Flags to encode member attribute, e.g. private
273 /// \param Discriminant The discriminant for this branch; null for
274 /// the default branch
275 /// \param Ty Parent type.
276 DIDerivedType *createVariantMemberType(DIScope *Scope, StringRef Name,
277 DIFile *File, unsigned LineNo,
278 uint64_t SizeInBits,
279 uint32_t AlignInBits,
280 uint64_t OffsetInBits,
281 Constant *Discriminant,
282 DINode::DIFlags Flags, DIType *Ty);
283
284 /// Create debugging information entry for a bit field member.
285 /// \param Scope Member scope.
286 /// \param Name Member name.
287 /// \param File File where this member is defined.
288 /// \param LineNo Line number.
289 /// \param SizeInBits Member size.
290 /// \param OffsetInBits Member offset.
291 /// \param StorageOffsetInBits Member storage offset.
292 /// \param Flags Flags to encode member attribute.
293 /// \param Ty Parent type.
294 DIDerivedType *createBitFieldMemberType(
295 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo,
296 uint64_t SizeInBits, uint64_t OffsetInBits,
297 uint64_t StorageOffsetInBits, DINode::DIFlags Flags, DIType *Ty);
298
299 /// Create debugging information entry for a
300 /// C++ static data member.
301 /// \param Scope Member scope.
302 /// \param Name Member name.
303 /// \param File File where this member is declared.
304 /// \param LineNo Line number.
305 /// \param Ty Type of the static member.
306 /// \param Flags Flags to encode member attribute, e.g. private.
307 /// \param Val Const initializer of the member.
308 /// \param AlignInBits Member alignment.
309 DIDerivedType *createStaticMemberType(DIScope *Scope, StringRef Name,
310 DIFile *File, unsigned LineNo,
311 DIType *Ty, DINode::DIFlags Flags,
312 Constant *Val,
313 uint32_t AlignInBits = 0);
314
315 /// Create debugging information entry for Objective-C
316 /// instance variable.
317 /// \param Name Member name.
318 /// \param File File where this member is defined.
319 /// \param LineNo Line number.
320 /// \param SizeInBits Member size.
321 /// \param AlignInBits Member alignment.
322 /// \param OffsetInBits Member offset.
323 /// \param Flags Flags to encode member attribute, e.g. private
324 /// \param Ty Parent type.
325 /// \param PropertyNode Property associated with this ivar.
326 DIDerivedType *createObjCIVar(StringRef Name, DIFile *File, unsigned LineNo,
327 uint64_t SizeInBits, uint32_t AlignInBits,
328 uint64_t OffsetInBits, DINode::DIFlags Flags,
329 DIType *Ty, MDNode *PropertyNode);
330
331 /// Create debugging information entry for Objective-C
332 /// property.
333 /// \param Name Property name.
334 /// \param File File where this property is defined.
335 /// \param LineNumber Line number.
336 /// \param GetterName Name of the Objective C property getter selector.
337 /// \param SetterName Name of the Objective C property setter selector.
338 /// \param PropertyAttributes Objective C property attributes.
339 /// \param Ty Type.
340 DIObjCProperty *createObjCProperty(StringRef Name, DIFile *File,
341 unsigned LineNumber,
342 StringRef GetterName,
343 StringRef SetterName,
344 unsigned PropertyAttributes, DIType *Ty);
345
346 /// Create debugging information entry for a class.
347 /// \param Scope Scope in which this class is defined.
348 /// \param Name class name.
349 /// \param File File where this member is defined.
350 /// \param LineNumber Line number.
351 /// \param SizeInBits Member size.
352 /// \param AlignInBits Member alignment.
353 /// \param OffsetInBits Member offset.
354 /// \param Flags Flags to encode member attribute, e.g. private
355 /// \param Elements class members.
356 /// \param VTableHolder Debug info of the base class that contains vtable
357 /// for this type. This is used in
358 /// DW_AT_containing_type. See DWARF documentation
359 /// for more info.
360 /// \param TemplateParms Template type parameters.
361 /// \param UniqueIdentifier A unique identifier for the class.
362 DICompositeType *createClassType(
363 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
364 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
365 DINode::DIFlags Flags, DIType *DerivedFrom, DINodeArray Elements,
366 DIType *VTableHolder = nullptr, MDNode *TemplateParms = nullptr,
367 StringRef UniqueIdentifier = "");
368
369 /// Create debugging information entry for a struct.
370 /// \param Scope Scope in which this struct is defined.
371 /// \param Name Struct name.
372 /// \param File File where this member is defined.
373 /// \param LineNumber Line number.
374 /// \param SizeInBits Member size.
375 /// \param AlignInBits Member alignment.
376 /// \param Flags Flags to encode member attribute, e.g. private
377 /// \param Elements Struct elements.
378 /// \param RunTimeLang Optional parameter, Objective-C runtime version.
379 /// \param UniqueIdentifier A unique identifier for the struct.
380 DICompositeType *createStructType(
381 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
382 uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
383 DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang = 0,
384 DIType *VTableHolder = nullptr, StringRef UniqueIdentifier = "");
385
386 /// Create debugging information entry for an union.
387 /// \param Scope Scope in which this union is defined.
388 /// \param Name Union name.
389 /// \param File File where this member is defined.
390 /// \param LineNumber Line number.
391 /// \param SizeInBits Member size.
392 /// \param AlignInBits Member alignment.
393 /// \param Flags Flags to encode member attribute, e.g. private
394 /// \param Elements Union elements.
395 /// \param RunTimeLang Optional parameter, Objective-C runtime version.
396 /// \param UniqueIdentifier A unique identifier for the union.
397 DICompositeType *createUnionType(DIScope *Scope, StringRef Name,
398 DIFile *File, unsigned LineNumber,
399 uint64_t SizeInBits, uint32_t AlignInBits,
400 DINode::DIFlags Flags,
401 DINodeArray Elements,
402 unsigned RunTimeLang = 0,
403 StringRef UniqueIdentifier = "");
404
405 /// Create debugging information entry for a variant part. A
406 /// variant part normally has a discriminator (though this is not
407 /// required) and a number of variant children.
408 /// \param Scope Scope in which this union is defined.
409 /// \param Name Union name.
410 /// \param File File where this member is defined.
411 /// \param LineNumber Line number.
412 /// \param SizeInBits Member size.
413 /// \param AlignInBits Member alignment.
414 /// \param Flags Flags to encode member attribute, e.g. private
415 /// \param Discriminator Discriminant member
416 /// \param Elements Variant elements.
417 /// \param UniqueIdentifier A unique identifier for the union.
418 DICompositeType *createVariantPart(DIScope *Scope, StringRef Name,
419 DIFile *File, unsigned LineNumber,
420 uint64_t SizeInBits, uint32_t AlignInBits,
421 DINode::DIFlags Flags,
422 DIDerivedType *Discriminator,
423 DINodeArray Elements,
424 StringRef UniqueIdentifier = "");
425
426 /// Create debugging information for template
427 /// type parameter.
428 /// \param Scope Scope in which this type is defined.
429 /// \param Name Type parameter name.
430 /// \param Ty Parameter type.
431 DITemplateTypeParameter *
432 createTemplateTypeParameter(DIScope *Scope, StringRef Name, DIType *Ty);
433
434 /// Create debugging information for template
435 /// value parameter.
436 /// \param Scope Scope in which this type is defined.
437 /// \param Name Value parameter name.
438 /// \param Ty Parameter type.
439 /// \param Val Constant parameter value.
440 DITemplateValueParameter *createTemplateValueParameter(DIScope *Scope,
441 StringRef Name,
442 DIType *Ty,
443 Constant *Val);
444
445 /// Create debugging information for a template template parameter.
446 /// \param Scope Scope in which this type is defined.
447 /// \param Name Value parameter name.
448 /// \param Ty Parameter type.
449 /// \param Val The fully qualified name of the template.
450 DITemplateValueParameter *createTemplateTemplateParameter(DIScope *Scope,
451 StringRef Name,
452 DIType *Ty,
453 StringRef Val);
454
455 /// Create debugging information for a template parameter pack.
456 /// \param Scope Scope in which this type is defined.
457 /// \param Name Value parameter name.
458 /// \param Ty Parameter type.
459 /// \param Val An array of types in the pack.
460 DITemplateValueParameter *createTemplateParameterPack(DIScope *Scope,
461 StringRef Name,
462 DIType *Ty,
463 DINodeArray Val);
464
465 /// Create debugging information entry for an array.
466 /// \param Size Array size.
467 /// \param AlignInBits Alignment.
468 /// \param Ty Element type.
469 /// \param Subscripts Subscripts.
470 DICompositeType *createArrayType(uint64_t Size, uint32_t AlignInBits,
471 DIType *Ty, DINodeArray Subscripts);
472
473 /// Create debugging information entry for a vector type.
474 /// \param Size Array size.
475 /// \param AlignInBits Alignment.
476 /// \param Ty Element type.
477 /// \param Subscripts Subscripts.
478 DICompositeType *createVectorType(uint64_t Size, uint32_t AlignInBits,
479 DIType *Ty, DINodeArray Subscripts);
480
481 /// Create debugging information entry for an
482 /// enumeration.
483 /// \param Scope Scope in which this enumeration is defined.
484 /// \param Name Union name.
485 /// \param File File where this member is defined.
486 /// \param LineNumber Line number.
487 /// \param SizeInBits Member size.
488 /// \param AlignInBits Member alignment.
489 /// \param Elements Enumeration elements.
490 /// \param UnderlyingType Underlying type of a C++11/ObjC fixed enum.
491 /// \param UniqueIdentifier A unique identifier for the enum.
492 /// \param IsFixed Boolean flag indicate if this is C++11/ObjC fixed enum.
493 DICompositeType *createEnumerationType(
494 DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
495 uint64_t SizeInBits, uint32_t AlignInBits, DINodeArray Elements,
496 DIType *UnderlyingType, StringRef UniqueIdentifier = "", bool IsFixed = false);
497
498 /// Create subroutine type.
499 /// \param ParameterTypes An array of subroutine parameter types. This
500 /// includes return type at 0th index.
501 /// \param Flags E.g.: LValueReference.
502 /// These flags are used to emit dwarf attributes.
503 /// \param CC Calling convention, e.g. dwarf::DW_CC_normal
504 DISubroutineType *
505 createSubroutineType(DITypeRefArray ParameterTypes,
506 DINode::DIFlags Flags = DINode::FlagZero,
507 unsigned CC = 0);
508
509 /// Create a new DIType* with "artificial" flag set.
510 DIType *createArtificialType(DIType *Ty);
511
512 /// Create a new DIType* with the "object pointer"
513 /// flag set.
514 DIType *createObjectPointerType(DIType *Ty);
515
516 /// Create a permanent forward-declared type.
517 DICompositeType *createForwardDecl(unsigned Tag, StringRef Name,
518 DIScope *Scope, DIFile *F, unsigned Line,
519 unsigned RuntimeLang = 0,
520 uint64_t SizeInBits = 0,
521 uint32_t AlignInBits = 0,
522 StringRef UniqueIdentifier = "");
523
524 /// Create a temporary forward-declared type.
525 DICompositeType *createReplaceableCompositeType(
526 unsigned Tag, StringRef Name, DIScope *Scope, DIFile *F, unsigned Line,
527 unsigned RuntimeLang = 0, uint64_t SizeInBits = 0,
528 uint32_t AlignInBits = 0, DINode::DIFlags Flags = DINode::FlagFwdDecl,
529 StringRef UniqueIdentifier = "");
530
531 /// Retain DIScope* in a module even if it is not referenced
532 /// through debug info anchors.
533 void retainType(DIScope *T);
534
535 /// Create unspecified parameter type
536 /// for a subroutine type.
537 DIBasicType *createUnspecifiedParameter();
538
539 /// Get a DINodeArray, create one if required.
540 DINodeArray getOrCreateArray(ArrayRef<Metadata *> Elements);
541
542 /// Get a DIMacroNodeArray, create one if required.
543 DIMacroNodeArray getOrCreateMacroArray(ArrayRef<Metadata *> Elements);
544
545 /// Get a DITypeRefArray, create one if required.
546 DITypeRefArray getOrCreateTypeArray(ArrayRef<Metadata *> Elements);
547
548 /// Create a descriptor for a value range. This
549 /// implicitly uniques the values returned.
550 DISubrange *getOrCreateSubrange(int64_t Lo, int64_t Count);
551 DISubrange *getOrCreateSubrange(int64_t Lo, Metadata *CountNode);
552
553 /// Create a new descriptor for the specified variable.
554 /// \param Context Variable scope.
555 /// \param Name Name of the variable.
556 /// \param LinkageName Mangled name of the variable.
557 /// \param File File where this variable is defined.
558 /// \param LineNo Line number.
559 /// \param Ty Variable Type.
560 /// \param isLocalToUnit Boolean flag indicate whether this variable is
561 /// externally visible or not.
562 /// \param Expr The location of the global relative to the attached
563 /// GlobalVariable.
564 /// \param Decl Reference to the corresponding declaration.
565 /// \param AlignInBits Variable alignment(or 0 if no alignment attr was
566 /// specified)
567 DIGlobalVariableExpression *createGlobalVariableExpression(
568 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
569 unsigned LineNo, DIType *Ty, bool isLocalToUnit,
570 DIExpression *Expr = nullptr, MDNode *Decl = nullptr,
571 uint32_t AlignInBits = 0);
572
573 /// Identical to createGlobalVariable
574 /// except that the resulting DbgNode is temporary and meant to be RAUWed.
575 DIGlobalVariable *createTempGlobalVariableFwdDecl(
576 DIScope *Context, StringRef Name, StringRef LinkageName, DIFile *File,
577 unsigned LineNo, DIType *Ty, bool isLocalToUnit, MDNode *Decl = nullptr,
578 uint32_t AlignInBits = 0);
579
580 /// Create a new descriptor for an auto variable. This is a local variable
581 /// that is not a subprogram parameter.
582 ///
583 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
584 /// leads to a \a DISubprogram.
585 ///
586 /// If \c AlwaysPreserve, this variable will be referenced from its
587 /// containing subprogram, and will survive some optimizations.
588 DILocalVariable *
589 createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File,
590 unsigned LineNo, DIType *Ty, bool AlwaysPreserve = false,
591 DINode::DIFlags Flags = DINode::FlagZero,
592 uint32_t AlignInBits = 0);
593
594 /// Create a new descriptor for a parameter variable.
595 ///
596 /// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
597 /// leads to a \a DISubprogram.
598 ///
599 /// \c ArgNo is the index (starting from \c 1) of this variable in the
600 /// subprogram parameters. \c ArgNo should not conflict with other
601 /// parameters of the same subprogram.
602 ///
603 /// If \c AlwaysPreserve, this variable will be referenced from its
604 /// containing subprogram, and will survive some optimizations.
605 DILocalVariable *
606 createParameterVariable(DIScope *Scope, StringRef Name, unsigned ArgNo,
607 DIFile *File, unsigned LineNo, DIType *Ty,
608 bool AlwaysPreserve = false,
609 DINode::DIFlags Flags = DINode::FlagZero);
610
611 /// Create a new descriptor for the specified
612 /// variable which has a complex address expression for its address.
613 /// \param Addr An array of complex address operations.
614 DIExpression *createExpression(ArrayRef<uint64_t> Addr = None);
615 DIExpression *createExpression(ArrayRef<int64_t> Addr);
616
617 /// Create an expression for a variable that does not have an address, but
618 /// does have a constant value.
619 DIExpression *createConstantValueExpression(uint64_t Val) {
620 return DIExpression::get(
621 VMContext, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_stack_value});
622 }
623
624 /// Create a new descriptor for the specified subprogram.
625 /// See comments in DISubprogram* for descriptions of these fields.
626 /// \param Scope Function scope.
627 /// \param Name Function name.
628 /// \param LinkageName Mangled function name.
629 /// \param File File where this variable is defined.
630 /// \param LineNo Line number.
631 /// \param Ty Function type.
632 /// \param isLocalToUnit True if this function is not externally visible.
633 /// \param isDefinition True if this is a function definition.
634 /// \param ScopeLine Set to the beginning of the scope this starts
635 /// \param Flags e.g. is this function prototyped or not.
636 /// These flags are used to emit dwarf attributes.
637 /// \param isOptimized True if optimization is ON.
638 /// \param TParams Function template parameters.
639 /// \param ThrownTypes Exception types this function may throw.
640 DISubprogram *createFunction(
641 DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File,
642 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
643 bool isDefinition, unsigned ScopeLine,
644 DINode::DIFlags Flags = DINode::FlagZero, bool isOptimized = false,
645 DITemplateParameterArray TParams = nullptr,
646 DISubprogram *Decl = nullptr, DITypeArray ThrownTypes = nullptr);
647
648 /// Identical to createFunction,
649 /// except that the resulting DbgNode is meant to be RAUWed.
650 DISubprogram *createTempFunctionFwdDecl(
651 DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File,
652 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
653 bool isDefinition, unsigned ScopeLine,
654 DINode::DIFlags Flags = DINode::FlagZero, bool isOptimized = false,
655 DITemplateParameterArray TParams = nullptr,
656 DISubprogram *Decl = nullptr, DITypeArray ThrownTypes = nullptr);
657
658 /// Create a new descriptor for the specified C++ method.
659 /// See comments in \a DISubprogram* for descriptions of these fields.
660 /// \param Scope Function scope.
661 /// \param Name Function name.
662 /// \param LinkageName Mangled function name.
663 /// \param File File where this variable is defined.
664 /// \param LineNo Line number.
665 /// \param Ty Function type.
666 /// \param isLocalToUnit True if this function is not externally visible..
667 /// \param isDefinition True if this is a function definition.
668 /// \param Virtuality Attributes describing virtualness. e.g. pure
669 /// virtual function.
670 /// \param VTableIndex Index no of this method in virtual table, or -1u if
671 /// unrepresentable.
672 /// \param ThisAdjustment
673 /// MS ABI-specific adjustment of 'this' that occurs
674 /// in the prologue.
675 /// \param VTableHolder Type that holds vtable.
676 /// \param Flags e.g. is this function prototyped or not.
677 /// This flags are used to emit dwarf attributes.
678 /// \param isOptimized True if optimization is ON.
679 /// \param TParams Function template parameters.
680 /// \param ThrownTypes Exception types this function may throw.
681 DISubprogram *createMethod(
682 DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File,
683 unsigned LineNo, DISubroutineType *Ty, bool isLocalToUnit,
684 bool isDefinition, unsigned Virtuality = 0, unsigned VTableIndex = 0,
685 int ThisAdjustment = 0, DIType *VTableHolder = nullptr,
686 DINode::DIFlags Flags = DINode::FlagZero, bool isOptimized = false,
687 DITemplateParameterArray TParams = nullptr,
688 DITypeArray ThrownTypes = nullptr);
689
690 /// This creates new descriptor for a namespace with the specified
691 /// parent scope.
692 /// \param Scope Namespace scope
693 /// \param Name Name of this namespace
694 /// \param ExportSymbols True for C++ inline namespaces.
695 DINamespace *createNameSpace(DIScope *Scope, StringRef Name,
696 bool ExportSymbols);
697
698 /// This creates new descriptor for a module with the specified
699 /// parent scope.
700 /// \param Scope Parent scope
701 /// \param Name Name of this module
702 /// \param ConfigurationMacros
703 /// A space-separated shell-quoted list of -D macro
704 /// definitions as they would appear on a command line.
705 /// \param IncludePath The path to the module map file.
706 /// \param ISysRoot The clang system root (value of -isysroot).
707 DIModule *createModule(DIScope *Scope, StringRef Name,
708 StringRef ConfigurationMacros,
709 StringRef IncludePath,
710 StringRef ISysRoot);
711
712 /// This creates a descriptor for a lexical block with a new file
713 /// attached. This merely extends the existing
714 /// lexical block as it crosses a file.
715 /// \param Scope Lexical block.
716 /// \param File Source file.
717 /// \param Discriminator DWARF path discriminator value.
718 DILexicalBlockFile *createLexicalBlockFile(DIScope *Scope, DIFile *File,
719 unsigned Discriminator = 0);
720
721 /// This creates a descriptor for a lexical block with the
722 /// specified parent context.
723 /// \param Scope Parent lexical scope.
724 /// \param File Source file.
725 /// \param Line Line number.
726 /// \param Col Column number.
727 DILexicalBlock *createLexicalBlock(DIScope *Scope, DIFile *File,
728 unsigned Line, unsigned Col);
729
730 /// Create a descriptor for an imported module.
731 /// \param Context The scope this module is imported into
732 /// \param NS The namespace being imported here.
733 /// \param File File where the declaration is located.
734 /// \param Line Line number of the declaration.
735 DIImportedEntity *createImportedModule(DIScope *Context, DINamespace *NS,
736 DIFile *File, unsigned Line);
737
738 /// Create a descriptor for an imported module.
739 /// \param Context The scope this module is imported into.
740 /// \param NS An aliased namespace.
741 /// \param File File where the declaration is located.
742 /// \param Line Line number of the declaration.
743 DIImportedEntity *createImportedModule(DIScope *Context,
744 DIImportedEntity *NS, DIFile *File,
745 unsigned Line);
746
747 /// Create a descriptor for an imported module.
748 /// \param Context The scope this module is imported into.
749 /// \param M The module being imported here
750 /// \param File File where the declaration is located.
751 /// \param Line Line number of the declaration.
752 DIImportedEntity *createImportedModule(DIScope *Context, DIModule *M,
753 DIFile *File, unsigned Line);
754
755 /// Create a descriptor for an imported function.
756 /// \param Context The scope this module is imported into.
757 /// \param Decl The declaration (or definition) of a function, type, or
758 /// variable.
759 /// \param File File where the declaration is located.
760 /// \param Line Line number of the declaration.
761 DIImportedEntity *createImportedDeclaration(DIScope *Context, DINode *Decl,
762 DIFile *File, unsigned Line,
763 StringRef Name = "");
764
765 /// Insert a new llvm.dbg.declare intrinsic call.
766 /// \param Storage llvm::Value of the variable
767 /// \param VarInfo Variable's debug info descriptor.
768 /// \param Expr A complex location expression.
769 /// \param DL Debug info location.
770 /// \param InsertAtEnd Location for the new intrinsic.
771 Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
772 DIExpression *Expr, const DILocation *DL,
773 BasicBlock *InsertAtEnd);
774
775 /// Insert a new llvm.dbg.declare intrinsic call.
776 /// \param Storage llvm::Value of the variable
777 /// \param VarInfo Variable's debug info descriptor.
778 /// \param Expr A complex location expression.
779 /// \param DL Debug info location.
780 /// \param InsertBefore Location for the new intrinsic.
781 Instruction *insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
782 DIExpression *Expr, const DILocation *DL,
783 Instruction *InsertBefore);
784
785 /// Insert a new llvm.dbg.value intrinsic call.
786 /// \param Val llvm::Value of the variable
787 /// \param VarInfo Variable's debug info descriptor.
788 /// \param Expr A complex location expression.
789 /// \param DL Debug info location.
790 /// \param InsertAtEnd Location for the new intrinsic.
791 Instruction *insertDbgValueIntrinsic(llvm::Value *Val,
792 DILocalVariable *VarInfo,
793 DIExpression *Expr,
794 const DILocation *DL,
795 BasicBlock *InsertAtEnd);
796
797 /// Insert a new llvm.dbg.value intrinsic call.
798 /// \param Val llvm::Value of the variable
799 /// \param VarInfo Variable's debug info descriptor.
800 /// \param Expr A complex location expression.
801 /// \param DL Debug info location.
802 /// \param InsertBefore Location for the new intrinsic.
803 Instruction *insertDbgValueIntrinsic(llvm::Value *Val,
804 DILocalVariable *VarInfo,
805 DIExpression *Expr,
806 const DILocation *DL,
807 Instruction *InsertBefore);
808
809 /// Replace the vtable holder in the given type.
810 ///
811 /// If this creates a self reference, it may orphan some unresolved cycles
812 /// in the operands of \c T, so \a DIBuilder needs to track that.
813 void replaceVTableHolder(DICompositeType *&T,
814 DIType *VTableHolder);
815
816 /// Replace arrays on a composite type.
817 ///
818 /// If \c T is resolved, but the arrays aren't -- which can happen if \c T
819 /// has a self-reference -- \a DIBuilder needs to track the array to
820 /// resolve cycles.
821 void replaceArrays(DICompositeType *&T, DINodeArray Elements,
822 DINodeArray TParams = DINodeArray());
823
824 /// Replace a temporary node.
825 ///
826 /// Call \a MDNode::replaceAllUsesWith() on \c N, replacing it with \c
827 /// Replacement.
828 ///
829 /// If \c Replacement is the same as \c N.get(), instead call \a
830 /// MDNode::replaceWithUniqued(). In this case, the uniqued node could
831 /// have a different address, so we return the final address.
832 template <class NodeTy>
833 NodeTy *replaceTemporary(TempMDNode &&N, NodeTy *Replacement) {
834 if (N.get() == Replacement)
835 return cast<NodeTy>(MDNode::replaceWithUniqued(std::move(N)));
836
837 N->replaceAllUsesWith(Replacement);
838 return Replacement;
839 }
840 };
841
842 // Create wrappers for C Binding types (see CBindingWrapping.h).
843 DEFINE_ISA_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
844
845} // end namespace llvm
846
847#endif // LLVM_IR_DIBUILDER_H