1 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/YAMLTraits.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/LineIterator.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Unicode.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Support/raw_ostream.h"
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 IO::IO(void *Context
) : Ctxt(Context
) {}
43 void *IO::getContext() {
47 void IO::setContext(void *Context
) {
51 //===----------------------------------------------------------------------===//
53 //===----------------------------------------------------------------------===//
55 Input::Input(StringRef InputContent
, void *Ctxt
,
56 SourceMgr::DiagHandlerTy DiagHandler
, void *DiagHandlerCtxt
)
57 : IO(Ctxt
), Strm(new Stream(InputContent
, SrcMgr
, false, &EC
)) {
59 SrcMgr
.setDiagHandler(DiagHandler
, DiagHandlerCtxt
);
60 DocIterator
= Strm
->begin();
63 Input::Input(MemoryBufferRef Input
, void *Ctxt
,
64 SourceMgr::DiagHandlerTy DiagHandler
, void *DiagHandlerCtxt
)
65 : IO(Ctxt
), Strm(new Stream(Input
, SrcMgr
, false, &EC
)) {
67 SrcMgr
.setDiagHandler(DiagHandler
, DiagHandlerCtxt
);
68 DocIterator
= Strm
->begin();
71 Input::~Input() = default;
73 std::error_code
Input::error() { return EC
; }
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() {}
82 bool Input::outputting() {
86 bool Input::setCurrentDocument() {
87 if (DocIterator
!= Strm
->end()) {
88 Node
*N
= DocIterator
->getRoot();
90 assert(Strm
->failed() && "Root is NULL iff parsing failed");
91 EC
= make_error_code(errc::invalid_argument
);
95 if (isa
<NullNode
>(N
)) {
96 // Empty files are allowed and ignored
98 return setCurrentDocument();
100 TopNode
= createHNodes(N
);
101 CurrentNode
= TopNode
.get();
107 bool Input::nextDocument() {
108 return ++DocIterator
!= Strm
->end();
111 const Node
*Input::getCurrentNode() const {
112 return CurrentNode
? CurrentNode
->_node
: nullptr;
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.
121 // Return true iff found tag matches supplied tag.
122 return Tag
.equals(foundTag
);
125 void Input::beginMapping() {
128 // CurrentNode can be null if the document is empty.
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
,
153 // CurrentNode is null for empty documents, which is an error in case required
154 // nodes are present.
157 EC
= make_error_code(errc::invalid_argument
);
161 MapHNode
*MN
= dyn_cast
<MapHNode
>(CurrentNode
);
163 if (Required
|| !isa
<EmptyHNode
>(CurrentNode
))
164 setError(CurrentNode
, "not a mapping");
167 MN
->ValidKeys
.push_back(Key
);
168 HNode
*Value
= MN
->Mapping
[Key
].get();
171 setError(CurrentNode
, Twine("missing required key '") + Key
+ "'");
176 SaveInfo
= CurrentNode
;
181 void Input::postflightKey(void *saveInfo
) {
182 CurrentNode
= reinterpret_cast<HNode
*>(saveInfo
);
185 void Input::endMapping() {
188 // CurrentNode can be null if the document is empty.
189 MapHNode
*MN
= dyn_cast_or_null
<MapHNode
>(CurrentNode
);
192 for (const auto &NN
: MN
->Mapping
) {
193 if (!is_contained(MN
->ValidKeys
, NN
.first())) {
194 setError(NN
.second
.get(), Twine("unknown key '") + NN
.first() + "'");
200 void Input::beginFlowMapping() { beginMapping(); }
202 void Input::endFlowMapping() { endMapping(); }
204 unsigned Input::beginSequence() {
205 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
))
206 return SQ
->Entries
.size();
207 if (isa
<EmptyHNode
>(CurrentNode
))
209 // Treat case where there's a scalar "null" value as an empty sequence.
210 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(CurrentNode
)) {
211 if (isNull(SN
->value()))
214 // Any other type of HNode is an error.
215 setError(CurrentNode
, "not a sequence");
219 void Input::endSequence() {
222 bool Input::preflightElement(unsigned Index
, void *&SaveInfo
) {
225 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
226 SaveInfo
= CurrentNode
;
227 CurrentNode
= SQ
->Entries
[Index
].get();
233 void Input::postflightElement(void *SaveInfo
) {
234 CurrentNode
= reinterpret_cast<HNode
*>(SaveInfo
);
237 unsigned Input::beginFlowSequence() { return beginSequence(); }
239 bool Input::preflightFlowElement(unsigned index
, void *&SaveInfo
) {
242 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
243 SaveInfo
= CurrentNode
;
244 CurrentNode
= SQ
->Entries
[index
].get();
250 void Input::postflightFlowElement(void *SaveInfo
) {
251 CurrentNode
= reinterpret_cast<HNode
*>(SaveInfo
);
254 void Input::endFlowSequence() {
257 void Input::beginEnumScalar() {
258 ScalarMatchFound
= false;
261 bool Input::matchEnumScalar(const char *Str
, bool) {
262 if (ScalarMatchFound
)
264 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(CurrentNode
)) {
265 if (SN
->value().equals(Str
)) {
266 ScalarMatchFound
= true;
273 bool Input::matchEnumFallback() {
274 if (ScalarMatchFound
)
276 ScalarMatchFound
= true;
280 void Input::endEnumScalar() {
281 if (!ScalarMatchFound
) {
282 setError(CurrentNode
, "unknown enumerated scalar");
286 bool Input::beginBitSetScalar(bool &DoClear
) {
287 BitValuesUsed
.clear();
288 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
289 BitValuesUsed
.insert(BitValuesUsed
.begin(), SQ
->Entries
.size(), false);
291 setError(CurrentNode
, "expected sequence of bit values");
297 bool Input::bitSetMatch(const char *Str
, bool) {
300 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
302 for (auto &N
: SQ
->Entries
) {
303 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(N
.get())) {
304 if (SN
->value().equals(Str
)) {
305 BitValuesUsed
[Index
] = true;
309 setError(CurrentNode
, "unexpected scalar in sequence of bit values");
314 setError(CurrentNode
, "expected sequence of bit values");
319 void Input::endBitSetScalar() {
322 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
323 assert(BitValuesUsed
.size() == SQ
->Entries
.size());
324 for (unsigned i
= 0; i
< SQ
->Entries
.size(); ++i
) {
325 if (!BitValuesUsed
[i
]) {
326 setError(SQ
->Entries
[i
].get(), "unknown bit value");
333 void Input::scalarString(StringRef
&S
, QuotingType
) {
334 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(CurrentNode
)) {
337 setError(CurrentNode
, "unexpected scalar");
341 void Input::blockScalarString(StringRef
&S
) { scalarString(S
, QuotingType::None
); }
343 void Input::scalarTag(std::string
&Tag
) {
344 Tag
= CurrentNode
->_node
->getVerbatimTag();
347 void Input::setError(HNode
*hnode
, const Twine
&message
) {
348 assert(hnode
&& "HNode must not be NULL");
349 setError(hnode
->_node
, message
);
352 NodeKind
Input::getNodeKind() {
353 if (isa
<ScalarHNode
>(CurrentNode
))
354 return NodeKind::Scalar
;
355 else if (isa
<MapHNode
>(CurrentNode
))
356 return NodeKind::Map
;
357 else if (isa
<SequenceHNode
>(CurrentNode
))
358 return NodeKind::Sequence
;
359 llvm_unreachable("Unsupported node kind");
362 void Input::setError(Node
*node
, const Twine
&message
) {
363 Strm
->printError(node
, message
);
364 EC
= make_error_code(errc::invalid_argument
);
367 std::unique_ptr
<Input::HNode
> Input::createHNodes(Node
*N
) {
368 SmallString
<128> StringStorage
;
369 if (ScalarNode
*SN
= dyn_cast
<ScalarNode
>(N
)) {
370 StringRef KeyStr
= SN
->getValue(StringStorage
);
371 if (!StringStorage
.empty()) {
372 // Copy string to permanent storage
373 KeyStr
= StringStorage
.str().copy(StringAllocator
);
375 return llvm::make_unique
<ScalarHNode
>(N
, KeyStr
);
376 } else if (BlockScalarNode
*BSN
= dyn_cast
<BlockScalarNode
>(N
)) {
377 StringRef ValueCopy
= BSN
->getValue().copy(StringAllocator
);
378 return llvm::make_unique
<ScalarHNode
>(N
, ValueCopy
);
379 } else if (SequenceNode
*SQ
= dyn_cast
<SequenceNode
>(N
)) {
380 auto SQHNode
= llvm::make_unique
<SequenceHNode
>(N
);
381 for (Node
&SN
: *SQ
) {
382 auto Entry
= createHNodes(&SN
);
385 SQHNode
->Entries
.push_back(std::move(Entry
));
387 return std::move(SQHNode
);
388 } else if (MappingNode
*Map
= dyn_cast
<MappingNode
>(N
)) {
389 auto mapHNode
= llvm::make_unique
<MapHNode
>(N
);
390 for (KeyValueNode
&KVN
: *Map
) {
391 Node
*KeyNode
= KVN
.getKey();
392 ScalarNode
*Key
= dyn_cast
<ScalarNode
>(KeyNode
);
393 Node
*Value
= KVN
.getValue();
394 if (!Key
|| !Value
) {
396 setError(KeyNode
, "Map key must be a scalar");
398 setError(KeyNode
, "Map value must not be empty");
401 StringStorage
.clear();
402 StringRef KeyStr
= Key
->getValue(StringStorage
);
403 if (!StringStorage
.empty()) {
404 // Copy string to permanent storage
405 KeyStr
= StringStorage
.str().copy(StringAllocator
);
407 auto ValueHNode
= createHNodes(Value
);
410 mapHNode
->Mapping
[KeyStr
] = std::move(ValueHNode
);
412 return std::move(mapHNode
);
413 } else if (isa
<NullNode
>(N
)) {
414 return llvm::make_unique
<EmptyHNode
>(N
);
416 setError(N
, "unknown node kind");
421 void Input::setError(const Twine
&Message
) {
422 setError(CurrentNode
, Message
);
425 bool Input::canElideEmptySequence() {
429 //===----------------------------------------------------------------------===//
431 //===----------------------------------------------------------------------===//
433 Output::Output(raw_ostream
&yout
, void *context
, int WrapColumn
)
434 : IO(context
), Out(yout
), WrapColumn(WrapColumn
) {}
436 Output::~Output() = default;
438 bool Output::outputting() {
442 void Output::beginMapping() {
443 StateStack
.push_back(inMapFirstKey
);
447 bool Output::mapTag(StringRef Tag
, bool Use
) {
449 // If this tag is being written inside a sequence we should write the start
450 // of the sequence before writing the tag, otherwise the tag won't be
451 // attached to the element in the sequence, but rather the sequence itself.
452 bool SequenceElement
= false;
453 if (StateStack
.size() > 1) {
454 auto &E
= StateStack
[StateStack
.size() - 2];
455 SequenceElement
= inSeqAnyElement(E
) || inFlowSeqAnyElement(E
);
457 if (SequenceElement
&& StateStack
.back() == inMapFirstKey
) {
463 if (SequenceElement
) {
464 // If we're writing the tag during the first element of a map, the tag
465 // takes the place of the first element in the sequence.
466 if (StateStack
.back() == inMapFirstKey
) {
467 StateStack
.pop_back();
468 StateStack
.push_back(inMapOtherKey
);
470 // Tags inside maps in sequences should act as keys in the map from a
471 // formatting perspective, so we always want a newline in a sequence.
478 void Output::endMapping() {
479 // If we did not map anything, we should explicitly emit an empty map
480 if (StateStack
.back() == inMapFirstKey
)
482 StateStack
.pop_back();
485 std::vector
<StringRef
> Output::keys() {
486 report_fatal_error("invalid call");
489 bool Output::preflightKey(const char *Key
, bool Required
, bool SameAsDefault
,
490 bool &UseDefault
, void *&) {
492 if (Required
|| !SameAsDefault
|| WriteDefaultValues
) {
493 auto State
= StateStack
.back();
494 if (State
== inFlowMapFirstKey
|| State
== inFlowMapOtherKey
) {
505 void Output::postflightKey(void *) {
506 if (StateStack
.back() == inMapFirstKey
) {
507 StateStack
.pop_back();
508 StateStack
.push_back(inMapOtherKey
);
509 } else if (StateStack
.back() == inFlowMapFirstKey
) {
510 StateStack
.pop_back();
511 StateStack
.push_back(inFlowMapOtherKey
);
515 void Output::beginFlowMapping() {
516 StateStack
.push_back(inFlowMapFirstKey
);
518 ColumnAtMapFlowStart
= Column
;
522 void Output::endFlowMapping() {
523 StateStack
.pop_back();
524 outputUpToEndOfLine(" }");
527 void Output::beginDocuments() {
528 outputUpToEndOfLine("---");
531 bool Output::preflightDocument(unsigned index
) {
533 outputUpToEndOfLine("\n---");
537 void Output::postflightDocument() {
540 void Output::endDocuments() {
544 unsigned Output::beginSequence() {
545 StateStack
.push_back(inSeqFirstElement
);
550 void Output::endSequence() {
551 // If we did not emit anything, we should explicitly emit an empty sequence
552 if (StateStack
.back() == inSeqFirstElement
)
554 StateStack
.pop_back();
557 bool Output::preflightElement(unsigned, void *&) {
561 void Output::postflightElement(void *) {
562 if (StateStack
.back() == inSeqFirstElement
) {
563 StateStack
.pop_back();
564 StateStack
.push_back(inSeqOtherElement
);
565 } else if (StateStack
.back() == inFlowSeqFirstElement
) {
566 StateStack
.pop_back();
567 StateStack
.push_back(inFlowSeqOtherElement
);
571 unsigned Output::beginFlowSequence() {
572 StateStack
.push_back(inFlowSeqFirstElement
);
574 ColumnAtFlowStart
= Column
;
576 NeedFlowSequenceComma
= false;
580 void Output::endFlowSequence() {
581 StateStack
.pop_back();
582 outputUpToEndOfLine(" ]");
585 bool Output::preflightFlowElement(unsigned, void *&) {
586 if (NeedFlowSequenceComma
)
588 if (WrapColumn
&& Column
> WrapColumn
) {
590 for (int i
= 0; i
< ColumnAtFlowStart
; ++i
)
592 Column
= ColumnAtFlowStart
;
598 void Output::postflightFlowElement(void *) {
599 NeedFlowSequenceComma
= true;
602 void Output::beginEnumScalar() {
603 EnumerationMatchFound
= false;
606 bool Output::matchEnumScalar(const char *Str
, bool Match
) {
607 if (Match
&& !EnumerationMatchFound
) {
609 outputUpToEndOfLine(Str
);
610 EnumerationMatchFound
= true;
615 bool Output::matchEnumFallback() {
616 if (EnumerationMatchFound
)
618 EnumerationMatchFound
= true;
622 void Output::endEnumScalar() {
623 if (!EnumerationMatchFound
)
624 llvm_unreachable("bad runtime enum value");
627 bool Output::beginBitSetScalar(bool &DoClear
) {
630 NeedBitValueComma
= false;
635 bool Output::bitSetMatch(const char *Str
, bool Matches
) {
637 if (NeedBitValueComma
)
640 NeedBitValueComma
= true;
645 void Output::endBitSetScalar() {
646 outputUpToEndOfLine(" ]");
649 void Output::scalarString(StringRef
&S
, QuotingType MustQuote
) {
652 // Print '' for the empty string because leaving the field empty is not
654 outputUpToEndOfLine("''");
657 if (MustQuote
== QuotingType::None
) {
658 // Only quote if we must.
659 outputUpToEndOfLine(S
);
665 unsigned End
= S
.size();
666 const char *Base
= S
.data();
668 const char *const Quote
= MustQuote
== QuotingType::Single
? "'" : "\"";
669 output(Quote
); // Starting quote.
671 // When using double-quoted strings (and only in that case), non-printable characters may be
672 // present, and will be escaped using a variety of unicode-scalar and special short-form
673 // escapes. This is handled in yaml::escape.
674 if (MustQuote
== QuotingType::Double
) {
675 output(yaml::escape(Base
, /* EscapePrintable= */ false));
676 outputUpToEndOfLine(Quote
);
680 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
682 if (S
[j
] == '\'') { // Escape quotes.
683 output(StringRef(&Base
[i
], j
- i
)); // "flush".
684 output(StringLiteral("''")); // Print it as ''
689 output(StringRef(&Base
[i
], j
- i
));
690 outputUpToEndOfLine(Quote
); // Ending quote.
693 void Output::blockScalarString(StringRef
&S
) {
694 if (!StateStack
.empty())
699 unsigned Indent
= StateStack
.empty() ? 1 : StateStack
.size();
701 auto Buffer
= MemoryBuffer::getMemBuffer(S
, "", false);
702 for (line_iterator
Lines(*Buffer
, false); !Lines
.is_at_end(); ++Lines
) {
703 for (unsigned I
= 0; I
< Indent
; ++I
) {
711 void Output::scalarTag(std::string
&Tag
) {
719 void Output::setError(const Twine
&message
) {
722 bool Output::canElideEmptySequence() {
723 // Normally, with an optional key/value where the value is an empty sequence,
724 // the whole key/value can be not written. But, that produces wrong yaml
725 // if the key/value is the only thing in the map and the map is used in
726 // a sequence. This detects if the this sequence is the first key/value
727 // in map that itself is embedded in a sequnce.
728 if (StateStack
.size() < 2)
730 if (StateStack
.back() != inMapFirstKey
)
732 return !inSeqAnyElement(StateStack
[StateStack
.size() - 2]);
735 void Output::output(StringRef s
) {
740 void Output::outputUpToEndOfLine(StringRef s
) {
742 if (StateStack
.empty() || (!inFlowSeqAnyElement(StateStack
.back()) &&
743 !inFlowMapAnyKey(StateStack
.back())))
747 void Output::outputNewLine() {
752 // if seq at top, indent as if map, then add "- "
753 // if seq in middle, use "- " if firstKey, else use " "
756 void Output::newLineCheck() {
759 NeedsNewLine
= false;
763 if (StateStack
.size() == 0)
766 unsigned Indent
= StateStack
.size() - 1;
767 bool OutputDash
= false;
769 if (StateStack
.back() == inSeqFirstElement
||
770 StateStack
.back() == inSeqOtherElement
) {
772 } else if ((StateStack
.size() > 1) &&
773 ((StateStack
.back() == inMapFirstKey
) ||
774 inFlowSeqAnyElement(StateStack
.back()) ||
775 (StateStack
.back() == inFlowMapFirstKey
)) &&
776 inSeqAnyElement(StateStack
[StateStack
.size() - 2])) {
781 for (unsigned i
= 0; i
< Indent
; ++i
) {
790 void Output::paddedKey(StringRef key
) {
793 const char *spaces
= " ";
794 if (key
.size() < strlen(spaces
))
795 output(&spaces
[key
.size()]);
800 void Output::flowKey(StringRef Key
) {
801 if (StateStack
.back() == inFlowMapOtherKey
)
803 if (WrapColumn
&& Column
> WrapColumn
) {
805 for (int I
= 0; I
< ColumnAtMapFlowStart
; ++I
)
807 Column
= ColumnAtMapFlowStart
;
814 NodeKind
Output::getNodeKind() { report_fatal_error("invalid call"); }
816 bool Output::inSeqAnyElement(InState State
) {
817 return State
== inSeqFirstElement
|| State
== inSeqOtherElement
;
820 bool Output::inFlowSeqAnyElement(InState State
) {
821 return State
== inFlowSeqFirstElement
|| State
== inFlowSeqOtherElement
;
824 bool Output::inMapAnyKey(InState State
) {
825 return State
== inMapFirstKey
|| State
== inMapOtherKey
;
828 bool Output::inFlowMapAnyKey(InState State
) {
829 return State
== inFlowMapFirstKey
|| State
== inFlowMapOtherKey
;
832 //===----------------------------------------------------------------------===//
833 // traits for built-in types
834 //===----------------------------------------------------------------------===//
836 void ScalarTraits
<bool>::output(const bool &Val
, void *, raw_ostream
&Out
) {
837 Out
<< (Val
? "true" : "false");
840 StringRef ScalarTraits
<bool>::input(StringRef Scalar
, void *, bool &Val
) {
841 if (Scalar
.equals("true")) {
844 } else if (Scalar
.equals("false")) {
848 return "invalid boolean";
851 void ScalarTraits
<StringRef
>::output(const StringRef
&Val
, void *,
856 StringRef ScalarTraits
<StringRef
>::input(StringRef Scalar
, void *,
862 void ScalarTraits
<std::string
>::output(const std::string
&Val
, void *,
867 StringRef ScalarTraits
<std::string
>::input(StringRef Scalar
, void *,
873 void ScalarTraits
<uint8_t>::output(const uint8_t &Val
, void *,
875 // use temp uin32_t because ostream thinks uint8_t is a character
880 StringRef ScalarTraits
<uint8_t>::input(StringRef Scalar
, void *, uint8_t &Val
) {
881 unsigned long long n
;
882 if (getAsUnsignedInteger(Scalar
, 0, n
))
883 return "invalid number";
885 return "out of range number";
890 void ScalarTraits
<uint16_t>::output(const uint16_t &Val
, void *,
895 StringRef ScalarTraits
<uint16_t>::input(StringRef Scalar
, void *,
897 unsigned long long n
;
898 if (getAsUnsignedInteger(Scalar
, 0, n
))
899 return "invalid number";
901 return "out of range number";
906 void ScalarTraits
<uint32_t>::output(const uint32_t &Val
, void *,
911 StringRef ScalarTraits
<uint32_t>::input(StringRef Scalar
, void *,
913 unsigned long long n
;
914 if (getAsUnsignedInteger(Scalar
, 0, n
))
915 return "invalid number";
916 if (n
> 0xFFFFFFFFUL
)
917 return "out of range number";
922 void ScalarTraits
<uint64_t>::output(const uint64_t &Val
, void *,
927 StringRef ScalarTraits
<uint64_t>::input(StringRef Scalar
, void *,
929 unsigned long long N
;
930 if (getAsUnsignedInteger(Scalar
, 0, N
))
931 return "invalid number";
936 void ScalarTraits
<int8_t>::output(const int8_t &Val
, void *, raw_ostream
&Out
) {
937 // use temp in32_t because ostream thinks int8_t is a character
942 StringRef ScalarTraits
<int8_t>::input(StringRef Scalar
, void *, int8_t &Val
) {
944 if (getAsSignedInteger(Scalar
, 0, N
))
945 return "invalid number";
946 if ((N
> 127) || (N
< -128))
947 return "out of range number";
952 void ScalarTraits
<int16_t>::output(const int16_t &Val
, void *,
957 StringRef ScalarTraits
<int16_t>::input(StringRef Scalar
, void *, int16_t &Val
) {
959 if (getAsSignedInteger(Scalar
, 0, N
))
960 return "invalid number";
961 if ((N
> INT16_MAX
) || (N
< INT16_MIN
))
962 return "out of range number";
967 void ScalarTraits
<int32_t>::output(const int32_t &Val
, void *,
972 StringRef ScalarTraits
<int32_t>::input(StringRef Scalar
, void *, int32_t &Val
) {
974 if (getAsSignedInteger(Scalar
, 0, N
))
975 return "invalid number";
976 if ((N
> INT32_MAX
) || (N
< INT32_MIN
))
977 return "out of range number";
982 void ScalarTraits
<int64_t>::output(const int64_t &Val
, void *,
987 StringRef ScalarTraits
<int64_t>::input(StringRef Scalar
, void *, int64_t &Val
) {
989 if (getAsSignedInteger(Scalar
, 0, N
))
990 return "invalid number";
995 void ScalarTraits
<double>::output(const double &Val
, void *, raw_ostream
&Out
) {
996 Out
<< format("%g", Val
);
999 StringRef ScalarTraits
<double>::input(StringRef Scalar
, void *, double &Val
) {
1000 if (to_float(Scalar
, Val
))
1002 return "invalid floating point number";
1005 void ScalarTraits
<float>::output(const float &Val
, void *, raw_ostream
&Out
) {
1006 Out
<< format("%g", Val
);
1009 StringRef ScalarTraits
<float>::input(StringRef Scalar
, void *, float &Val
) {
1010 if (to_float(Scalar
, Val
))
1012 return "invalid floating point number";
1015 void ScalarTraits
<Hex8
>::output(const Hex8
&Val
, void *, raw_ostream
&Out
) {
1017 Out
<< format("0x%02X", Num
);
1020 StringRef ScalarTraits
<Hex8
>::input(StringRef Scalar
, void *, Hex8
&Val
) {
1021 unsigned long long n
;
1022 if (getAsUnsignedInteger(Scalar
, 0, n
))
1023 return "invalid hex8 number";
1025 return "out of range hex8 number";
1030 void ScalarTraits
<Hex16
>::output(const Hex16
&Val
, void *, raw_ostream
&Out
) {
1032 Out
<< format("0x%04X", Num
);
1035 StringRef ScalarTraits
<Hex16
>::input(StringRef Scalar
, void *, Hex16
&Val
) {
1036 unsigned long long n
;
1037 if (getAsUnsignedInteger(Scalar
, 0, n
))
1038 return "invalid hex16 number";
1040 return "out of range hex16 number";
1045 void ScalarTraits
<Hex32
>::output(const Hex32
&Val
, void *, raw_ostream
&Out
) {
1047 Out
<< format("0x%08X", Num
);
1050 StringRef ScalarTraits
<Hex32
>::input(StringRef Scalar
, void *, Hex32
&Val
) {
1051 unsigned long long n
;
1052 if (getAsUnsignedInteger(Scalar
, 0, n
))
1053 return "invalid hex32 number";
1054 if (n
> 0xFFFFFFFFUL
)
1055 return "out of range hex32 number";
1060 void ScalarTraits
<Hex64
>::output(const Hex64
&Val
, void *, raw_ostream
&Out
) {
1062 Out
<< format("0x%016llX", Num
);
1065 StringRef ScalarTraits
<Hex64
>::input(StringRef Scalar
, void *, Hex64
&Val
) {
1066 unsigned long long Num
;
1067 if (getAsUnsignedInteger(Scalar
, 0, Num
))
1068 return "invalid hex64 number";