LLVM  10.0.0svn
MsgPackDocument.cpp
Go to the documentation of this file.
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 
19 
20 using namespace llvm;
21 using namespace msgpack;
22 
23 // Convert this DocNode into an empty array.
24 void DocNode::convertToArray() { *this = getDocument()->getArrayNode(); }
25 
26 // Convert this DocNode into an empty map.
27 void DocNode::convertToMap() { *this = getDocument()->getMapNode(); }
28 
29 /// Find the key in the MapDocNode.
30 DocNode::MapTy::iterator MapDocNode::find(StringRef S) {
31  return find(getDocument()->getNode(S));
32 }
33 
34 /// Member access for MapDocNode. The string data must remain valid for the
35 /// lifetime of the Document.
37  return (*this)[getDocument()->getNode(S)];
38 }
39 
40 /// Member access for MapDocNode.
42  assert(!Key.isEmpty());
43  MapTy::value_type Entry(Key, DocNode());
44  auto ItAndInserted = Map->insert(Entry);
45  if (ItAndInserted.second) {
46  // Ensure a new element has its KindAndDoc initialized.
47  ItAndInserted.first->second = getDocument()->getNode();
48  }
49  return ItAndInserted.first->second;
50 }
51 
52 /// Array element access. This extends the array if necessary.
54  if (size() <= Index) {
55  // Ensure new elements have their KindAndDoc initialized.
56  Array->resize(Index + 1, getDocument()->getNode());
57  }
58  return (*Array)[Index];
59 }
60 
61 // A level in the document reading stack.
62 struct StackLevel {
64  size_t Length;
65  // Points to map entry when we have just processed a map key.
67 };
68 
69 // Read a document from a binary msgpack blob.
70 // The blob data must remain valid for the lifetime of this Document (because a
71 // string object in the document contains a StringRef into the original blob).
72 // If Multi, then this sets root to an array and adds top-level objects to it.
73 // If !Multi, then it only reads a single top-level object, even if there are
74 // more, and sets root to that.
75 // Returns false if failed due to illegal format.
76 bool Document::readFromBlob(StringRef Blob, bool Multi) {
77  msgpack::Reader MPReader(Blob);
79  if (Multi) {
80  // Create the array for multiple top-level objects.
81  Root = getArrayNode();
82  Stack.push_back(StackLevel({Root, (size_t)-1, nullptr}));
83  }
84  do {
85  // On to next element (or key if doing a map key next).
86  // Read the value.
87  Object Obj;
88  if (!MPReader.read(Obj)) {
89  if (Multi && Stack.size() == 1) {
90  // OK to finish here as we've just done a top-level element with Multi
91  break;
92  }
93  return false; // Finished too early
94  }
95  // Convert it into a DocNode.
96  DocNode Node;
97  switch (Obj.Kind) {
98  case Type::Nil:
99  Node = getNode();
100  break;
101  case Type::Int:
102  Node = getNode(Obj.Int);
103  break;
104  case Type::UInt:
105  Node = getNode(Obj.UInt);
106  break;
107  case Type::Boolean:
108  Node = getNode(Obj.Bool);
109  break;
110  case Type::Float:
111  Node = getNode(Obj.Float);
112  break;
113  case Type::String:
114  Node = getNode(Obj.Raw);
115  break;
116  case Type::Map:
117  Node = getMapNode();
118  break;
119  case Type::Array:
120  Node = getArrayNode();
121  break;
122  default:
123  return false; // Raw and Extension not supported
124  }
125 
126  // Store it.
127  if (Stack.empty())
128  Root = Node;
129  else if (Stack.back().Node.getKind() == Type::Array) {
130  // Reading an array entry.
131  auto &Array = Stack.back().Node.getArray();
132  Array.push_back(Node);
133  } else {
134  auto &Map = Stack.back().Node.getMap();
135  if (!Stack.back().MapEntry) {
136  // Reading a map key.
137  Stack.back().MapEntry = &Map[Node];
138  } else {
139  // Reading the value for the map key read in the last iteration.
140  *Stack.back().MapEntry = Node;
141  Stack.back().MapEntry = nullptr;
142  }
143  }
144 
145  // See if we're starting a new array or map.
146  switch (Node.getKind()) {
148  case msgpack::Type::Map:
149  Stack.push_back(StackLevel({Node, Obj.Length, nullptr}));
150  break;
151  default:
152  break;
153  }
154 
155  // Pop finished stack levels.
156  while (!Stack.empty()) {
157  if (Stack.back().Node.getKind() == msgpack::Type::Array) {
158  if (Stack.back().Node.getArray().size() != Stack.back().Length)
159  break;
160  } else {
161  if (Stack.back().MapEntry ||
162  Stack.back().Node.getMap().size() != Stack.back().Length)
163  break;
164  }
165  Stack.pop_back();
166  }
167  } while (!Stack.empty());
168  return true;
169 }
170 
173  DocNode::MapTy::iterator MapIt;
174  DocNode::ArrayTy::iterator ArrayIt;
175  bool OnKey;
176 };
177 
178 /// Write a MsgPack document to a binary MsgPack blob.
179 void Document::writeToBlob(std::string &Blob) {
180  Blob.clear();
181  raw_string_ostream OS(Blob);
182  msgpack::Writer MPWriter(OS);
184  DocNode Node = getRoot();
185  for (;;) {
186  switch (Node.getKind()) {
187  case Type::Array:
188  MPWriter.writeArraySize(Node.getArray().size());
189  Stack.push_back(
190  {Node, DocNode::MapTy::iterator(), Node.getArray().begin(), false});
191  break;
192  case Type::Map:
193  MPWriter.writeMapSize(Node.getMap().size());
194  Stack.push_back(
195  {Node, Node.getMap().begin(), DocNode::ArrayTy::iterator(), true});
196  break;
197  case Type::Nil:
198  MPWriter.writeNil();
199  break;
200  case Type::Boolean:
201  MPWriter.write(Node.getBool());
202  break;
203  case Type::Int:
204  MPWriter.write(Node.getInt());
205  break;
206  case Type::UInt:
207  MPWriter.write(Node.getUInt());
208  break;
209  case Type::String:
210  MPWriter.write(Node.getString());
211  break;
212  default:
213  llvm_unreachable("unhandled msgpack object kind");
214  }
215  // Pop finished stack levels.
216  while (!Stack.empty()) {
217  if (Stack.back().Node.getKind() == Type::Map) {
218  if (Stack.back().MapIt != Stack.back().Node.getMap().end())
219  break;
220  } else {
221  if (Stack.back().ArrayIt != Stack.back().Node.getArray().end())
222  break;
223  }
224  Stack.pop_back();
225  }
226  if (Stack.empty())
227  break;
228  // Get the next value.
229  if (Stack.back().Node.getKind() == Type::Map) {
230  if (Stack.back().OnKey) {
231  // Do the key of a key,value pair in a map.
232  Node = Stack.back().MapIt->first;
233  Stack.back().OnKey = false;
234  } else {
235  Node = Stack.back().MapIt->second;
236  ++Stack.back().MapIt;
237  Stack.back().OnKey = true;
238  }
239  } else {
240  Node = *Stack.back().ArrayIt;
241  ++Stack.back().ArrayIt;
242  }
243  }
244 }
245 
Expected< bool > read(Object &Obj)
Read one object from the input buffer, advancing past it.
DocNode & operator[](size_t Index)
Element access. This extends the array if necessary.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Document * getDocument() const
DocNode & operator[](StringRef S)
Member access.
MapDocNode & getMap(bool Convert=false)
Get a MapDocNode for a map node.
void writeMapSize(uint32_t Size)
Write the header for a Map of the given size.
DocNode::MapTy::iterator MapIt
Writes MessagePack objects to an output stream, one at a time.
Definition: MsgPackWriter.h:40
Key
PAL metadata keys.
ArrayTy::iterator begin()
DocNode getNode()
Create a nil node associated with this Document.
StringRef getString() const
MessagePack object, represented as a tagged union of C++ types.
Definition: MsgPackReader.h:75
DocNode * MapEntry
ArrayDocNode getArrayNode()
Create an empty Array node associated with this Document.
MapTy::iterator find(DocNode Key)
void writeNil()
Write a Nil to the output stream.
Reads MessagePack objects from memory, one at a time.
Definition: MsgPackReader.h:98
ArrayDocNode & getArray(bool Convert=false)
Get an ArrayDocNode for an array node.
void writeArraySize(uint32_t Size)
Write the header for an Array of the given size.
size_t size() const
Definition: SmallVector.h:52
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1186
MapDocNode getMapNode()
Create an empty Map node associated with this Document.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
double Float
Value for Type::Float.
Definition: MsgPackReader.h:85
void write(bool b)
Write a Boolean to the output stream.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1146
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
void writeToBlob(std::string &Blob)
Write a MsgPack document to a binary MsgPack blob.
StringRef Raw
Value for Type::String and Type::Binary.
Definition: MsgPackReader.h:87
DocNode::ArrayTy::iterator ArrayIt
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
int64_t Int
Value for Type::Int.
Definition: MsgPackReader.h:79
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
bool Bool
Value for Type::Boolean.
Definition: MsgPackReader.h:83
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
uint64_t UInt
Value for Type::Uint.
Definition: MsgPackReader.h:81
size_t Length
Value for Type::Array and Type::Map.
Definition: MsgPackReader.h:89
bool readFromBlob(StringRef Blob, bool Multi)
Read a MsgPack document from a binary MsgPack blob.
A node in a MsgPack Document.
This file contains a MessagePack writer.