From cb5dd19992fb77ca2be866d9c6f2f4436c8b1c1e Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 7 Sep 2023 23:01:53 -0700 Subject: Incur l-value conversion cost during overload resolution. (#3195) * Incur l-value conversion cost during overload resolution. * Fix compile error. * cleanup. --------- Co-authored-by: Yong He --- source/slang/slang-check-overload.cpp | 58 +++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 20 deletions(-) (limited to 'source/slang/slang-check-overload.cpp') diff --git a/source/slang/slang-check-overload.cpp b/source/slang/slang-check-overload.cpp index 5e626705a..ac93d6505 100644 --- a/source/slang/slang-check-overload.cpp +++ b/source/slang/slang-check-overload.cpp @@ -394,21 +394,44 @@ namespace Slang return success; } + static QualType getParamQualType(ASTBuilder* astBuilder, DeclRef param) + { + auto paramType = getType(astBuilder, param); + bool isLVal = false; + switch (getParameterDirection(param.getDecl())) + { + case kParameterDirection_InOut: + case kParameterDirection_Out: + case kParameterDirection_Ref: + isLVal = true; + break; + } + return QualType(paramType, isLVal); + } + + static QualType getParamQualType(Type* paramType) + { + if (auto paramDirType = as(paramType)) + { + if (as(paramDirType) || as(paramDirType)) + return QualType(paramDirType->getValueType(), true); + } + return paramType; + } + bool SemanticsVisitor::TryCheckOverloadCandidateTypes( OverloadResolveContext& context, OverloadCandidate& candidate) { Index argCount = context.getArgCount(); - List paramTypes; -// List> params; + List paramTypes; switch (candidate.flavor) { case OverloadCandidate::Flavor::Func: for (auto param : getParameters(m_astBuilder, candidate.item.declRef.as())) { - auto paramType = getType(m_astBuilder, param); - paramTypes.add(paramType); + paramTypes.add(getParamQualType(m_astBuilder, param)); } break; @@ -418,13 +441,7 @@ namespace Slang Count paramCount = funcType->getParamCount(); for (Index i = 0; i < paramCount; ++i) { - auto paramType = funcType->getParamType(i); - - if(auto paramDirectionType = as(paramType)) - { - paramType = paramDirectionType->getValueType(); - } - + auto paramType = getParamQualType(funcType->getParamType(i)); paramTypes.add(paramType); } } @@ -445,8 +462,8 @@ namespace Slang for (Index ii = 0; ii < argCount; ++ii) { auto& arg = context.getArg(ii); - auto argType = context.getArgType(ii); auto paramType = paramTypes[ii]; + auto argType = QualType(context.getArgType(ii), paramType.isLeftValue); if (!paramType) return false; if (!argType) @@ -1318,7 +1335,7 @@ namespace Slang DeclRef genericDeclRef, OverloadResolveContext& context, ArrayView knownGenericArgs, - List *innerParameterTypes) + List *innerParameterTypes) { // We have been asked to infer zero or more arguments to // `genericDeclRef`, in a context where it is being applied @@ -1360,13 +1377,13 @@ namespace Slang if (auto funcDeclRef = as(genericDeclRef.getDecl()->inner)) { - List paramTypes; + List paramTypes; if (!innerParameterTypes) { auto params = getParameters(m_astBuilder, funcDeclRef).toArray(); for (auto param : params) { - paramTypes.add(getType(m_astBuilder, param)); + paramTypes.add(getParamQualType(m_astBuilder, param)); } innerParameterTypes = ¶mTypes; } @@ -1408,11 +1425,12 @@ namespace Slang // // So the question is then whether a mismatch during the // unification step should be taken as an immediate failure... - + auto argType = context.getArgTypeForInference(aa, this); + auto paramType = (*innerParameterTypes)[aa]; TryUnifyTypes( constraints, - context.getArgTypeForInference(aa, this), - (*innerParameterTypes)[aa]); + QualType(argType, paramType.isLeftValue), + paramType); } } else @@ -1679,10 +1697,10 @@ namespace Slang SLANG_ASSERT(diffFuncType); // Extract parameter list from processed type. - List paramTypes; + List paramTypes; for (Index ii = 0; ii < diffFuncType->getParamCount(); ii++) - paramTypes.add(removeParamDirType(diffFuncType->getParamType(ii))); + paramTypes.add(getParamQualType(diffFuncType->getParamType(ii))); // Try to infer generic arguments, based on the updated context. OverloadResolveContext subContext = context; -- cgit v1.2.3