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/StringRef.h"
20#include "llvm/IR/DebugInfo.h"
21#include "llvm/IR/TrackingMDRef.h"
22#include "llvm/IR/ValueHandle.h"
23#include "llvm/Support/DataTypes.h"
24
25namespace llvm {
26  class BasicBlock;
27  class Instruction;
28  class Function;
29  class Module;
30  class Value;
31  class Constant;
32  class LLVMContext;
33  class StringRef;
34
35  class DIBuilder {
36    Module &M;
37    LLVMContext &VMContext;
38
39    TempMDTuple TempEnumTypes;
40    TempMDTuple TempRetainTypes;
41    TempMDTuple TempSubprograms;
42    TempMDTuple TempGVs;
43    TempMDTuple TempImportedModules;
44
45    Function *DeclareFn;     // llvm.dbg.declare
46    Function *ValueFn;       // llvm.dbg.value
47
48    SmallVector<Metadata *, 4> AllEnumTypes;
49    /// Track the RetainTypes, since they can be updated later on.
50    SmallVector<TrackingMDNodeRef, 4> AllRetainTypes;
51    SmallVector<Metadata *, 4> AllSubprograms;
52    SmallVector<Metadata *, 4> AllGVs;
53    SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
54
55    /// \brief Track nodes that may be unresolved.
56    SmallVector<TrackingMDNodeRef, 4> UnresolvedNodes;
57    bool AllowUnresolvedNodes;
58
59    /// Each subprogram's preserved local variables.
60    DenseMap<MDNode *, std::vector<TrackingMDNodeRef>> PreservedVariables;
61
62    DIBuilder(const DIBuilder &) = delete;
63    void operator=(const DIBuilder &) = delete;
64
65    /// \brief Create a temporary.
66    ///
67    /// Create an \a temporary node and track it in \a UnresolvedNodes.
68    void trackIfUnresolved(MDNode *N);
69
70  public:
71    /// \brief Construct a builder for a module.
72    ///
73    /// If \c AllowUnresolved, collect unresolved nodes attached to the module
74    /// in order to resolve cycles during \a finalize().
75    explicit DIBuilder(Module &M, bool AllowUnresolved = true);
76    enum DebugEmissionKind { FullDebug=1, LineTablesOnly };
77
78    /// finalize - Construct any deferred debug info descriptors.
79    void finalize();
80
81    /// createCompileUnit - A CompileUnit provides an anchor for all debugging
82    /// information generated during this instance of compilation.
83    /// @param Lang     Source programming language, eg. dwarf::DW_LANG_C99
84    /// @param File     File name
85    /// @param Dir      Directory
86    /// @param Producer Identify the producer of debugging information and code.
87    ///                 Usually this is a compiler version string.
88    /// @param isOptimized A boolean flag which indicates whether optimization
89    ///                    is ON or not.
90    /// @param Flags    This string lists command line options. This string is
91    ///                 directly embedded in debug info output which may be used
92    ///                 by a tool analyzing generated debugging information.
93    /// @param RV       This indicates runtime version for languages like
94    ///                 Objective-C.
95    /// @param SplitName The name of the file that we'll split debug info out
96    ///                  into.
97    /// @param Kind     The kind of debug information to generate.
98    /// @param EmitDebugInfo   A boolean flag which indicates whether debug
99    ///                        information should be written to the final
100    ///                        output or not. When this is false, debug
101    ///                        information annotations will be present in
102    ///                        the IL but they are not written to the final
103    ///                        assembly or object file. This supports tracking
104    ///                        source location information in the back end
105    ///                        without actually changing the output (e.g.,
106    ///                        when using optimization remarks).
107    MDCompileUnit *createCompileUnit(unsigned Lang, StringRef File,
108                                     StringRef Dir, StringRef Producer,
109                                     bool isOptimized, StringRef Flags,
110                                     unsigned RV, StringRef SplitName = "",
111                                     DebugEmissionKind Kind = FullDebug,
112                                     bool EmitDebugInfo = true);
113
114    /// createFile - Create a file descriptor to hold debugging information
115    /// for a file.
116    MDFile *createFile(StringRef Filename, StringRef Directory);
117
118    /// createEnumerator - Create a single enumerator value.
119    MDEnumerator *createEnumerator(StringRef Name, int64_t Val);
120
121    /// \brief Create a DWARF unspecified type.
122    MDBasicType *createUnspecifiedType(StringRef Name);
123
124    /// \brief Create C++11 nullptr type.
125    MDBasicType *createNullPtrType();
126
127    /// createBasicType - Create debugging information entry for a basic
128    /// type.
129    /// @param Name        Type name.
130    /// @param SizeInBits  Size of the type.
131    /// @param AlignInBits Type alignment.
132    /// @param Encoding    DWARF encoding code, e.g. dwarf::DW_ATE_float.
133    MDBasicType *createBasicType(StringRef Name, uint64_t SizeInBits,
134                                 uint64_t AlignInBits, unsigned Encoding);
135
136    /// createQualifiedType - Create debugging information entry for a qualified
137    /// type, e.g. 'const int'.
138    /// @param Tag         Tag identifing type, e.g. dwarf::TAG_volatile_type
139    /// @param FromTy      Base Type.
140    MDDerivedType *createQualifiedType(unsigned Tag, MDType *FromTy);
141
142    /// createPointerType - Create debugging information entry for a pointer.
143    /// @param PointeeTy   Type pointed by this pointer.
144    /// @param SizeInBits  Size.
145    /// @param AlignInBits Alignment. (optional)
146    /// @param Name        Pointer type name. (optional)
147    MDDerivedType *createPointerType(MDType *PointeeTy, uint64_t SizeInBits,
148                                     uint64_t AlignInBits = 0,
149                                     StringRef Name = "");
150
151    /// \brief Create debugging information entry for a pointer to member.
152    /// @param PointeeTy Type pointed to by this pointer.
153    /// @param SizeInBits  Size.
154    /// @param AlignInBits Alignment. (optional)
155    /// @param Class Type for which this pointer points to members of.
156    MDDerivedType *createMemberPointerType(MDType *PointeeTy, MDType *Class,
157                                           uint64_t SizeInBits,
158                                           uint64_t AlignInBits = 0);
159
160    /// createReferenceType - Create debugging information entry for a c++
161    /// style reference or rvalue reference type.
162    MDDerivedType *createReferenceType(unsigned Tag, MDType *RTy);
163
164    /// createTypedef - Create debugging information entry for a typedef.
165    /// @param Ty          Original type.
166    /// @param Name        Typedef name.
167    /// @param File        File where this type is defined.
168    /// @param LineNo      Line number.
169    /// @param Context     The surrounding context for the typedef.
170    MDDerivedType *createTypedef(MDType *Ty, StringRef Name, MDFile *File,
171                                 unsigned LineNo, MDScope *Context);
172
173    /// createFriend - Create debugging information entry for a 'friend'.
174    MDDerivedType *createFriend(MDType *Ty, MDType *FriendTy);
175
176    /// createInheritance - Create debugging information entry to establish
177    /// inheritance relationship between two types.
178    /// @param Ty           Original type.
179    /// @param BaseTy       Base type. Ty is inherits from base.
180    /// @param BaseOffset   Base offset.
181    /// @param Flags        Flags to describe inheritance attribute,
182    ///                     e.g. private
183    MDDerivedType *createInheritance(MDType *Ty, MDType *BaseTy,
184                                     uint64_t BaseOffset, unsigned Flags);
185
186    /// createMemberType - Create debugging information entry for a member.
187    /// @param Scope        Member scope.
188    /// @param Name         Member name.
189    /// @param File         File where this member is defined.
190    /// @param LineNo       Line number.
191    /// @param SizeInBits   Member size.
192    /// @param AlignInBits  Member alignment.
193    /// @param OffsetInBits Member offset.
194    /// @param Flags        Flags to encode member attribute, e.g. private
195    /// @param Ty           Parent type.
196    MDDerivedType *createMemberType(MDScope *Scope, StringRef Name,
197                                    MDFile *File, unsigned LineNo,
198                                    uint64_t SizeInBits, uint64_t AlignInBits,
199                                    uint64_t OffsetInBits, unsigned Flags,
200                                    MDType *Ty);
201
202    /// createStaticMemberType - Create debugging information entry for a
203    /// C++ static data member.
204    /// @param Scope      Member scope.
205    /// @param Name       Member name.
206    /// @param File       File where this member is declared.
207    /// @param LineNo     Line number.
208    /// @param Ty         Type of the static member.
209    /// @param Flags      Flags to encode member attribute, e.g. private.
210    /// @param Val        Const initializer of the member.
211    MDDerivedType *createStaticMemberType(MDScope *Scope, StringRef Name,
212                                          MDFile *File, unsigned LineNo,
213                                          MDType *Ty, unsigned Flags,
214                                          llvm::Constant *Val);
215
216    /// createObjCIVar - Create debugging information entry for Objective-C
217    /// instance variable.
218    /// @param Name         Member name.
219    /// @param File         File where this member is defined.
220    /// @param LineNo       Line number.
221    /// @param SizeInBits   Member size.
222    /// @param AlignInBits  Member alignment.
223    /// @param OffsetInBits Member offset.
224    /// @param Flags        Flags to encode member attribute, e.g. private
225    /// @param Ty           Parent type.
226    /// @param PropertyNode Property associated with this ivar.
227    MDDerivedType *createObjCIVar(StringRef Name, MDFile *File, unsigned LineNo,
228                                  uint64_t SizeInBits, uint64_t AlignInBits,
229                                  uint64_t OffsetInBits, unsigned Flags,
230                                  MDType *Ty, MDNode *PropertyNode);
231
232    /// createObjCProperty - Create debugging information entry for Objective-C
233    /// property.
234    /// @param Name         Property name.
235    /// @param File         File where this property is defined.
236    /// @param LineNumber   Line number.
237    /// @param GetterName   Name of the Objective C property getter selector.
238    /// @param SetterName   Name of the Objective C property setter selector.
239    /// @param PropertyAttributes Objective C property attributes.
240    /// @param Ty           Type.
241    MDObjCProperty *createObjCProperty(StringRef Name, MDFile *File,
242                                       unsigned LineNumber,
243                                       StringRef GetterName,
244                                       StringRef SetterName,
245                                       unsigned PropertyAttributes, MDType *Ty);
246
247    /// createClassType - Create debugging information entry for a class.
248    /// @param Scope        Scope in which this class is defined.
249    /// @param Name         class name.
250    /// @param File         File where this member is defined.
251    /// @param LineNumber   Line number.
252    /// @param SizeInBits   Member size.
253    /// @param AlignInBits  Member alignment.
254    /// @param OffsetInBits Member offset.
255    /// @param Flags        Flags to encode member attribute, e.g. private
256    /// @param Elements     class members.
257    /// @param VTableHolder Debug info of the base class that contains vtable
258    ///                     for this type. This is used in
259    ///                     DW_AT_containing_type. See DWARF documentation
260    ///                     for more info.
261    /// @param TemplateParms Template type parameters.
262    /// @param UniqueIdentifier A unique identifier for the class.
263    MDCompositeType *createClassType(MDScope *Scope, StringRef Name,
264                                     MDFile *File, unsigned LineNumber,
265                                     uint64_t SizeInBits, uint64_t AlignInBits,
266                                     uint64_t OffsetInBits, unsigned Flags,
267                                     MDType *DerivedFrom, DIArray Elements,
268                                     MDType *VTableHolder = nullptr,
269                                     MDNode *TemplateParms = nullptr,
270                                     StringRef UniqueIdentifier = "");
271
272    /// createStructType - Create debugging information entry for a struct.
273    /// @param Scope        Scope in which this struct is defined.
274    /// @param Name         Struct name.
275    /// @param File         File where this member is defined.
276    /// @param LineNumber   Line number.
277    /// @param SizeInBits   Member size.
278    /// @param AlignInBits  Member alignment.
279    /// @param Flags        Flags to encode member attribute, e.g. private
280    /// @param Elements     Struct elements.
281    /// @param RunTimeLang  Optional parameter, Objective-C runtime version.
282    /// @param UniqueIdentifier A unique identifier for the struct.
283    MDCompositeType *createStructType(
284        MDScope *Scope, StringRef Name, MDFile *File, unsigned LineNumber,
285        uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
286        MDType *DerivedFrom, DIArray Elements, unsigned RunTimeLang = 0,
287        MDType *VTableHolder = nullptr, StringRef UniqueIdentifier = "");
288
289    /// createUnionType - Create debugging information entry for an union.
290    /// @param Scope        Scope in which this union is defined.
291    /// @param Name         Union name.
292    /// @param File         File where this member is defined.
293    /// @param LineNumber   Line number.
294    /// @param SizeInBits   Member size.
295    /// @param AlignInBits  Member alignment.
296    /// @param Flags        Flags to encode member attribute, e.g. private
297    /// @param Elements     Union elements.
298    /// @param RunTimeLang  Optional parameter, Objective-C runtime version.
299    /// @param UniqueIdentifier A unique identifier for the union.
300    MDCompositeType *createUnionType(MDScope *Scope, StringRef Name,
301                                     MDFile *File, unsigned LineNumber,
302                                     uint64_t SizeInBits, uint64_t AlignInBits,
303                                     unsigned Flags, DIArray Elements,
304                                     unsigned RunTimeLang = 0,
305                                     StringRef UniqueIdentifier = "");
306
307    /// createTemplateTypeParameter - Create debugging information for template
308    /// type parameter.
309    /// @param Scope        Scope in which this type is defined.
310    /// @param Name         Type parameter name.
311    /// @param Ty           Parameter type.
312    MDTemplateTypeParameter *
313    createTemplateTypeParameter(MDScope *Scope, StringRef Name, MDType *Ty);
314
315    /// createTemplateValueParameter - Create debugging information for template
316    /// value parameter.
317    /// @param Scope        Scope in which this type is defined.
318    /// @param Name         Value parameter name.
319    /// @param Ty           Parameter type.
320    /// @param Val          Constant parameter value.
321    MDTemplateValueParameter *createTemplateValueParameter(MDScope *Scope,
322                                                           StringRef Name,
323                                                           MDType *Ty,
324                                                           Constant *Val);
325
326    /// \brief Create debugging information for a template template parameter.
327    /// @param Scope        Scope in which this type is defined.
328    /// @param Name         Value parameter name.
329    /// @param Ty           Parameter type.
330    /// @param Val          The fully qualified name of the template.
331    MDTemplateValueParameter *createTemplateTemplateParameter(MDScope *Scope,
332                                                              StringRef Name,
333                                                              MDType *Ty,
334                                                              StringRef Val);
335
336    /// \brief Create debugging information for a template parameter pack.
337    /// @param Scope        Scope in which this type is defined.
338    /// @param Name         Value parameter name.
339    /// @param Ty           Parameter type.
340    /// @param Val          An array of types in the pack.
341    MDTemplateValueParameter *createTemplateParameterPack(MDScope *Scope,
342                                                          StringRef Name,
343                                                          MDType *Ty,
344                                                          DIArray Val);
345
346    /// createArrayType - Create debugging information entry for an array.
347    /// @param Size         Array size.
348    /// @param AlignInBits  Alignment.
349    /// @param Ty           Element type.
350    /// @param Subscripts   Subscripts.
351    MDCompositeType *createArrayType(uint64_t Size, uint64_t AlignInBits,
352                                     MDType *Ty, DIArray Subscripts);
353
354    /// createVectorType - Create debugging information entry for a vector type.
355    /// @param Size         Array size.
356    /// @param AlignInBits  Alignment.
357    /// @param Ty           Element type.
358    /// @param Subscripts   Subscripts.
359    MDCompositeType *createVectorType(uint64_t Size, uint64_t AlignInBits,
360                                      MDType *Ty, DIArray Subscripts);
361
362    /// createEnumerationType - Create debugging information entry for an
363    /// enumeration.
364    /// @param Scope          Scope in which this enumeration is defined.
365    /// @param Name           Union name.
366    /// @param File           File where this member is defined.
367    /// @param LineNumber     Line number.
368    /// @param SizeInBits     Member size.
369    /// @param AlignInBits    Member alignment.
370    /// @param Elements       Enumeration elements.
371    /// @param UnderlyingType Underlying type of a C++11/ObjC fixed enum.
372    /// @param UniqueIdentifier A unique identifier for the enum.
373    MDCompositeType *createEnumerationType(
374        MDScope *Scope, StringRef Name, MDFile *File, unsigned LineNumber,
375        uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
376        MDType *UnderlyingType, StringRef UniqueIdentifier = "");
377
378    /// createSubroutineType - Create subroutine type.
379    /// @param File            File in which this subroutine is defined.
380    /// @param ParameterTypes  An array of subroutine parameter types. This
381    ///                        includes return type at 0th index.
382    /// @param Flags           E.g.: LValueReference.
383    ///                        These flags are used to emit dwarf attributes.
384    MDSubroutineType *createSubroutineType(MDFile *File,
385                                           DITypeArray ParameterTypes,
386                                           unsigned Flags = 0);
387
388    /// createArtificialType - Create a new MDType* with "artificial" flag set.
389    MDType *createArtificialType(MDType *Ty);
390
391    /// createObjectPointerType - Create a new MDType* with the "object pointer"
392    /// flag set.
393    MDType *createObjectPointerType(MDType *Ty);
394
395    /// \brief Create a permanent forward-declared type.
396    MDCompositeType *createForwardDecl(unsigned Tag, StringRef Name,
397                                       MDScope *Scope, MDFile *F, unsigned Line,
398                                       unsigned RuntimeLang = 0,
399                                       uint64_t SizeInBits = 0,
400                                       uint64_t AlignInBits = 0,
401                                       StringRef UniqueIdentifier = "");
402
403    /// \brief Create a temporary forward-declared type.
404    MDCompositeType *createReplaceableCompositeType(
405        unsigned Tag, StringRef Name, MDScope *Scope, MDFile *F, unsigned Line,
406        unsigned RuntimeLang = 0, uint64_t SizeInBits = 0,
407        uint64_t AlignInBits = 0, unsigned Flags = DebugNode::FlagFwdDecl,
408        StringRef UniqueIdentifier = "");
409
410    /// retainType - Retain MDType* in a module even if it is not referenced
411    /// through debug info anchors.
412    void retainType(MDType *T);
413
414    /// createUnspecifiedParameter - Create unspecified parameter type
415    /// for a subroutine type.
416    MDBasicType *createUnspecifiedParameter();
417
418    /// getOrCreateArray - Get a DIArray, create one if required.
419    DIArray getOrCreateArray(ArrayRef<Metadata *> Elements);
420
421    /// getOrCreateTypeArray - Get a DITypeArray, create one if required.
422    DITypeArray getOrCreateTypeArray(ArrayRef<Metadata *> Elements);
423
424    /// getOrCreateSubrange - Create a descriptor for a value range.  This
425    /// implicitly uniques the values returned.
426    MDSubrange *getOrCreateSubrange(int64_t Lo, int64_t Count);
427
428    /// createGlobalVariable - Create a new descriptor for the specified
429    /// variable.
430    /// @param Context     Variable scope.
431    /// @param Name        Name of the variable.
432    /// @param LinkageName Mangled  name of the variable.
433    /// @param File        File where this variable is defined.
434    /// @param LineNo      Line number.
435    /// @param Ty          Variable Type.
436    /// @param isLocalToUnit Boolean flag indicate whether this variable is
437    ///                      externally visible or not.
438    /// @param Val         llvm::Value of the variable.
439    /// @param Decl        Reference to the corresponding declaration.
440    MDGlobalVariable *createGlobalVariable(MDScope *Context, StringRef Name,
441                                           StringRef LinkageName, MDFile *File,
442                                           unsigned LineNo, MDType *Ty,
443                                           bool isLocalToUnit,
444                                           llvm::Constant *Val,
445                                           MDNode *Decl = nullptr);
446
447    /// createTempGlobalVariableFwdDecl - Identical to createGlobalVariable
448    /// except that the resulting DbgNode is temporary and meant to be RAUWed.
449    MDGlobalVariable *createTempGlobalVariableFwdDecl(
450        MDScope *Context, StringRef Name, StringRef LinkageName, MDFile *File,
451        unsigned LineNo, MDType *Ty, bool isLocalToUnit, llvm::Constant *Val,
452        MDNode *Decl = nullptr);
453
454    /// createLocalVariable - Create a new descriptor for the specified
455    /// local variable.
456    /// @param Tag         Dwarf TAG. Usually DW_TAG_auto_variable or
457    ///                    DW_TAG_arg_variable.
458    /// @param Scope       Variable scope.
459    /// @param Name        Variable name.
460    /// @param File        File where this variable is defined.
461    /// @param LineNo      Line number.
462    /// @param Ty          Variable Type
463    /// @param AlwaysPreserve Boolean. Set to true if debug info for this
464    ///                       variable should be preserved in optimized build.
465    /// @param Flags       Flags, e.g. artificial variable.
466    /// @param ArgNo       If this variable is an argument then this argument's
467    ///                    number. 1 indicates 1st argument.
468    MDLocalVariable *createLocalVariable(unsigned Tag, MDScope *Scope,
469                                         StringRef Name, MDFile *File,
470                                         unsigned LineNo, MDType *Ty,
471                                         bool AlwaysPreserve = false,
472                                         unsigned Flags = 0,
473                                         unsigned ArgNo = 0);
474
475    /// createExpression - Create a new descriptor for the specified
476    /// variable which has a complex address expression for its address.
477    /// @param Addr        An array of complex address operations.
478    MDExpression *createExpression(ArrayRef<uint64_t> Addr = None);
479    MDExpression *createExpression(ArrayRef<int64_t> Addr);
480
481    /// createBitPieceExpression - Create a descriptor to describe one part
482    /// of aggregate variable that is fragmented across multiple Values.
483    ///
484    /// @param OffsetInBits Offset of the piece in bits.
485    /// @param SizeInBits   Size of the piece in bits.
486    MDExpression *createBitPieceExpression(unsigned OffsetInBits,
487                                           unsigned SizeInBits);
488
489    /// createFunction - Create a new descriptor for the specified subprogram.
490    /// See comments in MDSubprogram* for descriptions of these fields.
491    /// @param Scope         Function scope.
492    /// @param Name          Function name.
493    /// @param LinkageName   Mangled function name.
494    /// @param File          File where this variable is defined.
495    /// @param LineNo        Line number.
496    /// @param Ty            Function type.
497    /// @param isLocalToUnit True if this function is not externally visible.
498    /// @param isDefinition  True if this is a function definition.
499    /// @param ScopeLine     Set to the beginning of the scope this starts
500    /// @param Flags         e.g. is this function prototyped or not.
501    ///                      These flags are used to emit dwarf attributes.
502    /// @param isOptimized   True if optimization is ON.
503    /// @param Fn            llvm::Function pointer.
504    /// @param TParam        Function template parameters.
505    MDSubprogram *
506    createFunction(MDScope *Scope, StringRef Name, StringRef LinkageName,
507                   MDFile *File, unsigned LineNo, MDSubroutineType *Ty,
508                   bool isLocalToUnit, bool isDefinition, unsigned ScopeLine,
509                   unsigned Flags = 0, bool isOptimized = false,
510                   Function *Fn = nullptr, MDNode *TParam = nullptr,
511                   MDNode *Decl = nullptr);
512
513    /// createTempFunctionFwdDecl - Identical to createFunction,
514    /// except that the resulting DbgNode is meant to be RAUWed.
515    MDSubprogram *createTempFunctionFwdDecl(
516        MDScope *Scope, StringRef Name, StringRef LinkageName, MDFile *File,
517        unsigned LineNo, MDSubroutineType *Ty, bool isLocalToUnit,
518        bool isDefinition, unsigned ScopeLine, unsigned Flags = 0,
519        bool isOptimized = false, Function *Fn = nullptr,
520        MDNode *TParam = nullptr, MDNode *Decl = nullptr);
521
522    /// FIXME: this is added for dragonegg. Once we update dragonegg
523    /// to call resolve function, this will be removed.
524    MDSubprogram *
525    createFunction(DIScopeRef Scope, StringRef Name, StringRef LinkageName,
526                   MDFile *File, unsigned LineNo, MDSubroutineType *Ty,
527                   bool isLocalToUnit, bool isDefinition, unsigned ScopeLine,
528                   unsigned Flags = 0, bool isOptimized = false,
529                   Function *Fn = nullptr, MDNode *TParam = nullptr,
530                   MDNode *Decl = nullptr);
531
532    /// createMethod - Create a new descriptor for the specified C++ method.
533    /// See comments in MDSubprogram* for descriptions of these fields.
534    /// @param Scope         Function scope.
535    /// @param Name          Function name.
536    /// @param LinkageName   Mangled function name.
537    /// @param File          File where this variable is defined.
538    /// @param LineNo        Line number.
539    /// @param Ty            Function type.
540    /// @param isLocalToUnit True if this function is not externally visible..
541    /// @param isDefinition  True if this is a function definition.
542    /// @param Virtuality    Attributes describing virtualness. e.g. pure
543    ///                      virtual function.
544    /// @param VTableIndex   Index no of this method in virtual table.
545    /// @param VTableHolder  Type that holds vtable.
546    /// @param Flags         e.g. is this function prototyped or not.
547    ///                      This flags are used to emit dwarf attributes.
548    /// @param isOptimized   True if optimization is ON.
549    /// @param Fn            llvm::Function pointer.
550    /// @param TParam        Function template parameters.
551    MDSubprogram *
552    createMethod(MDScope *Scope, StringRef Name, StringRef LinkageName,
553                 MDFile *File, unsigned LineNo, MDSubroutineType *Ty,
554                 bool isLocalToUnit, bool isDefinition, unsigned Virtuality = 0,
555                 unsigned VTableIndex = 0, MDType *VTableHolder = nullptr,
556                 unsigned Flags = 0, bool isOptimized = false,
557                 Function *Fn = nullptr, MDNode *TParam = nullptr);
558
559    /// createNameSpace - This creates new descriptor for a namespace
560    /// with the specified parent scope.
561    /// @param Scope       Namespace scope
562    /// @param Name        Name of this namespace
563    /// @param File        Source file
564    /// @param LineNo      Line number
565    MDNamespace *createNameSpace(MDScope *Scope, StringRef Name, MDFile *File,
566                                 unsigned LineNo);
567
568    /// createLexicalBlockFile - This creates a descriptor for a lexical
569    /// block with a new file attached. This merely extends the existing
570    /// lexical block as it crosses a file.
571    /// @param Scope       Lexical block.
572    /// @param File        Source file.
573    /// @param Discriminator DWARF path discriminator value.
574    MDLexicalBlockFile *createLexicalBlockFile(MDScope *Scope, MDFile *File,
575                                               unsigned Discriminator = 0);
576
577    /// createLexicalBlock - This creates a descriptor for a lexical block
578    /// with the specified parent context.
579    /// @param Scope         Parent lexical scope.
580    /// @param File          Source file.
581    /// @param Line          Line number.
582    /// @param Col           Column number.
583    MDLexicalBlock *createLexicalBlock(MDScope *Scope, MDFile *File,
584                                       unsigned Line, unsigned Col);
585
586    /// \brief Create a descriptor for an imported module.
587    /// @param Context The scope this module is imported into
588    /// @param NS The namespace being imported here
589    /// @param Line Line number
590    MDImportedEntity *createImportedModule(MDScope *Context, MDNamespace *NS,
591                                           unsigned Line);
592
593    /// \brief Create a descriptor for an imported module.
594    /// @param Context The scope this module is imported into
595    /// @param NS An aliased namespace
596    /// @param Line Line number
597    MDImportedEntity *createImportedModule(MDScope *Context,
598                                           MDImportedEntity *NS, unsigned Line);
599
600    /// \brief Create a descriptor for an imported function.
601    /// @param Context The scope this module is imported into
602    /// @param Decl The declaration (or definition) of a function, type, or
603    ///             variable
604    /// @param Line Line number
605    MDImportedEntity *createImportedDeclaration(MDScope *Context,
606                                                DebugNode *Decl, unsigned Line,
607                                                StringRef Name = "");
608
609    /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
610    /// @param Storage     llvm::Value of the variable
611    /// @param VarInfo     Variable's debug info descriptor.
612    /// @param Expr         A complex location expression.
613    /// @param DL           Debug info location.
614    /// @param InsertAtEnd Location for the new intrinsic.
615    Instruction *insertDeclare(llvm::Value *Storage, MDLocalVariable *VarInfo,
616                               MDExpression *Expr, const MDLocation *DL,
617                               BasicBlock *InsertAtEnd);
618
619    /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
620    /// @param Storage      llvm::Value of the variable
621    /// @param VarInfo      Variable's debug info descriptor.
622    /// @param Expr         A complex location expression.
623    /// @param DL           Debug info location.
624    /// @param InsertBefore Location for the new intrinsic.
625    Instruction *insertDeclare(llvm::Value *Storage, MDLocalVariable *VarInfo,
626                               MDExpression *Expr, const MDLocation *DL,
627                               Instruction *InsertBefore);
628
629    /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
630    /// @param Val          llvm::Value of the variable
631    /// @param Offset       Offset
632    /// @param VarInfo      Variable's debug info descriptor.
633    /// @param Expr         A complex location expression.
634    /// @param DL           Debug info location.
635    /// @param InsertAtEnd Location for the new intrinsic.
636    Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
637                                         MDLocalVariable *VarInfo,
638                                         MDExpression *Expr,
639                                         const MDLocation *DL,
640                                         BasicBlock *InsertAtEnd);
641
642    /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
643    /// @param Val          llvm::Value of the variable
644    /// @param Offset       Offset
645    /// @param VarInfo      Variable's debug info descriptor.
646    /// @param Expr         A complex location expression.
647    /// @param DL           Debug info location.
648    /// @param InsertBefore Location for the new intrinsic.
649    Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
650                                         MDLocalVariable *VarInfo,
651                                         MDExpression *Expr,
652                                         const MDLocation *DL,
653                                         Instruction *InsertBefore);
654
655    /// \brief Replace the vtable holder in the given composite type.
656    ///
657    /// If this creates a self reference, it may orphan some unresolved cycles
658    /// in the operands of \c T, so \a DIBuilder needs to track that.
659    void replaceVTableHolder(MDCompositeType *&T,
660                             MDCompositeType *VTableHolder);
661
662    /// \brief Replace arrays on a composite type.
663    ///
664    /// If \c T is resolved, but the arrays aren't -- which can happen if \c T
665    /// has a self-reference -- \a DIBuilder needs to track the array to
666    /// resolve cycles.
667    void replaceArrays(MDCompositeType *&T, DIArray Elements,
668                       DIArray TParems = DIArray());
669
670    /// \brief Replace a temporary node.
671    ///
672    /// Call \a MDNode::replaceAllUsesWith() on \c N, replacing it with \c
673    /// Replacement.
674    ///
675    /// If \c Replacement is the same as \c N.get(), instead call \a
676    /// MDNode::replaceWithUniqued().  In this case, the uniqued node could
677    /// have a different address, so we return the final address.
678    template <class NodeTy>
679    NodeTy *replaceTemporary(TempMDNode &&N, NodeTy *Replacement) {
680      if (N.get() == Replacement)
681        return cast<NodeTy>(MDNode::replaceWithUniqued(std::move(N)));
682
683      N->replaceAllUsesWith(Replacement);
684      return Replacement;
685    }
686  };
687} // end namespace llvm
688
689#endif
690