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::bool_constant<
672 !has_ScalarEnumerationTraits<T>::value &&
673 !has_ScalarBitSetTraits<T>::value && !has_ScalarTraits<T>::value &&
674 !has_BlockScalarTraits<T>::value &&
675 !has_TaggedScalarTraits<T>::value &&
676 !has_MappingTraits<T, Context>::value &&
677 !has_SequenceTraits<T>::value && !has_CustomMappingTraits<T>::value &&
678 !has_DocumentListTraits<T>::value &&
679 !has_PolymorphicTraits<T>::value> {};
680
681template <typename T, typename Context>
683 : public std::bool_constant<has_MappingTraits<T, Context>::value &&
684 has_MappingValidateTraits<T, Context>::value> {
685};
686
687template <typename T, typename Context>
689 : public std::bool_constant<has_MappingTraits<T, Context>::value &&
690 !has_MappingValidateTraits<T, Context>::value> {
691};
692
693// Base class for Input and Output.
695public:
696 IO(void *Ctxt = nullptr);
697 virtual ~IO();
698
699 virtual bool outputting() const = 0;
700
701 virtual unsigned beginSequence() = 0;
702 virtual bool preflightElement(unsigned, void *&) = 0;
703 virtual void postflightElement(void *) = 0;
704 virtual void endSequence() = 0;
705 virtual bool canElideEmptySequence() = 0;
706
707 virtual unsigned beginFlowSequence() = 0;
708 virtual bool preflightFlowElement(unsigned, void *&) = 0;
709 virtual void postflightFlowElement(void *) = 0;
710 virtual void endFlowSequence() = 0;
711
712 virtual bool mapTag(StringRef Tag, bool Default = false) = 0;
713 virtual void beginMapping() = 0;
714 virtual void endMapping() = 0;
715 virtual bool preflightKey(const char *, bool, bool, bool &, void *&) = 0;
716 virtual void postflightKey(void *) = 0;
717 virtual std::vector<StringRef> keys() = 0;
718
719 virtual void beginFlowMapping() = 0;
720 virtual void endFlowMapping() = 0;
721
722 virtual void beginEnumScalar() = 0;
723 virtual bool matchEnumScalar(const char *, bool) = 0;
724 virtual bool matchEnumFallback() = 0;
725 virtual void endEnumScalar() = 0;
726
727 virtual bool beginBitSetScalar(bool &) = 0;
728 virtual bool bitSetMatch(const char *, bool) = 0;
729 virtual void endBitSetScalar() = 0;
730
731 virtual void scalarString(StringRef &, QuotingType) = 0;
732 virtual void blockScalarString(StringRef &) = 0;
733 virtual void scalarTag(std::string &) = 0;
734
735 virtual NodeKind getNodeKind() = 0;
736
737 virtual void setError(const Twine &) = 0;
738 virtual std::error_code error() = 0;
739 virtual void setAllowUnknownKeys(bool Allow);
740
741 template <typename T>
742 void enumCase(T &Val, const char *Str, const T ConstVal) {
743 if (matchEnumScalar(Str, outputting() && Val == ConstVal)) {
744 Val = ConstVal;
745 }
746 }
747
748 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
749 template <typename T>
750 void enumCase(T &Val, const char *Str, const uint32_t ConstVal) {
751 if (matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal))) {
752 Val = ConstVal;
753 }
754 }
755
756 template <typename FBT, typename T> void enumFallback(T &Val) {
757 if (matchEnumFallback()) {
758 EmptyContext Context;
759 // FIXME: Force integral conversion to allow strong typedefs to convert.
760 FBT Res = static_cast<typename FBT::BaseType>(Val);
761 yamlize(*this, Res, true, Context);
762 Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res));
763 }
764 }
765
766 template <typename T>
767 void bitSetCase(T &Val, const char *Str, const T ConstVal) {
768 if (bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal)) {
769 Val = static_cast<T>(Val | ConstVal);
770 }
771 }
772
773 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
774 template <typename T>
775 void bitSetCase(T &Val, const char *Str, const uint32_t ConstVal) {
776 if (bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal)) {
777 Val = static_cast<T>(Val | ConstVal);
778 }
779 }
780
781 template <typename T>
782 void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) {
783 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
784 Val = Val | ConstVal;
785 }
786
787 template <typename T>
788 void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal,
789 uint32_t Mask) {
790 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
791 Val = Val | ConstVal;
792 }
793
794 void *getContext() const;
795 void setContext(void *);
796
797 template <typename T> void mapRequired(const char *Key, T &Val) {
798 EmptyContext Ctx;
799 this->processKey(Key, Val, true, Ctx);
800 }
801
802 template <typename T, typename Context>
803 void mapRequired(const char *Key, T &Val, Context &Ctx) {
804 this->processKey(Key, Val, true, Ctx);
805 }
806
807 template <typename T> void mapOptional(const char *Key, T &Val) {
808 EmptyContext Ctx;
809 mapOptionalWithContext(Key, Val, Ctx);
810 }
811
812 template <typename T, typename DefaultT>
813 void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
814 EmptyContext Ctx;
816 }
817
818 template <typename T, typename Context>
819 void mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
820 if constexpr (has_SequenceTraits<T>::value) {
821 // omit key/value instead of outputting empty sequence
822 if (this->canElideEmptySequence() && Val.begin() == Val.end())
823 return;
824 }
825 this->processKey(Key, Val, false, Ctx);
826 }
827
828 template <typename T, typename Context>
829 void mapOptionalWithContext(const char *Key, std::optional<T> &Val,
830 Context &Ctx) {
831 this->processKeyWithDefault(Key, Val, std::optional<T>(),
832 /*Required=*/false, Ctx);
833 }
834
835 template <typename T, typename Context, typename DefaultT>
836 void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
837 Context &Ctx) {
838 static_assert(std::is_convertible<DefaultT, T>::value,
839 "Default type must be implicitly convertible to value type!");
840 this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
841 false, Ctx);
842 }
843
844private:
845 template <typename T, typename Context>
846 void processKeyWithDefault(const char *Key, std::optional<T> &Val,
847 const std::optional<T> &DefaultValue,
848 bool Required, Context &Ctx);
849
850 template <typename T, typename Context>
851 void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue,
852 bool Required, Context &Ctx) {
853 void *SaveInfo;
854 bool UseDefault;
855 const bool sameAsDefault = outputting() && Val == DefaultValue;
856 if (this->preflightKey(Key, Required, sameAsDefault, UseDefault,
857 SaveInfo)) {
858 yamlize(*this, Val, Required, Ctx);
859 this->postflightKey(SaveInfo);
860 } else {
861 if (UseDefault)
862 Val = DefaultValue;
863 }
864 }
865
866 template <typename T, typename Context>
867 void processKey(const char *Key, T &Val, bool Required, Context &Ctx) {
868 void *SaveInfo;
869 bool UseDefault;
870 if (this->preflightKey(Key, Required, false, UseDefault, SaveInfo)) {
871 yamlize(*this, Val, Required, Ctx);
872 this->postflightKey(SaveInfo);
873 }
874 }
875
876private:
877 void *Ctxt;
878};
879
880namespace detail {
881
882template <typename T, typename Context>
883void doMapping(IO &io, T &Val, Context &Ctx) {
885}
886
887template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) {
889}
890
891} // end namespace detail
892
893template <typename T>
894std::enable_if_t<has_ScalarEnumerationTraits<T>::value, void>
895yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
896 io.beginEnumScalar();
898 io.endEnumScalar();
899}
900
901template <typename T>
902std::enable_if_t<has_ScalarBitSetTraits<T>::value, void>
903yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
904 bool DoClear;
905 if (io.beginBitSetScalar(DoClear)) {
906 if (DoClear)
907 Val = T();
909 io.endBitSetScalar();
910 }
911}
912
913template <typename T>
914std::enable_if_t<has_ScalarTraits<T>::value, void> yamlize(IO &io, T &Val, bool,
915 EmptyContext &Ctx) {
916 if (io.outputting()) {
917 SmallString<128> Storage;
918 raw_svector_ostream Buffer(Storage);
919 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
920 StringRef Str = Buffer.str();
922 } else {
923 StringRef Str;
925 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
926 if (!Result.empty()) {
927 io.setError(Twine(Result));
928 }
929 }
930}
931
932template <typename T>
933std::enable_if_t<has_BlockScalarTraits<T>::value, void>
934yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) {
935 if (YamlIO.outputting()) {
936 std::string Storage;
937 raw_string_ostream Buffer(Storage);
938 BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
939 StringRef Str(Storage);
940 YamlIO.blockScalarString(Str);
941 } else {
942 StringRef Str;
943 YamlIO.blockScalarString(Str);
944 StringRef Result =
945 BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val);
946 if (!Result.empty())
947 YamlIO.setError(Twine(Result));
948 }
949}
950
951template <typename T>
952std::enable_if_t<has_TaggedScalarTraits<T>::value, void>
953yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
954 if (io.outputting()) {
955 std::string ScalarStorage, TagStorage;
956 raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage);
957 TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer,
958 TagBuffer);
959 io.scalarTag(TagStorage);
960 StringRef ScalarStr(ScalarStorage);
961 io.scalarString(ScalarStr,
962 TaggedScalarTraits<T>::mustQuote(Val, ScalarStr));
963 } else {
964 std::string Tag;
965 io.scalarTag(Tag);
966 StringRef Str;
968 StringRef Result =
970 if (!Result.empty()) {
971 io.setError(Twine(Result));
972 }
973 }
974}
975
976namespace detail {
977
978template <typename T, typename Context>
979std::string doValidate(IO &io, T &Val, Context &Ctx) {
981}
982
983template <typename T> std::string doValidate(IO &io, T &Val, EmptyContext &) {
984 return MappingTraits<T>::validate(io, Val);
985}
986
987} // namespace detail
988
989template <typename T, typename Context>
990std::enable_if_t<validatedMappingTraits<T, Context>::value, void>
991yamlize(IO &io, T &Val, bool, Context &Ctx) {
993 io.beginFlowMapping();
994 else
995 io.beginMapping();
996 if (io.outputting()) {
997 std::string Err = detail::doValidate(io, Val, Ctx);
998 if (!Err.empty()) {
999 errs() << Err << "\n";
1000 assert(Err.empty() && "invalid struct trying to be written as yaml");
1001 }
1002 }
1003 detail::doMapping(io, Val, Ctx);
1004 if (!io.outputting()) {
1005 std::string Err = detail::doValidate(io, Val, Ctx);
1006 if (!Err.empty())
1007 io.setError(Err);
1008 }
1009 if (has_FlowTraits<MappingTraits<T>>::value)
1010 io.endFlowMapping();
1011 else
1012 io.endMapping();
1013}
1014
1015template <typename T, typename Context>
1018 if (io.outputting())
1019 return false;
1020
1021 io.beginEnumScalar();
1023 bool Matched = !io.matchEnumFallback();
1024 io.endEnumScalar();
1025 return Matched;
1026 }
1027 return false;
1028}
1029
1030template <typename T, typename Context>
1031std::enable_if_t<unvalidatedMappingTraits<T, Context>::value, void>
1032yamlize(IO &io, T &Val, bool, Context &Ctx) {
1034 return;
1035 if (has_FlowTraits<MappingTraits<T>>::value) {
1036 io.beginFlowMapping();
1037 detail::doMapping(io, Val, Ctx);
1038 io.endFlowMapping();
1039 } else {
1040 io.beginMapping();
1041 detail::doMapping(io, Val, Ctx);
1042 io.endMapping();
1043 }
1044}
1045
1046template <typename T>
1047std::enable_if_t<has_CustomMappingTraits<T>::value, void>
1048yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1049 if (io.outputting()) {
1050 io.beginMapping();
1052 io.endMapping();
1053 } else {
1054 io.beginMapping();
1055 for (StringRef key : io.keys())
1057 io.endMapping();
1058 }
1059}
1060
1061template <typename T>
1062std::enable_if_t<has_PolymorphicTraits<T>::value, void>
1063yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1064 switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val)
1065 : io.getNodeKind()) {
1066 case NodeKind::Scalar:
1067 return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val), true, Ctx);
1068 case NodeKind::Map:
1069 return yamlize(io, PolymorphicTraits<T>::getAsMap(Val), true, Ctx);
1070 case NodeKind::Sequence:
1071 return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val), true, Ctx);
1072 }
1073}
1074
1075template <typename T>
1076std::enable_if_t<missingTraits<T, EmptyContext>::value, void>
1077yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1078 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1079}
1080
1081template <typename T, typename Context>
1082std::enable_if_t<has_SequenceTraits<T>::value, void>
1083yamlize(IO &io, T &Seq, bool, Context &Ctx) {
1084 if (has_FlowTraits<SequenceTraits<T>>::value) {
1085 unsigned incnt = io.beginFlowSequence();
1086 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
1087 for (unsigned i = 0; i < count; ++i) {
1088 void *SaveInfo;
1089 if (io.preflightFlowElement(i, SaveInfo)) {
1090 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
1091 io.postflightFlowElement(SaveInfo);
1092 }
1093 }
1094 io.endFlowSequence();
1095 } else {
1096 unsigned incnt = io.beginSequence();
1097 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
1098 for (unsigned i = 0; i < count; ++i) {
1099 void *SaveInfo;
1100 if (io.preflightElement(i, SaveInfo)) {
1101 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
1102 io.postflightElement(SaveInfo);
1103 }
1104 }
1105 io.endSequence();
1106 }
1107}
1108
1109template <> struct ScalarTraits<bool> {
1110 LLVM_ABI static void output(const bool &, void *, raw_ostream &);
1111 LLVM_ABI static StringRef input(StringRef, void *, bool &);
1113};
1114
1115template <> struct ScalarTraits<StringRef> {
1116 LLVM_ABI static void output(const StringRef &, void *, raw_ostream &);
1119};
1120
1121template <> struct ScalarTraits<std::string> {
1122 LLVM_ABI static void output(const std::string &, void *, raw_ostream &);
1123 LLVM_ABI static StringRef input(StringRef, void *, std::string &);
1125};
1126
1127template <> struct ScalarTraits<uint8_t> {
1128 LLVM_ABI static void output(const uint8_t &, void *, raw_ostream &);
1131};
1132
1133template <> struct ScalarTraits<uint16_t> {
1134 LLVM_ABI static void output(const uint16_t &, void *, raw_ostream &);
1137};
1138
1139template <> struct ScalarTraits<uint32_t> {
1140 LLVM_ABI static void output(const uint32_t &, void *, raw_ostream &);
1143};
1144
1145template <> struct ScalarTraits<uint64_t> {
1146 LLVM_ABI static void output(const uint64_t &, void *, raw_ostream &);
1149};
1150
1151template <> struct ScalarTraits<int8_t> {
1152 LLVM_ABI static void output(const int8_t &, void *, raw_ostream &);
1153 LLVM_ABI static StringRef input(StringRef, void *, int8_t &);
1155};
1156
1157template <> struct ScalarTraits<int16_t> {
1158 LLVM_ABI static void output(const int16_t &, void *, raw_ostream &);
1159 LLVM_ABI static StringRef input(StringRef, void *, int16_t &);
1161};
1162
1163template <> struct ScalarTraits<int32_t> {
1164 LLVM_ABI static void output(const int32_t &, void *, raw_ostream &);
1165 LLVM_ABI static StringRef input(StringRef, void *, int32_t &);
1167};
1168
1169template <> struct ScalarTraits<int64_t> {
1170 LLVM_ABI static void output(const int64_t &, void *, raw_ostream &);
1171 LLVM_ABI static StringRef input(StringRef, void *, int64_t &);
1173};
1174
1175template <> struct ScalarTraits<float> {
1176 LLVM_ABI static void output(const float &, void *, raw_ostream &);
1177 LLVM_ABI static StringRef input(StringRef, void *, float &);
1179};
1180
1181template <> struct ScalarTraits<double> {
1182 LLVM_ABI static void output(const double &, void *, raw_ostream &);
1183 LLVM_ABI static StringRef input(StringRef, void *, double &);
1185};
1186
1187// For endian types, we use existing scalar Traits class for the underlying
1188// type. This way endian aware types are supported whenever the traits are
1189// defined for the underlying type.
1190template <typename value_type, llvm::endianness endian, size_t alignment>
1191struct ScalarTraits<support::detail::packed_endian_specific_integral<
1192 value_type, endian, alignment>,
1193 std::enable_if_t<has_ScalarTraits<value_type>::value>> {
1196 alignment>;
1197
1198 static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) {
1199 ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream);
1200 }
1201
1202 static StringRef input(StringRef Str, void *Ctx, endian_type &E) {
1203 value_type V;
1204 auto R = ScalarTraits<value_type>::input(Str, Ctx, V);
1205 E = static_cast<endian_type>(V);
1206 return R;
1207 }
1208
1212};
1213
1214template <typename value_type, llvm::endianness endian, size_t alignment>
1216 support::detail::packed_endian_specific_integral<value_type, endian,
1217 alignment>,
1218 std::enable_if_t<has_ScalarEnumerationTraits<value_type>::value>> {
1221 alignment>;
1222
1223 static void enumeration(IO &io, endian_type &E) {
1224 value_type V = E;
1226 E = V;
1227 }
1228};
1229
1230template <typename value_type, llvm::endianness endian, size_t alignment>
1232 support::detail::packed_endian_specific_integral<value_type, endian,
1233 alignment>,
1234 std::enable_if_t<has_ScalarBitSetTraits<value_type>::value>> {
1237 alignment>;
1238 static void bitset(IO &io, endian_type &E) {
1239 value_type V = E;
1241 E = V;
1242 }
1243};
1244
1245// Utility for use within MappingTraits<>::mapping() method
1246// to [de]normalize an object for use with YAML conversion.
1247template <typename TNorm, typename TFinal> struct MappingNormalization {
1248 MappingNormalization(IO &i_o, TFinal &Obj)
1249 : io(i_o), BufPtr(nullptr), Result(Obj) {
1250 if (io.outputting()) {
1251 BufPtr = new (&Buffer) TNorm(io, Obj);
1252 } else {
1253 BufPtr = new (&Buffer) TNorm(io);
1254 }
1255 }
1256
1258 if (!io.outputting()) {
1259 Result = BufPtr->denormalize(io);
1260 }
1261 BufPtr->~TNorm();
1262 }
1263
1264 TNorm *operator->() { return BufPtr; }
1265
1266private:
1267 using Storage = AlignedCharArrayUnion<TNorm>;
1268
1269 Storage Buffer;
1270 IO &io;
1271 TNorm *BufPtr;
1272 TFinal &Result;
1273};
1274
1275// Utility for use within MappingTraits<>::mapping() method
1276// to [de]normalize an object for use with YAML conversion.
1277template <typename TNorm, typename TFinal> struct MappingNormalizationHeap {
1279 : io(i_o), Result(Obj) {
1280 if (io.outputting()) {
1281 BufPtr = new (&Buffer) TNorm(io, Obj);
1282 } else if (allocator) {
1283 BufPtr = allocator->Allocate<TNorm>();
1284 new (BufPtr) TNorm(io);
1285 } else {
1286 BufPtr = new TNorm(io);
1287 }
1288 }
1289
1291 if (io.outputting()) {
1292 BufPtr->~TNorm();
1293 } else {
1294 Result = BufPtr->denormalize(io);
1295 }
1296 }
1297
1298 TNorm *operator->() { return BufPtr; }
1299
1300private:
1301 using Storage = AlignedCharArrayUnion<TNorm>;
1302
1303 Storage Buffer;
1304 IO &io;
1305 TNorm *BufPtr = nullptr;
1306 TFinal &Result;
1307};
1308
1309///
1310/// The Input class is used to parse a yaml document into in-memory structs
1311/// and vectors.
1312///
1313/// It works by using YAMLParser to do a syntax parse of the entire yaml
1314/// document, then the Input class builds a graph of HNodes which wraps
1315/// each yaml Node. The extra layer is buffering. The low level yaml
1316/// parser only lets you look at each node once. The buffering layer lets
1317/// you search and interate multiple times. This is necessary because
1318/// the mapRequired() method calls may not be in the same order
1319/// as the keys in the document.
1320///
1321class LLVM_ABI Input : public IO {
1322public:
1323 // Construct a yaml Input object from a StringRef and optional
1324 // user-data. The DiagHandler can be specified to provide
1325 // alternative error reporting.
1326 Input(StringRef InputContent, void *Ctxt = nullptr,
1328 void *DiagHandlerCtxt = nullptr);
1329 Input(MemoryBufferRef Input, void *Ctxt = nullptr,
1331 void *DiagHandlerCtxt = nullptr);
1332 ~Input() override;
1333
1334 // Check if there was an syntax or semantic error during parsing.
1335 std::error_code error() override;
1336
1337private:
1338 bool outputting() const override;
1339 bool mapTag(StringRef, bool) override;
1340 void beginMapping() override;
1341 void endMapping() override;
1342 bool preflightKey(const char *, bool, bool, bool &, void *&) override;
1343 void postflightKey(void *) override;
1344 std::vector<StringRef> keys() override;
1345 void beginFlowMapping() override;
1346 void endFlowMapping() override;
1347 unsigned beginSequence() override;
1348 void endSequence() override;
1349 bool preflightElement(unsigned index, void *&) override;
1350 void postflightElement(void *) override;
1351 unsigned beginFlowSequence() override;
1352 bool preflightFlowElement(unsigned, void *&) override;
1353 void postflightFlowElement(void *) override;
1354 void endFlowSequence() override;
1355 void beginEnumScalar() override;
1356 bool matchEnumScalar(const char *, bool) override;
1357 bool matchEnumFallback() override;
1358 void endEnumScalar() override;
1359 bool beginBitSetScalar(bool &) override;
1360 bool bitSetMatch(const char *, bool) override;
1361 void endBitSetScalar() override;
1362 void scalarString(StringRef &, QuotingType) override;
1363 void blockScalarString(StringRef &) override;
1364 void scalarTag(std::string &) override;
1365 NodeKind getNodeKind() override;
1366 void setError(const Twine &message) override;
1367 bool canElideEmptySequence() override;
1368
1369 class HNode {
1370 public:
1371 HNode(Node *n) : _node(n) {}
1372
1373 static bool classof(const HNode *) { return true; }
1374
1375 Node *_node;
1376 };
1377
1378 class EmptyHNode : public HNode {
1379 public:
1380 EmptyHNode(Node *n) : HNode(n) {}
1381
1382 static bool classof(const HNode *n) { return NullNode::classof(n->_node); }
1383
1384 static bool classof(const EmptyHNode *) { return true; }
1385 };
1386
1387 class ScalarHNode : public HNode {
1388 public:
1389 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) {}
1390
1391 StringRef value() const { return _value; }
1392
1393 static bool classof(const HNode *n) {
1394 return ScalarNode::classof(n->_node) ||
1395 BlockScalarNode::classof(n->_node);
1396 }
1397
1398 static bool classof(const ScalarHNode *) { return true; }
1399
1400 protected:
1401 StringRef _value;
1402 };
1403
1404 class MapHNode : public HNode {
1405 public:
1406 MapHNode(Node *n) : HNode(n) {}
1407
1408 static bool classof(const HNode *n) {
1409 return MappingNode::classof(n->_node);
1410 }
1411
1412 static bool classof(const MapHNode *) { return true; }
1413
1414 using NameToNodeAndLoc = StringMap<std::pair<HNode *, SMRange>>;
1415
1416 NameToNodeAndLoc Mapping;
1417 SmallVector<std::string, 6> ValidKeys;
1418 };
1419
1420 class SequenceHNode : public HNode {
1421 public:
1422 SequenceHNode(Node *n) : HNode(n) {}
1423
1424 static bool classof(const HNode *n) {
1425 return SequenceNode::classof(n->_node);
1426 }
1427
1428 static bool classof(const SequenceHNode *) { return true; }
1429
1430 std::vector<HNode *> Entries;
1431 };
1432
1433 Input::HNode *createHNodes(Node *node);
1434 void setError(HNode *hnode, const Twine &message);
1435 void setError(Node *node, const Twine &message);
1436 void setError(const SMRange &Range, const Twine &message);
1437
1438 void reportWarning(HNode *hnode, const Twine &message);
1439 void reportWarning(Node *hnode, const Twine &message);
1440 void reportWarning(const SMRange &Range, const Twine &message);
1441
1442 /// Release memory used by HNodes.
1443 void releaseHNodeBuffers();
1444
1445public:
1446 // These are only used by operator>>. They could be private
1447 // if those templated things could be made friends.
1448 bool setCurrentDocument();
1449 bool nextDocument();
1450
1451 /// Returns the current node that's being parsed by the YAML Parser.
1452 const Node *getCurrentNode() const;
1453
1454 void setAllowUnknownKeys(bool Allow) override;
1455
1456private:
1457 SourceMgr SrcMgr; // must be before Strm
1458 std::unique_ptr<llvm::yaml::Stream> Strm;
1459 HNode *TopNode = nullptr;
1460 std::error_code EC;
1461 BumpPtrAllocator StringAllocator;
1462 SpecificBumpPtrAllocator<EmptyHNode> EmptyHNodeAllocator;
1463 SpecificBumpPtrAllocator<ScalarHNode> ScalarHNodeAllocator;
1464 SpecificBumpPtrAllocator<MapHNode> MapHNodeAllocator;
1465 SpecificBumpPtrAllocator<SequenceHNode> SequenceHNodeAllocator;
1466 document_iterator DocIterator;
1467 llvm::BitVector BitValuesUsed;
1468 HNode *CurrentNode = nullptr;
1469 bool ScalarMatchFound = false;
1470 bool AllowUnknownKeys = false;
1471};
1472
1473///
1474/// The Output class is used to generate a yaml document from in-memory structs
1475/// and vectors.
1476///
1477class LLVM_ABI Output : public IO {
1478public:
1479 Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
1480 ~Output() override;
1481
1482 /// Set whether or not to output optional values which are equal
1483 /// to the default value. By default, when outputting if you attempt
1484 /// to write a value that is equal to the default, the value gets ignored.
1485 /// Sometimes, it is useful to be able to see these in the resulting YAML
1486 /// anyway.
1487 void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; }
1488
1489 bool outputting() const override;
1490 bool mapTag(StringRef, bool) override;
1491 void beginMapping() override;
1492 void endMapping() override;
1493 bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
1494 void postflightKey(void *) override;
1495 std::vector<StringRef> keys() override;
1496 void beginFlowMapping() override;
1497 void endFlowMapping() override;
1498 unsigned beginSequence() override;
1499 void endSequence() override;
1500 bool preflightElement(unsigned, void *&) override;
1501 void postflightElement(void *) override;
1502 unsigned beginFlowSequence() override;
1503 bool preflightFlowElement(unsigned, void *&) override;
1504 void postflightFlowElement(void *) override;
1505 void endFlowSequence() override;
1506 void beginEnumScalar() override;
1507 bool matchEnumScalar(const char *, bool) override;
1508 bool matchEnumFallback() override;
1509 void endEnumScalar() override;
1510 bool beginBitSetScalar(bool &) override;
1511 bool bitSetMatch(const char *, bool) override;
1512 void endBitSetScalar() override;
1513 void scalarString(StringRef &, QuotingType) override;
1514 void blockScalarString(StringRef &) override;
1515 void scalarTag(std::string &) override;
1516 NodeKind getNodeKind() override;
1517 void setError(const Twine &message) override;
1518 std::error_code error() override;
1519 bool canElideEmptySequence() override;
1520
1521 // These are only used by operator<<. They could be private
1522 // if that templated operator could be made a friend.
1523 void beginDocuments();
1524 bool preflightDocument(unsigned);
1525 void postflightDocument();
1526 void endDocuments();
1527
1528private:
1529 void output(StringRef s);
1530 void output(StringRef, QuotingType);
1531 void outputUpToEndOfLine(StringRef s);
1532 void newLineCheck(bool EmptySequence = false);
1533 void outputNewLine();
1534 void paddedKey(StringRef key);
1535 void flowKey(StringRef Key);
1536
1537 enum InState {
1538 inSeqFirstElement,
1539 inSeqOtherElement,
1540 inFlowSeqFirstElement,
1541 inFlowSeqOtherElement,
1542 inMapFirstKey,
1543 inMapOtherKey,
1544 inFlowMapFirstKey,
1545 inFlowMapOtherKey
1546 };
1547
1548 static bool inSeqAnyElement(InState State);
1549 static bool inFlowSeqAnyElement(InState State);
1550 static bool inMapAnyKey(InState State);
1551 static bool inFlowMapAnyKey(InState State);
1552
1553 raw_ostream &Out;
1554 int WrapColumn;
1555 SmallVector<InState, 8> StateStack;
1556 int Column = 0;
1557 int ColumnAtFlowStart = 0;
1558 int ColumnAtMapFlowStart = 0;
1559 bool NeedBitValueComma = false;
1560 bool NeedFlowSequenceComma = false;
1561 bool EnumerationMatchFound = false;
1562 bool WriteDefaultValues = false;
1563 StringRef Padding;
1564 StringRef PaddingBeforeContainer;
1565};
1566
1567template <typename T, typename Context>
1568void IO::processKeyWithDefault(const char *Key, std::optional<T> &Val,
1569 const std::optional<T> &DefaultValue,
1570 bool Required, Context &Ctx) {
1571 assert(!DefaultValue && "std::optional<T> shouldn't have a value!");
1572 void *SaveInfo;
1573 bool UseDefault = true;
1574 const bool sameAsDefault = outputting() && !Val;
1575 if (!outputting() && !Val)
1576 Val = T();
1577 if (Val &&
1578 this->preflightKey(Key, Required, sameAsDefault, UseDefault, SaveInfo)) {
1579
1580 // When reading an std::optional<X> key from a YAML description, we allow
1581 // the special "<none>" value, which can be used to specify that no value
1582 // was requested, i.e. the DefaultValue will be assigned. The DefaultValue
1583 // is usually None.
1584 bool IsNone = false;
1585 if (!outputting())
1586 if (const auto *Node =
1587 dyn_cast<ScalarNode>(((Input *)this)->getCurrentNode()))
1588 // We use rtrim to ignore possible white spaces that might exist when a
1589 // comment is present on the same line.
1590 IsNone = Node->getRawValue().rtrim(' ') == "<none>";
1591
1592 if (IsNone)
1593 Val = DefaultValue;
1594 else
1595 yamlize(*this, *Val, Required, Ctx);
1596 this->postflightKey(SaveInfo);
1597 } else {
1598 if (UseDefault)
1599 Val = DefaultValue;
1600 }
1601}
1602
1603/// YAML I/O does conversion based on types. But often native data types
1604/// are just a typedef of built in intergral types (e.g. int). But the C++
1605/// type matching system sees through the typedef and all the typedefed types
1606/// look like a built in type. This will cause the generic YAML I/O conversion
1607/// to be used. To provide better control over the YAML conversion, you can
1608/// use this macro instead of typedef. It will create a class with one field
1609/// and automatic conversion operators to and from the base type.
1610/// Based on BOOST_STRONG_TYPEDEF
1611#define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
1612 struct _type { \
1613 _type() = default; \
1614 _type(const _base v) : value(v) {} \
1615 _type(const _type &v) = default; \
1616 _type &operator=(const _type &rhs) = default; \
1617 _type &operator=(const _base &rhs) { \
1618 value = rhs; \
1619 return *this; \
1620 } \
1621 operator const _base &() const { return value; } \
1622 bool operator==(const _type &rhs) const { return value == rhs.value; } \
1623 bool operator==(const _base &rhs) const { return value == rhs; } \
1624 bool operator<(const _type &rhs) const { return value < rhs.value; } \
1625 _base value; \
1626 using BaseType = _base; \
1627 };
1628
1629///
1630/// Use these types instead of uintXX_t in any mapping to have
1631/// its yaml output formatted as hexadecimal.
1632///
1637
1638template <> struct ScalarTraits<Hex8> {
1639 LLVM_ABI static void output(const Hex8 &, void *, raw_ostream &);
1640 LLVM_ABI static StringRef input(StringRef, void *, Hex8 &);
1642};
1643
1644template <> struct ScalarTraits<Hex16> {
1645 LLVM_ABI static void output(const Hex16 &, void *, raw_ostream &);
1646 LLVM_ABI static StringRef input(StringRef, void *, Hex16 &);
1648};
1649
1650template <> struct ScalarTraits<Hex32> {
1651 LLVM_ABI static void output(const Hex32 &, void *, raw_ostream &);
1652 LLVM_ABI static StringRef input(StringRef, void *, Hex32 &);
1654};
1655
1656template <> struct ScalarTraits<Hex64> {
1657 LLVM_ABI static void output(const Hex64 &, void *, raw_ostream &);
1658 LLVM_ABI static StringRef input(StringRef, void *, Hex64 &);
1660};
1661
1662template <> struct ScalarTraits<VersionTuple> {
1663 LLVM_ABI static void output(const VersionTuple &Value, void *,
1664 llvm::raw_ostream &Out);
1667};
1668
1669// Define non-member operator>> so that Input can stream in a document list.
1670template <typename T>
1671inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &>
1672operator>>(Input &yin, T &docList) {
1673 int i = 0;
1674 EmptyContext Ctx;
1675 while (yin.setCurrentDocument()) {
1676 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx);
1677 if (yin.error())
1678 return yin;
1679 yin.nextDocument();
1680 ++i;
1681 }
1682 return yin;
1683}
1684
1685// Define non-member operator>> so that Input can stream in a map as a document.
1686template <typename T>
1687inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Input &>
1688operator>>(Input &yin, T &docMap) {
1689 EmptyContext Ctx;
1690 yin.setCurrentDocument();
1691 yamlize(yin, docMap, true, Ctx);
1692 return yin;
1693}
1694
1695// Define non-member operator>> so that Input can stream in a sequence as
1696// a document.
1697template <typename T>
1698inline std::enable_if_t<has_SequenceTraits<T>::value, Input &>
1699operator>>(Input &yin, T &docSeq) {
1700 EmptyContext Ctx;
1701 if (yin.setCurrentDocument())
1702 yamlize(yin, docSeq, true, Ctx);
1703 return yin;
1704}
1705
1706// Define non-member operator>> so that Input can stream in a block scalar.
1707template <typename T>
1708inline std::enable_if_t<has_BlockScalarTraits<T>::value, Input &>
1709operator>>(Input &In, T &Val) {
1710 EmptyContext Ctx;
1711 if (In.setCurrentDocument())
1712 yamlize(In, Val, true, Ctx);
1713 return In;
1714}
1715
1716// Define non-member operator>> so that Input can stream in a string map.
1717template <typename T>
1718inline std::enable_if_t<has_CustomMappingTraits<T>::value, Input &>
1719operator>>(Input &In, T &Val) {
1720 EmptyContext Ctx;
1721 if (In.setCurrentDocument())
1722 yamlize(In, Val, true, Ctx);
1723 return In;
1724}
1725
1726// Define non-member operator>> so that Input can stream in a polymorphic type.
1727template <typename T>
1728inline std::enable_if_t<has_PolymorphicTraits<T>::value, Input &>
1729operator>>(Input &In, T &Val) {
1730 EmptyContext Ctx;
1731 if (In.setCurrentDocument())
1732 yamlize(In, Val, true, Ctx);
1733 return In;
1734}
1735
1736// Provide better error message about types missing a trait specialization
1737template <typename T>
1738inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Input &>
1739operator>>(Input &yin, T &docSeq) {
1740 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1741 return yin;
1742}
1743
1744// Define non-member operator<< so that Output can stream out document list.
1745template <typename T>
1746inline std::enable_if_t<has_DocumentListTraits<T>::value, Output &>
1747operator<<(Output &yout, T &docList) {
1748 EmptyContext Ctx;
1749 yout.beginDocuments();
1750 const size_t count = DocumentListTraits<T>::size(yout, docList);
1751 for (size_t i = 0; i < count; ++i) {
1752 if (yout.preflightDocument(i)) {
1753 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true,
1754 Ctx);
1755 yout.postflightDocument();
1756 }
1757 }
1758 yout.endDocuments();
1759 return yout;
1760}
1761
1762// Define non-member operator<< so that Output can stream out a map.
1763template <typename T>
1764inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Output &>
1765operator<<(Output &yout, T &map) {
1766 EmptyContext Ctx;
1767 yout.beginDocuments();
1768 if (yout.preflightDocument(0)) {
1769 yamlize(yout, map, true, Ctx);
1770 yout.postflightDocument();
1771 }
1772 yout.endDocuments();
1773 return yout;
1774}
1775
1776// Define non-member operator<< so that Output can stream out a sequence.
1777template <typename T>
1778inline std::enable_if_t<has_SequenceTraits<T>::value, Output &>
1779operator<<(Output &yout, T &seq) {
1780 EmptyContext Ctx;
1781 yout.beginDocuments();
1782 if (yout.preflightDocument(0)) {
1783 yamlize(yout, seq, true, Ctx);
1784 yout.postflightDocument();
1785 }
1786 yout.endDocuments();
1787 return yout;
1788}
1789
1790// Define non-member operator<< so that Output can stream out a block scalar.
1791template <typename T>
1792inline std::enable_if_t<has_BlockScalarTraits<T>::value, Output &>
1793operator<<(Output &Out, T &Val) {
1794 EmptyContext Ctx;
1795 Out.beginDocuments();
1796 if (Out.preflightDocument(0)) {
1797 yamlize(Out, Val, true, Ctx);
1798 Out.postflightDocument();
1799 }
1800 Out.endDocuments();
1801 return Out;
1802}
1803
1804// Define non-member operator<< so that Output can stream out a string map.
1805template <typename T>
1806inline std::enable_if_t<has_CustomMappingTraits<T>::value, Output &>
1807operator<<(Output &Out, T &Val) {
1808 EmptyContext Ctx;
1809 Out.beginDocuments();
1810 if (Out.preflightDocument(0)) {
1811 yamlize(Out, Val, true, Ctx);
1812 Out.postflightDocument();
1813 }
1814 Out.endDocuments();
1815 return Out;
1816}
1817
1818// Define non-member operator<< so that Output can stream out a polymorphic
1819// type.
1820template <typename T>
1821inline std::enable_if_t<has_PolymorphicTraits<T>::value, Output &>
1822operator<<(Output &Out, T &Val) {
1823 EmptyContext Ctx;
1824 Out.beginDocuments();
1825 if (Out.preflightDocument(0)) {
1826 // FIXME: The parser does not support explicit documents terminated with a
1827 // plain scalar; the end-marker is included as part of the scalar token.
1829 "plain scalar documents are not supported");
1830 yamlize(Out, Val, true, Ctx);
1831 Out.postflightDocument();
1832 }
1833 Out.endDocuments();
1834 return Out;
1835}
1836
1837// Provide better error message about types missing a trait specialization
1838template <typename T>
1839inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Output &>
1840operator<<(Output &yout, T &seq) {
1841 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1842 return yout;
1843}
1844
1845template <bool B> struct IsFlowSequenceBase {};
1846template <> struct IsFlowSequenceBase<true> {
1847 static const bool flow = true;
1848};
1849
1850template <typename T>
1851using check_resize_t = decltype(std::declval<T>().resize(0));
1852
1853template <typename T> struct IsResizableBase {
1854 using type = typename T::value_type;
1855
1856 static type &element(IO &io, T &seq, size_t index) {
1858 if (index >= seq.size())
1859 seq.resize(index + 1);
1860 } else {
1861 if (index >= seq.size()) {
1862 io.setError(Twine("value sequence extends beyond static size (") +
1863 Twine(seq.size()) + ")");
1864 return seq[0];
1865 }
1866 }
1867 return seq[index];
1868 }
1869};
1870
1871template <typename T, bool Flow>
1873 static size_t size(IO &io, T &seq) { return seq.size(); }
1874};
1875
1876// Simple helper to check an expression can be used as a bool-valued template
1877// argument.
1878template <bool> struct CheckIsBool {
1879 static const bool value = true;
1880};
1881
1882// If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have
1883// SequenceTraits that do the obvious thing.
1884template <typename T>
1886 std::vector<T>,
1887 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1888 : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
1889template <typename T, size_t N>
1891 std::array<T, N>,
1892 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1893 : SequenceTraitsImpl<std::array<T, N>, SequenceElementTraits<T>::flow> {};
1894template <typename T, unsigned N>
1896 SmallVector<T, N>,
1897 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1898 : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
1899template <typename T>
1902 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1903 : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {};
1904template <typename T>
1907 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1908 : SequenceTraitsImpl<MutableArrayRef<T>, SequenceElementTraits<T>::flow> {};
1909
1910// Sequences of fundamental types use flow formatting.
1911template <typename T>
1912struct SequenceElementTraits<T, std::enable_if_t<std::is_fundamental_v<T>>> {
1913 static const bool flow = true;
1914};
1915
1916// Sequences of strings use block formatting.
1917template <> struct SequenceElementTraits<std::string> {
1918 static const bool flow = false;
1919};
1921 static const bool flow = false;
1922};
1923template <> struct SequenceElementTraits<std::pair<std::string, std::string>> {
1924 static const bool flow = false;
1925};
1926
1927/// Implementation of CustomMappingTraits for std::map<std::string, T>.
1928template <typename T> struct StdMapStringCustomMappingTraitsImpl {
1929 using map_type = std::map<std::string, T>;
1930
1931 static void inputOne(IO &io, StringRef key, map_type &v) {
1932 io.mapRequired(key.str().c_str(), v[std::string(key)]);
1933 }
1934
1935 static void output(IO &io, map_type &v) {
1936 for (auto &p : v)
1937 io.mapRequired(p.first.c_str(), p.second);
1938 }
1939};
1940
1941} // end namespace yaml
1942} // end namespace llvm
1943
1944#define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW) \
1945 namespace llvm { \
1946 namespace yaml { \
1947 static_assert( \
1948 !std::is_fundamental_v<TYPE> && !std::is_same_v<TYPE, std::string> && \
1949 !std::is_same_v<TYPE, llvm::StringRef>, \
1950 "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \
1951 template <> struct SequenceElementTraits<TYPE> { \
1952 static const bool flow = FLOW; \
1953 }; \
1954 } \
1955 }
1956
1957/// Utility for declaring that a std::vector of a particular type
1958/// should be considered a YAML sequence.
1959#define LLVM_YAML_IS_SEQUENCE_VECTOR(type) \
1960 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false)
1961
1962/// Utility for declaring that a std::vector of a particular type
1963/// should be considered a YAML flow sequence.
1964#define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type) \
1965 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true)
1966
1967#define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type) \
1968 namespace llvm { \
1969 namespace yaml { \
1970 template <> struct LLVM_ABI MappingTraits<Type> { \
1971 static void mapping(IO &IO, Type &Obj); \
1972 }; \
1973 } \
1974 }
1975
1976#define LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(Type) \
1977 namespace llvm { \
1978 namespace yaml { \
1979 template <> struct MappingTraits<Type> { \
1980 static void mapping(IO &IO, Type &Obj); \
1981 }; \
1982 } \
1983 }
1984
1985#define LLVM_YAML_DECLARE_ENUM_TRAITS(Type) \
1986 namespace llvm { \
1987 namespace yaml { \
1988 template <> struct LLVM_ABI ScalarEnumerationTraits<Type> { \
1989 static void enumeration(IO &io, Type &Value); \
1990 }; \
1991 } \
1992 }
1993
1994#define LLVM_YAML_DECLARE_BITSET_TRAITS(Type) \
1995 namespace llvm { \
1996 namespace yaml { \
1997 template <> struct LLVM_ABI ScalarBitSetTraits<Type> { \
1998 static void bitset(IO &IO, Type &Options); \
1999 }; \
2000 } \
2001 }
2002
2003#define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote) \
2004 namespace llvm { \
2005 namespace yaml { \
2006 template <> struct LLVM_ABI ScalarTraits<Type> { \
2007 static void output(const Type &Value, void *ctx, raw_ostream &Out); \
2008 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
2009 static QuotingType mustQuote(StringRef) { return MustQuote; } \
2010 }; \
2011 } \
2012 }
2013
2014/// Utility for declaring that a std::vector of a particular type
2015/// should be considered a YAML document list.
2016#define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
2017 namespace llvm { \
2018 namespace yaml { \
2019 template <unsigned N> \
2020 struct DocumentListTraits<SmallVector<_type, N>> \
2021 : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \
2022 template <> \
2023 struct DocumentListTraits<std::vector<_type>> \
2024 : public SequenceTraitsImpl<std::vector<_type>, false> {}; \
2025 } \
2026 }
2027
2028/// Utility for declaring that std::map<std::string, _type> should be considered
2029/// a YAML map.
2030#define LLVM_YAML_IS_STRING_MAP(_type) \
2031 namespace llvm { \
2032 namespace yaml { \
2033 template <> \
2034 struct CustomMappingTraits<std::map<std::string, _type>> \
2035 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \
2036 } \
2037 }
2038
2039LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)
2040LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)
2041LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)
2042LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)
2043
2044#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:782
virtual void endSequence()=0
void bitSetCase(T &Val, const char *Str, const uint32_t ConstVal)
Definition YAMLTraits.h:775
void mapOptional(const char *Key, T &Val)
Definition YAMLTraits.h:807
virtual void endEnumScalar()=0
void bitSetCase(T &Val, const char *Str, const T ConstVal)
Definition YAMLTraits.h:767
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:803
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:788
virtual void endMapping()=0
virtual bool preflightElement(unsigned, void *&)=0
virtual unsigned beginSequence()=0
void mapRequired(const char *Key, T &Val)
Definition YAMLTraits.h:797
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:750
void mapOptionalWithContext(const char *Key, T &Val, Context &Ctx)
Definition YAMLTraits.h:819
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:756
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:742
virtual bool preflightFlowElement(unsigned, void *&)=0
virtual void endBitSetScalar()=0
void mapOptionalWithContext(const char *Key, std::optional< T > &Val, Context &Ctx)
Definition YAMLTraits.h:829
void mapOptional(const char *Key, T &Val, const DefaultT &Default)
Definition YAMLTraits.h:813
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:836
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:883
std::string doValidate(IO &io, T &Val, Context &Ctx)
Definition YAMLTraits.h:979
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:895
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