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