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,
49 SourceMgr::DiagHandlerTy DiagHandler,
50 void *DiagHandlerCtxt)
52 Strm(new Stream(InputContent, SrcMgr)),
53 CurrentNode(nullptr) {
55 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
56 DocIterator = Strm->begin();
62 std::error_code Input::error() { return EC; }
64 // Pin the vtables to this file.
65 void Input::HNode::anchor() {}
66 void Input::EmptyHNode::anchor() {}
67 void Input::ScalarHNode::anchor() {}
68 void Input::MapHNode::anchor() {}
69 void Input::SequenceHNode::anchor() {}
71 bool Input::outputting() {
75 bool Input::setCurrentDocument() {
76 if (DocIterator != Strm->end()) {
77 Node *N = DocIterator->getRoot();
79 assert(Strm->failed() && "Root is NULL iff parsing failed");
80 EC = make_error_code(errc::invalid_argument);
84 if (isa<NullNode>(N)) {
85 // Empty files are allowed and ignored
87 return setCurrentDocument();
89 TopNode = this->createHNodes(N);
90 CurrentNode = TopNode.get();
96 bool Input::nextDocument() {
97 return ++DocIterator != Strm->end();
100 bool Input::mapTag(StringRef Tag, bool Default) {
101 std::string foundTag = CurrentNode->_node->getVerbatimTag();
102 if (foundTag.empty()) {
103 // If no tag found and 'Tag' is the default, say it was found.
106 // Return true iff found tag matches supplied tag.
107 return Tag.equals(foundTag);
110 void Input::beginMapping() {
113 // CurrentNode can be null if the document is empty.
114 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
116 MN->ValidKeys.clear();
120 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
126 // CurrentNode is null for empty documents, which is an error in case required
127 // nodes are present.
130 EC = make_error_code(errc::invalid_argument);
134 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
136 setError(CurrentNode, "not a mapping");
139 MN->ValidKeys.push_back(Key);
140 HNode *Value = MN->Mapping[Key].get();
143 setError(CurrentNode, Twine("missing required key '") + Key + "'");
148 SaveInfo = CurrentNode;
153 void Input::postflightKey(void *saveInfo) {
154 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
157 void Input::endMapping() {
160 // CurrentNode can be null if the document is empty.
161 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
164 for (const auto &NN : MN->Mapping) {
165 if (!MN->isValidKey(NN.first())) {
166 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
172 void Input::beginFlowMapping() { beginMapping(); }
174 void Input::endFlowMapping() { endMapping(); }
176 unsigned Input::beginSequence() {
177 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
178 return SQ->Entries.size();
179 if (isa<EmptyHNode>(CurrentNode))
181 // Treat case where there's a scalar "null" value as an empty sequence.
182 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
183 if (isNull(SN->value()))
186 // Any other type of HNode is an error.
187 setError(CurrentNode, "not a sequence");
191 void Input::endSequence() {
194 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
197 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
198 SaveInfo = CurrentNode;
199 CurrentNode = SQ->Entries[Index].get();
205 void Input::postflightElement(void *SaveInfo) {
206 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
209 unsigned Input::beginFlowSequence() { return beginSequence(); }
211 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
214 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
215 SaveInfo = CurrentNode;
216 CurrentNode = SQ->Entries[index].get();
222 void Input::postflightFlowElement(void *SaveInfo) {
223 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
226 void Input::endFlowSequence() {
229 void Input::beginEnumScalar() {
230 ScalarMatchFound = false;
233 bool Input::matchEnumScalar(const char *Str, bool) {
234 if (ScalarMatchFound)
236 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
237 if (SN->value().equals(Str)) {
238 ScalarMatchFound = true;
245 bool Input::matchEnumFallback() {
246 if (ScalarMatchFound)
248 ScalarMatchFound = true;
252 void Input::endEnumScalar() {
253 if (!ScalarMatchFound) {
254 setError(CurrentNode, "unknown enumerated scalar");
258 bool Input::beginBitSetScalar(bool &DoClear) {
259 BitValuesUsed.clear();
260 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
261 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
263 setError(CurrentNode, "expected sequence of bit values");
269 bool Input::bitSetMatch(const char *Str, bool) {
272 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
274 for (auto &N : SQ->Entries) {
275 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
276 if (SN->value().equals(Str)) {
277 BitValuesUsed[Index] = true;
281 setError(CurrentNode, "unexpected scalar in sequence of bit values");
286 setError(CurrentNode, "expected sequence of bit values");
291 void Input::endBitSetScalar() {
294 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
295 assert(BitValuesUsed.size() == SQ->Entries.size());
296 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
297 if (!BitValuesUsed[i]) {
298 setError(SQ->Entries[i].get(), "unknown bit value");
305 void Input::scalarString(StringRef &S, bool) {
306 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
309 setError(CurrentNode, "unexpected scalar");
313 void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
315 void Input::setError(HNode *hnode, const Twine &message) {
316 assert(hnode && "HNode must not be NULL");
317 this->setError(hnode->_node, message);
320 void Input::setError(Node *node, const Twine &message) {
321 Strm->printError(node, message);
322 EC = make_error_code(errc::invalid_argument);
325 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
326 SmallString<128> StringStorage;
327 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
328 StringRef KeyStr = SN->getValue(StringStorage);
329 if (!StringStorage.empty()) {
330 // Copy string to permanent storage
331 unsigned Len = StringStorage.size();
332 char *Buf = StringAllocator.Allocate<char>(Len);
333 memcpy(Buf, &StringStorage[0], Len);
334 KeyStr = StringRef(Buf, Len);
336 return llvm::make_unique<ScalarHNode>(N, KeyStr);
337 } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
338 StringRef Value = BSN->getValue();
339 char *Buf = StringAllocator.Allocate<char>(Value.size());
340 memcpy(Buf, Value.data(), Value.size());
341 return llvm::make_unique<ScalarHNode>(N, StringRef(Buf, Value.size()));
342 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
343 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
344 for (Node &SN : *SQ) {
345 auto Entry = this->createHNodes(&SN);
348 SQHNode->Entries.push_back(std::move(Entry));
350 return std::move(SQHNode);
351 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
352 auto mapHNode = llvm::make_unique<MapHNode>(N);
353 for (KeyValueNode &KVN : *Map) {
354 Node *KeyNode = KVN.getKey();
355 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
357 setError(KeyNode, "Map key must be a scalar");
360 StringStorage.clear();
361 StringRef KeyStr = KeyScalar->getValue(StringStorage);
362 if (!StringStorage.empty()) {
363 // Copy string to permanent storage
364 unsigned Len = StringStorage.size();
365 char *Buf = StringAllocator.Allocate<char>(Len);
366 memcpy(Buf, &StringStorage[0], Len);
367 KeyStr = StringRef(Buf, Len);
369 auto ValueHNode = this->createHNodes(KVN.getValue());
372 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
374 return std::move(mapHNode);
375 } else if (isa<NullNode>(N)) {
376 return llvm::make_unique<EmptyHNode>(N);
378 setError(N, "unknown node kind");
383 bool Input::MapHNode::isValidKey(StringRef Key) {
384 for (const char *K : ValidKeys) {
391 void Input::setError(const Twine &Message) {
392 this->setError(CurrentNode, Message);
395 bool Input::canElideEmptySequence() {
399 //===----------------------------------------------------------------------===//
401 //===----------------------------------------------------------------------===//
403 Output::Output(raw_ostream &yout, void *context)
407 ColumnAtFlowStart(0),
408 ColumnAtMapFlowStart(0),
409 NeedBitValueComma(false),
410 NeedFlowSequenceComma(false),
411 EnumerationMatchFound(false),
412 NeedsNewLine(false) {
418 bool Output::outputting() {
422 void Output::beginMapping() {
423 StateStack.push_back(inMapFirstKey);
427 bool Output::mapTag(StringRef Tag, bool Use) {
435 void Output::endMapping() {
436 StateStack.pop_back();
439 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
440 bool &UseDefault, void *&) {
442 if (Required || !SameAsDefault) {
443 auto State = StateStack.back();
444 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
447 this->newLineCheck();
448 this->paddedKey(Key);
455 void Output::postflightKey(void *) {
456 if (StateStack.back() == inMapFirstKey) {
457 StateStack.pop_back();
458 StateStack.push_back(inMapOtherKey);
459 } else if (StateStack.back() == inFlowMapFirstKey) {
460 StateStack.pop_back();
461 StateStack.push_back(inFlowMapOtherKey);
465 void Output::beginFlowMapping() {
466 StateStack.push_back(inFlowMapFirstKey);
467 this->newLineCheck();
468 ColumnAtMapFlowStart = Column;
472 void Output::endFlowMapping() {
473 StateStack.pop_back();
474 this->outputUpToEndOfLine(" }");
477 void Output::beginDocuments() {
478 this->outputUpToEndOfLine("---");
481 bool Output::preflightDocument(unsigned index) {
483 this->outputUpToEndOfLine("\n---");
487 void Output::postflightDocument() {
490 void Output::endDocuments() {
494 unsigned Output::beginSequence() {
495 StateStack.push_back(inSeq);
500 void Output::endSequence() {
501 StateStack.pop_back();
504 bool Output::preflightElement(unsigned, void *&) {
508 void Output::postflightElement(void *) {
511 unsigned Output::beginFlowSequence() {
512 StateStack.push_back(inFlowSeq);
513 this->newLineCheck();
514 ColumnAtFlowStart = Column;
516 NeedFlowSequenceComma = false;
520 void Output::endFlowSequence() {
521 StateStack.pop_back();
522 this->outputUpToEndOfLine(" ]");
525 bool Output::preflightFlowElement(unsigned, void *&) {
526 if (NeedFlowSequenceComma)
530 for (int i = 0; i < ColumnAtFlowStart; ++i)
532 Column = ColumnAtFlowStart;
538 void Output::postflightFlowElement(void *) {
539 NeedFlowSequenceComma = true;
542 void Output::beginEnumScalar() {
543 EnumerationMatchFound = false;
546 bool Output::matchEnumScalar(const char *Str, bool Match) {
547 if (Match && !EnumerationMatchFound) {
548 this->newLineCheck();
549 this->outputUpToEndOfLine(Str);
550 EnumerationMatchFound = true;
555 bool Output::matchEnumFallback() {
556 if (EnumerationMatchFound)
558 EnumerationMatchFound = true;
562 void Output::endEnumScalar() {
563 if (!EnumerationMatchFound)
564 llvm_unreachable("bad runtime enum value");
567 bool Output::beginBitSetScalar(bool &DoClear) {
568 this->newLineCheck();
570 NeedBitValueComma = false;
575 bool Output::bitSetMatch(const char *Str, bool Matches) {
577 if (NeedBitValueComma)
580 NeedBitValueComma = true;
585 void Output::endBitSetScalar() {
586 this->outputUpToEndOfLine(" ]");
589 void Output::scalarString(StringRef &S, bool MustQuote) {
590 this->newLineCheck();
592 // Print '' for the empty string because leaving the field empty is not
594 this->outputUpToEndOfLine("''");
598 // Only quote if we must.
599 this->outputUpToEndOfLine(S);
604 unsigned End = S.size();
605 output("'"); // Starting single quote.
606 const char *Base = S.data();
608 // Escape a single quote by doubling it.
610 output(StringRef(&Base[i], j - i + 1));
616 output(StringRef(&Base[i], j - i));
617 this->outputUpToEndOfLine("'"); // Ending single quote.
620 void Output::blockScalarString(StringRef &S) {
621 if (!StateStack.empty())
626 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
628 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
629 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
630 for (unsigned I = 0; I < Indent; ++I) {
638 void Output::setError(const Twine &message) {
641 bool Output::canElideEmptySequence() {
642 // Normally, with an optional key/value where the value is an empty sequence,
643 // the whole key/value can be not written. But, that produces wrong yaml
644 // if the key/value is the only thing in the map and the map is used in
645 // a sequence. This detects if the this sequence is the first key/value
646 // in map that itself is embedded in a sequnce.
647 if (StateStack.size() < 2)
649 if (StateStack.back() != inMapFirstKey)
651 return (StateStack[StateStack.size()-2] != inSeq);
654 void Output::output(StringRef s) {
659 void Output::outputUpToEndOfLine(StringRef s) {
661 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
662 StateStack.back() != inFlowMapFirstKey &&
663 StateStack.back() != inFlowMapOtherKey))
667 void Output::outputNewLine() {
672 // if seq at top, indent as if map, then add "- "
673 // if seq in middle, use "- " if firstKey, else use " "
676 void Output::newLineCheck() {
679 NeedsNewLine = false;
681 this->outputNewLine();
683 assert(StateStack.size() > 0);
684 unsigned Indent = StateStack.size() - 1;
685 bool OutputDash = false;
687 if (StateStack.back() == inSeq) {
689 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
690 (StateStack.back() == inFlowSeq) ||
691 (StateStack.back() == inFlowMapFirstKey)) &&
692 (StateStack[StateStack.size() - 2] == inSeq)) {
697 for (unsigned i = 0; i < Indent; ++i) {
706 void Output::paddedKey(StringRef key) {
709 const char *spaces = " ";
710 if (key.size() < strlen(spaces))
711 output(&spaces[key.size()]);
716 void Output::flowKey(StringRef Key) {
717 if (StateStack.back() == inFlowMapOtherKey)
721 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
723 Column = ColumnAtMapFlowStart;
730 //===----------------------------------------------------------------------===//
731 // traits for built-in types
732 //===----------------------------------------------------------------------===//
734 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
735 Out << (Val ? "true" : "false");
738 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
739 if (Scalar.equals("true")) {
742 } else if (Scalar.equals("false")) {
746 return "invalid boolean";
749 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
754 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
760 void ScalarTraits<std::string>::output(const std::string &Val, void *,
765 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
771 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
773 // use temp uin32_t because ostream thinks uint8_t is a character
778 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
779 unsigned long long n;
780 if (getAsUnsignedInteger(Scalar, 0, n))
781 return "invalid number";
783 return "out of range number";
788 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
793 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
795 unsigned long long n;
796 if (getAsUnsignedInteger(Scalar, 0, n))
797 return "invalid number";
799 return "out of range number";
804 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
809 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
811 unsigned long long n;
812 if (getAsUnsignedInteger(Scalar, 0, n))
813 return "invalid number";
814 if (n > 0xFFFFFFFFUL)
815 return "out of range number";
820 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
825 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
827 unsigned long long N;
828 if (getAsUnsignedInteger(Scalar, 0, N))
829 return "invalid number";
834 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
835 // use temp in32_t because ostream thinks int8_t is a character
840 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
842 if (getAsSignedInteger(Scalar, 0, N))
843 return "invalid number";
844 if ((N > 127) || (N < -128))
845 return "out of range number";
850 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
855 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
857 if (getAsSignedInteger(Scalar, 0, N))
858 return "invalid number";
859 if ((N > INT16_MAX) || (N < INT16_MIN))
860 return "out of range number";
865 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
870 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
872 if (getAsSignedInteger(Scalar, 0, N))
873 return "invalid number";
874 if ((N > INT32_MAX) || (N < INT32_MIN))
875 return "out of range number";
880 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
885 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
887 if (getAsSignedInteger(Scalar, 0, N))
888 return "invalid number";
893 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
894 Out << format("%g", Val);
897 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
898 SmallString<32> buff(Scalar.begin(), Scalar.end());
900 Val = strtod(buff.c_str(), &end);
902 return "invalid floating point number";
906 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
907 Out << format("%g", Val);
910 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
911 SmallString<32> buff(Scalar.begin(), Scalar.end());
913 Val = strtod(buff.c_str(), &end);
915 return "invalid floating point number";
919 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
921 Out << format("0x%02X", Num);
924 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
925 unsigned long long n;
926 if (getAsUnsignedInteger(Scalar, 0, n))
927 return "invalid hex8 number";
929 return "out of range hex8 number";
934 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
936 Out << format("0x%04X", Num);
939 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
940 unsigned long long n;
941 if (getAsUnsignedInteger(Scalar, 0, n))
942 return "invalid hex16 number";
944 return "out of range hex16 number";
949 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
951 Out << format("0x%08X", Num);
954 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
955 unsigned long long n;
956 if (getAsUnsignedInteger(Scalar, 0, n))
957 return "invalid hex32 number";
958 if (n > 0xFFFFFFFFUL)
959 return "out of range hex32 number";
964 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
966 Out << format("0x%016llX", Num);
969 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
970 unsigned long long Num;
971 if (getAsUnsignedInteger(Scalar, 0, Num))
972 return "invalid hex64 number";