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