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