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; }
79void Input::HNode::anchor() {}
80void Input::EmptyHNode::anchor() {}
81void Input::ScalarHNode::anchor() {}
82void Input::MapHNode::anchor() {}
83void Input::SequenceHNode::anchor() {}
85bool Input::outputting()
const {
89bool Input::setCurrentDocument() {
90 if (DocIterator != Strm->end()) {
91 Node *
N = DocIterator->getRoot();
97 if (isa<NullNode>(
N)) {
100 return setCurrentDocument();
102 TopNode = createHNodes(
N);
103 CurrentNode = TopNode.get();
109bool Input::nextDocument() {
110 return ++DocIterator != Strm->end();
113const Node *Input::getCurrentNode()
const {
114 return CurrentNode ? CurrentNode->_node :
nullptr;
123 std::string foundTag = CurrentNode->_node->getVerbatimTag();
124 if (foundTag.empty()) {
129 return Tag.equals(foundTag);
132void Input::beginMapping() {
136 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
138 MN->ValidKeys.clear();
142std::vector<StringRef> Input::keys() {
143 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
144 std::vector<StringRef>
Ret;
146 setError(CurrentNode,
"not a mapping");
149 for (
auto &
P : MN->Mapping)
150 Ret.push_back(
P.first());
154bool Input::preflightKey(
const char *Key,
bool Required,
bool,
bool &UseDefault,
168 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
170 if (Required || !isa<EmptyHNode>(CurrentNode))
171 setError(CurrentNode,
"not a mapping");
176 MN->ValidKeys.push_back(Key);
177 HNode *
Value = MN->Mapping[
Key].first.get();
180 setError(CurrentNode,
Twine(
"missing required key '") + Key +
"'");
185 SaveInfo = CurrentNode;
190void Input::postflightKey(
void *saveInfo) {
191 CurrentNode =
reinterpret_cast<HNode *
>(saveInfo);
194void Input::endMapping() {
198 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
201 for (
const auto &NN : MN->Mapping) {
203 const SMRange &ReportLoc = NN.second.second;
204 if (!AllowUnknownKeys) {
205 setError(ReportLoc,
Twine(
"unknown key '") + NN.first() +
"'");
208 reportWarning(ReportLoc,
Twine(
"unknown key '") + NN.first() +
"'");
213void Input::beginFlowMapping() { beginMapping(); }
215void Input::endFlowMapping() { endMapping(); }
217unsigned Input::beginSequence() {
218 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
219 return SQ->Entries.size();
220 if (isa<EmptyHNode>(CurrentNode))
223 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
224 if (isNull(SN->value()))
228 setError(CurrentNode,
"not a sequence");
232void Input::endSequence() {
235bool Input::preflightElement(
unsigned Index,
void *&SaveInfo) {
238 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
239 SaveInfo = CurrentNode;
240 CurrentNode = SQ->Entries[
Index].get();
246void Input::postflightElement(
void *SaveInfo) {
247 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
250unsigned Input::beginFlowSequence() {
return beginSequence(); }
252bool Input::preflightFlowElement(
unsigned index,
void *&SaveInfo) {
255 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
256 SaveInfo = CurrentNode;
257 CurrentNode = SQ->Entries[index].get();
263void Input::postflightFlowElement(
void *SaveInfo) {
264 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
267void Input::endFlowSequence() {
270void Input::beginEnumScalar() {
271 ScalarMatchFound =
false;
274bool Input::matchEnumScalar(
const char *Str,
bool) {
275 if (ScalarMatchFound)
277 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
278 if (SN->value().equals(Str)) {
279 ScalarMatchFound =
true;
286bool Input::matchEnumFallback() {
287 if (ScalarMatchFound)
289 ScalarMatchFound =
true;
293void Input::endEnumScalar() {
294 if (!ScalarMatchFound) {
295 setError(CurrentNode,
"unknown enumerated scalar");
299bool Input::beginBitSetScalar(
bool &DoClear) {
300 BitValuesUsed.clear();
301 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
302 BitValuesUsed.resize(SQ->Entries.size());
304 setError(CurrentNode,
"expected sequence of bit values");
310bool Input::bitSetMatch(
const char *Str,
bool) {
313 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
315 for (
auto &
N : SQ->Entries) {
316 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(
N.get())) {
317 if (SN->value().equals(Str)) {
318 BitValuesUsed[
Index] =
true;
322 setError(CurrentNode,
"unexpected scalar in sequence of bit values");
327 setError(CurrentNode,
"expected sequence of bit values");
332void Input::endBitSetScalar() {
335 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
336 assert(BitValuesUsed.size() == SQ->Entries.size());
337 for (
unsigned i = 0; i < SQ->Entries.size(); ++i) {
338 if (!BitValuesUsed[i]) {
339 setError(SQ->Entries[i].get(),
"unknown bit value");
346void Input::scalarString(
StringRef &S, QuotingType) {
347 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
350 setError(CurrentNode,
"unexpected scalar");
354void Input::blockScalarString(
StringRef &S) { scalarString(S, QuotingType::None); }
356void Input::scalarTag(std::string &
Tag) {
357 Tag = CurrentNode->_node->getVerbatimTag();
360void Input::setError(HNode *hnode,
const Twine &message) {
361 assert(hnode &&
"HNode must not be NULL");
362 setError(hnode->_node, message);
366 if (isa<ScalarHNode>(CurrentNode))
367 return NodeKind::Scalar;
368 else if (isa<MapHNode>(CurrentNode))
369 return NodeKind::Map;
370 else if (isa<SequenceHNode>(CurrentNode))
371 return NodeKind::Sequence;
375void Input::setError(
Node *node,
const Twine &message) {
376 Strm->printError(node, message);
380void Input::setError(
const SMRange &range,
const Twine &message) {
381 Strm->printError(range, message);
385void Input::reportWarning(HNode *hnode,
const Twine &message) {
386 assert(hnode &&
"HNode must not be NULL");
390void Input::reportWarning(
Node *node,
const Twine &message) {
394void Input::reportWarning(
const SMRange &range,
const Twine &message) {
398std::unique_ptr<Input::HNode> Input::createHNodes(
Node *
N) {
401 StringRef KeyStr = SN->getValue(StringStorage);
402 if (!StringStorage.
empty()) {
404 KeyStr = StringStorage.
str().
copy(StringAllocator);
406 return std::make_unique<ScalarHNode>(
N, KeyStr);
409 return std::make_unique<ScalarHNode>(
N, ValueCopy);
411 auto SQHNode = std::make_unique<SequenceHNode>(
N);
412 for (
Node &SN : *SQ) {
413 auto Entry = createHNodes(&SN);
416 SQHNode->Entries.push_back(std::move(Entry));
418 return std::move(SQHNode);
419 }
else if (
MappingNode *Map = dyn_cast<MappingNode>(
N)) {
420 auto mapHNode = std::make_unique<MapHNode>(
N);
422 Node *KeyNode = KVN.getKey();
425 if (!Key || !
Value) {
427 setError(KeyNode,
"Map key must be a scalar");
429 setError(KeyNode,
"Map value must not be empty");
432 StringStorage.
clear();
434 if (!StringStorage.
empty()) {
436 KeyStr = StringStorage.
str().
copy(StringAllocator);
438 if (mapHNode->Mapping.count(KeyStr))
442 setError(KeyNode,
Twine(
"duplicated mapping key '") + KeyStr +
"'");
443 auto ValueHNode = createHNodes(
Value);
446 mapHNode->Mapping[KeyStr] =
447 std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
449 return std::move(mapHNode);
450 }
else if (isa<NullNode>(
N)) {
451 return std::make_unique<EmptyHNode>(
N);
453 setError(
N,
"unknown node kind");
458void Input::setError(
const Twine &Message) {
459 setError(CurrentNode, Message);
462void Input::setAllowUnknownKeys(
bool Allow) { AllowUnknownKeys =
Allow; }
464bool Input::canElideEmptySequence() {
472Output::Output(
raw_ostream &yout,
void *context,
int WrapColumn)
473 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
475Output::~Output() =
default;
477bool Output::outputting()
const {
481void Output::beginMapping() {
482 StateStack.push_back(inMapFirstKey);
483 PaddingBeforeContainer =
Padding;
492 bool SequenceElement =
false;
493 if (StateStack.size() > 1) {
494 auto &
E = StateStack[StateStack.size() - 2];
495 SequenceElement = inSeqAnyElement(
E) || inFlowSeqAnyElement(
E);
497 if (SequenceElement && StateStack.back() == inMapFirstKey) {
503 if (SequenceElement) {
506 if (StateStack.back() == inMapFirstKey) {
507 StateStack.pop_back();
508 StateStack.push_back(inMapOtherKey);
518void Output::endMapping() {
520 if (StateStack.back() == inMapFirstKey) {
521 Padding = PaddingBeforeContainer;
526 StateStack.pop_back();
529std::vector<StringRef> Output::keys() {
533bool Output::preflightKey(
const char *Key,
bool Required,
bool SameAsDefault,
534 bool &UseDefault,
void *&SaveInfo) {
537 if (Required || !SameAsDefault || WriteDefaultValues) {
538 auto State = StateStack.back();
539 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
550void Output::postflightKey(
void *) {
551 if (StateStack.back() == inMapFirstKey) {
552 StateStack.pop_back();
553 StateStack.push_back(inMapOtherKey);
554 }
else if (StateStack.back() == inFlowMapFirstKey) {
555 StateStack.pop_back();
556 StateStack.push_back(inFlowMapOtherKey);
560void Output::beginFlowMapping() {
561 StateStack.push_back(inFlowMapFirstKey);
563 ColumnAtMapFlowStart = Column;
567void Output::endFlowMapping() {
568 StateStack.pop_back();
569 outputUpToEndOfLine(
" }");
572void Output::beginDocuments() {
573 outputUpToEndOfLine(
"---");
576bool Output::preflightDocument(
unsigned index) {
578 outputUpToEndOfLine(
"\n---");
582void Output::postflightDocument() {
585void Output::endDocuments() {
589unsigned Output::beginSequence() {
590 StateStack.push_back(inSeqFirstElement);
591 PaddingBeforeContainer =
Padding;
596void Output::endSequence() {
598 if (StateStack.back() == inSeqFirstElement) {
599 Padding = PaddingBeforeContainer;
604 StateStack.pop_back();
607bool Output::preflightElement(
unsigned,
void *&SaveInfo) {
612void Output::postflightElement(
void *) {
613 if (StateStack.back() == inSeqFirstElement) {
614 StateStack.pop_back();
615 StateStack.push_back(inSeqOtherElement);
616 }
else if (StateStack.back() == inFlowSeqFirstElement) {
617 StateStack.pop_back();
618 StateStack.push_back(inFlowSeqOtherElement);
622unsigned Output::beginFlowSequence() {
623 StateStack.push_back(inFlowSeqFirstElement);
625 ColumnAtFlowStart = Column;
627 NeedFlowSequenceComma =
false;
631void Output::endFlowSequence() {
632 StateStack.pop_back();
633 outputUpToEndOfLine(
" ]");
636bool Output::preflightFlowElement(
unsigned,
void *&SaveInfo) {
637 if (NeedFlowSequenceComma)
639 if (WrapColumn && Column > WrapColumn) {
641 for (
int i = 0; i < ColumnAtFlowStart; ++i)
643 Column = ColumnAtFlowStart;
650void Output::postflightFlowElement(
void *) {
651 NeedFlowSequenceComma =
true;
654void Output::beginEnumScalar() {
655 EnumerationMatchFound =
false;
658bool Output::matchEnumScalar(
const char *Str,
bool Match) {
659 if (
Match && !EnumerationMatchFound) {
661 outputUpToEndOfLine(Str);
662 EnumerationMatchFound =
true;
667bool Output::matchEnumFallback() {
668 if (EnumerationMatchFound)
670 EnumerationMatchFound =
true;
674void Output::endEnumScalar() {
675 if (!EnumerationMatchFound)
679bool Output::beginBitSetScalar(
bool &DoClear) {
682 NeedBitValueComma =
false;
687bool Output::bitSetMatch(
const char *Str,
bool Matches) {
689 if (NeedBitValueComma)
692 NeedBitValueComma =
true;
697void Output::endBitSetScalar() {
698 outputUpToEndOfLine(
" ]");
701void Output::scalarString(
StringRef &S, QuotingType MustQuote) {
706 outputUpToEndOfLine(
"''");
709 if (MustQuote == QuotingType::None) {
711 outputUpToEndOfLine(S);
715 const char *
const Quote = MustQuote == QuotingType::Single ?
"'" :
"\"";
721 if (MustQuote == QuotingType::Double) {
723 outputUpToEndOfLine(Quote);
742 outputUpToEndOfLine(Quote);
745void Output::blockScalarString(
StringRef &S) {
746 if (!StateStack.empty())
751 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
755 for (
unsigned I = 0;
I < Indent; ++
I) {
763void Output::scalarTag(std::string &
Tag) {
771void Output::setError(
const Twine &message) {
774bool Output::canElideEmptySequence() {
780 if (StateStack.size() < 2)
782 if (StateStack.back() != inMapFirstKey)
784 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
792void Output::outputUpToEndOfLine(
StringRef s) {
794 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
795 !inFlowMapAnyKey(StateStack.back())))
799void Output::outputNewLine() {
808void Output::newLineCheck(
bool EmptySequence) {
809 if (Padding !=
"\n") {
817 if (StateStack.size() == 0 || EmptySequence)
820 unsigned Indent = StateStack.size() - 1;
821 bool OutputDash =
false;
823 if (StateStack.back() == inSeqFirstElement ||
824 StateStack.back() == inSeqOtherElement) {
826 }
else if ((StateStack.size() > 1) &&
827 ((StateStack.back() == inMapFirstKey) ||
828 inFlowSeqAnyElement(StateStack.back()) ||
829 (StateStack.back() == inFlowMapFirstKey)) &&
830 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
835 for (
unsigned i = 0; i < Indent; ++i) {
854 if (StateStack.back() == inFlowMapOtherKey)
856 if (WrapColumn && Column > WrapColumn) {
858 for (
int I = 0;
I < ColumnAtMapFlowStart; ++
I)
860 Column = ColumnAtMapFlowStart;
869bool Output::inSeqAnyElement(InState State) {
870 return State == inSeqFirstElement || State == inSeqOtherElement;
873bool Output::inFlowSeqAnyElement(InState State) {
874 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
877bool Output::inMapAnyKey(InState State) {
878 return State == inMapFirstKey || State == inMapOtherKey;
881bool Output::inFlowMapAnyKey(InState State) {
882 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
889void ScalarTraits<bool>::output(
const bool &Val,
void *,
raw_ostream &Out) {
890 Out << (Val ?
"true" :
"false");
894 if (std::optional<bool> Parsed =
parseBool(Scalar)) {
898 return "invalid boolean";
901void ScalarTraits<StringRef>::output(
const StringRef &Val,
void *,
912void ScalarTraits<std::string>::output(
const std::string &Val,
void *,
923void ScalarTraits<uint8_t>::output(
const uint8_t &Val,
void *,
931 unsigned long long n;
933 return "invalid number";
935 return "out of range number";
940void ScalarTraits<uint16_t>::output(
const uint16_t &Val,
void *,
947 unsigned long long n;
949 return "invalid number";
951 return "out of range number";
956void ScalarTraits<uint32_t>::output(
const uint32_t &Val,
void *,
963 unsigned long long n;
965 return "invalid number";
966 if (n > 0xFFFFFFFFUL)
967 return "out of range number";
972void ScalarTraits<uint64_t>::output(
const uint64_t &Val,
void *,
979 unsigned long long N;
981 return "invalid number";
986void ScalarTraits<int8_t>::output(
const int8_t &Val,
void *,
raw_ostream &Out) {
995 return "invalid number";
996 if ((
N > 127) || (
N < -128))
997 return "out of range number";
1002void ScalarTraits<int16_t>::output(
const int16_t &Val,
void *,
1010 return "invalid number";
1011 if ((
N > INT16_MAX) || (
N < INT16_MIN))
1012 return "out of range number";
1017void ScalarTraits<int32_t>::output(
const int32_t &Val,
void *,
1025 return "invalid number";
1026 if ((
N > INT32_MAX) || (
N < INT32_MIN))
1027 return "out of range number";
1032void ScalarTraits<int64_t>::output(
const int64_t &Val,
void *,
1040 return "invalid number";
1045void ScalarTraits<double>::output(
const double &Val,
void *,
raw_ostream &Out) {
1046 Out <<
format(
"%g", Val);
1050 if (to_float(Scalar, Val))
1052 return "invalid floating point number";
1055void ScalarTraits<float>::output(
const float &Val,
void *,
raw_ostream &Out) {
1056 Out <<
format(
"%g", Val);
1060 if (to_float(Scalar, Val))
1062 return "invalid floating point number";
1065void ScalarTraits<Hex8>::output(
const Hex8 &Val,
void *,
raw_ostream &Out) {
1066 Out <<
format(
"0x%" PRIX8, (uint8_t)Val);
1070 unsigned long long n;
1072 return "invalid hex8 number";
1074 return "out of range hex8 number";
1079void ScalarTraits<Hex16>::output(
const Hex16 &Val,
void *,
raw_ostream &Out) {
1084 unsigned long long n;
1086 return "invalid hex16 number";
1088 return "out of range hex16 number";
1093void ScalarTraits<Hex32>::output(
const Hex32 &Val,
void *,
raw_ostream &Out) {
1098 unsigned long long n;
1100 return "invalid hex32 number";
1101 if (n > 0xFFFFFFFFUL)
1102 return "out of range hex32 number";
1107void ScalarTraits<Hex64>::output(
const Hex64 &Val,
void *,
raw_ostream &Out) {
1112 unsigned long long Num;
1114 return "invalid hex64 number";
1119void ScalarTraits<VersionTuple>::output(
const VersionTuple &Val,
void *,
1127 return "invalid version format";
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
StringRef copy(Allocator &A) const
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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...
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...
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.