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"
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 IO::IO(void *Context) : Ctxt(Context) {
31 void *IO::getContext() {
35 void IO::setContext(void *Context) {
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 Input::Input(StringRef InputContent, void *Ctxt)
45 Strm(new Stream(InputContent, SrcMgr)),
47 DocIterator = Strm->begin();
53 error_code Input::error() {
57 void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
58 SrcMgr.setDiagHandler(Handler, Ctxt);
61 bool Input::outputting() {
65 bool Input::setCurrentDocument() {
66 if (DocIterator != Strm->end()) {
67 Node *N = DocIterator->getRoot();
68 if (isa<NullNode>(N)) {
69 // Empty files are allowed and ignored
71 return setCurrentDocument();
73 TopNode.reset(this->createHNodes(N));
74 CurrentNode = TopNode.get();
80 void Input::nextDocument() {
84 void Input::beginMapping() {
87 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
89 MN->ValidKeys.clear();
93 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
98 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
100 setError(CurrentNode, "not a mapping");
103 MN->ValidKeys.push_back(Key);
104 HNode *Value = MN->Mapping[Key];
107 setError(CurrentNode, Twine("missing required key '") + Key + "'");
112 SaveInfo = CurrentNode;
117 void Input::postflightKey(void *saveInfo) {
118 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
121 void Input::endMapping() {
124 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
127 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
128 End = MN->Mapping.end(); i != End; ++i) {
129 if (!MN->isValidKey(i->first())) {
130 setError(i->second, Twine("unknown key '") + i->first() + "'");
136 unsigned Input::beginSequence() {
137 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
138 return SQ->Entries.size();
143 void Input::endSequence() {
146 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
149 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
150 SaveInfo = CurrentNode;
151 CurrentNode = SQ->Entries[Index];
157 void Input::postflightElement(void *SaveInfo) {
158 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
161 unsigned Input::beginFlowSequence() {
162 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
163 return SQ->Entries.size();
168 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
172 SaveInfo = CurrentNode;
173 CurrentNode = SQ->Entries[index];
179 void Input::postflightFlowElement(void *SaveInfo) {
180 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
183 void Input::endFlowSequence() {
186 void Input::beginEnumScalar() {
187 ScalarMatchFound = false;
190 bool Input::matchEnumScalar(const char *Str, bool) {
191 if (ScalarMatchFound)
193 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
194 if (SN->value().equals(Str)) {
195 ScalarMatchFound = true;
202 void Input::endEnumScalar() {
203 if (!ScalarMatchFound) {
204 setError(CurrentNode, "unknown enumerated scalar");
208 bool Input::beginBitSetScalar(bool &DoClear) {
209 BitValuesUsed.clear();
210 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
211 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
213 setError(CurrentNode, "expected sequence of bit values");
219 bool Input::bitSetMatch(const char *Str, bool) {
222 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
224 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
225 End = SQ->Entries.end(); i != End; ++i) {
226 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
227 if (SN->value().equals(Str)) {
228 BitValuesUsed[Index] = true;
232 setError(CurrentNode, "unexpected scalar in sequence of bit values");
237 setError(CurrentNode, "expected sequence of bit values");
242 void Input::endBitSetScalar() {
245 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
246 assert(BitValuesUsed.size() == SQ->Entries.size());
247 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
248 if (!BitValuesUsed[i]) {
249 setError(SQ->Entries[i], "unknown bit value");
256 void Input::scalarString(StringRef &S) {
257 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
260 setError(CurrentNode, "unexpected scalar");
264 void Input::setError(HNode *hnode, const Twine &message) {
265 this->setError(hnode->_node, message);
268 void Input::setError(Node *node, const Twine &message) {
269 Strm->printError(node, message);
270 EC = make_error_code(errc::invalid_argument);
273 Input::HNode *Input::createHNodes(Node *N) {
274 SmallString<128> StringStorage;
275 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
276 StringRef KeyStr = SN->getValue(StringStorage);
277 if (!StringStorage.empty()) {
278 // Copy string to permanent storage
279 unsigned Len = StringStorage.size();
280 char *Buf = StringAllocator.Allocate<char>(Len);
281 memcpy(Buf, &StringStorage[0], Len);
282 KeyStr = StringRef(Buf, Len);
284 return new ScalarHNode(N, KeyStr);
285 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
286 SequenceHNode *SQHNode = new SequenceHNode(N);
287 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
289 HNode *Entry = this->createHNodes(i);
292 SQHNode->Entries.push_back(Entry);
295 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
296 MapHNode *mapHNode = new MapHNode(N);
297 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
299 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
300 StringStorage.clear();
301 StringRef KeyStr = KeyScalar->getValue(StringStorage);
302 if (!StringStorage.empty()) {
303 // Copy string to permanent storage
304 unsigned Len = StringStorage.size();
305 char *Buf = StringAllocator.Allocate<char>(Len);
306 memcpy(Buf, &StringStorage[0], Len);
307 KeyStr = StringRef(Buf, Len);
309 HNode *ValueHNode = this->createHNodes(i->getValue());
312 mapHNode->Mapping[KeyStr] = ValueHNode;
315 } else if (isa<NullNode>(N)) {
316 return new EmptyHNode(N);
318 setError(N, "unknown node kind");
323 bool Input::MapHNode::isValidKey(StringRef Key) {
324 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
325 End = ValidKeys.end(); i != End; ++i) {
332 void Input::setError(const Twine &Message) {
333 this->setError(CurrentNode, Message);
336 bool Input::canElideEmptySequence() {
340 Input::MapHNode::~MapHNode() {
341 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
347 Input::SequenceHNode::~SequenceHNode() {
348 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
356 //===----------------------------------------------------------------------===//
358 //===----------------------------------------------------------------------===//
360 Output::Output(raw_ostream &yout, void *context)
364 ColumnAtFlowStart(0),
365 NeedBitValueComma(false),
366 NeedFlowSequenceComma(false),
367 EnumerationMatchFound(false),
368 NeedsNewLine(false) {
374 bool Output::outputting() {
378 void Output::beginMapping() {
379 StateStack.push_back(inMapFirstKey);
383 void Output::endMapping() {
384 StateStack.pop_back();
387 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
388 bool &UseDefault, void *&) {
390 if (Required || !SameAsDefault) {
391 this->newLineCheck();
392 this->paddedKey(Key);
398 void Output::postflightKey(void *) {
399 if (StateStack.back() == inMapFirstKey) {
400 StateStack.pop_back();
401 StateStack.push_back(inMapOtherKey);
405 void Output::beginDocuments() {
406 this->outputUpToEndOfLine("---");
409 bool Output::preflightDocument(unsigned index) {
411 this->outputUpToEndOfLine("\n---");
415 void Output::postflightDocument() {
418 void Output::endDocuments() {
422 unsigned Output::beginSequence() {
423 StateStack.push_back(inSeq);
428 void Output::endSequence() {
429 StateStack.pop_back();
432 bool Output::preflightElement(unsigned, void *&) {
436 void Output::postflightElement(void *) {
439 unsigned Output::beginFlowSequence() {
440 StateStack.push_back(inFlowSeq);
441 this->newLineCheck();
442 ColumnAtFlowStart = Column;
444 NeedFlowSequenceComma = false;
448 void Output::endFlowSequence() {
449 StateStack.pop_back();
450 this->outputUpToEndOfLine(" ]");
453 bool Output::preflightFlowElement(unsigned, void *&) {
454 if (NeedFlowSequenceComma)
458 for (int i = 0; i < ColumnAtFlowStart; ++i)
460 Column = ColumnAtFlowStart;
466 void Output::postflightFlowElement(void *) {
467 NeedFlowSequenceComma = true;
470 void Output::beginEnumScalar() {
471 EnumerationMatchFound = false;
474 bool Output::matchEnumScalar(const char *Str, bool Match) {
475 if (Match && !EnumerationMatchFound) {
476 this->newLineCheck();
477 this->outputUpToEndOfLine(Str);
478 EnumerationMatchFound = true;
483 void Output::endEnumScalar() {
484 if (!EnumerationMatchFound)
485 llvm_unreachable("bad runtime enum value");
488 bool Output::beginBitSetScalar(bool &DoClear) {
489 this->newLineCheck();
491 NeedBitValueComma = false;
496 bool Output::bitSetMatch(const char *Str, bool Matches) {
498 if (NeedBitValueComma)
501 NeedBitValueComma = true;
506 void Output::endBitSetScalar() {
507 this->outputUpToEndOfLine(" ]");
510 void Output::scalarString(StringRef &S) {
511 this->newLineCheck();
512 if (S.find('\n') == StringRef::npos) {
513 // No embedded new-line chars, just print string.
514 this->outputUpToEndOfLine(S);
519 unsigned End = S.size();
520 output("'"); // Starting single quote.
521 const char *Base = S.data();
523 // Escape a single quote by doubling it.
525 output(StringRef(&Base[i], j - i + 1));
531 output(StringRef(&Base[i], j - i));
532 this->outputUpToEndOfLine("'"); // Ending single quote.
535 void Output::setError(const Twine &message) {
538 bool Output::canElideEmptySequence() {
539 // Normally, with an optional key/value where the value is an empty sequence,
540 // the whole key/value can be not written. But, that produces wrong yaml
541 // if the key/value is the only thing in the map and the map is used in
542 // a sequence. This detects if the this sequence is the first key/value
543 // in map that itself is embedded in a sequnce.
544 if (StateStack.size() < 2)
546 if (StateStack.back() != inMapFirstKey)
548 return (StateStack[StateStack.size()-2] != inSeq);
551 void Output::output(StringRef s) {
556 void Output::outputUpToEndOfLine(StringRef s) {
558 if (StateStack.empty() || StateStack.back() != inFlowSeq)
562 void Output::outputNewLine() {
567 // if seq at top, indent as if map, then add "- "
568 // if seq in middle, use "- " if firstKey, else use " "
571 void Output::newLineCheck() {
574 NeedsNewLine = false;
576 this->outputNewLine();
578 assert(StateStack.size() > 0);
579 unsigned Indent = StateStack.size() - 1;
580 bool OutputDash = false;
582 if (StateStack.back() == inSeq) {
584 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
585 (StateStack[StateStack.size() - 2] == inSeq)) {
590 for (unsigned i = 0; i < Indent; ++i) {
599 void Output::paddedKey(StringRef key) {
602 const char *spaces = " ";
603 if (key.size() < strlen(spaces))
604 output(&spaces[key.size()]);
609 //===----------------------------------------------------------------------===//
610 // traits for built-in types
611 //===----------------------------------------------------------------------===//
613 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
614 Out << (Val ? "true" : "false");
617 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
618 if (Scalar.equals("true")) {
621 } else if (Scalar.equals("false")) {
625 return "invalid boolean";
628 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
633 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
639 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
641 // use temp uin32_t because ostream thinks uint8_t is a character
646 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
647 unsigned long long n;
648 if (getAsUnsignedInteger(Scalar, 0, n))
649 return "invalid number";
651 return "out of range number";
656 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
661 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
663 unsigned long long n;
664 if (getAsUnsignedInteger(Scalar, 0, n))
665 return "invalid number";
667 return "out of range number";
672 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
677 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
679 unsigned long long n;
680 if (getAsUnsignedInteger(Scalar, 0, n))
681 return "invalid number";
682 if (n > 0xFFFFFFFFUL)
683 return "out of range number";
688 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
693 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
695 unsigned long long N;
696 if (getAsUnsignedInteger(Scalar, 0, N))
697 return "invalid number";
702 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
703 // use temp in32_t because ostream thinks int8_t is a character
708 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
710 if (getAsSignedInteger(Scalar, 0, N))
711 return "invalid number";
712 if ((N > 127) || (N < -128))
713 return "out of range number";
718 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
723 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
725 if (getAsSignedInteger(Scalar, 0, N))
726 return "invalid number";
727 if ((N > INT16_MAX) || (N < INT16_MIN))
728 return "out of range number";
733 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
738 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
740 if (getAsSignedInteger(Scalar, 0, N))
741 return "invalid number";
742 if ((N > INT32_MAX) || (N < INT32_MIN))
743 return "out of range number";
748 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
753 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
755 if (getAsSignedInteger(Scalar, 0, N))
756 return "invalid number";
761 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
762 Out << format("%g", Val);
765 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
766 SmallString<32> buff(Scalar.begin(), Scalar.end());
768 Val = strtod(buff.c_str(), &end);
770 return "invalid floating point number";
774 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
775 Out << format("%g", Val);
778 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
779 SmallString<32> buff(Scalar.begin(), Scalar.end());
781 Val = strtod(buff.c_str(), &end);
783 return "invalid floating point number";
787 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
789 Out << format("0x%02X", Num);
792 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
793 unsigned long long n;
794 if (getAsUnsignedInteger(Scalar, 0, n))
795 return "invalid hex8 number";
797 return "out of range hex8 number";
802 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
804 Out << format("0x%04X", Num);
807 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
808 unsigned long long n;
809 if (getAsUnsignedInteger(Scalar, 0, n))
810 return "invalid hex16 number";
812 return "out of range hex16 number";
817 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
819 Out << format("0x%08X", Num);
822 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
823 unsigned long long n;
824 if (getAsUnsignedInteger(Scalar, 0, n))
825 return "invalid hex32 number";
826 if (n > 0xFFFFFFFFUL)
827 return "out of range hex32 number";
832 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
834 Out << format("0x%016llX", Num);
837 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
838 unsigned long long Num;
839 if (getAsUnsignedInteger(Scalar, 0, Num))
840 return "invalid hex64 number";