1 /* $Id: validate.h,v 1.34 2001-06-26 10:59:34 dirk Exp $ */
2 /* Copyright (C) 1999,2000,2001 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_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
50 #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
52 #define SCM_SYSERROR_MSG(str, args, val) \
53 do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
55 #define SCM_MISC_ERROR(str, args) \
56 do { scm_misc_error (FUNC_NAME, str, args); } while (0)
58 #define SCM_WRONG_NUM_ARGS() \
59 do { scm_error_num_args_subr (FUNC_NAME); } while (0)
61 #define SCM_WRONG_TYPE_ARG(pos, obj) \
62 do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
64 #define SCM_NUM2SIZE(pos, arg) (scm_num2size (arg, pos, FUNC_NAME))
66 #define SCM_NUM2SIZE_DEF(pos, arg, def) \
67 (SCM_UNBNDP (arg) ? def : scm_num2size (arg, pos, FUNC_NAME))
69 #define SCM_NUM2PTRDIFF(pos, arg) (scm_num2ptrdiff (arg, pos, FUNC_NAME))
71 #define SCM_NUM2PTRDIFF_DEF(pos, arg, def) \
72 (SCM_UNBNDP (arg) ? def : scm_num2ptrdiff (arg, pos, FUNC_NAME))
74 #define SCM_NUM2SHORT(pos, arg) (scm_num2short (arg, pos, FUNC_NAME))
76 #define SCM_NUM2SHORT_DEF(pos, arg, def) \
77 (SCM_UNBNDP (arg) ? def : scm_num2short (arg, pos, FUNC_NAME))
79 #define SCM_NUM2USHORT(pos, arg) (scm_num2ushort (arg, pos, FUNC_NAME))
81 #define SCM_NUM2USHORT_DEF(pos, arg, def) \
82 (SCM_UNBNDP (arg) ? def : scm_num2ushort (arg, pos, FUNC_NAME))
84 #define SCM_NUM2INT(pos, arg) (scm_num2int (arg, pos, FUNC_NAME))
86 #define SCM_NUM2INT_DEF(pos, arg, def) \
87 (SCM_UNBNDP (arg) ? def : scm_num2int (arg, pos, FUNC_NAME))
89 #define SCM_NUM2UINT(pos, arg) (scm_num2uint (arg, pos, FUNC_NAME))
91 #define SCM_NUM2UINT_DEF(pos, arg, def) \
92 (SCM_UNBNDP (arg) ? def : scm_num2uint (arg, pos, FUNC_NAME))
94 #define SCM_NUM2ULONG(pos, arg) (scm_num2ulong (arg, pos, FUNC_NAME))
96 #define SCM_NUM2ULONG_DEF(pos, arg, def) \
97 (SCM_UNBNDP (arg) ? def : scm_num2ulong (arg, pos, FUNC_NAME))
99 #define SCM_NUM2LONG(pos, arg) (scm_num2long (arg, pos, FUNC_NAME))
101 #define SCM_NUM2LONG_DEF(pos, arg, def) \
102 (SCM_UNBNDP (arg) ? def : scm_num2long (arg, pos, FUNC_NAME))
104 #define SCM_NUM2LONG_LONG(pos, arg) \
105 (scm_num2long_long (arg, pos, FUNC_NAME))
107 #define SCM_NUM2LONG_LONG_DEF(pos, arg, def) \
108 (SCM_UNBNDP (arg) ? def : scm_num2long_long (arg, pos, FUNC_NAME))
110 #define SCM_NUM2ULONG_LONG(pos, arg) \
111 (scm_num2ulong_long (arg, pos, FUNC_NAME))
113 #define SCM_NUM2ULONG_LONG_DEF(pos, arg, def) \
114 (SCM_UNBNDP (arg) ? def : scm_num2ulong_long (arg, pos, FUNC_NAME))
116 #define SCM_OUT_OF_RANGE(pos, arg) \
117 do { scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
119 #define SCM_ASSERT_RANGE(pos, arg, f) \
120 do { if (!(f)) scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
122 #define SCM_MUST_MALLOC_TYPE(type) \
123 ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
125 #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
126 ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
128 #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
130 #define SCM_MAKE_VALIDATE(pos, var, pred) \
132 SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
137 #define SCM_VALIDATE_REST_ARGUMENT(x) \
139 if (SCM_DEBUG_REST_ARGUMENT) { \
140 if (scm_ilength (x) < 0) { \
141 SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
146 #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NIMP)
148 #define SCM_VALIDATE_BOOL(pos, flag) SCM_MAKE_VALIDATE(pos, flag, BOOLP)
150 #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
152 SCM_ASSERT (SCM_BOOLP (flag), flag, pos, FUNC_NAME); \
153 cvar = SCM_EQ_P (flag, SCM_BOOL_T) ? 1 : 0; \
156 #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CHARP)
158 #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
160 SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
161 cvar = SCM_CHAR (scm); \
164 #define SCM_VALIDATE_STRING(pos, str) SCM_MAKE_VALIDATE (pos, str, STRINGP)
166 #define SCM_VALIDATE_STRING_COPY(pos, str, cvar) \
168 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
169 cvar = SCM_STRING_CHARS(str); \
172 /* validate a string and optional start/end arguments which default to
173 0/string-len. this is unrelated to the old shared substring
174 support, so please do not deprecate it :) */
175 #define SCM_VALIDATE_SUBSTRING_SPEC_COPY(pos_str, str, c_str, \
176 pos_start, start, c_start,\
177 pos_end, end, c_end) \
179 SCM_VALIDATE_STRING_COPY (pos_str, str, c_str);\
180 SCM_VALIDATE_INUM_DEF_COPY (pos_start, start, 0, c_start);\
181 SCM_VALIDATE_INUM_DEF_COPY (pos_end, end, SCM_STRING_LENGTH (str), c_end);\
182 SCM_ASSERT_RANGE (pos_start, start,\
184 && (size_t) c_start <= SCM_STRING_LENGTH (str));\
185 SCM_ASSERT_RANGE (pos_end, end,\
187 && (size_t) c_end <= SCM_STRING_LENGTH (str));\
190 #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE (pos, z, REALP)
192 #define SCM_VALIDATE_NUMBER(pos, z) SCM_MAKE_VALIDATE (pos, z, NUMBERP)
194 #define SCM_VALIDATE_INUM(pos, k) SCM_MAKE_VALIDATE (pos, k, INUMP)
196 #define SCM_VALIDATE_INUM_COPY(pos, k, cvar) \
198 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
199 cvar = SCM_INUM (k); \
202 #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
204 cvar = SCM_NUM2ULONG (pos, k); \
207 #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
209 cvar = SCM_NUM2LONG (pos, k); \
212 #define SCM_VALIDATE_BIGINT(pos, k) SCM_MAKE_VALIDATE (pos, k, BIGP)
214 #define SCM_VALIDATE_INUM_MIN(pos, k, min) \
216 SCM_ASSERT (SCM_INUMP(k), k, pos, FUNC_NAME); \
217 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
220 #define SCM_VALIDATE_INUM_MIN_COPY(pos, k, min, cvar) \
222 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
223 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
224 cvar = SCM_INUM (k); \
227 #define SCM_VALIDATE_INUM_MIN_DEF_COPY(pos, k, min, default, cvar) \
229 if (SCM_UNBNDP (k)) \
230 k = SCM_MAKINUM (default); \
231 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
232 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
233 cvar = SCM_INUM (k); \
236 #define SCM_VALIDATE_INUM_DEF(pos, k, default) \
238 if (SCM_UNBNDP (k)) \
239 k = SCM_MAKINUM (default); \
240 else SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
243 #define SCM_VALIDATE_INUM_DEF_COPY(pos, k, default, cvar) \
245 if (SCM_UNBNDP (k)) \
247 k = SCM_MAKINUM (default); \
252 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
253 cvar = SCM_INUM (k); \
258 #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
259 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
260 SCM_ASSERT_RANGE(pos,k, \
261 (SCM_INUM (k) >= low && \
262 SCM_INUM (k) < high)); \
265 #define SCM_VALIDATE_INUM_RANGE_COPY(pos, k, low, high, cvar) \
267 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
268 SCM_ASSERT_RANGE (pos, k, low <= SCM_INUM (k) && SCM_INUM (k) < high); \
269 cvar = SCM_INUM (k); \
272 #define SCM_VALIDATE_NULL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULLP)
274 #define SCM_VALIDATE_CONS(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CONSP)
276 #define SCM_VALIDATE_LIST(pos, lst) \
278 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
281 #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
283 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
286 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
288 cvar = scm_ilength (lst); \
289 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
292 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
294 cvar = scm_ilength (lst); \
295 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
298 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
300 SCM_ASSERT (SCM_CONSP (alist) && SCM_CONSP (SCM_CAR (alist)), \
301 alist, pos, FUNC_NAME); \
304 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
306 SCM_ASSERT (SCM_CONSP (alist), alist, pos, FUNC_NAME); \
307 cvar = SCM_CAR (alist); \
308 SCM_ASSERT (SCM_CONSP (cvar), alist, pos, FUNC_NAME); \
311 #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
313 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
316 #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
318 #define SCM_VALIDATE_SMOB(pos, obj, type) \
320 SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
321 obj, pos, FUNC_NAME); \
324 #define SCM_VALIDATE_THREAD(pos, a) SCM_MAKE_VALIDATE (pos, a, THREADP)
326 #define SCM_VALIDATE_THUNK(pos, thunk) \
328 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
331 #define SCM_VALIDATE_SYMBOL(pos, sym) SCM_MAKE_VALIDATE (pos, sym, SYMBOLP)
333 #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE (pos, var, VARIABLEP)
335 #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE (pos, obj, MEMOIZEDP)
337 #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE (pos, obj, CLOSUREP)
339 #define SCM_VALIDATE_PROC(pos, proc) \
341 SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), proc, pos, FUNC_NAME); \
344 #define SCM_VALIDATE_NULLORCONS(pos, env) \
346 SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); \
349 #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE (pos, a, HOOKP)
351 #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE (pos, a, RGXP)
353 #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE (pos, port, DIRP)
355 #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE (pos, port, PORTP)
357 #define SCM_VALIDATE_INPUT_PORT(pos, port) \
358 SCM_MAKE_VALIDATE (pos, port, INPUT_PORT_P)
360 #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
361 SCM_MAKE_VALIDATE (pos, port, OUTPUT_PORT_P)
363 #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, FPORTP)
365 #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPFPORTP)
367 #define SCM_VALIDATE_OPINPORT(pos, port) \
368 SCM_MAKE_VALIDATE (pos, port, OPINPORTP)
370 #define SCM_VALIDATE_OPENPORT(pos,port) \
372 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
373 port, pos, FUNC_NAME); \
376 #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPPORTP)
378 #define SCM_VALIDATE_OPOUTPORT(pos, port) \
379 SCM_MAKE_VALIDATE (pos, port, OPOUTPORTP)
381 #define SCM_VALIDATE_OPOUTSTRPORT(pos, port) \
382 SCM_MAKE_VALIDATE (pos, port, OPOUTSTRPORTP)
384 #define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE (pos, fluid, FLUIDP)
386 #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE (pos, v, KEYWORDP)
388 #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE (pos, v, STACKP)
390 #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE (pos, v, FRAMEP)
392 #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE (pos, v, RSTATEP)
394 #define SCM_VALIDATE_ARRAY(pos,v) \
396 SCM_ASSERT (SCM_NIMP (v) \
397 && !SCM_FALSEP (scm_array_p (v, SCM_UNDEFINED)), \
398 v, pos, FUNC_NAME); \
401 #define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE (pos, v, VECTORP)
403 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
405 SCM_ASSERT ((SCM_VECTORP (v) \
406 || (SCM_NIMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
407 v, pos, FUNC_NAME); \
410 #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE (pos, v, STRUCTP)
412 #define SCM_VALIDATE_VTABLE(pos, v) \
414 SCM_ASSERT (SCM_NIMP (v) && SCM_NFALSEP (scm_struct_vtable_p (v)), \
415 v, pos, FUNC_NAME); \
418 #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
420 SCM_ASSERT (SCM_VECTORP (v) && len == SCM_VECTOR_LENGTH (v), v, pos, FUNC_NAME); \
425 #if (SCM_DEBUG_DEPRECATED == 0)
427 #define SCM_FUNC_NAME (scm_makfrom0str (FUNC_NAME))
429 #define SCM_WTA(pos, scm) \
430 do { scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
432 #define RETURN_SCM_WTA(pos, scm) \
433 do { return scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
435 #define SCM_VALIDATE_NUMBER_COPY(pos, z, cvar) \
438 cvar = (double) SCM_INUM (z); \
439 else if (SCM_REALP (z)) \
440 cvar = SCM_REAL_VALUE (z); \
441 else if (SCM_BIGP (z)) \
442 cvar = scm_i_big2dbl (z); \
446 SCM_WRONG_TYPE_ARG (pos, z); \
450 #define SCM_VALIDATE_NUMBER_DEF_COPY(pos, number, def, cvar) \
452 if (SCM_UNBNDP (number)) \
455 SCM_VALIDATE_NUMBER_COPY(pos, number, cvar); \
458 #define SCM_VALIDATE_STRINGORSUBSTR SCM_VALIDATE_STRING
460 #define SCM_VALIDATE_ROSTRING(pos, str) SCM_MAKE_VALIDATE (pos, str, ROSTRINGP)
462 #define SCM_VALIDATE_ROSTRING_COPY(pos, str, cvar) \
464 SCM_ASSERT (SCM_ROSTRINGP (str), str, pos, FUNC_NAME); \
465 cvar = SCM_ROCHARS (str); \
468 #define SCM_VALIDATE_NULLORROSTRING_COPY(pos, str, cvar) \
470 SCM_ASSERT (SCM_FALSEP (str) || SCM_ROSTRINGP (str), \
471 str, pos, FUNC_NAME); \
472 if (SCM_FALSEP(str)) \
475 cvar = SCM_ROCHARS(str); \
478 #define SCM_VALIDATE_RWSTRING(pos, str) \
480 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
481 if (!SCM_RWSTRINGP (str)) \
482 scm_misc_error (FUNC_NAME, "argument is a read-only string", str); \
485 #define SCM_VALIDATE_OPDIR(pos, port) SCM_MAKE_VALIDATE (pos, port, OPDIRP)
487 #endif /* SCM_DEBUG_DEPRECATED == 0 */