LLVM 22.0.0git
BinaryStreamRef.h
Go to the documentation of this file.
1//===- BinaryStreamRef.h - A copyable reference to a stream -----*- C++ -*-===//
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#ifndef LLVM_SUPPORT_BINARYSTREAMREF_H
10#define LLVM_SUPPORT_BINARYSTREAMREF_H
11
12#include "llvm/ADT/ArrayRef.h"
16#include "llvm/Support/Error.h"
17#include <cstdint>
18#include <memory>
19#include <optional>
20
21namespace llvm {
22
23/// Common stuff for mutable and immutable StreamRefs.
24template <class RefType, class StreamType> class BinaryStreamRefBase {
25protected:
27 explicit BinaryStreamRefBase(StreamType &BorrowedImpl)
29 if (!(BorrowedImpl.getFlags() & BSF_Append))
30 Length = BorrowedImpl.getLength();
31 }
32
33 BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint64_t Offset,
34 std::optional<uint64_t> Length)
38 std::optional<uint64_t> Length)
42
45
46public:
47 llvm::endianness getEndian() const { return BorrowedImpl->getEndian(); }
48
50 if (Length)
51 return *Length;
52
53 return BorrowedImpl ? (BorrowedImpl->getLength() - ViewOffset) : 0;
54 }
55
56 /// Return a new BinaryStreamRef with the first \p N elements removed. If
57 /// this BinaryStreamRef is length-tracking, then the resulting one will be
58 /// too.
59 RefType drop_front(uint64_t N) const {
60 if (!BorrowedImpl)
61 return RefType();
62
63 N = std::min(N, getLength());
64 RefType Result(static_cast<const RefType &>(*this));
65 if (N == 0)
66 return Result;
67
68 Result.ViewOffset += N;
69 if (Result.Length)
70 *Result.Length -= N;
71 return Result;
72 }
73
74 /// Return a new BinaryStreamRef with the last \p N elements removed. If
75 /// this BinaryStreamRef is length-tracking and \p N is greater than 0, then
76 /// this BinaryStreamRef will no longer length-track.
77 RefType drop_back(uint64_t N) const {
78 if (!BorrowedImpl)
79 return RefType();
80
81 RefType Result(static_cast<const RefType &>(*this));
82 N = std::min(N, getLength());
83
84 if (N == 0)
85 return Result;
86
87 // Since we're dropping non-zero bytes from the end, stop length-tracking
88 // by setting the length of the resulting StreamRef to an explicit value.
89 if (!Result.Length)
90 Result.Length = getLength();
91
92 *Result.Length -= N;
93 return Result;
94 }
95
96 /// Return a new BinaryStreamRef with only the first \p N elements remaining.
97 RefType keep_front(uint64_t N) const {
98 assert(N <= getLength());
99 return drop_back(getLength() - N);
100 }
101
102 /// Return a new BinaryStreamRef with only the last \p N elements remaining.
103 RefType keep_back(uint64_t N) const {
104 assert(N <= getLength());
105 return drop_front(getLength() - N);
106 }
107
108 /// Return a new BinaryStreamRef with the first and last \p N elements
109 /// removed.
110 RefType drop_symmetric(uint64_t N) const {
111 return drop_front(N).drop_back(N);
112 }
113
114 /// Return a new BinaryStreamRef with the first \p Offset elements removed,
115 /// and retaining exactly \p Len elements.
116 RefType slice(uint64_t Offset, uint64_t Len) const {
117 return drop_front(Offset).keep_front(Len);
118 }
119
120 bool valid() const { return BorrowedImpl != nullptr; }
121
122 friend bool operator==(const RefType &LHS, const RefType &RHS) {
123 if (LHS.BorrowedImpl != RHS.BorrowedImpl)
124 return false;
125 if (LHS.ViewOffset != RHS.ViewOffset)
126 return false;
127 if (LHS.Length != RHS.Length)
128 return false;
129 return true;
130 }
131
132protected:
134 if (Offset > getLength())
135 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
136 if (getLength() < DataSize + Offset)
137 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
138 return Error::success();
139 }
140
141 std::shared_ptr<StreamType> SharedImpl;
142 StreamType *BorrowedImpl = nullptr;
144 std::optional<uint64_t> Length;
145};
146
147/// BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It
148/// provides copy-semantics and read only access to a "window" of the underlying
149/// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to
150/// say, it does not inherit and override the methods of BinaryStream. In
151/// general, you should not pass around pointers or references to BinaryStreams
152/// and use inheritance to achieve polymorphism. Instead, you should pass
153/// around BinaryStreamRefs by value and achieve polymorphism that way.
155 : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> {
158 BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint64_t ViewOffset,
159 std::optional<uint64_t> Length)
161
162public:
163 BinaryStreamRef() = default;
166 std::optional<uint64_t> Length);
170
175
176 // Use BinaryStreamRef.slice() instead.
178 uint64_t Length) = delete;
179
180 /// Given an Offset into this StreamRef and a Size, return a reference to a
181 /// buffer owned by the stream.
182 ///
183 /// \returns a success error code if the entire range of data is within the
184 /// bounds of this BinaryStreamRef's view and the implementation could read
185 /// the data, and an appropriate error code otherwise.
187 ArrayRef<uint8_t> &Buffer) const;
188
189 /// Given an Offset into this BinaryStreamRef, return a reference to the
190 /// largest buffer the stream could support without necessitating a copy.
191 ///
192 /// \returns a success error code if implementation could read the data,
193 /// and an appropriate error code otherwise.
195 ArrayRef<uint8_t> &Buffer) const;
196};
197
199 uint64_t Offset = 0; // Offset in the parent stream
201
203 BinaryStreamRef SubSub = StreamData.slice(Off, Size);
204 return {Off + Offset, SubSub};
205 }
207 return slice(N, size() - N);
208 }
210
211 std::pair<BinarySubstreamRef, BinarySubstreamRef> split(uint64_t Off) const {
212 return std::make_pair(keep_front(Off), drop_front(Off));
213 }
214
215 uint64_t size() const { return StreamData.getLength(); }
216 bool empty() const { return size() == 0; }
217};
218
220 : public BinaryStreamRefBase<WritableBinaryStreamRef,
221 WritableBinaryStream> {
223 WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl,
224 uint64_t ViewOffset, std::optional<uint64_t> Length)
226
227 Error checkOffsetForWrite(uint64_t Offset, uint64_t DataSize) const {
228 if (!(BorrowedImpl->getFlags() & BSF_Append))
229 return checkOffsetForRead(Offset, DataSize);
230
231 if (Offset > getLength())
232 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
233 return Error::success();
234 }
235
236public:
241 std::optional<uint64_t> Length);
247
250
251 // Use WritableBinaryStreamRef.slice() instead.
253 uint64_t Length) = delete;
254
255 /// Given an Offset into this WritableBinaryStreamRef and some input data,
256 /// writes the data to the underlying stream.
257 ///
258 /// \returns a success error code if the data could fit within the underlying
259 /// stream at the specified location and the implementation could write the
260 /// data, and an appropriate error code otherwise.
262
263 /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef.
264 LLVM_ABI operator BinaryStreamRef() const;
265
266 /// For buffered streams, commits changes to the backing store.
268};
269
270} // end namespace llvm
271
272#endif // LLVM_SUPPORT_BINARYSTREAMREF_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Size
endianness Endian
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Common stuff for mutable and immutable StreamRefs.
friend bool operator==(const RefType &LHS, const RefType &RHS)
std::optional< uint64_t > Length
RefType drop_back(uint64_t N) const
Return a new BinaryStreamRef with the last N elements removed.
BinaryStreamRefBase & operator=(const BinaryStreamRefBase &Other)=default
RefType drop_front(uint64_t N) const
Return a new BinaryStreamRef with the first N elements removed.
BinaryStreamRefBase & operator=(BinaryStreamRefBase &&Other)=default
BinaryStreamRefBase(StreamType &BorrowedImpl, uint64_t Offset, std::optional< uint64_t > Length)
std::shared_ptr< StreamType > SharedImpl
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) const
RefType drop_symmetric(uint64_t N) const
Return a new BinaryStreamRef with the first and last N elements removed.
BinaryStreamRefBase(std::shared_ptr< StreamType > SharedImpl, uint64_t Offset, std::optional< uint64_t > Length)
RefType slice(uint64_t Offset, uint64_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
BinaryStreamRefBase(StreamType &BorrowedImpl)
llvm::endianness getEndian() const
RefType keep_back(uint64_t N) const
Return a new BinaryStreamRef with only the last N elements remaining.
RefType keep_front(uint64_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
BinaryStreamRefBase(BinaryStreamRefBase &&Other)=default
uint64_t getLength() const
BinaryStreamRefBase(const BinaryStreamRefBase &Other)=default
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
BinaryStreamRef(BinaryStreamRef &&Other)=default
LLVM_ABI Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this BinaryStreamRef, return a reference to the largest buffer the stream could ...
BinaryStreamRef(const BinaryStreamRef &Other)=default
BinaryStreamRef(BinaryStreamRef &S, uint64_t Offset, uint64_t Length)=delete
LLVM_ABI Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this StreamRef and a Size, return a reference to a buffer owned by the stream.
BinaryStreamRef & operator=(BinaryStreamRef &&Other)=default
BinaryStreamRef & operator=(const BinaryStreamRef &Other)=default
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:34
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
WritableBinaryStreamRef & operator=(const WritableBinaryStreamRef &Other)=default
WritableBinaryStreamRef(const WritableBinaryStreamRef &Other)=default
WritableBinaryStreamRef(WritableBinaryStreamRef &&Other)=default
WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint64_t Offset, uint64_t Length)=delete
LLVM_ABI Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
WritableBinaryStreamRef & operator=(WritableBinaryStreamRef &&Other)=default
LLVM_ABI Error commit()
For buffered streams, commits changes to the backing store.
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:72
BinaryStreamFlags getFlags() const override
Return the properties of this stream.
Definition: BinaryStream.h:85
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
@ BSF_Append
Definition: BinaryStream.h:23
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
@ Other
Any other memory.
endianness
Definition: bit.h:71
#define N
std::pair< BinarySubstreamRef, BinarySubstreamRef > split(uint64_t Off) const
BinarySubstreamRef keep_front(uint64_t N) const
BinarySubstreamRef slice(uint64_t Off, uint64_t Size) const
BinaryStreamRef StreamData
BinarySubstreamRef drop_front(uint64_t N) const