1 //===- llvm/Supporrt/YAMLTraits.h -------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_YAMLTRAITS_H
11 #define LLVM_SUPPORT_YAMLTRAITS_H
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseMapInfo.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/YAMLParser.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/system_error.h"
26 #include "llvm/Support/type_traits.h"
33 /// This class should be specialized by any type that needs to be converted
34 /// to/from a YAML mapping. For example:
36 /// struct ScalarBitSetTraits<MyStruct> {
37 /// static void mapping(IO &io, MyStruct &s) {
38 /// io.mapRequired("name", s.name);
39 /// io.mapRequired("size", s.size);
40 /// io.mapOptional("age", s.age);
44 struct MappingTraits {
46 // static void mapping(IO &io, T &fields);
47 // Optionally may provide:
48 // static StringRef validate(IO &io, T &fields);
52 /// This class should be specialized by any integral type that converts
53 /// to/from a YAML scalar where there is a one-to-one mapping between
54 /// in-memory values and a string in YAML. For example:
56 /// struct ScalarEnumerationTraits<Colors> {
57 /// static void enumeration(IO &io, Colors &value) {
58 /// io.enumCase(value, "red", cRed);
59 /// io.enumCase(value, "blue", cBlue);
60 /// io.enumCase(value, "green", cGreen);
64 struct ScalarEnumerationTraits {
66 // static void enumeration(IO &io, T &value);
70 /// This class should be specialized by any integer type that is a union
71 /// of bit values and the YAML representation is a flow sequence of
72 /// strings. For example:
74 /// struct ScalarBitSetTraits<MyFlags> {
75 /// static void bitset(IO &io, MyFlags &value) {
76 /// io.bitSetCase(value, "big", flagBig);
77 /// io.bitSetCase(value, "flat", flagFlat);
78 /// io.bitSetCase(value, "round", flagRound);
82 struct ScalarBitSetTraits {
84 // static void bitset(IO &io, T &value);
88 /// This class should be specialized by type that requires custom conversion
89 /// to/from a yaml scalar. For example:
92 /// struct ScalarTraits<MyType> {
93 /// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
94 /// // stream out custom formatting
95 /// out << llvm::format("%x", val);
97 /// static StringRef input(StringRef scalar, void*, MyType &value) {
98 /// // parse scalar and set `value`
99 /// // return empty string on success, or error string
100 /// return StringRef();
104 struct ScalarTraits {
107 // Function to write the value as a string:
108 //static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
110 // Function to convert a string to a value. Returns the empty
111 // StringRef on success or an error string if string is malformed:
112 //static StringRef input(StringRef scalar, void *ctxt, T &value);
116 /// This class should be specialized by any type that needs to be converted
117 /// to/from a YAML sequence. For example:
120 /// struct SequenceTraits< std::vector<MyType> > {
121 /// static size_t size(IO &io, std::vector<MyType> &seq) {
122 /// return seq.size();
124 /// static MyType& element(IO &, std::vector<MyType> &seq, size_t index) {
125 /// if ( index >= seq.size() )
126 /// seq.resize(index+1);
127 /// return seq[index];
131 struct SequenceTraits {
133 // static size_t size(IO &io, T &seq);
134 // static T::value_type& element(IO &io, T &seq, size_t index);
136 // The following is option and will cause generated YAML to use
137 // a flow sequence (e.g. [a,b,c]).
138 // static const bool flow = true;
142 /// This class should be specialized by any type that needs to be converted
143 /// to/from a list of YAML documents.
145 struct DocumentListTraits {
147 // static size_t size(IO &io, T &seq);
148 // static T::value_type& element(IO &io, T &seq, size_t index);
152 // Only used by compiler if both template types are the same
153 template <typename T, T>
156 // Only used for better diagnostics of missing traits
157 template <typename T>
162 // Test if ScalarEnumerationTraits<T> is defined on type T.
164 struct has_ScalarEnumerationTraits
166 typedef void (*Signature_enumeration)(class IO&, T&);
168 template <typename U>
169 static char test(SameType<Signature_enumeration, &U::enumeration>*);
171 template <typename U>
172 static double test(...);
175 static bool const value = (sizeof(test<ScalarEnumerationTraits<T> >(0)) == 1);
179 // Test if ScalarBitSetTraits<T> is defined on type T.
181 struct has_ScalarBitSetTraits
183 typedef void (*Signature_bitset)(class IO&, T&);
185 template <typename U>
186 static char test(SameType<Signature_bitset, &U::bitset>*);
188 template <typename U>
189 static double test(...);
192 static bool const value = (sizeof(test<ScalarBitSetTraits<T> >(0)) == 1);
196 // Test if ScalarTraits<T> is defined on type T.
198 struct has_ScalarTraits
200 typedef StringRef (*Signature_input)(StringRef, void*, T&);
201 typedef void (*Signature_output)(const T&, void*, llvm::raw_ostream&);
203 template <typename U>
204 static char test(SameType<Signature_input, &U::input>*,
205 SameType<Signature_output, &U::output>*);
207 template <typename U>
208 static double test(...);
211 static bool const value = (sizeof(test<ScalarTraits<T> >(0,0)) == 1);
215 // Test if MappingTraits<T> is defined on type T.
217 struct has_MappingTraits
219 typedef void (*Signature_mapping)(class IO&, T&);
221 template <typename U>
222 static char test(SameType<Signature_mapping, &U::mapping>*);
224 template <typename U>
225 static double test(...);
228 static bool const value = (sizeof(test<MappingTraits<T> >(0)) == 1);
231 // Test if MappingTraits<T>::validate() is defined on type T.
233 struct has_MappingValidateTraits
235 typedef StringRef (*Signature_validate)(class IO&, T&);
237 template <typename U>
238 static char test(SameType<Signature_validate, &U::validate>*);
240 template <typename U>
241 static double test(...);
244 static bool const value = (sizeof(test<MappingTraits<T> >(0)) == 1);
249 // Test if SequenceTraits<T> is defined on type T.
251 struct has_SequenceMethodTraits
253 typedef size_t (*Signature_size)(class IO&, T&);
255 template <typename U>
256 static char test(SameType<Signature_size, &U::size>*);
258 template <typename U>
259 static double test(...);
262 static bool const value = (sizeof(test<SequenceTraits<T> >(0)) == 1);
266 // has_FlowTraits<int> will cause an error with some compilers because
267 // it subclasses int. Using this wrapper only instantiates the
268 // real has_FlowTraits only if the template type is a class.
269 template <typename T, bool Enabled = llvm::is_class<T>::value>
273 static const bool value = false;
276 // Some older gcc compilers don't support straight forward tests
277 // for members, so test for ambiguity cause by the base and derived
278 // classes both defining the member.
280 struct has_FlowTraits<T, true>
282 struct Fallback { bool flow; };
283 struct Derived : T, Fallback { };
286 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
289 static char (&f(...))[2];
292 static bool const value = sizeof(f<Derived>(0)) == 2;
297 // Test if SequenceTraits<T> is defined on type T
299 struct has_SequenceTraits : public llvm::integral_constant<bool,
300 has_SequenceMethodTraits<T>::value > { };
303 // Test if DocumentListTraits<T> is defined on type T
305 struct has_DocumentListTraits
307 typedef size_t (*Signature_size)(class IO&, T&);
309 template <typename U>
310 static char test(SameType<Signature_size, &U::size>*);
312 template <typename U>
313 static double test(...);
316 static bool const value = (sizeof(test<DocumentListTraits<T> >(0)) == 1);
323 struct missingTraits : public llvm::integral_constant<bool,
324 !has_ScalarEnumerationTraits<T>::value
325 && !has_ScalarBitSetTraits<T>::value
326 && !has_ScalarTraits<T>::value
327 && !has_MappingTraits<T>::value
328 && !has_SequenceTraits<T>::value
329 && !has_DocumentListTraits<T>::value > {};
332 struct validatedMappingTraits : public llvm::integral_constant<bool,
333 has_MappingTraits<T>::value
334 && has_MappingValidateTraits<T>::value> {};
337 struct unvalidatedMappingTraits : public llvm::integral_constant<bool,
338 has_MappingTraits<T>::value
339 && !has_MappingValidateTraits<T>::value> {};
340 // Base class for Input and Output.
347 virtual bool outputting() = 0;
349 virtual unsigned beginSequence() = 0;
350 virtual bool preflightElement(unsigned, void *&) = 0;
351 virtual void postflightElement(void*) = 0;
352 virtual void endSequence() = 0;
353 virtual bool canElideEmptySequence() = 0;
355 virtual unsigned beginFlowSequence() = 0;
356 virtual bool preflightFlowElement(unsigned, void *&) = 0;
357 virtual void postflightFlowElement(void*) = 0;
358 virtual void endFlowSequence() = 0;
360 virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
361 virtual void beginMapping() = 0;
362 virtual void endMapping() = 0;
363 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
364 virtual void postflightKey(void*) = 0;
366 virtual void beginEnumScalar() = 0;
367 virtual bool matchEnumScalar(const char*, bool) = 0;
368 virtual void endEnumScalar() = 0;
370 virtual bool beginBitSetScalar(bool &) = 0;
371 virtual bool bitSetMatch(const char*, bool) = 0;
372 virtual void endBitSetScalar() = 0;
374 virtual void scalarString(StringRef &) = 0;
376 virtual void setError(const Twine &) = 0;
378 template <typename T>
379 void enumCase(T &Val, const char* Str, const T ConstVal) {
380 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
385 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
386 template <typename T>
387 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
388 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
393 template <typename T>
394 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
395 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
396 Val = Val | ConstVal;
400 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
401 template <typename T>
402 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
403 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
404 Val = Val | ConstVal;
409 void setContext(void *);
411 template <typename T>
412 void mapRequired(const char* Key, T& Val) {
413 this->processKey(Key, Val, true);
416 template <typename T>
417 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
418 mapOptional(const char* Key, T& Val) {
419 // omit key/value instead of outputting empty sequence
420 if ( this->canElideEmptySequence() && !(Val.begin() != Val.end()) )
422 this->processKey(Key, Val, false);
425 template <typename T>
426 typename llvm::enable_if_c<!has_SequenceTraits<T>::value,void>::type
427 mapOptional(const char* Key, T& Val) {
428 this->processKey(Key, Val, false);
431 template <typename T>
432 void mapOptional(const char* Key, T& Val, const T& Default) {
433 this->processKeyWithDefault(Key, Val, Default, false);
437 template <typename T>
438 void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,
442 const bool sameAsDefault = outputting() && Val == DefaultValue;
443 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
445 yamlize(*this, Val, Required);
446 this->postflightKey(SaveInfo);
454 template <typename T>
455 void processKey(const char *Key, T &Val, bool Required) {
458 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
459 yamlize(*this, Val, Required);
460 this->postflightKey(SaveInfo);
471 typename llvm::enable_if_c<has_ScalarEnumerationTraits<T>::value,void>::type
472 yamlize(IO &io, T &Val, bool) {
473 io.beginEnumScalar();
474 ScalarEnumerationTraits<T>::enumeration(io, Val);
479 typename llvm::enable_if_c<has_ScalarBitSetTraits<T>::value,void>::type
480 yamlize(IO &io, T &Val, bool) {
482 if ( io.beginBitSetScalar(DoClear) ) {
484 Val = static_cast<T>(0);
485 ScalarBitSetTraits<T>::bitset(io, Val);
486 io.endBitSetScalar();
492 typename llvm::enable_if_c<has_ScalarTraits<T>::value,void>::type
493 yamlize(IO &io, T &Val, bool) {
494 if ( io.outputting() ) {
496 llvm::raw_string_ostream Buffer(Storage);
497 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
498 StringRef Str = Buffer.str();
499 io.scalarString(Str);
503 io.scalarString(Str);
504 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
505 if ( !Result.empty() ) {
506 io.setError(llvm::Twine(Result));
513 typename llvm::enable_if_c<validatedMappingTraits<T>::value, void>::type
514 yamlize(IO &io, T &Val, bool) {
516 if (io.outputting()) {
517 StringRef Err = MappingTraits<T>::validate(io, Val);
519 llvm::errs() << Err << "\n";
520 assert(Err.empty() && "invalid struct trying to be written as yaml");
523 MappingTraits<T>::mapping(io, Val);
524 if (!io.outputting()) {
525 StringRef Err = MappingTraits<T>::validate(io, Val);
533 typename llvm::enable_if_c<unvalidatedMappingTraits<T>::value, void>::type
534 yamlize(IO &io, T &Val, bool) {
536 MappingTraits<T>::mapping(io, Val);
541 typename llvm::enable_if_c<missingTraits<T>::value, void>::type
542 yamlize(IO &io, T &Val, bool) {
543 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
547 typename llvm::enable_if_c<has_SequenceTraits<T>::value,void>::type
548 yamlize(IO &io, T &Seq, bool) {
549 if ( has_FlowTraits< SequenceTraits<T> >::value ) {
550 unsigned incnt = io.beginFlowSequence();
551 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
552 for(unsigned i=0; i < count; ++i) {
554 if ( io.preflightFlowElement(i, SaveInfo) ) {
555 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
556 io.postflightFlowElement(SaveInfo);
559 io.endFlowSequence();
562 unsigned incnt = io.beginSequence();
563 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
564 for(unsigned i=0; i < count; ++i) {
566 if ( io.preflightElement(i, SaveInfo) ) {
567 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
568 io.postflightElement(SaveInfo);
577 struct ScalarTraits<bool> {
578 static void output(const bool &, void*, llvm::raw_ostream &);
579 static StringRef input(StringRef, void*, bool &);
583 struct ScalarTraits<StringRef> {
584 static void output(const StringRef &, void*, llvm::raw_ostream &);
585 static StringRef input(StringRef, void*, StringRef &);
589 struct ScalarTraits<std::string> {
590 static void output(const std::string &, void*, llvm::raw_ostream &);
591 static StringRef input(StringRef, void*, std::string &);
595 struct ScalarTraits<uint8_t> {
596 static void output(const uint8_t &, void*, llvm::raw_ostream &);
597 static StringRef input(StringRef, void*, uint8_t &);
601 struct ScalarTraits<uint16_t> {
602 static void output(const uint16_t &, void*, llvm::raw_ostream &);
603 static StringRef input(StringRef, void*, uint16_t &);
607 struct ScalarTraits<uint32_t> {
608 static void output(const uint32_t &, void*, llvm::raw_ostream &);
609 static StringRef input(StringRef, void*, uint32_t &);
613 struct ScalarTraits<uint64_t> {
614 static void output(const uint64_t &, void*, llvm::raw_ostream &);
615 static StringRef input(StringRef, void*, uint64_t &);
619 struct ScalarTraits<int8_t> {
620 static void output(const int8_t &, void*, llvm::raw_ostream &);
621 static StringRef input(StringRef, void*, int8_t &);
625 struct ScalarTraits<int16_t> {
626 static void output(const int16_t &, void*, llvm::raw_ostream &);
627 static StringRef input(StringRef, void*, int16_t &);
631 struct ScalarTraits<int32_t> {
632 static void output(const int32_t &, void*, llvm::raw_ostream &);
633 static StringRef input(StringRef, void*, int32_t &);
637 struct ScalarTraits<int64_t> {
638 static void output(const int64_t &, void*, llvm::raw_ostream &);
639 static StringRef input(StringRef, void*, int64_t &);
643 struct ScalarTraits<float> {
644 static void output(const float &, void*, llvm::raw_ostream &);
645 static StringRef input(StringRef, void*, float &);
649 struct ScalarTraits<double> {
650 static void output(const double &, void*, llvm::raw_ostream &);
651 static StringRef input(StringRef, void*, double &);
656 // Utility for use within MappingTraits<>::mapping() method
657 // to [de]normalize an object for use with YAML conversion.
658 template <typename TNorm, typename TFinal>
659 struct MappingNormalization {
660 MappingNormalization(IO &i_o, TFinal &Obj)
661 : io(i_o), BufPtr(NULL), Result(Obj) {
662 if ( io.outputting() ) {
663 BufPtr = new (&Buffer) TNorm(io, Obj);
666 BufPtr = new (&Buffer) TNorm(io);
670 ~MappingNormalization() {
671 if ( ! io.outputting() ) {
672 Result = BufPtr->denormalize(io);
677 TNorm* operator->() { return BufPtr; }
680 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
690 // Utility for use within MappingTraits<>::mapping() method
691 // to [de]normalize an object for use with YAML conversion.
692 template <typename TNorm, typename TFinal>
693 struct MappingNormalizationHeap {
694 MappingNormalizationHeap(IO &i_o, TFinal &Obj)
695 : io(i_o), BufPtr(NULL), Result(Obj) {
696 if ( io.outputting() ) {
697 BufPtr = new (&Buffer) TNorm(io, Obj);
700 BufPtr = new TNorm(io);
704 ~MappingNormalizationHeap() {
705 if ( io.outputting() ) {
709 Result = BufPtr->denormalize(io);
713 TNorm* operator->() { return BufPtr; }
716 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
727 /// The Input class is used to parse a yaml document into in-memory structs
730 /// It works by using YAMLParser to do a syntax parse of the entire yaml
731 /// document, then the Input class builds a graph of HNodes which wraps
732 /// each yaml Node. The extra layer is buffering. The low level yaml
733 /// parser only lets you look at each node once. The buffering layer lets
734 /// you search and interate multiple times. This is necessary because
735 /// the mapRequired() method calls may not be in the same order
736 /// as the keys in the document.
738 class Input : public IO {
740 // Construct a yaml Input object from a StringRef and optional
741 // user-data. The DiagHandler can be specified to provide
742 // alternative error reporting.
743 Input(StringRef InputContent,
745 SourceMgr::DiagHandlerTy DiagHandler = NULL,
746 void *DiagHandlerCtxt = NULL);
749 // Check if there was an syntax or semantic error during parsing.
750 llvm::error_code error();
753 virtual bool outputting();
754 virtual bool mapTag(StringRef, bool);
755 virtual void beginMapping();
756 virtual void endMapping();
757 virtual bool preflightKey(const char *, bool, bool, bool &, void *&);
758 virtual void postflightKey(void *);
759 virtual unsigned beginSequence();
760 virtual void endSequence();
761 virtual bool preflightElement(unsigned index, void *&);
762 virtual void postflightElement(void *);
763 virtual unsigned beginFlowSequence();
764 virtual bool preflightFlowElement(unsigned , void *&);
765 virtual void postflightFlowElement(void *);
766 virtual void endFlowSequence();
767 virtual void beginEnumScalar();
768 virtual bool matchEnumScalar(const char*, bool);
769 virtual void endEnumScalar();
770 virtual bool beginBitSetScalar(bool &);
771 virtual bool bitSetMatch(const char *, bool );
772 virtual void endBitSetScalar();
773 virtual void scalarString(StringRef &);
774 virtual void setError(const Twine &message);
775 virtual bool canElideEmptySequence();
778 virtual void anchor();
780 HNode(Node *n) : _node(n) { }
782 static inline bool classof(const HNode *) { return true; }
787 class EmptyHNode : public HNode {
788 virtual void anchor();
790 EmptyHNode(Node *n) : HNode(n) { }
791 static inline bool classof(const HNode *n) {
792 return NullNode::classof(n->_node);
794 static inline bool classof(const EmptyHNode *) { return true; }
797 class ScalarHNode : public HNode {
798 virtual void anchor();
800 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
802 StringRef value() const { return _value; }
804 static inline bool classof(const HNode *n) {
805 return ScalarNode::classof(n->_node);
807 static inline bool classof(const ScalarHNode *) { return true; }
812 class MapHNode : public HNode {
814 MapHNode(Node *n) : HNode(n) { }
817 static inline bool classof(const HNode *n) {
818 return MappingNode::classof(n->_node);
820 static inline bool classof(const MapHNode *) { return true; }
822 typedef llvm::StringMap<HNode*> NameToNode;
824 bool isValidKey(StringRef key);
827 llvm::SmallVector<const char*, 6> ValidKeys;
830 class SequenceHNode : public HNode {
832 SequenceHNode(Node *n) : HNode(n) { }
833 virtual ~SequenceHNode();
835 static inline bool classof(const HNode *n) {
836 return SequenceNode::classof(n->_node);
838 static inline bool classof(const SequenceHNode *) { return true; }
840 std::vector<HNode*> Entries;
843 Input::HNode *createHNodes(Node *node);
844 void setError(HNode *hnode, const Twine &message);
845 void setError(Node *node, const Twine &message);
849 // These are only used by operator>>. They could be private
850 // if those templated things could be made friends.
851 bool setCurrentDocument();
855 llvm::SourceMgr SrcMgr; // must be before Strm
856 OwningPtr<llvm::yaml::Stream> Strm;
857 OwningPtr<HNode> TopNode;
859 llvm::BumpPtrAllocator StringAllocator;
860 llvm::yaml::document_iterator DocIterator;
861 std::vector<bool> BitValuesUsed;
863 bool ScalarMatchFound;
870 /// The Output class is used to generate a yaml document from in-memory structs
873 class Output : public IO {
875 Output(llvm::raw_ostream &, void *Ctxt=NULL);
878 virtual bool outputting();
879 virtual bool mapTag(StringRef, bool);
880 virtual void beginMapping();
881 virtual void endMapping();
882 virtual bool preflightKey(const char *key, bool, bool, bool &, void *&);
883 virtual void postflightKey(void *);
884 virtual unsigned beginSequence();
885 virtual void endSequence();
886 virtual bool preflightElement(unsigned, void *&);
887 virtual void postflightElement(void *);
888 virtual unsigned beginFlowSequence();
889 virtual bool preflightFlowElement(unsigned, void *&);
890 virtual void postflightFlowElement(void *);
891 virtual void endFlowSequence();
892 virtual void beginEnumScalar();
893 virtual bool matchEnumScalar(const char*, bool);
894 virtual void endEnumScalar();
895 virtual bool beginBitSetScalar(bool &);
896 virtual bool bitSetMatch(const char *, bool );
897 virtual void endBitSetScalar();
898 virtual void scalarString(StringRef &);
899 virtual void setError(const Twine &message);
900 virtual bool canElideEmptySequence();
902 // These are only used by operator<<. They could be private
903 // if that templated operator could be made a friend.
904 void beginDocuments();
905 bool preflightDocument(unsigned);
906 void postflightDocument();
910 void output(StringRef s);
911 void outputUpToEndOfLine(StringRef s);
913 void outputNewLine();
914 void paddedKey(StringRef key);
916 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
918 llvm::raw_ostream &Out;
919 SmallVector<InState, 8> StateStack;
921 int ColumnAtFlowStart;
922 bool NeedBitValueComma;
923 bool NeedFlowSequenceComma;
924 bool EnumerationMatchFound;
931 /// YAML I/O does conversion based on types. But often native data types
932 /// are just a typedef of built in intergral types (e.g. int). But the C++
933 /// type matching system sees through the typedef and all the typedefed types
934 /// look like a built in type. This will cause the generic YAML I/O conversion
935 /// to be used. To provide better control over the YAML conversion, you can
936 /// use this macro instead of typedef. It will create a class with one field
937 /// and automatic conversion operators to and from the base type.
938 /// Based on BOOST_STRONG_TYPEDEF
939 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
942 _type(const _base v) : value(v) { } \
943 _type(const _type &v) : value(v.value) {} \
944 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
945 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
946 operator const _base & () const { return value; } \
947 bool operator==(const _type &rhs) const { return value == rhs.value; } \
948 bool operator==(const _base &rhs) const { return value == rhs; } \
949 bool operator<(const _type &rhs) const { return value < rhs.value; } \
956 /// Use these types instead of uintXX_t in any mapping to have
957 /// its yaml output formatted as hexadecimal.
959 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
960 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
961 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
962 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
966 struct ScalarTraits<Hex8> {
967 static void output(const Hex8 &, void*, llvm::raw_ostream &);
968 static StringRef input(StringRef, void*, Hex8 &);
972 struct ScalarTraits<Hex16> {
973 static void output(const Hex16 &, void*, llvm::raw_ostream &);
974 static StringRef input(StringRef, void*, Hex16 &);
978 struct ScalarTraits<Hex32> {
979 static void output(const Hex32 &, void*, llvm::raw_ostream &);
980 static StringRef input(StringRef, void*, Hex32 &);
984 struct ScalarTraits<Hex64> {
985 static void output(const Hex64 &, void*, llvm::raw_ostream &);
986 static StringRef input(StringRef, void*, Hex64 &);
990 // Define non-member operator>> so that Input can stream in a document list.
991 template <typename T>
993 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Input &>::type
994 operator>>(Input &yin, T &docList) {
996 while ( yin.setCurrentDocument() ) {
997 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
1006 // Define non-member operator>> so that Input can stream in a map as a document.
1007 template <typename T>
1009 typename llvm::enable_if_c<has_MappingTraits<T>::value,Input &>::type
1010 operator>>(Input &yin, T &docMap) {
1011 yin.setCurrentDocument();
1012 yamlize(yin, docMap, true);
1016 // Define non-member operator>> so that Input can stream in a sequence as
1018 template <typename T>
1020 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Input &>::type
1021 operator>>(Input &yin, T &docSeq) {
1022 if (yin.setCurrentDocument())
1023 yamlize(yin, docSeq, true);
1027 // Provide better error message about types missing a trait specialization
1028 template <typename T>
1030 typename llvm::enable_if_c<missingTraits<T>::value,Input &>::type
1031 operator>>(Input &yin, T &docSeq) {
1032 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1037 // Define non-member operator<< so that Output can stream out document list.
1038 template <typename T>
1040 typename llvm::enable_if_c<has_DocumentListTraits<T>::value,Output &>::type
1041 operator<<(Output &yout, T &docList) {
1042 yout.beginDocuments();
1043 const size_t count = DocumentListTraits<T>::size(yout, docList);
1044 for(size_t i=0; i < count; ++i) {
1045 if ( yout.preflightDocument(i) ) {
1046 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
1047 yout.postflightDocument();
1050 yout.endDocuments();
1054 // Define non-member operator<< so that Output can stream out a map.
1055 template <typename T>
1057 typename llvm::enable_if_c<has_MappingTraits<T>::value,Output &>::type
1058 operator<<(Output &yout, T &map) {
1059 yout.beginDocuments();
1060 if ( yout.preflightDocument(0) ) {
1061 yamlize(yout, map, true);
1062 yout.postflightDocument();
1064 yout.endDocuments();
1068 // Define non-member operator<< so that Output can stream out a sequence.
1069 template <typename T>
1071 typename llvm::enable_if_c<has_SequenceTraits<T>::value,Output &>::type
1072 operator<<(Output &yout, T &seq) {
1073 yout.beginDocuments();
1074 if ( yout.preflightDocument(0) ) {
1075 yamlize(yout, seq, true);
1076 yout.postflightDocument();
1078 yout.endDocuments();
1082 // Provide better error message about types missing a trait specialization
1083 template <typename T>
1085 typename llvm::enable_if_c<missingTraits<T>::value,Output &>::type
1086 operator<<(Output &yout, T &seq) {
1087 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1096 /// Utility for declaring that a std::vector of a particular type
1097 /// should be considered a YAML sequence.
1098 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1102 struct SequenceTraits< std::vector<_type> > { \
1103 static size_t size(IO &io, std::vector<_type> &seq) { \
1104 return seq.size(); \
1106 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1107 if ( index >= seq.size() ) \
1108 seq.resize(index+1); \
1109 return seq[index]; \
1115 /// Utility for declaring that a std::vector of a particular type
1116 /// should be considered a YAML flow sequence.
1117 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1121 struct SequenceTraits< std::vector<_type> > { \
1122 static size_t size(IO &io, std::vector<_type> &seq) { \
1123 return seq.size(); \
1125 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1126 (void)flow; /* Remove this workaround after PR17897 is fixed */ \
1127 if ( index >= seq.size() ) \
1128 seq.resize(index+1); \
1129 return seq[index]; \
1131 static const bool flow = true; \
1136 /// Utility for declaring that a std::vector of a particular type
1137 /// should be considered a YAML document list.
1138 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1142 struct DocumentListTraits< std::vector<_type> > { \
1143 static size_t size(IO &io, std::vector<_type> &seq) { \
1144 return seq.size(); \
1146 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1147 if ( index >= seq.size() ) \
1148 seq.resize(index+1); \
1149 return seq[index]; \
1157 #endif // LLVM_SUPPORT_YAMLTRAITS_H