validate.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* classes: h_files */
  2. #ifndef SCM_VALIDATE_H
  3. #define SCM_VALIDATE_H
  4. /* Copyright (C) 1999,2000,2001, 2002, 2004, 2006, 2007 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
  21. #define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
  22. #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
  23. #define SCM_SYSERROR_MSG(str, args, val) \
  24. do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
  25. #define SCM_MISC_ERROR(str, args) \
  26. do { scm_misc_error (FUNC_NAME, str, args); } while (0)
  27. #define SCM_WRONG_NUM_ARGS() \
  28. do { scm_error_num_args_subr (FUNC_NAME); } while (0)
  29. #define SCM_WRONG_TYPE_ARG(pos, obj) \
  30. do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
  31. #define SCM_NUM2SIZE(pos, arg) (scm_to_size_t (arg))
  32. #define SCM_NUM2SIZE_DEF(pos, arg, def) \
  33. (SCM_UNBNDP (arg) ? def : scm_to_size_t (arg))
  34. #define SCM_NUM2PTRDIFF(pos, arg) (scm_to_ssize_t (arg))
  35. #define SCM_NUM2PTRDIFF_DEF(pos, arg, def) \
  36. (SCM_UNBNDP (arg) ? def : scm_to_ssize_t (arg))
  37. #define SCM_NUM2SHORT(pos, arg) (scm_to_short (arg))
  38. #define SCM_NUM2SHORT_DEF(pos, arg, def) \
  39. (SCM_UNBNDP (arg) ? def : scm_to_short (arg))
  40. #define SCM_NUM2USHORT(pos, arg) (scm_to_ushort (arg))
  41. #define SCM_NUM2USHORT_DEF(pos, arg, def) \
  42. (SCM_UNBNDP (arg) ? def : scm_to_ushort (arg))
  43. #define SCM_NUM2INT(pos, arg) (scm_to_int (arg))
  44. #define SCM_NUM2INT_DEF(pos, arg, def) \
  45. (SCM_UNBNDP (arg) ? def : scm_to_int (arg))
  46. #define SCM_NUM2UINT(pos, arg) (scm_to_uint (arg))
  47. #define SCM_NUM2UINT_DEF(pos, arg, def) \
  48. (SCM_UNBNDP (arg) ? def : scm_to_uint (arg))
  49. #define SCM_NUM2ULONG(pos, arg) (scm_to_ulong (arg))
  50. #define SCM_NUM2ULONG_DEF(pos, arg, def) \
  51. (SCM_UNBNDP (arg) ? def : scm_to_ulong (arg))
  52. #define SCM_NUM2LONG(pos, arg) (scm_to_long (arg))
  53. #define SCM_NUM2LONG_DEF(pos, arg, def) \
  54. (SCM_UNBNDP (arg) ? def : scm_to_long (arg))
  55. #define SCM_NUM2LONG_LONG(pos, arg) (scm_to_long_long (arg))
  56. #define SCM_NUM2LONG_LONG_DEF(pos, arg, def) \
  57. (SCM_UNBNDP (arg) ? def : scm_to_long_long (arg))
  58. #define SCM_NUM2ULONG_LONG(pos, arg) (scm_to_ulong_long (arg))
  59. #define SCM_NUM2ULONG_LONG_DEF(pos, arg, def) \
  60. (SCM_UNBNDP (arg) ? def : scm_to_ulong_long (arg))
  61. #define SCM_NUM2FLOAT(pos, arg) \
  62. (scm_num2float (arg, pos, FUNC_NAME))
  63. #define SCM_NUM2DOUBLE(pos, arg) \
  64. (scm_num2double (arg, pos, FUNC_NAME))
  65. #define SCM_OUT_OF_RANGE(pos, arg) \
  66. do { scm_out_of_range_pos (FUNC_NAME, arg, scm_from_int (pos)); } while (0)
  67. #define SCM_ASSERT_RANGE(pos, arg, f) \
  68. do { if (SCM_UNLIKELY (!(f))) \
  69. scm_out_of_range_pos (FUNC_NAME, arg, scm_from_int (pos)); } \
  70. while (0)
  71. #define SCM_MUST_MALLOC_TYPE(type) \
  72. ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
  73. #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
  74. ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
  75. #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
  76. #define SCM_MAKE_VALIDATE(pos, var, pred) \
  77. do { \
  78. SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
  79. } while (0)
  80. #define SCM_I_MAKE_VALIDATE_MSG2(pos, var, pred, msg) \
  81. do { \
  82. SCM_ASSERT_TYPE (pred (var), var, pos, FUNC_NAME, msg); \
  83. } while (0)
  84. #define SCM_MAKE_VALIDATE_MSG(pos, var, pred, msg) \
  85. SCM_I_MAKE_VALIDATE_MSG2 (pos, var, SCM_ ## pred, msg)
  86. #define SCM_VALIDATE_REST_ARGUMENT(x) \
  87. do { \
  88. if (SCM_DEBUG_REST_ARGUMENT) { \
  89. if (scm_ilength (x) < 0) { \
  90. SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
  91. } \
  92. } \
  93. } while (0)
  94. #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, NIMP, "non-immediate")
  95. #define SCM_VALIDATE_BOOL(pos, flag) \
  96. do { \
  97. SCM_ASSERT_TYPE (scm_is_bool (flag), flag, pos, FUNC_NAME, "boolean"); \
  98. } while (0)
  99. #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
  100. do { \
  101. SCM_ASSERT (scm_is_bool (flag), flag, pos, FUNC_NAME); \
  102. cvar = scm_to_bool (flag); \
  103. } while (0)
  104. #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, CHARP, "character")
  105. #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
  106. do { \
  107. SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
  108. cvar = SCM_CHAR (scm); \
  109. } while (0)
  110. #define SCM_VALIDATE_STRING(pos, str) \
  111. do { \
  112. SCM_ASSERT_TYPE (scm_is_string (str), str, pos, FUNC_NAME, "string"); \
  113. } while (0)
  114. #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE_MSG (pos, z, REALP, "real")
  115. #define SCM_VALIDATE_NUMBER(pos, z) SCM_MAKE_VALIDATE_MSG (pos, z, NUMBERP, "number")
  116. #define SCM_VALIDATE_USHORT_COPY(pos, k, cvar) \
  117. do { \
  118. cvar = SCM_NUM2USHORT (pos, k); \
  119. } while (0)
  120. #define SCM_VALIDATE_SHORT_COPY(pos, k, cvar) \
  121. do { \
  122. cvar = SCM_NUM2SHORT (pos, k); \
  123. } while (0)
  124. #define SCM_VALIDATE_UINT_COPY(pos, k, cvar) \
  125. do { \
  126. cvar = SCM_NUM2UINT (pos, k); \
  127. } while (0)
  128. #define SCM_VALIDATE_INT_COPY(pos, k, cvar) \
  129. do { \
  130. cvar = SCM_NUM2INT (pos, k); \
  131. } while (0)
  132. #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
  133. do { \
  134. cvar = SCM_NUM2ULONG (pos, k); \
  135. } while (0)
  136. #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
  137. do { \
  138. cvar = SCM_NUM2LONG (pos, k); \
  139. } while (0)
  140. #define SCM_VALIDATE_FLOAT_COPY(pos, k, cvar) \
  141. do { \
  142. cvar = SCM_NUM2FLOAT (pos, k); \
  143. } while (0)
  144. #define SCM_VALIDATE_DOUBLE_COPY(pos, k, cvar) \
  145. do { \
  146. cvar = SCM_NUM2DOUBLE (pos, k); \
  147. } while (0)
  148. #define SCM_VALIDATE_DOUBLE_DEF_COPY(pos, k, default, cvar) \
  149. do { \
  150. if (SCM_UNBNDP (k)) \
  151. { \
  152. k = scm_make_real (default); \
  153. cvar = default; \
  154. } \
  155. else \
  156. { \
  157. cvar = SCM_NUM2DOUBLE (pos, k); \
  158. } \
  159. } while (0)
  160. #define SCM_VALIDATE_NULL(pos, scm) \
  161. SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_null, "empty list")
  162. #define SCM_VALIDATE_NULL_OR_NIL(pos, scm) \
  163. SCM_MAKE_VALIDATE_MSG (pos, scm, NULL_OR_NIL_P, "empty list")
  164. #define SCM_VALIDATE_CONS(pos, scm) \
  165. SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_pair, "pair")
  166. #define SCM_VALIDATE_LIST(pos, lst) \
  167. do { \
  168. SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
  169. } while (0)
  170. #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
  171. do { \
  172. SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
  173. } while (0)
  174. #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
  175. do { \
  176. cvar = scm_ilength (lst); \
  177. SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
  178. } while (0)
  179. #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
  180. do { \
  181. cvar = scm_ilength (lst); \
  182. SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
  183. } while (0)
  184. #define SCM_VALIDATE_ALISTCELL(pos, alist) \
  185. do { \
  186. SCM_ASSERT (scm_is_pair (alist) && scm_is_pair (SCM_CAR (alist)), \
  187. alist, pos, FUNC_NAME); \
  188. } while (0)
  189. #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
  190. do { \
  191. SCM_ASSERT (scm_is_pair (alist), alist, pos, FUNC_NAME); \
  192. cvar = SCM_CAR (alist); \
  193. SCM_ASSERT (scm_is_pair (cvar), alist, pos, FUNC_NAME); \
  194. } while (0)
  195. #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
  196. do { \
  197. SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
  198. } while (0)
  199. #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE_MSG(pos, a, PRINT_STATE_P, "print-state")
  200. #define SCM_VALIDATE_SMOB(pos, obj, type) \
  201. do { \
  202. SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
  203. obj, pos, FUNC_NAME); \
  204. } while (0)
  205. #define SCM_VALIDATE_THUNK(pos, thunk) \
  206. do { \
  207. SCM_ASSERT (scm_is_true (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
  208. } while (0)
  209. #define SCM_VALIDATE_SYMBOL(pos, str) \
  210. do { \
  211. SCM_ASSERT_TYPE (scm_is_symbol (str), str, pos, FUNC_NAME, "symbol"); \
  212. } while (0)
  213. #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE_MSG (pos, var, VARIABLEP, "variable")
  214. #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE_MSG (pos, obj, MEMOIZEDP, "memoized code")
  215. #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE_MSG (pos, obj, CLOSUREP, "closure")
  216. #define SCM_VALIDATE_PROC(pos, proc) \
  217. do { \
  218. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, pos, FUNC_NAME); \
  219. } while (0)
  220. #define SCM_VALIDATE_NULLORCONS(pos, env) \
  221. do { \
  222. SCM_ASSERT (scm_is_null (env) || scm_is_pair (env), env, pos, FUNC_NAME); \
  223. } while (0)
  224. #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE_MSG (pos, a, HOOKP, "hook")
  225. #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE_MSG (pos, a, RGXP, "regexp")
  226. #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, DIRP, "directory port")
  227. #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, PORTP, "port")
  228. #define SCM_VALIDATE_INPUT_PORT(pos, port) \
  229. SCM_MAKE_VALIDATE_MSG (pos, port, INPUT_PORT_P, "input port")
  230. #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
  231. SCM_MAKE_VALIDATE_MSG (pos, port, OUTPUT_PORT_P, "output port")
  232. #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, FPORTP, "file port")
  233. #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, OPFPORTP, "open file port")
  234. #define SCM_VALIDATE_OPINPORT(pos, port) \
  235. SCM_MAKE_VALIDATE_MSG (pos, port, OPINPORTP, "open input port")
  236. #define SCM_VALIDATE_OPENPORT(pos, port) \
  237. do { \
  238. SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
  239. port, pos, FUNC_NAME); \
  240. } while (0)
  241. #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, OPPORTP, "open port")
  242. #define SCM_VALIDATE_OPOUTPORT(pos, port) \
  243. SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTPORTP, "open output port")
  244. #define SCM_VALIDATE_OPOUTSTRPORT(pos, port) \
  245. SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTSTRPORTP, "open output string port")
  246. #define SCM_VALIDATE_FLUID(pos, fluid) \
  247. SCM_I_MAKE_VALIDATE_MSG2 (pos, fluid, scm_is_fluid, "fluid")
  248. #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, KEYWORDP, "keyword")
  249. #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, STACKP, "stack")
  250. #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, FRAMEP, "frame")
  251. #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, RSTATEP, "random-generator-state")
  252. #define SCM_VALIDATE_ARRAY(pos, v) \
  253. do { \
  254. SCM_ASSERT (!SCM_IMP (v) \
  255. && scm_is_true (scm_array_p (v, SCM_UNDEFINED)), \
  256. v, pos, FUNC_NAME); \
  257. } while (0)
  258. #define SCM_VALIDATE_VECTOR(pos, v) \
  259. do { \
  260. SCM_ASSERT (scm_is_simple_vector (v), v, pos, FUNC_NAME); \
  261. } while (0)
  262. #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
  263. do { \
  264. SCM_ASSERT ((scm_is_simple_vector (v) \
  265. || (scm_is_true (scm_f64vector_p (v)))), \
  266. v, pos, FUNC_NAME); \
  267. } while (0)
  268. #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, STRUCTP, "struct")
  269. #define SCM_VALIDATE_VTABLE(pos, v) \
  270. do { \
  271. SCM_ASSERT (!SCM_IMP (v) && scm_is_true (scm_struct_vtable_p (v)), \
  272. v, pos, FUNC_NAME); \
  273. } while (0)
  274. #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
  275. do { \
  276. SCM_ASSERT (SCM_VECTORP (v) && len == SCM_VECTOR_LENGTH (v), v, pos, FUNC_NAME); \
  277. } while (0)
  278. #endif /* SCM_VALIDATE_H */
  279. /*
  280. Local Variables:
  281. c-file-style: "gnu"
  282. End:
  283. */