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