1 /* Copyright (C) 1995, 1996, 1999, 2000 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
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.
52 #include "libguile/_scm.h"
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"
65 #include "libguile/validate.h"
66 #include "libguile/objects.h"
69 SCM scm_metaclass_standard
;
70 SCM scm_metaclass_operator
;
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
;
80 SCM
*scm_port_class
= 0;
81 SCM
*scm_smob_class
= 0;
83 SCM scm_no_applicable_method
;
85 /* This function is used for efficient type dispatch. */
86 SCM_DEFINE (scm_class_of
, "class-of", 1, 0, 0,
88 "Return the class of @var{x}.")
89 #define FUNC_NAME s_scm_class_of
91 switch (SCM_ITAG3 (x
))
95 return scm_class_integer
;
99 return scm_class_char
;
102 switch (SCM_ISYMNUM (x
))
104 case SCM_ISYMNUM (SCM_BOOL_F
):
105 case SCM_ISYMNUM (SCM_BOOL_T
):
106 return scm_class_boolean
;
107 case SCM_ISYMNUM (SCM_EOL
):
108 return scm_class_null
;
110 return scm_class_unknown
;
115 switch (SCM_TYP7 (x
))
117 case scm_tcs_cons_nimcar
:
118 return scm_class_pair
;
119 case scm_tcs_closures
:
120 return scm_class_procedure
;
122 return scm_class_symbol
;
135 return scm_class_vector
;
137 case scm_tc7_substring
:
138 return scm_class_string
;
146 case scm_tc7_subr_1o
:
147 case scm_tc7_subr_2o
:
148 case scm_tc7_lsubr_2
:
150 if (SCM_SUBR_GENERIC (x
) && *SCM_SUBR_GENERIC (x
))
151 return scm_class_primitive_generic
;
153 return scm_class_procedure
;
155 return scm_class_procedure
;
157 return scm_class_procedure_with_setter
;
161 long type
= SCM_TYP16 (x
);
162 if (type
!= scm_tc16_port_with_ps
)
163 return scm_smob_class
[SCM_TC2SMOBNUM (type
)];
164 x
= SCM_PORT_WITH_PS_PORT (x
);
165 /* fall through to ports */
168 return scm_port_class
[(SCM_WRTNG
& SCM_CELL_WORD_0 (x
)
169 ? (SCM_RDNG
& SCM_CELL_WORD_0 (x
)
170 ? SCM_INOUT_PCLASS_INDEX
| SCM_PTOBNUM (x
)
171 : SCM_OUT_PCLASS_INDEX
| SCM_PTOBNUM (x
))
172 : SCM_IN_PCLASS_INDEX
| SCM_PTOBNUM (x
))];
173 case scm_tcs_cons_gloc
:
174 /* must be a struct */
175 if (SCM_OBJ_CLASS_FLAGS (x
) & SCM_CLASSF_GOOPS_VALID
)
176 return SCM_CLASS_OF (x
);
177 else if (SCM_OBJ_CLASS_FLAGS (x
) & SCM_CLASSF_GOOPS
)
180 if (! SCM_FALSEP (SCM_OBJ_CLASS_REDEF (x
)))
181 scm_change_object_class (x
,
182 SCM_CLASS_OF (x
), /* old */
183 SCM_OBJ_CLASS_REDEF (x
)); /* new */
184 return SCM_CLASS_OF (x
);
188 /* ordinary struct */
189 SCM handle
= scm_struct_create_handle (SCM_STRUCT_VTABLE (x
));
190 if (SCM_NFALSEP (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle
))))
191 return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle
));
194 SCM name
= SCM_STRUCT_TABLE_NAME (SCM_CDR (handle
));
195 SCM
class = scm_make_extended_class (SCM_NFALSEP (name
)
196 ? SCM_SYMBOL_CHARS (name
)
198 SCM_SET_STRUCT_TABLE_CLASS (SCM_CDR (handle
), class);
204 return scm_class_pair
;
206 return scm_class_unknown
;
209 case scm_tc3_cons_gloc
:
212 case scm_tc3_closure
:
216 return scm_class_unknown
;
220 /* (SCM_IM_DISPATCH ARGS N-SPECIALIZED
221 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
224 * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK
225 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
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.
233 * SCM_IM_DISPATCH expressions in generic functions always
234 * have ARGS = the symbol `args' or the iloc #@0-0.
236 * Need FORMALS in order to support varying arity. This
237 * also avoids the need for renaming of bindings.
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.
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.
252 scm_mcache_lookup_cmethod (SCM cache
, SCM args
)
255 SCM ls
, methods
, z
= SCM_CDDR (cache
);
256 n
= SCM_INUM (SCM_CAR (z
)); /* maximum number of specializers */
257 methods
= SCM_CADR (z
);
259 if (SCM_NIMP (methods
))
261 /* Prepare for linear search */
264 end
= SCM_VECTOR_LENGTH (methods
);
268 /* Compute a hash value */
269 int hashset
= SCM_INUM (methods
);
271 mask
= SCM_INUM (SCM_CAR (z
= SCM_CDDR (z
)));
272 methods
= SCM_CADR (z
);
278 i
+= SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls
)))
279 [scm_si_hashsets
+ hashset
];
282 while (j
-- && SCM_NIMP (ls
));
287 /* Search for match */
291 z
= SCM_VELTS (methods
)[i
];
292 ls
= args
; /* list of arguments */
296 /* More arguments than specifiers => CLASS != ENV */
297 if (! SCM_EQ_P (scm_class_of (SCM_CAR (ls
)), SCM_CAR (z
)))
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
))))
314 scm_mcache_compute_cmethod (SCM cache
, SCM args
)
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
);
324 scm_apply_generic (SCM gf
, SCM args
)
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
)),
330 SCM_CMETHOD_ENV (cmethod
)));
334 scm_call_generic_0 (SCM gf
)
336 return scm_apply_generic (gf
, SCM_EOL
);
340 scm_call_generic_1 (SCM gf
, SCM a1
)
342 return scm_apply_generic (gf
, SCM_LIST1 (a1
));
346 scm_call_generic_2 (SCM gf
, SCM a1
, SCM a2
)
348 return scm_apply_generic (gf
, SCM_LIST2 (a1
, a2
));
352 scm_call_generic_3 (SCM gf
, SCM a1
, SCM a2
, SCM a3
)
354 return scm_apply_generic (gf
, SCM_LIST3 (a1
, a2
, a3
));
357 SCM_DEFINE (scm_entity_p
, "entity?", 1, 0, 0,
359 "Return @code{#t} if @var{obj} is an entity.")
360 #define FUNC_NAME s_scm_entity_p
362 return SCM_BOOL(SCM_STRUCTP (obj
) && SCM_I_ENTITYP (obj
));
366 SCM_DEFINE (scm_operator_p
, "operator?", 1, 0, 0,
368 "Return @code{#t} if @var{obj} is an operator.")
369 #define FUNC_NAME s_scm_operator_p
371 return SCM_BOOL(SCM_STRUCTP (obj
)
372 && SCM_I_OPERATORP (obj
)
373 && !SCM_I_ENTITYP (obj
));
377 /* XXX - What code requires the object procedure to be only of certain
380 SCM_DEFINE (scm_valid_object_procedure_p
, "valid-object-procedure?", 1, 0, 0,
382 "Return @code{#t} iff @var{proc} is a procedure that can be used "
383 "with @code{set-object-procedure}. It is always valid to use "
384 "a closure constructed by @code{lambda}.")
385 #define FUNC_NAME s_scm_valid_object_procedure_p
389 switch (SCM_TYP7 (proc
))
393 case scm_tcs_closures
:
397 case scm_tc7_lsubr_2
:
403 SCM_DEFINE (scm_set_object_procedure_x
, "set-object-procedure!", 2, 0, 0,
405 "Set the object procedure of @var{obj} to @var{proc}.\n"
406 "@var{obj} must be either an entity or an operator.")
407 #define FUNC_NAME s_scm_set_object_procedure_x
409 SCM_ASSERT (SCM_STRUCTP (obj
)
410 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
411 || (SCM_I_ENTITYP (obj
)
412 && !(SCM_OBJ_CLASS_FLAGS (obj
)
413 & SCM_CLASSF_PURE_GENERIC
))),
417 SCM_ASSERT (scm_valid_object_procedure_p (proc
), proc
, SCM_ARG2
, FUNC_NAME
);
418 if (SCM_I_ENTITYP (obj
))
419 SCM_SET_ENTITY_PROCEDURE (obj
, proc
);
421 SCM_OPERATOR_CLASS (obj
)->procedure
= proc
;
422 return SCM_UNSPECIFIED
;
427 SCM_DEFINE (scm_object_procedure
, "object-procedure", 1, 0, 0,
429 "Return the object procedure of @var{obj}. @var{obj} must be\n"
430 "an entity or an operator.")
431 #define FUNC_NAME s_scm_object_procedure
433 SCM_ASSERT (SCM_STRUCTP (obj
)
434 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
435 || SCM_I_ENTITYP (obj
)),
436 obj
, SCM_ARG1
, FUNC_NAME
);
437 return (SCM_I_ENTITYP (obj
)
438 ? SCM_ENTITY_PROCEDURE (obj
)
439 : SCM_OPERATOR_CLASS (obj
)->procedure
);
442 #endif /* GUILE_DEBUG */
444 /* The following procedures are not a part of Goops but a minimal
445 * object system built upon structs. They are here for those who
446 * want to implement their own object system.
450 scm_i_make_class_object (SCM meta
,
455 SCM layout
= scm_make_struct_layout (layout_string
);
456 c
= scm_make_struct (meta
,
458 SCM_LIST4 (layout
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
459 SCM_SET_CLASS_FLAGS (c
, flags
);
463 SCM_DEFINE (scm_make_class_object
, "make-class-object", 2, 0, 0,
464 (SCM metaclass
, SCM layout
),
465 "Create a new class object of class @var{metaclass}, with the\n"
466 "slot layout specified by @var{layout}.")
467 #define FUNC_NAME s_scm_make_class_object
469 unsigned long flags
= 0;
470 SCM_VALIDATE_STRUCT (1,metaclass
);
471 SCM_VALIDATE_STRING (2,layout
);
472 if (SCM_EQ_P (metaclass
, scm_metaclass_operator
))
473 flags
= SCM_CLASSF_OPERATOR
;
474 return scm_i_make_class_object (metaclass
, layout
, flags
);
478 SCM_DEFINE (scm_make_subclass_object
, "make-subclass-object", 2, 0, 0,
479 (SCM
class, SCM layout
),
480 "Create a subclass object of @var{class}, with the slot layout\n"
481 "specified by @var{layout}.")
482 #define FUNC_NAME s_scm_make_subclass_object
485 SCM_VALIDATE_STRUCT (1,class);
486 SCM_VALIDATE_STRING (2,layout
);
487 pl
= SCM_PACK (SCM_STRUCT_DATA (class) [scm_vtable_index_layout
]);
488 /* Convert symbol->string */
489 pl
= scm_makfromstr (SCM_SYMBOL_CHARS (pl
), SCM_SYMBOL_LENGTH (pl
), 0);
490 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
491 scm_string_append (SCM_LIST2 (pl
, layout
)),
492 SCM_CLASS_FLAGS (class));
499 SCM ms
= scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT
);
500 SCM mt
= scm_make_vtable_vtable (ms
, SCM_INUM0
,
501 SCM_LIST3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
503 SCM os
= scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT
);
504 SCM ot
= scm_make_vtable_vtable (os
, SCM_INUM0
,
505 SCM_LIST3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
507 SCM es
= scm_makfrom0str (SCM_ENTITY_LAYOUT
);
508 SCM el
= scm_make_struct_layout (es
);
509 SCM et
= scm_make_struct (mt
, SCM_INUM0
,
510 SCM_LIST4 (el
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
512 scm_c_define ("<class>", mt
);
513 scm_metaclass_standard
= mt
;
514 scm_c_define ("<operator-class>", ot
);
515 scm_metaclass_operator
= ot
;
516 SCM_SET_CLASS_FLAGS (et
, SCM_CLASSF_OPERATOR
| SCM_CLASSF_ENTITY
);
517 SCM_SET_CLASS_DESTRUCTOR (et
, scm_struct_free_entity
);
518 scm_c_define ("<entity>", et
);
520 #ifndef SCM_MAGIC_SNARFER
521 #include "libguile/objects.x"