blob: e2108e5b7ae8c3498bec018098d8aff1c4773053 [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"
Nick Kledzikf60a9272012-12-12 20:46:15 +000017#include <cstring>
Rui Ueyama106eded2013-09-11 04:00:08 +000018#include <cctype>
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
Nick Kledzikdd34f772013-11-14 02:38:07 +000063bool Input::outputting() const {
Nick Kledzikf60a9272012-12-12 20:46:15 +000064 return false;
65}
66
67bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000068 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000069 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000070 if (!N) {
71 assert(Strm->failed() && "Root is NULL iff parsing failed");
72 EC = make_error_code(errc::invalid_argument);
73 return false;
74 }
75
Benjamin Kramer36b0f122012-12-12 22:40:02 +000076 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000077 // Empty files are allowed and ignored
78 ++DocIterator;
79 return setCurrentDocument();
80 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000081 TopNode.reset(this->createHNodes(N));
82 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000083 return true;
84 }
85 return false;
86}
87
88void Input::nextDocument() {
89 ++DocIterator;
90}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000091
Nick Kledzik1e6033c2013-11-14 00:59:59 +000092bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000093 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +000094 if (foundTag.empty()) {
95 // If no tag found and 'Tag' is the default, say it was found.
96 return Default;
97 }
98 // Return true iff found tag matches supplied tag.
99 return Tag.equals(foundTag);
100}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000101
102void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000103 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000104 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000105 // CurrentNode can be null if the document is empty.
106 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000107 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000108 MN->ValidKeys.clear();
109 }
110}
111
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000112bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
113 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000114 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000115 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000116 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000117
118 // CurrentNode is null for empty documents, which is an error in case required
119 // nodes are present.
120 if (!CurrentNode) {
121 if (Required)
122 EC = make_error_code(errc::invalid_argument);
123 return false;
124 }
125
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000126 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
127 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000128 setError(CurrentNode, "not a mapping");
129 return false;
130 }
131 MN->ValidKeys.push_back(Key);
132 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000133 if (!Value) {
134 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000135 setError(CurrentNode, Twine("missing required key '") + Key + "'");
136 else
137 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000138 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000139 }
140 SaveInfo = CurrentNode;
141 CurrentNode = Value;
142 return true;
143}
144
145void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000146 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000147}
148
149void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000150 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000152 // CurrentNode can be null if the document is empty.
153 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000154 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000155 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000156 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
157 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenkodf73c302013-08-07 05:51:27 +0000158 if (!MN->isValidKey(i->first())) {
159 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000160 break;
161 }
162 }
163}
164
Nick Kledzikf60a9272012-12-12 20:46:15 +0000165unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000166 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000167 return SQ->Entries.size();
168 }
169 return 0;
170}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000171
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172void Input::endSequence() {
173}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000174
Nick Kledzikf60a9272012-12-12 20:46:15 +0000175bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000176 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000177 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000178 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179 SaveInfo = CurrentNode;
180 CurrentNode = SQ->Entries[Index];
181 return true;
182 }
183 return false;
184}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000185
Nick Kledzikf60a9272012-12-12 20:46:15 +0000186void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000187 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000188}
189
190unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192 return SQ->Entries.size();
193 }
194 return 0;
195}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000196
Nick Kledzikf60a9272012-12-12 20:46:15 +0000197bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000198 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000199 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000200 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201 SaveInfo = CurrentNode;
202 CurrentNode = SQ->Entries[index];
203 return true;
204 }
205 return false;
206}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000207
Nick Kledzikf60a9272012-12-12 20:46:15 +0000208void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000209 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000210}
211
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000212void Input::endFlowSequence() {
213}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000214
215void Input::beginEnumScalar() {
216 ScalarMatchFound = false;
217}
218
219bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000220 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000221 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000222 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
223 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 ScalarMatchFound = true;
225 return true;
226 }
227 }
228 return false;
229}
230
231void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000232 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000233 setError(CurrentNode, "unknown enumerated scalar");
234 }
235}
236
Nick Kledzikf60a9272012-12-12 20:46:15 +0000237bool Input::beginBitSetScalar(bool &DoClear) {
238 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000239 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000240 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000241 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000242 setError(CurrentNode, "expected sequence of bit values");
243 }
244 DoClear = true;
245 return true;
246}
247
248bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000249 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000250 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000251 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000252 unsigned Index = 0;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000253 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
254 End = SQ->Entries.end(); i != End; ++i) {
255 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
256 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257 BitValuesUsed[Index] = true;
258 return true;
259 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000260 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000261 setError(CurrentNode, "unexpected scalar in sequence of bit values");
262 }
263 ++Index;
264 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000265 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000266 setError(CurrentNode, "expected sequence of bit values");
267 }
268 return false;
269}
270
271void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000272 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000273 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000274 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000275 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000276 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
277 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000278 setError(SQ->Entries[i], "unknown bit value");
279 return;
280 }
281 }
282 }
283}
284
Nick Kledzikf60a9272012-12-12 20:46:15 +0000285void Input::scalarString(StringRef &S) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000286 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000287 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 setError(CurrentNode, "unexpected scalar");
290 }
291}
292
293void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000294 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000295 this->setError(hnode->_node, message);
296}
297
298void Input::setError(Node *node, const Twine &message) {
299 Strm->printError(node, message);
300 EC = make_error_code(errc::invalid_argument);
301}
302
303Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000304 SmallString<128> StringStorage;
305 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000306 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000307 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000308 // Copy string to permanent storage
309 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000310 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000311 memcpy(Buf, &StringStorage[0], Len);
312 KeyStr = StringRef(Buf, Len);
313 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000314 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000315 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000316 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000317 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
318 ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000319 HNode *Entry = this->createHNodes(i);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000320 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000321 break;
322 SQHNode->Entries.push_back(Entry);
323 }
324 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000325 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000326 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000327 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
328 ++i) {
329 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
Nick Kledzikf60a9272012-12-12 20:46:15 +0000330 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000331 StringRef KeyStr = KeyScalar->getValue(StringStorage);
332 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000333 // Copy string to permanent storage
334 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000335 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336 memcpy(Buf, &StringStorage[0], Len);
337 KeyStr = StringRef(Buf, Len);
338 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000339 HNode *ValueHNode = this->createHNodes(i->getValue());
340 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000341 break;
342 mapHNode->Mapping[KeyStr] = ValueHNode;
343 }
344 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000345 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000346 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000347 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000348 setError(N, "unknown node kind");
349 return NULL;
350 }
351}
352
Nick Kledzikf60a9272012-12-12 20:46:15 +0000353bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000354 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000355 End = ValidKeys.end(); i != End; ++i) {
356 if (Key.equals(*i))
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() {
371 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
372 i != End; ++i) {
373 delete i->second;
374 }
375}
376
377Input::SequenceHNode::~SequenceHNode() {
378 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
379 i != End; ++i) {
380 delete *i;
381 }
382}
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 Kledzikdd34f772013-11-14 02:38:07 +0000404bool Output::outputting() const {
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
548void Output::scalarString(StringRef &S) {
Rui Ueyama106eded2013-09-11 04:00:08 +0000549 const char ScalarSafeChars[] = "abcdefghijklmnopqrstuvwxyz"
550 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
551
Nick Kledzikf60a9272012-12-12 20:46:15 +0000552 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000553 if (S.empty()) {
554 // Print '' for the empty string because leaving the field empty is not
555 // allowed.
556 this->outputUpToEndOfLine("''");
557 return;
558 }
559 if (S.find_first_not_of(ScalarSafeChars) == StringRef::npos &&
560 !isspace(S.front()) && !isspace(S.back())) {
561 // If the string consists only of safe characters, print it out without
562 // quotes.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000563 this->outputUpToEndOfLine(S);
564 return;
565 }
566 unsigned i = 0;
567 unsigned j = 0;
568 unsigned End = S.size();
569 output("'"); // Starting single quote.
570 const char *Base = S.data();
571 while (j < End) {
572 // Escape a single quote by doubling it.
573 if (S[j] == '\'') {
574 output(StringRef(&Base[i], j - i + 1));
575 output("'");
576 i = j + 1;
577 }
578 ++j;
579 }
580 output(StringRef(&Base[i], j - i));
581 this->outputUpToEndOfLine("'"); // Ending single quote.
582}
583
584void Output::setError(const Twine &message) {
585}
586
Aaron Ballman0e63e532013-08-15 23:17:53 +0000587bool Output::canElideEmptySequence() {
588 // Normally, with an optional key/value where the value is an empty sequence,
589 // the whole key/value can be not written. But, that produces wrong yaml
590 // if the key/value is the only thing in the map and the map is used in
591 // a sequence. This detects if the this sequence is the first key/value
592 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000593 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000594 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000595 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000596 return true;
597 return (StateStack[StateStack.size()-2] != inSeq);
598}
599
Nick Kledzikf60a9272012-12-12 20:46:15 +0000600void Output::output(StringRef s) {
601 Column += s.size();
602 Out << s;
603}
604
605void Output::outputUpToEndOfLine(StringRef s) {
606 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000607 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000608 NeedsNewLine = true;
609}
610
611void Output::outputNewLine() {
612 Out << "\n";
613 Column = 0;
614}
615
616// if seq at top, indent as if map, then add "- "
617// if seq in middle, use "- " if firstKey, else use " "
618//
619
620void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000621 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000622 return;
623 NeedsNewLine = false;
624
625 this->outputNewLine();
626
627 assert(StateStack.size() > 0);
628 unsigned Indent = StateStack.size() - 1;
629 bool OutputDash = false;
630
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000631 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000632 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000633 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
634 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000635 --Indent;
636 OutputDash = true;
637 }
638
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000639 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000640 output(" ");
641 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000642 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000643 output("- ");
644 }
645
646}
647
648void Output::paddedKey(StringRef key) {
649 output(key);
650 output(":");
651 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000652 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000653 output(&spaces[key.size()]);
654 else
655 output(" ");
656}
657
658//===----------------------------------------------------------------------===//
659// traits for built-in types
660//===----------------------------------------------------------------------===//
661
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000662void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
663 Out << (Val ? "true" : "false");
664}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000665
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000666StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
667 if (Scalar.equals("true")) {
668 Val = true;
669 return StringRef();
670 } else if (Scalar.equals("false")) {
671 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000672 return StringRef();
673 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000674 return "invalid boolean";
675}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000676
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000677void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
678 raw_ostream &Out) {
679 Out << Val;
680}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000681
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000682StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
683 StringRef &Val) {
684 Val = Scalar;
685 return StringRef();
686}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000687
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000688void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
689 raw_ostream &Out) {
690 // use temp uin32_t because ostream thinks uint8_t is a character
691 uint32_t Num = Val;
692 Out << Num;
693}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000694
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000695StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
696 unsigned long long n;
697 if (getAsUnsignedInteger(Scalar, 0, n))
698 return "invalid number";
699 if (n > 0xFF)
700 return "out of range number";
701 Val = n;
702 return StringRef();
703}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000704
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000705void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
706 raw_ostream &Out) {
707 Out << Val;
708}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000709
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000710StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
711 uint16_t &Val) {
712 unsigned long long n;
713 if (getAsUnsignedInteger(Scalar, 0, n))
714 return "invalid number";
715 if (n > 0xFFFF)
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<uint32_t>::output(const uint32_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<uint32_t>::input(StringRef Scalar, void *,
727 uint32_t &Val) {
728 unsigned long long n;
729 if (getAsUnsignedInteger(Scalar, 0, n))
730 return "invalid number";
731 if (n > 0xFFFFFFFFUL)
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<uint64_t>::output(const uint64_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<uint64_t>::input(StringRef Scalar, void *,
743 uint64_t &Val) {
744 unsigned long long N;
745 if (getAsUnsignedInteger(Scalar, 0, N))
746 return "invalid number";
747 Val = N;
748 return StringRef();
749}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000750
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000751void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
752 // use temp in32_t because ostream thinks int8_t is a character
753 int32_t Num = Val;
754 Out << Num;
755}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000756
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000757StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
758 long long N;
759 if (getAsSignedInteger(Scalar, 0, N))
760 return "invalid number";
761 if ((N > 127) || (N < -128))
762 return "out of range 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<int16_t>::output(const int16_t &Val, void *,
768 raw_ostream &Out) {
769 Out << Val;
770}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000771
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000772StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
773 long long N;
774 if (getAsSignedInteger(Scalar, 0, N))
775 return "invalid number";
776 if ((N > INT16_MAX) || (N < INT16_MIN))
777 return "out of range number";
778 Val = N;
779 return StringRef();
780}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000781
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000782void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
783 raw_ostream &Out) {
784 Out << Val;
785}
786
787StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
788 long long N;
789 if (getAsSignedInteger(Scalar, 0, N))
790 return "invalid number";
791 if ((N > INT32_MAX) || (N < INT32_MIN))
792 return "out of range number";
793 Val = N;
794 return StringRef();
795}
796
797void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
798 raw_ostream &Out) {
799 Out << Val;
800}
801
802StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
803 long long N;
804 if (getAsSignedInteger(Scalar, 0, N))
805 return "invalid number";
806 Val = N;
807 return StringRef();
808}
809
810void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000811 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000812}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000813
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000814StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
815 SmallString<32> buff(Scalar.begin(), Scalar.end());
816 char *end;
817 Val = strtod(buff.c_str(), &end);
818 if (*end != '\0')
819 return "invalid floating point number";
820 return StringRef();
821}
822
823void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000824 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000825}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000826
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000827StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
828 SmallString<32> buff(Scalar.begin(), Scalar.end());
829 char *end;
830 Val = strtod(buff.c_str(), &end);
831 if (*end != '\0')
832 return "invalid floating point number";
833 return StringRef();
834}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000835
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000836void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
837 uint8_t Num = Val;
838 Out << format("0x%02X", Num);
839}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000840
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000841StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
842 unsigned long long n;
843 if (getAsUnsignedInteger(Scalar, 0, n))
844 return "invalid hex8 number";
845 if (n > 0xFF)
846 return "out of range hex8 number";
847 Val = n;
848 return StringRef();
849}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000850
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000851void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
852 uint16_t Num = Val;
853 Out << format("0x%04X", Num);
854}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000855
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000856StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
857 unsigned long long n;
858 if (getAsUnsignedInteger(Scalar, 0, n))
859 return "invalid hex16 number";
860 if (n > 0xFFFF)
861 return "out of range hex16 number";
862 Val = n;
863 return StringRef();
864}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000865
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000866void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
867 uint32_t Num = Val;
868 Out << format("0x%08X", Num);
869}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000870
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000871StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
872 unsigned long long n;
873 if (getAsUnsignedInteger(Scalar, 0, n))
874 return "invalid hex32 number";
875 if (n > 0xFFFFFFFFUL)
876 return "out of range hex32 number";
877 Val = n;
878 return StringRef();
879}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000880
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000881void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
882 uint64_t Num = Val;
883 Out << format("0x%016llX", Num);
884}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000885
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000886StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
887 unsigned long long Num;
888 if (getAsUnsignedInteger(Scalar, 0, Num))
889 return "invalid hex64 number";
890 Val = Num;
891 return StringRef();
892}