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