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