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