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