blob: 183da5b3900228f1f88b888f5de57ec1144e4781 [file] [log] [blame]
Nick Kledzikf60a9272012-12-12 20:46:15 +00001//===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Nick Kledzikf60a9272012-12-12 20:46:15 +00006//
7//===----------------------------------------------------------------------===//
8
Benjamin Kramer16132e62015-03-23 18:07:13 +00009#include "llvm/Support/YAMLTraits.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000010#include "llvm/ADT/STLExtras.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000011#include "llvm/ADT/SmallString.h"
Pavel Labathec000f42017-06-23 12:55:02 +000012#include "llvm/ADT/StringExtras.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000013#include "llvm/ADT/StringRef.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000014#include "llvm/ADT/Twine.h"
15#include "llvm/Support/Casting.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000016#include "llvm/Support/Errc.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000017#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000018#include "llvm/Support/Format.h"
Alex Lorenz68e787b2015-05-14 23:08:22 +000019#include "llvm/Support/LineIterator.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000020#include "llvm/Support/MemoryBuffer.h"
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +000021#include "llvm/Support/Unicode.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000022#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000023#include "llvm/Support/raw_ostream.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000024#include <algorithm>
25#include <cassert>
26#include <cstdint>
27#include <cstdlib>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include <cstring>
Eugene Zelenko72208a82017-06-21 23:19:47 +000029#include <string>
30#include <vector>
31
Benjamin Kramer36b0f122012-12-12 22:40:02 +000032using namespace llvm;
33using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000034
35//===----------------------------------------------------------------------===//
36// IO
37//===----------------------------------------------------------------------===//
38
Eugene Zelenko72208a82017-06-21 23:19:47 +000039IO::IO(void *Context) : Ctxt(Context) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +000040
Eugene Zelenko72208a82017-06-21 23:19:47 +000041IO::~IO() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +000042
43void *IO::getContext() {
44 return Ctxt;
45}
46
47void IO::setContext(void *Context) {
48 Ctxt = Context;
49}
50
Nick Kledzikf60a9272012-12-12 20:46:15 +000051//===----------------------------------------------------------------------===//
52// Input
53//===----------------------------------------------------------------------===//
54
Mehdi Amini3ab3fef2016-11-28 21:38:52 +000055Input::Input(StringRef InputContent, void *Ctxt,
56 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
Eugene Zelenko72208a82017-06-21 23:19:47 +000057 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000058 if (DiagHandler)
59 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000060 DocIterator = Strm->begin();
61}
62
Alex Bradbury16843172017-07-17 11:41:30 +000063Input::Input(MemoryBufferRef Input, void *Ctxt,
64 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
65 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
66 if (DiagHandler)
67 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
68 DocIterator = Strm->begin();
69}
70
Eugene Zelenko72208a82017-06-21 23:19:47 +000071Input::~Input() = default;
Nick Kledzik0dcef842013-01-08 21:04:44 +000072
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000073std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000074
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000075// Pin the vtables to this file.
76void Input::HNode::anchor() {}
77void Input::EmptyHNode::anchor() {}
78void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000079void Input::MapHNode::anchor() {}
80void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000081
Nick Kledzik4761c602013-11-21 00:20:10 +000082bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000083 return false;
84}
85
86bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000087 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000088 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000089 if (!N) {
Alex Lorenz7a38d752015-05-12 17:44:32 +000090 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000091 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000092 return false;
93 }
94
Benjamin Kramer36b0f122012-12-12 22:40:02 +000095 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000096 // Empty files are allowed and ignored
97 ++DocIterator;
98 return setCurrentDocument();
99 }
Scott Linderad115b72018-10-10 18:14:02 +0000100 TopNode = createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +0000101 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000102 return true;
103 }
104 return false;
105}
106
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000107bool Input::nextDocument() {
108 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000109}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +0000110
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000111const Node *Input::getCurrentNode() const {
112 return CurrentNode ? CurrentNode->_node : nullptr;
113}
114
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000115bool Input::mapTag(StringRef Tag, bool Default) {
George Rimar45d042e2019-04-25 09:59:55 +0000116 // CurrentNode can be null if setCurrentDocument() was unable to
117 // parse the document because it was invalid or empty.
118 if (!CurrentNode)
119 return false;
120
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000121 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000122 if (foundTag.empty()) {
123 // If no tag found and 'Tag' is the default, say it was found.
124 return Default;
125 }
Alex Lorenz7a38d752015-05-12 17:44:32 +0000126 // Return true iff found tag matches supplied tag.
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000127 return Tag.equals(foundTag);
128}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000129
130void Input::beginMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000131 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000132 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000133 // CurrentNode can be null if the document is empty.
134 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000135 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000136 MN->ValidKeys.clear();
137 }
138}
139
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000140std::vector<StringRef> Input::keys() {
141 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
142 std::vector<StringRef> Ret;
143 if (!MN) {
144 setError(CurrentNode, "not a mapping");
145 return Ret;
146 }
147 for (auto &P : MN->Mapping)
148 Ret.push_back(P.first());
149 return Ret;
150}
151
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000152bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
153 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000154 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000155 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000156 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000157
158 // CurrentNode is null for empty documents, which is an error in case required
159 // nodes are present.
160 if (!CurrentNode) {
161 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000162 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000163 return false;
164 }
165
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000166 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
167 if (!MN) {
Dave Leec6f2e692017-11-16 17:46:11 +0000168 if (Required || !isa<EmptyHNode>(CurrentNode))
169 setError(CurrentNode, "not a mapping");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170 return false;
171 }
172 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000173 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000174 if (!Value) {
175 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000176 setError(CurrentNode, Twine("missing required key '") + Key + "'");
177 else
178 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000179 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180 }
181 SaveInfo = CurrentNode;
182 CurrentNode = Value;
183 return true;
184}
185
186void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000187 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000188}
189
190void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000191 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000193 // CurrentNode can be null if the document is empty.
194 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000195 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000196 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000197 for (const auto &NN : MN->Mapping) {
Peter Collingbourneefdff712017-01-04 20:10:43 +0000198 if (!is_contained(MN->ValidKeys, NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000199 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000200 break;
201 }
202 }
203}
204
Alex Lorenzb1225082015-05-04 20:11:40 +0000205void Input::beginFlowMapping() { beginMapping(); }
206
207void Input::endFlowMapping() { endMapping(); }
208
Nick Kledzikf60a9272012-12-12 20:46:15 +0000209unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000210 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000212 if (isa<EmptyHNode>(CurrentNode))
213 return 0;
214 // Treat case where there's a scalar "null" value as an empty sequence.
215 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
216 if (isNull(SN->value()))
217 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000218 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000219 // Any other type of HNode is an error.
220 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000221 return 0;
222}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000223
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224void Input::endSequence() {
225}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000226
Nick Kledzikf60a9272012-12-12 20:46:15 +0000227bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000228 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000229 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000230 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000231 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000232 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000233 return true;
234 }
235 return false;
236}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000237
Nick Kledzikf60a9272012-12-12 20:46:15 +0000238void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000239 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000240}
241
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000242unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000243
Nick Kledzikf60a9272012-12-12 20:46:15 +0000244bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000245 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000246 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000247 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000248 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000249 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000250 return true;
251 }
252 return false;
253}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000256 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257}
258
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000259void Input::endFlowSequence() {
260}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000261
262void Input::beginEnumScalar() {
263 ScalarMatchFound = false;
264}
265
266bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000267 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000268 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000269 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
270 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000271 ScalarMatchFound = true;
272 return true;
273 }
274 }
275 return false;
276}
277
Michael J. Spencer731cae32015-01-23 21:57:50 +0000278bool Input::matchEnumFallback() {
279 if (ScalarMatchFound)
280 return false;
281 ScalarMatchFound = true;
282 return true;
283}
284
Nick Kledzikf60a9272012-12-12 20:46:15 +0000285void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000286 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000287 setError(CurrentNode, "unknown enumerated scalar");
288 }
289}
290
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291bool Input::beginBitSetScalar(bool &DoClear) {
292 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000293 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000294 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000295 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000296 setError(CurrentNode, "expected sequence of bit values");
297 }
298 DoClear = true;
299 return true;
300}
301
302bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000303 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000304 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000305 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000306 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000307 for (auto &N : SQ->Entries) {
308 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000310 BitValuesUsed[Index] = true;
311 return true;
312 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000313 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000314 setError(CurrentNode, "unexpected scalar in sequence of bit values");
315 }
316 ++Index;
317 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000318 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000319 setError(CurrentNode, "expected sequence of bit values");
320 }
321 return false;
322}
323
324void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000325 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000326 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000327 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000328 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000329 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
330 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000331 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000332 return;
333 }
334 }
335 }
336}
337
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000338void Input::scalarString(StringRef &S, QuotingType) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000339 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000340 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000341 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000342 setError(CurrentNode, "unexpected scalar");
343 }
344}
345
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000346void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); }
Alex Lorenz68e787b2015-05-14 23:08:22 +0000347
Scott Linderc0830f52018-11-14 19:39:59 +0000348void Input::scalarTag(std::string &Tag) {
349 Tag = CurrentNode->_node->getVerbatimTag();
350}
351
Nick Kledzikf60a9272012-12-12 20:46:15 +0000352void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000353 assert(hnode && "HNode must not be NULL");
Scott Linderad115b72018-10-10 18:14:02 +0000354 setError(hnode->_node, message);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000355}
356
Scott Linderc0830f52018-11-14 19:39:59 +0000357NodeKind Input::getNodeKind() {
358 if (isa<ScalarHNode>(CurrentNode))
359 return NodeKind::Scalar;
360 else if (isa<MapHNode>(CurrentNode))
361 return NodeKind::Map;
362 else if (isa<SequenceHNode>(CurrentNode))
363 return NodeKind::Sequence;
364 llvm_unreachable("Unsupported node kind");
365}
366
Nick Kledzikf60a9272012-12-12 20:46:15 +0000367void Input::setError(Node *node, const Twine &message) {
368 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000369 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000370}
371
David Blaikied759fe52014-09-15 18:39:24 +0000372std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000373 SmallString<128> StringStorage;
374 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000375 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000376 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000377 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000378 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000379 }
David Blaikied759fe52014-09-15 18:39:24 +0000380 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000381 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000382 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
383 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000384 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000385 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000386 for (Node &SN : *SQ) {
Scott Linderad115b72018-10-10 18:14:02 +0000387 auto Entry = createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000388 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000389 break;
David Blaikied759fe52014-09-15 18:39:24 +0000390 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000391 }
David Blaikied759fe52014-09-15 18:39:24 +0000392 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000393 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000394 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000395 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000396 Node *KeyNode = KVN.getKey();
George Rimar3674fb62017-09-21 08:25:59 +0000397 ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode);
398 Node *Value = KVN.getValue();
399 if (!Key || !Value) {
400 if (!Key)
401 setError(KeyNode, "Map key must be a scalar");
402 if (!Value)
403 setError(KeyNode, "Map value must not be empty");
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000404 break;
405 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000406 StringStorage.clear();
George Rimar3674fb62017-09-21 08:25:59 +0000407 StringRef KeyStr = Key->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000408 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000409 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000410 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000411 }
Scott Linderad115b72018-10-10 18:14:02 +0000412 auto ValueHNode = createHNodes(Value);
Mehdi Amini43c24282016-11-28 04:57:04 +0000413 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000414 break;
David Blaikied759fe52014-09-15 18:39:24 +0000415 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000416 }
David Blaikied759fe52014-09-15 18:39:24 +0000417 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000418 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000419 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000420 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000422 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000423 }
424}
425
Nick Kledzikf60a9272012-12-12 20:46:15 +0000426void Input::setError(const Twine &Message) {
Scott Linderad115b72018-10-10 18:14:02 +0000427 setError(CurrentNode, Message);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000428}
429
Aaron Ballman0e63e532013-08-15 23:17:53 +0000430bool Input::canElideEmptySequence() {
431 return false;
432}
433
Nick Kledzikf60a9272012-12-12 20:46:15 +0000434//===----------------------------------------------------------------------===//
435// Output
436//===----------------------------------------------------------------------===//
437
Frederic Riss4939e6a2015-05-29 17:56:28 +0000438Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Eugene Zelenko72208a82017-06-21 23:19:47 +0000439 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000440
Eugene Zelenko72208a82017-06-21 23:19:47 +0000441Output::~Output() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000442
Nick Kledzik4761c602013-11-21 00:20:10 +0000443bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000444 return true;
445}
446
447void Output::beginMapping() {
448 StateStack.push_back(inMapFirstKey);
449 NeedsNewLine = true;
450}
451
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000452bool Output::mapTag(StringRef Tag, bool Use) {
453 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000454 // If this tag is being written inside a sequence we should write the start
455 // of the sequence before writing the tag, otherwise the tag won't be
456 // attached to the element in the sequence, but rather the sequence itself.
Scott Linderc0830f52018-11-14 19:39:59 +0000457 bool SequenceElement = false;
458 if (StateStack.size() > 1) {
459 auto &E = StateStack[StateStack.size() - 2];
460 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
461 }
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000462 if (SequenceElement && StateStack.back() == inMapFirstKey) {
Scott Linderad115b72018-10-10 18:14:02 +0000463 newLineCheck();
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000464 } else {
Scott Linderad115b72018-10-10 18:14:02 +0000465 output(" ");
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000466 }
Scott Linderad115b72018-10-10 18:14:02 +0000467 output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000468 if (SequenceElement) {
469 // If we're writing the tag during the first element of a map, the tag
470 // takes the place of the first element in the sequence.
471 if (StateStack.back() == inMapFirstKey) {
472 StateStack.pop_back();
473 StateStack.push_back(inMapOtherKey);
474 }
475 // Tags inside maps in sequences should act as keys in the map from a
476 // formatting perspective, so we always want a newline in a sequence.
477 NeedsNewLine = true;
478 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000479 }
480 return Use;
481}
482
Nick Kledzikf60a9272012-12-12 20:46:15 +0000483void Output::endMapping() {
Scott Linderc0830f52018-11-14 19:39:59 +0000484 // If we did not map anything, we should explicitly emit an empty map
485 if (StateStack.back() == inMapFirstKey)
486 output("{}");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000487 StateStack.pop_back();
488}
489
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000490std::vector<StringRef> Output::keys() {
491 report_fatal_error("invalid call");
492}
493
Nick Kledzikf60a9272012-12-12 20:46:15 +0000494bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000495 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000496 UseDefault = false;
Zachary Turner84efd4d2017-03-15 17:47:39 +0000497 if (Required || !SameAsDefault || WriteDefaultValues) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000498 auto State = StateStack.back();
499 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
500 flowKey(Key);
501 } else {
Scott Linderad115b72018-10-10 18:14:02 +0000502 newLineCheck();
503 paddedKey(Key);
Alex Lorenzb1225082015-05-04 20:11:40 +0000504 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000505 return true;
506 }
507 return false;
508}
509
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000510void Output::postflightKey(void *) {
511 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000512 StateStack.pop_back();
513 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000514 } else if (StateStack.back() == inFlowMapFirstKey) {
515 StateStack.pop_back();
516 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000517 }
518}
519
Alex Lorenzb1225082015-05-04 20:11:40 +0000520void Output::beginFlowMapping() {
521 StateStack.push_back(inFlowMapFirstKey);
Scott Linderad115b72018-10-10 18:14:02 +0000522 newLineCheck();
Alex Lorenzb1225082015-05-04 20:11:40 +0000523 ColumnAtMapFlowStart = Column;
524 output("{ ");
525}
526
527void Output::endFlowMapping() {
528 StateStack.pop_back();
Scott Linderad115b72018-10-10 18:14:02 +0000529 outputUpToEndOfLine(" }");
Alex Lorenzb1225082015-05-04 20:11:40 +0000530}
531
Nick Kledzikf60a9272012-12-12 20:46:15 +0000532void Output::beginDocuments() {
Scott Linderad115b72018-10-10 18:14:02 +0000533 outputUpToEndOfLine("---");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000534}
535
536bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000537 if (index > 0)
Scott Linderad115b72018-10-10 18:14:02 +0000538 outputUpToEndOfLine("\n---");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000539 return true;
540}
541
542void Output::postflightDocument() {
543}
544
545void Output::endDocuments() {
546 output("\n...\n");
547}
548
549unsigned Output::beginSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000550 StateStack.push_back(inSeqFirstElement);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000551 NeedsNewLine = true;
552 return 0;
553}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000554
Nick Kledzikf60a9272012-12-12 20:46:15 +0000555void Output::endSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000556 // If we did not emit anything, we should explicitly emit an empty sequence
557 if (StateStack.back() == inSeqFirstElement)
558 output("[]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000559 StateStack.pop_back();
560}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000561
562bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000563 return true;
564}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000565
566void Output::postflightElement(void *) {
Scott Linderc0830f52018-11-14 19:39:59 +0000567 if (StateStack.back() == inSeqFirstElement) {
568 StateStack.pop_back();
569 StateStack.push_back(inSeqOtherElement);
570 } else if (StateStack.back() == inFlowSeqFirstElement) {
571 StateStack.pop_back();
572 StateStack.push_back(inFlowSeqOtherElement);
573 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000574}
575
576unsigned Output::beginFlowSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000577 StateStack.push_back(inFlowSeqFirstElement);
Scott Linderad115b72018-10-10 18:14:02 +0000578 newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000579 ColumnAtFlowStart = Column;
580 output("[ ");
581 NeedFlowSequenceComma = false;
582 return 0;
583}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000584
Nick Kledzikf60a9272012-12-12 20:46:15 +0000585void Output::endFlowSequence() {
586 StateStack.pop_back();
Scott Linderad115b72018-10-10 18:14:02 +0000587 outputUpToEndOfLine(" ]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000588}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000589
590bool Output::preflightFlowElement(unsigned, void *&) {
591 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000592 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000593 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000594 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000595 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000596 output(" ");
597 Column = ColumnAtFlowStart;
598 output(" ");
599 }
600 return true;
601}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000602
603void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000604 NeedFlowSequenceComma = true;
605}
606
Nick Kledzikf60a9272012-12-12 20:46:15 +0000607void Output::beginEnumScalar() {
608 EnumerationMatchFound = false;
609}
610
611bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000612 if (Match && !EnumerationMatchFound) {
Scott Linderad115b72018-10-10 18:14:02 +0000613 newLineCheck();
614 outputUpToEndOfLine(Str);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000615 EnumerationMatchFound = true;
616 }
617 return false;
618}
619
Michael J. Spencer731cae32015-01-23 21:57:50 +0000620bool Output::matchEnumFallback() {
621 if (EnumerationMatchFound)
622 return false;
623 EnumerationMatchFound = true;
624 return true;
625}
626
Nick Kledzikf60a9272012-12-12 20:46:15 +0000627void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000628 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000629 llvm_unreachable("bad runtime enum value");
630}
631
Nick Kledzikf60a9272012-12-12 20:46:15 +0000632bool Output::beginBitSetScalar(bool &DoClear) {
Scott Linderad115b72018-10-10 18:14:02 +0000633 newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000634 output("[ ");
635 NeedBitValueComma = false;
636 DoClear = false;
637 return true;
638}
639
640bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000641 if (Matches) {
642 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000643 output(", ");
Scott Linderad115b72018-10-10 18:14:02 +0000644 output(Str);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000645 NeedBitValueComma = true;
646 }
647 return false;
648}
649
650void Output::endBitSetScalar() {
Scott Linderad115b72018-10-10 18:14:02 +0000651 outputUpToEndOfLine(" ]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000652}
653
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000654void Output::scalarString(StringRef &S, QuotingType MustQuote) {
Scott Linderad115b72018-10-10 18:14:02 +0000655 newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000656 if (S.empty()) {
657 // Print '' for the empty string because leaving the field empty is not
658 // allowed.
Scott Linderad115b72018-10-10 18:14:02 +0000659 outputUpToEndOfLine("''");
Rui Ueyama106eded2013-09-11 04:00:08 +0000660 return;
661 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000662 if (MustQuote == QuotingType::None) {
David Majnemer77880332014-04-10 07:37:33 +0000663 // Only quote if we must.
Scott Linderad115b72018-10-10 18:14:02 +0000664 outputUpToEndOfLine(S);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000665 return;
666 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000667
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000668 const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000669 output(Quote); // Starting quote.
670
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000671 // When using double-quoted strings (and only in that case), non-printable characters may be
672 // present, and will be escaped using a variety of unicode-scalar and special short-form
673 // escapes. This is handled in yaml::escape.
674 if (MustQuote == QuotingType::Double) {
Pavel Labathd7e12572019-04-11 14:57:34 +0000675 output(yaml::escape(S, /* EscapePrintable= */ false));
Scott Linderad115b72018-10-10 18:14:02 +0000676 outputUpToEndOfLine(Quote);
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000677 return;
678 }
679
Pavel Labathd7e12572019-04-11 14:57:34 +0000680 unsigned i = 0;
681 unsigned j = 0;
682 unsigned End = S.size();
683 const char *Base = S.data();
684
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000685 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000686 while (j < End) {
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000687 if (S[j] == '\'') { // Escape quotes.
688 output(StringRef(&Base[i], j - i)); // "flush".
689 output(StringLiteral("''")); // Print it as ''
Nick Kledzikf60a9272012-12-12 20:46:15 +0000690 i = j + 1;
691 }
692 ++j;
693 }
694 output(StringRef(&Base[i], j - i));
Scott Linderad115b72018-10-10 18:14:02 +0000695 outputUpToEndOfLine(Quote); // Ending quote.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000696}
697
Alex Lorenz68e787b2015-05-14 23:08:22 +0000698void Output::blockScalarString(StringRef &S) {
699 if (!StateStack.empty())
700 newLineCheck();
701 output(" |");
702 outputNewLine();
703
704 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
705
706 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
707 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
708 for (unsigned I = 0; I < Indent; ++I) {
709 output(" ");
710 }
711 output(*Lines);
712 outputNewLine();
713 }
714}
715
Scott Linderc0830f52018-11-14 19:39:59 +0000716void Output::scalarTag(std::string &Tag) {
717 if (Tag.empty())
718 return;
719 newLineCheck();
720 output(Tag);
721 output(" ");
722}
723
Nick Kledzikf60a9272012-12-12 20:46:15 +0000724void Output::setError(const Twine &message) {
725}
726
Aaron Ballman0e63e532013-08-15 23:17:53 +0000727bool Output::canElideEmptySequence() {
728 // Normally, with an optional key/value where the value is an empty sequence,
729 // the whole key/value can be not written. But, that produces wrong yaml
730 // if the key/value is the only thing in the map and the map is used in
731 // a sequence. This detects if the this sequence is the first key/value
732 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000733 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000734 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000735 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000736 return true;
Scott Linderc0830f52018-11-14 19:39:59 +0000737 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
Aaron Ballman0e63e532013-08-15 23:17:53 +0000738}
739
Nick Kledzikf60a9272012-12-12 20:46:15 +0000740void Output::output(StringRef s) {
741 Column += s.size();
742 Out << s;
743}
744
745void Output::outputUpToEndOfLine(StringRef s) {
Scott Linderad115b72018-10-10 18:14:02 +0000746 output(s);
Scott Linderc0830f52018-11-14 19:39:59 +0000747 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
748 !inFlowMapAnyKey(StateStack.back())))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000749 NeedsNewLine = true;
750}
751
752void Output::outputNewLine() {
753 Out << "\n";
754 Column = 0;
755}
756
757// if seq at top, indent as if map, then add "- "
758// if seq in middle, use "- " if firstKey, else use " "
759//
760
761void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000762 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000763 return;
764 NeedsNewLine = false;
765
Scott Linderad115b72018-10-10 18:14:02 +0000766 outputNewLine();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000767
Scott Linderc0830f52018-11-14 19:39:59 +0000768 if (StateStack.size() == 0)
769 return;
770
Nick Kledzikf60a9272012-12-12 20:46:15 +0000771 unsigned Indent = StateStack.size() - 1;
772 bool OutputDash = false;
773
Scott Linderc0830f52018-11-14 19:39:59 +0000774 if (StateStack.back() == inSeqFirstElement ||
775 StateStack.back() == inSeqOtherElement) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000776 OutputDash = true;
Scott Linderc0830f52018-11-14 19:39:59 +0000777 } else if ((StateStack.size() > 1) &&
778 ((StateStack.back() == inMapFirstKey) ||
779 inFlowSeqAnyElement(StateStack.back()) ||
780 (StateStack.back() == inFlowMapFirstKey)) &&
781 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000782 --Indent;
783 OutputDash = true;
784 }
785
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000786 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000787 output(" ");
788 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000789 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000790 output("- ");
791 }
792
793}
794
795void Output::paddedKey(StringRef key) {
796 output(key);
797 output(":");
798 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000799 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000800 output(&spaces[key.size()]);
801 else
802 output(" ");
803}
804
Alex Lorenzb1225082015-05-04 20:11:40 +0000805void Output::flowKey(StringRef Key) {
806 if (StateStack.back() == inFlowMapOtherKey)
807 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000808 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000809 output("\n");
810 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
811 output(" ");
812 Column = ColumnAtMapFlowStart;
813 output(" ");
814 }
815 output(Key);
816 output(": ");
817}
818
Scott Linderc0830f52018-11-14 19:39:59 +0000819NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); }
820
821bool Output::inSeqAnyElement(InState State) {
822 return State == inSeqFirstElement || State == inSeqOtherElement;
823}
824
825bool Output::inFlowSeqAnyElement(InState State) {
826 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
827}
828
829bool Output::inMapAnyKey(InState State) {
830 return State == inMapFirstKey || State == inMapOtherKey;
831}
832
833bool Output::inFlowMapAnyKey(InState State) {
834 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
835}
836
Nick Kledzikf60a9272012-12-12 20:46:15 +0000837//===----------------------------------------------------------------------===//
838// traits for built-in types
839//===----------------------------------------------------------------------===//
840
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000841void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
842 Out << (Val ? "true" : "false");
843}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000844
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000845StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
846 if (Scalar.equals("true")) {
847 Val = true;
848 return StringRef();
849 } else if (Scalar.equals("false")) {
850 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851 return StringRef();
852 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000853 return "invalid boolean";
854}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000855
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000856void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
857 raw_ostream &Out) {
858 Out << Val;
859}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000860
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000861StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
862 StringRef &Val) {
863 Val = Scalar;
864 return StringRef();
865}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000866
John Thompson48e018a2013-11-19 17:28:21 +0000867void ScalarTraits<std::string>::output(const std::string &Val, void *,
868 raw_ostream &Out) {
869 Out << Val;
870}
871
872StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
873 std::string &Val) {
874 Val = Scalar.str();
875 return StringRef();
876}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000877
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000878void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
879 raw_ostream &Out) {
880 // use temp uin32_t because ostream thinks uint8_t is a character
881 uint32_t Num = Val;
882 Out << Num;
883}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000884
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000885StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
886 unsigned long long n;
887 if (getAsUnsignedInteger(Scalar, 0, n))
888 return "invalid number";
889 if (n > 0xFF)
890 return "out of range number";
891 Val = n;
892 return StringRef();
893}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000894
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000895void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
896 raw_ostream &Out) {
897 Out << Val;
898}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000899
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000900StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
901 uint16_t &Val) {
902 unsigned long long n;
903 if (getAsUnsignedInteger(Scalar, 0, n))
904 return "invalid number";
905 if (n > 0xFFFF)
906 return "out of range number";
907 Val = n;
908 return StringRef();
909}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000910
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000911void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
912 raw_ostream &Out) {
913 Out << Val;
914}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000915
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000916StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
917 uint32_t &Val) {
918 unsigned long long n;
919 if (getAsUnsignedInteger(Scalar, 0, n))
920 return "invalid number";
921 if (n > 0xFFFFFFFFUL)
922 return "out of range number";
923 Val = n;
924 return StringRef();
925}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000926
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000927void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
928 raw_ostream &Out) {
929 Out << Val;
930}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000931
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000932StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
933 uint64_t &Val) {
934 unsigned long long N;
935 if (getAsUnsignedInteger(Scalar, 0, N))
936 return "invalid number";
937 Val = N;
938 return StringRef();
939}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000940
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000941void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
942 // use temp in32_t because ostream thinks int8_t is a character
943 int32_t Num = Val;
944 Out << Num;
945}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000946
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000947StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
948 long long N;
949 if (getAsSignedInteger(Scalar, 0, N))
950 return "invalid number";
951 if ((N > 127) || (N < -128))
952 return "out of range number";
953 Val = N;
954 return StringRef();
955}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000956
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000957void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
958 raw_ostream &Out) {
959 Out << Val;
960}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000961
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000962StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
963 long long N;
964 if (getAsSignedInteger(Scalar, 0, N))
965 return "invalid number";
966 if ((N > INT16_MAX) || (N < INT16_MIN))
967 return "out of range number";
968 Val = N;
969 return StringRef();
970}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000971
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000972void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
973 raw_ostream &Out) {
974 Out << Val;
975}
976
977StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
978 long long N;
979 if (getAsSignedInteger(Scalar, 0, N))
980 return "invalid number";
981 if ((N > INT32_MAX) || (N < INT32_MIN))
982 return "out of range number";
983 Val = N;
984 return StringRef();
985}
986
987void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
988 raw_ostream &Out) {
989 Out << Val;
990}
991
992StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
993 long long N;
994 if (getAsSignedInteger(Scalar, 0, N))
995 return "invalid number";
996 Val = N;
997 return StringRef();
998}
999
1000void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +00001001 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001002}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001003
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001004StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +00001005 if (to_float(Scalar, Val))
1006 return StringRef();
1007 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001008}
1009
1010void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +00001011 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001012}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001013
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001014StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +00001015 if (to_float(Scalar, Val))
1016 return StringRef();
1017 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001018}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001019
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001020void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
1021 uint8_t Num = Val;
1022 Out << format("0x%02X", Num);
1023}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001024
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001025StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
1026 unsigned long long n;
1027 if (getAsUnsignedInteger(Scalar, 0, n))
1028 return "invalid hex8 number";
1029 if (n > 0xFF)
1030 return "out of range hex8 number";
1031 Val = n;
1032 return StringRef();
1033}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001034
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001035void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
1036 uint16_t Num = Val;
1037 Out << format("0x%04X", Num);
1038}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001039
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001040StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
1041 unsigned long long n;
1042 if (getAsUnsignedInteger(Scalar, 0, n))
1043 return "invalid hex16 number";
1044 if (n > 0xFFFF)
1045 return "out of range hex16 number";
1046 Val = n;
1047 return StringRef();
1048}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001049
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001050void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1051 uint32_t Num = Val;
1052 Out << format("0x%08X", Num);
1053}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001054
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001055StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1056 unsigned long long n;
1057 if (getAsUnsignedInteger(Scalar, 0, n))
1058 return "invalid hex32 number";
1059 if (n > 0xFFFFFFFFUL)
1060 return "out of range hex32 number";
1061 Val = n;
1062 return StringRef();
1063}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001064
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001065void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1066 uint64_t Num = Val;
1067 Out << format("0x%016llX", Num);
1068}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001069
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001070StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1071 unsigned long long Num;
1072 if (getAsUnsignedInteger(Scalar, 0, Num))
1073 return "invalid hex64 number";
1074 Val = Num;
1075 return StringRef();
1076}