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