blob: 20f8b245c94267e3d2edfb09a001eb72305ae0a9 [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
Nick Kledzikf60a9272012-12-12 20:46:15 +000010#include "llvm/Support/YAMLTraits.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
13#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000014#include "llvm/Support/Format.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000015#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000016#include "llvm/Support/raw_ostream.h"
Rui Ueyama106eded2013-09-11 04:00:08 +000017#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include <cstring>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000019using namespace llvm;
20using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000021
22//===----------------------------------------------------------------------===//
23// IO
24//===----------------------------------------------------------------------===//
25
26IO::IO(void *Context) : Ctxt(Context) {
27}
28
29IO::~IO() {
30}
31
32void *IO::getContext() {
33 return Ctxt;
34}
35
36void IO::setContext(void *Context) {
37 Ctxt = Context;
38}
39
Nick Kledzikf60a9272012-12-12 20:46:15 +000040//===----------------------------------------------------------------------===//
41// Input
42//===----------------------------------------------------------------------===//
43
Alexander Kornienko681e37c2013-11-18 15:50:04 +000044Input::Input(StringRef InputContent,
45 void *Ctxt,
46 SourceMgr::DiagHandlerTy DiagHandler,
47 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000048 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000049 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000050 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000051 if (DiagHandler)
52 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000053 DocIterator = Strm->begin();
54}
55
Nick Kledzik0dcef842013-01-08 21:04:44 +000056Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000057}
58
Benjamin Kramer36b0f122012-12-12 22:40:02 +000059error_code Input::error() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000060 return EC;
61}
62
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000063// Pin the vtables to this file.
64void Input::HNode::anchor() {}
65void Input::EmptyHNode::anchor() {}
66void Input::ScalarHNode::anchor() {}
67
Nick Kledzik4761c602013-11-21 00:20:10 +000068bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000069 return false;
70}
71
72bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000073 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000074 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000075 if (!N) {
76 assert(Strm->failed() && "Root is NULL iff parsing failed");
77 EC = make_error_code(errc::invalid_argument);
78 return false;
79 }
80
Benjamin Kramer36b0f122012-12-12 22:40:02 +000081 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000082 // Empty files are allowed and ignored
83 ++DocIterator;
84 return setCurrentDocument();
85 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000086 TopNode.reset(this->createHNodes(N));
87 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000088 return true;
89 }
90 return false;
91}
92
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000093bool Input::nextDocument() {
94 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000095}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000096
Nick Kledzik1e6033c2013-11-14 00:59:59 +000097bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000098 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +000099 if (foundTag.empty()) {
100 // If no tag found and 'Tag' is the default, say it was found.
101 return Default;
102 }
103 // Return true iff found tag matches supplied tag.
104 return Tag.equals(foundTag);
105}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000106
107void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000108 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000109 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000110 // CurrentNode can be null if the document is empty.
111 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000112 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000113 MN->ValidKeys.clear();
114 }
115}
116
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000117bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
118 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000119 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000120 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000121 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000122
123 // CurrentNode is null for empty documents, which is an error in case required
124 // nodes are present.
125 if (!CurrentNode) {
126 if (Required)
127 EC = make_error_code(errc::invalid_argument);
128 return false;
129 }
130
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000131 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
132 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000133 setError(CurrentNode, "not a mapping");
134 return false;
135 }
136 MN->ValidKeys.push_back(Key);
137 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000138 if (!Value) {
139 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000140 setError(CurrentNode, Twine("missing required key '") + Key + "'");
141 else
142 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000143 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000144 }
145 SaveInfo = CurrentNode;
146 CurrentNode = Value;
147 return true;
148}
149
150void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000151 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000152}
153
154void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000155 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000156 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000157 // CurrentNode can be null if the document is empty.
158 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000159 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000160 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000161 for (const auto &NN : MN->Mapping) {
162 if (!MN->isValidKey(NN.first())) {
163 setError(NN.second, Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000164 break;
165 }
166 }
167}
168
Nick Kledzikf60a9272012-12-12 20:46:15 +0000169unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000170 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000171 return SQ->Entries.size();
172 }
173 return 0;
174}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000175
Nick Kledzikf60a9272012-12-12 20:46:15 +0000176void Input::endSequence() {
177}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000178
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000180 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000181 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000182 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000183 SaveInfo = CurrentNode;
184 CurrentNode = SQ->Entries[Index];
185 return true;
186 }
187 return false;
188}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000189
Nick Kledzikf60a9272012-12-12 20:46:15 +0000190void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192}
193
194unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000195 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000196 return SQ->Entries.size();
197 }
198 return 0;
199}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000200
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000202 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000203 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000204 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000205 SaveInfo = CurrentNode;
206 CurrentNode = SQ->Entries[index];
207 return true;
208 }
209 return false;
210}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000211
Nick Kledzikf60a9272012-12-12 20:46:15 +0000212void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000213 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000214}
215
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000216void Input::endFlowSequence() {
217}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000218
219void Input::beginEnumScalar() {
220 ScalarMatchFound = false;
221}
222
223bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000224 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000225 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000226 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
227 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000228 ScalarMatchFound = true;
229 return true;
230 }
231 }
232 return false;
233}
234
235void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000236 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000237 setError(CurrentNode, "unknown enumerated scalar");
238 }
239}
240
Nick Kledzikf60a9272012-12-12 20:46:15 +0000241bool Input::beginBitSetScalar(bool &DoClear) {
242 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000243 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000244 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000245 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000246 setError(CurrentNode, "expected sequence of bit values");
247 }
248 DoClear = true;
249 return true;
250}
251
252bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000253 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000254 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000255 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000256 unsigned Index = 0;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000257 for (HNode *N : SQ->Entries) {
258 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000259 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000260 BitValuesUsed[Index] = true;
261 return true;
262 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000263 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000264 setError(CurrentNode, "unexpected scalar in sequence of bit values");
265 }
266 ++Index;
267 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000268 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000269 setError(CurrentNode, "expected sequence of bit values");
270 }
271 return false;
272}
273
274void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000275 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000276 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000277 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000278 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000279 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
280 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000281 setError(SQ->Entries[i], "unknown bit value");
282 return;
283 }
284 }
285 }
286}
287
David Majnemer77880332014-04-10 07:37:33 +0000288void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000289 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000291 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000292 setError(CurrentNode, "unexpected scalar");
293 }
294}
295
296void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000297 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000298 this->setError(hnode->_node, message);
299}
300
301void Input::setError(Node *node, const Twine &message) {
302 Strm->printError(node, message);
303 EC = make_error_code(errc::invalid_argument);
304}
305
306Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000307 SmallString<128> StringStorage;
308 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000309 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000310 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000311 // Copy string to permanent storage
312 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000313 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000314 memcpy(Buf, &StringStorage[0], Len);
315 KeyStr = StringRef(Buf, Len);
316 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000317 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000318 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000319 SequenceHNode *SQHNode = new SequenceHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000320 for (Node &SN : *SQ) {
321 HNode *Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000322 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000323 break;
324 SQHNode->Entries.push_back(Entry);
325 }
326 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000327 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000328 MapHNode *mapHNode = new MapHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000329 for (KeyValueNode &KVN : *Map) {
330 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
Nick Kledzikf60a9272012-12-12 20:46:15 +0000331 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000332 StringRef KeyStr = KeyScalar->getValue(StringStorage);
333 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000334 // Copy string to permanent storage
335 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000336 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000337 memcpy(Buf, &StringStorage[0], Len);
338 KeyStr = StringRef(Buf, Len);
339 }
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000340 HNode *ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000341 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000342 break;
343 mapHNode->Mapping[KeyStr] = ValueHNode;
344 }
345 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000346 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000347 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000348 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000349 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000350 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000351 }
352}
353
Nick Kledzikf60a9272012-12-12 20:46:15 +0000354bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000355 for (const char *K : ValidKeys) {
356 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000357 return true;
358 }
359 return false;
360}
361
362void Input::setError(const Twine &Message) {
363 this->setError(CurrentNode, Message);
364}
365
Aaron Ballman0e63e532013-08-15 23:17:53 +0000366bool Input::canElideEmptySequence() {
367 return false;
368}
369
Nick Kledzik0dcef842013-01-08 21:04:44 +0000370Input::MapHNode::~MapHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000371 for (auto &N : Mapping)
372 delete N.second;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000373}
374
375Input::SequenceHNode::~SequenceHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000376 for (HNode *N : Entries)
377 delete N;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000378}
379
380
381
Nick Kledzikf60a9272012-12-12 20:46:15 +0000382//===----------------------------------------------------------------------===//
383// Output
384//===----------------------------------------------------------------------===//
385
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000386Output::Output(raw_ostream &yout, void *context)
387 : IO(context),
388 Out(yout),
389 Column(0),
390 ColumnAtFlowStart(0),
391 NeedBitValueComma(false),
392 NeedFlowSequenceComma(false),
393 EnumerationMatchFound(false),
394 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000395}
396
397Output::~Output() {
398}
399
Nick Kledzik4761c602013-11-21 00:20:10 +0000400bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000401 return true;
402}
403
404void Output::beginMapping() {
405 StateStack.push_back(inMapFirstKey);
406 NeedsNewLine = true;
407}
408
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000409bool Output::mapTag(StringRef Tag, bool Use) {
410 if (Use) {
411 this->output(" ");
412 this->output(Tag);
413 }
414 return Use;
415}
416
Nick Kledzikf60a9272012-12-12 20:46:15 +0000417void Output::endMapping() {
418 StateStack.pop_back();
419}
420
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000422 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000423 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000424 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000425 this->newLineCheck();
426 this->paddedKey(Key);
427 return true;
428 }
429 return false;
430}
431
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000432void Output::postflightKey(void *) {
433 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000434 StateStack.pop_back();
435 StateStack.push_back(inMapOtherKey);
436 }
437}
438
439void Output::beginDocuments() {
440 this->outputUpToEndOfLine("---");
441}
442
443bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000444 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000445 this->outputUpToEndOfLine("\n---");
446 return true;
447}
448
449void Output::postflightDocument() {
450}
451
452void Output::endDocuments() {
453 output("\n...\n");
454}
455
456unsigned Output::beginSequence() {
457 StateStack.push_back(inSeq);
458 NeedsNewLine = true;
459 return 0;
460}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000461
Nick Kledzikf60a9272012-12-12 20:46:15 +0000462void Output::endSequence() {
463 StateStack.pop_back();
464}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000465
466bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000467 return true;
468}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000469
470void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000471}
472
473unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000474 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000475 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000476 ColumnAtFlowStart = Column;
477 output("[ ");
478 NeedFlowSequenceComma = false;
479 return 0;
480}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000481
Nick Kledzikf60a9272012-12-12 20:46:15 +0000482void Output::endFlowSequence() {
483 StateStack.pop_back();
484 this->outputUpToEndOfLine(" ]");
485}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000486
487bool Output::preflightFlowElement(unsigned, void *&) {
488 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000489 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000490 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000491 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000492 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000493 output(" ");
494 Column = ColumnAtFlowStart;
495 output(" ");
496 }
497 return true;
498}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000499
500void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000501 NeedFlowSequenceComma = true;
502}
503
Nick Kledzikf60a9272012-12-12 20:46:15 +0000504void Output::beginEnumScalar() {
505 EnumerationMatchFound = false;
506}
507
508bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000509 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000510 this->newLineCheck();
511 this->outputUpToEndOfLine(Str);
512 EnumerationMatchFound = true;
513 }
514 return false;
515}
516
517void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000518 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000519 llvm_unreachable("bad runtime enum value");
520}
521
Nick Kledzikf60a9272012-12-12 20:46:15 +0000522bool Output::beginBitSetScalar(bool &DoClear) {
523 this->newLineCheck();
524 output("[ ");
525 NeedBitValueComma = false;
526 DoClear = false;
527 return true;
528}
529
530bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000531 if (Matches) {
532 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000533 output(", ");
534 this->output(Str);
535 NeedBitValueComma = true;
536 }
537 return false;
538}
539
540void Output::endBitSetScalar() {
541 this->outputUpToEndOfLine(" ]");
542}
543
David Majnemer77880332014-04-10 07:37:33 +0000544void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000545 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000546 if (S.empty()) {
547 // Print '' for the empty string because leaving the field empty is not
548 // allowed.
549 this->outputUpToEndOfLine("''");
550 return;
551 }
David Majnemer77880332014-04-10 07:37:33 +0000552 if (!MustQuote) {
553 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000554 this->outputUpToEndOfLine(S);
555 return;
556 }
557 unsigned i = 0;
558 unsigned j = 0;
559 unsigned End = S.size();
560 output("'"); // Starting single quote.
561 const char *Base = S.data();
562 while (j < End) {
563 // Escape a single quote by doubling it.
564 if (S[j] == '\'') {
565 output(StringRef(&Base[i], j - i + 1));
566 output("'");
567 i = j + 1;
568 }
569 ++j;
570 }
571 output(StringRef(&Base[i], j - i));
572 this->outputUpToEndOfLine("'"); // Ending single quote.
573}
574
575void Output::setError(const Twine &message) {
576}
577
Aaron Ballman0e63e532013-08-15 23:17:53 +0000578bool Output::canElideEmptySequence() {
579 // Normally, with an optional key/value where the value is an empty sequence,
580 // the whole key/value can be not written. But, that produces wrong yaml
581 // if the key/value is the only thing in the map and the map is used in
582 // a sequence. This detects if the this sequence is the first key/value
583 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000584 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000585 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000586 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000587 return true;
588 return (StateStack[StateStack.size()-2] != inSeq);
589}
590
Nick Kledzikf60a9272012-12-12 20:46:15 +0000591void Output::output(StringRef s) {
592 Column += s.size();
593 Out << s;
594}
595
596void Output::outputUpToEndOfLine(StringRef s) {
597 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000598 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000599 NeedsNewLine = true;
600}
601
602void Output::outputNewLine() {
603 Out << "\n";
604 Column = 0;
605}
606
607// if seq at top, indent as if map, then add "- "
608// if seq in middle, use "- " if firstKey, else use " "
609//
610
611void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000612 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000613 return;
614 NeedsNewLine = false;
615
616 this->outputNewLine();
617
618 assert(StateStack.size() > 0);
619 unsigned Indent = StateStack.size() - 1;
620 bool OutputDash = false;
621
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000622 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000623 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000624 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
625 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000626 --Indent;
627 OutputDash = true;
628 }
629
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000630 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000631 output(" ");
632 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000633 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000634 output("- ");
635 }
636
637}
638
639void Output::paddedKey(StringRef key) {
640 output(key);
641 output(":");
642 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000643 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000644 output(&spaces[key.size()]);
645 else
646 output(" ");
647}
648
649//===----------------------------------------------------------------------===//
650// traits for built-in types
651//===----------------------------------------------------------------------===//
652
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000653void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
654 Out << (Val ? "true" : "false");
655}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000656
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000657StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
658 if (Scalar.equals("true")) {
659 Val = true;
660 return StringRef();
661 } else if (Scalar.equals("false")) {
662 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000663 return StringRef();
664 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000665 return "invalid boolean";
666}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000667
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000668void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
669 raw_ostream &Out) {
670 Out << Val;
671}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000672
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000673StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
674 StringRef &Val) {
675 Val = Scalar;
676 return StringRef();
677}
John Thompson48e018a2013-11-19 17:28:21 +0000678
679void ScalarTraits<std::string>::output(const std::string &Val, void *,
680 raw_ostream &Out) {
681 Out << Val;
682}
683
684StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
685 std::string &Val) {
686 Val = Scalar.str();
687 return StringRef();
688}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000689
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000690void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
691 raw_ostream &Out) {
692 // use temp uin32_t because ostream thinks uint8_t is a character
693 uint32_t Num = Val;
694 Out << Num;
695}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000696
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000697StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
698 unsigned long long n;
699 if (getAsUnsignedInteger(Scalar, 0, n))
700 return "invalid number";
701 if (n > 0xFF)
702 return "out of range number";
703 Val = n;
704 return StringRef();
705}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000706
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000707void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
708 raw_ostream &Out) {
709 Out << Val;
710}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000711
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000712StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
713 uint16_t &Val) {
714 unsigned long long n;
715 if (getAsUnsignedInteger(Scalar, 0, n))
716 return "invalid number";
717 if (n > 0xFFFF)
718 return "out of range number";
719 Val = n;
720 return StringRef();
721}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000722
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000723void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
724 raw_ostream &Out) {
725 Out << Val;
726}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000727
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000728StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
729 uint32_t &Val) {
730 unsigned long long n;
731 if (getAsUnsignedInteger(Scalar, 0, n))
732 return "invalid number";
733 if (n > 0xFFFFFFFFUL)
734 return "out of range number";
735 Val = n;
736 return StringRef();
737}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000738
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000739void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
740 raw_ostream &Out) {
741 Out << Val;
742}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000743
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000744StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
745 uint64_t &Val) {
746 unsigned long long N;
747 if (getAsUnsignedInteger(Scalar, 0, N))
748 return "invalid number";
749 Val = N;
750 return StringRef();
751}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000752
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000753void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
754 // use temp in32_t because ostream thinks int8_t is a character
755 int32_t Num = Val;
756 Out << Num;
757}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000758
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000759StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
760 long long N;
761 if (getAsSignedInteger(Scalar, 0, N))
762 return "invalid number";
763 if ((N > 127) || (N < -128))
764 return "out of range number";
765 Val = N;
766 return StringRef();
767}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000768
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000769void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
770 raw_ostream &Out) {
771 Out << Val;
772}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000773
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000774StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
775 long long N;
776 if (getAsSignedInteger(Scalar, 0, N))
777 return "invalid number";
778 if ((N > INT16_MAX) || (N < INT16_MIN))
779 return "out of range number";
780 Val = N;
781 return StringRef();
782}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000783
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000784void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
785 raw_ostream &Out) {
786 Out << Val;
787}
788
789StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
790 long long N;
791 if (getAsSignedInteger(Scalar, 0, N))
792 return "invalid number";
793 if ((N > INT32_MAX) || (N < INT32_MIN))
794 return "out of range number";
795 Val = N;
796 return StringRef();
797}
798
799void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
800 raw_ostream &Out) {
801 Out << Val;
802}
803
804StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
805 long long N;
806 if (getAsSignedInteger(Scalar, 0, N))
807 return "invalid number";
808 Val = N;
809 return StringRef();
810}
811
812void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000813 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000814}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000815
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000816StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
817 SmallString<32> buff(Scalar.begin(), Scalar.end());
818 char *end;
819 Val = strtod(buff.c_str(), &end);
820 if (*end != '\0')
821 return "invalid floating point number";
822 return StringRef();
823}
824
825void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000826 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000827}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000828
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000829StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
830 SmallString<32> buff(Scalar.begin(), Scalar.end());
831 char *end;
832 Val = strtod(buff.c_str(), &end);
833 if (*end != '\0')
834 return "invalid floating point number";
835 return StringRef();
836}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000837
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000838void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
839 uint8_t Num = Val;
840 Out << format("0x%02X", Num);
841}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000842
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000843StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
844 unsigned long long n;
845 if (getAsUnsignedInteger(Scalar, 0, n))
846 return "invalid hex8 number";
847 if (n > 0xFF)
848 return "out of range hex8 number";
849 Val = n;
850 return StringRef();
851}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000852
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000853void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
854 uint16_t Num = Val;
855 Out << format("0x%04X", Num);
856}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000857
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000858StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
859 unsigned long long n;
860 if (getAsUnsignedInteger(Scalar, 0, n))
861 return "invalid hex16 number";
862 if (n > 0xFFFF)
863 return "out of range hex16 number";
864 Val = n;
865 return StringRef();
866}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000867
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000868void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
869 uint32_t Num = Val;
870 Out << format("0x%08X", Num);
871}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000872
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000873StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
874 unsigned long long n;
875 if (getAsUnsignedInteger(Scalar, 0, n))
876 return "invalid hex32 number";
877 if (n > 0xFFFFFFFFUL)
878 return "out of range hex32 number";
879 Val = n;
880 return StringRef();
881}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000882
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000883void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
884 uint64_t Num = Val;
885 Out << format("0x%016llX", Num);
886}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000887
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000888StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
889 unsigned long long Num;
890 if (getAsUnsignedInteger(Scalar, 0, Num))
891 return "invalid hex64 number";
892 Val = Num;
893 return StringRef();
894}