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