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