* objects.c, objects.h (scm_mcache_lookup_cmethod): Moved here
[bpt/guile.git] / libguile / objects.c
1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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 file and objects.h contains those minimal pieces of the Guile
44 * Object Oriented Programming System which need to be included in
45 * libguile. See the comments in objects.h.
46 */
47
48 #include "_scm.h"
49
50 #include "struct.h"
51 #include "procprop.h"
52 #include "chars.h"
53 #include "keywords.h"
54 #include "smob.h"
55 #include "eval.h"
56 #include "alist.h"
57
58 #include "objects.h"
59 \f
60
61 SCM scm_metaclass_standard;
62 SCM scm_metaclass_operator;
63
64 /* These variables are filled in by the object system when loaded. */
65 SCM scm_class_boolean, scm_class_char, scm_class_pair;
66 SCM scm_class_procedure, scm_class_string, scm_class_symbol;
67 SCM scm_class_procedure_with_setter, scm_class_primitive_generic;
68 SCM scm_class_vector, scm_class_null;
69 SCM scm_class_integer, scm_class_real, scm_class_complex;
70 SCM scm_class_unknown;
71
72 SCM *scm_port_class = 0;
73 SCM *scm_smob_class = 0;
74
75 SCM scm_no_applicable_method;
76
77 SCM (*scm_make_extended_class) (char *type_name);
78 void (*scm_make_port_classes) (int ptobnum, char *type_name);
79 void (*scm_change_object_class) (SCM, SCM, SCM);
80
81 /* This function is used for efficient type dispatch. */
82 SCM
83 scm_class_of (SCM x)
84 {
85 switch (SCM_ITAG3 (x))
86 {
87 case scm_tc3_int_1:
88 case scm_tc3_int_2:
89 return scm_class_integer;
90
91 case scm_tc3_imm24:
92 if (SCM_ICHRP (x))
93 return scm_class_char;
94 else
95 {
96 switch (SCM_ISYMNUM (x))
97 {
98 case SCM_ISYMNUM (SCM_BOOL_F):
99 case SCM_ISYMNUM (SCM_BOOL_T):
100 return scm_class_boolean;
101 case SCM_ISYMNUM (SCM_EOL):
102 return scm_class_null;
103 default:
104 return scm_class_unknown;
105 }
106 }
107
108 case scm_tc3_cons:
109 switch (SCM_TYP7 (x))
110 {
111 case scm_tcs_cons_nimcar:
112 return scm_class_pair;
113 case scm_tcs_closures:
114 return scm_class_procedure;
115 case scm_tcs_symbols:
116 return scm_class_symbol;
117 case scm_tc7_vector:
118 case scm_tc7_wvect:
119 case scm_tc7_bvect:
120 case scm_tc7_byvect:
121 case scm_tc7_svect:
122 case scm_tc7_ivect:
123 case scm_tc7_uvect:
124 case scm_tc7_fvect:
125 case scm_tc7_dvect:
126 case scm_tc7_cvect:
127 return scm_class_vector;
128 case scm_tc7_string:
129 case scm_tc7_substring:
130 return scm_class_string;
131 case scm_tc7_asubr:
132 case scm_tc7_subr_0:
133 case scm_tc7_subr_1:
134 case scm_tc7_cxr:
135 case scm_tc7_subr_3:
136 case scm_tc7_subr_2:
137 case scm_tc7_rpsubr:
138 case scm_tc7_subr_1o:
139 case scm_tc7_subr_2o:
140 case scm_tc7_lsubr_2:
141 case scm_tc7_lsubr:
142 if (SCM_SUBR_GENERIC (x) && *SCM_SUBR_GENERIC (x))
143 return scm_class_primitive_generic;
144 else
145 return scm_class_procedure;
146 case scm_tc7_cclo:
147 return scm_class_procedure;
148 case scm_tc7_pws:
149 return scm_class_procedure_with_setter;
150
151 case scm_tc7_smob:
152 {
153 SCM type = SCM_TYP16 (x);
154 if (type == scm_tc16_flo)
155 {
156 if (SCM_CAR (x) & SCM_IMAG_PART)
157 return scm_class_complex;
158 else
159 return scm_class_real;
160 }
161 else if (type != scm_tc16_port_with_ps)
162 return scm_smob_class[SCM_TC2SMOBNUM (type)];
163 x = SCM_PORT_WITH_PS_PORT (x);
164 /* fall through to ports */
165 }
166 case scm_tc7_port:
167 return scm_port_class[(SCM_WRTNG & SCM_CAR (x)
168 ? (SCM_RDNG & SCM_CAR (x)
169 ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
170 : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
171 : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
172 case scm_tcs_cons_gloc:
173 /* must be a struct */
174 if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
175 return SCM_CLASS_OF (x);
176 else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
177 {
178 /* Goops object */
179 if (SCM_OBJ_CLASS_REDEF (x) != SCM_BOOL_F)
180 scm_change_object_class (x,
181 SCM_CLASS_OF (x), /* old */
182 SCM_OBJ_CLASS_REDEF (x)); /* new */
183 return SCM_CLASS_OF (x);
184 }
185 else
186 {
187 /* ordinary struct */
188 SCM handle = scm_struct_create_handle (SCM_STRUCT_VTABLE (x));
189 if (SCM_NFALSEP (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle))))
190 return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle));
191 else
192 {
193 SCM name = SCM_STRUCT_TABLE_NAME (SCM_CDR (handle));
194 SCM class = scm_make_extended_class (SCM_NFALSEP (name)
195 ? SCM_ROCHARS (name)
196 : 0);
197 SCM_SET_STRUCT_TABLE_CLASS (handle, class);
198 return class;
199 }
200 }
201 default:
202 if (SCM_CONSP (x))
203 return scm_class_pair;
204 else
205 return scm_class_unknown;
206 }
207
208 case scm_tc3_cons_gloc:
209 case scm_tc3_tc7_1:
210 case scm_tc3_tc7_2:
211 case scm_tc3_closure:
212 /* Never reached */
213 break;
214 }
215 return scm_class_unknown;
216 }
217
218 /* (SCM_IM_DISPATCH ARGS N-SPECIALIZED
219 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
220 * GF)
221 *
222 * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK
223 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
224 * GF)
225 *
226 * ARGS is either a list of expressions, in which case they
227 * are interpreted as the arguments of an application, or
228 * a non-pair, which is interpreted as a single expression
229 * yielding all arguments.
230 *
231 * SCM_IM_DISPATCH expressions in generic functions always
232 * have ARGS = the symbol `args' or the iloc #@0-0.
233 *
234 * Need FORMALS in order to support varying arity. This
235 * also avoids the need for renaming of bindings.
236 *
237 * We should probably not complicate this mechanism by
238 * introducing "optimizations" for getters and setters or
239 * primitive methods. Getters and setter will normally be
240 * compiled into @slot-[ref|set!] or a procedure call.
241 * They rely on the dispatch performed before executing
242 * the code which contains them.
243 *
244 * We might want to use a more efficient representation of
245 * this form in the future, perhaps after we have introduced
246 * low-level support for syntax-case macros.
247 */
248
249 SCM
250 scm_mcache_lookup_cmethod (SCM cache, SCM args)
251 {
252 int i, n, end, mask;
253 SCM ls, methods, z = SCM_CDDR (cache);
254 n = SCM_INUM (SCM_CAR (z)); /* maximum number of specializers */
255 methods = SCM_CADR (z);
256
257 if (SCM_NIMP (methods))
258 {
259 /* Prepare for linear search */
260 mask = -1;
261 i = 0;
262 end = SCM_LENGTH (methods);
263 }
264 else
265 {
266 /* Compute a hash value */
267 int hashset = SCM_INUM (methods);
268 int j = n;
269 mask = SCM_INUM (SCM_CAR (z = SCM_CDDR (z)));
270 methods = SCM_CADR (z);
271 i = 0;
272 ls = args;
273 if (SCM_NIMP (ls))
274 do
275 {
276 i += (SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls)))
277 [scm_si_hashsets + hashset]);
278 ls = SCM_CDR (ls);
279 }
280 while (--j && SCM_NIMP (ls));
281 i &= mask;
282 end = i;
283 }
284
285 /* Search for match */
286 do
287 {
288 int j = n;
289 z = SCM_VELTS (methods)[i];
290 ls = args; /* list of arguments */
291 if (SCM_NIMP (ls))
292 do
293 {
294 /* More arguments than specifiers => CLASS != ENV */
295 if (scm_class_of (SCM_CAR (ls)) != SCM_CAR (z))
296 goto next_method;
297 ls = SCM_CDR (ls);
298 z = SCM_CDR (z);
299 }
300 while (--j && SCM_NIMP (ls));
301 /* Fewer arguments than specifiers => CAR != ENV */
302 if (!SCM_CONSP (SCM_CAR (z)))
303 goto next_method;
304 return z;
305 next_method:
306 i = (i + 1) & mask;
307 } while (i != end);
308 return SCM_BOOL_F;
309 }
310
311 SCM (*scm_memoize_method) (SCM, SCM);
312
313 SCM
314 scm_mcache_compute_cmethod (SCM cache, SCM args)
315 {
316 SCM cmethod = scm_mcache_lookup_cmethod (cache, args);
317 if (SCM_IMP (cmethod))
318 /* No match - memoize */
319 return scm_memoize_method (cache, args);
320 return cmethod;
321 }
322
323 SCM
324 scm_apply_generic (SCM gf, SCM args)
325 {
326 SCM cmethod = scm_mcache_compute_cmethod (SCM_ENTITY_PROCEDURE (gf), args);
327 return scm_eval_body (SCM_CDR (SCM_CMETHOD_CODE (cmethod)),
328 SCM_EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (cmethod)),
329 args,
330 SCM_CMETHOD_ENV (cmethod)));
331 }
332
333 SCM
334 scm_call_generic_0 (SCM gf)
335 {
336 return scm_apply_generic (gf, SCM_EOL);
337 }
338
339 SCM
340 scm_call_generic_1 (SCM gf, SCM a1)
341 {
342 return scm_apply_generic (gf, SCM_LIST1 (a1));
343 }
344
345 SCM
346 scm_call_generic_2 (SCM gf, SCM a1, SCM a2)
347 {
348 return scm_apply_generic (gf, SCM_LIST2 (a1, a2));
349 }
350
351 SCM
352 scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3)
353 {
354 return scm_apply_generic (gf, SCM_LIST3 (a1, a2, a3));
355 }
356
357 SCM_PROC (s_entity_p, "entity?", 1, 0, 0, scm_entity_p);
358
359 SCM
360 scm_entity_p (SCM obj)
361 {
362 return (SCM_NIMP (obj) && SCM_STRUCTP (obj) && SCM_I_ENTITYP (obj)
363 ? SCM_BOOL_T
364 : SCM_BOOL_F);
365 }
366
367 SCM_PROC (s_operator_p, "operator?", 1, 0, 0, scm_operator_p);
368
369 SCM
370 scm_operator_p (SCM obj)
371 {
372 return (SCM_NIMP (obj)
373 && SCM_STRUCTP (obj)
374 && SCM_I_OPERATORP (obj)
375 && !SCM_I_ENTITYP (obj)
376 ? SCM_BOOL_T
377 : SCM_BOOL_F);
378 }
379
380 SCM_PROC (s_set_object_procedure_x, "set-object-procedure!", 2, 0, 0, scm_set_object_procedure_x);
381
382 SCM
383 scm_set_object_procedure_x (SCM obj, SCM proc)
384 {
385 SCM_ASSERT (SCM_NIMP (obj) && SCM_STRUCTP (obj)
386 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
387 || (SCM_I_ENTITYP (obj)
388 && !(SCM_OBJ_CLASS_FLAGS (obj)
389 & SCM_CLASSF_PURE_GENERIC))),
390 obj,
391 SCM_ARG1,
392 s_set_object_procedure_x);
393 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (proc)),
394 proc, SCM_ARG2, s_set_object_procedure_x);
395 if (SCM_I_ENTITYP (obj))
396 SCM_ENTITY_PROCEDURE (obj) = proc;
397 else
398 SCM_OPERATOR_CLASS (obj)->procedure = proc;
399 return SCM_UNSPECIFIED;
400 }
401
402 #ifdef GUILE_DEBUG
403 SCM_PROC (s_object_procedure, "object-procedure", 1, 0, 0, scm_object_procedure);
404
405 SCM
406 scm_object_procedure (SCM obj)
407 {
408 SCM_ASSERT (SCM_NIMP (obj) && SCM_STRUCTP (obj)
409 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
410 || SCM_I_ENTITYP (obj)),
411 obj, SCM_ARG1, s_object_procedure);
412 return (SCM_I_ENTITYP (obj)
413 ? SCM_ENTITY_PROCEDURE (obj)
414 : SCM_OPERATOR_CLASS (obj)->procedure);
415 }
416 #endif /* GUILE_DEBUG */
417
418 /* The following procedures are not a part of Goops but a minimal
419 * object system built upon structs. They are here for those who
420 * want to implement their own object system.
421 */
422
423 SCM
424 scm_i_make_class_object (SCM meta,
425 SCM layout_string,
426 unsigned long flags)
427 {
428 SCM c;
429 SCM layout = scm_make_struct_layout (layout_string);
430 c = scm_make_struct (meta,
431 SCM_INUM0,
432 SCM_LIST4 (layout, SCM_BOOL_F, SCM_EOL, SCM_EOL));
433 SCM_SET_CLASS_FLAGS (c, flags);
434 return c;
435 }
436
437 SCM_PROC (s_make_class_object, "make-class-object", 2, 0, 0, scm_make_class_object);
438
439 SCM
440 scm_make_class_object (SCM metaclass, SCM layout)
441 {
442 unsigned long flags = 0;
443 SCM_ASSERT (SCM_NIMP (metaclass) && SCM_STRUCTP (metaclass),
444 metaclass, SCM_ARG1, s_make_class_object);
445 SCM_ASSERT (SCM_NIMP (layout) && SCM_STRINGP (layout),
446 layout, SCM_ARG2, s_make_class_object);
447 if (metaclass == scm_metaclass_operator)
448 flags = SCM_CLASSF_OPERATOR;
449 return scm_i_make_class_object (metaclass, layout, flags);
450 }
451
452 SCM_PROC (s_make_subclass_object, "make-subclass-object", 2, 0, 0, scm_make_subclass_object);
453
454 SCM
455 scm_make_subclass_object (SCM class, SCM layout)
456 {
457 SCM pl;
458 SCM_ASSERT (SCM_NIMP (class) && SCM_STRUCTP (class),
459 class,
460 SCM_ARG1,
461 s_make_subclass_object);
462 SCM_ASSERT (SCM_NIMP (layout) && SCM_STRINGP (layout),
463 layout,
464 SCM_ARG2,
465 s_make_subclass_object);
466 pl = SCM_STRUCT_DATA (class)[scm_vtable_index_layout];
467 /* Convert symbol->string */
468 pl = scm_makfromstr (SCM_CHARS (pl), (scm_sizet) SCM_LENGTH (pl), 0);
469 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
470 scm_string_append (SCM_LIST2 (pl, layout)),
471 SCM_CLASS_FLAGS (class));
472 }
473
474 void
475 scm_init_objects ()
476 {
477 SCM ms = scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT);
478 SCM ml = scm_make_struct_layout (ms);
479 SCM mt = scm_make_vtable_vtable (ml, SCM_INUM0,
480 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
481
482 SCM os = scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT);
483 SCM ol = scm_make_struct_layout (os);
484 SCM ot = scm_make_vtable_vtable (ol, SCM_INUM0,
485 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
486
487 SCM es = scm_makfrom0str (SCM_ENTITY_LAYOUT);
488 SCM el = scm_make_struct_layout (es);
489 SCM et = scm_make_struct (mt, SCM_INUM0,
490 SCM_LIST4 (el, SCM_BOOL_F, SCM_EOL, SCM_EOL));
491
492 scm_sysintern ("<class>", mt);
493 scm_metaclass_standard = mt;
494 scm_sysintern ("<operator-class>", ot);
495 scm_metaclass_operator = ot;
496 SCM_SET_CLASS_FLAGS (et, SCM_CLASSF_OPERATOR | SCM_CLASSF_ENTITY);
497 SCM_SET_CLASS_DESTRUCTOR (et, scm_struct_free_entity);
498 scm_sysintern ("<entity>", et);
499
500 #include "objects.x"
501 }