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