Don't use 'using std::error_code' in include/llvm.
[oota-llvm.git] / lib / Support / YAMLTraits.cpp
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
10 #include "llvm/Support/YAMLTraits.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/YAMLParser.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cctype>
18 #include <cstring>
19 using namespace llvm;
20 using namespace yaml;
21 using std::error_code;
22
23 //===----------------------------------------------------------------------===//
24 //  IO
25 //===----------------------------------------------------------------------===//
26
27 IO::IO(void *Context) : Ctxt(Context) {
28 }
29
30 IO::~IO() {
31 }
32
33 void *IO::getContext() {
34   return Ctxt;
35 }
36
37 void IO::setContext(void *Context) {
38   Ctxt = Context;
39 }
40
41 //===----------------------------------------------------------------------===//
42 //  Input
43 //===----------------------------------------------------------------------===//
44
45 Input::Input(StringRef InputContent,
46              void *Ctxt,
47              SourceMgr::DiagHandlerTy DiagHandler,
48              void *DiagHandlerCtxt)
49   : IO(Ctxt),
50     Strm(new Stream(InputContent, SrcMgr)),
51     CurrentNode(nullptr) {
52   if (DiagHandler)
53     SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
54   DocIterator = Strm->begin();
55 }
56
57 Input::~Input() {
58 }
59
60 error_code Input::error() {
61   return EC;
62 }
63
64 // Pin the vtables to this file.
65 void Input::HNode::anchor() {}
66 void Input::EmptyHNode::anchor() {}
67 void Input::ScalarHNode::anchor() {}
68
69 bool Input::outputting() {
70   return false;
71 }
72
73 bool Input::setCurrentDocument() {
74   if (DocIterator != Strm->end()) {
75     Node *N = DocIterator->getRoot();
76     if (!N) {
77       assert(Strm->failed() && "Root is NULL iff parsing failed");
78       EC = std::make_error_code(std::errc::invalid_argument);
79       return false;
80     }
81
82     if (isa<NullNode>(N)) {
83       // Empty files are allowed and ignored
84       ++DocIterator;
85       return setCurrentDocument();
86     }
87     TopNode.reset(this->createHNodes(N));
88     CurrentNode = TopNode.get();
89     return true;
90   }
91   return false;
92 }
93
94 bool Input::nextDocument() {
95   return ++DocIterator != Strm->end();
96 }
97
98 bool Input::mapTag(StringRef Tag, bool Default) {
99   std::string foundTag = CurrentNode->_node->getVerbatimTag();
100   if (foundTag.empty()) {
101     // If no tag found and 'Tag' is the default, say it was found.
102     return Default;
103   }
104   // Return true iff found tag matches supplied tag.
105   return Tag.equals(foundTag);
106 }
107
108 void Input::beginMapping() {
109   if (EC)
110     return;
111   // CurrentNode can be null if the document is empty.
112   MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
113   if (MN) {
114     MN->ValidKeys.clear();
115   }
116 }
117
118 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
119                          void *&SaveInfo) {
120   UseDefault = false;
121   if (EC)
122     return false;
123
124   // CurrentNode is null for empty documents, which is an error in case required
125   // nodes are present.
126   if (!CurrentNode) {
127     if (Required)
128       EC = std::make_error_code(std::errc::invalid_argument);
129     return false;
130   }
131
132   MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
133   if (!MN) {
134     setError(CurrentNode, "not a mapping");
135     return false;
136   }
137   MN->ValidKeys.push_back(Key);
138   HNode *Value = MN->Mapping[Key];
139   if (!Value) {
140     if (Required)
141       setError(CurrentNode, Twine("missing required key '") + Key + "'");
142     else
143       UseDefault = true;
144     return false;
145   }
146   SaveInfo = CurrentNode;
147   CurrentNode = Value;
148   return true;
149 }
150
151 void Input::postflightKey(void *saveInfo) {
152   CurrentNode = reinterpret_cast<HNode *>(saveInfo);
153 }
154
155 void Input::endMapping() {
156   if (EC)
157     return;
158   // CurrentNode can be null if the document is empty.
159   MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
160   if (!MN)
161     return;
162   for (const auto &NN : MN->Mapping) {
163     if (!MN->isValidKey(NN.first())) {
164       setError(NN.second, Twine("unknown key '") + NN.first() + "'");
165       break;
166     }
167   }
168 }
169
170 unsigned Input::beginSequence() {
171   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
172     return SQ->Entries.size();
173   }
174   return 0;
175 }
176
177 void Input::endSequence() {
178 }
179
180 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
181   if (EC)
182     return false;
183   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
184     SaveInfo = CurrentNode;
185     CurrentNode = SQ->Entries[Index];
186     return true;
187   }
188   return false;
189 }
190
191 void Input::postflightElement(void *SaveInfo) {
192   CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
193 }
194
195 unsigned Input::beginFlowSequence() {
196   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
197     return SQ->Entries.size();
198   }
199   return 0;
200 }
201
202 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
203   if (EC)
204     return false;
205   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
206     SaveInfo = CurrentNode;
207     CurrentNode = SQ->Entries[index];
208     return true;
209   }
210   return false;
211 }
212
213 void Input::postflightFlowElement(void *SaveInfo) {
214   CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
215 }
216
217 void Input::endFlowSequence() {
218 }
219
220 void Input::beginEnumScalar() {
221   ScalarMatchFound = false;
222 }
223
224 bool Input::matchEnumScalar(const char *Str, bool) {
225   if (ScalarMatchFound)
226     return false;
227   if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
228     if (SN->value().equals(Str)) {
229       ScalarMatchFound = true;
230       return true;
231     }
232   }
233   return false;
234 }
235
236 void Input::endEnumScalar() {
237   if (!ScalarMatchFound) {
238     setError(CurrentNode, "unknown enumerated scalar");
239   }
240 }
241
242 bool Input::beginBitSetScalar(bool &DoClear) {
243   BitValuesUsed.clear();
244   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
245     BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
246   } else {
247     setError(CurrentNode, "expected sequence of bit values");
248   }
249   DoClear = true;
250   return true;
251 }
252
253 bool Input::bitSetMatch(const char *Str, bool) {
254   if (EC)
255     return false;
256   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
257     unsigned Index = 0;
258     for (HNode *N : SQ->Entries) {
259       if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
260         if (SN->value().equals(Str)) {
261           BitValuesUsed[Index] = true;
262           return true;
263         }
264       } else {
265         setError(CurrentNode, "unexpected scalar in sequence of bit values");
266       }
267       ++Index;
268     }
269   } else {
270     setError(CurrentNode, "expected sequence of bit values");
271   }
272   return false;
273 }
274
275 void Input::endBitSetScalar() {
276   if (EC)
277     return;
278   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
279     assert(BitValuesUsed.size() == SQ->Entries.size());
280     for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
281       if (!BitValuesUsed[i]) {
282         setError(SQ->Entries[i], "unknown bit value");
283         return;
284       }
285     }
286   }
287 }
288
289 void Input::scalarString(StringRef &S, bool) {
290   if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
291     S = SN->value();
292   } else {
293     setError(CurrentNode, "unexpected scalar");
294   }
295 }
296
297 void Input::setError(HNode *hnode, const Twine &message) {
298   assert(hnode && "HNode must not be NULL");
299   this->setError(hnode->_node, message);
300 }
301
302 void Input::setError(Node *node, const Twine &message) {
303   Strm->printError(node, message);
304   EC = std::make_error_code(std::errc::invalid_argument);
305 }
306
307 Input::HNode *Input::createHNodes(Node *N) {
308   SmallString<128> StringStorage;
309   if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
310     StringRef KeyStr = SN->getValue(StringStorage);
311     if (!StringStorage.empty()) {
312       // Copy string to permanent storage
313       unsigned Len = StringStorage.size();
314       char *Buf = StringAllocator.Allocate<char>(Len);
315       memcpy(Buf, &StringStorage[0], Len);
316       KeyStr = StringRef(Buf, Len);
317     }
318     return new ScalarHNode(N, KeyStr);
319   } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
320     SequenceHNode *SQHNode = new SequenceHNode(N);
321     for (Node &SN : *SQ) {
322       HNode *Entry = this->createHNodes(&SN);
323       if (EC)
324         break;
325       SQHNode->Entries.push_back(Entry);
326     }
327     return SQHNode;
328   } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
329     MapHNode *mapHNode = new MapHNode(N);
330     for (KeyValueNode &KVN : *Map) {
331       ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
332       StringStorage.clear();
333       StringRef KeyStr = KeyScalar->getValue(StringStorage);
334       if (!StringStorage.empty()) {
335         // Copy string to permanent storage
336         unsigned Len = StringStorage.size();
337         char *Buf = StringAllocator.Allocate<char>(Len);
338         memcpy(Buf, &StringStorage[0], Len);
339         KeyStr = StringRef(Buf, Len);
340       }
341       HNode *ValueHNode = this->createHNodes(KVN.getValue());
342       if (EC)
343         break;
344       mapHNode->Mapping[KeyStr] = ValueHNode;
345     }
346     return mapHNode;
347   } else if (isa<NullNode>(N)) {
348     return new EmptyHNode(N);
349   } else {
350     setError(N, "unknown node kind");
351     return nullptr;
352   }
353 }
354
355 bool Input::MapHNode::isValidKey(StringRef Key) {
356   for (const char *K : ValidKeys) {
357     if (Key.equals(K))
358       return true;
359   }
360   return false;
361 }
362
363 void Input::setError(const Twine &Message) {
364   this->setError(CurrentNode, Message);
365 }
366
367 bool Input::canElideEmptySequence() {
368   return false;
369 }
370
371 Input::MapHNode::~MapHNode() {
372   for (auto &N : Mapping)
373     delete N.second;
374 }
375
376 Input::SequenceHNode::~SequenceHNode() {
377   for (HNode *N : Entries)
378     delete N;
379 }
380
381
382
383 //===----------------------------------------------------------------------===//
384 //  Output
385 //===----------------------------------------------------------------------===//
386
387 Output::Output(raw_ostream &yout, void *context)
388     : IO(context),
389       Out(yout),
390       Column(0),
391       ColumnAtFlowStart(0),
392       NeedBitValueComma(false),
393       NeedFlowSequenceComma(false),
394       EnumerationMatchFound(false),
395       NeedsNewLine(false) {
396 }
397
398 Output::~Output() {
399 }
400
401 bool Output::outputting() {
402   return true;
403 }
404
405 void Output::beginMapping() {
406   StateStack.push_back(inMapFirstKey);
407   NeedsNewLine = true;
408 }
409
410 bool Output::mapTag(StringRef Tag, bool Use) {
411   if (Use) {
412     this->output(" ");
413     this->output(Tag);
414   }
415   return Use;
416 }
417
418 void Output::endMapping() {
419   StateStack.pop_back();
420 }
421
422 bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
423                           bool &UseDefault, void *&) {
424   UseDefault = false;
425   if (Required || !SameAsDefault) {
426     this->newLineCheck();
427     this->paddedKey(Key);
428     return true;
429   }
430   return false;
431 }
432
433 void Output::postflightKey(void *) {
434   if (StateStack.back() == inMapFirstKey) {
435     StateStack.pop_back();
436     StateStack.push_back(inMapOtherKey);
437   }
438 }
439
440 void Output::beginDocuments() {
441   this->outputUpToEndOfLine("---");
442 }
443
444 bool Output::preflightDocument(unsigned index) {
445   if (index > 0)
446     this->outputUpToEndOfLine("\n---");
447   return true;
448 }
449
450 void Output::postflightDocument() {
451 }
452
453 void Output::endDocuments() {
454   output("\n...\n");
455 }
456
457 unsigned Output::beginSequence() {
458   StateStack.push_back(inSeq);
459   NeedsNewLine = true;
460   return 0;
461 }
462
463 void Output::endSequence() {
464   StateStack.pop_back();
465 }
466
467 bool Output::preflightElement(unsigned, void *&) {
468   return true;
469 }
470
471 void Output::postflightElement(void *) {
472 }
473
474 unsigned Output::beginFlowSequence() {
475   StateStack.push_back(inFlowSeq);
476   this->newLineCheck();
477   ColumnAtFlowStart = Column;
478   output("[ ");
479   NeedFlowSequenceComma = false;
480   return 0;
481 }
482
483 void Output::endFlowSequence() {
484   StateStack.pop_back();
485   this->outputUpToEndOfLine(" ]");
486 }
487
488 bool Output::preflightFlowElement(unsigned, void *&) {
489   if (NeedFlowSequenceComma)
490     output(", ");
491   if (Column > 70) {
492     output("\n");
493     for (int i = 0; i < ColumnAtFlowStart; ++i)
494       output(" ");
495     Column = ColumnAtFlowStart;
496     output("  ");
497   }
498   return true;
499 }
500
501 void Output::postflightFlowElement(void *) {
502   NeedFlowSequenceComma = true;
503 }
504
505 void Output::beginEnumScalar() {
506   EnumerationMatchFound = false;
507 }
508
509 bool Output::matchEnumScalar(const char *Str, bool Match) {
510   if (Match && !EnumerationMatchFound) {
511     this->newLineCheck();
512     this->outputUpToEndOfLine(Str);
513     EnumerationMatchFound = true;
514   }
515   return false;
516 }
517
518 void Output::endEnumScalar() {
519   if (!EnumerationMatchFound)
520     llvm_unreachable("bad runtime enum value");
521 }
522
523 bool Output::beginBitSetScalar(bool &DoClear) {
524   this->newLineCheck();
525   output("[ ");
526   NeedBitValueComma = false;
527   DoClear = false;
528   return true;
529 }
530
531 bool Output::bitSetMatch(const char *Str, bool Matches) {
532   if (Matches) {
533     if (NeedBitValueComma)
534       output(", ");
535     this->output(Str);
536     NeedBitValueComma = true;
537   }
538   return false;
539 }
540
541 void Output::endBitSetScalar() {
542   this->outputUpToEndOfLine(" ]");
543 }
544
545 void Output::scalarString(StringRef &S, bool MustQuote) {
546   this->newLineCheck();
547   if (S.empty()) {
548     // Print '' for the empty string because leaving the field empty is not
549     // allowed.
550     this->outputUpToEndOfLine("''");
551     return;
552   }
553   if (!MustQuote) {
554     // Only quote if we must.
555     this->outputUpToEndOfLine(S);
556     return;
557   }
558   unsigned i = 0;
559   unsigned j = 0;
560   unsigned End = S.size();
561   output("'"); // Starting single quote.
562   const char *Base = S.data();
563   while (j < End) {
564     // Escape a single quote by doubling it.
565     if (S[j] == '\'') {
566       output(StringRef(&Base[i], j - i + 1));
567       output("'");
568       i = j + 1;
569     }
570     ++j;
571   }
572   output(StringRef(&Base[i], j - i));
573   this->outputUpToEndOfLine("'"); // Ending single quote.
574 }
575
576 void Output::setError(const Twine &message) {
577 }
578
579 bool Output::canElideEmptySequence() {
580   // Normally, with an optional key/value where the value is an empty sequence,
581   // the whole key/value can be not written.  But, that produces wrong yaml
582   // if the key/value is the only thing in the map and the map is used in
583   // a sequence.  This detects if the this sequence is the first key/value
584   // in map that itself is embedded in a sequnce.
585   if (StateStack.size() < 2)
586     return true;
587   if (StateStack.back() != inMapFirstKey)
588     return true;
589   return (StateStack[StateStack.size()-2] != inSeq);
590 }
591
592 void Output::output(StringRef s) {
593   Column += s.size();
594   Out << s;
595 }
596
597 void Output::outputUpToEndOfLine(StringRef s) {
598   this->output(s);
599   if (StateStack.empty() || StateStack.back() != inFlowSeq)
600     NeedsNewLine = true;
601 }
602
603 void Output::outputNewLine() {
604   Out << "\n";
605   Column = 0;
606 }
607
608 // if seq at top, indent as if map, then add "- "
609 // if seq in middle, use "- " if firstKey, else use "  "
610 //
611
612 void Output::newLineCheck() {
613   if (!NeedsNewLine)
614     return;
615   NeedsNewLine = false;
616
617   this->outputNewLine();
618
619   assert(StateStack.size() > 0);
620   unsigned Indent = StateStack.size() - 1;
621   bool OutputDash = false;
622
623   if (StateStack.back() == inSeq) {
624     OutputDash = true;
625   } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
626              (StateStack[StateStack.size() - 2] == inSeq)) {
627     --Indent;
628     OutputDash = true;
629   }
630
631   for (unsigned i = 0; i < Indent; ++i) {
632     output("  ");
633   }
634   if (OutputDash) {
635     output("- ");
636   }
637
638 }
639
640 void Output::paddedKey(StringRef key) {
641   output(key);
642   output(":");
643   const char *spaces = "                ";
644   if (key.size() < strlen(spaces))
645     output(&spaces[key.size()]);
646   else
647     output(" ");
648 }
649
650 //===----------------------------------------------------------------------===//
651 //  traits for built-in types
652 //===----------------------------------------------------------------------===//
653
654 void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
655   Out << (Val ? "true" : "false");
656 }
657
658 StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
659   if (Scalar.equals("true")) {
660     Val = true;
661     return StringRef();
662   } else if (Scalar.equals("false")) {
663     Val = false;
664     return StringRef();
665   }
666   return "invalid boolean";
667 }
668
669 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
670                                      raw_ostream &Out) {
671   Out << Val;
672 }
673
674 StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
675                                          StringRef &Val) {
676   Val = Scalar;
677   return StringRef();
678 }
679  
680 void ScalarTraits<std::string>::output(const std::string &Val, void *,
681                                      raw_ostream &Out) {
682   Out << Val;
683 }
684
685 StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
686                                          std::string &Val) {
687   Val = Scalar.str();
688   return StringRef();
689 }
690
691 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
692                                    raw_ostream &Out) {
693   // use temp uin32_t because ostream thinks uint8_t is a character
694   uint32_t Num = Val;
695   Out << Num;
696 }
697
698 StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
699   unsigned long long n;
700   if (getAsUnsignedInteger(Scalar, 0, n))
701     return "invalid number";
702   if (n > 0xFF)
703     return "out of range number";
704   Val = n;
705   return StringRef();
706 }
707
708 void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
709                                     raw_ostream &Out) {
710   Out << Val;
711 }
712
713 StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
714                                         uint16_t &Val) {
715   unsigned long long n;
716   if (getAsUnsignedInteger(Scalar, 0, n))
717     return "invalid number";
718   if (n > 0xFFFF)
719     return "out of range number";
720   Val = n;
721   return StringRef();
722 }
723
724 void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
725                                     raw_ostream &Out) {
726   Out << Val;
727 }
728
729 StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
730                                         uint32_t &Val) {
731   unsigned long long n;
732   if (getAsUnsignedInteger(Scalar, 0, n))
733     return "invalid number";
734   if (n > 0xFFFFFFFFUL)
735     return "out of range number";
736   Val = n;
737   return StringRef();
738 }
739
740 void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
741                                     raw_ostream &Out) {
742   Out << Val;
743 }
744
745 StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
746                                         uint64_t &Val) {
747   unsigned long long N;
748   if (getAsUnsignedInteger(Scalar, 0, N))
749     return "invalid number";
750   Val = N;
751   return StringRef();
752 }
753
754 void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
755   // use temp in32_t because ostream thinks int8_t is a character
756   int32_t Num = Val;
757   Out << Num;
758 }
759
760 StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
761   long long N;
762   if (getAsSignedInteger(Scalar, 0, N))
763     return "invalid number";
764   if ((N > 127) || (N < -128))
765     return "out of range number";
766   Val = N;
767   return StringRef();
768 }
769
770 void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
771                                    raw_ostream &Out) {
772   Out << Val;
773 }
774
775 StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
776   long long N;
777   if (getAsSignedInteger(Scalar, 0, N))
778     return "invalid number";
779   if ((N > INT16_MAX) || (N < INT16_MIN))
780     return "out of range number";
781   Val = N;
782   return StringRef();
783 }
784
785 void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
786                                    raw_ostream &Out) {
787   Out << Val;
788 }
789
790 StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
791   long long N;
792   if (getAsSignedInteger(Scalar, 0, N))
793     return "invalid number";
794   if ((N > INT32_MAX) || (N < INT32_MIN))
795     return "out of range number";
796   Val = N;
797   return StringRef();
798 }
799
800 void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
801                                    raw_ostream &Out) {
802   Out << Val;
803 }
804
805 StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
806   long long N;
807   if (getAsSignedInteger(Scalar, 0, N))
808     return "invalid number";
809   Val = N;
810   return StringRef();
811 }
812
813 void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
814   Out << format("%g", Val);
815 }
816
817 StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
818   SmallString<32> buff(Scalar.begin(), Scalar.end());
819   char *end;
820   Val = strtod(buff.c_str(), &end);
821   if (*end != '\0')
822     return "invalid floating point number";
823   return StringRef();
824 }
825
826 void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
827   Out << format("%g", Val);
828 }
829
830 StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
831   SmallString<32> buff(Scalar.begin(), Scalar.end());
832   char *end;
833   Val = strtod(buff.c_str(), &end);
834   if (*end != '\0')
835     return "invalid floating point number";
836   return StringRef();
837 }
838
839 void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
840   uint8_t Num = Val;
841   Out << format("0x%02X", Num);
842 }
843
844 StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
845   unsigned long long n;
846   if (getAsUnsignedInteger(Scalar, 0, n))
847     return "invalid hex8 number";
848   if (n > 0xFF)
849     return "out of range hex8 number";
850   Val = n;
851   return StringRef();
852 }
853
854 void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
855   uint16_t Num = Val;
856   Out << format("0x%04X", Num);
857 }
858
859 StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
860   unsigned long long n;
861   if (getAsUnsignedInteger(Scalar, 0, n))
862     return "invalid hex16 number";
863   if (n > 0xFFFF)
864     return "out of range hex16 number";
865   Val = n;
866   return StringRef();
867 }
868
869 void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
870   uint32_t Num = Val;
871   Out << format("0x%08X", Num);
872 }
873
874 StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
875   unsigned long long n;
876   if (getAsUnsignedInteger(Scalar, 0, n))
877     return "invalid hex32 number";
878   if (n > 0xFFFFFFFFUL)
879     return "out of range hex32 number";
880   Val = n;
881   return StringRef();
882 }
883
884 void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
885   uint64_t Num = Val;
886   Out << format("0x%016llX", Num);
887 }
888
889 StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
890   unsigned long long Num;
891   if (getAsUnsignedInteger(Scalar, 0, Num))
892     return "invalid hex64 number";
893   Val = Num;
894   return StringRef();
895 }