[C++11] More 'nullptr' conversion or in some cases just using a boolean check instead...
[oota-llvm.git] / lib / IR / DIBuilder.cpp
1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
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 implements the DIBuilder.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/DIBuilder.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
22
23 using namespace llvm;
24 using namespace llvm::dwarf;
25
26 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
27   assert((Tag & LLVMDebugVersionMask) == 0 &&
28          "Tag too large for debug encoding!");
29   return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
30 }
31
32 DIBuilder::DIBuilder(Module &m)
33     : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr),
34       TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr),
35       DeclareFn(nullptr), ValueFn(nullptr) {}
36
37 /// finalize - Construct any deferred debug info descriptors.
38 void DIBuilder::finalize() {
39   DIArray Enums = getOrCreateArray(AllEnumTypes);
40   DIType(TempEnumTypes).replaceAllUsesWith(Enums);
41
42   SmallVector<Value *, 16> RetainValues;
43   // Declarations and definitions of the same type may be retained. Some
44   // clients RAUW these pairs, leaving duplicates in the retained types
45   // list. Use a set to remove the duplicates while we transform the
46   // TrackingVHs back into Values.
47   SmallPtrSet<Value *, 16> RetainSet;
48   for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
49     if (RetainSet.insert(AllRetainTypes[I]))
50       RetainValues.push_back(AllRetainTypes[I]);
51   DIArray RetainTypes = getOrCreateArray(RetainValues);
52   DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
53
54   DIArray SPs = getOrCreateArray(AllSubprograms);
55   DIType(TempSubprograms).replaceAllUsesWith(SPs);
56   for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
57     DISubprogram SP(SPs.getElement(i));
58     SmallVector<Value *, 4> Variables;
59     if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
60       for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
61         Variables.push_back(NMD->getOperand(ii));
62       NMD->eraseFromParent();
63     }
64     if (MDNode *Temp = SP.getVariablesNodes()) {
65       DIArray AV = getOrCreateArray(Variables);
66       DIType(Temp).replaceAllUsesWith(AV);
67     }
68   }
69
70   DIArray GVs = getOrCreateArray(AllGVs);
71   DIType(TempGVs).replaceAllUsesWith(GVs);
72
73   SmallVector<Value *, 16> RetainValuesI;
74   for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
75     RetainValuesI.push_back(AllImportedModules[I]);
76   DIArray IMs = getOrCreateArray(RetainValuesI);
77   DIType(TempImportedModules).replaceAllUsesWith(IMs);
78 }
79
80 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
81 /// N.
82 static MDNode *getNonCompileUnitScope(MDNode *N) {
83   if (DIDescriptor(N).isCompileUnit())
84     return nullptr;
85   return N;
86 }
87
88 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
89                                   StringRef Directory) {
90   assert(!Filename.empty() && "Unable to create file without name");
91   Value *Pair[] = {
92     MDString::get(VMContext, Filename),
93     MDString::get(VMContext, Directory)
94   };
95   return MDNode::get(VMContext, Pair);
96 }
97
98 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
99 /// information generated during this instance of compilation.
100 DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
101                                            StringRef Directory,
102                                            StringRef Producer, bool isOptimized,
103                                            StringRef Flags, unsigned RunTimeVer,
104                                            StringRef SplitName,
105                                            DebugEmissionKind Kind) {
106
107   assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
108           (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
109          "Invalid Language tag");
110   assert(!Filename.empty() &&
111          "Unable to create compile unit without filename");
112   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
113   TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
114
115   TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
116
117   TempSubprograms = MDNode::getTemporary(VMContext, TElts);
118
119   TempGVs = MDNode::getTemporary(VMContext, TElts);
120
121   TempImportedModules = MDNode::getTemporary(VMContext, TElts);
122
123   Value *Elts[] = {
124     GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
125     createFilePathPair(VMContext, Filename, Directory),
126     ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
127     MDString::get(VMContext, Producer),
128     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
129     MDString::get(VMContext, Flags),
130     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
131     TempEnumTypes,
132     TempRetainTypes,
133     TempSubprograms,
134     TempGVs,
135     TempImportedModules,
136     MDString::get(VMContext, SplitName),
137     ConstantInt::get(Type::getInt32Ty(VMContext), Kind)
138   };
139
140   MDNode *CUNode = MDNode::get(VMContext, Elts);
141
142   // Create a named metadata so that it is easier to find cu in a module.
143   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
144   NMD->addOperand(CUNode);
145
146   return DICompileUnit(CUNode);
147 }
148
149 static DIImportedEntity
150 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
151                      Value *NS, unsigned Line, StringRef Name,
152                      SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
153   const MDNode *R;
154   if (Name.empty()) {
155     Value *Elts[] = {
156       GetTagConstant(C, Tag),
157       Context,
158       NS,
159       ConstantInt::get(Type::getInt32Ty(C), Line),
160     };
161     R = MDNode::get(C, Elts);
162   } else {
163     Value *Elts[] = {
164       GetTagConstant(C, Tag),
165       Context,
166       NS,
167       ConstantInt::get(Type::getInt32Ty(C), Line),
168       MDString::get(C, Name)
169     };
170     R = MDNode::get(C, Elts);
171   }
172   DIImportedEntity M(R);
173   assert(M.Verify() && "Imported module should be valid");
174   AllImportedModules.push_back(TrackingVH<MDNode>(M));
175   return M;
176 }
177
178 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
179                                                  DINameSpace NS,
180                                                  unsigned Line) {
181   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
182                                 Context, NS, Line, StringRef(), AllImportedModules);
183 }
184
185 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
186                                                  DIImportedEntity NS,
187                                                  unsigned Line) {
188   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
189                                 Context, NS, Line, StringRef(), AllImportedModules);
190 }
191
192 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
193                                                       DIScope Decl,
194                                                       unsigned Line, StringRef Name) {
195   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
196                                 Context, Decl.getRef(), Line, Name,
197                                 AllImportedModules);
198 }
199
200 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
201                                                       DIImportedEntity Imp,
202                                                       unsigned Line, StringRef Name) {
203   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
204                                 Context, Imp, Line, Name, AllImportedModules);
205 }
206
207 /// createFile - Create a file descriptor to hold debugging information
208 /// for a file.
209 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
210   Value *Elts[] = {
211     GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
212     createFilePathPair(VMContext, Filename, Directory)
213   };
214   return DIFile(MDNode::get(VMContext, Elts));
215 }
216
217 /// createEnumerator - Create a single enumerator value.
218 DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
219   assert(!Name.empty() && "Unable to create enumerator without name");
220   Value *Elts[] = {
221     GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
222     MDString::get(VMContext, Name),
223     ConstantInt::get(Type::getInt64Ty(VMContext), Val)
224   };
225   return DIEnumerator(MDNode::get(VMContext, Elts));
226 }
227
228 /// \brief Create a DWARF unspecified type.
229 DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
230   assert(!Name.empty() && "Unable to create type without name");
231   // Unspecified types are encoded in DIBasicType format. Line number, filename,
232   // size, alignment, offset and flags are always empty here.
233   Value *Elts[] = {
234     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
235     nullptr, // Filename
236     nullptr, // Unused
237     MDString::get(VMContext, Name),
238     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
239     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
240     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
241     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
242     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
243     ConstantInt::get(Type::getInt32Ty(VMContext), 0)  // Encoding
244   };
245   return DIBasicType(MDNode::get(VMContext, Elts));
246 }
247
248 /// \brief Create C++11 nullptr type.
249 DIBasicType DIBuilder::createNullPtrType() {
250   return createUnspecifiedType("decltype(nullptr)");
251 }
252
253 /// createBasicType - Create debugging information entry for a basic
254 /// type, e.g 'char'.
255 DIBasicType
256 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
257                            uint64_t AlignInBits, unsigned Encoding) {
258   assert(!Name.empty() && "Unable to create type without name");
259   // Basic types are encoded in DIBasicType format. Line number, filename,
260   // offset and flags are always empty here.
261   Value *Elts[] = {
262     GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
263     nullptr, // File/directory name
264     nullptr, // Unused
265     MDString::get(VMContext, Name),
266     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
267     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
268     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
269     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
270     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
271     ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
272   };
273   return DIBasicType(MDNode::get(VMContext, Elts));
274 }
275
276 /// createQualifiedType - Create debugging information entry for a qualified
277 /// type, e.g. 'const int'.
278 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
279   // Qualified types are encoded in DIDerivedType format.
280   Value *Elts[] = {
281     GetTagConstant(VMContext, Tag),
282     nullptr, // Filename
283     nullptr, // Unused
284     MDString::get(VMContext, StringRef()), // Empty name.
285     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
286     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
287     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
288     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
289     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
290     FromTy.getRef()
291   };
292   return DIDerivedType(MDNode::get(VMContext, Elts));
293 }
294
295 /// createPointerType - Create debugging information entry for a pointer.
296 DIDerivedType
297 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
298                              uint64_t AlignInBits, StringRef Name) {
299   // Pointer types are encoded in DIDerivedType format.
300   Value *Elts[] = {
301     GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
302     nullptr, // Filename
303     nullptr, // Unused
304     MDString::get(VMContext, Name),
305     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
306     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
307     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
308     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
309     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
310     PointeeTy.getRef()
311   };
312   return DIDerivedType(MDNode::get(VMContext, Elts));
313 }
314
315 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
316                                                  DIType Base) {
317   // Pointer types are encoded in DIDerivedType format.
318   Value *Elts[] = {
319     GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
320     nullptr, // Filename
321     nullptr, // Unused
322     nullptr,
323     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
324     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
325     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
326     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
327     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
328     PointeeTy.getRef(),
329     Base.getRef()
330   };
331   return DIDerivedType(MDNode::get(VMContext, Elts));
332 }
333
334 /// createReferenceType - Create debugging information entry for a reference
335 /// type.
336 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
337   assert(RTy.isType() && "Unable to create reference type");
338   // References are encoded in DIDerivedType format.
339   Value *Elts[] = {
340     GetTagConstant(VMContext, Tag),
341     nullptr, // Filename
342     nullptr, // TheCU,
343     nullptr, // Name
344     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
345     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
346     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
347     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
348     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
349     RTy.getRef()
350   };
351   return DIDerivedType(MDNode::get(VMContext, Elts));
352 }
353
354 /// createTypedef - Create debugging information entry for a typedef.
355 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
356                                        unsigned LineNo, DIDescriptor Context) {
357   // typedefs are encoded in DIDerivedType format.
358   assert(Ty.isType() && "Invalid typedef type!");
359   Value *Elts[] = {
360     GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
361     File.getFileNode(),
362     DIScope(getNonCompileUnitScope(Context)).getRef(),
363     MDString::get(VMContext, Name),
364     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
365     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
366     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
367     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
368     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
369     Ty.getRef()
370   };
371   return DIDerivedType(MDNode::get(VMContext, Elts));
372 }
373
374 /// createFriend - Create debugging information entry for a 'friend'.
375 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
376   // typedefs are encoded in DIDerivedType format.
377   assert(Ty.isType() && "Invalid type!");
378   assert(FriendTy.isType() && "Invalid friend type!");
379   Value *Elts[] = {
380     GetTagConstant(VMContext, dwarf::DW_TAG_friend),
381     nullptr,
382     Ty.getRef(),
383     nullptr, // Name
384     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
385     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
386     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
387     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
388     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
389     FriendTy.getRef()
390   };
391   return DIDerivedType(MDNode::get(VMContext, Elts));
392 }
393
394 /// createInheritance - Create debugging information entry to establish
395 /// inheritance relationship between two types.
396 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
397                                            uint64_t BaseOffset,
398                                            unsigned Flags) {
399   assert(Ty.isType() && "Unable to create inheritance");
400   // TAG_inheritance is encoded in DIDerivedType format.
401   Value *Elts[] = {
402     GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
403     nullptr,
404     Ty.getRef(),
405     nullptr, // Name
406     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
407     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
408     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
409     ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
410     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
411     BaseTy.getRef()
412   };
413   return DIDerivedType(MDNode::get(VMContext, Elts));
414 }
415
416 /// createMemberType - Create debugging information entry for a member.
417 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
418                                           DIFile File, unsigned LineNumber,
419                                           uint64_t SizeInBits,
420                                           uint64_t AlignInBits,
421                                           uint64_t OffsetInBits, unsigned Flags,
422                                           DIType Ty) {
423   // TAG_member is encoded in DIDerivedType format.
424   Value *Elts[] = {
425     GetTagConstant(VMContext, dwarf::DW_TAG_member),
426     File.getFileNode(),
427     DIScope(getNonCompileUnitScope(Scope)).getRef(),
428     MDString::get(VMContext, Name),
429     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
430     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
431     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
432     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
433     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
434     Ty.getRef()
435   };
436   return DIDerivedType(MDNode::get(VMContext, Elts));
437 }
438
439 /// createStaticMemberType - Create debugging information entry for a
440 /// C++ static data member.
441 DIDerivedType
442 DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
443                                   DIFile File, unsigned LineNumber,
444                                   DIType Ty, unsigned Flags,
445                                   llvm::Value *Val) {
446   // TAG_member is encoded in DIDerivedType format.
447   Flags |= DIDescriptor::FlagStaticMember;
448   Value *Elts[] = {
449     GetTagConstant(VMContext, dwarf::DW_TAG_member),
450     File.getFileNode(),
451     DIScope(getNonCompileUnitScope(Scope)).getRef(),
452     MDString::get(VMContext, Name),
453     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
454     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
455     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
456     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
457     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
458     Ty.getRef(),
459     Val
460   };
461   return DIDerivedType(MDNode::get(VMContext, Elts));
462 }
463
464 /// createObjCIVar - Create debugging information entry for Objective-C
465 /// instance variable.
466 DIDerivedType
467 DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber,
468                           uint64_t SizeInBits, uint64_t AlignInBits,
469                           uint64_t OffsetInBits, unsigned Flags, DIType Ty,
470                           StringRef PropertyName, StringRef GetterName,
471                           StringRef SetterName, unsigned PropertyAttributes) {
472   // TAG_member is encoded in DIDerivedType format.
473   Value *Elts[] = {
474     GetTagConstant(VMContext, dwarf::DW_TAG_member),
475     File.getFileNode(),
476     getNonCompileUnitScope(File),
477     MDString::get(VMContext, Name),
478     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
479     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
480     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
481     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
482     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
483     Ty,
484     MDString::get(VMContext, PropertyName),
485     MDString::get(VMContext, GetterName),
486     MDString::get(VMContext, SetterName),
487     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
488   };
489   return DIDerivedType(MDNode::get(VMContext, Elts));
490 }
491
492 /// createObjCIVar - Create debugging information entry for Objective-C
493 /// instance variable.
494 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
495                                         unsigned LineNumber,
496                                         uint64_t SizeInBits,
497                                         uint64_t AlignInBits,
498                                         uint64_t OffsetInBits, unsigned Flags,
499                                         DIType Ty, MDNode *PropertyNode) {
500   // TAG_member is encoded in DIDerivedType format.
501   Value *Elts[] = {
502     GetTagConstant(VMContext, dwarf::DW_TAG_member),
503     File.getFileNode(),
504     getNonCompileUnitScope(File),
505     MDString::get(VMContext, Name),
506     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
507     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
508     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
509     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
510     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
511     Ty,
512     PropertyNode
513   };
514   return DIDerivedType(MDNode::get(VMContext, Elts));
515 }
516
517 /// createObjCProperty - Create debugging information entry for Objective-C
518 /// property.
519 DIObjCProperty
520 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
521                               StringRef GetterName, StringRef SetterName,
522                               unsigned PropertyAttributes, DIType Ty) {
523   Value *Elts[] = {
524     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
525     MDString::get(VMContext, Name),
526     File,
527     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
528     MDString::get(VMContext, GetterName),
529     MDString::get(VMContext, SetterName),
530     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
531     Ty
532   };
533   return DIObjCProperty(MDNode::get(VMContext, Elts));
534 }
535
536 /// createTemplateTypeParameter - Create debugging information for template
537 /// type parameter.
538 DITemplateTypeParameter
539 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
540                                        DIType Ty, MDNode *File, unsigned LineNo,
541                                        unsigned ColumnNo) {
542   Value *Elts[] = {
543     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
544     DIScope(getNonCompileUnitScope(Context)).getRef(),
545     MDString::get(VMContext, Name),
546     Ty.getRef(),
547     File,
548     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
549     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
550   };
551   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
552 }
553
554 DITemplateValueParameter
555 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
556                                         StringRef Name, DIType Ty,
557                                         Value *Val, MDNode *File,
558                                         unsigned LineNo,
559                                         unsigned ColumnNo) {
560   Value *Elts[] = {
561     GetTagConstant(VMContext, Tag),
562     DIScope(getNonCompileUnitScope(Context)).getRef(),
563     MDString::get(VMContext, Name),
564     Ty.getRef(),
565     Val,
566     File,
567     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
568     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
569   };
570   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
571 }
572
573 /// createTemplateValueParameter - Create debugging information for template
574 /// value parameter.
575 DITemplateValueParameter
576 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
577                                         DIType Ty, Value *Val,
578                                         MDNode *File, unsigned LineNo,
579                                         unsigned ColumnNo) {
580   return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
581                                       Context, Name, Ty, Val, File, LineNo,
582                                       ColumnNo);
583 }
584
585 DITemplateValueParameter
586 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
587                                            DIType Ty, StringRef Val,
588                                            MDNode *File, unsigned LineNo,
589                                            unsigned ColumnNo) {
590   return createTemplateValueParameter(
591       dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
592       MDString::get(VMContext, Val), File, LineNo, ColumnNo);
593 }
594
595 DITemplateValueParameter
596 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
597                                        DIType Ty, DIArray Val,
598                                        MDNode *File, unsigned LineNo,
599                                        unsigned ColumnNo) {
600   return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
601                                       Context, Name, Ty, Val, File, LineNo,
602                                       ColumnNo);
603 }
604
605 /// createClassType - Create debugging information entry for a class.
606 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
607                                            DIFile File, unsigned LineNumber,
608                                            uint64_t SizeInBits,
609                                            uint64_t AlignInBits,
610                                            uint64_t OffsetInBits,
611                                            unsigned Flags, DIType DerivedFrom,
612                                            DIArray Elements,
613                                            DIType VTableHolder,
614                                            MDNode *TemplateParams,
615                                            StringRef UniqueIdentifier) {
616   assert((!Context || Context.isScope() || Context.isType()) &&
617          "createClassType should be called with a valid Context");
618   // TAG_class_type is encoded in DICompositeType format.
619   Value *Elts[] = {
620     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
621     File.getFileNode(),
622     DIScope(getNonCompileUnitScope(Context)).getRef(),
623     MDString::get(VMContext, Name),
624     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
625     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
626     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
627     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
628     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
629     DerivedFrom.getRef(),
630     Elements,
631     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
632     VTableHolder.getRef(),
633     TemplateParams,
634     UniqueIdentifier.empty() ? nullptr
635                              : MDString::get(VMContext, UniqueIdentifier)
636   };
637   DICompositeType R(MDNode::get(VMContext, Elts));
638   assert(R.isCompositeType() &&
639          "createClassType should return a DICompositeType");
640   if (!UniqueIdentifier.empty())
641     retainType(R);
642   return R;
643 }
644
645 /// createStructType - Create debugging information entry for a struct.
646 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
647                                             StringRef Name, DIFile File,
648                                             unsigned LineNumber,
649                                             uint64_t SizeInBits,
650                                             uint64_t AlignInBits,
651                                             unsigned Flags, DIType DerivedFrom,
652                                             DIArray Elements,
653                                             unsigned RunTimeLang,
654                                             DIType VTableHolder,
655                                             StringRef UniqueIdentifier) {
656  // TAG_structure_type is encoded in DICompositeType format.
657   Value *Elts[] = {
658     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
659     File.getFileNode(),
660     DIScope(getNonCompileUnitScope(Context)).getRef(),
661     MDString::get(VMContext, Name),
662     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
663     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
664     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
665     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
666     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
667     DerivedFrom.getRef(),
668     Elements,
669     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
670     VTableHolder.getRef(),
671     nullptr,
672     UniqueIdentifier.empty() ? nullptr
673                              : MDString::get(VMContext, UniqueIdentifier)
674   };
675   DICompositeType R(MDNode::get(VMContext, Elts));
676   assert(R.isCompositeType() &&
677          "createStructType should return a DICompositeType");
678   if (!UniqueIdentifier.empty())
679     retainType(R);
680   return R;
681 }
682
683 /// createUnionType - Create debugging information entry for an union.
684 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
685                                            DIFile File, unsigned LineNumber,
686                                            uint64_t SizeInBits,
687                                            uint64_t AlignInBits, unsigned Flags,
688                                            DIArray Elements,
689                                            unsigned RunTimeLang,
690                                            StringRef UniqueIdentifier) {
691   // TAG_union_type is encoded in DICompositeType format.
692   Value *Elts[] = {
693     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
694     File.getFileNode(),
695     DIScope(getNonCompileUnitScope(Scope)).getRef(),
696     MDString::get(VMContext, Name),
697     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
698     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
699     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
700     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
701     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
702     nullptr,
703     Elements,
704     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
705     nullptr,
706     nullptr,
707     UniqueIdentifier.empty() ? nullptr
708                              : MDString::get(VMContext, UniqueIdentifier)
709   };
710   DICompositeType R(MDNode::get(VMContext, Elts));
711   if (!UniqueIdentifier.empty())
712     retainType(R);
713   return R;
714 }
715
716 /// createSubroutineType - Create subroutine type.
717 DICompositeType DIBuilder::createSubroutineType(DIFile File,
718                                                 DIArray ParameterTypes,
719                                                 unsigned Flags) {
720   // TAG_subroutine_type is encoded in DICompositeType format.
721   Value *Elts[] = {
722     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
723     Constant::getNullValue(Type::getInt32Ty(VMContext)),
724     nullptr,
725     MDString::get(VMContext, ""),
726     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
727     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
728     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
729     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
730     ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
731     nullptr,
732     ParameterTypes,
733     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
734     nullptr,
735     nullptr,
736     nullptr  // Type Identifer
737   };
738   return DICompositeType(MDNode::get(VMContext, Elts));
739 }
740
741 /// createEnumerationType - Create debugging information entry for an
742 /// enumeration.
743 DICompositeType DIBuilder::createEnumerationType(
744     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
745     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
746     DIType UnderlyingType, StringRef UniqueIdentifier) {
747   // TAG_enumeration_type is encoded in DICompositeType format.
748   Value *Elts[] = {
749     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
750     File.getFileNode(),
751     DIScope(getNonCompileUnitScope(Scope)).getRef(),
752     MDString::get(VMContext, Name),
753     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
754     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
755     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
756     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
757     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
758     UnderlyingType.getRef(),
759     Elements,
760     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
761     nullptr,
762     nullptr,
763     UniqueIdentifier.empty() ? nullptr
764                              : MDString::get(VMContext, UniqueIdentifier)
765   };
766   DICompositeType CTy(MDNode::get(VMContext, Elts));
767   AllEnumTypes.push_back(CTy);
768   if (!UniqueIdentifier.empty())
769     retainType(CTy);
770   return CTy;
771 }
772
773 /// createArrayType - Create debugging information entry for an array.
774 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
775                                            DIType Ty, DIArray Subscripts) {
776   // TAG_array_type is encoded in DICompositeType format.
777   Value *Elts[] = {
778     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
779     nullptr, // Filename/Directory,
780     nullptr, // Unused
781     MDString::get(VMContext, ""),
782     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
783     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
784     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
785     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
786     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
787     Ty.getRef(),
788     Subscripts,
789     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
790     nullptr,
791     nullptr,
792     nullptr  // Type Identifer
793   };
794   return DICompositeType(MDNode::get(VMContext, Elts));
795 }
796
797 /// createVectorType - Create debugging information entry for a vector.
798 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
799                                             DIType Ty, DIArray Subscripts) {
800   // A vector is an array type with the FlagVector flag applied.
801   Value *Elts[] = {
802     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
803     nullptr, // Filename/Directory,
804     nullptr, // Unused
805     MDString::get(VMContext, ""),
806     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
807     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
808     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
809     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
810     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
811     Ty.getRef(),
812     Subscripts,
813     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
814     nullptr,
815     nullptr,
816     nullptr  // Type Identifer
817   };
818   return DICompositeType(MDNode::get(VMContext, Elts));
819 }
820
821 /// createArtificialType - Create a new DIType with "artificial" flag set.
822 DIType DIBuilder::createArtificialType(DIType Ty) {
823   if (Ty.isArtificial())
824     return Ty;
825
826   SmallVector<Value *, 9> Elts;
827   MDNode *N = Ty;
828   assert (N && "Unexpected input DIType!");
829   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
830     Elts.push_back(N->getOperand(i));
831
832   unsigned CurFlags = Ty.getFlags();
833   CurFlags = CurFlags | DIType::FlagArtificial;
834
835   // Flags are stored at this slot.
836   // FIXME: Add an enum for this magic value.
837   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
838
839   return DIType(MDNode::get(VMContext, Elts));
840 }
841
842 /// createObjectPointerType - Create a new type with both the object pointer
843 /// and artificial flags set.
844 DIType DIBuilder::createObjectPointerType(DIType Ty) {
845   if (Ty.isObjectPointer())
846     return Ty;
847
848   SmallVector<Value *, 9> Elts;
849   MDNode *N = Ty;
850   assert (N && "Unexpected input DIType!");
851   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
852     Elts.push_back(N->getOperand(i));
853
854   unsigned CurFlags = Ty.getFlags();
855   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
856
857   // Flags are stored at this slot.
858   // FIXME: Add an enum for this magic value.
859   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
860
861   return DIType(MDNode::get(VMContext, Elts));
862 }
863
864 /// retainType - Retain DIType in a module even if it is not referenced
865 /// through debug info anchors.
866 void DIBuilder::retainType(DIType T) {
867   AllRetainTypes.push_back(TrackingVH<MDNode>(T));
868 }
869
870 /// createUnspecifiedParameter - Create unspeicified type descriptor
871 /// for the subroutine type.
872 DIDescriptor DIBuilder::createUnspecifiedParameter() {
873   Value *Elts[] = {
874     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
875   };
876   return DIDescriptor(MDNode::get(VMContext, Elts));
877 }
878
879 /// createForwardDecl - Create a temporary forward-declared type that
880 /// can be RAUW'd if the full type is seen.
881 DICompositeType
882 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
883                              DIFile F, unsigned Line, unsigned RuntimeLang,
884                              uint64_t SizeInBits, uint64_t AlignInBits,
885                              StringRef UniqueIdentifier) {
886   // Create a temporary MDNode.
887   Value *Elts[] = {
888     GetTagConstant(VMContext, Tag),
889     F.getFileNode(),
890     DIScope(getNonCompileUnitScope(Scope)).getRef(),
891     MDString::get(VMContext, Name),
892     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
893     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
894     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
895     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
896     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
897     nullptr,
898     DIArray(),
899     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
900     nullptr,
901     nullptr, //TemplateParams
902     UniqueIdentifier.empty() ? nullptr
903                              : MDString::get(VMContext, UniqueIdentifier)
904   };
905   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
906   DICompositeType RetTy(Node);
907   assert(RetTy.isCompositeType() &&
908          "createForwardDecl result should be a DIType");
909   if (!UniqueIdentifier.empty())
910     retainType(RetTy);
911   return RetTy;
912 }
913
914 /// getOrCreateArray - Get a DIArray, create one if required.
915 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
916   return DIArray(MDNode::get(VMContext, Elements));
917 }
918
919 /// getOrCreateSubrange - Create a descriptor for a value range.  This
920 /// implicitly uniques the values returned.
921 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
922   Value *Elts[] = {
923     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
924     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
925     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
926   };
927
928   return DISubrange(MDNode::get(VMContext, Elts));
929 }
930
931 /// \brief Create a new descriptor for the specified global.
932 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
933                                                  StringRef LinkageName,
934                                                  DIFile F, unsigned LineNumber,
935                                                  DITypeRef Ty, bool isLocalToUnit,
936                                                  Value *Val) {
937   Value *Elts[] = {
938     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
939     Constant::getNullValue(Type::getInt32Ty(VMContext)),
940     nullptr, // TheCU,
941     MDString::get(VMContext, Name),
942     MDString::get(VMContext, Name),
943     MDString::get(VMContext, LinkageName),
944     F,
945     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
946     Ty,
947     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
948     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
949     Val,
950     DIDescriptor()
951   };
952   MDNode *Node = MDNode::get(VMContext, Elts);
953   AllGVs.push_back(Node);
954   return DIGlobalVariable(Node);
955 }
956
957 /// \brief Create a new descriptor for the specified global.
958 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
959                                                  unsigned LineNumber,
960                                                  DITypeRef Ty,
961                                                  bool isLocalToUnit,
962                                                  Value *Val) {
963   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
964                               Val);
965 }
966
967 /// createStaticVariable - Create a new descriptor for the specified static
968 /// variable.
969 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
970                                                  StringRef Name,
971                                                  StringRef LinkageName,
972                                                  DIFile F, unsigned LineNumber,
973                                                  DITypeRef Ty,
974                                                  bool isLocalToUnit,
975                                                  Value *Val, MDNode *Decl) {
976   Value *Elts[] = {
977     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
978     Constant::getNullValue(Type::getInt32Ty(VMContext)),
979     getNonCompileUnitScope(Context),
980     MDString::get(VMContext, Name),
981     MDString::get(VMContext, Name),
982     MDString::get(VMContext, LinkageName),
983     F,
984     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
985     Ty,
986     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
987     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
988     Val,
989     DIDescriptor(Decl)
990   };
991   MDNode *Node = MDNode::get(VMContext, Elts);
992   AllGVs.push_back(Node);
993   return DIGlobalVariable(Node);
994 }
995
996 /// createVariable - Create a new descriptor for the specified variable.
997 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
998                                           StringRef Name, DIFile File,
999                                           unsigned LineNo, DITypeRef Ty,
1000                                           bool AlwaysPreserve, unsigned Flags,
1001                                           unsigned ArgNo) {
1002   DIDescriptor Context(getNonCompileUnitScope(Scope));
1003   assert((!Context || Context.isScope()) &&
1004          "createLocalVariable should be called with a valid Context");
1005   Value *Elts[] = {
1006     GetTagConstant(VMContext, Tag),
1007     getNonCompileUnitScope(Scope),
1008     MDString::get(VMContext, Name),
1009     File,
1010     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
1011     Ty,
1012     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1013     Constant::getNullValue(Type::getInt32Ty(VMContext))
1014   };
1015   MDNode *Node = MDNode::get(VMContext, Elts);
1016   if (AlwaysPreserve) {
1017     // The optimizer may remove local variable. If there is an interest
1018     // to preserve variable info in such situation then stash it in a
1019     // named mdnode.
1020     DISubprogram Fn(getDISubprogram(Scope));
1021     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
1022     FnLocals->addOperand(Node);
1023   }
1024   DIVariable RetVar(Node);
1025   assert(RetVar.isVariable() &&
1026          "createLocalVariable should return a valid DIVariable");
1027   return RetVar;
1028 }
1029
1030 /// createComplexVariable - Create a new descriptor for the specified variable
1031 /// which has a complex address expression for its address.
1032 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
1033                                             StringRef Name, DIFile F,
1034                                             unsigned LineNo,
1035                                             DITypeRef Ty,
1036                                             ArrayRef<Value *> Addr,
1037                                             unsigned ArgNo) {
1038   SmallVector<Value *, 15> Elts;
1039   Elts.push_back(GetTagConstant(VMContext, Tag));
1040   Elts.push_back(getNonCompileUnitScope(Scope)),
1041   Elts.push_back(MDString::get(VMContext, Name));
1042   Elts.push_back(F);
1043   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1044                                   (LineNo | (ArgNo << 24))));
1045   Elts.push_back(Ty);
1046   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1047   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1048   Elts.append(Addr.begin(), Addr.end());
1049
1050   return DIVariable(MDNode::get(VMContext, Elts));
1051 }
1052
1053 /// createFunction - Create a new descriptor for the specified function.
1054 /// FIXME: this is added for dragonegg. Once we update dragonegg
1055 /// to call resolve function, this will be removed.
1056 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
1057                                        StringRef LinkageName, DIFile File,
1058                                        unsigned LineNo, DICompositeType Ty,
1059                                        bool isLocalToUnit, bool isDefinition,
1060                                        unsigned ScopeLine, unsigned Flags,
1061                                        bool isOptimized, Function *Fn,
1062                                        MDNode *TParams, MDNode *Decl) {
1063   // dragonegg does not generate identifier for types, so using an empty map
1064   // to resolve the context should be fine.
1065   DITypeIdentifierMap EmptyMap;
1066   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
1067                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1068                         Flags, isOptimized, Fn, TParams, Decl);
1069 }
1070
1071 /// createFunction - Create a new descriptor for the specified function.
1072 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1073                                        StringRef LinkageName, DIFile File,
1074                                        unsigned LineNo, DICompositeType Ty,
1075                                        bool isLocalToUnit, bool isDefinition,
1076                                        unsigned ScopeLine, unsigned Flags,
1077                                        bool isOptimized, Function *Fn,
1078                                        MDNode *TParams, MDNode *Decl) {
1079   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1080          "function types should be subroutines");
1081   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1082   Value *Elts[] = {
1083     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1084     File.getFileNode(),
1085     DIScope(getNonCompileUnitScope(Context)).getRef(),
1086     MDString::get(VMContext, Name),
1087     MDString::get(VMContext, Name),
1088     MDString::get(VMContext, LinkageName),
1089     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1090     Ty,
1091     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1092     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1093     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1094     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1095     nullptr,
1096     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1097     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1098     Fn,
1099     TParams,
1100     Decl,
1101     MDNode::getTemporary(VMContext, TElts),
1102     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1103   };
1104   MDNode *Node = MDNode::get(VMContext, Elts);
1105
1106   // Create a named metadata so that we do not lose this mdnode.
1107   if (isDefinition)
1108     AllSubprograms.push_back(Node);
1109   DISubprogram S(Node);
1110   assert(S.isSubprogram() &&
1111          "createFunction should return a valid DISubprogram");
1112   return S;
1113 }
1114
1115 /// createMethod - Create a new descriptor for the specified C++ method.
1116 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1117                                      StringRef LinkageName, DIFile F,
1118                                      unsigned LineNo, DICompositeType Ty,
1119                                      bool isLocalToUnit, bool isDefinition,
1120                                      unsigned VK, unsigned VIndex,
1121                                      DIType VTableHolder, unsigned Flags,
1122                                      bool isOptimized, Function *Fn,
1123                                      MDNode *TParam) {
1124   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1125          "function types should be subroutines");
1126   assert(getNonCompileUnitScope(Context) &&
1127          "Methods should have both a Context and a context that isn't "
1128          "the compile unit.");
1129   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1130   Value *Elts[] = {
1131     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1132     F.getFileNode(),
1133     DIScope(Context).getRef(),
1134     MDString::get(VMContext, Name),
1135     MDString::get(VMContext, Name),
1136     MDString::get(VMContext, LinkageName),
1137     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1138     Ty,
1139     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1140     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1141     ConstantInt::get(Type::getInt32Ty(VMContext), VK),
1142     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1143     VTableHolder.getRef(),
1144     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1145     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1146     Fn,
1147     TParam,
1148     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1149     MDNode::getTemporary(VMContext, TElts),
1150     // FIXME: Do we want to use different scope/lines?
1151     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1152   };
1153   MDNode *Node = MDNode::get(VMContext, Elts);
1154   if (isDefinition)
1155     AllSubprograms.push_back(Node);
1156   DISubprogram S(Node);
1157   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1158   return S;
1159 }
1160
1161 /// createNameSpace - This creates new descriptor for a namespace
1162 /// with the specified parent scope.
1163 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1164                                        DIFile File, unsigned LineNo) {
1165   Value *Elts[] = {
1166     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1167     File.getFileNode(),
1168     getNonCompileUnitScope(Scope),
1169     MDString::get(VMContext, Name),
1170     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1171   };
1172   DINameSpace R(MDNode::get(VMContext, Elts));
1173   assert(R.Verify() &&
1174          "createNameSpace should return a verifiable DINameSpace");
1175   return R;
1176 }
1177
1178 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1179 /// an existing scope with a new filename.
1180 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1181                                                      DIFile File) {
1182   Value *Elts[] = {
1183     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1184     File.getFileNode(),
1185     Scope
1186   };
1187   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1188   assert(
1189       R.Verify() &&
1190       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1191   return R;
1192 }
1193
1194 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1195                                              unsigned Line, unsigned Col,
1196                                              unsigned Discriminator) {
1197   // Defeat MDNode uniquing for lexical blocks by using unique id.
1198   static unsigned int unique_id = 0;
1199   Value *Elts[] = {
1200     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1201     File.getFileNode(),
1202     getNonCompileUnitScope(Scope),
1203     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1204     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1205     ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
1206     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1207   };
1208   DILexicalBlock R(MDNode::get(VMContext, Elts));
1209   assert(R.Verify() &&
1210          "createLexicalBlock should return a verifiable DILexicalBlock");
1211   return R;
1212 }
1213
1214 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1215 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1216                                       Instruction *InsertBefore) {
1217   assert(Storage && "no storage passed to dbg.declare");
1218   assert(VarInfo.isVariable() &&
1219          "empty or invalid DIVariable passed to dbg.declare");
1220   if (!DeclareFn)
1221     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1222
1223   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1224   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1225 }
1226
1227 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1228 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1229                                       BasicBlock *InsertAtEnd) {
1230   assert(Storage && "no storage passed to dbg.declare");
1231   assert(VarInfo.isVariable() &&
1232          "empty or invalid DIVariable passed to dbg.declare");
1233   if (!DeclareFn)
1234     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1235
1236   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1237
1238   // If this block already has a terminator then insert this intrinsic
1239   // before the terminator.
1240   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1241     return CallInst::Create(DeclareFn, Args, "", T);
1242   else
1243     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1244 }
1245
1246 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1247 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1248                                                 DIVariable VarInfo,
1249                                                 Instruction *InsertBefore) {
1250   assert(V && "no value passed to dbg.value");
1251   assert(VarInfo.isVariable() &&
1252          "empty or invalid DIVariable passed to dbg.value");
1253   if (!ValueFn)
1254     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1255
1256   Value *Args[] = { MDNode::get(V->getContext(), V),
1257                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1258                     VarInfo };
1259   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1260 }
1261
1262 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1263 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1264                                                 DIVariable VarInfo,
1265                                                 BasicBlock *InsertAtEnd) {
1266   assert(V && "no value passed to dbg.value");
1267   assert(VarInfo.isVariable() &&
1268          "empty or invalid DIVariable passed to dbg.value");
1269   if (!ValueFn)
1270     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1271
1272   Value *Args[] = { MDNode::get(V->getContext(), V),
1273                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1274                     VarInfo };
1275   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1276 }