generic dispatch protocol in scheme, not yet wired up
[bpt/guile.git] / libguile / gsubr.c
1 /* Copyright (C) 1995,1996,1997,1998,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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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
16 * 02110-1301 USA
17 */
18
19 \f
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <alloca.h>
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include "libguile/_scm.h"
30 #include "libguile/procprop.h"
31 #include "libguile/root.h"
32
33 #include "libguile/gsubr.h"
34 #include "libguile/deprecation.h"
35
36 #include "libguile/private-options.h"
37 \f
38 /*
39 * gsubr.c
40 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
41 * and rest arguments.
42 */
43
44 /* #define GSUBR_TEST */
45
46 SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
47
48 static SCM
49 create_gsubr (int define, const char *name,
50 unsigned int req, unsigned int opt, unsigned int rst,
51 SCM (*fcn) ())
52 {
53 SCM subr;
54
55 switch (SCM_GSUBR_MAKTYPE (req, opt, rst))
56 {
57 case SCM_GSUBR_MAKTYPE(0, 0, 0):
58 subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
59 break;
60 case SCM_GSUBR_MAKTYPE(1, 0, 0):
61 subr = scm_c_make_subr (name, scm_tc7_subr_1, fcn);
62 break;
63 case SCM_GSUBR_MAKTYPE(0, 1, 0):
64 subr = scm_c_make_subr (name, scm_tc7_subr_1o, fcn);
65 break;
66 case SCM_GSUBR_MAKTYPE(1, 1, 0):
67 subr = scm_c_make_subr (name, scm_tc7_subr_2o, fcn);
68 break;
69 case SCM_GSUBR_MAKTYPE(2, 0, 0):
70 subr = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
71 break;
72 case SCM_GSUBR_MAKTYPE(3, 0, 0):
73 subr = scm_c_make_subr (name, scm_tc7_subr_3, fcn);
74 break;
75 case SCM_GSUBR_MAKTYPE(0, 0, 1):
76 subr = scm_c_make_subr (name, scm_tc7_lsubr, fcn);
77 break;
78 case SCM_GSUBR_MAKTYPE(2, 0, 1):
79 subr = scm_c_make_subr (name, scm_tc7_lsubr_2, fcn);
80 break;
81 default:
82 {
83 unsigned type;
84
85 type = SCM_GSUBR_MAKTYPE (req, opt, rst);
86 if (SCM_GSUBR_REQ (type) != req
87 || SCM_GSUBR_OPT (type) != opt
88 || SCM_GSUBR_REST (type) != rst)
89 scm_out_of_range ("create_gsubr", scm_from_uint (req + opt + rst));
90
91 subr = scm_c_make_subr (name, scm_tc7_gsubr | (type << 8U),
92 fcn);
93 }
94 }
95
96 if (define)
97 scm_define (SCM_SUBR_NAME (subr), subr);
98
99 return subr;
100 }
101
102 SCM
103 scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
104 {
105 return create_gsubr (0, name, req, opt, rst, fcn);
106 }
107
108 SCM
109 scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
110 {
111 return create_gsubr (1, name, req, opt, rst, fcn);
112 }
113
114 static SCM
115 create_gsubr_with_generic (int define,
116 const char *name,
117 int req,
118 int opt,
119 int rst,
120 SCM (*fcn)(),
121 SCM *gf)
122 {
123 SCM subr;
124
125 switch (SCM_GSUBR_MAKTYPE(req, opt, rst))
126 {
127 case SCM_GSUBR_MAKTYPE(0, 0, 0):
128 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_0, fcn, gf);
129 goto create_subr;
130 case SCM_GSUBR_MAKTYPE(1, 0, 0):
131 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1, fcn, gf);
132 goto create_subr;
133 case SCM_GSUBR_MAKTYPE(0, 1, 0):
134 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1o, fcn, gf);
135 goto create_subr;
136 case SCM_GSUBR_MAKTYPE(1, 1, 0):
137 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2o, fcn, gf);
138 goto create_subr;
139 case SCM_GSUBR_MAKTYPE(2, 0, 0):
140 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2, fcn, gf);
141 goto create_subr;
142 case SCM_GSUBR_MAKTYPE(3, 0, 0):
143 subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_3, fcn, gf);
144 goto create_subr;
145 case SCM_GSUBR_MAKTYPE(0, 0, 1):
146 subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr, fcn, gf);
147 goto create_subr;
148 case SCM_GSUBR_MAKTYPE(2, 0, 1):
149 subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr_2, fcn, gf);
150 create_subr:
151 if (define)
152 scm_define (SCM_SUBR_NAME (subr), subr);
153 return subr;
154 default:
155 ;
156 }
157 scm_misc_error ("scm_c_make_gsubr_with_generic",
158 "can't make primitive-generic with this arity",
159 SCM_EOL);
160 return SCM_BOOL_F; /* never reached */
161 }
162
163 SCM
164 scm_c_make_gsubr_with_generic (const char *name,
165 int req,
166 int opt,
167 int rst,
168 SCM (*fcn)(),
169 SCM *gf)
170 {
171 return create_gsubr_with_generic (0, name, req, opt, rst, fcn, gf);
172 }
173
174 SCM
175 scm_c_define_gsubr_with_generic (const char *name,
176 int req,
177 int opt,
178 int rst,
179 SCM (*fcn)(),
180 SCM *gf)
181 {
182 return create_gsubr_with_generic (1, name, req, opt, rst, fcn, gf);
183 }
184
185 /* Apply PROC, a gsubr, to the ARGC arguments in ARGV. ARGC is expected to
186 match the number of arguments of the underlying C function. */
187 static SCM
188 gsubr_apply_raw (SCM proc, unsigned int argc, const SCM *argv)
189 {
190 SCM (*fcn) ();
191 unsigned int type, argc_max;
192
193 type = SCM_GSUBR_TYPE (proc);
194 argc_max = SCM_GSUBR_REQ (type) + SCM_GSUBR_OPT (type)
195 + SCM_GSUBR_REST (type);
196
197 if (SCM_UNLIKELY (argc != argc_max))
198 /* We expect the exact argument count. */
199 scm_wrong_num_args (SCM_SUBR_NAME (proc));
200
201 fcn = SCM_SUBRF (proc);
202
203 switch (argc)
204 {
205 case 0:
206 return (*fcn) ();
207 case 1:
208 return (*fcn) (argv[0]);
209 case 2:
210 return (*fcn) (argv[0], argv[1]);
211 case 3:
212 return (*fcn) (argv[0], argv[1], argv[2]);
213 case 4:
214 return (*fcn) (argv[0], argv[1], argv[2], argv[3]);
215 case 5:
216 return (*fcn) (argv[0], argv[1], argv[2], argv[3], argv[4]);
217 case 6:
218 return (*fcn) (argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
219 case 7:
220 return (*fcn) (argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
221 argv[6]);
222 case 8:
223 return (*fcn) (argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
224 argv[6], argv[7]);
225 case 9:
226 return (*fcn) (argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
227 argv[6], argv[7], argv[8]);
228 case 10:
229 return (*fcn) (argv[0], argv[1], argv[2], argv[3], argv[4], argv[5],
230 argv[6], argv[7], argv[8], argv[9]);
231 default:
232 scm_misc_error ((char *) SCM_SUBR_NAME (proc),
233 "gsubr invocation with more than 10 arguments not implemented",
234 SCM_EOL);
235 }
236
237 return SCM_BOOL_F; /* Never reached. */
238 }
239
240 /* Apply PROC, a gsubr, to the given arguments. Missing optional arguments
241 are added, and rest arguments are turned into a list. */
242 SCM
243 scm_i_gsubr_apply (SCM proc, SCM arg, ...)
244 {
245 unsigned int type, argc, argc_max;
246 SCM *argv;
247 va_list arg_list;
248
249 type = SCM_GSUBR_TYPE (proc);
250 argc_max = SCM_GSUBR_REQ (type) + SCM_GSUBR_OPT (type);
251 argv = alloca ((argc_max + SCM_GSUBR_REST (type)) * sizeof (*argv));
252
253 va_start (arg_list, arg);
254
255 for (argc = 0;
256 !SCM_UNBNDP (arg) && argc < argc_max;
257 argc++, arg = va_arg (arg_list, SCM))
258 argv[argc] = arg;
259
260 if (SCM_UNLIKELY (argc < SCM_GSUBR_REQ (type)))
261 scm_wrong_num_args (SCM_SUBR_NAME (proc));
262
263 /* Fill in optional arguments that were not passed. */
264 while (argc < argc_max)
265 argv[argc++] = SCM_UNDEFINED;
266
267 if (SCM_GSUBR_REST (type))
268 {
269 /* Accumulate rest arguments in a list. */
270 SCM *rest_loc;
271
272 argv[argc_max] = SCM_EOL;
273
274 for (rest_loc = &argv[argc_max];
275 !SCM_UNBNDP (arg);
276 rest_loc = SCM_CDRLOC (*rest_loc), arg = va_arg (arg_list, SCM))
277 *rest_loc = scm_cons (arg, SCM_EOL);
278
279 argc = argc_max + 1;
280 }
281
282 va_end (arg_list);
283
284 return gsubr_apply_raw (proc, argc, argv);
285 }
286
287 /* Apply SELF, a gsubr, to the arguments listed in ARGS. Missing optional
288 arguments are added, and rest arguments are kept into a list. */
289 SCM
290 scm_i_gsubr_apply_list (SCM self, SCM args)
291 #define FUNC_NAME "scm_i_gsubr_apply"
292 {
293 SCM v[SCM_GSUBR_MAX];
294 unsigned int typ = SCM_GSUBR_TYPE (self);
295 long i, n = SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ) + SCM_GSUBR_REST (typ);
296
297 for (i = 0; i < SCM_GSUBR_REQ (typ); i++) {
298 if (scm_is_null (args))
299 scm_wrong_num_args (SCM_SUBR_NAME (self));
300 v[i] = SCM_CAR(args);
301 args = SCM_CDR(args);
302 }
303 for (; i < SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ); i++) {
304 if (SCM_NIMP (args)) {
305 v[i] = SCM_CAR (args);
306 args = SCM_CDR(args);
307 }
308 else
309 v[i] = SCM_UNDEFINED;
310 }
311 if (SCM_GSUBR_REST(typ))
312 v[i] = args;
313 else if (!scm_is_null (args))
314 scm_wrong_num_args (SCM_SUBR_NAME (self));
315
316 return gsubr_apply_raw (self, n, v);
317 }
318 #undef FUNC_NAME
319
320
321 #ifdef GSUBR_TEST
322 /* A silly example, taking 2 required args, 1 optional, and
323 a scm_list of rest args
324 */
325 SCM
326 gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst)
327 {
328 scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp);
329 scm_display(req1, scm_cur_outp);
330 scm_puts ("\n req2: ", scm_cur_outp);
331 scm_display(req2, scm_cur_outp);
332 scm_puts ("\n opt: ", scm_cur_outp);
333 scm_display(opt, scm_cur_outp);
334 scm_puts ("\n rest: ", scm_cur_outp);
335 scm_display(rst, scm_cur_outp);
336 scm_newline(scm_cur_outp);
337 return SCM_UNSPECIFIED;
338 }
339 #endif
340
341
342 void
343 scm_init_gsubr()
344 {
345 #ifdef GSUBR_TEST
346 scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */
347 #endif
348
349 #include "libguile/gsubr.x"
350 }
351
352 /*
353 Local Variables:
354 c-file-style: "gnu"
355 End:
356 */