*** empty log message ***
[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 "kw.h"
54
55 #include "objects.h"
56 \f
57
58 SCM scm_metaclass_standard;
59 SCM scm_metaclass_operator;
60
61 /* These variables are filled in by the object system when loaded. */
62 SCM scm_class_boolean, scm_class_char, scm_class_pair;
63 SCM scm_class_procedure, scm_class_string, scm_class_symbol;
64 SCM scm_class_vector, scm_class_null;
65 SCM scm_class_real, scm_class_complex, scm_class_integer;
66 SCM scm_class_keyword, scm_class_unknown;
67
68 void (*scm_change_object_class) (SCM, SCM, SCM);
69
70 /* This function is used for efficient type dispatch. */
71 SCM
72 scm_class_of (SCM x)
73 {
74 switch (SCM_ITAG3 (x))
75 {
76 case scm_tc3_int_1:
77 case scm_tc3_int_2:
78 return scm_class_integer;
79
80 case scm_tc3_imm24:
81 if (SCM_ICHRP (x))
82 return scm_class_char;
83 else
84 {
85 switch (SCM_ISYMNUM (x))
86 {
87 case SCM_ISYMNUM (SCM_BOOL_F):
88 case SCM_ISYMNUM (SCM_BOOL_T):
89 return scm_class_boolean;
90 case SCM_ISYMNUM (SCM_EOL):
91 return scm_class_null;
92 default:
93 return scm_class_unknown;
94 }
95 }
96
97 case scm_tc3_cons:
98 switch (SCM_TYP7 (x))
99 {
100 case scm_tcs_cons_nimcar:
101 return scm_class_pair;
102 case scm_tcs_closures:
103 return scm_class_procedure;
104 case scm_tcs_symbols:
105 return scm_class_symbol;
106 case scm_tc7_vector:
107 case scm_tc7_wvect:
108 case scm_tc7_bvect:
109 case scm_tc7_byvect:
110 case scm_tc7_svect:
111 case scm_tc7_ivect:
112 case scm_tc7_uvect:
113 case scm_tc7_fvect:
114 case scm_tc7_dvect:
115 case scm_tc7_cvect:
116 return scm_class_vector;
117 case scm_tc7_string:
118 case scm_tc7_substring:
119 return scm_class_string;
120 case scm_tc7_asubr:
121 case scm_tc7_subr_0:
122 case scm_tc7_subr_1:
123 case scm_tc7_cxr:
124 case scm_tc7_subr_3:
125 case scm_tc7_subr_2:
126 case scm_tc7_rpsubr:
127 case scm_tc7_subr_1o:
128 case scm_tc7_subr_2o:
129 case scm_tc7_lsubr_2:
130 case scm_tc7_lsubr:
131 return scm_class_procedure;
132
133 case scm_tc7_port:
134 return scm_class_unknown;
135 case scm_tc7_smob:
136 {
137 SCM type = SCM_TYP16 (x);
138 if (type == scm_tc16_flo)
139 {
140 if (SCM_CAR (x) & SCM_IMAG_PART)
141 return scm_class_complex;
142 else
143 return scm_class_real;
144 }
145 else if (type == scm_tc16_bigpos || type == scm_tc16_bigneg)
146 return scm_class_integer;
147 else if (type == scm_tc16_kw)
148 return scm_class_keyword;
149 else
150 return scm_class_unknown;
151 }
152 case scm_tcs_cons_gloc:
153 /* must be a struct */
154 if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
155 {
156 if (CLASS_REDEF (SCM_CLASS_OF (x)) != SCM_BOOL_F)
157 scm_change_object_class (x,
158 SCM_CLASS_OF (x),
159 CLASS_REDEF (SCM_CLASS_OF (x)));
160 return SCM_CLASS_OF (x);
161 }
162 default:
163 if (SCM_CONSP (x))
164 return scm_class_pair;
165 else
166 return scm_class_unknown;
167 }
168
169 case scm_tc3_cons_gloc:
170 case scm_tc3_tc7_1:
171 case scm_tc3_tc7_2:
172 case scm_tc3_closure:
173 /* Never reached */
174 break;
175 }
176 return scm_class_unknown;
177 }
178
179 SCM_PROC (s_entity_p, "entity?", 1, 0, 0, scm_entity_p);
180
181 SCM
182 scm_entity_p (SCM obj)
183 {
184 return (SCM_NIMP (obj) && SCM_STRUCTP (obj) && SCM_I_ENTITYP (obj)
185 ? SCM_BOOL_T
186 : SCM_BOOL_F);
187 }
188
189 SCM_PROC (s_set_object_procedure_x, "set-object-procedure!", 1, 0, 1, scm_set_object_procedure_x);
190
191 SCM
192 scm_set_object_procedure_x (SCM obj, SCM procs)
193 {
194 SCM proc[4], *pp, p, setp, arity;
195 int i, a, r;
196 SCM_ASSERT (SCM_NIMP (obj) && SCM_STRUCTP (obj)
197 && ((SCM_CLASS_FLAGS (obj) & SCM_CLASSF_OPERATOR)
198 || SCM_I_ENTITYP (obj)),
199 obj,
200 SCM_ARG1,
201 s_set_object_procedure_x);
202 for (i = 0; i < 4; ++i)
203 proc[i] = SCM_BOOL_F;
204 i = 0;
205 while (SCM_NIMP (procs))
206 {
207 if (i == 4)
208 scm_wrong_num_args (scm_makfrom0str (s_set_object_procedure_x));
209 p = SCM_CAR (procs);
210 setp = 0;
211 SCM_ASSERT (SCM_NIMP (p), p, SCM_ARG2 + i, s_set_object_procedure_x);
212 if (SCM_CLOSUREP (p))
213 {
214 arity = scm_procedure_property (p, scm_sym_arity);
215 a = SCM_INUM (SCM_CAR (arity));
216 /* Closures have zero optional args */
217 r = SCM_NFALSEP (SCM_CADDR (arity));
218 if (a == 1 || (a <= 1 && r))
219 {
220 if (SCM_NFALSEP (proc[0]))
221 goto ambiguous;
222 proc[0] = setp = p;
223 }
224 if (a == 2 || (a <= 2 && r))
225 {
226 if (SCM_NFALSEP (proc[1]))
227 goto ambiguous;
228 proc[1] = setp = p;
229 }
230 if (a == 3 || (a <= 3 && r))
231 {
232 if (SCM_NFALSEP (proc[2]))
233 goto ambiguous;
234 proc[2] = setp = p;
235 }
236 if (a <= 4 && r)
237 {
238 if (SCM_NFALSEP (proc[3]))
239 goto ambiguous;
240 proc[3] = setp = p;
241 }
242 }
243 else if (SCM_TYP7 (p) == scm_tc7_subr_1)
244 {
245 if (SCM_NFALSEP (proc[0]))
246 goto ambiguous;
247 proc[0] = setp = p;
248 }
249 else if (SCM_TYP7 (p) == scm_tc7_subr_2)
250 {
251 if (SCM_NFALSEP (proc[1]))
252 goto ambiguous;
253 proc[1] = setp = p;
254 }
255 else if (SCM_TYP7 (p) == scm_tc7_subr_3)
256 {
257 if (SCM_NFALSEP (proc[2]))
258 goto ambiguous;
259 proc[2] = setp = p;
260 }
261 else if (SCM_TYP7 (p) == scm_tc7_lsubr_2)
262 {
263 if (SCM_NFALSEP (proc[3]))
264 {
265 ambiguous:
266 SCM_ASSERT (0, p, "Ambiguous procedure arities",
267 s_set_object_procedure_x);
268 }
269 proc[3] = setp = p;
270 }
271 SCM_ASSERT (setp, p, SCM_ARG2 + i, s_set_object_procedure_x);
272 ++i;
273 procs = SCM_CDR (procs);
274 }
275 pp = (SCM_I_ENTITYP (obj)
276 ? &SCM_ENTITY_PROC_0 (obj)
277 : &SCM_OPERATOR_CLASS (obj)->proc0);
278 for (i = 0; i < 4; ++i)
279 *pp++ = proc[i];
280 return SCM_UNSPECIFIED;
281 }
282
283 SCM
284 scm_i_make_class_object (SCM meta,
285 SCM layout_string,
286 unsigned long flags)
287 {
288 SCM c;
289 SCM layout = scm_make_struct_layout (layout_string);
290 c = scm_make_struct (meta,
291 SCM_INUM0,
292 SCM_LIST4 (layout, SCM_BOOL_F, SCM_EOL, SCM_EOL));
293 SCM_SET_CLASS_FLAGS (c, flags);
294 return c;
295 }
296
297 SCM_PROC (s_make_class_object, "make-class-object", 2, 0, 0, scm_make_class_object);
298
299 SCM
300 scm_make_class_object (SCM metaclass, SCM layout)
301 {
302 unsigned long flags = 0;
303 SCM_ASSERT (SCM_NIMP (metaclass) && SCM_STRUCTP (metaclass),
304 metaclass, SCM_ARG1, s_make_class_object);
305 SCM_ASSERT (SCM_NIMP (layout) && SCM_STRINGP (layout),
306 layout, SCM_ARG2, s_make_class_object);
307 if (metaclass == scm_metaclass_operator)
308 flags = SCM_CLASSF_OPERATOR;
309 return scm_i_make_class_object (metaclass, layout, flags);
310 }
311
312 SCM_PROC (s_make_subclass_object, "make-subclass-object", 2, 0, 0, scm_make_subclass_object);
313
314 SCM
315 scm_make_subclass_object (SCM class, SCM layout)
316 {
317 SCM pl;
318 SCM_ASSERT (SCM_NIMP (class) && SCM_STRUCTP (class),
319 class,
320 SCM_ARG1,
321 s_make_subclass_object);
322 SCM_ASSERT (SCM_NIMP (layout) && SCM_STRINGP (layout),
323 layout,
324 SCM_ARG2,
325 s_make_subclass_object);
326 pl = SCM_STRUCT_DATA (class)[scm_vtable_index_layout];
327 /* Convert symbol->string */
328 pl = scm_makfromstr (SCM_CHARS (pl), (scm_sizet) SCM_LENGTH (pl), 0);
329 return scm_i_make_class_object (SCM_STRUCT_VTABLE (class),
330 scm_string_append (SCM_LIST2 (pl, layout)),
331 SCM_CLASS_FLAGS (class));
332 }
333
334 void
335 scm_init_objects ()
336 {
337 SCM ms = scm_makfrom0str (SCM_METACLASS_STANDARD_LAYOUT);
338 SCM ml = scm_make_struct_layout (ms);
339 SCM mt = scm_make_vtable_vtable (ml, SCM_INUM0,
340 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
341
342 SCM os = scm_makfrom0str (SCM_METACLASS_OPERATOR_LAYOUT);
343 SCM ol = scm_make_struct_layout (os);
344 SCM ot = scm_make_vtable_vtable (ol, SCM_INUM0,
345 SCM_LIST3 (SCM_BOOL_F, SCM_EOL, SCM_EOL));
346
347 SCM es = scm_makfrom0str (SCM_ENTITY_LAYOUT);
348 SCM el = scm_make_struct_layout (es);
349 SCM et = scm_make_struct (mt, SCM_INUM0,
350 SCM_LIST4 (el, SCM_BOOL_F, SCM_EOL, SCM_EOL));
351
352 scm_sysintern ("<class>", mt);
353 scm_metaclass_standard = mt;
354 scm_sysintern ("<operator-class>", ot);
355 scm_metaclass_operator = ot;
356 SCM_SET_CLASS_FLAGS (et, SCM_CLASSF_OPERATOR | SCM_CLASSF_ENTITY);
357 scm_sysintern ("<entity>", et);
358
359 #include "objects.x"
360 }