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