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/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/LineIterator.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Support/raw_ostream.h"
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 IO::IO(void *Context) : Ctxt(Context) {}
43 void *IO::getContext() {
47 void IO::setContext(void *Context) {
51 //===----------------------------------------------------------------------===//
53 //===----------------------------------------------------------------------===//
55 Input::Input(StringRef InputContent, void *Ctxt,
56 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
57 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
59 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
60 DocIterator = Strm->begin();
63 Input::Input(MemoryBufferRef Input, void *Ctxt,
64 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
65 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
67 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
68 DocIterator = Strm->begin();
71 Input::~Input() = default;
73 std::error_code Input::error() { return EC; }
75 // Pin the vtables to this file.
76 void Input::HNode::anchor() {}
77 void Input::EmptyHNode::anchor() {}
78 void Input::ScalarHNode::anchor() {}
79 void Input::MapHNode::anchor() {}
80 void Input::SequenceHNode::anchor() {}
82 bool Input::outputting() {
86 bool Input::setCurrentDocument() {
87 if (DocIterator != Strm->end()) {
88 Node *N = DocIterator->getRoot();
90 assert(Strm->failed() && "Root is NULL iff parsing failed");
91 EC = make_error_code(errc::invalid_argument);
95 if (isa<NullNode>(N)) {
96 // Empty files are allowed and ignored
98 return setCurrentDocument();
100 TopNode = this->createHNodes(N);
101 CurrentNode = TopNode.get();
107 bool Input::nextDocument() {
108 return ++DocIterator != Strm->end();
111 const Node *Input::getCurrentNode() const {
112 return CurrentNode ? CurrentNode->_node : nullptr;
115 bool Input::mapTag(StringRef Tag, bool Default) {
116 std::string foundTag = CurrentNode->_node->getVerbatimTag();
117 if (foundTag.empty()) {
118 // If no tag found and 'Tag' is the default, say it was found.
121 // Return true iff found tag matches supplied tag.
122 return Tag.equals(foundTag);
125 void Input::beginMapping() {
128 // CurrentNode can be null if the document is empty.
129 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
131 MN->ValidKeys.clear();
135 std::vector<StringRef> Input::keys() {
136 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
137 std::vector<StringRef> Ret;
139 setError(CurrentNode, "not a mapping");
142 for (auto &P : MN->Mapping)
143 Ret.push_back(P.first());
147 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
153 // CurrentNode is null for empty documents, which is an error in case required
154 // nodes are present.
157 EC = make_error_code(errc::invalid_argument);
161 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
163 setError(CurrentNode, "not a mapping");
166 MN->ValidKeys.push_back(Key);
167 HNode *Value = MN->Mapping[Key].get();
170 setError(CurrentNode, Twine("missing required key '") + Key + "'");
175 SaveInfo = CurrentNode;
180 void Input::postflightKey(void *saveInfo) {
181 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
184 void Input::endMapping() {
187 // CurrentNode can be null if the document is empty.
188 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
191 for (const auto &NN : MN->Mapping) {
192 if (!is_contained(MN->ValidKeys, NN.first())) {
193 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
199 void Input::beginFlowMapping() { beginMapping(); }
201 void Input::endFlowMapping() { endMapping(); }
203 unsigned Input::beginSequence() {
204 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
205 return SQ->Entries.size();
206 if (isa<EmptyHNode>(CurrentNode))
208 // Treat case where there's a scalar "null" value as an empty sequence.
209 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
210 if (isNull(SN->value()))
213 // Any other type of HNode is an error.
214 setError(CurrentNode, "not a sequence");
218 void Input::endSequence() {
221 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
224 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
225 SaveInfo = CurrentNode;
226 CurrentNode = SQ->Entries[Index].get();
232 void Input::postflightElement(void *SaveInfo) {
233 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
236 unsigned Input::beginFlowSequence() { return beginSequence(); }
238 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
241 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
242 SaveInfo = CurrentNode;
243 CurrentNode = SQ->Entries[index].get();
249 void Input::postflightFlowElement(void *SaveInfo) {
250 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
253 void Input::endFlowSequence() {
256 void Input::beginEnumScalar() {
257 ScalarMatchFound = false;
260 bool Input::matchEnumScalar(const char *Str, bool) {
261 if (ScalarMatchFound)
263 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
264 if (SN->value().equals(Str)) {
265 ScalarMatchFound = true;
272 bool Input::matchEnumFallback() {
273 if (ScalarMatchFound)
275 ScalarMatchFound = true;
279 void Input::endEnumScalar() {
280 if (!ScalarMatchFound) {
281 setError(CurrentNode, "unknown enumerated scalar");
285 bool Input::beginBitSetScalar(bool &DoClear) {
286 BitValuesUsed.clear();
287 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
288 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
290 setError(CurrentNode, "expected sequence of bit values");
296 bool Input::bitSetMatch(const char *Str, bool) {
299 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
301 for (auto &N : SQ->Entries) {
302 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
303 if (SN->value().equals(Str)) {
304 BitValuesUsed[Index] = true;
308 setError(CurrentNode, "unexpected scalar in sequence of bit values");
313 setError(CurrentNode, "expected sequence of bit values");
318 void Input::endBitSetScalar() {
321 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
322 assert(BitValuesUsed.size() == SQ->Entries.size());
323 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
324 if (!BitValuesUsed[i]) {
325 setError(SQ->Entries[i].get(), "unknown bit value");
332 void Input::scalarString(StringRef &S, bool) {
333 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
336 setError(CurrentNode, "unexpected scalar");
340 void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
342 void Input::setError(HNode *hnode, const Twine &message) {
343 assert(hnode && "HNode must not be NULL");
344 this->setError(hnode->_node, message);
347 void Input::setError(Node *node, const Twine &message) {
348 Strm->printError(node, message);
349 EC = make_error_code(errc::invalid_argument);
352 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
353 SmallString<128> StringStorage;
354 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
355 StringRef KeyStr = SN->getValue(StringStorage);
356 if (!StringStorage.empty()) {
357 // Copy string to permanent storage
358 KeyStr = StringStorage.str().copy(StringAllocator);
360 return llvm::make_unique<ScalarHNode>(N, KeyStr);
361 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
362 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
363 return llvm::make_unique<ScalarHNode>(N, ValueCopy);
364 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
365 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
366 for (Node &SN : *SQ) {
367 auto Entry = this->createHNodes(&SN);
370 SQHNode->Entries.push_back(std::move(Entry));
372 return std::move(SQHNode);
373 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
374 auto mapHNode = llvm::make_unique<MapHNode>(N);
375 for (KeyValueNode &KVN : *Map) {
376 Node *KeyNode = KVN.getKey();
377 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
379 setError(KeyNode, "Map key must be a scalar");
382 StringStorage.clear();
383 StringRef KeyStr = KeyScalar->getValue(StringStorage);
384 if (!StringStorage.empty()) {
385 // Copy string to permanent storage
386 KeyStr = StringStorage.str().copy(StringAllocator);
388 auto ValueHNode = this->createHNodes(KVN.getValue());
391 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
393 return std::move(mapHNode);
394 } else if (isa<NullNode>(N)) {
395 return llvm::make_unique<EmptyHNode>(N);
397 setError(N, "unknown node kind");
402 void Input::setError(const Twine &Message) {
403 this->setError(CurrentNode, Message);
406 bool Input::canElideEmptySequence() {
410 //===----------------------------------------------------------------------===//
412 //===----------------------------------------------------------------------===//
414 Output::Output(raw_ostream &yout, void *context, int WrapColumn)
415 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
417 Output::~Output() = default;
419 bool Output::outputting() {
423 void Output::beginMapping() {
424 StateStack.push_back(inMapFirstKey);
428 bool Output::mapTag(StringRef Tag, bool Use) {
430 // If this tag is being written inside a sequence we should write the start
431 // of the sequence before writing the tag, otherwise the tag won't be
432 // attached to the element in the sequence, but rather the sequence itself.
433 bool SequenceElement =
434 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
435 StateStack[StateStack.size() - 2] == inFlowSeq);
436 if (SequenceElement && StateStack.back() == inMapFirstKey) {
437 this->newLineCheck();
442 if (SequenceElement) {
443 // If we're writing the tag during the first element of a map, the tag
444 // takes the place of the first element in the sequence.
445 if (StateStack.back() == inMapFirstKey) {
446 StateStack.pop_back();
447 StateStack.push_back(inMapOtherKey);
449 // Tags inside maps in sequences should act as keys in the map from a
450 // formatting perspective, so we always want a newline in a sequence.
457 void Output::endMapping() {
458 StateStack.pop_back();
461 std::vector<StringRef> Output::keys() {
462 report_fatal_error("invalid call");
465 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
466 bool &UseDefault, void *&) {
468 if (Required || !SameAsDefault || WriteDefaultValues) {
469 auto State = StateStack.back();
470 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
473 this->newLineCheck();
474 this->paddedKey(Key);
481 void Output::postflightKey(void *) {
482 if (StateStack.back() == inMapFirstKey) {
483 StateStack.pop_back();
484 StateStack.push_back(inMapOtherKey);
485 } else if (StateStack.back() == inFlowMapFirstKey) {
486 StateStack.pop_back();
487 StateStack.push_back(inFlowMapOtherKey);
491 void Output::beginFlowMapping() {
492 StateStack.push_back(inFlowMapFirstKey);
493 this->newLineCheck();
494 ColumnAtMapFlowStart = Column;
498 void Output::endFlowMapping() {
499 StateStack.pop_back();
500 this->outputUpToEndOfLine(" }");
503 void Output::beginDocuments() {
504 this->outputUpToEndOfLine("---");
507 bool Output::preflightDocument(unsigned index) {
509 this->outputUpToEndOfLine("\n---");
513 void Output::postflightDocument() {
516 void Output::endDocuments() {
520 unsigned Output::beginSequence() {
521 StateStack.push_back(inSeq);
526 void Output::endSequence() {
527 StateStack.pop_back();
530 bool Output::preflightElement(unsigned, void *&) {
534 void Output::postflightElement(void *) {
537 unsigned Output::beginFlowSequence() {
538 StateStack.push_back(inFlowSeq);
539 this->newLineCheck();
540 ColumnAtFlowStart = Column;
542 NeedFlowSequenceComma = false;
546 void Output::endFlowSequence() {
547 StateStack.pop_back();
548 this->outputUpToEndOfLine(" ]");
551 bool Output::preflightFlowElement(unsigned, void *&) {
552 if (NeedFlowSequenceComma)
554 if (WrapColumn && Column > WrapColumn) {
556 for (int i = 0; i < ColumnAtFlowStart; ++i)
558 Column = ColumnAtFlowStart;
564 void Output::postflightFlowElement(void *) {
565 NeedFlowSequenceComma = true;
568 void Output::beginEnumScalar() {
569 EnumerationMatchFound = false;
572 bool Output::matchEnumScalar(const char *Str, bool Match) {
573 if (Match && !EnumerationMatchFound) {
574 this->newLineCheck();
575 this->outputUpToEndOfLine(Str);
576 EnumerationMatchFound = true;
581 bool Output::matchEnumFallback() {
582 if (EnumerationMatchFound)
584 EnumerationMatchFound = true;
588 void Output::endEnumScalar() {
589 if (!EnumerationMatchFound)
590 llvm_unreachable("bad runtime enum value");
593 bool Output::beginBitSetScalar(bool &DoClear) {
594 this->newLineCheck();
596 NeedBitValueComma = false;
601 bool Output::bitSetMatch(const char *Str, bool Matches) {
603 if (NeedBitValueComma)
606 NeedBitValueComma = true;
611 void Output::endBitSetScalar() {
612 this->outputUpToEndOfLine(" ]");
615 void Output::scalarString(StringRef &S, bool MustQuote) {
616 this->newLineCheck();
618 // Print '' for the empty string because leaving the field empty is not
620 this->outputUpToEndOfLine("''");
624 // Only quote if we must.
625 this->outputUpToEndOfLine(S);
630 unsigned End = S.size();
631 output("'"); // Starting single quote.
632 const char *Base = S.data();
634 // Escape a single quote by doubling it.
636 output(StringRef(&Base[i], j - i + 1));
642 output(StringRef(&Base[i], j - i));
643 this->outputUpToEndOfLine("'"); // Ending single quote.
646 void Output::blockScalarString(StringRef &S) {
647 if (!StateStack.empty())
652 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
654 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
655 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
656 for (unsigned I = 0; I < Indent; ++I) {
664 void Output::setError(const Twine &message) {
667 bool Output::canElideEmptySequence() {
668 // Normally, with an optional key/value where the value is an empty sequence,
669 // the whole key/value can be not written. But, that produces wrong yaml
670 // if the key/value is the only thing in the map and the map is used in
671 // a sequence. This detects if the this sequence is the first key/value
672 // in map that itself is embedded in a sequnce.
673 if (StateStack.size() < 2)
675 if (StateStack.back() != inMapFirstKey)
677 return (StateStack[StateStack.size()-2] != inSeq);
680 void Output::output(StringRef s) {
685 void Output::outputUpToEndOfLine(StringRef s) {
687 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
688 StateStack.back() != inFlowMapFirstKey &&
689 StateStack.back() != inFlowMapOtherKey))
693 void Output::outputNewLine() {
698 // if seq at top, indent as if map, then add "- "
699 // if seq in middle, use "- " if firstKey, else use " "
702 void Output::newLineCheck() {
705 NeedsNewLine = false;
707 this->outputNewLine();
709 assert(StateStack.size() > 0);
710 unsigned Indent = StateStack.size() - 1;
711 bool OutputDash = false;
713 if (StateStack.back() == inSeq) {
715 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
716 (StateStack.back() == inFlowSeq) ||
717 (StateStack.back() == inFlowMapFirstKey)) &&
718 (StateStack[StateStack.size() - 2] == inSeq)) {
723 for (unsigned i = 0; i < Indent; ++i) {
732 void Output::paddedKey(StringRef key) {
735 const char *spaces = " ";
736 if (key.size() < strlen(spaces))
737 output(&spaces[key.size()]);
742 void Output::flowKey(StringRef Key) {
743 if (StateStack.back() == inFlowMapOtherKey)
745 if (WrapColumn && Column > WrapColumn) {
747 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
749 Column = ColumnAtMapFlowStart;
756 //===----------------------------------------------------------------------===//
757 // traits for built-in types
758 //===----------------------------------------------------------------------===//
760 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
761 Out << (Val ? "true" : "false");
764 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
765 if (Scalar.equals("true")) {
768 } else if (Scalar.equals("false")) {
772 return "invalid boolean";
775 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
780 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
786 void ScalarTraits<std::string>::output(const std::string &Val, void *,
791 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
797 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
799 // use temp uin32_t because ostream thinks uint8_t is a character
804 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
805 unsigned long long n;
806 if (getAsUnsignedInteger(Scalar, 0, n))
807 return "invalid number";
809 return "out of range number";
814 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
819 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
821 unsigned long long n;
822 if (getAsUnsignedInteger(Scalar, 0, n))
823 return "invalid number";
825 return "out of range number";
830 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
835 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
837 unsigned long long n;
838 if (getAsUnsignedInteger(Scalar, 0, n))
839 return "invalid number";
840 if (n > 0xFFFFFFFFUL)
841 return "out of range number";
846 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
851 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
853 unsigned long long N;
854 if (getAsUnsignedInteger(Scalar, 0, N))
855 return "invalid number";
860 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
861 // use temp in32_t because ostream thinks int8_t is a character
866 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
868 if (getAsSignedInteger(Scalar, 0, N))
869 return "invalid number";
870 if ((N > 127) || (N < -128))
871 return "out of range number";
876 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
881 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
883 if (getAsSignedInteger(Scalar, 0, N))
884 return "invalid number";
885 if ((N > INT16_MAX) || (N < INT16_MIN))
886 return "out of range number";
891 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
896 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
898 if (getAsSignedInteger(Scalar, 0, N))
899 return "invalid number";
900 if ((N > INT32_MAX) || (N < INT32_MIN))
901 return "out of range number";
906 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
911 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
913 if (getAsSignedInteger(Scalar, 0, N))
914 return "invalid number";
919 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
920 Out << format("%g", Val);
923 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
924 if (to_float(Scalar, Val))
926 return "invalid floating point number";
929 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
930 Out << format("%g", Val);
933 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
934 if (to_float(Scalar, Val))
936 return "invalid floating point number";
939 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
941 Out << format("0x%02X", Num);
944 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
945 unsigned long long n;
946 if (getAsUnsignedInteger(Scalar, 0, n))
947 return "invalid hex8 number";
949 return "out of range hex8 number";
954 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
956 Out << format("0x%04X", Num);
959 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
960 unsigned long long n;
961 if (getAsUnsignedInteger(Scalar, 0, n))
962 return "invalid hex16 number";
964 return "out of range hex16 number";
969 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
971 Out << format("0x%08X", Num);
974 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
975 unsigned long long n;
976 if (getAsUnsignedInteger(Scalar, 0, n))
977 return "invalid hex32 number";
978 if (n > 0xFFFFFFFFUL)
979 return "out of range hex32 number";
984 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
986 Out << format("0x%016llX", Num);
989 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
990 unsigned long long Num;
991 if (getAsUnsignedInteger(Scalar, 0, Num))
992 return "invalid hex64 number";