* Removed SCM_TRUE_P since it may get confused with !SCM_FALSEP.
[bpt/guile.git] / libguile / validate.h
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
43 /* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
44
45 #ifndef SCM_VALIDATE_H__
46 #define SCM_VALIDATE_H__
47
48 #define SCM_FUNC_NAME (scm_makfrom0str (FUNC_NAME))
49
50 #define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
51
52 #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
53
54 #define SCM_SYSERROR_MSG(str, args, val) \
55 do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
56
57 #define SCM_COERCE_ROSTRING(pos, scm) \
58 do { scm = scm_coerce_rostring (scm, FUNC_NAME, pos); } while (0)
59
60 #define SCM_WTA(pos, scm) \
61 do { scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
62
63 #define RETURN_SCM_WTA(pos, scm) \
64 do { return scm_wta (scm, (char *) pos, FUNC_NAME); } while (0)
65
66 #define SCM_MISC_ERROR(str, args) \
67 do { scm_misc_error (FUNC_NAME, str, args); } while (0)
68
69 #define SCM_WRONG_TYPE_ARG(pos, obj) \
70 do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
71
72 #define SCM_NUM2ULONG(pos, arg) (scm_num2ulong (arg, (char *) pos, FUNC_NAME))
73
74 #define SCM_NUM2LONG(pos, arg) (scm_num2long (arg, (char *) pos, FUNC_NAME))
75
76 #define SCM_NUM2LONG_DEF(pos, arg, def) \
77 (SCM_UNBNDP (arg) ? def : scm_num2long (arg, (char *) pos, FUNC_NAME))
78
79 #define SCM_NUM2LONG_LONG(pos, arg) \
80 (scm_num2long_long (arg, (char *) pos, FUNC_NAME))
81
82 #define SCM_OUT_OF_RANGE(pos, arg) \
83 do { scm_out_of_range_pos (FUNC_NAME, arg, SCM_MAKINUM (pos)); } while (0)
84
85 #define SCM_ASSERT_RANGE(pos, arg, f) \
86 do { SCM_ASSERT (f, arg, SCM_OUTOFRANGE, FUNC_NAME); } while (0)
87
88 #define SCM_MUST_MALLOC_TYPE(type) \
89 ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
90
91 #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
92 ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
93
94 #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
95
96 #define SCM_MAKE_VALIDATE(pos, var, pred) \
97 do { \
98 SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
99 } while (0)
100
101 \f
102
103 #define SCM_VALIDATE_REST_ARGUMENT(x) \
104 do { \
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); \
108 } \
109 } \
110 } while (0)
111
112 #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NIMP)
113
114 #define SCM_VALIDATE_BOOL(pos, flag) SCM_MAKE_VALIDATE(pos, flag, BOOLP)
115
116 #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
117 do { \
118 SCM_ASSERT (SCM_BOOLP (flag), flag, pos, FUNC_NAME); \
119 cvar = SCM_EQ_P (flag, SCM_BOOL_T) ? 1 : 0; \
120 } while (0)
121
122 #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CHARP)
123
124 #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
125 do { \
126 SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
127 cvar = SCM_CHAR (scm); \
128 } while (0)
129
130 #define SCM_VALIDATE_ROSTRING(pos, str) SCM_MAKE_VALIDATE (pos, str, ROSTRINGP)
131
132 #define SCM_VALIDATE_ROSTRING_COPY(pos, str, cvar) \
133 do { \
134 SCM_ASSERT (SCM_ROSTRINGP (str), str, pos, FUNC_NAME); \
135 cvar = SCM_ROCHARS (str); \
136 } while (0)
137
138 #define SCM_VALIDATE_NULLORROSTRING_COPY(pos, str, cvar) \
139 do { \
140 SCM_ASSERT (SCM_FALSEP (str) || SCM_ROSTRINGP (str), \
141 str, pos, FUNC_NAME); \
142 if (SCM_FALSEP(str)) \
143 cvar = NULL; \
144 else \
145 cvar = SCM_ROCHARS(str); \
146 } while (0)
147
148 #define SCM_VALIDATE_STRING(pos, str) SCM_MAKE_VALIDATE (pos, str, STRINGP)
149
150 #define SCM_VALIDATE_STRINGORSUBSTR(pos, str) \
151 do { \
152 SCM_ASSERT (SCM_STRINGP (str) || SCM_SUBSTRP (str), \
153 str, pos, FUNC_NAME); \
154 } while (0)
155
156 #define SCM_VALIDATE_STRING_COPY(pos, str, cvar) \
157 do { \
158 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
159 cvar = SCM_CHARS(str); \
160 } while (0)
161
162 #define SCM_VALIDATE_RWSTRING(pos, str) \
163 do { \
164 SCM_ASSERT (SCM_STRINGP (str), str, pos, FUNC_NAME); \
165 if (!SCM_RWSTRINGP (str)) \
166 scm_misc_error (FUNC_NAME, "argument is a read-only string", str); \
167 } while (0)
168
169 #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE (pos, z, REALP)
170
171 #define SCM_VALIDATE_NUMBER_COPY(pos, z, cvar) \
172 do { \
173 if (SCM_INUMP (z)) \
174 cvar = (double) SCM_INUM (z); \
175 else if (SCM_REALP (z)) \
176 cvar = SCM_REAL_VALUE (z); \
177 else if (SCM_BIGP (z)) \
178 cvar = scm_big2dbl (z); \
179 else \
180 { \
181 cvar = 0.0; \
182 SCM_WTA (pos, z); \
183 } \
184 } while (0)
185
186 #define SCM_VALIDATE_INUM(pos, k) SCM_MAKE_VALIDATE (pos, k, INUMP)
187
188 #define SCM_VALIDATE_INUM_COPY(pos, k, cvar) \
189 do { \
190 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
191 cvar = SCM_INUM (k); \
192 } while (0)
193
194 #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
195 do { \
196 cvar = SCM_NUM2ULONG (pos, k); \
197 } while (0)
198
199 #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
200 do { \
201 cvar = SCM_NUM2LONG(pos, k); \
202 } while (0)
203
204 #define SCM_VALIDATE_BIGINT(pos, k) SCM_MAKE_VALIDATE (pos, k, BIGP)
205
206 #define SCM_VALIDATE_INUM_MIN(pos, k, min) \
207 do { \
208 SCM_ASSERT (SCM_INUMP(k), k, pos, FUNC_NAME); \
209 SCM_ASSERT_RANGE (pos, k, (SCM_INUM (k) >= min)); \
210 } while (0)
211
212 #define SCM_VALIDATE_INUM_MIN_COPY(pos, k, min, cvar) \
213 do { \
214 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
215 cvar = SCM_INUM (k); \
216 SCM_ASSERT_RANGE (pos, k, (cvar >= min)); \
217 } while (0)
218
219 #define SCM_VALIDATE_INUM_MIN_DEF_COPY(pos, k, min, default, cvar) \
220 do { \
221 if (SCM_UNBNDP (k)) \
222 k = SCM_MAKINUM (default); \
223 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
224 cvar = SCM_INUM (k); \
225 SCM_ASSERT_RANGE (pos, k, (cvar >= min)); \
226 } while (0)
227
228 #define SCM_VALIDATE_INUM_DEF(pos, k, default) \
229 do { \
230 if (SCM_UNBNDP (k)) \
231 k = SCM_MAKINUM (default); \
232 else SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
233 } while (0)
234
235 #define SCM_VALIDATE_INUM_DEF_COPY(pos, k, default, cvar) \
236 do { \
237 if (SCM_UNBNDP (k)) \
238 { \
239 k = SCM_MAKINUM (default); \
240 cvar = default; \
241 } \
242 else \
243 { \
244 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
245 cvar = SCM_INUM (k); \
246 } \
247 } while (0)
248
249 /* [low,high) */
250 #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
251 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
252 SCM_ASSERT_RANGE(pos,k, \
253 (SCM_INUM (k) >= low && \
254 SCM_INUM (k) < high)); \
255 } while (0)
256
257 #define SCM_VALIDATE_INUM_RANGE_COPY(pos, k, low, high, cvar) \
258 do { \
259 SCM_ASSERT (SCM_INUMP (k), k, pos, FUNC_NAME); \
260 cvar = SCM_INUM (k); \
261 SCM_ASSERT_RANGE (pos, k, cvar >= low && cvar < high); \
262 } while (0)
263
264 #define SCM_VALIDATE_NULL(pos, scm) SCM_MAKE_VALIDATE (pos, scm, NULLP)
265
266 #define SCM_VALIDATE_CONS(pos, scm) SCM_MAKE_VALIDATE (pos, scm, CONSP)
267
268 #define SCM_VALIDATE_LIST(pos, lst) \
269 do { \
270 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
271 } while (0)
272
273 #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
274 do { \
275 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
276 } while (0)
277
278 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
279 do { \
280 cvar = scm_ilength (lst); \
281 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
282 } while (0)
283
284 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
285 do { \
286 cvar = scm_ilength (lst); \
287 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
288 } while (0)
289
290 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
291 do { \
292 SCM_ASSERT (SCM_CONSP (alist) && SCM_CONSP (SCM_CAR (alist)), \
293 alist, pos, FUNC_NAME); \
294 } while (0)
295
296 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
297 do { \
298 SCM_ASSERT (SCM_CONSP (alist), alist, pos, FUNC_NAME); \
299 cvar = SCM_CAR (alist); \
300 SCM_ASSERT (SCM_CONSP (cvar), alist, pos, FUNC_NAME); \
301 } while (0)
302
303 #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
304 do { \
305 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
306 } while (0)
307
308 #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
309
310 #define SCM_VALIDATE_SMOB(pos, obj, type) \
311 do { \
312 SCM_ASSERT ((SCM_NIMP (obj) && SCM_TYP16 (obj) == scm_tc16_ ## type), \
313 obj, pos, FUNC_NAME); \
314 } while (0)
315
316 #define SCM_VALIDATE_THREAD(pos, a) SCM_MAKE_VALIDATE (pos, a, THREADP)
317
318 #define SCM_VALIDATE_THUNK(pos, thunk) \
319 do { \
320 SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
321 } while (0)
322
323 #define SCM_VALIDATE_SYMBOL(pos, sym) SCM_MAKE_VALIDATE (pos, sym, SYMBOLP)
324
325 #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE (pos, var, VARIABLEP)
326
327 #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE (pos, obj, MEMOIZEDP)
328
329 #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE (pos, obj, CLOSUREP)
330
331 #define SCM_VALIDATE_PROC(pos, proc) \
332 do { \
333 SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T), proc, pos, FUNC_NAME); \
334 } while (0)
335
336 #define SCM_VALIDATE_NULLORCONS(pos, env) \
337 do { \
338 SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); \
339 } while (0)
340
341 #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE (pos, a, HOOKP)
342
343 #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE (pos, a, RGXP)
344
345 #define SCM_VALIDATE_OPDIR(pos, port) SCM_MAKE_VALIDATE (pos, port, OPDIRP)
346
347 #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE (pos, port, DIRP)
348
349 #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE (pos, port, PORTP)
350
351 #define SCM_VALIDATE_INPUT_PORT(pos, port) \
352 SCM_MAKE_VALIDATE (pos, port, INPUT_PORT_P)
353
354 #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
355 SCM_MAKE_VALIDATE (pos, port, OUTPUT_PORT_P)
356
357 #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, FPORTP)
358
359 #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPFPORTP)
360
361 #define SCM_VALIDATE_OPINPORT(pos, port) \
362 SCM_MAKE_VALIDATE (pos, port, OPINPORTP)
363
364 #define SCM_VALIDATE_OPENPORT(pos,port) \
365 do { \
366 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
367 port, pos, FUNC_NAME); \
368 } while (0)
369
370 #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE (pos, port, OPPORTP)
371
372 #define SCM_VALIDATE_OPOUTPORT(pos, port) \
373 SCM_MAKE_VALIDATE (pos, port, OPOUTPORTP)
374
375 #define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE (pos, fluid, FLUIDP)
376
377 #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE (pos, v, KEYWORDP)
378
379 #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE (pos, v, STACKP)
380
381 #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE (pos, v, FRAMEP)
382
383 #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE (pos, v, RSTATEP)
384
385 #define SCM_VALIDATE_ARRAY(pos,v) \
386 do { \
387 SCM_ASSERT (SCM_NIMP (v) \
388 && !SCM_FALSEP (scm_array_p (v, SCM_UNDEFINED)), \
389 v, pos, FUNC_NAME); \
390 } while (0)
391
392 #define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE (pos, v, VECTORP)
393
394 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
395 do { \
396 SCM_ASSERT ((SCM_VECTORP (v) \
397 || (SCM_NIMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
398 v, pos, FUNC_NAME); \
399 } while (0)
400
401 #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE (pos, v, STRUCTP)
402
403 #define SCM_VALIDATE_VTABLE(pos, v) \
404 do { \
405 SCM_ASSERT (SCM_NIMP (v) && SCM_NFALSEP (scm_struct_vtable_p (v)), \
406 v, pos, FUNC_NAME); \
407 } while (0)
408
409 #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
410 do { \
411 SCM_ASSERT (SCM_VECTORP (v) && len == SCM_LENGTH (v), v, pos, FUNC_NAME); \
412 } while (0)
413
414 #endif
415
416 /*
417 Local Variables:
418 c-file-style: "gnu"
419 End:
420 */