123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #pragma once
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-parameter"
- #endif
- #ifndef LLVM_BINARYFORMAT_MSGPACKDOCUMENT_H
- #define LLVM_BINARYFORMAT_MSGPACKDOCUMENT_H
- #include "llvm/BinaryFormat/MsgPackReader.h"
- #include <map>
- namespace llvm {
- namespace msgpack {
- class ArrayDocNode;
- class Document;
- class MapDocNode;
- struct KindAndDocument {
- Document *Doc;
- Type Kind;
- };
- class DocNode {
- friend Document;
- public:
- typedef std::map<DocNode, DocNode> MapTy;
- typedef std::vector<DocNode> ArrayTy;
- private:
-
-
-
-
- const KindAndDocument *KindAndDoc;
- protected:
-
- union {
- int64_t Int;
- uint64_t UInt;
- bool Bool;
- double Float;
- StringRef Raw;
- ArrayTy *Array;
- MapTy *Map;
- };
- public:
-
-
- DocNode() : KindAndDoc(nullptr) {}
-
- bool isMap() const { return getKind() == Type::Map; }
- bool isArray() const { return getKind() == Type::Array; }
- bool isScalar() const { return !isMap() && !isArray(); }
- bool isString() const { return getKind() == Type::String; }
-
-
-
- bool isEmpty() const { return !KindAndDoc || getKind() == Type::Empty; }
- Type getKind() const { return KindAndDoc->Kind; }
- Document *getDocument() const { return KindAndDoc->Doc; }
- int64_t &getInt() {
- assert(getKind() == Type::Int);
- return Int;
- }
- uint64_t &getUInt() {
- assert(getKind() == Type::UInt);
- return UInt;
- }
- bool &getBool() {
- assert(getKind() == Type::Boolean);
- return Bool;
- }
- double &getFloat() {
- assert(getKind() == Type::Float);
- return Float;
- }
- int64_t getInt() const {
- assert(getKind() == Type::Int);
- return Int;
- }
- uint64_t getUInt() const {
- assert(getKind() == Type::UInt);
- return UInt;
- }
- bool getBool() const {
- assert(getKind() == Type::Boolean);
- return Bool;
- }
- double getFloat() const {
- assert(getKind() == Type::Float);
- return Float;
- }
- StringRef getString() const {
- assert(getKind() == Type::String);
- return Raw;
- }
-
-
- ArrayDocNode &getArray(bool Convert = false) {
- if (getKind() != Type::Array) {
- assert(Convert);
- convertToArray();
- }
-
- return *reinterpret_cast<ArrayDocNode *>(this);
- }
-
-
- MapDocNode &getMap(bool Convert = false) {
- if (getKind() != Type::Map) {
- assert(Convert);
- convertToMap();
- }
-
- return *reinterpret_cast<MapDocNode *>(this);
- }
-
- friend bool operator<(const DocNode &Lhs, const DocNode &Rhs) {
-
-
- if (Rhs.isEmpty())
- return false;
- if (Lhs.KindAndDoc != Rhs.KindAndDoc) {
- if (Lhs.isEmpty())
- return true;
- return (unsigned)Lhs.getKind() < (unsigned)Rhs.getKind();
- }
- switch (Lhs.getKind()) {
- case Type::Int:
- return Lhs.Int < Rhs.Int;
- case Type::UInt:
- return Lhs.UInt < Rhs.UInt;
- case Type::Nil:
- return false;
- case Type::Boolean:
- return Lhs.Bool < Rhs.Bool;
- case Type::Float:
- return Lhs.Float < Rhs.Float;
- case Type::String:
- case Type::Binary:
- return Lhs.Raw < Rhs.Raw;
- default:
- llvm_unreachable("bad map key type");
- }
- }
-
- friend bool operator==(const DocNode &Lhs, const DocNode &Rhs) {
- return !(Lhs < Rhs) && !(Rhs < Lhs);
- }
-
- friend bool operator!=(const DocNode &Lhs, const DocNode &Rhs) {
- return !(Lhs == Rhs);
- }
-
- std::string toString() const;
-
-
-
- StringRef fromString(StringRef S, StringRef Tag = "");
-
-
-
-
-
- DocNode &operator=(const char *Val) { return *this = StringRef(Val); }
- DocNode &operator=(StringRef Val);
- DocNode &operator=(bool Val);
- DocNode &operator=(int Val);
- DocNode &operator=(unsigned Val);
- DocNode &operator=(int64_t Val);
- DocNode &operator=(uint64_t Val);
- private:
-
- DocNode(const KindAndDocument *KindAndDoc) : KindAndDoc(KindAndDoc) {}
- void convertToArray();
- void convertToMap();
- };
- class MapDocNode : public DocNode {
- public:
- MapDocNode() = default;
- MapDocNode(DocNode &N) : DocNode(N) { assert(getKind() == Type::Map); }
-
- size_t size() const { return Map->size(); }
- bool empty() const { return !size(); }
- MapTy::iterator begin() { return Map->begin(); }
- MapTy::iterator end() { return Map->end(); }
- MapTy::iterator find(DocNode Key) { return Map->find(Key); }
- MapTy::iterator find(StringRef Key);
- MapTy::iterator erase(MapTy::const_iterator I) { return Map->erase(I); }
- size_t erase(DocNode Key) { return Map->erase(Key); }
- MapTy::iterator erase(MapTy::const_iterator First,
- MapTy::const_iterator Second) {
- return Map->erase(First, Second);
- }
-
-
- DocNode &operator[](StringRef S);
-
- DocNode &operator[](DocNode Key);
- DocNode &operator[](int Key);
- DocNode &operator[](unsigned Key);
- DocNode &operator[](int64_t Key);
- DocNode &operator[](uint64_t Key);
- };
- class ArrayDocNode : public DocNode {
- public:
- ArrayDocNode() = default;
- ArrayDocNode(DocNode &N) : DocNode(N) { assert(getKind() == Type::Array); }
-
- size_t size() const { return Array->size(); }
- bool empty() const { return !size(); }
- DocNode &back() const { return Array->back(); }
- ArrayTy::iterator begin() { return Array->begin(); }
- ArrayTy::iterator end() { return Array->end(); }
- void push_back(DocNode N) {
- assert(N.isEmpty() || N.getDocument() == getDocument());
- Array->push_back(N);
- }
-
- DocNode &operator[](size_t Index);
- };
- class Document {
-
-
- std::vector<std::unique_ptr<DocNode::MapTy>> Maps;
- std::vector<std::unique_ptr<DocNode::ArrayTy>> Arrays;
- std::vector<std::unique_ptr<char[]>> Strings;
-
- DocNode Root;
-
- KindAndDocument KindAndDocs[size_t(Type::Empty) + 1];
-
- bool HexMode = false;
- public:
- Document() {
- clear();
- for (unsigned T = 0; T != unsigned(Type::Empty) + 1; ++T)
- KindAndDocs[T] = {this, Type(T)};
- }
-
- DocNode &getRoot() { return Root; }
-
- void clear() { getRoot() = getEmptyNode(); }
-
- DocNode getEmptyNode() {
- auto N = DocNode(&KindAndDocs[size_t(Type::Empty)]);
- return N;
- }
-
- DocNode getNode() {
- auto N = DocNode(&KindAndDocs[size_t(Type::Nil)]);
- return N;
- }
-
- DocNode getNode(int64_t V) {
- auto N = DocNode(&KindAndDocs[size_t(Type::Int)]);
- N.Int = V;
- return N;
- }
-
- DocNode getNode(int V) {
- auto N = DocNode(&KindAndDocs[size_t(Type::Int)]);
- N.Int = V;
- return N;
- }
-
- DocNode getNode(uint64_t V) {
- auto N = DocNode(&KindAndDocs[size_t(Type::UInt)]);
- N.UInt = V;
- return N;
- }
-
- DocNode getNode(unsigned V) {
- auto N = DocNode(&KindAndDocs[size_t(Type::UInt)]);
- N.UInt = V;
- return N;
- }
-
- DocNode getNode(bool V) {
- auto N = DocNode(&KindAndDocs[size_t(Type::Boolean)]);
- N.Bool = V;
- return N;
- }
-
- DocNode getNode(double V) {
- auto N = DocNode(&KindAndDocs[size_t(Type::Float)]);
- N.Float = V;
- return N;
- }
-
-
- DocNode getNode(StringRef V, bool Copy = false) {
- if (Copy)
- V = addString(V);
- auto N = DocNode(&KindAndDocs[size_t(Type::String)]);
- N.Raw = V;
- return N;
- }
-
-
- DocNode getNode(const char *V, bool Copy = false) {
- return getNode(StringRef(V), Copy);
- }
-
- MapDocNode getMapNode() {
- auto N = DocNode(&KindAndDocs[size_t(Type::Map)]);
- Maps.push_back(std::unique_ptr<DocNode::MapTy>(new DocNode::MapTy));
- N.Map = Maps.back().get();
- return N.getMap();
- }
-
- ArrayDocNode getArrayNode() {
- auto N = DocNode(&KindAndDocs[size_t(Type::Array)]);
- Arrays.push_back(std::unique_ptr<DocNode::ArrayTy>(new DocNode::ArrayTy));
- N.Array = Arrays.back().get();
- return N.getArray();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool readFromBlob(
- StringRef Blob, bool Multi,
- function_ref<int(DocNode *DestNode, DocNode SrcNode, DocNode MapKey)>
- Merger = [](DocNode *DestNode, DocNode SrcNode, DocNode MapKey) {
- return -1;
- });
-
- void writeToBlob(std::string &Blob);
-
-
- StringRef addString(StringRef S) {
- Strings.push_back(std::unique_ptr<char[]>(new char[S.size()]));
- memcpy(&Strings.back()[0], S.data(), S.size());
- return StringRef(&Strings.back()[0], S.size());
- }
-
- void setHexMode(bool Val = true) { HexMode = Val; }
-
- bool getHexMode() const { return HexMode; }
-
- void toYAML(raw_ostream &OS);
-
- bool fromYAML(StringRef S);
- };
- }
- }
- #endif
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
|