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