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