diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2014-10-17 20:17:22 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2014-10-17 20:17:22 -0700 |
commit | 9b18f519decaf0cd7f52571c3c44012e3fc23a3a (patch) | |
tree | 5848a0bfd530e51183dbbf1e201a56ccf437fbd1 | |
parent | 9f158a44b7e88c1dcfb8d56f4f85d642fc423b59 (diff) | |
download | txr-9b18f519decaf0cd7f52571c3c44012e3fc23a3a.tar.gz txr-9b18f519decaf0cd7f52571c3c44012e3fc23a3a.tar.bz2 txr-9b18f519decaf0cd7f52571c3c44012e3fc23a3a.zip |
Converting cast expressions to macros that are retargetted
to C++ style casts when compiling as C++.
* lib.h (strip_qual, convert, coerce): New casting macros.
(TAG_MASK, tag, type, wli_noex, auto_str, static_str,
litptr, num_fast, chr, lit_noex, nil, nao): Use cast macros.
* arith.c (mul, isqrt_fixnum, bit): Use cast macros.
* configure (INT_PTR_MAX): Define using cast macro.
* debug.c (debug_init): Use cast macro.
* eval.c (do_eval, expand_macro, reg_op, reg_mac, eval_init):
Use cast macros.
* filter.c (filter_init): Use cast macro.
* gc.c (more, mark_obj, in_heap, mark, sweep_one, unmark): Use
cast macros.
* hash.c (hash_double, equal_hash, eql_hash, hash_equal_op,
hash_hash_op, hash_print_op, hash_mark, make_hash, make_similar_hash,
copy_hash, gethash_c, gethash, gethash_f, gethash_n, remhash,
hash_count, get_hash_userdata, set_hash_userdata, hash_iter_destroy,
hash_iter_mark, hash_begin, hash_uni, hash_diff, hash_isec): Use
cast macros.
* lib.c (code2type, chk_malloc, chk_malloc_gc_more, chk_calloc,
chk_realloc, chk_strdup, num, c_num, string, mkstring, mkustring,
upcase_str, downcase_str, string_extend, sub_str, cat_str, trim_str,
c_chr, vector, vec_set_length, copy_vec, sub_vec, cat_vec,
cobj_print_op, obj_init): Likewise.
* match.c (do_match_line, hv_trampoline, match_files, dir_tables_init):
Likewise.
* parser.l (grammar): Likewise.
* parser.y (parse): Likewise.
* rand.c (make_state, make_random_state, random_fixnum, random):
Likewise.
* regex.c (CHAR_SET_L2_LO, CHAR_SET_L2_HI, CHAR_SET_L1_LO,
CHAR_SET_L1_HI, CHAR_SET_L0_LO, CHAR_SET_L0_HI, L0_full, L0_fill_range,
L1_full, L1_fill_range, L1_contains, L1_free, L2_full, L2_fill_range,
L2_contains, L2_free, L3_fill_range, L3_contains, L3_free,
char_set_create, char_set_cobj_destroy, nfa_state_accept,
nfa_state_empty, nfa_state_single, nfa_state_wild, nfa_state_set,
-rw-r--r-- | ChangeLog | 94 | ||||
-rw-r--r-- | arith.c | 6 | ||||
-rwxr-xr-x | configure | 2 | ||||
-rw-r--r-- | debug.c | 2 | ||||
-rw-r--r-- | eval.c | 10 | ||||
-rw-r--r-- | filter.c | 2 | ||||
-rw-r--r-- | gc.c | 16 | ||||
-rw-r--r-- | hash.c | 90 | ||||
-rw-r--r-- | lib.c | 72 | ||||
-rw-r--r-- | lib.h | 49 | ||||
-rw-r--r-- | match.c | 142 | ||||
-rw-r--r-- | parser.l | 6 | ||||
-rw-r--r-- | parser.y | 2 | ||||
-rw-r--r-- | rand.c | 31 | ||||
-rw-r--r-- | regex.c | 134 | ||||
-rw-r--r-- | signal.c | 4 | ||||
-rw-r--r-- | stream.c | 238 | ||||
-rw-r--r-- | sysif.c | 4 | ||||
-rw-r--r-- | syslog.c | 14 | ||||
-rw-r--r-- | txr.c | 2 | ||||
-rw-r--r-- | unwind.c | 4 | ||||
-rw-r--r-- | utf8.c | 16 |
22 files changed, 529 insertions, 411 deletions
@@ -1,5 +1,99 @@ 2014-10-17 Kaz Kylheku <kaz@kylheku.com> + Converting cast expressions to macros that are retargetted + to C++ style casts when compiling as C++. + + * lib.h (strip_qual, convert, coerce): New casting macros. + (TAG_MASK, tag, type, wli_noex, auto_str, static_str, + litptr, num_fast, chr, lit_noex, nil, nao): Use cast macros. + + * arith.c (mul, isqrt_fixnum, bit): Use cast macros. + + * configure (INT_PTR_MAX): Define using cast macro. + + * debug.c (debug_init): Use cast macro. + + * eval.c (do_eval, expand_macro, reg_op, reg_mac, eval_init): + Use cast macros. + + * filter.c (filter_init): Use cast macro. + + * gc.c (more, mark_obj, in_heap, mark, sweep_one, unmark): Use + cast macros. + + * hash.c (hash_double, equal_hash, eql_hash, hash_equal_op, + hash_hash_op, hash_print_op, hash_mark, make_hash, make_similar_hash, + copy_hash, gethash_c, gethash, gethash_f, gethash_n, remhash, + hash_count, get_hash_userdata, set_hash_userdata, hash_iter_destroy, + hash_iter_mark, hash_begin, hash_uni, hash_diff, hash_isec): Use + cast macros. + + * lib.c (code2type, chk_malloc, chk_malloc_gc_more, chk_calloc, + chk_realloc, chk_strdup, num, c_num, string, mkstring, mkustring, + upcase_str, downcase_str, string_extend, sub_str, cat_str, trim_str, + c_chr, vector, vec_set_length, copy_vec, sub_vec, cat_vec, + cobj_print_op, obj_init): Likewise. + + * match.c (do_match_line, hv_trampoline, match_files, dir_tables_init): + Likewise. + + * parser.l (grammar): Likewise. + + * parser.y (parse): Likewise. + + * rand.c (make_state, make_random_state, random_fixnum, random): + Likewise. + + * regex.c (CHAR_SET_L2_LO, CHAR_SET_L2_HI, CHAR_SET_L1_LO, + CHAR_SET_L1_HI, CHAR_SET_L0_LO, CHAR_SET_L0_HI, L0_full, L0_fill_range, + L1_full, L1_fill_range, L1_contains, L1_free, L2_full, L2_fill_range, + L2_contains, L2_free, L3_fill_range, L3_contains, L3_free, + char_set_create, char_set_cobj_destroy, nfa_state_accept, + nfa_state_empty, nfa_state_single, nfa_state_wild, nfa_state_set, + nfa_free, nfa_run, regex_destroy, regex_mark, dv_compile_regex, + reg_derivative, regex_compile, regex_print, regex_run, + regex_machine_init): Likewise. + + * signal.c (sig_mask): Likewise. + + * stream.c (make_null_stream, stdio_stream_print, stdio_stream_destroy, + stdio_stream_mark, stdio_maybe_read_error, stdio_maybe_error, + stdio_put_char_callback, stdio_get_char_callback, stdio_put_string, + stdio_put_char, stdio_put_byte, stdio_flush, stdio_seek, + stdio_get_prop, snarf_line, stdio_get_line, stdio_get_char, + stdio_get_byte, stdio_unget_char, stdio_unget_byte, stdio_close, + tail_strategy, pipe_close, string_in_stream_mark, string_in_get_line, + string_in_get_char, string_in_unget_char, string_in_get_prop, + byte_in_stream_destroy, byte_in_get_byte, byte_in_unget_byte, + string_out_stream_destroy, string_out_byte_callback, + string_out_byte_flush, string_out_put_string, string_out_put_char, + string_out_put_byte, strlist_mark, strlist_out_put_string, + strlist_out_put_char, make_strlist_output_stream, get_list_from_stream, + dir_get_line, dir_close, make_stdio_stream_common, make_pipevp_stream, + make_string_input_stream, make_string_byte_input_stream, + make_string_output_stream, get_string_from_stream, make_dir_stream, + stream_set_prop, stream_get_prop, close_stream, get_line, get_char, + get_byte, unget_char, unget_byte, vformat, put_string, put_char, + put_byte, flush_stream, seek_stream, open_tail, open_process, run, + cat_stream_print, cat_get_line, cat_get_char, cat_get_byte, + cat_unget_byte, cat_unget_char, cat_get_prop, cat_mark, + make_catenated_stream): Likewise. + + * sysif.c (getcwd_wrap): Likewise. + + * syslog.c (syslog_mark, syslog_put_string, syslog_put_char, + syslog_put_byte, syslog_get_prop, syslog_set_prop, make_syslog_stream): + Likewise. + + * txr.c (get_self_path): Likewise. + + * unwind.c (uw_init): Likewise. + + * utf8.c (utf8_from_uc, utf8_to_uc, utf8_to, utf8_dup_from_uc, + utf8_dup_from, utf8_dup_to, utf8_decode): Likewise. + +2014-10-17 Kaz Kylheku <kaz@kylheku.com> + Purge stray occurrences of "void *" from code base. * lib.c (cobj_print_op): In the format call, cast @@ -571,7 +571,7 @@ tail: cnum a = c_num(anum); cnum b = c_num(bnum); #if HAVE_DOUBLE_INTPTR_T - double_intptr_t product = a * (double_intptr_t) b; + double_intptr_t product = a * convert(double_intptr_t, b); if (product < NUM_MIN || product > NUM_MAX) return bignum_dbl_ipt(product); return num_fast(product); @@ -1330,7 +1330,7 @@ bad: static int_ptr_t isqrt_fixnum(int_ptr_t a) { - int_ptr_t mask = (int_ptr_t) 1 << (highest_bit(a) / 2); + int_ptr_t mask = convert(int_ptr_t, 1) << (highest_bit(a) / 2); int_ptr_t root = 0; for (; mask != 0; mask >>= 1) { @@ -1898,7 +1898,7 @@ val bit(val a, val bit) { cnum an = c_num(a); if (bn < (SIZEOF_PTR * CHAR_BIT)) - return (an & ((cnum) 1 << bn)) ? t : nil; + return (an & (convert(cnum, 1) << bn)) ? t : nil; return an < 0 ? t : nil; } case BGNUM: @@ -1108,7 +1108,7 @@ if [ -n "$uintptr" ] ; then printf "#define HAVE_UINTPTR_T 1\n" >> config.h printf "typedef unsigned $intptr uint_ptr_t;\n" >> config.h fi -intptr_max_expr="((((($intptr) 1 << $((SIZEOF_PTR * 8 - 2))) - 1) << 1) + 1)" +intptr_max_expr="((((convert($intptr, 1) << $((SIZEOF_PTR * 8 - 2))) - 1) << 1) + 1)" printf "#define INT_PTR_MAX %s\n" "$intptr_max_expr" >> config.h printf "#define INT_PTR_MIN (-INT_PTR_MAX)\n" >> config.h @@ -245,7 +245,7 @@ val debug(val form, val bindings, val data, val line, val pos, val base) void debug_init(void) { step_mode = 1; - protect(&breakpoints, &last_command, (val *) 0); + protect(&breakpoints, &last_command, convert(val *, 0)); debug_block_s = intern(lit("debug-block"), system_package); { char *columns = getenv("COLUMNS"); @@ -934,7 +934,7 @@ static val do_eval(val form, val env, val ctx_form, val entry = gethash(op_table, oper); if (entry) { - opfun_t fp = (opfun_t) cptr_get(entry); + opfun_t fp = coerce(opfun_t, cptr_get(entry)); last_form_evaled = form; debug_return (fp(form, env)); } else { @@ -1331,7 +1331,7 @@ static val op_defmacro(val form, val env) static val expand_macro(val form, val expander, val menv) { if (cobjp(expander)) { - mefun_t fp = (mefun_t) cptr_get(expander); + mefun_t fp = coerce(mefun_t, cptr_get(expander)); return fp(form, menv); } else { debug_enter; @@ -3217,7 +3217,7 @@ static val force(val promise) static void reg_op(val sym, opfun_t fun) { assert (sym != 0); - sethash(op_table, sym, cptr((mem_t *) fun)); + sethash(op_table, sym, cptr(coerce(mem_t *, fun))); } void reg_fun(val sym, val fun) @@ -3229,7 +3229,7 @@ void reg_fun(val sym, val fun) static void reg_mac(val sym, mefun_t fun) { assert (sym != 0); - sethash(top_mb, sym, cptr((mem_t *) fun)); + sethash(top_mb, sym, cptr(coerce(mem_t *, fun))); } void reg_var(val sym, val val) @@ -3340,7 +3340,7 @@ static val merge_wrap(val list1, val list2, val lessfun, val keyfun) void eval_init(void) { protect(&top_vb, &top_fb, &top_mb, &top_smb, &special, &dyn_env, - &op_table, &last_form_evaled, (val *) 0); + &op_table, &last_form_evaled, convert(val *, 0)); top_fb = make_hash(t, nil, nil); top_vb = make_hash(t, nil, nil); top_mb = make_hash(t, nil, nil); @@ -682,7 +682,7 @@ static val html_decode(val str) void filter_init(void) { - protect(&filters, (val *) 0); + protect(&filters, convert(val *, 0)); filters = make_hash(nil, nil, nil); filter_k = intern(lit("filter"), keyword_package); @@ -128,7 +128,7 @@ void protect(val *first, ...) static void more(void) { - heap_t *heap = (heap_t *) chk_malloc_gc_more(sizeof *heap); + heap_t *heap = coerce(heap_t *, chk_malloc_gc_more(sizeof *heap)); obj_t *block = heap->block, *end = heap->block + HEAP_SIZE; if (end > heap_max_bound) @@ -139,7 +139,7 @@ static void more(void) while (block < end) { block->t.next = free_list; - block->t.type = (type_t) FREE; + block->t.type = convert(type_t, FREE); free_list = block++; } @@ -282,7 +282,7 @@ tail_call: if ((t & FREE) != 0) abort(); - obj->t.type = (type_t) (t | REACHABLE); + obj->t.type = convert(type_t, t | REACHABLE); #if EXTRA_DEBUGGING if (obj == break_obj) @@ -364,7 +364,7 @@ static int in_heap(val ptr) for (heap = heap_list; heap != 0; heap = heap->next) { if (ptr >= heap->block && ptr < heap->block + HEAP_SIZE) - if (((char *) ptr - (char *) heap->block) % sizeof (obj_t) == 0) + if ((coerce(char *, ptr) - coerce(char *, heap->block)) % sizeof (obj_t) == 0) return 1; } @@ -430,7 +430,7 @@ static void mark(mach_context_t *pmc, val *gc_stack_top) /* * Then the machine context */ - mark_mem_region((val *) pmc, (val *) (pmc + 1)); + mark_mem_region(coerce(val *, pmc), coerce(val *, (pmc + 1))); /* * Finally, the stack. @@ -455,7 +455,7 @@ static int sweep_one(obj_t *block) abort(); if (block->t.type & REACHABLE) { - block->t.type = (type_t) (block->t.type & ~REACHABLE); + block->t.type = convert(type_t, block->t.type & ~REACHABLE); #if CONFIG_GEN_GC block->t.gen = 1; #endif @@ -471,7 +471,7 @@ static int sweep_one(obj_t *block) } finalize(block); - block->t.type = (type_t) (block->t.type | FREE); + block->t.type = convert(type_t, block->t.type | FREE); /* If debugging is turned on, we want to catch instances where a reachable object is wrongly freed. This is difficult @@ -703,7 +703,7 @@ void unmark(void) block < end; block++) { - block->t.type = (type_t) (block->t.type & ~(FREE | REACHABLE)); + block->t.type = convert(type_t, block->t.type & ~(FREE | REACHABLE)); } } } @@ -105,7 +105,7 @@ static cnum hash_double(double n) unsigned long h = 0; #endif - mem_t *p = (mem_t *) &n, *q = p + sizeof(double); + mem_t *p = coerce(mem_t *, &n), *q = p + sizeof(double); while (p < q) { h = h << 8 | h >> (8 * sizeof h - 1); @@ -135,13 +135,13 @@ static cnum equal_hash(val obj) case ENV: switch (sizeof (mem_t *)) { case 4: - return (((cnum) obj) >> 4) & NUM_MAX; + return (coerce(cnum, obj) >> 4) & NUM_MAX; case 8: default: - return (((cnum) obj) >> 5) & NUM_MAX; + return (coerce(cnum, obj) >> 5) & NUM_MAX; } break; case FUN: - return ((cnum) obj->f.f.interp_fun + equal_hash(obj->f.env)) & NUM_MAX; + return (coerce(cnum, obj->f.f.interp_fun) + equal_hash(obj->f.env)) & NUM_MAX; case VEC: { val length = obj->v.vec[vec_length]; @@ -183,9 +183,9 @@ static cnum eql_hash(val obj) default: switch (sizeof (mem_t *)) { case 4: - return (((cnum) obj) >> 4) & NUM_MAX; + return (coerce(cnum, obj) >> 4) & NUM_MAX; case 8: default: - return (((cnum) obj) >> 5) & NUM_MAX; + return (coerce(cnum, obj) >> 5) & NUM_MAX; } } case TAG_CHR: @@ -195,9 +195,9 @@ static cnum eql_hash(val obj) case TAG_LIT: switch (sizeof (mem_t *)) { case 4: - return (((cnum) obj) >> 2) & NUM_MAX; + return (coerce(cnum, obj) >> 2) & NUM_MAX; case 8: default: - return (((cnum) obj) >> 3) & NUM_MAX; + return (coerce(cnum, obj) >> 3) & NUM_MAX; } } /* notreached */ @@ -208,9 +208,9 @@ cnum cobj_hash_op(val obj) { switch (sizeof (mem_t *)) { case 4: - return (((cnum) obj) >> 4) & NUM_MAX; + return (coerce(cnum, obj) >> 4) & NUM_MAX; case 8: default: - return (((cnum) obj) >> 5) & NUM_MAX; + return (coerce(cnum, obj) >> 5) & NUM_MAX; } /* notreached */ abort(); @@ -228,8 +228,8 @@ static val print_key_val(val out, val key, val value) static val hash_equal_op(val left, val right) { uses_or2; - struct hash *l = (struct hash *) left->co.handle; - struct hash *r = (struct hash *) right->co.handle; + struct hash *l = coerce(struct hash *, left->co.handle); + struct hash *r = coerce(struct hash *, right->co.handle); val liter, riter, lcell, rcell; val free_conses = nil; val pending = nil; @@ -314,14 +314,14 @@ static val hash_equal_op(val left, val right) static cnum hash_hash_op(val obj) { cnum out = 0; - struct hash *h = (struct hash *) obj->co.handle; + struct hash *h = coerce(struct hash *, obj->co.handle); val iter, cell; switch (sizeof (mem_t *)) { case 4: - out += ((cnum) h->hash_fun) >> 4; + out += coerce(cnum, h->hash_fun) >> 4; case 8: default: - out += ((cnum) h->hash_fun) >> 5; + out += coerce(cnum, h->hash_fun) >> 5; } out += equal_hash(h->userdata); @@ -339,7 +339,7 @@ static cnum hash_hash_op(val obj) static void hash_print_op(val hash, val out) { - struct hash *h = (struct hash *) hash->co.handle; + struct hash *h = coerce(struct hash *, hash->co.handle); int need_space = 0; put_string(lit("#H(("), out); @@ -371,7 +371,7 @@ static void hash_print_op(val hash, val out) static void hash_mark(val hash) { - struct hash *h = (struct hash *) hash->co.handle; + struct hash *h = coerce(struct hash *, hash->co.handle); cnum i; gc_mark(h->userdata); @@ -463,12 +463,12 @@ val make_hash(val weak_keys, val weak_vals, val equal_based) nao); } else { int flags = ((weak_vals != nil) << 1) | (weak_keys != nil); - struct hash *h = (struct hash *) chk_malloc(sizeof *h); + struct hash *h = coerce(struct hash *, chk_malloc(sizeof *h)); val mod = num_fast(256); val table = vector(mod, nil); - val hash = cobj((mem_t *) h, hash_s, &hash_ops); + val hash = cobj(coerce(mem_t *, h), hash_s, &hash_ops); - h->flags = (hash_flags_t) flags; + h->flags = convert(hash_flags_t, flags); h->modulus = c_num(mod); h->count = 0; h->table = table; @@ -485,11 +485,11 @@ val make_hash(val weak_keys, val weak_vals, val equal_based) val make_similar_hash(val existing) { - struct hash *ex = (struct hash *) cobj_handle(existing, hash_s); - struct hash *h = (struct hash *) chk_malloc(sizeof *h); + struct hash *ex = coerce(struct hash *, cobj_handle(existing, hash_s)); + struct hash *h = coerce(struct hash *, chk_malloc(sizeof *h)); val mod = num_fast(256); val table = vector(mod, nil); - val hash = cobj((mem_t *) h, hash_s, &hash_ops); + val hash = cobj(coerce(mem_t *, h), hash_s, &hash_ops); h->modulus = c_num(mod); h->count = 0; @@ -507,9 +507,9 @@ val make_similar_hash(val existing) val copy_hash(val existing) { - struct hash *ex = (struct hash *) cobj_handle(existing, hash_s); - struct hash *h = (struct hash *) chk_malloc(sizeof *h); - val hash = cobj((mem_t *) h, hash_s, &hash_ops); + struct hash *ex = coerce(struct hash *, cobj_handle(existing, hash_s)); + struct hash *h = coerce(struct hash *, chk_malloc(sizeof *h)); + val hash = cobj(coerce(mem_t *, h), hash_s, &hash_ops); val mod = num_fast(ex->modulus); val iter; @@ -531,7 +531,7 @@ val copy_hash(val existing) val gethash_c(val hash, val key, loc new_p) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); loc pchain = vecref_l(h->table, num_fast(h->hash_fun(key) % h->modulus)); val old = deref(pchain); val cell = h->acons_new_c_fun(key, new_p, pchain); @@ -542,7 +542,7 @@ val gethash_c(val hash, val key, loc new_p) val gethash(val hash, val key) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); val chain = vecref(h->table, num_fast(h->hash_fun(key) % h->modulus)); val found = h->assoc_fun(key, chain); return cdr(found); @@ -566,7 +566,7 @@ val inhash(val hash, val key, val init) val gethash_f(val hash, val key, loc found) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); val chain = vecref(h->table, num_fast(h->hash_fun(key) % h->modulus)); set(found, h->assoc_fun(key, chain)); return cdr(deref(found)); @@ -574,7 +574,7 @@ val gethash_f(val hash, val key, loc found) val gethash_n(val hash, val key, val notfound_val) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); val chain = vecref(h->table, num_fast(h->hash_fun(key) % h->modulus)); val existing = h->assoc_fun(key, chain); return if3(existing, cdr(existing), default_bool_arg(notfound_val)); @@ -596,7 +596,7 @@ val pushhash(val hash, val key, val value) val remhash(val hash, val key) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); loc pchain = vecref_l(h->table, num_fast(h->hash_fun(key) % h->modulus)); val existing = h->assoc_fun(key, deref(pchain)); @@ -612,19 +612,19 @@ val remhash(val hash, val key) val hash_count(val hash) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); return num_fast(h->count); } val get_hash_userdata(val hash) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); return h->userdata; } val set_hash_userdata(val hash, val data) { - struct hash *h = (struct hash *) cobj_handle(hash, hash_s); + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); val olddata = h->userdata; set(mkloc(h->userdata, hash), data); return olddata; @@ -637,7 +637,7 @@ val hashp(val obj) static void hash_iter_mark(val hash_iter) { - struct hash_iter *hi = (struct hash_iter *) hash_iter->co.handle; + struct hash_iter *hi = coerce(struct hash_iter *, hash_iter->co.handle); gc_mark(hi->hash); gc_mark(hi->cons); } @@ -656,20 +656,20 @@ val hash_begin(val hash) struct hash_iter *hi; class_check (hash, hash_s); - hi = (struct hash_iter *) chk_malloc(sizeof *hi); + hi = coerce(struct hash_iter *, chk_malloc(sizeof *hi)); hi->hash = nil; hi->chain = -1; hi->cons = nil; - hi_obj = cobj((mem_t *) hi, hash_iter_s, &hash_iter_ops); + hi_obj = cobj(coerce(mem_t *, hi), hash_iter_s, &hash_iter_ops); hi->hash = hash; return hi_obj; } val hash_next(val iter) { - struct hash_iter *hi = (struct hash_iter *) cobj_handle(iter, hash_iter_s); + struct hash_iter *hi = coerce(struct hash_iter *, cobj_handle(iter, hash_iter_s)); val hash = hi->hash; - struct hash *h = (struct hash *) hash->co.handle; + struct hash *h = coerce(struct hash *, hash->co.handle); if (hi->cons) hi->cons = cdr(hi->cons); while (nilp(hi->cons)) { @@ -926,8 +926,8 @@ val hash_alist(val hash) val hash_uni(val hash1, val hash2, val join_func) { - struct hash *h1 = (struct hash *) cobj_handle(hash1, hash_s); - struct hash *h2 = (struct hash *) cobj_handle(hash2, hash_s); + struct hash *h1 = coerce(struct hash *, cobj_handle(hash1, hash_s)); + struct hash *h2 = coerce(struct hash *, cobj_handle(hash2, hash_s)); if (h1->hash_fun != h2->hash_fun) uw_throwf(error_s, lit("hash-uni: ~a and ~a are incompatible hashes"), hash1, hash2, nao); @@ -961,8 +961,8 @@ val hash_uni(val hash1, val hash2, val join_func) val hash_diff(val hash1, val hash2) { - struct hash *h1 = (struct hash *) cobj_handle(hash1, hash_s); - struct hash *h2 = (struct hash *) cobj_handle(hash2, hash_s); + struct hash *h1 = coerce(struct hash *, cobj_handle(hash1, hash_s)); + struct hash *h2 = coerce(struct hash *, cobj_handle(hash2, hash_s)); if (h1->hash_fun != h2->hash_fun) uw_throwf(error_s, lit("hash-diff: ~a and ~a are incompatible hashes"), hash1, hash2, nao); @@ -984,8 +984,8 @@ val hash_diff(val hash1, val hash2) val hash_isec(val hash1, val hash2, val join_func) { - struct hash *h1 = (struct hash *) cobj_handle(hash1, hash_s); - struct hash *h2 = (struct hash *) cobj_handle(hash2, hash_s); + struct hash *h1 = coerce(struct hash *, cobj_handle(hash1, hash_s)); + struct hash *h2 = coerce(struct hash *, cobj_handle(hash2, hash_s)); if (h1->hash_fun != h2->hash_fun) uw_throwf(error_s, lit("hash-uni: ~a and ~a are incompatible hashes"), hash1, hash2, nao); @@ -135,7 +135,7 @@ val identity(val obj) static val code2type(int code) { - switch ((type_t) code) { + switch (convert(type_t, code)) { case NIL: return null_s; case CONS: return cons_s; case STR: return str_s; @@ -1702,34 +1702,34 @@ alloc_bytes_t malloc_bytes; mem_t *chk_malloc(size_t size) { - mem_t *ptr = (mem_t *) malloc(size); + mem_t *ptr = convert(mem_t *, malloc(size)); assert (!async_sig_enabled); if (size && ptr == 0) - ptr = (mem_t *) oom_realloc(0, size); + ptr = convert(mem_t *, oom_realloc(0, size)); malloc_bytes += size; return ptr; } mem_t *chk_malloc_gc_more(size_t size) { - mem_t *ptr = (mem_t *) malloc(size); + mem_t *ptr = convert(mem_t *, malloc(size)); assert (!async_sig_enabled); if (size && ptr == 0) - ptr = (mem_t *) oom_realloc(0, size); + ptr = convert(mem_t *, oom_realloc(0, size)); return ptr; } mem_t *chk_calloc(size_t n, size_t size) { - mem_t *ptr = (mem_t *) calloc(n, size); - cnum total = (cnum) size * (cnum) n; + mem_t *ptr = convert(mem_t *, calloc(n, size)); + cnum total = convert(cnum, size) * convert(cnum, n); assert (!async_sig_enabled); if (size && ptr == 0) { - ptr = (mem_t *) oom_realloc(0, total); + ptr = convert(mem_t *, oom_realloc(0, total)); memset(ptr, 0, total); } malloc_bytes += total; @@ -1738,7 +1738,7 @@ mem_t *chk_calloc(size_t n, size_t size) mem_t *chk_realloc(mem_t *old, size_t size) { - mem_t *newptr = (mem_t *) realloc(old, size); + mem_t *newptr = convert(mem_t *, realloc(old, size)); assert (!async_sig_enabled); @@ -1751,7 +1751,7 @@ mem_t *chk_realloc(mem_t *old, size_t size) wchar_t *chk_strdup(const wchar_t *str) { size_t nchar = wcslen(str) + 1; - wchar_t *copy = (wchar_t *) chk_malloc(nchar * sizeof *copy); + wchar_t *copy = coerce(wchar_t *, chk_malloc(nchar * sizeof *copy)); assert (!async_sig_enabled); wmemcpy(copy, str, nchar); return copy; @@ -1918,7 +1918,7 @@ val improper_plist_to_alist(val list, val boolean_keys) val num(cnum n) { if (n >= NUM_MIN && n <= NUM_MAX) - return (val) ((n << TAG_SHIFT) | TAG_NUM); + return coerce(val, (n << TAG_SHIFT) | TAG_NUM); return bignum(n); } @@ -1926,7 +1926,7 @@ cnum c_num(val num) { switch (type(num)) { case CHR: case NUM: - return ((cnum) num) >> TAG_SHIFT; + return coerce(cnum, num) >> TAG_SHIFT; case BGNUM: if (in_int_ptr_range(num)) { int_ptr_t out; @@ -2182,7 +2182,7 @@ val string(const wchar_t *str) { val obj = make_obj(); obj->st.type = STR; - obj->st.str = (wchar_t *) chk_strdup(str); + obj->st.str = coerce(wchar_t *, chk_strdup(str)); obj->st.len = nil; obj->st.alloc = nil; return obj; @@ -2201,7 +2201,7 @@ val string_utf8(const char *str) val mkstring(val len, val ch) { size_t nchar = c_num(len) + 1; - wchar_t *str = (wchar_t *) chk_malloc(nchar * sizeof *str); + wchar_t *str = coerce(wchar_t *, chk_malloc(nchar * sizeof *str)); val s = string_own(str); wmemset(str, c_chr(ch), nchar); s->st.len = len; @@ -2212,7 +2212,7 @@ val mkstring(val len, val ch) val mkustring(val len) { cnum l = c_num(len); - wchar_t *str = (wchar_t *) chk_malloc((l + 1) * sizeof *str); + wchar_t *str = coerce(wchar_t *, chk_malloc((l + 1) * sizeof *str)); val s = string_own(str); str[l] = 0; s->st.len = len; @@ -2234,7 +2234,7 @@ val copy_str(val str) val upcase_str(val str) { val len = length_str(str); - wchar_t *dst = (wchar_t *) chk_malloc((c_num(len) + 1) * sizeof *dst); + wchar_t *dst = coerce(wchar_t *, chk_malloc((c_num(len) + 1) * sizeof *dst)); const wchar_t *src = c_str(str); val out = string_own(dst); @@ -2247,7 +2247,7 @@ val upcase_str(val str) val downcase_str(val str) { val len = length_str(str); - wchar_t *dst = (wchar_t *) chk_malloc((c_num(len) + 1) * sizeof *dst); + wchar_t *dst = coerce(wchar_t *, chk_malloc((c_num(len) + 1) * sizeof *dst)); const wchar_t *src = c_str(str); val out = string_own(dst); @@ -2289,8 +2289,8 @@ val string_extend(val str, val tail) if (gt(needed, room)) uw_throwf(error_s, lit("string-extend: overflow"), nao); - str->st.str = (wchar_t *) chk_realloc((mem_t *) str->st.str, - alloc * sizeof *str->st.str); + str->st.str = coerce(wchar_t *, chk_realloc(coerce(mem_t *, str->st.str), + alloc * sizeof *str->st.str)); set(mkloc(str->st.alloc, str), num(alloc)); set(mkloc(str->st.len, str), plus(str->st.len, needed)); @@ -2578,7 +2578,7 @@ val sub_str(val str_in, val from, val to) return null_string; } else { size_t nchar = c_num(to) - c_num(from) + 1; - wchar_t *sub = (wchar_t *) chk_malloc(nchar * sizeof (wchar_t)); + wchar_t *sub = coerce(wchar_t *, chk_malloc(nchar * sizeof *sub)); const wchar_t *str = c_str(str_in); wcsncpy(sub, str + c_num(from), nchar); sub[nchar-1] = 0; @@ -2708,7 +2708,7 @@ val cat_str(val list, val sep) item, nao); } - str = (wchar_t *) chk_malloc((total + 1) * sizeof *str); + str = coerce(wchar_t *, chk_malloc((total + 1) * sizeof *str)); for (ptr = str, iter = list; iter != nil; iter = cdr(iter)) { val item = car(iter); @@ -2906,7 +2906,7 @@ val trim_str(val str) return null_string; } else { size_t len = end - start; - wchar_t *buf = (wchar_t *) chk_malloc((len + 1) * sizeof *buf); + wchar_t *buf = coerce(wchar_t *, chk_malloc((len + 1) * sizeof *buf)); wmemcpy(buf, start, len); buf[len] = 0; return string_own(buf); @@ -3179,7 +3179,7 @@ wchar_t c_chr(val chr) { if (!is_chr(chr)) type_mismatch(lit("~s is not a character"), chr, nao); - return (wchar_t) ((cnum) chr >> TAG_SHIFT); + return convert(wchar_t, coerce(cnum, chr) >> TAG_SHIFT); } val chr_isalnum(val ch) @@ -4598,10 +4598,10 @@ val vector(val length, val initval) int i; cnum alloc_plus = c_num(length) + 2; size_t size = alloc_plus * sizeof (val); - val *v = ((cnum) (size / sizeof *v) == alloc_plus) - ? (val *) chk_malloc(size) - : (val *) uw_throwf(error_s, lit("vector: length ~a is too large"), - length, nao); + val *v = (convert(cnum, size / sizeof *v) == alloc_plus) + ? coerce(val *, chk_malloc(size)) + : coerce(val *, uw_throwf(error_s, lit("vector: length ~a is too large"), + length, nao)); val vec = make_obj(); initval = default_bool_arg(initval); #if HAVE_VALGRIND @@ -4633,14 +4633,14 @@ val vec_set_length(val vec, val length) cnum length_delta = new_length - old_length; cnum alloc_delta = new_length - old_alloc; - if (new_length > (cnum) ((size_t) -1/sizeof (val) - 2)) + if (new_length > convert(cnum, (convert(size_t, -1)/sizeof (val) - 2))) uw_throwf(error_s, lit("vec-set-length: cannot extend to length ~s"), length, nao); if (alloc_delta > 0) { cnum new_alloc = max(new_length, 2*old_alloc); - val *newvec = (val *) chk_realloc((mem_t *) (vec->v.vec - 2), - (new_alloc + 2) * sizeof *newvec); + val *newvec = coerce(val *, chk_realloc(coerce(mem_t *, vec->v.vec - 2), + (new_alloc + 2) * sizeof *newvec)); vec->v.vec = newvec + 2; set(mkloc(vec->v.vec[vec_alloc], vec), num(new_alloc)); #if HAVE_VALGRIND @@ -4737,7 +4737,7 @@ val copy_vec(val vec_in) val length = length_vec(vec_in); cnum alloc_plus = c_num(length) + 2; val vec = make_obj(); - val *v = (val *) chk_malloc(alloc_plus * sizeof *v); + val *v = coerce(val *, chk_malloc(alloc_plus * sizeof *v)); #if HAVE_VALGRIND vec->v.vec_true_start = v; #endif @@ -4778,7 +4778,7 @@ val sub_vec(val vec_in, val from, val to) cnum cfrom = c_num(from); size_t nelem = c_num(to) - cfrom; val vec = make_obj(); - val *v = (val *) chk_malloc((nelem + 2) * sizeof *v); + val *v = coerce(val *, chk_malloc((nelem + 2) * sizeof *v)); #if HAVE_VALGRIND vec->v.vec_true_start = v; #endif @@ -4900,11 +4900,11 @@ val cat_vec(val list) total = newtot; } - if (total > ((size_t) -1)/(sizeof (val)) - 2) + if (total > (convert(size_t, -1)/(sizeof (val)) - 2)) goto toobig; vec = make_obj(); - v = (val *) chk_malloc((total + 2) * sizeof *v); + v = coerce(val *, chk_malloc((total + 2) * sizeof *v)); #if HAVE_VALGRIND vec->v.vec_true_start = v; @@ -5177,7 +5177,7 @@ void cobj_print_op(val obj, val out) { put_string(lit("#<"), out); obj_print(obj->co.cls, out); - format(out, lit(": ~p>"), (val) obj->co.handle, nao); + format(out, lit(": ~p>"), coerce(val, obj->co.handle), nao); } static val cptr_equal_op(val left, val right) @@ -6235,7 +6235,7 @@ static void obj_init(void) &null_list, &equal_f, &eq_f, &eql_f, &car_f, &cdr_f, &null_f, &list_f, &identity_f, &less_f, &greater_f, &prog_string, &env_list, - (val *) 0); + convert(val *, 0)); nil_string = lit("nil"); null_string = lit(""); @@ -28,8 +28,18 @@ typedef int_ptr_t cnum; +#ifdef __cplusplus +#define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR)) +#define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR)) +#define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR)) +#else +#define strip_qual(TYPE, EXPR) ((TYPE) (EXPR)) +#define convert(TYPE, EXPR) ((TYPE) (EXPR)) +#define coerce(TYPE, EXPR) ((TYPE) (EXPR)) +#endif + #define TAG_SHIFT 2 -#define TAG_MASK (((cnum) 1 << TAG_SHIFT) - 1) +#define TAG_MASK ((convert(cnum, 1) << TAG_SHIFT) - 1) #define TAG_PTR 0 #define TAG_NUM 1 #define TAG_CHR 2 @@ -273,7 +283,7 @@ typedef val *loc; #define mpush(val, lo) (push(val, lo)) #endif -INLINE cnum tag(val obj) { return ((cnum) obj) & TAG_MASK; } +INLINE cnum tag(val obj) { return coerce(cnum, obj) & TAG_MASK; } INLINE int is_ptr(val obj) { return obj && tag(obj) == TAG_PTR; } INLINE int is_num(val obj) { return tag(obj) == TAG_NUM; } INLINE int is_chr(val obj) { return tag(obj) == TAG_CHR; } @@ -281,8 +291,9 @@ INLINE int is_lit(val obj) { return tag(obj) == TAG_LIT; } INLINE type_t type(val obj) { - return obj ? tag(obj) - ? (type_t) tag(obj) + cnum tg = tag(obj); + return obj ? tg + ? convert(type_t, tg) : obj->t.type : NIL; } @@ -290,11 +301,15 @@ INLINE type_t type(val obj) typedef struct wli wchli_t; #if LIT_ALIGN < 4 -#define wli_noex(lit) ((const wchli_t *) (L"\0" L ## lit L"\0" + 1)) +#define wli_noex(lit) (coerce(const wchli_t *,\ + convert(const wchar_t *,\ + "\0" L ## lit L"\0" + 1))) #define wini(ini) L"\0" L ## ini L"\0" #define wref(arr) ((arr) + 1) #else -#define wli_noex(lit) ((const wchli_t *) L ## lit) +#define wli_noex(lit) (coerce(const wchli_t *,\ + convert(const wchar_t *,\ + L ## lit))) #define wini(ini) L ## ini #define wref(arr) (arr) #endif @@ -302,27 +317,27 @@ typedef struct wli wchli_t; INLINE val auto_str(const wchli_t *str) { - return (val) (((cnum) str) | TAG_LIT); + return coerce(val, coerce(cnum, str) | TAG_LIT); } INLINE val static_str(const wchli_t *str) { - return (val) (((cnum) str) | TAG_LIT); + return coerce(val, coerce(cnum, str) | TAG_LIT); } INLINE wchar_t *litptr(val obj) { #if LIT_ALIGN < 4 - wchar_t *ret = (wchar_t *) ((cnum) obj & ~TAG_MASK); + wchar_t *ret = coerce(wchar_t *, (coerce(cnum, obj) & ~TAG_MASK)); return (*ret == 0) ? ret + 1 : ret; #else - return (wchar_t *) ((cnum) obj & ~TAG_MASK); + return coerce(wchar_t *, coerce(cnum, obj) & ~TAG_MASK); #endif } INLINE val num_fast(cnum n) { - return (val) ((n << TAG_SHIFT) | TAG_NUM); + return coerce(val, (n << TAG_SHIFT) | TAG_NUM); } INLINE mp_int *mp(val bign) @@ -332,13 +347,15 @@ INLINE mp_int *mp(val bign) INLINE val chr(wchar_t ch) { - return (val) (((cnum) ch << TAG_SHIFT) | TAG_CHR); + return coerce(val, (convert(cnum, ch) << TAG_SHIFT) | TAG_CHR); } #if LIT_ALIGN < 4 -#define lit_noex(strlit) ((obj_t *) ((cnum) (L"\0" L ## strlit L"\0" + 1) | TAG_LIT)) +#define lit_noex(strlit) coerce(obj_t *,\ + coerce(cnum, L"\0" L ## strlit L"\0" + 1) | \ + TAG_LIT) #else -#define lit_noex(strlit) ((obj_t *) ((cnum) (L ## strlit) | TAG_LIT)) +#define lit_noex(strlit) coerce(obj_t *, coerce(cnum, L ## strlit) | TAG_LIT) #endif #define lit(strlit) lit_noex(strlit) @@ -810,7 +827,7 @@ void dump(val obj, val stream); void d(val obj); void breakpt(void); -#define nil ((obj_t *) 0) +#define nil convert(obj_t *, 0) INLINE val eq(val a, val b) { return a == b ? t : nil; } @@ -818,7 +835,7 @@ INLINE val null(val v) { return v ? nil : t; } #define nilp(o) ((o) == nil) -#define nao ((obj_t *) (1 << TAG_SHIFT)) /* "not an object" sentinel value. */ +#define nao coerce(obj_t *, 1 << TAG_SHIFT) /* "not an object" sentinel value. */ #define missingp(v) ((v) == colon_k) @@ -1214,7 +1214,7 @@ static val do_match_line(match_line_ctx *c) } else { val entry = gethash(h_directive_table, directive); if (entry) { - h_match_func hmf = (h_match_func) cptr_get(entry); + h_match_func hmf = coerce(h_match_func, cptr_get(entry)); val result = hmf(c); if (result == next_spec_k) { @@ -3064,7 +3064,7 @@ static val hv_trampoline(match_line_ctx *c) internal_error("hv_trampoline: missing dispatch table entry"); { - v_match_func vmf = (v_match_func) cptr_get(entry); + v_match_func vmf = coerce(v_match_func, cptr_get(entry)); ret = vmf(&mf); if (ret == next_spec_k) c->bindings = mf.bindings; @@ -3899,7 +3899,7 @@ repeat_spec_same_data: val entry = gethash(v_directive_table, sym); if (entry) { - v_match_func vmf = (v_match_func) cptr_get(entry); + v_match_func vmf = coerce(v_match_func, cptr_get(entry)); val result; result = vmf(&c); @@ -4090,74 +4090,74 @@ static void dir_tables_init(void) non_matching_directive_table = make_hash(nil, nil, nil); protect(&h_directive_table, &v_directive_table, - &non_matching_directive_table, (val *) 0); - - sethash(v_directive_table, skip_s, cptr((mem_t *) v_skip)); - sethash(v_directive_table, fuzz_s, cptr((mem_t *) v_fuzz)); - sethash(v_directive_table, trailer_s, cptr((mem_t *) v_trailer)); - sethash(v_directive_table, freeform_s, cptr((mem_t *) v_freeform)); - sethash(v_directive_table, block_s, cptr((mem_t *) v_block)); - sethash(v_directive_table, accept_s, cptr((mem_t *) v_accept_fail)); - sethash(v_directive_table, fail_s, cptr((mem_t *) v_accept_fail)); - sethash(v_directive_table, next_s, cptr((mem_t *) v_next)); - sethash(v_directive_table, some_s, cptr((mem_t *) v_parallel)); - sethash(v_directive_table, all_s, cptr((mem_t *) v_parallel)); - sethash(v_directive_table, none_s, cptr((mem_t *) v_parallel)); - sethash(v_directive_table, maybe_s, cptr((mem_t *) v_parallel)); - sethash(v_directive_table, cases_s, cptr((mem_t *) v_parallel)); - sethash(v_directive_table, choose_s, cptr((mem_t *) v_parallel)); - sethash(v_directive_table, gather_s, cptr((mem_t *) v_gather)); - sethash(v_directive_table, collect_s, cptr((mem_t *) v_collect)); - sethash(v_directive_table, repeat_s, cptr((mem_t *) v_collect)); - sethash(v_directive_table, flatten_s, cptr((mem_t *) v_flatten)); - sethash(v_directive_table, forget_s, cptr((mem_t *) v_forget_local)); - sethash(v_directive_table, local_s, cptr((mem_t *) v_forget_local)); - sethash(v_directive_table, merge_s, cptr((mem_t *) v_merge)); - sethash(v_directive_table, bind_s, cptr((mem_t *) v_bind)); - sethash(v_directive_table, rebind_s, cptr((mem_t *) v_rebind)); - sethash(v_directive_table, set_s, cptr((mem_t *) v_set)); - sethash(v_directive_table, cat_s, cptr((mem_t *) v_cat)); - sethash(v_directive_table, output_s, cptr((mem_t *) v_output)); - sethash(v_directive_table, define_s, cptr((mem_t *) v_define)); - sethash(v_directive_table, try_s, cptr((mem_t *) v_try)); - sethash(v_directive_table, defex_s, cptr((mem_t *) v_defex)); - sethash(v_directive_table, throw_s, cptr((mem_t *) v_throw)); - sethash(v_directive_table, deffilter_s, cptr((mem_t *) v_deffilter)); - sethash(v_directive_table, filter_s, cptr((mem_t *) v_filter)); - sethash(v_directive_table, eof_s, cptr((mem_t *) v_eof)); - sethash(v_directive_table, do_s, cptr((mem_t *) v_do)); - sethash(v_directive_table, require_s, cptr((mem_t *) v_require)); - sethash(v_directive_table, assert_s, cptr((mem_t *) v_assert)); - sethash(v_directive_table, load_s, cptr((mem_t *) v_load)); - sethash(v_directive_table, close_s, cptr((mem_t *) v_close)); - sethash(v_directive_table, line_s, cptr((mem_t *) v_line)); - - sethash(h_directive_table, text_s, cptr((mem_t *) h_text)); - sethash(h_directive_table, var_s, cptr((mem_t *) h_var)); - sethash(h_directive_table, skip_s, cptr((mem_t *) h_skip)); - sethash(h_directive_table, coll_s, cptr((mem_t *) h_coll)); - sethash(h_directive_table, flatten_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, forget_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, local_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, merge_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, bind_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, rebind_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, set_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, cat_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, filter_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, some_s, cptr((mem_t *) h_parallel)); - sethash(h_directive_table, all_s, cptr((mem_t *) h_parallel)); - sethash(h_directive_table, none_s, cptr((mem_t *) h_parallel)); - sethash(h_directive_table, maybe_s, cptr((mem_t *) h_parallel)); - sethash(h_directive_table, cases_s, cptr((mem_t *) h_parallel)); - sethash(h_directive_table, choose_s, cptr((mem_t *) h_parallel)); - sethash(h_directive_table, trailer_s, cptr((mem_t *) h_trailer)); - sethash(h_directive_table, define_s, cptr((mem_t *) h_define)); - sethash(h_directive_table, eol_s, cptr((mem_t *) h_eol)); - sethash(h_directive_table, chr_s, cptr((mem_t *) h_chr)); - sethash(h_directive_table, do_s, cptr((mem_t *) h_do)); - sethash(h_directive_table, require_s, cptr((mem_t *) hv_trampoline)); - sethash(h_directive_table, assert_s, cptr((mem_t *) h_assert)); + &non_matching_directive_table, convert(val *, 0)); + + sethash(v_directive_table, skip_s, cptr(coerce(mem_t *, v_skip))); + sethash(v_directive_table, fuzz_s, cptr(coerce(mem_t *, v_fuzz))); + sethash(v_directive_table, trailer_s, cptr(coerce(mem_t *, v_trailer))); + sethash(v_directive_table, freeform_s, cptr(coerce(mem_t *, v_freeform))); + sethash(v_directive_table, block_s, cptr(coerce(mem_t *, v_block))); + sethash(v_directive_table, accept_s, cptr(coerce(mem_t *, v_accept_fail))); + sethash(v_directive_table, fail_s, cptr(coerce(mem_t *, v_accept_fail))); + sethash(v_directive_table, next_s, cptr(coerce(mem_t *, v_next))); + sethash(v_directive_table, some_s, cptr(coerce(mem_t *, v_parallel))); + sethash(v_directive_table, all_s, cptr(coerce(mem_t *, v_parallel))); + sethash(v_directive_table, none_s, cptr(coerce(mem_t *, v_parallel))); + sethash(v_directive_table, maybe_s, cptr(coerce(mem_t *, v_parallel))); + sethash(v_directive_table, cases_s, cptr(coerce(mem_t *, v_parallel))); + sethash(v_directive_table, choose_s, cptr(coerce(mem_t *, v_parallel))); + sethash(v_directive_table, gather_s, cptr(coerce(mem_t *, v_gather))); + sethash(v_directive_table, collect_s, cptr(coerce(mem_t *, v_collect))); + sethash(v_directive_table, repeat_s, cptr(coerce(mem_t *, v_collect))); + sethash(v_directive_table, flatten_s, cptr(coerce(mem_t *, v_flatten))); + sethash(v_directive_table, forget_s, cptr(coerce(mem_t *, v_forget_local))); + sethash(v_directive_table, local_s, cptr(coerce(mem_t *, v_forget_local))); + sethash(v_directive_table, merge_s, cptr(coerce(mem_t *, v_merge))); + sethash(v_directive_table, bind_s, cptr(coerce(mem_t *, v_bind))); + sethash(v_directive_table, rebind_s, cptr(coerce(mem_t *, v_rebind))); + sethash(v_directive_table, set_s, cptr(coerce(mem_t *, v_set))); + sethash(v_directive_table, cat_s, cptr(coerce(mem_t *, v_cat))); + sethash(v_directive_table, output_s, cptr(coerce(mem_t *, v_output))); + sethash(v_directive_table, define_s, cptr(coerce(mem_t *, v_define))); + sethash(v_directive_table, try_s, cptr(coerce(mem_t *, v_try))); + sethash(v_directive_table, defex_s, cptr(coerce(mem_t *, v_defex))); + sethash(v_directive_table, throw_s, cptr(coerce(mem_t *, v_throw))); + sethash(v_directive_table, deffilter_s, cptr(coerce(mem_t *, v_deffilter))); + sethash(v_directive_table, filter_s, cptr(coerce(mem_t *, v_filter))); + sethash(v_directive_table, eof_s, cptr(coerce(mem_t *, v_eof))); + sethash(v_directive_table, do_s, cptr(coerce(mem_t *, v_do))); + sethash(v_directive_table, require_s, cptr(coerce(mem_t *, v_require))); + sethash(v_directive_table, assert_s, cptr(coerce(mem_t *, v_assert))); + sethash(v_directive_table, load_s, cptr(coerce(mem_t *, v_load))); + sethash(v_directive_table, close_s, cptr(coerce(mem_t *, v_close))); + sethash(v_directive_table, line_s, cptr(coerce(mem_t *, v_line))); + + sethash(h_directive_table, text_s, cptr(coerce(mem_t *, h_text))); + sethash(h_directive_table, var_s, cptr(coerce(mem_t *, h_var))); + sethash(h_directive_table, skip_s, cptr(coerce(mem_t *, h_skip))); + sethash(h_directive_table, coll_s, cptr(coerce(mem_t *, h_coll))); + sethash(h_directive_table, flatten_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, forget_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, local_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, merge_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, bind_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, rebind_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, set_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, cat_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, filter_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, some_s, cptr(coerce(mem_t *, h_parallel))); + sethash(h_directive_table, all_s, cptr(coerce(mem_t *, h_parallel))); + sethash(h_directive_table, none_s, cptr(coerce(mem_t *, h_parallel))); + sethash(h_directive_table, maybe_s, cptr(coerce(mem_t *, h_parallel))); + sethash(h_directive_table, cases_s, cptr(coerce(mem_t *, h_parallel))); + sethash(h_directive_table, choose_s, cptr(coerce(mem_t *, h_parallel))); + sethash(h_directive_table, trailer_s, cptr(coerce(mem_t *, h_trailer))); + sethash(h_directive_table, define_s, cptr(coerce(mem_t *, h_define))); + sethash(h_directive_table, eol_s, cptr(coerce(mem_t *, h_eol))); + sethash(h_directive_table, chr_s, cptr(coerce(mem_t *, h_chr))); + sethash(h_directive_table, do_s, cptr(coerce(mem_t *, h_do))); + sethash(h_directive_table, require_s, cptr(coerce(mem_t *, hv_trampoline))); + sethash(h_directive_table, assert_s, cptr(coerce(mem_t *, h_assert))); sethash(non_matching_directive_table, block_s, t); sethash(non_matching_directive_table, accept_s, t); @@ -696,7 +696,7 @@ UONLY {U2}{U}|{U3}{U}{U}|{U4}{U}{U}{U} <SPECIAL,QSPECIAL,NESTED,BRACED>. { yyerrprepf(yyg, lit("non-UTF-8 byte in directive: '\\x~02x'"), - num((unsigned char) yytext[0]), nao); + num(convert(unsigned char, yytext[0])), nao); return ERRTOK; } @@ -765,7 +765,7 @@ UONLY {U2}{U}|{U3}{U}{U}|{U4}{U}{U}{U} <REGEX>. { yyerrprepf(yyg, lit("non-UTF-8 byte in regex: '\\x~02x'"), - num((unsigned char) yytext[0]), nao); + num(convert(unsigned char, yytext[0])), nao); return ERRTOK; } @@ -899,7 +899,7 @@ UONLY {U2}{U}|{U3}{U}{U}|{U4}{U}{U}{U} <STRLIT,CHRLIT,QSILIT,WLIT,QWLIT>. { yyerrprepf(yyg, lit("non-UTF-8 byte in literal: '\\x~02x'"), - num((unsigned char) yytext[0]), nao); + num(convert(unsigned char, yytext[0])), nao); return ERRTOK; } @@ -1363,7 +1363,7 @@ int parse(val stream, val name, parser_t *parser) parser->prepared_msg = nil; parser->syntax_tree = nil; yylex_init(&scanner); - parser->scanner = (scanner_t *) scanner; + parser->scanner = convert(scanner_t *, scanner); yyset_extra(parser, parser->scanner); @@ -74,8 +74,8 @@ static struct cobj_ops random_state_ops = { static val make_state(void) { - struct rand_state *r = (struct rand_state *) chk_malloc(sizeof *r); - return cobj((mem_t *) r, random_state_s, &random_state_ops); + struct rand_state *r = coerce(struct rand_state *, chk_malloc(sizeof *r)); + return cobj(coerce(mem_t *, r), random_state_s, &random_state_ops); } val random_state_p(val obj) @@ -108,7 +108,8 @@ val make_random_state(val seed) { val rs = make_state(); int i; - struct rand_state *r = (struct rand_state *) cobj_handle(rs, random_state_s); + struct rand_state *r = coerce(struct rand_state *, + cobj_handle(rs, random_state_s)); r->cur = 0; @@ -147,13 +148,13 @@ val make_random_state(val seed) #endif } else if (nilp(seed)) { val time = time_sec_usec(); - r->state[0] = (rand32_t) c_num(car(time)); - r->state[1] = (rand32_t) c_num(cdr(time)); - r->state[2] = (rand32_t) getpid(); + r->state[0] = convert(rand32_t, c_num(car(time))); + r->state[1] = convert(rand32_t, c_num(cdr(time))); + r->state[2] = convert(rand32_t, getpid()); memset(r->state + 3, 0xAA, sizeof r->state - 3 * sizeof r->state[0]); } else if (random_state_p(seed)) { - struct rand_state *rseed = (struct rand_state *) - cobj_handle(seed, random_state_s); + struct rand_state *rseed = coerce(struct rand_state *, + cobj_handle(seed, random_state_s)); *r = *rseed; } else { uw_throwf(error_s, lit("make-random-state: seed ~s is not a number"), @@ -168,23 +169,23 @@ val make_random_state(val seed) val random_fixnum(val state) { - struct rand_state *r = (struct rand_state *) cobj_handle(default_arg(state, - random_state), - random_state_s); + struct rand_state *r = coerce(struct rand_state *, + cobj_handle(default_arg(state, random_state), + random_state_s)); return num(rand32(r) & NUM_MAX); } val random(val state, val modulus) { - struct rand_state *r = (struct rand_state *) cobj_handle(state, - random_state_s); + struct rand_state *r = coerce(struct rand_state *, + cobj_handle(state, random_state_s)); if (bignump(modulus)) { mp_int *m = mp(modulus); int bits = mp_count_bits(m); int rands_needed = (bits + 32 - 1) / 32; int msb_rand_bits = bits % 32; - rand32_t msb_rand_mask = ((rand32_t) -1) >> (32 - msb_rand_bits); + rand32_t msb_rand_mask = convert(rand32_t, -1) >> (32 - msb_rand_bits); val out = make_bignum(); mp_int *om = mp(out); @@ -223,7 +224,7 @@ val random(val state, val modulus) int rands_needed = (bits + 32 - 1) / 32; #endif int msb_rand_bits = bits % 32; - rand32_t msb_rand_mask = ((rand32_t) -1) >> (32 - msb_rand_bits); + rand32_t msb_rand_mask = convert(rand32_t, -1) >> (32 - msb_rand_bits); if (m <= 0) goto invalid; for (;;) { @@ -114,15 +114,15 @@ typedef unsigned int bitcell_t; #endif #ifdef FULL_UNICODE -#define CHAR_SET_L2_LO(CH) ((CH) & (~(wchar_t) 0xFFFF)) -#define CHAR_SET_L2_HI(CH) ((CH) | ((wchar_t) 0xFFFF)) +#define CHAR_SET_L2_LO(CH) ((CH) & ~convert(wchar_t, 0xFFFF)) +#define CHAR_SET_L2_HI(CH) ((CH) | convert(wchar_t, 0xFFFF)) #endif -#define CHAR_SET_L1_LO(CH) ((CH) & (~(wchar_t) 0xFFF)) -#define CHAR_SET_L1_HI(CH) ((CH) | ((wchar_t) 0xFFF)) +#define CHAR_SET_L1_LO(CH) ((CH) & ~convert(wchar_t, 0xFFF)) +#define CHAR_SET_L1_HI(CH) ((CH) | convert(wchar_t, 0xFFF)) -#define CHAR_SET_L0_LO(CH) ((CH) & (~(wchar_t) 0xFF)) -#define CHAR_SET_L0_HI(CH) ((CH) | ((wchar_t) 0xFF)) +#define CHAR_SET_L0_LO(CH) ((CH) & ~convert(wchar_t, 0xFF)) +#define CHAR_SET_L0_HI(CH) ((CH) | convert(wchar_t, 0xFF)) typedef enum { CHSET_SMALL, CHSET_DISPLACED, CHSET_LARGE, @@ -260,8 +260,8 @@ static int L0_full(cset_L0_t *L0) { int i; - for (i = 0; i < (int) CHAR_SET_SIZE; i++) - if ((*L0)[i] != ((bitcell_t) -1)) + for (i = 0; i < convert(int, CHAR_SET_SIZE); i++) + if ((*L0)[i] != convert(bitcell_t, -1)) return 0; return 1; } @@ -271,12 +271,12 @@ static void L0_fill_range(cset_L0_t *L0, wchar_t ch0, wchar_t ch1) int i; int bt0 = CHAR_SET_BIT(ch0); int bc0 = CHAR_SET_INDEX(ch0); - bitcell_t mask0 = ~(((bitcell_t) 1 << bt0) - 1); + bitcell_t mask0 = ~((convert(bitcell_t, 1) << bt0) - 1); int bt1 = CHAR_SET_BIT(ch1); int bc1 = CHAR_SET_INDEX(ch1); bitcell_t mask1 = (bt1 == (BITCELL_BIT - 1)) - ? (bitcell_t) -1 - : (((bitcell_t) 1 << (bt1 + 1)) - 1); + ? convert(bitcell_t, -1) + : (convert(bitcell_t, 1) << (bt1 + 1)) - 1; if (bc1 == bc0) { (*L0)[bc0] |= (mask0 & mask1); @@ -284,7 +284,7 @@ static void L0_fill_range(cset_L0_t *L0, wchar_t ch0, wchar_t ch1) (*L0)[bc0] |= mask0; (*L0)[bc1] |= mask1; for (i = bc0 + 1; i < bc1; i++) - (*L0)[i] = ((bitcell_t) -1); + (*L0)[i] = convert(bitcell_t, -1); } } @@ -297,7 +297,7 @@ static int L1_full(cset_L1_t *L1) { int i; for (i = 0; i < 16; i++) - if ((*L1)[i] != (cset_L0_t *) -1) + if ((*L1)[i] != coerce(cset_L0_t *, -1)) return 0; return 1; } @@ -315,7 +315,7 @@ static void L1_fill_range(cset_L1_t *L1, wchar_t ch0, wchar_t ch1) if (i1 > i10 && i1 < i11) { free((*L1)[i1]); - (*L1)[i1] = (cset_L0_t *) -1; + (*L1)[i1] = coerce(cset_L0_t *, -1); continue; } else if (i10 == i11) { c0 = ch0; @@ -328,12 +328,12 @@ static void L1_fill_range(cset_L1_t *L1, wchar_t ch0, wchar_t ch1) c1 = ch1; } - if ((L0 = (*L1)[i1]) == (cset_L0_t *) -1) + if ((L0 = (*L1)[i1]) == coerce(cset_L0_t *, -1)) continue; if (L0 == 0) { static cset_L0_t blank; - L0 = (*L1)[i1] = (cset_L0_t *) chk_malloc(sizeof *L0); + L0 = (*L1)[i1] = coerce(cset_L0_t *, chk_malloc(sizeof *L0)); memcpy(L0, &blank, sizeof *L0); } @@ -341,7 +341,7 @@ static void L1_fill_range(cset_L1_t *L1, wchar_t ch0, wchar_t ch1) if (L0_full(L0)) { free(L0); - (*L1)[i1] = (cset_L0_t *) -1; + (*L1)[i1] = coerce(cset_L0_t *, -1); } } } @@ -353,7 +353,7 @@ static int L1_contains(cset_L1_t *L1, wchar_t ch) if (L0 == 0) return 0; - else if (L0 == (cset_L0_t *) -1) + else if (L0 == coerce(cset_L0_t *, -1)) return 1; else return L0_contains(L0, CHAR_SET_L0(ch)); @@ -364,11 +364,11 @@ static void L1_free(cset_L1_t *L1) { int i1; - if (L1 == (cset_L1_t *) -1) + if (L1 == coerce(cset_L1_t *, -1)) return; for (i1 = 0; i1 < 16; i1++) - if ((*L1)[i1] != (cset_L0_t *) -1) + if ((*L1)[i1] != coerce(cset_L0_t *, -1)) free((*L1)[i1]); } @@ -377,7 +377,7 @@ static int L2_full(cset_L2_t *L2) { int i; for (i = 0; i < 16; i++) - if ((*L2)[i] != (cset_L1_t *) -1) + if ((*L2)[i] != coerce(cset_L1_t *, -1)) return 0; return 1; } @@ -396,7 +396,7 @@ static void L2_fill_range(cset_L2_t *L2, wchar_t ch0, wchar_t ch1) if (i2 > i20 && i2 < i21) { free((*L2)[i2]); - (*L2)[i2] = (cset_L1_t *) -1; + (*L2)[i2] = coerce(cset_L1_t *, -1); continue; } else if (i20 == i21) { c0 = ch0; @@ -409,12 +409,12 @@ static void L2_fill_range(cset_L2_t *L2, wchar_t ch0, wchar_t ch1) c1 = ch1; } - if ((L1 = (*L2)[i2]) == (cset_L1_t *) -1) + if ((L1 = (*L2)[i2]) == coerce(cset_L1_t *, -1)) continue; if (L1 == 0) { static cset_L1_t blank; - L1 = (*L2)[i2] = (cset_L1_t *) chk_malloc(sizeof *L1); + L1 = (*L2)[i2] = coerce(cset_L1_t *, chk_malloc(sizeof *L1)); memcpy(L1, &blank, sizeof *L1); } @@ -422,7 +422,7 @@ static void L2_fill_range(cset_L2_t *L2, wchar_t ch0, wchar_t ch1) if (L1_full(L1)) { free(L1); - (*L2)[i2] = (cset_L1_t *) -1; + (*L2)[i2] = coerce(cset_L1_t *, -1); } } } @@ -434,7 +434,7 @@ static int L2_contains(cset_L2_t *L2, wchar_t ch) if (L1 == 0) return 0; - else if (L1 == (cset_L1_t *) -1) + else if (L1 == coerce(cset_L1_t *, -1)) return 1; else return L1_contains(L1, ch); @@ -446,7 +446,7 @@ static void L2_free(cset_L2_t *L2) for (i2 = 0; i2 < 16; i2++) { cset_L1_t *L1 = (*L2)[i2]; - if (L1 != 0 && L1 != (cset_L1_t *) -1) { + if (L1 != 0 && L1 != coerce(cset_L1_t *, -1)) { L1_free((*L2)[i2]); free((*L2)[i2]); } @@ -468,7 +468,7 @@ static void L3_fill_range(cset_L3_t *L3, wchar_t ch0, wchar_t ch1) if (i3 > i30 && i3 < i31) { free((*L3)[i3]); - (*L3)[i3] = (cset_L2_t *) -1; + (*L3)[i3] = coerce(cset_L2_t *, -1); continue; } else if (i30 == i31) { c0 = ch0; @@ -481,19 +481,19 @@ static void L3_fill_range(cset_L3_t *L3, wchar_t ch0, wchar_t ch1) c1 = ch1; } - if ((L2 = (*L3)[i3]) == (cset_L2_t *) -1) + if ((L2 = (*L3)[i3]) == coerce(cset_L2_t *, -1)) continue; if (L2 == 0) { static cset_L2_t blank; - L2 = (*L3)[i3] = (cset_L2_t *) chk_malloc(sizeof *L2); + L2 = (*L3)[i3] = coerce(cset_L2_t *, chk_malloc(sizeof *L2)); memcpy(L2, &blank, sizeof *L2); } L2_fill_range(L2, c0, c1); if (L2_full(L2)) { free(L2); - (*L3)[i3] = (cset_L2_t *) -1; + (*L3)[i3] = coerce(cset_L2_t *, -1); } } } @@ -506,7 +506,7 @@ static int L3_contains(cset_L3_t *L3, wchar_t ch) if (L2 == 0) return 0; - else if (L2 == (cset_L2_t *) -1) + else if (L2 == coerce(cset_L2_t *, -1)) return 1; else return L2_contains(L2, ch); @@ -518,7 +518,7 @@ static void L3_free(cset_L3_t *L3) for (i3 = 0; i3 < 17; i3++) { cset_L2_t *L2 = (*L3)[i3]; - if (L2 != 0 && L2 != (cset_L2_t *) -1) { + if (L2 != 0 && L2 != coerce(cset_L2_t *, -1)) { L2_free((*L3)[i3]); free((*L3)[i3]); } @@ -530,7 +530,7 @@ static void L3_free(cset_L3_t *L3) static char_set_t *char_set_create(chset_type_t type, wchar_t base, unsigned st) { static char_set_t blank; - char_set_t *cs = (char_set_t *) chk_malloc(sizeof *cs); + char_set_t *cs = coerce(char_set_t *, chk_malloc(sizeof *cs)); *cs = blank; cs->any.type = type; cs->any.stat = st; @@ -792,7 +792,7 @@ static void init_special_char_sets(void) static void char_set_cobj_destroy(val chset) { - char_set_t *set = (char_set_t *) chset->co.handle; + char_set_t *set = coerce(char_set_t *, chset->co.handle); char_set_destroy(set); chset->co.handle = 0; } @@ -807,7 +807,7 @@ static struct cobj_ops char_set_obj_ops = { static nfa_state_t *nfa_state_accept(void) { - nfa_state_t *st = (nfa_state_t *) chk_malloc(sizeof *st); + nfa_state_t *st = coerce(nfa_state_t *, chk_malloc(sizeof *st)); st->a.kind = nfa_accept; st->a.visited = 0; return st; @@ -815,7 +815,7 @@ static nfa_state_t *nfa_state_accept(void) static nfa_state_t *nfa_state_empty(nfa_state_t *t0, nfa_state_t *t1) { - nfa_state_t *st = (nfa_state_t *) chk_malloc(sizeof *st); + nfa_state_t *st = coerce(nfa_state_t *, chk_malloc(sizeof *st)); st->e.kind = nfa_empty; st->e.visited = 0; st->e.trans0 = t0; @@ -825,7 +825,7 @@ static nfa_state_t *nfa_state_empty(nfa_state_t *t0, nfa_state_t *t1) static nfa_state_t *nfa_state_single(nfa_state_t *t, wchar_t ch) { - nfa_state_t *st = (nfa_state_t *) chk_malloc(sizeof *st); + nfa_state_t *st = coerce(nfa_state_t *, chk_malloc(sizeof *st)); st->o.kind = nfa_single; st->o.visited = 0; st->o.trans = t; @@ -835,7 +835,7 @@ static nfa_state_t *nfa_state_single(nfa_state_t *t, wchar_t ch) static nfa_state_t *nfa_state_wild(nfa_state_t *t) { - nfa_state_t *st = (nfa_state_t *) chk_malloc(sizeof *st); + nfa_state_t *st = coerce(nfa_state_t *, chk_malloc(sizeof *st)); st->o.kind = nfa_wild; st->o.visited = 0; st->o.trans = t; @@ -857,7 +857,7 @@ static void nfa_state_shallow_free(nfa_state_t *st) static nfa_state_t *nfa_state_set(nfa_state_t *t, char_set_t *cs) { - nfa_state_t *st = (nfa_state_t *) chk_malloc(sizeof *st); + nfa_state_t *st = coerce(nfa_state_t *, chk_malloc(sizeof *st)); st->s.kind = nfa_set; st->s.visited = 0; st->s.trans = t; @@ -1100,7 +1100,7 @@ static int nfa_all_states(nfa_state_t **inout, int num, unsigned visited) static void nfa_free(nfa_t nfa) { - nfa_state_t **all = (nfa_state_t **) chk_malloc(NFA_SET_SIZE * sizeof *all); + nfa_state_t **all = coerce(nfa_state_t **, chk_malloc(NFA_SET_SIZE * sizeof *all)); int nstates, i; all[0] = nfa.start; @@ -1250,9 +1250,9 @@ static cnum nfa_run(nfa_t nfa, const wchar_t *str) { const wchar_t *last_accept_pos = 0, *ptr = str; unsigned visited = nfa.start->a.visited + 1; - nfa_state_t **move = (nfa_state_t **) chk_malloc(NFA_SET_SIZE * sizeof *move); - nfa_state_t **clos = (nfa_state_t **) chk_malloc(NFA_SET_SIZE * sizeof *clos); - nfa_state_t **stack = (nfa_state_t **) chk_malloc(NFA_SET_SIZE * sizeof *stack); + nfa_state_t **move = coerce(nfa_state_t **, chk_malloc(NFA_SET_SIZE * sizeof *move)); + nfa_state_t **clos = coerce(nfa_state_t **, chk_malloc(NFA_SET_SIZE * sizeof *clos)); + nfa_state_t **stack = coerce(nfa_state_t **, chk_malloc(NFA_SET_SIZE * sizeof *stack)); int nmove = 1, nclos; int accept = 0; @@ -1294,7 +1294,7 @@ static cnum regex_machine_match_span(regex_machine_t *regm) static void regex_destroy(val obj) { - regex_t *regex = (regex_t *) obj->co.handle; + regex_t *regex = coerce(regex_t *, obj->co.handle); if (regex->kind == REGEX_NFA) nfa_free(regex->r.nfa); free(regex); @@ -1303,7 +1303,7 @@ static void regex_destroy(val obj) static void regex_mark(val obj) { - regex_t *regex = (regex_t *) obj->co.handle; + regex_t *regex = coerce(regex_t *, obj->co.handle); if (regex->kind == REGEX_DV) gc_mark(regex->r.dv); gc_mark(regex->source); @@ -1330,17 +1330,17 @@ static val reg_nullable(val); static val dv_compile_regex(val exp) { if (exp == space_k) { - return cobj((mem_t *) space_cs, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, space_cs), chset_s, &char_set_obj_ops); } else if (exp == digit_k) { - return cobj((mem_t *) digit_cs, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, digit_cs), chset_s, &char_set_obj_ops); } else if (exp == word_char_k) { - return cobj((mem_t *) word_cs, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, word_cs), chset_s, &char_set_obj_ops); } else if (exp == cspace_k) { - return cobj((mem_t *) cspace_cs, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, cspace_cs), chset_s, &char_set_obj_ops); } else if (exp == cdigit_k) { - return cobj((mem_t *) cdigit_cs, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, cdigit_cs), chset_s, &char_set_obj_ops); } else if (exp == cword_char_k) { - return cobj((mem_t *) cword_cs, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, cword_cs), chset_s, &char_set_obj_ops); } else if (symbolp(exp) || chrp(exp)) { return exp; } else if (stringp(exp)) { @@ -1351,7 +1351,7 @@ static val dv_compile_regex(val exp) if (sym == set_s || sym == cset_s) { char_set_t *set = char_set_compile(args, eq(sym, cset_s)); - return cobj((mem_t *) set, chset_s, &char_set_obj_ops); + return cobj(coerce(mem_t *, set), chset_s, &char_set_obj_ops); } else if (sym == compound_s) { list_collect_decl (out, iter); iter = list_collect(iter, compound_s); @@ -1551,7 +1551,7 @@ static val reg_derivative(val exp, val ch) } else if (chrp(exp)) { return null(eq(exp, ch)); } else if (typeof(exp) == chset_s) { - char_set_t *set = (char_set_t *) exp->co.handle; + char_set_t *set = coerce(char_set_t *, exp->co.handle); return if3(char_set_contains(set, c_chr(ch)), nil, t); } else if (exp == wild_s) { return nil; @@ -1670,21 +1670,21 @@ val regex_compile(val regex_sexp, val error_stream) regex_sexp = regex_parse(regex_sexp, default_bool_arg(error_stream)); return if2(regex_sexp, regex_compile(regex_sexp, error_stream)); } else if (opt_derivative_regex || regex_requires_dv(regex_sexp)) { - regex_t *regex = (regex_t *) chk_malloc(sizeof *regex); + regex_t *regex = coerce(regex_t *, chk_malloc(sizeof *regex)); val ret; regex->kind = REGEX_DV; regex->r.dv = nil; regex->source = nil; - ret = cobj((mem_t *) regex, regex_s, ®ex_obj_ops); + ret = cobj(coerce(mem_t *, regex), regex_s, ®ex_obj_ops); regex->r.dv = dv_compile_regex(regex_sexp); regex->source = regex_sexp; return ret; } else { - regex_t *regex = (regex_t *) chk_malloc(sizeof *regex); + regex_t *regex = coerce(regex_t *, chk_malloc(sizeof *regex)); val ret; regex->kind = REGEX_NFA; regex->source = nil; - ret = cobj((mem_t *) regex, regex_s, ®ex_obj_ops); + ret = cobj(coerce(mem_t *, regex), regex_s, ®ex_obj_ops); regex->r.nfa = nfa_compile_regex(regex_sexp); regex->source = regex_sexp; return ret; @@ -1836,7 +1836,7 @@ static void print_rec(val exp, val stream) static void regex_print(val obj, val stream) { - regex_t *regex = (regex_t *) cobj_handle(obj, regex_s); + regex_t *regex = coerce(regex_t *, cobj_handle(obj, regex_s)); put_string(lit("#/"), stream); print_rec(regex->source, stream); @@ -1845,7 +1845,7 @@ static void regex_print(val obj, val stream) static cnum regex_run(val compiled_regex, const wchar_t *str) { - regex_t *regex = (regex_t *) cobj_handle(compiled_regex, regex_s); + regex_t *regex = coerce(regex_t *, cobj_handle(compiled_regex, regex_s)); return if3(regex->kind == REGEX_DV, dv_run(regex->r.dv, str), @@ -1883,7 +1883,7 @@ static void regex_machine_reset(regex_machine_t *regm) static void regex_machine_init(regex_machine_t *regm, val reg) { - regex_t *regex = (regex_t *) cobj_handle(reg, regex_s); + regex_t *regex = coerce(regex_t *, cobj_handle(reg, regex_s)); if (regex->kind == REGEX_DV) { regm->n.is_nfa = 0; @@ -1891,12 +1891,12 @@ static void regex_machine_init(regex_machine_t *regm, val reg) } else { regm->n.is_nfa = 1; regm->n.nfa = regex->r.nfa; - regm->n.move = (nfa_state_t **) - chk_malloc(NFA_SET_SIZE * sizeof *regm->n.move); - regm->n.clos = (nfa_state_t **) - chk_malloc(NFA_SET_SIZE * sizeof *regm->n.clos); - regm->n.stack = (nfa_state_t **) - chk_malloc(NFA_SET_SIZE * sizeof *regm->n.stack); + regm->n.move = coerce(nfa_state_t **, + chk_malloc(NFA_SET_SIZE * sizeof *regm->n.move)); + regm->n.clos = coerce(nfa_state_t **, + chk_malloc(NFA_SET_SIZE * sizeof *regm->n.clos)); + regm->n.stack = coerce(nfa_state_t **, + chk_malloc(NFA_SET_SIZE * sizeof *regm->n.stack)); } regex_machine_reset(regm); @@ -326,12 +326,12 @@ int sig_mask(int how, const sigset_t *set, sigset_t *oldset) case SIG_BLOCK: pnew = &newset; newset = sig_blocked_cache; - mem_set_bits((mem_t *) &newset, (mem_t *) set, sizeof newset); + mem_set_bits(coerce(mem_t *, &newset), coerce(const mem_t *, set), sizeof newset); break; case SIG_UNBLOCK: pnew = &newset; newset = sig_blocked_cache; - mem_clr_bits((mem_t *) &newset, (mem_t *) set, sizeof newset); + mem_clr_bits(coerce(mem_t *, &newset), coerce(const mem_t *, set), sizeof newset); break; default: errno = EINVAL; @@ -106,7 +106,7 @@ static struct strm_ops null_ops = { val make_null_stream(void) { - return cobj((mem_t *) 0, stream_s, &null_ops.cobj_ops); + return cobj(convert(mem_t *, 0), stream_s, &null_ops.cobj_ops); } struct stdio_handle { @@ -126,7 +126,7 @@ struct stdio_handle { static void stdio_stream_print(val stream, val out) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->pid) format(out, lit("#<~s ~s>"), stream->co.cls, h->descr, nao); else @@ -135,21 +135,21 @@ static void stdio_stream_print(val stream, val out) static void stdio_stream_destroy(val stream) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); common_destroy(stream); free(h); } static void stdio_stream_mark(val stream) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); gc_mark(h->descr); gc_mark(h->mode); } static val stdio_maybe_read_error(val stream) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f == 0) uw_throwf(file_error_s, lit("error reading ~a: file closed"), stream, nao); if (ferror(h->f)) { @@ -162,7 +162,7 @@ static val stdio_maybe_read_error(val stream) static val stdio_maybe_error(val stream, val action) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f == 0) uw_throwf(file_error_s, lit("error ~a ~a: file closed"), stream, action, nao); clearerr(h->f); @@ -199,24 +199,24 @@ static int se_fflush(FILE *f) static int stdio_put_char_callback(int ch, mem_t *f) { - int ret = se_putc(ch, (FILE *) f) != EOF; + int ret = se_putc(ch, coerce(FILE *, f)) != EOF; return ret; } static int stdio_get_char_callback(mem_t *f) { - return se_getc((FILE *) f); + return se_getc(coerce(FILE *, f)); } static val stdio_put_string(val stream, val str) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f != 0) { const wchar_t *s = c_str(str); while (*s) { - if (!utf8_encode(*s++, stdio_put_char_callback, (mem_t *) h->f)) + if (!utf8_encode(*s++, stdio_put_char_callback, coerce(mem_t *, h->f))) return stdio_maybe_error(stream, lit("writing")); } return t; @@ -226,30 +226,31 @@ static val stdio_put_string(val stream, val str) static val stdio_put_char(val stream, val ch) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); - return h->f != 0 && utf8_encode(c_chr(ch), stdio_put_char_callback, (mem_t *) h->f) + return h->f != 0 && utf8_encode(c_chr(ch), stdio_put_char_callback, + coerce(mem_t *, h->f)) ? t : stdio_maybe_error(stream, lit("writing")); } static val stdio_put_byte(val stream, int b) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); - return h->f != 0 && se_putc(b, (FILE *) h->f) != EOF + return h->f != 0 && se_putc(b, coerce(FILE *, h->f)) != EOF ? t : stdio_maybe_error(stream, lit("writing")); } static val stdio_flush(val stream) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); return (h->f != 0 && se_fflush(h->f) == 0) ? t : stdio_maybe_error(stream, lit("flushing")); } static val stdio_seek(val stream, cnum offset, enum strm_whence whence) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f != 0) { if (offset == 0 && whence == strm_cur) { @@ -270,7 +271,7 @@ static val stdio_seek(val stream, cnum offset, enum strm_whence whence) static val stdio_get_prop(val stream, val ind) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (ind == real_time_k) { return h->is_real_time ? t : nil; @@ -283,7 +284,7 @@ static val stdio_get_prop(val stream, val ind) static val stdio_set_prop(val stream, val ind, val prop) { if (ind == real_time_k) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); h->is_real_time = prop ? 1 : 0; return t; } @@ -299,12 +300,12 @@ static wchar_t *snarf_line(struct stdio_handle *h) for (;;) { wint_t ch; - + if (h->unget_c) { ch = c_chr(h->unget_c); h->unget_c = nil; } else { - ch = utf8_decode(&h->ud, stdio_get_char_callback, (mem_t *) h->f); + ch = utf8_decode(&h->ud, stdio_get_char_callback, coerce(mem_t *, h->f)); } if (ch == WEOF && buf == 0) @@ -312,7 +313,8 @@ static wchar_t *snarf_line(struct stdio_handle *h) if (fill >= size) { size_t newsize = size ? size * 2 : min_size; - buf = (wchar_t *) chk_realloc((mem_t *) buf, newsize * sizeof *buf); + buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, buf), + newsize * sizeof *buf)); size = newsize; } @@ -324,7 +326,8 @@ static wchar_t *snarf_line(struct stdio_handle *h) } if (buf) - buf = (wchar_t *) chk_realloc((mem_t *) buf, fill * sizeof *buf); + buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, buf), + fill * sizeof *buf)); return buf; } @@ -334,7 +337,7 @@ static val stdio_get_line(val stream) if (stream->co.handle == 0) { return stdio_maybe_read_error(stream); } else { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); wchar_t *line = snarf_line(h); if (!line) return stdio_maybe_read_error(stream); @@ -344,14 +347,15 @@ static val stdio_get_line(val stream) static val stdio_get_char(val stream) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); val uc = h->unget_c; if (uc) { h->unget_c = nil; return uc; } if (h->f) { - wint_t ch = utf8_decode(&h->ud, stdio_get_char_callback, (mem_t *) h->f); + wint_t ch = utf8_decode(&h->ud, stdio_get_char_callback, + coerce(mem_t *, h->f)); return (ch != WEOF) ? chr(ch) : stdio_maybe_read_error(stream); } return stdio_maybe_read_error(stream); @@ -359,7 +363,7 @@ static val stdio_get_char(val stream) static val stdio_get_byte(val stream) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f) { int ch = se_getc(h->f); return (ch != EOF) ? num(ch) : stdio_maybe_read_error(stream); @@ -369,7 +373,7 @@ static val stdio_get_byte(val stream) static val stdio_unget_char(val stream, val ch) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (!is_chr(ch)) type_mismatch(lit("unget-char: ~s is not a character"), ch, nao); @@ -383,16 +387,16 @@ static val stdio_unget_char(val stream, val ch) static val stdio_unget_byte(val stream, int byte) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); - return h->f != 0 && ungetc(byte, (FILE *) h->f) != EOF + return h->f != 0 && ungetc(byte, coerce(FILE *, h->f)) != EOF ? num_fast(byte) : stdio_maybe_error(stream, lit("pushing back byte into")); } static val stdio_close(val stream, val throw_on_error) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f != 0 && h->f != stdin && h->f != stdout) { int result = fclose(h->f); @@ -452,7 +456,7 @@ int sleep(int sec) static void tail_strategy(val stream, unsigned long *state) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); int sec = 0, mod = 0; tail_calc(state, &sec, &mod); @@ -632,7 +636,7 @@ static int se_pclose(FILE *f) static val pipe_close(val stream, val throw_on_error) { - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); if (h->f != 0) { #if HAVE_FORK_STUFF @@ -700,7 +704,7 @@ static struct strm_ops pipe_ops = { static void string_in_stream_mark(val stream) { - val stuff = (val) stream->co.handle; + val stuff = coerce(val, stream->co.handle); gc_mark(stuff); } @@ -721,7 +725,7 @@ static val find_char(val string, val start, val ch) static val string_in_get_line(val stream) { - val pair = (val) stream->co.handle; + val pair = coerce(val, stream->co.handle); val string = car(pair); val pos = cdr(pair); @@ -737,7 +741,7 @@ static val string_in_get_line(val stream) static val string_in_get_char(val stream) { - val pair = (val) stream->co.handle; + val pair = coerce(val, stream->co.handle); val string = car(pair); val pos = cdr(pair); @@ -751,7 +755,7 @@ static val string_in_get_char(val stream) static val string_in_unget_char(val stream, val ch) { - val pair = (val) stream->co.handle; + val pair = coerce(val, stream->co.handle); val string = car(pair); val pos = cdr(pair); @@ -773,7 +777,7 @@ static val string_in_unget_char(val stream, val ch) static val string_in_get_prop(val stream, val ind) { if (ind == name_k) { - val pair = (val) stream->co.handle; + val pair = coerce(val, stream->co.handle); return format(nil, lit("string-stream (~s)"), car(pair), nao); } return nil; @@ -808,7 +812,7 @@ struct byte_input { static void byte_in_stream_destroy(val stream) { - struct byte_input *bi = (struct byte_input *) stream->co.handle; + struct byte_input *bi = coerce(struct byte_input *, stream->co.handle); if (bi) { free(bi->buf); @@ -820,7 +824,7 @@ static void byte_in_stream_destroy(val stream) static val byte_in_get_byte(val stream) { - struct byte_input *bi = (struct byte_input *) stream->co.handle; + struct byte_input *bi = coerce(struct byte_input *, stream->co.handle); if (bi->index < bi->size) return num(bi->buf[bi->index++]); @@ -829,7 +833,7 @@ static val byte_in_get_byte(val stream) static val byte_in_unget_byte(val stream, int byte) { - struct byte_input *bi = (struct byte_input *) stream->co.handle; + struct byte_input *bi = coerce(struct byte_input *, stream->co.handle); if (bi->index == 0) uw_throwf(file_error_s, @@ -873,7 +877,7 @@ struct string_output { static void string_out_stream_destroy(val stream) { - struct string_output *so = (struct string_output *) stream->co.handle; + struct string_output *so = coerce(struct string_output *, stream->co.handle); if (so) { free(so->buf); @@ -885,7 +889,7 @@ static void string_out_stream_destroy(val stream) static int string_out_byte_callback(mem_t *ctx) { - struct string_output *so = (struct string_output *) ctx; + struct string_output *so = coerce(struct string_output *, ctx); if (so->tail >= so->head) return EOF; return so->byte_buf[so->tail++]; @@ -898,7 +902,8 @@ static val string_out_byte_flush(struct string_output *so, val stream) val result = nil; if (so->tail < so->head) { - wint_t ch = utf8_decode(&so->ud, string_out_byte_callback, (mem_t *) so); + wint_t ch = utf8_decode(&so->ud, string_out_byte_callback, + coerce(mem_t *, so)); int remaining = so->head - so->tail; if (remaining != 0) memmove(so->byte_buf, so->byte_buf + so->tail, remaining); @@ -914,7 +919,7 @@ static val string_out_byte_flush(struct string_output *so, val stream) static val string_out_put_string(val stream, val str) { - struct string_output *so = (struct string_output *) stream->co.handle; + struct string_output *so = coerce(struct string_output *, stream->co.handle); if (so == 0) return nil; @@ -938,8 +943,8 @@ static val string_out_put_string(val stream, val str) } if (so->size != old_size) - so->buf = (wchar_t *) chk_realloc((mem_t *) so->buf, - so->size * sizeof *so->buf); + so->buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, so->buf), + so->size * sizeof *so->buf)); wmemcpy(so->buf + so->fill, s, len + 1); so->fill += len; return t; @@ -950,19 +955,20 @@ static val string_out_put_char(val stream, val ch) { wchar_t onech[] = wini(" "); wref(onech)[0] = c_chr(ch); - return string_out_put_string(stream, auto_str((const wchli_t *) wref(onech))); + return string_out_put_string(stream, + auto_str(coerce(const wchli_t *, wref(onech)))); } static val string_out_put_byte(val stream, int ch) { - struct string_output *so = (struct string_output *) stream->co.handle; + struct string_output *so = coerce(struct string_output *, stream->co.handle); if (so == 0) return nil; so->byte_buf[so->head++] = ch; - if (so->head >= (int) sizeof so->byte_buf) + if (so->head >= convert(int, sizeof so->byte_buf)) return string_out_byte_flush(so, stream); return t; @@ -991,13 +997,13 @@ static struct strm_ops string_out_ops = { static void strlist_mark(val stream) { - val stuff = (val) stream->co.handle; + val stuff = coerce(val, stream->co.handle); gc_mark(stuff); } static val strlist_out_put_string(val stream, val str) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); cons_bind (lines, strstream, cell); for (;;) { @@ -1025,7 +1031,7 @@ static val strlist_out_put_string(val stream, val str) static val strlist_out_put_char(val stream, val ch) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); cons_bind (lines, strstream, cell); if (ch == chr('\n')) { @@ -1064,7 +1070,7 @@ static struct strm_ops strlist_out_ops = { val make_strlist_output_stream(void) { - return cobj((mem_t *) cons(nil, make_string_output_stream()), + return cobj(coerce(mem_t *, cons(nil, make_string_output_stream())), stream_s, &strlist_out_ops.cobj_ops); } @@ -1075,7 +1081,7 @@ val get_list_from_stream(val stream) (lit("~a is not a stream"), stream, nao)); if (stream->co.ops == &strlist_out_ops.cobj_ops) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); cons_bind (lines, strstream, cell); val stray = get_string_from_stream(strstream); if (!zerop(length_str(stray))) @@ -1088,7 +1094,7 @@ val get_list_from_stream(val stream) static val dir_get_line(val stream) { - DIR *handle = (DIR *) stream->co.handle; + DIR *handle = coerce(DIR *, stream->co.handle); if (handle == 0) { return nil; @@ -1107,7 +1113,7 @@ static val dir_get_line(val stream) static val dir_close(val stream, val throw_on_error) { if (stream->co.handle != 0) { - closedir((DIR *) stream->co.handle); + closedir(coerce(DIR *, stream->co.handle)); stream->co.handle = 0; return t; } @@ -1138,8 +1144,8 @@ static struct strm_ops dir_ops = { static val make_stdio_stream_common(FILE *f, val descr, struct cobj_ops *ops) { - struct stdio_handle *h = (struct stdio_handle *) chk_malloc(sizeof *h); - val stream = cobj((mem_t *) h, stream_s, ops); + struct stdio_handle *h = coerce(struct stdio_handle *, chk_malloc(sizeof *h)); + val stream = cobj(coerce(mem_t *, h), stream_s, ops); h->f = f; h->descr = descr; h->unget_c = nil; @@ -1176,7 +1182,7 @@ val make_pipe_stream(FILE *f, val descr) static val make_pipevp_stream(FILE *f, val descr, pid_t pid) { val stream = make_stdio_stream_common(f, descr, &pipe_ops.cobj_ops); - struct stdio_handle *h = (struct stdio_handle *) stream->co.handle; + struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle); h->pid = pid; return stream; } @@ -1184,7 +1190,7 @@ static val make_pipevp_stream(FILE *f, val descr, pid_t pid) val make_string_input_stream(val string) { - return cobj((mem_t *) cons(string, zero), stream_s, &string_in_ops.cobj_ops); + return cobj(coerce(mem_t *, cons(string, zero)), stream_s, &string_in_ops.cobj_ops); } val make_string_byte_input_stream(val string) @@ -1192,25 +1198,25 @@ val make_string_byte_input_stream(val string) type_assert (stringp(string), (lit("~a is not a string"), string, nao)); { - struct byte_input *bi = (struct byte_input *) chk_malloc(sizeof *bi); + struct byte_input *bi = coerce(struct byte_input *, chk_malloc(sizeof *bi)); unsigned char *utf8 = utf8_dup_to_uc(c_str(string)); bi->buf = utf8; - bi->size = strlen((char *) utf8); + bi->size = strlen(coerce(char *, utf8)); bi->index = 0; - return cobj((mem_t *) bi, stream_s, &byte_in_ops.cobj_ops); + return cobj(coerce(mem_t *, bi), stream_s, &byte_in_ops.cobj_ops); } } val make_string_output_stream(void) { - struct string_output *so = (struct string_output *) chk_malloc(sizeof *so); + struct string_output *so = coerce(struct string_output *, chk_malloc(sizeof *so)); so->size = 128; - so->buf = (wchar_t *) chk_malloc(so->size * sizeof so->buf); + so->buf = coerce(wchar_t *, chk_malloc(so->size * sizeof so->buf)); so->fill = 0; so->buf[0] = 0; utf8_decoder_init(&so->ud); so->head = so->tail = 0; - return cobj((mem_t *) so, stream_s, &string_out_ops.cobj_ops); + return cobj(coerce(mem_t *, so), stream_s, &string_out_ops.cobj_ops); } val get_string_from_stream(val stream) @@ -1221,7 +1227,7 @@ val get_string_from_stream(val stream) stream, nao)); if (stream->co.ops == &string_out_ops.cobj_ops) { - struct string_output *so = (struct string_output *) stream->co.handle; + struct string_output *so = coerce(struct string_output *, stream->co.handle); val out = nil; if (!so) @@ -1232,8 +1238,8 @@ val get_string_from_stream(val stream) stream->co.handle = 0; - so->buf = (wchar_t *) chk_realloc((mem_t *) so->buf, - (so->fill + 1) * sizeof *so->buf); + so->buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, so->buf), + (so->fill + 1) * sizeof *so->buf)); out = string_own(so->buf); free(so); return out; @@ -1241,7 +1247,7 @@ val get_string_from_stream(val stream) type_assert (stream->co.ops == &string_in_ops.cobj_ops, (lit("~a is not a string stream"), stream, nao)); { - val pair = (val) stream->co.handle; + val pair = coerce(val, stream->co.handle); return pair ? car(pair) : nil; } } @@ -1249,7 +1255,7 @@ val get_string_from_stream(val stream) val make_dir_stream(DIR *dir) { - return cobj((mem_t *) dir, stream_s, &dir_ops.cobj_ops); + return cobj(coerce(mem_t *, dir), stream_s, &dir_ops.cobj_ops); } val streamp(val obj) @@ -1264,7 +1270,7 @@ val stream_set_prop(val stream, val ind, val prop) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->set_prop ? ops->set_prop(stream, ind, prop) : nil; } } @@ -1276,7 +1282,7 @@ val stream_get_prop(val stream, val ind) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->get_prop ? ops->get_prop(stream, ind) : nil; } } @@ -1284,7 +1290,7 @@ val stream_get_prop(val stream, val ind) val real_time_stream_p(val obj) { if (streamp(obj)) { - struct strm_ops *ops = (struct strm_ops *) obj->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, obj->co.ops); return ops->get_prop ? ops->get_prop(obj, real_time_k) : nil; } @@ -1298,7 +1304,7 @@ val close_stream(val stream, val throw_on_error) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->close ? ops->close(stream, throw_on_error) : nil; } } @@ -1312,7 +1318,7 @@ val get_line(val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->get_line ? ops->get_line(stream) : nil; } } @@ -1326,7 +1332,7 @@ val get_char(val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->get_char ? ops->get_char(stream) : nil; } } @@ -1340,7 +1346,7 @@ val get_byte(val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->get_byte ? ops->get_byte(stream) : nil; } } @@ -1354,7 +1360,7 @@ val unget_char(val ch, val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->unget_char ? ops->unget_char(stream, ch) : nil; } } @@ -1374,7 +1380,7 @@ val unget_byte(val byte, val stream) stream, byte, nao); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->unget_byte ? ops->unget_byte(stream, b) : nil; } } @@ -1635,10 +1641,10 @@ val vformat(val stream, val fmtstr, va_list vl) case 'x': case 'X': obj = va_arg(vl, val); if (bignump(obj)) { - int nchars = mp_radix_size(mp(obj), 16); - if (nchars >= (int) sizeof (num_buf)) - pnum = (char *) chk_malloc(nchars + 1); - mp_toradix_case(mp(obj), (unsigned char *) pnum, 16, 1); + int nchars = mp_radix_size(mp(obj), 16); + if (nchars >= convert(int, sizeof (num_buf))) + pnum = coerce(char *, chk_malloc(nchars + 1)); + mp_toradix_case(mp(obj), coerce(unsigned char *, pnum), 16, 1); } else { const char *fmt = ch == 'x' ? num_fmt->hex : num_fmt->HEX; value = c_num(obj); @@ -1654,9 +1660,9 @@ val vformat(val stream, val fmtstr, va_list vl) obj = va_arg(vl, val); if (bignump(obj)) { int nchars = mp_radix_size(mp(obj), 8); - if (nchars >= (int) sizeof (num_buf)) - pnum = (char *) chk_malloc(nchars + 1); - mp_toradix(mp(obj), (unsigned char *) pnum, 8); + if (nchars >= convert(int, sizeof (num_buf))) + pnum = coerce(char *, chk_malloc(nchars + 1)); + mp_toradix(mp(obj), coerce(unsigned char *, pnum), 8); } else { value = c_num(obj); sprintf(num_buf, num_fmt->oct, value); @@ -1679,7 +1685,7 @@ val vformat(val stream, val fmtstr, va_list vl) n = c_flo(obj); break; case NUM: - n = (double) c_num(obj); + n = convert(double, c_num(obj)); break; default: uw_throwf(error_s, lit("format: ~~~a conversion requires " @@ -1745,9 +1751,9 @@ val vformat(val stream, val fmtstr, va_list vl) case BGNUM: { int nchars = mp_radix_size(mp(obj), 10); - if (nchars >= (int) sizeof (num_buf)) - pnum = (char *) chk_malloc(nchars + 1); - mp_toradix(mp(obj), (unsigned char *) pnum, 10); + if (nchars >= convert(int, sizeof (num_buf))) + pnum = coerce(char *, chk_malloc(nchars + 1)); + mp_toradix(mp(obj), coerce(unsigned char *, pnum), 10); } goto output_num; case FLNUM: @@ -1811,7 +1817,7 @@ val vformat(val stream, val fmtstr, va_list vl) case 'p': { val ptr = va_arg(vl, val); - value = (cnum) ptr; + value = coerce(cnum, ptr); sprintf(num_buf, num_fmt->hex, value); } goto output_num; @@ -1933,7 +1939,7 @@ val put_string(val string, val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->put_string ? ops->put_string(stream, string) : nil; } } @@ -1947,7 +1953,7 @@ val put_char(val ch, val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->put_char ? ops->put_char(stream, ch) : nil; } } @@ -1967,7 +1973,7 @@ val put_byte(val byte, val stream) stream, byte, nao); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->put_byte ? ops->put_byte(stream, b) : nil; } } @@ -2002,7 +2008,7 @@ val flush_stream(val stream) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); return ops->flush ? ops->flush(stream) : t; } } @@ -2014,7 +2020,7 @@ val seek_stream(val stream, val offset, val whence) stream, nao)); { - struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); enum strm_whence w; cnum off = c_num(offset); @@ -2095,7 +2101,7 @@ val open_tail(val path, val mode_str, val seek_end_p) path, num(errno), string_utf8(strerror(errno)), nao); stream = make_tail_stream(f, path); - h = (struct stdio_handle *) stream->co.handle; + h = coerce(struct stdio_handle *, stream->co.handle); h->mode = mode; if (!f) tail_strategy(stream, &state); @@ -2131,7 +2137,7 @@ val open_process(val name, val mode_str, val args) name, num(errno), string_utf8(strerror(errno)), nao); } - argv = (char **) chk_malloc((nargs + 2) * sizeof *argv); + argv = coerce(char **, chk_malloc((nargs + 2) * sizeof *argv)); for (i = 0, iter = cons(name, args); iter; i++, iter = cdr(iter)) { val arg = car(iter); @@ -2289,7 +2295,7 @@ static val run(val name, val args) args = default_bool_arg(args); nargs = c_num(length(args)) + 1; - argv = (char **) chk_malloc((nargs + 2) * sizeof *argv); + argv = coerce(char **, chk_malloc((nargs + 2) * sizeof *argv)); for (i = 0, iter = cons(name, args); iter; i++, iter = cdr(iter)) { val arg = car(iter); @@ -2342,17 +2348,17 @@ static val run(val command, val args) prot1(&args); - wargv = (const wchar_t **) chk_malloc((nargs + 2) * sizeof *wargv); + wargv = coerce(const wchar_t **, chk_malloc((nargs + 2) * sizeof *wargv)); for (i = 0, iter = cons(command, args); iter; i++, iter = cdr(iter)) wargv[i] = c_str(car(iter)); wargv[i] = 0; status = _wspawnvp(_P_WAIT, c_str(command), wargv); - + for (i = 0; i < nargs; i++) - free((wchar_t *) wargv[i]); - free((wchar_t **) wargv); + free(strip_qual(wchar_t *, wargv[i])); + free(strip_qual(wchar_t **, wargv)); rel1(&args); @@ -2368,20 +2374,20 @@ static val run(val command, val args) static void cat_stream_print(val stream, val out) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); format(out, lit("#<~s catenated ~s>"), stream->co.cls, streams, nao); } static val cat_get_line(val stream) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); while (streams) { val line = get_line(first(streams)); if (line) return line; if ((streams = rest(streams)) != nil) - stream->co.handle = (mem_t *) streams; + stream->co.handle = coerce(mem_t *, streams); } return nil; @@ -2389,14 +2395,14 @@ static val cat_get_line(val stream) static val cat_get_char(val stream) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); while (streams) { val ch = get_char(first(streams)); if (ch) return ch; if ((streams = rest(streams)) != nil) - stream->co.handle = (mem_t *) streams; + stream->co.handle = coerce(mem_t *, streams); } return nil; @@ -2404,14 +2410,14 @@ static val cat_get_char(val stream) static val cat_get_byte(val stream) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); while (streams) { val byte = get_byte(first(streams)); if (byte) return byte; if ((streams = rest(streams)) != nil) - stream->co.handle = (mem_t *) streams; + stream->co.handle = coerce(mem_t *, streams); } return nil; @@ -2419,7 +2425,7 @@ static val cat_get_byte(val stream) static val cat_unget_byte(val stream, int byte) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); if (!streams) { uw_throwf(file_error_s, @@ -2435,7 +2441,7 @@ static val cat_unget_byte(val stream, int byte) static val cat_unget_char(val stream, val ch) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); if (!streams) { uw_throwf(file_error_s, @@ -2449,7 +2455,7 @@ static val cat_unget_char(val stream, val ch) static val cat_get_prop(val stream, val ind) { - val streams = (val) stream->co.handle; + val streams = coerce(val, stream->co.handle); if (streams) return stream_get_prop(first(streams), ind); return nil; @@ -2457,7 +2463,7 @@ static val cat_get_prop(val stream, val ind) static void cat_mark(val stream) { - val obj = (val) stream->co.handle; + val obj = coerce(val, stream->co.handle); gc_mark(obj); } @@ -2484,7 +2490,7 @@ static struct strm_ops cat_stream_ops = { val make_catenated_stream(val stream_list) { - return cobj((mem_t *) stream_list, stream_s, &cat_stream_ops.cobj_ops); + return cobj(coerce(mem_t *, stream_list), stream_s, &cat_stream_ops.cobj_ops); } val remove_path(val path) @@ -193,7 +193,7 @@ static val getcwd_wrap(void) size_t guess = 256; for (;;) { - char *u8buf = (char *) chk_malloc(guess); + char *u8buf = coerce(char *, chk_malloc(guess)); if (getcwd(u8buf, guess) == 0) { free(u8buf); @@ -291,7 +291,7 @@ static val readlink_wrap(val path) ssize_t guess = 256; for (;;) { - char *u8buf = (char *) chk_malloc(guess); + char *u8buf = coerce(char *, chk_malloc(guess)); ssize_t bytes = readlink(u8path, u8buf, guess); if (bytes >= guess) { @@ -115,13 +115,13 @@ val closelog_wrap(void) static void syslog_mark(val stream) { - val stuff = (val) stream->co.handle; + val stuff = coerce(val, stream->co.handle); gc_mark(stuff); } static val syslog_put_string(val stream, val str) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); cons_bind (prio, strstream, cell); for (;;) { @@ -147,7 +147,7 @@ static val syslog_put_string(val stream, val str) static val syslog_put_char(val stream, val ch) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); cons_bind (prio, strstream, cell); if (ch == chr('\n')) { @@ -163,7 +163,7 @@ static val syslog_put_char(val stream, val ch) static val syslog_put_byte(val stream, int ch) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); cons_bind (prio, strstream, cell); if (ch == '\n') { @@ -180,7 +180,7 @@ static val syslog_put_byte(val stream, int ch) static val syslog_get_prop(val stream, val ind) { if (ind == prio_k) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); return car(cell); } else if (ind == name_k) { return lit("syslog"); @@ -191,7 +191,7 @@ static val syslog_get_prop(val stream, val ind) static val syslog_set_prop(val stream, val ind, val prop) { if (ind == prio_k) { - val cell = (val) stream->co.handle; + val cell = coerce(val, stream->co.handle); set(car_l(cell), prop); return t; } @@ -221,6 +221,6 @@ static struct strm_ops syslog_strm_ops = { val make_syslog_stream(val prio) { - return cobj((mem_t *) cons(prio, make_string_output_stream()), + return cobj(coerce(mem_t *, cons(prio, make_string_output_stream())), stream_s, &syslog_strm_ops.cobj_ops); } @@ -180,7 +180,7 @@ static val get_self_path(void) char self[PATH_MAX] = { 0 }; int nchar = readlink("/proc/self/exe", self, sizeof self); - if (nchar < 0 || nchar >= (int) sizeof self) + if (nchar < 0 || nchar >= convert(int, sizeof self)) return nil; return string_utf8(self); } @@ -426,9 +426,9 @@ void uw_continue(uw_frame_t *current, uw_frame_t *cont) void uw_init(void) { - protect(&toplevel_env.ev.func_bindings, + protect(&toplevel_env.ev.func_bindings, &toplevel_env.ev.match_context, - &exception_subtypes, (val *) 0); + &exception_subtypes, convert(val *, 0)); exception_subtypes = cons(cons(t, nil), exception_subtypes); uw_register_subtype(type_error_s, error_s); uw_register_subtype(internal_error_s, error_s); @@ -114,7 +114,7 @@ size_t utf8_from_uc(wchar_t *wdst, const unsigned char *src) if (ch >= 0x80 && ch < 0xC0) { wch <<= 6; wch |= (ch & 0x3F); - state = (enum utf8_state) (state - 1); + state = convert(enum utf8_state, state - 1); if (state == utf8_init) { if (wch < wch_min || (wch <= 0xFFFF && (wch & 0xFF00) == 0xDC00) || @@ -147,7 +147,7 @@ size_t utf8_from_uc(wchar_t *wdst, const unsigned char *src) size_t utf8_from(wchar_t *wdst, const char *src) { - return utf8_from_uc(wdst, (const unsigned char *) src); + return utf8_from_uc(wdst, coerce(const unsigned char *, src)); } size_t utf8_to_uc(unsigned char *dst, const wchar_t *wsrc) @@ -197,13 +197,13 @@ size_t utf8_to_uc(unsigned char *dst, const wchar_t *wsrc) size_t utf8_to(char *dst, const wchar_t *wsrc) { - return utf8_to_uc((unsigned char *) dst, wsrc); + return utf8_to_uc(coerce(unsigned char *, dst), wsrc); } wchar_t *utf8_dup_from_uc(const unsigned char *str) { size_t nchar = utf8_from_uc(0, str); - wchar_t *wstr = (wchar_t *) chk_malloc(nchar * sizeof *wstr); + wchar_t *wstr = coerce(wchar_t *, chk_malloc(nchar * sizeof *wstr)); utf8_from_uc(wstr, str); return wstr; } @@ -211,7 +211,7 @@ wchar_t *utf8_dup_from_uc(const unsigned char *str) wchar_t *utf8_dup_from(const char *str) { size_t nchar = utf8_from(0, str); - wchar_t *wstr = (wchar_t *) chk_malloc(nchar * sizeof *wstr); + wchar_t *wstr = coerce(wchar_t *, chk_malloc(nchar * sizeof *wstr)); utf8_from(wstr, str); return wstr; } @@ -227,7 +227,7 @@ unsigned char *utf8_dup_to_uc(const wchar_t *wstr) char *utf8_dup_to(const wchar_t *wstr) { size_t nbyte = utf8_to(0, wstr); - char *str = (char *) chk_malloc(nbyte); + char *str = coerce(char *, chk_malloc(nbyte)); utf8_to(str, wstr); return str; } @@ -332,9 +332,9 @@ wint_t utf8_decode(utf8_decoder_t *ud, int (*get)(mem_t *ctx), mem_t *ctx) if (ch >= 0x80 && ch < 0xC0) { ud->wch <<= 6; ud->wch |= (ch & 0x3F); - ud->state = (enum utf8_state) (ud->state - 1); + ud->state = convert(enum utf8_state, ud->state - 1); if (ud->state == utf8_init) { - if (ud->wch < ud->wch_min || + if (ud->wch < ud->wch_min || (ud->wch <= 0xFFFF && (ud->wch & 0xFF00) == 0xDC00) || (ud->wch > 0x10FFFF)) { |