Skip to content
Myr

Declare cfunction.fptr as jv (*)() to avoid having to cast everywhere

Before adoption: this commit came in with the history the project adopted, so no landing records it.

You only need to specify the return type in a function pointer declaration in C. If you use () in the declaration, the function pointer can be called with any arguments, and the type of the arguments is decided for each function call based on the types of the arguments used for the call. (To declare a function pointer for a function with no arguments, you use `(void)'.) Since all the cfunction structs have a fptr that points to a functions that return jv, not void, we can we can just declare cfunction.fptr as jv (*)() and avoid having those annoying and technically not C-standard compliant casts everywhere.

authored by Emanuele Torre · committed by Nico Williams · parent f61f842 · Browse files · 4 files+81 added−88 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+81 added−88 removed

  • other:+81 added−88 removed

4 files+81 added−88 removed

modified:src/builtin.c+74 added-74 removed
1702 unmodified lines
1703 return jq_util_input_get_current_line(jq);1704}17051706#define LIBM_DD(name) \1707 {(cfunction_ptr)f_ ## name, #name, 1},1707 {f_ ## name, #name, 1},1708#define LIBM_DD_NO(name)17091710#define LIBM_DDD(name) \1711 {(cfunction_ptr)f_ ## name, #name, 3},1711 {f_ ## name, #name, 3},1712#define LIBM_DDD_NO(name)17131714#define LIBM_DDDD(name) \1715 {(cfunction_ptr)f_ ## name, #name, 4},1715 {f_ ## name, #name, 4},1716#define LIBM_DDDD_NO(name)17171718static const struct cfunction function_list[] = {1719#include "libm.h"1720#ifdef HAVE_FREXP1721 {(cfunction_ptr)f_frexp,"frexp", 1},1721 {f_frexp,"frexp", 1},1722#endif1723#ifdef HAVE_MODF1724 {(cfunction_ptr)f_modf,"modf", 1},1724 {f_modf,"modf", 1},1725#endif1726#ifdef HAVE_LGAMMA_R1727 {(cfunction_ptr)f_lgamma_r,"lgamma_r", 1},1727 {f_lgamma_r,"lgamma_r", 1},1728#endif1729 {(cfunction_ptr)f_plus, "_plus", 3},1730 {(cfunction_ptr)f_negate, "_negate", 1},1731 {(cfunction_ptr)f_minus, "_minus", 3},1732 {(cfunction_ptr)f_multiply, "_multiply", 3},1733 {(cfunction_ptr)f_divide, "_divide", 3},1734 {(cfunction_ptr)f_mod, "_mod", 3},1735 {(cfunction_ptr)f_dump, "tojson", 1},1736 {(cfunction_ptr)f_json_parse, "fromjson", 1},1737 {(cfunction_ptr)f_tonumber, "tonumber", 1},1738 {(cfunction_ptr)f_tostring, "tostring", 1},1739 {(cfunction_ptr)f_keys, "keys", 1},1740 {(cfunction_ptr)f_keys_unsorted, "keys_unsorted", 1},1741 {(cfunction_ptr)f_startswith, "startswith", 2},1742 {(cfunction_ptr)f_endswith, "endswith", 2},1743 {(cfunction_ptr)f_ltrimstr, "ltrimstr", 2},1744 {(cfunction_ptr)f_rtrimstr, "rtrimstr", 2},1745 {(cfunction_ptr)f_string_split, "split", 2},1746 {(cfunction_ptr)f_string_explode, "explode", 1},1747 {(cfunction_ptr)f_string_implode, "implode", 1},1748 {(cfunction_ptr)f_string_indexes, "_strindices", 2},1749 {(cfunction_ptr)f_setpath, "setpath", 3}, // FIXME typechecking1750 {(cfunction_ptr)f_getpath, "getpath", 2},1751 {(cfunction_ptr)f_delpaths, "delpaths", 2},1752 {(cfunction_ptr)f_has, "has", 2},1753 {(cfunction_ptr)f_equal, "_equal", 3},1754 {(cfunction_ptr)f_notequal, "_notequal", 3},1755 {(cfunction_ptr)f_less, "_less", 3},1756 {(cfunction_ptr)f_greater, "_greater", 3},1757 {(cfunction_ptr)f_lesseq, "_lesseq", 3},1758 {(cfunction_ptr)f_greatereq, "_greatereq", 3},1759 {(cfunction_ptr)f_contains, "contains", 2},1760 {(cfunction_ptr)f_length, "length", 1},1761 {(cfunction_ptr)f_utf8bytelength, "utf8bytelength", 1},1762 {(cfunction_ptr)f_type, "type", 1},1763 {(cfunction_ptr)f_isinfinite, "isinfinite", 1},1764 {(cfunction_ptr)f_isnan, "isnan", 1},1765 {(cfunction_ptr)f_isnormal, "isnormal", 1},1766 {(cfunction_ptr)f_infinite, "infinite", 1},1767 {(cfunction_ptr)f_nan, "nan", 1},1768 {(cfunction_ptr)f_sort, "sort", 1},1769 {(cfunction_ptr)f_sort_by_impl, "_sort_by_impl", 2},1770 {(cfunction_ptr)f_group_by_impl, "_group_by_impl", 2},1771 {(cfunction_ptr)f_min, "min", 1},1772 {(cfunction_ptr)f_max, "max", 1},1773 {(cfunction_ptr)f_min_by_impl, "_min_by_impl", 2},1774 {(cfunction_ptr)f_max_by_impl, "_max_by_impl", 2},1775 {(cfunction_ptr)f_error, "error", 1},1776 {(cfunction_ptr)f_format, "format", 2},1777 {(cfunction_ptr)f_env, "env", 1},1778 {(cfunction_ptr)f_halt, "halt", 1},1779 {(cfunction_ptr)f_halt_error, "halt_error", 2},1780 {(cfunction_ptr)f_get_search_list, "get_search_list", 1},1781 {(cfunction_ptr)f_get_prog_origin, "get_prog_origin", 1},1782 {(cfunction_ptr)f_get_jq_origin, "get_jq_origin", 1},1783 {(cfunction_ptr)f_match, "_match_impl", 4},1784 {(cfunction_ptr)f_modulemeta, "modulemeta", 1},1785 {(cfunction_ptr)f_input, "input", 1},1786 {(cfunction_ptr)f_debug, "debug", 1},1787 {(cfunction_ptr)f_stderr, "stderr", 1},1788 {(cfunction_ptr)f_strptime, "strptime", 2},1789 {(cfunction_ptr)f_strftime, "strftime", 2},1790 {(cfunction_ptr)f_strflocaltime, "strflocaltime", 2},1791 {(cfunction_ptr)f_mktime, "mktime", 1},1792 {(cfunction_ptr)f_gmtime, "gmtime", 1},1793 {(cfunction_ptr)f_localtime, "localtime", 1},1794 {(cfunction_ptr)f_now, "now", 1},1795 {(cfunction_ptr)f_current_filename, "input_filename", 1},1796 {(cfunction_ptr)f_current_line, "input_line_number", 1},1729 {f_plus, "_plus", 3},1730 {f_negate, "_negate", 1},1731 {f_minus, "_minus", 3},1732 {f_multiply, "_multiply", 3},1733 {f_divide, "_divide", 3},1734 {f_mod, "_mod", 3},1735 {f_dump, "tojson", 1},1736 {f_json_parse, "fromjson", 1},1737 {f_tonumber, "tonumber", 1},1738 {f_tostring, "tostring", 1},1739 {f_keys, "keys", 1},1740 {f_keys_unsorted, "keys_unsorted", 1},1741 {f_startswith, "startswith", 2},1742 {f_endswith, "endswith", 2},1743 {f_ltrimstr, "ltrimstr", 2},1744 {f_rtrimstr, "rtrimstr", 2},1745 {f_string_split, "split", 2},1746 {f_string_explode, "explode", 1},1747 {f_string_implode, "implode", 1},1748 {f_string_indexes, "_strindices", 2},1749 {f_setpath, "setpath", 3}, // FIXME typechecking1750 {f_getpath, "getpath", 2},1751 {f_delpaths, "delpaths", 2},1752 {f_has, "has", 2},1753 {f_equal, "_equal", 3},1754 {f_notequal, "_notequal", 3},1755 {f_less, "_less", 3},1756 {f_greater, "_greater", 3},1757 {f_lesseq, "_lesseq", 3},1758 {f_greatereq, "_greatereq", 3},1759 {f_contains, "contains", 2},1760 {f_length, "length", 1},1761 {f_utf8bytelength, "utf8bytelength", 1},1762 {f_type, "type", 1},1763 {f_isinfinite, "isinfinite", 1},1764 {f_isnan, "isnan", 1},1765 {f_isnormal, "isnormal", 1},1766 {f_infinite, "infinite", 1},1767 {f_nan, "nan", 1},1768 {f_sort, "sort", 1},1769 {f_sort_by_impl, "_sort_by_impl", 2},1770 {f_group_by_impl, "_group_by_impl", 2},1771 {f_min, "min", 1},1772 {f_max, "max", 1},1773 {f_min_by_impl, "_min_by_impl", 2},1774 {f_max_by_impl, "_max_by_impl", 2},1775 {f_error, "error", 1},1776 {f_format, "format", 2},1777 {f_env, "env", 1},1778 {f_halt, "halt", 1},1779 {f_halt_error, "halt_error", 2},1780 {f_get_search_list, "get_search_list", 1},1781 {f_get_prog_origin, "get_prog_origin", 1},1782 {f_get_jq_origin, "get_jq_origin", 1},1783 {f_match, "_match_impl", 4},1784 {f_modulemeta, "modulemeta", 1},1785 {f_input, "input", 1},1786 {f_debug, "debug", 1},1787 {f_stderr, "stderr", 1},1788 {f_strptime, "strptime", 2},1789 {f_strftime, "strftime", 2},1790 {f_strflocaltime, "strflocaltime", 2},1791 {f_mktime, "mktime", 1},1792 {f_gmtime, "gmtime", 1},1793 {f_localtime, "localtime", 1},1794 {f_now, "now", 1},1795 {f_current_filename, "input_filename", 1},1796 {f_current_line, "input_line_number", 1},1797};1798#undef LIBM_DDDD_NO1799#undef LIBM_DDD_NO1800#undef LIBM_DD_NO
106 unmodified lines
modified:src/bytecode.h+1 added-2 removed
43 unmodified lines
44const struct opcode_description* opcode_describe(opcode op);454647#define MAX_CFUNCTION_ARGS 1048typedef void (*cfunction_ptr)();48struct cfunction {50 cfunction_ptr fptr;49 jv (*fptr)();50 const char* name;51 int nargs;52};53
38 unmodified lines
modified:src/execute.c+5 added-10 removed
912 unmodified lines
913 for (int i = 1; i < nargs; i++) {914 in[i] = stack_pop(jq);915 }916 struct cfunction* function = &frame_current(jq)->bc->globals->cfunctions[*pc++];917 typedef jv (*func_1)(jq_state*,jv);918 typedef jv (*func_2)(jq_state*,jv,jv);919 typedef jv (*func_3)(jq_state*,jv,jv,jv);920 typedef jv (*func_4)(jq_state*,jv,jv,jv,jv);921 typedef jv (*func_5)(jq_state*,jv,jv,jv,jv,jv);917 switch (function->nargs) {923 case 1: top = ((func_1)function->fptr)(jq, in[0]); break;924 case 2: top = ((func_2)function->fptr)(jq, in[0], in[1]); break;925 case 3: top = ((func_3)function->fptr)(jq, in[0], in[1], in[2]); break;926 case 4: top = ((func_4)function->fptr)(jq, in[0], in[1], in[2], in[3]); break;927 case 5: top = ((func_5)function->fptr)(jq, in[0], in[1], in[2], in[3], in[4]); break;918 case 1: top = function->fptr(jq, in[0]); break;919 case 2: top = function->fptr(jq, in[0], in[1]); break;920 case 3: top = function->fptr(jq, in[0], in[1], in[2]); break;921 case 4: top = function->fptr(jq, in[0], in[1], in[2], in[3]); break;922 case 5: top = function->fptr(jq, in[0], in[1], in[2], in[3], in[4]); break;923 // FIXME: a) up to 7 arguments (input + 6), b) should assert924 // because the compiler should not generate this error.925 default: return jv_invalid_with_msg(jv_string("Function takes too many arguments"));926 }
420 unmodified lines
modified:Makefile.am+1 added-2 removed
18 unmodified lines
1920### C build options2122AM_CFLAGS = -Wextra -Wall -Wno-missing-field-initializers \23 -Wno-unused-parameter -Wno-unused-function \24 -Wno-cast-function-type23 -Wno-unused-parameter -Wno-unused-function2425if WIN3226AM_CFLAGS += -municode27endif
217 unmodified lines