Janitorial work on casts

This commit is contained in:
Roberto Ierusalimschy
2025-05-08 15:18:57 -03:00
parent d827e96f33
commit 7ade155762
8 changed files with 47 additions and 44 deletions

View File

@@ -1317,22 +1317,22 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
lu_byte temp = cast_byte(t->u.info); /* upvalue index */ lu_byte temp = cast_byte(t->u.info); /* upvalue index */
t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */
lua_assert(isKstr(fs, k)); 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; t->k = VINDEXUP;
} }
else { else {
/* register index of the table */ /* register index of the table */
t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
if (isKstr(fs, k)) { 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; t->k = VINDEXSTR;
} }
else if (isCint(k)) { /* int. constant in proper range? */ 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; t->k = VINDEXI;
} }
else { 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; t->k = VINDEXED;
} }
} }

View File

@@ -108,7 +108,7 @@ static void dumpSize (DumpState *D, size_t sz) {
static void dumpInt (DumpState *D, int x) { static void dumpInt (DumpState *D, int x) {
lua_assert(x >= 0); lua_assert(x >= 0);
dumpVarint(D, cast(size_t, x)); dumpVarint(D, cast_sizet(x));
} }

View File

@@ -137,12 +137,15 @@ typedef LUAI_UACINT l_uacInt;
#define cast_voidp(i) cast(void *, (i)) #define cast_voidp(i) cast(void *, (i))
#define cast_num(i) cast(lua_Number, (i)) #define cast_num(i) cast(lua_Number, (i))
#define cast_int(i) cast(int, (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_uint(i) cast(unsigned int, (i))
#define cast_byte(i) cast(lu_byte, (i)) #define cast_byte(i) cast(lu_byte, (i))
#define cast_uchar(i) cast(unsigned char, (i)) #define cast_uchar(i) cast(unsigned char, (i))
#define cast_char(i) cast(char, (i)) #define cast_char(i) cast(char, (i))
#define cast_charp(i) cast(char *, (i)) #define cast_charp(i) cast(char *, (i))
#define cast_sizet(i) cast(size_t, (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 */ /* cast a signed lua_Integer to lua_Unsigned */

View File

@@ -618,7 +618,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
} }
case 'I': { /* a 'lua_Integer' */ case 'I': { /* a 'lua_Integer' */
TValue num; TValue num;
setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); setivalue(&num, cast_Integer(va_arg(argp, l_uacInt)));
addnum2buff(&buff, &num); addnum2buff(&buff, &num);
break; break;
} }

View File

@@ -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 GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) ((cast_Inst(o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m) #define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0))) #define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
((cast(Instruction, v)<<pos)&MASK1(size,pos)))) ((cast_Inst(v)<<pos)&MASK1(size,pos))))
#define GETARG_A(i) getarg(i, POS_A, SIZE_A) #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A) #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
@@ -174,28 +174,28 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ) setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \ #define CREATE_ABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \ | (cast_Inst(a)<<POS_A) \
| (cast(Instruction, b)<<POS_B) \ | (cast_Inst(b)<<POS_B) \
| (cast(Instruction, c)<<POS_C) \ | (cast_Inst(c)<<POS_C) \
| (cast(Instruction, k)<<POS_k)) | (cast_Inst(k)<<POS_k))
#define CREATE_vABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \ #define CREATE_vABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \ | (cast_Inst(a)<<POS_A) \
| (cast(Instruction, b)<<POS_vB) \ | (cast_Inst(b)<<POS_vB) \
| (cast(Instruction, c)<<POS_vC) \ | (cast_Inst(c)<<POS_vC) \
| (cast(Instruction, k)<<POS_k)) | (cast_Inst(k)<<POS_k))
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ #define CREATE_ABx(o,a,bc) ((cast_Inst(o)<<POS_OP) \
| (cast(Instruction, a)<<POS_A) \ | (cast_Inst(a)<<POS_A) \
| (cast(Instruction, bc)<<POS_Bx)) | (cast_Inst(bc)<<POS_Bx))
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \ #define CREATE_Ax(o,a) ((cast_Inst(o)<<POS_OP) \
| (cast(Instruction, a)<<POS_Ax)) | (cast_Inst(a)<<POS_Ax))
#define CREATE_sJ(o,j,k) ((cast(Instruction, o) << POS_OP) \ #define CREATE_sJ(o,j,k) ((cast_Inst(o) << POS_OP) \
| (cast(Instruction, j) << POS_sJ) \ | (cast_Inst(j) << POS_sJ) \
| (cast(Instruction, k) << POS_k)) | (cast_Inst(k) << POS_k))
#if !defined(MAXINDEXRK) /* (for debugging only) */ #if !defined(MAXINDEXRK) /* (for debugging only) */

View File

@@ -276,7 +276,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) {
static void init_var (FuncState *fs, expdesc *e, int vidx) { static void init_var (FuncState *fs, expdesc *e, int vidx) {
e->f = e->t = NO_JUMP; e->f = e->t = NO_JUMP;
e->k = VLOCAL; 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; 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 */ luaK_exp2anyregup(fs, var); /* but could be a constant */
codestring(&key, varname); /* key is variable name */ codestring(&key, varname); /* key is variable name */
luaK_indexed(fs, var, &key); /* env[varname] */ 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 */
} }
} }

View File

@@ -910,9 +910,9 @@ static int get_limits (lua_State *L) {
static int mem_query (lua_State *L) { static int mem_query (lua_State *L) {
if (lua_isnone(L, 1)) { if (lua_isnone(L, 1)) {
lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total)); lua_pushinteger(L, cast_Integer(l_memcontrol.total));
lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks)); lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks));
lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem)); lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem));
return 3; return 3;
} }
else if (lua_isnumber(L, 1)) { else if (lua_isnumber(L, 1)) {
@@ -926,7 +926,7 @@ static int mem_query (lua_State *L) {
int i; int i;
for (i = LUA_NUMTYPES - 1; i >= 0; i--) { for (i = LUA_NUMTYPES - 1; i >= 0; i--) {
if (strcmp(t, ttypename(i)) == 0) { 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; return 1;
} }
} }
@@ -1074,7 +1074,7 @@ static int hash_query (lua_State *L) {
Table *t; Table *t;
luaL_checktype(L, 2, LUA_TTABLE); luaL_checktype(L, 2, LUA_TTABLE);
t = hvalue(obj_at(L, 2)); 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; return 1;
} }
@@ -1082,9 +1082,9 @@ static int hash_query (lua_State *L) {
static int stacklevel (lua_State *L) { static int stacklevel (lua_State *L) {
int a = 0; 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, 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, L->nci);
lua_pushinteger(L, (lua_Integer)(size_t)&a); lua_pushinteger(L, (lua_Integer)(size_t)&a);
return 5; return 5;
@@ -1099,9 +1099,9 @@ static int table_query (lua_State *L) {
t = hvalue(obj_at(L, 1)); t = hvalue(obj_at(L, 1));
asize = t->asize; asize = t->asize;
if (i == -1) { if (i == -1) {
lua_pushinteger(L, cast(lua_Integer, asize)); lua_pushinteger(L, cast_Integer(asize));
lua_pushinteger(L, cast(lua_Integer, allocsizenode(t))); lua_pushinteger(L, cast_Integer(allocsizenode(t)));
lua_pushinteger(L, cast(lua_Integer, asize > 0 ? *lenhint(t) : 0)); lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0));
return 3; return 3;
} }
else if (cast_uint(i) < asize) { else if (cast_uint(i) < asize) {
@@ -1157,7 +1157,7 @@ static int test_codeparam (lua_State *L) {
static int test_applyparam (lua_State *L) { static int test_applyparam (lua_State *L) {
lua_Integer p = luaL_checkinteger(L, 1); lua_Integer p = luaL_checkinteger(L, 1);
lua_Integer x = luaL_checkinteger(L, 2); 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; return 1;
} }
@@ -1257,7 +1257,7 @@ static int pushuserdata (lua_State *L) {
static int udataval (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; return 1;
} }
@@ -1294,7 +1294,7 @@ static int num2int (lua_State *L) {
static int makeseed (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; return 1;
} }
@@ -1638,7 +1638,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
} }
else if EQ("func2num") { else if EQ("func2num") {
lua_CFunction func = lua_tocfunction(L1, getindex); 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") { else if EQ("getfield") {
int t = getindex; int t = getindex;
@@ -2011,7 +2011,7 @@ static int Cfunc (lua_State *L) {
static int Cfunck (lua_State *L, int status, lua_KContext ctx) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
lua_pushstring(L, statcodes[status]); lua_pushstring(L, statcodes[status]);
lua_setglobal(L, "status"); lua_setglobal(L, "status");
lua_pushinteger(L, cast(lua_Integer, ctx)); lua_pushinteger(L, cast_Integer(ctx));
lua_setglobal(L, "ctx"); lua_setglobal(L, "ctx");
return runC(L, L, lua_tostring(L, cast_int(ctx))); return runC(L, L, lua_tostring(L, cast_int(ctx)));
} }

View File

@@ -149,7 +149,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) {
return; return;
} }
else if (size == 1) { /* previously saved string? */ 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; TValue stv;
luaH_getint(S->h, idx, &stv); /* get its value */ luaH_getint(S->h, idx, &stv); /* get its value */
*sl = ts = tsvalue(&stv); *sl = ts = tsvalue(&stv);