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