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/ADT/Twine.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/YAMLParser.h"
16 #include "llvm/Support/YAMLTraits.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() {}
66 void Input::MapHNode::anchor() {}
67 void Input::SequenceHNode::anchor() {}
69 bool Input::outputting() {
73 bool Input::setCurrentDocument() {
74 if (DocIterator != Strm->end()) {
75 Node *N = DocIterator->getRoot();
77 assert(Strm->failed() && "Root is NULL iff parsing failed");
78 EC = make_error_code(errc::invalid_argument);
82 if (isa<NullNode>(N)) {
83 // Empty files are allowed and ignored
85 return setCurrentDocument();
87 TopNode = this->createHNodes(N);
88 CurrentNode = TopNode.get();
94 bool Input::nextDocument() {
95 return ++DocIterator != Strm->end();
98 bool Input::mapTag(StringRef Tag, bool Default) {
99 std::string foundTag = CurrentNode->_node->getVerbatimTag();
100 if (foundTag.empty()) {
101 // If no tag found and 'Tag' is the default, say it was found.
104 // Return true iff found tag matches supplied tag.
105 return Tag.equals(foundTag);
108 void Input::beginMapping() {
111 // CurrentNode can be null if the document is empty.
112 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
114 MN->ValidKeys.clear();
118 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
124 // CurrentNode is null for empty documents, which is an error in case required
125 // nodes are present.
128 EC = make_error_code(errc::invalid_argument);
132 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
134 setError(CurrentNode, "not a mapping");
137 MN->ValidKeys.push_back(Key);
138 HNode *Value = MN->Mapping[Key].get();
141 setError(CurrentNode, Twine("missing required key '") + Key + "'");
146 SaveInfo = CurrentNode;
151 void Input::postflightKey(void *saveInfo) {
152 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
155 void Input::endMapping() {
158 // CurrentNode can be null if the document is empty.
159 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
162 for (const auto &NN : MN->Mapping) {
163 if (!MN->isValidKey(NN.first())) {
164 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
170 unsigned Input::beginSequence() {
171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
172 return SQ->Entries.size();
173 if (isa<EmptyHNode>(CurrentNode))
175 // Treat case where there's a scalar "null" value as an empty sequence.
176 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
177 if (isNull(SN->value()))
180 // Any other type of HNode is an error.
181 setError(CurrentNode, "not a sequence");
185 void Input::endSequence() {
188 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
191 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
192 SaveInfo = CurrentNode;
193 CurrentNode = SQ->Entries[Index].get();
199 void Input::postflightElement(void *SaveInfo) {
200 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
203 unsigned Input::beginFlowSequence() { return beginSequence(); }
205 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
208 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
209 SaveInfo = CurrentNode;
210 CurrentNode = SQ->Entries[index].get();
216 void Input::postflightFlowElement(void *SaveInfo) {
217 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
220 void Input::endFlowSequence() {
223 void Input::beginEnumScalar() {
224 ScalarMatchFound = false;
227 bool Input::matchEnumScalar(const char *Str, bool) {
228 if (ScalarMatchFound)
230 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
231 if (SN->value().equals(Str)) {
232 ScalarMatchFound = true;
239 bool Input::matchEnumFallback() {
240 if (ScalarMatchFound)
242 ScalarMatchFound = true;
246 void Input::endEnumScalar() {
247 if (!ScalarMatchFound) {
248 setError(CurrentNode, "unknown enumerated scalar");
252 bool Input::beginBitSetScalar(bool &DoClear) {
253 BitValuesUsed.clear();
254 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
255 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
257 setError(CurrentNode, "expected sequence of bit values");
263 bool Input::bitSetMatch(const char *Str, bool) {
266 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
268 for (auto &N : SQ->Entries) {
269 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
270 if (SN->value().equals(Str)) {
271 BitValuesUsed[Index] = true;
275 setError(CurrentNode, "unexpected scalar in sequence of bit values");
280 setError(CurrentNode, "expected sequence of bit values");
285 void Input::endBitSetScalar() {
288 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
289 assert(BitValuesUsed.size() == SQ->Entries.size());
290 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
291 if (!BitValuesUsed[i]) {
292 setError(SQ->Entries[i].get(), "unknown bit value");
299 void Input::scalarString(StringRef &S, bool) {
300 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
303 setError(CurrentNode, "unexpected scalar");
307 void Input::setError(HNode *hnode, const Twine &message) {
308 assert(hnode && "HNode must not be NULL");
309 this->setError(hnode->_node, message);
312 void Input::setError(Node *node, const Twine &message) {
313 Strm->printError(node, message);
314 EC = make_error_code(errc::invalid_argument);
317 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
318 SmallString<128> StringStorage;
319 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
320 StringRef KeyStr = SN->getValue(StringStorage);
321 if (!StringStorage.empty()) {
322 // Copy string to permanent storage
323 unsigned Len = StringStorage.size();
324 char *Buf = StringAllocator.Allocate<char>(Len);
325 memcpy(Buf, &StringStorage[0], Len);
326 KeyStr = StringRef(Buf, Len);
328 return llvm::make_unique<ScalarHNode>(N, KeyStr);
329 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
330 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
331 for (Node &SN : *SQ) {
332 auto Entry = this->createHNodes(&SN);
335 SQHNode->Entries.push_back(std::move(Entry));
337 return std::move(SQHNode);
338 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
339 auto mapHNode = llvm::make_unique<MapHNode>(N);
340 for (KeyValueNode &KVN : *Map) {
341 Node *KeyNode = KVN.getKey();
342 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
344 setError(KeyNode, "Map key must be a scalar");
347 StringStorage.clear();
348 StringRef KeyStr = KeyScalar->getValue(StringStorage);
349 if (!StringStorage.empty()) {
350 // Copy string to permanent storage
351 unsigned Len = StringStorage.size();
352 char *Buf = StringAllocator.Allocate<char>(Len);
353 memcpy(Buf, &StringStorage[0], Len);
354 KeyStr = StringRef(Buf, Len);
356 auto ValueHNode = this->createHNodes(KVN.getValue());
359 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
361 return std::move(mapHNode);
362 } else if (isa<NullNode>(N)) {
363 return llvm::make_unique<EmptyHNode>(N);
365 setError(N, "unknown node kind");
370 bool Input::MapHNode::isValidKey(StringRef Key) {
371 for (const char *K : ValidKeys) {
378 void Input::setError(const Twine &Message) {
379 this->setError(CurrentNode, Message);
382 bool Input::canElideEmptySequence() {
386 //===----------------------------------------------------------------------===//
388 //===----------------------------------------------------------------------===//
390 Output::Output(raw_ostream &yout, void *context)
394 ColumnAtFlowStart(0),
395 NeedBitValueComma(false),
396 NeedFlowSequenceComma(false),
397 EnumerationMatchFound(false),
398 NeedsNewLine(false) {
404 bool Output::outputting() {
408 void Output::beginMapping() {
409 StateStack.push_back(inMapFirstKey);
413 bool Output::mapTag(StringRef Tag, bool Use) {
421 void Output::endMapping() {
422 StateStack.pop_back();
425 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
426 bool &UseDefault, void *&) {
428 if (Required || !SameAsDefault) {
429 this->newLineCheck();
430 this->paddedKey(Key);
436 void Output::postflightKey(void *) {
437 if (StateStack.back() == inMapFirstKey) {
438 StateStack.pop_back();
439 StateStack.push_back(inMapOtherKey);
443 void Output::beginDocuments() {
444 this->outputUpToEndOfLine("---");
447 bool Output::preflightDocument(unsigned index) {
449 this->outputUpToEndOfLine("\n---");
453 void Output::postflightDocument() {
456 void Output::endDocuments() {
460 unsigned Output::beginSequence() {
461 StateStack.push_back(inSeq);
466 void Output::endSequence() {
467 StateStack.pop_back();
470 bool Output::preflightElement(unsigned, void *&) {
474 void Output::postflightElement(void *) {
477 unsigned Output::beginFlowSequence() {
478 StateStack.push_back(inFlowSeq);
479 this->newLineCheck();
480 ColumnAtFlowStart = Column;
482 NeedFlowSequenceComma = false;
486 void Output::endFlowSequence() {
487 StateStack.pop_back();
488 this->outputUpToEndOfLine(" ]");
491 bool Output::preflightFlowElement(unsigned, void *&) {
492 if (NeedFlowSequenceComma)
496 for (int i = 0; i < ColumnAtFlowStart; ++i)
498 Column = ColumnAtFlowStart;
504 void Output::postflightFlowElement(void *) {
505 NeedFlowSequenceComma = true;
508 void Output::beginEnumScalar() {
509 EnumerationMatchFound = false;
512 bool Output::matchEnumScalar(const char *Str, bool Match) {
513 if (Match && !EnumerationMatchFound) {
514 this->newLineCheck();
515 this->outputUpToEndOfLine(Str);
516 EnumerationMatchFound = true;
521 bool Output::matchEnumFallback() {
522 if (EnumerationMatchFound)
524 EnumerationMatchFound = true;
528 void Output::endEnumScalar() {
529 if (!EnumerationMatchFound)
530 llvm_unreachable("bad runtime enum value");
533 bool Output::beginBitSetScalar(bool &DoClear) {
534 this->newLineCheck();
536 NeedBitValueComma = false;
541 bool Output::bitSetMatch(const char *Str, bool Matches) {
543 if (NeedBitValueComma)
546 NeedBitValueComma = true;
551 void Output::endBitSetScalar() {
552 this->outputUpToEndOfLine(" ]");
555 void Output::scalarString(StringRef &S, bool MustQuote) {
556 this->newLineCheck();
558 // Print '' for the empty string because leaving the field empty is not
560 this->outputUpToEndOfLine("''");
564 // Only quote if we must.
565 this->outputUpToEndOfLine(S);
570 unsigned End = S.size();
571 output("'"); // Starting single quote.
572 const char *Base = S.data();
574 // Escape a single quote by doubling it.
576 output(StringRef(&Base[i], j - i + 1));
582 output(StringRef(&Base[i], j - i));
583 this->outputUpToEndOfLine("'"); // Ending single quote.
586 void Output::setError(const Twine &message) {
589 bool Output::canElideEmptySequence() {
590 // Normally, with an optional key/value where the value is an empty sequence,
591 // the whole key/value can be not written. But, that produces wrong yaml
592 // if the key/value is the only thing in the map and the map is used in
593 // a sequence. This detects if the this sequence is the first key/value
594 // in map that itself is embedded in a sequnce.
595 if (StateStack.size() < 2)
597 if (StateStack.back() != inMapFirstKey)
599 return (StateStack[StateStack.size()-2] != inSeq);
602 void Output::output(StringRef s) {
607 void Output::outputUpToEndOfLine(StringRef s) {
609 if (StateStack.empty() || StateStack.back() != inFlowSeq)
613 void Output::outputNewLine() {
618 // if seq at top, indent as if map, then add "- "
619 // if seq in middle, use "- " if firstKey, else use " "
622 void Output::newLineCheck() {
625 NeedsNewLine = false;
627 this->outputNewLine();
629 assert(StateStack.size() > 0);
630 unsigned Indent = StateStack.size() - 1;
631 bool OutputDash = false;
633 if (StateStack.back() == inSeq) {
635 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
636 (StateStack[StateStack.size() - 2] == inSeq)) {
641 for (unsigned i = 0; i < Indent; ++i) {
650 void Output::paddedKey(StringRef key) {
653 const char *spaces = " ";
654 if (key.size() < strlen(spaces))
655 output(&spaces[key.size()]);
660 //===----------------------------------------------------------------------===//
661 // traits for built-in types
662 //===----------------------------------------------------------------------===//
664 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
665 Out << (Val ? "true" : "false");
668 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
669 if (Scalar.equals("true")) {
672 } else if (Scalar.equals("false")) {
676 return "invalid boolean";
679 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
684 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
690 void ScalarTraits<std::string>::output(const std::string &Val, void *,
695 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
701 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
703 // use temp uin32_t because ostream thinks uint8_t is a character
708 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
709 unsigned long long n;
710 if (getAsUnsignedInteger(Scalar, 0, n))
711 return "invalid number";
713 return "out of range number";
718 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
723 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
725 unsigned long long n;
726 if (getAsUnsignedInteger(Scalar, 0, n))
727 return "invalid number";
729 return "out of range number";
734 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
739 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
741 unsigned long long n;
742 if (getAsUnsignedInteger(Scalar, 0, n))
743 return "invalid number";
744 if (n > 0xFFFFFFFFUL)
745 return "out of range number";
750 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
755 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
757 unsigned long long N;
758 if (getAsUnsignedInteger(Scalar, 0, N))
759 return "invalid number";
764 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
765 // use temp in32_t because ostream thinks int8_t is a character
770 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
772 if (getAsSignedInteger(Scalar, 0, N))
773 return "invalid number";
774 if ((N > 127) || (N < -128))
775 return "out of range number";
780 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
785 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
787 if (getAsSignedInteger(Scalar, 0, N))
788 return "invalid number";
789 if ((N > INT16_MAX) || (N < INT16_MIN))
790 return "out of range number";
795 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
800 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
802 if (getAsSignedInteger(Scalar, 0, N))
803 return "invalid number";
804 if ((N > INT32_MAX) || (N < INT32_MIN))
805 return "out of range number";
810 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
815 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
817 if (getAsSignedInteger(Scalar, 0, N))
818 return "invalid number";
823 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
824 Out << format("%g", Val);
827 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
828 SmallString<32> buff(Scalar.begin(), Scalar.end());
830 Val = strtod(buff.c_str(), &end);
832 return "invalid floating point number";
836 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
837 Out << format("%g", Val);
840 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
841 SmallString<32> buff(Scalar.begin(), Scalar.end());
843 Val = strtod(buff.c_str(), &end);
845 return "invalid floating point number";
849 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
851 Out << format("0x%02X", Num);
854 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
855 unsigned long long n;
856 if (getAsUnsignedInteger(Scalar, 0, n))
857 return "invalid hex8 number";
859 return "out of range hex8 number";
864 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
866 Out << format("0x%04X", Num);
869 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
870 unsigned long long n;
871 if (getAsUnsignedInteger(Scalar, 0, n))
872 return "invalid hex16 number";
874 return "out of range hex16 number";
879 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
881 Out << format("0x%08X", Num);
884 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
885 unsigned long long n;
886 if (getAsUnsignedInteger(Scalar, 0, n))
887 return "invalid hex32 number";
888 if (n > 0xFFFFFFFFUL)
889 return "out of range hex32 number";
894 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
896 Out << format("0x%016llX", Num);
899 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
900 unsigned long long Num;
901 if (getAsUnsignedInteger(Scalar, 0, Num))
902 return "invalid hex64 number";