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