Merge commit 'e20d7001c3f7150400169fecb0bf0eefdf122fe2' into vm-check
[bpt/guile.git] / libguile / procs.c
1 /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25
26 #include "libguile/objects.h"
27 #include "libguile/strings.h"
28 #include "libguile/vectors.h"
29 #include "libguile/smob.h"
30 #include "libguile/deprecation.h"
31
32 #include "libguile/validate.h"
33 #include "libguile/procs.h"
34 #include "libguile/procprop.h"
35 #include "libguile/objcodes.h"
36 #include "libguile/programs.h"
37 \f
38
39
40 /* {Procedures}
41 */
42
43
44 SCM
45 scm_c_make_subr (const char *name, long type, SCM (*fcn) ())
46 {
47 register SCM z;
48 SCM *meta_info;
49
50 meta_info = scm_gc_malloc (2 * sizeof (*meta_info), "subr meta-info");
51 meta_info[0] = scm_from_locale_symbol (name);
52 meta_info[1] = SCM_EOL; /* properties */
53
54 z = scm_double_cell ((scm_t_bits) type, (scm_t_bits) fcn,
55 0 /* generic */, (scm_t_bits) meta_info);
56
57 return z;
58 }
59
60 SCM
61 scm_c_define_subr (const char *name, long type, SCM (*fcn) ())
62 {
63 SCM subr = scm_c_make_subr (name, type, fcn);
64 scm_define (SCM_SNAME (subr), subr);
65 return subr;
66 }
67
68 /* This function isn't currently used since subrs are never freed. */
69 /* *fixme* Need mutex here. */
70 void
71 scm_free_subr_entry (SCM subr)
72 {
73 scm_gc_free (SCM_SUBR_META_INFO (subr), 2 * sizeof (SCM),
74 "subr meta-info");
75 }
76
77 SCM
78 scm_c_make_subr_with_generic (const char *name,
79 long type, SCM (*fcn) (), SCM *gf)
80 {
81 SCM subr = scm_c_make_subr (name, type, fcn);
82 SCM_SET_SUBR_GENERIC_LOC (subr, gf);
83 return subr;
84 }
85
86 SCM
87 scm_c_define_subr_with_generic (const char *name,
88 long type, SCM (*fcn) (), SCM *gf)
89 {
90 SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf);
91 scm_define (SCM_SNAME (subr), subr);
92 return subr;
93 }
94
95
96 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
97 (SCM obj),
98 "Return @code{#t} if @var{obj} is a procedure.")
99 #define FUNC_NAME s_scm_procedure_p
100 {
101 if (SCM_NIMP (obj))
102 switch (SCM_TYP7 (obj))
103 {
104 case scm_tcs_struct:
105 if (!SCM_I_OPERATORP (obj))
106 break;
107 case scm_tcs_closures:
108 case scm_tcs_subrs:
109 case scm_tc7_pws:
110 return SCM_BOOL_T;
111 case scm_tc7_smob:
112 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
113 default:
114 return SCM_BOOL_F;
115 }
116 return SCM_BOOL_F;
117 }
118 #undef FUNC_NAME
119
120 SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
121 (SCM obj),
122 "Return @code{#t} if @var{obj} is a closure.")
123 #define FUNC_NAME s_scm_closure_p
124 {
125 return scm_from_bool (SCM_CLOSUREP (obj));
126 }
127 #undef FUNC_NAME
128
129 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
130 (SCM obj),
131 "Return @code{#t} if @var{obj} is a thunk.")
132 #define FUNC_NAME s_scm_thunk_p
133 {
134 if (SCM_NIMP (obj))
135 {
136 again:
137 switch (SCM_TYP7 (obj))
138 {
139 case scm_tcs_closures:
140 return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj)));
141 case scm_tc7_subr_0:
142 case scm_tc7_subr_1o:
143 case scm_tc7_lsubr:
144 case scm_tc7_rpsubr:
145 case scm_tc7_asubr:
146 return SCM_BOOL_T;
147 case scm_tc7_gsubr:
148 return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj)) == 0);
149 case scm_tc7_pws:
150 obj = SCM_PROCEDURE (obj);
151 goto again;
152 default:
153 if (SCM_PROGRAM_P (obj) && SCM_PROGRAM_DATA (obj)->nargs == 0)
154 return SCM_BOOL_T;
155 /* otherwise fall through */
156 }
157 }
158 return SCM_BOOL_F;
159 }
160 #undef FUNC_NAME
161
162 /* Only used internally. */
163 int
164 scm_subr_p (SCM obj)
165 {
166 if (SCM_NIMP (obj))
167 switch (SCM_TYP7 (obj))
168 {
169 case scm_tcs_subrs:
170 return 1;
171 default:
172 ;
173 }
174 return 0;
175 }
176
177 SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
178 (SCM proc),
179 "Return the documentation string associated with @code{proc}. By\n"
180 "convention, if a procedure contains more than one expression and the\n"
181 "first expression is a string constant, that string is assumed to contain\n"
182 "documentation for that procedure.")
183 #define FUNC_NAME s_scm_procedure_documentation
184 {
185 SCM code;
186 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
187 proc, SCM_ARG1, FUNC_NAME);
188 switch (SCM_TYP7 (proc))
189 {
190 case scm_tcs_closures:
191 code = SCM_CLOSURE_BODY (proc);
192 if (scm_is_null (SCM_CDR (code)))
193 return SCM_BOOL_F;
194 code = SCM_CAR (code);
195 if (scm_is_string (code))
196 return code;
197 else
198 return SCM_BOOL_F;
199 default:
200 return SCM_BOOL_F;
201 }
202 }
203 #undef FUNC_NAME
204
205
206 /* Procedure-with-setter
207 */
208
209 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
210 (SCM obj),
211 "Return @code{#t} if @var{obj} is a procedure with an\n"
212 "associated setter procedure.")
213 #define FUNC_NAME s_scm_procedure_with_setter_p
214 {
215 return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj));
216 }
217 #undef FUNC_NAME
218
219 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
220 (SCM procedure, SCM setter),
221 "Create a new procedure which behaves like @var{procedure}, but\n"
222 "with the associated setter @var{setter}.")
223 #define FUNC_NAME s_scm_make_procedure_with_setter
224 {
225 SCM name, ret;
226 SCM_VALIDATE_PROC (1, procedure);
227 SCM_VALIDATE_PROC (2, setter);
228 ret = scm_double_cell (scm_tc7_pws,
229 SCM_UNPACK (procedure),
230 SCM_UNPACK (setter), 0);
231 /* don't use procedure_name, because don't care enough to do a reverse
232 lookup */
233 switch (SCM_TYP7 (procedure)) {
234 case scm_tcs_subrs:
235 name = SCM_SNAME (procedure);
236 break;
237 default:
238 name = scm_procedure_property (procedure, scm_sym_name);
239 break;
240 }
241 if (scm_is_true (name))
242 scm_set_procedure_property_x (ret, scm_sym_name, name);
243 return ret;
244 }
245 #undef FUNC_NAME
246
247 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
248 (SCM proc),
249 "Return the procedure of @var{proc}, which must be either a\n"
250 "procedure with setter, or an operator struct.")
251 #define FUNC_NAME s_scm_procedure
252 {
253 SCM_VALIDATE_NIM (1, proc);
254 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
255 return SCM_PROCEDURE (proc);
256 else if (SCM_STRUCTP (proc))
257 {
258 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
259 return proc;
260 }
261 SCM_WRONG_TYPE_ARG (1, proc);
262 return SCM_BOOL_F; /* not reached */
263 }
264 #undef FUNC_NAME
265
266 SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
267
268 SCM
269 scm_setter (SCM proc)
270 {
271 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
272 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
273 return SCM_SETTER (proc);
274 else if (SCM_STRUCTP (proc))
275 {
276 SCM setter;
277 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
278 g_setter, proc, SCM_ARG1, s_setter);
279 setter = (SCM_I_ENTITYP (proc)
280 ? SCM_ENTITY_SETTER (proc)
281 : SCM_OPERATOR_SETTER (proc));
282 if (SCM_NIMP (setter))
283 return setter;
284 /* fall through */
285 }
286 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
287 return SCM_BOOL_F; /* not reached */
288 }
289
290 \f
291 void
292 scm_init_procs ()
293 {
294 #include "libguile/procs.x"
295 }
296
297 /*
298 Local Variables:
299 c-file-style: "gnu"
300 End:
301 */