blob: 0bf9cf89088b543ebd5e0a6e67ad70941f6327da [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"
Rui Ueyama106eded2013-09-11 04:00:08 +000017#include <cctype>
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include <cstring>
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
Alexander Kornienko681e37c2013-11-18 15:50:04 +000044Input::Input(StringRef InputContent,
45 void *Ctxt,
46 SourceMgr::DiagHandlerTy DiagHandler,
47 void *DiagHandlerCtxt)
Rui Ueyama38dfffa2013-09-11 00:53:07 +000048 : IO(Ctxt),
Nick Kledzik0dcef842013-01-08 21:04:44 +000049 Strm(new Stream(InputContent, SrcMgr)),
Craig Topperc10719f2014-04-07 04:17:22 +000050 CurrentNode(nullptr) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +000051 if (DiagHandler)
52 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
Nick Kledzikf60a9272012-12-12 20:46:15 +000053 DocIterator = Strm->begin();
54}
55
Nick Kledzik0dcef842013-01-08 21:04:44 +000056Input::~Input() {
Nick Kledzik0dcef842013-01-08 21:04:44 +000057}
58
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000059std::error_code Input::error() { return EC; }
Nick Kledzikf60a9272012-12-12 20:46:15 +000060
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000061// Pin the vtables to this file.
62void Input::HNode::anchor() {}
63void Input::EmptyHNode::anchor() {}
64void Input::ScalarHNode::anchor() {}
65
Nick Kledzik4761c602013-11-21 00:20:10 +000066bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000067 return false;
68}
69
70bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000071 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000072 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000073 if (!N) {
74 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindolaed6882b2014-06-12 11:58:49 +000075 EC = std::make_error_code(std::errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000076 return false;
77 }
78
Benjamin Kramer36b0f122012-12-12 22:40:02 +000079 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000080 // Empty files are allowed and ignored
81 ++DocIterator;
82 return setCurrentDocument();
83 }
Nick Kledzik0dcef842013-01-08 21:04:44 +000084 TopNode.reset(this->createHNodes(N));
85 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000086 return true;
87 }
88 return false;
89}
90
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000091bool Input::nextDocument() {
92 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000093}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000094
Nick Kledzik1e6033c2013-11-14 00:59:59 +000095bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000096 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +000097 if (foundTag.empty()) {
98 // If no tag found and 'Tag' is the default, say it was found.
99 return Default;
100 }
101 // Return true iff found tag matches supplied tag.
102 return Tag.equals(foundTag);
103}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000104
105void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000106 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000107 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000108 // CurrentNode can be null if the document is empty.
109 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000110 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000111 MN->ValidKeys.clear();
112 }
113}
114
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000115bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
116 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000117 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000118 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000119 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000120
121 // CurrentNode is null for empty documents, which is an error in case required
122 // nodes are present.
123 if (!CurrentNode) {
124 if (Required)
Rafael Espindolaed6882b2014-06-12 11:58:49 +0000125 EC = std::make_error_code(std::errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000126 return false;
127 }
128
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000129 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
130 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000131 setError(CurrentNode, "not a mapping");
132 return false;
133 }
134 MN->ValidKeys.push_back(Key);
135 HNode *Value = MN->Mapping[Key];
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000136 if (!Value) {
137 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000138 setError(CurrentNode, Twine("missing required key '") + Key + "'");
139 else
140 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000141 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000142 }
143 SaveInfo = CurrentNode;
144 CurrentNode = Value;
145 return true;
146}
147
148void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000149 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000150}
151
152void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000153 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000154 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000155 // CurrentNode can be null if the document is empty.
156 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000157 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000158 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000159 for (const auto &NN : MN->Mapping) {
160 if (!MN->isValidKey(NN.first())) {
161 setError(NN.second, Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000162 break;
163 }
164 }
165}
166
Nick Kledzikf60a9272012-12-12 20:46:15 +0000167unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000168 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000169 return SQ->Entries.size();
170 }
171 return 0;
172}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000173
Nick Kledzikf60a9272012-12-12 20:46:15 +0000174void Input::endSequence() {
175}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000176
Nick Kledzikf60a9272012-12-12 20:46:15 +0000177bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000178 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000179 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000180 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000181 SaveInfo = CurrentNode;
182 CurrentNode = SQ->Entries[Index];
183 return true;
184 }
185 return false;
186}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000187
Nick Kledzikf60a9272012-12-12 20:46:15 +0000188void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000189 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000190}
191
192unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000193 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000194 return SQ->Entries.size();
195 }
196 return 0;
197}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000198
Nick Kledzikf60a9272012-12-12 20:46:15 +0000199bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000200 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000201 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000202 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000203 SaveInfo = CurrentNode;
204 CurrentNode = SQ->Entries[index];
205 return true;
206 }
207 return false;
208}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000209
Nick Kledzikf60a9272012-12-12 20:46:15 +0000210void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000211 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000212}
213
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000214void Input::endFlowSequence() {
215}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000216
217void Input::beginEnumScalar() {
218 ScalarMatchFound = false;
219}
220
221bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000222 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000223 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000224 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
225 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226 ScalarMatchFound = true;
227 return true;
228 }
229 }
230 return false;
231}
232
233void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000234 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000235 setError(CurrentNode, "unknown enumerated scalar");
236 }
237}
238
Nick Kledzikf60a9272012-12-12 20:46:15 +0000239bool Input::beginBitSetScalar(bool &DoClear) {
240 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000241 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000242 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000243 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000244 setError(CurrentNode, "expected sequence of bit values");
245 }
246 DoClear = true;
247 return true;
248}
249
250bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000251 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000252 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000253 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000254 unsigned Index = 0;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000255 for (HNode *N : SQ->Entries) {
256 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N)) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000257 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000258 BitValuesUsed[Index] = true;
259 return true;
260 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000261 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000262 setError(CurrentNode, "unexpected scalar in sequence of bit values");
263 }
264 ++Index;
265 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000266 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000267 setError(CurrentNode, "expected sequence of bit values");
268 }
269 return false;
270}
271
272void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000273 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000274 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000275 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000276 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000277 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
278 if (!BitValuesUsed[i]) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000279 setError(SQ->Entries[i], "unknown bit value");
280 return;
281 }
282 }
283 }
284}
285
David Majnemer77880332014-04-10 07:37:33 +0000286void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000287 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000288 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000289 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000290 setError(CurrentNode, "unexpected scalar");
291 }
292}
293
294void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000295 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000296 this->setError(hnode->_node, message);
297}
298
299void Input::setError(Node *node, const Twine &message) {
300 Strm->printError(node, message);
Rafael Espindolaed6882b2014-06-12 11:58:49 +0000301 EC = std::make_error_code(std::errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000302}
303
304Input::HNode *Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000305 SmallString<128> StringStorage;
306 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000307 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000308 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000309 // Copy string to permanent storage
310 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000311 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000312 memcpy(Buf, &StringStorage[0], Len);
313 KeyStr = StringRef(Buf, Len);
314 }
Nick Kledzik0dcef842013-01-08 21:04:44 +0000315 return new ScalarHNode(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000316 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000317 SequenceHNode *SQHNode = new SequenceHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000318 for (Node &SN : *SQ) {
319 HNode *Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000320 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000321 break;
322 SQHNode->Entries.push_back(Entry);
323 }
324 return SQHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000325 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000326 MapHNode *mapHNode = new MapHNode(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000327 for (KeyValueNode &KVN : *Map) {
328 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
Nick Kledzikf60a9272012-12-12 20:46:15 +0000329 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000330 StringRef KeyStr = KeyScalar->getValue(StringStorage);
331 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000332 // Copy string to permanent storage
333 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000334 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000335 memcpy(Buf, &StringStorage[0], Len);
336 KeyStr = StringRef(Buf, Len);
337 }
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000338 HNode *ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000339 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000340 break;
341 mapHNode->Mapping[KeyStr] = ValueHNode;
342 }
343 return mapHNode;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000344 } else if (isa<NullNode>(N)) {
Nick Kledzik0dcef842013-01-08 21:04:44 +0000345 return new EmptyHNode(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000346 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000347 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000348 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000349 }
350}
351
Nick Kledzikf60a9272012-12-12 20:46:15 +0000352bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000353 for (const char *K : ValidKeys) {
354 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000355 return true;
356 }
357 return false;
358}
359
360void Input::setError(const Twine &Message) {
361 this->setError(CurrentNode, Message);
362}
363
Aaron Ballman0e63e532013-08-15 23:17:53 +0000364bool Input::canElideEmptySequence() {
365 return false;
366}
367
Nick Kledzik0dcef842013-01-08 21:04:44 +0000368Input::MapHNode::~MapHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000369 for (auto &N : Mapping)
370 delete N.second;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000371}
372
373Input::SequenceHNode::~SequenceHNode() {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000374 for (HNode *N : Entries)
375 delete N;
Nick Kledzik0dcef842013-01-08 21:04:44 +0000376}
377
378
379
Nick Kledzikf60a9272012-12-12 20:46:15 +0000380//===----------------------------------------------------------------------===//
381// Output
382//===----------------------------------------------------------------------===//
383
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000384Output::Output(raw_ostream &yout, void *context)
385 : IO(context),
386 Out(yout),
387 Column(0),
388 ColumnAtFlowStart(0),
389 NeedBitValueComma(false),
390 NeedFlowSequenceComma(false),
391 EnumerationMatchFound(false),
392 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000393}
394
395Output::~Output() {
396}
397
Nick Kledzik4761c602013-11-21 00:20:10 +0000398bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000399 return true;
400}
401
402void Output::beginMapping() {
403 StateStack.push_back(inMapFirstKey);
404 NeedsNewLine = true;
405}
406
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000407bool Output::mapTag(StringRef Tag, bool Use) {
408 if (Use) {
409 this->output(" ");
410 this->output(Tag);
411 }
412 return Use;
413}
414
Nick Kledzikf60a9272012-12-12 20:46:15 +0000415void Output::endMapping() {
416 StateStack.pop_back();
417}
418
Nick Kledzikf60a9272012-12-12 20:46:15 +0000419bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000420 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000421 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000422 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000423 this->newLineCheck();
424 this->paddedKey(Key);
425 return true;
426 }
427 return false;
428}
429
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000430void Output::postflightKey(void *) {
431 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000432 StateStack.pop_back();
433 StateStack.push_back(inMapOtherKey);
434 }
435}
436
437void Output::beginDocuments() {
438 this->outputUpToEndOfLine("---");
439}
440
441bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000442 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000443 this->outputUpToEndOfLine("\n---");
444 return true;
445}
446
447void Output::postflightDocument() {
448}
449
450void Output::endDocuments() {
451 output("\n...\n");
452}
453
454unsigned Output::beginSequence() {
455 StateStack.push_back(inSeq);
456 NeedsNewLine = true;
457 return 0;
458}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000459
Nick Kledzikf60a9272012-12-12 20:46:15 +0000460void Output::endSequence() {
461 StateStack.pop_back();
462}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000463
464bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000465 return true;
466}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000467
468void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000469}
470
471unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000472 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000473 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000474 ColumnAtFlowStart = Column;
475 output("[ ");
476 NeedFlowSequenceComma = false;
477 return 0;
478}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000479
Nick Kledzikf60a9272012-12-12 20:46:15 +0000480void Output::endFlowSequence() {
481 StateStack.pop_back();
482 this->outputUpToEndOfLine(" ]");
483}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000484
485bool Output::preflightFlowElement(unsigned, void *&) {
486 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000487 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000488 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000489 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000490 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000491 output(" ");
492 Column = ColumnAtFlowStart;
493 output(" ");
494 }
495 return true;
496}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000497
498void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000499 NeedFlowSequenceComma = true;
500}
501
Nick Kledzikf60a9272012-12-12 20:46:15 +0000502void Output::beginEnumScalar() {
503 EnumerationMatchFound = false;
504}
505
506bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000507 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000508 this->newLineCheck();
509 this->outputUpToEndOfLine(Str);
510 EnumerationMatchFound = true;
511 }
512 return false;
513}
514
515void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000516 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000517 llvm_unreachable("bad runtime enum value");
518}
519
Nick Kledzikf60a9272012-12-12 20:46:15 +0000520bool Output::beginBitSetScalar(bool &DoClear) {
521 this->newLineCheck();
522 output("[ ");
523 NeedBitValueComma = false;
524 DoClear = false;
525 return true;
526}
527
528bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000529 if (Matches) {
530 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000531 output(", ");
532 this->output(Str);
533 NeedBitValueComma = true;
534 }
535 return false;
536}
537
538void Output::endBitSetScalar() {
539 this->outputUpToEndOfLine(" ]");
540}
541
David Majnemer77880332014-04-10 07:37:33 +0000542void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000543 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000544 if (S.empty()) {
545 // Print '' for the empty string because leaving the field empty is not
546 // allowed.
547 this->outputUpToEndOfLine("''");
548 return;
549 }
David Majnemer77880332014-04-10 07:37:33 +0000550 if (!MustQuote) {
551 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000552 this->outputUpToEndOfLine(S);
553 return;
554 }
555 unsigned i = 0;
556 unsigned j = 0;
557 unsigned End = S.size();
558 output("'"); // Starting single quote.
559 const char *Base = S.data();
560 while (j < End) {
561 // Escape a single quote by doubling it.
562 if (S[j] == '\'') {
563 output(StringRef(&Base[i], j - i + 1));
564 output("'");
565 i = j + 1;
566 }
567 ++j;
568 }
569 output(StringRef(&Base[i], j - i));
570 this->outputUpToEndOfLine("'"); // Ending single quote.
571}
572
573void Output::setError(const Twine &message) {
574}
575
Aaron Ballman0e63e532013-08-15 23:17:53 +0000576bool Output::canElideEmptySequence() {
577 // Normally, with an optional key/value where the value is an empty sequence,
578 // the whole key/value can be not written. But, that produces wrong yaml
579 // if the key/value is the only thing in the map and the map is used in
580 // a sequence. This detects if the this sequence is the first key/value
581 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000582 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000583 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000584 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000585 return true;
586 return (StateStack[StateStack.size()-2] != inSeq);
587}
588
Nick Kledzikf60a9272012-12-12 20:46:15 +0000589void Output::output(StringRef s) {
590 Column += s.size();
591 Out << s;
592}
593
594void Output::outputUpToEndOfLine(StringRef s) {
595 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000596 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000597 NeedsNewLine = true;
598}
599
600void Output::outputNewLine() {
601 Out << "\n";
602 Column = 0;
603}
604
605// if seq at top, indent as if map, then add "- "
606// if seq in middle, use "- " if firstKey, else use " "
607//
608
609void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000610 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000611 return;
612 NeedsNewLine = false;
613
614 this->outputNewLine();
615
616 assert(StateStack.size() > 0);
617 unsigned Indent = StateStack.size() - 1;
618 bool OutputDash = false;
619
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000620 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000621 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000622 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
623 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000624 --Indent;
625 OutputDash = true;
626 }
627
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000628 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000629 output(" ");
630 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000631 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000632 output("- ");
633 }
634
635}
636
637void Output::paddedKey(StringRef key) {
638 output(key);
639 output(":");
640 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000641 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000642 output(&spaces[key.size()]);
643 else
644 output(" ");
645}
646
647//===----------------------------------------------------------------------===//
648// traits for built-in types
649//===----------------------------------------------------------------------===//
650
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000651void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
652 Out << (Val ? "true" : "false");
653}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000654
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000655StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
656 if (Scalar.equals("true")) {
657 Val = true;
658 return StringRef();
659 } else if (Scalar.equals("false")) {
660 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000661 return StringRef();
662 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000663 return "invalid boolean";
664}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000665
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000666void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
667 raw_ostream &Out) {
668 Out << Val;
669}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000670
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000671StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
672 StringRef &Val) {
673 Val = Scalar;
674 return StringRef();
675}
John Thompson48e018a2013-11-19 17:28:21 +0000676
677void ScalarTraits<std::string>::output(const std::string &Val, void *,
678 raw_ostream &Out) {
679 Out << Val;
680}
681
682StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
683 std::string &Val) {
684 Val = Scalar.str();
685 return StringRef();
686}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000687
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000688void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
689 raw_ostream &Out) {
690 // use temp uin32_t because ostream thinks uint8_t is a character
691 uint32_t Num = Val;
692 Out << Num;
693}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000694
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000695StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
696 unsigned long long n;
697 if (getAsUnsignedInteger(Scalar, 0, n))
698 return "invalid number";
699 if (n > 0xFF)
700 return "out of range number";
701 Val = n;
702 return StringRef();
703}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000704
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000705void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
706 raw_ostream &Out) {
707 Out << Val;
708}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000709
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000710StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
711 uint16_t &Val) {
712 unsigned long long n;
713 if (getAsUnsignedInteger(Scalar, 0, n))
714 return "invalid number";
715 if (n > 0xFFFF)
716 return "out of range number";
717 Val = n;
718 return StringRef();
719}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000720
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000721void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
722 raw_ostream &Out) {
723 Out << Val;
724}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000725
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000726StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
727 uint32_t &Val) {
728 unsigned long long n;
729 if (getAsUnsignedInteger(Scalar, 0, n))
730 return "invalid number";
731 if (n > 0xFFFFFFFFUL)
732 return "out of range number";
733 Val = n;
734 return StringRef();
735}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000736
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000737void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
738 raw_ostream &Out) {
739 Out << Val;
740}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000741
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000742StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
743 uint64_t &Val) {
744 unsigned long long N;
745 if (getAsUnsignedInteger(Scalar, 0, N))
746 return "invalid number";
747 Val = N;
748 return StringRef();
749}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000750
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000751void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
752 // use temp in32_t because ostream thinks int8_t is a character
753 int32_t Num = Val;
754 Out << Num;
755}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000756
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000757StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
758 long long N;
759 if (getAsSignedInteger(Scalar, 0, N))
760 return "invalid number";
761 if ((N > 127) || (N < -128))
762 return "out of range number";
763 Val = N;
764 return StringRef();
765}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000766
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000767void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
768 raw_ostream &Out) {
769 Out << Val;
770}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000771
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000772StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
773 long long N;
774 if (getAsSignedInteger(Scalar, 0, N))
775 return "invalid number";
776 if ((N > INT16_MAX) || (N < INT16_MIN))
777 return "out of range number";
778 Val = N;
779 return StringRef();
780}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000781
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000782void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
783 raw_ostream &Out) {
784 Out << Val;
785}
786
787StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
788 long long N;
789 if (getAsSignedInteger(Scalar, 0, N))
790 return "invalid number";
791 if ((N > INT32_MAX) || (N < INT32_MIN))
792 return "out of range number";
793 Val = N;
794 return StringRef();
795}
796
797void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
798 raw_ostream &Out) {
799 Out << Val;
800}
801
802StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
803 long long N;
804 if (getAsSignedInteger(Scalar, 0, N))
805 return "invalid number";
806 Val = N;
807 return StringRef();
808}
809
810void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000811 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000812}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000813
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000814StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
815 SmallString<32> buff(Scalar.begin(), Scalar.end());
816 char *end;
817 Val = strtod(buff.c_str(), &end);
818 if (*end != '\0')
819 return "invalid floating point number";
820 return StringRef();
821}
822
823void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000824 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000825}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000826
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000827StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
828 SmallString<32> buff(Scalar.begin(), Scalar.end());
829 char *end;
830 Val = strtod(buff.c_str(), &end);
831 if (*end != '\0')
832 return "invalid floating point number";
833 return StringRef();
834}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000835
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000836void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
837 uint8_t Num = Val;
838 Out << format("0x%02X", Num);
839}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000840
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000841StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
842 unsigned long long n;
843 if (getAsUnsignedInteger(Scalar, 0, n))
844 return "invalid hex8 number";
845 if (n > 0xFF)
846 return "out of range hex8 number";
847 Val = n;
848 return StringRef();
849}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000850
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000851void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
852 uint16_t Num = Val;
853 Out << format("0x%04X", Num);
854}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000855
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000856StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
857 unsigned long long n;
858 if (getAsUnsignedInteger(Scalar, 0, n))
859 return "invalid hex16 number";
860 if (n > 0xFFFF)
861 return "out of range hex16 number";
862 Val = n;
863 return StringRef();
864}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000865
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000866void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
867 uint32_t Num = Val;
868 Out << format("0x%08X", Num);
869}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000870
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000871StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
872 unsigned long long n;
873 if (getAsUnsignedInteger(Scalar, 0, n))
874 return "invalid hex32 number";
875 if (n > 0xFFFFFFFFUL)
876 return "out of range hex32 number";
877 Val = n;
878 return StringRef();
879}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000880
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000881void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
882 uint64_t Num = Val;
883 Out << format("0x%016llX", Num);
884}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000885
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000886StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
887 unsigned long long Num;
888 if (getAsUnsignedInteger(Scalar, 0, Num))
889 return "invalid hex64 number";
890 Val = Num;
891 return StringRef();
892}