blob: 08562fce86004dbdd72560046b5709caed9673e2 [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
Nick Kledzikdd34f772013-11-14 02:38:07 +000062bool Input::outputting() const {
Nick Kledzikf60a9272012-12-12 20:46:15 +000063 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}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000084
Nick Kledzik1e6033c2013-11-14 00:59:59 +000085bool Input::mapTag(StringRef Tag, bool Default) {
86 StringRef foundTag = CurrentNode->_node->getVerbatimTag();
87 if (foundTag.empty()) {
88 // If no tag found and 'Tag' is the default, say it was found.
89 return Default;
90 }
91 // Return true iff found tag matches supplied tag.
92 return Tag.equals(foundTag);
93}
Nick Kledzikf60a9272012-12-12 20:46:15 +000094
95void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000096 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +000097 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +000098 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
99 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000100 MN->ValidKeys.clear();
101 }
102}
103
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000104bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
105 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000106 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000107 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000108 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000109 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
110 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000111 setError(CurrentNode, "not a mapping");
112 return false;
113 }
114 MN->ValidKeys.push_back(Key);
115 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000116 if (!Value) {
117 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000118 setError(CurrentNode, Twine("missing required key '") + Key + "'");
119 else
120 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000121 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000122 }
123 SaveInfo = CurrentNode;
124 CurrentNode = Value;
125 return true;
126}
127
128void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000129 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000130}
131
132void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000133 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000134 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000135 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
136 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000137 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000138 for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
139 End = MN->Mapping.end(); i != End; ++i) {
Dmitri Gribenkodf73c302013-08-07 05:51:27 +0000140 if (!MN->isValidKey(i->first())) {
141 setError(i->second, Twine("unknown key '") + i->first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000142 break;
143 }
144 }
145}
146
Nick Kledzikf60a9272012-12-12 20:46:15 +0000147unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000148 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000149 return SQ->Entries.size();
150 }
151 return 0;
152}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000153
Nick Kledzikf60a9272012-12-12 20:46:15 +0000154void Input::endSequence() {
155}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000156
Nick Kledzikf60a9272012-12-12 20:46:15 +0000157bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000158 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000159 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000160 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000161 SaveInfo = CurrentNode;
162 CurrentNode = SQ->Entries[Index];
163 return true;
164 }
165 return false;
166}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000167
Nick Kledzikf60a9272012-12-12 20:46:15 +0000168void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000169 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170}
171
172unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000173 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000174 return SQ->Entries.size();
175 }
176 return 0;
177}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000178
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000180 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000181 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000182 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000183 SaveInfo = CurrentNode;
184 CurrentNode = SQ->Entries[index];
185 return true;
186 }
187 return false;
188}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000189
Nick Kledzikf60a9272012-12-12 20:46:15 +0000190void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000191 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000192}
193
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194void Input::endFlowSequence() {
195}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000196
197void Input::beginEnumScalar() {
198 ScalarMatchFound = false;
199}
200
201bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000202 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000203 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000204 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
205 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206 ScalarMatchFound = true;
207 return true;
208 }
209 }
210 return false;
211}
212
213void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000214 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000215 setError(CurrentNode, "unknown enumerated scalar");
216 }
217}
218
Nick Kledzikf60a9272012-12-12 20:46:15 +0000219bool Input::beginBitSetScalar(bool &DoClear) {
220 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000221 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000222 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000223 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 setError(CurrentNode, "expected sequence of bit values");
225 }
226 DoClear = true;
227 return true;
228}
229
230bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000231 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000232 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000233 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000234 unsigned Index = 0;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000235 for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
236 End = SQ->Entries.end(); i != End; ++i) {
237 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
238 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000239 BitValuesUsed[Index] = true;
240 return true;
241 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000242 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000243 setError(CurrentNode, "unexpected scalar in sequence of bit values");
244 }
245 ++Index;
246 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000247 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000248 setError(CurrentNode, "expected sequence of bit values");
249 }
250 return false;
251}
252
253void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000256 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000258 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
259 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000260 setError(SQ->Entries[i], "unknown bit value");
261 return;
262 }
263 }
264 }
265}
266
Nick Kledzikf60a9272012-12-12 20:46:15 +0000267void Input::scalarString(StringRef &S) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000268 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000269 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000270 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000271 setError(CurrentNode, "unexpected scalar");
272 }
273}
274
275void Input::setError(HNode *hnode, const Twine &message) {
276 this->setError(hnode->_node, message);
277}
278
279void Input::setError(Node *node, const Twine &message) {
280 Strm->printError(node, message);
281 EC = make_error_code(errc::invalid_argument);
282}
283
284Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000285 SmallString<128> StringStorage;
286 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000287 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 // Copy string to permanent storage
290 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000291 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000292 memcpy(Buf, &StringStorage[0], Len);
293 KeyStr = StringRef(Buf, Len);
294 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000295 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000296 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000297 SequenceHNode *SQHNode = new SequenceHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000298 for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
299 ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000300 HNode *Entry = this->createHNodes(i);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000301 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000302 break;
303 SQHNode->Entries.push_back(Entry);
304 }
305 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000306 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000307 MapHNode *mapHNode = new MapHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000308 for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
309 ++i) {
310 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
Nick Kledzikf60a9272012-12-12 20:46:15 +0000311 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000312 StringRef KeyStr = KeyScalar->getValue(StringStorage);
313 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000314 // Copy string to permanent storage
315 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000316 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000317 memcpy(Buf, &StringStorage[0], Len);
318 KeyStr = StringRef(Buf, Len);
319 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000320 HNode *ValueHNode = this->createHNodes(i->getValue());
321 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322 break;
323 mapHNode->Mapping[KeyStr] = ValueHNode;
324 }
325 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000326 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000327 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000328 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000329 setError(N, "unknown node kind");
330 return NULL;
331 }
332}
333
Nick Kledzikf60a9272012-12-12 20:46:15 +0000334bool Input::MapHNode::isValidKey(StringRef Key) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000335 for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000336 End = ValidKeys.end(); i != End; ++i) {
337 if (Key.equals(*i))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000338 return true;
339 }
340 return false;
341}
342
343void Input::setError(const Twine &Message) {
344 this->setError(CurrentNode, Message);
345}
346
Aaron Ballman0e63e532013-08-15 23:17:53 +0000347bool Input::canElideEmptySequence() {
348 return false;
349}
350
Nick Kledzik0dcef842013-01-08 21:04:44 +0000351Input::MapHNode::~MapHNode() {
352 for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
353 i != End; ++i) {
354 delete i->second;
355 }
356}
357
358Input::SequenceHNode::~SequenceHNode() {
359 for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
360 i != End; ++i) {
361 delete *i;
362 }
363}
364
365
366
Nick Kledzikf60a9272012-12-12 20:46:15 +0000367//===----------------------------------------------------------------------===//
368// Output
369//===----------------------------------------------------------------------===//
370
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000371Output::Output(raw_ostream &yout, void *context)
372 : IO(context),
373 Out(yout),
374 Column(0),
375 ColumnAtFlowStart(0),
376 NeedBitValueComma(false),
377 NeedFlowSequenceComma(false),
378 EnumerationMatchFound(false),
379 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000380}
381
382Output::~Output() {
383}
384
Nick Kledzikdd34f772013-11-14 02:38:07 +0000385bool Output::outputting() const {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000386 return true;
387}
388
389void Output::beginMapping() {
390 StateStack.push_back(inMapFirstKey);
391 NeedsNewLine = true;
392}
393
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000394bool Output::mapTag(StringRef Tag, bool Use) {
395 if (Use) {
396 this->output(" ");
397 this->output(Tag);
398 }
399 return Use;
400}
401
Nick Kledzikf60a9272012-12-12 20:46:15 +0000402void Output::endMapping() {
403 StateStack.pop_back();
404}
405
Nick Kledzikf60a9272012-12-12 20:46:15 +0000406bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000407 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000408 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000409 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000410 this->newLineCheck();
411 this->paddedKey(Key);
412 return true;
413 }
414 return false;
415}
416
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000417void Output::postflightKey(void *) {
418 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000419 StateStack.pop_back();
420 StateStack.push_back(inMapOtherKey);
421 }
422}
423
424void Output::beginDocuments() {
425 this->outputUpToEndOfLine("---");
426}
427
428bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000429 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000430 this->outputUpToEndOfLine("\n---");
431 return true;
432}
433
434void Output::postflightDocument() {
435}
436
437void Output::endDocuments() {
438 output("\n...\n");
439}
440
441unsigned Output::beginSequence() {
442 StateStack.push_back(inSeq);
443 NeedsNewLine = true;
444 return 0;
445}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000446
Nick Kledzikf60a9272012-12-12 20:46:15 +0000447void Output::endSequence() {
448 StateStack.pop_back();
449}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000450
451bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000452 return true;
453}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000454
455void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000456}
457
458unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000459 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000460 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000461 ColumnAtFlowStart = Column;
462 output("[ ");
463 NeedFlowSequenceComma = false;
464 return 0;
465}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000466
Nick Kledzikf60a9272012-12-12 20:46:15 +0000467void Output::endFlowSequence() {
468 StateStack.pop_back();
469 this->outputUpToEndOfLine(" ]");
470}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000471
472bool Output::preflightFlowElement(unsigned, void *&) {
473 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000474 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000475 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000476 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000477 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000478 output(" ");
479 Column = ColumnAtFlowStart;
480 output(" ");
481 }
482 return true;
483}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000484
485void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000486 NeedFlowSequenceComma = true;
487}
488
Nick Kledzikf60a9272012-12-12 20:46:15 +0000489void Output::beginEnumScalar() {
490 EnumerationMatchFound = false;
491}
492
493bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000494 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000495 this->newLineCheck();
496 this->outputUpToEndOfLine(Str);
497 EnumerationMatchFound = true;
498 }
499 return false;
500}
501
502void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000503 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000504 llvm_unreachable("bad runtime enum value");
505}
506
Nick Kledzikf60a9272012-12-12 20:46:15 +0000507bool Output::beginBitSetScalar(bool &DoClear) {
508 this->newLineCheck();
509 output("[ ");
510 NeedBitValueComma = false;
511 DoClear = false;
512 return true;
513}
514
515bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000516 if (Matches) {
517 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000518 output(", ");
519 this->output(Str);
520 NeedBitValueComma = true;
521 }
522 return false;
523}
524
525void Output::endBitSetScalar() {
526 this->outputUpToEndOfLine(" ]");
527}
528
529void Output::scalarString(StringRef &S) {
Rui Ueyama106eded2013-09-11 04:00:08 +0000530 const char ScalarSafeChars[] = "abcdefghijklmnopqrstuvwxyz"
531 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
532
Nick Kledzikf60a9272012-12-12 20:46:15 +0000533 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000534 if (S.empty()) {
535 // Print '' for the empty string because leaving the field empty is not
536 // allowed.
537 this->outputUpToEndOfLine("''");
538 return;
539 }
540 if (S.find_first_not_of(ScalarSafeChars) == StringRef::npos &&
541 !isspace(S.front()) && !isspace(S.back())) {
542 // If the string consists only of safe characters, print it out without
543 // quotes.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000544 this->outputUpToEndOfLine(S);
545 return;
546 }
547 unsigned i = 0;
548 unsigned j = 0;
549 unsigned End = S.size();
550 output("'"); // Starting single quote.
551 const char *Base = S.data();
552 while (j < End) {
553 // Escape a single quote by doubling it.
554 if (S[j] == '\'') {
555 output(StringRef(&Base[i], j - i + 1));
556 output("'");
557 i = j + 1;
558 }
559 ++j;
560 }
561 output(StringRef(&Base[i], j - i));
562 this->outputUpToEndOfLine("'"); // Ending single quote.
563}
564
565void Output::setError(const Twine &message) {
566}
567
Aaron Ballman0e63e532013-08-15 23:17:53 +0000568bool Output::canElideEmptySequence() {
569 // Normally, with an optional key/value where the value is an empty sequence,
570 // the whole key/value can be not written. But, that produces wrong yaml
571 // if the key/value is the only thing in the map and the map is used in
572 // a sequence. This detects if the this sequence is the first key/value
573 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000574 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000575 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000576 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000577 return true;
578 return (StateStack[StateStack.size()-2] != inSeq);
579}
580
Nick Kledzikf60a9272012-12-12 20:46:15 +0000581void Output::output(StringRef s) {
582 Column += s.size();
583 Out << s;
584}
585
586void Output::outputUpToEndOfLine(StringRef s) {
587 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000588 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000589 NeedsNewLine = true;
590}
591
592void Output::outputNewLine() {
593 Out << "\n";
594 Column = 0;
595}
596
597// if seq at top, indent as if map, then add "- "
598// if seq in middle, use "- " if firstKey, else use " "
599//
600
601void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000602 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000603 return;
604 NeedsNewLine = false;
605
606 this->outputNewLine();
607
608 assert(StateStack.size() > 0);
609 unsigned Indent = StateStack.size() - 1;
610 bool OutputDash = false;
611
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000612 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000613 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000614 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
615 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000616 --Indent;
617 OutputDash = true;
618 }
619
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000620 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000621 output(" ");
622 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000623 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000624 output("- ");
625 }
626
627}
628
629void Output::paddedKey(StringRef key) {
630 output(key);
631 output(":");
632 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000633 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000634 output(&spaces[key.size()]);
635 else
636 output(" ");
637}
638
639//===----------------------------------------------------------------------===//
640// traits for built-in types
641//===----------------------------------------------------------------------===//
642
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000643void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
644 Out << (Val ? "true" : "false");
645}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000646
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000647StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
648 if (Scalar.equals("true")) {
649 Val = true;
650 return StringRef();
651 } else if (Scalar.equals("false")) {
652 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000653 return StringRef();
654 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000655 return "invalid boolean";
656}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000657
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000658void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
659 raw_ostream &Out) {
660 Out << Val;
661}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000662
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000663StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
664 StringRef &Val) {
665 Val = Scalar;
666 return StringRef();
667}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000668
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000669void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
670 raw_ostream &Out) {
671 // use temp uin32_t because ostream thinks uint8_t is a character
672 uint32_t Num = Val;
673 Out << Num;
674}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000675
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000676StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
677 unsigned long long n;
678 if (getAsUnsignedInteger(Scalar, 0, n))
679 return "invalid number";
680 if (n > 0xFF)
681 return "out of range number";
682 Val = n;
683 return StringRef();
684}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000685
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000686void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
687 raw_ostream &Out) {
688 Out << Val;
689}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000690
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000691StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
692 uint16_t &Val) {
693 unsigned long long n;
694 if (getAsUnsignedInteger(Scalar, 0, n))
695 return "invalid number";
696 if (n > 0xFFFF)
697 return "out of range number";
698 Val = n;
699 return StringRef();
700}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000701
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000702void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
703 raw_ostream &Out) {
704 Out << Val;
705}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000706
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000707StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
708 uint32_t &Val) {
709 unsigned long long n;
710 if (getAsUnsignedInteger(Scalar, 0, n))
711 return "invalid number";
712 if (n > 0xFFFFFFFFUL)
713 return "out of range number";
714 Val = n;
715 return StringRef();
716}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000717
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000718void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
719 raw_ostream &Out) {
720 Out << Val;
721}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000722
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000723StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
724 uint64_t &Val) {
725 unsigned long long N;
726 if (getAsUnsignedInteger(Scalar, 0, N))
727 return "invalid number";
728 Val = N;
729 return StringRef();
730}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000731
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000732void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
733 // use temp in32_t because ostream thinks int8_t is a character
734 int32_t Num = Val;
735 Out << Num;
736}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000737
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000738StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
739 long long N;
740 if (getAsSignedInteger(Scalar, 0, N))
741 return "invalid number";
742 if ((N > 127) || (N < -128))
743 return "out of range number";
744 Val = N;
745 return StringRef();
746}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000747
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000748void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
749 raw_ostream &Out) {
750 Out << Val;
751}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000752
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000753StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
754 long long N;
755 if (getAsSignedInteger(Scalar, 0, N))
756 return "invalid number";
757 if ((N > INT16_MAX) || (N < INT16_MIN))
758 return "out of range number";
759 Val = N;
760 return StringRef();
761}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000762
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000763void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
764 raw_ostream &Out) {
765 Out << Val;
766}
767
768StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
769 long long N;
770 if (getAsSignedInteger(Scalar, 0, N))
771 return "invalid number";
772 if ((N > INT32_MAX) || (N < INT32_MIN))
773 return "out of range number";
774 Val = N;
775 return StringRef();
776}
777
778void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
779 raw_ostream &Out) {
780 Out << Val;
781}
782
783StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
784 long long N;
785 if (getAsSignedInteger(Scalar, 0, N))
786 return "invalid number";
787 Val = N;
788 return StringRef();
789}
790
791void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000792 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000793}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000794
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000795StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
796 SmallString<32> buff(Scalar.begin(), Scalar.end());
797 char *end;
798 Val = strtod(buff.c_str(), &end);
799 if (*end != '\0')
800 return "invalid floating point number";
801 return StringRef();
802}
803
804void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000805 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000806}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000807
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000808StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
809 SmallString<32> buff(Scalar.begin(), Scalar.end());
810 char *end;
811 Val = strtod(buff.c_str(), &end);
812 if (*end != '\0')
813 return "invalid floating point number";
814 return StringRef();
815}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000816
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000817void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
818 uint8_t Num = Val;
819 Out << format("0x%02X", Num);
820}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000821
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000822StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
823 unsigned long long n;
824 if (getAsUnsignedInteger(Scalar, 0, n))
825 return "invalid hex8 number";
826 if (n > 0xFF)
827 return "out of range hex8 number";
828 Val = n;
829 return StringRef();
830}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000831
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000832void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
833 uint16_t Num = Val;
834 Out << format("0x%04X", Num);
835}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000836
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000837StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
838 unsigned long long n;
839 if (getAsUnsignedInteger(Scalar, 0, n))
840 return "invalid hex16 number";
841 if (n > 0xFFFF)
842 return "out of range hex16 number";
843 Val = n;
844 return StringRef();
845}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000846
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000847void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
848 uint32_t Num = Val;
849 Out << format("0x%08X", Num);
850}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000852StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
853 unsigned long long n;
854 if (getAsUnsignedInteger(Scalar, 0, n))
855 return "invalid hex32 number";
856 if (n > 0xFFFFFFFFUL)
857 return "out of range hex32 number";
858 Val = n;
859 return StringRef();
860}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000861
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000862void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
863 uint64_t Num = Val;
864 Out << format("0x%016llX", Num);
865}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000866
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000867StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
868 unsigned long long Num;
869 if (getAsUnsignedInteger(Scalar, 0, Num))
870 return "invalid hex64 number";
871 Val = Num;
872 return StringRef();
873}