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