blob: ae7f7dcb0d0555d0aee934c1f10617464e8e077b [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
Nick Kledzik02fa3832013-01-08 21:04:44 +000043Input::Input(StringRef InputContent, void *Ctxt)
44 : IO(Ctxt),
45 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() {
51
52}
53
Benjamin Kramerae3ce262012-12-12 22:40:02 +000054error_code Input::error() {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000055 return EC;
56}
57
Benjamin Kramerae3ce262012-12-12 22:40:02 +000058void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000059 SrcMgr.setDiagHandler(Handler, Ctxt);
60}
61
62bool Input::outputting() {
63 return false;
64}
65
66bool Input::setCurrentDocument() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +000067 if (DocIterator != Strm->end()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000068 Node *N = DocIterator->getRoot();
Benjamin Kramerae3ce262012-12-12 22:40:02 +000069 if (isa<NullNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000070 // Empty files are allowed and ignored
71 ++DocIterator;
72 return setCurrentDocument();
73 }
Nick Kledzik02fa3832013-01-08 21:04:44 +000074 TopNode.reset(this->createHNodes(N));
75 CurrentNode = TopNode.get();
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000076 return true;
77 }
78 return false;
79}
80
81void Input::nextDocument() {
82 ++DocIterator;
83}
84
85void Input::beginMapping() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +000086 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000087 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000088 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
89 if (MN) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000090 MN->ValidKeys.clear();
91 }
92}
93
Benjamin Kramerae3ce262012-12-12 22:40:02 +000094bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
95 void *&SaveInfo) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000096 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000097 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000098 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000099 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
100 if (!MN) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000101 setError(CurrentNode, "not a mapping");
102 return false;
103 }
104 MN->ValidKeys.push_back(Key);
105 HNode *Value = MN->Mapping[Key];
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000106 if (!Value) {
107 if (Required)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000108 setError(CurrentNode, Twine("missing required key '") + Key + "'");
109 else
110 UseDefault = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000111 return false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000112 }
113 SaveInfo = CurrentNode;
114 CurrentNode = Value;
115 return true;
116}
117
118void Input::postflightKey(void *saveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000119 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000120}
121
122void Input::endMapping() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000123 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000124 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000125 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
126 if (!MN)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000127 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000128 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
129 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenko9e8eafa2013-08-07 05:51:27 +0000130 if (!MN->isValidKey(i->first())) {
131 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000132 break;
133 }
134 }
135}
136
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000137unsigned Input::beginSequence() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000138 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000139 return SQ->Entries.size();
140 }
141 return 0;
142}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000143
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000144void Input::endSequence() {
145}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000146
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000147bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000148 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000149 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000150 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000151 SaveInfo = CurrentNode;
152 CurrentNode = SQ->Entries[Index];
153 return true;
154 }
155 return false;
156}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000157
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000158void Input::postflightElement(void *SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000159 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000160}
161
162unsigned Input::beginFlowSequence() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000163 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000164 return SQ->Entries.size();
165 }
166 return 0;
167}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000168
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000169bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000170 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000171 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000172 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000173 SaveInfo = CurrentNode;
174 CurrentNode = SQ->Entries[index];
175 return true;
176 }
177 return false;
178}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000179
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000180void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000181 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000182}
183
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000184void Input::endFlowSequence() {
185}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000186
187void Input::beginEnumScalar() {
188 ScalarMatchFound = false;
189}
190
191bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000192 if (ScalarMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000193 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000194 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
195 if (SN->value().equals(Str)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000196 ScalarMatchFound = true;
197 return true;
198 }
199 }
200 return false;
201}
202
203void Input::endEnumScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000204 if (!ScalarMatchFound) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000205 setError(CurrentNode, "unknown enumerated scalar");
206 }
207}
208
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000209bool Input::beginBitSetScalar(bool &DoClear) {
210 BitValuesUsed.clear();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000211 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000212 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000213 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000214 setError(CurrentNode, "expected sequence of bit values");
215 }
216 DoClear = true;
217 return true;
218}
219
220bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000221 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000222 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000223 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000224 unsigned Index = 0;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000225 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
226 End = SQ->Entries.end(); i != End; ++i) {
227 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
228 if (SN->value().equals(Str)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000229 BitValuesUsed[Index] = true;
230 return true;
231 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000232 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000233 setError(CurrentNode, "unexpected scalar in sequence of bit values");
234 }
235 ++Index;
236 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000237 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000238 setError(CurrentNode, "expected sequence of bit values");
239 }
240 return false;
241}
242
243void Input::endBitSetScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000244 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000245 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000246 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000247 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000248 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
249 if (!BitValuesUsed[i]) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000250 setError(SQ->Entries[i], "unknown bit value");
251 return;
252 }
253 }
254 }
255}
256
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000257void Input::scalarString(StringRef &S) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000258 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000259 S = SN->value();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000260 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000261 setError(CurrentNode, "unexpected scalar");
262 }
263}
264
265void Input::setError(HNode *hnode, const Twine &message) {
266 this->setError(hnode->_node, message);
267}
268
269void Input::setError(Node *node, const Twine &message) {
270 Strm->printError(node, message);
271 EC = make_error_code(errc::invalid_argument);
272}
273
274Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000275 SmallString<128> StringStorage;
276 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000277 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000278 if (!StringStorage.empty()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000279 // Copy string to permanent storage
280 unsigned Len = StringStorage.size();
Nick Kledzik02fa3832013-01-08 21:04:44 +0000281 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000282 memcpy(Buf, &StringStorage[0], Len);
283 KeyStr = StringRef(Buf, Len);
284 }
Nick Kledzik02fa3832013-01-08 21:04:44 +0000285 return new ScalarHNode(N, KeyStr);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000286 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik02fa3832013-01-08 21:04:44 +0000287 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000288 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
289 ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000290 HNode *Entry = this->createHNodes(i);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000291 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000292 break;
293 SQHNode->Entries.push_back(Entry);
294 }
295 return SQHNode;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000296 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik02fa3832013-01-08 21:04:44 +0000297 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000298 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
299 ++i) {
300 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000301 StringStorage.clear();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000302 StringRef KeyStr = KeyScalar->getValue(StringStorage);
303 if (!StringStorage.empty()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000304 // Copy string to permanent storage
305 unsigned Len = StringStorage.size();
Nick Kledzik02fa3832013-01-08 21:04:44 +0000306 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000307 memcpy(Buf, &StringStorage[0], Len);
308 KeyStr = StringRef(Buf, Len);
309 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000310 HNode *ValueHNode = this->createHNodes(i->getValue());
311 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000312 break;
313 mapHNode->Mapping[KeyStr] = ValueHNode;
314 }
315 return mapHNode;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000316 } else if (isa<NullNode>(N)) {
Nick Kledzik02fa3832013-01-08 21:04:44 +0000317 return new EmptyHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000318 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000319 setError(N, "unknown node kind");
320 return NULL;
321 }
322}
323
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000324bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000325 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000326 End = ValidKeys.end(); i != End; ++i) {
327 if (Key.equals(*i))
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000328 return true;
329 }
330 return false;
331}
332
333void Input::setError(const Twine &Message) {
334 this->setError(CurrentNode, Message);
335}
336
Aaron Ballmand5f33aa2013-08-15 23:17:53 +0000337bool Input::canElideEmptySequence() {
338 return false;
339}
340
Nick Kledzik02fa3832013-01-08 21:04:44 +0000341Input::MapHNode::~MapHNode() {
342 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
343 i != End; ++i) {
344 delete i->second;
345 }
346}
347
348Input::SequenceHNode::~SequenceHNode() {
349 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
350 i != End; ++i) {
351 delete *i;
352 }
353}
354
355
356
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000357//===----------------------------------------------------------------------===//
358// Output
359//===----------------------------------------------------------------------===//
360
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000361Output::Output(raw_ostream &yout, void *context)
362 : IO(context),
363 Out(yout),
364 Column(0),
365 ColumnAtFlowStart(0),
366 NeedBitValueComma(false),
367 NeedFlowSequenceComma(false),
368 EnumerationMatchFound(false),
369 NeedsNewLine(false) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000370}
371
372Output::~Output() {
373}
374
375bool Output::outputting() {
376 return true;
377}
378
379void Output::beginMapping() {
380 StateStack.push_back(inMapFirstKey);
381 NeedsNewLine = true;
382}
383
384void Output::endMapping() {
385 StateStack.pop_back();
386}
387
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000388bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000389 bool &UseDefault, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000390 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000391 if (Required || !SameAsDefault) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000392 this->newLineCheck();
393 this->paddedKey(Key);
394 return true;
395 }
396 return false;
397}
398
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000399void Output::postflightKey(void *) {
400 if (StateStack.back() == inMapFirstKey) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000401 StateStack.pop_back();
402 StateStack.push_back(inMapOtherKey);
403 }
404}
405
406void Output::beginDocuments() {
407 this->outputUpToEndOfLine("---");
408}
409
410bool Output::preflightDocument(unsigned index) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000411 if (index > 0)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000412 this->outputUpToEndOfLine("\n---");
413 return true;
414}
415
416void Output::postflightDocument() {
417}
418
419void Output::endDocuments() {
420 output("\n...\n");
421}
422
423unsigned Output::beginSequence() {
424 StateStack.push_back(inSeq);
425 NeedsNewLine = true;
426 return 0;
427}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000428
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000429void Output::endSequence() {
430 StateStack.pop_back();
431}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000432
433bool Output::preflightElement(unsigned, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000434 return true;
435}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000436
437void Output::postflightElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000438}
439
440unsigned Output::beginFlowSequence() {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000441 StateStack.push_back(inFlowSeq);
Nick Kledzik50c30422013-01-04 19:32:00 +0000442 this->newLineCheck();
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000443 ColumnAtFlowStart = Column;
444 output("[ ");
445 NeedFlowSequenceComma = false;
446 return 0;
447}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000448
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000449void Output::endFlowSequence() {
450 StateStack.pop_back();
451 this->outputUpToEndOfLine(" ]");
452}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000453
454bool Output::preflightFlowElement(unsigned, void *&) {
455 if (NeedFlowSequenceComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000456 output(", ");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000457 if (Column > 70) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000458 output("\n");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000459 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000460 output(" ");
461 Column = ColumnAtFlowStart;
462 output(" ");
463 }
464 return true;
465}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000466
467void Output::postflightFlowElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000468 NeedFlowSequenceComma = true;
469}
470
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000471void Output::beginEnumScalar() {
472 EnumerationMatchFound = false;
473}
474
475bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000476 if (Match && !EnumerationMatchFound) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000477 this->newLineCheck();
478 this->outputUpToEndOfLine(Str);
479 EnumerationMatchFound = true;
480 }
481 return false;
482}
483
484void Output::endEnumScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000485 if (!EnumerationMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000486 llvm_unreachable("bad runtime enum value");
487}
488
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000489bool Output::beginBitSetScalar(bool &DoClear) {
490 this->newLineCheck();
491 output("[ ");
492 NeedBitValueComma = false;
493 DoClear = false;
494 return true;
495}
496
497bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000498 if (Matches) {
499 if (NeedBitValueComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000500 output(", ");
501 this->output(Str);
502 NeedBitValueComma = true;
503 }
504 return false;
505}
506
507void Output::endBitSetScalar() {
508 this->outputUpToEndOfLine(" ]");
509}
510
511void Output::scalarString(StringRef &S) {
512 this->newLineCheck();
513 if (S.find('\n') == StringRef::npos) {
514 // No embedded new-line chars, just print string.
515 this->outputUpToEndOfLine(S);
516 return;
517 }
518 unsigned i = 0;
519 unsigned j = 0;
520 unsigned End = S.size();
521 output("'"); // Starting single quote.
522 const char *Base = S.data();
523 while (j < End) {
524 // Escape a single quote by doubling it.
525 if (S[j] == '\'') {
526 output(StringRef(&Base[i], j - i + 1));
527 output("'");
528 i = j + 1;
529 }
530 ++j;
531 }
532 output(StringRef(&Base[i], j - i));
533 this->outputUpToEndOfLine("'"); // Ending single quote.
534}
535
536void Output::setError(const Twine &message) {
537}
538
Aaron Ballmand5f33aa2013-08-15 23:17:53 +0000539bool Output::canElideEmptySequence() {
540 // Normally, with an optional key/value where the value is an empty sequence,
541 // the whole key/value can be not written. But, that produces wrong yaml
542 // if the key/value is the only thing in the map and the map is used in
543 // a sequence. This detects if the this sequence is the first key/value
544 // in map that itself is embedded in a sequnce.
545 if (StateStack.size() < 2)
546 return true;
547 if (StateStack.back() != inMapFirstKey)
548 return true;
549 return (StateStack[StateStack.size()-2] != inSeq);
550}
551
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000552void Output::output(StringRef s) {
553 Column += s.size();
554 Out << s;
555}
556
557void Output::outputUpToEndOfLine(StringRef s) {
558 this->output(s);
Richard Smith2b45dd52012-12-22 00:15:13 +0000559 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000560 NeedsNewLine = true;
561}
562
563void Output::outputNewLine() {
564 Out << "\n";
565 Column = 0;
566}
567
568// if seq at top, indent as if map, then add "- "
569// if seq in middle, use "- " if firstKey, else use " "
570//
571
572void Output::newLineCheck() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000573 if (!NeedsNewLine)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000574 return;
575 NeedsNewLine = false;
576
577 this->outputNewLine();
578
579 assert(StateStack.size() > 0);
580 unsigned Indent = StateStack.size() - 1;
581 bool OutputDash = false;
582
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000583 if (StateStack.back() == inSeq) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000584 OutputDash = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000585 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
586 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000587 --Indent;
588 OutputDash = true;
589 }
590
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000591 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000592 output(" ");
593 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000594 if (OutputDash) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000595 output("- ");
596 }
597
598}
599
600void Output::paddedKey(StringRef key) {
601 output(key);
602 output(":");
603 const char *spaces = " ";
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000604 if (key.size() < strlen(spaces))
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000605 output(&spaces[key.size()]);
606 else
607 output(" ");
608}
609
610//===----------------------------------------------------------------------===//
611// traits for built-in types
612//===----------------------------------------------------------------------===//
613
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000614void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
615 Out << (Val ? "true" : "false");
616}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000617
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000618StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
619 if (Scalar.equals("true")) {
620 Val = true;
621 return StringRef();
622 } else if (Scalar.equals("false")) {
623 Val = false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000624 return StringRef();
625 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000626 return "invalid boolean";
627}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000628
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000629void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
630 raw_ostream &Out) {
631 Out << Val;
632}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000633
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000634StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
635 StringRef &Val) {
636 Val = Scalar;
637 return StringRef();
638}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000639
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000640void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
641 raw_ostream &Out) {
642 // use temp uin32_t because ostream thinks uint8_t is a character
643 uint32_t Num = Val;
644 Out << Num;
645}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000646
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000647StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
648 unsigned long long n;
649 if (getAsUnsignedInteger(Scalar, 0, n))
650 return "invalid number";
651 if (n > 0xFF)
652 return "out of range number";
653 Val = n;
654 return StringRef();
655}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000656
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000657void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
658 raw_ostream &Out) {
659 Out << Val;
660}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000661
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000662StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
663 uint16_t &Val) {
664 unsigned long long n;
665 if (getAsUnsignedInteger(Scalar, 0, n))
666 return "invalid number";
667 if (n > 0xFFFF)
668 return "out of range number";
669 Val = n;
670 return StringRef();
671}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000672
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000673void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
674 raw_ostream &Out) {
675 Out << Val;
676}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000677
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000678StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
679 uint32_t &Val) {
680 unsigned long long n;
681 if (getAsUnsignedInteger(Scalar, 0, n))
682 return "invalid number";
683 if (n > 0xFFFFFFFFUL)
684 return "out of range number";
685 Val = n;
686 return StringRef();
687}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000688
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000689void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
690 raw_ostream &Out) {
691 Out << Val;
692}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000693
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000694StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
695 uint64_t &Val) {
696 unsigned long long N;
697 if (getAsUnsignedInteger(Scalar, 0, N))
698 return "invalid number";
699 Val = N;
700 return StringRef();
701}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000702
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000703void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
704 // use temp in32_t because ostream thinks int8_t is a character
705 int32_t Num = Val;
706 Out << Num;
707}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000708
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000709StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
710 long long N;
711 if (getAsSignedInteger(Scalar, 0, N))
712 return "invalid number";
713 if ((N > 127) || (N < -128))
714 return "out of range number";
715 Val = N;
716 return StringRef();
717}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000718
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000719void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
720 raw_ostream &Out) {
721 Out << Val;
722}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000723
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000724StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
725 long long N;
726 if (getAsSignedInteger(Scalar, 0, N))
727 return "invalid number";
728 if ((N > INT16_MAX) || (N < INT16_MIN))
729 return "out of range number";
730 Val = N;
731 return StringRef();
732}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000733
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000734void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
735 raw_ostream &Out) {
736 Out << Val;
737}
738
739StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
740 long long N;
741 if (getAsSignedInteger(Scalar, 0, N))
742 return "invalid number";
743 if ((N > INT32_MAX) || (N < INT32_MIN))
744 return "out of range number";
745 Val = N;
746 return StringRef();
747}
748
749void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
750 raw_ostream &Out) {
751 Out << Val;
752}
753
754StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
755 long long N;
756 if (getAsSignedInteger(Scalar, 0, N))
757 return "invalid number";
758 Val = N;
759 return StringRef();
760}
761
762void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000763 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000764}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000765
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000766StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
767 SmallString<32> buff(Scalar.begin(), Scalar.end());
768 char *end;
769 Val = strtod(buff.c_str(), &end);
770 if (*end != '\0')
771 return "invalid floating point number";
772 return StringRef();
773}
774
775void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000776 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000777}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000778
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000779StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
780 SmallString<32> buff(Scalar.begin(), Scalar.end());
781 char *end;
782 Val = strtod(buff.c_str(), &end);
783 if (*end != '\0')
784 return "invalid floating point number";
785 return StringRef();
786}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000787
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000788void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
789 uint8_t Num = Val;
790 Out << format("0x%02X", Num);
791}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000792
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000793StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
794 unsigned long long n;
795 if (getAsUnsignedInteger(Scalar, 0, n))
796 return "invalid hex8 number";
797 if (n > 0xFF)
798 return "out of range hex8 number";
799 Val = n;
800 return StringRef();
801}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000802
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000803void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
804 uint16_t Num = Val;
805 Out << format("0x%04X", Num);
806}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000807
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000808StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
809 unsigned long long n;
810 if (getAsUnsignedInteger(Scalar, 0, n))
811 return "invalid hex16 number";
812 if (n > 0xFFFF)
813 return "out of range hex16 number";
814 Val = n;
815 return StringRef();
816}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000817
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000818void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
819 uint32_t Num = Val;
820 Out << format("0x%08X", Num);
821}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000822
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000823StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
824 unsigned long long n;
825 if (getAsUnsignedInteger(Scalar, 0, n))
826 return "invalid hex32 number";
827 if (n > 0xFFFFFFFFUL)
828 return "out of range hex32 number";
829 Val = n;
830 return StringRef();
831}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000832
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000833void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
834 uint64_t Num = Val;
835 Out << format("0x%016llX", Num);
836}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000837
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000838StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
839 unsigned long long Num;
840 if (getAsUnsignedInteger(Scalar, 0, Num))
841 return "invalid hex64 number";
842 Val = Num;
843 return StringRef();
844}