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/Casting.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/YAMLParser.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/system_error.h"
27 #include "llvm/Support/type_traits.h"
34 /// This class should be specialized by any type that needs to be converted
35 /// to/from a YAML mapping. For example:
37 /// struct ScalarBitSetTraits<MyStruct> {
38 /// static void mapping(IO &io, MyStruct &s) {
39 /// io.mapRequired("name", s.name);
40 /// io.mapRequired("size", s.size);
41 /// io.mapOptional("age", s.age);
45 struct MappingTraits {
47 // static void mapping(IO &io, T &fields);
51 /// This class should be specialized by any integral type that converts
52 /// to/from a YAML scalar where there is a one-to-one mapping between
53 /// in-memory values and a string in YAML. For example:
55 /// struct ScalarEnumerationTraits<Colors> {
56 /// static void enumeration(IO &io, Colors &value) {
57 /// io.enumCase(value, "red", cRed);
58 /// io.enumCase(value, "blue", cBlue);
59 /// io.enumCase(value, "green", cGreen);
63 struct ScalarEnumerationTraits {
65 // static void enumeration(IO &io, T &value);
69 /// This class should be specialized by any integer type that is a union
70 /// of bit values and the YAML representation is a flow sequence of
71 /// strings. For example:
73 /// struct ScalarBitSetTraits<MyFlags> {
74 /// static void bitset(IO &io, MyFlags &value) {
75 /// io.bitSetCase(value, "big", flagBig);
76 /// io.bitSetCase(value, "flat", flagFlat);
77 /// io.bitSetCase(value, "round", flagRound);
81 struct ScalarBitSetTraits {
83 // static void bitset(IO &io, T &value);
87 /// This class should be specialized by type that requires custom conversion
88 /// to/from a yaml scalar. For example:
91 /// struct ScalarTraits<MyType> {
92 /// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
93 /// // stream out custom formatting
94 /// out << llvm::format("%x", val);
96 /// static StringRef input(StringRef scalar, void*, MyType &value) {
97 /// // parse scalar and set `value`
98 /// // return empty string on success, or error string
99 /// return StringRef();
103 struct ScalarTraits {
106 // Function to write the value as a string:
107 //static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
109 // Function to convert a string to a value. Returns the empty
110 // StringRef on success or an error string if string is malformed:
111 //static StringRef input(StringRef scalar, void *ctxt, T &value);
115 /// This class should be specialized by any type that needs to be converted
116 /// to/from a YAML sequence. For example:
119 /// struct SequenceTraits< std::vector<MyType> > {
120 /// static size_t size(IO &io, std::vector<MyType> &seq) {
121 /// return seq.size();
123 /// static MyType& element(IO &, std::vector<MyType> &seq, size_t index) {
124 /// if ( index >= seq.size() )
125 /// seq.resize(index+1);
126 /// return seq[index];
130 struct SequenceTraits {
132 // static size_t size(IO &io, T &seq);
133 // static T::value_type& element(IO &io, T &seq, size_t index);
135 // The following is option and will cause generated YAML to use
136 // a flow sequence (e.g. [a,b,c]).
137 // static const bool flow = true;
141 /// This class should be specialized by any type that needs to be converted
142 /// to/from a list of YAML documents.
144 struct DocumentListTraits {
146 // static size_t size(IO &io, T &seq);
147 // static T::value_type& element(IO &io, T &seq, size_t index);
151 // Only used by compiler if both template types are the same
152 template <typename T, T>
155 // Only used for better diagnostics of missing traits
156 template <typename T>
161 // Test if ScalarEnumerationTraits<T> is defined on type T.
163 struct has_ScalarEnumerationTraits
165 typedef void (*Signature_enumeration)(class IO&, T&);
167 template <typename U>
168 static char test(SameType<Signature_enumeration, &U::enumeration>*);
170 template <typename U>
171 static double test(...);
174 static bool const value = (sizeof(test<ScalarEnumerationTraits<T> >(0)) == 1);
178 // Test if ScalarBitSetTraits<T> is defined on type T.
180 struct has_ScalarBitSetTraits
182 typedef void (*Signature_bitset)(class IO&, T&);
184 template <typename U>
185 static char test(SameType<Signature_bitset, &U::bitset>*);
187 template <typename U>
188 static double test(...);
191 static bool const value = (sizeof(test<ScalarBitSetTraits<T> >(0)) == 1);
195 // Test if ScalarTraits<T> is defined on type T.
197 struct has_ScalarTraits
199 typedef StringRef (*Signature_input)(StringRef, void*, T&);
200 typedef void (*Signature_output)(const T&, void*, llvm::raw_ostream&);
202 template <typename U>
203 static char test(SameType<Signature_input, &U::input>*,
204 SameType<Signature_output, &U::output>*);
206 template <typename U>
207 static double test(...);
210 static bool const value = (sizeof(test<ScalarTraits<T> >(0,0)) == 1);
214 // Test if MappingTraits<T> is defined on type T.
216 struct has_MappingTraits
218 typedef void (*Signature_mapping)(class IO&, T&);
220 template <typename U>
221 static char test(SameType<Signature_mapping, &U::mapping>*);
223 template <typename U>
224 static double test(...);
227 static bool const value = (sizeof(test<MappingTraits<T> >(0)) == 1);
231 // Test if SequenceTraits<T> is defined on type T.
233 struct has_SequenceMethodTraits
235 typedef size_t (*Signature_size)(class IO&, T&);
237 template <typename U>
238 static char test(SameType<Signature_size, &U::size>*);
240 template <typename U>
241 static double test(...);
244 static bool const value = (sizeof(test<SequenceTraits<T> >(0)) == 1);
248 // has_FlowTraits<int> will cause an error with some compilers because
249 // it subclasses int. Using this wrapper only instantiates the
250 // real has_FlowTraits only if the template type is a class.
251 template <typename T, bool Enabled = llvm::is_class<T>::value>
255 static const bool value = false;
258 // Some older gcc compilers don't support straight forward tests
259 // for members, so test for ambiguity cause by the base and derived
260 // classes both defining the member.
262 struct has_FlowTraits<T, true>
264 struct Fallback { bool flow; };
265 struct Derived : T, Fallback { };
268 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
271 static char (&f(...))[2];
274 static bool const value = sizeof(f<Derived>(0)) == 2;
279 // Test if SequenceTraits<T> is defined on type T
281 struct has_SequenceTraits : public llvm::integral_constant<bool,
282 has_SequenceMethodTraits<T>::value > { };
285 // Test if DocumentListTraits<T> is defined on type T
287 struct has_DocumentListTraits
289 typedef size_t (*Signature_size)(class IO&, T&);
291 template <typename U>
292 static char test(SameType<Signature_size, &U::size>*);
294 template <typename U>
295 static double test(...);
298 static bool const value = (sizeof(test<DocumentListTraits<T> >(0)) == 1);
305 struct missingTraits : public llvm::integral_constant<bool,
306 !has_ScalarEnumerationTraits<T>::value
307 && !has_ScalarBitSetTraits<T>::value
308 && !has_ScalarTraits<T>::value
309 && !has_MappingTraits<T>::value
310 && !has_SequenceTraits<T>::value
311 && !has_DocumentListTraits<T>::value > {};
314 // Base class for Input and Output.
321 virtual bool outputting() const = 0;
323 virtual unsigned beginSequence() = 0;
324 virtual bool preflightElement(unsigned, void *&) = 0;
325 virtual void postflightElement(void*) = 0;
326 virtual void endSequence() = 0;
327 virtual bool canElideEmptySequence() = 0;
329 virtual unsigned beginFlowSequence() = 0;
330 virtual bool preflightFlowElement(unsigned, void *&) = 0;
331 virtual void postflightFlowElement(void*) = 0;
332 virtual void endFlowSequence() = 0;
334 virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
335 virtual void beginMapping() = 0;
336 virtual void endMapping() = 0;
337 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
338 virtual void postflightKey(void*) = 0;
340 virtual void beginEnumScalar() = 0;
341 virtual bool matchEnumScalar(const char*, bool) = 0;
342 virtual void endEnumScalar() = 0;
344 virtual bool beginBitSetScalar(bool &) = 0;
345 virtual bool bitSetMatch(const char*, bool) = 0;
346 virtual void endBitSetScalar() = 0;
348 virtual void scalarString(StringRef &) = 0;
350 virtual void setError(const Twine &) = 0;
352 template <typename T>
353 void enumCase(T &Val, const char* Str, const T ConstVal) {
354 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
359 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
360 template <typename T>
361 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
362 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
367 template <typename T>
368 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
369 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
370 Val = Val | ConstVal;
374 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
375 template <typename T>
376 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
377 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
378 Val = Val | ConstVal;
383 void setContext(void *);
385 template <typename T>
386 void mapRequired(const char* Key, T& Val) {
387 this->processKey(Key, Val, true);
390 template <typename T>
391 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
392 mapOptional(const char* Key, T& Val) {
393 // omit key/value instead of outputting empty sequence
394 if ( this->canElideEmptySequence() && !(Val.begin() != Val.end()) )
396 this->processKey(Key, Val, false);
399 template <typename T>
400 typename llvm::enable_if_c<!has_SequenceTraits<T>::value,void>::type
401 mapOptional(const char* Key, T& Val) {
402 this->processKey(Key, Val, false);
405 template <typename T>
406 void mapOptional(const char* Key, T& Val, const T& Default) {
407 this->processKeyWithDefault(Key, Val, Default, false);
411 template <typename T>
412 void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,
416 const bool sameAsDefault = outputting() && Val == DefaultValue;
417 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
419 yamlize(*this, Val, Required);
420 this->postflightKey(SaveInfo);
428 template <typename T>
429 void processKey(const char *Key, T &Val, bool Required) {
432 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
433 yamlize(*this, Val, Required);
434 this->postflightKey(SaveInfo);
445 typename llvm::enable_if_c<has_ScalarEnumerationTraits<T>::value,void>::type
446 yamlize(IO &io, T &Val, bool) {
447 io.beginEnumScalar();
448 ScalarEnumerationTraits<T>::enumeration(io, Val);
453 typename llvm::enable_if_c<has_ScalarBitSetTraits<T>::value,void>::type
454 yamlize(IO &io, T &Val, bool) {
456 if ( io.beginBitSetScalar(DoClear) ) {
458 Val = static_cast<T>(0);
459 ScalarBitSetTraits<T>::bitset(io, Val);
460 io.endBitSetScalar();
466 typename llvm::enable_if_c<has_ScalarTraits<T>::value,void>::type
467 yamlize(IO &io, T &Val, bool) {
468 if ( io.outputting() ) {
470 llvm::raw_string_ostream Buffer(Storage);
471 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
472 StringRef Str = Buffer.str();
473 io.scalarString(Str);
477 io.scalarString(Str);
478 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
479 if ( !Result.empty() ) {
480 io.setError(llvm::Twine(Result));
487 typename llvm::enable_if_c<has_MappingTraits<T>::value, void>::type
488 yamlize(IO &io, T &Val, bool) {
490 MappingTraits<T>::mapping(io, Val);
495 typename llvm::enable_if_c<missingTraits<T>::value, void>::type
496 yamlize(IO &io, T &Val, bool) {
497 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
501 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
502 yamlize(IO &io, T &Seq, bool) {
503 if ( has_FlowTraits< SequenceTraits<T> >::value ) {
504 unsigned incnt = io.beginFlowSequence();
505 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
506 for(unsigned i=0; i < count; ++i) {
508 if ( io.preflightFlowElement(i, SaveInfo) ) {
509 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
510 io.postflightFlowElement(SaveInfo);
513 io.endFlowSequence();
516 unsigned incnt = io.beginSequence();
517 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
518 for(unsigned i=0; i < count; ++i) {
520 if ( io.preflightElement(i, SaveInfo) ) {
521 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
522 io.postflightElement(SaveInfo);
531 struct ScalarTraits<bool> {
532 static void output(const bool &, void*, llvm::raw_ostream &);
533 static StringRef input(StringRef, void*, bool &);
537 struct ScalarTraits<StringRef> {
538 static void output(const StringRef &, void*, llvm::raw_ostream &);
539 static StringRef input(StringRef, void*, StringRef &);
543 struct ScalarTraits<uint8_t> {
544 static void output(const uint8_t &, void*, llvm::raw_ostream &);
545 static StringRef input(StringRef, void*, uint8_t &);
549 struct ScalarTraits<uint16_t> {
550 static void output(const uint16_t &, void*, llvm::raw_ostream &);
551 static StringRef input(StringRef, void*, uint16_t &);
555 struct ScalarTraits<uint32_t> {
556 static void output(const uint32_t &, void*, llvm::raw_ostream &);
557 static StringRef input(StringRef, void*, uint32_t &);
561 struct ScalarTraits<uint64_t> {
562 static void output(const uint64_t &, void*, llvm::raw_ostream &);
563 static StringRef input(StringRef, void*, uint64_t &);
567 struct ScalarTraits<int8_t> {
568 static void output(const int8_t &, void*, llvm::raw_ostream &);
569 static StringRef input(StringRef, void*, int8_t &);
573 struct ScalarTraits<int16_t> {
574 static void output(const int16_t &, void*, llvm::raw_ostream &);
575 static StringRef input(StringRef, void*, int16_t &);
579 struct ScalarTraits<int32_t> {
580 static void output(const int32_t &, void*, llvm::raw_ostream &);
581 static StringRef input(StringRef, void*, int32_t &);
585 struct ScalarTraits<int64_t> {
586 static void output(const int64_t &, void*, llvm::raw_ostream &);
587 static StringRef input(StringRef, void*, int64_t &);
591 struct ScalarTraits<float> {
592 static void output(const float &, void*, llvm::raw_ostream &);
593 static StringRef input(StringRef, void*, float &);
597 struct ScalarTraits<double> {
598 static void output(const double &, void*, llvm::raw_ostream &);
599 static StringRef input(StringRef, void*, double &);
604 // Utility for use within MappingTraits<>::mapping() method
605 // to [de]normalize an object for use with YAML conversion.
606 template <typename TNorm, typename TFinal>
607 struct MappingNormalization {
608 MappingNormalization(IO &i_o, TFinal &Obj)
609 : io(i_o), BufPtr(NULL), Result(Obj) {
610 if ( io.outputting() ) {
611 BufPtr = new (&Buffer) TNorm(io, Obj);
614 BufPtr = new (&Buffer) TNorm(io);
618 ~MappingNormalization() {
619 if ( ! io.outputting() ) {
620 Result = BufPtr->denormalize(io);
625 TNorm* operator->() { return BufPtr; }
628 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
638 // Utility for use within MappingTraits<>::mapping() method
639 // to [de]normalize an object for use with YAML conversion.
640 template <typename TNorm, typename TFinal>
641 struct MappingNormalizationHeap {
642 MappingNormalizationHeap(IO &i_o, TFinal &Obj)
643 : io(i_o), BufPtr(NULL), Result(Obj) {
644 if ( io.outputting() ) {
645 BufPtr = new (&Buffer) TNorm(io, Obj);
648 BufPtr = new TNorm(io);
652 ~MappingNormalizationHeap() {
653 if ( io.outputting() ) {
657 Result = BufPtr->denormalize(io);
661 TNorm* operator->() { return BufPtr; }
664 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
675 /// The Input class is used to parse a yaml document into in-memory structs
678 /// It works by using YAMLParser to do a syntax parse of the entire yaml
679 /// document, then the Input class builds a graph of HNodes which wraps
680 /// each yaml Node. The extra layer is buffering. The low level yaml
681 /// parser only lets you look at each node once. The buffering layer lets
682 /// you search and interate multiple times. This is necessary because
683 /// the mapRequired() method calls may not be in the same order
684 /// as the keys in the document.
686 class Input : public IO {
688 // Construct a yaml Input object from a StringRef and optional
689 // user-data. The DiagHandler can be specified to provide
690 // alternative error reporting.
691 Input(StringRef InputContent,
693 SourceMgr::DiagHandlerTy DiagHandler = NULL,
694 void *DiagHandlerCtxt = NULL);
697 // Check if there was an syntax or semantic error during parsing.
698 llvm::error_code error();
700 static bool classof(const IO *io) { return !io->outputting(); }
703 virtual bool outputting() const;
704 virtual bool mapTag(StringRef, bool);
705 virtual void beginMapping();
706 virtual void endMapping();
707 virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
708 virtual void postflightKey(void *);
709 virtual unsigned beginSequence();
710 virtual void endSequence();
711 virtual bool preflightElement(unsigned index, void *&);
712 virtual void postflightElement(void *);
713 virtual unsigned beginFlowSequence();
714 virtual bool preflightFlowElement(unsigned , void *&);
715 virtual void postflightFlowElement(void *);
716 virtual void endFlowSequence();
717 virtual void beginEnumScalar();
718 virtual bool matchEnumScalar(const char*, bool);
719 virtual void endEnumScalar();
720 virtual bool beginBitSetScalar(bool &);
721 virtual bool bitSetMatch(const char *, bool );
722 virtual void endBitSetScalar();
723 virtual void scalarString(StringRef &);
724 virtual void setError(const Twine &message);
725 virtual bool canElideEmptySequence();
729 HNode(Node *n) : _node(n) { }
731 static inline bool classof(const HNode *) { return true; }
736 class EmptyHNode : public HNode {
738 EmptyHNode(Node *n) : HNode(n) { }
739 virtual ~EmptyHNode() {}
740 static inline bool classof(const HNode *n) {
741 return NullNode::classof(n->_node);
743 static inline bool classof(const EmptyHNode *) { return true; }
746 class ScalarHNode : public HNode {
748 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
749 virtual ~ScalarHNode() { }
751 StringRef value() const { return _value; }
753 static inline bool classof(const HNode *n) {
754 return ScalarNode::classof(n->_node);
756 static inline bool classof(const ScalarHNode *) { return true; }
761 class MapHNode : public HNode {
763 MapHNode(Node *n) : HNode(n) { }
766 static inline bool classof(const HNode *n) {
767 return MappingNode::classof(n->_node);
769 static inline bool classof(const MapHNode *) { return true; }
771 typedef llvm::StringMap<HNode*> NameToNode;
773 bool isValidKey(StringRef key);
776 llvm::SmallVector<const char*, 6> ValidKeys;
779 class SequenceHNode : public HNode {
781 SequenceHNode(Node *n) : HNode(n) { }
782 virtual ~SequenceHNode();
784 static inline bool classof(const HNode *n) {
785 return SequenceNode::classof(n->_node);
787 static inline bool classof(const SequenceHNode *) { return true; }
789 std::vector<HNode*> Entries;
792 Input::HNode *createHNodes(Node *node);
793 void setError(HNode *hnode, const Twine &message);
794 void setError(Node *node, const Twine &message);
798 // These are only used by operator>>. They could be private
799 // if those templated things could be made friends.
800 bool setCurrentDocument();
804 llvm::SourceMgr SrcMgr; // must be before Strm
805 OwningPtr<llvm::yaml::Stream> Strm;
806 OwningPtr<HNode> TopNode;
808 llvm::BumpPtrAllocator StringAllocator;
809 llvm::yaml::document_iterator DocIterator;
810 std::vector<bool> BitValuesUsed;
812 bool ScalarMatchFound;
819 /// The Output class is used to generate a yaml document from in-memory structs
822 class Output : public IO {
824 Output(llvm::raw_ostream &, void *Ctxt=NULL);
827 static bool classof(const IO *io) { return io->outputting(); }
829 virtual bool outputting() const;
830 virtual bool mapTag(StringRef, bool);
831 virtual void beginMapping();
832 virtual void endMapping();
833 virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
834 virtual void postflightKey(void *);
835 virtual unsigned beginSequence();
836 virtual void endSequence();
837 virtual bool preflightElement(unsigned, void *&);
838 virtual void postflightElement(void *);
839 virtual unsigned beginFlowSequence();
840 virtual bool preflightFlowElement(unsigned, void *&);
841 virtual void postflightFlowElement(void *);
842 virtual void endFlowSequence();
843 virtual void beginEnumScalar();
844 virtual bool matchEnumScalar(const char*, bool);
845 virtual void endEnumScalar();
846 virtual bool beginBitSetScalar(bool &);
847 virtual bool bitSetMatch(const char *, bool );
848 virtual void endBitSetScalar();
849 virtual void scalarString(StringRef &);
850 virtual void setError(const Twine &message);
851 virtual bool canElideEmptySequence();
853 // These are only used by operator<<. They could be private
854 // if that templated operator could be made a friend.
855 void beginDocuments();
856 bool preflightDocument(unsigned);
857 void postflightDocument();
861 void output(StringRef s);
862 void outputUpToEndOfLine(StringRef s);
864 void outputNewLine();
865 void paddedKey(StringRef key);
867 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
869 llvm::raw_ostream &Out;
870 SmallVector<InState, 8> StateStack;
872 int ColumnAtFlowStart;
873 bool NeedBitValueComma;
874 bool NeedFlowSequenceComma;
875 bool EnumerationMatchFound;
882 /// YAML I/O does conversion based on types. But often native data types
883 /// are just a typedef of built in intergral types (e.g. int). But the C++
884 /// type matching system sees through the typedef and all the typedefed types
885 /// look like a built in type. This will cause the generic YAML I/O conversion
886 /// to be used. To provide better control over the YAML conversion, you can
887 /// use this macro instead of typedef. It will create a class with one field
888 /// and automatic conversion operators to and from the base type.
889 /// Based on BOOST_STRONG_TYPEDEF
890 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
893 _type(const _base v) : value(v) { } \
894 _type(const _type &v) : value(v.value) {} \
895 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
896 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
897 operator const _base & () const { return value; } \
898 bool operator==(const _type &rhs) const { return value == rhs.value; } \
899 bool operator==(const _base &rhs) const { return value == rhs; } \
900 bool operator<(const _type &rhs) const { return value < rhs.value; } \
907 /// Use these types instead of uintXX_t in any mapping to have
908 /// its yaml output formatted as hexadecimal.
910 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
911 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
912 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
913 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
917 struct ScalarTraits<Hex8> {
918 static void output(const Hex8 &, void*, llvm::raw_ostream &);
919 static StringRef input(StringRef, void*, Hex8 &);
923 struct ScalarTraits<Hex16> {
924 static void output(const Hex16 &, void*, llvm::raw_ostream &);
925 static StringRef input(StringRef, void*, Hex16 &);
929 struct ScalarTraits<Hex32> {
930 static void output(const Hex32 &, void*, llvm::raw_ostream &);
931 static StringRef input(StringRef, void*, Hex32 &);
935 struct ScalarTraits<Hex64> {
936 static void output(const Hex64 &, void*, llvm::raw_ostream &);
937 static StringRef input(StringRef, void*, Hex64 &);
941 // Define non-member operator>> so that Input can stream in a document list.
942 template <typename T>
944 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
945 operator>>(Input &yin, T &docList) {
947 while ( yin.setCurrentDocument() ) {
948 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
957 // Define non-member operator>> so that Input can stream in a map as a document.
958 template <typename T>
960 typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
961 operator>>(Input &yin, T &docMap) {
962 yin.setCurrentDocument();
963 yamlize(yin, docMap, true);
967 // Define non-member operator>> so that Input can stream in a sequence as
969 template <typename T>
971 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
972 operator>>(Input &yin, T &docSeq) {
973 if (yin.setCurrentDocument())
974 yamlize(yin, docSeq, true);
978 // Provide better error message about types missing a trait specialization
979 template <typename T>
981 typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
982 operator>>(Input &yin, T &docSeq) {
983 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
988 // Define non-member operator<< so that Output can stream out document list.
989 template <typename T>
991 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
992 operator<<(Output &yout, T &docList) {
993 yout.beginDocuments();
994 const size_t count = DocumentListTraits<T>::size(yout, docList);
995 for(size_t i=0; i < count; ++i) {
996 if ( yout.preflightDocument(i) ) {
997 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
998 yout.postflightDocument();
1001 yout.endDocuments();
1005 // Define non-member operator<< so that Output can stream out a map.
1006 template <typename T>
1008 typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
1009 operator<<(Output &yout, T &map) {
1010 yout.beginDocuments();
1011 if ( yout.preflightDocument(0) ) {
1012 yamlize(yout, map, true);
1013 yout.postflightDocument();
1015 yout.endDocuments();
1019 // Define non-member operator<< so that Output can stream out a sequence.
1020 template <typename T>
1022 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
1023 operator<<(Output &yout, T &seq) {
1024 yout.beginDocuments();
1025 if ( yout.preflightDocument(0) ) {
1026 yamlize(yout, seq, true);
1027 yout.postflightDocument();
1029 yout.endDocuments();
1033 // Provide better error message about types missing a trait specialization
1034 template <typename T>
1036 typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
1037 operator<<(Output &yout, T &seq) {
1038 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1047 /// Utility for declaring that a std::vector of a particular type
1048 /// should be considered a YAML sequence.
1049 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1053 struct SequenceTraits< std::vector<_type> > { \
1054 static size_t size(IO &io, std::vector<_type> &seq) { \
1055 return seq.size(); \
1057 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1058 if ( index >= seq.size() ) \
1059 seq.resize(index+1); \
1060 return seq[index]; \
1066 /// Utility for declaring that a std::vector of a particular type
1067 /// should be considered a YAML flow sequence.
1068 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1072 struct SequenceTraits< std::vector<_type> > { \
1073 static size_t size(IO &io, std::vector<_type> &seq) { \
1074 return seq.size(); \
1076 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1077 if ( index >= seq.size() ) \
1078 seq.resize(index+1); \
1079 return seq[index]; \
1081 static const bool flow = true; \
1086 /// Utility for declaring that a std::vector of a particular type
1087 /// should be considered a YAML document list.
1088 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1092 struct DocumentListTraits< std::vector<_type> > { \
1093 static size_t size(IO &io, std::vector<_type> &seq) { \
1094 return seq.size(); \
1096 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1097 if ( index >= seq.size() ) \
1098 seq.resize(index+1); \
1099 return seq[index]; \
1107 #endif // LLVM_SUPPORT_YAMLTRAITS_H