| 1 | //===-- MsgPackDocumentYAML.cpp - MsgPack Document YAML interface -------*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// This file implements YAMLIO on a msgpack::Document. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/BinaryFormat/MsgPackDocument.h" |
| 14 | #include "llvm/Support/YAMLTraits.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace msgpack; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Struct used to represent scalar node. (MapDocNode and ArrayDocNode already |
| 22 | // exist in MsgPackDocument.h.) |
| 23 | struct ScalarDocNode : DocNode { |
| 24 | ScalarDocNode(DocNode N) : DocNode(N) {} |
| 25 | |
| 26 | /// Get the YAML tag for this ScalarDocNode. This normally returns ""; it only |
| 27 | /// returns something else if the result of toString would be ambiguous, e.g. |
| 28 | /// a string that parses as a number or boolean. |
| 29 | StringRef getYAMLTag() const; |
| 30 | }; |
| 31 | |
| 32 | bool isJSONSchemaBoolLiteral(StringRef S) { |
| 33 | return S == "true" || S == "false" ; |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | /// Convert this DocNode to a string, assuming it is scalar. |
| 39 | std::string DocNode::toString() const { |
| 40 | std::string S; |
| 41 | raw_string_ostream OS(S); |
| 42 | switch (getKind()) { |
| 43 | case msgpack::Type::String: |
| 44 | OS << Raw; |
| 45 | break; |
| 46 | case msgpack::Type::Nil: |
| 47 | break; |
| 48 | case msgpack::Type::Boolean: |
| 49 | OS << (Bool ? "true" : "false" ); |
| 50 | break; |
| 51 | case msgpack::Type::Int: |
| 52 | OS << Int; |
| 53 | break; |
| 54 | case msgpack::Type::UInt: |
| 55 | if (getDocument()->getHexMode()) |
| 56 | OS << format(Fmt: "%#llx" , Vals: (unsigned long long)UInt); |
| 57 | else |
| 58 | OS << UInt; |
| 59 | break; |
| 60 | case msgpack::Type::Float: |
| 61 | OS << Float; |
| 62 | break; |
| 63 | default: |
| 64 | llvm_unreachable("not scalar" ); |
| 65 | break; |
| 66 | } |
| 67 | return OS.str(); |
| 68 | } |
| 69 | |
| 70 | /// Convert the StringRef and use it to set this DocNode (assuming scalar). If |
| 71 | /// it is a string, copy the string into the Document's strings list so we do |
| 72 | /// not rely on S having a lifetime beyond this call. Tag is "" or a YAML tag. |
| 73 | StringRef DocNode::fromString(StringRef S, StringRef Tag) { |
| 74 | if (Tag == "tag:yaml.org,2002:str" ) |
| 75 | Tag = "" ; |
| 76 | if (Tag == "!int" || Tag == "" ) { |
| 77 | // Try unsigned int then signed int. |
| 78 | *this = getDocument()->getNode(V: uint64_t(0)); |
| 79 | StringRef Err = yaml::ScalarTraits<uint64_t>::input(S, nullptr, getUInt()); |
| 80 | if (Err != "" ) { |
| 81 | *this = getDocument()->getNode(V: int64_t(0)); |
| 82 | Err = yaml::ScalarTraits<int64_t>::input(S, nullptr, getInt()); |
| 83 | } |
| 84 | if (Err == "" || Tag != "" ) |
| 85 | return Err; |
| 86 | } |
| 87 | if (Tag == "!nil" ) { |
| 88 | *this = getDocument()->getNode(); |
| 89 | return "" ; |
| 90 | } |
| 91 | if (Tag == "!bool" ) { |
| 92 | *this = getDocument()->getNode(V: false); |
| 93 | return yaml::ScalarTraits<bool>::input(S, nullptr, getBool()); |
| 94 | } |
| 95 | // FIXME: This enforces the "JSON Schema" tag resolution for boolean literals, |
| 96 | // defined at https://yaml.org/spec/1.2.2/#json-schema which we adopt because |
| 97 | // the more general pre-1.2 resolution includes many common strings (e.g. "n", |
| 98 | // "no", "y", "yes", ...). This should be handled at the YAMLTraits level, but |
| 99 | // that change would have a much broader impact. |
| 100 | if (Tag == "" && isJSONSchemaBoolLiteral(S)) { |
| 101 | *this = getDocument()->getNode(V: S == "true" ); |
| 102 | return "" ; |
| 103 | } |
| 104 | if (Tag == "!float" || Tag == "" ) { |
| 105 | *this = getDocument()->getNode(V: 0.0); |
| 106 | StringRef Err = yaml::ScalarTraits<double>::input(S, nullptr, getFloat()); |
| 107 | if (Err == "" || Tag != "" ) |
| 108 | return Err; |
| 109 | } |
| 110 | assert((Tag == "!str" || Tag == "" ) && "unsupported tag" ); |
| 111 | std::string V; |
| 112 | StringRef Err = yaml::ScalarTraits<std::string>::input(S, nullptr, V); |
| 113 | if (Err == "" ) |
| 114 | *this = getDocument()->getNode(V, /*Copy=*/true); |
| 115 | return Err; |
| 116 | } |
| 117 | |
| 118 | /// Get the YAML tag for this ScalarDocNode. This normally returns ""; it only |
| 119 | /// returns something else if the result of toString would be ambiguous, e.g. |
| 120 | /// a string that parses as a number or boolean. |
| 121 | StringRef ScalarDocNode::getYAMLTag() const { |
| 122 | if (getKind() == msgpack::Type::Nil) |
| 123 | return "!nil" ; |
| 124 | // Try converting both ways and see if we get the same kind. If not, we need |
| 125 | // a tag. |
| 126 | ScalarDocNode N = getDocument()->getNode(); |
| 127 | N.fromString(S: toString(), Tag: "" ); |
| 128 | if (N.getKind() == getKind()) |
| 129 | return "" ; |
| 130 | // Tolerate signedness of int changing, as tags do not differentiate between |
| 131 | // them anyway. |
| 132 | if (N.getKind() == msgpack::Type::UInt && getKind() == msgpack::Type::Int) |
| 133 | return "" ; |
| 134 | if (N.getKind() == msgpack::Type::Int && getKind() == msgpack::Type::UInt) |
| 135 | return "" ; |
| 136 | // We do need a tag. |
| 137 | switch (getKind()) { |
| 138 | case msgpack::Type::String: |
| 139 | return "!str" ; |
| 140 | case msgpack::Type::Int: |
| 141 | return "!int" ; |
| 142 | case msgpack::Type::UInt: |
| 143 | return "!int" ; |
| 144 | case msgpack::Type::Boolean: |
| 145 | return "!bool" ; |
| 146 | case msgpack::Type::Float: |
| 147 | return "!float" ; |
| 148 | default: |
| 149 | llvm_unreachable("unrecognized kind" ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | namespace llvm { |
| 154 | namespace yaml { |
| 155 | |
| 156 | /// YAMLIO for DocNode |
| 157 | template <> struct PolymorphicTraits<DocNode> { |
| 158 | |
| 159 | static NodeKind getKind(const DocNode &N) { |
| 160 | switch (N.getKind()) { |
| 161 | case msgpack::Type::Map: |
| 162 | return NodeKind::Map; |
| 163 | case msgpack::Type::Array: |
| 164 | return NodeKind::Sequence; |
| 165 | default: |
| 166 | return NodeKind::Scalar; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static MapDocNode &getAsMap(DocNode &N) { return N.getMap(/*Convert=*/true); } |
| 171 | |
| 172 | static ArrayDocNode &getAsSequence(DocNode &N) { |
| 173 | N.getArray(/*Convert=*/true); |
| 174 | return *static_cast<ArrayDocNode *>(&N); |
| 175 | } |
| 176 | |
| 177 | static ScalarDocNode &getAsScalar(DocNode &N) { |
| 178 | return *static_cast<ScalarDocNode *>(&N); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | /// YAMLIO for ScalarDocNode |
| 183 | template <> struct TaggedScalarTraits<ScalarDocNode> { |
| 184 | |
| 185 | static void output(const ScalarDocNode &S, void *Ctxt, raw_ostream &OS, |
| 186 | raw_ostream &TagOS) { |
| 187 | TagOS << S.getYAMLTag(); |
| 188 | OS << S.toString(); |
| 189 | } |
| 190 | |
| 191 | static StringRef input(StringRef Str, StringRef Tag, void *Ctxt, |
| 192 | ScalarDocNode &S) { |
| 193 | return S.fromString(S: Str, Tag); |
| 194 | } |
| 195 | |
| 196 | static QuotingType mustQuote(const ScalarDocNode &S, StringRef ScalarStr) { |
| 197 | switch (S.getKind()) { |
| 198 | case Type::Int: |
| 199 | return ScalarTraits<int64_t>::mustQuote(ScalarStr); |
| 200 | case Type::UInt: |
| 201 | return ScalarTraits<uint64_t>::mustQuote(ScalarStr); |
| 202 | case Type::Nil: |
| 203 | return ScalarTraits<StringRef>::mustQuote(S: ScalarStr); |
| 204 | case Type::Boolean: |
| 205 | return ScalarTraits<bool>::mustQuote(ScalarStr); |
| 206 | case Type::Float: |
| 207 | return ScalarTraits<double>::mustQuote(ScalarStr); |
| 208 | case Type::Binary: |
| 209 | case Type::String: |
| 210 | return ScalarTraits<std::string>::mustQuote(S: ScalarStr); |
| 211 | default: |
| 212 | llvm_unreachable("unrecognized ScalarKind" ); |
| 213 | } |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | /// YAMLIO for MapDocNode |
| 218 | template <> struct CustomMappingTraits<MapDocNode> { |
| 219 | |
| 220 | static void inputOne(IO &IO, StringRef Key, MapDocNode &M) { |
| 221 | ScalarDocNode KeyObj = M.getDocument()->getNode(); |
| 222 | KeyObj.fromString(S: Key, Tag: "" ); |
| 223 | IO.mapRequired(Key, Val&: M.getMap()[KeyObj]); |
| 224 | } |
| 225 | |
| 226 | static void output(IO &IO, MapDocNode &M) { |
| 227 | for (auto I : M.getMap()) { |
| 228 | IO.mapRequired(Key: I.first.toString(), Val&: I.second); |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | /// YAMLIO for ArrayNode |
| 234 | template <> struct SequenceTraits<ArrayDocNode> { |
| 235 | |
| 236 | static size_t size(IO &IO, ArrayDocNode &A) { return A.size(); } |
| 237 | |
| 238 | static DocNode &element(IO &IO, ArrayDocNode &A, size_t Index) { |
| 239 | return A[Index]; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | } // namespace yaml |
| 244 | } // namespace llvm |
| 245 | |
| 246 | /// Convert MsgPack Document to YAML text. |
| 247 | void msgpack::Document::toYAML(raw_ostream &OS) { |
| 248 | yaml::Output Yout(OS); |
| 249 | Yout << getRoot(); |
| 250 | } |
| 251 | |
| 252 | /// Read YAML text into the MsgPack document. Returns false on failure. |
| 253 | bool msgpack::Document::fromYAML(StringRef S) { |
| 254 | clear(); |
| 255 | yaml::Input Yin(S); |
| 256 | Yin >> getRoot(); |
| 257 | return !Yin.error(); |
| 258 | } |
| 259 | |
| 260 | |