summaryrefslogtreecommitdiff
path: root/include/llvm/Support
diff options
context:
space:
mode:
Diffstat (limited to 'include/llvm/Support')
-rw-r--r--include/llvm/Support/BinaryStreamReader.h21
-rw-r--r--include/llvm/Support/BinaryStreamRef.h150
-rw-r--r--include/llvm/Support/BinaryStreamWriter.h15
-rw-r--r--include/llvm/Support/FileSystem.h2
4 files changed, 123 insertions, 65 deletions
diff --git a/include/llvm/Support/BinaryStreamReader.h b/include/llvm/Support/BinaryStreamReader.h
index 77738077f5ff..56375f41d2c0 100644
--- a/include/llvm/Support/BinaryStreamReader.h
+++ b/include/llvm/Support/BinaryStreamReader.h
@@ -16,7 +16,6 @@
#include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
-#include "llvm/Support/MathExtras.h"
#include "llvm/Support/type_traits.h"
#include <string>
@@ -32,7 +31,21 @@ namespace llvm {
class BinaryStreamReader {
public:
BinaryStreamReader() = default;
- explicit BinaryStreamReader(BinaryStreamRef Stream);
+ explicit BinaryStreamReader(BinaryStreamRef Ref);
+ explicit BinaryStreamReader(BinaryStream &Stream);
+ explicit BinaryStreamReader(ArrayRef<uint8_t> Data,
+ llvm::support::endianness Endian);
+ explicit BinaryStreamReader(StringRef Data, llvm::support::endianness Endian);
+
+ BinaryStreamReader(const BinaryStreamReader &Other)
+ : Stream(Other.Stream), Offset(Other.Offset) {}
+
+ BinaryStreamReader &operator=(const BinaryStreamReader &Other) {
+ Stream = Other.Stream;
+ Offset = Other.Offset;
+ return *this;
+ }
+
virtual ~BinaryStreamReader() {}
/// Read as much as possible from the underlying string at the current offset
@@ -244,12 +257,14 @@ public:
/// \returns the next byte in the stream.
uint8_t peek() const;
+ Error padToAlignment(uint32_t Align);
+
std::pair<BinaryStreamReader, BinaryStreamReader>
split(uint32_t Offset) const;
private:
BinaryStreamRef Stream;
- uint32_t Offset;
+ uint32_t Offset = 0;
};
} // namespace llvm
diff --git a/include/llvm/Support/BinaryStreamRef.h b/include/llvm/Support/BinaryStreamRef.h
index 465e724a6886..e3bd4bf0860e 100644
--- a/include/llvm/Support/BinaryStreamRef.h
+++ b/include/llvm/Support/BinaryStreamRef.h
@@ -16,36 +16,74 @@
#include "llvm/Support/Error.h"
#include <algorithm>
#include <cstdint>
+#include <memory>
namespace llvm {
/// Common stuff for mutable and immutable StreamRefs.
-template <class StreamType, class RefType> class BinaryStreamRefBase {
-public:
- BinaryStreamRefBase() : Stream(nullptr), ViewOffset(0), Length(0) {}
- BinaryStreamRefBase(StreamType &Stream, uint32_t Offset, uint32_t Length)
- : Stream(&Stream), ViewOffset(Offset), Length(Length) {}
+template <class RefType, class StreamType> class BinaryStreamRefBase {
+protected:
+ BinaryStreamRefBase() = default;
+ BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint32_t Offset,
+ uint32_t Length)
+ : SharedImpl(SharedImpl), BorrowedImpl(SharedImpl.get()),
+ ViewOffset(Offset), Length(Length) {}
+ BinaryStreamRefBase(StreamType &BorrowedImpl, uint32_t Offset,
+ uint32_t Length)
+ : BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {}
+ BinaryStreamRefBase(const BinaryStreamRefBase &Other) {
+ SharedImpl = Other.SharedImpl;
+ BorrowedImpl = Other.BorrowedImpl;
+ ViewOffset = Other.ViewOffset;
+ Length = Other.Length;
+ }
- llvm::support::endianness getEndian() const { return Stream->getEndian(); }
+public:
+ llvm::support::endianness getEndian() const {
+ return BorrowedImpl->getEndian();
+ }
uint32_t getLength() const { return Length; }
- const StreamType *getStream() const { return Stream; }
/// Return a new BinaryStreamRef with the first \p N elements removed.
RefType drop_front(uint32_t N) const {
- if (!Stream)
+ if (!BorrowedImpl)
return RefType();
N = std::min(N, Length);
- return RefType(*Stream, ViewOffset + N, Length - N);
+ RefType Result(static_cast<const RefType &>(*this));
+ Result.ViewOffset += N;
+ Result.Length -= N;
+ return Result;
}
- /// Return a new BinaryStreamRef with only the first \p N elements remaining.
- RefType keep_front(uint32_t N) const {
- if (!Stream)
+ /// Return a new BinaryStreamRef with the first \p N elements removed.
+ RefType drop_back(uint32_t N) const {
+ if (!BorrowedImpl)
return RefType();
+
N = std::min(N, Length);
- return RefType(*Stream, ViewOffset, N);
+ RefType Result(static_cast<const RefType &>(*this));
+ Result.Length -= N;
+ return Result;
+ }
+
+ /// Return a new BinaryStreamRef with only the first \p N elements remaining.
+ RefType keep_front(uint32_t N) const {
+ assert(N <= getLength());
+ return drop_back(getLength() - N);
+ }
+
+ /// Return a new BinaryStreamRef with only the last \p N elements remaining.
+ RefType keep_back(uint32_t N) const {
+ assert(N <= getLength());
+ return drop_front(getLength() - N);
+ }
+
+ /// Return a new BinaryStreamRef with the first and last \p N elements
+ /// removed.
+ RefType drop_symmetric(uint32_t N) const {
+ return drop_front(N).drop_back(N);
}
/// Return a new BinaryStreamRef with the first \p Offset elements removed,
@@ -54,8 +92,10 @@ public:
return drop_front(Offset).keep_front(Len);
}
+ bool valid() const { return BorrowedImpl != nullptr; }
+
bool operator==(const RefType &Other) const {
- if (Stream != Other.Stream)
+ if (BorrowedImpl != Other.BorrowedImpl)
return false;
if (ViewOffset != Other.ViewOffset)
return false;
@@ -73,9 +113,10 @@ protected:
return Error::success();
}
- StreamType *Stream;
- uint32_t ViewOffset;
- uint32_t Length;
+ std::shared_ptr<StreamType> SharedImpl;
+ StreamType *BorrowedImpl = nullptr;
+ uint32_t ViewOffset = 0;
+ uint32_t Length = 0;
};
/// \brief BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It
@@ -86,21 +127,27 @@ protected:
/// and use inheritance to achieve polymorphism. Instead, you should pass
/// around BinaryStreamRefs by value and achieve polymorphism that way.
class BinaryStreamRef
- : public BinaryStreamRefBase<BinaryStream, BinaryStreamRef> {
+ : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> {
+ friend BinaryStreamRefBase<BinaryStreamRef, BinaryStream>;
+ friend class WritableBinaryStreamRef;
+ BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint32_t ViewOffset,
+ uint32_t Length)
+ : BinaryStreamRefBase(Impl, ViewOffset, Length) {}
+
public:
BinaryStreamRef() = default;
- BinaryStreamRef(BinaryStream &Stream)
- : BinaryStreamRefBase(Stream, 0, Stream.getLength()) {}
- BinaryStreamRef(BinaryStream &Stream, uint32_t Offset, uint32_t Length)
- : BinaryStreamRefBase(Stream, Offset, Length) {}
+ BinaryStreamRef(BinaryStream &Stream);
+ BinaryStreamRef(BinaryStream &Stream, uint32_t Offset, uint32_t Length);
+ explicit BinaryStreamRef(ArrayRef<uint8_t> Data,
+ llvm::support::endianness Endian);
+ explicit BinaryStreamRef(StringRef Data, llvm::support::endianness Endian);
+
+ BinaryStreamRef(const BinaryStreamRef &Other);
// Use BinaryStreamRef.slice() instead.
BinaryStreamRef(BinaryStreamRef &S, uint32_t Offset,
uint32_t Length) = delete;
- /// Check if a Stream is valid.
- bool valid() const { return Stream != nullptr; }
-
/// Given an Offset into this StreamRef and a Size, return a reference to a
/// buffer owned by the stream.
///
@@ -108,12 +155,7 @@ public:
/// bounds of this BinaryStreamRef's view and the implementation could read
/// the data, and an appropriate error code otherwise.
Error readBytes(uint32_t Offset, uint32_t Size,
- ArrayRef<uint8_t> &Buffer) const {
- if (auto EC = checkOffset(Offset, Size))
- return EC;
-
- return Stream->readBytes(ViewOffset + Offset, Size, Buffer);
- }
+ ArrayRef<uint8_t> &Buffer) const;
/// Given an Offset into this BinaryStreamRef, return a reference to the
/// largest buffer the stream could support without necessitating a copy.
@@ -121,33 +163,25 @@ public:
/// \returns a success error code if implementation could read the data,
/// and an appropriate error code otherwise.
Error readLongestContiguousChunk(uint32_t Offset,
- ArrayRef<uint8_t> &Buffer) const {
- if (auto EC = checkOffset(Offset, 1))
- return EC;
-
- if (auto EC =
- Stream->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
- return EC;
- // This StreamRef might refer to a smaller window over a larger stream. In
- // that case we will have read out more bytes than we should return, because
- // we should not read past the end of the current view.
- uint32_t MaxLength = Length - Offset;
- if (Buffer.size() > MaxLength)
- Buffer = Buffer.slice(0, MaxLength);
- return Error::success();
- }
+ ArrayRef<uint8_t> &Buffer) const;
};
class WritableBinaryStreamRef
- : public BinaryStreamRefBase<WritableBinaryStream,
- WritableBinaryStreamRef> {
+ : public BinaryStreamRefBase<WritableBinaryStreamRef,
+ WritableBinaryStream> {
+ friend BinaryStreamRefBase<WritableBinaryStreamRef, WritableBinaryStream>;
+ WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl,
+ uint32_t ViewOffset, uint32_t Length)
+ : BinaryStreamRefBase(Impl, ViewOffset, Length) {}
+
public:
WritableBinaryStreamRef() = default;
- WritableBinaryStreamRef(WritableBinaryStream &Stream)
- : BinaryStreamRefBase(Stream, 0, Stream.getLength()) {}
+ WritableBinaryStreamRef(WritableBinaryStream &Stream);
WritableBinaryStreamRef(WritableBinaryStream &Stream, uint32_t Offset,
- uint32_t Length)
- : BinaryStreamRefBase(Stream, Offset, Length) {}
+ uint32_t Length);
+ explicit WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
+ llvm::support::endianness Endian);
+ WritableBinaryStreamRef(const WritableBinaryStreamRef &Other);
// Use WritableBinaryStreamRef.slice() instead.
WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint32_t Offset,
@@ -159,17 +193,13 @@ public:
/// \returns a success error code if the data could fit within the underlying
/// stream at the specified location and the implementation could write the
/// data, and an appropriate error code otherwise.
- Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const {
- if (auto EC = checkOffset(Offset, Data.size()))
- return EC;
-
- return Stream->writeBytes(ViewOffset + Offset, Data);
- }
+ Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const;
- operator BinaryStreamRef() { return BinaryStreamRef(*Stream); }
+ /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef.
+ operator BinaryStreamRef() const;
/// \brief For buffered streams, commits changes to the backing store.
- Error commit() { return Stream->commit(); }
+ Error commit();
};
} // end namespace llvm
diff --git a/include/llvm/Support/BinaryStreamWriter.h b/include/llvm/Support/BinaryStreamWriter.h
index 1b61c32a2541..a4495a1ce27d 100644
--- a/include/llvm/Support/BinaryStreamWriter.h
+++ b/include/llvm/Support/BinaryStreamWriter.h
@@ -32,7 +32,20 @@ namespace llvm {
class BinaryStreamWriter {
public:
BinaryStreamWriter() = default;
- explicit BinaryStreamWriter(WritableBinaryStreamRef Stream);
+ explicit BinaryStreamWriter(WritableBinaryStreamRef Ref);
+ explicit BinaryStreamWriter(WritableBinaryStream &Stream);
+ explicit BinaryStreamWriter(MutableArrayRef<uint8_t> Data,
+ llvm::support::endianness Endian);
+
+ BinaryStreamWriter(const BinaryStreamWriter &Other)
+ : Stream(Other.Stream), Offset(Other.Offset) {}
+
+ BinaryStreamWriter &operator=(const BinaryStreamWriter &Other) {
+ Stream = Other.Stream;
+ Offset = Other.Offset;
+ return *this;
+ }
+
virtual ~BinaryStreamWriter() {}
/// Write the bytes specified in \p Buffer to the underlying stream.
diff --git a/include/llvm/Support/FileSystem.h b/include/llvm/Support/FileSystem.h
index e3c5de7fbe64..7caefb5359b8 100644
--- a/include/llvm/Support/FileSystem.h
+++ b/include/llvm/Support/FileSystem.h
@@ -261,7 +261,7 @@ struct file_magic {
coff_object, ///< COFF object file
coff_import_library, ///< COFF import library
pecoff_executable, ///< PECOFF executable file
- windows_resource, ///< Windows compiled resource file (.rc)
+ windows_resource, ///< Windows compiled resource file (.res)
wasm_object ///< WebAssembly Object file
};