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