Skip to content

Commit e2b15aa

Browse files
committed
janitor work on casts
1 parent 8911098 commit e2b15aa

18 files changed

+83
-76
lines changed

lapi.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lapi.c,v 2.279 2017/12/08 17:28:25 roberto Exp roberto $
2+
** $Id: lapi.c,v 2.280 2018/01/10 12:02:35 roberto Exp roberto $
33
** Lua API
44
** See Copyright Notice in lua.h
55
*/
@@ -439,7 +439,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
439439
case LUA_TTABLE: return hvalue(o);
440440
case LUA_TLCL: return clLvalue(o);
441441
case LUA_TCCL: return clCvalue(o);
442-
case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
442+
case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o)));
443443
case LUA_TTHREAD: return thvalue(o);
444444
case LUA_TUSERDATA: return getudatamem(uvalue(o));
445445
case LUA_TLIGHTUSERDATA: return pvalue(o);
@@ -685,7 +685,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
685685
lua_lock(L);
686686
t = index2value(L, idx);
687687
api_check(L, ttistable(t), "table expected");
688-
setpvalue(&k, cast(void *, p));
688+
setpvalue(&k, cast_voidp(p));
689689
setobj2s(L, L->top, luaH_get(hvalue(t), &k));
690690
api_incr_top(L);
691691
lua_unlock(L);
@@ -854,7 +854,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
854854
api_checknelems(L, 1);
855855
o = index2value(L, idx);
856856
api_check(L, ttistable(o), "table expected");
857-
setpvalue(&k, cast(void *, p));
857+
setpvalue(&k, cast_voidp(p));
858858
slot = luaH_set(L, hvalue(o), &k);
859859
setobj2t(L, slot, s2v(L->top - 1));
860860
luaC_barrierback(L, hvalue(o), s2v(L->top - 1));

lcode.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lcode.c,v 2.150 2018/01/18 16:24:31 roberto Exp roberto $
2+
** $Id: lcode.c,v 2.151 2018/01/27 16:56:33 roberto Exp roberto $
33
** Code generator for Lua
44
** See Copyright Notice in lua.h
55
*/
@@ -545,7 +545,7 @@ int luaK_stringK (FuncState *fs, TString *s) {
545545
*/
546546
static int luaK_intK (FuncState *fs, lua_Integer n) {
547547
TValue k, o;
548-
setpvalue(&k, cast(void*, cast(size_t, n)));
548+
setpvalue(&k, cast_voidp(cast_sizet(n)));
549549
setivalue(&o, n);
550550
return addk(fs, &k, &o);
551551
}

ldebug.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp roberto $
2+
** $Id: ldebug.h,v 2.15 2017/06/27 11:35:31 roberto Exp roberto $
33
** Auxiliary functions from Debug Interface module
44
** See Copyright Notice in lua.h
55
*/
@@ -11,7 +11,7 @@
1111
#include "lstate.h"
1212

1313

14-
#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
14+
#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1)
1515

1616
#define resethookcount(L) (L->hookcount = L->basehookcount)
1717

lfunc.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lfunc.h,v 2.17 2017/05/04 13:32:01 roberto Exp roberto $
2+
** $Id: lfunc.h,v 2.18 2017/06/29 15:06:44 roberto Exp roberto $
33
** Auxiliary functions to manipulate prototypes and closures
44
** See Copyright Notice in lua.h
55
*/
@@ -11,11 +11,11 @@
1111
#include "lobject.h"
1212

1313

14-
#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
15-
cast(int, sizeof(TValue)*((n)-1)))
14+
#define sizeCclosure(n) (cast_int(sizeof(CClosure)) + \
15+
cast_int(sizeof(TValue)*((n)-1)))
1616

17-
#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
18-
cast(int, sizeof(TValue *)*((n)-1)))
17+
#define sizeLclosure(n) (cast_int(sizeof(LClosure)) + \
18+
cast_int(sizeof(TValue *)*((n)-1)))
1919

2020

2121
/* test whether thread is in 'twups' list */

lgc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.c,v 2.243 2017/12/20 14:58:05 roberto Exp roberto $
2+
** $Id: lgc.c,v 2.244 2017/12/28 15:42:57 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -107,7 +107,7 @@ static lu_mem atomic (lua_State *L);
107107
/*
108108
** one after last element in a hash array
109109
*/
110-
#define gnodelast(h) gnode(h, cast(size_t, sizenode(h)))
110+
#define gnodelast(h) gnode(h, cast_sizet(sizenode(h)))
111111

112112

113113
/*

lgc.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.h,v 2.98 2017/05/26 19:14:29 roberto Exp roberto $
2+
** $Id: lgc.h,v 2.99 2017/10/11 12:38:45 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -57,7 +57,7 @@
5757
/*
5858
** some useful bit tricks
5959
*/
60-
#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
60+
#define resetbits(x,m) ((x) &= cast_byte(~(m)))
6161
#define setbits(x,m) ((x) |= (m))
6262
#define testbits(x,m) ((x) & (m))
6363
#define bitmask(b) (1<<(b))
@@ -95,7 +95,7 @@
9595
#define changewhite(x) ((x)->marked ^= WHITEBITS)
9696
#define gray2black(x) l_setbit((x)->marked, BLACKBIT)
9797

98-
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
98+
#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
9999

100100

101101
/* object age in generational mode */

llex.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: llex.c,v 2.97 2017/06/09 16:48:44 roberto Exp roberto $
2+
** $Id: llex.c,v 2.98 2017/06/29 15:06:44 roberto Exp roberto $
33
** Lexical Analyzer
44
** See Copyright Notice in lua.h
55
*/
@@ -63,7 +63,7 @@ static void save (LexState *ls, int c) {
6363
newsize = luaZ_sizebuffer(b) * 2;
6464
luaZ_resizebuffer(ls->L, b, newsize);
6565
}
66-
b->buffer[luaZ_bufflen(b)++] = cast(char, c);
66+
b->buffer[luaZ_bufflen(b)++] = cast_char(c);
6767
}
6868

6969

llex.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp roberto $
2+
** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp roberto $
33
** Lexical Analyzer
44
** See Copyright Notice in lua.h
55
*/
@@ -37,7 +37,7 @@ enum RESERVED {
3737
};
3838

3939
/* number of reserved words */
40-
#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
40+
#define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1))
4141

4242

4343
typedef union {

llimits.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: llimits.h,v 1.147 2017/12/11 18:53:53 roberto Exp roberto $
2+
** $Id: llimits.h,v 1.148 2017/12/28 11:51:00 roberto Exp roberto $
33
** Limits, basic types, and some other 'installation-dependent' definitions
44
** See Copyright Notice in lua.h
55
*/
@@ -104,10 +104,15 @@ typedef LUAI_UACINT l_uacInt;
104104
#define cast(t, exp) ((t)(exp))
105105

106106
#define cast_void(i) cast(void, (i))
107-
#define cast_byte(i) cast(lu_byte, (i))
107+
#define cast_voidp(i) cast(void *, (i))
108108
#define cast_num(i) cast(lua_Number, (i))
109109
#define cast_int(i) cast(int, (i))
110+
#define cast_uint(i) cast(unsigned int, (i))
111+
#define cast_byte(i) cast(lu_byte, (i))
110112
#define cast_uchar(i) cast(unsigned char, (i))
113+
#define cast_char(i) cast(char, (i))
114+
#define cast_charp(i) cast(char *, (i))
115+
#define cast_sizet(i) cast(size_t, (i))
111116

112117

113118
/* cast a signed lua_Integer to lua_Unsigned */

lmem.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lmem.c,v 1.94 2017/12/08 17:28:25 roberto Exp roberto $
2+
** $Id: lmem.c,v 1.95 2017/12/11 12:27:48 roberto Exp roberto $
33
** Interface to Memory Manager
44
** See Copyright Notice in lua.h
55
*/
@@ -71,8 +71,8 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
7171
}
7272
lua_assert(nelems + 1 <= size && size <= limit);
7373
/* 'limit' ensures that multiplication will not overflow */
74-
newblock = luaM_realloc_(L, block, cast(size_t, *psize) * size_elems,
75-
cast(size_t, size) * size_elems);
74+
newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems,
75+
cast_sizet(size) * size_elems);
7676
if (newblock == NULL)
7777
luaM_error(L);
7878
*psize = size; /* update only when everything else is OK */
@@ -84,8 +84,8 @@ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
8484
int final_n, int size_elem) {
8585
global_State *g = G(L);
8686
void *newblock;
87-
size_t oldsize = cast(size_t, (*size) * size_elem);
88-
size_t newsize = cast(size_t, final_n * size_elem);
87+
size_t oldsize = cast_sizet((*size) * size_elem);
88+
size_t newsize = cast_sizet(final_n * size_elem);
8989
lua_assert(newsize <= oldsize);
9090
newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
9191
if (newblock == NULL && final_n > 0) /* allocation failed? */

lmem.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lmem.h,v 1.45 2017/12/07 18:59:52 roberto Exp roberto $
2+
** $Id: lmem.h,v 1.46 2017/12/08 17:28:25 roberto Exp roberto $
33
** Interface to Memory Manager
44
** See Copyright Notice in lua.h
55
*/
@@ -29,7 +29,7 @@
2929
** avoiding this warning but also this optimization.)
3030
*/
3131
#define luaM_testsize(n,e) \
32-
(sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e))
32+
(sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
3333

3434
#define luaM_checksize(L,n,e) \
3535
(luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
@@ -42,13 +42,15 @@
4242
** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.)
4343
*/
4444
#define luaM_limitN(n,t) \
45-
((cast(size_t, n) > MAX_SIZET/sizeof(t)) ? (MAX_SIZET/sizeof(t)) : (n))
45+
((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
46+
cast_uint((MAX_SIZET/sizeof(t))))
47+
4648

4749
/*
4850
** Arrays of chars do not need any test
4951
*/
5052
#define luaM_reallocvchar(L,b,on,n) \
51-
cast(char *, luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
53+
cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
5254

5355
#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s))
5456
#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b)))
@@ -66,8 +68,8 @@
6668
luaM_limitN(limit,t),e)))
6769

6870
#define luaM_reallocvector(L, v,oldn,n,t) \
69-
(cast(t *, luaM_realloc_(L, v, cast(size_t, oldn) * sizeof(t), \
70-
cast(size_t, n) * sizeof(t))))
71+
(cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
72+
cast_sizet(n) * sizeof(t))))
7173

7274
#define luaM_shrinkvector(L,v,size,fs,t) \
7375
((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))

lobject.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lobject.c,v 2.121 2017/11/23 19:29:04 roberto Exp roberto $
2+
** $Id: lobject.c,v 2.122 2017/12/30 20:46:18 roberto Exp roberto $
33
** Some generic functions over Lua objects
44
** See Copyright Notice in lua.h
55
*/
@@ -204,7 +204,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
204204
int e = 0; /* exponent correction */
205205
int neg; /* 1 if number is negative */
206206
int hasdot = 0; /* true after seen a dot */
207-
*endptr = cast(char *, s); /* nothing is valid yet */
207+
*endptr = cast_charp(s); /* nothing is valid yet */
208208
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
209209
neg = isneg(&s); /* check sign */
210210
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
@@ -226,7 +226,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
226226
}
227227
if (nosigdig + sigdig == 0) /* no digits? */
228228
return 0.0; /* invalid format */
229-
*endptr = cast(char *, s); /* valid up to here */
229+
*endptr = cast_charp(s); /* valid up to here */
230230
e *= 4; /* each digit multiplies/divides value by 2^4 */
231231
if (*s == 'p' || *s == 'P') { /* exponent part? */
232232
int exp1 = 0; /* exponent value */
@@ -239,7 +239,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
239239
exp1 = exp1 * 10 + *(s++) - '0';
240240
if (neg1) exp1 = -exp1;
241241
e += exp1;
242-
*endptr = cast(char *, s); /* valid up to here */
242+
*endptr = cast_charp(s); /* valid up to here */
243243
}
244244
if (neg) r = -r;
245245
return l_mathop(ldexp)(r, e);
@@ -353,15 +353,15 @@ int luaO_utf8esc (char *buff, unsigned long x) {
353353
int n = 1; /* number of bytes put in buffer (backwards) */
354354
lua_assert(x <= 0x10FFFF);
355355
if (x < 0x80) /* ascii? */
356-
buff[UTF8BUFFSZ - 1] = cast(char, x);
356+
buff[UTF8BUFFSZ - 1] = cast_char(x);
357357
else { /* need continuation bytes */
358358
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
359359
do { /* add continuation bytes */
360-
buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
360+
buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
361361
x >>= 6; /* remove added bits */
362362
mfb >>= 1; /* now there is one less bit available in first byte */
363363
} while (x > mfb); /* still needs continuation byte? */
364-
buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */
364+
buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */
365365
}
366366
return n;
367367
}
@@ -417,7 +417,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
417417
break;
418418
}
419419
case 'c': { /* an 'int' as a character */
420-
char buff = cast(char, va_arg(argp, int));
420+
char buff = cast_char(va_arg(argp, int));
421421
if (lisprint(cast_uchar(buff)))
422422
pushstr(L, &buff, 1);
423423
else /* non-printable character; print its code */

lobject.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lobject.h,v 2.131 2017/11/23 19:29:04 roberto Exp roberto $
2+
** $Id: lobject.h,v 2.132 2018/01/28 12:07:53 roberto Exp roberto $
33
** Type definitions for Lua objects
44
** See Copyright Notice in lua.h
55
*/
@@ -351,7 +351,7 @@ typedef union UTString {
351351
** (Access to 'extra' ensures that value is really a 'TString'.)
352352
*/
353353
#define getstr(ts) \
354-
check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
354+
check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(UTString))
355355

356356

357357
/* get the actual string (array of bytes) from a Lua value */
@@ -391,7 +391,7 @@ typedef union UUdata {
391391
** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
392392
*/
393393
#define getudatamem(u) \
394-
check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
394+
check_exp(sizeof((u)->ttuv_), (cast_charp(u) + sizeof(UUdata)))
395395

396396
#define setuservalue(L,u,o) \
397397
{ const TValue *io=(o); Udata *iu = (u); \
@@ -607,7 +607,7 @@ typedef struct Table {
607607
** 'module' operation for hashing (size is always a power of 2)
608608
*/
609609
#define lmod(s,size) \
610-
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
610+
(check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
611611

612612

613613
#define twoto(x) (1<<(x))

0 commit comments

Comments
 (0)