]>
Commit | Line | Data |
---|---|---|
970d7e83 LB |
1 | //===- 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 | ||
1a4d82fc | 10 | #include "llvm/Support/Errc.h" |
970d7e83 LB |
11 | #include "llvm/Support/YAMLTraits.h" |
12 | #include "llvm/ADT/Twine.h" | |
13 | #include "llvm/Support/Casting.h" | |
14 | #include "llvm/Support/ErrorHandling.h" | |
15 | #include "llvm/Support/Format.h" | |
16 | #include "llvm/Support/YAMLParser.h" | |
17 | #include "llvm/Support/raw_ostream.h" | |
1a4d82fc | 18 | #include <cctype> |
970d7e83 LB |
19 | #include <cstring> |
20 | using namespace llvm; | |
21 | using namespace yaml; | |
22 | ||
23 | //===----------------------------------------------------------------------===// | |
24 | // IO | |
25 | //===----------------------------------------------------------------------===// | |
26 | ||
27 | IO::IO(void *Context) : Ctxt(Context) { | |
28 | } | |
29 | ||
30 | IO::~IO() { | |
31 | } | |
32 | ||
33 | void *IO::getContext() { | |
34 | return Ctxt; | |
35 | } | |
36 | ||
37 | void IO::setContext(void *Context) { | |
38 | Ctxt = Context; | |
39 | } | |
40 | ||
41 | //===----------------------------------------------------------------------===// | |
42 | // Input | |
43 | //===----------------------------------------------------------------------===// | |
44 | ||
1a4d82fc JJ |
45 | Input::Input(StringRef InputContent, |
46 | void *Ctxt, | |
47 | SourceMgr::DiagHandlerTy DiagHandler, | |
48 | void *DiagHandlerCtxt) | |
49 | : IO(Ctxt), | |
970d7e83 | 50 | Strm(new Stream(InputContent, SrcMgr)), |
1a4d82fc JJ |
51 | CurrentNode(nullptr) { |
52 | if (DiagHandler) | |
53 | SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); | |
970d7e83 LB |
54 | DocIterator = Strm->begin(); |
55 | } | |
56 | ||
57 | Input::~Input() { | |
970d7e83 LB |
58 | } |
59 | ||
1a4d82fc | 60 | std::error_code Input::error() { return EC; } |
970d7e83 | 61 | |
1a4d82fc JJ |
62 | // Pin the vtables to this file. |
63 | void Input::HNode::anchor() {} | |
64 | void Input::EmptyHNode::anchor() {} | |
65 | void Input::ScalarHNode::anchor() {} | |
66 | void Input::MapHNode::anchor() {} | |
67 | void Input::SequenceHNode::anchor() {} | |
970d7e83 LB |
68 | |
69 | bool Input::outputting() { | |
70 | return false; | |
71 | } | |
72 | ||
73 | bool Input::setCurrentDocument() { | |
74 | if (DocIterator != Strm->end()) { | |
75 | Node *N = DocIterator->getRoot(); | |
1a4d82fc JJ |
76 | if (!N) { |
77 | assert(Strm->failed() && "Root is NULL iff parsing failed"); | |
78 | EC = make_error_code(errc::invalid_argument); | |
79 | return false; | |
80 | } | |
81 | ||
970d7e83 LB |
82 | if (isa<NullNode>(N)) { |
83 | // Empty files are allowed and ignored | |
84 | ++DocIterator; | |
85 | return setCurrentDocument(); | |
86 | } | |
1a4d82fc | 87 | TopNode = this->createHNodes(N); |
970d7e83 LB |
88 | CurrentNode = TopNode.get(); |
89 | return true; | |
90 | } | |
91 | return false; | |
92 | } | |
93 | ||
1a4d82fc JJ |
94 | bool Input::nextDocument() { |
95 | return ++DocIterator != Strm->end(); | |
96 | } | |
97 | ||
98 | bool Input::mapTag(StringRef Tag, bool Default) { | |
99 | std::string foundTag = CurrentNode->_node->getVerbatimTag(); | |
100 | 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); | |
970d7e83 LB |
106 | } |
107 | ||
108 | void Input::beginMapping() { | |
109 | if (EC) | |
110 | return; | |
1a4d82fc JJ |
111 | // CurrentNode can be null if the document is empty. |
112 | MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); | |
970d7e83 LB |
113 | if (MN) { |
114 | MN->ValidKeys.clear(); | |
115 | } | |
116 | } | |
117 | ||
118 | bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, | |
119 | void *&SaveInfo) { | |
120 | UseDefault = false; | |
121 | if (EC) | |
122 | return false; | |
1a4d82fc JJ |
123 | |
124 | // CurrentNode is null for empty documents, which is an error in case required | |
125 | // nodes are present. | |
126 | if (!CurrentNode) { | |
127 | if (Required) | |
128 | EC = make_error_code(errc::invalid_argument); | |
129 | return false; | |
130 | } | |
131 | ||
970d7e83 LB |
132 | MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); |
133 | if (!MN) { | |
134 | setError(CurrentNode, "not a mapping"); | |
135 | return false; | |
136 | } | |
137 | MN->ValidKeys.push_back(Key); | |
1a4d82fc | 138 | HNode *Value = MN->Mapping[Key].get(); |
970d7e83 LB |
139 | if (!Value) { |
140 | if (Required) | |
141 | setError(CurrentNode, Twine("missing required key '") + Key + "'"); | |
142 | else | |
143 | UseDefault = true; | |
144 | return false; | |
145 | } | |
146 | SaveInfo = CurrentNode; | |
147 | CurrentNode = Value; | |
148 | return true; | |
149 | } | |
150 | ||
151 | void Input::postflightKey(void *saveInfo) { | |
152 | CurrentNode = reinterpret_cast<HNode *>(saveInfo); | |
153 | } | |
154 | ||
155 | void Input::endMapping() { | |
156 | if (EC) | |
157 | return; | |
1a4d82fc JJ |
158 | // CurrentNode can be null if the document is empty. |
159 | MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); | |
970d7e83 LB |
160 | if (!MN) |
161 | return; | |
1a4d82fc JJ |
162 | for (const auto &NN : MN->Mapping) { |
163 | if (!MN->isValidKey(NN.first())) { | |
164 | setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); | |
970d7e83 LB |
165 | break; |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | unsigned Input::beginSequence() { | |
171 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
172 | return SQ->Entries.size(); | |
173 | } | |
174 | return 0; | |
175 | } | |
176 | ||
177 | void Input::endSequence() { | |
178 | } | |
179 | ||
180 | bool Input::preflightElement(unsigned Index, void *&SaveInfo) { | |
181 | if (EC) | |
182 | return false; | |
183 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
184 | SaveInfo = CurrentNode; | |
1a4d82fc | 185 | CurrentNode = SQ->Entries[Index].get(); |
970d7e83 LB |
186 | return true; |
187 | } | |
188 | return false; | |
189 | } | |
190 | ||
191 | void Input::postflightElement(void *SaveInfo) { | |
192 | CurrentNode = reinterpret_cast<HNode *>(SaveInfo); | |
193 | } | |
194 | ||
195 | unsigned Input::beginFlowSequence() { | |
196 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
197 | return SQ->Entries.size(); | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
202 | bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) { | |
203 | if (EC) | |
204 | return false; | |
205 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
206 | SaveInfo = CurrentNode; | |
1a4d82fc | 207 | CurrentNode = SQ->Entries[index].get(); |
970d7e83 LB |
208 | return true; |
209 | } | |
210 | return false; | |
211 | } | |
212 | ||
213 | void Input::postflightFlowElement(void *SaveInfo) { | |
214 | CurrentNode = reinterpret_cast<HNode *>(SaveInfo); | |
215 | } | |
216 | ||
217 | void Input::endFlowSequence() { | |
218 | } | |
219 | ||
220 | void Input::beginEnumScalar() { | |
221 | ScalarMatchFound = false; | |
222 | } | |
223 | ||
224 | bool Input::matchEnumScalar(const char *Str, bool) { | |
225 | if (ScalarMatchFound) | |
226 | return false; | |
227 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { | |
228 | if (SN->value().equals(Str)) { | |
229 | ScalarMatchFound = true; | |
230 | return true; | |
231 | } | |
232 | } | |
233 | return false; | |
234 | } | |
235 | ||
236 | void Input::endEnumScalar() { | |
237 | if (!ScalarMatchFound) { | |
238 | setError(CurrentNode, "unknown enumerated scalar"); | |
239 | } | |
240 | } | |
241 | ||
242 | bool Input::beginBitSetScalar(bool &DoClear) { | |
243 | BitValuesUsed.clear(); | |
244 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
245 | BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false); | |
246 | } else { | |
247 | setError(CurrentNode, "expected sequence of bit values"); | |
248 | } | |
249 | DoClear = true; | |
250 | return true; | |
251 | } | |
252 | ||
253 | bool Input::bitSetMatch(const char *Str, bool) { | |
254 | if (EC) | |
255 | return false; | |
256 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
257 | unsigned Index = 0; | |
1a4d82fc JJ |
258 | for (auto &N : SQ->Entries) { |
259 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) { | |
970d7e83 LB |
260 | if (SN->value().equals(Str)) { |
261 | BitValuesUsed[Index] = true; | |
262 | return true; | |
263 | } | |
264 | } else { | |
265 | setError(CurrentNode, "unexpected scalar in sequence of bit values"); | |
266 | } | |
267 | ++Index; | |
268 | } | |
269 | } else { | |
270 | setError(CurrentNode, "expected sequence of bit values"); | |
271 | } | |
272 | return false; | |
273 | } | |
274 | ||
275 | void Input::endBitSetScalar() { | |
276 | if (EC) | |
277 | return; | |
278 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { | |
279 | assert(BitValuesUsed.size() == SQ->Entries.size()); | |
280 | for (unsigned i = 0; i < SQ->Entries.size(); ++i) { | |
281 | if (!BitValuesUsed[i]) { | |
1a4d82fc | 282 | setError(SQ->Entries[i].get(), "unknown bit value"); |
970d7e83 LB |
283 | return; |
284 | } | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
1a4d82fc | 289 | void Input::scalarString(StringRef &S, bool) { |
970d7e83 LB |
290 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { |
291 | S = SN->value(); | |
292 | } else { | |
293 | setError(CurrentNode, "unexpected scalar"); | |
294 | } | |
295 | } | |
296 | ||
297 | void Input::setError(HNode *hnode, const Twine &message) { | |
1a4d82fc | 298 | assert(hnode && "HNode must not be NULL"); |
970d7e83 LB |
299 | this->setError(hnode->_node, message); |
300 | } | |
301 | ||
302 | void Input::setError(Node *node, const Twine &message) { | |
303 | Strm->printError(node, message); | |
304 | EC = make_error_code(errc::invalid_argument); | |
305 | } | |
306 | ||
1a4d82fc | 307 | std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { |
970d7e83 LB |
308 | SmallString<128> StringStorage; |
309 | if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { | |
310 | StringRef KeyStr = SN->getValue(StringStorage); | |
311 | if (!StringStorage.empty()) { | |
312 | // Copy string to permanent storage | |
313 | unsigned Len = StringStorage.size(); | |
314 | char *Buf = StringAllocator.Allocate<char>(Len); | |
315 | memcpy(Buf, &StringStorage[0], Len); | |
316 | KeyStr = StringRef(Buf, Len); | |
317 | } | |
1a4d82fc | 318 | return llvm::make_unique<ScalarHNode>(N, KeyStr); |
970d7e83 | 319 | } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { |
1a4d82fc JJ |
320 | auto SQHNode = llvm::make_unique<SequenceHNode>(N); |
321 | for (Node &SN : *SQ) { | |
322 | auto Entry = this->createHNodes(&SN); | |
970d7e83 LB |
323 | if (EC) |
324 | break; | |
1a4d82fc | 325 | SQHNode->Entries.push_back(std::move(Entry)); |
970d7e83 | 326 | } |
1a4d82fc | 327 | return std::move(SQHNode); |
970d7e83 | 328 | } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { |
1a4d82fc JJ |
329 | auto mapHNode = llvm::make_unique<MapHNode>(N); |
330 | for (KeyValueNode &KVN : *Map) { | |
331 | Node *KeyNode = KVN.getKey(); | |
332 | ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode); | |
333 | if (!KeyScalar) { | |
334 | setError(KeyNode, "Map key must be a scalar"); | |
335 | break; | |
336 | } | |
970d7e83 LB |
337 | StringStorage.clear(); |
338 | StringRef KeyStr = KeyScalar->getValue(StringStorage); | |
339 | if (!StringStorage.empty()) { | |
340 | // Copy string to permanent storage | |
341 | unsigned Len = StringStorage.size(); | |
342 | char *Buf = StringAllocator.Allocate<char>(Len); | |
343 | memcpy(Buf, &StringStorage[0], Len); | |
344 | KeyStr = StringRef(Buf, Len); | |
345 | } | |
1a4d82fc | 346 | auto ValueHNode = this->createHNodes(KVN.getValue()); |
970d7e83 LB |
347 | if (EC) |
348 | break; | |
1a4d82fc | 349 | mapHNode->Mapping[KeyStr] = std::move(ValueHNode); |
970d7e83 | 350 | } |
1a4d82fc | 351 | return std::move(mapHNode); |
970d7e83 | 352 | } else if (isa<NullNode>(N)) { |
1a4d82fc | 353 | return llvm::make_unique<EmptyHNode>(N); |
970d7e83 LB |
354 | } else { |
355 | setError(N, "unknown node kind"); | |
1a4d82fc | 356 | return nullptr; |
970d7e83 LB |
357 | } |
358 | } | |
359 | ||
360 | bool Input::MapHNode::isValidKey(StringRef Key) { | |
1a4d82fc JJ |
361 | for (const char *K : ValidKeys) { |
362 | if (Key.equals(K)) | |
970d7e83 LB |
363 | return true; |
364 | } | |
365 | return false; | |
366 | } | |
367 | ||
368 | void Input::setError(const Twine &Message) { | |
369 | this->setError(CurrentNode, Message); | |
370 | } | |
371 | ||
1a4d82fc JJ |
372 | bool Input::canElideEmptySequence() { |
373 | return false; | |
970d7e83 LB |
374 | } |
375 | ||
970d7e83 LB |
376 | //===----------------------------------------------------------------------===// |
377 | // Output | |
378 | //===----------------------------------------------------------------------===// | |
379 | ||
380 | Output::Output(raw_ostream &yout, void *context) | |
381 | : IO(context), | |
382 | Out(yout), | |
383 | Column(0), | |
384 | ColumnAtFlowStart(0), | |
385 | NeedBitValueComma(false), | |
386 | NeedFlowSequenceComma(false), | |
387 | EnumerationMatchFound(false), | |
388 | NeedsNewLine(false) { | |
389 | } | |
390 | ||
391 | Output::~Output() { | |
392 | } | |
393 | ||
394 | bool Output::outputting() { | |
395 | return true; | |
396 | } | |
397 | ||
398 | void Output::beginMapping() { | |
399 | StateStack.push_back(inMapFirstKey); | |
400 | NeedsNewLine = true; | |
401 | } | |
402 | ||
1a4d82fc JJ |
403 | bool Output::mapTag(StringRef Tag, bool Use) { |
404 | if (Use) { | |
405 | this->output(" "); | |
406 | this->output(Tag); | |
407 | } | |
408 | return Use; | |
409 | } | |
410 | ||
970d7e83 LB |
411 | void Output::endMapping() { |
412 | StateStack.pop_back(); | |
413 | } | |
414 | ||
415 | bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, | |
416 | bool &UseDefault, void *&) { | |
417 | UseDefault = false; | |
418 | if (Required || !SameAsDefault) { | |
419 | this->newLineCheck(); | |
420 | this->paddedKey(Key); | |
421 | return true; | |
422 | } | |
423 | return false; | |
424 | } | |
425 | ||
426 | void Output::postflightKey(void *) { | |
427 | if (StateStack.back() == inMapFirstKey) { | |
428 | StateStack.pop_back(); | |
429 | StateStack.push_back(inMapOtherKey); | |
430 | } | |
431 | } | |
432 | ||
433 | void Output::beginDocuments() { | |
434 | this->outputUpToEndOfLine("---"); | |
435 | } | |
436 | ||
437 | bool Output::preflightDocument(unsigned index) { | |
438 | if (index > 0) | |
439 | this->outputUpToEndOfLine("\n---"); | |
440 | return true; | |
441 | } | |
442 | ||
443 | void Output::postflightDocument() { | |
444 | } | |
445 | ||
446 | void Output::endDocuments() { | |
447 | output("\n...\n"); | |
448 | } | |
449 | ||
450 | unsigned Output::beginSequence() { | |
451 | StateStack.push_back(inSeq); | |
452 | NeedsNewLine = true; | |
453 | return 0; | |
454 | } | |
455 | ||
456 | void Output::endSequence() { | |
457 | StateStack.pop_back(); | |
458 | } | |
459 | ||
460 | bool Output::preflightElement(unsigned, void *&) { | |
461 | return true; | |
462 | } | |
463 | ||
464 | void Output::postflightElement(void *) { | |
465 | } | |
466 | ||
467 | unsigned Output::beginFlowSequence() { | |
468 | StateStack.push_back(inFlowSeq); | |
469 | this->newLineCheck(); | |
470 | ColumnAtFlowStart = Column; | |
471 | output("[ "); | |
472 | NeedFlowSequenceComma = false; | |
473 | return 0; | |
474 | } | |
475 | ||
476 | void Output::endFlowSequence() { | |
477 | StateStack.pop_back(); | |
478 | this->outputUpToEndOfLine(" ]"); | |
479 | } | |
480 | ||
481 | bool Output::preflightFlowElement(unsigned, void *&) { | |
482 | if (NeedFlowSequenceComma) | |
483 | output(", "); | |
484 | if (Column > 70) { | |
485 | output("\n"); | |
486 | for (int i = 0; i < ColumnAtFlowStart; ++i) | |
487 | output(" "); | |
488 | Column = ColumnAtFlowStart; | |
489 | output(" "); | |
490 | } | |
491 | return true; | |
492 | } | |
493 | ||
494 | void Output::postflightFlowElement(void *) { | |
495 | NeedFlowSequenceComma = true; | |
496 | } | |
497 | ||
498 | void Output::beginEnumScalar() { | |
499 | EnumerationMatchFound = false; | |
500 | } | |
501 | ||
502 | bool Output::matchEnumScalar(const char *Str, bool Match) { | |
503 | if (Match && !EnumerationMatchFound) { | |
504 | this->newLineCheck(); | |
505 | this->outputUpToEndOfLine(Str); | |
506 | EnumerationMatchFound = true; | |
507 | } | |
508 | return false; | |
509 | } | |
510 | ||
511 | void Output::endEnumScalar() { | |
512 | if (!EnumerationMatchFound) | |
513 | llvm_unreachable("bad runtime enum value"); | |
514 | } | |
515 | ||
516 | bool Output::beginBitSetScalar(bool &DoClear) { | |
517 | this->newLineCheck(); | |
518 | output("[ "); | |
519 | NeedBitValueComma = false; | |
520 | DoClear = false; | |
521 | return true; | |
522 | } | |
523 | ||
524 | bool Output::bitSetMatch(const char *Str, bool Matches) { | |
525 | if (Matches) { | |
526 | if (NeedBitValueComma) | |
527 | output(", "); | |
528 | this->output(Str); | |
529 | NeedBitValueComma = true; | |
530 | } | |
531 | return false; | |
532 | } | |
533 | ||
534 | void Output::endBitSetScalar() { | |
535 | this->outputUpToEndOfLine(" ]"); | |
536 | } | |
537 | ||
1a4d82fc | 538 | void Output::scalarString(StringRef &S, bool MustQuote) { |
970d7e83 | 539 | this->newLineCheck(); |
1a4d82fc JJ |
540 | if (S.empty()) { |
541 | // Print '' for the empty string because leaving the field empty is not | |
542 | // allowed. | |
543 | this->outputUpToEndOfLine("''"); | |
544 | return; | |
545 | } | |
546 | if (!MustQuote) { | |
547 | // Only quote if we must. | |
970d7e83 LB |
548 | this->outputUpToEndOfLine(S); |
549 | return; | |
550 | } | |
551 | unsigned i = 0; | |
552 | unsigned j = 0; | |
553 | unsigned End = S.size(); | |
554 | output("'"); // Starting single quote. | |
555 | const char *Base = S.data(); | |
556 | while (j < End) { | |
557 | // Escape a single quote by doubling it. | |
558 | if (S[j] == '\'') { | |
559 | output(StringRef(&Base[i], j - i + 1)); | |
560 | output("'"); | |
561 | i = j + 1; | |
562 | } | |
563 | ++j; | |
564 | } | |
565 | output(StringRef(&Base[i], j - i)); | |
566 | this->outputUpToEndOfLine("'"); // Ending single quote. | |
567 | } | |
568 | ||
569 | void Output::setError(const Twine &message) { | |
570 | } | |
571 | ||
1a4d82fc JJ |
572 | bool Output::canElideEmptySequence() { |
573 | // Normally, with an optional key/value where the value is an empty sequence, | |
574 | // the whole key/value can be not written. But, that produces wrong yaml | |
575 | // if the key/value is the only thing in the map and the map is used in | |
576 | // a sequence. This detects if the this sequence is the first key/value | |
577 | // in map that itself is embedded in a sequnce. | |
578 | if (StateStack.size() < 2) | |
579 | return true; | |
580 | if (StateStack.back() != inMapFirstKey) | |
581 | return true; | |
582 | return (StateStack[StateStack.size()-2] != inSeq); | |
583 | } | |
584 | ||
970d7e83 LB |
585 | void Output::output(StringRef s) { |
586 | Column += s.size(); | |
587 | Out << s; | |
588 | } | |
589 | ||
590 | void Output::outputUpToEndOfLine(StringRef s) { | |
591 | this->output(s); | |
592 | if (StateStack.empty() || StateStack.back() != inFlowSeq) | |
593 | NeedsNewLine = true; | |
594 | } | |
595 | ||
596 | void Output::outputNewLine() { | |
597 | Out << "\n"; | |
598 | Column = 0; | |
599 | } | |
600 | ||
601 | // if seq at top, indent as if map, then add "- " | |
602 | // if seq in middle, use "- " if firstKey, else use " " | |
603 | // | |
604 | ||
605 | void Output::newLineCheck() { | |
606 | if (!NeedsNewLine) | |
607 | return; | |
608 | NeedsNewLine = false; | |
609 | ||
610 | this->outputNewLine(); | |
611 | ||
612 | assert(StateStack.size() > 0); | |
613 | unsigned Indent = StateStack.size() - 1; | |
614 | bool OutputDash = false; | |
615 | ||
616 | if (StateStack.back() == inSeq) { | |
617 | OutputDash = true; | |
618 | } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) && | |
619 | (StateStack[StateStack.size() - 2] == inSeq)) { | |
620 | --Indent; | |
621 | OutputDash = true; | |
622 | } | |
623 | ||
624 | for (unsigned i = 0; i < Indent; ++i) { | |
625 | output(" "); | |
626 | } | |
627 | if (OutputDash) { | |
628 | output("- "); | |
629 | } | |
630 | ||
631 | } | |
632 | ||
633 | void Output::paddedKey(StringRef key) { | |
634 | output(key); | |
635 | output(":"); | |
636 | const char *spaces = " "; | |
637 | if (key.size() < strlen(spaces)) | |
638 | output(&spaces[key.size()]); | |
639 | else | |
640 | output(" "); | |
641 | } | |
642 | ||
643 | //===----------------------------------------------------------------------===// | |
644 | // traits for built-in types | |
645 | //===----------------------------------------------------------------------===// | |
646 | ||
647 | void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { | |
648 | Out << (Val ? "true" : "false"); | |
649 | } | |
650 | ||
651 | StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) { | |
652 | if (Scalar.equals("true")) { | |
653 | Val = true; | |
654 | return StringRef(); | |
655 | } else if (Scalar.equals("false")) { | |
656 | Val = false; | |
657 | return StringRef(); | |
658 | } | |
659 | return "invalid boolean"; | |
660 | } | |
661 | ||
662 | void ScalarTraits<StringRef>::output(const StringRef &Val, void *, | |
663 | raw_ostream &Out) { | |
664 | Out << Val; | |
665 | } | |
666 | ||
667 | StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, | |
668 | StringRef &Val) { | |
669 | Val = Scalar; | |
670 | return StringRef(); | |
671 | } | |
1a4d82fc JJ |
672 | |
673 | void ScalarTraits<std::string>::output(const std::string &Val, void *, | |
674 | raw_ostream &Out) { | |
675 | Out << Val; | |
676 | } | |
677 | ||
678 | StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, | |
679 | std::string &Val) { | |
680 | Val = Scalar.str(); | |
681 | return StringRef(); | |
682 | } | |
970d7e83 LB |
683 | |
684 | void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *, | |
685 | raw_ostream &Out) { | |
686 | // use temp uin32_t because ostream thinks uint8_t is a character | |
687 | uint32_t Num = Val; | |
688 | Out << Num; | |
689 | } | |
690 | ||
691 | StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) { | |
692 | unsigned long long n; | |
693 | if (getAsUnsignedInteger(Scalar, 0, n)) | |
694 | return "invalid number"; | |
695 | if (n > 0xFF) | |
696 | return "out of range number"; | |
697 | Val = n; | |
698 | return StringRef(); | |
699 | } | |
700 | ||
701 | void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, | |
702 | raw_ostream &Out) { | |
703 | Out << Val; | |
704 | } | |
705 | ||
706 | StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *, | |
707 | uint16_t &Val) { | |
708 | unsigned long long n; | |
709 | if (getAsUnsignedInteger(Scalar, 0, n)) | |
710 | return "invalid number"; | |
711 | if (n > 0xFFFF) | |
712 | return "out of range number"; | |
713 | Val = n; | |
714 | return StringRef(); | |
715 | } | |
716 | ||
717 | void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, | |
718 | raw_ostream &Out) { | |
719 | Out << Val; | |
720 | } | |
721 | ||
722 | StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *, | |
723 | uint32_t &Val) { | |
724 | unsigned long long n; | |
725 | if (getAsUnsignedInteger(Scalar, 0, n)) | |
726 | return "invalid number"; | |
727 | if (n > 0xFFFFFFFFUL) | |
728 | return "out of range number"; | |
729 | Val = n; | |
730 | return StringRef(); | |
731 | } | |
732 | ||
733 | void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, | |
734 | raw_ostream &Out) { | |
735 | Out << Val; | |
736 | } | |
737 | ||
738 | StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *, | |
739 | uint64_t &Val) { | |
740 | unsigned long long N; | |
741 | if (getAsUnsignedInteger(Scalar, 0, N)) | |
742 | return "invalid number"; | |
743 | Val = N; | |
744 | return StringRef(); | |
745 | } | |
746 | ||
747 | void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) { | |
748 | // use temp in32_t because ostream thinks int8_t is a character | |
749 | int32_t Num = Val; | |
750 | Out << Num; | |
751 | } | |
752 | ||
753 | StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) { | |
754 | long long N; | |
755 | if (getAsSignedInteger(Scalar, 0, N)) | |
756 | return "invalid number"; | |
757 | if ((N > 127) || (N < -128)) | |
758 | return "out of range number"; | |
759 | Val = N; | |
760 | return StringRef(); | |
761 | } | |
762 | ||
763 | void ScalarTraits<int16_t>::output(const int16_t &Val, void *, | |
764 | raw_ostream &Out) { | |
765 | Out << Val; | |
766 | } | |
767 | ||
768 | StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) { | |
769 | long long N; | |
770 | if (getAsSignedInteger(Scalar, 0, N)) | |
771 | return "invalid number"; | |
772 | if ((N > INT16_MAX) || (N < INT16_MIN)) | |
773 | return "out of range number"; | |
774 | Val = N; | |
775 | return StringRef(); | |
776 | } | |
777 | ||
778 | void ScalarTraits<int32_t>::output(const int32_t &Val, void *, | |
779 | raw_ostream &Out) { | |
780 | Out << Val; | |
781 | } | |
782 | ||
783 | StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) { | |
784 | long long N; | |
785 | if (getAsSignedInteger(Scalar, 0, N)) | |
786 | return "invalid number"; | |
787 | if ((N > INT32_MAX) || (N < INT32_MIN)) | |
788 | return "out of range number"; | |
789 | Val = N; | |
790 | return StringRef(); | |
791 | } | |
792 | ||
793 | void ScalarTraits<int64_t>::output(const int64_t &Val, void *, | |
794 | raw_ostream &Out) { | |
795 | Out << Val; | |
796 | } | |
797 | ||
798 | StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) { | |
799 | long long N; | |
800 | if (getAsSignedInteger(Scalar, 0, N)) | |
801 | return "invalid number"; | |
802 | Val = N; | |
803 | return StringRef(); | |
804 | } | |
805 | ||
806 | void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { | |
807 | Out << format("%g", Val); | |
808 | } | |
809 | ||
810 | StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) { | |
811 | SmallString<32> buff(Scalar.begin(), Scalar.end()); | |
812 | char *end; | |
813 | Val = strtod(buff.c_str(), &end); | |
814 | if (*end != '\0') | |
815 | return "invalid floating point number"; | |
816 | return StringRef(); | |
817 | } | |
818 | ||
819 | void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { | |
820 | Out << format("%g", Val); | |
821 | } | |
822 | ||
823 | StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) { | |
824 | SmallString<32> buff(Scalar.begin(), Scalar.end()); | |
825 | char *end; | |
826 | Val = strtod(buff.c_str(), &end); | |
827 | if (*end != '\0') | |
828 | return "invalid floating point number"; | |
829 | return StringRef(); | |
830 | } | |
831 | ||
832 | void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { | |
833 | uint8_t Num = Val; | |
834 | Out << format("0x%02X", Num); | |
835 | } | |
836 | ||
837 | StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) { | |
838 | unsigned long long n; | |
839 | if (getAsUnsignedInteger(Scalar, 0, n)) | |
840 | return "invalid hex8 number"; | |
841 | if (n > 0xFF) | |
842 | return "out of range hex8 number"; | |
843 | Val = n; | |
844 | return StringRef(); | |
845 | } | |
846 | ||
847 | void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { | |
848 | uint16_t Num = Val; | |
849 | Out << format("0x%04X", Num); | |
850 | } | |
851 | ||
852 | StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) { | |
853 | unsigned long long n; | |
854 | if (getAsUnsignedInteger(Scalar, 0, n)) | |
855 | return "invalid hex16 number"; | |
856 | if (n > 0xFFFF) | |
857 | return "out of range hex16 number"; | |
858 | Val = n; | |
859 | return StringRef(); | |
860 | } | |
861 | ||
862 | void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { | |
863 | uint32_t Num = Val; | |
864 | Out << format("0x%08X", Num); | |
865 | } | |
866 | ||
867 | StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) { | |
868 | unsigned long long n; | |
869 | if (getAsUnsignedInteger(Scalar, 0, n)) | |
870 | return "invalid hex32 number"; | |
871 | if (n > 0xFFFFFFFFUL) | |
872 | return "out of range hex32 number"; | |
873 | Val = n; | |
874 | return StringRef(); | |
875 | } | |
876 | ||
877 | void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { | |
878 | uint64_t Num = Val; | |
879 | Out << format("0x%016llX", Num); | |
880 | } | |
881 | ||
882 | StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) { | |
883 | unsigned long long Num; | |
884 | if (getAsUnsignedInteger(Scalar, 0, Num)) | |
885 | return "invalid hex64 number"; | |
886 | Val = Num; | |
887 | return StringRef(); | |
888 | } |