Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1 | //===- lib/Support/YAMLTraits.cpp -----------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 9 | #include "llvm/Support/YAMLTraits.h" |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Pavel Labath | ec000f4 | 2017-06-23 12:55:02 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringRef.h" |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Twine.h" |
| 15 | #include "llvm/Support/Casting.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errc.h" |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | cbe0584 | 2012-12-12 20:55:44 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 19 | #include "llvm/Support/LineIterator.h" |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Unicode.h" |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 22 | #include "llvm/Support/YAMLParser.h" |
Benjamin Kramer | cbe0584 | 2012-12-12 20:55:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <cassert> |
| 26 | #include <cstdint> |
| 27 | #include <cstdlib> |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include <cstring> |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | using namespace yaml; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // IO |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 39 | IO::IO(void *Context) : Ctxt(Context) {} |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 40 | |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 41 | IO::~IO() = default; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 42 | |
| 43 | void *IO::getContext() { |
| 44 | return Ctxt; |
| 45 | } |
| 46 | |
| 47 | void IO::setContext(void *Context) { |
| 48 | Ctxt = Context; |
| 49 | } |
| 50 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // Input |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Mehdi Amini | 3ab3fef | 2016-11-28 21:38:52 +0000 | [diff] [blame] | 55 | Input::Input(StringRef InputContent, void *Ctxt, |
| 56 | SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt) |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 57 | : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) { |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 58 | if (DiagHandler) |
| 59 | SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 60 | DocIterator = Strm->begin(); |
| 61 | } |
| 62 | |
Alex Bradbury | 1684317 | 2017-07-17 11:41:30 +0000 | [diff] [blame] | 63 | Input::Input(MemoryBufferRef Input, void *Ctxt, |
| 64 | SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt) |
| 65 | : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) { |
| 66 | if (DiagHandler) |
| 67 | SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); |
| 68 | DocIterator = Strm->begin(); |
| 69 | } |
| 70 | |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 71 | Input::~Input() = default; |
Nick Kledzik | 0dcef84 | 2013-01-08 21:04:44 +0000 | [diff] [blame] | 72 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 73 | std::error_code Input::error() { return EC; } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 74 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 75 | // Pin the vtables to this file. |
| 76 | void Input::HNode::anchor() {} |
| 77 | void Input::EmptyHNode::anchor() {} |
| 78 | void Input::ScalarHNode::anchor() {} |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 79 | void Input::MapHNode::anchor() {} |
| 80 | void Input::SequenceHNode::anchor() {} |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 81 | |
Nick Kledzik | 4761c60 | 2013-11-21 00:20:10 +0000 | [diff] [blame] | 82 | bool Input::outputting() { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
| 86 | bool Input::setCurrentDocument() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 87 | if (DocIterator != Strm->end()) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 88 | Node *N = DocIterator->getRoot(); |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 89 | if (!N) { |
Alex Lorenz | 7a38d75 | 2015-05-12 17:44:32 +0000 | [diff] [blame] | 90 | assert(Strm->failed() && "Root is NULL iff parsing failed"); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 91 | EC = make_error_code(errc::invalid_argument); |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 95 | if (isa<NullNode>(N)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 96 | // Empty files are allowed and ignored |
| 97 | ++DocIterator; |
| 98 | return setCurrentDocument(); |
| 99 | } |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 100 | TopNode = createHNodes(N); |
Nick Kledzik | 0dcef84 | 2013-01-08 21:04:44 +0000 | [diff] [blame] | 101 | CurrentNode = TopNode.get(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
Simon Atanasyan | f97af8a | 2014-05-31 04:51:07 +0000 | [diff] [blame] | 107 | bool Input::nextDocument() { |
| 108 | return ++DocIterator != Strm->end(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 109 | } |
NAKAMURA Takumi | 9439c52 | 2013-11-14 07:08:49 +0000 | [diff] [blame] | 110 | |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 111 | const Node *Input::getCurrentNode() const { |
| 112 | return CurrentNode ? CurrentNode->_node : nullptr; |
| 113 | } |
| 114 | |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 115 | bool Input::mapTag(StringRef Tag, bool Default) { |
NAKAMURA Takumi | 5b94d28 | 2013-11-14 07:08:56 +0000 | [diff] [blame] | 116 | std::string foundTag = CurrentNode->_node->getVerbatimTag(); |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 117 | if (foundTag.empty()) { |
| 118 | // If no tag found and 'Tag' is the default, say it was found. |
| 119 | return Default; |
| 120 | } |
Alex Lorenz | 7a38d75 | 2015-05-12 17:44:32 +0000 | [diff] [blame] | 121 | // Return true iff found tag matches supplied tag. |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 122 | return Tag.equals(foundTag); |
| 123 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 124 | |
| 125 | void Input::beginMapping() { |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 126 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 127 | return; |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 128 | // CurrentNode can be null if the document is empty. |
| 129 | MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 130 | if (MN) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 131 | MN->ValidKeys.clear(); |
| 132 | } |
| 133 | } |
| 134 | |
Peter Collingbourne | 87dd2ab | 2017-01-04 03:51:36 +0000 | [diff] [blame] | 135 | std::vector<StringRef> Input::keys() { |
| 136 | MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); |
| 137 | std::vector<StringRef> Ret; |
| 138 | if (!MN) { |
| 139 | setError(CurrentNode, "not a mapping"); |
| 140 | return Ret; |
| 141 | } |
| 142 | for (auto &P : MN->Mapping) |
| 143 | Ret.push_back(P.first()); |
| 144 | return Ret; |
| 145 | } |
| 146 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 147 | bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault, |
| 148 | void *&SaveInfo) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 149 | UseDefault = false; |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 150 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 151 | return false; |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 152 | |
| 153 | // CurrentNode is null for empty documents, which is an error in case required |
| 154 | // nodes are present. |
| 155 | if (!CurrentNode) { |
| 156 | if (Required) |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 157 | EC = make_error_code(errc::invalid_argument); |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 161 | MapHNode *MN = dyn_cast<MapHNode>(CurrentNode); |
| 162 | if (!MN) { |
Dave Lee | c6f2e69 | 2017-11-16 17:46:11 +0000 | [diff] [blame] | 163 | if (Required || !isa<EmptyHNode>(CurrentNode)) |
| 164 | setError(CurrentNode, "not a mapping"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | MN->ValidKeys.push_back(Key); |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 168 | HNode *Value = MN->Mapping[Key].get(); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 169 | if (!Value) { |
| 170 | if (Required) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 171 | setError(CurrentNode, Twine("missing required key '") + Key + "'"); |
| 172 | else |
| 173 | UseDefault = true; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 174 | return false; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 175 | } |
| 176 | SaveInfo = CurrentNode; |
| 177 | CurrentNode = Value; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | void Input::postflightKey(void *saveInfo) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 182 | CurrentNode = reinterpret_cast<HNode *>(saveInfo); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void Input::endMapping() { |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 186 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 187 | return; |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 188 | // CurrentNode can be null if the document is empty. |
| 189 | MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 190 | if (!MN) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 191 | return; |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 192 | for (const auto &NN : MN->Mapping) { |
Peter Collingbourne | efdff71 | 2017-01-04 20:10:43 +0000 | [diff] [blame] | 193 | if (!is_contained(MN->ValidKeys, NN.first())) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 194 | setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 200 | void Input::beginFlowMapping() { beginMapping(); } |
| 201 | |
| 202 | void Input::endFlowMapping() { endMapping(); } |
| 203 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 204 | unsigned Input::beginSequence() { |
Justin Bogner | 64d2cdf | 2015-03-02 17:26:43 +0000 | [diff] [blame] | 205 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 206 | return SQ->Entries.size(); |
Justin Bogner | 64d2cdf | 2015-03-02 17:26:43 +0000 | [diff] [blame] | 207 | if (isa<EmptyHNode>(CurrentNode)) |
| 208 | return 0; |
| 209 | // Treat case where there's a scalar "null" value as an empty sequence. |
| 210 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { |
| 211 | if (isNull(SN->value())) |
| 212 | return 0; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 213 | } |
Justin Bogner | 64d2cdf | 2015-03-02 17:26:43 +0000 | [diff] [blame] | 214 | // Any other type of HNode is an error. |
| 215 | setError(CurrentNode, "not a sequence"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 218 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 219 | void Input::endSequence() { |
| 220 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 221 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 222 | bool Input::preflightElement(unsigned Index, void *&SaveInfo) { |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 223 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 224 | return false; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 225 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 226 | SaveInfo = CurrentNode; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 227 | CurrentNode = SQ->Entries[Index].get(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | return false; |
| 231 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 232 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 233 | void Input::postflightElement(void *SaveInfo) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 234 | CurrentNode = reinterpret_cast<HNode *>(SaveInfo); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Justin Bogner | 64d2cdf | 2015-03-02 17:26:43 +0000 | [diff] [blame] | 237 | unsigned Input::beginFlowSequence() { return beginSequence(); } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 238 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 239 | bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) { |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 240 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 241 | return false; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 242 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 243 | SaveInfo = CurrentNode; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 244 | CurrentNode = SQ->Entries[index].get(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | return false; |
| 248 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 249 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 250 | void Input::postflightFlowElement(void *SaveInfo) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 251 | CurrentNode = reinterpret_cast<HNode *>(SaveInfo); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 254 | void Input::endFlowSequence() { |
| 255 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 256 | |
| 257 | void Input::beginEnumScalar() { |
| 258 | ScalarMatchFound = false; |
| 259 | } |
| 260 | |
| 261 | bool Input::matchEnumScalar(const char *Str, bool) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 262 | if (ScalarMatchFound) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 263 | return false; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 264 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { |
| 265 | if (SN->value().equals(Str)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 266 | ScalarMatchFound = true; |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
Michael J. Spencer | 731cae3 | 2015-01-23 21:57:50 +0000 | [diff] [blame] | 273 | bool Input::matchEnumFallback() { |
| 274 | if (ScalarMatchFound) |
| 275 | return false; |
| 276 | ScalarMatchFound = true; |
| 277 | return true; |
| 278 | } |
| 279 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 280 | void Input::endEnumScalar() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 281 | if (!ScalarMatchFound) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 282 | setError(CurrentNode, "unknown enumerated scalar"); |
| 283 | } |
| 284 | } |
| 285 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 286 | bool Input::beginBitSetScalar(bool &DoClear) { |
| 287 | BitValuesUsed.clear(); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 288 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 289 | BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 290 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 291 | setError(CurrentNode, "expected sequence of bit values"); |
| 292 | } |
| 293 | DoClear = true; |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | bool Input::bitSetMatch(const char *Str, bool) { |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 298 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 299 | return false; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 300 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 301 | unsigned Index = 0; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 302 | for (auto &N : SQ->Entries) { |
| 303 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 304 | if (SN->value().equals(Str)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 305 | BitValuesUsed[Index] = true; |
| 306 | return true; |
| 307 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 308 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 309 | setError(CurrentNode, "unexpected scalar in sequence of bit values"); |
| 310 | } |
| 311 | ++Index; |
| 312 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 313 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 314 | setError(CurrentNode, "expected sequence of bit values"); |
| 315 | } |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | void Input::endBitSetScalar() { |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 320 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 321 | return; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 322 | if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 323 | assert(BitValuesUsed.size() == SQ->Entries.size()); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 324 | for (unsigned i = 0; i < SQ->Entries.size(); ++i) { |
| 325 | if (!BitValuesUsed[i]) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 326 | setError(SQ->Entries[i].get(), "unknown bit value"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 333 | void Input::scalarString(StringRef &S, QuotingType) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 334 | if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 335 | S = SN->value(); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 336 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 337 | setError(CurrentNode, "unexpected scalar"); |
| 338 | } |
| 339 | } |
| 340 | |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 341 | void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); } |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 342 | |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 343 | void Input::scalarTag(std::string &Tag) { |
| 344 | Tag = CurrentNode->_node->getVerbatimTag(); |
| 345 | } |
| 346 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 347 | void Input::setError(HNode *hnode, const Twine &message) { |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 348 | assert(hnode && "HNode must not be NULL"); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 349 | setError(hnode->_node, message); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 352 | NodeKind Input::getNodeKind() { |
| 353 | if (isa<ScalarHNode>(CurrentNode)) |
| 354 | return NodeKind::Scalar; |
| 355 | else if (isa<MapHNode>(CurrentNode)) |
| 356 | return NodeKind::Map; |
| 357 | else if (isa<SequenceHNode>(CurrentNode)) |
| 358 | return NodeKind::Sequence; |
| 359 | llvm_unreachable("Unsupported node kind"); |
| 360 | } |
| 361 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 362 | void Input::setError(Node *node, const Twine &message) { |
| 363 | Strm->printError(node, message); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 364 | EC = make_error_code(errc::invalid_argument); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 365 | } |
| 366 | |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 367 | std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 368 | SmallString<128> StringStorage; |
| 369 | if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 370 | StringRef KeyStr = SN->getValue(StringStorage); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 371 | if (!StringStorage.empty()) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 372 | // Copy string to permanent storage |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 373 | KeyStr = StringStorage.str().copy(StringAllocator); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 374 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 375 | return llvm::make_unique<ScalarHNode>(N, KeyStr); |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 376 | } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) { |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 377 | StringRef ValueCopy = BSN->getValue().copy(StringAllocator); |
| 378 | return llvm::make_unique<ScalarHNode>(N, ValueCopy); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 379 | } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 380 | auto SQHNode = llvm::make_unique<SequenceHNode>(N); |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 381 | for (Node &SN : *SQ) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 382 | auto Entry = createHNodes(&SN); |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 383 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 384 | break; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 385 | SQHNode->Entries.push_back(std::move(Entry)); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 386 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 387 | return std::move(SQHNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 388 | } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 389 | auto mapHNode = llvm::make_unique<MapHNode>(N); |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 390 | for (KeyValueNode &KVN : *Map) { |
Rafael Espindola | a97373f | 2014-08-08 13:58:00 +0000 | [diff] [blame] | 391 | Node *KeyNode = KVN.getKey(); |
George Rimar | 3674fb6 | 2017-09-21 08:25:59 +0000 | [diff] [blame] | 392 | ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode); |
| 393 | Node *Value = KVN.getValue(); |
| 394 | if (!Key || !Value) { |
| 395 | if (!Key) |
| 396 | setError(KeyNode, "Map key must be a scalar"); |
| 397 | if (!Value) |
| 398 | setError(KeyNode, "Map value must not be empty"); |
Rafael Espindola | a97373f | 2014-08-08 13:58:00 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 401 | StringStorage.clear(); |
George Rimar | 3674fb6 | 2017-09-21 08:25:59 +0000 | [diff] [blame] | 402 | StringRef KeyStr = Key->getValue(StringStorage); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 403 | if (!StringStorage.empty()) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 404 | // Copy string to permanent storage |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 405 | KeyStr = StringStorage.str().copy(StringAllocator); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 406 | } |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 407 | auto ValueHNode = createHNodes(Value); |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 408 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 409 | break; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 410 | mapHNode->Mapping[KeyStr] = std::move(ValueHNode); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 411 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 412 | return std::move(mapHNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 413 | } else if (isa<NullNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 414 | return llvm::make_unique<EmptyHNode>(N); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 415 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 416 | setError(N, "unknown node kind"); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 417 | return nullptr; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 421 | void Input::setError(const Twine &Message) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 422 | setError(CurrentNode, Message); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 425 | bool Input::canElideEmptySequence() { |
| 426 | return false; |
| 427 | } |
| 428 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 429 | //===----------------------------------------------------------------------===// |
| 430 | // Output |
| 431 | //===----------------------------------------------------------------------===// |
| 432 | |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 433 | Output::Output(raw_ostream &yout, void *context, int WrapColumn) |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 434 | : IO(context), Out(yout), WrapColumn(WrapColumn) {} |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 435 | |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 436 | Output::~Output() = default; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 437 | |
Nick Kledzik | 4761c60 | 2013-11-21 00:20:10 +0000 | [diff] [blame] | 438 | bool Output::outputting() { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 439 | return true; |
| 440 | } |
| 441 | |
| 442 | void Output::beginMapping() { |
| 443 | StateStack.push_back(inMapFirstKey); |
| 444 | NeedsNewLine = true; |
| 445 | } |
| 446 | |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 447 | bool Output::mapTag(StringRef Tag, bool Use) { |
| 448 | if (Use) { |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 449 | // If this tag is being written inside a sequence we should write the start |
| 450 | // of the sequence before writing the tag, otherwise the tag won't be |
| 451 | // attached to the element in the sequence, but rather the sequence itself. |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 452 | bool SequenceElement = false; |
| 453 | if (StateStack.size() > 1) { |
| 454 | auto &E = StateStack[StateStack.size() - 2]; |
| 455 | SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E); |
| 456 | } |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 457 | if (SequenceElement && StateStack.back() == inMapFirstKey) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 458 | newLineCheck(); |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 459 | } else { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 460 | output(" "); |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 461 | } |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 462 | output(Tag); |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 463 | if (SequenceElement) { |
| 464 | // If we're writing the tag during the first element of a map, the tag |
| 465 | // takes the place of the first element in the sequence. |
| 466 | if (StateStack.back() == inMapFirstKey) { |
| 467 | StateStack.pop_back(); |
| 468 | StateStack.push_back(inMapOtherKey); |
| 469 | } |
| 470 | // Tags inside maps in sequences should act as keys in the map from a |
| 471 | // formatting perspective, so we always want a newline in a sequence. |
| 472 | NeedsNewLine = true; |
| 473 | } |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 474 | } |
| 475 | return Use; |
| 476 | } |
| 477 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 478 | void Output::endMapping() { |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 479 | // If we did not map anything, we should explicitly emit an empty map |
| 480 | if (StateStack.back() == inMapFirstKey) |
| 481 | output("{}"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 482 | StateStack.pop_back(); |
| 483 | } |
| 484 | |
Peter Collingbourne | 87dd2ab | 2017-01-04 03:51:36 +0000 | [diff] [blame] | 485 | std::vector<StringRef> Output::keys() { |
| 486 | report_fatal_error("invalid call"); |
| 487 | } |
| 488 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 489 | bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 490 | bool &UseDefault, void *&) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 491 | UseDefault = false; |
Zachary Turner | 84efd4d | 2017-03-15 17:47:39 +0000 | [diff] [blame] | 492 | if (Required || !SameAsDefault || WriteDefaultValues) { |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 493 | auto State = StateStack.back(); |
| 494 | if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) { |
| 495 | flowKey(Key); |
| 496 | } else { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 497 | newLineCheck(); |
| 498 | paddedKey(Key); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 499 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 500 | return true; |
| 501 | } |
| 502 | return false; |
| 503 | } |
| 504 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 505 | void Output::postflightKey(void *) { |
| 506 | if (StateStack.back() == inMapFirstKey) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 507 | StateStack.pop_back(); |
| 508 | StateStack.push_back(inMapOtherKey); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 509 | } else if (StateStack.back() == inFlowMapFirstKey) { |
| 510 | StateStack.pop_back(); |
| 511 | StateStack.push_back(inFlowMapOtherKey); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 515 | void Output::beginFlowMapping() { |
| 516 | StateStack.push_back(inFlowMapFirstKey); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 517 | newLineCheck(); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 518 | ColumnAtMapFlowStart = Column; |
| 519 | output("{ "); |
| 520 | } |
| 521 | |
| 522 | void Output::endFlowMapping() { |
| 523 | StateStack.pop_back(); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 524 | outputUpToEndOfLine(" }"); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 527 | void Output::beginDocuments() { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 528 | outputUpToEndOfLine("---"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | bool Output::preflightDocument(unsigned index) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 532 | if (index > 0) |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 533 | outputUpToEndOfLine("\n---"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 534 | return true; |
| 535 | } |
| 536 | |
| 537 | void Output::postflightDocument() { |
| 538 | } |
| 539 | |
| 540 | void Output::endDocuments() { |
| 541 | output("\n...\n"); |
| 542 | } |
| 543 | |
| 544 | unsigned Output::beginSequence() { |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 545 | StateStack.push_back(inSeqFirstElement); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 546 | NeedsNewLine = true; |
| 547 | return 0; |
| 548 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 549 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 550 | void Output::endSequence() { |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 551 | // If we did not emit anything, we should explicitly emit an empty sequence |
| 552 | if (StateStack.back() == inSeqFirstElement) |
| 553 | output("[]"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 554 | StateStack.pop_back(); |
| 555 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 556 | |
| 557 | bool Output::preflightElement(unsigned, void *&) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 558 | return true; |
| 559 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 560 | |
| 561 | void Output::postflightElement(void *) { |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 562 | if (StateStack.back() == inSeqFirstElement) { |
| 563 | StateStack.pop_back(); |
| 564 | StateStack.push_back(inSeqOtherElement); |
| 565 | } else if (StateStack.back() == inFlowSeqFirstElement) { |
| 566 | StateStack.pop_back(); |
| 567 | StateStack.push_back(inFlowSeqOtherElement); |
| 568 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | unsigned Output::beginFlowSequence() { |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 572 | StateStack.push_back(inFlowSeqFirstElement); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 573 | newLineCheck(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 574 | ColumnAtFlowStart = Column; |
| 575 | output("[ "); |
| 576 | NeedFlowSequenceComma = false; |
| 577 | return 0; |
| 578 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 579 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 580 | void Output::endFlowSequence() { |
| 581 | StateStack.pop_back(); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 582 | outputUpToEndOfLine(" ]"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 583 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 584 | |
| 585 | bool Output::preflightFlowElement(unsigned, void *&) { |
| 586 | if (NeedFlowSequenceComma) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 587 | output(", "); |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 588 | if (WrapColumn && Column > WrapColumn) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 589 | output("\n"); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 590 | for (int i = 0; i < ColumnAtFlowStart; ++i) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 591 | output(" "); |
| 592 | Column = ColumnAtFlowStart; |
| 593 | output(" "); |
| 594 | } |
| 595 | return true; |
| 596 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 597 | |
| 598 | void Output::postflightFlowElement(void *) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 599 | NeedFlowSequenceComma = true; |
| 600 | } |
| 601 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 602 | void Output::beginEnumScalar() { |
| 603 | EnumerationMatchFound = false; |
| 604 | } |
| 605 | |
| 606 | bool Output::matchEnumScalar(const char *Str, bool Match) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 607 | if (Match && !EnumerationMatchFound) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 608 | newLineCheck(); |
| 609 | outputUpToEndOfLine(Str); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 610 | EnumerationMatchFound = true; |
| 611 | } |
| 612 | return false; |
| 613 | } |
| 614 | |
Michael J. Spencer | 731cae3 | 2015-01-23 21:57:50 +0000 | [diff] [blame] | 615 | bool Output::matchEnumFallback() { |
| 616 | if (EnumerationMatchFound) |
| 617 | return false; |
| 618 | EnumerationMatchFound = true; |
| 619 | return true; |
| 620 | } |
| 621 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 622 | void Output::endEnumScalar() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 623 | if (!EnumerationMatchFound) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 624 | llvm_unreachable("bad runtime enum value"); |
| 625 | } |
| 626 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 627 | bool Output::beginBitSetScalar(bool &DoClear) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 628 | newLineCheck(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 629 | output("[ "); |
| 630 | NeedBitValueComma = false; |
| 631 | DoClear = false; |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | bool Output::bitSetMatch(const char *Str, bool Matches) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 636 | if (Matches) { |
| 637 | if (NeedBitValueComma) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 638 | output(", "); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 639 | output(Str); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 640 | NeedBitValueComma = true; |
| 641 | } |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | void Output::endBitSetScalar() { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 646 | outputUpToEndOfLine(" ]"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 649 | void Output::scalarString(StringRef &S, QuotingType MustQuote) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 650 | newLineCheck(); |
Rui Ueyama | 106eded | 2013-09-11 04:00:08 +0000 | [diff] [blame] | 651 | if (S.empty()) { |
| 652 | // Print '' for the empty string because leaving the field empty is not |
| 653 | // allowed. |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 654 | outputUpToEndOfLine("''"); |
Rui Ueyama | 106eded | 2013-09-11 04:00:08 +0000 | [diff] [blame] | 655 | return; |
| 656 | } |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 657 | if (MustQuote == QuotingType::None) { |
David Majnemer | 7788033 | 2014-04-10 07:37:33 +0000 | [diff] [blame] | 658 | // Only quote if we must. |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 659 | outputUpToEndOfLine(S); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 660 | return; |
| 661 | } |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 662 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 663 | unsigned i = 0; |
| 664 | unsigned j = 0; |
| 665 | unsigned End = S.size(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 666 | const char *Base = S.data(); |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 667 | |
| 668 | const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\""; |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 669 | output(Quote); // Starting quote. |
| 670 | |
Graydon Hoare | 926cd9b | 2018-03-27 19:52:45 +0000 | [diff] [blame] | 671 | // When using double-quoted strings (and only in that case), non-printable characters may be |
| 672 | // present, and will be escaped using a variety of unicode-scalar and special short-form |
| 673 | // escapes. This is handled in yaml::escape. |
| 674 | if (MustQuote == QuotingType::Double) { |
| 675 | output(yaml::escape(Base, /* EscapePrintable= */ false)); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 676 | outputUpToEndOfLine(Quote); |
Graydon Hoare | 926cd9b | 2018-03-27 19:52:45 +0000 | [diff] [blame] | 677 | return; |
| 678 | } |
| 679 | |
| 680 | // When using single-quoted strings, any single quote ' must be doubled to be escaped. |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 681 | while (j < End) { |
Graydon Hoare | 926cd9b | 2018-03-27 19:52:45 +0000 | [diff] [blame] | 682 | if (S[j] == '\'') { // Escape quotes. |
| 683 | output(StringRef(&Base[i], j - i)); // "flush". |
| 684 | output(StringLiteral("''")); // Print it as '' |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 685 | i = j + 1; |
| 686 | } |
| 687 | ++j; |
| 688 | } |
| 689 | output(StringRef(&Base[i], j - i)); |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 690 | outputUpToEndOfLine(Quote); // Ending quote. |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 693 | void Output::blockScalarString(StringRef &S) { |
| 694 | if (!StateStack.empty()) |
| 695 | newLineCheck(); |
| 696 | output(" |"); |
| 697 | outputNewLine(); |
| 698 | |
| 699 | unsigned Indent = StateStack.empty() ? 1 : StateStack.size(); |
| 700 | |
| 701 | auto Buffer = MemoryBuffer::getMemBuffer(S, "", false); |
| 702 | for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) { |
| 703 | for (unsigned I = 0; I < Indent; ++I) { |
| 704 | output(" "); |
| 705 | } |
| 706 | output(*Lines); |
| 707 | outputNewLine(); |
| 708 | } |
| 709 | } |
| 710 | |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 711 | void Output::scalarTag(std::string &Tag) { |
| 712 | if (Tag.empty()) |
| 713 | return; |
| 714 | newLineCheck(); |
| 715 | output(Tag); |
| 716 | output(" "); |
| 717 | } |
| 718 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 719 | void Output::setError(const Twine &message) { |
| 720 | } |
| 721 | |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 722 | bool Output::canElideEmptySequence() { |
| 723 | // Normally, with an optional key/value where the value is an empty sequence, |
| 724 | // the whole key/value can be not written. But, that produces wrong yaml |
| 725 | // if the key/value is the only thing in the map and the map is used in |
| 726 | // a sequence. This detects if the this sequence is the first key/value |
| 727 | // in map that itself is embedded in a sequnce. |
Rui Ueyama | 38dfffa | 2013-09-11 00:53:07 +0000 | [diff] [blame] | 728 | if (StateStack.size() < 2) |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 729 | return true; |
Rui Ueyama | 38dfffa | 2013-09-11 00:53:07 +0000 | [diff] [blame] | 730 | if (StateStack.back() != inMapFirstKey) |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 731 | return true; |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 732 | return !inSeqAnyElement(StateStack[StateStack.size() - 2]); |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 735 | void Output::output(StringRef s) { |
| 736 | Column += s.size(); |
| 737 | Out << s; |
| 738 | } |
| 739 | |
| 740 | void Output::outputUpToEndOfLine(StringRef s) { |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 741 | output(s); |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 742 | if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) && |
| 743 | !inFlowMapAnyKey(StateStack.back()))) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 744 | NeedsNewLine = true; |
| 745 | } |
| 746 | |
| 747 | void Output::outputNewLine() { |
| 748 | Out << "\n"; |
| 749 | Column = 0; |
| 750 | } |
| 751 | |
| 752 | // if seq at top, indent as if map, then add "- " |
| 753 | // if seq in middle, use "- " if firstKey, else use " " |
| 754 | // |
| 755 | |
| 756 | void Output::newLineCheck() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 757 | if (!NeedsNewLine) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 758 | return; |
| 759 | NeedsNewLine = false; |
| 760 | |
Scott Linder | ad115b7 | 2018-10-10 18:14:02 +0000 | [diff] [blame] | 761 | outputNewLine(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 762 | |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 763 | if (StateStack.size() == 0) |
| 764 | return; |
| 765 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 766 | unsigned Indent = StateStack.size() - 1; |
| 767 | bool OutputDash = false; |
| 768 | |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 769 | if (StateStack.back() == inSeqFirstElement || |
| 770 | StateStack.back() == inSeqOtherElement) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 771 | OutputDash = true; |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 772 | } else if ((StateStack.size() > 1) && |
| 773 | ((StateStack.back() == inMapFirstKey) || |
| 774 | inFlowSeqAnyElement(StateStack.back()) || |
| 775 | (StateStack.back() == inFlowMapFirstKey)) && |
| 776 | inSeqAnyElement(StateStack[StateStack.size() - 2])) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 777 | --Indent; |
| 778 | OutputDash = true; |
| 779 | } |
| 780 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 781 | for (unsigned i = 0; i < Indent; ++i) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 782 | output(" "); |
| 783 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 784 | if (OutputDash) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 785 | output("- "); |
| 786 | } |
| 787 | |
| 788 | } |
| 789 | |
| 790 | void Output::paddedKey(StringRef key) { |
| 791 | output(key); |
| 792 | output(":"); |
| 793 | const char *spaces = " "; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 794 | if (key.size() < strlen(spaces)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 795 | output(&spaces[key.size()]); |
| 796 | else |
| 797 | output(" "); |
| 798 | } |
| 799 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 800 | void Output::flowKey(StringRef Key) { |
| 801 | if (StateStack.back() == inFlowMapOtherKey) |
| 802 | output(", "); |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 803 | if (WrapColumn && Column > WrapColumn) { |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 804 | output("\n"); |
| 805 | for (int I = 0; I < ColumnAtMapFlowStart; ++I) |
| 806 | output(" "); |
| 807 | Column = ColumnAtMapFlowStart; |
| 808 | output(" "); |
| 809 | } |
| 810 | output(Key); |
| 811 | output(": "); |
| 812 | } |
| 813 | |
Scott Linder | c0830f5 | 2018-11-14 19:39:59 +0000 | [diff] [blame] | 814 | NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); } |
| 815 | |
| 816 | bool Output::inSeqAnyElement(InState State) { |
| 817 | return State == inSeqFirstElement || State == inSeqOtherElement; |
| 818 | } |
| 819 | |
| 820 | bool Output::inFlowSeqAnyElement(InState State) { |
| 821 | return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement; |
| 822 | } |
| 823 | |
| 824 | bool Output::inMapAnyKey(InState State) { |
| 825 | return State == inMapFirstKey || State == inMapOtherKey; |
| 826 | } |
| 827 | |
| 828 | bool Output::inFlowMapAnyKey(InState State) { |
| 829 | return State == inFlowMapFirstKey || State == inFlowMapOtherKey; |
| 830 | } |
| 831 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 832 | //===----------------------------------------------------------------------===// |
| 833 | // traits for built-in types |
| 834 | //===----------------------------------------------------------------------===// |
| 835 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 836 | void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { |
| 837 | Out << (Val ? "true" : "false"); |
| 838 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 839 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 840 | StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) { |
| 841 | if (Scalar.equals("true")) { |
| 842 | Val = true; |
| 843 | return StringRef(); |
| 844 | } else if (Scalar.equals("false")) { |
| 845 | Val = false; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 846 | return StringRef(); |
| 847 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 848 | return "invalid boolean"; |
| 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<StringRef>::output(const StringRef &Val, void *, |
| 852 | raw_ostream &Out) { |
| 853 | Out << Val; |
| 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<StringRef>::input(StringRef Scalar, void *, |
| 857 | StringRef &Val) { |
| 858 | Val = Scalar; |
| 859 | return StringRef(); |
| 860 | } |
Alex Rosenberg | f298f16 | 2015-01-26 18:02:18 +0000 | [diff] [blame] | 861 | |
John Thompson | 48e018a | 2013-11-19 17:28:21 +0000 | [diff] [blame] | 862 | void ScalarTraits<std::string>::output(const std::string &Val, void *, |
| 863 | raw_ostream &Out) { |
| 864 | Out << Val; |
| 865 | } |
| 866 | |
| 867 | StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, |
| 868 | std::string &Val) { |
| 869 | Val = Scalar.str(); |
| 870 | return StringRef(); |
| 871 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 872 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 873 | void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *, |
| 874 | raw_ostream &Out) { |
| 875 | // use temp uin32_t because ostream thinks uint8_t is a character |
| 876 | uint32_t Num = Val; |
| 877 | Out << Num; |
| 878 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 879 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 880 | StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) { |
| 881 | unsigned long long n; |
| 882 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 883 | return "invalid number"; |
| 884 | if (n > 0xFF) |
| 885 | return "out of range number"; |
| 886 | Val = n; |
| 887 | return StringRef(); |
| 888 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 889 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 890 | void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, |
| 891 | raw_ostream &Out) { |
| 892 | Out << Val; |
| 893 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 894 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 895 | StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *, |
| 896 | uint16_t &Val) { |
| 897 | unsigned long long n; |
| 898 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 899 | return "invalid number"; |
| 900 | if (n > 0xFFFF) |
| 901 | return "out of range number"; |
| 902 | Val = n; |
| 903 | return StringRef(); |
| 904 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 905 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 906 | void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, |
| 907 | raw_ostream &Out) { |
| 908 | Out << Val; |
| 909 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 910 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 911 | StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *, |
| 912 | uint32_t &Val) { |
| 913 | unsigned long long n; |
| 914 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 915 | return "invalid number"; |
| 916 | if (n > 0xFFFFFFFFUL) |
| 917 | return "out of range number"; |
| 918 | Val = n; |
| 919 | return StringRef(); |
| 920 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 921 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 922 | void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, |
| 923 | raw_ostream &Out) { |
| 924 | Out << Val; |
| 925 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 926 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 927 | StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *, |
| 928 | uint64_t &Val) { |
| 929 | unsigned long long N; |
| 930 | if (getAsUnsignedInteger(Scalar, 0, N)) |
| 931 | return "invalid number"; |
| 932 | Val = N; |
| 933 | return StringRef(); |
| 934 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 935 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 936 | void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) { |
| 937 | // use temp in32_t because ostream thinks int8_t is a character |
| 938 | int32_t Num = Val; |
| 939 | Out << Num; |
| 940 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 941 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 942 | StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) { |
| 943 | long long N; |
| 944 | if (getAsSignedInteger(Scalar, 0, N)) |
| 945 | return "invalid number"; |
| 946 | if ((N > 127) || (N < -128)) |
| 947 | return "out of range number"; |
| 948 | Val = N; |
| 949 | return StringRef(); |
| 950 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 951 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 952 | void ScalarTraits<int16_t>::output(const int16_t &Val, void *, |
| 953 | raw_ostream &Out) { |
| 954 | Out << Val; |
| 955 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 956 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 957 | StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) { |
| 958 | long long N; |
| 959 | if (getAsSignedInteger(Scalar, 0, N)) |
| 960 | return "invalid number"; |
| 961 | if ((N > INT16_MAX) || (N < INT16_MIN)) |
| 962 | return "out of range number"; |
| 963 | Val = N; |
| 964 | return StringRef(); |
| 965 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 966 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 967 | void ScalarTraits<int32_t>::output(const int32_t &Val, void *, |
| 968 | raw_ostream &Out) { |
| 969 | Out << Val; |
| 970 | } |
| 971 | |
| 972 | StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) { |
| 973 | long long N; |
| 974 | if (getAsSignedInteger(Scalar, 0, N)) |
| 975 | return "invalid number"; |
| 976 | if ((N > INT32_MAX) || (N < INT32_MIN)) |
| 977 | return "out of range number"; |
| 978 | Val = N; |
| 979 | return StringRef(); |
| 980 | } |
| 981 | |
| 982 | void ScalarTraits<int64_t>::output(const int64_t &Val, void *, |
| 983 | raw_ostream &Out) { |
| 984 | Out << Val; |
| 985 | } |
| 986 | |
| 987 | StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) { |
| 988 | long long N; |
| 989 | if (getAsSignedInteger(Scalar, 0, N)) |
| 990 | return "invalid number"; |
| 991 | Val = N; |
| 992 | return StringRef(); |
| 993 | } |
| 994 | |
| 995 | void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 996 | Out << format("%g", Val); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 997 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 998 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 999 | StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) { |
Pavel Labath | ec000f4 | 2017-06-23 12:55:02 +0000 | [diff] [blame] | 1000 | if (to_float(Scalar, Val)) |
| 1001 | return StringRef(); |
| 1002 | return "invalid floating point number"; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1006 | Out << format("%g", Val); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1007 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1008 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1009 | StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) { |
Pavel Labath | ec000f4 | 2017-06-23 12:55:02 +0000 | [diff] [blame] | 1010 | if (to_float(Scalar, Val)) |
| 1011 | return StringRef(); |
| 1012 | return "invalid floating point number"; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1013 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1014 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1015 | void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { |
| 1016 | uint8_t Num = Val; |
| 1017 | Out << format("0x%02X", Num); |
| 1018 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1019 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1020 | StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) { |
| 1021 | unsigned long long n; |
| 1022 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 1023 | return "invalid hex8 number"; |
| 1024 | if (n > 0xFF) |
| 1025 | return "out of range hex8 number"; |
| 1026 | Val = n; |
| 1027 | return StringRef(); |
| 1028 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1029 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1030 | void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { |
| 1031 | uint16_t Num = Val; |
| 1032 | Out << format("0x%04X", Num); |
| 1033 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1034 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1035 | StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) { |
| 1036 | unsigned long long n; |
| 1037 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 1038 | return "invalid hex16 number"; |
| 1039 | if (n > 0xFFFF) |
| 1040 | return "out of range hex16 number"; |
| 1041 | Val = n; |
| 1042 | return StringRef(); |
| 1043 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1044 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1045 | void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { |
| 1046 | uint32_t Num = Val; |
| 1047 | Out << format("0x%08X", Num); |
| 1048 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1049 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1050 | StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) { |
| 1051 | unsigned long long n; |
| 1052 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 1053 | return "invalid hex32 number"; |
| 1054 | if (n > 0xFFFFFFFFUL) |
| 1055 | return "out of range hex32 number"; |
| 1056 | Val = n; |
| 1057 | return StringRef(); |
| 1058 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1059 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1060 | void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { |
| 1061 | uint64_t Num = Val; |
| 1062 | Out << format("0x%016llX", Num); |
| 1063 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1064 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1065 | StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) { |
| 1066 | unsigned long long Num; |
| 1067 | if (getAsUnsignedInteger(Scalar, 0, Num)) |
| 1068 | return "invalid hex64 number"; |
| 1069 | Val = Num; |
| 1070 | return StringRef(); |
| 1071 | } |