1 //===- llvm/Support/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/Optional.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Twine.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"
32 /// This class should be specialized by any type that needs to be converted
33 /// to/from a YAML mapping. For example:
35 /// struct MappingTraits<MyStruct> {
36 /// static void mapping(IO &io, MyStruct &s) {
37 /// io.mapRequired("name", s.name);
38 /// io.mapRequired("size", s.size);
39 /// io.mapOptional("age", s.age);
43 struct MappingTraits {
45 // static void mapping(IO &io, T &fields);
46 // Optionally may provide:
47 // static StringRef validate(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 =
211 (sizeof(test<ScalarTraits<T> >(nullptr,nullptr)) == 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 = std::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 std::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 std::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 std::integral_constant<bool,
333 has_MappingTraits<T>::value
334 && has_MappingValidateTraits<T>::value> {};
337 struct unvalidatedMappingTraits : public std::integral_constant<bool,
338 has_MappingTraits<T>::value
339 && !has_MappingValidateTraits<T>::value> {};
340 // Base class for Input and Output.
344 IO(void *Ctxt=nullptr);
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 std::enable_if<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 void mapOptional(const char* Key, Optional<T> &Val) {
427 processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false);
430 template <typename T>
431 typename std::enable_if<!has_SequenceTraits<T>::value,void>::type
432 mapOptional(const char* Key, T& Val) {
433 this->processKey(Key, Val, false);
436 template <typename T>
437 void mapOptional(const char* Key, T& Val, const T& Default) {
438 this->processKeyWithDefault(Key, Val, Default, false);
442 template <typename T>
443 void processKeyWithDefault(const char *Key, Optional<T> &Val,
444 const Optional<T> &DefaultValue, bool Required) {
445 assert(DefaultValue.hasValue() == false &&
446 "Optional<T> shouldn't have a value!");
449 const bool sameAsDefault = outputting() && !Val.hasValue();
450 if (!outputting() && !Val.hasValue())
452 if (this->preflightKey(Key, Required, sameAsDefault, UseDefault,
454 yamlize(*this, Val.getValue(), Required);
455 this->postflightKey(SaveInfo);
462 template <typename T>
463 void processKeyWithDefault(const char *Key, T &Val, const T& DefaultValue,
467 const bool sameAsDefault = outputting() && Val == DefaultValue;
468 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
470 yamlize(*this, Val, Required);
471 this->postflightKey(SaveInfo);
479 template <typename T>
480 void processKey(const char *Key, T &Val, bool Required) {
483 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
484 yamlize(*this, Val, Required);
485 this->postflightKey(SaveInfo);
496 typename std::enable_if<has_ScalarEnumerationTraits<T>::value,void>::type
497 yamlize(IO &io, T &Val, bool) {
498 io.beginEnumScalar();
499 ScalarEnumerationTraits<T>::enumeration(io, Val);
504 typename std::enable_if<has_ScalarBitSetTraits<T>::value,void>::type
505 yamlize(IO &io, T &Val, bool) {
507 if ( io.beginBitSetScalar(DoClear) ) {
509 Val = static_cast<T>(0);
510 ScalarBitSetTraits<T>::bitset(io, Val);
511 io.endBitSetScalar();
517 typename std::enable_if<has_ScalarTraits<T>::value,void>::type
518 yamlize(IO &io, T &Val, bool) {
519 if ( io.outputting() ) {
521 llvm::raw_string_ostream Buffer(Storage);
522 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
523 StringRef Str = Buffer.str();
524 io.scalarString(Str);
528 io.scalarString(Str);
529 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
530 if ( !Result.empty() ) {
531 io.setError(llvm::Twine(Result));
538 typename std::enable_if<validatedMappingTraits<T>::value, void>::type
539 yamlize(IO &io, T &Val, bool) {
541 if (io.outputting()) {
542 StringRef Err = MappingTraits<T>::validate(io, Val);
544 llvm::errs() << Err << "\n";
545 assert(Err.empty() && "invalid struct trying to be written as yaml");
548 MappingTraits<T>::mapping(io, Val);
549 if (!io.outputting()) {
550 StringRef Err = MappingTraits<T>::validate(io, Val);
558 typename std::enable_if<unvalidatedMappingTraits<T>::value, void>::type
559 yamlize(IO &io, T &Val, bool) {
561 MappingTraits<T>::mapping(io, Val);
566 typename std::enable_if<missingTraits<T>::value, void>::type
567 yamlize(IO &io, T &Val, bool) {
568 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
572 typename std::enable_if<has_SequenceTraits<T>::value,void>::type
573 yamlize(IO &io, T &Seq, bool) {
574 if ( has_FlowTraits< SequenceTraits<T> >::value ) {
575 unsigned incnt = io.beginFlowSequence();
576 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
577 for(unsigned i=0; i < count; ++i) {
579 if ( io.preflightFlowElement(i, SaveInfo) ) {
580 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
581 io.postflightFlowElement(SaveInfo);
584 io.endFlowSequence();
587 unsigned incnt = io.beginSequence();
588 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
589 for(unsigned i=0; i < count; ++i) {
591 if ( io.preflightElement(i, SaveInfo) ) {
592 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true);
593 io.postflightElement(SaveInfo);
602 struct ScalarTraits<bool> {
603 static void output(const bool &, void*, llvm::raw_ostream &);
604 static StringRef input(StringRef, void*, bool &);
608 struct ScalarTraits<StringRef> {
609 static void output(const StringRef &, void*, llvm::raw_ostream &);
610 static StringRef input(StringRef, void*, StringRef &);
614 struct ScalarTraits<std::string> {
615 static void output(const std::string &, void*, llvm::raw_ostream &);
616 static StringRef input(StringRef, void*, std::string &);
620 struct ScalarTraits<uint8_t> {
621 static void output(const uint8_t &, void*, llvm::raw_ostream &);
622 static StringRef input(StringRef, void*, uint8_t &);
626 struct ScalarTraits<uint16_t> {
627 static void output(const uint16_t &, void*, llvm::raw_ostream &);
628 static StringRef input(StringRef, void*, uint16_t &);
632 struct ScalarTraits<uint32_t> {
633 static void output(const uint32_t &, void*, llvm::raw_ostream &);
634 static StringRef input(StringRef, void*, uint32_t &);
638 struct ScalarTraits<uint64_t> {
639 static void output(const uint64_t &, void*, llvm::raw_ostream &);
640 static StringRef input(StringRef, void*, uint64_t &);
644 struct ScalarTraits<int8_t> {
645 static void output(const int8_t &, void*, llvm::raw_ostream &);
646 static StringRef input(StringRef, void*, int8_t &);
650 struct ScalarTraits<int16_t> {
651 static void output(const int16_t &, void*, llvm::raw_ostream &);
652 static StringRef input(StringRef, void*, int16_t &);
656 struct ScalarTraits<int32_t> {
657 static void output(const int32_t &, void*, llvm::raw_ostream &);
658 static StringRef input(StringRef, void*, int32_t &);
662 struct ScalarTraits<int64_t> {
663 static void output(const int64_t &, void*, llvm::raw_ostream &);
664 static StringRef input(StringRef, void*, int64_t &);
668 struct ScalarTraits<float> {
669 static void output(const float &, void*, llvm::raw_ostream &);
670 static StringRef input(StringRef, void*, float &);
674 struct ScalarTraits<double> {
675 static void output(const double &, void*, llvm::raw_ostream &);
676 static StringRef input(StringRef, void*, double &);
681 // Utility for use within MappingTraits<>::mapping() method
682 // to [de]normalize an object for use with YAML conversion.
683 template <typename TNorm, typename TFinal>
684 struct MappingNormalization {
685 MappingNormalization(IO &i_o, TFinal &Obj)
686 : io(i_o), BufPtr(NULL), Result(Obj) {
687 if ( io.outputting() ) {
688 BufPtr = new (&Buffer) TNorm(io, Obj);
691 BufPtr = new (&Buffer) TNorm(io);
695 ~MappingNormalization() {
696 if ( ! io.outputting() ) {
697 Result = BufPtr->denormalize(io);
702 TNorm* operator->() { return BufPtr; }
705 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
715 // Utility for use within MappingTraits<>::mapping() method
716 // to [de]normalize an object for use with YAML conversion.
717 template <typename TNorm, typename TFinal>
718 struct MappingNormalizationHeap {
719 MappingNormalizationHeap(IO &i_o, TFinal &Obj)
720 : io(i_o), BufPtr(NULL), Result(Obj) {
721 if ( io.outputting() ) {
722 BufPtr = new (&Buffer) TNorm(io, Obj);
725 BufPtr = new TNorm(io);
729 ~MappingNormalizationHeap() {
730 if ( io.outputting() ) {
734 Result = BufPtr->denormalize(io);
738 TNorm* operator->() { return BufPtr; }
741 typedef llvm::AlignedCharArrayUnion<TNorm> Storage;
752 /// The Input class is used to parse a yaml document into in-memory structs
755 /// It works by using YAMLParser to do a syntax parse of the entire yaml
756 /// document, then the Input class builds a graph of HNodes which wraps
757 /// each yaml Node. The extra layer is buffering. The low level yaml
758 /// parser only lets you look at each node once. The buffering layer lets
759 /// you search and interate multiple times. This is necessary because
760 /// the mapRequired() method calls may not be in the same order
761 /// as the keys in the document.
763 class Input : public IO {
765 // Construct a yaml Input object from a StringRef and optional
766 // user-data. The DiagHandler can be specified to provide
767 // alternative error reporting.
768 Input(StringRef InputContent,
769 void *Ctxt = nullptr,
770 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
771 void *DiagHandlerCtxt = nullptr);
774 // Check if there was an syntax or semantic error during parsing.
775 llvm::error_code error();
778 bool outputting() override;
779 bool mapTag(StringRef, bool) override;
780 void beginMapping() override;
781 void endMapping() override;
782 bool preflightKey(const char *, bool, bool, bool &, void *&) override;
783 void postflightKey(void *) override;
784 unsigned beginSequence() override;
785 void endSequence() override;
786 bool preflightElement(unsigned index, void *&) override;
787 void postflightElement(void *) override;
788 unsigned beginFlowSequence() override;
789 bool preflightFlowElement(unsigned , void *&) override;
790 void postflightFlowElement(void *) override;
791 void endFlowSequence() override;
792 void beginEnumScalar() override;
793 bool matchEnumScalar(const char*, bool) override;
794 void endEnumScalar() override;
795 bool beginBitSetScalar(bool &) override;
796 bool bitSetMatch(const char *, bool ) override;
797 void endBitSetScalar() override;
798 void scalarString(StringRef &) override;
799 void setError(const Twine &message) override;
800 bool canElideEmptySequence() override;
803 virtual void anchor();
805 HNode(Node *n) : _node(n) { }
807 static inline bool classof(const HNode *) { return true; }
812 class EmptyHNode : public HNode {
813 void anchor() override;
815 EmptyHNode(Node *n) : HNode(n) { }
816 static inline bool classof(const HNode *n) {
817 return NullNode::classof(n->_node);
819 static inline bool classof(const EmptyHNode *) { return true; }
822 class ScalarHNode : public HNode {
823 void anchor() override;
825 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
827 StringRef value() const { return _value; }
829 static inline bool classof(const HNode *n) {
830 return ScalarNode::classof(n->_node);
832 static inline bool classof(const ScalarHNode *) { return true; }
837 class MapHNode : public HNode {
839 MapHNode(Node *n) : HNode(n) { }
842 static inline bool classof(const HNode *n) {
843 return MappingNode::classof(n->_node);
845 static inline bool classof(const MapHNode *) { return true; }
847 typedef llvm::StringMap<HNode*> NameToNode;
849 bool isValidKey(StringRef key);
852 llvm::SmallVector<const char*, 6> ValidKeys;
855 class SequenceHNode : public HNode {
857 SequenceHNode(Node *n) : HNode(n) { }
858 virtual ~SequenceHNode();
860 static inline bool classof(const HNode *n) {
861 return SequenceNode::classof(n->_node);
863 static inline bool classof(const SequenceHNode *) { return true; }
865 std::vector<HNode*> Entries;
868 Input::HNode *createHNodes(Node *node);
869 void setError(HNode *hnode, const Twine &message);
870 void setError(Node *node, const Twine &message);
874 // These are only used by operator>>. They could be private
875 // if those templated things could be made friends.
876 bool setCurrentDocument();
880 llvm::SourceMgr SrcMgr; // must be before Strm
881 std::unique_ptr<llvm::yaml::Stream> Strm;
882 std::unique_ptr<HNode> TopNode;
884 llvm::BumpPtrAllocator StringAllocator;
885 llvm::yaml::document_iterator DocIterator;
886 std::vector<bool> BitValuesUsed;
888 bool ScalarMatchFound;
895 /// The Output class is used to generate a yaml document from in-memory structs
898 class Output : public IO {
900 Output(llvm::raw_ostream &, void *Ctxt=nullptr);
903 bool outputting() override;
904 bool mapTag(StringRef, bool) override;
905 void beginMapping() override;
906 void endMapping() override;
907 bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
908 void postflightKey(void *) override;
909 unsigned beginSequence() override;
910 void endSequence() override;
911 bool preflightElement(unsigned, void *&) override;
912 void postflightElement(void *) override;
913 unsigned beginFlowSequence() override;
914 bool preflightFlowElement(unsigned, void *&) override;
915 void postflightFlowElement(void *) override;
916 void endFlowSequence() override;
917 void beginEnumScalar() override;
918 bool matchEnumScalar(const char*, bool) override;
919 void endEnumScalar() override;
920 bool beginBitSetScalar(bool &) override;
921 bool bitSetMatch(const char *, bool ) override;
922 void endBitSetScalar() override;
923 void scalarString(StringRef &) override;
924 void setError(const Twine &message) override;
925 bool canElideEmptySequence() override;
927 // These are only used by operator<<. They could be private
928 // if that templated operator could be made a friend.
929 void beginDocuments();
930 bool preflightDocument(unsigned);
931 void postflightDocument();
935 void output(StringRef s);
936 void outputUpToEndOfLine(StringRef s);
938 void outputNewLine();
939 void paddedKey(StringRef key);
941 enum InState { inSeq, inFlowSeq, inMapFirstKey, inMapOtherKey };
943 llvm::raw_ostream &Out;
944 SmallVector<InState, 8> StateStack;
946 int ColumnAtFlowStart;
947 bool NeedBitValueComma;
948 bool NeedFlowSequenceComma;
949 bool EnumerationMatchFound;
956 /// YAML I/O does conversion based on types. But often native data types
957 /// are just a typedef of built in intergral types (e.g. int). But the C++
958 /// type matching system sees through the typedef and all the typedefed types
959 /// look like a built in type. This will cause the generic YAML I/O conversion
960 /// to be used. To provide better control over the YAML conversion, you can
961 /// use this macro instead of typedef. It will create a class with one field
962 /// and automatic conversion operators to and from the base type.
963 /// Based on BOOST_STRONG_TYPEDEF
964 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \
967 _type(const _base v) : value(v) { } \
968 _type(const _type &v) : value(v.value) {} \
969 _type &operator=(const _type &rhs) { value = rhs.value; return *this; }\
970 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
971 operator const _base & () const { return value; } \
972 bool operator==(const _type &rhs) const { return value == rhs.value; } \
973 bool operator==(const _base &rhs) const { return value == rhs; } \
974 bool operator<(const _type &rhs) const { return value < rhs.value; } \
981 /// Use these types instead of uintXX_t in any mapping to have
982 /// its yaml output formatted as hexadecimal.
984 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
985 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
986 LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)
987 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
991 struct ScalarTraits<Hex8> {
992 static void output(const Hex8 &, void*, llvm::raw_ostream &);
993 static StringRef input(StringRef, void*, Hex8 &);
997 struct ScalarTraits<Hex16> {
998 static void output(const Hex16 &, void*, llvm::raw_ostream &);
999 static StringRef input(StringRef, void*, Hex16 &);
1003 struct ScalarTraits<Hex32> {
1004 static void output(const Hex32 &, void*, llvm::raw_ostream &);
1005 static StringRef input(StringRef, void*, Hex32 &);
1009 struct ScalarTraits<Hex64> {
1010 static void output(const Hex64 &, void*, llvm::raw_ostream &);
1011 static StringRef input(StringRef, void*, Hex64 &);
1015 // Define non-member operator>> so that Input can stream in a document list.
1016 template <typename T>
1018 typename std::enable_if<has_DocumentListTraits<T>::value, Input &>::type
1019 operator>>(Input &yin, T &docList) {
1021 while ( yin.setCurrentDocument() ) {
1022 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true);
1031 // Define non-member operator>> so that Input can stream in a map as a document.
1032 template <typename T>
1034 typename std::enable_if<has_MappingTraits<T>::value, Input &>::type
1035 operator>>(Input &yin, T &docMap) {
1036 yin.setCurrentDocument();
1037 yamlize(yin, docMap, true);
1041 // Define non-member operator>> so that Input can stream in a sequence as
1043 template <typename T>
1045 typename std::enable_if<has_SequenceTraits<T>::value, Input &>::type
1046 operator>>(Input &yin, T &docSeq) {
1047 if (yin.setCurrentDocument())
1048 yamlize(yin, docSeq, true);
1052 // Provide better error message about types missing a trait specialization
1053 template <typename T>
1055 typename std::enable_if<missingTraits<T>::value, Input &>::type
1056 operator>>(Input &yin, T &docSeq) {
1057 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1062 // Define non-member operator<< so that Output can stream out document list.
1063 template <typename T>
1065 typename std::enable_if<has_DocumentListTraits<T>::value, Output &>::type
1066 operator<<(Output &yout, T &docList) {
1067 yout.beginDocuments();
1068 const size_t count = DocumentListTraits<T>::size(yout, docList);
1069 for(size_t i=0; i < count; ++i) {
1070 if ( yout.preflightDocument(i) ) {
1071 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true);
1072 yout.postflightDocument();
1075 yout.endDocuments();
1079 // Define non-member operator<< so that Output can stream out a map.
1080 template <typename T>
1082 typename std::enable_if<has_MappingTraits<T>::value, Output &>::type
1083 operator<<(Output &yout, T &map) {
1084 yout.beginDocuments();
1085 if ( yout.preflightDocument(0) ) {
1086 yamlize(yout, map, true);
1087 yout.postflightDocument();
1089 yout.endDocuments();
1093 // Define non-member operator<< so that Output can stream out a sequence.
1094 template <typename T>
1096 typename std::enable_if<has_SequenceTraits<T>::value, Output &>::type
1097 operator<<(Output &yout, T &seq) {
1098 yout.beginDocuments();
1099 if ( yout.preflightDocument(0) ) {
1100 yamlize(yout, seq, true);
1101 yout.postflightDocument();
1103 yout.endDocuments();
1107 // Provide better error message about types missing a trait specialization
1108 template <typename T>
1110 typename std::enable_if<missingTraits<T>::value, Output &>::type
1111 operator<<(Output &yout, T &seq) {
1112 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1121 /// Utility for declaring that a std::vector of a particular type
1122 /// should be considered a YAML sequence.
1123 #define LLVM_YAML_IS_SEQUENCE_VECTOR(_type) \
1127 struct SequenceTraits< std::vector<_type> > { \
1128 static size_t size(IO &io, std::vector<_type> &seq) { \
1129 return seq.size(); \
1131 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1132 if ( index >= seq.size() ) \
1133 seq.resize(index+1); \
1134 return seq[index]; \
1140 /// Utility for declaring that a std::vector of a particular type
1141 /// should be considered a YAML flow sequence.
1142 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(_type) \
1146 struct SequenceTraits< std::vector<_type> > { \
1147 static size_t size(IO &io, std::vector<_type> &seq) { \
1148 return seq.size(); \
1150 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1151 (void)flow; /* Remove this workaround after PR17897 is fixed */ \
1152 if ( index >= seq.size() ) \
1153 seq.resize(index+1); \
1154 return seq[index]; \
1156 static const bool flow = true; \
1161 /// Utility for declaring that a std::vector of a particular type
1162 /// should be considered a YAML document list.
1163 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \
1167 struct DocumentListTraits< std::vector<_type> > { \
1168 static size_t size(IO &io, std::vector<_type> &seq) { \
1169 return seq.size(); \
1171 static _type& element(IO &io, std::vector<_type> &seq, size_t index) {\
1172 if ( index >= seq.size() ) \
1173 seq.resize(index+1); \
1174 return seq[index]; \
1182 #endif // LLVM_SUPPORT_YAMLTRAITS_H