summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--slang.h10
-rw-r--r--source/core/slang-array-view.h220
-rw-r--r--source/core/slang-string-slice-pool.cpp78
-rw-r--r--source/core/slang-string-slice-pool.h61
-rw-r--r--source/slang/slang-diagnostic-defs.h2
-rw-r--r--source/slang/slang-emit-cpp.cpp3
-rw-r--r--source/slang/slang-ir-link.cpp2
-rw-r--r--source/slang/slang-ir-serialize-types.cpp17
-rw-r--r--source/slang/slang-ir-serialize-types.h3
-rw-r--r--source/slang/slang-ir-serialize.cpp7
-rw-r--r--source/slang/slang-ir-serialize.h4
-rw-r--r--source/slang/slang-ir-string-hash.cpp17
-rw-r--r--source/slang/slang-lower-to-ir.cpp5
-rw-r--r--source/slang/slang-reflection.cpp10
-rw-r--r--source/slang/slang-source-loc.h3
-rw-r--r--source/slang/slang-type-layout.h4
-rw-r--r--tools/slang-reflection-test/slang-reflection-test-main.cpp2
17 files changed, 289 insertions, 159 deletions
diff --git a/slang.h b/slang.h
index d11c4b7f2..233b418c3 100644
--- a/slang.h
+++ b/slang.h
@@ -2102,16 +2102,16 @@ extern "C"
SLANG_API SlangUInt spReflection_getHashedStringCount(
SlangReflection* reflection);
- /// Get a hashed string. The number of chars is writtent in outCount. Note the count does *NOT* including terminating 0,
- /// and the returned pointer will not generally have a terminating zero.
+ /// Get a hashed string. The number of chars is written in outCount.
+ /// The count does *NOT* including terminating 0. The returned string will be 0 terminated.
SLANG_API const char* spReflection_getHashedString(
SlangReflection* reflection,
SlangUInt index,
size_t* outCount);
- /// Calculate a string hash.
- // Count should *NOT* include terminating zero.
- SLANG_API int spCalcStringHash(const char* chars, size_t count);
+ /// Compute a string hash.
+ /// Count should *NOT* include terminating zero.
+ SLANG_API int spComputeStringHash(const char* chars, size_t count);
#ifdef __cplusplus
}
diff --git a/source/core/slang-array-view.h b/source/core/slang-array-view.h
index 8b653f4c7..56c936073 100644
--- a/source/core/slang-array-view.h
+++ b/source/core/slang-array-view.h
@@ -5,108 +5,148 @@
namespace Slang
{
- template<typename T>
- class ArrayView
- {
- private:
- T* m_buffer;
- int m_count;
- public:
- const T* begin() const { return m_buffer; }
- T* begin() { return m_buffer; }
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ConstArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+ template<typename T>
+ class ConstArrayView
+ {
+ public:
+ const T* begin() const { return m_buffer; }
+
const T* end() const { return m_buffer + m_count; }
- T* end() { return m_buffer + m_count; }
- public:
- ArrayView():
- m_buffer(nullptr),
- m_count(0)
+ inline Index getCount() const { return m_count; }
+
+ inline const T& operator [](Index idx) const
+ {
+ SLANG_ASSERT(idx >= 0 && idx < m_count);
+ return m_buffer[idx];
+ }
+
+ inline const T* getBuffer() const { return m_buffer; }
+
+ template<typename T2>
+ Index indexOf(const T2& val) const
+ {
+ for (Index i = 0; i < m_count; i++)
+ {
+ if (m_buffer[i] == val)
+ return i;
+ }
+ return -1;
+ }
+
+ template<typename T2>
+ Index lastIndexOf(const T2& val) const
+ {
+ for (Index i = m_count - 1; i >= 0; i--)
+ {
+ if (m_buffer[i] == val)
+ return i;
+ }
+ return -1;
+ }
+
+ template<typename Func>
+ Index findFirstIndex(const Func& predicate) const
+ {
+ for (Index i = 0; i < m_count; i++)
+ {
+ if (predicate(m_buffer[i]))
+ return i;
+ }
+ return -1;
+ }
+
+ template<typename Func>
+ Index findLastIndex(const Func& predicate) const
+ {
+ for (Index i = m_count - 1; i >= 0; i--)
+ {
+ if (predicate(m_buffer[i]))
+ return i;
+ }
+ return -1;
+ }
+
+ ConstArrayView() :
+ m_buffer(nullptr),
+ m_count(0)
{
- }
- ArrayView(T& singleObj):
- m_buffer(&singleObj),
- m_count(1)
+ }
+
+ ConstArrayView(const T* buffer, Index count) :
+ m_buffer(const_cast<T*>(buffer)),
+ m_count(count)
{
- }
- ArrayView(T* buffer, int size):
+ }
+
+ protected:
+ ConstArrayView(T* buffer, Index count) :
m_buffer(buffer),
- m_count(size)
- {
- }
-
- inline int getCount() const { return m_count; }
-
- inline const T& operator [](int idx) const
- {
- SLANG_ASSERT(idx >= 0 && idx <= m_count);
- return m_buffer[idx];
- }
- inline T& operator [](int idx)
+ m_count(count)
{
- SLANG_ASSERT(idx >= 0 && idx <= m_count);
+ }
+
+ T* m_buffer; ///< Note that this isn't const, as is used for derived class ArrayView also
+ Index m_count;
+ };
+
+ template<typename T>
+ ConstArrayView<T> makeConstArrayView(const T& obj)
+ {
+ return ConstArrayView<T>(&obj, 1);
+ }
+
+ template<typename T>
+ ConstArrayView<T> makeConstArrayView(const T* buffer, Index count)
+ {
+ return ConstArrayView<T>(buffer, count);
+ }
+
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArrayView !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+ template<typename T>
+ class ArrayView: public ConstArrayView<T>
+ {
+ public:
+ typedef ConstArrayView<T> Super;
+
+ using Super::m_buffer;
+ using Super::m_count;
+
+ using Super::begin;
+ T* begin() { return m_buffer; }
+
+ using Super::end;
+ T* end() { return m_buffer + m_count; }
+
+ using Super::operator[];
+ inline T& operator [](Index idx)
+ {
+ SLANG_ASSERT(idx >= 0 && idx < m_count);
return m_buffer[idx];
}
- inline const T* getBuffer() const { return m_buffer; }
+ using Super::getBuffer;
inline T* getBuffer() { return m_buffer; }
- template<typename T2>
- int indexOf(const T2 & val) const
- {
- for (int i = 0; i < m_count; i++)
- {
- if (m_buffer[i] == val)
- return i;
- }
- return -1;
- }
-
- template<typename T2>
- int lastIndexOf(const T2 & val) const
- {
- for (int i = m_count - 1; i >= 0; i--)
- {
- if (m_buffer[i] == val)
- return i;
- }
- return -1;
- }
-
- template<typename Func>
- int findFirstIndex(const Func& predicate) const
- {
- for (int i = 0; i < m_count; i++)
- {
- if (predicate(m_buffer[i]))
- return i;
- }
- return -1;
- }
-
- template<typename Func>
- int findLastIndex(const Func& predicate) const
- {
- for (int i = m_count - 1; i >= 0; i--)
- {
- if (predicate(m_buffer[i]))
- return i;
- }
- return -1;
- }
- };
-
- template<typename T>
- ArrayView<T> makeArrayView(T& obj)
- {
- return ArrayView<T>(obj);
- }
-
- template<typename T>
- ArrayView<T> makeArrayView(T* buffer, int count)
- {
- return ArrayView<T>(buffer, count);
- }
+ ArrayView() : Super() {}
+ ArrayView(T* buffer, Index size) :Super(buffer, size) {}
+ };
+
+ template<typename T>
+ ArrayView<T> makeArrayView(T& obj)
+ {
+ return ArrayView<T>(&obj, 1);
+ }
+
+ template<typename T>
+ ArrayView<T> makeArrayView(T* buffer, Index count)
+ {
+ return ArrayView<T>(buffer, count);
+ }
}
#endif
diff --git a/source/core/slang-string-slice-pool.cpp b/source/core/slang-string-slice-pool.cpp
index e3d9d8809..7ea15e0d6 100644
--- a/source/core/slang-string-slice-pool.cpp
+++ b/source/core/slang-string-slice-pool.cpp
@@ -5,10 +5,10 @@ namespace Slang {
/* static */ const StringSlicePool::Handle StringSlicePool::kNullHandle;
/* static */ const StringSlicePool::Handle StringSlicePool::kEmptyHandle;
-/* static */const int StringSlicePool::kNumDefaultHandles;
+/* static */const Index StringSlicePool::kDefaultHandlesCount;
-
-StringSlicePool::StringSlicePool() :
+StringSlicePool::StringSlicePool(Style style) :
+ m_style(style),
m_arena(1024)
{
clear();
@@ -16,15 +16,29 @@ StringSlicePool::StringSlicePool() :
void StringSlicePool::clear()
{
- m_slices.setCount(2);
-
- m_slices[0] = UnownedStringSlice((const char*)nullptr, (const char*)nullptr);
- m_slices[1] = UnownedStringSlice::fromLiteral("");
-
- // Add the empty entry
- m_map.Add(m_slices[1], kEmptyHandle);
-
m_map.Clear();
+
+ switch (m_style)
+ {
+ case Style::Default:
+ {
+ // Add the defaults
+ m_slices.setCount(2);
+
+ m_slices[0] = UnownedStringSlice((const char*)nullptr, (const char*)nullptr);
+ m_slices[1] = UnownedStringSlice::fromLiteral("");
+
+ // Add the empty entry
+ m_map.Add(m_slices[1], kEmptyHandle);
+ break;
+ }
+ case Style::Empty:
+ {
+ // There are no defaults
+ m_slices.clear();
+ break;
+ }
+ }
}
StringSlicePool::Handle StringSlicePool::add(const Slice& slice)
@@ -47,31 +61,53 @@ StringSlicePool::Handle StringSlicePool::add(const Slice& slice)
StringSlicePool::Handle StringSlicePool::add(StringRepresentation* stringRep)
{
- if (stringRep == nullptr)
+ if (stringRep == nullptr && m_style == Style::Default)
{
return kNullHandle;
}
return add(StringRepresentation::asSlice(stringRep));
}
-
StringSlicePool::Handle StringSlicePool::add(const char* chars)
{
- if (!chars)
+ switch (m_style)
{
- return kNullHandle;
- }
- if (chars[0] == 0)
- {
- return kEmptyHandle;
+ case Style::Default:
+ {
+ if (!chars)
+ {
+ return kNullHandle;
+ }
+ if (chars[0] == 0)
+ {
+ return kEmptyHandle;
+ }
+ break;
+ }
+ case Style::Empty:
+ {
+ if (chars == nullptr)
+ {
+ SLANG_ASSERT(!"Empty style doesn't support nullptr");
+ // Return an invalid handle
+ return Handle(~HandleIntegral(0));
+ }
+ }
}
+
return add(UnownedStringSlice(chars));
}
-int StringSlicePool::findIndex(const Slice& slice) const
+Index StringSlicePool::findIndex(const Slice& slice) const
{
const Handle* handlePtr = m_map.TryGetValue(slice);
- return handlePtr ? int(*handlePtr) : -1;
+ return handlePtr ? Index(*handlePtr) : -1;
+}
+ConstArrayView<UnownedStringSlice> StringSlicePool::getAdded() const
+{
+ const Index firstIndex = getFirstAddedIndex();
+ return makeConstArrayView(m_slices.getBuffer() + firstIndex, m_slices.getCount() - firstIndex);
}
+
} // namespace Slang
diff --git a/source/core/slang-string-slice-pool.h b/source/core/slang-string-slice-pool.h
index 4d5f91e37..2e4b41333 100644
--- a/source/core/slang-string-slice-pool.h
+++ b/source/core/slang-string-slice-pool.h
@@ -6,24 +6,52 @@
#include "slang-list.h"
#include "slang-memory-arena.h"
#include "slang-dictionary.h"
+#include "slang-array-view.h"
namespace Slang {
+/* Holds a unique set of slices.
+
+Note that all slices (except kNullHandle) are stored with terminating zeros.
+
+The default handles kNullHandle, kEmptyHandle can only be used on a StringSlicePool
+initialized with the Style::Default. Not doing so will return an undefined result.
+
+TODO(JS):
+An argument could be made to make different classes, perhaps deriving from a base class
+that exhibited the two behaviors. That doing so would make the default handles defined
+for that class for example.
+
+This is a little awkward in practice, because behavior of some methods need to change
+(like adding a c string with nullptr, or clearing, as well as some other perhaps less necessary
+optimizations). This could be achieved via virtual functions, but this all seems overkill.
+*/
class StringSlicePool
{
public:
+ typedef StringSlicePool ThisType;
+ typedef uint32_t HandleIntegral;
+
+ enum class Style
+ {
+ Default, ///< Default style - has default handles (like kNullHandle and kEmptyHandle)
+ Empty, ///< Empty style - has no handles by default. Using default handles will likely produce the wrong result.
+ };
- /// Handle of 0 is null. If accessed will be returned as the empty string
- enum class Handle : uint32_t;
+ enum class Handle : HandleIntegral;
typedef UnownedStringSlice Slice;
+ /// The following default handles *only* apply if constructed with the Style::Default
+
+ /// Handle of 0 is null. If accessed will be returned as the empty string with nullptr the chars
static const Handle kNullHandle = Handle(0);
+ /// Handle of 1 is the empty string.
static const Handle kEmptyHandle = Handle(1);
- static const int kNumDefaultHandles = 2;
+ static const Index kDefaultHandlesCount = 2;
/// Returns the index of a slice, if contained, or -1 if not found
- int findIndex(const Slice& slice) const;
+ Index findIndex(const Slice& slice) const;
/// True if has the slice
bool has(const Slice& slice) { return findIndex(slice) >= 0; }
@@ -46,17 +74,32 @@ public:
const List<UnownedStringSlice>& getSlices() const { return m_slices; }
/// Get the number of slices
- int getNumSlices() const { return int(m_slices.getCount()); }
+ Index getSlicesCount() const { return m_slices.getCount(); }
+
+ /// Returns true if the handle is a default one. Only meaningful on a Style::Default.
+ bool isDefaultHandle(Handle handle) const { SLANG_ASSERT(m_style == Style::Default && Index(handle) >= 0); return Index(handle) < kDefaultHandlesCount; }
/// Convert a handle to and index. (A handle is just an index!)
- static int asIndex(Handle handle) { return int(handle); }
- /// Returns true if the handle is to a slice that contains characters (ie not null or empty)
- static bool hasContents(Handle handle) { return int(handle) >= kNumDefaultHandles; }
+ static Index asIndex(Handle handle) { return Index(handle); }
+
+ /// Get the style of the pool
+ Style getStyle() const { return m_style; }
+
+ /// Get all the added slices (does not have default slices, if there are any)
+ ConstArrayView<UnownedStringSlice> getAdded() const;
+
+ /// Get the index of the first added handle
+ Index getFirstAddedIndex() const { return m_style == Style::Default ? kDefaultHandlesCount : 0; }
/// Ctor
- StringSlicePool();
+ explicit StringSlicePool(Style style);
protected:
+ // Disable copy ctor and assignment
+ StringSlicePool(const ThisType& rhs) = delete;
+ void operator=(const ThisType& rhs) = delete;
+
+ Style m_style;
List<UnownedStringSlice> m_slices;
Dictionary<UnownedStringSlice, Handle> m_map;
MemoryArena m_arena;
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index 15121babf..67b4be449 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -250,7 +250,7 @@ DIAGNOSTIC(30049, Note, thisIsImmutableByDefault, "a 'this' parameter is an imm
DIAGNOSTIC(30051, Error, invalidValueForArgument, "invalid value for argument '$0'")
DIAGNOSTIC(30052, Error, invalidSwizzleExpr, "invalid swizzle pattern '$0' on type '$1'")
-DIAGNOSTIC(30043, Error, getStringHashRequiresStringLiteral, "getStringHash parameter can only accept a non-zero length string literal")
+DIAGNOSTIC(30043, Error, getStringHashRequiresStringLiteral, "getStringHash parameter can only accept a string literal")
DIAGNOSTIC(30060, Error, expectedAType, "expected a type got a '$0'")
diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp
index 5592ba26a..e7f11fc92 100644
--- a/source/slang/slang-emit-cpp.cpp
+++ b/source/slang/slang-emit-cpp.cpp
@@ -1785,7 +1785,8 @@ void CPPSourceEmitter::emitOperationCall(IntrinsicOp op, IRInst* inst, IRUse* op
/* !!!!!!!!!!!!!!!!!!!!!! CPPSourceEmitter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
CPPSourceEmitter::CPPSourceEmitter(const Desc& desc):
- Super(desc)
+ Super(desc),
+ m_slicePool(StringSlicePool::Style::Default)
{
m_semanticUsedFlags = 0;
//m_semanticUsedFlags = SemanticUsedFlag::GroupID | SemanticUsedFlag::GroupThreadID | SemanticUsedFlag::DispatchThreadID;
diff --git a/source/slang/slang-ir-link.cpp b/source/slang/slang-ir-link.cpp
index 0678b5962..6399e3f6b 100644
--- a/source/slang/slang-ir-link.cpp
+++ b/source/slang/slang-ir-link.cpp
@@ -1393,7 +1393,7 @@ LinkedIR linkIR(
// Combine all of the contents of IRGlobalHashedStringLiterals
{
- StringSlicePool pool;
+ StringSlicePool pool(StringSlicePool::Style::Empty);
IRBuilder& builder = sharedContext->builderStorage;
for (IRModule* irModule : irModules)
{
diff --git a/source/slang/slang-ir-serialize-types.cpp b/source/slang/slang-ir-serialize-types.cpp
index 998fc9d7f..10e2776b2 100644
--- a/source/slang/slang-ir-serialize-types.cpp
+++ b/source/slang/slang-ir-serialize-types.cpp
@@ -77,8 +77,8 @@ void StringRepresentationCache::init(const List<char>* stringTable, NamePool* na
m_scopeManager = scopeManager;
// Decode the table
- m_entries.setCount(StringSlicePool::kNumDefaultHandles);
- SLANG_COMPILE_TIME_ASSERT(StringSlicePool::kNumDefaultHandles == 2);
+ m_entries.setCount(StringSlicePool::kDefaultHandlesCount);
+ SLANG_COMPILE_TIME_ASSERT(StringSlicePool::kDefaultHandlesCount == 2);
{
Entry& entry = m_entries[0];
@@ -200,15 +200,14 @@ char* StringRepresentationCache::getCStr(Handle handle)
/* static */void SerialStringTableUtil::encodeStringTable(const StringSlicePool& pool, List<char>& stringTable)
{
// Skip the default handles -> nothing is encoded via them
- return encodeStringTable(pool.getSlices().begin() + StringSlicePool::kNumDefaultHandles, pool.getNumSlices() - StringSlicePool::kNumDefaultHandles, stringTable);
+ return encodeStringTable(pool.getAdded(), stringTable);
}
-/* static */void SerialStringTableUtil::encodeStringTable(const UnownedStringSlice* slices, size_t numSlices, List<char>& stringTable)
+/* static */void SerialStringTableUtil::encodeStringTable(const ConstArrayView<UnownedStringSlice>& slices, List<char>& stringTable)
{
stringTable.clear();
- for (size_t i = 0; i < numSlices; ++i)
+ for (const auto& slice : slices)
{
- const UnownedStringSlice slice = slices[i];
const int len = int(slice.size());
// We need to write into the the string array
@@ -251,19 +250,19 @@ char* StringRepresentationCache::getCStr(Handle handle)
/* static */void SerialStringTableUtil::calcStringSlicePoolMap(const List<UnownedStringSlice>& slices, StringSlicePool& pool, List<StringSlicePool::Handle>& indexMapOut)
{
- SLANG_ASSERT(slices.getCount() >= StringSlicePool::kNumDefaultHandles);
+ SLANG_ASSERT(slices.getCount() >= StringSlicePool::kDefaultHandlesCount);
SLANG_ASSERT(slices[int(StringSlicePool::kNullHandle)] == "" && slices[int(StringSlicePool::kNullHandle)].begin() == nullptr);
SLANG_ASSERT(slices[int(StringSlicePool::kEmptyHandle)] == "");
indexMapOut.setCount(slices.getCount());
// Set up all of the defaults
- for (int i = 0; i < StringSlicePool::kNumDefaultHandles; ++i)
+ for (int i = 0; i < StringSlicePool::kDefaultHandlesCount; ++i)
{
indexMapOut[i] = StringSlicePool::Handle(i);
}
const Index numSlices = slices.getCount();
- for (Index i = StringSlicePool::kNumDefaultHandles; i < numSlices ; ++i)
+ for (Index i = StringSlicePool::kDefaultHandlesCount; i < numSlices ; ++i)
{
indexMapOut[i] = pool.add(slices[i]);
}
diff --git a/source/slang/slang-ir-serialize-types.h b/source/slang/slang-ir-serialize-types.h
index 0de9769c5..f4340e37e 100644
--- a/source/slang/slang-ir-serialize-types.h
+++ b/source/slang/slang-ir-serialize-types.h
@@ -5,6 +5,7 @@
#include "../core/slang-object-scope-manager.h"
#include "../core/slang-riff.h"
#include "../core/slang-string-slice-pool.h"
+#include "../core/slang-array-view.h"
#include "slang-name.h"
#include "slang-source-loc.h"
@@ -59,7 +60,7 @@ struct SerialStringTableUtil
{
/// Convert a pool into a string table
static void encodeStringTable(const StringSlicePool& pool, List<char>& stringTable);
- static void encodeStringTable(const UnownedStringSlice* slices, size_t numSlices, List<char>& stringTable);
+ static void encodeStringTable(const ConstArrayView<UnownedStringSlice>& slices, List<char>& stringTable);
/// Appends the decoded strings into slicesOut
static void appendDecodedStringTable(const List<char>& stringTable, List<UnownedStringSlice>& slicesOut);
/// Decodes a string table (and does so such that the indices are compatible with StringSlicePool)
diff --git a/source/slang/slang-ir-serialize.cpp b/source/slang/slang-ir-serialize.cpp
index a6ed35cc6..5d78f76f8 100644
--- a/source/slang/slang-ir-serialize.cpp
+++ b/source/slang/slang-ir-serialize.cpp
@@ -85,9 +85,12 @@ void IRSerialWriter::_addDebugSourceLocRun(SourceLoc sourceLoc, uint32_t startIn
adjustedLineInfo.m_lineInfo = lineInfo;
adjustedLineInfo.m_pathStringIndex = Ser::kNullStringIndex;
- if (StringSlicePool::hasContents(entry.m_pathHandle))
+ const auto& pool = sourceView->getSourceManager()->getStringSlicePool();
+ SLANG_ASSERT(pool.getStyle() == StringSlicePool::Style::Default);
+
+ if (!pool.isDefaultHandle(entry.m_pathHandle))
{
- UnownedStringSlice slice = sourceView->getSourceManager()->getStringSlicePool().getSlice(entry.m_pathHandle);
+ UnownedStringSlice slice = pool.getSlice(entry.m_pathHandle);
SLANG_ASSERT(slice.size() > 0);
adjustedLineInfo.m_pathStringIndex = Ser::StringIndex(m_debugStringSlicePool.add(slice));
}
diff --git a/source/slang/slang-ir-serialize.h b/source/slang/slang-ir-serialize.h
index 7e80aeaad..94c0666bb 100644
--- a/source/slang/slang-ir-serialize.h
+++ b/source/slang/slang-ir-serialize.h
@@ -56,7 +56,9 @@ struct IRSerialWriter
StringSlicePool& getDebugStringPool() { return m_debugStringSlicePool; }
IRSerialWriter() :
- m_serialData(nullptr)
+ m_serialData(nullptr),
+ m_stringSlicePool(StringSlicePool::Style::Default),
+ m_debugStringSlicePool(StringSlicePool::Style::Default)
{}
protected:
diff --git a/source/slang/slang-ir-string-hash.cpp b/source/slang/slang-ir-string-hash.cpp
index b9e2d4045..0a15ba52d 100644
--- a/source/slang/slang-ir-string-hash.cpp
+++ b/source/slang/slang-ir-string-hash.cpp
@@ -47,7 +47,7 @@ void replaceGetStringHash(IRModule* module, SharedIRBuilder& sharedBuilder, Stri
void replaceGetStringHashWithGlobal(IRModule* module, SharedIRBuilder& sharedBuilder)
{
- StringSlicePool pool;
+ StringSlicePool pool(StringSlicePool::Style::Empty);
replaceGetStringHash(module, sharedBuilder, pool);
addGlobalHashedStringLiterals(pool, sharedBuilder);
}
@@ -56,7 +56,7 @@ void findGlobalHashedStringLiterals(IRModule* module, StringSlicePool& pool)
{
IRModuleInst* moduleInst = module->getModuleInst();
- for (IRInst* child = moduleInst->getFirstDecorationOrChild(); child; child = child->getNextInst())
+ for(IRInst* child : moduleInst->getChildren())
{
if (IRGlobalHashedStringLiterals* hashedStringLits = as<IRGlobalHashedStringLiterals>(child))
{
@@ -72,7 +72,8 @@ void findGlobalHashedStringLiterals(IRModule* module, StringSlicePool& pool)
void addGlobalHashedStringLiterals(const StringSlicePool& pool, SharedIRBuilder& sharedBuilder)
{
- if (pool.getNumSlices() <= StringSlicePool::kNumDefaultHandles)
+ auto slices = pool.getAdded();
+ if (slices.getCount() == 0)
{
return;
}
@@ -86,18 +87,16 @@ void addGlobalHashedStringLiterals(const StringSlicePool& pool, SharedIRBuilder&
// We need to add a global instruction that references all of these string literals
builder.setInsertInto(module->getModuleInst());
- Index numSlices = Index(pool.getNumSlices() - StringSlicePool::kNumDefaultHandles);
+ const Index slicesCount = slices.getCount();
- IRInst* globalHashedInst = createEmptyInst(module, kIROp_GlobalHashedStringLiterals, int(numSlices));
+ IRInst* globalHashedInst = createEmptyInst(module, kIROp_GlobalHashedStringLiterals, int(slicesCount));
builder.addInst(globalHashedInst);
auto operands = globalHashedInst->getOperands();
- for (Index i = 0; i < numSlices; ++i)
+ for (Index i = 0; i < slicesCount; ++i)
{
- UnownedStringSlice slice = pool.getSlice(StringSlicePool::Handle(i + StringSlicePool::kNumDefaultHandles));
- IRStringLit* stringLit = builder.getStringValue(slice);
-
+ IRStringLit* stringLit = builder.getStringValue(slices[i]);
operands[i].init(globalHashedInst, stringLit);
}
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index 8b8a67f1e..2ea4f3d32 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -783,13 +783,10 @@ LoweredValInfo emitCallToDeclRef(
case kIROp_GetStringHash:
{
IRStringLit* stringLit = as<IRStringLit>(args[0]);
-
- if (stringLit == nullptr || stringLit->getStringSlice() == UnownedStringSlice())
+ if (stringLit == nullptr)
{
auto sink = context->getSink();
-
sink->diagnose(funcDecl, Diagnostics::getStringHashRequiresStringLiteral);
-
return LoweredValInfo();
}
diff --git a/source/slang/slang-reflection.cpp b/source/slang/slang-reflection.cpp
index 306c45c14..8901f6bdd 100644
--- a/source/slang/slang-reflection.cpp
+++ b/source/slang/slang-reflection.cpp
@@ -1504,7 +1504,8 @@ SLANG_API SlangUInt spReflection_getHashedStringCount(
SlangReflection* reflection)
{
auto programLayout = convert(reflection);
- return programLayout->hashedStringLiteralPool.getNumSlices() - StringSlicePool::kNumDefaultHandles;
+ auto slices = programLayout->hashedStringLiteralPool.getAdded();
+ return slices.getCount();
}
SLANG_API const char* spReflection_getHashedString(
@@ -1513,12 +1514,15 @@ SLANG_API const char* spReflection_getHashedString(
size_t* outCount)
{
auto programLayout = convert(reflection);
- UnownedStringSlice slice = programLayout->hashedStringLiteralPool.getSlice(StringSlicePool::Handle(index + StringSlicePool::kNumDefaultHandles));
+
+ auto slices = programLayout->hashedStringLiteralPool.getAdded();
+ auto slice = slices[Index(index)];
+
*outCount = slice.size();
return slice.begin();
}
-SLANG_API int spCalcStringHash(const char* chars, size_t count)
+SLANG_API int spComputeStringHash(const char* chars, size_t count)
{
UnownedStringSlice slice(chars, count);
return GetHashCode(slice);
diff --git a/source/slang/slang-source-loc.h b/source/slang/slang-source-loc.h
index 45b94bb5b..db2dcc394 100644
--- a/source/slang/slang-source-loc.h
+++ b/source/slang/slang-source-loc.h
@@ -376,7 +376,8 @@ struct SourceManager
const List<SourceFile*>& getSourceFiles() const { return m_sourceFiles; }
SourceManager() :
- m_memoryArena(2048)
+ m_memoryArena(2048),
+ m_slicePool(StringSlicePool::Style::Default)
{}
~SourceManager();
diff --git a/source/slang/slang-type-layout.h b/source/slang/slang-type-layout.h
index 1d3892d78..afca06ce1 100644
--- a/source/slang/slang-type-layout.h
+++ b/source/slang/slang-type-layout.h
@@ -732,6 +732,10 @@ public:
TargetRequest* getTargetReq() { return targetProgram->getTargetReq(); }
ComponentType* getProgram() { return targetProgram->getProgram(); }
+ ProgramLayout():
+ hashedStringLiteralPool(StringSlicePool::Style::Empty)
+ {
+ }
// We catalog the requested entry points here,
// and any entry-point-specific parameter data
diff --git a/tools/slang-reflection-test/slang-reflection-test-main.cpp b/tools/slang-reflection-test/slang-reflection-test-main.cpp
index 9dc507250..7480b6ae9 100644
--- a/tools/slang-reflection-test/slang-reflection-test-main.cpp
+++ b/tools/slang-reflection-test/slang-reflection-test-main.cpp
@@ -1074,7 +1074,7 @@ static void emitReflectionJSON(
size_t charsCount;
const char* chars = programReflection->getHashedString(i, &charsCount);
- const int hash = spCalcStringHash(chars, charsCount);
+ const int hash = spComputeStringHash(chars, charsCount);
writeEscapedString(writer, chars, charsCount);
write(writer, ": ");