blob: 5472e0e1dd49f27a9093b788efb1c66112b827e4 [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)),
50 CurrentNode(NULL) {
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
93void Input::nextDocument() {
94 ++DocIterator;
95}
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;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000161 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
162 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenkodf73c302013-08-07 05:51:27 +0000163 if (!MN->isValidKey(i->first())) {
164 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000165 break;
166 }
167 }
168}
169
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172 return SQ->Entries.size();
173 }
174 return 0;
175}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000176
Nick Kledzikf60a9272012-12-12 20:46:15 +0000177void Input::endSequence() {
178}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000179
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000181 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000183 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000184 SaveInfo = CurrentNode;
185 CurrentNode = SQ->Entries[Index];
186 return true;
187 }
188 return false;
189}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000190
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000192 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000193}
194
195unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000196 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000197 return SQ->Entries.size();
198 }
199 return 0;
200}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000201
Nick Kledzikf60a9272012-12-12 20:46:15 +0000202bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000205 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206 SaveInfo = CurrentNode;
207 CurrentNode = SQ->Entries[index];
208 return true;
209 }
210 return false;
211}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000212
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000214 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000215}
216
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000217void Input::endFlowSequence() {
218}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000219
220void Input::beginEnumScalar() {
221 ScalarMatchFound = false;
222}
223
224bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000227 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
228 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000229 ScalarMatchFound = true;
230 return true;
231 }
232 }
233 return false;
234}
235
236void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000237 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000238 setError(CurrentNode, "unknown enumerated scalar");
239 }
240}
241
Nick Kledzikf60a9272012-12-12 20:46:15 +0000242bool Input::beginBitSetScalar(bool &DoClear) {
243 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000244 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000246 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000247 setError(CurrentNode, "expected sequence of bit values");
248 }
249 DoClear = true;
250 return true;
251}
252
253bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000256 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257 unsigned Index = 0;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000258 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
259 End = SQ->Entries.end(); i != End; ++i) {
260 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
261 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000262 BitValuesUsed[Index] = true;
263 return true;
264 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000265 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000266 setError(CurrentNode, "unexpected scalar in sequence of bit values");
267 }
268 ++Index;
269 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000270 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000271 setError(CurrentNode, "expected sequence of bit values");
272 }
273 return false;
274}
275
276void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000277 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000278 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000279 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000281 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
282 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000283 setError(SQ->Entries[i], "unknown bit value");
284 return;
285 }
286 }
287 }
288}
289
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290void Input::scalarString(StringRef &S) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000291 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000292 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000293 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000294 setError(CurrentNode, "unexpected scalar");
295 }
296}
297
298void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000299 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000300 this->setError(hnode->_node, message);
301}
302
303void Input::setError(Node *node, const Twine &message) {
304 Strm->printError(node, message);
305 EC = make_error_code(errc::invalid_argument);
306}
307
308Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 SmallString<128> StringStorage;
310 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000311 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000312 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000313 // Copy string to permanent storage
314 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000315 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000316 memcpy(Buf, &StringStorage[0], Len);
317 KeyStr = StringRef(Buf, Len);
318 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000319 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000320 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000321 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000322 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
323 ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000324 HNode *Entry = this->createHNodes(i);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000325 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000326 break;
327 SQHNode->Entries.push_back(Entry);
328 }
329 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000330 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000331 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000332 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
333 ++i) {
334 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
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 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000344 HNode *ValueHNode = this->createHNodes(i->getValue());
345 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");
354 return NULL;
355 }
356}
357
Nick Kledzikf60a9272012-12-12 20:46:15 +0000358bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000359 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000360 End = ValidKeys.end(); i != End; ++i) {
361 if (Key.equals(*i))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000362 return true;
363 }
364 return false;
365}
366
367void Input::setError(const Twine &Message) {
368 this->setError(CurrentNode, Message);
369}
370
Aaron Ballman0e63e532013-08-15 23:17:53 +0000371bool Input::canElideEmptySequence() {
372 return false;
373}
374
Nick Kledzik0dcef842013-01-08 21:04:44 +0000375Input::MapHNode::~MapHNode() {
376 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
377 i != End; ++i) {
378 delete i->second;
379 }
380}
381
382Input::SequenceHNode::~SequenceHNode() {
383 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
384 i != End; ++i) {
385 delete *i;
386 }
387}
388
389
390
Nick Kledzikf60a9272012-12-12 20:46:15 +0000391//===----------------------------------------------------------------------===//
392// Output
393//===----------------------------------------------------------------------===//
394
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000395Output::Output(raw_ostream &yout, void *context)
396 : IO(context),
397 Out(yout),
398 Column(0),
399 ColumnAtFlowStart(0),
400 NeedBitValueComma(false),
401 NeedFlowSequenceComma(false),
402 EnumerationMatchFound(false),
403 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000404}
405
406Output::~Output() {
407}
408
Nick Kledzik4761c602013-11-21 00:20:10 +0000409bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000410 return true;
411}
412
413void Output::beginMapping() {
414 StateStack.push_back(inMapFirstKey);
415 NeedsNewLine = true;
416}
417
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000418bool Output::mapTag(StringRef Tag, bool Use) {
419 if (Use) {
420 this->output(" ");
421 this->output(Tag);
422 }
423 return Use;
424}
425
Nick Kledzikf60a9272012-12-12 20:46:15 +0000426void Output::endMapping() {
427 StateStack.pop_back();
428}
429
Nick Kledzikf60a9272012-12-12 20:46:15 +0000430bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000431 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000432 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000433 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000434 this->newLineCheck();
435 this->paddedKey(Key);
436 return true;
437 }
438 return false;
439}
440
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000441void Output::postflightKey(void *) {
442 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000443 StateStack.pop_back();
444 StateStack.push_back(inMapOtherKey);
445 }
446}
447
448void Output::beginDocuments() {
449 this->outputUpToEndOfLine("---");
450}
451
452bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000453 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000454 this->outputUpToEndOfLine("\n---");
455 return true;
456}
457
458void Output::postflightDocument() {
459}
460
461void Output::endDocuments() {
462 output("\n...\n");
463}
464
465unsigned Output::beginSequence() {
466 StateStack.push_back(inSeq);
467 NeedsNewLine = true;
468 return 0;
469}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000470
Nick Kledzikf60a9272012-12-12 20:46:15 +0000471void Output::endSequence() {
472 StateStack.pop_back();
473}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000474
475bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000476 return true;
477}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000478
479void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000480}
481
482unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000483 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000484 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000485 ColumnAtFlowStart = Column;
486 output("[ ");
487 NeedFlowSequenceComma = false;
488 return 0;
489}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000490
Nick Kledzikf60a9272012-12-12 20:46:15 +0000491void Output::endFlowSequence() {
492 StateStack.pop_back();
493 this->outputUpToEndOfLine(" ]");
494}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000495
496bool Output::preflightFlowElement(unsigned, void *&) {
497 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000498 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000499 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000500 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000501 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000502 output(" ");
503 Column = ColumnAtFlowStart;
504 output(" ");
505 }
506 return true;
507}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000508
509void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000510 NeedFlowSequenceComma = true;
511}
512
Nick Kledzikf60a9272012-12-12 20:46:15 +0000513void Output::beginEnumScalar() {
514 EnumerationMatchFound = false;
515}
516
517bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000518 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000519 this->newLineCheck();
520 this->outputUpToEndOfLine(Str);
521 EnumerationMatchFound = true;
522 }
523 return false;
524}
525
526void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000527 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000528 llvm_unreachable("bad runtime enum value");
529}
530
Nick Kledzikf60a9272012-12-12 20:46:15 +0000531bool Output::beginBitSetScalar(bool &DoClear) {
532 this->newLineCheck();
533 output("[ ");
534 NeedBitValueComma = false;
535 DoClear = false;
536 return true;
537}
538
539bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000540 if (Matches) {
541 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000542 output(", ");
543 this->output(Str);
544 NeedBitValueComma = true;
545 }
546 return false;
547}
548
549void Output::endBitSetScalar() {
550 this->outputUpToEndOfLine(" ]");
551}
552
553void Output::scalarString(StringRef &S) {
Rui Ueyama106eded2013-09-11 04:00:08 +0000554 const char ScalarSafeChars[] = "abcdefghijklmnopqrstuvwxyz"
555 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
556
Nick Kledzikf60a9272012-12-12 20:46:15 +0000557 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000558 if (S.empty()) {
559 // Print '' for the empty string because leaving the field empty is not
560 // allowed.
561 this->outputUpToEndOfLine("''");
562 return;
563 }
564 if (S.find_first_not_of(ScalarSafeChars) == StringRef::npos &&
565 !isspace(S.front()) && !isspace(S.back())) {
566 // If the string consists only of safe characters, print it out without
567 // quotes.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000568 this->outputUpToEndOfLine(S);
569 return;
570 }
571 unsigned i = 0;
572 unsigned j = 0;
573 unsigned End = S.size();
574 output("'"); // Starting single quote.
575 const char *Base = S.data();
576 while (j < End) {
577 // Escape a single quote by doubling it.
578 if (S[j] == '\'') {
579 output(StringRef(&Base[i], j - i + 1));
580 output("'");
581 i = j + 1;
582 }
583 ++j;
584 }
585 output(StringRef(&Base[i], j - i));
586 this->outputUpToEndOfLine("'"); // Ending single quote.
587}
588
589void Output::setError(const Twine &message) {
590}
591
Aaron Ballman0e63e532013-08-15 23:17:53 +0000592bool Output::canElideEmptySequence() {
593 // Normally, with an optional key/value where the value is an empty sequence,
594 // the whole key/value can be not written. But, that produces wrong yaml
595 // if the key/value is the only thing in the map and the map is used in
596 // a sequence. This detects if the this sequence is the first key/value
597 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000598 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000599 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000600 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000601 return true;
602 return (StateStack[StateStack.size()-2] != inSeq);
603}
604
Nick Kledzikf60a9272012-12-12 20:46:15 +0000605void Output::output(StringRef s) {
606 Column += s.size();
607 Out << s;
608}
609
610void Output::outputUpToEndOfLine(StringRef s) {
611 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000612 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000613 NeedsNewLine = true;
614}
615
616void Output::outputNewLine() {
617 Out << "\n";
618 Column = 0;
619}
620
621// if seq at top, indent as if map, then add "- "
622// if seq in middle, use "- " if firstKey, else use " "
623//
624
625void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000626 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000627 return;
628 NeedsNewLine = false;
629
630 this->outputNewLine();
631
632 assert(StateStack.size() > 0);
633 unsigned Indent = StateStack.size() - 1;
634 bool OutputDash = false;
635
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000636 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000637 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000638 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
639 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000640 --Indent;
641 OutputDash = true;
642 }
643
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000644 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000645 output(" ");
646 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000647 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000648 output("- ");
649 }
650
651}
652
653void Output::paddedKey(StringRef key) {
654 output(key);
655 output(":");
656 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000657 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000658 output(&spaces[key.size()]);
659 else
660 output(" ");
661}
662
663//===----------------------------------------------------------------------===//
664// traits for built-in types
665//===----------------------------------------------------------------------===//
666
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000667void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
668 Out << (Val ? "true" : "false");
669}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000670
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000671StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
672 if (Scalar.equals("true")) {
673 Val = true;
674 return StringRef();
675 } else if (Scalar.equals("false")) {
676 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000677 return StringRef();
678 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000679 return "invalid boolean";
680}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000681
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000682void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
683 raw_ostream &Out) {
684 Out << Val;
685}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000686
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000687StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
688 StringRef &Val) {
689 Val = Scalar;
690 return StringRef();
691}
John Thompson48e018a2013-11-19 17:28:21 +0000692
693void ScalarTraits<std::string>::output(const std::string &Val, void *,
694 raw_ostream &Out) {
695 Out << Val;
696}
697
698StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
699 std::string &Val) {
700 Val = Scalar.str();
701 return StringRef();
702}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000703
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000704void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
705 raw_ostream &Out) {
706 // use temp uin32_t because ostream thinks uint8_t is a character
707 uint32_t Num = Val;
708 Out << Num;
709}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000710
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000711StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
712 unsigned long long n;
713 if (getAsUnsignedInteger(Scalar, 0, n))
714 return "invalid number";
715 if (n > 0xFF)
716 return "out of range number";
717 Val = n;
718 return StringRef();
719}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000720
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000721void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
722 raw_ostream &Out) {
723 Out << Val;
724}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000725
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000726StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
727 uint16_t &Val) {
728 unsigned long long n;
729 if (getAsUnsignedInteger(Scalar, 0, n))
730 return "invalid number";
731 if (n > 0xFFFF)
732 return "out of range number";
733 Val = n;
734 return StringRef();
735}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000736
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000737void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
738 raw_ostream &Out) {
739 Out << Val;
740}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000741
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000742StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
743 uint32_t &Val) {
744 unsigned long long n;
745 if (getAsUnsignedInteger(Scalar, 0, n))
746 return "invalid number";
747 if (n > 0xFFFFFFFFUL)
748 return "out of range 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<uint64_t>::output(const uint64_t &Val, void *,
754 raw_ostream &Out) {
755 Out << Val;
756}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000757
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000758StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
759 uint64_t &Val) {
760 unsigned long long N;
761 if (getAsUnsignedInteger(Scalar, 0, N))
762 return "invalid number";
763 Val = N;
764 return StringRef();
765}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000766
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000767void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
768 // use temp in32_t because ostream thinks int8_t is a character
769 int32_t Num = Val;
770 Out << Num;
771}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000772
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000773StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
774 long long N;
775 if (getAsSignedInteger(Scalar, 0, N))
776 return "invalid number";
777 if ((N > 127) || (N < -128))
778 return "out of range number";
779 Val = N;
780 return StringRef();
781}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000782
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000783void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
784 raw_ostream &Out) {
785 Out << Val;
786}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000787
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000788StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
789 long long N;
790 if (getAsSignedInteger(Scalar, 0, N))
791 return "invalid number";
792 if ((N > INT16_MAX) || (N < INT16_MIN))
793 return "out of range number";
794 Val = N;
795 return StringRef();
796}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000797
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000798void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
799 raw_ostream &Out) {
800 Out << Val;
801}
802
803StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
804 long long N;
805 if (getAsSignedInteger(Scalar, 0, N))
806 return "invalid number";
807 if ((N > INT32_MAX) || (N < INT32_MIN))
808 return "out of range number";
809 Val = N;
810 return StringRef();
811}
812
813void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
814 raw_ostream &Out) {
815 Out << Val;
816}
817
818StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
819 long long N;
820 if (getAsSignedInteger(Scalar, 0, N))
821 return "invalid number";
822 Val = N;
823 return StringRef();
824}
825
826void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000827 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000828}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000829
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000830StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
831 SmallString<32> buff(Scalar.begin(), Scalar.end());
832 char *end;
833 Val = strtod(buff.c_str(), &end);
834 if (*end != '\0')
835 return "invalid floating point number";
836 return StringRef();
837}
838
839void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000840 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000841}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000842
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000843StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
844 SmallString<32> buff(Scalar.begin(), Scalar.end());
845 char *end;
846 Val = strtod(buff.c_str(), &end);
847 if (*end != '\0')
848 return "invalid floating point number";
849 return StringRef();
850}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000852void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
853 uint8_t Num = Val;
854 Out << format("0x%02X", Num);
855}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000856
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000857StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
858 unsigned long long n;
859 if (getAsUnsignedInteger(Scalar, 0, n))
860 return "invalid hex8 number";
861 if (n > 0xFF)
862 return "out of range hex8 number";
863 Val = n;
864 return StringRef();
865}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000866
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000867void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
868 uint16_t Num = Val;
869 Out << format("0x%04X", Num);
870}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000871
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000872StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
873 unsigned long long n;
874 if (getAsUnsignedInteger(Scalar, 0, n))
875 return "invalid hex16 number";
876 if (n > 0xFFFF)
877 return "out of range hex16 number";
878 Val = n;
879 return StringRef();
880}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000881
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000882void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
883 uint32_t Num = Val;
884 Out << format("0x%08X", Num);
885}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000886
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000887StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
888 unsigned long long n;
889 if (getAsUnsignedInteger(Scalar, 0, n))
890 return "invalid hex32 number";
891 if (n > 0xFFFFFFFFUL)
892 return "out of range hex32 number";
893 Val = n;
894 return StringRef();
895}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000896
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000897void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
898 uint64_t Num = Val;
899 Out << format("0x%016llX", Num);
900}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000901
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000902StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
903 unsigned long long Num;
904 if (getAsUnsignedInteger(Scalar, 0, Num))
905 return "invalid hex64 number";
906 Val = Num;
907 return StringRef();
908}