1 //===- llvm/Support/YAMLTraits.h --------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_YAMLTRAITS_H
11 #define LLVM_SUPPORT_YAMLTRAITS_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/AlignOf.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Regex.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/YAMLParser.h"
24 #include "llvm/Support/raw_ostream.h"
33 #include <system_error>
34 #include <type_traits>
40 struct EmptyContext {};
42 /// This class should be specialized by any type that needs to be converted
43 /// to/from a YAML mapping. For example:
45 /// struct MappingTraits<MyStruct> {
46 /// static void mapping(IO &io, MyStruct &s) {
47 /// io.mapRequired("name", s.name);
48 /// io.mapRequired("size", s.size);
49 /// io.mapOptional("age", s.age);
53 struct MappingTraits {
55 // static void mapping(IO &io, T &fields);
56 // Optionally may provide:
57 // static StringRef validate(IO &io, T &fields);
59 // The optional flow flag will cause generated YAML to use a flow mapping
60 // (e.g. { a: 0, b: 1 }):
61 // static const bool flow = true;
64 /// This class is similar to MappingTraits<T> but allows you to pass in
65 /// additional context for each map operation. For example:
67 /// struct MappingContextTraits<MyStruct, MyContext> {
68 /// static void mapping(IO &io, MyStruct &s, MyContext &c) {
69 /// io.mapRequired("name", s.name);
70 /// io.mapRequired("size", s.size);
71 /// io.mapOptional("age", s.age);
75 template <class T, class Context> struct MappingContextTraits {
77 // static void mapping(IO &io, T &fields, Context &Ctx);
78 // Optionally may provide:
79 // static StringRef validate(IO &io, T &fields, Context &Ctx);
81 // The optional flow flag will cause generated YAML to use a flow mapping
82 // (e.g. { a: 0, b: 1 }):
83 // static const bool flow = true;
86 /// This class should be specialized by any integral type that converts
87 /// to/from a YAML scalar where there is a one-to-one mapping between
88 /// in-memory values and a string in YAML. For example:
90 /// struct ScalarEnumerationTraits<Colors> {
91 /// static void enumeration(IO &io, Colors &value) {
92 /// io.enumCase(value, "red", cRed);
93 /// io.enumCase(value, "blue", cBlue);
94 /// io.enumCase(value, "green", cGreen);
98 struct ScalarEnumerationTraits {
100 // static void enumeration(IO &io, T &value);
103 /// This class should be specialized by any integer type that is a union
104 /// of bit values and the YAML representation is a flow sequence of
105 /// strings. For example:
107 /// struct ScalarBitSetTraits<MyFlags> {
108 /// static void bitset(IO &io, MyFlags &value) {
109 /// io.bitSetCase(value, "big", flagBig);
110 /// io.bitSetCase(value, "flat", flagFlat);
111 /// io.bitSetCase(value, "round", flagRound);
115 struct ScalarBitSetTraits {
117 // static void bitset(IO &io, T &value);
120 /// This class should be specialized by type that requires custom conversion
121 /// to/from a yaml scalar. For example:
124 /// struct ScalarTraits<MyType> {
125 /// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
126 /// // stream out custom formatting
127 /// out << llvm::format("%x", val);
129 /// static StringRef input(StringRef scalar, void*, MyType &value) {
130 /// // parse scalar and set `value`
131 /// // return empty string on success, or error string
132 /// return StringRef();
134 /// static bool mustQuote(StringRef) { return true; }
137 struct ScalarTraits {
140 // Function to write the value as a string:
141 //static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
143 // Function to convert a string to a value. Returns the empty
144 // StringRef on success or an error string if string is malformed:
145 //static StringRef input(StringRef scalar, void *ctxt, T &value);
147 // Function to determine if the value should be quoted.
148 //static bool mustQuote(StringRef);
151 /// This class should be specialized by type that requires custom conversion
152 /// to/from a YAML literal block scalar. For example:
155 /// struct BlockScalarTraits<MyType> {
156 /// static void output(const MyType &Value, void*, llvm::raw_ostream &Out)
158 /// // stream out custom formatting
161 /// static StringRef input(StringRef Scalar, void*, MyType &Value) {
162 /// // parse scalar and set `value`
163 /// // return empty string on success, or error string
164 /// return StringRef();
167 template <typename T>
168 struct BlockScalarTraits {
171 // Function to write the value as a string:
172 // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out);
174 // Function to convert a string to a value. Returns the empty
175 // StringRef on success or an error string if string is malformed:
176 // static StringRef input(StringRef Scalar, void *ctxt, T &Value);
179 /// This class should be specialized by any type that needs to be converted
180 /// to/from a YAML sequence. For example:
183 /// struct SequenceTraits< std::vector<MyType>> {
184 /// static size_t size(IO &io, std::vector<MyType> &seq) {
185 /// return seq.size();
187 /// static MyType& element(IO &, std::vector<MyType> &seq, size_t index) {
188 /// if ( index >= seq.size() )
189 /// seq.resize(index+1);
190 /// return seq[index];
194 struct SequenceTraits {
196 // static size_t size(IO &io, T &seq);
197 // static T::value_type& element(IO &io, T &seq, size_t index);
199 // The following is option and will cause generated YAML to use
200 // a flow sequence (e.g. [a,b,c]).
201 // static const bool flow = true;
204 /// This class should be specialized by any type that needs to be converted
205 /// to/from a list of YAML documents.
207 struct DocumentListTraits {
209 // static size_t size(IO &io, T &seq);
210 // static T::value_type& element(IO &io, T &seq, size_t index);
213 /// This class should be specialized by any type that needs to be converted
214 /// to/from a YAML mapping in the case where the names of the keys are not known
215 /// in advance, e.g. a string map.
216 template <typename T>
217 struct CustomMappingTraits {
218 // static void inputOne(IO &io, StringRef key, T &elem);
219 // static void output(IO &io, T &elem);
222 // Only used for better diagnostics of missing traits
223 template <typename T>
226 // Test if ScalarEnumerationTraits<T> is defined on type T.
228 struct has_ScalarEnumerationTraits
230 using Signature_enumeration = void (*)(class IO&, T&);
232 template <typename U>
233 static char test(SameType<Signature_enumeration, &U::enumeration>*);
235 template <typename U>
236 static double test(...);
239 static bool const value =
240 (sizeof(test<ScalarEnumerationTraits<T>>(nullptr)) == 1);
243 // Test if ScalarBitSetTraits<T> is defined on type T.
245 struct has_ScalarBitSetTraits
247 using Signature_bitset = void (*)(class IO&, T&);
249 template <typename U>
250 static char test(SameType<Signature_bitset, &U::bitset>*);
252 template <typename U>
253 static double test(...);
256 static bool const value = (sizeof(test<ScalarBitSetTraits<T>>(nullptr)) == 1);
259 // Test if ScalarTraits<T> is defined on type T.
261 struct has_ScalarTraits
263 using Signature_input = StringRef (*)(StringRef, void*, T&);
264 using Signature_output = void (*)(const T&, void*, raw_ostream&);
265 using Signature_mustQuote = bool (*)(StringRef);
267 template <typename U>
268 static char test(SameType<Signature_input, &U::input> *,
269 SameType<Signature_output, &U::output> *,
270 SameType<Signature_mustQuote, &U::mustQuote> *);
272 template <typename U>
273 static double test(...);
276 static bool const value =
277 (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1);
280 // Test if BlockScalarTraits<T> is defined on type T.
282 struct has_BlockScalarTraits
284 using Signature_input = StringRef (*)(StringRef, void *, T &);
285 using Signature_output = void (*)(const T &, void *, raw_ostream &);
287 template <typename U>
288 static char test(SameType<Signature_input, &U::input> *,
289 SameType<Signature_output, &U::output> *);
291 template <typename U>
292 static double test(...);
295 static bool const value =
296 (sizeof(test<BlockScalarTraits<T>>(nullptr, nullptr)) == 1);
299 // Test if MappingContextTraits<T> is defined on type T.
300 template <class T, class Context> struct has_MappingTraits {
301 using Signature_mapping = void (*)(class IO &, T &, Context &);
303 template <typename U>
304 static char test(SameType<Signature_mapping, &U::mapping>*);
306 template <typename U>
307 static double test(...);
310 static bool const value =
311 (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1);
314 // Test if MappingTraits<T> is defined on type T.
315 template <class T> struct has_MappingTraits<T, EmptyContext> {
316 using Signature_mapping = void (*)(class IO &, T &);
318 template <typename U>
319 static char test(SameType<Signature_mapping, &U::mapping> *);
321 template <typename U> static double test(...);
324 static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1);
327 // Test if MappingContextTraits<T>::validate() is defined on type T.
328 template <class T, class Context> struct has_MappingValidateTraits {
329 using Signature_validate = StringRef (*)(class IO &, T &, Context &);
331 template <typename U>
332 static char test(SameType<Signature_validate, &U::validate>*);
334 template <typename U>
335 static double test(...);
338 static bool const value =
339 (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1);
342 // Test if MappingTraits<T>::validate() is defined on type T.
343 template <class T> struct has_MappingValidateTraits<T, EmptyContext> {
344 using Signature_validate = StringRef (*)(class IO &, T &);
346 template <typename U>
347 static char test(SameType<Signature_validate, &U::validate> *);
349 template <typename U> static double test(...);
352 static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1);
355 // Test if SequenceTraits<T> is defined on type T.
357 struct has_SequenceMethodTraits
359 using Signature_size = size_t (*)(class IO&, T&);
361 template <typename U>
362 static char test(SameType<Signature_size, &U::size>*);
364 template <typename U>
365 static double test(...);
368 static bool const value = (sizeof(test<SequenceTraits<T>>(nullptr)) == 1);
371 // Test if CustomMappingTraits<T> is defined on type T.
373 struct has_CustomMappingTraits
375 using Signature_input = void (*)(IO &io, StringRef key, T &v);
377 template <typename U>
378 static char test(SameType<Signature_input, &U::inputOne>*);
380 template <typename U>
381 static double test(...);
384 static bool const value =
385 (sizeof(test<CustomMappingTraits<T>>(nullptr)) == 1);
388 // has_FlowTraits<int> will cause an error with some compilers because
389 // it subclasses int. Using this wrapper only instantiates the
390 // real has_FlowTraits only if the template type is a class.
391 template <typename T, bool Enabled = std::is_class<T>::value>
395 static const bool value = false;
398 // Some older gcc compilers don't support straight forward tests
399 // for members, so test for ambiguity cause by the base and derived
400 // classes both defining the member.
402 struct has_FlowTraits<T, true>
404 struct Fallback { bool flow; };
405 struct Derived : T, Fallback { };
408 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
411 static char (&f(...))[2];
414 static bool const value = sizeof(f<Derived>(nullptr)) == 2;
417 // Test if SequenceTraits<T> is defined on type T
419 struct has_SequenceTraits : public std::integral_constant<bool,
420 has_SequenceMethodTraits<T>::value > { };
422 // Test if DocumentListTraits<T> is defined on type T
424 struct has_DocumentListTraits
426 using Signature_size = size_t (*)(class IO &, T &);
428 template <typename U>
429 static char test(SameType<Signature_size, &U::size>*);
431 template <typename U>
432 static double test(...);
435 static bool const value = (sizeof(test<DocumentListTraits<T>>(nullptr))==1);
438 inline bool isNumber(StringRef S) {
439 static const char OctalChars[] = "01234567";
440 if (S.startswith("0") &&
441 S.drop_front().find_first_not_of(OctalChars) == StringRef::npos)
444 if (S.startswith("0o") &&
445 S.drop_front(2).find_first_not_of(OctalChars) == StringRef::npos)
448 static const char HexChars[] = "0123456789abcdefABCDEF";
449 if (S.startswith("0x") &&
450 S.drop_front(2).find_first_not_of(HexChars) == StringRef::npos)
453 static const char DecChars[] = "0123456789";
454 if (S.find_first_not_of(DecChars) == StringRef::npos)
457 if (S.equals(".inf") || S.equals(".Inf") || S.equals(".INF"))
460 Regex FloatMatcher("^(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$");
461 if (FloatMatcher.match(S))
467 inline bool isNumeric(StringRef S) {
468 if ((S.front() == '-' || S.front() == '+') && isNumber(S.drop_front()))
474 if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
480 inline bool isNull(StringRef S) {
481 return S.equals("null") || S.equals("Null") || S.equals("NULL") ||
485 inline bool isBool(StringRef S) {
486 return S.equals("true") || S.equals("True") || S.equals("TRUE") ||
487 S.equals("false") || S.equals("False") || S.equals("FALSE");
490 inline bool needsQuotes(StringRef S) {
493 if (isspace(S.front()) || isspace(S.back()))
495 if (S.front() == ',')
498 static const char ScalarSafeChars[] =
499 "abcdefghijklmnopqrstuvwxyz"
500 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
501 if (S.find_first_not_of(ScalarSafeChars) != StringRef::npos)
514 template <typename T, typename Context>
516 : public std::integral_constant<bool,
517 !has_ScalarEnumerationTraits<T>::value &&
518 !has_ScalarBitSetTraits<T>::value &&
519 !has_ScalarTraits<T>::value &&
520 !has_BlockScalarTraits<T>::value &&
521 !has_MappingTraits<T, Context>::value &&
522 !has_SequenceTraits<T>::value &&
523 !has_CustomMappingTraits<T>::value &&
524 !has_DocumentListTraits<T>::value> {};
526 template <typename T, typename Context>
527 struct validatedMappingTraits
528 : public std::integral_constant<
529 bool, has_MappingTraits<T, Context>::value &&
530 has_MappingValidateTraits<T, Context>::value> {};
532 template <typename T, typename Context>
533 struct unvalidatedMappingTraits
534 : public std::integral_constant<
535 bool, has_MappingTraits<T, Context>::value &&
536 !has_MappingValidateTraits<T, Context>::value> {};
538 // Base class for Input and Output.
541 IO(void *Ctxt = nullptr);
544 virtual bool outputting() = 0;
546 virtual unsigned beginSequence() = 0;
547 virtual bool preflightElement(unsigned, void *&) = 0;
548 virtual void postflightElement(void*) = 0;
549 virtual void endSequence() = 0;
550 virtual bool canElideEmptySequence() = 0;
552 virtual unsigned beginFlowSequence() = 0;
553 virtual bool preflightFlowElement(unsigned, void *&) = 0;
554 virtual void postflightFlowElement(void*) = 0;
555 virtual void endFlowSequence() = 0;
557 virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
558 virtual void beginMapping() = 0;
559 virtual void endMapping() = 0;
560 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
561 virtual void postflightKey(void*) = 0;
562 virtual std::vector<StringRef> keys() = 0;
564 virtual void beginFlowMapping() = 0;
565 virtual void endFlowMapping() = 0;
567 virtual void beginEnumScalar() = 0;
568 virtual bool matchEnumScalar(const char*, bool) = 0;
569 virtual bool matchEnumFallback() = 0;
570 virtual void endEnumScalar() = 0;
572 virtual bool beginBitSetScalar(bool &) = 0;
573 virtual bool bitSetMatch(const char*, bool) = 0;
574 virtual void endBitSetScalar() = 0;
576 virtual void scalarString(StringRef &, bool) = 0;
577 virtual void blockScalarString(StringRef &) = 0;
579 virtual void setError(const Twine &) = 0;
581 template <typename T>
582 void enumCase(T &Val, const char* Str, const T ConstVal) {
583 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
588 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
589 template <typename T>
590 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
591 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
596 template <typename FBT, typename T>
597 void enumFallback(T &Val) {
598 if (matchEnumFallback()) {
599 EmptyContext Context;
600 // FIXME: Force integral conversion to allow strong typedefs to convert.
601 FBT Res = static_cast<typename FBT::BaseType>(Val);
602 yamlize(*this, Res, true, Context);
603 Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res));
607 template <typename T>
608 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
609 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
610 Val = static_cast<T>(Val | ConstVal);
614 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
615 template <typename T>
616 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
617 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
618 Val = static_cast<T>(Val | ConstVal);
622 template <typename T>
623 void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) {
624 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
625 Val = Val | ConstVal;
628 template <typename T>
629 void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal,
631 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
632 Val = Val | ConstVal;
636 void setContext(void *);
638 template <typename T> void mapRequired(const char *Key, T &Val) {
640 this->processKey(Key, Val, true, Ctx);
643 template <typename T, typename Context>
644 void mapRequired(const char *Key, T &Val, Context &Ctx) {
645 this->processKey(Key, Val, true, Ctx);
648 template <typename T> void mapOptional(const char *Key, T &Val) {
650 mapOptionalWithContext(Key, Val, Ctx);
653 template <typename T>
654 void mapOptional(const char *Key, T &Val, const T &Default) {
656 mapOptionalWithContext(Key, Val, Default, Ctx);
659 template <typename T, typename Context>
660 typename std::enable_if<has_SequenceTraits<T>::value, void>::type
661 mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
662 // omit key/value instead of outputting empty sequence
663 if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
665 this->processKey(Key, Val, false, Ctx);
668 template <typename T, typename Context>
669 void mapOptionalWithContext(const char *Key, Optional<T> &Val, Context &Ctx) {
670 this->processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false,
674 template <typename T, typename Context>
675 typename std::enable_if<!has_SequenceTraits<T>::value, void>::type
676 mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
677 this->processKey(Key, Val, false, Ctx);
680 template <typename T, typename Context>
681 void mapOptionalWithContext(const char *Key, T &Val, const T &Default,
683 this->processKeyWithDefault(Key, Val, Default, false, Ctx);
687 template <typename T, typename Context>
688 void processKeyWithDefault(const char *Key, Optional<T> &Val,
689 const Optional<T> &DefaultValue, bool Required,
691 assert(DefaultValue.hasValue() == false &&
692 "Optional<T> shouldn't have a value!");
694 bool UseDefault = true;
695 const bool sameAsDefault = outputting() && !Val.hasValue();
696 if (!outputting() && !Val.hasValue())
698 if (Val.hasValue() &&
699 this->preflightKey(Key, Required, sameAsDefault, UseDefault,
701 yamlize(*this, Val.getValue(), Required, Ctx);
702 this->postflightKey(SaveInfo);
709 template <typename T, typename Context>
710 void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue,
711 bool Required, Context &Ctx) {
714 const bool sameAsDefault = outputting() && Val == DefaultValue;
715 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
717 yamlize(*this, Val, Required, Ctx);
718 this->postflightKey(SaveInfo);
726 template <typename T, typename Context>
727 void processKey(const char *Key, T &Val, bool Required, Context &Ctx) {
730 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
731 yamlize(*this, Val, Required, Ctx);
732 this->postflightKey(SaveInfo);
742 template <typename T, typename Context>
743 void doMapping(IO &io, T &Val, Context &Ctx) {
744 MappingContextTraits<T, Context>::mapping(io, Val, Ctx);
747 template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) {
748 MappingTraits<T>::mapping(io, Val);
751 } // end namespace detail
753 template <typename T>
754 typename std::enable_if<has_ScalarEnumerationTraits<T>::value, void>::type
755 yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
756 io.beginEnumScalar();
757 ScalarEnumerationTraits<T>::enumeration(io, Val);
761 template <typename T>
762 typename std::enable_if<has_ScalarBitSetTraits<T>::value, void>::type
763 yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
765 if ( io.beginBitSetScalar(DoClear) ) {
767 Val = static_cast<T>(0);
768 ScalarBitSetTraits<T>::bitset(io, Val);
769 io.endBitSetScalar();
773 template <typename T>
774 typename std::enable_if<has_ScalarTraits<T>::value, void>::type
775 yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
776 if ( io.outputting() ) {
778 raw_string_ostream Buffer(Storage);
779 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
780 StringRef Str = Buffer.str();
781 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
785 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
786 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
787 if ( !Result.empty() ) {
788 io.setError(Twine(Result));
793 template <typename T>
794 typename std::enable_if<has_BlockScalarTraits<T>::value, void>::type
795 yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) {
796 if (YamlIO.outputting()) {
798 raw_string_ostream Buffer(Storage);
799 BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
800 StringRef Str = Buffer.str();
801 YamlIO.blockScalarString(Str);
804 YamlIO.blockScalarString(Str);
806 BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val);
808 YamlIO.setError(Twine(Result));
812 template <typename T, typename Context>
813 typename std::enable_if<validatedMappingTraits<T, Context>::value, void>::type
814 yamlize(IO &io, T &Val, bool, Context &Ctx) {
815 if (has_FlowTraits<MappingTraits<T>>::value)
816 io.beginFlowMapping();
819 if (io.outputting()) {
820 StringRef Err = MappingTraits<T>::validate(io, Val);
822 errs() << Err << "\n";
823 assert(Err.empty() && "invalid struct trying to be written as yaml");
826 detail::doMapping(io, Val, Ctx);
827 if (!io.outputting()) {
828 StringRef Err = MappingTraits<T>::validate(io, Val);
832 if (has_FlowTraits<MappingTraits<T>>::value)
838 template <typename T, typename Context>
839 typename std::enable_if<unvalidatedMappingTraits<T, Context>::value, void>::type
840 yamlize(IO &io, T &Val, bool, Context &Ctx) {
841 if (has_FlowTraits<MappingTraits<T>>::value) {
842 io.beginFlowMapping();
843 detail::doMapping(io, Val, Ctx);
847 detail::doMapping(io, Val, Ctx);
852 template <typename T>
853 typename std::enable_if<has_CustomMappingTraits<T>::value, void>::type
854 yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
855 if ( io.outputting() ) {
857 CustomMappingTraits<T>::output(io, Val);
861 for (StringRef key : io.keys())
862 CustomMappingTraits<T>::inputOne(io, key, Val);
867 template <typename T>
868 typename std::enable_if<missingTraits<T, EmptyContext>::value, void>::type
869 yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
870 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
873 template <typename T, typename Context>
874 typename std::enable_if<has_SequenceTraits<T>::value, void>::type
875 yamlize(IO &io, T &Seq, bool, Context &Ctx) {
876 if ( has_FlowTraits< SequenceTraits<T>>::value ) {
877 unsigned incnt = io.beginFlowSequence();
878 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
879 for(unsigned i=0; i < count; ++i) {
881 if ( io.preflightFlowElement(i, SaveInfo) ) {
882 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
883 io.postflightFlowElement(SaveInfo);
886 io.endFlowSequence();
889 unsigned incnt = io.beginSequence();
890 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
891 for(unsigned i=0; i < count; ++i) {
893 if ( io.preflightElement(i, SaveInfo) ) {
894 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
895 io.postflightElement(SaveInfo);
903 struct ScalarTraits<bool> {
904 static void output(const bool &, void* , raw_ostream &);
905 static StringRef input(StringRef, void *, bool &);
906 static bool mustQuote(StringRef) { return false; }
910 struct ScalarTraits<StringRef> {
911 static void output(const StringRef &, void *, raw_ostream &);
912 static StringRef input(StringRef, void *, StringRef &);
913 static bool mustQuote(StringRef S) { return needsQuotes(S); }
917 struct ScalarTraits<std::string> {
918 static void output(const std::string &, void *, raw_ostream &);
919 static StringRef input(StringRef, void *, std::string &);
920 static bool mustQuote(StringRef S) { return needsQuotes(S); }
924 struct ScalarTraits<uint8_t> {
925 static void output(const uint8_t &, void *, raw_ostream &);
926 static StringRef input(StringRef, void *, uint8_t &);
927 static bool mustQuote(StringRef) { return false; }
931 struct ScalarTraits<uint16_t> {
932 static void output(const uint16_t &, void *, raw_ostream &);
933 static StringRef input(StringRef, void *, uint16_t &);
934 static bool mustQuote(StringRef) { return false; }
938 struct ScalarTraits<uint32_t> {
939 static void output(const uint32_t &, void *, raw_ostream &);
940 static StringRef input(StringRef, void *, uint32_t &);
941 static bool mustQuote(StringRef) { return false; }
945 struct ScalarTraits<uint64_t> {
946 static void output(const uint64_t &, void *, raw_ostream &);
947 static StringRef input(StringRef, void *, uint64_t &);
948 static bool mustQuote(StringRef) { return false; }
952 struct ScalarTraits<int8_t> {
953 static void output(const int8_t &, void *, raw_ostream &);
954 static StringRef input(StringRef, void *, int8_t &);
955 static bool mustQuote(StringRef) { return false; }
959 struct ScalarTraits<int16_t> {
960 static void output(const int16_t &, void *, raw_ostream &);
961 static StringRef input(StringRef, void *, int16_t &);
962 static bool mustQuote(StringRef) { return false; }
966 struct ScalarTraits<int32_t> {
967 static void output(const int32_t &, void *, raw_ostream &);
968 static StringRef input(StringRef, void *, int32_t &);
969 static bool mustQuote(StringRef) { return false; }
973 struct ScalarTraits<int64_t> {
974 static void output(const int64_t &, void *, raw_ostream &);
975 static StringRef input(StringRef, void *, int64_t &);
976 static bool mustQuote(StringRef) { return false; }
980 struct ScalarTraits<float> {
981 static void output(const float &, void *, raw_ostream &);
982 static StringRef input(StringRef, void *, float &);
983 static bool mustQuote(StringRef) { return false; }
987 struct ScalarTraits<double> {
988 static void output(const double &, void *, raw_ostream &);
989 static StringRef input(StringRef, void *, double &);
990 static bool mustQuote(StringRef) { return false; }
993 // For endian types, we just use the existing ScalarTraits for the underlying
994 // type. This way endian aware types are supported whenever a ScalarTraits
995 // is defined for the underlying type.
996 template <typename value_type, support::endianness endian, size_t alignment>
997 struct ScalarTraits<support::detail::packed_endian_specific_integral<
998 value_type, endian, alignment>> {
1000 support::detail::packed_endian_specific_integral<value_type, endian,
1003 static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) {
1004 ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream);
1007 static StringRef input(StringRef Str, void *Ctx, endian_type &E) {
1009 auto R = ScalarTraits<value_type>::input(Str, Ctx, V);
1010 E = static_cast<endian_type>(V);
1014 static bool mustQuote(StringRef Str) {
1015 return ScalarTraits<value_type>::mustQuote(Str);
1019 // Utility for use within MappingTraits<>::mapping() method
1020 // to [de]normalize an object for use with YAML conversion.
1021 template <typename TNorm, typename TFinal>
1022 struct MappingNormalization {
1023 MappingNormalization(IO &i_o, TFinal &Obj)
1024 : io(i_o), BufPtr(nullptr), Result(Obj) {
1025 if ( io.outputting() ) {
1026 BufPtr = new (&Buffer) TNorm(io, Obj);
1029 BufPtr = new (&Buffer) TNorm(io);
1033 ~MappingNormalization() {
1034 if ( ! io.outputting() ) {
1035 Result = BufPtr->denormalize(io);
1040 TNorm* operator->() { return BufPtr; }
1043 using Storage = AlignedCharArrayUnion<TNorm>;
1051 // Utility for use within MappingTraits<>::mapping() method
1052 // to [de]normalize an object for use with YAML conversion.
1053 template <typename TNorm, typename TFinal>
1054 struct MappingNormalizationHeap {
1055 MappingNormalizationHeap(IO &i_o, TFinal &Obj, BumpPtrAllocator *allocator)
1056 : io(i_o), Result(Obj) {
1057 if ( io.outputting() ) {
1058 BufPtr = new (&Buffer) TNorm(io, Obj);
1060 else if (allocator) {
1061 BufPtr = allocator->Allocate<TNorm>();
1062 new (BufPtr) TNorm(io);
1064 BufPtr = new TNorm(io);
1068 ~MappingNormalizationHeap() {
1069 if ( io.outputting() ) {
1073 Result = BufPtr->denormalize(io);
1077 TNorm* operator->() { return BufPtr; }
1080 using Storage = AlignedCharArrayUnion<TNorm>;
1084 TNorm *BufPtr = nullptr;
1089 /// The Input class is used to parse a yaml document into in-memory structs
1092 /// It works by using YAMLParser to do a syntax parse of the entire yaml
1093 /// document, then the Input class builds a graph of HNodes which wraps
1094 /// each yaml Node. The extra layer is buffering. The low level yaml
1095 /// parser only lets you look at each node once. The buffering layer lets
1096 /// you search and interate multiple times. This is necessary because
1097 /// the mapRequired() method calls may not be in the same order
1098 /// as the keys in the document.
1100 class Input : public IO {
1102 // Construct a yaml Input object from a StringRef and optional
1103 // user-data. The DiagHandler can be specified to provide
1104 // alternative error reporting.
1105 Input(StringRef InputContent,
1106 void *Ctxt = nullptr,
1107 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
1108 void *DiagHandlerCtxt = nullptr);
1111 // Check if there was an syntax or semantic error during parsing.
1112 std::error_code error();
1115 bool outputting() override;
1116 bool mapTag(StringRef, bool) override;
1117 void beginMapping() override;
1118 void endMapping() override;
1119 bool preflightKey(const char *, bool, bool, bool &, void *&) override;
1120 void postflightKey(void *) override;
1121 std::vector<StringRef> keys() override;
1122 void beginFlowMapping() override;
1123 void endFlowMapping() override;
1124 unsigned beginSequence() override;
1125 void endSequence() override;
1126 bool preflightElement(unsigned index, void *&) override;
1127 void postflightElement(void *) override;
1128 unsigned beginFlowSequence() override;
1129 bool preflightFlowElement(unsigned , void *&) override;
1130 void postflightFlowElement(void *) override;
1131 void endFlowSequence() override;
1132 void beginEnumScalar() override;
1133 bool matchEnumScalar(const char*, bool) override;
1134 bool matchEnumFallback() override;
1135 void endEnumScalar() override;
1136 bool beginBitSetScalar(bool &) override;
1137 bool bitSetMatch(const char *, bool ) override;
1138 void endBitSetScalar() override;
1139 void scalarString(StringRef &, bool) override;
1140 void blockScalarString(StringRef &) override;
1141 void setError(const Twine &message) override;
1142 bool canElideEmptySequence() override;
1145 virtual void anchor();
1148 HNode(Node *n) : _node(n) { }
1149 virtual ~HNode() = default;
1151 static inline bool classof(const HNode *) { return true; }
1156 class EmptyHNode : public HNode {
1157 void anchor() override;
1160 EmptyHNode(Node *n) : HNode(n) { }
1162 static inline bool classof(const HNode *n) {
1163 return NullNode::classof(n->_node);
1166 static inline bool classof(const EmptyHNode *) { return true; }
1169 class ScalarHNode : public HNode {
1170 void anchor() override;
1173 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
1175 StringRef value() const { return _value; }
1177 static inline bool classof(const HNode *n) {
1178 return ScalarNode::classof(n->_node) ||
1179 BlockScalarNode::classof(n->_node);
1182 static inline bool classof(const ScalarHNode *) { return true; }
1188 class MapHNode : public HNode {
1189 void anchor() override;
1192 MapHNode(Node *n) : HNode(n) { }
1194 static inline bool classof(const HNode *n) {
1195 return MappingNode::classof(n->_node);
1198 static inline bool classof(const MapHNode *) { return true; }
1200 using NameToNode = StringMap<std::unique_ptr<HNode>>;
1203 SmallVector<std::string, 6> ValidKeys;
1206 class SequenceHNode : public HNode {
1207 void anchor() override;
1210 SequenceHNode(Node *n) : HNode(n) { }
1212 static inline bool classof(const HNode *n) {
1213 return SequenceNode::classof(n->_node);
1216 static inline bool classof(const SequenceHNode *) { return true; }
1218 std::vector<std::unique_ptr<HNode>> Entries;
1221 std::unique_ptr<Input::HNode> createHNodes(Node *node);
1222 void setError(HNode *hnode, const Twine &message);
1223 void setError(Node *node, const Twine &message);
1226 // These are only used by operator>>. They could be private
1227 // if those templated things could be made friends.
1228 bool setCurrentDocument();
1229 bool nextDocument();
1231 /// Returns the current node that's being parsed by the YAML Parser.
1232 const Node *getCurrentNode() const;
1235 SourceMgr SrcMgr; // must be before Strm
1236 std::unique_ptr<llvm::yaml::Stream> Strm;
1237 std::unique_ptr<HNode> TopNode;
1239 BumpPtrAllocator StringAllocator;
1240 document_iterator DocIterator;
1241 std::vector<bool> BitValuesUsed;
1242 HNode *CurrentNode = nullptr;
1243 bool ScalarMatchFound;
1247 /// The Output class is used to generate a yaml document from in-memory structs
1250 class Output : public IO {
1252 Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
1255 /// \brief Set whether or not to output optional values which are equal
1256 /// to the default value. By default, when outputting if you attempt
1257 /// to write a value that is equal to the default, the value gets ignored.
1258 /// Sometimes, it is useful to be able to see these in the resulting YAML
1260 void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; }
1262 bool outputting() override;
1263 bool mapTag(StringRef, bool) override;
1264 void beginMapping() override;
1265 void endMapping() override;
1266 bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
1267 void postflightKey(void *) override;
1268 std::vector<StringRef> keys() override;
1269 void beginFlowMapping() override;
1270 void endFlowMapping() override;
1271 unsigned beginSequence() override;
1272 void endSequence() override;
1273 bool preflightElement(unsigned, void *&) override;
1274 void postflightElement(void *) override;
1275 unsigned beginFlowSequence() override;
1276 bool preflightFlowElement(unsigned, void *&) override;
1277 void postflightFlowElement(void *) override;
1278 void endFlowSequence() override;
1279 void beginEnumScalar() override;
1280 bool matchEnumScalar(const char*, bool) override;
1281 bool matchEnumFallback() override;
1282 void endEnumScalar() override;
1283 bool beginBitSetScalar(bool &) override;
1284 bool bitSetMatch(const char *, bool ) override;
1285 void endBitSetScalar() override;
1286 void scalarString(StringRef &, bool) override;
1287 void blockScalarString(StringRef &) override;
1288 void setError(const Twine &message) override;
1289 bool canElideEmptySequence() override;
1291 // These are only used by operator<<. They could be private
1292 // if that templated operator could be made a friend.
1293 void beginDocuments();
1294 bool preflightDocument(unsigned);
1295 void postflightDocument();
1296 void endDocuments();
1299 void output(StringRef s);
1300 void outputUpToEndOfLine(StringRef s);
1301 void newLineCheck();
1302 void outputNewLine();
1303 void paddedKey(StringRef key);
1304 void flowKey(StringRef Key);
1317 SmallVector<InState, 8> StateStack;
1319 int ColumnAtFlowStart = 0;
1320 int ColumnAtMapFlowStart = 0;
1321 bool NeedBitValueComma = false;
1322 bool NeedFlowSequenceComma = false;
1323 bool EnumerationMatchFound = false;
1324 bool NeedsNewLine = false;
1325 bool WriteDefaultValues = false;
1328 /// YAML I/O does conversion based on types. But often native data types
1329 /// are just a typedef of built in intergral types (e.g. int). But the C++
1330 /// type matching system sees through the typedef and all the typedefed types
1331 /// look like a built in type. This will cause the generic YAML I/O conversion
1332 /// to be used. To provide better control over the YAML conversion, you can
1333 /// use this macro instead of typedef. It will create a class with one field
1334 /// and automatic conversion operators to and from the base type.
1335 /// Based on BOOST_STRONG_TYPEDEF
1336 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
1338 _type() = default; \
1339 _type(const _base v) : value(v) {} \
1340 _type(const _type &v) = default; \
1341 _type &operator=(const _type &rhs) = default; \
1342 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
1343 operator const _base & () const { return value; } \
1344 bool operator==(const _type &rhs) const { return value == rhs.value; } \
1345 bool operator==(const _base &rhs) const { return value == rhs; } \
1346 bool operator<(const _type &rhs) const { return value < rhs.value; } \
1348 using BaseType = _base; \
1352 /// Use these types instead of uintXX_t in any mapping to have
1353 /// its yaml output formatted as hexadecimal.
1355 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
1356 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
1357 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
1358 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
1361 struct ScalarTraits<Hex8> {
1362 static void output(const Hex8 &, void *, raw_ostream &);
1363 static StringRef input(StringRef, void *, Hex8 &);
1364 static bool mustQuote(StringRef) { return false; }
1368 struct ScalarTraits<Hex16> {
1369 static void output(const Hex16 &, void *, raw_ostream &);
1370 static StringRef input(StringRef, void *, Hex16 &);
1371 static bool mustQuote(StringRef) { return false; }
1375 struct ScalarTraits<Hex32> {
1376 static void output(const Hex32 &, void *, raw_ostream &);
1377 static StringRef input(StringRef, void *, Hex32 &);
1378 static bool mustQuote(StringRef) { return false; }
1382 struct ScalarTraits<Hex64> {
1383 static void output(const Hex64 &, void *, raw_ostream &);
1384 static StringRef input(StringRef, void *, Hex64 &);
1385 static bool mustQuote(StringRef) { return false; }
1388 // Define non-member operator>> so that Input can stream in a document list.
1389 template <typename T>
1391 typename std::enable_if<has_DocumentListTraits<T>::value, Input &>::type
1392 operator>>(Input &yin, T &docList) {
1395 while ( yin.setCurrentDocument() ) {
1396 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx);
1405 // Define non-member operator>> so that Input can stream in a map as a document.
1406 template <typename T>
1407 inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value,
1409 operator>>(Input &yin, T &docMap) {
1411 yin.setCurrentDocument();
1412 yamlize(yin, docMap, true, Ctx);
1416 // Define non-member operator>> so that Input can stream in a sequence as
1418 template <typename T>
1420 typename std::enable_if<has_SequenceTraits<T>::value, Input &>::type
1421 operator>>(Input &yin, T &docSeq) {
1423 if (yin.setCurrentDocument())
1424 yamlize(yin, docSeq, true, Ctx);
1428 // Define non-member operator>> so that Input can stream in a block scalar.
1429 template <typename T>
1431 typename std::enable_if<has_BlockScalarTraits<T>::value, Input &>::type
1432 operator>>(Input &In, T &Val) {
1434 if (In.setCurrentDocument())
1435 yamlize(In, Val, true, Ctx);
1439 // Define non-member operator>> so that Input can stream in a string map.
1440 template <typename T>
1442 typename std::enable_if<has_CustomMappingTraits<T>::value, Input &>::type
1443 operator>>(Input &In, T &Val) {
1445 if (In.setCurrentDocument())
1446 yamlize(In, Val, true, Ctx);
1450 // Provide better error message about types missing a trait specialization
1451 template <typename T>
1452 inline typename std::enable_if<missingTraits<T, EmptyContext>::value,
1454 operator>>(Input &yin, T &docSeq) {
1455 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1459 // Define non-member operator<< so that Output can stream out document list.
1460 template <typename T>
1462 typename std::enable_if<has_DocumentListTraits<T>::value, Output &>::type
1463 operator<<(Output &yout, T &docList) {
1465 yout.beginDocuments();
1466 const size_t count = DocumentListTraits<T>::size(yout, docList);
1467 for(size_t i=0; i < count; ++i) {
1468 if ( yout.preflightDocument(i) ) {
1469 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true,
1471 yout.postflightDocument();
1474 yout.endDocuments();
1478 // Define non-member operator<< so that Output can stream out a map.
1479 template <typename T>
1480 inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value,
1482 operator<<(Output &yout, T &map) {
1484 yout.beginDocuments();
1485 if ( yout.preflightDocument(0) ) {
1486 yamlize(yout, map, true, Ctx);
1487 yout.postflightDocument();
1489 yout.endDocuments();
1493 // Define non-member operator<< so that Output can stream out a sequence.
1494 template <typename T>
1496 typename std::enable_if<has_SequenceTraits<T>::value, Output &>::type
1497 operator<<(Output &yout, T &seq) {
1499 yout.beginDocuments();
1500 if ( yout.preflightDocument(0) ) {
1501 yamlize(yout, seq, true, Ctx);
1502 yout.postflightDocument();
1504 yout.endDocuments();
1508 // Define non-member operator<< so that Output can stream out a block scalar.
1509 template <typename T>
1511 typename std::enable_if<has_BlockScalarTraits<T>::value, Output &>::type
1512 operator<<(Output &Out, T &Val) {
1514 Out.beginDocuments();
1515 if (Out.preflightDocument(0)) {
1516 yamlize(Out, Val, true, Ctx);
1517 Out.postflightDocument();
1523 // Define non-member operator<< so that Output can stream out a string map.
1524 template <typename T>
1526 typename std::enable_if<has_CustomMappingTraits<T>::value, Output &>::type
1527 operator<<(Output &Out, T &Val) {
1529 Out.beginDocuments();
1530 if (Out.preflightDocument(0)) {
1531 yamlize(Out, Val, true, Ctx);
1532 Out.postflightDocument();
1538 // Provide better error message about types missing a trait specialization
1539 template <typename T>
1540 inline typename std::enable_if<missingTraits<T, EmptyContext>::value,
1542 operator<<(Output &yout, T &seq) {
1543 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1547 template <typename T> struct SequenceTraitsImpl {
1548 using _type = typename T::value_type;
1550 static size_t size(IO &io, T &seq) { return seq.size(); }
1552 static _type &element(IO &io, T &seq, size_t index) {
1553 if (index >= seq.size())
1554 seq.resize(index + 1);
1559 /// Implementation of CustomMappingTraits for std::map<std::string, T>.
1560 template <typename T> struct StdMapStringCustomMappingTraitsImpl {
1561 using map_type = std::map<std::string, T>;
1563 static void inputOne(IO &io, StringRef key, map_type &v) {
1564 io.mapRequired(key.str().c_str(), v[key]);
1567 static void output(IO &io, map_type &v) {
1569 io.mapRequired(p.first.c_str(), p.second);
1573 } // end namespace yaml
1574 } // end namespace llvm
1576 /// Utility for declaring that a std::vector of a particular type
1577 /// should be considered a YAML sequence.
1578 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1582 struct SequenceTraits<std::vector<_type>> \
1583 : public SequenceTraitsImpl<std::vector<_type>> {}; \
1584 template <unsigned N> \
1585 struct SequenceTraits<SmallVector<_type, N>> \
1586 : public SequenceTraitsImpl<SmallVector<_type, N>> {}; \
1590 /// Utility for declaring that a std::vector of a particular type
1591 /// should be considered a YAML flow sequence.
1592 /// We need to do a partial specialization on the vector version, not a full.
1593 /// If this is a full specialization, the compiler is a bit too "smart" and
1594 /// decides to warn on -Wunused-const-variable. This workaround can be
1595 /// removed and we can do a full specialization on std::vector<T> once
1596 /// PR28878 is fixed.
1597 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1600 template <unsigned N> \
1601 struct SequenceTraits<SmallVector<_type, N>> \
1602 : public SequenceTraitsImpl<SmallVector<_type, N>> { \
1603 static const bool flow = true; \
1605 template <typename Allocator> \
1606 struct SequenceTraits<std::vector<_type, Allocator>> \
1607 : public SequenceTraitsImpl<std::vector<_type, Allocator>> { \
1608 static const bool flow = true; \
1613 #define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type) \
1616 template <> struct MappingTraits<Type> { \
1617 static void mapping(IO &IO, Type &Obj); \
1622 #define LLVM_YAML_DECLARE_ENUM_TRAITS(Type) \
1625 template <> struct ScalarEnumerationTraits<Type> { \
1626 static void enumeration(IO &io, Type &Value); \
1631 #define LLVM_YAML_DECLARE_BITSET_TRAITS(Type) \
1634 template <> struct ScalarBitSetTraits<Type> { \
1635 static void bitset(IO &IO, Type &Options); \
1640 #define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote) \
1643 template <> struct ScalarTraits<Type> { \
1644 static void output(const Type &Value, void *ctx, raw_ostream &Out); \
1645 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
1646 static bool mustQuote(StringRef) { return MustQuote; } \
1651 /// Utility for declaring that a std::vector of a particular type
1652 /// should be considered a YAML document list.
1653 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1656 template <unsigned N> \
1657 struct DocumentListTraits<SmallVector<_type, N>> \
1658 : public SequenceTraitsImpl<SmallVector<_type, N>> {}; \
1660 struct DocumentListTraits<std::vector<_type>> \
1661 : public SequenceTraitsImpl<std::vector<_type>> {}; \
1665 /// Utility for declaring that std::map<std::string, _type> should be considered
1667 #define LLVM_YAML_IS_STRING_MAP(_type) \
1671 struct CustomMappingTraits<std::map<std::string, _type>> \
1672 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \
1676 #endif // LLVM_SUPPORT_YAMLTRAITS_H