1 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/YAMLTraits.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/LineIterator.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/YAMLParser.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <algorithm>
24 #include <cassert>
25 #include <cstdint>
26 #include <cstring>
27 #include <string>
28 #include <vector>
29 
30 using namespace llvm;
31 using namespace yaml;
32 
33 //===----------------------------------------------------------------------===//
34 //  IO
35 //===----------------------------------------------------------------------===//
36 
37 IO::IO(void *Context) : Ctxt(Context) {}
38 
39 IO::~IO() = default;
40 
41 void *IO::getContext() const {
42   return Ctxt;
43 }
44 
45 void IO::setContext(void *Context) {
46   Ctxt = Context;
47 }
48 
49 void IO::setAllowUnknownKeys(bool Allow) {
50   llvm_unreachable("Only supported for Input");
51 }
52 
53 //===----------------------------------------------------------------------===//
54 //  Input
55 //===----------------------------------------------------------------------===//
56 
57 Input::Input(StringRef InputContent, void *Ctxt,
58              SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
59     : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
60   if (DiagHandler)
61     SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
62   DocIterator = Strm->begin();
63 }
64 
65 Input::Input(MemoryBufferRef Input, void *Ctxt,
66              SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
67     : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
68   if (DiagHandler)
69     SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
70   DocIterator = Strm->begin();
71 }
72 
73 Input::~Input() = default;
74 
75 std::error_code Input::error() { return EC; }
76 
77 // Pin the vtables to this file.
78 void Input::HNode::anchor() {}
79 void Input::EmptyHNode::anchor() {}
80 void Input::ScalarHNode::anchor() {}
81 void Input::MapHNode::anchor() {}
82 void Input::SequenceHNode::anchor() {}
83 
84 bool Input::outputting() const {
85   return false;
86 }
87 
88 bool Input::setCurrentDocument() {
89   if (DocIterator != Strm->end()) {
90     Node *N = DocIterator->getRoot();
91     if (!N) {
92       EC = make_error_code(errc::invalid_argument);
93       return false;
94     }
95 
96     if (isa<NullNode>(N)) {
97       // Empty files are allowed and ignored
98       ++DocIterator;
99       return setCurrentDocument();
100     }
101     TopNode = createHNodes(N);
102     CurrentNode = TopNode.get();
103     return true;
104   }
105   return false;
106 }
107 
108 bool Input::nextDocument() {
109   return ++DocIterator != Strm->end();
110 }
111 
112 const Node *Input::getCurrentNode() const {
113   return CurrentNode ? CurrentNode->_node : nullptr;
114 }
115 
116 bool Input::mapTag(StringRef Tag, bool Default) {
117   // CurrentNode can be null if setCurrentDocument() was unable to
118   // parse the document because it was invalid or empty.
119   if (!CurrentNode)
120     return false;
121 
122   std::string foundTag = CurrentNode->_node->getVerbatimTag();
123   if (foundTag.empty()) {
124     // If no tag found and 'Tag' is the default, say it was found.
125     return Default;
126   }
127   // Return true iff found tag matches supplied tag.
128   return Tag.equals(foundTag);
129 }
130 
131 void Input::beginMapping() {
132   if (EC)
133     return;
134   // CurrentNode can be null if the document is empty.
135   MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
136   if (MN) {
137     MN->ValidKeys.clear();
138   }
139 }
140 
141 std::vector<StringRef> Input::keys() {
142   MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
143   std::vector<StringRef> Ret;
144   if (!MN) {
145     setError(CurrentNode, "not a mapping");
146     return Ret;
147   }
148   for (auto &P : MN->Mapping)
149     Ret.push_back(P.first());
150   return Ret;
151 }
152 
153 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
154                          void *&SaveInfo) {
155   UseDefault = false;
156   if (EC)
157     return false;
158 
159   // CurrentNode is null for empty documents, which is an error in case required
160   // nodes are present.
161   if (!CurrentNode) {
162     if (Required)
163       EC = make_error_code(errc::invalid_argument);
164     return false;
165   }
166 
167   MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
168   if (!MN) {
169     if (Required || !isa<EmptyHNode>(CurrentNode))
170       setError(CurrentNode, "not a mapping");
171     else
172       UseDefault = true;
173     return false;
174   }
175   MN->ValidKeys.push_back(Key);
176   HNode *Value = MN->Mapping[Key].first.get();
177   if (!Value) {
178     if (Required)
179       setError(CurrentNode, Twine("missing required key '") + Key + "'");
180     else
181       UseDefault = true;
182     return false;
183   }
184   SaveInfo = CurrentNode;
185   CurrentNode = Value;
186   return true;
187 }
188 
189 void Input::postflightKey(void *saveInfo) {
190   CurrentNode = reinterpret_cast<HNode *>(saveInfo);
191 }
192 
193 void Input::endMapping() {
194   if (EC)
195     return;
196   // CurrentNode can be null if the document is empty.
197   MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
198   if (!MN)
199     return;
200   for (const auto &NN : MN->Mapping) {
201     if (!is_contained(MN->ValidKeys, NN.first())) {
202       const SMRange &ReportLoc = NN.second.second;
203       if (!AllowUnknownKeys) {
204         setError(ReportLoc, Twine("unknown key '") + NN.first() + "'");
205         break;
206       } else
207         reportWarning(ReportLoc, Twine("unknown key '") + NN.first() + "'");
208     }
209   }
210 }
211 
212 void Input::beginFlowMapping() { beginMapping(); }
213 
214 void Input::endFlowMapping() { endMapping(); }
215 
216 unsigned Input::beginSequence() {
217   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
218     return SQ->Entries.size();
219   if (isa<EmptyHNode>(CurrentNode))
220     return 0;
221   // Treat case where there's a scalar "null" value as an empty sequence.
222   if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
223     if (isNull(SN->value()))
224       return 0;
225   }
226   // Any other type of HNode is an error.
227   setError(CurrentNode, "not a sequence");
228   return 0;
229 }
230 
231 void Input::endSequence() {
232 }
233 
234 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
235   if (EC)
236     return false;
237   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
238     SaveInfo = CurrentNode;
239     CurrentNode = SQ->Entries[Index].get();
240     return true;
241   }
242   return false;
243 }
244 
245 void Input::postflightElement(void *SaveInfo) {
246   CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
247 }
248 
249 unsigned Input::beginFlowSequence() { return beginSequence(); }
250 
251 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
252   if (EC)
253     return false;
254   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
255     SaveInfo = CurrentNode;
256     CurrentNode = SQ->Entries[index].get();
257     return true;
258   }
259   return false;
260 }
261 
262 void Input::postflightFlowElement(void *SaveInfo) {
263   CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
264 }
265 
266 void Input::endFlowSequence() {
267 }
268 
269 void Input::beginEnumScalar() {
270   ScalarMatchFound = false;
271 }
272 
273 bool Input::matchEnumScalar(const char *Str, bool) {
274   if (ScalarMatchFound)
275     return false;
276   if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
277     if (SN->value().equals(Str)) {
278       ScalarMatchFound = true;
279       return true;
280     }
281   }
282   return false;
283 }
284 
285 bool Input::matchEnumFallback() {
286   if (ScalarMatchFound)
287     return false;
288   ScalarMatchFound = true;
289   return true;
290 }
291 
292 void Input::endEnumScalar() {
293   if (!ScalarMatchFound) {
294     setError(CurrentNode, "unknown enumerated scalar");
295   }
296 }
297 
298 bool Input::beginBitSetScalar(bool &DoClear) {
299   BitValuesUsed.clear();
300   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
301     BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
302   } else {
303     setError(CurrentNode, "expected sequence of bit values");
304   }
305   DoClear = true;
306   return true;
307 }
308 
309 bool Input::bitSetMatch(const char *Str, bool) {
310   if (EC)
311     return false;
312   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
313     unsigned Index = 0;
314     for (auto &N : SQ->Entries) {
315       if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
316         if (SN->value().equals(Str)) {
317           BitValuesUsed[Index] = true;
318           return true;
319         }
320       } else {
321         setError(CurrentNode, "unexpected scalar in sequence of bit values");
322       }
323       ++Index;
324     }
325   } else {
326     setError(CurrentNode, "expected sequence of bit values");
327   }
328   return false;
329 }
330 
331 void Input::endBitSetScalar() {
332   if (EC)
333     return;
334   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
335     assert(BitValuesUsed.size() == SQ->Entries.size());
336     for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
337       if (!BitValuesUsed[i]) {
338         setError(SQ->Entries[i].get(), "unknown bit value");
339         return;
340       }
341     }
342   }
343 }
344 
345 void Input::scalarString(StringRef &S, QuotingType) {
346   if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
347     S = SN->value();
348   } else {
349     setError(CurrentNode, "unexpected scalar");
350   }
351 }
352 
353 void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); }
354 
355 void Input::scalarTag(std::string &Tag) {
356   Tag = CurrentNode->_node->getVerbatimTag();
357 }
358 
359 void Input::setError(HNode *hnode, const Twine &message) {
360   assert(hnode && "HNode must not be NULL");
361   setError(hnode->_node, message);
362 }
363 
364 NodeKind Input::getNodeKind() {
365   if (isa<ScalarHNode>(CurrentNode))
366     return NodeKind::Scalar;
367   else if (isa<MapHNode>(CurrentNode))
368     return NodeKind::Map;
369   else if (isa<SequenceHNode>(CurrentNode))
370     return NodeKind::Sequence;
371   llvm_unreachable("Unsupported node kind");
372 }
373 
374 void Input::setError(Node *node, const Twine &message) {
375   Strm->printError(node, message);
376   EC = make_error_code(errc::invalid_argument);
377 }
378 
379 void Input::setError(const SMRange &range, const Twine &message) {
380   Strm->printError(range, message);
381   EC = make_error_code(errc::invalid_argument);
382 }
383 
384 void Input::reportWarning(HNode *hnode, const Twine &message) {
385   assert(hnode && "HNode must not be NULL");
386   Strm->printError(hnode->_node, message, SourceMgr::DK_Warning);
387 }
388 
389 void Input::reportWarning(Node *node, const Twine &message) {
390   Strm->printError(node, message, SourceMgr::DK_Warning);
391 }
392 
393 void Input::reportWarning(const SMRange &range, const Twine &message) {
394   Strm->printError(range, message, SourceMgr::DK_Warning);
395 }
396 
397 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
398   SmallString<128> StringStorage;
399   if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
400     StringRef KeyStr = SN->getValue(StringStorage);
401     if (!StringStorage.empty()) {
402       // Copy string to permanent storage
403       KeyStr = StringStorage.str().copy(StringAllocator);
404     }
405     return std::make_unique<ScalarHNode>(N, KeyStr);
406   } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
407     StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
408     return std::make_unique<ScalarHNode>(N, ValueCopy);
409   } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
410     auto SQHNode = std::make_unique<SequenceHNode>(N);
411     for (Node &SN : *SQ) {
412       auto Entry = createHNodes(&SN);
413       if (EC)
414         break;
415       SQHNode->Entries.push_back(std::move(Entry));
416     }
417     return std::move(SQHNode);
418   } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
419     auto mapHNode = std::make_unique<MapHNode>(N);
420     for (KeyValueNode &KVN : *Map) {
421       Node *KeyNode = KVN.getKey();
422       ScalarNode *Key = dyn_cast_or_null<ScalarNode>(KeyNode);
423       Node *Value = KVN.getValue();
424       if (!Key || !Value) {
425         if (!Key)
426           setError(KeyNode, "Map key must be a scalar");
427         if (!Value)
428           setError(KeyNode, "Map value must not be empty");
429         break;
430       }
431       StringStorage.clear();
432       StringRef KeyStr = Key->getValue(StringStorage);
433       if (!StringStorage.empty()) {
434         // Copy string to permanent storage
435         KeyStr = StringStorage.str().copy(StringAllocator);
436       }
437       auto ValueHNode = createHNodes(Value);
438       if (EC)
439         break;
440       mapHNode->Mapping[KeyStr] =
441           std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
442     }
443     return std::move(mapHNode);
444   } else if (isa<NullNode>(N)) {
445     return std::make_unique<EmptyHNode>(N);
446   } else {
447     setError(N, "unknown node kind");
448     return nullptr;
449   }
450 }
451 
452 void Input::setError(const Twine &Message) {
453   setError(CurrentNode, Message);
454 }
455 
456 void Input::setAllowUnknownKeys(bool Allow) { AllowUnknownKeys = Allow; }
457 
458 bool Input::canElideEmptySequence() {
459   return false;
460 }
461 
462 //===----------------------------------------------------------------------===//
463 //  Output
464 //===----------------------------------------------------------------------===//
465 
466 Output::Output(raw_ostream &yout, void *context, int WrapColumn)
467     : IO(context), Out(yout), WrapColumn(WrapColumn) {}
468 
469 Output::~Output() = default;
470 
471 bool Output::outputting() const {
472   return true;
473 }
474 
475 void Output::beginMapping() {
476   StateStack.push_back(inMapFirstKey);
477   PaddingBeforeContainer = Padding;
478   Padding = "\n";
479 }
480 
481 bool Output::mapTag(StringRef Tag, bool Use) {
482   if (Use) {
483     // If this tag is being written inside a sequence we should write the start
484     // of the sequence before writing the tag, otherwise the tag won't be
485     // attached to the element in the sequence, but rather the sequence itself.
486     bool SequenceElement = false;
487     if (StateStack.size() > 1) {
488       auto &E = StateStack[StateStack.size() - 2];
489       SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
490     }
491     if (SequenceElement && StateStack.back() == inMapFirstKey) {
492       newLineCheck();
493     } else {
494       output(" ");
495     }
496     output(Tag);
497     if (SequenceElement) {
498       // If we're writing the tag during the first element of a map, the tag
499       // takes the place of the first element in the sequence.
500       if (StateStack.back() == inMapFirstKey) {
501         StateStack.pop_back();
502         StateStack.push_back(inMapOtherKey);
503       }
504       // Tags inside maps in sequences should act as keys in the map from a
505       // formatting perspective, so we always want a newline in a sequence.
506       Padding = "\n";
507     }
508   }
509   return Use;
510 }
511 
512 void Output::endMapping() {
513   // If we did not map anything, we should explicitly emit an empty map
514   if (StateStack.back() == inMapFirstKey) {
515     Padding = PaddingBeforeContainer;
516     newLineCheck();
517     output("{}");
518     Padding = "\n";
519   }
520   StateStack.pop_back();
521 }
522 
523 std::vector<StringRef> Output::keys() {
524   report_fatal_error("invalid call");
525 }
526 
527 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
528                           bool &UseDefault, void *&SaveInfo) {
529   UseDefault = false;
530   SaveInfo = nullptr;
531   if (Required || !SameAsDefault || WriteDefaultValues) {
532     auto State = StateStack.back();
533     if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
534       flowKey(Key);
535     } else {
536       newLineCheck();
537       paddedKey(Key);
538     }
539     return true;
540   }
541   return false;
542 }
543 
544 void Output::postflightKey(void *) {
545   if (StateStack.back() == inMapFirstKey) {
546     StateStack.pop_back();
547     StateStack.push_back(inMapOtherKey);
548   } else if (StateStack.back() == inFlowMapFirstKey) {
549     StateStack.pop_back();
550     StateStack.push_back(inFlowMapOtherKey);
551   }
552 }
553 
554 void Output::beginFlowMapping() {
555   StateStack.push_back(inFlowMapFirstKey);
556   newLineCheck();
557   ColumnAtMapFlowStart = Column;
558   output("{ ");
559 }
560 
561 void Output::endFlowMapping() {
562   StateStack.pop_back();
563   outputUpToEndOfLine(" }");
564 }
565 
566 void Output::beginDocuments() {
567   outputUpToEndOfLine("---");
568 }
569 
570 bool Output::preflightDocument(unsigned index) {
571   if (index > 0)
572     outputUpToEndOfLine("\n---");
573   return true;
574 }
575 
576 void Output::postflightDocument() {
577 }
578 
579 void Output::endDocuments() {
580   output("\n...\n");
581 }
582 
583 unsigned Output::beginSequence() {
584   StateStack.push_back(inSeqFirstElement);
585   PaddingBeforeContainer = Padding;
586   Padding = "\n";
587   return 0;
588 }
589 
590 void Output::endSequence() {
591   // If we did not emit anything, we should explicitly emit an empty sequence
592   if (StateStack.back() == inSeqFirstElement) {
593     Padding = PaddingBeforeContainer;
594     newLineCheck(/*EmptySequence=*/true);
595     output("[]");
596     Padding = "\n";
597   }
598   StateStack.pop_back();
599 }
600 
601 bool Output::preflightElement(unsigned, void *&SaveInfo) {
602   SaveInfo = nullptr;
603   return true;
604 }
605 
606 void Output::postflightElement(void *) {
607   if (StateStack.back() == inSeqFirstElement) {
608     StateStack.pop_back();
609     StateStack.push_back(inSeqOtherElement);
610   } else if (StateStack.back() == inFlowSeqFirstElement) {
611     StateStack.pop_back();
612     StateStack.push_back(inFlowSeqOtherElement);
613   }
614 }
615 
616 unsigned Output::beginFlowSequence() {
617   StateStack.push_back(inFlowSeqFirstElement);
618   newLineCheck();
619   ColumnAtFlowStart = Column;
620   output("[ ");
621   NeedFlowSequenceComma = false;
622   return 0;
623 }
624 
625 void Output::endFlowSequence() {
626   StateStack.pop_back();
627   outputUpToEndOfLine(" ]");
628 }
629 
630 bool Output::preflightFlowElement(unsigned, void *&SaveInfo) {
631   if (NeedFlowSequenceComma)
632     output(", ");
633   if (WrapColumn && Column > WrapColumn) {
634     output("\n");
635     for (int i = 0; i < ColumnAtFlowStart; ++i)
636       output(" ");
637     Column = ColumnAtFlowStart;
638     output("  ");
639   }
640   SaveInfo = nullptr;
641   return true;
642 }
643 
644 void Output::postflightFlowElement(void *) {
645   NeedFlowSequenceComma = true;
646 }
647 
648 void Output::beginEnumScalar() {
649   EnumerationMatchFound = false;
650 }
651 
652 bool Output::matchEnumScalar(const char *Str, bool Match) {
653   if (Match && !EnumerationMatchFound) {
654     newLineCheck();
655     outputUpToEndOfLine(Str);
656     EnumerationMatchFound = true;
657   }
658   return false;
659 }
660 
661 bool Output::matchEnumFallback() {
662   if (EnumerationMatchFound)
663     return false;
664   EnumerationMatchFound = true;
665   return true;
666 }
667 
668 void Output::endEnumScalar() {
669   if (!EnumerationMatchFound)
670     llvm_unreachable("bad runtime enum value");
671 }
672 
673 bool Output::beginBitSetScalar(bool &DoClear) {
674   newLineCheck();
675   output("[ ");
676   NeedBitValueComma = false;
677   DoClear = false;
678   return true;
679 }
680 
681 bool Output::bitSetMatch(const char *Str, bool Matches) {
682   if (Matches) {
683     if (NeedBitValueComma)
684       output(", ");
685     output(Str);
686     NeedBitValueComma = true;
687   }
688   return false;
689 }
690 
691 void Output::endBitSetScalar() {
692   outputUpToEndOfLine(" ]");
693 }
694 
695 void Output::scalarString(StringRef &S, QuotingType MustQuote) {
696   newLineCheck();
697   if (S.empty()) {
698     // Print '' for the empty string because leaving the field empty is not
699     // allowed.
700     outputUpToEndOfLine("''");
701     return;
702   }
703   if (MustQuote == QuotingType::None) {
704     // Only quote if we must.
705     outputUpToEndOfLine(S);
706     return;
707   }
708 
709   const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
710   output(Quote); // Starting quote.
711 
712   // When using double-quoted strings (and only in that case), non-printable characters may be
713   // present, and will be escaped using a variety of unicode-scalar and special short-form
714   // escapes. This is handled in yaml::escape.
715   if (MustQuote == QuotingType::Double) {
716     output(yaml::escape(S, /* EscapePrintable= */ false));
717     outputUpToEndOfLine(Quote);
718     return;
719   }
720 
721   unsigned i = 0;
722   unsigned j = 0;
723   unsigned End = S.size();
724   const char *Base = S.data();
725 
726   // When using single-quoted strings, any single quote ' must be doubled to be escaped.
727   while (j < End) {
728     if (S[j] == '\'') {                    // Escape quotes.
729       output(StringRef(&Base[i], j - i));  // "flush".
730       output(StringLiteral("''"));         // Print it as ''
731       i = j + 1;
732     }
733     ++j;
734   }
735   output(StringRef(&Base[i], j - i));
736   outputUpToEndOfLine(Quote); // Ending quote.
737 }
738 
739 void Output::blockScalarString(StringRef &S) {
740   if (!StateStack.empty())
741     newLineCheck();
742   output(" |");
743   outputNewLine();
744 
745   unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
746 
747   auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
748   for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
749     for (unsigned I = 0; I < Indent; ++I) {
750       output("  ");
751     }
752     output(*Lines);
753     outputNewLine();
754   }
755 }
756 
757 void Output::scalarTag(std::string &Tag) {
758   if (Tag.empty())
759     return;
760   newLineCheck();
761   output(Tag);
762   output(" ");
763 }
764 
765 void Output::setError(const Twine &message) {
766 }
767 
768 bool Output::canElideEmptySequence() {
769   // Normally, with an optional key/value where the value is an empty sequence,
770   // the whole key/value can be not written.  But, that produces wrong yaml
771   // if the key/value is the only thing in the map and the map is used in
772   // a sequence.  This detects if the this sequence is the first key/value
773   // in map that itself is embedded in a sequence.
774   if (StateStack.size() < 2)
775     return true;
776   if (StateStack.back() != inMapFirstKey)
777     return true;
778   return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
779 }
780 
781 void Output::output(StringRef s) {
782   Column += s.size();
783   Out << s;
784 }
785 
786 void Output::outputUpToEndOfLine(StringRef s) {
787   output(s);
788   if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
789                              !inFlowMapAnyKey(StateStack.back())))
790     Padding = "\n";
791 }
792 
793 void Output::outputNewLine() {
794   Out << "\n";
795   Column = 0;
796 }
797 
798 // if seq at top, indent as if map, then add "- "
799 // if seq in middle, use "- " if firstKey, else use "  "
800 //
801 
802 void Output::newLineCheck(bool EmptySequence) {
803   if (Padding != "\n") {
804     output(Padding);
805     Padding = {};
806     return;
807   }
808   outputNewLine();
809   Padding = {};
810 
811   if (StateStack.size() == 0 || EmptySequence)
812     return;
813 
814   unsigned Indent = StateStack.size() - 1;
815   bool OutputDash = false;
816 
817   if (StateStack.back() == inSeqFirstElement ||
818       StateStack.back() == inSeqOtherElement) {
819     OutputDash = true;
820   } else if ((StateStack.size() > 1) &&
821              ((StateStack.back() == inMapFirstKey) ||
822               inFlowSeqAnyElement(StateStack.back()) ||
823               (StateStack.back() == inFlowMapFirstKey)) &&
824              inSeqAnyElement(StateStack[StateStack.size() - 2])) {
825     --Indent;
826     OutputDash = true;
827   }
828 
829   for (unsigned i = 0; i < Indent; ++i) {
830     output("  ");
831   }
832   if (OutputDash) {
833     output("- ");
834   }
835 }
836 
837 void Output::paddedKey(StringRef key) {
838   output(key);
839   output(":");
840   const char *spaces = "                ";
841   if (key.size() < strlen(spaces))
842     Padding = &spaces[key.size()];
843   else
844     Padding = " ";
845 }
846 
847 void Output::flowKey(StringRef Key) {
848   if (StateStack.back() == inFlowMapOtherKey)
849     output(", ");
850   if (WrapColumn && Column > WrapColumn) {
851     output("\n");
852     for (int I = 0; I < ColumnAtMapFlowStart; ++I)
853       output(" ");
854     Column = ColumnAtMapFlowStart;
855     output("  ");
856   }
857   output(Key);
858   output(": ");
859 }
860 
861 NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); }
862 
863 bool Output::inSeqAnyElement(InState State) {
864   return State == inSeqFirstElement || State == inSeqOtherElement;
865 }
866 
867 bool Output::inFlowSeqAnyElement(InState State) {
868   return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
869 }
870 
871 bool Output::inMapAnyKey(InState State) {
872   return State == inMapFirstKey || State == inMapOtherKey;
873 }
874 
875 bool Output::inFlowMapAnyKey(InState State) {
876   return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
877 }
878 
879 //===----------------------------------------------------------------------===//
880 //  traits for built-in types
881 //===----------------------------------------------------------------------===//
882 
883 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
884   Out << (Val ? "true" : "false");
885 }
886 
887 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
888   if (llvm::Optional<bool> Parsed = parseBool(Scalar)) {
889     Val = *Parsed;
890     return StringRef();
891   }
892   return "invalid boolean";
893 }
894 
895 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
896                                      raw_ostream &Out) {
897   Out << Val;
898 }
899 
900 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
901                                          StringRef &Val) {
902   Val = Scalar;
903   return StringRef();
904 }
905 
906 void ScalarTraits<std::string>::output(const std::string &Val, void *,
907                                        raw_ostream &Out) {
908   Out << Val;
909 }
910 
911 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
912                                            std::string &Val) {
913   Val = Scalar.str();
914   return StringRef();
915 }
916 
917 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
918                                    raw_ostream &Out) {
919   // use temp uin32_t because ostream thinks uint8_t is a character
920   uint32_t Num = Val;
921   Out << Num;
922 }
923 
924 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
925   unsigned long long n;
926   if (getAsUnsignedInteger(Scalar, 0, n))
927     return "invalid number";
928   if (n > 0xFF)
929     return "out of range number";
930   Val = n;
931   return StringRef();
932 }
933 
934 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
935                                     raw_ostream &Out) {
936   Out << Val;
937 }
938 
939 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
940                                         uint16_t &Val) {
941   unsigned long long n;
942   if (getAsUnsignedInteger(Scalar, 0, n))
943     return "invalid number";
944   if (n > 0xFFFF)
945     return "out of range number";
946   Val = n;
947   return StringRef();
948 }
949 
950 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
951                                     raw_ostream &Out) {
952   Out << Val;
953 }
954 
955 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
956                                         uint32_t &Val) {
957   unsigned long long n;
958   if (getAsUnsignedInteger(Scalar, 0, n))
959     return "invalid number";
960   if (n > 0xFFFFFFFFUL)
961     return "out of range number";
962   Val = n;
963   return StringRef();
964 }
965 
966 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
967                                     raw_ostream &Out) {
968   Out << Val;
969 }
970 
971 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
972                                         uint64_t &Val) {
973   unsigned long long N;
974   if (getAsUnsignedInteger(Scalar, 0, N))
975     return "invalid number";
976   Val = N;
977   return StringRef();
978 }
979 
980 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
981   // use temp in32_t because ostream thinks int8_t is a character
982   int32_t Num = Val;
983   Out << Num;
984 }
985 
986 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
987   long long N;
988   if (getAsSignedInteger(Scalar, 0, N))
989     return "invalid number";
990   if ((N > 127) || (N < -128))
991     return "out of range number";
992   Val = N;
993   return StringRef();
994 }
995 
996 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
997                                    raw_ostream &Out) {
998   Out << Val;
999 }
1000 
1001 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
1002   long long N;
1003   if (getAsSignedInteger(Scalar, 0, N))
1004     return "invalid number";
1005   if ((N > INT16_MAX) || (N < INT16_MIN))
1006     return "out of range number";
1007   Val = N;
1008   return StringRef();
1009 }
1010 
1011 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
1012                                    raw_ostream &Out) {
1013   Out << Val;
1014 }
1015 
1016 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
1017   long long N;
1018   if (getAsSignedInteger(Scalar, 0, N))
1019     return "invalid number";
1020   if ((N > INT32_MAX) || (N < INT32_MIN))
1021     return "out of range number";
1022   Val = N;
1023   return StringRef();
1024 }
1025 
1026 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
1027                                    raw_ostream &Out) {
1028   Out << Val;
1029 }
1030 
1031 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
1032   long long N;
1033   if (getAsSignedInteger(Scalar, 0, N))
1034     return "invalid number";
1035   Val = N;
1036   return StringRef();
1037 }
1038 
1039 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
1040   Out << format("%g", Val);
1041 }
1042 
1043 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
1044   if (to_float(Scalar, Val))
1045     return StringRef();
1046   return "invalid floating point number";
1047 }
1048 
1049 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
1050   Out << format("%g", Val);
1051 }
1052 
1053 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
1054   if (to_float(Scalar, Val))
1055     return StringRef();
1056   return "invalid floating point number";
1057 }
1058 
1059 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
1060   Out << format("0x%" PRIX8, (uint8_t)Val);
1061 }
1062 
1063 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
1064   unsigned long long n;
1065   if (getAsUnsignedInteger(Scalar, 0, n))
1066     return "invalid hex8 number";
1067   if (n > 0xFF)
1068     return "out of range hex8 number";
1069   Val = n;
1070   return StringRef();
1071 }
1072 
1073 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
1074   Out << format("0x%" PRIX16, (uint16_t)Val);
1075 }
1076 
1077 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
1078   unsigned long long n;
1079   if (getAsUnsignedInteger(Scalar, 0, n))
1080     return "invalid hex16 number";
1081   if (n > 0xFFFF)
1082     return "out of range hex16 number";
1083   Val = n;
1084   return StringRef();
1085 }
1086 
1087 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1088   Out << format("0x%" PRIX32, (uint32_t)Val);
1089 }
1090 
1091 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1092   unsigned long long n;
1093   if (getAsUnsignedInteger(Scalar, 0, n))
1094     return "invalid hex32 number";
1095   if (n > 0xFFFFFFFFUL)
1096     return "out of range hex32 number";
1097   Val = n;
1098   return StringRef();
1099 }
1100 
1101 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1102   Out << format("0x%" PRIX64, (uint64_t)Val);
1103 }
1104 
1105 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1106   unsigned long long Num;
1107   if (getAsUnsignedInteger(Scalar, 0, Num))
1108     return "invalid hex64 number";
1109   Val = Num;
1110   return StringRef();
1111 }
1112 
1113 void ScalarTraits<VersionTuple>::output(const VersionTuple &Val, void *,
1114                                         llvm::raw_ostream &Out) {
1115   Out << Val.getAsString();
1116 }
1117 
1118 StringRef ScalarTraits<VersionTuple>::input(StringRef Scalar, void *,
1119                                             VersionTuple &Val) {
1120   if (Val.tryParse(Scalar))
1121     return "invalid version format";
1122   return StringRef();
1123 }
1124