Debug Info: add an identifier field to DICompositeType.
[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     NULL  // Type Identifer
622   };
623   DICompositeType R(MDNode::get(VMContext, Elts));
624   assert(R.isCompositeType() &&
625          "createClassType should return a DICompositeType");
626   return R;
627 }
628
629 /// createStructType - Create debugging information entry for a struct.
630 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
631                                             StringRef Name, DIFile File,
632                                             unsigned LineNumber,
633                                             uint64_t SizeInBits,
634                                             uint64_t AlignInBits,
635                                             unsigned Flags, DIType DerivedFrom,
636                                             DIArray Elements,
637                                             unsigned RunTimeLang,
638                                             MDNode *VTableHolder) {
639  // TAG_structure_type is encoded in DICompositeType format.
640   Value *Elts[] = {
641     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
642     File.getFileNode(),
643     getNonCompileUnitScope(Context),
644     MDString::get(VMContext, Name),
645     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
646     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
647     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
648     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
649     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
650     DerivedFrom,
651     Elements,
652     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
653     VTableHolder,
654     NULL,
655     NULL  // Type Identifer
656   };
657   DICompositeType R(MDNode::get(VMContext, Elts));
658   assert(R.isCompositeType() &&
659          "createStructType should return a DICompositeType");
660   return R;
661 }
662
663 /// createUnionType - Create debugging information entry for an union.
664 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
665                                            DIFile File, unsigned LineNumber,
666                                            uint64_t SizeInBits,
667                                            uint64_t AlignInBits, unsigned Flags,
668                                            DIArray Elements,
669                                            unsigned RunTimeLang) {
670   // TAG_union_type is encoded in DICompositeType format.
671   Value *Elts[] = {
672     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
673     File.getFileNode(),
674     getNonCompileUnitScope(Scope),
675     MDString::get(VMContext, Name),
676     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
677     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
678     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
679     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
680     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
681     NULL,
682     Elements,
683     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
684     NULL,
685     NULL,
686     NULL  // Type Identifer
687   };
688   return DICompositeType(MDNode::get(VMContext, Elts));
689 }
690
691 /// createSubroutineType - Create subroutine type.
692 DICompositeType
693 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
694   // TAG_subroutine_type is encoded in DICompositeType format.
695   Value *Elts[] = {
696     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
697     Constant::getNullValue(Type::getInt32Ty(VMContext)),
698     Constant::getNullValue(Type::getInt32Ty(VMContext)),
699     MDString::get(VMContext, ""),
700     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
701     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
702     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
703     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
704     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
705     NULL,
706     ParameterTypes,
707     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
708     NULL,
709     NULL,
710     NULL  // Type Identifer
711   };
712   return DICompositeType(MDNode::get(VMContext, Elts));
713 }
714
715 /// createEnumerationType - Create debugging information entry for an
716 /// enumeration.
717 DICompositeType DIBuilder::createEnumerationType(
718     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
719     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
720     DIType UnderlyingType) {
721   // TAG_enumeration_type is encoded in DICompositeType format.
722   Value *Elts[] = {
723     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
724     File.getFileNode(),
725     getNonCompileUnitScope(Scope),
726     MDString::get(VMContext, Name),
727     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
728     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
729     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
730     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
731     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
732     UnderlyingType,
733     Elements,
734     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
735     NULL,
736     NULL,
737     NULL  // Type Identifer
738   };
739   MDNode *Node = MDNode::get(VMContext, Elts);
740   AllEnumTypes.push_back(Node);
741   return DICompositeType(Node);
742 }
743
744 /// createArrayType - Create debugging information entry for an array.
745 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
746                                            DIType Ty, DIArray Subscripts) {
747   // TAG_array_type is encoded in DICompositeType format.
748   Value *Elts[] = {
749     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
750     NULL, // Filename/Directory,
751     NULL, // Unused
752     MDString::get(VMContext, ""),
753     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
754     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
755     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
756     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
757     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
758     Ty,
759     Subscripts,
760     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
761     NULL,
762     NULL,
763     NULL  // Type Identifer
764   };
765   return DICompositeType(MDNode::get(VMContext, Elts));
766 }
767
768 /// createVectorType - Create debugging information entry for a vector.
769 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
770                                             DIType Ty, DIArray Subscripts) {
771   // A vector is an array type with the FlagVector flag applied.
772   Value *Elts[] = {
773     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
774     NULL, // Filename/Directory,
775     NULL, // Unused
776     MDString::get(VMContext, ""),
777     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
778     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
779     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
780     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
781     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
782     Ty,
783     Subscripts,
784     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
785     NULL,
786     NULL,
787     NULL  // Type Identifer
788   };
789   return DICompositeType(MDNode::get(VMContext, Elts));
790 }
791
792 /// createArtificialType - Create a new DIType with "artificial" flag set.
793 DIType DIBuilder::createArtificialType(DIType Ty) {
794   if (Ty.isArtificial())
795     return Ty;
796
797   SmallVector<Value *, 9> Elts;
798   MDNode *N = Ty;
799   assert (N && "Unexpected input DIType!");
800   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
801     if (Value *V = N->getOperand(i))
802       Elts.push_back(V);
803     else
804       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
805   }
806
807   unsigned CurFlags = Ty.getFlags();
808   CurFlags = CurFlags | DIType::FlagArtificial;
809
810   // Flags are stored at this slot.
811   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
812
813   return DIType(MDNode::get(VMContext, Elts));
814 }
815
816 /// createObjectPointerType - Create a new type with both the object pointer
817 /// and artificial flags set.
818 DIType DIBuilder::createObjectPointerType(DIType Ty) {
819   if (Ty.isObjectPointer())
820     return Ty;
821
822   SmallVector<Value *, 9> Elts;
823   MDNode *N = Ty;
824   assert (N && "Unexpected input DIType!");
825   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
826     if (Value *V = N->getOperand(i))
827       Elts.push_back(V);
828     else
829       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
830   }
831
832   unsigned CurFlags = Ty.getFlags();
833   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
834
835   // Flags are stored at this slot.
836   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
837
838   return DIType(MDNode::get(VMContext, Elts));
839 }
840
841 /// retainType - Retain DIType in a module even if it is not referenced
842 /// through debug info anchors.
843 void DIBuilder::retainType(DIType T) {
844   AllRetainTypes.push_back(T);
845 }
846
847 /// createUnspecifiedParameter - Create unspeicified type descriptor
848 /// for the subroutine type.
849 DIDescriptor DIBuilder::createUnspecifiedParameter() {
850   Value *Elts[] = {
851     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
852   };
853   return DIDescriptor(MDNode::get(VMContext, Elts));
854 }
855
856 /// createForwardDecl - Create a temporary forward-declared type that
857 /// can be RAUW'd if the full type is seen.
858 DICompositeType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
859                                     DIDescriptor Scope, DIFile F,
860                                     unsigned Line, unsigned RuntimeLang,
861                                     uint64_t SizeInBits,
862                                     uint64_t AlignInBits) {
863   // Create a temporary MDNode.
864   Value *Elts[] = {
865     GetTagConstant(VMContext, Tag),
866     F.getFileNode(),
867     getNonCompileUnitScope(Scope),
868     MDString::get(VMContext, Name),
869     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
870     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
871     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
872     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
873     ConstantInt::get(Type::getInt32Ty(VMContext),
874                      DIDescriptor::FlagFwdDecl),
875     NULL,
876     DIArray(),
877     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
878     NULL,
879     NULL, //TemplateParams
880     NULL  // Type Identifer
881   };
882   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
883   DICompositeType RetTy(Node);
884   assert(RetTy.isCompositeType() &&
885          "createForwardDecl result should be a DIType");
886   return RetTy;
887 }
888
889 /// getOrCreateArray - Get a DIArray, create one if required.
890 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
891   if (Elements.empty()) {
892     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
893     return DIArray(MDNode::get(VMContext, Null));
894   }
895   return DIArray(MDNode::get(VMContext, Elements));
896 }
897
898 /// getOrCreateSubrange - Create a descriptor for a value range.  This
899 /// implicitly uniques the values returned.
900 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
901   Value *Elts[] = {
902     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
903     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
904     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
905   };
906
907   return DISubrange(MDNode::get(VMContext, Elts));
908 }
909
910 /// \brief Create a new descriptor for the specified global.
911 DIGlobalVariable DIBuilder::
912 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
913                      unsigned LineNumber, DIType Ty, bool isLocalToUnit,
914                      Value *Val) {
915   Value *Elts[] = {
916     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
917     Constant::getNullValue(Type::getInt32Ty(VMContext)),
918     NULL, // TheCU,
919     MDString::get(VMContext, Name),
920     MDString::get(VMContext, Name),
921     MDString::get(VMContext, LinkageName),
922     F,
923     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
924     Ty,
925     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
926     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
927     Val,
928     DIDescriptor()
929   };
930   MDNode *Node = MDNode::get(VMContext, Elts);
931   AllGVs.push_back(Node);
932   return DIGlobalVariable(Node);
933 }
934
935 /// \brief Create a new descriptor for the specified global.
936 DIGlobalVariable DIBuilder::
937 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
938                      DIType Ty, bool isLocalToUnit, Value *Val) {
939   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
940                               Val);
941 }
942
943 /// createStaticVariable - Create a new descriptor for the specified static
944 /// variable.
945 DIGlobalVariable DIBuilder::
946 createStaticVariable(DIDescriptor Context, StringRef Name,
947                      StringRef LinkageName, DIFile F, unsigned LineNumber,
948                      DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
949   Value *Elts[] = {
950     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
951     Constant::getNullValue(Type::getInt32Ty(VMContext)),
952     getNonCompileUnitScope(Context),
953     MDString::get(VMContext, Name),
954     MDString::get(VMContext, Name),
955     MDString::get(VMContext, LinkageName),
956     F,
957     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
958     Ty,
959     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
960     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
961     Val,
962     DIDescriptor(Decl)
963   };
964   MDNode *Node = MDNode::get(VMContext, Elts);
965   AllGVs.push_back(Node);
966   return DIGlobalVariable(Node);
967 }
968
969 /// createVariable - Create a new descriptor for the specified variable.
970 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
971                                           StringRef Name, DIFile File,
972                                           unsigned LineNo, DIType Ty,
973                                           bool AlwaysPreserve, unsigned Flags,
974                                           unsigned ArgNo) {
975   DIDescriptor Context(getNonCompileUnitScope(Scope));
976   assert((!Context || Context.isScope()) &&
977          "createLocalVariable should be called with a valid Context");
978   assert(Ty.isType() &&
979          "createLocalVariable should be called with a valid type");
980   Value *Elts[] = {
981     GetTagConstant(VMContext, Tag),
982     getNonCompileUnitScope(Scope),
983     MDString::get(VMContext, Name),
984     File,
985     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
986     Ty,
987     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
988     Constant::getNullValue(Type::getInt32Ty(VMContext))
989   };
990   MDNode *Node = MDNode::get(VMContext, Elts);
991   if (AlwaysPreserve) {
992     // The optimizer may remove local variable. If there is an interest
993     // to preserve variable info in such situation then stash it in a
994     // named mdnode.
995     DISubprogram Fn(getDISubprogram(Scope));
996     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
997     FnLocals->addOperand(Node);
998   }
999   DIVariable RetVar(Node);
1000   assert(RetVar.isVariable() &&
1001          "createLocalVariable should return a valid DIVariable");
1002   return RetVar;
1003 }
1004
1005 /// createComplexVariable - Create a new descriptor for the specified variable
1006 /// which has a complex address expression for its address.
1007 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
1008                                             StringRef Name, DIFile F,
1009                                             unsigned LineNo,
1010                                             DIType Ty, ArrayRef<Value *> Addr,
1011                                             unsigned ArgNo) {
1012   SmallVector<Value *, 15> Elts;
1013   Elts.push_back(GetTagConstant(VMContext, Tag));
1014   Elts.push_back(getNonCompileUnitScope(Scope)),
1015   Elts.push_back(MDString::get(VMContext, Name));
1016   Elts.push_back(F);
1017   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1018                                   (LineNo | (ArgNo << 24))));
1019   Elts.push_back(Ty);
1020   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1021   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1022   Elts.append(Addr.begin(), Addr.end());
1023
1024   return DIVariable(MDNode::get(VMContext, Elts));
1025 }
1026
1027 /// createFunction - Create a new descriptor for the specified function.
1028 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
1029                                        StringRef Name,
1030                                        StringRef LinkageName,
1031                                        DIFile File, unsigned LineNo,
1032                                        DICompositeType Ty,
1033                                        bool isLocalToUnit, bool isDefinition,
1034                                        unsigned ScopeLine,
1035                                        unsigned Flags, bool isOptimized,
1036                                        Function *Fn,
1037                                        MDNode *TParams,
1038                                        MDNode *Decl) {
1039   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1040          "function types should be subroutines");
1041   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1042   Value *Elts[] = {
1043     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1044     File.getFileNode(),
1045     getNonCompileUnitScope(Context),
1046     MDString::get(VMContext, Name),
1047     MDString::get(VMContext, Name),
1048     MDString::get(VMContext, LinkageName),
1049     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1050     Ty,
1051     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1052     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1053     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1054     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1055     NULL,
1056     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1057     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1058     Fn,
1059     TParams,
1060     Decl,
1061     MDNode::getTemporary(VMContext, TElts),
1062     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1063   };
1064   MDNode *Node = MDNode::get(VMContext, Elts);
1065
1066   // Create a named metadata so that we do not lose this mdnode.
1067   if (isDefinition)
1068     AllSubprograms.push_back(Node);
1069   DISubprogram S(Node);
1070   assert(S.isSubprogram() && "createFunction should return a valid DISubprogram");
1071   return S;
1072 }
1073
1074 /// createMethod - Create a new descriptor for the specified C++ method.
1075 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
1076                                      StringRef Name,
1077                                      StringRef LinkageName,
1078                                      DIFile F,
1079                                      unsigned LineNo, DICompositeType Ty,
1080                                      bool isLocalToUnit,
1081                                      bool isDefinition,
1082                                      unsigned VK, unsigned VIndex,
1083                                      MDNode *VTableHolder,
1084                                      unsigned Flags,
1085                                      bool isOptimized,
1086                                      Function *Fn,
1087                                      MDNode *TParam) {
1088   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1089          "function types should be subroutines");
1090   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1091   Value *Elts[] = {
1092     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1093     F.getFileNode(),
1094     getNonCompileUnitScope(Context),
1095     MDString::get(VMContext, Name),
1096     MDString::get(VMContext, Name),
1097     MDString::get(VMContext, LinkageName),
1098     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1099     Ty,
1100     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1101     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1102     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1103     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1104     VTableHolder,
1105     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1106     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1107     Fn,
1108     TParam,
1109     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1110     MDNode::getTemporary(VMContext, TElts),
1111     // FIXME: Do we want to use different scope/lines?
1112     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1113   };
1114   MDNode *Node = MDNode::get(VMContext, Elts);
1115   if (isDefinition)
1116     AllSubprograms.push_back(Node);
1117   DISubprogram S(Node);
1118   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1119   return S;
1120 }
1121
1122 /// createNameSpace - This creates new descriptor for a namespace
1123 /// with the specified parent scope.
1124 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1125                                        DIFile File, unsigned LineNo) {
1126   Value *Elts[] = {
1127     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1128     File.getFileNode(),
1129     getNonCompileUnitScope(Scope),
1130     MDString::get(VMContext, Name),
1131     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1132   };
1133   DINameSpace R(MDNode::get(VMContext, Elts));
1134   assert(R.Verify() &&
1135          "createNameSpace should return a verifiable DINameSpace");
1136   return R;
1137 }
1138
1139 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1140 /// an existing scope with a new filename.
1141 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1142                                                      DIFile File) {
1143   Value *Elts[] = {
1144     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1145     File.getFileNode(),
1146     Scope
1147   };
1148   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1149   assert(
1150       R.Verify() &&
1151       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1152   return R;
1153 }
1154
1155 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1156                                              unsigned Line, unsigned Col) {
1157   // Defeat MDNode uniquing for lexical blocks by using unique id.
1158   static unsigned int unique_id = 0;
1159   Value *Elts[] = {
1160     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1161     File.getFileNode(),
1162     getNonCompileUnitScope(Scope),
1163     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1164     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1165     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1166   };
1167   DILexicalBlock R(MDNode::get(VMContext, Elts));
1168   assert(R.Verify() &&
1169          "createLexicalBlock should return a verifiable DILexicalBlock");
1170   return R;
1171 }
1172
1173 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1174 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1175                                       Instruction *InsertBefore) {
1176   assert(Storage && "no storage passed to dbg.declare");
1177   assert(VarInfo.isVariable() &&
1178          "empty or invalid DIVariable passed to dbg.declare");
1179   if (!DeclareFn)
1180     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1181
1182   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1183   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1184 }
1185
1186 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1187 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1188                                       BasicBlock *InsertAtEnd) {
1189   assert(Storage && "no storage passed to dbg.declare");
1190   assert(VarInfo.isVariable() &&
1191          "empty or invalid DIVariable passed to dbg.declare");
1192   if (!DeclareFn)
1193     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1194
1195   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1196
1197   // If this block already has a terminator then insert this intrinsic
1198   // before the terminator.
1199   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1200     return CallInst::Create(DeclareFn, Args, "", T);
1201   else
1202     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1203 }
1204
1205 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1206 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1207                                                 DIVariable VarInfo,
1208                                                 Instruction *InsertBefore) {
1209   assert(V && "no value passed to dbg.value");
1210   assert(VarInfo.isVariable() &&
1211          "empty or invalid DIVariable passed to dbg.value");
1212   if (!ValueFn)
1213     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1214
1215   Value *Args[] = { MDNode::get(V->getContext(), V),
1216                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1217                     VarInfo };
1218   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1219 }
1220
1221 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1222 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1223                                                 DIVariable VarInfo,
1224                                                 BasicBlock *InsertAtEnd) {
1225   assert(V && "no value passed to dbg.value");
1226   assert(VarInfo.isVariable() &&
1227          "empty or invalid DIVariable passed to dbg.value");
1228   if (!ValueFn)
1229     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1230
1231   Value *Args[] = { MDNode::get(V->getContext(), V),
1232                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1233                     VarInfo };
1234   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1235 }