summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-check-conversion.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-02-20 14:48:51 -0800
committerGitHub <noreply@github.com>2025-02-20 14:48:51 -0800
commit19867ffca6dca7995c799354081219c9e76f13d1 (patch)
treeb9153428fc8b7b6f3069931cf816ad374a2e7c52 /source/slang/slang-check-conversion.cpp
parent9580e311e0cefb0f8e11afc316783a67201654eb (diff)
Simplify implicit cast ctors for vector & matrix. (#6408)
* Simplify implicit cast ctors for vector & matrix. * Fix formatting. * Fix tests. * Fix Falcor test. * Mark __builtin_cast as internal.
Diffstat (limited to 'source/slang/slang-check-conversion.cpp')
-rw-r--r--source/slang/slang-check-conversion.cpp29
1 files changed, 22 insertions, 7 deletions
diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp
index 6dda9c1ea..a9785a585 100644
--- a/source/slang/slang-check-conversion.cpp
+++ b/source/slang/slang-check-conversion.cpp
@@ -1045,11 +1045,28 @@ int getTypeBitSize(Type* t)
}
ConversionCost SemanticsVisitor::getImplicitConversionCostWithKnownArg(
- Decl* decl,
+ DeclRef<Decl> decl,
Type* toType,
Expr* arg)
{
- ConversionCost candidateCost = getImplicitConversionCost(decl);
+ ConversionCost candidateCost = getImplicitConversionCost(decl.getDecl());
+
+ if (candidateCost == kConversionCost_TypeCoercionConstraint ||
+ candidateCost == kConversionCost_TypeCoercionConstraintPlusScalarToVector)
+ {
+ if (auto genApp = as<GenericAppDeclRef>(decl.declRefBase))
+ {
+ for (auto genArg : genApp->getArgs())
+ {
+ if (auto wit = as<TypeCoercionWitness>(genArg))
+ {
+ candidateCost -= kConversionCost_TypeCoercionConstraint;
+ candidateCost += getConversionCost(wit->getToType(), wit->getFromType());
+ break;
+ }
+ }
+ }
+ }
// Fix up the cost if the operand is a const lit.
if (isScalarIntegerType(toType))
@@ -1577,10 +1594,8 @@ bool SemanticsVisitor::_coerce(
ImplicitCastMethod method;
for (auto candidate : overloadContext.bestCandidates)
{
- ConversionCost candidateCost = getImplicitConversionCostWithKnownArg(
- candidate.item.declRef.getDecl(),
- toType,
- fromExpr);
+ ConversionCost candidateCost =
+ getImplicitConversionCostWithKnownArg(candidate.item.declRef, toType, fromExpr);
if (candidateCost < bestCost)
{
method.conversionFuncOverloadCandidate = candidate;
@@ -1632,7 +1647,7 @@ bool SemanticsVisitor::_coerce(
// cost associated with the initializer we are invoking.
//
ConversionCost cost = getImplicitConversionCostWithKnownArg(
- overloadContext.bestCandidate->item.declRef.getDecl(),
+ overloadContext.bestCandidate->item.declRef,
toType,
fromExpr);