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