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