10 #ifndef LLVM_SUPPORT_YAMLTRAITS_H 11 #define LLVM_SUPPORT_YAMLTRAITS_H 34 #include <system_error> 35 #include <type_traits> 41 struct EmptyContext {};
54 struct MappingTraits {
76 template <
class T,
class Context>
struct MappingContextTraits {
99 struct ScalarEnumerationTraits {
116 struct ScalarBitSetTraits {
143 struct ScalarTraits {
173 template <
typename T>
174 struct BlockScalarTraits {
199 template<
typename T,
typename EnableIf =
void>
200 struct SequenceTraits {
212 template<
typename T,
typename EnableIf =
void>
213 struct SequenceElementTraits {
221 struct DocumentListTraits {
230 template <
typename T>
231 struct CustomMappingTraits {
237 template <
typename T>
242 struct has_ScalarEnumerationTraits
244 using Signature_enumeration = void (*)(
class IO&,
T&);
246 template <
typename U>
247 static char test(SameType<Signature_enumeration, &U::enumeration>*);
249 template <
typename U>
250 static double test(...);
253 static bool const value =
254 (
sizeof(test<ScalarEnumerationTraits<T>>(
nullptr)) == 1);
259 struct has_ScalarBitSetTraits
261 using Signature_bitset = void (*)(
class IO&,
T&);
263 template <
typename U>
264 static char test(SameType<Signature_bitset, &U::bitset>*);
266 template <
typename U>
267 static double test(...);
270 static bool const value = (
sizeof(test<ScalarBitSetTraits<T>>(
nullptr)) == 1);
275 struct has_ScalarTraits
277 using Signature_input = StringRef (*)(StringRef,
void*,
T&);
278 using Signature_output = void (*)(
const T&,
void*, raw_ostream&);
279 using Signature_mustQuote = QuotingType (*)(StringRef);
281 template <
typename U>
282 static char test(SameType<Signature_input, &U::input> *,
283 SameType<Signature_output, &U::output> *,
284 SameType<Signature_mustQuote, &U::mustQuote> *);
286 template <
typename U>
287 static double test(...);
290 static bool const value =
291 (
sizeof(test<ScalarTraits<T>>(
nullptr,
nullptr,
nullptr)) == 1);
296 struct has_BlockScalarTraits
298 using Signature_input = StringRef (*)(StringRef,
void *,
T &);
299 using Signature_output = void (*)(
const T &,
void *, raw_ostream &);
301 template <
typename U>
302 static char test(SameType<Signature_input, &U::input> *,
303 SameType<Signature_output, &U::output> *);
305 template <
typename U>
306 static double test(...);
309 static bool const value =
310 (
sizeof(test<BlockScalarTraits<T>>(
nullptr,
nullptr)) == 1);
314 template <
class T,
class Context>
struct has_MappingTraits {
315 using Signature_mapping = void (*)(
class IO &,
T &,
Context &);
317 template <
typename U>
318 static char test(SameType<Signature_mapping, &U::mapping>*);
320 template <
typename U>
321 static double test(...);
324 static bool const value =
325 (
sizeof(test<MappingContextTraits<T, Context>>(
nullptr)) == 1);
329 template <
class T>
struct has_MappingTraits<
T, EmptyContext> {
330 using Signature_mapping = void (*)(
class IO &,
T &);
332 template <
typename U>
333 static char test(SameType<Signature_mapping, &U::mapping> *);
335 template <
typename U>
static double test(...);
338 static bool const value = (
sizeof(test<MappingTraits<T>>(
nullptr)) == 1);
342 template <
class T,
class Context>
struct has_MappingValidateTraits {
343 using Signature_validate = StringRef (*)(
class IO &,
T &,
Context &);
345 template <
typename U>
346 static char test(SameType<Signature_validate, &U::validate>*);
348 template <
typename U>
349 static double test(...);
352 static bool const value =
353 (
sizeof(test<MappingContextTraits<T, Context>>(
nullptr)) == 1);
357 template <
class T>
struct has_MappingValidateTraits<
T, EmptyContext> {
358 using Signature_validate = StringRef (*)(
class IO &,
T &);
360 template <
typename U>
361 static char test(SameType<Signature_validate, &U::validate> *);
363 template <
typename U>
static double test(...);
366 static bool const value = (
sizeof(test<MappingTraits<T>>(
nullptr)) == 1);
371 struct has_SequenceMethodTraits
373 using Signature_size =
size_t (*)(
class IO&,
T&);
375 template <
typename U>
376 static char test(SameType<Signature_size, &U::size>*);
378 template <
typename U>
379 static double test(...);
382 static bool const value = (
sizeof(test<SequenceTraits<T>>(
nullptr)) == 1);
387 struct has_CustomMappingTraits
389 using Signature_input = void (*)(IO &io, StringRef key,
T &v);
391 template <
typename U>
392 static char test(SameType<Signature_input, &U::inputOne>*);
394 template <
typename U>
395 static double test(...);
398 static bool const value =
399 (
sizeof(test<CustomMappingTraits<T>>(
nullptr)) == 1);
405 template <typename T, bool Enabled = std::is_class<T>::value>
409 static const bool value =
false;
416 struct has_FlowTraits<
T,
true>
418 struct Fallback {
bool flow; };
419 struct Derived :
T, Fallback { };
422 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
425 static char (&f(...))[2];
428 static bool const value =
sizeof(f<Derived>(
nullptr)) == 2;
433 struct has_SequenceTraits :
public std::integral_constant<bool,
434 has_SequenceMethodTraits<T>::value > { };
438 struct has_DocumentListTraits
440 using Signature_size =
size_t (*)(
class IO &,
T &);
442 template <
typename U>
443 static char test(SameType<Signature_size, &U::size>*);
445 template <
typename U>
446 static double test(...);
449 static bool const value = (
sizeof(test<DocumentListTraits<T>>(
nullptr))==1);
452 inline bool isNumber(StringRef S) {
453 static const char OctalChars[] =
"01234567";
454 if (S.startswith(
"0") &&
458 if (S.startswith(
"0o") &&
462 static const char HexChars[] =
"0123456789abcdefABCDEF";
463 if (S.startswith(
"0x") &&
467 static const char DecChars[] =
"0123456789";
471 if (S.equals(
".inf") || S.equals(
".Inf") || S.equals(
".INF"))
474 Regex FloatMatcher(
"^(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?$");
475 if (FloatMatcher.match(S))
481 inline bool isNumeric(StringRef S) {
482 if ((S.front() ==
'-' || S.front() ==
'+') && isNumber(S.drop_front()))
488 if (S.equals(
".nan") || S.equals(
".NaN") || S.equals(
".NAN"))
494 inline bool isNull(StringRef S) {
495 return S.equals(
"null") || S.equals(
"Null") || S.equals(
"NULL") ||
499 inline bool isBool(StringRef S) {
500 return S.equals(
"true") || S.equals(
"True") || S.equals(
"TRUE") ||
501 S.equals(
"false") || S.equals(
"False") || S.equals(
"FALSE");
509 inline QuotingType needsQuotes(StringRef S) {
512 if (isspace(S.front()) || isspace(S.back()))
524 static constexpr
char Indicators[] = R
"(-?:\,[]{}#&*!|>'"%@`)"; 525 if (S.find_first_of(Indicators) == 0)
529 for (
unsigned char C : S) {
554 return QuotingType::Double;
559 return QuotingType::Double;
563 return QuotingType::Double;
571 return MaxQuotingNeeded;
574 template <
typename T,
typename Context>
576 :
public std::integral_constant<bool,
577 !has_ScalarEnumerationTraits<T>::value &&
578 !has_ScalarBitSetTraits<T>::value &&
579 !has_ScalarTraits<T>::value &&
580 !has_BlockScalarTraits<T>::value &&
581 !has_MappingTraits<T, Context>::value &&
582 !has_SequenceTraits<T>::value &&
583 !has_CustomMappingTraits<T>::value &&
584 !has_DocumentListTraits<T>::value> {};
586 template <
typename T,
typename Context>
587 struct validatedMappingTraits
588 :
public std::integral_constant<
589 bool, has_MappingTraits<T, Context>::value &&
590 has_MappingValidateTraits<T, Context>::value> {};
592 template <
typename T,
typename Context>
593 struct unvalidatedMappingTraits
594 :
public std::integral_constant<
595 bool, has_MappingTraits<T, Context>::value &&
596 !has_MappingValidateTraits<T, Context>::value> {};
601 IO(
void *Ctxt =
nullptr);
604 virtual bool outputting() = 0;
606 virtual unsigned beginSequence() = 0;
607 virtual bool preflightElement(
unsigned,
void *&) = 0;
608 virtual void postflightElement(
void*) = 0;
609 virtual void endSequence() = 0;
610 virtual bool canElideEmptySequence() = 0;
612 virtual unsigned beginFlowSequence() = 0;
613 virtual bool preflightFlowElement(
unsigned,
void *&) = 0;
614 virtual void postflightFlowElement(
void*) = 0;
615 virtual void endFlowSequence() = 0;
617 virtual bool mapTag(StringRef Tag,
bool Default=
false) = 0;
618 virtual void beginMapping() = 0;
619 virtual void endMapping() = 0;
620 virtual bool preflightKey(
const char*,
bool,
bool,
bool &,
void *&) = 0;
621 virtual void postflightKey(
void*) = 0;
622 virtual std::vector<StringRef> keys() = 0;
624 virtual void beginFlowMapping() = 0;
625 virtual void endFlowMapping() = 0;
627 virtual void beginEnumScalar() = 0;
628 virtual bool matchEnumScalar(
const char*,
bool) = 0;
629 virtual bool matchEnumFallback() = 0;
630 virtual void endEnumScalar() = 0;
632 virtual bool beginBitSetScalar(
bool &) = 0;
633 virtual bool bitSetMatch(
const char*,
bool) = 0;
634 virtual void endBitSetScalar() = 0;
636 virtual void scalarString(StringRef &, QuotingType) = 0;
637 virtual void blockScalarString(StringRef &) = 0;
639 virtual void setError(
const Twine &) = 0;
641 template <
typename T>
642 void enumCase(
T &Val,
const char* Str,
const T ConstVal) {
643 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
649 template <
typename T>
650 void enumCase(
T &Val,
const char* Str,
const uint32_t ConstVal) {
651 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
656 template <
typename FBT,
typename T>
657 void enumFallback(
T &Val) {
658 if (matchEnumFallback()) {
662 yamlize(*
this, Res,
true, Context);
667 template <
typename T>
668 void bitSetCase(
T &Val,
const char* Str,
const T ConstVal) {
669 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
670 Val =
static_cast<T>(Val | ConstVal);
675 template <
typename T>
676 void bitSetCase(
T &Val,
const char* Str,
const uint32_t ConstVal) {
677 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
678 Val =
static_cast<T>(Val | ConstVal);
682 template <
typename T>
683 void maskedBitSetCase(
T &Val,
const char *Str,
T ConstVal,
T Mask) {
684 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
685 Val = Val | ConstVal;
688 template <
typename T>
689 void maskedBitSetCase(
T &Val,
const char *Str,
uint32_t ConstVal,
691 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
692 Val = Val | ConstVal;
696 void setContext(
void *);
698 template <
typename T>
void mapRequired(
const char *
Key,
T &Val) {
700 this->processKey(Key, Val,
true, Ctx);
703 template <
typename T,
typename Context>
704 void mapRequired(
const char *
Key,
T &Val,
Context &Ctx) {
705 this->processKey(Key, Val,
true, Ctx);
708 template <
typename T>
void mapOptional(
const char *
Key,
T &Val) {
710 mapOptionalWithContext(Key, Val, Ctx);
713 template <
typename T>
714 void mapOptional(
const char *
Key,
T &Val,
const T &Default) {
716 mapOptionalWithContext(Key, Val, Default, Ctx);
719 template <
typename T,
typename Context>
720 typename std::enable_if<has_SequenceTraits<T>::value,
void>
::type 721 mapOptionalWithContext(
const char *
Key,
T &Val,
Context &Ctx) {
723 if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
725 this->processKey(Key, Val,
false, Ctx);
728 template <
typename T,
typename Context>
729 void mapOptionalWithContext(
const char *Key, Optional<T> &Val,
Context &Ctx) {
730 this->processKeyWithDefault(Key, Val, Optional<T>(),
false,
734 template <
typename T,
typename Context>
735 typename std::enable_if<!has_SequenceTraits<T>::value,
void>
::type 736 mapOptionalWithContext(
const char *Key,
T &Val,
Context &Ctx) {
737 this->processKey(Key, Val,
false, Ctx);
740 template <
typename T,
typename Context>
741 void mapOptionalWithContext(
const char *Key,
T &Val,
const T &Default,
743 this->processKeyWithDefault(Key, Val, Default,
false, Ctx);
747 template <
typename T,
typename Context>
748 void processKeyWithDefault(
const char *Key, Optional<T> &Val,
749 const Optional<T> &DefaultValue,
bool Required,
751 assert(DefaultValue.hasValue() ==
false &&
752 "Optional<T> shouldn't have a value!");
754 bool UseDefault =
true;
755 const bool sameAsDefault = outputting() && !Val.hasValue();
756 if (!outputting() && !Val.hasValue())
758 if (Val.hasValue() &&
759 this->preflightKey(Key, Required, sameAsDefault, UseDefault,
761 yamlize(*
this, Val.getValue(),
Required, Ctx);
762 this->postflightKey(SaveInfo);
769 template <
typename T,
typename Context>
770 void processKeyWithDefault(
const char *Key,
T &Val,
const T &DefaultValue,
774 const bool sameAsDefault = outputting() && Val == DefaultValue;
775 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
777 yamlize(*
this, Val, Required, Ctx);
778 this->postflightKey(SaveInfo);
786 template <
typename T,
typename Context>
787 void processKey(
const char *Key,
T &Val,
bool Required,
Context &Ctx) {
790 if ( this->preflightKey(Key, Required,
false, UseDefault, SaveInfo) ) {
791 yamlize(*
this, Val, Required, Ctx);
792 this->postflightKey(SaveInfo);
802 template <
typename T,
typename Context>
803 void doMapping(IO &io,
T &Val,
Context &Ctx) {
804 MappingContextTraits<T, Context>::mapping(io, Val, Ctx);
807 template <
typename T>
void doMapping(IO &io,
T &Val, EmptyContext &Ctx) {
808 MappingTraits<T>::mapping(io, Val);
813 template <
typename T>
814 typename std::enable_if<has_ScalarEnumerationTraits<T>::value,
void>
::type 815 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
816 io.beginEnumScalar();
817 ScalarEnumerationTraits<T>::enumeration(io, Val);
821 template <
typename T>
822 typename std::enable_if<has_ScalarBitSetTraits<T>::value,
void>
::type 823 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
825 if ( io.beginBitSetScalar(DoClear) ) {
827 Val =
static_cast<T>(0);
828 ScalarBitSetTraits<T>::bitset(io, Val);
829 io.endBitSetScalar();
833 template <
typename T>
834 typename std::enable_if<has_ScalarTraits<T>::value,
void>
::type 835 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
836 if ( io.outputting() ) {
838 raw_string_ostream Buffer(Storage);
839 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
840 StringRef Str = Buffer.str();
841 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
845 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
846 StringRef
Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
847 if ( !Result.empty() ) {
848 io.setError(Twine(Result));
853 template <
typename T>
854 typename std::enable_if<has_BlockScalarTraits<T>::value,
void>
::type 855 yamlize(IO &YamlIO,
T &Val,
bool, EmptyContext &Ctx) {
856 if (YamlIO.outputting()) {
858 raw_string_ostream Buffer(Storage);
859 BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
860 StringRef Str = Buffer.str();
861 YamlIO.blockScalarString(Str);
864 YamlIO.blockScalarString(Str);
866 BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val);
868 YamlIO.setError(Twine(Result));
872 template <
typename T,
typename Context>
873 typename std::enable_if<validatedMappingTraits<T, Context>::value,
void>
::type 874 yamlize(IO &io,
T &Val,
bool,
Context &Ctx) {
875 if (has_FlowTraits<MappingTraits<T>>::value)
876 io.beginFlowMapping();
879 if (io.outputting()) {
880 StringRef Err = MappingTraits<T>::validate(io, Val);
882 errs() << Err <<
"\n";
883 assert(Err.empty() &&
"invalid struct trying to be written as yaml");
886 detail::doMapping(io, Val, Ctx);
887 if (!io.outputting()) {
888 StringRef Err = MappingTraits<T>::validate(io, Val);
892 if (has_FlowTraits<MappingTraits<T>>::value)
898 template <
typename T,
typename Context>
899 typename std::enable_if<unvalidatedMappingTraits<T, Context>::value,
void>
::type 900 yamlize(IO &io,
T &Val,
bool,
Context &Ctx) {
901 if (has_FlowTraits<MappingTraits<T>>::value) {
902 io.beginFlowMapping();
903 detail::doMapping(io, Val, Ctx);
907 detail::doMapping(io, Val, Ctx);
912 template <
typename T>
913 typename std::enable_if<has_CustomMappingTraits<T>::value,
void>
::type 914 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
915 if ( io.outputting() ) {
917 CustomMappingTraits<T>::output(io, Val);
921 for (StringRef key : io.keys())
922 CustomMappingTraits<T>::inputOne(io, key, Val);
927 template <
typename T>
928 typename std::enable_if<missingTraits<T, EmptyContext>::value,
void>
::type 929 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
930 char missing_yaml_trait_for_type[
sizeof(MissingTrait<T>)];
933 template <
typename T,
typename Context>
934 typename std::enable_if<has_SequenceTraits<T>::value,
void>
::type 935 yamlize(IO &io,
T &Seq,
bool,
Context &Ctx) {
936 if ( has_FlowTraits< SequenceTraits<T>>::value ) {
937 unsigned incnt = io.beginFlowSequence();
939 for(
unsigned i=0; i <
count; ++i) {
941 if ( io.preflightFlowElement(i, SaveInfo) ) {
942 yamlize(io, SequenceTraits<T>::element(io, Seq, i),
true, Ctx);
943 io.postflightFlowElement(SaveInfo);
946 io.endFlowSequence();
949 unsigned incnt = io.beginSequence();
951 for(
unsigned i=0; i <
count; ++i) {
953 if ( io.preflightElement(i, SaveInfo) ) {
954 yamlize(io, SequenceTraits<T>::element(io, Seq, i),
true, Ctx);
955 io.postflightElement(SaveInfo);
963 struct ScalarTraits<
bool> {
964 static void output(
const bool &,
void* , raw_ostream &);
965 static StringRef input(StringRef,
void *,
bool &);
970 struct ScalarTraits<StringRef> {
971 static void output(
const StringRef &,
void *, raw_ostream &);
972 static StringRef input(StringRef,
void *, StringRef &);
973 static QuotingType mustQuote(StringRef S) {
return needsQuotes(S); }
977 struct ScalarTraits<
std::string> {
978 static void output(
const std::string &,
void *, raw_ostream &);
979 static StringRef input(StringRef,
void *, std::string &);
980 static QuotingType mustQuote(StringRef S) {
return needsQuotes(S); }
984 struct ScalarTraits<uint8_t> {
985 static void output(
const uint8_t &,
void *, raw_ostream &);
986 static StringRef input(StringRef,
void *, uint8_t &);
991 struct ScalarTraits<uint16_t> {
992 static void output(
const uint16_t &,
void *, raw_ostream &);
993 static StringRef input(StringRef,
void *, uint16_t &);
999 static void output(
const uint32_t &,
void *, raw_ostream &);
1000 static StringRef input(StringRef,
void *,
uint32_t &);
1005 struct ScalarTraits<uint64_t> {
1006 static void output(
const uint64_t &,
void *, raw_ostream &);
1007 static StringRef input(StringRef,
void *, uint64_t &);
1012 struct ScalarTraits<int8_t> {
1013 static void output(
const int8_t &,
void *, raw_ostream &);
1014 static StringRef input(StringRef,
void *, int8_t &);
1019 struct ScalarTraits<int16_t> {
1020 static void output(
const int16_t &,
void *, raw_ostream &);
1021 static StringRef input(StringRef,
void *, int16_t &);
1026 struct ScalarTraits<int32_t> {
1027 static void output(
const int32_t &,
void *, raw_ostream &);
1028 static StringRef input(StringRef,
void *, int32_t &);
1033 struct ScalarTraits<int64_t> {
1034 static void output(
const int64_t &,
void *, raw_ostream &);
1035 static StringRef input(StringRef,
void *, int64_t &);
1040 struct ScalarTraits<float> {
1041 static void output(
const float &,
void *, raw_ostream &);
1042 static StringRef input(StringRef,
void *,
float &);
1047 struct ScalarTraits<double> {
1048 static void output(
const double &,
void *, raw_ostream &);
1049 static StringRef input(StringRef,
void *,
double &);
1056 template <
typename value_type, support::endianness endian,
size_t alignment>
1057 struct ScalarTraits<support::detail::packed_endian_specific_integral<
1058 value_type, endian, alignment>> {
1060 support::detail::packed_endian_specific_integral<value_type, endian,
1063 static void output(
const endian_type &
E,
void *Ctx, raw_ostream &Stream) {
1064 ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream);
1067 static StringRef input(StringRef Str,
void *Ctx, endian_type &E) {
1069 auto R = ScalarTraits<value_type>::input(Str, Ctx, V);
1070 E =
static_cast<endian_type
>(V);
1074 static QuotingType mustQuote(StringRef Str) {
1075 return ScalarTraits<value_type>::mustQuote(Str);
1081 template <
typename TNorm,
typename TFinal>
1082 struct MappingNormalization {
1083 MappingNormalization(IO &i_o, TFinal &Obj)
1084 : io(i_o), BufPtr(nullptr), Result(Obj) {
1085 if ( io.outputting() ) {
1086 BufPtr =
new (&Buffer) TNorm(io, Obj);
1089 BufPtr =
new (&Buffer) TNorm(io);
1093 ~MappingNormalization() {
1094 if ( ! io.outputting() ) {
1095 Result = BufPtr->denormalize(io);
1100 TNorm* operator->() {
return BufPtr; }
1103 using Storage = AlignedCharArrayUnion<TNorm>;
1113 template <
typename TNorm,
typename TFinal>
1114 struct MappingNormalizationHeap {
1115 MappingNormalizationHeap(IO &i_o, TFinal &Obj,
BumpPtrAllocator *allocator)
1116 : io(i_o), Result(Obj) {
1117 if ( io.outputting() ) {
1118 BufPtr =
new (&Buffer) TNorm(io, Obj);
1120 else if (allocator) {
1121 BufPtr = allocator->Allocate<TNorm>();
1122 new (BufPtr) TNorm(io);
1124 BufPtr =
new TNorm(io);
1128 ~MappingNormalizationHeap() {
1129 if ( io.outputting() ) {
1133 Result = BufPtr->denormalize(io);
1137 TNorm* operator->() {
return BufPtr; }
1140 using Storage = AlignedCharArrayUnion<TNorm>;
1144 TNorm *BufPtr =
nullptr;
1160 class Input :
public IO {
1165 Input(StringRef InputContent,
1166 void *Ctxt =
nullptr,
1168 void *DiagHandlerCtxt =
nullptr);
1169 Input(MemoryBufferRef Input,
1170 void *Ctxt =
nullptr,
1172 void *DiagHandlerCtxt =
nullptr);
1176 std::error_code
error();
1179 bool outputting()
override;
1180 bool mapTag(StringRef,
bool)
override;
1181 void beginMapping()
override;
1182 void endMapping()
override;
1183 bool preflightKey(
const char *,
bool,
bool,
bool &,
void *&)
override;
1184 void postflightKey(
void *)
override;
1185 std::vector<StringRef> keys()
override;
1186 void beginFlowMapping()
override;
1187 void endFlowMapping()
override;
1188 unsigned beginSequence()
override;
1189 void endSequence()
override;
1190 bool preflightElement(
unsigned index,
void *&)
override;
1191 void postflightElement(
void *)
override;
1192 unsigned beginFlowSequence()
override;
1193 bool preflightFlowElement(
unsigned ,
void *&)
override;
1194 void postflightFlowElement(
void *)
override;
1195 void endFlowSequence()
override;
1196 void beginEnumScalar()
override;
1197 bool matchEnumScalar(
const char*,
bool)
override;
1198 bool matchEnumFallback()
override;
1199 void endEnumScalar()
override;
1200 bool beginBitSetScalar(
bool &)
override;
1201 bool bitSetMatch(
const char *,
bool )
override;
1202 void endBitSetScalar()
override;
1203 void scalarString(StringRef &, QuotingType)
override;
1204 void blockScalarString(StringRef &)
override;
1205 void setError(
const Twine &message)
override;
1206 bool canElideEmptySequence()
override;
1209 virtual void anchor();
1212 HNode(Node *n) : _node(n) { }
1213 virtual ~HNode() =
default;
1215 static bool classof(
const HNode *) {
return true; }
1220 class EmptyHNode :
public HNode {
1221 void anchor()
override;
1224 EmptyHNode(Node *n) : HNode(n) { }
1228 static bool classof(
const EmptyHNode *) {
return true; }
1231 class ScalarHNode :
public HNode {
1232 void anchor()
override;
1235 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
1237 StringRef value()
const {
return _value; }
1239 static bool classof(
const HNode *n) {
1244 static bool classof(
const ScalarHNode *) {
return true; }
1250 class MapHNode :
public HNode {
1251 void anchor()
override;
1254 MapHNode(Node *n) : HNode(n) { }
1256 static bool classof(
const HNode *n) {
1260 static bool classof(
const MapHNode *) {
return true; }
1262 using NameToNode = StringMap<std::unique_ptr<HNode>>;
1265 SmallVector<std::string, 6> ValidKeys;
1268 class SequenceHNode :
public HNode {
1269 void anchor()
override;
1272 SequenceHNode(Node *n) : HNode(n) { }
1274 static bool classof(
const HNode *n) {
1278 static bool classof(
const SequenceHNode *) {
return true; }
1280 std::vector<std::unique_ptr<HNode>> Entries;
1283 std::unique_ptr<Input::HNode> createHNodes(Node *node);
1284 void setError(HNode *hnode,
const Twine &message);
1285 void setError(Node *node,
const Twine &message);
1290 bool setCurrentDocument();
1291 bool nextDocument();
1294 const Node *getCurrentNode()
const;
1298 std::unique_ptr<llvm::yaml::Stream> Strm;
1299 std::unique_ptr<HNode> TopNode;
1302 document_iterator DocIterator;
1303 std::vector<bool> BitValuesUsed;
1304 HNode *CurrentNode =
nullptr;
1305 bool ScalarMatchFound;
1312 class Output :
public IO {
1314 Output(raw_ostream &,
void *Ctxt =
nullptr,
int WrapColumn = 70);
1322 void setWriteDefaultValues(
bool Write) { WriteDefaultValues = Write; }
1324 bool outputting()
override;
1325 bool mapTag(StringRef,
bool)
override;
1326 void beginMapping()
override;
1327 void endMapping()
override;
1328 bool preflightKey(
const char *key,
bool,
bool,
bool &,
void *&)
override;
1329 void postflightKey(
void *)
override;
1330 std::vector<StringRef> keys()
override;
1331 void beginFlowMapping()
override;
1332 void endFlowMapping()
override;
1333 unsigned beginSequence()
override;
1334 void endSequence()
override;
1335 bool preflightElement(
unsigned,
void *&)
override;
1336 void postflightElement(
void *)
override;
1337 unsigned beginFlowSequence()
override;
1338 bool preflightFlowElement(
unsigned,
void *&)
override;
1339 void postflightFlowElement(
void *)
override;
1340 void endFlowSequence()
override;
1341 void beginEnumScalar()
override;
1342 bool matchEnumScalar(
const char*,
bool)
override;
1343 bool matchEnumFallback()
override;
1344 void endEnumScalar()
override;
1345 bool beginBitSetScalar(
bool &)
override;
1346 bool bitSetMatch(
const char *,
bool )
override;
1347 void endBitSetScalar()
override;
1348 void scalarString(StringRef &, QuotingType)
override;
1349 void blockScalarString(StringRef &)
override;
1350 void setError(
const Twine &message)
override;
1351 bool canElideEmptySequence()
override;
1355 void beginDocuments();
1356 bool preflightDocument(
unsigned);
1357 void postflightDocument();
1358 void endDocuments();
1361 void output(StringRef s);
1362 void outputUpToEndOfLine(StringRef s);
1363 void newLineCheck();
1364 void outputNewLine();
1365 void paddedKey(StringRef key);
1366 void flowKey(StringRef Key);
1379 SmallVector<InState, 8> StateStack;
1381 int ColumnAtFlowStart = 0;
1382 int ColumnAtMapFlowStart = 0;
1383 bool NeedBitValueComma =
false;
1384 bool NeedFlowSequenceComma =
false;
1385 bool EnumerationMatchFound =
false;
1386 bool NeedsNewLine =
false;
1387 bool WriteDefaultValues =
false;
1398 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \ 1400 _type() = default; \ 1401 _type(const _base v) : value(v) {} \ 1402 _type(const _type &v) = default; \ 1403 _type &operator=(const _type &rhs) = default; \ 1404 _type &operator=(const _base &rhs) { value = rhs; return *this; } \ 1405 operator const _base & () const { return value; } \ 1406 bool operator==(const _type &rhs) const { return value == rhs.value; } \ 1407 bool operator==(const _base &rhs) const { return value == rhs; } \ 1408 bool operator<(const _type &rhs) const { return value < rhs.value; } \ 1410 using BaseType = _base; \ 1417 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
1418 LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)
1419 LLVM_YAML_STRONG_TYPEDEF(
uint32_t, Hex32)
1420 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
1423 struct ScalarTraits<Hex8> {
1424 static void output(
const Hex8 &,
void *, raw_ostream &);
1425 static StringRef input(StringRef,
void *, Hex8 &);
1430 struct ScalarTraits<Hex16> {
1431 static void output(
const Hex16 &,
void *, raw_ostream &);
1432 static StringRef input(StringRef,
void *, Hex16 &);
1437 struct ScalarTraits<Hex32> {
1438 static void output(
const Hex32 &,
void *, raw_ostream &);
1439 static StringRef input(StringRef,
void *, Hex32 &);
1444 struct ScalarTraits<Hex64> {
1445 static void output(
const Hex64 &,
void *, raw_ostream &);
1446 static StringRef input(StringRef,
void *, Hex64 &);
1451 template <
typename T>
1453 typename std::enable_if<has_DocumentListTraits<T>::value, Input &>
::type 1457 while ( yin.setCurrentDocument() ) {
1458 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i),
true, Ctx);
1468 template <
typename T>
1469 inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value,
1473 yin.setCurrentDocument();
1474 yamlize(yin, docMap,
true, Ctx);
1480 template <
typename T>
1482 typename std::enable_if<has_SequenceTraits<T>::value, Input &>
::type 1485 if (yin.setCurrentDocument())
1486 yamlize(yin, docSeq,
true, Ctx);
1491 template <
typename T>
1493 typename std::enable_if<has_BlockScalarTraits<T>::value, Input &>
::type 1496 if (In.setCurrentDocument())
1497 yamlize(In, Val,
true, Ctx);
1502 template <
typename T>
1504 typename std::enable_if<has_CustomMappingTraits<T>::value, Input &>
::type 1507 if (In.setCurrentDocument())
1508 yamlize(In, Val,
true, Ctx);
1513 template <
typename T>
1514 inline typename std::enable_if<missingTraits<T, EmptyContext>::value,
1517 char missing_yaml_trait_for_type[
sizeof(MissingTrait<T>)];
1522 template <
typename T>
1524 typename std::enable_if<has_DocumentListTraits<T>::value, Output &>
::type 1527 yout.beginDocuments();
1529 for(
size_t i=0; i <
count; ++i) {
1530 if ( yout.preflightDocument(i) ) {
1531 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i),
true,
1533 yout.postflightDocument();
1536 yout.endDocuments();
1541 template <
typename T>
1542 inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value,
1546 yout.beginDocuments();
1547 if ( yout.preflightDocument(0) ) {
1548 yamlize(yout, map,
true, Ctx);
1549 yout.postflightDocument();
1551 yout.endDocuments();
1556 template <
typename T>
1558 typename std::enable_if<has_SequenceTraits<T>::value, Output &>
::type 1561 yout.beginDocuments();
1562 if ( yout.preflightDocument(0) ) {
1563 yamlize(yout, seq,
true, Ctx);
1564 yout.postflightDocument();
1566 yout.endDocuments();
1571 template <
typename T>
1573 typename std::enable_if<has_BlockScalarTraits<T>::value, Output &>
::type 1576 Out.beginDocuments();
1577 if (Out.preflightDocument(0)) {
1578 yamlize(Out, Val,
true, Ctx);
1579 Out.postflightDocument();
1586 template <
typename T>
1588 typename std::enable_if<has_CustomMappingTraits<T>::value, Output &>
::type 1591 Out.beginDocuments();
1592 if (Out.preflightDocument(0)) {
1593 yamlize(Out, Val,
true, Ctx);
1594 Out.postflightDocument();
1601 template <
typename T>
1602 inline typename std::enable_if<missingTraits<T, EmptyContext>::value,
1605 char missing_yaml_trait_for_type[
sizeof(MissingTrait<T>)];
1609 template <
bool B>
struct IsFlowSequenceBase {};
1610 template <>
struct IsFlowSequenceBase<
true> {
static const bool flow =
true; };
1612 template <
typename T,
bool Flow>
1613 struct SequenceTraitsImpl : IsFlowSequenceBase<Flow> {
1615 using type =
typename T::value_type;
1618 static size_t size(IO &io,
T &seq) {
return seq.size(); }
1620 static type &element(IO &io,
T &seq,
size_t index) {
1621 if (index >= seq.size())
1622 seq.resize(index + 1);
1629 template <
bool>
struct CheckIsBool {
static const bool value =
true; };
1633 template <
typename T>
1634 struct SequenceTraits<
std::vector<T>,
1635 typename std::enable_if<CheckIsBool<
1636 SequenceElementTraits<T>::flow>::value>
::type>
1637 : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
1638 template <
typename T,
unsigned N>
1639 struct SequenceTraits<SmallVector<
T,
N>,
1640 typename std::enable_if<CheckIsBool<
1641 SequenceElementTraits<T>::flow>::value>
::type>
1642 : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
1645 template <
typename T>
1646 struct SequenceElementTraits<
1647 T, typename std::enable_if<std::is_fundamental<T>::value>
::type> {
1648 static const bool flow =
true;
1652 template<>
struct SequenceElementTraits<std::string> {
1653 static const bool flow =
false;
1655 template<>
struct SequenceElementTraits<StringRef> {
1656 static const bool flow =
false;
1658 template<>
struct SequenceElementTraits<std::pair<std::string, std::string>> {
1659 static const bool flow =
false;
1663 template <
typename T>
struct StdMapStringCustomMappingTraitsImpl {
1664 using map_type = std::map<std::string, T>;
1666 static void inputOne(IO &io, StringRef key, map_type &v) {
1667 io.mapRequired(key.str().c_str(), v[key]);
1670 static void output(IO &io, map_type &v) {
1672 io.mapRequired(p.first.c_str(), p.second);
1679 #define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW) \ 1683 !std::is_fundamental<TYPE>::value && \ 1684 !std::is_same<TYPE, std::string>::value && \ 1685 !std::is_same<TYPE, llvm::StringRef>::value, \ 1686 "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \ 1687 template <> struct SequenceElementTraits<TYPE> { \ 1688 static const bool flow = FLOW; \ 1695 #define LLVM_YAML_IS_SEQUENCE_VECTOR(type) \ 1696 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false) 1700 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type) \ 1701 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true) 1703 #define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type) \ 1706 template <> struct MappingTraits<Type> { \ 1707 static void mapping(IO &IO, Type &Obj); \ 1712 #define LLVM_YAML_DECLARE_ENUM_TRAITS(Type) \ 1715 template <> struct ScalarEnumerationTraits<Type> { \ 1716 static void enumeration(IO &io, Type &Value); \ 1721 #define LLVM_YAML_DECLARE_BITSET_TRAITS(Type) \ 1724 template <> struct ScalarBitSetTraits<Type> { \ 1725 static void bitset(IO &IO, Type &Options); \ 1730 #define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote) \ 1733 template <> struct ScalarTraits<Type> { \ 1734 static void output(const Type &Value, void *ctx, raw_ostream &Out); \ 1735 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \ 1736 static QuotingType mustQuote(StringRef) { return MustQuote; } \ 1743 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \ 1746 template <unsigned N> \ 1747 struct DocumentListTraits<SmallVector<_type, N>> \ 1748 : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \ 1750 struct DocumentListTraits<std::vector<_type>> \ 1751 : public SequenceTraitsImpl<std::vector<_type>, false> {}; \ 1757 #define LLVM_YAML_IS_STRING_MAP(_type) \ 1761 struct CustomMappingTraits<std::map<std::string, _type>> \ 1762 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \ 1766 #endif // LLVM_SUPPORT_YAMLTRAITS_H static bool classof(const Node *N)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
block Block Frequency true
static bool classof(const Node *N)
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
static bool classof(const Node *N)
static bool classof(const Node *N)
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
iterator_range< detail::value_sequence_iterator< ValueT > > seq(ValueT Begin, ValueT End)
static bool classof(const Node *N)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
ScaledNumber< DigitsT > operator>>(const ScaledNumber< DigitsT > &L, int16_t Shift)