* ports.c (scm_port_for_each): Make a snapshot of the port table
[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);
b6311c08 381 if (len < 0 || len % 2 == 1)
23437298
DH
382 scm_misc_error (FUNC_NAME, "Bad keyword-value list: ~S", SCM_LIST1 (l));
383
384 return scm_i_get_keyword (key, l, len, default_value, FUNC_NAME);
80662eda 385}
23437298
DH
386#undef FUNC_NAME
387
80662eda 388
80662eda
MD
389SCM_KEYWORD (k_init_keyword, "init-keyword");
390
391static SCM get_slot_value (SCM class, SCM obj, SCM slotdef);
392static SCM set_slot_value (SCM class, SCM obj, SCM slotdef, SCM value);
393
398d8ee1
KN
394SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0,
395 (SCM obj, SCM initargs),
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 1368 if (SCM_I_ENTITYP (obj))
322ec19d 1369 SCM_SET_ENTITY_SETTER (obj, setter);
80662eda
MD
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),
00ffa0e7
KN
1540 scm_c_make_vector (SCM_INITIAL_MCACHE_SIZE,
1541 list_of_no_method),
80662eda
MD
1542 gf);
1543}
1544
1545static void
1546clear_method_cache (SCM gf)
1547{
322ec19d
ML
1548 SCM cache = scm_make_method_cache (gf);
1549 SCM_SET_ENTITY_PROCEDURE (gf, cache);
80662eda
MD
1550 SCM_SLOT (gf, scm_si_used_by) = SCM_BOOL_F;
1551}
1552
398d8ee1
KN
1553SCM_DEFINE (scm_sys_invalidate_method_cache_x, "%invalidate-method-cache!", 1, 0, 0,
1554 (SCM gf),
1555 "")
1556#define FUNC_NAME s_scm_sys_invalidate_method_cache_x
80662eda
MD
1557{
1558 SCM used_by;
25ba37df 1559 SCM_ASSERT (SCM_PUREGENERICP (gf), gf, SCM_ARG1, FUNC_NAME);
80662eda
MD
1560 used_by = SCM_SLOT (gf, scm_si_used_by);
1561 if (SCM_NFALSEP (used_by))
1562 {
1563 SCM methods = SCM_SLOT (gf, scm_si_methods);
c312aca7 1564 for (; SCM_CONSP (used_by); used_by = SCM_CDR (used_by))
80662eda
MD
1565 scm_sys_invalidate_method_cache_x (SCM_CAR (used_by));
1566 clear_method_cache (gf);
c312aca7 1567 for (; SCM_CONSP (methods); methods = SCM_CDR (methods))
80662eda
MD
1568 SCM_SLOT (SCM_CAR (methods), scm_si_code_table) = SCM_EOL;
1569 }
1570 {
55c4a132 1571 SCM n = SCM_SLOT (gf, scm_si_n_specialized);
80662eda 1572 /* The sign of n is a flag indicating rest args. */
55c4a132 1573 SCM_SET_MCACHE_N_SPECIALIZED (SCM_ENTITY_PROCEDURE (gf), n);
80662eda
MD
1574 }
1575 return SCM_UNSPECIFIED;
1576}
398d8ee1 1577#undef FUNC_NAME
80662eda 1578
398d8ee1
KN
1579SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
1580 (SCM proc),
1581 "")
1582#define FUNC_NAME s_scm_generic_capability_p
80662eda
MD
1583{
1584 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (proc)),
398d8ee1 1585 proc, SCM_ARG1, FUNC_NAME);
80662eda
MD
1586 return (scm_subr_p (proc) && SCM_SUBR_GENERIC (proc)
1587 ? SCM_BOOL_T
1588 : SCM_BOOL_F);
1589}
398d8ee1 1590#undef FUNC_NAME
80662eda 1591
398d8ee1
KN
1592SCM_DEFINE (scm_enable_primitive_generic_x, "enable-primitive-generic!", 0, 0, 1,
1593 (SCM subrs),
1594 "")
1595#define FUNC_NAME s_scm_enable_primitive_generic_x
80662eda
MD
1596{
1597 while (SCM_NIMP (subrs))
1598 {
1599 SCM subr = SCM_CAR (subrs);
1600 SCM_ASSERT (scm_subr_p (subr) && SCM_SUBR_GENERIC (subr),
398d8ee1 1601 subr, SCM_ARGn, FUNC_NAME);
80662eda
MD
1602 *SCM_SUBR_GENERIC (subr)
1603 = scm_make (SCM_LIST3 (scm_class_generic,
1604 k_name,
1605 SCM_SNAME (subr)));
1606 subrs = SCM_CDR (subrs);
1607 }
1608 return SCM_UNSPECIFIED;
1609}
398d8ee1 1610#undef FUNC_NAME
80662eda 1611
398d8ee1
KN
1612SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
1613 (SCM subr),
1614 "")
1615#define FUNC_NAME s_scm_primitive_generic_generic
80662eda
MD
1616{
1617 if (scm_subr_p (subr) && SCM_SUBR_GENERIC (subr))
1618 {
1619 SCM gf = *SCM_SUBR_GENERIC (subr);
1620 if (gf)
1621 return gf;
1622 }
398d8ee1 1623 return scm_wta (subr, (char *) SCM_ARG1, FUNC_NAME);
80662eda 1624}
398d8ee1 1625#undef FUNC_NAME
80662eda
MD
1626
1627/******************************************************************************
1628 *
1629 * Protocol for calling a generic fumction
1630 * This protocol is roughly equivalent to (parameter are a little bit different
1631 * for efficiency reasons):
1632 *
1633 * + apply-generic (gf args)
1634 * + compute-applicable-methods (gf args ...)
1635 * + sort-applicable-methods (methods args)
1636 * + apply-methods (gf methods args)
1637 *
1638 * apply-methods calls make-next-method to build the "continuation" of a a
1639 * method. Applying a next-method will call apply-next-method which in
1640 * turn will call apply again to call effectively the following method.
1641 *
1642 ******************************************************************************/
1643
1644static int
1645applicablep (SCM actual, SCM formal)
1646{
79a3dafe
DH
1647 /* We already know that the cpl is well formed. */
1648 return !SCM_FALSEP (scm_c_memq (formal, SCM_SLOT (actual, scm_si_cpl)));
80662eda
MD
1649}
1650
1651static int
1652more_specificp (SCM m1, SCM m2, SCM *targs)
1653{
1654 register SCM s1, s2;
1655 register int i;
1656 /*
1657 * Note:
1658 * m1 and m2 can have != length (i.e. one can be one element longer than the
1659 * other when we have a dotted parameter list). For instance, with the call
1660 * (M 1)
1661 * with
1662 * (define-method M (a . l) ....)
1663 * (define-method M (a) ....)
1664 *
1665 * we consider that the second method is more specific.
1666 *
1667 * BTW, targs is an array of types. We don't need it's size since
1668 * we already know that m1 and m2 are applicable (no risk to go past
1669 * the end of this array).
1670 *
1671 */
1672 for (i=0,s1=SPEC_OF(m1),s2=SPEC_OF(m2); ; i++,s1=SCM_CDR(s1),s2=SCM_CDR(s2)) {
1673 if (SCM_NULLP(s1)) return 1;
1674 if (SCM_NULLP(s2)) return 0;
1675 if (SCM_CAR(s1) != SCM_CAR(s2)) {
1676 register SCM l, cs1 = SCM_CAR(s1), cs2 = SCM_CAR(s2);
1677
1678 for (l = SCM_SLOT(targs[i], scm_si_cpl); ; l = SCM_CDR(l)) {
1679 if (cs1 == SCM_CAR(l))
1680 return 1;
1681 if (cs2 == SCM_CAR(l))
1682 return 0;
1683 }
1684 return 0;/* should not occur! */
1685 }
1686 }
1687 return 0; /* should not occur! */
1688}
1689
1690#define BUFFSIZE 32 /* big enough for most uses */
1691
1692static SCM
1693scm_i_vector2list (SCM l, int len)
1694{
1695 int j;
00ffa0e7 1696 SCM z = scm_c_make_vector (len, SCM_UNDEFINED);
80662eda
MD
1697
1698 for (j = 0; j < len; j++, l = SCM_CDR (l)) {
1699 SCM_VELTS (z)[j] = SCM_CAR (l);
1700 }
1701 return z;
1702}
1703
1704static SCM
1705sort_applicable_methods (SCM method_list, int size, SCM *targs)
1706{
1707 int i, j, incr;
1708 SCM *v, vector = SCM_EOL;
1709 SCM buffer[BUFFSIZE];
1710 SCM save = method_list;
1711
1712 /* For reasonably sized method_lists we can try to avoid all the
1713 * consing and reorder the list in place...
1714 * This idea is due to David McClain <Dave_McClain@msn.com>
1715 */
1716 if (size <= BUFFSIZE)
1717 {
1718 for (i = 0; i < size; i++)
1719 {
1720 buffer[i] = SCM_CAR (method_list);
1721 method_list = SCM_CDR (method_list);
1722 }
1723 v = buffer;
1724 }
1725 else
1726 {
1727 /* Too many elements in method_list to keep everything locally */
1728 vector = scm_i_vector2list (save, size);
1729 v = SCM_VELTS (vector);
1730 }
1731
1732 /* Use a simple shell sort since it is generally faster than qsort on
1733 * small vectors (which is probably mostly the case when we have to
1734 * sort a list of applicable methods).
1735 */
1736 for (incr = size / 2; incr; incr /= 2)
1737 {
1738 for (i = incr; i < size; i++)
1739 {
1740 for (j = i - incr; j >= 0; j -= incr)
1741 {
1742 if (more_specificp (v[j], v[j+incr], targs))
1743 break;
1744 else
1745 {
1746 SCM tmp = v[j + incr];
1747 v[j + incr] = v[j];
1748 v[j] = tmp;
1749 }
1750 }
1751 }
1752 }
1753
1754 if (size <= BUFFSIZE)
1755 {
1756 /* We did it in locally, so restore the original list (reordered) in-place */
1757 for (i = 0, method_list = save; i < size; i++, v++)
1758 {
1759 SCM_SETCAR (method_list, *v);
1760 method_list = SCM_CDR (method_list);
1761 }
1762 return save;
1763 }
1764 /* If we are here, that's that we did it the hard way... */
1765 return scm_vector_to_list (vector);
1766}
1767
1768SCM
1769scm_compute_applicable_methods (SCM gf, SCM args, int len, int find_method_p)
1770{
1771 register int i;
1772 int count = 0;
1773 SCM l, fl, applicable = SCM_EOL;
1774 SCM save = args;
1775 SCM buffer[BUFFSIZE], *types, *p;
1776 SCM tmp;
1777
1778 /* Build the list of arguments types */
1779 if (len >= BUFFSIZE) {
00ffa0e7 1780 tmp = scm_c_make_vector (len, SCM_UNDEFINED);
80662eda
MD
1781 /* NOTE: Using pointers to malloced memory won't work if we
1782 1. have preemtive threading, and,
1783 2. have a GC which moves objects. */
1784 types = p = SCM_VELTS(tmp);
1785 }
1786 else
1787 types = p = buffer;
1788
1789 for ( ; SCM_NNULLP (args); args = SCM_CDR (args))
1790 *p++ = scm_class_of (SCM_CAR (args));
1791
1792 /* Build a list of all applicable methods */
1793 for (l = SCM_SLOT (gf, scm_si_methods); SCM_NNULLP (l); l = SCM_CDR (l))
1794 {
1795 fl = SPEC_OF (SCM_CAR (l));
1796 /* Only accept accessors which match exactly in first arg. */
1797 if (SCM_ACCESSORP (SCM_CAR (l))
1798 && (SCM_IMP (fl) || types[0] != SCM_CAR (fl)))
1799 continue;
1800 for (i = 0; ; i++, fl = SCM_CDR (fl))
1801 {
c312aca7 1802 if (SCM_INSTANCEP (fl)
80662eda
MD
1803 /* We have a dotted argument list */
1804 || (i >= len && SCM_NULLP (fl)))
1805 { /* both list exhausted */
1806 applicable = scm_cons (SCM_CAR (l), applicable);
1807 count += 1;
1808 break;
1809 }
1810 if (i >= len
1811 || SCM_NULLP (fl)
1812 || !applicablep (types[i], SCM_CAR (fl)))
1813 break;
1814 }
1815 }
1816
1817 if (count == 0)
1818 {
1819 if (find_method_p)
1820 return SCM_BOOL_F;
1821 CALL_GF2 ("no-applicable-method", gf, save);
1822 /* if we are here, it's because no-applicable-method hasn't signaled an error */
1823 return SCM_BOOL_F;
1824 }
1825 return (count == 1
1826 ? applicable
1827 : sort_applicable_methods (applicable, count, types));
1828}
1829
1830#if 0
1831SCM_PROC (s_sys_compute_applicable_methods, "%compute-applicable-methods", 2, 0, 0, scm_sys_compute_applicable_methods);
1832#endif
1833
1834static const char s_sys_compute_applicable_methods[] = "%compute-applicable-methods";
1835
1836SCM
1837scm_sys_compute_applicable_methods (SCM gf, SCM args)
398d8ee1 1838#define FUNC_NAME s_sys_compute_applicable_methods
80662eda
MD
1839{
1840 int n;
398d8ee1 1841 SCM_VALIDATE_GENERIC (1, gf);
80662eda 1842 n = scm_ilength (args);
398d8ee1 1843 SCM_ASSERT (n >= 0, args, SCM_ARG2, FUNC_NAME);
80662eda
MD
1844 return scm_compute_applicable_methods (gf, args, n, 1);
1845}
398d8ee1 1846#undef FUNC_NAME
80662eda
MD
1847
1848SCM_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));
1849
1850SCM_SYNTAX (s_atslot_ref, "@slot-ref", scm_makmmacro, scm_m_atslot_ref);
1851
1852SCM
1853scm_m_atslot_ref (SCM xorig, SCM env)
1854{
1855 SCM x = SCM_CDR (xorig);
1856 SCM_ASSYNT (scm_ilength (x) == 2, xorig, scm_s_expression, s_atslot_ref);
1857 SCM_ASSYNT (SCM_INUMP (SCM_CADR (x)), SCM_CADR (x), SCM_ARG2, s_atslot_ref);
1858 return scm_cons (SCM_IM_SLOT_REF, x);
1859}
1860
1861SCM_SYNTAX (s_atslot_set_x, "@slot-set!", scm_makmmacro, scm_m_atslot_set_x);
1862
1863SCM
1864scm_m_atslot_set_x (SCM xorig, SCM env)
1865{
1866 SCM x = SCM_CDR (xorig);
1867 SCM_ASSYNT (scm_ilength (x) == 3, xorig, scm_s_expression, s_atslot_set_x);
1868 SCM_ASSYNT (SCM_INUMP (SCM_CADR (x)), SCM_CADR (x), SCM_ARG2, s_atslot_set_x);
1869 return scm_cons (SCM_IM_SLOT_SET_X, x);
1870}
1871
1872SCM_SYNTAX (s_atdispatch, "@dispatch", scm_makmmacro, scm_m_atdispatch);
1873
1874SCM_SYMBOL (sym_atdispatch, s_atdispatch);
1875
1876SCM
1877scm_m_atdispatch (SCM xorig, SCM env)
ca83b028 1878#define FUNC_NAME s_atdispatch
80662eda
MD
1879{
1880 SCM args, n, v, gf, x = SCM_CDR (xorig);
1881 SCM_ASSYNT (scm_ilength (x) == 4, xorig, scm_s_expression, s_atdispatch);
1882 args = SCM_CAR (x);
c312aca7 1883 SCM_ASSYNT (SCM_CONSP (args) || SCM_SYMBOLP (args),
80662eda
MD
1884 args, SCM_ARG1, s_atdispatch);
1885 x = SCM_CDR (x);
1886 n = SCM_XEVALCAR (x, env);
1887 SCM_ASSYNT (SCM_INUMP (n), n, SCM_ARG2, s_atdispatch);
ca83b028 1888 SCM_ASSERT_RANGE (0, n, SCM_INUM (n) >= 1);
80662eda
MD
1889 x = SCM_CDR (x);
1890 v = SCM_XEVALCAR (x, env);
c312aca7 1891 SCM_ASSYNT (SCM_VECTORP (v), v, SCM_ARG3, s_atdispatch);
80662eda
MD
1892 x = SCM_CDR (x);
1893 gf = SCM_XEVALCAR (x, env);
25ba37df 1894 SCM_ASSYNT (SCM_PUREGENERICP (gf), gf, SCM_ARG4, s_atdispatch);
80662eda
MD
1895 return SCM_LIST5 (SCM_IM_DISPATCH, args, n, v, gf);
1896}
ca83b028
DH
1897#undef FUNC_NAME
1898
80662eda
MD
1899
1900#ifdef USE_THREADS
1901static void
1902lock_cache_mutex (void *m)
1903{
1904 SCM mutex = (SCM) m;
1905 scm_lock_mutex (mutex);
1906}
1907
1908static void
1909unlock_cache_mutex (void *m)
1910{
1911 SCM mutex = (SCM) m;
1912 scm_unlock_mutex (mutex);
1913}
1914#endif
1915
1916static SCM
1917call_memoize_method (void *a)
1918{
1919 SCM args = (SCM) a;
1920 SCM gf = SCM_CAR (args);
1921 SCM x = SCM_CADR (args);
1922 /* First check if another thread has inserted a method between
1923 * the cache miss and locking the mutex.
1924 */
1925 SCM cmethod = scm_mcache_lookup_cmethod (x, SCM_CDDR (args));
1926 if (SCM_NIMP (cmethod))
1927 return cmethod;
1928 /*fixme* Use scm_apply */
1929 return CALL_GF3 ("memoize-method!", gf, SCM_CDDR (args), x);
1930}
1931
1932SCM
1933scm_memoize_method (SCM x, SCM args)
1934{
1935 SCM gf = SCM_CAR (scm_last_pair (x));
1936#ifdef USE_THREADS
1937 return scm_internal_dynamic_wind (lock_cache_mutex,
1938 call_memoize_method,
1939 unlock_cache_mutex,
1940 (void *) scm_cons2 (gf, x, args),
1941 (void *) SCM_SLOT (gf, scm_si_cache_mutex));
1942#else
1943 return call_memoize_method ((void *) scm_cons2 (gf, x, args));
1944#endif
1945}
1946
1947/******************************************************************************
1948 *
1949 * A simple make (which will be redefined later in Scheme)
1950 * This version handles only creation of gf, methods and classes (no instances)
1951 *
1952 * Since this code will disappear when Goops will be fully booted,
1953 * no precaution is taken to be efficient.
1954 *
1955 ******************************************************************************/
1956
1957SCM_KEYWORD (k_setter, "setter");
1958SCM_KEYWORD (k_specializers, "specializers");
1959SCM_KEYWORD (k_procedure, "procedure");
1960SCM_KEYWORD (k_dsupers, "dsupers");
1961SCM_KEYWORD (k_slots, "slots");
1962SCM_KEYWORD (k_gf, "generic-function");
1963
398d8ee1
KN
1964SCM_DEFINE (scm_make, "make", 0, 0, 1,
1965 (SCM args),
1966 "")
1967#define FUNC_NAME s_scm_make
80662eda
MD
1968{
1969 SCM class, z;
1970 int len = scm_ilength (args);
1971
1972 if (len <= 0 || (len & 1) == 0)
398d8ee1 1973 SCM_WRONG_NUM_ARGS ();
80662eda
MD
1974
1975 class = SCM_CAR(args);
1976 args = SCM_CDR(args);
1977
1978 if (class == scm_class_generic || class == scm_class_generic_with_setter)
1979 {
1980#ifdef USE_THREADS
1981 z = scm_make_struct (class, SCM_INUM0,
1982 SCM_LIST4 (SCM_EOL,
1983 SCM_INUM0,
1984 SCM_BOOL_F,
1985 scm_make_mutex ()));
1986#else
1987 z = scm_make_struct (class, SCM_INUM0,
1988 SCM_LIST3 (SCM_EOL, SCM_INUM0, SCM_BOOL_F));
1989#endif
1990 scm_set_procedure_property_x (z, scm_sym_name,
1991 scm_get_keyword (k_name,
1992 args,
1993 SCM_BOOL_F));
1994 clear_method_cache (z);
1995 if (class == scm_class_generic_with_setter)
1996 {
1997 SCM setter = scm_get_keyword (k_setter, args, SCM_BOOL_F);
1998 if (SCM_NIMP (setter))
1999 scm_sys_set_object_setter_x (z, setter);
2000 }
2001 }
2002 else
2003 {
2004 z = scm_sys_allocate_instance (class, args);
2005
2006 if (class == scm_class_method
2007 || class == scm_class_simple_method
2008 || class == scm_class_accessor)
2009 {
2010 SCM_SLOT (z, scm_si_generic_function) =
2011 scm_i_get_keyword (k_gf,
2012 args,
2013 len - 1,
2014 SCM_BOOL_F,
398d8ee1 2015 FUNC_NAME);
80662eda
MD
2016 SCM_SLOT (z, scm_si_specializers) =
2017 scm_i_get_keyword (k_specializers,
2018 args,
2019 len - 1,
2020 SCM_EOL,
398d8ee1 2021 FUNC_NAME);
80662eda
MD
2022 SCM_SLOT (z, scm_si_procedure) =
2023 scm_i_get_keyword (k_procedure,
2024 args,
2025 len - 1,
2026 SCM_EOL,
398d8ee1 2027 FUNC_NAME);
80662eda
MD
2028 SCM_SLOT (z, scm_si_code_table) = SCM_EOL;
2029 }
2030 else
2031 {
2032 /* In all the others case, make a new class .... No instance here */
2033 SCM_SLOT (z, scm_si_name) =
2034 scm_i_get_keyword (k_name,
2035 args,
2036 len - 1,
38ae064c 2037 scm_str2symbol ("???"),
398d8ee1 2038 FUNC_NAME);
80662eda
MD
2039 SCM_SLOT (z, scm_si_direct_supers) =
2040 scm_i_get_keyword (k_dsupers,
2041 args,
2042 len - 1,
2043 SCM_EOL,
398d8ee1 2044 FUNC_NAME);
80662eda
MD
2045 SCM_SLOT (z, scm_si_direct_slots) =
2046 scm_i_get_keyword (k_slots,
2047 args,
2048 len - 1,
2049 SCM_EOL,
398d8ee1 2050 FUNC_NAME);
80662eda
MD
2051 }
2052 }
2053 return z;
2054}
398d8ee1 2055#undef FUNC_NAME
80662eda 2056
398d8ee1
KN
2057SCM_DEFINE (scm_find_method, "find-method", 0, 0, 1,
2058 (SCM l),
2059 "")
2060#define FUNC_NAME s_scm_find_method
80662eda
MD
2061{
2062 SCM gf;
2063 int len = scm_ilength (l);
2064
2065 if (len == 0)
398d8ee1 2066 SCM_WRONG_NUM_ARGS ();
80662eda
MD
2067
2068 gf = SCM_CAR(l); l = SCM_CDR(l);
398d8ee1 2069 SCM_VALIDATE_GENERIC (1, gf);
80662eda 2070 if (SCM_NULLP (SCM_SLOT (gf, scm_si_methods)))
398d8ee1 2071 SCM_MISC_ERROR ("no methods for generic ~S", SCM_LIST1 (gf));
80662eda
MD
2072
2073 return scm_compute_applicable_methods (gf, l, len - 1, 1);
2074}
398d8ee1 2075#undef FUNC_NAME
80662eda 2076
398d8ee1
KN
2077SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0,
2078 (SCM m1, SCM m2, SCM targs),
2079 "")
2080#define FUNC_NAME s_scm_sys_method_more_specific_p
80662eda
MD
2081{
2082 SCM l, v;
2083 int i, len;
2084
398d8ee1
KN
2085 SCM_VALIDATE_METHOD (1, m1);
2086 SCM_VALIDATE_METHOD (2, m2);
2087 SCM_ASSERT ((len = scm_ilength (targs)) != -1, targs, SCM_ARG3, FUNC_NAME);
80662eda
MD
2088
2089 /* Verify that all the arguments of targs are classes and place them in a vector*/
00ffa0e7 2090 v = scm_c_make_vector (len, SCM_EOL);
80662eda
MD
2091
2092 for (i=0, l=targs; SCM_NNULLP(l); i++, l=SCM_CDR(l)) {
398d8ee1 2093 SCM_ASSERT (SCM_CLASSP (SCM_CAR (l)), targs, SCM_ARG3, FUNC_NAME);
80662eda
MD
2094 SCM_VELTS(v)[i] = SCM_CAR(l);
2095 }
2096 return more_specificp (m1, m2, SCM_VELTS(v)) ? SCM_BOOL_T: SCM_BOOL_F;
2097}
398d8ee1 2098#undef FUNC_NAME
80662eda
MD
2099
2100
2101
2102/******************************************************************************
2103 *
2104 * Initializations
2105 *
2106 ******************************************************************************/
2107
2108
2109static void
2110make_stdcls (SCM *var, char *name, SCM meta, SCM super, SCM slots)
2111{
38ae064c 2112 SCM tmp = scm_str2symbol (name);
80662eda
MD
2113
2114 *var = scm_permanent_object (scm_basic_make_class (meta,
2115 tmp,
2116 SCM_CONSP (super)
2117 ? super
2118 : SCM_LIST1 (super),
2119 slots));
2120 DEFVAR(tmp, *var);
2121}
2122
2123
2124SCM_KEYWORD (k_slot_definition, "slot-definition");
2125
2126static void
2127create_standard_classes (void)
2128{
2129 SCM slots;
38ae064c
DH
2130 SCM method_slots = SCM_LIST4 (scm_str2symbol ("generic-function"),
2131 scm_str2symbol ("specializers"),
2132 scm_str2symbol ("procedure"),
2133 scm_str2symbol ("code-table"));
2134 SCM amethod_slots = SCM_LIST1 (SCM_LIST3 (scm_str2symbol ("slot-definition"),
80662eda
MD
2135 k_init_keyword,
2136 k_slot_definition));
2137#ifdef USE_THREADS
38ae064c 2138 SCM mutex_slot = SCM_LIST1 (scm_str2symbol ("make-mutex"));
80662eda
MD
2139#else
2140 SCM mutex_slot = SCM_BOOL_F;
2141#endif
38ae064c
DH
2142 SCM gf_slots = SCM_LIST4 (scm_str2symbol ("methods"),
2143 SCM_LIST3 (scm_str2symbol ("n-specialized"),
80662eda
MD
2144 k_init_value,
2145 SCM_INUM0),
38ae064c 2146 SCM_LIST3 (scm_str2symbol ("used-by"),
80662eda
MD
2147 k_init_value,
2148 SCM_BOOL_F),
38ae064c 2149 SCM_LIST3 (scm_str2symbol ("cache-mutex"),
80662eda
MD
2150 k_init_thunk,
2151 scm_closure (SCM_LIST2 (SCM_EOL,
2152 mutex_slot),
2153 SCM_EOL)));
2154
2155 /* Foreign class slot classes */
2156 make_stdcls (&scm_class_foreign_slot, "<foreign-slot>",
2157 scm_class_class, scm_class_top, SCM_EOL);
2158 make_stdcls (&scm_class_protected, "<protected-slot>",
2159 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2160 make_stdcls (&scm_class_opaque, "<opaque-slot>",
2161 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2162 make_stdcls (&scm_class_read_only, "<read-only-slot>",
2163 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2164 make_stdcls (&scm_class_self, "<self-slot>",
2165 scm_class_class,
2166 SCM_LIST2 (scm_class_foreign_slot, scm_class_read_only),
2167 SCM_EOL);
2168 make_stdcls (&scm_class_protected_opaque, "<protected-opaque-slot>",
2169 scm_class_class,
2170 SCM_LIST2 (scm_class_protected, scm_class_opaque),
2171 SCM_EOL);
2172 make_stdcls (&scm_class_protected_read_only, "<protected-read-only-slot>",
2173 scm_class_class,
2174 SCM_LIST2 (scm_class_protected, scm_class_read_only),
2175 SCM_EOL);
2176 make_stdcls (&scm_class_scm, "<scm-slot>",
2177 scm_class_class, scm_class_protected, SCM_EOL);
2178 make_stdcls (&scm_class_int, "<int-slot>",
2179 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2180 make_stdcls (&scm_class_float, "<float-slot>",
2181 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2182 make_stdcls (&scm_class_double, "<double-slot>",
2183 scm_class_class, scm_class_foreign_slot, SCM_EOL);
2184
2185 /* Continue initialization of class <class> */
2186
2187 slots = build_class_class_slots ();
2188 SCM_SLOT (scm_class_class, scm_si_direct_slots) = slots;
2189 SCM_SLOT (scm_class_class, scm_si_slots) = slots;
2190 SCM_SLOT (scm_class_class, scm_si_getters_n_setters)
2191 = compute_getters_n_setters (slots);
2192
2193 make_stdcls (&scm_class_foreign_class, "<foreign-class>",
2194 scm_class_class, scm_class_class,
38ae064c 2195 SCM_LIST2 (SCM_LIST3 (scm_str2symbol ("constructor"),
80662eda
MD
2196 k_class,
2197 scm_class_opaque),
38ae064c 2198 SCM_LIST3 (scm_str2symbol ("destructor"),
80662eda
MD
2199 k_class,
2200 scm_class_opaque)));
2201 make_stdcls (&scm_class_foreign_object, "<foreign-object>",
2202 scm_class_foreign_class, scm_class_object, SCM_EOL);
2203 SCM_SET_CLASS_FLAGS (scm_class_foreign_object, SCM_CLASSF_FOREIGN);
2204
2205 /* scm_class_generic functions classes */
2206 make_stdcls (&scm_class_procedure_class, "<procedure-class>",
2207 scm_class_class, scm_class_class, SCM_EOL);
2208 make_stdcls (&scm_class_entity_class, "<entity-class>",
2209 scm_class_class, scm_class_procedure_class, SCM_EOL);
2210 make_stdcls (&scm_class_operator_class, "<operator-class>",
2211 scm_class_class, scm_class_procedure_class, SCM_EOL);
2212 make_stdcls (&scm_class_operator_with_setter_class,
2213 "<operator-with-setter-class>",
2214 scm_class_class, scm_class_operator_class, SCM_EOL);
2215 make_stdcls (&scm_class_method, "<method>",
2216 scm_class_class, scm_class_object, method_slots);
2217 make_stdcls (&scm_class_simple_method, "<simple-method>",
2218 scm_class_class, scm_class_method, SCM_EOL);
2219 SCM_SET_CLASS_FLAGS (scm_class_simple_method, SCM_CLASSF_SIMPLE_METHOD);
2220 make_stdcls (&scm_class_accessor, "<accessor-method>",
2221 scm_class_class, scm_class_simple_method, amethod_slots);
2222 SCM_SET_CLASS_FLAGS (scm_class_accessor, SCM_CLASSF_ACCESSOR_METHOD);
2223 make_stdcls (&scm_class_entity, "<entity>",
2224 scm_class_entity_class, scm_class_object, SCM_EOL);
2225 make_stdcls (&scm_class_entity_with_setter, "<entity-with-setter>",
2226 scm_class_entity_class, scm_class_entity, SCM_EOL);
2227 make_stdcls (&scm_class_generic, "<generic>",
2228 scm_class_entity_class, scm_class_entity, gf_slots);
2229 SCM_SET_CLASS_FLAGS (scm_class_generic, SCM_CLASSF_PURE_GENERIC);
2230 make_stdcls (&scm_class_generic_with_setter, "<generic-with-setter>",
2231 scm_class_entity_class,
2232 SCM_LIST2 (scm_class_generic, scm_class_entity_with_setter),
2233 SCM_EOL);
2234#if 0
2235 /* Patch cpl since compute_cpl doesn't support multiple inheritance. */
2236 SCM_SLOT (scm_class_generic_with_setter, scm_si_cpl) =
2237 scm_append (SCM_LIST3 (SCM_LIST2 (scm_class_generic_with_setter,
2238 scm_class_generic),
2239 SCM_SLOT (scm_class_entity_with_setter,
2240 scm_si_cpl),
2241 SCM_EOL));
2242#endif
2243 SCM_SET_CLASS_FLAGS (scm_class_generic_with_setter, SCM_CLASSF_PURE_GENERIC);
2244
2245 /* Primitive types classes */
2246 make_stdcls (&scm_class_boolean, "<boolean>",
2247 scm_class_class, scm_class_top, SCM_EOL);
2248 make_stdcls (&scm_class_char, "<char>",
2249 scm_class_class, scm_class_top, SCM_EOL);
2250 make_stdcls (&scm_class_list, "<list>",
2251 scm_class_class, scm_class_top, SCM_EOL);
2252 make_stdcls (&scm_class_pair, "<pair>",
2253 scm_class_class, scm_class_list, SCM_EOL);
2254 make_stdcls (&scm_class_null, "<null>",
2255 scm_class_class, scm_class_list, SCM_EOL);
2256 make_stdcls (&scm_class_string, "<string>",
2257 scm_class_class, scm_class_top, SCM_EOL);
2258 make_stdcls (&scm_class_symbol, "<symbol>",
2259 scm_class_class, scm_class_top, SCM_EOL);
2260 make_stdcls (&scm_class_vector, "<vector>",
2261 scm_class_class, scm_class_top, SCM_EOL);
2262 make_stdcls (&scm_class_number, "<number>",
2263 scm_class_class, scm_class_top, SCM_EOL);
2264 make_stdcls (&scm_class_complex, "<complex>",
2265 scm_class_class, scm_class_number, SCM_EOL);
2266 make_stdcls (&scm_class_real, "<real>",
2267 scm_class_class, scm_class_complex, SCM_EOL);
2268 make_stdcls (&scm_class_integer, "<integer>",
2269 scm_class_class, scm_class_real, SCM_EOL);
2270 make_stdcls (&scm_class_keyword, "<keyword>",
2271 scm_class_class, scm_class_top, SCM_EOL);
2272 make_stdcls (&scm_class_unknown, "<unknown>",
2273 scm_class_class, scm_class_top, SCM_EOL);
2274 make_stdcls (&scm_class_procedure, "<procedure>",
2275 scm_class_procedure_class, scm_class_top, SCM_EOL);
2276 make_stdcls (&scm_class_procedure_with_setter, "<procedure-with-setter>",
2277 scm_class_procedure_class, scm_class_procedure, SCM_EOL);
2278 make_stdcls (&scm_class_primitive_generic, "<primitive-generic>",
2279 scm_class_procedure_class, scm_class_procedure, SCM_EOL);
2280 make_stdcls (&scm_class_port, "<port>",
2281 scm_class_class, scm_class_top, SCM_EOL);
2282 make_stdcls (&scm_class_input_port, "<input-port>",
2283 scm_class_class, scm_class_port, SCM_EOL);
2284 make_stdcls (&scm_class_output_port, "<output-port>",
2285 scm_class_class, scm_class_port, SCM_EOL);
2286 make_stdcls (&scm_class_input_output_port, "<input-output-port>",
2287 scm_class_class,
2288 SCM_LIST2 (scm_class_input_port, scm_class_output_port),
2289 SCM_EOL);
2290}
2291
2292/**********************************************************************
2293 *
2294 * Smob classes
2295 *
2296 **********************************************************************/
2297
2298static SCM
2299make_class_from_template (char *template, char *type_name, SCM supers)
2300{
2301 SCM class, name;
2302 if (type_name)
2303 {
2304 char buffer[100];
2305 sprintf (buffer, template, type_name);
38ae064c 2306 name = scm_str2symbol (buffer);
80662eda
MD
2307 }
2308 else
2309 name = SCM_GOOPS_UNBOUND;
2310
2311 class = scm_permanent_object (scm_basic_make_class (scm_class_class,
2312 name,
2313 supers,
2314 SCM_EOL));
2315
2316 /* Only define name if doesn't already exist. */
2317 if (!SCM_GOOPS_UNBOUNDP (name)
2318 && SCM_FALSEP (scm_apply (scm_goops_lookup_closure,
2319 SCM_LIST2 (name, SCM_BOOL_F),
2320 SCM_EOL)))
2321 {
2322 /* Make sure we add the binding in the GOOPS module.
2323 * This kludge is needed until DEFVAR ceases to use `define-public'
2324 * or `define-public' ceases to use `current-module'.
2325 */
2326 SCM old_module = scm_select_module (scm_module_goops);
2327 DEFVAR (name, class);
2328 scm_select_module (old_module);
2329 }
2330 return class;
2331}
2332
2333SCM
2334scm_make_extended_class (char *type_name)
2335{
2336 return make_class_from_template ("<%s>",
2337 type_name,
2338 SCM_LIST1 (scm_class_top));
2339}
2340
2341static void
2342create_smob_classes (void)
2343{
2344 int i;
2345
2346 scm_smob_class = (SCM *) malloc (255 * sizeof (SCM));
2347 for (i = 0; i < 255; ++i)
2348 scm_smob_class[i] = 0;
2349
2350 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_big)] = scm_class_integer;
2351 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_real)] = scm_class_real;
2352 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_complex)] = scm_class_complex;
2353 scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_keyword)] = scm_class_keyword;
2354
2355 for (i = 0; i < scm_numsmob; ++i)
2356 if (!scm_smob_class[i])
2357 scm_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i));
2358}
2359
2360void
2361scm_make_port_classes (int ptobnum, char *type_name)
2362{
2363 SCM c, class = make_class_from_template ("<%s-port>",
2364 type_name,
2365 SCM_LIST1 (scm_class_port));
2366 scm_port_class[SCM_IN_PCLASS_INDEX + ptobnum]
2367 = make_class_from_template ("<%s-input-port>",
2368 type_name,
2369 SCM_LIST2 (class, scm_class_input_port));
2370 scm_port_class[SCM_OUT_PCLASS_INDEX + ptobnum]
2371 = make_class_from_template ("<%s-output-port>",
2372 type_name,
2373 SCM_LIST2 (class, scm_class_output_port));
2374 scm_port_class[SCM_INOUT_PCLASS_INDEX + ptobnum]
2375 = c
2376 = make_class_from_template ("<%s-input-output-port>",
2377 type_name,
2378 SCM_LIST2 (class,
2379 scm_class_input_output_port));
2380 /* Patch cpl (since this tree is too complex for the C level compute-cpl) */
2381 SCM_SLOT (c, scm_si_cpl)
2382 = scm_cons2 (c, class, SCM_SLOT (scm_class_input_output_port, scm_si_cpl));
2383}
2384
2385static void
2386create_port_classes (void)
2387{
2388 int i;
2389
2390 scm_port_class = (SCM *) malloc (3 * 256 * sizeof (SCM));
2391 for (i = 0; i < 3 * 256; ++i)
2392 scm_port_class[i] = 0;
2393
2394 for (i = 0; i < scm_numptob; ++i)
2395 scm_make_port_classes (i, SCM_PTOBNAME (i));
2396}
2397
2398static SCM
2399make_struct_class (void *closure, SCM key, SCM data, SCM prev)
2400{
2401 if (SCM_NFALSEP (SCM_STRUCT_TABLE_NAME (data)))
2402 SCM_SET_STRUCT_TABLE_CLASS (data,
2403 scm_make_extended_class
b24b5e13 2404 (SCM_SYMBOL_CHARS (SCM_STRUCT_TABLE_NAME (data))));
80662eda
MD
2405 return SCM_UNSPECIFIED;
2406}
2407
2408static void
2409create_struct_classes (void)
2410{
2411 scm_internal_hash_fold (make_struct_class, 0, SCM_BOOL_F, scm_struct_table);
2412}
2413
2414/**********************************************************************
2415 *
2416 * C interface
2417 *
2418 **********************************************************************/
2419
2420void
2421scm_load_goops ()
2422{
2423 if (!goops_loaded_p)
2424 scm_resolve_module (scm_read_0str ("(oop goops)"));
2425}
2426
2427SCM
2428scm_make_foreign_object (SCM class, SCM initargs)
2429{
2430 void * (*constructor) (SCM)
2431 = (void * (*) (SCM)) SCM_SLOT (class, scm_si_constructor);
2432 SCM_ASSERT (constructor != 0, class, "Can't make instances of this class",
398d8ee1 2433 s_scm_make);
80662eda
MD
2434 return scm_wrap_object (class, constructor (initargs));
2435}
2436
2437static size_t
2438scm_free_foreign_object (SCM *class, SCM *data)
2439{
2440 size_t (*destructor) (void *)
2441 = (size_t (*) (void *)) class[scm_si_destructor];
2442 return destructor (data);
2443}
2444
2445SCM
2446scm_make_class (SCM meta, char *s_name, SCM supers, size_t size,
2447 void * (*constructor) (SCM initargs),
2448 size_t (*destructor) (void *))
2449{
2450 SCM name, class;
38ae064c 2451 name = scm_str2symbol (s_name);
80662eda
MD
2452 if (SCM_IMP (supers))
2453 supers = SCM_LIST1 (scm_class_foreign_object);
2454 class = scm_basic_basic_make_class (meta, name, supers, SCM_EOL);
2455 scm_sys_inherit_magic_x (class, supers);
2456
2457 if (destructor != 0)
2458 {
2459 SCM_SLOT (class, scm_si_destructor) = (SCM) destructor;
2460 SCM_SET_CLASS_DESTRUCTOR (class, scm_free_foreign_object);
2461 }
2462 else if (size > 0)
2463 {
2464 SCM_SET_CLASS_DESTRUCTOR (class, scm_struct_free_light);
2465 SCM_SET_CLASS_INSTANCE_SIZE (class, size);
2466 }
2467
38ae064c 2468 SCM_SLOT (class, scm_si_layout) = scm_str2symbol ("");
80662eda
MD
2469 SCM_SLOT (class, scm_si_constructor) = (SCM) constructor;
2470
2471 return class;
2472}
2473
2474SCM_SYMBOL (sym_o, "o");
2475SCM_SYMBOL (sym_x, "x");
2476
2477SCM_KEYWORD (k_accessor, "accessor");
2478SCM_KEYWORD (k_getter, "getter");
2479
2480static SCM
2481default_setter (SCM obj, SCM c)
2482{
2483 scm_misc_error ("slot-set!", "read-only slot", SCM_EOL);
2484 return 0;
2485}
2486
2487void
2488scm_add_slot (SCM class, char *slot_name, SCM slot_class,
2489 SCM (*getter) (SCM obj),
2490 SCM (*setter) (SCM obj, SCM x),
2491 char *accessor_name)
2492{
2493 {
2494 SCM get = scm_make_subr_opt ("goops:get", scm_tc7_subr_1, getter, 0);
2495 SCM set = scm_make_subr_opt ("goops:set", scm_tc7_subr_2,
2496 setter ? setter : default_setter, 0);
2497 SCM getm = scm_closure (SCM_LIST2 (SCM_LIST1 (sym_o),
2498 SCM_LIST2 (get, sym_o)),
2499 SCM_EOL);
2500 SCM setm = scm_closure (SCM_LIST2 (SCM_LIST2 (sym_o, sym_x),
2501 SCM_LIST3 (set, sym_o, sym_x)),
2502 SCM_EOL);
2503 {
38ae064c
DH
2504 SCM name = scm_str2symbol (slot_name);
2505 SCM aname = scm_str2symbol (accessor_name);
80662eda
MD
2506 SCM gf = scm_ensure_accessor (aname);
2507 SCM slot = SCM_LIST5 (name,
2508 k_class, slot_class,
2509 setter ? k_accessor : k_getter,
2510 gf);
2511 SCM gns = SCM_LIST4 (name, SCM_BOOL_F, get, set);
2512
2513 scm_add_method (gf, scm_make (SCM_LIST5 (scm_class_accessor,
2514 k_specializers,
2515 SCM_LIST1 (class),
2516 k_procedure, getm)));
2517 scm_add_method (scm_setter (gf),
2518 scm_make (SCM_LIST5 (scm_class_accessor,
2519 k_specializers,
2520 SCM_LIST2 (class,
2521 scm_class_top),
2522 k_procedure, setm)));
2523 DEFVAR (aname, gf);
2524
2525 SCM_SLOT (class, scm_si_slots)
2526 = scm_append_x (SCM_LIST2 (SCM_SLOT (class, scm_si_slots),
2527 SCM_LIST1 (slot)));
2528 SCM_SLOT (class, scm_si_getters_n_setters)
2529 = scm_append_x (SCM_LIST2 (SCM_SLOT (class, scm_si_getters_n_setters),
2530 SCM_LIST1 (gns)));
2531 }
2532 }
2533 {
2534 int n = SCM_INUM (SCM_SLOT (class, scm_si_nfields));
2535
2536 SCM_SLOT (class, scm_si_nfields)
2537 = SCM_MAKINUM (n + 1);
2538 }
2539}
2540
2541SCM
2542scm_wrap_object (SCM class, void *data)
2543{
2544 SCM z;
2545 SCM_NEWCELL2 (z);
2546 SCM_SETCDR (z, (SCM) data);
2547 SCM_SET_STRUCT_GC_CHAIN (z, 0);
2548 SCM_SETCAR (z, SCM_UNPACK (SCM_CDR (class)) | scm_tc3_cons_gloc);
2549 return z;
2550}
2551
2552SCM scm_components;
2553
2554SCM
2555scm_wrap_component (SCM class, SCM container, void *data)
2556{
2557 SCM obj = scm_wrap_object (class, data);
2558 SCM handle = scm_hash_fn_create_handle_x (scm_components,
2559 obj,
2560 SCM_BOOL_F,
2561 scm_struct_ihashq,
2562 scm_sloppy_assq,
2563 0);
2564 SCM_SETCDR (handle, container);
2565 return obj;
2566}
2567
2568SCM
2569scm_ensure_accessor (SCM name)
2570{
2571 SCM gf = scm_apply (SCM_TOP_LEVEL_LOOKUP_CLOSURE,
2572 SCM_LIST2 (name, SCM_BOOL_F),
2573 SCM_EOL);
2574 if (!SCM_IS_A_P (gf, scm_class_generic_with_setter))
2575 {
2576 gf = scm_make (SCM_LIST3 (scm_class_generic, k_name, name));
2577 gf = scm_make (SCM_LIST5 (scm_class_generic_with_setter,
2578 k_name, name,
2579 k_setter, gf));
2580 }
2581 return gf;
2582}
2583
2584SCM_SYMBOL (sym_internal_add_method_x, "internal-add-method!");
2585
2586void
2587scm_add_method (SCM gf, SCM m)
2588{
ca83b028
DH
2589 scm_i_eval (SCM_LIST3 (sym_internal_add_method_x, gf, m),
2590 scm_top_level_env (scm_goops_lookup_closure));
80662eda
MD
2591}
2592
2593#ifdef GUILE_DEBUG
2594/*
2595 * Debugging utilities
2596 */
2597
398d8ee1
KN
2598SCM_DEFINE (scm_pure_generic_p, "pure-generic?", 1, 0, 0,
2599 (SCM obj),
2600 "")
2601#define FUNC_NAME s_scm_pure_generic_p
80662eda 2602{
25ba37df 2603 return SCM_BOOL (SCM_PUREGENERICP (obj));
80662eda 2604}
398d8ee1 2605#undef FUNC_NAME
80662eda
MD
2606
2607#endif /* GUILE_DEBUG */
2608
2609/*
2610 * Initialization
2611 */
2612
398d8ee1
KN
2613SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
2614 (),
2615 "")
2616#define FUNC_NAME s_scm_sys_goops_loaded
80662eda
MD
2617{
2618 goops_loaded_p = 1;
2619 var_compute_applicable_methods
2620 = SCM_CDR (scm_apply (scm_goops_lookup_closure,
2621 SCM_LIST2 (SCM_CAR (var_compute_applicable_methods),
2622 SCM_BOOL_F),
2623 SCM_EOL));
2624 return SCM_UNSPECIFIED;
2625}
398d8ee1 2626#undef FUNC_NAME
80662eda
MD
2627
2628SCM scm_module_goops;
2629
2630void
2631scm_init_goops (void)
2632{
2633 SCM old_module;
2634 scm_module_goops = scm_make_module (scm_read_0str ("(oop goops)"));
2635 old_module = scm_select_module (scm_module_goops);
2636
2637 scm_goops_lookup_closure = scm_module_lookup_closure (scm_module_goops);
2638
2639 scm_components = scm_permanent_object (scm_make_weak_key_hash_table
2640 (SCM_MAKINUM (37)));
2641
2642 goops_rstate = scm_c_make_rstate ("GOOPS", 5);
2643
8dc9439f 2644#ifndef SCM_MAGIC_SNARFER
80662eda 2645#include "libguile/goops.x"
8dc9439f 2646#endif
80662eda
MD
2647
2648 list_of_no_method = scm_permanent_object (SCM_LIST1 (sym_no_method));
2649
2650 hell = scm_must_malloc (hell_size, "hell");
2651#ifdef USE_THREADS
2652 scm_mutex_init (&hell_mutex);
2653#endif
2654
2655 create_basic_classes ();
2656 create_standard_classes ();
2657 create_smob_classes ();
2658 create_struct_classes ();
2659 create_port_classes ();
2660
2661 {
38ae064c 2662 SCM name = scm_str2symbol ("no-applicable-method");
80662eda
MD
2663 scm_no_applicable_method
2664 = scm_permanent_object (scm_make (SCM_LIST3 (scm_class_generic,
2665 k_name,
2666 name)));
2667 DEFVAR (name, scm_no_applicable_method);
2668 }
2669
2670 scm_select_module (old_module);
2671}
2672
2673void
2674scm_init_oop_goops_goopscore_module ()
2675{
2676 scm_register_module_xxx ("oop goops goopscore", (void *) scm_init_goops);
2677}
23437298
DH
2678
2679/*
2680 Local Variables:
2681 c-file-style: "gnu"
2682 End:
2683*/