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