blob: d888e69893716a29447bf848b59bbf5f0598deb8 [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
Rafael Espindola2a826e42014-06-13 17:20:48 +000010#include "llvm/Support/Errc.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
13#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000014#include "llvm/Support/Format.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000015#include "llvm/Support/YAMLParser.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/Support/YAMLTraits.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000017#include "llvm/Support/raw_ostream.h"
Rui Ueyama106eded2013-09-11 04:00:08 +000018#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include <cstring>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000020using namespace llvm;
21using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000022
23//===----------------------------------------------------------------------===//
24// IO
25//===----------------------------------------------------------------------===//
26
27IO::IO(void *Context) : Ctxt(Context) {
28}
29
30IO::~IO() {
31}
32
33void *IO::getContext() {
34 return Ctxt;
35}
36
37void IO::setContext(void *Context) {
38 Ctxt = Context;
39}
40
Nick Kledzikf60a9272012-12-12 20:46:15 +000041//===----------------------------------------------------------------------===//
42// Input
43//===----------------------------------------------------------------------===//
44
Alexander Kornienko681e37c2013-11-18 15:50:04 +000045Input::Input(StringRef InputContent,
46 void *Ctxt,
47 SourceMgr::DiagHandlerTy DiagHandler,
48 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000049 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000050 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000051 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000052 if (DiagHandler)
53 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000054 DocIterator = Strm->begin();
55}
56
Nick Kledzik0dcef842013-01-08 21:04:44 +000057Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000058}
59
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000060std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000061
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000062// Pin the vtables to this file.
63void Input::HNode::anchor() {}
64void Input::EmptyHNode::anchor() {}
65void Input::ScalarHNode::anchor() {}
David Blaikied759fe52014-09-15 18:39:24 +000066void Input::MapHNode::anchor() {}
67void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000068
Nick Kledzik4761c602013-11-21 00:20:10 +000069bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000070 return false;
71}
72
73bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000074 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000075 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000076 if (!N) {
77 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000078 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000079 return false;
80 }
81
Benjamin Kramer36b0f122012-12-12 22:40:02 +000082 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000083 // Empty files are allowed and ignored
84 ++DocIterator;
85 return setCurrentDocument();
86 }
David Blaikied759fe52014-09-15 18:39:24 +000087 TopNode = this->createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +000088 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000089 return true;
90 }
91 return false;
92}
93
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000094bool Input::nextDocument() {
95 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000096}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000097
Nick Kledzik1e6033c2013-11-14 00:59:59 +000098bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000099 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000100 if (foundTag.empty()) {
101 // If no tag found and 'Tag' is the default, say it was found.
102 return Default;
103 }
104 // Return true iff found tag matches supplied tag.
105 return Tag.equals(foundTag);
106}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000107
108void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000109 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000110 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000111 // CurrentNode can be null if the document is empty.
112 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000113 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000114 MN->ValidKeys.clear();
115 }
116}
117
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000118bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
119 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000120 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000121 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000122 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000123
124 // CurrentNode is null for empty documents, which is an error in case required
125 // nodes are present.
126 if (!CurrentNode) {
127 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000128 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000129 return false;
130 }
131
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000132 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
133 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000134 setError(CurrentNode, "not a mapping");
135 return false;
136 }
137 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000138 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000139 if (!Value) {
140 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000141 setError(CurrentNode, Twine("missing required key '") + Key + "'");
142 else
143 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000144 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000145 }
146 SaveInfo = CurrentNode;
147 CurrentNode = Value;
148 return true;
149}
150
151void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000152 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000153}
154
155void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000156 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000157 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000158 // CurrentNode can be null if the document is empty.
159 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000160 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000161 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000162 for (const auto &NN : MN->Mapping) {
163 if (!MN->isValidKey(NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000164 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000165 break;
166 }
167 }
168}
169
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170unsigned Input::beginSequence() {
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172 return SQ->Entries.size();
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000173 if (isa<EmptyHNode>(CurrentNode))
174 return 0;
175 // Treat case where there's a scalar "null" value as an empty sequence.
176 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
177 if (isNull(SN->value()))
178 return 0;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179 }
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000180 // Any other type of HNode is an error.
181 setError(CurrentNode, "not a sequence");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182 return 0;
183}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000184
Nick Kledzikf60a9272012-12-12 20:46:15 +0000185void Input::endSequence() {
186}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000187
Nick Kledzikf60a9272012-12-12 20:46:15 +0000188bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000189 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000190 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000193 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000194 return true;
195 }
196 return false;
197}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000198
Nick Kledzikf60a9272012-12-12 20:46:15 +0000199void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000200 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201}
202
Justin Bogner64d2cdf2015-03-02 17:26:43 +0000203unsigned Input::beginFlowSequence() { return beginSequence(); }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000204
Nick Kledzikf60a9272012-12-12 20:46:15 +0000205bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000206 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000207 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000208 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000209 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000210 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211 return true;
212 }
213 return false;
214}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000215
Nick Kledzikf60a9272012-12-12 20:46:15 +0000216void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000217 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000218}
219
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000220void Input::endFlowSequence() {
221}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000222
223void Input::beginEnumScalar() {
224 ScalarMatchFound = false;
225}
226
227bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000228 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000229 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000230 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
231 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000232 ScalarMatchFound = true;
233 return true;
234 }
235 }
236 return false;
237}
238
Michael J. Spencer731cae32015-01-23 21:57:50 +0000239bool Input::matchEnumFallback() {
240 if (ScalarMatchFound)
241 return false;
242 ScalarMatchFound = true;
243 return true;
244}
245
Nick Kledzikf60a9272012-12-12 20:46:15 +0000246void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000247 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000248 setError(CurrentNode, "unknown enumerated scalar");
249 }
250}
251
Nick Kledzikf60a9272012-12-12 20:46:15 +0000252bool Input::beginBitSetScalar(bool &DoClear) {
253 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000256 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257 setError(CurrentNode, "expected sequence of bit values");
258 }
259 DoClear = true;
260 return true;
261}
262
263bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000264 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000265 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000266 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000267 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000268 for (auto &N : SQ->Entries) {
269 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000270 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000271 BitValuesUsed[Index] = true;
272 return true;
273 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000274 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000275 setError(CurrentNode, "unexpected scalar in sequence of bit values");
276 }
277 ++Index;
278 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000279 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280 setError(CurrentNode, "expected sequence of bit values");
281 }
282 return false;
283}
284
285void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000286 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000287 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
291 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000292 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000293 return;
294 }
295 }
296 }
297}
298
David Majnemer77880332014-04-10 07:37:33 +0000299void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000300 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000301 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000302 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000303 setError(CurrentNode, "unexpected scalar");
304 }
305}
306
307void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000308 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000309 this->setError(hnode->_node, message);
310}
311
312void Input::setError(Node *node, const Twine &message) {
313 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000314 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000315}
316
David Blaikied759fe52014-09-15 18:39:24 +0000317std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000318 SmallString<128> StringStorage;
319 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000320 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000321 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322 // Copy string to permanent storage
323 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000324 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000325 memcpy(Buf, &StringStorage[0], Len);
326 KeyStr = StringRef(Buf, Len);
327 }
David Blaikied759fe52014-09-15 18:39:24 +0000328 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000329 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000330 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000331 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000332 auto Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000333 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000334 break;
David Blaikied759fe52014-09-15 18:39:24 +0000335 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336 }
David Blaikied759fe52014-09-15 18:39:24 +0000337 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000338 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000339 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000340 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000341 Node *KeyNode = KVN.getKey();
342 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
343 if (!KeyScalar) {
344 setError(KeyNode, "Map key must be a scalar");
345 break;
346 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000347 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000348 StringRef KeyStr = KeyScalar->getValue(StringStorage);
349 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000350 // Copy string to permanent storage
351 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000352 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000353 memcpy(Buf, &StringStorage[0], Len);
354 KeyStr = StringRef(Buf, Len);
355 }
David Blaikied759fe52014-09-15 18:39:24 +0000356 auto ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000357 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000358 break;
David Blaikied759fe52014-09-15 18:39:24 +0000359 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000360 }
David Blaikied759fe52014-09-15 18:39:24 +0000361 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000362 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000363 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000364 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000365 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000366 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000367 }
368}
369
Nick Kledzikf60a9272012-12-12 20:46:15 +0000370bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000371 for (const char *K : ValidKeys) {
372 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000373 return true;
374 }
375 return false;
376}
377
378void Input::setError(const Twine &Message) {
379 this->setError(CurrentNode, Message);
380}
381
Aaron Ballman0e63e532013-08-15 23:17:53 +0000382bool Input::canElideEmptySequence() {
383 return false;
384}
385
Nick Kledzikf60a9272012-12-12 20:46:15 +0000386//===----------------------------------------------------------------------===//
387// Output
388//===----------------------------------------------------------------------===//
389
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000390Output::Output(raw_ostream &yout, void *context)
391 : IO(context),
392 Out(yout),
393 Column(0),
394 ColumnAtFlowStart(0),
395 NeedBitValueComma(false),
396 NeedFlowSequenceComma(false),
397 EnumerationMatchFound(false),
398 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000399}
400
401Output::~Output() {
402}
403
Nick Kledzik4761c602013-11-21 00:20:10 +0000404bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405 return true;
406}
407
408void Output::beginMapping() {
409 StateStack.push_back(inMapFirstKey);
410 NeedsNewLine = true;
411}
412
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000413bool Output::mapTag(StringRef Tag, bool Use) {
414 if (Use) {
415 this->output(" ");
416 this->output(Tag);
417 }
418 return Use;
419}
420
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421void Output::endMapping() {
422 StateStack.pop_back();
423}
424
Nick Kledzikf60a9272012-12-12 20:46:15 +0000425bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000426 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000427 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000428 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000429 this->newLineCheck();
430 this->paddedKey(Key);
431 return true;
432 }
433 return false;
434}
435
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000436void Output::postflightKey(void *) {
437 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000438 StateStack.pop_back();
439 StateStack.push_back(inMapOtherKey);
440 }
441}
442
443void Output::beginDocuments() {
444 this->outputUpToEndOfLine("---");
445}
446
447bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000448 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000449 this->outputUpToEndOfLine("\n---");
450 return true;
451}
452
453void Output::postflightDocument() {
454}
455
456void Output::endDocuments() {
457 output("\n...\n");
458}
459
460unsigned Output::beginSequence() {
461 StateStack.push_back(inSeq);
462 NeedsNewLine = true;
463 return 0;
464}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000465
Nick Kledzikf60a9272012-12-12 20:46:15 +0000466void Output::endSequence() {
467 StateStack.pop_back();
468}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000469
470bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000471 return true;
472}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000473
474void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000475}
476
477unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000478 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000479 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000480 ColumnAtFlowStart = Column;
481 output("[ ");
482 NeedFlowSequenceComma = false;
483 return 0;
484}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000485
Nick Kledzikf60a9272012-12-12 20:46:15 +0000486void Output::endFlowSequence() {
487 StateStack.pop_back();
488 this->outputUpToEndOfLine(" ]");
489}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000490
491bool Output::preflightFlowElement(unsigned, void *&) {
492 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000493 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000494 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000495 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000496 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000497 output(" ");
498 Column = ColumnAtFlowStart;
499 output(" ");
500 }
501 return true;
502}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000503
504void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000505 NeedFlowSequenceComma = true;
506}
507
Nick Kledzikf60a9272012-12-12 20:46:15 +0000508void Output::beginEnumScalar() {
509 EnumerationMatchFound = false;
510}
511
512bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000513 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000514 this->newLineCheck();
515 this->outputUpToEndOfLine(Str);
516 EnumerationMatchFound = true;
517 }
518 return false;
519}
520
Michael J. Spencer731cae32015-01-23 21:57:50 +0000521bool Output::matchEnumFallback() {
522 if (EnumerationMatchFound)
523 return false;
524 EnumerationMatchFound = true;
525 return true;
526}
527
Nick Kledzikf60a9272012-12-12 20:46:15 +0000528void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000529 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000530 llvm_unreachable("bad runtime enum value");
531}
532
Nick Kledzikf60a9272012-12-12 20:46:15 +0000533bool Output::beginBitSetScalar(bool &DoClear) {
534 this->newLineCheck();
535 output("[ ");
536 NeedBitValueComma = false;
537 DoClear = false;
538 return true;
539}
540
541bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000542 if (Matches) {
543 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000544 output(", ");
545 this->output(Str);
546 NeedBitValueComma = true;
547 }
548 return false;
549}
550
551void Output::endBitSetScalar() {
552 this->outputUpToEndOfLine(" ]");
553}
554
David Majnemer77880332014-04-10 07:37:33 +0000555void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000556 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000557 if (S.empty()) {
558 // Print '' for the empty string because leaving the field empty is not
559 // allowed.
560 this->outputUpToEndOfLine("''");
561 return;
562 }
David Majnemer77880332014-04-10 07:37:33 +0000563 if (!MustQuote) {
564 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000565 this->outputUpToEndOfLine(S);
566 return;
567 }
568 unsigned i = 0;
569 unsigned j = 0;
570 unsigned End = S.size();
571 output("'"); // Starting single quote.
572 const char *Base = S.data();
573 while (j < End) {
574 // Escape a single quote by doubling it.
575 if (S[j] == '\'') {
576 output(StringRef(&Base[i], j - i + 1));
577 output("'");
578 i = j + 1;
579 }
580 ++j;
581 }
582 output(StringRef(&Base[i], j - i));
583 this->outputUpToEndOfLine("'"); // Ending single quote.
584}
585
586void Output::setError(const Twine &message) {
587}
588
Aaron Ballman0e63e532013-08-15 23:17:53 +0000589bool Output::canElideEmptySequence() {
590 // Normally, with an optional key/value where the value is an empty sequence,
591 // the whole key/value can be not written. But, that produces wrong yaml
592 // if the key/value is the only thing in the map and the map is used in
593 // a sequence. This detects if the this sequence is the first key/value
594 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000595 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000596 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000597 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000598 return true;
599 return (StateStack[StateStack.size()-2] != inSeq);
600}
601
Nick Kledzikf60a9272012-12-12 20:46:15 +0000602void Output::output(StringRef s) {
603 Column += s.size();
604 Out << s;
605}
606
607void Output::outputUpToEndOfLine(StringRef s) {
608 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000609 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000610 NeedsNewLine = true;
611}
612
613void Output::outputNewLine() {
614 Out << "\n";
615 Column = 0;
616}
617
618// if seq at top, indent as if map, then add "- "
619// if seq in middle, use "- " if firstKey, else use " "
620//
621
622void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000623 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000624 return;
625 NeedsNewLine = false;
626
627 this->outputNewLine();
628
629 assert(StateStack.size() > 0);
630 unsigned Indent = StateStack.size() - 1;
631 bool OutputDash = false;
632
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000633 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000634 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000635 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
636 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000637 --Indent;
638 OutputDash = true;
639 }
640
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000641 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000642 output(" ");
643 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000644 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000645 output("- ");
646 }
647
648}
649
650void Output::paddedKey(StringRef key) {
651 output(key);
652 output(":");
653 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000654 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000655 output(&spaces[key.size()]);
656 else
657 output(" ");
658}
659
660//===----------------------------------------------------------------------===//
661// traits for built-in types
662//===----------------------------------------------------------------------===//
663
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000664void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
665 Out << (Val ? "true" : "false");
666}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000667
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000668StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
669 if (Scalar.equals("true")) {
670 Val = true;
671 return StringRef();
672 } else if (Scalar.equals("false")) {
673 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000674 return StringRef();
675 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000676 return "invalid boolean";
677}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000678
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000679void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
680 raw_ostream &Out) {
681 Out << Val;
682}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000683
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000684StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
685 StringRef &Val) {
686 Val = Scalar;
687 return StringRef();
688}
Alex Rosenbergf298f162015-01-26 18:02:18 +0000689
John Thompson48e018a2013-11-19 17:28:21 +0000690void ScalarTraits<std::string>::output(const std::string &Val, void *,
691 raw_ostream &Out) {
692 Out << Val;
693}
694
695StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
696 std::string &Val) {
697 Val = Scalar.str();
698 return StringRef();
699}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000700
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000701void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
702 raw_ostream &Out) {
703 // use temp uin32_t because ostream thinks uint8_t is a character
704 uint32_t Num = Val;
705 Out << Num;
706}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000707
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000708StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
709 unsigned long long n;
710 if (getAsUnsignedInteger(Scalar, 0, n))
711 return "invalid number";
712 if (n > 0xFF)
713 return "out of range number";
714 Val = n;
715 return StringRef();
716}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000717
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000718void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
719 raw_ostream &Out) {
720 Out << Val;
721}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000722
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000723StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
724 uint16_t &Val) {
725 unsigned long long n;
726 if (getAsUnsignedInteger(Scalar, 0, n))
727 return "invalid number";
728 if (n > 0xFFFF)
729 return "out of range number";
730 Val = n;
731 return StringRef();
732}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000733
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000734void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
735 raw_ostream &Out) {
736 Out << Val;
737}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000738
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000739StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
740 uint32_t &Val) {
741 unsigned long long n;
742 if (getAsUnsignedInteger(Scalar, 0, n))
743 return "invalid number";
744 if (n > 0xFFFFFFFFUL)
745 return "out of range number";
746 Val = n;
747 return StringRef();
748}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000749
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000750void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
751 raw_ostream &Out) {
752 Out << Val;
753}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000754
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000755StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
756 uint64_t &Val) {
757 unsigned long long N;
758 if (getAsUnsignedInteger(Scalar, 0, N))
759 return "invalid number";
760 Val = N;
761 return StringRef();
762}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000763
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000764void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
765 // use temp in32_t because ostream thinks int8_t is a character
766 int32_t Num = Val;
767 Out << Num;
768}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000769
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000770StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
771 long long N;
772 if (getAsSignedInteger(Scalar, 0, N))
773 return "invalid number";
774 if ((N > 127) || (N < -128))
775 return "out of range number";
776 Val = N;
777 return StringRef();
778}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000779
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000780void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
781 raw_ostream &Out) {
782 Out << Val;
783}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000784
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000785StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
786 long long N;
787 if (getAsSignedInteger(Scalar, 0, N))
788 return "invalid number";
789 if ((N > INT16_MAX) || (N < INT16_MIN))
790 return "out of range number";
791 Val = N;
792 return StringRef();
793}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000794
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000795void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
796 raw_ostream &Out) {
797 Out << Val;
798}
799
800StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
801 long long N;
802 if (getAsSignedInteger(Scalar, 0, N))
803 return "invalid number";
804 if ((N > INT32_MAX) || (N < INT32_MIN))
805 return "out of range number";
806 Val = N;
807 return StringRef();
808}
809
810void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
811 raw_ostream &Out) {
812 Out << Val;
813}
814
815StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
816 long long N;
817 if (getAsSignedInteger(Scalar, 0, N))
818 return "invalid number";
819 Val = N;
820 return StringRef();
821}
822
823void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000824 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000825}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000826
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000827StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
828 SmallString<32> buff(Scalar.begin(), Scalar.end());
829 char *end;
830 Val = strtod(buff.c_str(), &end);
831 if (*end != '\0')
832 return "invalid floating point number";
833 return StringRef();
834}
835
836void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000837 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000838}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000839
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000840StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
841 SmallString<32> buff(Scalar.begin(), Scalar.end());
842 char *end;
843 Val = strtod(buff.c_str(), &end);
844 if (*end != '\0')
845 return "invalid floating point number";
846 return StringRef();
847}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000848
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000849void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
850 uint8_t Num = Val;
851 Out << format("0x%02X", Num);
852}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000853
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000854StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
855 unsigned long long n;
856 if (getAsUnsignedInteger(Scalar, 0, n))
857 return "invalid hex8 number";
858 if (n > 0xFF)
859 return "out of range hex8 number";
860 Val = n;
861 return StringRef();
862}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000863
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000864void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
865 uint16_t Num = Val;
866 Out << format("0x%04X", Num);
867}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000868
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000869StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
870 unsigned long long n;
871 if (getAsUnsignedInteger(Scalar, 0, n))
872 return "invalid hex16 number";
873 if (n > 0xFFFF)
874 return "out of range hex16 number";
875 Val = n;
876 return StringRef();
877}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000878
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000879void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
880 uint32_t Num = Val;
881 Out << format("0x%08X", Num);
882}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000883
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000884StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
885 unsigned long long n;
886 if (getAsUnsignedInteger(Scalar, 0, n))
887 return "invalid hex32 number";
888 if (n > 0xFFFFFFFFUL)
889 return "out of range hex32 number";
890 Val = n;
891 return StringRef();
892}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000893
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000894void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
895 uint64_t Num = Val;
896 Out << format("0x%016llX", Num);
897}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000898
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000899StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
900 unsigned long long Num;
901 if (getAsUnsignedInteger(Scalar, 0, Num))
902 return "invalid hex64 number";
903 Val = Num;
904 return StringRef();
905}