* benchmark-guile.in: Copied from check-guile.in and adapted for
[bpt/guile.git] / libguile / validate.h
1 /* classes: h_files */
2
3 #ifndef SCM_VALIDATE_H
4 #define SCM_VALIDATE_H
5
6 /* Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this software; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
22 *
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
25 *
26 * The exception is that, if you link the GUILE library with other files
27 * to produce an executable, this does not by itself cause the
28 * resulting executable to be covered by the GNU General Public License.
29 * Your use of that executable is in no way restricted on account of
30 * linking the GUILE library code into it.
31 *
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
34 *
35 * This exception applies only to the code released by the
36 * Free Software Foundation under the name GUILE. If you copy
37 * code from other Free Software Foundation releases into a copy of
38 * GUILE, as the General Public License permits, the exception does
39 * not apply to the code that you add in this way. To avoid misleading
40 * anyone as to the status of such modified files, you must delete
41 * this exception notice from them.
42 *
43 * If you write modifications of your own for GUILE, it is your choice
44 * whether to permit this exception to apply to your modifications.
45 * If you do not wish that, delete this exception notice. */
46
47 /* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
48
49 \f
50
51 #define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
52
53 #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
54
55 #define SCM_SYSERROR_MSG(str, args, val) \
56 do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
57
58 #define SCM_MISC_ERROR(str, args) \
59 do { scm_misc_error (FUNC_NAME, str, args); } while (0)
60
61 #define SCM_WRONG_NUM_ARGS() \
62 do { scm_error_num_args_subr (FUNC_NAME); } while (0)
63
64 #define SCM_WRONG_TYPE_ARG(pos, obj) \
65 do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
66
67 #define SCM_NUM2SIZE(pos, arg) (scm_num2size (arg, pos, FUNC_NAME))
68
69 #define SCM_NUM2SIZE_DEF(pos, arg, def) \
70 (SCM_UNBNDP (arg) ? def : scm_num2size (arg, pos, FUNC_NAME))
71
72 #define SCM_NUM2PTRDIFF(pos, arg) (scm_num2ptrdiff (arg, pos, FUNC_NAME))
73
74 #define SCM_NUM2PTRDIFF_DEF(pos, arg, def) \
75 (SCM_UNBNDP (arg) ? def : scm_num2ptrdiff (arg, pos, FUNC_NAME))
76
77 #define SCM_NUM2SHORT(pos, arg) (scm_num2short (arg, pos, FUNC_NAME))
78
79 #define SCM_NUM2SHORT_DEF(pos, arg, def) \
80 (SCM_UNBNDP (arg) ? def : scm_num2short (arg, pos, FUNC_NAME))
81
82 #define SCM_NUM2USHORT(pos, arg) (scm_num2ushort (arg, pos, FUNC_NAME))
83
84 #define SCM_NUM2USHORT_DEF(pos, arg, def) \
85 (SCM_UNBNDP (arg) ? def : scm_num2ushort (arg, pos, FUNC_NAME))
86
87 #define SCM_NUM2INT(pos, arg) (scm_num2int (arg, pos, FUNC_NAME))
88
89 #define SCM_NUM2INT_DEF(pos, arg, def) \
90 (SCM_UNBNDP (arg) ? def : scm_num2int (arg, pos, FUNC_NAME))
91
92 #define SCM_NUM2UINT(pos, arg) (scm_num2uint (arg, pos, FUNC_NAME))
93
94 #define SCM_NUM2UINT_DEF(pos, arg, def) \
95 (SCM_UNBNDP (arg) ? def : scm_num2uint (arg, pos, FUNC_NAME))
96
97 #define SCM_NUM2ULONG(pos, arg) (scm_num2ulong (arg, pos, FUNC_NAME))
98
99 #define SCM_NUM2ULONG_DEF(pos, arg, def) \
100 (SCM_UNBNDP (arg) ? def : scm_num2ulong (arg, pos, FUNC_NAME))
101
102 #define SCM_NUM2LONG(pos, arg) (scm_num2long (arg, pos, FUNC_NAME))
103
104 #define SCM_NUM2LONG_DEF(pos, arg, def) \
105 (SCM_UNBNDP (arg) ? def : scm_num2long (arg, pos, FUNC_NAME))
106
107 #define SCM_NUM2LONG_LONG(pos, arg) \
108 (scm_num2long_long (arg, pos, FUNC_NAME))
109
110 #define SCM_NUM2LONG_LONG_DEF(pos, arg, def) \
111 (SCM_UNBNDP (arg) ? def : scm_num2long_long (arg, pos, FUNC_NAME))
112
113 #define SCM_NUM2ULONG_LONG(pos, arg) \
114 (scm_num2ulong_long (arg, pos, FUNC_NAME))
115
116 #define SCM_NUM2ULONG_LONG_DEF(pos, arg, def) \
117 (SCM_UNBNDP (arg) ? def : scm_num2ulong_long (arg, pos, FUNC_NAME))
118
119 #define SCM_NUM2FLOAT(pos, arg) \
120 (scm_num2float (arg, pos, FUNC_NAME))
121
122 #define SCM_NUM2DOUBLE(pos, arg) \
123 (scm_num2double (arg, pos, FUNC_NAME))
124
125 #define SCM_OUT_OF_RANGE(pos, arg) \
126 do { scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
127
128 #define SCM_ASSERT_RANGE(pos, arg, f) \
129 do { if (!(f)) scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
130
131 #define SCM_MUST_MALLOC_TYPE(type) \
132 ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
133
134 #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
135 ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
136
137 #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
138
139 #define SCM_MAKE_VALIDATE(pos, var, pred) \
140 do { \
141 SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
142 } while (0)
143
144 \f
145
146 #define SCM_VALIDATE_REST_ARGUMENT(x) \
147 do { \
148 if (SCM_DEBUG_REST_ARGUMENT) { \
149 if (scm_ilength (x) < 0) { \
150 SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
151 } \
152 } \
153 } while (0)
154
155 #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NIMP)
156
157 #define SCM_VALIDATE_BOOL(pos, flag) SCM_MAKE_VALIDATE(pos, flag, BOOLP)
158
159 #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
160 do { \
161 SCM_ASSERT (SCM_BOOLP (flag), flag, pos, FUNC_NAME); \
162 cvar = SCM_EQ_P (flag, SCM_BOOL_T) ? 1 : 0; \
163 } while (0)
164
165 #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CHARP)
166
167 #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
168 do { \
169 SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
170 cvar = SCM_CHAR (scm); \
171 } while (0)
172
173 #define SCM_VALIDATE_STRING(pos, str) SCM_MAKE_VALIDATE (pos, str, STRINGP)
174
175 #define SCM_VALIDATE_STRING_COPY(pos, str, cvar) \
176 do { \
177 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
178 cvar = SCM_STRING_CHARS(str); \
179 } while (0)
180
181 /* validate a string and optional start/end arguments which default to
182 0/string-len. this is unrelated to the old shared substring
183 support, so please do not deprecate it :) */
184 #define SCM_VALIDATE_SUBSTRING_SPEC_COPY(pos_str, str, c_str, \
185 pos_start, start, c_start,\
186 pos_end, end, c_end) \
187 do {\
188 SCM_VALIDATE_STRING_COPY (pos_str, str, c_str);\
189 SCM_VALIDATE_INUM_DEF_COPY (pos_start, start, 0, c_start);\
190 SCM_VALIDATE_INUM_DEF_COPY (pos_end, end, SCM_STRING_LENGTH (str), c_end);\
191 SCM_ASSERT_RANGE (pos_start, start,\
192 0 <= c_start \
193 && (size_t) c_start <= SCM_STRING_LENGTH (str));\
194 SCM_ASSERT_RANGE (pos_end, end,\
195 c_start <= c_end \
196 && (size_t) c_end <= SCM_STRING_LENGTH (str));\
197 } while (0)
198
199 #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE (pos, z, REALP)
200
201 #define SCM_VALIDATE_NUMBER(pos, z) SCM_MAKE_VALIDATE (pos, z, NUMBERP)
202
203 #define SCM_VALIDATE_INUM(pos, k) SCM_MAKE_VALIDATE (pos, k, INUMP)
204
205 #define SCM_VALIDATE_INUM_COPY(pos, k, cvar) \
206 do { \
207 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
208 cvar = SCM_INUM (k); \
209 } while (0)
210
211 #define SCM_VALIDATE_USHORT_COPY(pos, k, cvar) \
212 do { \
213 cvar = SCM_NUM2USHORT (pos, k); \
214 } while (0)
215
216 #define SCM_VALIDATE_SHORT_COPY(pos, k, cvar) \
217 do { \
218 cvar = SCM_NUM2SHORT (pos, k); \
219 } while (0)
220
221 #define SCM_VALIDATE_UINT_COPY(pos, k, cvar) \
222 do { \
223 cvar = SCM_NUM2UINT (pos, k); \
224 } while (0)
225
226 #define SCM_VALIDATE_INT_COPY(pos, k, cvar) \
227 do { \
228 cvar = SCM_NUM2INT (pos, k); \
229 } while (0)
230
231 #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
232 do { \
233 cvar = SCM_NUM2ULONG (pos, k); \
234 } while (0)
235
236 #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
237 do { \
238 cvar = SCM_NUM2LONG (pos, k); \
239 } while (0)
240
241 #define SCM_VALIDATE_FLOAT_COPY(pos, k, cvar) \
242 do { \
243 cvar = SCM_NUM2FLOAT (pos, k); \
244 } while (0)
245
246 #define SCM_VALIDATE_DOUBLE_COPY(pos, k, cvar) \
247 do { \
248 cvar = SCM_NUM2DOUBLE (pos, k); \
249 } while (0)
250
251 #define SCM_VALIDATE_BIGINT(pos, k) SCM_MAKE_VALIDATE (pos, k, BIGP)
252
253 #define SCM_VALIDATE_INUM_MIN(pos, k, min) \
254 do { \
255 SCM_ASSERT (SCM_INUMP(k), k, pos, FUNC_NAME); \
256 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
257 } while (0)
258
259 #define SCM_VALIDATE_INUM_MIN_COPY(pos, k, min, cvar) \
260 do { \
261 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
262 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
263 cvar = SCM_INUM (k); \
264 } while (0)
265
266 #define SCM_VALIDATE_INUM_MIN_DEF_COPY(pos, k, min, default, cvar) \
267 do { \
268 if (SCM_UNBNDP (k)) \
269 k = SCM_MAKINUM (default); \
270 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
271 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
272 cvar = SCM_INUM (k); \
273 } while (0)
274
275 #define SCM_VALIDATE_INUM_DEF(pos, k, default) \
276 do { \
277 if (SCM_UNBNDP (k)) \
278 k = SCM_MAKINUM (default); \
279 else SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
280 } while (0)
281
282 #define SCM_VALIDATE_INUM_DEF_COPY(pos, k, default, cvar) \
283 do { \
284 if (SCM_UNBNDP (k)) \
285 { \
286 k = SCM_MAKINUM (default); \
287 cvar = default; \
288 } \
289 else \
290 { \
291 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
292 cvar = SCM_INUM (k); \
293 } \
294 } while (0)
295
296 #define SCM_VALIDATE_DOUBLE_DEF_COPY(pos, k, default, cvar) \
297 do { \
298 if (SCM_UNBNDP (k)) \
299 { \
300 k = scm_make_real (default); \
301 cvar = default; \
302 } \
303 else \
304 { \
305 cvar = SCM_NUM2DOUBLE (pos, k); \
306 } \
307 } while (0)
308
309 /* [low,high) */
310 #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
311 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
312 SCM_ASSERT_RANGE(pos,k, \
313 (SCM_INUM (k) >= low && \
314 SCM_INUM (k) < high)); \
315 } while (0)
316
317 #define SCM_VALIDATE_INUM_RANGE_COPY(pos, k, low, high, cvar) \
318 do { \
319 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
320 SCM_ASSERT_RANGE (pos, k, low <= SCM_INUM (k) && SCM_INUM (k) < high); \
321 cvar = SCM_INUM (k); \
322 } while (0)
323
324 #define SCM_VALIDATE_NULL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULLP)
325
326 #define SCM_VALIDATE_NULL_OR_NIL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULL_OR_NIL_P)
327
328 #define SCM_VALIDATE_CONS(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CONSP)
329
330 #define SCM_VALIDATE_LIST(pos, lst) \
331 do { \
332 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
333 } while (0)
334
335 #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
336 do { \
337 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
338 } while (0)
339
340 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
341 do { \
342 cvar = scm_ilength (lst); \
343 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
344 } while (0)
345
346 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
347 do { \
348 cvar = scm_ilength (lst); \
349 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
350 } while (0)
351
352 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
353 do { \
354 SCM_ASSERT (SCM_CONSP (alist) && SCM_CONSP (SCM_CAR (alist)), \
355 alist, pos, FUNC_NAME); \
356 } while (0)
357
358 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
359 do { \
360 SCM_ASSERT (SCM_CONSP (alist), alist, pos, FUNC_NAME); \
361 cvar = SCM_CAR (alist); \
362 SCM_ASSERT (SCM_CONSP (cvar), alist, pos, FUNC_NAME); \
363 } while (0)
364
365 #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
366 do { \
367 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
368 } while (0)
369
370 #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
371
372 #define SCM_VALIDATE_SMOB(pos, obj, type) \
373 do { \
374 SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
375 obj, pos, FUNC_NAME); \
376 } while (0)
377
378 #define SCM_VALIDATE_THREAD(pos, a) SCM_MAKE_VALIDATE (pos, a, THREADP)
379
380 #define SCM_VALIDATE_THUNK(pos, thunk) \
381 do { \
382 SCM_ASSERT (!SCM_FALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
383 } while (0)
384
385 #define SCM_VALIDATE_SYMBOL(pos, sym) SCM_MAKE_VALIDATE (pos, sym, SYMBOLP)
386
387 #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE (pos, var, VARIABLEP)
388
389 #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE (pos, obj, MEMOIZEDP)
390
391 #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE (pos, obj, CLOSUREP)
392
393 #define SCM_VALIDATE_PROC(pos, proc) \
394 do { \
395 SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), proc, pos, FUNC_NAME); \
396 } while (0)
397
398 #define SCM_VALIDATE_NULLORCONS(pos, env) \
399 do { \
400 SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); \
401 } while (0)
402
403 #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE (pos, a, HOOKP)
404
405 #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE (pos, a, RGXP)
406
407 #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE (pos, port, DIRP)
408
409 #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE (pos, port, PORTP)
410
411 #define SCM_VALIDATE_INPUT_PORT(pos, port) \
412 SCM_MAKE_VALIDATE (pos, port, INPUT_PORT_P)
413
414 #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
415 SCM_MAKE_VALIDATE (pos, port, OUTPUT_PORT_P)
416
417 #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, FPORTP)
418
419 #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPFPORTP)
420
421 #define SCM_VALIDATE_OPINPORT(pos, port) \
422 SCM_MAKE_VALIDATE (pos, port, OPINPORTP)
423
424 #define SCM_VALIDATE_OPENPORT(pos,port) \
425 do { \
426 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
427 port, pos, FUNC_NAME); \
428 } while (0)
429
430 #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPPORTP)
431
432 #define SCM_VALIDATE_OPOUTPORT(pos, port) \
433 SCM_MAKE_VALIDATE (pos, port, OPOUTPORTP)
434
435 #define SCM_VALIDATE_OPOUTSTRPORT(pos, port) \
436 SCM_MAKE_VALIDATE (pos, port, OPOUTSTRPORTP)
437
438 #define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE (pos, fluid, FLUIDP)
439
440 #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE (pos, v, KEYWORDP)
441
442 #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE (pos, v, STACKP)
443
444 #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE (pos, v, FRAMEP)
445
446 #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE (pos, v, RSTATEP)
447
448 #define SCM_VALIDATE_ARRAY(pos,v) \
449 do { \
450 SCM_ASSERT (!SCM_IMP (v) \
451 && !SCM_FALSEP (scm_array_p (v, SCM_UNDEFINED)), \
452 v, pos, FUNC_NAME); \
453 } while (0)
454
455 #define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE (pos, v, VECTORP)
456
457 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
458 do { \
459 SCM_ASSERT ((SCM_VECTORP (v) \
460 || (!SCM_IMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
461 v, pos, FUNC_NAME); \
462 } while (0)
463
464 #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE (pos, v, STRUCTP)
465
466 #define SCM_VALIDATE_VTABLE(pos, v) \
467 do { \
468 SCM_ASSERT (!SCM_IMP (v) && !SCM_FALSEP (scm_struct_vtable_p (v)), \
469 v, pos, FUNC_NAME); \
470 } while (0)
471
472 #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
473 do { \
474 SCM_ASSERT (SCM_VECTORP (v) && len == SCM_VECTOR_LENGTH (v), v, pos, FUNC_NAME); \
475 } while (0)
476
477 #endif /* SCM_VALIDATE_H */
478
479 /*
480 Local Variables:
481 c-file-style: "gnu"
482 End:
483 */