From 7ade1557627cf3f09c23c892ee227b7386f28414 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2025 15:18:57 -0300 Subject: Janitorial work on casts --- lcode.c | 8 ++++---- ldump.c | 2 +- llimits.h | 3 +++ lobject.c | 2 +- lopcodes.h | 40 ++++++++++++++++++++-------------------- lparser.c | 4 ++-- ltests.c | 30 +++++++++++++++--------------- lundump.c | 2 +- 8 files changed, 47 insertions(+), 44 deletions(-) diff --git a/lcode.c b/lcode.c index 8f658500..119d91ab 100644 --- a/lcode.c +++ b/lcode.c @@ -1317,22 +1317,22 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { lu_byte temp = cast_byte(t->u.info); /* upvalue index */ t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ lua_assert(isKstr(fs, k)); - t->u.ind.idx = cast(short, k->u.info); /* literal short string */ + t->u.ind.idx = cast_short(k->u.info); /* literal short string */ t->k = VINDEXUP; } else { /* register index of the table */ t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); if (isKstr(fs, k)) { - t->u.ind.idx = cast(short, k->u.info); /* literal short string */ + t->u.ind.idx = cast_short(k->u.info); /* literal short string */ t->k = VINDEXSTR; } else if (isCint(k)) { /* int. constant in proper range? */ - t->u.ind.idx = cast(short, k->u.ival); + t->u.ind.idx = cast_short(k->u.ival); t->k = VINDEXI; } else { - t->u.ind.idx = cast(short, luaK_exp2anyreg(fs, k)); /* register */ + t->u.ind.idx = cast_short(luaK_exp2anyreg(fs, k)); /* register */ t->k = VINDEXED; } } diff --git a/ldump.c b/ldump.c index d8fca317..79bb1dc9 100644 --- a/ldump.c +++ b/ldump.c @@ -108,7 +108,7 @@ static void dumpSize (DumpState *D, size_t sz) { static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); - dumpVarint(D, cast(size_t, x)); + dumpVarint(D, cast_sizet(x)); } diff --git a/llimits.h b/llimits.h index b1fc384b..223b5e6c 100644 --- a/llimits.h +++ b/llimits.h @@ -137,12 +137,15 @@ typedef LUAI_UACINT l_uacInt; #define cast_voidp(i) cast(void *, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) +#define cast_short(i) cast(short, (i)) #define cast_uint(i) cast(unsigned int, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) #define cast_char(i) cast(char, (i)) #define cast_charp(i) cast(char *, (i)) #define cast_sizet(i) cast(size_t, (i)) +#define cast_Integer(i) cast(lua_Integer, (i)) +#define cast_Inst(i) cast(Instruction, (i)) /* cast a signed lua_Integer to lua_Unsigned */ diff --git a/lobject.c b/lobject.c index 57fc6a91..1c32ecf7 100644 --- a/lobject.c +++ b/lobject.c @@ -618,7 +618,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } case 'I': { /* a 'lua_Integer' */ TValue num; - setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); + setivalue(&num, cast_Integer(va_arg(argp, l_uacInt))); addnum2buff(&buff, &num); break; } diff --git a/lopcodes.h b/lopcodes.h index 7511eb22..e3ac9d09 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -126,14 +126,14 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ - ((cast(Instruction, v)<f = e->t = NO_JUMP; e->k = VLOCAL; - e->u.var.vidx = cast(short, vidx); + e->u.var.vidx = cast_short(vidx); e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } @@ -495,7 +495,7 @@ static void buildvar (LexState *ls, TString *varname, expdesc *var) { luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ - var->u.ind.vidx = cast(short, info); /* mark it as a declared global */ + var->u.ind.vidx = cast_short(info); /* mark it as a declared global */ } } diff --git a/ltests.c b/ltests.c index 1517aa88..e7bc66dd 100644 --- a/ltests.c +++ b/ltests.c @@ -910,9 +910,9 @@ static int get_limits (lua_State *L) { static int mem_query (lua_State *L) { if (lua_isnone(L, 1)) { - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total)); - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks)); - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem)); + lua_pushinteger(L, cast_Integer(l_memcontrol.total)); + lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks)); + lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem)); return 3; } else if (lua_isnumber(L, 1)) { @@ -926,7 +926,7 @@ static int mem_query (lua_State *L) { int i; for (i = LUA_NUMTYPES - 1; i >= 0; i--) { if (strcmp(t, ttypename(i)) == 0) { - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.objcount[i])); + lua_pushinteger(L, cast_Integer(l_memcontrol.objcount[i])); return 1; } } @@ -1074,7 +1074,7 @@ static int hash_query (lua_State *L) { Table *t; luaL_checktype(L, 2, LUA_TTABLE); t = hvalue(obj_at(L, 2)); - lua_pushinteger(L, cast(lua_Integer, luaH_mainposition(t, o) - t->node)); + lua_pushinteger(L, cast_Integer(luaH_mainposition(t, o) - t->node)); } return 1; } @@ -1082,9 +1082,9 @@ static int hash_query (lua_State *L) { static int stacklevel (lua_State *L) { int a = 0; - lua_pushinteger(L, cast(lua_Integer, L->top.p - L->stack.p)); + lua_pushinteger(L, cast_Integer(L->top.p - L->stack.p)); lua_pushinteger(L, stacksize(L)); - lua_pushinteger(L, cast(lua_Integer, L->nCcalls)); + lua_pushinteger(L, cast_Integer(L->nCcalls)); lua_pushinteger(L, L->nci); lua_pushinteger(L, (lua_Integer)(size_t)&a); return 5; @@ -1099,9 +1099,9 @@ static int table_query (lua_State *L) { t = hvalue(obj_at(L, 1)); asize = t->asize; if (i == -1) { - lua_pushinteger(L, cast(lua_Integer, asize)); - lua_pushinteger(L, cast(lua_Integer, allocsizenode(t))); - lua_pushinteger(L, cast(lua_Integer, asize > 0 ? *lenhint(t) : 0)); + lua_pushinteger(L, cast_Integer(asize)); + lua_pushinteger(L, cast_Integer(allocsizenode(t))); + lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0)); return 3; } else if (cast_uint(i) < asize) { @@ -1157,7 +1157,7 @@ static int test_codeparam (lua_State *L) { static int test_applyparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_Integer x = luaL_checkinteger(L, 2); - lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x))); + lua_pushinteger(L, cast_Integer(luaO_applyparam(cast_byte(p), x))); return 1; } @@ -1257,7 +1257,7 @@ static int pushuserdata (lua_State *L) { static int udataval (lua_State *L) { - lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1)))); + lua_pushinteger(L, cast_st2S(cast_sizet(lua_touserdata(L, 1)))); return 1; } @@ -1294,7 +1294,7 @@ static int num2int (lua_State *L) { static int makeseed (lua_State *L) { - lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L))); + lua_pushinteger(L, cast_Integer(luaL_makeseed(L))); return 1; } @@ -1638,7 +1638,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func))); + lua_pushinteger(L1, cast_st2S(cast_sizet(func))); } else if EQ("getfield") { int t = getindex; @@ -2011,7 +2011,7 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); - lua_pushinteger(L, cast(lua_Integer, ctx)); + lua_pushinteger(L, cast_Integer(ctx)); lua_setglobal(L, "ctx"); return runC(L, L, lua_tostring(L, cast_int(ctx))); } diff --git a/lundump.c b/lundump.c index d53bfc9a..fccded7d 100644 --- a/lundump.c +++ b/lundump.c @@ -149,7 +149,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { return; } else if (size == 1) { /* previously saved string? */ - lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ + lua_Integer idx = cast_st2S(loadSize(S)); /* get its index */ TValue stv; luaH_getint(S->h, idx, &stv); /* get its value */ *sl = ts = tsvalue(&stv); -- cgit v1.2.3-55-g6feb