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