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/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/raw_ostream.h"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 IO::IO(void *Context) : Ctxt(Context) {
32 void *IO::getContext() {
36 void IO::setContext(void *Context) {
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
44 Input::Input(StringRef InputContent,
46 SourceMgr::DiagHandlerTy DiagHandler,
47 void *DiagHandlerCtxt)
49 Strm(new Stream(InputContent, SrcMgr)),
52 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
53 DocIterator = Strm->begin();
59 error_code Input::error() {
63 // Pin the vtables to this file.
64 void Input::HNode::anchor() {}
65 void Input::EmptyHNode::anchor() {}
66 void Input::ScalarHNode::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.reset(this->createHNodes(N));
87 CurrentNode = TopNode.get();
93 void Input::nextDocument() {
97 bool Input::mapTag(StringRef Tag, bool Default) {
98 std::string foundTag = CurrentNode->_node->getVerbatimTag();
99 if (foundTag.empty()) {
100 // If no tag found and 'Tag' is the default, say it was found.
103 // Return true iff found tag matches supplied tag.
104 return Tag.equals(foundTag);
107 void Input::beginMapping() {
110 // CurrentNode can be null if the document is empty.
111 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
113 MN->ValidKeys.clear();
117 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
123 // CurrentNode is null for empty documents, which is an error in case required
124 // nodes are present.
127 EC = make_error_code(errc::invalid_argument);
131 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
133 setError(CurrentNode, "not a mapping");
136 MN->ValidKeys.push_back(Key);
137 HNode *Value = MN->Mapping[Key];
140 setError(CurrentNode, Twine("missing required key '") + Key + "'");
145 SaveInfo = CurrentNode;
150 void Input::postflightKey(void *saveInfo) {
151 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
154 void Input::endMapping() {
157 // CurrentNode can be null if the document is empty.
158 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
161 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
162 End = MN->Mapping.end(); i != End; ++i) {
163 if (!MN->isValidKey(i->first())) {
164 setError(i->second, Twine("unknown key '") + i->first() + "'");
170 unsigned Input::beginSequence() {
171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
172 return SQ->Entries.size();
177 void Input::endSequence() {
180 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
183 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
184 SaveInfo = CurrentNode;
185 CurrentNode = SQ->Entries[Index];
191 void Input::postflightElement(void *SaveInfo) {
192 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
195 unsigned Input::beginFlowSequence() {
196 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
197 return SQ->Entries.size();
202 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
205 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
206 SaveInfo = CurrentNode;
207 CurrentNode = SQ->Entries[index];
213 void Input::postflightFlowElement(void *SaveInfo) {
214 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
217 void Input::endFlowSequence() {
220 void Input::beginEnumScalar() {
221 ScalarMatchFound = false;
224 bool Input::matchEnumScalar(const char *Str, bool) {
225 if (ScalarMatchFound)
227 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
228 if (SN->value().equals(Str)) {
229 ScalarMatchFound = true;
236 void Input::endEnumScalar() {
237 if (!ScalarMatchFound) {
238 setError(CurrentNode, "unknown enumerated scalar");
242 bool Input::beginBitSetScalar(bool &DoClear) {
243 BitValuesUsed.clear();
244 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
245 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
247 setError(CurrentNode, "expected sequence of bit values");
253 bool Input::bitSetMatch(const char *Str, bool) {
256 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
258 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
259 End = SQ->Entries.end(); i != End; ++i) {
260 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
261 if (SN->value().equals(Str)) {
262 BitValuesUsed[Index] = true;
266 setError(CurrentNode, "unexpected scalar in sequence of bit values");
271 setError(CurrentNode, "expected sequence of bit values");
276 void Input::endBitSetScalar() {
279 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
280 assert(BitValuesUsed.size() == SQ->Entries.size());
281 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
282 if (!BitValuesUsed[i]) {
283 setError(SQ->Entries[i], "unknown bit value");
290 void Input::scalarString(StringRef &S) {
291 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
294 setError(CurrentNode, "unexpected scalar");
298 void Input::setError(HNode *hnode, const Twine &message) {
299 assert(hnode && "HNode must not be NULL");
300 this->setError(hnode->_node, message);
303 void Input::setError(Node *node, const Twine &message) {
304 Strm->printError(node, message);
305 EC = make_error_code(errc::invalid_argument);
308 Input::HNode *Input::createHNodes(Node *N) {
309 SmallString<128> StringStorage;
310 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
311 StringRef KeyStr = SN->getValue(StringStorage);
312 if (!StringStorage.empty()) {
313 // Copy string to permanent storage
314 unsigned Len = StringStorage.size();
315 char *Buf = StringAllocator.Allocate<char>(Len);
316 memcpy(Buf, &StringStorage[0], Len);
317 KeyStr = StringRef(Buf, Len);
319 return new ScalarHNode(N, KeyStr);
320 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
321 SequenceHNode *SQHNode = new SequenceHNode(N);
322 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
324 HNode *Entry = this->createHNodes(i);
327 SQHNode->Entries.push_back(Entry);
330 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
331 MapHNode *mapHNode = new MapHNode(N);
332 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
334 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
335 StringStorage.clear();
336 StringRef KeyStr = KeyScalar->getValue(StringStorage);
337 if (!StringStorage.empty()) {
338 // Copy string to permanent storage
339 unsigned Len = StringStorage.size();
340 char *Buf = StringAllocator.Allocate<char>(Len);
341 memcpy(Buf, &StringStorage[0], Len);
342 KeyStr = StringRef(Buf, Len);
344 HNode *ValueHNode = this->createHNodes(i->getValue());
347 mapHNode->Mapping[KeyStr] = ValueHNode;
350 } else if (isa<NullNode>(N)) {
351 return new EmptyHNode(N);
353 setError(N, "unknown node kind");
358 bool Input::MapHNode::isValidKey(StringRef Key) {
359 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
360 End = ValidKeys.end(); i != End; ++i) {
367 void Input::setError(const Twine &Message) {
368 this->setError(CurrentNode, Message);
371 bool Input::canElideEmptySequence() {
375 Input::MapHNode::~MapHNode() {
376 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
382 Input::SequenceHNode::~SequenceHNode() {
383 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
391 //===----------------------------------------------------------------------===//
393 //===----------------------------------------------------------------------===//
395 Output::Output(raw_ostream &yout, void *context)
399 ColumnAtFlowStart(0),
400 NeedBitValueComma(false),
401 NeedFlowSequenceComma(false),
402 EnumerationMatchFound(false),
403 NeedsNewLine(false) {
409 bool Output::outputting() {
413 void Output::beginMapping() {
414 StateStack.push_back(inMapFirstKey);
418 bool Output::mapTag(StringRef Tag, bool Use) {
426 void Output::endMapping() {
427 StateStack.pop_back();
430 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
431 bool &UseDefault, void *&) {
433 if (Required || !SameAsDefault) {
434 this->newLineCheck();
435 this->paddedKey(Key);
441 void Output::postflightKey(void *) {
442 if (StateStack.back() == inMapFirstKey) {
443 StateStack.pop_back();
444 StateStack.push_back(inMapOtherKey);
448 void Output::beginDocuments() {
449 this->outputUpToEndOfLine("---");
452 bool Output::preflightDocument(unsigned index) {
454 this->outputUpToEndOfLine("\n---");
458 void Output::postflightDocument() {
461 void Output::endDocuments() {
465 unsigned Output::beginSequence() {
466 StateStack.push_back(inSeq);
471 void Output::endSequence() {
472 StateStack.pop_back();
475 bool Output::preflightElement(unsigned, void *&) {
479 void Output::postflightElement(void *) {
482 unsigned Output::beginFlowSequence() {
483 StateStack.push_back(inFlowSeq);
484 this->newLineCheck();
485 ColumnAtFlowStart = Column;
487 NeedFlowSequenceComma = false;
491 void Output::endFlowSequence() {
492 StateStack.pop_back();
493 this->outputUpToEndOfLine(" ]");
496 bool Output::preflightFlowElement(unsigned, void *&) {
497 if (NeedFlowSequenceComma)
501 for (int i = 0; i < ColumnAtFlowStart; ++i)
503 Column = ColumnAtFlowStart;
509 void Output::postflightFlowElement(void *) {
510 NeedFlowSequenceComma = true;
513 void Output::beginEnumScalar() {
514 EnumerationMatchFound = false;
517 bool Output::matchEnumScalar(const char *Str, bool Match) {
518 if (Match && !EnumerationMatchFound) {
519 this->newLineCheck();
520 this->outputUpToEndOfLine(Str);
521 EnumerationMatchFound = true;
526 void Output::endEnumScalar() {
527 if (!EnumerationMatchFound)
528 llvm_unreachable("bad runtime enum value");
531 bool Output::beginBitSetScalar(bool &DoClear) {
532 this->newLineCheck();
534 NeedBitValueComma = false;
539 bool Output::bitSetMatch(const char *Str, bool Matches) {
541 if (NeedBitValueComma)
544 NeedBitValueComma = true;
549 void Output::endBitSetScalar() {
550 this->outputUpToEndOfLine(" ]");
553 void Output::scalarString(StringRef &S) {
554 const char ScalarSafeChars[] = "abcdefghijklmnopqrstuvwxyz"
555 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
557 this->newLineCheck();
559 // Print '' for the empty string because leaving the field empty is not
561 this->outputUpToEndOfLine("''");
564 if (S.find_first_not_of(ScalarSafeChars) == StringRef::npos &&
565 !isspace(S.front()) && !isspace(S.back())) {
566 // If the string consists only of safe characters, print it out without
568 this->outputUpToEndOfLine(S);
573 unsigned End = S.size();
574 output("'"); // Starting single quote.
575 const char *Base = S.data();
577 // Escape a single quote by doubling it.
579 output(StringRef(&Base[i], j - i + 1));
585 output(StringRef(&Base[i], j - i));
586 this->outputUpToEndOfLine("'"); // Ending single quote.
589 void Output::setError(const Twine &message) {
592 bool Output::canElideEmptySequence() {
593 // Normally, with an optional key/value where the value is an empty sequence,
594 // the whole key/value can be not written. But, that produces wrong yaml
595 // if the key/value is the only thing in the map and the map is used in
596 // a sequence. This detects if the this sequence is the first key/value
597 // in map that itself is embedded in a sequnce.
598 if (StateStack.size() < 2)
600 if (StateStack.back() != inMapFirstKey)
602 return (StateStack[StateStack.size()-2] != inSeq);
605 void Output::output(StringRef s) {
610 void Output::outputUpToEndOfLine(StringRef s) {
612 if (StateStack.empty() || StateStack.back() != inFlowSeq)
616 void Output::outputNewLine() {
621 // if seq at top, indent as if map, then add "- "
622 // if seq in middle, use "- " if firstKey, else use " "
625 void Output::newLineCheck() {
628 NeedsNewLine = false;
630 this->outputNewLine();
632 assert(StateStack.size() > 0);
633 unsigned Indent = StateStack.size() - 1;
634 bool OutputDash = false;
636 if (StateStack.back() == inSeq) {
638 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
639 (StateStack[StateStack.size() - 2] == inSeq)) {
644 for (unsigned i = 0; i < Indent; ++i) {
653 void Output::paddedKey(StringRef key) {
656 const char *spaces = " ";
657 if (key.size() < strlen(spaces))
658 output(&spaces[key.size()]);
663 //===----------------------------------------------------------------------===//
664 // traits for built-in types
665 //===----------------------------------------------------------------------===//
667 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
668 Out << (Val ? "true" : "false");
671 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
672 if (Scalar.equals("true")) {
675 } else if (Scalar.equals("false")) {
679 return "invalid boolean";
682 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
687 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
693 void ScalarTraits<std::string>::output(const std::string &Val, void *,
698 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
704 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
706 // use temp uin32_t because ostream thinks uint8_t is a character
711 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
712 unsigned long long n;
713 if (getAsUnsignedInteger(Scalar, 0, n))
714 return "invalid number";
716 return "out of range number";
721 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
726 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
728 unsigned long long n;
729 if (getAsUnsignedInteger(Scalar, 0, n))
730 return "invalid number";
732 return "out of range number";
737 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
742 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
744 unsigned long long n;
745 if (getAsUnsignedInteger(Scalar, 0, n))
746 return "invalid number";
747 if (n > 0xFFFFFFFFUL)
748 return "out of range number";
753 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
758 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
760 unsigned long long N;
761 if (getAsUnsignedInteger(Scalar, 0, N))
762 return "invalid number";
767 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
768 // use temp in32_t because ostream thinks int8_t is a character
773 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
775 if (getAsSignedInteger(Scalar, 0, N))
776 return "invalid number";
777 if ((N > 127) || (N < -128))
778 return "out of range number";
783 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
788 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
790 if (getAsSignedInteger(Scalar, 0, N))
791 return "invalid number";
792 if ((N > INT16_MAX) || (N < INT16_MIN))
793 return "out of range number";
798 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
803 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
805 if (getAsSignedInteger(Scalar, 0, N))
806 return "invalid number";
807 if ((N > INT32_MAX) || (N < INT32_MIN))
808 return "out of range number";
813 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
818 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
820 if (getAsSignedInteger(Scalar, 0, N))
821 return "invalid number";
826 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
827 Out << format("%g", Val);
830 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
831 SmallString<32> buff(Scalar.begin(), Scalar.end());
833 Val = strtod(buff.c_str(), &end);
835 return "invalid floating point number";
839 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
840 Out << format("%g", Val);
843 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
844 SmallString<32> buff(Scalar.begin(), Scalar.end());
846 Val = strtod(buff.c_str(), &end);
848 return "invalid floating point number";
852 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
854 Out << format("0x%02X", Num);
857 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
858 unsigned long long n;
859 if (getAsUnsignedInteger(Scalar, 0, n))
860 return "invalid hex8 number";
862 return "out of range hex8 number";
867 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
869 Out << format("0x%04X", Num);
872 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
873 unsigned long long n;
874 if (getAsUnsignedInteger(Scalar, 0, n))
875 return "invalid hex16 number";
877 return "out of range hex16 number";
882 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
884 Out << format("0x%08X", Num);
887 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
888 unsigned long long n;
889 if (getAsUnsignedInteger(Scalar, 0, n))
890 return "invalid hex32 number";
891 if (n > 0xFFFFFFFFUL)
892 return "out of range hex32 number";
897 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
899 Out << format("0x%016llX", Num);
902 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
903 unsigned long long Num;
904 if (getAsUnsignedInteger(Scalar, 0, Num))
905 return "invalid hex64 number";