blob: a80adfda83034e294ba603ad8a20eb66ec636e5a [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"
Nick Kledzikf60a9272012-12-12 20:46:15 +000022#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000023#include "llvm/Support/raw_ostream.h"
Eugene Zelenko72208a82017-06-21 23:19:47 +000024#include <algorithm>
25#include <cassert>
26#include <cstdint>
27#include <cstdlib>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include <cstring>
Eugene Zelenko72208a82017-06-21 23:19:47 +000029#include <string>
30#include <vector>
31
Benjamin Kramer36b0f122012-12-12 22:40:02 +000032using namespace llvm;
33using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000034
35//===----------------------------------------------------------------------===//
36// IO
37//===----------------------------------------------------------------------===//
38
Eugene Zelenko72208a82017-06-21 23:19:47 +000039IO::IO(void *Context) : Ctxt(Context) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +000040
Eugene Zelenko72208a82017-06-21 23:19:47 +000041IO::~IO() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +000042
43void *IO::getContext() {
44 return Ctxt;
45}
46
47void IO::setContext(void *Context) {
48 Ctxt = Context;
49}
50
Nick Kledzikf60a9272012-12-12 20:46:15 +000051//===----------------------------------------------------------------------===//
52// Input
53//===----------------------------------------------------------------------===//
54
Mehdi Amini3ab3fef2016-11-28 21:38:52 +000055Input::Input(StringRef InputContent, void *Ctxt,
56 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
Eugene Zelenko72208a82017-06-21 23:19:47 +000057 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000058 if (DiagHandler)
59 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000060 DocIterator = Strm->begin();
61}
62
Alex Bradbury16843172017-07-17 11:41:30 +000063Input::Input(MemoryBufferRef Input, void *Ctxt,
64 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
65 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
66 if (DiagHandler)
67 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
68 DocIterator = Strm->begin();
69}
70
Eugene Zelenko72208a82017-06-21 23:19:47 +000071Input::~Input() = default;
Nick Kledzik0dcef842013-01-08 21:04:44 +000072
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000073std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000074
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000075// Pin the vtables to this file.
76void Input::HNode::anchor() {}
77void Input::EmptyHNode::anchor() {}
78void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000079void Input::MapHNode::anchor() {}
80void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000081
Nick Kledzik4761c602013-11-21 00:20:10 +000082bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000083 return false;
84}
85
86bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000087 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000088 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000089 if (!N) {
Alex Lorenz7a38d752015-05-12 17:44:32 +000090 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000091 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000092 return false;
93 }
94
Benjamin Kramer36b0f122012-12-12 22:40:02 +000095 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000096 // Empty files are allowed and ignored
97 ++DocIterator;
98 return setCurrentDocument();
99 }
David Blaikied759fe52014-09-15 18:39:24 +0000100 TopNode = this->createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +0000101 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000102 return true;
103 }
104 return false;
105}
106
Simon Atanasyanf97af8a2014-05-31 04:51:07 +0000107bool Input::nextDocument() {
108 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000109}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +0000110
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000111const Node *Input::getCurrentNode() const {
112 return CurrentNode ? CurrentNode->_node : nullptr;
113}
114
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000115bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000116 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000117 if (foundTag.empty()) {
118 // If no tag found and 'Tag' is the default, say it was found.
119 return Default;
120 }
Alex Lorenz7a38d752015-05-12 17:44:32 +0000121 // Return true iff found tag matches supplied tag.
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000122 return Tag.equals(foundTag);
123}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000124
125void Input::beginMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000126 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000127 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000128 // CurrentNode can be null if the document is empty.
129 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000130 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000131 MN->ValidKeys.clear();
132 }
133}
134
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000135std::vector<StringRef> Input::keys() {
136 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
137 std::vector<StringRef> Ret;
138 if (!MN) {
139 setError(CurrentNode, "not a mapping");
140 return Ret;
141 }
142 for (auto &P : MN->Mapping)
143 Ret.push_back(P.first());
144 return Ret;
145}
146
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000147bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
148 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000149 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000150 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000152
153 // CurrentNode is null for empty documents, which is an error in case required
154 // nodes are present.
155 if (!CurrentNode) {
156 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000157 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000158 return false;
159 }
160
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000161 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
162 if (!MN) {
Dave Leec6f2e692017-11-16 17:46:11 +0000163 if (Required || !isa<EmptyHNode>(CurrentNode))
164 setError(CurrentNode, "not a mapping");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000165 return false;
166 }
167 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000168 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000169 if (!Value) {
170 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000171 setError(CurrentNode, Twine("missing required key '") + Key + "'");
172 else
173 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000174 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000175 }
176 SaveInfo = CurrentNode;
177 CurrentNode = Value;
178 return true;
179}
180
181void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000182 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000183}
184
185void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000186 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000187 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000188 // CurrentNode can be null if the document is empty.
189 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000190 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000192 for (const auto &NN : MN->Mapping) {
Peter Collingbourneefdff712017-01-04 20:10:43 +0000193 if (!is_contained(MN->ValidKeys, NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000194 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195 break;
196 }
197 }
198}
199
Alex Lorenzb1225082015-05-04 20:11:40 +0000200void Input::beginFlowMapping() { beginMapping(); }
201
202void Input::endFlowMapping() { endMapping(); }
203
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000205 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000207 if (isa<EmptyHNode>(CurrentNode))
208 return 0;
209 // Treat case where there's a scalar "null" value as an empty sequence.
210 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
211 if (isNull(SN->value()))
212 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000214 // Any other type of HNode is an error.
215 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000216 return 0;
217}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000218
Nick Kledzikf60a9272012-12-12 20:46:15 +0000219void Input::endSequence() {
220}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000221
Nick Kledzikf60a9272012-12-12 20:46:15 +0000222bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000223 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000227 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000228 return true;
229 }
230 return false;
231}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000232
Nick Kledzikf60a9272012-12-12 20:46:15 +0000233void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000234 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000235}
236
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000237unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000238
Nick Kledzikf60a9272012-12-12 20:46:15 +0000239bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000240 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000241 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000242 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000243 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000244 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 return true;
246 }
247 return false;
248}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000249
Nick Kledzikf60a9272012-12-12 20:46:15 +0000250void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000251 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000252}
253
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254void Input::endFlowSequence() {
255}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000256
257void Input::beginEnumScalar() {
258 ScalarMatchFound = false;
259}
260
261bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000262 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000263 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000264 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
265 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000266 ScalarMatchFound = true;
267 return true;
268 }
269 }
270 return false;
271}
272
Michael J. Spencer731cae32015-01-23 21:57:50 +0000273bool Input::matchEnumFallback() {
274 if (ScalarMatchFound)
275 return false;
276 ScalarMatchFound = true;
277 return true;
278}
279
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000281 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000282 setError(CurrentNode, "unknown enumerated scalar");
283 }
284}
285
Nick Kledzikf60a9272012-12-12 20:46:15 +0000286bool Input::beginBitSetScalar(bool &DoClear) {
287 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291 setError(CurrentNode, "expected sequence of bit values");
292 }
293 DoClear = true;
294 return true;
295}
296
297bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000298 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000299 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000300 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000301 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000302 for (auto &N : SQ->Entries) {
303 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000304 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000305 BitValuesUsed[Index] = true;
306 return true;
307 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000308 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000309 setError(CurrentNode, "unexpected scalar in sequence of bit values");
310 }
311 ++Index;
312 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000313 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000314 setError(CurrentNode, "expected sequence of bit values");
315 }
316 return false;
317}
318
319void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000320 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000321 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000322 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000323 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000324 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
325 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000326 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000327 return;
328 }
329 }
330 }
331}
332
David Majnemer77880332014-04-10 07:37:33 +0000333void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000334 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000335 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000336 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000337 setError(CurrentNode, "unexpected scalar");
338 }
339}
340
Alex Lorenz68e787b2015-05-14 23:08:22 +0000341void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
342
Nick Kledzikf60a9272012-12-12 20:46:15 +0000343void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000344 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000345 this->setError(hnode->_node, message);
346}
347
348void Input::setError(Node *node, const Twine &message) {
349 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000350 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000351}
352
David Blaikied759fe52014-09-15 18:39:24 +0000353std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000354 SmallString<128> StringStorage;
355 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000356 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000357 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000358 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000359 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000360 }
David Blaikied759fe52014-09-15 18:39:24 +0000361 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000362 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000363 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
364 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000365 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000366 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000367 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000368 auto Entry = this->createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000369 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000370 break;
David Blaikied759fe52014-09-15 18:39:24 +0000371 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000372 }
David Blaikied759fe52014-09-15 18:39:24 +0000373 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000374 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000375 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000376 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000377 Node *KeyNode = KVN.getKey();
George Rimar3674fb62017-09-21 08:25:59 +0000378 ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode);
379 Node *Value = KVN.getValue();
380 if (!Key || !Value) {
381 if (!Key)
382 setError(KeyNode, "Map key must be a scalar");
383 if (!Value)
384 setError(KeyNode, "Map value must not be empty");
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000385 break;
386 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000387 StringStorage.clear();
George Rimar3674fb62017-09-21 08:25:59 +0000388 StringRef KeyStr = Key->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000389 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000390 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000391 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000392 }
George Rimar3674fb62017-09-21 08:25:59 +0000393 auto ValueHNode = this->createHNodes(Value);
Mehdi Amini43c24282016-11-28 04:57:04 +0000394 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000395 break;
David Blaikied759fe52014-09-15 18:39:24 +0000396 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000397 }
David Blaikied759fe52014-09-15 18:39:24 +0000398 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000399 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000400 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000401 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000402 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000403 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000404 }
405}
406
Nick Kledzikf60a9272012-12-12 20:46:15 +0000407void Input::setError(const Twine &Message) {
408 this->setError(CurrentNode, Message);
409}
410
Aaron Ballman0e63e532013-08-15 23:17:53 +0000411bool Input::canElideEmptySequence() {
412 return false;
413}
414
Nick Kledzikf60a9272012-12-12 20:46:15 +0000415//===----------------------------------------------------------------------===//
416// Output
417//===----------------------------------------------------------------------===//
418
Frederic Riss4939e6a2015-05-29 17:56:28 +0000419Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Eugene Zelenko72208a82017-06-21 23:19:47 +0000420 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421
Eugene Zelenko72208a82017-06-21 23:19:47 +0000422Output::~Output() = default;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000423
Nick Kledzik4761c602013-11-21 00:20:10 +0000424bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000425 return true;
426}
427
428void Output::beginMapping() {
429 StateStack.push_back(inMapFirstKey);
430 NeedsNewLine = true;
431}
432
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000433bool Output::mapTag(StringRef Tag, bool Use) {
434 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000435 // If this tag is being written inside a sequence we should write the start
436 // of the sequence before writing the tag, otherwise the tag won't be
437 // attached to the element in the sequence, but rather the sequence itself.
438 bool SequenceElement =
439 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
440 StateStack[StateStack.size() - 2] == inFlowSeq);
441 if (SequenceElement && StateStack.back() == inMapFirstKey) {
442 this->newLineCheck();
443 } else {
444 this->output(" ");
445 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000446 this->output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000447 if (SequenceElement) {
448 // If we're writing the tag during the first element of a map, the tag
449 // takes the place of the first element in the sequence.
450 if (StateStack.back() == inMapFirstKey) {
451 StateStack.pop_back();
452 StateStack.push_back(inMapOtherKey);
453 }
454 // Tags inside maps in sequences should act as keys in the map from a
455 // formatting perspective, so we always want a newline in a sequence.
456 NeedsNewLine = true;
457 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000458 }
459 return Use;
460}
461
Nick Kledzikf60a9272012-12-12 20:46:15 +0000462void Output::endMapping() {
463 StateStack.pop_back();
464}
465
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000466std::vector<StringRef> Output::keys() {
467 report_fatal_error("invalid call");
468}
469
Nick Kledzikf60a9272012-12-12 20:46:15 +0000470bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000471 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000472 UseDefault = false;
Zachary Turner84efd4d2017-03-15 17:47:39 +0000473 if (Required || !SameAsDefault || WriteDefaultValues) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000474 auto State = StateStack.back();
475 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
476 flowKey(Key);
477 } else {
478 this->newLineCheck();
479 this->paddedKey(Key);
480 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000481 return true;
482 }
483 return false;
484}
485
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000486void Output::postflightKey(void *) {
487 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000488 StateStack.pop_back();
489 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000490 } else if (StateStack.back() == inFlowMapFirstKey) {
491 StateStack.pop_back();
492 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000493 }
494}
495
Alex Lorenzb1225082015-05-04 20:11:40 +0000496void Output::beginFlowMapping() {
497 StateStack.push_back(inFlowMapFirstKey);
498 this->newLineCheck();
499 ColumnAtMapFlowStart = Column;
500 output("{ ");
501}
502
503void Output::endFlowMapping() {
504 StateStack.pop_back();
505 this->outputUpToEndOfLine(" }");
506}
507
Nick Kledzikf60a9272012-12-12 20:46:15 +0000508void Output::beginDocuments() {
509 this->outputUpToEndOfLine("---");
510}
511
512bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000513 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000514 this->outputUpToEndOfLine("\n---");
515 return true;
516}
517
518void Output::postflightDocument() {
519}
520
521void Output::endDocuments() {
522 output("\n...\n");
523}
524
525unsigned Output::beginSequence() {
526 StateStack.push_back(inSeq);
527 NeedsNewLine = true;
528 return 0;
529}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000530
Nick Kledzikf60a9272012-12-12 20:46:15 +0000531void Output::endSequence() {
532 StateStack.pop_back();
533}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000534
535bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000536 return true;
537}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000538
539void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000540}
541
542unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000543 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000544 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000545 ColumnAtFlowStart = Column;
546 output("[ ");
547 NeedFlowSequenceComma = false;
548 return 0;
549}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000550
Nick Kledzikf60a9272012-12-12 20:46:15 +0000551void Output::endFlowSequence() {
552 StateStack.pop_back();
553 this->outputUpToEndOfLine(" ]");
554}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000555
556bool Output::preflightFlowElement(unsigned, void *&) {
557 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000558 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000559 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000560 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000561 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000562 output(" ");
563 Column = ColumnAtFlowStart;
564 output(" ");
565 }
566 return true;
567}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000568
569void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000570 NeedFlowSequenceComma = true;
571}
572
Nick Kledzikf60a9272012-12-12 20:46:15 +0000573void Output::beginEnumScalar() {
574 EnumerationMatchFound = false;
575}
576
577bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000578 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000579 this->newLineCheck();
580 this->outputUpToEndOfLine(Str);
581 EnumerationMatchFound = true;
582 }
583 return false;
584}
585
Michael J. Spencer731cae32015-01-23 21:57:50 +0000586bool Output::matchEnumFallback() {
587 if (EnumerationMatchFound)
588 return false;
589 EnumerationMatchFound = true;
590 return true;
591}
592
Nick Kledzikf60a9272012-12-12 20:46:15 +0000593void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000594 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000595 llvm_unreachable("bad runtime enum value");
596}
597
Nick Kledzikf60a9272012-12-12 20:46:15 +0000598bool Output::beginBitSetScalar(bool &DoClear) {
599 this->newLineCheck();
600 output("[ ");
601 NeedBitValueComma = false;
602 DoClear = false;
603 return true;
604}
605
606bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000607 if (Matches) {
608 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000609 output(", ");
610 this->output(Str);
611 NeedBitValueComma = true;
612 }
613 return false;
614}
615
616void Output::endBitSetScalar() {
617 this->outputUpToEndOfLine(" ]");
618}
619
David Majnemer77880332014-04-10 07:37:33 +0000620void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000621 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000622 if (S.empty()) {
623 // Print '' for the empty string because leaving the field empty is not
624 // allowed.
625 this->outputUpToEndOfLine("''");
626 return;
627 }
David Majnemer77880332014-04-10 07:37:33 +0000628 if (!MustQuote) {
629 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000630 this->outputUpToEndOfLine(S);
631 return;
632 }
633 unsigned i = 0;
634 unsigned j = 0;
635 unsigned End = S.size();
636 output("'"); // Starting single quote.
637 const char *Base = S.data();
638 while (j < End) {
639 // Escape a single quote by doubling it.
640 if (S[j] == '\'') {
641 output(StringRef(&Base[i], j - i + 1));
642 output("'");
643 i = j + 1;
644 }
645 ++j;
646 }
647 output(StringRef(&Base[i], j - i));
648 this->outputUpToEndOfLine("'"); // Ending single quote.
649}
650
Alex Lorenz68e787b2015-05-14 23:08:22 +0000651void Output::blockScalarString(StringRef &S) {
652 if (!StateStack.empty())
653 newLineCheck();
654 output(" |");
655 outputNewLine();
656
657 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
658
659 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
660 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
661 for (unsigned I = 0; I < Indent; ++I) {
662 output(" ");
663 }
664 output(*Lines);
665 outputNewLine();
666 }
667}
668
Nick Kledzikf60a9272012-12-12 20:46:15 +0000669void Output::setError(const Twine &message) {
670}
671
Aaron Ballman0e63e532013-08-15 23:17:53 +0000672bool Output::canElideEmptySequence() {
673 // Normally, with an optional key/value where the value is an empty sequence,
674 // the whole key/value can be not written. But, that produces wrong yaml
675 // if the key/value is the only thing in the map and the map is used in
676 // a sequence. This detects if the this sequence is the first key/value
677 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000678 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000679 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000680 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000681 return true;
682 return (StateStack[StateStack.size()-2] != inSeq);
683}
684
Nick Kledzikf60a9272012-12-12 20:46:15 +0000685void Output::output(StringRef s) {
686 Column += s.size();
687 Out << s;
688}
689
690void Output::outputUpToEndOfLine(StringRef s) {
691 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000692 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
693 StateStack.back() != inFlowMapFirstKey &&
694 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000695 NeedsNewLine = true;
696}
697
698void Output::outputNewLine() {
699 Out << "\n";
700 Column = 0;
701}
702
703// if seq at top, indent as if map, then add "- "
704// if seq in middle, use "- " if firstKey, else use " "
705//
706
707void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000708 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000709 return;
710 NeedsNewLine = false;
711
712 this->outputNewLine();
713
714 assert(StateStack.size() > 0);
715 unsigned Indent = StateStack.size() - 1;
716 bool OutputDash = false;
717
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000718 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000719 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000720 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000721 (StateStack.back() == inFlowSeq) ||
722 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000723 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000724 --Indent;
725 OutputDash = true;
726 }
727
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000728 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000729 output(" ");
730 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000731 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000732 output("- ");
733 }
734
735}
736
737void Output::paddedKey(StringRef key) {
738 output(key);
739 output(":");
740 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000741 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000742 output(&spaces[key.size()]);
743 else
744 output(" ");
745}
746
Alex Lorenzb1225082015-05-04 20:11:40 +0000747void Output::flowKey(StringRef Key) {
748 if (StateStack.back() == inFlowMapOtherKey)
749 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000750 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000751 output("\n");
752 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
753 output(" ");
754 Column = ColumnAtMapFlowStart;
755 output(" ");
756 }
757 output(Key);
758 output(": ");
759}
760
Nick Kledzikf60a9272012-12-12 20:46:15 +0000761//===----------------------------------------------------------------------===//
762// traits for built-in types
763//===----------------------------------------------------------------------===//
764
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000765void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
766 Out << (Val ? "true" : "false");
767}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000768
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000769StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
770 if (Scalar.equals("true")) {
771 Val = true;
772 return StringRef();
773 } else if (Scalar.equals("false")) {
774 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000775 return StringRef();
776 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000777 return "invalid boolean";
778}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000779
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000780void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
781 raw_ostream &Out) {
782 Out << Val;
783}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000784
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000785StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
786 StringRef &Val) {
787 Val = Scalar;
788 return StringRef();
789}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000790
John Thompson48e018a2013-11-19 17:28:21 +0000791void ScalarTraits<std::string>::output(const std::string &Val, void *,
792 raw_ostream &Out) {
793 Out << Val;
794}
795
796StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
797 std::string &Val) {
798 Val = Scalar.str();
799 return StringRef();
800}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000801
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000802void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
803 raw_ostream &Out) {
804 // use temp uin32_t because ostream thinks uint8_t is a character
805 uint32_t Num = Val;
806 Out << Num;
807}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000808
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000809StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
810 unsigned long long n;
811 if (getAsUnsignedInteger(Scalar, 0, n))
812 return "invalid number";
813 if (n > 0xFF)
814 return "out of range number";
815 Val = n;
816 return StringRef();
817}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000818
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000819void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
820 raw_ostream &Out) {
821 Out << Val;
822}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000823
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000824StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
825 uint16_t &Val) {
826 unsigned long long n;
827 if (getAsUnsignedInteger(Scalar, 0, n))
828 return "invalid number";
829 if (n > 0xFFFF)
830 return "out of range number";
831 Val = n;
832 return StringRef();
833}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000834
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000835void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
836 raw_ostream &Out) {
837 Out << Val;
838}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000839
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000840StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
841 uint32_t &Val) {
842 unsigned long long n;
843 if (getAsUnsignedInteger(Scalar, 0, n))
844 return "invalid number";
845 if (n > 0xFFFFFFFFUL)
846 return "out of range number";
847 Val = n;
848 return StringRef();
849}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000850
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000851void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
852 raw_ostream &Out) {
853 Out << Val;
854}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000855
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000856StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
857 uint64_t &Val) {
858 unsigned long long N;
859 if (getAsUnsignedInteger(Scalar, 0, N))
860 return "invalid 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<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
866 // use temp in32_t because ostream thinks int8_t is a character
867 int32_t Num = Val;
868 Out << Num;
869}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000870
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000871StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
872 long long N;
873 if (getAsSignedInteger(Scalar, 0, N))
874 return "invalid number";
875 if ((N > 127) || (N < -128))
876 return "out of range number";
877 Val = N;
878 return StringRef();
879}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000880
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000881void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
882 raw_ostream &Out) {
883 Out << Val;
884}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000885
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000886StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
887 long long N;
888 if (getAsSignedInteger(Scalar, 0, N))
889 return "invalid number";
890 if ((N > INT16_MAX) || (N < INT16_MIN))
891 return "out of range 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<int32_t>::output(const int32_t &Val, void *,
897 raw_ostream &Out) {
898 Out << Val;
899}
900
901StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
902 long long N;
903 if (getAsSignedInteger(Scalar, 0, N))
904 return "invalid number";
905 if ((N > INT32_MAX) || (N < INT32_MIN))
906 return "out of range number";
907 Val = N;
908 return StringRef();
909}
910
911void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
912 raw_ostream &Out) {
913 Out << Val;
914}
915
916StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
917 long long N;
918 if (getAsSignedInteger(Scalar, 0, N))
919 return "invalid number";
920 Val = N;
921 return StringRef();
922}
923
924void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000925 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000926}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000927
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000928StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000929 if (to_float(Scalar, Val))
930 return StringRef();
931 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000932}
933
934void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000935 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000936}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000937
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000938StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000939 if (to_float(Scalar, Val))
940 return StringRef();
941 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000942}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000943
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000944void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
945 uint8_t Num = Val;
946 Out << format("0x%02X", Num);
947}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000948
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000949StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
950 unsigned long long n;
951 if (getAsUnsignedInteger(Scalar, 0, n))
952 return "invalid hex8 number";
953 if (n > 0xFF)
954 return "out of range hex8 number";
955 Val = n;
956 return StringRef();
957}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000958
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000959void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
960 uint16_t Num = Val;
961 Out << format("0x%04X", Num);
962}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000963
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000964StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
965 unsigned long long n;
966 if (getAsUnsignedInteger(Scalar, 0, n))
967 return "invalid hex16 number";
968 if (n > 0xFFFF)
969 return "out of range hex16 number";
970 Val = n;
971 return StringRef();
972}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000973
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000974void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
975 uint32_t Num = Val;
976 Out << format("0x%08X", Num);
977}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000978
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000979StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
980 unsigned long long n;
981 if (getAsUnsignedInteger(Scalar, 0, n))
982 return "invalid hex32 number";
983 if (n > 0xFFFFFFFFUL)
984 return "out of range hex32 number";
985 Val = n;
986 return StringRef();
987}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000988
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000989void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
990 uint64_t Num = Val;
991 Out << format("0x%016llX", Num);
992}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000993
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000994StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
995 unsigned long long Num;
996 if (getAsUnsignedInteger(Scalar, 0, Num))
997 return "invalid hex64 number";
998 Val = Num;
999 return StringRef();
1000}