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 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 101 | TopNode = this->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 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 344 | void Input::setError(HNode *hnode, const Twine &message) { |
Alexander Kornienko | 681e37c | 2013-11-18 15:50:04 +0000 | [diff] [blame] | 345 | assert(hnode && "HNode must not be NULL"); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 346 | this->setError(hnode->_node, message); |
| 347 | } |
| 348 | |
| 349 | void Input::setError(Node *node, const Twine &message) { |
| 350 | Strm->printError(node, message); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 351 | EC = make_error_code(errc::invalid_argument); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 352 | } |
| 353 | |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 354 | std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 355 | SmallString<128> StringStorage; |
| 356 | if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 357 | StringRef KeyStr = SN->getValue(StringStorage); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 358 | if (!StringStorage.empty()) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 359 | // Copy string to permanent storage |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 360 | KeyStr = StringStorage.str().copy(StringAllocator); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 361 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 362 | return llvm::make_unique<ScalarHNode>(N, KeyStr); |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 363 | } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) { |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 364 | StringRef ValueCopy = BSN->getValue().copy(StringAllocator); |
| 365 | return llvm::make_unique<ScalarHNode>(N, ValueCopy); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 366 | } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 367 | auto SQHNode = llvm::make_unique<SequenceHNode>(N); |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 368 | for (Node &SN : *SQ) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 369 | auto Entry = this->createHNodes(&SN); |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 370 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 371 | break; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 372 | SQHNode->Entries.push_back(std::move(Entry)); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 373 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 374 | return std::move(SQHNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 375 | } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 376 | auto mapHNode = llvm::make_unique<MapHNode>(N); |
Simon Atanasyan | 878bd8a | 2014-04-10 06:02:49 +0000 | [diff] [blame] | 377 | for (KeyValueNode &KVN : *Map) { |
Rafael Espindola | a97373f | 2014-08-08 13:58:00 +0000 | [diff] [blame] | 378 | Node *KeyNode = KVN.getKey(); |
George Rimar | 3674fb6 | 2017-09-21 08:25:59 +0000 | [diff] [blame] | 379 | ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode); |
| 380 | Node *Value = KVN.getValue(); |
| 381 | if (!Key || !Value) { |
| 382 | if (!Key) |
| 383 | setError(KeyNode, "Map key must be a scalar"); |
| 384 | if (!Value) |
| 385 | setError(KeyNode, "Map value must not be empty"); |
Rafael Espindola | a97373f | 2014-08-08 13:58:00 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 388 | StringStorage.clear(); |
George Rimar | 3674fb6 | 2017-09-21 08:25:59 +0000 | [diff] [blame] | 389 | StringRef KeyStr = Key->getValue(StringStorage); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 390 | if (!StringStorage.empty()) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 391 | // Copy string to permanent storage |
Benjamin Kramer | 7a92377 | 2015-08-05 14:16:38 +0000 | [diff] [blame] | 392 | KeyStr = StringStorage.str().copy(StringAllocator); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 393 | } |
George Rimar | 3674fb6 | 2017-09-21 08:25:59 +0000 | [diff] [blame] | 394 | auto ValueHNode = this->createHNodes(Value); |
Mehdi Amini | 43c2428 | 2016-11-28 04:57:04 +0000 | [diff] [blame] | 395 | if (EC) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 396 | break; |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 397 | mapHNode->Mapping[KeyStr] = std::move(ValueHNode); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 398 | } |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 399 | return std::move(mapHNode); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 400 | } else if (isa<NullNode>(N)) { |
David Blaikie | d759fe5 | 2014-09-15 18:39:24 +0000 | [diff] [blame] | 401 | return llvm::make_unique<EmptyHNode>(N); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 402 | } else { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 403 | setError(N, "unknown node kind"); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 404 | return nullptr; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 408 | void Input::setError(const Twine &Message) { |
| 409 | this->setError(CurrentNode, Message); |
| 410 | } |
| 411 | |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 412 | bool Input::canElideEmptySequence() { |
| 413 | return false; |
| 414 | } |
| 415 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 416 | //===----------------------------------------------------------------------===// |
| 417 | // Output |
| 418 | //===----------------------------------------------------------------------===// |
| 419 | |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 420 | Output::Output(raw_ostream &yout, void *context, int WrapColumn) |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 421 | : IO(context), Out(yout), WrapColumn(WrapColumn) {} |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 422 | |
Eugene Zelenko | 72208a8 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 423 | Output::~Output() = default; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 424 | |
Nick Kledzik | 4761c60 | 2013-11-21 00:20:10 +0000 | [diff] [blame] | 425 | bool Output::outputting() { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
| 429 | void Output::beginMapping() { |
| 430 | StateStack.push_back(inMapFirstKey); |
| 431 | NeedsNewLine = true; |
| 432 | } |
| 433 | |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 434 | bool Output::mapTag(StringRef Tag, bool Use) { |
| 435 | if (Use) { |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 436 | // If this tag is being written inside a sequence we should write the start |
| 437 | // of the sequence before writing the tag, otherwise the tag won't be |
| 438 | // attached to the element in the sequence, but rather the sequence itself. |
| 439 | bool SequenceElement = |
| 440 | StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq || |
| 441 | StateStack[StateStack.size() - 2] == inFlowSeq); |
| 442 | if (SequenceElement && StateStack.back() == inMapFirstKey) { |
| 443 | this->newLineCheck(); |
| 444 | } else { |
| 445 | this->output(" "); |
| 446 | } |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 447 | this->output(Tag); |
Chris Bieneman | 92b2e8a | 2016-06-28 21:10:26 +0000 | [diff] [blame] | 448 | if (SequenceElement) { |
| 449 | // If we're writing the tag during the first element of a map, the tag |
| 450 | // takes the place of the first element in the sequence. |
| 451 | if (StateStack.back() == inMapFirstKey) { |
| 452 | StateStack.pop_back(); |
| 453 | StateStack.push_back(inMapOtherKey); |
| 454 | } |
| 455 | // Tags inside maps in sequences should act as keys in the map from a |
| 456 | // formatting perspective, so we always want a newline in a sequence. |
| 457 | NeedsNewLine = true; |
| 458 | } |
Nick Kledzik | 1e6033c | 2013-11-14 00:59:59 +0000 | [diff] [blame] | 459 | } |
| 460 | return Use; |
| 461 | } |
| 462 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 463 | void Output::endMapping() { |
| 464 | StateStack.pop_back(); |
| 465 | } |
| 466 | |
Peter Collingbourne | 87dd2ab | 2017-01-04 03:51:36 +0000 | [diff] [blame] | 467 | std::vector<StringRef> Output::keys() { |
| 468 | report_fatal_error("invalid call"); |
| 469 | } |
| 470 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 471 | bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault, |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 472 | bool &UseDefault, void *&) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 473 | UseDefault = false; |
Zachary Turner | 84efd4d | 2017-03-15 17:47:39 +0000 | [diff] [blame] | 474 | if (Required || !SameAsDefault || WriteDefaultValues) { |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 475 | auto State = StateStack.back(); |
| 476 | if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) { |
| 477 | flowKey(Key); |
| 478 | } else { |
| 479 | this->newLineCheck(); |
| 480 | this->paddedKey(Key); |
| 481 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 482 | return true; |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 487 | void Output::postflightKey(void *) { |
| 488 | if (StateStack.back() == inMapFirstKey) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 489 | StateStack.pop_back(); |
| 490 | StateStack.push_back(inMapOtherKey); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 491 | } else if (StateStack.back() == inFlowMapFirstKey) { |
| 492 | StateStack.pop_back(); |
| 493 | StateStack.push_back(inFlowMapOtherKey); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 497 | void Output::beginFlowMapping() { |
| 498 | StateStack.push_back(inFlowMapFirstKey); |
| 499 | this->newLineCheck(); |
| 500 | ColumnAtMapFlowStart = Column; |
| 501 | output("{ "); |
| 502 | } |
| 503 | |
| 504 | void Output::endFlowMapping() { |
| 505 | StateStack.pop_back(); |
| 506 | this->outputUpToEndOfLine(" }"); |
| 507 | } |
| 508 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 509 | void Output::beginDocuments() { |
| 510 | this->outputUpToEndOfLine("---"); |
| 511 | } |
| 512 | |
| 513 | bool Output::preflightDocument(unsigned index) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 514 | if (index > 0) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 515 | this->outputUpToEndOfLine("\n---"); |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | void Output::postflightDocument() { |
| 520 | } |
| 521 | |
| 522 | void Output::endDocuments() { |
| 523 | output("\n...\n"); |
| 524 | } |
| 525 | |
| 526 | unsigned Output::beginSequence() { |
| 527 | StateStack.push_back(inSeq); |
| 528 | NeedsNewLine = true; |
| 529 | return 0; |
| 530 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 531 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 532 | void Output::endSequence() { |
| 533 | StateStack.pop_back(); |
| 534 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 535 | |
| 536 | bool Output::preflightElement(unsigned, void *&) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 537 | return true; |
| 538 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 539 | |
| 540 | void Output::postflightElement(void *) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | unsigned Output::beginFlowSequence() { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 544 | StateStack.push_back(inFlowSeq); |
Nick Kledzik | 11964f2 | 2013-01-04 19:32:00 +0000 | [diff] [blame] | 545 | this->newLineCheck(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 546 | ColumnAtFlowStart = Column; |
| 547 | output("[ "); |
| 548 | NeedFlowSequenceComma = false; |
| 549 | return 0; |
| 550 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 551 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 552 | void Output::endFlowSequence() { |
| 553 | StateStack.pop_back(); |
| 554 | this->outputUpToEndOfLine(" ]"); |
| 555 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 556 | |
| 557 | bool Output::preflightFlowElement(unsigned, void *&) { |
| 558 | if (NeedFlowSequenceComma) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 559 | output(", "); |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 560 | if (WrapColumn && Column > WrapColumn) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 561 | output("\n"); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 562 | for (int i = 0; i < ColumnAtFlowStart; ++i) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 563 | output(" "); |
| 564 | Column = ColumnAtFlowStart; |
| 565 | output(" "); |
| 566 | } |
| 567 | return true; |
| 568 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 569 | |
| 570 | void Output::postflightFlowElement(void *) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 571 | NeedFlowSequenceComma = true; |
| 572 | } |
| 573 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 574 | void Output::beginEnumScalar() { |
| 575 | EnumerationMatchFound = false; |
| 576 | } |
| 577 | |
| 578 | bool Output::matchEnumScalar(const char *Str, bool Match) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 579 | if (Match && !EnumerationMatchFound) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 580 | this->newLineCheck(); |
| 581 | this->outputUpToEndOfLine(Str); |
| 582 | EnumerationMatchFound = true; |
| 583 | } |
| 584 | return false; |
| 585 | } |
| 586 | |
Michael J. Spencer | 731cae3 | 2015-01-23 21:57:50 +0000 | [diff] [blame] | 587 | bool Output::matchEnumFallback() { |
| 588 | if (EnumerationMatchFound) |
| 589 | return false; |
| 590 | EnumerationMatchFound = true; |
| 591 | return true; |
| 592 | } |
| 593 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 594 | void Output::endEnumScalar() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 595 | if (!EnumerationMatchFound) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 596 | llvm_unreachable("bad runtime enum value"); |
| 597 | } |
| 598 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 599 | bool Output::beginBitSetScalar(bool &DoClear) { |
| 600 | this->newLineCheck(); |
| 601 | output("[ "); |
| 602 | NeedBitValueComma = false; |
| 603 | DoClear = false; |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | bool Output::bitSetMatch(const char *Str, bool Matches) { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 608 | if (Matches) { |
| 609 | if (NeedBitValueComma) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 610 | output(", "); |
| 611 | this->output(Str); |
| 612 | NeedBitValueComma = true; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | void Output::endBitSetScalar() { |
| 618 | this->outputUpToEndOfLine(" ]"); |
| 619 | } |
| 620 | |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 621 | void Output::scalarString(StringRef &S, QuotingType MustQuote) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 622 | this->newLineCheck(); |
Rui Ueyama | 106eded | 2013-09-11 04:00:08 +0000 | [diff] [blame] | 623 | if (S.empty()) { |
| 624 | // Print '' for the empty string because leaving the field empty is not |
| 625 | // allowed. |
| 626 | this->outputUpToEndOfLine("''"); |
| 627 | return; |
| 628 | } |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 629 | if (MustQuote == QuotingType::None) { |
David Majnemer | 7788033 | 2014-04-10 07:37:33 +0000 | [diff] [blame] | 630 | // Only quote if we must. |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 631 | this->outputUpToEndOfLine(S); |
| 632 | return; |
| 633 | } |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 634 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 635 | unsigned i = 0; |
| 636 | unsigned j = 0; |
| 637 | unsigned End = S.size(); |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 638 | const char *Base = S.data(); |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 639 | |
| 640 | const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\""; |
| 641 | const char QuoteChar = MustQuote == QuotingType::Single ? '\'' : '"'; |
| 642 | |
| 643 | output(Quote); // Starting quote. |
| 644 | |
| 645 | // When using single-quoted strings, any single quote ' must be doubled to be |
| 646 | // escaped. |
| 647 | // When using double-quoted strings, print \x + hex for non-printable ASCII |
| 648 | // characters, and escape double quotes. |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 649 | while (j < End) { |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 650 | if (S[j] == QuoteChar) { // Escape quotes. |
| 651 | output(StringRef(&Base[i], j - i)); // "flush". |
| 652 | if (MustQuote == QuotingType::Double) { // Print it as \" |
| 653 | output(StringLiteral("\\")); |
| 654 | output(StringRef(Quote, 1)); |
| 655 | } else { // Single |
| 656 | output(StringLiteral("''")); // Print it as '' |
| 657 | } |
| 658 | i = j + 1; |
| 659 | } else if (MustQuote == QuotingType::Double && |
Francis Visoiu Mistrih | b2b961a | 2017-12-21 17:14:09 +0000 | [diff] [blame^] | 660 | !sys::unicode::isPrintable(S[j]) && (S[j] & 0x80) == 0) { |
| 661 | // If we're double quoting non-printable characters, we prefer printing |
| 662 | // them as "\x" + their hex representation. Note that special casing is |
| 663 | // needed for UTF-8, where a byte may be part of a UTF-8 sequence and |
| 664 | // appear as non-printable, in which case we want to print the correct |
| 665 | // unicode character and not its hex representation. |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 666 | output(StringRef(&Base[i], j - i)); // "flush" |
| 667 | output(StringLiteral("\\x")); |
| 668 | |
| 669 | // Output the byte 0x0F as \x0f. |
| 670 | auto FormattedHex = format_hex_no_prefix(S[j], 2); |
| 671 | Out << FormattedHex; |
| 672 | Column += 4; // one for the '\', one for the 'x', and two for the hex |
| 673 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 674 | i = j + 1; |
| 675 | } |
| 676 | ++j; |
| 677 | } |
| 678 | output(StringRef(&Base[i], j - i)); |
Francis Visoiu Mistrih | b213b27 | 2017-12-18 17:38:03 +0000 | [diff] [blame] | 679 | this->outputUpToEndOfLine(Quote); // Ending quote. |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Alex Lorenz | 68e787b | 2015-05-14 23:08:22 +0000 | [diff] [blame] | 682 | void Output::blockScalarString(StringRef &S) { |
| 683 | if (!StateStack.empty()) |
| 684 | newLineCheck(); |
| 685 | output(" |"); |
| 686 | outputNewLine(); |
| 687 | |
| 688 | unsigned Indent = StateStack.empty() ? 1 : StateStack.size(); |
| 689 | |
| 690 | auto Buffer = MemoryBuffer::getMemBuffer(S, "", false); |
| 691 | for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) { |
| 692 | for (unsigned I = 0; I < Indent; ++I) { |
| 693 | output(" "); |
| 694 | } |
| 695 | output(*Lines); |
| 696 | outputNewLine(); |
| 697 | } |
| 698 | } |
| 699 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 700 | void Output::setError(const Twine &message) { |
| 701 | } |
| 702 | |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 703 | bool Output::canElideEmptySequence() { |
| 704 | // Normally, with an optional key/value where the value is an empty sequence, |
| 705 | // the whole key/value can be not written. But, that produces wrong yaml |
| 706 | // if the key/value is the only thing in the map and the map is used in |
| 707 | // a sequence. This detects if the this sequence is the first key/value |
| 708 | // in map that itself is embedded in a sequnce. |
Rui Ueyama | 38dfffa | 2013-09-11 00:53:07 +0000 | [diff] [blame] | 709 | if (StateStack.size() < 2) |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 710 | return true; |
Rui Ueyama | 38dfffa | 2013-09-11 00:53:07 +0000 | [diff] [blame] | 711 | if (StateStack.back() != inMapFirstKey) |
Aaron Ballman | 0e63e53 | 2013-08-15 23:17:53 +0000 | [diff] [blame] | 712 | return true; |
| 713 | return (StateStack[StateStack.size()-2] != inSeq); |
| 714 | } |
| 715 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 716 | void Output::output(StringRef s) { |
| 717 | Column += s.size(); |
| 718 | Out << s; |
| 719 | } |
| 720 | |
| 721 | void Output::outputUpToEndOfLine(StringRef s) { |
| 722 | this->output(s); |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 723 | if (StateStack.empty() || (StateStack.back() != inFlowSeq && |
| 724 | StateStack.back() != inFlowMapFirstKey && |
| 725 | StateStack.back() != inFlowMapOtherKey)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 726 | NeedsNewLine = true; |
| 727 | } |
| 728 | |
| 729 | void Output::outputNewLine() { |
| 730 | Out << "\n"; |
| 731 | Column = 0; |
| 732 | } |
| 733 | |
| 734 | // if seq at top, indent as if map, then add "- " |
| 735 | // if seq in middle, use "- " if firstKey, else use " " |
| 736 | // |
| 737 | |
| 738 | void Output::newLineCheck() { |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 739 | if (!NeedsNewLine) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 740 | return; |
| 741 | NeedsNewLine = false; |
| 742 | |
| 743 | this->outputNewLine(); |
| 744 | |
| 745 | assert(StateStack.size() > 0); |
| 746 | unsigned Indent = StateStack.size() - 1; |
| 747 | bool OutputDash = false; |
| 748 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 749 | if (StateStack.back() == inSeq) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 750 | OutputDash = true; |
Alex Lorenz | 42e91fa | 2015-05-01 18:34:25 +0000 | [diff] [blame] | 751 | } else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) || |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 752 | (StateStack.back() == inFlowSeq) || |
| 753 | (StateStack.back() == inFlowMapFirstKey)) && |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 754 | (StateStack[StateStack.size() - 2] == inSeq)) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 755 | --Indent; |
| 756 | OutputDash = true; |
| 757 | } |
| 758 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 759 | for (unsigned i = 0; i < Indent; ++i) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 760 | output(" "); |
| 761 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 762 | if (OutputDash) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 763 | output("- "); |
| 764 | } |
| 765 | |
| 766 | } |
| 767 | |
| 768 | void Output::paddedKey(StringRef key) { |
| 769 | output(key); |
| 770 | output(":"); |
| 771 | const char *spaces = " "; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 772 | if (key.size() < strlen(spaces)) |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 773 | output(&spaces[key.size()]); |
| 774 | else |
| 775 | output(" "); |
| 776 | } |
| 777 | |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 778 | void Output::flowKey(StringRef Key) { |
| 779 | if (StateStack.back() == inFlowMapOtherKey) |
| 780 | output(", "); |
Frederic Riss | 4939e6a | 2015-05-29 17:56:28 +0000 | [diff] [blame] | 781 | if (WrapColumn && Column > WrapColumn) { |
Alex Lorenz | b122508 | 2015-05-04 20:11:40 +0000 | [diff] [blame] | 782 | output("\n"); |
| 783 | for (int I = 0; I < ColumnAtMapFlowStart; ++I) |
| 784 | output(" "); |
| 785 | Column = ColumnAtMapFlowStart; |
| 786 | output(" "); |
| 787 | } |
| 788 | output(Key); |
| 789 | output(": "); |
| 790 | } |
| 791 | |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 792 | //===----------------------------------------------------------------------===// |
| 793 | // traits for built-in types |
| 794 | //===----------------------------------------------------------------------===// |
| 795 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 796 | void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) { |
| 797 | Out << (Val ? "true" : "false"); |
| 798 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 799 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 800 | StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) { |
| 801 | if (Scalar.equals("true")) { |
| 802 | Val = true; |
| 803 | return StringRef(); |
| 804 | } else if (Scalar.equals("false")) { |
| 805 | Val = false; |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 806 | return StringRef(); |
| 807 | } |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 808 | return "invalid boolean"; |
| 809 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 810 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 811 | void ScalarTraits<StringRef>::output(const StringRef &Val, void *, |
| 812 | raw_ostream &Out) { |
| 813 | Out << Val; |
| 814 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 815 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 816 | StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *, |
| 817 | StringRef &Val) { |
| 818 | Val = Scalar; |
| 819 | return StringRef(); |
| 820 | } |
Alex Rosenberg | f298f16 | 2015-01-26 18:02:18 +0000 | [diff] [blame] | 821 | |
John Thompson | 48e018a | 2013-11-19 17:28:21 +0000 | [diff] [blame] | 822 | void ScalarTraits<std::string>::output(const std::string &Val, void *, |
| 823 | raw_ostream &Out) { |
| 824 | Out << Val; |
| 825 | } |
| 826 | |
| 827 | StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *, |
| 828 | std::string &Val) { |
| 829 | Val = Scalar.str(); |
| 830 | return StringRef(); |
| 831 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 832 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 833 | void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *, |
| 834 | raw_ostream &Out) { |
| 835 | // use temp uin32_t because ostream thinks uint8_t is a character |
| 836 | uint32_t Num = Val; |
| 837 | Out << Num; |
| 838 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 839 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 840 | StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) { |
| 841 | unsigned long long n; |
| 842 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 843 | return "invalid number"; |
| 844 | if (n > 0xFF) |
| 845 | return "out of range number"; |
| 846 | Val = n; |
| 847 | return StringRef(); |
| 848 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 849 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 850 | void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *, |
| 851 | raw_ostream &Out) { |
| 852 | Out << Val; |
| 853 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 854 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 855 | StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *, |
| 856 | uint16_t &Val) { |
| 857 | unsigned long long n; |
| 858 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 859 | return "invalid number"; |
| 860 | if (n > 0xFFFF) |
| 861 | return "out of range number"; |
| 862 | Val = n; |
| 863 | return StringRef(); |
| 864 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 865 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 866 | void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *, |
| 867 | raw_ostream &Out) { |
| 868 | Out << Val; |
| 869 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 870 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 871 | StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *, |
| 872 | uint32_t &Val) { |
| 873 | unsigned long long n; |
| 874 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 875 | return "invalid number"; |
| 876 | if (n > 0xFFFFFFFFUL) |
| 877 | return "out of range number"; |
| 878 | Val = n; |
| 879 | return StringRef(); |
| 880 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 881 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 882 | void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *, |
| 883 | raw_ostream &Out) { |
| 884 | Out << Val; |
| 885 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 886 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 887 | StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *, |
| 888 | uint64_t &Val) { |
| 889 | unsigned long long N; |
| 890 | if (getAsUnsignedInteger(Scalar, 0, N)) |
| 891 | return "invalid number"; |
| 892 | Val = N; |
| 893 | return StringRef(); |
| 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 | void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) { |
| 897 | // use temp in32_t because ostream thinks int8_t is a character |
| 898 | int32_t Num = Val; |
| 899 | Out << Num; |
| 900 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 901 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 902 | StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) { |
| 903 | long long N; |
| 904 | if (getAsSignedInteger(Scalar, 0, N)) |
| 905 | return "invalid number"; |
| 906 | if ((N > 127) || (N < -128)) |
| 907 | return "out of range number"; |
| 908 | Val = N; |
| 909 | return StringRef(); |
| 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 | void ScalarTraits<int16_t>::output(const int16_t &Val, void *, |
| 913 | raw_ostream &Out) { |
| 914 | Out << Val; |
| 915 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 916 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 917 | StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) { |
| 918 | long long N; |
| 919 | if (getAsSignedInteger(Scalar, 0, N)) |
| 920 | return "invalid number"; |
| 921 | if ((N > INT16_MAX) || (N < INT16_MIN)) |
| 922 | return "out of range number"; |
| 923 | Val = N; |
| 924 | return StringRef(); |
| 925 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 926 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 927 | void ScalarTraits<int32_t>::output(const int32_t &Val, void *, |
| 928 | raw_ostream &Out) { |
| 929 | Out << Val; |
| 930 | } |
| 931 | |
| 932 | StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) { |
| 933 | long long N; |
| 934 | if (getAsSignedInteger(Scalar, 0, N)) |
| 935 | return "invalid number"; |
| 936 | if ((N > INT32_MAX) || (N < INT32_MIN)) |
| 937 | return "out of range number"; |
| 938 | Val = N; |
| 939 | return StringRef(); |
| 940 | } |
| 941 | |
| 942 | void ScalarTraits<int64_t>::output(const int64_t &Val, void *, |
| 943 | raw_ostream &Out) { |
| 944 | Out << Val; |
| 945 | } |
| 946 | |
| 947 | StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) { |
| 948 | long long N; |
| 949 | if (getAsSignedInteger(Scalar, 0, N)) |
| 950 | return "invalid number"; |
| 951 | Val = N; |
| 952 | return StringRef(); |
| 953 | } |
| 954 | |
| 955 | void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 956 | Out << format("%g", Val); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 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<double>::input(StringRef Scalar, void *, double &Val) { |
Pavel Labath | ec000f4 | 2017-06-23 12:55:02 +0000 | [diff] [blame] | 960 | if (to_float(Scalar, Val)) |
| 961 | return StringRef(); |
| 962 | return "invalid floating point number"; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) { |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 966 | Out << format("%g", Val); |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 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 | StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) { |
Pavel Labath | ec000f4 | 2017-06-23 12:55:02 +0000 | [diff] [blame] | 970 | if (to_float(Scalar, Val)) |
| 971 | return StringRef(); |
| 972 | return "invalid floating point number"; |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 973 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 974 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 975 | void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) { |
| 976 | uint8_t Num = Val; |
| 977 | Out << format("0x%02X", Num); |
| 978 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 979 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 980 | StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) { |
| 981 | unsigned long long n; |
| 982 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 983 | return "invalid hex8 number"; |
| 984 | if (n > 0xFF) |
| 985 | return "out of range hex8 number"; |
| 986 | Val = n; |
| 987 | return StringRef(); |
| 988 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 989 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 990 | void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) { |
| 991 | uint16_t Num = Val; |
| 992 | Out << format("0x%04X", Num); |
| 993 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 994 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 995 | StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) { |
| 996 | unsigned long long n; |
| 997 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 998 | return "invalid hex16 number"; |
| 999 | if (n > 0xFFFF) |
| 1000 | return "out of range hex16 number"; |
| 1001 | Val = n; |
| 1002 | return StringRef(); |
| 1003 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1004 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1005 | void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) { |
| 1006 | uint32_t Num = Val; |
| 1007 | Out << format("0x%08X", Num); |
| 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<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) { |
| 1011 | unsigned long long n; |
| 1012 | if (getAsUnsignedInteger(Scalar, 0, n)) |
| 1013 | return "invalid hex32 number"; |
| 1014 | if (n > 0xFFFFFFFFUL) |
| 1015 | return "out of range hex32 number"; |
| 1016 | Val = n; |
| 1017 | return StringRef(); |
| 1018 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1019 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1020 | void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) { |
| 1021 | uint64_t Num = Val; |
| 1022 | Out << format("0x%016llX", Num); |
| 1023 | } |
Nick Kledzik | f60a927 | 2012-12-12 20:46:15 +0000 | [diff] [blame] | 1024 | |
Benjamin Kramer | 36b0f12 | 2012-12-12 22:40:02 +0000 | [diff] [blame] | 1025 | StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) { |
| 1026 | unsigned long long Num; |
| 1027 | if (getAsUnsignedInteger(Scalar, 0, Num)) |
| 1028 | return "invalid hex64 number"; |
| 1029 | Val = Num; |
| 1030 | return StringRef(); |
| 1031 | } |