From fd07169a80607f4be4e030ee154c776ea8727c41 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 10 Jan 2021 15:27:39 +0900 Subject: [PATCH] [util] Support casts from int/uint to size_t I don't want to do implicit down casts (size_t to uint) but I needed to be able to reference swapchain array size. --- libs/util/cexpr-type.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libs/util/cexpr-type.c b/libs/util/cexpr-type.c index f400803cb..57c7a945b 100644 --- a/libs/util/cexpr-type.c +++ b/libs/util/cexpr-type.c @@ -31,6 +31,7 @@ #include "QF/cexpr.h" #include "QF/mathlib.h" +#include "QF/qfplist.h" #include "QF/simd/vec4f.h" #include "libs/util/cexpr-parse.h" @@ -181,6 +182,26 @@ BINOP(size_t, bor, unsigned, |) BINOP(size_t, xor, unsigned, ^) BINOP(size_t, rem, unsigned, %) +static void +size_t_cast_int (const exprval_t *val1, const exprval_t *src, + exprval_t *result, exprctx_t *ctx) +{ + int val = *(int *) src->value; + if (val < 0) { + PL_Message (ctx->messages, ctx->item, "int value clamped to 0: %d", + val); + val = 0; + } + *(size_t *) result->value = val; +} + +static void +size_t_cast_uint (const exprval_t *val1, const exprval_t *src, + exprval_t *result, exprctx_t *ctx) +{ + *(size_t *) result->value = *(unsigned *) src->value; +} + UNOP(size_t, pos, unsigned, +) UNOP(size_t, neg, unsigned, -) UNOP(size_t, tnot, unsigned, !) @@ -198,6 +219,8 @@ binop_t size_t_binops[] = { { '^', &cexpr_size_t, &cexpr_size_t, size_t_xor }, { '%', &cexpr_size_t, &cexpr_size_t, size_t_rem }, { MOD, &cexpr_size_t, &cexpr_size_t, size_t_rem }, + { '=', &cexpr_int, &cexpr_size_t, size_t_cast_int }, + { '=', &cexpr_uint, &cexpr_size_t, size_t_cast_uint }, {} };