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_YAML_TRAITS_H_
11 #define LLVM_YAML_TRAITS_H_
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseMapInfo.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.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 llvm::StringRef (*Signature_input)(llvm::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
231 // and SequenceTraits<T>::flow is *not* defined.
233 struct has_SequenceTraits
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(...);
243 template <typename U> static
244 char flowtest( char[sizeof(&U::flow)] ) ;
246 template <typename U>
247 static double flowtest(...);
250 static bool const value = (sizeof(test<SequenceTraits<T> >(0)) == 1)
251 && (sizeof(flowtest<T>(0)) != 1);
255 // Test if SequenceTraits<T> is defined on type T
256 // and SequenceTraits<T>::flow is defined.
258 struct has_FlowSequenceTraits
260 typedef size_t (*Signature_size)(class IO&, T&);
262 template <typename U>
263 static char test(SameType<Signature_size, &U::size>*);
265 template <typename U>
266 static double test(...);
268 template <typename U> static
269 char flowtest( char[sizeof(&U::flow)] ) ;
271 template <typename U>
272 static double flowtest(...);
275 static bool const value = (sizeof(test<SequenceTraits<T> >(0)) == 1)
276 && (sizeof(flowtest<T>(0)) == 1);
280 // Test if DocumentListTraits<T> is defined on type T
282 struct has_DocumentListTraits
284 typedef size_t (*Signature_size)(class IO&, T&);
286 template <typename U>
287 static char test(SameType<Signature_size, &U::size>*);
289 template <typename U>
290 static double test(...);
293 static bool const value = (sizeof(test<DocumentListTraits<T> >(0)) == 1);
300 struct missingTraits : public llvm::integral_constant<bool,
301 !has_ScalarEnumerationTraits<T>::value
302 && !has_ScalarBitSetTraits<T>::value
303 && !has_ScalarTraits<T>::value
304 && !has_MappingTraits<T>::value
305 && !has_SequenceTraits<T>::value
306 && !has_FlowSequenceTraits<T>::value
307 && !has_DocumentListTraits<T>::value > {};
310 // Base class for Input and Output.
317 virtual bool outputting() = 0;
319 virtual unsigned beginSequence() = 0;
320 virtual bool preflightElement(unsigned, void *&) = 0;
321 virtual void postflightElement(void*) = 0;
322 virtual void endSequence() = 0;
324 virtual unsigned beginFlowSequence() = 0;
325 virtual bool preflightFlowElement(unsigned, void *&) = 0;
326 virtual void postflightFlowElement(void*) = 0;
327 virtual void endFlowSequence() = 0;
329 virtual void beginMapping() = 0;
330 virtual void endMapping() = 0;
331 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
332 virtual void postflightKey(void*) = 0;
334 virtual void beginEnumScalar() = 0;
335 virtual bool matchEnumScalar(const char*, bool) = 0;
336 virtual void endEnumScalar() = 0;
338 virtual bool beginBitSetScalar(bool &) = 0;
339 virtual bool bitSetMatch(const char*, bool) = 0;
340 virtual void endBitSetScalar() = 0;
342 virtual void scalarString(StringRef &) = 0;
344 virtual void setError(const Twine &) = 0;
346 template <typename T>
347 void enumCase(T &Val, const char* Str, const T ConstVal) {
348 if ( matchEnumScalar(Str, (Val == ConstVal)) ) {
353 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
354 template <typename T>
355 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
356 if ( matchEnumScalar(Str, (Val == static_cast<T>(ConstVal))) ) {
361 template <typename T>
362 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
363 if ( bitSetMatch(Str, ((Val & ConstVal) == ConstVal)) ) {
364 Val = Val | ConstVal;
368 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
369 template <typename T>
370 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
371 if ( bitSetMatch(Str, ((Val & ConstVal) == ConstVal)) ) {
372 Val = Val | ConstVal;
377 void setContext(void *);
379 template <typename T>
380 void mapRequired(const char* Key, T& Val) {
381 this->processKey(Key, Val, true);
384 template <typename T>
385 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
386 mapOptional(const char* Key, T& Val) {
387 // omit key/value instead of outputting empty sequence
388 if ( this->outputting() && !(Val.begin() != Val.end()) )
390 this->processKey(Key, Val, false);
393 template <typename T>
394 typename llvm::enable_if_c<!has_SequenceTraits<T>::value,void>::type
395 mapOptional(const char* Key, T& Val) {
396 this->processKey(Key, Val, false);
399 template <typename T>
400 void mapOptional(const char* Key, T& Val, const T& Default) {
401 this->processKeyWithDefault(Key, Val, Default, false);
406 template <typename T>
407 void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,
411 const bool sameAsDefault = (Val == DefaultValue);
412 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
414 yamlize(*this, Val, Required);
415 this->postflightKey(SaveInfo);
423 template <typename T>
424 void processKey(const char *Key, T &Val, bool Required) {
427 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
428 yamlize(*this, Val, Required);
429 this->postflightKey(SaveInfo);
440 typename llvm::enable_if_c<has_ScalarEnumerationTraits<T>::value,void>::type
441 yamlize(IO &io, T &Val, bool) {
442 io.beginEnumScalar();
443 ScalarEnumerationTraits<T>::enumeration(io, Val);
448 typename llvm::enable_if_c<has_ScalarBitSetTraits<T>::value,void>::type
449 yamlize(IO &io, T &Val, bool) {
451 if ( io.beginBitSetScalar(DoClear) ) {
453 Val = static_cast<T>(0);
454 ScalarBitSetTraits<T>::bitset(io, Val);
455 io.endBitSetScalar();
461 typename llvm::enable_if_c<has_ScalarTraits<T>::value,void>::type
462 yamlize(IO &io, T &Val, bool) {
463 if ( io.outputting() ) {
465 llvm::raw_string_ostream Buffer(Storage);
466 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
467 StringRef Str = Buffer.str();
468 io.scalarString(Str);
472 io.scalarString(Str);
473 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
474 if ( !Result.empty() ) {
475 io.setError(llvm::Twine(Result));
482 typename llvm::enable_if_c<has_MappingTraits<T>::value, void>::type
483 yamlize(IO &io, T &Val, bool) {
485 MappingTraits<T>::mapping(io, Val);
490 typename llvm::enable_if_c<missingTraits<T>::value, void>::type
491 yamlize(IO &io, T &Val, bool) {
492 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
496 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
497 yamlize(IO &io, T &Seq, bool) {
498 unsigned incount = io.beginSequence();
499 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incount;
500 for(unsigned i=0; i < count; ++i) {
502 if ( io.preflightElement(i, SaveInfo) ) {
503 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
504 io.postflightElement(SaveInfo);
511 typename llvm::enable_if_c<has_FlowSequenceTraits<T>::value,void>::type
512 yamlize(IO &io, T &Seq, bool) {
513 unsigned incount = io.beginFlowSequence();
514 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incount;
515 for(unsigned i=0; i < count; ++i) {
517 if ( io.preflightFlowElement(i, SaveInfo) ) {
518 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
519 io.postflightFlowElement(SaveInfo);
522 io.endFlowSequence();
528 struct ScalarTraits<bool> {
529 static void output(const bool &, void*, llvm::raw_ostream &);
530 static llvm::StringRef input(llvm::StringRef , void*, bool &);
534 struct ScalarTraits<StringRef> {
535 static void output(const StringRef &, void*, llvm::raw_ostream &);
536 static llvm::StringRef input(llvm::StringRef , void*, StringRef &);
540 struct ScalarTraits<uint8_t> {
541 static void output(const uint8_t &, void*, llvm::raw_ostream &);
542 static llvm::StringRef input(llvm::StringRef , void*, uint8_t &);
546 struct ScalarTraits<uint16_t> {
547 static void output(const uint16_t &, void*, llvm::raw_ostream &);
548 static llvm::StringRef input(llvm::StringRef , void*, uint16_t &);
552 struct ScalarTraits<uint32_t> {
553 static void output(const uint32_t &, void*, llvm::raw_ostream &);
554 static llvm::StringRef input(llvm::StringRef , void*, uint32_t &);
558 struct ScalarTraits<uint64_t> {
559 static void output(const uint64_t &, void*, llvm::raw_ostream &);
560 static llvm::StringRef input(llvm::StringRef , void*, uint64_t &);
564 struct ScalarTraits<int8_t> {
565 static void output(const int8_t &, void*, llvm::raw_ostream &);
566 static llvm::StringRef input(llvm::StringRef , void*, int8_t &);
570 struct ScalarTraits<int16_t> {
571 static void output(const int16_t &, void*, llvm::raw_ostream &);
572 static llvm::StringRef input(llvm::StringRef , void*, int16_t &);
576 struct ScalarTraits<int32_t> {
577 static void output(const int32_t &, void*, llvm::raw_ostream &);
578 static llvm::StringRef input(llvm::StringRef , void*, int32_t &);
582 struct ScalarTraits<int64_t> {
583 static void output(const int64_t &, void*, llvm::raw_ostream &);
584 static llvm::StringRef input(llvm::StringRef , void*, int64_t &);
588 struct ScalarTraits<float> {
589 static void output(const float &, void*, llvm::raw_ostream &);
590 static llvm::StringRef input(llvm::StringRef , void*, float &);
594 struct ScalarTraits<double> {
595 static void output(const double &, void*, llvm::raw_ostream &);
596 static llvm::StringRef input(llvm::StringRef , void*, double &);
601 // Utility for use within MappingTraits<>::mapping() method
602 // to [de]normalize an object for use with YAML conversion.
603 template <typename TNorm, typename TFinal>
604 struct MappingNormalization {
605 MappingNormalization(IO &i_o, TFinal &Obj)
606 : io(i_o), BufPtr(NULL), Result(Obj) {
607 if ( io.outputting() ) {
608 BufPtr = new (&Buffer) TNorm(io, Obj);
611 BufPtr = new (&Buffer) TNorm(io);
615 ~MappingNormalization() {
616 if ( ! io.outputting() ) {
617 Result = BufPtr->denormalize(io);
622 TNorm* operator->() { return BufPtr; }
625 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
635 // Utility for use within MappingTraits<>::mapping() method
636 // to [de]normalize an object for use with YAML conversion.
637 template <typename TNorm, typename TFinal>
638 struct MappingNormalizationHeap {
639 MappingNormalizationHeap(IO &i_o, TFinal &Obj)
640 : io(i_o), BufPtr(NULL), Result(Obj) {
641 if ( io.outputting() ) {
642 BufPtr = new (&Buffer) TNorm(io, Obj);
645 BufPtr = new TNorm(io);
649 ~MappingNormalizationHeap() {
650 if ( io.outputting() ) {
654 Result = BufPtr->denormalize(io);
658 TNorm* operator->() { return BufPtr; }
661 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
672 /// The Input class is used to parse a yaml document into in-memory structs
675 /// It works by using YAMLParser to do a syntax parse of the entire yaml
676 /// document, then the Input class builds a graph of HNodes which wraps
677 /// each yaml Node. The extra layer is buffering. The low level yaml
678 /// parser only lets you look at each node once. The buffering layer lets
679 /// you search and interate multiple times. This is necessary because
680 /// the mapRequired() method calls may not be in the same order
681 /// as the keys in the document.
683 class Input : public IO {
685 // Construct a yaml Input object from a StringRef and optional user-data.
686 Input(StringRef InputContent, void *Ctxt=NULL);
688 // Check if there was an syntax or semantic error during parsing.
689 llvm::error_code error();
691 // To set alternate error reporting.
692 void setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt = 0);
695 virtual bool outputting();
696 virtual void beginMapping();
697 virtual void endMapping();
698 virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
699 virtual void postflightKey(void *);
700 virtual unsigned beginSequence();
701 virtual void endSequence();
702 virtual bool preflightElement(unsigned index, void *&);
703 virtual void postflightElement(void *);
704 virtual unsigned beginFlowSequence();
705 virtual bool preflightFlowElement(unsigned , void *&);
706 virtual void postflightFlowElement(void *);
707 virtual void endFlowSequence();
708 virtual void beginEnumScalar();
709 virtual bool matchEnumScalar(const char*, bool);
710 virtual void endEnumScalar();
711 virtual bool beginBitSetScalar(bool &);
712 virtual bool bitSetMatch(const char *, bool );
713 virtual void endBitSetScalar();
714 virtual void scalarString(StringRef &);
715 virtual void setError(const Twine &message);
719 HNode(Node *n) : _node(n) { }
720 static inline bool classof(const HNode *) { return true; }
725 class EmptyHNode : public HNode {
727 EmptyHNode(Node *n) : HNode(n) { }
728 static inline bool classof(const HNode *n) {
729 return NullNode::classof(n->_node);
731 static inline bool classof(const EmptyHNode *) { return true; }
734 class ScalarHNode : public HNode {
736 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
738 StringRef value() const { return _value; }
740 static inline bool classof(const HNode *n) {
741 return ScalarNode::classof(n->_node);
743 static inline bool classof(const ScalarHNode *) { return true; }
748 class MapHNode : public HNode {
750 MapHNode(Node *n) : HNode(n) { }
752 static inline bool classof(const HNode *n) {
753 return MappingNode::classof(n->_node);
755 static inline bool classof(const MapHNode *) { return true; }
757 struct StrMappingInfo {
758 static StringRef getEmptyKey() { return StringRef(); }
759 static StringRef getTombstoneKey() { return StringRef(" ", 0); }
760 static unsigned getHashValue(StringRef const val) {
761 return llvm::HashString(val); }
762 static bool isEqual(StringRef const lhs,
763 StringRef const rhs) { return lhs.equals(rhs); }
765 typedef llvm::DenseMap<StringRef, HNode*, StrMappingInfo> NameToNode;
767 bool isValidKey(StringRef key);
770 llvm::SmallVector<const char*, 6> ValidKeys;
773 class SequenceHNode : public HNode {
775 SequenceHNode(Node *n) : HNode(n) { }
777 static inline bool classof(const HNode *n) {
778 return SequenceNode::classof(n->_node);
780 static inline bool classof(const SequenceHNode *) { return true; }
782 std::vector<HNode*> Entries;
785 Input::HNode *createHNodes(Node *node);
786 void setError(HNode *hnode, const Twine &message);
787 void setError(Node *node, const Twine &message);
791 // These are only used by operator>>. They could be private
792 // if those templated things could be made friends.
793 bool setCurrentDocument();
797 llvm::yaml::Stream *Strm;
798 llvm::SourceMgr SrcMgr;
800 llvm::BumpPtrAllocator Allocator;
801 llvm::yaml::document_iterator DocIterator;
802 std::vector<bool> BitValuesUsed;
804 bool ScalarMatchFound;
811 /// The Output class is used to generate a yaml document from in-memory structs
814 class Output : public IO {
816 Output(llvm::raw_ostream &, void *Ctxt=NULL);
819 virtual bool outputting();
820 virtual void beginMapping();
821 virtual void endMapping();
822 virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
823 virtual void postflightKey(void *);
824 virtual unsigned beginSequence();
825 virtual void endSequence();
826 virtual bool preflightElement(unsigned, void *&);
827 virtual void postflightElement(void *);
828 virtual unsigned beginFlowSequence();
829 virtual bool preflightFlowElement(unsigned, void *&);
830 virtual void postflightFlowElement(void *);
831 virtual void endFlowSequence();
832 virtual void beginEnumScalar();
833 virtual bool matchEnumScalar(const char*, bool);
834 virtual void endEnumScalar();
835 virtual bool beginBitSetScalar(bool &);
836 virtual bool bitSetMatch(const char *, bool );
837 virtual void endBitSetScalar();
838 virtual void scalarString(StringRef &);
839 virtual void setError(const Twine &message);
842 // These are only used by operator<<. They could be private
843 // if that templated operator could be made a friend.
844 void beginDocuments();
845 bool preflightDocument(unsigned);
846 void postflightDocument();
850 void output(StringRef s);
851 void outputUpToEndOfLine(StringRef s);
853 void outputNewLine();
854 void paddedKey(StringRef key);
856 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
858 llvm::raw_ostream &Out;
859 SmallVector<InState, 8> StateStack;
861 int ColumnAtFlowStart;
862 bool NeedBitValueComma;
863 bool NeedFlowSequenceComma;
864 bool EnumerationMatchFound;
871 /// YAML I/O does conversion based on types. But often native data types
872 /// are just a typedef of built in intergral types (e.g. int). But the C++
873 /// type matching system sees through the typedef and all the typedefed types
874 /// look like a built in type. This will cause the generic YAML I/O conversion
875 /// to be used. To provide better control over the YAML conversion, you can
876 /// use this macro instead of typedef. It will create a class with one field
877 /// and automatic conversion operators to and from the base type.
878 /// Based on BOOST_STRONG_TYPEDEF
879 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
882 _type(const _base v) : value(v) { } \
883 _type(const _type &v) : value(v.value) {} \
884 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
885 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
886 operator const _base & () const { return value; } \
887 bool operator==(const _type &rhs) const { return value == rhs.value; } \
888 bool operator==(const _base &rhs) const { return value == rhs; } \
889 bool operator<(const _type &rhs) const { return value < rhs.value; } \
896 /// Use these types instead of uintXX_t in any mapping to have
897 /// its yaml output formatted as hexadecimal.
899 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
900 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
901 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
902 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
906 struct ScalarTraits<Hex8> {
907 static void output(const Hex8 &, void*, llvm::raw_ostream &);
908 static llvm::StringRef input(llvm::StringRef , void*, Hex8 &);
912 struct ScalarTraits<Hex16> {
913 static void output(const Hex16 &, void*, llvm::raw_ostream &);
914 static llvm::StringRef input(llvm::StringRef , void*, Hex16 &);
918 struct ScalarTraits<Hex32> {
919 static void output(const Hex32 &, void*, llvm::raw_ostream &);
920 static llvm::StringRef input(llvm::StringRef , void*, Hex32 &);
924 struct ScalarTraits<Hex64> {
925 static void output(const Hex64 &, void*, llvm::raw_ostream &);
926 static llvm::StringRef input(llvm::StringRef , void*, Hex64 &);
930 // Define non-member operator>> so that Input can stream in a document list.
931 template <typename T>
933 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
934 operator>>(Input &yin, T &docList) {
936 while ( yin.setCurrentDocument() ) {
937 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
946 // Define non-member operator>> so that Input can stream in a map as a document.
947 template <typename T>
949 typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
950 operator>>(Input &yin, T &docMap) {
951 yin.setCurrentDocument();
952 yamlize(yin, docMap, true);
956 // Define non-member operator>> so that Input can stream in a sequence as
958 template <typename T>
960 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
961 operator>>(Input &yin, T &docSeq) {
962 yin.setCurrentDocument();
963 yamlize(yin, docSeq, true);
967 // Provide better error message about types missing a trait specialization
968 template <typename T>
970 typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
971 operator>>(Input &yin, T &docSeq) {
972 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
977 // Define non-member operator<< so that Output can stream out document list.
978 template <typename T>
980 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
981 operator<<(Output &yout, T &docList) {
982 yout.beginDocuments();
983 const size_t count = DocumentListTraits<T>::size(yout, docList);
984 for(size_t i=0; i < count; ++i) {
985 if ( yout.preflightDocument(i) ) {
986 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
987 yout.postflightDocument();
994 // Define non-member operator<< so that Output can stream out a map.
995 template <typename T>
997 typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
998 operator<<(Output &yout, T &map) {
999 yout.beginDocuments();
1000 if ( yout.preflightDocument(0) ) {
1001 yamlize(yout, map, true);
1002 yout.postflightDocument();
1004 yout.endDocuments();
1008 // Define non-member operator<< so that Output can stream out a sequence.
1009 template <typename T>
1011 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
1012 operator<<(Output &yout, T &seq) {
1013 yout.beginDocuments();
1014 if ( yout.preflightDocument(0) ) {
1015 yamlize(yout, seq, true);
1016 yout.postflightDocument();
1018 yout.endDocuments();
1022 // Provide better error message about types missing a trait specialization
1023 template <typename T>
1025 typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
1026 operator<<(Output &yout, T &seq) {
1027 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1036 /// Utility for declaring that a std::vector of a particular type
1037 /// should be considered a YAML sequence.
1038 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1042 struct SequenceTraits< std::vector<_type> > { \
1043 static size_t size(IO &io, std::vector<_type> &seq) { \
1044 return seq.size(); \
1046 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1047 if ( index >= seq.size() ) \
1048 seq.resize(index+1); \
1049 return seq[index]; \
1055 /// Utility for declaring that a std::vector of a particular type
1056 /// should be considered a YAML flow sequence.
1057 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1061 struct SequenceTraits< std::vector<_type> > { \
1062 static size_t size(IO &io, std::vector<_type> &seq) { \
1063 return seq.size(); \
1065 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1066 if ( index >= seq.size() ) \
1067 seq.resize(index+1); \
1068 return seq[index]; \
1070 static const bool flow = true; \
1075 /// Utility for declaring that a std::vector of a particular type
1076 /// should be considered a YAML document list.
1077 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1081 struct DocumentListTraits< std::vector<_type> > { \
1082 static size_t size(IO &io, std::vector<_type> &seq) { \
1083 return seq.size(); \
1085 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1086 if ( index >= seq.size() ) \
1087 seq.resize(index+1); \
1088 return seq[index]; \
1096 #endif // LLVM_YAML_TRAITS_H_