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