* Grammar fix.
[bpt/guile.git] / libguile / goops.c
CommitLineData
726d810a 1/* Copyright (C) 1998,1999,2000,2001 Free Software Foundation, Inc.
80662eda
MD
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"
fdc28395 66#include "libguile/root.h"
80662eda
MD
67#include "libguile/smob.h"
68#include "libguile/strings.h"
69#include "libguile/strports.h"
70#include "libguile/vectors.h"
71#include "libguile/weaks.h"
72
ca83b028 73#include "libguile/validate.h"
80662eda
MD
74#include "libguile/goops.h"
75
80662eda
MD
76#define SPEC_OF(x) SCM_SLOT (x, scm_si_specializers)
77
80662eda 78#define DEFVAR(v,val) \
0ba8a0a5
MV
79{ scm_eval (SCM_LIST3 (scm_sym_define_public, (v), (val)), \
80 scm_module_goops); }
80662eda
MD
81/* Temporary hack until we get the new module system */
82/*fixme* Should optimize by keeping track of the variable object itself */
fdc28395
KN
83#define GETVAR(v) (SCM_VARIABLE_REF (scm_call_2 (scm_goops_lookup_closure, \
84 (v), SCM_BOOL_F)))
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
80662eda
MD
114/* The following definition is located in libguile/objects.h:
115#define SCM_OBJ_CLASS_REDEF(x) (SCM_STRUCT_VTABLE_DATA(x)[scm_si_redefined])
116*/
117
118#define TEST_CHANGE_CLASS(obj, class) \
119 { \
120 class = SCM_CLASS_OF (obj); \
121 if (SCM_OBJ_CLASS_REDEF (obj) != SCM_BOOL_F) \
122 CALL_GF3 ("change-object-class", \
123 obj, class, SCM_OBJ_CLASS_REDEF (obj)); \
124 }
125
126#define NXT_MTHD_METHODS(m) (SCM_VELTS (m)[1])
127#define NXT_MTHD_ARGS(m) (SCM_VELTS (m)[2])
128
129#define SCM_GOOPS_UNBOUND SCM_UNBOUND
130#define SCM_GOOPS_UNBOUNDP(x) ((x) == SCM_GOOPS_UNBOUND)
131
132static int goops_loaded_p = 0;
92c2555f 133static scm_t_rstate *goops_rstate;
80662eda
MD
134
135static SCM scm_goops_lookup_closure;
136
137/* Some classes are defined in libguile/objects.c. */
138SCM scm_class_top, scm_class_object, scm_class_class;
139SCM scm_class_entity, scm_class_entity_with_setter;
140SCM scm_class_generic, scm_class_generic_with_setter, scm_class_method;
141SCM scm_class_simple_method, scm_class_accessor;
142SCM scm_class_procedure_class;
143SCM scm_class_operator_class, scm_class_operator_with_setter_class;
144SCM scm_class_entity_class;
145SCM scm_class_number, scm_class_list;
146SCM scm_class_keyword;
147SCM scm_class_port, scm_class_input_output_port;
148SCM scm_class_input_port, scm_class_output_port;
149SCM scm_class_foreign_class, scm_class_foreign_object;
150SCM scm_class_foreign_slot;
151SCM scm_class_self, scm_class_protected;
152SCM scm_class_opaque, scm_class_read_only;
153SCM scm_class_protected_opaque, scm_class_protected_read_only;
154SCM scm_class_scm;
155SCM scm_class_int, scm_class_float, scm_class_double;
156
157SCM_SYMBOL (scm_sym_define_public, "define-public");
158
159static SCM scm_make_unbound (void);
160static SCM scm_unbound_p (SCM obj);
398d8ee1
KN
161static SCM scm_assert_bound (SCM value, SCM obj);
162static SCM scm_at_assert_bound_ref (SCM obj, SCM index);
163static SCM scm_sys_goops_loaded (void);
80662eda
MD
164
165/******************************************************************************
166 *
167 * Compute-cpl
168 *
169 * This version doesn't handle multiple-inheritance. It serves only for
dcb410ec 170 * booting classes and will be overloaded in Scheme
80662eda
MD
171 *
172 ******************************************************************************/
173
174#if 0
175static SCM
176compute_cpl (SCM supers, SCM res)
177{
178 return (SCM_NULLP (supers)
179 ? scm_reverse (res)
180 : compute_cpl (SCM_SLOT (SCM_CAR (supers), scm_si_direct_supers),
181 scm_cons (SCM_CAR (supers), res)));
182}
183#endif
184
185static SCM
186map (SCM (*proc) (SCM), SCM ls)
187{
188 if (SCM_IMP (ls))
189 return ls;
190 {
191 SCM res = scm_cons (proc (SCM_CAR (ls)), SCM_EOL);
192 SCM h = res;
193 ls = SCM_CDR (ls);
194 while (SCM_NIMP (ls))
195 {
196 SCM_SETCDR (h, scm_cons (proc (SCM_CAR (ls)), SCM_EOL));
197 h = SCM_CDR (h);
198 ls = SCM_CDR (ls);
199 }
200 return res;
201 }
202}
203
204static SCM
205filter_cpl (SCM ls)
206{
207 SCM res = SCM_EOL;
208 while (SCM_NIMP (ls))
209 {
210 SCM el = SCM_CAR (ls);
79a3dafe 211 if (SCM_FALSEP (scm_c_memq (el, res)))
80662eda
MD
212 res = scm_cons (el, res);
213 ls = SCM_CDR (ls);
214 }
215 return res;
216}
217
218static SCM
219compute_cpl (SCM class)
220{
221 if (goops_loaded_p)
222 return CALL_GF1 ("compute-cpl", class);
223 else
224 {
225 SCM supers = SCM_SLOT (class, scm_si_direct_supers);
226 SCM ls = scm_append (scm_acons (class, supers,
227 map (compute_cpl, supers)));
228 return scm_reverse_x (filter_cpl (ls), SCM_EOL);
229 }
230}
231
232/******************************************************************************
233 *
234 * compute-slots
235 *
236 ******************************************************************************/
237
238static SCM
239remove_duplicate_slots (SCM l, SCM res, SCM slots_already_seen)
240{
241 SCM tmp;
242
243 if (SCM_NULLP (l))
244 return res;
245
246 tmp = SCM_CAAR (l);
c312aca7
DH
247 if (!SCM_SYMBOLP (tmp))
248 scm_misc_error ("%compute-slots", "bad slot name ~S", SCM_LIST1 (tmp));
80662eda 249
79a3dafe 250 if (SCM_FALSEP (scm_c_memq (tmp, slots_already_seen))) {
80662eda
MD
251 res = scm_cons (SCM_CAR (l), res);
252 slots_already_seen = scm_cons (tmp, slots_already_seen);
253 }
254
255 return remove_duplicate_slots (SCM_CDR (l), res, slots_already_seen);
256}
257
258static SCM
259build_slots_list (SCM dslots, SCM cpl)
260{
261 register SCM res = dslots;
262
263 for (cpl = SCM_CDR(cpl); SCM_NNULLP(cpl); cpl = SCM_CDR(cpl))
264 res = scm_append (SCM_LIST2 (SCM_SLOT (SCM_CAR (cpl), scm_si_direct_slots),
265 res));
266
267 /* res contains a list of slots. Remove slots which appears more than once */
268 return remove_duplicate_slots (scm_reverse (res), SCM_EOL, SCM_EOL);
269}
270
271static SCM
272maplist (SCM ls)
273{
274 SCM orig = ls;
275 while (SCM_NIMP (ls))
276 {
c312aca7 277 if (!SCM_CONSP (SCM_CAR (ls)))
80662eda
MD
278 SCM_SETCAR (ls, scm_cons (SCM_CAR (ls), SCM_EOL));
279 ls = SCM_CDR (ls);
280 }
281 return orig;
282}
283
80662eda 284
23437298
DH
285SCM_DEFINE (scm_sys_compute_slots, "%compute-slots", 1, 0, 0,
286 (SCM class),
5352393c
MG
287 "Return a list consisting of the names of all slots belonging to\n"
288 "class @var{class}, i. e. the slots of @var{class} and of all of\n"
289 "its superclasses.")
23437298 290#define FUNC_NAME s_scm_sys_compute_slots
80662eda 291{
398d8ee1 292 SCM_VALIDATE_CLASS (1, class);
80662eda
MD
293 return build_slots_list (SCM_SLOT (class, scm_si_direct_slots),
294 SCM_SLOT (class, scm_si_cpl));
295}
23437298
DH
296#undef FUNC_NAME
297
80662eda
MD
298
299/******************************************************************************
300 *
301 * compute-getters-n-setters
302 *
303 * This version doesn't handle slot options. It serves only for booting
dcb410ec 304 * classes and will be overloaded in Scheme.
80662eda
MD
305 *
306 ******************************************************************************/
307
308SCM_KEYWORD (k_init_value, "init-value");
309SCM_KEYWORD (k_init_thunk, "init-thunk");
310
311static SCM
312compute_getters_n_setters (SCM slots)
313{
314 SCM res = SCM_EOL;
315 SCM *cdrloc = &res;
c014a02e 316 long i = 0;
80662eda
MD
317
318 for ( ; SCM_NNULLP(slots); slots = SCM_CDR(slots))
319 {
320 SCM init = SCM_BOOL_F;
321 SCM options = SCM_CDAR (slots);
322 if (SCM_NNULLP (options))
323 {
324 init = scm_get_keyword (k_init_value, options, 0);
325 if (init)
326 init = scm_closure (SCM_LIST2 (SCM_EOL, init), SCM_EOL);
327 else
328 init = scm_get_keyword (k_init_thunk, options, SCM_BOOL_F);
329 }
330 *cdrloc = scm_cons (scm_cons (SCM_CAAR (slots),
331 scm_cons (init,
332 SCM_MAKINUM (i++))),
333 SCM_EOL);
334 cdrloc = SCM_CDRLOC (*cdrloc);
335 }
336 return res;
337}
338
339/******************************************************************************
340 *
341 * initialize-object
342 *
343 ******************************************************************************/
344
345/*fixme* Manufacture keywords in advance */
346SCM
c014a02e 347scm_i_get_keyword (SCM key, SCM l, long len, SCM default_value, const char *subr)
80662eda 348{
c014a02e 349 long i;
23437298
DH
350
351 for (i = 0; i != len; i += 2)
80662eda 352 {
23437298
DH
353 SCM obj = SCM_CAR (l);
354
355 if (!SCM_KEYWORDP (obj))
356 scm_misc_error (subr, "bad keyword: ~S", SCM_LIST1 (obj));
357 else if (SCM_EQ_P (obj, key))
80662eda 358 return SCM_CADR (l);
23437298
DH
359 else
360 l = SCM_CDDR (l);
80662eda 361 }
23437298 362
80662eda
MD
363 return default_value;
364}
365
80662eda 366
23437298
DH
367SCM_DEFINE (scm_get_keyword, "get-keyword", 3, 0, 0,
368 (SCM key, SCM l, SCM default_value),
5352393c
MG
369 "Determine an associated value for the keyword @var{key} from\n"
370 "the list @var{l}. The list @var{l} has to consist of an even\n"
371 "number of elements, where, starting with the first, every\n"
372 "second element is a keyword, followed by its associated value.\n"
373 "If @var{l} does not hold a value for @var{key}, the value\n"
374 "@var{default_value} is returned.")
23437298 375#define FUNC_NAME s_scm_get_keyword
80662eda 376{
c014a02e 377 long 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);
c014a02e 402 long n_initargs;
80662eda 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 */
c014a02e 422 long n = scm_ilength (SCM_CDR (slot_name));
80662eda 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 480{
c014a02e 481 long i, n, len;
80662eda
MD
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 }
dcb410ec 530 SCM_SET_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;
c014a02e 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 {
c014a02e 562 long n = SCM_INUM (SCM_SLOT (class, scm_si_nfields));
80662eda
MD
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{
dcb410ec 591 unsigned int i;
80662eda
MD
592
593 for (i = 0; i < 7; ++i)
dcb410ec 594 SCM_SET_HASHSET (class, i, scm_c_uniform32 (goops_rstate));
80662eda
MD
595}
596
597/******************************************************************************/
598
599SCM
600scm_basic_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots)
601{
602 SCM z, cpl, slots, nfields, g_n_s;
603
604 /* Allocate one instance */
605 z = scm_make_struct (class, SCM_INUM0, SCM_EOL);
606
607 /* Initialize its slots */
608#if 0
609 cpl = compute_cpl (dsupers, SCM_LIST1(z));
610#endif
dcb410ec 611 SCM_SET_SLOT (z, scm_si_direct_supers, dsupers);
80662eda
MD
612 cpl = compute_cpl (z);
613 slots = build_slots_list (maplist (dslots), cpl);
614 nfields = SCM_MAKINUM (scm_ilength (slots));
615 g_n_s = compute_getters_n_setters (slots);
616
dcb410ec
DH
617 SCM_SET_SLOT (z, scm_si_name, name);
618 SCM_SET_SLOT (z, scm_si_direct_slots, dslots);
619 SCM_SET_SLOT (z, scm_si_direct_subclasses, SCM_EOL);
620 SCM_SET_SLOT (z, scm_si_direct_methods, SCM_EOL);
621 SCM_SET_SLOT (z, scm_si_cpl, cpl);
622 SCM_SET_SLOT (z, scm_si_slots, slots);
623 SCM_SET_SLOT (z, scm_si_nfields, nfields);
624 SCM_SET_SLOT (z, scm_si_getters_n_setters, g_n_s);
625 SCM_SET_SLOT (z, scm_si_redefined, SCM_BOOL_F);
626 SCM_SET_SLOT (z, scm_si_environment,
627 scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE));
80662eda
MD
628
629 /* Add this class in the direct-subclasses slot of dsupers */
630 {
631 SCM tmp;
dcb410ec
DH
632 for (tmp = dsupers; !SCM_NULLP (tmp); tmp = SCM_CDR (tmp))
633 SCM_SET_SLOT (SCM_CAR (tmp), scm_si_direct_subclasses,
634 scm_cons (z, SCM_SLOT (SCM_CAR (tmp),
635 scm_si_direct_subclasses)));
80662eda
MD
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
dcb410ec
DH
735 SCM_SET_SLOT (scm_class_class, scm_si_name, name);
736 SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, SCM_EOL); /* will be changed */
737 /* SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots_of_class); */
738 SCM_SET_SLOT (scm_class_class, scm_si_direct_subclasses, SCM_EOL);
739 SCM_SET_SLOT (scm_class_class, scm_si_direct_methods, SCM_EOL);
740 SCM_SET_SLOT (scm_class_class, scm_si_cpl, SCM_EOL); /* will be changed */
741 /* SCM_SET_SLOT (scm_class_class, scm_si_slots, slots_of_class); */
742 SCM_SET_SLOT (scm_class_class, scm_si_nfields, SCM_MAKINUM (SCM_N_CLASS_SLOTS));
743 /* SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters,
744 compute_getters_n_setters (slots_of_class)); */
745 SCM_SET_SLOT (scm_class_class, scm_si_redefined, SCM_BOOL_F);
746 SCM_SET_SLOT (scm_class_class, scm_si_environment,
747 scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE));
80662eda
MD
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 */
dcb410ec 772 SCM_SET_SLOT (scm_class_object, scm_si_direct_subclasses, SCM_LIST1 (scm_class_class));
80662eda 773
dcb410ec
DH
774 SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, SCM_LIST1 (scm_class_object));
775 SCM_SET_SLOT (scm_class_class, scm_si_cpl, SCM_LIST3 (scm_class_class, scm_class_object, scm_class_top));
80662eda
MD
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 936
5e03762c
MD
937SCM_DEFINE (scm_sys_tag_body, "%tag-body", 1, 0, 0,
938 (SCM body),
87e7741d 939 "Internal GOOPS magic---don't use this function!")
5e03762c
MD
940#define FUNC_NAME s_scm_sys_tag_body
941{
942 return scm_cons (SCM_IM_LAMBDA, body);
87e7741d
MD
943}
944#undef FUNC_NAME
80662eda
MD
945
946/******************************************************************************
947 *
948 * S l o t a c c e s s
949 *
950 ******************************************************************************/
951
398d8ee1
KN
952SCM_DEFINE (scm_make_unbound, "make-unbound", 0, 0, 0,
953 (),
6bcefd15 954 "Return the unbound value.")
398d8ee1 955#define FUNC_NAME s_scm_make_unbound
80662eda
MD
956{
957 return SCM_GOOPS_UNBOUND;
958}
398d8ee1 959#undef FUNC_NAME
80662eda 960
398d8ee1
KN
961SCM_DEFINE (scm_unbound_p, "unbound?", 1, 0, 0,
962 (SCM obj),
6bcefd15 963 "Return @code{#t} if @var{obj} is unbound.")
398d8ee1 964#define FUNC_NAME s_scm_unbound_p
80662eda
MD
965{
966 return SCM_GOOPS_UNBOUNDP (obj) ? SCM_BOOL_T : SCM_BOOL_F;
967}
398d8ee1 968#undef FUNC_NAME
80662eda 969
398d8ee1
KN
970SCM_DEFINE (scm_assert_bound, "assert-bound", 2, 0, 0,
971 (SCM value, SCM obj),
6bcefd15
MG
972 "Return @var{value} if it is bound, and invoke the\n"
973 "@var{slot-unbound} method of @var{obj} if it is not.")
398d8ee1 974#define FUNC_NAME s_scm_assert_bound
80662eda
MD
975{
976 if (SCM_GOOPS_UNBOUNDP (value))
977 return CALL_GF1 ("slot-unbound", obj);
978 return value;
979}
398d8ee1 980#undef FUNC_NAME
80662eda 981
398d8ee1
KN
982SCM_DEFINE (scm_at_assert_bound_ref, "@assert-bound-ref", 2, 0, 0,
983 (SCM obj, SCM index),
6bcefd15
MG
984 "Like @code{assert-bound}, but use @var{index} for accessing\n"
985 "the value from @var{obj}.")
398d8ee1 986#define FUNC_NAME s_scm_at_assert_bound_ref
80662eda
MD
987{
988 SCM value = SCM_SLOT (obj, SCM_INUM (index));
989 if (SCM_GOOPS_UNBOUNDP (value))
990 return CALL_GF1 ("slot-unbound", obj);
991 return value;
992}
398d8ee1 993#undef FUNC_NAME
80662eda 994
398d8ee1
KN
995SCM_DEFINE (scm_sys_fast_slot_ref, "%fast-slot-ref", 2, 0, 0,
996 (SCM obj, SCM index),
6bcefd15 997 "Return the slot value with index @var{index} from @var{obj}.")
398d8ee1 998#define FUNC_NAME s_scm_sys_fast_slot_ref
80662eda 999{
c014a02e 1000 register long i;
80662eda 1001
398d8ee1
KN
1002 SCM_VALIDATE_INSTANCE (1, obj);
1003 SCM_VALIDATE_INUM (2, index);
80662eda 1004 i = SCM_INUM (index);
ca83b028
DH
1005
1006 SCM_ASSERT_RANGE (2, index, i >= 0 && i < SCM_NUMBER_OF_SLOTS (obj));
80662eda
MD
1007 return scm_at_assert_bound_ref (obj, index);
1008}
ca83b028
DH
1009#undef FUNC_NAME
1010
398d8ee1
KN
1011SCM_DEFINE (scm_sys_fast_slot_set_x, "%fast-slot-set!", 3, 0, 0,
1012 (SCM obj, SCM index, SCM value),
6bcefd15
MG
1013 "Set the slot with index @var{index} in @var{obj} to\n"
1014 "@var{value}.")
398d8ee1 1015#define FUNC_NAME s_scm_sys_fast_slot_set_x
80662eda 1016{
c014a02e 1017 register long i;
80662eda 1018
398d8ee1
KN
1019 SCM_VALIDATE_INSTANCE (1, obj);
1020 SCM_VALIDATE_INUM (2, index);
80662eda 1021 i = SCM_INUM (index);
ca83b028 1022 SCM_ASSERT_RANGE (2, index, i >= 0 && i < SCM_NUMBER_OF_SLOTS (obj));
dcb410ec 1023 SCM_SET_SLOT (obj, i, value);
ca83b028 1024
80662eda
MD
1025 return SCM_UNSPECIFIED;
1026}
ca83b028
DH
1027#undef FUNC_NAME
1028
80662eda
MD
1029
1030/** Utilities **/
1031
1032/* In the future, this function will return the effective slot
1033 * definition associated with SLOT_NAME. Now it just returns some of
1034 * the information which will be stored in the effective slot
1035 * definition.
1036 */
1037
1038static SCM
1039slot_definition_using_name (SCM class, SCM slot_name)
1040{
1041 register SCM slots = SCM_SLOT (class, scm_si_getters_n_setters);
1042 for (; SCM_NIMP (slots); slots = SCM_CDR (slots))
1043 if (SCM_CAAR (slots) == slot_name)
1044 return SCM_CAR (slots);
1045 return SCM_BOOL_F;
1046}
1047
1048static SCM
e81d98ec 1049get_slot_value (SCM class SCM_UNUSED, SCM obj, SCM slotdef)
80662eda
MD
1050{
1051 SCM access = SCM_CDDR (slotdef);
1052 /* Two cases here:
1053 * - access is an integer (the offset of this slot in the slots vector)
1054 * - otherwise (car access) is the getter function to apply
23437298 1055 */
80662eda
MD
1056 if (SCM_INUMP (access))
1057 return SCM_SLOT (obj, SCM_INUM (access));
1058 else
1059 {
1060 /* We must evaluate (apply (car access) (list obj))
1061 * where (car access) is known to be a closure of arity 1 */
1062 register SCM code, env;
1063
1064 code = SCM_CAR (access);
1065 if (!SCM_CLOSUREP (code))
1066 return SCM_SUBRF (code) (obj);
726d810a 1067 env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (code),
80662eda
MD
1068 SCM_LIST1 (obj),
1069 SCM_ENV (code));
1070 /* Evaluate the closure body */
1071 return scm_eval_body (SCM_CDR (SCM_CODE (code)), env);
1072 }
1073}
1074
1075static SCM
1076get_slot_value_using_name (SCM class, SCM obj, SCM slot_name)
1077{
1078 SCM slotdef = slot_definition_using_name (class, slot_name);
1079 if (SCM_NFALSEP (slotdef))
1080 return get_slot_value (class, obj, slotdef);
1081 else
1082 return CALL_GF3 ("slot-missing", class, obj, slot_name);
1083}
1084
1085static SCM
e81d98ec 1086set_slot_value (SCM class SCM_UNUSED, SCM obj, SCM slotdef, SCM value)
80662eda
MD
1087{
1088 SCM access = SCM_CDDR (slotdef);
1089 /* Two cases here:
1090 * - access is an integer (the offset of this slot in the slots vector)
1091 * - otherwise (cadr access) is the setter function to apply
1092 */
1093 if (SCM_INUMP (access))
dcb410ec 1094 SCM_SET_SLOT (obj, SCM_INUM (access), value);
80662eda
MD
1095 else
1096 {
1097 /* We must evaluate (apply (cadr l) (list obj value))
1098 * where (cadr l) is known to be a closure of arity 2 */
1099 register SCM code, env;
1100
1101 code = SCM_CADR (access);
1102 if (!SCM_CLOSUREP (code))
1103 SCM_SUBRF (code) (obj, value);
1104 else
1105 {
726d810a 1106 env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (code),
80662eda
MD
1107 SCM_LIST2 (obj, value),
1108 SCM_ENV (code));
1109 /* Evaluate the closure body */
1110 scm_eval_body (SCM_CDR (SCM_CODE (code)), env);
1111 }
1112 }
1113 return SCM_UNSPECIFIED;
1114}
1115
1116static SCM
1117set_slot_value_using_name (SCM class, SCM obj, SCM slot_name, SCM value)
1118{
1119 SCM slotdef = slot_definition_using_name (class, slot_name);
1120 if (SCM_NFALSEP (slotdef))
1121 return set_slot_value (class, obj, slotdef, value);
1122 else
1123 return CALL_GF4 ("slot-missing", class, obj, slot_name, value);
1124}
1125
1126static SCM
e81d98ec 1127test_slot_existence (SCM class SCM_UNUSED, SCM obj, SCM slot_name)
80662eda
MD
1128{
1129 register SCM l;
1130
729dbac3
DH
1131 for (l = SCM_ACCESSORS_OF (obj); !SCM_NULLP (l); l = SCM_CDR (l))
1132 if (SCM_EQ_P (SCM_CAAR (l), slot_name))
80662eda
MD
1133 return SCM_BOOL_T;
1134
1135 return SCM_BOOL_F;
1136}
1137
80662eda
MD
1138 /* ======================================== */
1139
23437298
DH
1140SCM_DEFINE (scm_slot_ref_using_class, "slot-ref-using-class", 3, 0, 0,
1141 (SCM class, SCM obj, SCM slot_name),
1142 "")
1143#define FUNC_NAME s_scm_slot_ref_using_class
80662eda
MD
1144{
1145 SCM res;
1146
398d8ee1
KN
1147 SCM_VALIDATE_CLASS (1, class);
1148 SCM_VALIDATE_INSTANCE (2, obj);
1149 SCM_VALIDATE_SYMBOL (3, slot_name);
80662eda
MD
1150
1151 res = get_slot_value_using_name (class, obj, slot_name);
1152 if (SCM_GOOPS_UNBOUNDP (res))
1153 return CALL_GF3 ("slot-unbound", class, obj, slot_name);
1154 return res;
1155}
23437298 1156#undef FUNC_NAME
80662eda 1157
23437298
DH
1158
1159SCM_DEFINE (scm_slot_set_using_class_x, "slot-set-using-class!", 4, 0, 0,
1160 (SCM class, SCM obj, SCM slot_name, SCM value),
1161 "")
1162#define FUNC_NAME s_scm_slot_set_using_class_x
80662eda 1163{
398d8ee1
KN
1164 SCM_VALIDATE_CLASS (1, class);
1165 SCM_VALIDATE_INSTANCE (2, obj);
1166 SCM_VALIDATE_SYMBOL (3, slot_name);
23437298 1167
80662eda
MD
1168 return set_slot_value_using_name (class, obj, slot_name, value);
1169}
23437298
DH
1170#undef FUNC_NAME
1171
80662eda 1172
398d8ee1
KN
1173SCM_DEFINE (scm_slot_bound_using_class_p, "slot-bound-using-class?", 3, 0, 0,
1174 (SCM class, SCM obj, SCM slot_name),
1175 "")
1176#define FUNC_NAME s_scm_slot_bound_using_class_p
80662eda 1177{
398d8ee1
KN
1178 SCM_VALIDATE_CLASS (1, class);
1179 SCM_VALIDATE_INSTANCE (2, obj);
1180 SCM_VALIDATE_SYMBOL (3, slot_name);
80662eda
MD
1181
1182 return (SCM_GOOPS_UNBOUNDP (get_slot_value_using_name (class, obj, slot_name))
1183 ? SCM_BOOL_F
1184 : SCM_BOOL_T);
1185}
398d8ee1 1186#undef FUNC_NAME
80662eda 1187
398d8ee1
KN
1188SCM_DEFINE (scm_slot_exists_using_class_p, "slot-exists-using-class?", 3, 0, 0,
1189 (SCM class, SCM obj, SCM slot_name),
1190 "")
1191#define FUNC_NAME s_scm_slot_exists_using_class_p
1192{
1193 SCM_VALIDATE_CLASS (1, class);
1194 SCM_VALIDATE_INSTANCE (2, obj);
1195 SCM_VALIDATE_SYMBOL (3, slot_name);
80662eda
MD
1196 return test_slot_existence (class, obj, slot_name);
1197}
398d8ee1 1198#undef FUNC_NAME
80662eda
MD
1199
1200
1201 /* ======================================== */
1202
398d8ee1
KN
1203SCM_DEFINE (scm_slot_ref, "slot-ref", 2, 0, 0,
1204 (SCM obj, SCM slot_name),
6bcefd15
MG
1205 "Return the value from @var{obj}'s slot with the name\n"
1206 "@var{slot_name}.")
398d8ee1 1207#define FUNC_NAME s_scm_slot_ref
80662eda
MD
1208{
1209 SCM res, class;
1210
398d8ee1 1211 SCM_VALIDATE_INSTANCE (1, obj);
80662eda
MD
1212 TEST_CHANGE_CLASS (obj, class);
1213
1214 res = get_slot_value_using_name (class, obj, slot_name);
1215 if (SCM_GOOPS_UNBOUNDP (res))
1216 return CALL_GF3 ("slot-unbound", class, obj, slot_name);
1217 return res;
1218}
398d8ee1 1219#undef FUNC_NAME
80662eda 1220
398d8ee1
KN
1221SCM_DEFINE (scm_slot_set_x, "slot-set!", 3, 0, 0,
1222 (SCM obj, SCM slot_name, SCM value),
6bcefd15 1223 "Set the slot named @var{slot_name} of @var{obj} to @var{value}.")
398d8ee1 1224#define FUNC_NAME s_scm_slot_set_x
80662eda
MD
1225{
1226 SCM class;
1227
398d8ee1 1228 SCM_VALIDATE_INSTANCE (1, obj);
80662eda
MD
1229 TEST_CHANGE_CLASS(obj, class);
1230
1231 return set_slot_value_using_name (class, obj, slot_name, value);
1232}
398d8ee1 1233#undef FUNC_NAME
80662eda 1234
398d8ee1 1235const char *scm_s_slot_set_x = s_scm_slot_set_x;
80662eda 1236
398d8ee1
KN
1237SCM_DEFINE (scm_slot_bound_p, "slot-bound?", 2, 0, 0,
1238 (SCM obj, SCM slot_name),
6bcefd15
MG
1239 "Return @code{#t} if the slot named @var{slot_name} of @var{obj}\n"
1240 "is bound.")
398d8ee1 1241#define FUNC_NAME s_scm_slot_bound_p
80662eda
MD
1242{
1243 SCM class;
1244
398d8ee1 1245 SCM_VALIDATE_INSTANCE (1, obj);
80662eda
MD
1246 TEST_CHANGE_CLASS(obj, class);
1247
1248 return (SCM_GOOPS_UNBOUNDP (get_slot_value_using_name (class,
1249 obj,
1250 slot_name))
1251 ? SCM_BOOL_F
1252 : SCM_BOOL_T);
1253}
398d8ee1 1254#undef FUNC_NAME
80662eda 1255
398d8ee1
KN
1256SCM_DEFINE (scm_slots_exists_p, "slot-exists?", 2, 0, 0,
1257 (SCM obj, SCM slot_name),
6bcefd15 1258 "Return @code{#t} if @var{obj} has a slot named @var{slot_name}.")
398d8ee1 1259#define FUNC_NAME s_scm_slots_exists_p
80662eda
MD
1260{
1261 SCM class;
1262
398d8ee1
KN
1263 SCM_VALIDATE_INSTANCE (1, obj);
1264 SCM_VALIDATE_SYMBOL (2, slot_name);
80662eda
MD
1265 TEST_CHANGE_CLASS (obj, class);
1266
1267 return test_slot_existence (class, obj, slot_name);
1268}
398d8ee1 1269#undef FUNC_NAME
80662eda
MD
1270
1271
1272/******************************************************************************
1273 *
1274 * %allocate-instance (the low level instance allocation primitive)
1275 *
1276 ******************************************************************************/
1277
1278static void clear_method_cache (SCM);
1279
1280static SCM
c014a02e 1281wrap_init (SCM class, SCM *m, long n)
80662eda
MD
1282{
1283 SCM z;
c014a02e 1284 long i;
80662eda
MD
1285
1286 /* Set all slots to unbound */
1287 for (i = 0; i < n; i++)
1288 m[i] = SCM_GOOPS_UNBOUND;
1289
1290 SCM_NEWCELL2 (z);
80662eda 1291 SCM_SET_STRUCT_GC_CHAIN (z, 0);
729dbac3 1292 SCM_SET_CELL_WORD_1 (z, m);
92c2555f 1293 SCM_SET_CELL_WORD_0 (z, (scm_t_bits) SCM_STRUCT_DATA (class)
729dbac3 1294 | scm_tc3_cons_gloc);
80662eda
MD
1295
1296 return z;
1297}
1298
398d8ee1
KN
1299SCM_DEFINE (scm_sys_allocate_instance, "%allocate-instance", 2, 0, 0,
1300 (SCM class, SCM initargs),
6bcefd15
MG
1301 "Create a new instance of class @var{class} and initialize it\n"
1302 "from the arguments @var{initargs}.")
398d8ee1 1303#define FUNC_NAME s_scm_sys_allocate_instance
80662eda
MD
1304{
1305 SCM *m;
c014a02e 1306 long n;
80662eda 1307
398d8ee1 1308 SCM_VALIDATE_CLASS (1, class);
80662eda
MD
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 {
c014a02e 1346 long i;
80662eda
MD
1347
1348 /* allocate class object */
1349 SCM z = scm_make_struct (class, SCM_INUM0, SCM_EOL);
1350
dcb410ec 1351 SCM_SET_SLOT (z, scm_si_print, SCM_GOOPS_UNBOUND);
80662eda 1352 for (i = scm_si_goops_fields; i < n; i++)
dcb410ec 1353 SCM_SET_SLOT (z, i, SCM_GOOPS_UNBOUND);
80662eda
MD
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}
398d8ee1 1371#undef FUNC_NAME
80662eda 1372
398d8ee1
KN
1373SCM_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
80662eda 1377{
c312aca7 1378 SCM_ASSERT (SCM_STRUCTP (obj)
80662eda
MD
1379 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
1380 || SCM_I_ENTITYP (obj)),
1381 obj,
1382 SCM_ARG1,
398d8ee1 1383 FUNC_NAME);
80662eda 1384 if (SCM_I_ENTITYP (obj))
322ec19d 1385 SCM_SET_ENTITY_SETTER (obj, setter);
80662eda
MD
1386 else
1387 SCM_OPERATOR_CLASS (obj)->setter = setter;
1388 return SCM_UNSPECIFIED;
1389}
398d8ee1 1390#undef FUNC_NAME
80662eda
MD
1391
1392/******************************************************************************
1393 *
1394 * %modify-instance (used by change-class to modify in place)
1395 *
1396 ******************************************************************************/
1397
398d8ee1
KN
1398SCM_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
80662eda 1402{
398d8ee1
KN
1403 SCM_VALIDATE_INSTANCE (1, old);
1404 SCM_VALIDATE_INSTANCE (2, new);
80662eda
MD
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}
398d8ee1 1422#undef FUNC_NAME
80662eda 1423
398d8ee1
KN
1424SCM_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
80662eda 1428{
398d8ee1
KN
1429 SCM_VALIDATE_CLASS (1, old);
1430 SCM_VALIDATE_CLASS (2, new);
80662eda
MD
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));
729dbac3 1438 SCM_STRUCT_DATA (old)[scm_vtable_index_vtable] = SCM_UNPACK (old);
80662eda
MD
1439 SCM_SETCAR (new, car);
1440 SCM_SETCDR (new, cdr);
729dbac3 1441 SCM_STRUCT_DATA (new)[scm_vtable_index_vtable] = SCM_UNPACK (new);
80662eda
MD
1442 }
1443 SCM_REALLOW_INTS;
1444 return SCM_UNSPECIFIED;
1445}
398d8ee1 1446#undef FUNC_NAME
80662eda 1447
398d8ee1
KN
1448SCM_DEFINE (scm_sys_invalidate_class, "%invalidate-class", 1, 0, 0,
1449 (SCM class),
1450 "")
1451#define FUNC_NAME s_scm_sys_invalidate_class
80662eda 1452{
398d8ee1 1453 SCM_VALIDATE_CLASS (1, class);
80662eda
MD
1454 SCM_CLEAR_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_VALID);
1455 return SCM_UNSPECIFIED;
1456}
398d8ee1 1457#undef FUNC_NAME
80662eda
MD
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
92c2555f 1465static scm_t_bits **hell;
c014a02e
ML
1466static long n_hell = 1; /* one place for the evil one himself */
1467static long hell_size = 4;
80662eda 1468#ifdef USE_THREADS
92c2555f 1469static scm_t_mutex hell_mutex;
80662eda
MD
1470#endif
1471
c014a02e 1472static long
80662eda
MD
1473burnin (SCM o)
1474{
c014a02e 1475 long i;
80662eda
MD
1476 for (i = 1; i < n_hell; ++i)
1477 if (SCM_INST (o) == hell[i])
1478 return i;
1479 return 0;
1480}
1481
1482static void
1483go_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 {
c014a02e 1491 long new_size = 2 * hell_size;
80662eda
MD
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
1501static void
1502go_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
1513static SCM
1514purgatory (void *args)
1515{
fdc28395 1516 return scm_apply_0 (GETVAR (scm_str2symbol ("change-class")), (SCM) args);
80662eda
MD
1517}
1518
1519void
e81d98ec 1520scm_change_object_class (SCM obj, SCM old_class SCM_UNUSED, SCM new_class)
80662eda
MD
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
1544SCM_KEYWORD (k_name, "name");
1545
1546SCM_SYMBOL (sym_no_method, "no-method");
1547
1548static SCM list_of_no_method;
1549
1550SCM_SYMBOL (scm_sym_args, "args");
1551
1552SCM
1553scm_make_method_cache (SCM gf)
1554{
1555 return SCM_LIST5 (SCM_IM_DISPATCH, scm_sym_args, SCM_MAKINUM (1),
00ffa0e7
KN
1556 scm_c_make_vector (SCM_INITIAL_MCACHE_SIZE,
1557 list_of_no_method),
80662eda
MD
1558 gf);
1559}
1560
1561static void
1562clear_method_cache (SCM gf)
1563{
322ec19d
ML
1564 SCM cache = scm_make_method_cache (gf);
1565 SCM_SET_ENTITY_PROCEDURE (gf, cache);
dcb410ec 1566 SCM_SET_SLOT (gf, scm_si_used_by, SCM_BOOL_F);
80662eda
MD
1567}
1568
398d8ee1
KN
1569SCM_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
80662eda
MD
1573{
1574 SCM used_by;
25ba37df 1575 SCM_ASSERT (SCM_PUREGENERICP (gf), gf, SCM_ARG1, FUNC_NAME);
80662eda
MD
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);
c312aca7 1580 for (; SCM_CONSP (used_by); used_by = SCM_CDR (used_by))
80662eda
MD
1581 scm_sys_invalidate_method_cache_x (SCM_CAR (used_by));
1582 clear_method_cache (gf);
c312aca7 1583 for (; SCM_CONSP (methods); methods = SCM_CDR (methods))
dcb410ec 1584 SCM_SET_SLOT (SCM_CAR (methods), scm_si_code_table, SCM_EOL);
80662eda
MD
1585 }
1586 {
55c4a132 1587 SCM n = SCM_SLOT (gf, scm_si_n_specialized);
80662eda 1588 /* The sign of n is a flag indicating rest args. */
55c4a132 1589 SCM_SET_MCACHE_N_SPECIALIZED (SCM_ENTITY_PROCEDURE (gf), n);
80662eda
MD
1590 }
1591 return SCM_UNSPECIFIED;
1592}
398d8ee1 1593#undef FUNC_NAME
80662eda 1594
398d8ee1
KN
1595SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
1596 (SCM proc),
1597 "")
1598#define FUNC_NAME s_scm_generic_capability_p
80662eda
MD
1599{
1600 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (proc)),
398d8ee1 1601 proc, SCM_ARG1, FUNC_NAME);
80662eda
MD
1602 return (scm_subr_p (proc) && SCM_SUBR_GENERIC (proc)
1603 ? SCM_BOOL_T
1604 : SCM_BOOL_F);
1605}
398d8ee1 1606#undef FUNC_NAME
80662eda 1607
398d8ee1
KN
1608SCM_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
80662eda
MD
1612{
1613 while (SCM_NIMP (subrs))
1614 {
1615 SCM subr = SCM_CAR (subrs);
1616 SCM_ASSERT (scm_subr_p (subr) && SCM_SUBR_GENERIC (subr),
398d8ee1 1617 subr, SCM_ARGn, FUNC_NAME);
80662eda
MD
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}
398d8ee1 1626#undef FUNC_NAME
80662eda 1627
398d8ee1
KN
1628SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
1629 (SCM subr),
1630 "")
1631#define FUNC_NAME s_scm_primitive_generic_generic
80662eda
MD
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 }
db4b4ca6 1639 SCM_WRONG_TYPE_ARG (SCM_ARG1, subr);
80662eda 1640}
398d8ee1 1641#undef FUNC_NAME
80662eda
MD
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
1660static int
1661applicablep (SCM actual, SCM formal)
1662{
79a3dafe
DH
1663 /* We already know that the cpl is well formed. */
1664 return !SCM_FALSEP (scm_c_memq (formal, SCM_SLOT (actual, scm_si_cpl)));
80662eda
MD
1665}
1666
1667static int
1668more_specificp (SCM m1, SCM m2, SCM *targs)
1669{
1670 register SCM s1, s2;
c014a02e 1671 register long i;
80662eda
MD
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
dcb410ec 1694 for (l = SCM_SLOT (targs[i], scm_si_cpl); ; l = SCM_CDR(l)) {
80662eda
MD
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
1708static SCM
c014a02e 1709scm_i_vector2list (SCM l, long len)
80662eda 1710{
c014a02e 1711 long j;
00ffa0e7 1712 SCM z = scm_c_make_vector (len, SCM_UNDEFINED);
80662eda
MD
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
1720static SCM
c014a02e 1721sort_applicable_methods (SCM method_list, long size, SCM *targs)
80662eda 1722{
c014a02e 1723 long i, j, incr;
80662eda
MD
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
1784SCM
c014a02e 1785scm_compute_applicable_methods (SCM gf, SCM args, long len, int find_method_p)
80662eda 1786{
c014a02e
ML
1787 register long i;
1788 long count = 0;
80662eda
MD
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) {
00ffa0e7 1796 tmp = scm_c_make_vector (len, SCM_UNDEFINED);
80662eda
MD
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 {
c312aca7 1818 if (SCM_INSTANCEP (fl)
80662eda
MD
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
1847SCM_PROC (s_sys_compute_applicable_methods, "%compute-applicable-methods", 2, 0, 0, scm_sys_compute_applicable_methods);
1848#endif
1849
1850static const char s_sys_compute_applicable_methods[] = "%compute-applicable-methods";
1851
1852SCM
1853scm_sys_compute_applicable_methods (SCM gf, SCM args)
398d8ee1 1854#define FUNC_NAME s_sys_compute_applicable_methods
80662eda 1855{
c014a02e 1856 long n;
398d8ee1 1857 SCM_VALIDATE_GENERIC (1, gf);
80662eda 1858 n = scm_ilength (args);
398d8ee1 1859 SCM_ASSERT (n >= 0, args, SCM_ARG2, FUNC_NAME);
80662eda
MD
1860 return scm_compute_applicable_methods (gf, args, n, 1);
1861}
398d8ee1 1862#undef FUNC_NAME
80662eda 1863
86d31dfe 1864SCM_SYMBOL (sym_compute_applicable_methods, "compute-applicable-methods");
9a441ddb 1865SCM_VARIABLE_INIT (var_compute_applicable_methods, "compute-applicable-methods", scm_c_define_gsubr (s_sys_compute_applicable_methods, 2, 0, 0, scm_sys_compute_applicable_methods));
80662eda
MD
1866
1867SCM_SYNTAX (s_atslot_ref, "@slot-ref", scm_makmmacro, scm_m_atslot_ref);
1868
1869SCM
e81d98ec 1870scm_m_atslot_ref (SCM xorig, SCM env SCM_UNUSED)
e11208ca 1871#define FUNC_NAME s_atslot_ref
80662eda
MD
1872{
1873 SCM x = SCM_CDR (xorig);
160bb34a 1874 SCM_ASSYNT (scm_ilength (x) == 2, scm_s_expression, FUNC_NAME);
e11208ca 1875 SCM_VALIDATE_INUM (SCM_ARG2, SCM_CADR (x));
80662eda
MD
1876 return scm_cons (SCM_IM_SLOT_REF, x);
1877}
e11208ca
DH
1878#undef FUNC_NAME
1879
80662eda
MD
1880
1881SCM_SYNTAX (s_atslot_set_x, "@slot-set!", scm_makmmacro, scm_m_atslot_set_x);
1882
1883SCM
e81d98ec 1884scm_m_atslot_set_x (SCM xorig, SCM env SCM_UNUSED)
e11208ca 1885#define FUNC_NAME s_atslot_set_x
80662eda
MD
1886{
1887 SCM x = SCM_CDR (xorig);
160bb34a 1888 SCM_ASSYNT (scm_ilength (x) == 3, scm_s_expression, FUNC_NAME);
e11208ca 1889 SCM_VALIDATE_INUM (SCM_ARG2, SCM_CADR (x));
80662eda
MD
1890 return scm_cons (SCM_IM_SLOT_SET_X, x);
1891}
e11208ca
DH
1892#undef FUNC_NAME
1893
80662eda
MD
1894
1895SCM_SYNTAX (s_atdispatch, "@dispatch", scm_makmmacro, scm_m_atdispatch);
1896
1897SCM_SYMBOL (sym_atdispatch, s_atdispatch);
1898
1899SCM
1900scm_m_atdispatch (SCM xorig, SCM env)
ca83b028 1901#define FUNC_NAME s_atdispatch
80662eda
MD
1902{
1903 SCM args, n, v, gf, x = SCM_CDR (xorig);
160bb34a 1904 SCM_ASSYNT (scm_ilength (x) == 4, scm_s_expression, FUNC_NAME);
80662eda 1905 args = SCM_CAR (x);
e11208ca
DH
1906 if (!SCM_CONSP (args) && !SCM_SYMBOLP (args))
1907 SCM_WRONG_TYPE_ARG (SCM_ARG1, args);
80662eda
MD
1908 x = SCM_CDR (x);
1909 n = SCM_XEVALCAR (x, env);
e11208ca 1910 SCM_VALIDATE_INUM (SCM_ARG2, n);
ca83b028 1911 SCM_ASSERT_RANGE (0, n, SCM_INUM (n) >= 1);
80662eda
MD
1912 x = SCM_CDR (x);
1913 v = SCM_XEVALCAR (x, env);
e11208ca 1914 SCM_VALIDATE_VECTOR (SCM_ARG3, v);
80662eda
MD
1915 x = SCM_CDR (x);
1916 gf = SCM_XEVALCAR (x, env);
e11208ca 1917 SCM_VALIDATE_PUREGENERIC (SCM_ARG4, gf);
80662eda
MD
1918 return SCM_LIST5 (SCM_IM_DISPATCH, args, n, v, gf);
1919}
ca83b028
DH
1920#undef FUNC_NAME
1921
80662eda
MD
1922
1923#ifdef USE_THREADS
1924static void
1925lock_cache_mutex (void *m)
1926{
1927 SCM mutex = (SCM) m;
1928 scm_lock_mutex (mutex);
1929}
1930
1931static void
1932unlock_cache_mutex (void *m)
1933{
1934 SCM mutex = (SCM) m;
1935 scm_unlock_mutex (mutex);
1936}
1937#endif
1938
1939static SCM
1940call_memoize_method (void *a)
1941{
1942 SCM args = (SCM) a;
1943 SCM gf = SCM_CAR (args);
1944 SCM x = SCM_CADR (args);
1945 /* First check if another thread has inserted a method between
1946 * the cache miss and locking the mutex.
1947 */
1948 SCM cmethod = scm_mcache_lookup_cmethod (x, SCM_CDDR (args));
1949 if (SCM_NIMP (cmethod))
1950 return cmethod;
1951 /*fixme* Use scm_apply */
1952 return CALL_GF3 ("memoize-method!", gf, SCM_CDDR (args), x);
1953}
1954
1955SCM
1956scm_memoize_method (SCM x, SCM args)
1957{
1958 SCM gf = SCM_CAR (scm_last_pair (x));
1959#ifdef USE_THREADS
1960 return scm_internal_dynamic_wind (lock_cache_mutex,
1961 call_memoize_method,
1962 unlock_cache_mutex,
1963 (void *) scm_cons2 (gf, x, args),
1964 (void *) SCM_SLOT (gf, scm_si_cache_mutex));
1965#else
1966 return call_memoize_method ((void *) scm_cons2 (gf, x, args));
1967#endif
1968}
1969
1970/******************************************************************************
1971 *
1972 * A simple make (which will be redefined later in Scheme)
1973 * This version handles only creation of gf, methods and classes (no instances)
1974 *
1975 * Since this code will disappear when Goops will be fully booted,
1976 * no precaution is taken to be efficient.
1977 *
1978 ******************************************************************************/
1979
1980SCM_KEYWORD (k_setter, "setter");
1981SCM_KEYWORD (k_specializers, "specializers");
1982SCM_KEYWORD (k_procedure, "procedure");
1983SCM_KEYWORD (k_dsupers, "dsupers");
1984SCM_KEYWORD (k_slots, "slots");
1985SCM_KEYWORD (k_gf, "generic-function");
1986
398d8ee1
KN
1987SCM_DEFINE (scm_make, "make", 0, 0, 1,
1988 (SCM args),
27c37006 1989 "Make a new object. @var{args} must contain the class and\n"
6bcefd15 1990 "all necessary initialization information.")
398d8ee1 1991#define FUNC_NAME s_scm_make
80662eda
MD
1992{
1993 SCM class, z;
c014a02e 1994 long len = scm_ilength (args);
80662eda
MD
1995
1996 if (len <= 0 || (len & 1) == 0)
398d8ee1 1997 SCM_WRONG_NUM_ARGS ();
80662eda
MD
1998
1999 class = SCM_CAR(args);
2000 args = SCM_CDR(args);
2001
2002 if (class == scm_class_generic || class == scm_class_generic_with_setter)
2003 {
2004#ifdef USE_THREADS
2005 z = scm_make_struct (class, SCM_INUM0,
2006 SCM_LIST4 (SCM_EOL,
2007 SCM_INUM0,
2008 SCM_BOOL_F,
2009 scm_make_mutex ()));
2010#else
2011 z = scm_make_struct (class, SCM_INUM0,
2012 SCM_LIST3 (SCM_EOL, SCM_INUM0, SCM_BOOL_F));
2013#endif
2014 scm_set_procedure_property_x (z, scm_sym_name,
2015 scm_get_keyword (k_name,
2016 args,
2017 SCM_BOOL_F));
2018 clear_method_cache (z);
2019 if (class == scm_class_generic_with_setter)
2020 {
2021 SCM setter = scm_get_keyword (k_setter, args, SCM_BOOL_F);
2022 if (SCM_NIMP (setter))
2023 scm_sys_set_object_setter_x (z, setter);
2024 }
2025 }
2026 else
2027 {
2028 z = scm_sys_allocate_instance (class, args);
2029
2030 if (class == scm_class_method
2031 || class == scm_class_simple_method
2032 || class == scm_class_accessor)
2033 {
dcb410ec 2034 SCM_SET_SLOT (z, scm_si_generic_function,
80662eda
MD
2035 scm_i_get_keyword (k_gf,
2036 args,
2037 len - 1,
2038 SCM_BOOL_F,
dcb410ec
DH
2039 FUNC_NAME));
2040 SCM_SET_SLOT (z, scm_si_specializers,
80662eda
MD
2041 scm_i_get_keyword (k_specializers,
2042 args,
2043 len - 1,
2044 SCM_EOL,
dcb410ec
DH
2045 FUNC_NAME));
2046 SCM_SET_SLOT (z, scm_si_procedure,
80662eda
MD
2047 scm_i_get_keyword (k_procedure,
2048 args,
2049 len - 1,
2050 SCM_EOL,
dcb410ec
DH
2051 FUNC_NAME));
2052 SCM_SET_SLOT (z, scm_si_code_table, SCM_EOL);
80662eda
MD
2053 }
2054 else
2055 {
2056 /* In all the others case, make a new class .... No instance here */
dcb410ec 2057 SCM_SET_SLOT (z, scm_si_name,
80662eda
MD
2058 scm_i_get_keyword (k_name,
2059 args,
2060 len - 1,
38ae064c 2061 scm_str2symbol ("???"),
dcb410ec
DH
2062 FUNC_NAME));
2063 SCM_SET_SLOT (z, scm_si_direct_supers,
80662eda
MD
2064 scm_i_get_keyword (k_dsupers,
2065 args,
2066 len - 1,
2067 SCM_EOL,
dcb410ec
DH
2068 FUNC_NAME));
2069 SCM_SET_SLOT (z, scm_si_direct_slots,
80662eda
MD
2070 scm_i_get_keyword (k_slots,
2071 args,
2072 len - 1,
2073 SCM_EOL,
dcb410ec 2074 FUNC_NAME));
80662eda
MD
2075 }
2076 }
2077 return z;
2078}
398d8ee1 2079#undef FUNC_NAME
80662eda 2080
398d8ee1
KN
2081SCM_DEFINE (scm_find_method, "find-method", 0, 0, 1,
2082 (SCM l),
2083 "")
2084#define FUNC_NAME s_scm_find_method
80662eda
MD
2085{
2086 SCM gf;
c014a02e 2087 long len = scm_ilength (l);
80662eda
MD
2088
2089 if (len == 0)
398d8ee1 2090 SCM_WRONG_NUM_ARGS ();
80662eda
MD
2091
2092 gf = SCM_CAR(l); l = SCM_CDR(l);
398d8ee1 2093 SCM_VALIDATE_GENERIC (1, gf);
80662eda 2094 if (SCM_NULLP (SCM_SLOT (gf, scm_si_methods)))
398d8ee1 2095 SCM_MISC_ERROR ("no methods for generic ~S", SCM_LIST1 (gf));
80662eda
MD
2096
2097 return scm_compute_applicable_methods (gf, l, len - 1, 1);
2098}
398d8ee1 2099#undef FUNC_NAME
80662eda 2100
398d8ee1
KN
2101SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0,
2102 (SCM m1, SCM m2, SCM targs),
2103 "")
2104#define FUNC_NAME s_scm_sys_method_more_specific_p
80662eda
MD
2105{
2106 SCM l, v;
c014a02e 2107 long i, len;
80662eda 2108
398d8ee1
KN
2109 SCM_VALIDATE_METHOD (1, m1);
2110 SCM_VALIDATE_METHOD (2, m2);
2111 SCM_ASSERT ((len = scm_ilength (targs)) != -1, targs, SCM_ARG3, FUNC_NAME);
80662eda
MD
2112
2113 /* Verify that all the arguments of targs are classes and place them in a vector*/
00ffa0e7 2114 v = scm_c_make_vector (len, SCM_EOL);
80662eda
MD
2115
2116 for (i=0, l=targs; SCM_NNULLP(l); i++, l=SCM_CDR(l)) {
398d8ee1 2117 SCM_ASSERT (SCM_CLASSP (SCM_CAR (l)), targs, SCM_ARG3, FUNC_NAME);
80662eda
MD
2118 SCM_VELTS(v)[i] = SCM_CAR(l);
2119 }
2120 return more_specificp (m1, m2, SCM_VELTS(v)) ? SCM_BOOL_T: SCM_BOOL_F;
2121}
398d8ee1 2122#undef FUNC_NAME
80662eda
MD
2123
2124
2125
2126/******************************************************************************
2127 *
2128 * Initializations
2129 *
2130 ******************************************************************************/
2131
2132
2133static void
2134make_stdcls (SCM *var, char *name, SCM meta, SCM super, SCM slots)
2135{
38ae064c 2136 SCM tmp = scm_str2symbol (name);
80662eda
MD
2137
2138 *var = scm_permanent_object (scm_basic_make_class (meta,
2139 tmp,
2140 SCM_CONSP (super)
2141 ? super
2142 : SCM_LIST1 (super),
2143 slots));
2144 DEFVAR(tmp, *var);
2145}
2146
2147
2148SCM_KEYWORD (k_slot_definition, "slot-definition");
2149
2150static void
2151create_standard_classes (void)
2152{
2153 SCM slots;
38ae064c
DH
2154 SCM method_slots = SCM_LIST4 (scm_str2symbol ("generic-function"),
2155 scm_str2symbol ("specializers"),
2156 scm_str2symbol ("procedure"),
2157 scm_str2symbol ("code-table"));
2158 SCM amethod_slots = SCM_LIST1 (SCM_LIST3 (scm_str2symbol ("slot-definition"),
80662eda
MD
2159 k_init_keyword,
2160 k_slot_definition));
2161#ifdef USE_THREADS
38ae064c 2162 SCM mutex_slot = SCM_LIST1 (scm_str2symbol ("make-mutex"));
80662eda
MD
2163#else
2164 SCM mutex_slot = SCM_BOOL_F;
2165#endif
38ae064c
DH
2166 SCM gf_slots = SCM_LIST4 (scm_str2symbol ("methods"),
2167 SCM_LIST3 (scm_str2symbol ("n-specialized"),
80662eda
MD
2168 k_init_value,
2169 SCM_INUM0),
38ae064c 2170 SCM_LIST3 (scm_str2symbol ("used-by"),
80662eda
MD
2171 k_init_value,
2172 SCM_BOOL_F),
38ae064c 2173 SCM_LIST3 (scm_str2symbol ("cache-mutex"),
80662eda
MD
2174 k_init_thunk,
2175 scm_closure (SCM_LIST2 (SCM_EOL,
2176 mutex_slot),
2177 SCM_EOL)));
2178
2179 /* Foreign class slot classes */
2180 make_stdcls (&scm_class_foreign_slot, "<foreign-slot>",
2181 scm_class_class, scm_class_top, SCM_EOL);
2182 make_stdcls (&scm_class_protected, "<protected-slot>",
2183 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2184 make_stdcls (&scm_class_opaque, "<opaque-slot>",
2185 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2186 make_stdcls (&scm_class_read_only, "<read-only-slot>",
2187 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2188 make_stdcls (&scm_class_self, "<self-slot>",
2189 scm_class_class,
2190 SCM_LIST2 (scm_class_foreign_slot, scm_class_read_only),
2191 SCM_EOL);
2192 make_stdcls (&scm_class_protected_opaque, "<protected-opaque-slot>",
2193 scm_class_class,
2194 SCM_LIST2 (scm_class_protected, scm_class_opaque),
2195 SCM_EOL);
2196 make_stdcls (&scm_class_protected_read_only, "<protected-read-only-slot>",
2197 scm_class_class,
2198 SCM_LIST2 (scm_class_protected, scm_class_read_only),
2199 SCM_EOL);
2200 make_stdcls (&scm_class_scm, "<scm-slot>",
2201 scm_class_class, scm_class_protected, SCM_EOL);
2202 make_stdcls (&scm_class_int, "<int-slot>",
2203 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2204 make_stdcls (&scm_class_float, "<float-slot>",
2205 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2206 make_stdcls (&scm_class_double, "<double-slot>",
2207 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2208
2209 /* Continue initialization of class <class> */
2210
2211 slots = build_class_class_slots ();
dcb410ec
DH
2212 SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots);
2213 SCM_SET_SLOT (scm_class_class, scm_si_slots, slots);
2214 SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters,
2215 compute_getters_n_setters (slots));
80662eda
MD
2216
2217 make_stdcls (&scm_class_foreign_class, "<foreign-class>",
2218 scm_class_class, scm_class_class,
38ae064c 2219 SCM_LIST2 (SCM_LIST3 (scm_str2symbol ("constructor"),
80662eda
MD
2220 k_class,
2221 scm_class_opaque),
38ae064c 2222 SCM_LIST3 (scm_str2symbol ("destructor"),
80662eda
MD
2223 k_class,
2224 scm_class_opaque)));
2225 make_stdcls (&scm_class_foreign_object, "<foreign-object>",
2226 scm_class_foreign_class, scm_class_object, SCM_EOL);
2227 SCM_SET_CLASS_FLAGS (scm_class_foreign_object, SCM_CLASSF_FOREIGN);
2228
2229 /* scm_class_generic functions classes */
2230 make_stdcls (&scm_class_procedure_class, "<procedure-class>",
2231 scm_class_class, scm_class_class, SCM_EOL);
2232 make_stdcls (&scm_class_entity_class, "<entity-class>",
2233 scm_class_class, scm_class_procedure_class, SCM_EOL);
2234 make_stdcls (&scm_class_operator_class, "<operator-class>",
2235 scm_class_class, scm_class_procedure_class, SCM_EOL);
2236 make_stdcls (&scm_class_operator_with_setter_class,
2237 "<operator-with-setter-class>",
2238 scm_class_class, scm_class_operator_class, SCM_EOL);
2239 make_stdcls (&scm_class_method, "<method>",
2240 scm_class_class, scm_class_object, method_slots);
2241 make_stdcls (&scm_class_simple_method, "<simple-method>",
2242 scm_class_class, scm_class_method, SCM_EOL);
2243 SCM_SET_CLASS_FLAGS (scm_class_simple_method, SCM_CLASSF_SIMPLE_METHOD);
2244 make_stdcls (&scm_class_accessor, "<accessor-method>",
2245 scm_class_class, scm_class_simple_method, amethod_slots);
2246 SCM_SET_CLASS_FLAGS (scm_class_accessor, SCM_CLASSF_ACCESSOR_METHOD);
2247 make_stdcls (&scm_class_entity, "<entity>",
2248 scm_class_entity_class, scm_class_object, SCM_EOL);
2249 make_stdcls (&scm_class_entity_with_setter, "<entity-with-setter>",
2250 scm_class_entity_class, scm_class_entity, SCM_EOL);
2251 make_stdcls (&scm_class_generic, "<generic>",
2252 scm_class_entity_class, scm_class_entity, gf_slots);
2253 SCM_SET_CLASS_FLAGS (scm_class_generic, SCM_CLASSF_PURE_GENERIC);
2254 make_stdcls (&scm_class_generic_with_setter, "<generic-with-setter>",
2255 scm_class_entity_class,
2256 SCM_LIST2 (scm_class_generic, scm_class_entity_with_setter),
2257 SCM_EOL);
2258#if 0
2259 /* Patch cpl since compute_cpl doesn't support multiple inheritance. */
dcb410ec 2260 SCM_SET_SLOT (scm_class_generic_with_setter, scm_si_cpl,
80662eda
MD
2261 scm_append (SCM_LIST3 (SCM_LIST2 (scm_class_generic_with_setter,
2262 scm_class_generic),
2263 SCM_SLOT (scm_class_entity_with_setter,
2264 scm_si_cpl),
dcb410ec 2265 SCM_EOL)));
80662eda
MD
2266#endif
2267 SCM_SET_CLASS_FLAGS (scm_class_generic_with_setter, SCM_CLASSF_PURE_GENERIC);
2268
2269 /* Primitive types classes */
2270 make_stdcls (&scm_class_boolean, "<boolean>",
2271 scm_class_class, scm_class_top, SCM_EOL);
2272 make_stdcls (&scm_class_char, "<char>",
2273 scm_class_class, scm_class_top, SCM_EOL);
2274 make_stdcls (&scm_class_list, "<list>",
2275 scm_class_class, scm_class_top, SCM_EOL);
2276 make_stdcls (&scm_class_pair, "<pair>",
2277 scm_class_class, scm_class_list, SCM_EOL);
2278 make_stdcls (&scm_class_null, "<null>",
2279 scm_class_class, scm_class_list, SCM_EOL);
2280 make_stdcls (&scm_class_string, "<string>",
2281 scm_class_class, scm_class_top, SCM_EOL);
2282 make_stdcls (&scm_class_symbol, "<symbol>",
2283 scm_class_class, scm_class_top, SCM_EOL);
2284 make_stdcls (&scm_class_vector, "<vector>",
2285 scm_class_class, scm_class_top, SCM_EOL);
2286 make_stdcls (&scm_class_number, "<number>",
2287 scm_class_class, scm_class_top, SCM_EOL);
2288 make_stdcls (&scm_class_complex, "<complex>",
2289 scm_class_class, scm_class_number, SCM_EOL);
2290 make_stdcls (&scm_class_real, "<real>",
2291 scm_class_class, scm_class_complex, SCM_EOL);
2292 make_stdcls (&scm_class_integer, "<integer>",
2293 scm_class_class, scm_class_real, SCM_EOL);
2294 make_stdcls (&scm_class_keyword, "<keyword>",
2295 scm_class_class, scm_class_top, SCM_EOL);
2296 make_stdcls (&scm_class_unknown, "<unknown>",
2297 scm_class_class, scm_class_top, SCM_EOL);
2298 make_stdcls (&scm_class_procedure, "<procedure>",
2299 scm_class_procedure_class, scm_class_top, SCM_EOL);
2300 make_stdcls (&scm_class_procedure_with_setter, "<procedure-with-setter>",
2301 scm_class_procedure_class, scm_class_procedure, SCM_EOL);
2302 make_stdcls (&scm_class_primitive_generic, "<primitive-generic>",
2303 scm_class_procedure_class, scm_class_procedure, SCM_EOL);
2304 make_stdcls (&scm_class_port, "<port>",
2305 scm_class_class, scm_class_top, SCM_EOL);
2306 make_stdcls (&scm_class_input_port, "<input-port>",
2307 scm_class_class, scm_class_port, SCM_EOL);
2308 make_stdcls (&scm_class_output_port, "<output-port>",
2309 scm_class_class, scm_class_port, SCM_EOL);
2310 make_stdcls (&scm_class_input_output_port, "<input-output-port>",
2311 scm_class_class,
2312 SCM_LIST2 (scm_class_input_port, scm_class_output_port),
2313 SCM_EOL);
2314}
2315
2316/**********************************************************************
2317 *
2318 * Smob classes
2319 *
2320 **********************************************************************/
2321
2322static SCM
2323make_class_from_template (char *template, char *type_name, SCM supers)
2324{
2325 SCM class, name;
2326 if (type_name)
2327 {
2328 char buffer[100];
2329 sprintf (buffer, template, type_name);
38ae064c 2330 name = scm_str2symbol (buffer);
80662eda
MD
2331 }
2332 else
2333 name = SCM_GOOPS_UNBOUND;
2334
2335 class = scm_permanent_object (scm_basic_make_class (scm_class_class,
2336 name,
2337 supers,
2338 SCM_EOL));
2339
2340 /* Only define name if doesn't already exist. */
2341 if (!SCM_GOOPS_UNBOUNDP (name)
fdc28395 2342 && SCM_FALSEP (scm_call_2 (scm_goops_lookup_closure, name, SCM_BOOL_F)))
0ba8a0a5 2343 DEFVAR (name, class);
80662eda
MD
2344 return class;
2345}
2346
2347SCM
2348scm_make_extended_class (char *type_name)
2349{
2350 return make_class_from_template ("<%s>",
2351 type_name,
2352 SCM_LIST1 (scm_class_top));
2353}
2354
2355static void
2356create_smob_classes (void)
2357{
c014a02e 2358 long i;
80662eda
MD
2359
2360 scm_smob_class = (SCM *) malloc (255 * sizeof (SCM));
2361 for (i = 0; i < 255; ++i)
2362 scm_smob_class[i] = 0;
2363
2364 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_big)] = scm_class_integer;
2365 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_real)] = scm_class_real;
2366 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_complex)] = scm_class_complex;
2367 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_keyword)] = scm_class_keyword;
2368
2369 for (i = 0; i < scm_numsmob; ++i)
2370 if (!scm_smob_class[i])
2371 scm_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i));
2372}
2373
2374void
c014a02e 2375scm_make_port_classes (long ptobnum, char *type_name)
80662eda
MD
2376{
2377 SCM c, class = make_class_from_template ("<%s-port>",
2378 type_name,
2379 SCM_LIST1 (scm_class_port));
2380 scm_port_class[SCM_IN_PCLASS_INDEX + ptobnum]
2381 = make_class_from_template ("<%s-input-port>",
2382 type_name,
2383 SCM_LIST2 (class, scm_class_input_port));
2384 scm_port_class[SCM_OUT_PCLASS_INDEX + ptobnum]
2385 = make_class_from_template ("<%s-output-port>",
2386 type_name,
2387 SCM_LIST2 (class, scm_class_output_port));
2388 scm_port_class[SCM_INOUT_PCLASS_INDEX + ptobnum]
2389 = c
2390 = make_class_from_template ("<%s-input-output-port>",
2391 type_name,
2392 SCM_LIST2 (class,
2393 scm_class_input_output_port));
2394 /* Patch cpl (since this tree is too complex for the C level compute-cpl) */
dcb410ec
DH
2395 SCM_SET_SLOT (c, scm_si_cpl,
2396 scm_cons2 (c, class, SCM_SLOT (scm_class_input_output_port, scm_si_cpl)));
80662eda
MD
2397}
2398
2399static void
2400create_port_classes (void)
2401{
c014a02e 2402 long i;
80662eda
MD
2403
2404 scm_port_class = (SCM *) malloc (3 * 256 * sizeof (SCM));
2405 for (i = 0; i < 3 * 256; ++i)
2406 scm_port_class[i] = 0;
2407
2408 for (i = 0; i < scm_numptob; ++i)
2409 scm_make_port_classes (i, SCM_PTOBNAME (i));
2410}
2411
2412static SCM
e81d98ec
DH
2413make_struct_class (void *closure SCM_UNUSED, SCM key SCM_UNUSED,
2414 SCM data, SCM prev SCM_UNUSED)
80662eda
MD
2415{
2416 if (SCM_NFALSEP (SCM_STRUCT_TABLE_NAME (data)))
2417 SCM_SET_STRUCT_TABLE_CLASS (data,
2418 scm_make_extended_class
b24b5e13 2419 (SCM_SYMBOL_CHARS (SCM_STRUCT_TABLE_NAME (data))));
80662eda
MD
2420 return SCM_UNSPECIFIED;
2421}
2422
2423static void
2424create_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
2435void
2436scm_load_goops ()
2437{
2438 if (!goops_loaded_p)
abd28220 2439 scm_c_resolve_module ("oop goops");
80662eda
MD
2440}
2441
e11208ca 2442
80662eda
MD
2443SCM
2444scm_make_foreign_object (SCM class, SCM initargs)
e11208ca 2445#define FUNC_NAME s_scm_make
80662eda
MD
2446{
2447 void * (*constructor) (SCM)
2448 = (void * (*) (SCM)) SCM_SLOT (class, scm_si_constructor);
e11208ca
DH
2449 if (constructor == 0)
2450 SCM_MISC_ERROR ("Can't make instances of class ~S", SCM_LIST1 (class));
80662eda
MD
2451 return scm_wrap_object (class, constructor (initargs));
2452}
e11208ca
DH
2453#undef FUNC_NAME
2454
80662eda
MD
2455
2456static size_t
2457scm_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
2464SCM
2465scm_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;
38ae064c 2470 name = scm_str2symbol (s_name);
80662eda
MD
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 {
dcb410ec 2478 SCM_SET_SLOT (class, scm_si_destructor, (SCM) destructor);
80662eda
MD
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
dcb410ec
DH
2487 SCM_SET_SLOT (class, scm_si_layout, scm_str2symbol (""));
2488 SCM_SET_SLOT (class, scm_si_constructor, (SCM) constructor);
80662eda
MD
2489
2490 return class;
2491}
2492
2493SCM_SYMBOL (sym_o, "o");
2494SCM_SYMBOL (sym_x, "x");
2495
2496SCM_KEYWORD (k_accessor, "accessor");
2497SCM_KEYWORD (k_getter, "getter");
2498
2499static SCM
e81d98ec 2500default_setter (SCM obj SCM_UNUSED, SCM c SCM_UNUSED)
80662eda
MD
2501{
2502 scm_misc_error ("slot-set!", "read-only slot", SCM_EOL);
2503 return 0;
2504}
2505
2506void
2507scm_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 {
9a441ddb
MV
2513 SCM get = scm_c_make_subr ("goops:get", scm_tc7_subr_1, getter);
2514 SCM set = scm_c_make_subr ("goops:set", scm_tc7_subr_2,
2515 setter ? setter : default_setter);
80662eda
MD
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 {
38ae064c
DH
2523 SCM name = scm_str2symbol (slot_name);
2524 SCM aname = scm_str2symbol (accessor_name);
80662eda
MD
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
dcb410ec
DH
2544 SCM_SET_SLOT (class, scm_si_slots,
2545 scm_append_x (SCM_LIST2 (SCM_SLOT (class, scm_si_slots),
2546 SCM_LIST1 (slot))));
2547 SCM_SET_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))));
80662eda
MD
2550 }
2551 }
2552 {
c014a02e 2553 long n = SCM_INUM (SCM_SLOT (class, scm_si_nfields));
80662eda 2554
dcb410ec 2555 SCM_SET_SLOT (class, scm_si_nfields, SCM_MAKINUM (n + 1));
80662eda
MD
2556 }
2557}
2558
2559SCM
2560scm_wrap_object (SCM class, void *data)
2561{
2562 SCM z;
2563 SCM_NEWCELL2 (z);
2564 SCM_SETCDR (z, (SCM) data);
2565 SCM_SET_STRUCT_GC_CHAIN (z, 0);
2566 SCM_SETCAR (z, SCM_UNPACK (SCM_CDR (class)) | scm_tc3_cons_gloc);
2567 return z;
2568}
2569
2570SCM scm_components;
2571
2572SCM
2573scm_wrap_component (SCM class, SCM container, void *data)
2574{
2575 SCM obj = scm_wrap_object (class, data);
2576 SCM handle = scm_hash_fn_create_handle_x (scm_components,
2577 obj,
2578 SCM_BOOL_F,
2579 scm_struct_ihashq,
2580 scm_sloppy_assq,
2581 0);
2582 SCM_SETCDR (handle, container);
2583 return obj;
2584}
2585
2586SCM
2587scm_ensure_accessor (SCM name)
2588{
fdc28395 2589 SCM gf = scm_call_2 (SCM_TOP_LEVEL_LOOKUP_CLOSURE, name, SCM_BOOL_F);
80662eda
MD
2590 if (!SCM_IS_A_P (gf, scm_class_generic_with_setter))
2591 {
2592 gf = scm_make (SCM_LIST3 (scm_class_generic, k_name, name));
2593 gf = scm_make (SCM_LIST5 (scm_class_generic_with_setter,
2594 k_name, name,
2595 k_setter, gf));
2596 }
2597 return gf;
2598}
2599
2600SCM_SYMBOL (sym_internal_add_method_x, "internal-add-method!");
2601
2602void
2603scm_add_method (SCM gf, SCM m)
2604{
0ba8a0a5 2605 scm_eval (SCM_LIST3 (sym_internal_add_method_x, gf, m), scm_module_goops);
80662eda
MD
2606}
2607
2608#ifdef GUILE_DEBUG
2609/*
2610 * Debugging utilities
2611 */
2612
398d8ee1
KN
2613SCM_DEFINE (scm_pure_generic_p, "pure-generic?", 1, 0, 0,
2614 (SCM obj),
6bcefd15 2615 "Return @code{#t} if @var{obj} is a pure generic.")
398d8ee1 2616#define FUNC_NAME s_scm_pure_generic_p
80662eda 2617{
25ba37df 2618 return SCM_BOOL (SCM_PUREGENERICP (obj));
80662eda 2619}
398d8ee1 2620#undef FUNC_NAME
80662eda
MD
2621
2622#endif /* GUILE_DEBUG */
2623
2624/*
2625 * Initialization
2626 */
2627
398d8ee1
KN
2628SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
2629 (),
6bcefd15
MG
2630 "Announce that GOOPS is loaded and perform initialization\n"
2631 "on the C level which depends on the loaded GOOPS modules.")
398d8ee1 2632#define FUNC_NAME s_scm_sys_goops_loaded
80662eda
MD
2633{
2634 goops_loaded_p = 1;
86d31dfe
MV
2635 var_compute_applicable_methods =
2636 scm_sym2var (sym_compute_applicable_methods, scm_goops_lookup_closure,
2637 SCM_BOOL_F);
80662eda
MD
2638 return SCM_UNSPECIFIED;
2639}
398d8ee1 2640#undef FUNC_NAME
80662eda
MD
2641
2642SCM scm_module_goops;
2643
abd28220
MV
2644SCM
2645scm_init_goops_builtins (void)
80662eda 2646{
abd28220 2647 scm_module_goops = scm_current_module ();
80662eda
MD
2648 scm_goops_lookup_closure = scm_module_lookup_closure (scm_module_goops);
2649
0ba8a0a5
MV
2650 /* Not really necessary right now, but who knows...
2651 */
2652 scm_permanent_object (scm_module_goops);
2653 scm_permanent_object (scm_goops_lookup_closure);
2654
80662eda
MD
2655 scm_components = scm_permanent_object (scm_make_weak_key_hash_table
2656 (SCM_MAKINUM (37)));
2657
2658 goops_rstate = scm_c_make_rstate ("GOOPS", 5);
2659
8dc9439f 2660#ifndef SCM_MAGIC_SNARFER
80662eda 2661#include "libguile/goops.x"
8dc9439f 2662#endif
80662eda
MD
2663
2664 list_of_no_method = scm_permanent_object (SCM_LIST1 (sym_no_method));
2665
2666 hell = scm_must_malloc (hell_size, "hell");
2667#ifdef USE_THREADS
2668 scm_mutex_init (&hell_mutex);
2669#endif
2670
2671 create_basic_classes ();
2672 create_standard_classes ();
2673 create_smob_classes ();
2674 create_struct_classes ();
2675 create_port_classes ();
2676
2677 {
38ae064c 2678 SCM name = scm_str2symbol ("no-applicable-method");
80662eda
MD
2679 scm_no_applicable_method
2680 = scm_permanent_object (scm_make (SCM_LIST3 (scm_class_generic,
2681 k_name,
2682 name)));
2683 DEFVAR (name, scm_no_applicable_method);
2684 }
abd28220
MV
2685
2686 return SCM_UNSPECIFIED;
80662eda
MD
2687}
2688
2689void
abd28220 2690scm_init_goops ()
80662eda 2691{
9a441ddb
MV
2692 scm_c_define_gsubr ("%init-goops-builtins", 0, 0, 0,
2693 scm_init_goops_builtins);
80662eda 2694}
23437298
DH
2695
2696/*
2697 Local Variables:
2698 c-file-style: "gnu"
2699 End:
2700*/