LLVM  14.0.0git
MsgPackDocumentYAML.cpp
Go to the documentation of this file.
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 
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 } // namespace
33 
34 /// Convert this DocNode to a string, assuming it is scalar.
35 std::string DocNode::toString() const {
36  std::string S;
38  switch (getKind()) {
40  OS << Raw;
41  break;
42  case msgpack::Type::Nil:
43  break;
45  OS << (Bool ? "true" : "false");
46  break;
47  case msgpack::Type::Int:
48  OS << Int;
49  break;
51  if (getDocument()->getHexMode())
52  OS << format("%#llx", (unsigned long long)UInt);
53  else
54  OS << UInt;
55  break;
57  OS << Float;
58  break;
59  default:
60  llvm_unreachable("not scalar");
61  break;
62  }
63  return OS.str();
64 }
65 
66 /// Convert the StringRef and use it to set this DocNode (assuming scalar). If
67 /// it is a string, copy the string into the Document's strings list so we do
68 /// not rely on S having a lifetime beyond this call. Tag is "" or a YAML tag.
70  if (Tag == "tag:yaml.org,2002:str")
71  Tag = "";
72  if (Tag == "!int" || Tag == "") {
73  // Try unsigned int then signed int.
74  *this = getDocument()->getNode(uint64_t(0));
76  if (Err != "") {
77  *this = getDocument()->getNode(int64_t(0));
78  Err = yaml::ScalarTraits<int64_t>::input(S, nullptr, getInt());
79  }
80  if (Err == "" || Tag != "")
81  return Err;
82  }
83  if (Tag == "!nil") {
84  *this = getDocument()->getNode();
85  return "";
86  }
87  if (Tag == "!bool" || Tag == "") {
88  *this = getDocument()->getNode(false);
90  if (Err == "" || Tag != "")
91  return Err;
92  }
93  if (Tag == "!float" || Tag == "") {
94  *this = getDocument()->getNode(0.0);
96  if (Err == "" || Tag != "")
97  return Err;
98  }
99  assert((Tag == "!str" || Tag == "") && "unsupported tag");
100  std::string V;
102  if (Err == "")
103  *this = getDocument()->getNode(V, /*Copy=*/true);
104  return Err;
105 }
106 
107 /// Get the YAML tag for this ScalarDocNode. This normally returns ""; it only
108 /// returns something else if the result of toString would be ambiguous, e.g.
109 /// a string that parses as a number or boolean.
110 StringRef ScalarDocNode::getYAMLTag() const {
111  if (getKind() == msgpack::Type::Nil)
112  return "!nil";
113  // Try converting both ways and see if we get the same kind. If not, we need
114  // a tag.
115  ScalarDocNode N = getDocument()->getNode();
116  N.fromString(toString(), "");
117  if (N.getKind() == getKind())
118  return "";
119  // Tolerate signedness of int changing, as tags do not differentiate between
120  // them anyway.
121  if (N.getKind() == msgpack::Type::UInt && getKind() == msgpack::Type::Int)
122  return "";
123  if (N.getKind() == msgpack::Type::Int && getKind() == msgpack::Type::UInt)
124  return "";
125  // We do need a tag.
126  switch (getKind()) {
128  return "!str";
129  case msgpack::Type::Int:
130  return "!int";
131  case msgpack::Type::UInt:
132  return "!int";
134  return "!bool";
136  return "!float";
137  default:
138  llvm_unreachable("unrecognized kind");
139  }
140 }
141 
142 namespace llvm {
143 namespace yaml {
144 
145 /// YAMLIO for DocNode
146 template <> struct PolymorphicTraits<DocNode> {
147 
148  static NodeKind getKind(const DocNode &N) {
149  switch (N.getKind()) {
150  case msgpack::Type::Map:
151  return NodeKind::Map;
153  return NodeKind::Sequence;
154  default:
155  return NodeKind::Scalar;
156  }
157  }
158 
159  static MapDocNode &getAsMap(DocNode &N) { return N.getMap(/*Convert=*/true); }
160 
162  N.getArray(/*Convert=*/true);
163  return *static_cast<ArrayDocNode *>(&N);
164  }
165 
166  static ScalarDocNode &getAsScalar(DocNode &N) {
167  return *static_cast<ScalarDocNode *>(&N);
168  }
169 };
170 
171 /// YAMLIO for ScalarDocNode
172 template <> struct TaggedScalarTraits<ScalarDocNode> {
173 
174  static void output(const ScalarDocNode &S, void *Ctxt, raw_ostream &OS,
175  raw_ostream &TagOS) {
176  TagOS << S.getYAMLTag();
177  OS << S.toString();
178  }
179 
180  static StringRef input(StringRef Str, StringRef Tag, void *Ctxt,
181  ScalarDocNode &S) {
182  return S.fromString(Str, Tag);
183  }
184 
185  static QuotingType mustQuote(const ScalarDocNode &S, StringRef ScalarStr) {
186  switch (S.getKind()) {
187  case Type::Int:
188  return ScalarTraits<int64_t>::mustQuote(ScalarStr);
189  case Type::UInt:
190  return ScalarTraits<uint64_t>::mustQuote(ScalarStr);
191  case Type::Nil:
192  return ScalarTraits<StringRef>::mustQuote(ScalarStr);
193  case Type::Boolean:
194  return ScalarTraits<bool>::mustQuote(ScalarStr);
195  case Type::Float:
196  return ScalarTraits<double>::mustQuote(ScalarStr);
197  case Type::Binary:
198  case Type::String:
199  return ScalarTraits<std::string>::mustQuote(ScalarStr);
200  default:
201  llvm_unreachable("unrecognized ScalarKind");
202  }
203  }
204 };
205 
206 /// YAMLIO for MapDocNode
207 template <> struct CustomMappingTraits<MapDocNode> {
208 
209  static void inputOne(IO &IO, StringRef Key, MapDocNode &M) {
210  ScalarDocNode KeyObj = M.getDocument()->getNode();
211  KeyObj.fromString(Key, "");
212  IO.mapRequired(Key.str().c_str(), M.getMap()[KeyObj]);
213  }
214 
215  static void output(IO &IO, MapDocNode &M) {
216  for (auto I : M.getMap()) {
217  IO.mapRequired(I.first.toString().c_str(), I.second);
218  }
219  }
220 };
221 
222 /// YAMLIO for ArrayNode
223 template <> struct SequenceTraits<ArrayDocNode> {
224 
225  static size_t size(IO &IO, ArrayDocNode &A) { return A.size(); }
226 
227  static DocNode &element(IO &IO, ArrayDocNode &A, size_t Index) {
228  return A[Index];
229  }
230 };
231 
232 } // namespace yaml
233 } // namespace llvm
234 
235 /// Convert MsgPack Document to YAML text.
237  yaml::Output Yout(OS);
238  Yout << getRoot();
239 }
240 
241 /// Read YAML text into the MsgPack document. Returns false on failure.
243  clear();
244  yaml::Input Yin(S);
245  Yin >> getRoot();
246  return !Yin.error();
247 }
248 
llvm::yaml::TaggedScalarTraits< ScalarDocNode >::mustQuote
static QuotingType mustQuote(const ScalarDocNode &S, StringRef ScalarStr)
Definition: MsgPackDocumentYAML.cpp:185
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::msgpack::DocNode::Int
int64_t Int
Definition: MsgPackDocument.h:55
llvm::objcarc::Sequence
Sequence
Definition: PtrState.h:41
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::msgpack::DocNode::getDocument
Document * getDocument() const
Definition: MsgPackDocument.h:80
llvm::yaml::SequenceTraits< ArrayDocNode >::element
static DocNode & element(IO &IO, ArrayDocNode &A, size_t Index)
Definition: MsgPackDocumentYAML.cpp:227
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
llvm::toString
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:1020
llvm::yaml::PolymorphicTraits< DocNode >::getAsSequence
static ArrayDocNode & getAsSequence(DocNode &N)
Definition: MsgPackDocumentYAML.cpp:161
llvm::msgpack::DocNode::UInt
uint64_t UInt
Definition: MsgPackDocument.h:56
llvm::msgpack::Type::Array
@ Array
llvm::msgpack::DocNode::getUInt
uint64_t & getUInt()
Definition: MsgPackDocument.h:87
llvm::msgpack::Type::Binary
@ Binary
llvm::msgpack::Type::Int
@ Int
clear
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:233
llvm::msgpack::Type::Map
@ Map
llvm::msgpack::DocNode
A node in a MsgPack Document.
Definition: MsgPackDocument.h:38
llvm::msgpack::Type::UInt
@ UInt
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::msgpack::Type::String
@ String
input
The initial backend is deliberately restricted to z10 We should add support for later architectures at some point If an asm ties an i32 r result to an i64 input
Definition: README.txt:10
llvm::NVPTX::PTXLdStInstCode::Scalar
@ Scalar
Definition: NVPTX.h:122
llvm::msgpack::Document::fromYAML
bool fromYAML(StringRef S)
Read YAML text into the MsgPack document. Returns false on failure.
Definition: MsgPackDocumentYAML.cpp:242
llvm::msgpack::DocNode::toString
std::string toString() const
Convert this node to a string, assuming it is scalar.
Definition: MsgPackDocumentYAML.cpp:35
llvm::msgpack::MapDocNode
A DocNode that is a map.
Definition: MsgPackDocument.h:219
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
uint64_t
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::yaml::PolymorphicTraits< DocNode >::getKind
static NodeKind getKind(const DocNode &N)
Definition: MsgPackDocumentYAML.cpp:148
MsgPackDocument.h
llvm::yaml::TaggedScalarTraits< ScalarDocNode >::input
static StringRef input(StringRef Str, StringRef Tag, void *Ctxt, ScalarDocNode &S)
Definition: MsgPackDocumentYAML.cpp:180
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::msgpack::Document::getNode
DocNode getNode()
Create a nil node associated with this Document.
Definition: MsgPackDocument.h:308
llvm::msgpack::DocNode::getFloat
double & getFloat()
Definition: MsgPackDocument.h:97
YAMLTraits.h
llvm::yaml::CustomMappingTraits< MapDocNode >::output
static void output(IO &IO, MapDocNode &M)
Definition: MsgPackDocumentYAML.cpp:215
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::msgpack::DocNode::Raw
StringRef Raw
Definition: MsgPackDocument.h:59
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::msgpack::Document::getHexMode
bool getHexMode() const
Get Hexmode flag.
Definition: MsgPackDocument.h:432
llvm::msgpack::DocNode::fromString
StringRef fromString(StringRef S, StringRef Tag="")
Convert the StringRef and use it to set this DocNode (assuming scalar).
Definition: MsgPackDocumentYAML.cpp:69
llvm::msgpack::Document::toYAML
void toYAML(raw_ostream &OS)
Convert MsgPack Document to YAML text.
Definition: MsgPackDocumentYAML.cpp:236
llvm::msgpack::DocNode::Bool
bool Bool
Definition: MsgPackDocument.h:57
llvm::yaml::CustomMappingTraits< MapDocNode >::inputOne
static void inputOne(IO &IO, StringRef Key, MapDocNode &M)
Definition: MsgPackDocumentYAML.cpp:209
llvm::msgpack::Type::Boolean
@ Boolean
llvm::msgpack::DocNode::getBool
bool & getBool()
Definition: MsgPackDocument.h:92
llvm::msgpack::Type::Float
@ Float
llvm::yaml::PolymorphicTraits< DocNode >::getAsScalar
static ScalarDocNode & getAsScalar(DocNode &N)
Definition: MsgPackDocumentYAML.cpp:166
llvm::yaml::PolymorphicTraits< DocNode >::getAsMap
static MapDocNode & getAsMap(DocNode &N)
Definition: MsgPackDocumentYAML.cpp:159
llvm::msgpack::ArrayDocNode
A DocNode that is an array.
Definition: MsgPackDocument.h:249
llvm::ms_demangle::NodeKind
NodeKind
Definition: MicrosoftDemangleNodes.h:226
llvm::msgpack::DocNode::Float
double Float
Definition: MsgPackDocument.h:58
N
#define N
llvm::yaml::SequenceTraits< ArrayDocNode >::size
static size_t size(IO &IO, ArrayDocNode &A)
Definition: MsgPackDocumentYAML.cpp:225
llvm::yaml::TaggedScalarTraits< ScalarDocNode >::output
static void output(const ScalarDocNode &S, void *Ctxt, raw_ostream &OS, raw_ostream &TagOS)
Definition: MsgPackDocumentYAML.cpp:174
llvm::msgpack::DocNode::getKind
Type getKind() const
Definition: MsgPackDocument.h:79
llvm::msgpack::Type::Nil
@ Nil
llvm::raw_string_ostream::str
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:643
llvm::msgpack::DocNode::getInt
int64_t & getInt()
Definition: MsgPackDocument.h:82