LLVM  4.0.0
StreamArray.h
Go to the documentation of this file.
1 //===- StreamArray.h - Array backed by an arbitrary stream ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_MSF_STREAMARRAY_H
11 #define LLVM_DEBUGINFO_MSF_STREAMARRAY_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/iterator.h"
16 #include "llvm/Support/Error.h"
17 #include <cassert>
18 #include <cstdint>
19 
20 namespace llvm {
21 namespace msf {
22 
23 /// VarStreamArrayExtractor is intended to be specialized to provide customized
24 /// extraction logic. On input it receives a StreamRef pointing to the
25 /// beginning of the next record, but where the length of the record is not yet
26 /// known. Upon completion, it should return an appropriate Error instance if
27 /// a record could not be extracted, or if one could be extracted it should
28 /// return success and set Len to the number of bytes this record occupied in
29 /// the underlying stream, and it should fill out the fields of the value type
30 /// Item appropriately to represent the current record.
31 ///
32 /// You can specialize this template for your own custom value types to avoid
33 /// having to specify a second template argument to VarStreamArray (documented
34 /// below).
35 template <typename T> struct VarStreamArrayExtractor {
36  // Method intentionally deleted. You must provide an explicit specialization
37  // with the following method implemented.
39  T &Item) const = delete;
40 };
41 
42 /// VarStreamArray represents an array of variable length records backed by a
43 /// stream. This could be a contiguous sequence of bytes in memory, it could
44 /// be a file on disk, or it could be a PDB stream where bytes are stored as
45 /// discontiguous blocks in a file. Usually it is desirable to treat arrays
46 /// as contiguous blocks of memory, but doing so with large PDB files, for
47 /// example, could mean allocating huge amounts of memory just to allow
48 /// re-ordering of stream data to be contiguous before iterating over it. By
49 /// abstracting this out, we need not duplicate this memory, and we can
50 /// iterate over arrays in arbitrarily formatted streams. Elements are parsed
51 /// lazily on iteration, so there is no upfront cost associated with building
52 /// a VarStreamArray, no matter how large it may be.
53 ///
54 /// You create a VarStreamArray by specifying a ValueType and an Extractor type.
55 /// If you do not specify an Extractor type, it expects you to specialize
56 /// VarStreamArrayExtractor<T> for your ValueType.
57 ///
58 /// By default an Extractor is default constructed in the class, but in some
59 /// cases you might find it useful for an Extractor to maintain state across
60 /// extractions. In this case you can provide your own Extractor through a
61 /// secondary constructor. The following examples show various ways of
62 /// creating a VarStreamArray.
63 ///
64 /// // Will use VarStreamArrayExtractor<MyType> as the extractor.
65 /// VarStreamArray<MyType> MyTypeArray;
66 ///
67 /// // Will use a default-constructed MyExtractor as the extractor.
68 /// VarStreamArray<MyType, MyExtractor> MyTypeArray2;
69 ///
70 /// // Will use the specific instance of MyExtractor provided.
71 /// // MyExtractor need not be default-constructible in this case.
72 /// MyExtractor E(SomeContext);
73 /// VarStreamArray<MyType, MyExtractor> MyTypeArray3(E);
74 ///
75 template <typename ValueType, typename Extractor> class VarStreamArrayIterator;
76 
77 template <typename ValueType,
78  typename Extractor = VarStreamArrayExtractor<ValueType>>
79 
81  friend class VarStreamArrayIterator<ValueType, Extractor>;
82 
83 public:
85 
86  VarStreamArray() = default;
87  explicit VarStreamArray(const Extractor &E) : E(E) {}
88 
89  explicit VarStreamArray(ReadableStreamRef Stream) : Stream(Stream) {}
90  VarStreamArray(ReadableStreamRef Stream, const Extractor &E)
91  : Stream(Stream), E(E) {}
92 
94  : Stream(Other.Stream), E(Other.E) {}
95 
96  Iterator begin(bool *HadError = nullptr) const {
97  return Iterator(*this, E, HadError);
98  }
99 
100  Iterator end() const { return Iterator(E); }
101 
102  const Extractor &getExtractor() const { return E; }
103 
104  ReadableStreamRef getUnderlyingStream() const { return Stream; }
105 
106 private:
107  ReadableStreamRef Stream;
108  Extractor E;
109 };
110 
111 template <typename ValueType, typename Extractor>
112 class VarStreamArrayIterator
113  : public iterator_facade_base<VarStreamArrayIterator<ValueType, Extractor>,
114  std::forward_iterator_tag, ValueType> {
115  typedef VarStreamArrayIterator<ValueType, Extractor> IterType;
116  typedef VarStreamArray<ValueType, Extractor> ArrayType;
117 
118 public:
119  VarStreamArrayIterator(const ArrayType &Array, const Extractor &E,
120  bool *HadError = nullptr)
121  : IterRef(Array.Stream), Array(&Array), HadError(HadError), Extract(E) {
122  if (IterRef.getLength() == 0)
123  moveToEnd();
124  else {
125  auto EC = Extract(IterRef, ThisLen, ThisValue);
126  if (EC) {
127  consumeError(std::move(EC));
128  markError();
129  }
130  }
131  }
132  VarStreamArrayIterator() = default;
133  explicit VarStreamArrayIterator(const Extractor &E) : Extract(E) {}
134  ~VarStreamArrayIterator() = default;
135 
136  bool operator==(const IterType &R) const {
137  if (Array && R.Array) {
138  // Both have a valid array, make sure they're same.
139  assert(Array == R.Array);
140  return IterRef == R.IterRef;
141  }
142 
143  // Both iterators are at the end.
144  if (!Array && !R.Array)
145  return true;
146 
147  // One is not at the end and one is.
148  return false;
149  }
150 
151  const ValueType &operator*() const {
152  assert(Array && !HasError);
153  return ThisValue;
154  }
155 
157  // We are done with the current record, discard it so that we are
158  // positioned at the next record.
159  IterRef = IterRef.drop_front(ThisLen);
160  if (IterRef.getLength() == 0) {
161  // There is nothing after the current record, we must make this an end
162  // iterator.
163  moveToEnd();
164  } else {
165  // There is some data after the current record.
166  auto EC = Extract(IterRef, ThisLen, ThisValue);
167  if (EC) {
168  consumeError(std::move(EC));
169  markError();
170  } else if (ThisLen == 0) {
171  // An empty record? Make this an end iterator.
172  moveToEnd();
173  }
174  }
175  return *this;
176  }
177 
178 private:
179  void moveToEnd() {
180  Array = nullptr;
181  ThisLen = 0;
182  }
183  void markError() {
184  moveToEnd();
185  HasError = true;
186  if (HadError != nullptr)
187  *HadError = true;
188  }
189 
190  ValueType ThisValue;
191  ReadableStreamRef IterRef;
192  const ArrayType *Array{nullptr};
193  uint32_t ThisLen{0};
194  bool HasError{false};
195  bool *HadError{nullptr};
196  Extractor Extract;
197 };
198 
199 template <typename T> class FixedStreamArrayIterator;
200 
201 template <typename T> class FixedStreamArray {
203 
204 public:
205  FixedStreamArray() = default;
206  FixedStreamArray(ReadableStreamRef Stream) : Stream(Stream) {
207  assert(Stream.getLength() % sizeof(T) == 0);
208  }
209 
210  bool operator==(const FixedStreamArray<T> &Other) const {
211  return Stream == Other.Stream;
212  }
213 
214  bool operator!=(const FixedStreamArray<T> &Other) const {
215  return !(*this == Other);
216  }
217 
218  FixedStreamArray &operator=(const FixedStreamArray &) = default;
219 
220  const T &operator[](uint32_t Index) const {
221  assert(Index < size());
222  uint32_t Off = Index * sizeof(T);
223  ArrayRef<uint8_t> Data;
224  if (auto EC = Stream.readBytes(Off, sizeof(T), Data)) {
225  assert(false && "Unexpected failure reading from stream");
226  // This should never happen since we asserted that the stream length was
227  // an exact multiple of the element size.
228  consumeError(std::move(EC));
229  }
230  return *reinterpret_cast<const T *>(Data.data());
231  }
232 
233  uint32_t size() const { return Stream.getLength() / sizeof(T); }
234 
235  bool empty() const { return size() == 0; }
236 
238  return FixedStreamArrayIterator<T>(*this, 0);
239  }
240 
242  return FixedStreamArrayIterator<T>(*this, size());
243  }
244 
245  ReadableStreamRef getUnderlyingStream() const { return Stream; }
246 
247 private:
248  ReadableStreamRef Stream;
249 };
250 
251 template <typename T>
252 class FixedStreamArrayIterator
253  : public iterator_facade_base<FixedStreamArrayIterator<T>,
254  std::random_access_iterator_tag, T> {
255 
256 public:
258  : Array(Array), Index(Index) {}
259 
262  Array = Other.Array;
263  Index = Other.Index;
264  return *this;
265  }
266 
267  const T &operator*() const { return Array[Index]; }
268 
270  assert(Array == R.Array);
271  return (Index == R.Index) && (Array == R.Array);
272  }
273 
275  Index += N;
276  return *this;
277  }
278 
280  assert(Index >= N);
281  Index -= N;
282  return *this;
283  }
284 
285  std::ptrdiff_t operator-(const FixedStreamArrayIterator<T> &R) const {
286  assert(Array == R.Array);
287  assert(Index >= R.Index);
288  return Index - R.Index;
289  }
290 
291  bool operator<(const FixedStreamArrayIterator<T> &RHS) const {
292  assert(Array == RHS.Array);
293  return Index < RHS.Index;
294  }
295 
296 private:
297  FixedStreamArray<T> Array;
298  uint32_t Index;
299 };
300 
301 } // namespace msf
302 } // namespace llvm
303 
304 #endif // LLVM_DEBUGINFO_MSF_STREAMARRAY_H
bool operator==(const IterType &R) const
Definition: StreamArray.h:136
VarStreamArray(const Extractor &E)
Definition: StreamArray.h:87
Error operator()(ReadableStreamRef Stream, uint32_t &Len, T &Item) const =delete
VarStreamArray represents an array of variable length records backed by a stream. ...
Definition: StreamArray.h:75
bool operator==(const FixedStreamArray< T > &Other) const
Definition: StreamArray.h:210
FixedStreamArrayIterator< T > & operator=(const FixedStreamArrayIterator< T > &Other)
Definition: StreamArray.h:261
FixedStreamArrayIterator< T > end() const
Definition: StreamArray.h:241
FixedStreamArrayIterator< T > & operator+=(std::ptrdiff_t N)
Definition: StreamArray.h:274
const T & operator[](uint32_t Index) const
Definition: StreamArray.h:220
uint32_t size() const
Definition: StreamArray.h:233
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:662
Iterator begin(bool *HadError=nullptr) const
Definition: StreamArray.h:96
bool operator!=(const FixedStreamArray< T > &Other) const
Definition: StreamArray.h:214
FixedStreamArrayIterator(const FixedStreamArray< T > &Array, uint32_t Index)
Definition: StreamArray.h:257
#define T
FixedStreamArray(ReadableStreamRef Stream)
Definition: StreamArray.h:206
Class to represent array types.
Definition: DerivedTypes.h:345
FixedStreamArrayIterator< T > & operator-=(std::ptrdiff_t N)
Definition: StreamArray.h:279
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const
Definition: StreamRef.h:81
bool operator==(const FixedStreamArrayIterator< T > &R) const
Definition: StreamArray.h:269
ReadableStreamRef getUnderlyingStream() const
Definition: StreamArray.h:245
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:65
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
VarStreamArrayIterator(const ArrayType &Array, const Extractor &E, bool *HadError=nullptr)
Definition: StreamArray.h:119
RefType drop_front(uint32_t N) const
Definition: StreamRef.h:32
ReadableStreamRef getUnderlyingStream() const
Definition: StreamArray.h:104
uint32_t getLength() const
Definition: StreamRef.h:29
VarStreamArray(ReadableStreamRef Stream, const Extractor &E)
Definition: StreamArray.h:90
std::ptrdiff_t operator-(const FixedStreamArrayIterator< T > &R) const
Definition: StreamArray.h:285
VarStreamArray(ReadableStreamRef Stream)
Definition: StreamArray.h:89
void consumeError(Error Err)
Consume a Error without doing anything.
const Extractor & getExtractor() const
Definition: StreamArray.h:102
Iterator end() const
Definition: StreamArray.h:100
FixedStreamArrayIterator< T > begin() const
Definition: StreamArray.h:237
const ValueType & operator*() const
Definition: StreamArray.h:151
VarStreamArray(const VarStreamArray< ValueType, Extractor > &Other)
Definition: StreamArray.h:93
VarStreamArrayIterator(const Extractor &E)
Definition: StreamArray.h:133
VarStreamArrayIterator< ValueType, Extractor > Iterator
Definition: StreamArray.h:84
FixedStreamArray & operator=(const FixedStreamArray &)=default
#define N
VarStreamArrayExtractor is intended to be specialized to provide customized extraction logic...
Definition: StreamArray.h:35
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType