Adds a CompressionKind "enum" with values:
CompressionKind::Unknown ≈ 255
CompressionKind::Zlib ≈ 1
CompressionKind::ZStd ≈ 2
also note: OptionalCompressionKind is typedef'ed as Optional<CompressionKind>, and NoneType() is used to indicate no compression.
The CompressionKind "enum" has overrides for several operators:
CompressionKind::operator uint8_t() const
(Self-explanatory)
CompressionKind::operator bool() const
(true if supported, false otherwise)
bool operator==(CompressionKind a, CompressionKind b)
(Self-explanatory)
none compression is represented simply as a none type for use cases that will use Optional<CompressionKind>.
once you have a CompressionKind itself you can pass it around as a value (because a CompressionKind is just a struct containing one uint8_t (a fake "enum")).
due to my operator overloading, you can do stuff like this:
llvm::compression::OptionalCompressionKind OptionalCompressionScheme = llvm::compression::getOptionalCompressionKind(CompressionSchemeId); if (!OptionalCompressionScheme) { return llvm::MemoryBuffer::getMemBuffer(Blob, Name, true); } llvm::compression::CompressionKind CompressionScheme = *OptionalCompressionScheme; if (!CompressionScheme) { Error("compression class " + (CompressionScheme->Name + " is not available").str()); return nullptr; } SmallVector<uint8_t, 0> Uncompressed; if (llvm::Error E = CompressionScheme->decompress( llvm::arrayRefFromStringRef(Blob), Uncompressed, Record[0])) { Error("could not decompress embedded file contents: " + llvm::toString(std::move(E))); return nullptr; } return llvm::MemoryBuffer::getMemBufferCopy( llvm::toStringRef(Uncompressed), Name);
(excerpt from ASTReader.cpp)
you can even do calls like
llvm::compression::CompressionKind::Zlib->decompress(...)
(this can be useful in cases like elf decompression where you might switch on debug section type and in a switch case for DebugCompressionType::Z)
I also believe similar semantics to the nullptr suggestion have been achieved, due to the bool cast returning supported status, and the Unknown type acting as a nullptr of sorts, being always unsupported and -1 (255) as a uint8. Optional(Unknown) is returned from getOptionalCompressionKind(uint8_t) when it is not 0 (NoneType()), 1 (Optional(Zlib)), or 2 (Optional(ZStd)).
In places where explict compression must be used CompressionKind can be passed around, and possibly optional (sometimes none) compression is represented as llvm::Optional<CompressionKind> which I have type aliased as OptionalCompressionKind
We're generally trying to avoid global ctors in LLVM. So at most this should be a static local variable in a function that accesses the algorithm (though perhaps these "compression algorithm" classes shouldn't be accessible directly, and only through singleton accessors in the defined alongside - like there's no reason for LLVM to contain more than one instance of ZlibCompressionAlgorithm, I think?)