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/Errc.h"
11 #include "llvm/Support/YAMLTraits.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/YAMLParser.h"
17 #include "llvm/Support/raw_ostream.h"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 IO::IO(void *Context) : Ctxt(Context) {
33 void *IO::getContext() {
37 void IO::setContext(void *Context) {
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
45 Input::Input(StringRef InputContent,
47 SourceMgr::DiagHandlerTy DiagHandler,
48 void *DiagHandlerCtxt)
50 Strm(new Stream(InputContent, SrcMgr)),
51 CurrentNode(nullptr) {
53 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
54 DocIterator = Strm->begin();
60 std::error_code Input::error() { return EC; }
62 // Pin the vtables to this file.
63 void Input::HNode::anchor() {}
64 void Input::EmptyHNode::anchor() {}
65 void Input::ScalarHNode::anchor() {}
67 bool Input::outputting() {
71 bool Input::setCurrentDocument() {
72 if (DocIterator != Strm->end()) {
73 Node *N = DocIterator->getRoot();
75 assert(Strm->failed() && "Root is NULL iff parsing failed");
76 EC = make_error_code(errc::invalid_argument);
80 if (isa<NullNode>(N)) {
81 // Empty files are allowed and ignored
83 return setCurrentDocument();
85 TopNode.reset(this->createHNodes(N));
86 CurrentNode = TopNode.get();
92 bool Input::nextDocument() {
93 return ++DocIterator != Strm->end();
96 bool Input::mapTag(StringRef Tag, bool Default) {
97 std::string foundTag = CurrentNode->_node->getVerbatimTag();
98 if (foundTag.empty()) {
99 // If no tag found and 'Tag' is the default, say it was found.
102 // Return true iff found tag matches supplied tag.
103 return Tag.equals(foundTag);
106 void Input::beginMapping() {
109 // CurrentNode can be null if the document is empty.
110 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
112 MN->ValidKeys.clear();
116 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
122 // CurrentNode is null for empty documents, which is an error in case required
123 // nodes are present.
126 EC = make_error_code(errc::invalid_argument);
130 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
132 setError(CurrentNode, "not a mapping");
135 MN->ValidKeys.push_back(Key);
136 HNode *Value = MN->Mapping[Key];
139 setError(CurrentNode, Twine("missing required key '") + Key + "'");
144 SaveInfo = CurrentNode;
149 void Input::postflightKey(void *saveInfo) {
150 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
153 void Input::endMapping() {
156 // CurrentNode can be null if the document is empty.
157 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
160 for (const auto &NN : MN->Mapping) {
161 if (!MN->isValidKey(NN.first())) {
162 setError(NN.second, Twine("unknown key '") + NN.first() + "'");
168 unsigned Input::beginSequence() {
169 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
170 return SQ->Entries.size();
175 void Input::endSequence() {
178 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
181 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
182 SaveInfo = CurrentNode;
183 CurrentNode = SQ->Entries[Index];
189 void Input::postflightElement(void *SaveInfo) {
190 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
193 unsigned Input::beginFlowSequence() {
194 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
195 return SQ->Entries.size();
200 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
203 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
204 SaveInfo = CurrentNode;
205 CurrentNode = SQ->Entries[index];
211 void Input::postflightFlowElement(void *SaveInfo) {
212 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
215 void Input::endFlowSequence() {
218 void Input::beginEnumScalar() {
219 ScalarMatchFound = false;
222 bool Input::matchEnumScalar(const char *Str, bool) {
223 if (ScalarMatchFound)
225 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
226 if (SN->value().equals(Str)) {
227 ScalarMatchFound = true;
234 void Input::endEnumScalar() {
235 if (!ScalarMatchFound) {
236 setError(CurrentNode, "unknown enumerated scalar");
240 bool Input::beginBitSetScalar(bool &DoClear) {
241 BitValuesUsed.clear();
242 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
243 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
245 setError(CurrentNode, "expected sequence of bit values");
251 bool Input::bitSetMatch(const char *Str, bool) {
254 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
256 for (HNode *N : SQ->Entries) {
257 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
258 if (SN->value().equals(Str)) {
259 BitValuesUsed[Index] = true;
263 setError(CurrentNode, "unexpected scalar in sequence of bit values");
268 setError(CurrentNode, "expected sequence of bit values");
273 void Input::endBitSetScalar() {
276 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
277 assert(BitValuesUsed.size() == SQ->Entries.size());
278 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
279 if (!BitValuesUsed[i]) {
280 setError(SQ->Entries[i], "unknown bit value");
287 void Input::scalarString(StringRef &S, bool) {
288 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
291 setError(CurrentNode, "unexpected scalar");
295 void Input::setError(HNode *hnode, const Twine &message) {
296 assert(hnode && "HNode must not be NULL");
297 this->setError(hnode->_node, message);
300 void Input::setError(Node *node, const Twine &message) {
301 Strm->printError(node, message);
302 EC = make_error_code(errc::invalid_argument);
305 Input::HNode *Input::createHNodes(Node *N) {
306 SmallString<128> StringStorage;
307 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
308 StringRef KeyStr = SN->getValue(StringStorage);
309 if (!StringStorage.empty()) {
310 // Copy string to permanent storage
311 unsigned Len = StringStorage.size();
312 char *Buf = StringAllocator.Allocate<char>(Len);
313 memcpy(Buf, &StringStorage[0], Len);
314 KeyStr = StringRef(Buf, Len);
316 return new ScalarHNode(N, KeyStr);
317 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
318 SequenceHNode *SQHNode = new SequenceHNode(N);
319 for (Node &SN : *SQ) {
320 HNode *Entry = this->createHNodes(&SN);
323 SQHNode->Entries.push_back(Entry);
326 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
327 MapHNode *mapHNode = new MapHNode(N);
328 for (KeyValueNode &KVN : *Map) {
329 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
330 StringStorage.clear();
331 StringRef KeyStr = KeyScalar->getValue(StringStorage);
332 if (!StringStorage.empty()) {
333 // Copy string to permanent storage
334 unsigned Len = StringStorage.size();
335 char *Buf = StringAllocator.Allocate<char>(Len);
336 memcpy(Buf, &StringStorage[0], Len);
337 KeyStr = StringRef(Buf, Len);
339 HNode *ValueHNode = this->createHNodes(KVN.getValue());
342 mapHNode->Mapping[KeyStr] = ValueHNode;
345 } else if (isa<NullNode>(N)) {
346 return new EmptyHNode(N);
348 setError(N, "unknown node kind");
353 bool Input::MapHNode::isValidKey(StringRef Key) {
354 for (const char *K : ValidKeys) {
361 void Input::setError(const Twine &Message) {
362 this->setError(CurrentNode, Message);
365 bool Input::canElideEmptySequence() {
369 Input::MapHNode::~MapHNode() {
370 for (auto &N : Mapping)
374 Input::SequenceHNode::~SequenceHNode() {
375 for (HNode *N : Entries)
381 //===----------------------------------------------------------------------===//
383 //===----------------------------------------------------------------------===//
385 Output::Output(raw_ostream &yout, void *context)
389 ColumnAtFlowStart(0),
390 NeedBitValueComma(false),
391 NeedFlowSequenceComma(false),
392 EnumerationMatchFound(false),
393 NeedsNewLine(false) {
399 bool Output::outputting() {
403 void Output::beginMapping() {
404 StateStack.push_back(inMapFirstKey);
408 bool Output::mapTag(StringRef Tag, bool Use) {
416 void Output::endMapping() {
417 StateStack.pop_back();
420 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
421 bool &UseDefault, void *&) {
423 if (Required || !SameAsDefault) {
424 this->newLineCheck();
425 this->paddedKey(Key);
431 void Output::postflightKey(void *) {
432 if (StateStack.back() == inMapFirstKey) {
433 StateStack.pop_back();
434 StateStack.push_back(inMapOtherKey);
438 void Output::beginDocuments() {
439 this->outputUpToEndOfLine("---");
442 bool Output::preflightDocument(unsigned index) {
444 this->outputUpToEndOfLine("\n---");
448 void Output::postflightDocument() {
451 void Output::endDocuments() {
455 unsigned Output::beginSequence() {
456 StateStack.push_back(inSeq);
461 void Output::endSequence() {
462 StateStack.pop_back();
465 bool Output::preflightElement(unsigned, void *&) {
469 void Output::postflightElement(void *) {
472 unsigned Output::beginFlowSequence() {
473 StateStack.push_back(inFlowSeq);
474 this->newLineCheck();
475 ColumnAtFlowStart = Column;
477 NeedFlowSequenceComma = false;
481 void Output::endFlowSequence() {
482 StateStack.pop_back();
483 this->outputUpToEndOfLine(" ]");
486 bool Output::preflightFlowElement(unsigned, void *&) {
487 if (NeedFlowSequenceComma)
491 for (int i = 0; i < ColumnAtFlowStart; ++i)
493 Column = ColumnAtFlowStart;
499 void Output::postflightFlowElement(void *) {
500 NeedFlowSequenceComma = true;
503 void Output::beginEnumScalar() {
504 EnumerationMatchFound = false;
507 bool Output::matchEnumScalar(const char *Str, bool Match) {
508 if (Match && !EnumerationMatchFound) {
509 this->newLineCheck();
510 this->outputUpToEndOfLine(Str);
511 EnumerationMatchFound = true;
516 void Output::endEnumScalar() {
517 if (!EnumerationMatchFound)
518 llvm_unreachable("bad runtime enum value");
521 bool Output::beginBitSetScalar(bool &DoClear) {
522 this->newLineCheck();
524 NeedBitValueComma = false;
529 bool Output::bitSetMatch(const char *Str, bool Matches) {
531 if (NeedBitValueComma)
534 NeedBitValueComma = true;
539 void Output::endBitSetScalar() {
540 this->outputUpToEndOfLine(" ]");
543 void Output::scalarString(StringRef &S, bool MustQuote) {
544 this->newLineCheck();
546 // Print '' for the empty string because leaving the field empty is not
548 this->outputUpToEndOfLine("''");
552 // Only quote if we must.
553 this->outputUpToEndOfLine(S);
558 unsigned End = S.size();
559 output("'"); // Starting single quote.
560 const char *Base = S.data();
562 // Escape a single quote by doubling it.
564 output(StringRef(&Base[i], j - i + 1));
570 output(StringRef(&Base[i], j - i));
571 this->outputUpToEndOfLine("'"); // Ending single quote.
574 void Output::setError(const Twine &message) {
577 bool Output::canElideEmptySequence() {
578 // Normally, with an optional key/value where the value is an empty sequence,
579 // the whole key/value can be not written. But, that produces wrong yaml
580 // if the key/value is the only thing in the map and the map is used in
581 // a sequence. This detects if the this sequence is the first key/value
582 // in map that itself is embedded in a sequnce.
583 if (StateStack.size() < 2)
585 if (StateStack.back() != inMapFirstKey)
587 return (StateStack[StateStack.size()-2] != inSeq);
590 void Output::output(StringRef s) {
595 void Output::outputUpToEndOfLine(StringRef s) {
597 if (StateStack.empty() || StateStack.back() != inFlowSeq)
601 void Output::outputNewLine() {
606 // if seq at top, indent as if map, then add "- "
607 // if seq in middle, use "- " if firstKey, else use " "
610 void Output::newLineCheck() {
613 NeedsNewLine = false;
615 this->outputNewLine();
617 assert(StateStack.size() > 0);
618 unsigned Indent = StateStack.size() - 1;
619 bool OutputDash = false;
621 if (StateStack.back() == inSeq) {
623 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
624 (StateStack[StateStack.size() - 2] == inSeq)) {
629 for (unsigned i = 0; i < Indent; ++i) {
638 void Output::paddedKey(StringRef key) {
641 const char *spaces = " ";
642 if (key.size() < strlen(spaces))
643 output(&spaces[key.size()]);
648 //===----------------------------------------------------------------------===//
649 // traits for built-in types
650 //===----------------------------------------------------------------------===//
652 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
653 Out << (Val ? "true" : "false");
656 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
657 if (Scalar.equals("true")) {
660 } else if (Scalar.equals("false")) {
664 return "invalid boolean";
667 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
672 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
678 void ScalarTraits<std::string>::output(const std::string &Val, void *,
683 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
689 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
691 // use temp uin32_t because ostream thinks uint8_t is a character
696 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
697 unsigned long long n;
698 if (getAsUnsignedInteger(Scalar, 0, n))
699 return "invalid number";
701 return "out of range number";
706 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
711 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
713 unsigned long long n;
714 if (getAsUnsignedInteger(Scalar, 0, n))
715 return "invalid number";
717 return "out of range number";
722 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
727 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
729 unsigned long long n;
730 if (getAsUnsignedInteger(Scalar, 0, n))
731 return "invalid number";
732 if (n > 0xFFFFFFFFUL)
733 return "out of range number";
738 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
743 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
745 unsigned long long N;
746 if (getAsUnsignedInteger(Scalar, 0, N))
747 return "invalid number";
752 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
753 // use temp in32_t because ostream thinks int8_t is a character
758 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
760 if (getAsSignedInteger(Scalar, 0, N))
761 return "invalid number";
762 if ((N > 127) || (N < -128))
763 return "out of range number";
768 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
773 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
775 if (getAsSignedInteger(Scalar, 0, N))
776 return "invalid number";
777 if ((N > INT16_MAX) || (N < INT16_MIN))
778 return "out of range number";
783 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
788 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
790 if (getAsSignedInteger(Scalar, 0, N))
791 return "invalid number";
792 if ((N > INT32_MAX) || (N < INT32_MIN))
793 return "out of range number";
798 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
803 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
805 if (getAsSignedInteger(Scalar, 0, N))
806 return "invalid number";
811 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
812 Out << format("%g", Val);
815 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
816 SmallString<32> buff(Scalar.begin(), Scalar.end());
818 Val = strtod(buff.c_str(), &end);
820 return "invalid floating point number";
824 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
825 Out << format("%g", Val);
828 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
829 SmallString<32> buff(Scalar.begin(), Scalar.end());
831 Val = strtod(buff.c_str(), &end);
833 return "invalid floating point number";
837 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
839 Out << format("0x%02X", Num);
842 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
843 unsigned long long n;
844 if (getAsUnsignedInteger(Scalar, 0, n))
845 return "invalid hex8 number";
847 return "out of range hex8 number";
852 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
854 Out << format("0x%04X", Num);
857 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
858 unsigned long long n;
859 if (getAsUnsignedInteger(Scalar, 0, n))
860 return "invalid hex16 number";
862 return "out of range hex16 number";
867 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
869 Out << format("0x%08X", Num);
872 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
873 unsigned long long n;
874 if (getAsUnsignedInteger(Scalar, 0, n))
875 return "invalid hex32 number";
876 if (n > 0xFFFFFFFFUL)
877 return "out of range hex32 number";
882 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
884 Out << format("0x%016llX", Num);
887 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
888 unsigned long long Num;
889 if (getAsUnsignedInteger(Scalar, 0, Num))
890 return "invalid hex64 number";