38IO::IO(
void *Context) : Ctxt(Context) {}
42void *IO::getContext()
const {
46void IO::setContext(
void *Context) {
50void IO::setAllowUnknownKeys(
bool Allow) {
58Input::Input(
StringRef InputContent,
void *Ctxt,
63 DocIterator = Strm->begin();
71 DocIterator = Strm->begin();
74Input::~Input() =
default;
76std::error_code Input::error() {
return EC; }
78bool Input::outputting()
const {
82bool Input::setCurrentDocument() {
83 if (DocIterator != Strm->end()) {
84 Node *
N = DocIterator->getRoot();
90 if (isa<NullNode>(
N)) {
93 return setCurrentDocument();
95 releaseHNodeBuffers();
96 TopNode = createHNodes(
N);
97 CurrentNode = TopNode;
103bool Input::nextDocument() {
104 return ++DocIterator != Strm->end();
107const Node *Input::getCurrentNode()
const {
108 return CurrentNode ? CurrentNode->_node :
nullptr;
117 std::string foundTag = CurrentNode->_node->getVerbatimTag();
118 if (foundTag.empty()) {
123 return Tag == foundTag;
126void Input::beginMapping() {
130 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
132 MN->ValidKeys.clear();
136std::vector<StringRef> Input::keys() {
137 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
138 std::vector<StringRef>
Ret;
140 setError(CurrentNode,
"not a mapping");
143 for (
auto &
P : MN->Mapping)
144 Ret.push_back(
P.first());
148bool Input::preflightKey(
const char *Key,
bool Required,
bool,
bool &UseDefault,
164 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
166 if (Required || !isa<EmptyHNode>(CurrentNode))
167 setError(CurrentNode,
"not a mapping");
172 MN->ValidKeys.push_back(Key);
173 HNode *
Value = MN->Mapping[
Key].first;
176 setError(CurrentNode,
Twine(
"missing required key '") + Key +
"'");
181 SaveInfo = CurrentNode;
186void Input::postflightKey(
void *saveInfo) {
187 CurrentNode =
reinterpret_cast<HNode *
>(saveInfo);
190void Input::endMapping() {
194 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
197 for (
const auto &NN : MN->Mapping) {
199 const SMRange &ReportLoc = NN.second.second;
200 if (!AllowUnknownKeys) {
201 setError(ReportLoc,
Twine(
"unknown key '") + NN.first() +
"'");
204 reportWarning(ReportLoc,
Twine(
"unknown key '") + NN.first() +
"'");
209void Input::beginFlowMapping() { beginMapping(); }
211void Input::endFlowMapping() { endMapping(); }
213unsigned Input::beginSequence() {
214 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
215 return SQ->Entries.size();
216 if (isa<EmptyHNode>(CurrentNode))
219 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
220 if (isNull(SN->value()))
224 setError(CurrentNode,
"not a sequence");
228void Input::endSequence() {
231bool Input::preflightElement(
unsigned Index,
void *&SaveInfo) {
234 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
235 SaveInfo = CurrentNode;
236 CurrentNode = SQ->Entries[
Index];
242void Input::postflightElement(
void *SaveInfo) {
243 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
246unsigned Input::beginFlowSequence() {
return beginSequence(); }
248bool Input::preflightFlowElement(
unsigned index,
void *&SaveInfo) {
251 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
252 SaveInfo = CurrentNode;
253 CurrentNode = SQ->Entries[index];
259void Input::postflightFlowElement(
void *SaveInfo) {
260 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
263void Input::endFlowSequence() {
266void Input::beginEnumScalar() {
267 ScalarMatchFound =
false;
270bool Input::matchEnumScalar(
const char *Str,
bool) {
271 if (ScalarMatchFound)
273 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
274 if (SN->value() == Str) {
275 ScalarMatchFound =
true;
282bool Input::matchEnumFallback() {
283 if (ScalarMatchFound)
285 ScalarMatchFound =
true;
289void Input::endEnumScalar() {
290 if (!ScalarMatchFound) {
291 setError(CurrentNode,
"unknown enumerated scalar");
295bool Input::beginBitSetScalar(
bool &DoClear) {
296 BitValuesUsed.clear();
297 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
298 BitValuesUsed.resize(SQ->Entries.size());
300 setError(CurrentNode,
"expected sequence of bit values");
306bool Input::bitSetMatch(
const char *Str,
bool) {
309 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
311 for (
auto &
N : SQ->Entries) {
312 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(
N)) {
313 if (SN->value() == Str) {
314 BitValuesUsed[
Index] =
true;
318 setError(CurrentNode,
"unexpected scalar in sequence of bit values");
323 setError(CurrentNode,
"expected sequence of bit values");
328void Input::endBitSetScalar() {
331 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
332 assert(BitValuesUsed.size() == SQ->Entries.size());
333 for (
unsigned i = 0; i < SQ->Entries.size(); ++i) {
334 if (!BitValuesUsed[i]) {
335 setError(SQ->Entries[i],
"unknown bit value");
342void Input::scalarString(
StringRef &S, QuotingType) {
343 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
346 setError(CurrentNode,
"unexpected scalar");
350void Input::blockScalarString(
StringRef &S) { scalarString(S, QuotingType::None); }
352void Input::scalarTag(std::string &
Tag) {
353 Tag = CurrentNode->_node->getVerbatimTag();
356void Input::setError(HNode *hnode,
const Twine &message) {
357 assert(hnode &&
"HNode must not be NULL");
358 setError(hnode->_node, message);
362 if (isa<ScalarHNode>(CurrentNode))
363 return NodeKind::Scalar;
364 else if (isa<MapHNode>(CurrentNode))
365 return NodeKind::Map;
366 else if (isa<SequenceHNode>(CurrentNode))
367 return NodeKind::Sequence;
371void Input::setError(
Node *node,
const Twine &message) {
372 Strm->printError(node, message);
376void Input::setError(
const SMRange &range,
const Twine &message) {
377 Strm->printError(range, message);
381void Input::reportWarning(HNode *hnode,
const Twine &message) {
382 assert(hnode &&
"HNode must not be NULL");
386void Input::reportWarning(
Node *node,
const Twine &message) {
390void Input::reportWarning(
const SMRange &range,
const Twine &message) {
394void Input::releaseHNodeBuffers() {
395 EmptyHNodeAllocator.DestroyAll();
396 ScalarHNodeAllocator.DestroyAll();
397 SequenceHNodeAllocator.DestroyAll();
398 MapHNodeAllocator.DestroyAll();
401Input::HNode *Input::createHNodes(
Node *
N) {
403 switch (
N->getType()) {
404 case Node::NK_Scalar: {
407 if (!StringStorage.
empty()) {
409 KeyStr = StringStorage.
str().
copy(StringAllocator);
411 return new (ScalarHNodeAllocator.Allocate()) ScalarHNode(
N, KeyStr);
413 case Node::NK_BlockScalar: {
416 return new (ScalarHNodeAllocator.Allocate()) ScalarHNode(
N, ValueCopy);
418 case Node::NK_Sequence: {
420 auto SQHNode =
new (SequenceHNodeAllocator.Allocate()) SequenceHNode(
N);
421 for (
Node &SN : *SQ) {
422 auto Entry = createHNodes(&SN);
425 SQHNode->Entries.push_back(Entry);
429 case Node::NK_Mapping: {
431 auto mapHNode =
new (MapHNodeAllocator.Allocate()) MapHNode(
N);
433 Node *KeyNode = KVN.getKey();
436 if (!Key || !
Value) {
438 setError(KeyNode,
"Map key must be a scalar");
440 setError(KeyNode,
"Map value must not be empty");
443 StringStorage.
clear();
445 if (!StringStorage.
empty()) {
447 KeyStr = StringStorage.
str().
copy(StringAllocator);
449 if (mapHNode->Mapping.count(KeyStr))
453 setError(KeyNode,
Twine(
"duplicated mapping key '") + KeyStr +
"'");
454 auto ValueHNode = createHNodes(
Value);
457 mapHNode->Mapping[KeyStr] =
458 std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
460 return std::move(mapHNode);
463 return new (EmptyHNodeAllocator.Allocate()) EmptyHNode(
N);
465 setError(
N,
"unknown node kind");
470void Input::setError(
const Twine &Message) {
471 setError(CurrentNode, Message);
474void Input::setAllowUnknownKeys(
bool Allow) { AllowUnknownKeys =
Allow; }
476bool Input::canElideEmptySequence() {
484Output::Output(
raw_ostream &yout,
void *context,
int WrapColumn)
485 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
487Output::~Output() =
default;
489bool Output::outputting()
const {
493void Output::beginMapping() {
494 StateStack.push_back(inMapFirstKey);
495 PaddingBeforeContainer =
Padding;
504 bool SequenceElement =
false;
505 if (StateStack.size() > 1) {
506 auto &E = StateStack[StateStack.size() - 2];
507 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
509 if (SequenceElement && StateStack.back() == inMapFirstKey) {
515 if (SequenceElement) {
518 if (StateStack.back() == inMapFirstKey) {
519 StateStack.pop_back();
520 StateStack.push_back(inMapOtherKey);
530void Output::endMapping() {
532 if (StateStack.back() == inMapFirstKey) {
533 Padding = PaddingBeforeContainer;
538 StateStack.pop_back();
541std::vector<StringRef> Output::keys() {
545bool Output::preflightKey(
const char *Key,
bool Required,
bool SameAsDefault,
546 bool &UseDefault,
void *&SaveInfo) {
549 if (Required || !SameAsDefault || WriteDefaultValues) {
550 auto State = StateStack.back();
551 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
562void Output::postflightKey(
void *) {
563 if (StateStack.back() == inMapFirstKey) {
564 StateStack.pop_back();
565 StateStack.push_back(inMapOtherKey);
566 }
else if (StateStack.back() == inFlowMapFirstKey) {
567 StateStack.pop_back();
568 StateStack.push_back(inFlowMapOtherKey);
572void Output::beginFlowMapping() {
573 StateStack.push_back(inFlowMapFirstKey);
575 ColumnAtMapFlowStart = Column;
579void Output::endFlowMapping() {
580 StateStack.pop_back();
581 outputUpToEndOfLine(
" }");
584void Output::beginDocuments() {
585 outputUpToEndOfLine(
"---");
588bool Output::preflightDocument(
unsigned index) {
590 outputUpToEndOfLine(
"\n---");
594void Output::postflightDocument() {
597void Output::endDocuments() {
601unsigned Output::beginSequence() {
602 StateStack.push_back(inSeqFirstElement);
603 PaddingBeforeContainer =
Padding;
608void Output::endSequence() {
610 if (StateStack.back() == inSeqFirstElement) {
611 Padding = PaddingBeforeContainer;
616 StateStack.pop_back();
619bool Output::preflightElement(
unsigned,
void *&SaveInfo) {
624void Output::postflightElement(
void *) {
625 if (StateStack.back() == inSeqFirstElement) {
626 StateStack.pop_back();
627 StateStack.push_back(inSeqOtherElement);
628 }
else if (StateStack.back() == inFlowSeqFirstElement) {
629 StateStack.pop_back();
630 StateStack.push_back(inFlowSeqOtherElement);
634unsigned Output::beginFlowSequence() {
635 StateStack.push_back(inFlowSeqFirstElement);
637 ColumnAtFlowStart = Column;
639 NeedFlowSequenceComma =
false;
643void Output::endFlowSequence() {
644 StateStack.pop_back();
645 outputUpToEndOfLine(
" ]");
648bool Output::preflightFlowElement(
unsigned,
void *&SaveInfo) {
649 if (NeedFlowSequenceComma)
651 if (WrapColumn && Column > WrapColumn) {
653 for (
int i = 0; i < ColumnAtFlowStart; ++i)
655 Column = ColumnAtFlowStart;
662void Output::postflightFlowElement(
void *) {
663 NeedFlowSequenceComma =
true;
666void Output::beginEnumScalar() {
667 EnumerationMatchFound =
false;
670bool Output::matchEnumScalar(
const char *Str,
bool Match) {
671 if (
Match && !EnumerationMatchFound) {
673 outputUpToEndOfLine(Str);
674 EnumerationMatchFound =
true;
679bool Output::matchEnumFallback() {
680 if (EnumerationMatchFound)
682 EnumerationMatchFound =
true;
686void Output::endEnumScalar() {
687 if (!EnumerationMatchFound)
691bool Output::beginBitSetScalar(
bool &DoClear) {
694 NeedBitValueComma =
false;
699bool Output::bitSetMatch(
const char *Str,
bool Matches) {
701 if (NeedBitValueComma)
704 NeedBitValueComma =
true;
709void Output::endBitSetScalar() {
710 outputUpToEndOfLine(
" ]");
713void Output::scalarString(
StringRef &S, QuotingType MustQuote) {
718 outputUpToEndOfLine(
"''");
721 output(S, MustQuote);
722 outputUpToEndOfLine(
"");
725void Output::blockScalarString(
StringRef &S) {
726 if (!StateStack.empty())
731 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
735 for (
unsigned I = 0;
I < Indent; ++
I) {
743void Output::scalarTag(std::string &
Tag) {
751void Output::setError(
const Twine &message) {
754bool Output::canElideEmptySequence() {
760 if (StateStack.size() < 2)
762 if (StateStack.back() != inMapFirstKey)
764 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
772void Output::output(
StringRef S, QuotingType MustQuote) {
773 if (MustQuote == QuotingType::None) {
787 if (MustQuote == QuotingType::Double) {
812void Output::outputUpToEndOfLine(
StringRef s) {
814 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
815 !inFlowMapAnyKey(StateStack.back())))
819void Output::outputNewLine() {
828void Output::newLineCheck(
bool EmptySequence) {
829 if (Padding !=
"\n") {
837 if (StateStack.size() == 0 || EmptySequence)
840 unsigned Indent = StateStack.size() - 1;
841 bool OutputDash =
false;
843 if (StateStack.back() == inSeqFirstElement ||
844 StateStack.back() == inSeqOtherElement) {
846 }
else if ((StateStack.size() > 1) &&
847 ((StateStack.back() == inMapFirstKey) ||
848 inFlowSeqAnyElement(StateStack.back()) ||
849 (StateStack.back() == inFlowMapFirstKey)) &&
850 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
855 for (
unsigned i = 0; i < Indent; ++i) {
864 output(key, needsQuotes(key,
false));
874 if (StateStack.back() == inFlowMapOtherKey)
876 if (WrapColumn && Column > WrapColumn) {
878 for (
int I = 0;
I < ColumnAtMapFlowStart; ++
I)
880 Column = ColumnAtMapFlowStart;
883 output(Key, needsQuotes(Key,
false));
889bool Output::inSeqAnyElement(InState State) {
890 return State == inSeqFirstElement || State == inSeqOtherElement;
893bool Output::inFlowSeqAnyElement(InState State) {
894 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
897bool Output::inMapAnyKey(InState State) {
898 return State == inMapFirstKey || State == inMapOtherKey;
901bool Output::inFlowMapAnyKey(InState State) {
902 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
909void ScalarTraits<bool>::output(
const bool &Val,
void *,
raw_ostream &Out) {
910 Out << (Val ?
"true" :
"false");
914 if (std::optional<bool> Parsed =
parseBool(Scalar)) {
918 return "invalid boolean";
921void ScalarTraits<StringRef>::output(
const StringRef &Val,
void *,
932void ScalarTraits<std::string>::output(
const std::string &Val,
void *,
943void ScalarTraits<uint8_t>::output(
const uint8_t &Val,
void *,
951 unsigned long long n;
953 return "invalid number";
955 return "out of range number";
960void ScalarTraits<uint16_t>::output(
const uint16_t &Val,
void *,
967 unsigned long long n;
969 return "invalid number";
971 return "out of range number";
976void ScalarTraits<uint32_t>::output(
const uint32_t &Val,
void *,
983 unsigned long long n;
985 return "invalid number";
986 if (n > 0xFFFFFFFFUL)
987 return "out of range number";
992void ScalarTraits<uint64_t>::output(
const uint64_t &Val,
void *,
999 unsigned long long N;
1001 return "invalid number";
1006void ScalarTraits<int8_t>::output(
const int8_t &Val,
void *,
raw_ostream &Out) {
1015 return "invalid number";
1016 if ((
N > 127) || (
N < -128))
1017 return "out of range number";
1022void ScalarTraits<int16_t>::output(
const int16_t &Val,
void *,
1030 return "invalid number";
1031 if ((
N > INT16_MAX) || (
N < INT16_MIN))
1032 return "out of range number";
1037void ScalarTraits<int32_t>::output(
const int32_t &Val,
void *,
1045 return "invalid number";
1046 if ((
N > INT32_MAX) || (
N < INT32_MIN))
1047 return "out of range number";
1052void ScalarTraits<int64_t>::output(
const int64_t &Val,
void *,
1060 return "invalid number";
1065void ScalarTraits<double>::output(
const double &Val,
void *,
raw_ostream &Out) {
1066 Out <<
format(
"%g", Val);
1070 if (to_float(Scalar, Val))
1072 return "invalid floating point number";
1075void ScalarTraits<float>::output(
const float &Val,
void *,
raw_ostream &Out) {
1076 Out <<
format(
"%g", Val);
1080 if (to_float(Scalar, Val))
1082 return "invalid floating point number";
1085void ScalarTraits<Hex8>::output(
const Hex8 &Val,
void *,
raw_ostream &Out) {
1086 Out <<
format(
"0x%" PRIX8, (uint8_t)Val);
1090 unsigned long long n;
1092 return "invalid hex8 number";
1094 return "out of range hex8 number";
1099void ScalarTraits<Hex16>::output(
const Hex16 &Val,
void *,
raw_ostream &Out) {
1104 unsigned long long n;
1106 return "invalid hex16 number";
1108 return "out of range hex16 number";
1113void ScalarTraits<Hex32>::output(
const Hex32 &Val,
void *,
raw_ostream &Out) {
1118 unsigned long long n;
1120 return "invalid hex32 number";
1121 if (n > 0xFFFFFFFFUL)
1122 return "out of range hex32 number";
1127void ScalarTraits<Hex64>::output(
const Hex64 &Val,
void *,
raw_ostream &Out) {
1132 unsigned long long Num;
1134 return "invalid hex64 number";
1139void ScalarTraits<VersionTuple>::output(
const VersionTuple &Val,
void *,
1147 return "invalid version format";
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
Defines the llvm::VersionTuple class, which represents a version in the form major[....
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Represents a range in source code.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef str() const
Explicit conversion to StringRef.
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringRef copy(Allocator &A) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
Represents a version number in the form major[.minor[.subminor[.build]]].
bool tryParse(StringRef string)
Try to parse the given string as a version number.
std::string getAsString() const
Retrieve a string representation of the version number.
A forward iterator which reads text lines from a buffer.
This class implements an extremely fast bulk output stream that can only output to a stream.
A block scalar node is an opaque datum that can be presented as a series of zero or more Unicode scal...
StringRef getValue() const
Gets the value of this node as a StringRef.
Represents a YAML map created from either a block map for a flow map.
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
StringRef getValue(SmallVectorImpl< char > &Storage) const
Gets the value of this node as a StringRef.
Represents a YAML sequence created from either a block sequence for a flow sequence.
This class represents a YAML stream potentially containing multiple documents.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::optional< bool > parseBool(StringRef S)
Parse S as a bool according to https://yaml.org/type/bool.html.
std::string escape(StringRef Input, bool EscapePrintable=true)
Escape Input for a double quoted scalar; if EscapePrintable is true, all UTF8 sequences will be escap...
This is an optimization pass for GlobalISel generic memory operations.
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
std::error_code make_error_code(BitcodeError E)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
Determine the kind of a node from its type.