validate.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* $Id: validate.h,v 1.12 2000-06-05 11:39:46 dirk Exp $ */
  2. /* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; see the file COPYING. If not, write to
  16. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. * Boston, MA 02111-1307 USA
  18. *
  19. * As a special exception, the Free Software Foundation gives permission
  20. * for additional uses of the text contained in its release of GUILE.
  21. *
  22. * The exception is that, if you link the GUILE library with other files
  23. * to produce an executable, this does not by itself cause the
  24. * resulting executable to be covered by the GNU General Public License.
  25. * Your use of that executable is in no way restricted on account of
  26. * linking the GUILE library code into it.
  27. *
  28. * This exception does not however invalidate any other reasons why
  29. * the executable file might be covered by the GNU General Public License.
  30. *
  31. * This exception applies only to the code released by the
  32. * Free Software Foundation under the name GUILE. If you copy
  33. * code from other Free Software Foundation releases into a copy of
  34. * GUILE, as the General Public License permits, the exception does
  35. * not apply to the code that you add in this way. To avoid misleading
  36. * anyone as to the status of such modified files, you must delete
  37. * this exception notice from them.
  38. *
  39. * If you write modifications of your own for GUILE, it is your choice
  40. * whether to permit this exception to apply to your modifications.
  41. * If you do not wish that, delete this exception notice. */
  42. /* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
  43. #ifndef SCM_VALIDATE_H__
  44. #define SCM_VALIDATE_H__
  45. #define SCM_FUNC_NAME (scm_makfrom0str (FUNC_NAME))
  46. #define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
  47. #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
  48. #define SCM_SYSERROR_MSG(str, args, val) \
  49. do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
  50. #define SCM_COERCE_ROSTRING(pos, scm) \
  51. do { scm = scm_coerce_rostring (scm, FUNC_NAME, pos); } while (0)
  52. #define SCM_WTA(pos, scm) \
  53. do { scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
  54. #define RETURN_SCM_WTA(pos, scm) \
  55. do { return scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
  56. #define SCM_MISC_ERROR(str, args) \
  57. do { scm_misc_error (FUNC_NAME, str, args); } while (0)
  58. #define SCM_WRONG_TYPE_ARG(pos, obj) \
  59. do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
  60. #define SCM_NUM2ULONG(pos, arg) (scm_num2ulong (arg, (char *) pos, FUNC_NAME))
  61. #define SCM_NUM2LONG(pos, arg) (scm_num2long (arg, (char *) pos, FUNC_NAME))
  62. #define SCM_NUM2LONG_DEF(pos, arg, def) \
  63. (SCM_UNBNDP (arg) ? def : scm_num2long (arg, (char *) pos, FUNC_NAME))
  64. #define SCM_NUM2LONG_LONG(pos, arg) \
  65. (scm_num2long_long (arg, (char *) pos, FUNC_NAME))
  66. #define SCM_OUT_OF_RANGE(pos, arg) \
  67. do { scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
  68. #define SCM_ASSERT_RANGE(pos, arg, f) \
  69. do { SCM_ASSERT (f, arg, SCM_OUTOFRANGE, FUNC_NAME); } while (0)
  70. #define SCM_MUST_MALLOC_TYPE(type) \
  71. ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
  72. #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
  73. ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
  74. #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
  75. #define SCM_MAKE_VALIDATE(pos, var, pred) \
  76. do { \
  77. SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
  78. } while (0)
  79. #define SCM_VALIDATE_REST_ARGUMENT(x) \
  80. do { \
  81. if (SCM_DEBUG_REST_ARGUMENT) { \
  82. if (scm_ilength (x) < 0) { \
  83. SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
  84. } \
  85. } \
  86. } while (0)
  87. #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NIMP)
  88. #define SCM_VALIDATE_BOOL(pos, flag) SCM_MAKE_VALIDATE(pos, flag, BOOLP)
  89. #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
  90. do { \
  91. SCM_ASSERT (SCM_BOOLP (flag), flag, pos, FUNC_NAME); \
  92. cvar = SCM_EQ_P (flag, SCM_BOOL_T) ? 1 : 0; \
  93. } while (0)
  94. #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CHARP)
  95. #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
  96. do { \
  97. SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
  98. cvar = SCM_CHAR (scm); \
  99. } while (0)
  100. #define SCM_VALIDATE_ROSTRING(pos, str) SCM_MAKE_VALIDATE (pos, str, ROSTRINGP)
  101. #define SCM_VALIDATE_ROSTRING_COPY(pos, str, cvar) \
  102. do { \
  103. SCM_ASSERT (SCM_ROSTRINGP (str), str, pos, FUNC_NAME); \
  104. cvar = SCM_ROCHARS (str); \
  105. } while (0)
  106. #define SCM_VALIDATE_NULLORROSTRING_COPY(pos, str, cvar) \
  107. do { \
  108. SCM_ASSERT (SCM_FALSEP (str) || SCM_ROSTRINGP (str), \
  109. str, pos, FUNC_NAME); \
  110. if (SCM_FALSEP(str)) \
  111. cvar = NULL; \
  112. else \
  113. cvar = SCM_ROCHARS(str); \
  114. } while (0)
  115. #define SCM_VALIDATE_STRING(pos, str) SCM_MAKE_VALIDATE (pos, str, STRINGP)
  116. #define SCM_VALIDATE_STRINGORSUBSTR(pos, str) \
  117. do { \
  118. SCM_ASSERT (SCM_STRINGP (str) || SCM_SUBSTRP (str), \
  119. str, pos, FUNC_NAME); \
  120. } while (0)
  121. #define SCM_VALIDATE_STRING_COPY(pos, str, cvar) \
  122. do { \
  123. SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
  124. cvar = SCM_CHARS(str); \
  125. } while (0)
  126. #define SCM_VALIDATE_RWSTRING(pos, str) \
  127. do { \
  128. SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
  129. if (!SCM_RWSTRINGP (str)) \
  130. scm_misc_error (FUNC_NAME, "argument is a read-only string", str); \
  131. } while (0)
  132. #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE (pos, z, REALP)
  133. #define SCM_VALIDATE_NUMBER_COPY(pos, z, cvar) \
  134. do { \
  135. if (SCM_INUMP (z)) \
  136. cvar = (double) SCM_INUM (z); \
  137. else if (SCM_REALP (z)) \
  138. cvar = SCM_REAL_VALUE (z); \
  139. else if (SCM_BIGP (z)) \
  140. cvar = scm_big2dbl (z); \
  141. else \
  142. { \
  143. cvar = 0.0; \
  144. SCM_WTA (pos, z); \
  145. } \
  146. } while (0)
  147. #define SCM_VALIDATE_INUM(pos, k) SCM_MAKE_VALIDATE (pos, k, INUMP)
  148. #define SCM_VALIDATE_INUM_COPY(pos, k, cvar) \
  149. do { \
  150. SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
  151. cvar = SCM_INUM (k); \
  152. } while (0)
  153. #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
  154. do { \
  155. cvar = SCM_NUM2ULONG (pos, k); \
  156. } while (0)
  157. #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
  158. do { \
  159. cvar = SCM_NUM2LONG(pos, k); \
  160. } while (0)
  161. #define SCM_VALIDATE_BIGINT(pos, k) SCM_MAKE_VALIDATE (pos, k, BIGP)
  162. #define SCM_VALIDATE_INUM_MIN(pos, k, min) \
  163. do { \
  164. SCM_ASSERT (SCM_INUMP(k), k, pos, FUNC_NAME); \
  165. SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
  166. } while (0)
  167. #define SCM_VALIDATE_INUM_MIN_COPY(pos, k, min, cvar) \
  168. do { \
  169. SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
  170. cvar = SCM_INUM (k); \
  171. SCM_ASSERT_RANGE (pos, k, (cvar >= min)); \
  172. } while (0)
  173. #define SCM_VALIDATE_INUM_MIN_DEF_COPY(pos, k, min, default, cvar) \
  174. do { \
  175. if (SCM_UNBNDP (k)) \
  176. k = SCM_MAKINUM (default); \
  177. SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
  178. cvar = SCM_INUM (k); \
  179. SCM_ASSERT_RANGE (pos, k, (cvar >= min)); \
  180. } while (0)
  181. #define SCM_VALIDATE_INUM_DEF(pos, k, default) \
  182. do { \
  183. if (SCM_UNBNDP (k)) \
  184. k = SCM_MAKINUM (default); \
  185. else SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
  186. } while (0)
  187. #define SCM_VALIDATE_INUM_DEF_COPY(pos, k, default, cvar) \
  188. do { \
  189. if (SCM_UNBNDP (k)) \
  190. { \
  191. k = SCM_MAKINUM (default); \
  192. cvar = default; \
  193. } \
  194. else \
  195. { \
  196. SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
  197. cvar = SCM_INUM (k); \
  198. } \
  199. } while (0)
  200. /* [low,high) */
  201. #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
  202. do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
  203. SCM_ASSERT_RANGE(pos,k, \
  204. (SCM_INUM (k) >= low && \
  205. SCM_INUM (k) < high)); \
  206. } while (0)
  207. #define SCM_VALIDATE_INUM_RANGE_COPY(pos, k, low, high, cvar) \
  208. do { \
  209. SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
  210. cvar = SCM_INUM (k); \
  211. SCM_ASSERT_RANGE (pos, k, cvar >= low && cvar < high); \
  212. } while (0)
  213. #define SCM_VALIDATE_NULL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULLP)
  214. #define SCM_VALIDATE_CONS(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CONSP)
  215. #define SCM_VALIDATE_LIST(pos, lst) \
  216. do { \
  217. SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
  218. } while (0)
  219. #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
  220. do { \
  221. SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
  222. } while (0)
  223. #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
  224. do { \
  225. cvar = scm_ilength (lst); \
  226. SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
  227. } while (0)
  228. #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
  229. do { \
  230. cvar = scm_ilength (lst); \
  231. SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
  232. } while (0)
  233. #define SCM_VALIDATE_ALISTCELL(pos, alist) \
  234. do { \
  235. SCM_ASSERT (SCM_CONSP (alist) && SCM_CONSP (SCM_CAR (alist)), \
  236. alist, pos, FUNC_NAME); \
  237. } while (0)
  238. #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
  239. do { \
  240. SCM_ASSERT (SCM_CONSP (alist), alist, pos, FUNC_NAME); \
  241. cvar = SCM_CAR (alist); \
  242. SCM_ASSERT (SCM_CONSP (cvar), alist, pos, FUNC_NAME); \
  243. } while (0)
  244. #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
  245. do { \
  246. SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
  247. } while (0)
  248. #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
  249. #define SCM_VALIDATE_SMOB(pos, obj, type) \
  250. do { \
  251. SCM_ASSERT ((SCM_NIMP (obj) && SCM_TYP16 (obj) == scm_tc16_ ## type), \
  252. obj, pos, FUNC_NAME); \
  253. } while (0)
  254. #define SCM_VALIDATE_THREAD(pos, a) SCM_MAKE_VALIDATE (pos, a, THREADP)
  255. #define SCM_VALIDATE_THUNK(pos, thunk) \
  256. do { \
  257. SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
  258. } while (0)
  259. #define SCM_VALIDATE_SYMBOL(pos, sym) SCM_MAKE_VALIDATE (pos, sym, SYMBOLP)
  260. #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE (pos, var, VARIABLEP)
  261. #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE (pos, obj, MEMOIZEDP)
  262. #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE (pos, obj, CLOSUREP)
  263. #define SCM_VALIDATE_PROC(pos, proc) \
  264. do { \
  265. SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), proc, pos, FUNC_NAME); \
  266. } while (0)
  267. #define SCM_VALIDATE_NULLORCONS(pos, env) \
  268. do { \
  269. SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); \
  270. } while (0)
  271. #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE (pos, a, HOOKP)
  272. #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE (pos, a, RGXP)
  273. #define SCM_VALIDATE_OPDIR(pos, port) SCM_MAKE_VALIDATE (pos, port, OPDIRP)
  274. #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE (pos, port, DIRP)
  275. #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE (pos, port, PORTP)
  276. #define SCM_VALIDATE_INPUT_PORT(pos, port) \
  277. SCM_MAKE_VALIDATE (pos, port, INPUT_PORT_P)
  278. #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
  279. SCM_MAKE_VALIDATE (pos, port, OUTPUT_PORT_P)
  280. #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, FPORTP)
  281. #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPFPORTP)
  282. #define SCM_VALIDATE_OPINPORT(pos, port) \
  283. SCM_MAKE_VALIDATE (pos, port, OPINPORTP)
  284. #define SCM_VALIDATE_OPENPORT(pos,port) \
  285. do { \
  286. SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
  287. port, pos, FUNC_NAME); \
  288. } while (0)
  289. #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPPORTP)
  290. #define SCM_VALIDATE_OPOUTPORT(pos, port) \
  291. SCM_MAKE_VALIDATE (pos, port, OPOUTPORTP)
  292. #define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE (pos, fluid, FLUIDP)
  293. #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE (pos, v, KEYWORDP)
  294. #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE (pos, v, STACKP)
  295. #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE (pos, v, FRAMEP)
  296. #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE (pos, v, RSTATEP)
  297. #define SCM_VALIDATE_ARRAY(pos,v) \
  298. do { \
  299. SCM_ASSERT (SCM_NIMP (v) \
  300. && !SCM_FALSEP (scm_array_p (v, SCM_UNDEFINED)), \
  301. v, pos, FUNC_NAME); \
  302. } while (0)
  303. #define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE (pos, v, VECTORP)
  304. #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
  305. do { \
  306. SCM_ASSERT ((SCM_VECTORP (v) \
  307. || (SCM_NIMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
  308. v, pos, FUNC_NAME); \
  309. } while (0)
  310. #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE (pos, v, STRUCTP)
  311. #define SCM_VALIDATE_VTABLE(pos, v) \
  312. do { \
  313. SCM_ASSERT (SCM_NIMP (v) && SCM_NFALSEP (scm_struct_vtable_p (v)), \
  314. v, pos, FUNC_NAME); \
  315. } while (0)
  316. #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
  317. do { \
  318. SCM_ASSERT (SCM_VECTORP (v) && len == SCM_LENGTH (v), v, pos, FUNC_NAME); \
  319. } while (0)
  320. #endif
  321. /*
  322. Local Variables:
  323. c-file-style: "gnu"
  324. End:
  325. */