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