Merge branch 'master' of git://git.savannah.gnu.org/guile
[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, 2002, 2004, 2006, 2007, 2009 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library 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 GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
24
25 \f
26
27 #define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
28
29 #define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
30
31 #define SCM_SYSERROR_MSG(str, args, val) \
32 do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
33
34 #define SCM_MISC_ERROR(str, args) \
35 do { scm_misc_error (FUNC_NAME, str, args); } while (0)
36
37 #define SCM_WRONG_NUM_ARGS() \
38 do { scm_error_num_args_subr (FUNC_NAME); } while (0)
39
40 #define SCM_WRONG_TYPE_ARG(pos, obj) \
41 do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
42
43 #define SCM_NUM2SIZE(pos, arg) (scm_to_size_t (arg))
44
45 #define SCM_NUM2SIZE_DEF(pos, arg, def) \
46 (SCM_UNBNDP (arg) ? def : scm_to_size_t (arg))
47
48 #define SCM_NUM2PTRDIFF(pos, arg) (scm_to_ssize_t (arg))
49
50 #define SCM_NUM2PTRDIFF_DEF(pos, arg, def) \
51 (SCM_UNBNDP (arg) ? def : scm_to_ssize_t (arg))
52
53 #define SCM_NUM2SHORT(pos, arg) (scm_to_short (arg))
54
55 #define SCM_NUM2SHORT_DEF(pos, arg, def) \
56 (SCM_UNBNDP (arg) ? def : scm_to_short (arg))
57
58 #define SCM_NUM2USHORT(pos, arg) (scm_to_ushort (arg))
59
60 #define SCM_NUM2USHORT_DEF(pos, arg, def) \
61 (SCM_UNBNDP (arg) ? def : scm_to_ushort (arg))
62
63 #define SCM_NUM2INT(pos, arg) (scm_to_int (arg))
64
65 #define SCM_NUM2INT_DEF(pos, arg, def) \
66 (SCM_UNBNDP (arg) ? def : scm_to_int (arg))
67
68 #define SCM_NUM2UINT(pos, arg) (scm_to_uint (arg))
69
70 #define SCM_NUM2UINT_DEF(pos, arg, def) \
71 (SCM_UNBNDP (arg) ? def : scm_to_uint (arg))
72
73 #define SCM_NUM2ULONG(pos, arg) (scm_to_ulong (arg))
74
75 #define SCM_NUM2ULONG_DEF(pos, arg, def) \
76 (SCM_UNBNDP (arg) ? def : scm_to_ulong (arg))
77
78 #define SCM_NUM2LONG(pos, arg) (scm_to_long (arg))
79
80 #define SCM_NUM2LONG_DEF(pos, arg, def) \
81 (SCM_UNBNDP (arg) ? def : scm_to_long (arg))
82
83 #define SCM_NUM2LONG_LONG(pos, arg) (scm_to_long_long (arg))
84
85 #define SCM_NUM2LONG_LONG_DEF(pos, arg, def) \
86 (SCM_UNBNDP (arg) ? def : scm_to_long_long (arg))
87
88 #define SCM_NUM2ULONG_LONG(pos, arg) (scm_to_ulong_long (arg))
89
90 #define SCM_NUM2ULONG_LONG_DEF(pos, arg, def) \
91 (SCM_UNBNDP (arg) ? def : scm_to_ulong_long (arg))
92
93 #define SCM_NUM2FLOAT(pos, arg) \
94 (scm_num2float (arg, pos, FUNC_NAME))
95
96 #define SCM_NUM2DOUBLE(pos, arg) \
97 (scm_num2double (arg, pos, FUNC_NAME))
98
99 #define SCM_OUT_OF_RANGE(pos, arg) \
100 do { scm_out_of_range_pos (FUNC_NAME, arg, scm_from_int (pos)); } while (0)
101
102 #define SCM_ASSERT_RANGE(pos, arg, f) \
103 do { if (SCM_UNLIKELY (!(f))) \
104 scm_out_of_range_pos (FUNC_NAME, arg, scm_from_int (pos)); } \
105 while (0)
106
107 #define SCM_MUST_MALLOC_TYPE(type) \
108 ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
109
110 #define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
111 ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
112
113 #define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
114
115 #define SCM_MAKE_VALIDATE(pos, var, pred) \
116 do { \
117 SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
118 } while (0)
119
120 #define SCM_I_MAKE_VALIDATE_MSG2(pos, var, pred, msg) \
121 do { \
122 SCM_ASSERT_TYPE (pred (var), var, pos, FUNC_NAME, msg); \
123 } while (0)
124
125 #define SCM_MAKE_VALIDATE_MSG(pos, var, pred, msg) \
126 SCM_I_MAKE_VALIDATE_MSG2 (pos, var, SCM_ ## pred, msg)
127
128
129 \f
130
131 #define SCM_VALIDATE_REST_ARGUMENT(x) \
132 do { \
133 if (SCM_DEBUG_REST_ARGUMENT) { \
134 if (scm_ilength (x) < 0) { \
135 SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
136 } \
137 } \
138 } while (0)
139
140 #define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, NIMP, "non-immediate")
141
142 #define SCM_VALIDATE_BOOL(pos, flag) \
143 do { \
144 SCM_ASSERT_TYPE (scm_is_bool (flag), flag, pos, FUNC_NAME, "boolean"); \
145 } while (0)
146
147 #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
148 do { \
149 SCM_ASSERT (scm_is_bool (flag), flag, pos, FUNC_NAME); \
150 cvar = scm_to_bool (flag); \
151 } while (0)
152
153 #define SCM_VALIDATE_BYTEVECTOR(_pos, _obj) \
154 SCM_VALIDATE_SMOB ((_pos), (_obj), bytevector)
155
156 #define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, CHARP, "character")
157
158 #define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
159 do { \
160 SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
161 cvar = SCM_CHAR (scm); \
162 } while (0)
163
164 #define SCM_VALIDATE_STRING(pos, str) \
165 do { \
166 SCM_ASSERT_TYPE (scm_is_string (str), str, pos, FUNC_NAME, "string"); \
167 } while (0)
168
169 #define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE_MSG (pos, z, REALP, "real")
170
171 #define SCM_VALIDATE_NUMBER(pos, z) SCM_MAKE_VALIDATE_MSG (pos, z, NUMBERP, "number")
172
173 #define SCM_VALIDATE_USHORT_COPY(pos, k, cvar) \
174 do { \
175 cvar = SCM_NUM2USHORT (pos, k); \
176 } while (0)
177
178 #define SCM_VALIDATE_SHORT_COPY(pos, k, cvar) \
179 do { \
180 cvar = SCM_NUM2SHORT (pos, k); \
181 } while (0)
182
183 #define SCM_VALIDATE_UINT_COPY(pos, k, cvar) \
184 do { \
185 cvar = SCM_NUM2UINT (pos, k); \
186 } while (0)
187
188 #define SCM_VALIDATE_INT_COPY(pos, k, cvar) \
189 do { \
190 cvar = SCM_NUM2INT (pos, k); \
191 } while (0)
192
193 #define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
194 do { \
195 cvar = SCM_NUM2ULONG (pos, k); \
196 } while (0)
197
198 #define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
199 do { \
200 cvar = SCM_NUM2LONG (pos, k); \
201 } while (0)
202
203 #define SCM_VALIDATE_FLOAT_COPY(pos, k, cvar) \
204 do { \
205 cvar = SCM_NUM2FLOAT (pos, k); \
206 } while (0)
207
208 #define SCM_VALIDATE_DOUBLE_COPY(pos, k, cvar) \
209 do { \
210 cvar = SCM_NUM2DOUBLE (pos, k); \
211 } while (0)
212
213 #define SCM_VALIDATE_DOUBLE_DEF_COPY(pos, k, default, cvar) \
214 do { \
215 if (SCM_UNBNDP (k)) \
216 { \
217 k = scm_make_real (default); \
218 cvar = default; \
219 } \
220 else \
221 { \
222 cvar = SCM_NUM2DOUBLE (pos, k); \
223 } \
224 } while (0)
225
226 #define SCM_VALIDATE_NULL(pos, scm) \
227 SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_null, "empty list")
228
229 #define SCM_VALIDATE_NULL_OR_NIL(pos, scm) \
230 SCM_MAKE_VALIDATE_MSG (pos, scm, NULL_OR_NIL_P, "empty list")
231
232 #define SCM_VALIDATE_CONS(pos, scm) \
233 SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_pair, "pair")
234
235 #define SCM_VALIDATE_LIST(pos, lst) \
236 do { \
237 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
238 } while (0)
239
240 #define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
241 do { \
242 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
243 } while (0)
244
245 #define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
246 do { \
247 cvar = scm_ilength (lst); \
248 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
249 } while (0)
250
251 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
252 do { \
253 cvar = scm_ilength (lst); \
254 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
255 } while (0)
256
257 #define SCM_VALIDATE_ALISTCELL(pos, alist) \
258 do { \
259 SCM_ASSERT (scm_is_pair (alist) && scm_is_pair (SCM_CAR (alist)), \
260 alist, pos, FUNC_NAME); \
261 } while (0)
262
263 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
264 do { \
265 SCM_ASSERT (scm_is_pair (alist), alist, pos, FUNC_NAME); \
266 cvar = SCM_CAR (alist); \
267 SCM_ASSERT (scm_is_pair (cvar), alist, pos, FUNC_NAME); \
268 } while (0)
269
270 #define SCM_VALIDATE_OPORT_VALUE(pos, port) \
271 do { \
272 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
273 } while (0)
274
275 #define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE_MSG(pos, a, PRINT_STATE_P, "print-state")
276
277 #define SCM_VALIDATE_SMOB(pos, obj, type) \
278 do { \
279 SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
280 obj, pos, FUNC_NAME); \
281 } while (0)
282
283 #define SCM_VALIDATE_THUNK(pos, thunk) \
284 do { \
285 SCM_ASSERT (scm_is_true (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
286 } while (0)
287
288 #define SCM_VALIDATE_SYMBOL(pos, str) \
289 do { \
290 SCM_ASSERT_TYPE (scm_is_symbol (str), str, pos, FUNC_NAME, "symbol"); \
291 } while (0)
292
293 #define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE_MSG (pos, var, VARIABLEP, "variable")
294
295 #define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE_MSG (pos, obj, MEMOIZEDP, "memoized code")
296
297 #define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE_MSG (pos, obj, CLOSUREP, "closure")
298
299 #define SCM_VALIDATE_PROC(pos, proc) \
300 do { \
301 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, pos, FUNC_NAME); \
302 } while (0)
303
304 #define SCM_VALIDATE_NULLORCONS(pos, env) \
305 do { \
306 SCM_ASSERT (scm_is_null (env) || scm_is_pair (env), env, pos, FUNC_NAME); \
307 } while (0)
308
309 #define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE_MSG (pos, a, HOOKP, "hook")
310
311 #define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE_MSG (pos, a, RGXP, "regexp")
312
313 #define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, DIRP, "directory port")
314
315 #define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, PORTP, "port")
316
317 #define SCM_VALIDATE_INPUT_PORT(pos, port) \
318 SCM_MAKE_VALIDATE_MSG (pos, port, INPUT_PORT_P, "input port")
319
320 #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
321 SCM_MAKE_VALIDATE_MSG (pos, port, OUTPUT_PORT_P, "output port")
322
323 #define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, FPORTP, "file port")
324
325 #define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, OPFPORTP, "open file port")
326
327 #define SCM_VALIDATE_OPINPORT(pos, port) \
328 SCM_MAKE_VALIDATE_MSG (pos, port, OPINPORTP, "open input port")
329
330 #define SCM_VALIDATE_OPENPORT(pos, port) \
331 do { \
332 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
333 port, pos, FUNC_NAME); \
334 } while (0)
335
336 #define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, OPPORTP, "open port")
337
338 #define SCM_VALIDATE_OPOUTPORT(pos, port) \
339 SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTPORTP, "open output port")
340
341 #define SCM_VALIDATE_OPOUTSTRPORT(pos, port) \
342 SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTSTRPORTP, "open output string port")
343
344 #define SCM_VALIDATE_FLUID(pos, fluid) \
345 SCM_I_MAKE_VALIDATE_MSG2 (pos, fluid, scm_is_fluid, "fluid")
346
347 #define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, KEYWORDP, "keyword")
348
349 #define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, STACKP, "stack")
350
351 #define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, FRAMEP, "frame")
352
353 #define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, RSTATEP, "random-generator-state")
354
355 #define SCM_VALIDATE_ARRAY(pos, v) \
356 do { \
357 SCM_ASSERT (!SCM_IMP (v) \
358 && scm_is_true (scm_array_p (v, SCM_UNDEFINED)), \
359 v, pos, FUNC_NAME); \
360 } while (0)
361
362 #define SCM_VALIDATE_VECTOR(pos, v) \
363 do { \
364 SCM_ASSERT (scm_is_simple_vector (v), v, pos, FUNC_NAME); \
365 } while (0)
366
367 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
368 do { \
369 SCM_ASSERT ((scm_is_simple_vector (v) \
370 || (scm_is_true (scm_f64vector_p (v)))), \
371 v, pos, FUNC_NAME); \
372 } while (0)
373
374 #define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, STRUCTP, "struct")
375
376 #define SCM_VALIDATE_VTABLE(pos, v) \
377 do { \
378 SCM_ASSERT (!SCM_IMP (v) && scm_is_true (scm_struct_vtable_p (v)), \
379 v, pos, FUNC_NAME); \
380 } while (0)
381
382 #define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
383 do { \
384 SCM_ASSERT (scm_is_vector (v) && len == scm_c_vector_length (v), v, pos, FUNC_NAME); \
385 } while (0)
386
387
388 #endif /* SCM_VALIDATE_H */
389
390 /*
391 Local Variables:
392 c-file-style: "gnu"
393 End:
394 */