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