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