diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h index 8642069ad540..712633a8b21f 100644 --- a/llvm/include/llvm/Support/YAMLTraits.h +++ b/llvm/include/llvm/Support/YAMLTraits.h @@ -1123,16 +1123,42 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { char missing_yaml_trait_for_type[sizeof(MissingTrait)]; } +template +class DefaultInserter { +private: + IO &io; + T &Seq; + +public: + DefaultInserter(IO &io, T &Seq) : io(io), Seq(Seq) {} + + auto &preflightElement(unsigned i) { + return SequenceTraits::element(io, Seq, i); + } + + void postflightElement(unsigned i) {}; +}; + +template +auto getInserter(const T &) -> typename SequenceTraits::Inserter { + return std::declval::Inserter>(); +} + +template +DefaultInserter getInserter(...) { return DefaultInserter{}; } + template typename std::enable_if::value, void>::type yamlize(IO &io, T &Seq, bool, Context &Ctx) { + decltype(getInserter(std::declval())) I(io, Seq); if ( has_FlowTraits< SequenceTraits>::value ) { unsigned incnt = io.beginFlowSequence(); unsigned count = io.outputting() ? SequenceTraits::size(io, Seq) : incnt; for(unsigned i=0; i < count; ++i) { void *SaveInfo; if ( io.preflightFlowElement(i, SaveInfo) ) { - yamlize(io, SequenceTraits::element(io, Seq, i), true, Ctx); + yamlize(io, I.preflightElement(i), true, Ctx); + I.postflightElement(i); io.postflightFlowElement(SaveInfo); } } @@ -1144,7 +1170,8 @@ yamlize(IO &io, T &Seq, bool, Context &Ctx) { for(unsigned i=0; i < count; ++i) { void *SaveInfo; if ( io.preflightElement(i, SaveInfo) ) { - yamlize(io, SequenceTraits::element(io, Seq, i), true, Ctx); + yamlize(io, I.preflightElement(i), true, Ctx); + I.postflightElement(i); io.postflightElement(SaveInfo); } }