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