1 //===- llvm/Supporrt/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
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseMapInfo.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/YAMLParser.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
26 #include "llvm/Support/type_traits.h"
33 /// This class should be specialized by any type that needs to be converted
34 /// to/from a YAML mapping. For example:
36 /// struct ScalarBitSetTraits<MyStruct> {
37 /// static void mapping(IO &io, MyStruct &s) {
38 /// io.mapRequired("name", s.name);
39 /// io.mapRequired("size", s.size);
40 /// io.mapOptional("age", s.age);
44 struct MappingTraits {
46 // static void mapping(IO &io, T &fields);
50 /// This class should be specialized by any integral type that converts
51 /// to/from a YAML scalar where there is a one-to-one mapping between
52 /// in-memory values and a string in YAML. For example:
54 /// struct ScalarEnumerationTraits<Colors> {
55 /// static void enumeration(IO &io, Colors &value) {
56 /// io.enumCase(value, "red", cRed);
57 /// io.enumCase(value, "blue", cBlue);
58 /// io.enumCase(value, "green", cGreen);
62 struct ScalarEnumerationTraits {
64 // static void enumeration(IO &io, T &value);
68 /// This class should be specialized by any integer type that is a union
69 /// of bit values and the YAML representation is a flow sequence of
70 /// strings. For example:
72 /// struct ScalarBitSetTraits<MyFlags> {
73 /// static void bitset(IO &io, MyFlags &value) {
74 /// io.bitSetCase(value, "big", flagBig);
75 /// io.bitSetCase(value, "flat", flagFlat);
76 /// io.bitSetCase(value, "round", flagRound);
80 struct ScalarBitSetTraits {
82 // static void bitset(IO &io, T &value);
86 /// This class should be specialized by type that requires custom conversion
87 /// to/from a yaml scalar. For example:
90 /// struct ScalarTraits<MyType> {
91 /// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
92 /// // stream out custom formatting
93 /// out << llvm::format("%x", val);
95 /// static StringRef input(StringRef scalar, void*, MyType &value) {
96 /// // parse scalar and set `value`
97 /// // return empty string on success, or error string
98 /// return StringRef();
102 struct ScalarTraits {
105 // Function to write the value as a string:
106 //static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
108 // Function to convert a string to a value. Returns the empty
109 // StringRef on success or an error string if string is malformed:
110 //static StringRef input(StringRef scalar, void *ctxt, T &value);
114 /// This class should be specialized by any type that needs to be converted
115 /// to/from a YAML sequence. For example:
118 /// struct SequenceTraits< std::vector<MyType> > {
119 /// static size_t size(IO &io, std::vector<MyType> &seq) {
120 /// return seq.size();
122 /// static MyType& element(IO &, std::vector<MyType> &seq, size_t index) {
123 /// if ( index >= seq.size() )
124 /// seq.resize(index+1);
125 /// return seq[index];
129 struct SequenceTraits {
131 // static size_t size(IO &io, T &seq);
132 // static T::value_type& element(IO &io, T &seq, size_t index);
134 // The following is option and will cause generated YAML to use
135 // a flow sequence (e.g. [a,b,c]).
136 // static const bool flow = true;
140 /// This class should be specialized by any type that needs to be converted
141 /// to/from a list of YAML documents.
143 struct DocumentListTraits {
145 // static size_t size(IO &io, T &seq);
146 // static T::value_type& element(IO &io, T &seq, size_t index);
150 // Only used by compiler if both template types are the same
151 template <typename T, T>
154 // Only used for better diagnostics of missing traits
155 template <typename T>
160 // Test if ScalarEnumerationTraits<T> is defined on type T.
162 struct has_ScalarEnumerationTraits
164 typedef void (*Signature_enumeration)(class IO&, T&);
166 template <typename U>
167 static char test(SameType<Signature_enumeration, &U::enumeration>*);
169 template <typename U>
170 static double test(...);
173 static bool const value = (sizeof(test<ScalarEnumerationTraits<T> >(0)) == 1);
177 // Test if ScalarBitSetTraits<T> is defined on type T.
179 struct has_ScalarBitSetTraits
181 typedef void (*Signature_bitset)(class IO&, T&);
183 template <typename U>
184 static char test(SameType<Signature_bitset, &U::bitset>*);
186 template <typename U>
187 static double test(...);
190 static bool const value = (sizeof(test<ScalarBitSetTraits<T> >(0)) == 1);
194 // Test if ScalarTraits<T> is defined on type T.
196 struct has_ScalarTraits
198 typedef StringRef (*Signature_input)(StringRef, void*, T&);
199 typedef void (*Signature_output)(const T&, void*, llvm::raw_ostream&);
201 template <typename U>
202 static char test(SameType<Signature_input, &U::input>*,
203 SameType<Signature_output, &U::output>*);
205 template <typename U>
206 static double test(...);
209 static bool const value = (sizeof(test<ScalarTraits<T> >(0,0)) == 1);
213 // Test if MappingTraits<T> is defined on type T.
215 struct has_MappingTraits
217 typedef void (*Signature_mapping)(class IO&, T&);
219 template <typename U>
220 static char test(SameType<Signature_mapping, &U::mapping>*);
222 template <typename U>
223 static double test(...);
226 static bool const value = (sizeof(test<MappingTraits<T> >(0)) == 1);
230 // Test if SequenceTraits<T> is defined on type T.
232 struct has_SequenceMethodTraits
234 typedef size_t (*Signature_size)(class IO&, T&);
236 template <typename U>
237 static char test(SameType<Signature_size, &U::size>*);
239 template <typename U>
240 static double test(...);
243 static bool const value = (sizeof(test<SequenceTraits<T> >(0)) == 1);
247 // has_FlowTraits<int> will cause an error with some compilers because
248 // it subclasses int. Using this wrapper only instantiates the
249 // real has_FlowTraits only if the template type is a class.
250 template <typename T, bool Enabled = llvm::is_class<T>::value>
254 static const bool value = false;
257 // Some older gcc compilers don't support straight forward tests
258 // for members, so test for ambiguity cause by the base and derived
259 // classes both defining the member.
261 struct has_FlowTraits<T, true>
263 struct Fallback { bool flow; };
264 struct Derived : T, Fallback { };
267 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
270 static char (&f(...))[2];
273 static bool const value = sizeof(f<Derived>(0)) == 2;
278 // Test if SequenceTraits<T> is defined on type T
280 struct has_SequenceTraits : public llvm::integral_constant<bool,
281 has_SequenceMethodTraits<T>::value > { };
284 // Test if DocumentListTraits<T> is defined on type T
286 struct has_DocumentListTraits
288 typedef size_t (*Signature_size)(class IO&, T&);
290 template <typename U>
291 static char test(SameType<Signature_size, &U::size>*);
293 template <typename U>
294 static double test(...);
297 static bool const value = (sizeof(test<DocumentListTraits<T> >(0)) == 1);
304 struct missingTraits : public llvm::integral_constant<bool,
305 !has_ScalarEnumerationTraits<T>::value
306 && !has_ScalarBitSetTraits<T>::value
307 && !has_ScalarTraits<T>::value
308 && !has_MappingTraits<T>::value
309 && !has_SequenceTraits<T>::value
310 && !has_DocumentListTraits<T>::value > {};
313 // Base class for Input and Output.
320 virtual bool outputting() = 0;
322 virtual unsigned beginSequence() = 0;
323 virtual bool preflightElement(unsigned, void *&) = 0;
324 virtual void postflightElement(void*) = 0;
325 virtual void endSequence() = 0;
326 virtual bool canElideEmptySequence() = 0;
328 virtual unsigned beginFlowSequence() = 0;
329 virtual bool preflightFlowElement(unsigned, void *&) = 0;
330 virtual void postflightFlowElement(void*) = 0;
331 virtual void endFlowSequence() = 0;
333 virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
334 virtual void beginMapping() = 0;
335 virtual void endMapping() = 0;
336 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
337 virtual void postflightKey(void*) = 0;
339 virtual void beginEnumScalar() = 0;
340 virtual bool matchEnumScalar(const char*, bool) = 0;
341 virtual void endEnumScalar() = 0;
343 virtual bool beginBitSetScalar(bool &) = 0;
344 virtual bool bitSetMatch(const char*, bool) = 0;
345 virtual void endBitSetScalar() = 0;
347 virtual void scalarString(StringRef &) = 0;
349 virtual void setError(const Twine &) = 0;
351 template <typename T>
352 void enumCase(T &Val, const char* Str, const T ConstVal) {
353 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
358 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
359 template <typename T>
360 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
361 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
366 template <typename T>
367 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
368 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
369 Val = Val | ConstVal;
373 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
374 template <typename T>
375 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
376 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
377 Val = Val | ConstVal;
382 void setContext(void *);
384 template <typename T>
385 void mapRequired(const char* Key, T& Val) {
386 this->processKey(Key, Val, true);
389 template <typename T>
390 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
391 mapOptional(const char* Key, T& Val) {
392 // omit key/value instead of outputting empty sequence
393 if ( this->canElideEmptySequence() && !(Val.begin() != Val.end()) )
395 this->processKey(Key, Val, false);
398 template <typename T>
399 typename llvm::enable_if_c<!has_SequenceTraits<T>::value,void>::type
400 mapOptional(const char* Key, T& Val) {
401 this->processKey(Key, Val, false);
404 template <typename T>
405 void mapOptional(const char* Key, T& Val, const T& Default) {
406 this->processKeyWithDefault(Key, Val, Default, false);
410 template <typename T>
411 void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,
415 const bool sameAsDefault = outputting() && Val == DefaultValue;
416 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
418 yamlize(*this, Val, Required);
419 this->postflightKey(SaveInfo);
427 template <typename T>
428 void processKey(const char *Key, T &Val, bool Required) {
431 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
432 yamlize(*this, Val, Required);
433 this->postflightKey(SaveInfo);
444 typename llvm::enable_if_c<has_ScalarEnumerationTraits<T>::value,void>::type
445 yamlize(IO &io, T &Val, bool) {
446 io.beginEnumScalar();
447 ScalarEnumerationTraits<T>::enumeration(io, Val);
452 typename llvm::enable_if_c<has_ScalarBitSetTraits<T>::value,void>::type
453 yamlize(IO &io, T &Val, bool) {
455 if ( io.beginBitSetScalar(DoClear) ) {
457 Val = static_cast<T>(0);
458 ScalarBitSetTraits<T>::bitset(io, Val);
459 io.endBitSetScalar();
465 typename llvm::enable_if_c<has_ScalarTraits<T>::value,void>::type
466 yamlize(IO &io, T &Val, bool) {
467 if ( io.outputting() ) {
469 llvm::raw_string_ostream Buffer(Storage);
470 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
471 StringRef Str = Buffer.str();
472 io.scalarString(Str);
476 io.scalarString(Str);
477 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
478 if ( !Result.empty() ) {
479 io.setError(llvm::Twine(Result));
486 typename llvm::enable_if_c<has_MappingTraits<T>::value, void>::type
487 yamlize(IO &io, T &Val, bool) {
489 MappingTraits<T>::mapping(io, Val);
494 typename llvm::enable_if_c<missingTraits<T>::value, void>::type
495 yamlize(IO &io, T &Val, bool) {
496 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
500 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
501 yamlize(IO &io, T &Seq, bool) {
502 if ( has_FlowTraits< SequenceTraits<T> >::value ) {
503 unsigned incnt = io.beginFlowSequence();
504 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
505 for(unsigned i=0; i < count; ++i) {
507 if ( io.preflightFlowElement(i, SaveInfo) ) {
508 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
509 io.postflightFlowElement(SaveInfo);
512 io.endFlowSequence();
515 unsigned incnt = io.beginSequence();
516 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
517 for(unsigned i=0; i < count; ++i) {
519 if ( io.preflightElement(i, SaveInfo) ) {
520 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
521 io.postflightElement(SaveInfo);
530 struct ScalarTraits<bool> {
531 static void output(const bool &, void*, llvm::raw_ostream &);
532 static StringRef input(StringRef, void*, bool &);
536 struct ScalarTraits<StringRef> {
537 static void output(const StringRef &, void*, llvm::raw_ostream &);
538 static StringRef input(StringRef, void*, StringRef &);
542 struct ScalarTraits<std::string> {
543 static void output(const std::string &, void*, llvm::raw_ostream &);
544 static StringRef input(StringRef, void*, std::string &);
548 struct ScalarTraits<uint8_t> {
549 static void output(const uint8_t &, void*, llvm::raw_ostream &);
550 static StringRef input(StringRef, void*, uint8_t &);
554 struct ScalarTraits<uint16_t> {
555 static void output(const uint16_t &, void*, llvm::raw_ostream &);
556 static StringRef input(StringRef, void*, uint16_t &);
560 struct ScalarTraits<uint32_t> {
561 static void output(const uint32_t &, void*, llvm::raw_ostream &);
562 static StringRef input(StringRef, void*, uint32_t &);
566 struct ScalarTraits<uint64_t> {
567 static void output(const uint64_t &, void*, llvm::raw_ostream &);
568 static StringRef input(StringRef, void*, uint64_t &);
572 struct ScalarTraits<int8_t> {
573 static void output(const int8_t &, void*, llvm::raw_ostream &);
574 static StringRef input(StringRef, void*, int8_t &);
578 struct ScalarTraits<int16_t> {
579 static void output(const int16_t &, void*, llvm::raw_ostream &);
580 static StringRef input(StringRef, void*, int16_t &);
584 struct ScalarTraits<int32_t> {
585 static void output(const int32_t &, void*, llvm::raw_ostream &);
586 static StringRef input(StringRef, void*, int32_t &);
590 struct ScalarTraits<int64_t> {
591 static void output(const int64_t &, void*, llvm::raw_ostream &);
592 static StringRef input(StringRef, void*, int64_t &);
596 struct ScalarTraits<float> {
597 static void output(const float &, void*, llvm::raw_ostream &);
598 static StringRef input(StringRef, void*, float &);
602 struct ScalarTraits<double> {
603 static void output(const double &, void*, llvm::raw_ostream &);
604 static StringRef input(StringRef, void*, double &);
609 // Utility for use within MappingTraits<>::mapping() method
610 // to [de]normalize an object for use with YAML conversion.
611 template <typename TNorm, typename TFinal>
612 struct MappingNormalization {
613 MappingNormalization(IO &i_o, TFinal &Obj)
614 : io(i_o), BufPtr(NULL), Result(Obj) {
615 if ( io.outputting() ) {
616 BufPtr = new (&Buffer) TNorm(io, Obj);
619 BufPtr = new (&Buffer) TNorm(io);
623 ~MappingNormalization() {
624 if ( ! io.outputting() ) {
625 Result = BufPtr->denormalize(io);
630 TNorm* operator->() { return BufPtr; }
633 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
643 // Utility for use within MappingTraits<>::mapping() method
644 // to [de]normalize an object for use with YAML conversion.
645 template <typename TNorm, typename TFinal>
646 struct MappingNormalizationHeap {
647 MappingNormalizationHeap(IO &i_o, TFinal &Obj)
648 : io(i_o), BufPtr(NULL), Result(Obj) {
649 if ( io.outputting() ) {
650 BufPtr = new (&Buffer) TNorm(io, Obj);
653 BufPtr = new TNorm(io);
657 ~MappingNormalizationHeap() {
658 if ( io.outputting() ) {
662 Result = BufPtr->denormalize(io);
666 TNorm* operator->() { return BufPtr; }
669 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
680 /// The Input class is used to parse a yaml document into in-memory structs
683 /// It works by using YAMLParser to do a syntax parse of the entire yaml
684 /// document, then the Input class builds a graph of HNodes which wraps
685 /// each yaml Node. The extra layer is buffering. The low level yaml
686 /// parser only lets you look at each node once. The buffering layer lets
687 /// you search and interate multiple times. This is necessary because
688 /// the mapRequired() method calls may not be in the same order
689 /// as the keys in the document.
691 class Input : public IO {
693 // Construct a yaml Input object from a StringRef and optional
694 // user-data. The DiagHandler can be specified to provide
695 // alternative error reporting.
696 Input(StringRef InputContent,
698 SourceMgr::DiagHandlerTy DiagHandler = NULL,
699 void *DiagHandlerCtxt = NULL);
702 // Check if there was an syntax or semantic error during parsing.
703 llvm::error_code error();
706 virtual bool outputting();
707 virtual bool mapTag(StringRef, bool);
708 virtual void beginMapping();
709 virtual void endMapping();
710 virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
711 virtual void postflightKey(void *);
712 virtual unsigned beginSequence();
713 virtual void endSequence();
714 virtual bool preflightElement(unsigned index, void *&);
715 virtual void postflightElement(void *);
716 virtual unsigned beginFlowSequence();
717 virtual bool preflightFlowElement(unsigned , void *&);
718 virtual void postflightFlowElement(void *);
719 virtual void endFlowSequence();
720 virtual void beginEnumScalar();
721 virtual bool matchEnumScalar(const char*, bool);
722 virtual void endEnumScalar();
723 virtual bool beginBitSetScalar(bool &);
724 virtual bool bitSetMatch(const char *, bool );
725 virtual void endBitSetScalar();
726 virtual void scalarString(StringRef &);
727 virtual void setError(const Twine &message);
728 virtual bool canElideEmptySequence();
731 virtual void anchor();
733 HNode(Node *n) : _node(n) { }
735 static inline bool classof(const HNode *) { return true; }
740 class EmptyHNode : public HNode {
741 virtual void anchor();
743 EmptyHNode(Node *n) : HNode(n) { }
744 static inline bool classof(const HNode *n) {
745 return NullNode::classof(n->_node);
747 static inline bool classof(const EmptyHNode *) { return true; }
750 class ScalarHNode : public HNode {
751 virtual void anchor();
753 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
755 StringRef value() const { return _value; }
757 static inline bool classof(const HNode *n) {
758 return ScalarNode::classof(n->_node);
760 static inline bool classof(const ScalarHNode *) { return true; }
765 class MapHNode : public HNode {
767 MapHNode(Node *n) : HNode(n) { }
770 static inline bool classof(const HNode *n) {
771 return MappingNode::classof(n->_node);
773 static inline bool classof(const MapHNode *) { return true; }
775 typedef llvm::StringMap<HNode*> NameToNode;
777 bool isValidKey(StringRef key);
780 llvm::SmallVector<const char*, 6> ValidKeys;
783 class SequenceHNode : public HNode {
785 SequenceHNode(Node *n) : HNode(n) { }
786 virtual ~SequenceHNode();
788 static inline bool classof(const HNode *n) {
789 return SequenceNode::classof(n->_node);
791 static inline bool classof(const SequenceHNode *) { return true; }
793 std::vector<HNode*> Entries;
796 Input::HNode *createHNodes(Node *node);
797 void setError(HNode *hnode, const Twine &message);
798 void setError(Node *node, const Twine &message);
802 // These are only used by operator>>. They could be private
803 // if those templated things could be made friends.
804 bool setCurrentDocument();
808 llvm::SourceMgr SrcMgr; // must be before Strm
809 OwningPtr<llvm::yaml::Stream> Strm;
810 OwningPtr<HNode> TopNode;
812 llvm::BumpPtrAllocator StringAllocator;
813 llvm::yaml::document_iterator DocIterator;
814 std::vector<bool> BitValuesUsed;
816 bool ScalarMatchFound;
823 /// The Output class is used to generate a yaml document from in-memory structs
826 class Output : public IO {
828 Output(llvm::raw_ostream &, void *Ctxt=NULL);
831 virtual bool outputting();
832 virtual bool mapTag(StringRef, bool);
833 virtual void beginMapping();
834 virtual void endMapping();
835 virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
836 virtual void postflightKey(void *);
837 virtual unsigned beginSequence();
838 virtual void endSequence();
839 virtual bool preflightElement(unsigned, void *&);
840 virtual void postflightElement(void *);
841 virtual unsigned beginFlowSequence();
842 virtual bool preflightFlowElement(unsigned, void *&);
843 virtual void postflightFlowElement(void *);
844 virtual void endFlowSequence();
845 virtual void beginEnumScalar();
846 virtual bool matchEnumScalar(const char*, bool);
847 virtual void endEnumScalar();
848 virtual bool beginBitSetScalar(bool &);
849 virtual bool bitSetMatch(const char *, bool );
850 virtual void endBitSetScalar();
851 virtual void scalarString(StringRef &);
852 virtual void setError(const Twine &message);
853 virtual bool canElideEmptySequence();
855 // These are only used by operator<<. They could be private
856 // if that templated operator could be made a friend.
857 void beginDocuments();
858 bool preflightDocument(unsigned);
859 void postflightDocument();
863 void output(StringRef s);
864 void outputUpToEndOfLine(StringRef s);
866 void outputNewLine();
867 void paddedKey(StringRef key);
869 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
871 llvm::raw_ostream &Out;
872 SmallVector<InState, 8> StateStack;
874 int ColumnAtFlowStart;
875 bool NeedBitValueComma;
876 bool NeedFlowSequenceComma;
877 bool EnumerationMatchFound;
884 /// YAML I/O does conversion based on types. But often native data types
885 /// are just a typedef of built in intergral types (e.g. int). But the C++
886 /// type matching system sees through the typedef and all the typedefed types
887 /// look like a built in type. This will cause the generic YAML I/O conversion
888 /// to be used. To provide better control over the YAML conversion, you can
889 /// use this macro instead of typedef. It will create a class with one field
890 /// and automatic conversion operators to and from the base type.
891 /// Based on BOOST_STRONG_TYPEDEF
892 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
895 _type(const _base v) : value(v) { } \
896 _type(const _type &v) : value(v.value) {} \
897 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
898 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
899 operator const _base & () const { return value; } \
900 bool operator==(const _type &rhs) const { return value == rhs.value; } \
901 bool operator==(const _base &rhs) const { return value == rhs; } \
902 bool operator<(const _type &rhs) const { return value < rhs.value; } \
909 /// Use these types instead of uintXX_t in any mapping to have
910 /// its yaml output formatted as hexadecimal.
912 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
913 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
914 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
915 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
919 struct ScalarTraits<Hex8> {
920 static void output(const Hex8 &, void*, llvm::raw_ostream &);
921 static StringRef input(StringRef, void*, Hex8 &);
925 struct ScalarTraits<Hex16> {
926 static void output(const Hex16 &, void*, llvm::raw_ostream &);
927 static StringRef input(StringRef, void*, Hex16 &);
931 struct ScalarTraits<Hex32> {
932 static void output(const Hex32 &, void*, llvm::raw_ostream &);
933 static StringRef input(StringRef, void*, Hex32 &);
937 struct ScalarTraits<Hex64> {
938 static void output(const Hex64 &, void*, llvm::raw_ostream &);
939 static StringRef input(StringRef, void*, Hex64 &);
943 // Define non-member operator>> so that Input can stream in a document list.
944 template <typename T>
946 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
947 operator>>(Input &yin, T &docList) {
949 while ( yin.setCurrentDocument() ) {
950 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
959 // Define non-member operator>> so that Input can stream in a map as a document.
960 template <typename T>
962 typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
963 operator>>(Input &yin, T &docMap) {
964 yin.setCurrentDocument();
965 yamlize(yin, docMap, true);
969 // Define non-member operator>> so that Input can stream in a sequence as
971 template <typename T>
973 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
974 operator>>(Input &yin, T &docSeq) {
975 if (yin.setCurrentDocument())
976 yamlize(yin, docSeq, true);
980 // Provide better error message about types missing a trait specialization
981 template <typename T>
983 typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
984 operator>>(Input &yin, T &docSeq) {
985 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
990 // Define non-member operator<< so that Output can stream out document list.
991 template <typename T>
993 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
994 operator<<(Output &yout, T &docList) {
995 yout.beginDocuments();
996 const size_t count = DocumentListTraits<T>::size(yout, docList);
997 for(size_t i=0; i < count; ++i) {
998 if ( yout.preflightDocument(i) ) {
999 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
1000 yout.postflightDocument();
1003 yout.endDocuments();
1007 // Define non-member operator<< so that Output can stream out a map.
1008 template <typename T>
1010 typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
1011 operator<<(Output &yout, T &map) {
1012 yout.beginDocuments();
1013 if ( yout.preflightDocument(0) ) {
1014 yamlize(yout, map, true);
1015 yout.postflightDocument();
1017 yout.endDocuments();
1021 // Define non-member operator<< so that Output can stream out a sequence.
1022 template <typename T>
1024 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
1025 operator<<(Output &yout, T &seq) {
1026 yout.beginDocuments();
1027 if ( yout.preflightDocument(0) ) {
1028 yamlize(yout, seq, true);
1029 yout.postflightDocument();
1031 yout.endDocuments();
1035 // Provide better error message about types missing a trait specialization
1036 template <typename T>
1038 typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
1039 operator<<(Output &yout, T &seq) {
1040 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1049 /// Utility for declaring that a std::vector of a particular type
1050 /// should be considered a YAML sequence.
1051 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1055 struct SequenceTraits< std::vector<_type> > { \
1056 static size_t size(IO &io, std::vector<_type> &seq) { \
1057 return seq.size(); \
1059 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1060 if ( index >= seq.size() ) \
1061 seq.resize(index+1); \
1062 return seq[index]; \
1068 /// Utility for declaring that a std::vector of a particular type
1069 /// should be considered a YAML flow sequence.
1070 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1074 struct SequenceTraits< std::vector<_type> > { \
1075 static size_t size(IO &io, std::vector<_type> &seq) { \
1076 return seq.size(); \
1078 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1079 if ( index >= seq.size() ) \
1080 seq.resize(index+1); \
1081 return seq[index]; \
1083 static const bool flow = true; \
1088 /// Utility for declaring that a std::vector of a particular type
1089 /// should be considered a YAML document list.
1090 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1094 struct DocumentListTraits< std::vector<_type> > { \
1095 static size_t size(IO &io, std::vector<_type> &seq) { \
1096 return seq.size(); \
1098 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1099 if ( index >= seq.size() ) \
1100 seq.resize(index+1); \
1101 return seq[index]; \
1109 #endif // LLVM_SUPPORT_YAMLTRAITS_H