9 #ifndef LLVM_SUPPORT_YAMLTRAITS_H 10 #define LLVM_SUPPORT_YAMLTRAITS_H 34 #include <system_error> 35 #include <type_traits> 47 struct EmptyContext {};
60 struct MappingTraits {
82 template <
class T,
class Context>
struct MappingContextTraits {
104 template <
typename T,
typename Enable =
void>
struct ScalarEnumerationTraits {
120 template <
typename T,
typename Enable =
void>
struct ScalarBitSetTraits {
146 template <
typename T,
typename Enable =
void>
struct ScalarTraits {
176 template <
typename T>
177 struct BlockScalarTraits {
213 template <
typename T>
struct TaggedScalarTraits {
243 template<
typename T,
typename EnableIf =
void>
244 struct SequenceTraits {
256 template<
typename T,
typename EnableIf =
void>
257 struct SequenceElementTraits {
265 struct DocumentListTraits {
274 template <
typename T>
275 struct CustomMappingTraits {
297 template <
typename T>
struct PolymorphicTraits {
306 template <
typename T>
311 struct has_ScalarEnumerationTraits
313 using Signature_enumeration = void (*)(
class IO&,
T&);
315 template <
typename U>
316 static char test(SameType<Signature_enumeration, &U::enumeration>*);
318 template <
typename U>
319 static double test(...);
321 static bool const value =
322 (
sizeof(test<ScalarEnumerationTraits<T>>(
nullptr)) == 1);
327 struct has_ScalarBitSetTraits
329 using Signature_bitset = void (*)(
class IO&,
T&);
331 template <
typename U>
332 static char test(SameType<Signature_bitset, &U::bitset>*);
334 template <
typename U>
335 static double test(...);
337 static bool const value = (
sizeof(test<ScalarBitSetTraits<T>>(
nullptr)) == 1);
342 struct has_ScalarTraits
344 using Signature_input = StringRef (*)(StringRef,
void*,
T&);
345 using Signature_output = void (*)(
const T&,
void*, raw_ostream&);
346 using Signature_mustQuote = QuotingType (*)(StringRef);
348 template <
typename U>
349 static char test(SameType<Signature_input, &U::input> *,
350 SameType<Signature_output, &U::output> *,
351 SameType<Signature_mustQuote, &U::mustQuote> *);
353 template <
typename U>
354 static double test(...);
356 static bool const value =
357 (
sizeof(test<ScalarTraits<T>>(
nullptr,
nullptr,
nullptr)) == 1);
362 struct has_BlockScalarTraits
364 using Signature_input = StringRef (*)(StringRef,
void *,
T &);
365 using Signature_output = void (*)(
const T &,
void *, raw_ostream &);
367 template <
typename U>
368 static char test(SameType<Signature_input, &U::input> *,
369 SameType<Signature_output, &U::output> *);
371 template <
typename U>
372 static double test(...);
374 static bool const value =
375 (
sizeof(test<BlockScalarTraits<T>>(
nullptr,
nullptr)) == 1);
379 template <
class T>
struct has_TaggedScalarTraits {
380 using Signature_input = StringRef (*)(StringRef, StringRef,
void *,
T &);
381 using Signature_output = void (*)(
const T &,
void *, raw_ostream &,
383 using Signature_mustQuote = QuotingType (*)(
const T &, StringRef);
385 template <
typename U>
386 static char test(SameType<Signature_input, &U::input> *,
387 SameType<Signature_output, &U::output> *,
388 SameType<Signature_mustQuote, &U::mustQuote> *);
390 template <
typename U>
static double test(...);
392 static bool const value =
393 (
sizeof(test<TaggedScalarTraits<T>>(
nullptr,
nullptr,
nullptr)) == 1);
397 template <
class T,
class Context>
struct has_MappingTraits {
398 using Signature_mapping = void (*)(
class IO &,
T &,
Context &);
400 template <
typename U>
401 static char test(SameType<Signature_mapping, &U::mapping>*);
403 template <
typename U>
404 static double test(...);
406 static bool const value =
407 (
sizeof(test<MappingContextTraits<T, Context>>(
nullptr)) == 1);
411 template <
class T>
struct has_MappingTraits<
T, EmptyContext> {
412 using Signature_mapping = void (*)(
class IO &,
T &);
414 template <
typename U>
415 static char test(SameType<Signature_mapping, &U::mapping> *);
417 template <
typename U>
static double test(...);
419 static bool const value = (
sizeof(test<MappingTraits<T>>(
nullptr)) == 1);
423 template <
class T,
class Context>
struct has_MappingValidateTraits {
424 using Signature_validate = StringRef (*)(
class IO &,
T &,
Context &);
426 template <
typename U>
427 static char test(SameType<Signature_validate, &U::validate>*);
429 template <
typename U>
430 static double test(...);
432 static bool const value =
433 (
sizeof(test<MappingContextTraits<T, Context>>(
nullptr)) == 1);
437 template <
class T>
struct has_MappingValidateTraits<
T, EmptyContext> {
438 using Signature_validate = StringRef (*)(
class IO &,
T &);
440 template <
typename U>
441 static char test(SameType<Signature_validate, &U::validate> *);
443 template <
typename U>
static double test(...);
445 static bool const value = (
sizeof(test<MappingTraits<T>>(
nullptr)) == 1);
450 struct has_SequenceMethodTraits
452 using Signature_size =
size_t (*)(
class IO&,
T&);
454 template <
typename U>
455 static char test(SameType<Signature_size, &U::size>*);
457 template <
typename U>
458 static double test(...);
460 static bool const value = (
sizeof(test<SequenceTraits<T>>(
nullptr)) == 1);
465 struct has_CustomMappingTraits
467 using Signature_input = void (*)(IO &io, StringRef key,
T &v);
469 template <
typename U>
470 static char test(SameType<Signature_input, &U::inputOne>*);
472 template <
typename U>
473 static double test(...);
475 static bool const value =
476 (
sizeof(test<CustomMappingTraits<T>>(
nullptr)) == 1);
482 template <typename T, bool Enabled = std::is_class<T>::value>
486 static const bool value =
false;
493 struct has_FlowTraits<
T,
true>
495 struct Fallback {
bool flow; };
496 struct Derived :
T, Fallback { };
499 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
502 static char (&f(...))[2];
504 static bool const value =
sizeof(f<Derived>(
nullptr)) == 2;
509 struct has_SequenceTraits :
public std::integral_constant<bool,
510 has_SequenceMethodTraits<T>::value > { };
514 struct has_DocumentListTraits
516 using Signature_size =
size_t (*)(
class IO &,
T &);
518 template <
typename U>
519 static char test(SameType<Signature_size, &U::size>*);
521 template <
typename U>
522 static double test(...);
524 static bool const value = (
sizeof(test<DocumentListTraits<T>>(
nullptr))==1);
527 template <
class T>
struct has_PolymorphicTraits {
528 using Signature_getKind =
NodeKind (*)(
const T &);
530 template <
typename U>
531 static char test(SameType<Signature_getKind, &U::getKind> *);
533 template <
typename U>
static double test(...);
535 static bool const value = (
sizeof(test<PolymorphicTraits<T>>(
nullptr)) == 1);
538 inline bool isNumeric(StringRef S) {
539 const static auto skipDigits = [](StringRef Input) {
540 return Input.drop_front(
541 std::min(Input.find_first_not_of(
"0123456789"), Input.size()));
546 if (S.empty() || S.equals(
"+") || S.equals(
"-"))
549 if (S.equals(
".nan") || S.equals(
".NaN") || S.equals(
".NAN"))
553 StringRef
Tail = (S.front() ==
'-' || S.front() ==
'+') ? S.drop_front() : S;
557 if (Tail.equals(
".inf") || Tail.equals(
".Inf") || Tail.equals(
".INF"))
563 if (S.startswith(
"0o"))
564 return S.size() > 2 &&
567 if (S.startswith(
"0x"))
568 return S.size() > 2 && S.drop_front(2).find_first_not_of(
577 if (S.startswith(
".") &&
579 (S.size() > 1 && std::strchr(
"0123456789", S[1]) ==
nullptr)))
582 if (S.startswith(
"E") || S.startswith(
"e"))
598 if (S.front() ==
'.') {
601 }
else if (S.front() ==
'e' || S.front() ==
'E') {
602 State = FoundExponent;
608 if (State == FoundDot) {
613 if (S.front() ==
'e' || S.front() ==
'E') {
614 State = FoundExponent;
621 assert(State == FoundExponent &&
"Should have found exponent at this point.");
625 if (S.front() ==
'+' || S.front() ==
'-') {
631 return skipDigits(S).empty();
634 inline bool isNull(StringRef S) {
635 return S.equals(
"null") || S.equals(
"Null") || S.equals(
"NULL") ||
639 inline bool isBool(StringRef S) {
640 return S.equals(
"true") || S.equals(
"True") || S.equals(
"TRUE") ||
641 S.equals(
"false") || S.equals(
"False") || S.equals(
"FALSE");
649 inline QuotingType needsQuotes(StringRef S) {
652 if (isspace(static_cast<unsigned char>(S.front())) ||
653 isspace(static_cast<unsigned char>(S.back())))
665 static constexpr
char Indicators[] = R
"(-?:\,[]{}#&*!|>'"%@`)"; 666 if (S.find_first_of(Indicators) == 0)
670 for (
unsigned char C : S) {
717 return MaxQuotingNeeded;
720 template <
typename T,
typename Context>
722 :
public std::integral_constant<bool,
723 !has_ScalarEnumerationTraits<T>::value &&
724 !has_ScalarBitSetTraits<T>::value &&
725 !has_ScalarTraits<T>::value &&
726 !has_BlockScalarTraits<T>::value &&
727 !has_TaggedScalarTraits<T>::value &&
728 !has_MappingTraits<T, Context>::value &&
729 !has_SequenceTraits<T>::value &&
730 !has_CustomMappingTraits<T>::value &&
731 !has_DocumentListTraits<T>::value &&
732 !has_PolymorphicTraits<T>::value> {};
734 template <
typename T,
typename Context>
735 struct validatedMappingTraits
736 :
public std::integral_constant<
737 bool, has_MappingTraits<T, Context>::value &&
738 has_MappingValidateTraits<T, Context>::value> {};
740 template <
typename T,
typename Context>
741 struct unvalidatedMappingTraits
742 :
public std::integral_constant<
743 bool, has_MappingTraits<T, Context>::value &&
744 !has_MappingValidateTraits<T, Context>::value> {};
749 IO(
void *Ctxt =
nullptr);
752 virtual bool outputting()
const = 0;
754 virtual unsigned beginSequence() = 0;
755 virtual bool preflightElement(
unsigned,
void *&) = 0;
756 virtual void postflightElement(
void*) = 0;
757 virtual void endSequence() = 0;
758 virtual bool canElideEmptySequence() = 0;
760 virtual unsigned beginFlowSequence() = 0;
761 virtual bool preflightFlowElement(
unsigned,
void *&) = 0;
762 virtual void postflightFlowElement(
void*) = 0;
763 virtual void endFlowSequence() = 0;
765 virtual bool mapTag(StringRef Tag,
bool Default=
false) = 0;
766 virtual void beginMapping() = 0;
767 virtual void endMapping() = 0;
768 virtual bool preflightKey(
const char*,
bool,
bool,
bool &,
void *&) = 0;
769 virtual void postflightKey(
void*) = 0;
770 virtual std::vector<StringRef> keys() = 0;
772 virtual void beginFlowMapping() = 0;
773 virtual void endFlowMapping() = 0;
775 virtual void beginEnumScalar() = 0;
776 virtual bool matchEnumScalar(
const char*,
bool) = 0;
777 virtual bool matchEnumFallback() = 0;
778 virtual void endEnumScalar() = 0;
780 virtual bool beginBitSetScalar(
bool &) = 0;
781 virtual bool bitSetMatch(
const char*,
bool) = 0;
782 virtual void endBitSetScalar() = 0;
784 virtual void scalarString(StringRef &, QuotingType) = 0;
785 virtual void blockScalarString(StringRef &) = 0;
786 virtual void scalarTag(std::string &) = 0;
790 virtual void setError(
const Twine &) = 0;
792 template <
typename T>
793 void enumCase(
T &Val,
const char* Str,
const T ConstVal) {
794 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
800 template <
typename T>
801 void enumCase(
T &Val,
const char* Str,
const uint32_t ConstVal) {
802 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
807 template <
typename FBT,
typename T>
808 void enumFallback(
T &Val) {
809 if (matchEnumFallback()) {
813 yamlize(*
this, Res,
true, Context);
818 template <
typename T>
819 void bitSetCase(
T &Val,
const char* Str,
const T ConstVal) {
820 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
821 Val =
static_cast<T>(Val | ConstVal);
826 template <
typename T>
827 void bitSetCase(
T &Val,
const char* Str,
const uint32_t ConstVal) {
828 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
829 Val =
static_cast<T>(Val | ConstVal);
833 template <
typename T>
834 void maskedBitSetCase(
T &Val,
const char *Str,
T ConstVal,
T Mask) {
835 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
836 Val = Val | ConstVal;
839 template <
typename T>
840 void maskedBitSetCase(
T &Val,
const char *Str,
uint32_t ConstVal,
842 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
843 Val = Val | ConstVal;
846 void *getContext()
const;
847 void setContext(
void *);
849 template <
typename T>
void mapRequired(
const char *
Key,
T &Val) {
851 this->processKey(Key, Val,
true, Ctx);
854 template <
typename T,
typename Context>
855 void mapRequired(
const char *
Key,
T &Val,
Context &Ctx) {
856 this->processKey(Key, Val,
true, Ctx);
861 mapOptionalWithContext(Key, Val, Ctx);
864 template <
typename T,
typename DefaultT>
867 mapOptionalWithContext(Key, Val, Default, Ctx);
870 template <
typename T,
typename Context>
871 typename std::enable_if<has_SequenceTraits<T>::value,
void>
::type 872 mapOptionalWithContext(
const char *
Key,
T &Val,
Context &Ctx) {
874 if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
876 this->processKey(Key, Val,
false, Ctx);
879 template <
typename T,
typename Context>
880 void mapOptionalWithContext(
const char *Key, Optional<T> &Val,
Context &Ctx) {
881 this->processKeyWithDefault(Key, Val, Optional<T>(),
false,
885 template <
typename T,
typename Context>
886 typename std::enable_if<!has_SequenceTraits<T>::value,
void>
::type 887 mapOptionalWithContext(
const char *Key,
T &Val,
Context &Ctx) {
888 this->processKey(Key, Val,
false, Ctx);
891 template <
typename T,
typename Context,
typename DefaultT>
892 void mapOptionalWithContext(
const char *Key,
T &Val,
const DefaultT &Default,
894 static_assert(std::is_convertible<DefaultT, T>::value,
895 "Default type must be implicitly convertible to value type!");
896 this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
901 template <
typename T,
typename Context>
902 void processKeyWithDefault(
const char *Key, Optional<T> &Val,
903 const Optional<T> &DefaultValue,
bool Required,
905 assert(DefaultValue.hasValue() ==
false &&
906 "Optional<T> shouldn't have a value!");
908 bool UseDefault =
true;
909 const bool sameAsDefault = outputting() && !Val.hasValue();
910 if (!outputting() && !Val.hasValue())
912 if (Val.hasValue() &&
913 this->preflightKey(Key, Required, sameAsDefault, UseDefault,
915 yamlize(*
this, Val.getValue(),
Required, Ctx);
916 this->postflightKey(SaveInfo);
923 template <
typename T,
typename Context>
924 void processKeyWithDefault(
const char *Key,
T &Val,
const T &DefaultValue,
928 const bool sameAsDefault = outputting() && Val == DefaultValue;
929 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
931 yamlize(*
this, Val, Required, Ctx);
932 this->postflightKey(SaveInfo);
940 template <
typename T,
typename Context>
941 void processKey(
const char *Key,
T &Val,
bool Required,
Context &Ctx) {
944 if ( this->preflightKey(Key, Required,
false, UseDefault, SaveInfo) ) {
945 yamlize(*
this, Val, Required, Ctx);
946 this->postflightKey(SaveInfo);
956 template <
typename T,
typename Context>
957 void doMapping(IO &io,
T &Val,
Context &Ctx) {
958 MappingContextTraits<T, Context>::mapping(io, Val, Ctx);
961 template <
typename T>
void doMapping(IO &io,
T &Val, EmptyContext &Ctx) {
962 MappingTraits<T>::mapping(io, Val);
967 template <
typename T>
968 typename std::enable_if<has_ScalarEnumerationTraits<T>::value,
void>
::type 969 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
970 io.beginEnumScalar();
971 ScalarEnumerationTraits<T>::enumeration(io, Val);
975 template <
typename T>
976 typename std::enable_if<has_ScalarBitSetTraits<T>::value,
void>
::type 977 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
979 if ( io.beginBitSetScalar(DoClear) ) {
982 ScalarBitSetTraits<T>::bitset(io, Val);
983 io.endBitSetScalar();
987 template <
typename T>
988 typename std::enable_if<has_ScalarTraits<T>::value,
void>
::type 989 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
990 if ( io.outputting() ) {
992 raw_string_ostream Buffer(Storage);
993 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
994 StringRef Str = Buffer.str();
995 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
999 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
1000 StringRef
Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
1001 if ( !Result.empty() ) {
1002 io.setError(Twine(Result));
1007 template <
typename T>
1008 typename std::enable_if<has_BlockScalarTraits<T>::value,
void>
::type 1009 yamlize(IO &
YamlIO,
T &Val,
bool, EmptyContext &Ctx) {
1010 if (YamlIO.outputting()) {
1011 std::string Storage;
1012 raw_string_ostream Buffer(Storage);
1013 BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
1014 StringRef Str = Buffer.str();
1015 YamlIO.blockScalarString(Str);
1018 YamlIO.blockScalarString(Str);
1020 BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val);
1021 if (!Result.empty())
1022 YamlIO.setError(Twine(Result));
1026 template <
typename T>
1027 typename std::enable_if<has_TaggedScalarTraits<T>::value,
void>
::type 1028 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
1029 if (io.outputting()) {
1030 std::string ScalarStorage, TagStorage;
1031 raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage);
1032 TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer,
1034 io.scalarTag(TagBuffer.str());
1035 StringRef ScalarStr = ScalarBuffer.str();
1036 io.scalarString(ScalarStr,
1037 TaggedScalarTraits<T>::mustQuote(Val, ScalarStr));
1044 TaggedScalarTraits<T>::input(Str, Tag, io.getContext(), Val);
1045 if (!Result.empty()) {
1046 io.setError(Twine(Result));
1051 template <
typename T,
typename Context>
1052 typename std::enable_if<validatedMappingTraits<T, Context>::value,
void>
::type 1053 yamlize(IO &io,
T &Val,
bool,
Context &Ctx) {
1054 if (has_FlowTraits<MappingTraits<T>>::value)
1055 io.beginFlowMapping();
1058 if (io.outputting()) {
1061 errs() << Err <<
"\n";
1062 assert(Err.empty() &&
"invalid struct trying to be written as yaml");
1065 detail::doMapping(io, Val, Ctx);
1066 if (!io.outputting()) {
1071 if (has_FlowTraits<MappingTraits<T>>::value)
1072 io.endFlowMapping();
1077 template <
typename T,
typename Context>
1078 typename std::enable_if<unvalidatedMappingTraits<T, Context>::value,
void>
::type 1079 yamlize(IO &io,
T &Val,
bool,
Context &Ctx) {
1080 if (has_FlowTraits<MappingTraits<T>>::value) {
1081 io.beginFlowMapping();
1082 detail::doMapping(io, Val, Ctx);
1083 io.endFlowMapping();
1086 detail::doMapping(io, Val, Ctx);
1091 template <
typename T>
1092 typename std::enable_if<has_CustomMappingTraits<T>::value,
void>
::type 1093 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
1094 if ( io.outputting() ) {
1096 CustomMappingTraits<T>::output(io, Val);
1100 for (StringRef key : io.keys())
1101 CustomMappingTraits<T>::inputOne(io, key, Val);
1106 template <
typename T>
1107 typename std::enable_if<has_PolymorphicTraits<T>::value,
void>
::type 1108 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
1109 switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val)
1110 : io.getNodeKind()) {
1112 return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val),
true, Ctx);
1114 return yamlize(io, PolymorphicTraits<T>::getAsMap(Val),
true, Ctx);
1116 return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val),
true, Ctx);
1120 template <
typename T>
1121 typename std::enable_if<missingTraits<T, EmptyContext>::value,
void>
::type 1122 yamlize(IO &io,
T &Val,
bool, EmptyContext &Ctx) {
1123 char missing_yaml_trait_for_type[
sizeof(MissingTrait<T>)];
1126 template <
typename T,
typename Context>
1127 typename std::enable_if<has_SequenceTraits<T>::value,
void>
::type 1128 yamlize(IO &io,
T &Seq,
bool,
Context &Ctx) {
1129 if ( has_FlowTraits< SequenceTraits<T>>::value ) {
1130 unsigned incnt = io.beginFlowSequence();
1132 for(
unsigned i=0; i <
count; ++i) {
1134 if ( io.preflightFlowElement(i, SaveInfo) ) {
1135 yamlize(io, SequenceTraits<T>::element(io, Seq, i),
true, Ctx);
1136 io.postflightFlowElement(SaveInfo);
1139 io.endFlowSequence();
1142 unsigned incnt = io.beginSequence();
1144 for(
unsigned i=0; i <
count; ++i) {
1146 if ( io.preflightElement(i, SaveInfo) ) {
1147 yamlize(io, SequenceTraits<T>::element(io, Seq, i),
true, Ctx);
1148 io.postflightElement(SaveInfo);
1156 struct ScalarTraits<
bool> {
1157 static void output(
const bool &,
void* , raw_ostream &);
1158 static StringRef input(StringRef,
void *,
bool &);
1163 struct ScalarTraits<StringRef> {
1164 static void output(
const StringRef &,
void *, raw_ostream &);
1165 static StringRef input(StringRef,
void *, StringRef &);
1166 static QuotingType mustQuote(StringRef S) {
return needsQuotes(S); }
1171 static void output(
const std::string &,
void *, raw_ostream &);
1172 static StringRef input(StringRef,
void *, std::string &);
1173 static QuotingType mustQuote(StringRef S) {
return needsQuotes(S); }
1177 struct ScalarTraits<uint8_t> {
1178 static void output(
const uint8_t &,
void *, raw_ostream &);
1179 static StringRef input(StringRef,
void *, uint8_t &);
1185 static void output(
const uint16_t &,
void *, raw_ostream &);
1186 static StringRef input(StringRef,
void *,
uint16_t &);
1192 static void output(
const uint32_t &,
void *, raw_ostream &);
1193 static StringRef input(StringRef,
void *,
uint32_t &);
1198 struct ScalarTraits<uint64_t> {
1199 static void output(
const uint64_t &,
void *, raw_ostream &);
1200 static StringRef input(StringRef,
void *, uint64_t &);
1205 struct ScalarTraits<int8_t> {
1206 static void output(
const int8_t &,
void *, raw_ostream &);
1207 static StringRef input(StringRef,
void *, int8_t &);
1212 struct ScalarTraits<int16_t> {
1213 static void output(
const int16_t &,
void *, raw_ostream &);
1214 static StringRef input(StringRef,
void *, int16_t &);
1219 struct ScalarTraits<int32_t> {
1220 static void output(
const int32_t &,
void *, raw_ostream &);
1221 static StringRef input(StringRef,
void *, int32_t &);
1226 struct ScalarTraits<int64_t> {
1227 static void output(
const int64_t &,
void *, raw_ostream &);
1228 static StringRef input(StringRef,
void *, int64_t &);
1233 struct ScalarTraits<float> {
1234 static void output(
const float &,
void *, raw_ostream &);
1235 static StringRef input(StringRef,
void *,
float &);
1240 struct ScalarTraits<double> {
1241 static void output(
const double &,
void *, raw_ostream &);
1242 static StringRef input(StringRef,
void *,
double &);
1249 template <
typename value_type, support::endianness endian,
size_t alignment>
1250 struct ScalarTraits<
1251 support::detail::packed_endian_specific_integral<value_type, endian,
1253 typename std::enable_if<has_ScalarTraits<value_type>::value>
::type> {
1255 support::detail::packed_endian_specific_integral<value_type, endian,
1258 static void output(
const endian_type &
E,
void *Ctx, raw_ostream &Stream) {
1259 ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream);
1262 static StringRef input(StringRef Str,
void *Ctx, endian_type &E) {
1264 auto R = ScalarTraits<value_type>::input(Str, Ctx, V);
1265 E =
static_cast<endian_type
>(V);
1269 static QuotingType mustQuote(StringRef Str) {
1270 return ScalarTraits<value_type>::mustQuote(Str);
1274 template <
typename value_type, support::endianness endian,
size_t alignment>
1275 struct ScalarEnumerationTraits<
1276 support::detail::packed_endian_specific_integral<value_type, endian,
1278 typename std::enable_if<
1279 has_ScalarEnumerationTraits<value_type>::value>
::type> {
1281 support::detail::packed_endian_specific_integral<value_type, endian,
1284 static void enumeration(IO &io, endian_type &
E) {
1286 ScalarEnumerationTraits<value_type>::enumeration(io, V);
1291 template <
typename value_type, support::endianness endian,
size_t alignment>
1292 struct ScalarBitSetTraits<
1293 support::detail::packed_endian_specific_integral<value_type, endian,
1295 typename std::enable_if<has_ScalarBitSetTraits<value_type>::value>
::type> {
1297 support::detail::packed_endian_specific_integral<value_type, endian,
1299 static void bitset(IO &io, endian_type &
E) {
1301 ScalarBitSetTraits<value_type>::bitset(io, V);
1308 template <
typename TNorm,
typename TFinal>
1309 struct MappingNormalization {
1310 MappingNormalization(IO &i_o, TFinal &Obj)
1311 : io(i_o), BufPtr(nullptr), Result(Obj) {
1312 if ( io.outputting() ) {
1313 BufPtr =
new (&Buffer) TNorm(io, Obj);
1316 BufPtr =
new (&Buffer) TNorm(io);
1320 ~MappingNormalization() {
1321 if ( ! io.outputting() ) {
1322 Result = BufPtr->denormalize(io);
1327 TNorm* operator->() {
return BufPtr; }
1330 using Storage = AlignedCharArrayUnion<TNorm>;
1340 template <
typename TNorm,
typename TFinal>
1341 struct MappingNormalizationHeap {
1342 MappingNormalizationHeap(IO &i_o, TFinal &Obj,
BumpPtrAllocator *allocator)
1343 : io(i_o), Result(Obj) {
1344 if ( io.outputting() ) {
1345 BufPtr =
new (&Buffer) TNorm(io, Obj);
1347 else if (allocator) {
1348 BufPtr = allocator->Allocate<TNorm>();
1349 new (BufPtr) TNorm(io);
1351 BufPtr =
new TNorm(io);
1355 ~MappingNormalizationHeap() {
1356 if ( io.outputting() ) {
1360 Result = BufPtr->denormalize(io);
1364 TNorm* operator->() {
return BufPtr; }
1367 using Storage = AlignedCharArrayUnion<TNorm>;
1371 TNorm *BufPtr =
nullptr;
1387 class Input :
public IO {
1392 Input(StringRef InputContent,
1393 void *Ctxt =
nullptr,
1395 void *DiagHandlerCtxt =
nullptr);
1396 Input(MemoryBufferRef Input,
1397 void *Ctxt =
nullptr,
1399 void *DiagHandlerCtxt =
nullptr);
1403 std::error_code
error();
1406 bool outputting()
const override;
1407 bool mapTag(StringRef,
bool)
override;
1408 void beginMapping()
override;
1409 void endMapping()
override;
1410 bool preflightKey(
const char *,
bool,
bool,
bool &,
void *&)
override;
1411 void postflightKey(
void *)
override;
1412 std::vector<StringRef> keys()
override;
1413 void beginFlowMapping()
override;
1414 void endFlowMapping()
override;
1415 unsigned beginSequence()
override;
1416 void endSequence()
override;
1417 bool preflightElement(
unsigned index,
void *&)
override;
1418 void postflightElement(
void *)
override;
1419 unsigned beginFlowSequence()
override;
1420 bool preflightFlowElement(
unsigned ,
void *&)
override;
1421 void postflightFlowElement(
void *)
override;
1422 void endFlowSequence()
override;
1423 void beginEnumScalar()
override;
1424 bool matchEnumScalar(
const char*,
bool)
override;
1425 bool matchEnumFallback()
override;
1426 void endEnumScalar()
override;
1427 bool beginBitSetScalar(
bool &)
override;
1428 bool bitSetMatch(
const char *,
bool )
override;
1429 void endBitSetScalar()
override;
1430 void scalarString(StringRef &, QuotingType)
override;
1431 void blockScalarString(StringRef &)
override;
1432 void scalarTag(std::string &)
override;
1434 void setError(
const Twine &message)
override;
1435 bool canElideEmptySequence()
override;
1438 virtual void anchor();
1441 HNode(
Node *n) : _node(n) { }
1442 virtual ~HNode() =
default;
1444 static bool classof(
const HNode *) {
return true; }
1449 class EmptyHNode :
public HNode {
1450 void anchor()
override;
1453 EmptyHNode(
Node *n) : HNode(n) { }
1457 static bool classof(
const EmptyHNode *) {
return true; }
1460 class ScalarHNode :
public HNode {
1461 void anchor()
override;
1464 ScalarHNode(
Node *n, StringRef s) : HNode(n), _value(s) { }
1466 StringRef value()
const {
return _value; }
1468 static bool classof(
const HNode *n) {
1473 static bool classof(
const ScalarHNode *) {
return true; }
1479 class MapHNode :
public HNode {
1480 void anchor()
override;
1483 MapHNode(
Node *n) : HNode(n) { }
1485 static bool classof(
const HNode *n) {
1489 static bool classof(
const MapHNode *) {
return true; }
1491 using NameToNode = StringMap<std::unique_ptr<HNode>>;
1494 SmallVector<std::string, 6> ValidKeys;
1497 class SequenceHNode :
public HNode {
1498 void anchor()
override;
1501 SequenceHNode(
Node *n) : HNode(n) { }
1503 static bool classof(
const HNode *n) {
1507 static bool classof(
const SequenceHNode *) {
return true; }
1509 std::vector<std::unique_ptr<HNode>> Entries;
1512 std::unique_ptr<Input::HNode> createHNodes(
Node *node);
1513 void setError(HNode *hnode,
const Twine &message);
1514 void setError(
Node *node,
const Twine &message);
1519 bool setCurrentDocument();
1520 bool nextDocument();
1523 const Node *getCurrentNode()
const;
1527 std::unique_ptr<llvm::yaml::Stream> Strm;
1528 std::unique_ptr<HNode> TopNode;
1531 document_iterator DocIterator;
1532 std::vector<bool> BitValuesUsed;
1533 HNode *CurrentNode =
nullptr;
1534 bool ScalarMatchFound =
false;
1541 class Output :
public IO {
1543 Output(raw_ostream &,
void *Ctxt =
nullptr,
int WrapColumn = 70);
1551 void setWriteDefaultValues(
bool Write) { WriteDefaultValues = Write; }
1553 bool outputting()
const override;
1554 bool mapTag(StringRef,
bool)
override;
1555 void beginMapping()
override;
1556 void endMapping()
override;
1557 bool preflightKey(
const char *key,
bool,
bool,
bool &,
void *&)
override;
1558 void postflightKey(
void *)
override;
1559 std::vector<StringRef> keys()
override;
1560 void beginFlowMapping()
override;
1561 void endFlowMapping()
override;
1562 unsigned beginSequence()
override;
1563 void endSequence()
override;
1564 bool preflightElement(
unsigned,
void *&)
override;
1565 void postflightElement(
void *)
override;
1566 unsigned beginFlowSequence()
override;
1567 bool preflightFlowElement(
unsigned,
void *&)
override;
1568 void postflightFlowElement(
void *)
override;
1569 void endFlowSequence()
override;
1570 void beginEnumScalar()
override;
1571 bool matchEnumScalar(
const char*,
bool)
override;
1572 bool matchEnumFallback()
override;
1573 void endEnumScalar()
override;
1574 bool beginBitSetScalar(
bool &)
override;
1575 bool bitSetMatch(
const char *,
bool )
override;
1576 void endBitSetScalar()
override;
1577 void scalarString(StringRef &, QuotingType)
override;
1578 void blockScalarString(StringRef &)
override;
1579 void scalarTag(std::string &)
override;
1581 void setError(
const Twine &message)
override;
1582 bool canElideEmptySequence()
override;
1586 void beginDocuments();
1587 bool preflightDocument(
unsigned);
1588 void postflightDocument();
1589 void endDocuments();
1592 void output(StringRef s);
1593 void outputUpToEndOfLine(StringRef s);
1594 void newLineCheck();
1595 void outputNewLine();
1596 void paddedKey(StringRef key);
1597 void flowKey(StringRef Key);
1602 inFlowSeqFirstElement,
1603 inFlowSeqOtherElement,
1610 static bool inSeqAnyElement(InState State);
1611 static bool inFlowSeqAnyElement(InState State);
1612 static bool inMapAnyKey(InState State);
1613 static bool inFlowMapAnyKey(InState State);
1617 SmallVector<InState, 8> StateStack;
1619 int ColumnAtFlowStart = 0;
1620 int ColumnAtMapFlowStart = 0;
1621 bool NeedBitValueComma =
false;
1622 bool NeedFlowSequenceComma =
false;
1623 bool EnumerationMatchFound =
false;
1624 bool WriteDefaultValues =
false;
1626 StringRef PaddingBeforeContainer;
1637 #define LLVM_YAML_STRONG_TYPEDEF(_base, _type) \ 1639 _type() = default; \ 1640 _type(const _base v) : value(v) {} \ 1641 _type(const _type &v) = default; \ 1642 _type &operator=(const _type &rhs) = default; \ 1643 _type &operator=(const _base &rhs) { value = rhs; return *this; } \ 1644 operator const _base & () const { return value; } \ 1645 bool operator==(const _type &rhs) const { return value == rhs.value; } \ 1646 bool operator==(const _base &rhs) const { return value == rhs; } \ 1647 bool operator<(const _type &rhs) const { return value < rhs.value; } \ 1649 using BaseType = _base; \ 1656 LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)
1657 LLVM_YAML_STRONG_TYPEDEF(
uint16_t, Hex16)
1658 LLVM_YAML_STRONG_TYPEDEF(
uint32_t, Hex32)
1659 LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)
1662 struct ScalarTraits<Hex8> {
1663 static void output(
const Hex8 &,
void *, raw_ostream &);
1664 static StringRef input(StringRef,
void *, Hex8 &);
1669 struct ScalarTraits<Hex16> {
1670 static void output(
const Hex16 &,
void *, raw_ostream &);
1671 static StringRef input(StringRef,
void *, Hex16 &);
1676 struct ScalarTraits<Hex32> {
1677 static void output(
const Hex32 &,
void *, raw_ostream &);
1678 static StringRef input(StringRef,
void *, Hex32 &);
1683 struct ScalarTraits<Hex64> {
1684 static void output(
const Hex64 &,
void *, raw_ostream &);
1685 static StringRef input(StringRef,
void *, Hex64 &);
1690 template <
typename T>
1692 typename std::enable_if<has_DocumentListTraits<T>::value, Input &>
::type 1696 while ( yin.setCurrentDocument() ) {
1697 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i),
true, Ctx);
1707 template <
typename T>
1708 inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value,
1712 yin.setCurrentDocument();
1713 yamlize(yin, docMap,
true, Ctx);
1719 template <
typename T>
1721 typename std::enable_if<has_SequenceTraits<T>::value, Input &>
::type 1724 if (yin.setCurrentDocument())
1725 yamlize(yin, docSeq,
true, Ctx);
1730 template <
typename T>
1732 typename std::enable_if<has_BlockScalarTraits<T>::value, Input &>
::type 1735 if (In.setCurrentDocument())
1736 yamlize(In, Val,
true, Ctx);
1741 template <
typename T>
1743 typename std::enable_if<has_CustomMappingTraits<T>::value, Input &>
::type 1746 if (In.setCurrentDocument())
1747 yamlize(In, Val,
true, Ctx);
1752 template <
typename T>
1753 inline typename std::enable_if<has_PolymorphicTraits<T>::value, Input &>
::type 1756 if (In.setCurrentDocument())
1757 yamlize(In, Val,
true, Ctx);
1762 template <
typename T>
1763 inline typename std::enable_if<missingTraits<T, EmptyContext>::value,
1766 char missing_yaml_trait_for_type[
sizeof(MissingTrait<T>)];
1771 template <
typename T>
1773 typename std::enable_if<has_DocumentListTraits<T>::value, Output &>
::type 1776 yout.beginDocuments();
1778 for(
size_t i=0; i <
count; ++i) {
1779 if ( yout.preflightDocument(i) ) {
1780 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i),
true,
1782 yout.postflightDocument();
1785 yout.endDocuments();
1790 template <
typename T>
1791 inline typename std::enable_if<has_MappingTraits<T, EmptyContext>::value,
1795 yout.beginDocuments();
1796 if ( yout.preflightDocument(0) ) {
1797 yamlize(yout, map,
true, Ctx);
1798 yout.postflightDocument();
1800 yout.endDocuments();
1805 template <
typename T>
1807 typename std::enable_if<has_SequenceTraits<T>::value, Output &>
::type 1810 yout.beginDocuments();
1811 if ( yout.preflightDocument(0) ) {
1812 yamlize(yout, seq,
true, Ctx);
1813 yout.postflightDocument();
1815 yout.endDocuments();
1820 template <
typename T>
1822 typename std::enable_if<has_BlockScalarTraits<T>::value, Output &>
::type 1825 Out.beginDocuments();
1826 if (Out.preflightDocument(0)) {
1827 yamlize(Out, Val,
true, Ctx);
1828 Out.postflightDocument();
1835 template <
typename T>
1837 typename std::enable_if<has_CustomMappingTraits<T>::value, Output &>
::type 1840 Out.beginDocuments();
1841 if (Out.preflightDocument(0)) {
1842 yamlize(Out, Val,
true, Ctx);
1843 Out.postflightDocument();
1851 template <
typename T>
1852 inline typename std::enable_if<has_PolymorphicTraits<T>::value, Output &>
::type 1855 Out.beginDocuments();
1856 if (Out.preflightDocument(0)) {
1859 assert(PolymorphicTraits<T>::getKind(Val) !=
NodeKind::Scalar &&
"plain scalar documents are not supported");
1860 yamlize(Out, Val,
true, Ctx);
1861 Out.postflightDocument();
1868 template <
typename T>
1869 inline typename std::enable_if<missingTraits<T, EmptyContext>::value,
1872 char missing_yaml_trait_for_type[
sizeof(MissingTrait<T>)];
1876 template <
bool B>
struct IsFlowSequenceBase {};
1877 template <>
struct IsFlowSequenceBase<
true> {
static const bool flow =
true; };
1879 template <
typename T,
bool Flow>
1880 struct SequenceTraitsImpl : IsFlowSequenceBase<Flow> {
1882 using type =
typename T::value_type;
1885 static size_t size(IO &io,
T &seq) {
return seq.size(); }
1887 static type &element(IO &io,
T &seq,
size_t index) {
1888 if (index >= seq.size())
1889 seq.resize(index + 1);
1896 template <
bool>
struct CheckIsBool {
static const bool value =
true; };
1900 template <
typename T>
1901 struct SequenceTraits<
std::vector<T>,
1902 typename std::enable_if<CheckIsBool<
1903 SequenceElementTraits<T>::flow>::value>
::type>
1904 : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
1905 template <
typename T,
unsigned N>
1906 struct SequenceTraits<SmallVector<
T,
N>,
1907 typename std::enable_if<CheckIsBool<
1908 SequenceElementTraits<T>::flow>::value>
::type>
1909 : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
1910 template <
typename T>
1911 struct SequenceTraits<SmallVectorImpl<
T>,
1912 typename std::enable_if<CheckIsBool<
1913 SequenceElementTraits<T>::flow>::value>
::type>
1914 : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {};
1917 template <
typename T>
1918 struct SequenceElementTraits<
1919 T, typename std::enable_if<std::is_fundamental<T>::value>
::type> {
1920 static const bool flow =
true;
1924 template<>
struct SequenceElementTraits<std::
string> {
1925 static const bool flow =
false;
1927 template<>
struct SequenceElementTraits<StringRef> {
1928 static const bool flow =
false;
1930 template<>
struct SequenceElementTraits<std::pair<std::string, std::string>> {
1931 static const bool flow =
false;
1935 template <
typename T>
struct StdMapStringCustomMappingTraitsImpl {
1936 using map_type = std::map<std::string, T>;
1938 static void inputOne(IO &io, StringRef key, map_type &v) {
1939 io.mapRequired(key.str().c_str(), v[key]);
1942 static void output(IO &io, map_type &v) {
1944 io.mapRequired(p.first.c_str(), p.second);
1951 #define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW) \ 1955 !std::is_fundamental<TYPE>::value && \ 1956 !std::is_same<TYPE, std::string>::value && \ 1957 !std::is_same<TYPE, llvm::StringRef>::value, \ 1958 "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \ 1959 template <> struct SequenceElementTraits<TYPE> { \ 1960 static const bool flow = FLOW; \ 1967 #define LLVM_YAML_IS_SEQUENCE_VECTOR(type) \ 1968 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false) 1972 #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type) \ 1973 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true) 1975 #define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type) \ 1978 template <> struct MappingTraits<Type> { \ 1979 static void mapping(IO &IO, Type &Obj); \ 1984 #define LLVM_YAML_DECLARE_ENUM_TRAITS(Type) \ 1987 template <> struct ScalarEnumerationTraits<Type> { \ 1988 static void enumeration(IO &io, Type &Value); \ 1993 #define LLVM_YAML_DECLARE_BITSET_TRAITS(Type) \ 1996 template <> struct ScalarBitSetTraits<Type> { \ 1997 static void bitset(IO &IO, Type &Options); \ 2002 #define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote) \ 2005 template <> struct ScalarTraits<Type> { \ 2006 static void output(const Type &Value, void *ctx, raw_ostream &Out); \ 2007 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \ 2008 static QuotingType mustQuote(StringRef) { return MustQuote; } \ 2015 #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type) \ 2018 template <unsigned N> \ 2019 struct DocumentListTraits<SmallVector<_type, N>> \ 2020 : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \ 2022 struct DocumentListTraits<std::vector<_type>> \ 2023 : public SequenceTraitsImpl<std::vector<_type>, false> {}; \ 2029 #define LLVM_YAML_IS_STRING_MAP(_type) \ 2033 struct CustomMappingTraits<std::map<std::string, _type>> \ 2034 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \ 2038 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)
2039 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)
2040 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)
2041 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)
2043 #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.
This class represents lattice values for constants.
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 void DiagHandler(const SMDiagnostic &Diag, void *Context)
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")
Determine the kind of a node from its type.
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)
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
Tail - This calling convention attemps to make calls as fast as possible while guaranteeing that tail...
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)
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
static void mapOptional(yaml::IO &IO, const char *Key, EndianType &Val, typename EndianType::value_type Default)
Perform an optional yaml-mapping of an endian-aware type EndianType.