blob: 5212624f0cd3f1f6b7a86c1e836b0ea84976631b [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
Rafael Espindola2a826e42014-06-13 17:20:48 +000010#include "llvm/Support/Errc.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000011#include "llvm/Support/YAMLTraits.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000012#include "llvm/ADT/Twine.h"
13#include "llvm/Support/Casting.h"
14#include "llvm/Support/ErrorHandling.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000015#include "llvm/Support/Format.h"
Nick Kledzikf60a9272012-12-12 20:46:15 +000016#include "llvm/Support/YAMLParser.h"
Benjamin Kramercbe05842012-12-12 20:55:44 +000017#include "llvm/Support/raw_ostream.h"
Rui Ueyama106eded2013-09-11 04:00:08 +000018#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include <cstring>
Benjamin Kramer36b0f122012-12-12 22:40:02 +000020using namespace llvm;
21using namespace yaml;
Nick Kledzikf60a9272012-12-12 20:46:15 +000022
23//===----------------------------------------------------------------------===//
24// IO
25//===----------------------------------------------------------------------===//
26
27IO::IO(void *Context) : Ctxt(Context) {
28}
29
30IO::~IO() {
31}
32
33void *IO::getContext() {
34 return Ctxt;
35}
36
37void IO::setContext(void *Context) {
38 Ctxt = Context;
39}
40
Nick Kledzikf60a9272012-12-12 20:46:15 +000041//===----------------------------------------------------------------------===//
42// Input
43//===----------------------------------------------------------------------===//
44
Alexander Kornienko681e37c2013-11-18 15:50:04 +000045Input::Input(StringRef InputContent,
46 void *Ctxt,
47 SourceMgr::DiagHandlerTy DiagHandler,
48 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000049 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000050 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000051 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000052 if (DiagHandler)
53 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000054 DocIterator = Strm->begin();
55}
56
Nick Kledzik0dcef842013-01-08 21:04:44 +000057Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000058}
59
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000060std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000061
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000062// Pin the vtables to this file.
63void Input::HNode::anchor() {}
64void Input::EmptyHNode::anchor() {}
65void Input::ScalarHNode::anchor() {}
66
Nick Kledzik4761c602013-11-21 00:20:10 +000067bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000068 return false;
69}
70
71bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000072 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000073 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000074 if (!N) {
75 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000076 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000077 return false;
78 }
79
Benjamin Kramer36b0f122012-12-12 22:40:02 +000080 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000081 // Empty files are allowed and ignored
82 ++DocIterator;
83 return setCurrentDocument();
84 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000085 TopNode.reset(this->createHNodes(N));
86 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000087 return true;
88 }
89 return false;
90}
91
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000092bool Input::nextDocument() {
93 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000094}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000095
Nick Kledzik1e6033c2013-11-14 00:59:59 +000096bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000097 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +000098 if (foundTag.empty()) {
99 // If no tag found and 'Tag' is the default, say it was found.
100 return Default;
101 }
102 // Return true iff found tag matches supplied tag.
103 return Tag.equals(foundTag);
104}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000105
106void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000107 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000108 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000109 // CurrentNode can be null if the document is empty.
110 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000111 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000112 MN->ValidKeys.clear();
113 }
114}
115
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000116bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
117 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000118 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000119 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000120 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000121
122 // CurrentNode is null for empty documents, which is an error in case required
123 // nodes are present.
124 if (!CurrentNode) {
125 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000126 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000127 return false;
128 }
129
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000130 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
131 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000132 setError(CurrentNode, "not a mapping");
133 return false;
134 }
135 MN->ValidKeys.push_back(Key);
136 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000137 if (!Value) {
138 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000139 setError(CurrentNode, Twine("missing required key '") + Key + "'");
140 else
141 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000142 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000143 }
144 SaveInfo = CurrentNode;
145 CurrentNode = Value;
146 return true;
147}
148
149void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000150 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000151}
152
153void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000154 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000155 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000156 // CurrentNode can be null if the document is empty.
157 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000158 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000159 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000160 for (const auto &NN : MN->Mapping) {
161 if (!MN->isValidKey(NN.first())) {
162 setError(NN.second, Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000163 break;
164 }
165 }
166}
167
Nick Kledzikf60a9272012-12-12 20:46:15 +0000168unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000169 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170 return SQ->Entries.size();
171 }
172 return 0;
173}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000174
Nick Kledzikf60a9272012-12-12 20:46:15 +0000175void Input::endSequence() {
176}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000177
Nick Kledzikf60a9272012-12-12 20:46:15 +0000178bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000179 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000181 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182 SaveInfo = CurrentNode;
183 CurrentNode = SQ->Entries[Index];
184 return true;
185 }
186 return false;
187}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000188
Nick Kledzikf60a9272012-12-12 20:46:15 +0000189void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000190 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191}
192
193unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000194 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000195 return SQ->Entries.size();
196 }
197 return 0;
198}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000199
Nick Kledzikf60a9272012-12-12 20:46:15 +0000200bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000201 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000202 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204 SaveInfo = CurrentNode;
205 CurrentNode = SQ->Entries[index];
206 return true;
207 }
208 return false;
209}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000210
Nick Kledzikf60a9272012-12-12 20:46:15 +0000211void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000212 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213}
214
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000215void Input::endFlowSequence() {
216}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000217
218void Input::beginEnumScalar() {
219 ScalarMatchFound = false;
220}
221
222bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000223 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000224 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
226 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000227 ScalarMatchFound = true;
228 return true;
229 }
230 }
231 return false;
232}
233
234void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000235 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000236 setError(CurrentNode, "unknown enumerated scalar");
237 }
238}
239
Nick Kledzikf60a9272012-12-12 20:46:15 +0000240bool Input::beginBitSetScalar(bool &DoClear) {
241 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000242 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000243 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000244 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 setError(CurrentNode, "expected sequence of bit values");
246 }
247 DoClear = true;
248 return true;
249}
250
251bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000252 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000253 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 unsigned Index = 0;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000256 for (HNode *N : SQ->Entries) {
257 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000258 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000259 BitValuesUsed[Index] = true;
260 return true;
261 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000262 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000263 setError(CurrentNode, "unexpected scalar in sequence of bit values");
264 }
265 ++Index;
266 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000267 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000268 setError(CurrentNode, "expected sequence of bit values");
269 }
270 return false;
271}
272
273void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000274 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000275 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000276 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000277 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000278 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
279 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000280 setError(SQ->Entries[i], "unknown bit value");
281 return;
282 }
283 }
284 }
285}
286
David Majnemer77880332014-04-10 07:37:33 +0000287void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000288 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000289 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291 setError(CurrentNode, "unexpected scalar");
292 }
293}
294
295void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000296 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000297 this->setError(hnode->_node, message);
298}
299
300void Input::setError(Node *node, const Twine &message) {
301 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000302 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000303}
304
305Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000306 SmallString<128> StringStorage;
307 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000308 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000309 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000310 // Copy string to permanent storage
311 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000312 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000313 memcpy(Buf, &StringStorage[0], Len);
314 KeyStr = StringRef(Buf, Len);
315 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000316 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000317 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000318 SequenceHNode *SQHNode = new SequenceHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000319 for (Node &SN : *SQ) {
320 HNode *Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000321 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000322 break;
323 SQHNode->Entries.push_back(Entry);
324 }
325 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000326 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000327 MapHNode *mapHNode = new MapHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000328 for (KeyValueNode &KVN : *Map) {
329 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
Nick Kledzikf60a9272012-12-12 20:46:15 +0000330 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000331 StringRef KeyStr = KeyScalar->getValue(StringStorage);
332 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000333 // Copy string to permanent storage
334 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000335 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000336 memcpy(Buf, &StringStorage[0], Len);
337 KeyStr = StringRef(Buf, Len);
338 }
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000339 HNode *ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000340 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000341 break;
342 mapHNode->Mapping[KeyStr] = ValueHNode;
343 }
344 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000345 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000346 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000347 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000348 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000349 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000350 }
351}
352
Nick Kledzikf60a9272012-12-12 20:46:15 +0000353bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000354 for (const char *K : ValidKeys) {
355 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000356 return true;
357 }
358 return false;
359}
360
361void Input::setError(const Twine &Message) {
362 this->setError(CurrentNode, Message);
363}
364
Aaron Ballman0e63e532013-08-15 23:17:53 +0000365bool Input::canElideEmptySequence() {
366 return false;
367}
368
Nick Kledzik0dcef842013-01-08 21:04:44 +0000369Input::MapHNode::~MapHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000370 for (auto &N : Mapping)
371 delete N.second;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000372}
373
374Input::SequenceHNode::~SequenceHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000375 for (HNode *N : Entries)
376 delete N;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000377}
378
379
380
Nick Kledzikf60a9272012-12-12 20:46:15 +0000381//===----------------------------------------------------------------------===//
382// Output
383//===----------------------------------------------------------------------===//
384
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000385Output::Output(raw_ostream &yout, void *context)
386 : IO(context),
387 Out(yout),
388 Column(0),
389 ColumnAtFlowStart(0),
390 NeedBitValueComma(false),
391 NeedFlowSequenceComma(false),
392 EnumerationMatchFound(false),
393 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000394}
395
396Output::~Output() {
397}
398
Nick Kledzik4761c602013-11-21 00:20:10 +0000399bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000400 return true;
401}
402
403void Output::beginMapping() {
404 StateStack.push_back(inMapFirstKey);
405 NeedsNewLine = true;
406}
407
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000408bool Output::mapTag(StringRef Tag, bool Use) {
409 if (Use) {
410 this->output(" ");
411 this->output(Tag);
412 }
413 return Use;
414}
415
Nick Kledzikf60a9272012-12-12 20:46:15 +0000416void Output::endMapping() {
417 StateStack.pop_back();
418}
419
Nick Kledzikf60a9272012-12-12 20:46:15 +0000420bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000421 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000422 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000423 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000424 this->newLineCheck();
425 this->paddedKey(Key);
426 return true;
427 }
428 return false;
429}
430
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000431void Output::postflightKey(void *) {
432 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000433 StateStack.pop_back();
434 StateStack.push_back(inMapOtherKey);
435 }
436}
437
438void Output::beginDocuments() {
439 this->outputUpToEndOfLine("---");
440}
441
442bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000443 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000444 this->outputUpToEndOfLine("\n---");
445 return true;
446}
447
448void Output::postflightDocument() {
449}
450
451void Output::endDocuments() {
452 output("\n...\n");
453}
454
455unsigned Output::beginSequence() {
456 StateStack.push_back(inSeq);
457 NeedsNewLine = true;
458 return 0;
459}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000460
Nick Kledzikf60a9272012-12-12 20:46:15 +0000461void Output::endSequence() {
462 StateStack.pop_back();
463}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000464
465bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000466 return true;
467}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000468
469void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000470}
471
472unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000473 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000474 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000475 ColumnAtFlowStart = Column;
476 output("[ ");
477 NeedFlowSequenceComma = false;
478 return 0;
479}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000480
Nick Kledzikf60a9272012-12-12 20:46:15 +0000481void Output::endFlowSequence() {
482 StateStack.pop_back();
483 this->outputUpToEndOfLine(" ]");
484}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000485
486bool Output::preflightFlowElement(unsigned, void *&) {
487 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000488 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000489 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000490 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000491 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000492 output(" ");
493 Column = ColumnAtFlowStart;
494 output(" ");
495 }
496 return true;
497}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000498
499void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000500 NeedFlowSequenceComma = true;
501}
502
Nick Kledzikf60a9272012-12-12 20:46:15 +0000503void Output::beginEnumScalar() {
504 EnumerationMatchFound = false;
505}
506
507bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000508 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000509 this->newLineCheck();
510 this->outputUpToEndOfLine(Str);
511 EnumerationMatchFound = true;
512 }
513 return false;
514}
515
516void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000517 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000518 llvm_unreachable("bad runtime enum value");
519}
520
Nick Kledzikf60a9272012-12-12 20:46:15 +0000521bool Output::beginBitSetScalar(bool &DoClear) {
522 this->newLineCheck();
523 output("[ ");
524 NeedBitValueComma = false;
525 DoClear = false;
526 return true;
527}
528
529bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000530 if (Matches) {
531 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000532 output(", ");
533 this->output(Str);
534 NeedBitValueComma = true;
535 }
536 return false;
537}
538
539void Output::endBitSetScalar() {
540 this->outputUpToEndOfLine(" ]");
541}
542
David Majnemer77880332014-04-10 07:37:33 +0000543void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000544 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000545 if (S.empty()) {
546 // Print '' for the empty string because leaving the field empty is not
547 // allowed.
548 this->outputUpToEndOfLine("''");
549 return;
550 }
David Majnemer77880332014-04-10 07:37:33 +0000551 if (!MustQuote) {
552 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000553 this->outputUpToEndOfLine(S);
554 return;
555 }
556 unsigned i = 0;
557 unsigned j = 0;
558 unsigned End = S.size();
559 output("'"); // Starting single quote.
560 const char *Base = S.data();
561 while (j < End) {
562 // Escape a single quote by doubling it.
563 if (S[j] == '\'') {
564 output(StringRef(&Base[i], j - i + 1));
565 output("'");
566 i = j + 1;
567 }
568 ++j;
569 }
570 output(StringRef(&Base[i], j - i));
571 this->outputUpToEndOfLine("'"); // Ending single quote.
572}
573
574void Output::setError(const Twine &message) {
575}
576
Aaron Ballman0e63e532013-08-15 23:17:53 +0000577bool Output::canElideEmptySequence() {
578 // Normally, with an optional key/value where the value is an empty sequence,
579 // the whole key/value can be not written. But, that produces wrong yaml
580 // if the key/value is the only thing in the map and the map is used in
581 // a sequence. This detects if the this sequence is the first key/value
582 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000583 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000584 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000585 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000586 return true;
587 return (StateStack[StateStack.size()-2] != inSeq);
588}
589
Nick Kledzikf60a9272012-12-12 20:46:15 +0000590void Output::output(StringRef s) {
591 Column += s.size();
592 Out << s;
593}
594
595void Output::outputUpToEndOfLine(StringRef s) {
596 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000597 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000598 NeedsNewLine = true;
599}
600
601void Output::outputNewLine() {
602 Out << "\n";
603 Column = 0;
604}
605
606// if seq at top, indent as if map, then add "- "
607// if seq in middle, use "- " if firstKey, else use " "
608//
609
610void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000611 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000612 return;
613 NeedsNewLine = false;
614
615 this->outputNewLine();
616
617 assert(StateStack.size() > 0);
618 unsigned Indent = StateStack.size() - 1;
619 bool OutputDash = false;
620
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000621 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000622 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000623 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
624 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000625 --Indent;
626 OutputDash = true;
627 }
628
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000629 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000630 output(" ");
631 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000632 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000633 output("- ");
634 }
635
636}
637
638void Output::paddedKey(StringRef key) {
639 output(key);
640 output(":");
641 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000642 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000643 output(&spaces[key.size()]);
644 else
645 output(" ");
646}
647
648//===----------------------------------------------------------------------===//
649// traits for built-in types
650//===----------------------------------------------------------------------===//
651
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000652void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
653 Out << (Val ? "true" : "false");
654}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000655
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000656StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
657 if (Scalar.equals("true")) {
658 Val = true;
659 return StringRef();
660 } else if (Scalar.equals("false")) {
661 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000662 return StringRef();
663 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000664 return "invalid boolean";
665}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000666
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000667void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
668 raw_ostream &Out) {
669 Out << Val;
670}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000671
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000672StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
673 StringRef &Val) {
674 Val = Scalar;
675 return StringRef();
676}
John Thompson48e018a2013-11-19 17:28:21 +0000677
678void ScalarTraits<std::string>::output(const std::string &Val, void *,
679 raw_ostream &Out) {
680 Out << Val;
681}
682
683StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
684 std::string &Val) {
685 Val = Scalar.str();
686 return StringRef();
687}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000688
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000689void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
690 raw_ostream &Out) {
691 // use temp uin32_t because ostream thinks uint8_t is a character
692 uint32_t Num = Val;
693 Out << Num;
694}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000695
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000696StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
697 unsigned long long n;
698 if (getAsUnsignedInteger(Scalar, 0, n))
699 return "invalid number";
700 if (n > 0xFF)
701 return "out of range number";
702 Val = n;
703 return StringRef();
704}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000705
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000706void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
707 raw_ostream &Out) {
708 Out << Val;
709}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000710
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000711StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
712 uint16_t &Val) {
713 unsigned long long n;
714 if (getAsUnsignedInteger(Scalar, 0, n))
715 return "invalid number";
716 if (n > 0xFFFF)
717 return "out of range number";
718 Val = n;
719 return StringRef();
720}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000721
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000722void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
723 raw_ostream &Out) {
724 Out << Val;
725}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000726
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000727StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
728 uint32_t &Val) {
729 unsigned long long n;
730 if (getAsUnsignedInteger(Scalar, 0, n))
731 return "invalid number";
732 if (n > 0xFFFFFFFFUL)
733 return "out of range number";
734 Val = n;
735 return StringRef();
736}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000737
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000738void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
739 raw_ostream &Out) {
740 Out << Val;
741}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000742
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000743StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
744 uint64_t &Val) {
745 unsigned long long N;
746 if (getAsUnsignedInteger(Scalar, 0, N))
747 return "invalid number";
748 Val = N;
749 return StringRef();
750}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000751
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000752void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
753 // use temp in32_t because ostream thinks int8_t is a character
754 int32_t Num = Val;
755 Out << Num;
756}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000757
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000758StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
759 long long N;
760 if (getAsSignedInteger(Scalar, 0, N))
761 return "invalid number";
762 if ((N > 127) || (N < -128))
763 return "out of range number";
764 Val = N;
765 return StringRef();
766}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000767
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000768void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
769 raw_ostream &Out) {
770 Out << Val;
771}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000772
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000773StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
774 long long N;
775 if (getAsSignedInteger(Scalar, 0, N))
776 return "invalid number";
777 if ((N > INT16_MAX) || (N < INT16_MIN))
778 return "out of range number";
779 Val = N;
780 return StringRef();
781}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000782
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000783void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
784 raw_ostream &Out) {
785 Out << Val;
786}
787
788StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
789 long long N;
790 if (getAsSignedInteger(Scalar, 0, N))
791 return "invalid number";
792 if ((N > INT32_MAX) || (N < INT32_MIN))
793 return "out of range number";
794 Val = N;
795 return StringRef();
796}
797
798void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
799 raw_ostream &Out) {
800 Out << Val;
801}
802
803StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
804 long long N;
805 if (getAsSignedInteger(Scalar, 0, N))
806 return "invalid number";
807 Val = N;
808 return StringRef();
809}
810
811void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000812 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000813}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000814
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000815StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
816 SmallString<32> buff(Scalar.begin(), Scalar.end());
817 char *end;
818 Val = strtod(buff.c_str(), &end);
819 if (*end != '\0')
820 return "invalid floating point number";
821 return StringRef();
822}
823
824void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000825 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000826}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000827
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000828StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
829 SmallString<32> buff(Scalar.begin(), Scalar.end());
830 char *end;
831 Val = strtod(buff.c_str(), &end);
832 if (*end != '\0')
833 return "invalid floating point number";
834 return StringRef();
835}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000836
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000837void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
838 uint8_t Num = Val;
839 Out << format("0x%02X", Num);
840}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000841
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000842StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
843 unsigned long long n;
844 if (getAsUnsignedInteger(Scalar, 0, n))
845 return "invalid hex8 number";
846 if (n > 0xFF)
847 return "out of range hex8 number";
848 Val = n;
849 return StringRef();
850}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000852void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
853 uint16_t Num = Val;
854 Out << format("0x%04X", Num);
855}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000856
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000857StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
858 unsigned long long n;
859 if (getAsUnsignedInteger(Scalar, 0, n))
860 return "invalid hex16 number";
861 if (n > 0xFFFF)
862 return "out of range hex16 number";
863 Val = n;
864 return StringRef();
865}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000866
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000867void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
868 uint32_t Num = Val;
869 Out << format("0x%08X", Num);
870}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000871
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000872StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
873 unsigned long long n;
874 if (getAsUnsignedInteger(Scalar, 0, n))
875 return "invalid hex32 number";
876 if (n > 0xFFFFFFFFUL)
877 return "out of range hex32 number";
878 Val = n;
879 return StringRef();
880}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000881
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000882void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
883 uint64_t Num = Val;
884 Out << format("0x%016llX", Num);
885}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000886
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000887StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
888 unsigned long long Num;
889 if (getAsUnsignedInteger(Scalar, 0, Num))
890 return "invalid hex64 number";
891 Val = Num;
892 return StringRef();
893}