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