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<std::string> {
544 static void output(const std::string &, void*, llvm::raw_ostream &);
545 static StringRef input(StringRef, void*, std::string &);
549 struct ScalarTraits<uint8_t> {
550 static void output(const uint8_t &, void*, llvm::raw_ostream &);
551 static StringRef input(StringRef, void*, uint8_t &);
555 struct ScalarTraits<uint16_t> {
556 static void output(const uint16_t &, void*, llvm::raw_ostream &);
557 static StringRef input(StringRef, void*, uint16_t &);
561 struct ScalarTraits<uint32_t> {
562 static void output(const uint32_t &, void*, llvm::raw_ostream &);
563 static StringRef input(StringRef, void*, uint32_t &);
567 struct ScalarTraits<uint64_t> {
568 static void output(const uint64_t &, void*, llvm::raw_ostream &);
569 static StringRef input(StringRef, void*, uint64_t &);
573 struct ScalarTraits<int8_t> {
574 static void output(const int8_t &, void*, llvm::raw_ostream &);
575 static StringRef input(StringRef, void*, int8_t &);
579 struct ScalarTraits<int16_t> {
580 static void output(const int16_t &, void*, llvm::raw_ostream &);
581 static StringRef input(StringRef, void*, int16_t &);
585 struct ScalarTraits<int32_t> {
586 static void output(const int32_t &, void*, llvm::raw_ostream &);
587 static StringRef input(StringRef, void*, int32_t &);
591 struct ScalarTraits<int64_t> {
592 static void output(const int64_t &, void*, llvm::raw_ostream &);
593 static StringRef input(StringRef, void*, int64_t &);
597 struct ScalarTraits<float> {
598 static void output(const float &, void*, llvm::raw_ostream &);
599 static StringRef input(StringRef, void*, float &);
603 struct ScalarTraits<double> {
604 static void output(const double &, void*, llvm::raw_ostream &);
605 static StringRef input(StringRef, void*, double &);
610 // Utility for use within MappingTraits<>::mapping() method
611 // to [de]normalize an object for use with YAML conversion.
612 template <typename TNorm, typename TFinal>
613 struct MappingNormalization {
614 MappingNormalization(IO &i_o, TFinal &Obj)
615 : io(i_o), BufPtr(NULL), Result(Obj) {
616 if ( io.outputting() ) {
617 BufPtr = new (&Buffer) TNorm(io, Obj);
620 BufPtr = new (&Buffer) TNorm(io);
624 ~MappingNormalization() {
625 if ( ! io.outputting() ) {
626 Result = BufPtr->denormalize(io);
631 TNorm* operator->() { return BufPtr; }
634 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
644 // Utility for use within MappingTraits<>::mapping() method
645 // to [de]normalize an object for use with YAML conversion.
646 template <typename TNorm, typename TFinal>
647 struct MappingNormalizationHeap {
648 MappingNormalizationHeap(IO &i_o, TFinal &Obj)
649 : io(i_o), BufPtr(NULL), Result(Obj) {
650 if ( io.outputting() ) {
651 BufPtr = new (&Buffer) TNorm(io, Obj);
654 BufPtr = new TNorm(io);
658 ~MappingNormalizationHeap() {
659 if ( io.outputting() ) {
663 Result = BufPtr->denormalize(io);
667 TNorm* operator->() { return BufPtr; }
670 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
681 /// The Input class is used to parse a yaml document into in-memory structs
684 /// It works by using YAMLParser to do a syntax parse of the entire yaml
685 /// document, then the Input class builds a graph of HNodes which wraps
686 /// each yaml Node. The extra layer is buffering. The low level yaml
687 /// parser only lets you look at each node once. The buffering layer lets
688 /// you search and interate multiple times. This is necessary because
689 /// the mapRequired() method calls may not be in the same order
690 /// as the keys in the document.
692 class Input : public IO {
694 // Construct a yaml Input object from a StringRef and optional
695 // user-data. The DiagHandler can be specified to provide
696 // alternative error reporting.
697 Input(StringRef InputContent,
699 SourceMgr::DiagHandlerTy DiagHandler = NULL,
700 void *DiagHandlerCtxt = NULL);
703 // Check if there was an syntax or semantic error during parsing.
704 llvm::error_code error();
706 static bool classof(const IO *io) { return !io->outputting(); }
709 virtual bool outputting() const;
710 virtual bool mapTag(StringRef, bool);
711 virtual void beginMapping();
712 virtual void endMapping();
713 virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
714 virtual void postflightKey(void *);
715 virtual unsigned beginSequence();
716 virtual void endSequence();
717 virtual bool preflightElement(unsigned index, void *&);
718 virtual void postflightElement(void *);
719 virtual unsigned beginFlowSequence();
720 virtual bool preflightFlowElement(unsigned , void *&);
721 virtual void postflightFlowElement(void *);
722 virtual void endFlowSequence();
723 virtual void beginEnumScalar();
724 virtual bool matchEnumScalar(const char*, bool);
725 virtual void endEnumScalar();
726 virtual bool beginBitSetScalar(bool &);
727 virtual bool bitSetMatch(const char *, bool );
728 virtual void endBitSetScalar();
729 virtual void scalarString(StringRef &);
730 virtual void setError(const Twine &message);
731 virtual bool canElideEmptySequence();
734 virtual void anchor();
736 HNode(Node *n) : _node(n) { }
738 static inline bool classof(const HNode *) { return true; }
743 class EmptyHNode : public HNode {
744 virtual void anchor();
746 EmptyHNode(Node *n) : HNode(n) { }
747 static inline bool classof(const HNode *n) {
748 return NullNode::classof(n->_node);
750 static inline bool classof(const EmptyHNode *) { return true; }
753 class ScalarHNode : public HNode {
754 virtual void anchor();
756 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
758 StringRef value() const { return _value; }
760 static inline bool classof(const HNode *n) {
761 return ScalarNode::classof(n->_node);
763 static inline bool classof(const ScalarHNode *) { return true; }
768 class MapHNode : public HNode {
770 MapHNode(Node *n) : HNode(n) { }
773 static inline bool classof(const HNode *n) {
774 return MappingNode::classof(n->_node);
776 static inline bool classof(const MapHNode *) { return true; }
778 typedef llvm::StringMap<HNode*> NameToNode;
780 bool isValidKey(StringRef key);
783 llvm::SmallVector<const char*, 6> ValidKeys;
786 class SequenceHNode : public HNode {
788 SequenceHNode(Node *n) : HNode(n) { }
789 virtual ~SequenceHNode();
791 static inline bool classof(const HNode *n) {
792 return SequenceNode::classof(n->_node);
794 static inline bool classof(const SequenceHNode *) { return true; }
796 std::vector<HNode*> Entries;
799 Input::HNode *createHNodes(Node *node);
800 void setError(HNode *hnode, const Twine &message);
801 void setError(Node *node, const Twine &message);
805 // These are only used by operator>>. They could be private
806 // if those templated things could be made friends.
807 bool setCurrentDocument();
811 llvm::SourceMgr SrcMgr; // must be before Strm
812 OwningPtr<llvm::yaml::Stream> Strm;
813 OwningPtr<HNode> TopNode;
815 llvm::BumpPtrAllocator StringAllocator;
816 llvm::yaml::document_iterator DocIterator;
817 std::vector<bool> BitValuesUsed;
819 bool ScalarMatchFound;
826 /// The Output class is used to generate a yaml document from in-memory structs
829 class Output : public IO {
831 Output(llvm::raw_ostream &, void *Ctxt=NULL);
834 static bool classof(const IO *io) { return io->outputting(); }
836 virtual bool outputting() const;
837 virtual bool mapTag(StringRef, bool);
838 virtual void beginMapping();
839 virtual void endMapping();
840 virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
841 virtual void postflightKey(void *);
842 virtual unsigned beginSequence();
843 virtual void endSequence();
844 virtual bool preflightElement(unsigned, void *&);
845 virtual void postflightElement(void *);
846 virtual unsigned beginFlowSequence();
847 virtual bool preflightFlowElement(unsigned, void *&);
848 virtual void postflightFlowElement(void *);
849 virtual void endFlowSequence();
850 virtual void beginEnumScalar();
851 virtual bool matchEnumScalar(const char*, bool);
852 virtual void endEnumScalar();
853 virtual bool beginBitSetScalar(bool &);
854 virtual bool bitSetMatch(const char *, bool );
855 virtual void endBitSetScalar();
856 virtual void scalarString(StringRef &);
857 virtual void setError(const Twine &message);
858 virtual bool canElideEmptySequence();
860 // These are only used by operator<<. They could be private
861 // if that templated operator could be made a friend.
862 void beginDocuments();
863 bool preflightDocument(unsigned);
864 void postflightDocument();
868 void output(StringRef s);
869 void outputUpToEndOfLine(StringRef s);
871 void outputNewLine();
872 void paddedKey(StringRef key);
874 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
876 llvm::raw_ostream &Out;
877 SmallVector<InState, 8> StateStack;
879 int ColumnAtFlowStart;
880 bool NeedBitValueComma;
881 bool NeedFlowSequenceComma;
882 bool EnumerationMatchFound;
889 /// YAML I/O does conversion based on types. But often native data types
890 /// are just a typedef of built in intergral types (e.g. int). But the C++
891 /// type matching system sees through the typedef and all the typedefed types
892 /// look like a built in type. This will cause the generic YAML I/O conversion
893 /// to be used. To provide better control over the YAML conversion, you can
894 /// use this macro instead of typedef. It will create a class with one field
895 /// and automatic conversion operators to and from the base type.
896 /// Based on BOOST_STRONG_TYPEDEF
897 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
900 _type(const _base v) : value(v) { } \
901 _type(const _type &v) : value(v.value) {} \
902 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
903 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
904 operator const _base & () const { return value; } \
905 bool operator==(const _type &rhs) const { return value == rhs.value; } \
906 bool operator==(const _base &rhs) const { return value == rhs; } \
907 bool operator<(const _type &rhs) const { return value < rhs.value; } \
914 /// Use these types instead of uintXX_t in any mapping to have
915 /// its yaml output formatted as hexadecimal.
917 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
918 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
919 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
920 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
924 struct ScalarTraits<Hex8> {
925 static void output(const Hex8 &, void*, llvm::raw_ostream &);
926 static StringRef input(StringRef, void*, Hex8 &);
930 struct ScalarTraits<Hex16> {
931 static void output(const Hex16 &, void*, llvm::raw_ostream &);
932 static StringRef input(StringRef, void*, Hex16 &);
936 struct ScalarTraits<Hex32> {
937 static void output(const Hex32 &, void*, llvm::raw_ostream &);
938 static StringRef input(StringRef, void*, Hex32 &);
942 struct ScalarTraits<Hex64> {
943 static void output(const Hex64 &, void*, llvm::raw_ostream &);
944 static StringRef input(StringRef, void*, Hex64 &);
948 // Define non-member operator>> so that Input can stream in a document list.
949 template <typename T>
951 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
952 operator>>(Input &yin, T &docList) {
954 while ( yin.setCurrentDocument() ) {
955 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
964 // Define non-member operator>> so that Input can stream in a map as a document.
965 template <typename T>
967 typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
968 operator>>(Input &yin, T &docMap) {
969 yin.setCurrentDocument();
970 yamlize(yin, docMap, true);
974 // Define non-member operator>> so that Input can stream in a sequence as
976 template <typename T>
978 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
979 operator>>(Input &yin, T &docSeq) {
980 if (yin.setCurrentDocument())
981 yamlize(yin, docSeq, true);
985 // Provide better error message about types missing a trait specialization
986 template <typename T>
988 typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
989 operator>>(Input &yin, T &docSeq) {
990 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
995 // Define non-member operator<< so that Output can stream out document list.
996 template <typename T>
998 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
999 operator<<(Output &yout, T &docList) {
1000 yout.beginDocuments();
1001 const size_t count = DocumentListTraits<T>::size(yout, docList);
1002 for(size_t i=0; i < count; ++i) {
1003 if ( yout.preflightDocument(i) ) {
1004 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
1005 yout.postflightDocument();
1008 yout.endDocuments();
1012 // Define non-member operator<< so that Output can stream out a map.
1013 template <typename T>
1015 typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
1016 operator<<(Output &yout, T &map) {
1017 yout.beginDocuments();
1018 if ( yout.preflightDocument(0) ) {
1019 yamlize(yout, map, true);
1020 yout.postflightDocument();
1022 yout.endDocuments();
1026 // Define non-member operator<< so that Output can stream out a sequence.
1027 template <typename T>
1029 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
1030 operator<<(Output &yout, T &seq) {
1031 yout.beginDocuments();
1032 if ( yout.preflightDocument(0) ) {
1033 yamlize(yout, seq, true);
1034 yout.postflightDocument();
1036 yout.endDocuments();
1040 // Provide better error message about types missing a trait specialization
1041 template <typename T>
1043 typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
1044 operator<<(Output &yout, T &seq) {
1045 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1054 /// Utility for declaring that a std::vector of a particular type
1055 /// should be considered a YAML sequence.
1056 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1060 struct SequenceTraits< std::vector<_type> > { \
1061 static size_t size(IO &io, std::vector<_type> &seq) { \
1062 return seq.size(); \
1064 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1065 if ( index >= seq.size() ) \
1066 seq.resize(index+1); \
1067 return seq[index]; \
1073 /// Utility for declaring that a std::vector of a particular type
1074 /// should be considered a YAML flow sequence.
1075 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1079 struct SequenceTraits< std::vector<_type> > { \
1080 static size_t size(IO &io, std::vector<_type> &seq) { \
1081 return seq.size(); \
1083 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1084 if ( index >= seq.size() ) \
1085 seq.resize(index+1); \
1086 return seq[index]; \
1088 static const bool flow = true; \
1093 /// Utility for declaring that a std::vector of a particular type
1094 /// should be considered a YAML document list.
1095 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1099 struct DocumentListTraits< std::vector<_type> > { \
1100 static size_t size(IO &io, std::vector<_type> &seq) { \
1101 return seq.size(); \
1103 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1104 if ( index >= seq.size() ) \
1105 seq.resize(index+1); \
1106 return seq[index]; \
1114 #endif // LLVM_SUPPORT_YAMLTRAITS_H