xref: /llvm-project/llvm/include/llvm/BinaryFormat/Dwarf.h (revision 5d9aabbab3bcb83c84d29432f374db5119a4b578)
1 //===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file contains constants used for implementing Dwarf
11 /// debug support.
12 ///
13 /// For details on the Dwarf specfication see the latest DWARF Debugging
14 /// Information Format standard document on http://www.dwarfstd.org. This
15 /// file often includes support for non-released standard features.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_BINARYFORMAT_DWARF_H
20 #define LLVM_BINARYFORMAT_DWARF_H
21 
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormatVariadicDetails.h"
27 #include "llvm/TargetParser/Triple.h"
28 
29 #include <limits>
30 
31 namespace llvm {
32 class StringRef;
33 
34 namespace dwarf {
35 
36 //===----------------------------------------------------------------------===//
37 // DWARF constants as gleaned from the DWARF Debugging Information Format V.5
38 // reference manual http://www.dwarfstd.org/.
39 //
40 
41 // Do not mix the following two enumerations sets.  DW_TAG_invalid changes the
42 // enumeration base type.
43 
44 enum LLVMConstants : uint32_t {
45   /// LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
46   /// \{
47   DW_TAG_invalid = ~0U,        ///< Tag for invalid results.
48   DW_VIRTUALITY_invalid = ~0U, ///< Virtuality for invalid results.
49   DW_MACINFO_invalid = ~0U,    ///< Macinfo type for invalid results.
50   /// \}
51 
52   /// Special values for an initial length field.
53   /// \{
54   DW_LENGTH_lo_reserved = 0xfffffff0, ///< Lower bound of the reserved range.
55   DW_LENGTH_DWARF64 = 0xffffffff,     ///< Indicator of 64-bit DWARF format.
56   DW_LENGTH_hi_reserved = 0xffffffff, ///< Upper bound of the reserved range.
57   /// \}
58 
59   /// Other constants.
60   /// \{
61   DWARF_VERSION = 4,       ///< Default dwarf version we output.
62   DW_PUBTYPES_VERSION = 2, ///< Section version number for .debug_pubtypes.
63   DW_PUBNAMES_VERSION = 2, ///< Section version number for .debug_pubnames.
64   DW_ARANGES_VERSION = 2,  ///< Section version number for .debug_aranges.
65   /// \}
66 
67   /// Identifiers we use to distinguish vendor extensions.
68   /// \{
69   DWARF_VENDOR_DWARF = 0, ///< Defined in v2 or later of the DWARF standard.
70   DWARF_VENDOR_APPLE = 1,
71   DWARF_VENDOR_BORLAND = 2,
72   DWARF_VENDOR_GNU = 3,
73   DWARF_VENDOR_GOOGLE = 4,
74   DWARF_VENDOR_LLVM = 5,
75   DWARF_VENDOR_MIPS = 6,
76   DWARF_VENDOR_WASM = 7,
77   DWARF_VENDOR_ALTIUM,
78   DWARF_VENDOR_COMPAQ,
79   DWARF_VENDOR_GHS,
80   DWARF_VENDOR_GO,
81   DWARF_VENDOR_HP,
82   DWARF_VENDOR_IBM,
83   DWARF_VENDOR_INTEL,
84   DWARF_VENDOR_PGI,
85   DWARF_VENDOR_SUN,
86   DWARF_VENDOR_UPC,
87   ///\}
88 };
89 
90 /// Constants that define the DWARF format as 32 or 64 bit.
91 enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
92 
93 /// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
94 /// Not inside an enum because a 64-bit value is needed.
95 /// @{
96 const uint32_t DW_CIE_ID = UINT32_MAX;
97 const uint64_t DW64_CIE_ID = UINT64_MAX;
98 /// @}
99 
100 /// Identifier of an invalid DIE offset in the .debug_info section.
101 const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
102 
103 enum Tag : uint16_t {
104 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) DW_TAG_##NAME = ID,
105 #include "llvm/BinaryFormat/Dwarf.def"
106   DW_TAG_lo_user = 0x4080,
107   DW_TAG_hi_user = 0xffff,
108   DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
109 };
110 
111 inline bool isType(Tag T) {
112   switch (T) {
113   default:
114     return false;
115 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
116   case DW_TAG_##NAME:                                                          \
117     return (KIND == DW_KIND_TYPE);
118 #include "llvm/BinaryFormat/Dwarf.def"
119   }
120 }
121 
122 /// Attributes.
123 enum Attribute : uint16_t {
124 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
125 #include "llvm/BinaryFormat/Dwarf.def"
126   DW_AT_lo_user = 0x2000,
127   DW_AT_hi_user = 0x3fff,
128 };
129 
130 enum Form : uint16_t {
131 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
132 #include "llvm/BinaryFormat/Dwarf.def"
133   DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
134 };
135 
136 enum LocationAtom {
137 #define HANDLE_DW_OP(ID, NAME, OPERANDS, ARITY, VERSION, VENDOR)               \
138   DW_OP_##NAME = ID,
139 #include "llvm/BinaryFormat/Dwarf.def"
140   DW_OP_lo_user = 0xe0,
141   DW_OP_hi_user = 0xff,
142   DW_OP_LLVM_fragment = 0x1000,          ///< Only used in LLVM metadata.
143   DW_OP_LLVM_convert = 0x1001,           ///< Only used in LLVM metadata.
144   DW_OP_LLVM_tag_offset = 0x1002,        ///< Only used in LLVM metadata.
145   DW_OP_LLVM_entry_value = 0x1003,       ///< Only used in LLVM metadata.
146   DW_OP_LLVM_implicit_pointer = 0x1004,  ///< Only used in LLVM metadata.
147   DW_OP_LLVM_arg = 0x1005,               ///< Only used in LLVM metadata.
148   DW_OP_LLVM_extract_bits_sext = 0x1006, ///< Only used in LLVM metadata.
149   DW_OP_LLVM_extract_bits_zext = 0x1007, ///< Only used in LLVM metadata.
150 };
151 
152 enum LlvmUserLocationAtom {
153 #define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) DW_OP_LLVM_##NAME = ID,
154 #include "llvm/BinaryFormat/Dwarf.def"
155 };
156 
157 enum TypeKind : uint8_t {
158 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
159 #include "llvm/BinaryFormat/Dwarf.def"
160   DW_ATE_lo_user = 0x80,
161   DW_ATE_hi_user = 0xff
162 };
163 
164 enum DecimalSignEncoding {
165   // Decimal sign attribute values
166   DW_DS_unsigned = 0x01,
167   DW_DS_leading_overpunch = 0x02,
168   DW_DS_trailing_overpunch = 0x03,
169   DW_DS_leading_separate = 0x04,
170   DW_DS_trailing_separate = 0x05
171 };
172 
173 enum EndianityEncoding {
174   // Endianity attribute values
175 #define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
176 #include "llvm/BinaryFormat/Dwarf.def"
177   DW_END_lo_user = 0x40,
178   DW_END_hi_user = 0xff
179 };
180 
181 enum AccessAttribute {
182   // Accessibility codes
183   DW_ACCESS_public = 0x01,
184   DW_ACCESS_protected = 0x02,
185   DW_ACCESS_private = 0x03
186 };
187 
188 enum VisibilityAttribute {
189   // Visibility codes
190   DW_VIS_local = 0x01,
191   DW_VIS_exported = 0x02,
192   DW_VIS_qualified = 0x03
193 };
194 
195 enum VirtualityAttribute {
196 #define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
197 #include "llvm/BinaryFormat/Dwarf.def"
198   DW_VIRTUALITY_max = 0x02
199 };
200 
201 enum DefaultedMemberAttribute {
202 #define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
203 #include "llvm/BinaryFormat/Dwarf.def"
204   DW_DEFAULTED_max = 0x02
205 };
206 
207 enum SourceLanguage {
208 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
209   DW_LANG_##NAME = ID,
210 #include "llvm/BinaryFormat/Dwarf.def"
211   DW_LANG_lo_user = 0x8000,
212   DW_LANG_hi_user = 0xffff
213 };
214 
215 enum SourceLanguageName : uint16_t {
216 #define HANDLE_DW_LNAME(ID, NAME, DESC, LOWER_BOUND) DW_LNAME_##NAME = ID,
217 #include "llvm/BinaryFormat/Dwarf.def"
218 };
219 
220 /// Convert a DWARF 6 pair of language name and version to a DWARF 5 DW_LANG.
221 /// If the version number doesn't exactly match a known version it is
222 /// rounded up to the next-highest known version number.
223 inline std::optional<SourceLanguage> toDW_LANG(SourceLanguageName name,
224                                                uint32_t version) {
225   switch (name) {
226   case DW_LNAME_Ada: // YYYY
227     if (version <= 1983)
228       return DW_LANG_Ada83;
229     if (version <= 1995)
230       return DW_LANG_Ada95;
231     if (version <= 2005)
232       return DW_LANG_Ada2005;
233     if (version <= 2012)
234       return DW_LANG_Ada2012;
235     return {};
236   case DW_LNAME_BLISS:
237     return DW_LANG_BLISS;
238   case DW_LNAME_C: // YYYYMM, K&R 000000
239     if (version == 0)
240       return DW_LANG_C;
241     if (version <= 198912)
242       return DW_LANG_C89;
243     if (version <= 199901)
244       return DW_LANG_C99;
245     if (version <= 201112)
246       return DW_LANG_C11;
247     if (version <= 201710)
248       return DW_LANG_C17;
249     return {};
250   case DW_LNAME_C_plus_plus: // YYYYMM
251     if (version == 0)
252       return DW_LANG_C_plus_plus;
253     if (version <= 199711)
254       return DW_LANG_C_plus_plus;
255     if (version <= 200310)
256       return DW_LANG_C_plus_plus_03;
257     if (version <= 201103)
258       return DW_LANG_C_plus_plus_11;
259     if (version <= 201402)
260       return DW_LANG_C_plus_plus_14;
261     if (version <= 201703)
262       return DW_LANG_C_plus_plus_17;
263     if (version <= 202002)
264       return DW_LANG_C_plus_plus_20;
265     return {};
266   case DW_LNAME_Cobol: // YYYY
267     if (version <= 1974)
268       return DW_LANG_Cobol74;
269     if (version <= 1985)
270       return DW_LANG_Cobol85;
271     return {};
272   case DW_LNAME_Crystal:
273     return DW_LANG_Crystal;
274   case DW_LNAME_D:
275     return DW_LANG_D;
276   case DW_LNAME_Dylan:
277     return DW_LANG_Dylan;
278   case DW_LNAME_Fortran: // YYYY
279     if (version <= 1977)
280       return DW_LANG_Fortran77;
281     if (version <= 1990)
282       return DW_LANG_Fortran90;
283     if (version <= 1995)
284       return DW_LANG_Fortran95;
285     if (version <= 2003)
286       return DW_LANG_Fortran03;
287     if (version <= 2008)
288       return DW_LANG_Fortran08;
289     if (version <= 2018)
290       return DW_LANG_Fortran18;
291     return {};
292   case DW_LNAME_Go:
293     return DW_LANG_Go;
294   case DW_LNAME_Haskell:
295     return DW_LANG_Haskell;
296   // case DW_LNAME_HIP:
297   //   return DW_LANG_HIP;
298   case DW_LNAME_Java:
299     return DW_LANG_Java;
300   case DW_LNAME_Julia:
301     return DW_LANG_Julia;
302   case DW_LNAME_Kotlin:
303     return DW_LANG_Kotlin;
304   case DW_LNAME_Modula2:
305     return DW_LANG_Modula2;
306   case DW_LNAME_Modula3:
307     return DW_LANG_Modula3;
308   case DW_LNAME_ObjC:
309     return DW_LANG_ObjC;
310   case DW_LNAME_ObjC_plus_plus:
311     return DW_LANG_ObjC_plus_plus;
312   case DW_LNAME_OCaml:
313     return DW_LANG_OCaml;
314   case DW_LNAME_OpenCL_C:
315     return DW_LANG_OpenCL;
316   case DW_LNAME_Pascal:
317     return DW_LANG_Pascal83;
318   case DW_LNAME_PLI:
319     return DW_LANG_PLI;
320   case DW_LNAME_Python:
321     return DW_LANG_Python;
322   case DW_LNAME_RenderScript:
323     return DW_LANG_RenderScript;
324   case DW_LNAME_Rust:
325     return DW_LANG_Rust;
326   case DW_LNAME_Swift:
327     return DW_LANG_Swift;
328   case DW_LNAME_UPC:
329     return DW_LANG_UPC;
330   case DW_LNAME_Zig:
331     return DW_LANG_Zig;
332   case DW_LNAME_Assembly:
333     return DW_LANG_Assembly;
334   case DW_LNAME_C_sharp:
335     return DW_LANG_C_sharp;
336   case DW_LNAME_Mojo:
337     return DW_LANG_Mojo;
338   case DW_LNAME_GLSL:
339     return DW_LANG_GLSL;
340   case DW_LNAME_GLSL_ES:
341     return DW_LANG_GLSL_ES;
342   case DW_LNAME_HLSL:
343     return DW_LANG_HLSL;
344   case DW_LNAME_OpenCL_CPP:
345     return DW_LANG_OpenCL_CPP;
346   case DW_LNAME_CPP_for_OpenCL:
347     return {};
348   case DW_LNAME_SYCL:
349     return DW_LANG_SYCL;
350   case DW_LNAME_Ruby:
351     return DW_LANG_Ruby;
352   case DW_LNAME_Move:
353     return DW_LANG_Move;
354   case DW_LNAME_Hylo:
355     return DW_LANG_Hylo;
356   case DW_LNAME_Metal:
357     return DW_LANG_Metal;
358   }
359   return {};
360 }
361 
362 /// Convert a DWARF 5 DW_LANG to a DWARF 6 pair of language name and version.
363 inline std::optional<std::pair<SourceLanguageName, uint32_t>>
364 toDW_LNAME(SourceLanguage language) {
365   switch (language) {
366   case DW_LANG_Ada83:
367     return {{DW_LNAME_Ada, 1983}};
368   case DW_LANG_Ada95:
369     return {{DW_LNAME_Ada, 1995}};
370   case DW_LANG_Ada2005:
371     return {{DW_LNAME_Ada, 2005}};
372   case DW_LANG_Ada2012:
373     return {{DW_LNAME_Ada, 2012}};
374   case DW_LANG_BLISS:
375     return {{DW_LNAME_BLISS, 0}};
376   case DW_LANG_C:
377     return {{DW_LNAME_C, 0}};
378   case DW_LANG_C89:
379     return {{DW_LNAME_C, 198912}};
380   case DW_LANG_C99:
381     return {{DW_LNAME_C, 199901}};
382   case DW_LANG_C11:
383     return {{DW_LNAME_C, 201112}};
384   case DW_LANG_C17:
385     return {{DW_LNAME_C, 201712}};
386   case DW_LANG_C_plus_plus:
387     return {{DW_LNAME_C_plus_plus, 0}};
388   case DW_LANG_C_plus_plus_03:
389     return {{DW_LNAME_C_plus_plus, 200310}};
390   case DW_LANG_C_plus_plus_11:
391     return {{DW_LNAME_C_plus_plus, 201103}};
392   case DW_LANG_C_plus_plus_14:
393     return {{DW_LNAME_C_plus_plus, 201402}};
394   case DW_LANG_C_plus_plus_17:
395     return {{DW_LNAME_C_plus_plus, 201703}};
396   case DW_LANG_C_plus_plus_20:
397     return {{DW_LNAME_C_plus_plus, 202002}};
398   case DW_LANG_Cobol74:
399     return {{DW_LNAME_Cobol, 1974}};
400   case DW_LANG_Cobol85:
401     return {{DW_LNAME_Cobol, 1985}};
402   case DW_LANG_Crystal:
403     return {{DW_LNAME_Crystal, 0}};
404   case DW_LANG_D:
405     return {{DW_LNAME_D, 0}};
406   case DW_LANG_Dylan:
407     return {{DW_LNAME_Dylan, 0}};
408   case DW_LANG_Fortran77:
409     return {{DW_LNAME_Fortran, 1977}};
410   case DW_LANG_Fortran90:
411     return {{DW_LNAME_Fortran, 1990}};
412   case DW_LANG_Fortran95:
413     return {{DW_LNAME_Fortran, 1995}};
414   case DW_LANG_Fortran03:
415     return {{DW_LNAME_Fortran, 2003}};
416   case DW_LANG_Fortran08:
417     return {{DW_LNAME_Fortran, 2008}};
418   case DW_LANG_Fortran18:
419     return {{DW_LNAME_Fortran, 2018}};
420   case DW_LANG_Go:
421     return {{DW_LNAME_Go, 0}};
422   case DW_LANG_Haskell:
423     return {{DW_LNAME_Haskell, 0}};
424   case DW_LANG_HIP:
425     return {}; // return {{DW_LNAME_HIP, 0}};
426   case DW_LANG_Java:
427     return {{DW_LNAME_Java, 0}};
428   case DW_LANG_Julia:
429     return {{DW_LNAME_Julia, 0}};
430   case DW_LANG_Kotlin:
431     return {{DW_LNAME_Kotlin, 0}};
432   case DW_LANG_Modula2:
433     return {{DW_LNAME_Modula2, 0}};
434   case DW_LANG_Modula3:
435     return {{DW_LNAME_Modula3, 0}};
436   case DW_LANG_ObjC:
437     return {{DW_LNAME_ObjC, 0}};
438   case DW_LANG_ObjC_plus_plus:
439     return {{DW_LNAME_ObjC_plus_plus, 0}};
440   case DW_LANG_OCaml:
441     return {{DW_LNAME_OCaml, 0}};
442   case DW_LANG_OpenCL:
443     return {{DW_LNAME_OpenCL_C, 0}};
444   case DW_LANG_Pascal83:
445     return {{DW_LNAME_Pascal, 1983}};
446   case DW_LANG_PLI:
447     return {{DW_LNAME_PLI, 0}};
448   case DW_LANG_Python:
449     return {{DW_LNAME_Python, 0}};
450   case DW_LANG_RenderScript:
451   case DW_LANG_GOOGLE_RenderScript:
452     return {{DW_LNAME_RenderScript, 0}};
453   case DW_LANG_Rust:
454     return {{DW_LNAME_Rust, 0}};
455   case DW_LANG_Swift:
456     return {{DW_LNAME_Swift, 0}};
457   case DW_LANG_UPC:
458     return {{DW_LNAME_UPC, 0}};
459   case DW_LANG_Zig:
460     return {{DW_LNAME_Zig, 0}};
461   case DW_LANG_Assembly:
462   case DW_LANG_Mips_Assembler:
463     return {{DW_LNAME_Assembly, 0}};
464   case DW_LANG_C_sharp:
465     return {{DW_LNAME_C_sharp, 0}};
466   case DW_LANG_Mojo:
467     return {{DW_LNAME_Mojo, 0}};
468   case DW_LANG_GLSL:
469     return {{DW_LNAME_GLSL, 0}};
470   case DW_LANG_GLSL_ES:
471     return {{DW_LNAME_GLSL_ES, 0}};
472   case DW_LANG_HLSL:
473     return {{DW_LNAME_HLSL, 0}};
474   case DW_LANG_OpenCL_CPP:
475     return {{DW_LNAME_OpenCL_CPP, 0}};
476   case DW_LANG_SYCL:
477     return {{DW_LNAME_SYCL, 0}};
478   case DW_LANG_Ruby:
479     return {{DW_LNAME_Ruby, 0}};
480   case DW_LANG_Move:
481     return {{DW_LNAME_Move, 0}};
482   case DW_LANG_Hylo:
483     return {{DW_LNAME_Hylo, 0}};
484   case DW_LANG_Metal:
485     return {{DW_LNAME_Metal, 0}};
486   case DW_LANG_BORLAND_Delphi:
487   case DW_LANG_CPP_for_OpenCL:
488   case DW_LANG_lo_user:
489   case DW_LANG_hi_user:
490     return {};
491   }
492   return {};
493 }
494 
495 llvm::StringRef LanguageDescription(SourceLanguageName name);
496 
497 inline bool isCPlusPlus(SourceLanguage S) {
498   bool result = false;
499   // Deliberately enumerate all the language options so we get a warning when
500   // new language options are added (-Wswitch) that'll hopefully help keep this
501   // switch up-to-date when new C++ versions are added.
502   switch (S) {
503   case DW_LANG_C_plus_plus:
504   case DW_LANG_C_plus_plus_03:
505   case DW_LANG_C_plus_plus_11:
506   case DW_LANG_C_plus_plus_14:
507   case DW_LANG_C_plus_plus_17:
508   case DW_LANG_C_plus_plus_20:
509     result = true;
510     break;
511   case DW_LANG_C89:
512   case DW_LANG_C:
513   case DW_LANG_Ada83:
514   case DW_LANG_Cobol74:
515   case DW_LANG_Cobol85:
516   case DW_LANG_Fortran77:
517   case DW_LANG_Fortran90:
518   case DW_LANG_Pascal83:
519   case DW_LANG_Modula2:
520   case DW_LANG_Java:
521   case DW_LANG_C99:
522   case DW_LANG_Ada95:
523   case DW_LANG_Fortran95:
524   case DW_LANG_PLI:
525   case DW_LANG_ObjC:
526   case DW_LANG_ObjC_plus_plus:
527   case DW_LANG_UPC:
528   case DW_LANG_D:
529   case DW_LANG_Python:
530   case DW_LANG_OpenCL:
531   case DW_LANG_Go:
532   case DW_LANG_Modula3:
533   case DW_LANG_Haskell:
534   case DW_LANG_OCaml:
535   case DW_LANG_Rust:
536   case DW_LANG_C11:
537   case DW_LANG_Swift:
538   case DW_LANG_Julia:
539   case DW_LANG_Dylan:
540   case DW_LANG_Fortran03:
541   case DW_LANG_Fortran08:
542   case DW_LANG_RenderScript:
543   case DW_LANG_BLISS:
544   case DW_LANG_Mips_Assembler:
545   case DW_LANG_GOOGLE_RenderScript:
546   case DW_LANG_BORLAND_Delphi:
547   case DW_LANG_lo_user:
548   case DW_LANG_hi_user:
549   case DW_LANG_Kotlin:
550   case DW_LANG_Zig:
551   case DW_LANG_Crystal:
552   case DW_LANG_C17:
553   case DW_LANG_Fortran18:
554   case DW_LANG_Ada2005:
555   case DW_LANG_Ada2012:
556   case DW_LANG_HIP:
557   case DW_LANG_Assembly:
558   case DW_LANG_C_sharp:
559   case DW_LANG_Mojo:
560   case DW_LANG_GLSL:
561   case DW_LANG_GLSL_ES:
562   case DW_LANG_HLSL:
563   case DW_LANG_OpenCL_CPP:
564   case DW_LANG_CPP_for_OpenCL:
565   case DW_LANG_SYCL:
566   case DW_LANG_Ruby:
567   case DW_LANG_Move:
568   case DW_LANG_Hylo:
569   case DW_LANG_Metal:
570     result = false;
571     break;
572   }
573 
574   return result;
575 }
576 
577 inline bool isFortran(SourceLanguage S) {
578   bool result = false;
579   // Deliberately enumerate all the language options so we get a warning when
580   // new language options are added (-Wswitch) that'll hopefully help keep this
581   // switch up-to-date when new Fortran versions are added.
582   switch (S) {
583   case DW_LANG_Fortran77:
584   case DW_LANG_Fortran90:
585   case DW_LANG_Fortran95:
586   case DW_LANG_Fortran03:
587   case DW_LANG_Fortran08:
588   case DW_LANG_Fortran18:
589     result = true;
590     break;
591   case DW_LANG_C89:
592   case DW_LANG_C:
593   case DW_LANG_Ada83:
594   case DW_LANG_C_plus_plus:
595   case DW_LANG_Cobol74:
596   case DW_LANG_Cobol85:
597   case DW_LANG_Pascal83:
598   case DW_LANG_Modula2:
599   case DW_LANG_Java:
600   case DW_LANG_C99:
601   case DW_LANG_Ada95:
602   case DW_LANG_PLI:
603   case DW_LANG_ObjC:
604   case DW_LANG_ObjC_plus_plus:
605   case DW_LANG_UPC:
606   case DW_LANG_D:
607   case DW_LANG_Python:
608   case DW_LANG_OpenCL:
609   case DW_LANG_Go:
610   case DW_LANG_Modula3:
611   case DW_LANG_Haskell:
612   case DW_LANG_C_plus_plus_03:
613   case DW_LANG_C_plus_plus_11:
614   case DW_LANG_OCaml:
615   case DW_LANG_Rust:
616   case DW_LANG_C11:
617   case DW_LANG_Swift:
618   case DW_LANG_Julia:
619   case DW_LANG_Dylan:
620   case DW_LANG_C_plus_plus_14:
621   case DW_LANG_RenderScript:
622   case DW_LANG_BLISS:
623   case DW_LANG_Mips_Assembler:
624   case DW_LANG_GOOGLE_RenderScript:
625   case DW_LANG_BORLAND_Delphi:
626   case DW_LANG_lo_user:
627   case DW_LANG_hi_user:
628   case DW_LANG_Kotlin:
629   case DW_LANG_Zig:
630   case DW_LANG_Crystal:
631   case DW_LANG_C_plus_plus_17:
632   case DW_LANG_C_plus_plus_20:
633   case DW_LANG_C17:
634   case DW_LANG_Ada2005:
635   case DW_LANG_Ada2012:
636   case DW_LANG_HIP:
637   case DW_LANG_Assembly:
638   case DW_LANG_C_sharp:
639   case DW_LANG_Mojo:
640   case DW_LANG_GLSL:
641   case DW_LANG_GLSL_ES:
642   case DW_LANG_HLSL:
643   case DW_LANG_OpenCL_CPP:
644   case DW_LANG_CPP_for_OpenCL:
645   case DW_LANG_SYCL:
646   case DW_LANG_Ruby:
647   case DW_LANG_Move:
648   case DW_LANG_Hylo:
649   case DW_LANG_Metal:
650     result = false;
651     break;
652   }
653 
654   return result;
655 }
656 
657 inline bool isC(SourceLanguage S) {
658   // Deliberately enumerate all the language options so we get a warning when
659   // new language options are added (-Wswitch) that'll hopefully help keep this
660   // switch up-to-date when new C++ versions are added.
661   switch (S) {
662   case DW_LANG_C11:
663   case DW_LANG_C17:
664   case DW_LANG_C89:
665   case DW_LANG_C99:
666   case DW_LANG_C:
667   case DW_LANG_ObjC:
668     return true;
669   case DW_LANG_C_plus_plus:
670   case DW_LANG_C_plus_plus_03:
671   case DW_LANG_C_plus_plus_11:
672   case DW_LANG_C_plus_plus_14:
673   case DW_LANG_C_plus_plus_17:
674   case DW_LANG_C_plus_plus_20:
675   case DW_LANG_Ada83:
676   case DW_LANG_Cobol74:
677   case DW_LANG_Cobol85:
678   case DW_LANG_Fortran77:
679   case DW_LANG_Fortran90:
680   case DW_LANG_Pascal83:
681   case DW_LANG_Modula2:
682   case DW_LANG_Java:
683   case DW_LANG_Ada95:
684   case DW_LANG_Fortran95:
685   case DW_LANG_PLI:
686   case DW_LANG_ObjC_plus_plus:
687   case DW_LANG_UPC:
688   case DW_LANG_D:
689   case DW_LANG_Python:
690   case DW_LANG_OpenCL:
691   case DW_LANG_Go:
692   case DW_LANG_Modula3:
693   case DW_LANG_Haskell:
694   case DW_LANG_OCaml:
695   case DW_LANG_Rust:
696   case DW_LANG_Swift:
697   case DW_LANG_Julia:
698   case DW_LANG_Dylan:
699   case DW_LANG_Fortran03:
700   case DW_LANG_Fortran08:
701   case DW_LANG_RenderScript:
702   case DW_LANG_BLISS:
703   case DW_LANG_Mips_Assembler:
704   case DW_LANG_GOOGLE_RenderScript:
705   case DW_LANG_BORLAND_Delphi:
706   case DW_LANG_lo_user:
707   case DW_LANG_hi_user:
708   case DW_LANG_Kotlin:
709   case DW_LANG_Zig:
710   case DW_LANG_Crystal:
711   case DW_LANG_Fortran18:
712   case DW_LANG_Ada2005:
713   case DW_LANG_Ada2012:
714   case DW_LANG_HIP:
715   case DW_LANG_Assembly:
716   case DW_LANG_C_sharp:
717   case DW_LANG_Mojo:
718   case DW_LANG_GLSL:
719   case DW_LANG_GLSL_ES:
720   case DW_LANG_HLSL:
721   case DW_LANG_OpenCL_CPP:
722   case DW_LANG_CPP_for_OpenCL:
723   case DW_LANG_SYCL:
724   case DW_LANG_Ruby:
725   case DW_LANG_Move:
726   case DW_LANG_Hylo:
727   case DW_LANG_Metal:
728     return false;
729   }
730   llvm_unreachable("Unknown language kind.");
731 }
732 
733 inline TypeKind getArrayIndexTypeEncoding(SourceLanguage S) {
734   return isFortran(S) ? DW_ATE_signed : DW_ATE_unsigned;
735 }
736 
737 enum CaseSensitivity {
738   // Identifier case codes
739   DW_ID_case_sensitive = 0x00,
740   DW_ID_up_case = 0x01,
741   DW_ID_down_case = 0x02,
742   DW_ID_case_insensitive = 0x03
743 };
744 
745 enum CallingConvention {
746 // Calling convention codes
747 #define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
748 #include "llvm/BinaryFormat/Dwarf.def"
749   DW_CC_lo_user = 0x40,
750   DW_CC_hi_user = 0xff
751 };
752 
753 enum InlineAttribute {
754   // Inline codes
755   DW_INL_not_inlined = 0x00,
756   DW_INL_inlined = 0x01,
757   DW_INL_declared_not_inlined = 0x02,
758   DW_INL_declared_inlined = 0x03
759 };
760 
761 enum ArrayDimensionOrdering {
762   // Array ordering
763   DW_ORD_row_major = 0x00,
764   DW_ORD_col_major = 0x01
765 };
766 
767 enum DiscriminantList {
768   // Discriminant descriptor values
769   DW_DSC_label = 0x00,
770   DW_DSC_range = 0x01
771 };
772 
773 /// Line Number Standard Opcode Encodings.
774 enum LineNumberOps : uint8_t {
775 #define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
776 #include "llvm/BinaryFormat/Dwarf.def"
777 };
778 
779 /// Line Number Extended Opcode Encodings.
780 enum LineNumberExtendedOps {
781 #define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
782 #include "llvm/BinaryFormat/Dwarf.def"
783   DW_LNE_lo_user = 0x80,
784   DW_LNE_hi_user = 0xff
785 };
786 
787 enum LineNumberEntryFormat {
788 #define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
789 #include "llvm/BinaryFormat/Dwarf.def"
790   DW_LNCT_lo_user = 0x2000,
791   DW_LNCT_hi_user = 0x3fff,
792 };
793 
794 enum MacinfoRecordType {
795   // Macinfo Type Encodings
796   DW_MACINFO_define = 0x01,
797   DW_MACINFO_undef = 0x02,
798   DW_MACINFO_start_file = 0x03,
799   DW_MACINFO_end_file = 0x04,
800   DW_MACINFO_vendor_ext = 0xff
801 };
802 
803 /// DWARF v5 macro information entry type encodings.
804 enum MacroEntryType {
805 #define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
806 #include "llvm/BinaryFormat/Dwarf.def"
807   DW_MACRO_lo_user = 0xe0,
808   DW_MACRO_hi_user = 0xff
809 };
810 
811 /// GNU .debug_macro macro information entry type encodings.
812 enum GnuMacroEntryType {
813 #define HANDLE_DW_MACRO_GNU(ID, NAME) DW_MACRO_GNU_##NAME = ID,
814 #include "llvm/BinaryFormat/Dwarf.def"
815   DW_MACRO_GNU_lo_user = 0xe0,
816   DW_MACRO_GNU_hi_user = 0xff
817 };
818 
819 /// DWARF v5 range list entry encoding values.
820 enum RnglistEntries {
821 #define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
822 #include "llvm/BinaryFormat/Dwarf.def"
823 };
824 
825 /// DWARF v5 loc list entry encoding values.
826 enum LoclistEntries {
827 #define HANDLE_DW_LLE(ID, NAME) DW_LLE_##NAME = ID,
828 #include "llvm/BinaryFormat/Dwarf.def"
829 };
830 
831 /// Call frame instruction encodings.
832 enum CallFrameInfo {
833 #define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
834 #define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
835 #include "llvm/BinaryFormat/Dwarf.def"
836   DW_CFA_extended = 0x00,
837 
838   DW_CFA_lo_user = 0x1c,
839   DW_CFA_hi_user = 0x3f
840 };
841 
842 enum Constants {
843   // Children flag
844   DW_CHILDREN_no = 0x00,
845   DW_CHILDREN_yes = 0x01,
846 
847   DW_EH_PE_absptr = 0x00,
848   DW_EH_PE_omit = 0xff,
849   DW_EH_PE_uleb128 = 0x01,
850   DW_EH_PE_udata2 = 0x02,
851   DW_EH_PE_udata4 = 0x03,
852   DW_EH_PE_udata8 = 0x04,
853   DW_EH_PE_sleb128 = 0x09,
854   DW_EH_PE_sdata2 = 0x0A,
855   DW_EH_PE_sdata4 = 0x0B,
856   DW_EH_PE_sdata8 = 0x0C,
857   DW_EH_PE_signed = 0x08,
858   DW_EH_PE_pcrel = 0x10,
859   DW_EH_PE_textrel = 0x20,
860   DW_EH_PE_datarel = 0x30,
861   DW_EH_PE_funcrel = 0x40,
862   DW_EH_PE_aligned = 0x50,
863   DW_EH_PE_indirect = 0x80
864 };
865 
866 /// Constants for the DW_APPLE_PROPERTY_attributes attribute.
867 /// Keep this list in sync with clang's DeclObjCCommon.h
868 /// ObjCPropertyAttribute::Kind!
869 enum ApplePropertyAttributes {
870 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
871 #include "llvm/BinaryFormat/Dwarf.def"
872 };
873 
874 /// Constants for unit types in DWARF v5.
875 enum UnitType : unsigned char {
876 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
877 #include "llvm/BinaryFormat/Dwarf.def"
878   DW_UT_lo_user = 0x80,
879   DW_UT_hi_user = 0xff
880 };
881 
882 enum Index {
883 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
884 #include "llvm/BinaryFormat/Dwarf.def"
885   DW_IDX_lo_user = 0x2000,
886   DW_IDX_hi_user = 0x3fff
887 };
888 
889 inline bool isUnitType(uint8_t UnitType) {
890   switch (UnitType) {
891   case DW_UT_compile:
892   case DW_UT_type:
893   case DW_UT_partial:
894   case DW_UT_skeleton:
895   case DW_UT_split_compile:
896   case DW_UT_split_type:
897     return true;
898   default:
899     return false;
900   }
901 }
902 
903 inline bool isUnitType(dwarf::Tag T) {
904   switch (T) {
905   case DW_TAG_compile_unit:
906   case DW_TAG_type_unit:
907   case DW_TAG_partial_unit:
908   case DW_TAG_skeleton_unit:
909     return true;
910   default:
911     return false;
912   }
913 }
914 
915 // Constants for the DWARF v5 Accelerator Table Proposal
916 enum AcceleratorTable {
917   // Data layout descriptors.
918   DW_ATOM_null = 0u,       ///  Marker as the end of a list of atoms.
919   DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
920   DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
921                           // item in question.
922   DW_ATOM_die_tag = 3u,   // A tag entry.
923   DW_ATOM_type_flags = 4u, // Set of flags for a type.
924 
925   DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
926   DW_ATOM_qual_name_hash = 6u,  // Dsymutil qualified hash extension.
927 
928   // DW_ATOM_type_flags values.
929 
930   // Always set for C++, only set for ObjC if this is the @implementation for a
931   // class.
932   DW_FLAG_type_implementation = 2u,
933 
934   // Hash functions.
935 
936   // Daniel J. Bernstein hash.
937   DW_hash_function_djb = 0u
938 };
939 
940 // Return a suggested bucket count for the DWARF v5 Accelerator Table.
941 inline uint32_t getDebugNamesBucketCount(uint32_t UniqueHashCount) {
942   if (UniqueHashCount > 1024)
943     return UniqueHashCount / 4;
944   if (UniqueHashCount > 16)
945     return UniqueHashCount / 2;
946   return std::max<uint32_t>(UniqueHashCount, 1);
947 }
948 
949 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
950 enum GDBIndexEntryKind {
951   GIEK_NONE,
952   GIEK_TYPE,
953   GIEK_VARIABLE,
954   GIEK_FUNCTION,
955   GIEK_OTHER,
956   GIEK_UNUSED5,
957   GIEK_UNUSED6,
958   GIEK_UNUSED7
959 };
960 
961 enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
962 
963 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
964 ///
965 /// All these functions map their argument's value back to the
966 /// corresponding enumerator name or return an empty StringRef if the value
967 /// isn't known.
968 ///
969 /// @{
970 StringRef TagString(unsigned Tag);
971 StringRef ChildrenString(unsigned Children);
972 StringRef AttributeString(unsigned Attribute);
973 StringRef FormEncodingString(unsigned Encoding);
974 StringRef OperationEncodingString(unsigned Encoding);
975 StringRef SubOperationEncodingString(unsigned OpEncoding,
976                                      unsigned SubOpEncoding);
977 StringRef AttributeEncodingString(unsigned Encoding);
978 StringRef DecimalSignString(unsigned Sign);
979 StringRef EndianityString(unsigned Endian);
980 StringRef AccessibilityString(unsigned Access);
981 StringRef DefaultedMemberString(unsigned DefaultedEncodings);
982 StringRef VisibilityString(unsigned Visibility);
983 StringRef VirtualityString(unsigned Virtuality);
984 StringRef LanguageString(unsigned Language);
985 StringRef CaseString(unsigned Case);
986 StringRef ConventionString(unsigned Convention);
987 StringRef InlineCodeString(unsigned Code);
988 StringRef ArrayOrderString(unsigned Order);
989 StringRef LNStandardString(unsigned Standard);
990 StringRef LNExtendedString(unsigned Encoding);
991 StringRef MacinfoString(unsigned Encoding);
992 StringRef MacroString(unsigned Encoding);
993 StringRef GnuMacroString(unsigned Encoding);
994 StringRef RangeListEncodingString(unsigned Encoding);
995 StringRef LocListEncodingString(unsigned Encoding);
996 StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
997 StringRef ApplePropertyString(unsigned);
998 StringRef UnitTypeString(unsigned);
999 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
1237