deprecate scm_internal_dynamic_wind
[bpt/guile.git] / libguile / goops.c
... / ...
CommitLineData
1/* Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2008,2009,2010,2011
2 * Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19\f
20
21/* This software is a derivative work of other copyrighted softwares; the
22 * copyright notices of these softwares are placed in the file COPYRIGHTS
23 *
24 * This file is based upon stklos.c from the STk distribution by
25 * Erick Gallesio <eg@unice.fr>.
26 */
27
28#ifdef HAVE_CONFIG_H
29# include <config.h>
30#endif
31
32#include <stdio.h>
33
34#include "libguile/_scm.h"
35#include "libguile/alist.h"
36#include "libguile/async.h"
37#include "libguile/chars.h"
38#include "libguile/debug.h"
39#include "libguile/dynl.h"
40#include "libguile/dynwind.h"
41#include "libguile/eval.h"
42#include "libguile/gsubr.h"
43#include "libguile/hashtab.h"
44#include "libguile/keywords.h"
45#include "libguile/macros.h"
46#include "libguile/modules.h"
47#include "libguile/ports.h"
48#include "libguile/procprop.h"
49#include "libguile/programs.h"
50#include "libguile/random.h"
51#include "libguile/root.h"
52#include "libguile/smob.h"
53#include "libguile/strings.h"
54#include "libguile/strports.h"
55#include "libguile/vectors.h"
56#include "libguile/weaks.h"
57#include "libguile/vm.h"
58
59#include "libguile/validate.h"
60#include "libguile/goops.h"
61
62#define SPEC_OF(x) SCM_SLOT (x, scm_si_specializers)
63
64/* Port classes */
65#define SCM_IN_PCLASS_INDEX 0
66#define SCM_OUT_PCLASS_INDEX SCM_I_MAX_PORT_TYPE_COUNT
67#define SCM_INOUT_PCLASS_INDEX (2 * SCM_I_MAX_PORT_TYPE_COUNT)
68
69/* this file is a mess. in theory, though, we shouldn't have many SCM references
70 -- most of the references should be to vars. */
71
72static SCM var_slot_unbound = SCM_BOOL_F;
73static SCM var_slot_missing = SCM_BOOL_F;
74static SCM var_compute_cpl = SCM_BOOL_F;
75static SCM var_no_applicable_method = SCM_BOOL_F;
76static SCM var_change_class = SCM_BOOL_F;
77
78SCM_SYMBOL (sym_slot_unbound, "slot-unbound");
79SCM_SYMBOL (sym_slot_missing, "slot-missing");
80SCM_SYMBOL (sym_compute_cpl, "compute-cpl");
81SCM_SYMBOL (sym_no_applicable_method, "no-applicable-method");
82SCM_SYMBOL (sym_memoize_method_x, "memoize-method!");
83SCM_SYMBOL (sym_change_class, "change-class");
84
85SCM_VARIABLE (scm_var_make_extended_generic, "make-extended-generic");
86
87
88/* FIXME, exports should come from the scm file only */
89#define DEFVAR(v, val) \
90 { scm_module_define (scm_module_goops, (v), (val)); \
91 scm_module_export (scm_module_goops, scm_list_1 ((v))); \
92 }
93
94
95/* Class redefinition protocol:
96
97 A class is represented by a heap header h1 which points to a
98 malloc:ed memory block m1.
99
100 When a new version of a class is created, a new header h2 and
101 memory block m2 are allocated. The headers h1 and h2 then switch
102 pointers so that h1 refers to m2 and h2 to m1. In this way, names
103 bound to h1 will point to the new class at the same time as h2 will
104 be a handle which the GC will use to free m1.
105
106 The `redefined' slot of m1 will be set to point to h1. An old
107 instance will have its class pointer (the CAR of the heap header)
108 pointing to m1. The non-immediate `redefined'-slot in m1 indicates
109 the class modification and the new class pointer can be found via
110 h1.
111*/
112
113#define TEST_CHANGE_CLASS(obj, class) \
114 { \
115 class = SCM_CLASS_OF (obj); \
116 if (scm_is_true (SCM_OBJ_CLASS_REDEF (obj))) \
117 { \
118 scm_change_object_class (obj, class, SCM_OBJ_CLASS_REDEF (obj));\
119 class = SCM_CLASS_OF (obj); \
120 } \
121 }
122
123#define NXT_MTHD_METHODS(m) (SCM_VELTS (m)[1])
124#define NXT_MTHD_ARGS(m) (SCM_VELTS (m)[2])
125
126#define SCM_GOOPS_UNBOUND SCM_UNBOUND
127#define SCM_GOOPS_UNBOUNDP(x) ((x) == SCM_GOOPS_UNBOUND)
128
129static int goops_loaded_p = 0;
130static scm_t_rstate *goops_rstate;
131
132/* These variables are filled in by the object system when loaded. */
133SCM scm_class_boolean, scm_class_char, scm_class_pair;
134SCM scm_class_procedure, scm_class_string, scm_class_symbol;
135SCM scm_class_primitive_generic;
136SCM scm_class_vector, scm_class_null;
137SCM scm_class_integer, scm_class_real, scm_class_complex, scm_class_fraction;
138SCM scm_class_unknown;
139SCM scm_class_top, scm_class_object, scm_class_class;
140SCM scm_class_applicable;
141SCM scm_class_applicable_struct, scm_class_applicable_struct_with_setter;
142SCM scm_class_generic, scm_class_generic_with_setter;
143SCM scm_class_accessor;
144SCM scm_class_extended_generic, scm_class_extended_generic_with_setter;
145SCM scm_class_extended_accessor;
146SCM scm_class_method;
147SCM scm_class_accessor_method;
148SCM scm_class_procedure_class;
149SCM scm_class_applicable_struct_class;
150SCM scm_class_number, scm_class_list;
151SCM scm_class_keyword;
152SCM scm_class_port, scm_class_input_output_port;
153SCM scm_class_input_port, scm_class_output_port;
154SCM scm_class_foreign_slot;
155SCM scm_class_self, scm_class_protected;
156SCM scm_class_hidden, scm_class_opaque, scm_class_read_only;
157SCM scm_class_protected_hidden, scm_class_protected_opaque, scm_class_protected_read_only;
158SCM scm_class_scm;
159SCM scm_class_int, scm_class_float, scm_class_double;
160
161static SCM class_foreign;
162static SCM class_hashtable;
163static SCM class_fluid;
164static SCM class_dynamic_state;
165static SCM class_frame;
166static SCM class_objcode;
167static SCM class_vm;
168static SCM class_vm_cont;
169static SCM class_bytevector;
170static SCM class_uvec;
171
172static SCM vtable_class_map = SCM_BOOL_F;
173
174/* Port classes. Allocate 3 times the maximum number of port types so that
175 input ports, output ports, and in/out ports can be stored at different
176 offsets. See `SCM_IN_PCLASS_INDEX' et al. */
177SCM scm_port_class[3 * SCM_I_MAX_PORT_TYPE_COUNT];
178
179/* SMOB classes. */
180SCM scm_smob_class[SCM_I_MAX_SMOB_TYPE_COUNT];
181
182SCM scm_no_applicable_method;
183
184SCM_SYMBOL (scm_sym_define_public, "define-public");
185
186static SCM scm_make_unbound (void);
187static SCM scm_unbound_p (SCM obj);
188static SCM scm_assert_bound (SCM value, SCM obj);
189static SCM scm_at_assert_bound_ref (SCM obj, SCM index);
190static SCM scm_sys_goops_loaded (void);
191static SCM scm_make_extended_class_from_symbol (SCM type_name_sym,
192 int applicablep);
193
194
195SCM
196scm_i_define_class_for_vtable (SCM vtable)
197{
198 SCM class;
199
200 if (scm_is_false (vtable_class_map))
201 vtable_class_map = scm_make_weak_key_hash_table (SCM_UNDEFINED);
202
203 if (scm_is_false (scm_struct_vtable_p (vtable)))
204 abort ();
205
206 class = scm_hashq_ref (vtable_class_map, vtable, SCM_BOOL_F);
207
208 if (scm_is_false (class))
209 {
210 if (SCM_UNPACK (scm_class_class))
211 {
212 SCM name = SCM_VTABLE_NAME (vtable);
213 if (!scm_is_symbol (name))
214 name = scm_string_to_symbol (scm_nullstr);
215
216 class = scm_make_extended_class_from_symbol
217 (name, SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_APPLICABLE));
218 }
219 else
220 /* `create_struct_classes' will fill this in later. */
221 class = SCM_BOOL_F;
222
223 scm_hashq_set_x (vtable_class_map, vtable, class);
224 }
225
226 return class;
227}
228
229/* This function is used for efficient type dispatch. */
230SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
231 (SCM x),
232 "Return the class of @var{x}.")
233#define FUNC_NAME s_scm_class_of
234{
235 switch (SCM_ITAG3 (x))
236 {
237 case scm_tc3_int_1:
238 case scm_tc3_int_2:
239 return scm_class_integer;
240
241 case scm_tc3_imm24:
242 if (SCM_CHARP (x))
243 return scm_class_char;
244 else if (scm_is_bool (x))
245 return scm_class_boolean;
246 else if (scm_is_null (x))
247 return scm_class_null;
248 else
249 return scm_class_unknown;
250
251 case scm_tc3_cons:
252 switch (SCM_TYP7 (x))
253 {
254 case scm_tcs_cons_nimcar:
255 return scm_class_pair;
256 case scm_tc7_symbol:
257 return scm_class_symbol;
258 case scm_tc7_vector:
259 case scm_tc7_wvect:
260 return scm_class_vector;
261 case scm_tc7_pointer:
262 return class_foreign;
263 case scm_tc7_hashtable:
264 return class_hashtable;
265 case scm_tc7_fluid:
266 return class_fluid;
267 case scm_tc7_dynamic_state:
268 return class_dynamic_state;
269 case scm_tc7_frame:
270 return class_frame;
271 case scm_tc7_objcode:
272 return class_objcode;
273 case scm_tc7_vm:
274 return class_vm;
275 case scm_tc7_vm_cont:
276 return class_vm_cont;
277 case scm_tc7_bytevector:
278 if (SCM_BYTEVECTOR_ELEMENT_TYPE (x) == SCM_ARRAY_ELEMENT_TYPE_VU8)
279 return class_bytevector;
280 else
281 return class_uvec;
282 case scm_tc7_string:
283 return scm_class_string;
284 case scm_tc7_number:
285 switch SCM_TYP16 (x) {
286 case scm_tc16_big:
287 return scm_class_integer;
288 case scm_tc16_real:
289 return scm_class_real;
290 case scm_tc16_complex:
291 return scm_class_complex;
292 case scm_tc16_fraction:
293 return scm_class_fraction;
294 }
295 case scm_tc7_program:
296 if (SCM_PROGRAM_IS_PRIMITIVE_GENERIC (x) && *SCM_SUBR_GENERIC (x))
297 return scm_class_primitive_generic;
298 else
299 return scm_class_procedure;
300
301 case scm_tc7_smob:
302 {
303 scm_t_bits type = SCM_TYP16 (x);
304 if (type != scm_tc16_port_with_ps)
305 return scm_smob_class[SCM_TC2SMOBNUM (type)];
306 x = SCM_PORT_WITH_PS_PORT (x);
307 /* fall through to ports */
308 }
309 case scm_tc7_port:
310 return scm_port_class[(SCM_WRTNG & SCM_CELL_WORD_0 (x)
311 ? (SCM_RDNG & SCM_CELL_WORD_0 (x)
312 ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
313 : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
314 : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
315 case scm_tcs_struct:
316 if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
317 return SCM_CLASS_OF (x);
318 else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
319 {
320 /* Goops object */
321 if (! scm_is_false (SCM_OBJ_CLASS_REDEF (x)))
322 scm_change_object_class (x,
323 SCM_CLASS_OF (x), /* old */
324 SCM_OBJ_CLASS_REDEF (x)); /* new */
325 return SCM_CLASS_OF (x);
326 }
327 else
328 return scm_i_define_class_for_vtable (SCM_CLASS_OF (x));
329 default:
330 if (scm_is_pair (x))
331 return scm_class_pair;
332 else
333 return scm_class_unknown;
334 }
335
336 case scm_tc3_struct:
337 case scm_tc3_tc7_1:
338 case scm_tc3_tc7_2:
339 /* case scm_tc3_unused: */
340 /* Never reached */
341 break;
342 }
343 return scm_class_unknown;
344}
345#undef FUNC_NAME
346
347/******************************************************************************
348 *
349 * Compute-cpl
350 *
351 * This version doesn't fully handle multiple-inheritance. It serves
352 * only for booting classes and will be overloaded in Scheme
353 *
354 ******************************************************************************/
355
356static SCM
357map (SCM (*proc) (SCM), SCM ls)
358{
359 if (scm_is_null (ls))
360 return ls;
361 else
362 {
363 SCM res = scm_cons (proc (SCM_CAR (ls)), SCM_EOL);
364 SCM h = res;
365 ls = SCM_CDR (ls);
366 while (!scm_is_null (ls))
367 {
368 SCM_SETCDR (h, scm_cons (proc (SCM_CAR (ls)), SCM_EOL));
369 h = SCM_CDR (h);
370 ls = SCM_CDR (ls);
371 }
372 return res;
373 }
374}
375
376static SCM
377filter_cpl (SCM ls)
378{
379 SCM res = SCM_EOL;
380 while (!scm_is_null (ls))
381 {
382 SCM el = SCM_CAR (ls);
383 if (scm_is_false (scm_c_memq (el, res)))
384 res = scm_cons (el, res);
385 ls = SCM_CDR (ls);
386 }
387 return res;
388}
389
390static SCM
391compute_cpl (SCM class)
392{
393 if (goops_loaded_p)
394 return scm_call_1 (SCM_VARIABLE_REF (var_compute_cpl), class);
395 else
396 {
397 SCM supers = SCM_SLOT (class, scm_si_direct_supers);
398 SCM ls = scm_append (scm_acons (class, supers,
399 map (compute_cpl, supers)));
400 return scm_reverse_x (filter_cpl (ls), SCM_EOL);
401 }
402}
403
404/******************************************************************************
405 *
406 * compute-slots
407 *
408 ******************************************************************************/
409
410static SCM
411remove_duplicate_slots (SCM l, SCM res, SCM slots_already_seen)
412{
413 SCM tmp;
414
415 if (scm_is_null (l))
416 return res;
417
418 tmp = SCM_CAAR (l);
419 if (!scm_is_symbol (tmp))
420 scm_misc_error ("%compute-slots", "bad slot name ~S", scm_list_1 (tmp));
421
422 if (scm_is_false (scm_c_memq (tmp, slots_already_seen))) {
423 res = scm_cons (SCM_CAR (l), res);
424 slots_already_seen = scm_cons (tmp, slots_already_seen);
425 }
426
427 return remove_duplicate_slots (SCM_CDR (l), res, slots_already_seen);
428}
429
430static SCM
431build_slots_list (SCM dslots, SCM cpl)
432{
433 register SCM res = dslots;
434
435 for (cpl = SCM_CDR (cpl); !scm_is_null (cpl); cpl = SCM_CDR (cpl))
436 res = scm_append (scm_list_2 (SCM_SLOT (SCM_CAR (cpl),
437 scm_si_direct_slots),
438 res));
439
440 /* res contains a list of slots. Remove slots which appears more than once */
441 return remove_duplicate_slots (scm_reverse (res), SCM_EOL, SCM_EOL);
442}
443
444static SCM
445maplist (SCM ls)
446{
447 SCM orig = ls;
448 while (!scm_is_null (ls))
449 {
450 if (!scm_is_pair (SCM_CAR (ls)))
451 SCM_SETCAR (ls, scm_cons (SCM_CAR (ls), SCM_EOL));
452 ls = SCM_CDR (ls);
453 }
454 return orig;
455}
456
457
458SCM_DEFINE (scm_sys_compute_slots, "%compute-slots", 1, 0, 0,
459 (SCM class),
460 "Return a list consisting of the names of all slots belonging to\n"
461 "class @var{class}, i. e. the slots of @var{class} and of all of\n"
462 "its superclasses.")
463#define FUNC_NAME s_scm_sys_compute_slots
464{
465 SCM_VALIDATE_CLASS (1, class);
466 return build_slots_list (SCM_SLOT (class, scm_si_direct_slots),
467 SCM_SLOT (class, scm_si_cpl));
468}
469#undef FUNC_NAME
470
471
472/******************************************************************************
473 *
474 * compute-getters-n-setters
475 *
476 * This version doesn't handle slot options. It serves only for booting
477 * classes and will be overloaded in Scheme.
478 *
479 ******************************************************************************/
480
481SCM_KEYWORD (k_init_value, "init-value");
482SCM_KEYWORD (k_init_thunk, "init-thunk");
483
484static SCM
485compute_getters_n_setters (SCM slots)
486{
487 SCM res = SCM_EOL;
488 SCM *cdrloc = &res;
489 long i = 0;
490
491 for ( ; !scm_is_null (slots); slots = SCM_CDR (slots))
492 {
493 SCM init = SCM_BOOL_F;
494 SCM options = SCM_CDAR (slots);
495 if (!scm_is_null (options))
496 {
497 init = scm_get_keyword (k_init_value, options, 0);
498 if (init)
499 {
500 init = scm_primitive_eval (scm_list_3 (scm_sym_lambda,
501 SCM_EOL,
502 scm_list_2 (scm_sym_quote,
503 init)));
504 }
505 else
506 init = scm_get_keyword (k_init_thunk, options, SCM_BOOL_F);
507 }
508 *cdrloc = scm_cons (scm_cons (SCM_CAAR (slots),
509 scm_cons (init,
510 scm_from_int (i++))),
511 SCM_EOL);
512 cdrloc = SCM_CDRLOC (*cdrloc);
513 }
514 return res;
515}
516
517/******************************************************************************
518 *
519 * initialize-object
520 *
521 ******************************************************************************/
522
523/*fixme* Manufacture keywords in advance */
524SCM
525scm_i_get_keyword (SCM key, SCM l, long len, SCM default_value, const char *subr)
526{
527 long i;
528
529 for (i = 0; i != len; i += 2)
530 {
531 SCM obj = SCM_CAR (l);
532
533 if (!scm_is_keyword (obj))
534 scm_misc_error (subr, "bad keyword: ~S", scm_list_1 (obj));
535 else if (scm_is_eq (obj, key))
536 return SCM_CADR (l);
537 else
538 l = SCM_CDDR (l);
539 }
540
541 return default_value;
542}
543
544
545SCM_DEFINE (scm_get_keyword, "get-keyword", 3, 0, 0,
546 (SCM key, SCM l, SCM default_value),
547 "Determine an associated value for the keyword @var{key} from\n"
548 "the list @var{l}. The list @var{l} has to consist of an even\n"
549 "number of elements, where, starting with the first, every\n"
550 "second element is a keyword, followed by its associated value.\n"
551 "If @var{l} does not hold a value for @var{key}, the value\n"
552 "@var{default_value} is returned.")
553#define FUNC_NAME s_scm_get_keyword
554{
555 long len;
556
557 SCM_ASSERT (scm_is_keyword (key), key, SCM_ARG1, FUNC_NAME);
558 len = scm_ilength (l);
559 if (len < 0 || len % 2 == 1)
560 scm_misc_error (FUNC_NAME, "Bad keyword-value list: ~S", scm_list_1 (l));
561
562 return scm_i_get_keyword (key, l, len, default_value, FUNC_NAME);
563}
564#undef FUNC_NAME
565
566
567SCM_KEYWORD (k_init_keyword, "init-keyword");
568
569static SCM get_slot_value (SCM class, SCM obj, SCM slotdef);
570static SCM set_slot_value (SCM class, SCM obj, SCM slotdef, SCM value);
571
572SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0,
573 (SCM obj, SCM initargs),
574 "Initialize the object @var{obj} with the given arguments\n"
575 "@var{initargs}.")
576#define FUNC_NAME s_scm_sys_initialize_object
577{
578 SCM tmp, get_n_set, slots;
579 SCM class = SCM_CLASS_OF (obj);
580 long n_initargs;
581
582 SCM_VALIDATE_INSTANCE (1, obj);
583 n_initargs = scm_ilength (initargs);
584 SCM_ASSERT ((n_initargs & 1) == 0, initargs, SCM_ARG2, FUNC_NAME);
585
586 get_n_set = SCM_SLOT (class, scm_si_getters_n_setters);
587 slots = SCM_SLOT (class, scm_si_slots);
588
589 /* See for each slot how it must be initialized */
590 for (;
591 !scm_is_null (slots);
592 get_n_set = SCM_CDR (get_n_set), slots = SCM_CDR (slots))
593 {
594 SCM slot_name = SCM_CAR (slots);
595 SCM slot_value = 0;
596
597 if (!scm_is_null (SCM_CDR (slot_name)))
598 {
599 /* This slot admits (perhaps) to be initialized at creation time */
600 long n = scm_ilength (SCM_CDR (slot_name));
601 if (n & 1) /* odd or -1 */
602 SCM_MISC_ERROR ("class contains bogus slot definition: ~S",
603 scm_list_1 (slot_name));
604 tmp = scm_i_get_keyword (k_init_keyword,
605 SCM_CDR (slot_name),
606 n,
607 0,
608 FUNC_NAME);
609 slot_name = SCM_CAR (slot_name);
610 if (tmp)
611 {
612 /* an initarg was provided for this slot */
613 if (!scm_is_keyword (tmp))
614 SCM_MISC_ERROR ("initarg must be a keyword. It was ~S",
615 scm_list_1 (tmp));
616 slot_value = scm_i_get_keyword (tmp,
617 initargs,
618 n_initargs,
619 0,
620 FUNC_NAME);
621 }
622 }
623
624 if (slot_value)
625 /* set slot to provided value */
626 set_slot_value (class, obj, SCM_CAR (get_n_set), slot_value);
627 else
628 {
629 /* set slot to its :init-form if it exists */
630 tmp = SCM_CADAR (get_n_set);
631 if (scm_is_true (tmp))
632 {
633 slot_value = get_slot_value (class, obj, SCM_CAR (get_n_set));
634 if (SCM_GOOPS_UNBOUNDP (slot_value))
635 set_slot_value (class,
636 obj,
637 SCM_CAR (get_n_set),
638 scm_call_0 (tmp));
639 }
640 }
641 }
642
643 return obj;
644}
645#undef FUNC_NAME
646
647/* NOTE: The following macros are interdependent with code
648 * in goops.scm:compute-getters-n-setters
649 */
650#define SCM_GNS_INSTANCE_ALLOCATED_P(gns) \
651 (SCM_I_INUMP (SCM_CDDR (gns)) \
652 || (scm_is_pair (SCM_CDDR (gns)) \
653 && scm_is_pair (SCM_CDDDR (gns)) \
654 && scm_is_pair (SCM_CDDDDR (gns))))
655#define SCM_GNS_INDEX(gns) \
656 (SCM_I_INUMP (SCM_CDDR (gns)) \
657 ? SCM_I_INUM (SCM_CDDR (gns)) \
658 : scm_to_long (SCM_CAR (SCM_CDDDDR (gns))))
659#define SCM_GNS_SIZE(gns) \
660 (SCM_I_INUMP (SCM_CDDR (gns)) \
661 ? 1 \
662 : scm_to_long (SCM_CADR (SCM_CDDDDR (gns))))
663
664SCM_KEYWORD (k_class, "class");
665SCM_KEYWORD (k_allocation, "allocation");
666SCM_KEYWORD (k_instance, "instance");
667
668SCM_DEFINE (scm_sys_prep_layout_x, "%prep-layout!", 1, 0, 0,
669 (SCM class),
670 "")
671#define FUNC_NAME s_scm_sys_prep_layout_x
672{
673 SCM slots, getters_n_setters, nfields;
674 unsigned long int n, i;
675 char *s;
676 SCM layout;
677
678 SCM_VALIDATE_INSTANCE (1, class);
679 slots = SCM_SLOT (class, scm_si_slots);
680 getters_n_setters = SCM_SLOT (class, scm_si_getters_n_setters);
681 nfields = SCM_SLOT (class, scm_si_nfields);
682 if (!SCM_I_INUMP (nfields) || SCM_I_INUM (nfields) < 0)
683 SCM_MISC_ERROR ("bad value in nfields slot: ~S",
684 scm_list_1 (nfields));
685 n = 2 * SCM_I_INUM (nfields);
686 if (n < sizeof (SCM_CLASS_CLASS_LAYOUT) - 1
687 && SCM_SUBCLASSP (class, scm_class_class))
688 SCM_MISC_ERROR ("class object doesn't have enough fields: ~S",
689 scm_list_1 (nfields));
690
691 layout = scm_i_make_string (n, &s, 0);
692 i = 0;
693 while (scm_is_pair (getters_n_setters))
694 {
695 if (SCM_GNS_INSTANCE_ALLOCATED_P (SCM_CAR (getters_n_setters)))
696 {
697 SCM type;
698 int len, index, size;
699 char p, a;
700
701 if (i >= n || !scm_is_pair (slots))
702 goto inconsistent;
703
704 /* extract slot type */
705 len = scm_ilength (SCM_CDAR (slots));
706 type = scm_i_get_keyword (k_class, SCM_CDAR (slots),
707 len, SCM_BOOL_F, FUNC_NAME);
708 /* determine slot GC protection and access mode */
709 if (scm_is_false (type))
710 {
711 p = 'p';
712 a = 'w';
713 }
714 else
715 {
716 if (!SCM_CLASSP (type))
717 SCM_MISC_ERROR ("bad slot class", SCM_EOL);
718 else if (SCM_SUBCLASSP (type, scm_class_foreign_slot))
719 {
720 if (SCM_SUBCLASSP (type, scm_class_self))
721 p = 's';
722 else if (SCM_SUBCLASSP (type, scm_class_protected))
723 p = 'p';
724 else
725 p = 'u';
726
727 if (SCM_SUBCLASSP (type, scm_class_opaque))
728 a = 'o';
729 else if (SCM_SUBCLASSP (type, scm_class_read_only))
730 a = 'r';
731 else if (SCM_SUBCLASSP (type, scm_class_hidden))
732 a = 'h';
733 else
734 a = 'w';
735 }
736 else
737 {
738 p = 'p';
739 a = 'w';
740 }
741 }
742
743 index = SCM_GNS_INDEX (SCM_CAR (getters_n_setters));
744 if (index != (i >> 1))
745 goto inconsistent;
746 size = SCM_GNS_SIZE (SCM_CAR (getters_n_setters));
747 while (size)
748 {
749 s[i++] = p;
750 s[i++] = a;
751 --size;
752 }
753 }
754 slots = SCM_CDR (slots);
755 getters_n_setters = SCM_CDR (getters_n_setters);
756 }
757 if (!scm_is_null (slots))
758 {
759 inconsistent:
760 SCM_MISC_ERROR ("inconsistent getters-n-setters", SCM_EOL);
761 }
762 SCM_SET_VTABLE_LAYOUT (class, scm_string_to_symbol (layout));
763 return SCM_UNSPECIFIED;
764}
765#undef FUNC_NAME
766
767static void prep_hashsets (SCM);
768
769SCM_DEFINE (scm_sys_inherit_magic_x, "%inherit-magic!", 2, 0, 0,
770 (SCM class, SCM dsupers),
771 "")
772#define FUNC_NAME s_scm_sys_inherit_magic_x
773{
774 SCM_VALIDATE_INSTANCE (1, class);
775 scm_i_struct_inherit_vtable_magic (SCM_CLASS_OF (class), class);
776 SCM_SET_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_OR_VALID);
777
778 prep_hashsets (class);
779
780 return SCM_UNSPECIFIED;
781}
782#undef FUNC_NAME
783
784static void
785prep_hashsets (SCM class)
786{
787 unsigned int i;
788
789 for (i = 0; i < 8; ++i)
790 SCM_SET_HASHSET (class, i, scm_c_uniform32 (goops_rstate));
791}
792
793/******************************************************************************/
794
795SCM
796scm_basic_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots)
797{
798 SCM z, cpl, slots, nfields, g_n_s;
799
800 /* Allocate one instance */
801 z = scm_make_struct (class, SCM_INUM0, SCM_EOL);
802
803 /* Initialize its slots */
804 SCM_SET_SLOT (z, scm_si_direct_supers, dsupers);
805 cpl = compute_cpl (z);
806 slots = build_slots_list (maplist (dslots), cpl);
807 nfields = scm_from_int (scm_ilength (slots));
808 g_n_s = compute_getters_n_setters (slots);
809
810 SCM_SET_SLOT (z, scm_vtable_index_name, name);
811 SCM_SET_SLOT (z, scm_si_direct_slots, dslots);
812 SCM_SET_SLOT (z, scm_si_direct_subclasses, SCM_EOL);
813 SCM_SET_SLOT (z, scm_si_direct_methods, SCM_EOL);
814 SCM_SET_SLOT (z, scm_si_cpl, cpl);
815 SCM_SET_SLOT (z, scm_si_slots, slots);
816 SCM_SET_SLOT (z, scm_si_nfields, nfields);
817 SCM_SET_SLOT (z, scm_si_getters_n_setters, g_n_s);
818 SCM_SET_SLOT (z, scm_si_redefined, SCM_BOOL_F);
819
820 /* Add this class in the direct-subclasses slot of dsupers */
821 {
822 SCM tmp;
823 for (tmp = dsupers; !scm_is_null (tmp); tmp = SCM_CDR (tmp))
824 SCM_SET_SLOT (SCM_CAR (tmp), scm_si_direct_subclasses,
825 scm_cons (z, SCM_SLOT (SCM_CAR (tmp),
826 scm_si_direct_subclasses)));
827 }
828
829 return z;
830}
831
832SCM
833scm_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots)
834{
835 SCM z = scm_basic_basic_make_class (class, name, dsupers, dslots);
836 scm_sys_prep_layout_x (z);
837 scm_sys_inherit_magic_x (z, dsupers);
838 return z;
839}
840
841/******************************************************************************/
842
843SCM_SYMBOL (sym_layout, "layout");
844SCM_SYMBOL (sym_flags, "flags");
845SCM_SYMBOL (sym_self, "%self");
846SCM_SYMBOL (sym_instance_finalizer, "instance-finalizer");
847SCM_SYMBOL (sym_reserved_0, "%reserved-0");
848SCM_SYMBOL (sym_reserved_1, "%reserved-1");
849SCM_SYMBOL (sym_print, "print");
850SCM_SYMBOL (sym_procedure, "procedure");
851SCM_SYMBOL (sym_setter, "setter");
852SCM_SYMBOL (sym_redefined, "redefined");
853SCM_SYMBOL (sym_h0, "h0");
854SCM_SYMBOL (sym_h1, "h1");
855SCM_SYMBOL (sym_h2, "h2");
856SCM_SYMBOL (sym_h3, "h3");
857SCM_SYMBOL (sym_h4, "h4");
858SCM_SYMBOL (sym_h5, "h5");
859SCM_SYMBOL (sym_h6, "h6");
860SCM_SYMBOL (sym_h7, "h7");
861SCM_SYMBOL (sym_name, "name");
862SCM_SYMBOL (sym_direct_supers, "direct-supers");
863SCM_SYMBOL (sym_direct_slots, "direct-slots");
864SCM_SYMBOL (sym_direct_subclasses, "direct-subclasses");
865SCM_SYMBOL (sym_direct_methods, "direct-methods");
866SCM_SYMBOL (sym_cpl, "cpl");
867SCM_SYMBOL (sym_default_slot_definition_class, "default-slot-definition-class");
868SCM_SYMBOL (sym_slots, "slots");
869SCM_SYMBOL (sym_getters_n_setters, "getters-n-setters");
870SCM_SYMBOL (sym_keyword_access, "keyword-access");
871SCM_SYMBOL (sym_nfields, "nfields");
872
873
874static SCM
875build_class_class_slots ()
876{
877 /* has to be kept in sync with SCM_VTABLE_BASE_LAYOUT and
878 SCM_CLASS_CLASS_LAYOUT */
879 return scm_list_n (
880 scm_list_3 (sym_layout, k_class, scm_class_protected_read_only),
881 scm_list_3 (sym_flags, k_class, scm_class_hidden),
882 scm_list_3 (sym_self, k_class, scm_class_self),
883 scm_list_3 (sym_instance_finalizer, k_class, scm_class_hidden),
884 scm_list_1 (sym_print),
885 scm_list_3 (sym_name, k_class, scm_class_protected_hidden),
886 scm_list_3 (sym_reserved_0, k_class, scm_class_hidden),
887 scm_list_3 (sym_reserved_1, k_class, scm_class_hidden),
888 scm_list_1 (sym_redefined),
889 scm_list_3 (sym_h0, k_class, scm_class_int),
890 scm_list_3 (sym_h1, k_class, scm_class_int),
891 scm_list_3 (sym_h2, k_class, scm_class_int),
892 scm_list_3 (sym_h3, k_class, scm_class_int),
893 scm_list_3 (sym_h4, k_class, scm_class_int),
894 scm_list_3 (sym_h5, k_class, scm_class_int),
895 scm_list_3 (sym_h6, k_class, scm_class_int),
896 scm_list_3 (sym_h7, k_class, scm_class_int),
897 scm_list_1 (sym_direct_supers),
898 scm_list_1 (sym_direct_slots),
899 scm_list_1 (sym_direct_subclasses),
900 scm_list_1 (sym_direct_methods),
901 scm_list_1 (sym_cpl),
902 scm_list_1 (sym_default_slot_definition_class),
903 scm_list_1 (sym_slots),
904 scm_list_1 (sym_getters_n_setters),
905 scm_list_1 (sym_keyword_access),
906 scm_list_1 (sym_nfields),
907 SCM_UNDEFINED);
908}
909
910static void
911create_basic_classes (void)
912{
913 /* SCM slots_of_class = build_class_class_slots (); */
914
915 /**** <class> ****/
916 SCM cs = scm_from_locale_string (SCM_CLASS_CLASS_LAYOUT);
917 SCM name = scm_from_latin1_symbol ("<class>");
918 scm_class_class = scm_make_vtable_vtable (cs, SCM_INUM0, SCM_EOL);
919 SCM_SET_CLASS_FLAGS (scm_class_class, (SCM_CLASSF_GOOPS_OR_VALID
920 | SCM_CLASSF_METACLASS));
921
922 SCM_SET_SLOT (scm_class_class, scm_vtable_index_name, name);
923 SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, SCM_EOL); /* will be changed */
924 /* SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots_of_class); */
925 SCM_SET_SLOT (scm_class_class, scm_si_direct_subclasses, SCM_EOL);
926 SCM_SET_SLOT (scm_class_class, scm_si_direct_methods, SCM_EOL);
927 SCM_SET_SLOT (scm_class_class, scm_si_cpl, SCM_EOL); /* will be changed */
928 /* SCM_SET_SLOT (scm_class_class, scm_si_slots, slots_of_class); */
929 SCM_SET_SLOT (scm_class_class, scm_si_nfields, scm_from_int (SCM_N_CLASS_SLOTS));
930 /* SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters,
931 compute_getters_n_setters (slots_of_class)); */
932 SCM_SET_SLOT (scm_class_class, scm_si_redefined, SCM_BOOL_F);
933
934 prep_hashsets (scm_class_class);
935
936 DEFVAR(name, scm_class_class);
937
938 /**** <top> ****/
939 name = scm_from_latin1_symbol ("<top>");
940 scm_class_top = scm_basic_make_class (scm_class_class, name,
941 SCM_EOL, SCM_EOL);
942
943 DEFVAR(name, scm_class_top);
944
945 /**** <object> ****/
946 name = scm_from_latin1_symbol ("<object>");
947 scm_class_object = scm_basic_make_class (scm_class_class, name,
948 scm_list_1 (scm_class_top), SCM_EOL);
949
950 DEFVAR (name, scm_class_object);
951
952 /* <top> <object> and <class> were partially initialized. Correct them here */
953 SCM_SET_SLOT (scm_class_object, scm_si_direct_subclasses, scm_list_1 (scm_class_class));
954
955 SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, scm_list_1 (scm_class_object));
956 SCM_SET_SLOT (scm_class_class, scm_si_cpl, scm_list_3 (scm_class_class, scm_class_object, scm_class_top));
957}
958
959/******************************************************************************/
960
961SCM_DEFINE (scm_instance_p, "instance?", 1, 0, 0,
962 (SCM obj),
963 "Return @code{#t} if @var{obj} is an instance.")
964#define FUNC_NAME s_scm_instance_p
965{
966 return scm_from_bool (SCM_INSTANCEP (obj));
967}
968#undef FUNC_NAME
969
970
971/******************************************************************************
972 *
973 * Meta object accessors
974 *
975 ******************************************************************************/
976SCM_DEFINE (scm_class_name, "class-name", 1, 0, 0,
977 (SCM obj),
978 "Return the class name of @var{obj}.")
979#define FUNC_NAME s_scm_class_name
980{
981 SCM_VALIDATE_CLASS (1, obj);
982 return scm_slot_ref (obj, sym_name);
983}
984#undef FUNC_NAME
985
986SCM_DEFINE (scm_class_direct_supers, "class-direct-supers", 1, 0, 0,
987 (SCM obj),
988 "Return the direct superclasses of the class @var{obj}.")
989#define FUNC_NAME s_scm_class_direct_supers
990{
991 SCM_VALIDATE_CLASS (1, obj);
992 return scm_slot_ref (obj, sym_direct_supers);
993}
994#undef FUNC_NAME
995
996SCM_DEFINE (scm_class_direct_slots, "class-direct-slots", 1, 0, 0,
997 (SCM obj),
998 "Return the direct slots of the class @var{obj}.")
999#define FUNC_NAME s_scm_class_direct_slots
1000{
1001 SCM_VALIDATE_CLASS (1, obj);
1002 return scm_slot_ref (obj, sym_direct_slots);
1003}
1004#undef FUNC_NAME
1005
1006SCM_DEFINE (scm_class_direct_subclasses, "class-direct-subclasses", 1, 0, 0,
1007 (SCM obj),
1008 "Return the direct subclasses of the class @var{obj}.")
1009#define FUNC_NAME s_scm_class_direct_subclasses
1010{
1011 SCM_VALIDATE_CLASS (1, obj);
1012 return scm_slot_ref(obj, sym_direct_subclasses);
1013}
1014#undef FUNC_NAME
1015
1016SCM_DEFINE (scm_class_direct_methods, "class-direct-methods", 1, 0, 0,
1017 (SCM obj),
1018 "Return the direct methods of the class @var{obj}")
1019#define FUNC_NAME s_scm_class_direct_methods
1020{
1021 SCM_VALIDATE_CLASS (1, obj);
1022 return scm_slot_ref (obj, sym_direct_methods);
1023}
1024#undef FUNC_NAME
1025
1026SCM_DEFINE (scm_class_precedence_list, "class-precedence-list", 1, 0, 0,
1027 (SCM obj),
1028 "Return the class precedence list of the class @var{obj}.")
1029#define FUNC_NAME s_scm_class_precedence_list
1030{
1031 SCM_VALIDATE_CLASS (1, obj);
1032 return scm_slot_ref (obj, sym_cpl);
1033}
1034#undef FUNC_NAME
1035
1036SCM_DEFINE (scm_class_slots, "class-slots", 1, 0, 0,
1037 (SCM obj),
1038 "Return the slot list of the class @var{obj}.")
1039#define FUNC_NAME s_scm_class_slots
1040{
1041 SCM_VALIDATE_CLASS (1, obj);
1042 return scm_slot_ref (obj, sym_slots);
1043}
1044#undef FUNC_NAME
1045
1046SCM_DEFINE (scm_generic_function_name, "generic-function-name", 1, 0, 0,
1047 (SCM obj),
1048 "Return the name of the generic function @var{obj}.")
1049#define FUNC_NAME s_scm_generic_function_name
1050{
1051 SCM_VALIDATE_GENERIC (1, obj);
1052 return scm_procedure_property (obj, scm_sym_name);
1053}
1054#undef FUNC_NAME
1055
1056SCM_SYMBOL (sym_methods, "methods");
1057SCM_SYMBOL (sym_extended_by, "extended-by");
1058SCM_SYMBOL (sym_extends, "extends");
1059
1060static
1061SCM fold_downward_gf_methods (SCM method_lists, SCM gf)
1062{
1063 SCM gfs = scm_slot_ref (gf, sym_extended_by);
1064 method_lists = scm_cons (scm_slot_ref (gf, sym_methods), method_lists);
1065 while (!scm_is_null (gfs))
1066 {
1067 method_lists = fold_downward_gf_methods (method_lists, SCM_CAR (gfs));
1068 gfs = SCM_CDR (gfs);
1069 }
1070 return method_lists;
1071}
1072
1073static
1074SCM fold_upward_gf_methods (SCM method_lists, SCM gf)
1075{
1076 if (SCM_IS_A_P (gf, scm_class_extended_generic))
1077 {
1078 SCM gfs = scm_slot_ref (gf, sym_extends);
1079 while (!scm_is_null (gfs))
1080 {
1081 SCM methods = scm_slot_ref (SCM_CAR (gfs), sym_methods);
1082 method_lists = fold_upward_gf_methods (scm_cons (methods,
1083 method_lists),
1084 SCM_CAR (gfs));
1085 gfs = SCM_CDR (gfs);
1086 }
1087 }
1088 return method_lists;
1089}
1090
1091SCM_DEFINE (scm_generic_function_methods, "generic-function-methods", 1, 0, 0,
1092 (SCM obj),
1093 "Return the methods of the generic function @var{obj}.")
1094#define FUNC_NAME s_scm_generic_function_methods
1095{
1096 SCM methods;
1097 SCM_VALIDATE_GENERIC (1, obj);
1098 methods = fold_upward_gf_methods (SCM_EOL, obj);
1099 methods = fold_downward_gf_methods (methods, obj);
1100 return scm_append (methods);
1101}
1102#undef FUNC_NAME
1103
1104SCM_DEFINE (scm_method_generic_function, "method-generic-function", 1, 0, 0,
1105 (SCM obj),
1106 "Return the generic function for the method @var{obj}.")
1107#define FUNC_NAME s_scm_method_generic_function
1108{
1109 SCM_VALIDATE_METHOD (1, obj);
1110 return scm_slot_ref (obj, scm_from_latin1_symbol ("generic-function"));
1111}
1112#undef FUNC_NAME
1113
1114SCM_DEFINE (scm_method_specializers, "method-specializers", 1, 0, 0,
1115 (SCM obj),
1116 "Return specializers of the method @var{obj}.")
1117#define FUNC_NAME s_scm_method_specializers
1118{
1119 SCM_VALIDATE_METHOD (1, obj);
1120 return scm_slot_ref (obj, scm_from_latin1_symbol ("specializers"));
1121}
1122#undef FUNC_NAME
1123
1124SCM_DEFINE (scm_method_procedure, "method-procedure", 1, 0, 0,
1125 (SCM obj),
1126 "Return the procedure of the method @var{obj}.")
1127#define FUNC_NAME s_scm_method_procedure
1128{
1129 SCM_VALIDATE_METHOD (1, obj);
1130 return scm_slot_ref (obj, sym_procedure);
1131}
1132#undef FUNC_NAME
1133
1134/******************************************************************************
1135 *
1136 * S l o t a c c e s s
1137 *
1138 ******************************************************************************/
1139
1140SCM_DEFINE (scm_make_unbound, "make-unbound", 0, 0, 0,
1141 (),
1142 "Return the unbound value.")
1143#define FUNC_NAME s_scm_make_unbound
1144{
1145 return SCM_GOOPS_UNBOUND;
1146}
1147#undef FUNC_NAME
1148
1149SCM_DEFINE (scm_unbound_p, "unbound?", 1, 0, 0,
1150 (SCM obj),
1151 "Return @code{#t} if @var{obj} is unbound.")
1152#define FUNC_NAME s_scm_unbound_p
1153{
1154 return SCM_GOOPS_UNBOUNDP (obj) ? SCM_BOOL_T : SCM_BOOL_F;
1155}
1156#undef FUNC_NAME
1157
1158SCM_DEFINE (scm_assert_bound, "assert-bound", 2, 0, 0,
1159 (SCM value, SCM obj),
1160 "Return @var{value} if it is bound, and invoke the\n"
1161 "@var{slot-unbound} method of @var{obj} if it is not.")
1162#define FUNC_NAME s_scm_assert_bound
1163{
1164 if (SCM_GOOPS_UNBOUNDP (value))
1165 return scm_call_1 (SCM_VARIABLE_REF (var_slot_unbound), obj);
1166 return value;
1167}
1168#undef FUNC_NAME
1169
1170SCM_DEFINE (scm_at_assert_bound_ref, "@assert-bound-ref", 2, 0, 0,
1171 (SCM obj, SCM index),
1172 "Like @code{assert-bound}, but use @var{index} for accessing\n"
1173 "the value from @var{obj}.")
1174#define FUNC_NAME s_scm_at_assert_bound_ref
1175{
1176 SCM value = SCM_SLOT (obj, scm_to_int (index));
1177 if (SCM_GOOPS_UNBOUNDP (value))
1178 return scm_call_1 (SCM_VARIABLE_REF (var_slot_unbound), obj);
1179 return value;
1180}
1181#undef FUNC_NAME
1182
1183SCM_DEFINE (scm_sys_fast_slot_ref, "%fast-slot-ref", 2, 0, 0,
1184 (SCM obj, SCM index),
1185 "Return the slot value with index @var{index} from @var{obj}.")
1186#define FUNC_NAME s_scm_sys_fast_slot_ref
1187{
1188 scm_t_bits i;
1189
1190 SCM_VALIDATE_INSTANCE (1, obj);
1191 i = scm_to_unsigned_integer (index, 0,
1192 SCM_I_INUM (SCM_SLOT (SCM_CLASS_OF (obj),
1193 scm_si_nfields))
1194 - 1);
1195 return SCM_SLOT (obj, i);
1196}
1197#undef FUNC_NAME
1198
1199SCM_DEFINE (scm_sys_fast_slot_set_x, "%fast-slot-set!", 3, 0, 0,
1200 (SCM obj, SCM index, SCM value),
1201 "Set the slot with index @var{index} in @var{obj} to\n"
1202 "@var{value}.")
1203#define FUNC_NAME s_scm_sys_fast_slot_set_x
1204{
1205 scm_t_bits i;
1206
1207 SCM_VALIDATE_INSTANCE (1, obj);
1208 i = scm_to_unsigned_integer (index, 0,
1209 SCM_I_INUM (SCM_SLOT (SCM_CLASS_OF (obj),
1210 scm_si_nfields))
1211 - 1);
1212
1213 SCM_SET_SLOT (obj, i, value);
1214
1215 return SCM_UNSPECIFIED;
1216}
1217#undef FUNC_NAME
1218
1219
1220\f
1221/** Utilities **/
1222
1223/* In the future, this function will return the effective slot
1224 * definition associated with SLOT_NAME. Now it just returns some of
1225 * the information which will be stored in the effective slot
1226 * definition.
1227 */
1228
1229static SCM
1230slot_definition_using_name (SCM class, SCM slot_name)
1231{
1232 register SCM slots = SCM_SLOT (class, scm_si_getters_n_setters);
1233 for (; !scm_is_null (slots); slots = SCM_CDR (slots))
1234 if (SCM_CAAR (slots) == slot_name)
1235 return SCM_CAR (slots);
1236 return SCM_BOOL_F;
1237}
1238
1239static SCM
1240get_slot_value (SCM class SCM_UNUSED, SCM obj, SCM slotdef)
1241#define FUNC_NAME "%get-slot-value"
1242{
1243 SCM access = SCM_CDDR (slotdef);
1244 /* Two cases here:
1245 * - access is an integer (the offset of this slot in the slots vector)
1246 * - otherwise (car access) is the getter function to apply
1247 *
1248 * Instances have never more than SCM_MOST_POSITIVE_FIXNUM slots, so
1249 * we can just assume fixnums here.
1250 */
1251 if (SCM_I_INUMP (access))
1252 /* Don't poke at the slots directly, because scm_struct_ref handles the
1253 access bits for us. */
1254 return scm_struct_ref (obj, access);
1255 else
1256 return scm_call_1 (SCM_CAR (access), obj);
1257}
1258#undef FUNC_NAME
1259
1260static SCM
1261get_slot_value_using_name (SCM class, SCM obj, SCM slot_name)
1262{
1263 SCM slotdef = slot_definition_using_name (class, slot_name);
1264 if (scm_is_true (slotdef))
1265 return get_slot_value (class, obj, slotdef);
1266 else
1267 return scm_call_3 (SCM_VARIABLE_REF (var_slot_missing), class, obj, slot_name);
1268}
1269
1270static SCM
1271set_slot_value (SCM class SCM_UNUSED, SCM obj, SCM slotdef, SCM value)
1272#define FUNC_NAME "%set-slot-value"
1273{
1274 SCM access = SCM_CDDR (slotdef);
1275 /* Two cases here:
1276 * - access is an integer (the offset of this slot in the slots vector)
1277 * - otherwise (cadr access) is the setter function to apply
1278 *
1279 * Instances have never more than SCM_MOST_POSITIVE_FIXNUM slots, so
1280 * we can just assume fixnums here.
1281 */
1282 if (SCM_I_INUMP (access))
1283 /* obey permissions bits via going through struct-set! */
1284 scm_struct_set_x (obj, access, value);
1285 else
1286 /* ((cadr l) obj value) */
1287 scm_call_2 (SCM_CADR (access), obj, value);
1288 return SCM_UNSPECIFIED;
1289}
1290#undef FUNC_NAME
1291
1292static SCM
1293set_slot_value_using_name (SCM class, SCM obj, SCM slot_name, SCM value)
1294{
1295 SCM slotdef = slot_definition_using_name (class, slot_name);
1296 if (scm_is_true (slotdef))
1297 return set_slot_value (class, obj, slotdef, value);
1298 else
1299 return scm_call_4 (SCM_VARIABLE_REF (var_slot_missing), class, obj, slot_name, value);
1300}
1301
1302static SCM
1303test_slot_existence (SCM class SCM_UNUSED, SCM obj, SCM slot_name)
1304{
1305 register SCM l;
1306
1307 for (l = SCM_ACCESSORS_OF (obj); !scm_is_null (l); l = SCM_CDR (l))
1308 if (scm_is_eq (SCM_CAAR (l), slot_name))
1309 return SCM_BOOL_T;
1310
1311 return SCM_BOOL_F;
1312}
1313
1314 /* ======================================== */
1315
1316SCM_DEFINE (scm_slot_ref_using_class, "slot-ref-using-class", 3, 0, 0,
1317 (SCM class, SCM obj, SCM slot_name),
1318 "")
1319#define FUNC_NAME s_scm_slot_ref_using_class
1320{
1321 SCM res;
1322
1323 SCM_VALIDATE_CLASS (1, class);
1324 SCM_VALIDATE_INSTANCE (2, obj);
1325 SCM_VALIDATE_SYMBOL (3, slot_name);
1326
1327 res = get_slot_value_using_name (class, obj, slot_name);
1328 if (SCM_GOOPS_UNBOUNDP (res))
1329 return scm_call_3 (SCM_VARIABLE_REF (var_slot_unbound), class, obj, slot_name);
1330 return res;
1331}
1332#undef FUNC_NAME
1333
1334
1335SCM_DEFINE (scm_slot_set_using_class_x, "slot-set-using-class!", 4, 0, 0,
1336 (SCM class, SCM obj, SCM slot_name, SCM value),
1337 "")
1338#define FUNC_NAME s_scm_slot_set_using_class_x
1339{
1340 SCM_VALIDATE_CLASS (1, class);
1341 SCM_VALIDATE_INSTANCE (2, obj);
1342 SCM_VALIDATE_SYMBOL (3, slot_name);
1343
1344 return set_slot_value_using_name (class, obj, slot_name, value);
1345}
1346#undef FUNC_NAME
1347
1348
1349SCM_DEFINE (scm_slot_bound_using_class_p, "slot-bound-using-class?", 3, 0, 0,
1350 (SCM class, SCM obj, SCM slot_name),
1351 "")
1352#define FUNC_NAME s_scm_slot_bound_using_class_p
1353{
1354 SCM_VALIDATE_CLASS (1, class);
1355 SCM_VALIDATE_INSTANCE (2, obj);
1356 SCM_VALIDATE_SYMBOL (3, slot_name);
1357
1358 return (SCM_GOOPS_UNBOUNDP (get_slot_value_using_name (class, obj, slot_name))
1359 ? SCM_BOOL_F
1360 : SCM_BOOL_T);
1361}
1362#undef FUNC_NAME
1363
1364SCM_DEFINE (scm_slot_exists_using_class_p, "slot-exists-using-class?", 3, 0, 0,
1365 (SCM class, SCM obj, SCM slot_name),
1366 "")
1367#define FUNC_NAME s_scm_slot_exists_using_class_p
1368{
1369 SCM_VALIDATE_CLASS (1, class);
1370 SCM_VALIDATE_INSTANCE (2, obj);
1371 SCM_VALIDATE_SYMBOL (3, slot_name);
1372 return test_slot_existence (class, obj, slot_name);
1373}
1374#undef FUNC_NAME
1375
1376
1377 /* ======================================== */
1378
1379SCM_DEFINE (scm_slot_ref, "slot-ref", 2, 0, 0,
1380 (SCM obj, SCM slot_name),
1381 "Return the value from @var{obj}'s slot with the name\n"
1382 "@var{slot_name}.")
1383#define FUNC_NAME s_scm_slot_ref
1384{
1385 SCM res, class;
1386
1387 SCM_VALIDATE_INSTANCE (1, obj);
1388 TEST_CHANGE_CLASS (obj, class);
1389
1390 res = get_slot_value_using_name (class, obj, slot_name);
1391 if (SCM_GOOPS_UNBOUNDP (res))
1392 return scm_call_3 (SCM_VARIABLE_REF (var_slot_unbound), class, obj, slot_name);
1393 return res;
1394}
1395#undef FUNC_NAME
1396
1397SCM_DEFINE (scm_slot_set_x, "slot-set!", 3, 0, 0,
1398 (SCM obj, SCM slot_name, SCM value),
1399 "Set the slot named @var{slot_name} of @var{obj} to @var{value}.")
1400#define FUNC_NAME s_scm_slot_set_x
1401{
1402 SCM class;
1403
1404 SCM_VALIDATE_INSTANCE (1, obj);
1405 TEST_CHANGE_CLASS(obj, class);
1406
1407 return set_slot_value_using_name (class, obj, slot_name, value);
1408}
1409#undef FUNC_NAME
1410
1411const char *scm_s_slot_set_x = s_scm_slot_set_x;
1412
1413SCM_DEFINE (scm_slot_bound_p, "slot-bound?", 2, 0, 0,
1414 (SCM obj, SCM slot_name),
1415 "Return @code{#t} if the slot named @var{slot_name} of @var{obj}\n"
1416 "is bound.")
1417#define FUNC_NAME s_scm_slot_bound_p
1418{
1419 SCM class;
1420
1421 SCM_VALIDATE_INSTANCE (1, obj);
1422 TEST_CHANGE_CLASS(obj, class);
1423
1424 return (SCM_GOOPS_UNBOUNDP (get_slot_value_using_name (class,
1425 obj,
1426 slot_name))
1427 ? SCM_BOOL_F
1428 : SCM_BOOL_T);
1429}
1430#undef FUNC_NAME
1431
1432SCM_DEFINE (scm_slot_exists_p, "slot-exists?", 2, 0, 0,
1433 (SCM obj, SCM slot_name),
1434 "Return @code{#t} if @var{obj} has a slot named @var{slot_name}.")
1435#define FUNC_NAME s_scm_slot_exists_p
1436{
1437 SCM class;
1438
1439 SCM_VALIDATE_INSTANCE (1, obj);
1440 SCM_VALIDATE_SYMBOL (2, slot_name);
1441 TEST_CHANGE_CLASS (obj, class);
1442
1443 return test_slot_existence (class, obj, slot_name);
1444}
1445#undef FUNC_NAME
1446
1447
1448/******************************************************************************
1449 *
1450 * %allocate-instance (the low level instance allocation primitive)
1451 *
1452 ******************************************************************************/
1453
1454static void clear_method_cache (SCM);
1455
1456SCM_DEFINE (scm_sys_allocate_instance, "%allocate-instance", 2, 0, 0,
1457 (SCM class, SCM initargs),
1458 "Create a new instance of class @var{class} and initialize it\n"
1459 "from the arguments @var{initargs}.")
1460#define FUNC_NAME s_scm_sys_allocate_instance
1461{
1462 SCM obj;
1463 scm_t_signed_bits n, i;
1464 SCM layout;
1465
1466 SCM_VALIDATE_CLASS (1, class);
1467
1468 /* FIXME: duplicates some of scm_make_struct. */
1469
1470 n = SCM_I_INUM (SCM_SLOT (class, scm_si_nfields));
1471 obj = scm_i_alloc_struct (SCM_STRUCT_DATA (class), n);
1472
1473 layout = SCM_VTABLE_LAYOUT (class);
1474
1475 /* Set all SCM-holding slots to unbound */
1476 for (i = 0; i < n; i++)
1477 {
1478 scm_t_wchar c = scm_i_symbol_ref (layout, i*2);
1479 if (c == 'p')
1480 SCM_STRUCT_DATA (obj)[i] = SCM_UNPACK (SCM_GOOPS_UNBOUND);
1481 else if (c == 's')
1482 SCM_STRUCT_DATA (obj)[i] = SCM_UNPACK (obj);
1483 else
1484 SCM_STRUCT_DATA (obj)[i] = 0;
1485 }
1486
1487 if (SCM_CLASS_FLAGS (class) & SCM_CLASSF_PURE_GENERIC)
1488 clear_method_cache (obj);
1489
1490 return obj;
1491}
1492#undef FUNC_NAME
1493
1494SCM_DEFINE (scm_sys_set_object_setter_x, "%set-object-setter!", 2, 0, 0,
1495 (SCM obj, SCM setter),
1496 "")
1497#define FUNC_NAME s_scm_sys_set_object_setter_x
1498{
1499 SCM_ASSERT (SCM_STRUCTP (obj)
1500 && (SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC),
1501 obj,
1502 SCM_ARG1,
1503 FUNC_NAME);
1504 SCM_SET_GENERIC_SETTER (obj, setter);
1505 return SCM_UNSPECIFIED;
1506}
1507#undef FUNC_NAME
1508
1509/******************************************************************************
1510 *
1511 * %modify-instance (used by change-class to modify in place)
1512 *
1513 ******************************************************************************/
1514
1515SCM_DEFINE (scm_sys_modify_instance, "%modify-instance", 2, 0, 0,
1516 (SCM old, SCM new),
1517 "")
1518#define FUNC_NAME s_scm_sys_modify_instance
1519{
1520 SCM_VALIDATE_INSTANCE (1, old);
1521 SCM_VALIDATE_INSTANCE (2, new);
1522
1523 /* Exchange the data contained in old and new. We exchange rather than
1524 * scratch the old value with new to be correct with GC.
1525 * See "Class redefinition protocol above".
1526 */
1527 SCM_CRITICAL_SECTION_START;
1528 {
1529 scm_t_bits word0, word1;
1530 word0 = SCM_CELL_WORD_0 (old);
1531 word1 = SCM_CELL_WORD_1 (old);
1532 SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
1533 SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new));
1534 SCM_SET_CELL_WORD_0 (new, word0);
1535 SCM_SET_CELL_WORD_1 (new, word1);
1536 }
1537 SCM_CRITICAL_SECTION_END;
1538 return SCM_UNSPECIFIED;
1539}
1540#undef FUNC_NAME
1541
1542SCM_DEFINE (scm_sys_modify_class, "%modify-class", 2, 0, 0,
1543 (SCM old, SCM new),
1544 "")
1545#define FUNC_NAME s_scm_sys_modify_class
1546{
1547 SCM_VALIDATE_CLASS (1, old);
1548 SCM_VALIDATE_CLASS (2, new);
1549
1550 SCM_CRITICAL_SECTION_START;
1551 {
1552 scm_t_bits word0, word1;
1553 word0 = SCM_CELL_WORD_0 (old);
1554 word1 = SCM_CELL_WORD_1 (old);
1555 SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
1556 SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new));
1557 SCM_STRUCT_DATA (old)[scm_vtable_index_self] = SCM_UNPACK (old);
1558 SCM_SET_CELL_WORD_0 (new, word0);
1559 SCM_SET_CELL_WORD_1 (new, word1);
1560 SCM_STRUCT_DATA (new)[scm_vtable_index_self] = SCM_UNPACK (new);
1561 }
1562 SCM_CRITICAL_SECTION_END;
1563 return SCM_UNSPECIFIED;
1564}
1565#undef FUNC_NAME
1566
1567SCM_DEFINE (scm_sys_invalidate_class, "%invalidate-class", 1, 0, 0,
1568 (SCM class),
1569 "")
1570#define FUNC_NAME s_scm_sys_invalidate_class
1571{
1572 SCM_VALIDATE_CLASS (1, class);
1573 SCM_CLEAR_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_VALID);
1574 return SCM_UNSPECIFIED;
1575}
1576#undef FUNC_NAME
1577
1578/* When instances change class, they finally get a new body, but
1579 * before that, they go through purgatory in hell. Odd as it may
1580 * seem, this data structure saves us from eternal suffering in
1581 * infinite recursions.
1582 */
1583
1584static scm_t_bits **hell;
1585static long n_hell = 1; /* one place for the evil one himself */
1586static long hell_size = 4;
1587static SCM hell_mutex;
1588
1589static long
1590burnin (SCM o)
1591{
1592 long i;
1593 for (i = 1; i < n_hell; ++i)
1594 if (SCM_STRUCT_DATA (o) == hell[i])
1595 return i;
1596 return 0;
1597}
1598
1599static void
1600go_to_hell (void *o)
1601{
1602 SCM obj = *(SCM*)o;
1603 scm_lock_mutex (hell_mutex);
1604 if (n_hell >= hell_size)
1605 {
1606 hell_size *= 2;
1607 hell = scm_realloc (hell, hell_size * sizeof(*hell));
1608 }
1609 hell[n_hell++] = SCM_STRUCT_DATA (obj);
1610 scm_unlock_mutex (hell_mutex);
1611}
1612
1613static void
1614go_to_heaven (void *o)
1615{
1616 SCM obj = *(SCM*)o;
1617 scm_lock_mutex (hell_mutex);
1618 hell[burnin (obj)] = hell[--n_hell];
1619 scm_unlock_mutex (hell_mutex);
1620}
1621
1622
1623SCM_SYMBOL (scm_sym_change_class, "change-class");
1624
1625static SCM
1626purgatory (SCM obj, SCM new_class)
1627{
1628 return scm_call_2 (SCM_VARIABLE_REF (var_change_class), obj, new_class);
1629}
1630
1631/* This function calls the generic function change-class for all
1632 * instances which aren't currently undergoing class change.
1633 */
1634
1635void
1636scm_change_object_class (SCM obj, SCM old_class SCM_UNUSED, SCM new_class)
1637{
1638 if (!burnin (obj))
1639 {
1640 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
1641 scm_dynwind_rewind_handler (go_to_hell, &obj, SCM_F_WIND_EXPLICITLY);
1642 scm_dynwind_unwind_handler (go_to_heaven, &obj, SCM_F_WIND_EXPLICITLY);
1643 purgatory (obj, new_class);
1644 scm_dynwind_end ();
1645 }
1646}
1647
1648/******************************************************************************
1649 *
1650 * GGGG FFFFF
1651 * G F
1652 * G GG FFF
1653 * G G F
1654 * GGG E N E R I C F U N C T I O N S
1655 *
1656 * This implementation provides
1657 * - generic functions (with class specializers)
1658 * - multi-methods
1659 * - next-method
1660 * - a hard-coded MOP for standard gf, which can be overloaded for non-std gf
1661 *
1662 ******************************************************************************/
1663
1664SCM_KEYWORD (k_name, "name");
1665
1666SCM_GLOBAL_SYMBOL (scm_sym_args, "args");
1667
1668
1669SCM
1670scm_apply_generic (SCM gf, SCM args)
1671{
1672 return scm_apply (SCM_STRUCT_PROCEDURE (gf), args, SCM_EOL);
1673}
1674
1675SCM
1676scm_call_generic_0 (SCM gf)
1677{
1678 return scm_call_0 (SCM_STRUCT_PROCEDURE (gf));
1679}
1680
1681SCM
1682scm_call_generic_1 (SCM gf, SCM a1)
1683{
1684 return scm_call_1 (SCM_STRUCT_PROCEDURE (gf), a1);
1685}
1686
1687SCM
1688scm_call_generic_2 (SCM gf, SCM a1, SCM a2)
1689{
1690 return scm_call_2 (SCM_STRUCT_PROCEDURE (gf), a1, a2);
1691}
1692
1693SCM
1694scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3)
1695{
1696 return scm_call_3 (SCM_STRUCT_PROCEDURE (gf), a1, a2, a3);
1697}
1698
1699SCM_SYMBOL (sym_delayed_compile, "delayed-compile");
1700static SCM
1701make_dispatch_procedure (SCM gf)
1702{
1703 static SCM var = SCM_BOOL_F;
1704 if (var == SCM_BOOL_F)
1705 var = scm_module_variable (scm_c_resolve_module ("oop goops dispatch"),
1706 sym_delayed_compile);
1707 return scm_call_1 (SCM_VARIABLE_REF (var), gf);
1708}
1709
1710static void
1711clear_method_cache (SCM gf)
1712{
1713 SCM_SET_GENERIC_DISPATCH_PROCEDURE (gf, make_dispatch_procedure (gf));
1714 SCM_CLEAR_GENERIC_EFFECTIVE_METHODS (gf);
1715}
1716
1717SCM_DEFINE (scm_sys_invalidate_method_cache_x, "%invalidate-method-cache!", 1, 0, 0,
1718 (SCM gf),
1719 "")
1720#define FUNC_NAME s_scm_sys_invalidate_method_cache_x
1721{
1722 SCM_ASSERT (SCM_PUREGENERICP (gf), gf, SCM_ARG1, FUNC_NAME);
1723 clear_method_cache (gf);
1724 return SCM_UNSPECIFIED;
1725}
1726#undef FUNC_NAME
1727
1728SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
1729 (SCM proc),
1730 "")
1731#define FUNC_NAME s_scm_generic_capability_p
1732{
1733 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
1734 proc, SCM_ARG1, FUNC_NAME);
1735 return (SCM_PRIMITIVE_GENERIC_P (proc) ? SCM_BOOL_T : SCM_BOOL_F);
1736}
1737#undef FUNC_NAME
1738
1739SCM_DEFINE (scm_enable_primitive_generic_x, "enable-primitive-generic!", 0, 0, 1,
1740 (SCM subrs),
1741 "")
1742#define FUNC_NAME s_scm_enable_primitive_generic_x
1743{
1744 SCM_VALIDATE_REST_ARGUMENT (subrs);
1745 while (!scm_is_null (subrs))
1746 {
1747 SCM subr = SCM_CAR (subrs);
1748 SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARGn, FUNC_NAME);
1749 SCM_SET_SUBR_GENERIC (subr,
1750 scm_make (scm_list_3 (scm_class_generic,
1751 k_name,
1752 SCM_SUBR_NAME (subr))));
1753 subrs = SCM_CDR (subrs);
1754 }
1755 return SCM_UNSPECIFIED;
1756}
1757#undef FUNC_NAME
1758
1759SCM_DEFINE (scm_set_primitive_generic_x, "set-primitive-generic!", 2, 0, 0,
1760 (SCM subr, SCM generic),
1761 "")
1762#define FUNC_NAME s_scm_set_primitive_generic_x
1763{
1764 SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARG1, FUNC_NAME);
1765 SCM_ASSERT (SCM_PUREGENERICP (generic), generic, SCM_ARG2, FUNC_NAME);
1766 SCM_SET_SUBR_GENERIC (subr, generic);
1767 return SCM_UNSPECIFIED;
1768}
1769#undef FUNC_NAME
1770
1771SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
1772 (SCM subr),
1773 "")
1774#define FUNC_NAME s_scm_primitive_generic_generic
1775{
1776 if (SCM_PRIMITIVE_GENERIC_P (subr))
1777 {
1778 if (!*SCM_SUBR_GENERIC (subr))
1779 scm_enable_primitive_generic_x (scm_list_1 (subr));
1780 return *SCM_SUBR_GENERIC (subr);
1781 }
1782 SCM_WRONG_TYPE_ARG (SCM_ARG1, subr);
1783}
1784#undef FUNC_NAME
1785
1786typedef struct t_extension {
1787 struct t_extension *next;
1788 SCM extended;
1789 SCM extension;
1790} t_extension;
1791
1792
1793/* Hint for `scm_gc_malloc ()' et al. when allocating `t_extension'
1794 objects. */
1795static const char extension_gc_hint[] = "GOOPS extension";
1796
1797static t_extension *extensions = 0;
1798
1799void
1800scm_c_extend_primitive_generic (SCM extended, SCM extension)
1801{
1802 if (goops_loaded_p)
1803 {
1804 SCM gf, gext;
1805 if (!*SCM_SUBR_GENERIC (extended))
1806 scm_enable_primitive_generic_x (scm_list_1 (extended));
1807 gf = *SCM_SUBR_GENERIC (extended);
1808 gext = scm_call_2 (SCM_VARIABLE_REF (scm_var_make_extended_generic),
1809 gf,
1810 SCM_SUBR_NAME (extension));
1811 SCM_SET_SUBR_GENERIC (extension, gext);
1812 }
1813 else
1814 {
1815 t_extension *e = scm_gc_malloc (sizeof (t_extension),
1816 extension_gc_hint);
1817 t_extension **loc = &extensions;
1818 /* Make sure that extensions are placed before their own
1819 * extensions in the extensions list. O(N^2) algorithm, but
1820 * extensions of primitive generics are rare.
1821 */
1822 while (*loc && extension != (*loc)->extended)
1823 loc = &(*loc)->next;
1824 e->next = *loc;
1825 e->extended = extended;
1826 e->extension = extension;
1827 *loc = e;
1828 }
1829}
1830
1831static void
1832setup_extended_primitive_generics ()
1833{
1834 while (extensions)
1835 {
1836 t_extension *e = extensions;
1837 scm_c_extend_primitive_generic (e->extended, e->extension);
1838 extensions = e->next;
1839 }
1840}
1841
1842/******************************************************************************
1843 *
1844 * Protocol for calling a generic fumction
1845 * This protocol is roughly equivalent to (parameter are a little bit different
1846 * for efficiency reasons):
1847 *
1848 * + apply-generic (gf args)
1849 * + compute-applicable-methods (gf args ...)
1850 * + sort-applicable-methods (methods args)
1851 * + apply-methods (gf methods args)
1852 *
1853 * apply-methods calls make-next-method to build the "continuation" of a a
1854 * method. Applying a next-method will call apply-next-method which in
1855 * turn will call apply again to call effectively the following method.
1856 *
1857 ******************************************************************************/
1858
1859static int
1860applicablep (SCM actual, SCM formal)
1861{
1862 /* We already know that the cpl is well formed. */
1863 return scm_is_true (scm_c_memq (formal, SCM_SLOT (actual, scm_si_cpl)));
1864}
1865
1866static int
1867more_specificp (SCM m1, SCM m2, SCM const *targs)
1868{
1869 register SCM s1, s2;
1870 register long i;
1871 /*
1872 * Note:
1873 * m1 and m2 can have != length (i.e. one can be one element longer than the
1874 * other when we have a dotted parameter list). For instance, with the call
1875 * (M 1)
1876 * with
1877 * (define-method M (a . l) ....)
1878 * (define-method M (a) ....)
1879 *
1880 * we consider that the second method is more specific.
1881 *
1882 * BTW, targs is an array of types. We don't need it's size since
1883 * we already know that m1 and m2 are applicable (no risk to go past
1884 * the end of this array).
1885 *
1886 */
1887 for (i=0, s1=SPEC_OF(m1), s2=SPEC_OF(m2); ; i++, s1=SCM_CDR(s1), s2=SCM_CDR(s2)) {
1888 if (scm_is_null(s1)) return 1;
1889 if (scm_is_null(s2)) return 0;
1890 if (SCM_CAR(s1) != SCM_CAR(s2)) {
1891 register SCM l, cs1 = SCM_CAR(s1), cs2 = SCM_CAR(s2);
1892
1893 for (l = SCM_SLOT (targs[i], scm_si_cpl); ; l = SCM_CDR(l)) {
1894 if (cs1 == SCM_CAR(l))
1895 return 1;
1896 if (cs2 == SCM_CAR(l))
1897 return 0;
1898 }
1899 return 0;/* should not occur! */
1900 }
1901 }
1902 return 0; /* should not occur! */
1903}
1904
1905#define BUFFSIZE 32 /* big enough for most uses */
1906
1907static SCM
1908scm_i_vector2list (SCM l, long len)
1909{
1910 long j;
1911 SCM z = scm_c_make_vector (len, SCM_UNDEFINED);
1912
1913 for (j = 0; j < len; j++, l = SCM_CDR (l)) {
1914 SCM_SIMPLE_VECTOR_SET (z, j, SCM_CAR (l));
1915 }
1916 return z;
1917}
1918
1919static SCM
1920sort_applicable_methods (SCM method_list, long size, SCM const *targs)
1921{
1922 long i, j, incr;
1923 SCM *v, vector = SCM_EOL;
1924 SCM buffer[BUFFSIZE];
1925 SCM save = method_list;
1926 scm_t_array_handle handle;
1927
1928 /* For reasonably sized method_lists we can try to avoid all the
1929 * consing and reorder the list in place...
1930 * This idea is due to David McClain <Dave_McClain@msn.com>
1931 */
1932 if (size <= BUFFSIZE)
1933 {
1934 for (i = 0; i < size; i++)
1935 {
1936 buffer[i] = SCM_CAR (method_list);
1937 method_list = SCM_CDR (method_list);
1938 }
1939 v = buffer;
1940 }
1941 else
1942 {
1943 /* Too many elements in method_list to keep everything locally */
1944 vector = scm_i_vector2list (save, size);
1945 v = scm_vector_writable_elements (vector, &handle, NULL, NULL);
1946 }
1947
1948 /* Use a simple shell sort since it is generally faster than qsort on
1949 * small vectors (which is probably mostly the case when we have to
1950 * sort a list of applicable methods).
1951 */
1952 for (incr = size / 2; incr; incr /= 2)
1953 {
1954 for (i = incr; i < size; i++)
1955 {
1956 for (j = i - incr; j >= 0; j -= incr)
1957 {
1958 if (more_specificp (v[j], v[j+incr], targs))
1959 break;
1960 else
1961 {
1962 SCM tmp = v[j + incr];
1963 v[j + incr] = v[j];
1964 v[j] = tmp;
1965 }
1966 }
1967 }
1968 }
1969
1970 if (size <= BUFFSIZE)
1971 {
1972 /* We did it in locally, so restore the original list (reordered) in-place */
1973 for (i = 0, method_list = save; i < size; i++, v++)
1974 {
1975 SCM_SETCAR (method_list, *v);
1976 method_list = SCM_CDR (method_list);
1977 }
1978 return save;
1979 }
1980
1981 /* If we are here, that's that we did it the hard way... */
1982 scm_array_handle_release (&handle);
1983 return scm_vector_to_list (vector);
1984}
1985
1986SCM
1987scm_compute_applicable_methods (SCM gf, SCM args, long len, int find_method_p)
1988{
1989 register long i;
1990 long count = 0;
1991 SCM l, fl, applicable = SCM_EOL;
1992 SCM save = args;
1993 SCM buffer[BUFFSIZE];
1994 SCM const *types;
1995 SCM *p;
1996 SCM tmp = SCM_EOL;
1997 scm_t_array_handle handle;
1998
1999 /* Build the list of arguments types */
2000 if (len >= BUFFSIZE)
2001 {
2002 tmp = scm_c_make_vector (len, SCM_UNDEFINED);
2003 types = p = scm_vector_writable_elements (tmp, &handle, NULL, NULL);
2004
2005 /*
2006 note that we don't have to work to reset the generation
2007 count. TMP is a new vector anyway, and it is found
2008 conservatively.
2009 */
2010 }
2011 else
2012 types = p = buffer;
2013
2014 for ( ; !scm_is_null (args); args = SCM_CDR (args))
2015 *p++ = scm_class_of (SCM_CAR (args));
2016
2017 /* Build a list of all applicable methods */
2018 for (l = scm_generic_function_methods (gf); !scm_is_null (l); l = SCM_CDR (l))
2019 {
2020 fl = SPEC_OF (SCM_CAR (l));
2021 for (i = 0; ; i++, fl = SCM_CDR (fl))
2022 {
2023 if (SCM_INSTANCEP (fl)
2024 /* We have a dotted argument list */
2025 || (i >= len && scm_is_null (fl)))
2026 { /* both list exhausted */
2027 applicable = scm_cons (SCM_CAR (l), applicable);
2028 count += 1;
2029 break;
2030 }
2031 if (i >= len
2032 || scm_is_null (fl)
2033 || !applicablep (types[i], SCM_CAR (fl)))
2034 break;
2035 }
2036 }
2037
2038 if (len >= BUFFSIZE)
2039 scm_array_handle_release (&handle);
2040
2041 if (count == 0)
2042 {
2043 if (find_method_p)
2044 return SCM_BOOL_F;
2045 scm_call_2 (SCM_VARIABLE_REF (var_no_applicable_method), gf, save);
2046 /* if we are here, it's because no-applicable-method hasn't signaled an error */
2047 return SCM_BOOL_F;
2048 }
2049
2050 return (count == 1
2051 ? applicable
2052 : sort_applicable_methods (applicable, count, types));
2053}
2054
2055#if 0
2056SCM_PROC (s_sys_compute_applicable_methods, "%compute-applicable-methods", 2, 0, 0, scm_sys_compute_applicable_methods);
2057#endif
2058
2059static const char s_sys_compute_applicable_methods[] = "%compute-applicable-methods";
2060
2061SCM
2062scm_sys_compute_applicable_methods (SCM gf, SCM args)
2063#define FUNC_NAME s_sys_compute_applicable_methods
2064{
2065 long n;
2066 SCM_VALIDATE_GENERIC (1, gf);
2067 n = scm_ilength (args);
2068 SCM_ASSERT (n >= 0, args, SCM_ARG2, FUNC_NAME);
2069 return scm_compute_applicable_methods (gf, args, n, 1);
2070}
2071#undef FUNC_NAME
2072
2073SCM_SYMBOL (sym_compute_applicable_methods, "compute-applicable-methods");
2074SCM_VARIABLE_INIT (var_compute_applicable_methods, "compute-applicable-methods",
2075 scm_c_define_gsubr (s_sys_compute_applicable_methods, 2, 0, 0,
2076 scm_sys_compute_applicable_methods));
2077
2078/******************************************************************************
2079 *
2080 * A simple make (which will be redefined later in Scheme)
2081 * This version handles only creation of gf, methods and classes (no instances)
2082 *
2083 * Since this code will disappear when Goops will be fully booted,
2084 * no precaution is taken to be efficient.
2085 *
2086 ******************************************************************************/
2087
2088SCM_KEYWORD (k_setter, "setter");
2089SCM_KEYWORD (k_specializers, "specializers");
2090SCM_KEYWORD (k_procedure, "procedure");
2091SCM_KEYWORD (k_formals, "formals");
2092SCM_KEYWORD (k_body, "body");
2093SCM_KEYWORD (k_make_procedure, "make-procedure");
2094SCM_KEYWORD (k_dsupers, "dsupers");
2095SCM_KEYWORD (k_slots, "slots");
2096SCM_KEYWORD (k_gf, "generic-function");
2097
2098SCM_DEFINE (scm_make, "make", 0, 0, 1,
2099 (SCM args),
2100 "Make a new object. @var{args} must contain the class and\n"
2101 "all necessary initialization information.")
2102#define FUNC_NAME s_scm_make
2103{
2104 SCM class, z;
2105 long len = scm_ilength (args);
2106
2107 if (len <= 0 || (len & 1) == 0)
2108 SCM_WRONG_NUM_ARGS ();
2109
2110 class = SCM_CAR(args);
2111 args = SCM_CDR(args);
2112
2113 if (class == scm_class_generic || class == scm_class_accessor)
2114 {
2115 z = scm_make_struct (class, SCM_INUM0,
2116 scm_list_4 (SCM_BOOL_F,
2117 SCM_EOL,
2118 SCM_INUM0,
2119 SCM_EOL));
2120 scm_set_procedure_property_x (z, scm_sym_name,
2121 scm_get_keyword (k_name,
2122 args,
2123 SCM_BOOL_F));
2124 clear_method_cache (z);
2125 if (class == scm_class_accessor)
2126 {
2127 SCM setter = scm_get_keyword (k_setter, args, SCM_BOOL_F);
2128 if (scm_is_true (setter))
2129 scm_sys_set_object_setter_x (z, setter);
2130 }
2131 }
2132 else
2133 {
2134 z = scm_sys_allocate_instance (class, args);
2135
2136 if (class == scm_class_method
2137 || class == scm_class_accessor_method)
2138 {
2139 SCM_SET_SLOT (z, scm_si_generic_function,
2140 scm_i_get_keyword (k_gf,
2141 args,
2142 len - 1,
2143 SCM_BOOL_F,
2144 FUNC_NAME));
2145 SCM_SET_SLOT (z, scm_si_specializers,
2146 scm_i_get_keyword (k_specializers,
2147 args,
2148 len - 1,
2149 SCM_EOL,
2150 FUNC_NAME));
2151 SCM_SET_SLOT (z, scm_si_procedure,
2152 scm_i_get_keyword (k_procedure,
2153 args,
2154 len - 1,
2155 SCM_BOOL_F,
2156 FUNC_NAME));
2157 SCM_SET_SLOT (z, scm_si_formals,
2158 scm_i_get_keyword (k_formals,
2159 args,
2160 len - 1,
2161 SCM_EOL,
2162 FUNC_NAME));
2163 SCM_SET_SLOT (z, scm_si_body,
2164 scm_i_get_keyword (k_body,
2165 args,
2166 len - 1,
2167 SCM_EOL,
2168 FUNC_NAME));
2169 SCM_SET_SLOT (z, scm_si_make_procedure,
2170 scm_i_get_keyword (k_make_procedure,
2171 args,
2172 len - 1,
2173 SCM_BOOL_F,
2174 FUNC_NAME));
2175 }
2176 else
2177 {
2178 /* In all the others case, make a new class .... No instance here */
2179 SCM_SET_SLOT (z, scm_vtable_index_name,
2180 scm_i_get_keyword (k_name,
2181 args,
2182 len - 1,
2183 scm_from_latin1_symbol ("???"),
2184 FUNC_NAME));
2185 SCM_SET_SLOT (z, scm_si_direct_supers,
2186 scm_i_get_keyword (k_dsupers,
2187 args,
2188 len - 1,
2189 SCM_EOL,
2190 FUNC_NAME));
2191 SCM_SET_SLOT (z, scm_si_direct_slots,
2192 scm_i_get_keyword (k_slots,
2193 args,
2194 len - 1,
2195 SCM_EOL,
2196 FUNC_NAME));
2197 }
2198 }
2199 return z;
2200}
2201#undef FUNC_NAME
2202
2203SCM_DEFINE (scm_find_method, "find-method", 0, 0, 1,
2204 (SCM l),
2205 "")
2206#define FUNC_NAME s_scm_find_method
2207{
2208 SCM gf;
2209 long len = scm_ilength (l);
2210
2211 if (len == 0)
2212 SCM_WRONG_NUM_ARGS ();
2213
2214 gf = SCM_CAR(l); l = SCM_CDR(l);
2215 SCM_VALIDATE_GENERIC (1, gf);
2216 if (scm_is_null (SCM_SLOT (gf, scm_si_methods)))
2217 SCM_MISC_ERROR ("no methods for generic ~S", scm_list_1 (gf));
2218
2219 return scm_compute_applicable_methods (gf, l, len - 1, 1);
2220}
2221#undef FUNC_NAME
2222
2223SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0,
2224 (SCM m1, SCM m2, SCM targs),
2225 "Return true if method @var{m1} is more specific than @var{m2} "
2226 "given the argument types (classes) listed in @var{targs}.")
2227#define FUNC_NAME s_scm_sys_method_more_specific_p
2228{
2229 SCM l, v, result;
2230 SCM *v_elts;
2231 long i, len, m1_specs, m2_specs;
2232 scm_t_array_handle handle;
2233
2234 SCM_VALIDATE_METHOD (1, m1);
2235 SCM_VALIDATE_METHOD (2, m2);
2236
2237 len = scm_ilength (targs);
2238 m1_specs = scm_ilength (SPEC_OF (m1));
2239 m2_specs = scm_ilength (SPEC_OF (m2));
2240 SCM_ASSERT ((len >= m1_specs) || (len >= m2_specs),
2241 targs, SCM_ARG3, FUNC_NAME);
2242
2243 /* Verify that all the arguments of TARGS are classes and place them
2244 in a vector. */
2245
2246 v = scm_c_make_vector (len, SCM_EOL);
2247 v_elts = scm_vector_writable_elements (v, &handle, NULL, NULL);
2248
2249 for (i = 0, l = targs;
2250 i < len && scm_is_pair (l);
2251 i++, l = SCM_CDR (l))
2252 {
2253 SCM_ASSERT (SCM_CLASSP (SCM_CAR (l)), targs, SCM_ARG3, FUNC_NAME);
2254 v_elts[i] = SCM_CAR (l);
2255 }
2256 result = more_specificp (m1, m2, v_elts) ? SCM_BOOL_T: SCM_BOOL_F;
2257
2258 scm_array_handle_release (&handle);
2259
2260 return result;
2261}
2262#undef FUNC_NAME
2263
2264
2265
2266/******************************************************************************
2267 *
2268 * Initializations
2269 *
2270 ******************************************************************************/
2271
2272static void
2273fix_cpl (SCM c, SCM before, SCM after)
2274{
2275 SCM cpl = SCM_SLOT (c, scm_si_cpl);
2276 SCM ls = scm_c_memq (after, cpl);
2277 SCM tail = scm_delq1_x (before, SCM_CDR (ls));
2278 if (scm_is_false (ls))
2279 /* if this condition occurs, fix_cpl should not be applied this way */
2280 abort ();
2281 SCM_SETCAR (ls, before);
2282 SCM_SETCDR (ls, scm_cons (after, tail));
2283 {
2284 SCM dslots = SCM_SLOT (c, scm_si_direct_slots);
2285 SCM slots = build_slots_list (maplist (dslots), cpl);
2286 SCM g_n_s = compute_getters_n_setters (slots);
2287 SCM_SET_SLOT (c, scm_si_slots, slots);
2288 SCM_SET_SLOT (c, scm_si_getters_n_setters, g_n_s);
2289 }
2290}
2291
2292
2293static void
2294make_stdcls (SCM *var, char *name, SCM meta, SCM super, SCM slots)
2295{
2296 SCM tmp = scm_from_locale_symbol (name);
2297
2298 *var = scm_basic_make_class (meta, tmp,
2299 scm_is_pair (super) ? super : scm_list_1 (super),
2300 slots);
2301 DEFVAR(tmp, *var);
2302}
2303
2304
2305SCM_KEYWORD (k_slot_definition, "slot-definition");
2306
2307static void
2308create_standard_classes (void)
2309{
2310 SCM slots;
2311 SCM method_slots = scm_list_n (scm_from_latin1_symbol ("generic-function"),
2312 scm_from_latin1_symbol ("specializers"),
2313 sym_procedure,
2314 scm_from_latin1_symbol ("formals"),
2315 scm_from_latin1_symbol ("body"),
2316 scm_from_latin1_symbol ("make-procedure"),
2317 SCM_UNDEFINED);
2318 SCM amethod_slots = scm_list_1 (scm_list_3 (scm_from_latin1_symbol ("slot-definition"),
2319 k_init_keyword,
2320 k_slot_definition));
2321 SCM gf_slots = scm_list_4 (scm_from_latin1_symbol ("methods"),
2322 scm_list_3 (scm_from_latin1_symbol ("n-specialized"),
2323 k_init_value,
2324 SCM_INUM0),
2325 scm_list_3 (scm_from_latin1_symbol ("extended-by"),
2326 k_init_value,
2327 SCM_EOL),
2328 scm_from_latin1_symbol ("effective-methods"));
2329 SCM setter_slots = scm_list_1 (sym_setter);
2330 SCM egf_slots = scm_list_1 (scm_list_3 (scm_from_latin1_symbol ("extends"),
2331 k_init_value,
2332 SCM_EOL));
2333 /* Foreign class slot classes */
2334 make_stdcls (&scm_class_foreign_slot, "<foreign-slot>",
2335 scm_class_class, scm_class_top, SCM_EOL);
2336 make_stdcls (&scm_class_protected, "<protected-slot>",
2337 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2338 make_stdcls (&scm_class_hidden, "<hidden-slot>",
2339 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2340 make_stdcls (&scm_class_opaque, "<opaque-slot>",
2341 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2342 make_stdcls (&scm_class_read_only, "<read-only-slot>",
2343 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2344 make_stdcls (&scm_class_self, "<self-slot>",
2345 scm_class_class, scm_class_read_only, SCM_EOL);
2346 make_stdcls (&scm_class_protected_opaque, "<protected-opaque-slot>",
2347 scm_class_class,
2348 scm_list_2 (scm_class_protected, scm_class_opaque),
2349 SCM_EOL);
2350 make_stdcls (&scm_class_protected_hidden, "<protected-hidden-slot>",
2351 scm_class_class,
2352 scm_list_2 (scm_class_protected, scm_class_hidden),
2353 SCM_EOL);
2354 make_stdcls (&scm_class_protected_read_only, "<protected-read-only-slot>",
2355 scm_class_class,
2356 scm_list_2 (scm_class_protected, scm_class_read_only),
2357 SCM_EOL);
2358 make_stdcls (&scm_class_scm, "<scm-slot>",
2359 scm_class_class, scm_class_protected, SCM_EOL);
2360 make_stdcls (&scm_class_int, "<int-slot>",
2361 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2362 make_stdcls (&scm_class_float, "<float-slot>",
2363 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2364 make_stdcls (&scm_class_double, "<double-slot>",
2365 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2366
2367 /* Continue initialization of class <class> */
2368
2369 slots = build_class_class_slots ();
2370 SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots);
2371 SCM_SET_SLOT (scm_class_class, scm_si_slots, slots);
2372 SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters,
2373 compute_getters_n_setters (slots));
2374
2375 /* scm_class_generic functions classes */
2376 make_stdcls (&scm_class_procedure_class, "<procedure-class>",
2377 scm_class_class, scm_class_class, SCM_EOL);
2378 make_stdcls (&scm_class_applicable_struct_class, "<applicable-struct-class>",
2379 scm_class_class, scm_class_procedure_class, SCM_EOL);
2380 SCM_SET_VTABLE_FLAGS (scm_class_applicable_struct_class, SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
2381 make_stdcls (&scm_class_method, "<method>",
2382 scm_class_class, scm_class_object, method_slots);
2383 make_stdcls (&scm_class_accessor_method, "<accessor-method>",
2384 scm_class_class, scm_class_method, amethod_slots);
2385 make_stdcls (&scm_class_applicable, "<applicable>",
2386 scm_class_class, scm_class_top, SCM_EOL);
2387 make_stdcls (&scm_class_applicable_struct, "<applicable-struct>",
2388 scm_class_applicable_struct_class,
2389 scm_list_2 (scm_class_object, scm_class_applicable),
2390 scm_list_1 (sym_procedure));
2391 make_stdcls (&scm_class_generic, "<generic>",
2392 scm_class_applicable_struct_class, scm_class_applicable_struct, gf_slots);
2393 SCM_SET_CLASS_FLAGS (scm_class_generic, SCM_CLASSF_PURE_GENERIC);
2394 make_stdcls (&scm_class_extended_generic, "<extended-generic>",
2395 scm_class_applicable_struct_class, scm_class_generic, egf_slots);
2396 SCM_SET_CLASS_FLAGS (scm_class_extended_generic, SCM_CLASSF_PURE_GENERIC);
2397 make_stdcls (&scm_class_generic_with_setter, "<generic-with-setter>",
2398 scm_class_applicable_struct_class, scm_class_generic, setter_slots);
2399 SCM_SET_CLASS_FLAGS (scm_class_generic_with_setter, SCM_CLASSF_PURE_GENERIC);
2400 make_stdcls (&scm_class_accessor, "<accessor>",
2401 scm_class_applicable_struct_class, scm_class_generic_with_setter, SCM_EOL);
2402 SCM_SET_CLASS_FLAGS (scm_class_accessor, SCM_CLASSF_PURE_GENERIC);
2403 make_stdcls (&scm_class_extended_generic_with_setter,
2404 "<extended-generic-with-setter>",
2405 scm_class_applicable_struct_class,
2406 scm_list_2 (scm_class_generic_with_setter,
2407 scm_class_extended_generic),
2408 SCM_EOL);
2409 SCM_SET_CLASS_FLAGS (scm_class_extended_generic_with_setter,
2410 SCM_CLASSF_PURE_GENERIC);
2411 make_stdcls (&scm_class_extended_accessor, "<extended-accessor>",
2412 scm_class_applicable_struct_class,
2413 scm_list_2 (scm_class_accessor,
2414 scm_class_extended_generic_with_setter),
2415 SCM_EOL);
2416 fix_cpl (scm_class_extended_accessor,
2417 scm_class_extended_generic, scm_class_generic);
2418 SCM_SET_CLASS_FLAGS (scm_class_extended_accessor, SCM_CLASSF_PURE_GENERIC);
2419
2420 /* Primitive types classes */
2421 make_stdcls (&scm_class_boolean, "<boolean>",
2422 scm_class_class, scm_class_top, SCM_EOL);
2423 make_stdcls (&scm_class_char, "<char>",
2424 scm_class_class, scm_class_top, SCM_EOL);
2425 make_stdcls (&scm_class_list, "<list>",
2426 scm_class_class, scm_class_top, SCM_EOL);
2427 make_stdcls (&scm_class_pair, "<pair>",
2428 scm_class_class, scm_class_list, SCM_EOL);
2429 make_stdcls (&scm_class_null, "<null>",
2430 scm_class_class, scm_class_list, SCM_EOL);
2431 make_stdcls (&scm_class_string, "<string>",
2432 scm_class_class, scm_class_top, SCM_EOL);
2433 make_stdcls (&scm_class_symbol, "<symbol>",
2434 scm_class_class, scm_class_top, SCM_EOL);
2435 make_stdcls (&scm_class_vector, "<vector>",
2436 scm_class_class, scm_class_top, SCM_EOL);
2437 make_stdcls (&class_foreign, "<foreign>",
2438 scm_class_class, scm_class_top, SCM_EOL);
2439 make_stdcls (&class_hashtable, "<hashtable>",
2440 scm_class_class, scm_class_top, SCM_EOL);
2441 make_stdcls (&class_fluid, "<fluid>",
2442 scm_class_class, scm_class_top, SCM_EOL);
2443 make_stdcls (&class_dynamic_state, "<dynamic-state>",
2444 scm_class_class, scm_class_top, SCM_EOL);
2445 make_stdcls (&class_frame, "<frame>",
2446 scm_class_class, scm_class_top, SCM_EOL);
2447 make_stdcls (&class_objcode, "<objcode>",
2448 scm_class_class, scm_class_top, SCM_EOL);
2449 make_stdcls (&class_vm, "<vm>",
2450 scm_class_class, scm_class_top, SCM_EOL);
2451 make_stdcls (&class_vm_cont, "<vm-continuation>",
2452 scm_class_class, scm_class_top, SCM_EOL);
2453 make_stdcls (&class_bytevector, "<bytevector>",
2454 scm_class_class, scm_class_top, SCM_EOL);
2455 make_stdcls (&class_uvec, "<uvec>",
2456 scm_class_class, class_bytevector, SCM_EOL);
2457 make_stdcls (&scm_class_number, "<number>",
2458 scm_class_class, scm_class_top, SCM_EOL);
2459 make_stdcls (&scm_class_complex, "<complex>",
2460 scm_class_class, scm_class_number, SCM_EOL);
2461 make_stdcls (&scm_class_real, "<real>",
2462 scm_class_class, scm_class_complex, SCM_EOL);
2463 make_stdcls (&scm_class_integer, "<integer>",
2464 scm_class_class, scm_class_real, SCM_EOL);
2465 make_stdcls (&scm_class_fraction, "<fraction>",
2466 scm_class_class, scm_class_real, SCM_EOL);
2467 make_stdcls (&scm_class_keyword, "<keyword>",
2468 scm_class_class, scm_class_top, SCM_EOL);
2469 make_stdcls (&scm_class_unknown, "<unknown>",
2470 scm_class_class, scm_class_top, SCM_EOL);
2471 make_stdcls (&scm_class_procedure, "<procedure>",
2472 scm_class_procedure_class, scm_class_applicable, SCM_EOL);
2473 make_stdcls (&scm_class_primitive_generic, "<primitive-generic>",
2474 scm_class_procedure_class, scm_class_procedure, SCM_EOL);
2475 make_stdcls (&scm_class_port, "<port>",
2476 scm_class_class, scm_class_top, SCM_EOL);
2477 make_stdcls (&scm_class_input_port, "<input-port>",
2478 scm_class_class, scm_class_port, SCM_EOL);
2479 make_stdcls (&scm_class_output_port, "<output-port>",
2480 scm_class_class, scm_class_port, SCM_EOL);
2481 make_stdcls (&scm_class_input_output_port, "<input-output-port>",
2482 scm_class_class,
2483 scm_list_2 (scm_class_input_port, scm_class_output_port),
2484 SCM_EOL);
2485}
2486
2487/**********************************************************************
2488 *
2489 * Smob classes
2490 *
2491 **********************************************************************/
2492
2493static SCM
2494make_class_from_template (char const *template, char const *type_name, SCM supers, int applicablep)
2495{
2496 SCM class, name;
2497 if (type_name)
2498 {
2499 char buffer[100];
2500 sprintf (buffer, template, type_name);
2501 name = scm_from_locale_symbol (buffer);
2502 }
2503 else
2504 name = SCM_GOOPS_UNBOUND;
2505
2506 class = scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
2507 name, supers, SCM_EOL);
2508
2509 /* Only define name if doesn't already exist. */
2510 if (!SCM_GOOPS_UNBOUNDP (name)
2511 && scm_is_false (scm_module_variable (scm_module_goops, name)))
2512 DEFVAR (name, class);
2513 return class;
2514}
2515
2516static SCM
2517make_class_from_symbol (SCM type_name_sym, SCM supers, int applicablep)
2518{
2519 SCM class, name;
2520 if (type_name_sym != SCM_BOOL_F)
2521 {
2522 name = scm_string_append (scm_list_3 (scm_from_locale_string ("<"),
2523 scm_symbol_to_string (type_name_sym),
2524 scm_from_locale_string (">")));
2525 name = scm_string_to_symbol (name);
2526 }
2527 else
2528 name = SCM_GOOPS_UNBOUND;
2529
2530 class = scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
2531 name, supers, SCM_EOL);
2532
2533 /* Only define name if doesn't already exist. */
2534 if (!SCM_GOOPS_UNBOUNDP (name)
2535 && scm_is_false (scm_module_variable (scm_module_goops, name)))
2536 DEFVAR (name, class);
2537 return class;
2538}
2539
2540SCM
2541scm_make_extended_class (char const *type_name, int applicablep)
2542{
2543 return make_class_from_template ("<%s>",
2544 type_name,
2545 scm_list_1 (applicablep
2546 ? scm_class_applicable
2547 : scm_class_top),
2548 applicablep);
2549}
2550
2551static SCM
2552scm_make_extended_class_from_symbol (SCM type_name_sym, int applicablep)
2553{
2554 return make_class_from_symbol (type_name_sym,
2555 scm_list_1 (applicablep
2556 ? scm_class_applicable
2557 : scm_class_top),
2558 applicablep);
2559}
2560
2561void
2562scm_i_inherit_applicable (SCM c)
2563{
2564 if (!SCM_SUBCLASSP (c, scm_class_applicable))
2565 {
2566 SCM dsupers = SCM_SLOT (c, scm_si_direct_supers);
2567 SCM cpl = SCM_SLOT (c, scm_si_cpl);
2568 /* patch scm_class_applicable into direct-supers */
2569 SCM top = scm_c_memq (scm_class_top, dsupers);
2570 if (scm_is_false (top))
2571 dsupers = scm_append (scm_list_2 (dsupers,
2572 scm_list_1 (scm_class_applicable)));
2573 else
2574 {
2575 SCM_SETCAR (top, scm_class_applicable);
2576 SCM_SETCDR (top, scm_cons (scm_class_top, SCM_CDR (top)));
2577 }
2578 SCM_SET_SLOT (c, scm_si_direct_supers, dsupers);
2579 /* patch scm_class_applicable into cpl */
2580 top = scm_c_memq (scm_class_top, cpl);
2581 if (scm_is_false (top))
2582 abort ();
2583 else
2584 {
2585 SCM_SETCAR (top, scm_class_applicable);
2586 SCM_SETCDR (top, scm_cons (scm_class_top, SCM_CDR (top)));
2587 }
2588 /* add class to direct-subclasses of scm_class_applicable */
2589 SCM_SET_SLOT (scm_class_applicable,
2590 scm_si_direct_subclasses,
2591 scm_cons (c, SCM_SLOT (scm_class_applicable,
2592 scm_si_direct_subclasses)));
2593 }
2594}
2595
2596static void
2597create_smob_classes (void)
2598{
2599 long i;
2600
2601 for (i = 0; i < SCM_I_MAX_SMOB_TYPE_COUNT; ++i)
2602 scm_smob_class[i] = 0;
2603
2604 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_keyword)] = scm_class_keyword;
2605
2606 for (i = 0; i < scm_numsmob; ++i)
2607 if (!scm_smob_class[i])
2608 scm_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i),
2609 scm_smobs[i].apply != 0);
2610}
2611
2612void
2613scm_make_port_classes (long ptobnum, char *type_name)
2614{
2615 SCM c, class = make_class_from_template ("<%s-port>",
2616 type_name,
2617 scm_list_1 (scm_class_port),
2618 0);
2619 scm_port_class[SCM_IN_PCLASS_INDEX + ptobnum]
2620 = make_class_from_template ("<%s-input-port>",
2621 type_name,
2622 scm_list_2 (class, scm_class_input_port),
2623 0);
2624 scm_port_class[SCM_OUT_PCLASS_INDEX + ptobnum]
2625 = make_class_from_template ("<%s-output-port>",
2626 type_name,
2627 scm_list_2 (class, scm_class_output_port),
2628 0);
2629 scm_port_class[SCM_INOUT_PCLASS_INDEX + ptobnum]
2630 = c
2631 = make_class_from_template ("<%s-input-output-port>",
2632 type_name,
2633 scm_list_2 (class, scm_class_input_output_port),
2634 0);
2635 /* Patch cpl (since this tree is too complex for the C level compute-cpl) */
2636 SCM_SET_SLOT (c, scm_si_cpl,
2637 scm_cons2 (c, class, SCM_SLOT (scm_class_input_output_port, scm_si_cpl)));
2638}
2639
2640static void
2641create_port_classes (void)
2642{
2643 long i;
2644
2645 for (i = 0; i < scm_numptob; ++i)
2646 scm_make_port_classes (i, SCM_PTOBNAME (i));
2647}
2648
2649static SCM
2650make_struct_class (void *closure SCM_UNUSED,
2651 SCM vtable, SCM data, SCM prev SCM_UNUSED)
2652{
2653 if (scm_is_false (data))
2654 scm_i_define_class_for_vtable (vtable);
2655 return SCM_UNSPECIFIED;
2656}
2657
2658static void
2659create_struct_classes (void)
2660{
2661 scm_internal_hash_fold (make_struct_class, 0, SCM_BOOL_F,
2662 vtable_class_map);
2663}
2664
2665/**********************************************************************
2666 *
2667 * C interface
2668 *
2669 **********************************************************************/
2670
2671void
2672scm_load_goops ()
2673{
2674 if (!goops_loaded_p)
2675 scm_c_resolve_module ("oop goops");
2676}
2677
2678
2679SCM_SYMBOL (sym_o, "o");
2680SCM_SYMBOL (sym_x, "x");
2681
2682SCM_KEYWORD (k_accessor, "accessor");
2683SCM_KEYWORD (k_getter, "getter");
2684
2685SCM
2686scm_ensure_accessor (SCM name)
2687{
2688 SCM gf = scm_call_2 (SCM_TOP_LEVEL_LOOKUP_CLOSURE, name, SCM_BOOL_F);
2689 if (!SCM_IS_A_P (gf, scm_class_accessor))
2690 {
2691 gf = scm_make (scm_list_3 (scm_class_generic, k_name, name));
2692 gf = scm_make (scm_list_5 (scm_class_accessor,
2693 k_name, name, k_setter, gf));
2694 }
2695 return gf;
2696}
2697
2698#ifdef GUILE_DEBUG
2699/*
2700 * Debugging utilities
2701 */
2702
2703SCM_DEFINE (scm_pure_generic_p, "pure-generic?", 1, 0, 0,
2704 (SCM obj),
2705 "Return @code{#t} if @var{obj} is a pure generic.")
2706#define FUNC_NAME s_scm_pure_generic_p
2707{
2708 return scm_from_bool (SCM_PUREGENERICP (obj));
2709}
2710#undef FUNC_NAME
2711
2712#endif /* GUILE_DEBUG */
2713
2714/*
2715 * Initialization
2716 */
2717
2718SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
2719 (),
2720 "Announce that GOOPS is loaded and perform initialization\n"
2721 "on the C level which depends on the loaded GOOPS modules.")
2722#define FUNC_NAME s_scm_sys_goops_loaded
2723{
2724 goops_loaded_p = 1;
2725 var_compute_applicable_methods =
2726 scm_module_variable (scm_module_goops, sym_compute_applicable_methods);
2727 var_slot_unbound =
2728 scm_module_variable (scm_module_goops, sym_slot_unbound);
2729 var_slot_missing =
2730 scm_module_variable (scm_module_goops, sym_slot_missing);
2731 var_compute_cpl =
2732 scm_module_variable (scm_module_goops, sym_compute_cpl);
2733 var_no_applicable_method =
2734 scm_module_variable (scm_module_goops, sym_no_applicable_method);
2735 var_change_class =
2736 scm_module_variable (scm_module_goops, sym_change_class);
2737 setup_extended_primitive_generics ();
2738 return SCM_UNSPECIFIED;
2739}
2740#undef FUNC_NAME
2741
2742SCM scm_module_goops;
2743
2744SCM
2745scm_init_goops_builtins (void)
2746{
2747 scm_module_goops = scm_current_module ();
2748
2749 goops_rstate = scm_c_make_rstate ("GOOPS", 5);
2750
2751#include "libguile/goops.x"
2752
2753 hell = scm_calloc (hell_size * sizeof (*hell));
2754 hell_mutex = scm_make_mutex ();
2755
2756 create_basic_classes ();
2757 create_standard_classes ();
2758 create_smob_classes ();
2759 create_struct_classes ();
2760 create_port_classes ();
2761
2762 {
2763 SCM name = scm_from_latin1_symbol ("no-applicable-method");
2764 scm_no_applicable_method =
2765 scm_make (scm_list_3 (scm_class_generic, k_name, name));
2766 DEFVAR (name, scm_no_applicable_method);
2767 }
2768
2769 return SCM_UNSPECIFIED;
2770}
2771
2772void
2773scm_init_goops ()
2774{
2775 scm_c_define_gsubr ("%init-goops-builtins", 0, 0, 0,
2776 scm_init_goops_builtins);
2777}
2778
2779/*
2780 Local Variables:
2781 c-file-style: "gnu"
2782 End:
2783*/