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