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