37IO::IO(
void *Context) : Ctxt(Context) {}
41void *IO::getContext()
const {
45void IO::setContext(
void *Context) {
49void IO::setAllowUnknownKeys(
bool Allow) {
57Input::Input(
StringRef InputContent,
void *Ctxt,
62 DocIterator = Strm->begin();
70 DocIterator = Strm->begin();
73Input::~Input() =
default;
75std::error_code Input::error() {
return EC; }
77bool Input::outputting()
const {
81bool Input::setCurrentDocument() {
82 if (DocIterator != Strm->end()) {
83 Node *
N = DocIterator->getRoot();
89 if (isa<NullNode>(
N)) {
92 return setCurrentDocument();
94 releaseHNodeBuffers();
95 TopNode = createHNodes(
N);
96 CurrentNode = TopNode;
102bool Input::nextDocument() {
103 return ++DocIterator != Strm->end();
106const Node *Input::getCurrentNode()
const {
107 return CurrentNode ? CurrentNode->_node :
nullptr;
116 std::string foundTag = CurrentNode->_node->getVerbatimTag();
117 if (foundTag.empty()) {
122 return Tag == foundTag;
125void Input::beginMapping() {
129 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
131 MN->ValidKeys.clear();
135std::vector<StringRef> Input::keys() {
136 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
137 std::vector<StringRef>
Ret;
139 setError(CurrentNode,
"not a mapping");
142 for (
auto &
P : MN->Mapping)
143 Ret.push_back(
P.first());
147bool Input::preflightKey(
const char *Key,
bool Required,
bool,
bool &UseDefault,
163 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
165 if (Required || !isa<EmptyHNode>(CurrentNode))
166 setError(CurrentNode,
"not a mapping");
171 MN->ValidKeys.push_back(Key);
172 HNode *
Value = MN->Mapping[
Key].first;
175 setError(CurrentNode,
Twine(
"missing required key '") + Key +
"'");
180 SaveInfo = CurrentNode;
185void Input::postflightKey(
void *saveInfo) {
186 CurrentNode =
reinterpret_cast<HNode *
>(saveInfo);
189void Input::endMapping() {
193 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
196 for (
const auto &NN : MN->Mapping) {
198 const SMRange &ReportLoc = NN.second.second;
199 if (!AllowUnknownKeys) {
200 setError(ReportLoc,
Twine(
"unknown key '") + NN.first() +
"'");
203 reportWarning(ReportLoc,
Twine(
"unknown key '") + NN.first() +
"'");
208void Input::beginFlowMapping() { beginMapping(); }
210void Input::endFlowMapping() { endMapping(); }
212unsigned Input::beginSequence() {
213 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
214 return SQ->Entries.size();
215 if (isa<EmptyHNode>(CurrentNode))
218 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
219 if (isNull(SN->value()))
223 setError(CurrentNode,
"not a sequence");
227void Input::endSequence() {
230bool Input::preflightElement(
unsigned Index,
void *&SaveInfo) {
233 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
234 SaveInfo = CurrentNode;
235 CurrentNode = SQ->Entries[
Index];
241void Input::postflightElement(
void *SaveInfo) {
242 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
245unsigned Input::beginFlowSequence() {
return beginSequence(); }
247bool Input::preflightFlowElement(
unsigned index,
void *&SaveInfo) {
250 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
251 SaveInfo = CurrentNode;
252 CurrentNode = SQ->Entries[index];
258void Input::postflightFlowElement(
void *SaveInfo) {
259 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
262void Input::endFlowSequence() {
265void Input::beginEnumScalar() {
266 ScalarMatchFound =
false;
269bool Input::matchEnumScalar(
const char *Str,
bool) {
270 if (ScalarMatchFound)
272 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
273 if (SN->value() == Str) {
274 ScalarMatchFound =
true;
281bool Input::matchEnumFallback() {
282 if (ScalarMatchFound)
284 ScalarMatchFound =
true;
288void Input::endEnumScalar() {
289 if (!ScalarMatchFound) {
290 setError(CurrentNode,
"unknown enumerated scalar");
294bool Input::beginBitSetScalar(
bool &DoClear) {
295 BitValuesUsed.clear();
296 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
297 BitValuesUsed.resize(SQ->Entries.size());
299 setError(CurrentNode,
"expected sequence of bit values");
305bool Input::bitSetMatch(
const char *Str,
bool) {
308 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
310 for (
auto &
N : SQ->Entries) {
311 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(
N)) {
312 if (SN->value() == Str) {
313 BitValuesUsed[
Index] =
true;
317 setError(CurrentNode,
"unexpected scalar in sequence of bit values");
322 setError(CurrentNode,
"expected sequence of bit values");
327void Input::endBitSetScalar() {
330 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
331 assert(BitValuesUsed.size() == SQ->Entries.size());
332 for (
unsigned i = 0; i < SQ->Entries.size(); ++i) {
333 if (!BitValuesUsed[i]) {
334 setError(SQ->Entries[i],
"unknown bit value");
341void Input::scalarString(
StringRef &S, QuotingType) {
342 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
345 setError(CurrentNode,
"unexpected scalar");
349void Input::blockScalarString(
StringRef &S) { scalarString(S, QuotingType::None); }
351void Input::scalarTag(std::string &Tag) {
352 Tag = CurrentNode->_node->getVerbatimTag();
355void Input::setError(HNode *hnode,
const Twine &message) {
356 assert(hnode &&
"HNode must not be NULL");
357 setError(hnode->_node, message);
361 if (isa<ScalarHNode>(CurrentNode))
362 return NodeKind::Scalar;
363 else if (isa<MapHNode>(CurrentNode))
364 return NodeKind::Map;
365 else if (isa<SequenceHNode>(CurrentNode))
366 return NodeKind::Sequence;
370void Input::setError(
Node *node,
const Twine &message) {
371 Strm->printError(node, message);
375void Input::setError(
const SMRange &range,
const Twine &message) {
376 Strm->printError(range, message);
380void Input::reportWarning(HNode *hnode,
const Twine &message) {
381 assert(hnode &&
"HNode must not be NULL");
385void Input::reportWarning(
Node *node,
const Twine &message) {
389void Input::reportWarning(
const SMRange &range,
const Twine &message) {
393void Input::releaseHNodeBuffers() {
394 EmptyHNodeAllocator.DestroyAll();
395 ScalarHNodeAllocator.DestroyAll();
396 SequenceHNodeAllocator.DestroyAll();
397 MapHNodeAllocator.DestroyAll();
400Input::HNode *Input::createHNodes(
Node *
N) {
402 switch (
N->getType()) {
403 case Node::NK_Scalar: {
406 if (!StringStorage.
empty()) {
408 KeyStr = StringStorage.
str().
copy(StringAllocator);
410 return new (ScalarHNodeAllocator.Allocate()) ScalarHNode(
N, KeyStr);
412 case Node::NK_BlockScalar: {
415 return new (ScalarHNodeAllocator.Allocate()) ScalarHNode(
N, ValueCopy);
417 case Node::NK_Sequence: {
419 auto SQHNode =
new (SequenceHNodeAllocator.Allocate()) SequenceHNode(
N);
420 for (
Node &SN : *SQ) {
421 auto Entry = createHNodes(&SN);
424 SQHNode->Entries.push_back(Entry);
428 case Node::NK_Mapping: {
430 auto mapHNode =
new (MapHNodeAllocator.Allocate()) MapHNode(
N);
432 Node *KeyNode = KVN.getKey();
435 if (!Key || !
Value) {
437 setError(KeyNode,
"Map key must be a scalar");
439 setError(KeyNode,
"Map value must not be empty");
442 StringStorage.
clear();
444 if (!StringStorage.
empty()) {
446 KeyStr = StringStorage.
str().
copy(StringAllocator);
448 if (mapHNode->Mapping.count(KeyStr))
452 setError(KeyNode,
Twine(
"duplicated mapping key '") + KeyStr +
"'");
453 auto ValueHNode = createHNodes(
Value);
456 mapHNode->Mapping[KeyStr] =
457 std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
459 return std::move(mapHNode);
462 return new (EmptyHNodeAllocator.Allocate()) EmptyHNode(
N);
464 setError(
N,
"unknown node kind");
469void Input::setError(
const Twine &Message) {
470 setError(CurrentNode, Message);
473void Input::setAllowUnknownKeys(
bool Allow) { AllowUnknownKeys =
Allow; }
475bool Input::canElideEmptySequence() {
483Output::Output(
raw_ostream &yout,
void *context,
int WrapColumn)
484 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
486Output::~Output() =
default;
488bool Output::outputting()
const {
492void Output::beginMapping() {
493 StateStack.push_back(inMapFirstKey);
494 PaddingBeforeContainer =
Padding;
503 bool SequenceElement =
false;
504 if (StateStack.size() > 1) {
505 auto &E = StateStack[StateStack.size() - 2];
506 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
508 if (SequenceElement && StateStack.back() == inMapFirstKey) {
514 if (SequenceElement) {
517 if (StateStack.back() == inMapFirstKey) {
518 StateStack.pop_back();
519 StateStack.push_back(inMapOtherKey);
529void Output::endMapping() {
531 if (StateStack.back() == inMapFirstKey) {
532 Padding = PaddingBeforeContainer;
537 StateStack.pop_back();
540std::vector<StringRef> Output::keys() {
544bool Output::preflightKey(
const char *Key,
bool Required,
bool SameAsDefault,
545 bool &UseDefault,
void *&SaveInfo) {
548 if (Required || !SameAsDefault || WriteDefaultValues) {
549 auto State = StateStack.back();
550 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
561void Output::postflightKey(
void *) {
562 if (StateStack.back() == inMapFirstKey) {
563 StateStack.pop_back();
564 StateStack.push_back(inMapOtherKey);
565 }
else if (StateStack.back() == inFlowMapFirstKey) {
566 StateStack.pop_back();
567 StateStack.push_back(inFlowMapOtherKey);
571void Output::beginFlowMapping() {
572 StateStack.push_back(inFlowMapFirstKey);
574 ColumnAtMapFlowStart = Column;
578void Output::endFlowMapping() {
579 StateStack.pop_back();
580 outputUpToEndOfLine(
" }");
583void Output::beginDocuments() {
584 outputUpToEndOfLine(
"---");
587bool Output::preflightDocument(
unsigned index) {
589 outputUpToEndOfLine(
"\n---");
593void Output::postflightDocument() {
596void Output::endDocuments() {
600unsigned Output::beginSequence() {
601 StateStack.push_back(inSeqFirstElement);
602 PaddingBeforeContainer =
Padding;
607void Output::endSequence() {
609 if (StateStack.back() == inSeqFirstElement) {
610 Padding = PaddingBeforeContainer;
615 StateStack.pop_back();
618bool Output::preflightElement(
unsigned,
void *&SaveInfo) {
623void Output::postflightElement(
void *) {
624 if (StateStack.back() == inSeqFirstElement) {
625 StateStack.pop_back();
626 StateStack.push_back(inSeqOtherElement);
627 }
else if (StateStack.back() == inFlowSeqFirstElement) {
628 StateStack.pop_back();
629 StateStack.push_back(inFlowSeqOtherElement);
633unsigned Output::beginFlowSequence() {
634 StateStack.push_back(inFlowSeqFirstElement);
636 ColumnAtFlowStart = Column;
638 NeedFlowSequenceComma =
false;
642void Output::endFlowSequence() {
643 StateStack.pop_back();
644 outputUpToEndOfLine(
" ]");
647bool Output::preflightFlowElement(
unsigned,
void *&SaveInfo) {
648 if (NeedFlowSequenceComma)
650 if (WrapColumn && Column > WrapColumn) {
652 for (
int i = 0; i < ColumnAtFlowStart; ++i)
654 Column = ColumnAtFlowStart;
661void Output::postflightFlowElement(
void *) {
662 NeedFlowSequenceComma =
true;
665void Output::beginEnumScalar() {
666 EnumerationMatchFound =
false;
669bool Output::matchEnumScalar(
const char *Str,
bool Match) {
670 if (
Match && !EnumerationMatchFound) {
672 outputUpToEndOfLine(Str);
673 EnumerationMatchFound =
true;
678bool Output::matchEnumFallback() {
679 if (EnumerationMatchFound)
681 EnumerationMatchFound =
true;
685void Output::endEnumScalar() {
686 if (!EnumerationMatchFound)
690bool Output::beginBitSetScalar(
bool &DoClear) {
693 NeedBitValueComma =
false;
698bool Output::bitSetMatch(
const char *Str,
bool Matches) {
700 if (NeedBitValueComma)
703 NeedBitValueComma =
true;
708void Output::endBitSetScalar() {
709 outputUpToEndOfLine(
" ]");
712void Output::scalarString(
StringRef &S, QuotingType MustQuote) {
717 outputUpToEndOfLine(
"''");
720 output(S, MustQuote);
721 outputUpToEndOfLine(
"");
724void Output::blockScalarString(
StringRef &S) {
725 if (!StateStack.empty())
730 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
734 for (
unsigned I = 0;
I < Indent; ++
I) {
742void Output::scalarTag(std::string &Tag) {
750void Output::setError(
const Twine &message) {
753bool Output::canElideEmptySequence() {
759 if (StateStack.size() < 2)
761 if (StateStack.back() != inMapFirstKey)
763 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
771void Output::output(
StringRef S, QuotingType MustQuote) {
772 if (MustQuote == QuotingType::None) {
786 if (MustQuote == QuotingType::Double) {
811void Output::outputUpToEndOfLine(
StringRef s) {
813 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
814 !inFlowMapAnyKey(StateStack.back())))
818void Output::outputNewLine() {
827void Output::newLineCheck(
bool EmptySequence) {
828 if (Padding !=
"\n") {
836 if (StateStack.size() == 0 || EmptySequence)
839 unsigned Indent = StateStack.size() - 1;
840 bool PossiblyNestedSeq =
false;
841 auto I = StateStack.rbegin(), E = StateStack.rend();
843 if (inSeqAnyElement(*
I)) {
844 PossiblyNestedSeq =
true;
846 }
else if (*
I == inMapFirstKey || *
I == inFlowMapFirstKey ||
847 inFlowSeqAnyElement(*
I)) {
848 PossiblyNestedSeq =
true;
852 unsigned OutputDashCount = 0;
853 if (PossiblyNestedSeq) {
858 if (!inSeqAnyElement(*
I))
864 if (*
I++ != inSeqFirstElement)
869 for (
unsigned I = OutputDashCount;
I < Indent; ++
I)
872 for (
unsigned I = 0;
I < OutputDashCount; ++
I)
877 output(key, needsQuotes(key,
false));
887 if (StateStack.back() == inFlowMapOtherKey)
889 if (WrapColumn && Column > WrapColumn) {
891 for (
int I = 0;
I < ColumnAtMapFlowStart; ++
I)
893 Column = ColumnAtMapFlowStart;
896 output(Key, needsQuotes(Key,
false));
902bool Output::inSeqAnyElement(InState State) {
903 return State == inSeqFirstElement || State == inSeqOtherElement;
906bool Output::inFlowSeqAnyElement(InState State) {
907 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
910bool Output::inMapAnyKey(InState State) {
911 return State == inMapFirstKey || State == inMapOtherKey;
914bool Output::inFlowMapAnyKey(InState State) {
915 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
922void ScalarTraits<bool>::output(
const bool &Val,
void *,
raw_ostream &Out) {
923 Out << (Val ?
"true" :
"false");
927 if (std::optional<bool> Parsed =
parseBool(Scalar)) {
931 return "invalid boolean";
934void ScalarTraits<StringRef>::output(
const StringRef &Val,
void *,
945void ScalarTraits<std::string>::output(
const std::string &Val,
void *,
956void ScalarTraits<uint8_t>::output(
const uint8_t &Val,
void *,
964 unsigned long long n;
966 return "invalid number";
968 return "out of range number";
973void ScalarTraits<uint16_t>::output(
const uint16_t &Val,
void *,
980 unsigned long long n;
982 return "invalid number";
984 return "out of range number";
989void ScalarTraits<uint32_t>::output(
const uint32_t &Val,
void *,
996 unsigned long long n;
998 return "invalid number";
999 if (n > 0xFFFFFFFFUL)
1000 return "out of range number";
1005void ScalarTraits<uint64_t>::output(
const uint64_t &Val,
void *,
1012 unsigned long long N;
1014 return "invalid number";
1019void ScalarTraits<int8_t>::output(
const int8_t &Val,
void *,
raw_ostream &Out) {
1028 return "invalid number";
1029 if ((
N > 127) || (
N < -128))
1030 return "out of range number";
1035void ScalarTraits<int16_t>::output(
const int16_t &Val,
void *,
1043 return "invalid number";
1044 if ((
N > INT16_MAX) || (
N < INT16_MIN))
1045 return "out of range number";
1050void ScalarTraits<int32_t>::output(
const int32_t &Val,
void *,
1058 return "invalid number";
1059 if ((
N > INT32_MAX) || (
N < INT32_MIN))
1060 return "out of range number";
1065void ScalarTraits<int64_t>::output(
const int64_t &Val,
void *,
1073 return "invalid number";
1078void ScalarTraits<double>::output(
const double &Val,
void *,
raw_ostream &Out) {
1079 Out <<
format(
"%g", Val);
1083 if (to_float(Scalar, Val))
1085 return "invalid floating point number";
1088void ScalarTraits<float>::output(
const float &Val,
void *,
raw_ostream &Out) {
1089 Out <<
format(
"%g", Val);
1093 if (to_float(Scalar, Val))
1095 return "invalid floating point number";
1098void ScalarTraits<Hex8>::output(
const Hex8 &Val,
void *,
raw_ostream &Out) {
1103 unsigned long long n;
1105 return "invalid hex8 number";
1107 return "out of range hex8 number";
1112void ScalarTraits<Hex16>::output(
const Hex16 &Val,
void *,
raw_ostream &Out) {
1117 unsigned long long n;
1119 return "invalid hex16 number";
1121 return "out of range hex16 number";
1126void ScalarTraits<Hex32>::output(
const Hex32 &Val,
void *,
raw_ostream &Out) {
1131 unsigned long long n;
1133 return "invalid hex32 number";
1134 if (n > 0xFFFFFFFFUL)
1135 return "out of range hex32 number";
1140void ScalarTraits<Hex64>::output(
const Hex64 &Val,
void *,
raw_ostream &Out) {
1145 unsigned long long Num;
1147 return "invalid hex64 number";
1152void ScalarTraits<VersionTuple>::output(
const VersionTuple &Val,
void *,
1160 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.