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