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