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