lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
lua / llimits.h
| 1 | /* |
| 2 | ** $Id: llimits.h $ |
| 3 | ** Limits, basic types, and some other 'installation-dependent' definitions |
| 4 | ** See Copyright Notice in lua.h |
| 5 | */ |
| 6 | |
| 7 | #ifndef llimits_h |
| 8 | #define llimits_h |
| 9 | |
| 10 | |
| 11 | #include <limits.h> |
| 12 | #include <stddef.h> |
| 13 | |
| 14 | |
| 15 | #include "lua.h" |
| 16 | |
| 17 | |
| 18 | #define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT) |
| 19 | |
| 20 | /* |
| 21 | ** 'l_mem' is a signed integer big enough to count the total memory |
| 22 | ** used by Lua. (It is signed due to the use of debt in several |
| 23 | ** computations.) 'lu_mem' is a corresponding unsigned type. Usually, |
| 24 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. |
| 25 | */ |
| 26 | #if defined(LUAI_MEM) /* { external definitions? */ |
| 27 | typedef LUAI_MEM l_mem; |
| 28 | typedef LUAI_UMEM lu_mem; |
| 29 | #elif LUAI_IS32INT /* }{ */ |
| 30 | typedef ptrdiff_t l_mem; |
| 31 | typedef size_t lu_mem; |
| 32 | #else /* 16-bit ints */ /* }{ */ |
| 33 | typedef long l_mem; |
| 34 | typedef unsigned long lu_mem; |
| 35 | #endif /* } */ |
| 36 | |
| 37 | #define MAX_LMEM \ |
| 38 | cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1) |
| 39 | |
| 40 | |
| 41 | /* chars used as small naturals (so that 'char' is reserved for characters) */ |
| 42 | typedef unsigned char lu_byte; |
| 43 | typedef signed char ls_byte; |
| 44 | |
| 45 | |
| 46 | /* Type for thread status/error codes */ |
| 47 | typedef lu_byte TStatus; |
| 48 | |
| 49 | /* The C API still uses 'int' for status/error codes */ |
| 50 | #define APIstatus(st) cast_int(st) |
| 51 | |
| 52 | /* maximum value for size_t */ |
| 53 | #define MAX_SIZET ((size_t)(~(size_t)0)) |
| 54 | |
| 55 | /* |
| 56 | ** Maximum size for strings and userdata visible for Lua; should be |
| 57 | ** representable as a lua_Integer and as a size_t. |
| 58 | */ |
| 59 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ |
| 60 | : cast_sizet(LUA_MAXINTEGER)) |
| 61 | |
| 62 | /* |
| 63 | ** test whether an unsigned value is a power of 2 (or zero) |
| 64 | */ |
| 65 | #define ispow2(x) (((x) & ((x) - 1)) == 0) |
| 66 | |
| 67 | |
| 68 | /* number of chars of a literal string without the ending \0 */ |
| 69 | #define LL(x) (sizeof(x)/sizeof(char) - 1) |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | ** conversion of pointer to unsigned integer: this is for hashing only; |
| 74 | ** there is no problem if the integer cannot hold the whole pointer |
| 75 | ** value. (In strict ISO C this may cause undefined behavior, but no |
| 76 | ** actual machine seems to bother.) |
| 77 | */ |
| 78 | #if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ |
| 79 | __STDC_VERSION__ >= 199901L |
| 80 | #include <stdint.h> |
| 81 | #if defined(UINTPTR_MAX) /* even in C99 this type is optional */ |
| 82 | #define L_P2I uintptr_t |
| 83 | #else /* no 'intptr'? */ |
| 84 | #define L_P2I uintmax_t /* use the largest available integer */ |
| 85 | #endif |
| 86 | #else /* C89 option */ |
| 87 | #define L_P2I size_t |
| 88 | #endif |
| 89 | |
| 90 | #define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX) |
| 91 | |
| 92 | |
| 93 | |
| 94 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */ |
| 95 | typedef LUAI_UACNUMBER l_uacNumber; |
| 96 | typedef LUAI_UACINT l_uacInt; |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | ** Internal assertions for in-house debugging |
| 101 | */ |
| 102 | #if defined LUAI_ASSERT |
| 103 | #undef NDEBUG |
| 104 | #include <assert.h> |
| 105 | #define lua_assert(c) assert(c) |
| 106 | #define assert_code(c) c |
| 107 | #endif |
| 108 | |
| 109 | #if defined(lua_assert) |
| 110 | #else |
| 111 | #define lua_assert(c) ((void)0) |
| 112 | #define assert_code(c) ((void)0) |
| 113 | #endif |
| 114 | |
| 115 | #define check_exp(c,e) (lua_assert(c), (e)) |
| 116 | /* to avoid problems with conditions too long */ |
| 117 | #define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0)) |
| 118 | |
| 119 | |
| 120 | /* macro to avoid warnings about unused variables */ |
| 121 | #if !defined(UNUSED) |
| 122 | #define UNUSED(x) ((void)(x)) |
| 123 | #endif |
| 124 | |
| 125 | |
| 126 | /* type casts (a macro highlights casts in the code) */ |
| 127 | #define cast(t, exp) ((t)(exp)) |
| 128 | |
| 129 | #define cast_void(i) cast(void, (i)) |
| 130 | #define cast_voidp(i) cast(void *, (i)) |
| 131 | #define cast_num(i) cast(lua_Number, (i)) |
| 132 | #define cast_int(i) cast(int, (i)) |
| 133 | #define cast_short(i) cast(short, (i)) |
| 134 | #define cast_uint(i) cast(unsigned int, (i)) |
| 135 | #define cast_byte(i) cast(lu_byte, (i)) |
| 136 | #define cast_uchar(i) cast(unsigned char, (i)) |
| 137 | #define cast_char(i) cast(char, (i)) |
| 138 | #define cast_charp(i) cast(char *, (i)) |
| 139 | #define cast_sizet(i) cast(size_t, (i)) |
| 140 | #define cast_Integer(i) cast(lua_Integer, (i)) |
| 141 | #define cast_Inst(i) cast(Instruction, (i)) |
| 142 | |
| 143 | |
| 144 | /* cast a signed lua_Integer to lua_Unsigned */ |
| 145 | #if !defined(l_castS2U) |
| 146 | #define l_castS2U(i) ((lua_Unsigned)(i)) |
| 147 | #endif |
| 148 | |
| 149 | /* |
| 150 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is |
| 151 | ** not strict ISO C, but two-complement architectures should |
| 152 | ** work fine. |
| 153 | */ |
| 154 | #if !defined(l_castU2S) |
| 155 | #define l_castU2S(i) ((lua_Integer)(i)) |
| 156 | #endif |
| 157 | |
| 158 | /* |
| 159 | ** cast a size_t to lua_Integer: These casts are always valid for |
| 160 | ** sizes of Lua objects (see MAX_SIZE) |
| 161 | */ |
| 162 | #define cast_st2S(sz) ((lua_Integer)(sz)) |
| 163 | |
| 164 | /* Cast a ptrdiff_t to size_t, when it is known that the minuend |
| 165 | ** comes from the subtrahend (the base) |
| 166 | */ |
| 167 | #define ct_diff2sz(df) ((size_t)(df)) |
| 168 | |
| 169 | /* ptrdiff_t to lua_Integer */ |
| 170 | #define ct_diff2S(df) cast_st2S(ct_diff2sz(df)) |
| 171 | |
| 172 | /* |
| 173 | ** Special type equivalent to '(void*)' for functions (to suppress some |
| 174 | ** warnings when converting function pointers) |
| 175 | */ |
| 176 | typedef void (*voidf)(void); |
| 177 | |
| 178 | /* |
| 179 | ** Macro to convert pointer-to-void* to pointer-to-function. This cast |
| 180 | ** is undefined according to ISO C, but POSIX assumes that it works. |
| 181 | ** (The '__extension__' in gnu compilers is only to avoid warnings.) |
| 182 | */ |
| 183 | #if defined(__GNUC__) |
| 184 | #define cast_func(p) (__extension__ (voidf)(p)) |
| 185 | #else |
| 186 | #define cast_func(p) ((voidf)(p)) |
| 187 | #endif |
| 188 | |
| 189 | |
| 190 | |
| 191 | /* |
| 192 | ** non-return type |
| 193 | */ |
| 194 | #if !defined(l_noret) |
| 195 | |
| 196 | #if defined(__GNUC__) |
| 197 | #define l_noret void __attribute__((noreturn)) |
| 198 | #elif defined(_MSC_VER) && _MSC_VER >= 1200 |
| 199 | #define l_noret void __declspec(noreturn) |
| 200 | #else |
| 201 | #define l_noret void |
| 202 | #endif |
| 203 | |
| 204 | #endif |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | ** Inline functions |
| 209 | */ |
| 210 | #if !defined(LUA_USE_C89) |
| 211 | #define l_inline inline |
| 212 | #elif defined(__GNUC__) |
| 213 | #define l_inline __inline__ |
| 214 | #else |
| 215 | #define l_inline /* empty */ |
| 216 | #endif |
| 217 | |
| 218 | #define l_sinline static l_inline |
| 219 | |
| 220 | |
| 221 | /* |
| 222 | ** An unsigned with (at least) 4 bytes |
| 223 | */ |
| 224 | #if LUAI_IS32INT |
| 225 | typedef unsigned int l_uint32; |
| 226 | #else |
| 227 | typedef unsigned long l_uint32; |
| 228 | #endif |
| 229 | |
| 230 | |
| 231 | /* |
| 232 | ** The luai_num* macros define the primitive operations over numbers. |
| 233 | */ |
| 234 | |
| 235 | /* floor division (defined as 'floor(a/b)') */ |
| 236 | #if !defined(luai_numidiv) |
| 237 | #define luai_numidiv(L,a,b) l_floor(luai_numdiv(L,a,b)) |
| 238 | #endif |
| 239 | |
| 240 | /* float division */ |
| 241 | #if !defined(luai_numdiv) |
| 242 | #define luai_numdiv(L,a,b) ((void)L, (a)/(b)) |
| 243 | #endif |
| 244 | |
| 245 | /* |
| 246 | ** modulo: defined as 'a - floor(a/b)*b'; the direct computation |
| 247 | ** using this definition has several problems with rounding errors, |
| 248 | ** so it is better to use 'fmod'. 'fmod' gives the result of |
| 249 | ** 'a - trunc(a/b)*b', and therefore must be corrected when |
| 250 | ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a |
| 251 | ** non-integer negative result: non-integer result is equivalent to |
| 252 | ** a non-zero remainder 'm'; negative result is equivalent to 'a' and |
| 253 | ** 'b' with different signs, or 'm' and 'b' with different signs |
| 254 | ** (as the result 'm' of 'fmod' has the same sign of 'a'). |
| 255 | */ |
| 256 | #if !defined(luai_nummod) |
| 257 | #define luai_nummod(L,a,b,m) \ |
| 258 | { (void)L; (m) = l_mathop(fmod)(a,b); \ |
| 259 | if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } |
| 260 | #endif |
| 261 | |
| 262 | /* exponentiation */ |
| 263 | #if !defined(luai_numpow) |
| 264 | #define luai_numpow(L,a,b) \ |
| 265 | ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) |
| 266 | #endif |
| 267 | |
| 268 | /* the others are quite standard operations */ |
| 269 | #if !defined(luai_numadd) |
| 270 | #define luai_numadd(L,a,b) ((void)L, (a)+(b)) |
| 271 | #define luai_numsub(L,a,b) ((void)L, (a)-(b)) |
| 272 | #define luai_nummul(L,a,b) ((void)L, (a)*(b)) |
| 273 | #define luai_numunm(L,a) ((void)L, -(a)) |
| 274 | #define luai_numeq(a,b) ((a)==(b)) |
| 275 | #define luai_numlt(a,b) ((a)<(b)) |
| 276 | #define luai_numle(a,b) ((a)<=(b)) |
| 277 | #define luai_numgt(a,b) ((a)>(b)) |
| 278 | #define luai_numge(a,b) ((a)>=(b)) |
| 279 | #define luai_numisnan(a) (!luai_numeq((a), (a))) |
| 280 | #endif |
| 281 | |
| 282 | |
| 283 | |
| 284 | /* |
| 285 | ** lua_numbertointeger converts a float number with an integral value |
| 286 | ** to an integer, or returns 0 if the float is not within the range of |
| 287 | ** a lua_Integer. (The range comparisons are tricky because of |
| 288 | ** rounding. The tests here assume a two-complement representation, |
| 289 | ** where MININTEGER always has an exact representation as a float; |
| 290 | ** MAXINTEGER may not have one, and therefore its conversion to float |
| 291 | ** may have an ill-defined value.) |
| 292 | */ |
| 293 | #define lua_numbertointeger(n,p) \ |
| 294 | ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ |
| 295 | (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ |
| 296 | (*(p) = (LUA_INTEGER)(n), 1)) |
| 297 | |
| 298 | |
| 299 | |
| 300 | /* |
| 301 | ** LUAI_FUNC is a mark for all extern functions that are not to be |
| 302 | ** exported to outside modules. |
| 303 | ** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, |
| 304 | ** none of which to be exported to outside modules (LUAI_DDEF for |
| 305 | ** definitions and LUAI_DDEC for declarations). |
| 306 | ** Elf and MACH/gcc (versions 3.2 and later) mark them as "hidden" to |
| 307 | ** optimize access when Lua is compiled as a shared library. Not all elf |
| 308 | ** targets support this attribute. Unfortunately, gcc does not offer |
| 309 | ** a way to check whether the target offers that support, and those |
| 310 | ** without support give a warning about it. To avoid these warnings, |
| 311 | ** change to the default definition. |
| 312 | */ |
| 313 | #if !defined(LUAI_FUNC) |
| 314 | |
| 315 | #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ |
| 316 | (defined(__ELF__) || defined(__MACH__)) |
| 317 | #define LUAI_FUNC __attribute__((visibility("internal"))) extern |
| 318 | #else |
| 319 | #define LUAI_FUNC extern |
| 320 | #endif |
| 321 | |
| 322 | #define LUAI_DDEC(dec) LUAI_FUNC dec |
| 323 | #define LUAI_DDEF /* empty */ |
| 324 | |
| 325 | #endif |
| 326 | |
| 327 | |
| 328 | /* Give these macros simpler names for internal use */ |
| 329 | #define l_likely(x) luai_likely(x) |
| 330 | #define l_unlikely(x) luai_unlikely(x) |
| 331 | |
| 332 | /* |
| 333 | ** {================================================================== |
| 334 | ** "Abstraction Layer" for basic report of messages and errors |
| 335 | ** =================================================================== |
| 336 | */ |
| 337 | |
| 338 | /* print a string */ |
| 339 | #if !defined(lua_writestring) |
| 340 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) |
| 341 | #endif |
| 342 | |
| 343 | /* print a newline and flush the output */ |
| 344 | #if !defined(lua_writeline) |
| 345 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) |
| 346 | #endif |
| 347 | |
| 348 | /* print an error message */ |
| 349 | #if !defined(lua_writestringerror) |
| 350 | #define lua_writestringerror(s,p) \ |
| 351 | (fprintf(stderr, (s), (p)), fflush(stderr)) |
| 352 | #endif |
| 353 | |
| 354 | /* }================================================================== */ |
| 355 | |
| 356 | #endif |
| 357 |