1 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/YAMLTraits.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/StringExtras.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/ADT/Twine.h" 15 #include "llvm/Support/Casting.h" 16 #include "llvm/Support/Errc.h" 17 #include "llvm/Support/ErrorHandling.h" 18 #include "llvm/Support/Format.h" 19 #include "llvm/Support/LineIterator.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #include "llvm/Support/Unicode.h" 22 #include "llvm/Support/YAMLParser.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 #include <cassert> 26 #include <cstdint> 27 #include <cstdlib> 28 #include <cstring> 29 #include <string> 30 #include <vector> 31 32 using namespace llvm; 33 using namespace yaml; 34 35 //===----------------------------------------------------------------------===// 36 // IO 37 //===----------------------------------------------------------------------===// 38 39 IO::IO(void *Context) : Ctxt(Context) {} 40 41 IO::~IO() = default; 42 43 void *IO::getContext() { 44 return Ctxt; 45 } 46 47 void IO::setContext(void *Context) { 48 Ctxt = Context; 49 } 50 51 //===----------------------------------------------------------------------===// 52 // Input 53 //===----------------------------------------------------------------------===// 54 55 Input::Input(StringRef InputContent, void *Ctxt, 56 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt) 57 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) { 58 if (DiagHandler) 59 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); 60 DocIterator = Strm->begin(); 61 } 62 63 Input::Input(MemoryBufferRef Input, void *Ctxt, 64 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt) 65 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) { 66 if (DiagHandler) 67 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); 68 DocIterator = Strm->begin(); 69 } 70 71 Input::~Input() = default; 72 73 std::error_code Input::error() { return EC; } 74 75 // Pin the vtables to this file. 76 void Input::HNode::anchor() {} 77 void Input::EmptyHNode::anchor() {} 78 void Input::ScalarHNode::anchor() {} 79 void Input::MapHNode::anchor() {} 80 void Input::SequenceHNode::anchor() {} 81 82 bool Input::outputting() { 83 return false; 84 } 85 86 bool Input::setCurrentDocument() { 87 if (DocIterator != Strm->end()) { 88 Node *N = DocIterator->getRoot(); 89 if (!N) { 90 assert(Strm->failed() && "Root is NULL iff parsing failed"); 91 EC = make_error_code(errc::invalid_argument); 92 return false; 93 } 94 95 if (isa<NullNode>(N)) { 96 // Empty files are allowed and ignored 97 ++DocIterator; 98 return setCurrentDocument(); 99 } 100 TopNode = createHNodes(N); 101 CurrentNode = TopNode.get(); 102 return true; 103 } 104 return false; 105 } 106 107 bool Input::nextDocument() { 108 return ++DocIterator != Strm->end(); 109 } 110 111 const Node *Input::getCurrentNode() const { 112 return CurrentNode ? CurrentNode->_node : nullptr; 113 } 114 115 bool Input::mapTag(StringRef Tag, bool Default) { 116 // CurrentNode can be null if setCurrentDocument() was unable to 117 // parse the document because it was invalid or empty. 118 if (!CurrentNode) 119 return false; 120 121 std::string foundTag = CurrentNode->_node->getVerbatimTag(); 122 if (foundTag.empty()) { 123 // If no tag found and 'Tag' is the default, say it was found. 124 return Default; 125 } 126 // Return true iff found tag matches supplied tag. 127 return Tag.equals(foundTag); 128 } 129 130 void Input::beginMapping() { 131 if (EC) 132 return; 133 // CurrentNode can be null if the document is empty. 134 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 135 if (MN) { 136 MN->ValidKeys.clear(); 137 } 138 } 139 140 std::vector<StringRef> Input::keys() { 141 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); 142 std::vector<StringRef> Ret; 143 if (!MN) { 144 setError(CurrentNode, "not a mapping"); 145 return Ret; 146 } 147 for (auto &P : MN->Mapping) 148 Ret.push_back(P.first()); 149 return Ret; 150 } 151 152 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, 153 void *&SaveInfo) { 154 UseDefault = false; 155 if (EC) 156 return false; 157 158 // CurrentNode is null for empty documents, which is an error in case required 159 // nodes are present. 160 if (!CurrentNode) { 161 if (Required) 162 EC = make_error_code(errc::invalid_argument); 163 return false; 164 } 165 166 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); 167 if (!MN) { 168 if (Required || !isa<EmptyHNode>(CurrentNode)) 169 setError(CurrentNode, "not a mapping"); 170 return false; 171 } 172 MN->ValidKeys.push_back(Key); 173 HNode *Value = MN->Mapping[Key].get(); 174 if (!Value) { 175 if (Required) 176 setError(CurrentNode, Twine("missing required key '") + Key + "'"); 177 else 178 UseDefault = true; 179 return false; 180 } 181 SaveInfo = CurrentNode; 182 CurrentNode = Value; 183 return true; 184 } 185 186 void Input::postflightKey(void *saveInfo) { 187 CurrentNode = reinterpret_cast<HNode *>(saveInfo); 188 } 189 190 void Input::endMapping() { 191 if (EC) 192 return; 193 // CurrentNode can be null if the document is empty. 194 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 195 if (!MN) 196 return; 197 for (const auto &NN : MN->Mapping) { 198 if (!is_contained(MN->ValidKeys, NN.first())) { 199 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); 200 break; 201 } 202 } 203 } 204 205 void Input::beginFlowMapping() { beginMapping(); } 206 207 void Input::endFlowMapping() { endMapping(); } 208 209 unsigned Input::beginSequence() { 210 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) 211 return SQ->Entries.size(); 212 if (isa<EmptyHNode>(CurrentNode)) 213 return 0; 214 // Treat case where there's a scalar "null" value as an empty sequence. 215 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 216 if (isNull(SN->value())) 217 return 0; 218 } 219 // Any other type of HNode is an error. 220 setError(CurrentNode, "not a sequence"); 221 return 0; 222 } 223 224 void Input::endSequence() { 225 } 226 227 bool Input::preflightElement(unsigned Index, void *&SaveInfo) { 228 if (EC) 229 return false; 230 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 231 SaveInfo = CurrentNode; 232 CurrentNode = SQ->Entries[Index].get(); 233 return true; 234 } 235 return false; 236 } 237 238 void Input::postflightElement(void *SaveInfo) { 239 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 240 } 241 242 unsigned Input::beginFlowSequence() { return beginSequence(); } 243 244 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) { 245 if (EC) 246 return false; 247 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 248 SaveInfo = CurrentNode; 249 CurrentNode = SQ->Entries[index].get(); 250 return true; 251 } 252 return false; 253 } 254 255 void Input::postflightFlowElement(void *SaveInfo) { 256 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 257 } 258 259 void Input::endFlowSequence() { 260 } 261 262 void Input::beginEnumScalar() { 263 ScalarMatchFound = false; 264 } 265 266 bool Input::matchEnumScalar(const char *Str, bool) { 267 if (ScalarMatchFound) 268 return false; 269 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 270 if (SN->value().equals(Str)) { 271 ScalarMatchFound = true; 272 return true; 273 } 274 } 275 return false; 276 } 277 278 bool Input::matchEnumFallback() { 279 if (ScalarMatchFound) 280 return false; 281 ScalarMatchFound = true; 282 return true; 283 } 284 285 void Input::endEnumScalar() { 286 if (!ScalarMatchFound) { 287 setError(CurrentNode, "unknown enumerated scalar"); 288 } 289 } 290 291 bool Input::beginBitSetScalar(bool &DoClear) { 292 BitValuesUsed.clear(); 293 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 294 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false); 295 } else { 296 setError(CurrentNode, "expected sequence of bit values"); 297 } 298 DoClear = true; 299 return true; 300 } 301 302 bool Input::bitSetMatch(const char *Str, bool) { 303 if (EC) 304 return false; 305 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 306 unsigned Index = 0; 307 for (auto &N : SQ->Entries) { 308 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) { 309 if (SN->value().equals(Str)) { 310 BitValuesUsed[Index] = true; 311 return true; 312 } 313 } else { 314 setError(CurrentNode, "unexpected scalar in sequence of bit values"); 315 } 316 ++Index; 317 } 318 } else { 319 setError(CurrentNode, "expected sequence of bit values"); 320 } 321 return false; 322 } 323 324 void Input::endBitSetScalar() { 325 if (EC) 326 return; 327 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 328 assert(BitValuesUsed.size() == SQ->Entries.size()); 329 for (unsigned i = 0; i < SQ->Entries.size(); ++i) { 330 if (!BitValuesUsed[i]) { 331 setError(SQ->Entries[i].get(), "unknown bit value"); 332 return; 333 } 334 } 335 } 336 } 337 338 void Input::scalarString(StringRef &S, QuotingType) { 339 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 340 S = SN->value(); 341 } else { 342 setError(CurrentNode, "unexpected scalar"); 343 } 344 } 345 346 void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); } 347 348 void Input::scalarTag(std::string &Tag) { 349 Tag = CurrentNode->_node->getVerbatimTag(); 350 } 351 352 void Input::setError(HNode *hnode, const Twine &message) { 353 assert(hnode && "HNode must not be NULL"); 354 setError(hnode->_node, message); 355 } 356 357 NodeKind Input::getNodeKind() { 358 if (isa<ScalarHNode>(CurrentNode)) 359 return NodeKind::Scalar; 360 else if (isa<MapHNode>(CurrentNode)) 361 return NodeKind::Map; 362 else if (isa<SequenceHNode>(CurrentNode)) 363 return NodeKind::Sequence; 364 llvm_unreachable("Unsupported node kind"); 365 } 366 367 void Input::setError(Node *node, const Twine &message) { 368 Strm->printError(node, message); 369 EC = make_error_code(errc::invalid_argument); 370 } 371 372 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { 373 SmallString<128> StringStorage; 374 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { 375 StringRef KeyStr = SN->getValue(StringStorage); 376 if (!StringStorage.empty()) { 377 // Copy string to permanent storage 378 KeyStr = StringStorage.str().copy(StringAllocator); 379 } 380 return llvm::make_unique<ScalarHNode>(N, KeyStr); 381 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) { 382 StringRef ValueCopy = BSN->getValue().copy(StringAllocator); 383 return llvm::make_unique<ScalarHNode>(N, ValueCopy); 384 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { 385 auto SQHNode = llvm::make_unique<SequenceHNode>(N); 386 for (Node &SN : *SQ) { 387 auto Entry = createHNodes(&SN); 388 if (EC) 389 break; 390 SQHNode->Entries.push_back(std::move(Entry)); 391 } 392 return std::move(SQHNode); 393 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { 394 auto mapHNode = llvm::make_unique<MapHNode>(N); 395 for (KeyValueNode &KVN : *Map) { 396 Node *KeyNode = KVN.getKey(); 397 ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode); 398 Node *Value = KVN.getValue(); 399 if (!Key || !Value) { 400 if (!Key) 401 setError(KeyNode, "Map key must be a scalar"); 402 if (!Value) 403 setError(KeyNode, "Map value must not be empty"); 404 break; 405 } 406 StringStorage.clear(); 407 StringRef KeyStr = Key->getValue(StringStorage); 408 if (!StringStorage.empty()) { 409 // Copy string to permanent storage 410 KeyStr = StringStorage.str().copy(StringAllocator); 411 } 412 auto ValueHNode = createHNodes(Value); 413 if (EC) 414 break; 415 mapHNode->Mapping[KeyStr] = std::move(ValueHNode); 416 } 417 return std::move(mapHNode); 418 } else if (isa<NullNode>(N)) { 419 return llvm::make_unique<EmptyHNode>(N); 420 } else { 421 setError(N, "unknown node kind"); 422 return nullptr; 423 } 424 } 425 426 void Input::setError(const Twine &Message) { 427 setError(CurrentNode, Message); 428 } 429 430 bool Input::canElideEmptySequence() { 431 return false; 432 } 433 434 //===----------------------------------------------------------------------===// 435 // Output 436 //===----------------------------------------------------------------------===// 437 438 Output::Output(raw_ostream &yout, void *context, int WrapColumn) 439 : IO(context), Out(yout), WrapColumn(WrapColumn) {} 440 441 Output::~Output() = default; 442 443 bool Output::outputting() { 444 return true; 445 } 446 447 void Output::beginMapping() { 448 StateStack.push_back(inMapFirstKey); 449 NeedsNewLine = true; 450 } 451 452 bool Output::mapTag(StringRef Tag, bool Use) { 453 if (Use) { 454 // If this tag is being written inside a sequence we should write the start 455 // of the sequence before writing the tag, otherwise the tag won't be 456 // attached to the element in the sequence, but rather the sequence itself. 457 bool SequenceElement = false; 458 if (StateStack.size() > 1) { 459 auto &E = StateStack[StateStack.size() - 2]; 460 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E); 461 } 462 if (SequenceElement && StateStack.back() == inMapFirstKey) { 463 newLineCheck(); 464 } else { 465 output(" "); 466 } 467 output(Tag); 468 if (SequenceElement) { 469 // If we're writing the tag during the first element of a map, the tag 470 // takes the place of the first element in the sequence. 471 if (StateStack.back() == inMapFirstKey) { 472 StateStack.pop_back(); 473 StateStack.push_back(inMapOtherKey); 474 } 475 // Tags inside maps in sequences should act as keys in the map from a 476 // formatting perspective, so we always want a newline in a sequence. 477 NeedsNewLine = true; 478 } 479 } 480 return Use; 481 } 482 483 void Output::endMapping() { 484 // If we did not map anything, we should explicitly emit an empty map 485 if (StateStack.back() == inMapFirstKey) 486 output("{}"); 487 StateStack.pop_back(); 488 } 489 490 std::vector<StringRef> Output::keys() { 491 report_fatal_error("invalid call"); 492 } 493 494 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, 495 bool &UseDefault, void *&) { 496 UseDefault = false; 497 if (Required || !SameAsDefault || WriteDefaultValues) { 498 auto State = StateStack.back(); 499 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) { 500 flowKey(Key); 501 } else { 502 newLineCheck(); 503 paddedKey(Key); 504 } 505 return true; 506 } 507 return false; 508 } 509 510 void Output::postflightKey(void *) { 511 if (StateStack.back() == inMapFirstKey) { 512 StateStack.pop_back(); 513 StateStack.push_back(inMapOtherKey); 514 } else if (StateStack.back() == inFlowMapFirstKey) { 515 StateStack.pop_back(); 516 StateStack.push_back(inFlowMapOtherKey); 517 } 518 } 519 520 void Output::beginFlowMapping() { 521 StateStack.push_back(inFlowMapFirstKey); 522 newLineCheck(); 523 ColumnAtMapFlowStart = Column; 524 output("{ "); 525 } 526 527 void Output::endFlowMapping() { 528 StateStack.pop_back(); 529 outputUpToEndOfLine(" }"); 530 } 531 532 void Output::beginDocuments() { 533 outputUpToEndOfLine("---"); 534 } 535 536 bool Output::preflightDocument(unsigned index) { 537 if (index > 0) 538 outputUpToEndOfLine("\n---"); 539 return true; 540 } 541 542 void Output::postflightDocument() { 543 } 544 545 void Output::endDocuments() { 546 output("\n...\n"); 547 } 548 549 unsigned Output::beginSequence() { 550 StateStack.push_back(inSeqFirstElement); 551 NeedsNewLine = true; 552 return 0; 553 } 554 555 void Output::endSequence() { 556 // If we did not emit anything, we should explicitly emit an empty sequence 557 if (StateStack.back() == inSeqFirstElement) 558 output("[]"); 559 StateStack.pop_back(); 560 } 561 562 bool Output::preflightElement(unsigned, void *&) { 563 return true; 564 } 565 566 void Output::postflightElement(void *) { 567 if (StateStack.back() == inSeqFirstElement) { 568 StateStack.pop_back(); 569 StateStack.push_back(inSeqOtherElement); 570 } else if (StateStack.back() == inFlowSeqFirstElement) { 571 StateStack.pop_back(); 572 StateStack.push_back(inFlowSeqOtherElement); 573 } 574 } 575 576 unsigned Output::beginFlowSequence() { 577 StateStack.push_back(inFlowSeqFirstElement); 578 newLineCheck(); 579 ColumnAtFlowStart = Column; 580 output("[ "); 581 NeedFlowSequenceComma = false; 582 return 0; 583 } 584 585 void Output::endFlowSequence() { 586 StateStack.pop_back(); 587 outputUpToEndOfLine(" ]"); 588 } 589 590 bool Output::preflightFlowElement(unsigned, void *&) { 591 if (NeedFlowSequenceComma) 592 output(", "); 593 if (WrapColumn && Column > WrapColumn) { 594 output("\n"); 595 for (int i = 0; i < ColumnAtFlowStart; ++i) 596 output(" "); 597 Column = ColumnAtFlowStart; 598 output(" "); 599 } 600 return true; 601 } 602 603 void Output::postflightFlowElement(void *) { 604 NeedFlowSequenceComma = true; 605 } 606 607 void Output::beginEnumScalar() { 608 EnumerationMatchFound = false; 609 } 610 611 bool Output::matchEnumScalar(const char *Str, bool Match) { 612 if (Match && !EnumerationMatchFound) { 613 newLineCheck(); 614 outputUpToEndOfLine(Str); 615 EnumerationMatchFound = true; 616 } 617 return false; 618 } 619 620 bool Output::matchEnumFallback() { 621 if (EnumerationMatchFound) 622 return false; 623 EnumerationMatchFound = true; 624 return true; 625 } 626 627 void Output::endEnumScalar() { 628 if (!EnumerationMatchFound) 629 llvm_unreachable("bad runtime enum value"); 630 } 631 632 bool Output::beginBitSetScalar(bool &DoClear) { 633 newLineCheck(); 634 output("[ "); 635 NeedBitValueComma = false; 636 DoClear = false; 637 return true; 638 } 639 640 bool Output::bitSetMatch(const char *Str, bool Matches) { 641 if (Matches) { 642 if (NeedBitValueComma) 643 output(", "); 644 output(Str); 645 NeedBitValueComma = true; 646 } 647 return false; 648 } 649 650 void Output::endBitSetScalar() { 651 outputUpToEndOfLine(" ]"); 652 } 653 654 void Output::scalarString(StringRef &S, QuotingType MustQuote) { 655 newLineCheck(); 656 if (S.empty()) { 657 // Print '' for the empty string because leaving the field empty is not 658 // allowed. 659 outputUpToEndOfLine("''"); 660 return; 661 } 662 if (MustQuote == QuotingType::None) { 663 // Only quote if we must. 664 outputUpToEndOfLine(S); 665 return; 666 } 667 668 const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\""; 669 output(Quote); // Starting quote. 670 671 // When using double-quoted strings (and only in that case), non-printable characters may be 672 // present, and will be escaped using a variety of unicode-scalar and special short-form 673 // escapes. This is handled in yaml::escape. 674 if (MustQuote == QuotingType::Double) { 675 output(yaml::escape(S, /* EscapePrintable= */ false)); 676 outputUpToEndOfLine(Quote); 677 return; 678 } 679 680 unsigned i = 0; 681 unsigned j = 0; 682 unsigned End = S.size(); 683 const char *Base = S.data(); 684 685 // When using single-quoted strings, any single quote ' must be doubled to be escaped. 686 while (j < End) { 687 if (S[j] == '\'') { // Escape quotes. 688 output(StringRef(&Base[i], j - i)); // "flush". 689 output(StringLiteral("''")); // Print it as '' 690 i = j + 1; 691 } 692 ++j; 693 } 694 output(StringRef(&Base[i], j - i)); 695 outputUpToEndOfLine(Quote); // Ending quote. 696 } 697 698 void Output::blockScalarString(StringRef &S) { 699 if (!StateStack.empty()) 700 newLineCheck(); 701 output(" |"); 702 outputNewLine(); 703 704 unsigned Indent = StateStack.empty() ? 1 : StateStack.size(); 705 706 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false); 707 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) { 708 for (unsigned I = 0; I < Indent; ++I) { 709 output(" "); 710 } 711 output(*Lines); 712 outputNewLine(); 713 } 714 } 715 716 void Output::scalarTag(std::string &Tag) { 717 if (Tag.empty()) 718 return; 719 newLineCheck(); 720 output(Tag); 721 output(" "); 722 } 723 724 void Output::setError(const Twine &message) { 725 } 726 727 bool Output::canElideEmptySequence() { 728 // Normally, with an optional key/value where the value is an empty sequence, 729 // the whole key/value can be not written. But, that produces wrong yaml 730 // if the key/value is the only thing in the map and the map is used in 731 // a sequence. This detects if the this sequence is the first key/value 732 // in map that itself is embedded in a sequnce. 733 if (StateStack.size() < 2) 734 return true; 735 if (StateStack.back() != inMapFirstKey) 736 return true; 737 return !inSeqAnyElement(StateStack[StateStack.size() - 2]); 738 } 739 740 void Output::output(StringRef s) { 741 Column += s.size(); 742 Out << s; 743 } 744 745 void Output::outputUpToEndOfLine(StringRef s) { 746 output(s); 747 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) && 748 !inFlowMapAnyKey(StateStack.back()))) 749 NeedsNewLine = true; 750 } 751 752 void Output::outputNewLine() { 753 Out << "\n"; 754 Column = 0; 755 } 756 757 // if seq at top, indent as if map, then add "- " 758 // if seq in middle, use "- " if firstKey, else use " " 759 // 760 761 void Output::newLineCheck() { 762 if (!NeedsNewLine) 763 return; 764 NeedsNewLine = false; 765 766 outputNewLine(); 767 768 if (StateStack.size() == 0) 769 return; 770 771 unsigned Indent = StateStack.size() - 1; 772 bool OutputDash = false; 773 774 if (StateStack.back() == inSeqFirstElement || 775 StateStack.back() == inSeqOtherElement) { 776 OutputDash = true; 777 } else if ((StateStack.size() > 1) && 778 ((StateStack.back() == inMapFirstKey) || 779 inFlowSeqAnyElement(StateStack.back()) || 780 (StateStack.back() == inFlowMapFirstKey)) && 781 inSeqAnyElement(StateStack[StateStack.size() - 2])) { 782 --Indent; 783 OutputDash = true; 784 } 785 786 for (unsigned i = 0; i < Indent; ++i) { 787 output(" "); 788 } 789 if (OutputDash) { 790 output("- "); 791 } 792 793 } 794 795 void Output::paddedKey(StringRef key) { 796 output(key); 797 output(":"); 798 const char *spaces = " "; 799 if (key.size() < strlen(spaces)) 800 output(&spaces[key.size()]); 801 else 802 output(" "); 803 } 804 805 void Output::flowKey(StringRef Key) { 806 if (StateStack.back() == inFlowMapOtherKey) 807 output(", "); 808 if (WrapColumn && Column > WrapColumn) { 809 output("\n"); 810 for (int I = 0; I < ColumnAtMapFlowStart; ++I) 811 output(" "); 812 Column = ColumnAtMapFlowStart; 813 output(" "); 814 } 815 output(Key); 816 output(": "); 817 } 818 819 NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); } 820 821 bool Output::inSeqAnyElement(InState State) { 822 return State == inSeqFirstElement || State == inSeqOtherElement; 823 } 824 825 bool Output::inFlowSeqAnyElement(InState State) { 826 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement; 827 } 828 829 bool Output::inMapAnyKey(InState State) { 830 return State == inMapFirstKey || State == inMapOtherKey; 831 } 832 833 bool Output::inFlowMapAnyKey(InState State) { 834 return State == inFlowMapFirstKey || State == inFlowMapOtherKey; 835 } 836 837 //===----------------------------------------------------------------------===// 838 // traits for built-in types 839 //===----------------------------------------------------------------------===// 840 841 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { 842 Out << (Val ? "true" : "false"); 843 } 844 845 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) { 846 if (Scalar.equals("true")) { 847 Val = true; 848 return StringRef(); 849 } else if (Scalar.equals("false")) { 850 Val = false; 851 return StringRef(); 852 } 853 return "invalid boolean"; 854 } 855 856 void ScalarTraits<StringRef>::output(const StringRef &Val, void *, 857 raw_ostream &Out) { 858 Out << Val; 859 } 860 861 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, 862 StringRef &Val) { 863 Val = Scalar; 864 return StringRef(); 865 } 866 867 void ScalarTraits<std::string>::output(const std::string &Val, void *, 868 raw_ostream &Out) { 869 Out << Val; 870 } 871 872 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, 873 std::string &Val) { 874 Val = Scalar.str(); 875 return StringRef(); 876 } 877 878 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *, 879 raw_ostream &Out) { 880 // use temp uin32_t because ostream thinks uint8_t is a character 881 uint32_t Num = Val; 882 Out << Num; 883 } 884 885 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) { 886 unsigned long long n; 887 if (getAsUnsignedInteger(Scalar, 0, n)) 888 return "invalid number"; 889 if (n > 0xFF) 890 return "out of range number"; 891 Val = n; 892 return StringRef(); 893 } 894 895 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, 896 raw_ostream &Out) { 897 Out << Val; 898 } 899 900 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *, 901 uint16_t &Val) { 902 unsigned long long n; 903 if (getAsUnsignedInteger(Scalar, 0, n)) 904 return "invalid number"; 905 if (n > 0xFFFF) 906 return "out of range number"; 907 Val = n; 908 return StringRef(); 909 } 910 911 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, 912 raw_ostream &Out) { 913 Out << Val; 914 } 915 916 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *, 917 uint32_t &Val) { 918 unsigned long long n; 919 if (getAsUnsignedInteger(Scalar, 0, n)) 920 return "invalid number"; 921 if (n > 0xFFFFFFFFUL) 922 return "out of range number"; 923 Val = n; 924 return StringRef(); 925 } 926 927 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, 928 raw_ostream &Out) { 929 Out << Val; 930 } 931 932 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *, 933 uint64_t &Val) { 934 unsigned long long N; 935 if (getAsUnsignedInteger(Scalar, 0, N)) 936 return "invalid number"; 937 Val = N; 938 return StringRef(); 939 } 940 941 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) { 942 // use temp in32_t because ostream thinks int8_t is a character 943 int32_t Num = Val; 944 Out << Num; 945 } 946 947 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) { 948 long long N; 949 if (getAsSignedInteger(Scalar, 0, N)) 950 return "invalid number"; 951 if ((N > 127) || (N < -128)) 952 return "out of range number"; 953 Val = N; 954 return StringRef(); 955 } 956 957 void ScalarTraits<int16_t>::output(const int16_t &Val, void *, 958 raw_ostream &Out) { 959 Out << Val; 960 } 961 962 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) { 963 long long N; 964 if (getAsSignedInteger(Scalar, 0, N)) 965 return "invalid number"; 966 if ((N > INT16_MAX) || (N < INT16_MIN)) 967 return "out of range number"; 968 Val = N; 969 return StringRef(); 970 } 971 972 void ScalarTraits<int32_t>::output(const int32_t &Val, void *, 973 raw_ostream &Out) { 974 Out << Val; 975 } 976 977 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) { 978 long long N; 979 if (getAsSignedInteger(Scalar, 0, N)) 980 return "invalid number"; 981 if ((N > INT32_MAX) || (N < INT32_MIN)) 982 return "out of range number"; 983 Val = N; 984 return StringRef(); 985 } 986 987 void ScalarTraits<int64_t>::output(const int64_t &Val, void *, 988 raw_ostream &Out) { 989 Out << Val; 990 } 991 992 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) { 993 long long N; 994 if (getAsSignedInteger(Scalar, 0, N)) 995 return "invalid number"; 996 Val = N; 997 return StringRef(); 998 } 999 1000 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { 1001 Out << format("%g", Val); 1002 } 1003 1004 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) { 1005 if (to_float(Scalar, Val)) 1006 return StringRef(); 1007 return "invalid floating point number"; 1008 } 1009 1010 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { 1011 Out << format("%g", Val); 1012 } 1013 1014 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) { 1015 if (to_float(Scalar, Val)) 1016 return StringRef(); 1017 return "invalid floating point number"; 1018 } 1019 1020 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { 1021 uint8_t Num = Val; 1022 Out << format("0x%02X", Num); 1023 } 1024 1025 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) { 1026 unsigned long long n; 1027 if (getAsUnsignedInteger(Scalar, 0, n)) 1028 return "invalid hex8 number"; 1029 if (n > 0xFF) 1030 return "out of range hex8 number"; 1031 Val = n; 1032 return StringRef(); 1033 } 1034 1035 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { 1036 uint16_t Num = Val; 1037 Out << format("0x%04X", Num); 1038 } 1039 1040 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) { 1041 unsigned long long n; 1042 if (getAsUnsignedInteger(Scalar, 0, n)) 1043 return "invalid hex16 number"; 1044 if (n > 0xFFFF) 1045 return "out of range hex16 number"; 1046 Val = n; 1047 return StringRef(); 1048 } 1049 1050 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { 1051 uint32_t Num = Val; 1052 Out << format("0x%08X", Num); 1053 } 1054 1055 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) { 1056 unsigned long long n; 1057 if (getAsUnsignedInteger(Scalar, 0, n)) 1058 return "invalid hex32 number"; 1059 if (n > 0xFFFFFFFFUL) 1060 return "out of range hex32 number"; 1061 Val = n; 1062 return StringRef(); 1063 } 1064 1065 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { 1066 uint64_t Num = Val; 1067 Out << format("0x%016llX", Num); 1068 } 1069 1070 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) { 1071 unsigned long long Num; 1072 if (getAsUnsignedInteger(Scalar, 0, Num)) 1073 return "invalid hex64 number"; 1074 Val = Num; 1075 return StringRef(); 1076 } 1077