PR19623: Implement typedefs of void.
[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_OCaml && 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   Value *Elts[] = {
359     GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
360     File.getFileNode(),
361     DIScope(getNonCompileUnitScope(Context)).getRef(),
362     MDString::get(VMContext, Name),
363     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
364     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
365     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
366     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
367     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
368     Ty.getRef()
369   };
370   return DIDerivedType(MDNode::get(VMContext, Elts));
371 }
372
373 /// createFriend - Create debugging information entry for a 'friend'.
374 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
375   // typedefs are encoded in DIDerivedType format.
376   assert(Ty.isType() && "Invalid type!");
377   assert(FriendTy.isType() && "Invalid friend type!");
378   Value *Elts[] = {
379     GetTagConstant(VMContext, dwarf::DW_TAG_friend),
380     nullptr,
381     Ty.getRef(),
382     nullptr, // Name
383     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
384     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
385     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
386     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
387     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
388     FriendTy.getRef()
389   };
390   return DIDerivedType(MDNode::get(VMContext, Elts));
391 }
392
393 /// createInheritance - Create debugging information entry to establish
394 /// inheritance relationship between two types.
395 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
396                                            uint64_t BaseOffset,
397                                            unsigned Flags) {
398   assert(Ty.isType() && "Unable to create inheritance");
399   // TAG_inheritance is encoded in DIDerivedType format.
400   Value *Elts[] = {
401     GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
402     nullptr,
403     Ty.getRef(),
404     nullptr, // Name
405     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
406     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
407     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
408     ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
409     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
410     BaseTy.getRef()
411   };
412   return DIDerivedType(MDNode::get(VMContext, Elts));
413 }
414
415 /// createMemberType - Create debugging information entry for a member.
416 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
417                                           DIFile File, unsigned LineNumber,
418                                           uint64_t SizeInBits,
419                                           uint64_t AlignInBits,
420                                           uint64_t OffsetInBits, unsigned Flags,
421                                           DIType Ty) {
422   // TAG_member is encoded in DIDerivedType format.
423   Value *Elts[] = {
424     GetTagConstant(VMContext, dwarf::DW_TAG_member),
425     File.getFileNode(),
426     DIScope(getNonCompileUnitScope(Scope)).getRef(),
427     MDString::get(VMContext, Name),
428     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
429     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
430     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
431     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
432     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
433     Ty.getRef()
434   };
435   return DIDerivedType(MDNode::get(VMContext, Elts));
436 }
437
438 /// createStaticMemberType - Create debugging information entry for a
439 /// C++ static data member.
440 DIDerivedType
441 DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
442                                   DIFile File, unsigned LineNumber,
443                                   DIType Ty, unsigned Flags,
444                                   llvm::Value *Val) {
445   // TAG_member is encoded in DIDerivedType format.
446   Flags |= DIDescriptor::FlagStaticMember;
447   Value *Elts[] = {
448     GetTagConstant(VMContext, dwarf::DW_TAG_member),
449     File.getFileNode(),
450     DIScope(getNonCompileUnitScope(Scope)).getRef(),
451     MDString::get(VMContext, Name),
452     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
453     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
454     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
455     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
456     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
457     Ty.getRef(),
458     Val
459   };
460   return DIDerivedType(MDNode::get(VMContext, Elts));
461 }
462
463 /// createObjCIVar - Create debugging information entry for Objective-C
464 /// instance variable.
465 DIDerivedType
466 DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber,
467                           uint64_t SizeInBits, uint64_t AlignInBits,
468                           uint64_t OffsetInBits, unsigned Flags, DIType Ty,
469                           StringRef PropertyName, StringRef GetterName,
470                           StringRef SetterName, unsigned PropertyAttributes) {
471   // TAG_member is encoded in DIDerivedType format.
472   Value *Elts[] = {
473     GetTagConstant(VMContext, dwarf::DW_TAG_member),
474     File.getFileNode(),
475     getNonCompileUnitScope(File),
476     MDString::get(VMContext, Name),
477     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
478     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
479     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
480     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
481     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
482     Ty,
483     MDString::get(VMContext, PropertyName),
484     MDString::get(VMContext, GetterName),
485     MDString::get(VMContext, SetterName),
486     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
487   };
488   return DIDerivedType(MDNode::get(VMContext, Elts));
489 }
490
491 /// createObjCIVar - Create debugging information entry for Objective-C
492 /// instance variable.
493 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
494                                         unsigned LineNumber,
495                                         uint64_t SizeInBits,
496                                         uint64_t AlignInBits,
497                                         uint64_t OffsetInBits, unsigned Flags,
498                                         DIType Ty, MDNode *PropertyNode) {
499   // TAG_member is encoded in DIDerivedType format.
500   Value *Elts[] = {
501     GetTagConstant(VMContext, dwarf::DW_TAG_member),
502     File.getFileNode(),
503     getNonCompileUnitScope(File),
504     MDString::get(VMContext, Name),
505     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
506     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
507     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
508     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
509     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
510     Ty,
511     PropertyNode
512   };
513   return DIDerivedType(MDNode::get(VMContext, Elts));
514 }
515
516 /// createObjCProperty - Create debugging information entry for Objective-C
517 /// property.
518 DIObjCProperty
519 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
520                               StringRef GetterName, StringRef SetterName,
521                               unsigned PropertyAttributes, DIType Ty) {
522   Value *Elts[] = {
523     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
524     MDString::get(VMContext, Name),
525     File,
526     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
527     MDString::get(VMContext, GetterName),
528     MDString::get(VMContext, SetterName),
529     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
530     Ty
531   };
532   return DIObjCProperty(MDNode::get(VMContext, Elts));
533 }
534
535 /// createTemplateTypeParameter - Create debugging information for template
536 /// type parameter.
537 DITemplateTypeParameter
538 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
539                                        DIType Ty, MDNode *File, unsigned LineNo,
540                                        unsigned ColumnNo) {
541   Value *Elts[] = {
542     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
543     DIScope(getNonCompileUnitScope(Context)).getRef(),
544     MDString::get(VMContext, Name),
545     Ty.getRef(),
546     File,
547     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
548     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
549   };
550   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
551 }
552
553 DITemplateValueParameter
554 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
555                                         StringRef Name, DIType Ty,
556                                         Value *Val, MDNode *File,
557                                         unsigned LineNo,
558                                         unsigned ColumnNo) {
559   Value *Elts[] = {
560     GetTagConstant(VMContext, Tag),
561     DIScope(getNonCompileUnitScope(Context)).getRef(),
562     MDString::get(VMContext, Name),
563     Ty.getRef(),
564     Val,
565     File,
566     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
567     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
568   };
569   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
570 }
571
572 /// createTemplateValueParameter - Create debugging information for template
573 /// value parameter.
574 DITemplateValueParameter
575 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
576                                         DIType Ty, Value *Val,
577                                         MDNode *File, unsigned LineNo,
578                                         unsigned ColumnNo) {
579   return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
580                                       Context, Name, Ty, Val, File, LineNo,
581                                       ColumnNo);
582 }
583
584 DITemplateValueParameter
585 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
586                                            DIType Ty, StringRef Val,
587                                            MDNode *File, unsigned LineNo,
588                                            unsigned ColumnNo) {
589   return createTemplateValueParameter(
590       dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
591       MDString::get(VMContext, Val), File, LineNo, ColumnNo);
592 }
593
594 DITemplateValueParameter
595 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
596                                        DIType Ty, DIArray Val,
597                                        MDNode *File, unsigned LineNo,
598                                        unsigned ColumnNo) {
599   return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
600                                       Context, Name, Ty, Val, File, LineNo,
601                                       ColumnNo);
602 }
603
604 /// createClassType - Create debugging information entry for a class.
605 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
606                                            DIFile File, unsigned LineNumber,
607                                            uint64_t SizeInBits,
608                                            uint64_t AlignInBits,
609                                            uint64_t OffsetInBits,
610                                            unsigned Flags, DIType DerivedFrom,
611                                            DIArray Elements,
612                                            DIType VTableHolder,
613                                            MDNode *TemplateParams,
614                                            StringRef UniqueIdentifier) {
615   assert((!Context || Context.isScope() || Context.isType()) &&
616          "createClassType should be called with a valid Context");
617   // TAG_class_type is encoded in DICompositeType format.
618   Value *Elts[] = {
619     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
620     File.getFileNode(),
621     DIScope(getNonCompileUnitScope(Context)).getRef(),
622     MDString::get(VMContext, Name),
623     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
624     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
625     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
626     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
627     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
628     DerivedFrom.getRef(),
629     Elements,
630     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
631     VTableHolder.getRef(),
632     TemplateParams,
633     UniqueIdentifier.empty() ? nullptr
634                              : MDString::get(VMContext, UniqueIdentifier)
635   };
636   DICompositeType R(MDNode::get(VMContext, Elts));
637   assert(R.isCompositeType() &&
638          "createClassType should return a DICompositeType");
639   if (!UniqueIdentifier.empty())
640     retainType(R);
641   return R;
642 }
643
644 /// createStructType - Create debugging information entry for a struct.
645 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
646                                             StringRef Name, DIFile File,
647                                             unsigned LineNumber,
648                                             uint64_t SizeInBits,
649                                             uint64_t AlignInBits,
650                                             unsigned Flags, DIType DerivedFrom,
651                                             DIArray Elements,
652                                             unsigned RunTimeLang,
653                                             DIType VTableHolder,
654                                             StringRef UniqueIdentifier) {
655  // TAG_structure_type is encoded in DICompositeType format.
656   Value *Elts[] = {
657     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
658     File.getFileNode(),
659     DIScope(getNonCompileUnitScope(Context)).getRef(),
660     MDString::get(VMContext, Name),
661     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
662     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
663     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
664     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
665     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
666     DerivedFrom.getRef(),
667     Elements,
668     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
669     VTableHolder.getRef(),
670     nullptr,
671     UniqueIdentifier.empty() ? nullptr
672                              : MDString::get(VMContext, UniqueIdentifier)
673   };
674   DICompositeType R(MDNode::get(VMContext, Elts));
675   assert(R.isCompositeType() &&
676          "createStructType should return a DICompositeType");
677   if (!UniqueIdentifier.empty())
678     retainType(R);
679   return R;
680 }
681
682 /// createUnionType - Create debugging information entry for an union.
683 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
684                                            DIFile File, unsigned LineNumber,
685                                            uint64_t SizeInBits,
686                                            uint64_t AlignInBits, unsigned Flags,
687                                            DIArray Elements,
688                                            unsigned RunTimeLang,
689                                            StringRef UniqueIdentifier) {
690   // TAG_union_type is encoded in DICompositeType format.
691   Value *Elts[] = {
692     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
693     File.getFileNode(),
694     DIScope(getNonCompileUnitScope(Scope)).getRef(),
695     MDString::get(VMContext, Name),
696     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
697     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
698     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
699     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
700     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
701     nullptr,
702     Elements,
703     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
704     nullptr,
705     nullptr,
706     UniqueIdentifier.empty() ? nullptr
707                              : MDString::get(VMContext, UniqueIdentifier)
708   };
709   DICompositeType R(MDNode::get(VMContext, Elts));
710   if (!UniqueIdentifier.empty())
711     retainType(R);
712   return R;
713 }
714
715 /// createSubroutineType - Create subroutine type.
716 DICompositeType DIBuilder::createSubroutineType(DIFile File,
717                                                 DIArray ParameterTypes,
718                                                 unsigned Flags) {
719   // TAG_subroutine_type is encoded in DICompositeType format.
720   Value *Elts[] = {
721     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
722     Constant::getNullValue(Type::getInt32Ty(VMContext)),
723     nullptr,
724     MDString::get(VMContext, ""),
725     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
726     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
727     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
728     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
729     ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
730     nullptr,
731     ParameterTypes,
732     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
733     nullptr,
734     nullptr,
735     nullptr  // Type Identifer
736   };
737   return DICompositeType(MDNode::get(VMContext, Elts));
738 }
739
740 /// createEnumerationType - Create debugging information entry for an
741 /// enumeration.
742 DICompositeType DIBuilder::createEnumerationType(
743     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
744     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
745     DIType UnderlyingType, StringRef UniqueIdentifier) {
746   // TAG_enumeration_type is encoded in DICompositeType format.
747   Value *Elts[] = {
748     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
749     File.getFileNode(),
750     DIScope(getNonCompileUnitScope(Scope)).getRef(),
751     MDString::get(VMContext, Name),
752     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
753     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
754     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
755     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
756     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
757     UnderlyingType.getRef(),
758     Elements,
759     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
760     nullptr,
761     nullptr,
762     UniqueIdentifier.empty() ? nullptr
763                              : MDString::get(VMContext, UniqueIdentifier)
764   };
765   DICompositeType CTy(MDNode::get(VMContext, Elts));
766   AllEnumTypes.push_back(CTy);
767   if (!UniqueIdentifier.empty())
768     retainType(CTy);
769   return CTy;
770 }
771
772 /// createArrayType - Create debugging information entry for an array.
773 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
774                                            DIType Ty, DIArray Subscripts) {
775   // TAG_array_type is encoded in DICompositeType format.
776   Value *Elts[] = {
777     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
778     nullptr, // Filename/Directory,
779     nullptr, // Unused
780     MDString::get(VMContext, ""),
781     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
782     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
783     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
784     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
785     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
786     Ty.getRef(),
787     Subscripts,
788     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
789     nullptr,
790     nullptr,
791     nullptr  // Type Identifer
792   };
793   return DICompositeType(MDNode::get(VMContext, Elts));
794 }
795
796 /// createVectorType - Create debugging information entry for a vector.
797 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
798                                             DIType Ty, DIArray Subscripts) {
799   // A vector is an array type with the FlagVector flag applied.
800   Value *Elts[] = {
801     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
802     nullptr, // Filename/Directory,
803     nullptr, // Unused
804     MDString::get(VMContext, ""),
805     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
806     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
807     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
808     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
809     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
810     Ty.getRef(),
811     Subscripts,
812     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
813     nullptr,
814     nullptr,
815     nullptr  // Type Identifer
816   };
817   return DICompositeType(MDNode::get(VMContext, Elts));
818 }
819
820 /// createArtificialType - Create a new DIType with "artificial" flag set.
821 DIType DIBuilder::createArtificialType(DIType Ty) {
822   if (Ty.isArtificial())
823     return Ty;
824
825   SmallVector<Value *, 9> Elts;
826   MDNode *N = Ty;
827   assert (N && "Unexpected input DIType!");
828   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
829     Elts.push_back(N->getOperand(i));
830
831   unsigned CurFlags = Ty.getFlags();
832   CurFlags = CurFlags | DIType::FlagArtificial;
833
834   // Flags are stored at this slot.
835   // FIXME: Add an enum for this magic value.
836   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
837
838   return DIType(MDNode::get(VMContext, Elts));
839 }
840
841 /// createObjectPointerType - Create a new type with both the object pointer
842 /// and artificial flags set.
843 DIType DIBuilder::createObjectPointerType(DIType Ty) {
844   if (Ty.isObjectPointer())
845     return Ty;
846
847   SmallVector<Value *, 9> Elts;
848   MDNode *N = Ty;
849   assert (N && "Unexpected input DIType!");
850   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
851     Elts.push_back(N->getOperand(i));
852
853   unsigned CurFlags = Ty.getFlags();
854   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
855
856   // Flags are stored at this slot.
857   // FIXME: Add an enum for this magic value.
858   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
859
860   return DIType(MDNode::get(VMContext, Elts));
861 }
862
863 /// retainType - Retain DIType in a module even if it is not referenced
864 /// through debug info anchors.
865 void DIBuilder::retainType(DIType T) {
866   AllRetainTypes.push_back(TrackingVH<MDNode>(T));
867 }
868
869 /// createUnspecifiedParameter - Create unspeicified type descriptor
870 /// for the subroutine type.
871 DIDescriptor DIBuilder::createUnspecifiedParameter() {
872   Value *Elts[] = {
873     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
874   };
875   return DIDescriptor(MDNode::get(VMContext, Elts));
876 }
877
878 /// createForwardDecl - Create a temporary forward-declared type that
879 /// can be RAUW'd if the full type is seen.
880 DICompositeType
881 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
882                              DIFile F, unsigned Line, unsigned RuntimeLang,
883                              uint64_t SizeInBits, uint64_t AlignInBits,
884                              StringRef UniqueIdentifier) {
885   // Create a temporary MDNode.
886   Value *Elts[] = {
887     GetTagConstant(VMContext, Tag),
888     F.getFileNode(),
889     DIScope(getNonCompileUnitScope(Scope)).getRef(),
890     MDString::get(VMContext, Name),
891     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
892     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
893     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
894     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
895     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
896     nullptr,
897     DIArray(),
898     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
899     nullptr,
900     nullptr, //TemplateParams
901     UniqueIdentifier.empty() ? nullptr
902                              : MDString::get(VMContext, UniqueIdentifier)
903   };
904   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
905   DICompositeType RetTy(Node);
906   assert(RetTy.isCompositeType() &&
907          "createForwardDecl result should be a DIType");
908   if (!UniqueIdentifier.empty())
909     retainType(RetTy);
910   return RetTy;
911 }
912
913 /// getOrCreateArray - Get a DIArray, create one if required.
914 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
915   return DIArray(MDNode::get(VMContext, Elements));
916 }
917
918 /// getOrCreateSubrange - Create a descriptor for a value range.  This
919 /// implicitly uniques the values returned.
920 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
921   Value *Elts[] = {
922     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
923     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
924     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
925   };
926
927   return DISubrange(MDNode::get(VMContext, Elts));
928 }
929
930 /// \brief Create a new descriptor for the specified global.
931 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
932                                                  StringRef LinkageName,
933                                                  DIFile F, unsigned LineNumber,
934                                                  DITypeRef Ty, bool isLocalToUnit,
935                                                  Value *Val) {
936   Value *Elts[] = {
937     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
938     Constant::getNullValue(Type::getInt32Ty(VMContext)),
939     nullptr, // TheCU,
940     MDString::get(VMContext, Name),
941     MDString::get(VMContext, Name),
942     MDString::get(VMContext, LinkageName),
943     F,
944     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
945     Ty,
946     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
947     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
948     Val,
949     DIDescriptor()
950   };
951   MDNode *Node = MDNode::get(VMContext, Elts);
952   AllGVs.push_back(Node);
953   return DIGlobalVariable(Node);
954 }
955
956 /// \brief Create a new descriptor for the specified global.
957 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
958                                                  unsigned LineNumber,
959                                                  DITypeRef Ty,
960                                                  bool isLocalToUnit,
961                                                  Value *Val) {
962   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
963                               Val);
964 }
965
966 /// createStaticVariable - Create a new descriptor for the specified static
967 /// variable.
968 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
969                                                  StringRef Name,
970                                                  StringRef LinkageName,
971                                                  DIFile F, unsigned LineNumber,
972                                                  DITypeRef Ty,
973                                                  bool isLocalToUnit,
974                                                  Value *Val, MDNode *Decl) {
975   Value *Elts[] = {
976     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
977     Constant::getNullValue(Type::getInt32Ty(VMContext)),
978     getNonCompileUnitScope(Context),
979     MDString::get(VMContext, Name),
980     MDString::get(VMContext, Name),
981     MDString::get(VMContext, LinkageName),
982     F,
983     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
984     Ty,
985     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
986     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
987     Val,
988     DIDescriptor(Decl)
989   };
990   MDNode *Node = MDNode::get(VMContext, Elts);
991   AllGVs.push_back(Node);
992   return DIGlobalVariable(Node);
993 }
994
995 /// createVariable - Create a new descriptor for the specified variable.
996 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
997                                           StringRef Name, DIFile File,
998                                           unsigned LineNo, DITypeRef Ty,
999                                           bool AlwaysPreserve, unsigned Flags,
1000                                           unsigned ArgNo) {
1001   DIDescriptor Context(getNonCompileUnitScope(Scope));
1002   assert((!Context || Context.isScope()) &&
1003          "createLocalVariable should be called with a valid Context");
1004   Value *Elts[] = {
1005     GetTagConstant(VMContext, Tag),
1006     getNonCompileUnitScope(Scope),
1007     MDString::get(VMContext, Name),
1008     File,
1009     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
1010     Ty,
1011     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1012     Constant::getNullValue(Type::getInt32Ty(VMContext))
1013   };
1014   MDNode *Node = MDNode::get(VMContext, Elts);
1015   if (AlwaysPreserve) {
1016     // The optimizer may remove local variable. If there is an interest
1017     // to preserve variable info in such situation then stash it in a
1018     // named mdnode.
1019     DISubprogram Fn(getDISubprogram(Scope));
1020     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
1021     FnLocals->addOperand(Node);
1022   }
1023   DIVariable RetVar(Node);
1024   assert(RetVar.isVariable() &&
1025          "createLocalVariable should return a valid DIVariable");
1026   return RetVar;
1027 }
1028
1029 /// createComplexVariable - Create a new descriptor for the specified variable
1030 /// which has a complex address expression for its address.
1031 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
1032                                             StringRef Name, DIFile F,
1033                                             unsigned LineNo,
1034                                             DITypeRef Ty,
1035                                             ArrayRef<Value *> Addr,
1036                                             unsigned ArgNo) {
1037   SmallVector<Value *, 15> Elts;
1038   Elts.push_back(GetTagConstant(VMContext, Tag));
1039   Elts.push_back(getNonCompileUnitScope(Scope)),
1040   Elts.push_back(MDString::get(VMContext, Name));
1041   Elts.push_back(F);
1042   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1043                                   (LineNo | (ArgNo << 24))));
1044   Elts.push_back(Ty);
1045   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1046   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1047   Elts.append(Addr.begin(), Addr.end());
1048
1049   return DIVariable(MDNode::get(VMContext, Elts));
1050 }
1051
1052 /// createFunction - Create a new descriptor for the specified function.
1053 /// FIXME: this is added for dragonegg. Once we update dragonegg
1054 /// to call resolve function, this will be removed.
1055 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
1056                                        StringRef LinkageName, DIFile File,
1057                                        unsigned LineNo, DICompositeType Ty,
1058                                        bool isLocalToUnit, bool isDefinition,
1059                                        unsigned ScopeLine, unsigned Flags,
1060                                        bool isOptimized, Function *Fn,
1061                                        MDNode *TParams, MDNode *Decl) {
1062   // dragonegg does not generate identifier for types, so using an empty map
1063   // to resolve the context should be fine.
1064   DITypeIdentifierMap EmptyMap;
1065   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
1066                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1067                         Flags, isOptimized, Fn, TParams, Decl);
1068 }
1069
1070 /// createFunction - Create a new descriptor for the specified function.
1071 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1072                                        StringRef LinkageName, DIFile File,
1073                                        unsigned LineNo, DICompositeType Ty,
1074                                        bool isLocalToUnit, bool isDefinition,
1075                                        unsigned ScopeLine, unsigned Flags,
1076                                        bool isOptimized, Function *Fn,
1077                                        MDNode *TParams, MDNode *Decl) {
1078   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1079          "function types should be subroutines");
1080   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1081   Value *Elts[] = {
1082     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1083     File.getFileNode(),
1084     DIScope(getNonCompileUnitScope(Context)).getRef(),
1085     MDString::get(VMContext, Name),
1086     MDString::get(VMContext, Name),
1087     MDString::get(VMContext, LinkageName),
1088     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1089     Ty,
1090     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1091     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1092     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1093     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1094     nullptr,
1095     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1096     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1097     Fn,
1098     TParams,
1099     Decl,
1100     MDNode::getTemporary(VMContext, TElts),
1101     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1102   };
1103   MDNode *Node = MDNode::get(VMContext, Elts);
1104
1105   // Create a named metadata so that we do not lose this mdnode.
1106   if (isDefinition)
1107     AllSubprograms.push_back(Node);
1108   DISubprogram S(Node);
1109   assert(S.isSubprogram() &&
1110          "createFunction should return a valid DISubprogram");
1111   return S;
1112 }
1113
1114 /// createMethod - Create a new descriptor for the specified C++ method.
1115 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1116                                      StringRef LinkageName, DIFile F,
1117                                      unsigned LineNo, DICompositeType Ty,
1118                                      bool isLocalToUnit, bool isDefinition,
1119                                      unsigned VK, unsigned VIndex,
1120                                      DIType VTableHolder, unsigned Flags,
1121                                      bool isOptimized, Function *Fn,
1122                                      MDNode *TParam) {
1123   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1124          "function types should be subroutines");
1125   assert(getNonCompileUnitScope(Context) &&
1126          "Methods should have both a Context and a context that isn't "
1127          "the compile unit.");
1128   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1129   Value *Elts[] = {
1130     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1131     F.getFileNode(),
1132     DIScope(Context).getRef(),
1133     MDString::get(VMContext, Name),
1134     MDString::get(VMContext, Name),
1135     MDString::get(VMContext, LinkageName),
1136     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1137     Ty,
1138     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1139     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1140     ConstantInt::get(Type::getInt32Ty(VMContext), VK),
1141     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1142     VTableHolder.getRef(),
1143     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1144     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1145     Fn,
1146     TParam,
1147     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1148     MDNode::getTemporary(VMContext, TElts),
1149     // FIXME: Do we want to use different scope/lines?
1150     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1151   };
1152   MDNode *Node = MDNode::get(VMContext, Elts);
1153   if (isDefinition)
1154     AllSubprograms.push_back(Node);
1155   DISubprogram S(Node);
1156   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1157   return S;
1158 }
1159
1160 /// createNameSpace - This creates new descriptor for a namespace
1161 /// with the specified parent scope.
1162 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1163                                        DIFile File, unsigned LineNo) {
1164   Value *Elts[] = {
1165     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1166     File.getFileNode(),
1167     getNonCompileUnitScope(Scope),
1168     MDString::get(VMContext, Name),
1169     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1170   };
1171   DINameSpace R(MDNode::get(VMContext, Elts));
1172   assert(R.Verify() &&
1173          "createNameSpace should return a verifiable DINameSpace");
1174   return R;
1175 }
1176
1177 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1178 /// an existing scope with a new filename.
1179 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1180                                                      DIFile File) {
1181   Value *Elts[] = {
1182     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1183     File.getFileNode(),
1184     Scope
1185   };
1186   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1187   assert(
1188       R.Verify() &&
1189       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1190   return R;
1191 }
1192
1193 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1194                                              unsigned Line, unsigned Col,
1195                                              unsigned Discriminator) {
1196   // Defeat MDNode uniquing for lexical blocks by using unique id.
1197   static unsigned int unique_id = 0;
1198   Value *Elts[] = {
1199     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1200     File.getFileNode(),
1201     getNonCompileUnitScope(Scope),
1202     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1203     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1204     ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
1205     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1206   };
1207   DILexicalBlock R(MDNode::get(VMContext, Elts));
1208   assert(R.Verify() &&
1209          "createLexicalBlock should return a verifiable DILexicalBlock");
1210   return R;
1211 }
1212
1213 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1214 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1215                                       Instruction *InsertBefore) {
1216   assert(Storage && "no storage passed to dbg.declare");
1217   assert(VarInfo.isVariable() &&
1218          "empty or invalid DIVariable passed to dbg.declare");
1219   if (!DeclareFn)
1220     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1221
1222   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1223   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1224 }
1225
1226 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1227 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1228                                       BasicBlock *InsertAtEnd) {
1229   assert(Storage && "no storage passed to dbg.declare");
1230   assert(VarInfo.isVariable() &&
1231          "empty or invalid DIVariable passed to dbg.declare");
1232   if (!DeclareFn)
1233     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1234
1235   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1236
1237   // If this block already has a terminator then insert this intrinsic
1238   // before the terminator.
1239   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1240     return CallInst::Create(DeclareFn, Args, "", T);
1241   else
1242     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1243 }
1244
1245 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1246 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1247                                                 DIVariable VarInfo,
1248                                                 Instruction *InsertBefore) {
1249   assert(V && "no value passed to dbg.value");
1250   assert(VarInfo.isVariable() &&
1251          "empty or invalid DIVariable passed to dbg.value");
1252   if (!ValueFn)
1253     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1254
1255   Value *Args[] = { MDNode::get(V->getContext(), V),
1256                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1257                     VarInfo };
1258   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1259 }
1260
1261 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1262 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1263                                                 DIVariable VarInfo,
1264                                                 BasicBlock *InsertAtEnd) {
1265   assert(V && "no value passed to dbg.value");
1266   assert(VarInfo.isVariable() &&
1267          "empty or invalid DIVariable passed to dbg.value");
1268   if (!ValueFn)
1269     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1270
1271   Value *Args[] = { MDNode::get(V->getContext(), V),
1272                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1273                     VarInfo };
1274   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1275 }