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