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