blob: 8ef36a332808d3d989d8be234ebb8798bf345800 [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"
Nick Kledzikf60a9272012-12-12 20:46:15 +000017#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000018#include "llvm/Support/raw_ostream.h"
Rui Ueyama106eded2013-09-11 04:00:08 +000019#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include <cstring>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000021using namespace llvm;
22using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000023
24//===----------------------------------------------------------------------===//
25// IO
26//===----------------------------------------------------------------------===//
27
28IO::IO(void *Context) : Ctxt(Context) {
29}
30
31IO::~IO() {
32}
33
34void *IO::getContext() {
35 return Ctxt;
36}
37
38void IO::setContext(void *Context) {
39 Ctxt = Context;
40}
41
Nick Kledzikf60a9272012-12-12 20:46:15 +000042//===----------------------------------------------------------------------===//
43// Input
44//===----------------------------------------------------------------------===//
45
Alexander Kornienko681e37c2013-11-18 15:50:04 +000046Input::Input(StringRef InputContent,
47 void *Ctxt,
48 SourceMgr::DiagHandlerTy DiagHandler,
49 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000050 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000051 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000052 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000053 if (DiagHandler)
54 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000055 DocIterator = Strm->begin();
56}
57
Nick Kledzik0dcef842013-01-08 21:04:44 +000058Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000059}
60
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000061std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000062
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000063// Pin the vtables to this file.
64void Input::HNode::anchor() {}
65void Input::EmptyHNode::anchor() {}
66void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000067void Input::MapHNode::anchor() {}
68void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000069
Nick Kledzik4761c602013-11-21 00:20:10 +000070bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000071 return false;
72}
73
74bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000075 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000076 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000077 if (!N) {
78 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000079 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000080 return false;
81 }
82
Benjamin Kramer36b0f122012-12-12 22:40:02 +000083 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000084 // Empty files are allowed and ignored
85 ++DocIterator;
86 return setCurrentDocument();
87 }
David Blaikied759fe52014-09-15 18:39:24 +000088 TopNode = this->createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +000089 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000090 return true;
91 }
92 return false;
93}
94
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000095bool Input::nextDocument() {
96 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000097}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000098
Nick Kledzik1e6033c2013-11-14 00:59:59 +000099bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +0000100 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000101 if (foundTag.empty()) {
102 // If no tag found and 'Tag' is the default, say it was found.
103 return Default;
104 }
105 // Return true iff found tag matches supplied tag.
106 return Tag.equals(foundTag);
107}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000108
109void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000110 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000111 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000112 // CurrentNode can be null if the document is empty.
113 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000114 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000115 MN->ValidKeys.clear();
116 }
117}
118
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000119bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
120 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000121 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000122 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000123 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000124
125 // CurrentNode is null for empty documents, which is an error in case required
126 // nodes are present.
127 if (!CurrentNode) {
128 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000129 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000130 return false;
131 }
132
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000133 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
134 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000135 setError(CurrentNode, "not a mapping");
136 return false;
137 }
138 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000139 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000140 if (!Value) {
141 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000142 setError(CurrentNode, Twine("missing required key '") + Key + "'");
143 else
144 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000145 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000146 }
147 SaveInfo = CurrentNode;
148 CurrentNode = Value;
149 return true;
150}
151
152void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000153 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000154}
155
156void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000157 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000158 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000159 // CurrentNode can be null if the document is empty.
160 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000161 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000162 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000163 for (const auto &NN : MN->Mapping) {
164 if (!MN->isValidKey(NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000165 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000166 break;
167 }
168 }
169}
170
Alex Lorenzb1225082015-05-04 20:11:40 +0000171void Input::beginFlowMapping() { beginMapping(); }
172
173void Input::endFlowMapping() { endMapping(); }
174
Nick Kledzikf60a9272012-12-12 20:46:15 +0000175unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000176 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000177 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000178 if (isa<EmptyHNode>(CurrentNode))
179 return 0;
180 // Treat case where there's a scalar "null" value as an empty sequence.
181 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
182 if (isNull(SN->value()))
183 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000184 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000185 // Any other type of HNode is an error.
186 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000187 return 0;
188}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000189
Nick Kledzikf60a9272012-12-12 20:46:15 +0000190void Input::endSequence() {
191}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000192
Nick Kledzikf60a9272012-12-12 20:46:15 +0000193bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000196 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000197 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000198 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000199 return true;
200 }
201 return false;
202}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000205 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206}
207
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000208unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000209
Nick Kledzikf60a9272012-12-12 20:46:15 +0000210bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000211 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000212 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000213 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000214 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000215 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000216 return true;
217 }
218 return false;
219}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000220
Nick Kledzikf60a9272012-12-12 20:46:15 +0000221void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000222 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000223}
224
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225void Input::endFlowSequence() {
226}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000227
228void Input::beginEnumScalar() {
229 ScalarMatchFound = false;
230}
231
232bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000233 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000234 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000235 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
236 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000237 ScalarMatchFound = true;
238 return true;
239 }
240 }
241 return false;
242}
243
Michael J. Spencer731cae32015-01-23 21:57:50 +0000244bool Input::matchEnumFallback() {
245 if (ScalarMatchFound)
246 return false;
247 ScalarMatchFound = true;
248 return true;
249}
250
Nick Kledzikf60a9272012-12-12 20:46:15 +0000251void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000252 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000253 setError(CurrentNode, "unknown enumerated scalar");
254 }
255}
256
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257bool Input::beginBitSetScalar(bool &DoClear) {
258 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000259 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000260 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000261 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000262 setError(CurrentNode, "expected sequence of bit values");
263 }
264 DoClear = true;
265 return true;
266}
267
268bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000269 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000270 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000271 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000272 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000273 for (auto &N : SQ->Entries) {
274 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000275 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000276 BitValuesUsed[Index] = true;
277 return true;
278 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000279 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280 setError(CurrentNode, "unexpected scalar in sequence of bit values");
281 }
282 ++Index;
283 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000284 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000285 setError(CurrentNode, "expected sequence of bit values");
286 }
287 return false;
288}
289
290void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000291 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000292 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000293 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000294 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000295 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
296 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000297 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000298 return;
299 }
300 }
301 }
302}
303
David Majnemer77880332014-04-10 07:37:33 +0000304void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000305 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000306 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000307 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000308 setError(CurrentNode, "unexpected scalar");
309 }
310}
311
312void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000313 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000314 this->setError(hnode->_node, message);
315}
316
317void Input::setError(Node *node, const Twine &message) {
318 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000319 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000320}
321
David Blaikied759fe52014-09-15 18:39:24 +0000322std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000323 SmallString<128> StringStorage;
324 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000325 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000326 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000327 // Copy string to permanent storage
328 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000329 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000330 memcpy(Buf, &StringStorage[0], Len);
331 KeyStr = StringRef(Buf, Len);
332 }
David Blaikied759fe52014-09-15 18:39:24 +0000333 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000334 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000335 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000336 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000337 auto Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000338 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000339 break;
David Blaikied759fe52014-09-15 18:39:24 +0000340 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000341 }
David Blaikied759fe52014-09-15 18:39:24 +0000342 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000343 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000344 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000345 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000346 Node *KeyNode = KVN.getKey();
347 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
348 if (!KeyScalar) {
349 setError(KeyNode, "Map key must be a scalar");
350 break;
351 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000352 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000353 StringRef KeyStr = KeyScalar->getValue(StringStorage);
354 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000355 // Copy string to permanent storage
356 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000357 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000358 memcpy(Buf, &StringStorage[0], Len);
359 KeyStr = StringRef(Buf, Len);
360 }
David Blaikied759fe52014-09-15 18:39:24 +0000361 auto ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000362 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000363 break;
David Blaikied759fe52014-09-15 18:39:24 +0000364 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000365 }
David Blaikied759fe52014-09-15 18:39:24 +0000366 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000367 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000368 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000369 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000370 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000371 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000372 }
373}
374
Nick Kledzikf60a9272012-12-12 20:46:15 +0000375bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000376 for (const char *K : ValidKeys) {
377 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000378 return true;
379 }
380 return false;
381}
382
383void Input::setError(const Twine &Message) {
384 this->setError(CurrentNode, Message);
385}
386
Aaron Ballman0e63e532013-08-15 23:17:53 +0000387bool Input::canElideEmptySequence() {
388 return false;
389}
390
Nick Kledzikf60a9272012-12-12 20:46:15 +0000391//===----------------------------------------------------------------------===//
392// Output
393//===----------------------------------------------------------------------===//
394
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000395Output::Output(raw_ostream &yout, void *context)
396 : IO(context),
397 Out(yout),
398 Column(0),
399 ColumnAtFlowStart(0),
Alex Lorenzb1225082015-05-04 20:11:40 +0000400 ColumnAtMapFlowStart(0),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000401 NeedBitValueComma(false),
402 NeedFlowSequenceComma(false),
403 EnumerationMatchFound(false),
404 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405}
406
407Output::~Output() {
408}
409
Nick Kledzik4761c602013-11-21 00:20:10 +0000410bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000411 return true;
412}
413
414void Output::beginMapping() {
415 StateStack.push_back(inMapFirstKey);
416 NeedsNewLine = true;
417}
418
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000419bool Output::mapTag(StringRef Tag, bool Use) {
420 if (Use) {
421 this->output(" ");
422 this->output(Tag);
423 }
424 return Use;
425}
426
Nick Kledzikf60a9272012-12-12 20:46:15 +0000427void Output::endMapping() {
428 StateStack.pop_back();
429}
430
Nick Kledzikf60a9272012-12-12 20:46:15 +0000431bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000432 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000433 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000434 if (Required || !SameAsDefault) {
Alex Lorenzb1225082015-05-04 20:11:40 +0000435 auto State = StateStack.back();
436 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
437 flowKey(Key);
438 } else {
439 this->newLineCheck();
440 this->paddedKey(Key);
441 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000442 return true;
443 }
444 return false;
445}
446
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000447void Output::postflightKey(void *) {
448 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000449 StateStack.pop_back();
450 StateStack.push_back(inMapOtherKey);
Alex Lorenzb1225082015-05-04 20:11:40 +0000451 } else if (StateStack.back() == inFlowMapFirstKey) {
452 StateStack.pop_back();
453 StateStack.push_back(inFlowMapOtherKey);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000454 }
455}
456
Alex Lorenzb1225082015-05-04 20:11:40 +0000457void Output::beginFlowMapping() {
458 StateStack.push_back(inFlowMapFirstKey);
459 this->newLineCheck();
460 ColumnAtMapFlowStart = Column;
461 output("{ ");
462}
463
464void Output::endFlowMapping() {
465 StateStack.pop_back();
466 this->outputUpToEndOfLine(" }");
467}
468
Nick Kledzikf60a9272012-12-12 20:46:15 +0000469void Output::beginDocuments() {
470 this->outputUpToEndOfLine("---");
471}
472
473bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000474 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000475 this->outputUpToEndOfLine("\n---");
476 return true;
477}
478
479void Output::postflightDocument() {
480}
481
482void Output::endDocuments() {
483 output("\n...\n");
484}
485
486unsigned Output::beginSequence() {
487 StateStack.push_back(inSeq);
488 NeedsNewLine = true;
489 return 0;
490}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000491
Nick Kledzikf60a9272012-12-12 20:46:15 +0000492void Output::endSequence() {
493 StateStack.pop_back();
494}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000495
496bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000497 return true;
498}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000499
500void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000501}
502
503unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000504 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000505 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000506 ColumnAtFlowStart = Column;
507 output("[ ");
508 NeedFlowSequenceComma = false;
509 return 0;
510}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000511
Nick Kledzikf60a9272012-12-12 20:46:15 +0000512void Output::endFlowSequence() {
513 StateStack.pop_back();
514 this->outputUpToEndOfLine(" ]");
515}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000516
517bool Output::preflightFlowElement(unsigned, void *&) {
518 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000519 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000520 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000521 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000522 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000523 output(" ");
524 Column = ColumnAtFlowStart;
525 output(" ");
526 }
527 return true;
528}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000529
530void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000531 NeedFlowSequenceComma = true;
532}
533
Nick Kledzikf60a9272012-12-12 20:46:15 +0000534void Output::beginEnumScalar() {
535 EnumerationMatchFound = false;
536}
537
538bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000539 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000540 this->newLineCheck();
541 this->outputUpToEndOfLine(Str);
542 EnumerationMatchFound = true;
543 }
544 return false;
545}
546
Michael J. Spencer731cae32015-01-23 21:57:50 +0000547bool Output::matchEnumFallback() {
548 if (EnumerationMatchFound)
549 return false;
550 EnumerationMatchFound = true;
551 return true;
552}
553
Nick Kledzikf60a9272012-12-12 20:46:15 +0000554void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000555 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000556 llvm_unreachable("bad runtime enum value");
557}
558
Nick Kledzikf60a9272012-12-12 20:46:15 +0000559bool Output::beginBitSetScalar(bool &DoClear) {
560 this->newLineCheck();
561 output("[ ");
562 NeedBitValueComma = false;
563 DoClear = false;
564 return true;
565}
566
567bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000568 if (Matches) {
569 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000570 output(", ");
571 this->output(Str);
572 NeedBitValueComma = true;
573 }
574 return false;
575}
576
577void Output::endBitSetScalar() {
578 this->outputUpToEndOfLine(" ]");
579}
580
David Majnemer77880332014-04-10 07:37:33 +0000581void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000582 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000583 if (S.empty()) {
584 // Print '' for the empty string because leaving the field empty is not
585 // allowed.
586 this->outputUpToEndOfLine("''");
587 return;
588 }
David Majnemer77880332014-04-10 07:37:33 +0000589 if (!MustQuote) {
590 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000591 this->outputUpToEndOfLine(S);
592 return;
593 }
594 unsigned i = 0;
595 unsigned j = 0;
596 unsigned End = S.size();
597 output("'"); // Starting single quote.
598 const char *Base = S.data();
599 while (j < End) {
600 // Escape a single quote by doubling it.
601 if (S[j] == '\'') {
602 output(StringRef(&Base[i], j - i + 1));
603 output("'");
604 i = j + 1;
605 }
606 ++j;
607 }
608 output(StringRef(&Base[i], j - i));
609 this->outputUpToEndOfLine("'"); // Ending single quote.
610}
611
612void Output::setError(const Twine &message) {
613}
614
Aaron Ballman0e63e532013-08-15 23:17:53 +0000615bool Output::canElideEmptySequence() {
616 // Normally, with an optional key/value where the value is an empty sequence,
617 // the whole key/value can be not written. But, that produces wrong yaml
618 // if the key/value is the only thing in the map and the map is used in
619 // a sequence. This detects if the this sequence is the first key/value
620 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000621 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000622 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000623 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000624 return true;
625 return (StateStack[StateStack.size()-2] != inSeq);
626}
627
Nick Kledzikf60a9272012-12-12 20:46:15 +0000628void Output::output(StringRef s) {
629 Column += s.size();
630 Out << s;
631}
632
633void Output::outputUpToEndOfLine(StringRef s) {
634 this->output(s);
Alex Lorenzb1225082015-05-04 20:11:40 +0000635 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
636 StateStack.back() != inFlowMapFirstKey &&
637 StateStack.back() != inFlowMapOtherKey))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000638 NeedsNewLine = true;
639}
640
641void Output::outputNewLine() {
642 Out << "\n";
643 Column = 0;
644}
645
646// if seq at top, indent as if map, then add "- "
647// if seq in middle, use "- " if firstKey, else use " "
648//
649
650void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000651 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000652 return;
653 NeedsNewLine = false;
654
655 this->outputNewLine();
656
657 assert(StateStack.size() > 0);
658 unsigned Indent = StateStack.size() - 1;
659 bool OutputDash = false;
660
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000661 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000662 OutputDash = true;
Alex Lorenz42e91fa2015-05-01 18:34:25 +0000663 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
Alex Lorenzb1225082015-05-04 20:11:40 +0000664 (StateStack.back() == inFlowSeq) ||
665 (StateStack.back() == inFlowMapFirstKey)) &&
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000666 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000667 --Indent;
668 OutputDash = true;
669 }
670
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000671 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000672 output(" ");
673 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000674 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000675 output("- ");
676 }
677
678}
679
680void Output::paddedKey(StringRef key) {
681 output(key);
682 output(":");
683 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000684 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000685 output(&spaces[key.size()]);
686 else
687 output(" ");
688}
689
Alex Lorenzb1225082015-05-04 20:11:40 +0000690void Output::flowKey(StringRef Key) {
691 if (StateStack.back() == inFlowMapOtherKey)
692 output(", ");
693 if (Column > 70) {
694 output("\n");
695 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
696 output(" ");
697 Column = ColumnAtMapFlowStart;
698 output(" ");
699 }
700 output(Key);
701 output(": ");
702}
703
Nick Kledzikf60a9272012-12-12 20:46:15 +0000704//===----------------------------------------------------------------------===//
705// traits for built-in types
706//===----------------------------------------------------------------------===//
707
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000708void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
709 Out << (Val ? "true" : "false");
710}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000711
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000712StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
713 if (Scalar.equals("true")) {
714 Val = true;
715 return StringRef();
716 } else if (Scalar.equals("false")) {
717 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000718 return StringRef();
719 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000720 return "invalid boolean";
721}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000722
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000723void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
724 raw_ostream &Out) {
725 Out << Val;
726}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000727
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000728StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
729 StringRef &Val) {
730 Val = Scalar;
731 return StringRef();
732}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000733
John Thompson48e018a2013-11-19 17:28:21 +0000734void ScalarTraits<std::string>::output(const std::string &Val, void *,
735 raw_ostream &Out) {
736 Out << Val;
737}
738
739StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
740 std::string &Val) {
741 Val = Scalar.str();
742 return StringRef();
743}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000744
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000745void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
746 raw_ostream &Out) {
747 // use temp uin32_t because ostream thinks uint8_t is a character
748 uint32_t Num = Val;
749 Out << Num;
750}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000751
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000752StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
753 unsigned long long n;
754 if (getAsUnsignedInteger(Scalar, 0, n))
755 return "invalid number";
756 if (n > 0xFF)
757 return "out of range number";
758 Val = n;
759 return StringRef();
760}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000761
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000762void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
763 raw_ostream &Out) {
764 Out << Val;
765}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000766
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000767StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
768 uint16_t &Val) {
769 unsigned long long n;
770 if (getAsUnsignedInteger(Scalar, 0, n))
771 return "invalid number";
772 if (n > 0xFFFF)
773 return "out of range number";
774 Val = n;
775 return StringRef();
776}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000777
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000778void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
779 raw_ostream &Out) {
780 Out << Val;
781}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000782
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000783StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
784 uint32_t &Val) {
785 unsigned long long n;
786 if (getAsUnsignedInteger(Scalar, 0, n))
787 return "invalid number";
788 if (n > 0xFFFFFFFFUL)
789 return "out of range number";
790 Val = n;
791 return StringRef();
792}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000793
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000794void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
795 raw_ostream &Out) {
796 Out << Val;
797}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000798
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000799StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
800 uint64_t &Val) {
801 unsigned long long N;
802 if (getAsUnsignedInteger(Scalar, 0, N))
803 return "invalid 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<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
809 // use temp in32_t because ostream thinks int8_t is a character
810 int32_t Num = Val;
811 Out << Num;
812}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000813
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000814StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
815 long long N;
816 if (getAsSignedInteger(Scalar, 0, N))
817 return "invalid number";
818 if ((N > 127) || (N < -128))
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<int16_t>::output(const int16_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<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
830 long long N;
831 if (getAsSignedInteger(Scalar, 0, N))
832 return "invalid number";
833 if ((N > INT16_MAX) || (N < INT16_MIN))
834 return "out of range number";
835 Val = N;
836 return StringRef();
837}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000838
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000839void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
840 raw_ostream &Out) {
841 Out << Val;
842}
843
844StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
845 long long N;
846 if (getAsSignedInteger(Scalar, 0, N))
847 return "invalid number";
848 if ((N > INT32_MAX) || (N < INT32_MIN))
849 return "out of range number";
850 Val = N;
851 return StringRef();
852}
853
854void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
855 raw_ostream &Out) {
856 Out << Val;
857}
858
859StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
860 long long N;
861 if (getAsSignedInteger(Scalar, 0, N))
862 return "invalid number";
863 Val = N;
864 return StringRef();
865}
866
867void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000868 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000869}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000870
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000871StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
872 SmallString<32> buff(Scalar.begin(), Scalar.end());
873 char *end;
874 Val = strtod(buff.c_str(), &end);
875 if (*end != '\0')
876 return "invalid floating point number";
877 return StringRef();
878}
879
880void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000881 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000882}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000883
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000884StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
885 SmallString<32> buff(Scalar.begin(), Scalar.end());
886 char *end;
887 Val = strtod(buff.c_str(), &end);
888 if (*end != '\0')
889 return "invalid floating point number";
890 return StringRef();
891}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000892
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000893void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
894 uint8_t Num = Val;
895 Out << format("0x%02X", Num);
896}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000897
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000898StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
899 unsigned long long n;
900 if (getAsUnsignedInteger(Scalar, 0, n))
901 return "invalid hex8 number";
902 if (n > 0xFF)
903 return "out of range hex8 number";
904 Val = n;
905 return StringRef();
906}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000907
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000908void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
909 uint16_t Num = Val;
910 Out << format("0x%04X", Num);
911}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000912
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000913StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
914 unsigned long long n;
915 if (getAsUnsignedInteger(Scalar, 0, n))
916 return "invalid hex16 number";
917 if (n > 0xFFFF)
918 return "out of range hex16 number";
919 Val = n;
920 return StringRef();
921}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000922
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000923void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
924 uint32_t Num = Val;
925 Out << format("0x%08X", Num);
926}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000927
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000928StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
929 unsigned long long n;
930 if (getAsUnsignedInteger(Scalar, 0, n))
931 return "invalid hex32 number";
932 if (n > 0xFFFFFFFFUL)
933 return "out of range hex32 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<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
939 uint64_t Num = Val;
940 Out << format("0x%016llX", Num);
941}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000942
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000943StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
944 unsigned long long Num;
945 if (getAsUnsignedInteger(Scalar, 0, Num))
946 return "invalid hex64 number";
947 Val = Num;
948 return StringRef();
949}