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