validate.h 12 KB

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