瀏覽代碼

Janitorial work on casts

Roberto Ierusalimschy 2 月之前
父節點
當前提交
7ade155762
共有 8 個文件被更改,包括 47 次插入44 次删除
  1. 4 4
      lcode.c
  2. 1 1
      ldump.c
  3. 3 0
      llimits.h
  4. 1 1
      lobject.c
  5. 20 20
      lopcodes.h
  6. 2 2
      lparser.c
  7. 15 15
      ltests.c
  8. 1 1
      lundump.c

+ 4 - 4
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;
     }
   }

+ 1 - 1
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));
 }
 
 

+ 3 - 0
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 */

+ 1 - 1
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;
       }

+ 20 - 20
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_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 getarg(i,pos,size)	(cast_int(((i)>>(pos)) & MASK1(size,0)))
 #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 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)
 
 
-#define CREATE_ABCk(o,a,b,c,k)	((cast(Instruction, o)<<POS_OP) \
-			| (cast(Instruction, a)<<POS_A) \
-			| (cast(Instruction, b)<<POS_B) \
-			| (cast(Instruction, c)<<POS_C) \
-			| (cast(Instruction, k)<<POS_k))
+#define CREATE_ABCk(o,a,b,c,k)	((cast_Inst(o)<<POS_OP) \
+			| (cast_Inst(a)<<POS_A) \
+			| (cast_Inst(b)<<POS_B) \
+			| (cast_Inst(c)<<POS_C) \
+			| (cast_Inst(k)<<POS_k))
 
-#define CREATE_vABCk(o,a,b,c,k)	((cast(Instruction, o)<<POS_OP) \
-			| (cast(Instruction, a)<<POS_A) \
-			| (cast(Instruction, b)<<POS_vB) \
-			| (cast(Instruction, c)<<POS_vC) \
-			| (cast(Instruction, k)<<POS_k))
+#define CREATE_vABCk(o,a,b,c,k)	((cast_Inst(o)<<POS_OP) \
+			| (cast_Inst(a)<<POS_A) \
+			| (cast_Inst(b)<<POS_vB) \
+			| (cast_Inst(c)<<POS_vC) \
+			| (cast_Inst(k)<<POS_k))
 
-#define CREATE_ABx(o,a,bc)	((cast(Instruction, o)<<POS_OP) \
-			| (cast(Instruction, a)<<POS_A) \
-			| (cast(Instruction, bc)<<POS_Bx))
+#define CREATE_ABx(o,a,bc)	((cast_Inst(o)<<POS_OP) \
+			| (cast_Inst(a)<<POS_A) \
+			| (cast_Inst(bc)<<POS_Bx))
 
-#define CREATE_Ax(o,a)		((cast(Instruction, o)<<POS_OP) \
-			| (cast(Instruction, a)<<POS_Ax))
+#define CREATE_Ax(o,a)		((cast_Inst(o)<<POS_OP) \
+			| (cast_Inst(a)<<POS_Ax))
 
-#define CREATE_sJ(o,j,k)	((cast(Instruction, o) << POS_OP) \
-			| (cast(Instruction, j) << POS_sJ) \
-			| (cast(Instruction, k) << POS_k))
+#define CREATE_sJ(o,j,k)	((cast_Inst(o) << POS_OP) \
+			| (cast_Inst(j) << POS_sJ) \
+			| (cast_Inst(k) << POS_k))
 
 
 #if !defined(MAXINDEXRK)  /* (for debugging only) */

+ 2 - 2
lparser.c

@@ -276,7 +276,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) {
 static void init_var (FuncState *fs, expdesc *e, int vidx) {
   e->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 */
   }
 }
 

+ 15 - 15
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)));
 }

+ 1 - 1
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);