Commit | Line | Data |
---|---|---|
cc95e00a | 1 | /* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc. |
1d9ee7c7 | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
1d9ee7c7 | 7 | * |
73be1d9e MV |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
1d9ee7c7 | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
1d9ee7c7 MD |
19 | \f |
20 | ||
da7f71d7 MD |
21 | /* This file and objects.h contains those minimal pieces of the Guile |
22 | * Object Oriented Programming System which need to be included in | |
23 | * libguile. See the comments in objects.h. | |
1d9ee7c7 MD |
24 | */ |
25 | ||
a0599745 MD |
26 | #include "libguile/_scm.h" |
27 | ||
28 | #include "libguile/struct.h" | |
29 | #include "libguile/procprop.h" | |
30 | #include "libguile/chars.h" | |
31 | #include "libguile/keywords.h" | |
32 | #include "libguile/smob.h" | |
33 | #include "libguile/eval.h" | |
34 | #include "libguile/alist.h" | |
35 | #include "libguile/ports.h" | |
36 | #include "libguile/strings.h" | |
37 | #include "libguile/vectors.h" | |
38 | ||
39 | #include "libguile/validate.h" | |
40 | #include "libguile/objects.h" | |
1d9ee7c7 MD |
41 | \f |
42 | ||
43 | SCM scm_metaclass_standard; | |
da7f71d7 | 44 | SCM scm_metaclass_operator; |
1d9ee7c7 | 45 | |
5843e5c9 DH |
46 | /* The cache argument for scm_mcache_lookup_cmethod has one of two possible |
47 | * formats: | |
48 | * | |
49 | * Format #1: | |
50 | * (SCM_IM_DISPATCH ARGS N-SPECIALIZED | |
a12be546 MD |
51 | * #((TYPE1 ... ENV FORMALS FORM ...) ...) |
52 | * GF) | |
53 | * | |
5843e5c9 | 54 | * Format #2: |
a12be546 MD |
55 | * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK |
56 | * #((TYPE1 ... ENV FORMALS FORM ...) ...) | |
57 | * GF) | |
58 | * | |
59 | * ARGS is either a list of expressions, in which case they | |
60 | * are interpreted as the arguments of an application, or | |
61 | * a non-pair, which is interpreted as a single expression | |
62 | * yielding all arguments. | |
63 | * | |
64 | * SCM_IM_DISPATCH expressions in generic functions always | |
65 | * have ARGS = the symbol `args' or the iloc #@0-0. | |
66 | * | |
67 | * Need FORMALS in order to support varying arity. This | |
68 | * also avoids the need for renaming of bindings. | |
69 | * | |
70 | * We should probably not complicate this mechanism by | |
71 | * introducing "optimizations" for getters and setters or | |
72 | * primitive methods. Getters and setter will normally be | |
73 | * compiled into @slot-[ref|set!] or a procedure call. | |
74 | * They rely on the dispatch performed before executing | |
75 | * the code which contains them. | |
76 | * | |
77 | * We might want to use a more efficient representation of | |
78 | * this form in the future, perhaps after we have introduced | |
79 | * low-level support for syntax-case macros. | |
80 | */ | |
81 | ||
9de33deb MD |
82 | SCM |
83 | scm_mcache_lookup_cmethod (SCM cache, SCM args) | |
84 | { | |
e11e83f3 | 85 | unsigned long i, mask, n, end; |
9de33deb | 86 | SCM ls, methods, z = SCM_CDDR (cache); |
e11e83f3 | 87 | n = scm_to_ulong (SCM_CAR (z)); /* maximum number of specializers */ |
9de33deb MD |
88 | methods = SCM_CADR (z); |
89 | ||
4057a3e0 | 90 | if (scm_is_simple_vector (methods)) |
e11e83f3 MV |
91 | { |
92 | /* cache format #1: prepare for linear search */ | |
93 | mask = -1; | |
94 | i = 0; | |
4057a3e0 | 95 | end = SCM_SIMPLE_VECTOR_LENGTH (methods); |
e11e83f3 MV |
96 | } |
97 | else | |
9de33deb | 98 | { |
5843e5c9 | 99 | /* cache format #2: compute a hash value */ |
e11e83f3 | 100 | unsigned long hashset = scm_to_ulong (methods); |
c014a02e | 101 | long j = n; |
bab246f3 | 102 | z = SCM_CDDR (z); |
e11e83f3 | 103 | mask = scm_to_ulong (SCM_CAR (z)); |
9de33deb MD |
104 | methods = SCM_CADR (z); |
105 | i = 0; | |
106 | ls = args; | |
d2e53ed6 | 107 | if (!scm_is_null (ls)) |
a12be546 MD |
108 | do |
109 | { | |
d8c40b9f DH |
110 | i += SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls))) |
111 | [scm_si_hashsets + hashset]; | |
a12be546 MD |
112 | ls = SCM_CDR (ls); |
113 | } | |
d2e53ed6 | 114 | while (j-- && !scm_is_null (ls)); |
9de33deb MD |
115 | i &= mask; |
116 | end = i; | |
117 | } | |
118 | ||
119 | /* Search for match */ | |
120 | do | |
121 | { | |
c014a02e | 122 | long j = n; |
4057a3e0 | 123 | z = SCM_SIMPLE_VECTOR_REF (methods, i); |
9de33deb | 124 | ls = args; /* list of arguments */ |
d2e53ed6 | 125 | if (!scm_is_null (ls)) |
a12be546 MD |
126 | do |
127 | { | |
128 | /* More arguments than specifiers => CLASS != ENV */ | |
bc36d050 | 129 | if (! scm_is_eq (scm_class_of (SCM_CAR (ls)), SCM_CAR (z))) |
a12be546 MD |
130 | goto next_method; |
131 | ls = SCM_CDR (ls); | |
132 | z = SCM_CDR (z); | |
133 | } | |
d2e53ed6 | 134 | while (j-- && !scm_is_null (ls)); |
9de33deb | 135 | /* Fewer arguments than specifiers => CAR != ENV */ |
d2e53ed6 | 136 | if (scm_is_null (SCM_CAR (z)) || scm_is_pair (SCM_CAR (z))) |
5843e5c9 | 137 | return z; |
9de33deb MD |
138 | next_method: |
139 | i = (i + 1) & mask; | |
140 | } while (i != end); | |
141 | return SCM_BOOL_F; | |
142 | } | |
143 | ||
144 | SCM | |
a12be546 | 145 | scm_mcache_compute_cmethod (SCM cache, SCM args) |
9de33deb MD |
146 | { |
147 | SCM cmethod = scm_mcache_lookup_cmethod (cache, args); | |
7888309b | 148 | if (scm_is_false (cmethod)) |
9de33deb MD |
149 | /* No match - memoize */ |
150 | return scm_memoize_method (cache, args); | |
151 | return cmethod; | |
152 | } | |
153 | ||
154 | SCM | |
a12be546 | 155 | scm_apply_generic (SCM gf, SCM args) |
9de33deb | 156 | { |
a12be546 | 157 | SCM cmethod = scm_mcache_compute_cmethod (SCM_ENTITY_PROCEDURE (gf), args); |
9de33deb MD |
158 | return scm_eval_body (SCM_CDR (SCM_CMETHOD_CODE (cmethod)), |
159 | SCM_EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (cmethod)), | |
160 | args, | |
161 | SCM_CMETHOD_ENV (cmethod))); | |
162 | } | |
163 | ||
164 | SCM | |
a12be546 | 165 | scm_call_generic_0 (SCM gf) |
9de33deb | 166 | { |
a12be546 | 167 | return scm_apply_generic (gf, SCM_EOL); |
9de33deb MD |
168 | } |
169 | ||
170 | SCM | |
a12be546 | 171 | scm_call_generic_1 (SCM gf, SCM a1) |
9de33deb | 172 | { |
1afff620 | 173 | return scm_apply_generic (gf, scm_list_1 (a1)); |
9de33deb MD |
174 | } |
175 | ||
176 | SCM | |
a12be546 MD |
177 | scm_call_generic_2 (SCM gf, SCM a1, SCM a2) |
178 | { | |
1afff620 | 179 | return scm_apply_generic (gf, scm_list_2 (a1, a2)); |
a12be546 MD |
180 | } |
181 | ||
182 | SCM | |
183 | scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3) | |
9de33deb | 184 | { |
1afff620 | 185 | return scm_apply_generic (gf, scm_list_3 (a1, a2, a3)); |
9de33deb MD |
186 | } |
187 | ||
a1ec6916 | 188 | SCM_DEFINE (scm_entity_p, "entity?", 1, 0, 0, |
1bbd0b84 | 189 | (SCM obj), |
99ca0a7f | 190 | "Return @code{#t} if @var{obj} is an entity.") |
1bbd0b84 | 191 | #define FUNC_NAME s_scm_entity_p |
19c0dec2 | 192 | { |
7888309b | 193 | return scm_from_bool(SCM_STRUCTP (obj) && SCM_I_ENTITYP (obj)); |
19c0dec2 | 194 | } |
1bbd0b84 | 195 | #undef FUNC_NAME |
19c0dec2 | 196 | |
a1ec6916 | 197 | SCM_DEFINE (scm_operator_p, "operator?", 1, 0, 0, |
1bbd0b84 | 198 | (SCM obj), |
99ca0a7f | 199 | "Return @code{#t} if @var{obj} is an operator.") |
1bbd0b84 | 200 | #define FUNC_NAME s_scm_operator_p |
a43a8375 | 201 | { |
7888309b | 202 | return scm_from_bool(SCM_STRUCTP (obj) |
1bbd0b84 GB |
203 | && SCM_I_OPERATORP (obj) |
204 | && !SCM_I_ENTITYP (obj)); | |
a43a8375 | 205 | } |
1bbd0b84 | 206 | #undef FUNC_NAME |
a43a8375 | 207 | |
cec0d28c MV |
208 | /* XXX - What code requires the object procedure to be only of certain |
209 | types? */ | |
210 | ||
211 | SCM_DEFINE (scm_valid_object_procedure_p, "valid-object-procedure?", 1, 0, 0, | |
212 | (SCM proc), | |
213 | "Return @code{#t} iff @var{proc} is a procedure that can be used " | |
214 | "with @code{set-object-procedure}. It is always valid to use " | |
215 | "a closure constructed by @code{lambda}.") | |
216 | #define FUNC_NAME s_scm_valid_object_procedure_p | |
217 | { | |
218 | if (SCM_IMP (proc)) | |
219 | return SCM_BOOL_F; | |
220 | switch (SCM_TYP7 (proc)) | |
221 | { | |
222 | default: | |
223 | return SCM_BOOL_F; | |
224 | case scm_tcs_closures: | |
225 | case scm_tc7_subr_1: | |
226 | case scm_tc7_subr_2: | |
227 | case scm_tc7_subr_3: | |
228 | case scm_tc7_lsubr_2: | |
229 | return SCM_BOOL_T; | |
230 | } | |
231 | } | |
232 | #undef FUNC_NAME | |
233 | ||
a1ec6916 | 234 | SCM_DEFINE (scm_set_object_procedure_x, "set-object-procedure!", 2, 0, 0, |
1bbd0b84 | 235 | (SCM obj, SCM proc), |
cec0d28c | 236 | "Set the object procedure of @var{obj} to @var{proc}.\n" |
99ca0a7f | 237 | "@var{obj} must be either an entity or an operator.") |
1bbd0b84 | 238 | #define FUNC_NAME s_scm_set_object_procedure_x |
47b21050 | 239 | { |
0c95b57d | 240 | SCM_ASSERT (SCM_STRUCTP (obj) |
c1a6fd8f MD |
241 | && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR) |
242 | || (SCM_I_ENTITYP (obj) | |
243 | && !(SCM_OBJ_CLASS_FLAGS (obj) | |
244 | & SCM_CLASSF_PURE_GENERIC))), | |
47b21050 MD |
245 | obj, |
246 | SCM_ARG1, | |
1bbd0b84 | 247 | FUNC_NAME); |
cec0d28c | 248 | SCM_ASSERT (scm_valid_object_procedure_p (proc), proc, SCM_ARG2, FUNC_NAME); |
a12be546 | 249 | if (SCM_I_ENTITYP (obj)) |
d8c40b9f | 250 | SCM_SET_ENTITY_PROCEDURE (obj, proc); |
a12be546 MD |
251 | else |
252 | SCM_OPERATOR_CLASS (obj)->procedure = proc; | |
47b21050 MD |
253 | return SCM_UNSPECIFIED; |
254 | } | |
1bbd0b84 | 255 | #undef FUNC_NAME |
47b21050 | 256 | |
a43a8375 | 257 | #ifdef GUILE_DEBUG |
a1ec6916 | 258 | SCM_DEFINE (scm_object_procedure, "object-procedure", 1, 0, 0, |
1bbd0b84 | 259 | (SCM obj), |
99ca0a7f MG |
260 | "Return the object procedure of @var{obj}. @var{obj} must be\n" |
261 | "an entity or an operator.") | |
1bbd0b84 | 262 | #define FUNC_NAME s_scm_object_procedure |
a43a8375 | 263 | { |
0c95b57d | 264 | SCM_ASSERT (SCM_STRUCTP (obj) |
a12be546 MD |
265 | && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR) |
266 | || SCM_I_ENTITYP (obj)), | |
1bbd0b84 | 267 | obj, SCM_ARG1, FUNC_NAME); |
a43a8375 | 268 | return (SCM_I_ENTITYP (obj) |
a12be546 MD |
269 | ? SCM_ENTITY_PROCEDURE (obj) |
270 | : SCM_OPERATOR_CLASS (obj)->procedure); | |
a43a8375 | 271 | } |
1bbd0b84 | 272 | #undef FUNC_NAME |
a43a8375 MD |
273 | #endif /* GUILE_DEBUG */ |
274 | ||
ed6e0c83 MD |
275 | /* The following procedures are not a part of Goops but a minimal |
276 | * object system built upon structs. They are here for those who | |
277 | * want to implement their own object system. | |
278 | */ | |
279 | ||
036737fc MD |
280 | SCM |
281 | scm_i_make_class_object (SCM meta, | |
282 | SCM layout_string, | |
c014a02e | 283 | unsigned long flags) |
47b21050 MD |
284 | { |
285 | SCM c; | |
036737fc | 286 | SCM layout = scm_make_struct_layout (layout_string); |
47b21050 MD |
287 | c = scm_make_struct (meta, |
288 | SCM_INUM0, | |
1afff620 | 289 | scm_list_4 (layout, SCM_BOOL_F, SCM_EOL, SCM_EOL)); |
47b21050 MD |
290 | SCM_SET_CLASS_FLAGS (c, flags); |
291 | return c; | |
292 | } | |
293 | ||
a1ec6916 | 294 | SCM_DEFINE (scm_make_class_object, "make-class-object", 2, 0, 0, |
1bbd0b84 | 295 | (SCM metaclass, SCM layout), |
99ca0a7f MG |
296 | "Create a new class object of class @var{metaclass}, with the\n" |
297 | "slot layout specified by @var{layout}.") | |
1bbd0b84 | 298 | #define FUNC_NAME s_scm_make_class_object |
47b21050 | 299 | { |
c014a02e | 300 | unsigned long flags = 0; |
34d19ef6 HWN |
301 | SCM_VALIDATE_STRUCT (1, metaclass); |
302 | SCM_VALIDATE_STRING (2, layout); | |
bc36d050 | 303 | if (scm_is_eq (metaclass, scm_metaclass_operator)) |
47b21050 | 304 | flags = SCM_CLASSF_OPERATOR; |
036737fc | 305 | return scm_i_make_class_object (metaclass, layout, flags); |
47b21050 | 306 | } |
1bbd0b84 | 307 | #undef FUNC_NAME |
47b21050 | 308 | |
a1ec6916 | 309 | SCM_DEFINE (scm_make_subclass_object, "make-subclass-object", 2, 0, 0, |
1bbd0b84 | 310 | (SCM class, SCM layout), |
99ca0a7f MG |
311 | "Create a subclass object of @var{class}, with the slot layout\n" |
312 | "specified by @var{layout}.") | |
1bbd0b84 | 313 | #define FUNC_NAME s_scm_make_subclass_object |
47b21050 MD |
314 | { |
315 | SCM pl; | |
34d19ef6 HWN |
316 | SCM_VALIDATE_STRUCT (1, class); |
317 | SCM_VALIDATE_STRING (2, layout); | |
d8c40b9f | 318 | pl = SCM_PACK (SCM_STRUCT_DATA (class) [scm_vtable_index_layout]); |
cc95e00a | 319 | pl = scm_symbol_to_string (pl); |
036737fc | 320 | return scm_i_make_class_object (SCM_STRUCT_VTABLE (class), |
1afff620 | 321 | scm_string_append (scm_list_2 (pl, layout)), |
036737fc | 322 | SCM_CLASS_FLAGS (class)); |
47b21050 | 323 | } |
1bbd0b84 | 324 | #undef FUNC_NAME |
47b21050 | 325 | |
1d9ee7c7 MD |
326 | void |
327 | scm_init_objects () | |
328 | { | |
cc95e00a | 329 | SCM ms = scm_from_locale_string (SCM_METACLASS_STANDARD_LAYOUT); |
3ce4544c | 330 | SCM mt = scm_make_vtable_vtable (ms, SCM_INUM0, |
1afff620 | 331 | scm_list_3 (SCM_BOOL_F, SCM_EOL, SCM_EOL)); |
1d9ee7c7 | 332 | |
cc95e00a | 333 | SCM os = scm_from_locale_string (SCM_METACLASS_OPERATOR_LAYOUT); |
3ce4544c | 334 | SCM ot = scm_make_vtable_vtable (os, SCM_INUM0, |
1afff620 | 335 | scm_list_3 (SCM_BOOL_F, SCM_EOL, SCM_EOL)); |
da7f71d7 | 336 | |
cc95e00a | 337 | SCM es = scm_from_locale_string (SCM_ENTITY_LAYOUT); |
1d9ee7c7 MD |
338 | SCM el = scm_make_struct_layout (es); |
339 | SCM et = scm_make_struct (mt, SCM_INUM0, | |
1afff620 | 340 | scm_list_4 (el, SCM_BOOL_F, SCM_EOL, SCM_EOL)); |
1d9ee7c7 | 341 | |
86d31dfe | 342 | scm_c_define ("<class>", mt); |
1d9ee7c7 | 343 | scm_metaclass_standard = mt; |
86d31dfe | 344 | scm_c_define ("<operator-class>", ot); |
da7f71d7 MD |
345 | scm_metaclass_operator = ot; |
346 | SCM_SET_CLASS_FLAGS (et, SCM_CLASSF_OPERATOR | SCM_CLASSF_ENTITY); | |
135e76f8 | 347 | SCM_SET_CLASS_DESTRUCTOR (et, scm_struct_free_entity); |
86d31dfe | 348 | scm_c_define ("<entity>", et); |
47b21050 | 349 | |
a0599745 | 350 | #include "libguile/objects.x" |
1d9ee7c7 | 351 | } |
89e00824 ML |
352 | |
353 | /* | |
354 | Local Variables: | |
355 | c-file-style: "gnu" | |
356 | End: | |
357 | */ |