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