DebugInfo: Allow the addition of other (such as static data) members to a record...
[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 DICompositeType 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     NULL
868   };
869   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
870   DICompositeType RetTy(Node);
871   assert(RetTy.isCompositeType() &&
872          "createForwardDecl result should be a DIType");
873   return RetTy;
874 }
875
876 /// getOrCreateArray - Get a DIArray, create one if required.
877 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
878   if (Elements.empty()) {
879     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
880     return DIArray(MDNode::get(VMContext, Null));
881   }
882   return DIArray(MDNode::get(VMContext, Elements));
883 }
884
885 /// getOrCreateSubrange - Create a descriptor for a value range.  This
886 /// implicitly uniques the values returned.
887 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
888   Value *Elts[] = {
889     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
890     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
891     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
892   };
893
894   return DISubrange(MDNode::get(VMContext, Elts));
895 }
896
897 /// \brief Create a new descriptor for the specified global.
898 DIGlobalVariable DIBuilder::
899 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
900                      unsigned LineNumber, DIType Ty, bool isLocalToUnit,
901                      Value *Val) {
902   Value *Elts[] = {
903     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
904     Constant::getNullValue(Type::getInt32Ty(VMContext)),
905     NULL, // TheCU,
906     MDString::get(VMContext, Name),
907     MDString::get(VMContext, Name),
908     MDString::get(VMContext, LinkageName),
909     F,
910     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
911     Ty,
912     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
913     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
914     Val,
915     DIDescriptor()
916   };
917   MDNode *Node = MDNode::get(VMContext, Elts);
918   AllGVs.push_back(Node);
919   return DIGlobalVariable(Node);
920 }
921
922 /// \brief Create a new descriptor for the specified global.
923 DIGlobalVariable DIBuilder::
924 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
925                      DIType Ty, bool isLocalToUnit, Value *Val) {
926   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
927                               Val);
928 }
929
930 /// createStaticVariable - Create a new descriptor for the specified static
931 /// variable.
932 DIGlobalVariable DIBuilder::
933 createStaticVariable(DIDescriptor Context, StringRef Name,
934                      StringRef LinkageName, DIFile F, unsigned LineNumber,
935                      DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
936   Value *Elts[] = {
937     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
938     Constant::getNullValue(Type::getInt32Ty(VMContext)),
939     getNonCompileUnitScope(Context),
940     MDString::get(VMContext, Name),
941     MDString::get(VMContext, Name),
942     MDString::get(VMContext, LinkageName),
943     F,
944     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
945     Ty,
946     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
947     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
948     Val,
949     DIDescriptor(Decl)
950   };
951   MDNode *Node = MDNode::get(VMContext, Elts);
952   AllGVs.push_back(Node);
953   return DIGlobalVariable(Node);
954 }
955
956 /// createVariable - Create a new descriptor for the specified variable.
957 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
958                                           StringRef Name, DIFile File,
959                                           unsigned LineNo, DIType Ty,
960                                           bool AlwaysPreserve, unsigned Flags,
961                                           unsigned ArgNo) {
962   DIDescriptor Context(getNonCompileUnitScope(Scope));
963   assert((!Context || Context.isScope()) &&
964          "createLocalVariable should be called with a valid Context");
965   assert(Ty.isType() &&
966          "createLocalVariable should be called with a valid type");
967   Value *Elts[] = {
968     GetTagConstant(VMContext, Tag),
969     getNonCompileUnitScope(Scope),
970     MDString::get(VMContext, Name),
971     File,
972     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
973     Ty,
974     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
975     Constant::getNullValue(Type::getInt32Ty(VMContext))
976   };
977   MDNode *Node = MDNode::get(VMContext, Elts);
978   if (AlwaysPreserve) {
979     // The optimizer may remove local variable. If there is an interest
980     // to preserve variable info in such situation then stash it in a
981     // named mdnode.
982     DISubprogram Fn(getDISubprogram(Scope));
983     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
984     FnLocals->addOperand(Node);
985   }
986   DIVariable RetVar(Node);
987   assert(RetVar.isVariable() &&
988          "createLocalVariable should return a valid DIVariable");
989   return RetVar;
990 }
991
992 /// createComplexVariable - Create a new descriptor for the specified variable
993 /// which has a complex address expression for its address.
994 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
995                                             StringRef Name, DIFile F,
996                                             unsigned LineNo,
997                                             DIType Ty, ArrayRef<Value *> Addr,
998                                             unsigned ArgNo) {
999   SmallVector<Value *, 15> Elts;
1000   Elts.push_back(GetTagConstant(VMContext, Tag));
1001   Elts.push_back(getNonCompileUnitScope(Scope)),
1002   Elts.push_back(MDString::get(VMContext, Name));
1003   Elts.push_back(F);
1004   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1005                                   (LineNo | (ArgNo << 24))));
1006   Elts.push_back(Ty);
1007   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1008   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1009   Elts.append(Addr.begin(), Addr.end());
1010
1011   return DIVariable(MDNode::get(VMContext, Elts));
1012 }
1013
1014 /// createFunction - Create a new descriptor for the specified function.
1015 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
1016                                        StringRef Name,
1017                                        StringRef LinkageName,
1018                                        DIFile File, unsigned LineNo,
1019                                        DICompositeType Ty,
1020                                        bool isLocalToUnit, bool isDefinition,
1021                                        unsigned ScopeLine,
1022                                        unsigned Flags, bool isOptimized,
1023                                        Function *Fn,
1024                                        MDNode *TParams,
1025                                        MDNode *Decl) {
1026   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1027          "function types should be subroutines");
1028   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1029   Value *Elts[] = {
1030     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1031     File.getFileNode(),
1032     getNonCompileUnitScope(Context),
1033     MDString::get(VMContext, Name),
1034     MDString::get(VMContext, Name),
1035     MDString::get(VMContext, LinkageName),
1036     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1037     Ty,
1038     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1039     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1040     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1041     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1042     NULL,
1043     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1044     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1045     Fn,
1046     TParams,
1047     Decl,
1048     MDNode::getTemporary(VMContext, TElts),
1049     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1050   };
1051   MDNode *Node = MDNode::get(VMContext, Elts);
1052
1053   // Create a named metadata so that we do not lose this mdnode.
1054   if (isDefinition)
1055     AllSubprograms.push_back(Node);
1056   DISubprogram S(Node);
1057   assert(S.isSubprogram() && "createFunction should return a valid DISubprogram");
1058   return S;
1059 }
1060
1061 /// createMethod - Create a new descriptor for the specified C++ method.
1062 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
1063                                      StringRef Name,
1064                                      StringRef LinkageName,
1065                                      DIFile F,
1066                                      unsigned LineNo, DICompositeType Ty,
1067                                      bool isLocalToUnit,
1068                                      bool isDefinition,
1069                                      unsigned VK, unsigned VIndex,
1070                                      MDNode *VTableHolder,
1071                                      unsigned Flags,
1072                                      bool isOptimized,
1073                                      Function *Fn,
1074                                      MDNode *TParam) {
1075   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1076          "function types should be subroutines");
1077   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1078   Value *Elts[] = {
1079     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1080     F.getFileNode(),
1081     getNonCompileUnitScope(Context),
1082     MDString::get(VMContext, Name),
1083     MDString::get(VMContext, Name),
1084     MDString::get(VMContext, LinkageName),
1085     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1086     Ty,
1087     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1088     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1089     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1090     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1091     VTableHolder,
1092     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1093     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1094     Fn,
1095     TParam,
1096     Constant::getNullValue(Type::getInt32Ty(VMContext)),
1097     MDNode::getTemporary(VMContext, TElts),
1098     // FIXME: Do we want to use different scope/lines?
1099     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1100   };
1101   MDNode *Node = MDNode::get(VMContext, Elts);
1102   if (isDefinition)
1103     AllSubprograms.push_back(Node);
1104   DISubprogram S(Node);
1105   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1106   return S;
1107 }
1108
1109 /// createNameSpace - This creates new descriptor for a namespace
1110 /// with the specified parent scope.
1111 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1112                                        DIFile File, unsigned LineNo) {
1113   Value *Elts[] = {
1114     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1115     File.getFileNode(),
1116     getNonCompileUnitScope(Scope),
1117     MDString::get(VMContext, Name),
1118     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1119   };
1120   DINameSpace R(MDNode::get(VMContext, Elts));
1121   assert(R.Verify() &&
1122          "createNameSpace should return a verifiable DINameSpace");
1123   return R;
1124 }
1125
1126 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1127 /// an existing scope with a new filename.
1128 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1129                                                      DIFile File) {
1130   Value *Elts[] = {
1131     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1132     File.getFileNode(),
1133     Scope
1134   };
1135   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1136   assert(
1137       R.Verify() &&
1138       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1139   return R;
1140 }
1141
1142 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1143                                              unsigned Line, unsigned Col) {
1144   // Defeat MDNode uniquing for lexical blocks by using unique id.
1145   static unsigned int unique_id = 0;
1146   Value *Elts[] = {
1147     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1148     File.getFileNode(),
1149     getNonCompileUnitScope(Scope),
1150     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1151     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1152     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1153   };
1154   DILexicalBlock R(MDNode::get(VMContext, Elts));
1155   assert(R.Verify() &&
1156          "createLexicalBlock should return a verifiable DILexicalBlock");
1157   return R;
1158 }
1159
1160 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1161 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1162                                       Instruction *InsertBefore) {
1163   assert(Storage && "no storage passed to dbg.declare");
1164   assert(VarInfo.isVariable() &&
1165          "empty or invalid DIVariable passed to dbg.declare");
1166   if (!DeclareFn)
1167     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1168
1169   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1170   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1171 }
1172
1173 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1174 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1175                                       BasicBlock *InsertAtEnd) {
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
1184   // If this block already has a terminator then insert this intrinsic
1185   // before the terminator.
1186   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1187     return CallInst::Create(DeclareFn, Args, "", T);
1188   else
1189     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1190 }
1191
1192 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1193 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1194                                                 DIVariable VarInfo,
1195                                                 Instruction *InsertBefore) {
1196   assert(V && "no value passed to dbg.value");
1197   assert(VarInfo.isVariable() &&
1198          "empty or invalid DIVariable passed to dbg.value");
1199   if (!ValueFn)
1200     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1201
1202   Value *Args[] = { MDNode::get(V->getContext(), V),
1203                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1204                     VarInfo };
1205   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1206 }
1207
1208 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1209 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1210                                                 DIVariable VarInfo,
1211                                                 BasicBlock *InsertAtEnd) {
1212   assert(V && "no value passed to dbg.value");
1213   assert(VarInfo.isVariable() &&
1214          "empty or invalid DIVariable passed to dbg.value");
1215   if (!ValueFn)
1216     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1217
1218   Value *Args[] = { MDNode::get(V->getContext(), V),
1219                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1220                     VarInfo };
1221   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
1222 }