DebugInfo: Add FIXME regarding DILexicalBlock uniquing fields.
[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::get(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 /// createForwardDecl - Create a temporary forward-declared type that
914 /// can be RAUW'd if the full type is seen.
915 DICompositeType DIBuilder::createReplaceableForwardDecl(
916     unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
917     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
918     StringRef UniqueIdentifier) {
919   // Create a temporary MDNode.
920   Value *Elts[] = {
921     GetTagConstant(VMContext, Tag),
922     F.getFileNode(),
923     DIScope(getNonCompileUnitScope(Scope)).getRef(),
924     MDString::get(VMContext, Name),
925     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
926     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
927     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
928     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
929     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
930     nullptr,
931     DIArray(),
932     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
933     nullptr,
934     nullptr, //TemplateParams
935     UniqueIdentifier.empty() ? nullptr
936                              : MDString::get(VMContext, UniqueIdentifier)
937   };
938   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
939   DICompositeType RetTy(Node);
940   assert(RetTy.isCompositeType() &&
941          "createForwardDecl result should be a DIType");
942   if (!UniqueIdentifier.empty())
943     retainType(RetTy);
944   return RetTy;
945 }
946
947 /// getOrCreateArray - Get a DIArray, create one if required.
948 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
949   return DIArray(MDNode::get(VMContext, Elements));
950 }
951
952 /// getOrCreateSubrange - Create a descriptor for a value range.  This
953 /// implicitly uniques the values returned.
954 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
955   Value *Elts[] = {
956     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
957     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
958     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
959   };
960
961   return DISubrange(MDNode::get(VMContext, Elts));
962 }
963
964 /// \brief Create a new descriptor for the specified global.
965 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
966                                                  StringRef LinkageName,
967                                                  DIFile F, unsigned LineNumber,
968                                                  DITypeRef Ty, bool isLocalToUnit,
969                                                  Value *Val) {
970   Value *Elts[] = {
971     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
972     Constant::getNullValue(Type::getInt32Ty(VMContext)),
973     nullptr, // TheCU,
974     MDString::get(VMContext, Name),
975     MDString::get(VMContext, Name),
976     MDString::get(VMContext, LinkageName),
977     F,
978     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
979     Ty,
980     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
981     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
982     Val,
983     DIDescriptor()
984   };
985   MDNode *Node = MDNode::get(VMContext, Elts);
986   AllGVs.push_back(Node);
987   return DIGlobalVariable(Node);
988 }
989
990 /// \brief Create a new descriptor for the specified global.
991 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
992                                                  unsigned LineNumber,
993                                                  DITypeRef Ty,
994                                                  bool isLocalToUnit,
995                                                  Value *Val) {
996   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
997                               Val);
998 }
999
1000 /// createStaticVariable - Create a new descriptor for the specified static
1001 /// variable.
1002 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
1003                                                  StringRef Name,
1004                                                  StringRef LinkageName,
1005                                                  DIFile F, unsigned LineNumber,
1006                                                  DITypeRef Ty,
1007                                                  bool isLocalToUnit,
1008                                                  Value *Val, MDNode *Decl) {
1009   Value *Elts[] = {
1010     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
1011     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1012     getNonCompileUnitScope(Context),
1013     MDString::get(VMContext, Name),
1014     MDString::get(VMContext, Name),
1015     MDString::get(VMContext, LinkageName),
1016     F,
1017     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1018     Ty,
1019     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
1020     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
1021     Val,
1022     DIDescriptor(Decl)
1023   };
1024   MDNode *Node = MDNode::get(VMContext, Elts);
1025   AllGVs.push_back(Node);
1026   return DIGlobalVariable(Node);
1027 }
1028
1029 /// createVariable - Create a new descriptor for the specified variable.
1030 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
1031                                           StringRef Name, DIFile File,
1032                                           unsigned LineNo, DITypeRef Ty,
1033                                           bool AlwaysPreserve, unsigned Flags,
1034                                           unsigned ArgNo) {
1035   DIDescriptor Context(getNonCompileUnitScope(Scope));
1036   assert((!Context || Context.isScope()) &&
1037          "createLocalVariable should be called with a valid Context");
1038   Value *Elts[] = {
1039     GetTagConstant(VMContext, Tag),
1040     getNonCompileUnitScope(Scope),
1041     MDString::get(VMContext, Name),
1042     File,
1043     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
1044     Ty,
1045     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1046     Constant::getNullValue(Type::getInt32Ty(VMContext))
1047   };
1048   MDNode *Node = MDNode::get(VMContext, Elts);
1049   if (AlwaysPreserve) {
1050     // The optimizer may remove local variable. If there is an interest
1051     // to preserve variable info in such situation then stash it in a
1052     // named mdnode.
1053     DISubprogram Fn(getDISubprogram(Scope));
1054     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
1055     FnLocals->addOperand(Node);
1056   }
1057   DIVariable RetVar(Node);
1058   assert(RetVar.isVariable() &&
1059          "createLocalVariable should return a valid DIVariable");
1060   return RetVar;
1061 }
1062
1063 /// createComplexVariable - Create a new descriptor for the specified variable
1064 /// which has a complex address expression for its address.
1065 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
1066                                             StringRef Name, DIFile F,
1067                                             unsigned LineNo,
1068                                             DITypeRef Ty,
1069                                             ArrayRef<Value *> Addr,
1070                                             unsigned ArgNo) {
1071   SmallVector<Value *, 15> Elts;
1072   Elts.push_back(GetTagConstant(VMContext, Tag));
1073   Elts.push_back(getNonCompileUnitScope(Scope)),
1074   Elts.push_back(MDString::get(VMContext, Name));
1075   Elts.push_back(F);
1076   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1077                                   (LineNo | (ArgNo << 24))));
1078   Elts.push_back(Ty);
1079   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1080   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1081   Elts.append(Addr.begin(), Addr.end());
1082
1083   return DIVariable(MDNode::get(VMContext, Elts));
1084 }
1085
1086 /// createFunction - Create a new descriptor for the specified function.
1087 /// FIXME: this is added for dragonegg. Once we update dragonegg
1088 /// to call resolve function, this will be removed.
1089 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
1090                                        StringRef LinkageName, DIFile File,
1091                                        unsigned LineNo, DICompositeType Ty,
1092                                        bool isLocalToUnit, bool isDefinition,
1093                                        unsigned ScopeLine, unsigned Flags,
1094                                        bool isOptimized, Function *Fn,
1095                                        MDNode *TParams, MDNode *Decl) {
1096   // dragonegg does not generate identifier for types, so using an empty map
1097   // to resolve the context should be fine.
1098   DITypeIdentifierMap EmptyMap;
1099   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
1100                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1101                         Flags, isOptimized, Fn, TParams, Decl);
1102 }
1103
1104 /// createFunction - Create a new descriptor for the specified function.
1105 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1106                                        StringRef LinkageName, DIFile File,
1107                                        unsigned LineNo, DICompositeType Ty,
1108                                        bool isLocalToUnit, bool isDefinition,
1109                                        unsigned ScopeLine, unsigned Flags,
1110                                        bool isOptimized, Function *Fn,
1111                                        MDNode *TParams, MDNode *Decl) {
1112   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1113          "function types should be subroutines");
1114   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1115   Value *Elts[] = {
1116     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1117     File.getFileNode(),
1118     DIScope(getNonCompileUnitScope(Context)).getRef(),
1119     MDString::get(VMContext, Name),
1120     MDString::get(VMContext, Name),
1121     MDString::get(VMContext, LinkageName),
1122     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1123     Ty,
1124     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1125     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1126     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1127     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1128     nullptr,
1129     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1130     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1131     Fn,
1132     TParams,
1133     Decl,
1134     MDNode::getTemporary(VMContext, TElts),
1135     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1136   };
1137   MDNode *Node = MDNode::get(VMContext, Elts);
1138
1139   // Create a named metadata so that we do not lose this mdnode.
1140   if (isDefinition)
1141     AllSubprograms.push_back(Node);
1142   DISubprogram S(Node);
1143   assert(S.isSubprogram() &&
1144          "createFunction should return a valid DISubprogram");
1145   return S;
1146 }
1147
1148 /// createMethod - Create a new descriptor for the specified C++ method.
1149 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1150                                      StringRef LinkageName, DIFile F,
1151                                      unsigned LineNo, DICompositeType Ty,
1152                                      bool isLocalToUnit, bool isDefinition,
1153                                      unsigned VK, unsigned VIndex,
1154                                      DIType VTableHolder, unsigned Flags,
1155                                      bool isOptimized, Function *Fn,
1156                                      MDNode *TParam) {
1157   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1158          "function types should be subroutines");
1159   assert(getNonCompileUnitScope(Context) &&
1160          "Methods should have both a Context and a context that isn't "
1161          "the compile unit.");
1162   Value *Elts[] = {
1163     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1164     F.getFileNode(),
1165     DIScope(Context).getRef(),
1166     MDString::get(VMContext, Name),
1167     MDString::get(VMContext, Name),
1168     MDString::get(VMContext, LinkageName),
1169     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1170     Ty,
1171     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1172     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1173     ConstantInt::get(Type::getInt32Ty(VMContext), VK),
1174     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1175     VTableHolder.getRef(),
1176     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1177     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1178     Fn,
1179     TParam,
1180     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1181     nullptr,
1182     // FIXME: Do we want to use different scope/lines?
1183     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1184   };
1185   MDNode *Node = MDNode::get(VMContext, Elts);
1186   if (isDefinition)
1187     AllSubprograms.push_back(Node);
1188   DISubprogram S(Node);
1189   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1190   return S;
1191 }
1192
1193 /// createNameSpace - This creates new descriptor for a namespace
1194 /// with the specified parent scope.
1195 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1196                                        DIFile File, unsigned LineNo) {
1197   Value *Elts[] = {
1198     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1199     File.getFileNode(),
1200     getNonCompileUnitScope(Scope),
1201     MDString::get(VMContext, Name),
1202     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1203   };
1204   DINameSpace R(MDNode::get(VMContext, Elts));
1205   assert(R.Verify() &&
1206          "createNameSpace should return a verifiable DINameSpace");
1207   return R;
1208 }
1209
1210 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1211 /// an existing scope with a new filename.
1212 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1213                                                      DIFile File) {
1214   Value *Elts[] = {
1215     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1216     File.getFileNode(),
1217     Scope
1218   };
1219   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1220   assert(
1221       R.Verify() &&
1222       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1223   return R;
1224 }
1225
1226 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1227                                              unsigned Line, unsigned Col,
1228                                              unsigned Discriminator) {
1229   // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1230   // I believe the right way is to have a self-referential element in the node.
1231   // Also: why do we bother with line/column - they're not used and the
1232   // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1233   // for uniquing, yet then we have this other solution (because line/col were
1234   // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1235
1236   // Defeat MDNode uniquing for lexical blocks by using unique id.
1237   static unsigned int unique_id = 0;
1238   Value *Elts[] = {
1239     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1240     File.getFileNode(),
1241     getNonCompileUnitScope(Scope),
1242     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1243     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1244     ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
1245     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1246   };
1247   DILexicalBlock R(MDNode::get(VMContext, Elts));
1248   assert(R.Verify() &&
1249          "createLexicalBlock should return a verifiable DILexicalBlock");
1250   return R;
1251 }
1252
1253 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1254 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1255                                       Instruction *InsertBefore) {
1256   assert(Storage && "no storage passed to dbg.declare");
1257   assert(VarInfo.isVariable() &&
1258          "empty or invalid DIVariable passed to dbg.declare");
1259   if (!DeclareFn)
1260     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1261
1262   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1263   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1264 }
1265
1266 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1267 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1268                                       BasicBlock *InsertAtEnd) {
1269   assert(Storage && "no storage passed to dbg.declare");
1270   assert(VarInfo.isVariable() &&
1271          "empty or invalid DIVariable passed to dbg.declare");
1272   if (!DeclareFn)
1273     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1274
1275   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1276
1277   // If this block already has a terminator then insert this intrinsic
1278   // before the terminator.
1279   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1280     return CallInst::Create(DeclareFn, Args, "", T);
1281   else
1282     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1283 }
1284
1285 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1286 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1287                                                 DIVariable VarInfo,
1288                                                 Instruction *InsertBefore) {
1289   assert(V && "no value passed to dbg.value");
1290   assert(VarInfo.isVariable() &&
1291          "empty or invalid DIVariable passed to dbg.value");
1292   if (!ValueFn)
1293     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1294
1295   Value *Args[] = { MDNode::get(V->getContext(), V),
1296                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1297                     VarInfo };
1298   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1299 }
1300
1301 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1302 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1303                                                 DIVariable VarInfo,
1304                                                 BasicBlock *InsertAtEnd) {
1305   assert(V && "no value passed to dbg.value");
1306   assert(VarInfo.isVariable() &&
1307          "empty or invalid DIVariable passed to dbg.value");
1308   if (!ValueFn)
1309     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1310
1311   Value *Args[] = { MDNode::get(V->getContext(), V),
1312                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1313                     VarInfo };
1314   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1315 }