Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:21

0001 //===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// This file contains constants used for implementing Dwarf
0011 /// debug support.
0012 ///
0013 /// For details on the Dwarf specfication see the latest DWARF Debugging
0014 /// Information Format standard document on http://www.dwarfstd.org. This
0015 /// file often includes support for non-released standard features.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_BINARYFORMAT_DWARF_H
0020 #define LLVM_BINARYFORMAT_DWARF_H
0021 
0022 #include "llvm/Support/Compiler.h"
0023 #include "llvm/Support/DataTypes.h"
0024 #include "llvm/Support/ErrorHandling.h"
0025 #include "llvm/Support/Format.h"
0026 #include "llvm/Support/FormatVariadicDetails.h"
0027 #include "llvm/TargetParser/Triple.h"
0028 
0029 #include <limits>
0030 
0031 namespace llvm {
0032 class StringRef;
0033 
0034 namespace dwarf {
0035 
0036 //===----------------------------------------------------------------------===//
0037 // DWARF constants as gleaned from the DWARF Debugging Information Format V.5
0038 // reference manual http://www.dwarfstd.org/.
0039 //
0040 
0041 // Do not mix the following two enumerations sets.  DW_TAG_invalid changes the
0042 // enumeration base type.
0043 
0044 enum LLVMConstants : uint32_t {
0045   /// LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
0046   /// \{
0047   DW_TAG_invalid = ~0U,        ///< Tag for invalid results.
0048   DW_VIRTUALITY_invalid = ~0U, ///< Virtuality for invalid results.
0049   DW_MACINFO_invalid = ~0U,    ///< Macinfo type for invalid results.
0050   /// \}
0051 
0052   /// Special values for an initial length field.
0053   /// \{
0054   DW_LENGTH_lo_reserved = 0xfffffff0, ///< Lower bound of the reserved range.
0055   DW_LENGTH_DWARF64 = 0xffffffff,     ///< Indicator of 64-bit DWARF format.
0056   DW_LENGTH_hi_reserved = 0xffffffff, ///< Upper bound of the reserved range.
0057   /// \}
0058 
0059   /// Other constants.
0060   /// \{
0061   DWARF_VERSION = 4,       ///< Default dwarf version we output.
0062   DW_PUBTYPES_VERSION = 2, ///< Section version number for .debug_pubtypes.
0063   DW_PUBNAMES_VERSION = 2, ///< Section version number for .debug_pubnames.
0064   DW_ARANGES_VERSION = 2,  ///< Section version number for .debug_aranges.
0065   /// \}
0066 
0067   /// Identifiers we use to distinguish vendor extensions.
0068   /// \{
0069   DWARF_VENDOR_DWARF = 0, ///< Defined in v2 or later of the DWARF standard.
0070   DWARF_VENDOR_APPLE = 1,
0071   DWARF_VENDOR_BORLAND = 2,
0072   DWARF_VENDOR_GNU = 3,
0073   DWARF_VENDOR_GOOGLE = 4,
0074   DWARF_VENDOR_LLVM = 5,
0075   DWARF_VENDOR_MIPS = 6,
0076   DWARF_VENDOR_WASM = 7,
0077   DWARF_VENDOR_ALTIUM,
0078   DWARF_VENDOR_COMPAQ,
0079   DWARF_VENDOR_GHS,
0080   DWARF_VENDOR_GO,
0081   DWARF_VENDOR_HP,
0082   DWARF_VENDOR_IBM,
0083   DWARF_VENDOR_INTEL,
0084   DWARF_VENDOR_PGI,
0085   DWARF_VENDOR_SUN,
0086   DWARF_VENDOR_UPC,
0087   ///\}
0088 };
0089 
0090 /// Constants that define the DWARF format as 32 or 64 bit.
0091 enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
0092 
0093 /// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
0094 /// Not inside an enum because a 64-bit value is needed.
0095 /// @{
0096 const uint32_t DW_CIE_ID = UINT32_MAX;
0097 const uint64_t DW64_CIE_ID = UINT64_MAX;
0098 /// @}
0099 
0100 /// Identifier of an invalid DIE offset in the .debug_info section.
0101 const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
0102 
0103 enum Tag : uint16_t {
0104 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) DW_TAG_##NAME = ID,
0105 #include "llvm/BinaryFormat/Dwarf.def"
0106   DW_TAG_lo_user = 0x4080,
0107   DW_TAG_hi_user = 0xffff,
0108   DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
0109 };
0110 
0111 inline bool isType(Tag T) {
0112   switch (T) {
0113   default:
0114     return false;
0115 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
0116   case DW_TAG_##NAME:                                                          \
0117     return (KIND == DW_KIND_TYPE);
0118 #include "llvm/BinaryFormat/Dwarf.def"
0119   }
0120 }
0121 
0122 /// Attributes.
0123 enum Attribute : uint16_t {
0124 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
0125 #include "llvm/BinaryFormat/Dwarf.def"
0126   DW_AT_lo_user = 0x2000,
0127   DW_AT_hi_user = 0x3fff,
0128 };
0129 
0130 enum Form : uint16_t {
0131 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
0132 #include "llvm/BinaryFormat/Dwarf.def"
0133   DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
0134 };
0135 
0136 enum LocationAtom {
0137 #define HANDLE_DW_OP(ID, NAME, OPERANDS, ARITY, VERSION, VENDOR)               \
0138   DW_OP_##NAME = ID,
0139 #include "llvm/BinaryFormat/Dwarf.def"
0140   DW_OP_lo_user = 0xe0,
0141   DW_OP_hi_user = 0xff,
0142   DW_OP_LLVM_fragment = 0x1000,          ///< Only used in LLVM metadata.
0143   DW_OP_LLVM_convert = 0x1001,           ///< Only used in LLVM metadata.
0144   DW_OP_LLVM_tag_offset = 0x1002,        ///< Only used in LLVM metadata.
0145   DW_OP_LLVM_entry_value = 0x1003,       ///< Only used in LLVM metadata.
0146   DW_OP_LLVM_implicit_pointer = 0x1004,  ///< Only used in LLVM metadata.
0147   DW_OP_LLVM_arg = 0x1005,               ///< Only used in LLVM metadata.
0148   DW_OP_LLVM_extract_bits_sext = 0x1006, ///< Only used in LLVM metadata.
0149   DW_OP_LLVM_extract_bits_zext = 0x1007, ///< Only used in LLVM metadata.
0150 };
0151 
0152 enum LlvmUserLocationAtom {
0153 #define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) DW_OP_LLVM_##NAME = ID,
0154 #include "llvm/BinaryFormat/Dwarf.def"
0155 };
0156 
0157 enum TypeKind : uint8_t {
0158 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
0159 #include "llvm/BinaryFormat/Dwarf.def"
0160   DW_ATE_lo_user = 0x80,
0161   DW_ATE_hi_user = 0xff
0162 };
0163 
0164 enum DecimalSignEncoding {
0165   // Decimal sign attribute values
0166   DW_DS_unsigned = 0x01,
0167   DW_DS_leading_overpunch = 0x02,
0168   DW_DS_trailing_overpunch = 0x03,
0169   DW_DS_leading_separate = 0x04,
0170   DW_DS_trailing_separate = 0x05
0171 };
0172 
0173 enum EndianityEncoding {
0174   // Endianity attribute values
0175 #define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
0176 #include "llvm/BinaryFormat/Dwarf.def"
0177   DW_END_lo_user = 0x40,
0178   DW_END_hi_user = 0xff
0179 };
0180 
0181 enum AccessAttribute {
0182   // Accessibility codes
0183   DW_ACCESS_public = 0x01,
0184   DW_ACCESS_protected = 0x02,
0185   DW_ACCESS_private = 0x03
0186 };
0187 
0188 enum VisibilityAttribute {
0189   // Visibility codes
0190   DW_VIS_local = 0x01,
0191   DW_VIS_exported = 0x02,
0192   DW_VIS_qualified = 0x03
0193 };
0194 
0195 enum VirtualityAttribute {
0196 #define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
0197 #include "llvm/BinaryFormat/Dwarf.def"
0198   DW_VIRTUALITY_max = 0x02
0199 };
0200 
0201 enum DefaultedMemberAttribute {
0202 #define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
0203 #include "llvm/BinaryFormat/Dwarf.def"
0204   DW_DEFAULTED_max = 0x02
0205 };
0206 
0207 enum SourceLanguage {
0208 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
0209   DW_LANG_##NAME = ID,
0210 #include "llvm/BinaryFormat/Dwarf.def"
0211   DW_LANG_lo_user = 0x8000,
0212   DW_LANG_hi_user = 0xffff
0213 };
0214 
0215 enum SourceLanguageName : uint16_t {
0216 #define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND) DW_LNAME_##NAME = ID,
0217 #include "llvm/BinaryFormat/Dwarf.def"
0218 };
0219 
0220 /// Convert a DWARF 6 pair of language name and version to a DWARF 5 DW_LANG.
0221 /// If the version number doesn't exactly match a known version it is
0222 /// rounded up to the next-highest known version number.
0223 inline std::optional<SourceLanguage> toDW_LANG(SourceLanguageName name,
0224                                                uint32_t version) {
0225   switch (name) {
0226   case DW_LNAME_Ada: // YYYY
0227     if (version <= 1983)
0228       return DW_LANG_Ada83;
0229     if (version <= 1995)
0230       return DW_LANG_Ada95;
0231     if (version <= 2005)
0232       return DW_LANG_Ada2005;
0233     if (version <= 2012)
0234       return DW_LANG_Ada2012;
0235     return {};
0236   case DW_LNAME_BLISS:
0237     return DW_LANG_BLISS;
0238   case DW_LNAME_C: // YYYYMM, K&R 000000
0239     if (version == 0)
0240       return DW_LANG_C;
0241     if (version <= 198912)
0242       return DW_LANG_C89;
0243     if (version <= 199901)
0244       return DW_LANG_C99;
0245     if (version <= 201112)
0246       return DW_LANG_C11;
0247     if (version <= 201710)
0248       return DW_LANG_C17;
0249     return {};
0250   case DW_LNAME_C_plus_plus: // YYYYMM
0251     if (version == 0)
0252       return DW_LANG_C_plus_plus;
0253     if (version <= 199711)
0254       return DW_LANG_C_plus_plus;
0255     if (version <= 200310)
0256       return DW_LANG_C_plus_plus_03;
0257     if (version <= 201103)
0258       return DW_LANG_C_plus_plus_11;
0259     if (version <= 201402)
0260       return DW_LANG_C_plus_plus_14;
0261     if (version <= 201703)
0262       return DW_LANG_C_plus_plus_17;
0263     if (version <= 202002)
0264       return DW_LANG_C_plus_plus_20;
0265     return {};
0266   case DW_LNAME_Cobol: // YYYY
0267     if (version <= 1974)
0268       return DW_LANG_Cobol74;
0269     if (version <= 1985)
0270       return DW_LANG_Cobol85;
0271     return {};
0272   case DW_LNAME_Crystal:
0273     return DW_LANG_Crystal;
0274   case DW_LNAME_D:
0275     return DW_LANG_D;
0276   case DW_LNAME_Dylan:
0277     return DW_LANG_Dylan;
0278   case DW_LNAME_Fortran: // YYYY
0279     if (version <= 1977)
0280       return DW_LANG_Fortran77;
0281     if (version <= 1990)
0282       return DW_LANG_Fortran90;
0283     if (version <= 1995)
0284       return DW_LANG_Fortran95;
0285     if (version <= 2003)
0286       return DW_LANG_Fortran03;
0287     if (version <= 2008)
0288       return DW_LANG_Fortran08;
0289     if (version <= 2018)
0290       return DW_LANG_Fortran18;
0291     return {};
0292   case DW_LNAME_Go:
0293     return DW_LANG_Go;
0294   case DW_LNAME_Haskell:
0295     return DW_LANG_Haskell;
0296   // case DW_LNAME_HIP:
0297   //   return DW_LANG_HIP;
0298   case DW_LNAME_Java:
0299     return DW_LANG_Java;
0300   case DW_LNAME_Julia:
0301     return DW_LANG_Julia;
0302   case DW_LNAME_Kotlin:
0303     return DW_LANG_Kotlin;
0304   case DW_LNAME_Modula2:
0305     return DW_LANG_Modula2;
0306   case DW_LNAME_Modula3:
0307     return DW_LANG_Modula3;
0308   case DW_LNAME_ObjC:
0309     return DW_LANG_ObjC;
0310   case DW_LNAME_ObjC_plus_plus:
0311     return DW_LANG_ObjC_plus_plus;
0312   case DW_LNAME_OCaml:
0313     return DW_LANG_OCaml;
0314   case DW_LNAME_OpenCL_C:
0315     return DW_LANG_OpenCL;
0316   case DW_LNAME_Pascal:
0317     return DW_LANG_Pascal83;
0318   case DW_LNAME_PLI:
0319     return DW_LANG_PLI;
0320   case DW_LNAME_Python:
0321     return DW_LANG_Python;
0322   case DW_LNAME_RenderScript:
0323     return DW_LANG_RenderScript;
0324   case DW_LNAME_Rust:
0325     return DW_LANG_Rust;
0326   case DW_LNAME_Swift:
0327     return DW_LANG_Swift;
0328   case DW_LNAME_UPC:
0329     return DW_LANG_UPC;
0330   case DW_LNAME_Zig:
0331     return DW_LANG_Zig;
0332   case DW_LNAME_Assembly:
0333     return DW_LANG_Assembly;
0334   case DW_LNAME_C_sharp:
0335     return DW_LANG_C_sharp;
0336   case DW_LNAME_Mojo:
0337     return DW_LANG_Mojo;
0338   case DW_LNAME_GLSL:
0339     return DW_LANG_GLSL;
0340   case DW_LNAME_GLSL_ES:
0341     return DW_LANG_GLSL_ES;
0342   case DW_LNAME_HLSL:
0343     return DW_LANG_HLSL;
0344   case DW_LNAME_OpenCL_CPP:
0345     return DW_LANG_OpenCL_CPP;
0346   case DW_LNAME_CPP_for_OpenCL:
0347     return {};
0348   case DW_LNAME_SYCL:
0349     return DW_LANG_SYCL;
0350   case DW_LNAME_Ruby:
0351     return DW_LANG_Ruby;
0352   case DW_LNAME_Move:
0353     return DW_LANG_Move;
0354   case DW_LNAME_Hylo:
0355     return DW_LANG_Hylo;
0356   case DW_LNAME_Metal:
0357     return DW_LANG_Metal;
0358   }
0359   return {};
0360 }
0361 
0362 /// Convert a DWARF 5 DW_LANG to a DWARF 6 pair of language name and version.
0363 inline std::optional<std::pair<SourceLanguageName, uint32_t>>
0364 toDW_LNAME(SourceLanguage language) {
0365   switch (language) {
0366   case DW_LANG_Ada83:
0367     return {{DW_LNAME_Ada, 1983}};
0368   case DW_LANG_Ada95:
0369     return {{DW_LNAME_Ada, 1995}};
0370   case DW_LANG_Ada2005:
0371     return {{DW_LNAME_Ada, 2005}};
0372   case DW_LANG_Ada2012:
0373     return {{DW_LNAME_Ada, 2012}};
0374   case DW_LANG_BLISS:
0375     return {{DW_LNAME_BLISS, 0}};
0376   case DW_LANG_C:
0377     return {{DW_LNAME_C, 0}};
0378   case DW_LANG_C89:
0379     return {{DW_LNAME_C, 198912}};
0380   case DW_LANG_C99:
0381     return {{DW_LNAME_C, 199901}};
0382   case DW_LANG_C11:
0383     return {{DW_LNAME_C, 201112}};
0384   case DW_LANG_C17:
0385     return {{DW_LNAME_C, 201712}};
0386   case DW_LANG_C_plus_plus:
0387     return {{DW_LNAME_C_plus_plus, 0}};
0388   case DW_LANG_C_plus_plus_03:
0389     return {{DW_LNAME_C_plus_plus, 200310}};
0390   case DW_LANG_C_plus_plus_11:
0391     return {{DW_LNAME_C_plus_plus, 201103}};
0392   case DW_LANG_C_plus_plus_14:
0393     return {{DW_LNAME_C_plus_plus, 201402}};
0394   case DW_LANG_C_plus_plus_17:
0395     return {{DW_LNAME_C_plus_plus, 201703}};
0396   case DW_LANG_C_plus_plus_20:
0397     return {{DW_LNAME_C_plus_plus, 202002}};
0398   case DW_LANG_Cobol74:
0399     return {{DW_LNAME_Cobol, 1974}};
0400   case DW_LANG_Cobol85:
0401     return {{DW_LNAME_Cobol, 1985}};
0402   case DW_LANG_Crystal:
0403     return {{DW_LNAME_Crystal, 0}};
0404   case DW_LANG_D:
0405     return {{DW_LNAME_D, 0}};
0406   case DW_LANG_Dylan:
0407     return {{DW_LNAME_Dylan, 0}};
0408   case DW_LANG_Fortran77:
0409     return {{DW_LNAME_Fortran, 1977}};
0410   case DW_LANG_Fortran90:
0411     return {{DW_LNAME_Fortran, 1990}};
0412   case DW_LANG_Fortran95:
0413     return {{DW_LNAME_Fortran, 1995}};
0414   case DW_LANG_Fortran03:
0415     return {{DW_LNAME_Fortran, 2003}};
0416   case DW_LANG_Fortran08:
0417     return {{DW_LNAME_Fortran, 2008}};
0418   case DW_LANG_Fortran18:
0419     return {{DW_LNAME_Fortran, 2018}};
0420   case DW_LANG_Go:
0421     return {{DW_LNAME_Go, 0}};
0422   case DW_LANG_Haskell:
0423     return {{DW_LNAME_Haskell, 0}};
0424   case DW_LANG_HIP:
0425     return {}; // return {{DW_LNAME_HIP, 0}};
0426   case DW_LANG_Java:
0427     return {{DW_LNAME_Java, 0}};
0428   case DW_LANG_Julia:
0429     return {{DW_LNAME_Julia, 0}};
0430   case DW_LANG_Kotlin:
0431     return {{DW_LNAME_Kotlin, 0}};
0432   case DW_LANG_Modula2:
0433     return {{DW_LNAME_Modula2, 0}};
0434   case DW_LANG_Modula3:
0435     return {{DW_LNAME_Modula3, 0}};
0436   case DW_LANG_ObjC:
0437     return {{DW_LNAME_ObjC, 0}};
0438   case DW_LANG_ObjC_plus_plus:
0439     return {{DW_LNAME_ObjC_plus_plus, 0}};
0440   case DW_LANG_OCaml:
0441     return {{DW_LNAME_OCaml, 0}};
0442   case DW_LANG_OpenCL:
0443     return {{DW_LNAME_OpenCL_C, 0}};
0444   case DW_LANG_Pascal83:
0445     return {{DW_LNAME_Pascal, 1983}};
0446   case DW_LANG_PLI:
0447     return {{DW_LNAME_PLI, 0}};
0448   case DW_LANG_Python:
0449     return {{DW_LNAME_Python, 0}};
0450   case DW_LANG_RenderScript:
0451   case DW_LANG_GOOGLE_RenderScript:
0452     return {{DW_LNAME_RenderScript, 0}};
0453   case DW_LANG_Rust:
0454     return {{DW_LNAME_Rust, 0}};
0455   case DW_LANG_Swift:
0456     return {{DW_LNAME_Swift, 0}};
0457   case DW_LANG_UPC:
0458     return {{DW_LNAME_UPC, 0}};
0459   case DW_LANG_Zig:
0460     return {{DW_LNAME_Zig, 0}};
0461   case DW_LANG_Assembly:
0462   case DW_LANG_Mips_Assembler:
0463     return {{DW_LNAME_Assembly, 0}};
0464   case DW_LANG_C_sharp:
0465     return {{DW_LNAME_C_sharp, 0}};
0466   case DW_LANG_Mojo:
0467     return {{DW_LNAME_Mojo, 0}};
0468   case DW_LANG_GLSL:
0469     return {{DW_LNAME_GLSL, 0}};
0470   case DW_LANG_GLSL_ES:
0471     return {{DW_LNAME_GLSL_ES, 0}};
0472   case DW_LANG_HLSL:
0473     return {{DW_LNAME_HLSL, 0}};
0474   case DW_LANG_OpenCL_CPP:
0475     return {{DW_LNAME_OpenCL_CPP, 0}};
0476   case DW_LANG_SYCL:
0477     return {{DW_LNAME_SYCL, 0}};
0478   case DW_LANG_Ruby:
0479     return {{DW_LNAME_Ruby, 0}};
0480   case DW_LANG_Move:
0481     return {{DW_LNAME_Move, 0}};
0482   case DW_LANG_Hylo:
0483     return {{DW_LNAME_Hylo, 0}};
0484   case DW_LANG_Metal:
0485     return {{DW_LNAME_Metal, 0}};
0486   case DW_LANG_BORLAND_Delphi:
0487   case DW_LANG_CPP_for_OpenCL:
0488   case DW_LANG_lo_user:
0489   case DW_LANG_hi_user:
0490     return {};
0491   }
0492   return {};
0493 }
0494 
0495 llvm::StringRef LanguageDescription(SourceLanguageName name);
0496 
0497 inline bool isCPlusPlus(SourceLanguage S) {
0498   bool result = false;
0499   // Deliberately enumerate all the language options so we get a warning when
0500   // new language options are added (-Wswitch) that'll hopefully help keep this
0501   // switch up-to-date when new C++ versions are added.
0502   switch (S) {
0503   case DW_LANG_C_plus_plus:
0504   case DW_LANG_C_plus_plus_03:
0505   case DW_LANG_C_plus_plus_11:
0506   case DW_LANG_C_plus_plus_14:
0507   case DW_LANG_C_plus_plus_17:
0508   case DW_LANG_C_plus_plus_20:
0509     result = true;
0510     break;
0511   case DW_LANG_C89:
0512   case DW_LANG_C:
0513   case DW_LANG_Ada83:
0514   case DW_LANG_Cobol74:
0515   case DW_LANG_Cobol85:
0516   case DW_LANG_Fortran77:
0517   case DW_LANG_Fortran90:
0518   case DW_LANG_Pascal83:
0519   case DW_LANG_Modula2:
0520   case DW_LANG_Java:
0521   case DW_LANG_C99:
0522   case DW_LANG_Ada95:
0523   case DW_LANG_Fortran95:
0524   case DW_LANG_PLI:
0525   case DW_LANG_ObjC:
0526   case DW_LANG_ObjC_plus_plus:
0527   case DW_LANG_UPC:
0528   case DW_LANG_D:
0529   case DW_LANG_Python:
0530   case DW_LANG_OpenCL:
0531   case DW_LANG_Go:
0532   case DW_LANG_Modula3:
0533   case DW_LANG_Haskell:
0534   case DW_LANG_OCaml:
0535   case DW_LANG_Rust:
0536   case DW_LANG_C11:
0537   case DW_LANG_Swift:
0538   case DW_LANG_Julia:
0539   case DW_LANG_Dylan:
0540   case DW_LANG_Fortran03:
0541   case DW_LANG_Fortran08:
0542   case DW_LANG_RenderScript:
0543   case DW_LANG_BLISS:
0544   case DW_LANG_Mips_Assembler:
0545   case DW_LANG_GOOGLE_RenderScript:
0546   case DW_LANG_BORLAND_Delphi:
0547   case DW_LANG_lo_user:
0548   case DW_LANG_hi_user:
0549   case DW_LANG_Kotlin:
0550   case DW_LANG_Zig:
0551   case DW_LANG_Crystal:
0552   case DW_LANG_C17:
0553   case DW_LANG_Fortran18:
0554   case DW_LANG_Ada2005:
0555   case DW_LANG_Ada2012:
0556   case DW_LANG_HIP:
0557   case DW_LANG_Assembly:
0558   case DW_LANG_C_sharp:
0559   case DW_LANG_Mojo:
0560   case DW_LANG_GLSL:
0561   case DW_LANG_GLSL_ES:
0562   case DW_LANG_HLSL:
0563   case DW_LANG_OpenCL_CPP:
0564   case DW_LANG_CPP_for_OpenCL:
0565   case DW_LANG_SYCL:
0566   case DW_LANG_Ruby:
0567   case DW_LANG_Move:
0568   case DW_LANG_Hylo:
0569   case DW_LANG_Metal:
0570     result = false;
0571     break;
0572   }
0573 
0574   return result;
0575 }
0576 
0577 inline bool isFortran(SourceLanguage S) {
0578   bool result = false;
0579   // Deliberately enumerate all the language options so we get a warning when
0580   // new language options are added (-Wswitch) that'll hopefully help keep this
0581   // switch up-to-date when new Fortran versions are added.
0582   switch (S) {
0583   case DW_LANG_Fortran77:
0584   case DW_LANG_Fortran90:
0585   case DW_LANG_Fortran95:
0586   case DW_LANG_Fortran03:
0587   case DW_LANG_Fortran08:
0588   case DW_LANG_Fortran18:
0589     result = true;
0590     break;
0591   case DW_LANG_C89:
0592   case DW_LANG_C:
0593   case DW_LANG_Ada83:
0594   case DW_LANG_C_plus_plus:
0595   case DW_LANG_Cobol74:
0596   case DW_LANG_Cobol85:
0597   case DW_LANG_Pascal83:
0598   case DW_LANG_Modula2:
0599   case DW_LANG_Java:
0600   case DW_LANG_C99:
0601   case DW_LANG_Ada95:
0602   case DW_LANG_PLI:
0603   case DW_LANG_ObjC:
0604   case DW_LANG_ObjC_plus_plus:
0605   case DW_LANG_UPC:
0606   case DW_LANG_D:
0607   case DW_LANG_Python:
0608   case DW_LANG_OpenCL:
0609   case DW_LANG_Go:
0610   case DW_LANG_Modula3:
0611   case DW_LANG_Haskell:
0612   case DW_LANG_C_plus_plus_03:
0613   case DW_LANG_C_plus_plus_11:
0614   case DW_LANG_OCaml:
0615   case DW_LANG_Rust:
0616   case DW_LANG_C11:
0617   case DW_LANG_Swift:
0618   case DW_LANG_Julia:
0619   case DW_LANG_Dylan:
0620   case DW_LANG_C_plus_plus_14:
0621   case DW_LANG_RenderScript:
0622   case DW_LANG_BLISS:
0623   case DW_LANG_Mips_Assembler:
0624   case DW_LANG_GOOGLE_RenderScript:
0625   case DW_LANG_BORLAND_Delphi:
0626   case DW_LANG_lo_user:
0627   case DW_LANG_hi_user:
0628   case DW_LANG_Kotlin:
0629   case DW_LANG_Zig:
0630   case DW_LANG_Crystal:
0631   case DW_LANG_C_plus_plus_17:
0632   case DW_LANG_C_plus_plus_20:
0633   case DW_LANG_C17:
0634   case DW_LANG_Ada2005:
0635   case DW_LANG_Ada2012:
0636   case DW_LANG_HIP:
0637   case DW_LANG_Assembly:
0638   case DW_LANG_C_sharp:
0639   case DW_LANG_Mojo:
0640   case DW_LANG_GLSL:
0641   case DW_LANG_GLSL_ES:
0642   case DW_LANG_HLSL:
0643   case DW_LANG_OpenCL_CPP:
0644   case DW_LANG_CPP_for_OpenCL:
0645   case DW_LANG_SYCL:
0646   case DW_LANG_Ruby:
0647   case DW_LANG_Move:
0648   case DW_LANG_Hylo:
0649   case DW_LANG_Metal:
0650     result = false;
0651     break;
0652   }
0653 
0654   return result;
0655 }
0656 
0657 inline bool isC(SourceLanguage S) {
0658   // Deliberately enumerate all the language options so we get a warning when
0659   // new language options are added (-Wswitch) that'll hopefully help keep this
0660   // switch up-to-date when new C++ versions are added.
0661   switch (S) {
0662   case DW_LANG_C11:
0663   case DW_LANG_C17:
0664   case DW_LANG_C89:
0665   case DW_LANG_C99:
0666   case DW_LANG_C:
0667   case DW_LANG_ObjC:
0668     return true;
0669   case DW_LANG_C_plus_plus:
0670   case DW_LANG_C_plus_plus_03:
0671   case DW_LANG_C_plus_plus_11:
0672   case DW_LANG_C_plus_plus_14:
0673   case DW_LANG_C_plus_plus_17:
0674   case DW_LANG_C_plus_plus_20:
0675   case DW_LANG_Ada83:
0676   case DW_LANG_Cobol74:
0677   case DW_LANG_Cobol85:
0678   case DW_LANG_Fortran77:
0679   case DW_LANG_Fortran90:
0680   case DW_LANG_Pascal83:
0681   case DW_LANG_Modula2:
0682   case DW_LANG_Java:
0683   case DW_LANG_Ada95:
0684   case DW_LANG_Fortran95:
0685   case DW_LANG_PLI:
0686   case DW_LANG_ObjC_plus_plus:
0687   case DW_LANG_UPC:
0688   case DW_LANG_D:
0689   case DW_LANG_Python:
0690   case DW_LANG_OpenCL:
0691   case DW_LANG_Go:
0692   case DW_LANG_Modula3:
0693   case DW_LANG_Haskell:
0694   case DW_LANG_OCaml:
0695   case DW_LANG_Rust:
0696   case DW_LANG_Swift:
0697   case DW_LANG_Julia:
0698   case DW_LANG_Dylan:
0699   case DW_LANG_Fortran03:
0700   case DW_LANG_Fortran08:
0701   case DW_LANG_RenderScript:
0702   case DW_LANG_BLISS:
0703   case DW_LANG_Mips_Assembler:
0704   case DW_LANG_GOOGLE_RenderScript:
0705   case DW_LANG_BORLAND_Delphi:
0706   case DW_LANG_lo_user:
0707   case DW_LANG_hi_user:
0708   case DW_LANG_Kotlin:
0709   case DW_LANG_Zig:
0710   case DW_LANG_Crystal:
0711   case DW_LANG_Fortran18:
0712   case DW_LANG_Ada2005:
0713   case DW_LANG_Ada2012:
0714   case DW_LANG_HIP:
0715   case DW_LANG_Assembly:
0716   case DW_LANG_C_sharp:
0717   case DW_LANG_Mojo:
0718   case DW_LANG_GLSL:
0719   case DW_LANG_GLSL_ES:
0720   case DW_LANG_HLSL:
0721   case DW_LANG_OpenCL_CPP:
0722   case DW_LANG_CPP_for_OpenCL:
0723   case DW_LANG_SYCL:
0724   case DW_LANG_Ruby:
0725   case DW_LANG_Move:
0726   case DW_LANG_Hylo:
0727   case DW_LANG_Metal:
0728     return false;
0729   }
0730   llvm_unreachable("Unknown language kind.");
0731 }
0732 
0733 inline TypeKind getArrayIndexTypeEncoding(SourceLanguage S) {
0734   return isFortran(S) ? DW_ATE_signed : DW_ATE_unsigned;
0735 }
0736 
0737 enum CaseSensitivity {
0738   // Identifier case codes
0739   DW_ID_case_sensitive = 0x00,
0740   DW_ID_up_case = 0x01,
0741   DW_ID_down_case = 0x02,
0742   DW_ID_case_insensitive = 0x03
0743 };
0744 
0745 enum CallingConvention {
0746 // Calling convention codes
0747 #define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
0748 #include "llvm/BinaryFormat/Dwarf.def"
0749   DW_CC_lo_user = 0x40,
0750   DW_CC_hi_user = 0xff
0751 };
0752 
0753 enum InlineAttribute {
0754   // Inline codes
0755   DW_INL_not_inlined = 0x00,
0756   DW_INL_inlined = 0x01,
0757   DW_INL_declared_not_inlined = 0x02,
0758   DW_INL_declared_inlined = 0x03
0759 };
0760 
0761 enum ArrayDimensionOrdering {
0762   // Array ordering
0763   DW_ORD_row_major = 0x00,
0764   DW_ORD_col_major = 0x01
0765 };
0766 
0767 enum DiscriminantList {
0768   // Discriminant descriptor values
0769   DW_DSC_label = 0x00,
0770   DW_DSC_range = 0x01
0771 };
0772 
0773 /// Line Number Standard Opcode Encodings.
0774 enum LineNumberOps : uint8_t {
0775 #define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
0776 #include "llvm/BinaryFormat/Dwarf.def"
0777 };
0778 
0779 /// Line Number Extended Opcode Encodings.
0780 enum LineNumberExtendedOps {
0781 #define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
0782 #include "llvm/BinaryFormat/Dwarf.def"
0783   DW_LNE_lo_user = 0x80,
0784   DW_LNE_hi_user = 0xff
0785 };
0786 
0787 enum LineNumberEntryFormat {
0788 #define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
0789 #include "llvm/BinaryFormat/Dwarf.def"
0790   DW_LNCT_lo_user = 0x2000,
0791   DW_LNCT_hi_user = 0x3fff,
0792 };
0793 
0794 enum MacinfoRecordType {
0795   // Macinfo Type Encodings
0796   DW_MACINFO_define = 0x01,
0797   DW_MACINFO_undef = 0x02,
0798   DW_MACINFO_start_file = 0x03,
0799   DW_MACINFO_end_file = 0x04,
0800   DW_MACINFO_vendor_ext = 0xff
0801 };
0802 
0803 /// DWARF v5 macro information entry type encodings.
0804 enum MacroEntryType {
0805 #define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
0806 #include "llvm/BinaryFormat/Dwarf.def"
0807   DW_MACRO_lo_user = 0xe0,
0808   DW_MACRO_hi_user = 0xff
0809 };
0810 
0811 /// GNU .debug_macro macro information entry type encodings.
0812 enum GnuMacroEntryType {
0813 #define HANDLE_DW_MACRO_GNU(ID, NAME) DW_MACRO_GNU_##NAME = ID,
0814 #include "llvm/BinaryFormat/Dwarf.def"
0815   DW_MACRO_GNU_lo_user = 0xe0,
0816   DW_MACRO_GNU_hi_user = 0xff
0817 };
0818 
0819 /// DWARF v5 range list entry encoding values.
0820 enum RnglistEntries {
0821 #define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
0822 #include "llvm/BinaryFormat/Dwarf.def"
0823 };
0824 
0825 /// DWARF v5 loc list entry encoding values.
0826 enum LoclistEntries {
0827 #define HANDLE_DW_LLE(ID, NAME) DW_LLE_##NAME = ID,
0828 #include "llvm/BinaryFormat/Dwarf.def"
0829 };
0830 
0831 /// Call frame instruction encodings.
0832 enum CallFrameInfo {
0833 #define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
0834 #define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
0835 #include "llvm/BinaryFormat/Dwarf.def"
0836   DW_CFA_extended = 0x00,
0837 
0838   DW_CFA_lo_user = 0x1c,
0839   DW_CFA_hi_user = 0x3f
0840 };
0841 
0842 enum Constants {
0843   // Children flag
0844   DW_CHILDREN_no = 0x00,
0845   DW_CHILDREN_yes = 0x01,
0846 
0847   DW_EH_PE_absptr = 0x00,
0848   DW_EH_PE_omit = 0xff,
0849   DW_EH_PE_uleb128 = 0x01,
0850   DW_EH_PE_udata2 = 0x02,
0851   DW_EH_PE_udata4 = 0x03,
0852   DW_EH_PE_udata8 = 0x04,
0853   DW_EH_PE_sleb128 = 0x09,
0854   DW_EH_PE_sdata2 = 0x0A,
0855   DW_EH_PE_sdata4 = 0x0B,
0856   DW_EH_PE_sdata8 = 0x0C,
0857   DW_EH_PE_signed = 0x08,
0858   DW_EH_PE_pcrel = 0x10,
0859   DW_EH_PE_textrel = 0x20,
0860   DW_EH_PE_datarel = 0x30,
0861   DW_EH_PE_funcrel = 0x40,
0862   DW_EH_PE_aligned = 0x50,
0863   DW_EH_PE_indirect = 0x80
0864 };
0865 
0866 /// Constants for the DW_APPLE_PROPERTY_attributes attribute.
0867 /// Keep this list in sync with clang's DeclObjCCommon.h
0868 /// ObjCPropertyAttribute::Kind!
0869 enum ApplePropertyAttributes {
0870 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
0871 #include "llvm/BinaryFormat/Dwarf.def"
0872 };
0873 
0874 /// Constants for unit types in DWARF v5.
0875 enum UnitType : unsigned char {
0876 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
0877 #include "llvm/BinaryFormat/Dwarf.def"
0878   DW_UT_lo_user = 0x80,
0879   DW_UT_hi_user = 0xff
0880 };
0881 
0882 enum Index {
0883 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
0884 #include "llvm/BinaryFormat/Dwarf.def"
0885   DW_IDX_lo_user = 0x2000,
0886   DW_IDX_hi_user = 0x3fff
0887 };
0888 
0889 inline bool isUnitType(uint8_t UnitType) {
0890   switch (UnitType) {
0891   case DW_UT_compile:
0892   case DW_UT_type:
0893   case DW_UT_partial:
0894   case DW_UT_skeleton:
0895   case DW_UT_split_compile:
0896   case DW_UT_split_type:
0897     return true;
0898   default:
0899     return false;
0900   }
0901 }
0902 
0903 inline bool isUnitType(dwarf::Tag T) {
0904   switch (T) {
0905   case DW_TAG_compile_unit:
0906   case DW_TAG_type_unit:
0907   case DW_TAG_partial_unit:
0908   case DW_TAG_skeleton_unit:
0909     return true;
0910   default:
0911     return false;
0912   }
0913 }
0914 
0915 // Constants for the DWARF v5 Accelerator Table Proposal
0916 enum AcceleratorTable {
0917   // Data layout descriptors.
0918   DW_ATOM_null = 0u,       ///  Marker as the end of a list of atoms.
0919   DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
0920   DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
0921                           // item in question.
0922   DW_ATOM_die_tag = 3u,   // A tag entry.
0923   DW_ATOM_type_flags = 4u, // Set of flags for a type.
0924 
0925   DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
0926   DW_ATOM_qual_name_hash = 6u,  // Dsymutil qualified hash extension.
0927 
0928   // DW_ATOM_type_flags values.
0929 
0930   // Always set for C++, only set for ObjC if this is the @implementation for a
0931   // class.
0932   DW_FLAG_type_implementation = 2u,
0933 
0934   // Hash functions.
0935 
0936   // Daniel J. Bernstein hash.
0937   DW_hash_function_djb = 0u
0938 };
0939 
0940 // Return a suggested bucket count for the DWARF v5 Accelerator Table.
0941 inline uint32_t getDebugNamesBucketCount(uint32_t UniqueHashCount) {
0942   if (UniqueHashCount > 1024)
0943     return UniqueHashCount / 4;
0944   if (UniqueHashCount > 16)
0945     return UniqueHashCount / 2;
0946   return std::max<uint32_t>(UniqueHashCount, 1);
0947 }
0948 
0949 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
0950 enum GDBIndexEntryKind {
0951   GIEK_NONE,
0952   GIEK_TYPE,
0953   GIEK_VARIABLE,
0954   GIEK_FUNCTION,
0955   GIEK_OTHER,
0956   GIEK_UNUSED5,
0957   GIEK_UNUSED6,
0958   GIEK_UNUSED7
0959 };
0960 
0961 enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
0962 
0963 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
0964 ///
0965 /// All these functions map their argument's value back to the
0966 /// corresponding enumerator name or return an empty StringRef if the value
0967 /// isn't known.
0968 ///
0969 /// @{
0970 StringRef TagString(unsigned Tag);
0971 StringRef ChildrenString(unsigned Children);
0972 StringRef AttributeString(unsigned Attribute);
0973 StringRef FormEncodingString(unsigned Encoding);
0974 StringRef OperationEncodingString(unsigned Encoding);
0975 StringRef SubOperationEncodingString(unsigned OpEncoding,
0976                                      unsigned SubOpEncoding);
0977 StringRef AttributeEncodingString(unsigned Encoding);
0978 StringRef DecimalSignString(unsigned Sign);
0979 StringRef EndianityString(unsigned Endian);
0980 StringRef AccessibilityString(unsigned Access);
0981 StringRef DefaultedMemberString(unsigned DefaultedEncodings);
0982 StringRef VisibilityString(unsigned Visibility);
0983 StringRef VirtualityString(unsigned Virtuality);
0984 StringRef LanguageString(unsigned Language);
0985 StringRef CaseString(unsigned Case);
0986 StringRef ConventionString(unsigned Convention);
0987 StringRef InlineCodeString(unsigned Code);
0988 StringRef ArrayOrderString(unsigned Order);
0989 StringRef LNStandardString(unsigned Standard);
0990 StringRef LNExtendedString(unsigned Encoding);
0991 StringRef MacinfoString(unsigned Encoding);
0992 StringRef MacroString(unsigned Encoding);
0993 StringRef GnuMacroString(unsigned Encoding);
0994 StringRef RangeListEncodingString(unsigned Encoding);
0995 StringRef LocListEncodingString(unsigned Encoding);
0996 StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
0997 StringRef ApplePropertyString(unsigned);
0998 StringRef UnitTypeString(unsigned);
0999 StringRef AtomTypeString(unsigned Atom);
1000 StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
1001 StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
1002 StringRef IndexString(unsigned Idx);
1003 StringRef FormatString(DwarfFormat Format);
1004 StringRef FormatString(bool IsDWARF64);
1005 StringRef RLEString(unsigned RLE);
1006 /// @}
1007 
1008 /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
1009 ///
1010 /// These functions map their strings back to the corresponding enumeration
1011 /// value or return 0 if there is none, except for these exceptions:
1012 ///
1013 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
1014 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
1015 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
1016 ///
1017 /// @{
1018 unsigned getTag(StringRef TagString);
1019 unsigned getOperationEncoding(StringRef OperationEncodingString);
1020 unsigned getSubOperationEncoding(unsigned OpEncoding,
1021                                  StringRef SubOperationEncodingString);
1022 unsigned getVirtuality(StringRef VirtualityString);
1023 unsigned getLanguage(StringRef LanguageString);
1024 unsigned getCallingConvention(StringRef LanguageString);
1025 unsigned getAttributeEncoding(StringRef EncodingString);
1026 unsigned getMacinfo(StringRef MacinfoString);
1027 unsigned getMacro(StringRef MacroString);
1028 /// @}
1029 
1030 /// \defgroup DwarfConstantsVersioning Dwarf version for constants
1031 ///
1032 /// For constants defined by DWARF, returns the DWARF version when the constant
1033 /// was first defined. For vendor extensions, if there is a version-related
1034 /// policy for when to emit it, returns a version number for that policy.
1035 /// Otherwise returns 0.
1036 ///
1037 /// @{
1038 unsigned TagVersion(Tag T);
1039 unsigned AttributeVersion(Attribute A);
1040 unsigned FormVersion(Form F);
1041 unsigned OperationVersion(LocationAtom O);
1042 unsigned AttributeEncodingVersion(TypeKind E);
1043 unsigned LanguageVersion(SourceLanguage L);
1044 /// @}
1045 
1046 /// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
1047 ///
1048 /// These functions return an identifier describing "who" defined the constant,
1049 /// either the DWARF standard itself or the vendor who defined the extension.
1050 ///
1051 /// @{
1052 unsigned TagVendor(Tag T);
1053 unsigned AttributeVendor(Attribute A);
1054 unsigned FormVendor(Form F);
1055 unsigned OperationVendor(LocationAtom O);
1056 unsigned AttributeEncodingVendor(TypeKind E);
1057 unsigned LanguageVendor(SourceLanguage L);
1058 /// @}
1059 
1060 /// The number of operands for the given LocationAtom.
1061 std::optional<unsigned> OperationOperands(LocationAtom O);
1062 
1063 /// The arity of the given LocationAtom. This is the number of elements on the
1064 /// stack this operation operates on. Returns -1 if the arity is variable (e.g.
1065 /// depending on the argument) or unknown.
1066 std::optional<unsigned> OperationArity(LocationAtom O);
1067 
1068 std::optional<unsigned> LanguageLowerBound(SourceLanguage L);
1069 
1070 /// The size of a reference determined by the DWARF 32/64-bit format.
1071 inline uint8_t getDwarfOffsetByteSize(DwarfFormat Format) {
1072   switch (Format) {
1073   case DwarfFormat::DWARF32:
1074     return 4;
1075   case DwarfFormat::DWARF64:
1076     return 8;
1077   }
1078   llvm_unreachable("Invalid Format value");
1079 }
1080 
1081 /// A helper struct providing information about the byte size of DW_FORM
1082 /// values that vary in size depending on the DWARF version, address byte
1083 /// size, or DWARF32/DWARF64.
1084 struct FormParams {
1085   uint16_t Version;
1086   uint8_t AddrSize;
1087   DwarfFormat Format;
1088   /// True if DWARF v2 output generally uses relocations for references
1089   /// to other .debug_* sections.
1090   bool DwarfUsesRelocationsAcrossSections = false;
1091 
1092   /// The definition of the size of form DW_FORM_ref_addr depends on the
1093   /// version. In DWARF v2 it's the size of an address; after that, it's the
1094   /// size of a reference.
1095   uint8_t getRefAddrByteSize() const {
1096     if (Version == 2)
1097       return AddrSize;
1098     return getDwarfOffsetByteSize();
1099   }
1100 
1101   /// The size of a reference is determined by the DWARF 32/64-bit format.
1102   uint8_t getDwarfOffsetByteSize() const {
1103     return dwarf::getDwarfOffsetByteSize(Format);
1104   }
1105 
1106   explicit operator bool() const { return Version && AddrSize; }
1107 };
1108 
1109 /// Get the byte size of the unit length field depending on the DWARF format.
1110 inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format) {
1111   switch (Format) {
1112   case DwarfFormat::DWARF32:
1113     return 4;
1114   case DwarfFormat::DWARF64:
1115     return 12;
1116   }
1117   llvm_unreachable("Invalid Format value");
1118 }
1119 
1120 /// Get the fixed byte size for a given form.
1121 ///
1122 /// If the form has a fixed byte size, then an Optional with a value will be
1123 /// returned. If the form is always encoded using a variable length storage
1124 /// format (ULEB or SLEB numbers or blocks) then std::nullopt will be returned.
1125 ///
1126 /// \param Form DWARF form to get the fixed byte size for.
1127 /// \param Params DWARF parameters to help interpret forms.
1128 /// \returns std::optional<uint8_t> value with the fixed byte size or
1129 /// std::nullopt if \p Form doesn't have a fixed byte size.
1130 std::optional<uint8_t> getFixedFormByteSize(dwarf::Form Form,
1131                                             FormParams Params);
1132 
1133 /// Tells whether the specified form is defined in the specified version,
1134 /// or is an extension if extensions are allowed.
1135 bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
1136 
1137 /// Returns the symbolic string representing Val when used as a value
1138 /// for attribute Attr.
1139 StringRef AttributeValueString(uint16_t Attr, unsigned Val);
1140 
1141 /// Returns the symbolic string representing Val when used as a value
1142 /// for atom Atom.
1143 StringRef AtomValueString(uint16_t Atom, unsigned Val);
1144 
1145 /// Describes an entry of the various gnu_pub* debug sections.
1146 ///
1147 /// The gnu_pub* kind looks like:
1148 ///
1149 /// 0-3  reserved
1150 /// 4-6  symbol kind
1151 /// 7    0 == global, 1 == static
1152 ///
1153 /// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
1154 /// offset of the cu within the debug_info section stored in those 24 bits.
1155 struct PubIndexEntryDescriptor {
1156   GDBIndexEntryKind Kind;
1157   GDBIndexEntryLinkage Linkage;
1158   PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
1159       : Kind(Kind), Linkage(Linkage) {}
1160   /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
1161       : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
1162   explicit PubIndexEntryDescriptor(uint8_t Value)
1163       : Kind(
1164             static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
1165         Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
1166                                                   LINKAGE_OFFSET)) {}
1167   uint8_t toBits() const {
1168     return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
1169   }
1170 
1171 private:
1172   enum {
1173     KIND_OFFSET = 4,
1174     KIND_MASK = 7 << KIND_OFFSET,
1175     LINKAGE_OFFSET = 7,
1176     LINKAGE_MASK = 1 << LINKAGE_OFFSET
1177   };
1178 };
1179 
1180 template <typename Enum> struct EnumTraits : public std::false_type {};
1181 
1182 template <> struct EnumTraits<Attribute> : public std::true_type {
1183   static constexpr char Type[3] = "AT";
1184   static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
1185 };
1186 
1187 template <> struct EnumTraits<Form> : public std::true_type {
1188   static constexpr char Type[5] = "FORM";
1189   static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
1190 };
1191 
1192 template <> struct EnumTraits<Index> : public std::true_type {
1193   static constexpr char Type[4] = "IDX";
1194   static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
1195 };
1196 
1197 template <> struct EnumTraits<Tag> : public std::true_type {
1198   static constexpr char Type[4] = "TAG";
1199   static constexpr StringRef (*StringFn)(unsigned) = &TagString;
1200 };
1201 
1202 template <> struct EnumTraits<LineNumberOps> : public std::true_type {
1203   static constexpr char Type[4] = "LNS";
1204   static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
1205 };
1206 
1207 template <> struct EnumTraits<LocationAtom> : public std::true_type {
1208   static constexpr char Type[3] = "OP";
1209   static constexpr StringRef (*StringFn)(unsigned) = &OperationEncodingString;
1210 };
1211 
1212 inline uint64_t computeTombstoneAddress(uint8_t AddressByteSize) {
1213   return std::numeric_limits<uint64_t>::max() >> (8 - AddressByteSize) * 8;
1214 }
1215 
1216 } // End of namespace dwarf
1217 
1218 /// Dwarf constants format_provider
1219 ///
1220 /// Specialization of the format_provider template for dwarf enums. Unlike the
1221 /// dumping functions above, these format unknown enumerator values as
1222 /// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
1223 template <typename Enum>
1224 struct format_provider<Enum, std::enable_if_t<dwarf::EnumTraits<Enum>::value>> {
1225   static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
1226     StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
1227     if (Str.empty()) {
1228       OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
1229          << llvm::format("%x", E);
1230     } else
1231       OS << Str;
1232   }
1233 };
1234 } // End of namespace llvm
1235 
1236 #endif