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