blob: 526667fc59e7615bbb6bd7f54878a1942a658a40 [file] [log] [blame]
Nick Kledzikf60a9272012-12-12 20:46:15 +00001//===- 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
Rafael Espindola2a826e42014-06-13 17:20:48 +000010#include "llvm/Support/Errc.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/Support/YAMLTraits.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000012#include "llvm/ADT/Twine.h"
13#include "llvm/Support/Casting.h"
14#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000015#include "llvm/Support/Format.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000016#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000017#include "llvm/Support/raw_ostream.h"
Rui Ueyama106eded2013-09-11 04:00:08 +000018#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include <cstring>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000020using namespace llvm;
21using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000022
23//===----------------------------------------------------------------------===//
24// IO
25//===----------------------------------------------------------------------===//
26
27IO::IO(void *Context) : Ctxt(Context) {
28}
29
30IO::~IO() {
31}
32
33void *IO::getContext() {
34 return Ctxt;
35}
36
37void IO::setContext(void *Context) {
38 Ctxt = Context;
39}
40
Nick Kledzikf60a9272012-12-12 20:46:15 +000041//===----------------------------------------------------------------------===//
42// Input
43//===----------------------------------------------------------------------===//
44
Alexander Kornienko681e37c2013-11-18 15:50:04 +000045Input::Input(StringRef InputContent,
46 void *Ctxt,
47 SourceMgr::DiagHandlerTy DiagHandler,
48 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000049 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000050 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000051 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000052 if (DiagHandler)
53 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000054 DocIterator = Strm->begin();
55}
56
Nick Kledzik0dcef842013-01-08 21:04:44 +000057Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000058}
59
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000060std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000061
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000062// Pin the vtables to this file.
63void Input::HNode::anchor() {}
64void Input::EmptyHNode::anchor() {}
65void Input::ScalarHNode::anchor() {}
66
Nick Kledzik4761c602013-11-21 00:20:10 +000067bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000068 return false;
69}
70
71bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000072 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000073 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000074 if (!N) {
75 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000076 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000077 return false;
78 }
79
Benjamin Kramer36b0f122012-12-12 22:40:02 +000080 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000081 // Empty files are allowed and ignored
82 ++DocIterator;
83 return setCurrentDocument();
84 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000085 TopNode.reset(this->createHNodes(N));
86 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000087 return true;
88 }
89 return false;
90}
91
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000092bool Input::nextDocument() {
93 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000094}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000095
Nick Kledzik1e6033c2013-11-14 00:59:59 +000096bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000097 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +000098 if (foundTag.empty()) {
99 // If no tag found and 'Tag' is the default, say it was found.
100 return Default;
101 }
102 // Return true iff found tag matches supplied tag.
103 return Tag.equals(foundTag);
104}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000105
106void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000107 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000108 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000109 // CurrentNode can be null if the document is empty.
110 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000111 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000112 MN->ValidKeys.clear();
113 }
114}
115
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000116bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
117 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000118 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000119 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000120 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000121
122 // CurrentNode is null for empty documents, which is an error in case required
123 // nodes are present.
124 if (!CurrentNode) {
125 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000126 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000127 return false;
128 }
129
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000130 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
131 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000132 setError(CurrentNode, "not a mapping");
133 return false;
134 }
135 MN->ValidKeys.push_back(Key);
136 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000137 if (!Value) {
138 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000139 setError(CurrentNode, Twine("missing required key '") + Key + "'");
140 else
141 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000142 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000143 }
144 SaveInfo = CurrentNode;
145 CurrentNode = Value;
146 return true;
147}
148
149void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000150 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151}
152
153void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000154 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000155 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000156 // CurrentNode can be null if the document is empty.
157 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000158 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000159 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000160 for (const auto &NN : MN->Mapping) {
161 if (!MN->isValidKey(NN.first())) {
162 setError(NN.second, Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000163 break;
164 }
165 }
166}
167
Nick Kledzikf60a9272012-12-12 20:46:15 +0000168unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000169 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170 return SQ->Entries.size();
171 }
172 return 0;
173}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000174
Nick Kledzikf60a9272012-12-12 20:46:15 +0000175void Input::endSequence() {
176}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000177
Nick Kledzikf60a9272012-12-12 20:46:15 +0000178bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000179 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000181 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182 SaveInfo = CurrentNode;
183 CurrentNode = SQ->Entries[Index];
184 return true;
185 }
186 return false;
187}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000188
Nick Kledzikf60a9272012-12-12 20:46:15 +0000189void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000190 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191}
192
193unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195 return SQ->Entries.size();
196 }
197 return 0;
198}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000199
Nick Kledzikf60a9272012-12-12 20:46:15 +0000200bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000201 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000202 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204 SaveInfo = CurrentNode;
205 CurrentNode = SQ->Entries[index];
206 return true;
207 }
208 return false;
209}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000210
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000212 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213}
214
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000215void Input::endFlowSequence() {
216}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000217
218void Input::beginEnumScalar() {
219 ScalarMatchFound = false;
220}
221
222bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000223 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
226 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000227 ScalarMatchFound = true;
228 return true;
229 }
230 }
231 return false;
232}
233
234void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000235 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000236 setError(CurrentNode, "unknown enumerated scalar");
237 }
238}
239
Nick Kledzikf60a9272012-12-12 20:46:15 +0000240bool Input::beginBitSetScalar(bool &DoClear) {
241 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000242 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000243 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000244 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 setError(CurrentNode, "expected sequence of bit values");
246 }
247 DoClear = true;
248 return true;
249}
250
251bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000252 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000253 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 unsigned Index = 0;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000256 for (HNode *N : SQ->Entries) {
257 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000258 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000259 BitValuesUsed[Index] = true;
260 return true;
261 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000262 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000263 setError(CurrentNode, "unexpected scalar in sequence of bit values");
264 }
265 ++Index;
266 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000267 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000268 setError(CurrentNode, "expected sequence of bit values");
269 }
270 return false;
271}
272
273void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000274 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000275 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000276 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000277 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000278 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
279 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280 setError(SQ->Entries[i], "unknown bit value");
281 return;
282 }
283 }
284 }
285}
286
David Majnemer77880332014-04-10 07:37:33 +0000287void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291 setError(CurrentNode, "unexpected scalar");
292 }
293}
294
295void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000296 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000297 this->setError(hnode->_node, message);
298}
299
300void Input::setError(Node *node, const Twine &message) {
301 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000302 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000303}
304
305Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000306 SmallString<128> StringStorage;
307 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000308 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000310 // Copy string to permanent storage
311 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000312 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000313 memcpy(Buf, &StringStorage[0], Len);
314 KeyStr = StringRef(Buf, Len);
315 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000316 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000317 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000318 SequenceHNode *SQHNode = new SequenceHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000319 for (Node &SN : *SQ) {
320 HNode *Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000321 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322 break;
323 SQHNode->Entries.push_back(Entry);
324 }
325 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000326 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000327 MapHNode *mapHNode = new MapHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000328 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000329 Node *KeyNode = KVN.getKey();
330 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
331 if (!KeyScalar) {
332 setError(KeyNode, "Map key must be a scalar");
333 break;
334 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000335 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000336 StringRef KeyStr = KeyScalar->getValue(StringStorage);
337 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000338 // Copy string to permanent storage
339 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000340 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000341 memcpy(Buf, &StringStorage[0], Len);
342 KeyStr = StringRef(Buf, Len);
343 }
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000344 HNode *ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000345 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000346 break;
347 mapHNode->Mapping[KeyStr] = ValueHNode;
348 }
349 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000350 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000351 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000352 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000353 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000354 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000355 }
356}
357
Nick Kledzikf60a9272012-12-12 20:46:15 +0000358bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000359 for (const char *K : ValidKeys) {
360 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000361 return true;
362 }
363 return false;
364}
365
366void Input::setError(const Twine &Message) {
367 this->setError(CurrentNode, Message);
368}
369
Aaron Ballman0e63e532013-08-15 23:17:53 +0000370bool Input::canElideEmptySequence() {
371 return false;
372}
373
Nick Kledzik0dcef842013-01-08 21:04:44 +0000374Input::MapHNode::~MapHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000375 for (auto &N : Mapping)
376 delete N.second;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000377}
378
379Input::SequenceHNode::~SequenceHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000380 for (HNode *N : Entries)
381 delete N;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000382}
383
384
385
Nick Kledzikf60a9272012-12-12 20:46:15 +0000386//===----------------------------------------------------------------------===//
387// Output
388//===----------------------------------------------------------------------===//
389
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000390Output::Output(raw_ostream &yout, void *context)
391 : IO(context),
392 Out(yout),
393 Column(0),
394 ColumnAtFlowStart(0),
395 NeedBitValueComma(false),
396 NeedFlowSequenceComma(false),
397 EnumerationMatchFound(false),
398 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000399}
400
401Output::~Output() {
402}
403
Nick Kledzik4761c602013-11-21 00:20:10 +0000404bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000405 return true;
406}
407
408void Output::beginMapping() {
409 StateStack.push_back(inMapFirstKey);
410 NeedsNewLine = true;
411}
412
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000413bool Output::mapTag(StringRef Tag, bool Use) {
414 if (Use) {
415 this->output(" ");
416 this->output(Tag);
417 }
418 return Use;
419}
420
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421void Output::endMapping() {
422 StateStack.pop_back();
423}
424
Nick Kledzikf60a9272012-12-12 20:46:15 +0000425bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000426 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000427 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000428 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000429 this->newLineCheck();
430 this->paddedKey(Key);
431 return true;
432 }
433 return false;
434}
435
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000436void Output::postflightKey(void *) {
437 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000438 StateStack.pop_back();
439 StateStack.push_back(inMapOtherKey);
440 }
441}
442
443void Output::beginDocuments() {
444 this->outputUpToEndOfLine("---");
445}
446
447bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000448 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000449 this->outputUpToEndOfLine("\n---");
450 return true;
451}
452
453void Output::postflightDocument() {
454}
455
456void Output::endDocuments() {
457 output("\n...\n");
458}
459
460unsigned Output::beginSequence() {
461 StateStack.push_back(inSeq);
462 NeedsNewLine = true;
463 return 0;
464}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000465
Nick Kledzikf60a9272012-12-12 20:46:15 +0000466void Output::endSequence() {
467 StateStack.pop_back();
468}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000469
470bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000471 return true;
472}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000473
474void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000475}
476
477unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000478 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000479 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000480 ColumnAtFlowStart = Column;
481 output("[ ");
482 NeedFlowSequenceComma = false;
483 return 0;
484}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000485
Nick Kledzikf60a9272012-12-12 20:46:15 +0000486void Output::endFlowSequence() {
487 StateStack.pop_back();
488 this->outputUpToEndOfLine(" ]");
489}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000490
491bool Output::preflightFlowElement(unsigned, void *&) {
492 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000493 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000494 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000495 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000496 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000497 output(" ");
498 Column = ColumnAtFlowStart;
499 output(" ");
500 }
501 return true;
502}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000503
504void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000505 NeedFlowSequenceComma = true;
506}
507
Nick Kledzikf60a9272012-12-12 20:46:15 +0000508void Output::beginEnumScalar() {
509 EnumerationMatchFound = false;
510}
511
512bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000513 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000514 this->newLineCheck();
515 this->outputUpToEndOfLine(Str);
516 EnumerationMatchFound = true;
517 }
518 return false;
519}
520
521void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000522 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000523 llvm_unreachable("bad runtime enum value");
524}
525
Nick Kledzikf60a9272012-12-12 20:46:15 +0000526bool Output::beginBitSetScalar(bool &DoClear) {
527 this->newLineCheck();
528 output("[ ");
529 NeedBitValueComma = false;
530 DoClear = false;
531 return true;
532}
533
534bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000535 if (Matches) {
536 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000537 output(", ");
538 this->output(Str);
539 NeedBitValueComma = true;
540 }
541 return false;
542}
543
544void Output::endBitSetScalar() {
545 this->outputUpToEndOfLine(" ]");
546}
547
David Majnemer77880332014-04-10 07:37:33 +0000548void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000549 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000550 if (S.empty()) {
551 // Print '' for the empty string because leaving the field empty is not
552 // allowed.
553 this->outputUpToEndOfLine("''");
554 return;
555 }
David Majnemer77880332014-04-10 07:37:33 +0000556 if (!MustQuote) {
557 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000558 this->outputUpToEndOfLine(S);
559 return;
560 }
561 unsigned i = 0;
562 unsigned j = 0;
563 unsigned End = S.size();
564 output("'"); // Starting single quote.
565 const char *Base = S.data();
566 while (j < End) {
567 // Escape a single quote by doubling it.
568 if (S[j] == '\'') {
569 output(StringRef(&Base[i], j - i + 1));
570 output("'");
571 i = j + 1;
572 }
573 ++j;
574 }
575 output(StringRef(&Base[i], j - i));
576 this->outputUpToEndOfLine("'"); // Ending single quote.
577}
578
579void Output::setError(const Twine &message) {
580}
581
Aaron Ballman0e63e532013-08-15 23:17:53 +0000582bool Output::canElideEmptySequence() {
583 // Normally, with an optional key/value where the value is an empty sequence,
584 // the whole key/value can be not written. But, that produces wrong yaml
585 // if the key/value is the only thing in the map and the map is used in
586 // a sequence. This detects if the this sequence is the first key/value
587 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000588 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000589 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000590 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000591 return true;
592 return (StateStack[StateStack.size()-2] != inSeq);
593}
594
Nick Kledzikf60a9272012-12-12 20:46:15 +0000595void Output::output(StringRef s) {
596 Column += s.size();
597 Out << s;
598}
599
600void Output::outputUpToEndOfLine(StringRef s) {
601 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000602 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000603 NeedsNewLine = true;
604}
605
606void Output::outputNewLine() {
607 Out << "\n";
608 Column = 0;
609}
610
611// if seq at top, indent as if map, then add "- "
612// if seq in middle, use "- " if firstKey, else use " "
613//
614
615void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000616 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000617 return;
618 NeedsNewLine = false;
619
620 this->outputNewLine();
621
622 assert(StateStack.size() > 0);
623 unsigned Indent = StateStack.size() - 1;
624 bool OutputDash = false;
625
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000626 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000627 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000628 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
629 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000630 --Indent;
631 OutputDash = true;
632 }
633
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000634 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000635 output(" ");
636 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000637 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000638 output("- ");
639 }
640
641}
642
643void Output::paddedKey(StringRef key) {
644 output(key);
645 output(":");
646 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000647 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000648 output(&spaces[key.size()]);
649 else
650 output(" ");
651}
652
653//===----------------------------------------------------------------------===//
654// traits for built-in types
655//===----------------------------------------------------------------------===//
656
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000657void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
658 Out << (Val ? "true" : "false");
659}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000660
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000661StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
662 if (Scalar.equals("true")) {
663 Val = true;
664 return StringRef();
665 } else if (Scalar.equals("false")) {
666 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000667 return StringRef();
668 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000669 return "invalid boolean";
670}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000671
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000672void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
673 raw_ostream &Out) {
674 Out << Val;
675}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000676
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000677StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
678 StringRef &Val) {
679 Val = Scalar;
680 return StringRef();
681}
John Thompson48e018a2013-11-19 17:28:21 +0000682
683void ScalarTraits<std::string>::output(const std::string &Val, void *,
684 raw_ostream &Out) {
685 Out << Val;
686}
687
688StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
689 std::string &Val) {
690 Val = Scalar.str();
691 return StringRef();
692}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000693
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000694void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
695 raw_ostream &Out) {
696 // use temp uin32_t because ostream thinks uint8_t is a character
697 uint32_t Num = Val;
698 Out << Num;
699}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000700
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000701StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
702 unsigned long long n;
703 if (getAsUnsignedInteger(Scalar, 0, n))
704 return "invalid number";
705 if (n > 0xFF)
706 return "out of range number";
707 Val = n;
708 return StringRef();
709}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000710
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000711void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
712 raw_ostream &Out) {
713 Out << Val;
714}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000715
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000716StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
717 uint16_t &Val) {
718 unsigned long long n;
719 if (getAsUnsignedInteger(Scalar, 0, n))
720 return "invalid number";
721 if (n > 0xFFFF)
722 return "out of range number";
723 Val = n;
724 return StringRef();
725}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000726
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000727void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
728 raw_ostream &Out) {
729 Out << Val;
730}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000731
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000732StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
733 uint32_t &Val) {
734 unsigned long long n;
735 if (getAsUnsignedInteger(Scalar, 0, n))
736 return "invalid number";
737 if (n > 0xFFFFFFFFUL)
738 return "out of range number";
739 Val = n;
740 return StringRef();
741}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000742
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000743void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
744 raw_ostream &Out) {
745 Out << Val;
746}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000747
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000748StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
749 uint64_t &Val) {
750 unsigned long long N;
751 if (getAsUnsignedInteger(Scalar, 0, N))
752 return "invalid number";
753 Val = N;
754 return StringRef();
755}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000756
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000757void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
758 // use temp in32_t because ostream thinks int8_t is a character
759 int32_t Num = Val;
760 Out << Num;
761}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000762
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000763StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
764 long long N;
765 if (getAsSignedInteger(Scalar, 0, N))
766 return "invalid number";
767 if ((N > 127) || (N < -128))
768 return "out of range number";
769 Val = N;
770 return StringRef();
771}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000772
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000773void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
774 raw_ostream &Out) {
775 Out << Val;
776}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000777
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000778StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
779 long long N;
780 if (getAsSignedInteger(Scalar, 0, N))
781 return "invalid number";
782 if ((N > INT16_MAX) || (N < INT16_MIN))
783 return "out of range number";
784 Val = N;
785 return StringRef();
786}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000787
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000788void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
789 raw_ostream &Out) {
790 Out << Val;
791}
792
793StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
794 long long N;
795 if (getAsSignedInteger(Scalar, 0, N))
796 return "invalid number";
797 if ((N > INT32_MAX) || (N < INT32_MIN))
798 return "out of range number";
799 Val = N;
800 return StringRef();
801}
802
803void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
804 raw_ostream &Out) {
805 Out << Val;
806}
807
808StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
809 long long N;
810 if (getAsSignedInteger(Scalar, 0, N))
811 return "invalid number";
812 Val = N;
813 return StringRef();
814}
815
816void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000817 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000818}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000819
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000820StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
821 SmallString<32> buff(Scalar.begin(), Scalar.end());
822 char *end;
823 Val = strtod(buff.c_str(), &end);
824 if (*end != '\0')
825 return "invalid floating point number";
826 return StringRef();
827}
828
829void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000830 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000831}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000832
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000833StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
834 SmallString<32> buff(Scalar.begin(), Scalar.end());
835 char *end;
836 Val = strtod(buff.c_str(), &end);
837 if (*end != '\0')
838 return "invalid floating point number";
839 return StringRef();
840}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000841
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000842void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
843 uint8_t Num = Val;
844 Out << format("0x%02X", Num);
845}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000846
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000847StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
848 unsigned long long n;
849 if (getAsUnsignedInteger(Scalar, 0, n))
850 return "invalid hex8 number";
851 if (n > 0xFF)
852 return "out of range hex8 number";
853 Val = n;
854 return StringRef();
855}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000856
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000857void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
858 uint16_t Num = Val;
859 Out << format("0x%04X", Num);
860}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000861
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000862StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
863 unsigned long long n;
864 if (getAsUnsignedInteger(Scalar, 0, n))
865 return "invalid hex16 number";
866 if (n > 0xFFFF)
867 return "out of range hex16 number";
868 Val = n;
869 return StringRef();
870}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000871
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000872void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
873 uint32_t Num = Val;
874 Out << format("0x%08X", Num);
875}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000876
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000877StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
878 unsigned long long n;
879 if (getAsUnsignedInteger(Scalar, 0, n))
880 return "invalid hex32 number";
881 if (n > 0xFFFFFFFFUL)
882 return "out of range hex32 number";
883 Val = n;
884 return StringRef();
885}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000886
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000887void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
888 uint64_t Num = Val;
889 Out << format("0x%016llX", Num);
890}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000891
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000892StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
893 unsigned long long Num;
894 if (getAsUnsignedInteger(Scalar, 0, Num))
895 return "invalid hex64 number";
896 Val = Num;
897 return StringRef();
898}