* sort.c (quicksort): Added INC parameter for non-contigous
[bpt/guile.git] / libguile / validate.h
CommitLineData
b29058ff
DH
1/* classes: h_files */
2
3#ifndef SCM_VALIDATE_H
4#define SCM_VALIDATE_H
5
cc95e00a 6/* Copyright (C) 1999,2000,2001, 2002, 2004 Free Software Foundation, Inc.
e94e3f21 7 *
73be1d9e
MV
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.
e94e3f21 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
5bff3127 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
e94e3f21 17 *
73be1d9e
MV
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
5bff3127
GB
22
23/* Written by Greg J. Badros <gjb@cs.washington.edu>, Dec-1999 */
1bbd0b84 24
b29058ff 25\f
1bbd0b84 26
39bcc76e 27#define SCM_SYSERROR do { scm_syserror (FUNC_NAME); } while (0)
1bbd0b84 28
39bcc76e 29#define SCM_MEMORY_ERROR do { scm_memory_error (FUNC_NAME); } while (0)
1bbd0b84 30
39bcc76e
MD
31#define SCM_SYSERROR_MSG(str, args, val) \
32 do { scm_syserror_msg (FUNC_NAME, (str), (args), (val)); } while (0)
1bbd0b84 33
39bcc76e
MD
34#define SCM_MISC_ERROR(str, args) \
35 do { scm_misc_error (FUNC_NAME, str, args); } while (0)
1bbd0b84 36
398d8ee1 37#define SCM_WRONG_NUM_ARGS() \
68baa7e7 38 do { scm_error_num_args_subr (FUNC_NAME); } while (0)
398d8ee1 39
39bcc76e
MD
40#define SCM_WRONG_TYPE_ARG(pos, obj) \
41 do { scm_wrong_type_arg (FUNC_NAME, pos, obj); } while (0)
1bbd0b84 42
b9bd8526 43#define SCM_NUM2SIZE(pos, arg) (scm_to_size_t (arg))
1be6b49c
ML
44
45#define SCM_NUM2SIZE_DEF(pos, arg, def) \
b9bd8526 46 (SCM_UNBNDP (arg) ? def : scm_to_size_t (arg))
1be6b49c 47
b9bd8526 48#define SCM_NUM2PTRDIFF(pos, arg) (scm_to_ssize_t (arg))
1be6b49c
ML
49
50#define SCM_NUM2PTRDIFF_DEF(pos, arg, def) \
b9bd8526 51 (SCM_UNBNDP (arg) ? def : scm_to_ssize_t (arg))
1be6b49c 52
b9bd8526 53#define SCM_NUM2SHORT(pos, arg) (scm_to_short (arg))
1be6b49c
ML
54
55#define SCM_NUM2SHORT_DEF(pos, arg, def) \
b9bd8526 56 (SCM_UNBNDP (arg) ? def : scm_to_short (arg))
1be6b49c 57
b9bd8526 58#define SCM_NUM2USHORT(pos, arg) (scm_to_ushort (arg))
1be6b49c
ML
59
60#define SCM_NUM2USHORT_DEF(pos, arg, def) \
b9bd8526 61 (SCM_UNBNDP (arg) ? def : scm_to_ushort (arg))
1be6b49c 62
b9bd8526 63#define SCM_NUM2INT(pos, arg) (scm_to_int (arg))
1be6b49c
ML
64
65#define SCM_NUM2INT_DEF(pos, arg, def) \
b9bd8526 66 (SCM_UNBNDP (arg) ? def : scm_to_int (arg))
1be6b49c 67
b9bd8526 68#define SCM_NUM2UINT(pos, arg) (scm_to_uint (arg))
1be6b49c
ML
69
70#define SCM_NUM2UINT_DEF(pos, arg, def) \
b9bd8526 71 (SCM_UNBNDP (arg) ? def : scm_to_uint (arg))
1be6b49c 72
b9bd8526 73#define SCM_NUM2ULONG(pos, arg) (scm_to_ulong (arg))
1bbd0b84 74
1be6b49c 75#define SCM_NUM2ULONG_DEF(pos, arg, def) \
b9bd8526 76 (SCM_UNBNDP (arg) ? def : scm_to_ulong (arg))
1be6b49c 77
b9bd8526 78#define SCM_NUM2LONG(pos, arg) (scm_to_long (arg))
1bbd0b84 79
39bcc76e 80#define SCM_NUM2LONG_DEF(pos, arg, def) \
b9bd8526 81 (SCM_UNBNDP (arg) ? def : scm_to_long (arg))
c1bfcf60 82
b9bd8526 83#define SCM_NUM2LONG_LONG(pos, arg) (scm_to_long_long (arg))
1bbd0b84 84
1be6b49c 85#define SCM_NUM2LONG_LONG_DEF(pos, arg, def) \
b9bd8526 86 (SCM_UNBNDP (arg) ? def : scm_to_long_long (arg))
1be6b49c 87
b9bd8526 88#define SCM_NUM2ULONG_LONG(pos, arg) (scm_to_ulong_long (arg))
1be6b49c
ML
89
90#define SCM_NUM2ULONG_LONG_DEF(pos, arg, def) \
b9bd8526 91 (SCM_UNBNDP (arg) ? def : scm_to_ulong_long (arg))
1be6b49c 92
581ded70
MD
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
39bcc76e 99#define SCM_OUT_OF_RANGE(pos, arg) \
93ccaef0 100 do { scm_out_of_range_pos (FUNC_NAME, arg, scm_from_int (pos)); } while (0)
1bbd0b84 101
39bcc76e 102#define SCM_ASSERT_RANGE(pos, arg, f) \
93ccaef0 103 do { if (!(f)) scm_out_of_range_pos (FUNC_NAME, arg, scm_from_int (pos)); } while (0)
1bbd0b84 104
39bcc76e
MD
105#define SCM_MUST_MALLOC_TYPE(type) \
106 ((type *) scm_must_malloc (sizeof (type), FUNC_NAME))
1bbd0b84 107
39bcc76e
MD
108#define SCM_MUST_MALLOC_TYPE_NUM(type, num) \
109 ((type *) scm_must_malloc (sizeof (type) * (num), FUNC_NAME))
1bbd0b84 110
39bcc76e 111#define SCM_MUST_MALLOC(size) (scm_must_malloc ((size), FUNC_NAME))
1bbd0b84 112
39bcc76e
MD
113#define SCM_MAKE_VALIDATE(pos, var, pred) \
114 do { \
115 SCM_ASSERT_TYPE (SCM_ ## pred (var), var, pos, FUNC_NAME, #pred); \
116 } while (0)
6b5a304f 117
d2e53ed6 118#define SCM_I_MAKE_VALIDATE_MSG2(pos, var, pred, msg) \
6182ceac 119 do { \
d2e53ed6 120 SCM_ASSERT_TYPE (pred (var), var, pos, FUNC_NAME, msg); \
6182ceac
MV
121 } while (0)
122
d2e53ed6
MV
123#define SCM_MAKE_VALIDATE_MSG(pos, var, pred, msg) \
124 SCM_I_MAKE_VALIDATE_MSG2 (pos, var, SCM_ ## pred, msg)
125
126
6e8d25a6
GB
127\f
128
af45e3b0
DH
129#define SCM_VALIDATE_REST_ARGUMENT(x) \
130 do { \
131 if (SCM_DEBUG_REST_ARGUMENT) { \
132 if (scm_ilength (x) < 0) { \
133 SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); \
134 } \
135 } \
136 } while (0)
137
6182ceac 138#define SCM_VALIDATE_NIM(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, NIMP, "non-immediate")
6e8d25a6 139
7888309b
MV
140#define SCM_VALIDATE_BOOL(pos, flag) \
141 do { \
142 SCM_ASSERT_TYPE (scm_is_bool (flag), flag, pos, FUNC_NAME, "boolean"); \
143 } while (0)
1bbd0b84 144
39bcc76e
MD
145#define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
146 do { \
7888309b 147 SCM_ASSERT (scm_is_bool (flag), flag, pos, FUNC_NAME); \
bc36d050 148 cvar = scm_to_bool (flag); \
39bcc76e 149 } while (0)
1bbd0b84 150
6182ceac 151#define SCM_VALIDATE_CHAR(pos, scm) SCM_MAKE_VALIDATE_MSG (pos, scm, CHARP, "character")
1bbd0b84 152
39bcc76e
MD
153#define SCM_VALIDATE_CHAR_COPY(pos, scm, cvar) \
154 do { \
155 SCM_ASSERT (SCM_CHARP (scm), scm, pos, FUNC_NAME); \
156 cvar = SCM_CHAR (scm); \
157 } while (0)
1bbd0b84 158
cc95e00a
MV
159#define SCM_VALIDATE_STRING(pos, str) \
160 do { \
161 SCM_ASSERT_TYPE (scm_is_string (str), str, pos, FUNC_NAME, "string"); \
162 } while (0)
60d02d09 163
6182ceac 164#define SCM_VALIDATE_REAL(pos, z) SCM_MAKE_VALIDATE_MSG (pos, z, REALP, "real")
1bbd0b84 165
6182ceac 166#define SCM_VALIDATE_NUMBER(pos, z) SCM_MAKE_VALIDATE_MSG (pos, z, NUMBERP, "number")
e8a46ba8 167
d098d810
TTN
168#define SCM_VALIDATE_USHORT_COPY(pos, k, cvar) \
169 do { \
170 cvar = SCM_NUM2USHORT (pos, k); \
171 } while (0)
172
173#define SCM_VALIDATE_SHORT_COPY(pos, k, cvar) \
174 do { \
175 cvar = SCM_NUM2SHORT (pos, k); \
176 } while (0)
177
178#define SCM_VALIDATE_UINT_COPY(pos, k, cvar) \
179 do { \
180 cvar = SCM_NUM2UINT (pos, k); \
181 } while (0)
182
183#define SCM_VALIDATE_INT_COPY(pos, k, cvar) \
184 do { \
185 cvar = SCM_NUM2INT (pos, k); \
186 } while (0)
187
39bcc76e
MD
188#define SCM_VALIDATE_ULONG_COPY(pos, k, cvar) \
189 do { \
190 cvar = SCM_NUM2ULONG (pos, k); \
191 } while (0)
2cd04b42 192
39bcc76e
MD
193#define SCM_VALIDATE_LONG_COPY(pos, k, cvar) \
194 do { \
e4b265d8 195 cvar = SCM_NUM2LONG (pos, k); \
39bcc76e 196 } while (0)
2cd04b42 197
581ded70
MD
198#define SCM_VALIDATE_FLOAT_COPY(pos, k, cvar) \
199 do { \
200 cvar = SCM_NUM2FLOAT (pos, k); \
201 } while (0)
202
203#define SCM_VALIDATE_DOUBLE_COPY(pos, k, cvar) \
204 do { \
205 cvar = SCM_NUM2DOUBLE (pos, k); \
206 } while (0)
207
68665a97
MD
208#define SCM_VALIDATE_DOUBLE_DEF_COPY(pos, k, default, cvar) \
209 do { \
210 if (SCM_UNBNDP (k)) \
211 { \
212 k = scm_make_real (default); \
213 cvar = default; \
214 } \
215 else \
216 { \
217 cvar = SCM_NUM2DOUBLE (pos, k); \
218 } \
219 } while (0)
220
d2e53ed6
MV
221#define SCM_VALIDATE_NULL(pos, scm) \
222 SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_null, "empty list")
1bbd0b84 223
d2e53ed6
MV
224#define SCM_VALIDATE_NULL_OR_NIL(pos, scm) \
225 SCM_MAKE_VALIDATE_MSG (pos, scm, NULL_OR_NIL_P, "empty list")
c96d76b8 226
d2e53ed6
MV
227#define SCM_VALIDATE_CONS(pos, scm) \
228 SCM_I_MAKE_VALIDATE_MSG2 (pos, scm, scm_is_pair, "pair")
1bbd0b84 229
39bcc76e
MD
230#define SCM_VALIDATE_LIST(pos, lst) \
231 do { \
232 SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); \
233 } while (0)
1bbd0b84 234
39bcc76e
MD
235#define SCM_VALIDATE_NONEMPTYLIST(pos, lst) \
236 do { \
237 SCM_ASSERT (scm_ilength (lst) > 0, lst, pos, FUNC_NAME); \
238 } while (0)
1bbd0b84 239
39bcc76e
MD
240#define SCM_VALIDATE_LIST_COPYLEN(pos, lst, cvar) \
241 do { \
242 cvar = scm_ilength (lst); \
243 SCM_ASSERT (cvar >= 0, lst, pos, FUNC_NAME); \
244 } while (0)
c1bfcf60 245
39bcc76e
MD
246#define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos, lst, cvar) \
247 do { \
248 cvar = scm_ilength (lst); \
249 SCM_ASSERT (cvar >= 1, lst, pos, FUNC_NAME); \
250 } while (0)
1bbd0b84 251
39bcc76e
MD
252#define SCM_VALIDATE_ALISTCELL(pos, alist) \
253 do { \
d2e53ed6 254 SCM_ASSERT (scm_is_pair (alist) && scm_is_pair (SCM_CAR (alist)), \
39bcc76e
MD
255 alist, pos, FUNC_NAME); \
256 } while (0)
1bbd0b84 257
39bcc76e 258#define SCM_VALIDATE_ALISTCELL_COPYSCM(pos, alist, cvar) \
1bbd0b84 259 do { \
d2e53ed6 260 SCM_ASSERT (scm_is_pair (alist), alist, pos, FUNC_NAME); \
39bcc76e 261 cvar = SCM_CAR (alist); \
d2e53ed6 262 SCM_ASSERT (scm_is_pair (cvar), alist, pos, FUNC_NAME); \
30939477 263 } while (0)
1bbd0b84 264
39bcc76e 265#define SCM_VALIDATE_OPORT_VALUE(pos, port) \
1bbd0b84 266 do { \
39bcc76e
MD
267 SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); \
268 } while (0)
269
6182ceac 270#define SCM_VALIDATE_PRINTSTATE(pos, a) SCM_MAKE_VALIDATE_MSG(pos, a, PRINT_STATE_P, "print-state")
1bbd0b84 271
39bcc76e
MD
272#define SCM_VALIDATE_SMOB(pos, obj, type) \
273 do { \
e841c3e0 274 SCM_ASSERT (SCM_TYP16_PREDICATE (scm_tc16_ ## type, obj), \
39bcc76e
MD
275 obj, pos, FUNC_NAME); \
276 } while (0)
1bbd0b84 277
39bcc76e
MD
278#define SCM_VALIDATE_THUNK(pos, thunk) \
279 do { \
7888309b 280 SCM_ASSERT (scm_is_true (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); \
39bcc76e 281 } while (0)
1bbd0b84 282
cc95e00a
MV
283#define SCM_VALIDATE_SYMBOL(pos, str) \
284 do { \
285 SCM_ASSERT_TYPE (scm_is_symbol (str), str, pos, FUNC_NAME, "symbol"); \
286 } while (0)
0c95b57d 287
6182ceac 288#define SCM_VALIDATE_VARIABLE(pos, var) SCM_MAKE_VALIDATE_MSG (pos, var, VARIABLEP, "variable")
1bbd0b84 289
6182ceac 290#define SCM_VALIDATE_MEMOIZED(pos, obj) SCM_MAKE_VALIDATE_MSG (pos, obj, MEMOIZEDP, "memoized code")
1bbd0b84 291
6182ceac 292#define SCM_VALIDATE_CLOSURE(pos, obj) SCM_MAKE_VALIDATE_MSG (pos, obj, CLOSUREP, "closure")
1bbd0b84 293
39bcc76e
MD
294#define SCM_VALIDATE_PROC(pos, proc) \
295 do { \
bc36d050 296 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, pos, FUNC_NAME); \
39bcc76e 297 } while (0)
1bbd0b84 298
39bcc76e
MD
299#define SCM_VALIDATE_NULLORCONS(pos, env) \
300 do { \
d2e53ed6 301 SCM_ASSERT (scm_is_null (env) || scm_is_pair (env), env, pos, FUNC_NAME); \
39bcc76e 302 } while (0)
1bbd0b84 303
6182ceac 304#define SCM_VALIDATE_HOOK(pos, a) SCM_MAKE_VALIDATE_MSG (pos, a, HOOKP, "hook")
1bbd0b84 305
6182ceac 306#define SCM_VALIDATE_RGXP(pos, a) SCM_MAKE_VALIDATE_MSG (pos, a, RGXP, "regexp")
1bbd0b84 307
6182ceac 308#define SCM_VALIDATE_DIR(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, DIRP, "directory port")
1bbd0b84 309
6182ceac 310#define SCM_VALIDATE_PORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, PORTP, "port")
1bbd0b84 311
39bcc76e 312#define SCM_VALIDATE_INPUT_PORT(pos, port) \
6182ceac 313 SCM_MAKE_VALIDATE_MSG (pos, port, INPUT_PORT_P, "input port")
1bbd0b84 314
39bcc76e 315#define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
6182ceac 316 SCM_MAKE_VALIDATE_MSG (pos, port, OUTPUT_PORT_P, "output port")
1bbd0b84 317
6182ceac 318#define SCM_VALIDATE_FPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, FPORTP, "file port")
1bbd0b84 319
6182ceac 320#define SCM_VALIDATE_OPFPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, OPFPORTP, "open file port")
1bbd0b84 321
39bcc76e 322#define SCM_VALIDATE_OPINPORT(pos, port) \
6182ceac 323 SCM_MAKE_VALIDATE_MSG (pos, port, OPINPORTP, "open input port")
1bbd0b84 324
34d19ef6 325#define SCM_VALIDATE_OPENPORT(pos, port) \
39bcc76e
MD
326 do { \
327 SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
328 port, pos, FUNC_NAME); \
329 } while (0)
1bbd0b84 330
6182ceac 331#define SCM_VALIDATE_OPPORT(pos, port) SCM_MAKE_VALIDATE_MSG (pos, port, OPPORTP, "open port")
1bbd0b84 332
39bcc76e 333#define SCM_VALIDATE_OPOUTPORT(pos, port) \
6182ceac 334 SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTPORTP, "open output port")
1bbd0b84 335
e87a03fc 336#define SCM_VALIDATE_OPOUTSTRPORT(pos, port) \
6182ceac 337 SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTSTRPORTP, "open output string port")
e87a03fc 338
6182ceac 339#define SCM_VALIDATE_FLUID(pos, fluid) SCM_MAKE_VALIDATE_MSG (pos, fluid, FLUIDP, "fluid")
1bbd0b84 340
6182ceac 341#define SCM_VALIDATE_KEYWORD(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, KEYWORDP, "keyword")
1bbd0b84 342
6182ceac 343#define SCM_VALIDATE_STACK(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, STACKP, "stack")
1bbd0b84 344
6182ceac 345#define SCM_VALIDATE_FRAME(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, FRAMEP, "frame")
1bbd0b84 346
6182ceac 347#define SCM_VALIDATE_RSTATE(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, RSTATEP, "random-generator-state")
1bbd0b84 348
34d19ef6 349#define SCM_VALIDATE_ARRAY(pos, v) \
39bcc76e 350 do { \
b29058ff 351 SCM_ASSERT (!SCM_IMP (v) \
7888309b 352 && scm_is_true (scm_array_p (v, SCM_UNDEFINED)), \
39bcc76e
MD
353 v, pos, FUNC_NAME); \
354 } while (0)
1bbd0b84 355
6182ceac 356#define SCM_VALIDATE_VECTOR(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, VECTORP, "vector")
1bbd0b84 357
39bcc76e
MD
358#define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos, v) \
359 do { \
360 SCM_ASSERT ((SCM_VECTORP (v) \
df47dca3 361 || (scm_is_true (scm_f64vector_p (v)))), \
39bcc76e
MD
362 v, pos, FUNC_NAME); \
363 } while (0)
368cf54d 364
6182ceac 365#define SCM_VALIDATE_STRUCT(pos, v) SCM_MAKE_VALIDATE_MSG (pos, v, STRUCTP, "struct")
1bbd0b84 366
39bcc76e
MD
367#define SCM_VALIDATE_VTABLE(pos, v) \
368 do { \
7888309b 369 SCM_ASSERT (!SCM_IMP (v) && scm_is_true (scm_struct_vtable_p (v)), \
39bcc76e
MD
370 v, pos, FUNC_NAME); \
371 } while (0)
1bbd0b84 372
39bcc76e
MD
373#define SCM_VALIDATE_VECTOR_LEN(pos, v, len) \
374 do { \
9fd38a3d 375 SCM_ASSERT (SCM_VECTORP (v) && len == SCM_VECTOR_LENGTH (v), v, pos, FUNC_NAME); \
39bcc76e 376 } while (0)
1bbd0b84 377
6f14f578 378
b29058ff 379#endif /* SCM_VALIDATE_H */
89e00824
ML
380
381/*
382 Local Variables:
383 c-file-style: "gnu"
384 End:
385*/