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