* eval.c: define scm_unbound_variable_key ('unbound-variable).
[bpt/guile.git] / libguile / scm_validate.h
1 /* $Id: scm_validate.h,v 1.15 2000-01-09 13:41:53 ghouston Exp $ */
2 /* Copyright (C) 1999 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_WTA(pos,scm) \
58 do { scm_wta(scm,(char *)pos,FUNC_NAME); } while (0)
59
60 #define RETURN_SCM_WTA(pos,scm) \
61 do { return scm_wta(scm,(char *)pos,FUNC_NAME); } while (0)
62
63 #define SCM_MISC_ERROR(str,args) \
64 do { scm_misc_error(FUNC_NAME,str,args); } while (0)
65
66 #define SCM_WRONG_TYPE_ARG(pos,obj) \
67 do { scm_wrong_type_arg(FUNC_NAME,pos,obj); } while (0)
68
69 #define SCM_NUM2ULONG(pos,arg) (scm_num2ulong(arg, (char *) pos, FUNC_NAME))
70
71 #define SCM_NUM2LONG(pos,arg) (scm_num2long(arg, (char *) pos, FUNC_NAME))
72
73 #define SCM_NUM2LONG_LONG(pos,arg) (scm_num2long_long(arg, (char *) pos, FUNC_NAME))
74
75 #define SCM_OUT_OF_RANGE(pos,arg) do { scm_out_of_range_pos(FUNC_NAME,arg,scm_long2num(pos)); } while (0)
76
77 #define SCM_ASSERT_RANGE(pos,arg,f) do { SCM_ASSERT(f,arg,SCM_OUTOFRANGE,FUNC_NAME); } while (0)
78
79 #define SCM_MUST_MALLOC_TYPE(type) ((type *) scm_must_malloc(sizeof(type), FUNC_NAME))
80
81 #define SCM_MUST_MALLOC_TYPE_NUM(type,num) ((type *) scm_must_malloc(sizeof(type)*(num), FUNC_NAME))
82
83 #define SCM_MUST_MALLOC(size) (scm_must_malloc((size), FUNC_NAME))
84
85 #define SCM_MAKE_VALIDATE(pos,var,pred) \
86 do { SCM_ASSERT (SCM_ ## pred(var), var, pos, FUNC_NAME); } while (0)
87
88 \f
89
90 #define SCM_VALIDATE_NIM(pos,scm) SCM_MAKE_VALIDATE(pos,scm,NIMP)
91
92 #define SCM_VALIDATE_BOOL(pos,flag) SCM_MAKE_VALIDATE(pos,flag,BOOLP)
93
94 #define SCM_VALIDATE_BOOL_COPY(pos,flag,cvar) \
95 do { SCM_ASSERT(SCM_BOOLP(flag), flag, pos, FUNC_NAME); \
96 cvar = (SCM_BOOL_T == flag)? 1: 0; } while (0)
97
98 #define SCM_VALIDATE_CHAR(pos,scm) SCM_MAKE_VALIDATE(pos,scm,ICHRP)
99
100 #define SCM_VALIDATE_CHAR_COPY(pos,scm,cvar) \
101 do { SCM_ASSERT(SCM_ICHRP(scm), scm, pos, FUNC_NAME); \
102 cvar = SCM_ICHR(scm); } while (0)
103
104 #define SCM_VALIDATE_ROSTRING(pos,str) SCM_MAKE_VALIDATE(pos,str,ROSTRINGP)
105
106 #define SCM_VALIDATE_ROSTRING_COPY(pos,str,cvar) \
107 do { SCM_ASSERT(SCM_ROSTRINGP (str), str, pos, FUNC_NAME); \
108 cvar = SCM_ROCHARS(str); } while (0)
109
110 #define SCM_VALIDATE_NULLORROSTRING_COPY(pos,str,cvar) \
111 do { SCM_ASSERT(SCM_FALSEP(str) || SCM_ROSTRINGP (str), str, pos, FUNC_NAME); \
112 if (SCM_FALSEP(str)) cvar = NULL; else cvar = SCM_ROCHARS(str); } while (0)
113
114 #define SCM_VALIDATE_STRING(pos,str) SCM_MAKE_VALIDATE(pos,str,STRINGP)
115
116 #define SCM_VALIDATE_STRINGORSUBSTR(pos,str) \
117 do { SCM_ASSERT(SCM_SLOPPY_STRINGP (str) || \
118 SCM_SLOPPY_SUBSTRP(str), str, pos, FUNC_NAME); } while (0)
119
120 #define SCM_VALIDATE_STRING_COPY(pos,str,cvar) \
121 do { SCM_ASSERT(SCM_STRINGP (str), str, pos, FUNC_NAME); \
122 cvar = SCM_CHARS(str); } while (0)
123
124 #define SCM_VALIDATE_RWSTRING(pos,str) \
125 do { SCM_ASSERT(SCM_STRINGP (str), str, pos, FUNC_NAME); \
126 if (!SCM_RWSTRINGP(str)) scm_misc_error(FUNC_NAME, "argument is a read-only string", str); } while (0)
127
128 #define SCM_VALIDATE_REAL(pos,z) SCM_MAKE_VALIDATE(pos,z,REALP)
129
130 #define SCM_VALIDATE_INUM(pos,k) SCM_MAKE_VALIDATE(pos,k,INUMP)
131
132 #define SCM_VALIDATE_INUM_COPY(pos,k,cvar) \
133 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
134 cvar = SCM_INUM(k); } while (0)
135
136 #define SCM_VALIDATE_BIGINT(pos,k) SCM_MAKE_VALIDATE(pos,k,BIGP)
137
138 #define SCM_VALIDATE_INUM_MIN(pos,k,min) \
139 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
140 SCM_ASSERT_RANGE(pos,k,(SCM_INUM(k) >= min)); } while (0)
141
142 #define SCM_VALIDATE_INUM_MIN_COPY(pos,k,min,cvar) \
143 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
144 cvar = SCM_INUM(k); \
145 SCM_ASSERT_RANGE(pos,k,(cvar >= min)); } while (0)
146
147 #define SCM_VALIDATE_INUM_MIN_DEF_COPY(pos,k,min,default,cvar) \
148 do { if (SCM_UNBNDP(k)) k = SCM_MAKINUM(default); \
149 SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
150 cvar = SCM_INUM(k); \
151 SCM_ASSERT_RANGE(pos,k,(cvar >= min)); } while (0)
152
153 #define SCM_VALIDATE_INUM_DEF(pos,k,default) \
154 do { if (SCM_UNDEFINED==k) k = SCM_MAKINUM(default); \
155 else SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); } while (0)
156
157 #define SCM_VALIDATE_INUM_DEF_COPY(pos,k,default,cvar) \
158 do { if (SCM_UNDEFINED==k) { k = SCM_MAKINUM(default); cvar=default; } \
159 else { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); cvar = SCM_INUM(k); } } while (0)
160
161 /* [low,high) */
162 #define SCM_VALIDATE_INUM_RANGE(pos,k,low,high) \
163 do { SCM_ASSERT(SCM_INUMP(k), k, pos, FUNC_NAME); \
164 SCM_ASSERT_RANGE(pos,k,(SCM_INUM (k) >= low && ((unsigned) SCM_INUM (k)) < high)); \
165 } while (0)
166
167 #define SCM_VALIDATE_NULL(pos,scm) SCM_MAKE_VALIDATE(pos,scm,NULLP)
168
169 #define SCM_VALIDATE_CONS(pos,scm) SCM_MAKE_VALIDATE(pos,scm,CONSP)
170
171 #define SCM_VALIDATE_LIST(pos,lst) \
172 do { SCM_ASSERT (scm_ilength (lst) >= 0, lst, pos, FUNC_NAME); } while (0)
173
174 #define SCM_VALIDATE_LIST_COPYLEN(pos,lst,cvar) \
175 do { cvar = scm_ilength(lst); SCM_ASSERT(cvar >= 0,lst,pos,FUNC_NAME); } while (0)
176
177 #define SCM_VALIDATE_NONEMPTYLIST_COPYLEN(pos,lst,cvar) \
178 do { cvar = scm_ilength(lst); SCM_ASSERT(cvar >= 1,lst,pos,FUNC_NAME); } while (0)
179
180 #define SCM_VALIDATE_ALISTCELL(pos,alist) \
181 do { \
182 SCM_ASSERT(SCM_CONSP(alist) && SCM_CONSP(SCM_CAR(alist)), alist, pos, FUNC_NAME); \
183 } while (0)
184
185 #define SCM_VALIDATE_ALISTCELL_COPYSCM(pos,alist,cvar) \
186 do { \
187 SCM_ASSERT(SCM_CONSP(alist), alist, pos, FUNC_NAME); \
188 cvar = SCM_CAR(alist); \
189 SCM_ASSERT(SCM_CONSP(cvar), alist, pos, FUNC_NAME); } while (0)
190
191 #define SCM_VALIDATE_OPORT_VALUE(pos,port) \
192 do { SCM_ASSERT (scm_valid_oport_value_p (port), port, pos, FUNC_NAME); } while (0)
193
194 #define SCM_VALIDATE_PRINTSTATE(pos,a) SCM_MAKE_VALIDATE(pos,a,PRINT_STATE_P)
195
196 #define SCM_VALIDATE_SMOB(pos,obj,type) \
197 do { SCM_ASSERT ((SCM_NIMP(obj) && SCM_TYP16 (obj) == scm_tc16_ ## type), obj, pos, FUNC_NAME); } while (0)
198
199 #define SCM_VALIDATE_ASYNC(pos,a) SCM_MAKE_VALIDATE(pos,a,ASYNCP)
200
201 #define SCM_VALIDATE_ASYNC_COPY(pos,a,cvar) \
202 do { SCM_ASSERT (SCM_ASYNCP (a), a, pos, FUNC_NAME); \
203 cvar = SCM_ASYNC(a); } while (0)
204
205 #define SCM_VALIDATE_THREAD(pos,a) SCM_MAKE_VALIDATE(pos,a,THREADP)
206
207 #define SCM_VALIDATE_THUNK(pos,thunk) \
208 do { SCM_ASSERT (SCM_NFALSEP (scm_thunk_p (thunk)), thunk, pos, FUNC_NAME); } while (0)
209
210 #define SCM_VALIDATE_SYMBOL(pos,sym) SCM_MAKE_VALIDATE(pos,sym,SYMBOLP)
211
212 #define SCM_VALIDATE_VARIABLE(pos,var) SCM_MAKE_VALIDATE(pos,var,VARIABLEP)
213
214 #define SCM_VALIDATE_MEMOIZED(pos,obj) SCM_MAKE_VALIDATE(pos,obj,MEMOIZEDP)
215
216 #define SCM_VALIDATE_CLOSURE(pos,obj) SCM_MAKE_VALIDATE(pos,obj,CLOSUREP)
217
218 #define SCM_VALIDATE_PROC(pos,proc) \
219 do { SCM_ASSERT ( SCM_BOOL_T == scm_procedure_p(proc), proc, pos, FUNC_NAME); } while (0)
220
221 #define SCM_VALIDATE_NULLORCONS(pos,env) \
222 do { SCM_ASSERT (SCM_NULLP (env) || SCM_CONSP (env), env, pos, FUNC_NAME); } while (0)
223
224 #define SCM_VALIDATE_HOOK(pos,a) SCM_MAKE_VALIDATE(pos,a,HOOKP)
225
226 #define SCM_VALIDATE_RGXP(pos,a) SCM_MAKE_VALIDATE(pos,a,RGXP)
227
228 #define SCM_VALIDATE_OPDIR(pos,port) SCM_MAKE_VALIDATE(pos,port,OPDIRP)
229
230 #define SCM_VALIDATE_DIR(pos,port) SCM_MAKE_VALIDATE(pos,port,DIRP)
231
232 #define SCM_VALIDATE_PORT(pos,port) SCM_MAKE_VALIDATE(pos,port,PORTP)
233
234 #define SCM_VALIDATE_FPORT(pos,port) SCM_MAKE_VALIDATE(pos,port,FPORTP)
235
236 #define SCM_VALIDATE_OPFPORT(pos,port) SCM_MAKE_VALIDATE(pos,port,OPFPORTP)
237
238 #define SCM_VALIDATE_OPINPORT(pos,port) SCM_MAKE_VALIDATE(pos,port,OPINPORTP)
239
240 #define SCM_VALIDATE_OPENPORT(pos,port) \
241 do { SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP(port), port, pos, FUNC_NAME); } while (0)
242
243 #define SCM_VALIDATE_OPPORT(pos,port) SCM_MAKE_VALIDATE(pos,port,OPPORTP)
244
245 #define SCM_VALIDATE_OPOUTPORT(pos,port) SCM_MAKE_VALIDATE(pos,port,OPOUTPORTP)
246
247 #define SCM_VALIDATE_FLUID(pos,fluid) SCM_MAKE_VALIDATE(pos,fluid,FLUIDP)
248
249 #define SCM_VALIDATE_KEYWORD(pos,v) SCM_MAKE_VALIDATE(pos,v,KEYWORDP)
250
251 #define SCM_VALIDATE_STACK(pos,v) SCM_MAKE_VALIDATE(pos,v,STACKP)
252
253 #define SCM_VALIDATE_FRAME(pos,v) SCM_MAKE_VALIDATE(pos,v,FRAMEP)
254
255 #define SCM_VALIDATE_RSTATE(pos,v) SCM_MAKE_VALIDATE(pos,v,RSTATEP)
256
257 #define SCM_VALIDATE_ARRAY(pos,v) \
258 do { SCM_ASSERT (SCM_NIMP (v) && \
259 SCM_BOOL_F != scm_array_p(v,SCM_UNDEFINED), \
260 v, pos, FUNC_NAME); } while (0)
261
262 #define SCM_VALIDATE_VECTOR(pos,v) SCM_MAKE_VALIDATE(pos,v,VECTORP)
263
264 #define SCM_VALIDATE_VECTOR_OR_DVECTOR(pos,v) \
265 do { SCM_ASSERT ((SCM_VECTORP (v) || \
266 (SCM_NIMP (v) && SCM_TYP7 (v) == scm_tc7_dvect)), \
267 v, pos, FUNC_NAME); } while (0)
268
269 #define SCM_VALIDATE_STRUCT(pos,v) SCM_MAKE_VALIDATE(pos,v,STRUCTP)
270
271 #define SCM_VALIDATE_VTABLE(pos,v) \
272 do { SCM_ASSERT (SCM_NIMP (v) && SCM_NFALSEP(scm_struct_vtable_p(v)), v, pos, FUNC_NAME); } while (0)
273
274 #define SCM_VALIDATE_VECTOR_LEN(pos,v,len) \
275 do { SCM_ASSERT (SCM_VECTORP (v) && len == SCM_LENGTH(v), v, pos, FUNC_NAME); } while (0)
276
277 #endif