2012-12-12 20:46:15 +00:00
|
|
|
//===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-23 18:07:13 +00:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-12 20:46:15 +00:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2015-03-23 18:07:13 +00:00
|
|
|
#include "llvm/Support/Errc.h"
|
2012-12-12 20:46:15 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-12 20:55:44 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2015-05-14 23:08:22 +00:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
2012-12-12 20:46:15 +00:00
|
|
|
#include "llvm/Support/YAMLParser.h"
|
2012-12-12 20:55:44 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-09-11 04:00:08 +00:00
|
|
|
#include <cctype>
|
2014-01-07 11:48:04 +00:00
|
|
|
#include <cstring>
|
2012-12-12 22:40:02 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace yaml;
|
2012-12-12 20:46:15 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IO
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
IO::IO(void *Context) : Ctxt(Context) {
|
|
|
|
}
|
|
|
|
|
|
|
|
IO::~IO() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void *IO::getContext() {
|
|
|
|
return Ctxt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IO::setContext(void *Context) {
|
|
|
|
Ctxt = Context;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Input
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-11-18 15:50:04 +00:00
|
|
|
Input::Input(StringRef InputContent,
|
|
|
|
void *Ctxt,
|
|
|
|
SourceMgr::DiagHandlerTy DiagHandler,
|
|
|
|
void *DiagHandlerCtxt)
|
2013-09-11 00:53:07 +00:00
|
|
|
: IO(Ctxt),
|
2013-01-08 21:04:44 +00:00
|
|
|
Strm(new Stream(InputContent, SrcMgr)),
|
2014-04-07 04:17:22 +00:00
|
|
|
CurrentNode(nullptr) {
|
2013-11-18 15:50:04 +00:00
|
|
|
if (DiagHandler)
|
|
|
|
SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
|
2012-12-12 20:46:15 +00:00
|
|
|
DocIterator = Strm->begin();
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:04:44 +00:00
|
|
|
Input::~Input() {
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code Input::error() { return EC; }
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2013-11-19 00:57:56 +00:00
|
|
|
// Pin the vtables to this file.
|
|
|
|
void Input::HNode::anchor() {}
|
|
|
|
void Input::EmptyHNode::anchor() {}
|
|
|
|
void Input::ScalarHNode::anchor() {}
|
2014-09-15 18:39:24 +00:00
|
|
|
void Input::MapHNode::anchor() {}
|
|
|
|
void Input::SequenceHNode::anchor() {}
|
2013-11-19 00:57:56 +00:00
|
|
|
|
2013-11-21 00:20:10 +00:00
|
|
|
bool Input::outputting() {
|
2012-12-12 20:46:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Input::setCurrentDocument() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (DocIterator != Strm->end()) {
|
2012-12-12 20:46:15 +00:00
|
|
|
Node *N = DocIterator->getRoot();
|
2013-11-18 15:50:04 +00:00
|
|
|
if (!N) {
|
2015-05-12 17:44:32 +00:00
|
|
|
assert(Strm->failed() && "Root is NULL iff parsing failed");
|
2014-06-13 17:20:48 +00:00
|
|
|
EC = make_error_code(errc::invalid_argument);
|
2013-11-18 15:50:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
if (isa<NullNode>(N)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
// Empty files are allowed and ignored
|
|
|
|
++DocIterator;
|
|
|
|
return setCurrentDocument();
|
|
|
|
}
|
2014-09-15 18:39:24 +00:00
|
|
|
TopNode = this->createHNodes(N);
|
2013-01-08 21:04:44 +00:00
|
|
|
CurrentNode = TopNode.get();
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-31 04:51:07 +00:00
|
|
|
bool Input::nextDocument() {
|
|
|
|
return ++DocIterator != Strm->end();
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
2013-11-14 07:08:49 +00:00
|
|
|
|
2015-05-27 18:02:19 +00:00
|
|
|
const Node *Input::getCurrentNode() const {
|
|
|
|
return CurrentNode ? CurrentNode->_node : nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-14 00:59:59 +00:00
|
|
|
bool Input::mapTag(StringRef Tag, bool Default) {
|
2013-11-14 07:08:56 +00:00
|
|
|
std::string foundTag = CurrentNode->_node->getVerbatimTag();
|
2013-11-14 00:59:59 +00:00
|
|
|
if (foundTag.empty()) {
|
|
|
|
// If no tag found and 'Tag' is the default, say it was found.
|
|
|
|
return Default;
|
|
|
|
}
|
2015-05-12 17:44:32 +00:00
|
|
|
// Return true iff found tag matches supplied tag.
|
2013-11-14 00:59:59 +00:00
|
|
|
return Tag.equals(foundTag);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
|
|
|
void Input::beginMapping() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return;
|
2013-11-18 15:50:04 +00:00
|
|
|
// CurrentNode can be null if the document is empty.
|
|
|
|
MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
|
2012-12-12 22:40:02 +00:00
|
|
|
if (MN) {
|
2012-12-12 20:46:15 +00:00
|
|
|
MN->ValidKeys.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
|
|
|
|
void *&SaveInfo) {
|
2012-12-12 20:46:15 +00:00
|
|
|
UseDefault = false;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return false;
|
2013-11-18 15:50:04 +00:00
|
|
|
|
|
|
|
// CurrentNode is null for empty documents, which is an error in case required
|
|
|
|
// nodes are present.
|
|
|
|
if (!CurrentNode) {
|
|
|
|
if (Required)
|
2014-06-13 17:20:48 +00:00
|
|
|
EC = make_error_code(errc::invalid_argument);
|
2013-11-18 15:50:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
|
|
|
|
if (!MN) {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, "not a mapping");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MN->ValidKeys.push_back(Key);
|
2014-09-15 18:39:24 +00:00
|
|
|
HNode *Value = MN->Mapping[Key].get();
|
2012-12-12 22:40:02 +00:00
|
|
|
if (!Value) {
|
|
|
|
if (Required)
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, Twine("missing required key '") + Key + "'");
|
|
|
|
else
|
|
|
|
UseDefault = true;
|
2012-12-12 22:40:02 +00:00
|
|
|
return false;
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
SaveInfo = CurrentNode;
|
|
|
|
CurrentNode = Value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::postflightKey(void *saveInfo) {
|
2012-12-12 22:40:02 +00:00
|
|
|
CurrentNode = reinterpret_cast<HNode *>(saveInfo);
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Input::endMapping() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return;
|
2013-11-18 15:50:04 +00:00
|
|
|
// CurrentNode can be null if the document is empty.
|
|
|
|
MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
|
2012-12-12 22:40:02 +00:00
|
|
|
if (!MN)
|
2012-12-12 20:46:15 +00:00
|
|
|
return;
|
2014-04-10 06:02:49 +00:00
|
|
|
for (const auto &NN : MN->Mapping) {
|
|
|
|
if (!MN->isValidKey(NN.first())) {
|
2014-09-15 18:39:24 +00:00
|
|
|
setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'");
|
2012-12-12 20:46:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 20:11:40 +00:00
|
|
|
void Input::beginFlowMapping() { beginMapping(); }
|
|
|
|
|
|
|
|
void Input::endFlowMapping() { endMapping(); }
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
unsigned Input::beginSequence() {
|
2015-03-02 17:26:43 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
|
2012-12-12 20:46:15 +00:00
|
|
|
return SQ->Entries.size();
|
2015-03-02 17:26:43 +00:00
|
|
|
if (isa<EmptyHNode>(CurrentNode))
|
|
|
|
return 0;
|
|
|
|
// Treat case where there's a scalar "null" value as an empty sequence.
|
|
|
|
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
|
|
|
|
if (isNull(SN->value()))
|
|
|
|
return 0;
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
2015-03-02 17:26:43 +00:00
|
|
|
// Any other type of HNode is an error.
|
|
|
|
setError(CurrentNode, "not a sequence");
|
2012-12-12 20:46:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Input::endSequence() {
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return false;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
SaveInfo = CurrentNode;
|
2014-09-15 18:39:24 +00:00
|
|
|
CurrentNode = SQ->Entries[Index].get();
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Input::postflightElement(void *SaveInfo) {
|
2012-12-12 22:40:02 +00:00
|
|
|
CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 17:26:43 +00:00
|
|
|
unsigned Input::beginFlowSequence() { return beginSequence(); }
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return false;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
SaveInfo = CurrentNode;
|
2014-09-15 18:39:24 +00:00
|
|
|
CurrentNode = SQ->Entries[index].get();
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Input::postflightFlowElement(void *SaveInfo) {
|
2012-12-12 22:40:02 +00:00
|
|
|
CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Input::endFlowSequence() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::beginEnumScalar() {
|
|
|
|
ScalarMatchFound = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Input::matchEnumScalar(const char *Str, bool) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (ScalarMatchFound)
|
2012-12-12 20:46:15 +00:00
|
|
|
return false;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
|
|
|
|
if (SN->value().equals(Str)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
ScalarMatchFound = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:57:50 +00:00
|
|
|
bool Input::matchEnumFallback() {
|
|
|
|
if (ScalarMatchFound)
|
|
|
|
return false;
|
|
|
|
ScalarMatchFound = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Input::endEnumScalar() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (!ScalarMatchFound) {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, "unknown enumerated scalar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Input::beginBitSetScalar(bool &DoClear) {
|
|
|
|
BitValuesUsed.clear();
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
BitValuesUsed.insert(BitValuesUsed.begin(), SQ->Entries.size(), false);
|
2012-12-12 22:40:02 +00:00
|
|
|
} else {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, "expected sequence of bit values");
|
|
|
|
}
|
|
|
|
DoClear = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Input::bitSetMatch(const char *Str, bool) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return false;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
unsigned Index = 0;
|
2014-09-15 18:39:24 +00:00
|
|
|
for (auto &N : SQ->Entries) {
|
|
|
|
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SN->value().equals(Str)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
BitValuesUsed[Index] = true;
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
} else {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, "unexpected scalar in sequence of bit values");
|
|
|
|
}
|
|
|
|
++Index;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
} else {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, "expected sequence of bit values");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::endBitSetScalar() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
return;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
assert(BitValuesUsed.size() == SQ->Entries.size());
|
2012-12-12 22:40:02 +00:00
|
|
|
for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
|
|
|
|
if (!BitValuesUsed[i]) {
|
2014-09-15 18:39:24 +00:00
|
|
|
setError(SQ->Entries[i].get(), "unknown bit value");
|
2012-12-12 20:46:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 07:37:33 +00:00
|
|
|
void Input::scalarString(StringRef &S, bool) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
S = SN->value();
|
2012-12-12 22:40:02 +00:00
|
|
|
} else {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(CurrentNode, "unexpected scalar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 23:08:22 +00:00
|
|
|
void Input::blockScalarString(StringRef &S) { scalarString(S, false); }
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Input::setError(HNode *hnode, const Twine &message) {
|
2013-11-18 15:50:04 +00:00
|
|
|
assert(hnode && "HNode must not be NULL");
|
2012-12-12 20:46:15 +00:00
|
|
|
this->setError(hnode->_node, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::setError(Node *node, const Twine &message) {
|
|
|
|
Strm->printError(node, message);
|
2014-06-13 17:20:48 +00:00
|
|
|
EC = make_error_code(errc::invalid_argument);
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:39:24 +00:00
|
|
|
std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
|
2012-12-12 22:40:02 +00:00
|
|
|
SmallString<128> StringStorage;
|
|
|
|
if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
StringRef KeyStr = SN->getValue(StringStorage);
|
2012-12-12 22:40:02 +00:00
|
|
|
if (!StringStorage.empty()) {
|
2012-12-12 20:46:15 +00:00
|
|
|
// Copy string to permanent storage
|
|
|
|
unsigned Len = StringStorage.size();
|
2013-01-08 21:04:44 +00:00
|
|
|
char *Buf = StringAllocator.Allocate<char>(Len);
|
2012-12-12 20:46:15 +00:00
|
|
|
memcpy(Buf, &StringStorage[0], Len);
|
|
|
|
KeyStr = StringRef(Buf, Len);
|
|
|
|
}
|
2014-09-15 18:39:24 +00:00
|
|
|
return llvm::make_unique<ScalarHNode>(N, KeyStr);
|
2015-05-14 23:08:22 +00:00
|
|
|
} else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
|
|
|
|
StringRef Value = BSN->getValue();
|
|
|
|
char *Buf = StringAllocator.Allocate<char>(Value.size());
|
|
|
|
memcpy(Buf, Value.data(), Value.size());
|
|
|
|
return llvm::make_unique<ScalarHNode>(N, StringRef(Buf, Value.size()));
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
|
2014-09-15 18:39:24 +00:00
|
|
|
auto SQHNode = llvm::make_unique<SequenceHNode>(N);
|
2014-04-10 06:02:49 +00:00
|
|
|
for (Node &SN : *SQ) {
|
2014-09-15 18:39:24 +00:00
|
|
|
auto Entry = this->createHNodes(&SN);
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
break;
|
2014-09-15 18:39:24 +00:00
|
|
|
SQHNode->Entries.push_back(std::move(Entry));
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
2014-09-15 18:39:24 +00:00
|
|
|
return std::move(SQHNode);
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
|
2014-09-15 18:39:24 +00:00
|
|
|
auto mapHNode = llvm::make_unique<MapHNode>(N);
|
2014-04-10 06:02:49 +00:00
|
|
|
for (KeyValueNode &KVN : *Map) {
|
2014-08-08 13:58:00 +00:00
|
|
|
Node *KeyNode = KVN.getKey();
|
|
|
|
ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
|
|
|
|
if (!KeyScalar) {
|
|
|
|
setError(KeyNode, "Map key must be a scalar");
|
|
|
|
break;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
StringStorage.clear();
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef KeyStr = KeyScalar->getValue(StringStorage);
|
|
|
|
if (!StringStorage.empty()) {
|
2012-12-12 20:46:15 +00:00
|
|
|
// Copy string to permanent storage
|
|
|
|
unsigned Len = StringStorage.size();
|
2013-01-08 21:04:44 +00:00
|
|
|
char *Buf = StringAllocator.Allocate<char>(Len);
|
2012-12-12 20:46:15 +00:00
|
|
|
memcpy(Buf, &StringStorage[0], Len);
|
|
|
|
KeyStr = StringRef(Buf, Len);
|
|
|
|
}
|
2014-09-15 18:39:24 +00:00
|
|
|
auto ValueHNode = this->createHNodes(KVN.getValue());
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
break;
|
2014-09-15 18:39:24 +00:00
|
|
|
mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
2014-09-15 18:39:24 +00:00
|
|
|
return std::move(mapHNode);
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (isa<NullNode>(N)) {
|
2014-09-15 18:39:24 +00:00
|
|
|
return llvm::make_unique<EmptyHNode>(N);
|
2012-12-12 22:40:02 +00:00
|
|
|
} else {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(N, "unknown node kind");
|
2014-04-07 04:17:22 +00:00
|
|
|
return nullptr;
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Input::MapHNode::isValidKey(StringRef Key) {
|
2014-04-10 06:02:49 +00:00
|
|
|
for (const char *K : ValidKeys) {
|
|
|
|
if (Key.equals(K))
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::setError(const Twine &Message) {
|
|
|
|
this->setError(CurrentNode, Message);
|
|
|
|
}
|
|
|
|
|
2013-08-15 23:17:53 +00:00
|
|
|
bool Input::canElideEmptySequence() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Output
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
Output::Output(raw_ostream &yout, void *context)
|
|
|
|
: IO(context),
|
|
|
|
Out(yout),
|
|
|
|
Column(0),
|
|
|
|
ColumnAtFlowStart(0),
|
2015-05-04 20:11:40 +00:00
|
|
|
ColumnAtMapFlowStart(0),
|
2012-12-12 22:40:02 +00:00
|
|
|
NeedBitValueComma(false),
|
|
|
|
NeedFlowSequenceComma(false),
|
|
|
|
EnumerationMatchFound(false),
|
|
|
|
NeedsNewLine(false) {
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Output::~Output() {
|
|
|
|
}
|
|
|
|
|
2013-11-21 00:20:10 +00:00
|
|
|
bool Output::outputting() {
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::beginMapping() {
|
|
|
|
StateStack.push_back(inMapFirstKey);
|
|
|
|
NeedsNewLine = true;
|
|
|
|
}
|
|
|
|
|
2013-11-14 00:59:59 +00:00
|
|
|
bool Output::mapTag(StringRef Tag, bool Use) {
|
|
|
|
if (Use) {
|
|
|
|
this->output(" ");
|
|
|
|
this->output(Tag);
|
|
|
|
}
|
|
|
|
return Use;
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::endMapping() {
|
|
|
|
StateStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
|
2012-12-12 22:40:02 +00:00
|
|
|
bool &UseDefault, void *&) {
|
2012-12-12 20:46:15 +00:00
|
|
|
UseDefault = false;
|
2012-12-12 22:40:02 +00:00
|
|
|
if (Required || !SameAsDefault) {
|
2015-05-04 20:11:40 +00:00
|
|
|
auto State = StateStack.back();
|
|
|
|
if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
|
|
|
|
flowKey(Key);
|
|
|
|
} else {
|
|
|
|
this->newLineCheck();
|
|
|
|
this->paddedKey(Key);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void Output::postflightKey(void *) {
|
|
|
|
if (StateStack.back() == inMapFirstKey) {
|
2012-12-12 20:46:15 +00:00
|
|
|
StateStack.pop_back();
|
|
|
|
StateStack.push_back(inMapOtherKey);
|
2015-05-04 20:11:40 +00:00
|
|
|
} else if (StateStack.back() == inFlowMapFirstKey) {
|
|
|
|
StateStack.pop_back();
|
|
|
|
StateStack.push_back(inFlowMapOtherKey);
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 20:11:40 +00:00
|
|
|
void Output::beginFlowMapping() {
|
|
|
|
StateStack.push_back(inFlowMapFirstKey);
|
|
|
|
this->newLineCheck();
|
|
|
|
ColumnAtMapFlowStart = Column;
|
|
|
|
output("{ ");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::endFlowMapping() {
|
|
|
|
StateStack.pop_back();
|
|
|
|
this->outputUpToEndOfLine(" }");
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::beginDocuments() {
|
|
|
|
this->outputUpToEndOfLine("---");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Output::preflightDocument(unsigned index) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (index > 0)
|
2012-12-12 20:46:15 +00:00
|
|
|
this->outputUpToEndOfLine("\n---");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::postflightDocument() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::endDocuments() {
|
|
|
|
output("\n...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Output::beginSequence() {
|
|
|
|
StateStack.push_back(inSeq);
|
|
|
|
NeedsNewLine = true;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::endSequence() {
|
|
|
|
StateStack.pop_back();
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
|
|
|
bool Output::preflightElement(unsigned, void *&) {
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
|
|
|
void Output::postflightElement(void *) {
|
2012-12-12 20:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Output::beginFlowSequence() {
|
|
|
|
StateStack.push_back(inFlowSeq);
|
2013-01-04 19:32:00 +00:00
|
|
|
this->newLineCheck();
|
2012-12-12 20:46:15 +00:00
|
|
|
ColumnAtFlowStart = Column;
|
|
|
|
output("[ ");
|
|
|
|
NeedFlowSequenceComma = false;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::endFlowSequence() {
|
|
|
|
StateStack.pop_back();
|
|
|
|
this->outputUpToEndOfLine(" ]");
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
|
|
|
bool Output::preflightFlowElement(unsigned, void *&) {
|
|
|
|
if (NeedFlowSequenceComma)
|
2012-12-12 20:46:15 +00:00
|
|
|
output(", ");
|
2012-12-12 22:40:02 +00:00
|
|
|
if (Column > 70) {
|
2012-12-12 20:46:15 +00:00
|
|
|
output("\n");
|
2012-12-12 22:40:02 +00:00
|
|
|
for (int i = 0; i < ColumnAtFlowStart; ++i)
|
2012-12-12 20:46:15 +00:00
|
|
|
output(" ");
|
|
|
|
Column = ColumnAtFlowStart;
|
|
|
|
output(" ");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
|
|
|
|
void Output::postflightFlowElement(void *) {
|
2012-12-12 20:46:15 +00:00
|
|
|
NeedFlowSequenceComma = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::beginEnumScalar() {
|
|
|
|
EnumerationMatchFound = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Output::matchEnumScalar(const char *Str, bool Match) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (Match && !EnumerationMatchFound) {
|
2012-12-12 20:46:15 +00:00
|
|
|
this->newLineCheck();
|
|
|
|
this->outputUpToEndOfLine(Str);
|
|
|
|
EnumerationMatchFound = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:57:50 +00:00
|
|
|
bool Output::matchEnumFallback() {
|
|
|
|
if (EnumerationMatchFound)
|
|
|
|
return false;
|
|
|
|
EnumerationMatchFound = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::endEnumScalar() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (!EnumerationMatchFound)
|
2012-12-12 20:46:15 +00:00
|
|
|
llvm_unreachable("bad runtime enum value");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Output::beginBitSetScalar(bool &DoClear) {
|
|
|
|
this->newLineCheck();
|
|
|
|
output("[ ");
|
|
|
|
NeedBitValueComma = false;
|
|
|
|
DoClear = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Output::bitSetMatch(const char *Str, bool Matches) {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (Matches) {
|
|
|
|
if (NeedBitValueComma)
|
2012-12-12 20:46:15 +00:00
|
|
|
output(", ");
|
|
|
|
this->output(Str);
|
|
|
|
NeedBitValueComma = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::endBitSetScalar() {
|
|
|
|
this->outputUpToEndOfLine(" ]");
|
|
|
|
}
|
|
|
|
|
2014-04-10 07:37:33 +00:00
|
|
|
void Output::scalarString(StringRef &S, bool MustQuote) {
|
2012-12-12 20:46:15 +00:00
|
|
|
this->newLineCheck();
|
2013-09-11 04:00:08 +00:00
|
|
|
if (S.empty()) {
|
|
|
|
// Print '' for the empty string because leaving the field empty is not
|
|
|
|
// allowed.
|
|
|
|
this->outputUpToEndOfLine("''");
|
|
|
|
return;
|
|
|
|
}
|
2014-04-10 07:37:33 +00:00
|
|
|
if (!MustQuote) {
|
|
|
|
// Only quote if we must.
|
2012-12-12 20:46:15 +00:00
|
|
|
this->outputUpToEndOfLine(S);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned j = 0;
|
|
|
|
unsigned End = S.size();
|
|
|
|
output("'"); // Starting single quote.
|
|
|
|
const char *Base = S.data();
|
|
|
|
while (j < End) {
|
|
|
|
// Escape a single quote by doubling it.
|
|
|
|
if (S[j] == '\'') {
|
|
|
|
output(StringRef(&Base[i], j - i + 1));
|
|
|
|
output("'");
|
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
output(StringRef(&Base[i], j - i));
|
|
|
|
this->outputUpToEndOfLine("'"); // Ending single quote.
|
|
|
|
}
|
|
|
|
|
2015-05-14 23:08:22 +00:00
|
|
|
void Output::blockScalarString(StringRef &S) {
|
|
|
|
if (!StateStack.empty())
|
|
|
|
newLineCheck();
|
|
|
|
output(" |");
|
|
|
|
outputNewLine();
|
|
|
|
|
|
|
|
unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
|
|
|
|
|
|
|
|
auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
|
|
|
|
for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
|
|
|
|
for (unsigned I = 0; I < Indent; ++I) {
|
|
|
|
output(" ");
|
|
|
|
}
|
|
|
|
output(*Lines);
|
|
|
|
outputNewLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::setError(const Twine &message) {
|
|
|
|
}
|
|
|
|
|
2013-08-15 23:17:53 +00:00
|
|
|
bool Output::canElideEmptySequence() {
|
|
|
|
// Normally, with an optional key/value where the value is an empty sequence,
|
|
|
|
// the whole key/value can be not written. But, that produces wrong yaml
|
|
|
|
// if the key/value is the only thing in the map and the map is used in
|
|
|
|
// a sequence. This detects if the this sequence is the first key/value
|
|
|
|
// in map that itself is embedded in a sequnce.
|
2013-09-11 00:53:07 +00:00
|
|
|
if (StateStack.size() < 2)
|
2013-08-15 23:17:53 +00:00
|
|
|
return true;
|
2013-09-11 00:53:07 +00:00
|
|
|
if (StateStack.back() != inMapFirstKey)
|
2013-08-15 23:17:53 +00:00
|
|
|
return true;
|
|
|
|
return (StateStack[StateStack.size()-2] != inSeq);
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
void Output::output(StringRef s) {
|
|
|
|
Column += s.size();
|
|
|
|
Out << s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::outputUpToEndOfLine(StringRef s) {
|
|
|
|
this->output(s);
|
2015-05-04 20:11:40 +00:00
|
|
|
if (StateStack.empty() || (StateStack.back() != inFlowSeq &&
|
|
|
|
StateStack.back() != inFlowMapFirstKey &&
|
|
|
|
StateStack.back() != inFlowMapOtherKey))
|
2012-12-12 20:46:15 +00:00
|
|
|
NeedsNewLine = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::outputNewLine() {
|
|
|
|
Out << "\n";
|
|
|
|
Column = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if seq at top, indent as if map, then add "- "
|
|
|
|
// if seq in middle, use "- " if firstKey, else use " "
|
|
|
|
//
|
|
|
|
|
|
|
|
void Output::newLineCheck() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (!NeedsNewLine)
|
2012-12-12 20:46:15 +00:00
|
|
|
return;
|
|
|
|
NeedsNewLine = false;
|
|
|
|
|
|
|
|
this->outputNewLine();
|
|
|
|
|
|
|
|
assert(StateStack.size() > 0);
|
|
|
|
unsigned Indent = StateStack.size() - 1;
|
|
|
|
bool OutputDash = false;
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
if (StateStack.back() == inSeq) {
|
2012-12-12 20:46:15 +00:00
|
|
|
OutputDash = true;
|
2015-05-01 18:34:25 +00:00
|
|
|
} else if ((StateStack.size() > 1) && ((StateStack.back() == inMapFirstKey) ||
|
2015-05-04 20:11:40 +00:00
|
|
|
(StateStack.back() == inFlowSeq) ||
|
|
|
|
(StateStack.back() == inFlowMapFirstKey)) &&
|
2012-12-12 22:40:02 +00:00
|
|
|
(StateStack[StateStack.size() - 2] == inSeq)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
--Indent;
|
|
|
|
OutputDash = true;
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
for (unsigned i = 0; i < Indent; ++i) {
|
2012-12-12 20:46:15 +00:00
|
|
|
output(" ");
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
if (OutputDash) {
|
2012-12-12 20:46:15 +00:00
|
|
|
output("- ");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::paddedKey(StringRef key) {
|
|
|
|
output(key);
|
|
|
|
output(":");
|
|
|
|
const char *spaces = " ";
|
2012-12-12 22:40:02 +00:00
|
|
|
if (key.size() < strlen(spaces))
|
2012-12-12 20:46:15 +00:00
|
|
|
output(&spaces[key.size()]);
|
|
|
|
else
|
|
|
|
output(" ");
|
|
|
|
}
|
|
|
|
|
2015-05-04 20:11:40 +00:00
|
|
|
void Output::flowKey(StringRef Key) {
|
|
|
|
if (StateStack.back() == inFlowMapOtherKey)
|
|
|
|
output(", ");
|
|
|
|
if (Column > 70) {
|
|
|
|
output("\n");
|
|
|
|
for (int I = 0; I < ColumnAtMapFlowStart; ++I)
|
|
|
|
output(" ");
|
|
|
|
Column = ColumnAtMapFlowStart;
|
|
|
|
output(" ");
|
|
|
|
}
|
|
|
|
output(Key);
|
|
|
|
output(": ");
|
|
|
|
}
|
|
|
|
|
2012-12-12 20:46:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// traits for built-in types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
|
|
|
|
Out << (Val ? "true" : "false");
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
|
|
|
|
if (Scalar.equals("true")) {
|
|
|
|
Val = true;
|
2012-12-12 20:46:15 +00:00
|
|
|
return StringRef();
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (Scalar.equals("false")) {
|
|
|
|
Val = false;
|
2012-12-12 20:46:15 +00:00
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
return "invalid boolean";
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
|
|
|
|
StringRef &Val) {
|
|
|
|
Val = Scalar;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2015-01-26 18:02:18 +00:00
|
|
|
|
2013-11-19 17:28:21 +00:00
|
|
|
void ScalarTraits<std::string>::output(const std::string &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
|
|
|
|
std::string &Val) {
|
|
|
|
Val = Scalar.str();
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
// use temp uin32_t because ostream thinks uint8_t is a character
|
|
|
|
uint32_t Num = Val;
|
|
|
|
Out << Num;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
|
|
|
|
unsigned long long n;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, n))
|
|
|
|
return "invalid number";
|
|
|
|
if (n > 0xFF)
|
|
|
|
return "out of range number";
|
|
|
|
Val = n;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
|
|
|
|
uint16_t &Val) {
|
|
|
|
unsigned long long n;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, n))
|
|
|
|
return "invalid number";
|
|
|
|
if (n > 0xFFFF)
|
|
|
|
return "out of range number";
|
|
|
|
Val = n;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
|
|
|
|
uint32_t &Val) {
|
|
|
|
unsigned long long n;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, n))
|
|
|
|
return "invalid number";
|
|
|
|
if (n > 0xFFFFFFFFUL)
|
|
|
|
return "out of range number";
|
|
|
|
Val = n;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
|
|
|
|
uint64_t &Val) {
|
|
|
|
unsigned long long N;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, N))
|
|
|
|
return "invalid number";
|
|
|
|
Val = N;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
|
|
|
|
// use temp in32_t because ostream thinks int8_t is a character
|
|
|
|
int32_t Num = Val;
|
|
|
|
Out << Num;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
|
|
|
|
long long N;
|
|
|
|
if (getAsSignedInteger(Scalar, 0, N))
|
|
|
|
return "invalid number";
|
|
|
|
if ((N > 127) || (N < -128))
|
|
|
|
return "out of range number";
|
|
|
|
Val = N;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
|
|
|
|
long long N;
|
|
|
|
if (getAsSignedInteger(Scalar, 0, N))
|
|
|
|
return "invalid number";
|
|
|
|
if ((N > INT16_MAX) || (N < INT16_MIN))
|
|
|
|
return "out of range number";
|
|
|
|
Val = N;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
|
|
|
|
long long N;
|
|
|
|
if (getAsSignedInteger(Scalar, 0, N))
|
|
|
|
return "invalid number";
|
|
|
|
if ((N > INT32_MAX) || (N < INT32_MIN))
|
|
|
|
return "out of range number";
|
|
|
|
Val = N;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
Out << Val;
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
|
|
|
|
long long N;
|
|
|
|
if (getAsSignedInteger(Scalar, 0, N))
|
|
|
|
return "invalid number";
|
|
|
|
Val = N;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
|
|
|
|
Out << format("%g", Val);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
|
|
|
|
SmallString<32> buff(Scalar.begin(), Scalar.end());
|
|
|
|
char *end;
|
|
|
|
Val = strtod(buff.c_str(), &end);
|
|
|
|
if (*end != '\0')
|
|
|
|
return "invalid floating point number";
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
|
|
|
|
Out << format("%g", Val);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
|
|
|
|
SmallString<32> buff(Scalar.begin(), Scalar.end());
|
|
|
|
char *end;
|
|
|
|
Val = strtod(buff.c_str(), &end);
|
|
|
|
if (*end != '\0')
|
|
|
|
return "invalid floating point number";
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
|
|
|
|
uint8_t Num = Val;
|
|
|
|
Out << format("0x%02X", Num);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
|
|
|
|
unsigned long long n;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, n))
|
|
|
|
return "invalid hex8 number";
|
|
|
|
if (n > 0xFF)
|
|
|
|
return "out of range hex8 number";
|
|
|
|
Val = n;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
|
|
|
|
uint16_t Num = Val;
|
|
|
|
Out << format("0x%04X", Num);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
|
|
|
|
unsigned long long n;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, n))
|
|
|
|
return "invalid hex16 number";
|
|
|
|
if (n > 0xFFFF)
|
|
|
|
return "out of range hex16 number";
|
|
|
|
Val = n;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
|
|
|
|
uint32_t Num = Val;
|
|
|
|
Out << format("0x%08X", Num);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
|
|
|
|
unsigned long long n;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, n))
|
|
|
|
return "invalid hex32 number";
|
|
|
|
if (n > 0xFFFFFFFFUL)
|
|
|
|
return "out of range hex32 number";
|
|
|
|
Val = n;
|
|
|
|
return StringRef();
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
|
|
|
|
uint64_t Num = Val;
|
|
|
|
Out << format("0x%016llX", Num);
|
|
|
|
}
|
2012-12-12 20:46:15 +00:00
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
|
|
|
|
unsigned long long Num;
|
|
|
|
if (getAsUnsignedInteger(Scalar, 0, Num))
|
|
|
|
return "invalid hex64 number";
|
|
|
|
Val = Num;
|
|
|
|
return StringRef();
|
|
|
|
}
|