summaryrefslogtreecommitdiff
path: root/source/slang/slang-ast-val.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-08-19 15:03:56 -0700
committerGitHub <noreply@github.com>2024-08-19 15:03:56 -0700
commit453683bf44f2112719802eaac2b332d49eebd640 (patch)
treed399db4c9cba90c11980186d3df1ffcc4d423b5a /source/slang/slang-ast-val.cpp
parentecf85df6eee3da76ef54b14e4ab083f22da89e46 (diff)
Tuple swizzling, concat, comparison and `countof`. (#4856)
* Tuple swizzling and element access. * Update proposal status. * Cleanup. * Fix merrge error. * Address review.
Diffstat (limited to 'source/slang/slang-ast-val.cpp')
-rw-r--r--source/slang/slang-ast-val.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/source/slang/slang-ast-val.cpp b/source/slang/slang-ast-val.cpp
index e222e86e1..e8020aa04 100644
--- a/source/slang/slang-ast-val.cpp
+++ b/source/slang/slang-ast-val.cpp
@@ -1534,6 +1534,81 @@ Val* FuncCallIntVal::_substituteImplOverride(ASTBuilder* astBuilder, Substitutio
return this;
}
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CountOfIntVal !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+void CountOfIntVal::_toTextOverride(StringBuilder& out)
+{
+ out << "countof(";
+ getTypeArg()->toText(out);
+ out << ")";
+}
+
+Val* CountOfIntVal::tryFoldOrNull(ASTBuilder* astBuilder, Type* intType, Type* newType)
+{
+ if (auto typePack = as<ConcreteTypePack>(newType))
+ {
+ bool anyAbstract = false;
+ for (Index i = 0; i < typePack->getTypeCount(); i++)
+ {
+ if (isAbstractTypePack(typePack->getElementType(i)))
+ {
+ anyAbstract = true;
+ break;
+ }
+ }
+ if (!anyAbstract)
+ {
+ auto result = astBuilder->getIntVal(intType, typePack->getTypeCount());
+ return result;
+ }
+ }
+ else if (auto tupleType = as<TupleType>(newType))
+ {
+ bool anyAbstract = false;
+ for (Index i = 0; i < tupleType->getMemberCount(); i++)
+ {
+ if (isAbstractTypePack(tupleType->getMember(i)))
+ {
+ anyAbstract = true;
+ break;
+ }
+ }
+ if (!anyAbstract)
+ {
+ auto result = astBuilder->getIntVal(intType, tupleType->getMemberCount());
+ return result;
+ }
+ }
+ return nullptr;
+}
+
+Val* CountOfIntVal::tryFold(ASTBuilder* astBuilder, Type* intType, Type* newType)
+{
+ if (auto result = tryFoldOrNull(astBuilder, intType, newType))
+ return result;
+ auto result = astBuilder->getOrCreate<CountOfIntVal>(intType, newType);
+ return result;
+}
+
+Val* CountOfIntVal::_substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff)
+{
+ int diff = 0;
+ auto newType = as<Type>(getTypeArg()->substituteImpl(astBuilder, subst, &diff));
+ if (!diff)
+ return this;
+
+ (*ioDiff)++;
+ return tryFold(astBuilder, getType(), newType);
+}
+
+Val* CountOfIntVal::_resolveImplOverride()
+{
+ auto resolvedTypeArg = getTypeArg()->resolve();
+ if (resolvedTypeArg == getTypeArg())
+ return this;
+ return tryFold(getCurrentASTBuilder(), getType(), as<Type>(resolvedTypeArg));
+}
+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WitnessLookupIntVal !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void WitnessLookupIntVal::_toTextOverride(StringBuilder& out)