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-conversion.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'source/slang/slang-check-conversion.cpp') diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp index 5a9c8df12..c4efba658 100644 --- a/source/slang/slang-check-conversion.cpp +++ b/source/slang/slang-check-conversion.cpp @@ -705,7 +705,7 @@ namespace Slang CoercionSite site, Type* toType, Expr** outToExpr, - Type* fromType, + QualType fromType, Expr* fromExpr, ConversionCost* outCost) { @@ -773,7 +773,7 @@ namespace Slang auto toBase = toModified ? toModified->getBase() : toType; // auto fromModified = as(fromType); - auto fromBase = fromModified ? fromModified->getBase() : fromType; + auto fromBase = fromModified ? QualType(fromModified->getBase(), fromType.isLeftValue) : fromType; if((toModified || fromModified) && toBase->equals(fromBase)) @@ -1060,7 +1060,7 @@ namespace Slang OverloadResolveContext overloadContext; overloadContext.disallowNestedConversions = true; overloadContext.argCount = 1; - overloadContext.argTypes = &fromType; + overloadContext.argTypes = &fromType.type; overloadContext.args = &fromExpr; overloadContext.originalExpr = nullptr; @@ -1191,6 +1191,11 @@ namespace Slang } } } + if (fromType.isLeftValue) + { + // If we are implicitly casting the type of an l-value, we need to impose additional cost. + cost += kConversionCost_LValueCast; + } if(outCost) *outCost = cost; @@ -1245,7 +1250,7 @@ namespace Slang bool SemanticsVisitor::canCoerce( Type* toType, - Type* fromType, + QualType fromType, Expr* fromExpr, ConversionCost* outCost) { @@ -1380,7 +1385,7 @@ namespace Slang bool SemanticsVisitor::canConvertImplicitly( Type* toType, - Type* fromType) + QualType fromType) { auto conversionCost = getConversionCost(toType, fromType); @@ -1391,7 +1396,7 @@ namespace Slang return true; } - ConversionCost SemanticsVisitor::getConversionCost(Type* toType, Type* fromType) + ConversionCost SemanticsVisitor::getConversionCost(Type* toType, QualType fromType) { ConversionCost conversionCost = kConversionCost_Impossible; if (!canCoerce(toType, fromType, nullptr, &conversionCost)) -- cgit v1.2.3