From 69450a2be7575aa4f984b9ae2824da0e5634c9f0 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 5 Jul 2023 13:23:14 -0400 Subject: Initial sizeof/alignof implementation. (#2954) * Initial sizeof implementation. * Small macro improvement. * Fix some typos. * Refactor NaturalSize. Add more sizeof tests. * Use _makeParseExpr to add sizeof support. * Add size-of.slang diagnostic result. * Fix typo in folding with macro change. * Add a sizeof test of This. * Some more NaturalSize coverage. * Simple alignof support. * Testing for alignof. * Added 8 bit enum to check enums values are correctly sized. * Add alignof to completion. * Lower sizeof/alignof to IR. sizeof/alignof IR pass. Tests for simple generic scenarios. * Make append handle invalid properly. Improve comments. --------- Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> --- source/slang/slang-check-expr.cpp | 83 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 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 00ece3628..9dc359a5e 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -11,6 +11,8 @@ // // * `slang-check-conversion.cpp` is responsible for the logic of handling type conversion/coercion +#include "slang-ast-natural-layout.h" + #include "slang-lookup.h" #include "slang-ast-print.h" @@ -1279,8 +1281,6 @@ namespace Slang return nullptr; } - - // Let's not constant-fold operations with more than a certain number of arguments, for simplicity static const int kMaxArgs = 8; auto argCount = getArgCount(invokeExpr); @@ -1533,6 +1533,7 @@ namespace Slang SubstExpr expr, ConstantFoldingCircularityInfo* circularityInfo) { + // Unwrap any "identity" expressions while (auto parenExpr = expr.as()) { @@ -1629,7 +1630,23 @@ namespace Slang 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); + } + return nullptr; } @@ -2145,6 +2162,7 @@ namespace Slang return rs; } + Expr* SemanticsExprVisitor::visitSelectExpr(SelectExpr* expr) { auto result = visitInvokeExpr(expr); @@ -2695,6 +2713,67 @@ namespace Slang return expr; } + static bool _isSizeOfType(Type* type) + { + if (!type) + { + return false; + } + + if (as(type) || + as(type) || + as(type) || + as(type) || + as(type)) + { + return true; + } + + if (as(type)) + { + return true; + } + + return false; + } + + Expr* SemanticsExprVisitor::visitSizeOfLikeExpr(SizeOfLikeExpr* sizeOfLikeExpr) + { + auto valueExpr = dispatch(sizeOfLikeExpr->value); + + Type* type = nullptr; + + if (as(valueExpr->type)) + { + TypeExp typeExp; + typeExp.exp = valueExpr; + + auto properTypeExpr = CoerceToProperType(typeExp); + + type = properTypeExpr.type; + } + else + { + // Is this a proper type? + TypeExp typeExp(valueExpr->type); + TypeExp properType = tryCoerceToProperType(typeExp); + + type = properType.type; + } + + if (!_isSizeOfType(type)) + { + getSink()->diagnose(sizeOfLikeExpr, Diagnostics::sizeOfArgumentIsInvalid); + + sizeOfLikeExpr->type = m_astBuilder->getErrorType(); + return sizeOfLikeExpr; + } + + sizeOfLikeExpr->sizedType = type; + + return sizeOfLikeExpr; + } + Expr* SemanticsExprVisitor::visitTypeCastExpr(TypeCastExpr * expr) { // Check the term we are applying first -- cgit v1.2.3