blob: 19eaed1ac7aefc79c099b5c845c9625f0d36d49d [file] [log] [blame]
Nick Kledzikf60a9272012-12-12 20:46:15 +00001//===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Nick Kledzikf60a9272012-12-12 20:46:15 +000010#include "llvm/Support/YAMLTraits.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
13#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000014#include "llvm/Support/Format.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000015#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000016#include "llvm/Support/raw_ostream.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000017#include <cstring>
Rui Ueyama106eded2013-09-11 04:00:08 +000018#include <cctype>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000019using namespace llvm;
20using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000021
22//===----------------------------------------------------------------------===//
23// IO
24//===----------------------------------------------------------------------===//
25
26IO::IO(void *Context) : Ctxt(Context) {
27}
28
29IO::~IO() {
30}
31
32void *IO::getContext() {
33 return Ctxt;
34}
35
36void IO::setContext(void *Context) {
37 Ctxt = Context;
38}
39
Nick Kledzikf60a9272012-12-12 20:46:15 +000040//===----------------------------------------------------------------------===//
41// Input
42//===----------------------------------------------------------------------===//
43
Rui Ueyama38dfffa2013-09-11 00:53:07 +000044Input::Input(StringRef InputContent, void *Ctxt)
45 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000046 Strm(new Stream(InputContent, SrcMgr)),
47 CurrentNode(NULL) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000048 DocIterator = Strm->begin();
49}
50
Nick Kledzik0dcef842013-01-08 21:04:44 +000051Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000052}
53
Benjamin Kramer36b0f122012-12-12 22:40:02 +000054error_code Input::error() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000055 return EC;
56}
57
Benjamin Kramer36b0f122012-12-12 22:40:02 +000058void Input::setDiagHandler(SourceMgr::DiagHandlerTy Handler, void *Ctxt) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000059 SrcMgr.setDiagHandler(Handler, Ctxt);
60}
61
62bool Input::outputting() {
63 return false;
64}
65
66bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000067 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000068 Node *N = DocIterator->getRoot();
Benjamin Kramer36b0f122012-12-12 22:40:02 +000069 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000070 // Empty files are allowed and ignored
71 ++DocIterator;
72 return setCurrentDocument();
73 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000074 TopNode.reset(this->createHNodes(N));
75 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +000086 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +000087 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000088 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
89 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000090 MN->ValidKeys.clear();
91 }
92}
93
Benjamin Kramer36b0f122012-12-12 22:40:02 +000094bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
95 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000096 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000097 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +000098 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000099 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
100 if (!MN) {
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000106 if (!Value) {
107 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000108 setError(CurrentNode, Twine("missing required key '") + Key + "'");
109 else
110 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000111 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000112 }
113 SaveInfo = CurrentNode;
114 CurrentNode = Value;
115 return true;
116}
117
118void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000119 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000120}
121
122void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000123 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000124 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000125 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
126 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000127 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000128 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
129 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenkodf73c302013-08-07 05:51:27 +0000130 if (!MN->isValidKey(i->first())) {
131 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000132 break;
133 }
134 }
135}
136
Nick Kledzikf60a9272012-12-12 20:46:15 +0000137unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000138 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000139 return SQ->Entries.size();
140 }
141 return 0;
142}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000143
Nick Kledzikf60a9272012-12-12 20:46:15 +0000144void Input::endSequence() {
145}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000146
Nick Kledzikf60a9272012-12-12 20:46:15 +0000147bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000148 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000149 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000150 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151 SaveInfo = CurrentNode;
152 CurrentNode = SQ->Entries[Index];
153 return true;
154 }
155 return false;
156}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000157
Nick Kledzikf60a9272012-12-12 20:46:15 +0000158void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000159 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000160}
161
162unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000163 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000164 return SQ->Entries.size();
165 }
166 return 0;
167}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000168
Nick Kledzikf60a9272012-12-12 20:46:15 +0000169bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000170 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000171 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000172 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000173 SaveInfo = CurrentNode;
174 CurrentNode = SQ->Entries[index];
175 return true;
176 }
177 return false;
178}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000179
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000181 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182}
183
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000184void Input::endFlowSequence() {
185}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000186
187void Input::beginEnumScalar() {
188 ScalarMatchFound = false;
189}
190
191bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000192 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000193 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
195 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000196 ScalarMatchFound = true;
197 return true;
198 }
199 }
200 return false;
201}
202
203void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000204 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000205 setError(CurrentNode, "unknown enumerated scalar");
206 }
207}
208
Nick Kledzikf60a9272012-12-12 20:46:15 +0000209bool Input::beginBitSetScalar(bool &DoClear) {
210 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000211 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000212 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000213 } else {
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000221 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000222 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000223 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 unsigned Index = 0;
Benjamin Kramer36b0f122012-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 Kledzikf60a9272012-12-12 20:46:15 +0000229 BitValuesUsed[Index] = true;
230 return true;
231 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000232 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000233 setError(CurrentNode, "unexpected scalar in sequence of bit values");
234 }
235 ++Index;
236 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000237 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000238 setError(CurrentNode, "expected sequence of bit values");
239 }
240 return false;
241}
242
243void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000244 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000246 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000247 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000248 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
249 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000250 setError(SQ->Entries[i], "unknown bit value");
251 return;
252 }
253 }
254 }
255}
256
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257void Input::scalarString(StringRef &S) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000258 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000259 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000260 } else {
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000275 SmallString<128> StringStorage;
276 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000277 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000278 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000279 // Copy string to permanent storage
280 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000281 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000282 memcpy(Buf, &StringStorage[0], Len);
283 KeyStr = StringRef(Buf, Len);
284 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000285 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000286 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000287 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
289 ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290 HNode *Entry = this->createHNodes(i);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000291 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000292 break;
293 SQHNode->Entries.push_back(Entry);
294 }
295 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000296 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000297 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramer36b0f122012-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 Kledzikf60a9272012-12-12 20:46:15 +0000301 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000302 StringRef KeyStr = KeyScalar->getValue(StringStorage);
303 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000304 // Copy string to permanent storage
305 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000306 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000307 memcpy(Buf, &StringStorage[0], Len);
308 KeyStr = StringRef(Buf, Len);
309 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000310 HNode *ValueHNode = this->createHNodes(i->getValue());
311 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000312 break;
313 mapHNode->Mapping[KeyStr] = ValueHNode;
314 }
315 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000316 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000317 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000318 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000319 setError(N, "unknown node kind");
320 return NULL;
321 }
322}
323
Nick Kledzikf60a9272012-12-12 20:46:15 +0000324bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000325 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000326 End = ValidKeys.end(); i != End; ++i) {
327 if (Key.equals(*i))
Nick Kledzikf60a9272012-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 Ballman0e63e532013-08-15 23:17:53 +0000337bool Input::canElideEmptySequence() {
338 return false;
339}
340
Nick Kledzik0dcef842013-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 Kledzikf60a9272012-12-12 20:46:15 +0000357//===----------------------------------------------------------------------===//
358// Output
359//===----------------------------------------------------------------------===//
360
Benjamin Kramer36b0f122012-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 Kledzikf60a9272012-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 Kledzikf60a9272012-12-12 20:46:15 +0000388bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000389 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000390 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000391 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000392 this->newLineCheck();
393 this->paddedKey(Key);
394 return true;
395 }
396 return false;
397}
398
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000399void Output::postflightKey(void *) {
400 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000411 if (index > 0)
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000428
Nick Kledzikf60a9272012-12-12 20:46:15 +0000429void Output::endSequence() {
430 StateStack.pop_back();
431}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000432
433bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000434 return true;
435}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000436
437void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000438}
439
440unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000441 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000442 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000443 ColumnAtFlowStart = Column;
444 output("[ ");
445 NeedFlowSequenceComma = false;
446 return 0;
447}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000448
Nick Kledzikf60a9272012-12-12 20:46:15 +0000449void Output::endFlowSequence() {
450 StateStack.pop_back();
451 this->outputUpToEndOfLine(" ]");
452}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000453
454bool Output::preflightFlowElement(unsigned, void *&) {
455 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000456 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000457 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000458 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000459 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000460 output(" ");
461 Column = ColumnAtFlowStart;
462 output(" ");
463 }
464 return true;
465}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000466
467void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000468 NeedFlowSequenceComma = true;
469}
470
Nick Kledzikf60a9272012-12-12 20:46:15 +0000471void Output::beginEnumScalar() {
472 EnumerationMatchFound = false;
473}
474
475bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000476 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000485 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000486 llvm_unreachable("bad runtime enum value");
487}
488
Nick Kledzikf60a9272012-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 Kramer36b0f122012-12-12 22:40:02 +0000498 if (Matches) {
499 if (NeedBitValueComma)
Nick Kledzikf60a9272012-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) {
Rui Ueyama106eded2013-09-11 04:00:08 +0000512 const char ScalarSafeChars[] = "abcdefghijklmnopqrstuvwxyz"
513 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
514
Nick Kledzikf60a9272012-12-12 20:46:15 +0000515 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000516 if (S.empty()) {
517 // Print '' for the empty string because leaving the field empty is not
518 // allowed.
519 this->outputUpToEndOfLine("''");
520 return;
521 }
522 if (S.find_first_not_of(ScalarSafeChars) == StringRef::npos &&
523 !isspace(S.front()) && !isspace(S.back())) {
524 // If the string consists only of safe characters, print it out without
525 // quotes.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000526 this->outputUpToEndOfLine(S);
527 return;
528 }
529 unsigned i = 0;
530 unsigned j = 0;
531 unsigned End = S.size();
532 output("'"); // Starting single quote.
533 const char *Base = S.data();
534 while (j < End) {
535 // Escape a single quote by doubling it.
536 if (S[j] == '\'') {
537 output(StringRef(&Base[i], j - i + 1));
538 output("'");
539 i = j + 1;
540 }
541 ++j;
542 }
543 output(StringRef(&Base[i], j - i));
544 this->outputUpToEndOfLine("'"); // Ending single quote.
545}
546
547void Output::setError(const Twine &message) {
548}
549
Aaron Ballman0e63e532013-08-15 23:17:53 +0000550bool Output::canElideEmptySequence() {
551 // Normally, with an optional key/value where the value is an empty sequence,
552 // the whole key/value can be not written. But, that produces wrong yaml
553 // if the key/value is the only thing in the map and the map is used in
554 // a sequence. This detects if the this sequence is the first key/value
555 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000556 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000557 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000558 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000559 return true;
560 return (StateStack[StateStack.size()-2] != inSeq);
561}
562
Nick Kledzikf60a9272012-12-12 20:46:15 +0000563void Output::output(StringRef s) {
564 Column += s.size();
565 Out << s;
566}
567
568void Output::outputUpToEndOfLine(StringRef s) {
569 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000570 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000571 NeedsNewLine = true;
572}
573
574void Output::outputNewLine() {
575 Out << "\n";
576 Column = 0;
577}
578
579// if seq at top, indent as if map, then add "- "
580// if seq in middle, use "- " if firstKey, else use " "
581//
582
583void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000584 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000585 return;
586 NeedsNewLine = false;
587
588 this->outputNewLine();
589
590 assert(StateStack.size() > 0);
591 unsigned Indent = StateStack.size() - 1;
592 bool OutputDash = false;
593
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000594 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000595 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000596 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
597 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000598 --Indent;
599 OutputDash = true;
600 }
601
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000602 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000603 output(" ");
604 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000605 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000606 output("- ");
607 }
608
609}
610
611void Output::paddedKey(StringRef key) {
612 output(key);
613 output(":");
614 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000615 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000616 output(&spaces[key.size()]);
617 else
618 output(" ");
619}
620
621//===----------------------------------------------------------------------===//
622// traits for built-in types
623//===----------------------------------------------------------------------===//
624
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000625void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
626 Out << (Val ? "true" : "false");
627}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000628
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000629StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
630 if (Scalar.equals("true")) {
631 Val = true;
632 return StringRef();
633 } else if (Scalar.equals("false")) {
634 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000635 return StringRef();
636 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000637 return "invalid boolean";
638}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000639
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000640void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
641 raw_ostream &Out) {
642 Out << Val;
643}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000644
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000645StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
646 StringRef &Val) {
647 Val = Scalar;
648 return StringRef();
649}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000650
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000651void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
652 raw_ostream &Out) {
653 // use temp uin32_t because ostream thinks uint8_t is a character
654 uint32_t Num = Val;
655 Out << Num;
656}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000657
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000658StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
659 unsigned long long n;
660 if (getAsUnsignedInteger(Scalar, 0, n))
661 return "invalid number";
662 if (n > 0xFF)
663 return "out of range number";
664 Val = n;
665 return StringRef();
666}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000667
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000668void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
669 raw_ostream &Out) {
670 Out << Val;
671}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000672
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000673StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
674 uint16_t &Val) {
675 unsigned long long n;
676 if (getAsUnsignedInteger(Scalar, 0, n))
677 return "invalid number";
678 if (n > 0xFFFF)
679 return "out of range number";
680 Val = n;
681 return StringRef();
682}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000683
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000684void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
685 raw_ostream &Out) {
686 Out << Val;
687}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000688
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000689StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
690 uint32_t &Val) {
691 unsigned long long n;
692 if (getAsUnsignedInteger(Scalar, 0, n))
693 return "invalid number";
694 if (n > 0xFFFFFFFFUL)
695 return "out of range number";
696 Val = n;
697 return StringRef();
698}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000699
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000700void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
701 raw_ostream &Out) {
702 Out << Val;
703}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000704
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000705StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
706 uint64_t &Val) {
707 unsigned long long N;
708 if (getAsUnsignedInteger(Scalar, 0, N))
709 return "invalid number";
710 Val = N;
711 return StringRef();
712}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000713
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000714void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
715 // use temp in32_t because ostream thinks int8_t is a character
716 int32_t Num = Val;
717 Out << Num;
718}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000719
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000720StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
721 long long N;
722 if (getAsSignedInteger(Scalar, 0, N))
723 return "invalid number";
724 if ((N > 127) || (N < -128))
725 return "out of range number";
726 Val = N;
727 return StringRef();
728}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000729
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000730void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
731 raw_ostream &Out) {
732 Out << Val;
733}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000734
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000735StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
736 long long N;
737 if (getAsSignedInteger(Scalar, 0, N))
738 return "invalid number";
739 if ((N > INT16_MAX) || (N < INT16_MIN))
740 return "out of range number";
741 Val = N;
742 return StringRef();
743}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000744
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000745void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
746 raw_ostream &Out) {
747 Out << Val;
748}
749
750StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
751 long long N;
752 if (getAsSignedInteger(Scalar, 0, N))
753 return "invalid number";
754 if ((N > INT32_MAX) || (N < INT32_MIN))
755 return "out of range number";
756 Val = N;
757 return StringRef();
758}
759
760void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
761 raw_ostream &Out) {
762 Out << Val;
763}
764
765StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
766 long long N;
767 if (getAsSignedInteger(Scalar, 0, N))
768 return "invalid number";
769 Val = N;
770 return StringRef();
771}
772
773void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000774 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000775}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000776
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000777StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
778 SmallString<32> buff(Scalar.begin(), Scalar.end());
779 char *end;
780 Val = strtod(buff.c_str(), &end);
781 if (*end != '\0')
782 return "invalid floating point number";
783 return StringRef();
784}
785
786void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000787 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000788}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000789
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000790StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
791 SmallString<32> buff(Scalar.begin(), Scalar.end());
792 char *end;
793 Val = strtod(buff.c_str(), &end);
794 if (*end != '\0')
795 return "invalid floating point number";
796 return StringRef();
797}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000798
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000799void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
800 uint8_t Num = Val;
801 Out << format("0x%02X", Num);
802}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000803
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000804StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
805 unsigned long long n;
806 if (getAsUnsignedInteger(Scalar, 0, n))
807 return "invalid hex8 number";
808 if (n > 0xFF)
809 return "out of range hex8 number";
810 Val = n;
811 return StringRef();
812}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000813
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000814void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
815 uint16_t Num = Val;
816 Out << format("0x%04X", Num);
817}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000818
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000819StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
820 unsigned long long n;
821 if (getAsUnsignedInteger(Scalar, 0, n))
822 return "invalid hex16 number";
823 if (n > 0xFFFF)
824 return "out of range hex16 number";
825 Val = n;
826 return StringRef();
827}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000828
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000829void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
830 uint32_t Num = Val;
831 Out << format("0x%08X", Num);
832}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000833
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000834StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
835 unsigned long long n;
836 if (getAsUnsignedInteger(Scalar, 0, n))
837 return "invalid hex32 number";
838 if (n > 0xFFFFFFFFUL)
839 return "out of range hex32 number";
840 Val = n;
841 return StringRef();
842}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000843
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000844void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
845 uint64_t Num = Val;
846 Out << format("0x%016llX", Num);
847}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000848
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000849StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
850 unsigned long long Num;
851 if (getAsUnsignedInteger(Scalar, 0, Num))
852 return "invalid hex64 number";
853 Val = Num;
854 return StringRef();
855}