Merge commit '5af307de43e4b65eec7f235b48a8908f2a00f134'
[bpt/guile.git] / libguile / struct.c
1 /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007,
2 * 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <alloca.h>
26 #include <assert.h>
27
28 #define SCM_BUILDING_DEPRECATED_CODE
29
30 #include "libguile/_scm.h"
31 #include "libguile/async.h"
32 #include "libguile/chars.h"
33 #include "libguile/eval.h"
34 #include "libguile/alist.h"
35 #include "libguile/hashtab.h"
36 #include "libguile/ports.h"
37 #include "libguile/strings.h"
38 #include "libguile/srfi-13.h"
39
40 #include "libguile/validate.h"
41 #include "libguile/struct.h"
42
43 #include "libguile/eq.h"
44
45 #ifdef HAVE_STRING_H
46 #include <string.h>
47 #endif
48
49 #include "libguile/bdw-gc.h"
50
51 \f
52
53 /* A needlessly obscure test. */
54 #define SCM_LAYOUT_TAILP(X) (((X) & 32) == 0) /* R, W or O */
55
56 static SCM required_vtable_fields = SCM_BOOL_F;
57 static SCM required_applicable_fields = SCM_BOOL_F;
58 static SCM required_applicable_with_setter_fields = SCM_BOOL_F;
59 SCM scm_applicable_struct_vtable_vtable;
60 SCM scm_applicable_struct_with_setter_vtable_vtable;
61 SCM scm_standard_vtable_vtable;
62
63
64 \f
65 SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
66 (SCM fields),
67 "Return a new structure layout object.\n\n"
68 "@var{fields} must be a string made up of pairs of characters\n"
69 "strung together. The first character of each pair describes a field\n"
70 "type, the second a field protection. Allowed types are 'p' for\n"
71 "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n"
72 "a field that points to the structure itself. Allowed protections\n"
73 "are 'w' for mutable fields, 'h' for hidden fields, 'r' for read-only\n"
74 "fields, and 'o' for opaque fields.\n\n"
75 "Hidden fields are writable, but they will not consume an initializer arg\n"
76 "passed to @code{make-struct}. They are useful to add slots to a struct\n"
77 "in a way that preserves backward-compatibility with existing calls to\n"
78 "@code{make-struct}, especially for derived vtables.\n\n"
79 "The last field protection specification may be capitalized to indicate\n"
80 "that the field is a tail-array.")
81 #define FUNC_NAME s_scm_make_struct_layout
82 {
83 SCM new_sym;
84 scm_t_wchar c;
85
86 SCM_VALIDATE_STRING (1, fields);
87
88 { /* scope */
89 size_t len;
90 int x;
91
92 len = scm_i_string_length (fields);
93 if (len % 2 == 1)
94 SCM_MISC_ERROR ("odd length field specification: ~S",
95 scm_list_1 (fields));
96
97 for (x = 0; x < len; x += 2)
98 {
99 switch (c = scm_i_string_ref (fields, 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:
110 SCM_MISC_ERROR ("unrecognized field type: ~S",
111 scm_list_1 (SCM_MAKE_CHAR (c)));
112 }
113
114 switch (c = scm_i_string_ref (fields, x + 1))
115 {
116 case 'w':
117 case 'h':
118 if (scm_i_string_ref (fields, x) == 's')
119 SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
120 case 'r':
121 case 'o':
122 break;
123 case 'R':
124 case 'W':
125 case 'O':
126 if (scm_i_string_ref (fields, x) == 's')
127 SCM_MISC_ERROR ("self fields not allowed in tail array",
128 SCM_EOL);
129 if (x != len - 2)
130 SCM_MISC_ERROR ("tail array field must be last field in layout",
131 SCM_EOL);
132 break;
133 default:
134 SCM_MISC_ERROR ("unrecognized ref specification: ~S",
135 scm_list_1 (SCM_MAKE_CHAR (c)));
136 }
137 #if 0
138 if (scm_i_string_ref (fields, x, 'd'))
139 {
140 if (!scm_i_string_ref (fields, x+2, '-'))
141 SCM_MISC_ERROR ("missing dash field at position ~A",
142 scm_list_1 (scm_from_int (x / 2)));
143 x += 2;
144 goto recheck_ref;
145 }
146 #endif
147 }
148 new_sym = scm_string_to_symbol (fields);
149 }
150 scm_remember_upto_here_1 (fields);
151 return new_sym;
152 }
153 #undef FUNC_NAME
154
155 \f
156 /* Check whether VTABLE instances have a simple layout (i.e., either
157 only "pr" or only "pw" fields and no tail array) and update its flags
158 accordingly. */
159 static void
160 set_vtable_layout_flags (SCM vtable)
161 {
162 size_t len, field;
163 SCM layout;
164 const char *c_layout;
165 scm_t_bits flags = SCM_VTABLE_FLAG_SIMPLE;
166
167 layout = SCM_VTABLE_LAYOUT (vtable);
168 c_layout = scm_i_symbol_chars (layout);
169 len = scm_i_symbol_length (layout);
170
171 assert (len % 2 == 0);
172
173 /* Update FLAGS according to LAYOUT. */
174 for (field = 0;
175 field < len && flags & SCM_VTABLE_FLAG_SIMPLE;
176 field += 2)
177 {
178 if (c_layout[field] != 'p')
179 flags = 0;
180 else
181 switch (c_layout[field + 1])
182 {
183 case 'w':
184 if (field == 0)
185 flags |= SCM_VTABLE_FLAG_SIMPLE_RW;
186 break;
187
188 case 'r':
189 flags &= ~SCM_VTABLE_FLAG_SIMPLE_RW;
190 break;
191
192 default:
193 flags = 0;
194 }
195 }
196
197 if (flags & SCM_VTABLE_FLAG_SIMPLE)
198 {
199 /* VTABLE is simple so update its flags and record the size of its
200 instances. */
201 SCM_SET_VTABLE_FLAGS (vtable, flags);
202 SCM_STRUCT_DATA_SET (vtable, scm_vtable_index_size, len / 2);
203 }
204 }
205
206 static int
207 scm_is_valid_vtable_layout (SCM layout)
208 {
209 size_t len, n;
210 const char *c_layout;
211
212 c_layout = scm_i_symbol_chars (layout);
213 len = scm_i_symbol_length (layout);
214
215 if (len % 2)
216 return 0;
217
218 for (n = 0; n < len; n += 2)
219 switch (c_layout[n])
220 {
221 case 'u':
222 case 'p':
223 case 's':
224 switch (c_layout[n+1])
225 {
226 case 'W':
227 case 'R':
228 case 'O':
229 if (n + 2 != len)
230 return 0;
231 case 'w':
232 case 'h':
233 case 'r':
234 case 'o':
235 break;
236 default:
237 return 0;
238 }
239 break;
240 default:
241 return 0;
242 }
243 return 1;
244 }
245
246 /* Have OBJ, a newly created vtable, inherit flags from VTABLE. VTABLE is a
247 vtable-vtable and OBJ is an instance of VTABLE. */
248 void
249 scm_i_struct_inherit_vtable_magic (SCM vtable, SCM obj)
250 #define FUNC_NAME "%inherit-vtable-magic"
251 {
252 /* Verily, what is the deal here, you ask? Basically, we need to know a couple
253 of properties of structures at runtime. For example, "is this structure a
254 vtable of vtables (a metaclass)?"; also, "is this structure applicable?".
255 Both of these questions also imply a certain layout of the structure. So
256 instead of checking the layout at runtime, what we do is pre-verify the
257 layout -- so that at runtime we can just check the applicable flag and
258 dispatch directly to the Scheme procedure in slot 0. */
259 SCM olayout;
260
261 /* Verify that OBJ is a valid vtable. */
262 if (! scm_is_valid_vtable_layout (SCM_VTABLE_LAYOUT (obj)))
263 SCM_MISC_ERROR ("invalid layout for new vtable: ~a",
264 scm_list_1 (SCM_VTABLE_LAYOUT (obj)));
265
266 set_vtable_layout_flags (obj);
267
268 /* If OBJ's vtable is compatible with the required vtable (class) layout, it
269 is a metaclass. */
270 olayout = scm_symbol_to_string (SCM_VTABLE_LAYOUT (obj));
271 if (scm_is_true (scm_leq_p (scm_string_length (required_vtable_fields),
272 scm_string_length (olayout)))
273 && scm_is_true (scm_string_eq (olayout, required_vtable_fields,
274 scm_from_size_t (0),
275 scm_string_length (required_vtable_fields),
276 scm_from_size_t (0),
277 scm_string_length (required_vtable_fields))))
278 SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VTABLE);
279
280 /* Finally, if OBJ is an applicable class, verify that its vtable is
281 compatible with the required applicable layout. */
282 if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SETTER_VTABLE))
283 {
284 if (scm_is_false (scm_string_eq (olayout, required_applicable_with_setter_fields,
285 scm_from_size_t (0),
286 scm_from_size_t (4),
287 scm_from_size_t (0),
288 scm_from_size_t (4))))
289 SCM_MISC_ERROR ("invalid applicable-with-setter struct layout",
290 scm_list_1 (olayout));
291 SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE | SCM_VTABLE_FLAG_SETTER);
292 }
293 else if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_APPLICABLE_VTABLE))
294 {
295 if (scm_is_false (scm_string_eq (olayout, required_applicable_fields,
296 scm_from_size_t (0),
297 scm_from_size_t (2),
298 scm_from_size_t (0),
299 scm_from_size_t (2))))
300 SCM_MISC_ERROR ("invalid applicable struct layout",
301 scm_list_1 (olayout));
302 SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE);
303 }
304
305 SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VALIDATED);
306 }
307 #undef FUNC_NAME
308
309
310 static void
311 scm_struct_init (SCM handle, SCM layout, size_t n_tail,
312 size_t n_inits, scm_t_bits *inits)
313 {
314 SCM vtable;
315 scm_t_bits *mem;
316
317 vtable = SCM_STRUCT_VTABLE (handle);
318 mem = SCM_STRUCT_DATA (handle);
319
320 if (SCM_UNPACK (vtable) != 0
321 && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
322 && n_tail == 0
323 && n_inits == SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size))
324 /* The fast path: HANDLE has N_INITS "p" fields. */
325 memcpy (mem, inits, n_inits * sizeof (SCM));
326 else
327 {
328 scm_t_wchar prot = 0;
329 int n_fields = scm_i_symbol_length (layout) / 2;
330 int tailp = 0;
331 int i;
332 size_t inits_idx = 0;
333
334 i = -2;
335 while (n_fields)
336 {
337 if (!tailp)
338 {
339 i += 2;
340 prot = scm_i_symbol_ref (layout, i+1);
341 if (SCM_LAYOUT_TAILP (prot))
342 {
343 tailp = 1;
344 prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o';
345 *mem++ = (scm_t_bits)n_tail;
346 n_fields += n_tail - 1;
347 if (n_fields == 0)
348 break;
349 }
350 }
351 switch (scm_i_symbol_ref (layout, i))
352 {
353 case 'u':
354 if ((prot != 'r' && prot != 'w') || inits_idx == n_inits)
355 *mem = 0;
356 else
357 {
358 *mem = scm_to_ulong (SCM_PACK (inits[inits_idx]));
359 inits_idx++;
360 }
361 break;
362
363 case 'p':
364 if ((prot != 'r' && prot != 'w') || inits_idx == n_inits)
365 *mem = SCM_UNPACK (SCM_BOOL_F);
366 else
367 {
368 *mem = inits[inits_idx];
369 inits_idx++;
370 }
371
372 break;
373
374 case 's':
375 *mem = SCM_UNPACK (handle);
376 break;
377 }
378
379 n_fields--;
380 mem++;
381 }
382 }
383 }
384
385
386 SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
387 (SCM x),
388 "Return @code{#t} iff @var{x} is a structure object, else\n"
389 "@code{#f}.")
390 #define FUNC_NAME s_scm_struct_p
391 {
392 return scm_from_bool(SCM_STRUCTP (x));
393 }
394 #undef FUNC_NAME
395
396 SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
397 (SCM x),
398 "Return @code{#t} iff @var{x} is a vtable structure.")
399 #define FUNC_NAME s_scm_struct_vtable_p
400 {
401 if (!SCM_STRUCTP (x)
402 || !SCM_STRUCT_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VTABLE))
403 return SCM_BOOL_F;
404 if (!SCM_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VALIDATED))
405 SCM_MISC_ERROR ("vtable has invalid layout: ~A",
406 scm_list_1 (SCM_VTABLE_LAYOUT (x)));
407 return SCM_BOOL_T;
408 }
409 #undef FUNC_NAME
410
411
412 /* Finalization: invoke the finalizer of the struct pointed to by PTR. */
413 static void
414 struct_finalizer_trampoline (void *ptr, void *unused_data)
415 {
416 SCM obj = PTR2SCM (ptr);
417 scm_t_struct_finalize finalize = SCM_STRUCT_FINALIZER (obj);
418
419 if (finalize)
420 finalize (obj);
421 }
422
423 /* All struct data must be allocated at an address whose bottom three
424 bits are zero. This is because the tag for a struct lives in the
425 bottom three bits of the struct's car, and the upper bits point to
426 the data of its vtable, which is a struct itself. Thus, if the
427 address of that data doesn't end in three zeros, tagging it will
428 destroy the pointer.
429
430 I suppose we should make it clear here that, the data must be 8-byte aligned,
431 *within* the struct, and the struct itself should be 8-byte aligned. In
432 practice we ensure this because the data starts two words into a struct.
433
434 This function allocates an 8-byte aligned block of memory, whose first word
435 points to the given vtable data, then a data pointer, then n_words of data.
436 */
437 SCM
438 scm_i_alloc_struct (scm_t_bits *vtable_data, int n_words)
439 {
440 SCM ret;
441
442 ret = scm_words ((scm_t_bits)vtable_data | scm_tc3_struct, n_words + 2);
443 SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)SCM_CELL_OBJECT_LOC (ret, 2));
444
445 /* vtable_data can be null when making a vtable vtable */
446 if (vtable_data && vtable_data[scm_vtable_index_instance_finalize])
447 /* Register a finalizer for the newly created instance. */
448 scm_i_set_finalizer (SCM2PTR (ret), struct_finalizer_trampoline, NULL);
449
450 return ret;
451 }
452
453 \f
454 SCM
455 scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits *init)
456 #define FUNC_NAME "make-struct"
457 {
458 SCM layout;
459 size_t basic_size;
460 SCM obj;
461
462 SCM_VALIDATE_VTABLE (1, vtable);
463
464 layout = SCM_VTABLE_LAYOUT (vtable);
465 basic_size = scm_i_symbol_length (layout) / 2;
466
467 if (n_tail != 0)
468 {
469 SCM layout_str, last_char;
470
471 if (basic_size == 0)
472 {
473 bad_tail:
474 SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL);
475 }
476
477 layout_str = scm_symbol_to_string (layout);
478 last_char = scm_string_ref (layout_str,
479 scm_from_size_t (2 * basic_size - 1));
480 if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char)))
481 goto bad_tail;
482 }
483
484 obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), basic_size + n_tail);
485
486 scm_struct_init (obj, layout, n_tail, n_init, init);
487
488 /* If we're making a vtable, validate its layout and inherit
489 flags. However we allow for separation of allocation and
490 initialization, to humor GOOPS, so only validate if the layout was
491 passed as an initarg. */
492 if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE)
493 && scm_is_true (SCM_VTABLE_LAYOUT (obj)))
494 scm_i_struct_inherit_vtable_magic (vtable, obj);
495
496 return obj;
497 }
498 #undef FUNC_NAME
499
500 SCM
501 scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits init, ...)
502 {
503 va_list foo;
504 scm_t_bits *v;
505 size_t i;
506
507 v = alloca (sizeof (scm_t_bits) * n_init);
508
509 va_start (foo, init);
510 for (i = 0; i < n_init; i++)
511 {
512 v[i] = init;
513 init = va_arg (foo, scm_t_bits);
514 }
515 va_end (foo);
516
517 return scm_c_make_structv (vtable, n_tail, n_init, v);
518 }
519
520 SCM_DEFINE (scm_allocate_struct, "allocate-struct", 2, 0, 0,
521 (SCM vtable, SCM nfields),
522 "Allocate a new structure with space for @var{nfields} fields.\n\n"
523 "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
524 "@var{nfields} must be a non-negative integer. Strictly speaking\n"
525 "@var{nfields} is redundant, as the vtable carries the size\n"
526 "for its instances. However passing it is useful as a sanity\n"
527 "check, given that one module can inline a constructor in\n"
528 "another.\n\n"
529 "Fields will be initialized with their default values.")
530 #define FUNC_NAME s_scm_allocate_struct
531 {
532 SCM ret;
533 size_t c_nfields;
534
535 SCM_VALIDATE_VTABLE (1, vtable);
536 c_nfields = scm_to_size_t (nfields);
537
538 SCM_ASSERT (SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size) == c_nfields,
539 nfields, 2, FUNC_NAME);
540
541 ret = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), c_nfields);
542
543 if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)))
544 {
545 size_t n;
546 for (n = 0; n < c_nfields; n++)
547 SCM_STRUCT_DATA_SET (ret, n, SCM_UNPACK (SCM_BOOL_F));
548 }
549 else
550 scm_struct_init (ret, SCM_VTABLE_LAYOUT (vtable), 0, 0, NULL);
551
552 return ret;
553 }
554 #undef FUNC_NAME
555
556 SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
557 (SCM vtable, SCM tail_array_size, SCM init),
558 "Create a new structure.\n\n"
559 "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
560 "@var{tail_array_size} must be a non-negative integer. If the layout\n"
561 "specification indicated by @var{vtable} includes a tail-array,\n"
562 "this is the number of elements allocated to that array.\n\n"
563 "The @var{init1}, @dots{} are optional arguments describing how\n"
564 "successive fields of the structure should be initialized. Only fields\n"
565 "with protection 'r' or 'w' can be initialized, except for fields of\n"
566 "type 's', which are automatically initialized to point to the new\n"
567 "structure itself. Fields with protection 'o' can not be initialized by\n"
568 "Scheme programs.\n\n"
569 "If fewer optional arguments than initializable fields are supplied,\n"
570 "fields of type 'p' get default value #f while fields of type 'u' are\n"
571 "initialized to 0.")
572 #define FUNC_NAME s_scm_make_struct
573 {
574 size_t i, n_init;
575 long ilen;
576 scm_t_bits *v;
577
578 SCM_VALIDATE_VTABLE (1, vtable);
579 ilen = scm_ilength (init);
580 if (ilen < 0)
581 SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL);
582
583 n_init = (size_t)ilen;
584
585 /* best to use alloca, but init could be big, so hack to avoid a possible
586 stack overflow */
587 if (n_init < 64)
588 v = alloca (n_init * sizeof(scm_t_bits));
589 else
590 v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct");
591
592 for (i = 0; i < n_init; i++, init = SCM_CDR (init))
593 v[i] = SCM_UNPACK (SCM_CAR (init));
594
595 return scm_c_make_structv (vtable, scm_to_size_t (tail_array_size), n_init, v);
596 }
597 #undef FUNC_NAME
598
599 SCM
600 scm_i_make_vtable_vtable (SCM user_fields)
601 #define FUNC_NAME "make-vtable-vtable"
602 {
603 SCM fields, layout, obj;
604 size_t basic_size;
605 scm_t_bits v;
606
607 SCM_VALIDATE_STRING (1, user_fields);
608
609 fields = scm_string_append (scm_list_2 (required_vtable_fields,
610 user_fields));
611 layout = scm_make_struct_layout (fields);
612 if (!scm_is_valid_vtable_layout (layout))
613 SCM_MISC_ERROR ("invalid user fields", scm_list_1 (user_fields));
614
615 basic_size = scm_i_symbol_length (layout) / 2;
616
617 obj = scm_i_alloc_struct (NULL, basic_size);
618 /* Make it so that the vtable of OBJ is itself. */
619 SCM_SET_CELL_WORD_0 (obj, (scm_t_bits) SCM_STRUCT_DATA (obj) | scm_tc3_struct);
620
621 v = SCM_UNPACK (layout);
622 scm_struct_init (obj, layout, 0, 1, &v);
623 SCM_SET_VTABLE_FLAGS (obj,
624 SCM_VTABLE_FLAG_VTABLE | SCM_VTABLE_FLAG_VALIDATED);
625
626 return obj;
627 }
628 #undef FUNC_NAME
629
630 SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
631 (SCM fields, SCM printer),
632 "Create a vtable, for creating structures with the given\n"
633 "@var{fields}.\n"
634 "\n"
635 "The optional @var{printer} argument is a function to be called\n"
636 "@code{(@var{printer} struct port)} on the structures created.\n"
637 "It should look at @var{struct} and write to @var{port}.")
638 #define FUNC_NAME s_scm_make_vtable
639 {
640 if (SCM_UNBNDP (printer))
641 printer = SCM_BOOL_F;
642
643 return scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
644 scm_list_2 (scm_make_struct_layout (fields),
645 printer));
646 }
647 #undef FUNC_NAME
648
649
650 /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
651 contents are the same. Field protections are honored. Thus, it is an
652 error to test the equality of structures that contain opaque fields. */
653 SCM
654 scm_i_struct_equalp (SCM s1, SCM s2)
655 #define FUNC_NAME "scm_i_struct_equalp"
656 {
657 SCM vtable1, vtable2, layout;
658 size_t struct_size, field_num;
659
660 SCM_VALIDATE_STRUCT (1, s1);
661 SCM_VALIDATE_STRUCT (2, s2);
662
663 vtable1 = SCM_STRUCT_VTABLE (s1);
664 vtable2 = SCM_STRUCT_VTABLE (s2);
665
666 if (!scm_is_eq (vtable1, vtable2))
667 return SCM_BOOL_F;
668
669 layout = SCM_STRUCT_LAYOUT (s1);
670 struct_size = scm_i_symbol_length (layout) / 2;
671
672 for (field_num = 0; field_num < struct_size; field_num++)
673 {
674 SCM s_field_num;
675 SCM field1, field2;
676
677 /* We have to use `scm_struct_ref ()' here so that fields are accessed
678 consistently, notably wrt. field types and access rights. */
679 s_field_num = scm_from_size_t (field_num);
680 field1 = scm_struct_ref (s1, s_field_num);
681 field2 = scm_struct_ref (s2, s_field_num);
682
683 /* Self-referencing fields (type `s') must be skipped to avoid infinite
684 recursion. */
685 if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2))))
686 if (scm_is_false (scm_equal_p (field1, field2)))
687 return SCM_BOOL_F;
688 }
689
690 /* FIXME: Tail elements should be tested for equality. */
691
692 return SCM_BOOL_T;
693 }
694 #undef FUNC_NAME
695
696
697 \f
698
699
700 SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
701 (SCM handle, SCM pos),
702 "Access the @var{pos}th field of struct associated with\n"
703 "@var{handle}.\n"
704 "\n"
705 "If the field is of type 'p', then it can be set to an arbitrary\n"
706 "value.\n"
707 "\n"
708 "If the field is of type 'u', then it can only be set to a\n"
709 "non-negative integer value small enough to fit in one machine\n"
710 "word.")
711 #define FUNC_NAME s_scm_struct_ref
712 {
713 SCM vtable, answer = SCM_UNDEFINED;
714 scm_t_bits *data;
715 size_t p;
716
717 SCM_VALIDATE_STRUCT (1, handle);
718
719 vtable = SCM_STRUCT_VTABLE (handle);
720 data = SCM_STRUCT_DATA (handle);
721 p = scm_to_size_t (pos);
722
723 if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
724 && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)))
725 /* The fast path: HANDLE is a struct with only "p" fields. */
726 answer = SCM_PACK (data[p]);
727 else
728 {
729 SCM layout;
730 size_t layout_len, n_fields;
731 scm_t_wchar field_type = 0;
732
733 layout = SCM_STRUCT_LAYOUT (handle);
734 layout_len = scm_i_symbol_length (layout);
735 n_fields = layout_len / 2;
736
737 if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1)))
738 n_fields += data[n_fields - 1];
739
740 SCM_ASSERT_RANGE (1, pos, p < n_fields);
741
742 if (p * 2 < layout_len)
743 {
744 scm_t_wchar ref;
745 field_type = scm_i_symbol_ref (layout, p * 2);
746 ref = scm_i_symbol_ref (layout, p * 2 + 1);
747 if ((ref != 'r') && (ref != 'w') && (ref != 'h'))
748 {
749 if ((ref == 'R') || (ref == 'W'))
750 field_type = 'u';
751 else
752 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
753 }
754 }
755 else if (scm_i_symbol_ref (layout, layout_len - 1) != 'O')
756 field_type = scm_i_symbol_ref(layout, layout_len - 2);
757 else
758 SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
759
760 switch (field_type)
761 {
762 case 'u':
763 answer = scm_from_ulong (data[p]);
764 break;
765
766 #if 0
767 case 'i':
768 answer = scm_from_long (data[p]);
769 break;
770
771 case 'd':
772 answer = scm_make_real (*((double *)&(data[p])));
773 break;
774 #endif
775
776 case 's':
777 case 'p':
778 answer = SCM_PACK (data[p]);
779 break;
780
781
782 default:
783 SCM_MISC_ERROR ("unrecognized field type: ~S",
784 scm_list_1 (SCM_MAKE_CHAR (field_type)));
785 }
786 }
787
788 return answer;
789 }
790 #undef FUNC_NAME
791
792
793 SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
794 (SCM handle, SCM pos, SCM val),
795 "Set the slot of the structure @var{handle} with index @var{pos}\n"
796 "to @var{val}. Signal an error if the slot can not be written\n"
797 "to.")
798 #define FUNC_NAME s_scm_struct_set_x
799 {
800 SCM vtable;
801 scm_t_bits *data;
802 size_t p;
803
804 SCM_VALIDATE_STRUCT (1, handle);
805
806 vtable = SCM_STRUCT_VTABLE (handle);
807 data = SCM_STRUCT_DATA (handle);
808 p = scm_to_size_t (pos);
809
810 if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
811 && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE_RW)
812 && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)))
813 /* The fast path: HANDLE is a struct with only "pw" fields. */
814 data[p] = SCM_UNPACK (val);
815 else
816 {
817 SCM layout;
818 size_t layout_len, n_fields;
819 scm_t_wchar field_type = 0;
820
821 layout = SCM_STRUCT_LAYOUT (handle);
822 layout_len = scm_i_symbol_length (layout);
823 n_fields = layout_len / 2;
824
825 if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1)))
826 n_fields += data[n_fields - 1];
827
828 SCM_ASSERT_RANGE (1, pos, p < n_fields);
829
830 if (p * 2 < layout_len)
831 {
832 char set_x;
833 field_type = scm_i_symbol_ref (layout, p * 2);
834 set_x = scm_i_symbol_ref (layout, p * 2 + 1);
835 if (set_x != 'w' && set_x != 'h')
836 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
837 }
838 else if (scm_i_symbol_ref (layout, layout_len - 1) == 'W')
839 field_type = scm_i_symbol_ref (layout, layout_len - 2);
840 else
841 SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
842
843 switch (field_type)
844 {
845 case 'u':
846 data[p] = SCM_NUM2ULONG (3, val);
847 break;
848
849 #if 0
850 case 'i':
851 data[p] = SCM_NUM2LONG (3, val);
852 break;
853
854 case 'd':
855 *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
856 break;
857 #endif
858
859 case 'p':
860 data[p] = SCM_UNPACK (val);
861 break;
862
863 case 's':
864 SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
865
866 default:
867 SCM_MISC_ERROR ("unrecognized field type: ~S",
868 scm_list_1 (SCM_MAKE_CHAR (field_type)));
869 }
870 }
871
872 return val;
873 }
874 #undef FUNC_NAME
875
876
877 SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
878 (SCM handle),
879 "Return the vtable structure that describes the type of struct\n"
880 "associated with @var{handle}.")
881 #define FUNC_NAME s_scm_struct_vtable
882 {
883 SCM_VALIDATE_STRUCT (1, handle);
884 return SCM_STRUCT_VTABLE (handle);
885 }
886 #undef FUNC_NAME
887
888
889 /* {Associating names and classes with vtables}
890 *
891 * The name of a vtable should probably be stored as a slot. This is
892 * a backward compatible solution until agreement has been achieved on
893 * how to associate names with vtables.
894 */
895
896 unsigned long
897 scm_struct_ihashq (SCM obj, unsigned long n, void *closure)
898 {
899 /* The length of the hash table should be a relative prime it's not
900 necessary to shift down the address. */
901 return SCM_UNPACK (obj) % n;
902 }
903
904 SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
905 (SCM vtable),
906 "Return the name of the vtable @var{vtable}.")
907 #define FUNC_NAME s_scm_struct_vtable_name
908 {
909 SCM_VALIDATE_VTABLE (1, vtable);
910 return SCM_VTABLE_NAME (vtable);
911 }
912 #undef FUNC_NAME
913
914 SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
915 (SCM vtable, SCM name),
916 "Set the name of the vtable @var{vtable} to @var{name}.")
917 #define FUNC_NAME s_scm_set_struct_vtable_name_x
918 {
919 SCM_VALIDATE_VTABLE (1, vtable);
920 SCM_VALIDATE_SYMBOL (2, name);
921 SCM_SET_VTABLE_NAME (vtable, name);
922 /* FIXME: remove this, and implement proper struct classes instead.
923 (Vtables *are* classes.) */
924 scm_i_define_class_for_vtable (vtable);
925 return SCM_UNSPECIFIED;
926 }
927 #undef FUNC_NAME
928
929
930 \f
931
932 void
933 scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
934 {
935 if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
936 scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
937 else
938 {
939 SCM vtable = SCM_STRUCT_VTABLE (exp);
940 SCM name = scm_struct_vtable_name (vtable);
941 scm_puts_unlocked ("#<", port);
942 if (scm_is_true (name))
943 {
944 scm_display (name, port);
945 scm_putc_unlocked (' ', port);
946 }
947 else
948 {
949 if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE))
950 scm_puts_unlocked ("vtable:", port);
951 else
952 scm_puts_unlocked ("struct:", port);
953 scm_uintprint (SCM_UNPACK (vtable), 16, port);
954 scm_putc_unlocked (' ', port);
955 scm_write (SCM_VTABLE_LAYOUT (vtable), port);
956 scm_putc_unlocked (' ', port);
957 }
958 scm_uintprint (SCM_UNPACK (exp), 16, port);
959 /* hackety hack */
960 if (SCM_STRUCT_APPLICABLE_P (exp))
961 {
962 if (scm_is_true (SCM_STRUCT_PROCEDURE (exp)))
963 {
964 scm_puts_unlocked (" proc: ", port);
965 if (scm_is_true (scm_procedure_p (SCM_STRUCT_PROCEDURE (exp))))
966 scm_write (SCM_STRUCT_PROCEDURE (exp), port);
967 else
968 scm_puts_unlocked ("(not a procedure?)", port);
969 }
970 if (SCM_STRUCT_SETTER_P (exp))
971 {
972 scm_puts_unlocked (" setter: ", port);
973 scm_write (SCM_STRUCT_SETTER (exp), port);
974 }
975 }
976 scm_putc_unlocked ('>', port);
977 }
978 }
979
980 void
981 scm_init_struct ()
982 {
983 SCM name;
984
985 /* The first word of a struct is equal to `SCM_STRUCT_DATA (vtable) +
986 scm_tc3_struct', and `SCM_STRUCT_DATA (vtable)' is 2 words after VTABLE by
987 default. */
988 GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits) + scm_tc3_struct);
989
990 /* In the general case, `SCM_STRUCT_DATA (obj)' points 2 words after the
991 beginning of a GC-allocated region; that region is different from that of
992 OBJ once OBJ has undergone class redefinition. */
993 GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits));
994
995 required_vtable_fields = scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT);
996 scm_c_define ("standard-vtable-fields", required_vtable_fields);
997 required_applicable_fields = scm_from_locale_string (SCM_APPLICABLE_BASE_LAYOUT);
998 required_applicable_with_setter_fields = scm_from_locale_string (SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT);
999
1000 scm_standard_vtable_vtable = scm_i_make_vtable_vtable (scm_nullstr);
1001 name = scm_from_utf8_symbol ("<standard-vtable>");
1002 scm_set_struct_vtable_name_x (scm_standard_vtable_vtable, name);
1003 scm_define (name, scm_standard_vtable_vtable);
1004
1005 scm_applicable_struct_vtable_vtable =
1006 scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
1007 scm_list_1 (scm_make_struct_layout (required_vtable_fields)));
1008 name = scm_from_utf8_symbol ("<applicable-struct-vtable>");
1009 SCM_SET_VTABLE_FLAGS (scm_applicable_struct_vtable_vtable,
1010 SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
1011 scm_set_struct_vtable_name_x (scm_applicable_struct_vtable_vtable, name);
1012 scm_define (name, scm_applicable_struct_vtable_vtable);
1013
1014 scm_applicable_struct_with_setter_vtable_vtable =
1015 scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
1016 scm_list_1 (scm_make_struct_layout (required_vtable_fields)));
1017 name = scm_from_utf8_symbol ("<applicable-struct-with-setter-vtable>");
1018 scm_set_struct_vtable_name_x (scm_applicable_struct_with_setter_vtable_vtable, name);
1019 SCM_SET_VTABLE_FLAGS (scm_applicable_struct_with_setter_vtable_vtable,
1020 SCM_VTABLE_FLAG_APPLICABLE_VTABLE | SCM_VTABLE_FLAG_SETTER_VTABLE);
1021 scm_define (name, scm_applicable_struct_with_setter_vtable_vtable);
1022
1023 scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
1024 scm_c_define ("vtable-index-printer",
1025 scm_from_int (scm_vtable_index_instance_printer));
1026 scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
1027 #include "libguile/struct.x"
1028 }
1029
1030 /*
1031 Local Variables:
1032 c-file-style: "gnu"
1033 End:
1034 */