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