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