LLVM  5.0.0
YAMLTraits.cpp
Go to the documentation of this file.
1 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
2 //
3 // The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Format.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <cstdint>
27 #include <cstdlib>
28 #include <cstring>
29 #include <string>
30 #include <vector>
31 
32 using namespace llvm;
33 using namespace yaml;
34 
35 //===----------------------------------------------------------------------===//
36 // IO
37 //===----------------------------------------------------------------------===//
38 
39 IO::IO(void *Context) : Ctxt(Context) {}
40 
41 IO::~IO() = default;
42 
43 void *IO::getContext() {
44  return Ctxt;
45 }
46 
47 void IO::setContext(void *Context) {
48  Ctxt = Context;
49 }
50 
51 //===----------------------------------------------------------------------===//
52 // Input
53 //===----------------------------------------------------------------------===//
54 
55 Input::Input(StringRef InputContent, void *Ctxt,
56  SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
57  : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
58  if (DiagHandler)
59  SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
60  DocIterator = Strm->begin();
61 }
62 
64  SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
65  : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
66  if (DiagHandler)
67  SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
68  DocIterator = Strm->begin();
69 }
70 
71 Input::~Input() = default;
72 
73 std::error_code Input::error() { return EC; }
74 
75 // Pin the vtables to this file.
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() {}
81 
82 bool Input::outputting() {
83  return false;
84 }
85 
87  if (DocIterator != Strm->end()) {
88  Node *N = DocIterator->getRoot();
89  if (!N) {
90  assert(Strm->failed() && "Root is NULL iff parsing failed");
92  return false;
93  }
94 
95  if (isa<NullNode>(N)) {
96  // Empty files are allowed and ignored
97  ++DocIterator;
98  return setCurrentDocument();
99  }
100  TopNode = this->createHNodes(N);
101  CurrentNode = TopNode.get();
102  return true;
103  }
104  return false;
105 }
106 
108  return ++DocIterator != Strm->end();
109 }
110 
111 const Node *Input::getCurrentNode() const {
112  return CurrentNode ? CurrentNode->_node : nullptr;
113 }
114 
115 bool Input::mapTag(StringRef Tag, bool Default) {
116  std::string foundTag = CurrentNode->_node->getVerbatimTag();
117  if (foundTag.empty()) {
118  // If no tag found and 'Tag' is the default, say it was found.
119  return Default;
120  }
121  // Return true iff found tag matches supplied tag.
122  return Tag.equals(foundTag);
123 }
124 
125 void Input::beginMapping() {
126  if (EC)
127  return;
128  // CurrentNode can be null if the document is empty.
129  MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
130  if (MN) {
131  MN->ValidKeys.clear();
132  }
133 }
134 
135 std::vector<StringRef> Input::keys() {
136  MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
137  std::vector<StringRef> Ret;
138  if (!MN) {
139  setError(CurrentNode, "not a mapping");
140  return Ret;
141  }
142  for (auto &P : MN->Mapping)
143  Ret.push_back(P.first());
144  return Ret;
145 }
146 
147 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
148  void *&SaveInfo) {
149  UseDefault = false;
150  if (EC)
151  return false;
152 
153  // CurrentNode is null for empty documents, which is an error in case required
154  // nodes are present.
155  if (!CurrentNode) {
156  if (Required)
158  return false;
159  }
160 
161  MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
162  if (!MN) {
163  setError(CurrentNode, "not a mapping");
164  return false;
165  }
166  MN->ValidKeys.push_back(Key);
167  HNode *Value = MN->Mapping[Key].get();
168  if (!Value) {
169  if (Required)
170  setError(CurrentNode, Twine("missing required key '") + Key + "'");
171  else
172  UseDefault = true;
173  return false;
174  }
175  SaveInfo = CurrentNode;
176  CurrentNode = Value;
177  return true;
178 }
179 
180 void Input::postflightKey(void *saveInfo) {
181  CurrentNode = reinterpret_cast<HNode *>(saveInfo);
182 }
183 
184 void Input::endMapping() {
185  if (EC)
186  return;
187  // CurrentNode can be null if the document is empty.
188  MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
189  if (!MN)
190  return;
191  for (const auto &NN : MN->Mapping) {
192  if (!is_contained(MN->ValidKeys, NN.first())) {
193  setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
194  break;
195  }
196  }
197 }
198 
199 void Input::beginFlowMapping() { beginMapping(); }
200 
201 void Input::endFlowMapping() { endMapping(); }
202 
203 unsigned Input::beginSequence() {
204  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
205  return SQ->Entries.size();
206  if (isa<EmptyHNode>(CurrentNode))
207  return 0;
208  // Treat case where there's a scalar "null" value as an empty sequence.
209  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
210  if (isNull(SN->value()))
211  return 0;
212  }
213  // Any other type of HNode is an error.
214  setError(CurrentNode, "not a sequence");
215  return 0;
216 }
217 
218 void Input::endSequence() {
219 }
220 
221 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
222  if (EC)
223  return false;
224  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
225  SaveInfo = CurrentNode;
226  CurrentNode = SQ->Entries[Index].get();
227  return true;
228  }
229  return false;
230 }
231 
232 void Input::postflightElement(void *SaveInfo) {
233  CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
234 }
235 
236 unsigned Input::beginFlowSequence() { return beginSequence(); }
237 
238 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
239  if (EC)
240  return false;
241  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
242  SaveInfo = CurrentNode;
243  CurrentNode = SQ->Entries[index].get();
244  return true;
245  }
246  return false;
247 }
248 
249 void Input::postflightFlowElement(void *SaveInfo) {
250  CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
251 }
252 
253 void Input::endFlowSequence() {
254 }
255 
256 void Input::beginEnumScalar() {
257  ScalarMatchFound = false;
258 }
259 
260 bool Input::matchEnumScalar(const char *Str, bool) {
261  if (ScalarMatchFound)
262  return false;
263  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
264  if (SN->value().equals(Str)) {
265  ScalarMatchFound = true;
266  return true;
267  }
268  }
269  return false;
270 }
271 
272 bool Input::matchEnumFallback() {
273  if (ScalarMatchFound)
274  return false;
275  ScalarMatchFound = true;
276  return true;
277 }
278 
279 void Input::endEnumScalar() {
280  if (!ScalarMatchFound) {
281  setError(CurrentNode, "unknown enumerated scalar");
282  }
283 }
284 
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);
289  } else {
290  setError(CurrentNode, "expected sequence of bit values");
291  }
292  DoClear = true;
293  return true;
294 }
295 
296 bool Input::bitSetMatch(const char *Str, bool) {
297  if (EC)
298  return false;
299  if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
300  unsigned Index = 0;
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;
305  return true;
306  }
307  } else {
308  setError(CurrentNode, "unexpected scalar in sequence of bit values");
309  }
310  ++Index;
311  }
312  } else {
313  setError(CurrentNode, "expected sequence of bit values");
314  }
315  return false;
316 }
317 
318 void Input::endBitSetScalar() {
319  if (EC)
320  return;
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");
326  return;
327  }
328  }
329  }
330 }
331 
332 void Input::scalarString(StringRef &S, bool) {
333  if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
334  S = SN->value();
335  } else {
336  setError(CurrentNode, "unexpected scalar");
337  }
338 }
339 
340 void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
341 
342 void Input::setError(HNode *hnode, const Twine &message) {
343  assert(hnode && "HNode must not be NULL");
344  this->setError(hnode->_node, message);
345 }
346 
347 void Input::setError(Node *node, const Twine &message) {
348  Strm->printError(node, message);
350 }
351 
352 std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
353  SmallString<128> StringStorage;
354  if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
355  StringRef KeyStr = SN->getValue(StringStorage);
356  if (!StringStorage.empty()) {
357  // Copy string to permanent storage
358  KeyStr = StringStorage.str().copy(StringAllocator);
359  }
360  return llvm::make_unique<ScalarHNode>(N, KeyStr);
361  } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
362  StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
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);
368  if (EC)
369  break;
370  SQHNode->Entries.push_back(std::move(Entry));
371  }
372  return std::move(SQHNode);
373  } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
374  auto mapHNode = llvm::make_unique<MapHNode>(N);
375  for (KeyValueNode &KVN : *Map) {
376  Node *KeyNode = KVN.getKey();
377  ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
378  if (!KeyScalar) {
379  setError(KeyNode, "Map key must be a scalar");
380  break;
381  }
382  StringStorage.clear();
383  StringRef KeyStr = KeyScalar->getValue(StringStorage);
384  if (!StringStorage.empty()) {
385  // Copy string to permanent storage
386  KeyStr = StringStorage.str().copy(StringAllocator);
387  }
388  auto ValueHNode = this->createHNodes(KVN.getValue());
389  if (EC)
390  break;
391  mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
392  }
393  return std::move(mapHNode);
394  } else if (isa<NullNode>(N)) {
395  return llvm::make_unique<EmptyHNode>(N);
396  } else {
397  setError(N, "unknown node kind");
398  return nullptr;
399  }
400 }
401 
402 void Input::setError(const Twine &Message) {
403  this->setError(CurrentNode, Message);
404 }
405 
406 bool Input::canElideEmptySequence() {
407  return false;
408 }
409 
410 //===----------------------------------------------------------------------===//
411 // Output
412 //===----------------------------------------------------------------------===//
413 
414 Output::Output(raw_ostream &yout, void *context, int WrapColumn)
415  : IO(context), Out(yout), WrapColumn(WrapColumn) {}
416 
417 Output::~Output() = default;
418 
420  return true;
421 }
422 
424  StateStack.push_back(inMapFirstKey);
425  NeedsNewLine = true;
426 }
427 
428 bool Output::mapTag(StringRef Tag, bool Use) {
429  if (Use) {
430  // If this tag is being written inside a sequence we should write the start
431  // of the sequence before writing the tag, otherwise the tag won't be
432  // attached to the element in the sequence, but rather the sequence itself.
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();
438  } else {
439  this->output(" ");
440  }
441  this->output(Tag);
442  if (SequenceElement) {
443  // If we're writing the tag during the first element of a map, the tag
444  // takes the place of the first element in the sequence.
445  if (StateStack.back() == inMapFirstKey) {
446  StateStack.pop_back();
447  StateStack.push_back(inMapOtherKey);
448  }
449  // Tags inside maps in sequences should act as keys in the map from a
450  // formatting perspective, so we always want a newline in a sequence.
451  NeedsNewLine = true;
452  }
453  }
454  return Use;
455 }
456 
458  StateStack.pop_back();
459 }
460 
461 std::vector<StringRef> Output::keys() {
462  report_fatal_error("invalid call");
463 }
464 
465 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
466  bool &UseDefault, void *&) {
467  UseDefault = false;
468  if (Required || !SameAsDefault || WriteDefaultValues) {
469  auto State = StateStack.back();
470  if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
471  flowKey(Key);
472  } else {
473  this->newLineCheck();
474  this->paddedKey(Key);
475  }
476  return true;
477  }
478  return false;
479 }
480 
481 void Output::postflightKey(void *) {
482  if (StateStack.back() == inMapFirstKey) {
483  StateStack.pop_back();
484  StateStack.push_back(inMapOtherKey);
485  } else if (StateStack.back() == inFlowMapFirstKey) {
486  StateStack.pop_back();
487  StateStack.push_back(inFlowMapOtherKey);
488  }
489 }
490 
492  StateStack.push_back(inFlowMapFirstKey);
493  this->newLineCheck();
494  ColumnAtMapFlowStart = Column;
495  output("{ ");
496 }
497 
499  StateStack.pop_back();
500  this->outputUpToEndOfLine(" }");
501 }
502 
504  this->outputUpToEndOfLine("---");
505 }
506 
507 bool Output::preflightDocument(unsigned index) {
508  if (index > 0)
509  this->outputUpToEndOfLine("\n---");
510  return true;
511 }
512 
514 }
515 
517  output("\n...\n");
518 }
519 
521  StateStack.push_back(inSeq);
522  NeedsNewLine = true;
523  return 0;
524 }
525 
527  StateStack.pop_back();
528 }
529 
530 bool Output::preflightElement(unsigned, void *&) {
531  return true;
532 }
533 
535 }
536 
538  StateStack.push_back(inFlowSeq);
539  this->newLineCheck();
540  ColumnAtFlowStart = Column;
541  output("[ ");
542  NeedFlowSequenceComma = false;
543  return 0;
544 }
545 
547  StateStack.pop_back();
548  this->outputUpToEndOfLine(" ]");
549 }
550 
551 bool Output::preflightFlowElement(unsigned, void *&) {
552  if (NeedFlowSequenceComma)
553  output(", ");
554  if (WrapColumn && Column > WrapColumn) {
555  output("\n");
556  for (int i = 0; i < ColumnAtFlowStart; ++i)
557  output(" ");
558  Column = ColumnAtFlowStart;
559  output(" ");
560  }
561  return true;
562 }
563 
565  NeedFlowSequenceComma = true;
566 }
567 
569  EnumerationMatchFound = false;
570 }
571 
572 bool Output::matchEnumScalar(const char *Str, bool Match) {
573  if (Match && !EnumerationMatchFound) {
574  this->newLineCheck();
575  this->outputUpToEndOfLine(Str);
576  EnumerationMatchFound = true;
577  }
578  return false;
579 }
580 
582  if (EnumerationMatchFound)
583  return false;
584  EnumerationMatchFound = true;
585  return true;
586 }
587 
589  if (!EnumerationMatchFound)
590  llvm_unreachable("bad runtime enum value");
591 }
592 
593 bool Output::beginBitSetScalar(bool &DoClear) {
594  this->newLineCheck();
595  output("[ ");
596  NeedBitValueComma = false;
597  DoClear = false;
598  return true;
599 }
600 
601 bool Output::bitSetMatch(const char *Str, bool Matches) {
602  if (Matches) {
603  if (NeedBitValueComma)
604  output(", ");
605  this->output(Str);
606  NeedBitValueComma = true;
607  }
608  return false;
609 }
610 
612  this->outputUpToEndOfLine(" ]");
613 }
614 
615 void Output::scalarString(StringRef &S, bool MustQuote) {
616  this->newLineCheck();
617  if (S.empty()) {
618  // Print '' for the empty string because leaving the field empty is not
619  // allowed.
620  this->outputUpToEndOfLine("''");
621  return;
622  }
623  if (!MustQuote) {
624  // Only quote if we must.
625  this->outputUpToEndOfLine(S);
626  return;
627  }
628  unsigned i = 0;
629  unsigned j = 0;
630  unsigned End = S.size();
631  output("'"); // Starting single quote.
632  const char *Base = S.data();
633  while (j < End) {
634  // Escape a single quote by doubling it.
635  if (S[j] == '\'') {
636  output(StringRef(&Base[i], j - i + 1));
637  output("'");
638  i = j + 1;
639  }
640  ++j;
641  }
642  output(StringRef(&Base[i], j - i));
643  this->outputUpToEndOfLine("'"); // Ending single quote.
644 }
645 
647  if (!StateStack.empty())
648  newLineCheck();
649  output(" |");
650  outputNewLine();
651 
652  unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
653 
654  auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
655  for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
656  for (unsigned I = 0; I < Indent; ++I) {
657  output(" ");
658  }
659  output(*Lines);
660  outputNewLine();
661  }
662 }
663 
664 void Output::setError(const Twine &message) {
665 }
666 
668  // Normally, with an optional key/value where the value is an empty sequence,
669  // the whole key/value can be not written. But, that produces wrong yaml
670  // if the key/value is the only thing in the map and the map is used in
671  // a sequence. This detects if the this sequence is the first key/value
672  // in map that itself is embedded in a sequnce.
673  if (StateStack.size() < 2)
674  return true;
675  if (StateStack.back() != inMapFirstKey)
676  return true;
677  return (StateStack[StateStack.size()-2] != inSeq);
678 }
679 
680 void Output::output(StringRef s) {
681  Column += s.size();
682  Out << s;
683 }
684 
685 void Output::outputUpToEndOfLine(StringRef s) {
686  this->output(s);
687  if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
688  StateStack.back() != inFlowMapFirstKey &&
689  StateStack.back() != inFlowMapOtherKey))
690  NeedsNewLine = true;
691 }
692 
693 void Output::outputNewLine() {
694  Out << "\n";
695  Column = 0;
696 }
697 
698 // if seq at top, indent as if map, then add "- "
699 // if seq in middle, use "- " if firstKey, else use " "
700 //
701 
702 void Output::newLineCheck() {
703  if (!NeedsNewLine)
704  return;
705  NeedsNewLine = false;
706 
707  this->outputNewLine();
708 
709  assert(StateStack.size() > 0);
710  unsigned Indent = StateStack.size() - 1;
711  bool OutputDash = false;
712 
713  if (StateStack.back() == inSeq) {
714  OutputDash = true;
715  } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
716  (StateStack.back() == inFlowSeq) ||
717  (StateStack.back() == inFlowMapFirstKey)) &&
718  (StateStack[StateStack.size() - 2] == inSeq)) {
719  --Indent;
720  OutputDash = true;
721  }
722 
723  for (unsigned i = 0; i < Indent; ++i) {
724  output(" ");
725  }
726  if (OutputDash) {
727  output("- ");
728  }
729 
730 }
731 
732 void Output::paddedKey(StringRef key) {
733  output(key);
734  output(":");
735  const char *spaces = " ";
736  if (key.size() < strlen(spaces))
737  output(&spaces[key.size()]);
738  else
739  output(" ");
740 }
741 
742 void Output::flowKey(StringRef Key) {
743  if (StateStack.back() == inFlowMapOtherKey)
744  output(", ");
745  if (WrapColumn && Column > WrapColumn) {
746  output("\n");
747  for (int I = 0; I < ColumnAtMapFlowStart; ++I)
748  output(" ");
749  Column = ColumnAtMapFlowStart;
750  output(" ");
751  }
752  output(Key);
753  output(": ");
754 }
755 
756 //===----------------------------------------------------------------------===//
757 // traits for built-in types
758 //===----------------------------------------------------------------------===//
759 
760 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
761  Out << (Val ? "true" : "false");
762 }
763 
765  if (Scalar.equals("true")) {
766  Val = true;
767  return StringRef();
768  } else if (Scalar.equals("false")) {
769  Val = false;
770  return StringRef();
771  }
772  return "invalid boolean";
773 }
774 
775 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
776  raw_ostream &Out) {
777  Out << Val;
778 }
779 
781  StringRef &Val) {
782  Val = Scalar;
783  return StringRef();
784 }
785 
786 void ScalarTraits<std::string>::output(const std::string &Val, void *,
787  raw_ostream &Out) {
788  Out << Val;
789 }
790 
792  std::string &Val) {
793  Val = Scalar.str();
794  return StringRef();
795 }
796 
797 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
798  raw_ostream &Out) {
799  // use temp uin32_t because ostream thinks uint8_t is a character
800  uint32_t Num = Val;
801  Out << Num;
802 }
803 
804 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
805  unsigned long long n;
806  if (getAsUnsignedInteger(Scalar, 0, n))
807  return "invalid number";
808  if (n > 0xFF)
809  return "out of range number";
810  Val = n;
811  return StringRef();
812 }
813 
814 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
815  raw_ostream &Out) {
816  Out << Val;
817 }
818 
820  uint16_t &Val) {
821  unsigned long long n;
822  if (getAsUnsignedInteger(Scalar, 0, n))
823  return "invalid number";
824  if (n > 0xFFFF)
825  return "out of range number";
826  Val = n;
827  return StringRef();
828 }
829 
830 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
831  raw_ostream &Out) {
832  Out << Val;
833 }
834 
836  uint32_t &Val) {
837  unsigned long long n;
838  if (getAsUnsignedInteger(Scalar, 0, n))
839  return "invalid number";
840  if (n > 0xFFFFFFFFUL)
841  return "out of range number";
842  Val = n;
843  return StringRef();
844 }
845 
846 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
847  raw_ostream &Out) {
848  Out << Val;
849 }
850 
852  uint64_t &Val) {
853  unsigned long long N;
854  if (getAsUnsignedInteger(Scalar, 0, N))
855  return "invalid number";
856  Val = N;
857  return StringRef();
858 }
859 
860 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
861  // use temp in32_t because ostream thinks int8_t is a character
862  int32_t Num = Val;
863  Out << Num;
864 }
865 
866 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
867  long long N;
868  if (getAsSignedInteger(Scalar, 0, N))
869  return "invalid number";
870  if ((N > 127) || (N < -128))
871  return "out of range number";
872  Val = N;
873  return StringRef();
874 }
875 
876 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
877  raw_ostream &Out) {
878  Out << Val;
879 }
880 
881 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
882  long long N;
883  if (getAsSignedInteger(Scalar, 0, N))
884  return "invalid number";
885  if ((N > INT16_MAX) || (N < INT16_MIN))
886  return "out of range number";
887  Val = N;
888  return StringRef();
889 }
890 
891 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
892  raw_ostream &Out) {
893  Out << Val;
894 }
895 
896 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
897  long long N;
898  if (getAsSignedInteger(Scalar, 0, N))
899  return "invalid number";
900  if ((N > INT32_MAX) || (N < INT32_MIN))
901  return "out of range number";
902  Val = N;
903  return StringRef();
904 }
905 
906 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
907  raw_ostream &Out) {
908  Out << Val;
909 }
910 
911 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
912  long long N;
913  if (getAsSignedInteger(Scalar, 0, N))
914  return "invalid number";
915  Val = N;
916  return StringRef();
917 }
918 
919 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
920  Out << format("%g", Val);
921 }
922 
923 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
924  if (to_float(Scalar, Val))
925  return StringRef();
926  return "invalid floating point number";
927 }
928 
929 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
930  Out << format("%g", Val);
931 }
932 
933 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
934  if (to_float(Scalar, Val))
935  return StringRef();
936  return "invalid floating point number";
937 }
938 
939 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
940  uint8_t Num = Val;
941  Out << format("0x%02X", Num);
942 }
943 
944 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
945  unsigned long long n;
946  if (getAsUnsignedInteger(Scalar, 0, n))
947  return "invalid hex8 number";
948  if (n > 0xFF)
949  return "out of range hex8 number";
950  Val = n;
951  return StringRef();
952 }
953 
954 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
955  uint16_t Num = Val;
956  Out << format("0x%04X", Num);
957 }
958 
959 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
960  unsigned long long n;
961  if (getAsUnsignedInteger(Scalar, 0, n))
962  return "invalid hex16 number";
963  if (n > 0xFFFF)
964  return "out of range hex16 number";
965  Val = n;
966  return StringRef();
967 }
968 
969 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
970  uint32_t Num = Val;
971  Out << format("0x%08X", Num);
972 }
973 
974 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
975  unsigned long long n;
976  if (getAsUnsignedInteger(Scalar, 0, n))
977  return "invalid hex32 number";
978  if (n > 0xFFFFFFFFUL)
979  return "out of range hex32 number";
980  Val = n;
981  return StringRef();
982 }
983 
984 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
985  uint64_t Num = Val;
986  Out << format("0x%016llX", Num);
987 }
988 
989 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
990  unsigned long long Num;
991  if (getAsUnsignedInteger(Scalar, 0, Num))
992  return "invalid hex64 number";
993  Val = Num;
994  return StringRef();
995 }
void setError(const Twine &message) override
Definition: YAMLTraits.cpp:664
void push_back(const T &Elt)
Definition: SmallVector.h:211
std::error_code error()
Definition: YAMLTraits.cpp:73
bool preflightElement(unsigned, void *&) override
Definition: YAMLTraits.cpp:530
bool preflightDocument(unsigned)
Definition: YAMLTraits.cpp:507
~Output() override
LLVMContext & Context
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
void beginMapping() override
Definition: YAMLTraits.cpp:423
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
Definition: StringRef.cpp:504
SourceMgr SrcMgr
Definition: Error.cpp:23
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
void endFlowMapping() override
Definition: YAMLTraits.cpp:498
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.
Definition: YAMLParser.h:443
void * getContext()
Definition: YAMLTraits.cpp:43
void endSequence() override
Definition: YAMLTraits.cpp:526
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:32
void beginFlowMapping() override
Definition: YAMLTraits.cpp:491
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
void postflightElement(void *) override
Definition: YAMLTraits.cpp:534
bool matchEnumScalar(const char *, bool) override
Definition: YAMLTraits.cpp:572
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 ...
Definition: StringRef.h:169
std::error_code make_error_code(BitcodeError E)
void scalarString(StringRef &, bool) override
Definition: YAMLTraits.cpp:615
bool matchEnumFallback() override
Definition: YAMLTraits.cpp:581
bool beginBitSetScalar(bool &) override
Definition: YAMLTraits.cpp:593
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
The Input class is used to parse a yaml document into in-memory structs and vectors.
Definition: YAMLTraits.h:1108
A key and value pair.
Definition: YAMLParser.h:272
bool outputting() override
Definition: YAMLTraits.cpp:419
bool to_float(const Twine &T, float &Num)
Definition: StringExtras.h:148
std::vector< StringRef > keys() override
Definition: YAMLTraits.cpp:461
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
bool mapTag(StringRef, bool) override
Definition: YAMLTraits.cpp:428
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
void endMapping() override
Definition: YAMLTraits.cpp:457
const Node * getCurrentNode() const
Returns the current node that's being parsed by the YAML Parser.
Definition: YAMLTraits.cpp:111
Function Alias Analysis false
#define P(N)
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition: SourceMgr.h:90
void postflightFlowElement(void *) override
Definition: YAMLTraits.cpp:564
static const unsigned End
bool setCurrentDocument()
Definition: YAMLTraits.cpp:86
void endFlowSequence() override
Definition: YAMLTraits.cpp:546
bool preflightKey(const char *key, bool, bool, bool &, void *&) override
Definition: YAMLTraits.cpp:465
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
Definition: SourceMgr.h:52
Output(raw_ostream &, void *Ctxt=nullptr, int WrapColumn=70)
Definition: YAMLTraits.cpp:414
A scalar node is an opaque datum that can be presented as a series of zero or more Unicode scalar val...
Definition: YAMLParser.h:199
bool preflightFlowElement(unsigned, void *&) override
Definition: YAMLTraits.cpp:551
virtual ~IO()
This class represents a YAML stream potentially containing multiple documents.
Definition: YAMLParser.h:81
This class should be specialized by type that requires custom conversion to/from a yaml scalar...
Definition: YAMLTraits.h:137
const size_t N
void endBitSetScalar() override
Definition: YAMLTraits.cpp:611
bool bitSetMatch(const char *, bool) override
Definition: YAMLTraits.cpp:601
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
void setContext(void *)
Definition: YAMLTraits.cpp:47
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
LLVM_NODISCARD StringRef copy(Allocator &A) const
Definition: StringRef.h:156
unsigned beginFlowSequence() override
Definition: YAMLTraits.cpp:537
llvm_unreachable("Unknown RegBank!")
IO(void *Ctxt=nullptr)
Definition: YAMLTraits.cpp:39
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
A block scalar node is an opaque datum that can be presented as a series of zero or more Unicode scal...
Definition: YAMLParser.h:242
Represents a YAML map created from either a block map for a flow map.
Definition: YAMLParser.h:395
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isNull(StringRef S)
Definition: YAMLTraits.h:488
LLVM Value Representation.
Definition: Value.h:73
Input(StringRef InputContent, void *Ctxt=nullptr, SourceMgr::DiagHandlerTy DiagHandler=nullptr, void *DiagHandlerCtxt=nullptr)
Definition: YAMLTraits.cpp:55
void beginEnumScalar() override
Definition: YAMLTraits.cpp:568
unsigned beginSequence() override
Definition: YAMLTraits.cpp:520
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
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).
Definition: StringRef.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void endEnumScalar() override
Definition: YAMLTraits.cpp:588
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
Definition: StringRef.cpp:514
void postflightKey(void *) override
Definition: YAMLTraits.cpp:481
void blockScalarString(StringRef &) override
Definition: YAMLTraits.cpp:646
bool canElideEmptySequence() override
Definition: YAMLTraits.cpp:667
~Input() override
Abstract base class for all Nodes.
Definition: YAMLParser.h:111
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:872