xref: /freebsd/contrib/llvm-project/llvm/include/llvm/BinaryFormat/Dwarf.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
isType(Tag T)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.
toDW_LANG(SourceLanguageName name,uint32_t version)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   }
357   return {};
358 }
359 
360 /// Convert a DWARF 5 DW_LANG to a DWARF 6 pair of language name and version.
361 inline std::optional<std::pair<SourceLanguageName, uint32_t>>
toDW_LNAME(SourceLanguage language)362 toDW_LNAME(SourceLanguage language) {
363   switch (language) {
364   case DW_LANG_Ada83:
365     return {{DW_LNAME_Ada, 1983}};
366   case DW_LANG_Ada95:
367     return {{DW_LNAME_Ada, 1995}};
368   case DW_LANG_Ada2005:
369     return {{DW_LNAME_Ada, 2005}};
370   case DW_LANG_Ada2012:
371     return {{DW_LNAME_Ada, 2012}};
372   case DW_LANG_BLISS:
373     return {{DW_LNAME_BLISS, 0}};
374   case DW_LANG_C:
375     return {{DW_LNAME_C, 0}};
376   case DW_LANG_C89:
377     return {{DW_LNAME_C, 198912}};
378   case DW_LANG_C99:
379     return {{DW_LNAME_C, 199901}};
380   case DW_LANG_C11:
381     return {{DW_LNAME_C, 201112}};
382   case DW_LANG_C17:
383     return {{DW_LNAME_C, 201712}};
384   case DW_LANG_C_plus_plus:
385     return {{DW_LNAME_C_plus_plus, 0}};
386   case DW_LANG_C_plus_plus_03:
387     return {{DW_LNAME_C_plus_plus, 200310}};
388   case DW_LANG_C_plus_plus_11:
389     return {{DW_LNAME_C_plus_plus, 201103}};
390   case DW_LANG_C_plus_plus_14:
391     return {{DW_LNAME_C_plus_plus, 201402}};
392   case DW_LANG_C_plus_plus_17:
393     return {{DW_LNAME_C_plus_plus, 201703}};
394   case DW_LANG_C_plus_plus_20:
395     return {{DW_LNAME_C_plus_plus, 202002}};
396   case DW_LANG_Cobol74:
397     return {{DW_LNAME_Cobol, 1974}};
398   case DW_LANG_Cobol85:
399     return {{DW_LNAME_Cobol, 1985}};
400   case DW_LANG_Crystal:
401     return {{DW_LNAME_Crystal, 0}};
402   case DW_LANG_D:
403     return {{DW_LNAME_D, 0}};
404   case DW_LANG_Dylan:
405     return {{DW_LNAME_Dylan, 0}};
406   case DW_LANG_Fortran77:
407     return {{DW_LNAME_Fortran, 1977}};
408   case DW_LANG_Fortran90:
409     return {{DW_LNAME_Fortran, 1990}};
410   case DW_LANG_Fortran95:
411     return {{DW_LNAME_Fortran, 1995}};
412   case DW_LANG_Fortran03:
413     return {{DW_LNAME_Fortran, 2003}};
414   case DW_LANG_Fortran08:
415     return {{DW_LNAME_Fortran, 2008}};
416   case DW_LANG_Fortran18:
417     return {{DW_LNAME_Fortran, 2018}};
418   case DW_LANG_Go:
419     return {{DW_LNAME_Go, 0}};
420   case DW_LANG_Haskell:
421     return {{DW_LNAME_Haskell, 0}};
422   case DW_LANG_HIP:
423     return {}; // return {{DW_LNAME_HIP, 0}};
424   case DW_LANG_Java:
425     return {{DW_LNAME_Java, 0}};
426   case DW_LANG_Julia:
427     return {{DW_LNAME_Julia, 0}};
428   case DW_LANG_Kotlin:
429     return {{DW_LNAME_Kotlin, 0}};
430   case DW_LANG_Modula2:
431     return {{DW_LNAME_Modula2, 0}};
432   case DW_LANG_Modula3:
433     return {{DW_LNAME_Modula3, 0}};
434   case DW_LANG_ObjC:
435     return {{DW_LNAME_ObjC, 0}};
436   case DW_LANG_ObjC_plus_plus:
437     return {{DW_LNAME_ObjC_plus_plus, 0}};
438   case DW_LANG_OCaml:
439     return {{DW_LNAME_OCaml, 0}};
440   case DW_LANG_OpenCL:
441     return {{DW_LNAME_OpenCL_C, 0}};
442   case DW_LANG_Pascal83:
443     return {{DW_LNAME_Pascal, 1983}};
444   case DW_LANG_PLI:
445     return {{DW_LNAME_PLI, 0}};
446   case DW_LANG_Python:
447     return {{DW_LNAME_Python, 0}};
448   case DW_LANG_RenderScript:
449   case DW_LANG_GOOGLE_RenderScript:
450     return {{DW_LNAME_RenderScript, 0}};
451   case DW_LANG_Rust:
452     return {{DW_LNAME_Rust, 0}};
453   case DW_LANG_Swift:
454     return {{DW_LNAME_Swift, 0}};
455   case DW_LANG_UPC:
456     return {{DW_LNAME_UPC, 0}};
457   case DW_LANG_Zig:
458     return {{DW_LNAME_Zig, 0}};
459   case DW_LANG_Assembly:
460   case DW_LANG_Mips_Assembler:
461     return {{DW_LNAME_Assembly, 0}};
462   case DW_LANG_C_sharp:
463     return {{DW_LNAME_C_sharp, 0}};
464   case DW_LANG_Mojo:
465     return {{DW_LNAME_Mojo, 0}};
466   case DW_LANG_GLSL:
467     return {{DW_LNAME_GLSL, 0}};
468   case DW_LANG_GLSL_ES:
469     return {{DW_LNAME_GLSL_ES, 0}};
470   case DW_LANG_HLSL:
471     return {{DW_LNAME_HLSL, 0}};
472   case DW_LANG_OpenCL_CPP:
473     return {{DW_LNAME_OpenCL_CPP, 0}};
474   case DW_LANG_SYCL:
475     return {{DW_LNAME_SYCL, 0}};
476   case DW_LANG_Ruby:
477     return {{DW_LNAME_Ruby, 0}};
478   case DW_LANG_Move:
479     return {{DW_LNAME_Move, 0}};
480   case DW_LANG_Hylo:
481     return {{DW_LNAME_Hylo, 0}};
482   case DW_LANG_BORLAND_Delphi:
483   case DW_LANG_CPP_for_OpenCL:
484   case DW_LANG_lo_user:
485   case DW_LANG_hi_user:
486     return {};
487   }
488   return {};
489 }
490 
491 llvm::StringRef LanguageDescription(SourceLanguageName name);
492 
isCPlusPlus(SourceLanguage S)493 inline bool isCPlusPlus(SourceLanguage S) {
494   bool result = false;
495   // Deliberately enumerate all the language options so we get a warning when
496   // new language options are added (-Wswitch) that'll hopefully help keep this
497   // switch up-to-date when new C++ versions are added.
498   switch (S) {
499   case DW_LANG_C_plus_plus:
500   case DW_LANG_C_plus_plus_03:
501   case DW_LANG_C_plus_plus_11:
502   case DW_LANG_C_plus_plus_14:
503   case DW_LANG_C_plus_plus_17:
504   case DW_LANG_C_plus_plus_20:
505     result = true;
506     break;
507   case DW_LANG_C89:
508   case DW_LANG_C:
509   case DW_LANG_Ada83:
510   case DW_LANG_Cobol74:
511   case DW_LANG_Cobol85:
512   case DW_LANG_Fortran77:
513   case DW_LANG_Fortran90:
514   case DW_LANG_Pascal83:
515   case DW_LANG_Modula2:
516   case DW_LANG_Java:
517   case DW_LANG_C99:
518   case DW_LANG_Ada95:
519   case DW_LANG_Fortran95:
520   case DW_LANG_PLI:
521   case DW_LANG_ObjC:
522   case DW_LANG_ObjC_plus_plus:
523   case DW_LANG_UPC:
524   case DW_LANG_D:
525   case DW_LANG_Python:
526   case DW_LANG_OpenCL:
527   case DW_LANG_Go:
528   case DW_LANG_Modula3:
529   case DW_LANG_Haskell:
530   case DW_LANG_OCaml:
531   case DW_LANG_Rust:
532   case DW_LANG_C11:
533   case DW_LANG_Swift:
534   case DW_LANG_Julia:
535   case DW_LANG_Dylan:
536   case DW_LANG_Fortran03:
537   case DW_LANG_Fortran08:
538   case DW_LANG_RenderScript:
539   case DW_LANG_BLISS:
540   case DW_LANG_Mips_Assembler:
541   case DW_LANG_GOOGLE_RenderScript:
542   case DW_LANG_BORLAND_Delphi:
543   case DW_LANG_lo_user:
544   case DW_LANG_hi_user:
545   case DW_LANG_Kotlin:
546   case DW_LANG_Zig:
547   case DW_LANG_Crystal:
548   case DW_LANG_C17:
549   case DW_LANG_Fortran18:
550   case DW_LANG_Ada2005:
551   case DW_LANG_Ada2012:
552   case DW_LANG_HIP:
553   case DW_LANG_Assembly:
554   case DW_LANG_C_sharp:
555   case DW_LANG_Mojo:
556   case DW_LANG_GLSL:
557   case DW_LANG_GLSL_ES:
558   case DW_LANG_HLSL:
559   case DW_LANG_OpenCL_CPP:
560   case DW_LANG_CPP_for_OpenCL:
561   case DW_LANG_SYCL:
562   case DW_LANG_Ruby:
563   case DW_LANG_Move:
564   case DW_LANG_Hylo:
565     result = false;
566     break;
567   }
568 
569   return result;
570 }
571 
isFortran(SourceLanguage S)572 inline bool isFortran(SourceLanguage S) {
573   bool result = false;
574   // Deliberately enumerate all the language options so we get a warning when
575   // new language options are added (-Wswitch) that'll hopefully help keep this
576   // switch up-to-date when new Fortran versions are added.
577   switch (S) {
578   case DW_LANG_Fortran77:
579   case DW_LANG_Fortran90:
580   case DW_LANG_Fortran95:
581   case DW_LANG_Fortran03:
582   case DW_LANG_Fortran08:
583   case DW_LANG_Fortran18:
584     result = true;
585     break;
586   case DW_LANG_C89:
587   case DW_LANG_C:
588   case DW_LANG_Ada83:
589   case DW_LANG_C_plus_plus:
590   case DW_LANG_Cobol74:
591   case DW_LANG_Cobol85:
592   case DW_LANG_Pascal83:
593   case DW_LANG_Modula2:
594   case DW_LANG_Java:
595   case DW_LANG_C99:
596   case DW_LANG_Ada95:
597   case DW_LANG_PLI:
598   case DW_LANG_ObjC:
599   case DW_LANG_ObjC_plus_plus:
600   case DW_LANG_UPC:
601   case DW_LANG_D:
602   case DW_LANG_Python:
603   case DW_LANG_OpenCL:
604   case DW_LANG_Go:
605   case DW_LANG_Modula3:
606   case DW_LANG_Haskell:
607   case DW_LANG_C_plus_plus_03:
608   case DW_LANG_C_plus_plus_11:
609   case DW_LANG_OCaml:
610   case DW_LANG_Rust:
611   case DW_LANG_C11:
612   case DW_LANG_Swift:
613   case DW_LANG_Julia:
614   case DW_LANG_Dylan:
615   case DW_LANG_C_plus_plus_14:
616   case DW_LANG_RenderScript:
617   case DW_LANG_BLISS:
618   case DW_LANG_Mips_Assembler:
619   case DW_LANG_GOOGLE_RenderScript:
620   case DW_LANG_BORLAND_Delphi:
621   case DW_LANG_lo_user:
622   case DW_LANG_hi_user:
623   case DW_LANG_Kotlin:
624   case DW_LANG_Zig:
625   case DW_LANG_Crystal:
626   case DW_LANG_C_plus_plus_17:
627   case DW_LANG_C_plus_plus_20:
628   case DW_LANG_C17:
629   case DW_LANG_Ada2005:
630   case DW_LANG_Ada2012:
631   case DW_LANG_HIP:
632   case DW_LANG_Assembly:
633   case DW_LANG_C_sharp:
634   case DW_LANG_Mojo:
635   case DW_LANG_GLSL:
636   case DW_LANG_GLSL_ES:
637   case DW_LANG_HLSL:
638   case DW_LANG_OpenCL_CPP:
639   case DW_LANG_CPP_for_OpenCL:
640   case DW_LANG_SYCL:
641   case DW_LANG_Ruby:
642   case DW_LANG_Move:
643   case DW_LANG_Hylo:
644     result = false;
645     break;
646   }
647 
648   return result;
649 }
650 
isC(SourceLanguage S)651 inline bool isC(SourceLanguage S) {
652   // Deliberately enumerate all the language options so we get a warning when
653   // new language options are added (-Wswitch) that'll hopefully help keep this
654   // switch up-to-date when new C++ versions are added.
655   switch (S) {
656   case DW_LANG_C11:
657   case DW_LANG_C17:
658   case DW_LANG_C89:
659   case DW_LANG_C99:
660   case DW_LANG_C:
661   case DW_LANG_ObjC:
662     return true;
663   case DW_LANG_C_plus_plus:
664   case DW_LANG_C_plus_plus_03:
665   case DW_LANG_C_plus_plus_11:
666   case DW_LANG_C_plus_plus_14:
667   case DW_LANG_C_plus_plus_17:
668   case DW_LANG_C_plus_plus_20:
669   case DW_LANG_Ada83:
670   case DW_LANG_Cobol74:
671   case DW_LANG_Cobol85:
672   case DW_LANG_Fortran77:
673   case DW_LANG_Fortran90:
674   case DW_LANG_Pascal83:
675   case DW_LANG_Modula2:
676   case DW_LANG_Java:
677   case DW_LANG_Ada95:
678   case DW_LANG_Fortran95:
679   case DW_LANG_PLI:
680   case DW_LANG_ObjC_plus_plus:
681   case DW_LANG_UPC:
682   case DW_LANG_D:
683   case DW_LANG_Python:
684   case DW_LANG_OpenCL:
685   case DW_LANG_Go:
686   case DW_LANG_Modula3:
687   case DW_LANG_Haskell:
688   case DW_LANG_OCaml:
689   case DW_LANG_Rust:
690   case DW_LANG_Swift:
691   case DW_LANG_Julia:
692   case DW_LANG_Dylan:
693   case DW_LANG_Fortran03:
694   case DW_LANG_Fortran08:
695   case DW_LANG_RenderScript:
696   case DW_LANG_BLISS:
697   case DW_LANG_Mips_Assembler:
698   case DW_LANG_GOOGLE_RenderScript:
699   case DW_LANG_BORLAND_Delphi:
700   case DW_LANG_lo_user:
701   case DW_LANG_hi_user:
702   case DW_LANG_Kotlin:
703   case DW_LANG_Zig:
704   case DW_LANG_Crystal:
705   case DW_LANG_Fortran18:
706   case DW_LANG_Ada2005:
707   case DW_LANG_Ada2012:
708   case DW_LANG_HIP:
709   case DW_LANG_Assembly:
710   case DW_LANG_C_sharp:
711   case DW_LANG_Mojo:
712   case DW_LANG_GLSL:
713   case DW_LANG_GLSL_ES:
714   case DW_LANG_HLSL:
715   case DW_LANG_OpenCL_CPP:
716   case DW_LANG_CPP_for_OpenCL:
717   case DW_LANG_SYCL:
718   case DW_LANG_Ruby:
719   case DW_LANG_Move:
720   case DW_LANG_Hylo:
721     return false;
722   }
723   llvm_unreachable("Unknown language kind.");
724 }
725 
getArrayIndexTypeEncoding(SourceLanguage S)726 inline TypeKind getArrayIndexTypeEncoding(SourceLanguage S) {
727   return isFortran(S) ? DW_ATE_signed : DW_ATE_unsigned;
728 }
729 
730 enum CaseSensitivity {
731   // Identifier case codes
732   DW_ID_case_sensitive = 0x00,
733   DW_ID_up_case = 0x01,
734   DW_ID_down_case = 0x02,
735   DW_ID_case_insensitive = 0x03
736 };
737 
738 enum CallingConvention {
739 // Calling convention codes
740 #define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
741 #include "llvm/BinaryFormat/Dwarf.def"
742   DW_CC_lo_user = 0x40,
743   DW_CC_hi_user = 0xff
744 };
745 
746 enum InlineAttribute {
747   // Inline codes
748   DW_INL_not_inlined = 0x00,
749   DW_INL_inlined = 0x01,
750   DW_INL_declared_not_inlined = 0x02,
751   DW_INL_declared_inlined = 0x03
752 };
753 
754 enum ArrayDimensionOrdering {
755   // Array ordering
756   DW_ORD_row_major = 0x00,
757   DW_ORD_col_major = 0x01
758 };
759 
760 enum DiscriminantList {
761   // Discriminant descriptor values
762   DW_DSC_label = 0x00,
763   DW_DSC_range = 0x01
764 };
765 
766 /// Line Number Standard Opcode Encodings.
767 enum LineNumberOps : uint8_t {
768 #define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
769 #include "llvm/BinaryFormat/Dwarf.def"
770 };
771 
772 /// Line Number Extended Opcode Encodings.
773 enum LineNumberExtendedOps {
774 #define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
775 #include "llvm/BinaryFormat/Dwarf.def"
776   DW_LNE_lo_user = 0x80,
777   DW_LNE_hi_user = 0xff
778 };
779 
780 enum LineNumberEntryFormat {
781 #define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
782 #include "llvm/BinaryFormat/Dwarf.def"
783   DW_LNCT_lo_user = 0x2000,
784   DW_LNCT_hi_user = 0x3fff,
785 };
786 
787 enum MacinfoRecordType {
788   // Macinfo Type Encodings
789   DW_MACINFO_define = 0x01,
790   DW_MACINFO_undef = 0x02,
791   DW_MACINFO_start_file = 0x03,
792   DW_MACINFO_end_file = 0x04,
793   DW_MACINFO_vendor_ext = 0xff
794 };
795 
796 /// DWARF v5 macro information entry type encodings.
797 enum MacroEntryType {
798 #define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
799 #include "llvm/BinaryFormat/Dwarf.def"
800   DW_MACRO_lo_user = 0xe0,
801   DW_MACRO_hi_user = 0xff
802 };
803 
804 /// GNU .debug_macro macro information entry type encodings.
805 enum GnuMacroEntryType {
806 #define HANDLE_DW_MACRO_GNU(ID, NAME) DW_MACRO_GNU_##NAME = ID,
807 #include "llvm/BinaryFormat/Dwarf.def"
808   DW_MACRO_GNU_lo_user = 0xe0,
809   DW_MACRO_GNU_hi_user = 0xff
810 };
811 
812 /// DWARF v5 range list entry encoding values.
813 enum RnglistEntries {
814 #define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
815 #include "llvm/BinaryFormat/Dwarf.def"
816 };
817 
818 /// DWARF v5 loc list entry encoding values.
819 enum LoclistEntries {
820 #define HANDLE_DW_LLE(ID, NAME) DW_LLE_##NAME = ID,
821 #include "llvm/BinaryFormat/Dwarf.def"
822 };
823 
824 /// Call frame instruction encodings.
825 enum CallFrameInfo {
826 #define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
827 #define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
828 #include "llvm/BinaryFormat/Dwarf.def"
829   DW_CFA_extended = 0x00,
830 
831   DW_CFA_lo_user = 0x1c,
832   DW_CFA_hi_user = 0x3f
833 };
834 
835 enum Constants {
836   // Children flag
837   DW_CHILDREN_no = 0x00,
838   DW_CHILDREN_yes = 0x01,
839 
840   DW_EH_PE_absptr = 0x00,
841   DW_EH_PE_omit = 0xff,
842   DW_EH_PE_uleb128 = 0x01,
843   DW_EH_PE_udata2 = 0x02,
844   DW_EH_PE_udata4 = 0x03,
845   DW_EH_PE_udata8 = 0x04,
846   DW_EH_PE_sleb128 = 0x09,
847   DW_EH_PE_sdata2 = 0x0A,
848   DW_EH_PE_sdata4 = 0x0B,
849   DW_EH_PE_sdata8 = 0x0C,
850   DW_EH_PE_signed = 0x08,
851   DW_EH_PE_pcrel = 0x10,
852   DW_EH_PE_textrel = 0x20,
853   DW_EH_PE_datarel = 0x30,
854   DW_EH_PE_funcrel = 0x40,
855   DW_EH_PE_aligned = 0x50,
856   DW_EH_PE_indirect = 0x80
857 };
858 
859 /// Constants for the DW_APPLE_PROPERTY_attributes attribute.
860 /// Keep this list in sync with clang's DeclObjCCommon.h
861 /// ObjCPropertyAttribute::Kind!
862 enum ApplePropertyAttributes {
863 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
864 #include "llvm/BinaryFormat/Dwarf.def"
865 };
866 
867 /// Constants for unit types in DWARF v5.
868 enum UnitType : unsigned char {
869 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
870 #include "llvm/BinaryFormat/Dwarf.def"
871   DW_UT_lo_user = 0x80,
872   DW_UT_hi_user = 0xff
873 };
874 
875 enum Index {
876 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
877 #include "llvm/BinaryFormat/Dwarf.def"
878   DW_IDX_lo_user = 0x2000,
879   DW_IDX_hi_user = 0x3fff
880 };
881 
isUnitType(uint8_t UnitType)882 inline bool isUnitType(uint8_t UnitType) {
883   switch (UnitType) {
884   case DW_UT_compile:
885   case DW_UT_type:
886   case DW_UT_partial:
887   case DW_UT_skeleton:
888   case DW_UT_split_compile:
889   case DW_UT_split_type:
890     return true;
891   default:
892     return false;
893   }
894 }
895 
isUnitType(dwarf::Tag T)896 inline bool isUnitType(dwarf::Tag T) {
897   switch (T) {
898   case DW_TAG_compile_unit:
899   case DW_TAG_type_unit:
900   case DW_TAG_partial_unit:
901   case DW_TAG_skeleton_unit:
902     return true;
903   default:
904     return false;
905   }
906 }
907 
908 // Constants for the DWARF v5 Accelerator Table Proposal
909 enum AcceleratorTable {
910   // Data layout descriptors.
911   DW_ATOM_null = 0u,       ///  Marker as the end of a list of atoms.
912   DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
913   DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
914                           // item in question.
915   DW_ATOM_die_tag = 3u,   // A tag entry.
916   DW_ATOM_type_flags = 4u, // Set of flags for a type.
917 
918   DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
919   DW_ATOM_qual_name_hash = 6u,  // Dsymutil qualified hash extension.
920 
921   // DW_ATOM_type_flags values.
922 
923   // Always set for C++, only set for ObjC if this is the @implementation for a
924   // class.
925   DW_FLAG_type_implementation = 2u,
926 
927   // Hash functions.
928 
929   // Daniel J. Bernstein hash.
930   DW_hash_function_djb = 0u
931 };
932 
933 // Return a suggested bucket count for the DWARF v5 Accelerator Table.
getDebugNamesBucketCount(uint32_t UniqueHashCount)934 inline uint32_t getDebugNamesBucketCount(uint32_t UniqueHashCount) {
935   if (UniqueHashCount > 1024)
936     return UniqueHashCount / 4;
937   if (UniqueHashCount > 16)
938     return UniqueHashCount / 2;
939   return std::max<uint32_t>(UniqueHashCount, 1);
940 }
941 
942 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
943 enum GDBIndexEntryKind {
944   GIEK_NONE,
945   GIEK_TYPE,
946   GIEK_VARIABLE,
947   GIEK_FUNCTION,
948   GIEK_OTHER,
949   GIEK_UNUSED5,
950   GIEK_UNUSED6,
951   GIEK_UNUSED7
952 };
953 
954 enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
955 
956 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
957 ///
958 /// All these functions map their argument's value back to the
959 /// corresponding enumerator name or return an empty StringRef if the value
960 /// isn't known.
961 ///
962 /// @{
963 StringRef TagString(unsigned Tag);
964 StringRef ChildrenString(unsigned Children);
965 StringRef AttributeString(unsigned Attribute);
966 StringRef FormEncodingString(unsigned Encoding);
967 StringRef OperationEncodingString(unsigned Encoding);
968 StringRef SubOperationEncodingString(unsigned OpEncoding,
969                                      unsigned SubOpEncoding);
970 StringRef AttributeEncodingString(unsigned Encoding);
971 StringRef DecimalSignString(unsigned Sign);
972 StringRef EndianityString(unsigned Endian);
973 StringRef AccessibilityString(unsigned Access);
974 StringRef DefaultedMemberString(unsigned DefaultedEncodings);
975 StringRef VisibilityString(unsigned Visibility);
976 StringRef VirtualityString(unsigned Virtuality);
977 StringRef LanguageString(unsigned Language);
978 StringRef CaseString(unsigned Case);
979 StringRef ConventionString(unsigned Convention);
980 StringRef InlineCodeString(unsigned Code);
981 StringRef ArrayOrderString(unsigned Order);
982 StringRef LNStandardString(unsigned Standard);
983 StringRef LNExtendedString(unsigned Encoding);
984 StringRef MacinfoString(unsigned Encoding);
985 StringRef MacroString(unsigned Encoding);
986 StringRef GnuMacroString(unsigned Encoding);
987 StringRef RangeListEncodingString(unsigned Encoding);
988 StringRef LocListEncodingString(unsigned Encoding);
989 StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
990 StringRef ApplePropertyString(unsigned);
991 StringRef UnitTypeString(unsigned);
992 StringRef AtomTypeString(unsigned Atom);
993 StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
994 StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
995 StringRef IndexString(unsigned Idx);
996 StringRef FormatString(DwarfFormat Format);
997 StringRef FormatString(bool IsDWARF64);
998 StringRef RLEString(unsigned RLE);
999 /// @}
1000 
1001 /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
1002 ///
1003 /// These functions map their strings back to the corresponding enumeration
1004 /// value or return 0 if there is none, except for these exceptions:
1005 ///
1006 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
1007 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
1008 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
1009 ///
1010 /// @{
1011 unsigned getTag(StringRef TagString);
1012 unsigned getOperationEncoding(StringRef OperationEncodingString);
1013 unsigned getSubOperationEncoding(unsigned OpEncoding,
1014                                  StringRef SubOperationEncodingString);
1015 unsigned getVirtuality(StringRef VirtualityString);
1016 unsigned getLanguage(StringRef LanguageString);
1017 unsigned getCallingConvention(StringRef LanguageString);
1018 unsigned getAttributeEncoding(StringRef EncodingString);
1019 unsigned getMacinfo(StringRef MacinfoString);
1020 unsigned getMacro(StringRef MacroString);
1021 /// @}
1022 
1023 /// \defgroup DwarfConstantsVersioning Dwarf version for constants
1024 ///
1025 /// For constants defined by DWARF, returns the DWARF version when the constant
1026 /// was first defined. For vendor extensions, if there is a version-related
1027 /// policy for when to emit it, returns a version number for that policy.
1028 /// Otherwise returns 0.
1029 ///
1030 /// @{
1031 unsigned TagVersion(Tag T);
1032 unsigned AttributeVersion(Attribute A);
1033 unsigned FormVersion(Form F);
1034 unsigned OperationVersion(LocationAtom O);
1035 unsigned AttributeEncodingVersion(TypeKind E);
1036 unsigned LanguageVersion(SourceLanguage L);
1037 /// @}
1038 
1039 /// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
1040 ///
1041 /// These functions return an identifier describing "who" defined the constant,
1042 /// either the DWARF standard itself or the vendor who defined the extension.
1043 ///
1044 /// @{
1045 unsigned TagVendor(Tag T);
1046 unsigned AttributeVendor(Attribute A);
1047 unsigned FormVendor(Form F);
1048 unsigned OperationVendor(LocationAtom O);
1049 unsigned AttributeEncodingVendor(TypeKind E);
1050 unsigned LanguageVendor(SourceLanguage L);
1051 /// @}
1052 
1053 /// The number of operands for the given LocationAtom.
1054 std::optional<unsigned> OperationOperands(LocationAtom O);
1055 
1056 /// The arity of the given LocationAtom. This is the number of elements on the
1057 /// stack this operation operates on. Returns -1 if the arity is variable (e.g.
1058 /// depending on the argument) or unknown.
1059 std::optional<unsigned> OperationArity(LocationAtom O);
1060 
1061 std::optional<unsigned> LanguageLowerBound(SourceLanguage L);
1062 
1063 /// The size of a reference determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSize(DwarfFormat Format)1064 inline uint8_t getDwarfOffsetByteSize(DwarfFormat Format) {
1065   switch (Format) {
1066   case DwarfFormat::DWARF32:
1067     return 4;
1068   case DwarfFormat::DWARF64:
1069     return 8;
1070   }
1071   llvm_unreachable("Invalid Format value");
1072 }
1073 
1074 /// A helper struct providing information about the byte size of DW_FORM
1075 /// values that vary in size depending on the DWARF version, address byte
1076 /// size, or DWARF32/DWARF64.
1077 struct FormParams {
1078   uint16_t Version;
1079   uint8_t AddrSize;
1080   DwarfFormat Format;
1081   /// True if DWARF v2 output generally uses relocations for references
1082   /// to other .debug_* sections.
1083   bool DwarfUsesRelocationsAcrossSections = false;
1084 
1085   /// The definition of the size of form DW_FORM_ref_addr depends on the
1086   /// version. In DWARF v2 it's the size of an address; after that, it's the
1087   /// size of a reference.
getRefAddrByteSizeFormParams1088   uint8_t getRefAddrByteSize() const {
1089     if (Version == 2)
1090       return AddrSize;
1091     return getDwarfOffsetByteSize();
1092   }
1093 
1094   /// The size of a reference is determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSizeFormParams1095   uint8_t getDwarfOffsetByteSize() const {
1096     return dwarf::getDwarfOffsetByteSize(Format);
1097   }
1098 
1099   explicit operator bool() const { return Version && AddrSize; }
1100 };
1101 
1102 /// Get the byte size of the unit length field depending on the DWARF format.
getUnitLengthFieldByteSize(DwarfFormat Format)1103 inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format) {
1104   switch (Format) {
1105   case DwarfFormat::DWARF32:
1106     return 4;
1107   case DwarfFormat::DWARF64:
1108     return 12;
1109   }
1110   llvm_unreachable("Invalid Format value");
1111 }
1112 
1113 /// Get the fixed byte size for a given form.
1114 ///
1115 /// If the form has a fixed byte size, then an Optional with a value will be
1116 /// returned. If the form is always encoded using a variable length storage
1117 /// format (ULEB or SLEB numbers or blocks) then std::nullopt will be returned.
1118 ///
1119 /// \param Form DWARF form to get the fixed byte size for.
1120 /// \param Params DWARF parameters to help interpret forms.
1121 /// \returns std::optional<uint8_t> value with the fixed byte size or
1122 /// std::nullopt if \p Form doesn't have a fixed byte size.
1123 std::optional<uint8_t> getFixedFormByteSize(dwarf::Form Form,
1124                                             FormParams Params);
1125 
1126 /// Tells whether the specified form is defined in the specified version,
1127 /// or is an extension if extensions are allowed.
1128 bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
1129 
1130 /// Returns the symbolic string representing Val when used as a value
1131 /// for attribute Attr.
1132 StringRef AttributeValueString(uint16_t Attr, unsigned Val);
1133 
1134 /// Returns the symbolic string representing Val when used as a value
1135 /// for atom Atom.
1136 StringRef AtomValueString(uint16_t Atom, unsigned Val);
1137 
1138 /// Describes an entry of the various gnu_pub* debug sections.
1139 ///
1140 /// The gnu_pub* kind looks like:
1141 ///
1142 /// 0-3  reserved
1143 /// 4-6  symbol kind
1144 /// 7    0 == global, 1 == static
1145 ///
1146 /// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
1147 /// offset of the cu within the debug_info section stored in those 24 bits.
1148 struct PubIndexEntryDescriptor {
1149   GDBIndexEntryKind Kind;
1150   GDBIndexEntryLinkage Linkage;
PubIndexEntryDescriptorPubIndexEntryDescriptor1151   PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
1152       : Kind(Kind), Linkage(Linkage) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor1153   /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
1154       : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor1155   explicit PubIndexEntryDescriptor(uint8_t Value)
1156       : Kind(
1157             static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
1158         Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
1159                                                   LINKAGE_OFFSET)) {}
toBitsPubIndexEntryDescriptor1160   uint8_t toBits() const {
1161     return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
1162   }
1163 
1164 private:
1165   enum {
1166     KIND_OFFSET = 4,
1167     KIND_MASK = 7 << KIND_OFFSET,
1168     LINKAGE_OFFSET = 7,
1169     LINKAGE_MASK = 1 << LINKAGE_OFFSET
1170   };
1171 };
1172 
1173 template <typename Enum> struct EnumTraits : public std::false_type {};
1174 
1175 template <> struct EnumTraits<Attribute> : public std::true_type {
1176   static constexpr char Type[3] = "AT";
1177   static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
1178 };
1179 
1180 template <> struct EnumTraits<Form> : public std::true_type {
1181   static constexpr char Type[5] = "FORM";
1182   static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
1183 };
1184 
1185 template <> struct EnumTraits<Index> : public std::true_type {
1186   static constexpr char Type[4] = "IDX";
1187   static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
1188 };
1189 
1190 template <> struct EnumTraits<Tag> : public std::true_type {
1191   static constexpr char Type[4] = "TAG";
1192   static constexpr StringRef (*StringFn)(unsigned) = &TagString;
1193 };
1194 
1195 template <> struct EnumTraits<LineNumberOps> : public std::true_type {
1196   static constexpr char Type[4] = "LNS";
1197   static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
1198 };
1199 
1200 template <> struct EnumTraits<LocationAtom> : public std::true_type {
1201   static constexpr char Type[3] = "OP";
1202   static constexpr StringRef (*StringFn)(unsigned) = &OperationEncodingString;
1203 };
1204 
1205 inline uint64_t computeTombstoneAddress(uint8_t AddressByteSize) {
1206   return std::numeric_limits<uint64_t>::max() >> (8 - AddressByteSize) * 8;
1207 }
1208 
1209 } // End of namespace dwarf
1210 
1211 /// Dwarf constants format_provider
1212 ///
1213 /// Specialization of the format_provider template for dwarf enums. Unlike the
1214 /// dumping functions above, these format unknown enumerator values as
1215 /// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
1216 template <typename Enum>
1217 struct format_provider<Enum, std::enable_if_t<dwarf::EnumTraits<Enum>::value>> {
1218   static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
1219     StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
1220     if (Str.empty()) {
1221       OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
1222          << llvm::format("%x", E);
1223     } else
1224       OS << Str;
1225   }
1226 };
1227 } // End of namespace llvm
1228 
1229 #endif
1230