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; | |
135 | SCM scm_class_procedure_with_setter, scm_class_primitive_generic; | |
c99de5aa | 136 | SCM scm_class_vector, scm_class_hashtable, 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 | ||
63385df2 LC |
161 | /* Port classes. Allocate 3 times the maximum number of port types so that |
162 | input ports, output ports, and in/out ports can be stored at different | |
163 | offsets. See `SCM_IN_PCLASS_INDEX' et al. */ | |
164 | SCM scm_port_class[3 * SCM_I_MAX_PORT_TYPE_COUNT]; | |
165 | ||
166 | /* SMOB classes. */ | |
47455469 | 167 | SCM scm_smob_class[SCM_I_MAX_SMOB_TYPE_COUNT]; |
539d5410 MV |
168 | |
169 | SCM scm_no_applicable_method; | |
170 | ||
80662eda MD |
171 | SCM_SYMBOL (scm_sym_define_public, "define-public"); |
172 | ||
173 | static SCM scm_make_unbound (void); | |
174 | static SCM scm_unbound_p (SCM obj); | |
398d8ee1 KN |
175 | static SCM scm_assert_bound (SCM value, SCM obj); |
176 | static SCM scm_at_assert_bound_ref (SCM obj, SCM index); | |
177 | static SCM scm_sys_goops_loaded (void); | |
9db8cf16 MG |
178 | static SCM scm_make_extended_class_from_symbol (SCM type_name_sym, |
179 | int applicablep); | |
80662eda | 180 | |
539d5410 MV |
181 | /* This function is used for efficient type dispatch. */ |
182 | SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0, | |
183 | (SCM x), | |
184 | "Return the class of @var{x}.") | |
185 | #define FUNC_NAME s_scm_class_of | |
186 | { | |
187 | switch (SCM_ITAG3 (x)) | |
188 | { | |
189 | case scm_tc3_int_1: | |
190 | case scm_tc3_int_2: | |
191 | return scm_class_integer; | |
192 | ||
193 | case scm_tc3_imm24: | |
194 | if (SCM_CHARP (x)) | |
195 | return scm_class_char; | |
196 | else if (scm_is_bool (x)) | |
197 | return scm_class_boolean; | |
198 | else if (scm_is_null (x)) | |
199 | return scm_class_null; | |
200 | else | |
201 | return scm_class_unknown; | |
202 | ||
203 | case scm_tc3_cons: | |
204 | switch (SCM_TYP7 (x)) | |
205 | { | |
206 | case scm_tcs_cons_nimcar: | |
207 | return scm_class_pair; | |
539d5410 MV |
208 | case scm_tc7_symbol: |
209 | return scm_class_symbol; | |
210 | case scm_tc7_vector: | |
211 | case scm_tc7_wvect: | |
212 | return scm_class_vector; | |
c99de5aa AW |
213 | case scm_tc7_hashtable: |
214 | return scm_class_hashtable; | |
539d5410 MV |
215 | case scm_tc7_string: |
216 | return scm_class_string; | |
217 | case scm_tc7_number: | |
218 | switch SCM_TYP16 (x) { | |
219 | case scm_tc16_big: | |
220 | return scm_class_integer; | |
221 | case scm_tc16_real: | |
222 | return scm_class_real; | |
223 | case scm_tc16_complex: | |
224 | return scm_class_complex; | |
225 | case scm_tc16_fraction: | |
226 | return scm_class_fraction; | |
227 | } | |
df9ca8d8 | 228 | case scm_tc7_gsubr: |
539d5410 MV |
229 | if (SCM_SUBR_GENERIC (x) && *SCM_SUBR_GENERIC (x)) |
230 | return scm_class_primitive_generic; | |
231 | else | |
232 | return scm_class_procedure; | |
2fb924f6 | 233 | case scm_tc7_program: |
539d5410 MV |
234 | return scm_class_procedure; |
235 | case scm_tc7_pws: | |
236 | return scm_class_procedure_with_setter; | |
237 | ||
238 | case scm_tc7_smob: | |
239 | { | |
240 | scm_t_bits type = SCM_TYP16 (x); | |
241 | if (type != scm_tc16_port_with_ps) | |
242 | return scm_smob_class[SCM_TC2SMOBNUM (type)]; | |
243 | x = SCM_PORT_WITH_PS_PORT (x); | |
244 | /* fall through to ports */ | |
245 | } | |
246 | case scm_tc7_port: | |
247 | return scm_port_class[(SCM_WRTNG & SCM_CELL_WORD_0 (x) | |
248 | ? (SCM_RDNG & SCM_CELL_WORD_0 (x) | |
249 | ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x) | |
250 | : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x)) | |
251 | : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))]; | |
252 | case scm_tcs_struct: | |
253 | if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID) | |
254 | return SCM_CLASS_OF (x); | |
255 | else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS) | |
256 | { | |
257 | /* Goops object */ | |
258 | if (! scm_is_false (SCM_OBJ_CLASS_REDEF (x))) | |
259 | scm_change_object_class (x, | |
260 | SCM_CLASS_OF (x), /* old */ | |
261 | SCM_OBJ_CLASS_REDEF (x)); /* new */ | |
262 | return SCM_CLASS_OF (x); | |
263 | } | |
264 | else | |
265 | { | |
266 | /* ordinary struct */ | |
267 | SCM handle = scm_struct_create_handle (SCM_STRUCT_VTABLE (x)); | |
268 | if (scm_is_true (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle)))) | |
269 | return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle)); | |
270 | else | |
271 | { | |
288bbc44 LC |
272 | SCM class, name; |
273 | ||
274 | name = SCM_STRUCT_TABLE_NAME (SCM_CDR (handle)); | |
275 | if (!scm_is_symbol (name)) | |
276 | name = scm_string_to_symbol (scm_nullstr); | |
277 | ||
278 | class = | |
b6cf4d02 AW |
279 | scm_make_extended_class_from_symbol (name, |
280 | SCM_STRUCT_APPLICABLE_P (x)); | |
539d5410 MV |
281 | SCM_SET_STRUCT_TABLE_CLASS (SCM_CDR (handle), class); |
282 | return class; | |
283 | } | |
284 | } | |
285 | default: | |
286 | if (scm_is_pair (x)) | |
287 | return scm_class_pair; | |
288 | else | |
289 | return scm_class_unknown; | |
290 | } | |
291 | ||
292 | case scm_tc3_struct: | |
293 | case scm_tc3_tc7_1: | |
294 | case scm_tc3_tc7_2: | |
314b8716 | 295 | /* case scm_tc3_unused: */ |
539d5410 MV |
296 | /* Never reached */ |
297 | break; | |
298 | } | |
299 | return scm_class_unknown; | |
300 | } | |
301 | #undef FUNC_NAME | |
302 | ||
80662eda MD |
303 | /****************************************************************************** |
304 | * | |
305 | * Compute-cpl | |
306 | * | |
bbf8d523 MD |
307 | * This version doesn't fully handle multiple-inheritance. It serves |
308 | * only for booting classes and will be overloaded in Scheme | |
80662eda MD |
309 | * |
310 | ******************************************************************************/ | |
311 | ||
80662eda MD |
312 | static SCM |
313 | map (SCM (*proc) (SCM), SCM ls) | |
314 | { | |
d2e53ed6 | 315 | if (scm_is_null (ls)) |
80662eda | 316 | return ls; |
6b80d352 DH |
317 | else |
318 | { | |
319 | SCM res = scm_cons (proc (SCM_CAR (ls)), SCM_EOL); | |
320 | SCM h = res; | |
321 | ls = SCM_CDR (ls); | |
d2e53ed6 | 322 | while (!scm_is_null (ls)) |
6b80d352 DH |
323 | { |
324 | SCM_SETCDR (h, scm_cons (proc (SCM_CAR (ls)), SCM_EOL)); | |
325 | h = SCM_CDR (h); | |
326 | ls = SCM_CDR (ls); | |
327 | } | |
328 | return res; | |
329 | } | |
80662eda MD |
330 | } |
331 | ||
332 | static SCM | |
333 | filter_cpl (SCM ls) | |
334 | { | |
335 | SCM res = SCM_EOL; | |
d2e53ed6 | 336 | while (!scm_is_null (ls)) |
80662eda MD |
337 | { |
338 | SCM el = SCM_CAR (ls); | |
7888309b | 339 | if (scm_is_false (scm_c_memq (el, res))) |
80662eda MD |
340 | res = scm_cons (el, res); |
341 | ls = SCM_CDR (ls); | |
342 | } | |
343 | return res; | |
344 | } | |
345 | ||
346 | static SCM | |
347 | compute_cpl (SCM class) | |
348 | { | |
349 | if (goops_loaded_p) | |
bef95911 | 350 | return scm_call_1 (SCM_VARIABLE_REF (var_compute_cpl), class); |
80662eda MD |
351 | else |
352 | { | |
353 | SCM supers = SCM_SLOT (class, scm_si_direct_supers); | |
354 | SCM ls = scm_append (scm_acons (class, supers, | |
355 | map (compute_cpl, supers))); | |
356 | return scm_reverse_x (filter_cpl (ls), SCM_EOL); | |
357 | } | |
358 | } | |
359 | ||
360 | /****************************************************************************** | |
361 | * | |
362 | * compute-slots | |
363 | * | |
364 | ******************************************************************************/ | |
365 | ||
366 | static SCM | |
367 | remove_duplicate_slots (SCM l, SCM res, SCM slots_already_seen) | |
368 | { | |
369 | SCM tmp; | |
370 | ||
d2e53ed6 | 371 | if (scm_is_null (l)) |
80662eda MD |
372 | return res; |
373 | ||
374 | tmp = SCM_CAAR (l); | |
cc95e00a | 375 | if (!scm_is_symbol (tmp)) |
1afff620 | 376 | scm_misc_error ("%compute-slots", "bad slot name ~S", scm_list_1 (tmp)); |
6d77c894 | 377 | |
7888309b | 378 | if (scm_is_false (scm_c_memq (tmp, slots_already_seen))) { |
80662eda MD |
379 | res = scm_cons (SCM_CAR (l), res); |
380 | slots_already_seen = scm_cons (tmp, slots_already_seen); | |
381 | } | |
6d77c894 | 382 | |
80662eda MD |
383 | return remove_duplicate_slots (SCM_CDR (l), res, slots_already_seen); |
384 | } | |
385 | ||
386 | static SCM | |
387 | build_slots_list (SCM dslots, SCM cpl) | |
388 | { | |
389 | register SCM res = dslots; | |
390 | ||
d2e53ed6 | 391 | for (cpl = SCM_CDR (cpl); !scm_is_null (cpl); cpl = SCM_CDR (cpl)) |
1afff620 KN |
392 | res = scm_append (scm_list_2 (SCM_SLOT (SCM_CAR (cpl), |
393 | scm_si_direct_slots), | |
394 | res)); | |
80662eda MD |
395 | |
396 | /* res contains a list of slots. Remove slots which appears more than once */ | |
397 | return remove_duplicate_slots (scm_reverse (res), SCM_EOL, SCM_EOL); | |
398 | } | |
399 | ||
400 | static SCM | |
401 | maplist (SCM ls) | |
402 | { | |
403 | SCM orig = ls; | |
d2e53ed6 | 404 | while (!scm_is_null (ls)) |
80662eda | 405 | { |
d2e53ed6 | 406 | if (!scm_is_pair (SCM_CAR (ls))) |
80662eda MD |
407 | SCM_SETCAR (ls, scm_cons (SCM_CAR (ls), SCM_EOL)); |
408 | ls = SCM_CDR (ls); | |
409 | } | |
410 | return orig; | |
411 | } | |
412 | ||
80662eda | 413 | |
23437298 DH |
414 | SCM_DEFINE (scm_sys_compute_slots, "%compute-slots", 1, 0, 0, |
415 | (SCM class), | |
5352393c MG |
416 | "Return a list consisting of the names of all slots belonging to\n" |
417 | "class @var{class}, i. e. the slots of @var{class} and of all of\n" | |
6d77c894 | 418 | "its superclasses.") |
23437298 | 419 | #define FUNC_NAME s_scm_sys_compute_slots |
80662eda | 420 | { |
398d8ee1 | 421 | SCM_VALIDATE_CLASS (1, class); |
80662eda MD |
422 | return build_slots_list (SCM_SLOT (class, scm_si_direct_slots), |
423 | SCM_SLOT (class, scm_si_cpl)); | |
424 | } | |
23437298 DH |
425 | #undef FUNC_NAME |
426 | ||
80662eda MD |
427 | |
428 | /****************************************************************************** | |
429 | * | |
430 | * compute-getters-n-setters | |
6d77c894 TTN |
431 | * |
432 | * This version doesn't handle slot options. It serves only for booting | |
dcb410ec | 433 | * classes and will be overloaded in Scheme. |
80662eda MD |
434 | * |
435 | ******************************************************************************/ | |
436 | ||
437 | SCM_KEYWORD (k_init_value, "init-value"); | |
438 | SCM_KEYWORD (k_init_thunk, "init-thunk"); | |
439 | ||
440 | static SCM | |
441 | compute_getters_n_setters (SCM slots) | |
442 | { | |
443 | SCM res = SCM_EOL; | |
444 | SCM *cdrloc = &res; | |
c014a02e | 445 | long i = 0; |
80662eda | 446 | |
d2e53ed6 | 447 | for ( ; !scm_is_null (slots); slots = SCM_CDR (slots)) |
80662eda MD |
448 | { |
449 | SCM init = SCM_BOOL_F; | |
450 | SCM options = SCM_CDAR (slots); | |
d2e53ed6 | 451 | if (!scm_is_null (options)) |
80662eda MD |
452 | { |
453 | init = scm_get_keyword (k_init_value, options, 0); | |
454 | if (init) | |
366ecaec | 455 | { |
9d019f9b AW |
456 | init = scm_primitive_eval (scm_list_3 (scm_sym_lambda, |
457 | SCM_EOL, | |
458 | scm_list_2 (scm_sym_quote, | |
459 | init))); | |
366ecaec | 460 | } |
80662eda MD |
461 | else |
462 | init = scm_get_keyword (k_init_thunk, options, SCM_BOOL_F); | |
463 | } | |
464 | *cdrloc = scm_cons (scm_cons (SCM_CAAR (slots), | |
465 | scm_cons (init, | |
e11e83f3 | 466 | scm_from_int (i++))), |
80662eda MD |
467 | SCM_EOL); |
468 | cdrloc = SCM_CDRLOC (*cdrloc); | |
469 | } | |
470 | return res; | |
471 | } | |
472 | ||
473 | /****************************************************************************** | |
474 | * | |
475 | * initialize-object | |
476 | * | |
477 | ******************************************************************************/ | |
478 | ||
479 | /*fixme* Manufacture keywords in advance */ | |
480 | SCM | |
c014a02e | 481 | scm_i_get_keyword (SCM key, SCM l, long len, SCM default_value, const char *subr) |
80662eda | 482 | { |
c014a02e | 483 | long i; |
23437298 DH |
484 | |
485 | for (i = 0; i != len; i += 2) | |
80662eda | 486 | { |
23437298 DH |
487 | SCM obj = SCM_CAR (l); |
488 | ||
c598539a | 489 | if (!scm_is_keyword (obj)) |
1afff620 | 490 | scm_misc_error (subr, "bad keyword: ~S", scm_list_1 (obj)); |
bc36d050 | 491 | else if (scm_is_eq (obj, key)) |
80662eda | 492 | return SCM_CADR (l); |
23437298 DH |
493 | else |
494 | l = SCM_CDDR (l); | |
80662eda | 495 | } |
23437298 | 496 | |
80662eda MD |
497 | return default_value; |
498 | } | |
499 | ||
80662eda | 500 | |
23437298 DH |
501 | SCM_DEFINE (scm_get_keyword, "get-keyword", 3, 0, 0, |
502 | (SCM key, SCM l, SCM default_value), | |
5352393c MG |
503 | "Determine an associated value for the keyword @var{key} from\n" |
504 | "the list @var{l}. The list @var{l} has to consist of an even\n" | |
505 | "number of elements, where, starting with the first, every\n" | |
506 | "second element is a keyword, followed by its associated value.\n" | |
507 | "If @var{l} does not hold a value for @var{key}, the value\n" | |
508 | "@var{default_value} is returned.") | |
23437298 | 509 | #define FUNC_NAME s_scm_get_keyword |
80662eda | 510 | { |
c014a02e | 511 | long len; |
23437298 | 512 | |
c598539a | 513 | SCM_ASSERT (scm_is_keyword (key), key, SCM_ARG1, FUNC_NAME); |
80662eda | 514 | len = scm_ilength (l); |
b6311c08 | 515 | if (len < 0 || len % 2 == 1) |
1afff620 | 516 | scm_misc_error (FUNC_NAME, "Bad keyword-value list: ~S", scm_list_1 (l)); |
23437298 DH |
517 | |
518 | return scm_i_get_keyword (key, l, len, default_value, FUNC_NAME); | |
80662eda | 519 | } |
23437298 DH |
520 | #undef FUNC_NAME |
521 | ||
80662eda | 522 | |
80662eda MD |
523 | SCM_KEYWORD (k_init_keyword, "init-keyword"); |
524 | ||
525 | static SCM get_slot_value (SCM class, SCM obj, SCM slotdef); | |
526 | static SCM set_slot_value (SCM class, SCM obj, SCM slotdef, SCM value); | |
527 | ||
398d8ee1 KN |
528 | SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0, |
529 | (SCM obj, SCM initargs), | |
6bcefd15 MG |
530 | "Initialize the object @var{obj} with the given arguments\n" |
531 | "@var{initargs}.") | |
398d8ee1 | 532 | #define FUNC_NAME s_scm_sys_initialize_object |
80662eda MD |
533 | { |
534 | SCM tmp, get_n_set, slots; | |
535 | SCM class = SCM_CLASS_OF (obj); | |
c014a02e | 536 | long n_initargs; |
80662eda | 537 | |
398d8ee1 | 538 | SCM_VALIDATE_INSTANCE (1, obj); |
80662eda | 539 | n_initargs = scm_ilength (initargs); |
398d8ee1 | 540 | SCM_ASSERT ((n_initargs & 1) == 0, initargs, SCM_ARG2, FUNC_NAME); |
6d77c894 | 541 | |
80662eda MD |
542 | get_n_set = SCM_SLOT (class, scm_si_getters_n_setters); |
543 | slots = SCM_SLOT (class, scm_si_slots); | |
6d77c894 | 544 | |
80662eda MD |
545 | /* See for each slot how it must be initialized */ |
546 | for (; | |
d2e53ed6 | 547 | !scm_is_null (slots); |
80662eda MD |
548 | get_n_set = SCM_CDR (get_n_set), slots = SCM_CDR (slots)) |
549 | { | |
550 | SCM slot_name = SCM_CAR (slots); | |
551 | SCM slot_value = 0; | |
6d77c894 | 552 | |
d2e53ed6 | 553 | if (!scm_is_null (SCM_CDR (slot_name))) |
80662eda MD |
554 | { |
555 | /* This slot admits (perhaps) to be initialized at creation time */ | |
c014a02e | 556 | long n = scm_ilength (SCM_CDR (slot_name)); |
80662eda | 557 | if (n & 1) /* odd or -1 */ |
398d8ee1 | 558 | SCM_MISC_ERROR ("class contains bogus slot definition: ~S", |
1afff620 | 559 | scm_list_1 (slot_name)); |
80662eda MD |
560 | tmp = scm_i_get_keyword (k_init_keyword, |
561 | SCM_CDR (slot_name), | |
562 | n, | |
563 | 0, | |
398d8ee1 | 564 | FUNC_NAME); |
80662eda MD |
565 | slot_name = SCM_CAR (slot_name); |
566 | if (tmp) | |
567 | { | |
568 | /* an initarg was provided for this slot */ | |
c598539a | 569 | if (!scm_is_keyword (tmp)) |
398d8ee1 | 570 | SCM_MISC_ERROR ("initarg must be a keyword. It was ~S", |
1afff620 | 571 | scm_list_1 (tmp)); |
80662eda MD |
572 | slot_value = scm_i_get_keyword (tmp, |
573 | initargs, | |
574 | n_initargs, | |
575 | 0, | |
398d8ee1 | 576 | FUNC_NAME); |
80662eda MD |
577 | } |
578 | } | |
579 | ||
580 | if (slot_value) | |
581 | /* set slot to provided value */ | |
582 | set_slot_value (class, obj, SCM_CAR (get_n_set), slot_value); | |
583 | else | |
584 | { | |
585 | /* set slot to its :init-form if it exists */ | |
586 | tmp = SCM_CADAR (get_n_set); | |
7888309b | 587 | if (scm_is_true (tmp)) |
80662eda MD |
588 | { |
589 | slot_value = get_slot_value (class, obj, SCM_CAR (get_n_set)); | |
590 | if (SCM_GOOPS_UNBOUNDP (slot_value)) | |
fd7ac322 AW |
591 | set_slot_value (class, |
592 | obj, | |
593 | SCM_CAR (get_n_set), | |
594 | scm_call_0 (tmp)); | |
80662eda MD |
595 | } |
596 | } | |
597 | } | |
6d77c894 | 598 | |
80662eda MD |
599 | return obj; |
600 | } | |
398d8ee1 | 601 | #undef FUNC_NAME |
80662eda | 602 | |
21ab2aeb MD |
603 | /* NOTE: The following macros are interdependent with code |
604 | * in goops.scm:compute-getters-n-setters | |
605 | */ | |
606 | #define SCM_GNS_INSTANCE_ALLOCATED_P(gns) \ | |
e11e83f3 | 607 | (SCM_I_INUMP (SCM_CDDR (gns)) \ |
d2e53ed6 MV |
608 | || (scm_is_pair (SCM_CDDR (gns)) \ |
609 | && scm_is_pair (SCM_CDDDR (gns)) \ | |
610 | && scm_is_pair (SCM_CDDDDR (gns)))) | |
21ab2aeb | 611 | #define SCM_GNS_INDEX(gns) \ |
e11e83f3 MV |
612 | (SCM_I_INUMP (SCM_CDDR (gns)) \ |
613 | ? SCM_I_INUM (SCM_CDDR (gns)) \ | |
614 | : scm_to_long (SCM_CAR (SCM_CDDDDR (gns)))) | |
21ab2aeb | 615 | #define SCM_GNS_SIZE(gns) \ |
e11e83f3 | 616 | (SCM_I_INUMP (SCM_CDDR (gns)) \ |
21ab2aeb | 617 | ? 1 \ |
e11e83f3 | 618 | : scm_to_long (SCM_CADR (SCM_CDDDDR (gns)))) |
80662eda MD |
619 | |
620 | SCM_KEYWORD (k_class, "class"); | |
b46fae00 MD |
621 | SCM_KEYWORD (k_allocation, "allocation"); |
622 | SCM_KEYWORD (k_instance, "instance"); | |
80662eda | 623 | |
398d8ee1 KN |
624 | SCM_DEFINE (scm_sys_prep_layout_x, "%prep-layout!", 1, 0, 0, |
625 | (SCM class), | |
626 | "") | |
627 | #define FUNC_NAME s_scm_sys_prep_layout_x | |
80662eda | 628 | { |
21ab2aeb | 629 | SCM slots, getters_n_setters, nfields; |
6b80d352 DH |
630 | unsigned long int n, i; |
631 | char *s; | |
cc95e00a | 632 | SCM layout; |
80662eda | 633 | |
398d8ee1 | 634 | SCM_VALIDATE_INSTANCE (1, class); |
80662eda | 635 | slots = SCM_SLOT (class, scm_si_slots); |
21ab2aeb | 636 | getters_n_setters = SCM_SLOT (class, scm_si_getters_n_setters); |
80662eda | 637 | nfields = SCM_SLOT (class, scm_si_nfields); |
e11e83f3 | 638 | if (!SCM_I_INUMP (nfields) || SCM_I_INUM (nfields) < 0) |
398d8ee1 | 639 | SCM_MISC_ERROR ("bad value in nfields slot: ~S", |
1afff620 | 640 | scm_list_1 (nfields)); |
e11e83f3 | 641 | n = 2 * SCM_I_INUM (nfields); |
80662eda MD |
642 | if (n < sizeof (SCM_CLASS_CLASS_LAYOUT) - 1 |
643 | && SCM_SUBCLASSP (class, scm_class_class)) | |
398d8ee1 | 644 | SCM_MISC_ERROR ("class object doesn't have enough fields: ~S", |
1afff620 | 645 | scm_list_1 (nfields)); |
6d77c894 | 646 | |
cc95e00a | 647 | layout = scm_i_make_string (n, &s); |
21ab2aeb | 648 | i = 0; |
d2e53ed6 | 649 | while (scm_is_pair (getters_n_setters)) |
80662eda | 650 | { |
21ab2aeb | 651 | if (SCM_GNS_INSTANCE_ALLOCATED_P (SCM_CAR (getters_n_setters))) |
b46fae00 | 652 | { |
21ab2aeb MD |
653 | SCM type; |
654 | int len, index, size; | |
655 | char p, a; | |
656 | ||
d2e53ed6 | 657 | if (i >= n || !scm_is_pair (slots)) |
21ab2aeb MD |
658 | goto inconsistent; |
659 | ||
660 | /* extract slot type */ | |
b46fae00 | 661 | len = scm_ilength (SCM_CDAR (slots)); |
21ab2aeb MD |
662 | type = scm_i_get_keyword (k_class, SCM_CDAR (slots), |
663 | len, SCM_BOOL_F, FUNC_NAME); | |
664 | /* determine slot GC protection and access mode */ | |
7888309b | 665 | if (scm_is_false (type)) |
6b80d352 | 666 | { |
21ab2aeb MD |
667 | p = 'p'; |
668 | a = 'w'; | |
6b80d352 DH |
669 | } |
670 | else | |
671 | { | |
21ab2aeb | 672 | if (!SCM_CLASSP (type)) |
cc95e00a | 673 | SCM_MISC_ERROR ("bad slot class", SCM_EOL); |
21ab2aeb MD |
674 | else if (SCM_SUBCLASSP (type, scm_class_foreign_slot)) |
675 | { | |
676 | if (SCM_SUBCLASSP (type, scm_class_self)) | |
677 | p = 's'; | |
678 | else if (SCM_SUBCLASSP (type, scm_class_protected)) | |
679 | p = 'p'; | |
680 | else | |
681 | p = 'u'; | |
682 | ||
683 | if (SCM_SUBCLASSP (type, scm_class_opaque)) | |
684 | a = 'o'; | |
685 | else if (SCM_SUBCLASSP (type, scm_class_read_only)) | |
686 | a = 'r'; | |
b6cf4d02 AW |
687 | else if (SCM_SUBCLASSP (type, scm_class_hidden)) |
688 | a = 'h'; | |
21ab2aeb MD |
689 | else |
690 | a = 'w'; | |
691 | } | |
692 | else | |
693 | { | |
694 | p = 'p'; | |
695 | a = 'w'; | |
696 | } | |
697 | } | |
698 | ||
699 | index = SCM_GNS_INDEX (SCM_CAR (getters_n_setters)); | |
700 | if (index != (i >> 1)) | |
701 | goto inconsistent; | |
702 | size = SCM_GNS_SIZE (SCM_CAR (getters_n_setters)); | |
703 | while (size) | |
704 | { | |
705 | s[i++] = p; | |
706 | s[i++] = a; | |
707 | --size; | |
6b80d352 | 708 | } |
80662eda | 709 | } |
80662eda | 710 | slots = SCM_CDR (slots); |
21ab2aeb MD |
711 | getters_n_setters = SCM_CDR (getters_n_setters); |
712 | } | |
d2e53ed6 | 713 | if (!scm_is_null (slots)) |
21ab2aeb MD |
714 | { |
715 | inconsistent: | |
21ab2aeb | 716 | SCM_MISC_ERROR ("inconsistent getters-n-setters", SCM_EOL); |
80662eda | 717 | } |
b6cf4d02 | 718 | SCM_SET_VTABLE_LAYOUT (class, scm_string_to_symbol (layout)); |
80662eda MD |
719 | return SCM_UNSPECIFIED; |
720 | } | |
398d8ee1 | 721 | #undef FUNC_NAME |
80662eda MD |
722 | |
723 | static void prep_hashsets (SCM); | |
724 | ||
398d8ee1 KN |
725 | SCM_DEFINE (scm_sys_inherit_magic_x, "%inherit-magic!", 2, 0, 0, |
726 | (SCM class, SCM dsupers), | |
727 | "") | |
728 | #define FUNC_NAME s_scm_sys_inherit_magic_x | |
80662eda | 729 | { |
398d8ee1 | 730 | SCM_VALIDATE_INSTANCE (1, class); |
51f66c91 AW |
731 | scm_i_struct_inherit_vtable_magic (SCM_CLASS_OF (class), class); |
732 | SCM_SET_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_OR_VALID); | |
80662eda MD |
733 | |
734 | prep_hashsets (class); | |
6d77c894 | 735 | |
80662eda MD |
736 | return SCM_UNSPECIFIED; |
737 | } | |
398d8ee1 | 738 | #undef FUNC_NAME |
80662eda | 739 | |
63c1872f | 740 | static void |
80662eda MD |
741 | prep_hashsets (SCM class) |
742 | { | |
dcb410ec | 743 | unsigned int i; |
80662eda | 744 | |
e161c9f8 | 745 | for (i = 0; i < 8; ++i) |
dcb410ec | 746 | SCM_SET_HASHSET (class, i, scm_c_uniform32 (goops_rstate)); |
80662eda MD |
747 | } |
748 | ||
749 | /******************************************************************************/ | |
750 | ||
751 | SCM | |
752 | scm_basic_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots) | |
753 | { | |
754 | SCM z, cpl, slots, nfields, g_n_s; | |
755 | ||
756 | /* Allocate one instance */ | |
757 | z = scm_make_struct (class, SCM_INUM0, SCM_EOL); | |
758 | ||
759 | /* Initialize its slots */ | |
dcb410ec | 760 | SCM_SET_SLOT (z, scm_si_direct_supers, dsupers); |
80662eda MD |
761 | cpl = compute_cpl (z); |
762 | slots = build_slots_list (maplist (dslots), cpl); | |
e11e83f3 | 763 | nfields = scm_from_int (scm_ilength (slots)); |
80662eda MD |
764 | g_n_s = compute_getters_n_setters (slots); |
765 | ||
b6cf4d02 | 766 | SCM_SET_SLOT (z, scm_vtable_index_name, name); |
dcb410ec DH |
767 | SCM_SET_SLOT (z, scm_si_direct_slots, dslots); |
768 | SCM_SET_SLOT (z, scm_si_direct_subclasses, SCM_EOL); | |
769 | SCM_SET_SLOT (z, scm_si_direct_methods, SCM_EOL); | |
770 | SCM_SET_SLOT (z, scm_si_cpl, cpl); | |
771 | SCM_SET_SLOT (z, scm_si_slots, slots); | |
772 | SCM_SET_SLOT (z, scm_si_nfields, nfields); | |
773 | SCM_SET_SLOT (z, scm_si_getters_n_setters, g_n_s); | |
774 | SCM_SET_SLOT (z, scm_si_redefined, SCM_BOOL_F); | |
80662eda MD |
775 | |
776 | /* Add this class in the direct-subclasses slot of dsupers */ | |
777 | { | |
778 | SCM tmp; | |
d2e53ed6 | 779 | for (tmp = dsupers; !scm_is_null (tmp); tmp = SCM_CDR (tmp)) |
dcb410ec DH |
780 | SCM_SET_SLOT (SCM_CAR (tmp), scm_si_direct_subclasses, |
781 | scm_cons (z, SCM_SLOT (SCM_CAR (tmp), | |
782 | scm_si_direct_subclasses))); | |
80662eda MD |
783 | } |
784 | ||
80662eda MD |
785 | return z; |
786 | } | |
787 | ||
788 | SCM | |
789 | scm_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots) | |
790 | { | |
791 | SCM z = scm_basic_basic_make_class (class, name, dsupers, dslots); | |
80662eda | 792 | scm_sys_prep_layout_x (z); |
51f66c91 | 793 | scm_sys_inherit_magic_x (z, dsupers); |
80662eda MD |
794 | return z; |
795 | } | |
796 | ||
797 | /******************************************************************************/ | |
798 | ||
6b80d352 | 799 | SCM_SYMBOL (sym_layout, "layout"); |
b6cf4d02 AW |
800 | SCM_SYMBOL (sym_flags, "flags"); |
801 | SCM_SYMBOL (sym_self, "%self"); | |
802 | SCM_SYMBOL (sym_instance_finalizer, "instance-finalizer"); | |
803 | SCM_SYMBOL (sym_reserved_0, "%reserved-0"); | |
804 | SCM_SYMBOL (sym_reserved_1, "%reserved-1"); | |
6b80d352 DH |
805 | SCM_SYMBOL (sym_print, "print"); |
806 | SCM_SYMBOL (sym_procedure, "procedure"); | |
807 | SCM_SYMBOL (sym_setter, "setter"); | |
808 | SCM_SYMBOL (sym_redefined, "redefined"); | |
809 | SCM_SYMBOL (sym_h0, "h0"); | |
810 | SCM_SYMBOL (sym_h1, "h1"); | |
811 | SCM_SYMBOL (sym_h2, "h2"); | |
812 | SCM_SYMBOL (sym_h3, "h3"); | |
813 | SCM_SYMBOL (sym_h4, "h4"); | |
814 | SCM_SYMBOL (sym_h5, "h5"); | |
815 | SCM_SYMBOL (sym_h6, "h6"); | |
816 | SCM_SYMBOL (sym_h7, "h7"); | |
817 | SCM_SYMBOL (sym_name, "name"); | |
818 | SCM_SYMBOL (sym_direct_supers, "direct-supers"); | |
819 | SCM_SYMBOL (sym_direct_slots, "direct-slots"); | |
820 | SCM_SYMBOL (sym_direct_subclasses, "direct-subclasses"); | |
821 | SCM_SYMBOL (sym_direct_methods, "direct-methods"); | |
822 | SCM_SYMBOL (sym_cpl, "cpl"); | |
823 | SCM_SYMBOL (sym_default_slot_definition_class, "default-slot-definition-class"); | |
824 | SCM_SYMBOL (sym_slots, "slots"); | |
825 | SCM_SYMBOL (sym_getters_n_setters, "getters-n-setters"); | |
826 | SCM_SYMBOL (sym_keyword_access, "keyword-access"); | |
827 | SCM_SYMBOL (sym_nfields, "nfields"); | |
6b80d352 DH |
828 | |
829 | ||
80662eda MD |
830 | static SCM |
831 | build_class_class_slots () | |
832 | { | |
b6cf4d02 AW |
833 | /* has to be kept in sync with SCM_VTABLE_BASE_LAYOUT and |
834 | SCM_CLASS_CLASS_LAYOUT */ | |
6b80d352 DH |
835 | return scm_list_n ( |
836 | scm_list_3 (sym_layout, k_class, scm_class_protected_read_only), | |
b6cf4d02 AW |
837 | scm_list_3 (sym_flags, k_class, scm_class_hidden), |
838 | scm_list_3 (sym_self, k_class, scm_class_self), | |
839 | scm_list_3 (sym_instance_finalizer, k_class, scm_class_hidden), | |
6b80d352 | 840 | scm_list_1 (sym_print), |
b6cf4d02 AW |
841 | scm_list_3 (sym_name, k_class, scm_class_protected_hidden), |
842 | scm_list_3 (sym_reserved_0, k_class, scm_class_hidden), | |
843 | scm_list_3 (sym_reserved_1, k_class, scm_class_hidden), | |
6b80d352 DH |
844 | scm_list_1 (sym_redefined), |
845 | scm_list_3 (sym_h0, k_class, scm_class_int), | |
846 | scm_list_3 (sym_h1, k_class, scm_class_int), | |
847 | scm_list_3 (sym_h2, k_class, scm_class_int), | |
848 | scm_list_3 (sym_h3, k_class, scm_class_int), | |
849 | scm_list_3 (sym_h4, k_class, scm_class_int), | |
850 | scm_list_3 (sym_h5, k_class, scm_class_int), | |
851 | scm_list_3 (sym_h6, k_class, scm_class_int), | |
852 | scm_list_3 (sym_h7, k_class, scm_class_int), | |
6b80d352 DH |
853 | scm_list_1 (sym_direct_supers), |
854 | scm_list_1 (sym_direct_slots), | |
855 | scm_list_1 (sym_direct_subclasses), | |
856 | scm_list_1 (sym_direct_methods), | |
857 | scm_list_1 (sym_cpl), | |
858 | scm_list_1 (sym_default_slot_definition_class), | |
859 | scm_list_1 (sym_slots), | |
860 | scm_list_1 (sym_getters_n_setters), | |
861 | scm_list_1 (sym_keyword_access), | |
862 | scm_list_1 (sym_nfields), | |
6b80d352 | 863 | SCM_UNDEFINED); |
80662eda MD |
864 | } |
865 | ||
866 | static void | |
867 | create_basic_classes (void) | |
868 | { | |
869 | /* SCM slots_of_class = build_class_class_slots (); */ | |
870 | ||
b6cf4d02 AW |
871 | /**** <class> ****/ |
872 | SCM cs = scm_from_locale_string (SCM_CLASS_CLASS_LAYOUT); | |
cc95e00a | 873 | SCM name = scm_from_locale_symbol ("<class>"); |
80662eda MD |
874 | scm_class_class = scm_permanent_object (scm_make_vtable_vtable (cs, |
875 | SCM_INUM0, | |
876 | SCM_EOL)); | |
877 | SCM_SET_CLASS_FLAGS (scm_class_class, (SCM_CLASSF_GOOPS_OR_VALID | |
878 | | SCM_CLASSF_METACLASS)); | |
879 | ||
b6cf4d02 | 880 | SCM_SET_SLOT (scm_class_class, scm_vtable_index_name, name); |
dcb410ec DH |
881 | SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, SCM_EOL); /* will be changed */ |
882 | /* SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots_of_class); */ | |
883 | SCM_SET_SLOT (scm_class_class, scm_si_direct_subclasses, SCM_EOL); | |
6d77c894 | 884 | SCM_SET_SLOT (scm_class_class, scm_si_direct_methods, SCM_EOL); |
dcb410ec DH |
885 | SCM_SET_SLOT (scm_class_class, scm_si_cpl, SCM_EOL); /* will be changed */ |
886 | /* SCM_SET_SLOT (scm_class_class, scm_si_slots, slots_of_class); */ | |
e11e83f3 | 887 | SCM_SET_SLOT (scm_class_class, scm_si_nfields, scm_from_int (SCM_N_CLASS_SLOTS)); |
dcb410ec DH |
888 | /* SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters, |
889 | compute_getters_n_setters (slots_of_class)); */ | |
890 | SCM_SET_SLOT (scm_class_class, scm_si_redefined, SCM_BOOL_F); | |
80662eda MD |
891 | |
892 | prep_hashsets (scm_class_class); | |
893 | ||
894 | DEFVAR(name, scm_class_class); | |
895 | ||
51f66c91 | 896 | /**** <top> ****/ |
cc95e00a | 897 | name = scm_from_locale_symbol ("<top>"); |
80662eda MD |
898 | scm_class_top = scm_permanent_object (scm_basic_make_class (scm_class_class, |
899 | name, | |
900 | SCM_EOL, | |
901 | SCM_EOL)); | |
902 | ||
903 | DEFVAR(name, scm_class_top); | |
6d77c894 | 904 | |
51f66c91 | 905 | /**** <object> ****/ |
cc95e00a | 906 | name = scm_from_locale_symbol ("<object>"); |
80662eda MD |
907 | scm_class_object = scm_permanent_object (scm_basic_make_class (scm_class_class, |
908 | name, | |
1afff620 | 909 | scm_list_1 (scm_class_top), |
80662eda MD |
910 | SCM_EOL)); |
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 | ||
1625 | SCM_SYMBOL (sym_no_method, "no-method"); | |
1626 | ||
1627 | static SCM list_of_no_method; | |
1628 | ||
63c1872f | 1629 | SCM_GLOBAL_SYMBOL (scm_sym_args, "args"); |
80662eda | 1630 | |
a4aa2134 | 1631 | |
efcebb5b AW |
1632 | SCM |
1633 | scm_apply_generic (SCM gf, SCM args) | |
1634 | { | |
2f652c68 | 1635 | return scm_apply (SCM_STRUCT_PROCEDURE (gf), args, SCM_EOL); |
efcebb5b AW |
1636 | } |
1637 | ||
1638 | SCM | |
1639 | scm_call_generic_0 (SCM gf) | |
1640 | { | |
2f652c68 | 1641 | return scm_call_0 (SCM_STRUCT_PROCEDURE (gf)); |
efcebb5b AW |
1642 | } |
1643 | ||
1644 | SCM | |
1645 | scm_call_generic_1 (SCM gf, SCM a1) | |
1646 | { | |
2f652c68 | 1647 | return scm_call_1 (SCM_STRUCT_PROCEDURE (gf), a1); |
efcebb5b AW |
1648 | } |
1649 | ||
1650 | SCM | |
1651 | scm_call_generic_2 (SCM gf, SCM a1, SCM a2) | |
1652 | { | |
2f652c68 | 1653 | return scm_call_2 (SCM_STRUCT_PROCEDURE (gf), a1, a2); |
efcebb5b AW |
1654 | } |
1655 | ||
1656 | SCM | |
1657 | scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3) | |
1658 | { | |
2f652c68 | 1659 | return scm_call_3 (SCM_STRUCT_PROCEDURE (gf), a1, a2, a3); |
efcebb5b AW |
1660 | } |
1661 | ||
e29db33c AW |
1662 | SCM_SYMBOL (sym_delayed_compile, "delayed-compile"); |
1663 | static SCM | |
1664 | make_dispatch_procedure (SCM gf) | |
1665 | { | |
1666 | static SCM var = SCM_BOOL_F; | |
1667 | if (var == SCM_BOOL_F) | |
1668 | var = scm_module_variable (scm_c_resolve_module ("oop goops dispatch"), | |
1669 | sym_delayed_compile); | |
1670 | return scm_call_1 (SCM_VARIABLE_REF (var), gf); | |
1671 | } | |
1672 | ||
80662eda MD |
1673 | static void |
1674 | clear_method_cache (SCM gf) | |
1675 | { | |
e29db33c | 1676 | SCM_SET_GENERIC_DISPATCH_PROCEDURE (gf, make_dispatch_procedure (gf)); |
a9a90a88 | 1677 | SCM_CLEAR_GENERIC_EFFECTIVE_METHODS (gf); |
80662eda MD |
1678 | } |
1679 | ||
398d8ee1 KN |
1680 | SCM_DEFINE (scm_sys_invalidate_method_cache_x, "%invalidate-method-cache!", 1, 0, 0, |
1681 | (SCM gf), | |
1682 | "") | |
1683 | #define FUNC_NAME s_scm_sys_invalidate_method_cache_x | |
80662eda | 1684 | { |
25ba37df | 1685 | SCM_ASSERT (SCM_PUREGENERICP (gf), gf, SCM_ARG1, FUNC_NAME); |
6d33e90f | 1686 | clear_method_cache (gf); |
80662eda MD |
1687 | return SCM_UNSPECIFIED; |
1688 | } | |
398d8ee1 | 1689 | #undef FUNC_NAME |
80662eda | 1690 | |
398d8ee1 KN |
1691 | SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0, |
1692 | (SCM proc), | |
1693 | "") | |
1694 | #define FUNC_NAME s_scm_generic_capability_p | |
80662eda | 1695 | { |
7888309b | 1696 | SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), |
398d8ee1 | 1697 | proc, SCM_ARG1, FUNC_NAME); |
80662eda MD |
1698 | return (scm_subr_p (proc) && SCM_SUBR_GENERIC (proc) |
1699 | ? SCM_BOOL_T | |
1700 | : SCM_BOOL_F); | |
1701 | } | |
398d8ee1 | 1702 | #undef FUNC_NAME |
80662eda | 1703 | |
398d8ee1 KN |
1704 | SCM_DEFINE (scm_enable_primitive_generic_x, "enable-primitive-generic!", 0, 0, 1, |
1705 | (SCM subrs), | |
1706 | "") | |
1707 | #define FUNC_NAME s_scm_enable_primitive_generic_x | |
80662eda | 1708 | { |
6b80d352 | 1709 | SCM_VALIDATE_REST_ARGUMENT (subrs); |
d2e53ed6 | 1710 | while (!scm_is_null (subrs)) |
80662eda MD |
1711 | { |
1712 | SCM subr = SCM_CAR (subrs); | |
1713 | SCM_ASSERT (scm_subr_p (subr) && SCM_SUBR_GENERIC (subr), | |
398d8ee1 | 1714 | subr, SCM_ARGn, FUNC_NAME); |
80662eda | 1715 | *SCM_SUBR_GENERIC (subr) |
1afff620 KN |
1716 | = scm_make (scm_list_3 (scm_class_generic, |
1717 | k_name, | |
ce471ab8 | 1718 | SCM_SUBR_NAME (subr))); |
80662eda MD |
1719 | subrs = SCM_CDR (subrs); |
1720 | } | |
1721 | return SCM_UNSPECIFIED; | |
1722 | } | |
398d8ee1 | 1723 | #undef FUNC_NAME |
80662eda | 1724 | |
9f63ce02 AW |
1725 | SCM_DEFINE (scm_set_primitive_generic_x, "set-primitive-generic!", 2, 0, 0, |
1726 | (SCM subr, SCM generic), | |
1727 | "") | |
1728 | #define FUNC_NAME s_scm_set_primitive_generic_x | |
1729 | { | |
1730 | SCM_ASSERT (scm_subr_p (subr) && SCM_SUBR_GENERIC (subr), | |
1731 | subr, SCM_ARG1, FUNC_NAME); | |
1732 | SCM_ASSERT (SCM_PUREGENERICP (generic), generic, SCM_ARG2, FUNC_NAME); | |
1733 | *SCM_SUBR_GENERIC (subr) = generic; | |
1734 | return SCM_UNSPECIFIED; | |
1735 | } | |
1736 | #undef FUNC_NAME | |
1737 | ||
398d8ee1 KN |
1738 | SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0, |
1739 | (SCM subr), | |
1740 | "") | |
1741 | #define FUNC_NAME s_scm_primitive_generic_generic | |
80662eda MD |
1742 | { |
1743 | if (scm_subr_p (subr) && SCM_SUBR_GENERIC (subr)) | |
1744 | { | |
a48d60b1 MD |
1745 | if (!*SCM_SUBR_GENERIC (subr)) |
1746 | scm_enable_primitive_generic_x (scm_list_1 (subr)); | |
1747 | return *SCM_SUBR_GENERIC (subr); | |
80662eda | 1748 | } |
db4b4ca6 | 1749 | SCM_WRONG_TYPE_ARG (SCM_ARG1, subr); |
80662eda | 1750 | } |
398d8ee1 | 1751 | #undef FUNC_NAME |
80662eda | 1752 | |
a48d60b1 MD |
1753 | typedef struct t_extension { |
1754 | struct t_extension *next; | |
1755 | SCM extended; | |
1756 | SCM extension; | |
1757 | } t_extension; | |
1758 | ||
d0cad249 LC |
1759 | |
1760 | /* Hint for `scm_gc_malloc ()' et al. when allocating `t_extension' | |
1761 | objects. */ | |
1762 | static const char extension_gc_hint[] = "GOOPS extension"; | |
1763 | ||
a48d60b1 MD |
1764 | static t_extension *extensions = 0; |
1765 | ||
a48d60b1 MD |
1766 | void |
1767 | scm_c_extend_primitive_generic (SCM extended, SCM extension) | |
1768 | { | |
1769 | if (goops_loaded_p) | |
1770 | { | |
1771 | SCM gf, gext; | |
1772 | if (!*SCM_SUBR_GENERIC (extended)) | |
1773 | scm_enable_primitive_generic_x (scm_list_1 (extended)); | |
1774 | gf = *SCM_SUBR_GENERIC (extended); | |
1775 | gext = scm_call_2 (SCM_VARIABLE_REF (scm_var_make_extended_generic), | |
1776 | gf, | |
ce471ab8 | 1777 | SCM_SUBR_NAME (extension)); |
feccd2d3 | 1778 | SCM_SET_SUBR_GENERIC (extension, gext); |
a48d60b1 MD |
1779 | } |
1780 | else | |
1781 | { | |
d0cad249 LC |
1782 | t_extension *e = scm_gc_malloc (sizeof (t_extension), |
1783 | extension_gc_hint); | |
a48d60b1 MD |
1784 | t_extension **loc = &extensions; |
1785 | /* Make sure that extensions are placed before their own | |
1786 | * extensions in the extensions list. O(N^2) algorithm, but | |
1787 | * extensions of primitive generics are rare. | |
1788 | */ | |
1789 | while (*loc && extension != (*loc)->extended) | |
1790 | loc = &(*loc)->next; | |
1791 | e->next = *loc; | |
1792 | e->extended = extended; | |
1793 | e->extension = extension; | |
1794 | *loc = e; | |
1795 | } | |
1796 | } | |
1797 | ||
1798 | static void | |
1799 | setup_extended_primitive_generics () | |
1800 | { | |
1801 | while (extensions) | |
1802 | { | |
1803 | t_extension *e = extensions; | |
1804 | scm_c_extend_primitive_generic (e->extended, e->extension); | |
1805 | extensions = e->next; | |
a48d60b1 MD |
1806 | } |
1807 | } | |
1808 | ||
80662eda | 1809 | /****************************************************************************** |
6d77c894 | 1810 | * |
80662eda | 1811 | * Protocol for calling a generic fumction |
6d77c894 | 1812 | * This protocol is roughly equivalent to (parameter are a little bit different |
80662eda MD |
1813 | * for efficiency reasons): |
1814 | * | |
1815 | * + apply-generic (gf args) | |
1816 | * + compute-applicable-methods (gf args ...) | |
1817 | * + sort-applicable-methods (methods args) | |
1818 | * + apply-methods (gf methods args) | |
6d77c894 TTN |
1819 | * |
1820 | * apply-methods calls make-next-method to build the "continuation" of a a | |
80662eda MD |
1821 | * method. Applying a next-method will call apply-next-method which in |
1822 | * turn will call apply again to call effectively the following method. | |
1823 | * | |
1824 | ******************************************************************************/ | |
1825 | ||
1826 | static int | |
1827 | applicablep (SCM actual, SCM formal) | |
1828 | { | |
79a3dafe | 1829 | /* We already know that the cpl is well formed. */ |
7888309b | 1830 | return scm_is_true (scm_c_memq (formal, SCM_SLOT (actual, scm_si_cpl))); |
80662eda MD |
1831 | } |
1832 | ||
1833 | static int | |
34d19ef6 | 1834 | more_specificp (SCM m1, SCM m2, SCM const *targs) |
80662eda MD |
1835 | { |
1836 | register SCM s1, s2; | |
c014a02e | 1837 | register long i; |
6d77c894 TTN |
1838 | /* |
1839 | * Note: | |
1840 | * m1 and m2 can have != length (i.e. one can be one element longer than the | |
80662eda MD |
1841 | * other when we have a dotted parameter list). For instance, with the call |
1842 | * (M 1) | |
1843 | * with | |
1844 | * (define-method M (a . l) ....) | |
6d77c894 | 1845 | * (define-method M (a) ....) |
80662eda MD |
1846 | * |
1847 | * we consider that the second method is more specific. | |
1848 | * | |
1849 | * BTW, targs is an array of types. We don't need it's size since | |
1850 | * we already know that m1 and m2 are applicable (no risk to go past | |
1851 | * the end of this array). | |
1852 | * | |
1853 | */ | |
34d19ef6 | 1854 | for (i=0, s1=SPEC_OF(m1), s2=SPEC_OF(m2); ; i++, s1=SCM_CDR(s1), s2=SCM_CDR(s2)) { |
d2e53ed6 MV |
1855 | if (scm_is_null(s1)) return 1; |
1856 | if (scm_is_null(s2)) return 0; | |
80662eda MD |
1857 | if (SCM_CAR(s1) != SCM_CAR(s2)) { |
1858 | register SCM l, cs1 = SCM_CAR(s1), cs2 = SCM_CAR(s2); | |
6d77c894 | 1859 | |
dcb410ec | 1860 | for (l = SCM_SLOT (targs[i], scm_si_cpl); ; l = SCM_CDR(l)) { |
80662eda MD |
1861 | if (cs1 == SCM_CAR(l)) |
1862 | return 1; | |
1863 | if (cs2 == SCM_CAR(l)) | |
1864 | return 0; | |
1865 | } | |
1866 | return 0;/* should not occur! */ | |
1867 | } | |
1868 | } | |
1869 | return 0; /* should not occur! */ | |
1870 | } | |
1871 | ||
1872 | #define BUFFSIZE 32 /* big enough for most uses */ | |
1873 | ||
1874 | static SCM | |
c014a02e | 1875 | scm_i_vector2list (SCM l, long len) |
80662eda | 1876 | { |
c014a02e | 1877 | long j; |
00ffa0e7 | 1878 | SCM z = scm_c_make_vector (len, SCM_UNDEFINED); |
6d77c894 | 1879 | |
80662eda | 1880 | for (j = 0; j < len; j++, l = SCM_CDR (l)) { |
4057a3e0 | 1881 | SCM_SIMPLE_VECTOR_SET (z, j, SCM_CAR (l)); |
80662eda MD |
1882 | } |
1883 | return z; | |
1884 | } | |
1885 | ||
1886 | static SCM | |
34d19ef6 | 1887 | sort_applicable_methods (SCM method_list, long size, SCM const *targs) |
80662eda | 1888 | { |
c014a02e | 1889 | long i, j, incr; |
80662eda MD |
1890 | SCM *v, vector = SCM_EOL; |
1891 | SCM buffer[BUFFSIZE]; | |
1892 | SCM save = method_list; | |
4057a3e0 | 1893 | scm_t_array_handle handle; |
80662eda MD |
1894 | |
1895 | /* For reasonably sized method_lists we can try to avoid all the | |
1896 | * consing and reorder the list in place... | |
1897 | * This idea is due to David McClain <Dave_McClain@msn.com> | |
1898 | */ | |
1899 | if (size <= BUFFSIZE) | |
1900 | { | |
1901 | for (i = 0; i < size; i++) | |
1902 | { | |
1903 | buffer[i] = SCM_CAR (method_list); | |
1904 | method_list = SCM_CDR (method_list); | |
1905 | } | |
1906 | v = buffer; | |
6d77c894 | 1907 | } |
80662eda MD |
1908 | else |
1909 | { | |
1910 | /* Too many elements in method_list to keep everything locally */ | |
1911 | vector = scm_i_vector2list (save, size); | |
4057a3e0 | 1912 | v = scm_vector_writable_elements (vector, &handle, NULL, NULL); |
80662eda MD |
1913 | } |
1914 | ||
6d77c894 | 1915 | /* Use a simple shell sort since it is generally faster than qsort on |
80662eda MD |
1916 | * small vectors (which is probably mostly the case when we have to |
1917 | * sort a list of applicable methods). | |
1918 | */ | |
1919 | for (incr = size / 2; incr; incr /= 2) | |
1920 | { | |
1921 | for (i = incr; i < size; i++) | |
1922 | { | |
1923 | for (j = i - incr; j >= 0; j -= incr) | |
1924 | { | |
1925 | if (more_specificp (v[j], v[j+incr], targs)) | |
1926 | break; | |
1927 | else | |
1928 | { | |
1929 | SCM tmp = v[j + incr]; | |
1930 | v[j + incr] = v[j]; | |
1931 | v[j] = tmp; | |
1932 | } | |
1933 | } | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | if (size <= BUFFSIZE) | |
1938 | { | |
1939 | /* We did it in locally, so restore the original list (reordered) in-place */ | |
1940 | for (i = 0, method_list = save; i < size; i++, v++) | |
1941 | { | |
1942 | SCM_SETCAR (method_list, *v); | |
1943 | method_list = SCM_CDR (method_list); | |
1944 | } | |
1945 | return save; | |
1946 | } | |
4057a3e0 | 1947 | |
6d77c894 | 1948 | /* If we are here, that's that we did it the hard way... */ |
c8857a4d | 1949 | scm_array_handle_release (&handle); |
80662eda MD |
1950 | return scm_vector_to_list (vector); |
1951 | } | |
1952 | ||
1953 | SCM | |
c014a02e | 1954 | scm_compute_applicable_methods (SCM gf, SCM args, long len, int find_method_p) |
80662eda | 1955 | { |
c014a02e ML |
1956 | register long i; |
1957 | long count = 0; | |
80662eda MD |
1958 | SCM l, fl, applicable = SCM_EOL; |
1959 | SCM save = args; | |
34d19ef6 HWN |
1960 | SCM buffer[BUFFSIZE]; |
1961 | SCM const *types; | |
1962 | SCM *p; | |
1963 | SCM tmp = SCM_EOL; | |
4057a3e0 | 1964 | scm_t_array_handle handle; |
6d77c894 | 1965 | |
80662eda | 1966 | /* Build the list of arguments types */ |
4057a3e0 MV |
1967 | if (len >= BUFFSIZE) |
1968 | { | |
1969 | tmp = scm_c_make_vector (len, SCM_UNDEFINED); | |
1970 | types = p = scm_vector_writable_elements (tmp, &handle, NULL, NULL); | |
34d19ef6 HWN |
1971 | |
1972 | /* | |
1973 | note that we don't have to work to reset the generation | |
1974 | count. TMP is a new vector anyway, and it is found | |
1975 | conservatively. | |
1976 | */ | |
4057a3e0 | 1977 | } |
80662eda MD |
1978 | else |
1979 | types = p = buffer; | |
6d77c894 | 1980 | |
d2e53ed6 | 1981 | for ( ; !scm_is_null (args); args = SCM_CDR (args)) |
80662eda | 1982 | *p++ = scm_class_of (SCM_CAR (args)); |
34d19ef6 | 1983 | |
80662eda | 1984 | /* Build a list of all applicable methods */ |
d2e53ed6 | 1985 | for (l = scm_generic_function_methods (gf); !scm_is_null (l); l = SCM_CDR (l)) |
80662eda MD |
1986 | { |
1987 | fl = SPEC_OF (SCM_CAR (l)); | |
80662eda MD |
1988 | for (i = 0; ; i++, fl = SCM_CDR (fl)) |
1989 | { | |
c312aca7 | 1990 | if (SCM_INSTANCEP (fl) |
80662eda | 1991 | /* We have a dotted argument list */ |
d2e53ed6 | 1992 | || (i >= len && scm_is_null (fl))) |
80662eda MD |
1993 | { /* both list exhausted */ |
1994 | applicable = scm_cons (SCM_CAR (l), applicable); | |
1995 | count += 1; | |
1996 | break; | |
1997 | } | |
1998 | if (i >= len | |
d2e53ed6 | 1999 | || scm_is_null (fl) |
80662eda MD |
2000 | || !applicablep (types[i], SCM_CAR (fl))) |
2001 | break; | |
2002 | } | |
2003 | } | |
2004 | ||
c8857a4d MV |
2005 | if (len >= BUFFSIZE) |
2006 | scm_array_handle_release (&handle); | |
2007 | ||
80662eda MD |
2008 | if (count == 0) |
2009 | { | |
2010 | if (find_method_p) | |
2011 | return SCM_BOOL_F; | |
bef95911 | 2012 | scm_call_2 (SCM_VARIABLE_REF (var_no_applicable_method), gf, save); |
80662eda MD |
2013 | /* if we are here, it's because no-applicable-method hasn't signaled an error */ |
2014 | return SCM_BOOL_F; | |
2015 | } | |
34d19ef6 | 2016 | |
80662eda MD |
2017 | return (count == 1 |
2018 | ? applicable | |
2019 | : sort_applicable_methods (applicable, count, types)); | |
2020 | } | |
2021 | ||
2022 | #if 0 | |
2023 | SCM_PROC (s_sys_compute_applicable_methods, "%compute-applicable-methods", 2, 0, 0, scm_sys_compute_applicable_methods); | |
2024 | #endif | |
2025 | ||
2026 | static const char s_sys_compute_applicable_methods[] = "%compute-applicable-methods"; | |
2027 | ||
2028 | SCM | |
2029 | scm_sys_compute_applicable_methods (SCM gf, SCM args) | |
398d8ee1 | 2030 | #define FUNC_NAME s_sys_compute_applicable_methods |
80662eda | 2031 | { |
c014a02e | 2032 | long n; |
398d8ee1 | 2033 | SCM_VALIDATE_GENERIC (1, gf); |
80662eda | 2034 | n = scm_ilength (args); |
398d8ee1 | 2035 | SCM_ASSERT (n >= 0, args, SCM_ARG2, FUNC_NAME); |
80662eda MD |
2036 | return scm_compute_applicable_methods (gf, args, n, 1); |
2037 | } | |
398d8ee1 | 2038 | #undef FUNC_NAME |
80662eda | 2039 | |
86d31dfe | 2040 | SCM_SYMBOL (sym_compute_applicable_methods, "compute-applicable-methods"); |
9a441ddb | 2041 | 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 | 2042 | |
80662eda MD |
2043 | /****************************************************************************** |
2044 | * | |
2045 | * A simple make (which will be redefined later in Scheme) | |
2046 | * This version handles only creation of gf, methods and classes (no instances) | |
2047 | * | |
6d77c894 | 2048 | * Since this code will disappear when Goops will be fully booted, |
80662eda MD |
2049 | * no precaution is taken to be efficient. |
2050 | * | |
2051 | ******************************************************************************/ | |
2052 | ||
2053 | SCM_KEYWORD (k_setter, "setter"); | |
2054 | SCM_KEYWORD (k_specializers, "specializers"); | |
2055 | SCM_KEYWORD (k_procedure, "procedure"); | |
5487977b AW |
2056 | SCM_KEYWORD (k_formals, "formals"); |
2057 | SCM_KEYWORD (k_body, "body"); | |
e177058b | 2058 | SCM_KEYWORD (k_make_procedure, "make-procedure"); |
80662eda MD |
2059 | SCM_KEYWORD (k_dsupers, "dsupers"); |
2060 | SCM_KEYWORD (k_slots, "slots"); | |
2061 | SCM_KEYWORD (k_gf, "generic-function"); | |
2062 | ||
398d8ee1 KN |
2063 | SCM_DEFINE (scm_make, "make", 0, 0, 1, |
2064 | (SCM args), | |
27c37006 | 2065 | "Make a new object. @var{args} must contain the class and\n" |
6bcefd15 | 2066 | "all necessary initialization information.") |
398d8ee1 | 2067 | #define FUNC_NAME s_scm_make |
80662eda MD |
2068 | { |
2069 | SCM class, z; | |
c014a02e | 2070 | long len = scm_ilength (args); |
80662eda MD |
2071 | |
2072 | if (len <= 0 || (len & 1) == 0) | |
398d8ee1 | 2073 | SCM_WRONG_NUM_ARGS (); |
80662eda MD |
2074 | |
2075 | class = SCM_CAR(args); | |
2076 | args = SCM_CDR(args); | |
2077 | ||
f8af5c6d | 2078 | if (class == scm_class_generic || class == scm_class_accessor) |
80662eda | 2079 | { |
80662eda | 2080 | z = scm_make_struct (class, SCM_INUM0, |
72d2e7e6 | 2081 | scm_list_4 (SCM_BOOL_F, |
51f66c91 | 2082 | SCM_EOL, |
1afff620 | 2083 | SCM_INUM0, |
bbf8d523 | 2084 | SCM_EOL)); |
80662eda MD |
2085 | scm_set_procedure_property_x (z, scm_sym_name, |
2086 | scm_get_keyword (k_name, | |
2087 | args, | |
2088 | SCM_BOOL_F)); | |
2089 | clear_method_cache (z); | |
f8af5c6d | 2090 | if (class == scm_class_accessor) |
80662eda MD |
2091 | { |
2092 | SCM setter = scm_get_keyword (k_setter, args, SCM_BOOL_F); | |
7888309b | 2093 | if (scm_is_true (setter)) |
80662eda MD |
2094 | scm_sys_set_object_setter_x (z, setter); |
2095 | } | |
2096 | } | |
2097 | else | |
2098 | { | |
2099 | z = scm_sys_allocate_instance (class, args); | |
2100 | ||
2101 | if (class == scm_class_method | |
f8af5c6d | 2102 | || class == scm_class_accessor_method) |
80662eda | 2103 | { |
6d77c894 | 2104 | SCM_SET_SLOT (z, scm_si_generic_function, |
80662eda MD |
2105 | scm_i_get_keyword (k_gf, |
2106 | args, | |
2107 | len - 1, | |
2108 | SCM_BOOL_F, | |
dcb410ec | 2109 | FUNC_NAME)); |
6d77c894 | 2110 | SCM_SET_SLOT (z, scm_si_specializers, |
80662eda MD |
2111 | scm_i_get_keyword (k_specializers, |
2112 | args, | |
2113 | len - 1, | |
2114 | SCM_EOL, | |
dcb410ec | 2115 | FUNC_NAME)); |
6d77c894 | 2116 | SCM_SET_SLOT (z, scm_si_procedure, |
80662eda MD |
2117 | scm_i_get_keyword (k_procedure, |
2118 | args, | |
2119 | len - 1, | |
e177058b | 2120 | SCM_BOOL_F, |
dcb410ec | 2121 | FUNC_NAME)); |
5487977b AW |
2122 | SCM_SET_SLOT (z, scm_si_formals, |
2123 | scm_i_get_keyword (k_formals, | |
2124 | args, | |
2125 | len - 1, | |
2126 | SCM_EOL, | |
2127 | FUNC_NAME)); | |
2128 | SCM_SET_SLOT (z, scm_si_body, | |
2129 | scm_i_get_keyword (k_body, | |
2130 | args, | |
2131 | len - 1, | |
2132 | SCM_EOL, | |
2133 | FUNC_NAME)); | |
e177058b AW |
2134 | SCM_SET_SLOT (z, scm_si_make_procedure, |
2135 | scm_i_get_keyword (k_make_procedure, | |
5487977b AW |
2136 | args, |
2137 | len - 1, | |
2138 | SCM_BOOL_F, | |
2139 | FUNC_NAME)); | |
80662eda MD |
2140 | } |
2141 | else | |
2142 | { | |
2143 | /* In all the others case, make a new class .... No instance here */ | |
b6cf4d02 | 2144 | SCM_SET_SLOT (z, scm_vtable_index_name, |
80662eda MD |
2145 | scm_i_get_keyword (k_name, |
2146 | args, | |
2147 | len - 1, | |
cc95e00a | 2148 | scm_from_locale_symbol ("???"), |
dcb410ec | 2149 | FUNC_NAME)); |
6d77c894 | 2150 | SCM_SET_SLOT (z, scm_si_direct_supers, |
80662eda MD |
2151 | scm_i_get_keyword (k_dsupers, |
2152 | args, | |
2153 | len - 1, | |
2154 | SCM_EOL, | |
dcb410ec | 2155 | FUNC_NAME)); |
6d77c894 | 2156 | SCM_SET_SLOT (z, scm_si_direct_slots, |
80662eda MD |
2157 | scm_i_get_keyword (k_slots, |
2158 | args, | |
2159 | len - 1, | |
2160 | SCM_EOL, | |
dcb410ec | 2161 | FUNC_NAME)); |
80662eda MD |
2162 | } |
2163 | } | |
2164 | return z; | |
2165 | } | |
398d8ee1 | 2166 | #undef FUNC_NAME |
80662eda | 2167 | |
398d8ee1 KN |
2168 | SCM_DEFINE (scm_find_method, "find-method", 0, 0, 1, |
2169 | (SCM l), | |
2170 | "") | |
2171 | #define FUNC_NAME s_scm_find_method | |
80662eda MD |
2172 | { |
2173 | SCM gf; | |
c014a02e | 2174 | long len = scm_ilength (l); |
80662eda MD |
2175 | |
2176 | if (len == 0) | |
398d8ee1 | 2177 | SCM_WRONG_NUM_ARGS (); |
80662eda MD |
2178 | |
2179 | gf = SCM_CAR(l); l = SCM_CDR(l); | |
398d8ee1 | 2180 | SCM_VALIDATE_GENERIC (1, gf); |
d2e53ed6 | 2181 | if (scm_is_null (SCM_SLOT (gf, scm_si_methods))) |
1afff620 | 2182 | SCM_MISC_ERROR ("no methods for generic ~S", scm_list_1 (gf)); |
80662eda MD |
2183 | |
2184 | return scm_compute_applicable_methods (gf, l, len - 1, 1); | |
2185 | } | |
398d8ee1 | 2186 | #undef FUNC_NAME |
80662eda | 2187 | |
398d8ee1 KN |
2188 | SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0, |
2189 | (SCM m1, SCM m2, SCM targs), | |
b1f57ea4 LC |
2190 | "Return true if method @var{m1} is more specific than @var{m2} " |
2191 | "given the argument types (classes) listed in @var{targs}.") | |
398d8ee1 | 2192 | #define FUNC_NAME s_scm_sys_method_more_specific_p |
80662eda | 2193 | { |
4057a3e0 MV |
2194 | SCM l, v, result; |
2195 | SCM *v_elts; | |
b1f57ea4 | 2196 | long i, len, m1_specs, m2_specs; |
4057a3e0 | 2197 | scm_t_array_handle handle; |
80662eda | 2198 | |
398d8ee1 KN |
2199 | SCM_VALIDATE_METHOD (1, m1); |
2200 | SCM_VALIDATE_METHOD (2, m2); | |
80662eda | 2201 | |
b1f57ea4 LC |
2202 | len = scm_ilength (targs); |
2203 | m1_specs = scm_ilength (SPEC_OF (m1)); | |
2204 | m2_specs = scm_ilength (SPEC_OF (m2)); | |
2205 | SCM_ASSERT ((len >= m1_specs) || (len >= m2_specs), | |
2206 | targs, SCM_ARG3, FUNC_NAME); | |
2207 | ||
2208 | /* Verify that all the arguments of TARGS are classes and place them | |
2209 | in a vector. */ | |
4057a3e0 | 2210 | |
00ffa0e7 | 2211 | v = scm_c_make_vector (len, SCM_EOL); |
4057a3e0 | 2212 | v_elts = scm_vector_writable_elements (v, &handle, NULL, NULL); |
80662eda | 2213 | |
b1f57ea4 LC |
2214 | for (i = 0, l = targs; |
2215 | i < len && scm_is_pair (l); | |
2216 | i++, l = SCM_CDR (l)) | |
4057a3e0 MV |
2217 | { |
2218 | SCM_ASSERT (SCM_CLASSP (SCM_CAR (l)), targs, SCM_ARG3, FUNC_NAME); | |
b1f57ea4 | 2219 | v_elts[i] = SCM_CAR (l); |
4057a3e0 | 2220 | } |
4057a3e0 | 2221 | result = more_specificp (m1, m2, v_elts) ? SCM_BOOL_T: SCM_BOOL_F; |
c8857a4d MV |
2222 | |
2223 | scm_array_handle_release (&handle); | |
2224 | ||
4057a3e0 | 2225 | return result; |
80662eda | 2226 | } |
398d8ee1 | 2227 | #undef FUNC_NAME |
6d77c894 TTN |
2228 | |
2229 | ||
80662eda MD |
2230 | |
2231 | /****************************************************************************** | |
2232 | * | |
6d77c894 | 2233 | * Initializations |
80662eda MD |
2234 | * |
2235 | ******************************************************************************/ | |
2236 | ||
74b6d6e4 MD |
2237 | static void |
2238 | fix_cpl (SCM c, SCM before, SCM after) | |
2239 | { | |
2240 | SCM cpl = SCM_SLOT (c, scm_si_cpl); | |
2241 | SCM ls = scm_c_memq (after, cpl); | |
2242 | SCM tail = scm_delq1_x (before, SCM_CDR (ls)); | |
7888309b | 2243 | if (scm_is_false (ls)) |
74b6d6e4 MD |
2244 | /* if this condition occurs, fix_cpl should not be applied this way */ |
2245 | abort (); | |
2246 | SCM_SETCAR (ls, before); | |
2247 | SCM_SETCDR (ls, scm_cons (after, tail)); | |
2248 | { | |
2249 | SCM dslots = SCM_SLOT (c, scm_si_direct_slots); | |
2250 | SCM slots = build_slots_list (maplist (dslots), cpl); | |
2251 | SCM g_n_s = compute_getters_n_setters (slots); | |
2252 | SCM_SET_SLOT (c, scm_si_slots, slots); | |
2253 | SCM_SET_SLOT (c, scm_si_getters_n_setters, g_n_s); | |
2254 | } | |
2255 | } | |
2256 | ||
80662eda MD |
2257 | |
2258 | static void | |
2259 | make_stdcls (SCM *var, char *name, SCM meta, SCM super, SCM slots) | |
2260 | { | |
cc95e00a | 2261 | SCM tmp = scm_from_locale_symbol (name); |
6d77c894 | 2262 | |
80662eda MD |
2263 | *var = scm_permanent_object (scm_basic_make_class (meta, |
2264 | tmp, | |
d2e53ed6 | 2265 | scm_is_pair (super) |
80662eda | 2266 | ? super |
1afff620 | 2267 | : scm_list_1 (super), |
80662eda MD |
2268 | slots)); |
2269 | DEFVAR(tmp, *var); | |
2270 | } | |
2271 | ||
2272 | ||
2273 | SCM_KEYWORD (k_slot_definition, "slot-definition"); | |
2274 | ||
2275 | static void | |
2276 | create_standard_classes (void) | |
2277 | { | |
2278 | SCM slots; | |
21497600 | 2279 | SCM method_slots = scm_list_n (scm_from_locale_symbol ("generic-function"), |
cc95e00a | 2280 | scm_from_locale_symbol ("specializers"), |
6b80d352 | 2281 | sym_procedure, |
21497600 AW |
2282 | scm_from_locale_symbol ("formals"), |
2283 | scm_from_locale_symbol ("body"), | |
e177058b | 2284 | scm_from_locale_symbol ("make-procedure"), |
21497600 | 2285 | SCM_UNDEFINED); |
cc95e00a | 2286 | SCM amethod_slots = scm_list_1 (scm_list_3 (scm_from_locale_symbol ("slot-definition"), |
1afff620 KN |
2287 | k_init_keyword, |
2288 | k_slot_definition)); | |
c06e3eb0 | 2289 | SCM gf_slots = scm_list_4 (scm_from_locale_symbol ("methods"), |
cc95e00a | 2290 | scm_list_3 (scm_from_locale_symbol ("n-specialized"), |
1afff620 KN |
2291 | k_init_value, |
2292 | SCM_INUM0), | |
cc95e00a | 2293 | scm_list_3 (scm_from_locale_symbol ("extended-by"), |
bbf8d523 | 2294 | k_init_value, |
b6cf4d02 | 2295 | SCM_EOL), |
72d2e7e6 | 2296 | scm_from_locale_symbol ("effective-methods")); |
a9a90a88 | 2297 | SCM setter_slots = scm_list_1 (sym_setter); |
cc95e00a | 2298 | SCM egf_slots = scm_list_1 (scm_list_3 (scm_from_locale_symbol ("extends"), |
bbf8d523 MD |
2299 | k_init_value, |
2300 | SCM_EOL)); | |
80662eda MD |
2301 | /* Foreign class slot classes */ |
2302 | make_stdcls (&scm_class_foreign_slot, "<foreign-slot>", | |
2303 | scm_class_class, scm_class_top, SCM_EOL); | |
2304 | make_stdcls (&scm_class_protected, "<protected-slot>", | |
2305 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
b6cf4d02 AW |
2306 | make_stdcls (&scm_class_hidden, "<hidden-slot>", |
2307 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
80662eda MD |
2308 | make_stdcls (&scm_class_opaque, "<opaque-slot>", |
2309 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
2310 | make_stdcls (&scm_class_read_only, "<read-only-slot>", | |
2311 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
2312 | make_stdcls (&scm_class_self, "<self-slot>", | |
b6cf4d02 | 2313 | scm_class_class, scm_class_read_only, SCM_EOL); |
80662eda MD |
2314 | make_stdcls (&scm_class_protected_opaque, "<protected-opaque-slot>", |
2315 | scm_class_class, | |
1afff620 | 2316 | scm_list_2 (scm_class_protected, scm_class_opaque), |
80662eda | 2317 | SCM_EOL); |
b6cf4d02 AW |
2318 | make_stdcls (&scm_class_protected_hidden, "<protected-hidden-slot>", |
2319 | scm_class_class, | |
2320 | scm_list_2 (scm_class_protected, scm_class_hidden), | |
2321 | SCM_EOL); | |
80662eda MD |
2322 | make_stdcls (&scm_class_protected_read_only, "<protected-read-only-slot>", |
2323 | scm_class_class, | |
1afff620 | 2324 | scm_list_2 (scm_class_protected, scm_class_read_only), |
80662eda MD |
2325 | SCM_EOL); |
2326 | make_stdcls (&scm_class_scm, "<scm-slot>", | |
2327 | scm_class_class, scm_class_protected, SCM_EOL); | |
2328 | make_stdcls (&scm_class_int, "<int-slot>", | |
2329 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
2330 | make_stdcls (&scm_class_float, "<float-slot>", | |
2331 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
2332 | make_stdcls (&scm_class_double, "<double-slot>", | |
2333 | scm_class_class, scm_class_foreign_slot, SCM_EOL); | |
2334 | ||
2335 | /* Continue initialization of class <class> */ | |
6d77c894 | 2336 | |
80662eda | 2337 | slots = build_class_class_slots (); |
dcb410ec DH |
2338 | SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots); |
2339 | SCM_SET_SLOT (scm_class_class, scm_si_slots, slots); | |
2340 | SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters, | |
2341 | compute_getters_n_setters (slots)); | |
6d77c894 | 2342 | |
80662eda MD |
2343 | /* scm_class_generic functions classes */ |
2344 | make_stdcls (&scm_class_procedure_class, "<procedure-class>", | |
2345 | scm_class_class, scm_class_class, SCM_EOL); | |
51f66c91 | 2346 | make_stdcls (&scm_class_applicable_struct_class, "<applicable-struct-class>", |
80662eda | 2347 | scm_class_class, scm_class_procedure_class, SCM_EOL); |
2f652c68 | 2348 | SCM_SET_VTABLE_FLAGS (scm_class_applicable_struct_class, SCM_VTABLE_FLAG_APPLICABLE_VTABLE); |
80662eda MD |
2349 | make_stdcls (&scm_class_method, "<method>", |
2350 | scm_class_class, scm_class_object, method_slots); | |
f8af5c6d | 2351 | make_stdcls (&scm_class_accessor_method, "<accessor-method>", |
51f66c91 | 2352 | scm_class_class, scm_class_method, amethod_slots); |
74b6d6e4 MD |
2353 | make_stdcls (&scm_class_applicable, "<applicable>", |
2354 | scm_class_class, scm_class_top, SCM_EOL); | |
51f66c91 AW |
2355 | make_stdcls (&scm_class_applicable_struct, "<applicable-struct>", |
2356 | scm_class_applicable_struct_class, | |
74b6d6e4 | 2357 | scm_list_2 (scm_class_object, scm_class_applicable), |
51f66c91 | 2358 | scm_list_1 (sym_procedure)); |
80662eda | 2359 | make_stdcls (&scm_class_generic, "<generic>", |
51f66c91 | 2360 | scm_class_applicable_struct_class, scm_class_applicable_struct, gf_slots); |
80662eda | 2361 | SCM_SET_CLASS_FLAGS (scm_class_generic, SCM_CLASSF_PURE_GENERIC); |
bbf8d523 | 2362 | make_stdcls (&scm_class_extended_generic, "<extended-generic>", |
51f66c91 | 2363 | scm_class_applicable_struct_class, scm_class_generic, egf_slots); |
bbf8d523 | 2364 | SCM_SET_CLASS_FLAGS (scm_class_extended_generic, SCM_CLASSF_PURE_GENERIC); |
80662eda | 2365 | make_stdcls (&scm_class_generic_with_setter, "<generic-with-setter>", |
51f66c91 | 2366 | scm_class_applicable_struct_class, scm_class_generic, setter_slots); |
80662eda | 2367 | SCM_SET_CLASS_FLAGS (scm_class_generic_with_setter, SCM_CLASSF_PURE_GENERIC); |
f8af5c6d | 2368 | make_stdcls (&scm_class_accessor, "<accessor>", |
51f66c91 | 2369 | scm_class_applicable_struct_class, scm_class_generic_with_setter, SCM_EOL); |
f8af5c6d | 2370 | SCM_SET_CLASS_FLAGS (scm_class_accessor, SCM_CLASSF_PURE_GENERIC); |
bbf8d523 MD |
2371 | make_stdcls (&scm_class_extended_generic_with_setter, |
2372 | "<extended-generic-with-setter>", | |
51f66c91 | 2373 | scm_class_applicable_struct_class, |
74b6d6e4 MD |
2374 | scm_list_2 (scm_class_generic_with_setter, |
2375 | scm_class_extended_generic), | |
bbf8d523 MD |
2376 | SCM_EOL); |
2377 | SCM_SET_CLASS_FLAGS (scm_class_extended_generic_with_setter, | |
2378 | SCM_CLASSF_PURE_GENERIC); | |
74b6d6e4 | 2379 | make_stdcls (&scm_class_extended_accessor, "<extended-accessor>", |
51f66c91 | 2380 | scm_class_applicable_struct_class, |
74b6d6e4 MD |
2381 | scm_list_2 (scm_class_accessor, |
2382 | scm_class_extended_generic_with_setter), | |
2383 | SCM_EOL); | |
2384 | fix_cpl (scm_class_extended_accessor, | |
2385 | scm_class_extended_generic, scm_class_generic); | |
2386 | SCM_SET_CLASS_FLAGS (scm_class_extended_accessor, SCM_CLASSF_PURE_GENERIC); | |
80662eda MD |
2387 | |
2388 | /* Primitive types classes */ | |
2389 | make_stdcls (&scm_class_boolean, "<boolean>", | |
2390 | scm_class_class, scm_class_top, SCM_EOL); | |
2391 | make_stdcls (&scm_class_char, "<char>", | |
2392 | scm_class_class, scm_class_top, SCM_EOL); | |
2393 | make_stdcls (&scm_class_list, "<list>", | |
2394 | scm_class_class, scm_class_top, SCM_EOL); | |
2395 | make_stdcls (&scm_class_pair, "<pair>", | |
2396 | scm_class_class, scm_class_list, SCM_EOL); | |
2397 | make_stdcls (&scm_class_null, "<null>", | |
2398 | scm_class_class, scm_class_list, SCM_EOL); | |
2399 | make_stdcls (&scm_class_string, "<string>", | |
2400 | scm_class_class, scm_class_top, SCM_EOL); | |
2401 | make_stdcls (&scm_class_symbol, "<symbol>", | |
2402 | scm_class_class, scm_class_top, SCM_EOL); | |
2403 | make_stdcls (&scm_class_vector, "<vector>", | |
2404 | scm_class_class, scm_class_top, SCM_EOL); | |
c99de5aa AW |
2405 | make_stdcls (&scm_class_hashtable, "<hashtable>", |
2406 | scm_class_class, scm_class_top, SCM_EOL); | |
80662eda MD |
2407 | make_stdcls (&scm_class_number, "<number>", |
2408 | scm_class_class, scm_class_top, SCM_EOL); | |
2409 | make_stdcls (&scm_class_complex, "<complex>", | |
2410 | scm_class_class, scm_class_number, SCM_EOL); | |
2411 | make_stdcls (&scm_class_real, "<real>", | |
2412 | scm_class_class, scm_class_complex, SCM_EOL); | |
2413 | make_stdcls (&scm_class_integer, "<integer>", | |
2414 | scm_class_class, scm_class_real, SCM_EOL); | |
f92e85f7 MV |
2415 | make_stdcls (&scm_class_fraction, "<fraction>", |
2416 | scm_class_class, scm_class_real, SCM_EOL); | |
80662eda MD |
2417 | make_stdcls (&scm_class_keyword, "<keyword>", |
2418 | scm_class_class, scm_class_top, SCM_EOL); | |
2419 | make_stdcls (&scm_class_unknown, "<unknown>", | |
2420 | scm_class_class, scm_class_top, SCM_EOL); | |
2421 | make_stdcls (&scm_class_procedure, "<procedure>", | |
74b6d6e4 | 2422 | scm_class_procedure_class, scm_class_applicable, SCM_EOL); |
80662eda MD |
2423 | make_stdcls (&scm_class_procedure_with_setter, "<procedure-with-setter>", |
2424 | scm_class_procedure_class, scm_class_procedure, SCM_EOL); | |
2425 | make_stdcls (&scm_class_primitive_generic, "<primitive-generic>", | |
2426 | scm_class_procedure_class, scm_class_procedure, SCM_EOL); | |
2427 | make_stdcls (&scm_class_port, "<port>", | |
2428 | scm_class_class, scm_class_top, SCM_EOL); | |
2429 | make_stdcls (&scm_class_input_port, "<input-port>", | |
2430 | scm_class_class, scm_class_port, SCM_EOL); | |
2431 | make_stdcls (&scm_class_output_port, "<output-port>", | |
2432 | scm_class_class, scm_class_port, SCM_EOL); | |
2433 | make_stdcls (&scm_class_input_output_port, "<input-output-port>", | |
2434 | scm_class_class, | |
1afff620 | 2435 | scm_list_2 (scm_class_input_port, scm_class_output_port), |
80662eda MD |
2436 | SCM_EOL); |
2437 | } | |
2438 | ||
2439 | /********************************************************************** | |
2440 | * | |
2441 | * Smob classes | |
2442 | * | |
2443 | **********************************************************************/ | |
2444 | ||
2445 | static SCM | |
da0e6c2b | 2446 | make_class_from_template (char const *template, char const *type_name, SCM supers, int applicablep) |
80662eda MD |
2447 | { |
2448 | SCM class, name; | |
2449 | if (type_name) | |
2450 | { | |
2451 | char buffer[100]; | |
2452 | sprintf (buffer, template, type_name); | |
cc95e00a | 2453 | name = scm_from_locale_symbol (buffer); |
80662eda MD |
2454 | } |
2455 | else | |
2456 | name = SCM_GOOPS_UNBOUND; | |
2457 | ||
74b6d6e4 MD |
2458 | class = scm_permanent_object (scm_basic_make_class (applicablep |
2459 | ? scm_class_procedure_class | |
2460 | : scm_class_class, | |
80662eda MD |
2461 | name, |
2462 | supers, | |
2463 | SCM_EOL)); | |
2464 | ||
2465 | /* Only define name if doesn't already exist. */ | |
2466 | if (!SCM_GOOPS_UNBOUNDP (name) | |
bef95911 | 2467 | && scm_is_false (scm_module_variable (scm_module_goops, name))) |
0ba8a0a5 | 2468 | DEFVAR (name, class); |
80662eda MD |
2469 | return class; |
2470 | } | |
2471 | ||
9db8cf16 MG |
2472 | static SCM |
2473 | make_class_from_symbol (SCM type_name_sym, SCM supers, int applicablep) | |
2474 | { | |
2475 | SCM class, name; | |
2476 | if (type_name_sym != SCM_BOOL_F) | |
2477 | { | |
2478 | name = scm_string_append (scm_list_3 (scm_from_locale_string ("<"), | |
2479 | scm_symbol_to_string (type_name_sym), | |
2480 | scm_from_locale_string (">"))); | |
2481 | name = scm_string_to_symbol (name); | |
2482 | } | |
2483 | else | |
2484 | name = SCM_GOOPS_UNBOUND; | |
2485 | ||
2486 | class = scm_permanent_object (scm_basic_make_class (applicablep | |
2487 | ? scm_class_procedure_class | |
2488 | : scm_class_class, | |
2489 | name, | |
2490 | supers, | |
2491 | SCM_EOL)); | |
2492 | ||
2493 | /* Only define name if doesn't already exist. */ | |
2494 | if (!SCM_GOOPS_UNBOUNDP (name) | |
2495 | && scm_is_false (scm_module_variable (scm_module_goops, name))) | |
2496 | DEFVAR (name, class); | |
2497 | return class; | |
2498 | } | |
2499 | ||
80662eda | 2500 | SCM |
da0e6c2b | 2501 | scm_make_extended_class (char const *type_name, int applicablep) |
80662eda MD |
2502 | { |
2503 | return make_class_from_template ("<%s>", | |
2504 | type_name, | |
74b6d6e4 MD |
2505 | scm_list_1 (applicablep |
2506 | ? scm_class_applicable | |
2507 | : scm_class_top), | |
2508 | applicablep); | |
2509 | } | |
2510 | ||
9db8cf16 MG |
2511 | static SCM |
2512 | scm_make_extended_class_from_symbol (SCM type_name_sym, int applicablep) | |
2513 | { | |
2514 | return make_class_from_symbol (type_name_sym, | |
2515 | scm_list_1 (applicablep | |
2516 | ? scm_class_applicable | |
2517 | : scm_class_top), | |
2518 | applicablep); | |
2519 | } | |
2520 | ||
74b6d6e4 MD |
2521 | void |
2522 | scm_i_inherit_applicable (SCM c) | |
2523 | { | |
2524 | if (!SCM_SUBCLASSP (c, scm_class_applicable)) | |
2525 | { | |
2526 | SCM dsupers = SCM_SLOT (c, scm_si_direct_supers); | |
2527 | SCM cpl = SCM_SLOT (c, scm_si_cpl); | |
2528 | /* patch scm_class_applicable into direct-supers */ | |
2529 | SCM top = scm_c_memq (scm_class_top, dsupers); | |
7888309b | 2530 | if (scm_is_false (top)) |
74b6d6e4 MD |
2531 | dsupers = scm_append (scm_list_2 (dsupers, |
2532 | scm_list_1 (scm_class_applicable))); | |
2533 | else | |
2534 | { | |
2535 | SCM_SETCAR (top, scm_class_applicable); | |
2536 | SCM_SETCDR (top, scm_cons (scm_class_top, SCM_CDR (top))); | |
2537 | } | |
2538 | SCM_SET_SLOT (c, scm_si_direct_supers, dsupers); | |
2539 | /* patch scm_class_applicable into cpl */ | |
2540 | top = scm_c_memq (scm_class_top, cpl); | |
7888309b | 2541 | if (scm_is_false (top)) |
74b6d6e4 MD |
2542 | abort (); |
2543 | else | |
2544 | { | |
2545 | SCM_SETCAR (top, scm_class_applicable); | |
2546 | SCM_SETCDR (top, scm_cons (scm_class_top, SCM_CDR (top))); | |
2547 | } | |
2548 | /* add class to direct-subclasses of scm_class_applicable */ | |
2549 | SCM_SET_SLOT (scm_class_applicable, | |
2550 | scm_si_direct_subclasses, | |
2551 | scm_cons (c, SCM_SLOT (scm_class_applicable, | |
2552 | scm_si_direct_subclasses))); | |
2553 | } | |
80662eda MD |
2554 | } |
2555 | ||
2556 | static void | |
2557 | create_smob_classes (void) | |
2558 | { | |
c014a02e | 2559 | long i; |
80662eda | 2560 | |
c891a40e | 2561 | for (i = 0; i < SCM_I_MAX_SMOB_TYPE_COUNT; ++i) |
80662eda MD |
2562 | scm_smob_class[i] = 0; |
2563 | ||
80662eda | 2564 | scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_keyword)] = scm_class_keyword; |
6d77c894 | 2565 | |
80662eda MD |
2566 | for (i = 0; i < scm_numsmob; ++i) |
2567 | if (!scm_smob_class[i]) | |
74b6d6e4 MD |
2568 | scm_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i), |
2569 | scm_smobs[i].apply != 0); | |
80662eda MD |
2570 | } |
2571 | ||
2572 | void | |
c014a02e | 2573 | scm_make_port_classes (long ptobnum, char *type_name) |
80662eda MD |
2574 | { |
2575 | SCM c, class = make_class_from_template ("<%s-port>", | |
2576 | type_name, | |
74b6d6e4 MD |
2577 | scm_list_1 (scm_class_port), |
2578 | 0); | |
80662eda MD |
2579 | scm_port_class[SCM_IN_PCLASS_INDEX + ptobnum] |
2580 | = make_class_from_template ("<%s-input-port>", | |
2581 | type_name, | |
74b6d6e4 MD |
2582 | scm_list_2 (class, scm_class_input_port), |
2583 | 0); | |
80662eda MD |
2584 | scm_port_class[SCM_OUT_PCLASS_INDEX + ptobnum] |
2585 | = make_class_from_template ("<%s-output-port>", | |
2586 | type_name, | |
74b6d6e4 MD |
2587 | scm_list_2 (class, scm_class_output_port), |
2588 | 0); | |
80662eda MD |
2589 | scm_port_class[SCM_INOUT_PCLASS_INDEX + ptobnum] |
2590 | = c | |
2591 | = make_class_from_template ("<%s-input-output-port>", | |
2592 | type_name, | |
74b6d6e4 MD |
2593 | scm_list_2 (class, scm_class_input_output_port), |
2594 | 0); | |
80662eda | 2595 | /* Patch cpl (since this tree is too complex for the C level compute-cpl) */ |
dcb410ec DH |
2596 | SCM_SET_SLOT (c, scm_si_cpl, |
2597 | scm_cons2 (c, class, SCM_SLOT (scm_class_input_output_port, scm_si_cpl))); | |
80662eda MD |
2598 | } |
2599 | ||
2600 | static void | |
2601 | create_port_classes (void) | |
2602 | { | |
c014a02e | 2603 | long i; |
80662eda | 2604 | |
80662eda MD |
2605 | for (i = 0; i < scm_numptob; ++i) |
2606 | scm_make_port_classes (i, SCM_PTOBNAME (i)); | |
2607 | } | |
2608 | ||
2609 | static SCM | |
74b6d6e4 MD |
2610 | make_struct_class (void *closure SCM_UNUSED, |
2611 | SCM vtable, SCM data, SCM prev SCM_UNUSED) | |
80662eda | 2612 | { |
9db8cf16 MG |
2613 | SCM sym = SCM_STRUCT_TABLE_NAME (data); |
2614 | if (scm_is_true (sym)) | |
2615 | { | |
b6cf4d02 | 2616 | int applicablep = SCM_CLASS_FLAGS (vtable) & SCM_VTABLE_FLAG_APPLICABLE; |
9db8cf16 MG |
2617 | |
2618 | SCM_SET_STRUCT_TABLE_CLASS (data, | |
2619 | scm_make_extended_class_from_symbol (sym, applicablep)); | |
2620 | } | |
2621 | ||
2622 | scm_remember_upto_here_2 (data, vtable); | |
80662eda MD |
2623 | return SCM_UNSPECIFIED; |
2624 | } | |
2625 | ||
2626 | static void | |
2627 | create_struct_classes (void) | |
2628 | { | |
2629 | scm_internal_hash_fold (make_struct_class, 0, SCM_BOOL_F, scm_struct_table); | |
2630 | } | |
2631 | ||
2632 | /********************************************************************** | |
2633 | * | |
2634 | * C interface | |
2635 | * | |
2636 | **********************************************************************/ | |
2637 | ||
2638 | void | |
2639 | scm_load_goops () | |
2640 | { | |
2641 | if (!goops_loaded_p) | |
abd28220 | 2642 | scm_c_resolve_module ("oop goops"); |
80662eda MD |
2643 | } |
2644 | ||
e11208ca | 2645 | |
80662eda MD |
2646 | SCM_SYMBOL (sym_o, "o"); |
2647 | SCM_SYMBOL (sym_x, "x"); | |
2648 | ||
2649 | SCM_KEYWORD (k_accessor, "accessor"); | |
2650 | SCM_KEYWORD (k_getter, "getter"); | |
2651 | ||
80662eda MD |
2652 | SCM |
2653 | scm_ensure_accessor (SCM name) | |
2654 | { | |
fdc28395 | 2655 | SCM gf = scm_call_2 (SCM_TOP_LEVEL_LOOKUP_CLOSURE, name, SCM_BOOL_F); |
f8af5c6d | 2656 | if (!SCM_IS_A_P (gf, scm_class_accessor)) |
80662eda | 2657 | { |
1afff620 | 2658 | gf = scm_make (scm_list_3 (scm_class_generic, k_name, name)); |
f8af5c6d | 2659 | gf = scm_make (scm_list_5 (scm_class_accessor, |
1afff620 | 2660 | k_name, name, k_setter, gf)); |
80662eda MD |
2661 | } |
2662 | return gf; | |
2663 | } | |
2664 | ||
2665 | SCM_SYMBOL (sym_internal_add_method_x, "internal-add-method!"); | |
2666 | ||
2667 | void | |
2668 | scm_add_method (SCM gf, SCM m) | |
2669 | { | |
1afff620 | 2670 | scm_eval (scm_list_3 (sym_internal_add_method_x, gf, m), scm_module_goops); |
80662eda MD |
2671 | } |
2672 | ||
2673 | #ifdef GUILE_DEBUG | |
2674 | /* | |
2675 | * Debugging utilities | |
2676 | */ | |
2677 | ||
398d8ee1 KN |
2678 | SCM_DEFINE (scm_pure_generic_p, "pure-generic?", 1, 0, 0, |
2679 | (SCM obj), | |
6bcefd15 | 2680 | "Return @code{#t} if @var{obj} is a pure generic.") |
398d8ee1 | 2681 | #define FUNC_NAME s_scm_pure_generic_p |
80662eda | 2682 | { |
7888309b | 2683 | return scm_from_bool (SCM_PUREGENERICP (obj)); |
80662eda | 2684 | } |
398d8ee1 | 2685 | #undef FUNC_NAME |
80662eda MD |
2686 | |
2687 | #endif /* GUILE_DEBUG */ | |
2688 | ||
2689 | /* | |
2690 | * Initialization | |
2691 | */ | |
2692 | ||
398d8ee1 KN |
2693 | SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0, |
2694 | (), | |
6bcefd15 MG |
2695 | "Announce that GOOPS is loaded and perform initialization\n" |
2696 | "on the C level which depends on the loaded GOOPS modules.") | |
398d8ee1 | 2697 | #define FUNC_NAME s_scm_sys_goops_loaded |
80662eda MD |
2698 | { |
2699 | goops_loaded_p = 1; | |
86d31dfe | 2700 | var_compute_applicable_methods = |
bef95911 AW |
2701 | scm_permanent_object |
2702 | (scm_module_variable (scm_module_goops, sym_compute_applicable_methods)); | |
2703 | var_slot_unbound = | |
2704 | scm_permanent_object | |
2705 | (scm_module_variable (scm_module_goops, sym_slot_unbound)); | |
2706 | var_slot_missing = | |
2707 | scm_permanent_object | |
2708 | (scm_module_variable (scm_module_goops, sym_slot_missing)); | |
2709 | var_compute_cpl = | |
2710 | scm_permanent_object | |
2711 | (scm_module_variable (scm_module_goops, sym_compute_cpl)); | |
2712 | var_no_applicable_method = | |
2713 | scm_permanent_object | |
2714 | (scm_module_variable (scm_module_goops, sym_no_applicable_method)); | |
2715 | var_change_class = | |
2716 | scm_permanent_object | |
2717 | (scm_module_variable (scm_module_goops, sym_change_class)); | |
a48d60b1 | 2718 | setup_extended_primitive_generics (); |
80662eda MD |
2719 | return SCM_UNSPECIFIED; |
2720 | } | |
398d8ee1 | 2721 | #undef FUNC_NAME |
80662eda MD |
2722 | |
2723 | SCM scm_module_goops; | |
2724 | ||
abd28220 MV |
2725 | SCM |
2726 | scm_init_goops_builtins (void) | |
80662eda | 2727 | { |
abd28220 | 2728 | scm_module_goops = scm_current_module (); |
80662eda | 2729 | |
6d77c894 | 2730 | /* Not really necessary right now, but who knows... |
0ba8a0a5 MV |
2731 | */ |
2732 | scm_permanent_object (scm_module_goops); | |
0ba8a0a5 | 2733 | |
80662eda MD |
2734 | goops_rstate = scm_c_make_rstate ("GOOPS", 5); |
2735 | ||
2736 | #include "libguile/goops.x" | |
2737 | ||
1afff620 | 2738 | list_of_no_method = scm_permanent_object (scm_list_1 (sym_no_method)); |
80662eda | 2739 | |
bb764c0e | 2740 | hell = scm_calloc (hell_size * sizeof (*hell)); |
2132f0d2 | 2741 | hell_mutex = scm_permanent_object (scm_make_mutex ()); |
80662eda MD |
2742 | |
2743 | create_basic_classes (); | |
2744 | create_standard_classes (); | |
2745 | create_smob_classes (); | |
2746 | create_struct_classes (); | |
2747 | create_port_classes (); | |
2748 | ||
2749 | { | |
cc95e00a | 2750 | SCM name = scm_from_locale_symbol ("no-applicable-method"); |
80662eda | 2751 | scm_no_applicable_method |
1afff620 KN |
2752 | = scm_permanent_object (scm_make (scm_list_3 (scm_class_generic, |
2753 | k_name, | |
2754 | name))); | |
80662eda MD |
2755 | DEFVAR (name, scm_no_applicable_method); |
2756 | } | |
abd28220 MV |
2757 | |
2758 | return SCM_UNSPECIFIED; | |
80662eda MD |
2759 | } |
2760 | ||
2761 | void | |
abd28220 | 2762 | scm_init_goops () |
80662eda | 2763 | { |
9a441ddb MV |
2764 | scm_c_define_gsubr ("%init-goops-builtins", 0, 0, 0, |
2765 | scm_init_goops_builtins); | |
80662eda | 2766 | } |
23437298 DH |
2767 | |
2768 | /* | |
2769 | Local Variables: | |
2770 | c-file-style: "gnu" | |
2771 | End: | |
2772 | */ |