1 /* Copyright (C) 1995,1996 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. */
43 /* This file and objects.h contains those minimal pieces of the Guile
44 * Object Oriented Programming System which need to be included in
45 * libguile. See the comments in objects.h.
59 SCM scm_metaclass_standard
;
60 SCM scm_metaclass_operator
;
62 /* These variables are filled in by the object system when loaded. */
63 SCM scm_class_boolean
, scm_class_char
, scm_class_pair
;
64 SCM scm_class_procedure
, scm_class_string
, scm_class_symbol
;
65 SCM scm_class_procedure_with_setter
;
66 SCM scm_class_vector
, scm_class_null
;
67 SCM scm_class_integer
, scm_class_real
, scm_class_complex
;
68 SCM scm_class_unknown
;
70 SCM
*scm_port_class
= 0;
71 SCM
*scm_smob_class
= 0;
73 SCM scm_apply_generic_env
;
75 SCM (*scm_make_extended_class
) (char *type_name
);
76 void (*scm_make_port_classes
) (int ptobnum
, char *type_name
);
77 void (*scm_change_object_class
) (SCM
, SCM
, SCM
);
79 /* This function is used for efficient type dispatch. */
83 switch (SCM_ITAG3 (x
))
87 return scm_class_integer
;
91 return scm_class_char
;
94 switch (SCM_ISYMNUM (x
))
96 case SCM_ISYMNUM (SCM_BOOL_F
):
97 case SCM_ISYMNUM (SCM_BOOL_T
):
98 return scm_class_boolean
;
99 case SCM_ISYMNUM (SCM_EOL
):
100 return scm_class_null
;
102 return scm_class_unknown
;
107 switch (SCM_TYP7 (x
))
109 case scm_tcs_cons_nimcar
:
110 return scm_class_pair
;
111 case scm_tcs_closures
:
112 return scm_class_procedure
;
113 case scm_tcs_symbols
:
114 return scm_class_symbol
;
125 return scm_class_vector
;
127 case scm_tc7_substring
:
128 return scm_class_string
;
136 case scm_tc7_subr_1o
:
137 case scm_tc7_subr_2o
:
138 case scm_tc7_lsubr_2
:
141 return scm_class_procedure
;
143 return scm_class_procedure_with_setter
;
146 return scm_port_class
[(SCM_WRTNG
& SCM_CAR (x
)
147 ? (SCM_RDNG
& SCM_CAR (x
)
148 ? SCM_INOUT_PCLASS_INDEX
| SCM_PTOBNUM (x
)
149 : SCM_OUT_PCLASS_INDEX
| SCM_PTOBNUM (x
))
150 : SCM_IN_PCLASS_INDEX
| SCM_PTOBNUM (x
))];
153 SCM type
= SCM_TYP16 (x
);
154 if (type
== scm_tc16_flo
)
156 if (SCM_CAR (x
) & SCM_IMAG_PART
)
157 return scm_class_complex
;
159 return scm_class_real
;
162 return scm_smob_class
[SCM_TC2SMOBNUM (type
)];
164 case scm_tcs_cons_gloc
:
165 /* must be a struct */
166 if (SCM_OBJ_CLASS_FLAGS (x
) & SCM_CLASSF_GOOPS_VALID
)
167 return SCM_CLASS_OF (x
);
168 else if (SCM_OBJ_CLASS_FLAGS (x
) & SCM_CLASSF_GOOPS
)
171 if (SCM_OBJ_CLASS_REDEF (x
) != SCM_BOOL_F
)
172 scm_change_object_class (x
,
173 SCM_CLASS_OF (x
), /* old */
174 SCM_OBJ_CLASS_REDEF (x
)); /* new */
175 return SCM_CLASS_OF (x
);
179 /* ordinary struct */
180 SCM handle
= scm_struct_create_handle (SCM_STRUCT_VTABLE (x
));
181 if (SCM_NFALSEP (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle
))))
182 return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle
));
185 SCM name
= SCM_STRUCT_TABLE_NAME (SCM_CDR (handle
));
186 SCM
class = scm_make_extended_class (SCM_NFALSEP (name
)
189 SCM_SET_STRUCT_TABLE_CLASS (handle
, class);
195 return scm_class_pair
;
197 return scm_class_unknown
;
200 case scm_tc3_cons_gloc
:
203 case scm_tc3_closure
:
207 return scm_class_unknown
;
210 SCM_PROC (s_entity_p
, "entity?", 1, 0, 0, scm_entity_p
);
213 scm_entity_p (SCM obj
)
215 return (SCM_NIMP (obj
) && SCM_STRUCTP (obj
) && SCM_I_ENTITYP (obj
)
220 SCM_PROC (s_operator_p
, "operator?", 1, 0, 0, scm_operator_p
);
223 scm_operator_p (SCM obj
)
225 return (SCM_NIMP (obj
)
227 && SCM_I_OPERATORP (obj
)
228 && !SCM_I_ENTITYP (obj
)
233 SCM_PROC (s_set_object_procedure_x
, "set-object-procedure!", 1, 0, 1, scm_set_object_procedure_x
);
235 SCM_SYMBOL (scm_sym_atdispatch
, "@dispatch");
238 scm_set_object_procedure_x (SCM obj
, SCM procs
)
240 SCM proc
[4], *pp
, p
, setp
, arity
;
242 SCM_ASSERT (SCM_NIMP (obj
) && SCM_STRUCTP (obj
)
243 && ((SCM_CLASS_FLAGS (obj
) & SCM_CLASSF_OPERATOR
)
244 || (SCM_I_ENTITYP (obj
)
245 && !(SCM_OBJ_CLASS_FLAGS (obj
)
246 & SCM_CLASSF_PURE_GENERIC
))),
249 s_set_object_procedure_x
);
250 for (i
= 0; i
< 4; ++i
)
251 proc
[i
] = SCM_BOOL_F
;
253 while (SCM_NIMP (procs
))
256 scm_wrong_num_args (scm_makfrom0str (s_set_object_procedure_x
));
259 SCM_ASSERT (SCM_NIMP (p
), p
, SCM_ARG2
+ i
, s_set_object_procedure_x
);
260 if (SCM_CLOSUREP (p
))
262 arity
= scm_procedure_property (p
, scm_sym_arity
);
263 a
= SCM_INUM (SCM_CAR (arity
));
264 /* Closures have zero optional args */
265 r
= SCM_NFALSEP (SCM_CADDR (arity
));
266 if (a
== 1 || (a
<= 1 && r
))
268 if (SCM_NFALSEP (proc
[0]))
272 if (a
== 2 || (a
<= 2 && r
))
274 if (SCM_NFALSEP (proc
[1]))
278 if (a
== 3 || (a
<= 3 && r
))
280 if (SCM_NFALSEP (proc
[2]))
286 if (SCM_NFALSEP (proc
[3]))
291 else if (SCM_TYP7 (p
) == scm_tc7_subr_1
)
293 if (SCM_NFALSEP (proc
[0]))
297 else if (SCM_TYP7 (p
) == scm_tc7_subr_2
)
299 if (SCM_NFALSEP (proc
[1]))
303 else if (SCM_TYP7 (p
) == scm_tc7_subr_3
)
305 if (SCM_NFALSEP (proc
[2]))
309 else if (SCM_TYP7 (p
) == scm_tc7_lsubr_2
)
311 if (SCM_NFALSEP (proc
[3]))
314 SCM_ASSERT (0, p
, "Ambiguous procedure arities",
315 s_set_object_procedure_x
);
319 SCM_ASSERT (setp
, p
, SCM_ARG2
+ i
, s_set_object_procedure_x
);
321 procs
= SCM_CDR (procs
);
323 pp
= (SCM_I_ENTITYP (obj
)
324 ? &SCM_ENTITY_PROC_0 (obj
)
325 : &SCM_OPERATOR_CLASS (obj
)->proc0
);
326 for (i
= 0; i
< 4; ++i
)
328 return SCM_UNSPECIFIED
;
332 SCM_PROC (s_object_procedures
, "object-procedures", 1, 0, 0, scm_object_procedures
);
335 scm_object_procedures (SCM obj
)
337 SCM_ASSERT (SCM_NIMP (obj
) && SCM_STRUCTP (obj
) && SCM_I_OPERATORP (obj
),
338 obj
, SCM_ARG1
, s_object_procedures
);
339 return (SCM_I_ENTITYP (obj
)
340 ? SCM_LIST4 (SCM_ENTITY_PROC_0 (obj
),
341 SCM_ENTITY_PROC_1 (obj
),
342 SCM_ENTITY_PROC_2 (obj
),
343 SCM_ENTITY_PROC_3 (obj
))
344 : SCM_LIST4 (SCM_OPERATOR_PROC_0 (obj
),
345 SCM_OPERATOR_PROC_1 (obj
),
346 SCM_OPERATOR_PROC_2 (obj
),
347 SCM_OPERATOR_PROC_3 (obj
)));
349 #endif /* GUILE_DEBUG */
351 /* The following procedures are not a part of Goops but a minimal
352 * object system built upon structs. They are here for those who
353 * want to implement their own object system.
357 scm_i_make_class_object (SCM meta
,
362 SCM layout
= scm_make_struct_layout (layout_string
);
363 c
= scm_make_struct (meta
,
365 SCM_LIST4 (layout
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
366 SCM_SET_CLASS_FLAGS (c
, flags
);
370 SCM_PROC (s_make_class_object
, "make-class-object", 2, 0, 0, scm_make_class_object
);
373 scm_make_class_object (SCM metaclass
, SCM layout
)
375 unsigned long flags
= 0;
376 SCM_ASSERT (SCM_NIMP (metaclass
) && SCM_STRUCTP (metaclass
),
377 metaclass
, SCM_ARG1
, s_make_class_object
);
378 SCM_ASSERT (SCM_NIMP (layout
) && SCM_STRINGP (layout
),
379 layout
, SCM_ARG2
, s_make_class_object
);
380 if (metaclass
== scm_metaclass_operator
)
381 flags
= SCM_CLASSF_OPERATOR
;
382 return scm_i_make_class_object (metaclass
, layout
, flags
);
385 SCM_PROC (s_make_subclass_object
, "make-subclass-object", 2, 0, 0, scm_make_subclass_object
);
388 scm_make_subclass_object (SCM
class, SCM layout
)
391 SCM_ASSERT (SCM_NIMP (class) && SCM_STRUCTP (class),
394 s_make_subclass_object
);
395 SCM_ASSERT (SCM_NIMP (layout
) && SCM_STRINGP (layout
),
398 s_make_subclass_object
);
399 pl
= SCM_STRUCT_DATA (class)[scm_vtable_index_layout
];
400 /* Convert symbol->string */
401 pl
= scm_makfromstr (SCM_CHARS (pl
), (scm_sizet
) SCM_LENGTH (pl
), 0);
402 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
403 scm_string_append (SCM_LIST2 (pl
, layout
)),
404 SCM_CLASS_FLAGS (class));
410 SCM ms
= scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT
);
411 SCM ml
= scm_make_struct_layout (ms
);
412 SCM mt
= scm_make_vtable_vtable (ml
, SCM_INUM0
,
413 SCM_LIST3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
415 SCM os
= scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT
);
416 SCM ol
= scm_make_struct_layout (os
);
417 SCM ot
= scm_make_vtable_vtable (ol
, SCM_INUM0
,
418 SCM_LIST3 (SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
420 SCM es
= scm_makfrom0str (SCM_ENTITY_LAYOUT
);
421 SCM el
= scm_make_struct_layout (es
);
422 SCM et
= scm_make_struct (mt
, SCM_INUM0
,
423 SCM_LIST4 (el
, SCM_BOOL_F
, SCM_EOL
, SCM_EOL
));
425 scm_sysintern ("<class>", mt
);
426 scm_metaclass_standard
= mt
;
427 scm_sysintern ("<operator-class>", ot
);
428 scm_metaclass_operator
= ot
;
429 SCM_SET_CLASS_FLAGS (et
, SCM_CLASSF_OPERATOR
| SCM_CLASSF_ENTITY
);
430 SCM_SET_CLASS_DESTRUCTOR (et
, scm_struct_free_entity
);
431 scm_sysintern ("<entity>", et
);