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