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 user-data.
689 Input(StringRef InputContent, void *Ctxt=NULL);
692 // Check if there was an syntax or semantic error during parsing.
693 llvm::error_code error();
695 // To set alternate error reporting.
696 void setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt = 0);
698 static bool classof(const IO *io) { return !io->outputting(); }
701 virtual bool outputting() const;
702 virtual bool mapTag(StringRef, bool);
703 virtual void beginMapping();
704 virtual void endMapping();
705 virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
706 virtual void postflightKey(void *);
707 virtual unsigned beginSequence();
708 virtual void endSequence();
709 virtual bool preflightElement(unsigned index, void *&);
710 virtual void postflightElement(void *);
711 virtual unsigned beginFlowSequence();
712 virtual bool preflightFlowElement(unsigned , void *&);
713 virtual void postflightFlowElement(void *);
714 virtual void endFlowSequence();
715 virtual void beginEnumScalar();
716 virtual bool matchEnumScalar(const char*, bool);
717 virtual void endEnumScalar();
718 virtual bool beginBitSetScalar(bool &);
719 virtual bool bitSetMatch(const char *, bool );
720 virtual void endBitSetScalar();
721 virtual void scalarString(StringRef &);
722 virtual void setError(const Twine &message);
723 virtual bool canElideEmptySequence();
726 virtual void anchor();
728 HNode(Node *n) : _node(n) { }
730 static inline bool classof(const HNode *) { return true; }
735 class EmptyHNode : public HNode {
736 virtual void anchor();
738 EmptyHNode(Node *n) : HNode(n) { }
739 static inline bool classof(const HNode *n) {
740 return NullNode::classof(n->_node);
742 static inline bool classof(const EmptyHNode *) { return true; }
745 class ScalarHNode : public HNode {
746 virtual void anchor();
748 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
750 StringRef value() const { return _value; }
752 static inline bool classof(const HNode *n) {
753 return ScalarNode::classof(n->_node);
755 static inline bool classof(const ScalarHNode *) { return true; }
760 class MapHNode : public HNode {
761 virtual void anchor();
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 {
780 virtual void anchor();
782 SequenceHNode(Node *n) : HNode(n) { }
783 virtual ~SequenceHNode();
785 static inline bool classof(const HNode *n) {
786 return SequenceNode::classof(n->_node);
788 static inline bool classof(const SequenceHNode *) { return true; }
790 std::vector<HNode*> Entries;
793 Input::HNode *createHNodes(Node *node);
794 void setError(HNode *hnode, const Twine &message);
795 void setError(Node *node, const Twine &message);
799 // These are only used by operator>>. They could be private
800 // if those templated things could be made friends.
801 bool setCurrentDocument();
805 llvm::SourceMgr SrcMgr; // must be before Strm
806 OwningPtr<llvm::yaml::Stream> Strm;
807 OwningPtr<HNode> TopNode;
809 llvm::BumpPtrAllocator StringAllocator;
810 llvm::yaml::document_iterator DocIterator;
811 std::vector<bool> BitValuesUsed;
813 bool ScalarMatchFound;
820 /// The Output class is used to generate a yaml document from in-memory structs
823 class Output : public IO {
825 Output(llvm::raw_ostream &, void *Ctxt=NULL);
828 static bool classof(const IO *io) { return io->outputting(); }
830 virtual bool outputting() const;
831 virtual bool mapTag(StringRef, bool);
832 virtual void beginMapping();
833 virtual void endMapping();
834 virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
835 virtual void postflightKey(void *);
836 virtual unsigned beginSequence();
837 virtual void endSequence();
838 virtual bool preflightElement(unsigned, void *&);
839 virtual void postflightElement(void *);
840 virtual unsigned beginFlowSequence();
841 virtual bool preflightFlowElement(unsigned, void *&);
842 virtual void postflightFlowElement(void *);
843 virtual void endFlowSequence();
844 virtual void beginEnumScalar();
845 virtual bool matchEnumScalar(const char*, bool);
846 virtual void endEnumScalar();
847 virtual bool beginBitSetScalar(bool &);
848 virtual bool bitSetMatch(const char *, bool );
849 virtual void endBitSetScalar();
850 virtual void scalarString(StringRef &);
851 virtual void setError(const Twine &message);
852 virtual bool canElideEmptySequence();
854 // These are only used by operator<<. They could be private
855 // if that templated operator could be made a friend.
856 void beginDocuments();
857 bool preflightDocument(unsigned);
858 void postflightDocument();
862 void output(StringRef s);
863 void outputUpToEndOfLine(StringRef s);
865 void outputNewLine();
866 void paddedKey(StringRef key);
868 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
870 llvm::raw_ostream &Out;
871 SmallVector<InState, 8> StateStack;
873 int ColumnAtFlowStart;
874 bool NeedBitValueComma;
875 bool NeedFlowSequenceComma;
876 bool EnumerationMatchFound;
883 /// YAML I/O does conversion based on types. But often native data types
884 /// are just a typedef of built in intergral types (e.g. int). But the C++
885 /// type matching system sees through the typedef and all the typedefed types
886 /// look like a built in type. This will cause the generic YAML I/O conversion
887 /// to be used. To provide better control over the YAML conversion, you can
888 /// use this macro instead of typedef. It will create a class with one field
889 /// and automatic conversion operators to and from the base type.
890 /// Based on BOOST_STRONG_TYPEDEF
891 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
894 _type(const _base v) : value(v) { } \
895 _type(const _type &v) : value(v.value) {} \
896 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
897 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
898 operator const _base & () const { return value; } \
899 bool operator==(const _type &rhs) const { return value == rhs.value; } \
900 bool operator==(const _base &rhs) const { return value == rhs; } \
901 bool operator<(const _type &rhs) const { return value < rhs.value; } \
908 /// Use these types instead of uintXX_t in any mapping to have
909 /// its yaml output formatted as hexadecimal.
911 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
912 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
913 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
914 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
918 struct ScalarTraits<Hex8> {
919 static void output(const Hex8 &, void*, llvm::raw_ostream &);
920 static StringRef input(StringRef, void*, Hex8 &);
924 struct ScalarTraits<Hex16> {
925 static void output(const Hex16 &, void*, llvm::raw_ostream &);
926 static StringRef input(StringRef, void*, Hex16 &);
930 struct ScalarTraits<Hex32> {
931 static void output(const Hex32 &, void*, llvm::raw_ostream &);
932 static StringRef input(StringRef, void*, Hex32 &);
936 struct ScalarTraits<Hex64> {
937 static void output(const Hex64 &, void*, llvm::raw_ostream &);
938 static StringRef input(StringRef, void*, Hex64 &);
942 // Define non-member operator>> so that Input can stream in a document list.
943 template <typename T>
945 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
946 operator>>(Input &yin, T &docList) {
948 while ( yin.setCurrentDocument() ) {
949 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
958 // Define non-member operator>> so that Input can stream in a map as a document.
959 template <typename T>
961 typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
962 operator>>(Input &yin, T &docMap) {
963 yin.setCurrentDocument();
964 yamlize(yin, docMap, true);
968 // Define non-member operator>> so that Input can stream in a sequence as
970 template <typename T>
972 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
973 operator>>(Input &yin, T &docSeq) {
974 yin.setCurrentDocument();
975 yamlize(yin, docSeq, true);
979 // Provide better error message about types missing a trait specialization
980 template <typename T>
982 typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
983 operator>>(Input &yin, T &docSeq) {
984 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
989 // Define non-member operator<< so that Output can stream out document list.
990 template <typename T>
992 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
993 operator<<(Output &yout, T &docList) {
994 yout.beginDocuments();
995 const size_t count = DocumentListTraits<T>::size(yout, docList);
996 for(size_t i=0; i < count; ++i) {
997 if ( yout.preflightDocument(i) ) {
998 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
999 yout.postflightDocument();
1002 yout.endDocuments();
1006 // Define non-member operator<< so that Output can stream out a map.
1007 template <typename T>
1009 typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
1010 operator<<(Output &yout, T &map) {
1011 yout.beginDocuments();
1012 if ( yout.preflightDocument(0) ) {
1013 yamlize(yout, map, true);
1014 yout.postflightDocument();
1016 yout.endDocuments();
1020 // Define non-member operator<< so that Output can stream out a sequence.
1021 template <typename T>
1023 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
1024 operator<<(Output &yout, T &seq) {
1025 yout.beginDocuments();
1026 if ( yout.preflightDocument(0) ) {
1027 yamlize(yout, seq, true);
1028 yout.postflightDocument();
1030 yout.endDocuments();
1034 // Provide better error message about types missing a trait specialization
1035 template <typename T>
1037 typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
1038 operator<<(Output &yout, T &seq) {
1039 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1048 /// Utility for declaring that a std::vector of a particular type
1049 /// should be considered a YAML sequence.
1050 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1054 struct SequenceTraits< std::vector<_type> > { \
1055 static size_t size(IO &io, std::vector<_type> &seq) { \
1056 return seq.size(); \
1058 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1059 if ( index >= seq.size() ) \
1060 seq.resize(index+1); \
1061 return seq[index]; \
1067 /// Utility for declaring that a std::vector of a particular type
1068 /// should be considered a YAML flow sequence.
1069 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1073 struct SequenceTraits< std::vector<_type> > { \
1074 static size_t size(IO &io, std::vector<_type> &seq) { \
1075 return seq.size(); \
1077 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1078 if ( index >= seq.size() ) \
1079 seq.resize(index+1); \
1080 return seq[index]; \
1082 static const bool flow = true; \
1087 /// Utility for declaring that a std::vector of a particular type
1088 /// should be considered a YAML document list.
1089 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1093 struct DocumentListTraits< std::vector<_type> > { \
1094 static size_t size(IO &io, std::vector<_type> &seq) { \
1095 return seq.size(); \
1097 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1098 if ( index >= seq.size() ) \
1099 seq.resize(index+1); \
1100 return seq[index]; \
1108 #endif // LLVM_SUPPORT_YAMLTRAITS_H