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/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/LineIterator.h"
18 #include "llvm/Support/YAMLParser.h"
19 #include "llvm/Support/raw_ostream.h"
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 IO::IO(void *Context) : Ctxt(Context) {
35 void *IO::getContext() {
39 void IO::setContext(void *Context) {
43 //===----------------------------------------------------------------------===//
45 //===----------------------------------------------------------------------===//
47 Input::Input(StringRef InputContent, void *Ctxt,
48 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
49 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)),
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() {}
65 void Input::MapHNode::anchor() {}
66 void Input::SequenceHNode::anchor() {}
68 bool Input::outputting() {
72 bool Input::setCurrentDocument() {
73 if (DocIterator != Strm->end()) {
74 Node *N = DocIterator->getRoot();
76 assert(Strm->failed() && "Root is NULL iff parsing failed");
77 EC = make_error_code(errc::invalid_argument);
81 if (isa<NullNode>(N)) {
82 // Empty files are allowed and ignored
84 return setCurrentDocument();
86 TopNode = this->createHNodes(N);
87 CurrentNode = TopNode.get();
93 bool Input::nextDocument() {
94 return ++DocIterator != Strm->end();
97 const Node *Input::getCurrentNode() const {
98 return CurrentNode ? CurrentNode->_node : nullptr;
101 bool Input::mapTag(StringRef Tag, bool Default) {
102 std::string foundTag = CurrentNode->_node->getVerbatimTag();
103 if (foundTag.empty()) {
104 // If no tag found and 'Tag' is the default, say it was found.
107 // Return true iff found tag matches supplied tag.
108 return Tag.equals(foundTag);
111 void Input::beginMapping() {
114 // CurrentNode can be null if the document is empty.
115 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
117 MN->ValidKeys.clear();
121 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
127 // CurrentNode is null for empty documents, which is an error in case required
128 // nodes are present.
131 EC = make_error_code(errc::invalid_argument);
135 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
137 setError(CurrentNode, "not a mapping");
140 MN->ValidKeys.push_back(Key);
141 HNode *Value = MN->Mapping[Key].get();
144 setError(CurrentNode, Twine("missing required key '") + Key + "'");
149 SaveInfo = CurrentNode;
154 void Input::postflightKey(void *saveInfo) {
155 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
158 void Input::endMapping() {
161 // CurrentNode can be null if the document is empty.
162 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
165 for (const auto &NN : MN->Mapping) {
166 if (!MN->isValidKey(NN.first())) {
167 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
173 void Input::beginFlowMapping() { beginMapping(); }
175 void Input::endFlowMapping() { endMapping(); }
177 unsigned Input::beginSequence() {
178 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
179 return SQ->Entries.size();
180 if (isa<EmptyHNode>(CurrentNode))
182 // Treat case where there's a scalar "null" value as an empty sequence.
183 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
184 if (isNull(SN->value()))
187 // Any other type of HNode is an error.
188 setError(CurrentNode, "not a sequence");
192 void Input::endSequence() {
195 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
198 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
199 SaveInfo = CurrentNode;
200 CurrentNode = SQ->Entries[Index].get();
206 void Input::postflightElement(void *SaveInfo) {
207 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
210 unsigned Input::beginFlowSequence() { return beginSequence(); }
212 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
215 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
216 SaveInfo = CurrentNode;
217 CurrentNode = SQ->Entries[index].get();
223 void Input::postflightFlowElement(void *SaveInfo) {
224 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
227 void Input::endFlowSequence() {
230 void Input::beginEnumScalar() {
231 ScalarMatchFound = false;
234 bool Input::matchEnumScalar(const char *Str, bool) {
235 if (ScalarMatchFound)
237 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
238 if (SN->value().equals(Str)) {
239 ScalarMatchFound = true;
246 bool Input::matchEnumFallback() {
247 if (ScalarMatchFound)
249 ScalarMatchFound = true;
253 void Input::endEnumScalar() {
254 if (!ScalarMatchFound) {
255 setError(CurrentNode, "unknown enumerated scalar");
259 bool Input::beginBitSetScalar(bool &DoClear) {
260 BitValuesUsed.clear();
261 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
262 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
264 setError(CurrentNode, "expected sequence of bit values");
270 bool Input::bitSetMatch(const char *Str, bool) {
273 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
275 for (auto &N : SQ->Entries) {
276 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
277 if (SN->value().equals(Str)) {
278 BitValuesUsed[Index] = true;
282 setError(CurrentNode, "unexpected scalar in sequence of bit values");
287 setError(CurrentNode, "expected sequence of bit values");
292 void Input::endBitSetScalar() {
295 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
296 assert(BitValuesUsed.size() == SQ->Entries.size());
297 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
298 if (!BitValuesUsed[i]) {
299 setError(SQ->Entries[i].get(), "unknown bit value");
306 void Input::scalarString(StringRef &S, bool) {
307 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
310 setError(CurrentNode, "unexpected scalar");
314 void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
316 void Input::setError(HNode *hnode, const Twine &message) {
317 assert(hnode && "HNode must not be NULL");
318 this->setError(hnode->_node, message);
321 void Input::setError(Node *node, const Twine &message) {
322 Strm->printError(node, message);
323 EC = make_error_code(errc::invalid_argument);
326 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
327 SmallString<128> StringStorage;
328 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
329 StringRef KeyStr = SN->getValue(StringStorage);
330 if (!StringStorage.empty()) {
331 // Copy string to permanent storage
332 KeyStr = StringStorage.str().copy(StringAllocator);
334 return llvm::make_unique<ScalarHNode>(N, KeyStr);
335 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
336 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
337 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
338 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
339 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
340 for (Node &SN : *SQ) {
341 auto Entry = this->createHNodes(&SN);
344 SQHNode->Entries.push_back(std::move(Entry));
346 return std::move(SQHNode);
347 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
348 auto mapHNode = llvm::make_unique<MapHNode>(N);
349 for (KeyValueNode &KVN : *Map) {
350 Node *KeyNode = KVN.getKey();
351 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
353 setError(KeyNode, "Map key must be a scalar");
356 StringStorage.clear();
357 StringRef KeyStr = KeyScalar->getValue(StringStorage);
358 if (!StringStorage.empty()) {
359 // Copy string to permanent storage
360 KeyStr = StringStorage.str().copy(StringAllocator);
362 auto ValueHNode = this->createHNodes(KVN.getValue());
365 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
367 return std::move(mapHNode);
368 } else if (isa<NullNode>(N)) {
369 return llvm::make_unique<EmptyHNode>(N);
371 setError(N, "unknown node kind");
376 bool Input::MapHNode::isValidKey(StringRef Key) {
377 for (const char *K : ValidKeys) {
384 void Input::setError(const Twine &Message) {
385 this->setError(CurrentNode, Message);
388 bool Input::canElideEmptySequence() {
392 //===----------------------------------------------------------------------===//
394 //===----------------------------------------------------------------------===//
396 Output::Output(raw_ostream &yout, void *context, int WrapColumn)
399 WrapColumn(WrapColumn),
401 ColumnAtFlowStart(0),
402 ColumnAtMapFlowStart(0),
403 NeedBitValueComma(false),
404 NeedFlowSequenceComma(false),
405 EnumerationMatchFound(false),
406 NeedsNewLine(false) {
412 bool Output::outputting() {
416 void Output::beginMapping() {
417 StateStack.push_back(inMapFirstKey);
421 bool Output::mapTag(StringRef Tag, bool Use) {
423 // If this tag is being written inside a sequence we should write the start
424 // of the sequence before writing the tag, otherwise the tag won't be
425 // attached to the element in the sequence, but rather the sequence itself.
426 bool SequenceElement =
427 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
428 StateStack[StateStack.size() - 2] == inFlowSeq);
429 if (SequenceElement && StateStack.back() == inMapFirstKey) {
430 this->newLineCheck();
435 if (SequenceElement) {
436 // If we're writing the tag during the first element of a map, the tag
437 // takes the place of the first element in the sequence.
438 if (StateStack.back() == inMapFirstKey) {
439 StateStack.pop_back();
440 StateStack.push_back(inMapOtherKey);
442 // Tags inside maps in sequences should act as keys in the map from a
443 // formatting perspective, so we always want a newline in a sequence.
450 void Output::endMapping() {
451 StateStack.pop_back();
454 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
455 bool &UseDefault, void *&) {
457 if (Required || !SameAsDefault) {
458 auto State = StateStack.back();
459 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
462 this->newLineCheck();
463 this->paddedKey(Key);
470 void Output::postflightKey(void *) {
471 if (StateStack.back() == inMapFirstKey) {
472 StateStack.pop_back();
473 StateStack.push_back(inMapOtherKey);
474 } else if (StateStack.back() == inFlowMapFirstKey) {
475 StateStack.pop_back();
476 StateStack.push_back(inFlowMapOtherKey);
480 void Output::beginFlowMapping() {
481 StateStack.push_back(inFlowMapFirstKey);
482 this->newLineCheck();
483 ColumnAtMapFlowStart = Column;
487 void Output::endFlowMapping() {
488 StateStack.pop_back();
489 this->outputUpToEndOfLine(" }");
492 void Output::beginDocuments() {
493 this->outputUpToEndOfLine("---");
496 bool Output::preflightDocument(unsigned index) {
498 this->outputUpToEndOfLine("\n---");
502 void Output::postflightDocument() {
505 void Output::endDocuments() {
509 unsigned Output::beginSequence() {
510 StateStack.push_back(inSeq);
515 void Output::endSequence() {
516 StateStack.pop_back();
519 bool Output::preflightElement(unsigned, void *&) {
523 void Output::postflightElement(void *) {
526 unsigned Output::beginFlowSequence() {
527 StateStack.push_back(inFlowSeq);
528 this->newLineCheck();
529 ColumnAtFlowStart = Column;
531 NeedFlowSequenceComma = false;
535 void Output::endFlowSequence() {
536 StateStack.pop_back();
537 this->outputUpToEndOfLine(" ]");
540 bool Output::preflightFlowElement(unsigned, void *&) {
541 if (NeedFlowSequenceComma)
543 if (WrapColumn && Column > WrapColumn) {
545 for (int i = 0; i < ColumnAtFlowStart; ++i)
547 Column = ColumnAtFlowStart;
553 void Output::postflightFlowElement(void *) {
554 NeedFlowSequenceComma = true;
557 void Output::beginEnumScalar() {
558 EnumerationMatchFound = false;
561 bool Output::matchEnumScalar(const char *Str, bool Match) {
562 if (Match && !EnumerationMatchFound) {
563 this->newLineCheck();
564 this->outputUpToEndOfLine(Str);
565 EnumerationMatchFound = true;
570 bool Output::matchEnumFallback() {
571 if (EnumerationMatchFound)
573 EnumerationMatchFound = true;
577 void Output::endEnumScalar() {
578 if (!EnumerationMatchFound)
579 llvm_unreachable("bad runtime enum value");
582 bool Output::beginBitSetScalar(bool &DoClear) {
583 this->newLineCheck();
585 NeedBitValueComma = false;
590 bool Output::bitSetMatch(const char *Str, bool Matches) {
592 if (NeedBitValueComma)
595 NeedBitValueComma = true;
600 void Output::endBitSetScalar() {
601 this->outputUpToEndOfLine(" ]");
604 void Output::scalarString(StringRef &S, bool MustQuote) {
605 this->newLineCheck();
607 // Print '' for the empty string because leaving the field empty is not
609 this->outputUpToEndOfLine("''");
613 // Only quote if we must.
614 this->outputUpToEndOfLine(S);
619 unsigned End = S.size();
620 output("'"); // Starting single quote.
621 const char *Base = S.data();
623 // Escape a single quote by doubling it.
625 output(StringRef(&Base[i], j - i + 1));
631 output(StringRef(&Base[i], j - i));
632 this->outputUpToEndOfLine("'"); // Ending single quote.
635 void Output::blockScalarString(StringRef &S) {
636 if (!StateStack.empty())
641 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
643 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
644 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
645 for (unsigned I = 0; I < Indent; ++I) {
653 void Output::setError(const Twine &message) {
656 bool Output::canElideEmptySequence() {
657 // Normally, with an optional key/value where the value is an empty sequence,
658 // the whole key/value can be not written. But, that produces wrong yaml
659 // if the key/value is the only thing in the map and the map is used in
660 // a sequence. This detects if the this sequence is the first key/value
661 // in map that itself is embedded in a sequnce.
662 if (StateStack.size() < 2)
664 if (StateStack.back() != inMapFirstKey)
666 return (StateStack[StateStack.size()-2] != inSeq);
669 void Output::output(StringRef s) {
674 void Output::outputUpToEndOfLine(StringRef s) {
676 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
677 StateStack.back() != inFlowMapFirstKey &&
678 StateStack.back() != inFlowMapOtherKey))
682 void Output::outputNewLine() {
687 // if seq at top, indent as if map, then add "- "
688 // if seq in middle, use "- " if firstKey, else use " "
691 void Output::newLineCheck() {
694 NeedsNewLine = false;
696 this->outputNewLine();
698 assert(StateStack.size() > 0);
699 unsigned Indent = StateStack.size() - 1;
700 bool OutputDash = false;
702 if (StateStack.back() == inSeq) {
704 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
705 (StateStack.back() == inFlowSeq) ||
706 (StateStack.back() == inFlowMapFirstKey)) &&
707 (StateStack[StateStack.size() - 2] == inSeq)) {
712 for (unsigned i = 0; i < Indent; ++i) {
721 void Output::paddedKey(StringRef key) {
724 const char *spaces = " ";
725 if (key.size() < strlen(spaces))
726 output(&spaces[key.size()]);
731 void Output::flowKey(StringRef Key) {
732 if (StateStack.back() == inFlowMapOtherKey)
734 if (WrapColumn && Column > WrapColumn) {
736 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
738 Column = ColumnAtMapFlowStart;
745 //===----------------------------------------------------------------------===//
746 // traits for built-in types
747 //===----------------------------------------------------------------------===//
749 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
750 Out << (Val ? "true" : "false");
753 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
754 if (Scalar.equals("true")) {
757 } else if (Scalar.equals("false")) {
761 return "invalid boolean";
764 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
769 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
775 void ScalarTraits<std::string>::output(const std::string &Val, void *,
780 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
786 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
788 // use temp uin32_t because ostream thinks uint8_t is a character
793 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
794 unsigned long long n;
795 if (getAsUnsignedInteger(Scalar, 0, n))
796 return "invalid number";
798 return "out of range number";
803 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
808 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
810 unsigned long long n;
811 if (getAsUnsignedInteger(Scalar, 0, n))
812 return "invalid number";
814 return "out of range number";
819 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
824 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
826 unsigned long long n;
827 if (getAsUnsignedInteger(Scalar, 0, n))
828 return "invalid number";
829 if (n > 0xFFFFFFFFUL)
830 return "out of range number";
835 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
840 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
842 unsigned long long N;
843 if (getAsUnsignedInteger(Scalar, 0, N))
844 return "invalid number";
849 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
850 // use temp in32_t because ostream thinks int8_t is a character
855 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
857 if (getAsSignedInteger(Scalar, 0, N))
858 return "invalid number";
859 if ((N > 127) || (N < -128))
860 return "out of range number";
865 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
870 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
872 if (getAsSignedInteger(Scalar, 0, N))
873 return "invalid number";
874 if ((N > INT16_MAX) || (N < INT16_MIN))
875 return "out of range number";
880 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
885 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
887 if (getAsSignedInteger(Scalar, 0, N))
888 return "invalid number";
889 if ((N > INT32_MAX) || (N < INT32_MIN))
890 return "out of range number";
895 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
900 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
902 if (getAsSignedInteger(Scalar, 0, N))
903 return "invalid number";
908 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
909 Out << format("%g", Val);
912 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
913 SmallString<32> buff(Scalar.begin(), Scalar.end());
915 Val = strtod(buff.c_str(), &end);
917 return "invalid floating point number";
921 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
922 Out << format("%g", Val);
925 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
926 SmallString<32> buff(Scalar.begin(), Scalar.end());
928 Val = strtod(buff.c_str(), &end);
930 return "invalid floating point number";
934 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
936 Out << format("0x%02X", Num);
939 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
940 unsigned long long n;
941 if (getAsUnsignedInteger(Scalar, 0, n))
942 return "invalid hex8 number";
944 return "out of range hex8 number";
949 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
951 Out << format("0x%04X", Num);
954 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
955 unsigned long long n;
956 if (getAsUnsignedInteger(Scalar, 0, n))
957 return "invalid hex16 number";
959 return "out of range hex16 number";
964 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
966 Out << format("0x%08X", Num);
969 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
970 unsigned long long n;
971 if (getAsUnsignedInteger(Scalar, 0, n))
972 return "invalid hex32 number";
973 if (n > 0xFFFFFFFFUL)
974 return "out of range hex32 number";
979 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
981 Out << format("0x%016llX", Num);
984 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
985 unsigned long long Num;
986 if (getAsUnsignedInteger(Scalar, 0, Num))
987 return "invalid hex64 number";