Merge from lcourtes@laas.fr--2005-mobile
[bpt/guile.git] / libguile / struct.c
1 /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 \f
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include "libguile/_scm.h"
24 #include "libguile/async.h"
25 #include "libguile/chars.h"
26 #include "libguile/eval.h"
27 #include "libguile/alist.h"
28 #include "libguile/weaks.h"
29 #include "libguile/hashtab.h"
30 #include "libguile/ports.h"
31 #include "libguile/strings.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/struct.h"
35
36 #include "libguile/eq.h"
37
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41
42 \f
43
44 static SCM required_vtable_fields = SCM_BOOL_F;
45 SCM scm_struct_table;
46
47 \f
48 SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
49 (SCM fields),
50 "Return a new structure layout object.\n\n"
51 "@var{fields} must be a string made up of pairs of characters\n"
52 "strung together. The first character of each pair describes a field\n"
53 "type, the second a field protection. Allowed types are 'p' for\n"
54 "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n"
55 "a field that points to the structure itself. Allowed protections\n"
56 "are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque\n"
57 "fields. The last field protection specification may be capitalized to\n"
58 "indicate that the field is a tail-array.")
59 #define FUNC_NAME s_scm_make_struct_layout
60 {
61 SCM new_sym;
62 SCM_VALIDATE_STRING (1, fields);
63
64 { /* scope */
65 const char * field_desc;
66 size_t len;
67 int x;
68
69 len = scm_i_string_length (fields);
70 if (len % 2 == 1)
71 SCM_MISC_ERROR ("odd length field specification: ~S",
72 scm_list_1 (fields));
73
74 field_desc = scm_i_string_chars (fields);
75
76 for (x = 0; x < len; x += 2)
77 {
78 switch (field_desc[x])
79 {
80 case 'u':
81 case 'p':
82 #if 0
83 case 'i':
84 case 'd':
85 #endif
86 case 's':
87 break;
88 default:
89 SCM_MISC_ERROR ("unrecognized field type: ~S",
90 scm_list_1 (SCM_MAKE_CHAR (field_desc[x])));
91 }
92
93 switch (field_desc[x + 1])
94 {
95 case 'w':
96 if (field_desc[x] == 's')
97 SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
98 case 'r':
99 case 'o':
100 break;
101 case 'R':
102 case 'W':
103 case 'O':
104 if (field_desc[x] == 's')
105 SCM_MISC_ERROR ("self fields not allowed in tail array",
106 SCM_EOL);
107 if (x != len - 2)
108 SCM_MISC_ERROR ("tail array field must be last field in layout",
109 SCM_EOL);
110 break;
111 default:
112 SCM_MISC_ERROR ("unrecognized ref specification: ~S",
113 scm_list_1 (SCM_MAKE_CHAR (field_desc[x + 1])));
114 }
115 #if 0
116 if (field_desc[x] == 'd')
117 {
118 if (field_desc[x + 2] != '-')
119 SCM_MISC_ERROR ("missing dash field at position ~A",
120 scm_list_1 (scm_from_int (x / 2)));
121 x += 2;
122 goto recheck_ref;
123 }
124 #endif
125 }
126 new_sym = scm_string_to_symbol (fields);
127 }
128 scm_remember_upto_here_1 (fields);
129 return new_sym;
130 }
131 #undef FUNC_NAME
132
133 \f
134
135
136
137 static void
138 scm_struct_init (SCM handle, SCM layout, scm_t_bits * mem, int tail_elts, SCM inits)
139 {
140 unsigned const char *fields_desc =
141 (unsigned const char *) scm_i_symbol_chars (layout) - 2;
142 unsigned char prot = 0;
143 int n_fields = scm_i_symbol_length (layout) / 2;
144 int tailp = 0;
145
146 while (n_fields)
147 {
148 if (!tailp)
149 {
150 fields_desc += 2;
151 prot = fields_desc[1];
152 if (SCM_LAYOUT_TAILP (prot))
153 {
154 tailp = 1;
155 prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o';
156 *mem++ = tail_elts;
157 n_fields += tail_elts - 1;
158 if (n_fields == 0)
159 break;
160 }
161 }
162
163 switch (*fields_desc)
164 {
165 #if 0
166 case 'i':
167 if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
168 *mem = 0;
169 else
170 {
171 *mem = scm_to_long (SCM_CAR (inits));
172 inits = SCM_CDR (inits);
173 }
174 break;
175 #endif
176
177 case 'u':
178 if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
179 *mem = 0;
180 else
181 {
182 *mem = scm_to_ulong (SCM_CAR (inits));
183 inits = SCM_CDR (inits);
184 }
185 break;
186
187 case 'p':
188 if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
189 *mem = SCM_UNPACK (SCM_BOOL_F);
190 else
191 {
192 *mem = SCM_UNPACK (SCM_CAR (inits));
193 inits = SCM_CDR (inits);
194 }
195
196 break;
197
198 #if 0
199 case 'd':
200 if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
201 *((double *)mem) = 0.0;
202 else
203 {
204 *mem = scm_num2dbl (SCM_CAR (inits), "scm_struct_init");
205 inits = SCM_CDR (inits);
206 }
207 fields_desc += 2;
208 break;
209 #endif
210
211 case 's':
212 *mem = SCM_UNPACK (handle);
213 break;
214 }
215
216 n_fields--;
217 mem++;
218 }
219 }
220
221
222 SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
223 (SCM x),
224 "Return @code{#t} iff @var{x} is a structure object, else\n"
225 "@code{#f}.")
226 #define FUNC_NAME s_scm_struct_p
227 {
228 return scm_from_bool(SCM_STRUCTP (x));
229 }
230 #undef FUNC_NAME
231
232 SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
233 (SCM x),
234 "Return @code{#t} iff @var{x} is a vtable structure.")
235 #define FUNC_NAME s_scm_struct_vtable_p
236 {
237 SCM layout;
238 scm_t_bits * mem;
239 int tmp;
240
241 if (!SCM_STRUCTP (x))
242 return SCM_BOOL_F;
243
244 layout = SCM_STRUCT_LAYOUT (x);
245
246 if (scm_i_symbol_length (layout)
247 < scm_i_string_length (required_vtable_fields))
248 return SCM_BOOL_F;
249
250 tmp = strncmp (scm_i_symbol_chars (layout),
251 scm_i_string_chars (required_vtable_fields),
252 scm_i_string_length (required_vtable_fields));
253 scm_remember_upto_here_1 (required_vtable_fields);
254 if (tmp)
255 return SCM_BOOL_F;
256
257 mem = SCM_STRUCT_DATA (x);
258
259 return scm_from_bool (scm_is_symbol (SCM_PACK (mem[scm_vtable_index_layout])));
260 }
261 #undef FUNC_NAME
262
263
264 /* All struct data must be allocated at an address whose bottom three
265 bits are zero. This is because the tag for a struct lives in the
266 bottom three bits of the struct's car, and the upper bits point to
267 the data of its vtable, which is a struct itself. Thus, if the
268 address of that data doesn't end in three zeros, tagging it will
269 destroy the pointer.
270
271 This function allocates a block of memory, and returns a pointer at
272 least scm_struct_n_extra_words words into the block. Furthermore,
273 it guarantees that that pointer's least three significant bits are
274 all zero.
275
276 The argument n_words should be the number of words that should
277 appear after the returned address. (That is, it shouldn't include
278 scm_struct_n_extra_words.)
279
280 This function initializes the following fields of the struct:
281
282 scm_struct_i_ptr --- the actual start of the block of memory; the
283 address you should pass to 'free' to dispose of the block.
284 This field allows us to both guarantee that the returned
285 address is divisible by eight, and allow the GC to free the
286 block.
287
288 scm_struct_i_n_words --- the number of words allocated to the
289 block, including the extra fields. This is used by the GC.
290
291 Ugh. */
292
293
294 scm_t_bits *
295 scm_alloc_struct (int n_words, int n_extra, const char *what)
296 {
297 int size = sizeof (scm_t_bits) * (n_words + n_extra) + 7;
298 void * block = scm_gc_malloc (size, what);
299
300 /* Adjust the pointer to hide the extra words. */
301 scm_t_bits * p = (scm_t_bits *) block + n_extra;
302
303 /* Adjust it even further so it's aligned on an eight-byte boundary. */
304 p = (scm_t_bits *) (((scm_t_bits) p + 7) & ~7);
305
306 /* Initialize a few fields as described above. */
307 p[scm_struct_i_free] = (scm_t_bits) scm_struct_free_standard;
308 p[scm_struct_i_ptr] = (scm_t_bits) block;
309 p[scm_struct_i_n_words] = n_words;
310 p[scm_struct_i_flags] = 0;
311
312 return p;
313 }
314
315 void
316 scm_struct_free_0 (scm_t_bits * vtable SCM_UNUSED,
317 scm_t_bits * data SCM_UNUSED)
318 {
319 }
320
321 void
322 scm_struct_free_light (scm_t_bits * vtable, scm_t_bits * data)
323 {
324 size_t n = vtable [scm_struct_i_size] & ~SCM_STRUCTF_MASK;
325 scm_gc_free (data, n, "struct");
326 }
327
328 void
329 scm_struct_free_standard (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
330 {
331 size_t n = (data[scm_struct_i_n_words] + scm_struct_n_extra_words)
332 * sizeof (scm_t_bits) + 7;
333 scm_gc_free ((void *) data[scm_struct_i_ptr], n, "heavy struct");
334 }
335
336 void
337 scm_struct_free_entity (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
338 {
339 size_t n = (data[scm_struct_i_n_words] + scm_struct_entity_n_extra_words)
340 * sizeof (scm_t_bits) + 7;
341 scm_gc_free ((void *) data[scm_struct_i_ptr], n, "entity struct");
342 }
343
344 static void *
345 scm_struct_gc_init (void *dummy1 SCM_UNUSED,
346 void *dummy2 SCM_UNUSED,
347 void *dummy3 SCM_UNUSED)
348 {
349 scm_i_structs_to_free = SCM_EOL;
350 return 0;
351 }
352
353
354 /* During collection, this accumulates structures which are to be freed.
355 */
356 SCM scm_i_structs_to_free;
357
358 static void *
359 scm_free_structs (void *dummy1 SCM_UNUSED,
360 void *dummy2 SCM_UNUSED,
361 void *dummy3 SCM_UNUSED)
362 {
363 #if 0
364 SCM newchain = scm_i_structs_to_free;
365 do
366 {
367 /* Mark vtables in GC chain. GC mark set means delay freeing. */
368 SCM chain = newchain;
369 while (!scm_is_null (chain))
370 {
371 SCM vtable = SCM_STRUCT_VTABLE (chain);
372 if (SCM_STRUCT_GC_CHAIN (vtable) != 0 && vtable != chain)
373 SCM_SET_GC_MARK (vtable);
374 chain = SCM_STRUCT_GC_CHAIN (chain);
375 }
376 /* Free unmarked structs. */
377 chain = newchain;
378 newchain = SCM_EOL;
379 while (!scm_is_null (chain))
380 {
381 SCM obj = chain;
382 chain = SCM_STRUCT_GC_CHAIN (chain);
383 if (SCM_GC_MARK_P (obj))
384 {
385 SCM_CLEAR_GC_MARK (obj);
386 SCM_SET_STRUCT_GC_CHAIN (obj, newchain);
387 newchain = obj;
388 }
389 else
390 {
391 scm_t_bits * vtable_data = SCM_STRUCT_VTABLE_DATA (obj);
392 scm_t_bits * data = SCM_STRUCT_DATA (obj);
393 scm_t_struct_free free_struct_data
394 = ((scm_t_struct_free) vtable_data[scm_struct_i_free]);
395 SCM_SET_CELL_TYPE (obj, scm_tc_free_cell);
396 free_struct_data (vtable_data, data);
397 }
398 }
399 }
400 while (!scm_is_null (newchain));
401 return 0;
402 #endif
403 }
404
405 SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
406 (SCM vtable, SCM tail_array_size, SCM init),
407 "Create a new structure.\n\n"
408 "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n"
409 "@var{tail-elts} must be a non-negative integer. If the layout\n"
410 "specification indicated by @var{type} includes a tail-array,\n"
411 "this is the number of elements allocated to that array.\n\n"
412 "The @var{init1}, @dots{} are optional arguments describing how\n"
413 "successive fields of the structure should be initialized. Only fields\n"
414 "with protection 'r' or 'w' can be initialized, except for fields of\n"
415 "type 's', which are automatically initialized to point to the new\n"
416 "structure itself; fields with protection 'o' can not be initialized by\n"
417 "Scheme programs.\n\n"
418 "If fewer optional arguments than initializable fields are supplied,\n"
419 "fields of type 'p' get default value #f while fields of type 'u' are\n"
420 "initialized to 0.\n\n"
421 "Structs are currently the basic representation for record-like data\n"
422 "structures in Guile. The plan is to eventually replace them with a\n"
423 "new representation which will at the same time be easier to use and\n"
424 "more powerful.\n\n"
425 "For more information, see the documentation for @code{make-vtable-vtable}.")
426 #define FUNC_NAME s_scm_make_struct
427 {
428 SCM layout;
429 size_t basic_size;
430 size_t tail_elts;
431 scm_t_bits * data;
432 SCM handle;
433
434 SCM_VALIDATE_VTABLE (1, vtable);
435 SCM_VALIDATE_REST_ARGUMENT (init);
436
437 layout = SCM_PACK (SCM_STRUCT_DATA (vtable) [scm_vtable_index_layout]);
438 basic_size = scm_i_symbol_length (layout) / 2;
439 tail_elts = scm_to_size_t (tail_array_size);
440 SCM_CRITICAL_SECTION_START;
441 if (SCM_STRUCT_DATA (vtable)[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
442 {
443 data = scm_alloc_struct (basic_size + tail_elts,
444 scm_struct_entity_n_extra_words,
445 "entity struct");
446 data[scm_struct_i_procedure] = SCM_UNPACK (SCM_BOOL_F);
447 data[scm_struct_i_setter] = SCM_UNPACK (SCM_BOOL_F);
448 }
449 else
450 data = scm_alloc_struct (basic_size + tail_elts,
451 scm_struct_n_extra_words,
452 "struct");
453 handle = scm_double_cell ((((scm_t_bits) SCM_STRUCT_DATA (vtable))
454 + scm_tc3_struct),
455 (scm_t_bits) data, 0, 0);
456 scm_struct_init (handle, layout, data, tail_elts, init);
457 SCM_CRITICAL_SECTION_END;
458 return handle;
459 }
460 #undef FUNC_NAME
461
462
463
464 SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1,
465 (SCM user_fields, SCM tail_array_size, SCM init),
466 "Return a new, self-describing vtable structure.\n\n"
467 "@var{user-fields} is a string describing user defined fields of the\n"
468 "vtable beginning at index @code{vtable-offset-user}\n"
469 "(see @code{make-struct-layout}).\n\n"
470 "@var{tail-size} specifies the size of the tail-array (if any) of\n"
471 "this vtable.\n\n"
472 "@var{init1}, @dots{} are the optional initializers for the fields of\n"
473 "the vtable.\n\n"
474 "Vtables have one initializable system field---the struct printer.\n"
475 "This field comes before the user fields in the initializers passed\n"
476 "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n"
477 "a third optional argument to @code{make-vtable-vtable} and a fourth to\n"
478 "@code{make-struct} when creating vtables:\n\n"
479 "If the value is a procedure, it will be called instead of the standard\n"
480 "printer whenever a struct described by this vtable is printed.\n"
481 "The procedure will be called with arguments STRUCT and PORT.\n\n"
482 "The structure of a struct is described by a vtable, so the vtable is\n"
483 "in essence the type of the struct. The vtable is itself a struct with\n"
484 "a vtable. This could go on forever if it weren't for the\n"
485 "vtable-vtables which are self-describing vtables, and thus terminate\n"
486 "the chain.\n\n"
487 "There are several potential ways of using structs, but the standard\n"
488 "one is to use three kinds of structs, together building up a type\n"
489 "sub-system: one vtable-vtable working as the root and one or several\n"
490 "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n"
491 "compared to the class <class> which is the class of itself.)\n\n"
492 "@lisp\n"
493 "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n"
494 "(define (make-ball-type ball-color)\n"
495 " (make-struct ball-root 0\n"
496 " (make-struct-layout \"pw\")\n"
497 " (lambda (ball port)\n"
498 " (format port \"#<a ~A ball owned by ~A>\"\n"
499 " (color ball)\n"
500 " (owner ball)))\n"
501 " ball-color))\n"
502 "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n"
503 "(define (owner ball) (struct-ref ball 0))\n\n"
504 "(define red (make-ball-type 'red))\n"
505 "(define green (make-ball-type 'green))\n\n"
506 "(define (make-ball type owner) (make-struct type 0 owner))\n\n"
507 "(define ball (make-ball green 'Nisse))\n"
508 "ball @result{} #<a green ball owned by Nisse>\n"
509 "@end lisp")
510 #define FUNC_NAME s_scm_make_vtable_vtable
511 {
512 SCM fields;
513 SCM layout;
514 size_t basic_size;
515 size_t tail_elts;
516 scm_t_bits *data;
517 SCM handle;
518
519 SCM_VALIDATE_STRING (1, user_fields);
520 SCM_VALIDATE_REST_ARGUMENT (init);
521
522 fields = scm_string_append (scm_list_2 (required_vtable_fields,
523 user_fields));
524 layout = scm_make_struct_layout (fields);
525 basic_size = scm_i_symbol_length (layout) / 2;
526 tail_elts = scm_to_size_t (tail_array_size);
527 SCM_CRITICAL_SECTION_START;
528 data = scm_alloc_struct (basic_size + tail_elts,
529 scm_struct_n_extra_words,
530 "struct");
531 handle = scm_double_cell ((scm_t_bits) data + scm_tc3_struct,
532 (scm_t_bits) data, 0, 0);
533 data [scm_vtable_index_layout] = SCM_UNPACK (layout);
534 scm_struct_init (handle, layout, data, tail_elts, scm_cons (layout, init));
535 SCM_CRITICAL_SECTION_END;
536 return handle;
537 }
538 #undef FUNC_NAME
539
540
541 /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
542 contents are the same. Field protections are honored. Thus, it is an
543 error to test the equality of structures that contain opaque fields. */
544 SCM
545 scm_i_struct_equalp (SCM s1, SCM s2)
546 #define FUNC_NAME "scm_i_struct_equalp"
547 {
548 SCM vtable1, vtable2, layout;
549 size_t struct_size, field_num;
550
551 SCM_VALIDATE_STRUCT (1, s1);
552 SCM_VALIDATE_STRUCT (2, s2);
553
554 vtable1 = SCM_STRUCT_VTABLE (s1);
555 vtable2 = SCM_STRUCT_VTABLE (s2);
556
557 if (!scm_is_eq (vtable1, vtable2))
558 return SCM_BOOL_F;
559
560 layout = SCM_STRUCT_LAYOUT (s1);
561 struct_size = scm_i_symbol_length (layout) / 2;
562
563 for (field_num = 0; field_num < struct_size; field_num++)
564 {
565 SCM s_field_num;
566 SCM field1, field2;
567
568 /* We have to use `scm_struct_ref ()' here so that fields are accessed
569 consistently, notably wrt. field types and access rights. */
570 s_field_num = scm_from_size_t (field_num);
571 field1 = scm_struct_ref (s1, s_field_num);
572 field2 = scm_struct_ref (s2, s_field_num);
573
574 if (scm_is_false (scm_equal_p (field1, field2)))
575 return SCM_BOOL_F;
576 }
577
578 return SCM_BOOL_T;
579 }
580 #undef FUNC_NAME
581
582
583 \f
584
585
586 SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
587 (SCM handle, SCM pos),
588 "@deffnx {Scheme Procedure} struct-set! struct n value\n"
589 "Access (or modify) the @var{n}th field of @var{struct}.\n\n"
590 "If the field is of type 'p', then it can be set to an arbitrary value.\n\n"
591 "If the field is of type 'u', then it can only be set to a non-negative\n"
592 "integer value small enough to fit in one machine word.")
593 #define FUNC_NAME s_scm_struct_ref
594 {
595 SCM answer = SCM_UNDEFINED;
596 scm_t_bits * data;
597 SCM layout;
598 size_t layout_len;
599 size_t p;
600 scm_t_bits n_fields;
601 const char *fields_desc;
602 char field_type = 0;
603
604
605 SCM_VALIDATE_STRUCT (1, handle);
606
607 layout = SCM_STRUCT_LAYOUT (handle);
608 data = SCM_STRUCT_DATA (handle);
609 p = scm_to_size_t (pos);
610
611 fields_desc = scm_i_symbol_chars (layout);
612 layout_len = scm_i_symbol_length (layout);
613 n_fields = data[scm_struct_i_n_words];
614
615 SCM_ASSERT_RANGE(1, pos, p < n_fields);
616
617 if (p * 2 < layout_len)
618 {
619 char ref;
620 field_type = fields_desc[p * 2];
621 ref = fields_desc[p * 2 + 1];
622 if ((ref != 'r') && (ref != 'w'))
623 {
624 if ((ref == 'R') || (ref == 'W'))
625 field_type = 'u';
626 else
627 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
628 }
629 }
630 else if (fields_desc[layout_len - 1] != 'O')
631 field_type = fields_desc[layout_len - 2];
632 else
633 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
634
635 switch (field_type)
636 {
637 case 'u':
638 answer = scm_from_ulong (data[p]);
639 break;
640
641 #if 0
642 case 'i':
643 answer = scm_from_long (data[p]);
644 break;
645
646 case 'd':
647 answer = scm_make_real (*((double *)&(data[p])));
648 break;
649 #endif
650
651 case 's':
652 case 'p':
653 answer = SCM_PACK (data[p]);
654 break;
655
656
657 default:
658 SCM_MISC_ERROR ("unrecognized field type: ~S",
659 scm_list_1 (SCM_MAKE_CHAR (field_type)));
660 }
661
662 return answer;
663 }
664 #undef FUNC_NAME
665
666
667 SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
668 (SCM handle, SCM pos, SCM val),
669 "Set the slot of the structure @var{handle} with index @var{pos}\n"
670 "to @var{val}. Signal an error if the slot can not be written\n"
671 "to.")
672 #define FUNC_NAME s_scm_struct_set_x
673 {
674 scm_t_bits * data;
675 SCM layout;
676 size_t layout_len;
677 size_t p;
678 int n_fields;
679 const char *fields_desc;
680 char field_type = 0;
681
682 SCM_VALIDATE_STRUCT (1, handle);
683
684 layout = SCM_STRUCT_LAYOUT (handle);
685 data = SCM_STRUCT_DATA (handle);
686 p = scm_to_size_t (pos);
687
688 fields_desc = scm_i_symbol_chars (layout);
689 layout_len = scm_i_symbol_length (layout);
690 n_fields = data[scm_struct_i_n_words];
691
692 SCM_ASSERT_RANGE (1, pos, p < n_fields);
693
694 if (p * 2 < layout_len)
695 {
696 char set_x;
697 field_type = fields_desc[p * 2];
698 set_x = fields_desc [p * 2 + 1];
699 if (set_x != 'w')
700 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
701 }
702 else if (fields_desc[layout_len - 1] == 'W')
703 field_type = fields_desc[layout_len - 2];
704 else
705 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
706
707 switch (field_type)
708 {
709 case 'u':
710 data[p] = SCM_NUM2ULONG (3, val);
711 break;
712
713 #if 0
714 case 'i':
715 data[p] = SCM_NUM2LONG (3, val);
716 break;
717
718 case 'd':
719 *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
720 break;
721 #endif
722
723 case 'p':
724 data[p] = SCM_UNPACK (val);
725 break;
726
727 case 's':
728 SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
729
730 default:
731 SCM_MISC_ERROR ("unrecognized field type: ~S",
732 scm_list_1 (SCM_MAKE_CHAR (field_type)));
733 }
734
735 return val;
736 }
737 #undef FUNC_NAME
738
739
740 SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
741 (SCM handle),
742 "Return the vtable structure that describes the type of @var{struct}.")
743 #define FUNC_NAME s_scm_struct_vtable
744 {
745 SCM_VALIDATE_STRUCT (1, handle);
746 return SCM_STRUCT_VTABLE (handle);
747 }
748 #undef FUNC_NAME
749
750
751 SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0,
752 (SCM handle),
753 "Return the vtable tag of the structure @var{handle}.")
754 #define FUNC_NAME s_scm_struct_vtable_tag
755 {
756 SCM_VALIDATE_VTABLE (1, handle);
757 return scm_from_ulong (((unsigned long)SCM_STRUCT_DATA (handle)) >> 3);
758 }
759 #undef FUNC_NAME
760
761 /* {Associating names and classes with vtables}
762 *
763 * The name of a vtable should probably be stored as a slot. This is
764 * a backward compatible solution until agreement has been achieved on
765 * how to associate names with vtables.
766 */
767
768 unsigned long
769 scm_struct_ihashq (SCM obj, unsigned long n)
770 {
771 /* The length of the hash table should be a relative prime it's not
772 necessary to shift down the address. */
773 return SCM_UNPACK (obj) % n;
774 }
775
776 SCM
777 scm_struct_create_handle (SCM obj)
778 {
779 SCM handle = scm_hash_fn_create_handle_x (scm_struct_table,
780 obj,
781 SCM_BOOL_F,
782 scm_struct_ihashq,
783 scm_sloppy_assq,
784 0);
785 if (scm_is_false (SCM_CDR (handle)))
786 SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F));
787 return handle;
788 }
789
790 SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
791 (SCM vtable),
792 "Return the name of the vtable @var{vtable}.")
793 #define FUNC_NAME s_scm_struct_vtable_name
794 {
795 SCM_VALIDATE_VTABLE (1, vtable);
796 return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)));
797 }
798 #undef FUNC_NAME
799
800 SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
801 (SCM vtable, SCM name),
802 "Set the name of the vtable @var{vtable} to @var{name}.")
803 #define FUNC_NAME s_scm_set_struct_vtable_name_x
804 {
805 SCM_VALIDATE_VTABLE (1, vtable);
806 SCM_VALIDATE_SYMBOL (2, name);
807 SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)),
808 name);
809 return SCM_UNSPECIFIED;
810 }
811 #undef FUNC_NAME
812
813
814 \f
815
816 void
817 scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
818 {
819 if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
820 scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
821 else
822 {
823 SCM vtable = SCM_STRUCT_VTABLE (exp);
824 SCM name = scm_struct_vtable_name (vtable);
825 scm_puts ("#<", port);
826 if (scm_is_true (name))
827 scm_display (name, port);
828 else
829 scm_puts ("struct", port);
830 scm_putc (' ', port);
831 scm_uintprint (SCM_UNPACK (vtable), 16, port);
832 scm_putc (':', port);
833 scm_uintprint (SCM_UNPACK (exp), 16, port);
834 scm_putc ('>', port);
835 }
836 }
837
838 void
839 scm_struct_prehistory ()
840 {
841 scm_i_structs_to_free = SCM_EOL;
842 scm_c_hook_add (&scm_before_sweep_c_hook, scm_struct_gc_init, 0, 0);
843 /* With the new lazy sweep GC, the point at which the entire heap is
844 swept is just before the mark phase. */
845 scm_c_hook_add (&scm_before_mark_c_hook, scm_free_structs, 0, 0);
846 }
847
848 void
849 scm_init_struct ()
850 {
851 scm_struct_table
852 = scm_permanent_object (scm_make_weak_key_hash_table (scm_from_int (31)));
853 required_vtable_fields = scm_from_locale_string ("prsrpw");
854 scm_permanent_object (required_vtable_fields);
855 scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
856 scm_c_define ("vtable-index-vtable", scm_from_int (scm_vtable_index_vtable));
857 scm_c_define ("vtable-index-printer",
858 scm_from_int (scm_vtable_index_printer));
859 scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
860 #include "libguile/struct.x"
861 }
862
863 /*
864 Local Variables:
865 c-file-style: "gnu"
866 End:
867 */