1 /* Copyright (C) 1995,1996,1999,2000,2001, 2003 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* This file and objects.h contains those minimal pieces of the Guile
22 * Object Oriented Programming System which need to be included in
23 * libguile. See the comments in objects.h.
26 #include "libguile/_scm.h"
28 #include "libguile/struct.h"
29 #include "libguile/procprop.h"
30 #include "libguile/chars.h"
31 #include "libguile/keywords.h"
32 #include "libguile/smob.h"
33 #include "libguile/eval.h"
34 #include "libguile/alist.h"
35 #include "libguile/ports.h"
36 #include "libguile/strings.h"
37 #include "libguile/vectors.h"
39 #include "libguile/validate.h"
40 #include "libguile/objects.h"
43 SCM scm_metaclass_standard
;
44 SCM scm_metaclass_operator
;
46 /* These variables are filled in by the object system when loaded. */
47 SCM scm_class_boolean
, scm_class_char
, scm_class_pair
;
48 SCM scm_class_procedure
, scm_class_string
, scm_class_symbol
;
49 SCM scm_class_procedure_with_setter
, scm_class_primitive_generic
;
50 SCM scm_class_vector
, scm_class_null
;
51 SCM scm_class_integer
, scm_class_real
, scm_class_complex
;
52 SCM scm_class_unknown
;
54 SCM
*scm_port_class
= 0;
55 SCM
*scm_smob_class
= 0;
57 SCM scm_no_applicable_method
;
59 /* This function is used for efficient type dispatch. */
60 SCM_DEFINE (scm_class_of
, "class-of", 1, 0, 0,
62 "Return the class of @var{x}.")
63 #define FUNC_NAME s_scm_class_of
65 switch (SCM_ITAG3 (x
))
69 return scm_class_integer
;
73 return scm_class_char
;
76 switch (SCM_ISYMNUM (x
))
78 case SCM_ISYMNUM (SCM_BOOL_F
):
79 case SCM_ISYMNUM (SCM_BOOL_T
):
80 return scm_class_boolean
;
81 case SCM_ISYMNUM (SCM_EOL
):
82 return scm_class_null
;
84 return scm_class_unknown
;
91 case scm_tcs_cons_nimcar
:
92 return scm_class_pair
;
93 case scm_tcs_closures
:
94 return scm_class_procedure
;
96 return scm_class_symbol
;
109 return scm_class_vector
;
111 return scm_class_string
;
119 case scm_tc7_subr_1o
:
120 case scm_tc7_subr_2o
:
121 case scm_tc7_lsubr_2
:
123 if (SCM_SUBR_GENERIC (x
) && *SCM_SUBR_GENERIC (x
))
124 return scm_class_primitive_generic
;
126 return scm_class_procedure
;
128 return scm_class_procedure
;
130 return scm_class_procedure_with_setter
;
134 scm_t_bits type
= SCM_TYP16 (x
);
135 if (type
!= scm_tc16_port_with_ps
)
136 return scm_smob_class
[SCM_TC2SMOBNUM (type
)];
137 x
= SCM_PORT_WITH_PS_PORT (x
);
138 /* fall through to ports */
141 return scm_port_class
[(SCM_WRTNG
& SCM_CELL_WORD_0 (x
)
142 ? (SCM_RDNG
& SCM_CELL_WORD_0 (x
)
143 ? SCM_INOUT_PCLASS_INDEX
| SCM_PTOBNUM (x
)
144 : SCM_OUT_PCLASS_INDEX
| SCM_PTOBNUM (x
))
145 : SCM_IN_PCLASS_INDEX
| SCM_PTOBNUM (x
))];
147 if (SCM_OBJ_CLASS_FLAGS (x
) & SCM_CLASSF_GOOPS_VALID
)
148 return SCM_CLASS_OF (x
);
149 else if (SCM_OBJ_CLASS_FLAGS (x
) & SCM_CLASSF_GOOPS
)
152 if (! SCM_FALSEP (SCM_OBJ_CLASS_REDEF (x
)))
153 scm_change_object_class (x
,
154 SCM_CLASS_OF (x
), /* old */
155 SCM_OBJ_CLASS_REDEF (x
)); /* new */
156 return SCM_CLASS_OF (x
);
160 /* ordinary struct */
161 SCM handle
= scm_struct_create_handle (SCM_STRUCT_VTABLE (x
));
162 if (!SCM_FALSEP (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle
))))
163 return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle
));
166 SCM name
= SCM_STRUCT_TABLE_NAME (SCM_CDR (handle
));
167 SCM
class = scm_make_extended_class (!SCM_FALSEP (name
)
168 ? SCM_SYMBOL_CHARS (name
)
170 SCM_I_OPERATORP (x
));
171 SCM_SET_STRUCT_TABLE_CLASS (SCM_CDR (handle
), class);
177 return scm_class_pair
;
179 return scm_class_unknown
;
185 case scm_tc3_closure
:
189 return scm_class_unknown
;
193 /* The cache argument for scm_mcache_lookup_cmethod has one of two possible
197 * (SCM_IM_DISPATCH ARGS N-SPECIALIZED
198 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
202 * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK
203 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
206 * ARGS is either a list of expressions, in which case they
207 * are interpreted as the arguments of an application, or
208 * a non-pair, which is interpreted as a single expression
209 * yielding all arguments.
211 * SCM_IM_DISPATCH expressions in generic functions always
212 * have ARGS = the symbol `args' or the iloc #@0-0.
214 * Need FORMALS in order to support varying arity. This
215 * also avoids the need for renaming of bindings.
217 * We should probably not complicate this mechanism by
218 * introducing "optimizations" for getters and setters or
219 * primitive methods. Getters and setter will normally be
220 * compiled into @slot-[ref|set!] or a procedure call.
221 * They rely on the dispatch performed before executing
222 * the code which contains them.
224 * We might want to use a more efficient representation of
225 * this form in the future, perhaps after we have introduced
226 * low-level support for syntax-case macros.
230 scm_mcache_lookup_cmethod (SCM cache
, SCM args
)
232 long i
, n
, end
, mask
;
233 SCM ls
, methods
, z
= SCM_CDDR (cache
);
234 n
= SCM_INUM (SCM_CAR (z
)); /* maximum number of specializers */
235 methods
= SCM_CADR (z
);
237 if (SCM_INUMP (methods
))
239 /* cache format #2: compute a hash value */
240 long hashset
= SCM_INUM (methods
);
243 mask
= SCM_INUM (SCM_CAR (z
));
244 methods
= SCM_CADR (z
);
250 i
+= SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls
)))
251 [scm_si_hashsets
+ hashset
];
254 while (j
-- && !SCM_NULLP (ls
));
258 else /* SCM_VECTORP (methods) */
260 /* cache format #1: prepare for linear search */
263 end
= SCM_VECTOR_LENGTH (methods
);
266 /* Search for match */
270 z
= SCM_VELTS (methods
)[i
];
271 ls
= args
; /* list of arguments */
275 /* More arguments than specifiers => CLASS != ENV */
276 if (! SCM_EQ_P (scm_class_of (SCM_CAR (ls
)), SCM_CAR (z
)))
281 while (j
-- && !SCM_NULLP (ls
));
282 /* Fewer arguments than specifiers => CAR != ENV */
283 if (SCM_NULLP (SCM_CAR (z
)) || SCM_CONSP (SCM_CAR (z
)))
292 scm_mcache_compute_cmethod (SCM cache
, SCM args
)
294 SCM cmethod
= scm_mcache_lookup_cmethod (cache
, args
);
295 if (SCM_FALSEP (cmethod
))
296 /* No match - memoize */
297 return scm_memoize_method (cache
, args
);
302 scm_apply_generic (SCM gf
, SCM args
)
304 SCM cmethod
= scm_mcache_compute_cmethod (SCM_ENTITY_PROCEDURE (gf
), args
);
305 return scm_eval_body (SCM_CDR (SCM_CMETHOD_CODE (cmethod
)),
306 SCM_EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (cmethod
)),
308 SCM_CMETHOD_ENV (cmethod
)));
312 scm_call_generic_0 (SCM gf
)
314 return scm_apply_generic (gf
, SCM_EOL
);
318 scm_call_generic_1 (SCM gf
, SCM a1
)
320 return scm_apply_generic (gf
, scm_list_1 (a1
));
324 scm_call_generic_2 (SCM gf
, SCM a1
, SCM a2
)
326 return scm_apply_generic (gf
, scm_list_2 (a1
, a2
));
330 scm_call_generic_3 (SCM gf
, SCM a1
, SCM a2
, SCM a3
)
332 return scm_apply_generic (gf
, scm_list_3 (a1
, a2
, a3
));
335 SCM_DEFINE (scm_entity_p
, "entity?", 1, 0, 0,
337 "Return @code{#t} if @var{obj} is an entity.")
338 #define FUNC_NAME s_scm_entity_p
340 return SCM_BOOL(SCM_STRUCTP (obj
) && SCM_I_ENTITYP (obj
));
344 SCM_DEFINE (scm_operator_p
, "operator?", 1, 0, 0,
346 "Return @code{#t} if @var{obj} is an operator.")
347 #define FUNC_NAME s_scm_operator_p
349 return SCM_BOOL(SCM_STRUCTP (obj
)
350 && SCM_I_OPERATORP (obj
)
351 && !SCM_I_ENTITYP (obj
));
355 /* XXX - What code requires the object procedure to be only of certain
358 SCM_DEFINE (scm_valid_object_procedure_p
, "valid-object-procedure?", 1, 0, 0,
360 "Return @code{#t} iff @var{proc} is a procedure that can be used "
361 "with @code{set-object-procedure}. It is always valid to use "
362 "a closure constructed by @code{lambda}.")
363 #define FUNC_NAME s_scm_valid_object_procedure_p
367 switch (SCM_TYP7 (proc
))
371 case scm_tcs_closures
:
375 case scm_tc7_lsubr_2
:
381 SCM_DEFINE (scm_set_object_procedure_x
, "set-object-procedure!", 2, 0, 0,
383 "Set the object procedure of @var{obj} to @var{proc}.\n"
384 "@var{obj} must be either an entity or an operator.")
385 #define FUNC_NAME s_scm_set_object_procedure_x
387 SCM_ASSERT (SCM_STRUCTP (obj
)
388 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
389 || (SCM_I_ENTITYP (obj
)
390 && !(SCM_OBJ_CLASS_FLAGS (obj
)
391 & SCM_CLASSF_PURE_GENERIC
))),
395 SCM_ASSERT (scm_valid_object_procedure_p (proc
), proc
, SCM_ARG2
, FUNC_NAME
);
396 if (SCM_I_ENTITYP (obj
))
397 SCM_SET_ENTITY_PROCEDURE (obj
, proc
);
399 SCM_OPERATOR_CLASS (obj
)->procedure
= proc
;
400 return SCM_UNSPECIFIED
;
405 SCM_DEFINE (scm_object_procedure
, "object-procedure", 1, 0, 0,
407 "Return the object procedure of @var{obj}. @var{obj} must be\n"
408 "an entity or an operator.")
409 #define FUNC_NAME s_scm_object_procedure
411 SCM_ASSERT (SCM_STRUCTP (obj
)
412 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
413 || SCM_I_ENTITYP (obj
)),
414 obj
, SCM_ARG1
, FUNC_NAME
);
415 return (SCM_I_ENTITYP (obj
)
416 ? SCM_ENTITY_PROCEDURE (obj
)
417 : SCM_OPERATOR_CLASS (obj
)->procedure
);
420 #endif /* GUILE_DEBUG */
422 /* The following procedures are not a part of Goops but a minimal
423 * object system built upon structs. They are here for those who
424 * want to implement their own object system.
428 scm_i_make_class_object (SCM meta
,
433 SCM layout
= scm_make_struct_layout (layout_string
);
434 c
= scm_make_struct (meta
,
436 scm_list_4 (layout
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
437 SCM_SET_CLASS_FLAGS (c
, flags
);
441 SCM_DEFINE (scm_make_class_object
, "make-class-object", 2, 0, 0,
442 (SCM metaclass
, SCM layout
),
443 "Create a new class object of class @var{metaclass}, with the\n"
444 "slot layout specified by @var{layout}.")
445 #define FUNC_NAME s_scm_make_class_object
447 unsigned long flags
= 0;
448 SCM_VALIDATE_STRUCT (1, metaclass
);
449 SCM_VALIDATE_STRING (2, layout
);
450 if (SCM_EQ_P (metaclass
, scm_metaclass_operator
))
451 flags
= SCM_CLASSF_OPERATOR
;
452 return scm_i_make_class_object (metaclass
, layout
, flags
);
456 SCM_DEFINE (scm_make_subclass_object
, "make-subclass-object", 2, 0, 0,
457 (SCM
class, SCM layout
),
458 "Create a subclass object of @var{class}, with the slot layout\n"
459 "specified by @var{layout}.")
460 #define FUNC_NAME s_scm_make_subclass_object
463 SCM_VALIDATE_STRUCT (1, class);
464 SCM_VALIDATE_STRING (2, layout
);
465 pl
= SCM_PACK (SCM_STRUCT_DATA (class) [scm_vtable_index_layout
]);
466 /* Convert symbol->string */
467 pl
= scm_mem2string (SCM_SYMBOL_CHARS (pl
), SCM_SYMBOL_LENGTH (pl
));
468 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
469 scm_string_append (scm_list_2 (pl
, layout
)),
470 SCM_CLASS_FLAGS (class));
477 SCM ms
= scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT
);
478 SCM mt
= scm_make_vtable_vtable (ms
, SCM_INUM0
,
479 scm_list_3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
481 SCM os
= scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT
);
482 SCM ot
= scm_make_vtable_vtable (os
, SCM_INUM0
,
483 scm_list_3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
485 SCM es
= scm_makfrom0str (SCM_ENTITY_LAYOUT
);
486 SCM el
= scm_make_struct_layout (es
);
487 SCM et
= scm_make_struct (mt
, SCM_INUM0
,
488 scm_list_4 (el
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
490 scm_c_define ("<class>", mt
);
491 scm_metaclass_standard
= mt
;
492 scm_c_define ("<operator-class>", ot
);
493 scm_metaclass_operator
= ot
;
494 SCM_SET_CLASS_FLAGS (et
, SCM_CLASSF_OPERATOR
| SCM_CLASSF_ENTITY
);
495 SCM_SET_CLASS_DESTRUCTOR (et
, scm_struct_free_entity
);
496 scm_c_define ("<entity>", et
);
498 #include "libguile/objects.x"