60 DocIterator = Strm->begin();
68 DocIterator = Strm->begin();
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() {
87 if (DocIterator != Strm->end()) {
88 Node *
N = DocIterator->getRoot();
90 assert(Strm->failed() &&
"Root is NULL iff parsing failed");
95 if (isa<NullNode>(N)) {
100 TopNode = this->createHNodes(N);
101 CurrentNode = TopNode.get();
108 return ++DocIterator != Strm->end();
112 return CurrentNode ? CurrentNode->_node :
nullptr;
116 std::string foundTag = CurrentNode->_node->getVerbatimTag();
117 if (foundTag.empty()) {
122 return Tag.
equals(foundTag);
125 void Input::beginMapping() {
129 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
131 MN->ValidKeys.clear();
135 std::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());
147 bool Input::preflightKey(
const char *Key,
bool Required,
bool,
bool &UseDefault,
161 MapHNode *MN =
dyn_cast<MapHNode>(CurrentNode);
163 setError(CurrentNode,
"not a mapping");
166 MN->ValidKeys.push_back(Key);
167 HNode *
Value = MN->Mapping[Key].get();
170 setError(CurrentNode,
Twine(
"missing required key '") + Key +
"'");
175 SaveInfo = CurrentNode;
180 void Input::postflightKey(
void *saveInfo) {
181 CurrentNode =
reinterpret_cast<HNode *
>(saveInfo);
184 void Input::endMapping() {
188 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
191 for (
const auto &NN : MN->Mapping) {
193 setError(NN.second.get(),
Twine(
"unknown key '") + NN.first() +
"'");
199 void Input::beginFlowMapping() { beginMapping(); }
201 void Input::endFlowMapping() { endMapping(); }
203 unsigned Input::beginSequence() {
204 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
205 return SQ->Entries.size();
206 if (isa<EmptyHNode>(CurrentNode))
209 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
214 setError(CurrentNode,
"not a sequence");
218 void Input::endSequence() {
221 bool Input::preflightElement(
unsigned Index,
void *&SaveInfo) {
224 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
225 SaveInfo = CurrentNode;
226 CurrentNode = SQ->Entries[Index].get();
232 void Input::postflightElement(
void *SaveInfo) {
233 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
236 unsigned Input::beginFlowSequence() {
return beginSequence(); }
238 bool Input::preflightFlowElement(
unsigned index,
void *&SaveInfo) {
241 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
242 SaveInfo = CurrentNode;
243 CurrentNode = SQ->Entries[index].get();
249 void Input::postflightFlowElement(
void *SaveInfo) {
250 CurrentNode =
reinterpret_cast<HNode *
>(SaveInfo);
253 void Input::endFlowSequence() {
256 void Input::beginEnumScalar() {
257 ScalarMatchFound =
false;
260 bool Input::matchEnumScalar(
const char *Str,
bool) {
261 if (ScalarMatchFound)
263 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
264 if (SN->value().equals(Str)) {
265 ScalarMatchFound =
true;
272 bool Input::matchEnumFallback() {
273 if (ScalarMatchFound)
275 ScalarMatchFound =
true;
279 void Input::endEnumScalar() {
280 if (!ScalarMatchFound) {
281 setError(CurrentNode,
"unknown enumerated scalar");
285 bool Input::beginBitSetScalar(
bool &DoClear) {
286 BitValuesUsed.clear();
287 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
288 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(),
false);
290 setError(CurrentNode,
"expected sequence of bit values");
296 bool Input::bitSetMatch(
const char *Str,
bool) {
299 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
301 for (
auto &
N : SQ->Entries) {
302 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(
N.get())) {
303 if (SN->value().equals(Str)) {
304 BitValuesUsed[Index] =
true;
308 setError(CurrentNode,
"unexpected scalar in sequence of bit values");
313 setError(CurrentNode,
"expected sequence of bit values");
318 void Input::endBitSetScalar() {
321 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
322 assert(BitValuesUsed.size() == SQ->Entries.size());
323 for (
unsigned i = 0; i < SQ->Entries.size(); ++i) {
324 if (!BitValuesUsed[i]) {
325 setError(SQ->Entries[i].get(),
"unknown bit value");
332 void Input::scalarString(
StringRef &S,
bool) {
333 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
336 setError(CurrentNode,
"unexpected scalar");
340 void Input::blockScalarString(
StringRef &S) { scalarString(S,
false); }
342 void Input::setError(HNode *hnode,
const Twine &message) {
343 assert(hnode &&
"HNode must not be NULL");
344 this->setError(hnode->_node, message);
347 void Input::setError(
Node *node,
const Twine &message) {
348 Strm->printError(node, message);
352 std::unique_ptr<Input::HNode> Input::createHNodes(
Node *
N) {
354 if (
ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
355 StringRef KeyStr = SN->getValue(StringStorage);
356 if (!StringStorage.
empty()) {
358 KeyStr = StringStorage.
str().
copy(StringAllocator);
360 return llvm::make_unique<ScalarHNode>(
N, KeyStr);
363 return llvm::make_unique<ScalarHNode>(
N, ValueCopy);
364 }
else if (
SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
365 auto SQHNode = llvm::make_unique<SequenceHNode>(
N);
366 for (
Node &SN : *SQ) {
367 auto Entry = this->createHNodes(&SN);
370 SQHNode->Entries.push_back(std::move(Entry));
372 return std::move(SQHNode);
373 }
else if (
MappingNode *Map = dyn_cast<MappingNode>(N)) {
374 auto mapHNode = llvm::make_unique<MapHNode>(
N);
376 Node *KeyNode = KVN.getKey();
379 setError(KeyNode,
"Map key must be a scalar");
382 StringStorage.
clear();
384 if (!StringStorage.
empty()) {
386 KeyStr = StringStorage.
str().
copy(StringAllocator);
388 auto ValueHNode = this->createHNodes(KVN.getValue());
391 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
393 return std::move(mapHNode);
394 }
else if (isa<NullNode>(N)) {
395 return llvm::make_unique<EmptyHNode>(
N);
397 setError(N,
"unknown node kind");
402 void Input::setError(
const Twine &Message) {
403 this->setError(CurrentNode, Message);
406 bool Input::canElideEmptySequence() {
415 :
IO(context), Out(yout), WrapColumn(WrapColumn) {}
433 bool SequenceElement =
434 StateStack.
size() > 1 && (StateStack[StateStack.
size() - 2] == inSeq ||
435 StateStack[StateStack.
size() - 2] == inFlowSeq);
436 if (SequenceElement && StateStack.
back() == inMapFirstKey) {
437 this->newLineCheck();
442 if (SequenceElement) {
445 if (StateStack.
back() == inMapFirstKey) {
466 bool &UseDefault,
void *&) {
468 if (Required || !SameAsDefault || WriteDefaultValues) {
469 auto State = StateStack.
back();
470 if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
473 this->newLineCheck();
474 this->paddedKey(Key);
482 if (StateStack.
back() == inMapFirstKey) {
485 }
else if (StateStack.
back() == inFlowMapFirstKey) {
493 this->newLineCheck();
494 ColumnAtMapFlowStart = Column;
500 this->outputUpToEndOfLine(
" }");
504 this->outputUpToEndOfLine(
"---");
509 this->outputUpToEndOfLine(
"\n---");
539 this->newLineCheck();
540 ColumnAtFlowStart = Column;
542 NeedFlowSequenceComma =
false;
548 this->outputUpToEndOfLine(
" ]");
552 if (NeedFlowSequenceComma)
554 if (WrapColumn && Column > WrapColumn) {
556 for (
int i = 0; i < ColumnAtFlowStart; ++i)
558 Column = ColumnAtFlowStart;
565 NeedFlowSequenceComma =
true;
569 EnumerationMatchFound =
false;
573 if (Match && !EnumerationMatchFound) {
574 this->newLineCheck();
575 this->outputUpToEndOfLine(Str);
576 EnumerationMatchFound =
true;
582 if (EnumerationMatchFound)
584 EnumerationMatchFound =
true;
589 if (!EnumerationMatchFound)
594 this->newLineCheck();
596 NeedBitValueComma =
false;
603 if (NeedBitValueComma)
606 NeedBitValueComma =
true;
612 this->outputUpToEndOfLine(
" ]");
616 this->newLineCheck();
620 this->outputUpToEndOfLine(
"''");
625 this->outputUpToEndOfLine(S);
643 this->outputUpToEndOfLine(
"'");
647 if (!StateStack.
empty())
652 unsigned Indent = StateStack.
empty() ? 1 : StateStack.
size();
655 for (
line_iterator Lines(*Buffer,
false); !Lines.is_at_end(); ++Lines) {
656 for (
unsigned I = 0;
I < Indent; ++
I) {
673 if (StateStack.
size() < 2)
675 if (StateStack.
back() != inMapFirstKey)
677 return (StateStack[StateStack.
size()-2] != inSeq);
685 void Output::outputUpToEndOfLine(
StringRef s) {
687 if (StateStack.
empty() || (StateStack.
back() != inFlowSeq &&
688 StateStack.
back() != inFlowMapFirstKey &&
689 StateStack.
back() != inFlowMapOtherKey))
693 void Output::outputNewLine() {
702 void Output::newLineCheck() {
705 NeedsNewLine =
false;
707 this->outputNewLine();
710 unsigned Indent = StateStack.
size() - 1;
711 bool OutputDash =
false;
713 if (StateStack.
back() == inSeq) {
715 }
else if ((StateStack.
size() > 1) && ((StateStack.
back() == inMapFirstKey) ||
716 (StateStack.
back() == inFlowSeq) ||
717 (StateStack.
back() == inFlowMapFirstKey)) &&
718 (StateStack[StateStack.
size() - 2] == inSeq)) {
723 for (
unsigned i = 0; i < Indent; ++i) {
735 const char *spaces =
" ";
736 if (key.
size() < strlen(spaces))
737 output(&spaces[key.
size()]);
743 if (StateStack.
back() == inFlowMapOtherKey)
745 if (WrapColumn && Column > WrapColumn) {
747 for (
int I = 0;
I < ColumnAtMapFlowStart; ++
I)
749 Column = ColumnAtMapFlowStart;
761 Out << (Val ?
"true" :
"false");
765 if (Scalar.
equals(
"true")) {
768 }
else if (Scalar.
equals(
"false")) {
772 return "invalid boolean";
805 unsigned long long n;
807 return "invalid number";
809 return "out of range number";
821 unsigned long long n;
823 return "invalid number";
825 return "out of range number";
837 unsigned long long n;
839 return "invalid number";
840 if (n > 0xFFFFFFFFUL)
841 return "out of range number";
853 unsigned long long N;
855 return "invalid number";
869 return "invalid number";
870 if ((N > 127) || (N < -128))
871 return "out of range number";
884 return "invalid number";
885 if ((N > INT16_MAX) || (N < INT16_MIN))
886 return "out of range number";
899 return "invalid number";
900 if ((N > INT32_MAX) || (N < INT32_MIN))
901 return "out of range number";
914 return "invalid number";
926 return "invalid floating point number";
936 return "invalid floating point number";
941 Out <<
format(
"0x%02X", Num);
945 unsigned long long n;
947 return "invalid hex8 number";
949 return "out of range hex8 number";
956 Out <<
format(
"0x%04X", Num);
960 unsigned long long n;
962 return "invalid hex16 number";
964 return "out of range hex16 number";
971 Out <<
format(
"0x%08X", Num);
975 unsigned long long n;
977 return "invalid hex32 number";
978 if (n > 0xFFFFFFFFUL)
979 return "out of range hex32 number";
986 Out <<
format(
"0x%016llX", Num);
990 unsigned long long Num;
992 return "invalid hex64 number";
void setError(const Twine &message) override
void push_back(const T &Elt)
bool preflightElement(unsigned, void *&) override
bool preflightDocument(unsigned)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
void beginMapping() override
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.
void endFlowMapping() override
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.
void endSequence() override
A forward iterator which reads text lines from a buffer.
void beginFlowMapping() override
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.
void postflightElement(void *) override
bool matchEnumScalar(const char *, bool) override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
std::error_code make_error_code(BitcodeError E)
void scalarString(StringRef &, bool) override
bool matchEnumFallback() override
bool beginBitSetScalar(bool &) override
LLVM_NODISCARD bool empty() const
bool outputting() override
bool to_float(const Twine &T, float &Num)
std::vector< StringRef > keys() override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
bool mapTag(StringRef, bool) override
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
void endMapping() override
Function Alias Analysis false
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
void postflightDocument()
void postflightFlowElement(void *) override
static const unsigned End
void endFlowSequence() override
bool preflightKey(const char *key, bool, bool, bool &, void *&) override
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
Output(raw_ostream &, void *Ctxt=nullptr, int WrapColumn=70)
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
bool preflightFlowElement(unsigned, void *&) override
This class represents a YAML stream potentially containing multiple documents.
This class should be specialized by type that requires custom conversion to/from a yaml scalar...
void endBitSetScalar() override
bool bitSetMatch(const char *, bool) override
StringRef str() const
Explicit conversion to StringRef.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
LLVM_NODISCARD StringRef copy(Allocator &A) const
unsigned beginFlowSequence() override
llvm_unreachable("Unknown RegBank!")
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
void beginEnumScalar() override
unsigned beginSequence() override
This class implements an extremely fast bulk output stream that can only output to a stream...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringRef - Represent a constant reference to a string, i.e.
void endEnumScalar() override
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
void postflightKey(void *) override
void blockScalarString(StringRef &) override
bool canElideEmptySequence() override
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.