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