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