From 5ac7ba2c6d3405f1a59f4350c753ec990af8f6dc Mon Sep 17 00:00:00 2001 From: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> Date: Tue, 20 Sep 2022 12:37:33 -0700 Subject: 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( From fromValue ) { .... } A user would like to be able to call this operation like: int i = convert( 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( 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(...)` 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. --- source/slang/slang-ast-type.cpp | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) (limited to 'source/slang/slang-ast-type.cpp') diff --git a/source/slang/slang-ast-type.cpp b/source/slang/slang-ast-type.cpp index 077d6de0a..39b7a8e04 100644 --- a/source/slang/slang-ast-type.cpp +++ b/source/slang/slang-ast-type.cpp @@ -196,6 +196,8 @@ Type* DeclRefType::_createCanonicalTypeOverride() return this; } +Val* maybeSubstituteGenericParam(Val* paramVal, Decl* paramDecl, SubstitutionSet subst, int* ioDiff); + Val* DeclRefType::_substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff) { if (!subst) return this; @@ -204,41 +206,8 @@ Val* DeclRefType::_substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSe // of a generic parameter, since that is what we might be substituting... if (auto genericTypeParamDecl = as(declRef.getDecl())) { - // search for a substitution that might apply to us - for (auto s = subst.substitutions; s; s = s->outer) - { - auto genericSubst = as(s); - if (!genericSubst) - continue; - - // the generic decl associated with the substitution list must be - // the generic decl that declared this parameter - auto genericDecl = genericSubst->genericDecl; - if (genericDecl != genericTypeParamDecl->parentDecl) - continue; - - int index = 0; - for (auto m : genericDecl->members) - { - if (m == genericTypeParamDecl) - { - // We've found it, so return the corresponding specialization argument - (*ioDiff)++; - return genericSubst->getArgs()[index]; - } - else if (auto typeParam = as(m)) - { - index++; - } - else if (auto valParam = as(m)) - { - index++; - } - else - { - } - } - } + if (auto result = maybeSubstituteGenericParam(this, genericTypeParamDecl, subst, ioDiff)) + return result; } int diff = 0; DeclRef substDeclRef = declRef.substituteImpl(astBuilder, subst, &diff); -- cgit v1.2.3