blob: b9bbee7883c61b5c9f29c72f0e55aefd6e1c71f6 [file] [log] [blame]
Nick Kledzikf60a9272012-12-12 20:46:15 +00001//===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Benjamin Kramer16132e62015-03-23 18:07:13 +000010#include "llvm/Support/YAMLTraits.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000011#include "llvm/ADT/STLExtras.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000012#include "llvm/ADT/SmallString.h"
Pavel Labathec000f42017-06-23 12:55:02 +000013#include "llvm/ADT/StringExtras.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000014#include "llvm/ADT/StringRef.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000015#include "llvm/ADT/Twine.h"
16#include "llvm/Support/Casting.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000017#include "llvm/Support/Errc.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000018#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000019#include "llvm/Support/Format.h"
Alex Lorenz68e787b2015-05-14 23:08:22 +000020#include "llvm/Support/LineIterator.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000021#include "llvm/Support/MemoryBuffer.h"
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +000022#include "llvm/Support/Unicode.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000023#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000024#include "llvm/Support/raw_ostream.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000025#include <algorithm>
26#include <cassert>
27#include <cstdint>
28#include <cstdlib>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000029#include <cstring>
Eugene Zelenko72208a82017-06-21 23:19:47 +000030#include <string>
31#include <vector>
32
Benjamin Kramer36b0f122012-12-12 22:40:02 +000033using namespace llvm;
34using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000035
36//===----------------------------------------------------------------------===//
37// IO
38//===----------------------------------------------------------------------===//
39
Eugene Zelenko72208a82017-06-21 23:19:47 +000040IO::IO(void *Context) : Ctxt(Context) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +000041
Eugene Zelenko72208a82017-06-21 23:19:47 +000042IO::~IO() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +000043
44void *IO::getContext() {
45 return Ctxt;
46}
47
48void IO::setContext(void *Context) {
49 Ctxt = Context;
50}
51
Nick Kledzikf60a9272012-12-12 20:46:15 +000052//===----------------------------------------------------------------------===//
53// Input
54//===----------------------------------------------------------------------===//
55
Mehdi Amini3ab3fef2016-11-28 21:38:52 +000056Input::Input(StringRef InputContent, void *Ctxt,
57 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
Eugene Zelenko72208a82017-06-21 23:19:47 +000058 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000059 if (DiagHandler)
60 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000061 DocIterator = Strm->begin();
62}
63
Alex Bradbury16843172017-07-17 11:41:30 +000064Input::Input(MemoryBufferRef Input, void *Ctxt,
65 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
66 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
67 if (DiagHandler)
68 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
69 DocIterator = Strm->begin();
70}
71
Eugene Zelenko72208a82017-06-21 23:19:47 +000072Input::~Input() = default;
Nick Kledzik0dcef842013-01-08 21:04:44 +000073
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000074std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000075
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000076// Pin the vtables to this file.
77void Input::HNode::anchor() {}
78void Input::EmptyHNode::anchor() {}
79void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000080void Input::MapHNode::anchor() {}
81void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000082
Nick Kledzik4761c602013-11-21 00:20:10 +000083bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000084 return false;
85}
86
87bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000088 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000089 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000090 if (!N) {
Alex Lorenz7a38d752015-05-12 17:44:32 +000091 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000092 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000093 return false;
94 }
95
Benjamin Kramer36b0f122012-12-12 22:40:02 +000096 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000097 // Empty files are allowed and ignored
98 ++DocIterator;
99 return setCurrentDocument();
100 }
Scott Linderad115b72018-10-10 18:14:02 +0000101 TopNode = createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +0000102 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000103 return true;
104 }
105 return false;
106}
107
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000108bool Input::nextDocument() {
109 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000110}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +0000111
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000112const Node *Input::getCurrentNode() const {
113 return CurrentNode ? CurrentNode->_node : nullptr;
114}
115
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000116bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000117 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000118 if (foundTag.empty()) {
119 // If no tag found and 'Tag' is the default, say it was found.
120 return Default;
121 }
Alex Lorenz7a38d752015-05-12 17:44:32 +0000122 // Return true iff found tag matches supplied tag.
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000123 return Tag.equals(foundTag);
124}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000125
126void Input::beginMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000127 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000128 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000129 // CurrentNode can be null if the document is empty.
130 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000131 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000132 MN->ValidKeys.clear();
133 }
134}
135
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000136std::vector<StringRef> Input::keys() {
137 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
138 std::vector<StringRef> Ret;
139 if (!MN) {
140 setError(CurrentNode, "not a mapping");
141 return Ret;
142 }
143 for (auto &P : MN->Mapping)
144 Ret.push_back(P.first());
145 return Ret;
146}
147
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000148bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
149 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000150 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000151 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000152 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000153
154 // CurrentNode is null for empty documents, which is an error in case required
155 // nodes are present.
156 if (!CurrentNode) {
157 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000158 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000159 return false;
160 }
161
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000162 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
163 if (!MN) {
Dave Leec6f2e692017-11-16 17:46:11 +0000164 if (Required || !isa<EmptyHNode>(CurrentNode))
165 setError(CurrentNode, "not a mapping");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000166 return false;
167 }
168 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000169 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000170 if (!Value) {
171 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172 setError(CurrentNode, Twine("missing required key '") + Key + "'");
173 else
174 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000175 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000176 }
177 SaveInfo = CurrentNode;
178 CurrentNode = Value;
179 return true;
180}
181
182void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000183 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000184}
185
186void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000187 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000188 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000189 // CurrentNode can be null if the document is empty.
190 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000193 for (const auto &NN : MN->Mapping) {
Peter Collingbourneefdff712017-01-04 20:10:43 +0000194 if (!is_contained(MN->ValidKeys, NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000195 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000196 break;
197 }
198 }
199}
200
Alex Lorenzb1225082015-05-04 20:11:40 +0000201void Input::beginFlowMapping() { beginMapping(); }
202
203void Input::endFlowMapping() { endMapping(); }
204
Nick Kledzikf60a9272012-12-12 20:46:15 +0000205unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000206 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000207 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000208 if (isa<EmptyHNode>(CurrentNode))
209 return 0;
210 // Treat case where there's a scalar "null" value as an empty sequence.
211 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
212 if (isNull(SN->value()))
213 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000214 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000215 // Any other type of HNode is an error.
216 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000217 return 0;
218}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000219
Nick Kledzikf60a9272012-12-12 20:46:15 +0000220void Input::endSequence() {
221}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000222
Nick Kledzikf60a9272012-12-12 20:46:15 +0000223bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000224 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000225 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000226 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000227 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000228 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000229 return true;
230 }
231 return false;
232}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000233
Nick Kledzikf60a9272012-12-12 20:46:15 +0000234void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000235 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000236}
237
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000238unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000239
Nick Kledzikf60a9272012-12-12 20:46:15 +0000240bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000241 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000242 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000243 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000244 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000245 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000246 return true;
247 }
248 return false;
249}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000250
Nick Kledzikf60a9272012-12-12 20:46:15 +0000251void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000252 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000253}
254
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000255void Input::endFlowSequence() {
256}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257
258void Input::beginEnumScalar() {
259 ScalarMatchFound = false;
260}
261
262bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000263 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000264 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000265 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
266 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000267 ScalarMatchFound = true;
268 return true;
269 }
270 }
271 return false;
272}
273
Michael J. Spencer731cae32015-01-23 21:57:50 +0000274bool Input::matchEnumFallback() {
275 if (ScalarMatchFound)
276 return false;
277 ScalarMatchFound = true;
278 return true;
279}
280
Nick Kledzikf60a9272012-12-12 20:46:15 +0000281void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000282 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000283 setError(CurrentNode, "unknown enumerated scalar");
284 }
285}
286
Nick Kledzikf60a9272012-12-12 20:46:15 +0000287bool Input::beginBitSetScalar(bool &DoClear) {
288 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000289 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000291 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000292 setError(CurrentNode, "expected sequence of bit values");
293 }
294 DoClear = true;
295 return true;
296}
297
298bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000299 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000300 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000301 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000302 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000303 for (auto &N : SQ->Entries) {
304 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000305 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000306 BitValuesUsed[Index] = true;
307 return true;
308 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000310 setError(CurrentNode, "unexpected scalar in sequence of bit values");
311 }
312 ++Index;
313 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000314 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000315 setError(CurrentNode, "expected sequence of bit values");
316 }
317 return false;
318}
319
320void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000321 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000323 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000324 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000325 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
326 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000327 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000328 return;
329 }
330 }
331 }
332}
333
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000334void Input::scalarString(StringRef &S, QuotingType) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000335 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000337 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000338 setError(CurrentNode, "unexpected scalar");
339 }
340}
341
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000342void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); }
Alex Lorenz68e787b2015-05-14 23:08:22 +0000343
Scott Linderc0830f52018-11-14 19:39:59 +0000344void Input::scalarTag(std::string &Tag) {
345 Tag = CurrentNode->_node->getVerbatimTag();
346}
347
Nick Kledzikf60a9272012-12-12 20:46:15 +0000348void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000349 assert(hnode && "HNode must not be NULL");
Scott Linderad115b72018-10-10 18:14:02 +0000350 setError(hnode->_node, message);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000351}
352
Scott Linderc0830f52018-11-14 19:39:59 +0000353NodeKind Input::getNodeKind() {
354 if (isa<ScalarHNode>(CurrentNode))
355 return NodeKind::Scalar;
356 else if (isa<MapHNode>(CurrentNode))
357 return NodeKind::Map;
358 else if (isa<SequenceHNode>(CurrentNode))
359 return NodeKind::Sequence;
360 llvm_unreachable("Unsupported node kind");
361}
362
Nick Kledzikf60a9272012-12-12 20:46:15 +0000363void Input::setError(Node *node, const Twine &message) {
364 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000365 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000366}
367
David Blaikied759fe52014-09-15 18:39:24 +0000368std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000369 SmallString<128> StringStorage;
370 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000371 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000372 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000373 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000374 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000375 }
David Blaikied759fe52014-09-15 18:39:24 +0000376 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000377 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000378 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
379 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000380 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000381 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000382 for (Node &SN : *SQ) {
Scott Linderad115b72018-10-10 18:14:02 +0000383 auto Entry = createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000384 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000385 break;
David Blaikied759fe52014-09-15 18:39:24 +0000386 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000387 }
David Blaikied759fe52014-09-15 18:39:24 +0000388 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000389 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000390 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000391 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000392 Node *KeyNode = KVN.getKey();
George Rimar3674fb62017-09-21 08:25:59 +0000393 ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode);
394 Node *Value = KVN.getValue();
395 if (!Key || !Value) {
396 if (!Key)
397 setError(KeyNode, "Map key must be a scalar");
398 if (!Value)
399 setError(KeyNode, "Map value must not be empty");
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000400 break;
401 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000402 StringStorage.clear();
George Rimar3674fb62017-09-21 08:25:59 +0000403 StringRef KeyStr = Key->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000404 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000406 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000407 }
Scott Linderad115b72018-10-10 18:14:02 +0000408 auto ValueHNode = createHNodes(Value);
Mehdi Amini43c24282016-11-28 04:57:04 +0000409 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000410 break;
David Blaikied759fe52014-09-15 18:39:24 +0000411 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000412 }
David Blaikied759fe52014-09-15 18:39:24 +0000413 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000414 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000415 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000416 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000417 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000418 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000419 }
420}
421
Nick Kledzikf60a9272012-12-12 20:46:15 +0000422void Input::setError(const Twine &Message) {
Scott Linderad115b72018-10-10 18:14:02 +0000423 setError(CurrentNode, Message);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000424}
425
Aaron Ballman0e63e532013-08-15 23:17:53 +0000426bool Input::canElideEmptySequence() {
427 return false;
428}
429
Nick Kledzikf60a9272012-12-12 20:46:15 +0000430//===----------------------------------------------------------------------===//
431// Output
432//===----------------------------------------------------------------------===//
433
Frederic Riss4939e6a2015-05-29 17:56:28 +0000434Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Eugene Zelenko72208a82017-06-21 23:19:47 +0000435 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000436
Eugene Zelenko72208a82017-06-21 23:19:47 +0000437Output::~Output() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000438
Nick Kledzik4761c602013-11-21 00:20:10 +0000439bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000440 return true;
441}
442
443void Output::beginMapping() {
444 StateStack.push_back(inMapFirstKey);
445 NeedsNewLine = true;
446}
447
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000448bool Output::mapTag(StringRef Tag, bool Use) {
449 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000450 // If this tag is being written inside a sequence we should write the start
451 // of the sequence before writing the tag, otherwise the tag won't be
452 // attached to the element in the sequence, but rather the sequence itself.
Scott Linderc0830f52018-11-14 19:39:59 +0000453 bool SequenceElement = false;
454 if (StateStack.size() > 1) {
455 auto &E = StateStack[StateStack.size() - 2];
456 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
457 }
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000458 if (SequenceElement && StateStack.back() == inMapFirstKey) {
Scott Linderad115b72018-10-10 18:14:02 +0000459 newLineCheck();
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000460 } else {
Scott Linderad115b72018-10-10 18:14:02 +0000461 output(" ");
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000462 }
Scott Linderad115b72018-10-10 18:14:02 +0000463 output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000464 if (SequenceElement) {
465 // If we're writing the tag during the first element of a map, the tag
466 // takes the place of the first element in the sequence.
467 if (StateStack.back() == inMapFirstKey) {
468 StateStack.pop_back();
469 StateStack.push_back(inMapOtherKey);
470 }
471 // Tags inside maps in sequences should act as keys in the map from a
472 // formatting perspective, so we always want a newline in a sequence.
473 NeedsNewLine = true;
474 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000475 }
476 return Use;
477}
478
Nick Kledzikf60a9272012-12-12 20:46:15 +0000479void Output::endMapping() {
Scott Linderc0830f52018-11-14 19:39:59 +0000480 // If we did not map anything, we should explicitly emit an empty map
481 if (StateStack.back() == inMapFirstKey)
482 output("{}");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000483 StateStack.pop_back();
484}
485
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000486std::vector<StringRef> Output::keys() {
487 report_fatal_error("invalid call");
488}
489
Nick Kledzikf60a9272012-12-12 20:46:15 +0000490bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000491 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000492 UseDefault = false;
Zachary Turner84efd4d2017-03-15 17:47:39 +0000493 if (Required || !SameAsDefault || WriteDefaultValues) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000494 auto State = StateStack.back();
495 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
496 flowKey(Key);
497 } else {
Scott Linderad115b72018-10-10 18:14:02 +0000498 newLineCheck();
499 paddedKey(Key);
Alex Lorenzb1225082015-05-04 20:11:40 +0000500 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000501 return true;
502 }
503 return false;
504}
505
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000506void Output::postflightKey(void *) {
507 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000508 StateStack.pop_back();
509 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000510 } else if (StateStack.back() == inFlowMapFirstKey) {
511 StateStack.pop_back();
512 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000513 }
514}
515
Alex Lorenzb1225082015-05-04 20:11:40 +0000516void Output::beginFlowMapping() {
517 StateStack.push_back(inFlowMapFirstKey);
Scott Linderad115b72018-10-10 18:14:02 +0000518 newLineCheck();
Alex Lorenzb1225082015-05-04 20:11:40 +0000519 ColumnAtMapFlowStart = Column;
520 output("{ ");
521}
522
523void Output::endFlowMapping() {
524 StateStack.pop_back();
Scott Linderad115b72018-10-10 18:14:02 +0000525 outputUpToEndOfLine(" }");
Alex Lorenzb1225082015-05-04 20:11:40 +0000526}
527
Nick Kledzikf60a9272012-12-12 20:46:15 +0000528void Output::beginDocuments() {
Scott Linderad115b72018-10-10 18:14:02 +0000529 outputUpToEndOfLine("---");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000530}
531
532bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000533 if (index > 0)
Scott Linderad115b72018-10-10 18:14:02 +0000534 outputUpToEndOfLine("\n---");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000535 return true;
536}
537
538void Output::postflightDocument() {
539}
540
541void Output::endDocuments() {
542 output("\n...\n");
543}
544
545unsigned Output::beginSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000546 StateStack.push_back(inSeqFirstElement);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000547 NeedsNewLine = true;
548 return 0;
549}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000550
Nick Kledzikf60a9272012-12-12 20:46:15 +0000551void Output::endSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000552 // If we did not emit anything, we should explicitly emit an empty sequence
553 if (StateStack.back() == inSeqFirstElement)
554 output("[]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000555 StateStack.pop_back();
556}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000557
558bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000559 return true;
560}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000561
562void Output::postflightElement(void *) {
Scott Linderc0830f52018-11-14 19:39:59 +0000563 if (StateStack.back() == inSeqFirstElement) {
564 StateStack.pop_back();
565 StateStack.push_back(inSeqOtherElement);
566 } else if (StateStack.back() == inFlowSeqFirstElement) {
567 StateStack.pop_back();
568 StateStack.push_back(inFlowSeqOtherElement);
569 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000570}
571
572unsigned Output::beginFlowSequence() {
Scott Linderc0830f52018-11-14 19:39:59 +0000573 StateStack.push_back(inFlowSeqFirstElement);
Scott Linderad115b72018-10-10 18:14:02 +0000574 newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000575 ColumnAtFlowStart = Column;
576 output("[ ");
577 NeedFlowSequenceComma = false;
578 return 0;
579}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000580
Nick Kledzikf60a9272012-12-12 20:46:15 +0000581void Output::endFlowSequence() {
582 StateStack.pop_back();
Scott Linderad115b72018-10-10 18:14:02 +0000583 outputUpToEndOfLine(" ]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000584}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000585
586bool Output::preflightFlowElement(unsigned, void *&) {
587 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000588 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000589 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000590 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000591 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000592 output(" ");
593 Column = ColumnAtFlowStart;
594 output(" ");
595 }
596 return true;
597}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000598
599void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000600 NeedFlowSequenceComma = true;
601}
602
Nick Kledzikf60a9272012-12-12 20:46:15 +0000603void Output::beginEnumScalar() {
604 EnumerationMatchFound = false;
605}
606
607bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000608 if (Match && !EnumerationMatchFound) {
Scott Linderad115b72018-10-10 18:14:02 +0000609 newLineCheck();
610 outputUpToEndOfLine(Str);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000611 EnumerationMatchFound = true;
612 }
613 return false;
614}
615
Michael J. Spencer731cae32015-01-23 21:57:50 +0000616bool Output::matchEnumFallback() {
617 if (EnumerationMatchFound)
618 return false;
619 EnumerationMatchFound = true;
620 return true;
621}
622
Nick Kledzikf60a9272012-12-12 20:46:15 +0000623void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000624 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000625 llvm_unreachable("bad runtime enum value");
626}
627
Nick Kledzikf60a9272012-12-12 20:46:15 +0000628bool Output::beginBitSetScalar(bool &DoClear) {
Scott Linderad115b72018-10-10 18:14:02 +0000629 newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000630 output("[ ");
631 NeedBitValueComma = false;
632 DoClear = false;
633 return true;
634}
635
636bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000637 if (Matches) {
638 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000639 output(", ");
Scott Linderad115b72018-10-10 18:14:02 +0000640 output(Str);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000641 NeedBitValueComma = true;
642 }
643 return false;
644}
645
646void Output::endBitSetScalar() {
Scott Linderad115b72018-10-10 18:14:02 +0000647 outputUpToEndOfLine(" ]");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000648}
649
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000650void Output::scalarString(StringRef &S, QuotingType MustQuote) {
Scott Linderad115b72018-10-10 18:14:02 +0000651 newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000652 if (S.empty()) {
653 // Print '' for the empty string because leaving the field empty is not
654 // allowed.
Scott Linderad115b72018-10-10 18:14:02 +0000655 outputUpToEndOfLine("''");
Rui Ueyama106eded2013-09-11 04:00:08 +0000656 return;
657 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000658 if (MustQuote == QuotingType::None) {
David Majnemer77880332014-04-10 07:37:33 +0000659 // Only quote if we must.
Scott Linderad115b72018-10-10 18:14:02 +0000660 outputUpToEndOfLine(S);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000661 return;
662 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000663
Nick Kledzikf60a9272012-12-12 20:46:15 +0000664 unsigned i = 0;
665 unsigned j = 0;
666 unsigned End = S.size();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000667 const char *Base = S.data();
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000668
669 const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000670 output(Quote); // Starting quote.
671
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000672 // When using double-quoted strings (and only in that case), non-printable characters may be
673 // present, and will be escaped using a variety of unicode-scalar and special short-form
674 // escapes. This is handled in yaml::escape.
675 if (MustQuote == QuotingType::Double) {
676 output(yaml::escape(Base, /* EscapePrintable= */ false));
Scott Linderad115b72018-10-10 18:14:02 +0000677 outputUpToEndOfLine(Quote);
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000678 return;
679 }
680
681 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000682 while (j < End) {
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000683 if (S[j] == '\'') { // Escape quotes.
684 output(StringRef(&Base[i], j - i)); // "flush".
685 output(StringLiteral("''")); // Print it as ''
Nick Kledzikf60a9272012-12-12 20:46:15 +0000686 i = j + 1;
687 }
688 ++j;
689 }
690 output(StringRef(&Base[i], j - i));
Scott Linderad115b72018-10-10 18:14:02 +0000691 outputUpToEndOfLine(Quote); // Ending quote.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000692}
693
Alex Lorenz68e787b2015-05-14 23:08:22 +0000694void Output::blockScalarString(StringRef &S) {
695 if (!StateStack.empty())
696 newLineCheck();
697 output(" |");
698 outputNewLine();
699
700 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
701
702 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
703 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
704 for (unsigned I = 0; I < Indent; ++I) {
705 output(" ");
706 }
707 output(*Lines);
708 outputNewLine();
709 }
710}
711
Scott Linderc0830f52018-11-14 19:39:59 +0000712void Output::scalarTag(std::string &Tag) {
713 if (Tag.empty())
714 return;
715 newLineCheck();
716 output(Tag);
717 output(" ");
718}
719
Nick Kledzikf60a9272012-12-12 20:46:15 +0000720void Output::setError(const Twine &message) {
721}
722
Aaron Ballman0e63e532013-08-15 23:17:53 +0000723bool Output::canElideEmptySequence() {
724 // Normally, with an optional key/value where the value is an empty sequence,
725 // the whole key/value can be not written. But, that produces wrong yaml
726 // if the key/value is the only thing in the map and the map is used in
727 // a sequence. This detects if the this sequence is the first key/value
728 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000729 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000730 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000731 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000732 return true;
Scott Linderc0830f52018-11-14 19:39:59 +0000733 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
Aaron Ballman0e63e532013-08-15 23:17:53 +0000734}
735
Nick Kledzikf60a9272012-12-12 20:46:15 +0000736void Output::output(StringRef s) {
737 Column += s.size();
738 Out << s;
739}
740
741void Output::outputUpToEndOfLine(StringRef s) {
Scott Linderad115b72018-10-10 18:14:02 +0000742 output(s);
Scott Linderc0830f52018-11-14 19:39:59 +0000743 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
744 !inFlowMapAnyKey(StateStack.back())))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000745 NeedsNewLine = true;
746}
747
748void Output::outputNewLine() {
749 Out << "\n";
750 Column = 0;
751}
752
753// if seq at top, indent as if map, then add "- "
754// if seq in middle, use "- " if firstKey, else use " "
755//
756
757void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000758 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000759 return;
760 NeedsNewLine = false;
761
Scott Linderad115b72018-10-10 18:14:02 +0000762 outputNewLine();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000763
Scott Linderc0830f52018-11-14 19:39:59 +0000764 if (StateStack.size() == 0)
765 return;
766
Nick Kledzikf60a9272012-12-12 20:46:15 +0000767 unsigned Indent = StateStack.size() - 1;
768 bool OutputDash = false;
769
Scott Linderc0830f52018-11-14 19:39:59 +0000770 if (StateStack.back() == inSeqFirstElement ||
771 StateStack.back() == inSeqOtherElement) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000772 OutputDash = true;
Scott Linderc0830f52018-11-14 19:39:59 +0000773 } else if ((StateStack.size() > 1) &&
774 ((StateStack.back() == inMapFirstKey) ||
775 inFlowSeqAnyElement(StateStack.back()) ||
776 (StateStack.back() == inFlowMapFirstKey)) &&
777 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000778 --Indent;
779 OutputDash = true;
780 }
781
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000782 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000783 output(" ");
784 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000785 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000786 output("- ");
787 }
788
789}
790
791void Output::paddedKey(StringRef key) {
792 output(key);
793 output(":");
794 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000795 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000796 output(&spaces[key.size()]);
797 else
798 output(" ");
799}
800
Alex Lorenzb1225082015-05-04 20:11:40 +0000801void Output::flowKey(StringRef Key) {
802 if (StateStack.back() == inFlowMapOtherKey)
803 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000804 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000805 output("\n");
806 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
807 output(" ");
808 Column = ColumnAtMapFlowStart;
809 output(" ");
810 }
811 output(Key);
812 output(": ");
813}
814
Scott Linderc0830f52018-11-14 19:39:59 +0000815NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); }
816
817bool Output::inSeqAnyElement(InState State) {
818 return State == inSeqFirstElement || State == inSeqOtherElement;
819}
820
821bool Output::inFlowSeqAnyElement(InState State) {
822 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
823}
824
825bool Output::inMapAnyKey(InState State) {
826 return State == inMapFirstKey || State == inMapOtherKey;
827}
828
829bool Output::inFlowMapAnyKey(InState State) {
830 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
831}
832
Nick Kledzikf60a9272012-12-12 20:46:15 +0000833//===----------------------------------------------------------------------===//
834// traits for built-in types
835//===----------------------------------------------------------------------===//
836
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000837void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
838 Out << (Val ? "true" : "false");
839}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000840
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000841StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
842 if (Scalar.equals("true")) {
843 Val = true;
844 return StringRef();
845 } else if (Scalar.equals("false")) {
846 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000847 return StringRef();
848 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000849 return "invalid boolean";
850}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000852void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
853 raw_ostream &Out) {
854 Out << Val;
855}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000856
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000857StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
858 StringRef &Val) {
859 Val = Scalar;
860 return StringRef();
861}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000862
John Thompson48e018a2013-11-19 17:28:21 +0000863void ScalarTraits<std::string>::output(const std::string &Val, void *,
864 raw_ostream &Out) {
865 Out << Val;
866}
867
868StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
869 std::string &Val) {
870 Val = Scalar.str();
871 return StringRef();
872}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000873
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000874void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
875 raw_ostream &Out) {
876 // use temp uin32_t because ostream thinks uint8_t is a character
877 uint32_t Num = Val;
878 Out << Num;
879}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000880
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000881StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
882 unsigned long long n;
883 if (getAsUnsignedInteger(Scalar, 0, n))
884 return "invalid number";
885 if (n > 0xFF)
886 return "out of range number";
887 Val = n;
888 return StringRef();
889}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000890
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000891void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
892 raw_ostream &Out) {
893 Out << Val;
894}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000895
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000896StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
897 uint16_t &Val) {
898 unsigned long long n;
899 if (getAsUnsignedInteger(Scalar, 0, n))
900 return "invalid number";
901 if (n > 0xFFFF)
902 return "out of range number";
903 Val = n;
904 return StringRef();
905}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000906
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000907void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
908 raw_ostream &Out) {
909 Out << Val;
910}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000911
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000912StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
913 uint32_t &Val) {
914 unsigned long long n;
915 if (getAsUnsignedInteger(Scalar, 0, n))
916 return "invalid number";
917 if (n > 0xFFFFFFFFUL)
918 return "out of range number";
919 Val = n;
920 return StringRef();
921}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000922
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000923void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
924 raw_ostream &Out) {
925 Out << Val;
926}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000927
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000928StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
929 uint64_t &Val) {
930 unsigned long long N;
931 if (getAsUnsignedInteger(Scalar, 0, N))
932 return "invalid number";
933 Val = N;
934 return StringRef();
935}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000936
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000937void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
938 // use temp in32_t because ostream thinks int8_t is a character
939 int32_t Num = Val;
940 Out << Num;
941}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000942
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000943StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
944 long long N;
945 if (getAsSignedInteger(Scalar, 0, N))
946 return "invalid number";
947 if ((N > 127) || (N < -128))
948 return "out of range number";
949 Val = N;
950 return StringRef();
951}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000952
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000953void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
954 raw_ostream &Out) {
955 Out << Val;
956}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000957
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000958StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
959 long long N;
960 if (getAsSignedInteger(Scalar, 0, N))
961 return "invalid number";
962 if ((N > INT16_MAX) || (N < INT16_MIN))
963 return "out of range number";
964 Val = N;
965 return StringRef();
966}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000967
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000968void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
969 raw_ostream &Out) {
970 Out << Val;
971}
972
973StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
974 long long N;
975 if (getAsSignedInteger(Scalar, 0, N))
976 return "invalid number";
977 if ((N > INT32_MAX) || (N < INT32_MIN))
978 return "out of range number";
979 Val = N;
980 return StringRef();
981}
982
983void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
984 raw_ostream &Out) {
985 Out << Val;
986}
987
988StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
989 long long N;
990 if (getAsSignedInteger(Scalar, 0, N))
991 return "invalid number";
992 Val = N;
993 return StringRef();
994}
995
996void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000997 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000998}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000999
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001000StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +00001001 if (to_float(Scalar, Val))
1002 return StringRef();
1003 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001004}
1005
1006void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +00001007 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001008}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001009
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001010StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +00001011 if (to_float(Scalar, Val))
1012 return StringRef();
1013 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001014}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001015
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001016void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
1017 uint8_t Num = Val;
1018 Out << format("0x%02X", Num);
1019}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001020
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001021StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
1022 unsigned long long n;
1023 if (getAsUnsignedInteger(Scalar, 0, n))
1024 return "invalid hex8 number";
1025 if (n > 0xFF)
1026 return "out of range hex8 number";
1027 Val = n;
1028 return StringRef();
1029}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001030
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001031void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
1032 uint16_t Num = Val;
1033 Out << format("0x%04X", Num);
1034}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001035
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001036StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
1037 unsigned long long n;
1038 if (getAsUnsignedInteger(Scalar, 0, n))
1039 return "invalid hex16 number";
1040 if (n > 0xFFFF)
1041 return "out of range hex16 number";
1042 Val = n;
1043 return StringRef();
1044}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001045
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001046void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1047 uint32_t Num = Val;
1048 Out << format("0x%08X", Num);
1049}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001050
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001051StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1052 unsigned long long n;
1053 if (getAsUnsignedInteger(Scalar, 0, n))
1054 return "invalid hex32 number";
1055 if (n > 0xFFFFFFFFUL)
1056 return "out of range hex32 number";
1057 Val = n;
1058 return StringRef();
1059}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001060
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001061void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1062 uint64_t Num = Val;
1063 Out << format("0x%016llX", Num);
1064}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001065
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001066StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1067 unsigned long long Num;
1068 if (getAsUnsignedInteger(Scalar, 0, Num))
1069 return "invalid hex64 number";
1070 Val = Num;
1071 return StringRef();
1072}