39 IO::IO(
void *
Context) : Ctxt(Context) {}
43 void *IO::getContext()
const {
47 void IO::setContext(
void *
Context) {
55 Input::Input(
StringRef InputContent,
void *Ctxt,
60 DocIterator = Strm->begin();
68 DocIterator = Strm->begin();
71 Input::~Input() =
default;
76 void Input::HNode::anchor() {}
77 void Input::EmptyHNode::anchor() {}
78 void Input::ScalarHNode::anchor() {}
79 void Input::MapHNode::anchor() {}
80 void Input::SequenceHNode::anchor() {}
82 bool Input::outputting()
const {
86 bool Input::setCurrentDocument() {
87 if (DocIterator != Strm->end()) {
88 Node *
N = DocIterator->getRoot();
94 if (isa<NullNode>(N)) {
97 return setCurrentDocument();
99 TopNode = createHNodes(N);
100 CurrentNode = TopNode.get();
106 bool Input::nextDocument() {
107 return ++DocIterator != Strm->end();
110 const Node *Input::getCurrentNode()
const {
111 return CurrentNode ? CurrentNode->_node :
nullptr;
114 bool Input::mapTag(
StringRef Tag,
bool Default) {
121 if (foundTag.empty()) {
126 return Tag.
equals(foundTag);
129 void Input::beginMapping() {
133 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
135 MN->ValidKeys.clear();
139 std::vector<StringRef> Input::keys() {
140 MapHNode *MN =
dyn_cast<MapHNode>(CurrentNode);
141 std::vector<StringRef>
Ret;
143 setError(CurrentNode,
"not a mapping");
146 for (
auto &
P : MN->Mapping)
147 Ret.push_back(
P.first());
151 bool Input::preflightKey(
const char *
Key,
bool Required,
bool,
bool &UseDefault,
165 MapHNode *MN =
dyn_cast<MapHNode>(CurrentNode);
167 if (Required || !isa<EmptyHNode>(CurrentNode))
168 setError(CurrentNode,
"not a mapping");
171 MN->ValidKeys.push_back(Key);
172 HNode *
Value = MN->Mapping[
Key].get();
175 setError(CurrentNode,
Twine(
"missing required key '") + Key +
"'");
180 SaveInfo = CurrentNode;
185 void Input::postflightKey(
void *saveInfo) {
186 CurrentNode =
reinterpret_cast<HNode *
>(saveInfo);
189 void Input::endMapping() {
193 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
196 for (
const auto &NN : MN->Mapping) {
198 setError(NN.second.get(),
Twine(
"unknown key '") + NN.first() +
"'");
204 void Input::beginFlowMapping() { beginMapping(); }
206 void Input::endFlowMapping() { endMapping(); }
208 unsigned Input::beginSequence() {
209 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
210 return SQ->Entries.size();
211 if (isa<EmptyHNode>(CurrentNode))
214 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
215 if (isNull(SN->value()))
219 setError(CurrentNode,
"not a sequence");
223 void Input::endSequence() {
226 bool Input::preflightElement(
unsigned Index,
void *&SaveInfo) {
229 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
230 SaveInfo = CurrentNode;
231 CurrentNode = SQ->Entries[
Index].get();
237 void Input::postflightElement(
void *SaveInfo) {
238 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
241 unsigned Input::beginFlowSequence() {
return beginSequence(); }
243 bool Input::preflightFlowElement(
unsigned index,
void *&SaveInfo) {
246 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
247 SaveInfo = CurrentNode;
248 CurrentNode = SQ->Entries[index].get();
254 void Input::postflightFlowElement(
void *SaveInfo) {
255 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
258 void Input::endFlowSequence() {
261 void Input::beginEnumScalar() {
262 ScalarMatchFound =
false;
265 bool Input::matchEnumScalar(
const char *Str,
bool) {
266 if (ScalarMatchFound)
268 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
269 if (SN->value().equals(Str)) {
270 ScalarMatchFound =
true;
277 bool Input::matchEnumFallback() {
278 if (ScalarMatchFound)
280 ScalarMatchFound =
true;
284 void Input::endEnumScalar() {
285 if (!ScalarMatchFound) {
286 setError(CurrentNode,
"unknown enumerated scalar");
290 bool Input::beginBitSetScalar(
bool &DoClear) {
291 BitValuesUsed.clear();
292 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
293 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(),
false);
295 setError(CurrentNode,
"expected sequence of bit values");
301 bool Input::bitSetMatch(
const char *Str,
bool) {
304 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
306 for (
auto &N : SQ->Entries) {
307 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
308 if (SN->value().equals(Str)) {
309 BitValuesUsed[
Index] =
true;
313 setError(CurrentNode,
"unexpected scalar in sequence of bit values");
318 setError(CurrentNode,
"expected sequence of bit values");
323 void Input::endBitSetScalar() {
326 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
327 assert(BitValuesUsed.size() == SQ->Entries.size());
328 for (
unsigned i = 0; i < SQ->Entries.size(); ++i) {
329 if (!BitValuesUsed[i]) {
330 setError(SQ->Entries[i].get(),
"unknown bit value");
337 void Input::scalarString(
StringRef &S, QuotingType) {
338 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
341 setError(CurrentNode,
"unexpected scalar");
347 void Input::scalarTag(std::string &Tag) {
348 Tag = CurrentNode->_node->getVerbatimTag();
351 void Input::setError(HNode *hnode,
const Twine &message) {
352 assert(hnode &&
"HNode must not be NULL");
353 setError(hnode->_node, message);
357 if (isa<ScalarHNode>(CurrentNode))
359 else if (isa<MapHNode>(CurrentNode))
360 return NodeKind::Map;
361 else if (isa<SequenceHNode>(CurrentNode))
366 void Input::setError(
Node *node,
const Twine &message) {
367 Strm->printError(node, message);
371 std::unique_ptr<Input::HNode> Input::createHNodes(
Node *N) {
373 if (
ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
374 StringRef KeyStr = SN->getValue(StringStorage);
375 if (!StringStorage.
empty()) {
377 KeyStr = StringStorage.
str().
copy(StringAllocator);
379 return std::make_unique<ScalarHNode>(
N, KeyStr);
382 return std::make_unique<ScalarHNode>(
N, ValueCopy);
383 }
else if (
SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
384 auto SQHNode = std::make_unique<SequenceHNode>(
N);
385 for (
Node &SN : *SQ) {
386 auto Entry = createHNodes(&SN);
389 SQHNode->Entries.push_back(std::move(
Entry));
391 return std::move(SQHNode);
392 }
else if (
MappingNode *Map = dyn_cast<MappingNode>(N)) {
393 auto mapHNode = std::make_unique<MapHNode>(
N);
395 Node *KeyNode = KVN.getKey();
396 ScalarNode *Key = dyn_cast_or_null<ScalarNode>(KeyNode);
397 Node *Value = KVN.getValue();
398 if (!Key || !Value) {
400 setError(KeyNode,
"Map key must be a scalar");
402 setError(KeyNode,
"Map value must not be empty");
405 StringStorage.
clear();
407 if (!StringStorage.
empty()) {
409 KeyStr = StringStorage.
str().
copy(StringAllocator);
411 auto ValueHNode = createHNodes(Value);
414 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
416 return std::move(mapHNode);
417 }
else if (isa<NullNode>(N)) {
418 return std::make_unique<EmptyHNode>(
N);
420 setError(N,
"unknown node kind");
425 void Input::setError(
const Twine &Message) {
426 setError(CurrentNode, Message);
429 bool Input::canElideEmptySequence() {
437 Output::Output(
raw_ostream &yout,
void *context,
int WrapColumn)
438 : IO(context), Out(yout), WrapColumn(WrapColumn) {}
440 Output::~Output() =
default;
442 bool Output::outputting()
const {
446 void Output::beginMapping() {
447 StateStack.push_back(inMapFirstKey);
448 PaddingBeforeContainer = Padding;
457 bool SequenceElement =
false;
458 if (StateStack.size() > 1) {
459 auto &
E = StateStack[StateStack.size() - 2];
460 SequenceElement = inSeqAnyElement(
E) || inFlowSeqAnyElement(
E);
462 if (SequenceElement && StateStack.back() == inMapFirstKey) {
468 if (SequenceElement) {
471 if (StateStack.back() == inMapFirstKey) {
472 StateStack.pop_back();
473 StateStack.push_back(inMapOtherKey);
483 void Output::endMapping() {
485 if (StateStack.back() == inMapFirstKey) {
486 Padding = PaddingBeforeContainer;
491 StateStack.pop_back();
494 std::vector<StringRef> Output::keys() {
498 bool Output::preflightKey(
const char *Key,
bool Required,
bool SameAsDefault,
499 bool &UseDefault,
void *&) {
501 if (Required || !SameAsDefault || WriteDefaultValues) {
502 auto State = StateStack.back();
503 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
514 void Output::postflightKey(
void *) {
515 if (StateStack.back() == inMapFirstKey) {
516 StateStack.pop_back();
517 StateStack.push_back(inMapOtherKey);
518 }
else if (StateStack.back() == inFlowMapFirstKey) {
519 StateStack.pop_back();
520 StateStack.push_back(inFlowMapOtherKey);
524 void Output::beginFlowMapping() {
525 StateStack.push_back(inFlowMapFirstKey);
527 ColumnAtMapFlowStart = Column;
531 void Output::endFlowMapping() {
532 StateStack.pop_back();
533 outputUpToEndOfLine(
" }");
536 void Output::beginDocuments() {
537 outputUpToEndOfLine(
"---");
540 bool Output::preflightDocument(
unsigned index) {
542 outputUpToEndOfLine(
"\n---");
546 void Output::postflightDocument() {
549 void Output::endDocuments() {
553 unsigned Output::beginSequence() {
554 StateStack.push_back(inSeqFirstElement);
555 PaddingBeforeContainer = Padding;
560 void Output::endSequence() {
562 if (StateStack.back() == inSeqFirstElement) {
563 Padding = PaddingBeforeContainer;
568 StateStack.pop_back();
571 bool Output::preflightElement(
unsigned,
void *&) {
575 void Output::postflightElement(
void *) {
576 if (StateStack.back() == inSeqFirstElement) {
577 StateStack.pop_back();
578 StateStack.push_back(inSeqOtherElement);
579 }
else if (StateStack.back() == inFlowSeqFirstElement) {
580 StateStack.pop_back();
581 StateStack.push_back(inFlowSeqOtherElement);
585 unsigned Output::beginFlowSequence() {
586 StateStack.push_back(inFlowSeqFirstElement);
588 ColumnAtFlowStart = Column;
590 NeedFlowSequenceComma =
false;
594 void Output::endFlowSequence() {
595 StateStack.pop_back();
596 outputUpToEndOfLine(
" ]");
599 bool Output::preflightFlowElement(
unsigned,
void *&) {
600 if (NeedFlowSequenceComma)
602 if (WrapColumn && Column > WrapColumn) {
604 for (
int i = 0; i < ColumnAtFlowStart; ++i)
606 Column = ColumnAtFlowStart;
612 void Output::postflightFlowElement(
void *) {
613 NeedFlowSequenceComma =
true;
616 void Output::beginEnumScalar() {
617 EnumerationMatchFound =
false;
620 bool Output::matchEnumScalar(
const char *Str,
bool Match) {
621 if (Match && !EnumerationMatchFound) {
623 outputUpToEndOfLine(Str);
624 EnumerationMatchFound =
true;
629 bool Output::matchEnumFallback() {
630 if (EnumerationMatchFound)
632 EnumerationMatchFound =
true;
636 void Output::endEnumScalar() {
637 if (!EnumerationMatchFound)
641 bool Output::beginBitSetScalar(
bool &DoClear) {
644 NeedBitValueComma =
false;
649 bool Output::bitSetMatch(
const char *Str,
bool Matches) {
651 if (NeedBitValueComma)
654 NeedBitValueComma =
true;
659 void Output::endBitSetScalar() {
660 outputUpToEndOfLine(
" ]");
663 void Output::scalarString(
StringRef &S, QuotingType MustQuote) {
668 outputUpToEndOfLine(
"''");
673 outputUpToEndOfLine(S);
685 outputUpToEndOfLine(Quote);
691 unsigned End = S.
size();
704 outputUpToEndOfLine(Quote);
707 void Output::blockScalarString(
StringRef &S) {
708 if (!StateStack.empty())
713 unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
717 for (
unsigned I = 0;
I < Indent; ++
I) {
725 void Output::scalarTag(std::string &Tag) {
733 void Output::setError(
const Twine &message) {
736 bool Output::canElideEmptySequence() {
742 if (StateStack.size() < 2)
744 if (StateStack.back() != inMapFirstKey)
746 return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
754 void Output::outputUpToEndOfLine(
StringRef s) {
756 if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
757 !inFlowMapAnyKey(StateStack.back())))
761 void Output::outputNewLine() {
770 void Output::newLineCheck() {
771 if (Padding !=
"\n") {
779 if (StateStack.size() == 0)
782 unsigned Indent = StateStack.size() - 1;
783 bool OutputDash =
false;
785 if (StateStack.back() == inSeqFirstElement ||
786 StateStack.back() == inSeqOtherElement) {
788 }
else if ((StateStack.size() > 1) &&
789 ((StateStack.back() == inMapFirstKey) ||
790 inFlowSeqAnyElement(StateStack.back()) ||
791 (StateStack.back() == inFlowMapFirstKey)) &&
792 inSeqAnyElement(StateStack[StateStack.size() - 2])) {
797 for (
unsigned i = 0; i < Indent; ++i) {
809 const char *spaces =
" ";
810 if (key.
size() < strlen(spaces))
811 Padding = &spaces[key.
size()];
817 if (StateStack.back() == inFlowMapOtherKey)
819 if (WrapColumn && Column > WrapColumn) {
821 for (
int I = 0;
I < ColumnAtMapFlowStart; ++
I)
823 Column = ColumnAtMapFlowStart;
832 bool Output::inSeqAnyElement(InState State) {
833 return State == inSeqFirstElement || State == inSeqOtherElement;
836 bool Output::inFlowSeqAnyElement(InState State) {
837 return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
840 bool Output::inMapAnyKey(InState State) {
841 return State == inMapFirstKey || State == inMapOtherKey;
844 bool Output::inFlowMapAnyKey(InState State) {
845 return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
852 void ScalarTraits<bool>::output(
const bool &Val,
void *,
raw_ostream &Out) {
853 Out << (Val ?
"true" :
"false");
857 if (Scalar.
equals(
"true")) {
860 }
else if (Scalar.
equals(
"false")) {
864 return "invalid boolean";
867 void ScalarTraits<StringRef>::output(
const StringRef &Val,
void *,
878 void ScalarTraits<std::string>::output(
const std::string &Val,
void *,
889 void ScalarTraits<uint8_t>::output(
const uint8_t &Val,
void *,
897 unsigned long long n;
899 return "invalid number";
901 return "out of range number";
906 void ScalarTraits<uint16_t>::output(
const uint16_t &Val,
void *,
913 unsigned long long n;
915 return "invalid number";
917 return "out of range number";
922 void ScalarTraits<uint32_t>::output(
const uint32_t &Val,
void *,
929 unsigned long long n;
931 return "invalid number";
932 if (n > 0xFFFFFFFFUL)
933 return "out of range number";
938 void ScalarTraits<uint64_t>::output(
const uint64_t &Val,
void *,
945 unsigned long long N;
947 return "invalid number";
952 void ScalarTraits<int8_t>::output(
const int8_t &Val,
void *,
raw_ostream &Out) {
961 return "invalid number";
962 if ((N > 127) || (N < -128))
963 return "out of range number";
968 void ScalarTraits<int16_t>::output(
const int16_t &Val,
void *,
976 return "invalid number";
977 if ((N > INT16_MAX) || (N < INT16_MIN))
978 return "out of range number";
983 void ScalarTraits<int32_t>::output(
const int32_t &Val,
void *,
991 return "invalid number";
992 if ((N > INT32_MAX) || (N < INT32_MIN))
993 return "out of range number";
998 void ScalarTraits<int64_t>::output(
const int64_t &Val,
void *,
1006 return "invalid number";
1011 void ScalarTraits<double>::output(
const double &Val,
void *,
raw_ostream &Out) {
1012 Out <<
format(
"%g", Val);
1018 return "invalid floating point number";
1021 void ScalarTraits<float>::output(
const float &Val,
void *,
raw_ostream &Out) {
1022 Out <<
format(
"%g", Val);
1028 return "invalid floating point number";
1031 void ScalarTraits<Hex8>::output(
const Hex8 &Val,
void *,
raw_ostream &Out) {
1033 Out <<
format(
"0x%02X", Num);
1037 unsigned long long n;
1039 return "invalid hex8 number";
1041 return "out of range hex8 number";
1046 void ScalarTraits<Hex16>::output(
const Hex16 &Val,
void *,
raw_ostream &Out) {
1048 Out <<
format(
"0x%04X", Num);
1052 unsigned long long n;
1054 return "invalid hex16 number";
1056 return "out of range hex16 number";
1061 void ScalarTraits<Hex32>::output(
const Hex32 &Val,
void *,
raw_ostream &Out) {
1063 Out <<
format(
"0x%08X", Num);
1067 unsigned long long n;
1069 return "invalid hex32 number";
1070 if (n > 0xFFFFFFFFUL)
1071 return "out of range hex32 number";
1076 void ScalarTraits<Hex64>::output(
const Hex64 &Val,
void *,
raw_ostream &Out) {
1078 Out <<
format(
"0x%016llX", Num);
1082 unsigned long long Num;
1084 return "invalid hex64 number";
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Represents a YAML sequence created from either a block sequence for a flow sequence.
A forward iterator which reads text lines from a buffer.
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...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_NODISCARD StringRef copy(Allocator &A) const
A Use represents the edge between a Value definition and its users.
std::error_code make_error_code(BitcodeError E)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
StringRef str() const
Explicit conversion to StringRef.
bool to_float(const Twine &T, float &Num)
LLVM_NODISCARD size_t size() const
size - Get the string size.
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...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
This class represents a YAML stream potentially containing multiple documents.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
LLVM_NODISCARD bool empty() const
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
std::string getVerbatimTag() const
Get the verbatium tag for a given Node.
StringRef - Represent a constant reference to a string, i.e.
StringRef getValue(SmallVectorImpl< char > &Storage) const
Gets the value of this node as a StringRef.
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
Abstract base class for all Nodes.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.