File: | include/llvm/ObjectYAML/ELFYAML.h |
Warning: | line 48, column 1 The left operand of '==' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This file defines classes for handling the YAML representation of ELF. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #include "llvm/ObjectYAML/ELFYAML.h" | |||
15 | #include "llvm/ADT/StringRef.h" | |||
16 | #include "llvm/BinaryFormat/ELF.h" | |||
17 | #include "llvm/Support/Casting.h" | |||
18 | #include "llvm/Support/ErrorHandling.h" | |||
19 | #include "llvm/Support/MipsABIFlags.h" | |||
20 | #include "llvm/Support/YAMLTraits.h" | |||
21 | #include <cassert> | |||
22 | #include <cstdint> | |||
23 | ||||
24 | namespace llvm { | |||
25 | ||||
26 | ELFYAML::Section::~Section() = default; | |||
27 | ||||
28 | namespace yaml { | |||
29 | ||||
30 | void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration( | |||
31 | IO &IO, ELFYAML::ELF_ET &Value) { | |||
32 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
33 | ECase(ET_NONE); | |||
34 | ECase(ET_REL); | |||
35 | ECase(ET_EXEC); | |||
36 | ECase(ET_DYN); | |||
37 | ECase(ET_CORE); | |||
38 | #undef ECase | |||
39 | IO.enumFallback<Hex16>(Value); | |||
40 | } | |||
41 | ||||
42 | void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration( | |||
43 | IO &IO, ELFYAML::ELF_PT &Value) { | |||
44 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
45 | ECase(PT_NULL); | |||
46 | ECase(PT_LOAD); | |||
47 | ECase(PT_DYNAMIC); | |||
48 | ECase(PT_INTERP); | |||
49 | ECase(PT_NOTE); | |||
50 | ECase(PT_SHLIB); | |||
51 | ECase(PT_PHDR); | |||
52 | ECase(PT_TLS); | |||
53 | ECase(PT_GNU_EH_FRAME); | |||
54 | #undef ECase | |||
55 | IO.enumFallback<Hex32>(Value); | |||
56 | } | |||
57 | ||||
58 | void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration( | |||
59 | IO &IO, ELFYAML::ELF_EM &Value) { | |||
60 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
61 | ECase(EM_NONE); | |||
62 | ECase(EM_M32); | |||
63 | ECase(EM_SPARC); | |||
64 | ECase(EM_386); | |||
65 | ECase(EM_68K); | |||
66 | ECase(EM_88K); | |||
67 | ECase(EM_IAMCU); | |||
68 | ECase(EM_860); | |||
69 | ECase(EM_MIPS); | |||
70 | ECase(EM_S370); | |||
71 | ECase(EM_MIPS_RS3_LE); | |||
72 | ECase(EM_PARISC); | |||
73 | ECase(EM_VPP500); | |||
74 | ECase(EM_SPARC32PLUS); | |||
75 | ECase(EM_960); | |||
76 | ECase(EM_PPC); | |||
77 | ECase(EM_PPC64); | |||
78 | ECase(EM_S390); | |||
79 | ECase(EM_SPU); | |||
80 | ECase(EM_V800); | |||
81 | ECase(EM_FR20); | |||
82 | ECase(EM_RH32); | |||
83 | ECase(EM_RCE); | |||
84 | ECase(EM_ARM); | |||
85 | ECase(EM_ALPHA); | |||
86 | ECase(EM_SH); | |||
87 | ECase(EM_SPARCV9); | |||
88 | ECase(EM_TRICORE); | |||
89 | ECase(EM_ARC); | |||
90 | ECase(EM_H8_300); | |||
91 | ECase(EM_H8_300H); | |||
92 | ECase(EM_H8S); | |||
93 | ECase(EM_H8_500); | |||
94 | ECase(EM_IA_64); | |||
95 | ECase(EM_MIPS_X); | |||
96 | ECase(EM_COLDFIRE); | |||
97 | ECase(EM_68HC12); | |||
98 | ECase(EM_MMA); | |||
99 | ECase(EM_PCP); | |||
100 | ECase(EM_NCPU); | |||
101 | ECase(EM_NDR1); | |||
102 | ECase(EM_STARCORE); | |||
103 | ECase(EM_ME16); | |||
104 | ECase(EM_ST100); | |||
105 | ECase(EM_TINYJ); | |||
106 | ECase(EM_X86_64); | |||
107 | ECase(EM_PDSP); | |||
108 | ECase(EM_PDP10); | |||
109 | ECase(EM_PDP11); | |||
110 | ECase(EM_FX66); | |||
111 | ECase(EM_ST9PLUS); | |||
112 | ECase(EM_ST7); | |||
113 | ECase(EM_68HC16); | |||
114 | ECase(EM_68HC11); | |||
115 | ECase(EM_68HC08); | |||
116 | ECase(EM_68HC05); | |||
117 | ECase(EM_SVX); | |||
118 | ECase(EM_ST19); | |||
119 | ECase(EM_VAX); | |||
120 | ECase(EM_CRIS); | |||
121 | ECase(EM_JAVELIN); | |||
122 | ECase(EM_FIREPATH); | |||
123 | ECase(EM_ZSP); | |||
124 | ECase(EM_MMIX); | |||
125 | ECase(EM_HUANY); | |||
126 | ECase(EM_PRISM); | |||
127 | ECase(EM_AVR); | |||
128 | ECase(EM_FR30); | |||
129 | ECase(EM_D10V); | |||
130 | ECase(EM_D30V); | |||
131 | ECase(EM_V850); | |||
132 | ECase(EM_M32R); | |||
133 | ECase(EM_MN10300); | |||
134 | ECase(EM_MN10200); | |||
135 | ECase(EM_PJ); | |||
136 | ECase(EM_OPENRISC); | |||
137 | ECase(EM_ARC_COMPACT); | |||
138 | ECase(EM_XTENSA); | |||
139 | ECase(EM_VIDEOCORE); | |||
140 | ECase(EM_TMM_GPP); | |||
141 | ECase(EM_NS32K); | |||
142 | ECase(EM_TPC); | |||
143 | ECase(EM_SNP1K); | |||
144 | ECase(EM_ST200); | |||
145 | ECase(EM_IP2K); | |||
146 | ECase(EM_MAX); | |||
147 | ECase(EM_CR); | |||
148 | ECase(EM_F2MC16); | |||
149 | ECase(EM_MSP430); | |||
150 | ECase(EM_BLACKFIN); | |||
151 | ECase(EM_SE_C33); | |||
152 | ECase(EM_SEP); | |||
153 | ECase(EM_ARCA); | |||
154 | ECase(EM_UNICORE); | |||
155 | ECase(EM_EXCESS); | |||
156 | ECase(EM_DXP); | |||
157 | ECase(EM_ALTERA_NIOS2); | |||
158 | ECase(EM_CRX); | |||
159 | ECase(EM_XGATE); | |||
160 | ECase(EM_C166); | |||
161 | ECase(EM_M16C); | |||
162 | ECase(EM_DSPIC30F); | |||
163 | ECase(EM_CE); | |||
164 | ECase(EM_M32C); | |||
165 | ECase(EM_TSK3000); | |||
166 | ECase(EM_RS08); | |||
167 | ECase(EM_SHARC); | |||
168 | ECase(EM_ECOG2); | |||
169 | ECase(EM_SCORE7); | |||
170 | ECase(EM_DSP24); | |||
171 | ECase(EM_VIDEOCORE3); | |||
172 | ECase(EM_LATTICEMICO32); | |||
173 | ECase(EM_SE_C17); | |||
174 | ECase(EM_TI_C6000); | |||
175 | ECase(EM_TI_C2000); | |||
176 | ECase(EM_TI_C5500); | |||
177 | ECase(EM_MMDSP_PLUS); | |||
178 | ECase(EM_CYPRESS_M8C); | |||
179 | ECase(EM_R32C); | |||
180 | ECase(EM_TRIMEDIA); | |||
181 | ECase(EM_HEXAGON); | |||
182 | ECase(EM_8051); | |||
183 | ECase(EM_STXP7X); | |||
184 | ECase(EM_NDS32); | |||
185 | ECase(EM_ECOG1); | |||
186 | ECase(EM_ECOG1X); | |||
187 | ECase(EM_MAXQ30); | |||
188 | ECase(EM_XIMO16); | |||
189 | ECase(EM_MANIK); | |||
190 | ECase(EM_CRAYNV2); | |||
191 | ECase(EM_RX); | |||
192 | ECase(EM_METAG); | |||
193 | ECase(EM_MCST_ELBRUS); | |||
194 | ECase(EM_ECOG16); | |||
195 | ECase(EM_CR16); | |||
196 | ECase(EM_ETPU); | |||
197 | ECase(EM_SLE9X); | |||
198 | ECase(EM_L10M); | |||
199 | ECase(EM_K10M); | |||
200 | ECase(EM_AARCH64); | |||
201 | ECase(EM_AVR32); | |||
202 | ECase(EM_STM8); | |||
203 | ECase(EM_TILE64); | |||
204 | ECase(EM_TILEPRO); | |||
205 | ECase(EM_CUDA); | |||
206 | ECase(EM_TILEGX); | |||
207 | ECase(EM_CLOUDSHIELD); | |||
208 | ECase(EM_COREA_1ST); | |||
209 | ECase(EM_COREA_2ND); | |||
210 | ECase(EM_ARC_COMPACT2); | |||
211 | ECase(EM_OPEN8); | |||
212 | ECase(EM_RL78); | |||
213 | ECase(EM_VIDEOCORE5); | |||
214 | ECase(EM_78KOR); | |||
215 | ECase(EM_56800EX); | |||
216 | ECase(EM_AMDGPU); | |||
217 | ECase(EM_RISCV); | |||
218 | ECase(EM_LANAI); | |||
219 | ECase(EM_BPF); | |||
220 | #undef ECase | |||
221 | } | |||
222 | ||||
223 | void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration( | |||
224 | IO &IO, ELFYAML::ELF_ELFCLASS &Value) { | |||
225 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
226 | // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it | |||
227 | // here. | |||
228 | ECase(ELFCLASS32); | |||
229 | ECase(ELFCLASS64); | |||
230 | #undef ECase | |||
231 | } | |||
232 | ||||
233 | void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration( | |||
234 | IO &IO, ELFYAML::ELF_ELFDATA &Value) { | |||
235 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
236 | // Since the semantics of ELFDATANONE is "invalid", just don't accept it | |||
237 | // here. | |||
238 | ECase(ELFDATA2LSB); | |||
239 | ECase(ELFDATA2MSB); | |||
240 | #undef ECase | |||
241 | } | |||
242 | ||||
243 | void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration( | |||
244 | IO &IO, ELFYAML::ELF_ELFOSABI &Value) { | |||
245 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
246 | ECase(ELFOSABI_NONE); | |||
247 | ECase(ELFOSABI_HPUX); | |||
248 | ECase(ELFOSABI_NETBSD); | |||
249 | ECase(ELFOSABI_GNU); | |||
250 | ECase(ELFOSABI_HURD); | |||
251 | ECase(ELFOSABI_SOLARIS); | |||
252 | ECase(ELFOSABI_AIX); | |||
253 | ECase(ELFOSABI_IRIX); | |||
254 | ECase(ELFOSABI_FREEBSD); | |||
255 | ECase(ELFOSABI_TRU64); | |||
256 | ECase(ELFOSABI_MODESTO); | |||
257 | ECase(ELFOSABI_OPENBSD); | |||
258 | ECase(ELFOSABI_OPENVMS); | |||
259 | ECase(ELFOSABI_NSK); | |||
260 | ECase(ELFOSABI_AROS); | |||
261 | ECase(ELFOSABI_FENIXOS); | |||
262 | ECase(ELFOSABI_CLOUDABI); | |||
263 | ECase(ELFOSABI_AMDGPU_HSA); | |||
264 | ECase(ELFOSABI_AMDGPU_PAL); | |||
265 | ECase(ELFOSABI_AMDGPU_MESA3D); | |||
266 | ECase(ELFOSABI_ARM); | |||
267 | ECase(ELFOSABI_C6000_ELFABI); | |||
268 | ECase(ELFOSABI_C6000_LINUX); | |||
269 | ECase(ELFOSABI_STANDALONE); | |||
270 | #undef ECase | |||
271 | } | |||
272 | ||||
273 | void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO, | |||
274 | ELFYAML::ELF_EF &Value) { | |||
275 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
276 | assert(Object && "The IO context is not initialized")((Object && "The IO context is not initialized") ? static_cast <void> (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 276, __PRETTY_FUNCTION__)); | |||
277 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
278 | #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M) | |||
279 | switch (Object->Header.Machine) { | |||
280 | case ELF::EM_ARM: | |||
281 | BCase(EF_ARM_SOFT_FLOAT); | |||
282 | BCase(EF_ARM_VFP_FLOAT); | |||
283 | BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK); | |||
284 | BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK); | |||
285 | BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK); | |||
286 | BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK); | |||
287 | BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK); | |||
288 | BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK); | |||
289 | break; | |||
290 | case ELF::EM_MIPS: | |||
291 | BCase(EF_MIPS_NOREORDER); | |||
292 | BCase(EF_MIPS_PIC); | |||
293 | BCase(EF_MIPS_CPIC); | |||
294 | BCase(EF_MIPS_ABI2); | |||
295 | BCase(EF_MIPS_32BITMODE); | |||
296 | BCase(EF_MIPS_FP64); | |||
297 | BCase(EF_MIPS_NAN2008); | |||
298 | BCase(EF_MIPS_MICROMIPS); | |||
299 | BCase(EF_MIPS_ARCH_ASE_M16); | |||
300 | BCase(EF_MIPS_ARCH_ASE_MDMX); | |||
301 | BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI); | |||
302 | BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI); | |||
303 | BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI); | |||
304 | BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI); | |||
305 | BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH); | |||
306 | BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH); | |||
307 | BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH); | |||
308 | BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH); | |||
309 | BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH); | |||
310 | BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH); | |||
311 | BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH); | |||
312 | BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH); | |||
313 | BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH); | |||
314 | BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH); | |||
315 | BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH); | |||
316 | BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH); | |||
317 | BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH); | |||
318 | BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH); | |||
319 | BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH); | |||
320 | BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH); | |||
321 | BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH); | |||
322 | BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH); | |||
323 | BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH); | |||
324 | BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH); | |||
325 | BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH); | |||
326 | BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH); | |||
327 | BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH); | |||
328 | BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH); | |||
329 | BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH); | |||
330 | BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH); | |||
331 | BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH); | |||
332 | BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH); | |||
333 | BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH); | |||
334 | break; | |||
335 | case ELF::EM_HEXAGON: | |||
336 | BCase(EF_HEXAGON_MACH_V2); | |||
337 | BCase(EF_HEXAGON_MACH_V3); | |||
338 | BCase(EF_HEXAGON_MACH_V4); | |||
339 | BCase(EF_HEXAGON_MACH_V5); | |||
340 | BCase(EF_HEXAGON_MACH_V55); | |||
341 | BCase(EF_HEXAGON_MACH_V60); | |||
342 | BCase(EF_HEXAGON_MACH_V62); | |||
343 | BCase(EF_HEXAGON_MACH_V65); | |||
344 | BCase(EF_HEXAGON_ISA_V2); | |||
345 | BCase(EF_HEXAGON_ISA_V3); | |||
346 | BCase(EF_HEXAGON_ISA_V4); | |||
347 | BCase(EF_HEXAGON_ISA_V5); | |||
348 | BCase(EF_HEXAGON_ISA_V55); | |||
349 | BCase(EF_HEXAGON_ISA_V60); | |||
350 | BCase(EF_HEXAGON_ISA_V62); | |||
351 | BCase(EF_HEXAGON_ISA_V65); | |||
352 | break; | |||
353 | case ELF::EM_AVR: | |||
354 | BCase(EF_AVR_ARCH_AVR1); | |||
355 | BCase(EF_AVR_ARCH_AVR2); | |||
356 | BCase(EF_AVR_ARCH_AVR25); | |||
357 | BCase(EF_AVR_ARCH_AVR3); | |||
358 | BCase(EF_AVR_ARCH_AVR31); | |||
359 | BCase(EF_AVR_ARCH_AVR35); | |||
360 | BCase(EF_AVR_ARCH_AVR4); | |||
361 | BCase(EF_AVR_ARCH_AVR51); | |||
362 | BCase(EF_AVR_ARCH_AVR6); | |||
363 | BCase(EF_AVR_ARCH_AVRTINY); | |||
364 | BCase(EF_AVR_ARCH_XMEGA1); | |||
365 | BCase(EF_AVR_ARCH_XMEGA2); | |||
366 | BCase(EF_AVR_ARCH_XMEGA3); | |||
367 | BCase(EF_AVR_ARCH_XMEGA4); | |||
368 | BCase(EF_AVR_ARCH_XMEGA5); | |||
369 | BCase(EF_AVR_ARCH_XMEGA6); | |||
370 | BCase(EF_AVR_ARCH_XMEGA7); | |||
371 | break; | |||
372 | case ELF::EM_RISCV: | |||
373 | BCase(EF_RISCV_RVC); | |||
374 | BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI); | |||
375 | BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI); | |||
376 | BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI); | |||
377 | BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI); | |||
378 | BCase(EF_RISCV_RVE); | |||
379 | break; | |||
380 | case ELF::EM_AMDGPU: | |||
381 | BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH); | |||
382 | BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH); | |||
383 | BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH); | |||
384 | BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH); | |||
385 | BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH); | |||
386 | BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH); | |||
387 | BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH); | |||
388 | BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH); | |||
389 | BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH); | |||
390 | BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH); | |||
391 | BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH); | |||
392 | BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH); | |||
393 | BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH); | |||
394 | BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH); | |||
395 | BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH); | |||
396 | BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH); | |||
397 | BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH); | |||
398 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH); | |||
399 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH); | |||
400 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH); | |||
401 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH); | |||
402 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH); | |||
403 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH); | |||
404 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH); | |||
405 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH); | |||
406 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH); | |||
407 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH); | |||
408 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH); | |||
409 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH); | |||
410 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH); | |||
411 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH); | |||
412 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH); | |||
413 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH); | |||
414 | BCase(EF_AMDGPU_XNACK); | |||
415 | BCase(EF_AMDGPU_SRAM_ECC); | |||
416 | break; | |||
417 | case ELF::EM_X86_64: | |||
418 | break; | |||
419 | default: | |||
420 | llvm_unreachable("Unsupported architecture")::llvm::llvm_unreachable_internal("Unsupported architecture", "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 420); | |||
421 | } | |||
422 | #undef BCase | |||
423 | #undef BCaseMask | |||
424 | } | |||
425 | ||||
426 | void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration( | |||
427 | IO &IO, ELFYAML::ELF_SHT &Value) { | |||
428 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
429 | assert(Object && "The IO context is not initialized")((Object && "The IO context is not initialized") ? static_cast <void> (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 429, __PRETTY_FUNCTION__)); | |||
430 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
431 | ECase(SHT_NULL); | |||
432 | ECase(SHT_PROGBITS); | |||
433 | ECase(SHT_SYMTAB); | |||
434 | // FIXME: Issue a diagnostic with this information. | |||
435 | ECase(SHT_STRTAB); | |||
436 | ECase(SHT_RELA); | |||
437 | ECase(SHT_HASH); | |||
438 | ECase(SHT_DYNAMIC); | |||
439 | ECase(SHT_NOTE); | |||
440 | ECase(SHT_NOBITS); | |||
441 | ECase(SHT_REL); | |||
442 | ECase(SHT_SHLIB); | |||
443 | ECase(SHT_DYNSYM); | |||
444 | ECase(SHT_INIT_ARRAY); | |||
445 | ECase(SHT_FINI_ARRAY); | |||
446 | ECase(SHT_PREINIT_ARRAY); | |||
447 | ECase(SHT_GROUP); | |||
448 | ECase(SHT_SYMTAB_SHNDX); | |||
449 | ECase(SHT_RELR); | |||
450 | ECase(SHT_LOOS); | |||
451 | ECase(SHT_ANDROID_REL); | |||
452 | ECase(SHT_ANDROID_RELA); | |||
453 | ECase(SHT_ANDROID_RELR); | |||
454 | ECase(SHT_LLVM_ODRTAB); | |||
455 | ECase(SHT_LLVM_LINKER_OPTIONS); | |||
456 | ECase(SHT_LLVM_CALL_GRAPH_PROFILE); | |||
457 | ECase(SHT_LLVM_ADDRSIG); | |||
458 | ECase(SHT_GNU_ATTRIBUTES); | |||
459 | ECase(SHT_GNU_HASH); | |||
460 | ECase(SHT_GNU_verdef); | |||
461 | ECase(SHT_GNU_verneed); | |||
462 | ECase(SHT_GNU_versym); | |||
463 | ECase(SHT_HIOS); | |||
464 | ECase(SHT_LOPROC); | |||
465 | switch (Object->Header.Machine) { | |||
466 | case ELF::EM_ARM: | |||
467 | ECase(SHT_ARM_EXIDX); | |||
468 | ECase(SHT_ARM_PREEMPTMAP); | |||
469 | ECase(SHT_ARM_ATTRIBUTES); | |||
470 | ECase(SHT_ARM_DEBUGOVERLAY); | |||
471 | ECase(SHT_ARM_OVERLAYSECTION); | |||
472 | break; | |||
473 | case ELF::EM_HEXAGON: | |||
474 | ECase(SHT_HEX_ORDERED); | |||
475 | break; | |||
476 | case ELF::EM_X86_64: | |||
477 | ECase(SHT_X86_64_UNWIND); | |||
478 | break; | |||
479 | case ELF::EM_MIPS: | |||
480 | ECase(SHT_MIPS_REGINFO); | |||
481 | ECase(SHT_MIPS_OPTIONS); | |||
482 | ECase(SHT_MIPS_ABIFLAGS); | |||
483 | break; | |||
484 | default: | |||
485 | // Nothing to do. | |||
486 | break; | |||
487 | } | |||
488 | #undef ECase | |||
489 | } | |||
490 | ||||
491 | void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO, | |||
492 | ELFYAML::ELF_PF &Value) { | |||
493 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
494 | BCase(PF_X); | |||
495 | BCase(PF_W); | |||
496 | BCase(PF_R); | |||
497 | } | |||
498 | ||||
499 | void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO, | |||
500 | ELFYAML::ELF_SHF &Value) { | |||
501 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
502 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
503 | BCase(SHF_WRITE); | |||
504 | BCase(SHF_ALLOC); | |||
505 | BCase(SHF_EXCLUDE); | |||
506 | BCase(SHF_EXECINSTR); | |||
507 | BCase(SHF_MERGE); | |||
508 | BCase(SHF_STRINGS); | |||
509 | BCase(SHF_INFO_LINK); | |||
510 | BCase(SHF_LINK_ORDER); | |||
511 | BCase(SHF_OS_NONCONFORMING); | |||
512 | BCase(SHF_GROUP); | |||
513 | BCase(SHF_TLS); | |||
514 | BCase(SHF_COMPRESSED); | |||
515 | switch (Object->Header.Machine) { | |||
516 | case ELF::EM_ARM: | |||
517 | BCase(SHF_ARM_PURECODE); | |||
518 | break; | |||
519 | case ELF::EM_HEXAGON: | |||
520 | BCase(SHF_HEX_GPREL); | |||
521 | break; | |||
522 | case ELF::EM_MIPS: | |||
523 | BCase(SHF_MIPS_NODUPES); | |||
524 | BCase(SHF_MIPS_NAMES); | |||
525 | BCase(SHF_MIPS_LOCAL); | |||
526 | BCase(SHF_MIPS_NOSTRIP); | |||
527 | BCase(SHF_MIPS_GPREL); | |||
528 | BCase(SHF_MIPS_MERGE); | |||
529 | BCase(SHF_MIPS_ADDR); | |||
530 | BCase(SHF_MIPS_STRING); | |||
531 | break; | |||
532 | case ELF::EM_X86_64: | |||
533 | BCase(SHF_X86_64_LARGE); | |||
534 | break; | |||
535 | default: | |||
536 | // Nothing to do. | |||
537 | break; | |||
538 | } | |||
539 | #undef BCase | |||
540 | } | |||
541 | ||||
542 | void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration( | |||
543 | IO &IO, ELFYAML::ELF_SHN &Value) { | |||
544 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
545 | ECase(SHN_UNDEF); | |||
546 | ECase(SHN_LORESERVE); | |||
547 | ECase(SHN_LOPROC); | |||
548 | ECase(SHN_HIPROC); | |||
549 | ECase(SHN_LOOS); | |||
550 | ECase(SHN_HIOS); | |||
551 | ECase(SHN_ABS); | |||
552 | ECase(SHN_COMMON); | |||
553 | ECase(SHN_XINDEX); | |||
554 | ECase(SHN_HIRESERVE); | |||
555 | ECase(SHN_HEXAGON_SCOMMON); | |||
556 | ECase(SHN_HEXAGON_SCOMMON_1); | |||
557 | ECase(SHN_HEXAGON_SCOMMON_2); | |||
558 | ECase(SHN_HEXAGON_SCOMMON_4); | |||
559 | ECase(SHN_HEXAGON_SCOMMON_8); | |||
560 | #undef ECase | |||
561 | IO.enumFallback<Hex32>(Value); | |||
562 | } | |||
563 | ||||
564 | void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration( | |||
565 | IO &IO, ELFYAML::ELF_STT &Value) { | |||
566 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
567 | ECase(STT_NOTYPE); | |||
568 | ECase(STT_OBJECT); | |||
569 | ECase(STT_FUNC); | |||
570 | ECase(STT_SECTION); | |||
571 | ECase(STT_FILE); | |||
572 | ECase(STT_COMMON); | |||
573 | ECase(STT_TLS); | |||
574 | ECase(STT_GNU_IFUNC); | |||
575 | #undef ECase | |||
576 | } | |||
577 | ||||
578 | void ScalarEnumerationTraits<ELFYAML::ELF_STV>::enumeration( | |||
579 | IO &IO, ELFYAML::ELF_STV &Value) { | |||
580 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
581 | ECase(STV_DEFAULT); | |||
582 | ECase(STV_INTERNAL); | |||
583 | ECase(STV_HIDDEN); | |||
584 | ECase(STV_PROTECTED); | |||
585 | #undef ECase | |||
586 | } | |||
587 | ||||
588 | void ScalarBitSetTraits<ELFYAML::ELF_STO>::bitset(IO &IO, | |||
589 | ELFYAML::ELF_STO &Value) { | |||
590 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
591 | assert(Object && "The IO context is not initialized")((Object && "The IO context is not initialized") ? static_cast <void> (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 591, __PRETTY_FUNCTION__)); | |||
592 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
593 | switch (Object->Header.Machine) { | |||
594 | case ELF::EM_MIPS: | |||
595 | BCase(STO_MIPS_OPTIONAL); | |||
596 | BCase(STO_MIPS_PLT); | |||
597 | BCase(STO_MIPS_PIC); | |||
598 | BCase(STO_MIPS_MICROMIPS); | |||
599 | break; | |||
600 | default: | |||
601 | break; // Nothing to do | |||
602 | } | |||
603 | #undef BCase | |||
604 | #undef BCaseMask | |||
605 | } | |||
606 | ||||
607 | void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration( | |||
608 | IO &IO, ELFYAML::ELF_RSS &Value) { | |||
609 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
610 | ECase(RSS_UNDEF); | |||
611 | ECase(RSS_GP); | |||
612 | ECase(RSS_GP0); | |||
613 | ECase(RSS_LOC); | |||
614 | #undef ECase | |||
615 | } | |||
616 | ||||
617 | void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration( | |||
618 | IO &IO, ELFYAML::ELF_REL &Value) { | |||
619 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
620 | assert(Object && "The IO context is not initialized")((Object && "The IO context is not initialized") ? static_cast <void> (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 620, __PRETTY_FUNCTION__)); | |||
621 | #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X); | |||
622 | switch (Object->Header.Machine) { | |||
623 | case ELF::EM_X86_64: | |||
624 | #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" | |||
625 | break; | |||
626 | case ELF::EM_MIPS: | |||
627 | #include "llvm/BinaryFormat/ELFRelocs/Mips.def" | |||
628 | break; | |||
629 | case ELF::EM_HEXAGON: | |||
630 | #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" | |||
631 | break; | |||
632 | case ELF::EM_386: | |||
633 | case ELF::EM_IAMCU: | |||
634 | #include "llvm/BinaryFormat/ELFRelocs/i386.def" | |||
635 | break; | |||
636 | case ELF::EM_AARCH64: | |||
637 | #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" | |||
638 | break; | |||
639 | case ELF::EM_ARM: | |||
640 | #include "llvm/BinaryFormat/ELFRelocs/ARM.def" | |||
641 | break; | |||
642 | case ELF::EM_ARC: | |||
643 | #include "llvm/BinaryFormat/ELFRelocs/ARC.def" | |||
644 | break; | |||
645 | case ELF::EM_RISCV: | |||
646 | #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" | |||
647 | break; | |||
648 | case ELF::EM_LANAI: | |||
649 | #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" | |||
650 | break; | |||
651 | case ELF::EM_AMDGPU: | |||
652 | #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" | |||
653 | break; | |||
654 | case ELF::EM_BPF: | |||
655 | #include "llvm/BinaryFormat/ELFRelocs/BPF.def" | |||
656 | break; | |||
657 | default: | |||
658 | llvm_unreachable("Unsupported architecture")::llvm::llvm_unreachable_internal("Unsupported architecture", "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 658); | |||
659 | } | |||
660 | #undef ELF_RELOC | |||
661 | IO.enumFallback<Hex32>(Value); | |||
662 | } | |||
663 | ||||
664 | void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration( | |||
665 | IO &IO, ELFYAML::MIPS_AFL_REG &Value) { | |||
666 | #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) | |||
667 | ECase(REG_NONE); | |||
668 | ECase(REG_32); | |||
669 | ECase(REG_64); | |||
670 | ECase(REG_128); | |||
671 | #undef ECase | |||
672 | } | |||
673 | ||||
674 | void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration( | |||
675 | IO &IO, ELFYAML::MIPS_ABI_FP &Value) { | |||
676 | #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X) | |||
677 | ECase(FP_ANY); | |||
678 | ECase(FP_DOUBLE); | |||
679 | ECase(FP_SINGLE); | |||
680 | ECase(FP_SOFT); | |||
681 | ECase(FP_OLD_64); | |||
682 | ECase(FP_XX); | |||
683 | ECase(FP_64); | |||
684 | ECase(FP_64A); | |||
685 | #undef ECase | |||
686 | } | |||
687 | ||||
688 | void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration( | |||
689 | IO &IO, ELFYAML::MIPS_AFL_EXT &Value) { | |||
690 | #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) | |||
691 | ECase(EXT_NONE); | |||
692 | ECase(EXT_XLR); | |||
693 | ECase(EXT_OCTEON2); | |||
694 | ECase(EXT_OCTEONP); | |||
695 | ECase(EXT_LOONGSON_3A); | |||
696 | ECase(EXT_OCTEON); | |||
697 | ECase(EXT_5900); | |||
698 | ECase(EXT_4650); | |||
699 | ECase(EXT_4010); | |||
700 | ECase(EXT_4100); | |||
701 | ECase(EXT_3900); | |||
702 | ECase(EXT_10000); | |||
703 | ECase(EXT_SB1); | |||
704 | ECase(EXT_4111); | |||
705 | ECase(EXT_4120); | |||
706 | ECase(EXT_5400); | |||
707 | ECase(EXT_5500); | |||
708 | ECase(EXT_LOONGSON_2E); | |||
709 | ECase(EXT_LOONGSON_2F); | |||
710 | ECase(EXT_OCTEON3); | |||
711 | #undef ECase | |||
712 | } | |||
713 | ||||
714 | void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration( | |||
715 | IO &IO, ELFYAML::MIPS_ISA &Value) { | |||
716 | IO.enumCase(Value, "MIPS1", 1); | |||
717 | IO.enumCase(Value, "MIPS2", 2); | |||
718 | IO.enumCase(Value, "MIPS3", 3); | |||
719 | IO.enumCase(Value, "MIPS4", 4); | |||
720 | IO.enumCase(Value, "MIPS5", 5); | |||
721 | IO.enumCase(Value, "MIPS32", 32); | |||
722 | IO.enumCase(Value, "MIPS64", 64); | |||
723 | } | |||
724 | ||||
725 | void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset( | |||
726 | IO &IO, ELFYAML::MIPS_AFL_ASE &Value) { | |||
727 | #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X) | |||
728 | BCase(DSP); | |||
729 | BCase(DSPR2); | |||
730 | BCase(EVA); | |||
731 | BCase(MCU); | |||
732 | BCase(MDMX); | |||
733 | BCase(MIPS3D); | |||
734 | BCase(MT); | |||
735 | BCase(SMARTMIPS); | |||
736 | BCase(VIRT); | |||
737 | BCase(MSA); | |||
738 | BCase(MIPS16); | |||
739 | BCase(MICROMIPS); | |||
740 | BCase(XPA); | |||
741 | #undef BCase | |||
742 | } | |||
743 | ||||
744 | void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset( | |||
745 | IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) { | |||
746 | #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X) | |||
747 | BCase(ODDSPREG); | |||
748 | #undef BCase | |||
749 | } | |||
750 | ||||
751 | void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO, | |||
752 | ELFYAML::FileHeader &FileHdr) { | |||
753 | IO.mapRequired("Class", FileHdr.Class); | |||
754 | IO.mapRequired("Data", FileHdr.Data); | |||
755 | IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0)); | |||
756 | IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0)); | |||
757 | IO.mapRequired("Type", FileHdr.Type); | |||
758 | IO.mapRequired("Machine", FileHdr.Machine); | |||
759 | IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0)); | |||
760 | IO.mapOptional("Entry", FileHdr.Entry, Hex64(0)); | |||
761 | } | |||
762 | ||||
763 | void MappingTraits<ELFYAML::ProgramHeader>::mapping( | |||
764 | IO &IO, ELFYAML::ProgramHeader &Phdr) { | |||
765 | IO.mapRequired("Type", Phdr.Type); | |||
766 | IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0)); | |||
767 | IO.mapOptional("Sections", Phdr.Sections); | |||
768 | IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0)); | |||
769 | IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0)); | |||
770 | IO.mapOptional("Align", Phdr.Align); | |||
771 | } | |||
772 | ||||
773 | namespace { | |||
774 | ||||
775 | struct NormalizedOther { | |||
776 | NormalizedOther(IO &) | |||
777 | : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {} | |||
778 | NormalizedOther(IO &, uint8_t Original) | |||
779 | : Visibility(Original & 0x3), Other(Original & ~0x3) {} | |||
780 | ||||
781 | uint8_t denormalize(IO &) { return Visibility | Other; } | |||
782 | ||||
783 | ELFYAML::ELF_STV Visibility; | |||
784 | ELFYAML::ELF_STO Other; | |||
785 | }; | |||
786 | ||||
787 | } // end anonymous namespace | |||
788 | ||||
789 | void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) { | |||
790 | IO.mapOptional("Name", Symbol.Name, StringRef()); | |||
791 | IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0)); | |||
792 | IO.mapOptional("Section", Symbol.Section, StringRef()); | |||
793 | IO.mapOptional("Index", Symbol.Index); | |||
794 | IO.mapOptional("Value", Symbol.Value, Hex64(0)); | |||
795 | IO.mapOptional("Size", Symbol.Size, Hex64(0)); | |||
796 | ||||
797 | MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other); | |||
798 | IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0)); | |||
799 | IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0)); | |||
800 | } | |||
801 | ||||
802 | StringRef MappingTraits<ELFYAML::Symbol>::validate(IO &IO, | |||
803 | ELFYAML::Symbol &Symbol) { | |||
804 | if (Symbol.Index && Symbol.Section.data()) { | |||
805 | return "Index and Section cannot both be specified for Symbol"; | |||
806 | } | |||
807 | if (Symbol.Index && *Symbol.Index == ELFYAML::ELF_SHN(ELF::SHN_XINDEX)) { | |||
808 | return "Large indexes are not supported"; | |||
809 | } | |||
810 | if (Symbol.Index && *Symbol.Index < ELFYAML::ELF_SHN(ELF::SHN_LORESERVE)) { | |||
811 | return "Use a section name to define which section a symbol is defined in"; | |||
812 | } | |||
813 | return StringRef(); | |||
814 | } | |||
815 | ||||
816 | void MappingTraits<ELFYAML::LocalGlobalWeakSymbols>::mapping( | |||
817 | IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols) { | |||
818 | IO.mapOptional("Local", Symbols.Local); | |||
819 | IO.mapOptional("Global", Symbols.Global); | |||
820 | IO.mapOptional("Weak", Symbols.Weak); | |||
821 | } | |||
822 | ||||
823 | static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) { | |||
824 | IO.mapOptional("Name", Section.Name, StringRef()); | |||
825 | IO.mapRequired("Type", Section.Type); | |||
826 | IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0)); | |||
827 | IO.mapOptional("Address", Section.Address, Hex64(0)); | |||
828 | IO.mapOptional("Link", Section.Link, StringRef()); | |||
829 | IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0)); | |||
830 | IO.mapOptional("EntSize", Section.EntSize); | |||
831 | IO.mapOptional("Info", Section.Info, StringRef()); | |||
832 | } | |||
833 | ||||
834 | static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { | |||
835 | commonSectionMapping(IO, Section); | |||
836 | IO.mapOptional("Content", Section.Content); | |||
837 | IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size())); | |||
838 | } | |||
839 | ||||
840 | static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) { | |||
841 | commonSectionMapping(IO, Section); | |||
842 | IO.mapOptional("Size", Section.Size, Hex64(0)); | |||
843 | } | |||
844 | ||||
845 | static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) { | |||
846 | commonSectionMapping(IO, Section); | |||
847 | IO.mapOptional("Relocations", Section.Relocations); | |||
848 | } | |||
849 | ||||
850 | static void groupSectionMapping(IO &IO, ELFYAML::Group &group) { | |||
851 | commonSectionMapping(IO, group); | |||
852 | IO.mapRequired("Members", group.Members); | |||
853 | } | |||
854 | ||||
855 | void MappingTraits<ELFYAML::SectionOrType>::mapping( | |||
856 | IO &IO, ELFYAML::SectionOrType §ionOrType) { | |||
857 | IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType); | |||
858 | } | |||
859 | ||||
860 | void MappingTraits<ELFYAML::SectionName>::mapping( | |||
861 | IO &IO, ELFYAML::SectionName §ionName) { | |||
862 | IO.mapRequired("Section", sectionName.Section); | |||
863 | } | |||
864 | ||||
865 | static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) { | |||
866 | commonSectionMapping(IO, Section); | |||
867 | IO.mapOptional("Version", Section.Version, Hex16(0)); | |||
868 | IO.mapRequired("ISA", Section.ISALevel); | |||
869 | IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0)); | |||
870 | IO.mapOptional("ISAExtension", Section.ISAExtension, | |||
871 | ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE)); | |||
872 | IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0)); | |||
873 | IO.mapOptional("FpABI", Section.FpABI, | |||
874 | ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY)); | |||
875 | IO.mapOptional("GPRSize", Section.GPRSize, | |||
876 | ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); | |||
877 | IO.mapOptional("CPR1Size", Section.CPR1Size, | |||
878 | ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); | |||
879 | IO.mapOptional("CPR2Size", Section.CPR2Size, | |||
880 | ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); | |||
881 | IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0)); | |||
882 | IO.mapOptional("Flags2", Section.Flags2, Hex32(0)); | |||
883 | } | |||
884 | ||||
885 | void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping( | |||
886 | IO &IO, std::unique_ptr<ELFYAML::Section> &Section) { | |||
887 | ELFYAML::ELF_SHT sectionType; | |||
| ||||
888 | if (IO.outputting()) | |||
889 | sectionType = Section->Type; | |||
890 | else | |||
891 | IO.mapRequired("Type", sectionType); | |||
892 | ||||
893 | switch (sectionType) { | |||
894 | case ELF::SHT_REL: | |||
895 | case ELF::SHT_RELA: | |||
896 | if (!IO.outputting()) | |||
897 | Section.reset(new ELFYAML::RelocationSection()); | |||
898 | sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get())); | |||
899 | break; | |||
900 | case ELF::SHT_GROUP: | |||
901 | if (!IO.outputting()) | |||
902 | Section.reset(new ELFYAML::Group()); | |||
903 | groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get())); | |||
904 | break; | |||
905 | case ELF::SHT_NOBITS: | |||
906 | if (!IO.outputting()) | |||
907 | Section.reset(new ELFYAML::NoBitsSection()); | |||
908 | sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get())); | |||
909 | break; | |||
910 | case ELF::SHT_MIPS_ABIFLAGS: | |||
911 | if (!IO.outputting()) | |||
912 | Section.reset(new ELFYAML::MipsABIFlags()); | |||
913 | sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get())); | |||
914 | break; | |||
915 | default: | |||
916 | if (!IO.outputting()) | |||
917 | Section.reset(new ELFYAML::RawContentSection()); | |||
918 | sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get())); | |||
919 | } | |||
920 | } | |||
921 | ||||
922 | StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate( | |||
923 | IO &io, std::unique_ptr<ELFYAML::Section> &Section) { | |||
924 | const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get()); | |||
925 | if (!RawSection || RawSection->Size >= RawSection->Content.binary_size()) | |||
926 | return StringRef(); | |||
927 | return "Section size must be greater or equal to the content size"; | |||
928 | } | |||
929 | ||||
930 | namespace { | |||
931 | ||||
932 | struct NormalizedMips64RelType { | |||
933 | NormalizedMips64RelType(IO &) | |||
934 | : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), | |||
935 | Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), | |||
936 | Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), | |||
937 | SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {} | |||
938 | NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original) | |||
939 | : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF), | |||
940 | Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {} | |||
941 | ||||
942 | ELFYAML::ELF_REL denormalize(IO &) { | |||
943 | ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24; | |||
944 | return Res; | |||
945 | } | |||
946 | ||||
947 | ELFYAML::ELF_REL Type; | |||
948 | ELFYAML::ELF_REL Type2; | |||
949 | ELFYAML::ELF_REL Type3; | |||
950 | ELFYAML::ELF_RSS SpecSym; | |||
951 | }; | |||
952 | ||||
953 | } // end anonymous namespace | |||
954 | ||||
955 | void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO, | |||
956 | ELFYAML::Relocation &Rel) { | |||
957 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
958 | assert(Object && "The IO context is not initialized")((Object && "The IO context is not initialized") ? static_cast <void> (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 958, __PRETTY_FUNCTION__)); | |||
959 | ||||
960 | IO.mapRequired("Offset", Rel.Offset); | |||
961 | IO.mapOptional("Symbol", Rel.Symbol); | |||
962 | ||||
963 | if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) && | |||
964 | Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { | |||
965 | MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key( | |||
966 | IO, Rel.Type); | |||
967 | IO.mapRequired("Type", Key->Type); | |||
968 | IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); | |||
969 | IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); | |||
970 | IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF)); | |||
971 | } else | |||
972 | IO.mapRequired("Type", Rel.Type); | |||
973 | ||||
974 | IO.mapOptional("Addend", Rel.Addend, (int64_t)0); | |||
975 | } | |||
976 | ||||
977 | void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) { | |||
978 | assert(!IO.getContext() && "The IO context is initialized already")((!IO.getContext() && "The IO context is initialized already" ) ? static_cast<void> (0) : __assert_fail ("!IO.getContext() && \"The IO context is initialized already\"" , "/build/llvm-toolchain-snapshot-8~svn350071/lib/ObjectYAML/ELFYAML.cpp" , 978, __PRETTY_FUNCTION__)); | |||
979 | IO.setContext(&Object); | |||
980 | IO.mapTag("!ELF", true); | |||
981 | IO.mapRequired("FileHeader", Object.Header); | |||
982 | IO.mapOptional("ProgramHeaders", Object.ProgramHeaders); | |||
983 | IO.mapOptional("Sections", Object.Sections); | |||
984 | IO.mapOptional("Symbols", Object.Symbols); | |||
985 | IO.mapOptional("DynamicSymbols", Object.DynamicSymbols); | |||
986 | IO.setContext(nullptr); | |||
987 | } | |||
988 | ||||
989 | LLVM_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; } ; | |||
990 | LLVM_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; } ; | |||
991 | LLVM_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; }; | |||
992 | LLVM_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; }; | |||
993 | LLVM_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; }; | |||
994 | ||||
995 | } // end namespace yaml | |||
996 | ||||
997 | } // end namespace llvm |
1 | //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | /// | |||
10 | /// \file | |||
11 | /// This file declares classes for handling the YAML representation | |||
12 | /// of ELF. | |||
13 | /// | |||
14 | //===----------------------------------------------------------------------===// | |||
15 | ||||
16 | #ifndef LLVM_OBJECTYAML_ELFYAML_H | |||
17 | #define LLVM_OBJECTYAML_ELFYAML_H | |||
18 | ||||
19 | #include "llvm/ADT/StringRef.h" | |||
20 | #include "llvm/ObjectYAML/YAML.h" | |||
21 | #include "llvm/Support/YAMLTraits.h" | |||
22 | #include <cstdint> | |||
23 | #include <memory> | |||
24 | #include <vector> | |||
25 | ||||
26 | namespace llvm { | |||
27 | namespace ELFYAML { | |||
28 | ||||
29 | // These types are invariant across 32/64-bit ELF, so for simplicity just | |||
30 | // directly give them their exact sizes. We don't need to worry about | |||
31 | // endianness because these are just the types in the YAMLIO structures, | |||
32 | // and are appropriately converted to the necessary endianness when | |||
33 | // reading/generating binary object files. | |||
34 | // The naming of these types is intended to be ELF_PREFIX, where PREFIX is | |||
35 | // the common prefix of the respective constants. E.g. ELF_EM corresponds | |||
36 | // to the `e_machine` constants, like `EM_X86_64`. | |||
37 | // In the future, these would probably be better suited by C++11 enum | |||
38 | // class's with appropriate fixed underlying type. | |||
39 | LLVM_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 ; }; | |||
40 | LLVM_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 ; }; | |||
41 | LLVM_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 ; }; | |||
42 | LLVM_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; } ; | |||
43 | LLVM_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; } ; | |||
44 | LLVM_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; } ; | |||
45 | // Just use 64, since it can hold 32-bit values too. | |||
46 | LLVM_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 ; }; | |||
47 | LLVM_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 ; }; | |||
48 | LLVM_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; }; | |||
| ||||
49 | LLVM_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; }; | |||
50 | LLVM_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; }; | |||
51 | // Just use 64, since it can hold 32-bit values too. | |||
52 | LLVM_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; }; | |||
53 | LLVM_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; }; | |||
54 | LLVM_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; }; | |||
55 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)struct ELF_STV { ELF_STV() = default; ELF_STV(const uint8_t v ) : value(v) {} ELF_STV(const ELF_STV &v) = default; ELF_STV &operator=(const ELF_STV &rhs) = default; ELF_STV & operator=(const uint8_t &rhs) { value = rhs; return *this ; } operator const uint8_t & () const { return value; } bool operator==(const ELF_STV &rhs) const { return value == rhs .value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_STV &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; }; | |||
56 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)struct ELF_STO { ELF_STO() = default; ELF_STO(const uint8_t v ) : value(v) {} ELF_STO(const ELF_STO &v) = default; ELF_STO &operator=(const ELF_STO &rhs) = default; ELF_STO & operator=(const uint8_t &rhs) { value = rhs; return *this ; } operator const uint8_t & () const { return value; } bool operator==(const ELF_STO &rhs) const { return value == rhs .value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_STO &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; }; | |||
57 | ||||
58 | LLVM_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; } ; | |||
59 | LLVM_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; } ; | |||
60 | LLVM_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; }; | |||
61 | LLVM_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; }; | |||
62 | LLVM_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; }; | |||
63 | LLVM_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; }; | |||
64 | ||||
65 | // For now, hardcode 64 bits everywhere that 32 or 64 would be needed | |||
66 | // since 64-bit can hold 32-bit values too. | |||
67 | struct FileHeader { | |||
68 | ELF_ELFCLASS Class; | |||
69 | ELF_ELFDATA Data; | |||
70 | ELF_ELFOSABI OSABI; | |||
71 | llvm::yaml::Hex8 ABIVersion; | |||
72 | ELF_ET Type; | |||
73 | ELF_EM Machine; | |||
74 | ELF_EF Flags; | |||
75 | llvm::yaml::Hex64 Entry; | |||
76 | }; | |||
77 | ||||
78 | struct SectionName { | |||
79 | StringRef Section; | |||
80 | }; | |||
81 | ||||
82 | struct ProgramHeader { | |||
83 | ELF_PT Type; | |||
84 | ELF_PF Flags; | |||
85 | llvm::yaml::Hex64 VAddr; | |||
86 | llvm::yaml::Hex64 PAddr; | |||
87 | Optional<llvm::yaml::Hex64> Align; | |||
88 | std::vector<SectionName> Sections; | |||
89 | }; | |||
90 | ||||
91 | struct Symbol { | |||
92 | StringRef Name; | |||
93 | ELF_STT Type; | |||
94 | StringRef Section; | |||
95 | Optional<ELF_SHN> Index; | |||
96 | llvm::yaml::Hex64 Value; | |||
97 | llvm::yaml::Hex64 Size; | |||
98 | uint8_t Other; | |||
99 | }; | |||
100 | ||||
101 | struct LocalGlobalWeakSymbols { | |||
102 | std::vector<Symbol> Local; | |||
103 | std::vector<Symbol> Global; | |||
104 | std::vector<Symbol> Weak; | |||
105 | }; | |||
106 | ||||
107 | struct SectionOrType { | |||
108 | StringRef sectionNameOrType; | |||
109 | }; | |||
110 | ||||
111 | struct Section { | |||
112 | enum class SectionKind { | |||
113 | Group, | |||
114 | RawContent, | |||
115 | Relocation, | |||
116 | NoBits, | |||
117 | MipsABIFlags | |||
118 | }; | |||
119 | SectionKind Kind; | |||
120 | StringRef Name; | |||
121 | ELF_SHT Type; | |||
122 | ELF_SHF Flags; | |||
123 | llvm::yaml::Hex64 Address; | |||
124 | StringRef Link; | |||
125 | StringRef Info; | |||
126 | llvm::yaml::Hex64 AddressAlign; | |||
127 | Optional<llvm::yaml::Hex64> EntSize; | |||
128 | ||||
129 | Section(SectionKind Kind) : Kind(Kind) {} | |||
130 | virtual ~Section(); | |||
131 | }; | |||
132 | struct RawContentSection : Section { | |||
133 | yaml::BinaryRef Content; | |||
134 | llvm::yaml::Hex64 Size; | |||
135 | ||||
136 | RawContentSection() : Section(SectionKind::RawContent) {} | |||
137 | ||||
138 | static bool classof(const Section *S) { | |||
139 | return S->Kind == SectionKind::RawContent; | |||
140 | } | |||
141 | }; | |||
142 | ||||
143 | struct NoBitsSection : Section { | |||
144 | llvm::yaml::Hex64 Size; | |||
145 | ||||
146 | NoBitsSection() : Section(SectionKind::NoBits) {} | |||
147 | ||||
148 | static bool classof(const Section *S) { | |||
149 | return S->Kind == SectionKind::NoBits; | |||
150 | } | |||
151 | }; | |||
152 | ||||
153 | struct Group : Section { | |||
154 | // Members of a group contain a flag and a list of section indices | |||
155 | // that are part of the group. | |||
156 | std::vector<SectionOrType> Members; | |||
157 | ||||
158 | Group() : Section(SectionKind::Group) {} | |||
159 | ||||
160 | static bool classof(const Section *S) { | |||
161 | return S->Kind == SectionKind::Group; | |||
162 | } | |||
163 | }; | |||
164 | ||||
165 | struct Relocation { | |||
166 | llvm::yaml::Hex64 Offset; | |||
167 | int64_t Addend; | |||
168 | ELF_REL Type; | |||
169 | Optional<StringRef> Symbol; | |||
170 | }; | |||
171 | ||||
172 | struct RelocationSection : Section { | |||
173 | std::vector<Relocation> Relocations; | |||
174 | ||||
175 | RelocationSection() : Section(SectionKind::Relocation) {} | |||
176 | ||||
177 | static bool classof(const Section *S) { | |||
178 | return S->Kind == SectionKind::Relocation; | |||
179 | } | |||
180 | }; | |||
181 | ||||
182 | // Represents .MIPS.abiflags section | |||
183 | struct MipsABIFlags : Section { | |||
184 | llvm::yaml::Hex16 Version; | |||
185 | MIPS_ISA ISALevel; | |||
186 | llvm::yaml::Hex8 ISARevision; | |||
187 | MIPS_AFL_REG GPRSize; | |||
188 | MIPS_AFL_REG CPR1Size; | |||
189 | MIPS_AFL_REG CPR2Size; | |||
190 | MIPS_ABI_FP FpABI; | |||
191 | MIPS_AFL_EXT ISAExtension; | |||
192 | MIPS_AFL_ASE ASEs; | |||
193 | MIPS_AFL_FLAGS1 Flags1; | |||
194 | llvm::yaml::Hex32 Flags2; | |||
195 | ||||
196 | MipsABIFlags() : Section(SectionKind::MipsABIFlags) {} | |||
197 | ||||
198 | static bool classof(const Section *S) { | |||
199 | return S->Kind == SectionKind::MipsABIFlags; | |||
200 | } | |||
201 | }; | |||
202 | ||||
203 | struct Object { | |||
204 | FileHeader Header; | |||
205 | std::vector<ProgramHeader> ProgramHeaders; | |||
206 | std::vector<std::unique_ptr<Section>> Sections; | |||
207 | // Although in reality the symbols reside in a section, it is a lot | |||
208 | // cleaner and nicer if we read them from the YAML as a separate | |||
209 | // top-level key, which automatically ensures that invariants like there | |||
210 | // being a single SHT_SYMTAB section are upheld. | |||
211 | LocalGlobalWeakSymbols Symbols; | |||
212 | LocalGlobalWeakSymbols DynamicSymbols; | |||
213 | }; | |||
214 | ||||
215 | } // end namespace ELFYAML | |||
216 | } // end namespace llvm | |||
217 | ||||
218 | LLVM_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; }; } } | |||
219 | LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <std::unique_ptr<llvm::ELFYAML::Section> >::value && !std::is_same<std::unique_ptr<llvm::ELFYAML ::Section>, std::string>::value && !std::is_same <std::unique_ptr<llvm::ELFYAML::Section>, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<std::unique_ptr <llvm::ELFYAML::Section> > { static const bool flow = false; }; } } | |||
220 | LLVM_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; }; } } | |||
221 | LLVM_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; }; } } | |||
222 | LLVM_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; }; } } | |||
223 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionName)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::SectionName>::value && !std::is_same <llvm::ELFYAML::SectionName, std::string>::value && !std::is_same<llvm::ELFYAML::SectionName, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::SectionName> { static const bool flow = false; }; } } | |||
224 | ||||
225 | namespace llvm { | |||
226 | namespace yaml { | |||
227 | ||||
228 | template <> | |||
229 | struct ScalarEnumerationTraits<ELFYAML::ELF_ET> { | |||
230 | static void enumeration(IO &IO, ELFYAML::ELF_ET &Value); | |||
231 | }; | |||
232 | ||||
233 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> { | |||
234 | static void enumeration(IO &IO, ELFYAML::ELF_PT &Value); | |||
235 | }; | |||
236 | ||||
237 | template <> | |||
238 | struct ScalarEnumerationTraits<ELFYAML::ELF_EM> { | |||
239 | static void enumeration(IO &IO, ELFYAML::ELF_EM &Value); | |||
240 | }; | |||
241 | ||||
242 | template <> | |||
243 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> { | |||
244 | static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value); | |||
245 | }; | |||
246 | ||||
247 | template <> | |||
248 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> { | |||
249 | static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value); | |||
250 | }; | |||
251 | ||||
252 | template <> | |||
253 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> { | |||
254 | static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value); | |||
255 | }; | |||
256 | ||||
257 | template <> | |||
258 | struct ScalarBitSetTraits<ELFYAML::ELF_EF> { | |||
259 | static void bitset(IO &IO, ELFYAML::ELF_EF &Value); | |||
260 | }; | |||
261 | ||||
262 | template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> { | |||
263 | static void bitset(IO &IO, ELFYAML::ELF_PF &Value); | |||
264 | }; | |||
265 | ||||
266 | template <> | |||
267 | struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> { | |||
268 | static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value); | |||
269 | }; | |||
270 | ||||
271 | template <> | |||
272 | struct ScalarBitSetTraits<ELFYAML::ELF_SHF> { | |||
273 | static void bitset(IO &IO, ELFYAML::ELF_SHF &Value); | |||
274 | }; | |||
275 | ||||
276 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> { | |||
277 | static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value); | |||
278 | }; | |||
279 | ||||
280 | template <> | |||
281 | struct ScalarEnumerationTraits<ELFYAML::ELF_STT> { | |||
282 | static void enumeration(IO &IO, ELFYAML::ELF_STT &Value); | |||
283 | }; | |||
284 | ||||
285 | template <> | |||
286 | struct ScalarEnumerationTraits<ELFYAML::ELF_STV> { | |||
287 | static void enumeration(IO &IO, ELFYAML::ELF_STV &Value); | |||
288 | }; | |||
289 | ||||
290 | template <> | |||
291 | struct ScalarBitSetTraits<ELFYAML::ELF_STO> { | |||
292 | static void bitset(IO &IO, ELFYAML::ELF_STO &Value); | |||
293 | }; | |||
294 | ||||
295 | template <> | |||
296 | struct ScalarEnumerationTraits<ELFYAML::ELF_REL> { | |||
297 | static void enumeration(IO &IO, ELFYAML::ELF_REL &Value); | |||
298 | }; | |||
299 | ||||
300 | template <> | |||
301 | struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> { | |||
302 | static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value); | |||
303 | }; | |||
304 | ||||
305 | template <> | |||
306 | struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> { | |||
307 | static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value); | |||
308 | }; | |||
309 | ||||
310 | template <> | |||
311 | struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> { | |||
312 | static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value); | |||
313 | }; | |||
314 | ||||
315 | template <> | |||
316 | struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> { | |||
317 | static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value); | |||
318 | }; | |||
319 | ||||
320 | template <> | |||
321 | struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> { | |||
322 | static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value); | |||
323 | }; | |||
324 | ||||
325 | template <> | |||
326 | struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> { | |||
327 | static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value); | |||
328 | }; | |||
329 | ||||
330 | template <> | |||
331 | struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> { | |||
332 | static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value); | |||
333 | }; | |||
334 | ||||
335 | template <> | |||
336 | struct MappingTraits<ELFYAML::FileHeader> { | |||
337 | static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr); | |||
338 | }; | |||
339 | ||||
340 | template <> struct MappingTraits<ELFYAML::ProgramHeader> { | |||
341 | static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr); | |||
342 | }; | |||
343 | ||||
344 | template <> | |||
345 | struct MappingTraits<ELFYAML::Symbol> { | |||
346 | static void mapping(IO &IO, ELFYAML::Symbol &Symbol); | |||
347 | static StringRef validate(IO &IO, ELFYAML::Symbol &Symbol); | |||
348 | }; | |||
349 | ||||
350 | template <> | |||
351 | struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> { | |||
352 | static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols); | |||
353 | }; | |||
354 | ||||
355 | template <> struct MappingTraits<ELFYAML::Relocation> { | |||
356 | static void mapping(IO &IO, ELFYAML::Relocation &Rel); | |||
357 | }; | |||
358 | ||||
359 | template <> | |||
360 | struct MappingTraits<std::unique_ptr<ELFYAML::Section>> { | |||
361 | static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section); | |||
362 | static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section); | |||
363 | }; | |||
364 | ||||
365 | template <> | |||
366 | struct MappingTraits<ELFYAML::Object> { | |||
367 | static void mapping(IO &IO, ELFYAML::Object &Object); | |||
368 | }; | |||
369 | ||||
370 | template <> struct MappingTraits<ELFYAML::SectionOrType> { | |||
371 | static void mapping(IO &IO, ELFYAML::SectionOrType §ionOrType); | |||
372 | }; | |||
373 | ||||
374 | template <> struct MappingTraits<ELFYAML::SectionName> { | |||
375 | static void mapping(IO &IO, ELFYAML::SectionName §ionName); | |||
376 | }; | |||
377 | ||||
378 | } // end namespace yaml | |||
379 | } // end namespace llvm | |||
380 | ||||
381 | #endif // LLVM_OBJECTYAML_ELFYAML_H |
1 | //===- llvm/Support/YAMLTraits.h --------------------------------*- C++ -*-===// |
2 | // |
3 | // The LLVM Linker |
4 | // |
5 | // This file is distributed under the University of Illinois Open Source |
6 | // License. See LICENSE.TXT for details. |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #ifndef LLVM_SUPPORT_YAMLTRAITS_H |
11 | #define LLVM_SUPPORT_YAMLTRAITS_H |
12 | |
13 | #include "llvm/ADT/Optional.h" |
14 | #include "llvm/ADT/SmallVector.h" |
15 | #include "llvm/ADT/StringExtras.h" |
16 | #include "llvm/ADT/StringMap.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "llvm/ADT/Twine.h" |
19 | #include "llvm/Support/AlignOf.h" |
20 | #include "llvm/Support/Allocator.h" |
21 | #include "llvm/Support/Endian.h" |
22 | #include "llvm/Support/Regex.h" |
23 | #include "llvm/Support/SourceMgr.h" |
24 | #include "llvm/Support/YAMLParser.h" |
25 | #include "llvm/Support/raw_ostream.h" |
26 | #include <cassert> |
27 | #include <cctype> |
28 | #include <cstddef> |
29 | #include <cstdint> |
30 | #include <iterator> |
31 | #include <map> |
32 | #include <memory> |
33 | #include <new> |
34 | #include <string> |
35 | #include <system_error> |
36 | #include <type_traits> |
37 | #include <vector> |
38 | |
39 | namespace llvm { |
40 | namespace yaml { |
41 | |
42 | enum class NodeKind : uint8_t { |
43 | Scalar, |
44 | Map, |
45 | Sequence, |
46 | }; |
47 | |
48 | struct EmptyContext {}; |
49 | |
50 | /// This class should be specialized by any type that needs to be converted |
51 | /// to/from a YAML mapping. For example: |
52 | /// |
53 | /// struct MappingTraits<MyStruct> { |
54 | /// static void mapping(IO &io, MyStruct &s) { |
55 | /// io.mapRequired("name", s.name); |
56 | /// io.mapRequired("size", s.size); |
57 | /// io.mapOptional("age", s.age); |
58 | /// } |
59 | /// }; |
60 | template<class T> |
61 | struct MappingTraits { |
62 | // Must provide: |
63 | // static void mapping(IO &io, T &fields); |
64 | // Optionally may provide: |
65 | // static StringRef validate(IO &io, T &fields); |
66 | // |
67 | // The optional flow flag will cause generated YAML to use a flow mapping |
68 | // (e.g. { a: 0, b: 1 }): |
69 | // static const bool flow = true; |
70 | }; |
71 | |
72 | /// This class is similar to MappingTraits<T> but allows you to pass in |
73 | /// additional context for each map operation. For example: |
74 | /// |
75 | /// struct MappingContextTraits<MyStruct, MyContext> { |
76 | /// static void mapping(IO &io, MyStruct &s, MyContext &c) { |
77 | /// io.mapRequired("name", s.name); |
78 | /// io.mapRequired("size", s.size); |
79 | /// io.mapOptional("age", s.age); |
80 | /// ++c.TimesMapped; |
81 | /// } |
82 | /// }; |
83 | template <class T, class Context> struct MappingContextTraits { |
84 | // Must provide: |
85 | // static void mapping(IO &io, T &fields, Context &Ctx); |
86 | // Optionally may provide: |
87 | // static StringRef validate(IO &io, T &fields, Context &Ctx); |
88 | // |
89 | // The optional flow flag will cause generated YAML to use a flow mapping |
90 | // (e.g. { a: 0, b: 1 }): |
91 | // static const bool flow = true; |
92 | }; |
93 | |
94 | /// This class should be specialized by any integral type that converts |
95 | /// to/from a YAML scalar where there is a one-to-one mapping between |
96 | /// in-memory values and a string in YAML. For example: |
97 | /// |
98 | /// struct ScalarEnumerationTraits<Colors> { |
99 | /// static void enumeration(IO &io, Colors &value) { |
100 | /// io.enumCase(value, "red", cRed); |
101 | /// io.enumCase(value, "blue", cBlue); |
102 | /// io.enumCase(value, "green", cGreen); |
103 | /// } |
104 | /// }; |
105 | template<typename T> |
106 | 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 | /// }; |
122 | template<typename T> |
123 | struct ScalarBitSetTraits { |
124 | // Must provide: |
125 | // static void bitset(IO &io, T &value); |
126 | }; |
127 | |
128 | /// Describe which type of quotes should be used when quoting is necessary. |
129 | /// Some non-printable characters need to be double-quoted, while some others |
130 | /// are fine with simple-quoting, and some don't need any quoting. |
131 | enum class QuotingType { None, Single, Double }; |
132 | |
133 | /// This class should be specialized by type that requires custom conversion |
134 | /// to/from a yaml scalar. For example: |
135 | /// |
136 | /// template<> |
137 | /// struct ScalarTraits<MyType> { |
138 | /// static void output(const MyType &val, void*, llvm::raw_ostream &out) { |
139 | /// // stream out custom formatting |
140 | /// out << llvm::format("%x", val); |
141 | /// } |
142 | /// static StringRef input(StringRef scalar, void*, MyType &value) { |
143 | /// // parse scalar and set `value` |
144 | /// // return empty string on success, or error string |
145 | /// return StringRef(); |
146 | /// } |
147 | /// static QuotingType mustQuote(StringRef) { return QuotingType::Single; } |
148 | /// }; |
149 | template<typename T> |
150 | struct ScalarTraits { |
151 | // Must provide: |
152 | // |
153 | // Function to write the value as a string: |
154 | // static void output(const T &value, void *ctxt, llvm::raw_ostream &out); |
155 | // |
156 | // Function to convert a string to a value. Returns the empty |
157 | // StringRef on success or an error string if string is malformed: |
158 | // static StringRef input(StringRef scalar, void *ctxt, T &value); |
159 | // |
160 | // Function to determine if the value should be quoted. |
161 | // static QuotingType mustQuote(StringRef); |
162 | }; |
163 | |
164 | /// This class should be specialized by type that requires custom conversion |
165 | /// to/from a YAML literal block scalar. For example: |
166 | /// |
167 | /// template <> |
168 | /// struct BlockScalarTraits<MyType> { |
169 | /// static void output(const MyType &Value, void*, llvm::raw_ostream &Out) |
170 | /// { |
171 | /// // stream out custom formatting |
172 | /// Out << Value; |
173 | /// } |
174 | /// static StringRef input(StringRef Scalar, void*, MyType &Value) { |
175 | /// // parse scalar and set `value` |
176 | /// // return empty string on success, or error string |
177 | /// return StringRef(); |
178 | /// } |
179 | /// }; |
180 | template <typename T> |
181 | struct BlockScalarTraits { |
182 | // Must provide: |
183 | // |
184 | // Function to write the value as a string: |
185 | // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out); |
186 | // |
187 | // Function to convert a string to a value. Returns the empty |
188 | // StringRef on success or an error string if string is malformed: |
189 | // static StringRef input(StringRef Scalar, void *ctxt, T &Value); |
190 | // |
191 | // Optional: |
192 | // static StringRef inputTag(T &Val, std::string Tag) |
193 | // static void outputTag(const T &Val, raw_ostream &Out) |
194 | }; |
195 | |
196 | /// This class should be specialized by type that requires custom conversion |
197 | /// to/from a YAML scalar with optional tags. For example: |
198 | /// |
199 | /// template <> |
200 | /// struct TaggedScalarTraits<MyType> { |
201 | /// static void output(const MyType &Value, void*, llvm::raw_ostream |
202 | /// &ScalarOut, llvm::raw_ostream &TagOut) |
203 | /// { |
204 | /// // stream out custom formatting including optional Tag |
205 | /// Out << Value; |
206 | /// } |
207 | /// static StringRef input(StringRef Scalar, StringRef Tag, void*, MyType |
208 | /// &Value) { |
209 | /// // parse scalar and set `value` |
210 | /// // return empty string on success, or error string |
211 | /// return StringRef(); |
212 | /// } |
213 | /// static QuotingType mustQuote(const MyType &Value, StringRef) { |
214 | /// return QuotingType::Single; |
215 | /// } |
216 | /// }; |
217 | template <typename T> struct TaggedScalarTraits { |
218 | // Must provide: |
219 | // |
220 | // Function to write the value and tag as strings: |
221 | // static void output(const T &Value, void *ctx, llvm::raw_ostream &ScalarOut, |
222 | // llvm::raw_ostream &TagOut); |
223 | // |
224 | // Function to convert a string to a value. Returns the empty |
225 | // StringRef on success or an error string if string is malformed: |
226 | // static StringRef input(StringRef Scalar, StringRef Tag, void *ctxt, T |
227 | // &Value); |
228 | // |
229 | // Function to determine if the value should be quoted. |
230 | // static QuotingType mustQuote(const T &Value, StringRef Scalar); |
231 | }; |
232 | |
233 | /// This class should be specialized by any type that needs to be converted |
234 | /// to/from a YAML sequence. For example: |
235 | /// |
236 | /// template<> |
237 | /// struct SequenceTraits<MyContainer> { |
238 | /// static size_t size(IO &io, MyContainer &seq) { |
239 | /// return seq.size(); |
240 | /// } |
241 | /// static MyType& element(IO &, MyContainer &seq, size_t index) { |
242 | /// if ( index >= seq.size() ) |
243 | /// seq.resize(index+1); |
244 | /// return seq[index]; |
245 | /// } |
246 | /// }; |
247 | template<typename T, typename EnableIf = void> |
248 | struct SequenceTraits { |
249 | // Must provide: |
250 | // static size_t size(IO &io, T &seq); |
251 | // static T::value_type& element(IO &io, T &seq, size_t index); |
252 | // |
253 | // The following is option and will cause generated YAML to use |
254 | // a flow sequence (e.g. [a,b,c]). |
255 | // static const bool flow = true; |
256 | }; |
257 | |
258 | /// This class should be specialized by any type for which vectors of that |
259 | /// type need to be converted to/from a YAML sequence. |
260 | template<typename T, typename EnableIf = void> |
261 | struct SequenceElementTraits { |
262 | // Must provide: |
263 | // static const bool flow; |
264 | }; |
265 | |
266 | /// This class should be specialized by any type that needs to be converted |
267 | /// to/from a list of YAML documents. |
268 | template<typename T> |
269 | struct DocumentListTraits { |
270 | // Must provide: |
271 | // static size_t size(IO &io, T &seq); |
272 | // static T::value_type& element(IO &io, T &seq, size_t index); |
273 | }; |
274 | |
275 | /// This class should be specialized by any type that needs to be converted |
276 | /// to/from a YAML mapping in the case where the names of the keys are not known |
277 | /// in advance, e.g. a string map. |
278 | template <typename T> |
279 | struct CustomMappingTraits { |
280 | // static void inputOne(IO &io, StringRef key, T &elem); |
281 | // static void output(IO &io, T &elem); |
282 | }; |
283 | |
284 | /// This class should be specialized by any type that can be represented as |
285 | /// a scalar, map, or sequence, decided dynamically. For example: |
286 | /// |
287 | /// typedef std::unique_ptr<MyBase> MyPoly; |
288 | /// |
289 | /// template<> |
290 | /// struct PolymorphicTraits<MyPoly> { |
291 | /// static NodeKind getKind(const MyPoly &poly) { |
292 | /// return poly->getKind(); |
293 | /// } |
294 | /// static MyScalar& getAsScalar(MyPoly &poly) { |
295 | /// if (!poly || !isa<MyScalar>(poly)) |
296 | /// poly.reset(new MyScalar()); |
297 | /// return *cast<MyScalar>(poly.get()); |
298 | /// } |
299 | /// // ... |
300 | /// }; |
301 | template <typename T> struct PolymorphicTraits { |
302 | // Must provide: |
303 | // static NodeKind getKind(const T &poly); |
304 | // static scalar_type &getAsScalar(T &poly); |
305 | // static map_type &getAsMap(T &poly); |
306 | // static sequence_type &getAsSequence(T &poly); |
307 | }; |
308 | |
309 | // Only used for better diagnostics of missing traits |
310 | template <typename T> |
311 | struct MissingTrait; |
312 | |
313 | // Test if ScalarEnumerationTraits<T> is defined on type T. |
314 | template <class T> |
315 | struct has_ScalarEnumerationTraits |
316 | { |
317 | using Signature_enumeration = void (*)(class IO&, T&); |
318 | |
319 | template <typename U> |
320 | static char test(SameType<Signature_enumeration, &U::enumeration>*); |
321 | |
322 | template <typename U> |
323 | static double test(...); |
324 | |
325 | static bool const value = |
326 | (sizeof(test<ScalarEnumerationTraits<T>>(nullptr)) == 1); |
327 | }; |
328 | |
329 | // Test if ScalarBitSetTraits<T> is defined on type T. |
330 | template <class T> |
331 | struct has_ScalarBitSetTraits |
332 | { |
333 | using Signature_bitset = void (*)(class IO&, T&); |
334 | |
335 | template <typename U> |
336 | static char test(SameType<Signature_bitset, &U::bitset>*); |
337 | |
338 | template <typename U> |
339 | static double test(...); |
340 | |
341 | static bool const value = (sizeof(test<ScalarBitSetTraits<T>>(nullptr)) == 1); |
342 | }; |
343 | |
344 | // Test if ScalarTraits<T> is defined on type T. |
345 | template <class T> |
346 | struct has_ScalarTraits |
347 | { |
348 | using Signature_input = StringRef (*)(StringRef, void*, T&); |
349 | using Signature_output = void (*)(const T&, void*, raw_ostream&); |
350 | using Signature_mustQuote = QuotingType (*)(StringRef); |
351 | |
352 | template <typename U> |
353 | static char test(SameType<Signature_input, &U::input> *, |
354 | SameType<Signature_output, &U::output> *, |
355 | SameType<Signature_mustQuote, &U::mustQuote> *); |
356 | |
357 | template <typename U> |
358 | static double test(...); |
359 | |
360 | static bool const value = |
361 | (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1); |
362 | }; |
363 | |
364 | // Test if BlockScalarTraits<T> is defined on type T. |
365 | template <class T> |
366 | struct has_BlockScalarTraits |
367 | { |
368 | using Signature_input = StringRef (*)(StringRef, void *, T &); |
369 | using Signature_output = void (*)(const T &, void *, raw_ostream &); |
370 | |
371 | template <typename U> |
372 | static char test(SameType<Signature_input, &U::input> *, |
373 | SameType<Signature_output, &U::output> *); |
374 | |
375 | template <typename U> |
376 | static double test(...); |
377 | |
378 | static bool const value = |
379 | (sizeof(test<BlockScalarTraits<T>>(nullptr, nullptr)) == 1); |
380 | }; |
381 | |
382 | // Test if TaggedScalarTraits<T> is defined on type T. |
383 | template <class T> struct has_TaggedScalarTraits { |
384 | using Signature_input = StringRef (*)(StringRef, StringRef, void *, T &); |
385 | using Signature_output = void (*)(const T &, void *, raw_ostream &, |
386 | raw_ostream &); |
387 | using Signature_mustQuote = QuotingType (*)(const T &, StringRef); |
388 | |
389 | template <typename U> |
390 | static char test(SameType<Signature_input, &U::input> *, |
391 | SameType<Signature_output, &U::output> *, |
392 | SameType<Signature_mustQuote, &U::mustQuote> *); |
393 | |
394 | template <typename U> static double test(...); |
395 | |
396 | static bool const value = |
397 | (sizeof(test<TaggedScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1); |
398 | }; |
399 | |
400 | // Test if MappingContextTraits<T> is defined on type T. |
401 | template <class T, class Context> struct has_MappingTraits { |
402 | using Signature_mapping = void (*)(class IO &, T &, Context &); |
403 | |
404 | template <typename U> |
405 | static char test(SameType<Signature_mapping, &U::mapping>*); |
406 | |
407 | template <typename U> |
408 | static double test(...); |
409 | |
410 | static bool const value = |
411 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); |
412 | }; |
413 | |
414 | // Test if MappingTraits<T> is defined on type T. |
415 | template <class T> struct has_MappingTraits<T, EmptyContext> { |
416 | using Signature_mapping = void (*)(class IO &, T &); |
417 | |
418 | template <typename U> |
419 | static char test(SameType<Signature_mapping, &U::mapping> *); |
420 | |
421 | template <typename U> static double test(...); |
422 | |
423 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); |
424 | }; |
425 | |
426 | // Test if MappingContextTraits<T>::validate() is defined on type T. |
427 | template <class T, class Context> struct has_MappingValidateTraits { |
428 | using Signature_validate = StringRef (*)(class IO &, T &, Context &); |
429 | |
430 | template <typename U> |
431 | static char test(SameType<Signature_validate, &U::validate>*); |
432 | |
433 | template <typename U> |
434 | static double test(...); |
435 | |
436 | static bool const value = |
437 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); |
438 | }; |
439 | |
440 | // Test if MappingTraits<T>::validate() is defined on type T. |
441 | template <class T> struct has_MappingValidateTraits<T, EmptyContext> { |
442 | using Signature_validate = StringRef (*)(class IO &, T &); |
443 | |
444 | template <typename U> |
445 | static char test(SameType<Signature_validate, &U::validate> *); |
446 | |
447 | template <typename U> static double test(...); |
448 | |
449 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); |
450 | }; |
451 | |
452 | // Test if SequenceTraits<T> is defined on type T. |
453 | template <class T> |
454 | struct has_SequenceMethodTraits |
455 | { |
456 | using Signature_size = size_t (*)(class IO&, T&); |
457 | |
458 | template <typename U> |
459 | static char test(SameType<Signature_size, &U::size>*); |
460 | |
461 | template <typename U> |
462 | static double test(...); |
463 | |
464 | static bool const value = (sizeof(test<SequenceTraits<T>>(nullptr)) == 1); |
465 | }; |
466 | |
467 | // Test if CustomMappingTraits<T> is defined on type T. |
468 | template <class T> |
469 | struct has_CustomMappingTraits |
470 | { |
471 | using Signature_input = void (*)(IO &io, StringRef key, T &v); |
472 | |
473 | template <typename U> |
474 | static char test(SameType<Signature_input, &U::inputOne>*); |
475 | |
476 | template <typename U> |
477 | static double test(...); |
478 | |
479 | static bool const value = |
480 | (sizeof(test<CustomMappingTraits<T>>(nullptr)) == 1); |
481 | }; |
482 | |
483 | // has_FlowTraits<int> will cause an error with some compilers because |
484 | // it subclasses int. Using this wrapper only instantiates the |
485 | // real has_FlowTraits only if the template type is a class. |
486 | template <typename T, bool Enabled = std::is_class<T>::value> |
487 | class has_FlowTraits |
488 | { |
489 | public: |
490 | static const bool value = false; |
491 | }; |
492 | |
493 | // Some older gcc compilers don't support straight forward tests |
494 | // for members, so test for ambiguity cause by the base and derived |
495 | // classes both defining the member. |
496 | template <class T> |
497 | struct has_FlowTraits<T, true> |
498 | { |
499 | struct Fallback { bool flow; }; |
500 | struct Derived : T, Fallback { }; |
501 | |
502 | template<typename C> |
503 | static char (&f(SameType<bool Fallback::*, &C::flow>*))[1]; |
504 | |
505 | template<typename C> |
506 | static char (&f(...))[2]; |
507 | |
508 | static bool const value = sizeof(f<Derived>(nullptr)) == 2; |
509 | }; |
510 | |
511 | // Test if SequenceTraits<T> is defined on type T |
512 | template<typename T> |
513 | struct has_SequenceTraits : public std::integral_constant<bool, |
514 | has_SequenceMethodTraits<T>::value > { }; |
515 | |
516 | // Test if DocumentListTraits<T> is defined on type T |
517 | template <class T> |
518 | struct has_DocumentListTraits |
519 | { |
520 | using Signature_size = size_t (*)(class IO &, T &); |
521 | |
522 | template <typename U> |
523 | static char test(SameType<Signature_size, &U::size>*); |
524 | |
525 | template <typename U> |
526 | static double test(...); |
527 | |
528 | static bool const value = (sizeof(test<DocumentListTraits<T>>(nullptr))==1); |
529 | }; |
530 | |
531 | template <class T> struct has_PolymorphicTraits { |
532 | using Signature_getKind = NodeKind (*)(const T &); |
533 | |
534 | template <typename U> |
535 | static char test(SameType<Signature_getKind, &U::getKind> *); |
536 | |
537 | template <typename U> static double test(...); |
538 | |
539 | static bool const value = (sizeof(test<PolymorphicTraits<T>>(nullptr)) == 1); |
540 | }; |
541 | |
542 | inline bool isNumeric(StringRef S) { |
543 | const static auto skipDigits = [](StringRef Input) { |
544 | return Input.drop_front( |
545 | std::min(Input.find_first_not_of("0123456789"), Input.size())); |
546 | }; |
547 | |
548 | // Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls |
549 | // safe. |
550 | if (S.empty() || S.equals("+") || S.equals("-")) |
551 | return false; |
552 | |
553 | if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN")) |
554 | return true; |
555 | |
556 | // Infinity and decimal numbers can be prefixed with sign. |
557 | StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S; |
558 | |
559 | // Check for infinity first, because checking for hex and oct numbers is more |
560 | // expensive. |
561 | if (Tail.equals(".inf") || Tail.equals(".Inf") || Tail.equals(".INF")) |
562 | return true; |
563 | |
564 | // Section 10.3.2 Tag Resolution |
565 | // YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with |
566 | // [-+], so S should be used instead of Tail. |
567 | if (S.startswith("0o")) |
568 | return S.size() > 2 && |
569 | S.drop_front(2).find_first_not_of("01234567") == StringRef::npos; |
570 | |
571 | if (S.startswith("0x")) |
572 | return S.size() > 2 && S.drop_front(2).find_first_not_of( |
573 | "0123456789abcdefABCDEF") == StringRef::npos; |
574 | |
575 | // Parse float: [-+]? (\. [0-9]+ | [0-9]+ (\. [0-9]* )?) ([eE] [-+]? [0-9]+)? |
576 | S = Tail; |
577 | |
578 | // Handle cases when the number starts with '.' and hence needs at least one |
579 | // digit after dot (as opposed by number which has digits before the dot), but |
580 | // doesn't have one. |
581 | if (S.startswith(".") && |
582 | (S.equals(".") || |
583 | (S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr))) |
584 | return false; |
585 | |
586 | if (S.startswith("E") || S.startswith("e")) |
587 | return false; |
588 | |
589 | enum ParseState { |
590 | Default, |
591 | FoundDot, |
592 | FoundExponent, |
593 | }; |
594 | ParseState State = Default; |
595 | |
596 | S = skipDigits(S); |
597 | |
598 | // Accept decimal integer. |
599 | if (S.empty()) |
600 | return true; |
601 | |
602 | if (S.front() == '.') { |
603 | State = FoundDot; |
604 | S = S.drop_front(); |
605 | } else if (S.front() == 'e' || S.front() == 'E') { |
606 | State = FoundExponent; |
607 | S = S.drop_front(); |
608 | } else { |
609 | return false; |
610 | } |
611 | |
612 | if (State == FoundDot) { |
613 | S = skipDigits(S); |
614 | if (S.empty()) |
615 | return true; |
616 | |
617 | if (S.front() == 'e' || S.front() == 'E') { |
618 | State = FoundExponent; |
619 | S = S.drop_front(); |
620 | } else { |
621 | return false; |
622 | } |
623 | } |
624 | |
625 | assert(State == FoundExponent && "Should have found exponent at this point.")((State == FoundExponent && "Should have found exponent at this point." ) ? static_cast<void> (0) : __assert_fail ("State == FoundExponent && \"Should have found exponent at this point.\"" , "/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/Support/YAMLTraits.h" , 625, __PRETTY_FUNCTION__)); |
626 | if (S.empty()) |
627 | return false; |
628 | |
629 | if (S.front() == '+' || S.front() == '-') { |
630 | S = S.drop_front(); |
631 | if (S.empty()) |
632 | return false; |
633 | } |
634 | |
635 | return skipDigits(S).empty(); |
636 | } |
637 | |
638 | inline bool isNull(StringRef S) { |
639 | return S.equals("null") || S.equals("Null") || S.equals("NULL") || |
640 | S.equals("~"); |
641 | } |
642 | |
643 | inline bool isBool(StringRef S) { |
644 | return S.equals("true") || S.equals("True") || S.equals("TRUE") || |
645 | S.equals("false") || S.equals("False") || S.equals("FALSE"); |
646 | } |
647 | |
648 | // 5.1. Character Set |
649 | // The allowed character range explicitly excludes the C0 control block #x0-#x1F |
650 | // (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1 |
651 | // control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate |
652 | // block #xD800-#xDFFF, #xFFFE, and #xFFFF. |
653 | inline QuotingType needsQuotes(StringRef S) { |
654 | if (S.empty()) |
655 | return QuotingType::Single; |
656 | if (isspace(S.front()) || isspace(S.back())) |
657 | return QuotingType::Single; |
658 | if (isNull(S)) |
659 | return QuotingType::Single; |
660 | if (isBool(S)) |
661 | return QuotingType::Single; |
662 | if (isNumeric(S)) |
663 | return QuotingType::Single; |
664 | |
665 | // 7.3.3 Plain Style |
666 | // Plain scalars must not begin with most indicators, as this would cause |
667 | // ambiguity with other YAML constructs. |
668 | static constexpr char Indicators[] = R"(-?:\,[]{}#&*!|>'"%@`)"; |
669 | if (S.find_first_of(Indicators) == 0) |
670 | return QuotingType::Single; |
671 | |
672 | QuotingType MaxQuotingNeeded = QuotingType::None; |
673 | for (unsigned char C : S) { |
674 | // Alphanum is safe. |
675 | if (isAlnum(C)) |
676 | continue; |
677 | |
678 | switch (C) { |
679 | // Safe scalar characters. |
680 | case '_': |
681 | case '-': |
682 | case '^': |
683 | case '.': |
684 | case ',': |
685 | case ' ': |
686 | // TAB (0x9) is allowed in unquoted strings. |
687 | case 0x9: |
688 | continue; |
689 | // LF(0xA) and CR(0xD) may delimit values and so require at least single |
690 | // quotes. |
691 | case 0xA: |
692 | case 0xD: |
693 | MaxQuotingNeeded = QuotingType::Single; |
694 | continue; |
695 | // DEL (0x7F) are excluded from the allowed character range. |
696 | case 0x7F: |
697 | return QuotingType::Double; |
698 | // Forward slash is allowed to be unquoted, but we quote it anyway. We have |
699 | // many tests that use FileCheck against YAML output, and this output often |
700 | // contains paths. If we quote backslashes but not forward slashes then |
701 | // paths will come out either quoted or unquoted depending on which platform |
702 | // the test is run on, making FileCheck comparisons difficult. |
703 | case '/': |
704 | default: { |
705 | // C0 control block (0x0 - 0x1F) is excluded from the allowed character |
706 | // range. |
707 | if (C <= 0x1F) |
708 | return QuotingType::Double; |
709 | |
710 | // Always double quote UTF-8. |
711 | if ((C & 0x80) != 0) |
712 | return QuotingType::Double; |
713 | |
714 | // The character is not safe, at least simple quoting needed. |
715 | MaxQuotingNeeded = QuotingType::Single; |
716 | } |
717 | } |
718 | } |
719 | |
720 | return MaxQuotingNeeded; |
721 | } |
722 | |
723 | template <typename T, typename Context> |
724 | struct missingTraits |
725 | : public std::integral_constant<bool, |
726 | !has_ScalarEnumerationTraits<T>::value && |
727 | !has_ScalarBitSetTraits<T>::value && |
728 | !has_ScalarTraits<T>::value && |
729 | !has_BlockScalarTraits<T>::value && |
730 | !has_TaggedScalarTraits<T>::value && |
731 | !has_MappingTraits<T, Context>::value && |
732 | !has_SequenceTraits<T>::value && |
733 | !has_CustomMappingTraits<T>::value && |
734 | !has_DocumentListTraits<T>::value && |
735 | !has_PolymorphicTraits<T>::value> {}; |
736 | |
737 | template <typename T, typename Context> |
738 | struct validatedMappingTraits |
739 | : public std::integral_constant< |
740 | bool, has_MappingTraits<T, Context>::value && |
741 | has_MappingValidateTraits<T, Context>::value> {}; |
742 | |
743 | template <typename T, typename Context> |
744 | struct unvalidatedMappingTraits |
745 | : public std::integral_constant< |
746 | bool, has_MappingTraits<T, Context>::value && |
747 | !has_MappingValidateTraits<T, Context>::value> {}; |
748 | |
749 | // Base class for Input and Output. |
750 | class IO { |
751 | public: |
752 | IO(void *Ctxt = nullptr); |
753 | virtual ~IO(); |
754 | |
755 | virtual bool outputting() = 0; |
756 | |
757 | virtual unsigned beginSequence() = 0; |
758 | virtual bool preflightElement(unsigned, void *&) = 0; |
759 | virtual void postflightElement(void*) = 0; |
760 | virtual void endSequence() = 0; |
761 | virtual bool canElideEmptySequence() = 0; |
762 | |
763 | virtual unsigned beginFlowSequence() = 0; |
764 | virtual bool preflightFlowElement(unsigned, void *&) = 0; |
765 | virtual void postflightFlowElement(void*) = 0; |
766 | virtual void endFlowSequence() = 0; |
767 | |
768 | virtual bool mapTag(StringRef Tag, bool Default=false) = 0; |
769 | virtual void beginMapping() = 0; |
770 | virtual void endMapping() = 0; |
771 | virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0; |
772 | virtual void postflightKey(void*) = 0; |
773 | virtual std::vector<StringRef> keys() = 0; |
774 | |
775 | virtual void beginFlowMapping() = 0; |
776 | virtual void endFlowMapping() = 0; |
777 | |
778 | virtual void beginEnumScalar() = 0; |
779 | virtual bool matchEnumScalar(const char*, bool) = 0; |
780 | virtual bool matchEnumFallback() = 0; |
781 | virtual void endEnumScalar() = 0; |
782 | |
783 | virtual bool beginBitSetScalar(bool &) = 0; |
784 | virtual bool bitSetMatch(const char*, bool) = 0; |
785 | virtual void endBitSetScalar() = 0; |
786 | |
787 | virtual void scalarString(StringRef &, QuotingType) = 0; |
788 | virtual void blockScalarString(StringRef &) = 0; |
789 | virtual void scalarTag(std::string &) = 0; |
790 | |
791 | virtual NodeKind getNodeKind() = 0; |
792 | |
793 | virtual void setError(const Twine &) = 0; |
794 | |
795 | template <typename T> |
796 | void enumCase(T &Val, const char* Str, const T ConstVal) { |
797 | if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) { |
798 | Val = ConstVal; |
799 | } |
800 | } |
801 | |
802 | // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF |
803 | template <typename T> |
804 | void enumCase(T &Val, const char* Str, const uint32_t ConstVal) { |
805 | if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) { |
806 | Val = ConstVal; |
807 | } |
808 | } |
809 | |
810 | template <typename FBT, typename T> |
811 | void enumFallback(T &Val) { |
812 | if (matchEnumFallback()) { |
813 | EmptyContext Context; |
814 | // FIXME: Force integral conversion to allow strong typedefs to convert. |
815 | FBT Res = static_cast<typename FBT::BaseType>(Val); |
816 | yamlize(*this, Res, true, Context); |
817 | Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res)); |
818 | } |
819 | } |
820 | |
821 | template <typename T> |
822 | void bitSetCase(T &Val, const char* Str, const T ConstVal) { |
823 | if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) { |
824 | Val = static_cast<T>(Val | ConstVal); |
825 | } |
826 | } |
827 | |
828 | // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF |
829 | template <typename T> |
830 | void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) { |
831 | if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) { |
832 | Val = static_cast<T>(Val | ConstVal); |
833 | } |
834 | } |
835 | |
836 | template <typename T> |
837 | void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) { |
838 | if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal)) |
839 | Val = Val | ConstVal; |
840 | } |
841 | |
842 | template <typename T> |
843 | void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal, |
844 | uint32_t Mask) { |
845 | if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal)) |
846 | Val = Val | ConstVal; |
847 | } |
848 | |
849 | void *getContext(); |
850 | void setContext(void *); |
851 | |
852 | template <typename T> void mapRequired(const char *Key, T &Val) { |
853 | EmptyContext Ctx; |
854 | this->processKey(Key, Val, true, Ctx); |
855 | } |
856 | |
857 | template <typename T, typename Context> |
858 | void mapRequired(const char *Key, T &Val, Context &Ctx) { |
859 | this->processKey(Key, Val, true, Ctx); |
860 | } |
861 | |
862 | template <typename T> void mapOptional(const char *Key, T &Val) { |
863 | EmptyContext Ctx; |
864 | mapOptionalWithContext(Key, Val, Ctx); |
865 | } |
866 | |
867 | template <typename T> |
868 | void mapOptional(const char *Key, T &Val, const T &Default) { |
869 | EmptyContext Ctx; |
870 | mapOptionalWithContext(Key, Val, Default, Ctx); |
871 | } |
872 | |
873 | template <typename T, typename Context> |
874 | typename std::enable_if<has_SequenceTraits<T>::value, void>::type |
875 | mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) { |
876 | // omit key/value instead of outputting empty sequence |
877 | if (this->canElideEmptySequence() && !(Val.begin() != Val.end())) |
878 | return; |
879 | this->processKey(Key, Val, false, Ctx); |
880 | } |
881 | |
882 | template <typename T, typename Context> |
883 | void mapOptionalWithContext(const char *Key, Optional<T> &Val, Context &Ctx) { |
884 | this->processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false, |
885 | Ctx); |
886 | } |
887 | |
888 | template <typename T, typename Context> |
889 | typename std::enable_if<!has_SequenceTraits<T>::value, void>::type |
890 | mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) { |
891 | this->processKey(Key, Val, false, Ctx); |
892 | } |
893 | |
894 | template <typename T, typename Context> |
895 | void mapOptionalWithContext(const char *Key, T &Val, const T &Default, |
896 | Context &Ctx) { |
897 | this->processKeyWithDefault(Key, Val, Default, false, Ctx); |
898 | } |
899 | |
900 | private: |
901 | template <typename T, typename Context> |
902 | void processKeyWithDefault(const char *Key, Optional<T> &Val, |
903 | const Optional<T> &DefaultValue, bool Required, |
904 | Context &Ctx) { |
905 | assert(DefaultValue.hasValue() == false &&((DefaultValue.hasValue() == false && "Optional<T> shouldn't have a value!" ) ? static_cast<void> (0) : __assert_fail ("DefaultValue.hasValue() == false && \"Optional<T> shouldn't have a value!\"" , "/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/Support/YAMLTraits.h" , 906, __PRETTY_FUNCTION__)) |
906 | "Optional<T> shouldn't have a value!")((DefaultValue.hasValue() == false && "Optional<T> shouldn't have a value!" ) ? static_cast<void> (0) : __assert_fail ("DefaultValue.hasValue() == false && \"Optional<T> shouldn't have a value!\"" , "/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/Support/YAMLTraits.h" , 906, __PRETTY_FUNCTION__)); |
907 | void *SaveInfo; |
908 | bool UseDefault = true; |
909 | const bool sameAsDefault = outputting() && !Val.hasValue(); |
910 | if (!outputting() && !Val.hasValue()) |
911 | Val = T(); |
912 | if (Val.hasValue() && |
913 | this->preflightKey(Key, Required, sameAsDefault, UseDefault, |
914 | SaveInfo)) { |
915 | yamlize(*this, Val.getValue(), Required, Ctx); |
916 | this->postflightKey(SaveInfo); |
917 | } else { |
918 | if (UseDefault) |
919 | Val = DefaultValue; |
920 | } |
921 | } |
922 | |
923 | template <typename T, typename Context> |
924 | void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue, |
925 | bool Required, Context &Ctx) { |
926 | void *SaveInfo; |
927 | bool UseDefault; |
928 | const bool sameAsDefault = outputting() && Val == DefaultValue; |
929 | if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault, |
930 | SaveInfo) ) { |
931 | yamlize(*this, Val, Required, Ctx); |
932 | this->postflightKey(SaveInfo); |
933 | } |
934 | else { |
935 | if ( UseDefault ) |
936 | Val = DefaultValue; |
937 | } |
938 | } |
939 | |
940 | template <typename T, typename Context> |
941 | void processKey(const char *Key, T &Val, bool Required, Context &Ctx) { |
942 | void *SaveInfo; |
943 | bool UseDefault; |
944 | if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) { |
945 | yamlize(*this, Val, Required, Ctx); |
946 | this->postflightKey(SaveInfo); |
947 | } |
948 | } |
949 | |
950 | private: |
951 | void *Ctxt; |
952 | }; |
953 | |
954 | namespace detail { |
955 | |
956 | template <typename T, typename Context> |
957 | void doMapping(IO &io, T &Val, Context &Ctx) { |
958 | MappingContextTraits<T, Context>::mapping(io, Val, Ctx); |
959 | } |
960 | |
961 | template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) { |
962 | MappingTraits<T>::mapping(io, Val); |
963 | } |
964 | |
965 | } // end namespace detail |
966 | |
967 | template <typename T> |
968 | typename std::enable_if<has_ScalarEnumerationTraits<T>::value, void>::type |
969 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
970 | io.beginEnumScalar(); |
971 | ScalarEnumerationTraits<T>::enumeration(io, Val); |
972 | io.endEnumScalar(); |
973 | } |
974 | |
975 | template <typename T> |
976 | typename std::enable_if<has_ScalarBitSetTraits<T>::value, void>::type |
977 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
978 | bool DoClear; |
979 | if ( io.beginBitSetScalar(DoClear) ) { |
980 | if ( DoClear ) |
981 | Val = static_cast<T>(0); |
982 | ScalarBitSetTraits<T>::bitset(io, Val); |
983 | io.endBitSetScalar(); |
984 | } |
985 | } |
986 | |
987 | template <typename T> |
988 | typename std::enable_if<has_ScalarTraits<T>::value, void>::type |
989 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
990 | if ( io.outputting() ) { |
991 | std::string Storage; |
992 | raw_string_ostream Buffer(Storage); |
993 | ScalarTraits<T>::output(Val, io.getContext(), Buffer); |
994 | StringRef Str = Buffer.str(); |
995 | io.scalarString(Str, ScalarTraits<T>::mustQuote(Str)); |
996 | } |
997 | else { |
998 | StringRef Str; |
999 | io.scalarString(Str, ScalarTraits<T>::mustQuote(Str)); |
1000 | StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val); |
1001 | if ( !Result.empty() ) { |
1002 | io.setError(Twine(Result)); |
1003 | } |
1004 | } |
1005 | } |
1006 | |
1007 | template <typename T> |
1008 | typename std::enable_if<has_BlockScalarTraits<T>::value, void>::type |
1009 | yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) { |
1010 | if (YamlIO.outputting()) { |
1011 | std::string Storage; |
1012 | raw_string_ostream Buffer(Storage); |
1013 | BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer); |
1014 | StringRef Str = Buffer.str(); |
1015 | YamlIO.blockScalarString(Str); |
1016 | } else { |
1017 | StringRef Str; |
1018 | YamlIO.blockScalarString(Str); |
1019 | StringRef Result = |
1020 | BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val); |
1021 | if (!Result.empty()) |
1022 | YamlIO.setError(Twine(Result)); |
1023 | } |
1024 | } |
1025 | |
1026 | template <typename T> |
1027 | typename std::enable_if<has_TaggedScalarTraits<T>::value, void>::type |
1028 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1029 | if (io.outputting()) { |
1030 | std::string ScalarStorage, TagStorage; |
1031 | raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage); |
1032 | TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer, |
1033 | TagBuffer); |
1034 | io.scalarTag(TagBuffer.str()); |
1035 | StringRef ScalarStr = ScalarBuffer.str(); |
1036 | io.scalarString(ScalarStr, |
1037 | TaggedScalarTraits<T>::mustQuote(Val, ScalarStr)); |
1038 | } else { |
1039 | std::string Tag; |
1040 | io.scalarTag(Tag); |
1041 | StringRef Str; |
1042 | io.scalarString(Str, QuotingType::None); |
1043 | StringRef Result = |
1044 | TaggedScalarTraits<T>::input(Str, Tag, io.getContext(), Val); |
1045 | if (!Result.empty()) { |
1046 | io.setError(Twine(Result)); |
1047 | } |
1048 | } |
1049 | } |
1050 | |
1051 | template <typename T, typename Context> |
1052 | typename std::enable_if<validatedMappingTraits<T, Context>::value, void>::type |
1053 | yamlize(IO &io, T &Val, bool, Context &Ctx) { |
1054 | if (has_FlowTraits<MappingTraits<T>>::value) |
1055 | io.beginFlowMapping(); |
1056 | else |
1057 | io.beginMapping(); |
1058 | if (io.outputting()) { |
1059 | StringRef Err = MappingTraits<T>::validate(io, Val); |
1060 | if (!Err.empty()) { |
1061 | errs() << Err << "\n"; |
1062 | assert(Err.empty() && "invalid struct trying to be written as yaml")((Err.empty() && "invalid struct trying to be written as yaml" ) ? static_cast<void> (0) : __assert_fail ("Err.empty() && \"invalid struct trying to be written as yaml\"" , "/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/Support/YAMLTraits.h" , 1062, __PRETTY_FUNCTION__)); |
1063 | } |
1064 | } |
1065 | detail::doMapping(io, Val, Ctx); |
1066 | if (!io.outputting()) { |
1067 | StringRef Err = MappingTraits<T>::validate(io, Val); |
1068 | if (!Err.empty()) |
1069 | io.setError(Err); |
1070 | } |
1071 | if (has_FlowTraits<MappingTraits<T>>::value) |
1072 | io.endFlowMapping(); |
1073 | else |
1074 | io.endMapping(); |
1075 | } |
1076 | |
1077 | template <typename T, typename Context> |
1078 | typename std::enable_if<unvalidatedMappingTraits<T, Context>::value, void>::type |
1079 | yamlize(IO &io, T &Val, bool, Context &Ctx) { |
1080 | if (has_FlowTraits<MappingTraits<T>>::value) { |
1081 | io.beginFlowMapping(); |
1082 | detail::doMapping(io, Val, Ctx); |
1083 | io.endFlowMapping(); |
1084 | } else { |
1085 | io.beginMapping(); |
1086 | detail::doMapping(io, Val, Ctx); |
1087 | io.endMapping(); |
1088 | } |
1089 | } |
1090 | |
1091 | template <typename T> |
1092 | typename std::enable_if<has_CustomMappingTraits<T>::value, void>::type |
1093 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1094 | if ( io.outputting() ) { |
1095 | io.beginMapping(); |
1096 | CustomMappingTraits<T>::output(io, Val); |
1097 | io.endMapping(); |
1098 | } else { |
1099 | io.beginMapping(); |
1100 | for (StringRef key : io.keys()) |
1101 | CustomMappingTraits<T>::inputOne(io, key, Val); |
1102 | io.endMapping(); |
1103 | } |
1104 | } |
1105 | |
1106 | template <typename T> |
1107 | typename std::enable_if<has_PolymorphicTraits<T>::value, void>::type |
1108 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1109 | switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val) |
1110 | : io.getNodeKind()) { |
1111 | case NodeKind::Scalar: |
1112 | return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val), true, Ctx); |
1113 | case NodeKind::Map: |
1114 | return yamlize(io, PolymorphicTraits<T>::getAsMap(Val), true, Ctx); |
1115 | case NodeKind::Sequence: |
1116 | return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val), true, Ctx); |
1117 | } |
1118 | } |
1119 | |
1120 | template <typename T> |
1121 | typename std::enable_if<missingTraits<T, EmptyContext>::value, void>::type |
1122 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1123 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; |
1124 | } |
1125 | |
1126 | template <typename T, typename Context> |
1127 | typename std::enable_if<has_SequenceTraits<T>::value, void>::type |
1128 | yamlize(IO &io, T &Seq, bool, Context &Ctx) { |
1129 | if ( has_FlowTraits< SequenceTraits<T>>::value ) { |
1130 | unsigned incnt = io.beginFlowSequence(); |
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.preflightFlowElement(i, SaveInfo) ) { |
1135 | yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx); |
1136 | io.postflightFlowElement(SaveInfo); |
1137 | } |
1138 | } |
1139 | io.endFlowSequence(); |
1140 | } |
1141 | else { |
1142 | unsigned incnt = io.beginSequence(); |
1143 | unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt; |
1144 | for(unsigned i=0; i < count; ++i) { |
1145 | void *SaveInfo; |
1146 | if ( io.preflightElement(i, SaveInfo) ) { |
1147 | yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx); |
1148 | io.postflightElement(SaveInfo); |
1149 | } |
1150 | } |
1151 | io.endSequence(); |
1152 | } |
1153 | } |
1154 | |
1155 | template<> |
1156 | struct ScalarTraits<bool> { |
1157 | static void output(const bool &, void* , raw_ostream &); |
1158 | static StringRef input(StringRef, void *, bool &); |
1159 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1160 | }; |
1161 | |
1162 | template<> |
1163 | struct ScalarTraits<StringRef> { |
1164 | static void output(const StringRef &, void *, raw_ostream &); |
1165 | static StringRef input(StringRef, void *, StringRef &); |
1166 | static QuotingType mustQuote(StringRef S) { return needsQuotes(S); } |
1167 | }; |
1168 | |
1169 | template<> |
1170 | struct ScalarTraits<std::string> { |
1171 | static void output(const std::string &, void *, raw_ostream &); |
1172 | static StringRef input(StringRef, void *, std::string &); |
1173 | static QuotingType mustQuote(StringRef S) { return needsQuotes(S); } |
1174 | }; |
1175 | |
1176 | template<> |
1177 | struct ScalarTraits<uint8_t> { |
1178 | static void output(const uint8_t &, void *, raw_ostream &); |
1179 | static StringRef input(StringRef, void *, uint8_t &); |
1180 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1181 | }; |
1182 | |
1183 | template<> |
1184 | struct ScalarTraits<uint16_t> { |
1185 | static void output(const uint16_t &, void *, raw_ostream &); |
1186 | static StringRef input(StringRef, void *, uint16_t &); |
1187 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1188 | }; |
1189 | |
1190 | template<> |
1191 | struct ScalarTraits<uint32_t> { |
1192 | static void output(const uint32_t &, void *, raw_ostream &); |
1193 | static StringRef input(StringRef, void *, uint32_t &); |
1194 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1195 | }; |
1196 | |
1197 | template<> |
1198 | struct ScalarTraits<uint64_t> { |
1199 | static void output(const uint64_t &, void *, raw_ostream &); |
1200 | static StringRef input(StringRef, void *, uint64_t &); |
1201 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1202 | }; |
1203 | |
1204 | template<> |
1205 | struct ScalarTraits<int8_t> { |
1206 | static void output(const int8_t &, void *, raw_ostream &); |
1207 | static StringRef input(StringRef, void *, int8_t &); |
1208 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1209 | }; |
1210 | |
1211 | template<> |
1212 | struct ScalarTraits<int16_t> { |
1213 | static void output(const int16_t &, void *, raw_ostream &); |
1214 | static StringRef input(StringRef, void *, int16_t &); |
1215 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1216 | }; |
1217 | |
1218 | template<> |
1219 | struct ScalarTraits<int32_t> { |
1220 | static void output(const int32_t &, void *, raw_ostream &); |
1221 | static StringRef input(StringRef, void *, int32_t &); |
1222 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1223 | }; |
1224 | |
1225 | template<> |
1226 | struct ScalarTraits<int64_t> { |
1227 | static void output(const int64_t &, void *, raw_ostream &); |
1228 | static StringRef input(StringRef, void *, int64_t &); |
1229 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1230 | }; |
1231 | |
1232 | template<> |
1233 | struct ScalarTraits<float> { |
1234 | static void output(const float &, void *, raw_ostream &); |
1235 | static StringRef input(StringRef, void *, float &); |
1236 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1237 | }; |
1238 | |
1239 | template<> |
1240 | struct ScalarTraits<double> { |
1241 | static void output(const double &, void *, raw_ostream &); |
1242 | static StringRef input(StringRef, void *, double &); |
1243 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1244 | }; |
1245 | |
1246 | // For endian types, we just use the existing ScalarTraits for the underlying |
1247 | // type. This way endian aware types are supported whenever a ScalarTraits |
1248 | // is defined for the underlying type. |
1249 | template <typename value_type, support::endianness endian, size_t alignment> |
1250 | struct ScalarTraits<support::detail::packed_endian_specific_integral< |
1251 | value_type, endian, alignment>> { |
1252 | using endian_type = |
1253 | support::detail::packed_endian_specific_integral<value_type, endian, |
1254 | alignment>; |
1255 | |
1256 | static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) { |
1257 | ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream); |
1258 | } |
1259 | |
1260 | static StringRef input(StringRef Str, void *Ctx, endian_type &E) { |
1261 | value_type V; |
1262 | auto R = ScalarTraits<value_type>::input(Str, Ctx, V); |
1263 | E = static_cast<endian_type>(V); |
1264 | return R; |
1265 | } |
1266 | |
1267 | static QuotingType mustQuote(StringRef Str) { |
1268 | return ScalarTraits<value_type>::mustQuote(Str); |
1269 | } |
1270 | }; |
1271 | |
1272 | // Utility for use within MappingTraits<>::mapping() method |
1273 | // to [de]normalize an object for use with YAML conversion. |
1274 | template <typename TNorm, typename TFinal> |
1275 | struct MappingNormalization { |
1276 | MappingNormalization(IO &i_o, TFinal &Obj) |
1277 | : io(i_o), BufPtr(nullptr), Result(Obj) { |
1278 | if ( io.outputting() ) { |
1279 | BufPtr = new (&Buffer) TNorm(io, Obj); |
1280 | } |
1281 | else { |
1282 | BufPtr = new (&Buffer) TNorm(io); |
1283 | } |
1284 | } |
1285 | |
1286 | ~MappingNormalization() { |
1287 | if ( ! io.outputting() ) { |
1288 | Result = BufPtr->denormalize(io); |
1289 | } |
1290 | BufPtr->~TNorm(); |
1291 | } |
1292 | |
1293 | TNorm* operator->() { return BufPtr; } |
1294 | |
1295 | private: |
1296 | using Storage = AlignedCharArrayUnion<TNorm>; |
1297 | |
1298 | Storage Buffer; |
1299 | IO &io; |
1300 | TNorm *BufPtr; |
1301 | TFinal &Result; |
1302 | }; |
1303 | |
1304 | // Utility for use within MappingTraits<>::mapping() method |
1305 | // to [de]normalize an object for use with YAML conversion. |
1306 | template <typename TNorm, typename TFinal> |
1307 | struct MappingNormalizationHeap { |
1308 | MappingNormalizationHeap(IO &i_o, TFinal &Obj, BumpPtrAllocator *allocator) |
1309 | : io(i_o), Result(Obj) { |
1310 | if ( io.outputting() ) { |
1311 | BufPtr = new (&Buffer) TNorm(io, Obj); |
1312 | } |
1313 | else if (allocator) { |
1314 | BufPtr = allocator->Allocate<TNorm>(); |
1315 | new (BufPtr) TNorm(io); |
1316 | } else { |
1317 | BufPtr = new TNorm(io); |
1318 | } |
1319 | } |
1320 | |
1321 | ~MappingNormalizationHeap() { |
1322 | if ( io.outputting() ) { |
1323 | BufPtr->~TNorm(); |
1324 | } |
1325 | else { |
1326 | Result = BufPtr->denormalize(io); |
1327 | } |
1328 | } |
1329 | |
1330 | TNorm* operator->() { return BufPtr; } |
1331 | |
1332 | private: |
1333 | using Storage = AlignedCharArrayUnion<TNorm>; |
1334 | |
1335 | Storage Buffer; |
1336 | IO &io; |
1337 | TNorm *BufPtr = nullptr; |
1338 | TFinal &Result; |
1339 | }; |
1340 | |
1341 | /// |
1342 | /// The Input class is used to parse a yaml document into in-memory structs |
1343 | /// and vectors. |
1344 | /// |
1345 | /// It works by using YAMLParser to do a syntax parse of the entire yaml |
1346 | /// document, then the Input class builds a graph of HNodes which wraps |
1347 | /// each yaml Node. The extra layer is buffering. The low level yaml |
1348 | /// parser only lets you look at each node once. The buffering layer lets |
1349 | /// you search and interate multiple times. This is necessary because |
1350 | /// the mapRequired() method calls may not be in the same order |
1351 | /// as the keys in the document. |
1352 | /// |
1353 | class Input : public IO { |
1354 | public: |
1355 | // Construct a yaml Input object from a StringRef and optional |
1356 | // user-data. The DiagHandler can be specified to provide |
1357 | // alternative error reporting. |
1358 | Input(StringRef InputContent, |
1359 | void *Ctxt = nullptr, |
1360 | SourceMgr::DiagHandlerTy DiagHandler = nullptr, |
1361 | void *DiagHandlerCtxt = nullptr); |
1362 | Input(MemoryBufferRef Input, |
1363 | void *Ctxt = nullptr, |
1364 | SourceMgr::DiagHandlerTy DiagHandler = nullptr, |
1365 | void *DiagHandlerCtxt = nullptr); |
1366 | ~Input() override; |
1367 | |
1368 | // Check if there was an syntax or semantic error during parsing. |
1369 | std::error_code error(); |
1370 | |
1371 | private: |
1372 | bool outputting() override; |
1373 | bool mapTag(StringRef, bool) override; |
1374 | void beginMapping() override; |
1375 | void endMapping() override; |
1376 | bool preflightKey(const char *, bool, bool, bool &, void *&) override; |
1377 | void postflightKey(void *) override; |
1378 | std::vector<StringRef> keys() override; |
1379 | void beginFlowMapping() override; |
1380 | void endFlowMapping() override; |
1381 | unsigned beginSequence() override; |
1382 | void endSequence() override; |
1383 | bool preflightElement(unsigned index, void *&) override; |
1384 | void postflightElement(void *) override; |
1385 | unsigned beginFlowSequence() override; |
1386 | bool preflightFlowElement(unsigned , void *&) override; |
1387 | void postflightFlowElement(void *) override; |
1388 | void endFlowSequence() override; |
1389 | void beginEnumScalar() override; |
1390 | bool matchEnumScalar(const char*, bool) override; |
1391 | bool matchEnumFallback() override; |
1392 | void endEnumScalar() override; |
1393 | bool beginBitSetScalar(bool &) override; |
1394 | bool bitSetMatch(const char *, bool ) override; |
1395 | void endBitSetScalar() override; |
1396 | void scalarString(StringRef &, QuotingType) override; |
1397 | void blockScalarString(StringRef &) override; |
1398 | void scalarTag(std::string &) override; |
1399 | NodeKind getNodeKind() override; |
1400 | void setError(const Twine &message) override; |
1401 | bool canElideEmptySequence() override; |
1402 | |
1403 | class HNode { |
1404 | virtual void anchor(); |
1405 | |
1406 | public: |
1407 | HNode(Node *n) : _node(n) { } |
1408 | virtual ~HNode() = default; |
1409 | |
1410 | static bool classof(const HNode *) { return true; } |
1411 | |
1412 | Node *_node; |
1413 | }; |
1414 | |
1415 | class EmptyHNode : public HNode { |
1416 | void anchor() override; |
1417 | |
1418 | public: |
1419 | EmptyHNode(Node *n) : HNode(n) { } |
1420 | |
1421 | static bool classof(const HNode *n) { return NullNode::classof(n->_node); } |
1422 | |
1423 | static bool classof(const EmptyHNode *) { return true; } |
1424 | }; |
1425 | |
1426 | class ScalarHNode : public HNode { |
1427 | void anchor() override; |
1428 | |
1429 | public: |
1430 | ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { } |
1431 | |
1432 | StringRef value() const { return _value; } |
1433 | |
1434 | static bool classof(const HNode *n) { |
1435 | return ScalarNode::classof(n->_node) || |
1436 | BlockScalarNode::classof(n->_node); |
1437 | } |
1438 | |
1439 | static bool classof(const ScalarHNode *) { return true; } |
1440 | |
1441 | protected: |
1442 | StringRef _value; |
1443 | }; |
1444 | |
1445 | class MapHNode : public HNode { |
1446 | void anchor() override; |
1447 | |
1448 | public: |
1449 | MapHNode(Node *n) : HNode(n) { } |
1450 | |
1451 | static bool classof(const HNode *n) { |
1452 | return MappingNode::classof(n->_node); |
1453 | } |
1454 | |
1455 | static bool classof(const MapHNode *) { return true; } |
1456 | |
1457 | using NameToNode = StringMap<std::unique_ptr<HNode>>; |
1458 | |
1459 | NameToNode Mapping; |
1460 | SmallVector<std::string, 6> ValidKeys; |
1461 | }; |
1462 | |
1463 | class SequenceHNode : public HNode { |
1464 | void anchor() override; |
1465 | |
1466 | public: |
1467 | SequenceHNode(Node *n) : HNode(n) { } |
1468 | |
1469 | static bool classof(const HNode *n) { |
1470 | return SequenceNode::classof(n->_node); |
1471 | } |
1472 | |
1473 | static bool classof(const SequenceHNode *) { return true; } |
1474 | |
1475 | std::vector<std::unique_ptr<HNode>> Entries; |
1476 | }; |
1477 | |
1478 | std::unique_ptr<Input::HNode> createHNodes(Node *node); |
1479 | void setError(HNode *hnode, const Twine &message); |
1480 | void setError(Node *node, const Twine &message); |
1481 | |
1482 | public: |
1483 | // These are only used by operator>>. They could be private |
1484 | // if those templated things could be made friends. |
1485 | bool setCurrentDocument(); |
1486 | bool nextDocument(); |
1487 | |
1488 | /// Returns the current node that's being parsed by the YAML Parser. |
1489 | const Node *getCurrentNode() const; |
1490 | |
1491 | private: |
1492 | SourceMgr SrcMgr; // must be before Strm |
1493 | std::unique_ptr<llvm::yaml::Stream> Strm; |
1494 | std::unique_ptr<HNode> TopNode; |
1495 | std::error_code EC; |
1496 | BumpPtrAllocator StringAllocator; |
1497 | document_iterator DocIterator; |
1498 | std::vector<bool> BitValuesUsed; |
1499 | HNode *CurrentNode = nullptr; |
1500 | bool ScalarMatchFound; |
1501 | }; |
1502 | |
1503 | /// |
1504 | /// The Output class is used to generate a yaml document from in-memory structs |
1505 | /// and vectors. |
1506 | /// |
1507 | class Output : public IO { |
1508 | public: |
1509 | Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70); |
1510 | ~Output() override; |
1511 | |
1512 | /// Set whether or not to output optional values which are equal |
1513 | /// to the default value. By default, when outputting if you attempt |
1514 | /// to write a value that is equal to the default, the value gets ignored. |
1515 | /// Sometimes, it is useful to be able to see these in the resulting YAML |
1516 | /// anyway. |
1517 | void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; } |
1518 | |
1519 | bool outputting() override; |
1520 | bool mapTag(StringRef, bool) override; |
1521 | void beginMapping() override; |
1522 | void endMapping() override; |
1523 | bool preflightKey(const char *key, bool, bool, bool &, void *&) override; |
1524 | void postflightKey(void *) override; |
1525 | std::vector<StringRef> keys() override; |
1526 | void beginFlowMapping() override; |
1527 | void endFlowMapping() override; |
1528 | unsigned beginSequence() override; |
1529 | void endSequence() override; |
1530 | bool preflightElement(unsigned, void *&) override; |
1531 | void postflightElement(void *) override; |
1532 | unsigned beginFlowSequence() override; |
1533 | bool preflightFlowElement(unsigned, void *&) override; |
1534 | void postflightFlowElement(void *) override; |
1535 | void endFlowSequence() override; |
1536 | void beginEnumScalar() override; |
1537 | bool matchEnumScalar(const char*, bool) override; |
1538 | bool matchEnumFallback() override; |
1539 | void endEnumScalar() override; |
1540 | bool beginBitSetScalar(bool &) override; |
1541 | bool bitSetMatch(const char *, bool ) override; |
1542 | void endBitSetScalar() override; |
1543 | void scalarString(StringRef &, QuotingType) override; |
1544 | void blockScalarString(StringRef &) override; |
1545 | void scalarTag(std::string &) override; |
1546 | NodeKind getNodeKind() override; |
1547 | void setError(const Twine &message) override; |
1548 | bool canElideEmptySequence() override; |
1549 | |
1550 | // These are only used by operator<<. They could be private |
1551 | // if that templated operator could be made a friend. |
1552 | void beginDocuments(); |
1553 | bool preflightDocument(unsigned); |
1554 | void postflightDocument(); |
1555 | void endDocuments(); |
1556 | |
1557 | private: |
1558 | void output(StringRef s); |
1559 | void outputUpToEndOfLine(StringRef s); |
1560 | void newLineCheck(); |
1561 | void outputNewLine(); |
1562 | void paddedKey(StringRef key); |
1563 | void flowKey(StringRef Key); |
1564 | |
1565 | enum InState { |
1566 | inSeqFirstElement, |
1567 | inSeqOtherElement, |
1568 | inFlowSeqFirstElement, |
1569 | inFlowSeqOtherElement, |
1570 | inMapFirstKey, |
1571 | inMapOtherKey, |
1572 | inFlowMapFirstKey, |
1573 | inFlowMapOtherKey |
1574 | }; |
1575 | |
1576 | static bool inSeqAnyElement(InState State); |
1577 | static bool inFlowSeqAnyElement(InState State); |
1578 | static bool inMapAnyKey(InState State); |
1579 | static bool inFlowMapAnyKey(InState State); |
1580 | |
1581 | raw_ostream &Out; |
1582 | int WrapColumn; |
1583 | SmallVector<InState, 8> StateStack; |
1584 | int Column = 0; |
1585 | int ColumnAtFlowStart = 0; |
1586 | int ColumnAtMapFlowStart = 0; |
1587 | bool NeedBitValueComma = false; |
1588 | bool NeedFlowSequenceComma = false; |
1589 | bool EnumerationMatchFound = false; |
1590 | bool NeedsNewLine = false; |
1591 | bool WriteDefaultValues = false; |
1592 | }; |
1593 | |
1594 | /// YAML I/O does conversion based on types. But often native data types |
1595 | /// are just a typedef of built in intergral types (e.g. int). But the C++ |
1596 | /// type matching system sees through the typedef and all the typedefed types |
1597 | /// look like a built in type. This will cause the generic YAML I/O conversion |
1598 | /// to be used. To provide better control over the YAML conversion, you can |
1599 | /// use this macro instead of typedef. It will create a class with one field |
1600 | /// and automatic conversion operators to and from the base type. |
1601 | /// Based on BOOST_STRONG_TYPEDEF |
1602 | #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; }; \ |
1603 | struct _type { \ |
1604 | _type() = default; \ |
1605 | _type(const _base v) : value(v) {} \ |
1606 | _type(const _type &v) = default; \ |
1607 | _type &operator=(const _type &rhs) = default; \ |
1608 | _type &operator=(const _base &rhs) { value = rhs; return *this; } \ |
1609 | operator const _base & () const { return value; } \ |
1610 | bool operator==(const _type &rhs) const { return value == rhs.value; } \ |
1611 | bool operator==(const _base &rhs) const { return value == rhs; } \ |
1612 | bool operator<(const _type &rhs) const { return value < rhs.value; } \ |
1613 | _base value; \ |
1614 | using BaseType = _base; \ |
1615 | }; |
1616 | |
1617 | /// |
1618 | /// Use these types instead of uintXX_t in any mapping to have |
1619 | /// its yaml output formatted as hexadecimal. |
1620 | /// |
1621 | LLVM_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; }; |
1622 | LLVM_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; }; |
1623 | LLVM_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; }; |
1624 | LLVM_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; }; |
1625 | |
1626 | template<> |
1627 | struct ScalarTraits<Hex8> { |
1628 | static void output(const Hex8 &, void *, raw_ostream &); |
1629 | static StringRef input(StringRef, void *, Hex8 &); |
1630 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1631 | }; |
1632 | |
1633 | template<> |
1634 | struct ScalarTraits<Hex16> { |
1635 | static void output(const Hex16 &, void *, raw_ostream &); |
1636 | static StringRef input(StringRef, void *, Hex16 &); |
1637 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1638 | }; |
1639 | |
1640 | template<> |
1641 | struct ScalarTraits<Hex32> { |
1642 | static void output(const Hex32 &, void *, raw_ostream &); |
1643 | static StringRef input(StringRef, void *, Hex32 &); |
1644 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1645 | }; |
1646 | |
1647 | template<> |
1648 | struct ScalarTraits<Hex64> { |
1649 | static void output(const Hex64 &, void *, raw_ostream &); |
1650 | static StringRef input(StringRef, void *, Hex64 &); |
1651 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1652 | }; |
1653 | |
1654 | // Define non-member operator>> so that Input can stream in a document list. |
1655 | template <typename T> |
1656 | inline |
1657 | typename std::enable_if<has_DocumentListTraits<T>::value, Input &>::type |
1658 | operator>>(Input &yin, T &docList) { |
1659 | int i = 0; |
1660 | EmptyContext Ctx; |
1661 | while ( yin.setCurrentDocument() ) { |
1662 | yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx); |
1663 | if ( yin.error() ) |
1664 | return yin; |
1665 | yin.nextDocument(); |
1666 | ++i; |
1667 | } |
1668 | return yin; |
1669 | } |
1670 | |
1671 | // Define non-member operator>> so that Input can stream in a map as a document. |
1672 | template <typename T> |
1673 | inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value, |
1674 | Input &>::type |
1675 | operator>>(Input &yin, T &docMap) { |
1676 | EmptyContext Ctx; |
1677 | yin.setCurrentDocument(); |
1678 | yamlize(yin, docMap, true, Ctx); |
1679 | return yin; |
1680 | } |
1681 | |
1682 | // Define non-member operator>> so that Input can stream in a sequence as |
1683 | // a document. |
1684 | template <typename T> |
1685 | inline |
1686 | typename std::enable_if<has_SequenceTraits<T>::value, Input &>::type |
1687 | operator>>(Input &yin, T &docSeq) { |
1688 | EmptyContext Ctx; |
1689 | if (yin.setCurrentDocument()) |
1690 | yamlize(yin, docSeq, true, Ctx); |
1691 | return yin; |
1692 | } |
1693 | |
1694 | // Define non-member operator>> so that Input can stream in a block scalar. |
1695 | template <typename T> |
1696 | inline |
1697 | typename std::enable_if<has_BlockScalarTraits<T>::value, Input &>::type |
1698 | operator>>(Input &In, T &Val) { |
1699 | EmptyContext Ctx; |
1700 | if (In.setCurrentDocument()) |
1701 | yamlize(In, Val, true, Ctx); |
1702 | return In; |
1703 | } |
1704 | |
1705 | // Define non-member operator>> so that Input can stream in a string map. |
1706 | template <typename T> |
1707 | inline |
1708 | typename std::enable_if<has_CustomMappingTraits<T>::value, Input &>::type |
1709 | operator>>(Input &In, T &Val) { |
1710 | EmptyContext Ctx; |
1711 | if (In.setCurrentDocument()) |
1712 | yamlize(In, Val, true, Ctx); |
1713 | return In; |
1714 | } |
1715 | |
1716 | // Define non-member operator>> so that Input can stream in a polymorphic type. |
1717 | template <typename T> |
1718 | inline typename std::enable_if<has_PolymorphicTraits<T>::value, Input &>::type |
1719 | operator>>(Input &In, T &Val) { |
1720 | EmptyContext Ctx; |
1721 | if (In.setCurrentDocument()) |
1722 | yamlize(In, Val, true, Ctx); |
1723 | return In; |
1724 | } |
1725 | |
1726 | // Provide better error message about types missing a trait specialization |
1727 | template <typename T> |
1728 | inline typename std::enable_if<missingTraits<T, EmptyContext>::value, |
1729 | Input &>::type |
1730 | operator>>(Input &yin, T &docSeq) { |
1731 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; |
1732 | return yin; |
1733 | } |
1734 | |
1735 | // Define non-member operator<< so that Output can stream out document list. |
1736 | template <typename T> |
1737 | inline |
1738 | typename std::enable_if<has_DocumentListTraits<T>::value, Output &>::type |
1739 | operator<<(Output &yout, T &docList) { |
1740 | EmptyContext Ctx; |
1741 | yout.beginDocuments(); |
1742 | const size_t count = DocumentListTraits<T>::size(yout, docList); |
1743 | for(size_t i=0; i < count; ++i) { |
1744 | if ( yout.preflightDocument(i) ) { |
1745 | yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true, |
1746 | Ctx); |
1747 | yout.postflightDocument(); |
1748 | } |
1749 | } |
1750 | yout.endDocuments(); |
1751 | return yout; |
1752 | } |
1753 | |
1754 | // Define non-member operator<< so that Output can stream out a map. |
1755 | template <typename T> |
1756 | inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value, |
1757 | Output &>::type |
1758 | operator<<(Output &yout, T &map) { |
1759 | EmptyContext Ctx; |
1760 | yout.beginDocuments(); |
1761 | if ( yout.preflightDocument(0) ) { |
1762 | yamlize(yout, map, true, Ctx); |
1763 | yout.postflightDocument(); |
1764 | } |
1765 | yout.endDocuments(); |
1766 | return yout; |
1767 | } |
1768 | |
1769 | // Define non-member operator<< so that Output can stream out a sequence. |
1770 | template <typename T> |
1771 | inline |
1772 | typename std::enable_if<has_SequenceTraits<T>::value, Output &>::type |
1773 | operator<<(Output &yout, T &seq) { |
1774 | EmptyContext Ctx; |
1775 | yout.beginDocuments(); |
1776 | if ( yout.preflightDocument(0) ) { |
1777 | yamlize(yout, seq, true, Ctx); |
1778 | yout.postflightDocument(); |
1779 | } |
1780 | yout.endDocuments(); |
1781 | return yout; |
1782 | } |
1783 | |
1784 | // Define non-member operator<< so that Output can stream out a block scalar. |
1785 | template <typename T> |
1786 | inline |
1787 | typename std::enable_if<has_BlockScalarTraits<T>::value, Output &>::type |
1788 | operator<<(Output &Out, T &Val) { |
1789 | EmptyContext Ctx; |
1790 | Out.beginDocuments(); |
1791 | if (Out.preflightDocument(0)) { |
1792 | yamlize(Out, Val, true, Ctx); |
1793 | Out.postflightDocument(); |
1794 | } |
1795 | Out.endDocuments(); |
1796 | return Out; |
1797 | } |
1798 | |
1799 | // Define non-member operator<< so that Output can stream out a string map. |
1800 | template <typename T> |
1801 | inline |
1802 | typename std::enable_if<has_CustomMappingTraits<T>::value, Output &>::type |
1803 | operator<<(Output &Out, T &Val) { |
1804 | EmptyContext Ctx; |
1805 | Out.beginDocuments(); |
1806 | if (Out.preflightDocument(0)) { |
1807 | yamlize(Out, Val, true, Ctx); |
1808 | Out.postflightDocument(); |
1809 | } |
1810 | Out.endDocuments(); |
1811 | return Out; |
1812 | } |
1813 | |
1814 | // Define non-member operator<< so that Output can stream out a polymorphic |
1815 | // type. |
1816 | template <typename T> |
1817 | inline typename std::enable_if<has_PolymorphicTraits<T>::value, Output &>::type |
1818 | operator<<(Output &Out, T &Val) { |
1819 | EmptyContext Ctx; |
1820 | Out.beginDocuments(); |
1821 | if (Out.preflightDocument(0)) { |
1822 | // FIXME: The parser does not support explicit documents terminated with a |
1823 | // plain scalar; the end-marker is included as part of the scalar token. |
1824 | assert(PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && "plain scalar documents are not supported")((PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && "plain scalar documents are not supported") ? static_cast <void> (0) : __assert_fail ("PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && \"plain scalar documents are not supported\"" , "/build/llvm-toolchain-snapshot-8~svn350071/include/llvm/Support/YAMLTraits.h" , 1824, __PRETTY_FUNCTION__)); |
1825 | yamlize(Out, Val, true, Ctx); |
1826 | Out.postflightDocument(); |
1827 | } |
1828 | Out.endDocuments(); |
1829 | return Out; |
1830 | } |
1831 | |
1832 | // Provide better error message about types missing a trait specialization |
1833 | template <typename T> |
1834 | inline typename std::enable_if<missingTraits<T, EmptyContext>::value, |
1835 | Output &>::type |
1836 | operator<<(Output &yout, T &seq) { |
1837 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; |
1838 | return yout; |
1839 | } |
1840 | |
1841 | template <bool B> struct IsFlowSequenceBase {}; |
1842 | template <> struct IsFlowSequenceBase<true> { static const bool flow = true; }; |
1843 | |
1844 | template <typename T, bool Flow> |
1845 | struct SequenceTraitsImpl : IsFlowSequenceBase<Flow> { |
1846 | private: |
1847 | using type = typename T::value_type; |
1848 | |
1849 | public: |
1850 | static size_t size(IO &io, T &seq) { return seq.size(); } |
1851 | |
1852 | static type &element(IO &io, T &seq, size_t index) { |
1853 | if (index >= seq.size()) |
1854 | seq.resize(index + 1); |
1855 | return seq[index]; |
1856 | } |
1857 | }; |
1858 | |
1859 | // Simple helper to check an expression can be used as a bool-valued template |
1860 | // argument. |
1861 | template <bool> struct CheckIsBool { static const bool value = true; }; |
1862 | |
1863 | // If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have |
1864 | // SequenceTraits that do the obvious thing. |
1865 | template <typename T> |
1866 | struct SequenceTraits<std::vector<T>, |
1867 | typename std::enable_if<CheckIsBool< |
1868 | SequenceElementTraits<T>::flow>::value>::type> |
1869 | : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {}; |
1870 | template <typename T, unsigned N> |
1871 | struct SequenceTraits<SmallVector<T, N>, |
1872 | typename std::enable_if<CheckIsBool< |
1873 | SequenceElementTraits<T>::flow>::value>::type> |
1874 | : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {}; |
1875 | |
1876 | // Sequences of fundamental types use flow formatting. |
1877 | template <typename T> |
1878 | struct SequenceElementTraits< |
1879 | T, typename std::enable_if<std::is_fundamental<T>::value>::type> { |
1880 | static const bool flow = true; |
1881 | }; |
1882 | |
1883 | // Sequences of strings use block formatting. |
1884 | template<> struct SequenceElementTraits<std::string> { |
1885 | static const bool flow = false; |
1886 | }; |
1887 | template<> struct SequenceElementTraits<StringRef> { |
1888 | static const bool flow = false; |
1889 | }; |
1890 | template<> struct SequenceElementTraits<std::pair<std::string, std::string>> { |
1891 | static const bool flow = false; |
1892 | }; |
1893 | |
1894 | /// Implementation of CustomMappingTraits for std::map<std::string, T>. |
1895 | template <typename T> struct StdMapStringCustomMappingTraitsImpl { |
1896 | using map_type = std::map<std::string, T>; |
1897 | |
1898 | static void inputOne(IO &io, StringRef key, map_type &v) { |
1899 | io.mapRequired(key.str().c_str(), v[key]); |
1900 | } |
1901 | |
1902 | static void output(IO &io, map_type &v) { |
1903 | for (auto &p : v) |
1904 | io.mapRequired(p.first.c_str(), p.second); |
1905 | } |
1906 | }; |
1907 | |
1908 | } // end namespace yaml |
1909 | } // end namespace llvm |
1910 | |
1911 | #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; }; } } \ |
1912 | namespace llvm { \ |
1913 | namespace yaml { \ |
1914 | static_assert( \ |
1915 | !std::is_fundamental<TYPE>::value && \ |
1916 | !std::is_same<TYPE, std::string>::value && \ |
1917 | !std::is_same<TYPE, llvm::StringRef>::value, \ |
1918 | "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \ |
1919 | template <> struct SequenceElementTraits<TYPE> { \ |
1920 | static const bool flow = FLOW; \ |
1921 | }; \ |
1922 | } \ |
1923 | } |
1924 | |
1925 | /// Utility for declaring that a std::vector of a particular type |
1926 | /// should be considered a YAML sequence. |
1927 | #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; }; } } \ |
1928 | 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; }; } } |
1929 | |
1930 | /// Utility for declaring that a std::vector of a particular type |
1931 | /// should be considered a YAML flow sequence. |
1932 | #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; }; } } \ |
1933 | 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; }; } } |
1934 | |
1935 | #define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type)namespace llvm { namespace yaml { template <> struct MappingTraits <Type> { static void mapping(IO &IO, Type &Obj) ; }; } } \ |
1936 | namespace llvm { \ |
1937 | namespace yaml { \ |
1938 | template <> struct MappingTraits<Type> { \ |
1939 | static void mapping(IO &IO, Type &Obj); \ |
1940 | }; \ |
1941 | } \ |
1942 | } |
1943 | |
1944 | #define LLVM_YAML_DECLARE_ENUM_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarEnumerationTraits <Type> { static void enumeration(IO &io, Type & Value); }; } } \ |
1945 | namespace llvm { \ |
1946 | namespace yaml { \ |
1947 | template <> struct ScalarEnumerationTraits<Type> { \ |
1948 | static void enumeration(IO &io, Type &Value); \ |
1949 | }; \ |
1950 | } \ |
1951 | } |
1952 | |
1953 | #define LLVM_YAML_DECLARE_BITSET_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarBitSetTraits <Type> { static void bitset(IO &IO, Type &Options ); }; } } \ |
1954 | namespace llvm { \ |
1955 | namespace yaml { \ |
1956 | template <> struct ScalarBitSetTraits<Type> { \ |
1957 | static void bitset(IO &IO, Type &Options); \ |
1958 | }; \ |
1959 | } \ |
1960 | } |
1961 | |
1962 | #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; } }; } } \ |
1963 | namespace llvm { \ |
1964 | namespace yaml { \ |
1965 | template <> struct ScalarTraits<Type> { \ |
1966 | static void output(const Type &Value, void *ctx, raw_ostream &Out); \ |
1967 | static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \ |
1968 | static QuotingType mustQuote(StringRef) { return MustQuote; } \ |
1969 | }; \ |
1970 | } \ |
1971 | } |
1972 | |
1973 | /// Utility for declaring that a std::vector of a particular type |
1974 | /// should be considered a YAML document list. |
1975 | #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> {}; } } \ |
1976 | namespace llvm { \ |
1977 | namespace yaml { \ |
1978 | template <unsigned N> \ |
1979 | struct DocumentListTraits<SmallVector<_type, N>> \ |
1980 | : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \ |
1981 | template <> \ |
1982 | struct DocumentListTraits<std::vector<_type>> \ |
1983 | : public SequenceTraitsImpl<std::vector<_type>, false> {}; \ |
1984 | } \ |
1985 | } |
1986 | |
1987 | /// Utility for declaring that std::map<std::string, _type> should be considered |
1988 | /// a YAML map. |
1989 | #define LLVM_YAML_IS_STRING_MAP(_type)namespace llvm { namespace yaml { template <> struct CustomMappingTraits <std::map<std::string, _type>> : public StdMapStringCustomMappingTraitsImpl <_type> {}; } } \ |
1990 | namespace llvm { \ |
1991 | namespace yaml { \ |
1992 | template <> \ |
1993 | struct CustomMappingTraits<std::map<std::string, _type>> \ |
1994 | : public StdMapStringCustomMappingTraitsImpl<_type> {}; \ |
1995 | } \ |
1996 | } |
1997 | |
1998 | #endif // LLVM_SUPPORT_YAMLTRAITS_H |