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