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