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