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