[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / lib / Support / YAMLTraits.cpp
blob9325a09faaea0229ad1b2a1dbcbaf0a65e718889
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 //===----------------------------------------------------------------------===//
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/VersionTuple.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <cstdint>
27 #include <cstring>
28 #include <string>
29 #include <vector>
31 using namespace llvm;
32 using namespace yaml;
34 //===----------------------------------------------------------------------===//
35 // IO
36 //===----------------------------------------------------------------------===//
38 IO::IO(void *Context) : Ctxt(Context) {}
40 IO::~IO() = default;
42 void *IO::getContext() const {
43 return Ctxt;
46 void IO::setContext(void *Context) {
47 Ctxt = Context;
50 void IO::setAllowUnknownKeys(bool Allow) {
51 llvm_unreachable("Only supported for Input");
54 //===----------------------------------------------------------------------===//
55 // Input
56 //===----------------------------------------------------------------------===//
58 Input::Input(StringRef InputContent, void *Ctxt,
59 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
60 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
61 if (DiagHandler)
62 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
63 DocIterator = Strm->begin();
66 Input::Input(MemoryBufferRef Input, void *Ctxt,
67 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
68 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
69 if (DiagHandler)
70 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
71 DocIterator = Strm->begin();
74 Input::~Input() = default;
76 std::error_code Input::error() { return EC; }
78 bool Input::outputting() const {
79 return false;
82 bool Input::setCurrentDocument() {
83 if (DocIterator != Strm->end()) {
84 Node *N = DocIterator->getRoot();
85 if (!N) {
86 EC = make_error_code(errc::invalid_argument);
87 return false;
90 if (isa<NullNode>(N)) {
91 // Empty files are allowed and ignored
92 ++DocIterator;
93 return setCurrentDocument();
95 releaseHNodeBuffers();
96 TopNode = createHNodes(N);
97 CurrentNode = TopNode;
98 return true;
100 return false;
103 bool Input::nextDocument() {
104 return ++DocIterator != Strm->end();
107 const Node *Input::getCurrentNode() const {
108 return CurrentNode ? CurrentNode->_node : nullptr;
111 bool Input::mapTag(StringRef Tag, bool Default) {
112 // CurrentNode can be null if setCurrentDocument() was unable to
113 // parse the document because it was invalid or empty.
114 if (!CurrentNode)
115 return false;
117 std::string foundTag = CurrentNode->_node->getVerbatimTag();
118 if (foundTag.empty()) {
119 // If no tag found and 'Tag' is the default, say it was found.
120 return Default;
122 // Return true iff found tag matches supplied tag.
123 return Tag.equals(foundTag);
126 void Input::beginMapping() {
127 if (EC)
128 return;
129 // CurrentNode can be null if the document is empty.
130 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
131 if (MN) {
132 MN->ValidKeys.clear();
136 std::vector<StringRef> Input::keys() {
137 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
138 std::vector<StringRef> Ret;
139 if (!MN) {
140 setError(CurrentNode, "not a mapping");
141 return Ret;
143 for (auto &P : MN->Mapping)
144 Ret.push_back(P.first());
145 return Ret;
148 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
149 void *&SaveInfo) {
150 UseDefault = false;
151 if (EC)
152 return false;
154 // CurrentNode is null for empty documents, which is an error in case required
155 // nodes are present.
156 if (!CurrentNode) {
157 if (Required)
158 EC = make_error_code(errc::invalid_argument);
159 return false;
162 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
163 if (!MN) {
164 if (Required || !isa<EmptyHNode>(CurrentNode))
165 setError(CurrentNode, "not a mapping");
166 else
167 UseDefault = true;
168 return false;
170 MN->ValidKeys.push_back(Key);
171 HNode *Value = MN->Mapping[Key].first;
172 if (!Value) {
173 if (Required)
174 setError(CurrentNode, Twine("missing required key '") + Key + "'");
175 else
176 UseDefault = true;
177 return false;
179 SaveInfo = CurrentNode;
180 CurrentNode = Value;
181 return true;
184 void Input::postflightKey(void *saveInfo) {
185 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
188 void Input::endMapping() {
189 if (EC)
190 return;
191 // CurrentNode can be null if the document is empty.
192 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
193 if (!MN)
194 return;
195 for (const auto &NN : MN->Mapping) {
196 if (!is_contained(MN->ValidKeys, NN.first())) {
197 const SMRange &ReportLoc = NN.second.second;
198 if (!AllowUnknownKeys) {
199 setError(ReportLoc, Twine("unknown key '") + NN.first() + "'");
200 break;
201 } else
202 reportWarning(ReportLoc, Twine("unknown key '") + NN.first() + "'");
207 void Input::beginFlowMapping() { beginMapping(); }
209 void Input::endFlowMapping() { endMapping(); }
211 unsigned Input::beginSequence() {
212 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
213 return SQ->Entries.size();
214 if (isa<EmptyHNode>(CurrentNode))
215 return 0;
216 // Treat case where there's a scalar "null" value as an empty sequence.
217 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
218 if (isNull(SN->value()))
219 return 0;
221 // Any other type of HNode is an error.
222 setError(CurrentNode, "not a sequence");
223 return 0;
226 void Input::endSequence() {
229 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
230 if (EC)
231 return false;
232 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
233 SaveInfo = CurrentNode;
234 CurrentNode = SQ->Entries[Index];
235 return true;
237 return false;
240 void Input::postflightElement(void *SaveInfo) {
241 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
244 unsigned Input::beginFlowSequence() { return beginSequence(); }
246 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
247 if (EC)
248 return false;
249 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
250 SaveInfo = CurrentNode;
251 CurrentNode = SQ->Entries[index];
252 return true;
254 return false;
257 void Input::postflightFlowElement(void *SaveInfo) {
258 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
261 void Input::endFlowSequence() {
264 void Input::beginEnumScalar() {
265 ScalarMatchFound = false;
268 bool Input::matchEnumScalar(const char *Str, bool) {
269 if (ScalarMatchFound)
270 return false;
271 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
272 if (SN->value().equals(Str)) {
273 ScalarMatchFound = true;
274 return true;
277 return false;
280 bool Input::matchEnumFallback() {
281 if (ScalarMatchFound)
282 return false;
283 ScalarMatchFound = true;
284 return true;
287 void Input::endEnumScalar() {
288 if (!ScalarMatchFound) {
289 setError(CurrentNode, "unknown enumerated scalar");
293 bool Input::beginBitSetScalar(bool &DoClear) {
294 BitValuesUsed.clear();
295 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
296 BitValuesUsed.resize(SQ->Entries.size());
297 } else {
298 setError(CurrentNode, "expected sequence of bit values");
300 DoClear = true;
301 return true;
304 bool Input::bitSetMatch(const char *Str, bool) {
305 if (EC)
306 return false;
307 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
308 unsigned Index = 0;
309 for (auto &N : SQ->Entries) {
310 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
311 if (SN->value().equals(Str)) {
312 BitValuesUsed[Index] = true;
313 return true;
315 } else {
316 setError(CurrentNode, "unexpected scalar in sequence of bit values");
318 ++Index;
320 } else {
321 setError(CurrentNode, "expected sequence of bit values");
323 return false;
326 void Input::endBitSetScalar() {
327 if (EC)
328 return;
329 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
330 assert(BitValuesUsed.size() == SQ->Entries.size());
331 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
332 if (!BitValuesUsed[i]) {
333 setError(SQ->Entries[i], "unknown bit value");
334 return;
340 void Input::scalarString(StringRef &S, QuotingType) {
341 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
342 S = SN->value();
343 } else {
344 setError(CurrentNode, "unexpected scalar");
348 void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); }
350 void Input::scalarTag(std::string &Tag) {
351 Tag = CurrentNode->_node->getVerbatimTag();
354 void Input::setError(HNode *hnode, const Twine &message) {
355 assert(hnode && "HNode must not be NULL");
356 setError(hnode->_node, message);
359 NodeKind Input::getNodeKind() {
360 if (isa<ScalarHNode>(CurrentNode))
361 return NodeKind::Scalar;
362 else if (isa<MapHNode>(CurrentNode))
363 return NodeKind::Map;
364 else if (isa<SequenceHNode>(CurrentNode))
365 return NodeKind::Sequence;
366 llvm_unreachable("Unsupported node kind");
369 void Input::setError(Node *node, const Twine &message) {
370 Strm->printError(node, message);
371 EC = make_error_code(errc::invalid_argument);
374 void Input::setError(const SMRange &range, const Twine &message) {
375 Strm->printError(range, message);
376 EC = make_error_code(errc::invalid_argument);
379 void Input::reportWarning(HNode *hnode, const Twine &message) {
380 assert(hnode && "HNode must not be NULL");
381 Strm->printError(hnode->_node, message, SourceMgr::DK_Warning);
384 void Input::reportWarning(Node *node, const Twine &message) {
385 Strm->printError(node, message, SourceMgr::DK_Warning);
388 void Input::reportWarning(const SMRange &range, const Twine &message) {
389 Strm->printError(range, message, SourceMgr::DK_Warning);
392 void Input::releaseHNodeBuffers() {
393 EmptyHNodeAllocator.DestroyAll();
394 ScalarHNodeAllocator.DestroyAll();
395 SequenceHNodeAllocator.DestroyAll();
396 MapHNodeAllocator.DestroyAll();
399 Input::HNode *Input::createHNodes(Node *N) {
400 SmallString<128> StringStorage;
401 switch (N->getType()) {
402 case Node::NK_Scalar: {
403 ScalarNode *SN = dyn_cast<ScalarNode>(N);
404 StringRef KeyStr = SN->getValue(StringStorage);
405 if (!StringStorage.empty()) {
406 // Copy string to permanent storage
407 KeyStr = StringStorage.str().copy(StringAllocator);
409 return new (ScalarHNodeAllocator.Allocate()) ScalarHNode(N, KeyStr);
411 case Node::NK_BlockScalar: {
412 BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N);
413 StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
414 return new (ScalarHNodeAllocator.Allocate()) ScalarHNode(N, ValueCopy);
416 case Node::NK_Sequence: {
417 SequenceNode *SQ = dyn_cast<SequenceNode>(N);
418 auto SQHNode = new (SequenceHNodeAllocator.Allocate()) SequenceHNode(N);
419 for (Node &SN : *SQ) {
420 auto Entry = createHNodes(&SN);
421 if (EC)
422 break;
423 SQHNode->Entries.push_back(Entry);
425 return SQHNode;
427 case Node::NK_Mapping: {
428 MappingNode *Map = dyn_cast<MappingNode>(N);
429 auto mapHNode = new (MapHNodeAllocator.Allocate()) MapHNode(N);
430 for (KeyValueNode &KVN : *Map) {
431 Node *KeyNode = KVN.getKey();
432 ScalarNode *Key = dyn_cast_or_null<ScalarNode>(KeyNode);
433 Node *Value = KVN.getValue();
434 if (!Key || !Value) {
435 if (!Key)
436 setError(KeyNode, "Map key must be a scalar");
437 if (!Value)
438 setError(KeyNode, "Map value must not be empty");
439 break;
441 StringStorage.clear();
442 StringRef KeyStr = Key->getValue(StringStorage);
443 if (!StringStorage.empty()) {
444 // Copy string to permanent storage
445 KeyStr = StringStorage.str().copy(StringAllocator);
447 if (mapHNode->Mapping.count(KeyStr))
448 // From YAML spec: "The content of a mapping node is an unordered set of
449 // key/value node pairs, with the restriction that each of the keys is
450 // unique."
451 setError(KeyNode, Twine("duplicated mapping key '") + KeyStr + "'");
452 auto ValueHNode = createHNodes(Value);
453 if (EC)
454 break;
455 mapHNode->Mapping[KeyStr] =
456 std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
458 return std::move(mapHNode);
460 case Node::NK_Null:
461 return new (EmptyHNodeAllocator.Allocate()) EmptyHNode(N);
462 default:
463 setError(N, "unknown node kind");
464 return nullptr;
468 void Input::setError(const Twine &Message) {
469 setError(CurrentNode, Message);
472 void Input::setAllowUnknownKeys(bool Allow) { AllowUnknownKeys = Allow; }
474 bool Input::canElideEmptySequence() {
475 return false;
478 //===----------------------------------------------------------------------===//
479 // Output
480 //===----------------------------------------------------------------------===//
482 Output::Output(raw_ostream &yout, void *context, int WrapColumn)
483 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
485 Output::~Output() = default;
487 bool Output::outputting() const {
488 return true;
491 void Output::beginMapping() {
492 StateStack.push_back(inMapFirstKey);
493 PaddingBeforeContainer = Padding;
494 Padding = "\n";
497 bool Output::mapTag(StringRef Tag, bool Use) {
498 if (Use) {
499 // If this tag is being written inside a sequence we should write the start
500 // of the sequence before writing the tag, otherwise the tag won't be
501 // attached to the element in the sequence, but rather the sequence itself.
502 bool SequenceElement = false;
503 if (StateStack.size() > 1) {
504 auto &E = StateStack[StateStack.size() - 2];
505 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
507 if (SequenceElement && StateStack.back() == inMapFirstKey) {
508 newLineCheck();
509 } else {
510 output(" ");
512 output(Tag);
513 if (SequenceElement) {
514 // If we're writing the tag during the first element of a map, the tag
515 // takes the place of the first element in the sequence.
516 if (StateStack.back() == inMapFirstKey) {
517 StateStack.pop_back();
518 StateStack.push_back(inMapOtherKey);
520 // Tags inside maps in sequences should act as keys in the map from a
521 // formatting perspective, so we always want a newline in a sequence.
522 Padding = "\n";
525 return Use;
528 void Output::endMapping() {
529 // If we did not map anything, we should explicitly emit an empty map
530 if (StateStack.back() == inMapFirstKey) {
531 Padding = PaddingBeforeContainer;
532 newLineCheck();
533 output("{}");
534 Padding = "\n";
536 StateStack.pop_back();
539 std::vector<StringRef> Output::keys() {
540 report_fatal_error("invalid call");
543 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
544 bool &UseDefault, void *&SaveInfo) {
545 UseDefault = false;
546 SaveInfo = nullptr;
547 if (Required || !SameAsDefault || WriteDefaultValues) {
548 auto State = StateStack.back();
549 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
550 flowKey(Key);
551 } else {
552 newLineCheck();
553 paddedKey(Key);
555 return true;
557 return false;
560 void Output::postflightKey(void *) {
561 if (StateStack.back() == inMapFirstKey) {
562 StateStack.pop_back();
563 StateStack.push_back(inMapOtherKey);
564 } else if (StateStack.back() == inFlowMapFirstKey) {
565 StateStack.pop_back();
566 StateStack.push_back(inFlowMapOtherKey);
570 void Output::beginFlowMapping() {
571 StateStack.push_back(inFlowMapFirstKey);
572 newLineCheck();
573 ColumnAtMapFlowStart = Column;
574 output("{ ");
577 void Output::endFlowMapping() {
578 StateStack.pop_back();
579 outputUpToEndOfLine(" }");
582 void Output::beginDocuments() {
583 outputUpToEndOfLine("---");
586 bool Output::preflightDocument(unsigned index) {
587 if (index > 0)
588 outputUpToEndOfLine("\n---");
589 return true;
592 void Output::postflightDocument() {
595 void Output::endDocuments() {
596 output("\n...\n");
599 unsigned Output::beginSequence() {
600 StateStack.push_back(inSeqFirstElement);
601 PaddingBeforeContainer = Padding;
602 Padding = "\n";
603 return 0;
606 void Output::endSequence() {
607 // If we did not emit anything, we should explicitly emit an empty sequence
608 if (StateStack.back() == inSeqFirstElement) {
609 Padding = PaddingBeforeContainer;
610 newLineCheck(/*EmptySequence=*/true);
611 output("[]");
612 Padding = "\n";
614 StateStack.pop_back();
617 bool Output::preflightElement(unsigned, void *&SaveInfo) {
618 SaveInfo = nullptr;
619 return true;
622 void Output::postflightElement(void *) {
623 if (StateStack.back() == inSeqFirstElement) {
624 StateStack.pop_back();
625 StateStack.push_back(inSeqOtherElement);
626 } else if (StateStack.back() == inFlowSeqFirstElement) {
627 StateStack.pop_back();
628 StateStack.push_back(inFlowSeqOtherElement);
632 unsigned Output::beginFlowSequence() {
633 StateStack.push_back(inFlowSeqFirstElement);
634 newLineCheck();
635 ColumnAtFlowStart = Column;
636 output("[ ");
637 NeedFlowSequenceComma = false;
638 return 0;
641 void Output::endFlowSequence() {
642 StateStack.pop_back();
643 outputUpToEndOfLine(" ]");
646 bool Output::preflightFlowElement(unsigned, void *&SaveInfo) {
647 if (NeedFlowSequenceComma)
648 output(", ");
649 if (WrapColumn && Column > WrapColumn) {
650 output("\n");
651 for (int i = 0; i < ColumnAtFlowStart; ++i)
652 output(" ");
653 Column = ColumnAtFlowStart;
654 output(" ");
656 SaveInfo = nullptr;
657 return true;
660 void Output::postflightFlowElement(void *) {
661 NeedFlowSequenceComma = true;
664 void Output::beginEnumScalar() {
665 EnumerationMatchFound = false;
668 bool Output::matchEnumScalar(const char *Str, bool Match) {
669 if (Match && !EnumerationMatchFound) {
670 newLineCheck();
671 outputUpToEndOfLine(Str);
672 EnumerationMatchFound = true;
674 return false;
677 bool Output::matchEnumFallback() {
678 if (EnumerationMatchFound)
679 return false;
680 EnumerationMatchFound = true;
681 return true;
684 void Output::endEnumScalar() {
685 if (!EnumerationMatchFound)
686 llvm_unreachable("bad runtime enum value");
689 bool Output::beginBitSetScalar(bool &DoClear) {
690 newLineCheck();
691 output("[ ");
692 NeedBitValueComma = false;
693 DoClear = false;
694 return true;
697 bool Output::bitSetMatch(const char *Str, bool Matches) {
698 if (Matches) {
699 if (NeedBitValueComma)
700 output(", ");
701 output(Str);
702 NeedBitValueComma = true;
704 return false;
707 void Output::endBitSetScalar() {
708 outputUpToEndOfLine(" ]");
711 void Output::scalarString(StringRef &S, QuotingType MustQuote) {
712 newLineCheck();
713 if (S.empty()) {
714 // Print '' for the empty string because leaving the field empty is not
715 // allowed.
716 outputUpToEndOfLine("''");
717 return;
719 if (MustQuote == QuotingType::None) {
720 // Only quote if we must.
721 outputUpToEndOfLine(S);
722 return;
725 const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
726 output(Quote); // Starting quote.
728 // When using double-quoted strings (and only in that case), non-printable characters may be
729 // present, and will be escaped using a variety of unicode-scalar and special short-form
730 // escapes. This is handled in yaml::escape.
731 if (MustQuote == QuotingType::Double) {
732 output(yaml::escape(S, /* EscapePrintable= */ false));
733 outputUpToEndOfLine(Quote);
734 return;
737 unsigned i = 0;
738 unsigned j = 0;
739 unsigned End = S.size();
740 const char *Base = S.data();
742 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
743 while (j < End) {
744 if (S[j] == '\'') { // Escape quotes.
745 output(StringRef(&Base[i], j - i)); // "flush".
746 output(StringLiteral("''")); // Print it as ''
747 i = j + 1;
749 ++j;
751 output(StringRef(&Base[i], j - i));
752 outputUpToEndOfLine(Quote); // Ending quote.
755 void Output::blockScalarString(StringRef &S) {
756 if (!StateStack.empty())
757 newLineCheck();
758 output(" |");
759 outputNewLine();
761 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
763 auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
764 for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
765 for (unsigned I = 0; I < Indent; ++I) {
766 output(" ");
768 output(*Lines);
769 outputNewLine();
773 void Output::scalarTag(std::string &Tag) {
774 if (Tag.empty())
775 return;
776 newLineCheck();
777 output(Tag);
778 output(" ");
781 void Output::setError(const Twine &message) {
784 bool Output::canElideEmptySequence() {
785 // Normally, with an optional key/value where the value is an empty sequence,
786 // the whole key/value can be not written. But, that produces wrong yaml
787 // if the key/value is the only thing in the map and the map is used in
788 // a sequence. This detects if the this sequence is the first key/value
789 // in map that itself is embedded in a sequence.
790 if (StateStack.size() < 2)
791 return true;
792 if (StateStack.back() != inMapFirstKey)
793 return true;
794 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
797 void Output::output(StringRef s) {
798 Column += s.size();
799 Out << s;
802 void Output::outputUpToEndOfLine(StringRef s) {
803 output(s);
804 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
805 !inFlowMapAnyKey(StateStack.back())))
806 Padding = "\n";
809 void Output::outputNewLine() {
810 Out << "\n";
811 Column = 0;
814 // if seq at top, indent as if map, then add "- "
815 // if seq in middle, use "- " if firstKey, else use " "
818 void Output::newLineCheck(bool EmptySequence) {
819 if (Padding != "\n") {
820 output(Padding);
821 Padding = {};
822 return;
824 outputNewLine();
825 Padding = {};
827 if (StateStack.size() == 0 || EmptySequence)
828 return;
830 unsigned Indent = StateStack.size() - 1;
831 bool OutputDash = false;
833 if (StateStack.back() == inSeqFirstElement ||
834 StateStack.back() == inSeqOtherElement) {
835 OutputDash = true;
836 } else if ((StateStack.size() > 1) &&
837 ((StateStack.back() == inMapFirstKey) ||
838 inFlowSeqAnyElement(StateStack.back()) ||
839 (StateStack.back() == inFlowMapFirstKey)) &&
840 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
841 --Indent;
842 OutputDash = true;
845 for (unsigned i = 0; i < Indent; ++i) {
846 output(" ");
848 if (OutputDash) {
849 output("- ");
853 void Output::paddedKey(StringRef key) {
854 output(key);
855 output(":");
856 const char *spaces = " ";
857 if (key.size() < strlen(spaces))
858 Padding = &spaces[key.size()];
859 else
860 Padding = " ";
863 void Output::flowKey(StringRef Key) {
864 if (StateStack.back() == inFlowMapOtherKey)
865 output(", ");
866 if (WrapColumn && Column > WrapColumn) {
867 output("\n");
868 for (int I = 0; I < ColumnAtMapFlowStart; ++I)
869 output(" ");
870 Column = ColumnAtMapFlowStart;
871 output(" ");
873 output(Key);
874 output(": ");
877 NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); }
879 bool Output::inSeqAnyElement(InState State) {
880 return State == inSeqFirstElement || State == inSeqOtherElement;
883 bool Output::inFlowSeqAnyElement(InState State) {
884 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
887 bool Output::inMapAnyKey(InState State) {
888 return State == inMapFirstKey || State == inMapOtherKey;
891 bool Output::inFlowMapAnyKey(InState State) {
892 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
895 //===----------------------------------------------------------------------===//
896 // traits for built-in types
897 //===----------------------------------------------------------------------===//
899 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
900 Out << (Val ? "true" : "false");
903 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
904 if (std::optional<bool> Parsed = parseBool(Scalar)) {
905 Val = *Parsed;
906 return StringRef();
908 return "invalid boolean";
911 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
912 raw_ostream &Out) {
913 Out << Val;
916 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
917 StringRef &Val) {
918 Val = Scalar;
919 return StringRef();
922 void ScalarTraits<std::string>::output(const std::string &Val, void *,
923 raw_ostream &Out) {
924 Out << Val;
927 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
928 std::string &Val) {
929 Val = Scalar.str();
930 return StringRef();
933 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
934 raw_ostream &Out) {
935 // use temp uin32_t because ostream thinks uint8_t is a character
936 uint32_t Num = Val;
937 Out << Num;
940 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
941 unsigned long long n;
942 if (getAsUnsignedInteger(Scalar, 0, n))
943 return "invalid number";
944 if (n > 0xFF)
945 return "out of range number";
946 Val = n;
947 return StringRef();
950 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
951 raw_ostream &Out) {
952 Out << Val;
955 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
956 uint16_t &Val) {
957 unsigned long long n;
958 if (getAsUnsignedInteger(Scalar, 0, n))
959 return "invalid number";
960 if (n > 0xFFFF)
961 return "out of range number";
962 Val = n;
963 return StringRef();
966 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
967 raw_ostream &Out) {
968 Out << Val;
971 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
972 uint32_t &Val) {
973 unsigned long long n;
974 if (getAsUnsignedInteger(Scalar, 0, n))
975 return "invalid number";
976 if (n > 0xFFFFFFFFUL)
977 return "out of range number";
978 Val = n;
979 return StringRef();
982 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
983 raw_ostream &Out) {
984 Out << Val;
987 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
988 uint64_t &Val) {
989 unsigned long long N;
990 if (getAsUnsignedInteger(Scalar, 0, N))
991 return "invalid number";
992 Val = N;
993 return StringRef();
996 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
997 // use temp in32_t because ostream thinks int8_t is a character
998 int32_t Num = Val;
999 Out << Num;
1002 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
1003 long long N;
1004 if (getAsSignedInteger(Scalar, 0, N))
1005 return "invalid number";
1006 if ((N > 127) || (N < -128))
1007 return "out of range number";
1008 Val = N;
1009 return StringRef();
1012 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
1013 raw_ostream &Out) {
1014 Out << Val;
1017 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
1018 long long N;
1019 if (getAsSignedInteger(Scalar, 0, N))
1020 return "invalid number";
1021 if ((N > INT16_MAX) || (N < INT16_MIN))
1022 return "out of range number";
1023 Val = N;
1024 return StringRef();
1027 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
1028 raw_ostream &Out) {
1029 Out << Val;
1032 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
1033 long long N;
1034 if (getAsSignedInteger(Scalar, 0, N))
1035 return "invalid number";
1036 if ((N > INT32_MAX) || (N < INT32_MIN))
1037 return "out of range number";
1038 Val = N;
1039 return StringRef();
1042 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
1043 raw_ostream &Out) {
1044 Out << Val;
1047 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
1048 long long N;
1049 if (getAsSignedInteger(Scalar, 0, N))
1050 return "invalid number";
1051 Val = N;
1052 return StringRef();
1055 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
1056 Out << format("%g", Val);
1059 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
1060 if (to_float(Scalar, Val))
1061 return StringRef();
1062 return "invalid floating point number";
1065 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
1066 Out << format("%g", Val);
1069 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
1070 if (to_float(Scalar, Val))
1071 return StringRef();
1072 return "invalid floating point number";
1075 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
1076 Out << format("0x%" PRIX8, (uint8_t)Val);
1079 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
1080 unsigned long long n;
1081 if (getAsUnsignedInteger(Scalar, 0, n))
1082 return "invalid hex8 number";
1083 if (n > 0xFF)
1084 return "out of range hex8 number";
1085 Val = n;
1086 return StringRef();
1089 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
1090 Out << format("0x%" PRIX16, (uint16_t)Val);
1093 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
1094 unsigned long long n;
1095 if (getAsUnsignedInteger(Scalar, 0, n))
1096 return "invalid hex16 number";
1097 if (n > 0xFFFF)
1098 return "out of range hex16 number";
1099 Val = n;
1100 return StringRef();
1103 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
1104 Out << format("0x%" PRIX32, (uint32_t)Val);
1107 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
1108 unsigned long long n;
1109 if (getAsUnsignedInteger(Scalar, 0, n))
1110 return "invalid hex32 number";
1111 if (n > 0xFFFFFFFFUL)
1112 return "out of range hex32 number";
1113 Val = n;
1114 return StringRef();
1117 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
1118 Out << format("0x%" PRIX64, (uint64_t)Val);
1121 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
1122 unsigned long long Num;
1123 if (getAsUnsignedInteger(Scalar, 0, Num))
1124 return "invalid hex64 number";
1125 Val = Num;
1126 return StringRef();
1129 void ScalarTraits<VersionTuple>::output(const VersionTuple &Val, void *,
1130 llvm::raw_ostream &Out) {
1131 Out << Val.getAsString();
1134 StringRef ScalarTraits<VersionTuple>::input(StringRef Scalar, void *,
1135 VersionTuple &Val) {
1136 if (Val.tryParse(Scalar))
1137 return "invalid version format";
1138 return StringRef();