blob: cf19509ee88c4c1a1f1b38f3c5ed1724d6258228 [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>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000018using namespace llvm;
19using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000020
21//===----------------------------------------------------------------------===//
22// IO
23//===----------------------------------------------------------------------===//
24
25IO::IO(void *Context) : Ctxt(Context) {
26}
27
28IO::~IO() {
29}
30
31void *IO::getContext() {
32 return Ctxt;
33}
34
35void IO::setContext(void *Context) {
36 Ctxt = Context;
37}
38
Nick Kledzikf60a9272012-12-12 20:46:15 +000039//===----------------------------------------------------------------------===//
40// Input
41//===----------------------------------------------------------------------===//
42
Rui Ueyama38dfffa2013-09-11 00:53:07 +000043Input::Input(StringRef InputContent, void *Ctxt)
44 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000045 Strm(new Stream(InputContent, SrcMgr)),
46 CurrentNode(NULL) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000047 DocIterator = Strm->begin();
48}
49
Nick Kledzik0dcef842013-01-08 21:04:44 +000050Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000051}
52
Benjamin Kramer36b0f122012-12-12 22:40:02 +000053error_code Input::error() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000054 return EC;
55}
56
Benjamin Kramer36b0f122012-12-12 22:40:02 +000057void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000058 SrcMgr.setDiagHandler(Handler, Ctxt);
59}
60
61bool Input::outputting() {
62 return false;
63}
64
65bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000066 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000067 Node *N = DocIterator->getRoot();
Benjamin Kramer36b0f122012-12-12 22:40:02 +000068 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000069 // Empty files are allowed and ignored
70 ++DocIterator;
71 return setCurrentDocument();
72 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000073 TopNode.reset(this->createHNodes(N));
74 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000075 return true;
76 }
77 return false;
78}
79
80void Input::nextDocument() {
81 ++DocIterator;
82}
83
84void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000085 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +000086 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000087 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
88 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000089 MN->ValidKeys.clear();
90 }
91}
92
Benjamin Kramer36b0f122012-12-12 22:40:02 +000093bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
94 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000095 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000096 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +000097 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000098 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
99 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000100 setError(CurrentNode, "not a mapping");
101 return false;
102 }
103 MN->ValidKeys.push_back(Key);
104 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000105 if (!Value) {
106 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000107 setError(CurrentNode, Twine("missing required key '") + Key + "'");
108 else
109 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000110 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000111 }
112 SaveInfo = CurrentNode;
113 CurrentNode = Value;
114 return true;
115}
116
117void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000118 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000119}
120
121void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000122 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000123 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000124 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
125 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000126 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000127 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
128 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenkodf73c302013-08-07 05:51:27 +0000129 if (!MN->isValidKey(i->first())) {
130 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000131 break;
132 }
133 }
134}
135
Nick Kledzikf60a9272012-12-12 20:46:15 +0000136unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000137 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000138 return SQ->Entries.size();
139 }
140 return 0;
141}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000142
Nick Kledzikf60a9272012-12-12 20:46:15 +0000143void Input::endSequence() {
144}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000145
Nick Kledzikf60a9272012-12-12 20:46:15 +0000146bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000147 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000148 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000149 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000150 SaveInfo = CurrentNode;
151 CurrentNode = SQ->Entries[Index];
152 return true;
153 }
154 return false;
155}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000156
Nick Kledzikf60a9272012-12-12 20:46:15 +0000157void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000158 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000159}
160
161unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000162 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000163 return SQ->Entries.size();
164 }
165 return 0;
166}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000167
Nick Kledzikf60a9272012-12-12 20:46:15 +0000168bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000169 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172 SaveInfo = CurrentNode;
173 CurrentNode = SQ->Entries[index];
174 return true;
175 }
176 return false;
177}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000178
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000180 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000181}
182
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000183void Input::endFlowSequence() {
184}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000185
186void Input::beginEnumScalar() {
187 ScalarMatchFound = false;
188}
189
190bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000193 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
194 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195 ScalarMatchFound = true;
196 return true;
197 }
198 }
199 return false;
200}
201
202void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204 setError(CurrentNode, "unknown enumerated scalar");
205 }
206}
207
Nick Kledzikf60a9272012-12-12 20:46:15 +0000208bool Input::beginBitSetScalar(bool &DoClear) {
209 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000210 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000212 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213 setError(CurrentNode, "expected sequence of bit values");
214 }
215 DoClear = true;
216 return true;
217}
218
219bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000220 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000221 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000222 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000223 unsigned Index = 0;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000224 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
225 End = SQ->Entries.end(); i != End; ++i) {
226 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
227 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000228 BitValuesUsed[Index] = true;
229 return true;
230 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000231 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000232 setError(CurrentNode, "unexpected scalar in sequence of bit values");
233 }
234 ++Index;
235 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000236 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000237 setError(CurrentNode, "expected sequence of bit values");
238 }
239 return false;
240}
241
242void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000243 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000244 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000245 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000246 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000247 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
248 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000249 setError(SQ->Entries[i], "unknown bit value");
250 return;
251 }
252 }
253 }
254}
255
Nick Kledzikf60a9272012-12-12 20:46:15 +0000256void Input::scalarString(StringRef &S) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000257 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000258 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000259 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000260 setError(CurrentNode, "unexpected scalar");
261 }
262}
263
264void Input::setError(HNode *hnode, const Twine &message) {
265 this->setError(hnode->_node, message);
266}
267
268void Input::setError(Node *node, const Twine &message) {
269 Strm->printError(node, message);
270 EC = make_error_code(errc::invalid_argument);
271}
272
273Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000274 SmallString<128> StringStorage;
275 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000276 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000277 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000278 // Copy string to permanent storage
279 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000280 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000281 memcpy(Buf, &StringStorage[0], Len);
282 KeyStr = StringRef(Buf, Len);
283 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000284 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000285 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000286 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000287 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
288 ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 HNode *Entry = this->createHNodes(i);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291 break;
292 SQHNode->Entries.push_back(Entry);
293 }
294 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000295 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000296 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000297 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
298 ++i) {
299 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
Nick Kledzikf60a9272012-12-12 20:46:15 +0000300 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000301 StringRef KeyStr = KeyScalar->getValue(StringStorage);
302 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000303 // Copy string to permanent storage
304 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000305 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000306 memcpy(Buf, &StringStorage[0], Len);
307 KeyStr = StringRef(Buf, Len);
308 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 HNode *ValueHNode = this->createHNodes(i->getValue());
310 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000311 break;
312 mapHNode->Mapping[KeyStr] = ValueHNode;
313 }
314 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000315 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000316 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000317 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000318 setError(N, "unknown node kind");
319 return NULL;
320 }
321}
322
Nick Kledzikf60a9272012-12-12 20:46:15 +0000323bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000324 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000325 End = ValidKeys.end(); i != End; ++i) {
326 if (Key.equals(*i))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000327 return true;
328 }
329 return false;
330}
331
332void Input::setError(const Twine &Message) {
333 this->setError(CurrentNode, Message);
334}
335
Aaron Ballman0e63e532013-08-15 23:17:53 +0000336bool Input::canElideEmptySequence() {
337 return false;
338}
339
Nick Kledzik0dcef842013-01-08 21:04:44 +0000340Input::MapHNode::~MapHNode() {
341 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
342 i != End; ++i) {
343 delete i->second;
344 }
345}
346
347Input::SequenceHNode::~SequenceHNode() {
348 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
349 i != End; ++i) {
350 delete *i;
351 }
352}
353
354
355
Nick Kledzikf60a9272012-12-12 20:46:15 +0000356//===----------------------------------------------------------------------===//
357// Output
358//===----------------------------------------------------------------------===//
359
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000360Output::Output(raw_ostream &yout, void *context)
361 : IO(context),
362 Out(yout),
363 Column(0),
364 ColumnAtFlowStart(0),
365 NeedBitValueComma(false),
366 NeedFlowSequenceComma(false),
367 EnumerationMatchFound(false),
368 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000369}
370
371Output::~Output() {
372}
373
374bool Output::outputting() {
375 return true;
376}
377
378void Output::beginMapping() {
379 StateStack.push_back(inMapFirstKey);
380 NeedsNewLine = true;
381}
382
383void Output::endMapping() {
384 StateStack.pop_back();
385}
386
Nick Kledzikf60a9272012-12-12 20:46:15 +0000387bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000388 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000389 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000390 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000391 this->newLineCheck();
392 this->paddedKey(Key);
393 return true;
394 }
395 return false;
396}
397
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000398void Output::postflightKey(void *) {
399 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000400 StateStack.pop_back();
401 StateStack.push_back(inMapOtherKey);
402 }
403}
404
405void Output::beginDocuments() {
406 this->outputUpToEndOfLine("---");
407}
408
409bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000410 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000411 this->outputUpToEndOfLine("\n---");
412 return true;
413}
414
415void Output::postflightDocument() {
416}
417
418void Output::endDocuments() {
419 output("\n...\n");
420}
421
422unsigned Output::beginSequence() {
423 StateStack.push_back(inSeq);
424 NeedsNewLine = true;
425 return 0;
426}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000427
Nick Kledzikf60a9272012-12-12 20:46:15 +0000428void Output::endSequence() {
429 StateStack.pop_back();
430}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000431
432bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000433 return true;
434}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000435
436void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000437}
438
439unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000440 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000441 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000442 ColumnAtFlowStart = Column;
443 output("[ ");
444 NeedFlowSequenceComma = false;
445 return 0;
446}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000447
Nick Kledzikf60a9272012-12-12 20:46:15 +0000448void Output::endFlowSequence() {
449 StateStack.pop_back();
450 this->outputUpToEndOfLine(" ]");
451}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000452
453bool Output::preflightFlowElement(unsigned, void *&) {
454 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000455 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000456 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000457 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000458 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000459 output(" ");
460 Column = ColumnAtFlowStart;
461 output(" ");
462 }
463 return true;
464}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000465
466void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000467 NeedFlowSequenceComma = true;
468}
469
Nick Kledzikf60a9272012-12-12 20:46:15 +0000470void Output::beginEnumScalar() {
471 EnumerationMatchFound = false;
472}
473
474bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000475 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000476 this->newLineCheck();
477 this->outputUpToEndOfLine(Str);
478 EnumerationMatchFound = true;
479 }
480 return false;
481}
482
483void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000484 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000485 llvm_unreachable("bad runtime enum value");
486}
487
Nick Kledzikf60a9272012-12-12 20:46:15 +0000488bool Output::beginBitSetScalar(bool &DoClear) {
489 this->newLineCheck();
490 output("[ ");
491 NeedBitValueComma = false;
492 DoClear = false;
493 return true;
494}
495
496bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000497 if (Matches) {
498 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000499 output(", ");
500 this->output(Str);
501 NeedBitValueComma = true;
502 }
503 return false;
504}
505
506void Output::endBitSetScalar() {
507 this->outputUpToEndOfLine(" ]");
508}
509
510void Output::scalarString(StringRef &S) {
511 this->newLineCheck();
Hans Wennborg33ae7ce2013-09-11 01:59:32 +0000512 if (S.find('\n') == StringRef::npos) {
513 // No embedded new-line chars, just print string.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000514 this->outputUpToEndOfLine(S);
515 return;
516 }
517 unsigned i = 0;
518 unsigned j = 0;
519 unsigned End = S.size();
520 output("'"); // Starting single quote.
521 const char *Base = S.data();
522 while (j < End) {
523 // Escape a single quote by doubling it.
524 if (S[j] == '\'') {
525 output(StringRef(&Base[i], j - i + 1));
526 output("'");
527 i = j + 1;
528 }
529 ++j;
530 }
531 output(StringRef(&Base[i], j - i));
532 this->outputUpToEndOfLine("'"); // Ending single quote.
533}
534
535void Output::setError(const Twine &message) {
536}
537
Aaron Ballman0e63e532013-08-15 23:17:53 +0000538bool Output::canElideEmptySequence() {
539 // Normally, with an optional key/value where the value is an empty sequence,
540 // the whole key/value can be not written. But, that produces wrong yaml
541 // if the key/value is the only thing in the map and the map is used in
542 // a sequence. This detects if the this sequence is the first key/value
543 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000544 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000545 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000546 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000547 return true;
548 return (StateStack[StateStack.size()-2] != inSeq);
549}
550
Nick Kledzikf60a9272012-12-12 20:46:15 +0000551void Output::output(StringRef s) {
552 Column += s.size();
553 Out << s;
554}
555
556void Output::outputUpToEndOfLine(StringRef s) {
557 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000558 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000559 NeedsNewLine = true;
560}
561
562void Output::outputNewLine() {
563 Out << "\n";
564 Column = 0;
565}
566
567// if seq at top, indent as if map, then add "- "
568// if seq in middle, use "- " if firstKey, else use " "
569//
570
571void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000572 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000573 return;
574 NeedsNewLine = false;
575
576 this->outputNewLine();
577
578 assert(StateStack.size() > 0);
579 unsigned Indent = StateStack.size() - 1;
580 bool OutputDash = false;
581
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000582 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000583 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000584 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
585 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000586 --Indent;
587 OutputDash = true;
588 }
589
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000590 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000591 output(" ");
592 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000593 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000594 output("- ");
595 }
596
597}
598
599void Output::paddedKey(StringRef key) {
600 output(key);
601 output(":");
602 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000603 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000604 output(&spaces[key.size()]);
605 else
606 output(" ");
607}
608
609//===----------------------------------------------------------------------===//
610// traits for built-in types
611//===----------------------------------------------------------------------===//
612
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000613void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
614 Out << (Val ? "true" : "false");
615}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000616
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000617StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
618 if (Scalar.equals("true")) {
619 Val = true;
620 return StringRef();
621 } else if (Scalar.equals("false")) {
622 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000623 return StringRef();
624 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000625 return "invalid boolean";
626}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000627
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000628void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
629 raw_ostream &Out) {
630 Out << Val;
631}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000632
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000633StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
634 StringRef &Val) {
635 Val = Scalar;
636 return StringRef();
637}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000638
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000639void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
640 raw_ostream &Out) {
641 // use temp uin32_t because ostream thinks uint8_t is a character
642 uint32_t Num = Val;
643 Out << Num;
644}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000645
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000646StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
647 unsigned long long n;
648 if (getAsUnsignedInteger(Scalar, 0, n))
649 return "invalid number";
650 if (n > 0xFF)
651 return "out of range number";
652 Val = n;
653 return StringRef();
654}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000655
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000656void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
657 raw_ostream &Out) {
658 Out << Val;
659}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000660
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000661StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
662 uint16_t &Val) {
663 unsigned long long n;
664 if (getAsUnsignedInteger(Scalar, 0, n))
665 return "invalid number";
666 if (n > 0xFFFF)
667 return "out of range number";
668 Val = n;
669 return StringRef();
670}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000671
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000672void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
673 raw_ostream &Out) {
674 Out << Val;
675}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000676
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000677StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
678 uint32_t &Val) {
679 unsigned long long n;
680 if (getAsUnsignedInteger(Scalar, 0, n))
681 return "invalid number";
682 if (n > 0xFFFFFFFFUL)
683 return "out of range number";
684 Val = n;
685 return StringRef();
686}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000687
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000688void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
689 raw_ostream &Out) {
690 Out << Val;
691}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000692
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000693StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
694 uint64_t &Val) {
695 unsigned long long N;
696 if (getAsUnsignedInteger(Scalar, 0, N))
697 return "invalid number";
698 Val = N;
699 return StringRef();
700}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000701
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000702void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
703 // use temp in32_t because ostream thinks int8_t is a character
704 int32_t Num = Val;
705 Out << Num;
706}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000707
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000708StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
709 long long N;
710 if (getAsSignedInteger(Scalar, 0, N))
711 return "invalid number";
712 if ((N > 127) || (N < -128))
713 return "out of range number";
714 Val = N;
715 return StringRef();
716}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000717
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000718void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
719 raw_ostream &Out) {
720 Out << Val;
721}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000722
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000723StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
724 long long N;
725 if (getAsSignedInteger(Scalar, 0, N))
726 return "invalid number";
727 if ((N > INT16_MAX) || (N < INT16_MIN))
728 return "out of range number";
729 Val = N;
730 return StringRef();
731}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000732
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000733void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
734 raw_ostream &Out) {
735 Out << Val;
736}
737
738StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
739 long long N;
740 if (getAsSignedInteger(Scalar, 0, N))
741 return "invalid number";
742 if ((N > INT32_MAX) || (N < INT32_MIN))
743 return "out of range number";
744 Val = N;
745 return StringRef();
746}
747
748void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
749 raw_ostream &Out) {
750 Out << Val;
751}
752
753StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
754 long long N;
755 if (getAsSignedInteger(Scalar, 0, N))
756 return "invalid number";
757 Val = N;
758 return StringRef();
759}
760
761void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000762 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000763}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000764
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000765StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
766 SmallString<32> buff(Scalar.begin(), Scalar.end());
767 char *end;
768 Val = strtod(buff.c_str(), &end);
769 if (*end != '\0')
770 return "invalid floating point number";
771 return StringRef();
772}
773
774void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000775 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000776}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000777
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000778StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
779 SmallString<32> buff(Scalar.begin(), Scalar.end());
780 char *end;
781 Val = strtod(buff.c_str(), &end);
782 if (*end != '\0')
783 return "invalid floating point number";
784 return StringRef();
785}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000786
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000787void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
788 uint8_t Num = Val;
789 Out << format("0x%02X", Num);
790}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000791
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000792StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
793 unsigned long long n;
794 if (getAsUnsignedInteger(Scalar, 0, n))
795 return "invalid hex8 number";
796 if (n > 0xFF)
797 return "out of range hex8 number";
798 Val = n;
799 return StringRef();
800}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000801
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000802void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
803 uint16_t Num = Val;
804 Out << format("0x%04X", Num);
805}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000806
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000807StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
808 unsigned long long n;
809 if (getAsUnsignedInteger(Scalar, 0, n))
810 return "invalid hex16 number";
811 if (n > 0xFFFF)
812 return "out of range hex16 number";
813 Val = n;
814 return StringRef();
815}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000816
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000817void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
818 uint32_t Num = Val;
819 Out << format("0x%08X", Num);
820}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000821
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000822StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
823 unsigned long long n;
824 if (getAsUnsignedInteger(Scalar, 0, n))
825 return "invalid hex32 number";
826 if (n > 0xFFFFFFFFUL)
827 return "out of range hex32 number";
828 Val = n;
829 return StringRef();
830}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000831
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000832void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
833 uint64_t Num = Val;
834 Out << format("0x%016llX", Num);
835}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000836
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000837StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
838 unsigned long long Num;
839 if (getAsUnsignedInteger(Scalar, 0, Num))
840 return "invalid hex64 number";
841 Val = Num;
842 return StringRef();
843}