Bug Summary

File:llvm/include/llvm/ObjectYAML/ELFYAML.h
Warning:line 55, column 1
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ELFYAML.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/ObjectYAML -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/ObjectYAML -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/ObjectYAML -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/ObjectYAML -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/ObjectYAML/ELFYAML.cpp

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/ObjectYAML/ELFYAML.cpp

1//===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===//
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// This file defines classes for handling the YAML representation of ELF.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ObjectYAML/ELFYAML.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/MapVector.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/BinaryFormat/ELF.h"
18#include "llvm/Support/ARMEHABI.h"
19#include "llvm/Support/Casting.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/MipsABIFlags.h"
22#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/WithColor.h"
24#include <cassert>
25#include <cstdint>
26
27namespace llvm {
28
29ELFYAML::Chunk::~Chunk() = default;
30
31namespace ELFYAML {
32unsigned Object::getMachine() const {
33 if (Header.Machine)
34 return *Header.Machine;
35 return llvm::ELF::EM_NONE;
36}
37
38constexpr StringRef SectionHeaderTable::TypeStr;
39} // namespace ELFYAML
40
41namespace yaml {
42
43void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(
44 IO &IO, ELFYAML::ELF_ET &Value) {
45#define ECase(X) IO.enumCase(Value, #X, ELF::X)
46 ECase(ET_NONE);
47 ECase(ET_REL);
48 ECase(ET_EXEC);
49 ECase(ET_DYN);
50 ECase(ET_CORE);
51#undef ECase
52 IO.enumFallback<Hex16>(Value);
53}
54
55void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration(
56 IO &IO, ELFYAML::ELF_PT &Value) {
57#define ECase(X) IO.enumCase(Value, #X, ELF::X)
58 ECase(PT_NULL);
59 ECase(PT_LOAD);
60 ECase(PT_DYNAMIC);
61 ECase(PT_INTERP);
62 ECase(PT_NOTE);
63 ECase(PT_SHLIB);
64 ECase(PT_PHDR);
65 ECase(PT_TLS);
66 ECase(PT_GNU_EH_FRAME);
67 ECase(PT_GNU_STACK);
68 ECase(PT_GNU_RELRO);
69 ECase(PT_GNU_PROPERTY);
70#undef ECase
71 IO.enumFallback<Hex32>(Value);
72}
73
74void ScalarEnumerationTraits<ELFYAML::ELF_NT>::enumeration(
75 IO &IO, ELFYAML::ELF_NT &Value) {
76#define ECase(X) IO.enumCase(Value, #X, ELF::X)
77 // Generic note types.
78 ECase(NT_VERSION);
79 ECase(NT_ARCH);
80 ECase(NT_GNU_BUILD_ATTRIBUTE_OPEN);
81 ECase(NT_GNU_BUILD_ATTRIBUTE_FUNC);
82 // Core note types.
83 ECase(NT_PRSTATUS);
84 ECase(NT_FPREGSET);
85 ECase(NT_PRPSINFO);
86 ECase(NT_TASKSTRUCT);
87 ECase(NT_AUXV);
88 ECase(NT_PSTATUS);
89 ECase(NT_FPREGS);
90 ECase(NT_PSINFO);
91 ECase(NT_LWPSTATUS);
92 ECase(NT_LWPSINFO);
93 ECase(NT_WIN32PSTATUS);
94 ECase(NT_PPC_VMX);
95 ECase(NT_PPC_VSX);
96 ECase(NT_PPC_TAR);
97 ECase(NT_PPC_PPR);
98 ECase(NT_PPC_DSCR);
99 ECase(NT_PPC_EBB);
100 ECase(NT_PPC_PMU);
101 ECase(NT_PPC_TM_CGPR);
102 ECase(NT_PPC_TM_CFPR);
103 ECase(NT_PPC_TM_CVMX);
104 ECase(NT_PPC_TM_CVSX);
105 ECase(NT_PPC_TM_SPR);
106 ECase(NT_PPC_TM_CTAR);
107 ECase(NT_PPC_TM_CPPR);
108 ECase(NT_PPC_TM_CDSCR);
109 ECase(NT_386_TLS);
110 ECase(NT_386_IOPERM);
111 ECase(NT_X86_XSTATE);
112 ECase(NT_S390_HIGH_GPRS);
113 ECase(NT_S390_TIMER);
114 ECase(NT_S390_TODCMP);
115 ECase(NT_S390_TODPREG);
116 ECase(NT_S390_CTRS);
117 ECase(NT_S390_PREFIX);
118 ECase(NT_S390_LAST_BREAK);
119 ECase(NT_S390_SYSTEM_CALL);
120 ECase(NT_S390_TDB);
121 ECase(NT_S390_VXRS_LOW);
122 ECase(NT_S390_VXRS_HIGH);
123 ECase(NT_S390_GS_CB);
124 ECase(NT_S390_GS_BC);
125 ECase(NT_ARM_VFP);
126 ECase(NT_ARM_TLS);
127 ECase(NT_ARM_HW_BREAK);
128 ECase(NT_ARM_HW_WATCH);
129 ECase(NT_ARM_SVE);
130 ECase(NT_ARM_PAC_MASK);
131 ECase(NT_FILE);
132 ECase(NT_PRXFPREG);
133 ECase(NT_SIGINFO);
134 // LLVM-specific notes.
135 ECase(NT_LLVM_HWASAN_GLOBALS);
136 // GNU note types
137 ECase(NT_GNU_ABI_TAG);
138 ECase(NT_GNU_HWCAP);
139 ECase(NT_GNU_BUILD_ID);
140 ECase(NT_GNU_GOLD_VERSION);
141 ECase(NT_GNU_PROPERTY_TYPE_0);
142 // FreeBSD note types.
143 ECase(NT_FREEBSD_ABI_TAG);
144 ECase(NT_FREEBSD_NOINIT_TAG);
145 ECase(NT_FREEBSD_ARCH_TAG);
146 ECase(NT_FREEBSD_FEATURE_CTL);
147 // FreeBSD core note types.
148 ECase(NT_FREEBSD_THRMISC);
149 ECase(NT_FREEBSD_PROCSTAT_PROC);
150 ECase(NT_FREEBSD_PROCSTAT_FILES);
151 ECase(NT_FREEBSD_PROCSTAT_VMMAP);
152 ECase(NT_FREEBSD_PROCSTAT_GROUPS);
153 ECase(NT_FREEBSD_PROCSTAT_UMASK);
154 ECase(NT_FREEBSD_PROCSTAT_RLIMIT);
155 ECase(NT_FREEBSD_PROCSTAT_OSREL);
156 ECase(NT_FREEBSD_PROCSTAT_PSSTRINGS);
157 ECase(NT_FREEBSD_PROCSTAT_AUXV);
158 // AMD specific notes. (Code Object V2)
159 ECase(NT_AMD_HSA_CODE_OBJECT_VERSION);
160 ECase(NT_AMD_HSA_HSAIL);
161 ECase(NT_AMD_HSA_ISA_VERSION);
162 ECase(NT_AMD_HSA_METADATA);
163 ECase(NT_AMD_HSA_ISA_NAME);
164 ECase(NT_AMD_PAL_METADATA);
165 // AMDGPU specific notes. (Code Object V3)
166 ECase(NT_AMDGPU_METADATA);
167#undef ECase
168 IO.enumFallback<Hex32>(Value);
169}
170
171void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(
172 IO &IO, ELFYAML::ELF_EM &Value) {
173#define ECase(X) IO.enumCase(Value, #X, ELF::X)
174 ECase(EM_NONE);
175 ECase(EM_M32);
176 ECase(EM_SPARC);
177 ECase(EM_386);
178 ECase(EM_68K);
179 ECase(EM_88K);
180 ECase(EM_IAMCU);
181 ECase(EM_860);
182 ECase(EM_MIPS);
183 ECase(EM_S370);
184 ECase(EM_MIPS_RS3_LE);
185 ECase(EM_PARISC);
186 ECase(EM_VPP500);
187 ECase(EM_SPARC32PLUS);
188 ECase(EM_960);
189 ECase(EM_PPC);
190 ECase(EM_PPC64);
191 ECase(EM_S390);
192 ECase(EM_SPU);
193 ECase(EM_V800);
194 ECase(EM_FR20);
195 ECase(EM_RH32);
196 ECase(EM_RCE);
197 ECase(EM_ARM);
198 ECase(EM_ALPHA);
199 ECase(EM_SH);
200 ECase(EM_SPARCV9);
201 ECase(EM_TRICORE);
202 ECase(EM_ARC);
203 ECase(EM_H8_300);
204 ECase(EM_H8_300H);
205 ECase(EM_H8S);
206 ECase(EM_H8_500);
207 ECase(EM_IA_64);
208 ECase(EM_MIPS_X);
209 ECase(EM_COLDFIRE);
210 ECase(EM_68HC12);
211 ECase(EM_MMA);
212 ECase(EM_PCP);
213 ECase(EM_NCPU);
214 ECase(EM_NDR1);
215 ECase(EM_STARCORE);
216 ECase(EM_ME16);
217 ECase(EM_ST100);
218 ECase(EM_TINYJ);
219 ECase(EM_X86_64);
220 ECase(EM_PDSP);
221 ECase(EM_PDP10);
222 ECase(EM_PDP11);
223 ECase(EM_FX66);
224 ECase(EM_ST9PLUS);
225 ECase(EM_ST7);
226 ECase(EM_68HC16);
227 ECase(EM_68HC11);
228 ECase(EM_68HC08);
229 ECase(EM_68HC05);
230 ECase(EM_SVX);
231 ECase(EM_ST19);
232 ECase(EM_VAX);
233 ECase(EM_CRIS);
234 ECase(EM_JAVELIN);
235 ECase(EM_FIREPATH);
236 ECase(EM_ZSP);
237 ECase(EM_MMIX);
238 ECase(EM_HUANY);
239 ECase(EM_PRISM);
240 ECase(EM_AVR);
241 ECase(EM_FR30);
242 ECase(EM_D10V);
243 ECase(EM_D30V);
244 ECase(EM_V850);
245 ECase(EM_M32R);
246 ECase(EM_MN10300);
247 ECase(EM_MN10200);
248 ECase(EM_PJ);
249 ECase(EM_OPENRISC);
250 ECase(EM_ARC_COMPACT);
251 ECase(EM_XTENSA);
252 ECase(EM_VIDEOCORE);
253 ECase(EM_TMM_GPP);
254 ECase(EM_NS32K);
255 ECase(EM_TPC);
256 ECase(EM_SNP1K);
257 ECase(EM_ST200);
258 ECase(EM_IP2K);
259 ECase(EM_MAX);
260 ECase(EM_CR);
261 ECase(EM_F2MC16);
262 ECase(EM_MSP430);
263 ECase(EM_BLACKFIN);
264 ECase(EM_SE_C33);
265 ECase(EM_SEP);
266 ECase(EM_ARCA);
267 ECase(EM_UNICORE);
268 ECase(EM_EXCESS);
269 ECase(EM_DXP);
270 ECase(EM_ALTERA_NIOS2);
271 ECase(EM_CRX);
272 ECase(EM_XGATE);
273 ECase(EM_C166);
274 ECase(EM_M16C);
275 ECase(EM_DSPIC30F);
276 ECase(EM_CE);
277 ECase(EM_M32C);
278 ECase(EM_TSK3000);
279 ECase(EM_RS08);
280 ECase(EM_SHARC);
281 ECase(EM_ECOG2);
282 ECase(EM_SCORE7);
283 ECase(EM_DSP24);
284 ECase(EM_VIDEOCORE3);
285 ECase(EM_LATTICEMICO32);
286 ECase(EM_SE_C17);
287 ECase(EM_TI_C6000);
288 ECase(EM_TI_C2000);
289 ECase(EM_TI_C5500);
290 ECase(EM_MMDSP_PLUS);
291 ECase(EM_CYPRESS_M8C);
292 ECase(EM_R32C);
293 ECase(EM_TRIMEDIA);
294 ECase(EM_HEXAGON);
295 ECase(EM_8051);
296 ECase(EM_STXP7X);
297 ECase(EM_NDS32);
298 ECase(EM_ECOG1);
299 ECase(EM_ECOG1X);
300 ECase(EM_MAXQ30);
301 ECase(EM_XIMO16);
302 ECase(EM_MANIK);
303 ECase(EM_CRAYNV2);
304 ECase(EM_RX);
305 ECase(EM_METAG);
306 ECase(EM_MCST_ELBRUS);
307 ECase(EM_ECOG16);
308 ECase(EM_CR16);
309 ECase(EM_ETPU);
310 ECase(EM_SLE9X);
311 ECase(EM_L10M);
312 ECase(EM_K10M);
313 ECase(EM_AARCH64);
314 ECase(EM_AVR32);
315 ECase(EM_STM8);
316 ECase(EM_TILE64);
317 ECase(EM_TILEPRO);
318 ECase(EM_MICROBLAZE);
319 ECase(EM_CUDA);
320 ECase(EM_TILEGX);
321 ECase(EM_CLOUDSHIELD);
322 ECase(EM_COREA_1ST);
323 ECase(EM_COREA_2ND);
324 ECase(EM_ARC_COMPACT2);
325 ECase(EM_OPEN8);
326 ECase(EM_RL78);
327 ECase(EM_VIDEOCORE5);
328 ECase(EM_78KOR);
329 ECase(EM_56800EX);
330 ECase(EM_AMDGPU);
331 ECase(EM_RISCV);
332 ECase(EM_LANAI);
333 ECase(EM_BPF);
334 ECase(EM_VE);
335 ECase(EM_CSKY);
336#undef ECase
337 IO.enumFallback<Hex16>(Value);
338}
339
340void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
341 IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
342#define ECase(X) IO.enumCase(Value, #X, ELF::X)
343 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
344 // here.
345 ECase(ELFCLASS32);
346 ECase(ELFCLASS64);
347#undef ECase
348}
349
350void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
351 IO &IO, ELFYAML::ELF_ELFDATA &Value) {
352#define ECase(X) IO.enumCase(Value, #X, ELF::X)
353 // ELFDATANONE is an invalid data encoding, but we accept it because
354 // we want to be able to produce invalid binaries for the tests.
355 ECase(ELFDATANONE);
356 ECase(ELFDATA2LSB);
357 ECase(ELFDATA2MSB);
358#undef ECase
359}
360
361void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
362 IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
363#define ECase(X) IO.enumCase(Value, #X, ELF::X)
364 ECase(ELFOSABI_NONE);
365 ECase(ELFOSABI_HPUX);
366 ECase(ELFOSABI_NETBSD);
367 ECase(ELFOSABI_GNU);
368 ECase(ELFOSABI_LINUX);
369 ECase(ELFOSABI_HURD);
370 ECase(ELFOSABI_SOLARIS);
371 ECase(ELFOSABI_AIX);
372 ECase(ELFOSABI_IRIX);
373 ECase(ELFOSABI_FREEBSD);
374 ECase(ELFOSABI_TRU64);
375 ECase(ELFOSABI_MODESTO);
376 ECase(ELFOSABI_OPENBSD);
377 ECase(ELFOSABI_OPENVMS);
378 ECase(ELFOSABI_NSK);
379 ECase(ELFOSABI_AROS);
380 ECase(ELFOSABI_FENIXOS);
381 ECase(ELFOSABI_CLOUDABI);
382 ECase(ELFOSABI_AMDGPU_HSA);
383 ECase(ELFOSABI_AMDGPU_PAL);
384 ECase(ELFOSABI_AMDGPU_MESA3D);
385 ECase(ELFOSABI_ARM);
386 ECase(ELFOSABI_C6000_ELFABI);
387 ECase(ELFOSABI_C6000_LINUX);
388 ECase(ELFOSABI_STANDALONE);
389#undef ECase
390 IO.enumFallback<Hex8>(Value);
391}
392
393void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
394 ELFYAML::ELF_EF &Value) {
395 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
396 assert(Object && "The IO context is not initialized")(static_cast<void> (0));
397#define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
398#define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
399 switch (Object->getMachine()) {
400 case ELF::EM_ARM:
401 BCase(EF_ARM_SOFT_FLOAT);
402 BCase(EF_ARM_VFP_FLOAT);
403 BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK);
404 BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK);
405 BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK);
406 BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK);
407 BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK);
408 BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK);
409 break;
410 case ELF::EM_MIPS:
411 BCase(EF_MIPS_NOREORDER);
412 BCase(EF_MIPS_PIC);
413 BCase(EF_MIPS_CPIC);
414 BCase(EF_MIPS_ABI2);
415 BCase(EF_MIPS_32BITMODE);
416 BCase(EF_MIPS_FP64);
417 BCase(EF_MIPS_NAN2008);
418 BCase(EF_MIPS_MICROMIPS);
419 BCase(EF_MIPS_ARCH_ASE_M16);
420 BCase(EF_MIPS_ARCH_ASE_MDMX);
421 BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI);
422 BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI);
423 BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI);
424 BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI);
425 BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH);
426 BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH);
427 BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH);
428 BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH);
429 BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH);
430 BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH);
431 BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH);
432 BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH);
433 BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH);
434 BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH);
435 BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH);
436 BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH);
437 BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH);
438 BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH);
439 BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH);
440 BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH);
441 BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH);
442 BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH);
443 BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH);
444 BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH);
445 BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH);
446 BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH);
447 BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH);
448 BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH);
449 BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH);
450 BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH);
451 BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH);
452 BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH);
453 BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH);
454 break;
455 case ELF::EM_HEXAGON:
456 BCase(EF_HEXAGON_MACH_V2);
457 BCase(EF_HEXAGON_MACH_V3);
458 BCase(EF_HEXAGON_MACH_V4);
459 BCase(EF_HEXAGON_MACH_V5);
460 BCase(EF_HEXAGON_MACH_V55);
461 BCase(EF_HEXAGON_MACH_V60);
462 BCase(EF_HEXAGON_MACH_V62);
463 BCase(EF_HEXAGON_MACH_V65);
464 BCase(EF_HEXAGON_MACH_V66);
465 BCase(EF_HEXAGON_MACH_V67);
466 BCase(EF_HEXAGON_MACH_V67T);
467 BCase(EF_HEXAGON_MACH_V68);
468 BCase(EF_HEXAGON_ISA_V2);
469 BCase(EF_HEXAGON_ISA_V3);
470 BCase(EF_HEXAGON_ISA_V4);
471 BCase(EF_HEXAGON_ISA_V5);
472 BCase(EF_HEXAGON_ISA_V55);
473 BCase(EF_HEXAGON_ISA_V60);
474 BCase(EF_HEXAGON_ISA_V62);
475 BCase(EF_HEXAGON_ISA_V65);
476 BCase(EF_HEXAGON_ISA_V66);
477 BCase(EF_HEXAGON_ISA_V67);
478 BCase(EF_HEXAGON_ISA_V68);
479 break;
480 case ELF::EM_AVR:
481 BCaseMask(EF_AVR_ARCH_AVR1, EF_AVR_ARCH_MASK);
482 BCaseMask(EF_AVR_ARCH_AVR2, EF_AVR_ARCH_MASK);
483 BCaseMask(EF_AVR_ARCH_AVR25, EF_AVR_ARCH_MASK);
484 BCaseMask(EF_AVR_ARCH_AVR3, EF_AVR_ARCH_MASK);
485 BCaseMask(EF_AVR_ARCH_AVR31, EF_AVR_ARCH_MASK);
486 BCaseMask(EF_AVR_ARCH_AVR35, EF_AVR_ARCH_MASK);
487 BCaseMask(EF_AVR_ARCH_AVR4, EF_AVR_ARCH_MASK);
488 BCaseMask(EF_AVR_ARCH_AVR5, EF_AVR_ARCH_MASK);
489 BCaseMask(EF_AVR_ARCH_AVR51, EF_AVR_ARCH_MASK);
490 BCaseMask(EF_AVR_ARCH_AVR6, EF_AVR_ARCH_MASK);
491 BCaseMask(EF_AVR_ARCH_AVRTINY, EF_AVR_ARCH_MASK);
492 BCaseMask(EF_AVR_ARCH_XMEGA1, EF_AVR_ARCH_MASK);
493 BCaseMask(EF_AVR_ARCH_XMEGA2, EF_AVR_ARCH_MASK);
494 BCaseMask(EF_AVR_ARCH_XMEGA3, EF_AVR_ARCH_MASK);
495 BCaseMask(EF_AVR_ARCH_XMEGA4, EF_AVR_ARCH_MASK);
496 BCaseMask(EF_AVR_ARCH_XMEGA5, EF_AVR_ARCH_MASK);
497 BCaseMask(EF_AVR_ARCH_XMEGA6, EF_AVR_ARCH_MASK);
498 BCaseMask(EF_AVR_ARCH_XMEGA7, EF_AVR_ARCH_MASK);
499 BCase(EF_AVR_LINKRELAX_PREPARED);
500 break;
501 case ELF::EM_RISCV:
502 BCase(EF_RISCV_RVC);
503 BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI);
504 BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI);
505 BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI);
506 BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI);
507 BCase(EF_RISCV_RVE);
508 break;
509 case ELF::EM_AMDGPU:
510 BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH);
511 BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH);
512 BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH);
513 BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH);
514 BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH);
515 BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH);
516 BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH);
517 BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH);
518 BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH);
519 BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH);
520 BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH);
521 BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH);
522 BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH);
523 BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH);
524 BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH);
525 BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH);
526 BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH);
527 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH);
528 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH);
529 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX602, EF_AMDGPU_MACH);
530 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH);
531 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH);
532 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH);
533 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH);
534 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH);
535 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX705, EF_AMDGPU_MACH);
536 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH);
537 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH);
538 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH);
539 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX805, EF_AMDGPU_MACH);
540 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH);
541 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH);
542 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH);
543 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH);
544 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH);
545 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH);
546 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH);
547 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90A, EF_AMDGPU_MACH);
548 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90C, EF_AMDGPU_MACH);
549 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH);
550 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH);
551 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH);
552 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1013, EF_AMDGPU_MACH);
553 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1030, EF_AMDGPU_MACH);
554 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1031, EF_AMDGPU_MACH);
555 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1032, EF_AMDGPU_MACH);
556 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1033, EF_AMDGPU_MACH);
557 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1034, EF_AMDGPU_MACH);
558 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1035, EF_AMDGPU_MACH);
559 switch (Object->Header.ABIVersion) {
560 default:
561 // ELFOSABI_AMDGPU_PAL, ELFOSABI_AMDGPU_MESA3D support *_V3 flags.
562 LLVM_FALLTHROUGH[[gnu::fallthrough]];
563 case ELF::ELFABIVERSION_AMDGPU_HSA_V3:
564 BCase(EF_AMDGPU_FEATURE_XNACK_V3);
565 BCase(EF_AMDGPU_FEATURE_SRAMECC_V3);
566 break;
567 case ELF::ELFABIVERSION_AMDGPU_HSA_V4:
568 BCaseMask(EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4,
569 EF_AMDGPU_FEATURE_XNACK_V4);
570 BCaseMask(EF_AMDGPU_FEATURE_XNACK_ANY_V4,
571 EF_AMDGPU_FEATURE_XNACK_V4);
572 BCaseMask(EF_AMDGPU_FEATURE_XNACK_OFF_V4,
573 EF_AMDGPU_FEATURE_XNACK_V4);
574 BCaseMask(EF_AMDGPU_FEATURE_XNACK_ON_V4,
575 EF_AMDGPU_FEATURE_XNACK_V4);
576 BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4,
577 EF_AMDGPU_FEATURE_SRAMECC_V4);
578 BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ANY_V4,
579 EF_AMDGPU_FEATURE_SRAMECC_V4);
580 BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_OFF_V4,
581 EF_AMDGPU_FEATURE_SRAMECC_V4);
582 BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ON_V4,
583 EF_AMDGPU_FEATURE_SRAMECC_V4);
584 break;
585 }
586 break;
587 default:
588 break;
589 }
590#undef BCase
591#undef BCaseMask
592}
593
594void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
595 IO &IO, ELFYAML::ELF_SHT &Value) {
596 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
597 assert(Object && "The IO context is not initialized")(static_cast<void> (0));
598#define ECase(X) IO.enumCase(Value, #X, ELF::X)
599 ECase(SHT_NULL);
14
Calling 'IO::enumCase'
600 ECase(SHT_PROGBITS);
601 ECase(SHT_SYMTAB);
602 // FIXME: Issue a diagnostic with this information.
603 ECase(SHT_STRTAB);
604 ECase(SHT_RELA);
605 ECase(SHT_HASH);
606 ECase(SHT_DYNAMIC);
607 ECase(SHT_NOTE);
608 ECase(SHT_NOBITS);
609 ECase(SHT_REL);
610 ECase(SHT_SHLIB);
611 ECase(SHT_DYNSYM);
612 ECase(SHT_INIT_ARRAY);
613 ECase(SHT_FINI_ARRAY);
614 ECase(SHT_PREINIT_ARRAY);
615 ECase(SHT_GROUP);
616 ECase(SHT_SYMTAB_SHNDX);
617 ECase(SHT_RELR);
618 ECase(SHT_ANDROID_REL);
619 ECase(SHT_ANDROID_RELA);
620 ECase(SHT_ANDROID_RELR);
621 ECase(SHT_LLVM_ODRTAB);
622 ECase(SHT_LLVM_LINKER_OPTIONS);
623 ECase(SHT_LLVM_CALL_GRAPH_PROFILE);
624 ECase(SHT_LLVM_ADDRSIG);
625 ECase(SHT_LLVM_DEPENDENT_LIBRARIES);
626 ECase(SHT_LLVM_SYMPART);
627 ECase(SHT_LLVM_PART_EHDR);
628 ECase(SHT_LLVM_PART_PHDR);
629 ECase(SHT_LLVM_BB_ADDR_MAP);
630 ECase(SHT_GNU_ATTRIBUTES);
631 ECase(SHT_GNU_HASH);
632 ECase(SHT_GNU_verdef);
633 ECase(SHT_GNU_verneed);
634 ECase(SHT_GNU_versym);
635 switch (Object->getMachine()) {
636 case ELF::EM_ARM:
637 ECase(SHT_ARM_EXIDX);
638 ECase(SHT_ARM_PREEMPTMAP);
639 ECase(SHT_ARM_ATTRIBUTES);
640 ECase(SHT_ARM_DEBUGOVERLAY);
641 ECase(SHT_ARM_OVERLAYSECTION);
642 break;
643 case ELF::EM_HEXAGON:
644 ECase(SHT_HEX_ORDERED);
645 break;
646 case ELF::EM_X86_64:
647 ECase(SHT_X86_64_UNWIND);
648 break;
649 case ELF::EM_MIPS:
650 ECase(SHT_MIPS_REGINFO);
651 ECase(SHT_MIPS_OPTIONS);
652 ECase(SHT_MIPS_DWARF);
653 ECase(SHT_MIPS_ABIFLAGS);
654 break;
655 case ELF::EM_RISCV:
656 ECase(SHT_RISCV_ATTRIBUTES);
657 break;
658 default:
659 // Nothing to do.
660 break;
661 }
662#undef ECase
663 IO.enumFallback<Hex32>(Value);
664}
665
666void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO,
667 ELFYAML::ELF_PF &Value) {
668#define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
669 BCase(PF_X);
670 BCase(PF_W);
671 BCase(PF_R);
672}
673
674void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
675 ELFYAML::ELF_SHF &Value) {
676 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
677#define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
678 BCase(SHF_WRITE);
679 BCase(SHF_ALLOC);
680 BCase(SHF_EXCLUDE);
681 BCase(SHF_EXECINSTR);
682 BCase(SHF_MERGE);
683 BCase(SHF_STRINGS);
684 BCase(SHF_INFO_LINK);
685 BCase(SHF_LINK_ORDER);
686 BCase(SHF_OS_NONCONFORMING);
687 BCase(SHF_GROUP);
688 BCase(SHF_TLS);
689 BCase(SHF_COMPRESSED);
690 BCase(SHF_GNU_RETAIN);
691 switch (Object->getMachine()) {
692 case ELF::EM_ARM:
693 BCase(SHF_ARM_PURECODE);
694 break;
695 case ELF::EM_HEXAGON:
696 BCase(SHF_HEX_GPREL);
697 break;
698 case ELF::EM_MIPS:
699 BCase(SHF_MIPS_NODUPES);
700 BCase(SHF_MIPS_NAMES);
701 BCase(SHF_MIPS_LOCAL);
702 BCase(SHF_MIPS_NOSTRIP);
703 BCase(SHF_MIPS_GPREL);
704 BCase(SHF_MIPS_MERGE);
705 BCase(SHF_MIPS_ADDR);
706 BCase(SHF_MIPS_STRING);
707 break;
708 case ELF::EM_X86_64:
709 BCase(SHF_X86_64_LARGE);
710 break;
711 default:
712 // Nothing to do.
713 break;
714 }
715#undef BCase
716}
717
718void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration(
719 IO &IO, ELFYAML::ELF_SHN &Value) {
720#define ECase(X) IO.enumCase(Value, #X, ELF::X)
721 ECase(SHN_UNDEF);
722 ECase(SHN_LORESERVE);
723 ECase(SHN_LOPROC);
724 ECase(SHN_HIPROC);
725 ECase(SHN_LOOS);
726 ECase(SHN_HIOS);
727 ECase(SHN_ABS);
728 ECase(SHN_COMMON);
729 ECase(SHN_XINDEX);
730 ECase(SHN_HIRESERVE);
731 ECase(SHN_AMDGPU_LDS);
732 ECase(SHN_HEXAGON_SCOMMON);
733 ECase(SHN_HEXAGON_SCOMMON_1);
734 ECase(SHN_HEXAGON_SCOMMON_2);
735 ECase(SHN_HEXAGON_SCOMMON_4);
736 ECase(SHN_HEXAGON_SCOMMON_8);
737#undef ECase
738 IO.enumFallback<Hex16>(Value);
739}
740
741void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration(
742 IO &IO, ELFYAML::ELF_STB &Value) {
743#define ECase(X) IO.enumCase(Value, #X, ELF::X)
744 ECase(STB_LOCAL);
745 ECase(STB_GLOBAL);
746 ECase(STB_WEAK);
747 ECase(STB_GNU_UNIQUE);
748#undef ECase
749 IO.enumFallback<Hex8>(Value);
750}
751
752void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
753 IO &IO, ELFYAML::ELF_STT &Value) {
754#define ECase(X) IO.enumCase(Value, #X, ELF::X)
755 ECase(STT_NOTYPE);
756 ECase(STT_OBJECT);
757 ECase(STT_FUNC);
758 ECase(STT_SECTION);
759 ECase(STT_FILE);
760 ECase(STT_COMMON);
761 ECase(STT_TLS);
762 ECase(STT_GNU_IFUNC);
763#undef ECase
764 IO.enumFallback<Hex8>(Value);
765}
766
767
768void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
769 IO &IO, ELFYAML::ELF_RSS &Value) {
770#define ECase(X) IO.enumCase(Value, #X, ELF::X)
771 ECase(RSS_UNDEF);
772 ECase(RSS_GP);
773 ECase(RSS_GP0);
774 ECase(RSS_LOC);
775#undef ECase
776}
777
778void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
779 IO &IO, ELFYAML::ELF_REL &Value) {
780 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
781 assert(Object && "The IO context is not initialized")(static_cast<void> (0));
782#define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
783 switch (Object->getMachine()) {
784 case ELF::EM_X86_64:
785#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
786 break;
787 case ELF::EM_MIPS:
788#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
789 break;
790 case ELF::EM_HEXAGON:
791#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
792 break;
793 case ELF::EM_386:
794 case ELF::EM_IAMCU:
795#include "llvm/BinaryFormat/ELFRelocs/i386.def"
796 break;
797 case ELF::EM_AARCH64:
798#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
799 break;
800 case ELF::EM_ARM:
801#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
802 break;
803 case ELF::EM_ARC:
804#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
805 break;
806 case ELF::EM_RISCV:
807#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
808 break;
809 case ELF::EM_LANAI:
810#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
811 break;
812 case ELF::EM_AMDGPU:
813#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
814 break;
815 case ELF::EM_BPF:
816#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
817 break;
818 case ELF::EM_VE:
819#include "llvm/BinaryFormat/ELFRelocs/VE.def"
820 break;
821 case ELF::EM_CSKY:
822#include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
823 break;
824 case ELF::EM_PPC64:
825#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
826 break;
827 case ELF::EM_68K:
828#include "llvm/BinaryFormat/ELFRelocs/M68k.def"
829 break;
830 default:
831 // Nothing to do.
832 break;
833 }
834#undef ELF_RELOC
835 IO.enumFallback<Hex32>(Value);
836}
837
838void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration(
839 IO &IO, ELFYAML::ELF_DYNTAG &Value) {
840 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
841 assert(Object && "The IO context is not initialized")(static_cast<void> (0));
842
843// Disable architecture specific tags by default. We might enable them below.
844#define AARCH64_DYNAMIC_TAG(name, value)
845#define MIPS_DYNAMIC_TAG(name, value)
846#define HEXAGON_DYNAMIC_TAG(name, value)
847#define PPC_DYNAMIC_TAG(name, value)
848#define PPC64_DYNAMIC_TAG(name, value)
849// Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
850#define DYNAMIC_TAG_MARKER(name, value)
851
852#define STRINGIFY(X) (#X)
853#define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
854 switch (Object->getMachine()) {
855 case ELF::EM_AARCH64:
856#undef AARCH64_DYNAMIC_TAG
857#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
858#include "llvm/BinaryFormat/DynamicTags.def"
859#undef AARCH64_DYNAMIC_TAG
860#define AARCH64_DYNAMIC_TAG(name, value)
861 break;
862 case ELF::EM_MIPS:
863#undef MIPS_DYNAMIC_TAG
864#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
865#include "llvm/BinaryFormat/DynamicTags.def"
866#undef MIPS_DYNAMIC_TAG
867#define MIPS_DYNAMIC_TAG(name, value)
868 break;
869 case ELF::EM_HEXAGON:
870#undef HEXAGON_DYNAMIC_TAG
871#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
872#include "llvm/BinaryFormat/DynamicTags.def"
873#undef HEXAGON_DYNAMIC_TAG
874#define HEXAGON_DYNAMIC_TAG(name, value)
875 break;
876 case ELF::EM_PPC:
877#undef PPC_DYNAMIC_TAG
878#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
879#include "llvm/BinaryFormat/DynamicTags.def"
880#undef PPC_DYNAMIC_TAG
881#define PPC_DYNAMIC_TAG(name, value)
882 break;
883 case ELF::EM_PPC64:
884#undef PPC64_DYNAMIC_TAG
885#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
886#include "llvm/BinaryFormat/DynamicTags.def"
887#undef PPC64_DYNAMIC_TAG
888#define PPC64_DYNAMIC_TAG(name, value)
889 break;
890 default:
891#include "llvm/BinaryFormat/DynamicTags.def"
892 break;
893 }
894#undef AARCH64_DYNAMIC_TAG
895#undef MIPS_DYNAMIC_TAG
896#undef HEXAGON_DYNAMIC_TAG
897#undef PPC_DYNAMIC_TAG
898#undef PPC64_DYNAMIC_TAG
899#undef DYNAMIC_TAG_MARKER
900#undef STRINGIFY
901#undef DYNAMIC_TAG
902
903 IO.enumFallback<Hex64>(Value);
904}
905
906void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
907 IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
908#define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
909 ECase(REG_NONE);
910 ECase(REG_32);
911 ECase(REG_64);
912 ECase(REG_128);
913#undef ECase
914}
915
916void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
917 IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
918#define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
919 ECase(FP_ANY);
920 ECase(FP_DOUBLE);
921 ECase(FP_SINGLE);
922 ECase(FP_SOFT);
923 ECase(FP_OLD_64);
924 ECase(FP_XX);
925 ECase(FP_64);
926 ECase(FP_64A);
927#undef ECase
928}
929
930void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
931 IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
932#define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
933 ECase(EXT_NONE);
934 ECase(EXT_XLR);
935 ECase(EXT_OCTEON2);
936 ECase(EXT_OCTEONP);
937 ECase(EXT_LOONGSON_3A);
938 ECase(EXT_OCTEON);
939 ECase(EXT_5900);
940 ECase(EXT_4650);
941 ECase(EXT_4010);
942 ECase(EXT_4100);
943 ECase(EXT_3900);
944 ECase(EXT_10000);
945 ECase(EXT_SB1);
946 ECase(EXT_4111);
947 ECase(EXT_4120);
948 ECase(EXT_5400);
949 ECase(EXT_5500);
950 ECase(EXT_LOONGSON_2E);
951 ECase(EXT_LOONGSON_2F);
952 ECase(EXT_OCTEON3);
953#undef ECase
954}
955
956void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
957 IO &IO, ELFYAML::MIPS_ISA &Value) {
958 IO.enumCase(Value, "MIPS1", 1);
959 IO.enumCase(Value, "MIPS2", 2);
960 IO.enumCase(Value, "MIPS3", 3);
961 IO.enumCase(Value, "MIPS4", 4);
962 IO.enumCase(Value, "MIPS5", 5);
963 IO.enumCase(Value, "MIPS32", 32);
964 IO.enumCase(Value, "MIPS64", 64);
965 IO.enumFallback<Hex32>(Value);
966}
967
968void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
969 IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
970#define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
971 BCase(DSP);
972 BCase(DSPR2);
973 BCase(EVA);
974 BCase(MCU);
975 BCase(MDMX);
976 BCase(MIPS3D);
977 BCase(MT);
978 BCase(SMARTMIPS);
979 BCase(VIRT);
980 BCase(MSA);
981 BCase(MIPS16);
982 BCase(MICROMIPS);
983 BCase(XPA);
984 BCase(CRC);
985 BCase(GINV);
986#undef BCase
987}
988
989void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
990 IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
991#define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
992 BCase(ODDSPREG);
993#undef BCase
994}
995
996void MappingTraits<ELFYAML::SectionHeader>::mapping(
997 IO &IO, ELFYAML::SectionHeader &SHdr) {
998 IO.mapRequired("Name", SHdr.Name);
999}
1000
1001void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
1002 ELFYAML::FileHeader &FileHdr) {
1003 IO.mapRequired("Class", FileHdr.Class);
1004 IO.mapRequired("Data", FileHdr.Data);
1005 IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
1006 IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
1007 IO.mapRequired("Type", FileHdr.Type);
1008 IO.mapOptional("Machine", FileHdr.Machine);
1009 IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
1010 IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
1011 IO.mapOptional("SectionHeaderStringTable", FileHdr.SectionHeaderStringTable);
1012
1013 // obj2yaml does not dump these fields.
1014 assert(!IO.outputting() ||(static_cast<void> (0))
1015 (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum))(static_cast<void> (0));
1016 IO.mapOptional("EPhOff", FileHdr.EPhOff);
1017 IO.mapOptional("EPhEntSize", FileHdr.EPhEntSize);
1018 IO.mapOptional("EPhNum", FileHdr.EPhNum);
1019 IO.mapOptional("EShEntSize", FileHdr.EShEntSize);
1020 IO.mapOptional("EShOff", FileHdr.EShOff);
1021 IO.mapOptional("EShNum", FileHdr.EShNum);
1022 IO.mapOptional("EShStrNdx", FileHdr.EShStrNdx);
1023}
1024
1025void MappingTraits<ELFYAML::ProgramHeader>::mapping(
1026 IO &IO, ELFYAML::ProgramHeader &Phdr) {
1027 IO.mapRequired("Type", Phdr.Type);
1028 IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
1029 IO.mapOptional("FirstSec", Phdr.FirstSec);
1030 IO.mapOptional("LastSec", Phdr.LastSec);
1031 IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
1032 IO.mapOptional("PAddr", Phdr.PAddr, Phdr.VAddr);
1033 IO.mapOptional("Align", Phdr.Align);
1034 IO.mapOptional("FileSize", Phdr.FileSize);
1035 IO.mapOptional("MemSize", Phdr.MemSize);
1036 IO.mapOptional("Offset", Phdr.Offset);
1037}
1038
1039std::string MappingTraits<ELFYAML::ProgramHeader>::validate(
1040 IO &IO, ELFYAML::ProgramHeader &FileHdr) {
1041 if (!FileHdr.FirstSec && FileHdr.LastSec)
1042 return "the \"LastSec\" key can't be used without the \"FirstSec\" key";
1043 if (FileHdr.FirstSec && !FileHdr.LastSec)
1044 return "the \"FirstSec\" key can't be used without the \"LastSec\" key";
1045 return "";
1046}
1047
1048LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece)struct StOtherPiece { StOtherPiece() = default; StOtherPiece(
const StringRef v) : value(v) {} StOtherPiece(const StOtherPiece
&v) = default; StOtherPiece &operator=(const StOtherPiece
&rhs) = default; StOtherPiece &operator=(const StringRef
&rhs) { value = rhs; return *this; } operator const StringRef
& () const { return value; } bool operator==(const StOtherPiece
&rhs) const { return value == rhs.value; } bool operator
==(const StringRef &rhs) const { return value == rhs; } bool
operator<(const StOtherPiece &rhs) const { return value
< rhs.value; } StringRef value; using BaseType = StringRef
; };
1049
1050template <> struct ScalarTraits<StOtherPiece> {
1051 static void output(const StOtherPiece &Val, void *, raw_ostream &Out) {
1052 Out << Val;
1053 }
1054 static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) {
1055 Val = Scalar;
1056 return {};
1057 }
1058 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1059};
1060template <> struct SequenceElementTraits<StOtherPiece> {
1061 static const bool flow = true;
1062};
1063
1064template <> struct ScalarTraits<ELFYAML::YAMLFlowString> {
1065 static void output(const ELFYAML::YAMLFlowString &Val, void *,
1066 raw_ostream &Out) {
1067 Out << Val;
1068 }
1069 static StringRef input(StringRef Scalar, void *,
1070 ELFYAML::YAMLFlowString &Val) {
1071 Val = Scalar;
1072 return {};
1073 }
1074 static QuotingType mustQuote(StringRef S) {
1075 return ScalarTraits<StringRef>::mustQuote(S);
1076 }
1077};
1078template <> struct SequenceElementTraits<ELFYAML::YAMLFlowString> {
1079 static const bool flow = true;
1080};
1081
1082namespace {
1083
1084struct NormalizedOther {
1085 NormalizedOther(IO &IO) : YamlIO(IO) {}
1086 NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) {
1087 assert(Original && "This constructor is only used for outputting YAML and "(static_cast<void> (0))
1088 "assumes a non-empty Original")(static_cast<void> (0));
1089 std::vector<StOtherPiece> Ret;
1090 const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
1091 for (std::pair<StringRef, uint8_t> &P :
1092 getFlags(Object->getMachine()).takeVector()) {
1093 uint8_t FlagValue = P.second;
1094 if ((*Original & FlagValue) != FlagValue)
1095 continue;
1096 *Original &= ~FlagValue;
1097 Ret.push_back({P.first});
1098 }
1099
1100 if (*Original != 0) {
1101 UnknownFlagsHolder = std::to_string(*Original);
1102 Ret.push_back({UnknownFlagsHolder});
1103 }
1104
1105 if (!Ret.empty())
1106 Other = std::move(Ret);
1107 }
1108
1109 uint8_t toValue(StringRef Name) {
1110 const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
1111 MapVector<StringRef, uint8_t> Flags = getFlags(Object->getMachine());
1112
1113 auto It = Flags.find(Name);
1114 if (It != Flags.end())
1115 return It->second;
1116
1117 uint8_t Val;
1118 if (to_integer(Name, Val))
1119 return Val;
1120
1121 YamlIO.setError("an unknown value is used for symbol's 'Other' field: " +
1122 Name);
1123 return 0;
1124 }
1125
1126 Optional<uint8_t> denormalize(IO &) {
1127 if (!Other)
1128 return None;
1129 uint8_t Ret = 0;
1130 for (StOtherPiece &Val : *Other)
1131 Ret |= toValue(Val);
1132 return Ret;
1133 }
1134
1135 // st_other field is used to encode symbol visibility and platform-dependent
1136 // flags and values. This method returns a name to value map that is used for
1137 // parsing and encoding this field.
1138 MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) {
1139 MapVector<StringRef, uint8_t> Map;
1140 // STV_* values are just enumeration values. We add them in a reversed order
1141 // because when we convert the st_other to named constants when printing
1142 // YAML we want to use a maximum number of bits on each step:
1143 // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but
1144 // not as STV_HIDDEN (2) + STV_INTERNAL (1).
1145 Map["STV_PROTECTED"] = ELF::STV_PROTECTED;
1146 Map["STV_HIDDEN"] = ELF::STV_HIDDEN;
1147 Map["STV_INTERNAL"] = ELF::STV_INTERNAL;
1148 // STV_DEFAULT is used to represent the default visibility and has a value
1149 // 0. We want to be able to read it from YAML documents, but there is no
1150 // reason to print it.
1151 if (!YamlIO.outputting())
1152 Map["STV_DEFAULT"] = ELF::STV_DEFAULT;
1153
1154 // MIPS is not consistent. All of the STO_MIPS_* values are bit flags,
1155 // except STO_MIPS_MIPS16 which overlaps them. It should be checked and
1156 // consumed first when we print the output, because we do not want to print
1157 // any other flags that have the same bits instead.
1158 if (EMachine == ELF::EM_MIPS) {
1159 Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16;
1160 Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS;
1161 Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC;
1162 Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT;
1163 Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL;
1164 }
1165
1166 if (EMachine == ELF::EM_AARCH64)
1167 Map["STO_AARCH64_VARIANT_PCS"] = ELF::STO_AARCH64_VARIANT_PCS;
1168 return Map;
1169 }
1170
1171 IO &YamlIO;
1172 Optional<std::vector<StOtherPiece>> Other;
1173 std::string UnknownFlagsHolder;
1174};
1175
1176} // end anonymous namespace
1177
1178void ScalarTraits<ELFYAML::YAMLIntUInt>::output(const ELFYAML::YAMLIntUInt &Val,
1179 void *Ctx, raw_ostream &Out) {
1180 Out << Val;
1181}
1182
1183StringRef ScalarTraits<ELFYAML::YAMLIntUInt>::input(StringRef Scalar, void *Ctx,
1184 ELFYAML::YAMLIntUInt &Val) {
1185 const bool Is64 = static_cast<ELFYAML::Object *>(Ctx)->Header.Class ==
1186 ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1187 StringRef ErrMsg = "invalid number";
1188 // We do not accept negative hex numbers because their meaning is ambiguous.
1189 // For example, would -0xfffffffff mean 1 or INT32_MIN?
1190 if (Scalar.empty() || Scalar.startswith("-0x"))
1191 return ErrMsg;
1192
1193 if (Scalar.startswith("-")) {
1194 const int64_t MinVal = Is64 ? INT64_MIN(-9223372036854775807L -1) : INT32_MIN(-2147483647-1);
1195 long long Int;
1196 if (getAsSignedInteger(Scalar, /*Radix=*/0, Int) || (Int < MinVal))
1197 return ErrMsg;
1198 Val = Int;
1199 return "";
1200 }
1201
1202 const uint64_t MaxVal = Is64 ? UINT64_MAX(18446744073709551615UL) : UINT32_MAX(4294967295U);
1203 unsigned long long UInt;
1204 if (getAsUnsignedInteger(Scalar, /*Radix=*/0, UInt) || (UInt > MaxVal))
1205 return ErrMsg;
1206 Val = UInt;
1207 return "";
1208}
1209
1210void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
1211 IO.mapOptional("Name", Symbol.Name, StringRef());
1212 IO.mapOptional("StName", Symbol.StName);
1213 IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
1214 IO.mapOptional("Section", Symbol.Section);
1215 IO.mapOptional("Index", Symbol.Index);
1216 IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
1217 IO.mapOptional("Value", Symbol.Value);
1218 IO.mapOptional("Size", Symbol.Size);
1219
1220 // Symbol's Other field is a bit special. It is usually a field that
1221 // represents st_other and holds the symbol visibility. However, on some
1222 // platforms, it can contain bit fields and regular values, or even sometimes a
1223 // crazy mix of them (see comments for NormalizedOther). Because of this, we
1224 // need special handling.
1225 MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO,
1226 Symbol.Other);
1227 IO.mapOptional("Other", Keys->Other);
1228}
1229
1230std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
1231 ELFYAML::Symbol &Symbol) {
1232 if (Symbol.Index && Symbol.Section)
1233 return "Index and Section cannot both be specified for Symbol";
1234 return "";
1235}
1236
1237static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
1238 IO.mapOptional("Name", Section.Name, StringRef());
1239 IO.mapRequired("Type", Section.Type);
1240 IO.mapOptional("Flags", Section.Flags);
1241 IO.mapOptional("Address", Section.Address);
1242 IO.mapOptional("Link", Section.Link);
1243 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
1244 IO.mapOptional("EntSize", Section.EntSize);
1245 IO.mapOptional("Offset", Section.Offset);
1246
1247 IO.mapOptional("Content", Section.Content);
1248 IO.mapOptional("Size", Section.Size);
1249
1250 // obj2yaml does not dump these fields. They are expected to be empty when we
1251 // are producing YAML, because yaml2obj sets appropriate values for them
1252 // automatically when they are not explicitly defined.
1253 assert(!IO.outputting() ||(static_cast<void> (0))
1254 (!Section.ShOffset && !Section.ShSize && !Section.ShName &&(static_cast<void> (0))
1255 !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign))(static_cast<void> (0));
1256 IO.mapOptional("ShAddrAlign", Section.ShAddrAlign);
1257 IO.mapOptional("ShName", Section.ShName);
1258 IO.mapOptional("ShOffset", Section.ShOffset);
1259 IO.mapOptional("ShSize", Section.ShSize);
1260 IO.mapOptional("ShFlags", Section.ShFlags);
1261 IO.mapOptional("ShType", Section.ShType);
1262}
1263
1264static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
1265 commonSectionMapping(IO, Section);
1266 IO.mapOptional("Entries", Section.Entries);
1267}
1268
1269static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
1270 commonSectionMapping(IO, Section);
1271
1272 // We also support reading a content as array of bytes using the ContentArray
1273 // key. obj2yaml never prints this field.
1274 assert(!IO.outputting() || !Section.ContentBuf.hasValue())(static_cast<void> (0));
1275 IO.mapOptional("ContentArray", Section.ContentBuf);
1276 if (Section.ContentBuf) {
1277 if (Section.Content)
1278 IO.setError("Content and ContentArray can't be used together");
1279 Section.Content = yaml::BinaryRef(*Section.ContentBuf);
1280 }
1281
1282 IO.mapOptional("Info", Section.Info);
1283}
1284
1285static void sectionMapping(IO &IO, ELFYAML::BBAddrMapSection &Section) {
1286 commonSectionMapping(IO, Section);
1287 IO.mapOptional("Content", Section.Content);
1288 IO.mapOptional("Entries", Section.Entries);
1289}
1290
1291static void sectionMapping(IO &IO, ELFYAML::StackSizesSection &Section) {
1292 commonSectionMapping(IO, Section);
1293 IO.mapOptional("Entries", Section.Entries);
1294}
1295
1296static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) {
1297 commonSectionMapping(IO, Section);
1298 IO.mapOptional("Bucket", Section.Bucket);
1299 IO.mapOptional("Chain", Section.Chain);
1300
1301 // obj2yaml does not dump these fields. They can be used to override nchain
1302 // and nbucket values for creating broken sections.
1303 assert(!IO.outputting() ||(static_cast<void> (0))
1304 (!Section.NBucket.hasValue() && !Section.NChain.hasValue()))(static_cast<void> (0));
1305 IO.mapOptional("NChain", Section.NChain);
1306 IO.mapOptional("NBucket", Section.NBucket);
1307}
1308
1309static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) {
1310 commonSectionMapping(IO, Section);
1311 IO.mapOptional("Notes", Section.Notes);
1312}
1313
1314
1315static void sectionMapping(IO &IO, ELFYAML::GnuHashSection &Section) {
1316 commonSectionMapping(IO, Section);
1317 IO.mapOptional("Header", Section.Header);
1318 IO.mapOptional("BloomFilter", Section.BloomFilter);
1319 IO.mapOptional("HashBuckets", Section.HashBuckets);
1320 IO.mapOptional("HashValues", Section.HashValues);
1321}
1322static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
1323 commonSectionMapping(IO, Section);
1324}
1325
1326static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) {
1327 commonSectionMapping(IO, Section);
1328 IO.mapOptional("Info", Section.Info);
1329 IO.mapOptional("Entries", Section.Entries);
1330}
1331
1332static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) {
1333 commonSectionMapping(IO, Section);
1334 IO.mapOptional("Entries", Section.Entries);
1335}
1336
1337static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) {
1338 commonSectionMapping(IO, Section);
1339 IO.mapOptional("Info", Section.Info);
1340 IO.mapOptional("Dependencies", Section.VerneedV);
1341}
1342
1343static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
1344 commonSectionMapping(IO, Section);
1345 IO.mapOptional("Info", Section.RelocatableSec, StringRef());
1346 IO.mapOptional("Relocations", Section.Relocations);
1347}
1348
1349static void sectionMapping(IO &IO, ELFYAML::RelrSection &Section) {
1350 commonSectionMapping(IO, Section);
1351 IO.mapOptional("Entries", Section.Entries);
1352}
1353
1354static void groupSectionMapping(IO &IO, ELFYAML::GroupSection &Group) {
1355 commonSectionMapping(IO, Group);
1356 IO.mapOptional("Info", Group.Signature);
1357 IO.mapOptional("Members", Group.Members);
1358}
1359
1360static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) {
1361 commonSectionMapping(IO, Section);
1362 IO.mapOptional("Entries", Section.Entries);
1363}
1364
1365static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) {
1366 commonSectionMapping(IO, Section);
1367 IO.mapOptional("Symbols", Section.Symbols);
1368}
1369
1370static void fillMapping(IO &IO, ELFYAML::Fill &Fill) {
1371 IO.mapOptional("Name", Fill.Name, StringRef());
1372 IO.mapOptional("Pattern", Fill.Pattern);
1373 IO.mapOptional("Offset", Fill.Offset);
1374 IO.mapRequired("Size", Fill.Size);
1375}
1376
1377static void sectionHeaderTableMapping(IO &IO,
1378 ELFYAML::SectionHeaderTable &SHT) {
1379 IO.mapOptional("Offset", SHT.Offset);
1380 IO.mapOptional("Sections", SHT.Sections);
1381 IO.mapOptional("Excluded", SHT.Excluded);
1382 IO.mapOptional("NoHeaders", SHT.NoHeaders);
1383}
1384
1385static void sectionMapping(IO &IO, ELFYAML::LinkerOptionsSection &Section) {
1386 commonSectionMapping(IO, Section);
1387 IO.mapOptional("Options", Section.Options);
1388}
1389
1390static void sectionMapping(IO &IO,
1391 ELFYAML::DependentLibrariesSection &Section) {
1392 commonSectionMapping(IO, Section);
1393 IO.mapOptional("Libraries", Section.Libs);
1394}
1395
1396static void sectionMapping(IO &IO, ELFYAML::CallGraphProfileSection &Section) {
1397 commonSectionMapping(IO, Section);
1398 IO.mapOptional("Entries", Section.Entries);
1399}
1400
1401void MappingTraits<ELFYAML::SectionOrType>::mapping(
1402 IO &IO, ELFYAML::SectionOrType &sectionOrType) {
1403 IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
1404}
1405
1406static void sectionMapping(IO &IO, ELFYAML::ARMIndexTableSection &Section) {
1407 commonSectionMapping(IO, Section);
1408 IO.mapOptional("Entries", Section.Entries);
1409}
1410
1411static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
1412 commonSectionMapping(IO, Section);
1413 IO.mapOptional("Version", Section.Version, Hex16(0));
1414 IO.mapRequired("ISA", Section.ISALevel);
1415 IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
1416 IO.mapOptional("ISAExtension", Section.ISAExtension,
1417 ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
1418 IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
1419 IO.mapOptional("FpABI", Section.FpABI,
1420 ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
1421 IO.mapOptional("GPRSize", Section.GPRSize,
1422 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1423 IO.mapOptional("CPR1Size", Section.CPR1Size,
1424 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1425 IO.mapOptional("CPR2Size", Section.CPR2Size,
1426 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1427 IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
1428 IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
1429}
1430
1431static StringRef getStringValue(IO &IO, const char *Key) {
1432 StringRef Val;
1433 IO.mapRequired(Key, Val);
1434 return Val;
1435}
1436
1437static void setStringValue(IO &IO, const char *Key, StringRef Val) {
1438 IO.mapRequired(Key, Val);
1439}
1440
1441static bool isInteger(StringRef Val) {
1442 APInt Tmp;
1443 return !Val.getAsInteger(0, Tmp);
1444}
1445
1446void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
1447 IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
1448 ELFYAML::ELF_SHT Type;
1
Calling defaulted default constructor for 'ELF_SHT'
3
Returning from default constructor for 'ELF_SHT'
1449 StringRef TypeStr;
1450 if (IO.outputting()) {
4
Assuming the condition is false
5
Taking false branch
1451 if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
1452 Type = S->Type;
1453 else if (auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(Section.get()))
1454 TypeStr = SHT->TypeStr;
1455 } else {
1456 // When the Type string does not have a "SHT_" prefix, we know it is not a
1457 // description of a regular ELF output section.
1458 TypeStr = getStringValue(IO, "Type");
1459 if (TypeStr.startswith("SHT_") || isInteger(TypeStr))
6
Assuming the condition is true
7
Taking true branch
1460 IO.mapRequired("Type", Type);
8
Calling 'IO::mapRequired'
1461 }
1462
1463 if (TypeStr == "Fill") {
1464 assert(!IO.outputting())(static_cast<void> (0)); // We don't dump fills currently.
1465 Section.reset(new ELFYAML::Fill());
1466 fillMapping(IO, *cast<ELFYAML::Fill>(Section.get()));
1467 return;
1468 }
1469
1470 if (TypeStr == ELFYAML::SectionHeaderTable::TypeStr) {
1471 if (IO.outputting())
1472 setStringValue(IO, "Type", TypeStr);
1473 else
1474 Section.reset(new ELFYAML::SectionHeaderTable(/*IsImplicit=*/false));
1475
1476 sectionHeaderTableMapping(
1477 IO, *cast<ELFYAML::SectionHeaderTable>(Section.get()));
1478 return;
1479 }
1480
1481 const auto &Obj = *static_cast<ELFYAML::Object *>(IO.getContext());
1482 if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) {
1483 if (!IO.outputting())
1484 Section.reset(new ELFYAML::MipsABIFlags());
1485 sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
1486 return;
1487 }
1488
1489 if (Obj.getMachine() == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX) {
1490 if (!IO.outputting())
1491 Section.reset(new ELFYAML::ARMIndexTableSection());
1492 sectionMapping(IO, *cast<ELFYAML::ARMIndexTableSection>(Section.get()));
1493 return;
1494 }
1495
1496 switch (Type) {
1497 case ELF::SHT_DYNAMIC:
1498 if (!IO.outputting())
1499 Section.reset(new ELFYAML::DynamicSection());
1500 sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
1501 break;
1502 case ELF::SHT_REL:
1503 case ELF::SHT_RELA:
1504 if (!IO.outputting())
1505 Section.reset(new ELFYAML::RelocationSection());
1506 sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
1507 break;
1508 case ELF::SHT_RELR:
1509 if (!IO.outputting())
1510 Section.reset(new ELFYAML::RelrSection());
1511 sectionMapping(IO, *cast<ELFYAML::RelrSection>(Section.get()));
1512 break;
1513 case ELF::SHT_GROUP:
1514 if (!IO.outputting())
1515 Section.reset(new ELFYAML::GroupSection());
1516 groupSectionMapping(IO, *cast<ELFYAML::GroupSection>(Section.get()));
1517 break;
1518 case ELF::SHT_NOBITS:
1519 if (!IO.outputting())
1520 Section.reset(new ELFYAML::NoBitsSection());
1521 sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
1522 break;
1523 case ELF::SHT_HASH:
1524 if (!IO.outputting())
1525 Section.reset(new ELFYAML::HashSection());
1526 sectionMapping(IO, *cast<ELFYAML::HashSection>(Section.get()));
1527 break;
1528 case ELF::SHT_NOTE:
1529 if (!IO.outputting())
1530 Section.reset(new ELFYAML::NoteSection());
1531 sectionMapping(IO, *cast<ELFYAML::NoteSection>(Section.get()));
1532 break;
1533 case ELF::SHT_GNU_HASH:
1534 if (!IO.outputting())
1535 Section.reset(new ELFYAML::GnuHashSection());
1536 sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get()));
1537 break;
1538 case ELF::SHT_GNU_verdef:
1539 if (!IO.outputting())
1540 Section.reset(new ELFYAML::VerdefSection());
1541 sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
1542 break;
1543 case ELF::SHT_GNU_versym:
1544 if (!IO.outputting())
1545 Section.reset(new ELFYAML::SymverSection());
1546 sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
1547 break;
1548 case ELF::SHT_GNU_verneed:
1549 if (!IO.outputting())
1550 Section.reset(new ELFYAML::VerneedSection());
1551 sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
1552 break;
1553 case ELF::SHT_SYMTAB_SHNDX:
1554 if (!IO.outputting())
1555 Section.reset(new ELFYAML::SymtabShndxSection());
1556 sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get()));
1557 break;
1558 case ELF::SHT_LLVM_ADDRSIG:
1559 if (!IO.outputting())
1560 Section.reset(new ELFYAML::AddrsigSection());
1561 sectionMapping(IO, *cast<ELFYAML::AddrsigSection>(Section.get()));
1562 break;
1563 case ELF::SHT_LLVM_LINKER_OPTIONS:
1564 if (!IO.outputting())
1565 Section.reset(new ELFYAML::LinkerOptionsSection());
1566 sectionMapping(IO, *cast<ELFYAML::LinkerOptionsSection>(Section.get()));
1567 break;
1568 case ELF::SHT_LLVM_DEPENDENT_LIBRARIES:
1569 if (!IO.outputting())
1570 Section.reset(new ELFYAML::DependentLibrariesSection());
1571 sectionMapping(IO,
1572 *cast<ELFYAML::DependentLibrariesSection>(Section.get()));
1573 break;
1574 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1575 if (!IO.outputting())
1576 Section.reset(new ELFYAML::CallGraphProfileSection());
1577 sectionMapping(IO, *cast<ELFYAML::CallGraphProfileSection>(Section.get()));
1578 break;
1579 case ELF::SHT_LLVM_BB_ADDR_MAP:
1580 if (!IO.outputting())
1581 Section.reset(new ELFYAML::BBAddrMapSection());
1582 sectionMapping(IO, *cast<ELFYAML::BBAddrMapSection>(Section.get()));
1583 break;
1584 default:
1585 if (!IO.outputting()) {
1586 StringRef Name;
1587 IO.mapOptional("Name", Name, StringRef());
1588 Name = ELFYAML::dropUniqueSuffix(Name);
1589
1590 if (ELFYAML::StackSizesSection::nameMatches(Name))
1591 Section = std::make_unique<ELFYAML::StackSizesSection>();
1592 else
1593 Section = std::make_unique<ELFYAML::RawContentSection>();
1594 }
1595
1596 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Section.get()))
1597 sectionMapping(IO, *S);
1598 else
1599 sectionMapping(IO, *cast<ELFYAML::StackSizesSection>(Section.get()));
1600 }
1601}
1602
1603std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
1604 IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
1605 if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) {
1606 if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
1607 return "\"Size\" can't be 0 when \"Pattern\" is not empty";
1608 return "";
1609 }
1610
1611 if (const auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) {
1612 if (SHT->NoHeaders && (SHT->Sections || SHT->Excluded || SHT->Offset))
1613 return "NoHeaders can't be used together with Offset/Sections/Excluded";
1614 return "";
1615 }
1616
1617 const ELFYAML::Section &Sec = *cast<ELFYAML::Section>(C.get());
1618 if (Sec.Size && Sec.Content &&
1619 (uint64_t)(*Sec.Size) < Sec.Content->binary_size())
1620 return "Section size must be greater than or equal to the content size";
1621
1622 auto BuildErrPrefix = [](ArrayRef<std::pair<StringRef, bool>> EntV) {
1623 std::string Msg;
1624 for (size_t I = 0, E = EntV.size(); I != E; ++I) {
1625 StringRef Name = EntV[I].first;
1626 if (I == 0) {
1627 Msg = "\"" + Name.str() + "\"";
1628 continue;
1629 }
1630 if (I != EntV.size() - 1)
1631 Msg += ", \"" + Name.str() + "\"";
1632 else
1633 Msg += " and \"" + Name.str() + "\"";
1634 }
1635 return Msg;
1636 };
1637
1638 std::vector<std::pair<StringRef, bool>> Entries = Sec.getEntries();
1639 const size_t NumUsedEntries = llvm::count_if(
1640 Entries, [](const std::pair<StringRef, bool> &P) { return P.second; });
1641
1642 if ((Sec.Size || Sec.Content) && NumUsedEntries > 0)
1643 return BuildErrPrefix(Entries) +
1644 " cannot be used with \"Content\" or \"Size\"";
1645
1646 if (NumUsedEntries > 0 && Entries.size() != NumUsedEntries)
1647 return BuildErrPrefix(Entries) + " must be used together";
1648
1649 if (const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(C.get())) {
1650 if (RawSection->Flags && RawSection->ShFlags)
1651 return "ShFlags and Flags cannot be used together";
1652 return "";
1653 }
1654
1655 if (const auto *NB = dyn_cast<ELFYAML::NoBitsSection>(C.get())) {
1656 if (NB->Content)
1657 return "SHT_NOBITS section cannot have \"Content\"";
1658 return "";
1659 }
1660
1661 if (const auto *MF = dyn_cast<ELFYAML::MipsABIFlags>(C.get())) {
1662 if (MF->Content)
1663 return "\"Content\" key is not implemented for SHT_MIPS_ABIFLAGS "
1664 "sections";
1665 if (MF->Size)
1666 return "\"Size\" key is not implemented for SHT_MIPS_ABIFLAGS sections";
1667 return "";
1668 }
1669
1670 return "";
1671}
1672
1673namespace {
1674
1675struct NormalizedMips64RelType {
1676 NormalizedMips64RelType(IO &)
1677 : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1678 Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1679 Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1680 SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
1681 NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
1682 : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
1683 Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
1684
1685 ELFYAML::ELF_REL denormalize(IO &) {
1686 ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
1687 return Res;
1688 }
1689
1690 ELFYAML::ELF_REL Type;
1691 ELFYAML::ELF_REL Type2;
1692 ELFYAML::ELF_REL Type3;
1693 ELFYAML::ELF_RSS SpecSym;
1694};
1695
1696} // end anonymous namespace
1697
1698void MappingTraits<ELFYAML::StackSizeEntry>::mapping(
1699 IO &IO, ELFYAML::StackSizeEntry &E) {
1700 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1701 IO.mapOptional("Address", E.Address, Hex64(0));
1702 IO.mapRequired("Size", E.Size);
1703}
1704
1705void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping(
1706 IO &IO, ELFYAML::BBAddrMapEntry &E) {
1707 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1708 IO.mapOptional("Address", E.Address, Hex64(0));
1709 IO.mapOptional("NumBlocks", E.NumBlocks);
1710 IO.mapOptional("BBEntries", E.BBEntries);
1711}
1712
1713void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping(
1714 IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &E) {
1715 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1716 IO.mapRequired("AddressOffset", E.AddressOffset);
1717 IO.mapRequired("Size", E.Size);
1718 IO.mapRequired("Metadata", E.Metadata);
1719}
1720
1721void MappingTraits<ELFYAML::GnuHashHeader>::mapping(IO &IO,
1722 ELFYAML::GnuHashHeader &E) {
1723 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1724 IO.mapOptional("NBuckets", E.NBuckets);
1725 IO.mapRequired("SymNdx", E.SymNdx);
1726 IO.mapOptional("MaskWords", E.MaskWords);
1727 IO.mapRequired("Shift2", E.Shift2);
1728}
1729
1730void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO,
1731 ELFYAML::DynamicEntry &Rel) {
1732 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1733
1734 IO.mapRequired("Tag", Rel.Tag);
1735 IO.mapRequired("Value", Rel.Val);
1736}
1737
1738void MappingTraits<ELFYAML::NoteEntry>::mapping(IO &IO, ELFYAML::NoteEntry &N) {
1739 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1740
1741 IO.mapOptional("Name", N.Name);
1742 IO.mapOptional("Desc", N.Desc);
1743 IO.mapRequired("Type", N.Type);
1744}
1745
1746void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO,
1747 ELFYAML::VerdefEntry &E) {
1748 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1749
1750 IO.mapOptional("Version", E.Version);
1751 IO.mapOptional("Flags", E.Flags);
1752 IO.mapOptional("VersionNdx", E.VersionNdx);
1753 IO.mapOptional("Hash", E.Hash);
1754 IO.mapRequired("Names", E.VerNames);
1755}
1756
1757void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO,
1758 ELFYAML::VerneedEntry &E) {
1759 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1760
1761 IO.mapRequired("Version", E.Version);
1762 IO.mapRequired("File", E.File);
1763 IO.mapRequired("Entries", E.AuxV);
1764}
1765
1766void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO,
1767 ELFYAML::VernauxEntry &E) {
1768 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1769
1770 IO.mapRequired("Name", E.Name);
1771 IO.mapRequired("Hash", E.Hash);
1772 IO.mapRequired("Flags", E.Flags);
1773 IO.mapRequired("Other", E.Other);
1774}
1775
1776void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
1777 ELFYAML::Relocation &Rel) {
1778 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
1779 assert(Object && "The IO context is not initialized")(static_cast<void> (0));
1780
1781 IO.mapOptional("Offset", Rel.Offset, (Hex64)0);
1782 IO.mapOptional("Symbol", Rel.Symbol);
1783
1784 if (Object->getMachine() == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
1785 Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
1786 MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
1787 IO, Rel.Type);
1788 IO.mapRequired("Type", Key->Type);
1789 IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1790 IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1791 IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
1792 } else
1793 IO.mapRequired("Type", Rel.Type);
1794
1795 IO.mapOptional("Addend", Rel.Addend, (ELFYAML::YAMLIntUInt)0);
1796}
1797
1798void MappingTraits<ELFYAML::ARMIndexTableEntry>::mapping(
1799 IO &IO, ELFYAML::ARMIndexTableEntry &E) {
1800 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1801 IO.mapRequired("Offset", E.Offset);
1802
1803 StringRef CantUnwind = "EXIDX_CANTUNWIND";
1804 if (IO.outputting() && (uint32_t)E.Value == ARM::EHABI::EXIDX_CANTUNWIND)
1805 IO.mapRequired("Value", CantUnwind);
1806 else if (!IO.outputting() && getStringValue(IO, "Value") == CantUnwind)
1807 E.Value = ARM::EHABI::EXIDX_CANTUNWIND;
1808 else
1809 IO.mapRequired("Value", E.Value);
1810}
1811
1812void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
1813 assert(!IO.getContext() && "The IO context is initialized already")(static_cast<void> (0));
1814 IO.setContext(&Object);
1815 IO.mapTag("!ELF", true);
1816 IO.mapRequired("FileHeader", Object.Header);
1817 IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
1818 IO.mapOptional("Sections", Object.Chunks);
1819 IO.mapOptional("Symbols", Object.Symbols);
1820 IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
1821 IO.mapOptional("DWARF", Object.DWARF);
1822 if (Object.DWARF) {
1823 Object.DWARF->IsLittleEndian =
1824 Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
1825 Object.DWARF->Is64BitAddrSize =
1826 Object.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
1827 }
1828 IO.setContext(nullptr);
1829}
1830
1831void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO,
1832 ELFYAML::LinkerOption &Opt) {
1833 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1834 IO.mapRequired("Name", Opt.Key);
1835 IO.mapRequired("Value", Opt.Value);
1836}
1837
1838void MappingTraits<ELFYAML::CallGraphEntryWeight>::mapping(
1839 IO &IO, ELFYAML::CallGraphEntryWeight &E) {
1840 assert(IO.getContext() && "The IO context is not initialized")(static_cast<void> (0));
1841 IO.mapRequired("Weight", E.Weight);
1842}
1843
1844LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)struct MIPS_AFL_REG { MIPS_AFL_REG() = default; MIPS_AFL_REG(
const uint8_t v) : value(v) {} MIPS_AFL_REG(const MIPS_AFL_REG
&v) = default; MIPS_AFL_REG &operator=(const MIPS_AFL_REG
&rhs) = default; MIPS_AFL_REG &operator=(const uint8_t
&rhs) { value = rhs; return *this; } operator const uint8_t
& () const { return value; } bool operator==(const MIPS_AFL_REG
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_REG &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
1845LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)struct MIPS_ABI_FP { MIPS_ABI_FP() = default; MIPS_ABI_FP(const
uint8_t v) : value(v) {} MIPS_ABI_FP(const MIPS_ABI_FP &
v) = default; MIPS_ABI_FP &operator=(const MIPS_ABI_FP &
rhs) = default; MIPS_ABI_FP &operator=(const uint8_t &
rhs) { value = rhs; return *this; } operator const uint8_t &
() const { return value; } bool operator==(const MIPS_ABI_FP
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_ABI_FP &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
1846LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)struct MIPS_AFL_EXT { MIPS_AFL_EXT() = default; MIPS_AFL_EXT(
const uint32_t v) : value(v) {} MIPS_AFL_EXT(const MIPS_AFL_EXT
&v) = default; MIPS_AFL_EXT &operator=(const MIPS_AFL_EXT
&rhs) = default; MIPS_AFL_EXT &operator=(const uint32_t
&rhs) { value = rhs; return *this; } operator const uint32_t
& () const { return value; } bool operator==(const MIPS_AFL_EXT
&rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_EXT &rhs) const { return value
< rhs.value; } uint32_t value; using BaseType = uint32_t;
};
1847LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)struct MIPS_AFL_ASE { MIPS_AFL_ASE() = default; MIPS_AFL_ASE(
const uint32_t v) : value(v) {} MIPS_AFL_ASE(const MIPS_AFL_ASE
&v) = default; MIPS_AFL_ASE &operator=(const MIPS_AFL_ASE
&rhs) = default; MIPS_AFL_ASE &operator=(const uint32_t
&rhs) { value = rhs; return *this; } operator const uint32_t
& () const { return value; } bool operator==(const MIPS_AFL_ASE
&rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_ASE &rhs) const { return value
< rhs.value; } uint32_t value; using BaseType = uint32_t;
};
1848LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)struct MIPS_AFL_FLAGS1 { MIPS_AFL_FLAGS1() = default; MIPS_AFL_FLAGS1
(const uint32_t v) : value(v) {} MIPS_AFL_FLAGS1(const MIPS_AFL_FLAGS1
&v) = default; MIPS_AFL_FLAGS1 &operator=(const MIPS_AFL_FLAGS1
&rhs) = default; MIPS_AFL_FLAGS1 &operator=(const uint32_t
&rhs) { value = rhs; return *this; } operator const uint32_t
& () const { return value; } bool operator==(const MIPS_AFL_FLAGS1
&rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_FLAGS1 &rhs) const { return value
< rhs.value; } uint32_t value; using BaseType = uint32_t;
};
1849
1850} // end namespace yaml
1851
1852} // end namespace llvm

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include/llvm/ObjectYAML/ELFYAML.h

1//===- ELFYAML.h - ELF YAMLIO implementation --------------------*- 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 declares classes for handling the YAML representation
11/// of ELF.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OBJECTYAML_ELFYAML_H
16#define LLVM_OBJECTYAML_ELFYAML_H
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/BinaryFormat/ELF.h"
20#include "llvm/Object/ELFTypes.h"
21#include "llvm/ObjectYAML/DWARFYAML.h"
22#include "llvm/ObjectYAML/YAML.h"
23#include "llvm/Support/YAMLTraits.h"
24#include <cstdint>
25#include <memory>
26#include <vector>
27
28namespace llvm {
29namespace ELFYAML {
30
31StringRef dropUniqueSuffix(StringRef S);
32std::string appendUniqueSuffix(StringRef Name, const Twine& Msg);
33
34// These types are invariant across 32/64-bit ELF, so for simplicity just
35// directly give them their exact sizes. We don't need to worry about
36// endianness because these are just the types in the YAMLIO structures,
37// and are appropriately converted to the necessary endianness when
38// reading/generating binary object files.
39// The naming of these types is intended to be ELF_PREFIX, where PREFIX is
40// the common prefix of the respective constants. E.g. ELF_EM corresponds
41// to the `e_machine` constants, like `EM_X86_64`.
42// In the future, these would probably be better suited by C++11 enum
43// class's with appropriate fixed underlying type.
44LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)struct ELF_ET { ELF_ET() = default; ELF_ET(const uint16_t v) :
value(v) {} ELF_ET(const ELF_ET &v) = default; ELF_ET &
operator=(const ELF_ET &rhs) = default; ELF_ET &operator
=(const uint16_t &rhs) { value = rhs; return *this; } operator
const uint16_t & () const { return value; } bool operator
==(const ELF_ET &rhs) const { return value == rhs.value; }
bool operator==(const uint16_t &rhs) const { return value
== rhs; } bool operator<(const ELF_ET &rhs) const { return
value < rhs.value; } uint16_t value; using BaseType = uint16_t
; };
45LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT)struct ELF_PT { ELF_PT() = default; ELF_PT(const uint32_t v) :
value(v) {} ELF_PT(const ELF_PT &v) = default; ELF_PT &
operator=(const ELF_PT &rhs) = default; ELF_PT &operator
=(const uint32_t &rhs) { value = rhs; return *this; } operator
const uint32_t & () const { return value; } bool operator
==(const ELF_PT &rhs) const { return value == rhs.value; }
bool operator==(const uint32_t &rhs) const { return value
== rhs; } bool operator<(const ELF_PT &rhs) const { return
value < rhs.value; } uint32_t value; using BaseType = uint32_t
; };
46LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)struct ELF_EM { ELF_EM() = default; ELF_EM(const uint32_t v) :
value(v) {} ELF_EM(const ELF_EM &v) = default; ELF_EM &
operator=(const ELF_EM &rhs) = default; ELF_EM &operator
=(const uint32_t &rhs) { value = rhs; return *this; } operator
const uint32_t & () const { return value; } bool operator
==(const ELF_EM &rhs) const { return value == rhs.value; }
bool operator==(const uint32_t &rhs) const { return value
== rhs; } bool operator<(const ELF_EM &rhs) const { return
value < rhs.value; } uint32_t value; using BaseType = uint32_t
; };
47LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)struct ELF_ELFCLASS { ELF_ELFCLASS() = default; ELF_ELFCLASS(
const uint8_t v) : value(v) {} ELF_ELFCLASS(const ELF_ELFCLASS
&v) = default; ELF_ELFCLASS &operator=(const ELF_ELFCLASS
&rhs) = default; ELF_ELFCLASS &operator=(const uint8_t
&rhs) { value = rhs; return *this; } operator const uint8_t
& () const { return value; } bool operator==(const ELF_ELFCLASS
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const ELF_ELFCLASS &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
48LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)struct ELF_ELFDATA { ELF_ELFDATA() = default; ELF_ELFDATA(const
uint8_t v) : value(v) {} ELF_ELFDATA(const ELF_ELFDATA &
v) = default; ELF_ELFDATA &operator=(const ELF_ELFDATA &
rhs) = default; ELF_ELFDATA &operator=(const uint8_t &
rhs) { value = rhs; return *this; } operator const uint8_t &
() const { return value; } bool operator==(const ELF_ELFDATA
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const ELF_ELFDATA &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
49LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)struct ELF_ELFOSABI { ELF_ELFOSABI() = default; ELF_ELFOSABI(
const uint8_t v) : value(v) {} ELF_ELFOSABI(const ELF_ELFOSABI
&v) = default; ELF_ELFOSABI &operator=(const ELF_ELFOSABI
&rhs) = default; ELF_ELFOSABI &operator=(const uint8_t
&rhs) { value = rhs; return *this; } operator const uint8_t
& () const { return value; } bool operator==(const ELF_ELFOSABI
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const ELF_ELFOSABI &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
50// Just use 64, since it can hold 32-bit values too.
51LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)struct ELF_EF { ELF_EF() = default; ELF_EF(const uint64_t v) :
value(v) {} ELF_EF(const ELF_EF &v) = default; ELF_EF &
operator=(const ELF_EF &rhs) = default; ELF_EF &operator
=(const uint64_t &rhs) { value = rhs; return *this; } operator
const uint64_t & () const { return value; } bool operator
==(const ELF_EF &rhs) const { return value == rhs.value; }
bool operator==(const uint64_t &rhs) const { return value
== rhs; } bool operator<(const ELF_EF &rhs) const { return
value < rhs.value; } uint64_t value; using BaseType = uint64_t
; };
52// Just use 64, since it can hold 32-bit values too.
53LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG)struct ELF_DYNTAG { ELF_DYNTAG() = default; ELF_DYNTAG(const uint64_t
v) : value(v) {} ELF_DYNTAG(const ELF_DYNTAG &v) = default
; ELF_DYNTAG &operator=(const ELF_DYNTAG &rhs) = default
; ELF_DYNTAG &operator=(const uint64_t &rhs) { value =
rhs; return *this; } operator const uint64_t & () const {
return value; } bool operator==(const ELF_DYNTAG &rhs) const
{ return value == rhs.value; } bool operator==(const uint64_t
&rhs) const { return value == rhs; } bool operator<(const
ELF_DYNTAG &rhs) const { return value < rhs.value; } uint64_t
value; using BaseType = uint64_t; };
54LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)struct ELF_PF { ELF_PF() = default; ELF_PF(const uint32_t v) :
value(v) {} ELF_PF(const ELF_PF &v) = default; ELF_PF &
operator=(const ELF_PF &rhs) = default; ELF_PF &operator
=(const uint32_t &rhs) { value = rhs; return *this; } operator
const uint32_t & () const { return value; } bool operator
==(const ELF_PF &rhs) const { return value == rhs.value; }
bool operator==(const uint32_t &rhs) const { return value
== rhs; } bool operator<(const ELF_PF &rhs) const { return
value < rhs.value; } uint32_t value; using BaseType = uint32_t
; };
55LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)struct ELF_SHT { ELF_SHT() = default; ELF_SHT(const uint32_t v
) : value(v) {} ELF_SHT(const ELF_SHT &v) = default; ELF_SHT
&operator=(const ELF_SHT &rhs) = default; ELF_SHT &
operator=(const uint32_t &rhs) { value = rhs; return *this
; } operator const uint32_t & () const { return value; } bool
operator==(const ELF_SHT &rhs) const { return value == rhs
.value; } bool operator==(const uint32_t &rhs) const { return
value == rhs; } bool operator<(const ELF_SHT &rhs) const
{ return value < rhs.value; } uint32_t value; using BaseType
= uint32_t; };
2
Returning without writing to 'this->value'
17
The left operand of '==' is a garbage value
56LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)struct ELF_REL { ELF_REL() = default; ELF_REL(const uint32_t v
) : value(v) {} ELF_REL(const ELF_REL &v) = default; ELF_REL
&operator=(const ELF_REL &rhs) = default; ELF_REL &
operator=(const uint32_t &rhs) { value = rhs; return *this
; } operator const uint32_t & () const { return value; } bool
operator==(const ELF_REL &rhs) const { return value == rhs
.value; } bool operator==(const uint32_t &rhs) const { return
value == rhs; } bool operator<(const ELF_REL &rhs) const
{ return value < rhs.value; } uint32_t value; using BaseType
= uint32_t; };
57LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)struct ELF_RSS { ELF_RSS() = default; ELF_RSS(const uint8_t v
) : value(v) {} ELF_RSS(const ELF_RSS &v) = default; ELF_RSS
&operator=(const ELF_RSS &rhs) = default; ELF_RSS &
operator=(const uint8_t &rhs) { value = rhs; return *this
; } operator const uint8_t & () const { return value; } bool
operator==(const ELF_RSS &rhs) const { return value == rhs
.value; } bool operator==(const uint8_t &rhs) const { return
value == rhs; } bool operator<(const ELF_RSS &rhs) const
{ return value < rhs.value; } uint8_t value; using BaseType
= uint8_t; };
58// Just use 64, since it can hold 32-bit values too.
59LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)struct ELF_SHF { ELF_SHF() = default; ELF_SHF(const uint64_t v
) : value(v) {} ELF_SHF(const ELF_SHF &v) = default; ELF_SHF
&operator=(const ELF_SHF &rhs) = default; ELF_SHF &
operator=(const uint64_t &rhs) { value = rhs; return *this
; } operator const uint64_t & () const { return value; } bool
operator==(const ELF_SHF &rhs) const { return value == rhs
.value; } bool operator==(const uint64_t &rhs) const { return
value == rhs; } bool operator<(const ELF_SHF &rhs) const
{ return value < rhs.value; } uint64_t value; using BaseType
= uint64_t; };
60LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)struct ELF_SHN { ELF_SHN() = default; ELF_SHN(const uint16_t v
) : value(v) {} ELF_SHN(const ELF_SHN &v) = default; ELF_SHN
&operator=(const ELF_SHN &rhs) = default; ELF_SHN &
operator=(const uint16_t &rhs) { value = rhs; return *this
; } operator const uint16_t & () const { return value; } bool
operator==(const ELF_SHN &rhs) const { return value == rhs
.value; } bool operator==(const uint16_t &rhs) const { return
value == rhs; } bool operator<(const ELF_SHN &rhs) const
{ return value < rhs.value; } uint16_t value; using BaseType
= uint16_t; };
61LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STB)struct ELF_STB { ELF_STB() = default; ELF_STB(const uint8_t v
) : value(v) {} ELF_STB(const ELF_STB &v) = default; ELF_STB
&operator=(const ELF_STB &rhs) = default; ELF_STB &
operator=(const uint8_t &rhs) { value = rhs; return *this
; } operator const uint8_t & () const { return value; } bool
operator==(const ELF_STB &rhs) const { return value == rhs
.value; } bool operator==(const uint8_t &rhs) const { return
value == rhs; } bool operator<(const ELF_STB &rhs) const
{ return value < rhs.value; } uint8_t value; using BaseType
= uint8_t; };
62LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)struct ELF_STT { ELF_STT() = default; ELF_STT(const uint8_t v
) : value(v) {} ELF_STT(const ELF_STT &v) = default; ELF_STT
&operator=(const ELF_STT &rhs) = default; ELF_STT &
operator=(const uint8_t &rhs) { value = rhs; return *this
; } operator const uint8_t & () const { return value; } bool
operator==(const ELF_STT &rhs) const { return value == rhs
.value; } bool operator==(const uint8_t &rhs) const { return
value == rhs; } bool operator<(const ELF_STT &rhs) const
{ return value < rhs.value; } uint8_t value; using BaseType
= uint8_t; };
63LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_NT)struct ELF_NT { ELF_NT() = default; ELF_NT(const uint32_t v) :
value(v) {} ELF_NT(const ELF_NT &v) = default; ELF_NT &
operator=(const ELF_NT &rhs) = default; ELF_NT &operator
=(const uint32_t &rhs) { value = rhs; return *this; } operator
const uint32_t & () const { return value; } bool operator
==(const ELF_NT &rhs) const { return value == rhs.value; }
bool operator==(const uint32_t &rhs) const { return value
== rhs; } bool operator<(const ELF_NT &rhs) const { return
value < rhs.value; } uint32_t value; using BaseType = uint32_t
; };
64
65LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)struct MIPS_AFL_REG { MIPS_AFL_REG() = default; MIPS_AFL_REG(
const uint8_t v) : value(v) {} MIPS_AFL_REG(const MIPS_AFL_REG
&v) = default; MIPS_AFL_REG &operator=(const MIPS_AFL_REG
&rhs) = default; MIPS_AFL_REG &operator=(const uint8_t
&rhs) { value = rhs; return *this; } operator const uint8_t
& () const { return value; } bool operator==(const MIPS_AFL_REG
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_REG &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
66LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)struct MIPS_ABI_FP { MIPS_ABI_FP() = default; MIPS_ABI_FP(const
uint8_t v) : value(v) {} MIPS_ABI_FP(const MIPS_ABI_FP &
v) = default; MIPS_ABI_FP &operator=(const MIPS_ABI_FP &
rhs) = default; MIPS_ABI_FP &operator=(const uint8_t &
rhs) { value = rhs; return *this; } operator const uint8_t &
() const { return value; } bool operator==(const MIPS_ABI_FP
&rhs) const { return value == rhs.value; } bool operator
==(const uint8_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_ABI_FP &rhs) const { return value
< rhs.value; } uint8_t value; using BaseType = uint8_t; }
;
67LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)struct MIPS_AFL_EXT { MIPS_AFL_EXT() = default; MIPS_AFL_EXT(
const uint32_t v) : value(v) {} MIPS_AFL_EXT(const MIPS_AFL_EXT
&v) = default; MIPS_AFL_EXT &operator=(const MIPS_AFL_EXT
&rhs) = default; MIPS_AFL_EXT &operator=(const uint32_t
&rhs) { value = rhs; return *this; } operator const uint32_t
& () const { return value; } bool operator==(const MIPS_AFL_EXT
&rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_EXT &rhs) const { return value
< rhs.value; } uint32_t value; using BaseType = uint32_t;
};
68LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)struct MIPS_AFL_ASE { MIPS_AFL_ASE() = default; MIPS_AFL_ASE(
const uint32_t v) : value(v) {} MIPS_AFL_ASE(const MIPS_AFL_ASE
&v) = default; MIPS_AFL_ASE &operator=(const MIPS_AFL_ASE
&rhs) = default; MIPS_AFL_ASE &operator=(const uint32_t
&rhs) { value = rhs; return *this; } operator const uint32_t
& () const { return value; } bool operator==(const MIPS_AFL_ASE
&rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_ASE &rhs) const { return value
< rhs.value; } uint32_t value; using BaseType = uint32_t;
};
69LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)struct MIPS_AFL_FLAGS1 { MIPS_AFL_FLAGS1() = default; MIPS_AFL_FLAGS1
(const uint32_t v) : value(v) {} MIPS_AFL_FLAGS1(const MIPS_AFL_FLAGS1
&v) = default; MIPS_AFL_FLAGS1 &operator=(const MIPS_AFL_FLAGS1
&rhs) = default; MIPS_AFL_FLAGS1 &operator=(const uint32_t
&rhs) { value = rhs; return *this; } operator const uint32_t
& () const { return value; } bool operator==(const MIPS_AFL_FLAGS1
&rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const MIPS_AFL_FLAGS1 &rhs) const { return value
< rhs.value; } uint32_t value; using BaseType = uint32_t;
};
70LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)struct MIPS_ISA { MIPS_ISA() = default; MIPS_ISA(const uint32_t
v) : value(v) {} MIPS_ISA(const MIPS_ISA &v) = default; MIPS_ISA
&operator=(const MIPS_ISA &rhs) = default; MIPS_ISA &
operator=(const uint32_t &rhs) { value = rhs; return *this
; } operator const uint32_t & () const { return value; } bool
operator==(const MIPS_ISA &rhs) const { return value == rhs
.value; } bool operator==(const uint32_t &rhs) const { return
value == rhs; } bool operator<(const MIPS_ISA &rhs) const
{ return value < rhs.value; } uint32_t value; using BaseType
= uint32_t; };
71
72LLVM_YAML_STRONG_TYPEDEF(StringRef, YAMLFlowString)struct YAMLFlowString { YAMLFlowString() = default; YAMLFlowString
(const StringRef v) : value(v) {} YAMLFlowString(const YAMLFlowString
&v) = default; YAMLFlowString &operator=(const YAMLFlowString
&rhs) = default; YAMLFlowString &operator=(const StringRef
&rhs) { value = rhs; return *this; } operator const StringRef
& () const { return value; } bool operator==(const YAMLFlowString
&rhs) const { return value == rhs.value; } bool operator
==(const StringRef &rhs) const { return value == rhs; } bool
operator<(const YAMLFlowString &rhs) const { return value
< rhs.value; } StringRef value; using BaseType = StringRef
; };
73LLVM_YAML_STRONG_TYPEDEF(int64_t, YAMLIntUInt)struct YAMLIntUInt { YAMLIntUInt() = default; YAMLIntUInt(const
int64_t v) : value(v) {} YAMLIntUInt(const YAMLIntUInt &
v) = default; YAMLIntUInt &operator=(const YAMLIntUInt &
rhs) = default; YAMLIntUInt &operator=(const int64_t &
rhs) { value = rhs; return *this; } operator const int64_t &
() const { return value; } bool operator==(const YAMLIntUInt
&rhs) const { return value == rhs.value; } bool operator
==(const int64_t &rhs) const { return value == rhs; } bool
operator<(const YAMLIntUInt &rhs) const { return value
< rhs.value; } int64_t value; using BaseType = int64_t; }
;
74
75template <class ELFT>
76unsigned getDefaultShEntSize(unsigned EMachine, ELF_SHT SecType,
77 StringRef SecName) {
78 if (EMachine == ELF::EM_MIPS && SecType == ELF::SHT_MIPS_ABIFLAGS)
79 return sizeof(object::Elf_Mips_ABIFlags<ELFT>);
80
81 switch (SecType) {
82 case ELF::SHT_SYMTAB:
83 case ELF::SHT_DYNSYM:
84 return sizeof(typename ELFT::Sym);
85 case ELF::SHT_GROUP:
86 return sizeof(typename ELFT::Word);
87 case ELF::SHT_REL:
88 return sizeof(typename ELFT::Rel);
89 case ELF::SHT_RELA:
90 return sizeof(typename ELFT::Rela);
91 case ELF::SHT_RELR:
92 return sizeof(typename ELFT::Relr);
93 case ELF::SHT_DYNAMIC:
94 return sizeof(typename ELFT::Dyn);
95 case ELF::SHT_HASH:
96 return sizeof(typename ELFT::Word);
97 case ELF::SHT_SYMTAB_SHNDX:
98 return sizeof(typename ELFT::Word);
99 case ELF::SHT_GNU_versym:
100 return sizeof(typename ELFT::Half);
101 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
102 return sizeof(object::Elf_CGProfile_Impl<ELFT>);
103 default:
104 if (SecName == ".debug_str")
105 return 1;
106 return 0;
107 }
108}
109
110// For now, hardcode 64 bits everywhere that 32 or 64 would be needed
111// since 64-bit can hold 32-bit values too.
112struct FileHeader {
113 ELF_ELFCLASS Class;
114 ELF_ELFDATA Data;
115 ELF_ELFOSABI OSABI;
116 llvm::yaml::Hex8 ABIVersion;
117 ELF_ET Type;
118 Optional<ELF_EM> Machine;
119 ELF_EF Flags;
120 llvm::yaml::Hex64 Entry;
121 Optional<StringRef> SectionHeaderStringTable;
122
123 Optional<llvm::yaml::Hex64> EPhOff;
124 Optional<llvm::yaml::Hex16> EPhEntSize;
125 Optional<llvm::yaml::Hex16> EPhNum;
126 Optional<llvm::yaml::Hex16> EShEntSize;
127 Optional<llvm::yaml::Hex64> EShOff;
128 Optional<llvm::yaml::Hex16> EShNum;
129 Optional<llvm::yaml::Hex16> EShStrNdx;
130};
131
132struct SectionHeader {
133 StringRef Name;
134};
135
136struct Symbol {
137 StringRef Name;
138 ELF_STT Type;
139 Optional<StringRef> Section;
140 Optional<ELF_SHN> Index;
141 ELF_STB Binding;
142 Optional<llvm::yaml::Hex64> Value;
143 Optional<llvm::yaml::Hex64> Size;
144 Optional<uint8_t> Other;
145
146 Optional<uint32_t> StName;
147};
148
149struct SectionOrType {
150 StringRef sectionNameOrType;
151};
152
153struct DynamicEntry {
154 ELF_DYNTAG Tag;
155 llvm::yaml::Hex64 Val;
156};
157
158struct BBAddrMapEntry {
159 struct BBEntry {
160 llvm::yaml::Hex64 AddressOffset;
161 llvm::yaml::Hex64 Size;
162 llvm::yaml::Hex64 Metadata;
163 };
164 llvm::yaml::Hex64 Address;
165 Optional<uint64_t> NumBlocks;
166 Optional<std::vector<BBEntry>> BBEntries;
167};
168
169struct StackSizeEntry {
170 llvm::yaml::Hex64 Address;
171 llvm::yaml::Hex64 Size;
172};
173
174struct NoteEntry {
175 StringRef Name;
176 yaml::BinaryRef Desc;
177 ELF_NT Type;
178};
179
180struct Chunk {
181 enum class ChunkKind {
182 Dynamic,
183 Group,
184 RawContent,
185 Relocation,
186 Relr,
187 NoBits,
188 Note,
189 Hash,
190 GnuHash,
191 Verdef,
192 Verneed,
193 StackSizes,
194 SymtabShndxSection,
195 Symver,
196 ARMIndexTable,
197 MipsABIFlags,
198 Addrsig,
199 LinkerOptions,
200 DependentLibraries,
201 CallGraphProfile,
202 BBAddrMap,
203
204 // Special chunks.
205 SpecialChunksStart,
206 Fill = SpecialChunksStart,
207 SectionHeaderTable,
208 };
209
210 ChunkKind Kind;
211 StringRef Name;
212 Optional<llvm::yaml::Hex64> Offset;
213
214 // Usually chunks are not created implicitly, but rather loaded from YAML.
215 // This flag is used to signal whether this is the case or not.
216 bool IsImplicit;
217
218 Chunk(ChunkKind K, bool Implicit) : Kind(K), IsImplicit(Implicit) {}
219 virtual ~Chunk();
220};
221
222struct Section : public Chunk {
223 ELF_SHT Type;
224 Optional<ELF_SHF> Flags;
225 Optional<llvm::yaml::Hex64> Address;
226 Optional<StringRef> Link;
227 llvm::yaml::Hex64 AddressAlign;
228 Optional<llvm::yaml::Hex64> EntSize;
229
230 Optional<yaml::BinaryRef> Content;
231 Optional<llvm::yaml::Hex64> Size;
232
233 // Holds the original section index.
234 unsigned OriginalSecNdx;
235
236 Section(ChunkKind Kind, bool IsImplicit = false) : Chunk(Kind, IsImplicit) {}
237
238 static bool classof(const Chunk *S) {
239 return S->Kind < ChunkKind::SpecialChunksStart;
240 }
241
242 // Some derived sections might have their own special entries. This method
243 // returns a vector of <entry name, is used> pairs. It is used for section
244 // validation.
245 virtual std::vector<std::pair<StringRef, bool>> getEntries() const {
246 return {};
247 };
248
249 // The following members are used to override section fields which is
250 // useful for creating invalid objects.
251
252 // This can be used to override the sh_addralign field.
253 Optional<llvm::yaml::Hex64> ShAddrAlign;
254
255 // This can be used to override the offset stored in the sh_name field.
256 // It does not affect the name stored in the string table.
257 Optional<llvm::yaml::Hex64> ShName;
258
259 // This can be used to override the sh_offset field. It does not place the
260 // section data at the offset specified.
261 Optional<llvm::yaml::Hex64> ShOffset;
262
263 // This can be used to override the sh_size field. It does not affect the
264 // content written.
265 Optional<llvm::yaml::Hex64> ShSize;
266
267 // This can be used to override the sh_flags field.
268 Optional<llvm::yaml::Hex64> ShFlags;
269
270 // This can be used to override the sh_type field. It is useful when we
271 // want to use specific YAML keys for a section of a particular type to
272 // describe the content, but still want to have a different final type
273 // for the section.
274 Optional<ELF_SHT> ShType;
275};
276
277// Fill is a block of data which is placed outside of sections. It is
278// not present in the sections header table, but it might affect the output file
279// size and program headers produced.
280struct Fill : Chunk {
281 Optional<yaml::BinaryRef> Pattern;
282 llvm::yaml::Hex64 Size;
283
284 Fill() : Chunk(ChunkKind::Fill, /*Implicit=*/false) {}
285
286 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Fill; }
287};
288
289struct SectionHeaderTable : Chunk {
290 SectionHeaderTable(bool IsImplicit)
291 : Chunk(ChunkKind::SectionHeaderTable, IsImplicit) {}
292
293 static bool classof(const Chunk *S) {
294 return S->Kind == ChunkKind::SectionHeaderTable;
295 }
296
297 Optional<std::vector<SectionHeader>> Sections;
298 Optional<std::vector<SectionHeader>> Excluded;
299 Optional<bool> NoHeaders;
300
301 size_t getNumHeaders(size_t SectionsNum) const {
302 if (IsImplicit || isDefault())
303 return SectionsNum;
304 if (NoHeaders)
305 return (*NoHeaders) ? 0 : SectionsNum;
306 return (Sections ? Sections->size() : 0) + /*Null section*/ 1;
307 }
308
309 bool isDefault() const { return !Sections && !Excluded && !NoHeaders; }
310
311 static constexpr StringRef TypeStr = "SectionHeaderTable";
312};
313
314struct BBAddrMapSection : Section {
315 Optional<std::vector<BBAddrMapEntry>> Entries;
316
317 BBAddrMapSection() : Section(ChunkKind::BBAddrMap) {}
318
319 std::vector<std::pair<StringRef, bool>> getEntries() const override {
320 return {{"Entries", Entries.hasValue()}};
321 };
322
323 static bool classof(const Chunk *S) {
324 return S->Kind == ChunkKind::BBAddrMap;
325 }
326};
327
328struct StackSizesSection : Section {
329 Optional<std::vector<StackSizeEntry>> Entries;
330
331 StackSizesSection() : Section(ChunkKind::StackSizes) {}
332
333 std::vector<std::pair<StringRef, bool>> getEntries() const override {
334 return {{"Entries", Entries.hasValue()}};
335 };
336
337 static bool classof(const Chunk *S) {
338 return S->Kind == ChunkKind::StackSizes;
339 }
340
341 static bool nameMatches(StringRef Name) {
342 return Name == ".stack_sizes";
343 }
344};
345
346struct DynamicSection : Section {
347 Optional<std::vector<DynamicEntry>> Entries;
348
349 DynamicSection() : Section(ChunkKind::Dynamic) {}
350
351 std::vector<std::pair<StringRef, bool>> getEntries() const override {
352 return {{"Entries", Entries.hasValue()}};
353 };
354
355 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Dynamic; }
356};
357
358struct RawContentSection : Section {
359 Optional<llvm::yaml::Hex64> Info;
360
361 RawContentSection() : Section(ChunkKind::RawContent) {}
362
363 static bool classof(const Chunk *S) {
364 return S->Kind == ChunkKind::RawContent;
365 }
366
367 // Is used when a content is read as an array of bytes.
368 Optional<std::vector<uint8_t>> ContentBuf;
369};
370
371struct NoBitsSection : Section {
372 NoBitsSection() : Section(ChunkKind::NoBits) {}
373
374 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::NoBits; }
375};
376
377struct NoteSection : Section {
378 Optional<std::vector<ELFYAML::NoteEntry>> Notes;
379
380 NoteSection() : Section(ChunkKind::Note) {}
381
382 std::vector<std::pair<StringRef, bool>> getEntries() const override {
383 return {{"Notes", Notes.hasValue()}};
384 };
385
386 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Note; }
387};
388
389struct HashSection : Section {
390 Optional<std::vector<uint32_t>> Bucket;
391 Optional<std::vector<uint32_t>> Chain;
392
393 std::vector<std::pair<StringRef, bool>> getEntries() const override {
394 return {{"Bucket", Bucket.hasValue()}, {"Chain", Chain.hasValue()}};
395 };
396
397 // The following members are used to override section fields.
398 // This is useful for creating invalid objects.
399 Optional<llvm::yaml::Hex64> NBucket;
400 Optional<llvm::yaml::Hex64> NChain;
401
402 HashSection() : Section(ChunkKind::Hash) {}
403
404 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Hash; }
405};
406
407struct GnuHashHeader {
408 // The number of hash buckets.
409 // Not used when dumping the object, but can be used to override
410 // the real number of buckets when emiting an object from a YAML document.
411 Optional<llvm::yaml::Hex32> NBuckets;
412
413 // Index of the first symbol in the dynamic symbol table
414 // included in the hash table.
415 llvm::yaml::Hex32 SymNdx;
416
417 // The number of words in the Bloom filter.
418 // Not used when dumping the object, but can be used to override the real
419 // number of words in the Bloom filter when emiting an object from a YAML
420 // document.
421 Optional<llvm::yaml::Hex32> MaskWords;
422
423 // A shift constant used by the Bloom filter.
424 llvm::yaml::Hex32 Shift2;
425};
426
427struct GnuHashSection : Section {
428 Optional<GnuHashHeader> Header;
429 Optional<std::vector<llvm::yaml::Hex64>> BloomFilter;
430 Optional<std::vector<llvm::yaml::Hex32>> HashBuckets;
431 Optional<std::vector<llvm::yaml::Hex32>> HashValues;
432
433 GnuHashSection() : Section(ChunkKind::GnuHash) {}
434
435 std::vector<std::pair<StringRef, bool>> getEntries() const override {
436 return {{"Header", Header.hasValue()},
437 {"BloomFilter", BloomFilter.hasValue()},
438 {"HashBuckets", HashBuckets.hasValue()},
439 {"HashValues", HashValues.hasValue()}};
440 };
441
442 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::GnuHash; }
443};
444
445struct VernauxEntry {
446 uint32_t Hash;
447 uint16_t Flags;
448 uint16_t Other;
449 StringRef Name;
450};
451
452struct VerneedEntry {
453 uint16_t Version;
454 StringRef File;
455 std::vector<VernauxEntry> AuxV;
456};
457
458struct VerneedSection : Section {
459 Optional<std::vector<VerneedEntry>> VerneedV;
460 Optional<llvm::yaml::Hex64> Info;
461
462 VerneedSection() : Section(ChunkKind::Verneed) {}
463
464 std::vector<std::pair<StringRef, bool>> getEntries() const override {
465 return {{"Dependencies", VerneedV.hasValue()}};
466 };
467
468 static bool classof(const Chunk *S) {
469 return S->Kind == ChunkKind::Verneed;
470 }
471};
472
473struct AddrsigSection : Section {
474 Optional<std::vector<YAMLFlowString>> Symbols;
475
476 AddrsigSection() : Section(ChunkKind::Addrsig) {}
477
478 std::vector<std::pair<StringRef, bool>> getEntries() const override {
479 return {{"Symbols", Symbols.hasValue()}};
480 };
481
482 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Addrsig; }
483};
484
485struct LinkerOption {
486 StringRef Key;
487 StringRef Value;
488};
489
490struct LinkerOptionsSection : Section {
491 Optional<std::vector<LinkerOption>> Options;
492
493 LinkerOptionsSection() : Section(ChunkKind::LinkerOptions) {}
494
495 std::vector<std::pair<StringRef, bool>> getEntries() const override {
496 return {{"Options", Options.hasValue()}};
497 };
498
499 static bool classof(const Chunk *S) {
500 return S->Kind == ChunkKind::LinkerOptions;
501 }
502};
503
504struct DependentLibrariesSection : Section {
505 Optional<std::vector<YAMLFlowString>> Libs;
506
507 DependentLibrariesSection() : Section(ChunkKind::DependentLibraries) {}
508
509 std::vector<std::pair<StringRef, bool>> getEntries() const override {
510 return {{"Libraries", Libs.hasValue()}};
511 };
512
513 static bool classof(const Chunk *S) {
514 return S->Kind == ChunkKind::DependentLibraries;
515 }
516};
517
518// Represents the call graph profile section entry.
519struct CallGraphEntryWeight {
520 // The weight of the edge.
521 uint64_t Weight;
522};
523
524struct CallGraphProfileSection : Section {
525 Optional<std::vector<CallGraphEntryWeight>> Entries;
526
527 CallGraphProfileSection() : Section(ChunkKind::CallGraphProfile) {}
528
529 std::vector<std::pair<StringRef, bool>> getEntries() const override {
530 return {{"Entries", Entries.hasValue()}};
531 };
532
533 static bool classof(const Chunk *S) {
534 return S->Kind == ChunkKind::CallGraphProfile;
535 }
536};
537
538struct SymverSection : Section {
539 Optional<std::vector<uint16_t>> Entries;
540
541 SymverSection() : Section(ChunkKind::Symver) {}
542
543 std::vector<std::pair<StringRef, bool>> getEntries() const override {
544 return {{"Entries", Entries.hasValue()}};
545 };
546
547 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Symver; }
548};
549
550struct VerdefEntry {
551 Optional<uint16_t> Version;
552 Optional<uint16_t> Flags;
553 Optional<uint16_t> VersionNdx;
554 Optional<uint32_t> Hash;
555 std::vector<StringRef> VerNames;
556};
557
558struct VerdefSection : Section {
559 Optional<std::vector<VerdefEntry>> Entries;
560 Optional<llvm::yaml::Hex64> Info;
561
562 VerdefSection() : Section(ChunkKind::Verdef) {}
563
564 std::vector<std::pair<StringRef, bool>> getEntries() const override {
565 return {{"Entries", Entries.hasValue()}};
566 };
567
568 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Verdef; }
569};
570
571struct GroupSection : Section {
572 // Members of a group contain a flag and a list of section indices
573 // that are part of the group.
574 Optional<std::vector<SectionOrType>> Members;
575 Optional<StringRef> Signature; /* Info */
576
577 GroupSection() : Section(ChunkKind::Group) {}
578
579 std::vector<std::pair<StringRef, bool>> getEntries() const override {
580 return {{"Members", Members.hasValue()}};
581 };
582
583 static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Group; }
584};
585
586struct Relocation {
587 llvm::yaml::Hex64 Offset;
588 YAMLIntUInt Addend;
589 ELF_REL Type;
590 Optional<StringRef> Symbol;
591};
592
593struct RelocationSection : Section {
594 Optional<std::vector<Relocation>> Relocations;
595 StringRef RelocatableSec; /* Info */
596
597 RelocationSection() : Section(ChunkKind::Relocation) {}
598
599 std::vector<std::pair<StringRef, bool>> getEntries() const override {
600 return {{"Relocations", Relocations.hasValue()}};
601 };
602
603 static bool classof(const Chunk *S) {
604 return S->Kind == ChunkKind::Relocation;
605 }
606};
607
608struct RelrSection : Section {
609 Optional<std::vector<llvm::yaml::Hex64>> Entries;
610
611 RelrSection() : Section(ChunkKind::Relr) {}
612
613 std::vector<std::pair<StringRef, bool>> getEntries() const override {
614 return {{"Entries", Entries.hasValue()}};
615 };
616
617 static bool classof(const Chunk *S) {
618 return S->Kind == ChunkKind::Relr;
619 }
620};
621
622struct SymtabShndxSection : Section {
623 Optional<std::vector<uint32_t>> Entries;
624
625 SymtabShndxSection() : Section(ChunkKind::SymtabShndxSection) {}
626
627 std::vector<std::pair<StringRef, bool>> getEntries() const override {
628 return {{"Entries", Entries.hasValue()}};
629 };
630
631 static bool classof(const Chunk *S) {
632 return S->Kind == ChunkKind::SymtabShndxSection;
633 }
634};
635
636struct ARMIndexTableEntry {
637 llvm::yaml::Hex32 Offset;
638 llvm::yaml::Hex32 Value;
639};
640
641struct ARMIndexTableSection : Section {
642 Optional<std::vector<ARMIndexTableEntry>> Entries;
643
644 ARMIndexTableSection() : Section(ChunkKind::ARMIndexTable) {}
645
646 std::vector<std::pair<StringRef, bool>> getEntries() const override {
647 return {{"Entries", Entries.hasValue()}};
648 };
649
650 static bool classof(const Chunk *S) {
651 return S->Kind == ChunkKind::ARMIndexTable;
652 }
653};
654
655// Represents .MIPS.abiflags section
656struct MipsABIFlags : Section {
657 llvm::yaml::Hex16 Version;
658 MIPS_ISA ISALevel;
659 llvm::yaml::Hex8 ISARevision;
660 MIPS_AFL_REG GPRSize;
661 MIPS_AFL_REG CPR1Size;
662 MIPS_AFL_REG CPR2Size;
663 MIPS_ABI_FP FpABI;
664 MIPS_AFL_EXT ISAExtension;
665 MIPS_AFL_ASE ASEs;
666 MIPS_AFL_FLAGS1 Flags1;
667 llvm::yaml::Hex32 Flags2;
668
669 MipsABIFlags() : Section(ChunkKind::MipsABIFlags) {}
670
671 static bool classof(const Chunk *S) {
672 return S->Kind == ChunkKind::MipsABIFlags;
673 }
674};
675
676struct ProgramHeader {
677 ELF_PT Type;
678 ELF_PF Flags;
679 llvm::yaml::Hex64 VAddr;
680 llvm::yaml::Hex64 PAddr;
681 Optional<llvm::yaml::Hex64> Align;
682 Optional<llvm::yaml::Hex64> FileSize;
683 Optional<llvm::yaml::Hex64> MemSize;
684 Optional<llvm::yaml::Hex64> Offset;
685 Optional<StringRef> FirstSec;
686 Optional<StringRef> LastSec;
687
688 // This vector contains all chunks from [FirstSec, LastSec].
689 std::vector<Chunk *> Chunks;
690};
691
692struct Object {
693 FileHeader Header;
694 std::vector<ProgramHeader> ProgramHeaders;
695
696 // An object might contain output section descriptions as well as
697 // custom data that does not belong to any section.
698 std::vector<std::unique_ptr<Chunk>> Chunks;
699
700 // Although in reality the symbols reside in a section, it is a lot
701 // cleaner and nicer if we read them from the YAML as a separate
702 // top-level key, which automatically ensures that invariants like there
703 // being a single SHT_SYMTAB section are upheld.
704 Optional<std::vector<Symbol>> Symbols;
705 Optional<std::vector<Symbol>> DynamicSymbols;
706 Optional<DWARFYAML::Data> DWARF;
707
708 std::vector<Section *> getSections() {
709 std::vector<Section *> Ret;
710 for (const std::unique_ptr<Chunk> &Sec : Chunks)
711 if (auto S = dyn_cast<ELFYAML::Section>(Sec.get()))
712 Ret.push_back(S);
713 return Ret;
714 }
715
716 const SectionHeaderTable &getSectionHeaderTable() const {
717 for (const std::unique_ptr<Chunk> &C : Chunks)
718 if (auto *S = dyn_cast<ELFYAML::SectionHeaderTable>(C.get()))
719 return *S;
720 llvm_unreachable("the section header table chunk must always be present")__builtin_unreachable();
721 }
722
723 unsigned getMachine() const;
724};
725
726bool shouldAllocateFileSpace(ArrayRef<ProgramHeader> Phdrs,
727 const NoBitsSection &S);
728
729} // end namespace ELFYAML
730} // end namespace llvm
731
732LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::StackSizeEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::StackSizeEntry>::value && !std::
is_same<llvm::ELFYAML::StackSizeEntry, std::string>::value
&& !std::is_same<llvm::ELFYAML::StackSizeEntry, llvm
::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::StackSizeEntry> { static const bool flow = false; }; } }
733LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::BBAddrMapEntry>::value && !std::
is_same<llvm::ELFYAML::BBAddrMapEntry, std::string>::value
&& !std::is_same<llvm::ELFYAML::BBAddrMapEntry, llvm
::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::BBAddrMapEntry> { static const bool flow = false; }; } }
734LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry::BBEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::BBAddrMapEntry::BBEntry>::value &&
!std::is_same<llvm::ELFYAML::BBAddrMapEntry::BBEntry, std
::string>::value && !std::is_same<llvm::ELFYAML
::BBAddrMapEntry::BBEntry, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::BBAddrMapEntry::BBEntry> { static const bool flow = false
; }; } }
735LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::DynamicEntry>::value && !std::is_same
<llvm::ELFYAML::DynamicEntry, std::string>::value &&
!std::is_same<llvm::ELFYAML::DynamicEntry, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::DynamicEntry> { static const bool flow = false; }; } }
736LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::LinkerOption)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::LinkerOption>::value && !std::is_same
<llvm::ELFYAML::LinkerOption, std::string>::value &&
!std::is_same<llvm::ELFYAML::LinkerOption, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::LinkerOption> { static const bool flow = false; }; } }
737LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::CallGraphEntryWeight)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::CallGraphEntryWeight>::value &&
!std::is_same<llvm::ELFYAML::CallGraphEntryWeight, std::string
>::value && !std::is_same<llvm::ELFYAML::CallGraphEntryWeight
, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::CallGraphEntryWeight> { static const bool flow = false; }
; } }
738LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::NoteEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::NoteEntry>::value && !std::is_same
<llvm::ELFYAML::NoteEntry, std::string>::value &&
!std::is_same<llvm::ELFYAML::NoteEntry, llvm::StringRef>
::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::NoteEntry> { static const bool flow = false; }; } }
739LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::ProgramHeader>::value && !std::
is_same<llvm::ELFYAML::ProgramHeader, std::string>::value
&& !std::is_same<llvm::ELFYAML::ProgramHeader, llvm
::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::ProgramHeader> { static const bool flow = false; }; } }
740LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionHeader)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::SectionHeader>::value && !std::
is_same<llvm::ELFYAML::SectionHeader, std::string>::value
&& !std::is_same<llvm::ELFYAML::SectionHeader, llvm
::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::SectionHeader> { static const bool flow = false; }; } }
741LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Chunk>)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<std::unique_ptr<llvm::ELFYAML::Chunk> >::value &&
!std::is_same<std::unique_ptr<llvm::ELFYAML::Chunk>
, std::string>::value && !std::is_same<std::unique_ptr
<llvm::ELFYAML::Chunk>, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<std::unique_ptr
<llvm::ELFYAML::Chunk> > { static const bool flow = false
; }; } }
742LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::Symbol>::value && !std::is_same
<llvm::ELFYAML::Symbol, std::string>::value && !
std::is_same<llvm::ELFYAML::Symbol, llvm::StringRef>::value
, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::Symbol> { static const bool flow = false; }; } }
743LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerdefEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::VerdefEntry>::value && !std::is_same
<llvm::ELFYAML::VerdefEntry, std::string>::value &&
!std::is_same<llvm::ELFYAML::VerdefEntry, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::VerdefEntry> { static const bool flow = false; }; } }
744LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VernauxEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::VernauxEntry>::value && !std::is_same
<llvm::ELFYAML::VernauxEntry, std::string>::value &&
!std::is_same<llvm::ELFYAML::VernauxEntry, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::VernauxEntry> { static const bool flow = false; }; } }
745LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerneedEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::VerneedEntry>::value && !std::is_same
<llvm::ELFYAML::VerneedEntry, std::string>::value &&
!std::is_same<llvm::ELFYAML::VerneedEntry, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::VerneedEntry> { static const bool flow = false; }; } }
746LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::Relocation>::value && !std::is_same
<llvm::ELFYAML::Relocation, std::string>::value &&
!std::is_same<llvm::ELFYAML::Relocation, llvm::StringRef>
::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::Relocation> { static const bool flow = false; }; } }
747LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::SectionOrType>::value && !std::
is_same<llvm::ELFYAML::SectionOrType, std::string>::value
&& !std::is_same<llvm::ELFYAML::SectionOrType, llvm
::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::SectionOrType> { static const bool flow = false; }; } }
748LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ARMIndexTableEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::ELFYAML::ARMIndexTableEntry>::value && !
std::is_same<llvm::ELFYAML::ARMIndexTableEntry, std::string
>::value && !std::is_same<llvm::ELFYAML::ARMIndexTableEntry
, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::ELFYAML
::ARMIndexTableEntry> { static const bool flow = false; };
} }
749
750namespace llvm {
751namespace yaml {
752
753template <> struct ScalarTraits<ELFYAML::YAMLIntUInt> {
754 static void output(const ELFYAML::YAMLIntUInt &Val, void *Ctx,
755 raw_ostream &Out);
756 static StringRef input(StringRef Scalar, void *Ctx,
757 ELFYAML::YAMLIntUInt &Val);
758 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
759};
760
761template <>
762struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
763 static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
764};
765
766template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
767 static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
768};
769
770template <> struct ScalarEnumerationTraits<ELFYAML::ELF_NT> {
771 static void enumeration(IO &IO, ELFYAML::ELF_NT &Value);
772};
773
774template <>
775struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
776 static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
777};
778
779template <>
780struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
781 static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
782};
783
784template <>
785struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
786 static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
787};
788
789template <>
790struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
791 static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
792};
793
794template <>
795struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
796 static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
797};
798
799template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
800 static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
801};
802
803template <>
804struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
805 static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
806};
807
808template <>
809struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
810 static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
811};
812
813template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
814 static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
815};
816
817template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
818 static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
819};
820
821template <>
822struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
823 static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
824};
825
826template <>
827struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
828 static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
829};
830
831template <>
832struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
833 static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
834};
835
836template <>
837struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
838 static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
839};
840
841template <>
842struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
843 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
844};
845
846template <>
847struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
848 static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
849};
850
851template <>
852struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
853 static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
854};
855
856template <>
857struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
858 static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
859};
860
861template <>
862struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
863 static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
864};
865
866template <>
867struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
868 static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
869};
870
871template <>
872struct MappingTraits<ELFYAML::FileHeader> {
873 static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
874};
875
876template <> struct MappingTraits<ELFYAML::SectionHeader> {
877 static void mapping(IO &IO, ELFYAML::SectionHeader &SHdr);
878};
879
880template <> struct MappingTraits<ELFYAML::ProgramHeader> {
881 static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
882 static std::string validate(IO &IO, ELFYAML::ProgramHeader &FileHdr);
883};
884
885template <>
886struct MappingTraits<ELFYAML::Symbol> {
887 static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
888 static std::string validate(IO &IO, ELFYAML::Symbol &Symbol);
889};
890
891template <> struct MappingTraits<ELFYAML::StackSizeEntry> {
892 static void mapping(IO &IO, ELFYAML::StackSizeEntry &Rel);
893};
894
895template <> struct MappingTraits<ELFYAML::BBAddrMapEntry> {
896 static void mapping(IO &IO, ELFYAML::BBAddrMapEntry &Rel);
897};
898
899template <> struct MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry> {
900 static void mapping(IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &Rel);
901};
902
903template <> struct MappingTraits<ELFYAML::GnuHashHeader> {
904 static void mapping(IO &IO, ELFYAML::GnuHashHeader &Rel);
905};
906
907template <> struct MappingTraits<ELFYAML::DynamicEntry> {
908 static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
909};
910
911template <> struct MappingTraits<ELFYAML::NoteEntry> {
912 static void mapping(IO &IO, ELFYAML::NoteEntry &N);
913};
914
915template <> struct MappingTraits<ELFYAML::VerdefEntry> {
916 static void mapping(IO &IO, ELFYAML::VerdefEntry &E);
917};
918
919template <> struct MappingTraits<ELFYAML::VerneedEntry> {
920 static void mapping(IO &IO, ELFYAML::VerneedEntry &E);
921};
922
923template <> struct MappingTraits<ELFYAML::VernauxEntry> {
924 static void mapping(IO &IO, ELFYAML::VernauxEntry &E);
925};
926
927template <> struct MappingTraits<ELFYAML::LinkerOption> {
928 static void mapping(IO &IO, ELFYAML::LinkerOption &Sym);
929};
930
931template <> struct MappingTraits<ELFYAML::CallGraphEntryWeight> {
932 static void mapping(IO &IO, ELFYAML::CallGraphEntryWeight &E);
933};
934
935template <> struct MappingTraits<ELFYAML::Relocation> {
936 static void mapping(IO &IO, ELFYAML::Relocation &Rel);
937};
938
939template <> struct MappingTraits<ELFYAML::ARMIndexTableEntry> {
940 static void mapping(IO &IO, ELFYAML::ARMIndexTableEntry &E);
941};
942
943template <> struct MappingTraits<std::unique_ptr<ELFYAML::Chunk>> {
944 static void mapping(IO &IO, std::unique_ptr<ELFYAML::Chunk> &C);
945 static std::string validate(IO &io, std::unique_ptr<ELFYAML::Chunk> &C);
946};
947
948template <>
949struct MappingTraits<ELFYAML::Object> {
950 static void mapping(IO &IO, ELFYAML::Object &Object);
951};
952
953template <> struct MappingTraits<ELFYAML::SectionOrType> {
954 static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
955};
956
957} // end namespace yaml
958} // end namespace llvm
959
960#endif // LLVM_OBJECTYAML_ELFYAML_H

/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include/llvm/Support/YAMLTraits.h

1//===- llvm/Support/YAMLTraits.h --------------------------------*- 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#ifndef LLVM_SUPPORT_YAMLTRAITS_H
10#define LLVM_SUPPORT_YAMLTRAITS_H
11
12#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Support/AlignOf.h"
19#include "llvm/Support/Allocator.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/Regex.h"
22#include "llvm/Support/SMLoc.h"
23#include "llvm/Support/SourceMgr.h"
24#include "llvm/Support/VersionTuple.h"
25#include "llvm/Support/YAMLParser.h"
26#include "llvm/Support/raw_ostream.h"
27#include <cassert>
28#include <cctype>
29#include <cstddef>
30#include <cstdint>
31#include <iterator>
32#include <map>
33#include <memory>
34#include <new>
35#include <string>
36#include <system_error>
37#include <type_traits>
38#include <vector>
39
40namespace llvm {
41namespace yaml {
42
43enum class NodeKind : uint8_t {
44 Scalar,
45 Map,
46 Sequence,
47};
48
49struct EmptyContext {};
50
51/// This class should be specialized by any type that needs to be converted
52/// to/from a YAML mapping. For example:
53///
54/// struct MappingTraits<MyStruct> {
55/// static void mapping(IO &io, MyStruct &s) {
56/// io.mapRequired("name", s.name);
57/// io.mapRequired("size", s.size);
58/// io.mapOptional("age", s.age);
59/// }
60/// };
61template<class T>
62struct MappingTraits {
63 // Must provide:
64 // static void mapping(IO &io, T &fields);
65 // Optionally may provide:
66 // static std::string validate(IO &io, T &fields);
67 //
68 // The optional flow flag will cause generated YAML to use a flow mapping
69 // (e.g. { a: 0, b: 1 }):
70 // static const bool flow = true;
71};
72
73/// This class is similar to MappingTraits<T> but allows you to pass in
74/// additional context for each map operation. For example:
75///
76/// struct MappingContextTraits<MyStruct, MyContext> {
77/// static void mapping(IO &io, MyStruct &s, MyContext &c) {
78/// io.mapRequired("name", s.name);
79/// io.mapRequired("size", s.size);
80/// io.mapOptional("age", s.age);
81/// ++c.TimesMapped;
82/// }
83/// };
84template <class T, class Context> struct MappingContextTraits {
85 // Must provide:
86 // static void mapping(IO &io, T &fields, Context &Ctx);
87 // Optionally may provide:
88 // static std::string validate(IO &io, T &fields, Context &Ctx);
89 //
90 // The optional flow flag will cause generated YAML to use a flow mapping
91 // (e.g. { a: 0, b: 1 }):
92 // static const bool flow = true;
93};
94
95/// This class should be specialized by any integral type that converts
96/// to/from a YAML scalar where there is a one-to-one mapping between
97/// in-memory values and a string in YAML. For example:
98///
99/// struct ScalarEnumerationTraits<Colors> {
100/// static void enumeration(IO &io, Colors &value) {
101/// io.enumCase(value, "red", cRed);
102/// io.enumCase(value, "blue", cBlue);
103/// io.enumCase(value, "green", cGreen);
104/// }
105/// };
106template <typename T, typename Enable = void> struct ScalarEnumerationTraits {
107 // Must provide:
108 // static void enumeration(IO &io, T &value);
109};
110
111/// This class should be specialized by any integer type that is a union
112/// of bit values and the YAML representation is a flow sequence of
113/// strings. For example:
114///
115/// struct ScalarBitSetTraits<MyFlags> {
116/// static void bitset(IO &io, MyFlags &value) {
117/// io.bitSetCase(value, "big", flagBig);
118/// io.bitSetCase(value, "flat", flagFlat);
119/// io.bitSetCase(value, "round", flagRound);
120/// }
121/// };
122template <typename T, typename Enable = void> struct ScalarBitSetTraits {
123 // Must provide:
124 // static void bitset(IO &io, T &value);
125};
126
127/// Describe which type of quotes should be used when quoting is necessary.
128/// Some non-printable characters need to be double-quoted, while some others
129/// are fine with simple-quoting, and some don't need any quoting.
130enum class QuotingType { None, Single, Double };
131
132/// This class should be specialized by type that requires custom conversion
133/// to/from a yaml scalar. For example:
134///
135/// template<>
136/// struct ScalarTraits<MyType> {
137/// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
138/// // stream out custom formatting
139/// out << llvm::format("%x", val);
140/// }
141/// static StringRef input(StringRef scalar, void*, MyType &value) {
142/// // parse scalar and set `value`
143/// // return empty string on success, or error string
144/// return StringRef();
145/// }
146/// static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
147/// };
148template <typename T, typename Enable = void> struct ScalarTraits {
149 // Must provide:
150 //
151 // Function to write the value as a string:
152 // static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
153 //
154 // Function to convert a string to a value. Returns the empty
155 // StringRef on success or an error string if string is malformed:
156 // static StringRef input(StringRef scalar, void *ctxt, T &value);
157 //
158 // Function to determine if the value should be quoted.
159 // static QuotingType mustQuote(StringRef);
160};
161
162/// This class should be specialized by type that requires custom conversion
163/// to/from a YAML literal block scalar. For example:
164///
165/// template <>
166/// struct BlockScalarTraits<MyType> {
167/// static void output(const MyType &Value, void*, llvm::raw_ostream &Out)
168/// {
169/// // stream out custom formatting
170/// Out << Value;
171/// }
172/// static StringRef input(StringRef Scalar, void*, MyType &Value) {
173/// // parse scalar and set `value`
174/// // return empty string on success, or error string
175/// return StringRef();
176/// }
177/// };
178template <typename T>
179struct BlockScalarTraits {
180 // Must provide:
181 //
182 // Function to write the value as a string:
183 // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out);
184 //
185 // Function to convert a string to a value. Returns the empty
186 // StringRef on success or an error string if string is malformed:
187 // static StringRef input(StringRef Scalar, void *ctxt, T &Value);
188 //
189 // Optional:
190 // static StringRef inputTag(T &Val, std::string Tag)
191 // static void outputTag(const T &Val, raw_ostream &Out)
192};
193
194/// This class should be specialized by type that requires custom conversion
195/// to/from a YAML scalar with optional tags. For example:
196///
197/// template <>
198/// struct TaggedScalarTraits<MyType> {
199/// static void output(const MyType &Value, void*, llvm::raw_ostream
200/// &ScalarOut, llvm::raw_ostream &TagOut)
201/// {
202/// // stream out custom formatting including optional Tag
203/// Out << Value;
204/// }
205/// static StringRef input(StringRef Scalar, StringRef Tag, void*, MyType
206/// &Value) {
207/// // parse scalar and set `value`
208/// // return empty string on success, or error string
209/// return StringRef();
210/// }
211/// static QuotingType mustQuote(const MyType &Value, StringRef) {
212/// return QuotingType::Single;
213/// }
214/// };
215template <typename T> struct TaggedScalarTraits {
216 // Must provide:
217 //
218 // Function to write the value and tag as strings:
219 // static void output(const T &Value, void *ctx, llvm::raw_ostream &ScalarOut,
220 // llvm::raw_ostream &TagOut);
221 //
222 // Function to convert a string to a value. Returns the empty
223 // StringRef on success or an error string if string is malformed:
224 // static StringRef input(StringRef Scalar, StringRef Tag, void *ctxt, T
225 // &Value);
226 //
227 // Function to determine if the value should be quoted.
228 // static QuotingType mustQuote(const T &Value, StringRef Scalar);
229};
230
231/// This class should be specialized by any type that needs to be converted
232/// to/from a YAML sequence. For example:
233///
234/// template<>
235/// struct SequenceTraits<MyContainer> {
236/// static size_t size(IO &io, MyContainer &seq) {
237/// return seq.size();
238/// }
239/// static MyType& element(IO &, MyContainer &seq, size_t index) {
240/// if ( index >= seq.size() )
241/// seq.resize(index+1);
242/// return seq[index];
243/// }
244/// };
245template<typename T, typename EnableIf = void>
246struct SequenceTraits {
247 // Must provide:
248 // static size_t size(IO &io, T &seq);
249 // static T::value_type& element(IO &io, T &seq, size_t index);
250 //
251 // The following is option and will cause generated YAML to use
252 // a flow sequence (e.g. [a,b,c]).
253 // static const bool flow = true;
254};
255
256/// This class should be specialized by any type for which vectors of that
257/// type need to be converted to/from a YAML sequence.
258template<typename T, typename EnableIf = void>
259struct SequenceElementTraits {
260 // Must provide:
261 // static const bool flow;
262};
263
264/// This class should be specialized by any type that needs to be converted
265/// to/from a list of YAML documents.
266template<typename T>
267struct DocumentListTraits {
268 // Must provide:
269 // static size_t size(IO &io, T &seq);
270 // static T::value_type& element(IO &io, T &seq, size_t index);
271};
272
273/// This class should be specialized by any type that needs to be converted
274/// to/from a YAML mapping in the case where the names of the keys are not known
275/// in advance, e.g. a string map.
276template <typename T>
277struct CustomMappingTraits {
278 // static void inputOne(IO &io, StringRef key, T &elem);
279 // static void output(IO &io, T &elem);
280};
281
282/// This class should be specialized by any type that can be represented as
283/// a scalar, map, or sequence, decided dynamically. For example:
284///
285/// typedef std::unique_ptr<MyBase> MyPoly;
286///
287/// template<>
288/// struct PolymorphicTraits<MyPoly> {
289/// static NodeKind getKind(const MyPoly &poly) {
290/// return poly->getKind();
291/// }
292/// static MyScalar& getAsScalar(MyPoly &poly) {
293/// if (!poly || !isa<MyScalar>(poly))
294/// poly.reset(new MyScalar());
295/// return *cast<MyScalar>(poly.get());
296/// }
297/// // ...
298/// };
299template <typename T> struct PolymorphicTraits {
300 // Must provide:
301 // static NodeKind getKind(const T &poly);
302 // static scalar_type &getAsScalar(T &poly);
303 // static map_type &getAsMap(T &poly);
304 // static sequence_type &getAsSequence(T &poly);
305};
306
307// Only used for better diagnostics of missing traits
308template <typename T>
309struct MissingTrait;
310
311// Test if ScalarEnumerationTraits<T> is defined on type T.
312template <class T>
313struct has_ScalarEnumerationTraits
314{
315 using Signature_enumeration = void (*)(class IO&, T&);
316
317 template <typename U>
318 static char test(SameType<Signature_enumeration, &U::enumeration>*);
319
320 template <typename U>
321 static double test(...);
322
323 static bool const value =
324 (sizeof(test<ScalarEnumerationTraits<T>>(nullptr)) == 1);
325};
326
327// Test if ScalarBitSetTraits<T> is defined on type T.
328template <class T>
329struct has_ScalarBitSetTraits
330{
331 using Signature_bitset = void (*)(class IO&, T&);
332
333 template <typename U>
334 static char test(SameType<Signature_bitset, &U::bitset>*);
335
336 template <typename U>
337 static double test(...);
338
339 static bool const value = (sizeof(test<ScalarBitSetTraits<T>>(nullptr)) == 1);
340};
341
342// Test if ScalarTraits<T> is defined on type T.
343template <class T>
344struct has_ScalarTraits
345{
346 using Signature_input = StringRef (*)(StringRef, void*, T&);
347 using Signature_output = void (*)(const T&, void*, raw_ostream&);
348 using Signature_mustQuote = QuotingType (*)(StringRef);
349
350 template <typename U>
351 static char test(SameType<Signature_input, &U::input> *,
352 SameType<Signature_output, &U::output> *,
353 SameType<Signature_mustQuote, &U::mustQuote> *);
354
355 template <typename U>
356 static double test(...);
357
358 static bool const value =
359 (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1);
360};
361
362// Test if BlockScalarTraits<T> is defined on type T.
363template <class T>
364struct has_BlockScalarTraits
365{
366 using Signature_input = StringRef (*)(StringRef, void *, T &);
367 using Signature_output = void (*)(const T &, void *, raw_ostream &);
368
369 template <typename U>
370 static char test(SameType<Signature_input, &U::input> *,
371 SameType<Signature_output, &U::output> *);
372
373 template <typename U>
374 static double test(...);
375
376 static bool const value =
377 (sizeof(test<BlockScalarTraits<T>>(nullptr, nullptr)) == 1);
378};
379
380// Test if TaggedScalarTraits<T> is defined on type T.
381template <class T> struct has_TaggedScalarTraits {
382 using Signature_input = StringRef (*)(StringRef, StringRef, void *, T &);
383 using Signature_output = void (*)(const T &, void *, raw_ostream &,
384 raw_ostream &);
385 using Signature_mustQuote = QuotingType (*)(const T &, StringRef);
386
387 template <typename U>
388 static char test(SameType<Signature_input, &U::input> *,
389 SameType<Signature_output, &U::output> *,
390 SameType<Signature_mustQuote, &U::mustQuote> *);
391
392 template <typename U> static double test(...);
393
394 static bool const value =
395 (sizeof(test<TaggedScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1);
396};
397
398// Test if MappingContextTraits<T> is defined on type T.
399template <class T, class Context> struct has_MappingTraits {
400 using Signature_mapping = void (*)(class IO &, T &, Context &);
401
402 template <typename U>
403 static char test(SameType<Signature_mapping, &U::mapping>*);
404
405 template <typename U>
406 static double test(...);
407
408 static bool const value =
409 (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1);
410};
411
412// Test if MappingTraits<T> is defined on type T.
413template <class T> struct has_MappingTraits<T, EmptyContext> {
414 using Signature_mapping = void (*)(class IO &, T &);
415
416 template <typename U>
417 static char test(SameType<Signature_mapping, &U::mapping> *);
418
419 template <typename U> static double test(...);
420
421 static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1);
422};
423
424// Test if MappingContextTraits<T>::validate() is defined on type T.
425template <class T, class Context> struct has_MappingValidateTraits {
426 using Signature_validate = std::string (*)(class IO &, T &, Context &);
427
428 template <typename U>
429 static char test(SameType<Signature_validate, &U::validate>*);
430
431 template <typename U>
432 static double test(...);
433
434 static bool const value =
435 (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1);
436};
437
438// Test if MappingTraits<T>::validate() is defined on type T.
439template <class T> struct has_MappingValidateTraits<T, EmptyContext> {
440 using Signature_validate = std::string (*)(class IO &, T &);
441
442 template <typename U>
443 static char test(SameType<Signature_validate, &U::validate> *);
444
445 template <typename U> static double test(...);
446
447 static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1);
448};
449
450// Test if SequenceTraits<T> is defined on type T.
451template <class T>
452struct has_SequenceMethodTraits
453{
454 using Signature_size = size_t (*)(class IO&, T&);
455
456 template <typename U>
457 static char test(SameType<Signature_size, &U::size>*);
458
459 template <typename U>
460 static double test(...);
461
462 static bool const value = (sizeof(test<SequenceTraits<T>>(nullptr)) == 1);
463};
464
465// Test if CustomMappingTraits<T> is defined on type T.
466template <class T>
467struct has_CustomMappingTraits
468{
469 using Signature_input = void (*)(IO &io, StringRef key, T &v);
470
471 template <typename U>
472 static char test(SameType<Signature_input, &U::inputOne>*);
473
474 template <typename U>
475 static double test(...);
476
477 static bool const value =
478 (sizeof(test<CustomMappingTraits<T>>(nullptr)) == 1);
479};
480
481// has_FlowTraits<int> will cause an error with some compilers because
482// it subclasses int. Using this wrapper only instantiates the
483// real has_FlowTraits only if the template type is a class.
484template <typename T, bool Enabled = std::is_class<T>::value>
485class has_FlowTraits
486{
487public:
488 static const bool value = false;
489};
490
491// Some older gcc compilers don't support straight forward tests
492// for members, so test for ambiguity cause by the base and derived
493// classes both defining the member.
494template <class T>
495struct has_FlowTraits<T, true>
496{
497 struct Fallback { bool flow; };
498 struct Derived : T, Fallback { };
499
500 template<typename C>
501 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
502
503 template<typename C>
504 static char (&f(...))[2];
505
506 static bool const value = sizeof(f<Derived>(nullptr)) == 2;
507};
508
509// Test if SequenceTraits<T> is defined on type T
510template<typename T>
511struct has_SequenceTraits : public std::integral_constant<bool,
512 has_SequenceMethodTraits<T>::value > { };
513
514// Test if DocumentListTraits<T> is defined on type T
515template <class T>
516struct has_DocumentListTraits
517{
518 using Signature_size = size_t (*)(class IO &, T &);
519
520 template <typename U>
521 static char test(SameType<Signature_size, &U::size>*);
522
523 template <typename U>
524 static double test(...);
525
526 static bool const value = (sizeof(test<DocumentListTraits<T>>(nullptr))==1);
527};
528
529template <class T> struct has_PolymorphicTraits {
530 using Signature_getKind = NodeKind (*)(const T &);
531
532 template <typename U>
533 static char test(SameType<Signature_getKind, &U::getKind> *);
534
535 template <typename U> static double test(...);
536
537 static bool const value = (sizeof(test<PolymorphicTraits<T>>(nullptr)) == 1);
538};
539
540inline bool isNumeric(StringRef S) {
541 const static auto skipDigits = [](StringRef Input) {
542 return Input.drop_front(
543 std::min(Input.find_first_not_of("0123456789"), Input.size()));
544 };
545
546 // Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls
547 // safe.
548 if (S.empty() || S.equals("+") || S.equals("-"))
549 return false;
550
551 if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
552 return true;
553
554 // Infinity and decimal numbers can be prefixed with sign.
555 StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S;
556
557 // Check for infinity first, because checking for hex and oct numbers is more
558 // expensive.
559 if (Tail.equals(".inf") || Tail.equals(".Inf") || Tail.equals(".INF"))
560 return true;
561
562 // Section 10.3.2 Tag Resolution
563 // YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with
564 // [-+], so S should be used instead of Tail.
565 if (S.startswith("0o"))
566 return S.size() > 2 &&
567 S.drop_front(2).find_first_not_of("01234567") == StringRef::npos;
568
569 if (S.startswith("0x"))
570 return S.size() > 2 && S.drop_front(2).find_first_not_of(
571 "0123456789abcdefABCDEF") == StringRef::npos;
572
573 // Parse float: [-+]? (\. [0-9]+ | [0-9]+ (\. [0-9]* )?) ([eE] [-+]? [0-9]+)?
574 S = Tail;
575
576 // Handle cases when the number starts with '.' and hence needs at least one
577 // digit after dot (as opposed by number which has digits before the dot), but
578 // doesn't have one.
579 if (S.startswith(".") &&
580 (S.equals(".") ||
581 (S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr)))
582 return false;
583
584 if (S.startswith("E") || S.startswith("e"))
585 return false;
586
587 enum ParseState {
588 Default,
589 FoundDot,
590 FoundExponent,
591 };
592 ParseState State = Default;
593
594 S = skipDigits(S);
595
596 // Accept decimal integer.
597 if (S.empty())
598 return true;
599
600 if (S.front() == '.') {
601 State = FoundDot;
602 S = S.drop_front();
603 } else if (S.front() == 'e' || S.front() == 'E') {
604 State = FoundExponent;
605 S = S.drop_front();
606 } else {
607 return false;
608 }
609
610 if (State == FoundDot) {
611 S = skipDigits(S);
612 if (S.empty())
613 return true;
614
615 if (S.front() == 'e' || S.front() == 'E') {
616 State = FoundExponent;
617 S = S.drop_front();
618 } else {
619 return false;
620 }
621 }
622
623 assert(State == FoundExponent && "Should have found exponent at this point.")(static_cast<void> (0));
624 if (S.empty())
625 return false;
626
627 if (S.front() == '+' || S.front() == '-') {
628 S = S.drop_front();
629 if (S.empty())
630 return false;
631 }
632
633 return skipDigits(S).empty();
634}
635
636inline bool isNull(StringRef S) {
637 return S.equals("null") || S.equals("Null") || S.equals("NULL") ||
638 S.equals("~");
639}
640
641inline bool isBool(StringRef S) {
642 // FIXME: using parseBool is causing multiple tests to fail.
643 return S.equals("true") || S.equals("True") || S.equals("TRUE") ||
644 S.equals("false") || S.equals("False") || S.equals("FALSE");
645}
646
647// 5.1. Character Set
648// The allowed character range explicitly excludes the C0 control block #x0-#x1F
649// (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1
650// control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate
651// block #xD800-#xDFFF, #xFFFE, and #xFFFF.
652inline QuotingType needsQuotes(StringRef S) {
653 if (S.empty())
654 return QuotingType::Single;
655
656 QuotingType MaxQuotingNeeded = QuotingType::None;
657 if (isSpace(static_cast<unsigned char>(S.front())) ||
658 isSpace(static_cast<unsigned char>(S.back())))
659 MaxQuotingNeeded = QuotingType::Single;
660 if (isNull(S))
661 MaxQuotingNeeded = QuotingType::Single;
662 if (isBool(S))
663 MaxQuotingNeeded = QuotingType::Single;
664 if (isNumeric(S))
665 MaxQuotingNeeded = QuotingType::Single;
666
667 // 7.3.3 Plain Style
668 // Plain scalars must not begin with most indicators, as this would cause
669 // ambiguity with other YAML constructs.
670 static constexpr char Indicators[] = R"(-?:\,[]{}#&*!|>'"%@`)";
671 if (S.find_first_of(Indicators) == 0)
672 MaxQuotingNeeded = QuotingType::Single;
673
674 for (unsigned char C : S) {
675 // Alphanum is safe.
676 if (isAlnum(C))
677 continue;
678
679 switch (C) {
680 // Safe scalar characters.
681 case '_':
682 case '-':
683 case '^':
684 case '.':
685 case ',':
686 case ' ':
687 // TAB (0x9) is allowed in unquoted strings.
688 case 0x9:
689 continue;
690 // LF(0xA) and CR(0xD) may delimit values and so require at least single
691 // quotes. LLVM YAML parser cannot handle single quoted multiline so use
692 // double quoting to produce valid YAML.
693 case 0xA:
694 case 0xD:
695 return QuotingType::Double;
696 // DEL (0x7F) are excluded from the allowed character range.
697 case 0x7F:
698 return QuotingType::Double;
699 // Forward slash is allowed to be unquoted, but we quote it anyway. We have
700 // many tests that use FileCheck against YAML output, and this output often
701 // contains paths. If we quote backslashes but not forward slashes then
702 // paths will come out either quoted or unquoted depending on which platform
703 // the test is run on, making FileCheck comparisons difficult.
704 case '/':
705 default: {
706 // C0 control block (0x0 - 0x1F) is excluded from the allowed character
707 // range.
708 if (C <= 0x1F)
709 return QuotingType::Double;
710
711 // Always double quote UTF-8.
712 if ((C & 0x80) != 0)
713 return QuotingType::Double;
714
715 // The character is not safe, at least simple quoting needed.
716 MaxQuotingNeeded = QuotingType::Single;
717 }
718 }
719 }
720
721 return MaxQuotingNeeded;
722}
723
724template <typename T, typename Context>
725struct missingTraits
726 : public std::integral_constant<bool,
727 !has_ScalarEnumerationTraits<T>::value &&
728 !has_ScalarBitSetTraits<T>::value &&
729 !has_ScalarTraits<T>::value &&
730 !has_BlockScalarTraits<T>::value &&
731 !has_TaggedScalarTraits<T>::value &&
732 !has_MappingTraits<T, Context>::value &&
733 !has_SequenceTraits<T>::value &&
734 !has_CustomMappingTraits<T>::value &&
735 !has_DocumentListTraits<T>::value &&
736 !has_PolymorphicTraits<T>::value> {};
737
738template <typename T, typename Context>
739struct validatedMappingTraits
740 : public std::integral_constant<
741 bool, has_MappingTraits<T, Context>::value &&
742 has_MappingValidateTraits<T, Context>::value> {};
743
744template <typename T, typename Context>
745struct unvalidatedMappingTraits
746 : public std::integral_constant<
747 bool, has_MappingTraits<T, Context>::value &&
748 !has_MappingValidateTraits<T, Context>::value> {};
749
750// Base class for Input and Output.
751class IO {
752public:
753 IO(void *Ctxt = nullptr);
754 virtual ~IO();
755
756 virtual bool outputting() const = 0;
757
758 virtual unsigned beginSequence() = 0;
759 virtual bool preflightElement(unsigned, void *&) = 0;
760 virtual void postflightElement(void*) = 0;
761 virtual void endSequence() = 0;
762 virtual bool canElideEmptySequence() = 0;
763
764 virtual unsigned beginFlowSequence() = 0;
765 virtual bool preflightFlowElement(unsigned, void *&) = 0;
766 virtual void postflightFlowElement(void*) = 0;
767 virtual void endFlowSequence() = 0;
768
769 virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
770 virtual void beginMapping() = 0;
771 virtual void endMapping() = 0;
772 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
773 virtual void postflightKey(void*) = 0;
774 virtual std::vector<StringRef> keys() = 0;
775
776 virtual void beginFlowMapping() = 0;
777 virtual void endFlowMapping() = 0;
778
779 virtual void beginEnumScalar() = 0;
780 virtual bool matchEnumScalar(const char*, bool) = 0;
781 virtual bool matchEnumFallback() = 0;
782 virtual void endEnumScalar() = 0;
783
784 virtual bool beginBitSetScalar(bool &) = 0;
785 virtual bool bitSetMatch(const char*, bool) = 0;
786 virtual void endBitSetScalar() = 0;
787
788 virtual void scalarString(StringRef &, QuotingType) = 0;
789 virtual void blockScalarString(StringRef &) = 0;
790 virtual void scalarTag(std::string &) = 0;
791
792 virtual NodeKind getNodeKind() = 0;
793
794 virtual void setError(const Twine &) = 0;
795 virtual void setAllowUnknownKeys(bool Allow);
796
797 template <typename T>
798 void enumCase(T &Val, const char* Str, const T ConstVal) {
799 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
800 Val = ConstVal;
801 }
802 }
803
804 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
805 template <typename T>
806 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
807 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
15
Assuming the condition is true
16
Calling 'ELF_SHT::operator=='
808 Val = ConstVal;
809 }
810 }
811
812 template <typename FBT, typename T>
813 void enumFallback(T &Val) {
814 if (matchEnumFallback()) {
815 EmptyContext Context;
816 // FIXME: Force integral conversion to allow strong typedefs to convert.
817 FBT Res = static_cast<typename FBT::BaseType>(Val);
818 yamlize(*this, Res, true, Context);
819 Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res));
820 }
821 }
822
823 template <typename T>
824 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
825 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
826 Val = static_cast<T>(Val | ConstVal);
827 }
828 }
829
830 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
831 template <typename T>
832 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
833 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
834 Val = static_cast<T>(Val | ConstVal);
835 }
836 }
837
838 template <typename T>
839 void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) {
840 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
841 Val = Val | ConstVal;
842 }
843
844 template <typename T>
845 void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal,
846 uint32_t Mask) {
847 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
848 Val = Val | ConstVal;
849 }
850
851 void *getContext() const;
852 void setContext(void *);
853
854 template <typename T> void mapRequired(const char *Key, T &Val) {
855 EmptyContext Ctx;
856 this->processKey(Key, Val, true, Ctx);
9
Calling 'IO::processKey'
857 }
858
859 template <typename T, typename Context>
860 void mapRequired(const char *Key, T &Val, Context &Ctx) {
861 this->processKey(Key, Val, true, Ctx);
862 }
863
864 template <typename T> void mapOptional(const char *Key, T &Val) {
865 EmptyContext Ctx;
866 mapOptionalWithContext(Key, Val, Ctx);
867 }
868
869 template <typename T, typename DefaultT>
870 void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
871 EmptyContext Ctx;
872 mapOptionalWithContext(Key, Val, Default, Ctx);
873 }
874
875 template <typename T, typename Context>
876 std::enable_if_t<has_SequenceTraits<T>::value, void>
877 mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
878 // omit key/value instead of outputting empty sequence
879 if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
880 return;
881 this->processKey(Key, Val, false, Ctx);
882 }
883
884 template <typename T, typename Context>
885 void mapOptionalWithContext(const char *Key, Optional<T> &Val, Context &Ctx) {
886 this->processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false,
887 Ctx);
888 }
889
890 template <typename T, typename Context>
891 std::enable_if_t<!has_SequenceTraits<T>::value, void>
892 mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
893 this->processKey(Key, Val, false, Ctx);
894 }
895
896 template <typename T, typename Context, typename DefaultT>
897 void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
898 Context &Ctx) {
899 static_assert(std::is_convertible<DefaultT, T>::value,
900 "Default type must be implicitly convertible to value type!");
901 this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
902 false, Ctx);
903 }
904
905private:
906 template <typename T, typename Context>
907 void processKeyWithDefault(const char *Key, Optional<T> &Val,
908 const Optional<T> &DefaultValue, bool Required,
909 Context &Ctx);
910
911 template <typename T, typename Context>
912 void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue,
913 bool Required, Context &Ctx) {
914 void *SaveInfo;
915 bool UseDefault;
916 const bool sameAsDefault = outputting() && Val == DefaultValue;
917 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
918 SaveInfo) ) {
919 yamlize(*this, Val, Required, Ctx);
920 this->postflightKey(SaveInfo);
921 }
922 else {
923 if ( UseDefault )
924 Val = DefaultValue;
925 }
926 }
927
928 template <typename T, typename Context>
929 void processKey(const char *Key, T &Val, bool Required, Context &Ctx) {
930 void *SaveInfo;
931 bool UseDefault;
932 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
10
Assuming the condition is true
11
Taking true branch
933 yamlize(*this, Val, Required, Ctx);
12
Calling 'yamlize<llvm::ELFYAML::ELF_SHT>'
934 this->postflightKey(SaveInfo);
935 }
936 }
937
938private:
939 void *Ctxt;
940};
941
942namespace detail {
943
944template <typename T, typename Context>
945void doMapping(IO &io, T &Val, Context &Ctx) {
946 MappingContextTraits<T, Context>::mapping(io, Val, Ctx);
947}
948
949template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) {
950 MappingTraits<T>::mapping(io, Val);
951}
952
953} // end namespace detail
954
955template <typename T>
956std::enable_if_t<has_ScalarEnumerationTraits<T>::value, void>
957yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
958 io.beginEnumScalar();
959 ScalarEnumerationTraits<T>::enumeration(io, Val);
13
Calling 'ScalarEnumerationTraits::enumeration'
960 io.endEnumScalar();
961}
962
963template <typename T>
964std::enable_if_t<has_ScalarBitSetTraits<T>::value, void>
965yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
966 bool DoClear;
967 if ( io.beginBitSetScalar(DoClear) ) {
968 if ( DoClear )
969 Val = T();
970 ScalarBitSetTraits<T>::bitset(io, Val);
971 io.endBitSetScalar();
972 }
973}
974
975template <typename T>
976std::enable_if_t<has_ScalarTraits<T>::value, void> yamlize(IO &io, T &Val, bool,
977 EmptyContext &Ctx) {
978 if ( io.outputting() ) {
979 std::string Storage;
980 raw_string_ostream Buffer(Storage);
981 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
982 StringRef Str = Buffer.str();
983 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
984 }
985 else {
986 StringRef Str;
987 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
988 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
989 if ( !Result.empty() ) {
990 io.setError(Twine(Result));
991 }
992 }
993}
994
995template <typename T>
996std::enable_if_t<has_BlockScalarTraits<T>::value, void>
997yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) {
998 if (YamlIO.outputting()) {
999 std::string Storage;
1000 raw_string_ostream Buffer(Storage);
1001 BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
1002 StringRef Str = Buffer.str();
1003 YamlIO.blockScalarString(Str);
1004 } else {
1005 StringRef Str;
1006 YamlIO.blockScalarString(Str);
1007 StringRef Result =
1008 BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val);
1009 if (!Result.empty())
1010 YamlIO.setError(Twine(Result));
1011 }
1012}
1013
1014template <typename T>
1015std::enable_if_t<has_TaggedScalarTraits<T>::value, void>
1016yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1017 if (io.outputting()) {
1018 std::string ScalarStorage, TagStorage;
1019 raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage);
1020 TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer,
1021 TagBuffer);
1022 io.scalarTag(TagBuffer.str());
1023 StringRef ScalarStr = ScalarBuffer.str();
1024 io.scalarString(ScalarStr,
1025 TaggedScalarTraits<T>::mustQuote(Val, ScalarStr));
1026 } else {
1027 std::string Tag;
1028 io.scalarTag(Tag);
1029 StringRef Str;
1030 io.scalarString(Str, QuotingType::None);
1031 StringRef Result =
1032 TaggedScalarTraits<T>::input(Str, Tag, io.getContext(), Val);
1033 if (!Result.empty()) {
1034 io.setError(Twine(Result));
1035 }
1036 }
1037}
1038
1039template <typename T, typename Context>
1040std::enable_if_t<validatedMappingTraits<T, Context>::value, void>
1041yamlize(IO &io, T &Val, bool, Context &Ctx) {
1042 if (has_FlowTraits<MappingTraits<T>>::value)
1043 io.beginFlowMapping();
1044 else
1045 io.beginMapping();
1046 if (io.outputting()) {
1047 std::string Err = MappingTraits<T>::validate(io, Val);
1048 if (!Err.empty()) {
1049 errs() << Err << "\n";
1050 assert(Err.empty() && "invalid struct trying to be written as yaml")(static_cast<void> (0));
1051 }
1052 }
1053 detail::doMapping(io, Val, Ctx);
1054 if (!io.outputting()) {
1055 std::string Err = MappingTraits<T>::validate(io, Val);
1056 if (!Err.empty())
1057 io.setError(Err);
1058 }
1059 if (has_FlowTraits<MappingTraits<T>>::value)
1060 io.endFlowMapping();
1061 else
1062 io.endMapping();
1063}
1064
1065template <typename T, typename Context>
1066std::enable_if_t<unvalidatedMappingTraits<T, Context>::value, void>
1067yamlize(IO &io, T &Val, bool, Context &Ctx) {
1068 if (has_FlowTraits<MappingTraits<T>>::value) {
1069 io.beginFlowMapping();
1070 detail::doMapping(io, Val, Ctx);
1071 io.endFlowMapping();
1072 } else {
1073 io.beginMapping();
1074 detail::doMapping(io, Val, Ctx);
1075 io.endMapping();
1076 }
1077}
1078
1079template <typename T>
1080std::enable_if_t<has_CustomMappingTraits<T>::value, void>
1081yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1082 if ( io.outputting() ) {
1083 io.beginMapping();
1084 CustomMappingTraits<T>::output(io, Val);
1085 io.endMapping();
1086 } else {
1087 io.beginMapping();
1088 for (StringRef key : io.keys())
1089 CustomMappingTraits<T>::inputOne(io, key, Val);
1090 io.endMapping();
1091 }
1092}
1093
1094template <typename T>
1095std::enable_if_t<has_PolymorphicTraits<T>::value, void>
1096yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1097 switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val)
1098 : io.getNodeKind()) {
1099 case NodeKind::Scalar:
1100 return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val), true, Ctx);
1101 case NodeKind::Map:
1102 return yamlize(io, PolymorphicTraits<T>::getAsMap(Val), true, Ctx);
1103 case NodeKind::Sequence:
1104 return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val), true, Ctx);
1105 }
1106}
1107
1108template <typename T>
1109std::enable_if_t<missingTraits<T, EmptyContext>::value, void>
1110yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1111 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1112}
1113
1114template <typename T, typename Context>
1115std::enable_if_t<has_SequenceTraits<T>::value, void>
1116yamlize(IO &io, T &Seq, bool, Context &Ctx) {
1117 if ( has_FlowTraits< SequenceTraits<T>>::value ) {
1118 unsigned incnt = io.beginFlowSequence();
1119 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
1120 for(unsigned i=0; i < count; ++i) {
1121 void *SaveInfo;
1122 if ( io.preflightFlowElement(i, SaveInfo) ) {
1123 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
1124 io.postflightFlowElement(SaveInfo);
1125 }
1126 }
1127 io.endFlowSequence();
1128 }
1129 else {
1130 unsigned incnt = io.beginSequence();
1131 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
1132 for(unsigned i=0; i < count; ++i) {
1133 void *SaveInfo;
1134 if ( io.preflightElement(i, SaveInfo) ) {
1135 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
1136 io.postflightElement(SaveInfo);
1137 }
1138 }
1139 io.endSequence();
1140 }
1141}
1142
1143template<>
1144struct ScalarTraits<bool> {
1145 static void output(const bool &, void* , raw_ostream &);
1146 static StringRef input(StringRef, void *, bool &);
1147 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1148};
1149
1150template<>
1151struct ScalarTraits<StringRef> {
1152 static void output(const StringRef &, void *, raw_ostream &);
1153 static StringRef input(StringRef, void *, StringRef &);
1154 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
1155};
1156
1157template<>
1158struct ScalarTraits<std::string> {
1159 static void output(const std::string &, void *, raw_ostream &);
1160 static StringRef input(StringRef, void *, std::string &);
1161 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
1162};
1163
1164template<>
1165struct ScalarTraits<uint8_t> {
1166 static void output(const uint8_t &, void *, raw_ostream &);
1167 static StringRef input(StringRef, void *, uint8_t &);
1168 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1169};
1170
1171template<>
1172struct ScalarTraits<uint16_t> {
1173 static void output(const uint16_t &, void *, raw_ostream &);
1174 static StringRef input(StringRef, void *, uint16_t &);
1175 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1176};
1177
1178template<>
1179struct ScalarTraits<uint32_t> {
1180 static void output(const uint32_t &, void *, raw_ostream &);
1181 static StringRef input(StringRef, void *, uint32_t &);
1182 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1183};
1184
1185template<>
1186struct ScalarTraits<uint64_t> {
1187 static void output(const uint64_t &, void *, raw_ostream &);
1188 static StringRef input(StringRef, void *, uint64_t &);
1189 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1190};
1191
1192template<>
1193struct ScalarTraits<int8_t> {
1194 static void output(const int8_t &, void *, raw_ostream &);
1195 static StringRef input(StringRef, void *, int8_t &);
1196 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1197};
1198
1199template<>
1200struct ScalarTraits<int16_t> {
1201 static void output(const int16_t &, void *, raw_ostream &);
1202 static StringRef input(StringRef, void *, int16_t &);
1203 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1204};
1205
1206template<>
1207struct ScalarTraits<int32_t> {
1208 static void output(const int32_t &, void *, raw_ostream &);
1209 static StringRef input(StringRef, void *, int32_t &);
1210 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1211};
1212
1213template<>
1214struct ScalarTraits<int64_t> {
1215 static void output(const int64_t &, void *, raw_ostream &);
1216 static StringRef input(StringRef, void *, int64_t &);
1217 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1218};
1219
1220template<>
1221struct ScalarTraits<float> {
1222 static void output(const float &, void *, raw_ostream &);
1223 static StringRef input(StringRef, void *, float &);
1224 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1225};
1226
1227template<>
1228struct ScalarTraits<double> {
1229 static void output(const double &, void *, raw_ostream &);
1230 static StringRef input(StringRef, void *, double &);
1231 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1232};
1233
1234// For endian types, we use existing scalar Traits class for the underlying
1235// type. This way endian aware types are supported whenever the traits are
1236// defined for the underlying type.
1237template <typename value_type, support::endianness endian, size_t alignment>
1238struct ScalarTraits<support::detail::packed_endian_specific_integral<
1239 value_type, endian, alignment>,
1240 std::enable_if_t<has_ScalarTraits<value_type>::value>> {
1241 using endian_type =
1242 support::detail::packed_endian_specific_integral<value_type, endian,
1243 alignment>;
1244
1245 static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) {
1246 ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream);
1247 }
1248
1249 static StringRef input(StringRef Str, void *Ctx, endian_type &E) {
1250 value_type V;
1251 auto R = ScalarTraits<value_type>::input(Str, Ctx, V);
1252 E = static_cast<endian_type>(V);
1253 return R;
1254 }
1255
1256 static QuotingType mustQuote(StringRef Str) {
1257 return ScalarTraits<value_type>::mustQuote(Str);
1258 }
1259};
1260
1261template <typename value_type, support::endianness endian, size_t alignment>
1262struct ScalarEnumerationTraits<
1263 support::detail::packed_endian_specific_integral<value_type, endian,
1264 alignment>,
1265 std::enable_if_t<has_ScalarEnumerationTraits<value_type>::value>> {
1266 using endian_type =
1267 support::detail::packed_endian_specific_integral<value_type, endian,
1268 alignment>;
1269
1270 static void enumeration(IO &io, endian_type &E) {
1271 value_type V = E;
1272 ScalarEnumerationTraits<value_type>::enumeration(io, V);
1273 E = V;
1274 }
1275};
1276
1277template <typename value_type, support::endianness endian, size_t alignment>
1278struct ScalarBitSetTraits<
1279 support::detail::packed_endian_specific_integral<value_type, endian,
1280 alignment>,
1281 std::enable_if_t<has_ScalarBitSetTraits<value_type>::value>> {
1282 using endian_type =
1283 support::detail::packed_endian_specific_integral<value_type, endian,
1284 alignment>;
1285 static void bitset(IO &io, endian_type &E) {
1286 value_type V = E;
1287 ScalarBitSetTraits<value_type>::bitset(io, V);
1288 E = V;
1289 }
1290};
1291
1292// Utility for use within MappingTraits<>::mapping() method
1293// to [de]normalize an object for use with YAML conversion.
1294template <typename TNorm, typename TFinal>
1295struct MappingNormalization {
1296 MappingNormalization(IO &i_o, TFinal &Obj)
1297 : io(i_o), BufPtr(nullptr), Result(Obj) {
1298 if ( io.outputting() ) {
1299 BufPtr = new (&Buffer) TNorm(io, Obj);
1300 }
1301 else {
1302 BufPtr = new (&Buffer) TNorm(io);
1303 }
1304 }
1305
1306 ~MappingNormalization() {
1307 if ( ! io.outputting() ) {
1308 Result = BufPtr->denormalize(io);
1309 }
1310 BufPtr->~TNorm();
1311 }
1312
1313 TNorm* operator->() { return BufPtr; }
1314
1315private:
1316 using Storage = AlignedCharArrayUnion<TNorm>;
1317
1318 Storage Buffer;
1319 IO &io;
1320 TNorm *BufPtr;
1321 TFinal &Result;
1322};
1323
1324// Utility for use within MappingTraits<>::mapping() method
1325// to [de]normalize an object for use with YAML conversion.
1326template <typename TNorm, typename TFinal>
1327struct MappingNormalizationHeap {
1328 MappingNormalizationHeap(IO &i_o, TFinal &Obj, BumpPtrAllocator *allocator)
1329 : io(i_o), Result(Obj) {
1330 if ( io.outputting() ) {
1331 BufPtr = new (&Buffer) TNorm(io, Obj);
1332 }
1333 else if (allocator) {
1334 BufPtr = allocator->Allocate<TNorm>();
1335 new (BufPtr) TNorm(io);
1336 } else {
1337 BufPtr = new TNorm(io);
1338 }
1339 }
1340
1341 ~MappingNormalizationHeap() {
1342 if ( io.outputting() ) {
1343 BufPtr->~TNorm();
1344 }
1345 else {
1346 Result = BufPtr->denormalize(io);
1347 }
1348 }
1349
1350 TNorm* operator->() { return BufPtr; }
1351
1352private:
1353 using Storage = AlignedCharArrayUnion<TNorm>;
1354
1355 Storage Buffer;
1356 IO &io;
1357 TNorm *BufPtr = nullptr;
1358 TFinal &Result;
1359};
1360
1361///
1362/// The Input class is used to parse a yaml document into in-memory structs
1363/// and vectors.
1364///
1365/// It works by using YAMLParser to do a syntax parse of the entire yaml
1366/// document, then the Input class builds a graph of HNodes which wraps
1367/// each yaml Node. The extra layer is buffering. The low level yaml
1368/// parser only lets you look at each node once. The buffering layer lets
1369/// you search and interate multiple times. This is necessary because
1370/// the mapRequired() method calls may not be in the same order
1371/// as the keys in the document.
1372///
1373class Input : public IO {
1374public:
1375 // Construct a yaml Input object from a StringRef and optional
1376 // user-data. The DiagHandler can be specified to provide
1377 // alternative error reporting.
1378 Input(StringRef InputContent,
1379 void *Ctxt = nullptr,
1380 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
1381 void *DiagHandlerCtxt = nullptr);
1382 Input(MemoryBufferRef Input,
1383 void *Ctxt = nullptr,
1384 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
1385 void *DiagHandlerCtxt = nullptr);
1386 ~Input() override;
1387
1388 // Check if there was an syntax or semantic error during parsing.
1389 std::error_code error();
1390
1391private:
1392 bool outputting() const override;
1393 bool mapTag(StringRef, bool) override;
1394 void beginMapping() override;
1395 void endMapping() override;
1396 bool preflightKey(const char *, bool, bool, bool &, void *&) override;
1397 void postflightKey(void *) override;
1398 std::vector<StringRef> keys() override;
1399 void beginFlowMapping() override;
1400 void endFlowMapping() override;
1401 unsigned beginSequence() override;
1402 void endSequence() override;
1403 bool preflightElement(unsigned index, void *&) override;
1404 void postflightElement(void *) override;
1405 unsigned beginFlowSequence() override;
1406 bool preflightFlowElement(unsigned , void *&) override;
1407 void postflightFlowElement(void *) override;
1408 void endFlowSequence() override;
1409 void beginEnumScalar() override;
1410 bool matchEnumScalar(const char*, bool) override;
1411 bool matchEnumFallback() override;
1412 void endEnumScalar() override;
1413 bool beginBitSetScalar(bool &) override;
1414 bool bitSetMatch(const char *, bool ) override;
1415 void endBitSetScalar() override;
1416 void scalarString(StringRef &, QuotingType) override;
1417 void blockScalarString(StringRef &) override;
1418 void scalarTag(std::string &) override;
1419 NodeKind getNodeKind() override;
1420 void setError(const Twine &message) override;
1421 bool canElideEmptySequence() override;
1422
1423 class HNode {
1424 virtual void anchor();
1425
1426 public:
1427 HNode(Node *n) : _node(n) { }
1428 virtual ~HNode() = default;
1429
1430 static bool classof(const HNode *) { return true; }
1431
1432 Node *_node;
1433 };
1434
1435 class EmptyHNode : public HNode {
1436 void anchor() override;
1437
1438 public:
1439 EmptyHNode(Node *n) : HNode(n) { }
1440
1441 static bool classof(const HNode *n) { return NullNode::classof(n->_node); }
1442
1443 static bool classof(const EmptyHNode *) { return true; }
1444 };
1445
1446 class ScalarHNode : public HNode {
1447 void anchor() override;
1448
1449 public:
1450 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
1451
1452 StringRef value() const { return _value; }
1453
1454 static bool classof(const HNode *n) {
1455 return ScalarNode::classof(n->_node) ||
1456 BlockScalarNode::classof(n->_node);
1457 }
1458
1459 static bool classof(const ScalarHNode *) { return true; }
1460
1461 protected:
1462 StringRef _value;
1463 };
1464
1465 class MapHNode : public HNode {
1466 void anchor() override;
1467
1468 public:
1469 MapHNode(Node *n) : HNode(n) { }
1470
1471 static bool classof(const HNode *n) {
1472 return MappingNode::classof(n->_node);
1473 }
1474
1475 static bool classof(const MapHNode *) { return true; }
1476
1477 using NameToNodeAndLoc =
1478 StringMap<std::pair<std::unique_ptr<HNode>, SMRange>>;
1479
1480 NameToNodeAndLoc Mapping;
1481 SmallVector<std::string, 6> ValidKeys;
1482 };
1483
1484 class SequenceHNode : public HNode {
1485 void anchor() override;
1486
1487 public:
1488 SequenceHNode(Node *n) : HNode(n) { }
1489
1490 static bool classof(const HNode *n) {
1491 return SequenceNode::classof(n->_node);
1492 }
1493
1494 static bool classof(const SequenceHNode *) { return true; }
1495
1496 std::vector<std::unique_ptr<HNode>> Entries;
1497 };
1498
1499 std::unique_ptr<Input::HNode> createHNodes(Node *node);
1500 void setError(HNode *hnode, const Twine &message);
1501 void setError(Node *node, const Twine &message);
1502 void setError(const SMRange &Range, const Twine &message);
1503
1504 void reportWarning(HNode *hnode, const Twine &message);
1505 void reportWarning(Node *hnode, const Twine &message);
1506 void reportWarning(const SMRange &Range, const Twine &message);
1507
1508public:
1509 // These are only used by operator>>. They could be private
1510 // if those templated things could be made friends.
1511 bool setCurrentDocument();
1512 bool nextDocument();
1513
1514 /// Returns the current node that's being parsed by the YAML Parser.
1515 const Node *getCurrentNode() const;
1516
1517 void setAllowUnknownKeys(bool Allow) override;
1518
1519private:
1520 SourceMgr SrcMgr; // must be before Strm
1521 std::unique_ptr<llvm::yaml::Stream> Strm;
1522 std::unique_ptr<HNode> TopNode;
1523 std::error_code EC;
1524 BumpPtrAllocator StringAllocator;
1525 document_iterator DocIterator;
1526 std::vector<bool> BitValuesUsed;
1527 HNode *CurrentNode = nullptr;
1528 bool ScalarMatchFound = false;
1529 bool AllowUnknownKeys = false;
1530};
1531
1532///
1533/// The Output class is used to generate a yaml document from in-memory structs
1534/// and vectors.
1535///
1536class Output : public IO {
1537public:
1538 Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
1539 ~Output() override;
1540
1541 /// Set whether or not to output optional values which are equal
1542 /// to the default value. By default, when outputting if you attempt
1543 /// to write a value that is equal to the default, the value gets ignored.
1544 /// Sometimes, it is useful to be able to see these in the resulting YAML
1545 /// anyway.
1546 void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; }
1547
1548 bool outputting() const override;
1549 bool mapTag(StringRef, bool) override;
1550 void beginMapping() override;
1551 void endMapping() override;
1552 bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
1553 void postflightKey(void *) override;
1554 std::vector<StringRef> keys() override;
1555 void beginFlowMapping() override;
1556 void endFlowMapping() override;
1557 unsigned beginSequence() override;
1558 void endSequence() override;
1559 bool preflightElement(unsigned, void *&) override;
1560 void postflightElement(void *) override;
1561 unsigned beginFlowSequence() override;
1562 bool preflightFlowElement(unsigned, void *&) override;
1563 void postflightFlowElement(void *) override;
1564 void endFlowSequence() override;
1565 void beginEnumScalar() override;
1566 bool matchEnumScalar(const char*, bool) override;
1567 bool matchEnumFallback() override;
1568 void endEnumScalar() override;
1569 bool beginBitSetScalar(bool &) override;
1570 bool bitSetMatch(const char *, bool ) override;
1571 void endBitSetScalar() override;
1572 void scalarString(StringRef &, QuotingType) override;
1573 void blockScalarString(StringRef &) override;
1574 void scalarTag(std::string &) override;
1575 NodeKind getNodeKind() override;
1576 void setError(const Twine &message) override;
1577 bool canElideEmptySequence() override;
1578
1579 // These are only used by operator<<. They could be private
1580 // if that templated operator could be made a friend.
1581 void beginDocuments();
1582 bool preflightDocument(unsigned);
1583 void postflightDocument();
1584 void endDocuments();
1585
1586private:
1587 void output(StringRef s);
1588 void outputUpToEndOfLine(StringRef s);
1589 void newLineCheck(bool EmptySequence = false);
1590 void outputNewLine();
1591 void paddedKey(StringRef key);
1592 void flowKey(StringRef Key);
1593
1594 enum InState {
1595 inSeqFirstElement,
1596 inSeqOtherElement,
1597 inFlowSeqFirstElement,
1598 inFlowSeqOtherElement,
1599 inMapFirstKey,
1600 inMapOtherKey,
1601 inFlowMapFirstKey,
1602 inFlowMapOtherKey
1603 };
1604
1605 static bool inSeqAnyElement(InState State);
1606 static bool inFlowSeqAnyElement(InState State);
1607 static bool inMapAnyKey(InState State);
1608 static bool inFlowMapAnyKey(InState State);
1609
1610 raw_ostream &Out;
1611 int WrapColumn;
1612 SmallVector<InState, 8> StateStack;
1613 int Column = 0;
1614 int ColumnAtFlowStart = 0;
1615 int ColumnAtMapFlowStart = 0;
1616 bool NeedBitValueComma = false;
1617 bool NeedFlowSequenceComma = false;
1618 bool EnumerationMatchFound = false;
1619 bool WriteDefaultValues = false;
1620 StringRef Padding;
1621 StringRef PaddingBeforeContainer;
1622};
1623
1624template <typename T, typename Context>
1625void IO::processKeyWithDefault(const char *Key, Optional<T> &Val,
1626 const Optional<T> &DefaultValue, bool Required,
1627 Context &Ctx) {
1628 assert(DefaultValue.hasValue() == false &&(static_cast<void> (0))
1629 "Optional<T> shouldn't have a value!")(static_cast<void> (0));
1630 void *SaveInfo;
1631 bool UseDefault = true;
1632 const bool sameAsDefault = outputting() && !Val.hasValue();
1633 if (!outputting() && !Val.hasValue())
1634 Val = T();
1635 if (Val.hasValue() &&
1636 this->preflightKey(Key, Required, sameAsDefault, UseDefault, SaveInfo)) {
1637
1638 // When reading an Optional<X> key from a YAML description, we allow the
1639 // special "<none>" value, which can be used to specify that no value was
1640 // requested, i.e. the DefaultValue will be assigned. The DefaultValue is
1641 // usually None.
1642 bool IsNone = false;
1643 if (!outputting())
1644 if (auto *Node = dyn_cast<ScalarNode>(((Input *)this)->getCurrentNode()))
1645 // We use rtrim to ignore possible white spaces that might exist when a
1646 // comment is present on the same line.
1647 IsNone = Node->getRawValue().rtrim(' ') == "<none>";
1648
1649 if (IsNone)
1650 Val = DefaultValue;
1651 else
1652 yamlize(*this, Val.getValue(), Required, Ctx);
1653 this->postflightKey(SaveInfo);
1654 } else {
1655 if (UseDefault)
1656 Val = DefaultValue;
1657 }
1658}
1659
1660/// YAML I/O does conversion based on types. But often native data types
1661/// are just a typedef of built in intergral types (e.g. int). But the C++
1662/// type matching system sees through the typedef and all the typedefed types
1663/// look like a built in type. This will cause the generic YAML I/O conversion
1664/// to be used. To provide better control over the YAML conversion, you can
1665/// use this macro instead of typedef. It will create a class with one field
1666/// and automatic conversion operators to and from the base type.
1667/// Based on BOOST_STRONG_TYPEDEF
1668#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)struct _type { _type() = default; _type(const _base v) : value
(v) {} _type(const _type &v) = default; _type &operator
=(const _type &rhs) = default; _type &operator=(const
_base &rhs) { value = rhs; return *this; } operator const
_base & () const { return value; } bool operator==(const
_type &rhs) const { return value == rhs.value; } bool operator
==(const _base &rhs) const { return value == rhs; } bool operator
<(const _type &rhs) const { return value < rhs.value
; } _base value; using BaseType = _base; };
\
1669 struct _type { \
1670 _type() = default; \
1671 _type(const _base v) : value(v) {} \
1672 _type(const _type &v) = default; \
1673 _type &operator=(const _type &rhs) = default; \
1674 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
1675 operator const _base & () const { return value; } \
1676 bool operator==(const _type &rhs) const { return value == rhs.value; } \
1677 bool operator==(const _base &rhs) const { return value == rhs; } \
1678 bool operator<(const _type &rhs) const { return value < rhs.value; } \
1679 _base value; \
1680 using BaseType = _base; \
1681 };
1682
1683///
1684/// Use these types instead of uintXX_t in any mapping to have
1685/// its yaml output formatted as hexadecimal.
1686///
1687LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)struct Hex8 { Hex8() = default; Hex8(const uint8_t v) : value
(v) {} Hex8(const Hex8 &v) = default; Hex8 &operator=
(const Hex8 &rhs) = default; Hex8 &operator=(const uint8_t
&rhs) { value = rhs; return *this; } operator const uint8_t
& () const { return value; } bool operator==(const Hex8 &
rhs) const { return value == rhs.value; } bool operator==(const
uint8_t &rhs) const { return value == rhs; } bool operator
<(const Hex8 &rhs) const { return value < rhs.value
; } uint8_t value; using BaseType = uint8_t; };
1688LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)struct Hex16 { Hex16() = default; Hex16(const uint16_t v) : value
(v) {} Hex16(const Hex16 &v) = default; Hex16 &operator
=(const Hex16 &rhs) = default; Hex16 &operator=(const
uint16_t &rhs) { value = rhs; return *this; } operator const
uint16_t & () const { return value; } bool operator==(const
Hex16 &rhs) const { return value == rhs.value; } bool operator
==(const uint16_t &rhs) const { return value == rhs; } bool
operator<(const Hex16 &rhs) const { return value <
rhs.value; } uint16_t value; using BaseType = uint16_t; };
1689LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)struct Hex32 { Hex32() = default; Hex32(const uint32_t v) : value
(v) {} Hex32(const Hex32 &v) = default; Hex32 &operator
=(const Hex32 &rhs) = default; Hex32 &operator=(const
uint32_t &rhs) { value = rhs; return *this; } operator const
uint32_t & () const { return value; } bool operator==(const
Hex32 &rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const Hex32 &rhs) const { return value <
rhs.value; } uint32_t value; using BaseType = uint32_t; };
1690LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)struct Hex64 { Hex64() = default; Hex64(const uint64_t v) : value
(v) {} Hex64(const Hex64 &v) = default; Hex64 &operator
=(const Hex64 &rhs) = default; Hex64 &operator=(const
uint64_t &rhs) { value = rhs; return *this; } operator const
uint64_t & () const { return value; } bool operator==(const
Hex64 &rhs) const { return value == rhs.value; } bool operator
==(const uint64_t &rhs) const { return value == rhs; } bool
operator<(const Hex64 &rhs) const { return value <
rhs.value; } uint64_t value; using BaseType = uint64_t; };
1691
1692template<>
1693struct ScalarTraits<Hex8> {
1694 static void output(const Hex8 &, void *, raw_ostream &);
1695 static StringRef input(StringRef, void *, Hex8 &);
1696 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1697};
1698
1699template<>
1700struct ScalarTraits<Hex16> {
1701 static void output(const Hex16 &, void *, raw_ostream &);
1702 static StringRef input(StringRef, void *, Hex16 &);
1703 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1704};
1705
1706template<>
1707struct ScalarTraits<Hex32> {
1708 static void output(const Hex32 &, void *, raw_ostream &);
1709 static StringRef input(StringRef, void *, Hex32 &);
1710 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1711};
1712
1713template<>
1714struct ScalarTraits<Hex64> {
1715 static void output(const Hex64 &, void *, raw_ostream &);
1716 static StringRef input(StringRef, void *, Hex64 &);
1717 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1718};
1719
1720template <> struct ScalarTraits<VersionTuple> {
1721 static void output(const VersionTuple &Value, void *, llvm::raw_ostream &Out);
1722 static StringRef input(StringRef, void *, VersionTuple &);
1723 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1724};
1725
1726// Define non-member operator>> so that Input can stream in a document list.
1727template <typename T>
1728inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &>
1729operator>>(Input &yin, T &docList) {
1730 int i = 0;
1731 EmptyContext Ctx;
1732 while ( yin.setCurrentDocument() ) {
1733 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx);
1734 if ( yin.error() )
1735 return yin;
1736 yin.nextDocument();
1737 ++i;
1738 }
1739 return yin;
1740}
1741
1742// Define non-member operator>> so that Input can stream in a map as a document.
1743template <typename T>
1744inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Input &>
1745operator>>(Input &yin, T &docMap) {
1746 EmptyContext Ctx;
1747 yin.setCurrentDocument();
1748 yamlize(yin, docMap, true, Ctx);
1749 return yin;
1750}
1751
1752// Define non-member operator>> so that Input can stream in a sequence as
1753// a document.
1754template <typename T>
1755inline std::enable_if_t<has_SequenceTraits<T>::value, Input &>
1756operator>>(Input &yin, T &docSeq) {
1757 EmptyContext Ctx;
1758 if (yin.setCurrentDocument())
1759 yamlize(yin, docSeq, true, Ctx);
1760 return yin;
1761}
1762
1763// Define non-member operator>> so that Input can stream in a block scalar.
1764template <typename T>
1765inline std::enable_if_t<has_BlockScalarTraits<T>::value, Input &>
1766operator>>(Input &In, T &Val) {
1767 EmptyContext Ctx;
1768 if (In.setCurrentDocument())
1769 yamlize(In, Val, true, Ctx);
1770 return In;
1771}
1772
1773// Define non-member operator>> so that Input can stream in a string map.
1774template <typename T>
1775inline std::enable_if_t<has_CustomMappingTraits<T>::value, Input &>
1776operator>>(Input &In, T &Val) {
1777 EmptyContext Ctx;
1778 if (In.setCurrentDocument())
1779 yamlize(In, Val, true, Ctx);
1780 return In;
1781}
1782
1783// Define non-member operator>> so that Input can stream in a polymorphic type.
1784template <typename T>
1785inline std::enable_if_t<has_PolymorphicTraits<T>::value, Input &>
1786operator>>(Input &In, T &Val) {
1787 EmptyContext Ctx;
1788 if (In.setCurrentDocument())
1789 yamlize(In, Val, true, Ctx);
1790 return In;
1791}
1792
1793// Provide better error message about types missing a trait specialization
1794template <typename T>
1795inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Input &>
1796operator>>(Input &yin, T &docSeq) {
1797 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1798 return yin;
1799}
1800
1801// Define non-member operator<< so that Output can stream out document list.
1802template <typename T>
1803inline std::enable_if_t<has_DocumentListTraits<T>::value, Output &>
1804operator<<(Output &yout, T &docList) {
1805 EmptyContext Ctx;
1806 yout.beginDocuments();
1807 const size_t count = DocumentListTraits<T>::size(yout, docList);
1808 for(size_t i=0; i < count; ++i) {
1809 if ( yout.preflightDocument(i) ) {
1810 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true,
1811 Ctx);
1812 yout.postflightDocument();
1813 }
1814 }
1815 yout.endDocuments();
1816 return yout;
1817}
1818
1819// Define non-member operator<< so that Output can stream out a map.
1820template <typename T>
1821inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Output &>
1822operator<<(Output &yout, T &map) {
1823 EmptyContext Ctx;
1824 yout.beginDocuments();
1825 if ( yout.preflightDocument(0) ) {
1826 yamlize(yout, map, true, Ctx);
1827 yout.postflightDocument();
1828 }
1829 yout.endDocuments();
1830 return yout;
1831}
1832
1833// Define non-member operator<< so that Output can stream out a sequence.
1834template <typename T>
1835inline std::enable_if_t<has_SequenceTraits<T>::value, Output &>
1836operator<<(Output &yout, T &seq) {
1837 EmptyContext Ctx;
1838 yout.beginDocuments();
1839 if ( yout.preflightDocument(0) ) {
1840 yamlize(yout, seq, true, Ctx);
1841 yout.postflightDocument();
1842 }
1843 yout.endDocuments();
1844 return yout;
1845}
1846
1847// Define non-member operator<< so that Output can stream out a block scalar.
1848template <typename T>
1849inline std::enable_if_t<has_BlockScalarTraits<T>::value, Output &>
1850operator<<(Output &Out, T &Val) {
1851 EmptyContext Ctx;
1852 Out.beginDocuments();
1853 if (Out.preflightDocument(0)) {
1854 yamlize(Out, Val, true, Ctx);
1855 Out.postflightDocument();
1856 }
1857 Out.endDocuments();
1858 return Out;
1859}
1860
1861// Define non-member operator<< so that Output can stream out a string map.
1862template <typename T>
1863inline std::enable_if_t<has_CustomMappingTraits<T>::value, Output &>
1864operator<<(Output &Out, T &Val) {
1865 EmptyContext Ctx;
1866 Out.beginDocuments();
1867 if (Out.preflightDocument(0)) {
1868 yamlize(Out, Val, true, Ctx);
1869 Out.postflightDocument();
1870 }
1871 Out.endDocuments();
1872 return Out;
1873}
1874
1875// Define non-member operator<< so that Output can stream out a polymorphic
1876// type.
1877template <typename T>
1878inline std::enable_if_t<has_PolymorphicTraits<T>::value, Output &>
1879operator<<(Output &Out, T &Val) {
1880 EmptyContext Ctx;
1881 Out.beginDocuments();
1882 if (Out.preflightDocument(0)) {
1883 // FIXME: The parser does not support explicit documents terminated with a
1884 // plain scalar; the end-marker is included as part of the scalar token.
1885 assert(PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && "plain scalar documents are not supported")(static_cast<void> (0));
1886 yamlize(Out, Val, true, Ctx);
1887 Out.postflightDocument();
1888 }
1889 Out.endDocuments();
1890 return Out;
1891}
1892
1893// Provide better error message about types missing a trait specialization
1894template <typename T>
1895inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Output &>
1896operator<<(Output &yout, T &seq) {
1897 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1898 return yout;
1899}
1900
1901template <bool B> struct IsFlowSequenceBase {};
1902template <> struct IsFlowSequenceBase<true> { static const bool flow = true; };
1903
1904template <typename T, bool Flow>
1905struct SequenceTraitsImpl : IsFlowSequenceBase<Flow> {
1906private:
1907 using type = typename T::value_type;
1908
1909public:
1910 static size_t size(IO &io, T &seq) { return seq.size(); }
1911
1912 static type &element(IO &io, T &seq, size_t index) {
1913 if (index >= seq.size())
1914 seq.resize(index + 1);
1915 return seq[index];
1916 }
1917};
1918
1919// Simple helper to check an expression can be used as a bool-valued template
1920// argument.
1921template <bool> struct CheckIsBool { static const bool value = true; };
1922
1923// If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have
1924// SequenceTraits that do the obvious thing.
1925template <typename T>
1926struct SequenceTraits<
1927 std::vector<T>,
1928 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1929 : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
1930template <typename T, unsigned N>
1931struct SequenceTraits<
1932 SmallVector<T, N>,
1933 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1934 : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
1935template <typename T>
1936struct SequenceTraits<
1937 SmallVectorImpl<T>,
1938 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1939 : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {};
1940
1941// Sequences of fundamental types use flow formatting.
1942template <typename T>
1943struct SequenceElementTraits<T,
1944 std::enable_if_t<std::is_fundamental<T>::value>> {
1945 static const bool flow = true;
1946};
1947
1948// Sequences of strings use block formatting.
1949template<> struct SequenceElementTraits<std::string> {
1950 static const bool flow = false;
1951};
1952template<> struct SequenceElementTraits<StringRef> {
1953 static const bool flow = false;
1954};
1955template<> struct SequenceElementTraits<std::pair<std::string, std::string>> {
1956 static const bool flow = false;
1957};
1958
1959/// Implementation of CustomMappingTraits for std::map<std::string, T>.
1960template <typename T> struct StdMapStringCustomMappingTraitsImpl {
1961 using map_type = std::map<std::string, T>;
1962
1963 static void inputOne(IO &io, StringRef key, map_type &v) {
1964 io.mapRequired(key.str().c_str(), v[std::string(key)]);
1965 }
1966
1967 static void output(IO &io, map_type &v) {
1968 for (auto &p : v)
1969 io.mapRequired(p.first.c_str(), p.second);
1970 }
1971};
1972
1973} // end namespace yaml
1974} // end namespace llvm
1975
1976#define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<TYPE>::value && !std::is_same<TYPE, std::string
>::value && !std::is_same<TYPE, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<TYPE>
{ static const bool flow = FLOW; }; } }
\
1977 namespace llvm { \
1978 namespace yaml { \
1979 static_assert( \
1980 !std::is_fundamental<TYPE>::value && \
1981 !std::is_same<TYPE, std::string>::value && \
1982 !std::is_same<TYPE, llvm::StringRef>::value, \
1983 "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \
1984 template <> struct SequenceElementTraits<TYPE> { \
1985 static const bool flow = FLOW; \
1986 }; \
1987 } \
1988 }
1989
1990/// Utility for declaring that a std::vector of a particular type
1991/// should be considered a YAML sequence.
1992#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = false; }; } }
\
1993 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = false; }; } }
1994
1995/// Utility for declaring that a std::vector of a particular type
1996/// should be considered a YAML flow sequence.
1997#define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = true; }; } }
\
1998 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = true; }; } }
1999
2000#define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type)namespace llvm { namespace yaml { template <> struct MappingTraits
<Type> { static void mapping(IO &IO, Type &Obj)
; }; } }
\
2001 namespace llvm { \
2002 namespace yaml { \
2003 template <> struct MappingTraits<Type> { \
2004 static void mapping(IO &IO, Type &Obj); \
2005 }; \
2006 } \
2007 }
2008
2009#define LLVM_YAML_DECLARE_ENUM_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarEnumerationTraits
<Type> { static void enumeration(IO &io, Type &
Value); }; } }
\
2010 namespace llvm { \
2011 namespace yaml { \
2012 template <> struct ScalarEnumerationTraits<Type> { \
2013 static void enumeration(IO &io, Type &Value); \
2014 }; \
2015 } \
2016 }
2017
2018#define LLVM_YAML_DECLARE_BITSET_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarBitSetTraits
<Type> { static void bitset(IO &IO, Type &Options
); }; } }
\
2019 namespace llvm { \
2020 namespace yaml { \
2021 template <> struct ScalarBitSetTraits<Type> { \
2022 static void bitset(IO &IO, Type &Options); \
2023 }; \
2024 } \
2025 }
2026
2027#define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote)namespace llvm { namespace yaml { template <> struct ScalarTraits
<Type> { static void output(const Type &Value, void
*ctx, raw_ostream &Out); static StringRef input(StringRef
Scalar, void *ctxt, Type &Value); static QuotingType mustQuote
(StringRef) { return MustQuote; } }; } }
\
2028 namespace llvm { \
2029 namespace yaml { \
2030 template <> struct ScalarTraits<Type> { \
2031 static void output(const Type &Value, void *ctx, raw_ostream &Out); \
2032 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
2033 static QuotingType mustQuote(StringRef) { return MustQuote; } \
2034 }; \
2035 } \
2036 }
2037
2038/// Utility for declaring that a std::vector of a particular type
2039/// should be considered a YAML document list.
2040#define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type)namespace llvm { namespace yaml { template <unsigned N>
struct DocumentListTraits<SmallVector<_type, N>>
: public SequenceTraitsImpl<SmallVector<_type, N>, false
> {}; template <> struct DocumentListTraits<std::
vector<_type>> : public SequenceTraitsImpl<std::vector
<_type>, false> {}; } }
\
2041 namespace llvm { \
2042 namespace yaml { \
2043 template <unsigned N> \
2044 struct DocumentListTraits<SmallVector<_type, N>> \
2045 : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \
2046 template <> \
2047 struct DocumentListTraits<std::vector<_type>> \
2048 : public SequenceTraitsImpl<std::vector<_type>, false> {}; \
2049 } \
2050 }
2051
2052/// Utility for declaring that std::map<std::string, _type> should be considered
2053/// a YAML map.
2054#define LLVM_YAML_IS_STRING_MAP(_type)namespace llvm { namespace yaml { template <> struct CustomMappingTraits
<std::map<std::string, _type>> : public StdMapStringCustomMappingTraitsImpl
<_type> {}; } }
\
2055 namespace llvm { \
2056 namespace yaml { \
2057 template <> \
2058 struct CustomMappingTraits<std::map<std::string, _type>> \
2059 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \
2060 } \
2061 }
2062
2063LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex64>::value && !std::is_same<
llvm::yaml::Hex64, std::string>::value && !std::is_same
<llvm::yaml::Hex64, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex64> { static const bool flow = true; }; } }
2064LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex32>::value && !std::is_same<
llvm::yaml::Hex32, std::string>::value && !std::is_same
<llvm::yaml::Hex32, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex32> { static const bool flow = true; }; } }
2065LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex16>::value && !std::is_same<
llvm::yaml::Hex16, std::string>::value && !std::is_same
<llvm::yaml::Hex16, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex16> { static const bool flow = true; }; } }
2066LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex8>::value && !std::is_same<llvm
::yaml::Hex8, std::string>::value && !std::is_same
<llvm::yaml::Hex8, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex8> { static const bool flow = true; }; } }
2067
2068#endif // LLVM_SUPPORT_YAMLTRAITS_H