blob: 81edca2c0b5f7fcd0f9d69b35c1e54619ad9dabe [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() {}
David Blaikied759fe52014-09-15 18:39:24 +000066void Input::MapHNode::anchor() {}
67void Input::SequenceHNode::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000068
Nick Kledzik4761c602013-11-21 00:20:10 +000069bool Input::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +000070 return false;
71}
72
73bool Input::setCurrentDocument() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +000074 if (DocIterator != Strm->end()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000075 Node *N = DocIterator->getRoot();
Alexander Kornienko681e37c2013-11-18 15:50:04 +000076 if (!N) {
77 assert(Strm->failed() && "Root is NULL iff parsing failed");
Rafael Espindola2a826e42014-06-13 17:20:48 +000078 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +000079 return false;
80 }
81
Benjamin Kramer36b0f122012-12-12 22:40:02 +000082 if (isa<NullNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +000083 // Empty files are allowed and ignored
84 ++DocIterator;
85 return setCurrentDocument();
86 }
David Blaikied759fe52014-09-15 18:39:24 +000087 TopNode = this->createHNodes(N);
Nick Kledzik0dcef842013-01-08 21:04:44 +000088 CurrentNode = TopNode.get();
Nick Kledzikf60a9272012-12-12 20:46:15 +000089 return true;
90 }
91 return false;
92}
93
Simon Atanasyanf97af8a2014-05-31 04:51:07 +000094bool Input::nextDocument() {
95 return ++DocIterator != Strm->end();
Nick Kledzikf60a9272012-12-12 20:46:15 +000096}
NAKAMURA Takumi9439c522013-11-14 07:08:49 +000097
Nick Kledzik1e6033c2013-11-14 00:59:59 +000098bool Input::mapTag(StringRef Tag, bool Default) {
NAKAMURA Takumi5b94d282013-11-14 07:08:56 +000099 std::string foundTag = CurrentNode->_node->getVerbatimTag();
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000100 if (foundTag.empty()) {
101 // If no tag found and 'Tag' is the default, say it was found.
102 return Default;
103 }
104 // Return true iff found tag matches supplied tag.
105 return Tag.equals(foundTag);
106}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000107
108void Input::beginMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000109 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000110 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000111 // CurrentNode can be null if the document is empty.
112 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000113 if (MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000114 MN->ValidKeys.clear();
115 }
116}
117
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000118bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
119 void *&SaveInfo) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000120 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000121 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000122 return false;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000123
124 // CurrentNode is null for empty documents, which is an error in case required
125 // nodes are present.
126 if (!CurrentNode) {
127 if (Required)
Rafael Espindola2a826e42014-06-13 17:20:48 +0000128 EC = make_error_code(errc::invalid_argument);
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000129 return false;
130 }
131
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000132 MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
133 if (!MN) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000134 setError(CurrentNode, "not a mapping");
135 return false;
136 }
137 MN->ValidKeys.push_back(Key);
David Blaikied759fe52014-09-15 18:39:24 +0000138 HNode *Value = MN->Mapping[Key].get();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000139 if (!Value) {
140 if (Required)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000141 setError(CurrentNode, Twine("missing required key '") + Key + "'");
142 else
143 UseDefault = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000144 return false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000145 }
146 SaveInfo = CurrentNode;
147 CurrentNode = Value;
148 return true;
149}
150
151void Input::postflightKey(void *saveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000152 CurrentNode = reinterpret_cast<HNode *>(saveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000153}
154
155void Input::endMapping() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000156 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000157 return;
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000158 // CurrentNode can be null if the document is empty.
159 MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000160 if (!MN)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000161 return;
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000162 for (const auto &NN : MN->Mapping) {
163 if (!MN->isValidKey(NN.first())) {
David Blaikied759fe52014-09-15 18:39:24 +0000164 setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000165 break;
166 }
167 }
168}
169
Nick Kledzikf60a9272012-12-12 20:46:15 +0000170unsigned Input::beginSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000171 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000172 return SQ->Entries.size();
173 }
174 return 0;
175}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000176
Nick Kledzikf60a9272012-12-12 20:46:15 +0000177void Input::endSequence() {
178}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000179
Nick Kledzikf60a9272012-12-12 20:46:15 +0000180bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000181 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000182 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000183 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000184 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000185 CurrentNode = SQ->Entries[Index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000186 return true;
187 }
188 return false;
189}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000190
Nick Kledzikf60a9272012-12-12 20:46:15 +0000191void Input::postflightElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000192 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000193}
194
195unsigned Input::beginFlowSequence() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000196 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000197 return SQ->Entries.size();
198 }
199 return 0;
200}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000201
Nick Kledzikf60a9272012-12-12 20:46:15 +0000202bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000203 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000204 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000205 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000206 SaveInfo = CurrentNode;
David Blaikied759fe52014-09-15 18:39:24 +0000207 CurrentNode = SQ->Entries[index].get();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000208 return true;
209 }
210 return false;
211}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000212
Nick Kledzikf60a9272012-12-12 20:46:15 +0000213void Input::postflightFlowElement(void *SaveInfo) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000214 CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000215}
216
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000217void Input::endFlowSequence() {
218}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000219
220void Input::beginEnumScalar() {
221 ScalarMatchFound = false;
222}
223
224bool Input::matchEnumScalar(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000225 if (ScalarMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000226 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000227 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
228 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000229 ScalarMatchFound = true;
230 return true;
231 }
232 }
233 return false;
234}
235
236void Input::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000237 if (!ScalarMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000238 setError(CurrentNode, "unknown enumerated scalar");
239 }
240}
241
Nick Kledzikf60a9272012-12-12 20:46:15 +0000242bool Input::beginBitSetScalar(bool &DoClear) {
243 BitValuesUsed.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000244 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000245 BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000246 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000247 setError(CurrentNode, "expected sequence of bit values");
248 }
249 DoClear = true;
250 return true;
251}
252
253bool Input::bitSetMatch(const char *Str, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000254 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000255 return false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000256 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000257 unsigned Index = 0;
David Blaikied759fe52014-09-15 18:39:24 +0000258 for (auto &N : SQ->Entries) {
259 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000260 if (SN->value().equals(Str)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000261 BitValuesUsed[Index] = true;
262 return true;
263 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000264 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000265 setError(CurrentNode, "unexpected scalar in sequence of bit values");
266 }
267 ++Index;
268 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000269 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000270 setError(CurrentNode, "expected sequence of bit values");
271 }
272 return false;
273}
274
275void Input::endBitSetScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000276 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000277 return;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000278 if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000279 assert(BitValuesUsed.size() == SQ->Entries.size());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000280 for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
281 if (!BitValuesUsed[i]) {
David Blaikied759fe52014-09-15 18:39:24 +0000282 setError(SQ->Entries[i].get(), "unknown bit value");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000283 return;
284 }
285 }
286 }
287}
288
David Majnemer77880332014-04-10 07:37:33 +0000289void Input::scalarString(StringRef &S, bool) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000290 if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000291 S = SN->value();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000292 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000293 setError(CurrentNode, "unexpected scalar");
294 }
295}
296
297void Input::setError(HNode *hnode, const Twine &message) {
Alexander Kornienko681e37c2013-11-18 15:50:04 +0000298 assert(hnode && "HNode must not be NULL");
Nick Kledzikf60a9272012-12-12 20:46:15 +0000299 this->setError(hnode->_node, message);
300}
301
302void Input::setError(Node *node, const Twine &message) {
303 Strm->printError(node, message);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000304 EC = make_error_code(errc::invalid_argument);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000305}
306
David Blaikied759fe52014-09-15 18:39:24 +0000307std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000308 SmallString<128> StringStorage;
309 if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000310 StringRef KeyStr = SN->getValue(StringStorage);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000311 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000312 // Copy string to permanent storage
313 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000314 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000315 memcpy(Buf, &StringStorage[0], Len);
316 KeyStr = StringRef(Buf, Len);
317 }
David Blaikied759fe52014-09-15 18:39:24 +0000318 return llvm::make_unique<ScalarHNode>(N, KeyStr);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000319 } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000320 auto SQHNode = llvm::make_unique<SequenceHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000321 for (Node &SN : *SQ) {
David Blaikied759fe52014-09-15 18:39:24 +0000322 auto Entry = this->createHNodes(&SN);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000323 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000324 break;
David Blaikied759fe52014-09-15 18:39:24 +0000325 SQHNode->Entries.push_back(std::move(Entry));
Nick Kledzikf60a9272012-12-12 20:46:15 +0000326 }
David Blaikied759fe52014-09-15 18:39:24 +0000327 return std::move(SQHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000328 } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000329 auto mapHNode = llvm::make_unique<MapHNode>(N);
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000330 for (KeyValueNode &KVN : *Map) {
Rafael Espindolaa97373f2014-08-08 13:58:00 +0000331 Node *KeyNode = KVN.getKey();
332 ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
333 if (!KeyScalar) {
334 setError(KeyNode, "Map key must be a scalar");
335 break;
336 }
Nick Kledzikf60a9272012-12-12 20:46:15 +0000337 StringStorage.clear();
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000338 StringRef KeyStr = KeyScalar->getValue(StringStorage);
339 if (!StringStorage.empty()) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000340 // Copy string to permanent storage
341 unsigned Len = StringStorage.size();
Nick Kledzik0dcef842013-01-08 21:04:44 +0000342 char *Buf = StringAllocator.Allocate<char>(Len);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000343 memcpy(Buf, &StringStorage[0], Len);
344 KeyStr = StringRef(Buf, Len);
345 }
David Blaikied759fe52014-09-15 18:39:24 +0000346 auto ValueHNode = this->createHNodes(KVN.getValue());
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000347 if (EC)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000348 break;
David Blaikied759fe52014-09-15 18:39:24 +0000349 mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
Nick Kledzikf60a9272012-12-12 20:46:15 +0000350 }
David Blaikied759fe52014-09-15 18:39:24 +0000351 return std::move(mapHNode);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000352 } else if (isa<NullNode>(N)) {
David Blaikied759fe52014-09-15 18:39:24 +0000353 return llvm::make_unique<EmptyHNode>(N);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000354 } else {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000355 setError(N, "unknown node kind");
Craig Topperc10719f2014-04-07 04:17:22 +0000356 return nullptr;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000357 }
358}
359
Nick Kledzikf60a9272012-12-12 20:46:15 +0000360bool Input::MapHNode::isValidKey(StringRef Key) {
Simon Atanasyan878bd8a2014-04-10 06:02:49 +0000361 for (const char *K : ValidKeys) {
362 if (Key.equals(K))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000363 return true;
364 }
365 return false;
366}
367
368void Input::setError(const Twine &Message) {
369 this->setError(CurrentNode, Message);
370}
371
Aaron Ballman0e63e532013-08-15 23:17:53 +0000372bool Input::canElideEmptySequence() {
373 return false;
374}
375
Nick Kledzikf60a9272012-12-12 20:46:15 +0000376//===----------------------------------------------------------------------===//
377// Output
378//===----------------------------------------------------------------------===//
379
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000380Output::Output(raw_ostream &yout, void *context)
381 : IO(context),
382 Out(yout),
383 Column(0),
384 ColumnAtFlowStart(0),
385 NeedBitValueComma(false),
386 NeedFlowSequenceComma(false),
387 EnumerationMatchFound(false),
388 NeedsNewLine(false) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000389}
390
391Output::~Output() {
392}
393
Nick Kledzik4761c602013-11-21 00:20:10 +0000394bool Output::outputting() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000395 return true;
396}
397
398void Output::beginMapping() {
399 StateStack.push_back(inMapFirstKey);
400 NeedsNewLine = true;
401}
402
Nick Kledzik1e6033c2013-11-14 00:59:59 +0000403bool Output::mapTag(StringRef Tag, bool Use) {
404 if (Use) {
405 this->output(" ");
406 this->output(Tag);
407 }
408 return Use;
409}
410
Nick Kledzikf60a9272012-12-12 20:46:15 +0000411void Output::endMapping() {
412 StateStack.pop_back();
413}
414
Nick Kledzikf60a9272012-12-12 20:46:15 +0000415bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000416 bool &UseDefault, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000417 UseDefault = false;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000418 if (Required || !SameAsDefault) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000419 this->newLineCheck();
420 this->paddedKey(Key);
421 return true;
422 }
423 return false;
424}
425
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000426void Output::postflightKey(void *) {
427 if (StateStack.back() == inMapFirstKey) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000428 StateStack.pop_back();
429 StateStack.push_back(inMapOtherKey);
430 }
431}
432
433void Output::beginDocuments() {
434 this->outputUpToEndOfLine("---");
435}
436
437bool Output::preflightDocument(unsigned index) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000438 if (index > 0)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000439 this->outputUpToEndOfLine("\n---");
440 return true;
441}
442
443void Output::postflightDocument() {
444}
445
446void Output::endDocuments() {
447 output("\n...\n");
448}
449
450unsigned Output::beginSequence() {
451 StateStack.push_back(inSeq);
452 NeedsNewLine = true;
453 return 0;
454}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000455
Nick Kledzikf60a9272012-12-12 20:46:15 +0000456void Output::endSequence() {
457 StateStack.pop_back();
458}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000459
460bool Output::preflightElement(unsigned, void *&) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000461 return true;
462}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000463
464void Output::postflightElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000465}
466
467unsigned Output::beginFlowSequence() {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000468 StateStack.push_back(inFlowSeq);
Nick Kledzik11964f22013-01-04 19:32:00 +0000469 this->newLineCheck();
Nick Kledzikf60a9272012-12-12 20:46:15 +0000470 ColumnAtFlowStart = Column;
471 output("[ ");
472 NeedFlowSequenceComma = false;
473 return 0;
474}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000475
Nick Kledzikf60a9272012-12-12 20:46:15 +0000476void Output::endFlowSequence() {
477 StateStack.pop_back();
478 this->outputUpToEndOfLine(" ]");
479}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000480
481bool Output::preflightFlowElement(unsigned, void *&) {
482 if (NeedFlowSequenceComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000483 output(", ");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000484 if (Column > 70) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000485 output("\n");
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000486 for (int i = 0; i < ColumnAtFlowStart; ++i)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000487 output(" ");
488 Column = ColumnAtFlowStart;
489 output(" ");
490 }
491 return true;
492}
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000493
494void Output::postflightFlowElement(void *) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000495 NeedFlowSequenceComma = true;
496}
497
Nick Kledzikf60a9272012-12-12 20:46:15 +0000498void Output::beginEnumScalar() {
499 EnumerationMatchFound = false;
500}
501
502bool Output::matchEnumScalar(const char *Str, bool Match) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000503 if (Match && !EnumerationMatchFound) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000504 this->newLineCheck();
505 this->outputUpToEndOfLine(Str);
506 EnumerationMatchFound = true;
507 }
508 return false;
509}
510
511void Output::endEnumScalar() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000512 if (!EnumerationMatchFound)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000513 llvm_unreachable("bad runtime enum value");
514}
515
Nick Kledzikf60a9272012-12-12 20:46:15 +0000516bool Output::beginBitSetScalar(bool &DoClear) {
517 this->newLineCheck();
518 output("[ ");
519 NeedBitValueComma = false;
520 DoClear = false;
521 return true;
522}
523
524bool Output::bitSetMatch(const char *Str, bool Matches) {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000525 if (Matches) {
526 if (NeedBitValueComma)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000527 output(", ");
528 this->output(Str);
529 NeedBitValueComma = true;
530 }
531 return false;
532}
533
534void Output::endBitSetScalar() {
535 this->outputUpToEndOfLine(" ]");
536}
537
David Majnemer77880332014-04-10 07:37:33 +0000538void Output::scalarString(StringRef &S, bool MustQuote) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000539 this->newLineCheck();
Rui Ueyama106eded2013-09-11 04:00:08 +0000540 if (S.empty()) {
541 // Print '' for the empty string because leaving the field empty is not
542 // allowed.
543 this->outputUpToEndOfLine("''");
544 return;
545 }
David Majnemer77880332014-04-10 07:37:33 +0000546 if (!MustQuote) {
547 // Only quote if we must.
Nick Kledzikf60a9272012-12-12 20:46:15 +0000548 this->outputUpToEndOfLine(S);
549 return;
550 }
551 unsigned i = 0;
552 unsigned j = 0;
553 unsigned End = S.size();
554 output("'"); // Starting single quote.
555 const char *Base = S.data();
556 while (j < End) {
557 // Escape a single quote by doubling it.
558 if (S[j] == '\'') {
559 output(StringRef(&Base[i], j - i + 1));
560 output("'");
561 i = j + 1;
562 }
563 ++j;
564 }
565 output(StringRef(&Base[i], j - i));
566 this->outputUpToEndOfLine("'"); // Ending single quote.
567}
568
569void Output::setError(const Twine &message) {
570}
571
Aaron Ballman0e63e532013-08-15 23:17:53 +0000572bool Output::canElideEmptySequence() {
573 // Normally, with an optional key/value where the value is an empty sequence,
574 // the whole key/value can be not written. But, that produces wrong yaml
575 // if the key/value is the only thing in the map and the map is used in
576 // a sequence. This detects if the this sequence is the first key/value
577 // in map that itself is embedded in a sequnce.
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000578 if (StateStack.size() < 2)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000579 return true;
Rui Ueyama38dfffa2013-09-11 00:53:07 +0000580 if (StateStack.back() != inMapFirstKey)
Aaron Ballman0e63e532013-08-15 23:17:53 +0000581 return true;
582 return (StateStack[StateStack.size()-2] != inSeq);
583}
584
Nick Kledzikf60a9272012-12-12 20:46:15 +0000585void Output::output(StringRef s) {
586 Column += s.size();
587 Out << s;
588}
589
590void Output::outputUpToEndOfLine(StringRef s) {
591 this->output(s);
Richard Smith045e4f12012-12-22 00:15:13 +0000592 if (StateStack.empty() || StateStack.back() != inFlowSeq)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000593 NeedsNewLine = true;
594}
595
596void Output::outputNewLine() {
597 Out << "\n";
598 Column = 0;
599}
600
601// if seq at top, indent as if map, then add "- "
602// if seq in middle, use "- " if firstKey, else use " "
603//
604
605void Output::newLineCheck() {
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000606 if (!NeedsNewLine)
Nick Kledzikf60a9272012-12-12 20:46:15 +0000607 return;
608 NeedsNewLine = false;
609
610 this->outputNewLine();
611
612 assert(StateStack.size() > 0);
613 unsigned Indent = StateStack.size() - 1;
614 bool OutputDash = false;
615
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000616 if (StateStack.back() == inSeq) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000617 OutputDash = true;
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000618 } else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
619 (StateStack[StateStack.size() - 2] == inSeq)) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000620 --Indent;
621 OutputDash = true;
622 }
623
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000624 for (unsigned i = 0; i < Indent; ++i) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000625 output(" ");
626 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000627 if (OutputDash) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000628 output("- ");
629 }
630
631}
632
633void Output::paddedKey(StringRef key) {
634 output(key);
635 output(":");
636 const char *spaces = " ";
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000637 if (key.size() < strlen(spaces))
Nick Kledzikf60a9272012-12-12 20:46:15 +0000638 output(&spaces[key.size()]);
639 else
640 output(" ");
641}
642
643//===----------------------------------------------------------------------===//
644// traits for built-in types
645//===----------------------------------------------------------------------===//
646
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000647void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
648 Out << (Val ? "true" : "false");
649}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000650
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000651StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
652 if (Scalar.equals("true")) {
653 Val = true;
654 return StringRef();
655 } else if (Scalar.equals("false")) {
656 Val = false;
Nick Kledzikf60a9272012-12-12 20:46:15 +0000657 return StringRef();
658 }
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000659 return "invalid boolean";
660}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000661
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000662void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
663 raw_ostream &Out) {
664 Out << Val;
665}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000666
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000667StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
668 StringRef &Val) {
669 Val = Scalar;
670 return StringRef();
671}
John Thompson48e018a2013-11-19 17:28:21 +0000672
673void ScalarTraits<std::string>::output(const std::string &Val, void *,
674 raw_ostream &Out) {
675 Out << Val;
676}
677
678StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
679 std::string &Val) {
680 Val = Scalar.str();
681 return StringRef();
682}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000683
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000684void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
685 raw_ostream &Out) {
686 // use temp uin32_t because ostream thinks uint8_t is a character
687 uint32_t Num = Val;
688 Out << Num;
689}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000690
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000691StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
692 unsigned long long n;
693 if (getAsUnsignedInteger(Scalar, 0, n))
694 return "invalid number";
695 if (n > 0xFF)
696 return "out of range number";
697 Val = n;
698 return StringRef();
699}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000700
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000701void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
702 raw_ostream &Out) {
703 Out << Val;
704}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000705
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000706StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
707 uint16_t &Val) {
708 unsigned long long n;
709 if (getAsUnsignedInteger(Scalar, 0, n))
710 return "invalid number";
711 if (n > 0xFFFF)
712 return "out of range number";
713 Val = n;
714 return StringRef();
715}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000716
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000717void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
718 raw_ostream &Out) {
719 Out << Val;
720}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000721
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000722StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
723 uint32_t &Val) {
724 unsigned long long n;
725 if (getAsUnsignedInteger(Scalar, 0, n))
726 return "invalid number";
727 if (n > 0xFFFFFFFFUL)
728 return "out of range number";
729 Val = n;
730 return StringRef();
731}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000732
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000733void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
734 raw_ostream &Out) {
735 Out << Val;
736}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000737
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000738StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
739 uint64_t &Val) {
740 unsigned long long N;
741 if (getAsUnsignedInteger(Scalar, 0, N))
742 return "invalid number";
743 Val = N;
744 return StringRef();
745}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000746
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000747void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
748 // use temp in32_t because ostream thinks int8_t is a character
749 int32_t Num = Val;
750 Out << Num;
751}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000752
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000753StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
754 long long N;
755 if (getAsSignedInteger(Scalar, 0, N))
756 return "invalid number";
757 if ((N > 127) || (N < -128))
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<int16_t>::output(const int16_t &Val, void *,
764 raw_ostream &Out) {
765 Out << Val;
766}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000767
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000768StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
769 long long N;
770 if (getAsSignedInteger(Scalar, 0, N))
771 return "invalid number";
772 if ((N > INT16_MAX) || (N < INT16_MIN))
773 return "out of range number";
774 Val = N;
775 return StringRef();
776}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000777
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000778void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
779 raw_ostream &Out) {
780 Out << Val;
781}
782
783StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
784 long long N;
785 if (getAsSignedInteger(Scalar, 0, N))
786 return "invalid number";
787 if ((N > INT32_MAX) || (N < INT32_MIN))
788 return "out of range number";
789 Val = N;
790 return StringRef();
791}
792
793void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
794 raw_ostream &Out) {
795 Out << Val;
796}
797
798StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
799 long long N;
800 if (getAsSignedInteger(Scalar, 0, N))
801 return "invalid number";
802 Val = N;
803 return StringRef();
804}
805
806void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000807 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000808}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000809
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000810StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
811 SmallString<32> buff(Scalar.begin(), Scalar.end());
812 char *end;
813 Val = strtod(buff.c_str(), &end);
814 if (*end != '\0')
815 return "invalid floating point number";
816 return StringRef();
817}
818
819void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
Nick Kledzikf60a9272012-12-12 20:46:15 +0000820 Out << format("%g", Val);
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000821}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000822
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000823StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
824 SmallString<32> buff(Scalar.begin(), Scalar.end());
825 char *end;
826 Val = strtod(buff.c_str(), &end);
827 if (*end != '\0')
828 return "invalid floating point number";
829 return StringRef();
830}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000831
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000832void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
833 uint8_t Num = Val;
834 Out << format("0x%02X", Num);
835}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000836
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000837StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
838 unsigned long long n;
839 if (getAsUnsignedInteger(Scalar, 0, n))
840 return "invalid hex8 number";
841 if (n > 0xFF)
842 return "out of range hex8 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<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
848 uint16_t Num = Val;
849 Out << format("0x%04X", Num);
850}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000851
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000852StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
853 unsigned long long n;
854 if (getAsUnsignedInteger(Scalar, 0, n))
855 return "invalid hex16 number";
856 if (n > 0xFFFF)
857 return "out of range hex16 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<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
863 uint32_t Num = Val;
864 Out << format("0x%08X", Num);
865}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000866
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000867StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
868 unsigned long long n;
869 if (getAsUnsignedInteger(Scalar, 0, n))
870 return "invalid hex32 number";
871 if (n > 0xFFFFFFFFUL)
872 return "out of range hex32 number";
873 Val = n;
874 return StringRef();
875}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000876
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000877void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
878 uint64_t Num = Val;
879 Out << format("0x%016llX", Num);
880}
Nick Kledzikf60a9272012-12-12 20:46:15 +0000881
Benjamin Kramer36b0f122012-12-12 22:40:02 +0000882StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
883 unsigned long long Num;
884 if (getAsUnsignedInteger(Scalar, 0, Num))
885 return "invalid hex64 number";
886 Val = Num;
887 return StringRef();
888}