diff options
| author | Theresa Foley <10618364+tangent-vector@users.noreply.github.com> | 2022-09-20 12:37:33 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-20 12:37:33 -0700 |
| commit | 5ac7ba2c6d3405f1a59f4350c753ec990af8f6dc (patch) | |
| tree | 13aba4c8e57cd5cbe6e3859bea130a8091c0a13a /source/slang/slang-ast-val.cpp | |
| parent | 8e44968be297c0fa0ab00510a5e5922630d8c401 (diff) | |
Support partial inference of generic arguments (#2404)
A commonly requested feature is to be able to supply only some
of the arguments to a generic explicitly, while allowing the rest
to be inferred. A common example is a function that performs some
kind of conversion:
To convert<To, From>( From fromValue ) { .... }
A user would like to be able to call this operation like:
int i = convert<int>( 1.0f );
but the current Slang type checker requires all or none of the generic
arguments be supplied. Supplying all of the arguments is tedious:
int i = convert<int, float>( 1.0f );
In this case, the `float` type argument is redundant and could be
inferred from context. However, if the user tries to omit the generic
argument list:
int i = convert( 1.0f );
The current type-checker cannot infer the `int` type argument (even if
one might claim it *should* infer based on the desired result type).
This change adds support for the `convert<int>(...)` case, by allowing
a generic to be applied to a prefix of its explicit arguments, and then
inferring the remaining arguments from contextual information when that
"partially applied" generic is applied to value-level arguments.
Most of the changes are just plumbing: adding the notion of a partially
applied generic and then supporting them during overload resolution.
A single test case is included that covers the `convert`-style use case.
It is likely that more testing is needed to cover failure modes of this
feature.
Diffstat (limited to 'source/slang/slang-ast-val.cpp')
| -rw-r--r-- | source/slang/slang-ast-val.cpp | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/source/slang/slang-ast-val.cpp b/source/slang/slang-ast-val.cpp index a70a79535..b5291509f 100644 --- a/source/slang/slang-ast-val.cpp +++ b/source/slang/slang-ast-val.cpp @@ -115,7 +115,7 @@ HashCode GenericParamIntVal::_getHashCodeOverride() return declRef.getHashCode() ^ HashCode(0xFFFF); } -Val* GenericParamIntVal::_substituteImplOverride(ASTBuilder* /* astBuilder */, SubstitutionSet subst, int* ioDiff) +Val* maybeSubstituteGenericParam(Val* paramVal, Decl* paramDecl, SubstitutionSet subst, int* ioDiff) { // search for a substitution that might apply to us for (auto s = subst.substitutions; s; s = s->outer) @@ -127,25 +127,45 @@ Val* GenericParamIntVal::_substituteImplOverride(ASTBuilder* /* astBuilder */, S // the generic decl associated with the substitution list must be // the generic decl that declared this parameter auto genericDecl = genSubst->genericDecl; - if (genericDecl != declRef.getDecl()->parentDecl) + if (genericDecl != paramDecl->parentDecl) continue; - int index = 0; + // In some cases, we construct a `DeclRef` to a `GenericDecl` + // (or a declaration under one) that only includes argument + // values for a prefix of the parameters of the generic. + // + // If we aren't careful, we could end up indexing into the + // argument list past the available range. + // + Count argCount = genSubst->getArgs().getCount(); + + Count argIndex = 0; for (auto m : genericDecl->members) { - if (m == declRef.getDecl()) + // If we have run out of arguments, then we can stop + // iterating over the parameters, because `this` + // parameter will not be replaced with anything by + // the substituion. + // + if (argIndex >= argCount) + { + return paramVal; + } + + + if (m == paramDecl) { // We've found it, so return the corresponding specialization argument (*ioDiff)++; - return genSubst->getArgs()[index]; + return genSubst->getArgs()[argIndex]; } else if (auto typeParam = as<GenericTypeParamDecl>(m)) { - index++; + argIndex++; } else if (auto valParam = as<GenericValueParamDecl>(m)) { - index++; + argIndex++; } else { @@ -154,6 +174,15 @@ Val* GenericParamIntVal::_substituteImplOverride(ASTBuilder* /* astBuilder */, S } // Nothing found: don't substitute. + return paramVal; + +} + +Val* GenericParamIntVal::_substituteImplOverride(ASTBuilder* /* astBuilder */, SubstitutionSet subst, int* ioDiff) +{ + if (auto result = maybeSubstituteGenericParam(this, declRef.getDecl(), subst, ioDiff)) + return result; + return this; } |
