1 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/YAMLTraits.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/raw_ostream.h"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 IO::IO(void *Context) : Ctxt(Context) {
32 void *IO::getContext() {
36 void IO::setContext(void *Context) {
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
44 Input::Input(StringRef InputContent,
46 SourceMgr::DiagHandlerTy DiagHandler,
47 void *DiagHandlerCtxt)
49 Strm(new Stream(InputContent, SrcMgr)),
50 CurrentNode(nullptr) {
52 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
53 DocIterator = Strm->begin();
59 std::error_code Input::error() { return EC; }
61 // Pin the vtables to this file.
62 void Input::HNode::anchor() {}
63 void Input::EmptyHNode::anchor() {}
64 void Input::ScalarHNode::anchor() {}
66 bool Input::outputting() {
70 bool Input::setCurrentDocument() {
71 if (DocIterator != Strm->end()) {
72 Node *N = DocIterator->getRoot();
74 assert(Strm->failed() && "Root is NULL iff parsing failed");
75 EC = std::make_error_code(std::errc::invalid_argument);
79 if (isa<NullNode>(N)) {
80 // Empty files are allowed and ignored
82 return setCurrentDocument();
84 TopNode.reset(this->createHNodes(N));
85 CurrentNode = TopNode.get();
91 bool Input::nextDocument() {
92 return ++DocIterator != Strm->end();
95 bool Input::mapTag(StringRef Tag, bool Default) {
96 std::string foundTag = CurrentNode->_node->getVerbatimTag();
97 if (foundTag.empty()) {
98 // If no tag found and 'Tag' is the default, say it was found.
101 // Return true iff found tag matches supplied tag.
102 return Tag.equals(foundTag);
105 void Input::beginMapping() {
108 // CurrentNode can be null if the document is empty.
109 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
111 MN->ValidKeys.clear();
115 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
121 // CurrentNode is null for empty documents, which is an error in case required
122 // nodes are present.
125 EC = std::make_error_code(std::errc::invalid_argument);
129 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
131 setError(CurrentNode, "not a mapping");
134 MN->ValidKeys.push_back(Key);
135 HNode *Value = MN->Mapping[Key];
138 setError(CurrentNode, Twine("missing required key '") + Key + "'");
143 SaveInfo = CurrentNode;
148 void Input::postflightKey(void *saveInfo) {
149 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
152 void Input::endMapping() {
155 // CurrentNode can be null if the document is empty.
156 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
159 for (const auto &NN : MN->Mapping) {
160 if (!MN->isValidKey(NN.first())) {
161 setError(NN.second, Twine("unknown key '") + NN.first() + "'");
167 unsigned Input::beginSequence() {
168 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
169 return SQ->Entries.size();
174 void Input::endSequence() {
177 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
180 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
181 SaveInfo = CurrentNode;
182 CurrentNode = SQ->Entries[Index];
188 void Input::postflightElement(void *SaveInfo) {
189 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
192 unsigned Input::beginFlowSequence() {
193 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
194 return SQ->Entries.size();
199 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
202 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
203 SaveInfo = CurrentNode;
204 CurrentNode = SQ->Entries[index];
210 void Input::postflightFlowElement(void *SaveInfo) {
211 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
214 void Input::endFlowSequence() {
217 void Input::beginEnumScalar() {
218 ScalarMatchFound = false;
221 bool Input::matchEnumScalar(const char *Str, bool) {
222 if (ScalarMatchFound)
224 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
225 if (SN->value().equals(Str)) {
226 ScalarMatchFound = true;
233 void Input::endEnumScalar() {
234 if (!ScalarMatchFound) {
235 setError(CurrentNode, "unknown enumerated scalar");
239 bool Input::beginBitSetScalar(bool &DoClear) {
240 BitValuesUsed.clear();
241 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
242 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
244 setError(CurrentNode, "expected sequence of bit values");
250 bool Input::bitSetMatch(const char *Str, bool) {
253 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
255 for (HNode *N : SQ->Entries) {
256 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
257 if (SN->value().equals(Str)) {
258 BitValuesUsed[Index] = true;
262 setError(CurrentNode, "unexpected scalar in sequence of bit values");
267 setError(CurrentNode, "expected sequence of bit values");
272 void Input::endBitSetScalar() {
275 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
276 assert(BitValuesUsed.size() == SQ->Entries.size());
277 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
278 if (!BitValuesUsed[i]) {
279 setError(SQ->Entries[i], "unknown bit value");
286 void Input::scalarString(StringRef &S, bool) {
287 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
290 setError(CurrentNode, "unexpected scalar");
294 void Input::setError(HNode *hnode, const Twine &message) {
295 assert(hnode && "HNode must not be NULL");
296 this->setError(hnode->_node, message);
299 void Input::setError(Node *node, const Twine &message) {
300 Strm->printError(node, message);
301 EC = std::make_error_code(std::errc::invalid_argument);
304 Input::HNode *Input::createHNodes(Node *N) {
305 SmallString<128> StringStorage;
306 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
307 StringRef KeyStr = SN->getValue(StringStorage);
308 if (!StringStorage.empty()) {
309 // Copy string to permanent storage
310 unsigned Len = StringStorage.size();
311 char *Buf = StringAllocator.Allocate<char>(Len);
312 memcpy(Buf, &StringStorage[0], Len);
313 KeyStr = StringRef(Buf, Len);
315 return new ScalarHNode(N, KeyStr);
316 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
317 SequenceHNode *SQHNode = new SequenceHNode(N);
318 for (Node &SN : *SQ) {
319 HNode *Entry = this->createHNodes(&SN);
322 SQHNode->Entries.push_back(Entry);
325 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
326 MapHNode *mapHNode = new MapHNode(N);
327 for (KeyValueNode &KVN : *Map) {
328 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
329 StringStorage.clear();
330 StringRef KeyStr = KeyScalar->getValue(StringStorage);
331 if (!StringStorage.empty()) {
332 // Copy string to permanent storage
333 unsigned Len = StringStorage.size();
334 char *Buf = StringAllocator.Allocate<char>(Len);
335 memcpy(Buf, &StringStorage[0], Len);
336 KeyStr = StringRef(Buf, Len);
338 HNode *ValueHNode = this->createHNodes(KVN.getValue());
341 mapHNode->Mapping[KeyStr] = ValueHNode;
344 } else if (isa<NullNode>(N)) {
345 return new EmptyHNode(N);
347 setError(N, "unknown node kind");
352 bool Input::MapHNode::isValidKey(StringRef Key) {
353 for (const char *K : ValidKeys) {
360 void Input::setError(const Twine &Message) {
361 this->setError(CurrentNode, Message);
364 bool Input::canElideEmptySequence() {
368 Input::MapHNode::~MapHNode() {
369 for (auto &N : Mapping)
373 Input::SequenceHNode::~SequenceHNode() {
374 for (HNode *N : Entries)
380 //===----------------------------------------------------------------------===//
382 //===----------------------------------------------------------------------===//
384 Output::Output(raw_ostream &yout, void *context)
388 ColumnAtFlowStart(0),
389 NeedBitValueComma(false),
390 NeedFlowSequenceComma(false),
391 EnumerationMatchFound(false),
392 NeedsNewLine(false) {
398 bool Output::outputting() {
402 void Output::beginMapping() {
403 StateStack.push_back(inMapFirstKey);
407 bool Output::mapTag(StringRef Tag, bool Use) {
415 void Output::endMapping() {
416 StateStack.pop_back();
419 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
420 bool &UseDefault, void *&) {
422 if (Required || !SameAsDefault) {
423 this->newLineCheck();
424 this->paddedKey(Key);
430 void Output::postflightKey(void *) {
431 if (StateStack.back() == inMapFirstKey) {
432 StateStack.pop_back();
433 StateStack.push_back(inMapOtherKey);
437 void Output::beginDocuments() {
438 this->outputUpToEndOfLine("---");
441 bool Output::preflightDocument(unsigned index) {
443 this->outputUpToEndOfLine("\n---");
447 void Output::postflightDocument() {
450 void Output::endDocuments() {
454 unsigned Output::beginSequence() {
455 StateStack.push_back(inSeq);
460 void Output::endSequence() {
461 StateStack.pop_back();
464 bool Output::preflightElement(unsigned, void *&) {
468 void Output::postflightElement(void *) {
471 unsigned Output::beginFlowSequence() {
472 StateStack.push_back(inFlowSeq);
473 this->newLineCheck();
474 ColumnAtFlowStart = Column;
476 NeedFlowSequenceComma = false;
480 void Output::endFlowSequence() {
481 StateStack.pop_back();
482 this->outputUpToEndOfLine(" ]");
485 bool Output::preflightFlowElement(unsigned, void *&) {
486 if (NeedFlowSequenceComma)
490 for (int i = 0; i < ColumnAtFlowStart; ++i)
492 Column = ColumnAtFlowStart;
498 void Output::postflightFlowElement(void *) {
499 NeedFlowSequenceComma = true;
502 void Output::beginEnumScalar() {
503 EnumerationMatchFound = false;
506 bool Output::matchEnumScalar(const char *Str, bool Match) {
507 if (Match && !EnumerationMatchFound) {
508 this->newLineCheck();
509 this->outputUpToEndOfLine(Str);
510 EnumerationMatchFound = true;
515 void Output::endEnumScalar() {
516 if (!EnumerationMatchFound)
517 llvm_unreachable("bad runtime enum value");
520 bool Output::beginBitSetScalar(bool &DoClear) {
521 this->newLineCheck();
523 NeedBitValueComma = false;
528 bool Output::bitSetMatch(const char *Str, bool Matches) {
530 if (NeedBitValueComma)
533 NeedBitValueComma = true;
538 void Output::endBitSetScalar() {
539 this->outputUpToEndOfLine(" ]");
542 void Output::scalarString(StringRef &S, bool MustQuote) {
543 this->newLineCheck();
545 // Print '' for the empty string because leaving the field empty is not
547 this->outputUpToEndOfLine("''");
551 // Only quote if we must.
552 this->outputUpToEndOfLine(S);
557 unsigned End = S.size();
558 output("'"); // Starting single quote.
559 const char *Base = S.data();
561 // Escape a single quote by doubling it.
563 output(StringRef(&Base[i], j - i + 1));
569 output(StringRef(&Base[i], j - i));
570 this->outputUpToEndOfLine("'"); // Ending single quote.
573 void Output::setError(const Twine &message) {
576 bool Output::canElideEmptySequence() {
577 // Normally, with an optional key/value where the value is an empty sequence,
578 // the whole key/value can be not written. But, that produces wrong yaml
579 // if the key/value is the only thing in the map and the map is used in
580 // a sequence. This detects if the this sequence is the first key/value
581 // in map that itself is embedded in a sequnce.
582 if (StateStack.size() < 2)
584 if (StateStack.back() != inMapFirstKey)
586 return (StateStack[StateStack.size()-2] != inSeq);
589 void Output::output(StringRef s) {
594 void Output::outputUpToEndOfLine(StringRef s) {
596 if (StateStack.empty() || StateStack.back() != inFlowSeq)
600 void Output::outputNewLine() {
605 // if seq at top, indent as if map, then add "- "
606 // if seq in middle, use "- " if firstKey, else use " "
609 void Output::newLineCheck() {
612 NeedsNewLine = false;
614 this->outputNewLine();
616 assert(StateStack.size() > 0);
617 unsigned Indent = StateStack.size() - 1;
618 bool OutputDash = false;
620 if (StateStack.back() == inSeq) {
622 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
623 (StateStack[StateStack.size() - 2] == inSeq)) {
628 for (unsigned i = 0; i < Indent; ++i) {
637 void Output::paddedKey(StringRef key) {
640 const char *spaces = " ";
641 if (key.size() < strlen(spaces))
642 output(&spaces[key.size()]);
647 //===----------------------------------------------------------------------===//
648 // traits for built-in types
649 //===----------------------------------------------------------------------===//
651 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
652 Out << (Val ? "true" : "false");
655 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
656 if (Scalar.equals("true")) {
659 } else if (Scalar.equals("false")) {
663 return "invalid boolean";
666 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
671 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
677 void ScalarTraits<std::string>::output(const std::string &Val, void *,
682 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
688 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
690 // use temp uin32_t because ostream thinks uint8_t is a character
695 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
696 unsigned long long n;
697 if (getAsUnsignedInteger(Scalar, 0, n))
698 return "invalid number";
700 return "out of range number";
705 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
710 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
712 unsigned long long n;
713 if (getAsUnsignedInteger(Scalar, 0, n))
714 return "invalid number";
716 return "out of range number";
721 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
726 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
728 unsigned long long n;
729 if (getAsUnsignedInteger(Scalar, 0, n))
730 return "invalid number";
731 if (n > 0xFFFFFFFFUL)
732 return "out of range number";
737 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
742 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
744 unsigned long long N;
745 if (getAsUnsignedInteger(Scalar, 0, N))
746 return "invalid number";
751 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
752 // use temp in32_t because ostream thinks int8_t is a character
757 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
759 if (getAsSignedInteger(Scalar, 0, N))
760 return "invalid number";
761 if ((N > 127) || (N < -128))
762 return "out of range number";
767 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
772 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
774 if (getAsSignedInteger(Scalar, 0, N))
775 return "invalid number";
776 if ((N > INT16_MAX) || (N < INT16_MIN))
777 return "out of range number";
782 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
787 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
789 if (getAsSignedInteger(Scalar, 0, N))
790 return "invalid number";
791 if ((N > INT32_MAX) || (N < INT32_MIN))
792 return "out of range number";
797 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
802 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
804 if (getAsSignedInteger(Scalar, 0, N))
805 return "invalid number";
810 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
811 Out << format("%g", Val);
814 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
815 SmallString<32> buff(Scalar.begin(), Scalar.end());
817 Val = strtod(buff.c_str(), &end);
819 return "invalid floating point number";
823 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
824 Out << format("%g", Val);
827 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
828 SmallString<32> buff(Scalar.begin(), Scalar.end());
830 Val = strtod(buff.c_str(), &end);
832 return "invalid floating point number";
836 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
838 Out << format("0x%02X", Num);
841 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
842 unsigned long long n;
843 if (getAsUnsignedInteger(Scalar, 0, n))
844 return "invalid hex8 number";
846 return "out of range hex8 number";
851 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
853 Out << format("0x%04X", Num);
856 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
857 unsigned long long n;
858 if (getAsUnsignedInteger(Scalar, 0, n))
859 return "invalid hex16 number";
861 return "out of range hex16 number";
866 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
868 Out << format("0x%08X", Num);
871 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
872 unsigned long long n;
873 if (getAsUnsignedInteger(Scalar, 0, n))
874 return "invalid hex32 number";
875 if (n > 0xFFFFFFFFUL)
876 return "out of range hex32 number";
881 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
883 Out << format("0x%016llX", Num);
886 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
887 unsigned long long Num;
888 if (getAsUnsignedInteger(Scalar, 0, Num))
889 return "invalid hex64 number";