1 /* $Id: validate.h,v 1.22 2000-12-16 20:25:08 kei Exp $ */
2 /* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
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)
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.
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
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
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.
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
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.
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. */
43 /* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
45 #ifndef SCM_VALIDATE_H__
46 #define SCM_VALIDATE_H__
48 #define SCM_FUNC_NAME (scm_makfrom0str (FUNC_NAME))
50 #define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
52 #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
54 #define SCM_SYSERROR_MSG(str, args, val) \
55 do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
57 #define SCM_WTA(pos, scm) \
58 do { scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
60 #define RETURN_SCM_WTA(pos, scm) \
61 do { return scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
63 #define SCM_MISC_ERROR(str, args) \
64 do { scm_misc_error (FUNC_NAME, str, args); } while (0)
66 #define SCM_WRONG_NUM_ARGS() \
67 do { scm_wrong_num_args (scm_makfrom0str (FUNC_NAME)); } while (0)
69 #define SCM_WRONG_TYPE_ARG(pos, obj) \
70 do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
72 #define SCM_NUM2ULONG(pos, arg) (scm_num2ulong (arg, (char *) pos, FUNC_NAME))
74 #define SCM_NUM2LONG(pos, arg) (scm_num2long (arg, (char *) pos, FUNC_NAME))
76 #define SCM_NUM2LONG_DEF(pos, arg, def) \
77 (SCM_UNBNDP (arg) ? def : scm_num2long (arg, (char *) pos, FUNC_NAME))
79 #define SCM_NUM2LONG_LONG(pos, arg) \
80 (scm_num2long_long (arg, (char *) pos, FUNC_NAME))
82 #define SCM_OUT_OF_RANGE(pos, arg) \
83 do { scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
85 #define SCM_ASSERT_RANGE(pos, arg, f) \
86 do { if (!(f)) scm_out_of_range (FUNC_NAME, arg); } while (0)
88 #define SCM_MUST_MALLOC_TYPE(type) \
89 ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
91 #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
92 ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
94 #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
96 #define SCM_MAKE_VALIDATE(pos, var, pred) \
98 SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
103 #define SCM_VALIDATE_REST_ARGUMENT(x) \
105 if (SCM_DEBUG_REST_ARGUMENT) { \
106 if (scm_ilength (x) < 0) { \
107 SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
112 #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NIMP)
114 #define SCM_VALIDATE_BOOL(pos, flag) SCM_MAKE_VALIDATE(pos, flag, BOOLP)
116 #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
118 SCM_ASSERT (SCM_BOOLP (flag), flag, pos, FUNC_NAME); \
119 cvar = SCM_EQ_P (flag, SCM_BOOL_T) ? 1 : 0; \
122 #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CHARP)
124 #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
126 SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
127 cvar = SCM_CHAR (scm); \
130 #define SCM_VALIDATE_STRING(pos, str) SCM_MAKE_VALIDATE (pos, str, STRINGP)
132 #define SCM_VALIDATE_STRING_COPY(pos, str, cvar) \
134 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
135 cvar = SCM_STRING_CHARS(str); \
138 #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE (pos, z, REALP)
140 #define SCM_VALIDATE_NUMBER_COPY(pos, z, cvar) \
143 cvar = (double) SCM_INUM (z); \
144 else if (SCM_REALP (z)) \
145 cvar = SCM_REAL_VALUE (z); \
146 else if (SCM_BIGP (z)) \
147 cvar = scm_big2dbl (z); \
155 #define SCM_VALIDATE_NUMBER_DEF_COPY(pos, number, def, cvar) \
157 if (SCM_UNBNDP (number)) \
160 SCM_VALIDATE_NUMBER_COPY(pos, number, cvar); \
163 #define SCM_VALIDATE_INUM(pos, k) SCM_MAKE_VALIDATE (pos, k, INUMP)
165 #define SCM_VALIDATE_INUM_COPY(pos, k, cvar) \
167 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
168 cvar = SCM_INUM (k); \
171 #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
173 cvar = SCM_NUM2ULONG (pos, k); \
176 #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
178 cvar = SCM_NUM2LONG(pos, k); \
181 #define SCM_VALIDATE_BIGINT(pos, k) SCM_MAKE_VALIDATE (pos, k, BIGP)
183 #define SCM_VALIDATE_INUM_MIN(pos, k, min) \
185 SCM_ASSERT (SCM_INUMP(k), k, pos, FUNC_NAME); \
186 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
189 #define SCM_VALIDATE_INUM_MIN_COPY(pos, k, min, cvar) \
191 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
192 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
193 cvar = SCM_INUM (k); \
196 #define SCM_VALIDATE_INUM_MIN_DEF_COPY(pos, k, min, default, cvar) \
198 if (SCM_UNBNDP (k)) \
199 k = SCM_MAKINUM (default); \
200 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
201 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
202 cvar = SCM_INUM (k); \
205 #define SCM_VALIDATE_INUM_DEF(pos, k, default) \
207 if (SCM_UNBNDP (k)) \
208 k = SCM_MAKINUM (default); \
209 else SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
212 #define SCM_VALIDATE_INUM_DEF_COPY(pos, k, default, cvar) \
214 if (SCM_UNBNDP (k)) \
216 k = SCM_MAKINUM (default); \
221 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
222 cvar = SCM_INUM (k); \
227 #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
228 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
229 SCM_ASSERT_RANGE(pos,k, \
230 (SCM_INUM (k) >= low && \
231 SCM_INUM (k) < high)); \
234 #define SCM_VALIDATE_INUM_RANGE_COPY(pos, k, low, high, cvar) \
236 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
237 SCM_ASSERT_RANGE (pos, k, low <= SCM_INUM (k) && SCM_INUM (k) < high); \
238 cvar = SCM_INUM (k); \
241 #define SCM_VALIDATE_NULL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULLP)
243 #define SCM_VALIDATE_CONS(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CONSP)
245 #define SCM_VALIDATE_LIST(pos, lst) \
247 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
250 #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
252 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
255 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
257 cvar = scm_ilength (lst); \
258 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
261 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
263 cvar = scm_ilength (lst); \
264 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
267 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
269 SCM_ASSERT (SCM_CONSP (alist) && SCM_CONSP (SCM_CAR (alist)), \
270 alist, pos, FUNC_NAME); \
273 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
275 SCM_ASSERT (SCM_CONSP (alist), alist, pos, FUNC_NAME); \
276 cvar = SCM_CAR (alist); \
277 SCM_ASSERT (SCM_CONSP (cvar), alist, pos, FUNC_NAME); \
280 #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
282 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
285 #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
287 #define SCM_VALIDATE_SMOB(pos, obj, type) \
289 SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
290 obj, pos, FUNC_NAME); \
293 #define SCM_VALIDATE_THREAD(pos, a) SCM_MAKE_VALIDATE (pos, a, THREADP)
295 #define SCM_VALIDATE_THUNK(pos, thunk) \
297 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
300 #define SCM_VALIDATE_SYMBOL(pos, sym) SCM_MAKE_VALIDATE (pos, sym, SYMBOLP)
302 #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE (pos, var, VARIABLEP)
304 #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE (pos, obj, MEMOIZEDP)
306 #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE (pos, obj, CLOSUREP)
308 #define SCM_VALIDATE_PROC(pos, proc) \
310 SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), proc, pos, FUNC_NAME); \
313 #define SCM_VALIDATE_NULLORCONS(pos, env) \
315 SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); \
318 #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE (pos, a, HOOKP)
320 #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE (pos, a, RGXP)
322 #define SCM_VALIDATE_OPDIR(pos, port) SCM_MAKE_VALIDATE (pos, port, OPDIRP)
324 #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE (pos, port, DIRP)
326 #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE (pos, port, PORTP)
328 #define SCM_VALIDATE_INPUT_PORT(pos, port) \
329 SCM_MAKE_VALIDATE (pos, port, INPUT_PORT_P)
331 #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
332 SCM_MAKE_VALIDATE (pos, port, OUTPUT_PORT_P)
334 #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, FPORTP)
336 #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPFPORTP)
338 #define SCM_VALIDATE_OPINPORT(pos, port) \
339 SCM_MAKE_VALIDATE (pos, port, OPINPORTP)
341 #define SCM_VALIDATE_OPENPORT(pos,port) \
343 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
344 port, pos, FUNC_NAME); \
347 #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPPORTP)
349 #define SCM_VALIDATE_OPOUTPORT(pos, port) \
350 SCM_MAKE_VALIDATE (pos, port, OPOUTPORTP)
352 #define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE (pos, fluid, FLUIDP)
354 #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE (pos, v, KEYWORDP)
356 #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE (pos, v, STACKP)
358 #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE (pos, v, FRAMEP)
360 #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE (pos, v, RSTATEP)
362 #define SCM_VALIDATE_ARRAY(pos,v) \
364 SCM_ASSERT (SCM_NIMP (v) \
365 && !SCM_FALSEP (scm_array_p (v, SCM_UNDEFINED)), \
366 v, pos, FUNC_NAME); \
369 #define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE (pos, v, VECTORP)
371 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
373 SCM_ASSERT ((SCM_VECTORP (v) \
374 || (SCM_NIMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
375 v, pos, FUNC_NAME); \
378 #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE (pos, v, STRUCTP)
380 #define SCM_VALIDATE_VTABLE(pos, v) \
382 SCM_ASSERT (SCM_NIMP (v) && SCM_NFALSEP (scm_struct_vtable_p (v)), \
383 v, pos, FUNC_NAME); \
386 #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
388 SCM_ASSERT (SCM_VECTORP (v) && len == SCM_VECTOR_LENGTH (v), v, pos, FUNC_NAME); \
393 #if (SCM_DEBUG_DEPRECATED == 0)
395 #define SCM_VALIDATE_STRINGORSUBSTR SCM_VALIDATE_STRING
397 #define SCM_VALIDATE_ROSTRING(pos, str) SCM_MAKE_VALIDATE (pos, str, ROSTRINGP)
399 #define SCM_VALIDATE_ROSTRING_COPY(pos, str, cvar) \
401 SCM_ASSERT (SCM_ROSTRINGP (str), str, pos, FUNC_NAME); \
402 cvar = SCM_ROCHARS (str); \
405 #define SCM_VALIDATE_NULLORROSTRING_COPY(pos, str, cvar) \
407 SCM_ASSERT (SCM_FALSEP (str) || SCM_ROSTRINGP (str), \
408 str, pos, FUNC_NAME); \
409 if (SCM_FALSEP(str)) \
412 cvar = SCM_ROCHARS(str); \
415 #define SCM_VALIDATE_RWSTRING(pos, str) \
417 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
418 if (!SCM_RWSTRINGP (str)) \
419 scm_misc_error (FUNC_NAME, "argument is a read-only string", str); \
422 #endif /* SCM_DEBUG_DEPRECATED == 0 */