* list.h (scm_list_1, scm_list_2, scm_list_3, scm_list_4, scm_list_5,
[bpt/guile.git] / libguile / objects.c
1 /* Copyright (C) 1995,1996,1999,2000,2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 /* This file and objects.h contains those minimal pieces of the Guile
48 * Object Oriented Programming System which need to be included in
49 * libguile. See the comments in objects.h.
50 */
51
52 #include "libguile/_scm.h"
53
54 #include "libguile/struct.h"
55 #include "libguile/procprop.h"
56 #include "libguile/chars.h"
57 #include "libguile/keywords.h"
58 #include "libguile/smob.h"
59 #include "libguile/eval.h"
60 #include "libguile/alist.h"
61 #include "libguile/ports.h"
62 #include "libguile/strings.h"
63 #include "libguile/vectors.h"
64
65 #include "libguile/validate.h"
66 #include "libguile/objects.h"
67 \f
68
69 SCM scm_metaclass_standard;
70 SCM scm_metaclass_operator;
71
72 /* These variables are filled in by the object system when loaded. */
73 SCM scm_class_boolean, scm_class_char, scm_class_pair;
74 SCM scm_class_procedure, scm_class_string, scm_class_symbol;
75 SCM scm_class_procedure_with_setter, scm_class_primitive_generic;
76 SCM scm_class_vector, scm_class_null;
77 SCM scm_class_integer, scm_class_real, scm_class_complex;
78 SCM scm_class_unknown;
79
80 SCM *scm_port_class = 0;
81 SCM *scm_smob_class = 0;
82
83 SCM scm_no_applicable_method;
84
85 /* This function is used for efficient type dispatch. */
86 SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
87 (SCM x),
88 "Return the class of @var{x}.")
89 #define FUNC_NAME s_scm_class_of
90 {
91 switch (SCM_ITAG3 (x))
92 {
93 case scm_tc3_int_1:
94 case scm_tc3_int_2:
95 return scm_class_integer;
96
97 case scm_tc3_imm24:
98 if (SCM_CHARP (x))
99 return scm_class_char;
100 else
101 {
102 switch (SCM_ISYMNUM (x))
103 {
104 case SCM_ISYMNUM (SCM_BOOL_F):
105 case SCM_ISYMNUM (SCM_BOOL_T):
106 return scm_class_boolean;
107 case SCM_ISYMNUM (SCM_EOL):
108 return scm_class_null;
109 default:
110 return scm_class_unknown;
111 }
112 }
113
114 case scm_tc3_cons:
115 switch (SCM_TYP7 (x))
116 {
117 case scm_tcs_cons_nimcar:
118 return scm_class_pair;
119 case scm_tcs_closures:
120 return scm_class_procedure;
121 case scm_tc7_symbol:
122 return scm_class_symbol;
123 case scm_tc7_vector:
124 case scm_tc7_wvect:
125 #ifdef HAVE_ARRAYS
126 case scm_tc7_bvect:
127 case scm_tc7_byvect:
128 case scm_tc7_svect:
129 case scm_tc7_ivect:
130 case scm_tc7_uvect:
131 case scm_tc7_fvect:
132 case scm_tc7_dvect:
133 case scm_tc7_cvect:
134 #endif
135 return scm_class_vector;
136 case scm_tc7_string:
137 case scm_tc7_substring:
138 return scm_class_string;
139 case scm_tc7_asubr:
140 case scm_tc7_subr_0:
141 case scm_tc7_subr_1:
142 case scm_tc7_cxr:
143 case scm_tc7_subr_3:
144 case scm_tc7_subr_2:
145 case scm_tc7_rpsubr:
146 case scm_tc7_subr_1o:
147 case scm_tc7_subr_2o:
148 case scm_tc7_lsubr_2:
149 case scm_tc7_lsubr:
150 if (SCM_SUBR_GENERIC (x) && *SCM_SUBR_GENERIC (x))
151 return scm_class_primitive_generic;
152 else
153 return scm_class_procedure;
154 case scm_tc7_cclo:
155 return scm_class_procedure;
156 case scm_tc7_pws:
157 return scm_class_procedure_with_setter;
158
159 case scm_tc7_smob:
160 {
161 scm_t_bits type = SCM_TYP16 (x);
162 if (type != scm_tc16_port_with_ps)
163 return scm_smob_class[SCM_TC2SMOBNUM (type)];
164 x = SCM_PORT_WITH_PS_PORT (x);
165 /* fall through to ports */
166 }
167 case scm_tc7_port:
168 return scm_port_class[(SCM_WRTNG & SCM_CELL_WORD_0 (x)
169 ? (SCM_RDNG & SCM_CELL_WORD_0 (x)
170 ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
171 : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
172 : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
173 case scm_tcs_cons_gloc:
174 /* must be a struct */
175 if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
176 return SCM_CLASS_OF (x);
177 else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
178 {
179 /* Goops object */
180 if (! SCM_FALSEP (SCM_OBJ_CLASS_REDEF (x)))
181 scm_change_object_class (x,
182 SCM_CLASS_OF (x), /* old */
183 SCM_OBJ_CLASS_REDEF (x)); /* new */
184 return SCM_CLASS_OF (x);
185 }
186 else
187 {
188 /* ordinary struct */
189 SCM handle = scm_struct_create_handle (SCM_STRUCT_VTABLE (x));
190 if (!SCM_FALSEP (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle))))
191 return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle));
192 else
193 {
194 SCM name = SCM_STRUCT_TABLE_NAME (SCM_CDR (handle));
195 SCM class = scm_make_extended_class (!SCM_FALSEP (name)
196 ? SCM_SYMBOL_CHARS (name)
197 : 0);
198 SCM_SET_STRUCT_TABLE_CLASS (SCM_CDR (handle), class);
199 return class;
200 }
201 }
202 default:
203 if (SCM_CONSP (x))
204 return scm_class_pair;
205 else
206 return scm_class_unknown;
207 }
208
209 case scm_tc3_cons_gloc:
210 case scm_tc3_tc7_1:
211 case scm_tc3_tc7_2:
212 case scm_tc3_closure:
213 /* Never reached */
214 break;
215 }
216 return scm_class_unknown;
217 }
218 #undef FUNC_NAME
219
220 /* The cache argument for scm_mcache_lookup_cmethod has one of two possible
221 * formats:
222 *
223 * Format #1:
224 * (SCM_IM_DISPATCH ARGS N-SPECIALIZED
225 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
226 * GF)
227 *
228 * Format #2:
229 * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK
230 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
231 * GF)
232 *
233 * ARGS is either a list of expressions, in which case they
234 * are interpreted as the arguments of an application, or
235 * a non-pair, which is interpreted as a single expression
236 * yielding all arguments.
237 *
238 * SCM_IM_DISPATCH expressions in generic functions always
239 * have ARGS = the symbol `args' or the iloc #@0-0.
240 *
241 * Need FORMALS in order to support varying arity. This
242 * also avoids the need for renaming of bindings.
243 *
244 * We should probably not complicate this mechanism by
245 * introducing "optimizations" for getters and setters or
246 * primitive methods. Getters and setter will normally be
247 * compiled into @slot-[ref|set!] or a procedure call.
248 * They rely on the dispatch performed before executing
249 * the code which contains them.
250 *
251 * We might want to use a more efficient representation of
252 * this form in the future, perhaps after we have introduced
253 * low-level support for syntax-case macros.
254 */
255
256 SCM
257 scm_mcache_lookup_cmethod (SCM cache, SCM args)
258 {
259 long i, n, end, mask;
260 SCM ls, methods, z = SCM_CDDR (cache);
261 n = SCM_INUM (SCM_CAR (z)); /* maximum number of specializers */
262 methods = SCM_CADR (z);
263
264 if (SCM_INUMP (methods))
265 {
266 /* cache format #2: compute a hash value */
267 long hashset = SCM_INUM (methods);
268 long j = n;
269 z = SCM_CDDR (z);
270 mask = SCM_INUM (SCM_CAR (z));
271 methods = SCM_CADR (z);
272 i = 0;
273 ls = args;
274 if (!SCM_NULLP (ls))
275 do
276 {
277 i += SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls)))
278 [scm_si_hashsets + hashset];
279 ls = SCM_CDR (ls);
280 }
281 while (j-- && !SCM_NULLP (ls));
282 i &= mask;
283 end = i;
284 }
285 else /* SCM_VECTORP (methods) */
286 {
287 /* cache format #1: prepare for linear search */
288 mask = -1;
289 i = 0;
290 end = SCM_VECTOR_LENGTH (methods);
291 }
292
293 /* Search for match */
294 do
295 {
296 long j = n;
297 z = SCM_VELTS (methods)[i];
298 ls = args; /* list of arguments */
299 if (!SCM_NULLP (ls))
300 do
301 {
302 /* More arguments than specifiers => CLASS != ENV */
303 if (! SCM_EQ_P (scm_class_of (SCM_CAR (ls)), SCM_CAR (z)))
304 goto next_method;
305 ls = SCM_CDR (ls);
306 z = SCM_CDR (z);
307 }
308 while (j-- && !SCM_NULLP (ls));
309 /* Fewer arguments than specifiers => CAR != ENV */
310 if (SCM_NULLP (SCM_CAR (z)) || SCM_CONSP (SCM_CAR (z)))
311 return z;
312 next_method:
313 i = (i + 1) & mask;
314 } while (i != end);
315 return SCM_BOOL_F;
316 }
317
318 SCM
319 scm_mcache_compute_cmethod (SCM cache, SCM args)
320 {
321 SCM cmethod = scm_mcache_lookup_cmethod (cache, args);
322 if (SCM_FALSEP (cmethod))
323 /* No match - memoize */
324 return scm_memoize_method (cache, args);
325 return cmethod;
326 }
327
328 SCM
329 scm_apply_generic (SCM gf, SCM args)
330 {
331 SCM cmethod = scm_mcache_compute_cmethod (SCM_ENTITY_PROCEDURE (gf), args);
332 return scm_eval_body (SCM_CDR (SCM_CMETHOD_CODE (cmethod)),
333 SCM_EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (cmethod)),
334 args,
335 SCM_CMETHOD_ENV (cmethod)));
336 }
337
338 SCM
339 scm_call_generic_0 (SCM gf)
340 {
341 return scm_apply_generic (gf, SCM_EOL);
342 }
343
344 SCM
345 scm_call_generic_1 (SCM gf, SCM a1)
346 {
347 return scm_apply_generic (gf, scm_list_1 (a1));
348 }
349
350 SCM
351 scm_call_generic_2 (SCM gf, SCM a1, SCM a2)
352 {
353 return scm_apply_generic (gf, scm_list_2 (a1, a2));
354 }
355
356 SCM
357 scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3)
358 {
359 return scm_apply_generic (gf, scm_list_3 (a1, a2, a3));
360 }
361
362 SCM_DEFINE (scm_entity_p, "entity?", 1, 0, 0,
363 (SCM obj),
364 "Return @code{#t} if @var{obj} is an entity.")
365 #define FUNC_NAME s_scm_entity_p
366 {
367 return SCM_BOOL(SCM_STRUCTP (obj) && SCM_I_ENTITYP (obj));
368 }
369 #undef FUNC_NAME
370
371 SCM_DEFINE (scm_operator_p, "operator?", 1, 0, 0,
372 (SCM obj),
373 "Return @code{#t} if @var{obj} is an operator.")
374 #define FUNC_NAME s_scm_operator_p
375 {
376 return SCM_BOOL(SCM_STRUCTP (obj)
377 && SCM_I_OPERATORP (obj)
378 && !SCM_I_ENTITYP (obj));
379 }
380 #undef FUNC_NAME
381
382 /* XXX - What code requires the object procedure to be only of certain
383 types? */
384
385 SCM_DEFINE (scm_valid_object_procedure_p, "valid-object-procedure?", 1, 0, 0,
386 (SCM proc),
387 "Return @code{#t} iff @var{proc} is a procedure that can be used "
388 "with @code{set-object-procedure}. It is always valid to use "
389 "a closure constructed by @code{lambda}.")
390 #define FUNC_NAME s_scm_valid_object_procedure_p
391 {
392 if (SCM_IMP (proc))
393 return SCM_BOOL_F;
394 switch (SCM_TYP7 (proc))
395 {
396 default:
397 return SCM_BOOL_F;
398 case scm_tcs_closures:
399 case scm_tc7_subr_1:
400 case scm_tc7_subr_2:
401 case scm_tc7_subr_3:
402 case scm_tc7_lsubr_2:
403 return SCM_BOOL_T;
404 }
405 }
406 #undef FUNC_NAME
407
408 SCM_DEFINE (scm_set_object_procedure_x, "set-object-procedure!", 2, 0, 0,
409 (SCM obj, SCM proc),
410 "Set the object procedure of @var{obj} to @var{proc}.\n"
411 "@var{obj} must be either an entity or an operator.")
412 #define FUNC_NAME s_scm_set_object_procedure_x
413 {
414 SCM_ASSERT (SCM_STRUCTP (obj)
415 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
416 || (SCM_I_ENTITYP (obj)
417 && !(SCM_OBJ_CLASS_FLAGS (obj)
418 & SCM_CLASSF_PURE_GENERIC))),
419 obj,
420 SCM_ARG1,
421 FUNC_NAME);
422 SCM_ASSERT (scm_valid_object_procedure_p (proc), proc, SCM_ARG2, FUNC_NAME);
423 if (SCM_I_ENTITYP (obj))
424 SCM_SET_ENTITY_PROCEDURE (obj, proc);
425 else
426 SCM_OPERATOR_CLASS (obj)->procedure = proc;
427 return SCM_UNSPECIFIED;
428 }
429 #undef FUNC_NAME
430
431 #ifdef GUILE_DEBUG
432 SCM_DEFINE (scm_object_procedure, "object-procedure", 1, 0, 0,
433 (SCM obj),
434 "Return the object procedure of @var{obj}. @var{obj} must be\n"
435 "an entity or an operator.")
436 #define FUNC_NAME s_scm_object_procedure
437 {
438 SCM_ASSERT (SCM_STRUCTP (obj)
439 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
440 || SCM_I_ENTITYP (obj)),
441 obj, SCM_ARG1, FUNC_NAME);
442 return (SCM_I_ENTITYP (obj)
443 ? SCM_ENTITY_PROCEDURE (obj)
444 : SCM_OPERATOR_CLASS (obj)->procedure);
445 }
446 #undef FUNC_NAME
447 #endif /* GUILE_DEBUG */
448
449 /* The following procedures are not a part of Goops but a minimal
450 * object system built upon structs. They are here for those who
451 * want to implement their own object system.
452 */
453
454 SCM
455 scm_i_make_class_object (SCM meta,
456 SCM layout_string,
457 unsigned long flags)
458 {
459 SCM c;
460 SCM layout = scm_make_struct_layout (layout_string);
461 c = scm_make_struct (meta,
462 SCM_INUM0,
463 scm_list_4 (layout, SCM_BOOL_F, SCM_EOL, SCM_EOL));
464 SCM_SET_CLASS_FLAGS (c, flags);
465 return c;
466 }
467
468 SCM_DEFINE (scm_make_class_object, "make-class-object", 2, 0, 0,
469 (SCM metaclass, SCM layout),
470 "Create a new class object of class @var{metaclass}, with the\n"
471 "slot layout specified by @var{layout}.")
472 #define FUNC_NAME s_scm_make_class_object
473 {
474 unsigned long flags = 0;
475 SCM_VALIDATE_STRUCT (1,metaclass);
476 SCM_VALIDATE_STRING (2,layout);
477 if (SCM_EQ_P (metaclass, scm_metaclass_operator))
478 flags = SCM_CLASSF_OPERATOR;
479 return scm_i_make_class_object (metaclass, layout, flags);
480 }
481 #undef FUNC_NAME
482
483 SCM_DEFINE (scm_make_subclass_object, "make-subclass-object", 2, 0, 0,
484 (SCM class, SCM layout),
485 "Create a subclass object of @var{class}, with the slot layout\n"
486 "specified by @var{layout}.")
487 #define FUNC_NAME s_scm_make_subclass_object
488 {
489 SCM pl;
490 SCM_VALIDATE_STRUCT (1,class);
491 SCM_VALIDATE_STRING (2,layout);
492 pl = SCM_PACK (SCM_STRUCT_DATA (class) [scm_vtable_index_layout]);
493 /* Convert symbol->string */
494 pl = scm_mem2string (SCM_SYMBOL_CHARS (pl), SCM_SYMBOL_LENGTH (pl));
495 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
496 scm_string_append (scm_list_2 (pl, layout)),
497 SCM_CLASS_FLAGS (class));
498 }
499 #undef FUNC_NAME
500
501 void
502 scm_init_objects ()
503 {
504 SCM ms = scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT);
505 SCM mt = scm_make_vtable_vtable (ms, SCM_INUM0,
506 scm_list_3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
507
508 SCM os = scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT);
509 SCM ot = scm_make_vtable_vtable (os, SCM_INUM0,
510 scm_list_3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
511
512 SCM es = scm_makfrom0str (SCM_ENTITY_LAYOUT);
513 SCM el = scm_make_struct_layout (es);
514 SCM et = scm_make_struct (mt, SCM_INUM0,
515 scm_list_4 (el, SCM_BOOL_F, SCM_EOL, SCM_EOL));
516
517 scm_c_define ("<class>", mt);
518 scm_metaclass_standard = mt;
519 scm_c_define ("<operator-class>", ot);
520 scm_metaclass_operator = ot;
521 SCM_SET_CLASS_FLAGS (et, SCM_CLASSF_OPERATOR | SCM_CLASSF_ENTITY);
522 SCM_SET_CLASS_DESTRUCTOR (et, scm_struct_free_entity);
523 scm_c_define ("<entity>", et);
524
525 #ifndef SCM_MAGIC_SNARFER
526 #include "libguile/objects.x"
527 #endif
528 }
529
530 /*
531 Local Variables:
532 c-file-style: "gnu"
533 End:
534 */