blob: 05ca40f03018a9bf23ba35fcb198d01e3f42c69b [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 &&
660 !sys::unicode::isPrintable(S[j])) {
661 output(StringRef(&Base[i], j - i)); // "flush"
662 output(StringLiteral("\\x"));
663
664 // Output the byte 0x0F as \x0f.
665 auto FormattedHex = format_hex_no_prefix(S[j], 2);
666 Out << FormattedHex;
667 Column += 4; // one for the '\', one for the 'x', and two for the hex
668
Nick Kledzikf60a9272012-12-12 20:46:15 +0000669 i = j + 1;
670 }
671 ++j;
672 }
673 output(StringRef(&Base[i], j - i));
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000674 this->outputUpToEndOfLine(Quote); // Ending quote.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000675}
676
Alex Lorenz68e787b2015-05-14 23:08:22 +0000677void Output::blockScalarString(StringRef &S) {
678 if (!StateStack.empty())
679 newLineCheck();
680 output(" |");
681 outputNewLine();
682
683 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
684
685 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
686 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
687 for (unsigned I = 0; I < Indent; ++I) {
688 output(" ");
689 }
690 output(*Lines);
691 outputNewLine();
692 }
693}
694
Nick Kledzikf60a9272012-12-12 20:46:15 +0000695void Output::setError(const Twine &message) {
696}
697
Aaron Ballman0e63e532013-08-15 23:17:53 +0000698bool Output::canElideEmptySequence() {
699 // Normally, with an optional key/value where the value is an empty sequence,
700 // the whole key/value can be not written. But, that produces wrong yaml
701 // if the key/value is the only thing in the map and the map is used in
702 // a sequence. This detects if the this sequence is the first key/value
703 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000704 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000705 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000706 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000707 return true;
708 return (StateStack[StateStack.size()-2] != inSeq);
709}
710
Nick Kledzikf60a9272012-12-12 20:46:15 +0000711void Output::output(StringRef s) {
712 Column += s.size();
713 Out << s;
714}
715
716void Output::outputUpToEndOfLine(StringRef s) {
717 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000718 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
719 StateStack.back() != inFlowMapFirstKey &&
720 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000721 NeedsNewLine = true;
722}
723
724void Output::outputNewLine() {
725 Out << "\n";
726 Column = 0;
727}
728
729// if seq at top, indent as if map, then add "- "
730// if seq in middle, use "- " if firstKey, else use " "
731//
732
733void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000734 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000735 return;
736 NeedsNewLine = false;
737
738 this->outputNewLine();
739
740 assert(StateStack.size() > 0);
741 unsigned Indent = StateStack.size() - 1;
742 bool OutputDash = false;
743
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000744 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000745 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000746 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000747 (StateStack.back() == inFlowSeq) ||
748 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000749 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000750 --Indent;
751 OutputDash = true;
752 }
753
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000754 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000755 output(" ");
756 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000757 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000758 output("- ");
759 }
760
761}
762
763void Output::paddedKey(StringRef key) {
764 output(key);
765 output(":");
766 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000767 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000768 output(&spaces[key.size()]);
769 else
770 output(" ");
771}
772
Alex Lorenzb1225082015-05-04 20:11:40 +0000773void Output::flowKey(StringRef Key) {
774 if (StateStack.back() == inFlowMapOtherKey)
775 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000776 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000777 output("\n");
778 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
779 output(" ");
780 Column = ColumnAtMapFlowStart;
781 output(" ");
782 }
783 output(Key);
784 output(": ");
785}
786
Nick Kledzikf60a9272012-12-12 20:46:15 +0000787//===----------------------------------------------------------------------===//
788// traits for built-in types
789//===----------------------------------------------------------------------===//
790
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000791void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
792 Out << (Val ? "true" : "false");
793}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000794
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000795StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
796 if (Scalar.equals("true")) {
797 Val = true;
798 return StringRef();
799 } else if (Scalar.equals("false")) {
800 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000801 return StringRef();
802 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000803 return "invalid boolean";
804}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000805
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000806void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
807 raw_ostream &Out) {
808 Out << Val;
809}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000810
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000811StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
812 StringRef &Val) {
813 Val = Scalar;
814 return StringRef();
815}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000816
John Thompson48e018a2013-11-19 17:28:21 +0000817void ScalarTraits<std::string>::output(const std::string &Val, void *,
818 raw_ostream &Out) {
819 Out << Val;
820}
821
822StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
823 std::string &Val) {
824 Val = Scalar.str();
825 return StringRef();
826}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000827
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000828void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
829 raw_ostream &Out) {
830 // use temp uin32_t because ostream thinks uint8_t is a character
831 uint32_t Num = Val;
832 Out << Num;
833}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000834
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000835StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
836 unsigned long long n;
837 if (getAsUnsignedInteger(Scalar, 0, n))
838 return "invalid number";
839 if (n > 0xFF)
840 return "out of range number";
841 Val = n;
842 return StringRef();
843}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000844
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000845void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
846 raw_ostream &Out) {
847 Out << Val;
848}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000849
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000850StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
851 uint16_t &Val) {
852 unsigned long long n;
853 if (getAsUnsignedInteger(Scalar, 0, n))
854 return "invalid number";
855 if (n > 0xFFFF)
856 return "out of range number";
857 Val = n;
858 return StringRef();
859}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000860
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000861void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
862 raw_ostream &Out) {
863 Out << Val;
864}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000865
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000866StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
867 uint32_t &Val) {
868 unsigned long long n;
869 if (getAsUnsignedInteger(Scalar, 0, n))
870 return "invalid number";
871 if (n > 0xFFFFFFFFUL)
872 return "out of range number";
873 Val = n;
874 return StringRef();
875}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000876
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000877void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
878 raw_ostream &Out) {
879 Out << Val;
880}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000881
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000882StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
883 uint64_t &Val) {
884 unsigned long long N;
885 if (getAsUnsignedInteger(Scalar, 0, N))
886 return "invalid number";
887 Val = N;
888 return StringRef();
889}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000890
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000891void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
892 // use temp in32_t because ostream thinks int8_t is a character
893 int32_t Num = Val;
894 Out << Num;
895}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000896
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000897StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
898 long long N;
899 if (getAsSignedInteger(Scalar, 0, N))
900 return "invalid number";
901 if ((N > 127) || (N < -128))
902 return "out of range number";
903 Val = N;
904 return StringRef();
905}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000906
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000907void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
908 raw_ostream &Out) {
909 Out << Val;
910}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000911
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000912StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
913 long long N;
914 if (getAsSignedInteger(Scalar, 0, N))
915 return "invalid number";
916 if ((N > INT16_MAX) || (N < INT16_MIN))
917 return "out of range number";
918 Val = N;
919 return StringRef();
920}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000921
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000922void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
923 raw_ostream &Out) {
924 Out << Val;
925}
926
927StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
928 long long N;
929 if (getAsSignedInteger(Scalar, 0, N))
930 return "invalid number";
931 if ((N > INT32_MAX) || (N < INT32_MIN))
932 return "out of range number";
933 Val = N;
934 return StringRef();
935}
936
937void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
938 raw_ostream &Out) {
939 Out << Val;
940}
941
942StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
943 long long N;
944 if (getAsSignedInteger(Scalar, 0, N))
945 return "invalid number";
946 Val = N;
947 return StringRef();
948}
949
950void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000951 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000952}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000953
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000954StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000955 if (to_float(Scalar, Val))
956 return StringRef();
957 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000958}
959
960void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000961 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000962}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000963
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000964StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
Pavel Labathec000f42017-06-23 12:55:02 +0000965 if (to_float(Scalar, Val))
966 return StringRef();
967 return "invalid floating point number";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000968}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000969
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000970void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
971 uint8_t Num = Val;
972 Out << format("0x%02X", Num);
973}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000974
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000975StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
976 unsigned long long n;
977 if (getAsUnsignedInteger(Scalar, 0, n))
978 return "invalid hex8 number";
979 if (n > 0xFF)
980 return "out of range hex8 number";
981 Val = n;
982 return StringRef();
983}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000984
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000985void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
986 uint16_t Num = Val;
987 Out << format("0x%04X", Num);
988}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000989
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000990StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
991 unsigned long long n;
992 if (getAsUnsignedInteger(Scalar, 0, n))
993 return "invalid hex16 number";
994 if (n > 0xFFFF)
995 return "out of range hex16 number";
996 Val = n;
997 return StringRef();
998}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000999
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001000void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1001 uint32_t Num = Val;
1002 Out << format("0x%08X", Num);
1003}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001004
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001005StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1006 unsigned long long n;
1007 if (getAsUnsignedInteger(Scalar, 0, n))
1008 return "invalid hex32 number";
1009 if (n > 0xFFFFFFFFUL)
1010 return "out of range hex32 number";
1011 Val = n;
1012 return StringRef();
1013}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001014
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001015void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1016 uint64_t Num = Val;
1017 Out << format("0x%016llX", Num);
1018}
Nick Kledzikf60a9272012-12-12 20:46:15 +00001019
Benjamin Kramer36b0f122012-12-12 22:40:02 +00001020StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1021 unsigned long long Num;
1022 if (getAsUnsignedInteger(Scalar, 0, Num))
1023 return "invalid hex64 number";
1024 Val = Num;
1025 return StringRef();
1026}