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 10 #include "llvm/Support/Errc.h" 11 #include "llvm/ADT/Twine.h" 12 #include "llvm/Support/Casting.h" 13 #include "llvm/Support/ErrorHandling.h" 14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/YAMLParser.h" 16 #include "llvm/Support/YAMLTraits.h" 17 #include "llvm/Support/raw_ostream.h" 18 #include <cctype> 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 45 Input::Input(StringRef InputContent, 46 void *Ctxt, 47 SourceMgr::DiagHandlerTy DiagHandler, 48 void *DiagHandlerCtxt) 49 : IO(Ctxt), 50 Strm(new Stream(InputContent, SrcMgr)), 51 CurrentNode(nullptr) { 52 if (DiagHandler) 53 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); 54 DocIterator = Strm->begin(); 55 } 56 57 Input::~Input() { 58 } 59 60 std::error_code Input::error() { return EC; } 61 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() {} 68 69 bool Input::outputting() { 70 return false; 71 } 72 73 bool Input::setCurrentDocument() { 74 if (DocIterator != Strm->end()) { 75 Node *N = DocIterator->getRoot(); 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 82 if (isa<NullNode>(N)) { 83 // Empty files are allowed and ignored 84 ++DocIterator; 85 return setCurrentDocument(); 86 } 87 TopNode = this->createHNodes(N); 88 CurrentNode = TopNode.get(); 89 return true; 90 } 91 return false; 92 } 93 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); 106 } 107 108 void Input::beginMapping() { 109 if (EC) 110 return; 111 // CurrentNode can be null if the document is empty. 112 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 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; 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 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); 138 HNode *Value = MN->Mapping[Key].get(); 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; 158 // CurrentNode can be null if the document is empty. 159 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 160 if (!MN) 161 return; 162 for (const auto &NN : MN->Mapping) { 163 if (!MN->isValidKey(NN.first())) { 164 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); 165 break; 166 } 167 } 168 } 169 170 unsigned Input::beginSequence() { 171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) 172 return SQ->Entries.size(); 173 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; 179 } 180 // Any other type of HNode is an error. 181 setError(CurrentNode, "not a sequence"); 182 return 0; 183 } 184 185 void Input::endSequence() { 186 } 187 188 bool Input::preflightElement(unsigned Index, void *&SaveInfo) { 189 if (EC) 190 return false; 191 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 192 SaveInfo = CurrentNode; 193 CurrentNode = SQ->Entries[Index].get(); 194 return true; 195 } 196 return false; 197 } 198 199 void Input::postflightElement(void *SaveInfo) { 200 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 201 } 202 203 unsigned Input::beginFlowSequence() { return beginSequence(); } 204 205 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) { 206 if (EC) 207 return false; 208 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 209 SaveInfo = CurrentNode; 210 CurrentNode = SQ->Entries[index].get(); 211 return true; 212 } 213 return false; 214 } 215 216 void Input::postflightFlowElement(void *SaveInfo) { 217 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 218 } 219 220 void Input::endFlowSequence() { 221 } 222 223 void Input::beginEnumScalar() { 224 ScalarMatchFound = false; 225 } 226 227 bool Input::matchEnumScalar(const char *Str, bool) { 228 if (ScalarMatchFound) 229 return false; 230 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 231 if (SN->value().equals(Str)) { 232 ScalarMatchFound = true; 233 return true; 234 } 235 } 236 return false; 237 } 238 239 bool Input::matchEnumFallback() { 240 if (ScalarMatchFound) 241 return false; 242 ScalarMatchFound = true; 243 return true; 244 } 245 246 void Input::endEnumScalar() { 247 if (!ScalarMatchFound) { 248 setError(CurrentNode, "unknown enumerated scalar"); 249 } 250 } 251 252 bool Input::beginBitSetScalar(bool &DoClear) { 253 BitValuesUsed.clear(); 254 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 255 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false); 256 } else { 257 setError(CurrentNode, "expected sequence of bit values"); 258 } 259 DoClear = true; 260 return true; 261 } 262 263 bool Input::bitSetMatch(const char *Str, bool) { 264 if (EC) 265 return false; 266 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 267 unsigned Index = 0; 268 for (auto &N : SQ->Entries) { 269 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) { 270 if (SN->value().equals(Str)) { 271 BitValuesUsed[Index] = true; 272 return true; 273 } 274 } else { 275 setError(CurrentNode, "unexpected scalar in sequence of bit values"); 276 } 277 ++Index; 278 } 279 } else { 280 setError(CurrentNode, "expected sequence of bit values"); 281 } 282 return false; 283 } 284 285 void Input::endBitSetScalar() { 286 if (EC) 287 return; 288 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 289 assert(BitValuesUsed.size() == SQ->Entries.size()); 290 for (unsigned i = 0; i < SQ->Entries.size(); ++i) { 291 if (!BitValuesUsed[i]) { 292 setError(SQ->Entries[i].get(), "unknown bit value"); 293 return; 294 } 295 } 296 } 297 } 298 299 void Input::scalarString(StringRef &S, bool) { 300 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 301 S = SN->value(); 302 } else { 303 setError(CurrentNode, "unexpected scalar"); 304 } 305 } 306 307 void Input::setError(HNode *hnode, const Twine &message) { 308 assert(hnode && "HNode must not be NULL"); 309 this->setError(hnode->_node, message); 310 } 311 312 void Input::setError(Node *node, const Twine &message) { 313 Strm->printError(node, message); 314 EC = make_error_code(errc::invalid_argument); 315 } 316 317 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { 318 SmallString<128> StringStorage; 319 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { 320 StringRef KeyStr = SN->getValue(StringStorage); 321 if (!StringStorage.empty()) { 322 // Copy string to permanent storage 323 unsigned Len = StringStorage.size(); 324 char *Buf = StringAllocator.Allocate<char>(Len); 325 memcpy(Buf, &StringStorage[0], Len); 326 KeyStr = StringRef(Buf, Len); 327 } 328 return llvm::make_unique<ScalarHNode>(N, KeyStr); 329 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { 330 auto SQHNode = llvm::make_unique<SequenceHNode>(N); 331 for (Node &SN : *SQ) { 332 auto Entry = this->createHNodes(&SN); 333 if (EC) 334 break; 335 SQHNode->Entries.push_back(std::move(Entry)); 336 } 337 return std::move(SQHNode); 338 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { 339 auto mapHNode = llvm::make_unique<MapHNode>(N); 340 for (KeyValueNode &KVN : *Map) { 341 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 } 347 StringStorage.clear(); 348 StringRef KeyStr = KeyScalar->getValue(StringStorage); 349 if (!StringStorage.empty()) { 350 // Copy string to permanent storage 351 unsigned Len = StringStorage.size(); 352 char *Buf = StringAllocator.Allocate<char>(Len); 353 memcpy(Buf, &StringStorage[0], Len); 354 KeyStr = StringRef(Buf, Len); 355 } 356 auto ValueHNode = this->createHNodes(KVN.getValue()); 357 if (EC) 358 break; 359 mapHNode->Mapping[KeyStr] = std::move(ValueHNode); 360 } 361 return std::move(mapHNode); 362 } else if (isa<NullNode>(N)) { 363 return llvm::make_unique<EmptyHNode>(N); 364 } else { 365 setError(N, "unknown node kind"); 366 return nullptr; 367 } 368 } 369 370 bool Input::MapHNode::isValidKey(StringRef Key) { 371 for (const char *K : ValidKeys) { 372 if (Key.equals(K)) 373 return true; 374 } 375 return false; 376 } 377 378 void Input::setError(const Twine &Message) { 379 this->setError(CurrentNode, Message); 380 } 381 382 bool Input::canElideEmptySequence() { 383 return false; 384 } 385 386 //===----------------------------------------------------------------------===// 387 // Output 388 //===----------------------------------------------------------------------===// 389 390 Output::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) { 399 } 400 401 Output::~Output() { 402 } 403 404 bool Output::outputting() { 405 return true; 406 } 407 408 void Output::beginMapping() { 409 StateStack.push_back(inMapFirstKey); 410 NeedsNewLine = true; 411 } 412 413 bool Output::mapTag(StringRef Tag, bool Use) { 414 if (Use) { 415 this->output(" "); 416 this->output(Tag); 417 } 418 return Use; 419 } 420 421 void Output::endMapping() { 422 StateStack.pop_back(); 423 } 424 425 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, 426 bool &UseDefault, void *&) { 427 UseDefault = false; 428 if (Required || !SameAsDefault) { 429 this->newLineCheck(); 430 this->paddedKey(Key); 431 return true; 432 } 433 return false; 434 } 435 436 void Output::postflightKey(void *) { 437 if (StateStack.back() == inMapFirstKey) { 438 StateStack.pop_back(); 439 StateStack.push_back(inMapOtherKey); 440 } 441 } 442 443 void Output::beginDocuments() { 444 this->outputUpToEndOfLine("---"); 445 } 446 447 bool Output::preflightDocument(unsigned index) { 448 if (index > 0) 449 this->outputUpToEndOfLine("\n---"); 450 return true; 451 } 452 453 void Output::postflightDocument() { 454 } 455 456 void Output::endDocuments() { 457 output("\n...\n"); 458 } 459 460 unsigned Output::beginSequence() { 461 StateStack.push_back(inSeq); 462 NeedsNewLine = true; 463 return 0; 464 } 465 466 void Output::endSequence() { 467 StateStack.pop_back(); 468 } 469 470 bool Output::preflightElement(unsigned, void *&) { 471 return true; 472 } 473 474 void Output::postflightElement(void *) { 475 } 476 477 unsigned Output::beginFlowSequence() { 478 StateStack.push_back(inFlowSeq); 479 this->newLineCheck(); 480 ColumnAtFlowStart = Column; 481 output("[ "); 482 NeedFlowSequenceComma = false; 483 return 0; 484 } 485 486 void Output::endFlowSequence() { 487 StateStack.pop_back(); 488 this->outputUpToEndOfLine(" ]"); 489 } 490 491 bool Output::preflightFlowElement(unsigned, void *&) { 492 if (NeedFlowSequenceComma) 493 output(", "); 494 if (Column > 70) { 495 output("\n"); 496 for (int i = 0; i < ColumnAtFlowStart; ++i) 497 output(" "); 498 Column = ColumnAtFlowStart; 499 output(" "); 500 } 501 return true; 502 } 503 504 void Output::postflightFlowElement(void *) { 505 NeedFlowSequenceComma = true; 506 } 507 508 void Output::beginEnumScalar() { 509 EnumerationMatchFound = false; 510 } 511 512 bool Output::matchEnumScalar(const char *Str, bool Match) { 513 if (Match && !EnumerationMatchFound) { 514 this->newLineCheck(); 515 this->outputUpToEndOfLine(Str); 516 EnumerationMatchFound = true; 517 } 518 return false; 519 } 520 521 bool Output::matchEnumFallback() { 522 if (EnumerationMatchFound) 523 return false; 524 EnumerationMatchFound = true; 525 return true; 526 } 527 528 void Output::endEnumScalar() { 529 if (!EnumerationMatchFound) 530 llvm_unreachable("bad runtime enum value"); 531 } 532 533 bool Output::beginBitSetScalar(bool &DoClear) { 534 this->newLineCheck(); 535 output("[ "); 536 NeedBitValueComma = false; 537 DoClear = false; 538 return true; 539 } 540 541 bool Output::bitSetMatch(const char *Str, bool Matches) { 542 if (Matches) { 543 if (NeedBitValueComma) 544 output(", "); 545 this->output(Str); 546 NeedBitValueComma = true; 547 } 548 return false; 549 } 550 551 void Output::endBitSetScalar() { 552 this->outputUpToEndOfLine(" ]"); 553 } 554 555 void Output::scalarString(StringRef &S, bool MustQuote) { 556 this->newLineCheck(); 557 if (S.empty()) { 558 // Print '' for the empty string because leaving the field empty is not 559 // allowed. 560 this->outputUpToEndOfLine("''"); 561 return; 562 } 563 if (!MustQuote) { 564 // Only quote if we must. 565 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 586 void Output::setError(const Twine &message) { 587 } 588 589 bool 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. 595 if (StateStack.size() < 2) 596 return true; 597 if (StateStack.back() != inMapFirstKey) 598 return true; 599 return (StateStack[StateStack.size()-2] != inSeq); 600 } 601 602 void Output::output(StringRef s) { 603 Column += s.size(); 604 Out << s; 605 } 606 607 void Output::outputUpToEndOfLine(StringRef s) { 608 this->output(s); 609 if (StateStack.empty() || StateStack.back() != inFlowSeq) 610 NeedsNewLine = true; 611 } 612 613 void 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 622 void Output::newLineCheck() { 623 if (!NeedsNewLine) 624 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 633 if (StateStack.back() == inSeq) { 634 OutputDash = true; 635 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) && 636 (StateStack[StateStack.size() - 2] == inSeq)) { 637 --Indent; 638 OutputDash = true; 639 } 640 641 for (unsigned i = 0; i < Indent; ++i) { 642 output(" "); 643 } 644 if (OutputDash) { 645 output("- "); 646 } 647 648 } 649 650 void Output::paddedKey(StringRef key) { 651 output(key); 652 output(":"); 653 const char *spaces = " "; 654 if (key.size() < strlen(spaces)) 655 output(&spaces[key.size()]); 656 else 657 output(" "); 658 } 659 660 //===----------------------------------------------------------------------===// 661 // traits for built-in types 662 //===----------------------------------------------------------------------===// 663 664 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { 665 Out << (Val ? "true" : "false"); 666 } 667 668 StringRef 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; 674 return StringRef(); 675 } 676 return "invalid boolean"; 677 } 678 679 void ScalarTraits<StringRef>::output(const StringRef &Val, void *, 680 raw_ostream &Out) { 681 Out << Val; 682 } 683 684 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, 685 StringRef &Val) { 686 Val = Scalar; 687 return StringRef(); 688 } 689 690 void ScalarTraits<std::string>::output(const std::string &Val, void *, 691 raw_ostream &Out) { 692 Out << Val; 693 } 694 695 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, 696 std::string &Val) { 697 Val = Scalar.str(); 698 return StringRef(); 699 } 700 701 void 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 } 707 708 StringRef 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 } 717 718 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, 719 raw_ostream &Out) { 720 Out << Val; 721 } 722 723 StringRef 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 } 733 734 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, 735 raw_ostream &Out) { 736 Out << Val; 737 } 738 739 StringRef 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 } 749 750 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, 751 raw_ostream &Out) { 752 Out << Val; 753 } 754 755 StringRef 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 } 763 764 void 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 } 769 770 StringRef 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 } 779 780 void ScalarTraits<int16_t>::output(const int16_t &Val, void *, 781 raw_ostream &Out) { 782 Out << Val; 783 } 784 785 StringRef 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 } 794 795 void ScalarTraits<int32_t>::output(const int32_t &Val, void *, 796 raw_ostream &Out) { 797 Out << Val; 798 } 799 800 StringRef 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 810 void ScalarTraits<int64_t>::output(const int64_t &Val, void *, 811 raw_ostream &Out) { 812 Out << Val; 813 } 814 815 StringRef 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 823 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { 824 Out << format("%g", Val); 825 } 826 827 StringRef 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 836 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { 837 Out << format("%g", Val); 838 } 839 840 StringRef 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 } 848 849 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { 850 uint8_t Num = Val; 851 Out << format("0x%02X", Num); 852 } 853 854 StringRef 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 } 863 864 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { 865 uint16_t Num = Val; 866 Out << format("0x%04X", Num); 867 } 868 869 StringRef 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 } 878 879 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { 880 uint32_t Num = Val; 881 Out << format("0x%08X", Num); 882 } 883 884 StringRef 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 } 893 894 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { 895 uint64_t Num = Val; 896 Out << format("0x%016llX", Num); 897 } 898 899 StringRef 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 } 906