LLVM API Documentation
00001 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===// 00002 // 00003 // The LLVM Linker 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/Support/Errc.h" 00011 #include "llvm/Support/YAMLTraits.h" 00012 #include "llvm/ADT/Twine.h" 00013 #include "llvm/Support/Casting.h" 00014 #include "llvm/Support/ErrorHandling.h" 00015 #include "llvm/Support/Format.h" 00016 #include "llvm/Support/YAMLParser.h" 00017 #include "llvm/Support/raw_ostream.h" 00018 #include <cctype> 00019 #include <cstring> 00020 using namespace llvm; 00021 using namespace yaml; 00022 00023 //===----------------------------------------------------------------------===// 00024 // IO 00025 //===----------------------------------------------------------------------===// 00026 00027 IO::IO(void *Context) : Ctxt(Context) { 00028 } 00029 00030 IO::~IO() { 00031 } 00032 00033 void *IO::getContext() { 00034 return Ctxt; 00035 } 00036 00037 void IO::setContext(void *Context) { 00038 Ctxt = Context; 00039 } 00040 00041 //===----------------------------------------------------------------------===// 00042 // Input 00043 //===----------------------------------------------------------------------===// 00044 00045 Input::Input(StringRef InputContent, 00046 void *Ctxt, 00047 SourceMgr::DiagHandlerTy DiagHandler, 00048 void *DiagHandlerCtxt) 00049 : IO(Ctxt), 00050 Strm(new Stream(InputContent, SrcMgr)), 00051 CurrentNode(nullptr) { 00052 if (DiagHandler) 00053 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); 00054 DocIterator = Strm->begin(); 00055 } 00056 00057 Input::~Input() { 00058 } 00059 00060 std::error_code Input::error() { return EC; } 00061 00062 // Pin the vtables to this file. 00063 void Input::HNode::anchor() {} 00064 void Input::EmptyHNode::anchor() {} 00065 void Input::ScalarHNode::anchor() {} 00066 void Input::MapHNode::anchor() {} 00067 void Input::SequenceHNode::anchor() {} 00068 00069 bool Input::outputting() { 00070 return false; 00071 } 00072 00073 bool Input::setCurrentDocument() { 00074 if (DocIterator != Strm->end()) { 00075 Node *N = DocIterator->getRoot(); 00076 if (!N) { 00077 assert(Strm->failed() && "Root is NULL iff parsing failed"); 00078 EC = make_error_code(errc::invalid_argument); 00079 return false; 00080 } 00081 00082 if (isa<NullNode>(N)) { 00083 // Empty files are allowed and ignored 00084 ++DocIterator; 00085 return setCurrentDocument(); 00086 } 00087 TopNode = this->createHNodes(N); 00088 CurrentNode = TopNode.get(); 00089 return true; 00090 } 00091 return false; 00092 } 00093 00094 bool Input::nextDocument() { 00095 return ++DocIterator != Strm->end(); 00096 } 00097 00098 bool Input::mapTag(StringRef Tag, bool Default) { 00099 std::string foundTag = CurrentNode->_node->getVerbatimTag(); 00100 if (foundTag.empty()) { 00101 // If no tag found and 'Tag' is the default, say it was found. 00102 return Default; 00103 } 00104 // Return true iff found tag matches supplied tag. 00105 return Tag.equals(foundTag); 00106 } 00107 00108 void Input::beginMapping() { 00109 if (EC) 00110 return; 00111 // CurrentNode can be null if the document is empty. 00112 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 00113 if (MN) { 00114 MN->ValidKeys.clear(); 00115 } 00116 } 00117 00118 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, 00119 void *&SaveInfo) { 00120 UseDefault = false; 00121 if (EC) 00122 return false; 00123 00124 // CurrentNode is null for empty documents, which is an error in case required 00125 // nodes are present. 00126 if (!CurrentNode) { 00127 if (Required) 00128 EC = make_error_code(errc::invalid_argument); 00129 return false; 00130 } 00131 00132 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); 00133 if (!MN) { 00134 setError(CurrentNode, "not a mapping"); 00135 return false; 00136 } 00137 MN->ValidKeys.push_back(Key); 00138 HNode *Value = MN->Mapping[Key].get(); 00139 if (!Value) { 00140 if (Required) 00141 setError(CurrentNode, Twine("missing required key '") + Key + "'"); 00142 else 00143 UseDefault = true; 00144 return false; 00145 } 00146 SaveInfo = CurrentNode; 00147 CurrentNode = Value; 00148 return true; 00149 } 00150 00151 void Input::postflightKey(void *saveInfo) { 00152 CurrentNode = reinterpret_cast<HNode *>(saveInfo); 00153 } 00154 00155 void Input::endMapping() { 00156 if (EC) 00157 return; 00158 // CurrentNode can be null if the document is empty. 00159 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); 00160 if (!MN) 00161 return; 00162 for (const auto &NN : MN->Mapping) { 00163 if (!MN->isValidKey(NN.first())) { 00164 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); 00165 break; 00166 } 00167 } 00168 } 00169 00170 unsigned Input::beginSequence() { 00171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00172 return SQ->Entries.size(); 00173 } 00174 return 0; 00175 } 00176 00177 void Input::endSequence() { 00178 } 00179 00180 bool Input::preflightElement(unsigned Index, void *&SaveInfo) { 00181 if (EC) 00182 return false; 00183 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00184 SaveInfo = CurrentNode; 00185 CurrentNode = SQ->Entries[Index].get(); 00186 return true; 00187 } 00188 return false; 00189 } 00190 00191 void Input::postflightElement(void *SaveInfo) { 00192 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 00193 } 00194 00195 unsigned Input::beginFlowSequence() { 00196 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00197 return SQ->Entries.size(); 00198 } 00199 return 0; 00200 } 00201 00202 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) { 00203 if (EC) 00204 return false; 00205 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00206 SaveInfo = CurrentNode; 00207 CurrentNode = SQ->Entries[index].get(); 00208 return true; 00209 } 00210 return false; 00211 } 00212 00213 void Input::postflightFlowElement(void *SaveInfo) { 00214 CurrentNode = reinterpret_cast<HNode *>(SaveInfo); 00215 } 00216 00217 void Input::endFlowSequence() { 00218 } 00219 00220 void Input::beginEnumScalar() { 00221 ScalarMatchFound = false; 00222 } 00223 00224 bool Input::matchEnumScalar(const char *Str, bool) { 00225 if (ScalarMatchFound) 00226 return false; 00227 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 00228 if (SN->value().equals(Str)) { 00229 ScalarMatchFound = true; 00230 return true; 00231 } 00232 } 00233 return false; 00234 } 00235 00236 void Input::endEnumScalar() { 00237 if (!ScalarMatchFound) { 00238 setError(CurrentNode, "unknown enumerated scalar"); 00239 } 00240 } 00241 00242 bool Input::beginBitSetScalar(bool &DoClear) { 00243 BitValuesUsed.clear(); 00244 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00245 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false); 00246 } else { 00247 setError(CurrentNode, "expected sequence of bit values"); 00248 } 00249 DoClear = true; 00250 return true; 00251 } 00252 00253 bool Input::bitSetMatch(const char *Str, bool) { 00254 if (EC) 00255 return false; 00256 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00257 unsigned Index = 0; 00258 for (auto &N : SQ->Entries) { 00259 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) { 00260 if (SN->value().equals(Str)) { 00261 BitValuesUsed[Index] = true; 00262 return true; 00263 } 00264 } else { 00265 setError(CurrentNode, "unexpected scalar in sequence of bit values"); 00266 } 00267 ++Index; 00268 } 00269 } else { 00270 setError(CurrentNode, "expected sequence of bit values"); 00271 } 00272 return false; 00273 } 00274 00275 void Input::endBitSetScalar() { 00276 if (EC) 00277 return; 00278 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { 00279 assert(BitValuesUsed.size() == SQ->Entries.size()); 00280 for (unsigned i = 0; i < SQ->Entries.size(); ++i) { 00281 if (!BitValuesUsed[i]) { 00282 setError(SQ->Entries[i].get(), "unknown bit value"); 00283 return; 00284 } 00285 } 00286 } 00287 } 00288 00289 void Input::scalarString(StringRef &S, bool) { 00290 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { 00291 S = SN->value(); 00292 } else { 00293 setError(CurrentNode, "unexpected scalar"); 00294 } 00295 } 00296 00297 void Input::setError(HNode *hnode, const Twine &message) { 00298 assert(hnode && "HNode must not be NULL"); 00299 this->setError(hnode->_node, message); 00300 } 00301 00302 void Input::setError(Node *node, const Twine &message) { 00303 Strm->printError(node, message); 00304 EC = make_error_code(errc::invalid_argument); 00305 } 00306 00307 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { 00308 SmallString<128> StringStorage; 00309 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { 00310 StringRef KeyStr = SN->getValue(StringStorage); 00311 if (!StringStorage.empty()) { 00312 // Copy string to permanent storage 00313 unsigned Len = StringStorage.size(); 00314 char *Buf = StringAllocator.Allocate<char>(Len); 00315 memcpy(Buf, &StringStorage[0], Len); 00316 KeyStr = StringRef(Buf, Len); 00317 } 00318 return llvm::make_unique<ScalarHNode>(N, KeyStr); 00319 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { 00320 auto SQHNode = llvm::make_unique<SequenceHNode>(N); 00321 for (Node &SN : *SQ) { 00322 auto Entry = this->createHNodes(&SN); 00323 if (EC) 00324 break; 00325 SQHNode->Entries.push_back(std::move(Entry)); 00326 } 00327 return std::move(SQHNode); 00328 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { 00329 auto mapHNode = llvm::make_unique<MapHNode>(N); 00330 for (KeyValueNode &KVN : *Map) { 00331 Node *KeyNode = KVN.getKey(); 00332 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode); 00333 if (!KeyScalar) { 00334 setError(KeyNode, "Map key must be a scalar"); 00335 break; 00336 } 00337 StringStorage.clear(); 00338 StringRef KeyStr = KeyScalar->getValue(StringStorage); 00339 if (!StringStorage.empty()) { 00340 // Copy string to permanent storage 00341 unsigned Len = StringStorage.size(); 00342 char *Buf = StringAllocator.Allocate<char>(Len); 00343 memcpy(Buf, &StringStorage[0], Len); 00344 KeyStr = StringRef(Buf, Len); 00345 } 00346 auto ValueHNode = this->createHNodes(KVN.getValue()); 00347 if (EC) 00348 break; 00349 mapHNode->Mapping[KeyStr] = std::move(ValueHNode); 00350 } 00351 return std::move(mapHNode); 00352 } else if (isa<NullNode>(N)) { 00353 return llvm::make_unique<EmptyHNode>(N); 00354 } else { 00355 setError(N, "unknown node kind"); 00356 return nullptr; 00357 } 00358 } 00359 00360 bool Input::MapHNode::isValidKey(StringRef Key) { 00361 for (const char *K : ValidKeys) { 00362 if (Key.equals(K)) 00363 return true; 00364 } 00365 return false; 00366 } 00367 00368 void Input::setError(const Twine &Message) { 00369 this->setError(CurrentNode, Message); 00370 } 00371 00372 bool Input::canElideEmptySequence() { 00373 return false; 00374 } 00375 00376 //===----------------------------------------------------------------------===// 00377 // Output 00378 //===----------------------------------------------------------------------===// 00379 00380 Output::Output(raw_ostream &yout, void *context) 00381 : IO(context), 00382 Out(yout), 00383 Column(0), 00384 ColumnAtFlowStart(0), 00385 NeedBitValueComma(false), 00386 NeedFlowSequenceComma(false), 00387 EnumerationMatchFound(false), 00388 NeedsNewLine(false) { 00389 } 00390 00391 Output::~Output() { 00392 } 00393 00394 bool Output::outputting() { 00395 return true; 00396 } 00397 00398 void Output::beginMapping() { 00399 StateStack.push_back(inMapFirstKey); 00400 NeedsNewLine = true; 00401 } 00402 00403 bool Output::mapTag(StringRef Tag, bool Use) { 00404 if (Use) { 00405 this->output(" "); 00406 this->output(Tag); 00407 } 00408 return Use; 00409 } 00410 00411 void Output::endMapping() { 00412 StateStack.pop_back(); 00413 } 00414 00415 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, 00416 bool &UseDefault, void *&) { 00417 UseDefault = false; 00418 if (Required || !SameAsDefault) { 00419 this->newLineCheck(); 00420 this->paddedKey(Key); 00421 return true; 00422 } 00423 return false; 00424 } 00425 00426 void Output::postflightKey(void *) { 00427 if (StateStack.back() == inMapFirstKey) { 00428 StateStack.pop_back(); 00429 StateStack.push_back(inMapOtherKey); 00430 } 00431 } 00432 00433 void Output::beginDocuments() { 00434 this->outputUpToEndOfLine("---"); 00435 } 00436 00437 bool Output::preflightDocument(unsigned index) { 00438 if (index > 0) 00439 this->outputUpToEndOfLine("\n---"); 00440 return true; 00441 } 00442 00443 void Output::postflightDocument() { 00444 } 00445 00446 void Output::endDocuments() { 00447 output("\n...\n"); 00448 } 00449 00450 unsigned Output::beginSequence() { 00451 StateStack.push_back(inSeq); 00452 NeedsNewLine = true; 00453 return 0; 00454 } 00455 00456 void Output::endSequence() { 00457 StateStack.pop_back(); 00458 } 00459 00460 bool Output::preflightElement(unsigned, void *&) { 00461 return true; 00462 } 00463 00464 void Output::postflightElement(void *) { 00465 } 00466 00467 unsigned Output::beginFlowSequence() { 00468 StateStack.push_back(inFlowSeq); 00469 this->newLineCheck(); 00470 ColumnAtFlowStart = Column; 00471 output("[ "); 00472 NeedFlowSequenceComma = false; 00473 return 0; 00474 } 00475 00476 void Output::endFlowSequence() { 00477 StateStack.pop_back(); 00478 this->outputUpToEndOfLine(" ]"); 00479 } 00480 00481 bool Output::preflightFlowElement(unsigned, void *&) { 00482 if (NeedFlowSequenceComma) 00483 output(", "); 00484 if (Column > 70) { 00485 output("\n"); 00486 for (int i = 0; i < ColumnAtFlowStart; ++i) 00487 output(" "); 00488 Column = ColumnAtFlowStart; 00489 output(" "); 00490 } 00491 return true; 00492 } 00493 00494 void Output::postflightFlowElement(void *) { 00495 NeedFlowSequenceComma = true; 00496 } 00497 00498 void Output::beginEnumScalar() { 00499 EnumerationMatchFound = false; 00500 } 00501 00502 bool Output::matchEnumScalar(const char *Str, bool Match) { 00503 if (Match && !EnumerationMatchFound) { 00504 this->newLineCheck(); 00505 this->outputUpToEndOfLine(Str); 00506 EnumerationMatchFound = true; 00507 } 00508 return false; 00509 } 00510 00511 void Output::endEnumScalar() { 00512 if (!EnumerationMatchFound) 00513 llvm_unreachable("bad runtime enum value"); 00514 } 00515 00516 bool Output::beginBitSetScalar(bool &DoClear) { 00517 this->newLineCheck(); 00518 output("[ "); 00519 NeedBitValueComma = false; 00520 DoClear = false; 00521 return true; 00522 } 00523 00524 bool Output::bitSetMatch(const char *Str, bool Matches) { 00525 if (Matches) { 00526 if (NeedBitValueComma) 00527 output(", "); 00528 this->output(Str); 00529 NeedBitValueComma = true; 00530 } 00531 return false; 00532 } 00533 00534 void Output::endBitSetScalar() { 00535 this->outputUpToEndOfLine(" ]"); 00536 } 00537 00538 void Output::scalarString(StringRef &S, bool MustQuote) { 00539 this->newLineCheck(); 00540 if (S.empty()) { 00541 // Print '' for the empty string because leaving the field empty is not 00542 // allowed. 00543 this->outputUpToEndOfLine("''"); 00544 return; 00545 } 00546 if (!MustQuote) { 00547 // Only quote if we must. 00548 this->outputUpToEndOfLine(S); 00549 return; 00550 } 00551 unsigned i = 0; 00552 unsigned j = 0; 00553 unsigned End = S.size(); 00554 output("'"); // Starting single quote. 00555 const char *Base = S.data(); 00556 while (j < End) { 00557 // Escape a single quote by doubling it. 00558 if (S[j] == '\'') { 00559 output(StringRef(&Base[i], j - i + 1)); 00560 output("'"); 00561 i = j + 1; 00562 } 00563 ++j; 00564 } 00565 output(StringRef(&Base[i], j - i)); 00566 this->outputUpToEndOfLine("'"); // Ending single quote. 00567 } 00568 00569 void Output::setError(const Twine &message) { 00570 } 00571 00572 bool Output::canElideEmptySequence() { 00573 // Normally, with an optional key/value where the value is an empty sequence, 00574 // the whole key/value can be not written. But, that produces wrong yaml 00575 // if the key/value is the only thing in the map and the map is used in 00576 // a sequence. This detects if the this sequence is the first key/value 00577 // in map that itself is embedded in a sequnce. 00578 if (StateStack.size() < 2) 00579 return true; 00580 if (StateStack.back() != inMapFirstKey) 00581 return true; 00582 return (StateStack[StateStack.size()-2] != inSeq); 00583 } 00584 00585 void Output::output(StringRef s) { 00586 Column += s.size(); 00587 Out << s; 00588 } 00589 00590 void Output::outputUpToEndOfLine(StringRef s) { 00591 this->output(s); 00592 if (StateStack.empty() || StateStack.back() != inFlowSeq) 00593 NeedsNewLine = true; 00594 } 00595 00596 void Output::outputNewLine() { 00597 Out << "\n"; 00598 Column = 0; 00599 } 00600 00601 // if seq at top, indent as if map, then add "- " 00602 // if seq in middle, use "- " if firstKey, else use " " 00603 // 00604 00605 void Output::newLineCheck() { 00606 if (!NeedsNewLine) 00607 return; 00608 NeedsNewLine = false; 00609 00610 this->outputNewLine(); 00611 00612 assert(StateStack.size() > 0); 00613 unsigned Indent = StateStack.size() - 1; 00614 bool OutputDash = false; 00615 00616 if (StateStack.back() == inSeq) { 00617 OutputDash = true; 00618 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) && 00619 (StateStack[StateStack.size() - 2] == inSeq)) { 00620 --Indent; 00621 OutputDash = true; 00622 } 00623 00624 for (unsigned i = 0; i < Indent; ++i) { 00625 output(" "); 00626 } 00627 if (OutputDash) { 00628 output("- "); 00629 } 00630 00631 } 00632 00633 void Output::paddedKey(StringRef key) { 00634 output(key); 00635 output(":"); 00636 const char *spaces = " "; 00637 if (key.size() < strlen(spaces)) 00638 output(&spaces[key.size()]); 00639 else 00640 output(" "); 00641 } 00642 00643 //===----------------------------------------------------------------------===// 00644 // traits for built-in types 00645 //===----------------------------------------------------------------------===// 00646 00647 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { 00648 Out << (Val ? "true" : "false"); 00649 } 00650 00651 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) { 00652 if (Scalar.equals("true")) { 00653 Val = true; 00654 return StringRef(); 00655 } else if (Scalar.equals("false")) { 00656 Val = false; 00657 return StringRef(); 00658 } 00659 return "invalid boolean"; 00660 } 00661 00662 void ScalarTraits<StringRef>::output(const StringRef &Val, void *, 00663 raw_ostream &Out) { 00664 Out << Val; 00665 } 00666 00667 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, 00668 StringRef &Val) { 00669 Val = Scalar; 00670 return StringRef(); 00671 } 00672 00673 void ScalarTraits<std::string>::output(const std::string &Val, void *, 00674 raw_ostream &Out) { 00675 Out << Val; 00676 } 00677 00678 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, 00679 std::string &Val) { 00680 Val = Scalar.str(); 00681 return StringRef(); 00682 } 00683 00684 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *, 00685 raw_ostream &Out) { 00686 // use temp uin32_t because ostream thinks uint8_t is a character 00687 uint32_t Num = Val; 00688 Out << Num; 00689 } 00690 00691 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) { 00692 unsigned long long n; 00693 if (getAsUnsignedInteger(Scalar, 0, n)) 00694 return "invalid number"; 00695 if (n > 0xFF) 00696 return "out of range number"; 00697 Val = n; 00698 return StringRef(); 00699 } 00700 00701 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, 00702 raw_ostream &Out) { 00703 Out << Val; 00704 } 00705 00706 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *, 00707 uint16_t &Val) { 00708 unsigned long long n; 00709 if (getAsUnsignedInteger(Scalar, 0, n)) 00710 return "invalid number"; 00711 if (n > 0xFFFF) 00712 return "out of range number"; 00713 Val = n; 00714 return StringRef(); 00715 } 00716 00717 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, 00718 raw_ostream &Out) { 00719 Out << Val; 00720 } 00721 00722 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *, 00723 uint32_t &Val) { 00724 unsigned long long n; 00725 if (getAsUnsignedInteger(Scalar, 0, n)) 00726 return "invalid number"; 00727 if (n > 0xFFFFFFFFUL) 00728 return "out of range number"; 00729 Val = n; 00730 return StringRef(); 00731 } 00732 00733 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, 00734 raw_ostream &Out) { 00735 Out << Val; 00736 } 00737 00738 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *, 00739 uint64_t &Val) { 00740 unsigned long long N; 00741 if (getAsUnsignedInteger(Scalar, 0, N)) 00742 return "invalid number"; 00743 Val = N; 00744 return StringRef(); 00745 } 00746 00747 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) { 00748 // use temp in32_t because ostream thinks int8_t is a character 00749 int32_t Num = Val; 00750 Out << Num; 00751 } 00752 00753 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) { 00754 long long N; 00755 if (getAsSignedInteger(Scalar, 0, N)) 00756 return "invalid number"; 00757 if ((N > 127) || (N < -128)) 00758 return "out of range number"; 00759 Val = N; 00760 return StringRef(); 00761 } 00762 00763 void ScalarTraits<int16_t>::output(const int16_t &Val, void *, 00764 raw_ostream &Out) { 00765 Out << Val; 00766 } 00767 00768 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) { 00769 long long N; 00770 if (getAsSignedInteger(Scalar, 0, N)) 00771 return "invalid number"; 00772 if ((N > INT16_MAX) || (N < INT16_MIN)) 00773 return "out of range number"; 00774 Val = N; 00775 return StringRef(); 00776 } 00777 00778 void ScalarTraits<int32_t>::output(const int32_t &Val, void *, 00779 raw_ostream &Out) { 00780 Out << Val; 00781 } 00782 00783 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) { 00784 long long N; 00785 if (getAsSignedInteger(Scalar, 0, N)) 00786 return "invalid number"; 00787 if ((N > INT32_MAX) || (N < INT32_MIN)) 00788 return "out of range number"; 00789 Val = N; 00790 return StringRef(); 00791 } 00792 00793 void ScalarTraits<int64_t>::output(const int64_t &Val, void *, 00794 raw_ostream &Out) { 00795 Out << Val; 00796 } 00797 00798 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) { 00799 long long N; 00800 if (getAsSignedInteger(Scalar, 0, N)) 00801 return "invalid number"; 00802 Val = N; 00803 return StringRef(); 00804 } 00805 00806 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { 00807 Out << format("%g", Val); 00808 } 00809 00810 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) { 00811 SmallString<32> buff(Scalar.begin(), Scalar.end()); 00812 char *end; 00813 Val = strtod(buff.c_str(), &end); 00814 if (*end != '\0') 00815 return "invalid floating point number"; 00816 return StringRef(); 00817 } 00818 00819 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { 00820 Out << format("%g", Val); 00821 } 00822 00823 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) { 00824 SmallString<32> buff(Scalar.begin(), Scalar.end()); 00825 char *end; 00826 Val = strtod(buff.c_str(), &end); 00827 if (*end != '\0') 00828 return "invalid floating point number"; 00829 return StringRef(); 00830 } 00831 00832 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { 00833 uint8_t Num = Val; 00834 Out << format("0x%02X", Num); 00835 } 00836 00837 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) { 00838 unsigned long long n; 00839 if (getAsUnsignedInteger(Scalar, 0, n)) 00840 return "invalid hex8 number"; 00841 if (n > 0xFF) 00842 return "out of range hex8 number"; 00843 Val = n; 00844 return StringRef(); 00845 } 00846 00847 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { 00848 uint16_t Num = Val; 00849 Out << format("0x%04X", Num); 00850 } 00851 00852 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) { 00853 unsigned long long n; 00854 if (getAsUnsignedInteger(Scalar, 0, n)) 00855 return "invalid hex16 number"; 00856 if (n > 0xFFFF) 00857 return "out of range hex16 number"; 00858 Val = n; 00859 return StringRef(); 00860 } 00861 00862 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { 00863 uint32_t Num = Val; 00864 Out << format("0x%08X", Num); 00865 } 00866 00867 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) { 00868 unsigned long long n; 00869 if (getAsUnsignedInteger(Scalar, 0, n)) 00870 return "invalid hex32 number"; 00871 if (n > 0xFFFFFFFFUL) 00872 return "out of range hex32 number"; 00873 Val = n; 00874 return StringRef(); 00875 } 00876 00877 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { 00878 uint64_t Num = Val; 00879 Out << format("0x%016llX", Num); 00880 } 00881 00882 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) { 00883 unsigned long long Num; 00884 if (getAsUnsignedInteger(Scalar, 0, Num)) 00885 return "invalid hex64 number"; 00886 Val = Num; 00887 return StringRef(); 00888 }