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