* validate.h (SCM_NUM2FLOAT, SCM_NUM2DOUBLE,
[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 /* [low,high) */
297 #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
298 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
299 SCM_ASSERT_RANGE(pos,k, \
300 (SCM_INUM (k) >= low && \
301 SCM_INUM (k) < high)); \
302 } while (0)
303
304 #define SCM_VALIDATE_INUM_RANGE_COPY(pos, k, low, high, cvar) \
305 do { \
306 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
307 SCM_ASSERT_RANGE (pos, k, low <= SCM_INUM (k) && SCM_INUM (k) < high); \
308 cvar = SCM_INUM (k); \
309 } while (0)
310
311 #define SCM_VALIDATE_NULL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULLP)
312
313 #define SCM_VALIDATE_CONS(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CONSP)
314
315 #define SCM_VALIDATE_LIST(pos, lst) \
316 do { \
317 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
318 } while (0)
319
320 #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
321 do { \
322 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
323 } while (0)
324
325 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
326 do { \
327 cvar = scm_ilength (lst); \
328 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
329 } while (0)
330
331 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
332 do { \
333 cvar = scm_ilength (lst); \
334 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
335 } while (0)
336
337 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
338 do { \
339 SCM_ASSERT (SCM_CONSP (alist) && SCM_CONSP (SCM_CAR (alist)), \
340 alist, pos, FUNC_NAME); \
341 } while (0)
342
343 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
344 do { \
345 SCM_ASSERT (SCM_CONSP (alist), alist, pos, FUNC_NAME); \
346 cvar = SCM_CAR (alist); \
347 SCM_ASSERT (SCM_CONSP (cvar), alist, pos, FUNC_NAME); \
348 } while (0)
349
350 #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
351 do { \
352 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
353 } while (0)
354
355 #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
356
357 #define SCM_VALIDATE_SMOB(pos, obj, type) \
358 do { \
359 SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
360 obj, pos, FUNC_NAME); \
361 } while (0)
362
363 #define SCM_VALIDATE_THREAD(pos, a) SCM_MAKE_VALIDATE (pos, a, THREADP)
364
365 #define SCM_VALIDATE_THUNK(pos, thunk) \
366 do { \
367 SCM_ASSERT (!SCM_FALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
368 } while (0)
369
370 #define SCM_VALIDATE_SYMBOL(pos, sym) SCM_MAKE_VALIDATE (pos, sym, SYMBOLP)
371
372 #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE (pos, var, VARIABLEP)
373
374 #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE (pos, obj, MEMOIZEDP)
375
376 #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE (pos, obj, CLOSUREP)
377
378 #define SCM_VALIDATE_PROC(pos, proc) \
379 do { \
380 SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), proc, pos, FUNC_NAME); \
381 } while (0)
382
383 #define SCM_VALIDATE_NULLORCONS(pos, env) \
384 do { \
385 SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); \
386 } while (0)
387
388 #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE (pos, a, HOOKP)
389
390 #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE (pos, a, RGXP)
391
392 #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE (pos, port, DIRP)
393
394 #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE (pos, port, PORTP)
395
396 #define SCM_VALIDATE_INPUT_PORT(pos, port) \
397 SCM_MAKE_VALIDATE (pos, port, INPUT_PORT_P)
398
399 #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
400 SCM_MAKE_VALIDATE (pos, port, OUTPUT_PORT_P)
401
402 #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, FPORTP)
403
404 #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPFPORTP)
405
406 #define SCM_VALIDATE_OPINPORT(pos, port) \
407 SCM_MAKE_VALIDATE (pos, port, OPINPORTP)
408
409 #define SCM_VALIDATE_OPENPORT(pos,port) \
410 do { \
411 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
412 port, pos, FUNC_NAME); \
413 } while (0)
414
415 #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPPORTP)
416
417 #define SCM_VALIDATE_OPOUTPORT(pos, port) \
418 SCM_MAKE_VALIDATE (pos, port, OPOUTPORTP)
419
420 #define SCM_VALIDATE_OPOUTSTRPORT(pos, port) \
421 SCM_MAKE_VALIDATE (pos, port, OPOUTSTRPORTP)
422
423 #define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE (pos, fluid, FLUIDP)
424
425 #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE (pos, v, KEYWORDP)
426
427 #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE (pos, v, STACKP)
428
429 #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE (pos, v, FRAMEP)
430
431 #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE (pos, v, RSTATEP)
432
433 #define SCM_VALIDATE_ARRAY(pos,v) \
434 do { \
435 SCM_ASSERT (!SCM_IMP (v) \
436 && !SCM_FALSEP (scm_array_p (v, SCM_UNDEFINED)), \
437 v, pos, FUNC_NAME); \
438 } while (0)
439
440 #define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE (pos, v, VECTORP)
441
442 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
443 do { \
444 SCM_ASSERT ((SCM_VECTORP (v) \
445 || (!SCM_IMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
446 v, pos, FUNC_NAME); \
447 } while (0)
448
449 #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE (pos, v, STRUCTP)
450
451 #define SCM_VALIDATE_VTABLE(pos, v) \
452 do { \
453 SCM_ASSERT (!SCM_IMP (v) && !SCM_FALSEP (scm_struct_vtable_p (v)), \
454 v, pos, FUNC_NAME); \
455 } while (0)
456
457 #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
458 do { \
459 SCM_ASSERT (SCM_VECTORP (v) && len == SCM_VECTOR_LENGTH (v), v, pos, FUNC_NAME); \
460 } while (0)
461
462 #endif /* SCM_VALIDATE_H */
463
464 /*
465 Local Variables:
466 c-file-style: "gnu"
467 End:
468 */