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