summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-slice-allocator.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2022-08-26 20:32:53 -0400
committerGitHub <noreply@github.com>2022-08-26 20:32:53 -0400
commit5c2c2cfc9918bb43225159e67a851e196e17759a (patch)
tree216009d02afe9dc17b074fdd394141ef71472268 /source/compiler-core/slang-slice-allocator.h
parentef067bef2f2188a4b3c420cbcd8d223874888ed2 (diff)
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++.
Diffstat (limited to 'source/compiler-core/slang-slice-allocator.h')
-rw-r--r--source/compiler-core/slang-slice-allocator.h103
1 files changed, 103 insertions, 0 deletions
diff --git a/source/compiler-core/slang-slice-allocator.h b/source/compiler-core/slang-slice-allocator.h
new file mode 100644
index 000000000..41ed3943a
--- /dev/null
+++ b/source/compiler-core/slang-slice-allocator.h
@@ -0,0 +1,103 @@
+// slang-slice-allocator.h
+#ifndef SLANG_SLICE_ALLOCATOR_H
+#define SLANG_SLICE_ALLOCATOR_H
+
+// Has definition of CharSlice
+#include "slang-artifact.h"
+
+#include "../core/slang-memory-arena.h"
+
+namespace Slang
+{
+
+/*
+The reason to wrap in a struct rather than have as free functions is doing so will lead to compile time
+errors with incorrect usage around temporaries.
+*/
+struct SliceCaster
+{
+ /// The slice will only be in scope whilst the string is
+ static TerminatedCharSlice asTerminatedCharSlice(const String& in) { auto unowned = in.getUnownedSlice(); return TerminatedCharSlice(unowned.begin(), unowned.getLength()); }
+
+ static CharSlice asCharSlice(const String& in) { auto unowned = in.getUnownedSlice(); return CharSlice(unowned.begin(), unowned.getLength()); }
+
+ template <typename T>
+ static Slice<T*> asSlice(const List<ComPtr<T>>& list) { return makeSlice((T*const*)list.getBuffer(), list.getCount()); }
+
+ /// Get a list as a slice
+ template <typename T>
+ static Slice<T> asSlice(const List<T>& list) { return Slice<T>(list.getBuffer(), list.getCount()); }
+
+private:
+ /// We don't want to make a temporary list into a slice..
+ template <typename T>
+ static Slice<T> asSlice(const List<T>&& list) = delete;
+ // We don't want temporaries to be 'asSliced' so disable
+ static TerminatedCharSlice asTerminatedCharSlice(const String&& in) = delete;
+ static CharSlice asCharSlice(const String&& in) = delete;
+};
+
+struct SliceAllocator;
+
+struct SliceConverter
+{
+ /// Convert into a list of strings
+ static List<String> toList(const Slice<TerminatedCharSlice>& in);
+
+ /// NOTE! the slice is only guarenteed to stay in scope whilst the blob does
+ static TerminatedCharSlice toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob);
+
+ template <typename T>
+ static List<ComPtr<T>> toComPtrList(const Slice<T*>& in)
+ {
+ ISlangUnknown* check = (T*)nullptr;
+ SLANG_UNUSED(check);
+ List<ComPtr<T>> list;
+ list.setCount(in.count);
+ for (Index i = 0; i < in.count; ++i) list[i] = ComPtr<T>(in[i]);
+ return list;
+ }
+};
+
+SLANG_FORCE_INLINE UnownedStringSlice asStringSlice(const CharSlice& slice)
+{
+ return UnownedStringSlice(slice.begin(), slice.end());
+}
+
+SLANG_FORCE_INLINE CharSlice asCharSlice(const UnownedStringSlice& slice)
+{
+ return CharSlice(slice.begin(), slice.getLength());
+}
+
+SLANG_FORCE_INLINE String asString(const CharSlice& slice)
+{
+ return String(slice.begin(), slice.end());
+}
+
+struct SliceAllocator
+{
+ TerminatedCharSlice allocate(const Slice<char>& slice);
+ TerminatedCharSlice allocate(const UnownedStringSlice& slice);
+ TerminatedCharSlice allocate(const String& in) { return allocate(in.getUnownedSlice()); }
+ TerminatedCharSlice allocate(const char* in);
+ TerminatedCharSlice allocate(const char* start, const char* end) { return allocate(UnownedStringSlice(start, end)); }
+
+ Slice<TerminatedCharSlice> allocate(const List<String>& in);
+
+ /// Get the backing arena
+ MemoryArena& getArena() { return m_arena; }
+
+ void deallocateAll() { m_arena.deallocateAll(); }
+
+ SliceAllocator():
+ m_arena(1024)
+ {
+ }
+protected:
+
+ MemoryArena m_arena;
+};
+
+} // namespace Slang
+
+#endif