blob: a31c31915a170eda5e8112190a092c8fc94efe45 [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
Benjamin Kramerae3ce262012-12-12 22:40:02 +000043Input::Input(StringRef InputContent, void *Ctxt) : IO(Ctxt), CurrentNode(NULL) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000044 Strm = new Stream(InputContent, SrcMgr);
45 DocIterator = Strm->begin();
46}
47
Benjamin Kramerae3ce262012-12-12 22:40:02 +000048error_code Input::error() {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000049 return EC;
50}
51
Benjamin Kramerae3ce262012-12-12 22:40:02 +000052void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000053 SrcMgr.setDiagHandler(Handler, Ctxt);
54}
55
56bool Input::outputting() {
57 return false;
58}
59
60bool Input::setCurrentDocument() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +000061 if (DocIterator != Strm->end()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000062 Node *N = DocIterator->getRoot();
Benjamin Kramerae3ce262012-12-12 22:40:02 +000063 if (isa<NullNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000064 // Empty files are allowed and ignored
65 ++DocIterator;
66 return setCurrentDocument();
67 }
68 CurrentNode = this->createHNodes(N);
69 return true;
70 }
71 return false;
72}
73
74void Input::nextDocument() {
75 ++DocIterator;
76}
77
78void Input::beginMapping() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +000079 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000080 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000081 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
82 if (MN) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000083 MN->ValidKeys.clear();
84 }
85}
86
Benjamin Kramerae3ce262012-12-12 22:40:02 +000087bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
88 void *&SaveInfo) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000089 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000090 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000091 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +000092 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
93 if (!MN) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +000094 setError(CurrentNode, "not a mapping");
95 return false;
96 }
97 MN->ValidKeys.push_back(Key);
98 HNode *Value = MN->Mapping[Key];
Benjamin Kramerae3ce262012-12-12 22:40:02 +000099 if (!Value) {
100 if (Required)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000101 setError(CurrentNode, Twine("missing required key '") + Key + "'");
102 else
103 UseDefault = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000104 return false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000105 }
106 SaveInfo = CurrentNode;
107 CurrentNode = Value;
108 return true;
109}
110
111void Input::postflightKey(void *saveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000112 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000113}
114
115void Input::endMapping() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000116 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000117 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000118 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
119 if (!MN)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000120 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000121 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
122 End = MN->Mapping.end(); i != End; ++i) {
123 if (!MN->isValidKey(i->first)) {
124 setError(i->second, Twine("unknown key '") + i->first + "'");
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000125 break;
126 }
127 }
128}
129
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000130unsigned Input::beginSequence() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000131 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000132 return SQ->Entries.size();
133 }
134 return 0;
135}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000136
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000137void Input::endSequence() {
138}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000139
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000140bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000141 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000142 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000143 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000144 SaveInfo = CurrentNode;
145 CurrentNode = SQ->Entries[Index];
146 return true;
147 }
148 return false;
149}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000150
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000151void Input::postflightElement(void *SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000152 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000153}
154
155unsigned Input::beginFlowSequence() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000156 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000157 return SQ->Entries.size();
158 }
159 return 0;
160}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000161
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000162bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000163 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000164 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000165 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000166 SaveInfo = CurrentNode;
167 CurrentNode = SQ->Entries[index];
168 return true;
169 }
170 return false;
171}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000172
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000173void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000174 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000175}
176
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000177void Input::endFlowSequence() {
178}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000179
180void Input::beginEnumScalar() {
181 ScalarMatchFound = false;
182}
183
184bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000185 if (ScalarMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000186 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000187 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
188 if (SN->value().equals(Str)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000189 ScalarMatchFound = true;
190 return true;
191 }
192 }
193 return false;
194}
195
196void Input::endEnumScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000197 if (!ScalarMatchFound) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000198 setError(CurrentNode, "unknown enumerated scalar");
199 }
200}
201
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000202bool Input::beginBitSetScalar(bool &DoClear) {
203 BitValuesUsed.clear();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000204 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000205 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000206 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000207 setError(CurrentNode, "expected sequence of bit values");
208 }
209 DoClear = true;
210 return true;
211}
212
213bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000214 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000215 return false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000216 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000217 unsigned Index = 0;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000218 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
219 End = SQ->Entries.end(); i != End; ++i) {
220 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
221 if (SN->value().equals(Str)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000222 BitValuesUsed[Index] = true;
223 return true;
224 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000225 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000226 setError(CurrentNode, "unexpected scalar in sequence of bit values");
227 }
228 ++Index;
229 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000230 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000231 setError(CurrentNode, "expected sequence of bit values");
232 }
233 return false;
234}
235
236void Input::endBitSetScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000237 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000238 return;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000239 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000240 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000241 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
242 if (!BitValuesUsed[i]) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000243 setError(SQ->Entries[i], "unknown bit value");
244 return;
245 }
246 }
247 }
248}
249
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000250void Input::scalarString(StringRef &S) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000251 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000252 S = SN->value();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000253 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000254 setError(CurrentNode, "unexpected scalar");
255 }
256}
257
258void Input::setError(HNode *hnode, const Twine &message) {
259 this->setError(hnode->_node, message);
260}
261
262void Input::setError(Node *node, const Twine &message) {
263 Strm->printError(node, message);
264 EC = make_error_code(errc::invalid_argument);
265}
266
267Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000268 SmallString<128> StringStorage;
269 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000270 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000271 if (!StringStorage.empty()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000272 // Copy string to permanent storage
273 unsigned Len = StringStorage.size();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000274 char *Buf = Allocator.Allocate<char>(Len);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000275 memcpy(Buf, &StringStorage[0], Len);
276 KeyStr = StringRef(Buf, Len);
277 }
278 return new (Allocator) ScalarHNode(N, KeyStr);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000279 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000280 SequenceHNode *SQHNode = new (Allocator) SequenceHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000281 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
282 ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000283 HNode *Entry = this->createHNodes(i);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000284 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000285 break;
286 SQHNode->Entries.push_back(Entry);
287 }
288 return SQHNode;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000289 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000290 MapHNode *mapHNode = new (Allocator) MapHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000291 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
292 ++i) {
293 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000294 StringStorage.clear();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000295 StringRef KeyStr = KeyScalar->getValue(StringStorage);
296 if (!StringStorage.empty()) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000297 // Copy string to permanent storage
298 unsigned Len = StringStorage.size();
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000299 char *Buf = Allocator.Allocate<char>(Len);
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000300 memcpy(Buf, &StringStorage[0], Len);
301 KeyStr = StringRef(Buf, Len);
302 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000303 HNode *ValueHNode = this->createHNodes(i->getValue());
304 if (EC)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000305 break;
306 mapHNode->Mapping[KeyStr] = ValueHNode;
307 }
308 return mapHNode;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000309 } else if (isa<NullNode>(N)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000310 return new (Allocator) EmptyHNode(N);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000311 } else {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000312 setError(N, "unknown node kind");
313 return NULL;
314 }
315}
316
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000317bool Input::MapHNode::isValidKey(StringRef Key) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000318 for (SmallVector<const char *, 6>::iterator i = ValidKeys.begin(),
319 End = ValidKeys.end(); i != End; ++i) {
320 if (Key.equals(*i))
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000321 return true;
322 }
323 return false;
324}
325
326void Input::setError(const Twine &Message) {
327 this->setError(CurrentNode, Message);
328}
329
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000330//===----------------------------------------------------------------------===//
331// Output
332//===----------------------------------------------------------------------===//
333
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000334Output::Output(raw_ostream &yout, void *context)
335 : IO(context),
336 Out(yout),
337 Column(0),
338 ColumnAtFlowStart(0),
339 NeedBitValueComma(false),
340 NeedFlowSequenceComma(false),
341 EnumerationMatchFound(false),
342 NeedsNewLine(false) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000343}
344
345Output::~Output() {
346}
347
348bool Output::outputting() {
349 return true;
350}
351
352void Output::beginMapping() {
353 StateStack.push_back(inMapFirstKey);
354 NeedsNewLine = true;
355}
356
357void Output::endMapping() {
358 StateStack.pop_back();
359}
360
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000361bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000362 bool &UseDefault, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000363 UseDefault = false;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000364 if (Required || !SameAsDefault) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000365 this->newLineCheck();
366 this->paddedKey(Key);
367 return true;
368 }
369 return false;
370}
371
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000372void Output::postflightKey(void *) {
373 if (StateStack.back() == inMapFirstKey) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000374 StateStack.pop_back();
375 StateStack.push_back(inMapOtherKey);
376 }
377}
378
379void Output::beginDocuments() {
380 this->outputUpToEndOfLine("---");
381}
382
383bool Output::preflightDocument(unsigned index) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000384 if (index > 0)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000385 this->outputUpToEndOfLine("\n---");
386 return true;
387}
388
389void Output::postflightDocument() {
390}
391
392void Output::endDocuments() {
393 output("\n...\n");
394}
395
396unsigned Output::beginSequence() {
397 StateStack.push_back(inSeq);
398 NeedsNewLine = true;
399 return 0;
400}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000401
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000402void Output::endSequence() {
403 StateStack.pop_back();
404}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000405
406bool Output::preflightElement(unsigned, void *&) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000407 return true;
408}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000409
410void Output::postflightElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000411}
412
413unsigned Output::beginFlowSequence() {
414 this->newLineCheck();
415 StateStack.push_back(inFlowSeq);
416 ColumnAtFlowStart = Column;
417 output("[ ");
418 NeedFlowSequenceComma = false;
419 return 0;
420}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000421
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000422void Output::endFlowSequence() {
423 StateStack.pop_back();
424 this->outputUpToEndOfLine(" ]");
425}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000426
427bool Output::preflightFlowElement(unsigned, void *&) {
428 if (NeedFlowSequenceComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000429 output(", ");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000430 if (Column > 70) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000431 output("\n");
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000432 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000433 output(" ");
434 Column = ColumnAtFlowStart;
435 output(" ");
436 }
437 return true;
438}
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000439
440void Output::postflightFlowElement(void *) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000441 NeedFlowSequenceComma = true;
442}
443
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000444void Output::beginEnumScalar() {
445 EnumerationMatchFound = false;
446}
447
448bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000449 if (Match && !EnumerationMatchFound) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000450 this->newLineCheck();
451 this->outputUpToEndOfLine(Str);
452 EnumerationMatchFound = true;
453 }
454 return false;
455}
456
457void Output::endEnumScalar() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000458 if (!EnumerationMatchFound)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000459 llvm_unreachable("bad runtime enum value");
460}
461
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000462bool Output::beginBitSetScalar(bool &DoClear) {
463 this->newLineCheck();
464 output("[ ");
465 NeedBitValueComma = false;
466 DoClear = false;
467 return true;
468}
469
470bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000471 if (Matches) {
472 if (NeedBitValueComma)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000473 output(", ");
474 this->output(Str);
475 NeedBitValueComma = true;
476 }
477 return false;
478}
479
480void Output::endBitSetScalar() {
481 this->outputUpToEndOfLine(" ]");
482}
483
484void Output::scalarString(StringRef &S) {
485 this->newLineCheck();
486 if (S.find('\n') == StringRef::npos) {
487 // No embedded new-line chars, just print string.
488 this->outputUpToEndOfLine(S);
489 return;
490 }
491 unsigned i = 0;
492 unsigned j = 0;
493 unsigned End = S.size();
494 output("'"); // Starting single quote.
495 const char *Base = S.data();
496 while (j < End) {
497 // Escape a single quote by doubling it.
498 if (S[j] == '\'') {
499 output(StringRef(&Base[i], j - i + 1));
500 output("'");
501 i = j + 1;
502 }
503 ++j;
504 }
505 output(StringRef(&Base[i], j - i));
506 this->outputUpToEndOfLine("'"); // Ending single quote.
507}
508
509void Output::setError(const Twine &message) {
510}
511
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000512void Output::output(StringRef s) {
513 Column += s.size();
514 Out << s;
515}
516
517void Output::outputUpToEndOfLine(StringRef s) {
518 this->output(s);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000519 if (StateStack.back() != inFlowSeq)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000520 NeedsNewLine = true;
521}
522
523void Output::outputNewLine() {
524 Out << "\n";
525 Column = 0;
526}
527
528// if seq at top, indent as if map, then add "- "
529// if seq in middle, use "- " if firstKey, else use " "
530//
531
532void Output::newLineCheck() {
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000533 if (!NeedsNewLine)
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000534 return;
535 NeedsNewLine = false;
536
537 this->outputNewLine();
538
539 assert(StateStack.size() > 0);
540 unsigned Indent = StateStack.size() - 1;
541 bool OutputDash = false;
542
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000543 if (StateStack.back() == inSeq) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000544 OutputDash = true;
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000545 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
546 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000547 --Indent;
548 OutputDash = true;
549 }
550
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000551 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000552 output(" ");
553 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000554 if (OutputDash) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000555 output("- ");
556 }
557
558}
559
560void Output::paddedKey(StringRef key) {
561 output(key);
562 output(":");
563 const char *spaces = " ";
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000564 if (key.size() < strlen(spaces))
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000565 output(&spaces[key.size()]);
566 else
567 output(" ");
568}
569
570//===----------------------------------------------------------------------===//
571// traits for built-in types
572//===----------------------------------------------------------------------===//
573
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000574void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
575 Out << (Val ? "true" : "false");
576}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000577
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000578StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
579 if (Scalar.equals("true")) {
580 Val = true;
581 return StringRef();
582 } else if (Scalar.equals("false")) {
583 Val = false;
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000584 return StringRef();
585 }
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000586 return "invalid boolean";
587}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000588
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000589void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
590 raw_ostream &Out) {
591 Out << Val;
592}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000593
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000594StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
595 StringRef &Val) {
596 Val = Scalar;
597 return StringRef();
598}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000599
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000600void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
601 raw_ostream &Out) {
602 // use temp uin32_t because ostream thinks uint8_t is a character
603 uint32_t Num = Val;
604 Out << Num;
605}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000606
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000607StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
608 unsigned long long n;
609 if (getAsUnsignedInteger(Scalar, 0, n))
610 return "invalid number";
611 if (n > 0xFF)
612 return "out of range number";
613 Val = n;
614 return StringRef();
615}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000616
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000617void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
618 raw_ostream &Out) {
619 Out << Val;
620}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000621
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000622StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
623 uint16_t &Val) {
624 unsigned long long n;
625 if (getAsUnsignedInteger(Scalar, 0, n))
626 return "invalid number";
627 if (n > 0xFFFF)
628 return "out of range number";
629 Val = n;
630 return StringRef();
631}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000632
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000633void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
634 raw_ostream &Out) {
635 Out << Val;
636}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000637
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000638StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
639 uint32_t &Val) {
640 unsigned long long n;
641 if (getAsUnsignedInteger(Scalar, 0, n))
642 return "invalid number";
643 if (n > 0xFFFFFFFFUL)
644 return "out of range number";
645 Val = n;
646 return StringRef();
647}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000648
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000649void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
650 raw_ostream &Out) {
651 Out << Val;
652}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000653
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000654StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
655 uint64_t &Val) {
656 unsigned long long N;
657 if (getAsUnsignedInteger(Scalar, 0, N))
658 return "invalid number";
659 Val = N;
660 return StringRef();
661}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000662
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000663void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
664 // use temp in32_t because ostream thinks int8_t is a character
665 int32_t Num = Val;
666 Out << Num;
667}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000668
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000669StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
670 long long N;
671 if (getAsSignedInteger(Scalar, 0, N))
672 return "invalid number";
673 if ((N > 127) || (N < -128))
674 return "out of range number";
675 Val = N;
676 return StringRef();
677}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000678
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000679void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
680 raw_ostream &Out) {
681 Out << Val;
682}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000683
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000684StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
685 long long N;
686 if (getAsSignedInteger(Scalar, 0, N))
687 return "invalid number";
688 if ((N > INT16_MAX) || (N < INT16_MIN))
689 return "out of range number";
690 Val = N;
691 return StringRef();
692}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000693
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000694void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
695 raw_ostream &Out) {
696 Out << Val;
697}
698
699StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
700 long long N;
701 if (getAsSignedInteger(Scalar, 0, N))
702 return "invalid number";
703 if ((N > INT32_MAX) || (N < INT32_MIN))
704 return "out of range number";
705 Val = N;
706 return StringRef();
707}
708
709void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
710 raw_ostream &Out) {
711 Out << Val;
712}
713
714StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
715 long long N;
716 if (getAsSignedInteger(Scalar, 0, N))
717 return "invalid number";
718 Val = N;
719 return StringRef();
720}
721
722void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000723 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000724}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000725
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000726StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
727 SmallString<32> buff(Scalar.begin(), Scalar.end());
728 char *end;
729 Val = strtod(buff.c_str(), &end);
730 if (*end != '\0')
731 return "invalid floating point number";
732 return StringRef();
733}
734
735void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000736 Out << format("%g", Val);
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000737}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000738
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000739StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
740 SmallString<32> buff(Scalar.begin(), Scalar.end());
741 char *end;
742 Val = strtod(buff.c_str(), &end);
743 if (*end != '\0')
744 return "invalid floating point number";
745 return StringRef();
746}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000747
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000748void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
749 uint8_t Num = Val;
750 Out << format("0x%02X", Num);
751}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000752
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000753StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
754 unsigned long long n;
755 if (getAsUnsignedInteger(Scalar, 0, n))
756 return "invalid hex8 number";
757 if (n > 0xFF)
758 return "out of range hex8 number";
759 Val = n;
760 return StringRef();
761}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000762
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000763void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
764 uint16_t Num = Val;
765 Out << format("0x%04X", Num);
766}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000767
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000768StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
769 unsigned long long n;
770 if (getAsUnsignedInteger(Scalar, 0, n))
771 return "invalid hex16 number";
772 if (n > 0xFFFF)
773 return "out of range hex16 number";
774 Val = n;
775 return StringRef();
776}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000777
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000778void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
779 uint32_t Num = Val;
780 Out << format("0x%08X", Num);
781}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000782
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000783StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
784 unsigned long long n;
785 if (getAsUnsignedInteger(Scalar, 0, n))
786 return "invalid hex32 number";
787 if (n > 0xFFFFFFFFUL)
788 return "out of range hex32 number";
789 Val = n;
790 return StringRef();
791}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000792
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000793void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
794 uint64_t Num = Val;
795 Out << format("0x%016llX", Num);
796}
Nick Kledzik8ceb8b72012-12-12 20:46:15 +0000797
Benjamin Kramerae3ce262012-12-12 22:40:02 +0000798StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
799 unsigned long long Num;
800 if (getAsUnsignedInteger(Scalar, 0, Num))
801 return "invalid hex64 number";
802 Val = Num;
803 return StringRef();
804}