From 02bb741a8d1b4ed31a65c46b7e43d153b42a7b73 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 31 May 2023 14:28:45 -0700 Subject: Preserve type cast during AST constant folding. (#2912) * Preserve type cast during AST constant folding. Fixes #2891. * Fix. * Fix truncating. * fix test. --------- Co-authored-by: Yong He --- source/slang/slang-check-expr.cpp | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'source/slang/slang-check-expr.cpp') diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index a91ec1e98..e0084e08e 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -1385,26 +1385,12 @@ namespace Slang auto targetBasicType = as(invokeExpr.getExpr()->type.type); if (!targetBasicType) return nullptr; - switch (targetBasicType->baseType) - { - case BaseType::Bool: - resultValue = constArgVals[0] != 0; - break; - case BaseType::Int: - case BaseType::UInt: - case BaseType::UInt16: - case BaseType::Int16: - case BaseType::UInt8: - case BaseType::Int8: - case BaseType::UIntPtr: - case BaseType::IntPtr: - case BaseType::Int64: - case BaseType::UInt64: - resultValue = constArgVals[0]; - break; - default: - return nullptr; - } + auto foldVal = as( + TypeCastIntVal::tryFoldImpl(m_astBuilder, targetBasicType, argVals[0], getSink())); + if (foldVal) + return foldVal; + auto result = m_astBuilder->getOrCreate(targetBasicType, argVals[0]); + return result; } else { @@ -1619,9 +1605,23 @@ namespace Slang if(auto castExpr = expr.as()) { + auto substType = getType(m_astBuilder, expr); + if (!substType) + return nullptr; + if (!isScalarIntegerType(substType)) + return nullptr; auto val = tryConstantFoldExpr(getArg(castExpr, 0), circularityInfo); - if(val) - return val; + if (val) + { + if (!castExpr.getExpr()->type) + return nullptr; + auto foldVal = as( + TypeCastIntVal::tryFoldImpl(m_astBuilder, substType, val, getSink())); + if (foldVal) + return foldVal; + auto result = m_astBuilder->getOrCreate(substType, val); + return result; + } } else if (auto invokeExpr = expr.as()) { -- cgit v1.2.3