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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-12 20:55:44 +00:00
|
|
|
#include "llvm/Support/Format.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)),
|
|
|
|
CurrentNode(NULL) {
|
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() {
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:40:02 +00:00
|
|
|
error_code Input::error() {
|
2012-12-12 20:46:15 +00:00
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
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() {}
|
|
|
|
|
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) {
|
|
|
|
assert(Strm->failed() && "Root is NULL iff parsing failed");
|
|
|
|
EC = make_error_code(errc::invalid_argument);
|
|
|
|
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();
|
|
|
|
}
|
2013-01-08 21:04:44 +00:00
|
|
|
TopNode.reset(this->createHNodes(N));
|
|
|
|
CurrentNode = TopNode.get();
|
2012-12-12 20:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::nextDocument() {
|
|
|
|
++DocIterator;
|
|
|
|
}
|
2013-11-14 07:08:49 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// Return true iff found tag matches supplied tag.
|
|
|
|
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)
|
|
|
|
EC = make_error_code(errc::invalid_argument);
|
|
|
|
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);
|
|
|
|
HNode *Value = MN->Mapping[Key];
|
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;
|
2012-12-12 22:40:02 +00:00
|
|
|
for (MapHNode::NameToNode::iterator i = MN->Mapping.begin(),
|
|
|
|
End = MN->Mapping.end(); i != End; ++i) {
|
2013-08-07 05:51:27 +00:00
|
|
|
if (!MN->isValidKey(i->first())) {
|
|
|
|
setError(i->second, Twine("unknown key '") + i->first() + "'");
|
2012-12-12 20:46:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Input::beginSequence() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
return SQ->Entries.size();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
CurrentNode = SQ->Entries[Index];
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Input::beginFlowSequence() {
|
2012-12-12 22:40:02 +00:00
|
|
|
if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
|
2012-12-12 20:46:15 +00:00
|
|
|
return SQ->Entries.size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
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;
|
|
|
|
CurrentNode = SQ->Entries[index];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-12-12 22:40:02 +00:00
|
|
|
for (std::vector<HNode *>::iterator i = SQ->Entries.begin(),
|
|
|
|
End = SQ->Entries.end(); i != End; ++i) {
|
|
|
|
if (ScalarHNode *SN = dyn_cast<ScalarHNode>(*i)) {
|
|
|
|
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]) {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(SQ->Entries[i], "unknown bit value");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Input::scalarString(StringRef &S) {
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
EC = make_error_code(errc::invalid_argument);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-01-08 21:04:44 +00:00
|
|
|
return new ScalarHNode(N, KeyStr);
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
|
2013-01-08 21:04:44 +00:00
|
|
|
SequenceHNode *SQHNode = new SequenceHNode(N);
|
2012-12-12 22:40:02 +00:00
|
|
|
for (SequenceNode::iterator i = SQ->begin(), End = SQ->end(); i != End;
|
|
|
|
++i) {
|
2012-12-12 20:46:15 +00:00
|
|
|
HNode *Entry = this->createHNodes(i);
|
2012-12-12 22:40:02 +00:00
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
break;
|
|
|
|
SQHNode->Entries.push_back(Entry);
|
|
|
|
}
|
|
|
|
return SQHNode;
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
|
2013-01-08 21:04:44 +00:00
|
|
|
MapHNode *mapHNode = new MapHNode(N);
|
2012-12-12 22:40:02 +00:00
|
|
|
for (MappingNode::iterator i = Map->begin(), End = Map->end(); i != End;
|
|
|
|
++i) {
|
|
|
|
ScalarNode *KeyScalar = dyn_cast<ScalarNode>(i->getKey());
|
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);
|
|
|
|
}
|
2012-12-12 22:40:02 +00:00
|
|
|
HNode *ValueHNode = this->createHNodes(i->getValue());
|
|
|
|
if (EC)
|
2012-12-12 20:46:15 +00:00
|
|
|
break;
|
|
|
|
mapHNode->Mapping[KeyStr] = ValueHNode;
|
|
|
|
}
|
|
|
|
return mapHNode;
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if (isa<NullNode>(N)) {
|
2013-01-08 21:04:44 +00:00
|
|
|
return new EmptyHNode(N);
|
2012-12-12 22:40:02 +00:00
|
|
|
} else {
|
2012-12-12 20:46:15 +00:00
|
|
|
setError(N, "unknown node kind");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Input::MapHNode::isValidKey(StringRef Key) {
|
2013-07-04 01:31:24 +00:00
|
|
|
for (SmallVectorImpl<const char *>::iterator i = ValidKeys.begin(),
|
2012-12-12 22:40:02 +00:00
|
|
|
End = ValidKeys.end(); i != End; ++i) {
|
|
|
|
if (Key.equals(*i))
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:04:44 +00:00
|
|
|
Input::MapHNode::~MapHNode() {
|
|
|
|
for (MapHNode::NameToNode::iterator i = Mapping.begin(), End = Mapping.end();
|
|
|
|
i != End; ++i) {
|
|
|
|
delete i->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Input::SequenceHNode::~SequenceHNode() {
|
|
|
|
for (std::vector<HNode*>::iterator i = Entries.begin(), End = Entries.end();
|
|
|
|
i != End; ++i) {
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
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) {
|
2012-12-12 20:46:15 +00:00
|
|
|
this->newLineCheck();
|
|
|
|
this->paddedKey(Key);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(" ]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::scalarString(StringRef &S) {
|
2013-09-11 04:00:08 +00:00
|
|
|
const char ScalarSafeChars[] = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-/^., \t";
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (S.find_first_not_of(ScalarSafeChars) == StringRef::npos &&
|
|
|
|
!isspace(S.front()) && !isspace(S.back())) {
|
|
|
|
// If the string consists only of safe characters, print it out without
|
|
|
|
// quotes.
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-12-22 00:15:13 +00:00
|
|
|
if (StateStack.empty() || StateStack.back() != inFlowSeq)
|
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;
|
2012-12-12 22:40:02 +00:00
|
|
|
} else if ((StateStack.size() > 1) && (StateStack.back() == inMapFirstKey) &&
|
|
|
|
(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(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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();
|
|
|
|
}
|
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();
|
|
|
|
}
|