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