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