LLVM API Documentation

DIBuilder.cpp
Go to the documentation of this file.
00001 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the DIBuilder.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/DIBuilder.h"
00015 #include "llvm/ADT/STLExtras.h"
00016 #include "llvm/DebugInfo.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/IntrinsicInst.h"
00019 #include "llvm/IR/Module.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Support/Dwarf.h"
00022 
00023 using namespace llvm;
00024 using namespace llvm::dwarf;
00025 
00026 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
00027   assert((Tag & LLVMDebugVersionMask) == 0 &&
00028          "Tag too large for debug encoding!");
00029   return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
00030 }
00031 
00032 DIBuilder::DIBuilder(Module &m)
00033   : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
00034     TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
00035     ValueFn(0)
00036 {}
00037 
00038 /// finalize - Construct any deferred debug info descriptors.
00039 void DIBuilder::finalize() {
00040   DIArray Enums = getOrCreateArray(AllEnumTypes);
00041   DIType(TempEnumTypes).replaceAllUsesWith(Enums);
00042 
00043   DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
00044   DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
00045 
00046   DIArray SPs = getOrCreateArray(AllSubprograms);
00047   DIType(TempSubprograms).replaceAllUsesWith(SPs);
00048   for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
00049     DISubprogram SP(SPs.getElement(i));
00050     SmallVector<Value *, 4> Variables;
00051     if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
00052       for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
00053         Variables.push_back(NMD->getOperand(ii));
00054       NMD->eraseFromParent();
00055     }
00056     if (MDNode *Temp = SP.getVariablesNodes()) {
00057       DIArray AV = getOrCreateArray(Variables);
00058       DIType(Temp).replaceAllUsesWith(AV);
00059     }
00060   }
00061 
00062   DIArray GVs = getOrCreateArray(AllGVs);
00063   DIType(TempGVs).replaceAllUsesWith(GVs);
00064 
00065   DIArray IMs = getOrCreateArray(AllImportedModules);
00066   DIType(TempImportedModules).replaceAllUsesWith(IMs);
00067 }
00068 
00069 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
00070 /// N.
00071 static MDNode *getNonCompileUnitScope(MDNode *N) {
00072   if (DIDescriptor(N).isCompileUnit())
00073     return NULL;
00074   return N;
00075 }
00076 
00077 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
00078                                   StringRef Directory) {
00079   assert(!Filename.empty() && "Unable to create file without name");
00080   Value *Pair[] = {
00081     MDString::get(VMContext, Filename),
00082     MDString::get(VMContext, Directory),
00083   };
00084   return MDNode::get(VMContext, Pair);
00085 }
00086 
00087 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
00088 /// information generated during this instance of compilation.
00089 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
00090                                   StringRef Directory, StringRef Producer,
00091                                   bool isOptimized, StringRef Flags,
00092                                   unsigned RunTimeVer, StringRef SplitName) {
00093   assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
00094           (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
00095          "Invalid Language tag");
00096   assert(!Filename.empty() &&
00097          "Unable to create compile unit without filename");
00098   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
00099   TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
00100 
00101   TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
00102 
00103   TempSubprograms = MDNode::getTemporary(VMContext, TElts);
00104 
00105   TempGVs = MDNode::getTemporary(VMContext, TElts);
00106 
00107   TempImportedModules = MDNode::getTemporary(VMContext, TElts);
00108 
00109   Value *Elts[] = {
00110     GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
00111     createFilePathPair(VMContext, Filename, Directory),
00112     ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
00113     MDString::get(VMContext, Producer),
00114     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
00115     MDString::get(VMContext, Flags),
00116     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
00117     TempEnumTypes,
00118     TempRetainTypes,
00119     TempSubprograms,
00120     TempGVs,
00121     TempImportedModules,
00122     MDString::get(VMContext, SplitName)
00123   };
00124   TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
00125 
00126   // Create a named metadata so that it is easier to find cu in a module.
00127   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
00128   NMD->addOperand(TheCU);
00129 }
00130 
00131 static DIImportedEntity
00132 createImportedModule(LLVMContext &C, DIScope Context, DIDescriptor NS,
00133                      unsigned Line, StringRef Name,
00134                      SmallVectorImpl<Value *> &AllImportedModules) {
00135   const MDNode *R;
00136   if (Name.empty()) {
00137     Value *Elts[] = {
00138       GetTagConstant(C, dwarf::DW_TAG_imported_module),
00139       Context,
00140       NS,
00141       ConstantInt::get(Type::getInt32Ty(C), Line),
00142     };
00143     R = MDNode::get(C, Elts);
00144   } else {
00145     Value *Elts[] = {
00146       GetTagConstant(C, dwarf::DW_TAG_imported_module),
00147       Context,
00148       NS,
00149       ConstantInt::get(Type::getInt32Ty(C), Line),
00150       MDString::get(C, Name)
00151     };
00152     R = MDNode::get(C, Elts);
00153   }
00154   DIImportedEntity M(R);
00155   assert(M.Verify() && "Imported module should be valid");
00156   AllImportedModules.push_back(M);
00157   return M;
00158 }
00159 
00160 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
00161                                                  DINameSpace NS, unsigned Line,
00162                                                  StringRef Name) {
00163   return ::createImportedModule(VMContext, Context, NS, Line, Name,
00164                                 AllImportedModules);
00165 }
00166 
00167 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
00168                                                  DIImportedEntity NS,
00169                                                  unsigned Line,
00170                                                  StringRef Name) {
00171   return ::createImportedModule(VMContext, Context, NS, Line, Name,
00172                                 AllImportedModules);
00173 }
00174 
00175 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
00176                                                       DIDescriptor Decl,
00177                                                       unsigned Line) {
00178   Value *Elts[] = {
00179     GetTagConstant(VMContext, dwarf::DW_TAG_imported_declaration),
00180     Context,
00181     Decl,
00182     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00183   };
00184   DIImportedEntity M(MDNode::get(VMContext, Elts));
00185   assert(M.Verify() && "Imported module should be valid");
00186   AllImportedModules.push_back(M);
00187   return M;
00188 }
00189 
00190 /// createFile - Create a file descriptor to hold debugging information
00191 /// for a file.
00192 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
00193   Value *Elts[] = {
00194     GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
00195     createFilePathPair(VMContext, Filename, Directory)
00196   };
00197   return DIFile(MDNode::get(VMContext, Elts));
00198 }
00199 
00200 /// createEnumerator - Create a single enumerator value.
00201 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
00202   assert(!Name.empty() && "Unable to create enumerator without name");
00203   Value *Elts[] = {
00204     GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
00205     MDString::get(VMContext, Name),
00206     ConstantInt::get(Type::getInt64Ty(VMContext), Val)
00207   };
00208   return DIEnumerator(MDNode::get(VMContext, Elts));
00209 }
00210 
00211 /// createNullPtrType - Create C++0x nullptr type.
00212 DIType DIBuilder::createNullPtrType(StringRef Name) {
00213   assert(!Name.empty() && "Unable to create type without name");
00214   // nullptr is encoded in DIBasicType format. Line number, filename,
00215   // ,size, alignment, offset and flags are always empty here.
00216   Value *Elts[] = {
00217     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
00218     NULL, // Filename
00219     NULL, //TheCU,
00220     MDString::get(VMContext, Name),
00221     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00222     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00223     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00224     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00225     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
00226     ConstantInt::get(Type::getInt32Ty(VMContext), 0)  // Encoding
00227   };
00228   return DIType(MDNode::get(VMContext, Elts));
00229 }
00230 
00231 /// createBasicType - Create debugging information entry for a basic
00232 /// type, e.g 'char'.
00233 DIBasicType
00234 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
00235                            uint64_t AlignInBits, unsigned Encoding) {
00236   assert(!Name.empty() && "Unable to create type without name");
00237   // Basic types are encoded in DIBasicType format. Line number, filename,
00238   // offset and flags are always empty here.
00239   Value *Elts[] = {
00240     GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
00241     NULL, // File/directory name
00242     NULL, //TheCU,
00243     MDString::get(VMContext, Name),
00244     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00245     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00246     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00247     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00248     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
00249     ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
00250   };
00251   return DIBasicType(MDNode::get(VMContext, Elts));
00252 }
00253 
00254 /// createQualifiedType - Create debugging information entry for a qualified
00255 /// type, e.g. 'const int'.
00256 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
00257   // Qualified types are encoded in DIDerivedType format.
00258   Value *Elts[] = {
00259     GetTagConstant(VMContext, Tag),
00260     NULL, // Filename
00261     NULL, //TheCU,
00262     MDString::get(VMContext, StringRef()), // Empty name.
00263     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00264     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00265     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00266     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00267     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00268     FromTy
00269   };
00270   return DIDerivedType(MDNode::get(VMContext, Elts));
00271 }
00272 
00273 /// createPointerType - Create debugging information entry for a pointer.
00274 DIDerivedType
00275 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
00276                              uint64_t AlignInBits, StringRef Name) {
00277   // Pointer types are encoded in DIDerivedType format.
00278   Value *Elts[] = {
00279     GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
00280     NULL, // Filename
00281     NULL, //TheCU,
00282     MDString::get(VMContext, Name),
00283     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00284     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00285     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00286     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00287     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00288     PointeeTy
00289   };
00290   return DIDerivedType(MDNode::get(VMContext, Elts));
00291 }
00292 
00293 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
00294                                                  DIType Base) {
00295   // Pointer types are encoded in DIDerivedType format.
00296   Value *Elts[] = {
00297     GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
00298     NULL, // Filename
00299     NULL, //TheCU,
00300     NULL,
00301     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00302     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
00303     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
00304     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00305     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00306     PointeeTy,
00307     Base
00308   };
00309   return DIDerivedType(MDNode::get(VMContext, Elts));
00310 }
00311 
00312 /// createReferenceType - Create debugging information entry for a reference
00313 /// type.
00314 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
00315   assert(RTy.Verify() && "Unable to create reference type");
00316   // References are encoded in DIDerivedType format.
00317   Value *Elts[] = {
00318     GetTagConstant(VMContext, Tag),
00319     NULL, // Filename
00320     NULL, // TheCU,
00321     NULL, // Name
00322     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00323     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00324     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00325     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00326     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00327     RTy
00328   };
00329   return DIDerivedType(MDNode::get(VMContext, Elts));
00330 }
00331 
00332 /// createTypedef - Create debugging information entry for a typedef.
00333 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
00334                                        unsigned LineNo, DIDescriptor Context) {
00335   // typedefs are encoded in DIDerivedType format.
00336   assert(Ty.Verify() && "Invalid typedef type!");
00337   Value *Elts[] = {
00338     GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
00339     File.getFileNode(),
00340     getNonCompileUnitScope(Context),
00341     MDString::get(VMContext, Name),
00342     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00343     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00344     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00345     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00346     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00347     Ty
00348   };
00349   return DIDerivedType(MDNode::get(VMContext, Elts));
00350 }
00351 
00352 /// createFriend - Create debugging information entry for a 'friend'.
00353 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
00354   // typedefs are encoded in DIDerivedType format.
00355   assert(Ty.Verify() && "Invalid type!");
00356   assert(FriendTy.Verify() && "Invalid friend type!");
00357   Value *Elts[] = {
00358     GetTagConstant(VMContext, dwarf::DW_TAG_friend),
00359     NULL,
00360     Ty,
00361     NULL, // Name
00362     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00363     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00364     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00365     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00366     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00367     FriendTy
00368   };
00369   return DIType(MDNode::get(VMContext, Elts));
00370 }
00371 
00372 /// createInheritance - Create debugging information entry to establish
00373 /// inheritance relationship between two types.
00374 DIDerivedType DIBuilder::createInheritance(
00375     DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
00376   assert(Ty.Verify() && "Unable to create inheritance");
00377   // TAG_inheritance is encoded in DIDerivedType format.
00378   Value *Elts[] = {
00379     GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
00380     NULL,
00381     Ty,
00382     NULL, // Name
00383     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00384     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00385     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00386     ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
00387     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00388     BaseTy
00389   };
00390   return DIDerivedType(MDNode::get(VMContext, Elts));
00391 }
00392 
00393 /// createMemberType - Create debugging information entry for a member.
00394 DIDerivedType DIBuilder::createMemberType(
00395     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
00396     uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
00397     unsigned Flags, DIType Ty) {
00398   // TAG_member is encoded in DIDerivedType format.
00399   Value *Elts[] = {
00400     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00401     File.getFileNode(),
00402     getNonCompileUnitScope(Scope),
00403     MDString::get(VMContext, Name),
00404     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00405     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00406     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00407     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00408     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00409     Ty
00410   };
00411   return DIDerivedType(MDNode::get(VMContext, Elts));
00412 }
00413 
00414 /// createStaticMemberType - Create debugging information entry for a
00415 /// C++ static data member.
00416 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
00417                                          DIFile File, unsigned LineNumber,
00418                                          DIType Ty, unsigned Flags,
00419                                          llvm::Value *Val) {
00420   // TAG_member is encoded in DIDerivedType format.
00421   Flags |= DIDescriptor::FlagStaticMember;
00422   Value *Elts[] = {
00423     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00424     File.getFileNode(),
00425     getNonCompileUnitScope(Scope),
00426     MDString::get(VMContext, Name),
00427     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00428     ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
00429     ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
00430     ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
00431     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00432     Ty,
00433     Val
00434   };
00435   return DIType(MDNode::get(VMContext, Elts));
00436 }
00437 
00438 /// createObjCIVar - Create debugging information entry for Objective-C
00439 /// instance variable.
00440 DIType DIBuilder::createObjCIVar(StringRef Name,
00441                                  DIFile File, unsigned LineNumber,
00442                                  uint64_t SizeInBits, uint64_t AlignInBits,
00443                                  uint64_t OffsetInBits, unsigned Flags,
00444                                  DIType Ty, StringRef PropertyName,
00445                                  StringRef GetterName, StringRef SetterName,
00446                                  unsigned PropertyAttributes) {
00447   // TAG_member is encoded in DIDerivedType format.
00448   Value *Elts[] = {
00449     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00450     File.getFileNode(),
00451     getNonCompileUnitScope(File),
00452     MDString::get(VMContext, Name),
00453     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00454     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00455     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00456     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00457     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00458     Ty,
00459     MDString::get(VMContext, PropertyName),
00460     MDString::get(VMContext, GetterName),
00461     MDString::get(VMContext, SetterName),
00462     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
00463   };
00464   return DIType(MDNode::get(VMContext, Elts));
00465 }
00466 
00467 /// createObjCIVar - Create debugging information entry for Objective-C
00468 /// instance variable.
00469 DIType DIBuilder::createObjCIVar(StringRef Name,
00470                                  DIFile File, unsigned LineNumber,
00471                                  uint64_t SizeInBits, uint64_t AlignInBits,
00472                                  uint64_t OffsetInBits, unsigned Flags,
00473                                  DIType Ty, MDNode *PropertyNode) {
00474   // TAG_member is encoded in DIDerivedType format.
00475   Value *Elts[] = {
00476     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00477     File.getFileNode(),
00478     getNonCompileUnitScope(File),
00479     MDString::get(VMContext, Name),
00480     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00481     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00482     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00483     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00484     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00485     Ty,
00486     PropertyNode
00487   };
00488   return DIType(MDNode::get(VMContext, Elts));
00489 }
00490 
00491 /// createObjCProperty - Create debugging information entry for Objective-C
00492 /// property.
00493 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
00494                                              DIFile File, unsigned LineNumber,
00495                                              StringRef GetterName,
00496                                              StringRef SetterName,
00497                                              unsigned PropertyAttributes,
00498                                              DIType Ty) {
00499   Value *Elts[] = {
00500     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
00501     MDString::get(VMContext, Name),
00502     File,
00503     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00504     MDString::get(VMContext, GetterName),
00505     MDString::get(VMContext, SetterName),
00506     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
00507     Ty
00508   };
00509   return DIObjCProperty(MDNode::get(VMContext, Elts));
00510 }
00511 
00512 /// createTemplateTypeParameter - Create debugging information for template
00513 /// type parameter.
00514 DITemplateTypeParameter
00515 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
00516                                        DIType Ty, MDNode *File, unsigned LineNo,
00517                                        unsigned ColumnNo) {
00518   Value *Elts[] = {
00519     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
00520     getNonCompileUnitScope(Context),
00521     MDString::get(VMContext, Name),
00522     Ty,
00523     File,
00524     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00525     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
00526   };
00527   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
00528 }
00529 
00530 /// createTemplateValueParameter - Create debugging information for template
00531 /// value parameter.
00532 DITemplateValueParameter
00533 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
00534                                         DIType Ty, Value *Val,
00535                                         MDNode *File, unsigned LineNo,
00536                                         unsigned ColumnNo) {
00537   Value *Elts[] = {
00538     GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
00539     getNonCompileUnitScope(Context),
00540     MDString::get(VMContext, Name),
00541     Ty,
00542     Val,
00543     File,
00544     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00545     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
00546   };
00547   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
00548 }
00549 
00550 /// createClassType - Create debugging information entry for a class.
00551 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
00552                                            DIFile File, unsigned LineNumber,
00553                                            uint64_t SizeInBits,
00554                                            uint64_t AlignInBits,
00555                                            uint64_t OffsetInBits,
00556                                            unsigned Flags, DIType DerivedFrom,
00557                                            DIArray Elements,
00558                                            MDNode *VTableHolder,
00559                                            MDNode *TemplateParams) {
00560   assert((!Context || Context.Verify()) &&
00561          "createClassType should be called with a valid Context");
00562   // TAG_class_type is encoded in DICompositeType format.
00563   Value *Elts[] = {
00564     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
00565     File.getFileNode(),
00566     getNonCompileUnitScope(Context),
00567     MDString::get(VMContext, Name),
00568     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00569     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00570     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00571     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
00572     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00573     DerivedFrom,
00574     Elements,
00575     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00576     VTableHolder,
00577     TemplateParams
00578   };
00579   DICompositeType R(MDNode::get(VMContext, Elts));
00580   assert(R.Verify() && "createClassType should return a verifiable DIType");
00581   return R;
00582 }
00583 
00584 /// createStructType - Create debugging information entry for a struct.
00585 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
00586                                             StringRef Name, DIFile File,
00587                                             unsigned LineNumber,
00588                                             uint64_t SizeInBits,
00589                                             uint64_t AlignInBits,
00590                                             unsigned Flags, DIType DerivedFrom,
00591                                             DIArray Elements,
00592                                             unsigned RunTimeLang,
00593                                             MDNode *VTableHolder) {
00594  // TAG_structure_type is encoded in DICompositeType format.
00595   Value *Elts[] = {
00596     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
00597     File.getFileNode(),
00598     getNonCompileUnitScope(Context),
00599     MDString::get(VMContext, Name),
00600     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00601     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00602     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00603     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00604     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00605     DerivedFrom,
00606     Elements,
00607     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
00608     VTableHolder,
00609     NULL,
00610   };
00611   DICompositeType R(MDNode::get(VMContext, Elts));
00612   assert(R.Verify() && "createStructType should return a verifiable DIType");
00613   return R;
00614 }
00615 
00616 /// createUnionType - Create debugging information entry for an union.
00617 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
00618                                            DIFile File, unsigned LineNumber,
00619                                            uint64_t SizeInBits,
00620                                            uint64_t AlignInBits, unsigned Flags,
00621                                            DIArray Elements,
00622                                            unsigned RunTimeLang) {
00623   // TAG_union_type is encoded in DICompositeType format.
00624   Value *Elts[] = {
00625     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
00626     File.getFileNode(),
00627     getNonCompileUnitScope(Scope),
00628     MDString::get(VMContext, Name),
00629     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00630     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00631     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00632     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
00633     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00634     NULL,
00635     Elements,
00636     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
00637     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00638     NULL
00639   };
00640   return DICompositeType(MDNode::get(VMContext, Elts));
00641 }
00642 
00643 /// createSubroutineType - Create subroutine type.
00644 DICompositeType
00645 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
00646   // TAG_subroutine_type is encoded in DICompositeType format.
00647   Value *Elts[] = {
00648     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
00649     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00650     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00651     MDString::get(VMContext, ""),
00652     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00653     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
00654     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
00655     ConstantInt::get(Type::getInt64Ty(VMContext), 0),
00656     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00657     NULL,
00658     ParameterTypes,
00659     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00660     Constant::getNullValue(Type::getInt32Ty(VMContext))
00661   };
00662   return DICompositeType(MDNode::get(VMContext, Elts));
00663 }
00664 
00665 /// createEnumerationType - Create debugging information entry for an
00666 /// enumeration.
00667 DICompositeType DIBuilder::createEnumerationType(
00668     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
00669     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
00670     DIType UnderlyingType) {
00671   // TAG_enumeration_type is encoded in DICompositeType format.
00672   Value *Elts[] = {
00673     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
00674     File.getFileNode(),
00675     getNonCompileUnitScope(Scope),
00676     MDString::get(VMContext, Name),
00677     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00678     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00679     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00680     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00681     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00682     UnderlyingType,
00683     Elements,
00684     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00685     Constant::getNullValue(Type::getInt32Ty(VMContext))
00686   };
00687   MDNode *Node = MDNode::get(VMContext, Elts);
00688   AllEnumTypes.push_back(Node);
00689   return DICompositeType(Node);
00690 }
00691 
00692 /// createArrayType - Create debugging information entry for an array.
00693 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
00694                                            DIType Ty, DIArray Subscripts) {
00695   // TAG_array_type is encoded in DICompositeType format.
00696   Value *Elts[] = {
00697     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
00698     NULL, // Filename/Directory,
00699     NULL, //TheCU,
00700     MDString::get(VMContext, ""),
00701     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00702     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
00703     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00704     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00705     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00706     Ty,
00707     Subscripts,
00708     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00709     Constant::getNullValue(Type::getInt32Ty(VMContext))
00710   };
00711   return DICompositeType(MDNode::get(VMContext, Elts));
00712 }
00713 
00714 /// createVectorType - Create debugging information entry for a vector.
00715 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
00716                                    DIType Ty, DIArray Subscripts) {
00717 
00718   // A vector is an array type with the FlagVector flag applied.
00719   Value *Elts[] = {
00720     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
00721     NULL, // Filename/Directory,
00722     NULL, //TheCU,
00723     MDString::get(VMContext, ""),
00724     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00725     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
00726     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00727     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00728     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
00729     Ty,
00730     Subscripts,
00731     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00732     Constant::getNullValue(Type::getInt32Ty(VMContext))
00733   };
00734   return DIType(MDNode::get(VMContext, Elts));
00735 }
00736 
00737 /// createArtificialType - Create a new DIType with "artificial" flag set.
00738 DIType DIBuilder::createArtificialType(DIType Ty) {
00739   if (Ty.isArtificial())
00740     return Ty;
00741 
00742   SmallVector<Value *, 9> Elts;
00743   MDNode *N = Ty;
00744   assert (N && "Unexpected input DIType!");
00745   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
00746     if (Value *V = N->getOperand(i))
00747       Elts.push_back(V);
00748     else
00749       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
00750   }
00751 
00752   unsigned CurFlags = Ty.getFlags();
00753   CurFlags = CurFlags | DIType::FlagArtificial;
00754 
00755   // Flags are stored at this slot.
00756   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
00757 
00758   return DIType(MDNode::get(VMContext, Elts));
00759 }
00760 
00761 /// createObjectPointerType - Create a new type with both the object pointer
00762 /// and artificial flags set.
00763 DIType DIBuilder::createObjectPointerType(DIType Ty) {
00764   if (Ty.isObjectPointer())
00765     return Ty;
00766 
00767   SmallVector<Value *, 9> Elts;
00768   MDNode *N = Ty;
00769   assert (N && "Unexpected input DIType!");
00770   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
00771     if (Value *V = N->getOperand(i))
00772       Elts.push_back(V);
00773     else
00774       Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
00775   }
00776 
00777   unsigned CurFlags = Ty.getFlags();
00778   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
00779 
00780   // Flags are stored at this slot.
00781   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
00782 
00783   return DIType(MDNode::get(VMContext, Elts));
00784 }
00785 
00786 /// retainType - Retain DIType in a module even if it is not referenced
00787 /// through debug info anchors.
00788 void DIBuilder::retainType(DIType T) {
00789   AllRetainTypes.push_back(T);
00790 }
00791 
00792 /// createUnspecifiedParameter - Create unspeicified type descriptor
00793 /// for the subroutine type.
00794 DIDescriptor DIBuilder::createUnspecifiedParameter() {
00795   Value *Elts[] = {
00796     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
00797   };
00798   return DIDescriptor(MDNode::get(VMContext, Elts));
00799 }
00800 
00801 /// createForwardDecl - Create a temporary forward-declared type that
00802 /// can be RAUW'd if the full type is seen.
00803 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
00804                                     DIDescriptor Scope, DIFile F,
00805                                     unsigned Line, unsigned RuntimeLang,
00806                                     uint64_t SizeInBits,
00807                                     uint64_t AlignInBits) {
00808   // Create a temporary MDNode.
00809   Value *Elts[] = {
00810     GetTagConstant(VMContext, Tag),
00811     F.getFileNode(),
00812     getNonCompileUnitScope(Scope),
00813     MDString::get(VMContext, Name),
00814     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00815     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00816     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00817     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00818     ConstantInt::get(Type::getInt32Ty(VMContext),
00819                      DIDescriptor::FlagFwdDecl),
00820     NULL,
00821     DIArray(),
00822     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
00823   };
00824   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
00825   assert(DIType(Node).Verify() &&
00826          "createForwardDecl result should be verifiable");
00827   return DIType(Node);
00828 }
00829 
00830 /// getOrCreateArray - Get a DIArray, create one if required.
00831 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
00832   if (Elements.empty()) {
00833     Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
00834     return DIArray(MDNode::get(VMContext, Null));
00835   }
00836   return DIArray(MDNode::get(VMContext, Elements));
00837 }
00838 
00839 /// getOrCreateSubrange - Create a descriptor for a value range.  This
00840 /// implicitly uniques the values returned.
00841 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
00842   Value *Elts[] = {
00843     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
00844     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
00845     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
00846   };
00847 
00848   return DISubrange(MDNode::get(VMContext, Elts));
00849 }
00850 
00851 /// \brief Create a new descriptor for the specified global.
00852 DIGlobalVariable DIBuilder::
00853 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
00854                      unsigned LineNumber, DIType Ty, bool isLocalToUnit,
00855                      Value *Val) {
00856   Value *Elts[] = {
00857     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
00858     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00859     NULL, // TheCU,
00860     MDString::get(VMContext, Name),
00861     MDString::get(VMContext, Name),
00862     MDString::get(VMContext, LinkageName),
00863     F,
00864     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00865     Ty,
00866     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
00867     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
00868     Val,
00869     DIDescriptor()
00870   };
00871   MDNode *Node = MDNode::get(VMContext, Elts);
00872   AllGVs.push_back(Node);
00873   return DIGlobalVariable(Node);
00874 }
00875 
00876 /// \brief Create a new descriptor for the specified global.
00877 DIGlobalVariable DIBuilder::
00878 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
00879                      DIType Ty, bool isLocalToUnit, Value *Val) {
00880   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
00881                               Val);
00882 }
00883 
00884 /// createStaticVariable - Create a new descriptor for the specified static
00885 /// variable.
00886 DIGlobalVariable DIBuilder::
00887 createStaticVariable(DIDescriptor Context, StringRef Name,
00888                      StringRef LinkageName, DIFile F, unsigned LineNumber,
00889                      DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
00890   Value *Elts[] = {
00891     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
00892     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00893     getNonCompileUnitScope(Context),
00894     MDString::get(VMContext, Name),
00895     MDString::get(VMContext, Name),
00896     MDString::get(VMContext, LinkageName),
00897     F,
00898     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00899     Ty,
00900     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
00901     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
00902     Val,
00903     DIDescriptor(Decl)
00904   };
00905   MDNode *Node = MDNode::get(VMContext, Elts);
00906   AllGVs.push_back(Node);
00907   return DIGlobalVariable(Node);
00908 }
00909 
00910 /// createVariable - Create a new descriptor for the specified variable.
00911 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
00912                                           StringRef Name, DIFile File,
00913                                           unsigned LineNo, DIType Ty,
00914                                           bool AlwaysPreserve, unsigned Flags,
00915                                           unsigned ArgNo) {
00916   DIDescriptor Context(getNonCompileUnitScope(Scope));
00917   assert((!Context || Context.Verify()) &&
00918          "createLocalVariable should be called with a valid Context");
00919   assert(Ty.Verify() &&
00920          "createLocalVariable should be called with a valid type");
00921   Value *Elts[] = {
00922     GetTagConstant(VMContext, Tag),
00923     getNonCompileUnitScope(Scope),
00924     MDString::get(VMContext, Name),
00925     File,
00926     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
00927     Ty,
00928     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00929     Constant::getNullValue(Type::getInt32Ty(VMContext))
00930   };
00931   MDNode *Node = MDNode::get(VMContext, Elts);
00932   if (AlwaysPreserve) {
00933     // The optimizer may remove local variable. If there is an interest
00934     // to preserve variable info in such situation then stash it in a
00935     // named mdnode.
00936     DISubprogram Fn(getDISubprogram(Scope));
00937     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
00938     FnLocals->addOperand(Node);
00939   }
00940   assert(DIVariable(Node).Verify() &&
00941          "createLocalVariable should return a verifiable DIVariable");
00942   return DIVariable(Node);
00943 }
00944 
00945 /// createComplexVariable - Create a new descriptor for the specified variable
00946 /// which has a complex address expression for its address.
00947 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
00948                                             StringRef Name, DIFile F,
00949                                             unsigned LineNo,
00950                                             DIType Ty, ArrayRef<Value *> Addr,
00951                                             unsigned ArgNo) {
00952   SmallVector<Value *, 15> Elts;
00953   Elts.push_back(GetTagConstant(VMContext, Tag));
00954   Elts.push_back(getNonCompileUnitScope(Scope)),
00955   Elts.push_back(MDString::get(VMContext, Name));
00956   Elts.push_back(F);
00957   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
00958                                   (LineNo | (ArgNo << 24))));
00959   Elts.push_back(Ty);
00960   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
00961   Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
00962   Elts.append(Addr.begin(), Addr.end());
00963 
00964   return DIVariable(MDNode::get(VMContext, Elts));
00965 }
00966 
00967 /// createFunction - Create a new descriptor for the specified function.
00968 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
00969                                        StringRef Name,
00970                                        StringRef LinkageName,
00971                                        DIFile File, unsigned LineNo,
00972                                        DICompositeType Ty,
00973                                        bool isLocalToUnit, bool isDefinition,
00974                                        unsigned ScopeLine,
00975                                        unsigned Flags, bool isOptimized,
00976                                        Function *Fn,
00977                                        MDNode *TParams,
00978                                        MDNode *Decl) {
00979   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
00980          "function types should be subroutines");
00981   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
00982   Value *Elts[] = {
00983     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
00984     File.getFileNode(),
00985     getNonCompileUnitScope(Context),
00986     MDString::get(VMContext, Name),
00987     MDString::get(VMContext, Name),
00988     MDString::get(VMContext, LinkageName),
00989     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00990     Ty,
00991     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
00992     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
00993     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00994     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00995     NULL,
00996     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00997     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
00998     Fn,
00999     TParams,
01000     Decl,
01001     MDNode::getTemporary(VMContext, TElts),
01002     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
01003   };
01004   MDNode *Node = MDNode::get(VMContext, Elts);
01005 
01006   // Create a named metadata so that we do not lose this mdnode.
01007   if (isDefinition)
01008     AllSubprograms.push_back(Node);
01009   DISubprogram S(Node);
01010   assert(S.Verify() && "createFunction should return a valid DISubprogram");
01011   return S;
01012 }
01013 
01014 /// createMethod - Create a new descriptor for the specified C++ method.
01015 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
01016                                      StringRef Name,
01017                                      StringRef LinkageName,
01018                                      DIFile F,
01019                                      unsigned LineNo, DICompositeType Ty,
01020                                      bool isLocalToUnit,
01021                                      bool isDefinition,
01022                                      unsigned VK, unsigned VIndex,
01023                                      MDNode *VTableHolder,
01024                                      unsigned Flags,
01025                                      bool isOptimized,
01026                                      Function *Fn,
01027                                      MDNode *TParam) {
01028   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01029          "function types should be subroutines");
01030   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
01031   Value *Elts[] = {
01032     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01033     F.getFileNode(),
01034     getNonCompileUnitScope(Context),
01035     MDString::get(VMContext, Name),
01036     MDString::get(VMContext, Name),
01037     MDString::get(VMContext, LinkageName),
01038     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01039     Ty,
01040     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01041     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01042     ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
01043     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
01044     VTableHolder,
01045     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01046     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01047     Fn,
01048     TParam,
01049     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01050     MDNode::getTemporary(VMContext, TElts),
01051     // FIXME: Do we want to use different scope/lines?
01052     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01053   };
01054   MDNode *Node = MDNode::get(VMContext, Elts);
01055   if (isDefinition)
01056     AllSubprograms.push_back(Node);
01057   DISubprogram S(Node);
01058   assert(S.Verify() && "createMethod should return a valid DISubprogram");
01059   return S;
01060 }
01061 
01062 /// createNameSpace - This creates new descriptor for a namespace
01063 /// with the specified parent scope.
01064 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
01065                                        DIFile File, unsigned LineNo) {
01066   Value *Elts[] = {
01067     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
01068     File.getFileNode(),
01069     getNonCompileUnitScope(Scope),
01070     MDString::get(VMContext, Name),
01071     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01072   };
01073   DINameSpace R(MDNode::get(VMContext, Elts));
01074   assert(R.Verify() &&
01075          "createNameSpace should return a verifiable DINameSpace");
01076   return R;
01077 }
01078 
01079 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
01080 /// an existing scope with a new filename.
01081 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
01082                                                      DIFile File) {
01083   Value *Elts[] = {
01084     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01085     File.getFileNode(),
01086     Scope
01087   };
01088   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
01089   assert(
01090       R.Verify() &&
01091       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
01092   return R;
01093 }
01094 
01095 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
01096                                              unsigned Line, unsigned Col) {
01097   // Defeat MDNode uniqing for lexical blocks by using unique id.
01098   static unsigned int unique_id = 0;
01099   Value *Elts[] = {
01100     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01101     File.getFileNode(),
01102     getNonCompileUnitScope(Scope),
01103     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
01104     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
01105     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
01106   };
01107   DILexicalBlock R(MDNode::get(VMContext, Elts));
01108   assert(R.Verify() &&
01109          "createLexicalBlock should return a verifiable DILexicalBlock");
01110   return R;
01111 }
01112 
01113 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01114 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01115                                       Instruction *InsertBefore) {
01116   assert(Storage && "no storage passed to dbg.declare");
01117   assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
01118   if (!DeclareFn)
01119     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01120 
01121   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01122   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
01123 }
01124 
01125 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01126 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01127                                       BasicBlock *InsertAtEnd) {
01128   assert(Storage && "no storage passed to dbg.declare");
01129   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
01130   if (!DeclareFn)
01131     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01132 
01133   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01134 
01135   // If this block already has a terminator then insert this intrinsic
01136   // before the terminator.
01137   if (TerminatorInst *T = InsertAtEnd->getTerminator())
01138     return CallInst::Create(DeclareFn, Args, "", T);
01139   else
01140     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
01141 }
01142 
01143 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01144 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01145                                                 DIVariable VarInfo,
01146                                                 Instruction *InsertBefore) {
01147   assert(V && "no value passed to dbg.value");
01148   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
01149   if (!ValueFn)
01150     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01151 
01152   Value *Args[] = { MDNode::get(V->getContext(), V),
01153                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01154                     VarInfo };
01155   return CallInst::Create(ValueFn, Args, "", InsertBefore);
01156 }
01157 
01158 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01159 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01160                                                 DIVariable VarInfo,
01161                                                 BasicBlock *InsertAtEnd) {
01162   assert(V && "no value passed to dbg.value");
01163   assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
01164   if (!ValueFn)
01165     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01166 
01167   Value *Args[] = { MDNode::get(V->getContext(), V),
01168                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01169                     VarInfo };
01170   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
01171 }