blob: d6345efd00cd3a50ace6f67ed16ff45848553003 [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 }
David Blaikied759fe52014-09-15 18:39:24 +0000101 TopNode = this->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
Nick Kledzikf60a9272012-12-12 20:46:15 +0000344void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000345 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000346 this->setError(hnode->_node, message);
347}
348
349void Input::setError(Node *node, const Twine &message) {
350 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000351 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000352}
353
David Blaikied759fe52014-09-15 18:39:24 +0000354std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000355 SmallString<128> StringStorage;
356 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000357 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000358 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000359 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000360 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000361 }
David Blaikied759fe52014-09-15 18:39:24 +0000362 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000363 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000364 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
365 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000366 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000367 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000368 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000369 auto Entry = this->createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000370 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000371 break;
David Blaikied759fe52014-09-15 18:39:24 +0000372 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000373 }
David Blaikied759fe52014-09-15 18:39:24 +0000374 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000375 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000376 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000377 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000378 Node *KeyNode = KVN.getKey();
George Rimar3674fb62017-09-21 08:25:59 +0000379 ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode);
380 Node *Value = KVN.getValue();
381 if (!Key || !Value) {
382 if (!Key)
383 setError(KeyNode, "Map key must be a scalar");
384 if (!Value)
385 setError(KeyNode, "Map value must not be empty");
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000386 break;
387 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000388 StringStorage.clear();
George Rimar3674fb62017-09-21 08:25:59 +0000389 StringRef KeyStr = Key->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000390 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000391 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000392 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000393 }
George Rimar3674fb62017-09-21 08:25:59 +0000394 auto ValueHNode = this->createHNodes(Value);
Mehdi Amini43c24282016-11-28 04:57:04 +0000395 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000396 break;
David Blaikied759fe52014-09-15 18:39:24 +0000397 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000398 }
David Blaikied759fe52014-09-15 18:39:24 +0000399 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000400 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000401 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000402 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000403 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000404 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405 }
406}
407
Nick Kledzikf60a9272012-12-12 20:46:15 +0000408void Input::setError(const Twine &Message) {
409 this->setError(CurrentNode, Message);
410}
411
Aaron Ballman0e63e532013-08-15 23:17:53 +0000412bool Input::canElideEmptySequence() {
413 return false;
414}
415
Nick Kledzikf60a9272012-12-12 20:46:15 +0000416//===----------------------------------------------------------------------===//
417// Output
418//===----------------------------------------------------------------------===//
419
Frederic Riss4939e6a2015-05-29 17:56:28 +0000420Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Eugene Zelenko72208a82017-06-21 23:19:47 +0000421 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000422
Eugene Zelenko72208a82017-06-21 23:19:47 +0000423Output::~Output() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000424
Nick Kledzik4761c602013-11-21 00:20:10 +0000425bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000426 return true;
427}
428
429void Output::beginMapping() {
430 StateStack.push_back(inMapFirstKey);
431 NeedsNewLine = true;
432}
433
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000434bool Output::mapTag(StringRef Tag, bool Use) {
435 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000436 // If this tag is being written inside a sequence we should write the start
437 // of the sequence before writing the tag, otherwise the tag won't be
438 // attached to the element in the sequence, but rather the sequence itself.
439 bool SequenceElement =
440 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
441 StateStack[StateStack.size() - 2] == inFlowSeq);
442 if (SequenceElement && StateStack.back() == inMapFirstKey) {
443 this->newLineCheck();
444 } else {
445 this->output(" ");
446 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000447 this->output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000448 if (SequenceElement) {
449 // If we're writing the tag during the first element of a map, the tag
450 // takes the place of the first element in the sequence.
451 if (StateStack.back() == inMapFirstKey) {
452 StateStack.pop_back();
453 StateStack.push_back(inMapOtherKey);
454 }
455 // Tags inside maps in sequences should act as keys in the map from a
456 // formatting perspective, so we always want a newline in a sequence.
457 NeedsNewLine = true;
458 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000459 }
460 return Use;
461}
462
Nick Kledzikf60a9272012-12-12 20:46:15 +0000463void Output::endMapping() {
464 StateStack.pop_back();
465}
466
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000467std::vector<StringRef> Output::keys() {
468 report_fatal_error("invalid call");
469}
470
Nick Kledzikf60a9272012-12-12 20:46:15 +0000471bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000472 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000473 UseDefault = false;
Zachary Turner84efd4d2017-03-15 17:47:39 +0000474 if (Required || !SameAsDefault || WriteDefaultValues) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000475 auto State = StateStack.back();
476 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
477 flowKey(Key);
478 } else {
479 this->newLineCheck();
480 this->paddedKey(Key);
481 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000482 return true;
483 }
484 return false;
485}
486
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000487void Output::postflightKey(void *) {
488 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000489 StateStack.pop_back();
490 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000491 } else if (StateStack.back() == inFlowMapFirstKey) {
492 StateStack.pop_back();
493 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000494 }
495}
496
Alex Lorenzb1225082015-05-04 20:11:40 +0000497void Output::beginFlowMapping() {
498 StateStack.push_back(inFlowMapFirstKey);
499 this->newLineCheck();
500 ColumnAtMapFlowStart = Column;
501 output("{ ");
502}
503
504void Output::endFlowMapping() {
505 StateStack.pop_back();
506 this->outputUpToEndOfLine(" }");
507}
508
Nick Kledzikf60a9272012-12-12 20:46:15 +0000509void Output::beginDocuments() {
510 this->outputUpToEndOfLine("---");
511}
512
513bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000514 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000515 this->outputUpToEndOfLine("\n---");
516 return true;
517}
518
519void Output::postflightDocument() {
520}
521
522void Output::endDocuments() {
523 output("\n...\n");
524}
525
526unsigned Output::beginSequence() {
527 StateStack.push_back(inSeq);
528 NeedsNewLine = true;
529 return 0;
530}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000531
Nick Kledzikf60a9272012-12-12 20:46:15 +0000532void Output::endSequence() {
533 StateStack.pop_back();
534}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000535
536bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000537 return true;
538}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000539
540void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000541}
542
543unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000544 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000545 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000546 ColumnAtFlowStart = Column;
547 output("[ ");
548 NeedFlowSequenceComma = false;
549 return 0;
550}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000551
Nick Kledzikf60a9272012-12-12 20:46:15 +0000552void Output::endFlowSequence() {
553 StateStack.pop_back();
554 this->outputUpToEndOfLine(" ]");
555}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000556
557bool Output::preflightFlowElement(unsigned, void *&) {
558 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000559 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000560 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000561 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000562 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000563 output(" ");
564 Column = ColumnAtFlowStart;
565 output(" ");
566 }
567 return true;
568}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000569
570void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000571 NeedFlowSequenceComma = true;
572}
573
Nick Kledzikf60a9272012-12-12 20:46:15 +0000574void Output::beginEnumScalar() {
575 EnumerationMatchFound = false;
576}
577
578bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000579 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000580 this->newLineCheck();
581 this->outputUpToEndOfLine(Str);
582 EnumerationMatchFound = true;
583 }
584 return false;
585}
586
Michael J. Spencer731cae32015-01-23 21:57:50 +0000587bool Output::matchEnumFallback() {
588 if (EnumerationMatchFound)
589 return false;
590 EnumerationMatchFound = true;
591 return true;
592}
593
Nick Kledzikf60a9272012-12-12 20:46:15 +0000594void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000595 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000596 llvm_unreachable("bad runtime enum value");
597}
598
Nick Kledzikf60a9272012-12-12 20:46:15 +0000599bool Output::beginBitSetScalar(bool &DoClear) {
600 this->newLineCheck();
601 output("[ ");
602 NeedBitValueComma = false;
603 DoClear = false;
604 return true;
605}
606
607bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000608 if (Matches) {
609 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000610 output(", ");
611 this->output(Str);
612 NeedBitValueComma = true;
613 }
614 return false;
615}
616
617void Output::endBitSetScalar() {
618 this->outputUpToEndOfLine(" ]");
619}
620
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000621void Output::scalarString(StringRef &S, QuotingType MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000622 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000623 if (S.empty()) {
624 // Print '' for the empty string because leaving the field empty is not
625 // allowed.
626 this->outputUpToEndOfLine("''");
627 return;
628 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000629 if (MustQuote == QuotingType::None) {
David Majnemer77880332014-04-10 07:37:33 +0000630 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000631 this->outputUpToEndOfLine(S);
632 return;
633 }
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000634
Nick Kledzikf60a9272012-12-12 20:46:15 +0000635 unsigned i = 0;
636 unsigned j = 0;
637 unsigned End = S.size();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000638 const char *Base = S.data();
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000639
640 const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000641 output(Quote); // Starting quote.
642
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000643 // When using double-quoted strings (and only in that case), non-printable characters may be
644 // present, and will be escaped using a variety of unicode-scalar and special short-form
645 // escapes. This is handled in yaml::escape.
646 if (MustQuote == QuotingType::Double) {
647 output(yaml::escape(Base, /* EscapePrintable= */ false));
648 this->outputUpToEndOfLine(Quote);
649 return;
650 }
651
652 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000653 while (j < End) {
Graydon Hoare926cd9b2018-03-27 19:52:45 +0000654 if (S[j] == '\'') { // Escape quotes.
655 output(StringRef(&Base[i], j - i)); // "flush".
656 output(StringLiteral("''")); // Print it as ''
Nick Kledzikf60a9272012-12-12 20:46:15 +0000657 i = j + 1;
658 }
659 ++j;
660 }
661 output(StringRef(&Base[i], j - i));
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000662 this->outputUpToEndOfLine(Quote); // Ending quote.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000663}
664
Alex Lorenz68e787b2015-05-14 23:08:22 +0000665void Output::blockScalarString(StringRef &S) {
666 if (!StateStack.empty())
667 newLineCheck();
668 output(" |");
669 outputNewLine();
670
671 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
672
673 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
674 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
675 for (unsigned I = 0; I < Indent; ++I) {
676 output(" ");
677 }
678 output(*Lines);
679 outputNewLine();
680 }
681}
682
Nick Kledzikf60a9272012-12-12 20:46:15 +0000683void Output::setError(const Twine &message) {
684}
685
Aaron Ballman0e63e532013-08-15 23:17:53 +0000686bool Output::canElideEmptySequence() {
687 // Normally, with an optional key/value where the value is an empty sequence,
688 // the whole key/value can be not written. But, that produces wrong yaml
689 // if the key/value is the only thing in the map and the map is used in
690 // a sequence. This detects if the this sequence is the first key/value
691 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000692 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000693 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000694 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000695 return true;
696 return (StateStack[StateStack.size()-2] != inSeq);
697}
698
Nick Kledzikf60a9272012-12-12 20:46:15 +0000699void Output::output(StringRef s) {
700 Column += s.size();
701 Out << s;
702}
703
704void Output::outputUpToEndOfLine(StringRef s) {
705 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000706 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
707 StateStack.back() != inFlowMapFirstKey &&
708 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000709 NeedsNewLine = true;
710}
711
712void Output::outputNewLine() {
713 Out << "\n";
714 Column = 0;
715}
716
717// if seq at top, indent as if map, then add "- "
718// if seq in middle, use "- " if firstKey, else use " "
719//
720
721void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000722 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000723 return;
724 NeedsNewLine = false;
725
726 this->outputNewLine();
727
728 assert(StateStack.size() > 0);
729 unsigned Indent = StateStack.size() - 1;
730 bool OutputDash = false;
731
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000732 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000733 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000734 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000735 (StateStack.back() == inFlowSeq) ||
736 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000737 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000738 --Indent;
739 OutputDash = true;
740 }
741
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000742 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000743 output(" ");
744 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000745 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000746 output("- ");
747 }
748
749}
750
751void Output::paddedKey(StringRef key) {
752 output(key);
753 output(":");
754 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000755 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000756 output(&spaces[key.size()]);
757 else
758 output(" ");
759}
760
Alex Lorenzb1225082015-05-04 20:11:40 +0000761void Output::flowKey(StringRef Key) {
762 if (StateStack.back() == inFlowMapOtherKey)
763 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000764 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000765 output("\n");
766 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
767 output(" ");
768 Column = ColumnAtMapFlowStart;
769 output(" ");
770 }
771 output(Key);
772 output(": ");
773}
774
Nick Kledzikf60a9272012-12-12 20:46:15 +0000775//===----------------------------------------------------------------------===//
776// traits for built-in types
777//===----------------------------------------------------------------------===//
778
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000779void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
780 Out << (Val ? "true" : "false");
781}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000782
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000783StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
784 if (Scalar.equals("true")) {
785 Val = true;
786 return StringRef();
787 } else if (Scalar.equals("false")) {
788 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000789 return StringRef();
790 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000791 return "invalid boolean";
792}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000793
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000794void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
795 raw_ostream &Out) {
796 Out << Val;
797}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000798
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000799StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
800 StringRef &Val) {
801 Val = Scalar;
802 return StringRef();
803}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000804
John Thompson48e018a2013-11-19 17:28:21 +0000805void ScalarTraits<std::string>::output(const std::string &Val, void *,
806 raw_ostream &Out) {
807 Out << Val;
808}
809
810StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
811 std::string &Val) {
812 Val = Scalar.str();
813 return StringRef();
814}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000815
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000816void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
817 raw_ostream &Out) {
818 // use temp uin32_t because ostream thinks uint8_t is a character
819 uint32_t Num = Val;
820 Out << Num;
821}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000822
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000823StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
824 unsigned long long n;
825 if (getAsUnsignedInteger(Scalar, 0, n))
826 return "invalid number";
827 if (n > 0xFF)
828 return "out of range number";
829 Val = n;
830 return StringRef();
831}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000832
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000833void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
834 raw_ostream &Out) {
835 Out << Val;
836}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000837
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000838StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
839 uint16_t &Val) {
840 unsigned long long n;
841 if (getAsUnsignedInteger(Scalar, 0, n))
842 return "invalid number";
843 if (n > 0xFFFF)
844 return "out of range number";
845 Val = n;
846 return StringRef();
847}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000848
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000849void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
850 raw_ostream &Out) {
851 Out << Val;
852}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000853
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000854StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
855 uint32_t &Val) {
856 unsigned long long n;
857 if (getAsUnsignedInteger(Scalar, 0, n))
858 return "invalid number";
859 if (n > 0xFFFFFFFFUL)
860 return "out of range number";
861 Val = n;
862 return StringRef();
863}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000864
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000865void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
866 raw_ostream &Out) {
867 Out << Val;
868}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000869
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000870StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
871 uint64_t &Val) {
872 unsigned long long N;
873 if (getAsUnsignedInteger(Scalar, 0, N))
874 return "invalid number";
875 Val = N;
876 return StringRef();
877}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000878
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000879void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
880 // use temp in32_t because ostream thinks int8_t is a character
881 int32_t Num = Val;
882 Out << Num;
883}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000884
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000885StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
886 long long N;
887 if (getAsSignedInteger(Scalar, 0, N))
888 return "invalid number";
889 if ((N > 127) || (N < -128))
890 return "out of range number";
891 Val = N;
892 return StringRef();
893}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000894
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000895void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
896 raw_ostream &Out) {
897 Out << Val;
898}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000899
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000900StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
901 long long N;
902 if (getAsSignedInteger(Scalar, 0, N))
903 return "invalid number";
904 if ((N > INT16_MAX) || (N < INT16_MIN))
905 return "out of range number";
906 Val = N;
907 return StringRef();
908}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000909
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000910void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
911 raw_ostream &Out) {
912 Out << Val;
913}
914
915StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
916 long long N;
917 if (getAsSignedInteger(Scalar, 0, N))
918 return "invalid number";
919 if ((N > INT32_MAX) || (N < INT32_MIN))
920 return "out of range number";
921 Val = N;
922 return StringRef();
923}
924
925void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
926 raw_ostream &Out) {
927 Out << Val;
928}
929
930StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
931 long long N;
932 if (getAsSignedInteger(Scalar, 0, N))
933 return "invalid number";
934 Val = N;
935 return StringRef();
936}
937
938void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000939 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000940}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000941
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000942StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000943 if (to_float(Scalar, Val))
944 return StringRef();
945 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000946}
947
948void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000949 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000950}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000951
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000952StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000953 if (to_float(Scalar, Val))
954 return StringRef();
955 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000956}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000957
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000958void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
959 uint8_t Num = Val;
960 Out << format("0x%02X", Num);
961}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000962
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000963StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
964 unsigned long long n;
965 if (getAsUnsignedInteger(Scalar, 0, n))
966 return "invalid hex8 number";
967 if (n > 0xFF)
968 return "out of range hex8 number";
969 Val = n;
970 return StringRef();
971}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000972
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000973void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
974 uint16_t Num = Val;
975 Out << format("0x%04X", Num);
976}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000977
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000978StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
979 unsigned long long n;
980 if (getAsUnsignedInteger(Scalar, 0, n))
981 return "invalid hex16 number";
982 if (n > 0xFFFF)
983 return "out of range hex16 number";
984 Val = n;
985 return StringRef();
986}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000987
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000988void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
989 uint32_t Num = Val;
990 Out << format("0x%08X", Num);
991}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000992
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000993StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
994 unsigned long long n;
995 if (getAsUnsignedInteger(Scalar, 0, n))
996 return "invalid hex32 number";
997 if (n > 0xFFFFFFFFUL)
998 return "out of range hex32 number";
999 Val = n;
1000 return StringRef();
1001}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001002
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001003void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1004 uint64_t Num = Val;
1005 Out << format("0x%016llX", Num);
1006}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001007
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001008StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1009 unsigned long long Num;
1010 if (getAsUnsignedInteger(Scalar, 0, Num))
1011 return "invalid hex64 number";
1012 Val = Num;
1013 return StringRef();
1014}