YAMLTraits.cpp revision 280031
1219354Spjd//===- lib/Support/YAMLTraits.cpp -----------------------------------------===// 2219354Spjd// 3219354Spjd// The LLVM Linker 4219354Spjd// 5219354Spjd// This file is distributed under the University of Illinois Open Source 6219354Spjd// License. See LICENSE.TXT for details. 7219354Spjd// 8219354Spjd//===----------------------------------------------------------------------===// 9219354Spjd 10219354Spjd#include "llvm/Support/Errc.h" 11219354Spjd#include "llvm/ADT/Twine.h" 12219354Spjd#include "llvm/Support/Casting.h" 13219354Spjd#include "llvm/Support/ErrorHandling.h" 14219354Spjd#include "llvm/Support/Format.h" 15219354Spjd#include "llvm/Support/YAMLParser.h" 16219354Spjd#include "llvm/Support/YAMLTraits.h" 17219354Spjd#include "llvm/Support/raw_ostream.h" 18219354Spjd#include <cctype> 19219354Spjd#include <cstring> 20219354Spjdusing namespace llvm; 21219354Spjdusing namespace yaml; 22219354Spjd 23219354Spjd//===----------------------------------------------------------------------===// 24219354Spjd// IO 25219354Spjd//===----------------------------------------------------------------------===// 26219354Spjd 27219354SpjdIO::IO(void *Context) : Ctxt(Context) { 28219354Spjd} 29219354Spjd 30219354SpjdIO::~IO() { 31219354Spjd} 32219354Spjd 33219354Spjdvoid *IO::getContext() { 34219354Spjd return Ctxt; 35219354Spjd} 36219354Spjd 37219354Spjdvoid IO::setContext(void *Context) { 38219354Spjd Ctxt = Context; 39219354Spjd} 40219354Spjd 41219354Spjd//===----------------------------------------------------------------------===// 42219354Spjd// Input 43219354Spjd//===----------------------------------------------------------------------===// 44219354Spjd 45219354SpjdInput::Input(StringRef InputContent, 46219354Spjd void *Ctxt, 47219354Spjd SourceMgr::DiagHandlerTy DiagHandler, 48219354Spjd void *DiagHandlerCtxt) 49219354Spjd : IO(Ctxt), 50219354Spjd Strm(new Stream(InputContent, SrcMgr)), 51219354Spjd CurrentNode(nullptr) { 52219354Spjd if (DiagHandler) 53219354Spjd SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); 54219354Spjd DocIterator = Strm->begin(); 55219354Spjd} 56219354Spjd 57219354SpjdInput::~Input() { 58219354Spjd} 59219354Spjd 60219354Spjdstd::error_code Input::error() { return EC; } 61219354Spjd 62219354Spjd// Pin the vtables to this file. 63219354Spjdvoid Input::HNode::anchor() {} 64219354Spjdvoid Input::EmptyHNode::anchor() {} 65219354Spjdvoid Input::ScalarHNode::anchor() {} 66219354Spjdvoid Input::MapHNode::anchor() {} 67219354Spjdvoid Input::SequenceHNode::anchor() {} 68219354Spjd 69219354Spjdbool Input::outputting() { 70219354Spjd return false; 71219354Spjd} 72219354Spjd 73219354Spjdbool Input::setCurrentDocument() { 74219354Spjd if (DocIterator != Strm->end()) { 75219354Spjd Node *N = DocIterator->getRoot(); 76219354Spjd if (!N) { 77219354Spjd assert(Strm->failed() && "Root is NULL iff parsing failed"); 78219354Spjd EC = make_error_code(errc::invalid_argument); 79219354Spjd return false; 80219354Spjd } 81219354Spjd 82219354Spjd if (isa<NullNode>(N)) { 83219354Spjd // Empty files are allowed and ignored 84219354Spjd ++DocIterator; 85219354Spjd return setCurrentDocument(); 86219354Spjd } 87219354Spjd TopNode = this->createHNodes(N); 88219354Spjd CurrentNode = TopNode.get(); 89219354Spjd return true; 90219354Spjd } 91219354Spjd return false; 92219354Spjd} 93219354Spjd 94219354Spjdbool Input::nextDocument() { 95219354Spjd return ++DocIterator != Strm->end(); 96219354Spjd} 97219354Spjd 98219354Spjdbool Input::mapTag(StringRef Tag, bool Default) { 99219354Spjd std::string foundTag = CurrentNode->_node->getVerbatimTag(); 100219354Spjd if (foundTag.empty()) { 101219354Spjd // If no tag found and 'Tag' is the default, say it was found. 102219354Spjd return Default; 103219354Spjd } 104219354Spjd // Return true iff found tag matches supplied tag. 105219354Spjd return Tag.equals(foundTag); 106219354Spjd} 107219354Spjd 108219354Spjdvoid Input::beginMapping() { 109219354Spjd if (EC) 110219354Spjd return; 111219354Spjd // CurrentNode can be null if the document is empty. 112219354Spjd MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 113219354Spjd if (MN) { 114219354Spjd MN->ValidKeys.clear(); 115219354Spjd } 116219354Spjd} 117219354Spjd 118219354Spjdbool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, 119219354Spjd void *&SaveInfo) { 120219354Spjd UseDefault = false; 121219354Spjd if (EC) 122219354Spjd return false; 123219354Spjd 124219354Spjd // CurrentNode is null for empty documents, which is an error in case required 125219354Spjd // nodes are present. 126219354Spjd if (!CurrentNode) { 127219354Spjd if (Required) 128219354Spjd EC = make_error_code(errc::invalid_argument); 129219354Spjd return false; 130219354Spjd } 131219354Spjd 132219354Spjd MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); 133219354Spjd if (!MN) { 134219354Spjd setError(CurrentNode, "not a mapping"); 135219354Spjd return false; 136219354Spjd } 137219354Spjd MN->ValidKeys.push_back(Key); 138219354Spjd HNode *Value = MN->Mapping[Key].get(); 139219354Spjd if (!Value) { 140219354Spjd if (Required) 141219354Spjd setError(CurrentNode, Twine("missing required key '") + Key + "'"); 142219354Spjd else 143219354Spjd UseDefault = true; 144219354Spjd return false; 145219354Spjd } 146219354Spjd SaveInfo = CurrentNode; 147219354Spjd CurrentNode = Value; 148219354Spjd return true; 149231017Strociny} 150219354Spjd 151219354Spjdvoid Input::postflightKey(void *saveInfo) { 152219354Spjd CurrentNode = reinterpret_cast<HNode *>(saveInfo); 153219354Spjd} 154219354Spjd 155219354Spjdvoid Input::endMapping() { 156219354Spjd if (EC) 157219354Spjd return; 158219354Spjd // CurrentNode can be null if the document is empty. 159219354Spjd MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 160219354Spjd if (!MN) 161219354Spjd return; 162219354Spjd for (const auto &NN : MN->Mapping) { 163219354Spjd if (!MN->isValidKey(NN.first())) { 164219354Spjd setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); 165219354Spjd break; 166219354Spjd } 167219354Spjd } 168219354Spjd} 169219354Spjd 170231017Strocinyunsigned Input::beginSequence() { 171219354Spjd if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 172219354Spjd return SQ->Entries.size(); 173219354Spjd } 174219354Spjd return 0; 175219354Spjd} 176219354Spjd 177219354Spjdvoid Input::endSequence() { 178219354Spjd} 179219354Spjd 180219354Spjdbool Input::preflightElement(unsigned Index, void *&SaveInfo) { 181219354Spjd if (EC) 182219354Spjd return false; 183219354Spjd if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 184219354Spjd SaveInfo = CurrentNode; 185219354Spjd CurrentNode = SQ->Entries[Index].get(); 186219354Spjd return true; 187219354Spjd } 188219354Spjd return false; 189219354Spjd} 190219354Spjd 191219354Spjdvoid Input::postflightElement(void *SaveInfo) { 192219354Spjd CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 193219354Spjd} 194219354Spjd 195219354Spjdunsigned Input::beginFlowSequence() { 196219354Spjd if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 197219354Spjd return SQ->Entries.size(); 198219354Spjd } 199219354Spjd return 0; 200219354Spjd} 201219354Spjd 202219354Spjdbool Input::preflightFlowElement(unsigned index, void *&SaveInfo) { 203219354Spjd if (EC) 204219354Spjd return false; 205219354Spjd if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 206219354Spjd SaveInfo = CurrentNode; 207219354Spjd CurrentNode = SQ->Entries[index].get(); 208219354Spjd return true; 209219354Spjd } 210219354Spjd return false; 211219354Spjd} 212 213void Input::postflightFlowElement(void *SaveInfo) { 214 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 215} 216 217void Input::endFlowSequence() { 218} 219 220void Input::beginEnumScalar() { 221 ScalarMatchFound = false; 222} 223 224bool 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 236void Input::endEnumScalar() { 237 if (!ScalarMatchFound) { 238 setError(CurrentNode, "unknown enumerated scalar"); 239 } 240} 241 242bool 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 253bool Input::bitSetMatch(const char *Str, bool) { 254 if (EC) 255 return false; 256 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 257 unsigned Index = 0; 258 for (auto &N : SQ->Entries) { 259 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) { 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 275void 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]) { 282 setError(SQ->Entries[i].get(), "unknown bit value"); 283 return; 284 } 285 } 286 } 287} 288 289void Input::scalarString(StringRef &S, bool) { 290 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 291 S = SN->value(); 292 } else { 293 setError(CurrentNode, "unexpected scalar"); 294 } 295} 296 297void Input::setError(HNode *hnode, const Twine &message) { 298 assert(hnode && "HNode must not be NULL"); 299 this->setError(hnode->_node, message); 300} 301 302void Input::setError(Node *node, const Twine &message) { 303 Strm->printError(node, message); 304 EC = make_error_code(errc::invalid_argument); 305} 306 307std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { 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 } 318 return llvm::make_unique<ScalarHNode>(N, KeyStr); 319 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { 320 auto SQHNode = llvm::make_unique<SequenceHNode>(N); 321 for (Node &SN : *SQ) { 322 auto Entry = this->createHNodes(&SN); 323 if (EC) 324 break; 325 SQHNode->Entries.push_back(std::move(Entry)); 326 } 327 return std::move(SQHNode); 328 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { 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 } 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 } 346 auto ValueHNode = this->createHNodes(KVN.getValue()); 347 if (EC) 348 break; 349 mapHNode->Mapping[KeyStr] = std::move(ValueHNode); 350 } 351 return std::move(mapHNode); 352 } else if (isa<NullNode>(N)) { 353 return llvm::make_unique<EmptyHNode>(N); 354 } else { 355 setError(N, "unknown node kind"); 356 return nullptr; 357 } 358} 359 360bool Input::MapHNode::isValidKey(StringRef Key) { 361 for (const char *K : ValidKeys) { 362 if (Key.equals(K)) 363 return true; 364 } 365 return false; 366} 367 368void Input::setError(const Twine &Message) { 369 this->setError(CurrentNode, Message); 370} 371 372bool Input::canElideEmptySequence() { 373 return false; 374} 375 376//===----------------------------------------------------------------------===// 377// Output 378//===----------------------------------------------------------------------===// 379 380Output::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 391Output::~Output() { 392} 393 394bool Output::outputting() { 395 return true; 396} 397 398void Output::beginMapping() { 399 StateStack.push_back(inMapFirstKey); 400 NeedsNewLine = true; 401} 402 403bool Output::mapTag(StringRef Tag, bool Use) { 404 if (Use) { 405 this->output(" "); 406 this->output(Tag); 407 } 408 return Use; 409} 410 411void Output::endMapping() { 412 StateStack.pop_back(); 413} 414 415bool 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 426void Output::postflightKey(void *) { 427 if (StateStack.back() == inMapFirstKey) { 428 StateStack.pop_back(); 429 StateStack.push_back(inMapOtherKey); 430 } 431} 432 433void Output::beginDocuments() { 434 this->outputUpToEndOfLine("---"); 435} 436 437bool Output::preflightDocument(unsigned index) { 438 if (index > 0) 439 this->outputUpToEndOfLine("\n---"); 440 return true; 441} 442 443void Output::postflightDocument() { 444} 445 446void Output::endDocuments() { 447 output("\n...\n"); 448} 449 450unsigned Output::beginSequence() { 451 StateStack.push_back(inSeq); 452 NeedsNewLine = true; 453 return 0; 454} 455 456void Output::endSequence() { 457 StateStack.pop_back(); 458} 459 460bool Output::preflightElement(unsigned, void *&) { 461 return true; 462} 463 464void Output::postflightElement(void *) { 465} 466 467unsigned Output::beginFlowSequence() { 468 StateStack.push_back(inFlowSeq); 469 this->newLineCheck(); 470 ColumnAtFlowStart = Column; 471 output("[ "); 472 NeedFlowSequenceComma = false; 473 return 0; 474} 475 476void Output::endFlowSequence() { 477 StateStack.pop_back(); 478 this->outputUpToEndOfLine(" ]"); 479} 480 481bool 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 494void Output::postflightFlowElement(void *) { 495 NeedFlowSequenceComma = true; 496} 497 498void Output::beginEnumScalar() { 499 EnumerationMatchFound = false; 500} 501 502bool 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 511void Output::endEnumScalar() { 512 if (!EnumerationMatchFound) 513 llvm_unreachable("bad runtime enum value"); 514} 515 516bool Output::beginBitSetScalar(bool &DoClear) { 517 this->newLineCheck(); 518 output("[ "); 519 NeedBitValueComma = false; 520 DoClear = false; 521 return true; 522} 523 524bool 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 534void Output::endBitSetScalar() { 535 this->outputUpToEndOfLine(" ]"); 536} 537 538void Output::scalarString(StringRef &S, bool MustQuote) { 539 this->newLineCheck(); 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. 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 569void Output::setError(const Twine &message) { 570} 571 572bool 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 585void Output::output(StringRef s) { 586 Column += s.size(); 587 Out << s; 588} 589 590void Output::outputUpToEndOfLine(StringRef s) { 591 this->output(s); 592 if (StateStack.empty() || StateStack.back() != inFlowSeq) 593 NeedsNewLine = true; 594} 595 596void 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 605void 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 633void 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 647void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { 648 Out << (Val ? "true" : "false"); 649} 650 651StringRef 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 662void ScalarTraits<StringRef>::output(const StringRef &Val, void *, 663 raw_ostream &Out) { 664 Out << Val; 665} 666 667StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, 668 StringRef &Val) { 669 Val = Scalar; 670 return StringRef(); 671} 672 673void ScalarTraits<std::string>::output(const std::string &Val, void *, 674 raw_ostream &Out) { 675 Out << Val; 676} 677 678StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, 679 std::string &Val) { 680 Val = Scalar.str(); 681 return StringRef(); 682} 683 684void 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 691StringRef 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 701void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, 702 raw_ostream &Out) { 703 Out << Val; 704} 705 706StringRef 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 717void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, 718 raw_ostream &Out) { 719 Out << Val; 720} 721 722StringRef 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 733void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, 734 raw_ostream &Out) { 735 Out << Val; 736} 737 738StringRef 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 747void 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 753StringRef 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 763void ScalarTraits<int16_t>::output(const int16_t &Val, void *, 764 raw_ostream &Out) { 765 Out << Val; 766} 767 768StringRef 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 778void ScalarTraits<int32_t>::output(const int32_t &Val, void *, 779 raw_ostream &Out) { 780 Out << Val; 781} 782 783StringRef 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 793void ScalarTraits<int64_t>::output(const int64_t &Val, void *, 794 raw_ostream &Out) { 795 Out << Val; 796} 797 798StringRef 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 806void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { 807 Out << format("%g", Val); 808} 809 810StringRef 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 819void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { 820 Out << format("%g", Val); 821} 822 823StringRef 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 832void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { 833 uint8_t Num = Val; 834 Out << format("0x%02X", Num); 835} 836 837StringRef 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 847void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { 848 uint16_t Num = Val; 849 Out << format("0x%04X", Num); 850} 851 852StringRef 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 862void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { 863 uint32_t Num = Val; 864 Out << format("0x%08X", Num); 865} 866 867StringRef 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 877void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { 878 uint64_t Num = Val; 879 Out << format("0x%016llX", Num); 880} 881 882StringRef 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} 889