summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-check-expr.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-07-05 13:23:14 -0400
committerGitHub <noreply@github.com>2023-07-05 13:23:14 -0400
commit69450a2be7575aa4f984b9ae2824da0e5634c9f0 (patch)
treed554404f441af7fd113737cae8e1bde4897a814e /source/slang/slang-check-expr.cpp
parentf9b73eab7edcedc9dc2c7825fcd4171631d14ac7 (diff)
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>
Diffstat (limited to 'source/slang/slang-check-expr.cpp')
-rw-r--r--source/slang/slang-check-expr.cpp83
1 files changed, 81 insertions, 2 deletions
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> expr,
ConstantFoldingCircularityInfo* circularityInfo)
{
+
// Unwrap any "identity" expressions
while (auto parenExpr = expr.as<ParenExpr>())
{
@@ -1629,7 +1630,23 @@ namespace Slang
if (val)
return val;
}
+ else if (auto sizeOfLikeExpr = as<SizeOfLikeExpr>(expr.getExpr()))
+ {
+ ASTNaturalLayoutContext context(getASTBuilder(), nullptr);
+ const auto size = context.calcSize(sizeOfLikeExpr->sizedType);
+ if (!size)
+ {
+ return nullptr;
+ }
+ auto value = as<AlignOfExpr>(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<ArithmeticExpressionType>(type) ||
+ as<ArrayExpressionType>(type) ||
+ as<PtrTypeBase>(type) ||
+ as<TupleType>(type) ||
+ as<GenericDeclRefType>(type))
+ {
+ return true;
+ }
+
+ if (as<DeclRefType>(type))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ Expr* SemanticsExprVisitor::visitSizeOfLikeExpr(SizeOfLikeExpr* sizeOfLikeExpr)
+ {
+ auto valueExpr = dispatch(sizeOfLikeExpr->value);
+
+ Type* type = nullptr;
+
+ if (as<TypeType>(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