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