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/YAMLParser.h"
18 #include "llvm/Support/raw_ostream.h"
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 IO::IO(void *Context) : Ctxt(Context) {
34 void *IO::getContext() {
38 void IO::setContext(void *Context) {
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
46 Input::Input(StringRef InputContent,
48 SourceMgr::DiagHandlerTy DiagHandler,
49 void *DiagHandlerCtxt)
51 Strm(new Stream(InputContent, SrcMgr)),
52 CurrentNode(nullptr) {
54 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
55 DocIterator = Strm->begin();
61 std::error_code Input::error() { return EC; }
63 // Pin the vtables to this file.
64 void Input::HNode::anchor() {}
65 void Input::EmptyHNode::anchor() {}
66 void Input::ScalarHNode::anchor() {}
67 void Input::MapHNode::anchor() {}
68 void Input::SequenceHNode::anchor() {}
70 bool Input::outputting() {
74 bool Input::setCurrentDocument() {
75 if (DocIterator != Strm->end()) {
76 Node *N = DocIterator->getRoot();
78 assert(Strm->failed() && "Root is NULL iff parsing failed");
79 EC = make_error_code(errc::invalid_argument);
83 if (isa<NullNode>(N)) {
84 // Empty files are allowed and ignored
86 return setCurrentDocument();
88 TopNode = this->createHNodes(N);
89 CurrentNode = TopNode.get();
95 bool Input::nextDocument() {
96 return ++DocIterator != Strm->end();
99 bool Input::mapTag(StringRef Tag, bool Default) {
100 std::string foundTag = CurrentNode->_node->getVerbatimTag();
101 if (foundTag.empty()) {
102 // If no tag found and 'Tag' is the default, say it was found.
105 // Return true iff found tag matches supplied tag.
106 return Tag.equals(foundTag);
109 void Input::beginMapping() {
112 // CurrentNode can be null if the document is empty.
113 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
115 MN->ValidKeys.clear();
119 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
125 // CurrentNode is null for empty documents, which is an error in case required
126 // nodes are present.
129 EC = make_error_code(errc::invalid_argument);
133 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
135 setError(CurrentNode, "not a mapping");
138 MN->ValidKeys.push_back(Key);
139 HNode *Value = MN->Mapping[Key].get();
142 setError(CurrentNode, Twine("missing required key '") + Key + "'");
147 SaveInfo = CurrentNode;
152 void Input::postflightKey(void *saveInfo) {
153 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
156 void Input::endMapping() {
159 // CurrentNode can be null if the document is empty.
160 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
163 for (const auto &NN : MN->Mapping) {
164 if (!MN->isValidKey(NN.first())) {
165 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
171 void Input::beginFlowMapping() { beginMapping(); }
173 void Input::endFlowMapping() { endMapping(); }
175 unsigned Input::beginSequence() {
176 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
177 return SQ->Entries.size();
178 if (isa<EmptyHNode>(CurrentNode))
180 // Treat case where there's a scalar "null" value as an empty sequence.
181 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
182 if (isNull(SN->value()))
185 // Any other type of HNode is an error.
186 setError(CurrentNode, "not a sequence");
190 void Input::endSequence() {
193 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
196 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
197 SaveInfo = CurrentNode;
198 CurrentNode = SQ->Entries[Index].get();
204 void Input::postflightElement(void *SaveInfo) {
205 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
208 unsigned Input::beginFlowSequence() { return beginSequence(); }
210 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
213 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
214 SaveInfo = CurrentNode;
215 CurrentNode = SQ->Entries[index].get();
221 void Input::postflightFlowElement(void *SaveInfo) {
222 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
225 void Input::endFlowSequence() {
228 void Input::beginEnumScalar() {
229 ScalarMatchFound = false;
232 bool Input::matchEnumScalar(const char *Str, bool) {
233 if (ScalarMatchFound)
235 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
236 if (SN->value().equals(Str)) {
237 ScalarMatchFound = true;
244 bool Input::matchEnumFallback() {
245 if (ScalarMatchFound)
247 ScalarMatchFound = true;
251 void Input::endEnumScalar() {
252 if (!ScalarMatchFound) {
253 setError(CurrentNode, "unknown enumerated scalar");
257 bool Input::beginBitSetScalar(bool &DoClear) {
258 BitValuesUsed.clear();
259 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
260 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
262 setError(CurrentNode, "expected sequence of bit values");
268 bool Input::bitSetMatch(const char *Str, bool) {
271 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
273 for (auto &N : SQ->Entries) {
274 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
275 if (SN->value().equals(Str)) {
276 BitValuesUsed[Index] = true;
280 setError(CurrentNode, "unexpected scalar in sequence of bit values");
285 setError(CurrentNode, "expected sequence of bit values");
290 void Input::endBitSetScalar() {
293 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
294 assert(BitValuesUsed.size() == SQ->Entries.size());
295 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
296 if (!BitValuesUsed[i]) {
297 setError(SQ->Entries[i].get(), "unknown bit value");
304 void Input::scalarString(StringRef &S, bool) {
305 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
308 setError(CurrentNode, "unexpected scalar");
312 void Input::setError(HNode *hnode, const Twine &message) {
313 assert(hnode && "HNode must not be NULL");
314 this->setError(hnode->_node, message);
317 void Input::setError(Node *node, const Twine &message) {
318 Strm->printError(node, message);
319 EC = make_error_code(errc::invalid_argument);
322 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
323 SmallString<128> StringStorage;
324 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
325 StringRef KeyStr = SN->getValue(StringStorage);
326 if (!StringStorage.empty()) {
327 // Copy string to permanent storage
328 unsigned Len = StringStorage.size();
329 char *Buf = StringAllocator.Allocate<char>(Len);
330 memcpy(Buf, &StringStorage[0], Len);
331 KeyStr = StringRef(Buf, Len);
333 return llvm::make_unique<ScalarHNode>(N, KeyStr);
334 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
335 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
336 for (Node &SN : *SQ) {
337 auto Entry = this->createHNodes(&SN);
340 SQHNode->Entries.push_back(std::move(Entry));
342 return std::move(SQHNode);
343 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
344 auto mapHNode = llvm::make_unique<MapHNode>(N);
345 for (KeyValueNode &KVN : *Map) {
346 Node *KeyNode = KVN.getKey();
347 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
349 setError(KeyNode, "Map key must be a scalar");
352 StringStorage.clear();
353 StringRef KeyStr = KeyScalar->getValue(StringStorage);
354 if (!StringStorage.empty()) {
355 // Copy string to permanent storage
356 unsigned Len = StringStorage.size();
357 char *Buf = StringAllocator.Allocate<char>(Len);
358 memcpy(Buf, &StringStorage[0], Len);
359 KeyStr = StringRef(Buf, Len);
361 auto ValueHNode = this->createHNodes(KVN.getValue());
364 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
366 return std::move(mapHNode);
367 } else if (isa<NullNode>(N)) {
368 return llvm::make_unique<EmptyHNode>(N);
370 setError(N, "unknown node kind");
375 bool Input::MapHNode::isValidKey(StringRef Key) {
376 for (const char *K : ValidKeys) {
383 void Input::setError(const Twine &Message) {
384 this->setError(CurrentNode, Message);
387 bool Input::canElideEmptySequence() {
391 //===----------------------------------------------------------------------===//
393 //===----------------------------------------------------------------------===//
395 Output::Output(raw_ostream &yout, void *context)
399 ColumnAtFlowStart(0),
400 ColumnAtMapFlowStart(0),
401 NeedBitValueComma(false),
402 NeedFlowSequenceComma(false),
403 EnumerationMatchFound(false),
404 NeedsNewLine(false) {
410 bool Output::outputting() {
414 void Output::beginMapping() {
415 StateStack.push_back(inMapFirstKey);
419 bool Output::mapTag(StringRef Tag, bool Use) {
427 void Output::endMapping() {
428 StateStack.pop_back();
431 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
432 bool &UseDefault, void *&) {
434 if (Required || !SameAsDefault) {
435 auto State = StateStack.back();
436 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
439 this->newLineCheck();
440 this->paddedKey(Key);
447 void Output::postflightKey(void *) {
448 if (StateStack.back() == inMapFirstKey) {
449 StateStack.pop_back();
450 StateStack.push_back(inMapOtherKey);
451 } else if (StateStack.back() == inFlowMapFirstKey) {
452 StateStack.pop_back();
453 StateStack.push_back(inFlowMapOtherKey);
457 void Output::beginFlowMapping() {
458 StateStack.push_back(inFlowMapFirstKey);
459 this->newLineCheck();
460 ColumnAtMapFlowStart = Column;
464 void Output::endFlowMapping() {
465 StateStack.pop_back();
466 this->outputUpToEndOfLine(" }");
469 void Output::beginDocuments() {
470 this->outputUpToEndOfLine("---");
473 bool Output::preflightDocument(unsigned index) {
475 this->outputUpToEndOfLine("\n---");
479 void Output::postflightDocument() {
482 void Output::endDocuments() {
486 unsigned Output::beginSequence() {
487 StateStack.push_back(inSeq);
492 void Output::endSequence() {
493 StateStack.pop_back();
496 bool Output::preflightElement(unsigned, void *&) {
500 void Output::postflightElement(void *) {
503 unsigned Output::beginFlowSequence() {
504 StateStack.push_back(inFlowSeq);
505 this->newLineCheck();
506 ColumnAtFlowStart = Column;
508 NeedFlowSequenceComma = false;
512 void Output::endFlowSequence() {
513 StateStack.pop_back();
514 this->outputUpToEndOfLine(" ]");
517 bool Output::preflightFlowElement(unsigned, void *&) {
518 if (NeedFlowSequenceComma)
522 for (int i = 0; i < ColumnAtFlowStart; ++i)
524 Column = ColumnAtFlowStart;
530 void Output::postflightFlowElement(void *) {
531 NeedFlowSequenceComma = true;
534 void Output::beginEnumScalar() {
535 EnumerationMatchFound = false;
538 bool Output::matchEnumScalar(const char *Str, bool Match) {
539 if (Match && !EnumerationMatchFound) {
540 this->newLineCheck();
541 this->outputUpToEndOfLine(Str);
542 EnumerationMatchFound = true;
547 bool Output::matchEnumFallback() {
548 if (EnumerationMatchFound)
550 EnumerationMatchFound = true;
554 void Output::endEnumScalar() {
555 if (!EnumerationMatchFound)
556 llvm_unreachable("bad runtime enum value");
559 bool Output::beginBitSetScalar(bool &DoClear) {
560 this->newLineCheck();
562 NeedBitValueComma = false;
567 bool Output::bitSetMatch(const char *Str, bool Matches) {
569 if (NeedBitValueComma)
572 NeedBitValueComma = true;
577 void Output::endBitSetScalar() {
578 this->outputUpToEndOfLine(" ]");
581 void Output::scalarString(StringRef &S, bool MustQuote) {
582 this->newLineCheck();
584 // Print '' for the empty string because leaving the field empty is not
586 this->outputUpToEndOfLine("''");
590 // Only quote if we must.
591 this->outputUpToEndOfLine(S);
596 unsigned End = S.size();
597 output("'"); // Starting single quote.
598 const char *Base = S.data();
600 // Escape a single quote by doubling it.
602 output(StringRef(&Base[i], j - i + 1));
608 output(StringRef(&Base[i], j - i));
609 this->outputUpToEndOfLine("'"); // Ending single quote.
612 void Output::setError(const Twine &message) {
615 bool Output::canElideEmptySequence() {
616 // Normally, with an optional key/value where the value is an empty sequence,
617 // the whole key/value can be not written. But, that produces wrong yaml
618 // if the key/value is the only thing in the map and the map is used in
619 // a sequence. This detects if the this sequence is the first key/value
620 // in map that itself is embedded in a sequnce.
621 if (StateStack.size() < 2)
623 if (StateStack.back() != inMapFirstKey)
625 return (StateStack[StateStack.size()-2] != inSeq);
628 void Output::output(StringRef s) {
633 void Output::outputUpToEndOfLine(StringRef s) {
635 if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
636 StateStack.back() != inFlowMapFirstKey &&
637 StateStack.back() != inFlowMapOtherKey))
641 void Output::outputNewLine() {
646 // if seq at top, indent as if map, then add "- "
647 // if seq in middle, use "- " if firstKey, else use " "
650 void Output::newLineCheck() {
653 NeedsNewLine = false;
655 this->outputNewLine();
657 assert(StateStack.size() > 0);
658 unsigned Indent = StateStack.size() - 1;
659 bool OutputDash = false;
661 if (StateStack.back() == inSeq) {
663 } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
664 (StateStack.back() == inFlowSeq) ||
665 (StateStack.back() == inFlowMapFirstKey)) &&
666 (StateStack[StateStack.size() - 2] == inSeq)) {
671 for (unsigned i = 0; i < Indent; ++i) {
680 void Output::paddedKey(StringRef key) {
683 const char *spaces = " ";
684 if (key.size() < strlen(spaces))
685 output(&spaces[key.size()]);
690 void Output::flowKey(StringRef Key) {
691 if (StateStack.back() == inFlowMapOtherKey)
695 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
697 Column = ColumnAtMapFlowStart;
704 //===----------------------------------------------------------------------===//
705 // traits for built-in types
706 //===----------------------------------------------------------------------===//
708 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
709 Out << (Val ? "true" : "false");
712 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
713 if (Scalar.equals("true")) {
716 } else if (Scalar.equals("false")) {
720 return "invalid boolean";
723 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
728 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
734 void ScalarTraits<std::string>::output(const std::string &Val, void *,
739 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
745 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
747 // use temp uin32_t because ostream thinks uint8_t is a character
752 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
753 unsigned long long n;
754 if (getAsUnsignedInteger(Scalar, 0, n))
755 return "invalid number";
757 return "out of range number";
762 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
767 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
769 unsigned long long n;
770 if (getAsUnsignedInteger(Scalar, 0, n))
771 return "invalid number";
773 return "out of range number";
778 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
783 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
785 unsigned long long n;
786 if (getAsUnsignedInteger(Scalar, 0, n))
787 return "invalid number";
788 if (n > 0xFFFFFFFFUL)
789 return "out of range number";
794 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
799 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
801 unsigned long long N;
802 if (getAsUnsignedInteger(Scalar, 0, N))
803 return "invalid number";
808 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
809 // use temp in32_t because ostream thinks int8_t is a character
814 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
816 if (getAsSignedInteger(Scalar, 0, N))
817 return "invalid number";
818 if ((N > 127) || (N < -128))
819 return "out of range number";
824 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
829 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
831 if (getAsSignedInteger(Scalar, 0, N))
832 return "invalid number";
833 if ((N > INT16_MAX) || (N < INT16_MIN))
834 return "out of range number";
839 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
844 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
846 if (getAsSignedInteger(Scalar, 0, N))
847 return "invalid number";
848 if ((N > INT32_MAX) || (N < INT32_MIN))
849 return "out of range number";
854 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
859 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
861 if (getAsSignedInteger(Scalar, 0, N))
862 return "invalid number";
867 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
868 Out << format("%g", Val);
871 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
872 SmallString<32> buff(Scalar.begin(), Scalar.end());
874 Val = strtod(buff.c_str(), &end);
876 return "invalid floating point number";
880 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
881 Out << format("%g", Val);
884 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
885 SmallString<32> buff(Scalar.begin(), Scalar.end());
887 Val = strtod(buff.c_str(), &end);
889 return "invalid floating point number";
893 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
895 Out << format("0x%02X", Num);
898 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
899 unsigned long long n;
900 if (getAsUnsignedInteger(Scalar, 0, n))
901 return "invalid hex8 number";
903 return "out of range hex8 number";
908 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
910 Out << format("0x%04X", Num);
913 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
914 unsigned long long n;
915 if (getAsUnsignedInteger(Scalar, 0, n))
916 return "invalid hex16 number";
918 return "out of range hex16 number";
923 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
925 Out << format("0x%08X", Num);
928 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
929 unsigned long long n;
930 if (getAsUnsignedInteger(Scalar, 0, n))
931 return "invalid hex32 number";
932 if (n > 0xFFFFFFFFUL)
933 return "out of range hex32 number";
938 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
940 Out << format("0x%016llX", Num);
943 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
944 unsigned long long Num;
945 if (getAsUnsignedInteger(Scalar, 0, Num))
946 return "invalid hex64 number";