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 AW |
262 | if (! scm_is_valid_vtable_layout (SCM_VTABLE_LAYOUT (obj))) |
263 | scm_misc_error (FUNC_NAME, "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)))) | |
289 | scm_misc_error (FUNC_NAME, "invalid applicable-with-setter struct layout", | |
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)))) | |
300 | scm_misc_error (FUNC_NAME, "invalid applicable struct layout", | |
301 | scm_list_1 (olayout)); | |
302 | SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE); | |
303 | } | |
304 | } | |
305 | #undef FUNC_NAME | |
0f2d19dd | 306 | |
1cc91f1b | 307 | |
f7620510 | 308 | static void |
66e78727 AW |
309 | scm_struct_init (SCM handle, SCM layout, size_t n_tail, |
310 | size_t n_inits, scm_t_bits *inits) | |
0f2d19dd | 311 | { |
aa42c036 LC |
312 | SCM vtable; |
313 | scm_t_bits *mem; | |
314 | ||
315 | vtable = SCM_STRUCT_VTABLE (handle); | |
316 | mem = SCM_STRUCT_DATA (handle); | |
317 | ||
318 | if (SCM_UNPACK (vtable) != 0 | |
319 | && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE) | |
320 | && n_tail == 0 | |
321 | && n_inits == SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)) | |
322 | /* The fast path: HANDLE has N_INITS "p" fields. */ | |
323 | memcpy (mem, inits, n_inits * sizeof (SCM)); | |
324 | else | |
0f2d19dd | 325 | { |
aa42c036 LC |
326 | scm_t_wchar prot = 0; |
327 | int n_fields = scm_i_symbol_length (layout) / 2; | |
328 | int tailp = 0; | |
329 | int i; | |
330 | size_t inits_idx = 0; | |
331 | ||
332 | i = -2; | |
333 | while (n_fields) | |
2c36c351 | 334 | { |
aa42c036 | 335 | if (!tailp) |
2c36c351 | 336 | { |
aa42c036 LC |
337 | i += 2; |
338 | prot = scm_i_symbol_ref (layout, i+1); | |
339 | if (SCM_LAYOUT_TAILP (prot)) | |
340 | { | |
341 | tailp = 1; | |
342 | prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o'; | |
343 | *mem++ = (scm_t_bits)n_tail; | |
344 | n_fields += n_tail - 1; | |
345 | if (n_fields == 0) | |
346 | break; | |
347 | } | |
2c36c351 | 348 | } |
aa42c036 | 349 | switch (scm_i_symbol_ref (layout, i)) |
0f2d19dd | 350 | { |
aa42c036 LC |
351 | case 'u': |
352 | if ((prot != 'r' && prot != 'w') || inits_idx == n_inits) | |
353 | *mem = 0; | |
354 | else | |
355 | { | |
356 | *mem = scm_to_ulong (SCM_PACK (inits[inits_idx])); | |
357 | inits_idx++; | |
358 | } | |
359 | break; | |
360 | ||
361 | case 'p': | |
362 | if ((prot != 'r' && prot != 'w') || inits_idx == n_inits) | |
363 | *mem = SCM_UNPACK (SCM_BOOL_F); | |
364 | else | |
365 | { | |
366 | *mem = inits[inits_idx]; | |
367 | inits_idx++; | |
368 | } | |
369 | ||
370 | break; | |
371 | ||
372 | case 's': | |
373 | *mem = SCM_UNPACK (handle); | |
374 | break; | |
0f2d19dd | 375 | } |
0f2d19dd | 376 | |
aa42c036 LC |
377 | n_fields--; |
378 | mem++; | |
0f2d19dd | 379 | } |
0f2d19dd JB |
380 | } |
381 | } | |
382 | ||
383 | ||
a1ec6916 | 384 | SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0, |
1bbd0b84 | 385 | (SCM x), |
0233bfc1 | 386 | "Return @code{#t} iff @var{x} is a structure object, else\n" |
942e5b91 | 387 | "@code{#f}.") |
1bbd0b84 | 388 | #define FUNC_NAME s_scm_struct_p |
0f2d19dd | 389 | { |
7888309b | 390 | return scm_from_bool(SCM_STRUCTP (x)); |
0f2d19dd | 391 | } |
1bbd0b84 | 392 | #undef FUNC_NAME |
0f2d19dd | 393 | |
a1ec6916 | 394 | SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0, |
1bbd0b84 | 395 | (SCM x), |
0233bfc1 | 396 | "Return @code{#t} iff @var{x} is a vtable structure.") |
1bbd0b84 | 397 | #define FUNC_NAME s_scm_struct_vtable_p |
0f2d19dd | 398 | { |
b6cf4d02 AW |
399 | return scm_from_bool |
400 | (SCM_STRUCTP (x) | |
401 | && SCM_STRUCT_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VTABLE)); | |
0f2d19dd | 402 | } |
1bbd0b84 | 403 | #undef FUNC_NAME |
0f2d19dd | 404 | |
14d1400f | 405 | |
51f66c91 AW |
406 | /* Finalization: invoke the finalizer of the struct pointed to by PTR. */ |
407 | static void | |
408 | struct_finalizer_trampoline (GC_PTR ptr, GC_PTR unused_data) | |
409 | { | |
410 | SCM obj = PTR2SCM (ptr); | |
411 | scm_t_struct_finalize finalize = SCM_STRUCT_FINALIZER (obj); | |
412 | ||
413 | if (finalize) | |
414 | finalize (obj); | |
415 | } | |
416 | ||
14d1400f JB |
417 | /* All struct data must be allocated at an address whose bottom three |
418 | bits are zero. This is because the tag for a struct lives in the | |
419 | bottom three bits of the struct's car, and the upper bits point to | |
420 | the data of its vtable, which is a struct itself. Thus, if the | |
421 | address of that data doesn't end in three zeros, tagging it will | |
422 | destroy the pointer. | |
423 | ||
b6cf4d02 AW |
424 | I suppose we should make it clear here that, the data must be 8-byte aligned, |
425 | *within* the struct, and the struct itself should be 8-byte aligned. In | |
426 | practice we ensure this because the data starts two words into a struct. | |
14d1400f | 427 | |
b6cf4d02 AW |
428 | This function allocates an 8-byte aligned block of memory, whose first word |
429 | points to the given vtable data, then a data pointer, then n_words of data. | |
430 | */ | |
431 | SCM | |
96a44c1c | 432 | scm_i_alloc_struct (scm_t_bits *vtable_data, int n_words) |
14d1400f | 433 | { |
9a974fd3 AW |
434 | SCM ret; |
435 | ||
436 | ret = scm_words ((scm_t_bits)vtable_data | scm_tc3_struct, n_words + 2); | |
437 | SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)SCM_CELL_OBJECT_LOC (ret, 2)); | |
5e67dc27 | 438 | |
51f66c91 AW |
439 | /* vtable_data can be null when making a vtable vtable */ |
440 | if (vtable_data && vtable_data[scm_vtable_index_instance_finalize]) | |
441 | { | |
442 | /* Register a finalizer for the newly created instance. */ | |
443 | GC_finalization_proc prev_finalizer; | |
444 | GC_PTR prev_finalizer_data; | |
9a974fd3 | 445 | GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret), |
51f66c91 AW |
446 | struct_finalizer_trampoline, |
447 | NULL, | |
448 | &prev_finalizer, | |
449 | &prev_finalizer_data); | |
450 | } | |
5e67dc27 | 451 | |
9a974fd3 | 452 | return ret; |
5e67dc27 LC |
453 | } |
454 | ||
5e67dc27 | 455 | \f |
66e78727 AW |
456 | SCM |
457 | scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits *init) | |
458 | #define FUNC_NAME "make-struct" | |
0f2d19dd JB |
459 | { |
460 | SCM layout; | |
a55c2b68 | 461 | size_t basic_size; |
b6cf4d02 | 462 | SCM obj; |
0f2d19dd | 463 | |
34d19ef6 | 464 | SCM_VALIDATE_VTABLE (1, vtable); |
0f2d19dd | 465 | |
b6cf4d02 | 466 | layout = SCM_VTABLE_LAYOUT (vtable); |
cc95e00a | 467 | basic_size = scm_i_symbol_length (layout) / 2; |
651f2cd2 | 468 | |
66e78727 | 469 | if (n_tail != 0) |
651f2cd2 KR |
470 | { |
471 | SCM layout_str, last_char; | |
472 | ||
473 | if (basic_size == 0) | |
474 | { | |
475 | bad_tail: | |
476 | SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL); | |
477 | } | |
478 | ||
479 | layout_str = scm_symbol_to_string (layout); | |
480 | last_char = scm_string_ref (layout_str, | |
481 | scm_from_size_t (2 * basic_size - 1)); | |
482 | if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char))) | |
483 | goto bad_tail; | |
484 | } | |
cb823e63 | 485 | |
96a44c1c | 486 | obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), basic_size + n_tail); |
5e67dc27 | 487 | |
66e78727 | 488 | scm_struct_init (obj, layout, n_tail, n_init, init); |
b6cf4d02 | 489 | |
51f66c91 AW |
490 | /* only check things and inherit magic if the layout was passed as an initarg. |
491 | something of a hack, but it's for back-compatibility. */ | |
b6cf4d02 | 492 | if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE) |
b6cf4d02 | 493 | && scm_is_true (SCM_VTABLE_LAYOUT (obj))) |
51f66c91 | 494 | scm_i_struct_inherit_vtable_magic (vtable, obj); |
651f2cd2 | 495 | |
b6cf4d02 | 496 | return obj; |
0f2d19dd | 497 | } |
1bbd0b84 | 498 | #undef FUNC_NAME |
0f2d19dd | 499 | |
66e78727 AW |
500 | SCM |
501 | scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits init, ...) | |
502 | { | |
503 | va_list foo; | |
504 | scm_t_bits *v; | |
505 | size_t i; | |
506 | ||
507 | v = alloca (sizeof (scm_t_bits) * n_init); | |
508 | ||
509 | va_start (foo, init); | |
510 | for (i = 0; i < n_init; i++) | |
511 | { | |
512 | v[i] = init; | |
513 | init = va_arg (foo, scm_t_bits); | |
514 | } | |
515 | va_end (foo); | |
516 | ||
517 | return scm_c_make_structv (vtable, n_tail, n_init, v); | |
518 | } | |
519 | ||
520 | SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1, | |
521 | (SCM vtable, SCM tail_array_size, SCM init), | |
522 | "Create a new structure.\n\n" | |
523 | "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n" | |
524 | "@var{tail-elts} must be a non-negative integer. If the layout\n" | |
525 | "specification indicated by @var{type} includes a tail-array,\n" | |
526 | "this is the number of elements allocated to that array.\n\n" | |
527 | "The @var{init1}, @dots{} are optional arguments describing how\n" | |
528 | "successive fields of the structure should be initialized. Only fields\n" | |
529 | "with protection 'r' or 'w' can be initialized, except for fields of\n" | |
530 | "type 's', which are automatically initialized to point to the new\n" | |
531 | "structure itself. Fields with protection 'o' can not be initialized by\n" | |
532 | "Scheme programs.\n\n" | |
533 | "If fewer optional arguments than initializable fields are supplied,\n" | |
534 | "fields of type 'p' get default value #f while fields of type 'u' are\n" | |
535 | "initialized to 0.\n\n" | |
536 | "For more information, see the documentation for @code{make-vtable-vtable}.") | |
537 | #define FUNC_NAME s_scm_make_struct | |
538 | { | |
539 | size_t i, n_init; | |
540 | long ilen; | |
541 | scm_t_bits *v; | |
542 | ||
543 | SCM_VALIDATE_VTABLE (1, vtable); | |
544 | ilen = scm_ilength (init); | |
545 | if (ilen < 0) | |
546 | SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); | |
547 | ||
548 | n_init = (size_t)ilen; | |
549 | ||
550 | /* best to use alloca, but init could be big, so hack to avoid a possible | |
551 | stack overflow */ | |
552 | if (n_init < 64) | |
553 | v = alloca (n_init * sizeof(scm_t_bits)); | |
554 | else | |
555 | v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct"); | |
556 | ||
557 | for (i = 0; i < n_init; i++, init = SCM_CDR (init)) | |
558 | v[i] = SCM_UNPACK (SCM_CAR (init)); | |
559 | ||
560 | return scm_c_make_structv (vtable, scm_to_size_t (tail_array_size), n_init, v); | |
561 | } | |
562 | #undef FUNC_NAME | |
563 | ||
0f2d19dd JB |
564 | |
565 | ||
a1ec6916 | 566 | SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1, |
04323af4 | 567 | (SCM user_fields, SCM tail_array_size, SCM init), |
b380b885 | 568 | "Return a new, self-describing vtable structure.\n\n" |
04323af4 MD |
569 | "@var{user-fields} is a string describing user defined fields of the\n" |
570 | "vtable beginning at index @code{vtable-offset-user}\n" | |
571 | "(see @code{make-struct-layout}).\n\n" | |
b380b885 MD |
572 | "@var{tail-size} specifies the size of the tail-array (if any) of\n" |
573 | "this vtable.\n\n" | |
6386e25c | 574 | "@var{init1}, @dots{} are the optional initializers for the fields of\n" |
04323af4 MD |
575 | "the vtable.\n\n" |
576 | "Vtables have one initializable system field---the struct printer.\n" | |
577 | "This field comes before the user fields in the initializers passed\n" | |
578 | "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n" | |
579 | "a third optional argument to @code{make-vtable-vtable} and a fourth to\n" | |
580 | "@code{make-struct} when creating vtables:\n\n" | |
581 | "If the value is a procedure, it will be called instead of the standard\n" | |
582 | "printer whenever a struct described by this vtable is printed.\n" | |
583 | "The procedure will be called with arguments STRUCT and PORT.\n\n" | |
584 | "The structure of a struct is described by a vtable, so the vtable is\n" | |
585 | "in essence the type of the struct. The vtable is itself a struct with\n" | |
586 | "a vtable. This could go on forever if it weren't for the\n" | |
29b4f9fb | 587 | "vtable-vtables which are self-describing vtables, and thus terminate\n" |
04323af4 MD |
588 | "the chain.\n\n" |
589 | "There are several potential ways of using structs, but the standard\n" | |
590 | "one is to use three kinds of structs, together building up a type\n" | |
591 | "sub-system: one vtable-vtable working as the root and one or several\n" | |
592 | "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n" | |
29b4f9fb | 593 | "compared to the class <class> which is the class of itself.)\n\n" |
1e6808ea | 594 | "@lisp\n" |
04323af4 MD |
595 | "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n" |
596 | "(define (make-ball-type ball-color)\n" | |
597 | " (make-struct ball-root 0\n" | |
598 | " (make-struct-layout \"pw\")\n" | |
599 | " (lambda (ball port)\n" | |
600 | " (format port \"#<a ~A ball owned by ~A>\"\n" | |
601 | " (color ball)\n" | |
602 | " (owner ball)))\n" | |
603 | " ball-color))\n" | |
604 | "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n" | |
605 | "(define (owner ball) (struct-ref ball 0))\n\n" | |
606 | "(define red (make-ball-type 'red))\n" | |
607 | "(define green (make-ball-type 'green))\n\n" | |
608 | "(define (make-ball type owner) (make-struct type 0 owner))\n\n" | |
609 | "(define ball (make-ball green 'Nisse))\n" | |
610 | "ball @result{} #<a green ball owned by Nisse>\n" | |
9401323e | 611 | "@end lisp") |
1bbd0b84 | 612 | #define FUNC_NAME s_scm_make_vtable_vtable |
0f2d19dd | 613 | { |
696ac4df LC |
614 | SCM fields, layout, obj; |
615 | size_t basic_size, n_tail, i, n_init; | |
66e78727 AW |
616 | long ilen; |
617 | scm_t_bits *v; | |
0f2d19dd | 618 | |
d1ca2c64 | 619 | SCM_VALIDATE_STRING (1, user_fields); |
66e78727 AW |
620 | ilen = scm_ilength (init); |
621 | if (ilen < 0) | |
622 | SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL); | |
623 | ||
624 | n_init = (size_t)ilen + 1; /* + 1 for the layout */ | |
625 | ||
626 | /* best to use alloca, but init could be big, so hack to avoid a possible | |
627 | stack overflow */ | |
628 | if (n_init < 64) | |
629 | v = alloca (n_init * sizeof(scm_t_bits)); | |
630 | else | |
631 | v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct"); | |
0f2d19dd | 632 | |
1afff620 KN |
633 | fields = scm_string_append (scm_list_2 (required_vtable_fields, |
634 | user_fields)); | |
0f2d19dd | 635 | layout = scm_make_struct_layout (fields); |
cc95e00a | 636 | basic_size = scm_i_symbol_length (layout) / 2; |
66e78727 AW |
637 | n_tail = scm_to_size_t (tail_array_size); |
638 | ||
639 | i = 0; | |
640 | v[i++] = SCM_UNPACK (layout); | |
641 | for (; i < n_init; i++, init = SCM_CDR (init)) | |
642 | v[i] = SCM_UNPACK (SCM_CAR (init)); | |
643 | ||
9de87eea | 644 | SCM_CRITICAL_SECTION_START; |
96a44c1c | 645 | obj = scm_i_alloc_struct (NULL, basic_size + n_tail); |
696ac4df LC |
646 | /* Make it so that the vtable of OBJ is itself. */ |
647 | SCM_SET_CELL_WORD_0 (obj, (scm_t_bits) SCM_STRUCT_DATA (obj) | scm_tc3_struct); | |
9de87eea | 648 | SCM_CRITICAL_SECTION_END; |
696ac4df | 649 | |
66e78727 | 650 | scm_struct_init (obj, layout, n_tail, n_init, v); |
b6cf4d02 | 651 | SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VTABLE); |
696ac4df | 652 | |
b6cf4d02 | 653 | return obj; |
0f2d19dd | 654 | } |
1bbd0b84 | 655 | #undef FUNC_NAME |
0f2d19dd | 656 | |
d15ad007 | 657 | |
651f2cd2 KR |
658 | SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0, |
659 | (SCM fields, SCM printer), | |
660 | "Create a vtable, for creating structures with the given\n" | |
661 | "@var{fields}.\n" | |
662 | "\n" | |
663 | "The optional @var{printer} argument is a function to be called\n" | |
664 | "@code{(@var{printer} struct port)} on the structures created.\n" | |
665 | "It should look at @var{struct} and write to @var{port}.") | |
666 | #define FUNC_NAME s_scm_make_vtable | |
667 | { | |
668 | if (SCM_UNBNDP (printer)) | |
669 | printer = SCM_BOOL_F; | |
670 | ||
db5ed685 | 671 | return scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0, |
651f2cd2 KR |
672 | scm_list_2 (scm_make_struct_layout (fields), |
673 | printer)); | |
674 | } | |
675 | #undef FUNC_NAME | |
676 | ||
677 | ||
d15ad007 LC |
678 | /* Return true if S1 and S2 are equal structures, i.e., if their vtable and |
679 | contents are the same. Field protections are honored. Thus, it is an | |
680 | error to test the equality of structures that contain opaque fields. */ | |
681 | SCM | |
682 | scm_i_struct_equalp (SCM s1, SCM s2) | |
683 | #define FUNC_NAME "scm_i_struct_equalp" | |
684 | { | |
685 | SCM vtable1, vtable2, layout; | |
686 | size_t struct_size, field_num; | |
687 | ||
688 | SCM_VALIDATE_STRUCT (1, s1); | |
689 | SCM_VALIDATE_STRUCT (2, s2); | |
690 | ||
691 | vtable1 = SCM_STRUCT_VTABLE (s1); | |
692 | vtable2 = SCM_STRUCT_VTABLE (s2); | |
693 | ||
694 | if (!scm_is_eq (vtable1, vtable2)) | |
695 | return SCM_BOOL_F; | |
696 | ||
697 | layout = SCM_STRUCT_LAYOUT (s1); | |
698 | struct_size = scm_i_symbol_length (layout) / 2; | |
699 | ||
700 | for (field_num = 0; field_num < struct_size; field_num++) | |
701 | { | |
702 | SCM s_field_num; | |
703 | SCM field1, field2; | |
704 | ||
705 | /* We have to use `scm_struct_ref ()' here so that fields are accessed | |
706 | consistently, notably wrt. field types and access rights. */ | |
707 | s_field_num = scm_from_size_t (field_num); | |
708 | field1 = scm_struct_ref (s1, s_field_num); | |
709 | field2 = scm_struct_ref (s2, s_field_num); | |
710 | ||
42ddb3cb LC |
711 | /* Self-referencing fields (type `s') must be skipped to avoid infinite |
712 | recursion. */ | |
713 | if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2)))) | |
714 | if (scm_is_false (scm_equal_p (field1, field2))) | |
715 | return SCM_BOOL_F; | |
d15ad007 LC |
716 | } |
717 | ||
42ddb3cb LC |
718 | /* FIXME: Tail elements should be tested for equality. */ |
719 | ||
d15ad007 LC |
720 | return SCM_BOOL_T; |
721 | } | |
722 | #undef FUNC_NAME | |
723 | ||
724 | ||
0f2d19dd JB |
725 | \f |
726 | ||
727 | ||
a1ec6916 | 728 | SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0, |
1bbd0b84 | 729 | (SCM handle, SCM pos), |
b6cf4d02 | 730 | "Access the @var{n}th field of @var{struct}.\n\n" |
b380b885 MD |
731 | "If the field is of type 'p', then it can be set to an arbitrary value.\n\n" |
732 | "If the field is of type 'u', then it can only be set to a non-negative\n" | |
733 | "integer value small enough to fit in one machine word.") | |
1bbd0b84 | 734 | #define FUNC_NAME s_scm_struct_ref |
0f2d19dd | 735 | { |
aa42c036 LC |
736 | SCM vtable, answer = SCM_UNDEFINED; |
737 | scm_t_bits *data; | |
a55c2b68 | 738 | size_t p; |
0f2d19dd | 739 | |
34d19ef6 | 740 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd | 741 | |
aa42c036 | 742 | vtable = SCM_STRUCT_VTABLE (handle); |
0f2d19dd | 743 | data = SCM_STRUCT_DATA (handle); |
a55c2b68 | 744 | p = scm_to_size_t (pos); |
0f2d19dd | 745 | |
aa42c036 LC |
746 | if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE) |
747 | && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size))) | |
e03b7f73 LC |
748 | /* The fast path: HANDLE is a struct with only "p" fields. */ |
749 | answer = SCM_PACK (data[p]); | |
2c36c351 | 750 | else |
0f2d19dd | 751 | { |
aa42c036 LC |
752 | SCM layout; |
753 | size_t layout_len, n_fields; | |
754 | scm_t_wchar field_type = 0; | |
755 | ||
756 | layout = SCM_STRUCT_LAYOUT (handle); | |
757 | layout_len = scm_i_symbol_length (layout); | |
758 | n_fields = layout_len / 2; | |
759 | ||
760 | if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1))) | |
761 | n_fields += data[n_fields - 1]; | |
762 | ||
763 | SCM_ASSERT_RANGE (1, pos, p < n_fields); | |
764 | ||
765 | if (p * 2 < layout_len) | |
766 | { | |
767 | scm_t_wchar ref; | |
768 | field_type = scm_i_symbol_ref (layout, p * 2); | |
769 | ref = scm_i_symbol_ref (layout, p * 2 + 1); | |
770 | if ((ref != 'r') && (ref != 'w') && (ref != 'h')) | |
771 | { | |
772 | if ((ref == 'R') || (ref == 'W')) | |
773 | field_type = 'u'; | |
774 | else | |
775 | SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos)); | |
776 | } | |
777 | } | |
778 | else if (scm_i_symbol_ref (layout, layout_len - 1) != 'O') | |
779 | field_type = scm_i_symbol_ref(layout, layout_len - 2); | |
780 | else | |
781 | SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos)); | |
782 | ||
783 | switch (field_type) | |
784 | { | |
785 | case 'u': | |
786 | answer = scm_from_ulong (data[p]); | |
787 | break; | |
0f2d19dd JB |
788 | |
789 | #if 0 | |
aa42c036 LC |
790 | case 'i': |
791 | answer = scm_from_long (data[p]); | |
792 | break; | |
0f2d19dd | 793 | |
aa42c036 LC |
794 | case 'd': |
795 | answer = scm_make_real (*((double *)&(data[p]))); | |
796 | break; | |
0f2d19dd JB |
797 | #endif |
798 | ||
aa42c036 LC |
799 | case 's': |
800 | case 'p': | |
801 | answer = SCM_PACK (data[p]); | |
802 | break; | |
0f2d19dd JB |
803 | |
804 | ||
aa42c036 LC |
805 | default: |
806 | SCM_MISC_ERROR ("unrecognized field type: ~S", | |
807 | scm_list_1 (SCM_MAKE_CHAR (field_type))); | |
808 | } | |
0f2d19dd JB |
809 | } |
810 | ||
811 | return answer; | |
812 | } | |
1bbd0b84 | 813 | #undef FUNC_NAME |
0f2d19dd JB |
814 | |
815 | ||
a1ec6916 | 816 | SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0, |
1bbd0b84 | 817 | (SCM handle, SCM pos, SCM val), |
e3239868 DH |
818 | "Set the slot of the structure @var{handle} with index @var{pos}\n" |
819 | "to @var{val}. Signal an error if the slot can not be written\n" | |
820 | "to.") | |
1bbd0b84 | 821 | #define FUNC_NAME s_scm_struct_set_x |
0f2d19dd | 822 | { |
aa42c036 LC |
823 | SCM vtable; |
824 | scm_t_bits *data; | |
a55c2b68 | 825 | size_t p; |
0f2d19dd | 826 | |
34d19ef6 | 827 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd | 828 | |
aa42c036 | 829 | vtable = SCM_STRUCT_VTABLE (handle); |
0f2d19dd | 830 | data = SCM_STRUCT_DATA (handle); |
a55c2b68 | 831 | p = scm_to_size_t (pos); |
0f2d19dd | 832 | |
aa42c036 LC |
833 | if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE) |
834 | && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE_RW) | |
835 | && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size))) | |
e03b7f73 | 836 | /* The fast path: HANDLE is a struct with only "pw" fields. */ |
aa42c036 LC |
837 | data[p] = SCM_UNPACK (val); |
838 | else | |
839 | { | |
840 | SCM layout; | |
841 | size_t layout_len, n_fields; | |
842 | scm_t_wchar field_type = 0; | |
0f2d19dd | 843 | |
aa42c036 LC |
844 | layout = SCM_STRUCT_LAYOUT (handle); |
845 | layout_len = scm_i_symbol_length (layout); | |
846 | n_fields = layout_len / 2; | |
0f2d19dd | 847 | |
aa42c036 LC |
848 | if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1))) |
849 | n_fields += data[n_fields - 1]; | |
850 | ||
851 | SCM_ASSERT_RANGE (1, pos, p < n_fields); | |
852 | ||
853 | if (p * 2 < layout_len) | |
854 | { | |
855 | char set_x; | |
856 | field_type = scm_i_symbol_ref (layout, p * 2); | |
857 | set_x = scm_i_symbol_ref (layout, p * 2 + 1); | |
858 | if (set_x != 'w' && set_x != 'h') | |
859 | SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos)); | |
860 | } | |
861 | else if (scm_i_symbol_ref (layout, layout_len - 1) == 'W') | |
862 | field_type = scm_i_symbol_ref (layout, layout_len - 2); | |
863 | else | |
1afff620 | 864 | SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos)); |
aa42c036 LC |
865 | |
866 | switch (field_type) | |
867 | { | |
868 | case 'u': | |
869 | data[p] = SCM_NUM2ULONG (3, val); | |
870 | break; | |
0f2d19dd JB |
871 | |
872 | #if 0 | |
aa42c036 LC |
873 | case 'i': |
874 | data[p] = SCM_NUM2LONG (3, val); | |
875 | break; | |
0f2d19dd | 876 | |
aa42c036 LC |
877 | case 'd': |
878 | *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3); | |
879 | break; | |
0f2d19dd JB |
880 | #endif |
881 | ||
aa42c036 LC |
882 | case 'p': |
883 | data[p] = SCM_UNPACK (val); | |
884 | break; | |
0f2d19dd | 885 | |
aa42c036 LC |
886 | case 's': |
887 | SCM_MISC_ERROR ("self fields immutable", SCM_EOL); | |
0f2d19dd | 888 | |
aa42c036 LC |
889 | default: |
890 | SCM_MISC_ERROR ("unrecognized field type: ~S", | |
891 | scm_list_1 (SCM_MAKE_CHAR (field_type))); | |
892 | } | |
0f2d19dd JB |
893 | } |
894 | ||
895 | return val; | |
896 | } | |
1bbd0b84 | 897 | #undef FUNC_NAME |
0f2d19dd JB |
898 | |
899 | ||
a1ec6916 | 900 | SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0, |
1bbd0b84 | 901 | (SCM handle), |
b380b885 | 902 | "Return the vtable structure that describes the type of @var{struct}.") |
1bbd0b84 | 903 | #define FUNC_NAME s_scm_struct_vtable |
0f2d19dd | 904 | { |
34d19ef6 | 905 | SCM_VALIDATE_STRUCT (1, handle); |
0f2d19dd JB |
906 | return SCM_STRUCT_VTABLE (handle); |
907 | } | |
1bbd0b84 | 908 | #undef FUNC_NAME |
0f2d19dd JB |
909 | |
910 | ||
a1ec6916 | 911 | SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0, |
1bbd0b84 | 912 | (SCM handle), |
e3239868 | 913 | "Return the vtable tag of the structure @var{handle}.") |
1bbd0b84 | 914 | #define FUNC_NAME s_scm_struct_vtable_tag |
0f2d19dd | 915 | { |
34d19ef6 | 916 | SCM_VALIDATE_VTABLE (1, handle); |
b9bd8526 | 917 | return scm_from_ulong (((unsigned long)SCM_STRUCT_DATA (handle)) >> 3); |
98d5f601 | 918 | } |
1bbd0b84 | 919 | #undef FUNC_NAME |
98d5f601 MD |
920 | |
921 | /* {Associating names and classes with vtables} | |
922 | * | |
923 | * The name of a vtable should probably be stored as a slot. This is | |
924 | * a backward compatible solution until agreement has been achieved on | |
925 | * how to associate names with vtables. | |
926 | */ | |
927 | ||
c014a02e | 928 | unsigned long |
d587c9e8 | 929 | scm_struct_ihashq (SCM obj, unsigned long n, void *closure) |
98d5f601 | 930 | { |
ad196599 MD |
931 | /* The length of the hash table should be a relative prime it's not |
932 | necessary to shift down the address. */ | |
f1267706 | 933 | return SCM_UNPACK (obj) % n; |
98d5f601 MD |
934 | } |
935 | ||
936 | SCM | |
937 | scm_struct_create_handle (SCM obj) | |
938 | { | |
939 | SCM handle = scm_hash_fn_create_handle_x (scm_struct_table, | |
940 | obj, | |
941 | SCM_BOOL_F, | |
942 | scm_struct_ihashq, | |
d587c9e8 | 943 | (scm_t_assoc_fn) scm_sloppy_assq, |
98d5f601 | 944 | 0); |
7888309b | 945 | if (scm_is_false (SCM_CDR (handle))) |
98d5f601 MD |
946 | SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F)); |
947 | return handle; | |
948 | } | |
949 | ||
a1ec6916 | 950 | SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0, |
1bbd0b84 | 951 | (SCM vtable), |
e3239868 | 952 | "Return the name of the vtable @var{vtable}.") |
1bbd0b84 | 953 | #define FUNC_NAME s_scm_struct_vtable_name |
98d5f601 | 954 | { |
34d19ef6 | 955 | SCM_VALIDATE_VTABLE (1, vtable); |
98d5f601 MD |
956 | return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable))); |
957 | } | |
1bbd0b84 | 958 | #undef FUNC_NAME |
98d5f601 | 959 | |
a1ec6916 | 960 | SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0, |
1bbd0b84 | 961 | (SCM vtable, SCM name), |
e3239868 | 962 | "Set the name of the vtable @var{vtable} to @var{name}.") |
1bbd0b84 | 963 | #define FUNC_NAME s_scm_set_struct_vtable_name_x |
98d5f601 | 964 | { |
34d19ef6 HWN |
965 | SCM_VALIDATE_VTABLE (1, vtable); |
966 | SCM_VALIDATE_SYMBOL (2, name); | |
98d5f601 MD |
967 | SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)), |
968 | name); | |
969 | return SCM_UNSPECIFIED; | |
0f2d19dd | 970 | } |
1bbd0b84 | 971 | #undef FUNC_NAME |
0f2d19dd JB |
972 | |
973 | ||
974 | \f | |
975 | ||
bafcafb2 | 976 | void |
1bbd0b84 | 977 | scm_print_struct (SCM exp, SCM port, scm_print_state *pstate) |
bafcafb2 | 978 | { |
7888309b | 979 | if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp)))) |
4bfdf158 MD |
980 | scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate); |
981 | else | |
bafcafb2 | 982 | { |
a1ae1799 MD |
983 | SCM vtable = SCM_STRUCT_VTABLE (exp); |
984 | SCM name = scm_struct_vtable_name (vtable); | |
985 | scm_puts ("#<", port); | |
7888309b | 986 | if (scm_is_true (name)) |
b6cf4d02 AW |
987 | { |
988 | scm_display (name, port); | |
989 | scm_putc (' ', port); | |
990 | } | |
a1ae1799 | 991 | else |
b6cf4d02 AW |
992 | { |
993 | if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE)) | |
994 | scm_puts ("vtable:", port); | |
995 | else | |
996 | scm_puts ("struct:", port); | |
997 | scm_uintprint (SCM_UNPACK (vtable), 16, port); | |
998 | scm_putc (' ', port); | |
999 | scm_write (SCM_VTABLE_LAYOUT (vtable), port); | |
1000 | scm_putc (' ', port); | |
1001 | } | |
0345e278 | 1002 | scm_uintprint (SCM_UNPACK (exp), 16, port); |
b6cf4d02 AW |
1003 | /* hackety hack */ |
1004 | if (SCM_STRUCT_APPLICABLE_P (exp)) | |
1005 | { | |
1006 | if (scm_is_true (SCM_STRUCT_PROCEDURE (exp))) | |
1007 | { | |
1008 | scm_puts (" proc: ", port); | |
1009 | if (scm_is_true (scm_procedure_p (SCM_STRUCT_PROCEDURE (exp)))) | |
1010 | scm_write (SCM_STRUCT_PROCEDURE (exp), port); | |
1011 | else | |
1012 | scm_puts ("(not a procedure?)", port); | |
1013 | } | |
1014 | if (SCM_STRUCT_SETTER_P (exp)) | |
1015 | { | |
1016 | scm_puts (" setter: ", port); | |
1017 | scm_write (SCM_STRUCT_SETTER (exp), port); | |
1018 | } | |
1019 | } | |
b7f3516f | 1020 | scm_putc ('>', port); |
bafcafb2 | 1021 | } |
bafcafb2 | 1022 | } |
1cc91f1b | 1023 | |
0f2d19dd JB |
1024 | void |
1025 | scm_init_struct () | |
0f2d19dd | 1026 | { |
01e74380 LC |
1027 | /* The first word of a struct is equal to `SCM_STRUCT_DATA (vtable) + |
1028 | scm_tc3_struct', and `SCM_STRUCT_DATA (vtable)' is 2 words after VTABLE by | |
1029 | default. */ | |
1030 | GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits) + scm_tc3_struct); | |
b6cf4d02 | 1031 | |
227eff6a LC |
1032 | /* In the general case, `SCM_STRUCT_DATA (obj)' points 2 words after the |
1033 | beginning of a GC-allocated region; that region is different from that of | |
1034 | OBJ once OBJ has undergone class redefinition. */ | |
1035 | GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits)); | |
1036 | ||
b6cf4d02 AW |
1037 | scm_struct_table = scm_make_weak_key_hash_table (scm_from_int (31)); |
1038 | required_vtable_fields = scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT); | |
1039 | required_applicable_fields = scm_from_locale_string (SCM_APPLICABLE_BASE_LAYOUT); | |
1040 | required_applicable_with_setter_fields = scm_from_locale_string (SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT); | |
651f2cd2 | 1041 | |
db5ed685 | 1042 | scm_standard_vtable_vtable = |
b6cf4d02 AW |
1043 | scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL); |
1044 | ||
1045 | scm_applicable_struct_vtable_vtable = | |
db5ed685 | 1046 | scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0, |
b6cf4d02 AW |
1047 | scm_list_1 (scm_make_struct_layout (required_vtable_fields))); |
1048 | SCM_SET_VTABLE_FLAGS (scm_applicable_struct_vtable_vtable, | |
1049 | SCM_VTABLE_FLAG_APPLICABLE_VTABLE); | |
1050 | scm_c_define ("<applicable-struct-vtable>", scm_applicable_struct_vtable_vtable); | |
1051 | ||
1052 | scm_applicable_struct_with_setter_vtable_vtable = | |
db5ed685 | 1053 | scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0, |
b6cf4d02 AW |
1054 | scm_list_1 (scm_make_struct_layout (required_vtable_fields))); |
1055 | SCM_SET_VTABLE_FLAGS (scm_applicable_struct_with_setter_vtable_vtable, | |
1056 | SCM_VTABLE_FLAG_APPLICABLE_VTABLE | SCM_VTABLE_FLAG_SETTER_VTABLE); | |
1057 | scm_c_define ("<applicable-struct-with-setter-vtable>", scm_applicable_struct_with_setter_vtable_vtable); | |
651f2cd2 | 1058 | |
e11e83f3 | 1059 | scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout)); |
86d31dfe | 1060 | scm_c_define ("vtable-index-printer", |
b6cf4d02 | 1061 | scm_from_int (scm_vtable_index_instance_printer)); |
e11e83f3 | 1062 | scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user)); |
a0599745 | 1063 | #include "libguile/struct.x" |
0f2d19dd | 1064 | } |
89e00824 ML |
1065 | |
1066 | /* | |
1067 | Local Variables: | |
1068 | c-file-style: "gnu" | |
1069 | End: | |
1070 | */ |