LLVM 23.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"
14#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
23#include "llvm/Support/Endian.h"
24#include "llvm/Support/SMLoc.h"
28#include <array>
29#include <cassert>
30#include <map>
31#include <memory>
32#include <new>
33#include <optional>
34#include <string>
35#include <system_error>
36#include <type_traits>
37#include <vector>
38
39namespace llvm {
40
41class VersionTuple;
42
43namespace yaml {
44
50
51struct EmptyContext {};
52
53/// This class should be specialized by any type that needs to be converted
54/// to/from a YAML mapping. For example:
55///
56/// struct MappingTraits<MyStruct> {
57/// static void mapping(IO &io, MyStruct &s) {
58/// io.mapRequired("name", s.name);
59/// io.mapRequired("size", s.size);
60/// io.mapOptional("age", s.age);
61/// }
62/// };
63template <class T> struct MappingTraits {
64 // Must provide:
65 // static void mapping(IO &io, T &fields);
66 // Optionally may provide:
67 // static std::string validate(IO &io, T &fields);
68 // static void enumInput(IO &io, T &value);
69 //
70 // The optional flow flag will cause generated YAML to use a flow mapping
71 // (e.g. { a: 0, b: 1 }):
72 // static const bool flow = true;
73};
74
75/// This class is similar to MappingTraits<T> but allows you to pass in
76/// additional context for each map operation. For example:
77///
78/// struct MappingContextTraits<MyStruct, MyContext> {
79/// static void mapping(IO &io, MyStruct &s, MyContext &c) {
80/// io.mapRequired("name", s.name);
81/// io.mapRequired("size", s.size);
82/// io.mapOptional("age", s.age);
83/// ++c.TimesMapped;
84/// }
85/// };
86template <class T, class Context> struct MappingContextTraits {
87 // Must provide:
88 // static void mapping(IO &io, T &fields, Context &Ctx);
89 // Optionally may provide:
90 // static std::string validate(IO &io, T &fields, Context &Ctx);
91 //
92 // The optional flow flag will cause generated YAML to use a flow mapping
93 // (e.g. { a: 0, b: 1 }):
94 // static const bool flow = true;
95};
96
97/// This class should be specialized by any integral type that converts
98/// to/from a YAML scalar where there is a one-to-one mapping between
99/// in-memory values and a string in YAML. For example:
100///
101/// struct ScalarEnumerationTraits<Colors> {
102/// static void enumeration(IO &io, Colors &value) {
103/// io.enumCase(value, "red", cRed);
104/// io.enumCase(value, "blue", cBlue);
105/// io.enumCase(value, "green", cGreen);
106/// }
107/// };
108template <typename T, typename Enable = void> struct ScalarEnumerationTraits {
109 // Must provide:
110 // static void enumeration(IO &io, T &value);
111};
112
113/// This class should be specialized by any integer type that is a union
114/// of bit values and the YAML representation is a flow sequence of
115/// strings. For example:
116///
117/// struct ScalarBitSetTraits<MyFlags> {
118/// static void bitset(IO &io, MyFlags &value) {
119/// io.bitSetCase(value, "big", flagBig);
120/// io.bitSetCase(value, "flat", flagFlat);
121/// io.bitSetCase(value, "round", flagRound);
122/// }
123/// };
124template <typename T, typename Enable = void> struct ScalarBitSetTraits {
125 // Must provide:
126 // static void bitset(IO &io, T &value);
127};
128
129/// Describe which type of quotes should be used when quoting is necessary.
130/// Some non-printable characters need to be double-quoted, while some others
131/// are fine with simple-quoting, and some don't need any quoting.
132enum class QuotingType { None, Single, Double };
133
134/// This class should be specialized by type that requires custom conversion
135/// to/from a yaml scalar. For example:
136///
137/// template<>
138/// struct ScalarTraits<MyType> {
139/// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
140/// // stream out custom formatting
141/// out << llvm::format("%x", val);
142/// }
143/// static StringRef input(StringRef scalar, void*, MyType &value) {
144/// // parse scalar and set `value`
145/// // return empty string on success, or error string
146/// return StringRef();
147/// }
148/// static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
149/// };
150template <typename T, typename Enable = void> struct ScalarTraits {
151 // Must provide:
152 //
153 // Function to write the value as a string:
154 // static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
155 //
156 // Function to convert a string to a value. Returns the empty
157 // StringRef on success or an error string if string is malformed:
158 // static StringRef input(StringRef scalar, void *ctxt, T &value);
159 //
160 // Function to determine if the value should be quoted.
161 // static QuotingType mustQuote(StringRef);
162};
163
164/// This class should be specialized by type that requires custom conversion
165/// to/from a YAML literal block scalar. For example:
166///
167/// template <>
168/// struct BlockScalarTraits<MyType> {
169/// static void output(const MyType &Value, void*, llvm::raw_ostream &Out)
170/// {
171/// // stream out custom formatting
172/// Out << Value;
173/// }
174/// static StringRef input(StringRef Scalar, void*, MyType &Value) {
175/// // parse scalar and set `value`
176/// // return empty string on success, or error string
177/// return StringRef();
178/// }
179/// };
180template <typename T> struct BlockScalarTraits {
181 // Must provide:
182 //
183 // Function to write the value as a string:
184 // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out);
185 //
186 // Function to convert a string to a value. Returns the empty
187 // StringRef on success or an error string if string is malformed:
188 // static StringRef input(StringRef Scalar, void *ctxt, T &Value);
189 //
190 // Optional:
191 // static StringRef inputTag(T &Val, std::string Tag)
192 // static void outputTag(const T &Val, raw_ostream &Out)
193};
194
195/// This class should be specialized by type that requires custom conversion
196/// to/from a YAML scalar with optional tags. For example:
197///
198/// template <>
199/// struct TaggedScalarTraits<MyType> {
200/// static void output(const MyType &Value, void*, llvm::raw_ostream
201/// &ScalarOut, llvm::raw_ostream &TagOut)
202/// {
203/// // stream out custom formatting including optional Tag
204/// Out << Value;
205/// }
206/// static StringRef input(StringRef Scalar, StringRef Tag, void*, MyType
207/// &Value) {
208/// // parse scalar and set `value`
209/// // return empty string on success, or error string
210/// return StringRef();
211/// }
212/// static QuotingType mustQuote(const MyType &Value, StringRef) {
213/// return QuotingType::Single;
214/// }
215/// };
216template <typename T> struct TaggedScalarTraits {
217 // Must provide:
218 //
219 // Function to write the value and tag as strings:
220 // static void output(const T &Value, void *ctx, llvm::raw_ostream &ScalarOut,
221 // llvm::raw_ostream &TagOut);
222 //
223 // Function to convert a string to a value. Returns the empty
224 // StringRef on success or an error string if string is malformed:
225 // static StringRef input(StringRef Scalar, StringRef Tag, void *ctxt, T
226 // &Value);
227 //
228 // Function to determine if the value should be quoted.
229 // static QuotingType mustQuote(const T &Value, StringRef Scalar);
230};
231
232/// This class should be specialized by any type that needs to be converted
233/// to/from a YAML sequence. For example:
234///
235/// template<>
236/// struct SequenceTraits<MyContainer> {
237/// static size_t size(IO &io, MyContainer &seq) {
238/// return seq.size();
239/// }
240/// static MyType& element(IO &, MyContainer &seq, size_t index) {
241/// if ( index >= seq.size() )
242/// seq.resize(index+1);
243/// return seq[index];
244/// }
245/// };
246template <typename T, typename EnableIf = void> struct SequenceTraits {
247 // Must provide:
248 // static size_t size(IO &io, T &seq);
249 // static T::value_type& element(IO &io, T &seq, size_t index);
250 //
251 // The following is option and will cause generated YAML to use
252 // a flow sequence (e.g. [a,b,c]).
253 // static const bool flow = true;
254};
255
256/// This class should be specialized by any type for which vectors of that
257/// type need to be converted to/from a YAML sequence.
258template <typename T, typename EnableIf = void> struct SequenceElementTraits {
259 // Must provide:
260 // static const bool flow;
261};
262
263/// This class should be specialized by any type that needs to be converted
264/// to/from a list of YAML documents.
265template <typename T> struct DocumentListTraits {
266 // Must provide:
267 // static size_t size(IO &io, T &seq);
268 // static T::value_type& element(IO &io, T &seq, size_t index);
269};
270
271/// This class should be specialized by any type that needs to be converted
272/// to/from a YAML mapping in the case where the names of the keys are not known
273/// in advance, e.g. a string map.
274template <typename T> struct CustomMappingTraits {
275 // static void inputOne(IO &io, StringRef key, T &elem);
276 // static void output(IO &io, T &elem);
277};
278
279/// This class should be specialized by any type that can be represented as
280/// a scalar, map, or sequence, decided dynamically. For example:
281///
282/// typedef std::unique_ptr<MyBase> MyPoly;
283///
284/// template<>
285/// struct PolymorphicTraits<MyPoly> {
286/// static NodeKind getKind(const MyPoly &poly) {
287/// return poly->getKind();
288/// }
289/// static MyScalar& getAsScalar(MyPoly &poly) {
290/// if (!poly || !isa<MyScalar>(poly))
291/// poly.reset(new MyScalar());
292/// return *cast<MyScalar>(poly.get());
293/// }
294/// // ...
295/// };
296template <typename T> struct PolymorphicTraits {
297 // Must provide:
298 // static NodeKind getKind(const T &poly);
299 // static scalar_type &getAsScalar(T &poly);
300 // static map_type &getAsMap(T &poly);
301 // static sequence_type &getAsSequence(T &poly);
302};
303
304// Only used for better diagnostics of missing traits
305template <typename T> struct MissingTrait;
306
307// Test if ScalarEnumerationTraits<T> is defined on type T.
308template <class T> struct has_ScalarEnumerationTraits {
309 using SignatureEnumeration = void (*)(class IO &, T &);
310
311 template <class U>
312 using check =
314
315 static constexpr bool value = is_detected<check, T>::value;
316};
317
318// Test if ScalarBitSetTraits<T> is defined on type T.
319template <class T> struct has_ScalarBitSetTraits {
320 using SignatureBitset = void (*)(class IO &, T &);
321
322 template <class U>
324
325 static constexpr bool value = is_detected<check, T>::value;
326};
327
328// Test if ScalarTraits<T> is defined on type T.
329template <class T> struct has_ScalarTraits {
330 using SignatureInput = StringRef (*)(StringRef, void *, T &);
331 using SignatureOutput = void (*)(const T &, void *, raw_ostream &);
333
334 template <class U>
335 using check = std::tuple<SameType<SignatureInput, &U::input>,
338
340};
341
342// Test if BlockScalarTraits<T> is defined on type T.
343template <class T> struct has_BlockScalarTraits {
344 using SignatureInput = StringRef (*)(StringRef, void *, T &);
345 using SignatureOutput = void (*)(const T &, void *, raw_ostream &);
346
347 template <class U>
348 using check = std::tuple<SameType<SignatureInput, &U::input>,
350
352};
353
354// Test if TaggedScalarTraits<T> is defined on type T.
355template <class T> struct has_TaggedScalarTraits {
356 using SignatureInput = StringRef (*)(StringRef, StringRef, void *, T &);
357 using SignatureOutput = void (*)(const T &, void *, raw_ostream &,
358 raw_ostream &);
360
361 template <class U>
362 using check = std::tuple<SameType<SignatureInput, &U::input>,
365
366 static constexpr bool value =
368};
369
370// Test if MappingContextTraits<T> is defined on type T.
371template <class T, class Context> struct has_MappingTraits {
372 using SignatureMapping = void (*)(class IO &, T &, Context &);
373
375
376 static constexpr bool value =
378};
379
380// Test if MappingTraits<T> is defined on type T.
381template <class T> struct has_MappingTraits<T, EmptyContext> {
382 using SignatureMapping = void (*)(class IO &, T &);
383
385
387};
388
389// Test if MappingContextTraits<T>::validate() is defined on type T.
390template <class T, class Context> struct has_MappingValidateTraits {
391 using SignatureValidate = std::string (*)(class IO &, T &, Context &);
392
394
395 static constexpr bool value =
397};
398
399// Test if MappingTraits<T>::validate() is defined on type T.
400template <class T> struct has_MappingValidateTraits<T, EmptyContext> {
401 using SignatureValidate = std::string (*)(class IO &, T &);
402
404
406};
407
408// Test if MappingContextTraits<T>::enumInput() is defined on type T.
409template <class T, class Context> struct has_MappingEnumInputTraits {
410 using SignatureEnumInput = void (*)(class IO &, T &);
411
413
414 static constexpr bool value =
416};
417
418// Test if MappingTraits<T>::enumInput() is defined on type T.
419template <class T> struct has_MappingEnumInputTraits<T, EmptyContext> {
420 using SignatureEnumInput = void (*)(class IO &, T &);
421
423
425};
426
427// Test if SequenceTraits<T> is defined on type T.
428template <class T> struct has_SequenceMethodTraits {
429 using SignatureSize = size_t (*)(class IO &, T &);
430
431 template <class U> using check = SameType<SignatureSize, &U::size>;
432
434};
435
436// Test if CustomMappingTraits<T> is defined on type T.
437template <class T> struct has_CustomMappingTraits {
438 using SignatureInput = void (*)(IO &io, StringRef key, T &v);
439
441
442 static constexpr bool value =
444};
445
446// Test if flow is defined on type T.
447template <typename T> struct has_FlowTraits {
448 template <class U> using check = decltype(&U::flow);
449
450 static constexpr bool value = is_detected<check, T>::value;
451};
452
453// Test if SequenceTraits<T> is defined on type T
454template <typename T>
456 : public std::bool_constant<has_SequenceMethodTraits<T>::value> {};
457
458// Test if DocumentListTraits<T> is defined on type T
459template <class T> struct has_DocumentListTraits {
460 using SignatureSize = size_t (*)(class IO &, T &);
461
462 template <class U> using check = SameType<SignatureSize, &U::size>;
463
464 static constexpr bool value =
466};
467
468template <class T> struct has_PolymorphicTraits {
469 using SignatureGetKind = NodeKind (*)(const T &);
470
472
474};
475
476inline bool isNumeric(StringRef S) {
477 const auto skipDigits = [](StringRef Input) {
478 return Input.ltrim("0123456789");
479 };
480
481 // Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls
482 // safe.
483 if (S.empty() || S == "+" || S == "-")
484 return false;
485
486 if (S == ".nan" || S == ".NaN" || S == ".NAN")
487 return true;
488
489 // Infinity and decimal numbers can be prefixed with sign.
490 StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S;
491
492 // Check for infinity first, because checking for hex and oct numbers is more
493 // expensive.
494 if (Tail == ".inf" || Tail == ".Inf" || Tail == ".INF")
495 return true;
496
497 // Section 10.3.2 Tag Resolution
498 // YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with
499 // [-+], so S should be used instead of Tail.
500 if (S.starts_with("0o"))
501 return S.size() > 2 &&
502 S.drop_front(2).find_first_not_of("01234567") == StringRef::npos;
503
504 if (S.starts_with("0x"))
505 return S.size() > 2 && S.drop_front(2).find_first_not_of(
506 "0123456789abcdefABCDEF") == StringRef::npos;
507
508 // Parse float: [-+]? (\. [0-9]+ | [0-9]+ (\. [0-9]* )?) ([eE] [-+]? [0-9]+)?
509 S = Tail;
510
511 // Handle cases when the number starts with '.' and hence needs at least one
512 // digit after dot (as opposed by number which has digits before the dot), but
513 // doesn't have one.
514 if (S.starts_with(".") &&
515 (S == "." ||
516 (S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr)))
517 return false;
518
519 if (S.starts_with("E") || S.starts_with("e"))
520 return false;
521
522 enum ParseState {
523 Default,
524 FoundDot,
525 FoundExponent,
526 };
527 ParseState State = Default;
528
529 S = skipDigits(S);
530
531 // Accept decimal integer.
532 if (S.empty())
533 return true;
534
535 if (S.front() == '.') {
536 State = FoundDot;
537 S = S.drop_front();
538 } else if (S.front() == 'e' || S.front() == 'E') {
539 State = FoundExponent;
540 S = S.drop_front();
541 } else {
542 return false;
543 }
544
545 if (State == FoundDot) {
546 S = skipDigits(S);
547 if (S.empty())
548 return true;
549
550 if (S.front() == 'e' || S.front() == 'E') {
551 State = FoundExponent;
552 S = S.drop_front();
553 } else {
554 return false;
555 }
556 }
557
558 assert(State == FoundExponent && "Should have found exponent at this point.");
559 if (S.empty())
560 return false;
561
562 if (S.front() == '+' || S.front() == '-') {
563 S = S.drop_front();
564 if (S.empty())
565 return false;
566 }
567
568 return skipDigits(S).empty();
569}
570
571inline bool isNull(StringRef S) {
572 return S == "null" || S == "Null" || S == "NULL" || S == "~";
573}
574
575inline bool isBool(StringRef S) {
576 // FIXME: using parseBool is causing multiple tests to fail.
577 return S == "true" || S == "True" || S == "TRUE" || S == "false" ||
578 S == "False" || S == "FALSE";
579}
580
581// 5.1. Character Set
582// The allowed character range explicitly excludes the C0 control block #x0-#x1F
583// (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1
584// control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate
585// block #xD800-#xDFFF, #xFFFE, and #xFFFF.
586//
587// Some strings are valid YAML values even unquoted, but without quotes are
588// interpreted as non-string type, for instance null, boolean or numeric values.
589// If ForcePreserveAsString is set, such strings are quoted.
590inline QuotingType needsQuotes(StringRef S, bool ForcePreserveAsString = true) {
591 if (S.empty())
592 return QuotingType::Single;
593
594 QuotingType MaxQuotingNeeded = QuotingType::None;
595 if (isSpace(static_cast<unsigned char>(S.front())) ||
596 isSpace(static_cast<unsigned char>(S.back())))
597 MaxQuotingNeeded = QuotingType::Single;
598 if (ForcePreserveAsString) {
599 if (isNull(S))
600 MaxQuotingNeeded = QuotingType::Single;
601 if (isBool(S))
602 MaxQuotingNeeded = QuotingType::Single;
603 if (isNumeric(S))
604 MaxQuotingNeeded = QuotingType::Single;
605 }
606
607 // 7.3.3 Plain Style
608 // Plain scalars must not begin with most indicators, as this would cause
609 // ambiguity with other YAML constructs.
610 if (std::strchr(R"(-?:\,[]{}#&*!|>'"%@`)", S[0]) != nullptr)
611 MaxQuotingNeeded = QuotingType::Single;
612
613 for (unsigned char C : S) {
614 // Alphanum is safe.
615 if (isAlnum(C))
616 continue;
617
618 switch (C) {
619 // Safe scalar characters.
620 case '_':
621 case '-':
622 case '^':
623 case '.':
624 case ',':
625 case ' ':
626 // TAB (0x9) is allowed in unquoted strings.
627 case 0x9:
628 continue;
629 // LF(0xA) and CR(0xD) may delimit values and so require at least single
630 // quotes. LLVM YAML parser cannot handle single quoted multiline so use
631 // double quoting to produce valid YAML.
632 case 0xA:
633 case 0xD:
634 return QuotingType::Double;
635 // DEL (0x7F) are excluded from the allowed character range.
636 case 0x7F:
637 return QuotingType::Double;
638 // Forward slash is allowed to be unquoted, but we quote it anyway. We have
639 // many tests that use FileCheck against YAML output, and this output often
640 // contains paths. If we quote backslashes but not forward slashes then
641 // paths will come out either quoted or unquoted depending on which platform
642 // the test is run on, making FileCheck comparisons difficult.
643 case '/':
644 default: {
645 // C0 control block (0x0 - 0x1F) is excluded from the allowed character
646 // range.
647 if (C <= 0x1F)
648 return QuotingType::Double;
649
650 // Always double quote UTF-8.
651 if ((C & 0x80) != 0)
652 return QuotingType::Double;
653
654 // The character is not safe, at least simple quoting needed.
655 MaxQuotingNeeded = QuotingType::Single;
656 }
657 }
658 }
659
660 return MaxQuotingNeeded;
661}
662
663template <typename T, typename Context>
665 : public std::bool_constant<
666 !has_ScalarEnumerationTraits<T>::value &&
667 !has_ScalarBitSetTraits<T>::value && !has_ScalarTraits<T>::value &&
668 !has_BlockScalarTraits<T>::value &&
669 !has_TaggedScalarTraits<T>::value &&
670 !has_MappingTraits<T, Context>::value &&
671 !has_SequenceTraits<T>::value && !has_CustomMappingTraits<T>::value &&
672 !has_DocumentListTraits<T>::value &&
673 !has_PolymorphicTraits<T>::value> {};
674
675template <typename T, typename Context>
677 : public std::bool_constant<has_MappingTraits<T, Context>::value &&
678 has_MappingValidateTraits<T, Context>::value> {
679};
680
681template <typename T, typename Context>
683 : public std::bool_constant<has_MappingTraits<T, Context>::value &&
684 !has_MappingValidateTraits<T, Context>::value> {
685};
686
687// Base class for Input and Output.
689public:
690 IO(void *Ctxt = nullptr);
691 virtual ~IO();
692
693 virtual bool outputting() const = 0;
694
695 virtual unsigned beginSequence() = 0;
696 virtual bool preflightElement(unsigned, void *&) = 0;
697 virtual void postflightElement(void *) = 0;
698 virtual void endSequence() = 0;
699 virtual bool canElideEmptySequence() = 0;
700
701 virtual unsigned beginFlowSequence() = 0;
702 virtual bool preflightFlowElement(unsigned, void *&) = 0;
703 virtual void postflightFlowElement(void *) = 0;
704 virtual void endFlowSequence() = 0;
705
706 virtual bool mapTag(StringRef Tag, bool Default = false) = 0;
707 virtual void beginMapping() = 0;
708 virtual void endMapping() = 0;
709 virtual bool preflightKey(StringRef, bool, bool, bool &, void *&) = 0;
710 virtual void postflightKey(void *) = 0;
711 virtual std::vector<StringRef> keys() = 0;
712
713 virtual void beginFlowMapping() = 0;
714 virtual void endFlowMapping() = 0;
715
716 virtual void beginEnumScalar() = 0;
717 virtual bool matchEnumScalar(StringRef, bool) = 0;
718 virtual bool matchEnumFallback() = 0;
719 virtual void endEnumScalar() = 0;
720
721 virtual bool beginBitSetScalar(bool &) = 0;
722 virtual bool bitSetMatch(StringRef, bool) = 0;
723 virtual void endBitSetScalar() = 0;
724
725 virtual void scalarString(StringRef &, QuotingType) = 0;
726 virtual void blockScalarString(StringRef &) = 0;
727 virtual void scalarTag(std::string &) = 0;
728
729 virtual NodeKind getNodeKind() = 0;
730
731 virtual void setError(const Twine &) = 0;
732 virtual std::error_code error() = 0;
733 virtual void setAllowUnknownKeys(bool Allow);
734
735 template <typename T> void enumCase(T &Val, StringRef 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, StringRef 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, StringRef 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, StringRef 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, StringRef 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, StringRef 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(StringRef Key, T &Val) {
791 EmptyContext Ctx;
792 this->processKey(Key, Val, true, Ctx);
793 }
794
795 template <typename T, typename Context>
796 void mapRequired(StringRef Key, T &Val, Context &Ctx) {
797 this->processKey(Key, Val, true, Ctx);
798 }
799
800 template <typename T> void mapOptional(StringRef Key, T &Val) {
801 EmptyContext Ctx;
802 mapOptionalWithContext(Key, Val, Ctx);
803 }
804
805 template <typename T, typename DefaultT>
806 void mapOptional(StringRef Key, T &Val, const DefaultT &Default) {
807 EmptyContext Ctx;
809 }
810
811 template <typename T, typename Context>
812 void mapOptionalWithContext(StringRef 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(StringRef 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(StringRef 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(StringRef 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(StringRef 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(StringRef 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(StringRef Key, 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(StringRef, bool) override;
1350 bool matchEnumFallback() override;
1351 void endEnumScalar() override;
1352 bool beginBitSetScalar(bool &) override;
1353 bool bitSetMatch(StringRef, 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 void saveAliasHNode(Node *node, HNode *hnode);
1427 Input::HNode *createHNodes(Node *node);
1428 void setError(HNode *hnode, const Twine &message);
1429 void setError(Node *node, const Twine &message);
1430 void setError(const SMRange &Range, const Twine &message);
1431
1432 void reportWarning(HNode *hnode, const Twine &message);
1433 void reportWarning(Node *hnode, const Twine &message);
1434 void reportWarning(const SMRange &Range, const Twine &message);
1435
1436 /// Release memory used by HNodes.
1437 void releaseHNodeBuffers();
1438
1439public:
1440 // These are only used by operator>>. They could be private
1441 // if those templated things could be made friends.
1442 bool setCurrentDocument();
1443 bool nextDocument();
1444
1445 /// Returns the current node that's being parsed by the YAML Parser.
1446 const Node *getCurrentNode() const;
1447
1448 void setAllowUnknownKeys(bool Allow) override;
1449
1450private:
1451 SourceMgr SrcMgr; // must be before Strm
1452 std::unique_ptr<llvm::yaml::Stream> Strm;
1453 HNode *TopNode = nullptr;
1454 std::error_code EC;
1455 BumpPtrAllocator StringAllocator;
1456 SpecificBumpPtrAllocator<EmptyHNode> EmptyHNodeAllocator;
1457 SpecificBumpPtrAllocator<ScalarHNode> ScalarHNodeAllocator;
1458 SpecificBumpPtrAllocator<MapHNode> MapHNodeAllocator;
1459 SpecificBumpPtrAllocator<SequenceHNode> SequenceHNodeAllocator;
1460 document_iterator DocIterator;
1461 llvm::BitVector BitValuesUsed;
1462 HNode *CurrentNode = nullptr;
1463 bool ScalarMatchFound = false;
1464 bool AllowUnknownKeys = false;
1465 DenseMap<StringRef, HNode *> AliasMap;
1466};
1467
1468///
1469/// The Output class is used to generate a yaml document from in-memory structs
1470/// and vectors.
1471///
1472class LLVM_ABI Output : public IO {
1473public:
1474 Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
1475 ~Output() override;
1476
1477 /// Set whether or not to output optional values which are equal
1478 /// to the default value. By default, when outputting if you attempt
1479 /// to write a value that is equal to the default, the value gets ignored.
1480 /// Sometimes, it is useful to be able to see these in the resulting YAML
1481 /// anyway.
1482 void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; }
1483
1484 bool outputting() const override;
1485 bool mapTag(StringRef, bool) override;
1486 void beginMapping() override;
1487 void endMapping() override;
1488 bool preflightKey(StringRef Key, bool, bool, bool &, void *&) override;
1489 void postflightKey(void *) override;
1490 std::vector<StringRef> keys() override;
1491 void beginFlowMapping() override;
1492 void endFlowMapping() override;
1493 unsigned beginSequence() override;
1494 void endSequence() override;
1495 bool preflightElement(unsigned, void *&) override;
1496 void postflightElement(void *) override;
1497 unsigned beginFlowSequence() override;
1498 bool preflightFlowElement(unsigned, void *&) override;
1499 void postflightFlowElement(void *) override;
1500 void endFlowSequence() override;
1501 void beginEnumScalar() override;
1502 bool matchEnumScalar(StringRef, bool) override;
1503 bool matchEnumFallback() override;
1504 void endEnumScalar() override;
1505 bool beginBitSetScalar(bool &) override;
1506 bool bitSetMatch(StringRef, bool) override;
1507 void endBitSetScalar() override;
1508 void scalarString(StringRef &, QuotingType) override;
1509 void blockScalarString(StringRef &) override;
1510 void scalarTag(std::string &) override;
1511 NodeKind getNodeKind() override;
1512 void setError(const Twine &message) override;
1513 std::error_code error() override;
1514 bool canElideEmptySequence() override;
1515
1516 // These are only used by operator<<. They could be private
1517 // if that templated operator could be made a friend.
1518 void beginDocuments();
1519 bool preflightDocument(unsigned);
1520 void postflightDocument();
1521 void endDocuments();
1522
1523private:
1524 void output(StringRef s);
1525 void output(StringRef, QuotingType);
1526 void outputUpToEndOfLine(StringRef s);
1527 void newLineCheck(bool EmptySequence = false);
1528 void outputNewLine();
1529 void paddedKey(StringRef key);
1530 void flowKey(StringRef Key);
1531
1532 enum InState {
1533 inSeqFirstElement,
1534 inSeqOtherElement,
1535 inFlowSeqFirstElement,
1536 inFlowSeqOtherElement,
1537 inMapFirstKey,
1538 inMapOtherKey,
1539 inFlowMapFirstKey,
1540 inFlowMapOtherKey
1541 };
1542
1543 static bool inSeqAnyElement(InState State);
1544 static bool inFlowSeqAnyElement(InState State);
1545 static bool inMapAnyKey(InState State);
1546 static bool inFlowMapAnyKey(InState State);
1547
1548 raw_ostream &Out;
1549 int WrapColumn;
1550 SmallVector<InState, 8> StateStack;
1551 int Column = 0;
1552 int ColumnAtFlowStart = 0;
1553 int ColumnAtMapFlowStart = 0;
1554 bool NeedBitValueComma = false;
1555 bool NeedFlowSequenceComma = false;
1556 bool EnumerationMatchFound = false;
1557 bool WriteDefaultValues = false;
1558 StringRef Padding;
1559 StringRef PaddingBeforeContainer;
1560};
1561
1562template <typename T, typename Context>
1563void IO::processKeyWithDefault(StringRef Key, std::optional<T> &Val,
1564 const std::optional<T> &DefaultValue,
1565 bool Required, Context &Ctx) {
1566 assert(!DefaultValue && "std::optional<T> shouldn't have a value!");
1567 void *SaveInfo;
1568 bool UseDefault = true;
1569 const bool sameAsDefault = outputting() && !Val;
1570 if (!outputting() && !Val)
1571 Val = T();
1572 if (Val &&
1573 this->preflightKey(Key, Required, sameAsDefault, UseDefault, SaveInfo)) {
1574
1575 // When reading an std::optional<X> key from a YAML description, we allow
1576 // the special "<none>" value, which can be used to specify that no value
1577 // was requested, i.e. the DefaultValue will be assigned. The DefaultValue
1578 // is usually None.
1579 bool IsNone = false;
1580 if (!outputting())
1581 if (const auto *Node =
1582 dyn_cast<ScalarNode>(((Input *)this)->getCurrentNode()))
1583 // We use rtrim to ignore possible white spaces that might exist when a
1584 // comment is present on the same line.
1585 IsNone = Node->getRawValue().rtrim(' ') == "<none>";
1586
1587 if (IsNone)
1588 Val = DefaultValue;
1589 else
1590 yamlize(*this, *Val, Required, Ctx);
1591 this->postflightKey(SaveInfo);
1592 } else {
1593 if (UseDefault)
1594 Val = DefaultValue;
1595 }
1596}
1597
1598/// YAML I/O does conversion based on types. But often native data types
1599/// are just a typedef of built in intergral types (e.g. int). But the C++
1600/// type matching system sees through the typedef and all the typedefed types
1601/// look like a built in type. This will cause the generic YAML I/O conversion
1602/// to be used. To provide better control over the YAML conversion, you can
1603/// use this macro instead of typedef. It will create a class with one field
1604/// and automatic conversion operators to and from the base type.
1605/// Based on BOOST_STRONG_TYPEDEF
1606#define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
1607 struct _type { \
1608 _type() = default; \
1609 _type(const _base v) : value(v) {} \
1610 _type(const _type &v) = default; \
1611 _type &operator=(const _type &rhs) = default; \
1612 _type &operator=(const _base &rhs) { \
1613 value = rhs; \
1614 return *this; \
1615 } \
1616 operator const _base &() const { return value; } \
1617 bool operator==(const _type &rhs) const { return value == rhs.value; } \
1618 bool operator==(const _base &rhs) const { return value == rhs; } \
1619 bool operator<(const _type &rhs) const { return value < rhs.value; } \
1620 _base value; \
1621 using BaseType = _base; \
1622 };
1623
1624///
1625/// Use these types instead of uintXX_t in any mapping to have
1626/// its yaml output formatted as hexadecimal.
1627///
1632
1633template <> struct ScalarTraits<Hex8> {
1634 LLVM_ABI static void output(const Hex8 &, void *, raw_ostream &);
1635 LLVM_ABI static StringRef input(StringRef, void *, Hex8 &);
1637};
1638
1639template <> struct ScalarTraits<Hex16> {
1640 LLVM_ABI static void output(const Hex16 &, void *, raw_ostream &);
1641 LLVM_ABI static StringRef input(StringRef, void *, Hex16 &);
1643};
1644
1645template <> struct ScalarTraits<Hex32> {
1646 LLVM_ABI static void output(const Hex32 &, void *, raw_ostream &);
1647 LLVM_ABI static StringRef input(StringRef, void *, Hex32 &);
1649};
1650
1651template <> struct ScalarTraits<Hex64> {
1652 LLVM_ABI static void output(const Hex64 &, void *, raw_ostream &);
1653 LLVM_ABI static StringRef input(StringRef, void *, Hex64 &);
1655};
1656
1657template <> struct ScalarTraits<VersionTuple> {
1658 LLVM_ABI static void output(const VersionTuple &Value, void *,
1659 llvm::raw_ostream &Out);
1662};
1663
1664// Define non-member operator>> so that Input can stream in a document list.
1665template <typename T>
1666inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &>
1667operator>>(Input &yin, T &docList) {
1668 int i = 0;
1669 EmptyContext Ctx;
1670 while (yin.setCurrentDocument()) {
1671 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx);
1672 if (yin.error())
1673 return yin;
1674 yin.nextDocument();
1675 ++i;
1676 }
1677 return yin;
1678}
1679
1680// Define non-member operator>> so that Input can stream in a map as a document.
1681template <typename T>
1682inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Input &>
1683operator>>(Input &yin, T &docMap) {
1684 EmptyContext Ctx;
1685 yin.setCurrentDocument();
1686 yamlize(yin, docMap, true, Ctx);
1687 return yin;
1688}
1689
1690// Define non-member operator>> so that Input can stream in a sequence as
1691// a document.
1692template <typename T>
1693inline std::enable_if_t<has_SequenceTraits<T>::value, Input &>
1694operator>>(Input &yin, T &docSeq) {
1695 EmptyContext Ctx;
1696 if (yin.setCurrentDocument())
1697 yamlize(yin, docSeq, true, Ctx);
1698 return yin;
1699}
1700
1701// Define non-member operator>> so that Input can stream in a block scalar.
1702template <typename T>
1703inline std::enable_if_t<has_BlockScalarTraits<T>::value, Input &>
1704operator>>(Input &In, T &Val) {
1705 EmptyContext Ctx;
1706 if (In.setCurrentDocument())
1707 yamlize(In, Val, true, Ctx);
1708 return In;
1709}
1710
1711// Define non-member operator>> so that Input can stream in a string map.
1712template <typename T>
1713inline std::enable_if_t<has_CustomMappingTraits<T>::value, Input &>
1714operator>>(Input &In, T &Val) {
1715 EmptyContext Ctx;
1716 if (In.setCurrentDocument())
1717 yamlize(In, Val, true, Ctx);
1718 return In;
1719}
1720
1721// Define non-member operator>> so that Input can stream in a polymorphic type.
1722template <typename T>
1723inline std::enable_if_t<has_PolymorphicTraits<T>::value, Input &>
1724operator>>(Input &In, T &Val) {
1725 EmptyContext Ctx;
1726 if (In.setCurrentDocument())
1727 yamlize(In, Val, true, Ctx);
1728 return In;
1729}
1730
1731// Provide better error message about types missing a trait specialization
1732template <typename T>
1733inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Input &>
1734operator>>(Input &yin, T &docSeq) {
1735 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1736 return yin;
1737}
1738
1739// Define non-member operator<< so that Output can stream out document list.
1740template <typename T>
1741inline std::enable_if_t<has_DocumentListTraits<T>::value, Output &>
1742operator<<(Output &yout, T &docList) {
1743 EmptyContext Ctx;
1744 yout.beginDocuments();
1745 const size_t count = DocumentListTraits<T>::size(yout, docList);
1746 for (size_t i = 0; i < count; ++i) {
1747 if (yout.preflightDocument(i)) {
1748 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true,
1749 Ctx);
1750 yout.postflightDocument();
1751 }
1752 }
1753 yout.endDocuments();
1754 return yout;
1755}
1756
1757// Define non-member operator<< so that Output can stream out a map.
1758template <typename T>
1759inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Output &>
1760operator<<(Output &yout, T &map) {
1761 EmptyContext Ctx;
1762 yout.beginDocuments();
1763 if (yout.preflightDocument(0)) {
1764 yamlize(yout, map, true, Ctx);
1765 yout.postflightDocument();
1766 }
1767 yout.endDocuments();
1768 return yout;
1769}
1770
1771// Define non-member operator<< so that Output can stream out a sequence.
1772template <typename T>
1773inline std::enable_if_t<has_SequenceTraits<T>::value, Output &>
1774operator<<(Output &yout, T &seq) {
1775 EmptyContext Ctx;
1776 yout.beginDocuments();
1777 if (yout.preflightDocument(0)) {
1778 yamlize(yout, seq, true, Ctx);
1779 yout.postflightDocument();
1780 }
1781 yout.endDocuments();
1782 return yout;
1783}
1784
1785// Define non-member operator<< so that Output can stream out a block scalar.
1786template <typename T>
1787inline std::enable_if_t<has_BlockScalarTraits<T>::value, Output &>
1788operator<<(Output &Out, T &Val) {
1789 EmptyContext Ctx;
1790 Out.beginDocuments();
1791 if (Out.preflightDocument(0)) {
1792 yamlize(Out, Val, true, Ctx);
1793 Out.postflightDocument();
1794 }
1795 Out.endDocuments();
1796 return Out;
1797}
1798
1799// Define non-member operator<< so that Output can stream out a string map.
1800template <typename T>
1801inline std::enable_if_t<has_CustomMappingTraits<T>::value, Output &>
1802operator<<(Output &Out, T &Val) {
1803 EmptyContext Ctx;
1804 Out.beginDocuments();
1805 if (Out.preflightDocument(0)) {
1806 yamlize(Out, Val, true, Ctx);
1807 Out.postflightDocument();
1808 }
1809 Out.endDocuments();
1810 return Out;
1811}
1812
1813// Define non-member operator<< so that Output can stream out a polymorphic
1814// type.
1815template <typename T>
1816inline std::enable_if_t<has_PolymorphicTraits<T>::value, Output &>
1817operator<<(Output &Out, T &Val) {
1818 EmptyContext Ctx;
1819 Out.beginDocuments();
1820 if (Out.preflightDocument(0)) {
1821 // FIXME: The parser does not support explicit documents terminated with a
1822 // plain scalar; the end-marker is included as part of the scalar token.
1824 "plain scalar documents are not supported");
1825 yamlize(Out, Val, true, Ctx);
1826 Out.postflightDocument();
1827 }
1828 Out.endDocuments();
1829 return Out;
1830}
1831
1832// Provide better error message about types missing a trait specialization
1833template <typename T>
1834inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Output &>
1835operator<<(Output &yout, T &seq) {
1836 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1837 return yout;
1838}
1839
1840template <bool B> struct IsFlowSequenceBase {};
1841template <> struct IsFlowSequenceBase<true> {
1842 static const bool flow = true;
1843};
1844
1845template <typename T>
1846using check_resize_t = decltype(std::declval<T>().resize(0));
1847
1848template <typename T> struct IsResizableBase {
1849 using type = typename T::value_type;
1850
1851 static type &element(IO &io, T &seq, size_t index) {
1853 if (index >= seq.size())
1854 seq.resize(index + 1);
1855 } else {
1856 if (index >= seq.size()) {
1857 io.setError(Twine("value sequence extends beyond static size (") +
1858 Twine(seq.size()) + ")");
1859 return seq[0];
1860 }
1861 }
1862 return seq[index];
1863 }
1864};
1865
1866template <typename T, bool Flow>
1868 static size_t size(IO &io, T &seq) { return seq.size(); }
1869};
1870
1871// Simple helper to check an expression can be used as a bool-valued template
1872// argument.
1873template <bool> struct CheckIsBool {
1874 static const bool value = true;
1875};
1876
1877// If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have
1878// SequenceTraits that do the obvious thing.
1879template <typename T>
1881 std::vector<T>,
1882 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1883 : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
1884template <typename T, size_t N>
1886 std::array<T, N>,
1887 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1888 : SequenceTraitsImpl<std::array<T, N>, SequenceElementTraits<T>::flow> {};
1889template <typename T, unsigned N>
1891 SmallVector<T, N>,
1892 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1893 : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
1894template <typename T>
1897 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1898 : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {};
1899template <typename T>
1902 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1903 : SequenceTraitsImpl<MutableArrayRef<T>, SequenceElementTraits<T>::flow> {};
1904
1905// Sequences of fundamental types use flow formatting.
1906template <typename T>
1907struct SequenceElementTraits<T, std::enable_if_t<std::is_fundamental_v<T>>> {
1908 static const bool flow = true;
1909};
1910
1911// Sequences of strings use block formatting.
1912template <> struct SequenceElementTraits<std::string> {
1913 static const bool flow = false;
1914};
1916 static const bool flow = false;
1917};
1918template <> struct SequenceElementTraits<std::pair<std::string, std::string>> {
1919 static const bool flow = false;
1920};
1921
1922/// Implementation of CustomMappingTraits for std::map<std::string, T>.
1923template <typename T> struct StdMapStringCustomMappingTraitsImpl {
1924 using map_type = std::map<std::string, T>;
1925
1926 static void inputOne(IO &io, StringRef key, map_type &v) {
1927 io.mapRequired(key, v[std::string(key)]);
1928 }
1929
1930 static void output(IO &io, map_type &v) {
1931 for (auto &p : v)
1932 io.mapRequired(p.first, p.second);
1933 }
1934};
1935
1936} // end namespace yaml
1937} // end namespace llvm
1938
1939#define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW) \
1940 namespace llvm { \
1941 namespace yaml { \
1942 static_assert( \
1943 !std::is_fundamental_v<TYPE> && !std::is_same_v<TYPE, std::string> && \
1944 !std::is_same_v<TYPE, llvm::StringRef>, \
1945 "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \
1946 template <> struct SequenceElementTraits<TYPE> { \
1947 static const bool flow = FLOW; \
1948 }; \
1949 } \
1950 }
1951
1952/// Utility for declaring that a std::vector of a particular type
1953/// should be considered a YAML sequence.
1954#define LLVM_YAML_IS_SEQUENCE_VECTOR(type) \
1955 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false)
1956
1957/// Utility for declaring that a std::vector of a particular type
1958/// should be considered a YAML flow sequence.
1959#define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type) \
1960 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true)
1961
1962#define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type) \
1963 namespace llvm { \
1964 namespace yaml { \
1965 template <> struct LLVM_ABI MappingTraits<Type> { \
1966 static void mapping(IO &IO, Type &Obj); \
1967 }; \
1968 } \
1969 }
1970
1971#define LLVM_YAML_DECLARE_MAPPING_TRAITS_PRIVATE(Type) \
1972 namespace llvm { \
1973 namespace yaml { \
1974 template <> struct MappingTraits<Type> { \
1975 static void mapping(IO &IO, Type &Obj); \
1976 }; \
1977 } \
1978 }
1979
1980#define LLVM_YAML_DECLARE_ENUM_TRAITS(Type) \
1981 namespace llvm { \
1982 namespace yaml { \
1983 template <> struct LLVM_ABI ScalarEnumerationTraits<Type> { \
1984 static void enumeration(IO &io, Type &Value); \
1985 }; \
1986 } \
1987 }
1988
1989#define LLVM_YAML_DECLARE_BITSET_TRAITS(Type) \
1990 namespace llvm { \
1991 namespace yaml { \
1992 template <> struct LLVM_ABI ScalarBitSetTraits<Type> { \
1993 static void bitset(IO &IO, Type &Options); \
1994 }; \
1995 } \
1996 }
1997
1998#define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote) \
1999 namespace llvm { \
2000 namespace yaml { \
2001 template <> struct LLVM_ABI ScalarTraits<Type> { \
2002 static void output(const Type &Value, void *ctx, raw_ostream &Out); \
2003 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
2004 static QuotingType mustQuote(StringRef) { return MustQuote; } \
2005 }; \
2006 } \
2007 }
2008
2009/// Utility for declaring that a std::vector of a particular type
2010/// should be considered a YAML document list.
2011#define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
2012 namespace llvm { \
2013 namespace yaml { \
2014 template <unsigned N> \
2015 struct DocumentListTraits<SmallVector<_type, N>> \
2016 : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \
2017 template <> \
2018 struct DocumentListTraits<std::vector<_type>> \
2019 : public SequenceTraitsImpl<std::vector<_type>, false> {}; \
2020 } \
2021 }
2022
2023/// Utility for declaring that std::map<std::string, _type> should be considered
2024/// a YAML map.
2025#define LLVM_YAML_IS_STRING_MAP(_type) \
2026 namespace llvm { \
2027 namespace yaml { \
2028 template <> \
2029 struct CustomMappingTraits<std::map<std::string, _type>> \
2030 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \
2031 } \
2032 }
2033
2034LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)
2035LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)
2036LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)
2037LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)
2038
2039#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
This file defines the DenseMap class.
#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:298
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:49
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static constexpr size_t npos
Definition StringRef.h:57
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:629
char back() const
back - Get the last character in the string.
Definition StringRef.h:152
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
char front() const
front - Get the first character in the string.
Definition StringRef.h:146
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:760
virtual bool matchEnumScalar(StringRef, bool)=0
virtual void endEnumScalar()=0
void bitSetCase(T &Val, StringRef Str, const uint32_t ConstVal)
Definition YAMLTraits.h:768
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:829
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:822
void enumCase(T &Val, StringRef Str, const T ConstVal)
Definition YAMLTraits.h:735
virtual void endMapping()=0
void mapOptionalWithContext(StringRef Key, T &Val, Context &Ctx)
Definition YAMLTraits.h:812
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:781
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:800
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:743
virtual void endFlowMapping()=0
void mapRequired(StringRef Key, T &Val, Context &Ctx)
Definition YAMLTraits.h:796
void enumFallback(T &Val)
Definition YAMLTraits.h:749
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:806
void mapRequired(StringRef Key, T &Val)
Definition YAMLTraits.h:790
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:775
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
std::map< AliasEntry, AliasEntry > AliasMap
Definition Utils.h:86
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:132
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:476
std::enable_if_t< has_DocumentListTraits< T >::value, Input & > operator>>(Input &yin, T &docList)
QuotingType needsQuotes(StringRef S, bool ForcePreserveAsString=true)
Definition YAMLTraits.h:590
bool isNull(StringRef S)
Definition YAMLTraits.h:571
bool isBool(StringRef S)
Definition YAMLTraits.h:575
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:643
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...
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:2011
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
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
@ Default
The result value is 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:870
#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:180
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:274
This class should be specialized by any type that needs to be converted to/from a list of YAML docume...
Definition YAMLTraits.h:265
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:86
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:63
This class should be specialized by any type that can be represented as a scalar, map,...
Definition YAMLTraits.h:296
This class should be specialized by any integer type that is a union of bit values and the YAML repre...
Definition YAMLTraits.h:124
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition YAMLTraits.h:108
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:150
This class should be specialized by any type for which vectors of that type need to be converted to/f...
Definition YAMLTraits.h:258
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:246
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:216
StringRef(*)(StringRef, void *, T &) SignatureInput
Definition YAMLTraits.h:344
static constexpr bool value
Definition YAMLTraits.h:351
void(*)(const T &, void *, raw_ostream &) SignatureOutput
Definition YAMLTraits.h:345
std::tuple< SameType< SignatureInput, &U::input >, SameType< SignatureOutput, &U::output > > check
Definition YAMLTraits.h:348
SameType< SignatureInput, &U::inputOne > check
Definition YAMLTraits.h:440
void(*)(IO &io, StringRef key, T &v) SignatureInput
Definition YAMLTraits.h:438
static constexpr bool value
Definition YAMLTraits.h:464
size_t(*)(class IO &, T &) SignatureSize
Definition YAMLTraits.h:460
SameType< SignatureSize, &U::size > check
Definition YAMLTraits.h:462
decltype(&U::flow) check
Definition YAMLTraits.h:448
static constexpr bool value
Definition YAMLTraits.h:450
SameType< SignatureEnumInput, &U::enumInput > check
Definition YAMLTraits.h:422
void(*)(class IO &, T &) SignatureEnumInput
Definition YAMLTraits.h:410
SameType< SignatureEnumInput, &U::enumInput > check
Definition YAMLTraits.h:412
SameType< SignatureMapping, &U::mapping > check
Definition YAMLTraits.h:384
void(*)(class IO &, T &, Context &) SignatureMapping
Definition YAMLTraits.h:372
SameType< SignatureMapping, &U::mapping > check
Definition YAMLTraits.h:374
static constexpr bool value
Definition YAMLTraits.h:376
SameType< SignatureValidate, &U::validate > check
Definition YAMLTraits.h:403
std::string(*)(class IO &, T &, Context &) SignatureValidate
Definition YAMLTraits.h:391
SameType< SignatureValidate, &U::validate > check
Definition YAMLTraits.h:393
NodeKind(*)(const T &) SignatureGetKind
Definition YAMLTraits.h:469
static constexpr bool value
Definition YAMLTraits.h:473
SameType< SignatureGetKind, &U::getKind > check
Definition YAMLTraits.h:471
void(*)(class IO &, T &) SignatureBitset
Definition YAMLTraits.h:320
static constexpr bool value
Definition YAMLTraits.h:325
SameType< SignatureBitset, &ScalarBitSetTraits< U >::bitset > check
Definition YAMLTraits.h:323
void(*)(class IO &, T &) SignatureEnumeration
Definition YAMLTraits.h:309
SameType< SignatureEnumeration, &ScalarEnumerationTraits< U >::enumeration > check
Definition YAMLTraits.h:312
void(*)(const T &, void *, raw_ostream &) SignatureOutput
Definition YAMLTraits.h:331
QuotingType(*)(StringRef) SignatureMustQuote
Definition YAMLTraits.h:332
std::tuple< SameType< SignatureInput, &U::input >, SameType< SignatureOutput, &U::output >, SameType< SignatureMustQuote, &U::mustQuote > > check
Definition YAMLTraits.h:335
StringRef(*)(StringRef, void *, T &) SignatureInput
Definition YAMLTraits.h:330
static constexpr bool value
Definition YAMLTraits.h:339
size_t(*)(class IO &, T &) SignatureSize
Definition YAMLTraits.h:429
SameType< SignatureSize, &U::size > check
Definition YAMLTraits.h:431
StringRef(*)(StringRef, StringRef, void *, T &) SignatureInput
Definition YAMLTraits.h:356
QuotingType(*)(const T &, StringRef) SignatureMustQuote
Definition YAMLTraits.h:359
std::tuple< SameType< SignatureInput, &U::input >, SameType< SignatureOutput, &U::output >, SameType< SignatureMustQuote, &U::mustQuote > > check
Definition YAMLTraits.h:362
void(*)(const T &, void *, raw_ostream &, raw_ostream &) SignatureOutput
Definition YAMLTraits.h:357
static constexpr bool value
Definition YAMLTraits.h:366