Debian Codegraph @

src/proc.c

raw
1 /*
2 * proc.c - Procedures
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define LIBGAUCHE_BODY
35 #include "gauche.h"
36 #include "gauche/priv/configP.h"
37 #include "gauche/code.h"
38 #include "gauche/priv/builtin-syms.h"
39 #include "gauche/priv/procP.h"
40
41 /*=================================================================
42 * Classes
43 */
44
45 static void proc_print(ScmObj obj, ScmPort *port, ScmWriteContext *);
46
47 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_ProcedureClass, proc_print);
48
49 static ScmObj print_tag_key = SCM_FALSE; /* set by Scm__InitProc() */
50
51 static void proc_print(ScmObj obj, ScmPort *port,
52 ScmWriteContext *ctx SCM_UNUSED)
53 {
54 /* A procedure can have custom printer in the tag. */
55 ScmObj printer = Scm_Assq(print_tag_key, SCM_PROCEDURE(obj)->tagsAlist);
56 if (SCM_PAIRP(printer)) {
57 Scm_ApplyRec2(SCM_CDR(printer), obj, SCM_OBJ(port));
58 return;
59 }
60
61 /* Standard printer */
62 ScmObj info = SCM_PROCEDURE_INFO(obj);
63 if (SCM_PROCEDURE_TYPE(obj) == SCM_PROC_SUBR) {
64 SCM_PUTZ("#<subr", -1, port);
65 if (!SCM_FALSEP(info)) {
66 Scm_Printf(port, " %S", info);
67 }
68 SCM_PUTC('>', port);
69 } else {
70 Scm_Printf(port, "#<closure %S>", info);
71 }
72 }
73
74 /*=================================================================
75 * Common
76 */
77
78 void Scm__ProcedureInit(ScmProcedure *proc,
79 int type, /* ScmProcedureType */
80 int required,
81 int optional,
82 ScmObj info)
83 {
84 proc->required = required;
85 proc->optional = optional;
86 proc->type = type;
87 proc->locked = FALSE;
88 proc->currying = FALSE;
89 proc->constant = FALSE;
90 proc->leaf = FALSE;
91 proc->reserved = 0;
92 proc->info = info;
93 proc->setter = SCM_FALSE;
94 proc->inliner = SCM_FALSE;
95 #if GAUCHE_API_VERSION >= 98
96 proc->reserved32 = 0;
97 proc->typeHint = SCM_FALSE;
98 proc->tagsAlist = SCM_NIL;
99 #endif /*GAUCHE_API_VERSION >= 98*/
100 }
101
102 /*=================================================================
103 * Closure
104 */
105
106 ScmObj Scm_MakeClosureWithTags(ScmObj code, ScmEnvFrame *env, ScmObj tags)
107 {
108 ScmClosure *c = SCM_NEW(ScmClosure);
109
110 SCM_ASSERT(SCM_COMPILED_CODE(code));
111 /* CODE->signatureInfo can be #f or (<signature> . <other-info>) */
112 ScmObj sig = SCM_COMPILED_CODE(code)->signatureInfo;
113 ScmObj info;
114 if (!SCM_PAIRP(sig)) {
115 /* No info available. */
116 info = Scm_Cons(Scm_CompiledCodeFullName(SCM_COMPILED_CODE(code)),
117 SCM_FALSE);
118 } else {
119 /* We can just use signature info. */
120 info = SCM_CAR(sig);
121 }
122 int req = SCM_COMPILED_CODE_REQUIRED_ARGS(code);
123 int opt = SCM_COMPILED_CODE_OPTIONAL_ARGS(code);
124
125 SCM_SET_CLASS(c, SCM_CLASS_PROCEDURE);
126 Scm__ProcedureInit(SCM_PROCEDURE(c), SCM_PROC_CLOSURE, req, opt, info);
127 c->code = code;
128 c->env = env;
129 SCM_PROCEDURE(c)->inliner = SCM_COMPILED_CODE(code)->intermediateForm;
130 SCM_PROCEDURE(c)->tagsAlist = tags;
131
132 return SCM_OBJ(c);
133 }
134
135 ScmObj Scm_MakeClosure(ScmObj code, ScmEnvFrame *env)
136 {
137 return Scm_MakeClosureWithTags(code, env, SCM_NIL);
138 }
139
140 /*=================================================================
141 * Subr
142 */
143
144 ScmObj Scm_MakeSubrWithTags(ScmSubrProc *func,
145 void *data,
146 int required, int optional,
147 ScmObj info, ScmObj tags)
148 {
149 ScmSubr *s = SCM_NEW(ScmSubr);
150 SCM_SET_CLASS(s, SCM_CLASS_PROCEDURE);
151 Scm__ProcedureInit(SCM_PROCEDURE(s), SCM_PROC_SUBR,
152 required, optional, info);
153 s->func = func;
154 s->data = data;
155 SCM_PROCEDURE(s)->tagsAlist = tags;
156 return SCM_OBJ(s);
157 }
158
159 ScmObj Scm_MakeSubr(ScmSubrProc *func,
160 void *data,
161 int required, int optional,
162 ScmObj info)
163 {
164 return Scm_MakeSubrWithTags(func, data, required, optional, info, SCM_NIL);
165 }
166
167 /*
168 * A dummy function which does nothing. Convenient to pass to other
169 * functions which requires a thunk.
170 */
171 static ScmObj theNullProc = SCM_NIL;
172
173 static ScmObj null_proc(ScmObj *args SCM_UNUSED,
174 int nargs SCM_UNUSED,
175 void *data SCM_UNUSED)
176 {
177 return SCM_UNDEFINED;
178 }
179
180 ScmObj Scm_NullProc(void)
181 {
182 if (SCM_NULLP(theNullProc)) {
183 theNullProc = Scm_MakeSubr(null_proc, NULL, 0, 1,
184 SCM_MAKE_STR("nullproc"));
185 }
186 return SCM_OBJ(theNullProc);
187 }
188
189 /*=================================================================
190 * Copying
191 */
192
193 /* Procedure is inherently an immutable entity, so copying doesn't
194 make much sense. However, sometimes we do need to mutate procedure
195 instances to add various bookkeeping info during construction,
196 and it comes handy to do "copy, then modify" workflow. */
197 ScmObj Scm__CopyProcedure(ScmProcedure *proc , ScmObj tagsAlist)
198 {
199 ScmObj n = SCM_UNDEFINED;
200
201 switch (proc->type) {
202 case SCM_PROC_SUBR:
203 n = Scm_MakeSubr(SCM_SUBR_FUNC(proc),
204 SCM_SUBR_DATA(proc),
205 SCM_PROCEDURE_REQUIRED(proc),
206 SCM_PROCEDURE_OPTIONAL(proc),
207 SCM_PROCEDURE_INFO(proc));
208 SCM_SUBR_FLAGS(n) = SCM_SUBR_FLAGS(proc);
209 break;
210 case SCM_PROC_CLOSURE:
211 n = Scm_MakeClosure(SCM_CLOSURE_CODE(proc),
212 SCM_CLOSURE_ENV(proc));
213 break;
214 case SCM_PROC_GENERIC:
215 case SCM_PROC_METHOD:
216 case SCM_PROC_NEXT_METHOD:
217 /* CLOS is inherently mutable and relies on object identity;
218 copying won't make much sense. */
219 Scm_Error("procedure-copy can only copy subr or closure: %S",
220 SCM_OBJ(proc));
221 }
222 /* Copying common fields */
223 SCM_PROCEDURE_INLINER(n) = SCM_PROCEDURE_INLINER(proc);
224 SCM_PROCEDURE_SETTER(n) = SCM_PROCEDURE_SETTER(proc);
225 SCM_PROCEDURE_SETTER_LOCKED(n) = SCM_PROCEDURE_SETTER_LOCKED(proc);
226 SCM_PROCEDURE_CURRYING(n) = SCM_PROCEDURE_CURRYING(proc);
227 #if GAUCHE_API_VERSION >= 98
228 SCM_PROCEDURE(n)->reserved32 = proc->reserved32;
229 SCM_PROCEDURE(n)->typeHint = proc->typeHint;
230 if (SCM_FALSEP(tagsAlist)) {
231 SCM_PROCEDURE(n)->tagsAlist = proc->tagsAlist;
232 } else {
233 SCM_ASSERT(SCM_LISTP(tagsAlist));
234 SCM_PROCEDURE(n)->tagsAlist = tagsAlist;
235 }
236 #endif /*GAUCHE_API_VERSION >= 98*/
237 return n;
238 }
239
240 /*=================================================================
241 * Currying
242 */
243
244 /* NB: This code doesn't work yet if the original procedure takes
245 variable length arguments. We disable this feature for now. */
246
247 #if 0
248
249 /* When a procedure is marked autocurrying and it is given with args less
250 * than reqargs, Scm_CurryProcedure is called. It saves the given args
251 * into the curried_packet and creates a subr of curried procedure.
252 *
253 * The argument GIVEN points the beginning of argument array. It actually
254 * points into the VM stack, so we should copy the information before
255 * calling anything that might change the VM state.
256 * The # of given argument is indicated by NGIVEN.
257 *
258 * Note that some of the given arguments are already folded into a list
259 * (in case we are called via APPLY).
260 *
261 * when FOLDLEN < 0 when FOLDLEN >= 0
262 * N = NGIVEN-1 N = NGIVEN-1
263 * K = NGIVEN-1-FOLDLEN
264 *
265 * | argN | | ----> (argK argK+1 ... argN)
266 * : : : :
267 * | arg1 | | arg1 |
268 * given >| arg0 | given >| arg0 |
269 *
270 * We assume at least one arg should be given in order to curry, and
271 * ngiven should always be smaller than the # of required args (otherwise
272 * we don't need to curry at all!). Thus 0 < NGIVEN < REQUIRED.
273 */
274 typedef struct curried_packet_rec {
275 ScmObj proc;
276 int ngiven;
277 ScmObj argv[4]; /* keep first 4 args unfolded */
278 ScmObj more; /* the rest of args */
279 } curried_packet;
280
281 static ScmObj kick_curried(ScmObj *args, int nargs, void *data)
282 {
283 curried_packet *p = (curried_packet*)data;
284 ScmObj proc = p->proc;
285 ScmObj *av = p->argv;
286 SCM_ASSERT(p->ngiven + nargs >= 2);
287
288 /* TODO: if p->proc takes variable length arguments, we shouldn't use
289 Scm_VMApply* family below, since the last word of args already
290 contains folded arguments, and Scm_VMApply causes to fold the
291 arguments again.
292 */
293
294 switch (p->ngiven + nargs) {
295 case 2:
296 return Scm_VMApply2(proc, p->argv[0], args[0]);
297 case 3:
298 switch (nargs) {
299 case 1: return Scm_VMApply3(proc, av[0], av[1], args[0]);
300 case 2: return Scm_VMApply3(proc, av[0], args[0], args[1]);
301 default: break; /*NOTREACHED*/
302 }
303 case 4:
304 switch (nargs) {
305 case 1: return Scm_VMApply4(proc, av[0], av[1], av[2], args[0]);
306 case 2: return Scm_VMApply4(proc, av[0], av[1], args[0], args[1]);
307 case 3: return Scm_VMApply4(proc, av[0], args[0], args[1], args[2]);
308 default: break; /*NOTREACHED*/
309 }
310 default:
311 {
312 ScmObj h = SCM_NIL, t = SCM_NIL;
313 for (int i = 0; i < p->ngiven; i++) SCM_APPEND1(h, t, av[i]);
314 if (SCM_PAIRP(p->more)) SCM_APPEND(h, t, Scm_CopyList(p->more));
315 for (int i = 0; i < nargs; i++) SCM_APPEND1(h, t, args[i]);
316 return Scm_VMApply(proc, h);
317 }
318 }
319 return SCM_UNDEFINED; /* dummy */
320 }
321
322 ScmObj Scm_CurryProcedure(ScmObj proc, ScmObj *given, int ngiven, int foldlen)
323 {
324 int required = SCM_PROCEDURE_REQUIRED(proc);
325 int n = ngiven - foldlen;
326 ScmObj h = SCM_NIL, t = SCM_NIL;
327 ScmObj restarg = (foldlen > 0)? given[n] : SCM_NIL;
328
329 SCM_ASSERT(SCM_PROCEDUREP(proc));
330 SCM_ASSERT(ngiven < required && ngiven > 0);
331 curried_packet *packet = SCM_NEW(curried_packet);
332 packet->proc = proc;
333 packet->ngiven = ngiven;
334
335 /* pack the given args into the packet */
336 switch (n) {
337 default: packet->argv[3] = given[3]; /*FALLTHROUGH*/
338 case 3: packet->argv[2] = given[2]; /*FALLTHROUGH*/
339 case 2: packet->argv[1] = given[1]; /*FALLTHROUGH*/
340 case 1: packet->argv[0] = given[0]; /*FALLTHROUGH*/
341 }
342 if (foldlen > 0) {
343 for (int i=n; i<4 && SCM_PAIRP(restarg); i++, restarg = SCM_CDR(restarg)) {
344 packet->argv[i] = SCM_CAR(restarg);
345 }
346 }
347 for (int i=4; i<n; i++) {
348 SCM_APPEND1(h, t, given[i]);
349 }
350 if (SCM_PAIRP(restarg)) {
351 SCM_APPEND(h, t, Scm_CopyList(restarg));
352 }
353 packet->more = h;
354
355 ScmObj subr = Scm_MakeSubr(kick_curried, (void*)packet,
356 required - ngiven, SCM_PROCEDURE_OPTIONAL(proc),
357 Scm_Cons(SCM_SYM_CURRIED, SCM_PROCEDURE_INFO(proc)));
358 SCM_PROCEDURE_CURRYING(subr) = TRUE;
359 return subr;
360 }
361
362 #endif /* 0 : disabling currying feature for now */
363
364 /*=================================================================
365 * Mapper family
366 * OBSOLETED - map and for-each is defiend in Scheme now (liblist.scm)
367 * These are just kept for the backward compatibility. Will be gone
368 * in 1.0.
369 */
370
371 #if GAUCHE_API_VERSION <= 97
372
373 ScmObj Scm_ForEach1(ScmObj proc, ScmObj args)
374 {
375 return Scm_ForEach(proc, args, SCM_NIL);
376 }
377
378 ScmObj Scm_Map1(ScmObj proc, ScmObj args)
379 {
380 return Scm_Map(proc, args, SCM_NIL);
381 }
382
383 ScmObj Scm_ForEach(ScmObj proc, ScmObj arg1, ScmObj args)
384 {
385 static ScmObj stub = SCM_UNDEFINED;
386 SCM_BIND_PROC(stub, "for-each", Scm_SchemeModule());
387 return Scm_VMApply(stub, Scm_Cons(proc, Scm_Cons(arg1, args)));
388 }
389
390 ScmObj Scm_Map(ScmObj proc, ScmObj arg1, ScmObj args)
391 {
392 static ScmObj stub = SCM_UNDEFINED;
393 SCM_BIND_PROC(stub, "map", Scm_SchemeModule());
394 return Scm_VMApply(stub, Scm_Cons(proc, Scm_Cons(arg1, args)));
395 }
396
397 #endif /*GAUCHE_API_VERSION <= 97*/
398
399 /*=================================================================
400 * Generic setter
401 */
402
403 ScmObj Scm_SetterSet(ScmProcedure *proc, ScmProcedure *setter, int lock)
404 {
405 if (SCM_PROCEDURE_SETTER_LOCKED(proc)) {
406 Scm_Error("can't change the locked setter of procedure %S", proc);
407 }
408 proc->setter = SCM_OBJ(setter);
409 proc->locked = lock;
410 return SCM_OBJ(proc);
411 }
412
413 static ScmObj object_setter(ScmObj *args, int nargs, void *data)
414 {
415 SCM_ASSERT(nargs == 1);
416 return Scm_VMApply(SCM_OBJ(&Scm_GenericObjectSetter),
417 Scm_Cons(SCM_OBJ(data), args[0]));
418 }
419
420 static SCM_DEFINE_STRING_CONST(object_setter__NAME, "object-setter", 13, 13);
421
422 ScmObj Scm_Setter(ScmObj proc)
423 {
424 if (SCM_PROCEDUREP(proc)) {
425 /* NB: This used to signal an error if no setter procedure is associated
426 to proc; now it returns #f in such case */
427 return SCM_PROCEDURE(proc)->setter;
428 } else {
429 /* fallback to (setter object-apply) */
430 return Scm_MakeSubr(object_setter, (void*)proc, 0, 1,
431 SCM_OBJ(&object_setter__NAME));
432 }
433 }
434
435 int Scm_HasSetter(ScmObj proc)
436 {
437 if (SCM_PROCEDUREP(proc)) {
438 return !SCM_FALSEP(SCM_PROCEDURE(proc)->setter);
439 } else {
440 /* setter of object-apply is used. */
441 return TRUE;
442 }
443 }
444
445 /*=================================================================
446 * Scheme-level accessors
447 */
448 static ScmObj proc_required(ScmProcedure *p)
449 {
450 return SCM_MAKE_INT(p->required);
451 }
452
453 static ScmObj proc_optional(ScmProcedure *p)
454 {
455 return SCM_MAKE_BOOL(p->optional); /* for backward compatibility */
456 }
457
458 static ScmObj proc_optcount(ScmProcedure *p)
459 {
460 return SCM_MAKE_INT(p->optional);
461 }
462
463 static ScmObj proc_locked(ScmProcedure *p)
464 {
465 return SCM_MAKE_BOOL(SCM_PROCEDURE_SETTER_LOCKED(p));
466 }
467
468 static ScmObj proc_currying(ScmProcedure *p)
469 {
470 return SCM_MAKE_BOOL(p->currying);
471 }
472
473 static ScmObj proc_constant(ScmProcedure *p)
474 {
475 return SCM_MAKE_BOOL(p->constant);
476 }
477
478 static ScmObj proc_info(ScmProcedure *p)
479 {
480 return p->info;
481 }
482
483 static ScmObj proc_setter(ScmProcedure *p)
484 {
485 return p->setter;
486 }
487
488 static ScmClassStaticSlotSpec proc_slots[] = {
489 SCM_CLASS_SLOT_SPEC("required", proc_required, NULL),
490 SCM_CLASS_SLOT_SPEC("optional", proc_optional, NULL),
491 SCM_CLASS_SLOT_SPEC("optcount", proc_optcount, NULL),
492 SCM_CLASS_SLOT_SPEC("locked", proc_locked, NULL),
493 SCM_CLASS_SLOT_SPEC("currying", proc_currying, NULL),
494 SCM_CLASS_SLOT_SPEC("constant", proc_constant, NULL),
495 SCM_CLASS_SLOT_SPEC("info", proc_info, NULL),
496 SCM_CLASS_SLOT_SPEC("setter", proc_setter, NULL),
497 SCM_CLASS_SLOT_SPEC_END()
498 };
499
500
501 /*=================================================================
502 * Initialization
503 */
504 void Scm__InitProc(void)
505 {
506 Scm_InitStaticClass(&Scm_ProcedureClass, "<procedure>",
507 Scm_GaucheModule(), proc_slots, 0);
508 Scm_ProcedureClass.flags |= SCM_CLASS_APPLICABLE;
509
510 print_tag_key = Scm_MakeSymbol(SCM_STRING(SCM_MAKE_STR("print")), FALSE);
511 }