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