blob: f8a80ba87873f6d2898d6b3b21390b9fe798d26c [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 ? "'" : "\"";
641 const char QuoteChar = MustQuote == QuotingType::Single ? '\'' : '"';
642
643 output(Quote); // Starting quote.
644
645 // When using single-quoted strings, any single quote ' must be doubled to be
646 // escaped.
647 // When using double-quoted strings, print \x + hex for non-printable ASCII
648 // characters, and escape double quotes.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000649 while (j < End) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000650 if (S[j] == QuoteChar) { // Escape quotes.
651 output(StringRef(&Base[i], j - i)); // "flush".
652 if (MustQuote == QuotingType::Double) { // Print it as \"
653 output(StringLiteral("\\"));
654 output(StringRef(Quote, 1));
655 } else { // Single
656 output(StringLiteral("''")); // Print it as ''
657 }
658 i = j + 1;
659 } else if (MustQuote == QuotingType::Double &&
Francis Visoiu Mistrihb2b961a2017-12-21 17:14:09 +0000660 !sys::unicode::isPrintable(S[j]) && (S[j] & 0x80) == 0) {
661 // If we're double quoting non-printable characters, we prefer printing
662 // them as "\x" + their hex representation. Note that special casing is
663 // needed for UTF-8, where a byte may be part of a UTF-8 sequence and
664 // appear as non-printable, in which case we want to print the correct
665 // unicode character and not its hex representation.
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000666 output(StringRef(&Base[i], j - i)); // "flush"
667 output(StringLiteral("\\x"));
668
669 // Output the byte 0x0F as \x0f.
670 auto FormattedHex = format_hex_no_prefix(S[j], 2);
671 Out << FormattedHex;
672 Column += 4; // one for the '\', one for the 'x', and two for the hex
673
Nick Kledzikf60a9272012-12-12 20:46:15 +0000674 i = j + 1;
675 }
676 ++j;
677 }
678 output(StringRef(&Base[i], j - i));
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000679 this->outputUpToEndOfLine(Quote); // Ending quote.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000680}
681
Alex Lorenz68e787b2015-05-14 23:08:22 +0000682void Output::blockScalarString(StringRef &S) {
683 if (!StateStack.empty())
684 newLineCheck();
685 output(" |");
686 outputNewLine();
687
688 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
689
690 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
691 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
692 for (unsigned I = 0; I < Indent; ++I) {
693 output(" ");
694 }
695 output(*Lines);
696 outputNewLine();
697 }
698}
699
Nick Kledzikf60a9272012-12-12 20:46:15 +0000700void Output::setError(const Twine &message) {
701}
702
Aaron Ballman0e63e532013-08-15 23:17:53 +0000703bool Output::canElideEmptySequence() {
704 // Normally, with an optional key/value where the value is an empty sequence,
705 // the whole key/value can be not written. But, that produces wrong yaml
706 // if the key/value is the only thing in the map and the map is used in
707 // a sequence. This detects if the this sequence is the first key/value
708 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000709 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000710 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000711 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000712 return true;
713 return (StateStack[StateStack.size()-2] != inSeq);
714}
715
Nick Kledzikf60a9272012-12-12 20:46:15 +0000716void Output::output(StringRef s) {
717 Column += s.size();
718 Out << s;
719}
720
721void Output::outputUpToEndOfLine(StringRef s) {
722 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000723 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
724 StateStack.back() != inFlowMapFirstKey &&
725 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000726 NeedsNewLine = true;
727}
728
729void Output::outputNewLine() {
730 Out << "\n";
731 Column = 0;
732}
733
734// if seq at top, indent as if map, then add "- "
735// if seq in middle, use "- " if firstKey, else use " "
736//
737
738void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000739 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000740 return;
741 NeedsNewLine = false;
742
743 this->outputNewLine();
744
745 assert(StateStack.size() > 0);
746 unsigned Indent = StateStack.size() - 1;
747 bool OutputDash = false;
748
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000749 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000750 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000751 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000752 (StateStack.back() == inFlowSeq) ||
753 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000754 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000755 --Indent;
756 OutputDash = true;
757 }
758
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000759 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000760 output(" ");
761 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000762 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000763 output("- ");
764 }
765
766}
767
768void Output::paddedKey(StringRef key) {
769 output(key);
770 output(":");
771 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000772 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000773 output(&spaces[key.size()]);
774 else
775 output(" ");
776}
777
Alex Lorenzb1225082015-05-04 20:11:40 +0000778void Output::flowKey(StringRef Key) {
779 if (StateStack.back() == inFlowMapOtherKey)
780 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000781 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000782 output("\n");
783 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
784 output(" ");
785 Column = ColumnAtMapFlowStart;
786 output(" ");
787 }
788 output(Key);
789 output(": ");
790}
791
Nick Kledzikf60a9272012-12-12 20:46:15 +0000792//===----------------------------------------------------------------------===//
793// traits for built-in types
794//===----------------------------------------------------------------------===//
795
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000796void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
797 Out << (Val ? "true" : "false");
798}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000799
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000800StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
801 if (Scalar.equals("true")) {
802 Val = true;
803 return StringRef();
804 } else if (Scalar.equals("false")) {
805 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000806 return StringRef();
807 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000808 return "invalid boolean";
809}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000810
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000811void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
812 raw_ostream &Out) {
813 Out << Val;
814}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000815
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000816StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
817 StringRef &Val) {
818 Val = Scalar;
819 return StringRef();
820}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000821
John Thompson48e018a2013-11-19 17:28:21 +0000822void ScalarTraits<std::string>::output(const std::string &Val, void *,
823 raw_ostream &Out) {
824 Out << Val;
825}
826
827StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
828 std::string &Val) {
829 Val = Scalar.str();
830 return StringRef();
831}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000832
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000833void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
834 raw_ostream &Out) {
835 // use temp uin32_t because ostream thinks uint8_t is a character
836 uint32_t Num = Val;
837 Out << Num;
838}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000839
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000840StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
841 unsigned long long n;
842 if (getAsUnsignedInteger(Scalar, 0, n))
843 return "invalid number";
844 if (n > 0xFF)
845 return "out of range number";
846 Val = n;
847 return StringRef();
848}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000849
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000850void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
851 raw_ostream &Out) {
852 Out << Val;
853}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000854
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000855StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
856 uint16_t &Val) {
857 unsigned long long n;
858 if (getAsUnsignedInteger(Scalar, 0, n))
859 return "invalid number";
860 if (n > 0xFFFF)
861 return "out of range number";
862 Val = n;
863 return StringRef();
864}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000865
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000866void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
867 raw_ostream &Out) {
868 Out << Val;
869}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000870
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000871StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
872 uint32_t &Val) {
873 unsigned long long n;
874 if (getAsUnsignedInteger(Scalar, 0, n))
875 return "invalid number";
876 if (n > 0xFFFFFFFFUL)
877 return "out of range number";
878 Val = n;
879 return StringRef();
880}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000881
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000882void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
883 raw_ostream &Out) {
884 Out << Val;
885}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000886
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000887StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
888 uint64_t &Val) {
889 unsigned long long N;
890 if (getAsUnsignedInteger(Scalar, 0, N))
891 return "invalid number";
892 Val = N;
893 return StringRef();
894}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000895
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000896void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
897 // use temp in32_t because ostream thinks int8_t is a character
898 int32_t Num = Val;
899 Out << Num;
900}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000901
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000902StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
903 long long N;
904 if (getAsSignedInteger(Scalar, 0, N))
905 return "invalid number";
906 if ((N > 127) || (N < -128))
907 return "out of range number";
908 Val = N;
909 return StringRef();
910}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000911
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000912void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
913 raw_ostream &Out) {
914 Out << Val;
915}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000916
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000917StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
918 long long N;
919 if (getAsSignedInteger(Scalar, 0, N))
920 return "invalid number";
921 if ((N > INT16_MAX) || (N < INT16_MIN))
922 return "out of range number";
923 Val = N;
924 return StringRef();
925}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000926
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000927void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
928 raw_ostream &Out) {
929 Out << Val;
930}
931
932StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
933 long long N;
934 if (getAsSignedInteger(Scalar, 0, N))
935 return "invalid number";
936 if ((N > INT32_MAX) || (N < INT32_MIN))
937 return "out of range number";
938 Val = N;
939 return StringRef();
940}
941
942void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
943 raw_ostream &Out) {
944 Out << Val;
945}
946
947StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
948 long long N;
949 if (getAsSignedInteger(Scalar, 0, N))
950 return "invalid number";
951 Val = N;
952 return StringRef();
953}
954
955void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000956 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000957}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000958
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000959StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000960 if (to_float(Scalar, Val))
961 return StringRef();
962 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000963}
964
965void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000966 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000967}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000968
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000969StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000970 if (to_float(Scalar, Val))
971 return StringRef();
972 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000973}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000974
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000975void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
976 uint8_t Num = Val;
977 Out << format("0x%02X", Num);
978}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000979
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000980StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
981 unsigned long long n;
982 if (getAsUnsignedInteger(Scalar, 0, n))
983 return "invalid hex8 number";
984 if (n > 0xFF)
985 return "out of range hex8 number";
986 Val = n;
987 return StringRef();
988}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000989
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000990void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
991 uint16_t Num = Val;
992 Out << format("0x%04X", Num);
993}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000994
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000995StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
996 unsigned long long n;
997 if (getAsUnsignedInteger(Scalar, 0, n))
998 return "invalid hex16 number";
999 if (n > 0xFFFF)
1000 return "out of range hex16 number";
1001 Val = n;
1002 return StringRef();
1003}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001004
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001005void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1006 uint32_t Num = Val;
1007 Out << format("0x%08X", Num);
1008}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001009
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001010StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1011 unsigned long long n;
1012 if (getAsUnsignedInteger(Scalar, 0, n))
1013 return "invalid hex32 number";
1014 if (n > 0xFFFFFFFFUL)
1015 return "out of range hex32 number";
1016 Val = n;
1017 return StringRef();
1018}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001019
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001020void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1021 uint64_t Num = Val;
1022 Out << format("0x%016llX", Num);
1023}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001024
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001025StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1026 unsigned long long Num;
1027 if (getAsUnsignedInteger(Scalar, 0, Num))
1028 return "invalid hex64 number";
1029 Val = Num;
1030 return StringRef();
1031}