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