blob: 4b5bf6ad30d36c68d65ebfd5a0317b3978126daa [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) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000116 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000117 if (foundTag.empty()) {
118 // If no tag found and 'Tag' is the default, say it was found.
119 return Default;
120 }
Alex Lorenz7a38d752015-05-12 17:44:32 +0000121 // Return true iff found tag matches supplied tag.
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000122 return Tag.equals(foundTag);
123}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000124
125void Input::beginMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000126 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000127 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000128 // CurrentNode can be null if the document is empty.
129 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000130 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000131 MN->ValidKeys.clear();
132 }
133}
134
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000135std::vector<StringRef> Input::keys() {
136 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
137 std::vector<StringRef> Ret;
138 if (!MN) {
139 setError(CurrentNode, "not a mapping");
140 return Ret;
141 }
142 for (auto &P : MN->Mapping)
143 Ret.push_back(P.first());
144 return Ret;
145}
146
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000147bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
148 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000149 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000150 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000152
153 // CurrentNode is null for empty documents, which is an error in case required
154 // nodes are present.
155 if (!CurrentNode) {
156 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000157 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000158 return false;
159 }
160
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000161 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
162 if (!MN) {
Dave Leec6f2e692017-11-16 17:46:11 +0000163 if (Required || !isa<EmptyHNode>(CurrentNode))
164 setError(CurrentNode, "not a mapping");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000165 return false;
166 }
167 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000168 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000169 if (!Value) {
170 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000171 setError(CurrentNode, Twine("missing required key '") + Key + "'");
172 else
173 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000174 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000175 }
176 SaveInfo = CurrentNode;
177 CurrentNode = Value;
178 return true;
179}
180
181void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000182 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000183}
184
185void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000186 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000187 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000188 // CurrentNode can be null if the document is empty.
189 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000190 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000192 for (const auto &NN : MN->Mapping) {
Peter Collingbourneefdff712017-01-04 20:10:43 +0000193 if (!is_contained(MN->ValidKeys, NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000194 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195 break;
196 }
197 }
198}
199
Alex Lorenzb1225082015-05-04 20:11:40 +0000200void Input::beginFlowMapping() { beginMapping(); }
201
202void Input::endFlowMapping() { endMapping(); }
203
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000205 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000207 if (isa<EmptyHNode>(CurrentNode))
208 return 0;
209 // Treat case where there's a scalar "null" value as an empty sequence.
210 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
211 if (isNull(SN->value()))
212 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000214 // Any other type of HNode is an error.
215 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000216 return 0;
217}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000218
Nick Kledzikf60a9272012-12-12 20:46:15 +0000219void Input::endSequence() {
220}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000221
Nick Kledzikf60a9272012-12-12 20:46:15 +0000222bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000223 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000227 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000228 return true;
229 }
230 return false;
231}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000232
Nick Kledzikf60a9272012-12-12 20:46:15 +0000233void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000234 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000235}
236
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000237unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000238
Nick Kledzikf60a9272012-12-12 20:46:15 +0000239bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000240 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000241 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000242 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000243 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000244 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 return true;
246 }
247 return false;
248}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000249
Nick Kledzikf60a9272012-12-12 20:46:15 +0000250void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000251 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000252}
253
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254void Input::endFlowSequence() {
255}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000256
257void Input::beginEnumScalar() {
258 ScalarMatchFound = false;
259}
260
261bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000262 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000263 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000264 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
265 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000266 ScalarMatchFound = true;
267 return true;
268 }
269 }
270 return false;
271}
272
Michael J. Spencer731cae32015-01-23 21:57:50 +0000273bool Input::matchEnumFallback() {
274 if (ScalarMatchFound)
275 return false;
276 ScalarMatchFound = true;
277 return true;
278}
279
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000281 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000282 setError(CurrentNode, "unknown enumerated scalar");
283 }
284}
285
Nick Kledzikf60a9272012-12-12 20:46:15 +0000286bool Input::beginBitSetScalar(bool &DoClear) {
287 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291 setError(CurrentNode, "expected sequence of bit values");
292 }
293 DoClear = true;
294 return true;
295}
296
297bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000298 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000299 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000300 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000301 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000302 for (auto &N : SQ->Entries) {
303 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000304 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000305 BitValuesUsed[Index] = true;
306 return true;
307 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000308 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000309 setError(CurrentNode, "unexpected scalar in sequence of bit values");
310 }
311 ++Index;
312 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000313 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000314 setError(CurrentNode, "expected sequence of bit values");
315 }
316 return false;
317}
318
319void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000320 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000321 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000322 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000323 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000324 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
325 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000326 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000327 return;
328 }
329 }
330 }
331}
332
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000333void Input::scalarString(StringRef &S, QuotingType) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000334 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000335 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000336 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000337 setError(CurrentNode, "unexpected scalar");
338 }
339}
340
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000341void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); }
Alex Lorenz68e787b2015-05-14 23:08:22 +0000342
Scott Linderc0830f52018-11-14 19:39:59 +0000343void Input::scalarTag(std::string &Tag) {
344 Tag = CurrentNode->_node->getVerbatimTag();
345}
346
Nick Kledzikf60a9272012-12-12 20:46:15 +0000347void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000348 assert(hnode && "HNode must not be NULL");
Scott Linderad115b72018-10-10 18:14:02 +0000349 setError(hnode->_node, message);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000350}
351
Scott Linderc0830f52018-11-14 19:39:59 +0000352NodeKind Input::getNodeKind() {
353 if (isa<ScalarHNode>(CurrentNode))
354 return NodeKind::Scalar;
355 else if (isa<MapHNode>(CurrentNode))
356 return NodeKind::Map;
357 else if (isa<SequenceHNode>(CurrentNode))
358 return NodeKind::Sequence;
359 llvm_unreachable("Unsupported node kind");
360}
361
Nick Kledzikf60a9272012-12-12 20:46:15 +0000362void Input::setError(Node *node, const Twine &message) {
363 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000364 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000365}
366
David Blaikied759fe52014-09-15 18:39:24 +0000367std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000368 SmallString<128> StringStorage;
369 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000370 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000371 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000372 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000373 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000374 }
David Blaikied759fe52014-09-15 18:39:24 +0000375 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000376 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000377 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
378 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000379 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000380 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000381 for (Node &SN : *SQ) {
Scott Linderad115b72018-10-10 18:14:02 +0000382 auto Entry = createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000383 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000384 break;
David Blaikied759fe52014-09-15 18:39:24 +0000385 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000386 }
David Blaikied759fe52014-09-15 18:39:24 +0000387 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000388 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000389 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000390 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000391 Node *KeyNode = KVN.getKey();
George Rimar3674fb62017-09-21 08:25:59 +0000392 ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode);
393 Node *Value = KVN.getValue();
394 if (!Key || !Value) {
395 if (!Key)
396 setError(KeyNode, "Map key must be a scalar");
397 if (!Value)
398 setError(KeyNode, "Map value must not be empty");
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000399 break;
400 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000401 StringStorage.clear();
George Rimar3674fb62017-09-21 08:25:59 +0000402 StringRef KeyStr = Key->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000403 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000404 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000405 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000406 }
Scott Linderad115b72018-10-10 18:14:02 +0000407 auto ValueHNode = createHNodes(Value);
Mehdi Amini43c24282016-11-28 04:57:04 +0000408 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000409 break;
David Blaikied759fe52014-09-15 18:39:24 +0000410 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000411 }
David Blaikied759fe52014-09-15 18:39:24 +0000412 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000413 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000414 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000415 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000416 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000417 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000418 }
419}
420
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421void Input::setError(const Twine &Message) {
Scott Linderad115b72018-10-10 18:14:02 +0000422 setError(CurrentNode, Message);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000423}
424
Aaron Ballman0e63e532013-08-15 23:17:53 +0000425bool Input::canElideEmptySequence() {
426 return false;
427}
428
Nick Kledzikf60a9272012-12-12 20:46:15 +0000429//===----------------------------------------------------------------------===//
430// Output
431//===----------------------------------------------------------------------===//
432
Frederic Riss4939e6a2015-05-29 17:56:28 +0000433Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Eugene Zelenko72208a82017-06-21 23:19:47 +0000434 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000435
Eugene Zelenko72208a82017-06-21 23:19:47 +0000436Output::~Output() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000437
Nick Kledzik4761c602013-11-21 00:20:10 +0000438bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000439 return true;
440}
441
442void Output::beginMapping() {
443 StateStack.push_back(inMapFirstKey);
444 NeedsNewLine = true;
445}
446
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000447bool Output::mapTag(StringRef Tag, bool Use) {
448 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000449 // If this tag is being written inside a sequence we should write the start
450 // of the sequence before writing the tag, otherwise the tag won't be
451 // attached to the element in the sequence, but rather the sequence itself.
Scott Linderc0830f52018-11-14 19:39:59 +0000452 bool SequenceElement = false;
453 if (StateStack.size() > 1) {
454 auto &E = StateStack[StateStack.size() - 2];
455 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
456 }
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000457 if (SequenceElement && StateStack.back() == inMapFirstKey) {
Scott Linderad115b72018-10-10 18:14:02 +0000458 newLineCheck();
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000459 } else {
Scott Linderad115b72018-10-10 18:14:02 +0000460 output(" ");
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000461 }
Scott Linderad115b72018-10-10 18:14:02 +0000462 output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000463 if (SequenceElement) {
464 // If we're writing the tag during the first element of a map, the tag
465 // takes the place of the first element in the sequence.
466 if (StateStack.back() == inMapFirstKey) {
467 StateStack.pop_back();
468 StateStack.push_back(inMapOtherKey);
469 }
470 // Tags inside maps in sequences should act as keys in the map from a
471 // formatting perspective, so we always want a newline in a sequence.
472 NeedsNewLine = true;
473 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000474 }
475 return Use;
476}
477
Nick Kledzikf60a9272012-12-12 20:46:15 +0000478void Output::endMapping() {
Scott Linderc0830f52018-11-14 19:39:59 +0000479 // If we did not map anything, we should explicitly emit an empty map
480 if (StateStack.back() == inMapFirstKey)
481 output("{}");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000482 StateStack.pop_back();
483}
484
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000485std::vector<StringRef> Output::keys() {
486 report_fatal_error("invalid call");
487}
488
Nick Kledzikf60a9272012-12-12 20:46:15 +0000489bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000490 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000491 UseDefault = false;
Zachary Turner84efd4d2017-03-15 17:47:39 +0000492 if (Required || !SameAsDefault || WriteDefaultValues) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000493 auto State = StateStack.back();
494 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
495 flowKey(Key);
496 } else {
Scott Linderad115b72018-10-10 18:14:02 +0000497 newLineCheck();
498 paddedKey(Key);
Alex Lorenzb1225082015-05-04 20:11:40 +0000499 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000500 return true;
501 }
502 return false;
503}
504
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000505void Output::postflightKey(void *) {
506 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000507 StateStack.pop_back();
508 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000509 } else if (StateStack.back() == inFlowMapFirstKey) {
510 StateStack.pop_back();
511 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000512 }
513}
514
Alex Lorenzb1225082015-05-04 20:11:40 +0000515void Output::beginFlowMapping() {
516 StateStack.push_back(inFlowMapFirstKey);
Scott Linderad115b72018-10-10 18:14:02 +0000517 newLineCheck();
Alex Lorenzb1225082015-05-04 20:11:40 +0000518 ColumnAtMapFlowStart = Column;
519 output("{ ");
520}
521
522void Output::endFlowMapping() {
523 StateStack.pop_back();
Scott Linderad115b72018-10-10 18:14:02 +0000524 outputUpToEndOfLine(" }");
Alex Lorenzb1225082015-05-04 20:11:40 +0000525}
526
Nick Kledzikf60a9272012-12-12 20:46:15 +0000527void Output::beginDocuments() {
Scott Linderad115b72018-10-10 18:14:02 +0000528 outputUpToEndOfLine("---");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000529}
530
531bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000532 if (index > 0)
Scott Linderad115b72018-10-10 18:14:02 +0000533 outputUpToEndOfLine("\n---");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000534 return true;
535}
536
537void Output::postflightDocument() {
538}
539
540void Output::endDocuments() {
541 output("\n...\n");
542}
543
544unsigned Output::beginSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000545 StateStack.push_back(inSeqFirstElement);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000546 NeedsNewLine = true;
547 return 0;
548}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000549
Nick Kledzikf60a9272012-12-12 20:46:15 +0000550void Output::endSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000551 // If we did not emit anything, we should explicitly emit an empty sequence
552 if (StateStack.back() == inSeqFirstElement)
553 output("[]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000554 StateStack.pop_back();
555}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000556
557bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000558 return true;
559}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000560
561void Output::postflightElement(void *) {
Scott Linderc0830f52018-11-14 19:39:59 +0000562 if (StateStack.back() == inSeqFirstElement) {
563 StateStack.pop_back();
564 StateStack.push_back(inSeqOtherElement);
565 } else if (StateStack.back() == inFlowSeqFirstElement) {
566 StateStack.pop_back();
567 StateStack.push_back(inFlowSeqOtherElement);
568 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000569}
570
571unsigned Output::beginFlowSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000572 StateStack.push_back(inFlowSeqFirstElement);
Scott Linderad115b72018-10-10 18:14:02 +0000573 newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000574 ColumnAtFlowStart = Column;
575 output("[ ");
576 NeedFlowSequenceComma = false;
577 return 0;
578}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000579
Nick Kledzikf60a9272012-12-12 20:46:15 +0000580void Output::endFlowSequence() {
581 StateStack.pop_back();
Scott Linderad115b72018-10-10 18:14:02 +0000582 outputUpToEndOfLine(" ]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000583}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000584
585bool Output::preflightFlowElement(unsigned, void *&) {
586 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000587 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000588 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000589 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000590 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000591 output(" ");
592 Column = ColumnAtFlowStart;
593 output(" ");
594 }
595 return true;
596}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000597
598void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000599 NeedFlowSequenceComma = true;
600}
601
Nick Kledzikf60a9272012-12-12 20:46:15 +0000602void Output::beginEnumScalar() {
603 EnumerationMatchFound = false;
604}
605
606bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000607 if (Match && !EnumerationMatchFound) {
Scott Linderad115b72018-10-10 18:14:02 +0000608 newLineCheck();
609 outputUpToEndOfLine(Str);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000610 EnumerationMatchFound = true;
611 }
612 return false;
613}
614
Michael J. Spencer731cae32015-01-23 21:57:50 +0000615bool Output::matchEnumFallback() {
616 if (EnumerationMatchFound)
617 return false;
618 EnumerationMatchFound = true;
619 return true;
620}
621
Nick Kledzikf60a9272012-12-12 20:46:15 +0000622void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000623 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000624 llvm_unreachable("bad runtime enum value");
625}
626
Nick Kledzikf60a9272012-12-12 20:46:15 +0000627bool Output::beginBitSetScalar(bool &DoClear) {
Scott Linderad115b72018-10-10 18:14:02 +0000628 newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000629 output("[ ");
630 NeedBitValueComma = false;
631 DoClear = false;
632 return true;
633}
634
635bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000636 if (Matches) {
637 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000638 output(", ");
Scott Linderad115b72018-10-10 18:14:02 +0000639 output(Str);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000640 NeedBitValueComma = true;
641 }
642 return false;
643}
644
645void Output::endBitSetScalar() {
Scott Linderad115b72018-10-10 18:14:02 +0000646 outputUpToEndOfLine(" ]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000647}
648
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000649void Output::scalarString(StringRef &S, QuotingType MustQuote) {
Scott Linderad115b72018-10-10 18:14:02 +0000650 newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000651 if (S.empty()) {
652 // Print '' for the empty string because leaving the field empty is not
653 // allowed.
Scott Linderad115b72018-10-10 18:14:02 +0000654 outputUpToEndOfLine("''");
Rui Ueyama106eded2013-09-11 04:00:08 +0000655 return;
656 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000657 if (MustQuote == QuotingType::None) {
David Majnemer77880332014-04-10 07:37:33 +0000658 // Only quote if we must.
Scott Linderad115b72018-10-10 18:14:02 +0000659 outputUpToEndOfLine(S);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000660 return;
661 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000662
Nick Kledzikf60a9272012-12-12 20:46:15 +0000663 unsigned i = 0;
664 unsigned j = 0;
665 unsigned End = S.size();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000666 const char *Base = S.data();
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000667
668 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) {
675 output(yaml::escape(Base, /* EscapePrintable= */ false));
Scott Linderad115b72018-10-10 18:14:02 +0000676 outputUpToEndOfLine(Quote);
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000677 return;
678 }
679
680 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000681 while (j < End) {
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000682 if (S[j] == '\'') { // Escape quotes.
683 output(StringRef(&Base[i], j - i)); // "flush".
684 output(StringLiteral("''")); // Print it as ''
Nick Kledzikf60a9272012-12-12 20:46:15 +0000685 i = j + 1;
686 }
687 ++j;
688 }
689 output(StringRef(&Base[i], j - i));
Scott Linderad115b72018-10-10 18:14:02 +0000690 outputUpToEndOfLine(Quote); // Ending quote.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000691}
692
Alex Lorenz68e787b2015-05-14 23:08:22 +0000693void Output::blockScalarString(StringRef &S) {
694 if (!StateStack.empty())
695 newLineCheck();
696 output(" |");
697 outputNewLine();
698
699 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
700
701 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
702 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
703 for (unsigned I = 0; I < Indent; ++I) {
704 output(" ");
705 }
706 output(*Lines);
707 outputNewLine();
708 }
709}
710
Scott Linderc0830f52018-11-14 19:39:59 +0000711void Output::scalarTag(std::string &Tag) {
712 if (Tag.empty())
713 return;
714 newLineCheck();
715 output(Tag);
716 output(" ");
717}
718
Nick Kledzikf60a9272012-12-12 20:46:15 +0000719void Output::setError(const Twine &message) {
720}
721
Aaron Ballman0e63e532013-08-15 23:17:53 +0000722bool Output::canElideEmptySequence() {
723 // Normally, with an optional key/value where the value is an empty sequence,
724 // the whole key/value can be not written. But, that produces wrong yaml
725 // if the key/value is the only thing in the map and the map is used in
726 // a sequence. This detects if the this sequence is the first key/value
727 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000728 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000729 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000730 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000731 return true;
Scott Linderc0830f52018-11-14 19:39:59 +0000732 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
Aaron Ballman0e63e532013-08-15 23:17:53 +0000733}
734
Nick Kledzikf60a9272012-12-12 20:46:15 +0000735void Output::output(StringRef s) {
736 Column += s.size();
737 Out << s;
738}
739
740void Output::outputUpToEndOfLine(StringRef s) {
Scott Linderad115b72018-10-10 18:14:02 +0000741 output(s);
Scott Linderc0830f52018-11-14 19:39:59 +0000742 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
743 !inFlowMapAnyKey(StateStack.back())))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000744 NeedsNewLine = true;
745}
746
747void Output::outputNewLine() {
748 Out << "\n";
749 Column = 0;
750}
751
752// if seq at top, indent as if map, then add "- "
753// if seq in middle, use "- " if firstKey, else use " "
754//
755
756void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000757 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000758 return;
759 NeedsNewLine = false;
760
Scott Linderad115b72018-10-10 18:14:02 +0000761 outputNewLine();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000762
Scott Linderc0830f52018-11-14 19:39:59 +0000763 if (StateStack.size() == 0)
764 return;
765
Nick Kledzikf60a9272012-12-12 20:46:15 +0000766 unsigned Indent = StateStack.size() - 1;
767 bool OutputDash = false;
768
Scott Linderc0830f52018-11-14 19:39:59 +0000769 if (StateStack.back() == inSeqFirstElement ||
770 StateStack.back() == inSeqOtherElement) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000771 OutputDash = true;
Scott Linderc0830f52018-11-14 19:39:59 +0000772 } else if ((StateStack.size() > 1) &&
773 ((StateStack.back() == inMapFirstKey) ||
774 inFlowSeqAnyElement(StateStack.back()) ||
775 (StateStack.back() == inFlowMapFirstKey)) &&
776 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000777 --Indent;
778 OutputDash = true;
779 }
780
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000781 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000782 output(" ");
783 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000784 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000785 output("- ");
786 }
787
788}
789
790void Output::paddedKey(StringRef key) {
791 output(key);
792 output(":");
793 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000794 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000795 output(&spaces[key.size()]);
796 else
797 output(" ");
798}
799
Alex Lorenzb1225082015-05-04 20:11:40 +0000800void Output::flowKey(StringRef Key) {
801 if (StateStack.back() == inFlowMapOtherKey)
802 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000803 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000804 output("\n");
805 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
806 output(" ");
807 Column = ColumnAtMapFlowStart;
808 output(" ");
809 }
810 output(Key);
811 output(": ");
812}
813
Scott Linderc0830f52018-11-14 19:39:59 +0000814NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); }
815
816bool Output::inSeqAnyElement(InState State) {
817 return State == inSeqFirstElement || State == inSeqOtherElement;
818}
819
820bool Output::inFlowSeqAnyElement(InState State) {
821 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
822}
823
824bool Output::inMapAnyKey(InState State) {
825 return State == inMapFirstKey || State == inMapOtherKey;
826}
827
828bool Output::inFlowMapAnyKey(InState State) {
829 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
830}
831
Nick Kledzikf60a9272012-12-12 20:46:15 +0000832//===----------------------------------------------------------------------===//
833// traits for built-in types
834//===----------------------------------------------------------------------===//
835
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000836void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
837 Out << (Val ? "true" : "false");
838}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000839
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000840StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
841 if (Scalar.equals("true")) {
842 Val = true;
843 return StringRef();
844 } else if (Scalar.equals("false")) {
845 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000846 return StringRef();
847 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000848 return "invalid boolean";
849}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000850
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000851void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
852 raw_ostream &Out) {
853 Out << Val;
854}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000855
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000856StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
857 StringRef &Val) {
858 Val = Scalar;
859 return StringRef();
860}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000861
John Thompson48e018a2013-11-19 17:28:21 +0000862void ScalarTraits<std::string>::output(const std::string &Val, void *,
863 raw_ostream &Out) {
864 Out << Val;
865}
866
867StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
868 std::string &Val) {
869 Val = Scalar.str();
870 return StringRef();
871}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000872
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000873void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
874 raw_ostream &Out) {
875 // use temp uin32_t because ostream thinks uint8_t is a character
876 uint32_t Num = Val;
877 Out << Num;
878}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000879
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000880StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
881 unsigned long long n;
882 if (getAsUnsignedInteger(Scalar, 0, n))
883 return "invalid number";
884 if (n > 0xFF)
885 return "out of range number";
886 Val = n;
887 return StringRef();
888}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000889
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000890void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
891 raw_ostream &Out) {
892 Out << Val;
893}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000894
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000895StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
896 uint16_t &Val) {
897 unsigned long long n;
898 if (getAsUnsignedInteger(Scalar, 0, n))
899 return "invalid number";
900 if (n > 0xFFFF)
901 return "out of range number";
902 Val = n;
903 return StringRef();
904}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000905
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000906void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
907 raw_ostream &Out) {
908 Out << Val;
909}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000910
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000911StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
912 uint32_t &Val) {
913 unsigned long long n;
914 if (getAsUnsignedInteger(Scalar, 0, n))
915 return "invalid number";
916 if (n > 0xFFFFFFFFUL)
917 return "out of range number";
918 Val = n;
919 return StringRef();
920}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000921
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000922void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
923 raw_ostream &Out) {
924 Out << Val;
925}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000926
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000927StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
928 uint64_t &Val) {
929 unsigned long long N;
930 if (getAsUnsignedInteger(Scalar, 0, N))
931 return "invalid number";
932 Val = N;
933 return StringRef();
934}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000935
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000936void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
937 // use temp in32_t because ostream thinks int8_t is a character
938 int32_t Num = Val;
939 Out << Num;
940}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000941
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000942StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
943 long long N;
944 if (getAsSignedInteger(Scalar, 0, N))
945 return "invalid number";
946 if ((N > 127) || (N < -128))
947 return "out of range number";
948 Val = N;
949 return StringRef();
950}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000951
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000952void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
953 raw_ostream &Out) {
954 Out << Val;
955}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000956
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000957StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
958 long long N;
959 if (getAsSignedInteger(Scalar, 0, N))
960 return "invalid number";
961 if ((N > INT16_MAX) || (N < INT16_MIN))
962 return "out of range number";
963 Val = N;
964 return StringRef();
965}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000966
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000967void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
968 raw_ostream &Out) {
969 Out << Val;
970}
971
972StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
973 long long N;
974 if (getAsSignedInteger(Scalar, 0, N))
975 return "invalid number";
976 if ((N > INT32_MAX) || (N < INT32_MIN))
977 return "out of range number";
978 Val = N;
979 return StringRef();
980}
981
982void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
983 raw_ostream &Out) {
984 Out << Val;
985}
986
987StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
988 long long N;
989 if (getAsSignedInteger(Scalar, 0, N))
990 return "invalid number";
991 Val = N;
992 return StringRef();
993}
994
995void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000996 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000997}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000998
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000999StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +00001000 if (to_float(Scalar, Val))
1001 return StringRef();
1002 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001003}
1004
1005void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +00001006 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001007}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001008
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001009StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +00001010 if (to_float(Scalar, Val))
1011 return StringRef();
1012 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001013}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001014
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001015void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
1016 uint8_t Num = Val;
1017 Out << format("0x%02X", Num);
1018}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001019
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001020StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
1021 unsigned long long n;
1022 if (getAsUnsignedInteger(Scalar, 0, n))
1023 return "invalid hex8 number";
1024 if (n > 0xFF)
1025 return "out of range hex8 number";
1026 Val = n;
1027 return StringRef();
1028}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001029
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001030void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
1031 uint16_t Num = Val;
1032 Out << format("0x%04X", Num);
1033}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001034
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001035StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
1036 unsigned long long n;
1037 if (getAsUnsignedInteger(Scalar, 0, n))
1038 return "invalid hex16 number";
1039 if (n > 0xFFFF)
1040 return "out of range hex16 number";
1041 Val = n;
1042 return StringRef();
1043}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001044
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001045void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1046 uint32_t Num = Val;
1047 Out << format("0x%08X", Num);
1048}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001049
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001050StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1051 unsigned long long n;
1052 if (getAsUnsignedInteger(Scalar, 0, n))
1053 return "invalid hex32 number";
1054 if (n > 0xFFFFFFFFUL)
1055 return "out of range hex32 number";
1056 Val = n;
1057 return StringRef();
1058}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001059
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001060void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1061 uint64_t Num = Val;
1062 Out << format("0x%016llX", Num);
1063}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001064
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001065StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1066 unsigned long long Num;
1067 if (getAsUnsignedInteger(Scalar, 0, Num))
1068 return "invalid hex64 number";
1069 Val = Num;
1070 return StringRef();
1071}