blob: 0d47f37edbddfbf6aa516c256a66ece814713f48 [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() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +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;
Benjamin Kramer36b0f122012-12-12 22:40:02 +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() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +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) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +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) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +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) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +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() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +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
335 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000336 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000337 memcpy(Buf, &StringStorage[0], Len);
338 KeyStr = StringRef(Buf, Len);
339 }
David Blaikied759fe52014-09-15 18:39:24 +0000340 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000341 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
342 StringRef Value = BSN->getValue();
343 char *Buf = StringAllocator.Allocate<char>(Value.size());
344 memcpy(Buf, Value.data(), Value.size());
345 return llvm::make_unique<ScalarHNode>(N, StringRef(Buf, Value.size()));
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000346 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000347 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000348 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000349 auto Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000350 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000351 break;
David Blaikied759fe52014-09-15 18:39:24 +0000352 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000353 }
David Blaikied759fe52014-09-15 18:39:24 +0000354 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000355 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000356 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000357 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000358 Node *KeyNode = KVN.getKey();
359 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
360 if (!KeyScalar) {
361 setError(KeyNode, "Map key must be a scalar");
362 break;
363 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000364 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000365 StringRef KeyStr = KeyScalar->getValue(StringStorage);
366 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000367 // Copy string to permanent storage
368 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000369 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000370 memcpy(Buf, &StringStorage[0], Len);
371 KeyStr = StringRef(Buf, Len);
372 }
David Blaikied759fe52014-09-15 18:39:24 +0000373 auto ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000374 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000375 break;
David Blaikied759fe52014-09-15 18:39:24 +0000376 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000377 }
David Blaikied759fe52014-09-15 18:39:24 +0000378 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000379 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000380 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000381 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000382 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000383 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000384 }
385}
386
Nick Kledzikf60a9272012-12-12 20:46:15 +0000387bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000388 for (const char *K : ValidKeys) {
389 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000390 return true;
391 }
392 return false;
393}
394
395void Input::setError(const Twine &Message) {
396 this->setError(CurrentNode, Message);
397}
398
Aaron Ballman0e63e532013-08-15 23:17:53 +0000399bool Input::canElideEmptySequence() {
400 return false;
401}
402
Nick Kledzikf60a9272012-12-12 20:46:15 +0000403//===----------------------------------------------------------------------===//
404// Output
405//===----------------------------------------------------------------------===//
406
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000407Output::Output(raw_ostream &yout, void *context)
408 : IO(context),
409 Out(yout),
410 Column(0),
411 ColumnAtFlowStart(0),
Alex Lorenzb1225082015-05-04 20:11:40 +0000412 ColumnAtMapFlowStart(0),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000413 NeedBitValueComma(false),
414 NeedFlowSequenceComma(false),
415 EnumerationMatchFound(false),
416 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000417}
418
419Output::~Output() {
420}
421
Nick Kledzik4761c602013-11-21 00:20:10 +0000422bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000423 return true;
424}
425
426void Output::beginMapping() {
427 StateStack.push_back(inMapFirstKey);
428 NeedsNewLine = true;
429}
430
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000431bool Output::mapTag(StringRef Tag, bool Use) {
432 if (Use) {
433 this->output(" ");
434 this->output(Tag);
435 }
436 return Use;
437}
438
Nick Kledzikf60a9272012-12-12 20:46:15 +0000439void Output::endMapping() {
440 StateStack.pop_back();
441}
442
Nick Kledzikf60a9272012-12-12 20:46:15 +0000443bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000444 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000445 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000446 if (Required || !SameAsDefault) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000447 auto State = StateStack.back();
448 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
449 flowKey(Key);
450 } else {
451 this->newLineCheck();
452 this->paddedKey(Key);
453 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000454 return true;
455 }
456 return false;
457}
458
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000459void Output::postflightKey(void *) {
460 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000461 StateStack.pop_back();
462 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000463 } else if (StateStack.back() == inFlowMapFirstKey) {
464 StateStack.pop_back();
465 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000466 }
467}
468
Alex Lorenzb1225082015-05-04 20:11:40 +0000469void Output::beginFlowMapping() {
470 StateStack.push_back(inFlowMapFirstKey);
471 this->newLineCheck();
472 ColumnAtMapFlowStart = Column;
473 output("{ ");
474}
475
476void Output::endFlowMapping() {
477 StateStack.pop_back();
478 this->outputUpToEndOfLine(" }");
479}
480
Nick Kledzikf60a9272012-12-12 20:46:15 +0000481void Output::beginDocuments() {
482 this->outputUpToEndOfLine("---");
483}
484
485bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000486 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000487 this->outputUpToEndOfLine("\n---");
488 return true;
489}
490
491void Output::postflightDocument() {
492}
493
494void Output::endDocuments() {
495 output("\n...\n");
496}
497
498unsigned Output::beginSequence() {
499 StateStack.push_back(inSeq);
500 NeedsNewLine = true;
501 return 0;
502}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000503
Nick Kledzikf60a9272012-12-12 20:46:15 +0000504void Output::endSequence() {
505 StateStack.pop_back();
506}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000507
508bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000509 return true;
510}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000511
512void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000513}
514
515unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000516 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000517 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000518 ColumnAtFlowStart = Column;
519 output("[ ");
520 NeedFlowSequenceComma = false;
521 return 0;
522}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000523
Nick Kledzikf60a9272012-12-12 20:46:15 +0000524void Output::endFlowSequence() {
525 StateStack.pop_back();
526 this->outputUpToEndOfLine(" ]");
527}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000528
529bool Output::preflightFlowElement(unsigned, void *&) {
530 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000531 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000532 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000533 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000534 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000535 output(" ");
536 Column = ColumnAtFlowStart;
537 output(" ");
538 }
539 return true;
540}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000541
542void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000543 NeedFlowSequenceComma = true;
544}
545
Nick Kledzikf60a9272012-12-12 20:46:15 +0000546void Output::beginEnumScalar() {
547 EnumerationMatchFound = false;
548}
549
550bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000551 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000552 this->newLineCheck();
553 this->outputUpToEndOfLine(Str);
554 EnumerationMatchFound = true;
555 }
556 return false;
557}
558
Michael J. Spencer731cae32015-01-23 21:57:50 +0000559bool Output::matchEnumFallback() {
560 if (EnumerationMatchFound)
561 return false;
562 EnumerationMatchFound = true;
563 return true;
564}
565
Nick Kledzikf60a9272012-12-12 20:46:15 +0000566void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000567 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000568 llvm_unreachable("bad runtime enum value");
569}
570
Nick Kledzikf60a9272012-12-12 20:46:15 +0000571bool Output::beginBitSetScalar(bool &DoClear) {
572 this->newLineCheck();
573 output("[ ");
574 NeedBitValueComma = false;
575 DoClear = false;
576 return true;
577}
578
579bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000580 if (Matches) {
581 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000582 output(", ");
583 this->output(Str);
584 NeedBitValueComma = true;
585 }
586 return false;
587}
588
589void Output::endBitSetScalar() {
590 this->outputUpToEndOfLine(" ]");
591}
592
David Majnemer77880332014-04-10 07:37:33 +0000593void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000594 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000595 if (S.empty()) {
596 // Print '' for the empty string because leaving the field empty is not
597 // allowed.
598 this->outputUpToEndOfLine("''");
599 return;
600 }
David Majnemer77880332014-04-10 07:37:33 +0000601 if (!MustQuote) {
602 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000603 this->outputUpToEndOfLine(S);
604 return;
605 }
606 unsigned i = 0;
607 unsigned j = 0;
608 unsigned End = S.size();
609 output("'"); // Starting single quote.
610 const char *Base = S.data();
611 while (j < End) {
612 // Escape a single quote by doubling it.
613 if (S[j] == '\'') {
614 output(StringRef(&Base[i], j - i + 1));
615 output("'");
616 i = j + 1;
617 }
618 ++j;
619 }
620 output(StringRef(&Base[i], j - i));
621 this->outputUpToEndOfLine("'"); // Ending single quote.
622}
623
Alex Lorenz68e787b2015-05-14 23:08:22 +0000624void Output::blockScalarString(StringRef &S) {
625 if (!StateStack.empty())
626 newLineCheck();
627 output(" |");
628 outputNewLine();
629
630 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
631
632 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
633 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
634 for (unsigned I = 0; I < Indent; ++I) {
635 output(" ");
636 }
637 output(*Lines);
638 outputNewLine();
639 }
640}
641
Nick Kledzikf60a9272012-12-12 20:46:15 +0000642void Output::setError(const Twine &message) {
643}
644
Aaron Ballman0e63e532013-08-15 23:17:53 +0000645bool Output::canElideEmptySequence() {
646 // Normally, with an optional key/value where the value is an empty sequence,
647 // the whole key/value can be not written. But, that produces wrong yaml
648 // if the key/value is the only thing in the map and the map is used in
649 // a sequence. This detects if the this sequence is the first key/value
650 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000651 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000652 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000653 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000654 return true;
655 return (StateStack[StateStack.size()-2] != inSeq);
656}
657
Nick Kledzikf60a9272012-12-12 20:46:15 +0000658void Output::output(StringRef s) {
659 Column += s.size();
660 Out << s;
661}
662
663void Output::outputUpToEndOfLine(StringRef s) {
664 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000665 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
666 StateStack.back() != inFlowMapFirstKey &&
667 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000668 NeedsNewLine = true;
669}
670
671void Output::outputNewLine() {
672 Out << "\n";
673 Column = 0;
674}
675
676// if seq at top, indent as if map, then add "- "
677// if seq in middle, use "- " if firstKey, else use " "
678//
679
680void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000681 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000682 return;
683 NeedsNewLine = false;
684
685 this->outputNewLine();
686
687 assert(StateStack.size() > 0);
688 unsigned Indent = StateStack.size() - 1;
689 bool OutputDash = false;
690
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000691 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000692 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000693 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000694 (StateStack.back() == inFlowSeq) ||
695 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000696 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000697 --Indent;
698 OutputDash = true;
699 }
700
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000701 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000702 output(" ");
703 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000704 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000705 output("- ");
706 }
707
708}
709
710void Output::paddedKey(StringRef key) {
711 output(key);
712 output(":");
713 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000714 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000715 output(&spaces[key.size()]);
716 else
717 output(" ");
718}
719
Alex Lorenzb1225082015-05-04 20:11:40 +0000720void Output::flowKey(StringRef Key) {
721 if (StateStack.back() == inFlowMapOtherKey)
722 output(", ");
723 if (Column > 70) {
724 output("\n");
725 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
726 output(" ");
727 Column = ColumnAtMapFlowStart;
728 output(" ");
729 }
730 output(Key);
731 output(": ");
732}
733
Nick Kledzikf60a9272012-12-12 20:46:15 +0000734//===----------------------------------------------------------------------===//
735// traits for built-in types
736//===----------------------------------------------------------------------===//
737
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000738void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
739 Out << (Val ? "true" : "false");
740}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000741
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000742StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
743 if (Scalar.equals("true")) {
744 Val = true;
745 return StringRef();
746 } else if (Scalar.equals("false")) {
747 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000748 return StringRef();
749 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000750 return "invalid boolean";
751}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000752
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000753void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
754 raw_ostream &Out) {
755 Out << Val;
756}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000757
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000758StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
759 StringRef &Val) {
760 Val = Scalar;
761 return StringRef();
762}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000763
John Thompson48e018a2013-11-19 17:28:21 +0000764void ScalarTraits<std::string>::output(const std::string &Val, void *,
765 raw_ostream &Out) {
766 Out << Val;
767}
768
769StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
770 std::string &Val) {
771 Val = Scalar.str();
772 return StringRef();
773}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000774
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000775void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
776 raw_ostream &Out) {
777 // use temp uin32_t because ostream thinks uint8_t is a character
778 uint32_t Num = Val;
779 Out << Num;
780}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000781
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000782StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
783 unsigned long long n;
784 if (getAsUnsignedInteger(Scalar, 0, n))
785 return "invalid number";
786 if (n > 0xFF)
787 return "out of range number";
788 Val = n;
789 return StringRef();
790}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000791
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000792void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
793 raw_ostream &Out) {
794 Out << Val;
795}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000796
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000797StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
798 uint16_t &Val) {
799 unsigned long long n;
800 if (getAsUnsignedInteger(Scalar, 0, n))
801 return "invalid number";
802 if (n > 0xFFFF)
803 return "out of range number";
804 Val = n;
805 return StringRef();
806}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000807
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000808void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
809 raw_ostream &Out) {
810 Out << Val;
811}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000812
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000813StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
814 uint32_t &Val) {
815 unsigned long long n;
816 if (getAsUnsignedInteger(Scalar, 0, n))
817 return "invalid number";
818 if (n > 0xFFFFFFFFUL)
819 return "out of range number";
820 Val = n;
821 return StringRef();
822}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000823
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000824void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
825 raw_ostream &Out) {
826 Out << Val;
827}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000828
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000829StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
830 uint64_t &Val) {
831 unsigned long long N;
832 if (getAsUnsignedInteger(Scalar, 0, N))
833 return "invalid 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<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
839 // use temp in32_t because ostream thinks int8_t is a character
840 int32_t Num = Val;
841 Out << Num;
842}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000843
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000844StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
845 long long N;
846 if (getAsSignedInteger(Scalar, 0, N))
847 return "invalid number";
848 if ((N > 127) || (N < -128))
849 return "out of range number";
850 Val = N;
851 return StringRef();
852}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000853
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000854void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
855 raw_ostream &Out) {
856 Out << Val;
857}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000858
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000859StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
860 long long N;
861 if (getAsSignedInteger(Scalar, 0, N))
862 return "invalid number";
863 if ((N > INT16_MAX) || (N < INT16_MIN))
864 return "out of range number";
865 Val = N;
866 return StringRef();
867}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000868
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000869void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
870 raw_ostream &Out) {
871 Out << Val;
872}
873
874StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
875 long long N;
876 if (getAsSignedInteger(Scalar, 0, N))
877 return "invalid number";
878 if ((N > INT32_MAX) || (N < INT32_MIN))
879 return "out of range number";
880 Val = N;
881 return StringRef();
882}
883
884void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
885 raw_ostream &Out) {
886 Out << Val;
887}
888
889StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
890 long long N;
891 if (getAsSignedInteger(Scalar, 0, N))
892 return "invalid number";
893 Val = N;
894 return StringRef();
895}
896
897void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000898 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000899}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000900
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000901StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
902 SmallString<32> buff(Scalar.begin(), Scalar.end());
903 char *end;
904 Val = strtod(buff.c_str(), &end);
905 if (*end != '\0')
906 return "invalid floating point number";
907 return StringRef();
908}
909
910void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000911 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000912}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000913
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000914StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
915 SmallString<32> buff(Scalar.begin(), Scalar.end());
916 char *end;
917 Val = strtod(buff.c_str(), &end);
918 if (*end != '\0')
919 return "invalid floating point number";
920 return StringRef();
921}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000922
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000923void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
924 uint8_t Num = Val;
925 Out << format("0x%02X", Num);
926}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000927
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000928StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
929 unsigned long long n;
930 if (getAsUnsignedInteger(Scalar, 0, n))
931 return "invalid hex8 number";
932 if (n > 0xFF)
933 return "out of range hex8 number";
934 Val = n;
935 return StringRef();
936}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000937
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000938void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
939 uint16_t Num = Val;
940 Out << format("0x%04X", Num);
941}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000942
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000943StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
944 unsigned long long n;
945 if (getAsUnsignedInteger(Scalar, 0, n))
946 return "invalid hex16 number";
947 if (n > 0xFFFF)
948 return "out of range hex16 number";
949 Val = n;
950 return StringRef();
951}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000952
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000953void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
954 uint32_t Num = Val;
955 Out << format("0x%08X", Num);
956}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000957
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000958StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
959 unsigned long long n;
960 if (getAsUnsignedInteger(Scalar, 0, n))
961 return "invalid hex32 number";
962 if (n > 0xFFFFFFFFUL)
963 return "out of range hex32 number";
964 Val = n;
965 return StringRef();
966}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000967
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000968void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
969 uint64_t Num = Val;
970 Out << format("0x%016llX", Num);
971}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000972
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000973StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
974 unsigned long long Num;
975 if (getAsUnsignedInteger(Scalar, 0, Num))
976 return "invalid hex64 number";
977 Val = Num;
978 return StringRef();
979}