blob: 75fac20a8edb026c3d8cd166bb7cdb697ef5e5f3 [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"
11#include "llvm/ADT/SmallString.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000012#include "llvm/ADT/Twine.h"
13#include "llvm/Support/Casting.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000014#include "llvm/Support/Errc.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000015#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000016#include "llvm/Support/Format.h"
Alex Lorenz68e787b2015-05-14 23:08:22 +000017#include "llvm/Support/LineIterator.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000018#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000019#include "llvm/Support/raw_ostream.h"
Rui Ueyama106eded2013-09-11 04:00:08 +000020#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include <cstring>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000022using namespace llvm;
23using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000024
25//===----------------------------------------------------------------------===//
26// IO
27//===----------------------------------------------------------------------===//
28
29IO::IO(void *Context) : Ctxt(Context) {
30}
31
32IO::~IO() {
33}
34
35void *IO::getContext() {
36 return Ctxt;
37}
38
39void IO::setContext(void *Context) {
40 Ctxt = Context;
41}
42
Nick Kledzikf60a9272012-12-12 20:46:15 +000043//===----------------------------------------------------------------------===//
44// Input
45//===----------------------------------------------------------------------===//
46
Alexander Kornienko681e37c2013-11-18 15:50:04 +000047Input::Input(StringRef InputContent,
48 void *Ctxt,
49 SourceMgr::DiagHandlerTy DiagHandler,
50 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000051 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000052 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000053 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000054 if (DiagHandler)
55 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000056 DocIterator = Strm->begin();
57}
58
Nick Kledzik0dcef842013-01-08 21:04:44 +000059Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000060}
61
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000062std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000063
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000064// Pin the vtables to this file.
65void Input::HNode::anchor() {}
66void Input::EmptyHNode::anchor() {}
67void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000068void Input::MapHNode::anchor() {}
69void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000070
Nick Kledzik4761c602013-11-21 00:20:10 +000071bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000072 return false;
73}
74
75bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000076 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000077 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000078 if (!N) {
Alex Lorenz7a38d752015-05-12 17:44:32 +000079 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000080 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000081 return false;
82 }
83
Benjamin Kramer36b0f122012-12-12 22:40:02 +000084 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000085 // Empty files are allowed and ignored
86 ++DocIterator;
87 return setCurrentDocument();
88 }
David Blaikied759fe52014-09-15 18:39:24 +000089 TopNode = this->createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +000090 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000091 return true;
92 }
93 return false;
94}
95
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000096bool Input::nextDocument() {
97 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000098}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000099
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000100const Node *Input::getCurrentNode() const {
101 return CurrentNode ? CurrentNode->_node : nullptr;
102}
103
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000104bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000105 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000106 if (foundTag.empty()) {
107 // If no tag found and 'Tag' is the default, say it was found.
108 return Default;
109 }
Alex Lorenz7a38d752015-05-12 17:44:32 +0000110 // Return true iff found tag matches supplied tag.
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000111 return Tag.equals(foundTag);
112}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000113
114void Input::beginMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000115 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000116 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000117 // CurrentNode can be null if the document is empty.
118 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000119 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000120 MN->ValidKeys.clear();
121 }
122}
123
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000124bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
125 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000126 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000127 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000128 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000129
130 // CurrentNode is null for empty documents, which is an error in case required
131 // nodes are present.
132 if (!CurrentNode) {
133 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000134 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000135 return false;
136 }
137
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000138 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
139 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000140 setError(CurrentNode, "not a mapping");
141 return false;
142 }
143 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000144 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000145 if (!Value) {
146 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000147 setError(CurrentNode, Twine("missing required key '") + Key + "'");
148 else
149 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000150 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151 }
152 SaveInfo = CurrentNode;
153 CurrentNode = Value;
154 return true;
155}
156
157void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000158 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000159}
160
161void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000162 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000163 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000164 // CurrentNode can be null if the document is empty.
165 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000166 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000167 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000168 for (const auto &NN : MN->Mapping) {
169 if (!MN->isValidKey(NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000170 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000171 break;
172 }
173 }
174}
175
Alex Lorenzb1225082015-05-04 20:11:40 +0000176void Input::beginFlowMapping() { beginMapping(); }
177
178void Input::endFlowMapping() { endMapping(); }
179
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000181 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000183 if (isa<EmptyHNode>(CurrentNode))
184 return 0;
185 // Treat case where there's a scalar "null" value as an empty sequence.
186 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
187 if (isNull(SN->value()))
188 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000189 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000190 // Any other type of HNode is an error.
191 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192 return 0;
193}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195void Input::endSequence() {
196}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000197
Nick Kledzikf60a9272012-12-12 20:46:15 +0000198bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000199 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000200 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000201 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000202 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000203 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204 return true;
205 }
206 return false;
207}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000208
Nick Kledzikf60a9272012-12-12 20:46:15 +0000209void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000210 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211}
212
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000213unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000214
Nick Kledzikf60a9272012-12-12 20:46:15 +0000215bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000216 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000217 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000218 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000219 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000220 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000221 return true;
222 }
223 return false;
224}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000227 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000228}
229
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000230void Input::endFlowSequence() {
231}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000232
233void Input::beginEnumScalar() {
234 ScalarMatchFound = false;
235}
236
237bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000238 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000239 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000240 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
241 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000242 ScalarMatchFound = true;
243 return true;
244 }
245 }
246 return false;
247}
248
Michael J. Spencer731cae32015-01-23 21:57:50 +0000249bool Input::matchEnumFallback() {
250 if (ScalarMatchFound)
251 return false;
252 ScalarMatchFound = true;
253 return true;
254}
255
Nick Kledzikf60a9272012-12-12 20:46:15 +0000256void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000257 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000258 setError(CurrentNode, "unknown enumerated scalar");
259 }
260}
261
Nick Kledzikf60a9272012-12-12 20:46:15 +0000262bool Input::beginBitSetScalar(bool &DoClear) {
263 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000264 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000265 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000266 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000267 setError(CurrentNode, "expected sequence of bit values");
268 }
269 DoClear = true;
270 return true;
271}
272
273bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000274 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000275 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000276 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000277 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000278 for (auto &N : SQ->Entries) {
279 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000280 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000281 BitValuesUsed[Index] = true;
282 return true;
283 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000284 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000285 setError(CurrentNode, "unexpected scalar in sequence of bit values");
286 }
287 ++Index;
288 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000289 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290 setError(CurrentNode, "expected sequence of bit values");
291 }
292 return false;
293}
294
295void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000296 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000297 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000298 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000299 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000300 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
301 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000302 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000303 return;
304 }
305 }
306 }
307}
308
David Majnemer77880332014-04-10 07:37:33 +0000309void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000310 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000311 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000312 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000313 setError(CurrentNode, "unexpected scalar");
314 }
315}
316
Alex Lorenz68e787b2015-05-14 23:08:22 +0000317void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
318
Nick Kledzikf60a9272012-12-12 20:46:15 +0000319void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000320 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000321 this->setError(hnode->_node, message);
322}
323
324void Input::setError(Node *node, const Twine &message) {
325 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000326 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000327}
328
David Blaikied759fe52014-09-15 18:39:24 +0000329std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000330 SmallString<128> StringStorage;
331 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000332 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000333 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000334 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000335 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336 }
David Blaikied759fe52014-09-15 18:39:24 +0000337 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000338 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000339 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
340 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000341 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000342 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000343 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000344 auto Entry = this->createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000345 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000346 break;
David Blaikied759fe52014-09-15 18:39:24 +0000347 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000348 }
David Blaikied759fe52014-09-15 18:39:24 +0000349 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000350 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000351 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000352 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000353 Node *KeyNode = KVN.getKey();
354 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
355 if (!KeyScalar) {
356 setError(KeyNode, "Map key must be a scalar");
357 break;
358 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000359 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000360 StringRef KeyStr = KeyScalar->getValue(StringStorage);
361 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000362 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000363 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000364 }
David Blaikied759fe52014-09-15 18:39:24 +0000365 auto ValueHNode = this->createHNodes(KVN.getValue());
Mehdi Amini43c24282016-11-28 04:57:04 +0000366 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000367 break;
David Blaikied759fe52014-09-15 18:39:24 +0000368 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000369 }
David Blaikied759fe52014-09-15 18:39:24 +0000370 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000371 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000372 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000373 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000374 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000375 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000376 }
377}
378
Nick Kledzikf60a9272012-12-12 20:46:15 +0000379bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000380 for (const char *K : ValidKeys) {
381 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000382 return true;
383 }
384 return false;
385}
386
387void Input::setError(const Twine &Message) {
388 this->setError(CurrentNode, Message);
389}
390
Aaron Ballman0e63e532013-08-15 23:17:53 +0000391bool Input::canElideEmptySequence() {
392 return false;
393}
394
Nick Kledzikf60a9272012-12-12 20:46:15 +0000395//===----------------------------------------------------------------------===//
396// Output
397//===----------------------------------------------------------------------===//
398
Frederic Riss4939e6a2015-05-29 17:56:28 +0000399Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000400 : IO(context),
401 Out(yout),
Frederic Riss4939e6a2015-05-29 17:56:28 +0000402 WrapColumn(WrapColumn),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000403 Column(0),
404 ColumnAtFlowStart(0),
Alex Lorenzb1225082015-05-04 20:11:40 +0000405 ColumnAtMapFlowStart(0),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000406 NeedBitValueComma(false),
407 NeedFlowSequenceComma(false),
408 EnumerationMatchFound(false),
409 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000410}
411
412Output::~Output() {
413}
414
Nick Kledzik4761c602013-11-21 00:20:10 +0000415bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000416 return true;
417}
418
419void Output::beginMapping() {
420 StateStack.push_back(inMapFirstKey);
421 NeedsNewLine = true;
422}
423
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000424bool Output::mapTag(StringRef Tag, bool Use) {
425 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000426 // If this tag is being written inside a sequence we should write the start
427 // of the sequence before writing the tag, otherwise the tag won't be
428 // attached to the element in the sequence, but rather the sequence itself.
429 bool SequenceElement =
430 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
431 StateStack[StateStack.size() - 2] == inFlowSeq);
432 if (SequenceElement && StateStack.back() == inMapFirstKey) {
433 this->newLineCheck();
434 } else {
435 this->output(" ");
436 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000437 this->output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000438 if (SequenceElement) {
439 // If we're writing the tag during the first element of a map, the tag
440 // takes the place of the first element in the sequence.
441 if (StateStack.back() == inMapFirstKey) {
442 StateStack.pop_back();
443 StateStack.push_back(inMapOtherKey);
444 }
445 // Tags inside maps in sequences should act as keys in the map from a
446 // formatting perspective, so we always want a newline in a sequence.
447 NeedsNewLine = true;
448 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000449 }
450 return Use;
451}
452
Nick Kledzikf60a9272012-12-12 20:46:15 +0000453void Output::endMapping() {
454 StateStack.pop_back();
455}
456
Nick Kledzikf60a9272012-12-12 20:46:15 +0000457bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000458 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000459 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000460 if (Required || !SameAsDefault) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000461 auto State = StateStack.back();
462 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
463 flowKey(Key);
464 } else {
465 this->newLineCheck();
466 this->paddedKey(Key);
467 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000468 return true;
469 }
470 return false;
471}
472
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000473void Output::postflightKey(void *) {
474 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000475 StateStack.pop_back();
476 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000477 } else if (StateStack.back() == inFlowMapFirstKey) {
478 StateStack.pop_back();
479 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000480 }
481}
482
Alex Lorenzb1225082015-05-04 20:11:40 +0000483void Output::beginFlowMapping() {
484 StateStack.push_back(inFlowMapFirstKey);
485 this->newLineCheck();
486 ColumnAtMapFlowStart = Column;
487 output("{ ");
488}
489
490void Output::endFlowMapping() {
491 StateStack.pop_back();
492 this->outputUpToEndOfLine(" }");
493}
494
Nick Kledzikf60a9272012-12-12 20:46:15 +0000495void Output::beginDocuments() {
496 this->outputUpToEndOfLine("---");
497}
498
499bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000500 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000501 this->outputUpToEndOfLine("\n---");
502 return true;
503}
504
505void Output::postflightDocument() {
506}
507
508void Output::endDocuments() {
509 output("\n...\n");
510}
511
512unsigned Output::beginSequence() {
513 StateStack.push_back(inSeq);
514 NeedsNewLine = true;
515 return 0;
516}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000517
Nick Kledzikf60a9272012-12-12 20:46:15 +0000518void Output::endSequence() {
519 StateStack.pop_back();
520}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000521
522bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000523 return true;
524}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000525
526void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000527}
528
529unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000530 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000531 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000532 ColumnAtFlowStart = Column;
533 output("[ ");
534 NeedFlowSequenceComma = false;
535 return 0;
536}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000537
Nick Kledzikf60a9272012-12-12 20:46:15 +0000538void Output::endFlowSequence() {
539 StateStack.pop_back();
540 this->outputUpToEndOfLine(" ]");
541}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000542
543bool Output::preflightFlowElement(unsigned, void *&) {
544 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000545 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000546 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000547 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000548 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000549 output(" ");
550 Column = ColumnAtFlowStart;
551 output(" ");
552 }
553 return true;
554}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000555
556void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000557 NeedFlowSequenceComma = true;
558}
559
Nick Kledzikf60a9272012-12-12 20:46:15 +0000560void Output::beginEnumScalar() {
561 EnumerationMatchFound = false;
562}
563
564bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000565 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000566 this->newLineCheck();
567 this->outputUpToEndOfLine(Str);
568 EnumerationMatchFound = true;
569 }
570 return false;
571}
572
Michael J. Spencer731cae32015-01-23 21:57:50 +0000573bool Output::matchEnumFallback() {
574 if (EnumerationMatchFound)
575 return false;
576 EnumerationMatchFound = true;
577 return true;
578}
579
Nick Kledzikf60a9272012-12-12 20:46:15 +0000580void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000581 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000582 llvm_unreachable("bad runtime enum value");
583}
584
Nick Kledzikf60a9272012-12-12 20:46:15 +0000585bool Output::beginBitSetScalar(bool &DoClear) {
586 this->newLineCheck();
587 output("[ ");
588 NeedBitValueComma = false;
589 DoClear = false;
590 return true;
591}
592
593bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000594 if (Matches) {
595 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000596 output(", ");
597 this->output(Str);
598 NeedBitValueComma = true;
599 }
600 return false;
601}
602
603void Output::endBitSetScalar() {
604 this->outputUpToEndOfLine(" ]");
605}
606
David Majnemer77880332014-04-10 07:37:33 +0000607void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000608 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000609 if (S.empty()) {
610 // Print '' for the empty string because leaving the field empty is not
611 // allowed.
612 this->outputUpToEndOfLine("''");
613 return;
614 }
David Majnemer77880332014-04-10 07:37:33 +0000615 if (!MustQuote) {
616 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000617 this->outputUpToEndOfLine(S);
618 return;
619 }
620 unsigned i = 0;
621 unsigned j = 0;
622 unsigned End = S.size();
623 output("'"); // Starting single quote.
624 const char *Base = S.data();
625 while (j < End) {
626 // Escape a single quote by doubling it.
627 if (S[j] == '\'') {
628 output(StringRef(&Base[i], j - i + 1));
629 output("'");
630 i = j + 1;
631 }
632 ++j;
633 }
634 output(StringRef(&Base[i], j - i));
635 this->outputUpToEndOfLine("'"); // Ending single quote.
636}
637
Alex Lorenz68e787b2015-05-14 23:08:22 +0000638void Output::blockScalarString(StringRef &S) {
639 if (!StateStack.empty())
640 newLineCheck();
641 output(" |");
642 outputNewLine();
643
644 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
645
646 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
647 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
648 for (unsigned I = 0; I < Indent; ++I) {
649 output(" ");
650 }
651 output(*Lines);
652 outputNewLine();
653 }
654}
655
Nick Kledzikf60a9272012-12-12 20:46:15 +0000656void Output::setError(const Twine &message) {
657}
658
Aaron Ballman0e63e532013-08-15 23:17:53 +0000659bool Output::canElideEmptySequence() {
660 // Normally, with an optional key/value where the value is an empty sequence,
661 // the whole key/value can be not written. But, that produces wrong yaml
662 // if the key/value is the only thing in the map and the map is used in
663 // a sequence. This detects if the this sequence is the first key/value
664 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000665 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000666 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000667 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000668 return true;
669 return (StateStack[StateStack.size()-2] != inSeq);
670}
671
Nick Kledzikf60a9272012-12-12 20:46:15 +0000672void Output::output(StringRef s) {
673 Column += s.size();
674 Out << s;
675}
676
677void Output::outputUpToEndOfLine(StringRef s) {
678 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000679 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
680 StateStack.back() != inFlowMapFirstKey &&
681 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000682 NeedsNewLine = true;
683}
684
685void Output::outputNewLine() {
686 Out << "\n";
687 Column = 0;
688}
689
690// if seq at top, indent as if map, then add "- "
691// if seq in middle, use "- " if firstKey, else use " "
692//
693
694void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000695 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000696 return;
697 NeedsNewLine = false;
698
699 this->outputNewLine();
700
701 assert(StateStack.size() > 0);
702 unsigned Indent = StateStack.size() - 1;
703 bool OutputDash = false;
704
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000705 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000706 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000707 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000708 (StateStack.back() == inFlowSeq) ||
709 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000710 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000711 --Indent;
712 OutputDash = true;
713 }
714
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000715 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000716 output(" ");
717 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000718 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000719 output("- ");
720 }
721
722}
723
724void Output::paddedKey(StringRef key) {
725 output(key);
726 output(":");
727 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000728 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000729 output(&spaces[key.size()]);
730 else
731 output(" ");
732}
733
Alex Lorenzb1225082015-05-04 20:11:40 +0000734void Output::flowKey(StringRef Key) {
735 if (StateStack.back() == inFlowMapOtherKey)
736 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000737 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000738 output("\n");
739 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
740 output(" ");
741 Column = ColumnAtMapFlowStart;
742 output(" ");
743 }
744 output(Key);
745 output(": ");
746}
747
Nick Kledzikf60a9272012-12-12 20:46:15 +0000748//===----------------------------------------------------------------------===//
749// traits for built-in types
750//===----------------------------------------------------------------------===//
751
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000752void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
753 Out << (Val ? "true" : "false");
754}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000755
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000756StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
757 if (Scalar.equals("true")) {
758 Val = true;
759 return StringRef();
760 } else if (Scalar.equals("false")) {
761 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000762 return StringRef();
763 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000764 return "invalid boolean";
765}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000766
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000767void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
768 raw_ostream &Out) {
769 Out << Val;
770}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000771
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000772StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
773 StringRef &Val) {
774 Val = Scalar;
775 return StringRef();
776}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000777
John Thompson48e018a2013-11-19 17:28:21 +0000778void ScalarTraits<std::string>::output(const std::string &Val, void *,
779 raw_ostream &Out) {
780 Out << Val;
781}
782
783StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
784 std::string &Val) {
785 Val = Scalar.str();
786 return StringRef();
787}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000788
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000789void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
790 raw_ostream &Out) {
791 // use temp uin32_t because ostream thinks uint8_t is a character
792 uint32_t Num = Val;
793 Out << Num;
794}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000795
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000796StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
797 unsigned long long n;
798 if (getAsUnsignedInteger(Scalar, 0, n))
799 return "invalid number";
800 if (n > 0xFF)
801 return "out of range number";
802 Val = n;
803 return StringRef();
804}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000805
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000806void ScalarTraits<uint16_t>::output(const uint16_t &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<uint16_t>::input(StringRef Scalar, void *,
812 uint16_t &Val) {
813 unsigned long long n;
814 if (getAsUnsignedInteger(Scalar, 0, n))
815 return "invalid number";
816 if (n > 0xFFFF)
817 return "out of range number";
818 Val = n;
819 return StringRef();
820}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000821
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000822void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
823 raw_ostream &Out) {
824 Out << Val;
825}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000826
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000827StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
828 uint32_t &Val) {
829 unsigned long long n;
830 if (getAsUnsignedInteger(Scalar, 0, n))
831 return "invalid number";
832 if (n > 0xFFFFFFFFUL)
833 return "out of range number";
834 Val = n;
835 return StringRef();
836}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000837
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000838void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
839 raw_ostream &Out) {
840 Out << Val;
841}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000842
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000843StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
844 uint64_t &Val) {
845 unsigned long long N;
846 if (getAsUnsignedInteger(Scalar, 0, N))
847 return "invalid number";
848 Val = N;
849 return StringRef();
850}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000852void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
853 // use temp in32_t because ostream thinks int8_t is a character
854 int32_t Num = Val;
855 Out << Num;
856}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000857
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000858StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
859 long long N;
860 if (getAsSignedInteger(Scalar, 0, N))
861 return "invalid number";
862 if ((N > 127) || (N < -128))
863 return "out of range number";
864 Val = N;
865 return StringRef();
866}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000867
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000868void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
869 raw_ostream &Out) {
870 Out << Val;
871}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000872
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000873StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
874 long long N;
875 if (getAsSignedInteger(Scalar, 0, N))
876 return "invalid number";
877 if ((N > INT16_MAX) || (N < INT16_MIN))
878 return "out of range number";
879 Val = N;
880 return StringRef();
881}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000882
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000883void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
884 raw_ostream &Out) {
885 Out << Val;
886}
887
888StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
889 long long N;
890 if (getAsSignedInteger(Scalar, 0, N))
891 return "invalid number";
892 if ((N > INT32_MAX) || (N < INT32_MIN))
893 return "out of range number";
894 Val = N;
895 return StringRef();
896}
897
898void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
899 raw_ostream &Out) {
900 Out << Val;
901}
902
903StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
904 long long N;
905 if (getAsSignedInteger(Scalar, 0, N))
906 return "invalid number";
907 Val = N;
908 return StringRef();
909}
910
911void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000912 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000913}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000914
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000915StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
916 SmallString<32> buff(Scalar.begin(), Scalar.end());
917 char *end;
918 Val = strtod(buff.c_str(), &end);
919 if (*end != '\0')
920 return "invalid floating point number";
921 return StringRef();
922}
923
924void ScalarTraits<float>::output(const float &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<float>::input(StringRef Scalar, void *, float &Val) {
929 SmallString<32> buff(Scalar.begin(), Scalar.end());
930 char *end;
931 Val = strtod(buff.c_str(), &end);
932 if (*end != '\0')
933 return "invalid floating point number";
934 return StringRef();
935}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000936
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000937void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
938 uint8_t Num = Val;
939 Out << format("0x%02X", Num);
940}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000941
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000942StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
943 unsigned long long n;
944 if (getAsUnsignedInteger(Scalar, 0, n))
945 return "invalid hex8 number";
946 if (n > 0xFF)
947 return "out of range hex8 number";
948 Val = n;
949 return StringRef();
950}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000951
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000952void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
953 uint16_t Num = Val;
954 Out << format("0x%04X", Num);
955}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000956
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000957StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
958 unsigned long long n;
959 if (getAsUnsignedInteger(Scalar, 0, n))
960 return "invalid hex16 number";
961 if (n > 0xFFFF)
962 return "out of range hex16 number";
963 Val = n;
964 return StringRef();
965}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000966
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000967void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
968 uint32_t Num = Val;
969 Out << format("0x%08X", Num);
970}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000971
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000972StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
973 unsigned long long n;
974 if (getAsUnsignedInteger(Scalar, 0, n))
975 return "invalid hex32 number";
976 if (n > 0xFFFFFFFFUL)
977 return "out of range hex32 number";
978 Val = n;
979 return StringRef();
980}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000981
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000982void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
983 uint64_t Num = Val;
984 Out << format("0x%016llX", Num);
985}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000986
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000987StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
988 unsigned long long Num;
989 if (getAsUnsignedInteger(Scalar, 0, Num))
990 return "invalid hex64 number";
991 Val = Num;
992 return StringRef();
993}