blob: c410b1d5608605cdc67aa204b7f935e6c89478bd [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
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000121std::vector<StringRef> Input::keys() {
122 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
123 std::vector<StringRef> Ret;
124 if (!MN) {
125 setError(CurrentNode, "not a mapping");
126 return Ret;
127 }
128 for (auto &P : MN->Mapping)
129 Ret.push_back(P.first());
130 return Ret;
131}
132
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000133bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
134 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000135 UseDefault = false;
Mehdi Amini43c24282016-11-28 04:57:04 +0000136 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000137 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000138
139 // CurrentNode is null for empty documents, which is an error in case required
140 // nodes are present.
141 if (!CurrentNode) {
142 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000143 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000144 return false;
145 }
146
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000147 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
148 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000149 setError(CurrentNode, "not a mapping");
150 return false;
151 }
152 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000153 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000154 if (!Value) {
155 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000156 setError(CurrentNode, Twine("missing required key '") + Key + "'");
157 else
158 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000159 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000160 }
161 SaveInfo = CurrentNode;
162 CurrentNode = Value;
163 return true;
164}
165
166void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000167 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000168}
169
170void Input::endMapping() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000171 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000173 // CurrentNode can be null if the document is empty.
174 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000175 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000176 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000177 for (const auto &NN : MN->Mapping) {
Peter Collingbourneefdff712017-01-04 20:10:43 +0000178 if (!is_contained(MN->ValidKeys, NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000179 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180 break;
181 }
182 }
183}
184
Alex Lorenzb1225082015-05-04 20:11:40 +0000185void Input::beginFlowMapping() { beginMapping(); }
186
187void Input::endFlowMapping() { endMapping(); }
188
Nick Kledzikf60a9272012-12-12 20:46:15 +0000189unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000190 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000192 if (isa<EmptyHNode>(CurrentNode))
193 return 0;
194 // Treat case where there's a scalar "null" value as an empty sequence.
195 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
196 if (isNull(SN->value()))
197 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000198 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000199 // Any other type of HNode is an error.
200 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201 return 0;
202}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204void Input::endSequence() {
205}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000206
Nick Kledzikf60a9272012-12-12 20:46:15 +0000207bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000208 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000209 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000210 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000212 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213 return true;
214 }
215 return false;
216}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000217
Nick Kledzikf60a9272012-12-12 20:46:15 +0000218void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000219 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000220}
221
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000222unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000223
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000225 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000227 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000228 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000229 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000230 return true;
231 }
232 return false;
233}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000234
Nick Kledzikf60a9272012-12-12 20:46:15 +0000235void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000236 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000237}
238
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000239void Input::endFlowSequence() {
240}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000241
242void Input::beginEnumScalar() {
243 ScalarMatchFound = false;
244}
245
246bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000247 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000248 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000249 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
250 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000251 ScalarMatchFound = true;
252 return true;
253 }
254 }
255 return false;
256}
257
Michael J. Spencer731cae32015-01-23 21:57:50 +0000258bool Input::matchEnumFallback() {
259 if (ScalarMatchFound)
260 return false;
261 ScalarMatchFound = true;
262 return true;
263}
264
Nick Kledzikf60a9272012-12-12 20:46:15 +0000265void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000266 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000267 setError(CurrentNode, "unknown enumerated scalar");
268 }
269}
270
Nick Kledzikf60a9272012-12-12 20:46:15 +0000271bool Input::beginBitSetScalar(bool &DoClear) {
272 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000273 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000274 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000275 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000276 setError(CurrentNode, "expected sequence of bit values");
277 }
278 DoClear = true;
279 return true;
280}
281
282bool Input::bitSetMatch(const char *Str, bool) {
Mehdi Amini43c24282016-11-28 04:57:04 +0000283 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000284 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000285 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000286 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000287 for (auto &N : SQ->Entries) {
288 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000289 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290 BitValuesUsed[Index] = true;
291 return true;
292 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000293 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000294 setError(CurrentNode, "unexpected scalar in sequence of bit values");
295 }
296 ++Index;
297 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000298 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000299 setError(CurrentNode, "expected sequence of bit values");
300 }
301 return false;
302}
303
304void Input::endBitSetScalar() {
Mehdi Amini43c24282016-11-28 04:57:04 +0000305 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000306 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000307 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000308 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
310 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000311 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000312 return;
313 }
314 }
315 }
316}
317
David Majnemer77880332014-04-10 07:37:33 +0000318void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000319 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000320 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000321 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322 setError(CurrentNode, "unexpected scalar");
323 }
324}
325
Alex Lorenz68e787b2015-05-14 23:08:22 +0000326void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
327
Nick Kledzikf60a9272012-12-12 20:46:15 +0000328void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000329 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000330 this->setError(hnode->_node, message);
331}
332
333void Input::setError(Node *node, const Twine &message) {
334 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000335 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336}
337
David Blaikied759fe52014-09-15 18:39:24 +0000338std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000339 SmallString<128> StringStorage;
340 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000341 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000342 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000343 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000344 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000345 }
David Blaikied759fe52014-09-15 18:39:24 +0000346 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Alex Lorenz68e787b2015-05-14 23:08:22 +0000347 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
Benjamin Kramer7a923772015-08-05 14:16:38 +0000348 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
349 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000350 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000351 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000352 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000353 auto Entry = this->createHNodes(&SN);
Mehdi Amini43c24282016-11-28 04:57:04 +0000354 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000355 break;
David Blaikied759fe52014-09-15 18:39:24 +0000356 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000357 }
David Blaikied759fe52014-09-15 18:39:24 +0000358 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000359 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000360 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000361 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000362 Node *KeyNode = KVN.getKey();
363 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
364 if (!KeyScalar) {
365 setError(KeyNode, "Map key must be a scalar");
366 break;
367 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000368 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000369 StringRef KeyStr = KeyScalar->getValue(StringStorage);
370 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000371 // Copy string to permanent storage
Benjamin Kramer7a923772015-08-05 14:16:38 +0000372 KeyStr = StringStorage.str().copy(StringAllocator);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000373 }
David Blaikied759fe52014-09-15 18:39:24 +0000374 auto ValueHNode = this->createHNodes(KVN.getValue());
Mehdi Amini43c24282016-11-28 04:57:04 +0000375 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000376 break;
David Blaikied759fe52014-09-15 18:39:24 +0000377 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000378 }
David Blaikied759fe52014-09-15 18:39:24 +0000379 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000380 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000381 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000382 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000383 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000384 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000385 }
386}
387
Nick Kledzikf60a9272012-12-12 20:46:15 +0000388void Input::setError(const Twine &Message) {
389 this->setError(CurrentNode, Message);
390}
391
Aaron Ballman0e63e532013-08-15 23:17:53 +0000392bool Input::canElideEmptySequence() {
393 return false;
394}
395
Nick Kledzikf60a9272012-12-12 20:46:15 +0000396//===----------------------------------------------------------------------===//
397// Output
398//===----------------------------------------------------------------------===//
399
Frederic Riss4939e6a2015-05-29 17:56:28 +0000400Output::Output(raw_ostream &yout, void *context, int WrapColumn)
Zachary Turner84efd4d2017-03-15 17:47:39 +0000401 : IO(context), Out(yout), WrapColumn(WrapColumn), Column(0),
402 ColumnAtFlowStart(0), ColumnAtMapFlowStart(0), NeedBitValueComma(false),
403 NeedFlowSequenceComma(false), EnumerationMatchFound(false),
404 NeedsNewLine(false), WriteDefaultValues(false) {}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405
406Output::~Output() {
407}
408
Nick Kledzik4761c602013-11-21 00:20:10 +0000409bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000410 return true;
411}
412
413void Output::beginMapping() {
414 StateStack.push_back(inMapFirstKey);
415 NeedsNewLine = true;
416}
417
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000418bool Output::mapTag(StringRef Tag, bool Use) {
419 if (Use) {
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000420 // If this tag is being written inside a sequence we should write the start
421 // of the sequence before writing the tag, otherwise the tag won't be
422 // attached to the element in the sequence, but rather the sequence itself.
423 bool SequenceElement =
424 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
425 StateStack[StateStack.size() - 2] == inFlowSeq);
426 if (SequenceElement && StateStack.back() == inMapFirstKey) {
427 this->newLineCheck();
428 } else {
429 this->output(" ");
430 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000431 this->output(Tag);
Chris Bieneman92b2e8a2016-06-28 21:10:26 +0000432 if (SequenceElement) {
433 // If we're writing the tag during the first element of a map, the tag
434 // takes the place of the first element in the sequence.
435 if (StateStack.back() == inMapFirstKey) {
436 StateStack.pop_back();
437 StateStack.push_back(inMapOtherKey);
438 }
439 // Tags inside maps in sequences should act as keys in the map from a
440 // formatting perspective, so we always want a newline in a sequence.
441 NeedsNewLine = true;
442 }
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000443 }
444 return Use;
445}
446
Nick Kledzikf60a9272012-12-12 20:46:15 +0000447void Output::endMapping() {
448 StateStack.pop_back();
449}
450
Peter Collingbourne87dd2ab2017-01-04 03:51:36 +0000451std::vector<StringRef> Output::keys() {
452 report_fatal_error("invalid call");
453}
454
Nick Kledzikf60a9272012-12-12 20:46:15 +0000455bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000456 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000457 UseDefault = false;
Zachary Turner84efd4d2017-03-15 17:47:39 +0000458 if (Required || !SameAsDefault || WriteDefaultValues) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000459 auto State = StateStack.back();
460 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
461 flowKey(Key);
462 } else {
463 this->newLineCheck();
464 this->paddedKey(Key);
465 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000466 return true;
467 }
468 return false;
469}
470
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000471void Output::postflightKey(void *) {
472 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000473 StateStack.pop_back();
474 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000475 } else if (StateStack.back() == inFlowMapFirstKey) {
476 StateStack.pop_back();
477 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000478 }
479}
480
Alex Lorenzb1225082015-05-04 20:11:40 +0000481void Output::beginFlowMapping() {
482 StateStack.push_back(inFlowMapFirstKey);
483 this->newLineCheck();
484 ColumnAtMapFlowStart = Column;
485 output("{ ");
486}
487
488void Output::endFlowMapping() {
489 StateStack.pop_back();
490 this->outputUpToEndOfLine(" }");
491}
492
Nick Kledzikf60a9272012-12-12 20:46:15 +0000493void Output::beginDocuments() {
494 this->outputUpToEndOfLine("---");
495}
496
497bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000498 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000499 this->outputUpToEndOfLine("\n---");
500 return true;
501}
502
503void Output::postflightDocument() {
504}
505
506void Output::endDocuments() {
507 output("\n...\n");
508}
509
510unsigned Output::beginSequence() {
511 StateStack.push_back(inSeq);
512 NeedsNewLine = true;
513 return 0;
514}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000515
Nick Kledzikf60a9272012-12-12 20:46:15 +0000516void Output::endSequence() {
517 StateStack.pop_back();
518}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000519
520bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000521 return true;
522}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000523
524void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000525}
526
527unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000528 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000529 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000530 ColumnAtFlowStart = Column;
531 output("[ ");
532 NeedFlowSequenceComma = false;
533 return 0;
534}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000535
Nick Kledzikf60a9272012-12-12 20:46:15 +0000536void Output::endFlowSequence() {
537 StateStack.pop_back();
538 this->outputUpToEndOfLine(" ]");
539}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000540
541bool Output::preflightFlowElement(unsigned, void *&) {
542 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000543 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000544 if (WrapColumn && Column > WrapColumn) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000545 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000546 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000547 output(" ");
548 Column = ColumnAtFlowStart;
549 output(" ");
550 }
551 return true;
552}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000553
554void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000555 NeedFlowSequenceComma = true;
556}
557
Nick Kledzikf60a9272012-12-12 20:46:15 +0000558void Output::beginEnumScalar() {
559 EnumerationMatchFound = false;
560}
561
562bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000563 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000564 this->newLineCheck();
565 this->outputUpToEndOfLine(Str);
566 EnumerationMatchFound = true;
567 }
568 return false;
569}
570
Michael J. Spencer731cae32015-01-23 21:57:50 +0000571bool Output::matchEnumFallback() {
572 if (EnumerationMatchFound)
573 return false;
574 EnumerationMatchFound = true;
575 return true;
576}
577
Nick Kledzikf60a9272012-12-12 20:46:15 +0000578void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000579 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000580 llvm_unreachable("bad runtime enum value");
581}
582
Nick Kledzikf60a9272012-12-12 20:46:15 +0000583bool Output::beginBitSetScalar(bool &DoClear) {
584 this->newLineCheck();
585 output("[ ");
586 NeedBitValueComma = false;
587 DoClear = false;
588 return true;
589}
590
591bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000592 if (Matches) {
593 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000594 output(", ");
595 this->output(Str);
596 NeedBitValueComma = true;
597 }
598 return false;
599}
600
601void Output::endBitSetScalar() {
602 this->outputUpToEndOfLine(" ]");
603}
604
David Majnemer77880332014-04-10 07:37:33 +0000605void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000606 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000607 if (S.empty()) {
608 // Print '' for the empty string because leaving the field empty is not
609 // allowed.
610 this->outputUpToEndOfLine("''");
611 return;
612 }
David Majnemer77880332014-04-10 07:37:33 +0000613 if (!MustQuote) {
614 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000615 this->outputUpToEndOfLine(S);
616 return;
617 }
618 unsigned i = 0;
619 unsigned j = 0;
620 unsigned End = S.size();
621 output("'"); // Starting single quote.
622 const char *Base = S.data();
623 while (j < End) {
624 // Escape a single quote by doubling it.
625 if (S[j] == '\'') {
626 output(StringRef(&Base[i], j - i + 1));
627 output("'");
628 i = j + 1;
629 }
630 ++j;
631 }
632 output(StringRef(&Base[i], j - i));
633 this->outputUpToEndOfLine("'"); // Ending single quote.
634}
635
Alex Lorenz68e787b2015-05-14 23:08:22 +0000636void Output::blockScalarString(StringRef &S) {
637 if (!StateStack.empty())
638 newLineCheck();
639 output(" |");
640 outputNewLine();
641
642 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
643
644 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
645 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
646 for (unsigned I = 0; I < Indent; ++I) {
647 output(" ");
648 }
649 output(*Lines);
650 outputNewLine();
651 }
652}
653
Nick Kledzikf60a9272012-12-12 20:46:15 +0000654void Output::setError(const Twine &message) {
655}
656
Aaron Ballman0e63e532013-08-15 23:17:53 +0000657bool Output::canElideEmptySequence() {
658 // Normally, with an optional key/value where the value is an empty sequence,
659 // the whole key/value can be not written. But, that produces wrong yaml
660 // if the key/value is the only thing in the map and the map is used in
661 // a sequence. This detects if the this sequence is the first key/value
662 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000663 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000664 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000665 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000666 return true;
667 return (StateStack[StateStack.size()-2] != inSeq);
668}
669
Nick Kledzikf60a9272012-12-12 20:46:15 +0000670void Output::output(StringRef s) {
671 Column += s.size();
672 Out << s;
673}
674
675void Output::outputUpToEndOfLine(StringRef s) {
676 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000677 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
678 StateStack.back() != inFlowMapFirstKey &&
679 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000680 NeedsNewLine = true;
681}
682
683void Output::outputNewLine() {
684 Out << "\n";
685 Column = 0;
686}
687
688// if seq at top, indent as if map, then add "- "
689// if seq in middle, use "- " if firstKey, else use " "
690//
691
692void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000693 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000694 return;
695 NeedsNewLine = false;
696
697 this->outputNewLine();
698
699 assert(StateStack.size() > 0);
700 unsigned Indent = StateStack.size() - 1;
701 bool OutputDash = false;
702
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000703 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000704 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000705 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000706 (StateStack.back() == inFlowSeq) ||
707 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000708 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000709 --Indent;
710 OutputDash = true;
711 }
712
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000713 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000714 output(" ");
715 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000716 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000717 output("- ");
718 }
719
720}
721
722void Output::paddedKey(StringRef key) {
723 output(key);
724 output(":");
725 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000726 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000727 output(&spaces[key.size()]);
728 else
729 output(" ");
730}
731
Alex Lorenzb1225082015-05-04 20:11:40 +0000732void Output::flowKey(StringRef Key) {
733 if (StateStack.back() == inFlowMapOtherKey)
734 output(", ");
Frederic Riss4939e6a2015-05-29 17:56:28 +0000735 if (WrapColumn && Column > WrapColumn) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000736 output("\n");
737 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
738 output(" ");
739 Column = ColumnAtMapFlowStart;
740 output(" ");
741 }
742 output(Key);
743 output(": ");
744}
745
Nick Kledzikf60a9272012-12-12 20:46:15 +0000746//===----------------------------------------------------------------------===//
747// traits for built-in types
748//===----------------------------------------------------------------------===//
749
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000750void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
751 Out << (Val ? "true" : "false");
752}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000753
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000754StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
755 if (Scalar.equals("true")) {
756 Val = true;
757 return StringRef();
758 } else if (Scalar.equals("false")) {
759 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000760 return StringRef();
761 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000762 return "invalid boolean";
763}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000764
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000765void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
766 raw_ostream &Out) {
767 Out << Val;
768}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000769
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000770StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
771 StringRef &Val) {
772 Val = Scalar;
773 return StringRef();
774}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000775
John Thompson48e018a2013-11-19 17:28:21 +0000776void ScalarTraits<std::string>::output(const std::string &Val, void *,
777 raw_ostream &Out) {
778 Out << Val;
779}
780
781StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
782 std::string &Val) {
783 Val = Scalar.str();
784 return StringRef();
785}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000786
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000787void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
788 raw_ostream &Out) {
789 // use temp uin32_t because ostream thinks uint8_t is a character
790 uint32_t Num = Val;
791 Out << Num;
792}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000793
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000794StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
795 unsigned long long n;
796 if (getAsUnsignedInteger(Scalar, 0, n))
797 return "invalid number";
798 if (n > 0xFF)
799 return "out of range number";
800 Val = n;
801 return StringRef();
802}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000803
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000804void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
805 raw_ostream &Out) {
806 Out << Val;
807}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000808
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000809StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
810 uint16_t &Val) {
811 unsigned long long n;
812 if (getAsUnsignedInteger(Scalar, 0, n))
813 return "invalid number";
814 if (n > 0xFFFF)
815 return "out of range number";
816 Val = n;
817 return StringRef();
818}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000819
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000820void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
821 raw_ostream &Out) {
822 Out << Val;
823}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000824
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000825StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
826 uint32_t &Val) {
827 unsigned long long n;
828 if (getAsUnsignedInteger(Scalar, 0, n))
829 return "invalid number";
830 if (n > 0xFFFFFFFFUL)
831 return "out of range number";
832 Val = n;
833 return StringRef();
834}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000835
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000836void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
837 raw_ostream &Out) {
838 Out << Val;
839}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000840
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000841StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
842 uint64_t &Val) {
843 unsigned long long N;
844 if (getAsUnsignedInteger(Scalar, 0, N))
845 return "invalid number";
846 Val = N;
847 return StringRef();
848}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000849
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000850void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
851 // use temp in32_t because ostream thinks int8_t is a character
852 int32_t Num = Val;
853 Out << Num;
854}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000855
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000856StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
857 long long N;
858 if (getAsSignedInteger(Scalar, 0, N))
859 return "invalid number";
860 if ((N > 127) || (N < -128))
861 return "out of range number";
862 Val = N;
863 return StringRef();
864}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000865
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000866void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
867 raw_ostream &Out) {
868 Out << Val;
869}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000870
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000871StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
872 long long N;
873 if (getAsSignedInteger(Scalar, 0, N))
874 return "invalid number";
875 if ((N > INT16_MAX) || (N < INT16_MIN))
876 return "out of range number";
877 Val = N;
878 return StringRef();
879}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000880
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000881void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
882 raw_ostream &Out) {
883 Out << Val;
884}
885
886StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
887 long long N;
888 if (getAsSignedInteger(Scalar, 0, N))
889 return "invalid number";
890 if ((N > INT32_MAX) || (N < INT32_MIN))
891 return "out of range number";
892 Val = N;
893 return StringRef();
894}
895
896void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
897 raw_ostream &Out) {
898 Out << Val;
899}
900
901StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
902 long long N;
903 if (getAsSignedInteger(Scalar, 0, N))
904 return "invalid number";
905 Val = N;
906 return StringRef();
907}
908
909void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000910 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000911}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000912
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000913StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
914 SmallString<32> buff(Scalar.begin(), Scalar.end());
915 char *end;
916 Val = strtod(buff.c_str(), &end);
917 if (*end != '\0')
918 return "invalid floating point number";
919 return StringRef();
920}
921
922void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000923 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000924}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000925
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000926StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
927 SmallString<32> buff(Scalar.begin(), Scalar.end());
928 char *end;
929 Val = strtod(buff.c_str(), &end);
930 if (*end != '\0')
931 return "invalid floating point number";
932 return StringRef();
933}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000934
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000935void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
936 uint8_t Num = Val;
937 Out << format("0x%02X", Num);
938}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000939
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000940StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
941 unsigned long long n;
942 if (getAsUnsignedInteger(Scalar, 0, n))
943 return "invalid hex8 number";
944 if (n > 0xFF)
945 return "out of range hex8 number";
946 Val = n;
947 return StringRef();
948}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000949
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000950void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
951 uint16_t Num = Val;
952 Out << format("0x%04X", Num);
953}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000954
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000955StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
956 unsigned long long n;
957 if (getAsUnsignedInteger(Scalar, 0, n))
958 return "invalid hex16 number";
959 if (n > 0xFFFF)
960 return "out of range hex16 number";
961 Val = n;
962 return StringRef();
963}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000964
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000965void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
966 uint32_t Num = Val;
967 Out << format("0x%08X", Num);
968}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000969
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000970StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
971 unsigned long long n;
972 if (getAsUnsignedInteger(Scalar, 0, n))
973 return "invalid hex32 number";
974 if (n > 0xFFFFFFFFUL)
975 return "out of range hex32 number";
976 Val = n;
977 return StringRef();
978}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000979
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000980void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
981 uint64_t Num = Val;
982 Out << format("0x%016llX", Num);
983}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000984
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000985StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
986 unsigned long long Num;
987 if (getAsUnsignedInteger(Scalar, 0, Num))
988 return "invalid hex64 number";
989 Val = Num;
990 return StringRef();
991}