blob: 9da2aa7c841d6e7d32fce1ccf1990a7de886eb72 [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) {
130 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) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000325 for (SmallVector<const char *, 6>::iterator i = ValidKeys.begin(),
326 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
Nick Kledzik02fa3832013-01-08 21:04:44 +0000337Input::MapHNode::~MapHNode() {
338 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
339 i != End; ++i) {
340 delete i->second;
341 }
342}
343
344Input::SequenceHNode::~SequenceHNode() {
345 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
346 i != End; ++i) {
347 delete *i;
348 }
349}
350
351
352
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000353//===----------------------------------------------------------------------===//
354// Output
355//===----------------------------------------------------------------------===//
356
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000357Output::Output(raw_ostream &yout, void *context)
358 : IO(context),
359 Out(yout),
360 Column(0),
361 ColumnAtFlowStart(0),
362 NeedBitValueComma(false),
363 NeedFlowSequenceComma(false),
364 EnumerationMatchFound(false),
365 NeedsNewLine(false) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000366}
367
368Output::~Output() {
369}
370
371bool Output::outputting() {
372 return true;
373}
374
375void Output::beginMapping() {
376 StateStack.push_back(inMapFirstKey);
377 NeedsNewLine = true;
378}
379
380void Output::endMapping() {
381 StateStack.pop_back();
382}
383
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000384bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000385 bool &UseDefault, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000386 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000387 if (Required || !SameAsDefault) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000388 this->newLineCheck();
389 this->paddedKey(Key);
390 return true;
391 }
392 return false;
393}
394
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000395void Output::postflightKey(void *) {
396 if (StateStack.back() == inMapFirstKey) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000397 StateStack.pop_back();
398 StateStack.push_back(inMapOtherKey);
399 }
400}
401
402void Output::beginDocuments() {
403 this->outputUpToEndOfLine("---");
404}
405
406bool Output::preflightDocument(unsigned index) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000407 if (index > 0)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000408 this->outputUpToEndOfLine("\n---");
409 return true;
410}
411
412void Output::postflightDocument() {
413}
414
415void Output::endDocuments() {
416 output("\n...\n");
417}
418
419unsigned Output::beginSequence() {
420 StateStack.push_back(inSeq);
421 NeedsNewLine = true;
422 return 0;
423}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000424
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000425void Output::endSequence() {
426 StateStack.pop_back();
427}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000428
429bool Output::preflightElement(unsigned, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000430 return true;
431}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000432
433void Output::postflightElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000434}
435
436unsigned Output::beginFlowSequence() {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000437 StateStack.push_back(inFlowSeq);
Nick Kledzik50c30422013-01-04 19:32:00 +0000438 this->newLineCheck();
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000439 ColumnAtFlowStart = Column;
440 output("[ ");
441 NeedFlowSequenceComma = false;
442 return 0;
443}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000444
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000445void Output::endFlowSequence() {
446 StateStack.pop_back();
447 this->outputUpToEndOfLine(" ]");
448}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000449
450bool Output::preflightFlowElement(unsigned, void *&) {
451 if (NeedFlowSequenceComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000452 output(", ");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000453 if (Column > 70) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000454 output("\n");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000455 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000456 output(" ");
457 Column = ColumnAtFlowStart;
458 output(" ");
459 }
460 return true;
461}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000462
463void Output::postflightFlowElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000464 NeedFlowSequenceComma = true;
465}
466
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000467void Output::beginEnumScalar() {
468 EnumerationMatchFound = false;
469}
470
471bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000472 if (Match && !EnumerationMatchFound) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000473 this->newLineCheck();
474 this->outputUpToEndOfLine(Str);
475 EnumerationMatchFound = true;
476 }
477 return false;
478}
479
480void Output::endEnumScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000481 if (!EnumerationMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000482 llvm_unreachable("bad runtime enum value");
483}
484
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000485bool Output::beginBitSetScalar(bool &DoClear) {
486 this->newLineCheck();
487 output("[ ");
488 NeedBitValueComma = false;
489 DoClear = false;
490 return true;
491}
492
493bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000494 if (Matches) {
495 if (NeedBitValueComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000496 output(", ");
497 this->output(Str);
498 NeedBitValueComma = true;
499 }
500 return false;
501}
502
503void Output::endBitSetScalar() {
504 this->outputUpToEndOfLine(" ]");
505}
506
507void Output::scalarString(StringRef &S) {
508 this->newLineCheck();
509 if (S.find('\n') == StringRef::npos) {
510 // No embedded new-line chars, just print string.
511 this->outputUpToEndOfLine(S);
512 return;
513 }
514 unsigned i = 0;
515 unsigned j = 0;
516 unsigned End = S.size();
517 output("'"); // Starting single quote.
518 const char *Base = S.data();
519 while (j < End) {
520 // Escape a single quote by doubling it.
521 if (S[j] == '\'') {
522 output(StringRef(&Base[i], j - i + 1));
523 output("'");
524 i = j + 1;
525 }
526 ++j;
527 }
528 output(StringRef(&Base[i], j - i));
529 this->outputUpToEndOfLine("'"); // Ending single quote.
530}
531
532void Output::setError(const Twine &message) {
533}
534
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000535void Output::output(StringRef s) {
536 Column += s.size();
537 Out << s;
538}
539
540void Output::outputUpToEndOfLine(StringRef s) {
541 this->output(s);
Richard Smith2b45dd52012-12-22 00:15:13 +0000542 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000543 NeedsNewLine = true;
544}
545
546void Output::outputNewLine() {
547 Out << "\n";
548 Column = 0;
549}
550
551// if seq at top, indent as if map, then add "- "
552// if seq in middle, use "- " if firstKey, else use " "
553//
554
555void Output::newLineCheck() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000556 if (!NeedsNewLine)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000557 return;
558 NeedsNewLine = false;
559
560 this->outputNewLine();
561
562 assert(StateStack.size() > 0);
563 unsigned Indent = StateStack.size() - 1;
564 bool OutputDash = false;
565
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000566 if (StateStack.back() == inSeq) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000567 OutputDash = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000568 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
569 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000570 --Indent;
571 OutputDash = true;
572 }
573
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000574 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000575 output(" ");
576 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000577 if (OutputDash) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000578 output("- ");
579 }
580
581}
582
583void Output::paddedKey(StringRef key) {
584 output(key);
585 output(":");
586 const char *spaces = " ";
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000587 if (key.size() < strlen(spaces))
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000588 output(&spaces[key.size()]);
589 else
590 output(" ");
591}
592
593//===----------------------------------------------------------------------===//
594// traits for built-in types
595//===----------------------------------------------------------------------===//
596
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000597void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
598 Out << (Val ? "true" : "false");
599}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000600
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000601StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
602 if (Scalar.equals("true")) {
603 Val = true;
604 return StringRef();
605 } else if (Scalar.equals("false")) {
606 Val = false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000607 return StringRef();
608 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000609 return "invalid boolean";
610}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000611
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000612void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
613 raw_ostream &Out) {
614 Out << Val;
615}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000616
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000617StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
618 StringRef &Val) {
619 Val = Scalar;
620 return StringRef();
621}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000622
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000623void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
624 raw_ostream &Out) {
625 // use temp uin32_t because ostream thinks uint8_t is a character
626 uint32_t Num = Val;
627 Out << Num;
628}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000629
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000630StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
631 unsigned long long n;
632 if (getAsUnsignedInteger(Scalar, 0, n))
633 return "invalid number";
634 if (n > 0xFF)
635 return "out of range number";
636 Val = n;
637 return StringRef();
638}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000639
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000640void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
641 raw_ostream &Out) {
642 Out << Val;
643}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000644
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000645StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
646 uint16_t &Val) {
647 unsigned long long n;
648 if (getAsUnsignedInteger(Scalar, 0, n))
649 return "invalid number";
650 if (n > 0xFFFF)
651 return "out of range number";
652 Val = n;
653 return StringRef();
654}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000655
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000656void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
657 raw_ostream &Out) {
658 Out << Val;
659}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000660
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000661StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
662 uint32_t &Val) {
663 unsigned long long n;
664 if (getAsUnsignedInteger(Scalar, 0, n))
665 return "invalid number";
666 if (n > 0xFFFFFFFFUL)
667 return "out of range number";
668 Val = n;
669 return StringRef();
670}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000671
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000672void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
673 raw_ostream &Out) {
674 Out << Val;
675}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000676
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000677StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
678 uint64_t &Val) {
679 unsigned long long N;
680 if (getAsUnsignedInteger(Scalar, 0, N))
681 return "invalid number";
682 Val = N;
683 return StringRef();
684}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000685
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000686void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
687 // use temp in32_t because ostream thinks int8_t is a character
688 int32_t Num = Val;
689 Out << Num;
690}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000691
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000692StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
693 long long N;
694 if (getAsSignedInteger(Scalar, 0, N))
695 return "invalid number";
696 if ((N > 127) || (N < -128))
697 return "out of range number";
698 Val = N;
699 return StringRef();
700}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000701
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000702void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
703 raw_ostream &Out) {
704 Out << Val;
705}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000706
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000707StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
708 long long N;
709 if (getAsSignedInteger(Scalar, 0, N))
710 return "invalid number";
711 if ((N > INT16_MAX) || (N < INT16_MIN))
712 return "out of range number";
713 Val = N;
714 return StringRef();
715}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000716
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000717void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
718 raw_ostream &Out) {
719 Out << Val;
720}
721
722StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
723 long long N;
724 if (getAsSignedInteger(Scalar, 0, N))
725 return "invalid number";
726 if ((N > INT32_MAX) || (N < INT32_MIN))
727 return "out of range number";
728 Val = N;
729 return StringRef();
730}
731
732void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
733 raw_ostream &Out) {
734 Out << Val;
735}
736
737StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
738 long long N;
739 if (getAsSignedInteger(Scalar, 0, N))
740 return "invalid number";
741 Val = N;
742 return StringRef();
743}
744
745void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000746 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000747}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000748
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000749StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
750 SmallString<32> buff(Scalar.begin(), Scalar.end());
751 char *end;
752 Val = strtod(buff.c_str(), &end);
753 if (*end != '\0')
754 return "invalid floating point number";
755 return StringRef();
756}
757
758void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000759 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000760}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000761
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000762StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
763 SmallString<32> buff(Scalar.begin(), Scalar.end());
764 char *end;
765 Val = strtod(buff.c_str(), &end);
766 if (*end != '\0')
767 return "invalid floating point number";
768 return StringRef();
769}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000770
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000771void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
772 uint8_t Num = Val;
773 Out << format("0x%02X", Num);
774}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000775
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000776StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
777 unsigned long long n;
778 if (getAsUnsignedInteger(Scalar, 0, n))
779 return "invalid hex8 number";
780 if (n > 0xFF)
781 return "out of range hex8 number";
782 Val = n;
783 return StringRef();
784}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000785
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000786void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
787 uint16_t Num = Val;
788 Out << format("0x%04X", Num);
789}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000790
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000791StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
792 unsigned long long n;
793 if (getAsUnsignedInteger(Scalar, 0, n))
794 return "invalid hex16 number";
795 if (n > 0xFFFF)
796 return "out of range hex16 number";
797 Val = n;
798 return StringRef();
799}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000800
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000801void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
802 uint32_t Num = Val;
803 Out << format("0x%08X", Num);
804}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000805
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000806StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
807 unsigned long long n;
808 if (getAsUnsignedInteger(Scalar, 0, n))
809 return "invalid hex32 number";
810 if (n > 0xFFFFFFFFUL)
811 return "out of range hex32 number";
812 Val = n;
813 return StringRef();
814}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000815
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000816void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
817 uint64_t Num = Val;
818 Out << format("0x%016llX", Num);
819}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000820
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000821StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
822 unsigned long long Num;
823 if (getAsUnsignedInteger(Scalar, 0, Num))
824 return "invalid hex64 number";
825 Val = Num;
826 return StringRef();
827}