#include "llvm/BinaryFormat/MsgPackDocument.h"
#include "llvm/BinaryFormat/MsgPackWriter.h"
using namespace llvm;
using namespace msgpack;
void DocNode::convertToArray() { *this = getDocument()->getArrayNode(); }
void DocNode::convertToMap() { *this = getDocument()->getMapNode(); }
DocNode::MapTy::iterator MapDocNode::find(StringRef S) {
return find(getDocument()->getNode(S));
}
DocNode &MapDocNode::operator[](StringRef S) {
return (*this)[getDocument()->getNode(S)];
}
DocNode &MapDocNode::operator[](DocNode Key) {
assert(!Key.isEmpty());
DocNode &N = (*Map)[Key];
if (N.isEmpty()) {
N = getDocument()->getEmptyNode();
}
return N;
}
DocNode &MapDocNode::operator[](int Key) {
return (*this)[getDocument()->getNode(Key)];
}
DocNode &MapDocNode::operator[](unsigned Key) {
return (*this)[getDocument()->getNode(Key)];
}
DocNode &MapDocNode::operator[](int64_t Key) {
return (*this)[getDocument()->getNode(Key)];
}
DocNode &MapDocNode::operator[](uint64_t Key) {
return (*this)[getDocument()->getNode(Key)];
}
DocNode &ArrayDocNode::operator[](size_t Index) {
if (size() <= Index) {
Array->resize(Index + 1, getDocument()->getEmptyNode());
}
return (*Array)[Index];
}
DocNode &DocNode::operator=(StringRef Val) {
*this = getDocument()->getNode(Val);
return *this;
}
DocNode &DocNode::operator=(bool Val) {
*this = getDocument()->getNode(Val);
return *this;
}
DocNode &DocNode::operator=(int Val) {
*this = getDocument()->getNode(Val);
return *this;
}
DocNode &DocNode::operator=(unsigned Val) {
*this = getDocument()->getNode(Val);
return *this;
}
DocNode &DocNode::operator=(int64_t Val) {
*this = getDocument()->getNode(Val);
return *this;
}
DocNode &DocNode::operator=(uint64_t Val) {
*this = getDocument()->getNode(Val);
return *this;
}
struct StackLevel {
StackLevel(DocNode Node, size_t StartIndex, size_t Length,
DocNode *MapEntry = nullptr)
: Node(Node), Index(StartIndex), End(StartIndex + Length),
MapEntry(MapEntry) {}
DocNode Node;
size_t Index;
size_t End;
DocNode *MapEntry;
DocNode MapKey;
};
bool Document::readFromBlob(
StringRef Blob, bool Multi,
function_ref<int(DocNode *DestNode, DocNode SrcNode, DocNode MapKey)>
Merger) {
msgpack::Reader MPReader(Blob);
SmallVector<StackLevel, 4> Stack;
if (Multi) {
Root = getArrayNode();
Stack.push_back(StackLevel(Root, 0, (size_t)-1));
}
do {
Object Obj;
if (!MPReader.read(Obj)) {
if (Multi && Stack.size() == 1) {
break;
}
return false; }
DocNode Node;
switch (Obj.Kind) {
case Type::Nil:
Node = getNode();
break;
case Type::Int:
Node = getNode(Obj.Int);
break;
case Type::UInt:
Node = getNode(Obj.UInt);
break;
case Type::Boolean:
Node = getNode(Obj.Bool);
break;
case Type::Float:
Node = getNode(Obj.Float);
break;
case Type::String:
Node = getNode(Obj.Raw);
break;
case Type::Map:
Node = getMapNode();
break;
case Type::Array:
Node = getArrayNode();
break;
default:
return false; }
DocNode *DestNode = nullptr;
if (Stack.empty())
DestNode = &Root;
else if (Stack.back().Node.getKind() == Type::Array) {
auto &Array = Stack.back().Node.getArray();
DestNode = &Array[Stack.back().Index++];
} else {
auto &Map = Stack.back().Node.getMap();
if (!Stack.back().MapEntry) {
Stack.back().MapKey = Node;
Stack.back().MapEntry = &Map[Node];
continue;
}
DestNode = Stack.back().MapEntry;
Stack.back().MapEntry = nullptr;
++Stack.back().Index;
}
int MergeResult = 0;
if (!DestNode->isEmpty()) {
DocNode MapKey = !Stack.empty() && !Stack.back().MapKey.isEmpty()
? Stack.back().MapKey
: getNode();
MergeResult = Merger(DestNode, Node, MapKey);
if (MergeResult < 0)
return false; assert(!((Node.isMap() && !DestNode->isMap()) ||
(Node.isArray() && !DestNode->isArray())));
} else
*DestNode = Node;
switch (DestNode->getKind()) {
case msgpack::Type::Array:
case msgpack::Type::Map:
Stack.push_back(StackLevel(*DestNode, MergeResult, Obj.Length, nullptr));
break;
default:
break;
}
while (!Stack.empty()) {
if (Stack.back().MapEntry)
break;
if (Stack.back().Index != Stack.back().End)
break;
Stack.pop_back();
}
} while (!Stack.empty());
return true;
}
struct WriterStackLevel {
DocNode Node;
DocNode::MapTy::iterator MapIt;
DocNode::ArrayTy::iterator ArrayIt;
bool OnKey;
};
void Document::writeToBlob(std::string &Blob) {
Blob.clear();
raw_string_ostream OS(Blob);
msgpack::Writer MPWriter(OS);
SmallVector<WriterStackLevel, 4> Stack;
DocNode Node = getRoot();
for (;;) {
switch (Node.getKind()) {
case Type::Array:
MPWriter.writeArraySize(Node.getArray().size());
Stack.push_back(
{Node, DocNode::MapTy::iterator(), Node.getArray().begin(), false});
break;
case Type::Map:
MPWriter.writeMapSize(Node.getMap().size());
Stack.push_back(
{Node, Node.getMap().begin(), DocNode::ArrayTy::iterator(), true});
break;
case Type::Nil:
MPWriter.writeNil();
break;
case Type::Boolean:
MPWriter.write(Node.getBool());
break;
case Type::Int:
MPWriter.write(Node.getInt());
break;
case Type::UInt:
MPWriter.write(Node.getUInt());
break;
case Type::String:
MPWriter.write(Node.getString());
break;
case Type::Empty:
llvm_unreachable("unhandled empty msgpack node");
default:
llvm_unreachable("unhandled msgpack object kind");
}
while (!Stack.empty()) {
if (Stack.back().Node.getKind() == Type::Map) {
if (Stack.back().MapIt != Stack.back().Node.getMap().end())
break;
} else {
if (Stack.back().ArrayIt != Stack.back().Node.getArray().end())
break;
}
Stack.pop_back();
}
if (Stack.empty())
break;
if (Stack.back().Node.getKind() == Type::Map) {
if (Stack.back().OnKey) {
Node = Stack.back().MapIt->first;
Stack.back().OnKey = false;
} else {
Node = Stack.back().MapIt->second;
++Stack.back().MapIt;
Stack.back().OnKey = true;
}
} else {
Node = *Stack.back().ArrayIt;
++Stack.back().ArrayIt;
}
}
}