39IO::IO(
void *Context) : Ctxt(Context) {}
61 :
IO(Ctxt), Strm(new
Stream(InputContent, SrcMgr,
false, &EC)) {
63 SrcMgr.setDiagHandler(
DiagHandler, DiagHandlerCtxt);
64 DocIterator = Strm->begin();
71 SrcMgr.setDiagHandler(
DiagHandler, DiagHandlerCtxt);
72 DocIterator = Strm->begin();
84 if (DocIterator != Strm->end()) {
85 Node *
N = DocIterator->getRoot();
96 releaseHNodeBuffers();
97 TopNode = createHNodes(
N);
98 CurrentNode = TopNode;
105 return ++DocIterator != Strm->end();
109 return CurrentNode ? CurrentNode->_node :
nullptr;
119 if (foundTag.empty()) {
124 return Tag == foundTag;
133 MN->ValidKeys.clear();
139 std::vector<StringRef> Ret;
141 setError(CurrentNode,
"not a mapping");
144 for (
auto &
P : MN->Mapping)
145 Ret.push_back(
P.first());
149bool Input::preflightKey(StringRef
Key,
bool Required,
bool,
bool &UseDefault,
168 setError(CurrentNode,
"not a mapping");
173 MN->ValidKeys.push_back(
Key.str());
174 HNode *
Value = MN->Mapping[
Key].first;
177 setError(CurrentNode, Twine(
"missing required key '") +
Key +
"'");
182 SaveInfo = CurrentNode;
187void Input::postflightKey(
void *saveInfo) {
188 CurrentNode =
reinterpret_cast<HNode *
>(saveInfo);
198 for (
const auto &NN : MN->Mapping) {
200 const SMRange &ReportLoc = NN.second.second;
201 if (!AllowUnknownKeys) {
202 setError(ReportLoc, Twine(
"unknown key '") + NN.first() +
"'");
205 reportWarning(ReportLoc, Twine(
"unknown key '") + NN.first() +
"'");
216 return SQ->Entries.size();
225 setError(CurrentNode,
"not a sequence");
232bool Input::preflightElement(
unsigned Index,
void *&SaveInfo) {
236 SaveInfo = CurrentNode;
237 CurrentNode = SQ->Entries[
Index];
243void Input::postflightElement(
void *SaveInfo) {
244 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
249bool Input::preflightFlowElement(
unsigned index,
void *&SaveInfo) {
253 SaveInfo = CurrentNode;
254 CurrentNode = SQ->Entries[index];
260void Input::postflightFlowElement(
void *SaveInfo) {
261 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
268 ScalarMatchFound =
false;
271bool Input::matchEnumScalar(StringRef Str,
bool) {
272 if (ScalarMatchFound)
275 if (SN->value() == Str) {
276 ScalarMatchFound =
true;
284 if (ScalarMatchFound)
286 ScalarMatchFound =
true;
291 if (!ScalarMatchFound) {
292 setError(CurrentNode,
"unknown enumerated scalar");
296bool Input::beginBitSetScalar(
bool &DoClear) {
297 BitValuesUsed.clear();
299 BitValuesUsed.resize(SQ->Entries.size());
301 setError(CurrentNode,
"expected sequence of bit values");
307bool Input::bitSetMatch(StringRef Str,
bool) {
312 for (
auto &
N : SQ->Entries) {
314 if (SN->value() == Str) {
315 BitValuesUsed[
Index] =
true;
319 setError(CurrentNode,
"unexpected scalar in sequence of bit values");
324 setError(CurrentNode,
"expected sequence of bit values");
333 assert(BitValuesUsed.size() == SQ->Entries.size());
334 for (
unsigned i = 0; i < SQ->Entries.size(); ++i) {
335 if (!BitValuesUsed[i]) {
336 setError(SQ->Entries[i],
"unknown bit value");
343void Input::scalarString(StringRef &S,
QuotingType) {
347 setError(CurrentNode,
"unexpected scalar");
351void Input::blockScalarString(StringRef &S) { scalarString(S,
QuotingType::None); }
353void Input::scalarTag(std::string &
Tag) {
354 Tag = CurrentNode->_node->getVerbatimTag();
357void Input::setError(HNode *hnode,
const Twine &message) {
358 assert(hnode &&
"HNode must not be NULL");
359 setError(hnode->_node, message);
372void Input::setError(
Node *node,
const Twine &message) {
373 Strm->printError(node, message);
377void Input::setError(
const SMRange &range,
const Twine &message) {
378 Strm->printError(range, message);
382void Input::reportWarning(HNode *hnode,
const Twine &message) {
383 assert(hnode &&
"HNode must not be NULL");
387void Input::reportWarning(
Node *node,
const Twine &message) {
391void Input::reportWarning(
const SMRange &range,
const Twine &message) {
395void Input::releaseHNodeBuffers() {
396 EmptyHNodeAllocator.DestroyAll();
397 ScalarHNodeAllocator.DestroyAll();
398 SequenceHNodeAllocator.DestroyAll();
399 MapHNodeAllocator.DestroyAll();
402void Input::saveAliasHNode(
Node *
N, HNode *HN) {
403 StringRef Anchor =
N->getAnchor();
411 AliasMap[Anchor] = HN;
414Input::HNode *Input::createHNodes(
Node *
N) {
415 SmallString<128> StringStorage;
416 switch (
N->getType()) {
419 StringRef KeyStr = SN->
getValue(StringStorage);
420 if (!StringStorage.
empty()) {
422 KeyStr = StringStorage.
str().
copy(StringAllocator);
424 auto *SHNode =
new (ScalarHNodeAllocator.Allocate()) ScalarHNode(
N, KeyStr);
425 saveAliasHNode(SN, SHNode);
430 StringRef ValueCopy = BSN->
getValue().
copy(StringAllocator);
432 new (ScalarHNodeAllocator.Allocate()) ScalarHNode(
N, ValueCopy);
433 saveAliasHNode(BSN, BSHNode);
438 auto SQHNode =
new (SequenceHNodeAllocator.Allocate()) SequenceHNode(
N);
439 for (Node &SN : *SQ) {
440 auto Entry = createHNodes(&SN);
443 SQHNode->Entries.push_back(Entry);
445 saveAliasHNode(SQ, SQHNode);
450 auto mapHNode =
new (MapHNodeAllocator.Allocate()) MapHNode(
N);
451 for (KeyValueNode &KVN : *
Map) {
452 Node *KeyNode = KVN.getKey();
457 setError(KeyNode,
"Map key must be a scalar");
459 setError(KeyNode,
"Map value must not be empty");
462 StringStorage.
clear();
463 StringRef KeyStr =
Key->getValue(StringStorage);
464 if (!StringStorage.
empty()) {
466 KeyStr = StringStorage.
str().
copy(StringAllocator);
468 if (mapHNode->Mapping.count(KeyStr))
472 setError(KeyNode, Twine(
"duplicated mapping key '") + KeyStr +
"'");
473 auto ValueHNode = createHNodes(
Value);
476 mapHNode->Mapping[KeyStr] =
477 std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
479 saveAliasHNode(
Map, mapHNode);
480 return std::move(mapHNode);
485 return new (EmptyHNodeAllocator.Allocate()) EmptyHNode(
N);
488 auto AliasName = AN->
getName();
489 auto AHN = AliasMap.
find(AliasName);
490 if (AHN == AliasMap.end()) {
491 setError(AN, Twine(
"undefined alias '" + AliasName +
"'"));
497 setError(
N,
"unknown node kind");
502void Input::setError(
const Twine &Message) {
503 setError(CurrentNode, Message);
517 :
IO(context), Out(yout), WrapColumn(WrapColumn) {}
526 StateStack.push_back(inMapFirstKey);
527 PaddingBeforeContainer = Padding;
536 bool SequenceElement =
false;
537 if (StateStack.size() > 1) {
538 auto &E = StateStack[StateStack.size() - 2];
539 SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
541 if (SequenceElement && StateStack.back() == inMapFirstKey) {
547 if (SequenceElement) {
550 if (StateStack.back() == inMapFirstKey) {
551 StateStack.pop_back();
552 StateStack.push_back(inMapOtherKey);
564 if (StateStack.back() == inMapFirstKey) {
565 Padding = PaddingBeforeContainer;
570 StateStack.pop_back();
578 bool &UseDefault,
void *&SaveInfo) {
581 if (Required || !SameAsDefault || WriteDefaultValues) {
582 auto State = StateStack.back();
583 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
595 if (StateStack.back() == inMapFirstKey) {
596 StateStack.pop_back();
597 StateStack.push_back(inMapOtherKey);
598 }
else if (StateStack.back() == inFlowMapFirstKey) {
599 StateStack.pop_back();
600 StateStack.push_back(inFlowMapOtherKey);
605 StateStack.push_back(inFlowMapFirstKey);
607 ColumnAtMapFlowStart = Column;
612 StateStack.pop_back();
613 outputUpToEndOfLine(
" }");
617 outputUpToEndOfLine(
"---");
622 outputUpToEndOfLine(
"\n---");
634 StateStack.push_back(inSeqFirstElement);
635 PaddingBeforeContainer = Padding;
642 if (StateStack.back() == inSeqFirstElement) {
643 Padding = PaddingBeforeContainer;
648 StateStack.pop_back();
657 if (StateStack.back() == inSeqFirstElement) {
658 StateStack.pop_back();
659 StateStack.push_back(inSeqOtherElement);
660 }
else if (StateStack.back() == inFlowSeqFirstElement) {
661 StateStack.pop_back();
662 StateStack.push_back(inFlowSeqOtherElement);
667 StateStack.push_back(inFlowSeqFirstElement);
669 ColumnAtFlowStart = Column;
671 NeedFlowSequenceComma =
false;
676 StateStack.pop_back();
677 outputUpToEndOfLine(
" ]");
681 if (NeedFlowSequenceComma)
683 if (WrapColumn && Column > WrapColumn) {
685 for (
int i = 0; i < ColumnAtFlowStart; ++i)
687 Column = ColumnAtFlowStart;
695 NeedFlowSequenceComma =
true;
699 EnumerationMatchFound =
false;
703 if (Match && !EnumerationMatchFound) {
705 outputUpToEndOfLine(Str);
706 EnumerationMatchFound =
true;
712 if (EnumerationMatchFound)
714 EnumerationMatchFound =
true;
719 if (!EnumerationMatchFound)
726 NeedBitValueComma =
false;
733 if (NeedBitValueComma)
736 NeedBitValueComma =
true;
742 outputUpToEndOfLine(
" ]");
750 outputUpToEndOfLine(
"''");
753 output(S, MustQuote);
754 outputUpToEndOfLine(
"");
768 if (!
isPrint(*Ptr) && *Ptr !=
'\t' && *Ptr !=
'\n')
773 const UTF8 *Start = Ptr;
793 if (!StateStack.empty())
802 else if (LineBreaks > 1)
806 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
809 for (
line_iterator Lines(*Buffer,
false); !Lines.is_at_end(); ++Lines) {
811 for (
unsigned I = 0;
I < Indent; ++
I) {
816 outputUpToEndOfLine(
"");
838 if (StateStack.size() < 2)
840 if (StateStack.back() != inMapFirstKey)
842 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
880 output(StringRef(&
Base[i], j - i));
881 output(StringLiteral(
"''"));
886 output(StringRef(&
Base[i], j - i));
890void Output::outputUpToEndOfLine(StringRef s) {
892 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
893 !inFlowMapAnyKey(StateStack.back())))
897void Output::outputNewLine() {
906void Output::newLineCheck(
bool EmptySequence) {
907 if (Padding !=
"\n") {
915 if (StateStack.size() == 0 || EmptySequence)
918 unsigned Indent = StateStack.size() - 1;
919 bool PossiblyNestedSeq =
false;
920 auto I = StateStack.rbegin(),
E = StateStack.rend();
922 if (inSeqAnyElement(*
I)) {
923 PossiblyNestedSeq =
true;
925 }
else if (*
I == inMapFirstKey || *
I == inFlowMapFirstKey ||
926 inFlowSeqAnyElement(*
I)) {
927 PossiblyNestedSeq =
true;
931 unsigned OutputDashCount = 0;
932 if (PossiblyNestedSeq) {
937 if (!inSeqAnyElement(*
I))
943 if (*
I++ != inSeqFirstElement)
948 for (
unsigned I = OutputDashCount;
I < Indent; ++
I)
951 for (
unsigned I = 0;
I < OutputDashCount; ++
I)
955void Output::paddedKey(StringRef key) {
965void Output::flowKey(StringRef
Key) {
966 if (StateStack.back() == inFlowMapOtherKey)
968 if (WrapColumn && Column > WrapColumn) {
970 for (
int I = 0;
I < ColumnAtMapFlowStart; ++
I)
972 Column = ColumnAtMapFlowStart;
981bool Output::inSeqAnyElement(InState State) {
982 return State == inSeqFirstElement || State == inSeqOtherElement;
985bool Output::inFlowSeqAnyElement(InState State) {
986 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
989bool Output::inMapAnyKey(InState State) {
990 return State == inMapFirstKey || State == inMapOtherKey;
993bool Output::inFlowMapAnyKey(InState State) {
994 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
1002 Out << (Val ?
"true" :
"false");
1010 return "invalid boolean";
1043 unsigned long long n;
1045 return "invalid number";
1047 return "out of range number";
1059 unsigned long long n;
1061 return "invalid number";
1063 return "out of range number";
1075 unsigned long long n;
1077 return "invalid number";
1078 if (n > 0xFFFFFFFFUL)
1079 return "out of range number";
1091 unsigned long long N;
1093 return "invalid number";
1107 return "invalid number";
1108 if ((
N > 127) || (
N < -128))
1109 return "out of range number";
1122 return "invalid number";
1123 if ((
N > INT16_MAX) || (
N < INT16_MIN))
1124 return "out of range number";
1137 return "invalid number";
1138 if ((
N > INT32_MAX) || (
N < INT32_MIN))
1139 return "out of range number";
1152 return "invalid number";
1164 return "invalid floating point number";
1174 return "invalid floating point number";
1178 Out <<
format(
"0x%" PRIX8, (uint8_t)Val);
1182 unsigned long long n;
1184 return "invalid hex8 number";
1186 return "out of range hex8 number";
1192 Out <<
format(
"0x%" PRIX16, (uint16_t)Val);
1196 unsigned long long n;
1198 return "invalid hex16 number";
1200 return "out of range hex16 number";
1206 Out <<
format(
"0x%" PRIX32, (uint32_t)Val);
1210 unsigned long long n;
1212 return "invalid hex32 number";
1213 if (n > 0xFFFFFFFFUL)
1214 return "out of range hex32 number";
1224 unsigned long long Num;
1226 return "invalid hex64 number";
1232 llvm::raw_ostream &Out) {
1237 VersionTuple &Val) {
1239 return "invalid version format";
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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 bool canUseBlockScalar(StringRef S)
A literal block scalar has no escapes, so it can only hold printable characters, tab and line feed.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
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...
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Represent a constant reference to a string, i.e.
const unsigned char * bytes_end() const
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
Check if the string is empty.
constexpr size_t size() const
Get the string size.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
StringRef copy(Allocator &A) const
const unsigned char * bytes_begin() 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_ABI bool tryParse(StringRef string)
Try to parse the given string as a version number.
LLVM_ABI 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.
StringRef getName() const
StringRef getValue() const
Gets the value of this node as a StringRef.
virtual bool canElideEmptySequence()=0
virtual NodeKind getNodeKind()=0
virtual void endSequence()=0
virtual void endEnumScalar()=0
virtual bool outputting() const =0
virtual unsigned beginFlowSequence()=0
virtual void endFlowSequence()=0
virtual void beginMapping()=0
virtual void setAllowUnknownKeys(bool Allow)
virtual void endMapping()=0
virtual unsigned beginSequence()=0
virtual void beginEnumScalar()=0
void * getContext() const
virtual void endFlowMapping()=0
virtual void beginFlowMapping()=0
virtual bool matchEnumFallback()=0
virtual void endBitSetScalar()=0
virtual std::vector< StringRef > keys()=0
Abstract base class for all Nodes.
std::string getVerbatimTag() const
Get the verbatium tag for a given Node.
unsigned beginFlowSequence() override
std::vector< StringRef > keys() override
Output(raw_ostream &, void *Ctxt=nullptr, int WrapColumn=70)
void setError(const Twine &message) override
void scalarString(StringRef &, QuotingType) override
void endBitSetScalar() override
void beginFlowMapping() override
void endFlowMapping() override
void endFlowSequence() override
void postflightElement(void *) override
void endEnumScalar() override
void blockScalarString(StringRef &) override
bool preflightDocument(unsigned)
bool bitSetMatch(StringRef, bool) override
bool canElideEmptySequence() override
void beginMapping() override
void postflightFlowElement(void *) override
void scalarTag(std::string &) override
bool beginBitSetScalar(bool &) override
bool mapTag(StringRef, bool) override
bool preflightElement(unsigned, void *&) override
bool matchEnumFallback() override
void beginEnumScalar() override
bool preflightKey(StringRef Key, bool, bool, bool &, void *&) override
void endSequence() override
bool matchEnumScalar(StringRef, bool) override
bool outputting() const override
unsigned beginSequence() override
NodeKind getNodeKind() override
void endMapping() override
std::error_code error() override
void postflightKey(void *) override
void postflightDocument()
bool preflightFlowElement(unsigned, void *&) override
StringRef getValue(SmallVectorImpl< char > &Storage) const
Gets the value of this node as a StringRef.
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.
NodeAddr< NodeBase * > Node
LLVM_ABI bool isPrintable(int UCS)
Determines if a character is likely to be displayed correctly on the terminal.
QuotingType
Describe which type of quotes should be used when quoting is necessary.
LLVM_ABI std::optional< bool > parseBool(StringRef S)
Parse S as a bool according to https://yaml.org/type/bool.html.
QuotingType needsQuotes(StringRef S, bool ForcePreserveAsString=true)
LLVM_ABI 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.
LLVM_ABI bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
std::error_code make_error_code(BitcodeError E)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
bool to_float(const Twine &T, float &Num)
ConversionResult convertUTF8Sequence(const UTF8 **source, const UTF8 *sourceEnd, UTF32 *target, ConversionFlags flags)
Convert the first UTF8 sequence in the given source buffer to a UTF32 code point.
auto dyn_cast_or_null(const Y &Val)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
bool isASCII(char C)
Checks whether character C is valid ASCII (high bit is zero).
bool isPrint(char C)
Checks whether character C is printable.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
@ End
Run the machine verifier once, at the end of the pipeline.
This class should be specialized by type that requires custom conversion to/from a yaml scalar.