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