1 /* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004, 2006, 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
30 #include "libguile/_scm.h"
32 #include "libguile/struct.h"
33 #include "libguile/procprop.h"
34 #include "libguile/chars.h"
35 #include "libguile/keywords.h"
36 #include "libguile/smob.h"
37 #include "libguile/eval.h"
38 #include "libguile/alist.h"
39 #include "libguile/ports.h"
40 #include "libguile/strings.h"
41 #include "libguile/vectors.h"
43 #include "libguile/validate.h"
44 #include "libguile/objects.h"
45 #include "libguile/goops.h"
49 SCM scm_metaclass_standard
;
50 SCM scm_metaclass_operator
;
52 /* The cache argument for scm_mcache_lookup_cmethod has one of two possible
56 * (SCM_IM_DISPATCH ARGS N-SPECIALIZED
57 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
61 * (SCM_IM_HASH_DISPATCH ARGS N-SPECIALIZED HASHSET MASK
62 * #((TYPE1 ... ENV FORMALS FORM ...) ...)
65 * ARGS is either a list of expressions, in which case they
66 * are interpreted as the arguments of an application, or
67 * a non-pair, which is interpreted as a single expression
68 * yielding all arguments.
70 * SCM_IM_DISPATCH expressions in generic functions always
71 * have ARGS = the symbol `args' or the iloc #@0-0.
73 * Need FORMALS in order to support varying arity. This
74 * also avoids the need for renaming of bindings.
76 * We should probably not complicate this mechanism by
77 * introducing "optimizations" for getters and setters or
78 * primitive methods. Getters and setter will normally be
79 * compiled into @slot-[ref|set!] or a procedure call.
80 * They rely on the dispatch performed before executing
81 * the code which contains them.
83 * We might want to use a more efficient representation of
84 * this form in the future, perhaps after we have introduced
85 * low-level support for syntax-case macros.
89 scm_mcache_lookup_cmethod (SCM cache
, SCM args
)
91 unsigned long i
, mask
, n
, end
;
92 SCM ls
, methods
, z
= SCM_CDDR (cache
);
93 n
= scm_to_ulong (SCM_CAR (z
)); /* maximum number of specializers */
94 methods
= SCM_CADR (z
);
96 if (scm_is_simple_vector (methods
))
98 /* cache format #1: prepare for linear search */
101 end
= SCM_SIMPLE_VECTOR_LENGTH (methods
);
105 /* cache format #2: compute a hash value */
106 unsigned long hashset
= scm_to_ulong (methods
);
109 mask
= scm_to_ulong (SCM_CAR (z
));
110 methods
= SCM_CADR (z
);
113 if (!scm_is_null (ls
))
116 i
+= SCM_STRUCT_DATA (scm_class_of (SCM_CAR (ls
)))
117 [scm_si_hashsets
+ hashset
];
120 while (j
-- && !scm_is_null (ls
));
125 /* Search for match */
129 z
= SCM_SIMPLE_VECTOR_REF (methods
, i
);
130 ls
= args
; /* list of arguments */
131 if (!scm_is_null (ls
))
134 /* More arguments than specifiers => CLASS != ENV */
135 if (! scm_is_eq (scm_class_of (SCM_CAR (ls
)), SCM_CAR (z
)))
140 while (j
-- && !scm_is_null (ls
));
141 /* Fewer arguments than specifiers => CAR != CLASS */
142 if (!SCM_CLASSP (SCM_CAR (z
)))
151 scm_mcache_compute_cmethod (SCM cache
, SCM args
)
153 SCM cmethod
= scm_mcache_lookup_cmethod (cache
, args
);
154 if (scm_is_false (cmethod
))
155 /* No match - memoize */
156 return scm_memoize_method (cache
, args
);
161 scm_apply_generic (SCM gf
, SCM args
)
163 SCM cmethod
= scm_mcache_compute_cmethod (SCM_ENTITY_PROCEDURE (gf
), args
);
164 if (scm_is_pair (cmethod
))
165 return scm_eval_body (SCM_CDR (SCM_CMETHOD_CODE (cmethod
)),
166 SCM_EXTEND_ENV (SCM_CAR (SCM_CMETHOD_CODE (cmethod
)),
168 SCM_CMETHOD_ENV (cmethod
)));
170 return scm_apply (cmethod
, args
, SCM_EOL
);
174 scm_call_generic_0 (SCM gf
)
176 return scm_apply_generic (gf
, SCM_EOL
);
180 scm_call_generic_1 (SCM gf
, SCM a1
)
182 return scm_apply_generic (gf
, scm_list_1 (a1
));
186 scm_call_generic_2 (SCM gf
, SCM a1
, SCM a2
)
188 return scm_apply_generic (gf
, scm_list_2 (a1
, a2
));
192 scm_call_generic_3 (SCM gf
, SCM a1
, SCM a2
, SCM a3
)
194 return scm_apply_generic (gf
, scm_list_3 (a1
, a2
, a3
));
197 SCM_DEFINE (scm_entity_p
, "entity?", 1, 0, 0,
199 "Return @code{#t} if @var{obj} is an entity.")
200 #define FUNC_NAME s_scm_entity_p
202 return scm_from_bool(SCM_STRUCTP (obj
) && SCM_I_ENTITYP (obj
));
206 SCM_DEFINE (scm_operator_p
, "operator?", 1, 0, 0,
208 "Return @code{#t} if @var{obj} is an operator.")
209 #define FUNC_NAME s_scm_operator_p
211 return scm_from_bool(SCM_STRUCTP (obj
)
212 && SCM_I_OPERATORP (obj
)
213 && !SCM_I_ENTITYP (obj
));
217 /* XXX - What code requires the object procedure to be only of certain
220 SCM_DEFINE (scm_valid_object_procedure_p
, "valid-object-procedure?", 1, 0, 0,
222 "Return @code{#t} iff @var{proc} is a procedure that can be used "
223 "with @code{set-object-procedure}. It is always valid to use "
224 "a closure constructed by @code{lambda}.")
225 #define FUNC_NAME s_scm_valid_object_procedure_p
229 switch (SCM_TYP7 (proc
))
233 case scm_tcs_closures
:
237 case scm_tc7_lsubr_2
:
243 SCM_DEFINE (scm_set_object_procedure_x
, "set-object-procedure!", 2, 0, 0,
245 "Set the object procedure of @var{obj} to @var{proc}.\n"
246 "@var{obj} must be either an entity or an operator.")
247 #define FUNC_NAME s_scm_set_object_procedure_x
249 SCM_ASSERT (SCM_STRUCTP (obj
)
250 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
251 || (SCM_I_ENTITYP (obj
)
252 && !(SCM_OBJ_CLASS_FLAGS (obj
)
253 & SCM_CLASSF_PURE_GENERIC
))),
257 SCM_ASSERT (scm_valid_object_procedure_p (proc
), proc
, SCM_ARG2
, FUNC_NAME
);
258 if (SCM_I_ENTITYP (obj
))
259 SCM_SET_ENTITY_PROCEDURE (obj
, proc
);
261 SCM_OPERATOR_CLASS (obj
)->procedure
= proc
;
262 return SCM_UNSPECIFIED
;
267 SCM_DEFINE (scm_object_procedure
, "object-procedure", 1, 0, 0,
269 "Return the object procedure of @var{obj}. @var{obj} must be\n"
270 "an entity or an operator.")
271 #define FUNC_NAME s_scm_object_procedure
273 SCM_ASSERT (SCM_STRUCTP (obj
)
274 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
275 || SCM_I_ENTITYP (obj
)),
276 obj
, SCM_ARG1
, FUNC_NAME
);
277 return (SCM_I_ENTITYP (obj
)
278 ? SCM_ENTITY_PROCEDURE (obj
)
279 : SCM_OPERATOR_CLASS (obj
)->procedure
);
282 #endif /* GUILE_DEBUG */
284 /* The following procedures are not a part of Goops but a minimal
285 * object system built upon structs. They are here for those who
286 * want to implement their own object system.
290 scm_i_make_class_object (SCM meta
,
295 SCM layout
= scm_make_struct_layout (layout_string
);
296 c
= scm_make_struct (meta
,
298 scm_list_4 (layout
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
299 SCM_SET_CLASS_FLAGS (c
, flags
);
303 SCM_DEFINE (scm_make_class_object
, "make-class-object", 2, 0, 0,
304 (SCM metaclass
, SCM layout
),
305 "Create a new class object of class @var{metaclass}, with the\n"
306 "slot layout specified by @var{layout}.")
307 #define FUNC_NAME s_scm_make_class_object
309 unsigned long flags
= 0;
310 SCM_VALIDATE_STRUCT (1, metaclass
);
311 SCM_VALIDATE_STRING (2, layout
);
312 if (scm_is_eq (metaclass
, scm_metaclass_operator
))
313 flags
= SCM_CLASSF_OPERATOR
;
314 return scm_i_make_class_object (metaclass
, layout
, flags
);
318 SCM_DEFINE (scm_make_subclass_object
, "make-subclass-object", 2, 0, 0,
319 (SCM
class, SCM layout
),
320 "Create a subclass object of @var{class}, with the slot layout\n"
321 "specified by @var{layout}.")
322 #define FUNC_NAME s_scm_make_subclass_object
325 SCM_VALIDATE_STRUCT (1, class);
326 SCM_VALIDATE_STRING (2, layout
);
327 pl
= SCM_PACK (SCM_STRUCT_DATA (class) [scm_vtable_index_layout
]);
328 pl
= scm_symbol_to_string (pl
);
329 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
330 scm_string_append (scm_list_2 (pl
, layout
)),
331 SCM_CLASS_FLAGS (class));
338 SCM ms
= scm_from_locale_string (SCM_METACLASS_STANDARD_LAYOUT
);
339 SCM mt
= scm_make_vtable_vtable (ms
, SCM_INUM0
,
340 scm_list_3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
342 SCM os
= scm_from_locale_string (SCM_METACLASS_OPERATOR_LAYOUT
);
343 SCM ot
= scm_make_vtable_vtable (os
, SCM_INUM0
,
344 scm_list_3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
346 SCM es
= scm_from_locale_string (SCM_ENTITY_LAYOUT
);
347 SCM el
= scm_make_struct_layout (es
);
348 SCM et
= scm_make_struct (mt
, SCM_INUM0
,
349 scm_list_4 (el
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
351 scm_c_define ("<class>", mt
);
352 scm_metaclass_standard
= mt
;
353 scm_c_define ("<operator-class>", ot
);
354 scm_metaclass_operator
= ot
;
355 SCM_SET_CLASS_FLAGS (et
, SCM_CLASSF_OPERATOR
| SCM_CLASSF_ENTITY
);
356 SCM_SET_CLASS_DESTRUCTOR (et
, scm_struct_free_entity
);
357 scm_c_define ("<entity>", et
);
359 #include "libguile/objects.x"