Debian Codegraph @

src/class.c

raw
1 /*
2 * class.c - class metaobject implementation
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define LIBGAUCHE_BODY
35 #include "gauche.h"
36 #include "gauche/code.h"
37 #include "gauche/priv/configP.h"
38 #include "gauche/priv/builtin-syms.h"
39 #include "gauche/priv/classP.h"
40 #include "gauche/priv/identifierP.h"
41 #include "gauche/priv/macroP.h"
42 #include "gauche/priv/pairP.h"
43 #include "gauche/priv/writerP.h"
44 #include "gauche/priv/dispatchP.h"
45 #include "gauche/priv/parameterP.h"
46 #include "gauche/priv/procP.h"
47 #include "gauche/priv/stringP.h"
48 #include "gauche/priv/vmP.h"
49
50 /* Some routines uses small array on stack to keep data about
51 arguments to dispatch. If the # of args used for dispach is bigger
52 than this, the routine allocates an array in heap. */
53 #define PREALLOC_SIZE 32
54
55
56 /*===================================================================
57 * Built-in classes
58 */
59
60 static void class_print(ScmObj, ScmPort *, ScmWriteContext*);
61 static void generic_print(ScmObj, ScmPort *, ScmWriteContext*);
62 static void method_print(ScmObj, ScmPort *, ScmWriteContext*);
63 static void next_method_print(ScmObj, ScmPort *, ScmWriteContext*);
64 static void slot_accessor_print(ScmObj, ScmPort *, ScmWriteContext*);
65 static void accessor_method_print(ScmObj, ScmPort *, ScmWriteContext*);
66 static void proxy_type_print(ScmObj, ScmPort *, ScmWriteContext*);
67 static int proxy_type_compare(ScmObj, ScmObj, int);
68
69 static ScmObj class_allocate(ScmClass *klass, ScmObj initargs);
70 static ScmObj generic_allocate(ScmClass *klass, ScmObj initargs);
71 static ScmObj method_allocate(ScmClass *klass, ScmObj initargs);
72 static ScmObj slot_accessor_allocate(ScmClass *klass, ScmObj initargs);
73 static void initialize_builtin_cpl(ScmClass *klass, ScmObj supers);
74
75 static ScmObj instance_class_redefinition(ScmObj obj, ScmClass *old);
76 static ScmObj slot_set_using_accessor(ScmObj obj, ScmSlotAccessor *sa,
77 ScmObj val);
78 static void immutable_setter(ScmObj, ScmObj);
79 static ScmObj instance_allocate(ScmClass *klass, ScmObj initargs);
80
81 static ScmObj fallback_compare(ScmObj *, int, ScmGeneric *);
82
83 static ScmObj builtin_initialize(ScmObj *, int, ScmGeneric *);
84
85 ScmClass *Scm_DefaultCPL[] = {
86 SCM_CLASS_STATIC_PTR(Scm_TopClass),
87 NULL
88 };
89
90 ScmClass *Scm_ObjectCPL[] = {
91 SCM_CLASS_STATIC_PTR(Scm_ObjectClass),
92 SCM_CLASS_STATIC_PTR(Scm_TopClass),
93 NULL
94 };
95
96 static ScmClass *Scm_MethodCPL[] = {
97 SCM_CLASS_STATIC_PTR(Scm_MethodClass),
98 SCM_CLASS_STATIC_PTR(Scm_ObjectClass),
99 SCM_CLASS_STATIC_PTR(Scm_TopClass),
100 NULL
101 };
102
103 ScmClass *Scm_MetaclassCPL[] = {
104 SCM_CLASS_STATIC_PTR(Scm_ClassClass),
105 SCM_CLASS_STATIC_PTR(Scm_ObjectClass),
106 SCM_CLASS_STATIC_PTR(Scm_TypeClass),
107 SCM_CLASS_STATIC_PTR(Scm_TopClass),
108 NULL
109 };
110
111 /* Class <top> is the superclass of all classes. The class initialization
112 routine ensures that the class precedence list always terminates by <top>.
113 Class <bottom> is the subclass of all classes. It won't appear in the
114 class precedence list, but Scm_SubTypeP treats it specially and answers
115 yes to Scm_SubTypeP(<bottom>, <any-class>). */
116 SCM_DEFINE_ABSTRACT_CLASS(Scm_TopClass, NULL);
117 SCM_DEFINE_ABSTRACT_CLASS(Scm_BottomClass, NULL);
118 SCM_DEFINE_ABSTRACT_CLASS(Scm_TypeClass, NULL);
119
120 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_BoolClass, NULL);
121 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_CharClass, NULL);
122 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_UnknownClass, NULL);
123 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_EOFObjectClass, NULL);
124 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_UndefinedObjectClass, NULL);
125 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_ForeignPointerClass, NULL);
126
127 SCM_DEFINE_BASE_CLASS(Scm_ObjectClass, ScmInstance,
128 NULL, NULL, NULL, instance_allocate,
129 SCM_CLASS_DEFAULT_CPL);
130
131 /* Basic metaobjects */
132 SCM_DEFINE_BASE_CLASS(Scm_ClassClass, ScmClass,
133 class_print, NULL, NULL, class_allocate,
134 SCM_CLASS_METACLASS_CPL+1);
135 SCM_DEFINE_BASE_CLASS(Scm_GenericClass, ScmGeneric,
136 generic_print, NULL, NULL, generic_allocate,
137 SCM_CLASS_OBJECT_CPL);
138 SCM_DEFINE_BASE_CLASS(Scm_MethodClass, ScmMethod,
139 method_print, NULL, NULL, method_allocate,
140 SCM_CLASS_OBJECT_CPL);
141
142 /* Internally used classes */
143 SCM_DEFINE_BUILTIN_CLASS(Scm_SlotAccessorClass,
144 slot_accessor_print, NULL, NULL,
145 slot_accessor_allocate,
146 SCM_CLASS_DEFAULT_CPL);
147 SCM_DEFINE_BUILTIN_CLASS(Scm_AccessorMethodClass,
148 accessor_method_print, NULL, NULL,
149 method_allocate,
150 Scm_MethodCPL);
151 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_NextMethodClass, next_method_print);
152
153 SCM_DEFINE_BASE_CLASS(Scm_ProxyTypeClass, ScmProxyType,
154 proxy_type_print, proxy_type_compare, NULL, NULL,
155 SCM_CLASS_TYPE_CPL);
156
157 /* Builtin generic functions */
158 SCM_DEFINE_GENERIC(Scm_GenericMake, Scm_NoNextMethod, NULL);
159 SCM_DEFINE_GENERIC(Scm_GenericAllocate, Scm_NoNextMethod, NULL);
160 SCM_DEFINE_GENERIC(Scm_GenericInitialize, builtin_initialize, NULL);
161 SCM_DEFINE_GENERIC(Scm_GenericAddMethod, Scm_NoNextMethod, NULL);
162 SCM_DEFINE_GENERIC(Scm_GenericDeleteMethod, Scm_NoNextMethod, NULL);
163 SCM_DEFINE_GENERIC(Scm_GenericComputeCPL, Scm_NoNextMethod, NULL);
164 SCM_DEFINE_GENERIC(Scm_GenericComputeSlots, Scm_NoNextMethod, NULL);
165 SCM_DEFINE_GENERIC(Scm_GenericComputeGetNSet, Scm_NoNextMethod, NULL);
166 SCM_DEFINE_GENERIC(Scm_GenericComputeApplicableMethods, Scm_NoNextMethod, NULL);
167 SCM_DEFINE_GENERIC(Scm_GenericUpdateDirectMethod, Scm_NoNextMethod, NULL);
168 SCM_DEFINE_GENERIC(Scm_GenericApplyGeneric, Scm_NoNextMethod, NULL);
169 SCM_DEFINE_GENERIC(Scm_GenericMethodMoreSpecificP, Scm_NoNextMethod, NULL);
170 SCM_DEFINE_GENERIC(Scm_GenericSlotMissing, Scm_NoNextMethod, NULL);
171 SCM_DEFINE_GENERIC(Scm_GenericSlotUnbound, Scm_NoNextMethod, NULL);
172 SCM_DEFINE_GENERIC(Scm_GenericSlotRefUsingClass, Scm_NoNextMethod, NULL);
173 SCM_DEFINE_GENERIC(Scm_GenericSlotSetUsingClass, Scm_NoNextMethod, NULL);
174 SCM_DEFINE_GENERIC(Scm_GenericSlotBoundUsingClassP, Scm_NoNextMethod, NULL);
175 SCM_DEFINE_GENERIC(Scm_GenericObjectEqualP, fallback_compare, NULL);
176 SCM_DEFINE_GENERIC(Scm_GenericObjectCompare, fallback_compare, NULL);
177 SCM_DEFINE_GENERIC(Scm_GenericObjectHash, Scm_NoNextMethod, NULL);
178 SCM_DEFINE_GENERIC(Scm_GenericObjectApply, Scm_InvalidApply, NULL);
179 SCM_DEFINE_GENERIC(Scm_GenericObjectSetter, Scm_InvalidApply, NULL);
180 SCM_DEFINE_GENERIC(Scm_GenericChangeClass, Scm_NoNextMethod, NULL);
181
182 /* Some frequently-used pointers */
183 static ScmObj key_allocation = SCM_FALSE;
184 static ScmObj key_slot_accessor = SCM_FALSE;
185 static ScmObj key_builtin = SCM_FALSE;
186 static ScmObj key_name = SCM_FALSE;
187 static ScmObj key_lambda_list = SCM_FALSE;
188 static ScmObj key_method_locked = SCM_FALSE;
189 static ScmObj key_generic = SCM_FALSE;
190 static ScmObj key_specializers = SCM_FALSE;
191 static ScmObj key_body = SCM_FALSE;
192
193 /* TRANSIENT: Global flag to disable generic dispatcher. This is an escape
194 pod to fall back the default mechanism when we find a serious bug in
195 dispatch accelerator. Can be turned on with a environment variable. */
196 static int disable_generic_dispatcher = FALSE;
197
198 /* A global lock to serialize class redefinition. We need it since
199 class redefinition is not a local effect---it propagates through
200 its subclasses. So it is pretty difficult to guarantee consistency
201 if two threads enter the class redefinition, even if they redefine
202 different classes.
203 This lock works as a recursive lock. Scm_StartClassRedefinition
204 increments the lock count, and Scm_CommitClassRedefinition decrements it.
205 */
206 static struct {
207 ScmVM *owner; /* thread that grabs the lock, or NULL */
208 int count;
209 ScmInternalMutex mutex;
210 ScmInternalCond cv;
211 } class_redefinition_lock;
212
213 /* Imporant slots in <class> metaboject can be modified only when the
214 class is in 'malleable' state. Here's the check. */
215 #define CHECK_MALLEABLE(k, who) \
216 if (!SCM_CLASS_MALLEABLE_P(k)) { \
217 Scm_Error("%s: class is not malleable: %S", \
218 who, SCM_OBJ(k)); \
219 }
220
221 /*=====================================================================
222 * Auxiliary utilities
223 */
224
225 static ScmClass **class_list_to_array(ScmObj classes, int len)
226 {
227 ScmObj cp;
228 ScmClass **v, **vp;
229 v = vp = SCM_NEW_ARRAY(ScmClass*, len+1);
230 SCM_FOR_EACH(cp, classes) {
231 if (!Scm_TypeP(SCM_CAR(cp), SCM_CLASS_CLASS))
232 Scm_Error("list of classes required, but found non-class object"
233 " %S in %S", SCM_CAR(cp), classes);
234 *vp++ = SCM_CLASS(SCM_CAR(cp));
235 }
236 *vp = NULL;
237 return v;
238 }
239
240 static ScmObj class_array_to_list(ScmClass **array, int len)
241 {
242 ScmObj h = SCM_NIL, t = SCM_NIL;
243 if (array) while (len-- > 0) SCM_APPEND1(h, t, SCM_OBJ(*array++));
244 return h;
245 }
246
247 static ScmObj class_array_to_names(ScmClass **array, int len)
248 {
249 ScmObj h = SCM_NIL, t = SCM_NIL;
250 for (int i=0; i<len; i++, array++) SCM_APPEND1(h, t, (*array)->name);
251 return h;
252 }
253
254 /* If the class name has brackets '<' and '>', as in Gauche's convention,
255 returns a string without those brackets. Otherwise returns the class
256 name in a string. This is used by some print method. Always returns
257 a string. */
258 ScmObj Scm_ShortClassName(ScmClass *klass)
259 {
260 ScmObj name = klass->name;
261
262 if (SCM_SYMBOLP(name)) {
263 const ScmStringBody *b = SCM_STRING_BODY(SCM_SYMBOL_NAME(name));
264 int size;
265 if (((size = SCM_STRING_BODY_SIZE(b)) > 2)
266 && SCM_STRING_BODY_START(b)[0] == '<'
267 && SCM_STRING_BODY_START(b)[size-1] == '>') {
268 return Scm_Substring(SCM_SYMBOL_NAME(name), 1,
269 SCM_STRING_BODY_LENGTH(b)-1, FALSE);
270 } else {
271 return SCM_OBJ(SCM_SYMBOL_NAME(name));
272 }
273 }
274 /* Fallback. At this moment we don't have unnamed classes,
275 so this is an ad hoc code. We may need better handling
276 (like write-to-string) later. */
277 return SCM_MAKE_STR("(unnamed class)");
278 }
279
280 /*=====================================================================
281 * Class metaobject
282 */
283
284 /* One of the design goals of Gauche object system is to make Scheme-defined
285 * class easily accessible from C code, and vice versa.
286 *
287 * Class is implemented in two layers; Scheme layer and C layer. The two
288 * layers work together to realize efficient MOP. In the following
289 * description, (FOOBAR baz) indicates Scheme call where FooBar(baz) indicates
290 * C call.
291 *
292 * Class instantiation is handled as follows.
293 *
294 * (MAKE class . initargs)
295 * If class is a descendant of <class> eventually this calls
296 * a method (MAKE <class> . initargs).
297 *
298 * (MAKE <class> . initargs)
299 * Defined in lib/gauche/object.scm. This calls
300 * (ALLOCATE-INSTANCE <class> initargs), then
301 * (INITIALIZE obj initargs).
302 *
303 * (ALLOCATE-INSTANCE <class> <list>)
304 * This is a C-defined method, and calls allocate() below.
305 *
306 * static ScmObj allocate(ScmNextMethod *, ScmObj *, int, void*)
307 * The default allocation dispatcher. This calls class->allocate().
308 * Some builtin function doesn't allow instantiation from Scheme and
309 * sets class->allocate() to NULL; an error is raised in such case.
310 *
311 * The class->allocate() function usually allocates the instance
312 * (Scm*** structure) and initializes its slots with reasonable values.
313 * For example, if class is <class>, class->allocate allocates
314 * ScmClass structure. If the class allows subclassing, class->allocate
315 * must allocate extra storage for as many slots as class->numInstanceSlots.
316 *
317 * The allocated and set up structure is returned as ScmObj, which
318 * eventually retured by (ALLOCATE-INSTANCE ...) method, and passed to
319 * (INITIALIZE obj initargs) structure.
320 *
321 * (INITIALIZE obj initargs)
322 * In most cases this method is defined in Scheme, if ever defined.
323 * The Scheme method does whatever it want, but it must call
324 * (NEXT-METHOD) in it, and it eventually calls the C-defined fallback
325 * method buildin_initialize().
326 *
327 * ScmObj builtin_initialize(ScmObj *, int, ScmGeneric*)
328 * This function traverses the slot accessors, and if the slot has
329 * not been initialized, initialize it as specified in initargs or
330 * slot options.
331 */
332
333 /* Defining builtin class in C.
334 *
335 * Defining classes in C is divided in two steps. First, you have to
336 * define the static part of the class; it is done by one of the
337 * SCM_DEFINE_***_CLASS macros provided in gauche.h, and it defines
338 * static instance of ScmClass structure. Then, in the initialization
339 * phase, you have to call Scm_InitStaticClass to initialize the dynamic
340 * part of the structure.
341 *
342 * void Scm_InitStaticClass(ScmClass *klass, const char *name,
343 * ScmModule *mod,
344 * ScmClassStaticSlotSpec *slots,
345 * int flags)
346 *
347 * This function fills the ScmClass structure that can't be
348 * defined statically, and inserts the binding from the named
349 * symbol to the class object in the specified module.
350 * The 'flags' arg is reserved for future use, and must be 0
351 * for the time being.
352 *
353 * See comments in gauche.h (around "Class categories") about
354 * the categories of C-defined classes.
355 */
356
357 /*
358 * Built-in protocols
359 *
360 * ScmObj klass->allocate(ScmClass *klass, ScmObj initargs)
361 * Called at the bottom of the chain of allocate-instance method.
362 * Besides allocating the required space, it must initialize
363 * members of the C-specific part of the instance, including SCM_HEADER.
364 * This protocol can be NULL for core base classes; if so, attempt
365 * to "make" such class reports an error.
366 *
367 * void klass->print(ScmObj obj, ScmPort *sink, ScmWriteContext *ctx)
368 * OBJ is an instance of klass (you can safely assume it). This
369 * function should print OBJ into SINK. See write.c about the
370 * details of the context.
371 * If this function pointer is not set, a default print method
372 * is used.
373 *
374 * int klass->compare(ScmObj x, ScmObj y, int equalp)
375 * X and Y are instances of klass. If equalp is FALSE,
376 * return -1, 0, or 1, when X < Y, X == Y or X > Y, respectively.
377 * In case if klass is not orderable, it can signal an error.
378 * If equalp is TRUE, just test the equality: return -1 if X != Y
379 * and 0 if X == Y.
380 *
381 * int klass->serialize(ScmObj obj, ScmPort *sink, ScmObj table)
382 * OBJ is an instance of klass. This method is only called when OBJ
383 * has not been output in the current serializing session.
384 */
385
386 /* A note on the 'data' member of ScmClass
387 *
388 * It can be used to hang an opaque data to a specific class. So far,
389 * we use it only for <simple> class mechanism. Its use is highly
390 * controversial; I mean, The Right Thing is to define a metaclass
391 * which defines an extra member, and allocate <simple> class as an
392 * instance of it. However, creating metaclass from C is messy now,
393 * so I chose to hack. In future we may have a nice C API to create
394 * a metaclass, and then we may remove this 'data' member. So DO NOT
395 * RELY ON ITS EXISTENCE.
396 */
397
398 /*
399 * Class metaobject protocol implementation
400 */
401
402 /* Allocate class structure. klass is a metaclass. */
403 static ScmObj class_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
404 {
405 /* NB: In some context, klass's initialization hasn't finished yet and
406 klass->coreSize is 0. In such case we can safely assume the class
407 is the same size as ScmClass. */
408 int classCoreSize = ((size_t)klass->coreSize < sizeof(ScmClass)
409 ? (int)sizeof(ScmClass)
410 : klass->coreSize);
411 ScmClass *instance = (ScmClass*)Scm_NewInstance(klass, classCoreSize);
412 instance->allocate = NULL; /* will be set when CPL is set */
413 instance->print = NULL;
414 instance->compare = Scm_ObjectCompare;
415 instance->hash = NULL;
416 instance->cpa = NULL;
417 instance->numInstanceSlots = 0; /* will be adjusted in class init */
418 instance->coreSize = 0; /* will be set when CPL is set. this is
419 core size of the instance, not the
420 classCoreSize above. */
421 instance->flags = SCM_CLASS_SCHEME|SCM_CLASS_MALLEABLE; /* default */
422 instance->name = SCM_FALSE;
423 instance->directSupers = SCM_NIL;
424 instance->accessors = SCM_NIL;
425 instance->cpl = SCM_NIL;
426 instance->directSlots = SCM_NIL;
427 instance->slots = SCM_NIL;
428 instance->directSubclasses = SCM_NIL;
429 instance->directMethods = SCM_NIL;
430 instance->initargs = SCM_NIL;
431 instance->modules = SCM_NIL;
432 instance->redefined = SCM_FALSE;
433 (void)SCM_INTERNAL_MUTEX_INIT(instance->mutex);
434 (void)SCM_INTERNAL_COND_INIT(instance->cv);
435 instance->data = NULL; /* see the above note on the 'data' member */
436 return SCM_OBJ(instance);
437 }
438
439 static void class_print(ScmObj obj, ScmPort *port,
440 ScmWriteContext *ctx SCM_UNUSED)
441 {
442 Scm_Printf(port, "#<class %A%s>",
443 SCM_CLASS(obj)->name,
444 (SCM_FALSEP(SCM_CLASS(obj)->redefined)? "" : " (redefined)"));
445 }
446
447 /*
448 * (make <class> ...) - default method to make a class instance.
449 */
450
451 /* defined in Scheme */
452
453 /*
454 * (allocate-instance <class> initargs)
455 */
456 ScmObj Scm_Allocate(ScmClass *c, ScmObj initargs)
457 {
458 if (c->allocate == NULL) {
459 Scm_Error("built-in class can't be allocated via allocate-instance: %S",
460 SCM_OBJ(c));
461 }
462 return c->allocate(c, initargs);
463 }
464
465 static ScmObj allocate(ScmNextMethod *nm SCM_UNUSED,
466 ScmObj *argv,
467 int argc SCM_UNUSED,
468 void *d SCM_UNUSED)
469 {
470 return Scm_Allocate(SCM_CLASS(argv[0]), argv[1]);
471 }
472
473 static ScmClass *class_allocate_SPEC[] = {
474 SCM_CLASS_STATIC_PTR(Scm_ClassClass), SCM_CLASS_STATIC_PTR(Scm_ListClass)
475 };
476 static SCM_DEFINE_METHOD(class_allocate_rec, &Scm_GenericAllocate,
477 2, 0, class_allocate_SPEC, allocate, NULL);
478
479 /*
480 * (compute-cpl <class>)
481 */
482 static ScmObj class_compute_cpl(ScmNextMethod *nm SCM_UNUSED,
483 ScmObj *argv,
484 int argc SCM_UNUSED,
485 void *d SCM_UNUSED)
486 {
487 ScmClass *c = SCM_CLASS(argv[0]);
488 return Scm_ComputeCPL(c);
489 }
490
491 static ScmClass *class_compute_cpl_SPEC[] = {
492 SCM_CLASS_STATIC_PTR(Scm_ClassClass)
493 };
494 static SCM_DEFINE_METHOD(class_compute_cpl_rec, &Scm_GenericComputeCPL,
495 1, 0, class_compute_cpl_SPEC,
496 class_compute_cpl, NULL);
497
498 /*
499 * (class-of obj)
500 */
501
502 ScmClass *Scm_ClassOf(ScmObj obj)
503 {
504 if (!SCM_PTRP(obj)) {
505 if (SCM_TRUEP(obj) || SCM_FALSEP(obj)) return SCM_CLASS_BOOL;
506 if (SCM_NULLP(obj)) return SCM_CLASS_NULL;
507 if (SCM_CHARP(obj)) return SCM_CLASS_CHAR;
508 if (SCM_INTP(obj)) return SCM_CLASS_INTEGER;
509 if (SCM_EOFP(obj)) return SCM_CLASS_EOF_OBJECT;
510 if (SCM_UNDEFINEDP(obj)) return SCM_CLASS_UNDEFINED_OBJECT;
511 if (SCM_STRING_CURSOR_SMALL_P(obj)) return SCM_CLASS_STRING_CURSOR;
512 else return SCM_CLASS_UNKNOWN;
513 }
514 if (SCM_FLONUMP(obj)) return SCM_CLASS_REAL;
515 /* check lazy pair first, so that we won't trigger forcing. */
516 if (SCM_LAZY_PAIR_P(obj)) return SCM_CLASS_PAIR;
517 if (SCM_PAIRP(obj)) {
518 ScmExtendedPairDescriptor *d = Scm__GetExtendedPairDescriptor(obj);
519 if (d) {
520 return d->klass;
521 } else {
522 return SCM_CLASS_PAIR;
523 }
524 }
525
526 return SCM_CLASS_OF(obj);
527 }
528
529 /* Returns the pointer of the first base class found in the given
530 class's CPA. If the class is pure abstract or builtin, NULL is
531 returned. */
532 ScmClass *Scm_BaseClassOf(ScmClass *klass)
533 {
534 ScmClass **cp = klass->cpa;
535 ScmClass *k;
536 while ((k = *cp++) != NULL) {
537 if (SCM_CLASS_CATEGORY(k) == SCM_CLASS_BASE) return k;
538 }
539 return NULL;
540 }
541
542 /*
543 * (class-of obj class)
544 * - if obj's class is redefined, first updates obj.
545 */
546 ScmObj class_of_cc(ScmObj result, void **data SCM_UNUSED)
547 {
548 return Scm_VMClassOf(result);
549 }
550
551 ScmObj Scm_VMClassOf(ScmObj obj)
552 {
553 ScmClass *k = Scm_ClassOf(obj);
554 if (!SCM_FALSEP(k->redefined)) {
555 Scm_VMPushCC(class_of_cc, NULL, 0);
556 return instance_class_redefinition(obj, k);
557 }
558 return SCM_OBJ(k);
559 }
560
561 /*
562 * (is-a? obj class)
563 * - if obj's class is redefined, first updates obj.
564 */
565 ScmObj is_a_cc(ScmObj result SCM_UNUSED, void **data)
566 {
567 return Scm_VMIsA(SCM_OBJ(data[0]), SCM_CLASS(data[1]));
568 }
569
570 ScmObj Scm_VMIsA(ScmObj obj, ScmClass *klass)
571 {
572 ScmClass *k = Scm_ClassOf(obj);
573 if (!SCM_FALSEP(k->redefined)) {
574 void *data[2];
575 data[0] = obj;
576 data[1] = klass;
577 Scm_VMPushCC(is_a_cc, data, 2);
578 return instance_class_redefinition(obj, k);
579 }
580 return SCM_MAKE_BOOL(Scm_TypeP(obj, klass));
581 }
582
583 /*
584 * Setting/resetting SCM_CLASS_MALLEABLE flag
585 */
586 void Scm_ClassMalleableSet(ScmClass *klass, int flag)
587 {
588 if (SCM_CLASS_CATEGORY(klass) != SCM_CLASS_SCHEME) {
589 Scm_Error("You cannot modify malleable flag of a class not defined in Scheme: %S", SCM_OBJ(klass));
590 }
591 if (flag) {
592 klass->flags |= SCM_CLASS_MALLEABLE;
593 } else {
594 klass->flags &= ~SCM_CLASS_MALLEABLE;
595 }
596 }
597
598 /*--------------------------------------------------------------
599 * Metainformation accessors
600 */
601
602 /* TODO: disable modification of system-builtin classes */
603
604 static ScmObj class_name(ScmClass *klass)
605 {
606 return klass->name;
607 }
608
609 static void class_name_set(ScmClass *klass, ScmObj val)
610 {
611 CHECK_MALLEABLE(klass, "(setter name)");
612 klass->name = val;
613 }
614
615 static ScmObj class_cpl(ScmClass *klass)
616 {
617 return klass->cpl;
618 }
619
620 /* Subroutine for class_cpl_set. Scans KLASS's CPL and find out the
621 suitable allocator function, C-struct core size, and some flags.
622 If KLASS inherits more than one C-defined classes (BASEs), they must
623 form a single inheritance chain. */
624 static void find_core_allocator(ScmClass *klass)
625 {
626 ScmClass *b = NULL; /* the base class klass gets the allocate func */
627 int object_inherited = FALSE;
628
629 klass->allocate = NULL;
630 for (ScmClass **p = klass->cpa; *p; p++) {
631 if (SCM_CLASS_CATEGORY(*p) == SCM_CLASS_BUILTIN) {
632 Scm_Error("class '%S' attempted to inherit from a builtin class "
633 "%S; you cannot subclass a builtin class.",
634 klass->name, *p);
635 }
636
637 if ((*p)->allocate == instance_allocate) {
638 /* Check if we certainly inherited <object> */
639 object_inherited = TRUE;
640 continue;
641 }
642
643 if ((*p)->flags & SCM_CLASS_APPLICABLE) {
644 klass->flags |= SCM_CLASS_APPLICABLE;
645 }
646
647 if (b
648 && SCM_CLASS_CATEGORY(*p) == SCM_CLASS_BASE
649 && b->allocate != (*p)->allocate) {
650 /* found different C-defined class. check to see if (*p) is
651 superclass of b. If not, the single-inheritance rule is
652 violated. */
653 ScmClass **bp = b->cpa;
654 for (; *bp; bp++) {
655 if (*bp == *p) break;
656 }
657 if (!*bp) {
658 Scm_Error("class '%S' attempted to inherit multiple C-defined "
659 "base class (%S and %S) which are not in a "
660 "superclass-subclass relathionship.",
661 klass->name, b, *p);
662 }
663 continue;
664 }
665 if (!b) {
666 /* Here we found the closest C-base class. Get the allocator
667 from it. */
668 b = *p;
669 klass->allocate = b->allocate;
670 klass->coreSize = b->coreSize;
671 }
672 }
673
674 if (!object_inherited) {
675 Scm_Error("class %S's precedence list doesn't have a base class: %S",
676 klass->name, klass->cpl);
677 }
678 if (!klass->allocate) {
679 klass->allocate = instance_allocate;
680 klass->coreSize = sizeof(ScmInstance);
681 }
682 }
683
684 static void class_cpl_set(ScmClass *klass, ScmObj val)
685 {
686 CHECK_MALLEABLE(klass, "(setter cpl)");
687
688 /* We make sure things are consistent. */
689 if (!SCM_PAIRP(val)) goto err;
690 /* check if the CPL begins with the class itself. */
691 if (SCM_CAR(val) != SCM_OBJ(klass)) goto err;
692
693 /* set up the cpa */
694 ScmObj cp = SCM_CDR(val);
695 int len = Scm_Length(cp);
696 if (len < 0) goto err;
697 klass->cpa = class_list_to_array(cp, len);
698 for (int i=0; i<len; i++) {
699 /* sanity check */
700 if (klass->cpa[i] == SCM_CLASS_BOTTOM) goto err;
701 }
702 if (klass->cpa[len-1] != SCM_CLASS_TOP) goto err;
703 klass->cpl = Scm_CopyList(val);
704 /* find correct allocation method */
705 find_core_allocator(klass);
706 return;
707 err:
708 Scm_Error("class precedence list must be a proper list of class "
709 "metaobject, beginning from the class itself owing the list, "
710 "and ending by the class <top>, and must not include <bottom>: "
711 "%S", val);
712 }
713
714 static ScmObj class_direct_supers(ScmClass *klass)
715 {
716 return klass->directSupers;
717 }
718
719 static void class_direct_supers_set(ScmClass *klass, ScmObj val)
720 {
721 CHECK_MALLEABLE(klass, "(setter direct-supers)")
722 ScmObj vp;
723 SCM_FOR_EACH(vp, val) {
724 if (!Scm_TypeP(SCM_CAR(vp), SCM_CLASS_CLASS))
725 Scm_Error("non-class object found in direct superclass list: %S",
726 SCM_CAR(vp));
727 }
728 klass->directSupers = val;
729 }
730
731 static ScmObj class_direct_slots(ScmClass *klass)
732 {
733 return klass->directSlots;
734 }
735
736 static void class_direct_slots_set(ScmClass *klass, ScmObj val)
737 {
738 CHECK_MALLEABLE(klass, "(setter direct-slots)");
739 ScmObj vp;
740 SCM_FOR_EACH(vp, val) {
741 if (!SCM_PAIRP(SCM_CAR(vp)))
742 Scm_Error("bad slot spec found in direct slot list: %S",
743 SCM_CAR(vp));
744 }
745 klass->directSlots = val;
746 }
747
748 static ScmObj class_slots_ref(ScmClass *klass)
749 {
750 return klass->slots;
751 }
752
753 static void class_slots_set(ScmClass *klass, ScmObj val)
754 {
755 CHECK_MALLEABLE(klass, "(setter slots)");
756 ScmObj vp;
757 SCM_FOR_EACH(vp, val) {
758 if (!SCM_PAIRP(SCM_CAR(vp)))
759 Scm_Error("bad slot spec found in slot list: %S",
760 SCM_CAR(vp));
761 }
762 klass->slots = val;
763 }
764
765 static ScmObj class_accessors(ScmClass *klass)
766 {
767 return klass->accessors;
768 }
769
770 static void class_accessors_set(ScmClass *klass, ScmObj val)
771 {
772 CHECK_MALLEABLE(klass, "(setter accessors)");
773 ScmObj vp;
774 SCM_FOR_EACH(vp, val) {
775 if (!SCM_PAIRP(SCM_CAR(vp))
776 || !SCM_SLOT_ACCESSOR_P(SCM_CDAR(vp)))
777 Scm_Error("slot accessor list must be an assoc-list of slot name and slot accessor object, but found: %S",
778 SCM_CAR(vp));
779 }
780 klass->accessors = val;
781 }
782
783 static ScmObj class_numislots(ScmClass *klass)
784 {
785 return Scm_MakeInteger(klass->numInstanceSlots);
786 }
787
788 static void class_numislots_set(ScmClass *klass, ScmObj snf)
789 {
790 CHECK_MALLEABLE(klass, "(setter num-instance-slots)");
791 int nf = 0;
792 if (!SCM_INTP(snf) || (nf = SCM_INT_VALUE(snf)) < 0) {
793 Scm_Error("invalid argument: %S", snf);
794 /*NOTREACHED*/
795 }
796 klass->numInstanceSlots = nf;
797 }
798
799 static ScmObj class_category(ScmClass *klass)
800 {
801 switch (SCM_CLASS_CATEGORY(klass)) {
802 case SCM_CLASS_BUILTIN: return SCM_SYM_BUILTIN;
803 case SCM_CLASS_ABSTRACT: return SCM_SYM_ABSTRACT;
804 case SCM_CLASS_BASE: return SCM_SYM_BASE;
805 default: return SCM_SYM_SCHEME;
806 }
807 }
808
809 static ScmObj class_core_size(ScmClass *klass)
810 {
811 return SCM_MAKE_INT(klass->coreSize);
812 }
813
814 static ScmObj class_initargs(ScmClass *klass)
815 {
816 return klass->initargs;
817 }
818
819 static void class_initargs_set(ScmClass *klass, ScmObj val)
820 {
821 CHECK_MALLEABLE(klass, "(setter initargs)");
822 int len = Scm_Length(val);
823 if (len < 0 || len%2 != 0) {
824 Scm_Error("class-initargs must be a list of even number of elements, but got %S", val);
825 }
826 klass->initargs = val;
827 }
828
829 static ScmObj class_defined_modules(ScmClass *klass)
830 {
831 return klass->modules;
832 }
833
834 static void class_defined_modules_set(ScmClass *klass, ScmObj val)
835 {
836 CHECK_MALLEABLE(klass, "(setter defined-modules)");
837 ScmObj cp;
838 SCM_FOR_EACH(cp, val) {
839 if (!SCM_MODULEP(SCM_CAR(cp))) goto err;
840 }
841 if (!SCM_NULLP(cp)) goto err;
842 klass->modules = val;
843 return;
844 err:
845 Scm_Error("list of modules required, bot got %S", val);
846 }
847
848 /*
849 * The following slots should only be modified by a special MT-safe procedures.
850 */
851 static ScmObj class_direct_subclasses(ScmClass *klass)
852 {
853 return klass->directSubclasses;
854 }
855
856 static ScmObj class_direct_methods(ScmClass *klass)
857 {
858 return klass->directMethods;
859 }
860
861 static ScmObj class_redefined(ScmClass *klass)
862 {
863 int abandoned = FALSE;
864
865 /* If this class is being redefined by other thread, you should wait */
866 (void)SCM_INTERNAL_MUTEX_LOCK(klass->mutex);
867 while (SCM_VMP(klass->redefined)) {
868 if (SCM_VM(klass->redefined)->state == SCM_VM_TERMINATED) {
869 /* TODO: this means redefinition of klass has been abandoned,
870 so the state of klass may be inconsistent. Should we do
871 something to it? */
872 abandoned = TRUE;
873 klass->redefined = SCM_FALSE;
874 } else {
875 (void)SCM_INTERNAL_COND_WAIT(klass->cv, klass->mutex);
876 }
877 }
878 ScmObj r = klass->redefined;
879 (void)SCM_INTERNAL_MUTEX_UNLOCK(klass->mutex);
880 if (abandoned) {
881 Scm_Warn("redefinition of class %S has been abandoned", klass);
882 }
883 return r;
884 }
885
886 /*--------------------------------------------------------------
887 * Implicit metaclass
888 */
889 /* This function does the equivalent to
890 * (make <class> :name NAME :supers (list <class>))
891 */
892
893 static ScmClass *make_implicit_meta(const char *name,
894 ScmClass **cpa,
895 ScmModule *mod)
896 {
897 ScmClass *meta = (ScmClass*)class_allocate(SCM_CLASS_CLASS, SCM_NIL);
898 ScmObj s = SCM_INTERN(name);
899 ScmClass **metas = SCM_CLASS_METACLASS_CPL;
900
901 /* check to see if parent class has also metaclass, and if so,
902 adds it to the CPA. We know all the builtin classes use
903 single inheritance, so the CPA calculation should be straightforward.
904 Note that this assumes the parent classes are already initialized.
905 */
906 {
907 ScmClass **parent;
908 int numExtraMetas = 0;
909 for (parent = cpa; *parent; parent++) {
910 if (SCM_CLASS_OF(*parent) != SCM_CLASS_CLASS) {
911 numExtraMetas++;
912 }
913 }
914 if (numExtraMetas) {
915 size_t newmetasize
916 = numExtraMetas + (sizeof(Scm_MetaclassCPL)/sizeof(ScmClass*));
917 metas = SCM_NEW_ARRAY(ScmClass*, newmetasize);
918 size_t i = 0;
919 for (parent = cpa; *parent; parent++) {
920 if (SCM_CLASS_OF(*parent) != SCM_CLASS_CLASS) {
921 metas[i++] = SCM_CLASS_OF(*parent);
922 }
923 }
924 for (size_t j = 0; i < newmetasize; i++, j++) {
925 metas[i] = Scm_MetaclassCPL[j];
926 }
927 }
928 }
929
930 meta->name = s;
931 meta->allocate = class_allocate;
932 meta->print = class_print;
933 meta->cpa = metas;
934 meta->flags = SCM_CLASS_ABSTRACT;
935 initialize_builtin_cpl(meta, SCM_FALSE);
936 Scm_Define(mod, SCM_SYMBOL(s), SCM_OBJ(meta));
937 meta->slots = Scm_ClassClass.slots;
938 meta->accessors = Scm_ClassClass.accessors;
939 meta->modules = SCM_LIST1(SCM_OBJ(mod));
940 return meta;
941 }
942
943 /*--------------------------------------------------------------
944 * External interface
945 */
946
947 #if GAUCHE_API_VERSION >= 98
948 int Scm_SubclassP(ScmClass *sub, ScmClass *type)
949 #else /*GAUCHE_API_VERSION < 98*/
950 int Scm_SubtypeP(ScmClass *sub, ScmClass *type)
951 #endif /*GAUCHE_API_VERSION < 98*/
952 {
953 if (sub == type) return TRUE;
954 if (sub == SCM_CLASS_BOTTOM) return TRUE;
955
956 ScmClass **p = sub->cpa;
957 while (*p) {
958 if (*p++ == type) return TRUE;
959 }
960 return FALSE;
961 }
962
963 int Scm_TypeP(ScmObj obj, ScmClass *type)
964 {
965 return Scm_SubclassP(Scm_ClassOf(obj), type);
966 }
967
968 /*
969 * compute-cpl
970 */
971 ScmObj Scm_ComputeCPL(ScmClass *klass)
972 {
973 ScmObj seqh = SCM_NIL, seqt = SCM_NIL;
974
975 /* a trick to ensure we have <object> <top> at the end of CPL. */
976 ScmObj ds = Scm_Delete(SCM_OBJ(SCM_CLASS_OBJECT), klass->directSupers,
977 SCM_CMP_EQ);
978 ds = Scm_Delete(SCM_OBJ(SCM_CLASS_TOP), ds, SCM_CMP_EQ);
979 ds = Scm_Append2(ds, SCM_LIST1(SCM_OBJ(SCM_CLASS_OBJECT)));
980
981 ScmObj dp;
982 SCM_FOR_EACH(dp, klass->directSupers) {
983 if (!Scm_TypeP(SCM_CAR(dp), SCM_CLASS_CLASS))
984 Scm_Error("non-class found in direct superclass list: %S",
985 klass->directSupers);
986 if (SCM_CAR(dp) == SCM_OBJ(SCM_CLASS_OBJECT)
987 || SCM_CAR(dp) == SCM_OBJ(SCM_CLASS_TOP))
988 continue;
989 SCM_APPEND1(seqh, seqt, SCM_CLASS(SCM_CAR(dp))->cpl);
990 }
991 SCM_APPEND1(seqh, seqt, Scm_ObjectClass.cpl);
992
993 SCM_APPEND1(seqh, seqt, ds);
994
995 ScmObj result = Scm_MonotonicMerge1(seqh);
996 if (SCM_FALSEP(result))
997 Scm_Error("discrepancy found in class precedence lists of the superclasses: %S",
998 klass->directSupers);
999 return Scm_Cons(SCM_OBJ(klass), result);
1000 }
1001
1002 /*
1003 * Internal procedures for class redefinition
1004 */
1005
1006 /* global lock manipulation */
1007 static void lock_class_redefinition(ScmVM *vm)
1008 {
1009 ScmVM *stolefrom = NULL;
1010 if (class_redefinition_lock.owner == vm) {
1011 class_redefinition_lock.count++;
1012 } else {
1013 (void)SCM_INTERNAL_MUTEX_LOCK(class_redefinition_lock.mutex);
1014 while (class_redefinition_lock.owner != vm) {
1015 if (class_redefinition_lock.owner == NULL) {
1016 class_redefinition_lock.owner = vm;
1017 } else if (class_redefinition_lock.owner->state
1018 == SCM_VM_TERMINATED) {
1019 stolefrom = class_redefinition_lock.owner;
1020 class_redefinition_lock.owner = vm;
1021 } else {
1022 (void)SCM_INTERNAL_COND_WAIT(class_redefinition_lock.cv,
1023 class_redefinition_lock.mutex);
1024 }
1025 }
1026 (void)SCM_INTERNAL_MUTEX_UNLOCK(class_redefinition_lock.mutex);
1027 if (stolefrom) {
1028 Scm_Warn("a thread holding class redefinition lock has been terminated: %S", stolefrom);
1029 }
1030 class_redefinition_lock.count = 1;
1031 }
1032 }
1033
1034 static void unlock_class_redefinition(ScmVM *vm)
1035 {
1036 if (class_redefinition_lock.owner != vm) return;
1037 if (--class_redefinition_lock.count <= 0) {
1038 (void)SCM_INTERNAL_MUTEX_LOCK(class_redefinition_lock.mutex);
1039 (void)SCM_INTERNAL_COND_BROADCAST(class_redefinition_lock.cv);
1040 class_redefinition_lock.owner = NULL;
1041 (void)SCM_INTERNAL_MUTEX_UNLOCK(class_redefinition_lock.mutex);
1042 }
1043 }
1044
1045 /* %start-class-redefinition klass */
1046 void Scm_StartClassRedefinition(ScmClass *klass)
1047 {
1048 if (SCM_CLASS_CATEGORY(klass) != SCM_CLASS_SCHEME) {
1049 Scm_Error("cannot redefine class %S, which is not a Scheme-defined class", klass);
1050 }
1051 ScmVM *vm = Scm_VM();
1052
1053 /* First, acquire the global lock. */
1054 lock_class_redefinition(vm);
1055
1056 /* Mark this class to be redefined. */
1057 int success = FALSE;
1058 (void)SCM_INTERNAL_MUTEX_LOCK(klass->mutex);
1059 if (SCM_FALSEP(klass->redefined)) {
1060 klass->redefined = SCM_OBJ(vm);
1061 success = TRUE;
1062 }
1063 (void)SCM_INTERNAL_MUTEX_UNLOCK(klass->mutex);
1064
1065 if (!success) {
1066 unlock_class_redefinition(vm);
1067 Scm_Error("class %S seems abandoned during class redefinition", klass);
1068 }
1069
1070 /* Allow modification of important slots */
1071 Scm_ClassMalleableSet(klass, TRUE);
1072 }
1073
1074 /* %commit-class-redefinition klass newklass */
1075 void Scm_CommitClassRedefinition(ScmClass *klass, ScmObj newklass)
1076 {
1077 if (SCM_CLASS_CATEGORY(klass) != SCM_CLASS_SCHEME) return;
1078 if (!SCM_FALSEP(newklass)&&!SCM_CLASSP(newklass)) {
1079 Scm_Error("class or #f required, but got %S", newklass);
1080 }
1081
1082 ScmVM *vm = Scm_VM();
1083
1084 /* Release the lock of the class.
1085 We execute this regardless of class_redefinition_lock.owner.
1086 Theoretically, this procedure shouldn't be called unless the thread
1087 owns global class_redefinition_lock. However, we don't require it,
1088 so that this procedure can be used for a program to exit from
1089 obscure state. */
1090 (void)SCM_INTERNAL_MUTEX_LOCK(klass->mutex);
1091 if (SCM_EQ(klass->redefined, SCM_OBJ(vm))) {
1092 Scm_ClassMalleableSet(klass, FALSE); /* disable modification */
1093 klass->redefined = newklass;
1094 (void)SCM_INTERNAL_COND_BROADCAST(klass->cv);
1095 }
1096 (void)SCM_INTERNAL_MUTEX_UNLOCK(klass->mutex);
1097
1098 /* Decrement the recursive global lock. */
1099 unlock_class_redefinition(vm);
1100 }
1101
1102 /* %check-class-binding name module
1103 See the bindings of name in module, and iff it is bound to a class,
1104 returns the class; otherwise returns #f. */
1105 ScmObj Scm_CheckClassBinding(ScmObj name, ScmModule *module)
1106 {
1107 if (!SCM_SYMBOLP(name)) return SCM_FALSE;
1108 ScmObj v = Scm_GlobalVariableRef(module, SCM_SYMBOL(name), 0);
1109 return SCM_CLASSP(v) ? v : SCM_FALSE;
1110 }
1111
1112 /* %replace-class-binding! klass newklass
1113 Called when a descendant of klass is redefined. If klass has a global
1114 binding, replace it to newklass. */
1115 void Scm_ReplaceClassBinding(ScmClass *klass, ScmClass *newklass)
1116 {
1117 if (!SCM_SYMBOLP(klass->name)) return;
1118 ScmObj cp;
1119 SCM_FOR_EACH(cp, klass->modules) {
1120 if (!SCM_MODULEP(SCM_CAR(cp))) continue;
1121 Scm_MakeBinding(SCM_MODULE(SCM_CAR(cp)),
1122 SCM_SYMBOL(klass->name),
1123 SCM_OBJ(newklass),
1124 SCM_BINDING_INLINABLE);
1125 }
1126 }
1127
1128 /* %add-direct-subclass! super sub */
1129 void Scm_AddDirectSubclass(ScmClass *super, ScmClass *sub)
1130 {
1131 if (SCM_CLASS_CATEGORY(super) == SCM_CLASS_SCHEME) {
1132 ScmObj p = Scm_Cons(SCM_OBJ(sub), SCM_NIL);
1133 (void)SCM_INTERNAL_MUTEX_LOCK(super->mutex);
1134 /* avoid duplication */
1135 if (SCM_FALSEP(Scm_Memq(super->directSubclasses, SCM_OBJ(sub)))) {
1136 SCM_SET_CDR_UNCHECKED(p, super->directSubclasses);
1137 super->directSubclasses = p;
1138 }
1139 (void)SCM_INTERNAL_MUTEX_UNLOCK(super->mutex);
1140 }
1141 }
1142
1143 /* %delete-direct-subclass! super sub */
1144 void Scm_DeleteDirectSubclass(ScmClass *super, ScmClass *sub)
1145 {
1146 if (SCM_CLASS_CATEGORY(super) == SCM_CLASS_SCHEME) {
1147 (void)SCM_INTERNAL_MUTEX_LOCK(super->mutex);
1148 super->directSubclasses =
1149 Scm_DeleteX(SCM_OBJ(sub), super->directSubclasses, SCM_CMP_EQ);
1150 (void)SCM_INTERNAL_MUTEX_UNLOCK(super->mutex);
1151 }
1152 }
1153
1154 /* %add-direct-method! super method
1155 method can be added or deleted freely, so we don't check malleablility. */
1156 void Scm_AddDirectMethod(ScmClass *super, ScmMethod *m)
1157 {
1158 if (SCM_CLASS_CATEGORY(super) == SCM_CLASS_SCHEME) {
1159 ScmObj p = Scm_Cons(SCM_OBJ(m), SCM_NIL);
1160 (void)SCM_INTERNAL_MUTEX_LOCK(super->mutex);
1161 /* avoid duplication */
1162 if (SCM_FALSEP(Scm_Memq(super->directMethods, SCM_OBJ(m)))) {
1163 SCM_SET_CDR_UNCHECKED(p, super->directMethods);
1164 super->directMethods = p;
1165 }
1166 (void)SCM_INTERNAL_MUTEX_UNLOCK(super->mutex);
1167 }
1168 }
1169
1170 /* %delete-direct-method! super method
1171 method can be added or deleted freely, so we don't check malleablility. */
1172 void Scm_DeleteDirectMethod(ScmClass *super, ScmMethod *m)
1173 {
1174 if (SCM_CLASS_CATEGORY(super) == SCM_CLASS_SCHEME) {
1175 (void)SCM_INTERNAL_MUTEX_LOCK(super->mutex);
1176 super->directMethods =
1177 Scm_DeleteX(SCM_OBJ(m), super->directMethods, SCM_CMP_EQ);
1178 (void)SCM_INTERNAL_MUTEX_UNLOCK(super->mutex);
1179 }
1180 }
1181
1182 /* %transplant-instance! src dst */
1183 /* Copies the contents of the core structure pointed by src over
1184 the contents of dst. The contents of dst is destroyed. This
1185 astonishingly dangerous operation has to be done at the last stage
1186 of change-class, in order to keep the identity of the instance
1187 being updated.
1188
1189 Note that this procedure doesn't overwrite the Scheme slot
1190 vectors. */
1191 void Scm_TransplantInstance(ScmObj src, ScmObj dst)
1192 {
1193 ScmClass *srcklass = Scm_ClassOf(src);
1194 ScmClass *dstklass = Scm_ClassOf(dst);
1195 ScmClass *base;
1196
1197 /* Extra check. We can't transplant the contents to different
1198 an instance that has different base class. */
1199 if ((base = Scm_BaseClassOf(srcklass)) == NULL
1200 || !SCM_EQ(base, Scm_BaseClassOf(dstklass))) {
1201 Scm_Error("%%transplant-instance: classes are incompatible between %S and %S",
1202 src, dst);
1203 }
1204 if (base->coreSize < (int)sizeof(ScmInstance)) {
1205 Scm_Error("%%transplant-instance: baseclass is too small (implementation error?)");
1206 }
1207 memcpy(dst, src, base->coreSize);
1208 }
1209
1210 /* touch-instance! obj
1211 * If obj's class is redefined, update obj. Otherwise it does nothing.
1212 * Handy to ensure obj is in the newest state. Returns obj.
1213 */
1214 ScmObj Scm_VMTouchInstance(ScmObj obj)
1215 {
1216 ScmClass *klass = Scm_ClassOf(obj);
1217 if (!SCM_FALSEP(klass->redefined)) {
1218 return instance_class_redefinition(obj, klass);
1219 }
1220 return obj;
1221 }
1222
1223 /*=====================================================================
1224 * Scheme slot access
1225 */
1226
1227 /* Scheme slots are stored in ScmObj array pointed by slots field
1228 * of ScmInstance. This one-level indirection allows an instance
1229 * to be redefined.
1230 */
1231
1232 /* Unbound slot: if the slot value yields either SCM_UNBOUND or
1233 * SCM_UNDEFINED, a generic function slot-unbound is called.
1234 * We count SCM_UNDEFINED as unbound so that a Scheme program can
1235 * make slot unbound, especially needed for procedural slots.
1236 */
1237
1238 /* A common routine to be used to allocate object.
1239 Coresize should be a size of base C structure in bytes.
1240 Klass may be a subclass. If klass is inheritable by Scheme
1241 (i.e. it's category is either SCM_CLASS_BASE or SCM_CLASS_SCHEME),
1242 This routine also allocates a slot vector, and initializes the
1243 slot vector with SCM_UNBOUND.
1244 We don't care class redefinition at this point. If the class is
1245 redefined simultaneously, it will be handled by the subsequent initialize
1246 method.
1247 */
1248 ScmObj Scm_NewInstance(ScmClass *klass, int coresize)
1249 {
1250 ScmObj obj = SCM_NEW2(ScmObj, coresize);
1251
1252 if (SCM_CLASS_CATEGORY(klass) == SCM_CLASS_BASE
1253 || SCM_CLASS_CATEGORY(klass) == SCM_CLASS_SCHEME) {
1254 ScmObj *slots = SCM_NEW_ARRAY(ScmObj, klass->numInstanceSlots);
1255
1256 /* NB: actually, for Scheme instances, 'coresize' argument is
1257 redundant since klass->coreSize has it. There's a historical
1258 confusion in the class protocol. We should clear it out someday.
1259 */
1260 if (coresize != klass->coreSize) {
1261 Scm_Printf(SCM_CURERR, "WARNING: allocating instance of class %S: coresize argument %d doesn't match the class definition's (%d)\n", klass, coresize, klass->coreSize);
1262 }
1263
1264 for (int i=0; i<klass->numInstanceSlots; i++) {
1265 slots[i] = SCM_UNBOUND;
1266 }
1267 SCM_INSTANCE(obj)->slots = slots;
1268 }
1269 SCM_SET_CLASS(obj, klass);
1270 return obj;
1271 }
1272
1273 /* A special procedure that shortcuts allocate-instance and initialize
1274 * slots directly.
1275 * This is mainly used for fast construction of records. This bypasses
1276 * normal MOP initialization steps, so this shouldn't be used casually.
1277 */
1278 ScmObj Scm__AllocateAndInitializeInstance(ScmClass *klass,
1279 ScmObj *inits, int numInits,
1280 u_long flags SCM_UNUSED /*reserved*/)
1281 {
1282 if (SCM_CLASS_CATEGORY(klass) != SCM_CLASS_BASE
1283 && SCM_CLASS_CATEGORY(klass) != SCM_CLASS_SCHEME) {
1284 Scm_Error("Scm_AllocateAndInitializeInstance can't be called on "
1285 "this class: %S", SCM_OBJ(klass));
1286 }
1287
1288 /* We allocate an instance and a slot vector at once for speed.
1289 It is reasonable optimization, since record classes won't be redefined,
1290 and the instances' slot vector will never be replaced.
1291 We may provide an another mode to allocate the slot vector separately,
1292 using FLAGS argument. */
1293 int corewords = (klass->coreSize + sizeof(ScmObj)-1)/sizeof(ScmObj);
1294 ScmObj obj = SCM_NEW2(ScmObj, (corewords+klass->numInstanceSlots)*sizeof(ScmObj));
1295 SCM_SET_CLASS(obj, klass);
1296 ScmObj *slots = ((ScmObj*)obj) + corewords;
1297 for (int i=0; i<klass->numInstanceSlots; i++) {
1298 if (i < numInits) slots[i] = inits[i];
1299 else slots[i] = SCM_UNBOUND;
1300 }
1301 SCM_INSTANCE(obj)->slots = slots;
1302 return obj;
1303 }
1304
1305 /* Invoke class redefinition method */
1306 static ScmObj instance_class_redefinition(ScmObj obj, ScmClass *old)
1307 {
1308 (void)SCM_INTERNAL_MUTEX_LOCK(old->mutex);
1309 while (!SCM_ISA(old->redefined, SCM_CLASS_CLASS)) {
1310 (void)SCM_INTERNAL_COND_WAIT(old->cv, old->mutex);
1311 }
1312 ScmObj newc = old->redefined;
1313 (void)SCM_INTERNAL_MUTEX_UNLOCK(old->mutex);
1314 if (SCM_CLASSP(newc)) {
1315 return Scm_VMApply2(SCM_OBJ(&Scm_GenericChangeClass), obj, newc);
1316 } else {
1317 return SCM_OBJ(old);
1318 }
1319 }
1320
1321 /* most primitive internal accessor */
1322 static inline ScmObj scheme_slot_ref(ScmObj obj, ScmSmallInt number)
1323 {
1324 ScmClass *k = Scm_ClassOf(obj);
1325 if (number < 0 || number >= k->numInstanceSlots)
1326 Scm_Error("instance slot index %ld out of bounds for %S", number, obj);
1327 return SCM_INSTANCE_SLOTS(obj)[number];
1328 }
1329
1330 static inline void scheme_slot_set(ScmObj obj, ScmSmallInt number, ScmObj val)
1331 {
1332 ScmClass *k = Scm_ClassOf(obj);
1333 if (number < 0 || number >= k->numInstanceSlots)
1334 Scm_Error("instance slot index %ld out of bounds for %S", number, obj);
1335 SCM_INSTANCE_SLOTS(obj)[number] = val;
1336 }
1337
1338 /* These three are exposed to Scheme to do some nasty things.
1339 We shouldn't do class redefinition check here, since the slot number
1340 is calculated based on the old class, if the class is ever redefined.
1341 */
1342 ScmObj Scm_InstanceSlotRef(ScmObj obj, ScmSmallInt number, ScmObj fallback)
1343 {
1344 ScmObj v = scheme_slot_ref(obj, number);
1345 if (SCM_UNBOUNDP(v)) {
1346 if (SCM_UNBOUNDP(fallback)) {
1347 Scm_Error("Slot #%d of object of class %S is unbound.",
1348 number, SCM_OBJ(Scm_ClassOf(obj)));
1349 }
1350 return fallback;
1351 }
1352 return v;
1353 }
1354
1355 void Scm_InstanceSlotSet(ScmObj obj, ScmSmallInt number, ScmObj val)
1356 {
1357 scheme_slot_set(obj, number, val);
1358 }
1359
1360 /* Initialize a slot according to its accessor spec
1361 TODO: class redefintion check
1362 */
1363 static ScmObj slot_initialize_cc(ScmObj result, void **data)
1364 {
1365 ScmObj obj = data[0];
1366 ScmSlotAccessor *sa = SCM_SLOT_ACCESSOR(data[1]);
1367 return slot_set_using_accessor(obj, sa, result);
1368 }
1369
1370 ScmObj Scm_VMSlotInitializeUsingAccessor(ScmObj obj,
1371 ScmSlotAccessor *sa,
1372 ScmObj initargs)
1373 {
1374 /* (1) see if we have init-keyword */
1375 if (SCM_KEYWORDP(sa->initKeyword)) {
1376 ScmObj v = Scm_GetKeyword(sa->initKeyword, initargs, SCM_UNDEFINED);
1377 if (!SCM_UNDEFINEDP(v)) {
1378 return slot_set_using_accessor(obj, sa, v);
1379 }
1380 }
1381 /* (2) use init-value or init-thunk, if this slot is initializable. */
1382 if (sa->initializable) {
1383 if (!SCM_UNBOUNDP(sa->initValue)) {
1384 return slot_set_using_accessor(obj, sa, sa->initValue);
1385 }
1386 if (SCM_PROCEDUREP(sa->initThunk)) {
1387 void *data[2];
1388 data[0] = (void*)obj;
1389 data[1] = (void*)sa;
1390 Scm_VMPushCC(slot_initialize_cc, data, 2);
1391 return Scm_VMApply(sa->initThunk, SCM_NIL);
1392 }
1393 }
1394 return SCM_UNDEFINED;
1395 }
1396
1397 /*-------------------------------------------------------------------
1398 * slot-ref, slot-set! and families
1399 */
1400
1401 /* helper macros */
1402 #define SLOT_UNBOUND(klass, obj, slot) \
1403 Scm_VMApply(SCM_OBJ(&Scm_GenericSlotUnbound), \
1404 SCM_LIST3(SCM_OBJ(klass), obj, slot))
1405
1406 #define SLOT_MISSING3(klass, obj, slot) \
1407 Scm_VMApply(SCM_OBJ(&Scm_GenericSlotMissing), \
1408 SCM_LIST3(SCM_OBJ(klass), obj, slot))
1409
1410 #define SLOT_MISSING4(klass, obj, slot, val) \
1411 Scm_VMApply(SCM_OBJ(&Scm_GenericSlotMissing), \
1412 SCM_LIST4(SCM_OBJ(klass), obj, slot, val))
1413
1414 /* GET-SLOT-ACCESSOR
1415 *
1416 * (define (get-slot-accessor class slot)
1417 * (cond ((assq slot (ref class 'accessors)) => cdr)
1418 * (else (error !!!))))
1419 */
1420 ScmSlotAccessor *Scm_GetSlotAccessor(ScmClass *klass, ScmObj slot)
1421 {
1422 ScmObj p = Scm_Assq(slot, klass->accessors);
1423 if (!SCM_PAIRP(p)) return NULL;
1424 if (!SCM_XTYPEP(SCM_CDR(p), SCM_CLASS_SLOT_ACCESSOR))
1425 Scm_Error("slot accessor information of class %S, slot %S is screwed up.",
1426 SCM_OBJ(klass), slot);
1427 return SCM_SLOT_ACCESSOR(SCM_CDR(p));
1428 }
1429
1430 /* (internal) slot-ref-using-accessor
1431 *
1432 * - assumes accessor belongs to the proper class.
1433 * - no class redefinition check is done
1434 */
1435 static ScmObj slot_ref_using_accessor_cc(ScmObj result, void **data)
1436 {
1437 ScmObj obj = data[0];
1438 ScmObj slot = data[1];
1439 int boundp = (data[2] != NULL);
1440
1441 if (SCM_UNBOUNDP(result) || SCM_UNDEFINEDP(result)) {
1442 if (boundp) {
1443 return SCM_FALSE;
1444 } else {
1445 return SLOT_UNBOUND(Scm_ClassOf(obj), obj, slot);
1446 }
1447 } else {
1448 if (boundp) return SCM_TRUE;
1449 else return result;
1450 }
1451 }
1452
1453 static ScmObj slot_boundp_using_accessor_cc(ScmObj result,
1454 void **data SCM_UNUSED)
1455 {
1456 return SCM_FALSEP(result)? SCM_FALSE:SCM_TRUE;
1457 }
1458
1459 static ScmObj slot_ref_using_accessor(ScmObj obj,
1460 ScmSlotAccessor *sa,
1461 int boundp)
1462 {
1463 ScmObj val = SCM_UNBOUND;
1464 if (sa->getter) {
1465 val = sa->getter(obj);
1466 } else if (sa->slotNumber >= 0) {
1467 val = scheme_slot_ref(obj, sa->slotNumber);
1468 } else if (boundp && SCM_PROCEDUREP(sa->schemeBoundp)) {
1469 void *data[3];
1470 data[0] = obj;
1471 data[1] = sa->name;
1472 data[2] = (void*)(intptr_t)boundp;
1473 Scm_VMPushCC(slot_boundp_using_accessor_cc, data, 3);
1474 return Scm_VMApply(sa->schemeBoundp, SCM_LIST1(obj));
1475 } else if (SCM_PROCEDUREP(sa->schemeGetter)) {
1476 void *data[3];
1477 data[0] = obj;
1478 data[1] = sa->name;
1479 data[2] = (void*)(intptr_t)boundp;
1480 Scm_VMPushCC(slot_ref_using_accessor_cc, data, 3);
1481 return Scm_VMApply(sa->schemeGetter, SCM_LIST1(obj));
1482 } else {
1483 Scm_Error("don't know how to retrieve value of slot %S of object %S (MOP error?)",
1484 sa->name, obj);
1485 }
1486 if (boundp) {
1487 return SCM_MAKE_BOOL(!(SCM_UNBOUNDP(val) || SCM_UNDEFINEDP(val)));
1488 } else {
1489 if (SCM_UNBOUNDP(val) || SCM_UNDEFINEDP(val)) {
1490 return SLOT_UNBOUND(Scm_ClassOf(obj), obj, sa->name);
1491 } else {
1492 return val;
1493 }
1494 }
1495 }
1496
1497 /* SLOT-REF
1498 *
1499 *(define (slot-ref obj slot bound-check?)
1500 * (%check-class-redefined (class-of obj))
1501 * (let ((sa (get-slot-accessor (class-of obj) slot)))
1502 * (if sa
1503 * (%internal-slot-ref-using-accessor obj sa bound-check?)
1504 * (slot-missing (class-of obj) obj slot))))
1505 */
1506 static ScmObj slot_ref_cc(ScmObj result SCM_UNUSED, void **data)
1507 {
1508 return Scm_VMSlotRef(SCM_OBJ(data[0]), SCM_OBJ(data[1]), (int)(intptr_t)data[2]);
1509 }
1510
1511 ScmObj Scm_VMSlotRef(ScmObj obj, ScmObj slot, int boundp)
1512 {
1513 ScmClass *klass = Scm_ClassOf(obj);
1514
1515 if (!SCM_FALSEP(klass->redefined)) {
1516 void *data[3];
1517 data[0] = obj;
1518 data[1] = slot;
1519 data[2] = (void*)(intptr_t)boundp;
1520 Scm_VMPushCC(slot_ref_cc, data, 3);
1521 return instance_class_redefinition(obj, klass);
1522 }
1523 ScmSlotAccessor *sa = Scm_GetSlotAccessor(klass, slot);
1524 if (sa == NULL) return SLOT_MISSING3(klass, obj, slot);
1525 else return slot_ref_using_accessor(obj, sa, boundp);
1526 }
1527
1528 /* SLOT-REF-USING-ACCESSOR
1529 *
1530 * (define (slot-ref-using-accessor obj sa bound-check?)
1531 * (%check-if-sa-is-valid-for-object obj sa)
1532 * (%internal-slot-ref-using-accessor obj sa bound-check?))
1533 *
1534 * - no class redefinition check is performed. if obj isn't updated
1535 * for the new class, sa must come from the old class.
1536 */
1537 ScmObj Scm_VMSlotRefUsingAccessor(ScmObj obj, ScmSlotAccessor *sa, int boundp)
1538 {
1539 ScmClass *klass = Scm_ClassOf(obj);
1540 if (klass != sa->klass) {
1541 Scm_Error("attempt to use a slot accessor %S on the object of different class: %S",
1542 SCM_OBJ(sa), obj);
1543 }
1544 return slot_ref_using_accessor(obj, sa, boundp);
1545 }
1546
1547 /* SLOT-REF-USING-CLASS
1548 *
1549 * (define-method slot-ref-using-class
1550 * ((class <class>) (obj <object>) slot)
1551 * (unless (eq? (class-of obj) class) (error !!!))
1552 * (let ((sa (get-slot-accessor class slot)))
1553 * (if sa
1554 * (%internal-slot-ref-using-accessor obj sa #f)
1555 * (slot-missing class obj slot))))
1556 *
1557 * - no class redefinition check is performed. if obj isn't updated,
1558 * and class is an old class, then it can access to the old instance's
1559 * slot value.
1560 */
1561 static ScmObj slot_ref_using_class(ScmNextMethod *nm SCM_UNUSED,
1562 ScmObj *argv,
1563 int argc SCM_UNUSED,
1564 void *d SCM_UNUSED)
1565 {
1566 ScmClass *klass = SCM_CLASS(argv[0]);
1567 ScmObj obj = argv[1];
1568 ScmObj slot = argv[2];
1569
1570 if (!SCM_EQ(SCM_OBJ(klass), SCM_OBJ(Scm_ClassOf(obj)))) {
1571 Scm_Error("slot-ref-using-class: class %S is not the class of object %S", klass, obj);
1572 }
1573 ScmSlotAccessor *sa = Scm_GetSlotAccessor(klass, slot);
1574 if (sa == NULL) return SLOT_MISSING3(klass, obj, slot);
1575 else return slot_ref_using_accessor(obj, sa, FALSE);
1576 }
1577
1578 static ScmClass *slot_ref_using_class_SPEC[] = {
1579 SCM_CLASS_STATIC_PTR(Scm_ClassClass),
1580 SCM_CLASS_STATIC_PTR(Scm_ObjectClass),
1581 SCM_CLASS_STATIC_PTR(Scm_TopClass)
1582 };
1583 static SCM_DEFINE_METHOD(slot_ref_using_class_rec,
1584 &Scm_GenericSlotRefUsingClass,
1585 3, 0, slot_ref_using_class_SPEC,
1586 slot_ref_using_class, NULL);
1587
1588 /* (internal) SLOT-SET-USING-ACCESSOR
1589 *
1590 * - assumes accessor belongs to the proper class.
1591 * - no class redefinition check is done
1592 */
1593 ScmObj slot_set_using_accessor(ScmObj obj,
1594 ScmSlotAccessor *sa,
1595 ScmObj val)
1596 {
1597 if (sa->setter && sa->setter != immutable_setter) {
1598 sa->setter(obj, val);
1599 } else if (sa->slotNumber >= 0) {
1600 ScmClass *k = sa->klass;
1601 if (sa->slotNumber >= k->numInstanceSlots) {
1602 Scm_Error("slot accessor %S has an invalid instance slot number "
1603 "for %S", SCM_OBJ(sa), obj);
1604 }
1605 if (sa->setter == immutable_setter
1606 && !SCM_UNBOUNDP(SCM_INSTANCE_SLOTS(obj)[sa->slotNumber])) {
1607 Scm_Error("Instance slot %S is immutable for object %S",
1608 SCM_OBJ(sa), obj);
1609 }
1610 scheme_slot_set(obj, sa->slotNumber, val);
1611 } else if (SCM_PROCEDUREP(sa->schemeSetter)) {
1612 return Scm_VMApply(sa->schemeSetter, SCM_LIST2(obj, val));
1613 } else {
1614 Scm_Error("slot %S of class %S is read-only", sa->name,
1615 SCM_OBJ(Scm_ClassOf(obj)));
1616 }
1617 return SCM_UNDEFINED;
1618 }
1619
1620 /* (internal) SLOT-ACCESSOR-SETTABLE
1621 * must be in sync with slot_set_using_accessor.
1622 * this won't detect the case when slot mutation is rejected by
1623 * the setter procedure.
1624 */
1625 static int slot_accessor_settable_p(ScmSlotAccessor *sa)
1626 {
1627 if (sa->setter
1628 || sa->slotNumber >= 0
1629 || SCM_PROCEDUREP(sa->schemeSetter))
1630 return TRUE;
1631 else
1632 return FALSE;
1633 }
1634
1635 /* SLOT-SET!
1636 *
1637 * (define (slot-set! obj slot val)
1638 * (%check-class-redefined (class-of obj))
1639 * (let ((sa (get-slot-accessor (class-of obj) slot)))
1640 * (if sa
1641 * (%internal-slot-set-using-accessor obj sa val)
1642 * (slot-missing (class-of obj) obj slot val))))
1643 */
1644 static ScmObj slot_set_cc(ScmObj result SCM_UNUSED, void **data)
1645 {
1646 return Scm_VMSlotSet(SCM_OBJ(data[0]), SCM_OBJ(data[1]), SCM_OBJ(data[2]));
1647 }
1648
1649 ScmObj Scm_VMSlotSet(ScmObj obj, ScmObj slot, ScmObj val)
1650 {
1651 ScmClass *klass = Scm_ClassOf(obj);
1652 if (!SCM_FALSEP(klass->redefined)) {
1653 void *data[3];
1654 data[0] = obj;
1655 data[1] = slot;
1656 data[2] = val;
1657 Scm_VMPushCC(slot_set_cc, data, 3);
1658 return instance_class_redefinition(obj, klass);
1659 }
1660 ScmSlotAccessor *sa = Scm_GetSlotAccessor(klass, slot);
1661 if (sa == NULL) return SLOT_MISSING4(klass, obj, slot, val);
1662 else return slot_set_using_accessor(obj, sa, val);
1663 }
1664
1665 /* SLOT-SET-USING-ACCESSOR
1666 *
1667 * (define (slot-set-using-accessor obj sa val)
1668 * (%check-if-sa-is-valid-for-object obj sa)
1669 * (%internal-slot-set-using-accessor obj sa val))
1670 *
1671 * - no class redefinition check is performed. if obj isn't updated
1672 * for the new class, sa must come from the old class.
1673 */
1674 ScmObj Scm_VMSlotSetUsingAccessor(ScmObj obj, ScmSlotAccessor *sa, ScmObj val)
1675 {
1676 ScmClass *klass = Scm_ClassOf(obj);
1677 if (klass != sa->klass) {
1678 Scm_Error("attempt to use a slot accessor %S on the object of different class: %S",
1679 SCM_OBJ(sa), obj);
1680 }
1681 return slot_set_using_accessor(obj, sa, val);
1682 }
1683
1684 /* SLOT-SET-USING-CLASS
1685 *
1686 * (define-method slot-set-using-class
1687 * ((class <class>) (obj <object>) slot val)
1688 * (unless (eq? (class-of obj) class) (error !!!))
1689 * (let ((sa (get-slot-accessor class slot)))
1690 * (if sa
1691 * (%internal-slot-set-using-accessor obj sa val)
1692 * (slot-missing class obj slot val))))
1693 *
1694 * - no class redefinition check is performed. if obj isn't updated,
1695 * and class is an old class, then it can access to the old instance's
1696 * slot value.
1697 */
1698 static ScmObj slot_set_using_class(ScmNextMethod *nm SCM_UNUSED,
1699 ScmObj *argv,
1700 int argc SCM_UNUSED,
1701 void *d SCM_UNUSED)
1702 {
1703 ScmClass *klass = SCM_CLASS(argv[0]);
1704 ScmObj obj = argv[1];
1705 ScmObj slot = argv[2];
1706 ScmObj val = argv[3];
1707
1708 if (!SCM_EQ(SCM_OBJ(klass), SCM_OBJ(Scm_ClassOf(obj)))) {
1709 Scm_Error("slot-ref-using-class: class %S is not the class of object %S", klass, obj);
1710 }
1711 ScmSlotAccessor *sa = Scm_GetSlotAccessor(klass, slot);
1712 if (sa == NULL) return SLOT_MISSING4(klass, obj, slot, val);
1713 else return slot_set_using_accessor(obj, sa, val);
1714 }
1715
1716 static ScmClass *slot_set_using_class_SPEC[] = {
1717 SCM_CLASS_STATIC_PTR(Scm_ClassClass),
1718 SCM_CLASS_STATIC_PTR(Scm_ObjectClass),
1719 SCM_CLASS_STATIC_PTR(Scm_TopClass),
1720 SCM_CLASS_STATIC_PTR(Scm_TopClass)
1721 };
1722 static SCM_DEFINE_METHOD(slot_set_using_class_rec,
1723 &Scm_GenericSlotSetUsingClass,
1724 4, 0, slot_set_using_class_SPEC,
1725 slot_set_using_class, NULL);
1726
1727 /* SLOT-BOUND?
1728 *
1729 * (define (slot-bound? obj slot)
1730 * (%check-class-redefined (class-of obj))
1731 * (slot-bound-using-class (class-of obj) obj slot))
1732 */
1733 static ScmObj slot_boundp_cc(ScmObj result SCM_UNUSED, void **data)
1734 {
1735 ScmObj obj = SCM_OBJ(data[0]);
1736 ScmObj slot = SCM_OBJ(data[1]);
1737 return Scm_VMSlotBoundP(obj, slot);
1738 }
1739
1740 ScmObj Scm_VMSlotBoundP(ScmObj obj, ScmObj slot)
1741 {
1742 ScmClass *klass = Scm_ClassOf(obj);
1743
1744 if (!SCM_FALSEP(klass->redefined)) {
1745 void *data[2];
1746 data[0] = obj;
1747 data[1] = slot;
1748 Scm_VMPushCC(slot_boundp_cc, data, 2);
1749 return instance_class_redefinition(obj, Scm_ClassOf(obj));
1750 }
1751 return Scm_VMApply(SCM_OBJ(&Scm_GenericSlotBoundUsingClassP),
1752 SCM_LIST3(SCM_OBJ(klass), obj, slot));
1753 }
1754
1755 /* SLOT-BOUND-USING-CLASS?
1756 *
1757 * (define-method slot-bound-using-class? ((class <class>)
1758 * (obj <obj>)
1759 * slot)
1760 * (unless (eq? class (class-of obj)) (error !!!))
1761 * (let ((sa (get-slot-accessor class slot)))
1762 * (if sa
1763 * (%internal-slot-ref-using-accessor obj sa #t)
1764 * (slot-missing class obj slot)))
1765 *
1766 * - no redefinition check!
1767 */
1768 static ScmObj slot_bound_using_class_p(ScmNextMethod *nm SCM_UNUSED,
1769 ScmObj *argv,
1770 int argc SCM_UNUSED,
1771 void *data SCM_UNUSED)
1772 {
1773 ScmClass *klass = SCM_CLASS(argv[0]);
1774 ScmObj obj = argv[1];
1775 ScmObj slot = argv[2];
1776
1777 if (!SCM_EQ(SCM_OBJ(klass), SCM_OBJ(Scm_ClassOf(obj)))) {
1778 Scm_Error("slot-bound-using-class?: class %S is not the class of object %S", klass, obj);
1779 }
1780 ScmSlotAccessor *sa =Scm_GetSlotAccessor(klass, slot);
1781 if (sa == NULL) return SLOT_MISSING3(klass, obj, slot);
1782 else return slot_ref_using_accessor(obj, sa, TRUE);
1783 }
1784
1785 static ScmClass *slot_bound_using_class_p_SPEC[] = {
1786 SCM_CLASS_STATIC_PTR(Scm_ClassClass),
1787 SCM_CLASS_STATIC_PTR(Scm_TopClass),
1788 SCM_CLASS_STATIC_PTR(Scm_TopClass)
1789 };
1790 static SCM_DEFINE_METHOD(slot_bound_using_class_p_rec,
1791 &Scm_GenericSlotBoundUsingClassP,
1792 3, 0,
1793 slot_bound_using_class_p_SPEC,
1794 slot_bound_using_class_p, NULL);
1795
1796 /*
1797 * Builtin object initializer
1798 * This is the fallback method of generic initialize. Since all the
1799 * Scheme-defined objects will be initialized by object_initialize,
1800 * this method is called only for built-in classes.
1801 */
1802 static ScmObj builtin_initialize(ScmObj *argv, int argc,
1803 ScmGeneric *gf SCM_UNUSED)
1804 {
1805 SCM_ASSERT(argc == 2);
1806 ScmObj instance = argv[0];
1807 ScmObj initargs = argv[1];
1808 if (Scm_Length(initargs) % 2) {
1809 Scm_Error("initializer list is not even: %S", initargs);
1810 }
1811 ScmClass *klass = Scm_ClassOf(instance);
1812 ScmObj ap;
1813 SCM_FOR_EACH(ap, klass->accessors) {
1814 ScmSlotAccessor *acc = SCM_SLOT_ACCESSOR(SCM_CDAR(ap));
1815 if (acc->setter && SCM_KEYWORDP(acc->initKeyword)) {
1816 ScmObj val = Scm_GetKeyword(acc->initKeyword, initargs, SCM_UNDEFINED);
1817 if (!SCM_UNDEFINEDP(val)) {
1818 acc->setter(instance, val);
1819 }
1820 }
1821 }
1822 return instance;
1823 }
1824
1825 /*--------------------------------------------------------------
1826 * Slot accessor object
1827 */
1828
1829 /* we initialize fields appropriately here. */
1830 static ScmObj slot_accessor_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
1831 {
1832 ScmSlotAccessor *sa = SCM_NEW(ScmSlotAccessor);
1833
1834 SCM_SET_CLASS(sa, klass);
1835 sa->name = SCM_FALSE;
1836 sa->getter = NULL;
1837 sa->setter = NULL;
1838 sa->initValue = SCM_UNBOUND;
1839 sa->initKeyword = SCM_FALSE;
1840 sa->initThunk = SCM_FALSE;
1841 sa->initializable = FALSE;
1842 sa->slotNumber = -1;
1843 sa->schemeGetter = SCM_FALSE;
1844 sa->schemeSetter = SCM_FALSE;
1845 sa->schemeBoundp = SCM_FALSE;
1846 return SCM_OBJ(sa);
1847 }
1848
1849 static void slot_accessor_print(ScmObj obj, ScmPort *out,
1850 ScmWriteContext *ctx SCM_UNUSED)
1851 {
1852 ScmSlotAccessor *sa = SCM_SLOT_ACCESSOR(obj);
1853
1854 Scm_Printf(out, "#<slot-accessor %S.%S ",
1855 (sa->klass? sa->klass->name : SCM_FALSE),
1856 sa->name);
1857 if (sa->getter) Scm_Printf(out, "native");
1858 else if (!SCM_FALSEP(sa->schemeGetter)) Scm_Printf(out, "proc");
1859 else if (sa->slotNumber >= 0) Scm_Printf(out, "%d", sa->slotNumber);
1860 else Scm_Printf(out, "unknown");
1861 if (!SCM_FALSEP(sa->initKeyword))
1862 Scm_Printf(out, " %S", sa->initKeyword);
1863 Scm_Printf(out, ">");
1864 }
1865
1866 /* A special 'setter' value to indicate immutable instance slot.
1867 The procedure itself is never called. */
1868 static void immutable_setter(ScmObj obj SCM_UNUSED, ScmObj val SCM_UNUSED)
1869 {
1870 }
1871
1872
1873 /* some information is visible from Scheme world */
1874 static ScmObj slot_accessor_class(ScmSlotAccessor *sa)
1875 {
1876 return SCM_OBJ(sa->klass);
1877 }
1878
1879 static void slot_accessor_class_set(ScmSlotAccessor *sa, ScmObj v)
1880 {
1881 if (!Scm_TypeP(v, SCM_CLASS_CLASS)) {
1882 Scm_Error(":class argument must be a class metaobject, but got %S", v);
1883 }
1884 sa->klass = SCM_CLASS(v);
1885 }
1886
1887 static ScmObj slot_accessor_name(ScmSlotAccessor *sa)
1888 {
1889 return sa->name;
1890 }
1891
1892 static void slot_accessor_name_set(ScmSlotAccessor *sa, ScmObj v)
1893 {
1894 sa->name = v;
1895 }
1896
1897 static ScmObj slot_accessor_init_value(ScmSlotAccessor *sa)
1898 {
1899 return sa->initValue;
1900 }
1901
1902 static void slot_accessor_init_value_set(ScmSlotAccessor *sa, ScmObj v)
1903 {
1904 sa->initValue = v;
1905 }
1906
1907 static ScmObj slot_accessor_init_keyword(ScmSlotAccessor *sa)
1908 {
1909 return sa->initKeyword;
1910 }
1911
1912 static void slot_accessor_init_keyword_set(ScmSlotAccessor *sa, ScmObj v)
1913 {
1914 sa->initKeyword = v;
1915 }
1916
1917 static ScmObj slot_accessor_init_thunk(ScmSlotAccessor *sa)
1918 {
1919 return sa->initThunk;
1920 }
1921
1922 static void slot_accessor_init_thunk_set(ScmSlotAccessor *sa, ScmObj v)
1923 {
1924 sa->initThunk = v;
1925 }
1926
1927 static ScmObj slot_accessor_immutable(ScmSlotAccessor *sa)
1928 {
1929 return SCM_MAKE_BOOL(sa->setter == immutable_setter);
1930 }
1931
1932 static void slot_accessor_immutable_set(ScmSlotAccessor *sa, ScmObj v)
1933 {
1934 /* NB: Calling this with v == #f on the slot accessor with C setter
1935 would cause inconsistency. */
1936 if (SCM_FALSEP(v)) sa->setter = NULL;
1937 else sa->setter = immutable_setter;
1938 }
1939
1940 static ScmObj slot_accessor_slot_number(ScmSlotAccessor *sa)
1941 {
1942 return SCM_MAKE_INT(sa->slotNumber);
1943 }
1944
1945 static void slot_accessor_slot_number_set(ScmSlotAccessor *sa, ScmObj val)
1946 {
1947 int n = 0;
1948 if (!SCM_INTP(val) || ((n = SCM_INT_VALUE(val)) < 0))
1949 Scm_Error("small positive integer required, but got %S", val);
1950 sa->slotNumber = n;
1951 }
1952
1953 static ScmObj slot_accessor_initializable(ScmSlotAccessor *sa)
1954 {
1955 return SCM_MAKE_BOOL(sa->initializable);
1956 }
1957
1958 static void slot_accessor_initializable_set(ScmSlotAccessor *sa, ScmObj v)
1959 {
1960 sa->initializable = SCM_FALSEP(v)? FALSE : TRUE;
1961 }
1962
1963 static ScmObj slot_accessor_settable(ScmSlotAccessor *sa)
1964 {
1965 return SCM_MAKE_BOOL(slot_accessor_settable_p(sa));
1966 }
1967
1968 static ScmObj slot_accessor_scheme_getter(ScmSlotAccessor *sa)
1969 {
1970 return sa->schemeGetter;
1971 }
1972
1973 static void slot_accessor_scheme_getter_set(ScmSlotAccessor *sa, ScmObj p)
1974 {
1975 /* TODO: check */
1976 sa->schemeGetter = p;
1977 }
1978
1979 static ScmObj slot_accessor_scheme_setter(ScmSlotAccessor *sa)
1980 {
1981 return sa->schemeSetter;
1982 }
1983
1984 static void slot_accessor_scheme_setter_set(ScmSlotAccessor *sa, ScmObj p)
1985 {
1986 /* TODO: check */
1987 sa->schemeSetter = p;
1988 }
1989
1990 static ScmObj slot_accessor_scheme_boundp(ScmSlotAccessor *sa)
1991 {
1992 return sa->schemeBoundp;
1993 }
1994
1995 static void slot_accessor_scheme_boundp_set(ScmSlotAccessor *sa, ScmObj p)
1996 {
1997 /* TODO: check */
1998 sa->schemeBoundp = p;
1999 }
2000
2001 /*=====================================================================
2002 * <object> class initialization
2003 */
2004
2005 static ScmObj instance_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
2006 {
2007 return SCM_OBJ(SCM_NEW_INSTANCE(ScmInstance, klass));
2008 }
2009
2010 /* (initialize <object> initargs) */
2011 static ScmObj object_initialize_cc(ScmObj result, void **data);
2012
2013 static ScmObj object_initialize1(ScmObj obj, ScmObj accs, ScmObj initargs)
2014 {
2015 if (SCM_NULLP(accs)) return obj;
2016 SCM_ASSERT(SCM_PAIRP(SCM_CAR(accs))
2017 && SCM_SLOT_ACCESSOR_P(SCM_CDAR(accs)));
2018 void *next[3];
2019 next[0] = obj;
2020 next[1] = SCM_CDR(accs);
2021 next[2] = initargs;
2022 Scm_VMPushCC(object_initialize_cc, next, 3);
2023 return Scm_VMSlotInitializeUsingAccessor(obj,
2024 SCM_SLOT_ACCESSOR(SCM_CDAR(accs)),
2025 initargs);
2026 }
2027
2028 static ScmObj object_initialize_cc(ScmObj result SCM_UNUSED, void **data)
2029 {
2030 ScmObj obj = SCM_OBJ(data[0]);
2031 ScmObj accs = SCM_OBJ(data[1]);
2032 ScmObj initargs = SCM_OBJ(data[2]);
2033 return object_initialize1(obj, accs, initargs);
2034 }
2035
2036 static ScmObj object_initialize(ScmNextMethod *nm SCM_UNUSED,
2037 ScmObj *argv,
2038 int argc SCM_UNUSED,
2039 void *data SCM_UNUSED)
2040 {
2041 ScmObj obj = argv[0];
2042 ScmObj initargs = argv[1];
2043 ScmObj accs = Scm_ClassOf(obj)->accessors;
2044 if (SCM_NULLP(accs)) return obj;
2045 return object_initialize1(obj, accs, initargs);
2046 }
2047
2048 static ScmClass *object_initialize_SPEC[] = {
2049 SCM_CLASS_STATIC_PTR(Scm_ObjectClass), SCM_CLASS_STATIC_PTR(Scm_ListClass)
2050 };
2051 static SCM_DEFINE_METHOD(object_initialize_rec,
2052 &Scm_GenericInitialize,
2053 2, 0,
2054 object_initialize_SPEC,
2055 object_initialize, NULL);
2056
2057 /* Default equal? delegates compare action to generic function object-equal?.
2058 We can't use VMApply here */
2059 int Scm_ObjectCompare(ScmObj x, ScmObj y, int equalp)
2060 {
2061 ScmObj r;
2062 if (equalp) {
2063 r = Scm_ApplyRec2(SCM_OBJ(&Scm_GenericObjectEqualP), x, y);
2064 return (SCM_FALSEP(r)? -1 : 0);
2065 } else {
2066 r = Scm_ApplyRec2(SCM_OBJ(&Scm_GenericObjectCompare), x, y);
2067 if (SCM_INTP(r)) {
2068 int ri = SCM_INT_VALUE(r);
2069 if (ri < 0) return -1;
2070 if (ri > 0) return 1;
2071 else return 0;
2072 }
2073 Scm_Error("object %S and %S can't be ordered", x, y);
2074 return 0; /* dummy */
2075 }
2076 }
2077
2078 /* Fallback of object-equal? and object-compare.
2079 We return #f for fallback of object-compare, which means two objects
2080 can't be ordered.
2081 */
2082 static ScmObj fallback_compare(ScmObj *argv, int argc, ScmGeneric *gf)
2083 {
2084 if (argc == 2) return SCM_FALSE;
2085 else return Scm_NoNextMethod(argv, argc, gf);
2086 }
2087
2088
2089 /*=====================================================================
2090 * Generic function
2091 */
2092
2093 static ScmObj generic_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
2094 {
2095 ScmGeneric *gf = SCM_NEW_INSTANCE(ScmGeneric, klass);
2096 Scm__ProcedureInit(SCM_PROCEDURE(gf), SCM_PROC_GENERIC, 0, 0, SCM_FALSE);
2097 gf->methods = SCM_NIL;
2098 gf->dispatcher = NULL;
2099 gf->fallback = Scm_NoNextMethod;
2100 gf->data = NULL;
2101 gf->maxReqargs = 0;
2102 (void)SCM_INTERNAL_MUTEX_INIT(gf->lock);
2103 return SCM_OBJ(gf);
2104 }
2105
2106 static void generic_print(ScmObj obj, ScmPort *port,
2107 ScmWriteContext *ctx SCM_UNUSED)
2108 {
2109 #if 0 /* enable this to show maxReqargs */
2110 Scm_Printf(port, "#<generic %S (%d:%d)>",
2111 SCM_GENERIC(obj)->common.info,
2112 Scm_Length(SCM_GENERIC(obj)->methods),
2113 SCM_GENERIC(obj)->maxReqargs);
2114 #else
2115 Scm_Printf(port, "#<generic %S (%d)>",
2116 SCM_GENERIC(obj)->common.info,
2117 Scm_Length(SCM_GENERIC(obj)->methods));
2118 #endif
2119 }
2120
2121 /*
2122 * Accessors
2123 */
2124 static ScmObj generic_name(ScmGeneric *gf)
2125 {
2126 return gf->common.info;
2127 }
2128
2129 static void generic_name_set(ScmGeneric *gf, ScmObj val)
2130 {
2131 gf->common.info = val;
2132 }
2133
2134 static ScmObj generic_sealed(ScmGeneric *gf)
2135 {
2136 return SCM_GENERIC_SEALED_P(gf)? SCM_TRUE:SCM_FALSE;
2137 }
2138
2139 static ScmObj generic_info(ScmGeneric *gf)
2140 {
2141 return gf->common.info;
2142 }
2143
2144 static ScmObj generic_methods(ScmGeneric *gf)
2145 {
2146 return gf->methods;
2147 }
2148
2149 static void generic_methods_set(ScmGeneric *gf, ScmObj val)
2150 {
2151 int reqs = 0;
2152 ScmObj cp;
2153 SCM_FOR_EACH(cp, val) {
2154 if (!SCM_METHODP(SCM_CAR(cp))) {
2155 Scm_Error("The methods slot of <generic> must be a list of method, but given: %S", val);
2156 }
2157 if (SCM_PROCEDURE_REQUIRED(SCM_CAR(cp)) > reqs) {
2158 reqs = SCM_PROCEDURE_REQUIRED(SCM_CAR(cp));
2159 }
2160 }
2161 if (!SCM_NULLP(cp)) {
2162 Scm_Error("The methods slot of <generic> cannot contain an improper list: %S", val);
2163 }
2164 (void)SCM_INTERNAL_MUTEX_LOCK(gf->lock);
2165 gf->methods = val;
2166 gf->maxReqargs = reqs;
2167 (void)SCM_INTERNAL_MUTEX_UNLOCK(gf->lock);
2168 }
2169
2170 /* Make base generic function from C */
2171 ScmObj Scm_MakeBaseGeneric(ScmObj name,
2172 ScmObj (*fallback)(ScmObj *, int, ScmGeneric*),
2173 void *data)
2174 {
2175 ScmGeneric *gf = SCM_GENERIC(generic_allocate(SCM_CLASS_GENERIC, SCM_NIL));
2176 gf->common.info = name;
2177 if (fallback) {
2178 gf->fallback = fallback;
2179 gf->data = data;
2180 }
2181 return SCM_OBJ(gf);
2182 }
2183
2184 /* default "default method" */
2185 ScmObj Scm_NoNextMethod(ScmObj *argv, int argc, ScmGeneric *gf)
2186 {
2187 Scm_Error("no applicable method for %S with arguments %S",
2188 SCM_OBJ(gf), Scm_ArrayToList(argv, argc));
2189 return SCM_UNDEFINED; /* dummy */
2190 }
2191
2192 /* another handy "default method", which does nothing. */
2193 ScmObj Scm_NoOperation(ScmObj *argv SCM_UNUSED,
2194 int argc SCM_UNUSED,
2195 ScmGeneric *gf SCM_UNUSED)
2196 {
2197 return SCM_UNDEFINED;
2198 }
2199
2200 /* fallback of object-apply */
2201 ScmObj Scm_InvalidApply(ScmObj *argv, int argc, ScmGeneric *gf SCM_UNUSED)
2202 {
2203 Scm_Error("invalid application: %S", Scm_ArrayToList(argv, argc));
2204 return SCM_UNDEFINED;
2205 }
2206
2207 /* method-appliable-for-classes?
2208 NB: This may need to be redesigned once we support eqv specializer.
2209 */
2210 int Scm_MethodApplicableForClasses(ScmMethod *m, ScmClass *types[], int nargs)
2211 {
2212 if (nargs < m->common.required
2213 || (!m->common.optional && nargs != m->common.required)) {
2214 return FALSE;
2215 } else {
2216 ScmClass **sp = m->specializers;
2217 int n = 0;
2218 for (; n < m->common.required; n++) {
2219 if (SCM_EQ(sp[n], SCM_CLASS_TOP)) continue;
2220 if (!Scm_SubclassP(types[n], sp[n])) return FALSE;
2221 }
2222 }
2223 return TRUE;
2224 }
2225
2226 /* compute-applicable-methods */
2227 ScmObj Scm_ComputeApplicableMethods(ScmGeneric *gf, ScmObj *argv, int argc,
2228 int applyargs)
2229 {
2230 ScmObj methods = gf->methods, mp, ap;
2231 ScmObj h = SCM_NIL, t = SCM_NIL;
2232 ScmClass *typev_s[PREALLOC_SIZE], **typev = typev_s;
2233 int i, nsel;
2234
2235 if (SCM_NULLP(methods)) return SCM_NIL;
2236
2237 if (gf->maxReqargs > PREALLOC_SIZE) {
2238 typev = SCM_NEW_ATOMIC_ARRAY(ScmClass*, gf->maxReqargs);
2239 }
2240 nsel = gf->maxReqargs;
2241 if (applyargs) argc--;
2242 for (i = 0; i < argc && nsel >= 0; i++, nsel--) {
2243 typev[i] = Scm_ClassOf(argv[i]);
2244 }
2245 if (applyargs && nsel) {
2246 SCM_FOR_EACH(ap, argv[argc]) {
2247 if (--nsel >= 0) typev[i++] = Scm_ClassOf(SCM_CAR(ap));
2248 argc++;
2249 }
2250 }
2251
2252 if (gf->dispatcher
2253 && argc <= SCM_DISPATCHER_MAX_NARGS
2254 && argc >= 1) {
2255 ScmMethodDispatcher *dis = (ScmMethodDispatcher*)gf->dispatcher;
2256 ScmObj p = Scm__MethodDispatcherLookup(dis, typev, argc);
2257 if (SCM_PAIRP(p)) methods = p;
2258 }
2259
2260 SCM_ASSERT(SCM_PAIRP(methods));
2261 if (SCM_NULLP(SCM_CDR(methods))) {
2262 /* We have only one method, so just check its applicability
2263 and retrun the list without allocation if possible. */
2264 if (Scm_MethodApplicableForClasses(SCM_METHOD(SCM_CAR(methods)),
2265 typev, argc)) {
2266 return methods;
2267 } else {
2268 return SCM_NIL;
2269 }
2270 } else {
2271 SCM_FOR_EACH(mp, methods) {
2272 ScmObj m = SCM_CAR(mp);
2273 SCM_ASSERT(SCM_METHODP(m));
2274 if (Scm_MethodApplicableForClasses(SCM_METHOD(m), typev, argc)) {
2275 SCM_APPEND1(h, t, SCM_OBJ(m));
2276 }
2277 }
2278 return h;
2279 }
2280 }
2281
2282 static ScmObj compute_applicable_methods(ScmNextMethod *nm SCM_UNUSED,
2283 ScmObj *argv,
2284 int argc SCM_UNUSED,
2285 void *data SCM_UNUSED)
2286 {
2287 ScmGeneric *gf = SCM_GENERIC(argv[0]);
2288 ScmObj arglist = argv[1];
2289 int n = Scm_Length(arglist);
2290 if (n < 0) Scm_Error("bad argument list: %S", arglist);
2291
2292 return Scm_ComputeApplicableMethods(gf, &arglist, 1, TRUE);
2293 }
2294
2295 static ScmClass *compute_applicable_methods_SPEC[] = {
2296 SCM_CLASS_STATIC_PTR(Scm_GenericClass), SCM_CLASS_STATIC_PTR(Scm_ListClass)
2297 };
2298 static SCM_DEFINE_METHOD(compute_applicable_methods_rec,
2299 &Scm_GenericComputeApplicableMethods,
2300 2, 0,
2301 compute_applicable_methods_SPEC,
2302 compute_applicable_methods, NULL);
2303
2304 /* method-more-specific? */
2305 static inline int method_more_specific(ScmMethod *x, ScmMethod *y,
2306 ScmClass **targv, int argc SCM_UNUSED)
2307 {
2308 ScmClass **xs = x->specializers;
2309 ScmClass **ys = y->specializers;
2310 int xreq = SCM_PROCEDURE_REQUIRED(x), yreq = SCM_PROCEDURE_REQUIRED(y);
2311
2312 for (int i=0; i<xreq && i<yreq; i++) {
2313 if (xs[i] != ys[i]) {
2314 ScmClass *ac = targv[i];
2315 if (xs[i] == ac) return TRUE;
2316 if (ys[i] == ac) return FALSE;
2317 for (ScmClass **acpl = ac->cpa; *acpl; acpl++) {
2318 if (xs[i] == *acpl) return TRUE;
2319 if (ys[i] == *acpl) return FALSE;
2320 }
2321 /* If we're here, two methods are not orderable. */
2322 Scm_Error("Couldn't determine which method is more specific:"
2323 " %S and %S: Check if compute-applicable-methods is "
2324 "working properly.", SCM_OBJ(x), SCM_OBJ(y));
2325 }
2326 }
2327 if (xreq > yreq) return TRUE;
2328 if (xreq < yreq) return FALSE;
2329
2330 /* all specializers match. the one without optional arg is more special.*/
2331 if (SCM_PROCEDURE_OPTIONAL(y)) return TRUE;
2332 else return FALSE;
2333 }
2334
2335 static ScmObj method_more_specific_p(ScmNextMethod *nm SCM_UNUSED,
2336 ScmObj *argv,
2337 int argc SCM_UNUSED,
2338 void *data SCM_UNUSED)
2339 {
2340 ScmMethod *x = SCM_METHOD(argv[0]);
2341 ScmMethod *y = SCM_METHOD(argv[1]);
2342 ScmObj targlist = argv[2];
2343 ScmClass *targv_s[PREALLOC_SIZE], **targv = targv_s;
2344 int targc = Scm_Length(targlist);
2345 if (targc < 0) Scm_Error("bad targ list: %S", targlist);
2346 if (targc > PREALLOC_SIZE) {
2347 targv = SCM_NEW_ARRAY(ScmClass*, targc);
2348 }
2349 int i = 0;
2350 ScmObj tp;
2351 SCM_FOR_EACH(tp, targlist) {
2352 if (!Scm_TypeP(SCM_CAR(tp), SCM_CLASS_CLASS))
2353 Scm_Error("bad class object in type list: %S", SCM_CAR(tp));
2354 targv[i++] = SCM_CLASS(SCM_CAR(tp));
2355 }
2356 return SCM_MAKE_BOOL(method_more_specific(x, y, targv, targc));
2357 }
2358 static ScmClass *method_more_specific_p_SPEC[] = {
2359 SCM_CLASS_STATIC_PTR(Scm_MethodClass),
2360 SCM_CLASS_STATIC_PTR(Scm_MethodClass),
2361 SCM_CLASS_STATIC_PTR(Scm_ListClass)
2362 };
2363 static SCM_DEFINE_METHOD(method_more_specific_p_rec,
2364 &Scm_GenericMethodMoreSpecificP,
2365 3, 0,
2366 method_more_specific_p_SPEC,
2367 method_more_specific_p, NULL);
2368
2369 /* sort-methods
2370 * This is a naive implementation just to make things work.
2371 *
2372 * Argv/argc is used to create an array of classes used to order methods.
2373 * We never need the arguments more than the maximum number of required
2374 * arguments among the given methods; when VM calls Scm_SortMethods, it
2375 * only puts as many args as gf->maxReqargs.
2376 *
2377 * TODO: can't we carry around the method list in array
2378 * instead of list, at least internally?
2379 */
2380 ScmObj Scm_SortMethods(ScmObj methods, ScmObj *argv, int argc)
2381 {
2382 ScmObj array_s[PREALLOC_SIZE], *array = array_s;
2383 ScmClass *targv_s[PREALLOC_SIZE], **targv = targv_s;
2384 int cnt = 0, len = Scm_Length(methods);
2385
2386 if (len >= PREALLOC_SIZE) array = SCM_NEW_ARRAY(ScmObj, len);
2387 if (argc >= PREALLOC_SIZE) targv = SCM_NEW_ARRAY(ScmClass*, argc);
2388
2389 ScmObj mp;
2390 SCM_FOR_EACH(mp, methods) {
2391 if (!Scm_TypeP(SCM_CAR(mp), SCM_CLASS_METHOD))
2392 Scm_Error("bad method in applicable method list: %S", SCM_CAR(mp));
2393 array[cnt] = SCM_CAR(mp);
2394 cnt++;
2395 }
2396 for (int i=0; i<argc; i++) targv[i] = Scm_ClassOf(argv[i]);
2397
2398 for (int step = len/2; step > 0; step /= 2) {
2399 for (int i=step; i<len; i++) {
2400 for (int j=i-step; j >= 0; j -= step) {
2401 if (method_more_specific(SCM_METHOD(array[j]),
2402 SCM_METHOD(array[j+step]),
2403 targv, argc)) {
2404 break;
2405 } else {
2406 ScmObj tmp = array[j+step];
2407 array[j+step] = array[j];
2408 array[j] = tmp;
2409 }
2410 }
2411 }
2412 }
2413 return Scm_ArrayToList(array, len);
2414 }
2415
2416
2417 /* Developer API. Accessible from Scheme via generic-build-dispatcher!
2418 If axis is out of range, we do nothing and returns #f.
2419 */
2420 ScmObj Scm__GenericBuildDispatcher(ScmGeneric *gf, int axis)
2421 {
2422 if (!disable_generic_dispatcher
2423 && axis >= 0 && axis < SCM_DISPATCHER_MAX_NARGS) {
2424 (void)SCM_INTERNAL_MUTEX_LOCK(gf->lock);
2425 gf->dispatcher = Scm__BuildMethodDispatcher(gf->methods, axis);
2426 (void)SCM_INTERNAL_MUTEX_UNLOCK(gf->lock);
2427 return SCM_TRUE;
2428 } else {
2429 return SCM_FALSE;
2430 }
2431 }
2432
2433 /* Developer API */
2434 void Scm__GenericInvalidateDispatcher(ScmGeneric *gf)
2435 {
2436 (void)SCM_INTERNAL_MUTEX_LOCK(gf->lock);
2437 gf->dispatcher = NULL;
2438 (void)SCM_INTERNAL_MUTEX_UNLOCK(gf->lock);
2439 }
2440
2441 /* Developer API */
2442 ScmObj Scm__GenericDispatcherInfo(ScmGeneric *gf)
2443 {
2444 if (gf->dispatcher) {
2445 return Scm__MethodDispatcherInfo(gf->dispatcher);
2446 } else {
2447 return SCM_FALSE;
2448 }
2449 }
2450
2451 /* Developer API */
2452 void Scm__GenericDispatcherDump(ScmGeneric *gf, ScmPort *port)
2453 {
2454 if (gf->dispatcher) {
2455 Scm_Printf(port, "%S's dispatcher:\n", gf);
2456 Scm__MethodDispatcherDump((ScmMethodDispatcher*)gf->dispatcher, port);
2457 } else {
2458 Scm_Printf(port, "%S doesn't have a dispatcher.\n", gf);
2459 }
2460 }
2461
2462 /*=====================================================================
2463 * Method
2464 */
2465
2466 static ScmObj method_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED)
2467 {
2468 ScmMethod *meth = SCM_NEW_INSTANCE(ScmMethod, klass);
2469 Scm__ProcedureInit(SCM_PROCEDURE(meth), SCM_PROC_METHOD, 0, 0, SCM_FALSE);
2470 meth->generic = NULL;
2471 meth->specializers = NULL;
2472 meth->func = NULL;
2473 return SCM_OBJ(meth);
2474 }
2475
2476 static void method_print(ScmObj obj, ScmPort *port,
2477 ScmWriteContext *ctx SCM_UNUSED)
2478 {
2479 Scm_Printf(port, "#<method %S>", SCM_METHOD(obj)->common.info);
2480 }
2481
2482 /* See if this method doesn't use 'next-method' in its body. */
2483 static int method_leaf_p(ScmClosure *body)
2484 {
2485 ScmCompiledCode *code = SCM_COMPILED_CODE(SCM_CLOSURE_CODE(body));
2486 if (!SCM_PAIRP(code->signatureInfo)
2487 || !SCM_PAIRP(SCM_CAR(code->signatureInfo))) return FALSE;
2488 ScmObj attr = Scm_PairAttrGet(SCM_PAIR(SCM_CAR(code->signatureInfo)),
2489 SCM_SYM_UNUSED_ARGS,
2490 SCM_NIL);
2491 return !SCM_FALSEP(Scm_Memq(SCM_SYM_NEXT_METHOD, attr));
2492 }
2493
2494 /*
2495 * (initialize <method> (&key lamdba-list generic specializers body method-locked))
2496 * Method initialization. This needs to be hardcoded, since
2497 * we can't call Scheme version of initialize to initialize the
2498 * "initialize" method (chicken-and-egg circularity).
2499 */
2500 static ScmObj method_initialize(ScmNextMethod *nm SCM_UNUSED,
2501 ScmObj *argv,
2502 int argc SCM_UNUSED,
2503 void *data SCM_UNUSED)
2504 {
2505 ScmMethod *m = SCM_METHOD(argv[0]);
2506 ScmObj initargs = argv[1];
2507 ScmObj llist = Scm_GetKeyword(key_lambda_list, initargs, SCM_FALSE);
2508 ScmObj generic = Scm_GetKeyword(key_generic, initargs, SCM_FALSE);
2509 ScmObj specs = Scm_GetKeyword(key_specializers, initargs, SCM_FALSE);
2510 ScmObj body = Scm_GetKeyword(key_body, initargs, SCM_FALSE);
2511 ScmObj locked = Scm_GetKeyword(key_method_locked, initargs, SCM_FALSE);
2512 ScmObj lp, h, t;
2513 int speclen = 0, req = 0, opt = 0;
2514
2515 if (!Scm_TypeP(generic, SCM_CLASS_GENERIC))
2516 Scm_Error("generic function required for :generic argument: %S",
2517 generic);
2518 ScmGeneric *g = SCM_GENERIC(generic);
2519 if ((speclen = Scm_Length(specs)) < 0)
2520 Scm_Error("invalid specializers list: %S", specs);
2521 ScmClass **specarray = class_list_to_array(specs, speclen);
2522
2523 /* find out # of args from lambda list */
2524 SCM_FOR_EACH(lp, llist) req++;
2525 if (!SCM_NULLP(lp)) opt++;
2526
2527 if (SCM_PROCEDURE_REQUIRED(body) != req + opt + 1)
2528 Scm_Error("method body %S doesn't match with lambda list %S",
2529 body, llist);
2530 if (speclen != req)
2531 Scm_Error("specializer list doesn't match with lambda list: %S",specs);
2532
2533 /* Attach source code location info of body to the 'info' if possible*/
2534 m->common.info = Scm_ExtendedCons(g->common.info,
2535 class_array_to_names(specarray, speclen));
2536 if (!SCM_CLOSUREP(body))
2537 Scm_Error("closure required for :body argument: %S", body);
2538 ScmObj code = SCM_CLOSURE_CODE(body);
2539 /* TODO: debugInfo may still be packed at this moment. We don't want
2540 to unpack it yet. */
2541 if (SCM_COMPILED_CODE_P(code) &&
2542 SCM_PAIRP(SCM_COMPILED_CODE(code)->debugInfo)) {
2543 ScmObj def = Scm_Assq(SCM_SYM_DEFINITION,
2544 SCM_COMPILED_CODE(code)->debugInfo);
2545 if (SCM_PAIRP(def)) {
2546 ScmObj si = Scm_Assq(SCM_SYM_SOURCE_INFO, SCM_CDR(def));
2547 if (SCM_PAIRP(si) && SCM_PAIRP(SCM_CDR(si))) {
2548 ScmObj loc = Scm_PairAttrGet(SCM_PAIR(SCM_CDR(si)),
2549 SCM_SYM_SOURCE_INFO,
2550 SCM_FALSE);
2551 if (SCM_PAIRP(loc)) {
2552 Scm_PairAttrSet(SCM_PAIR(m->common.info),
2553 SCM_SYM_SOURCE_INFO,
2554 loc);
2555 }
2556 }
2557 }
2558 }
2559
2560 m->common.required = req;
2561 m->common.optional = opt;
2562 m->common.leaf = method_leaf_p(SCM_CLOSURE(body));
2563 m->generic = g;
2564 m->specializers = specarray;
2565 m->func = NULL;
2566 m->data = SCM_CLOSURE_CODE(body);
2567 m->env = SCM_CLOSURE_ENV(body);
2568
2569 SCM_METHOD_LOCKED(m) = SCM_BOOL_VALUE(locked);
2570
2571 /* NB: for comprehensive debugging & profiling information, we modify
2572 the 'name' field of the compiled code to contain
2573 (generic-name specializer-class-names ...). It may be a hazard if
2574 some existing named closure is given as BODY; as far as the standard
2575 macro is used, though, altering it should be OK. */
2576 h = t = SCM_NIL;
2577 for (int i=0; i<speclen; i++) {
2578 SCM_APPEND1(h, t, specarray[i]->name);
2579 }
2580 SCM_COMPILED_CODE(m->data)->name = Scm_Cons(SCM_PROCEDURE_INFO(g), h);
2581
2582 /* Register this method to all classes in the specializers.
2583 This has to come after the part that may throw an error. */
2584 for (int i=0; i<speclen; i++) {
2585 Scm_AddDirectMethod(specarray[i], m);
2586 }
2587 return SCM_OBJ(m);
2588 }
2589
2590 static ScmClass *method_initialize_SPEC[] = {
2591 SCM_CLASS_STATIC_PTR(Scm_MethodClass),
2592 SCM_CLASS_STATIC_PTR(Scm_ListClass)
2593 };
2594 static SCM_DEFINE_METHOD(method_initialize_rec,
2595 &Scm_GenericInitialize,
2596 2, 0,
2597 method_initialize_SPEC,
2598 method_initialize, NULL);
2599
2600 /*
2601 * Accessors
2602 */
2603 static ScmObj method_required(ScmMethod *m)
2604 {
2605 return SCM_MAKE_INT(m->common.required);
2606 }
2607
2608 static ScmObj method_optional(ScmMethod *m)
2609 {
2610 return SCM_MAKE_BOOL(m->common.optional);
2611 }
2612
2613 static ScmObj method_locked(ScmMethod *m)
2614 {
2615 return SCM_MAKE_BOOL(SCM_METHOD_LOCKED(m));
2616 }
2617
2618 static ScmObj method_leaf(ScmMethod *m)
2619 {
2620 return SCM_MAKE_BOOL(SCM_METHOD_LEAF_P(m));
2621 }
2622
2623 static ScmObj method_info(ScmMethod *m)
2624 {
2625 return m->common.info;
2626 }
2627
2628 static ScmObj method_generic(ScmMethod *m)
2629 {
2630 return m->generic ? SCM_OBJ(m->generic) : SCM_FALSE;
2631 }
2632
2633 static void method_generic_set(ScmMethod *m, ScmObj val)
2634 {
2635 if (SCM_GENERICP(val))
2636 m->generic = SCM_GENERIC(val);
2637 else
2638 Scm_Error("generic function required, but got %S", val);
2639 }
2640
2641 static ScmObj method_specializers(ScmMethod *m)
2642 {
2643 if (m->specializers) {
2644 return class_array_to_list(m->specializers, m->common.required);
2645 } else {
2646 return SCM_NIL;
2647 }
2648 }
2649
2650 static void method_specializers_set(ScmMethod *m, ScmObj val)
2651 {
2652 int len = Scm_Length(val);
2653 if (len != m->common.required)
2654 Scm_Error("specializer list doesn't match body's lambda list: %S", val);
2655 if (len == 0)
2656 m->specializers = NULL;
2657 else
2658 m->specializers = class_list_to_array(val, len);
2659 }
2660
2661 /* update-direct-method! method old-class new-class
2662 * To be called during class redefinition, and swaps reference of
2663 * old-class for new-class.
2664 *
2665 * This procedure swaps the pointer "in-place", so as far as the pointer
2666 * arithmetic is atomic, we won't have a race condition. Class
2667 * redefinition is serialized inside class-redefinition, so we won't
2668 * have the case that more than one thread call this procedure with
2669 * the same OLD pointer. It is possible that more than one thread call
2670 * this procedure on the same method simultaneously, but the OLD pointer
2671 * should differ, and it won't do any harm for them to run concurrently.
2672 *
2673 * Note that if we implement this in Scheme, we need a mutex to lock the
2674 * specializer array.
2675 */
2676 ScmObj Scm_UpdateDirectMethod(ScmMethod *m, ScmClass *old, ScmClass *newc)
2677 {
2678 int rec = SCM_PROCEDURE_REQUIRED(m);
2679 ScmClass **sp = m->specializers;
2680 for (int i=0; i<rec; i++) {
2681 if (sp[i] == old) sp[i] = newc;
2682 }
2683 if (SCM_FALSEP(Scm_Memq(SCM_OBJ(m), newc->directMethods))) {
2684 newc->directMethods = Scm_Cons(SCM_OBJ(m), newc->directMethods);
2685 }
2686 /* NB: For now, we just invalidate dispatcher. Redefining class may
2687 trigger massive update-direct-method! and it's inefficient to rebuild
2688 dispatcher table for every invocation of it.
2689 */
2690 Scm__GenericInvalidateDispatcher(m->generic);
2691 return SCM_OBJ(m);
2692 }
2693
2694 static ScmObj generic_updatedirectmethod(ScmNextMethod *nm SCM_UNUSED,
2695 ScmObj *argv,
2696 int argc SCM_UNUSED,
2697 void *data SCM_UNUSED)
2698 {
2699 return Scm_UpdateDirectMethod(SCM_METHOD(argv[0]),
2700 SCM_CLASS(argv[1]),
2701 SCM_CLASS(argv[2]));
2702 }
2703
2704 static ScmClass *generic_updatedirectmethod_SPEC[] = {
2705 SCM_CLASS_STATIC_PTR(Scm_MethodClass),
2706 SCM_CLASS_STATIC_PTR(Scm_ClassClass),
2707 SCM_CLASS_STATIC_PTR(Scm_ClassClass)
2708 };
2709 static SCM_DEFINE_METHOD(generic_updatedirectmethod_rec,
2710 &Scm_GenericUpdateDirectMethod, 3, 0,
2711 generic_updatedirectmethod_SPEC,
2712 generic_updatedirectmethod, NULL);
2713
2714 /*
2715 * ADD-METHOD, and it's default method version.
2716 */
2717 ScmObj Scm_AddMethod(ScmGeneric *gf, ScmMethod *method)
2718 {
2719 if (method->generic && method->generic != gf)
2720 Scm_Error("method %S already added to a generic function %S",
2721 method, method->generic);
2722 if (!SCM_FALSEP(Scm_Memq(SCM_OBJ(method), gf->methods)))
2723 Scm_Error("method %S already appears in a method list of generic %S"
2724 " something wrong in MOP implementation?",
2725 method, gf);
2726 if (SCM_GENERIC_SEALED_P(gf)) {
2727 Scm_Warn("Attempt to add a method to a sealed generic %S. "
2728 "You may need to recompile code that calls it.", gf);
2729 }
2730
2731 int reqs = gf->maxReqargs; /* # of maximum required args */
2732 method->generic = gf;
2733 /* pre-allocate cons pair to avoid triggering GC in the critical region */
2734 ScmObj pair = Scm_Cons(SCM_OBJ(method), gf->methods);
2735 if (SCM_PROCEDURE_REQUIRED(method) > reqs) {
2736 reqs = SCM_PROCEDURE_REQUIRED(method);
2737 }
2738
2739 /* Check if a method with the same signature exists.
2740 If so, we replace the method instead of adding it. */
2741 ScmMethod *replaced = NULL;
2742 ScmMethod *method_locked = NULL;
2743 (void)SCM_INTERNAL_MUTEX_LOCK(gf->lock);
2744 ScmObj mp;
2745 SCM_FOR_EACH(mp, gf->methods) {
2746 ScmMethod *mm = SCM_METHOD(SCM_CAR(mp));
2747 if (SCM_PROCEDURE_REQUIRED(method) == SCM_PROCEDURE_REQUIRED(mm)
2748 && SCM_PROCEDURE_OPTIONAL(method) == SCM_PROCEDURE_OPTIONAL(mm)) {
2749 ScmClass **sp1 = method->specializers;
2750 ScmClass **sp2 = mm->specializers;
2751 int i;
2752 for (i=0; i<SCM_PROCEDURE_REQUIRED(method); i++) {
2753 if (sp1[i] != sp2[i]) break;
2754 }
2755 if (i == SCM_PROCEDURE_REQUIRED(method)) {
2756 if (SCM_METHOD_LOCKED(mm)) {
2757 /* We'll throw an error */
2758 method_locked = mm;
2759 } else {
2760 replaced = mm;
2761 Scm_SetCar(mp, SCM_OBJ(method));
2762 }
2763 break;
2764 }
2765 }
2766 }
2767 if (!replaced && (method_locked == NULL)) {
2768 gf->methods = pair;
2769 gf->maxReqargs = reqs;
2770 #if GAUCHE_API_VERSION >= 98
2771 /* the type info needs to be recomputed */
2772 gf->common.typeHint = SCM_FALSE;
2773 #endif /*GAUCHE_API_VERSION >= 98*/
2774 }
2775 if (gf->dispatcher && (method_locked == NULL)) {
2776 ScmMethodDispatcher *dis = (ScmMethodDispatcher*)gf->dispatcher;
2777 if (replaced) Scm__MethodDispatcherDelete(dis, replaced);
2778 Scm__MethodDispatcherAdd(dis, method);
2779 }
2780 (void)SCM_INTERNAL_MUTEX_UNLOCK(gf->lock);
2781
2782 if (method_locked != NULL) {
2783 Scm_Error("Attempt to replace a locked method %S",
2784 SCM_OBJ(method_locked));
2785 }
2786 return SCM_UNDEFINED;
2787 }
2788
2789 static ScmObj generic_addmethod(ScmNextMethod *nm SCM_UNUSED,
2790 ScmObj *argv,
2791 int argc SCM_UNUSED,
2792 void *data SCM_UNUSED)
2793 {
2794 return Scm_AddMethod(SCM_GENERIC(argv[0]), SCM_METHOD(argv[1]));
2795 }
2796
2797 static ScmClass *generic_addmethod_SPEC[] = {
2798 SCM_CLASS_STATIC_PTR(Scm_GenericClass),
2799 SCM_CLASS_STATIC_PTR(Scm_MethodClass)
2800 };
2801 static SCM_DEFINE_METHOD(generic_addmethod_rec,
2802 &Scm_GenericAddMethod, 2, 0,
2803 generic_addmethod_SPEC,
2804 generic_addmethod, NULL);
2805
2806 /*
2807 * DELETE-METHOD, and it's default method version.
2808 */
2809 ScmObj Scm_DeleteMethod(ScmGeneric *gf, ScmMethod *method)
2810 {
2811 if (!method->generic || method->generic != gf) return SCM_UNDEFINED;
2812
2813 (void)SCM_INTERNAL_MUTEX_LOCK(gf->lock);
2814 ScmObj mp = gf->methods;
2815 if (SCM_PAIRP(mp)) {
2816 if (SCM_EQ(SCM_CAR(mp), SCM_OBJ(method))) {
2817 gf->methods = SCM_CDR(mp);
2818 method->generic = NULL;
2819 } else {
2820 while (SCM_PAIRP(SCM_CDR(mp))) {
2821 if (SCM_EQ(SCM_CADR(mp), SCM_OBJ(method))) {
2822 SCM_CDR(mp) = SCM_CDDR(mp);
2823 method->generic = NULL;
2824 break;
2825 }
2826 mp = SCM_CDR(mp);
2827 }
2828 }
2829 }
2830 if (gf->dispatcher) {
2831 Scm__MethodDispatcherDelete((ScmMethodDispatcher*)gf->dispatcher,
2832 method);
2833 }
2834 SCM_FOR_EACH(mp, gf->methods) {
2835 /* sync # of required selector */
2836 if (SCM_PROCEDURE_REQUIRED(SCM_CAR(mp)) > gf->maxReqargs) {
2837 gf->maxReqargs = SCM_PROCEDURE_REQUIRED(SCM_CAR(mp));
2838 }
2839 }
2840 (void)SCM_INTERNAL_MUTEX_UNLOCK(gf->lock);
2841 return SCM_UNDEFINED;
2842 }
2843
2844 static ScmObj generic_deletemethod(ScmNextMethod *nm SCM_UNUSED,
2845 ScmObj *argv,
2846 int argc SCM_UNUSED,
2847 void *data SCM_UNUSED)
2848 {
2849 return Scm_DeleteMethod(SCM_GENERIC(argv[0]), SCM_METHOD(argv[1]));
2850 }
2851
2852 static ScmClass *generic_deletemethod_SPEC[] = {
2853 SCM_CLASS_STATIC_PTR(Scm_GenericClass),
2854 SCM_CLASS_STATIC_PTR(Scm_MethodClass)
2855 };
2856 static SCM_DEFINE_METHOD(generic_deletemethod_rec,
2857 &Scm_GenericDeleteMethod, 2, 0,
2858 generic_deletemethod_SPEC,
2859 generic_deletemethod, NULL);
2860
2861 /*=====================================================================
2862 * Next Method
2863 */
2864
2865 ScmObj Scm_MakeNextMethod(ScmGeneric *gf, ScmObj methods,
2866 ScmObj *argv, int argc, int copyargs, int applyargs)
2867 {
2868 ScmNextMethod *nm = SCM_NEW(ScmNextMethod);
2869 SCM_SET_CLASS(nm, SCM_CLASS_NEXT_METHOD);
2870 Scm__ProcedureInit(SCM_PROCEDURE(nm), SCM_PROC_NEXT_METHOD, 0, 0, SCM_FALSE);
2871 nm->generic = gf;
2872 nm->methods = methods;
2873 if (copyargs) {
2874 nm->argv = SCM_NEW_ARRAY(ScmObj, argc);
2875 memcpy(nm->argv, argv, sizeof(ScmObj)*argc);
2876 } else {
2877 nm->argv = argv;
2878 }
2879 nm->argc = argc;
2880 nm->applyargs = applyargs;
2881 return SCM_OBJ(nm);
2882 }
2883
2884 static void next_method_print(ScmObj obj, ScmPort *out,
2885 ScmWriteContext *ctx SCM_UNUSED)
2886 {
2887 ScmNextMethod *nm = SCM_NEXT_METHOD(obj);
2888 ScmObj args = Scm_ArrayToList(nm->argv, nm->argc);
2889 Scm_Printf(out, "#<next-method %S%d %S>", nm->methods, nm->applyargs, args);
2890 }
2891
2892 /*=====================================================================
2893 * Accessor Method
2894 */
2895
2896 static void accessor_method_print(ScmObj obj, ScmPort *port,
2897 ScmWriteContext *ctx SCM_UNUSED)
2898 {
2899 Scm_Printf(port, "#<accessor-method %S>", SCM_METHOD(obj)->common.info);
2900 }
2901
2902 static ScmObj accessor_get_proc(ScmNextMethod *nm SCM_UNUSED,
2903 ScmObj *argv,
2904 int argc SCM_UNUSED,
2905 void *data SCM_UNUSED)
2906 {
2907 ScmObj obj = argv[0];
2908 ScmSlotAccessor *ca = (ScmSlotAccessor*)data;
2909 /* NB: we need this extra check, in case if the getter method of parent
2910 class and the one of subclass don't share the generic function, and
2911 the getter method of parent class is called on the subclass's instance.
2912 See test/object.scm "module and accessor" section for a concrete
2913 example. */
2914 if (!SCM_EQ(Scm_ClassOf(obj), ca->klass)) {
2915 /* fallback to a normal protocol */
2916 return Scm_VMSlotRef(obj, ca->name, FALSE);
2917 }
2918 /* Standard path. We can skip searching the slot, so it is faster. */
2919 return slot_ref_using_accessor(obj, ca, FALSE);
2920 }
2921
2922 static ScmObj accessor_set_proc(ScmNextMethod *nm SCM_UNUSED,
2923 ScmObj *argv,
2924 int argc SCM_UNUSED,
2925 void *data SCM_UNUSED)
2926 {
2927 ScmObj obj = argv[0];
2928 ScmObj val = argv[1];
2929 ScmSlotAccessor *ca = (ScmSlotAccessor*)data;
2930 /* See the comment in accessor_get_proc above about this check. */
2931 if (!SCM_EQ(Scm_ClassOf(obj), ca->klass)) {
2932 return Scm_VMSlotSet(obj, ca->name, val);
2933 }
2934 return slot_set_using_accessor(obj, ca, val);
2935 }
2936
2937 /* Accessor method can be just created by usual allocate/initialize
2938 sequence. But it requires :slot-accessor initarg. The method body
2939 is overridden by C function, and the closure given to :body doesn't
2940 have an effect. */
2941 static ScmObj accessor_method_initialize(ScmNextMethod *nm, ScmObj *argv,
2942 int argc, void *data)
2943 {
2944 ScmMethod *m = SCM_METHOD(method_initialize(nm, argv, argc, data));
2945 ScmObj initargs = argv[1];
2946 ScmObj sa = Scm_GetKeyword(key_slot_accessor, initargs, SCM_FALSE);
2947
2948 if (!SCM_SLOT_ACCESSOR_P(sa)) {
2949 Scm_Error("slot accessor required for :slot-accessor argument: %S",
2950 sa);
2951 }
2952
2953 m->data = sa;
2954 switch (SCM_PROCEDURE_REQUIRED(m)) {
2955 case 1: /* accessor <obj> - this is a getter */
2956 m->func = accessor_get_proc;
2957 break;
2958 case 2: /* accessor <obj> <val> - this is a setter */
2959 m->func = accessor_set_proc;
2960 break;
2961 default:
2962 Scm_Error("bad initialization parameter for accessor method %S", m);
2963 }
2964 return SCM_OBJ(m);
2965 }
2966
2967 static ScmClass *accessor_method_initialize_SPEC[] = {
2968 SCM_CLASS_STATIC_PTR(Scm_AccessorMethodClass),
2969 SCM_CLASS_STATIC_PTR(Scm_ListClass)
2970 };
2971 static SCM_DEFINE_METHOD(accessor_method_initialize_rec,
2972 &Scm_GenericInitialize,
2973 2, 0,
2974 accessor_method_initialize_SPEC,
2975 accessor_method_initialize, NULL);
2976
2977 static ScmObj accessor_method_slot_accessor(ScmAccessorMethod *m)
2978 {
2979 SCM_ASSERT(SCM_SLOT_ACCESSOR_P(m->data));
2980 return SCM_OBJ(m->data);
2981 }
2982
2983 static void accessor_method_slot_accessor_set(ScmAccessorMethod *m, ScmObj v)
2984 {
2985 if (!SCM_SLOT_ACCESSOR_P(v)) {
2986 Scm_Error("slot accessor required, but got %S", v);
2987 }
2988 m->data = v;
2989 }
2990
2991 /*=====================================================================
2992 * Foreign pointer mechanism
2993 */
2994
2995 /* foreign pointer instance flags */
2996 enum {
2997 SCM_FOREIGN_POINTER_INVALID = (1L<<0) /* The pointer is no longer valid. */
2998 };
2999
3000 struct foreign_data_rec {
3001 int flags;
3002 ScmForeignCleanupProc cleanup;
3003 ScmInternalMutex attr_mutex; /* lock for updating foreign pointer's
3004 "attribute" slot. we use one-per-class
3005 mutex, assuming the mutation of attrs
3006 is rare and saving space per foreign
3007 pointer is more important. */
3008 ScmHashCore *identity_map; /* for KEEP_IDENTITY */
3009 ScmInternalMutex identity_mutex; /* lock for identity_map */
3010 };
3011
3012 /* This is used if custom printer isn't given to
3013 Scm_MakeForeignPointerClass. */
3014 static void fptr_print(ScmObj obj, ScmPort *port,
3015 ScmWriteContext *ctx SCM_UNUSED)
3016 {
3017 ScmForeignPointer *fptr = SCM_FOREIGN_POINTER(obj);
3018 ScmClass *klass = SCM_CLASS_OF(obj);
3019 ScmObj name = Scm_ForeignPointerAttrGet(fptr, SCM_SYM_NAME, SCM_UNDEFINED);
3020 void *ptr = SCM_FOREIGN_POINTER_REF(void*, fptr);
3021 if (SCM_UNDEFINEDP(name)) {
3022 Scm_Printf(port, "#<%A @%p>", Scm_ShortClassName(klass), ptr);
3023 } else {
3024 Scm_Printf(port, "#<%A %S@%p>", Scm_ShortClassName(klass), name, ptr);
3025 }
3026 }
3027
3028 /* Compare and hash methods are common to all subclasses */
3029 static int fptr_compare(ScmObj x, ScmObj y, int equalp)
3030 {
3031 ScmForeignPointer *fx = SCM_FOREIGN_POINTER(x);
3032 ScmForeignPointer *fy = SCM_FOREIGN_POINTER(y);
3033 void *px = SCM_FOREIGN_POINTER_REF(void*, fx);
3034 void *py = SCM_FOREIGN_POINTER_REF(void*, fy);
3035 if (equalp) {
3036 return px == py;
3037 } else {
3038 if (px < py) return -1;
3039 if (px > py) return 1;
3040 return 0;
3041 }
3042 }
3043
3044 static ScmSmallInt fptr_hash(ScmObj obj, ScmSmallInt salt, u_long flags)
3045 {
3046 ScmForeignPointer *fptr = SCM_FOREIGN_POINTER(obj);
3047 /* no portable hashing of fptr is possible, except that we return
3048 a constant value. So we ignore fptr in such case. */
3049 void *ptr = ((flags & SCM_HASH_PORTABLE)
3050 ? NULL
3051 : SCM_FOREIGN_POINTER_REF(void*, fptr));
3052 /* See hash.c for the magic number */
3053 return (ScmSmallInt)(((intptr_t)ptr + salt) * 2654435761UL);
3054 }
3055
3056 ScmClass *Scm_MakeForeignPointerClass(ScmModule *mod,
3057 const char *name,
3058 ScmClassPrintProc print_proc,
3059 ScmForeignCleanupProc cleanup_proc,
3060 int flags)
3061 {
3062 ScmClass *fp = (ScmClass*)class_allocate(SCM_CLASS_CLASS, SCM_NIL);
3063 ScmObj s = SCM_INTERN(name);
3064 struct foreign_data_rec *data = SCM_NEW(struct foreign_data_rec);
3065 /* NB: here we don't need to use SCM_CLASS_STATIC_PTR, since we only
3066 refer intra-dll classes, and we don't go through init_class.
3067 If we ever find the need to go through init_class, do not forget
3068 to change fpcpa initializers as well, to make it work on windows. */
3069 static ScmClass *fpcpa[] = { SCM_CLASS_FOREIGN_POINTER,
3070 SCM_CLASS_TOP,
3071 NULL };
3072 fp->name = s;
3073 fp->allocate = NULL;
3074 fp->print = (print_proc? print_proc : fptr_print);
3075 fp->compare = fptr_compare;
3076 fp->hash = fptr_hash;
3077 fp->cpa = fpcpa;
3078 fp->flags = SCM_CLASS_BUILTIN;
3079 initialize_builtin_cpl(fp, SCM_FALSE);
3080 Scm_Define(mod, SCM_SYMBOL(s), SCM_OBJ(fp));
3081 fp->slots = SCM_NIL;
3082 fp->accessors = SCM_NIL;
3083 data->flags = flags;
3084 data->cleanup = cleanup_proc;
3085 (void)SCM_INTERNAL_MUTEX_INIT(data->attr_mutex);
3086 if (flags & SCM_FOREIGN_POINTER_KEEP_IDENTITY) {
3087 (void)SCM_INTERNAL_MUTEX_INIT(data->identity_mutex);
3088 data->identity_map = SCM_NEW(ScmHashCore);
3089 Scm_HashCoreInitSimple(data->identity_map, SCM_HASH_WORD, 256, NULL);
3090 } else {
3091 data->identity_map = NULL;
3092 }
3093 fp->data = (void*)data; /* see the note above class_allocate() */
3094 return fp;
3095 }
3096
3097 static void fp_finalize(ScmObj obj, void *data)
3098 {
3099 void (*cleanup)(ScmObj) = (void (*)(ScmObj))data;
3100 cleanup(obj);
3101 }
3102
3103 /* This shouldn't raise an error. */
3104 static ScmForeignPointer *make_foreign_int(ScmClass *klass, void *ptr,
3105 ScmObj attr,
3106 struct foreign_data_rec *data)
3107 {
3108 ScmForeignPointer *obj = SCM_NEW(ScmForeignPointer);
3109 SCM_SET_CLASS(obj, klass);
3110 obj->ptr = ptr;
3111 obj->attributes = attr;
3112 obj->flags = 0;
3113 if (data->cleanup) {
3114 Scm_RegisterFinalizer(SCM_OBJ(obj), fp_finalize, data->cleanup);
3115 }
3116 return obj;
3117 }
3118
3119 /* Note for future API: Scm_MakeForeignPointer should take attr argument.
3120 We add *WithAttr only to keep ABI compatibility. */
3121 ScmObj Scm_MakeForeignPointer(ScmClass *klass, void *ptr)
3122 {
3123 return Scm_MakeForeignPointerWithAttr(klass, ptr, SCM_NIL);
3124 }
3125
3126 ScmObj Scm_MakeForeignPointerWithAttr(ScmClass *klass, void *ptr, ScmObj attr)
3127 {
3128 ScmForeignPointer *obj;
3129 struct foreign_data_rec *data = (struct foreign_data_rec *)klass->data;
3130 SCM_ASSERT(data != NULL); /* if this happens, user directly uses
3131 <foreign-pointer> class without subclassing*/
3132
3133 if (!klass) { /* for extra safety */
3134 Scm_Error("NULL pointer passed to Scm_MakeForeignPointer");
3135 }
3136 if (!Scm_SubclassP(klass, SCM_CLASS_FOREIGN_POINTER)) {
3137 Scm_Error("attempt to instantiate non-foreign-pointer class %S via Scm_MakeForeignPointer", klass);
3138 }
3139
3140 if (ptr == NULL && (data->flags & SCM_FOREIGN_POINTER_MAP_NULL)) {
3141 return SCM_FALSE;
3142 }
3143
3144 if (data->identity_map) {
3145 (void)SCM_INTERNAL_MUTEX_LOCK(data->identity_mutex);
3146 ScmDictEntry *e = Scm_HashCoreSearch(data->identity_map,
3147 (intptr_t)ptr, SCM_DICT_CREATE);
3148 if (e->value) {
3149 if (Scm_WeakBoxEmptyP((ScmWeakBox*)e->value)) {
3150 obj = make_foreign_int(klass, ptr, attr, data);
3151 Scm_WeakBoxSet((ScmWeakBox*)e->value, obj);
3152 } else {
3153 obj = (ScmForeignPointer*)Scm_WeakBoxRef((ScmWeakBox*)e->value);
3154 }
3155 } else {
3156 obj = make_foreign_int(klass, ptr, attr, data);
3157 e->value = (intptr_t)Scm_MakeWeakBox(obj);
3158 }
3159 (void)SCM_INTERNAL_MUTEX_UNLOCK(data->identity_mutex);
3160 } else {
3161 obj = make_foreign_int(klass, ptr, attr, data);
3162 }
3163 return SCM_OBJ(obj);
3164 }
3165
3166 void *Scm_ForeignPointerRef(ScmForeignPointer *fp)
3167 {
3168 if (Scm_ForeignPointerInvalidP(fp)) {
3169 Scm_Error("attempt to dereference a foreign pointer "
3170 "that is no longer valid: %S", SCM_OBJ(fp));
3171
3172 }
3173 return fp->ptr;
3174 }
3175
3176 int Scm_ForeignPointerInvalidP(ScmForeignPointer *fp)
3177 {
3178 return (fp->flags & SCM_FOREIGN_POINTER_INVALID);
3179 }
3180
3181 void Scm_ForeignPointerInvalidate(ScmForeignPointer *fp)
3182 {
3183 fp->flags |= SCM_FOREIGN_POINTER_INVALID;
3184 }
3185
3186 ScmObj Scm_ForeignPointerAttr(ScmForeignPointer *fp)
3187 {
3188 return fp->attributes;
3189 }
3190
3191 ScmObj Scm_ForeignPointerAttrGet(ScmForeignPointer *fp,
3192 ScmObj key, ScmObj fallback)
3193 {
3194 /* no need to lock, for AttrSet won't make fp->attributes inconsisnent
3195 at any moment. */
3196 ScmObj p = Scm_Assq(key, fp->attributes);
3197 if (SCM_PAIRP(p)) return SCM_CDR(p);
3198 if (SCM_UNBOUNDP(fallback)) {
3199 Scm_Error("No value associated with key %S in a foreign pointer %S",
3200 key, SCM_OBJ(fp));
3201 }
3202 return fallback;
3203 }
3204
3205 ScmObj Scm_ForeignPointerAttrSet(ScmForeignPointer *fp,
3206 ScmObj key, ScmObj value)
3207 {
3208 struct foreign_data_rec *data
3209 = (struct foreign_data_rec*)(SCM_CLASS_OF(fp)->data);
3210
3211 /* NB: We presume mutating foreign pointer attributes is rare operation,
3212 so we don't try hard to make it efficient. Particularly, we use
3213 one mutex shared among all instances of the same class, in order to
3214 keep the size of each foreign pointer instance small. We'll reconsider
3215 the design if the performance ever becomes a problem. */
3216 (void)SCM_INTERNAL_MUTEX_LOCK(data->attr_mutex);
3217 ScmObj r = SCM_UNDEFINED;
3218 ScmObj p = Scm_Assq(key, fp->attributes);
3219 if (SCM_PAIRP(p)) {
3220 SCM_SET_CDR_UNCHECKED(p, value);
3221 r = value;
3222 } else {
3223 fp->attributes = Scm_Acons(key, value, fp->attributes);
3224 }
3225 (void)SCM_INTERNAL_MUTEX_UNLOCK(data->attr_mutex);
3226 return r;
3227 }
3228
3229 /*=====================================================================
3230 * Proxy type
3231 */
3232
3233 static void proxy_type_print(ScmObj obj, ScmPort *port,
3234 ScmWriteContext *ctx SCM_UNUSED)
3235 {
3236 ScmIdentifier *id = SCM_PROXY_TYPE(obj)->id;
3237 ScmGloc *g = SCM_PROXY_TYPE(obj)->ref;
3238 if (g == NULL || Scm_GlocPhantomBindingP(g)) {
3239 Scm_Printf(port, "#<%A (unresolved)>", id->name);
3240 } else {
3241 Scm_Printf(port, "#<%A>", id->name);
3242 }
3243 }
3244
3245 static int proxy_type_compare(ScmObj x, ScmObj y, int equalp)
3246 {
3247 SCM_ASSERT(SCM_PROXY_TYPE_P(x));
3248 SCM_ASSERT(SCM_PROXY_TYPE_P(y));
3249 ScmClass *cx = Scm_ProxyTypeRef(SCM_PROXY_TYPE(x));
3250 ScmClass *cy = Scm_ProxyTypeRef(SCM_PROXY_TYPE(y));
3251 if (equalp) {
3252 return cx != cy;
3253 } else {
3254 /* delegate comparison to generic function */
3255 return Scm_ObjectCompare(SCM_OBJ(cx), SCM_OBJ(cy), FALSE);
3256 }
3257 }
3258
3259 static ScmClass *proxy_type_get_class(ScmIdentifier *id, ScmGloc *ref)
3260 {
3261 ScmObj klass = Scm_GlocGetValue(ref);
3262 if (!SCM_ISA(klass, SCM_CLASS_CLASS)) {
3263 Scm_Error("Identifier %S wrapped by a proxy-type has to be bound "
3264 "to a class, but it is bound to %S. This shouldn't happen.",
3265 id, klass);
3266 }
3267 return SCM_CLASS(klass);
3268 }
3269
3270 ScmObj Scm_MakeProxyType(ScmIdentifier *id, ScmGloc *ref)
3271 {
3272 /* If REF != NULL, it must be the binding of ID. We trust the caller,
3273 and just check that it is bound to a class. */
3274 if (ref != NULL) {
3275 (void)proxy_type_get_class(id, ref); /* detect error early */
3276 }
3277 ScmProxyType *p = SCM_NEW(ScmProxyType);
3278 SCM_SET_CLASS(p, SCM_CLASS_PROXY_TYPE);
3279 p->id = id;
3280 p->ref = ref;
3281 return SCM_OBJ(p);
3282 }
3283
3284 ScmClass *Scm_ProxyTypeRef(ScmProxyType *p)
3285 {
3286 if (p->ref == NULL) {
3287 /* Lazily get the binding. This is idempotent operation as long as
3288 p->id is already bound, so MT-safe. */
3289 ScmGloc *g = Scm_IdentifierGlobalBinding(p->id);
3290 if (g == NULL || Scm_GlocPhantomBindingP(g)) {
3291 Scm_Error("Identifier wrapped by a proxy-type is unbound: %S",
3292 SCM_OBJ(p->id));
3293 }
3294 p->ref = g;
3295 }
3296 return proxy_type_get_class(p->id, p->ref);
3297 }
3298
3299 ScmObj Scm_ProxyTypeId(ScmProxyType *p)
3300 {
3301 return SCM_OBJ(p->id);
3302 }
3303
3304 /*=====================================================================
3305 * Class initialization
3306 */
3307
3308 /* TODO: need a cleaner way! */
3309 /* static declaration of some structures */
3310
3311 static ScmClassStaticSlotSpec class_slots[] = {
3312 SCM_CLASS_SLOT_SPEC("name", class_name, class_name_set),
3313 SCM_CLASS_SLOT_SPEC("cpl", class_cpl, class_cpl_set),
3314 SCM_CLASS_SLOT_SPEC("direct-supers", class_direct_supers, class_direct_supers_set),
3315 SCM_CLASS_SLOT_SPEC("accessors", class_accessors, class_accessors_set),
3316 SCM_CLASS_SLOT_SPEC("slots", class_slots_ref, class_slots_set),
3317 SCM_CLASS_SLOT_SPEC("direct-slots", class_direct_slots, class_direct_slots_set),
3318 SCM_CLASS_SLOT_SPEC("num-instance-slots", class_numislots, class_numislots_set),
3319 SCM_CLASS_SLOT_SPEC("direct-subclasses", class_direct_subclasses, NULL),
3320 SCM_CLASS_SLOT_SPEC("direct-methods", class_direct_methods, NULL),
3321 SCM_CLASS_SLOT_SPEC("initargs", class_initargs, class_initargs_set),
3322 SCM_CLASS_SLOT_SPEC("defined-modules", class_defined_modules, class_defined_modules_set),
3323 SCM_CLASS_SLOT_SPEC("redefined", class_redefined, NULL),
3324 SCM_CLASS_SLOT_SPEC("category", class_category, NULL),
3325 SCM_CLASS_SLOT_SPEC("core-size", class_core_size, NULL),
3326 SCM_CLASS_SLOT_SPEC_END()
3327 };
3328
3329 static ScmClassStaticSlotSpec generic_slots[] = {
3330 SCM_CLASS_SLOT_SPEC("name", generic_name, generic_name_set),
3331 SCM_CLASS_SLOT_SPEC("sealed", generic_sealed, NULL),
3332 SCM_CLASS_SLOT_SPEC("info", generic_info, NULL),
3333 SCM_CLASS_SLOT_SPEC("methods", generic_methods, generic_methods_set),
3334 SCM_CLASS_SLOT_SPEC_END()
3335 };
3336
3337 static ScmClassStaticSlotSpec method_slots[] = {
3338 SCM_CLASS_SLOT_SPEC("required", method_required, NULL),
3339 SCM_CLASS_SLOT_SPEC("optional", method_optional, NULL),
3340 SCM_CLASS_SLOT_SPEC("method-locked", method_locked, NULL),
3341 SCM_CLASS_SLOT_SPEC("leaf?", method_leaf, NULL),
3342 SCM_CLASS_SLOT_SPEC("generic", method_generic, method_generic_set),
3343 SCM_CLASS_SLOT_SPEC("specializers", method_specializers, method_specializers_set),
3344 SCM_CLASS_SLOT_SPEC("info", method_info, NULL),
3345 SCM_CLASS_SLOT_SPEC_END()
3346 };
3347
3348 static ScmClassStaticSlotSpec accessor_method_slots[] = {
3349 SCM_CLASS_SLOT_SPEC("required", method_required, NULL),
3350 SCM_CLASS_SLOT_SPEC("optional", method_optional, NULL),
3351 SCM_CLASS_SLOT_SPEC("method-locked", method_locked, NULL),
3352 SCM_CLASS_SLOT_SPEC("leaf?", method_leaf, NULL),
3353 SCM_CLASS_SLOT_SPEC("generic", method_generic, method_generic_set),
3354 SCM_CLASS_SLOT_SPEC("specializers", method_specializers, method_specializers_set),
3355 SCM_CLASS_SLOT_SPEC("slot-accessor", accessor_method_slot_accessor, accessor_method_slot_accessor_set),
3356 SCM_CLASS_SLOT_SPEC_END()
3357 };
3358
3359 static ScmClassStaticSlotSpec slot_accessor_slots[] = {
3360 SCM_CLASS_SLOT_SPEC("class", slot_accessor_class,
3361 slot_accessor_class_set),
3362 SCM_CLASS_SLOT_SPEC("name", slot_accessor_name,
3363 slot_accessor_name_set),
3364 SCM_CLASS_SLOT_SPEC("init-value", slot_accessor_init_value,
3365 slot_accessor_init_value_set),
3366 SCM_CLASS_SLOT_SPEC("init-keyword", slot_accessor_init_keyword,
3367 slot_accessor_init_keyword_set),
3368 SCM_CLASS_SLOT_SPEC("init-thunk", slot_accessor_init_thunk,
3369 slot_accessor_init_thunk_set),
3370 SCM_CLASS_SLOT_SPEC("immutable", slot_accessor_immutable,
3371 slot_accessor_immutable_set),
3372 SCM_CLASS_SLOT_SPEC("initializable", slot_accessor_initializable,
3373 slot_accessor_initializable_set),
3374 SCM_CLASS_SLOT_SPEC("settable", slot_accessor_settable,
3375 NULL),
3376 SCM_CLASS_SLOT_SPEC("slot-number", slot_accessor_slot_number,
3377 slot_accessor_slot_number_set),
3378 SCM_CLASS_SLOT_SPEC("getter", slot_accessor_scheme_getter,
3379 slot_accessor_scheme_getter_set),
3380 SCM_CLASS_SLOT_SPEC("setter", slot_accessor_scheme_setter,
3381 slot_accessor_scheme_setter_set),
3382 SCM_CLASS_SLOT_SPEC("bound?", slot_accessor_scheme_boundp,
3383 slot_accessor_scheme_boundp_set),
3384 SCM_CLASS_SLOT_SPEC_END()
3385 };
3386
3387 /*
3388 * Sets up CPL from CPA
3389 */
3390 static void initialize_builtin_cpl(ScmClass *klass, ScmObj supers)
3391 {
3392 ScmObj h = SCM_NIL, t = SCM_NIL;
3393
3394 SCM_APPEND1(h, t, SCM_OBJ(klass));
3395 for (ScmClass **p = klass->cpa; *p; p++) SCM_APPEND1(h, t, SCM_OBJ(*p));
3396 klass->cpl = h;
3397 if (SCM_PAIRP(supers)) {
3398 /* Check validity of the given supers. */
3399 ScmObj cp, sp = supers;
3400 SCM_FOR_EACH(cp, klass->cpl) {
3401 if (SCM_EQ(SCM_CAR(cp), SCM_CAR(sp))) {
3402 sp = SCM_CDR(sp);
3403 if (SCM_NULLP(sp)) break;
3404 }
3405 }
3406 if (!SCM_NULLP(sp)) {
3407 /* NB: At this point we may not have initialized error handing
3408 mechanism, so we have no option but quit. */
3409 const char *cname = "(unnamed class)";
3410 if (SCM_STRINGP(klass->name)) {
3411 cname = Scm_GetStringConst(SCM_STRING(klass->name));
3412 }
3413 Scm_Panic("Class %s is being initialized with inconsistent super class list. Must be an implementation error. Report to the author.", cname);
3414 }
3415 klass->directSupers = supers;
3416 } else if (SCM_PAIRP(SCM_CDR(h))) {
3417 /* Default: take the next class of CPL as the only direct super */
3418 klass->directSupers = SCM_LIST1(SCM_CADR(h));
3419 } else {
3420 /* Should this happen? */
3421 klass->directSupers = SCM_NIL;
3422 }
3423 }
3424
3425 /*
3426 * Special handling for static metaclass initialization
3427 * If a metaclass is statically allocated (via define-cclass)
3428 * and it doesn't have a custom allocate handler, we need
3429 * the default class allocator which isn't public.
3430 */
3431 static void ensure_metaclass_allocate(ScmClass *klass)
3432 {
3433 for (int i = 0; klass->cpa[i] != NULL; i++) {
3434 if (SCM_EQ(klass->cpa[i], SCM_CLASS_CLASS)) {
3435 if (klass->allocate == NULL) {
3436 klass->allocate = class_allocate;
3437 }
3438 return;
3439 }
3440 }
3441 }
3442
3443 /*
3444 * A common part for builtin class initialization
3445 */
3446 static void init_class(ScmClass *klass,
3447 const char *name,
3448 ScmModule *mod,
3449 ScmObj supers, /* SCM_FALSE if using default */
3450 ScmClassStaticSlotSpec *specs,
3451 int flags SCM_UNUSED) /* reserved */
3452 {
3453 ScmObj slots = SCM_NIL, t = SCM_NIL, acc = SCM_NIL;
3454
3455 /* initialize CPL and directSupers */
3456 if (klass->cpa == NULL) {
3457 klass->cpa = SCM_CLASS_DEFAULT_CPL;
3458 }
3459
3460 klass->name = SCM_INTERN(name);
3461 initialize_builtin_cpl(klass, supers);
3462
3463 klass->modules = SCM_LIST1(SCM_OBJ(mod)); /* defined module */
3464
3465 /* if the class is a metaclass, ensure proper allocate */
3466 ensure_metaclass_allocate(klass);
3467
3468 /* On Windows, mutex and cv must be initialized at runtime. */
3469 SCM_INTERNAL_MUTEX_INIT(klass->mutex);
3470 SCM_INTERNAL_COND_INIT(klass->cv);
3471
3472 /* insert binding */
3473 Scm_MakeBinding(mod, SCM_SYMBOL(klass->name), SCM_OBJ(klass),
3474 SCM_BINDING_INLINABLE);
3475
3476 /* initialize direct slots */
3477 if (specs) {
3478 for (;specs->name; specs++) {
3479 ScmObj snam = SCM_INTERN(specs->name);
3480 specs->accessor.klass = klass;
3481 specs->accessor.name = snam;
3482 acc = Scm_Acons(snam, SCM_OBJ(&specs->accessor), acc);
3483 specs->accessor.initKeyword = SCM_MAKE_KEYWORD(specs->name);
3484 SCM_APPEND1(slots, t,
3485 Scm_List(snam,
3486 key_allocation, key_builtin,
3487 key_slot_accessor, SCM_OBJ(&specs->accessor),
3488 NULL));
3489 }
3490 }
3491 klass->directSlots = slots;
3492
3493 /* compute other slots inherited from supers */
3494 for (ScmClass **super = klass->cpa; *super; super++) {
3495 ScmObj sp;
3496 SCM_FOR_EACH(sp, (*super)->directSlots) {
3497 ScmObj slot = SCM_CAR(sp), snam, p, a;
3498 SCM_ASSERT(SCM_PAIRP(slot));
3499 snam = SCM_CAR(slot);
3500 p = Scm_Assq(snam, slots);
3501 if (SCM_FALSEP(p)) {
3502 slots = Scm_Cons(Scm_CopyList(slot), slots);
3503 a = Scm_GetKeyword(key_slot_accessor, SCM_CDR(slot), SCM_FALSE);
3504 SCM_ASSERT(SCM_HOBJP(a));
3505 SCM_ASSERT(SCM_SLOT_ACCESSOR_P(a));
3506 acc = Scm_Acons(snam, a, acc);
3507 }
3508 }
3509 }
3510 klass->slots = slots;
3511 klass->accessors = acc;
3512 }
3513
3514 /*
3515 * Inter-module API
3516 */
3517
3518 /* The most standard way to initialize a class. */
3519 void Scm_InitStaticClass(ScmClass *klass,
3520 const char *name,
3521 ScmModule *mod,
3522 ScmClassStaticSlotSpec *specs,
3523 int flags) /* reserved */
3524 {
3525 init_class(klass, name, mod, SCM_FALSE, specs, flags);
3526 }
3527
3528 /* If the builtin class needs multiple inheritance... */
3529 void Scm_InitStaticClassWithSupers(ScmClass *klass,
3530 const char *name,
3531 ScmModule *mod,
3532 ScmObj supers,
3533 ScmClassStaticSlotSpec *specs,
3534 int flags) /* reserved */
3535 {
3536 init_class(klass, name, mod, supers, specs, flags);
3537 }
3538
3539 /* A special initialization for some of builtin classes.
3540 Sets klass's metaclass to META. If META is NULL, a new metaclass
3541 (whose name has "-meta" after the original class name except brackets)
3542 is created automatically. This procedure should be used only if
3543 metaclass is absolutely required (e.g. all condition classes should
3544 be an instance of <condition-meta>). The older version of Gauche
3545 has metaclasses for many builtin classes, which is a compensation of
3546 lack of eqv-method specializer; such use of metaclass is deprecated
3547 and will be removed in future. */
3548 void Scm_InitStaticClassWithMeta(ScmClass *klass,
3549 const char *name,
3550 ScmModule *mod,
3551 ScmClass *meta,
3552 ScmObj supers,
3553 ScmClassStaticSlotSpec *specs,
3554 int flags)
3555 {
3556 init_class(klass, name, mod, supers, specs, flags);
3557
3558 if (meta) {
3559 SCM_SET_CLASS(klass, meta);
3560 } else {
3561 int nlen = (int)strlen(name);
3562 char *metaname = SCM_NEW_ATOMIC2(char *, nlen + 6);
3563
3564 if (name[nlen - 1] == '>') {
3565 strncpy(metaname, name, nlen-1);
3566 strcpy(metaname+nlen-1, "-meta>");
3567 } else {
3568 strcpy(metaname, name);
3569 strcat(metaname, "-meta");
3570 }
3571 SCM_SET_CLASS(klass, make_implicit_meta(metaname, klass->cpa, mod));
3572 }
3573 }
3574
3575 /* TRANSIENT: The old API - deprecated. We keep this around for a while
3576 for backward compatibility. */
3577 #if GAUCHE_API_VERSION < 1000
3578 void Scm_InitBuiltinClass(ScmClass *klass, const char *name,
3579 ScmClassStaticSlotSpec *specs,
3580 int withMeta, ScmModule *mod)
3581 {
3582 if (withMeta) {
3583 Scm_InitStaticClassWithMeta(klass, name, mod, NULL, SCM_FALSE, specs, 0);
3584 } else {
3585 Scm_InitStaticClass(klass, name, mod, specs, 0);
3586 }
3587 }
3588 #endif /*GAUCHE_API_VERSION < 1000*/
3589
3590 void Scm_InitBuiltinGeneric(ScmGeneric *gf, const char *name, ScmModule *mod)
3591 {
3592 ScmObj s = SCM_INTERN(name);
3593 gf->common.info = s;
3594 if (gf->fallback == NULL) {
3595 gf->fallback = Scm_NoNextMethod;
3596 }
3597 (void)SCM_INTERNAL_MUTEX_INIT(gf->lock);
3598 Scm_Define(mod, SCM_SYMBOL(s), SCM_OBJ(gf));
3599 }
3600
3601 void Scm_InitBuiltinMethod(ScmMethod *m)
3602 {
3603 m->common.info = Scm_Cons(m->generic->common.info,
3604 class_array_to_names(m->specializers,
3605 m->common.required));
3606 Scm_AddMethod(m->generic, m);
3607 }
3608
3609 void Scm__InitClass(void)
3610 {
3611 ScmModule *mod = Scm_GaucheModule();
3612 static ScmClass *nullcpa[1] = {NULL}; /* for <top> */
3613
3614 key_allocation = SCM_MAKE_KEYWORD("allocation");
3615 key_builtin = SCM_MAKE_KEYWORD("builtin");
3616 key_slot_accessor = SCM_MAKE_KEYWORD("slot-accessor");
3617 key_name = SCM_MAKE_KEYWORD("name");
3618 key_lambda_list = SCM_MAKE_KEYWORD("lambda-list");
3619 key_generic = SCM_MAKE_KEYWORD("generic");
3620 key_method_locked = SCM_MAKE_KEYWORD("method-locked");
3621 key_specializers = SCM_MAKE_KEYWORD("specializers");
3622 key_body = SCM_MAKE_KEYWORD("body");
3623
3624 (void)SCM_INTERNAL_MUTEX_INIT(class_redefinition_lock.mutex);
3625 (void)SCM_INTERNAL_COND_INIT(class_redefinition_lock.cv);
3626
3627 if (Scm_GetEnv("GAUCHE_DISABLE_GENERIC_DISPATCHER") != NULL) {
3628 disable_generic_dispatcher = TRUE;
3629 }
3630
3631 /* booting class metaobject */
3632 Scm_TopClass.cpa = nullcpa;
3633
3634 #define BINIT(cl, nam, slots) \
3635 Scm_InitStaticClass(cl, nam, mod, slots, 0)
3636
3637 #define CINIT(cl, nam) \
3638 Scm_InitStaticClassWithMeta(cl, nam, mod, NULL, SCM_FALSE, NULL, 0)
3639
3640 /* Need to initialize these first */
3641 BINIT(SCM_CLASS_TYPE, "<type>", NULL);
3642 BINIT(SCM_CLASS_CLASS, "<class>", class_slots);
3643 BINIT(SCM_CLASS_TOP, "<top>", NULL);
3644 BINIT(SCM_CLASS_BOTTOM, "<bottom>", NULL);
3645
3646 /* box.c */
3647 CINIT(SCM_CLASS_BOX, "<box>");
3648 CINIT(SCM_CLASS_MVBOX, "<mv-box>");
3649 BINIT(SCM_CLASS_ATOMIC_BASE, "<atomic-base>", NULL);
3650 BINIT(SCM_CLASS_ATOMIC_BOX, "<atomic-box>", NULL);
3651 BINIT(SCM_CLASS_ATOMIC_FLAG, "<atomic-flag>", NULL);
3652 BINIT(SCM_CLASS_ATOMIC_FXBOX, "<atomic-fxbox>", NULL);
3653 BINIT(SCM_CLASS_ATOMIC_PAIR, "<atomic-pair>", NULL);
3654
3655 /* class.c */
3656 CINIT(SCM_CLASS_BOOL, "<boolean>");
3657 CINIT(SCM_CLASS_CHAR, "<char>");
3658 BINIT(SCM_CLASS_EOF_OBJECT,"<eof-object>", NULL);
3659 BINIT(SCM_CLASS_UNDEFINED_OBJECT,"<undefined-object>", NULL);
3660 BINIT(SCM_CLASS_UNKNOWN,"<unknown>", NULL);
3661 BINIT(SCM_CLASS_OBJECT, "<object>", NULL);
3662 BINIT(SCM_CLASS_GENERIC,"<generic>", generic_slots);
3663 Scm_GenericClass.flags |= SCM_CLASS_APPLICABLE;
3664 BINIT(SCM_CLASS_METHOD, "<method>", method_slots);
3665 Scm_MethodClass.flags |= SCM_CLASS_APPLICABLE;
3666 BINIT(SCM_CLASS_NEXT_METHOD, "<next-method>", NULL);
3667 Scm_NextMethodClass.flags |= SCM_CLASS_APPLICABLE;
3668 BINIT(SCM_CLASS_ACCESSOR_METHOD, "<accessor-method>", accessor_method_slots);
3669 Scm_AccessorMethodClass.flags |= SCM_CLASS_APPLICABLE;
3670 BINIT(SCM_CLASS_SLOT_ACCESSOR,"<slot-accessor>", slot_accessor_slots);
3671 BINIT(SCM_CLASS_FOREIGN_POINTER, "<foreign-pointer>", NULL);
3672 BINIT(SCM_CLASS_PROXY_TYPE, "<proxy-type>", NULL);
3673
3674 /* char.c */
3675 CINIT(SCM_CLASS_CHAR_SET, "<char-set>");
3676
3677 /* connection.c */
3678 BINIT(SCM_CLASS_CONNECTION, "<connection>", NULL);
3679
3680 /* contmarks.c */
3681 CINIT(SCM_CLASS_CONTINUATION_MARK_SET, "<continuation-mark-set>");
3682
3683 /* comparator.c */
3684 /* initialized in Scm__InitComparator */
3685
3686 /* compile.c */
3687 /* initialized in Scm__InitCompiler */
3688
3689 /* error.c */
3690 /* initialized in Scm__InitExceptions */
3691
3692 /* hash.c */
3693 CINIT(SCM_CLASS_HASH_TABLE, "<hash-table>");
3694
3695 /* list.c */
3696 CINIT(SCM_CLASS_LIST, "<list>");
3697 CINIT(SCM_CLASS_PAIR, "<pair>");
3698 CINIT(SCM_CLASS_NULL, "<null>");
3699
3700 /* load.c */
3701 CINIT(SCM_CLASS_AUTOLOAD, "<autoload>");
3702
3703 /* macro.c */
3704 CINIT(SCM_CLASS_SYNTAX, "<syntax>");
3705 CINIT(SCM_CLASS_MACRO, "<macro>");
3706 CINIT(SCM_CLASS_SYNTAX_RULES, "<syntax-rules>");
3707
3708 /* module.c */
3709 /* class initialized in libmod.scm */
3710
3711 /* number.c */
3712 CINIT(SCM_CLASS_NUMBER, "<number>");
3713 CINIT(SCM_CLASS_COMPLEX, "<complex>");
3714 CINIT(SCM_CLASS_REAL, "<real>");
3715 CINIT(SCM_CLASS_RATIONAL, "<rational>");
3716 CINIT(SCM_CLASS_INTEGER, "<integer>");
3717
3718 /* parameter.c */
3719 BINIT(SCM_CLASS_PRIMITIVE_PARAMETER, "<primitive-parameter>", NULL);
3720 CINIT(SCM_CLASS_PARAMETERIZATION, "<parameterization>");
3721
3722 /* port.c */
3723 /* initialized in Scm__InitPort */
3724
3725 /* proc.c */
3726 /* initialized in Scm__InitProc */
3727
3728 /* promise.c */
3729 CINIT(SCM_CLASS_PROMISE, "<promise>");
3730 CINIT(SCM_CLASS_LAZY_PAIR, "<lazy-pair>");
3731
3732 /* read.c */
3733 BINIT(SCM_CLASS_READ_CONTEXT, "<read-context>", NULL);
3734 BINIT(SCM_CLASS_READ_REFERENCE, "<read-reference>", NULL);
3735
3736 /* regexp.c */
3737 CINIT(SCM_CLASS_REGEXP, "<regexp>");
3738 CINIT(SCM_CLASS_REGMATCH, "<regmatch>");
3739
3740 /* string.c */
3741 CINIT(SCM_CLASS_STRING, "<string>");
3742 BINIT(SCM_CLASS_STRING_CURSOR, "<string-cursor>", NULL);
3743 BINIT(SCM_CLASS_STRING_CURSOR_LARGE, "<string-cursor-large>", NULL);
3744
3745 /* symbol.c */
3746 CINIT(SCM_CLASS_SYMBOL, "<symbol>");
3747 CINIT(SCM_CLASS_GLOC, "<gloc>");
3748 CINIT(SCM_CLASS_KEYWORD, "<keyword>");
3749
3750 /* system.c */
3751 /* initialized in Scm__InitSystem */
3752
3753 /* threadlocal.c */
3754 CINIT(SCM_CLASS_THREAD_LOCAL, "<thread-local>");
3755
3756 /* treemap.c */
3757 CINIT(SCM_CLASS_TREE_MAP, "<tree-map>");
3758
3759 /* vector.c */
3760 CINIT(SCM_CLASS_VECTOR, "<vector>");
3761 CINIT(SCM_CLASS_UVECTOR, "<uvector>");
3762 CINIT(SCM_CLASS_S8VECTOR, "<s8vector>");
3763 CINIT(SCM_CLASS_U8VECTOR, "<u8vector>");
3764 CINIT(SCM_CLASS_S16VECTOR, "<s16vector>");
3765 CINIT(SCM_CLASS_U16VECTOR, "<u16vector>");
3766 CINIT(SCM_CLASS_S32VECTOR, "<s32vector>");
3767 CINIT(SCM_CLASS_U32VECTOR, "<u32vector>");
3768 CINIT(SCM_CLASS_S64VECTOR, "<s64vector>");
3769 CINIT(SCM_CLASS_U64VECTOR, "<u64vector>");
3770 CINIT(SCM_CLASS_F16VECTOR, "<f16vector>");
3771 CINIT(SCM_CLASS_F32VECTOR, "<f32vector>");
3772 CINIT(SCM_CLASS_F64VECTOR, "<f64vector>");
3773 CINIT(SCM_CLASS_C32VECTOR, "<c32vector>");
3774 CINIT(SCM_CLASS_C64VECTOR, "<c64vector>");
3775 CINIT(SCM_CLASS_C128VECTOR, "<c128vector>");
3776 CINIT(SCM_CLASS_BITVECTOR, "<bitvector>");
3777
3778 /* vm.c */
3779 CINIT(SCM_CLASS_PROMPT_TAG, "<prompt-tag>");
3780 CINIT(SCM_CLASS_DYNAMIC_HANDLER, "<dynamic-handler>");
3781 CINIT(SCM_CLASS_ESCAPE_POINT, "<escape-point>");
3782
3783 /* weak.c */
3784 CINIT(SCM_CLASS_WEAK_VECTOR, "<weak-vector>");
3785 CINIT(SCM_CLASS_WEAK_HASH_TABLE, "<weak-hash-table>");
3786
3787 /* write.c */
3788 BINIT(SCM_CLASS_WRITE_STATE, "<write-state>", NULL);
3789
3790 #define GINIT(gf, nam) \
3791 Scm_InitBuiltinGeneric(gf, nam, mod);
3792
3793 GINIT(&Scm_GenericMake, "make");
3794 GINIT(&Scm_GenericAllocate, "allocate-instance");
3795 GINIT(&Scm_GenericInitialize, "initialize");
3796 GINIT(&Scm_GenericAddMethod, "add-method!");
3797 GINIT(&Scm_GenericDeleteMethod, "delete-method!");
3798 GINIT(&Scm_GenericComputeCPL, "compute-cpl");
3799 GINIT(&Scm_GenericComputeSlots, "compute-slots");
3800 GINIT(&Scm_GenericComputeGetNSet, "compute-get-n-set");
3801 GINIT(&Scm_GenericComputeApplicableMethods, "compute-applicable-methods");
3802 GINIT(&Scm_GenericUpdateDirectMethod, "update-direct-method!");
3803 GINIT(&Scm_GenericMethodMoreSpecificP, "method-more-specific?");
3804 GINIT(&Scm_GenericApplyGeneric, "apply-generic");
3805 GINIT(&Scm_GenericSlotMissing, "slot-missing");
3806 GINIT(&Scm_GenericSlotUnbound, "slot-unbound");
3807 GINIT(&Scm_GenericSlotRefUsingClass, "slot-ref-using-class");
3808 GINIT(&Scm_GenericSlotSetUsingClass, "slot-set-using-class!");
3809 GINIT(&Scm_GenericSlotBoundUsingClassP, "slot-bound-using-class?");
3810 GINIT(&Scm_GenericObjectEqualP, "object-equal?");
3811 GINIT(&Scm_GenericObjectCompare, "object-compare");
3812 GINIT(&Scm_GenericObjectHash, "object-hash");
3813 GINIT(&Scm_GenericObjectApply, "object-apply");
3814 GINIT(&Scm_GenericObjectSetter, "setter of object-apply");
3815 GINIT(&Scm_GenericChangeClass, "change-class");
3816
3817 Scm_SetterSet(SCM_PROCEDURE(&Scm_GenericObjectApply),
3818 SCM_PROCEDURE(&Scm_GenericObjectSetter),
3819 TRUE);
3820
3821 Scm_InitBuiltinMethod(&class_allocate_rec);
3822 Scm_InitBuiltinMethod(&class_compute_cpl_rec);
3823 Scm_InitBuiltinMethod(&slot_ref_using_class_rec);
3824 Scm_InitBuiltinMethod(&slot_set_using_class_rec);
3825 Scm_InitBuiltinMethod(&slot_bound_using_class_p_rec);
3826 Scm_InitBuiltinMethod(&object_initialize_rec);
3827 Scm_InitBuiltinMethod(&generic_addmethod_rec);
3828 Scm_InitBuiltinMethod(&generic_deletemethod_rec);
3829 Scm_InitBuiltinMethod(&method_initialize_rec);
3830 Scm_InitBuiltinMethod(&accessor_method_initialize_rec);
3831 Scm_InitBuiltinMethod(&compute_applicable_methods_rec);
3832 Scm_InitBuiltinMethod(&generic_updatedirectmethod_rec);
3833 Scm_InitBuiltinMethod(&method_more_specific_p_rec);
3834 }