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