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 DIBuilder::createObjCIVar(StringRef Name, DIFile File,
00473                                         unsigned LineNumber,
00474                                         uint64_t SizeInBits,
00475                                         uint64_t AlignInBits,
00476                                         uint64_t OffsetInBits, unsigned Flags,
00477                                         DIType Ty, MDNode *PropertyNode) {
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     PropertyNode
00491   };
00492   return DIDerivedType(MDNode::get(VMContext, Elts));
00493 }
00494 
00495 /// createObjCProperty - Create debugging information entry for Objective-C
00496 /// property.
00497 DIObjCProperty
00498 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
00499                               StringRef GetterName, StringRef SetterName,
00500                               unsigned PropertyAttributes, DIType Ty) {
00501   Value *Elts[] = {
00502     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
00503     MDString::get(VMContext, Name),
00504     File,
00505     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00506     MDString::get(VMContext, GetterName),
00507     MDString::get(VMContext, SetterName),
00508     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
00509     Ty
00510   };
00511   return DIObjCProperty(MDNode::get(VMContext, Elts));
00512 }
00513 
00514 /// createTemplateTypeParameter - Create debugging information for template
00515 /// type parameter.
00516 DITemplateTypeParameter
00517 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
00518                                        DIType Ty, MDNode *File, unsigned LineNo,
00519                                        unsigned ColumnNo) {
00520   Value *Elts[] = {
00521     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
00522     DIScope(getNonCompileUnitScope(Context)).getRef(),
00523     MDString::get(VMContext, Name),
00524     Ty.getRef(),
00525     File,
00526     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00527     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
00528   };
00529   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
00530 }
00531 
00532 DITemplateValueParameter
00533 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
00534                                         StringRef Name, DIType Ty,
00535                                         Value *Val, MDNode *File,
00536                                         unsigned LineNo,
00537                                         unsigned ColumnNo) {
00538   Value *Elts[] = {
00539     GetTagConstant(VMContext, Tag),
00540     DIScope(getNonCompileUnitScope(Context)).getRef(),
00541     MDString::get(VMContext, Name),
00542     Ty.getRef(),
00543     Val,
00544     File,
00545     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00546     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
00547   };
00548   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
00549 }
00550 
00551 /// createTemplateValueParameter - Create debugging information for template
00552 /// value parameter.
00553 DITemplateValueParameter
00554 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
00555                                         DIType Ty, Value *Val,
00556                                         MDNode *File, unsigned LineNo,
00557                                         unsigned ColumnNo) {
00558   return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
00559                                       Context, Name, Ty, Val, File, LineNo,
00560                                       ColumnNo);
00561 }
00562 
00563 DITemplateValueParameter
00564 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
00565                                            DIType Ty, StringRef Val,
00566                                            MDNode *File, unsigned LineNo,
00567                                            unsigned ColumnNo) {
00568   return createTemplateValueParameter(
00569       dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
00570       MDString::get(VMContext, Val), File, LineNo, ColumnNo);
00571 }
00572 
00573 DITemplateValueParameter
00574 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
00575                                        DIType Ty, DIArray Val,
00576                                        MDNode *File, unsigned LineNo,
00577                                        unsigned ColumnNo) {
00578   return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
00579                                       Context, Name, Ty, Val, File, LineNo,
00580                                       ColumnNo);
00581 }
00582 
00583 /// createClassType - Create debugging information entry for a class.
00584 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
00585                                            DIFile File, unsigned LineNumber,
00586                                            uint64_t SizeInBits,
00587                                            uint64_t AlignInBits,
00588                                            uint64_t OffsetInBits,
00589                                            unsigned Flags, DIType DerivedFrom,
00590                                            DIArray Elements,
00591                                            DIType VTableHolder,
00592                                            MDNode *TemplateParams,
00593                                            StringRef UniqueIdentifier) {
00594   assert((!Context || Context.isScope() || Context.isType()) &&
00595          "createClassType should be called with a valid Context");
00596   // TAG_class_type is encoded in DICompositeType format.
00597   Value *Elts[] = {
00598     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
00599     File.getFileNode(),
00600     DIScope(getNonCompileUnitScope(Context)).getRef(),
00601     MDString::get(VMContext, Name),
00602     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00603     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00604     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00605     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
00606     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00607     DerivedFrom.getRef(),
00608     Elements,
00609     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00610     VTableHolder.getRef(),
00611     TemplateParams,
00612     UniqueIdentifier.empty() ? nullptr
00613                              : MDString::get(VMContext, UniqueIdentifier)
00614   };
00615   DICompositeType R(MDNode::get(VMContext, Elts));
00616   assert(R.isCompositeType() &&
00617          "createClassType should return a DICompositeType");
00618   if (!UniqueIdentifier.empty())
00619     retainType(R);
00620   return R;
00621 }
00622 
00623 /// createStructType - Create debugging information entry for a struct.
00624 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
00625                                             StringRef Name, DIFile File,
00626                                             unsigned LineNumber,
00627                                             uint64_t SizeInBits,
00628                                             uint64_t AlignInBits,
00629                                             unsigned Flags, DIType DerivedFrom,
00630                                             DIArray Elements,
00631                                             unsigned RunTimeLang,
00632                                             DIType VTableHolder,
00633                                             StringRef UniqueIdentifier) {
00634  // TAG_structure_type is encoded in DICompositeType format.
00635   Value *Elts[] = {
00636     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
00637     File.getFileNode(),
00638     DIScope(getNonCompileUnitScope(Context)).getRef(),
00639     MDString::get(VMContext, Name),
00640     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00641     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00642     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00643     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00644     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00645     DerivedFrom.getRef(),
00646     Elements,
00647     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
00648     VTableHolder.getRef(),
00649     nullptr,
00650     UniqueIdentifier.empty() ? nullptr
00651                              : MDString::get(VMContext, UniqueIdentifier)
00652   };
00653   DICompositeType R(MDNode::get(VMContext, Elts));
00654   assert(R.isCompositeType() &&
00655          "createStructType should return a DICompositeType");
00656   if (!UniqueIdentifier.empty())
00657     retainType(R);
00658   return R;
00659 }
00660 
00661 /// createUnionType - Create debugging information entry for an union.
00662 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
00663                                            DIFile File, unsigned LineNumber,
00664                                            uint64_t SizeInBits,
00665                                            uint64_t AlignInBits, unsigned Flags,
00666                                            DIArray Elements,
00667                                            unsigned RunTimeLang,
00668                                            StringRef UniqueIdentifier) {
00669   // TAG_union_type is encoded in DICompositeType format.
00670   Value *Elts[] = {
00671     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
00672     File.getFileNode(),
00673     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00674     MDString::get(VMContext, Name),
00675     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00676     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00677     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00678     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00679     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00680     nullptr,
00681     Elements,
00682     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
00683     nullptr,
00684     nullptr,
00685     UniqueIdentifier.empty() ? nullptr
00686                              : MDString::get(VMContext, UniqueIdentifier)
00687   };
00688   DICompositeType R(MDNode::get(VMContext, Elts));
00689   if (!UniqueIdentifier.empty())
00690     retainType(R);
00691   return R;
00692 }
00693 
00694 /// createSubroutineType - Create subroutine type.
00695 DISubroutineType DIBuilder::createSubroutineType(DIFile File,
00696                                                  DITypeArray ParameterTypes,
00697                                                  unsigned Flags) {
00698   // TAG_subroutine_type is encoded in DICompositeType format.
00699   Value *Elts[] = {
00700     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
00701     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00702     nullptr,
00703     MDString::get(VMContext, ""),
00704     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00705     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00706     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00707     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00708     ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
00709     nullptr,
00710     ParameterTypes,
00711     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00712     nullptr,
00713     nullptr,
00714     nullptr  // Type Identifer
00715   };
00716   return DISubroutineType(MDNode::get(VMContext, Elts));
00717 }
00718 
00719 /// createEnumerationType - Create debugging information entry for an
00720 /// enumeration.
00721 DICompositeType DIBuilder::createEnumerationType(
00722     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
00723     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
00724     DIType UnderlyingType, StringRef UniqueIdentifier) {
00725   // TAG_enumeration_type is encoded in DICompositeType format.
00726   Value *Elts[] = {
00727     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
00728     File.getFileNode(),
00729     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00730     MDString::get(VMContext, Name),
00731     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00732     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00733     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00734     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00735     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00736     UnderlyingType.getRef(),
00737     Elements,
00738     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00739     nullptr,
00740     nullptr,
00741     UniqueIdentifier.empty() ? nullptr
00742                              : MDString::get(VMContext, UniqueIdentifier)
00743   };
00744   DICompositeType CTy(MDNode::get(VMContext, Elts));
00745   AllEnumTypes.push_back(CTy);
00746   if (!UniqueIdentifier.empty())
00747     retainType(CTy);
00748   return CTy;
00749 }
00750 
00751 /// createArrayType - Create debugging information entry for an array.
00752 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
00753                                            DIType Ty, DIArray Subscripts) {
00754   // TAG_array_type is encoded in DICompositeType format.
00755   Value *Elts[] = {
00756     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
00757     nullptr, // Filename/Directory,
00758     nullptr, // Unused
00759     MDString::get(VMContext, ""),
00760     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00761     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
00762     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00763     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00764     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00765     Ty.getRef(),
00766     Subscripts,
00767     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00768     nullptr,
00769     nullptr,
00770     nullptr  // Type Identifer
00771   };
00772   return DICompositeType(MDNode::get(VMContext, Elts));
00773 }
00774 
00775 /// createVectorType - Create debugging information entry for a vector.
00776 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
00777                                             DIType Ty, DIArray Subscripts) {
00778   // A vector is an array type with the FlagVector flag applied.
00779   Value *Elts[] = {
00780     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
00781     nullptr, // Filename/Directory,
00782     nullptr, // Unused
00783     MDString::get(VMContext, ""),
00784     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00785     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
00786     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00787     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00788     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
00789     Ty.getRef(),
00790     Subscripts,
00791     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00792     nullptr,
00793     nullptr,
00794     nullptr  // Type Identifer
00795   };
00796   return DICompositeType(MDNode::get(VMContext, Elts));
00797 }
00798 
00799 /// createArtificialType - Create a new DIType with "artificial" flag set.
00800 DIType DIBuilder::createArtificialType(DIType Ty) {
00801   if (Ty.isArtificial())
00802     return Ty;
00803 
00804   SmallVector<Value *, 9> Elts;
00805   MDNode *N = Ty;
00806   assert (N && "Unexpected input DIType!");
00807   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
00808     Elts.push_back(N->getOperand(i));
00809 
00810   unsigned CurFlags = Ty.getFlags();
00811   CurFlags = CurFlags | DIType::FlagArtificial;
00812 
00813   // Flags are stored at this slot.
00814   // FIXME: Add an enum for this magic value.
00815   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
00816 
00817   return DIType(MDNode::get(VMContext, Elts));
00818 }
00819 
00820 /// createObjectPointerType - Create a new type with both the object pointer
00821 /// and artificial flags set.
00822 DIType DIBuilder::createObjectPointerType(DIType Ty) {
00823   if (Ty.isObjectPointer())
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::FlagObjectPointer | 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 /// retainType - Retain DIType in a module even if it is not referenced
00843 /// through debug info anchors.
00844 void DIBuilder::retainType(DIType T) {
00845   AllRetainTypes.push_back(TrackingVH<MDNode>(T));
00846 }
00847 
00848 /// createUnspecifiedParameter - Create unspeicified type descriptor
00849 /// for the subroutine type.
00850 DIBasicType DIBuilder::createUnspecifiedParameter() {
00851   return DIBasicType();
00852 }
00853 
00854 /// createForwardDecl - Create a permanent forward-declared type.
00855 DICompositeType
00856 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
00857                              DIFile F, unsigned Line, unsigned RuntimeLang,
00858                              uint64_t SizeInBits, uint64_t AlignInBits,
00859                              StringRef UniqueIdentifier) {
00860   // Create a temporary MDNode.
00861   Value *Elts[] = {
00862     GetTagConstant(VMContext, Tag),
00863     F.getFileNode(),
00864     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00865     MDString::get(VMContext, Name),
00866     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00867     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00868     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00869     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00870     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
00871     nullptr,
00872     DIArray(),
00873     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
00874     nullptr,
00875     nullptr, //TemplateParams
00876     UniqueIdentifier.empty() ? nullptr
00877                              : MDString::get(VMContext, UniqueIdentifier)
00878   };
00879   MDNode *Node = MDNode::get(VMContext, Elts);
00880   DICompositeType RetTy(Node);
00881   assert(RetTy.isCompositeType() &&
00882          "createForwardDecl result should be a DIType");
00883   if (!UniqueIdentifier.empty())
00884     retainType(RetTy);
00885   return RetTy;
00886 }
00887 
00888 /// createReplaceableForwardDecl - Create a temporary forward-declared type that
00889 /// can be RAUW'd if the full type is seen.
00890 DICompositeType DIBuilder::createReplaceableForwardDecl(
00891     unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
00892     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
00893     StringRef UniqueIdentifier) {
00894   // Create a temporary MDNode.
00895   Value *Elts[] = {
00896     GetTagConstant(VMContext, Tag),
00897     F.getFileNode(),
00898     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00899     MDString::get(VMContext, Name),
00900     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00901     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00902     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00903     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00904     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
00905     nullptr,
00906     DIArray(),
00907     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
00908     nullptr,
00909     nullptr, //TemplateParams
00910     UniqueIdentifier.empty() ? nullptr
00911                              : MDString::get(VMContext, UniqueIdentifier)
00912   };
00913   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
00914   DICompositeType RetTy(Node);
00915   assert(RetTy.isCompositeType() &&
00916          "createReplaceableForwardDecl result should be a DIType");
00917   if (!UniqueIdentifier.empty())
00918     retainType(RetTy);
00919   return RetTy;
00920 }
00921 
00922 /// getOrCreateArray - Get a DIArray, create one if required.
00923 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
00924   return DIArray(MDNode::get(VMContext, Elements));
00925 }
00926 
00927 /// getOrCreateTypeArray - Get a DITypeArray, create one if required.
00928 DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
00929   SmallVector<llvm::Value *, 16> Elts; 
00930   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
00931     if (Elements[i] && isa<MDNode>(Elements[i]))
00932       Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
00933     else
00934       Elts.push_back(Elements[i]);
00935   }
00936   return DITypeArray(MDNode::get(VMContext, Elts));
00937 }
00938 
00939 /// getOrCreateSubrange - Create a descriptor for a value range.  This
00940 /// implicitly uniques the values returned.
00941 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
00942   Value *Elts[] = {
00943     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
00944     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
00945     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
00946   };
00947 
00948   return DISubrange(MDNode::get(VMContext, Elts));
00949 }
00950 
00951 /// \brief Create a new descriptor for the specified global.
00952 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
00953                                                  StringRef LinkageName,
00954                                                  DIFile F, unsigned LineNumber,
00955                                                  DITypeRef Ty, bool isLocalToUnit,
00956                                                  Value *Val) {
00957   Value *Elts[] = {
00958     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
00959     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00960     nullptr, // TheCU,
00961     MDString::get(VMContext, Name),
00962     MDString::get(VMContext, Name),
00963     MDString::get(VMContext, LinkageName),
00964     F,
00965     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00966     Ty,
00967     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
00968     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
00969     Val,
00970     DIDescriptor()
00971   };
00972   MDNode *Node = MDNode::get(VMContext, Elts);
00973   AllGVs.push_back(Node);
00974   return DIGlobalVariable(Node);
00975 }
00976 
00977 /// \brief Create a new descriptor for the specified global.
00978 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
00979                                                  unsigned LineNumber,
00980                                                  DITypeRef Ty,
00981                                                  bool isLocalToUnit,
00982                                                  Value *Val) {
00983   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
00984                               Val);
00985 }
00986 
00987 static DIGlobalVariable
00988 createStaticVariableHelper(LLVMContext &VMContext, DIDescriptor Context,
00989                            StringRef Name, StringRef LinkageName, DIFile F,
00990                            unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit,
00991                            Value *Val, MDNode *Decl, bool isDefinition,
00992                            std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
00993   Value *Elts[] = {
00994     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
00995     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00996     getNonCompileUnitScope(Context),
00997     MDString::get(VMContext, Name),
00998     MDString::get(VMContext, Name),
00999     MDString::get(VMContext, LinkageName),
01000     F,
01001     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
01002     Ty,
01003     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
01004     ConstantInt::get(Type::getInt32Ty(VMContext), isDefinition),
01005     Val,
01006     DIDescriptor(Decl)
01007   };
01008 
01009   return DIGlobalVariable(CreateFunc(Elts));
01010 }
01011 
01012 /// createStaticVariable - Create a new descriptor for the specified
01013 /// variable.
01014 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
01015                                                  StringRef Name,
01016                                                  StringRef LinkageName,
01017                                                  DIFile F, unsigned LineNumber,
01018                                                  DITypeRef Ty,
01019                                                  bool isLocalToUnit,
01020                                                  Value *Val, MDNode *Decl) {
01021   return createStaticVariableHelper(VMContext, Context, Name, LinkageName, F,
01022                                     LineNumber, Ty, isLocalToUnit, Val, Decl, true,
01023                                     [&] (ArrayRef<Value *> Elts) -> MDNode * {
01024                                       MDNode *Node = MDNode::get(VMContext, Elts);
01025                                       AllGVs.push_back(Node);
01026                                       return Node;
01027                                     });
01028 }
01029 
01030 /// createTempStaticVariableFwdDecl - Create a new temporary descriptor for the
01031 /// specified variable declarartion.
01032 DIGlobalVariable
01033 DIBuilder::createTempStaticVariableFwdDecl(DIDescriptor Context,
01034                                            StringRef Name,
01035                                            StringRef LinkageName,
01036                                            DIFile F, unsigned LineNumber,
01037                                            DITypeRef Ty,
01038                                            bool isLocalToUnit,
01039                                            Value *Val, MDNode *Decl) {
01040   return createStaticVariableHelper(VMContext, Context, Name, LinkageName, F,
01041                                     LineNumber, Ty, isLocalToUnit, Val, Decl, false,
01042                                     [&] (ArrayRef<Value *> Elts) {
01043                                       return MDNode::getTemporary(VMContext, Elts);
01044                                     });
01045 }
01046 
01047 /// createVariable - Create a new descriptor for the specified variable.
01048 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
01049                                           StringRef Name, DIFile File,
01050                                           unsigned LineNo, DITypeRef Ty,
01051                                           bool AlwaysPreserve, unsigned Flags,
01052                                           unsigned ArgNo) {
01053   DIDescriptor Context(getNonCompileUnitScope(Scope));
01054   assert((!Context || Context.isScope()) &&
01055          "createLocalVariable should be called with a valid Context");
01056   Value *Elts[] = {
01057     GetTagConstant(VMContext, Tag),
01058     getNonCompileUnitScope(Scope),
01059     MDString::get(VMContext, Name),
01060     File,
01061     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
01062     Ty,
01063     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01064     Constant::getNullValue(Type::getInt32Ty(VMContext))
01065   };
01066   MDNode *Node = MDNode::get(VMContext, Elts);
01067   if (AlwaysPreserve) {
01068     // The optimizer may remove local variable. If there is an interest
01069     // to preserve variable info in such situation then stash it in a
01070     // named mdnode.
01071     DISubprogram Fn(getDISubprogram(Scope));
01072     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
01073     FnLocals->addOperand(Node);
01074   }
01075   DIVariable RetVar(Node);
01076   assert(RetVar.isVariable() &&
01077          "createLocalVariable should return a valid DIVariable");
01078   return RetVar;
01079 }
01080 
01081 /// createComplexVariable - Create a new descriptor for the specified variable
01082 /// which has a complex address expression for its address.
01083 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
01084                                             StringRef Name, DIFile F,
01085                                             unsigned LineNo,
01086                                             DITypeRef Ty,
01087                                             ArrayRef<Value *> Addr,
01088                                             unsigned ArgNo) {
01089   assert(Addr.size() > 0 && "complex address is empty");
01090   Value *Elts[] = {
01091     GetTagConstant(VMContext, Tag),
01092     getNonCompileUnitScope(Scope),
01093     MDString::get(VMContext, Name),
01094     F,
01095     ConstantInt::get(Type::getInt32Ty(VMContext),
01096                      (LineNo | (ArgNo << 24))),
01097     Ty,
01098     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01099     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01100     MDNode::get(VMContext, Addr)
01101   };
01102   return DIVariable(MDNode::get(VMContext, Elts));
01103 }
01104 
01105 /// createVariablePiece - Create a descriptor to describe one part
01106 /// of aggregate variable that is fragmented across multiple Values.
01107 DIVariable DIBuilder::createVariablePiece(DIVariable Variable,
01108                                           unsigned OffsetInBytes,
01109                                           unsigned SizeInBytes) {
01110   assert(SizeInBytes > 0 && "zero-size piece");
01111   Value *Addr[] = {
01112     ConstantInt::get(Type::getInt32Ty(VMContext), OpPiece),
01113     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBytes),
01114     ConstantInt::get(Type::getInt32Ty(VMContext), SizeInBytes)
01115   };
01116 
01117   assert((Variable->getNumOperands() == 8 || Variable.isVariablePiece()) &&
01118          "variable already has a complex address");
01119   SmallVector<Value *, 9> Elts;
01120   for (unsigned i = 0; i < 8; ++i)
01121     Elts.push_back(Variable->getOperand(i));
01122 
01123   Elts.push_back(MDNode::get(VMContext, Addr));
01124   return DIVariable(MDNode::get(VMContext, Elts));
01125 }
01126 
01127 /// createFunction - Create a new descriptor for the specified function.
01128 /// FIXME: this is added for dragonegg. Once we update dragonegg
01129 /// to call resolve function, this will be removed.
01130 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
01131                                        StringRef LinkageName, DIFile File,
01132                                        unsigned LineNo, DICompositeType Ty,
01133                                        bool isLocalToUnit, bool isDefinition,
01134                                        unsigned ScopeLine, unsigned Flags,
01135                                        bool isOptimized, Function *Fn,
01136                                        MDNode *TParams, MDNode *Decl) {
01137   // dragonegg does not generate identifier for types, so using an empty map
01138   // to resolve the context should be fine.
01139   DITypeIdentifierMap EmptyMap;
01140   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
01141                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01142                         Flags, isOptimized, Fn, TParams, Decl);
01143 }
01144 
01145 static DISubprogram
01146 createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
01147                      StringRef LinkageName, DIFile File, unsigned LineNo,
01148                      DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
01149                      unsigned ScopeLine, unsigned Flags, bool isOptimized,
01150                      Function *Fn, MDNode *TParams, MDNode *Decl,
01151                      std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
01152   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01153          "function types should be subroutines");
01154   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
01155   Value *Elts[] = {
01156     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01157     File.getFileNode(),
01158     DIScope(getNonCompileUnitScope(Context)).getRef(),
01159     MDString::get(VMContext, Name),
01160     MDString::get(VMContext, Name),
01161     MDString::get(VMContext, LinkageName),
01162     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01163     Ty,
01164     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01165     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01166     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
01167     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
01168     nullptr,
01169     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01170     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01171     Fn,
01172     TParams,
01173     Decl,
01174     MDNode::getTemporary(VMContext, TElts),
01175     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
01176   };
01177 
01178   DISubprogram S(CreateFunc(Elts));
01179   assert(S.isSubprogram() &&
01180          "createFunction should return a valid DISubprogram");
01181   return S;
01182 }
01183 
01184 
01185 /// createFunction - Create a new descriptor for the specified function.
01186 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
01187                                        StringRef LinkageName, DIFile File,
01188                                        unsigned LineNo, DICompositeType Ty,
01189                                        bool isLocalToUnit, bool isDefinition,
01190                                        unsigned ScopeLine, unsigned Flags,
01191                                        bool isOptimized, Function *Fn,
01192                                        MDNode *TParams, MDNode *Decl) {
01193   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
01194                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01195                               Flags, isOptimized, Fn, TParams, Decl,
01196                               [&] (ArrayRef<Value *> Elts) -> MDNode *{
01197                                 MDNode *Node = MDNode::get(VMContext, Elts);
01198                                 // Create a named metadata so that we
01199                                 // do not lose this mdnode.
01200                                 if (isDefinition)
01201                                   AllSubprograms.push_back(Node);
01202                                 return Node;
01203                               });
01204 }
01205 
01206 /// createTempFunctionFwdDecl - Create a new temporary descriptor for
01207 /// the specified function declaration.
01208 DISubprogram
01209 DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
01210                                      StringRef LinkageName, DIFile File,
01211                                      unsigned LineNo, DICompositeType Ty,
01212                                      bool isLocalToUnit, bool isDefinition,
01213                                      unsigned ScopeLine, unsigned Flags,
01214                                      bool isOptimized, Function *Fn,
01215                                      MDNode *TParams, MDNode *Decl) {
01216   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
01217                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01218                               Flags, isOptimized, Fn, TParams, Decl,
01219                               [&] (ArrayRef<Value *> Elts) {
01220                                 return MDNode::getTemporary(VMContext, Elts);
01221                               });
01222 }
01223 
01224 /// createMethod - Create a new descriptor for the specified C++ method.
01225 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
01226                                      StringRef LinkageName, DIFile F,
01227                                      unsigned LineNo, DICompositeType Ty,
01228                                      bool isLocalToUnit, bool isDefinition,
01229                                      unsigned VK, unsigned VIndex,
01230                                      DIType VTableHolder, unsigned Flags,
01231                                      bool isOptimized, Function *Fn,
01232                                      MDNode *TParam) {
01233   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01234          "function types should be subroutines");
01235   assert(getNonCompileUnitScope(Context) &&
01236          "Methods should have both a Context and a context that isn't "
01237          "the compile unit.");
01238   Value *Elts[] = {
01239     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01240     F.getFileNode(),
01241     DIScope(Context).getRef(),
01242     MDString::get(VMContext, Name),
01243     MDString::get(VMContext, Name),
01244     MDString::get(VMContext, LinkageName),
01245     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01246     Ty,
01247     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01248     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01249     ConstantInt::get(Type::getInt32Ty(VMContext), VK),
01250     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
01251     VTableHolder.getRef(),
01252     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01253     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01254     Fn,
01255     TParam,
01256     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01257     nullptr,
01258     // FIXME: Do we want to use different scope/lines?
01259     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01260   };
01261   MDNode *Node = MDNode::get(VMContext, Elts);
01262   if (isDefinition)
01263     AllSubprograms.push_back(Node);
01264   DISubprogram S(Node);
01265   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
01266   return S;
01267 }
01268 
01269 /// createNameSpace - This creates new descriptor for a namespace
01270 /// with the specified parent scope.
01271 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
01272                                        DIFile File, unsigned LineNo) {
01273   Value *Elts[] = {
01274     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
01275     File.getFileNode(),
01276     getNonCompileUnitScope(Scope),
01277     MDString::get(VMContext, Name),
01278     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01279   };
01280   DINameSpace R(MDNode::get(VMContext, Elts));
01281   assert(R.Verify() &&
01282          "createNameSpace should return a verifiable DINameSpace");
01283   return R;
01284 }
01285 
01286 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
01287 /// an existing scope with a new filename.
01288 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
01289                                                      DIFile File,
01290                                                      unsigned Discriminator) {
01291   Value *Elts[] = {
01292     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01293     File.getFileNode(),
01294     Scope,
01295     ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
01296   };
01297   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
01298   assert(
01299       R.Verify() &&
01300       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
01301   return R;
01302 }
01303 
01304 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
01305                                              unsigned Line, unsigned Col) {
01306   // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
01307   // I believe the right way is to have a self-referential element in the node.
01308   // Also: why do we bother with line/column - they're not used and the
01309   // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
01310   // for uniquing, yet then we have this other solution (because line/col were
01311   // inadequate) anyway. Remove all 3 and replace them with a self-reference.
01312 
01313   // Defeat MDNode uniquing for lexical blocks by using unique id.
01314   static unsigned int unique_id = 0;
01315   Value *Elts[] = {
01316     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01317     File.getFileNode(),
01318     getNonCompileUnitScope(Scope),
01319     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
01320     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
01321     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
01322   };
01323   DILexicalBlock R(MDNode::get(VMContext, Elts));
01324   assert(R.Verify() &&
01325          "createLexicalBlock should return a verifiable DILexicalBlock");
01326   return R;
01327 }
01328 
01329 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01330 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01331                                       Instruction *InsertBefore) {
01332   assert(Storage && "no storage passed to dbg.declare");
01333   assert(VarInfo.isVariable() &&
01334          "empty or invalid DIVariable passed to dbg.declare");
01335   if (!DeclareFn)
01336     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01337 
01338   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01339   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
01340 }
01341 
01342 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01343 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01344                                       BasicBlock *InsertAtEnd) {
01345   assert(Storage && "no storage passed to dbg.declare");
01346   assert(VarInfo.isVariable() &&
01347          "empty or invalid DIVariable passed to dbg.declare");
01348   if (!DeclareFn)
01349     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01350 
01351   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01352 
01353   // If this block already has a terminator then insert this intrinsic
01354   // before the terminator.
01355   if (TerminatorInst *T = InsertAtEnd->getTerminator())
01356     return CallInst::Create(DeclareFn, Args, "", T);
01357   else
01358     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
01359 }
01360 
01361 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01362 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01363                                                 DIVariable VarInfo,
01364                                                 Instruction *InsertBefore) {
01365   assert(V && "no value passed to dbg.value");
01366   assert(VarInfo.isVariable() &&
01367          "empty or invalid DIVariable passed to dbg.value");
01368   if (!ValueFn)
01369     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01370 
01371   Value *Args[] = { MDNode::get(V->getContext(), V),
01372                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01373                     VarInfo };
01374   return CallInst::Create(ValueFn, Args, "", InsertBefore);
01375 }
01376 
01377 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01378 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01379                                                 DIVariable VarInfo,
01380                                                 BasicBlock *InsertAtEnd) {
01381   assert(V && "no value passed to dbg.value");
01382   assert(VarInfo.isVariable() &&
01383          "empty or invalid DIVariable passed to dbg.value");
01384   if (!ValueFn)
01385     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01386 
01387   Value *Args[] = { MDNode::get(V->getContext(), V),
01388                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01389                     VarInfo };
01390   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
01391 }