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