blob: d0e4b1b902dc8516c383234e320b9e18dfc30c20 [file] [log] [blame]
Nick Kledzik8ceb8b72012-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 Kledzik8ceb8b72012-12-12 20:46:15 +000010#include "llvm/Support/YAMLTraits.h"
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000011#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
13#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer11b07f62012-12-12 20:55:44 +000014#include "llvm/Support/Format.h"
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000015#include "llvm/Support/YAMLParser.h"
Benjamin Kramer11b07f62012-12-12 20:55:44 +000016#include "llvm/Support/raw_ostream.h"
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000017#include <cstring>
Benjamin Kramerae3ce262012-12-12 22:40:02 +000018using namespace llvm;
19using namespace yaml;
Nick Kledzik8ceb8b72012-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 Kledzik8ceb8b72012-12-12 20:46:15 +000039//===----------------------------------------------------------------------===//
40// Input
41//===----------------------------------------------------------------------===//
42
Rui Ueyamae9cf2832013-09-11 00:53:07 +000043Input::Input(StringRef InputContent, void *Ctxt)
44 : IO(Ctxt),
Nick Kledzik02fa3832013-01-08 21:04:44 +000045 Strm(new Stream(InputContent, SrcMgr)),
46 CurrentNode(NULL) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000047 DocIterator = Strm->begin();
48}
49
Nick Kledzik02fa3832013-01-08 21:04:44 +000050Input::~Input() {
Nick Kledzik02fa3832013-01-08 21:04:44 +000051}
52
Benjamin Kramerae3ce262012-12-12 22:40:02 +000053error_code Input::error() {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000054 return EC;
55}
56
Benjamin Kramerae3ce262012-12-12 22:40:02 +000057void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000058 SrcMgr.setDiagHandler(Handler, Ctxt);
59}
60
61bool Input::outputting() {
62 return false;
63}
64
65bool Input::setCurrentDocument() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +000066 if (DocIterator != Strm->end()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000067 Node *N = DocIterator->getRoot();
Benjamin Kramerae3ce262012-12-12 22:40:02 +000068 if (isa<NullNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000069 // Empty files are allowed and ignored
70 ++DocIterator;
71 return setCurrentDocument();
72 }
Nick Kledzik02fa3832013-01-08 21:04:44 +000073 TopNode.reset(this->createHNodes(N));
74 CurrentNode = TopNode.get();
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +000085 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000086 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000087 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
88 if (MN) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000089 MN->ValidKeys.clear();
90 }
91}
92
Benjamin Kramerae3ce262012-12-12 22:40:02 +000093bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
94 void *&SaveInfo) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000095 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000096 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000097 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000098 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
99 if (!MN) {
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000105 if (!Value) {
106 if (Required)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000107 setError(CurrentNode, Twine("missing required key '") + Key + "'");
108 else
109 UseDefault = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000110 return false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000111 }
112 SaveInfo = CurrentNode;
113 CurrentNode = Value;
114 return true;
115}
116
117void Input::postflightKey(void *saveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000118 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000119}
120
121void Input::endMapping() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000122 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000123 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000124 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
125 if (!MN)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000126 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000127 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
128 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenko9e8eafa2013-08-07 05:51:27 +0000129 if (!MN->isValidKey(i->first())) {
130 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000131 break;
132 }
133 }
134}
135
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000136unsigned Input::beginSequence() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000137 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000138 return SQ->Entries.size();
139 }
140 return 0;
141}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000142
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000143void Input::endSequence() {
144}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000145
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000146bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000147 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000148 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000149 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000150 SaveInfo = CurrentNode;
151 CurrentNode = SQ->Entries[Index];
152 return true;
153 }
154 return false;
155}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000156
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000157void Input::postflightElement(void *SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000158 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000159}
160
161unsigned Input::beginFlowSequence() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000162 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000163 return SQ->Entries.size();
164 }
165 return 0;
166}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000167
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000168bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000169 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000170 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000172 SaveInfo = CurrentNode;
173 CurrentNode = SQ->Entries[index];
174 return true;
175 }
176 return false;
177}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000178
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000179void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000180 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000181}
182
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000183void Input::endFlowSequence() {
184}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000185
186void Input::beginEnumScalar() {
187 ScalarMatchFound = false;
188}
189
190bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000191 if (ScalarMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000192 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000193 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
194 if (SN->value().equals(Str)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000195 ScalarMatchFound = true;
196 return true;
197 }
198 }
199 return false;
200}
201
202void Input::endEnumScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000203 if (!ScalarMatchFound) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000204 setError(CurrentNode, "unknown enumerated scalar");
205 }
206}
207
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000208bool Input::beginBitSetScalar(bool &DoClear) {
209 BitValuesUsed.clear();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000210 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000211 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000212 } else {
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000220 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000221 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000222 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000223 unsigned Index = 0;
Benjamin Kramerae3ce262012-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 Kledzik8ceb8b72012-12-12 20:46:15 +0000228 BitValuesUsed[Index] = true;
229 return true;
230 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000231 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000232 setError(CurrentNode, "unexpected scalar in sequence of bit values");
233 }
234 ++Index;
235 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000236 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000237 setError(CurrentNode, "expected sequence of bit values");
238 }
239 return false;
240}
241
242void Input::endBitSetScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000243 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000244 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000245 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000246 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000247 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
248 if (!BitValuesUsed[i]) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000249 setError(SQ->Entries[i], "unknown bit value");
250 return;
251 }
252 }
253 }
254}
255
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000256void Input::scalarString(StringRef &S) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000257 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000258 S = SN->value();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000259 } else {
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000274 SmallString<128> StringStorage;
275 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000276 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000277 if (!StringStorage.empty()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000278 // Copy string to permanent storage
279 unsigned Len = StringStorage.size();
Nick Kledzik02fa3832013-01-08 21:04:44 +0000280 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000281 memcpy(Buf, &StringStorage[0], Len);
282 KeyStr = StringRef(Buf, Len);
283 }
Nick Kledzik02fa3832013-01-08 21:04:44 +0000284 return new ScalarHNode(N, KeyStr);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000285 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik02fa3832013-01-08 21:04:44 +0000286 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000287 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
288 ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000289 HNode *Entry = this->createHNodes(i);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000290 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000291 break;
292 SQHNode->Entries.push_back(Entry);
293 }
294 return SQHNode;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000295 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik02fa3832013-01-08 21:04:44 +0000296 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramerae3ce262012-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 Kledzik8ceb8b72012-12-12 20:46:15 +0000300 StringStorage.clear();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000301 StringRef KeyStr = KeyScalar->getValue(StringStorage);
302 if (!StringStorage.empty()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000303 // Copy string to permanent storage
304 unsigned Len = StringStorage.size();
Nick Kledzik02fa3832013-01-08 21:04:44 +0000305 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000306 memcpy(Buf, &StringStorage[0], Len);
307 KeyStr = StringRef(Buf, Len);
308 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000309 HNode *ValueHNode = this->createHNodes(i->getValue());
310 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000311 break;
312 mapHNode->Mapping[KeyStr] = ValueHNode;
313 }
314 return mapHNode;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000315 } else if (isa<NullNode>(N)) {
Nick Kledzik02fa3832013-01-08 21:04:44 +0000316 return new EmptyHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000317 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000318 setError(N, "unknown node kind");
319 return NULL;
320 }
321}
322
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000323bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000324 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000325 End = ValidKeys.end(); i != End; ++i) {
326 if (Key.equals(*i))
Nick Kledzik8ceb8b72012-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 Ballmand5f33aa2013-08-15 23:17:53 +0000336bool Input::canElideEmptySequence() {
337 return false;
338}
339
Nick Kledzik02fa3832013-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 Kledzik8ceb8b72012-12-12 20:46:15 +0000356//===----------------------------------------------------------------------===//
357// Output
358//===----------------------------------------------------------------------===//
359
Benjamin Kramerae3ce262012-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 Kledzik8ceb8b72012-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 Kledzik8ceb8b72012-12-12 20:46:15 +0000387bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000388 bool &UseDefault, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000389 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000390 if (Required || !SameAsDefault) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000391 this->newLineCheck();
392 this->paddedKey(Key);
393 return true;
394 }
395 return false;
396}
397
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000398void Output::postflightKey(void *) {
399 if (StateStack.back() == inMapFirstKey) {
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000410 if (index > 0)
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000427
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000428void Output::endSequence() {
429 StateStack.pop_back();
430}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000431
432bool Output::preflightElement(unsigned, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000433 return true;
434}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000435
436void Output::postflightElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000437}
438
439unsigned Output::beginFlowSequence() {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000440 StateStack.push_back(inFlowSeq);
Nick Kledzik50c30422013-01-04 19:32:00 +0000441 this->newLineCheck();
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000442 ColumnAtFlowStart = Column;
443 output("[ ");
444 NeedFlowSequenceComma = false;
445 return 0;
446}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000447
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000448void Output::endFlowSequence() {
449 StateStack.pop_back();
450 this->outputUpToEndOfLine(" ]");
451}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000452
453bool Output::preflightFlowElement(unsigned, void *&) {
454 if (NeedFlowSequenceComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000455 output(", ");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000456 if (Column > 70) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000457 output("\n");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000458 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000459 output(" ");
460 Column = ColumnAtFlowStart;
461 output(" ");
462 }
463 return true;
464}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000465
466void Output::postflightFlowElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000467 NeedFlowSequenceComma = true;
468}
469
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000470void Output::beginEnumScalar() {
471 EnumerationMatchFound = false;
472}
473
474bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000475 if (Match && !EnumerationMatchFound) {
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000484 if (!EnumerationMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000485 llvm_unreachable("bad runtime enum value");
486}
487
Nick Kledzik8ceb8b72012-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 Kramerae3ce262012-12-12 22:40:02 +0000497 if (Matches) {
498 if (NeedBitValueComma)
Nick Kledzik8ceb8b72012-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();
Rui Ueyama7eb8b0f2013-09-11 00:45:48 +0000512
513 if (S.empty()) {
514 // Print '' for the empty string because leaving the field empty is not
515 // allowed.
516 this->outputUpToEndOfLine("''");
517 return;
518 }
519 if (!strchr("'`@\"", S.front()) && S.find('\n') == StringRef::npos) {
520 // Plain string cannot start with double quote or single quote. Backquote
521 // and atsign are reserved characters. Newline is not allowed.
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000522 this->outputUpToEndOfLine(S);
523 return;
524 }
525 unsigned i = 0;
526 unsigned j = 0;
527 unsigned End = S.size();
528 output("'"); // Starting single quote.
529 const char *Base = S.data();
530 while (j < End) {
531 // Escape a single quote by doubling it.
532 if (S[j] == '\'') {
533 output(StringRef(&Base[i], j - i + 1));
534 output("'");
535 i = j + 1;
536 }
537 ++j;
538 }
539 output(StringRef(&Base[i], j - i));
540 this->outputUpToEndOfLine("'"); // Ending single quote.
541}
542
543void Output::setError(const Twine &message) {
544}
545
Aaron Ballmand5f33aa2013-08-15 23:17:53 +0000546bool Output::canElideEmptySequence() {
547 // Normally, with an optional key/value where the value is an empty sequence,
548 // the whole key/value can be not written. But, that produces wrong yaml
549 // if the key/value is the only thing in the map and the map is used in
550 // a sequence. This detects if the this sequence is the first key/value
551 // in map that itself is embedded in a sequnce.
Rui Ueyamae9cf2832013-09-11 00:53:07 +0000552 if (StateStack.size() < 2)
Aaron Ballmand5f33aa2013-08-15 23:17:53 +0000553 return true;
Rui Ueyamae9cf2832013-09-11 00:53:07 +0000554 if (StateStack.back() != inMapFirstKey)
Aaron Ballmand5f33aa2013-08-15 23:17:53 +0000555 return true;
556 return (StateStack[StateStack.size()-2] != inSeq);
557}
558
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000559void Output::output(StringRef s) {
560 Column += s.size();
561 Out << s;
562}
563
564void Output::outputUpToEndOfLine(StringRef s) {
565 this->output(s);
Richard Smith2b45dd52012-12-22 00:15:13 +0000566 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000567 NeedsNewLine = true;
568}
569
570void Output::outputNewLine() {
571 Out << "\n";
572 Column = 0;
573}
574
575// if seq at top, indent as if map, then add "- "
576// if seq in middle, use "- " if firstKey, else use " "
577//
578
579void Output::newLineCheck() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000580 if (!NeedsNewLine)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000581 return;
582 NeedsNewLine = false;
583
584 this->outputNewLine();
585
586 assert(StateStack.size() > 0);
587 unsigned Indent = StateStack.size() - 1;
588 bool OutputDash = false;
589
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000590 if (StateStack.back() == inSeq) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000591 OutputDash = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000592 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
593 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000594 --Indent;
595 OutputDash = true;
596 }
597
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000598 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000599 output(" ");
600 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000601 if (OutputDash) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000602 output("- ");
603 }
604
605}
606
607void Output::paddedKey(StringRef key) {
608 output(key);
609 output(":");
610 const char *spaces = " ";
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000611 if (key.size() < strlen(spaces))
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000612 output(&spaces[key.size()]);
613 else
614 output(" ");
615}
616
617//===----------------------------------------------------------------------===//
618// traits for built-in types
619//===----------------------------------------------------------------------===//
620
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000621void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
622 Out << (Val ? "true" : "false");
623}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000624
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000625StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
626 if (Scalar.equals("true")) {
627 Val = true;
628 return StringRef();
629 } else if (Scalar.equals("false")) {
630 Val = false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000631 return StringRef();
632 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000633 return "invalid boolean";
634}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000635
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000636void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
637 raw_ostream &Out) {
638 Out << Val;
639}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000640
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000641StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
642 StringRef &Val) {
643 Val = Scalar;
644 return StringRef();
645}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000646
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000647void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
648 raw_ostream &Out) {
649 // use temp uin32_t because ostream thinks uint8_t is a character
650 uint32_t Num = Val;
651 Out << Num;
652}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000653
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000654StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
655 unsigned long long n;
656 if (getAsUnsignedInteger(Scalar, 0, n))
657 return "invalid number";
658 if (n > 0xFF)
659 return "out of range number";
660 Val = n;
661 return StringRef();
662}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000663
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000664void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
665 raw_ostream &Out) {
666 Out << Val;
667}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000668
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000669StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
670 uint16_t &Val) {
671 unsigned long long n;
672 if (getAsUnsignedInteger(Scalar, 0, n))
673 return "invalid number";
674 if (n > 0xFFFF)
675 return "out of range number";
676 Val = n;
677 return StringRef();
678}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000679
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000680void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
681 raw_ostream &Out) {
682 Out << Val;
683}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000684
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000685StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
686 uint32_t &Val) {
687 unsigned long long n;
688 if (getAsUnsignedInteger(Scalar, 0, n))
689 return "invalid number";
690 if (n > 0xFFFFFFFFUL)
691 return "out of range number";
692 Val = n;
693 return StringRef();
694}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000695
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000696void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
697 raw_ostream &Out) {
698 Out << Val;
699}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000700
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000701StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
702 uint64_t &Val) {
703 unsigned long long N;
704 if (getAsUnsignedInteger(Scalar, 0, N))
705 return "invalid number";
706 Val = N;
707 return StringRef();
708}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000709
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000710void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
711 // use temp in32_t because ostream thinks int8_t is a character
712 int32_t Num = Val;
713 Out << Num;
714}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000715
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000716StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
717 long long N;
718 if (getAsSignedInteger(Scalar, 0, N))
719 return "invalid number";
720 if ((N > 127) || (N < -128))
721 return "out of range number";
722 Val = N;
723 return StringRef();
724}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000725
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000726void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
727 raw_ostream &Out) {
728 Out << Val;
729}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000730
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000731StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
732 long long N;
733 if (getAsSignedInteger(Scalar, 0, N))
734 return "invalid number";
735 if ((N > INT16_MAX) || (N < INT16_MIN))
736 return "out of range number";
737 Val = N;
738 return StringRef();
739}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000740
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000741void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
742 raw_ostream &Out) {
743 Out << Val;
744}
745
746StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
747 long long N;
748 if (getAsSignedInteger(Scalar, 0, N))
749 return "invalid number";
750 if ((N > INT32_MAX) || (N < INT32_MIN))
751 return "out of range number";
752 Val = N;
753 return StringRef();
754}
755
756void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
757 raw_ostream &Out) {
758 Out << Val;
759}
760
761StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
762 long long N;
763 if (getAsSignedInteger(Scalar, 0, N))
764 return "invalid number";
765 Val = N;
766 return StringRef();
767}
768
769void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000770 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000771}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000772
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000773StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
774 SmallString<32> buff(Scalar.begin(), Scalar.end());
775 char *end;
776 Val = strtod(buff.c_str(), &end);
777 if (*end != '\0')
778 return "invalid floating point number";
779 return StringRef();
780}
781
782void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000783 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000784}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000785
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000786StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
787 SmallString<32> buff(Scalar.begin(), Scalar.end());
788 char *end;
789 Val = strtod(buff.c_str(), &end);
790 if (*end != '\0')
791 return "invalid floating point number";
792 return StringRef();
793}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000794
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000795void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
796 uint8_t Num = Val;
797 Out << format("0x%02X", Num);
798}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000799
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000800StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
801 unsigned long long n;
802 if (getAsUnsignedInteger(Scalar, 0, n))
803 return "invalid hex8 number";
804 if (n > 0xFF)
805 return "out of range hex8 number";
806 Val = n;
807 return StringRef();
808}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000809
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000810void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
811 uint16_t Num = Val;
812 Out << format("0x%04X", Num);
813}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000814
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000815StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
816 unsigned long long n;
817 if (getAsUnsignedInteger(Scalar, 0, n))
818 return "invalid hex16 number";
819 if (n > 0xFFFF)
820 return "out of range hex16 number";
821 Val = n;
822 return StringRef();
823}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000824
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000825void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
826 uint32_t Num = Val;
827 Out << format("0x%08X", Num);
828}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000829
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000830StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
831 unsigned long long n;
832 if (getAsUnsignedInteger(Scalar, 0, n))
833 return "invalid hex32 number";
834 if (n > 0xFFFFFFFFUL)
835 return "out of range hex32 number";
836 Val = n;
837 return StringRef();
838}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000839
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000840void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
841 uint64_t Num = Val;
842 Out << format("0x%016llX", Num);
843}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000844
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000845StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
846 unsigned long long Num;
847 if (getAsUnsignedInteger(Scalar, 0, Num))
848 return "invalid hex64 number";
849 Val = Num;
850 return StringRef();
851}