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 |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 335 | KeyStr = StringStorage.str().copy(StringAllocator); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 336 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 337 | return llvm::make_unique<ScalarHNode>(N, KeyStr); |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 338 | } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) { |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 339 | StringRef ValueCopy = BSN->getValue().copy(StringAllocator); |
| 340 | return llvm::make_unique<ScalarHNode>(N, ValueCopy); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 341 | } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 342 | auto SQHNode = llvm::make_unique<SequenceHNode>(N); |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 343 | for (Node &SN : *SQ) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 344 | auto Entry = this->createHNodes(&SN); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 345 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 346 | break; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 347 | SQHNode->Entries.push_back(std::move(Entry)); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 348 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 349 | return std::move(SQHNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 350 | } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 351 | auto mapHNode = llvm::make_unique<MapHNode>(N); |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 352 | for (KeyValueNode &KVN : *Map) { |
Rafael Espindola | a97373f | 2014-08-08 13:58:00 +0000 | [diff] [blame] | 353 | Node *KeyNode = KVN.getKey(); |
| 354 | ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode); |
| 355 | if (!KeyScalar) { |
| 356 | setError(KeyNode, "Map key must be a scalar"); |
| 357 | break; |
| 358 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 359 | StringStorage.clear(); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 360 | StringRef KeyStr = KeyScalar->getValue(StringStorage); |
| 361 | if (!StringStorage.empty()) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 362 | // Copy string to permanent storage |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 363 | KeyStr = StringStorage.str().copy(StringAllocator); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 364 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 365 | auto ValueHNode = this->createHNodes(KVN.getValue()); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 366 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 367 | break; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 368 | mapHNode->Mapping[KeyStr] = std::move(ValueHNode); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 369 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 370 | return std::move(mapHNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 371 | } else if (isa<NullNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 372 | return llvm::make_unique<EmptyHNode>(N); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 373 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 374 | setError(N, "unknown node kind"); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 375 | return nullptr; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 379 | bool Input::MapHNode::isValidKey(StringRef Key) { |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 380 | for (const char *K : ValidKeys) { |
| 381 | if (Key.equals(K)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | void Input::setError(const Twine &Message) { |
| 388 | this->setError(CurrentNode, Message); |
| 389 | } |
| 390 | |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 391 | bool Input::canElideEmptySequence() { |
| 392 | return false; |
| 393 | } |
| 394 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 395 | //===----------------------------------------------------------------------===// |
| 396 | // Output |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 399 | Output::Output(raw_ostream &yout, void *context, int WrapColumn) |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 400 | : IO(context), |
| 401 | Out(yout), |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 402 | WrapColumn(WrapColumn), |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 403 | Column(0), |
| 404 | ColumnAtFlowStart(0), |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 405 | ColumnAtMapFlowStart(0), |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 406 | NeedBitValueComma(false), |
| 407 | NeedFlowSequenceComma(false), |
| 408 | EnumerationMatchFound(false), |
| 409 | NeedsNewLine(false) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | Output::~Output() { |
| 413 | } |
| 414 | |
Nick Kledzik | 4761c60 | 2013-11-21 00:20:10 +0000 | [diff] [blame] | 415 | bool Output::outputting() { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 416 | return true; |
| 417 | } |
| 418 | |
| 419 | void Output::beginMapping() { |
| 420 | StateStack.push_back(inMapFirstKey); |
| 421 | NeedsNewLine = true; |
| 422 | } |
| 423 | |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 424 | bool Output::mapTag(StringRef Tag, bool Use) { |
| 425 | if (Use) { |
| 426 | this->output(" "); |
| 427 | this->output(Tag); |
| 428 | } |
| 429 | return Use; |
| 430 | } |
| 431 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 432 | void Output::endMapping() { |
| 433 | StateStack.pop_back(); |
| 434 | } |
| 435 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 436 | bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 437 | bool &UseDefault, void *&) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 438 | UseDefault = false; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 439 | if (Required || !SameAsDefault) { |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 440 | auto State = StateStack.back(); |
| 441 | if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) { |
| 442 | flowKey(Key); |
| 443 | } else { |
| 444 | this->newLineCheck(); |
| 445 | this->paddedKey(Key); |
| 446 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 447 | return true; |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 452 | void Output::postflightKey(void *) { |
| 453 | if (StateStack.back() == inMapFirstKey) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 454 | StateStack.pop_back(); |
| 455 | StateStack.push_back(inMapOtherKey); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 456 | } else if (StateStack.back() == inFlowMapFirstKey) { |
| 457 | StateStack.pop_back(); |
| 458 | StateStack.push_back(inFlowMapOtherKey); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 462 | void Output::beginFlowMapping() { |
| 463 | StateStack.push_back(inFlowMapFirstKey); |
| 464 | this->newLineCheck(); |
| 465 | ColumnAtMapFlowStart = Column; |
| 466 | output("{ "); |
| 467 | } |
| 468 | |
| 469 | void Output::endFlowMapping() { |
| 470 | StateStack.pop_back(); |
| 471 | this->outputUpToEndOfLine(" }"); |
| 472 | } |
| 473 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 474 | void Output::beginDocuments() { |
| 475 | this->outputUpToEndOfLine("---"); |
| 476 | } |
| 477 | |
| 478 | bool Output::preflightDocument(unsigned index) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 479 | if (index > 0) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 480 | this->outputUpToEndOfLine("\n---"); |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | void Output::postflightDocument() { |
| 485 | } |
| 486 | |
| 487 | void Output::endDocuments() { |
| 488 | output("\n...\n"); |
| 489 | } |
| 490 | |
| 491 | unsigned Output::beginSequence() { |
| 492 | StateStack.push_back(inSeq); |
| 493 | NeedsNewLine = true; |
| 494 | return 0; |
| 495 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 496 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 497 | void Output::endSequence() { |
| 498 | StateStack.pop_back(); |
| 499 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 500 | |
| 501 | bool Output::preflightElement(unsigned, void *&) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 502 | return true; |
| 503 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 504 | |
| 505 | void Output::postflightElement(void *) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | unsigned Output::beginFlowSequence() { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 509 | StateStack.push_back(inFlowSeq); |
Nick Kledzik | 11964f2 | 2013-01-04 19:32:00 +0000 | [diff] [blame] | 510 | this->newLineCheck(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 511 | ColumnAtFlowStart = Column; |
| 512 | output("[ "); |
| 513 | NeedFlowSequenceComma = false; |
| 514 | return 0; |
| 515 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 516 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 517 | void Output::endFlowSequence() { |
| 518 | StateStack.pop_back(); |
| 519 | this->outputUpToEndOfLine(" ]"); |
| 520 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 521 | |
| 522 | bool Output::preflightFlowElement(unsigned, void *&) { |
| 523 | if (NeedFlowSequenceComma) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 524 | output(", "); |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 525 | if (WrapColumn && Column > WrapColumn) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 526 | output("\n"); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 527 | for (int i = 0; i < ColumnAtFlowStart; ++i) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 528 | output(" "); |
| 529 | Column = ColumnAtFlowStart; |
| 530 | output(" "); |
| 531 | } |
| 532 | return true; |
| 533 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 534 | |
| 535 | void Output::postflightFlowElement(void *) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 536 | NeedFlowSequenceComma = true; |
| 537 | } |
| 538 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 539 | void Output::beginEnumScalar() { |
| 540 | EnumerationMatchFound = false; |
| 541 | } |
| 542 | |
| 543 | bool Output::matchEnumScalar(const char *Str, bool Match) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 544 | if (Match && !EnumerationMatchFound) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 545 | this->newLineCheck(); |
| 546 | this->outputUpToEndOfLine(Str); |
| 547 | EnumerationMatchFound = true; |
| 548 | } |
| 549 | return false; |
| 550 | } |
| 551 | |
Michael J. Spencer | 731cae3 | 2015-01-23 21:57:50 +0000 | [diff] [blame] | 552 | bool Output::matchEnumFallback() { |
| 553 | if (EnumerationMatchFound) |
| 554 | return false; |
| 555 | EnumerationMatchFound = true; |
| 556 | return true; |
| 557 | } |
| 558 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 559 | void Output::endEnumScalar() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 560 | if (!EnumerationMatchFound) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 561 | llvm_unreachable("bad runtime enum value"); |
| 562 | } |
| 563 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 564 | bool Output::beginBitSetScalar(bool &DoClear) { |
| 565 | this->newLineCheck(); |
| 566 | output("[ "); |
| 567 | NeedBitValueComma = false; |
| 568 | DoClear = false; |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | bool Output::bitSetMatch(const char *Str, bool Matches) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 573 | if (Matches) { |
| 574 | if (NeedBitValueComma) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 575 | output(", "); |
| 576 | this->output(Str); |
| 577 | NeedBitValueComma = true; |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | void Output::endBitSetScalar() { |
| 583 | this->outputUpToEndOfLine(" ]"); |
| 584 | } |
| 585 | |
David Majnemer | 7788033 | 2014-04-10 07:37:33 +0000 | [diff] [blame] | 586 | void Output::scalarString(StringRef &S, bool MustQuote) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 587 | this->newLineCheck(); |
Rui Ueyama | 106eded | 2013-09-11 04:00:08 +0000 | [diff] [blame] | 588 | if (S.empty()) { |
| 589 | // Print '' for the empty string because leaving the field empty is not |
| 590 | // allowed. |
| 591 | this->outputUpToEndOfLine("''"); |
| 592 | return; |
| 593 | } |
David Majnemer | 7788033 | 2014-04-10 07:37:33 +0000 | [diff] [blame] | 594 | if (!MustQuote) { |
| 595 | // Only quote if we must. |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 596 | this->outputUpToEndOfLine(S); |
| 597 | return; |
| 598 | } |
| 599 | unsigned i = 0; |
| 600 | unsigned j = 0; |
| 601 | unsigned End = S.size(); |
| 602 | output("'"); // Starting single quote. |
| 603 | const char *Base = S.data(); |
| 604 | while (j < End) { |
| 605 | // Escape a single quote by doubling it. |
| 606 | if (S[j] == '\'') { |
| 607 | output(StringRef(&Base[i], j - i + 1)); |
| 608 | output("'"); |
| 609 | i = j + 1; |
| 610 | } |
| 611 | ++j; |
| 612 | } |
| 613 | output(StringRef(&Base[i], j - i)); |
| 614 | this->outputUpToEndOfLine("'"); // Ending single quote. |
| 615 | } |
| 616 | |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 617 | void Output::blockScalarString(StringRef &S) { |
| 618 | if (!StateStack.empty()) |
| 619 | newLineCheck(); |
| 620 | output(" |"); |
| 621 | outputNewLine(); |
| 622 | |
| 623 | unsigned Indent = StateStack.empty() ? 1 : StateStack.size(); |
| 624 | |
| 625 | auto Buffer = MemoryBuffer::getMemBuffer(S, "", false); |
| 626 | for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) { |
| 627 | for (unsigned I = 0; I < Indent; ++I) { |
| 628 | output(" "); |
| 629 | } |
| 630 | output(*Lines); |
| 631 | outputNewLine(); |
| 632 | } |
| 633 | } |
| 634 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 635 | void Output::setError(const Twine &message) { |
| 636 | } |
| 637 | |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 638 | bool Output::canElideEmptySequence() { |
| 639 | // Normally, with an optional key/value where the value is an empty sequence, |
| 640 | // the whole key/value can be not written. But, that produces wrong yaml |
| 641 | // if the key/value is the only thing in the map and the map is used in |
| 642 | // a sequence. This detects if the this sequence is the first key/value |
| 643 | // in map that itself is embedded in a sequnce. |
Rui Ueyama | 38dfffa | 2013-09-11 00:53:07 +0000 | [diff] [blame] | 644 | if (StateStack.size() < 2) |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 645 | return true; |
Rui Ueyama | 38dfffa | 2013-09-11 00:53:07 +0000 | [diff] [blame] | 646 | if (StateStack.back() != inMapFirstKey) |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 647 | return true; |
| 648 | return (StateStack[StateStack.size()-2] != inSeq); |
| 649 | } |
| 650 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 651 | void Output::output(StringRef s) { |
| 652 | Column += s.size(); |
| 653 | Out << s; |
| 654 | } |
| 655 | |
| 656 | void Output::outputUpToEndOfLine(StringRef s) { |
| 657 | this->output(s); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 658 | if (StateStack.empty() || (StateStack.back() != inFlowSeq && |
| 659 | StateStack.back() != inFlowMapFirstKey && |
| 660 | StateStack.back() != inFlowMapOtherKey)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 661 | NeedsNewLine = true; |
| 662 | } |
| 663 | |
| 664 | void Output::outputNewLine() { |
| 665 | Out << "\n"; |
| 666 | Column = 0; |
| 667 | } |
| 668 | |
| 669 | // if seq at top, indent as if map, then add "- " |
| 670 | // if seq in middle, use "- " if firstKey, else use " " |
| 671 | // |
| 672 | |
| 673 | void Output::newLineCheck() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 674 | if (!NeedsNewLine) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 675 | return; |
| 676 | NeedsNewLine = false; |
| 677 | |
| 678 | this->outputNewLine(); |
| 679 | |
| 680 | assert(StateStack.size() > 0); |
| 681 | unsigned Indent = StateStack.size() - 1; |
| 682 | bool OutputDash = false; |
| 683 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 684 | if (StateStack.back() == inSeq) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 685 | OutputDash = true; |
Alex Lorenz | 42e91fa | 2015-05-01 18:34:25 +0000 | [diff] [blame] | 686 | } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) || |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 687 | (StateStack.back() == inFlowSeq) || |
| 688 | (StateStack.back() == inFlowMapFirstKey)) && |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 689 | (StateStack[StateStack.size() - 2] == inSeq)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 690 | --Indent; |
| 691 | OutputDash = true; |
| 692 | } |
| 693 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 694 | for (unsigned i = 0; i < Indent; ++i) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 695 | output(" "); |
| 696 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 697 | if (OutputDash) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 698 | output("- "); |
| 699 | } |
| 700 | |
| 701 | } |
| 702 | |
| 703 | void Output::paddedKey(StringRef key) { |
| 704 | output(key); |
| 705 | output(":"); |
| 706 | const char *spaces = " "; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 707 | if (key.size() < strlen(spaces)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 708 | output(&spaces[key.size()]); |
| 709 | else |
| 710 | output(" "); |
| 711 | } |
| 712 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 713 | void Output::flowKey(StringRef Key) { |
| 714 | if (StateStack.back() == inFlowMapOtherKey) |
| 715 | output(", "); |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 716 | if (WrapColumn && Column > WrapColumn) { |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 717 | output("\n"); |
| 718 | for (int I = 0; I < ColumnAtMapFlowStart; ++I) |
| 719 | output(" "); |
| 720 | Column = ColumnAtMapFlowStart; |
| 721 | output(" "); |
| 722 | } |
| 723 | output(Key); |
| 724 | output(": "); |
| 725 | } |
| 726 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 727 | //===----------------------------------------------------------------------===// |
| 728 | // traits for built-in types |
| 729 | //===----------------------------------------------------------------------===// |
| 730 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 731 | void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { |
| 732 | Out << (Val ? "true" : "false"); |
| 733 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 734 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 735 | StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) { |
| 736 | if (Scalar.equals("true")) { |
| 737 | Val = true; |
| 738 | return StringRef(); |
| 739 | } else if (Scalar.equals("false")) { |
| 740 | Val = false; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 741 | return StringRef(); |
| 742 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 743 | return "invalid boolean"; |
| 744 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 745 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 746 | void ScalarTraits<StringRef>::output(const StringRef &Val, void *, |
| 747 | raw_ostream &Out) { |
| 748 | Out << Val; |
| 749 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 750 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 751 | StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, |
| 752 | StringRef &Val) { |
| 753 | Val = Scalar; |
| 754 | return StringRef(); |
| 755 | } |
Alex Rosenberg | f298f16 | 2015-01-26 18:02:18 +0000 | [diff] [blame] | 756 | |
John Thompson | 48e018a | 2013-11-19 17:28:21 +0000 | [diff] [blame] | 757 | void ScalarTraits<std::string>::output(const std::string &Val, void *, |
| 758 | raw_ostream &Out) { |
| 759 | Out << Val; |
| 760 | } |
| 761 | |
| 762 | StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, |
| 763 | std::string &Val) { |
| 764 | Val = Scalar.str(); |
| 765 | return StringRef(); |
| 766 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 767 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 768 | void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *, |
| 769 | raw_ostream &Out) { |
| 770 | // use temp uin32_t because ostream thinks uint8_t is a character |
| 771 | uint32_t Num = Val; |
| 772 | Out << Num; |
| 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 | StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) { |
| 776 | unsigned long long n; |
| 777 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 778 | return "invalid number"; |
| 779 | if (n > 0xFF) |
| 780 | return "out of range number"; |
| 781 | Val = n; |
| 782 | return StringRef(); |
| 783 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 784 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 785 | void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, |
| 786 | raw_ostream &Out) { |
| 787 | Out << Val; |
| 788 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 789 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 790 | StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *, |
| 791 | uint16_t &Val) { |
| 792 | unsigned long long n; |
| 793 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 794 | return "invalid number"; |
| 795 | if (n > 0xFFFF) |
| 796 | return "out of range number"; |
| 797 | Val = n; |
| 798 | return StringRef(); |
| 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 | void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, |
| 802 | raw_ostream &Out) { |
| 803 | Out << Val; |
| 804 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 805 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 806 | StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *, |
| 807 | uint32_t &Val) { |
| 808 | unsigned long long n; |
| 809 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 810 | return "invalid number"; |
| 811 | if (n > 0xFFFFFFFFUL) |
| 812 | return "out of range number"; |
| 813 | Val = n; |
| 814 | return StringRef(); |
| 815 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 816 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 817 | void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, |
| 818 | raw_ostream &Out) { |
| 819 | Out << Val; |
| 820 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 821 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 822 | StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *, |
| 823 | uint64_t &Val) { |
| 824 | unsigned long long N; |
| 825 | if (getAsUnsignedInteger(Scalar, 0, N)) |
| 826 | return "invalid number"; |
| 827 | Val = N; |
| 828 | return StringRef(); |
| 829 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 830 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 831 | void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) { |
| 832 | // use temp in32_t because ostream thinks int8_t is a character |
| 833 | int32_t Num = Val; |
| 834 | Out << Num; |
| 835 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 836 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 837 | StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) { |
| 838 | long long N; |
| 839 | if (getAsSignedInteger(Scalar, 0, N)) |
| 840 | return "invalid number"; |
| 841 | if ((N > 127) || (N < -128)) |
| 842 | return "out of range number"; |
| 843 | Val = N; |
| 844 | return StringRef(); |
| 845 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 846 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 847 | void ScalarTraits<int16_t>::output(const int16_t &Val, void *, |
| 848 | raw_ostream &Out) { |
| 849 | Out << Val; |
| 850 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 851 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 852 | StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) { |
| 853 | long long N; |
| 854 | if (getAsSignedInteger(Scalar, 0, N)) |
| 855 | return "invalid number"; |
| 856 | if ((N > INT16_MAX) || (N < INT16_MIN)) |
| 857 | return "out of range number"; |
| 858 | Val = N; |
| 859 | return StringRef(); |
| 860 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 861 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 862 | void ScalarTraits<int32_t>::output(const int32_t &Val, void *, |
| 863 | raw_ostream &Out) { |
| 864 | Out << Val; |
| 865 | } |
| 866 | |
| 867 | StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) { |
| 868 | long long N; |
| 869 | if (getAsSignedInteger(Scalar, 0, N)) |
| 870 | return "invalid number"; |
| 871 | if ((N > INT32_MAX) || (N < INT32_MIN)) |
| 872 | return "out of range number"; |
| 873 | Val = N; |
| 874 | return StringRef(); |
| 875 | } |
| 876 | |
| 877 | void ScalarTraits<int64_t>::output(const int64_t &Val, void *, |
| 878 | raw_ostream &Out) { |
| 879 | Out << Val; |
| 880 | } |
| 881 | |
| 882 | StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) { |
| 883 | long long N; |
| 884 | if (getAsSignedInteger(Scalar, 0, N)) |
| 885 | return "invalid number"; |
| 886 | Val = N; |
| 887 | return StringRef(); |
| 888 | } |
| 889 | |
| 890 | void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 891 | Out << format("%g", Val); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 892 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 893 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 894 | StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) { |
| 895 | SmallString<32> buff(Scalar.begin(), Scalar.end()); |
| 896 | char *end; |
| 897 | Val = strtod(buff.c_str(), &end); |
| 898 | if (*end != '\0') |
| 899 | return "invalid floating point number"; |
| 900 | return StringRef(); |
| 901 | } |
| 902 | |
| 903 | void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 904 | Out << format("%g", Val); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 905 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 906 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 907 | StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) { |
| 908 | SmallString<32> buff(Scalar.begin(), Scalar.end()); |
| 909 | char *end; |
| 910 | Val = strtod(buff.c_str(), &end); |
| 911 | if (*end != '\0') |
| 912 | return "invalid floating point number"; |
| 913 | return StringRef(); |
| 914 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 915 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 916 | void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { |
| 917 | uint8_t Num = Val; |
| 918 | Out << format("0x%02X", Num); |
| 919 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 920 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 921 | StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) { |
| 922 | unsigned long long n; |
| 923 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 924 | return "invalid hex8 number"; |
| 925 | if (n > 0xFF) |
| 926 | return "out of range hex8 number"; |
| 927 | Val = n; |
| 928 | return StringRef(); |
| 929 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 930 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 931 | void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { |
| 932 | uint16_t Num = Val; |
| 933 | Out << format("0x%04X", Num); |
| 934 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 935 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 936 | StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) { |
| 937 | unsigned long long n; |
| 938 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 939 | return "invalid hex16 number"; |
| 940 | if (n > 0xFFFF) |
| 941 | return "out of range hex16 number"; |
| 942 | Val = n; |
| 943 | return StringRef(); |
| 944 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 945 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 946 | void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { |
| 947 | uint32_t Num = Val; |
| 948 | Out << format("0x%08X", Num); |
| 949 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 950 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 951 | StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) { |
| 952 | unsigned long long n; |
| 953 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 954 | return "invalid hex32 number"; |
| 955 | if (n > 0xFFFFFFFFUL) |
| 956 | return "out of range hex32 number"; |
| 957 | Val = n; |
| 958 | return StringRef(); |
| 959 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 960 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 961 | void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { |
| 962 | uint64_t Num = Val; |
| 963 | Out << format("0x%016llX", Num); |
| 964 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 965 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 966 | StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) { |
| 967 | unsigned long long Num; |
| 968 | if (getAsUnsignedInteger(Scalar, 0, Num)) |
| 969 | return "invalid hex64 number"; |
| 970 | Val = Num; |
| 971 | return StringRef(); |
| 972 | } |