summaryrefslogtreecommitdiff
path: root/source/slang/slang-check-constraint.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-constraint.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-constraint.cpp')
-rw-r--r--source/slang/slang-check-constraint.cpp59
1 files changed, 52 insertions, 7 deletions
diff --git a/source/slang/slang-check-constraint.cpp b/source/slang/slang-check-constraint.cpp
index 872d2616c..642a4bf6a 100644
--- a/source/slang/slang-check-constraint.cpp
+++ b/source/slang/slang-check-constraint.cpp
@@ -715,13 +715,6 @@ DeclRef<Decl> SemanticsVisitor::trySolveConstraintSystem(
// system as being solved now, as a result of the witness we found.
}
- // Add a flat cost to all unconstrained generic params.
- for (auto typeParamDecl : genericDeclRef.getDecl()->getMembersOfType<GenericTypeParamDecl>())
- {
- if (!constrainedGenericParams.contains(typeParamDecl))
- outBaseCost += kConversionCost_UnconstraintGenericParam;
- }
-
// Make sure we haven't constructed any spurious constraints
// that we aren't able to satisfy:
for (auto c : system->constraints)
@@ -732,6 +725,58 @@ DeclRef<Decl> SemanticsVisitor::trySolveConstraintSystem(
}
}
+ // Verify that all type coercion constraints can be satisfied.
+ for (auto constraintDecl :
+ genericDeclRef.getDecl()->getMembersOfType<TypeCoercionConstraintDecl>())
+ {
+ DeclRef<TypeCoercionConstraintDecl> constraintDeclRef =
+ m_astBuilder
+ ->getGenericAppDeclRef(
+ genericDeclRef,
+ args.getArrayView().arrayView,
+ constraintDecl)
+ .as<TypeCoercionConstraintDecl>();
+ auto fromType = constraintDeclRef.substitute(m_astBuilder, constraintDecl->fromType.Ptr());
+ auto toType = constraintDeclRef.substitute(m_astBuilder, constraintDecl->toType.Ptr());
+ auto conversionCost = getConversionCost(toType, fromType);
+ if (constraintDecl->findModifier<ImplicitConversionModifier>())
+ {
+ if (conversionCost > kConversionCost_GeneralConversion)
+ {
+ // The type arguments are not implicitly convertible, return failure.
+ return DeclRef<Decl>();
+ }
+ }
+ else
+ {
+ if (conversionCost == kConversionCost_Impossible)
+ {
+ // The type arguments are not convertible, return failure.
+ return DeclRef<Decl>();
+ }
+ }
+ if (auto fromDecl = isDeclRefTypeOf<Decl>(constraintDecl->fromType))
+ {
+ constrainedGenericParams.add(fromDecl.getDecl());
+ }
+ if (auto toDecl = isDeclRefTypeOf<Decl>(constraintDecl->toType))
+ {
+ constrainedGenericParams.add(toDecl.getDecl());
+ }
+ // If we are to expand the support of type coercion constraint beyond simple builtin core
+ // module functions, then the witness should be a reference to the conversion function. For
+ // now, this isn't required, and it is not easy to get it from the coercion logic, so we
+ // leave it empty.
+ args.add(m_astBuilder->getTypeCoercionWitness(fromType, toType, DeclRef<Decl>()));
+ }
+
+ // Add a flat cost to all unconstrained generic params.
+ for (auto typeParamDecl : genericDeclRef.getDecl()->getMembersOfType<GenericTypeParamDecl>())
+ {
+ if (!constrainedGenericParams.contains(typeParamDecl))
+ outBaseCost += kConversionCost_UnconstraintGenericParam;
+ }
+
return m_astBuilder->getGenericAppDeclRef(genericDeclRef, args.getArrayView().arrayView);
}