* objects.c (scm_class_of): Treat scm_tc16_port_with_ps as ports.
[bpt/guile.git] / libguile / objects.c
1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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. */
41 \f
42
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.
46 */
47
48 #include "_scm.h"
49
50 #include "struct.h"
51 #include "procprop.h"
52 #include "chars.h"
53 #include "keywords.h"
54 #include "smob.h"
55
56 #include "objects.h"
57 \f
58
59 SCM scm_metaclass_standard;
60 SCM scm_metaclass_operator;
61
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;
69
70 SCM *scm_port_class = 0;
71 SCM *scm_smob_class = 0;
72
73 SCM scm_apply_generic_env;
74
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);
78
79 /* This function is used for efficient type dispatch. */
80 SCM
81 scm_class_of (SCM x)
82 {
83 switch (SCM_ITAG3 (x))
84 {
85 case scm_tc3_int_1:
86 case scm_tc3_int_2:
87 return scm_class_integer;
88
89 case scm_tc3_imm24:
90 if (SCM_ICHRP (x))
91 return scm_class_char;
92 else
93 {
94 switch (SCM_ISYMNUM (x))
95 {
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;
101 default:
102 return scm_class_unknown;
103 }
104 }
105
106 case scm_tc3_cons:
107 switch (SCM_TYP7 (x))
108 {
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;
115 case scm_tc7_vector:
116 case scm_tc7_wvect:
117 case scm_tc7_bvect:
118 case scm_tc7_byvect:
119 case scm_tc7_svect:
120 case scm_tc7_ivect:
121 case scm_tc7_uvect:
122 case scm_tc7_fvect:
123 case scm_tc7_dvect:
124 case scm_tc7_cvect:
125 return scm_class_vector;
126 case scm_tc7_string:
127 case scm_tc7_substring:
128 return scm_class_string;
129 case scm_tc7_asubr:
130 case scm_tc7_subr_0:
131 case scm_tc7_subr_1:
132 case scm_tc7_cxr:
133 case scm_tc7_subr_3:
134 case scm_tc7_subr_2:
135 case scm_tc7_rpsubr:
136 case scm_tc7_subr_1o:
137 case scm_tc7_subr_2o:
138 case scm_tc7_lsubr_2:
139 case scm_tc7_lsubr:
140 case scm_tc7_cclo:
141 return scm_class_procedure;
142 case scm_tc7_pws:
143 return scm_class_procedure_with_setter;
144
145 case scm_tc7_smob:
146 {
147 SCM type = SCM_TYP16 (x);
148 if (type == scm_tc16_flo)
149 {
150 if (SCM_CAR (x) & SCM_IMAG_PART)
151 return scm_class_complex;
152 else
153 return scm_class_real;
154 }
155 else if (type != scm_tc16_port_with_ps)
156 return scm_smob_class[SCM_TC2SMOBNUM (type)];
157 x = SCM_PORT_WITH_PS_PORT (x);
158 /* fall through to ports */
159 }
160 case scm_tc7_port:
161 return scm_port_class[(SCM_WRTNG & SCM_CAR (x)
162 ? (SCM_RDNG & SCM_CAR (x)
163 ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
164 : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
165 : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
166 case scm_tcs_cons_gloc:
167 /* must be a struct */
168 if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
169 return SCM_CLASS_OF (x);
170 else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
171 {
172 /* Goops object */
173 if (SCM_OBJ_CLASS_REDEF (x) != SCM_BOOL_F)
174 scm_change_object_class (x,
175 SCM_CLASS_OF (x), /* old */
176 SCM_OBJ_CLASS_REDEF (x)); /* new */
177 return SCM_CLASS_OF (x);
178 }
179 else
180 {
181 /* ordinary struct */
182 SCM handle = scm_struct_create_handle (SCM_STRUCT_VTABLE (x));
183 if (SCM_NFALSEP (SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle))))
184 return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle));
185 else
186 {
187 SCM name = SCM_STRUCT_TABLE_NAME (SCM_CDR (handle));
188 SCM class = scm_make_extended_class (SCM_NFALSEP (name)
189 ? SCM_ROCHARS (name)
190 : 0);
191 SCM_SET_STRUCT_TABLE_CLASS (handle, class);
192 return class;
193 }
194 }
195 default:
196 if (SCM_CONSP (x))
197 return scm_class_pair;
198 else
199 return scm_class_unknown;
200 }
201
202 case scm_tc3_cons_gloc:
203 case scm_tc3_tc7_1:
204 case scm_tc3_tc7_2:
205 case scm_tc3_closure:
206 /* Never reached */
207 break;
208 }
209 return scm_class_unknown;
210 }
211
212 SCM_PROC (s_entity_p, "entity?", 1, 0, 0, scm_entity_p);
213
214 SCM
215 scm_entity_p (SCM obj)
216 {
217 return (SCM_NIMP (obj) && SCM_STRUCTP (obj) && SCM_I_ENTITYP (obj)
218 ? SCM_BOOL_T
219 : SCM_BOOL_F);
220 }
221
222 SCM_PROC (s_operator_p, "operator?", 1, 0, 0, scm_operator_p);
223
224 SCM
225 scm_operator_p (SCM obj)
226 {
227 return (SCM_NIMP (obj)
228 && SCM_STRUCTP (obj)
229 && SCM_I_OPERATORP (obj)
230 && !SCM_I_ENTITYP (obj)
231 ? SCM_BOOL_T
232 : SCM_BOOL_F);
233 }
234
235 SCM_PROC (s_set_object_procedure_x, "set-object-procedure!", 1, 0, 1, scm_set_object_procedure_x);
236
237 SCM_SYMBOL (scm_sym_atdispatch, "@dispatch");
238
239 SCM
240 scm_set_object_procedure_x (SCM obj, SCM procs)
241 {
242 SCM proc[4], *pp, p, setp, arity;
243 int i, a, r;
244 SCM_ASSERT (SCM_NIMP (obj) && SCM_STRUCTP (obj)
245 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
246 || (SCM_I_ENTITYP (obj)
247 && !(SCM_OBJ_CLASS_FLAGS (obj)
248 & SCM_CLASSF_PURE_GENERIC))),
249 obj,
250 SCM_ARG1,
251 s_set_object_procedure_x);
252 for (i = 0; i < 4; ++i)
253 proc[i] = SCM_BOOL_F;
254 i = 0;
255 while (SCM_NIMP (procs))
256 {
257 if (i == 4)
258 scm_wrong_num_args (scm_makfrom0str (s_set_object_procedure_x));
259 p = SCM_CAR (procs);
260 setp = 0;
261 SCM_ASSERT (SCM_NIMP (p), p, SCM_ARG2 + i, s_set_object_procedure_x);
262 if (SCM_CLOSUREP (p))
263 {
264 arity = scm_procedure_property (p, scm_sym_arity);
265 a = SCM_INUM (SCM_CAR (arity));
266 /* Closures have zero optional args */
267 r = SCM_NFALSEP (SCM_CADDR (arity));
268 if (a == 1 || (a <= 1 && r))
269 {
270 if (SCM_NFALSEP (proc[0]))
271 goto ambiguous;
272 proc[0] = setp = p;
273 }
274 if (a == 2 || (a <= 2 && r))
275 {
276 if (SCM_NFALSEP (proc[1]))
277 goto ambiguous;
278 proc[1] = setp = p;
279 }
280 if (a == 3 || (a <= 3 && r))
281 {
282 if (SCM_NFALSEP (proc[2]))
283 goto ambiguous;
284 proc[2] = setp = p;
285 }
286 if (a <= 4 && r)
287 {
288 if (SCM_NFALSEP (proc[3]))
289 goto ambiguous;
290 proc[3] = setp = p;
291 }
292 }
293 else if (SCM_TYP7 (p) == scm_tc7_subr_1)
294 {
295 if (SCM_NFALSEP (proc[0]))
296 goto ambiguous;
297 proc[0] = setp = p;
298 }
299 else if (SCM_TYP7 (p) == scm_tc7_subr_2)
300 {
301 if (SCM_NFALSEP (proc[1]))
302 goto ambiguous;
303 proc[1] = setp = p;
304 }
305 else if (SCM_TYP7 (p) == scm_tc7_subr_3)
306 {
307 if (SCM_NFALSEP (proc[2]))
308 goto ambiguous;
309 proc[2] = setp = p;
310 }
311 else if (SCM_TYP7 (p) == scm_tc7_lsubr_2)
312 {
313 if (SCM_NFALSEP (proc[3]))
314 {
315 ambiguous:
316 SCM_ASSERT (0, p, "Ambiguous procedure arities",
317 s_set_object_procedure_x);
318 }
319 proc[3] = setp = p;
320 }
321 SCM_ASSERT (setp, p, SCM_ARG2 + i, s_set_object_procedure_x);
322 ++i;
323 procs = SCM_CDR (procs);
324 }
325 pp = (SCM_I_ENTITYP (obj)
326 ? &SCM_ENTITY_PROC_0 (obj)
327 : &SCM_OPERATOR_CLASS (obj)->proc0);
328 for (i = 0; i < 4; ++i)
329 *pp++ = proc[i];
330 return SCM_UNSPECIFIED;
331 }
332
333 #ifdef GUILE_DEBUG
334 SCM_PROC (s_object_procedures, "object-procedures", 1, 0, 0, scm_object_procedures);
335
336 SCM
337 scm_object_procedures (SCM obj)
338 {
339 SCM_ASSERT (SCM_NIMP (obj) && SCM_STRUCTP (obj) && SCM_I_OPERATORP (obj),
340 obj, SCM_ARG1, s_object_procedures);
341 return (SCM_I_ENTITYP (obj)
342 ? SCM_LIST4 (SCM_ENTITY_PROC_0 (obj),
343 SCM_ENTITY_PROC_1 (obj),
344 SCM_ENTITY_PROC_2 (obj),
345 SCM_ENTITY_PROC_3 (obj))
346 : SCM_LIST4 (SCM_OPERATOR_PROC_0 (obj),
347 SCM_OPERATOR_PROC_1 (obj),
348 SCM_OPERATOR_PROC_2 (obj),
349 SCM_OPERATOR_PROC_3 (obj)));
350 }
351 #endif /* GUILE_DEBUG */
352
353 /* The following procedures are not a part of Goops but a minimal
354 * object system built upon structs. They are here for those who
355 * want to implement their own object system.
356 */
357
358 SCM
359 scm_i_make_class_object (SCM meta,
360 SCM layout_string,
361 unsigned long flags)
362 {
363 SCM c;
364 SCM layout = scm_make_struct_layout (layout_string);
365 c = scm_make_struct (meta,
366 SCM_INUM0,
367 SCM_LIST4 (layout, SCM_BOOL_F, SCM_EOL, SCM_EOL));
368 SCM_SET_CLASS_FLAGS (c, flags);
369 return c;
370 }
371
372 SCM_PROC (s_make_class_object, "make-class-object", 2, 0, 0, scm_make_class_object);
373
374 SCM
375 scm_make_class_object (SCM metaclass, SCM layout)
376 {
377 unsigned long flags = 0;
378 SCM_ASSERT (SCM_NIMP (metaclass) && SCM_STRUCTP (metaclass),
379 metaclass, SCM_ARG1, s_make_class_object);
380 SCM_ASSERT (SCM_NIMP (layout) && SCM_STRINGP (layout),
381 layout, SCM_ARG2, s_make_class_object);
382 if (metaclass == scm_metaclass_operator)
383 flags = SCM_CLASSF_OPERATOR;
384 return scm_i_make_class_object (metaclass, layout, flags);
385 }
386
387 SCM_PROC (s_make_subclass_object, "make-subclass-object", 2, 0, 0, scm_make_subclass_object);
388
389 SCM
390 scm_make_subclass_object (SCM class, SCM layout)
391 {
392 SCM pl;
393 SCM_ASSERT (SCM_NIMP (class) && SCM_STRUCTP (class),
394 class,
395 SCM_ARG1,
396 s_make_subclass_object);
397 SCM_ASSERT (SCM_NIMP (layout) && SCM_STRINGP (layout),
398 layout,
399 SCM_ARG2,
400 s_make_subclass_object);
401 pl = SCM_STRUCT_DATA (class)[scm_vtable_index_layout];
402 /* Convert symbol->string */
403 pl = scm_makfromstr (SCM_CHARS (pl), (scm_sizet) SCM_LENGTH (pl), 0);
404 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
405 scm_string_append (SCM_LIST2 (pl, layout)),
406 SCM_CLASS_FLAGS (class));
407 }
408
409 void
410 scm_init_objects ()
411 {
412 SCM ms = scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT);
413 SCM ml = scm_make_struct_layout (ms);
414 SCM mt = scm_make_vtable_vtable (ml, SCM_INUM0,
415 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
416
417 SCM os = scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT);
418 SCM ol = scm_make_struct_layout (os);
419 SCM ot = scm_make_vtable_vtable (ol, SCM_INUM0,
420 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
421
422 SCM es = scm_makfrom0str (SCM_ENTITY_LAYOUT);
423 SCM el = scm_make_struct_layout (es);
424 SCM et = scm_make_struct (mt, SCM_INUM0,
425 SCM_LIST4 (el, SCM_BOOL_F, SCM_EOL, SCM_EOL));
426
427 scm_sysintern ("<class>", mt);
428 scm_metaclass_standard = mt;
429 scm_sysintern ("<operator-class>", ot);
430 scm_metaclass_operator = ot;
431 SCM_SET_CLASS_FLAGS (et, SCM_CLASSF_OPERATOR | SCM_CLASSF_ENTITY);
432 SCM_SET_CLASS_DESTRUCTOR (et, scm_struct_free_entity);
433 scm_sysintern ("<entity>", et);
434
435 #include "objects.x"
436 }