* alist.c, arbiters.c, async.c, backtrace.c, boolean.c, chars.c,
[bpt/guile.git] / libguile / objects.c
CommitLineData
950cc72b 1/* Copyright (C) 1995, 1996, 1999, 2000 Free Software Foundation, Inc.
1d9ee7c7
MD
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. */
1bbd0b84
GB
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
1d9ee7c7
MD
45\f
46
da7f71d7
MD
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.
1d9ee7c7
MD
50 */
51
a0599745
MD
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"
1d9ee7c7
MD
67\f
68
69SCM scm_metaclass_standard;
da7f71d7 70SCM scm_metaclass_operator;
1d9ee7c7 71
ab7288bc
MD
72/* These variables are filled in by the object system when loaded. */
73SCM scm_class_boolean, scm_class_char, scm_class_pair;
74SCM scm_class_procedure, scm_class_string, scm_class_symbol;
9de33deb 75SCM scm_class_procedure_with_setter, scm_class_primitive_generic;
ab7288bc 76SCM scm_class_vector, scm_class_null;
ed6e0c83
MD
77SCM scm_class_integer, scm_class_real, scm_class_complex;
78SCM scm_class_unknown;
ab7288bc 79
d0efbe61 80SCM *scm_port_class = 0;
ed6e0c83
MD
81SCM *scm_smob_class = 0;
82
9de33deb
MD
83SCM scm_no_applicable_method;
84
ab7288bc
MD
85/* This function is used for efficient type dispatch. */
86SCM
87scm_class_of (SCM x)
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:
7866a09b 96 if (SCM_CHARP (x))
ab7288bc
MD
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;
28b06554 119 case scm_tc7_symbol:
ab7288bc
MD
120 return scm_class_symbol;
121 case scm_tc7_vector:
122 case scm_tc7_wvect:
afe5177e 123#ifdef HAVE_ARRAYS
ab7288bc
MD
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:
afe5177e 132#endif
ab7288bc
MD
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:
9de33deb
MD
148 if (SCM_SUBR_GENERIC (x) && *SCM_SUBR_GENERIC (x))
149 return scm_class_primitive_generic;
150 else
151 return scm_class_procedure;
ed6e0c83 152 case scm_tc7_cclo:
ab7288bc
MD
153 return scm_class_procedure;
154 case scm_tc7_pws:
155 return scm_class_procedure_with_setter;
156
ab7288bc
MD
157 case scm_tc7_smob:
158 {
c209c88e 159 long type = SCM_TYP16 (x);
950cc72b 160 if (type != scm_tc16_port_with_ps)
ed6e0c83 161 return scm_smob_class[SCM_TC2SMOBNUM (type)];
55d54750
MD
162 x = SCM_PORT_WITH_PS_PORT (x);
163 /* fall through to ports */
ab7288bc 164 }
55d54750 165 case scm_tc7_port:
206d3de3
DH
166 return scm_port_class[(SCM_WRTNG & SCM_CELL_WORD_0 (x)
167 ? (SCM_RDNG & SCM_CELL_WORD_0 (x)
55d54750
MD
168 ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
169 : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
170 : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
ab7288bc
MD
171 case scm_tcs_cons_gloc:
172 /* must be a struct */
7151229d
MD
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)
ed6e0c83
MD
176 {
177 /* Goops object */
54778cd3 178 if (! SCM_FALSEP (SCM_OBJ_CLASS_REDEF (x)))
ed6e0c83
MD
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_NFALSEP (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_NFALSEP (name)
b24b5e13 194 ? SCM_SYMBOL_CHARS (name)
ed6e0c83 195 : 0);
11f2a643 196 SCM_SET_STRUCT_TABLE_CLASS (SCM_CDR (handle), class);
ed6e0c83
MD
197 return class;
198 }
199 }
ab7288bc
MD
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
a12be546
MD
217/* (SCM_IM_DISPATCH ARGS N-SPECIALIZED
218 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
219 * GF)
220 *
221 * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK
222 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
223 * GF)
224 *
225 * ARGS is either a list of expressions, in which case they
226 * are interpreted as the arguments of an application, or
227 * a non-pair, which is interpreted as a single expression
228 * yielding all arguments.
229 *
230 * SCM_IM_DISPATCH expressions in generic functions always
231 * have ARGS = the symbol `args' or the iloc #@0-0.
232 *
233 * Need FORMALS in order to support varying arity. This
234 * also avoids the need for renaming of bindings.
235 *
236 * We should probably not complicate this mechanism by
237 * introducing "optimizations" for getters and setters or
238 * primitive methods. Getters and setter will normally be
239 * compiled into @slot-[ref|set!] or a procedure call.
240 * They rely on the dispatch performed before executing
241 * the code which contains them.
242 *
243 * We might want to use a more efficient representation of
244 * this form in the future, perhaps after we have introduced
245 * low-level support for syntax-case macros.
246 */
247
9de33deb
MD
248SCM
249scm_mcache_lookup_cmethod (SCM cache, SCM args)
250{
251 int i, n, end, mask;
252 SCM ls, methods, z = SCM_CDDR (cache);
253 n = SCM_INUM (SCM_CAR (z)); /* maximum number of specializers */
254 methods = SCM_CADR (z);
255
256 if (SCM_NIMP (methods))
257 {
258 /* Prepare for linear search */
259 mask = -1;
260 i = 0;
bfa974f0 261 end = SCM_VECTOR_LENGTH (methods);
9de33deb
MD
262 }
263 else
264 {
265 /* Compute a hash value */
266 int hashset = SCM_INUM (methods);
267 int j = n;
268 mask = SCM_INUM (SCM_CAR (z = SCM_CDDR (z)));
269 methods = SCM_CADR (z);
270 i = 0;
271 ls = args;
a12be546
MD
272 if (SCM_NIMP (ls))
273 do
274 {
d8c40b9f
DH
275 i += SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls)))
276 [scm_si_hashsets + hashset];
a12be546
MD
277 ls = SCM_CDR (ls);
278 }
4ea6a431 279 while (j-- && SCM_NIMP (ls));
9de33deb
MD
280 i &= mask;
281 end = i;
282 }
283
284 /* Search for match */
285 do
286 {
287 int j = n;
288 z = SCM_VELTS (methods)[i];
289 ls = args; /* list of arguments */
a12be546
MD
290 if (SCM_NIMP (ls))
291 do
292 {
293 /* More arguments than specifiers => CLASS != ENV */
54778cd3 294 if (! SCM_EQ_P (scm_class_of (SCM_CAR (ls)), SCM_CAR (z)))
a12be546
MD
295 goto next_method;
296 ls = SCM_CDR (ls);
297 z = SCM_CDR (z);
298 }
4ea6a431 299 while (j-- && SCM_NIMP (ls));
9de33deb 300 /* Fewer arguments than specifiers => CAR != ENV */
0824bbb3 301 if (!(SCM_IMP (SCM_CAR (z)) || SCM_CONSP (SCM_CAR (z))))
9de33deb
MD
302 goto next_method;
303 return z;
304 next_method:
305 i = (i + 1) & mask;
306 } while (i != end);
307 return SCM_BOOL_F;
308}
309
310SCM
a12be546 311scm_mcache_compute_cmethod (SCM cache, SCM args)
9de33deb
MD
312{
313 SCM cmethod = scm_mcache_lookup_cmethod (cache, args);
314 if (SCM_IMP (cmethod))
315 /* No match - memoize */
316 return scm_memoize_method (cache, args);
317 return cmethod;
318}
319
320SCM
a12be546 321scm_apply_generic (SCM gf, SCM args)
9de33deb 322{
a12be546 323 SCM cmethod = scm_mcache_compute_cmethod (SCM_ENTITY_PROCEDURE (gf), args);
9de33deb
MD
324 return scm_eval_body (SCM_CDR (SCM_CMETHOD_CODE (cmethod)),
325 SCM_EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (cmethod)),
326 args,
327 SCM_CMETHOD_ENV (cmethod)));
328}
329
330SCM
a12be546 331scm_call_generic_0 (SCM gf)
9de33deb 332{
a12be546 333 return scm_apply_generic (gf, SCM_EOL);
9de33deb
MD
334}
335
336SCM
a12be546 337scm_call_generic_1 (SCM gf, SCM a1)
9de33deb 338{
a12be546 339 return scm_apply_generic (gf, SCM_LIST1 (a1));
9de33deb
MD
340}
341
342SCM
a12be546
MD
343scm_call_generic_2 (SCM gf, SCM a1, SCM a2)
344{
345 return scm_apply_generic (gf, SCM_LIST2 (a1, a2));
346}
347
348SCM
349scm_call_generic_3 (SCM gf, SCM a1, SCM a2, SCM a3)
9de33deb 350{
a12be546 351 return scm_apply_generic (gf, SCM_LIST3 (a1, a2, a3));
9de33deb
MD
352}
353
a1ec6916 354SCM_DEFINE (scm_entity_p, "entity?", 1, 0, 0,
1bbd0b84 355 (SCM obj),
717050c8 356"")
1bbd0b84 357#define FUNC_NAME s_scm_entity_p
19c0dec2 358{
0c95b57d 359 return SCM_BOOL(SCM_STRUCTP (obj) && SCM_I_ENTITYP (obj));
19c0dec2 360}
1bbd0b84 361#undef FUNC_NAME
19c0dec2 362
a1ec6916 363SCM_DEFINE (scm_operator_p, "operator?", 1, 0, 0,
1bbd0b84 364 (SCM obj),
717050c8 365"")
1bbd0b84 366#define FUNC_NAME s_scm_operator_p
a43a8375 367{
0b5f3f34 368 return SCM_BOOL(SCM_STRUCTP (obj)
1bbd0b84
GB
369 && SCM_I_OPERATORP (obj)
370 && !SCM_I_ENTITYP (obj));
a43a8375 371}
1bbd0b84 372#undef FUNC_NAME
a43a8375 373
a1ec6916 374SCM_DEFINE (scm_set_object_procedure_x, "set-object-procedure!", 2, 0, 0,
1bbd0b84 375 (SCM obj, SCM proc),
717050c8 376"")
1bbd0b84 377#define FUNC_NAME s_scm_set_object_procedure_x
47b21050 378{
0c95b57d 379 SCM_ASSERT (SCM_STRUCTP (obj)
c1a6fd8f
MD
380 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
381 || (SCM_I_ENTITYP (obj)
382 && !(SCM_OBJ_CLASS_FLAGS (obj)
383 & SCM_CLASSF_PURE_GENERIC))),
47b21050
MD
384 obj,
385 SCM_ARG1,
1bbd0b84 386 FUNC_NAME);
3b3b36dd 387 SCM_VALIDATE_PROC (2,proc);
a12be546 388 if (SCM_I_ENTITYP (obj))
d8c40b9f 389 SCM_SET_ENTITY_PROCEDURE (obj, proc);
a12be546
MD
390 else
391 SCM_OPERATOR_CLASS (obj)->procedure = proc;
47b21050
MD
392 return SCM_UNSPECIFIED;
393}
1bbd0b84 394#undef FUNC_NAME
47b21050 395
a43a8375 396#ifdef GUILE_DEBUG
a1ec6916 397SCM_DEFINE (scm_object_procedure, "object-procedure", 1, 0, 0,
1bbd0b84 398 (SCM obj),
717050c8 399"")
1bbd0b84 400#define FUNC_NAME s_scm_object_procedure
a43a8375 401{
0c95b57d 402 SCM_ASSERT (SCM_STRUCTP (obj)
a12be546
MD
403 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
404 || SCM_I_ENTITYP (obj)),
1bbd0b84 405 obj, SCM_ARG1, FUNC_NAME);
a43a8375 406 return (SCM_I_ENTITYP (obj)
a12be546
MD
407 ? SCM_ENTITY_PROCEDURE (obj)
408 : SCM_OPERATOR_CLASS (obj)->procedure);
a43a8375 409}
1bbd0b84 410#undef FUNC_NAME
a43a8375
MD
411#endif /* GUILE_DEBUG */
412
ed6e0c83
MD
413/* The following procedures are not a part of Goops but a minimal
414 * object system built upon structs. They are here for those who
415 * want to implement their own object system.
416 */
417
036737fc
MD
418SCM
419scm_i_make_class_object (SCM meta,
420 SCM layout_string,
421 unsigned long flags)
47b21050
MD
422{
423 SCM c;
036737fc 424 SCM layout = scm_make_struct_layout (layout_string);
47b21050
MD
425 c = scm_make_struct (meta,
426 SCM_INUM0,
427 SCM_LIST4 (layout, SCM_BOOL_F, SCM_EOL, SCM_EOL));
428 SCM_SET_CLASS_FLAGS (c, flags);
429 return c;
430}
431
a1ec6916 432SCM_DEFINE (scm_make_class_object, "make-class-object", 2, 0, 0,
1bbd0b84 433 (SCM metaclass, SCM layout),
717050c8 434"")
1bbd0b84 435#define FUNC_NAME s_scm_make_class_object
47b21050
MD
436{
437 unsigned long flags = 0;
3b3b36dd
GB
438 SCM_VALIDATE_STRUCT (1,metaclass);
439 SCM_VALIDATE_STRING (2,layout);
54778cd3 440 if (SCM_EQ_P (metaclass, scm_metaclass_operator))
47b21050 441 flags = SCM_CLASSF_OPERATOR;
036737fc 442 return scm_i_make_class_object (metaclass, layout, flags);
47b21050 443}
1bbd0b84 444#undef FUNC_NAME
47b21050 445
a1ec6916 446SCM_DEFINE (scm_make_subclass_object, "make-subclass-object", 2, 0, 0,
1bbd0b84 447 (SCM class, SCM layout),
717050c8 448"")
1bbd0b84 449#define FUNC_NAME s_scm_make_subclass_object
47b21050
MD
450{
451 SCM pl;
3b3b36dd
GB
452 SCM_VALIDATE_STRUCT (1,class);
453 SCM_VALIDATE_STRING (2,layout);
d8c40b9f 454 pl = SCM_PACK (SCM_STRUCT_DATA (class) [scm_vtable_index_layout]);
036737fc 455 /* Convert symbol->string */
bfa974f0 456 pl = scm_makfromstr (SCM_SYMBOL_CHARS (pl), SCM_SYMBOL_LENGTH (pl), 0);
036737fc
MD
457 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
458 scm_string_append (SCM_LIST2 (pl, layout)),
459 SCM_CLASS_FLAGS (class));
47b21050 460}
1bbd0b84 461#undef FUNC_NAME
47b21050 462
1d9ee7c7
MD
463void
464scm_init_objects ()
465{
466 SCM ms = scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT);
3ce4544c 467 SCM mt = scm_make_vtable_vtable (ms, SCM_INUM0,
1d9ee7c7
MD
468 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
469
da7f71d7 470 SCM os = scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT);
3ce4544c 471 SCM ot = scm_make_vtable_vtable (os, SCM_INUM0,
da7f71d7
MD
472 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
473
1d9ee7c7
MD
474 SCM es = scm_makfrom0str (SCM_ENTITY_LAYOUT);
475 SCM el = scm_make_struct_layout (es);
476 SCM et = scm_make_struct (mt, SCM_INUM0,
477 SCM_LIST4 (el, SCM_BOOL_F, SCM_EOL, SCM_EOL));
478
19c0dec2 479 scm_sysintern ("<class>", mt);
1d9ee7c7 480 scm_metaclass_standard = mt;
af3645c5 481 scm_sysintern ("<operator-class>", ot);
da7f71d7
MD
482 scm_metaclass_operator = ot;
483 SCM_SET_CLASS_FLAGS (et, SCM_CLASSF_OPERATOR | SCM_CLASSF_ENTITY);
135e76f8 484 SCM_SET_CLASS_DESTRUCTOR (et, scm_struct_free_entity);
af3645c5 485 scm_sysintern ("<entity>", et);
47b21050 486
8dc9439f 487#ifndef SCM_MAGIC_SNARFER
a0599745 488#include "libguile/objects.x"
8dc9439f 489#endif
1d9ee7c7 490}
89e00824
ML
491
492/*
493 Local Variables:
494 c-file-style: "gnu"
495 End:
496*/