MsgPackDocument.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //===-- MsgPackDocument.cpp - MsgPack Document --------------------------*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// This file implements a class that exposes a simple in-memory representation
  10. /// of a document of MsgPack objects, that can be read from MsgPack, written to
  11. /// MsgPack, and inspected and modified in memory. This is intended to be a
  12. /// lighter-weight (in terms of memory allocations) replacement for
  13. /// MsgPackTypes.
  14. ///
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/BinaryFormat/MsgPackDocument.h"
  17. #include "llvm/BinaryFormat/MsgPackWriter.h"
  18. using namespace llvm;
  19. using namespace msgpack;
  20. // Convert this DocNode into an empty array.
  21. void DocNode::convertToArray() { *this = getDocument()->getArrayNode(); }
  22. // Convert this DocNode into an empty map.
  23. void DocNode::convertToMap() { *this = getDocument()->getMapNode(); }
  24. /// Find the key in the MapDocNode.
  25. DocNode::MapTy::iterator MapDocNode::find(StringRef S) {
  26. return find(getDocument()->getNode(S));
  27. }
  28. /// Member access for MapDocNode. The string data must remain valid for the
  29. /// lifetime of the Document.
  30. DocNode &MapDocNode::operator[](StringRef S) {
  31. return (*this)[getDocument()->getNode(S)];
  32. }
  33. /// Member access for MapDocNode.
  34. DocNode &MapDocNode::operator[](DocNode Key) {
  35. assert(!Key.isEmpty());
  36. DocNode &N = (*Map)[Key];
  37. if (N.isEmpty()) {
  38. // Ensure a new element has its KindAndDoc initialized.
  39. N = getDocument()->getEmptyNode();
  40. }
  41. return N;
  42. }
  43. /// Member access for MapDocNode for integer key.
  44. DocNode &MapDocNode::operator[](int Key) {
  45. return (*this)[getDocument()->getNode(Key)];
  46. }
  47. DocNode &MapDocNode::operator[](unsigned Key) {
  48. return (*this)[getDocument()->getNode(Key)];
  49. }
  50. DocNode &MapDocNode::operator[](int64_t Key) {
  51. return (*this)[getDocument()->getNode(Key)];
  52. }
  53. DocNode &MapDocNode::operator[](uint64_t Key) {
  54. return (*this)[getDocument()->getNode(Key)];
  55. }
  56. /// Array element access. This extends the array if necessary.
  57. DocNode &ArrayDocNode::operator[](size_t Index) {
  58. if (size() <= Index) {
  59. // Ensure new elements have their KindAndDoc initialized.
  60. Array->resize(Index + 1, getDocument()->getEmptyNode());
  61. }
  62. return (*Array)[Index];
  63. }
  64. // Convenience assignment operators. This only works if the destination
  65. // DocNode has an associated Document, i.e. it was not constructed using the
  66. // default constructor. The string one does not copy, so the string must
  67. // remain valid for the lifetime of the Document. Use fromString to avoid
  68. // that restriction.
  69. DocNode &DocNode::operator=(StringRef Val) {
  70. *this = getDocument()->getNode(Val);
  71. return *this;
  72. }
  73. DocNode &DocNode::operator=(bool Val) {
  74. *this = getDocument()->getNode(Val);
  75. return *this;
  76. }
  77. DocNode &DocNode::operator=(int Val) {
  78. *this = getDocument()->getNode(Val);
  79. return *this;
  80. }
  81. DocNode &DocNode::operator=(unsigned Val) {
  82. *this = getDocument()->getNode(Val);
  83. return *this;
  84. }
  85. DocNode &DocNode::operator=(int64_t Val) {
  86. *this = getDocument()->getNode(Val);
  87. return *this;
  88. }
  89. DocNode &DocNode::operator=(uint64_t Val) {
  90. *this = getDocument()->getNode(Val);
  91. return *this;
  92. }
  93. // A level in the document reading stack.
  94. struct StackLevel {
  95. StackLevel(DocNode Node, size_t StartIndex, size_t Length,
  96. DocNode *MapEntry = nullptr)
  97. : Node(Node), Index(StartIndex), End(StartIndex + Length),
  98. MapEntry(MapEntry) {}
  99. DocNode Node;
  100. size_t Index;
  101. size_t End;
  102. // Points to map entry when we have just processed a map key.
  103. DocNode *MapEntry;
  104. DocNode MapKey;
  105. };
  106. // Read a document from a binary msgpack blob, merging into anything already in
  107. // the Document.
  108. // The blob data must remain valid for the lifetime of this Document (because a
  109. // string object in the document contains a StringRef into the original blob).
  110. // If Multi, then this sets root to an array and adds top-level objects to it.
  111. // If !Multi, then it only reads a single top-level object, even if there are
  112. // more, and sets root to that.
  113. // Returns false if failed due to illegal format or merge error.
  114. bool Document::readFromBlob(
  115. StringRef Blob, bool Multi,
  116. function_ref<int(DocNode *DestNode, DocNode SrcNode, DocNode MapKey)>
  117. Merger) {
  118. msgpack::Reader MPReader(Blob);
  119. SmallVector<StackLevel, 4> Stack;
  120. if (Multi) {
  121. // Create the array for multiple top-level objects.
  122. Root = getArrayNode();
  123. Stack.push_back(StackLevel(Root, 0, (size_t)-1));
  124. }
  125. do {
  126. // On to next element (or key if doing a map key next).
  127. // Read the value.
  128. Object Obj;
  129. if (!MPReader.read(Obj)) {
  130. if (Multi && Stack.size() == 1) {
  131. // OK to finish here as we've just done a top-level element with Multi
  132. break;
  133. }
  134. return false; // Finished too early
  135. }
  136. // Convert it into a DocNode.
  137. DocNode Node;
  138. switch (Obj.Kind) {
  139. case Type::Nil:
  140. Node = getNode();
  141. break;
  142. case Type::Int:
  143. Node = getNode(Obj.Int);
  144. break;
  145. case Type::UInt:
  146. Node = getNode(Obj.UInt);
  147. break;
  148. case Type::Boolean:
  149. Node = getNode(Obj.Bool);
  150. break;
  151. case Type::Float:
  152. Node = getNode(Obj.Float);
  153. break;
  154. case Type::String:
  155. Node = getNode(Obj.Raw);
  156. break;
  157. case Type::Map:
  158. Node = getMapNode();
  159. break;
  160. case Type::Array:
  161. Node = getArrayNode();
  162. break;
  163. default:
  164. return false; // Raw and Extension not supported
  165. }
  166. // Store it.
  167. DocNode *DestNode = nullptr;
  168. if (Stack.empty())
  169. DestNode = &Root;
  170. else if (Stack.back().Node.getKind() == Type::Array) {
  171. // Reading an array entry.
  172. auto &Array = Stack.back().Node.getArray();
  173. DestNode = &Array[Stack.back().Index++];
  174. } else {
  175. auto &Map = Stack.back().Node.getMap();
  176. if (!Stack.back().MapEntry) {
  177. // Reading a map key.
  178. Stack.back().MapKey = Node;
  179. Stack.back().MapEntry = &Map[Node];
  180. continue;
  181. }
  182. // Reading the value for the map key read in the last iteration.
  183. DestNode = Stack.back().MapEntry;
  184. Stack.back().MapEntry = nullptr;
  185. ++Stack.back().Index;
  186. }
  187. int MergeResult = 0;
  188. if (!DestNode->isEmpty()) {
  189. // In a merge, there is already a value at this position. Call the
  190. // callback to attempt to resolve the conflict. The resolution must result
  191. // in an array or map if Node is an array or map respectively.
  192. DocNode MapKey = !Stack.empty() && !Stack.back().MapKey.isEmpty()
  193. ? Stack.back().MapKey
  194. : getNode();
  195. MergeResult = Merger(DestNode, Node, MapKey);
  196. if (MergeResult < 0)
  197. return false; // Merge conflict resolution failed
  198. assert(!((Node.isMap() && !DestNode->isMap()) ||
  199. (Node.isArray() && !DestNode->isArray())));
  200. } else
  201. *DestNode = Node;
  202. // See if we're starting a new array or map.
  203. switch (DestNode->getKind()) {
  204. case msgpack::Type::Array:
  205. case msgpack::Type::Map:
  206. Stack.push_back(StackLevel(*DestNode, MergeResult, Obj.Length, nullptr));
  207. break;
  208. default:
  209. break;
  210. }
  211. // Pop finished stack levels.
  212. while (!Stack.empty()) {
  213. if (Stack.back().MapEntry)
  214. break;
  215. if (Stack.back().Index != Stack.back().End)
  216. break;
  217. Stack.pop_back();
  218. }
  219. } while (!Stack.empty());
  220. return true;
  221. }
  222. struct WriterStackLevel {
  223. DocNode Node;
  224. DocNode::MapTy::iterator MapIt;
  225. DocNode::ArrayTy::iterator ArrayIt;
  226. bool OnKey;
  227. };
  228. /// Write a MsgPack document to a binary MsgPack blob.
  229. void Document::writeToBlob(std::string &Blob) {
  230. Blob.clear();
  231. raw_string_ostream OS(Blob);
  232. msgpack::Writer MPWriter(OS);
  233. SmallVector<WriterStackLevel, 4> Stack;
  234. DocNode Node = getRoot();
  235. for (;;) {
  236. switch (Node.getKind()) {
  237. case Type::Array:
  238. MPWriter.writeArraySize(Node.getArray().size());
  239. Stack.push_back(
  240. {Node, DocNode::MapTy::iterator(), Node.getArray().begin(), false});
  241. break;
  242. case Type::Map:
  243. MPWriter.writeMapSize(Node.getMap().size());
  244. Stack.push_back(
  245. {Node, Node.getMap().begin(), DocNode::ArrayTy::iterator(), true});
  246. break;
  247. case Type::Nil:
  248. MPWriter.writeNil();
  249. break;
  250. case Type::Boolean:
  251. MPWriter.write(Node.getBool());
  252. break;
  253. case Type::Int:
  254. MPWriter.write(Node.getInt());
  255. break;
  256. case Type::UInt:
  257. MPWriter.write(Node.getUInt());
  258. break;
  259. case Type::String:
  260. MPWriter.write(Node.getString());
  261. break;
  262. case Type::Empty:
  263. llvm_unreachable("unhandled empty msgpack node");
  264. default:
  265. llvm_unreachable("unhandled msgpack object kind");
  266. }
  267. // Pop finished stack levels.
  268. while (!Stack.empty()) {
  269. if (Stack.back().Node.getKind() == Type::Map) {
  270. if (Stack.back().MapIt != Stack.back().Node.getMap().end())
  271. break;
  272. } else {
  273. if (Stack.back().ArrayIt != Stack.back().Node.getArray().end())
  274. break;
  275. }
  276. Stack.pop_back();
  277. }
  278. if (Stack.empty())
  279. break;
  280. // Get the next value.
  281. if (Stack.back().Node.getKind() == Type::Map) {
  282. if (Stack.back().OnKey) {
  283. // Do the key of a key,value pair in a map.
  284. Node = Stack.back().MapIt->first;
  285. Stack.back().OnKey = false;
  286. } else {
  287. Node = Stack.back().MapIt->second;
  288. ++Stack.back().MapIt;
  289. Stack.back().OnKey = true;
  290. }
  291. } else {
  292. Node = *Stack.back().ArrayIt;
  293. ++Stack.back().ArrayIt;
  294. }
  295. }
  296. }