Commit | Line | Data |
---|---|---|
aa42c036 | 1 | /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
dbb605f5 | 20 | #ifdef HAVE_CONFIG_H |
a6f7f57d RB |
21 | # include <config.h> |
22 | #endif | |
0f2d19dd | 23 | |
66e78727 | 24 | #include <alloca.h> |
aa42c036 | 25 | #include <assert.h> |
66e78727 | 26 | |
a0599745 | 27 | #include "libguile/_scm.h" |
4e047c3e | 28 | #include "libguile/async.h" |
a0599745 MD |
29 | #include "libguile/chars.h" |
30 | #include "libguile/eval.h" | |
31 | #include "libguile/alist.h" | |
32 | #include "libguile/weaks.h" | |
33 | #include "libguile/hashtab.h" | |
34 | #include "libguile/ports.h" | |
35 | #include "libguile/strings.h" | |
27646f41 | 36 | #include "libguile/srfi-13.h" |
a0599745 MD |
37 | |
38 | #include "libguile/validate.h" | |
39 | #include "libguile/struct.h" | |
0f2d19dd | 40 | |
d15ad007 LC |
41 | #include "libguile/eq.h" |
42 | ||
95b88819 GH |
43 | #ifdef HAVE_STRING_H |
44 | #include <string.h> | |
45 | #endif | |
46 | ||
1c44468d | 47 | #include "libguile/bdw-gc.h" |
5e67dc27 | 48 | |
0f2d19dd JB |
49 | \f |
50 | ||
b6cf4d02 AW |
51 | /* A needlessly obscure test. */ |
52 | #define SCM_LAYOUT_TAILP(X) (((X) & 32) == 0) /* R, W or O */ | |
53 | ||
0f2d19dd | 54 | static SCM required_vtable_fields = SCM_BOOL_F; |
b6cf4d02 AW |
55 | static SCM required_applicable_fields = SCM_BOOL_F; |
56 | static SCM required_applicable_with_setter_fields = SCM_BOOL_F; | |
57 | SCM scm_struct_table = SCM_BOOL_F; | |
db5ed685 AW |
58 | SCM scm_applicable_struct_vtable_vtable; |
59 | SCM scm_applicable_struct_with_setter_vtable_vtable; | |
60 | SCM scm_standard_vtable_vtable; | |
61 | ||
0f2d19dd JB |
62 | |
63 | \f | |
a1ec6916 | 64 | SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0, |
1bbd0b84 | 65 | (SCM fields), |
b380b885 | 66 | "Return a new structure layout object.\n\n" |
7c31152f | 67 | "@var{fields} must be a string made up of pairs of characters\n" |
b380b885 MD |
68 | "strung together. The first character of each pair describes a field\n" |
69 | "type, the second a field protection. Allowed types are 'p' for\n" | |
70 | "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n" | |
04323af4 | 71 | "a field that points to the structure itself. Allowed protections\n" |
b6cf4d02 AW |
72 | "are 'w' for mutable fields, 'h' for hidden fields, 'r' for read-only\n" |
73 | "fields, and 'o' for opaque fields.\n\n" | |
74 | "Hidden fields are writable, but they will not consume an initializer arg\n" | |
75 | "passed to @code{make-struct}. They are useful to add slots to a struct\n" | |
76 | "in a way that preserves backward-compatibility with existing calls to\n" | |
77 | "@code{make-struct}, especially for derived vtables.\n\n" | |
78 | "The last field protection specification may be capitalized to indicate\n" | |
79 | "that the field is a tail-array.") | |
1bbd0b84 | 80 | #define FUNC_NAME s_scm_make_struct_layout |
0f2d19dd JB |
81 | { |
82 | SCM new_sym; | |
27646f41 | 83 | scm_t_wchar c; |
2ade72d7 | 84 | |
7f991c7d LC |
85 | SCM_VALIDATE_STRING (1, fields); |
86 | ||
1bbd0b84 | 87 | { /* scope */ |
1be6b49c | 88 | size_t len; |
0f2d19dd JB |
89 | int x; |
90 | ||
cc95e00a | 91 | len = scm_i_string_length (fields); |
2ade72d7 DH |
92 | if (len % 2 == 1) |
93 | SCM_MISC_ERROR ("odd length field specification: ~S", | |
1afff620 | 94 | scm_list_1 (fields)); |
2ade72d7 | 95 | |
0f2d19dd JB |
96 | for (x = 0; x < len; x += 2) |
97 | { | |
27646f41 | 98 | switch (c = scm_i_string_ref (fields, x)) |
0f2d19dd JB |
99 | { |
100 | case 'u': | |
101 | case 'p': | |
102 | #if 0 | |
103 | case 'i': | |
104 | case 'd': | |
105 | #endif | |
106 | case 's': | |
107 | break; | |
108 | default: | |
2ade72d7 | 109 | SCM_MISC_ERROR ("unrecognized field type: ~S", |
27646f41 | 110 | scm_list_1 (SCM_MAKE_CHAR (c))); |
0f2d19dd JB |
111 | } |
112 | ||
27646f41 | 113 | switch (c = scm_i_string_ref (fields, x + 1)) |
0f2d19dd JB |
114 | { |
115 | case 'w': | |
b6cf4d02 | 116 | case 'h': |
27646f41 | 117 | if (scm_i_string_ref (fields, x) == 's') |
2ade72d7 | 118 | SCM_MISC_ERROR ("self fields not writable", SCM_EOL); |
0f2d19dd JB |
119 | case 'r': |
120 | case 'o': | |
121 | break; | |
2c36c351 MD |
122 | case 'R': |
123 | case 'W': | |
124 | case 'O': | |
27646f41 | 125 | if (scm_i_string_ref (fields, x) == 's') |
2ade72d7 DH |
126 | SCM_MISC_ERROR ("self fields not allowed in tail array", |
127 | SCM_EOL); | |
128 | if (x != len - 2) | |
129 | SCM_MISC_ERROR ("tail array field must be last field in layout", | |
130 | SCM_EOL); | |
2c36c351 | 131 | break; |
0f2d19dd | 132 | default: |
2ade72d7 | 133 | SCM_MISC_ERROR ("unrecognized ref specification: ~S", |
27646f41 | 134 | scm_list_1 (SCM_MAKE_CHAR (c))); |
0f2d19dd JB |
135 | } |
136 | #if 0 | |
27646f41 | 137 | if (scm_i_string_ref (fields, x, 'd')) |
0f2d19dd | 138 | { |
27646f41 | 139 | if (!scm_i_string_ref (fields, x+2, '-')) |
2ade72d7 | 140 | SCM_MISC_ERROR ("missing dash field at position ~A", |
e11e83f3 | 141 | scm_list_1 (scm_from_int (x / 2))); |
0f2d19dd JB |
142 | x += 2; |
143 | goto recheck_ref; | |
144 | } | |
145 | #endif | |
146 | } | |
cc95e00a | 147 | new_sym = scm_string_to_symbol (fields); |
0f2d19dd | 148 | } |
8824ac88 MV |
149 | scm_remember_upto_here_1 (fields); |
150 | return new_sym; | |
0f2d19dd | 151 | } |
1bbd0b84 | 152 | #undef FUNC_NAME |
0f2d19dd JB |
153 | |
154 | \f | |
aa42c036 LC |
155 | /* Check whether VTABLE instances have a simple layout (i.e., either only "pr" |
156 | or only "pw" fields) and update its flags accordingly. */ | |
157 | static void | |
158 | set_vtable_layout_flags (SCM vtable) | |
159 | { | |
160 | size_t len, field; | |
161 | SCM layout; | |
162 | const char *c_layout; | |
163 | scm_t_bits flags = SCM_VTABLE_FLAG_SIMPLE; | |
164 | ||
165 | layout = SCM_VTABLE_LAYOUT (vtable); | |
166 | c_layout = scm_i_symbol_chars (layout); | |
167 | len = scm_i_symbol_length (layout); | |
168 | ||
169 | assert (len % 2 == 0); | |
170 | ||
171 | /* Update FLAGS according to LAYOUT. */ | |
172 | for (field = 0; | |
173 | field < len && flags & SCM_VTABLE_FLAG_SIMPLE; | |
174 | field += 2) | |
175 | { | |
176 | if (c_layout[field] != 'p') | |
177 | flags = 0; | |
178 | else | |
179 | switch (c_layout[field + 1]) | |
180 | { | |
181 | case 'w': | |
182 | case 'W': | |
e03b7f73 | 183 | if (field == 0) |
aa42c036 LC |
184 | flags |= SCM_VTABLE_FLAG_SIMPLE_RW; |
185 | break; | |
186 | ||
187 | case 'r': | |
188 | case 'R': | |
e03b7f73 | 189 | flags &= ~SCM_VTABLE_FLAG_SIMPLE_RW; |
aa42c036 LC |
190 | break; |
191 | ||
192 | default: | |
193 | flags = 0; | |
194 | } | |
195 | } | |
196 | ||
197 | if (flags & SCM_VTABLE_FLAG_SIMPLE) | |
198 | { | |
199 | /* VTABLE is simple so update its flags and record the size of its | |
200 | instances. */ | |
201 | SCM_SET_VTABLE_FLAGS (vtable, flags); | |
202 | SCM_STRUCT_DATA_SET (vtable, scm_vtable_index_size, len / 2); | |
203 | } | |
204 | } | |
0f2d19dd | 205 | |
631237b4 AW |
206 | static int |
207 | scm_is_valid_vtable_layout (SCM layout) | |
208 | { | |
209 | size_t len, n; | |
210 | const char *c_layout; | |
211 | ||
212 | c_layout = scm_i_symbol_chars (layout); | |
213 | len = scm_i_symbol_length (layout); | |
214 | ||
215 | if (len % 2) | |
216 | return 0; | |
217 | ||
218 | for (n = 0; n < len; n += 2) | |
219 | switch (c_layout[n]) | |
220 | { | |
221 | case 'u': | |
222 | case 'p': | |
223 | case 's': | |
224 | switch (c_layout[n+1]) | |
225 | { | |
226 | case 'W': | |
227 | case 'R': | |
228 | case 'O': | |
229 | if (n + 2 != len) | |
230 | return 0; | |
231 | case 'w': | |
232 | case 'h': | |
233 | case 'r': | |
234 | case 'o': | |
235 | break; | |
236 | default: | |
237 | return 0; | |
238 | } | |
239 | break; | |
240 | default: | |
241 | return 0; | |
242 | } | |
243 | return 1; | |
244 | } | |
245 | ||
696ac4df LC |
246 | /* Have OBJ, a newly created vtable, inherit flags from VTABLE. VTABLE is a |
247 | vtable-vtable and OBJ is an instance of VTABLE. */ | |
51f66c91 AW |
248 | void |
249 | scm_i_struct_inherit_vtable_magic (SCM vtable, SCM obj) | |
250 | #define FUNC_NAME "%inherit-vtable-magic" | |
251 | { | |
252 | /* Verily, what is the deal here, you ask? Basically, we need to know a couple | |
253 | of properties of structures at runtime. For example, "is this structure a | |
254 | vtable of vtables (a metaclass)?"; also, "is this structure applicable?". | |
255 | Both of these questions also imply a certain layout of the structure. So | |
256 | instead of checking the layout at runtime, what we do is pre-verify the | |
257 | layout -- so that at runtime we can just check the applicable flag and | |
696ac4df | 258 | dispatch directly to the Scheme procedure in slot 0. */ |
51f66c91 AW |
259 | SCM olayout; |
260 | ||
696ac4df | 261 | /* Verify that OBJ is a valid vtable. */ |
631237b4 | 262 | if (! scm_is_valid_vtable_layout (SCM_VTABLE_LAYOUT (obj))) |
a2220d7e | 263 | SCM_MISC_ERROR ("invalid layout for new vtable: ~a", |
51f66c91 AW |
264 | scm_list_1 (SCM_VTABLE_LAYOUT (obj))); |
265 | ||
aa42c036 LC |
266 | set_vtable_layout_flags (obj); |
267 | ||
696ac4df LC |
268 | /* If OBJ's vtable is compatible with the required vtable (class) layout, it |
269 | is a metaclass. */ | |
51f66c91 AW |
270 | olayout = scm_symbol_to_string (SCM_VTABLE_LAYOUT (obj)); |
271 | if (scm_is_true (scm_leq_p (scm_string_length (required_vtable_fields), | |
272 | scm_string_length (olayout))) | |
273 | && scm_is_true (scm_string_eq (olayout, required_vtable_fields, | |
274 | scm_from_size_t (0), | |
275 | scm_string_length (required_vtable_fields), | |
276 | scm_from_size_t (0), | |
277 | scm_string_length (required_vtable_fields)))) | |
278 | SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VTABLE); | |
279 | ||
696ac4df LC |
280 | /* Finally, if OBJ is an applicable class, verify that its vtable is |
281 | compatible with the required applicable layout. */ | |
51f66c91 AW |
282 | if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SETTER_VTABLE)) |
283 | { | |
284 | if (scm_is_false (scm_string_eq (olayout, required_applicable_with_setter_fields, | |
285 | scm_from_size_t (0), | |
286 | scm_from_size_t (4), | |
287 | scm_from_size_t (0), | |
288 | scm_from_size_t (4)))) | |
a2220d7e | 289 | SCM_MISC_ERROR ("invalid applicable-with-setter struct layout", |
51f66c91 AW |
290 | scm_list_1 (olayout)); |
291 | SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE | SCM_VTABLE_FLAG_SETTER); | |
292 | } | |
293 | else if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_APPLICABLE_VTABLE)) | |
294 | { | |
295 | if (scm_is_false (scm_string_eq (olayout, required_applicable_fields, | |
296 | scm_from_size_t (0), | |
297 | scm_from_size_t (2), | |
298 | scm_from_size_t (0), | |
299 | scm_from_size_t (2)))) | |
a2220d7e | 300 | SCM_MISC_ERROR ("invalid applicable struct layout", |
51f66c91 AW |
301 | scm_list_1 (olayout)); |
302 | SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE); | |
303 | } | |
a2220d7e AW |
304 | |
305 | SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VALIDATED); | |
51f66c91 AW |
306 | } |
307 | #undef FUNC_NAME | |
0f2d19dd | 308 | |
1cc91f1b | 309 | |
f7620510 | 310 | static void |
66e78727 AW |
311 | scm_struct_init (SCM handle, SCM layout, size_t n_tail, |
312 | size_t n_inits, scm_t_bits *inits) | |
0f2d19dd | 313 | { |
aa42c036 LC |
314 | SCM vtable; |
315 | scm_t_bits *mem; | |
316 | ||
317 | vtable = SCM_STRUCT_VTABLE (handle); | |
318 | mem = SCM_STRUCT_DATA (handle); | |
319 | ||
320 | if (SCM_UNPACK (vtable) != 0 | |
321 | && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE) | |
322 | && n_tail == 0 | |
323 | && n_inits == SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)) | |
324 | /* The fast path: HANDLE has N_INITS "p" fields. */ | |
325 | memcpy (mem, inits, n_inits * sizeof (SCM)); | |
326 | else | |
0f2d19dd | 327 | { |
aa42c036 LC |
328 | scm_t_wchar prot = 0; |
329 | int n_fields = scm_i_symbol_length (layout) / 2; | |
330 | int tailp = 0; | |
331 | int i; | |
332 | size_t inits_idx = 0; | |
333 | ||
334 | i = -2; | |
335 | while (n_fields) | |
2c36c351 | 336 | { |
aa42c036 | 337 | if (!tailp) |
2c36c351 | 338 | { |
aa42c036 LC |
339 | i += 2; |
340 | prot = scm_i_symbol_ref (layout, i+1); | |
341 | if (SCM_LAYOUT_TAILP (prot)) | |
342 | { | |
343 | tailp = 1; | |
344 | prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o'; | |
345 | *mem++ = (scm_t_bits)n_tail; | |
346 | n_fields += n_tail - 1; | |
347 | if (n_fields == 0) | |
348 | break; | |
349 | } | |
2c36c351 | 350 | } |
aa42c036 | 351 | switch (scm_i_symbol_ref (layout, i)) |
0f2d19dd | 352 | { |
aa42c036 LC |
353 | case 'u': |
354 | if ((prot != 'r' && prot != 'w') || inits_idx == n_inits) | |
355 | *mem = 0; | |
356 | else | |
357 | { | |
358 | *mem = scm_to_ulong (SCM_PACK (inits[inits_idx])); | |
359 | inits_idx++; | |
360 | } | |
361 | break; | |
362 | ||
363 | case 'p': | |
364 | if ((prot != 'r' && prot != 'w') || inits_idx == n_inits) | |
365 | *mem = SCM_UNPACK (SCM_BOOL_F); | |
366 | else | |
367 | { | |
368 | *mem = inits[inits_idx]; | |
369 | inits_idx++; | |
370 | } | |
371 | ||
372 | break; | |
373 | ||
374 | case 's': | |
375 | *mem = SCM_UNPACK (handle); | |
376 | break; | |
0f2d19dd | 377 | } |
0f2d19dd | 378 | |
aa42c036 LC |
379 | n_fields--; |
380 | mem++; | |
0f2d19dd | 381 | } |
0f2d19dd JB |
382 | } |
383 | } | |
384 | ||
385 | ||
a1ec6916 | 386 | SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0, |
1bbd0b84 | 387 | (SCM x), |
0233bfc1 | 388 | "Return @code{#t} iff @var{x} is a structure object, else\n" |
942e5b91 | 389 | "@code{#f}.") |
1bbd0b84 | 390 | #define FUNC_NAME s_scm_struct_p |
0f2d19dd | 391 | { |
7888309b | 392 | return scm_from_bool(SCM_STRUCTP (x)); |
0f2d19dd | 393 | } |
1bbd0b84 | 394 | #undef FUNC_NAME |
0f2d19dd | 395 | |
a1ec6916 | 396 | SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0, |
1bbd0b84 | 397 | (SCM x), |
0233bfc1 | 398 | "Return @code{#t} iff @var{x} is a vtable structure.") |
1bbd0b84 | 399 | #define FUNC_NAME s_scm_struct_vtable_p |
0f2d19dd | 400 | { |
a2220d7e AW |
401 | if (!SCM_STRUCTP (x) |
402 | || !SCM_STRUCT_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VTABLE)) | |
403 | return SCM_BOOL_F; | |
404 | if (!SCM_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VALIDATED)) | |
405 | SCM_MISC_ERROR ("vtable has invalid layout: ~A", | |
406 | scm_list_1 (SCM_VTABLE_LAYOUT (x))); | |
407 | return SCM_BOOL_T; | |
0f2d19dd | 408 | } |
1bbd0b84 | 409 | #undef FUNC_NAME |
0f2d19dd | 410 | |
14d1400f | 411 | |
51f66c91 AW |
412 | /* Finalization: invoke the finalizer of the struct pointed to by PTR. */ |
413 | static void | |
414 | struct_finalizer_trampoline (GC_PTR ptr, GC_PTR unused_data) | |
415 | { | |
416 | SCM obj = PTR2SCM (ptr); | |
417 | scm_t_struct_finalize finalize = SCM_STRUCT_FINALIZER (obj); | |
418 | ||
419 | if (finalize) | |
420 | finalize (obj); | |
421 | } | |
422 | ||
14d1400f JB |
423 | /* All struct data must be allocated at an address whose bottom three |
424 | bits are zero. This is because the tag for a struct lives in the | |
425 | bottom three bits of the struct's car, and the upper bits point to | |
426 | the data of its vtable, which is a struct itself. Thus, if the | |
427 | address of that data doesn't end in three zeros, tagging it will | |
428 | destroy the pointer. | |
429 | ||
b6cf4d02 AW |
430 | I suppose we should make it clear here that, the data must be 8-byte aligned, |
431 | *within* the struct, and the struct itself should be 8-byte aligned. In | |
432 | practice we ensure this because the data starts two words into a struct. | |
14d1400f | 433 | |
b6cf4d02 AW |
434 | This function allocates an 8-byte aligned block of memory, whose first word |
435 | points to the given vtable data, then a data pointer, then n_words of data. | |
436 | */ | |
437 | SCM | |
96a44c1c | 438 | scm_i_alloc_struct (scm_t_bits *vtable_data, int n_words) |
14d1400f | 439 | { |
9a974fd3 AW |
440 | SCM ret; |
441 | ||
442 | ret = scm_words ((scm_t_bits)vtable_data | scm_tc3_struct, n_words + 2); | |
443 | SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)SCM_CELL_OBJECT_LOC (ret, 2)); | |
5e67dc27 | 444 | |
51f66c91 AW |
445 | /* vtable_data can be null when making a vtable vtable */ |
446 | if (vtable_data && vtable_data[scm_vtable_index_instance_finalize]) | |
447 | { | |
448 | /* Register a finalizer for the newly created instance. */ | |
449 | GC_finalization_proc prev_finalizer; | |
450 | GC_PTR prev_finalizer_data; | |
9a974fd3 | 451 | GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret), |
51f66c91 AW |
452 | struct_finalizer_trampoline, |
453 | NULL, | |
454 | &prev_finalizer, | |
455 | &prev_finalizer_data); | |
456 | } | |
5e67dc27 | 457 | |
9a974fd3 | 458 | return ret; |
5e67dc27 LC |
459 | } |
460 | ||
5e67dc27 | 461 | \f |
66e78727 AW |
462 | SCM |
463 | scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits *init) | |
464 | #define FUNC_NAME "make-struct" | |
0f2d19dd JB |
465 | { |
466 | SCM layout; | |
a55c2b68 | 467 | size_t basic_size; |
b6cf4d02 | 468 | SCM obj; |
0f2d19dd | 469 | |
34d19ef6 | 470 | SCM_VALIDATE_VTABLE (1, vtable); |
0f2d19dd | 471 | |
b6cf4d02 | 472 | layout = SCM_VTABLE_LAYOUT (vtable); |
cc95e00a | 473 | basic_size = scm_i_symbol_length (layout) / 2; |
651f2cd2 | 474 | |
66e78727 | 475 | if (n_tail != 0) |
651f2cd2 KR |
476 | { |
477 | SCM layout_str, last_char; | |
478 | ||
479 | if (basic_size == 0) | |
480 | { | |
481 | bad_tail: | |
482 | SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL); | |
483 | } | |
484 | ||
485 | layout_str = scm_symbol_to_string (layout); | |
486 | last_char = scm_string_ref (layout_str, | |
487 | scm_from_size_t (2 * basic_size - 1)); | |
488 | if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char))) | |
489 | goto bad_tail; | |
490 | } | |
cb823e63 | 491 | |
96a44c1c | 492 | obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), basic_size + n_tail); |
5e67dc27 | 493 | |
66e78727 | 494 | scm_struct_init (obj, layout, n_tail, n_init, init); |
b6cf4d02 | 495 | |
a2220d7e AW |
496 | /* If we're making a vtable, validate its layout and inherit |
497 | flags. However we allow for separation of allocation and | |
498 | initialization, to humor GOOPS, so only validate if the layout was | |
499 | passed as an initarg. */ | |
b6cf4d02 | 500 | if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE) |
b6cf4d02 | 501 | && scm_is_true (SCM_VTABLE_LAYOUT (obj))) |
51f66c91 | 502 | scm_i_struct_inherit_vtable_magic (vtable, obj); |
651f2cd2 | 503 | |
b6cf4d02 | 504 | return obj; |
0f2d19dd | 505 | } |
1bbd0b84 | 506 | #undef FUNC_NAME |
0f2d19dd | 507 | |
66e78727 AW |
508 | SCM |
509 | scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits init, ...) | |
510 | { | |
511 | va_list foo; | |
512 | scm_t_bits *v; | |
513 | size_t i; | |
514 | ||
515 | v = alloca (sizeof (scm_t_bits) * n_init); | |
516 | ||
517 | va_start (foo, init); | |
518 | for (i = 0; i < n_init; i++) | |
519 | { | |
520 | v[i] = init; | |
521 | init = va_arg (foo, scm_t_bits); | |
522 | } | |
523 | va_end (foo); | |
524 | ||
525 | return scm_c_make_structv (vtable, n_tail, n_init, v); | |
526 | } | |
527 | ||
528 | SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1, | |
529 | (SCM vtable, SCM tail_array_size, SCM init), | |
530 | "Create a new structure.\n\n" | |
531 | "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n" | |
532 | "@var{tail-elts} must be a non-negative integer. If the layout\n" | |
533 | "specification indicated by @var{type} includes a tail-array,\n" | |
534 | "this is the number of elements allocated to that array.\n\n" | |
535 | "The @var{init1}, @dots{} are optional arguments describing how\n" | |
536 | "successive fields of the structure should be initialized. Only fields\n" | |
537 | "with protection 'r' or 'w' can be initialized, except for fields of\n" | |
538 | "type 's', which are automatically initialized to point to the new\n" | |
539 | "structure itself. Fields with protection 'o' can not be initialized by\n" | |
540 | "Scheme programs.\n\n" | |
541 | "If fewer optional arguments than initializable fields are supplied,\n" | |
542 | "fields of type 'p' get default value #f while fields of type 'u' are\n" | |
543 | "initialized to 0.\n\n" | |
544 | "For more information, see the documentation for @code{make-vtable-vtable}.") | |
545 | #define FUNC_NAME s_scm_make_struct | |
546 | { | |
547 | size_t i, n_init; | |
548 | long ilen; | |
549 | scm_t_bits *v; | |
550 | ||
551 | SCM_VALIDATE_VTABLE (1, vtable); | |
552 | ilen = scm_ilength (init); | |
553 | if (ilen < 0) | |
554 | SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); | |
555 | ||
556 | n_init = (size_t)ilen; | |
557 | ||
558 | /* best to use alloca, but init could be big, so hack to avoid a possible | |
559 | stack overflow */ | |
560 | if (n_init < 64) | |
561 | v = alloca (n_init * sizeof(scm_t_bits)); | |
562 | else | |
563 | v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct"); | |
564 | ||
565 | for (i = 0; i < n_init; i++, init = SCM_CDR (init)) | |
566 | v[i] = SCM_UNPACK (SCM_CAR (init)); | |
567 | ||
568 | return scm_c_make_structv (vtable, scm_to_size_t (tail_array_size), n_init, v); | |
569 | } | |
570 | #undef FUNC_NAME | |
571 | ||
0f2d19dd JB |
572 | |
573 | ||
a1ec6916 | 574 | SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1, |
04323af4 | 575 | (SCM user_fields, SCM tail_array_size, SCM init), |
b380b885 | 576 | "Return a new, self-describing vtable structure.\n\n" |
04323af4 MD |
577 | "@var{user-fields} is a string describing user defined fields of the\n" |
578 | "vtable beginning at index @code{vtable-offset-user}\n" | |
579 | "(see @code{make-struct-layout}).\n\n" | |
b380b885 MD |
580 | "@var{tail-size} specifies the size of the tail-array (if any) of\n" |
581 | "this vtable.\n\n" | |
6386e25c | 582 | "@var{init1}, @dots{} are the optional initializers for the fields of\n" |
04323af4 MD |
583 | "the vtable.\n\n" |
584 | "Vtables have one initializable system field---the struct printer.\n" | |
585 | "This field comes before the user fields in the initializers passed\n" | |
586 | "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n" | |
587 | "a third optional argument to @code{make-vtable-vtable} and a fourth to\n" | |
588 | "@code{make-struct} when creating vtables:\n\n" | |
589 | "If the value is a procedure, it will be called instead of the standard\n" | |
590 | "printer whenever a struct described by this vtable is printed.\n" | |
591 | "The procedure will be called with arguments STRUCT and PORT.\n\n" | |
592 | "The structure of a struct is described by a vtable, so the vtable is\n" | |
593 | "in essence the type of the struct. The vtable is itself a struct with\n" | |
594 | "a vtable. This could go on forever if it weren't for the\n" | |
29b4f9fb | 595 | "vtable-vtables which are self-describing vtables, and thus terminate\n" |
04323af4 MD |
596 | "the chain.\n\n" |
597 | "There are several potential ways of using structs, but the standard\n" | |
598 | "one is to use three kinds of structs, together building up a type\n" | |
599 | "sub-system: one vtable-vtable working as the root and one or several\n" | |
600 | "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n" | |
29b4f9fb | 601 | "compared to the class <class> which is the class of itself.)\n\n" |
1e6808ea | 602 | "@lisp\n" |
04323af4 MD |
603 | "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n" |
604 | "(define (make-ball-type ball-color)\n" | |
605 | " (make-struct ball-root 0\n" | |
606 | " (make-struct-layout \"pw\")\n" | |
607 | " (lambda (ball port)\n" | |
608 | " (format port \"#<a ~A ball owned by ~A>\"\n" | |
609 | " (color ball)\n" | |
610 | " (owner ball)))\n" | |
611 | " ball-color))\n" | |
612 | "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n" | |
613 | "(define (owner ball) (struct-ref ball 0))\n\n" | |
614 | "(define red (make-ball-type 'red))\n" | |
615 | "(define green (make-ball-type 'green))\n\n" | |
616 | "(define (make-ball type owner) (make-struct type 0 owner))\n\n" | |
617 | "(define ball (make-ball green 'Nisse))\n" | |
618 | "ball @result{} #<a green ball owned by Nisse>\n" | |
9401323e | 619 | "@end lisp") |
1bbd0b84 | 620 | #define FUNC_NAME s_scm_make_vtable_vtable |
0f2d19dd | 621 | { |
696ac4df LC |
622 | SCM fields, layout, obj; |
623 | size_t basic_size, n_tail, i, n_init; | |
66e78727 AW |
624 | long ilen; |
625 | scm_t_bits *v; | |
0f2d19dd | 626 | |
d1ca2c64 | 627 | SCM_VALIDATE_STRING (1, user_fields); |
66e78727 AW |
628 | ilen = scm_ilength (init); |
629 | if (ilen < 0) | |
630 | SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); | |
631 | ||
632 | n_init = (size_t)ilen + 1; /* + 1 for the layout */ | |
633 | ||
634 | /* best to use alloca, but init could be big, so hack to avoid a possible | |
635 | stack overflow */ | |
636 | if (n_init < 64) | |
637 | v = alloca (n_init * sizeof(scm_t_bits)); | |
638 | else | |
639 | v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct"); | |
0f2d19dd | 640 | |
1afff620 KN |
641 | fields = scm_string_append (scm_list_2 (required_vtable_fields, |
642 | user_fields)); | |
0f2d19dd | 643 | layout = scm_make_struct_layout (fields); |
a2220d7e AW |
644 | if (!scm_is_valid_vtable_layout (layout)) |
645 | SCM_MISC_ERROR ("invalid user fields", scm_list_1 (user_fields)); | |
646 | ||
cc95e00a | 647 | basic_size = scm_i_symbol_length (layout) / 2; |
66e78727 AW |
648 | n_tail = scm_to_size_t (tail_array_size); |
649 | ||
650 | i = 0; | |
651 | v[i++] = SCM_UNPACK (layout); | |
652 | for (; i < n_init; i++, init = SCM_CDR (init)) | |
653 | v[i] = SCM_UNPACK (SCM_CAR (init)); | |
654 | ||
9de87eea | 655 | SCM_CRITICAL_SECTION_START; |
96a44c1c | 656 | obj = scm_i_alloc_struct (NULL, basic_size + n_tail); |
696ac4df LC |
657 | /* Make it so that the vtable of OBJ is itself. */ |
658 | SCM_SET_CELL_WORD_0 (obj, (scm_t_bits) SCM_STRUCT_DATA (obj) | scm_tc3_struct); | |
9de87eea | 659 | SCM_CRITICAL_SECTION_END; |
696ac4df | 660 | |
66e78727 | 661 | scm_struct_init (obj, layout, n_tail, n_init, v); |
a2220d7e AW |
662 | SCM_SET_VTABLE_FLAGS (obj, |
663 | SCM_VTABLE_FLAG_VTABLE | SCM_VTABLE_FLAG_VALIDATED); | |
696ac4df | 664 | |
b6cf4d02 | 665 | return obj; |
0f2d19dd | 666 | } |
1bbd0b84 | 667 | #undef FUNC_NAME |
0f2d19dd | 668 | |
d15ad007 | 669 | |
651f2cd2 KR |
670 | SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0, |
671 | (SCM fields, SCM printer), | |
672 | "Create a vtable, for creating structures with the given\n" | |
673 | "@var{fields}.\n" | |
674 | "\n" | |
675 | "The optional @var{printer} argument is a function to be called\n" | |
676 | "@code{(@var{printer} struct port)} on the structures created.\n" | |
677 | "It should look at @var{struct} and write to @var{port}.") | |
678 | #define FUNC_NAME s_scm_make_vtable | |
679 | { | |
680 | if (SCM_UNBNDP (printer)) | |
681 | printer = SCM_BOOL_F; | |
682 | ||
db5ed685 | 683 | return scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0, |
651f2cd2 KR |
684 | scm_list_2 (scm_make_struct_layout (fields), |
685 | printer)); | |
686 | } | |
687 | #undef FUNC_NAME | |
688 | ||
689 | ||
d15ad007 LC |
690 | /* Return true if S1 and S2 are equal structures, i.e., if their vtable and |
691 | contents are the same. Field protections are honored. Thus, it is an | |
692 | error to test the equality of structures that contain opaque fields. */ | |
693 | SCM | |
694 | scm_i_struct_equalp (SCM s1, SCM s2) | |
695 | #define FUNC_NAME "scm_i_struct_equalp" | |
696 | { | |
697 | SCM vtable1, vtable2, layout; | |
698 | size_t struct_size, field_num; | |
699 | ||
700 | SCM_VALIDATE_STRUCT (1, s1); | |
701 | SCM_VALIDATE_STRUCT (2, s2); | |
702 | ||
703 | vtable1 = SCM_STRUCT_VTABLE (s1); | |
704 | vtable2 = SCM_STRUCT_VTABLE (s2); | |
705 | ||
706 | if (!scm_is_eq (vtable1, vtable2)) | |
707 | return SCM_BOOL_F; | |
708 | ||
709 | layout = SCM_STRUCT_LAYOUT (s1); | |
710 | struct_size = scm_i_symbol_length (layout) / 2; | |
711 | ||
712 | for (field_num = 0; field_num < struct_size; field_num++) | |
713 | { | |
714 | SCM s_field_num; | |
715 | SCM field1, field2; | |
716 | ||
717 | /* We have to use `scm_struct_ref ()' here so that fields are accessed | |
718 | consistently, notably wrt. field types and access rights. */ | |
719 | s_field_num = scm_from_size_t (field_num); | |
720 | field1 = scm_struct_ref (s1, s_field_num); | |
721 | field2 = scm_struct_ref (s2, s_field_num); | |
722 | ||
42ddb3cb LC |
723 | /* Self-referencing fields (type `s') must be skipped to avoid infinite |
724 | recursion. */ | |
725 | if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2)))) | |
726 | if (scm_is_false (scm_equal_p (field1, field2))) | |
727 | return SCM_BOOL_F; | |
d15ad007 LC |
728 | } |
729 | ||
42ddb3cb LC |
730 | /* FIXME: Tail elements should be tested for equality. */ |
731 | ||
d15ad007 LC |
732 | return SCM_BOOL_T; |
733 | } | |
734 | #undef FUNC_NAME | |
735 | ||
736 | ||
0f2d19dd JB |
737 | \f |
738 | ||
739 | ||
a1ec6916 | 740 | SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0, |
1bbd0b84 | 741 | (SCM handle, SCM pos), |
b6cf4d02 | 742 | "Access the @var{n}th field of @var{struct}.\n\n" |
b380b885 MD |
743 | "If the field is of type 'p', then it can be set to an arbitrary value.\n\n" |
744 | "If the field is of type 'u', then it can only be set to a non-negative\n" | |
745 | "integer value small enough to fit in one machine word.") | |
1bbd0b84 | 746 | #define FUNC_NAME s_scm_struct_ref |
0f2d19dd | 747 | { |
aa42c036 LC |
748 | SCM vtable, answer = SCM_UNDEFINED; |
749 | scm_t_bits *data; | |
a55c2b68 | 750 | size_t p; |
0f2d19dd | 751 | |
34d19ef6 | 752 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd | 753 | |
aa42c036 | 754 | vtable = SCM_STRUCT_VTABLE (handle); |
0f2d19dd | 755 | data = SCM_STRUCT_DATA (handle); |
a55c2b68 | 756 | p = scm_to_size_t (pos); |
0f2d19dd | 757 | |
aa42c036 LC |
758 | if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE) |
759 | && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size))) | |
e03b7f73 LC |
760 | /* The fast path: HANDLE is a struct with only "p" fields. */ |
761 | answer = SCM_PACK (data[p]); | |
2c36c351 | 762 | else |
0f2d19dd | 763 | { |
aa42c036 LC |
764 | SCM layout; |
765 | size_t layout_len, n_fields; | |
766 | scm_t_wchar field_type = 0; | |
767 | ||
768 | layout = SCM_STRUCT_LAYOUT (handle); | |
769 | layout_len = scm_i_symbol_length (layout); | |
770 | n_fields = layout_len / 2; | |
771 | ||
772 | if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1))) | |
773 | n_fields += data[n_fields - 1]; | |
774 | ||
775 | SCM_ASSERT_RANGE (1, pos, p < n_fields); | |
776 | ||
777 | if (p * 2 < layout_len) | |
778 | { | |
779 | scm_t_wchar ref; | |
780 | field_type = scm_i_symbol_ref (layout, p * 2); | |
781 | ref = scm_i_symbol_ref (layout, p * 2 + 1); | |
782 | if ((ref != 'r') && (ref != 'w') && (ref != 'h')) | |
783 | { | |
784 | if ((ref == 'R') || (ref == 'W')) | |
785 | field_type = 'u'; | |
786 | else | |
787 | SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos)); | |
788 | } | |
789 | } | |
790 | else if (scm_i_symbol_ref (layout, layout_len - 1) != 'O') | |
791 | field_type = scm_i_symbol_ref(layout, layout_len - 2); | |
792 | else | |
793 | SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos)); | |
794 | ||
795 | switch (field_type) | |
796 | { | |
797 | case 'u': | |
798 | answer = scm_from_ulong (data[p]); | |
799 | break; | |
0f2d19dd JB |
800 | |
801 | #if 0 | |
aa42c036 LC |
802 | case 'i': |
803 | answer = scm_from_long (data[p]); | |
804 | break; | |
0f2d19dd | 805 | |
aa42c036 LC |
806 | case 'd': |
807 | answer = scm_make_real (*((double *)&(data[p]))); | |
808 | break; | |
0f2d19dd JB |
809 | #endif |
810 | ||
aa42c036 LC |
811 | case 's': |
812 | case 'p': | |
813 | answer = SCM_PACK (data[p]); | |
814 | break; | |
0f2d19dd JB |
815 | |
816 | ||
aa42c036 LC |
817 | default: |
818 | SCM_MISC_ERROR ("unrecognized field type: ~S", | |
819 | scm_list_1 (SCM_MAKE_CHAR (field_type))); | |
820 | } | |
0f2d19dd JB |
821 | } |
822 | ||
823 | return answer; | |
824 | } | |
1bbd0b84 | 825 | #undef FUNC_NAME |
0f2d19dd JB |
826 | |
827 | ||
a1ec6916 | 828 | SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0, |
1bbd0b84 | 829 | (SCM handle, SCM pos, SCM val), |
e3239868 DH |
830 | "Set the slot of the structure @var{handle} with index @var{pos}\n" |
831 | "to @var{val}. Signal an error if the slot can not be written\n" | |
832 | "to.") | |
1bbd0b84 | 833 | #define FUNC_NAME s_scm_struct_set_x |
0f2d19dd | 834 | { |
aa42c036 LC |
835 | SCM vtable; |
836 | scm_t_bits *data; | |
a55c2b68 | 837 | size_t p; |
0f2d19dd | 838 | |
34d19ef6 | 839 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd | 840 | |
aa42c036 | 841 | vtable = SCM_STRUCT_VTABLE (handle); |
0f2d19dd | 842 | data = SCM_STRUCT_DATA (handle); |
a55c2b68 | 843 | p = scm_to_size_t (pos); |
0f2d19dd | 844 | |
aa42c036 LC |
845 | if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE) |
846 | && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE_RW) | |
847 | && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size))) | |
e03b7f73 | 848 | /* The fast path: HANDLE is a struct with only "pw" fields. */ |
aa42c036 LC |
849 | data[p] = SCM_UNPACK (val); |
850 | else | |
851 | { | |
852 | SCM layout; | |
853 | size_t layout_len, n_fields; | |
854 | scm_t_wchar field_type = 0; | |
0f2d19dd | 855 | |
aa42c036 LC |
856 | layout = SCM_STRUCT_LAYOUT (handle); |
857 | layout_len = scm_i_symbol_length (layout); | |
858 | n_fields = layout_len / 2; | |
0f2d19dd | 859 | |
aa42c036 LC |
860 | if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1))) |
861 | n_fields += data[n_fields - 1]; | |
862 | ||
863 | SCM_ASSERT_RANGE (1, pos, p < n_fields); | |
864 | ||
865 | if (p * 2 < layout_len) | |
866 | { | |
867 | char set_x; | |
868 | field_type = scm_i_symbol_ref (layout, p * 2); | |
869 | set_x = scm_i_symbol_ref (layout, p * 2 + 1); | |
870 | if (set_x != 'w' && set_x != 'h') | |
871 | SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos)); | |
872 | } | |
873 | else if (scm_i_symbol_ref (layout, layout_len - 1) == 'W') | |
874 | field_type = scm_i_symbol_ref (layout, layout_len - 2); | |
875 | else | |
1afff620 | 876 | SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos)); |
aa42c036 LC |
877 | |
878 | switch (field_type) | |
879 | { | |
880 | case 'u': | |
881 | data[p] = SCM_NUM2ULONG (3, val); | |
882 | break; | |
0f2d19dd JB |
883 | |
884 | #if 0 | |
aa42c036 LC |
885 | case 'i': |
886 | data[p] = SCM_NUM2LONG (3, val); | |
887 | break; | |
0f2d19dd | 888 | |
aa42c036 LC |
889 | case 'd': |
890 | *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3); | |
891 | break; | |
0f2d19dd JB |
892 | #endif |
893 | ||
aa42c036 LC |
894 | case 'p': |
895 | data[p] = SCM_UNPACK (val); | |
896 | break; | |
0f2d19dd | 897 | |
aa42c036 LC |
898 | case 's': |
899 | SCM_MISC_ERROR ("self fields immutable", SCM_EOL); | |
0f2d19dd | 900 | |
aa42c036 LC |
901 | default: |
902 | SCM_MISC_ERROR ("unrecognized field type: ~S", | |
903 | scm_list_1 (SCM_MAKE_CHAR (field_type))); | |
904 | } | |
0f2d19dd JB |
905 | } |
906 | ||
907 | return val; | |
908 | } | |
1bbd0b84 | 909 | #undef FUNC_NAME |
0f2d19dd JB |
910 | |
911 | ||
a1ec6916 | 912 | SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0, |
1bbd0b84 | 913 | (SCM handle), |
b380b885 | 914 | "Return the vtable structure that describes the type of @var{struct}.") |
1bbd0b84 | 915 | #define FUNC_NAME s_scm_struct_vtable |
0f2d19dd | 916 | { |
34d19ef6 | 917 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd JB |
918 | return SCM_STRUCT_VTABLE (handle); |
919 | } | |
1bbd0b84 | 920 | #undef FUNC_NAME |
0f2d19dd JB |
921 | |
922 | ||
a1ec6916 | 923 | SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0, |
1bbd0b84 | 924 | (SCM handle), |
e3239868 | 925 | "Return the vtable tag of the structure @var{handle}.") |
1bbd0b84 | 926 | #define FUNC_NAME s_scm_struct_vtable_tag |
0f2d19dd | 927 | { |
34d19ef6 | 928 | SCM_VALIDATE_VTABLE (1, handle); |
3d27ef4b AW |
929 | return scm_from_unsigned_integer |
930 | (((scm_t_bits)SCM_STRUCT_DATA (handle)) >> 3); | |
98d5f601 | 931 | } |
1bbd0b84 | 932 | #undef FUNC_NAME |
98d5f601 MD |
933 | |
934 | /* {Associating names and classes with vtables} | |
935 | * | |
936 | * The name of a vtable should probably be stored as a slot. This is | |
937 | * a backward compatible solution until agreement has been achieved on | |
938 | * how to associate names with vtables. | |
939 | */ | |
940 | ||
c014a02e | 941 | unsigned long |
d587c9e8 | 942 | scm_struct_ihashq (SCM obj, unsigned long n, void *closure) |
98d5f601 | 943 | { |
ad196599 MD |
944 | /* The length of the hash table should be a relative prime it's not |
945 | necessary to shift down the address. */ | |
f1267706 | 946 | return SCM_UNPACK (obj) % n; |
98d5f601 MD |
947 | } |
948 | ||
949 | SCM | |
950 | scm_struct_create_handle (SCM obj) | |
951 | { | |
952 | SCM handle = scm_hash_fn_create_handle_x (scm_struct_table, | |
953 | obj, | |
954 | SCM_BOOL_F, | |
955 | scm_struct_ihashq, | |
d587c9e8 | 956 | (scm_t_assoc_fn) scm_sloppy_assq, |
98d5f601 | 957 | 0); |
7888309b | 958 | if (scm_is_false (SCM_CDR (handle))) |
98d5f601 MD |
959 | SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F)); |
960 | return handle; | |
961 | } | |
962 | ||
a1ec6916 | 963 | SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0, |
1bbd0b84 | 964 | (SCM vtable), |
e3239868 | 965 | "Return the name of the vtable @var{vtable}.") |
1bbd0b84 | 966 | #define FUNC_NAME s_scm_struct_vtable_name |
98d5f601 | 967 | { |
34d19ef6 | 968 | SCM_VALIDATE_VTABLE (1, vtable); |
98d5f601 MD |
969 | return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable))); |
970 | } | |
1bbd0b84 | 971 | #undef FUNC_NAME |
98d5f601 | 972 | |
a1ec6916 | 973 | SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0, |
1bbd0b84 | 974 | (SCM vtable, SCM name), |
e3239868 | 975 | "Set the name of the vtable @var{vtable} to @var{name}.") |
1bbd0b84 | 976 | #define FUNC_NAME s_scm_set_struct_vtable_name_x |
98d5f601 | 977 | { |
34d19ef6 HWN |
978 | SCM_VALIDATE_VTABLE (1, vtable); |
979 | SCM_VALIDATE_SYMBOL (2, name); | |
98d5f601 MD |
980 | SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)), |
981 | name); | |
982 | return SCM_UNSPECIFIED; | |
0f2d19dd | 983 | } |
1bbd0b84 | 984 | #undef FUNC_NAME |
0f2d19dd JB |
985 | |
986 | ||
987 | \f | |
988 | ||
bafcafb2 | 989 | void |
1bbd0b84 | 990 | scm_print_struct (SCM exp, SCM port, scm_print_state *pstate) |
bafcafb2 | 991 | { |
7888309b | 992 | if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp)))) |
4bfdf158 MD |
993 | scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate); |
994 | else | |
bafcafb2 | 995 | { |
a1ae1799 MD |
996 | SCM vtable = SCM_STRUCT_VTABLE (exp); |
997 | SCM name = scm_struct_vtable_name (vtable); | |
998 | scm_puts ("#<", port); | |
7888309b | 999 | if (scm_is_true (name)) |
b6cf4d02 AW |
1000 | { |
1001 | scm_display (name, port); | |
1002 | scm_putc (' ', port); | |
1003 | } | |
a1ae1799 | 1004 | else |
b6cf4d02 AW |
1005 | { |
1006 | if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE)) | |
1007 | scm_puts ("vtable:", port); | |
1008 | else | |
1009 | scm_puts ("struct:", port); | |
1010 | scm_uintprint (SCM_UNPACK (vtable), 16, port); | |
1011 | scm_putc (' ', port); | |
1012 | scm_write (SCM_VTABLE_LAYOUT (vtable), port); | |
1013 | scm_putc (' ', port); | |
1014 | } | |
0345e278 | 1015 | scm_uintprint (SCM_UNPACK (exp), 16, port); |
b6cf4d02 AW |
1016 | /* hackety hack */ |
1017 | if (SCM_STRUCT_APPLICABLE_P (exp)) | |
1018 | { | |
1019 | if (scm_is_true (SCM_STRUCT_PROCEDURE (exp))) | |
1020 | { | |
1021 | scm_puts (" proc: ", port); | |
1022 | if (scm_is_true (scm_procedure_p (SCM_STRUCT_PROCEDURE (exp)))) | |
1023 | scm_write (SCM_STRUCT_PROCEDURE (exp), port); | |
1024 | else | |
1025 | scm_puts ("(not a procedure?)", port); | |
1026 | } | |
1027 | if (SCM_STRUCT_SETTER_P (exp)) | |
1028 | { | |
1029 | scm_puts (" setter: ", port); | |
1030 | scm_write (SCM_STRUCT_SETTER (exp), port); | |
1031 | } | |
1032 | } | |
b7f3516f | 1033 | scm_putc ('>', port); |
bafcafb2 | 1034 | } |
bafcafb2 | 1035 | } |
1cc91f1b | 1036 | |
0f2d19dd JB |
1037 | void |
1038 | scm_init_struct () | |
0f2d19dd | 1039 | { |
01e74380 LC |
1040 | /* The first word of a struct is equal to `SCM_STRUCT_DATA (vtable) + |
1041 | scm_tc3_struct', and `SCM_STRUCT_DATA (vtable)' is 2 words after VTABLE by | |
1042 | default. */ | |
1043 | GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits) + scm_tc3_struct); | |
b6cf4d02 | 1044 | |
227eff6a LC |
1045 | /* In the general case, `SCM_STRUCT_DATA (obj)' points 2 words after the |
1046 | beginning of a GC-allocated region; that region is different from that of | |
1047 | OBJ once OBJ has undergone class redefinition. */ | |
1048 | GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits)); | |
1049 | ||
b6cf4d02 AW |
1050 | scm_struct_table = scm_make_weak_key_hash_table (scm_from_int (31)); |
1051 | required_vtable_fields = scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT); | |
1052 | required_applicable_fields = scm_from_locale_string (SCM_APPLICABLE_BASE_LAYOUT); | |
1053 | required_applicable_with_setter_fields = scm_from_locale_string (SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT); | |
651f2cd2 | 1054 | |
db5ed685 | 1055 | scm_standard_vtable_vtable = |
b6cf4d02 AW |
1056 | scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL); |
1057 | ||
1058 | scm_applicable_struct_vtable_vtable = | |
db5ed685 | 1059 | scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0, |
b6cf4d02 AW |
1060 | scm_list_1 (scm_make_struct_layout (required_vtable_fields))); |
1061 | SCM_SET_VTABLE_FLAGS (scm_applicable_struct_vtable_vtable, | |
1062 | SCM_VTABLE_FLAG_APPLICABLE_VTABLE); | |
1063 | scm_c_define ("<applicable-struct-vtable>", scm_applicable_struct_vtable_vtable); | |
1064 | ||
1065 | scm_applicable_struct_with_setter_vtable_vtable = | |
db5ed685 | 1066 | scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0, |
b6cf4d02 AW |
1067 | scm_list_1 (scm_make_struct_layout (required_vtable_fields))); |
1068 | SCM_SET_VTABLE_FLAGS (scm_applicable_struct_with_setter_vtable_vtable, | |
1069 | SCM_VTABLE_FLAG_APPLICABLE_VTABLE | SCM_VTABLE_FLAG_SETTER_VTABLE); | |
1070 | scm_c_define ("<applicable-struct-with-setter-vtable>", scm_applicable_struct_with_setter_vtable_vtable); | |
651f2cd2 | 1071 | |
e11e83f3 | 1072 | scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout)); |
86d31dfe | 1073 | scm_c_define ("vtable-index-printer", |
b6cf4d02 | 1074 | scm_from_int (scm_vtable_index_instance_printer)); |
e11e83f3 | 1075 | scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user)); |
a0599745 | 1076 | #include "libguile/struct.x" |
0f2d19dd | 1077 | } |
89e00824 ML |
1078 | |
1079 | /* | |
1080 | Local Variables: | |
1081 | c-file-style: "gnu" | |
1082 | End: | |
1083 | */ |