From 5c2c2cfc9918bb43225159e67a851e196e17759a Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 26 Aug 2022 20:32:53 -0400 Subject: DownstreamCompileOptions using POD types (#2381) * #include an absolute path didn't work - because paths were taken to always be relative. * Make DownstreamCompileOptions use POD types. * CharSliceAllocator -> SliceAllocator Added SliceConverter CharSliceCaster -> SliceCaster * First attempt at zero terminating around blobs. * Fix clang warning. * Add SlangTerminatedChars Make Blob implementations support it. Make most blobs 'terminated'. * Fix bug setting up sourceFiles for CommandLineDownstreamCompiler. * Traffic in TerminatedCharSlice for sourceFiles. Use ArtifactDesc to generate temporary file names for source. * Fix typo in testing for shared library/C++. --- source/compiler-core/slang-slice-allocator.cpp | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 source/compiler-core/slang-slice-allocator.cpp (limited to 'source/compiler-core/slang-slice-allocator.cpp') diff --git a/source/compiler-core/slang-slice-allocator.cpp b/source/compiler-core/slang-slice-allocator.cpp new file mode 100644 index 000000000..9985f6b19 --- /dev/null +++ b/source/compiler-core/slang-slice-allocator.cpp @@ -0,0 +1,94 @@ +// slang-slice-allocator.cpp +#include "slang-slice-allocator.h" + +#include "../core/slang-blob.h" + +namespace Slang { + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceConverter !!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +/* static */ List SliceConverter::toList(const Slice& in) +{ + List list; + const auto count = in.count; + + list.setCount(count); + for (Index i = 0; i < count; ++i) + { + list[i] = asStringSlice(in[i]); + } + return list; +} + +/* static */TerminatedCharSlice SliceConverter::toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob) +{ + const auto size = blob->getBufferSize(); + + if (size == 0) + { + return TerminatedCharSlice(); + } + + // If there is a 0 at the end byte, we are zero terminated + const char* chars = (const char*)blob->getBufferPointer(); + if (chars[size - 1] == 0) + { + return TerminatedCharSlice(chars, Count(size - 1)); + } + + // See if it has a castable interface + ComPtr castable; + if (SLANG_SUCCEEDED(blob->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef()))) + { + if (castable->castAs(SlangTerminatedChars::getTypeGuid())) + { + return TerminatedCharSlice(chars, Count(size)); + } + } + + // We are out of options, we just have to allocate with zero termination which allocateString does + auto dst = allocator.getArena().allocateString(chars, Count(size)); + return TerminatedCharSlice(dst, Count(size)); +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceAllocator !!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +TerminatedCharSlice SliceAllocator::allocate(const char* in) +{ + const size_t length = ::strlen(in); + auto dst = m_arena.allocateString(in, length); + return TerminatedCharSlice(dst, length); +} + +TerminatedCharSlice SliceAllocator::allocate(const UnownedStringSlice& slice) +{ + const auto length = slice.getLength(); + auto dst = m_arena.allocateString(slice.begin(), length); + return TerminatedCharSlice(dst, length); +} + +TerminatedCharSlice SliceAllocator::allocate(const Slice& slice) +{ + const auto count = slice.count; + auto dst = m_arena.allocateString(slice.begin(), count); + return TerminatedCharSlice(dst, count); +} + +Slice SliceAllocator::allocate(const List& in) +{ + const auto count = in.getCount(); + if (count == 0) + { + return Slice(nullptr, 0); + } + + auto dst = m_arena.allocateArray(count); + for (Index i = 0; i < count; ++i) + { + dst[i] = allocate(in[i]); + } + + return Slice(dst, count); +} + +} // namespace Slang -- cgit v1.2.3