blob: 99d2070cb6ed7de0d1f25c28b0b539d593cb89d5 [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
Mehdi Amini3ab3fef2016-11-28 21:38:52 +000047Input::Input(StringRef InputContent, void *Ctxt,
48 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
49 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)),
50 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000051 if (DiagHandler)
52 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000053 DocIterator = Strm->begin();
54}
55
Nick Kledzik0dcef842013-01-08 21:04:44 +000056Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000057}
58
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000059std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000060
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000061// Pin the vtables to this file.
62void Input::HNode::anchor() {}
63void Input::EmptyHNode::anchor() {}
64void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000065void Input::MapHNode::anchor() {}
66void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000067
Nick Kledzik4761c602013-11-21 00:20:10 +000068bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000069 return false;
70}
71
72bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000073 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000074 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000075 if (!N) {
Alex Lorenz7a38d752015-05-12 17:44:32 +000076 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000077 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000078 return false;
79 }
80
Benjamin Kramer36b0f122012-12-12 22:40:02 +000081 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000082 // Empty files are allowed and ignored
83 ++DocIterator;
84 return setCurrentDocument();
85 }
David Blaikied759fe52014-09-15 18:39:24 +000086 TopNode = this->createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +000087 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000088 return true;
89 }
90 return false;
91}
92
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000093bool Input::nextDocument() {
94 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000095}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000096
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000097const Node *Input::getCurrentNode() const {
98 return CurrentNode ? CurrentNode->_node : nullptr;
99}
100
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000101bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000102 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000103 if (foundTag.empty()) {
104 // If no tag found and 'Tag' is the default, say it was found.
105 return Default;
106 }
Alex Lorenz7a38d752015-05-12 17:44:32 +0000107 // Return true iff found tag matches supplied tag.
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000108 return Tag.equals(foundTag);
109}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000110
111void Input::beginMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000112 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000113 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000114 // CurrentNode can be null if the document is empty.
115 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000116 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000117 MN->ValidKeys.clear();
118 }
119}
120
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000121bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
122 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000123 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000124 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000125 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000126
127 // CurrentNode is null for empty documents, which is an error in case required
128 // nodes are present.
129 if (!CurrentNode) {
130 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000131 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000132 return false;
133 }
134
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000135 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
136 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000137 setError(CurrentNode, "not a mapping");
138 return false;
139 }
140 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000141 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000142 if (!Value) {
143 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000144 setError(CurrentNode, Twine("missing required key '") + Key + "'");
145 else
146 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000147 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000148 }
149 SaveInfo = CurrentNode;
150 CurrentNode = Value;
151 return true;
152}
153
154void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000155 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000156}
157
158void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000159 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000160 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000161 // CurrentNode can be null if the document is empty.
162 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000163 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000164 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000165 for (const auto &NN : MN->Mapping) {
166 if (!MN->isValidKey(NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000167 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000168 break;
169 }
170 }
171}
172
Alex Lorenzb1225082015-05-04 20:11:40 +0000173void Input::beginFlowMapping() { beginMapping(); }
174
175void Input::endFlowMapping() { endMapping(); }
176
Nick Kledzikf60a9272012-12-12 20:46:15 +0000177unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000178 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000180 if (isa<EmptyHNode>(CurrentNode))
181 return 0;
182 // Treat case where there's a scalar "null" value as an empty sequence.
183 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
184 if (isNull(SN->value()))
185 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000186 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000187 // Any other type of HNode is an error.
188 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000189 return 0;
190}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192void Input::endSequence() {
193}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000196 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000197 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000198 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000199 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000200 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201 return true;
202 }
203 return false;
204}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000205
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000207 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000208}
209
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000210unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000211
Nick Kledzikf60a9272012-12-12 20:46:15 +0000212bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000213 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000214 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000215 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000216 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000217 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000218 return true;
219 }
220 return false;
221}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000222
Nick Kledzikf60a9272012-12-12 20:46:15 +0000223void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000224 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000225}
226
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000227void Input::endFlowSequence() {
228}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000229
230void Input::beginEnumScalar() {
231 ScalarMatchFound = false;
232}
233
234bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000235 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000236 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000237 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
238 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000239 ScalarMatchFound = true;
240 return true;
241 }
242 }
243 return false;
244}
245
Michael J. Spencer731cae32015-01-23 21:57:50 +0000246bool Input::matchEnumFallback() {
247 if (ScalarMatchFound)
248 return false;
249 ScalarMatchFound = true;
250 return true;
251}
252
Nick Kledzikf60a9272012-12-12 20:46:15 +0000253void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 setError(CurrentNode, "unknown enumerated scalar");
256 }
257}
258
Nick Kledzikf60a9272012-12-12 20:46:15 +0000259bool Input::beginBitSetScalar(bool &DoClear) {
260 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000261 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000262 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000263 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000264 setError(CurrentNode, "expected sequence of bit values");
265 }
266 DoClear = true;
267 return true;
268}
269
270bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000271 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000272 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000273 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000274 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000275 for (auto &N : SQ->Entries) {
276 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000277 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000278 BitValuesUsed[Index] = true;
279 return true;
280 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000281 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000282 setError(CurrentNode, "unexpected scalar in sequence of bit values");
283 }
284 ++Index;
285 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000286 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000287 setError(CurrentNode, "expected sequence of bit values");
288 }
289 return false;
290}
291
292void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000293 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000294 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000295 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000296 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000297 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
298 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000299 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000300 return;
301 }
302 }
303 }
304}
305
David Majnemer77880332014-04-10 07:37:33 +0000306void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000307 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000308 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000310 setError(CurrentNode, "unexpected scalar");
311 }
312}
313
Alex Lorenz68e787b2015-05-14 23:08:22 +0000314void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
315
Nick Kledzikf60a9272012-12-12 20:46:15 +0000316void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000317 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000318 this->setError(hnode->_node, message);
319}
320
321void Input::setError(Node *node, const Twine &message) {
322 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000323 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000324}
325
David Blaikied759fe52014-09-15 18:39:24 +0000326std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000327 SmallString<128> StringStorage;
328 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000329 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000330 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000331 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000332 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000333 }
David Blaikied759fe52014-09-15 18:39:24 +0000334 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000335 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000336 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
337 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000338 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000339 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000340 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000341 auto Entry = this->createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000342 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000343 break;
David Blaikied759fe52014-09-15 18:39:24 +0000344 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000345 }
David Blaikied759fe52014-09-15 18:39:24 +0000346 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000347 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000348 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000349 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000350 Node *KeyNode = KVN.getKey();
351 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
352 if (!KeyScalar) {
353 setError(KeyNode, "Map key must be a scalar");
354 break;
355 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000356 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000357 StringRef KeyStr = KeyScalar->getValue(StringStorage);
358 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 auto ValueHNode = this->createHNodes(KVN.getValue());
Mehdi Amini43c24282016-11-28 04:57:04 +0000363 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000364 break;
David Blaikied759fe52014-09-15 18:39:24 +0000365 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000366 }
David Blaikied759fe52014-09-15 18:39:24 +0000367 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000368 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000369 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000370 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000371 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000372 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000373 }
374}
375
Nick Kledzikf60a9272012-12-12 20:46:15 +0000376bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000377 for (const char *K : ValidKeys) {
378 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000379 return true;
380 }
381 return false;
382}
383
384void Input::setError(const Twine &Message) {
385 this->setError(CurrentNode, Message);
386}
387
Aaron Ballman0e63e532013-08-15 23:17:53 +0000388bool Input::canElideEmptySequence() {
389 return false;
390}
391
Nick Kledzikf60a9272012-12-12 20:46:15 +0000392//===----------------------------------------------------------------------===//
393// Output
394//===----------------------------------------------------------------------===//
395
Frederic Riss4939e6a2015-05-29 17:56:28 +0000396Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000397 : IO(context),
398 Out(yout),
Frederic Riss4939e6a2015-05-29 17:56:28 +0000399 WrapColumn(WrapColumn),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000400 Column(0),
401 ColumnAtFlowStart(0),
Alex Lorenzb1225082015-05-04 20:11:40 +0000402 ColumnAtMapFlowStart(0),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000403 NeedBitValueComma(false),
404 NeedFlowSequenceComma(false),
405 EnumerationMatchFound(false),
406 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000407}
408
409Output::~Output() {
410}
411
Nick Kledzik4761c602013-11-21 00:20:10 +0000412bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000413 return true;
414}
415
416void Output::beginMapping() {
417 StateStack.push_back(inMapFirstKey);
418 NeedsNewLine = true;
419}
420
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000421bool Output::mapTag(StringRef Tag, bool Use) {
422 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000423 // If this tag is being written inside a sequence we should write the start
424 // of the sequence before writing the tag, otherwise the tag won't be
425 // attached to the element in the sequence, but rather the sequence itself.
426 bool SequenceElement =
427 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
428 StateStack[StateStack.size() - 2] == inFlowSeq);
429 if (SequenceElement && StateStack.back() == inMapFirstKey) {
430 this->newLineCheck();
431 } else {
432 this->output(" ");
433 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000434 this->output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000435 if (SequenceElement) {
436 // If we're writing the tag during the first element of a map, the tag
437 // takes the place of the first element in the sequence.
438 if (StateStack.back() == inMapFirstKey) {
439 StateStack.pop_back();
440 StateStack.push_back(inMapOtherKey);
441 }
442 // Tags inside maps in sequences should act as keys in the map from a
443 // formatting perspective, so we always want a newline in a sequence.
444 NeedsNewLine = true;
445 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000446 }
447 return Use;
448}
449
Nick Kledzikf60a9272012-12-12 20:46:15 +0000450void Output::endMapping() {
451 StateStack.pop_back();
452}
453
Nick Kledzikf60a9272012-12-12 20:46:15 +0000454bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000455 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000456 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000457 if (Required || !SameAsDefault) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000458 auto State = StateStack.back();
459 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
460 flowKey(Key);
461 } else {
462 this->newLineCheck();
463 this->paddedKey(Key);
464 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000465 return true;
466 }
467 return false;
468}
469
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000470void Output::postflightKey(void *) {
471 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000472 StateStack.pop_back();
473 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000474 } else if (StateStack.back() == inFlowMapFirstKey) {
475 StateStack.pop_back();
476 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000477 }
478}
479
Alex Lorenzb1225082015-05-04 20:11:40 +0000480void Output::beginFlowMapping() {
481 StateStack.push_back(inFlowMapFirstKey);
482 this->newLineCheck();
483 ColumnAtMapFlowStart = Column;
484 output("{ ");
485}
486
487void Output::endFlowMapping() {
488 StateStack.pop_back();
489 this->outputUpToEndOfLine(" }");
490}
491
Nick Kledzikf60a9272012-12-12 20:46:15 +0000492void Output::beginDocuments() {
493 this->outputUpToEndOfLine("---");
494}
495
496bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000497 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000498 this->outputUpToEndOfLine("\n---");
499 return true;
500}
501
502void Output::postflightDocument() {
503}
504
505void Output::endDocuments() {
506 output("\n...\n");
507}
508
509unsigned Output::beginSequence() {
510 StateStack.push_back(inSeq);
511 NeedsNewLine = true;
512 return 0;
513}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000514
Nick Kledzikf60a9272012-12-12 20:46:15 +0000515void Output::endSequence() {
516 StateStack.pop_back();
517}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000518
519bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000520 return true;
521}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000522
523void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000524}
525
526unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000527 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000528 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000529 ColumnAtFlowStart = Column;
530 output("[ ");
531 NeedFlowSequenceComma = false;
532 return 0;
533}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000534
Nick Kledzikf60a9272012-12-12 20:46:15 +0000535void Output::endFlowSequence() {
536 StateStack.pop_back();
537 this->outputUpToEndOfLine(" ]");
538}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000539
540bool Output::preflightFlowElement(unsigned, void *&) {
541 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000542 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000543 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000544 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000545 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000546 output(" ");
547 Column = ColumnAtFlowStart;
548 output(" ");
549 }
550 return true;
551}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000552
553void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000554 NeedFlowSequenceComma = true;
555}
556
Nick Kledzikf60a9272012-12-12 20:46:15 +0000557void Output::beginEnumScalar() {
558 EnumerationMatchFound = false;
559}
560
561bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000562 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000563 this->newLineCheck();
564 this->outputUpToEndOfLine(Str);
565 EnumerationMatchFound = true;
566 }
567 return false;
568}
569
Michael J. Spencer731cae32015-01-23 21:57:50 +0000570bool Output::matchEnumFallback() {
571 if (EnumerationMatchFound)
572 return false;
573 EnumerationMatchFound = true;
574 return true;
575}
576
Nick Kledzikf60a9272012-12-12 20:46:15 +0000577void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000578 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000579 llvm_unreachable("bad runtime enum value");
580}
581
Nick Kledzikf60a9272012-12-12 20:46:15 +0000582bool Output::beginBitSetScalar(bool &DoClear) {
583 this->newLineCheck();
584 output("[ ");
585 NeedBitValueComma = false;
586 DoClear = false;
587 return true;
588}
589
590bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000591 if (Matches) {
592 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000593 output(", ");
594 this->output(Str);
595 NeedBitValueComma = true;
596 }
597 return false;
598}
599
600void Output::endBitSetScalar() {
601 this->outputUpToEndOfLine(" ]");
602}
603
David Majnemer77880332014-04-10 07:37:33 +0000604void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000605 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000606 if (S.empty()) {
607 // Print '' for the empty string because leaving the field empty is not
608 // allowed.
609 this->outputUpToEndOfLine("''");
610 return;
611 }
David Majnemer77880332014-04-10 07:37:33 +0000612 if (!MustQuote) {
613 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000614 this->outputUpToEndOfLine(S);
615 return;
616 }
617 unsigned i = 0;
618 unsigned j = 0;
619 unsigned End = S.size();
620 output("'"); // Starting single quote.
621 const char *Base = S.data();
622 while (j < End) {
623 // Escape a single quote by doubling it.
624 if (S[j] == '\'') {
625 output(StringRef(&Base[i], j - i + 1));
626 output("'");
627 i = j + 1;
628 }
629 ++j;
630 }
631 output(StringRef(&Base[i], j - i));
632 this->outputUpToEndOfLine("'"); // Ending single quote.
633}
634
Alex Lorenz68e787b2015-05-14 23:08:22 +0000635void Output::blockScalarString(StringRef &S) {
636 if (!StateStack.empty())
637 newLineCheck();
638 output(" |");
639 outputNewLine();
640
641 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
642
643 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
644 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
645 for (unsigned I = 0; I < Indent; ++I) {
646 output(" ");
647 }
648 output(*Lines);
649 outputNewLine();
650 }
651}
652
Nick Kledzikf60a9272012-12-12 20:46:15 +0000653void Output::setError(const Twine &message) {
654}
655
Aaron Ballman0e63e532013-08-15 23:17:53 +0000656bool Output::canElideEmptySequence() {
657 // Normally, with an optional key/value where the value is an empty sequence,
658 // the whole key/value can be not written. But, that produces wrong yaml
659 // if the key/value is the only thing in the map and the map is used in
660 // a sequence. This detects if the this sequence is the first key/value
661 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000662 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000663 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000664 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000665 return true;
666 return (StateStack[StateStack.size()-2] != inSeq);
667}
668
Nick Kledzikf60a9272012-12-12 20:46:15 +0000669void Output::output(StringRef s) {
670 Column += s.size();
671 Out << s;
672}
673
674void Output::outputUpToEndOfLine(StringRef s) {
675 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000676 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
677 StateStack.back() != inFlowMapFirstKey &&
678 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000679 NeedsNewLine = true;
680}
681
682void Output::outputNewLine() {
683 Out << "\n";
684 Column = 0;
685}
686
687// if seq at top, indent as if map, then add "- "
688// if seq in middle, use "- " if firstKey, else use " "
689//
690
691void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000692 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000693 return;
694 NeedsNewLine = false;
695
696 this->outputNewLine();
697
698 assert(StateStack.size() > 0);
699 unsigned Indent = StateStack.size() - 1;
700 bool OutputDash = false;
701
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000702 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000703 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000704 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000705 (StateStack.back() == inFlowSeq) ||
706 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000707 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000708 --Indent;
709 OutputDash = true;
710 }
711
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000712 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000713 output(" ");
714 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000715 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000716 output("- ");
717 }
718
719}
720
721void Output::paddedKey(StringRef key) {
722 output(key);
723 output(":");
724 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000725 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000726 output(&spaces[key.size()]);
727 else
728 output(" ");
729}
730
Alex Lorenzb1225082015-05-04 20:11:40 +0000731void Output::flowKey(StringRef Key) {
732 if (StateStack.back() == inFlowMapOtherKey)
733 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000734 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000735 output("\n");
736 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
737 output(" ");
738 Column = ColumnAtMapFlowStart;
739 output(" ");
740 }
741 output(Key);
742 output(": ");
743}
744
Nick Kledzikf60a9272012-12-12 20:46:15 +0000745//===----------------------------------------------------------------------===//
746// traits for built-in types
747//===----------------------------------------------------------------------===//
748
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000749void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
750 Out << (Val ? "true" : "false");
751}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000752
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000753StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
754 if (Scalar.equals("true")) {
755 Val = true;
756 return StringRef();
757 } else if (Scalar.equals("false")) {
758 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000759 return StringRef();
760 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000761 return "invalid boolean";
762}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000763
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000764void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
765 raw_ostream &Out) {
766 Out << Val;
767}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000768
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000769StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
770 StringRef &Val) {
771 Val = Scalar;
772 return StringRef();
773}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000774
John Thompson48e018a2013-11-19 17:28:21 +0000775void ScalarTraits<std::string>::output(const std::string &Val, void *,
776 raw_ostream &Out) {
777 Out << Val;
778}
779
780StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
781 std::string &Val) {
782 Val = Scalar.str();
783 return StringRef();
784}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000785
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000786void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
787 raw_ostream &Out) {
788 // use temp uin32_t because ostream thinks uint8_t is a character
789 uint32_t Num = Val;
790 Out << Num;
791}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000792
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000793StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
794 unsigned long long n;
795 if (getAsUnsignedInteger(Scalar, 0, n))
796 return "invalid number";
797 if (n > 0xFF)
798 return "out of range number";
799 Val = n;
800 return StringRef();
801}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000802
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000803void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
804 raw_ostream &Out) {
805 Out << Val;
806}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000807
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000808StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
809 uint16_t &Val) {
810 unsigned long long n;
811 if (getAsUnsignedInteger(Scalar, 0, n))
812 return "invalid number";
813 if (n > 0xFFFF)
814 return "out of range number";
815 Val = n;
816 return StringRef();
817}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000818
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000819void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
820 raw_ostream &Out) {
821 Out << Val;
822}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000823
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000824StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
825 uint32_t &Val) {
826 unsigned long long n;
827 if (getAsUnsignedInteger(Scalar, 0, n))
828 return "invalid number";
829 if (n > 0xFFFFFFFFUL)
830 return "out of range number";
831 Val = n;
832 return StringRef();
833}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000834
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000835void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
836 raw_ostream &Out) {
837 Out << Val;
838}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000839
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000840StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
841 uint64_t &Val) {
842 unsigned long long N;
843 if (getAsUnsignedInteger(Scalar, 0, N))
844 return "invalid number";
845 Val = N;
846 return StringRef();
847}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000848
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000849void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
850 // use temp in32_t because ostream thinks int8_t is a character
851 int32_t Num = Val;
852 Out << Num;
853}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000854
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000855StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
856 long long N;
857 if (getAsSignedInteger(Scalar, 0, N))
858 return "invalid number";
859 if ((N > 127) || (N < -128))
860 return "out of range number";
861 Val = N;
862 return StringRef();
863}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000864
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000865void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
866 raw_ostream &Out) {
867 Out << Val;
868}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000869
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000870StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
871 long long N;
872 if (getAsSignedInteger(Scalar, 0, N))
873 return "invalid number";
874 if ((N > INT16_MAX) || (N < INT16_MIN))
875 return "out of range number";
876 Val = N;
877 return StringRef();
878}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000879
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000880void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
881 raw_ostream &Out) {
882 Out << Val;
883}
884
885StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
886 long long N;
887 if (getAsSignedInteger(Scalar, 0, N))
888 return "invalid number";
889 if ((N > INT32_MAX) || (N < INT32_MIN))
890 return "out of range number";
891 Val = N;
892 return StringRef();
893}
894
895void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
896 raw_ostream &Out) {
897 Out << Val;
898}
899
900StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
901 long long N;
902 if (getAsSignedInteger(Scalar, 0, N))
903 return "invalid number";
904 Val = N;
905 return StringRef();
906}
907
908void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000909 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000910}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000911
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000912StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
913 SmallString<32> buff(Scalar.begin(), Scalar.end());
914 char *end;
915 Val = strtod(buff.c_str(), &end);
916 if (*end != '\0')
917 return "invalid floating point number";
918 return StringRef();
919}
920
921void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000922 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000923}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000924
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000925StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
926 SmallString<32> buff(Scalar.begin(), Scalar.end());
927 char *end;
928 Val = strtod(buff.c_str(), &end);
929 if (*end != '\0')
930 return "invalid floating point number";
931 return StringRef();
932}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000933
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000934void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
935 uint8_t Num = Val;
936 Out << format("0x%02X", Num);
937}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000938
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000939StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
940 unsigned long long n;
941 if (getAsUnsignedInteger(Scalar, 0, n))
942 return "invalid hex8 number";
943 if (n > 0xFF)
944 return "out of range hex8 number";
945 Val = n;
946 return StringRef();
947}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000948
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000949void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
950 uint16_t Num = Val;
951 Out << format("0x%04X", Num);
952}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000953
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000954StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
955 unsigned long long n;
956 if (getAsUnsignedInteger(Scalar, 0, n))
957 return "invalid hex16 number";
958 if (n > 0xFFFF)
959 return "out of range hex16 number";
960 Val = n;
961 return StringRef();
962}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000963
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000964void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
965 uint32_t Num = Val;
966 Out << format("0x%08X", Num);
967}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000968
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000969StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
970 unsigned long long n;
971 if (getAsUnsignedInteger(Scalar, 0, n))
972 return "invalid hex32 number";
973 if (n > 0xFFFFFFFFUL)
974 return "out of range hex32 number";
975 Val = n;
976 return StringRef();
977}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000978
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000979void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
980 uint64_t Num = Val;
981 Out << format("0x%016llX", Num);
982}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000983
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000984StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
985 unsigned long long Num;
986 if (getAsUnsignedInteger(Scalar, 0, Num))
987 return "invalid hex64 number";
988 Val = Num;
989 return StringRef();
990}