From ce238dd878038bf857968931773cc9b10f3b225d Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Thu, 22 May 2025 22:10:42 +0300 Subject: Make sizeof(T) & alignof(T) of generic types work as compile-time constants (#7213) * Make sizeof(generic) work as compile-time constant * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/slang/slang-check-expr.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 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 ad36a7e4a..db507c060 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -2060,13 +2060,25 @@ IntVal* SemanticsVisitor::tryConstantFoldExpr( } } - if (auto countOfExpr = expr.as()) + if (auto sizeOfLikeExpr = expr.as()) { - auto type = - as(countOfExpr.getExpr()->sizedType->substitute(m_astBuilder, expr.getSubsts())); - if (type) + auto type = as( + sizeOfLikeExpr.getExpr()->sizedType->substitute(m_astBuilder, expr.getSubsts())); + + if (auto sizeOfExpr = expr.as()) + { + return as(SizeOfIntVal::tryFold(m_astBuilder, expr.getExpr()->type.type, type)); + } + else if (auto alignOfExpr = expr.as()) + { + return as( + AlignOfIntVal::tryFold(m_astBuilder, expr.getExpr()->type.type, type)); + } + else if (auto countOfExpr = expr.as()) + { return as( CountOfIntVal::tryFold(m_astBuilder, expr.getExpr()->type.type, type)); + } } // it is possible that we are referring to a generic value param @@ -2159,20 +2171,6 @@ IntVal* SemanticsVisitor::tryConstantFoldExpr( if (val) return val; } - else if (auto sizeOfLikeExpr = as(expr.getExpr())) - { - ASTNaturalLayoutContext context(getASTBuilder(), nullptr); - const auto size = context.calcSize(sizeOfLikeExpr->sizedType); - if (!size) - { - return nullptr; - } - - auto value = as(sizeOfLikeExpr) ? size.alignment : size.size; - - // We can return as an IntVal - return getASTBuilder()->getIntVal(expr.getExpr()->type, value); - } else if (auto indexExpr = expr.as()) { return tryFoldIndexExpr(indexExpr.getExpr(), kind, circularityInfo); -- cgit v1.2.3