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