blob: 3757c75f6ba1ce473aa07a30d3cd4cc520eba9ca [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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/ADT/Optional.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/DataTypes.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/Format.h"
27#include "llvm/Support/FormatVariadicDetails.h"
Andrew Walbran16937d02019-10-22 13:54:20 +010028#include "llvm/ADT/Triple.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029
30namespace llvm {
31class StringRef;
32
33namespace dwarf {
34
35//===----------------------------------------------------------------------===//
36// DWARF constants as gleaned from the DWARF Debugging Information Format V.5
37// reference manual http://www.dwarfstd.org/.
38//
39
40// Do not mix the following two enumerations sets. DW_TAG_invalid changes the
41// enumeration base type.
42
43enum LLVMConstants : uint32_t {
44 // LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
45 DW_TAG_invalid = ~0U, // Tag for invalid results.
46 DW_VIRTUALITY_invalid = ~0U, // Virtuality for invalid results.
47 DW_MACINFO_invalid = ~0U, // Macinfo type for invalid results.
48
49 // Other constants.
50 DWARF_VERSION = 4, // Default dwarf version we output.
51 DW_PUBTYPES_VERSION = 2, // Section version number for .debug_pubtypes.
52 DW_PUBNAMES_VERSION = 2, // Section version number for .debug_pubnames.
53 DW_ARANGES_VERSION = 2, // Section version number for .debug_aranges.
54 // Identifiers we use to distinguish vendor extensions.
55 DWARF_VENDOR_DWARF = 0, // Defined in v2 or later of the DWARF standard.
56 DWARF_VENDOR_APPLE = 1,
57 DWARF_VENDOR_BORLAND = 2,
58 DWARF_VENDOR_GNU = 3,
59 DWARF_VENDOR_GOOGLE = 4,
60 DWARF_VENDOR_LLVM = 5,
61 DWARF_VENDOR_MIPS = 6
62};
63
64/// Constants that define the DWARF format as 32 or 64 bit.
65enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
66
67/// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
68/// Not inside an enum because a 64-bit value is needed.
69/// @{
70const uint32_t DW_CIE_ID = UINT32_MAX;
71const uint64_t DW64_CIE_ID = UINT64_MAX;
72/// @}
73
74/// Identifier of an invalid DIE offset in the .debug_info section.
75const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
76
77enum Tag : uint16_t {
78#define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR) DW_TAG_##NAME = ID,
79#include "llvm/BinaryFormat/Dwarf.def"
80 DW_TAG_lo_user = 0x4080,
81 DW_TAG_hi_user = 0xffff,
82 DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
83};
84
85inline bool isType(Tag T) {
86 switch (T) {
87 case DW_TAG_array_type:
88 case DW_TAG_class_type:
89 case DW_TAG_interface_type:
90 case DW_TAG_enumeration_type:
91 case DW_TAG_pointer_type:
92 case DW_TAG_reference_type:
93 case DW_TAG_rvalue_reference_type:
94 case DW_TAG_string_type:
95 case DW_TAG_structure_type:
96 case DW_TAG_subroutine_type:
97 case DW_TAG_union_type:
98 case DW_TAG_ptr_to_member_type:
99 case DW_TAG_set_type:
100 case DW_TAG_subrange_type:
101 case DW_TAG_base_type:
102 case DW_TAG_const_type:
103 case DW_TAG_file_type:
104 case DW_TAG_packed_type:
105 case DW_TAG_volatile_type:
106 case DW_TAG_typedef:
107 return true;
108 default:
109 return false;
110 }
111}
112
113/// Attributes.
114enum Attribute : uint16_t {
115#define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
116#include "llvm/BinaryFormat/Dwarf.def"
117 DW_AT_lo_user = 0x2000,
118 DW_AT_hi_user = 0x3fff,
119};
120
121enum Form : uint16_t {
122#define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
123#include "llvm/BinaryFormat/Dwarf.def"
124 DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
125};
126
127enum LocationAtom {
128#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID,
129#include "llvm/BinaryFormat/Dwarf.def"
130 DW_OP_lo_user = 0xe0,
131 DW_OP_hi_user = 0xff,
132 DW_OP_LLVM_fragment = 0x1000 ///< Only used in LLVM metadata.
133};
134
135enum TypeKind : uint8_t {
136#define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
137#include "llvm/BinaryFormat/Dwarf.def"
138 DW_ATE_lo_user = 0x80,
139 DW_ATE_hi_user = 0xff
140};
141
142enum DecimalSignEncoding {
143 // Decimal sign attribute values
144 DW_DS_unsigned = 0x01,
145 DW_DS_leading_overpunch = 0x02,
146 DW_DS_trailing_overpunch = 0x03,
147 DW_DS_leading_separate = 0x04,
148 DW_DS_trailing_separate = 0x05
149};
150
151enum EndianityEncoding {
152 // Endianity attribute values
Andrew Scull0372a572018-11-16 15:47:06 +0000153#define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
154#include "llvm/BinaryFormat/Dwarf.def"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100155 DW_END_lo_user = 0x40,
156 DW_END_hi_user = 0xff
157};
158
159enum AccessAttribute {
160 // Accessibility codes
161 DW_ACCESS_public = 0x01,
162 DW_ACCESS_protected = 0x02,
163 DW_ACCESS_private = 0x03
164};
165
166enum VisibilityAttribute {
167 // Visibility codes
168 DW_VIS_local = 0x01,
169 DW_VIS_exported = 0x02,
170 DW_VIS_qualified = 0x03
171};
172
173enum VirtualityAttribute {
174#define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
175#include "llvm/BinaryFormat/Dwarf.def"
176 DW_VIRTUALITY_max = 0x02
177};
178
179enum DefaultedMemberAttribute {
180#define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
181#include "llvm/BinaryFormat/Dwarf.def"
182 DW_DEFAULTED_max = 0x02
183};
184
185enum SourceLanguage {
Andrew Walbran16937d02019-10-22 13:54:20 +0100186#define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
187 DW_LANG_##NAME = ID,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100188#include "llvm/BinaryFormat/Dwarf.def"
189 DW_LANG_lo_user = 0x8000,
190 DW_LANG_hi_user = 0xffff
191};
192
193enum CaseSensitivity {
194 // Identifier case codes
195 DW_ID_case_sensitive = 0x00,
196 DW_ID_up_case = 0x01,
197 DW_ID_down_case = 0x02,
198 DW_ID_case_insensitive = 0x03
199};
200
201enum CallingConvention {
202// Calling convention codes
203#define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
204#include "llvm/BinaryFormat/Dwarf.def"
205 DW_CC_lo_user = 0x40,
206 DW_CC_hi_user = 0xff
207};
208
209enum InlineAttribute {
210 // Inline codes
211 DW_INL_not_inlined = 0x00,
212 DW_INL_inlined = 0x01,
213 DW_INL_declared_not_inlined = 0x02,
214 DW_INL_declared_inlined = 0x03
215};
216
217enum ArrayDimensionOrdering {
218 // Array ordering
219 DW_ORD_row_major = 0x00,
220 DW_ORD_col_major = 0x01
221};
222
223enum DiscriminantList {
224 // Discriminant descriptor values
225 DW_DSC_label = 0x00,
226 DW_DSC_range = 0x01
227};
228
229/// Line Number Standard Opcode Encodings.
230enum LineNumberOps : uint8_t {
231#define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
232#include "llvm/BinaryFormat/Dwarf.def"
233};
234
235/// Line Number Extended Opcode Encodings.
236enum LineNumberExtendedOps {
237#define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
238#include "llvm/BinaryFormat/Dwarf.def"
239 DW_LNE_lo_user = 0x80,
240 DW_LNE_hi_user = 0xff
241};
242
243enum LineNumberEntryFormat {
244#define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
245#include "llvm/BinaryFormat/Dwarf.def"
246 DW_LNCT_lo_user = 0x2000,
247 DW_LNCT_hi_user = 0x3fff,
248};
249
250enum MacinfoRecordType {
251 // Macinfo Type Encodings
252 DW_MACINFO_define = 0x01,
253 DW_MACINFO_undef = 0x02,
254 DW_MACINFO_start_file = 0x03,
255 DW_MACINFO_end_file = 0x04,
256 DW_MACINFO_vendor_ext = 0xff
257};
258
259/// DWARF v5 macro information entry type encodings.
260enum MacroEntryType {
261#define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
262#include "llvm/BinaryFormat/Dwarf.def"
263 DW_MACRO_lo_user = 0xe0,
264 DW_MACRO_hi_user = 0xff
265};
266
267/// DWARF v5 range list entry encoding values.
268enum RangeListEntries {
269#define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
270#include "llvm/BinaryFormat/Dwarf.def"
271};
272
273/// Call frame instruction encodings.
274enum CallFrameInfo {
275#define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
Andrew Walbran16937d02019-10-22 13:54:20 +0100276#define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100277#include "llvm/BinaryFormat/Dwarf.def"
278 DW_CFA_extended = 0x00,
279
280 DW_CFA_lo_user = 0x1c,
281 DW_CFA_hi_user = 0x3f
282};
283
284enum Constants {
285 // Children flag
286 DW_CHILDREN_no = 0x00,
287 DW_CHILDREN_yes = 0x01,
288
289 DW_EH_PE_absptr = 0x00,
290 DW_EH_PE_omit = 0xff,
291 DW_EH_PE_uleb128 = 0x01,
292 DW_EH_PE_udata2 = 0x02,
293 DW_EH_PE_udata4 = 0x03,
294 DW_EH_PE_udata8 = 0x04,
295 DW_EH_PE_sleb128 = 0x09,
296 DW_EH_PE_sdata2 = 0x0A,
297 DW_EH_PE_sdata4 = 0x0B,
298 DW_EH_PE_sdata8 = 0x0C,
299 DW_EH_PE_signed = 0x08,
300 DW_EH_PE_pcrel = 0x10,
301 DW_EH_PE_textrel = 0x20,
302 DW_EH_PE_datarel = 0x30,
303 DW_EH_PE_funcrel = 0x40,
304 DW_EH_PE_aligned = 0x50,
305 DW_EH_PE_indirect = 0x80
306};
307
308/// Constants for location lists in DWARF v5.
309enum LocationListEntry : unsigned char {
310 DW_LLE_end_of_list = 0x00,
311 DW_LLE_base_addressx = 0x01,
312 DW_LLE_startx_endx = 0x02,
313 DW_LLE_startx_length = 0x03,
314 DW_LLE_offset_pair = 0x04,
315 DW_LLE_default_location = 0x05,
316 DW_LLE_base_address = 0x06,
317 DW_LLE_start_end = 0x07,
318 DW_LLE_start_length = 0x08
319};
320
321/// Constants for the DW_APPLE_PROPERTY_attributes attribute.
322/// Keep this list in sync with clang's DeclSpec.h ObjCPropertyAttributeKind!
323enum ApplePropertyAttributes {
324#define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
325#include "llvm/BinaryFormat/Dwarf.def"
326};
327
328/// Constants for unit types in DWARF v5.
329enum UnitType : unsigned char {
330#define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
331#include "llvm/BinaryFormat/Dwarf.def"
332 DW_UT_lo_user = 0x80,
333 DW_UT_hi_user = 0xff
334};
335
336enum Index {
337#define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
338#include "llvm/BinaryFormat/Dwarf.def"
339 DW_IDX_lo_user = 0x2000,
340 DW_IDX_hi_user = 0x3fff
341};
342
343inline bool isUnitType(uint8_t UnitType) {
344 switch (UnitType) {
345 case DW_UT_compile:
346 case DW_UT_type:
347 case DW_UT_partial:
348 case DW_UT_skeleton:
349 case DW_UT_split_compile:
350 case DW_UT_split_type:
351 return true;
352 default:
353 return false;
354 }
355}
356
357inline bool isUnitType(dwarf::Tag T) {
358 switch (T) {
359 case DW_TAG_compile_unit:
360 case DW_TAG_type_unit:
361 case DW_TAG_partial_unit:
362 case DW_TAG_skeleton_unit:
363 return true;
364 default:
365 return false;
366 }
367}
368
369// Constants for the DWARF v5 Accelerator Table Proposal
370enum AcceleratorTable {
371 // Data layout descriptors.
372 DW_ATOM_null = 0u, /// Marker as the end of a list of atoms.
373 DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
374 DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
375 // item in question.
376 DW_ATOM_die_tag = 3u, // A tag entry.
377 DW_ATOM_type_flags = 4u, // Set of flags for a type.
378
379 DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
380 DW_ATOM_qual_name_hash = 6u, // Dsymutil qualified hash extension.
381
382 // DW_ATOM_type_flags values.
383
384 // Always set for C++, only set for ObjC if this is the @implementation for a
385 // class.
386 DW_FLAG_type_implementation = 2u,
387
388 // Hash functions.
389
390 // Daniel J. Bernstein hash.
391 DW_hash_function_djb = 0u
392};
393
394// Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
395enum GDBIndexEntryKind {
396 GIEK_NONE,
397 GIEK_TYPE,
398 GIEK_VARIABLE,
399 GIEK_FUNCTION,
400 GIEK_OTHER,
401 GIEK_UNUSED5,
402 GIEK_UNUSED6,
403 GIEK_UNUSED7
404};
405
406enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
407
408/// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
409///
410/// All these functions map their argument's value back to the
411/// corresponding enumerator name or return an empty StringRef if the value
412/// isn't known.
413///
414/// @{
415StringRef TagString(unsigned Tag);
416StringRef ChildrenString(unsigned Children);
417StringRef AttributeString(unsigned Attribute);
418StringRef FormEncodingString(unsigned Encoding);
419StringRef OperationEncodingString(unsigned Encoding);
420StringRef AttributeEncodingString(unsigned Encoding);
421StringRef DecimalSignString(unsigned Sign);
422StringRef EndianityString(unsigned Endian);
423StringRef AccessibilityString(unsigned Access);
424StringRef VisibilityString(unsigned Visibility);
425StringRef VirtualityString(unsigned Virtuality);
426StringRef LanguageString(unsigned Language);
427StringRef CaseString(unsigned Case);
428StringRef ConventionString(unsigned Convention);
429StringRef InlineCodeString(unsigned Code);
430StringRef ArrayOrderString(unsigned Order);
431StringRef LNStandardString(unsigned Standard);
432StringRef LNExtendedString(unsigned Encoding);
433StringRef MacinfoString(unsigned Encoding);
434StringRef RangeListEncodingString(unsigned Encoding);
Andrew Walbran16937d02019-10-22 13:54:20 +0100435StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100436StringRef ApplePropertyString(unsigned);
437StringRef UnitTypeString(unsigned);
438StringRef AtomTypeString(unsigned Atom);
439StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
440StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
441StringRef IndexString(unsigned Idx);
442/// @}
443
444/// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
445///
446/// These functions map their strings back to the corresponding enumeration
447/// value or return 0 if there is none, except for these exceptions:
448///
449/// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
450/// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
451/// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
452///
453/// @{
454unsigned getTag(StringRef TagString);
455unsigned getOperationEncoding(StringRef OperationEncodingString);
456unsigned getVirtuality(StringRef VirtualityString);
457unsigned getLanguage(StringRef LanguageString);
458unsigned getCallingConvention(StringRef LanguageString);
459unsigned getAttributeEncoding(StringRef EncodingString);
460unsigned getMacinfo(StringRef MacinfoString);
461/// @}
462
463/// \defgroup DwarfConstantsVersioning Dwarf version for constants
464///
465/// For constants defined by DWARF, returns the DWARF version when the constant
466/// was first defined. For vendor extensions, if there is a version-related
467/// policy for when to emit it, returns a version number for that policy.
468/// Otherwise returns 0.
469///
470/// @{
471unsigned TagVersion(Tag T);
472unsigned AttributeVersion(Attribute A);
473unsigned FormVersion(Form F);
474unsigned OperationVersion(LocationAtom O);
475unsigned AttributeEncodingVersion(TypeKind E);
476unsigned LanguageVersion(SourceLanguage L);
477/// @}
478
479/// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
480///
481/// These functions return an identifier describing "who" defined the constant,
482/// either the DWARF standard itself or the vendor who defined the extension.
483///
484/// @{
485unsigned TagVendor(Tag T);
486unsigned AttributeVendor(Attribute A);
487unsigned FormVendor(Form F);
488unsigned OperationVendor(LocationAtom O);
489unsigned AttributeEncodingVendor(TypeKind E);
490unsigned LanguageVendor(SourceLanguage L);
491/// @}
492
Andrew Walbran16937d02019-10-22 13:54:20 +0100493Optional<unsigned> LanguageLowerBound(SourceLanguage L);
494
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100495/// A helper struct providing information about the byte size of DW_FORM
496/// values that vary in size depending on the DWARF version, address byte
497/// size, or DWARF32/DWARF64.
498struct FormParams {
499 uint16_t Version;
500 uint8_t AddrSize;
501 DwarfFormat Format;
502
503 /// The definition of the size of form DW_FORM_ref_addr depends on the
504 /// version. In DWARF v2 it's the size of an address; after that, it's the
505 /// size of a reference.
506 uint8_t getRefAddrByteSize() const {
507 if (Version == 2)
508 return AddrSize;
509 return getDwarfOffsetByteSize();
510 }
511
512 /// The size of a reference is determined by the DWARF 32/64-bit format.
513 uint8_t getDwarfOffsetByteSize() const {
514 switch (Format) {
515 case DwarfFormat::DWARF32:
516 return 4;
517 case DwarfFormat::DWARF64:
518 return 8;
519 }
520 llvm_unreachable("Invalid Format value");
521 }
522
523 explicit operator bool() const { return Version && AddrSize; }
524};
525
526/// Get the fixed byte size for a given form.
527///
528/// If the form has a fixed byte size, then an Optional with a value will be
529/// returned. If the form is always encoded using a variable length storage
530/// format (ULEB or SLEB numbers or blocks) then None will be returned.
531///
532/// \param Form DWARF form to get the fixed byte size for.
533/// \param Params DWARF parameters to help interpret forms.
534/// \returns Optional<uint8_t> value with the fixed byte size or None if
535/// \p Form doesn't have a fixed byte size.
536Optional<uint8_t> getFixedFormByteSize(dwarf::Form Form, FormParams Params);
537
538/// Tells whether the specified form is defined in the specified version,
539/// or is an extension if extensions are allowed.
540bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
541
542/// Returns the symbolic string representing Val when used as a value
543/// for attribute Attr.
544StringRef AttributeValueString(uint16_t Attr, unsigned Val);
545
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100546/// Returns the symbolic string representing Val when used as a value
547/// for atom Atom.
548StringRef AtomValueString(uint16_t Atom, unsigned Val);
549
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100550/// Describes an entry of the various gnu_pub* debug sections.
551///
552/// The gnu_pub* kind looks like:
553///
554/// 0-3 reserved
555/// 4-6 symbol kind
556/// 7 0 == global, 1 == static
557///
558/// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
559/// offset of the cu within the debug_info section stored in those 24 bits.
560struct PubIndexEntryDescriptor {
561 GDBIndexEntryKind Kind;
562 GDBIndexEntryLinkage Linkage;
563 PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
564 : Kind(Kind), Linkage(Linkage) {}
565 /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
566 : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
567 explicit PubIndexEntryDescriptor(uint8_t Value)
568 : Kind(
569 static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
570 Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
571 LINKAGE_OFFSET)) {}
572 uint8_t toBits() const {
573 return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
574 }
575
576private:
577 enum {
578 KIND_OFFSET = 4,
579 KIND_MASK = 7 << KIND_OFFSET,
580 LINKAGE_OFFSET = 7,
581 LINKAGE_MASK = 1 << LINKAGE_OFFSET
582 };
583};
584
585template <typename Enum> struct EnumTraits : public std::false_type {};
586
587template <> struct EnumTraits<Attribute> : public std::true_type {
588 static constexpr char Type[3] = "AT";
589 static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
590};
591
592template <> struct EnumTraits<Form> : public std::true_type {
593 static constexpr char Type[5] = "FORM";
594 static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
595};
596
597template <> struct EnumTraits<Index> : public std::true_type {
598 static constexpr char Type[4] = "IDX";
599 static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
600};
601
602template <> struct EnumTraits<Tag> : public std::true_type {
603 static constexpr char Type[4] = "TAG";
604 static constexpr StringRef (*StringFn)(unsigned) = &TagString;
605};
606} // End of namespace dwarf
607
608/// Dwarf constants format_provider
609///
610/// Specialization of the format_provider template for dwarf enums. Unlike the
611/// dumping functions above, these format unknown enumerator values as
612/// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
613template <typename Enum>
614struct format_provider<
615 Enum, typename std::enable_if<dwarf::EnumTraits<Enum>::value>::type> {
616 static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
617 StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
618 if (Str.empty()) {
619 OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
620 << llvm::format("%x", E);
621 } else
622 OS << Str;
623 }
624};
625} // End of namespace llvm
626
627#endif