From 453683bf44f2112719802eaac2b332d49eebd640 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 19 Aug 2024 15:03:56 -0700 Subject: Tuple swizzling, concat, comparison and `countof`. (#4856) * Tuple swizzling and element access. * Update proposal status. * Cleanup. * Fix merrge error. * Address review. --- source/slang/slang-ast-val.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'source/slang/slang-ast-val.cpp') 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(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(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(intType, newType); + return result; +} + +Val* CountOfIntVal::_substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff) +{ + int diff = 0; + auto newType = as(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(resolvedTypeArg)); +} + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WitnessLookupIntVal !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void WitnessLookupIntVal::_toTextOverride(StringBuilder& out) -- cgit v1.2.3