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