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