/
struct.ss
1512 lines (1384 loc) · 68.9 KB
/
struct.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Naming conventions:
;; - `rtd*` means an rtd that is not impersonators
;; - `init-count` means the number of fields supplied to the constructor,
;; not counting inherited fields
;; - `init*-count` means `init-count` plus inherited constructed fields
;; - `auto-count` means the number of fields automatically added,
;; not counting inherited fields
;; - `auto*-count` means `auto-count` plus inherited auto fields
;; - `total-count` means `init-count` plus `auto-count`
;; - `total*-count` means `init*-count` plus `auto*-count`
;; - `prefab-key+count` has a `total*-count`
(define-record struct-type-prop (name guard supers))
;; Record the properties that are implemented by each rtd; used
;; without a lock
(define rtd-props (make-ephemeron-eq-hashtable))
;; Maps a property-accessor function to `(cons predicate-proc can-impersonate)`;
;; used without a lock
(define property-accessors (make-ephemeron-eq-hashtable))
;; Maps a property-predicate function to `struct-property`; used without
;; a lock
(define property-predicates (make-ephemeron-eq-hashtable))
(define (struct-type-property? v)
(struct-type-prop? v))
(define/who make-struct-type-property
(case-lambda
[(name) (make-struct-type-property name #f '() #f #f #f default-realm)]
[(name guard) (make-struct-type-property name guard '() #f #f #f default-realm)]
[(name guard supers) (make-struct-type-property name guard supers #f #f #f default-realm)]
[(name guard supers can-impersonate?)
(make-struct-type-property name guard supers can-impersonate? #f #f default-realm)]
[(name guard supers can-impersonate? accessor-name)
(make-struct-type-property name guard supers can-impersonate? accessor-name #f default-realm)]
[(name guard supers can-impersonate? accessor-name contract)
(make-struct-type-property name guard supers can-impersonate? accessor-name contract default-realm)]
[(name guard supers can-impersonate? accessor-name contract realm)
(check who symbol? name)
(check who symbol? :or-false accessor-name)
(check who (lambda (x) (or (symbol? x) (string? x) (not x)))
:contract "(or/c symbol? string? #f)"
contract)
(check who symbol? realm)
(unless (or (not guard)
(eq? guard 'can-impersonate)
(and (#%procedure? guard) ; avoid `procedure?` until it's defined
(bitwise-bit-set? (#%procedure-arity-mask guard) 2))
(and (procedure? guard)
(procedure-arity-includes? guard 2)))
(raise-argument-error who "(or/c (procedure-arity-includes/c 2) #f 'can-impersonate)" guard))
(unless (and (#%list? supers)
(#%andmap (lambda (p)
(and (pair? p)
(struct-type-property? (car p))
(procedure? (cdr p))
(procedure-arity-includes? (cdr p) 1)))
supers))
(raise-argument-error who "(listof (cons/c struct-type-property? (procedure-arity-includes/c 1)))" supers))
(let* ([can-impersonate? (and (or can-impersonate? (eq? guard 'can-impersonate)) #t)]
[st (make-struct-type-prop name (and (not (eq? guard 'can-impersonate)) guard) supers)]
[accessor-name (or accessor-name
(string->symbol (string-append
(symbol->string name)
"-accessor")))]
[predicate-name (string->symbol
(string-append
(symbol->string name)
"?"))]
[pred (procedure-rename*
(lambda (v)
(let* ([v (strip-impersonator v)]
[rtd (if (record-type-descriptor? v)
v
(and (record? v)
(record-rtd v)))])
(and rtd
(not (eq? none (struct-property-ref st rtd none))))))
2
predicate-name
realm)])
(letrec ([acc
(procedure-rename*
(case-lambda
[(v fail)
(let ([val (let ([v (strip-impersonator v)])
(if (record-type-descriptor? v)
(struct-property-ref st v none)
(if (record? v)
(struct-property-ref st (record-rtd v) none)
none)))])
(cond
[(eq? val none)
(cond
[(eq? fail none)
(raise-argument-error accessor-name
(cond
[(not contract) (symbol->immutable-string predicate-name)]
[(string? contract) contract]
[else (symbol->immutable-string contract)])
v)]
[(procedure? fail) (|#%app| fail)]
[else fail])]
[(impersonator? v)
(impersonate-struct-or-property-ref val #f #|key1:|# acc #|key2:|# #f v #f #f #f)]
[else val]))]
[(v) (acc v none)])
6
accessor-name
realm)])
(add-to-table! property-accessors
acc
(cons pred can-impersonate?))
(add-to-table! property-predicates
pred
st)
(values st
pred
acc)))]))
(define (struct-type-property-accessor-procedure? v)
(let ([v (strip-impersonator v)])
(and (#%procedure? v)
(eq-hashtable-contains? property-accessors v))))
(define/who struct-type-property-predicate-procedure?
(case-lambda
[(v) (struct-type-property-predicate-procedure? v #f)]
[(v spt)
(check who struct-type-property? :or-false spt)
(let ([v (strip-impersonator v)])
(and (#%procedure? v)
(let ([spt-c (eq-hashtable-ref property-predicates v #f)])
(cond
[(not spt-c) #f]
[(not spt) #t]
[else (eq? spt spt-c)]))))]))
(define (struct-type-property-accessor-procedure-pred v)
(car (eq-hashtable-ref property-accessors v #f)))
(define (struct-type-property-accessor-procedure-can-impersonate? v)
(cdr (eq-hashtable-ref property-accessors v #f)))
(define (struct-property-ref prop rtd default)
(getprop (record-type-uid rtd) prop default))
(define (struct-property-set! prop rtd val)
(putprop (record-type-uid rtd) prop val))
;; Must be consistent with `procedure-rename` in "procedure.ss",
;; but needed before that one is defined:
(define (procedure-rename* proc mask name realm)
(make-arity-wrapper-procedure proc
mask
(vector name realm proc)))
;; ----------------------------------------
(define-record-type (inspector new-inspector inspector?)
(fields parent)
(sealed #t))
(define root-inspector (new-inspector #f))
(define/who make-inspector
(case-lambda
[() (new-inspector (current-inspector))]
[(i)
(check who inspector? i)
(new-inspector i)]))
(define/who make-sibling-inspector
(case-lambda
[() (make-sibling-inspector (current-inspector))]
[(i)
(check who inspector? i)
(new-inspector (inspector-parent i))]))
(define/who (inspector-superior? sup-insp sub-insp)
(check who inspector? sup-insp)
(check who inspector? sub-insp)
(if (eq? sub-insp root-inspector)
#f
(let ([parent (inspector-parent sub-insp)])
(or (eq? parent sup-insp)
(and parent
(inspector-superior? sup-insp parent))))))
;; result can be 'prefab, #f, an inspector, or `none`, where
;; `none` is the result for opaque "system" records
(define (inspector-ref rtd)
(getprop (record-type-uid rtd) 'inspector none))
(define (inspector-set! rtd insp)
(putprop (record-type-uid rtd) 'inspector insp))
;; ----------------------------------------
;; returns a procedure that takes an rtd and finishes creating/installing it
(define (check-make-struct-type-arguments who name parent-rtd init-count auto-count
props insp proc-spec immutables guard constructor-name
system?)
(check who symbol? name)
(check who :or-false struct-type? parent-rtd)
(check who exact-nonnegative-integer? init-count)
(check who exact-nonnegative-integer? auto-count)
(check who
:test (or (not proc-spec)
(procedure? proc-spec)
(exact-nonnegative-integer? proc-spec))
:contract "(or/c procedure? exact-nonnegative-integer? #f)"
proc-spec)
(check who
:test (and (#%list? props)
(#%andmap (lambda (i) (and (pair? i) (struct-type-property? (car i))))
props))
:contract "(listof (cons/c struct-type-property? any/c))"
props)
(check who
:test (or (not insp)
(inspector? insp)
(eq? insp 'prefab))
:contract "(or/c inspector? #f 'prefab)"
insp)
(check who
:test (and (#%list? immutables)
(#%andmap exact-nonnegative-integer? immutables))
:contract "(listof exact-nonnegative-integer?)"
immutables)
(check who :or-false procedure? guard)
(check who :or-false symbol? constructor-name)
(cond
[(eq? insp 'prefab)
(let ([bad
(or (and (impersonator? parent-rtd)
"chaperoned supertype disallowed for non-generative structure type")
(and parent-rtd
(not (eq? (inspector-ref (strip-impersonator parent-rtd)) 'prefab))
"generative supertype disallowed for non-generative structure type")
(and (pair? props)
"properties disallowed for non-generative structure type")
(and proc-spec
"procedure specification disallowed for non-generative structure type")
(and guard
"guard disallowed for non-generative structure type"))])
(when bad
(raise-arguments-error who bad
"structure type name" name))
;; everything else to be done for a for prefab must be covered in `prefab-key+count->rtd`
(lambda (rtd)
(void)))]
[else
(when parent-rtd
(when (#%$record-type-act-sealed? (strip-impersonator parent-rtd))
(raise-arguments-error who
"cannot make a subtype of a sealed type"
"type name" name
"sealed type" parent-rtd)))
;; The rest has to be delayed until we have an rtd:
(lambda (rtd)
(let* ([parent-rtd* (strip-impersonator parent-rtd)]
[parent-props
(if parent-rtd*
(eq-hashtable-ref rtd-props parent-rtd* '())
'())]
[all-immutables (if (integer? proc-spec)
(cons proc-spec immutables)
immutables)])
;; Record properties implemented by this type:
(let ([props (let ([props (append (map car props) parent-props)])
(if proc-spec
(cons prop:procedure props)
props))])
(add-to-table! rtd-props rtd props))
;; Copy parent properties for this type:
(for-each (lambda (prop)
(let loop ([prop prop])
(struct-property-set! prop rtd (struct-property-ref prop parent-rtd* #f))
(for-each (lambda (super)
(loop (car super)))
(struct-type-prop-supers prop))))
parent-props)
;; set default comparison
(unless (struct-property-ref prop:equal+hash rtd #f)
(struct-set-default-equal+hash! rtd))
;; Finish checking and install new property values:
(let ([props-ht
;; Check for duplicates and record property values
(let ([get-struct-info
(lambda ()
(let ([parent-total*-count (if parent-rtd*
(struct-type-total*-field-count parent-rtd*)
0)])
(list name
init-count
auto-count
(make-position-based-accessor rtd parent-total*-count (+ init-count auto-count))
(make-position-based-mutator rtd parent-total*-count (+ init-count auto-count))
all-immutables
parent-rtd
#f)))])
(let loop ([props props] [ht empty-hasheq])
(cond
[(null? props)
(if proc-spec
(let-values ([(ht props) (check-and-add-property who prop:procedure proc-spec rtd ht '()
get-struct-info)])
ht)
ht)]
[else
(let-values ([(ht props) (check-and-add-property who (caar props) (cdar props) rtd ht (cdr props)
get-struct-info)])
(loop props ht))])))])
(let loop ([ht empty-hasheqv] [imms immutables])
(cond
[(null? imms) (void)]
[else
(let ([i (car imms)])
(when (hash-ref ht i #f)
(raise-arguments-error who
"redundant immutable field index"
"index" i
"in list" immutables))
(unless (< i init-count)
(raise-arguments-error who
"index for immutable field >= initialized-field count"
"index" i
"initialized-field count" init-count
"in list" immutables))
(loop (hash-set ht i #t) (cdr imms)))]))
(let ([v (hash-ref props-ht prop:procedure #f)])
(when v
(cond
[(exact-nonnegative-integer? v)
(unless (< v init-count)
(raise-arguments-error who
"index for procedure >= initialized-field count"
"index" v
"field count" init-count))
(unless (or (eq? v proc-spec) (#%memv v immutables))
(raise-arguments-error who
"field is not specified as immutable for a prop:procedure index"
"index" v))]
[(procedure? v)
(void)]
[else
(raise-arguments-error who
"given value did not satisfy the contract for prop:procedure"
"expected" "(or/c procedure? exact-nonnegative-integer?)"
"given" v)])))
(let ([parent-rtd* (strip-impersonator parent-rtd)])
(when parent-rtd*
(let ([authentic? (not (eq? (hash-ref props-ht prop:authentic none) none))]
[authentic-parent? (struct-property-ref prop:authentic parent-rtd* #f)])
(when (not (eq? authentic? authentic-parent?))
(if authentic?
(raise-arguments-error who
"cannot make an authentic subtype of a non-authentic type"
"type name" name
"non-authentic type" parent-rtd)
(raise-arguments-error who
"cannot make a non-authentic subtype of an authentic type"
"type name" name
"authentic type" parent-rtd)))))
(when guard
(let ([expected-count (+ 1
init-count
(if parent-rtd*
(get-field-info-init*-count (struct-type-field-info parent-rtd*))
0))])
(unless (procedure-arity-includes? guard expected-count)
(raise-arguments-error who
(string-append
"guard procedure does not accept correct number of arguments;\n"
" should accept one more than the number of constructor arguments")
"guard procedure" guard
"expected arity" expected-count))))))
;; Record inspector
(unless (and system? insp)
(inspector-set! rtd insp))
;; Register guard
(register-guards! rtd parent-rtd guard 'at-start)))]))
(define (check-and-add-property who prop val rtd ht props get-struct-info)
(let* ([guarded-val
(let ([guard (struct-type-prop-guard prop)])
(if guard
(|#%app| guard val (get-struct-info))
val))]
[check-val (cond
[(eq? prop prop:procedure)
;; Save and check the original value, since the true
;; guard is in `check-make-struct-type-arguments`
;; (for historical reasons)
val]
[else guarded-val])]
[old-v (hash-ref ht prop none)])
(unless (or (eq? old-v none)
(eq? old-v check-val))
(raise-arguments-error who
"duplicate property binding"
"property" prop))
(cond
[(eq? prop prop:sealed)
(#%$record-type-act-sealed! rtd)
(values ht props)]
[else
(struct-property-set! prop rtd guarded-val)
(values (hash-set ht prop check-val)
(append
(if (eq? old-v none)
(map (lambda (super)
(cons (car super)
(|#%app| (cdr super) guarded-val)))
(struct-type-prop-supers prop))
;; skip supers, because property is already added
null)
props))])))
;; used to install equality and hashing on rumble records or as default implementation:
(define (struct-set-equal+hash! rtd eql? hash-code)
(struct-property-set! prop:equal+hash rtd
(list (or eql?
(lambda (a b eql?) (eq? a b)))
hash-code
hash-code)))
(define (struct-set-equal-mode+hash! rtd eql? hash-code)
(struct-property-set! prop:equal+hash rtd
(list (or eql?
(lambda (a b eql? mode) (eq? a b)))
hash-code)))
(define (inherit-equal+hash! rtd parent-rtd)
(struct-property-set! prop:equal+hash rtd (struct-property-ref prop:equal+hash parent-rtd #f)))
;; variant of `check-make-struct-type-arguments` called by schemified
(define make-struct-type-install-properties
(case-lambda
[(name init-count auto-count parent-rtd)
(make-struct-type-install-properties name init-count auto-count parent-rtd '() (current-inspector) #f '() #f #f)]
[(name init-count auto-count parent-rtd props)
(make-struct-type-install-properties name init-count auto-count parent-rtd props (current-inspector) #f '() #f #f)]
[(name init-count auto-count parent-rtd props insp)
(make-struct-type-install-properties name init-count auto-count parent-rtd props insp #f '() #f #f)]
[(name init-count auto-count parent-rtd props insp proc-spec)
(make-struct-type-install-properties name init-count auto-count parent-rtd props insp proc-spec '() #f #f)]
[(name init-count auto-count parent-rtd props insp proc-spec immutables)
(make-struct-type-install-properties name init-count auto-count parent-rtd props insp proc-spec immutables #f #f)]
[(name init-count auto-count parent-rtd props insp proc-spec immutables guard)
(make-struct-type-install-properties name init-count auto-count parent-rtd props insp proc-spec immutables guard #f)]
[(name init-count auto-count parent-rtd props insp proc-spec immutables guard constructor-name)
;; returns a finishing procedure
(check-make-struct-type-arguments 'make-struct-type (if (pair? name) (car name) name) parent-rtd init-count auto-count
props insp proc-spec immutables guard constructor-name (pair? name))]))
;; ----------------------------------------
;; Records which fields of an prefab rtd are mutable, where an rtd that is
;; not in the table has no mutable fields, and the field list can be
;; empty if a parent type is mutable; this table is used without
;; a lock
(define rtd-mutables (make-ephemeron-eq-hashtable))
(define struct-proc-tables-need-resize? #f)
;; Accessors and mutators that need a position are wrapped in these records:
(define-record position-based-accessor (rtd offset field-count))
(define-record position-based-mutator (rtd offset field-count))
(define (position-based-accessor-name f)
(let ([rtd (position-based-accessor-rtd f)])
(string->symbol (string-append-immutable (symbol->immutable-string (record-type-name rtd)) "-ref"))))
(define (position-based-mutator-name f)
(let ([rtd (position-based-mutator-rtd f)])
(string->symbol (string-append-immutable (symbol->immutable-string (record-type-name rtd)) "-set!"))))
(define (add-to-table! table key val)
(if (eq-hashtable-try-atomic-cell table key val)
(set! struct-proc-tables-need-resize? #t)
(add-to-table! table key val)))
;; Called in the collect-request handler:
(define (update-struct-procs-table-sizes!)
(when struct-proc-tables-need-resize?
(set! struct-proc-tables-need-resize? #f)
(let ([resize! (lambda (ht)
;; Since `add-to-table!` uses `eq-hashtable-try-atomic-cell`,
;; give the tabel a chance to resize
(let ([p (cons #f #f)])
(eq-hashtable-set! ht p #t)
(eq-hashtable-delete! ht p)))])
(resize! property-accessors)
(resize! property-predicates)
(resize! rtd-mutables)
(resize! rtd-props))))
(define (|#%struct-constructor| p arity-mask)
(make-wrapper-procedure p arity-mask #\c))
(define (|#%struct-predicate| p)
(make-wrapper-procedure p 2 #\p))
(define (|#%struct-field-accessor| p rtd pos)
(make-wrapper-procedure p 2 (cons rtd pos)))
(define (|#%struct-field-mutator| p rtd pos)
(make-wrapper-procedure p 4 (cons pos rtd)))
(define (struct-constructor-procedure? v)
(let ([v (strip-impersonator v)])
(and (wrapper-procedure? v)
(eq? #\c (wrapper-procedure-data v)))))
(define (struct-predicate-procedure? v)
(let ([v (strip-impersonator v)])
(and (wrapper-procedure? v)
(eq? #\p (wrapper-procedure-data v)))))
(define (struct-accessor-procedure? v)
(let ([v (strip-impersonator v)])
(or (position-based-accessor? v)
(and (wrapper-procedure? v)
(let ([d (wrapper-procedure-data v)])
(and (pair? d)
(record-type-descriptor? (car d))))))))
(define (struct-mutator-procedure? v)
(let ([v (strip-impersonator v)])
(or (position-based-mutator? v)
(and (wrapper-procedure? v)
(let ([d (wrapper-procedure-data v)])
(and (pair? d)
(record-type-descriptor? (cdr d))))))))
(define (struct-accessor-procedure-rtd+pos v)
(if (position-based-accessor? v)
(cons (position-based-accessor-rtd v)
(position-based-accessor-offset v))
(wrapper-procedure-data v)))
(define (struct-mutator-procedure-pos+rtd v)
(if (position-based-mutator? v)
(cons (position-based-mutator-offset v)
(position-based-mutator-rtd v))
(wrapper-procedure-data v)))
(define-syntax (|#%nongenerative-uid| stx)
(syntax-case stx ()
[(_ name) #`(quote #,(datum->syntax #'name ((current-generate-id) (datum name))))]
[else #'#f]))
(define |#%struct-ref-error|
(case-lambda
[(v record-name field-name)
(#%$app/no-return struct-ref-error v record-name field-name)]
[(v proc-name contract/rtd realm)
(#%$app/no-return struct-ref-error v proc-name contract/rtd realm)]))
(define |#%struct-set!-error|
(case-lambda
[(v record-name field-name)
(#%$app/no-return struct-set!-error v record-name field-name)]
[(v proc-name contract/rtd realm)
(#%$app/no-return struct-set!-error v proc-name contract/rtd realm)]))
(define struct-ref-error
(case-lambda
[(v record-name field-name)
(raise-argument-error* (if field-name
(make-struct-accessor-name record-name field-name)
'accessor)
default-realm
(record-name->contract-string record-name)
v)]
[(v proc-name contract/rtd realm)
(raise-argument-error* proc-name realm (struct-contract->string contract/rtd) v)]))
(define struct-set!-error
(case-lambda
[(v record-name field-name)
(raise-argument-error* (if field-name
(make-struct-mutator-name record-name field-name)
'mutator)
default-realm
(record-name->contract-string record-name)
v)]
[(v proc-name contract/rtd realm)
(raise-argument-error* proc-name realm (struct-contract->string contract/rtd) v)]))
(define (make-struct-accessor-name rec-name field-name)
(string->symbol (string-append-immutable
(symbol->immutable-string rec-name)
"-"
(if (symbol? field-name)
(symbol->immutable-string field-name)
field-name))))
(define (make-struct-mutator-name rec-name field-name)
(string->symbol (string-append-immutable
"set-"
(symbol->immutable-string rec-name)
"-"
(if (symbol? field-name)
(symbol->immutable-string field-name)
field-name)
"!")))
(define (struct-contract->string contract/rtd)
(cond
[(string? contract/rtd) contract/rtd]
[(symbol? contract/rtd) (symbol->immutable-string contract/rtd)]
[else (record-name->contract-string (record-type-name contract/rtd))]))
(define (record-name->contract-string record-name)
(string-append-immutable (symbol->immutable-string record-name) "?"))
;; ----------------------------------------
;; General structure-type creation, but not called when a `schemify`
;; transformation keeps the record type exposed to the compiler
(define make-struct-type
(case-lambda
[(name parent-rtd init-count auto-count)
(make-struct-type name parent-rtd init-count auto-count #f '() (current-inspector) #f '() #f #f)]
[(name parent-rtd init-count auto-count auto-val)
(make-struct-type name parent-rtd init-count auto-count auto-val '() (current-inspector) #f '() #f #f)]
[(name parent-rtd init-count auto-count auto-val props)
(make-struct-type name parent-rtd init-count auto-count auto-val props (current-inspector) #f '() #f #f)]
[(name parent-rtd init-count auto-count auto-val props insp)
(make-struct-type name parent-rtd init-count auto-count auto-val props insp #f '() #f #f)]
[(name parent-rtd init-count auto-count auto-val props insp proc-spec)
(make-struct-type name parent-rtd init-count auto-count auto-val props insp proc-spec '() #f #f)]
[(name parent-rtd init-count auto-count auto-val props insp proc-spec immutables)
(make-struct-type name parent-rtd init-count auto-count auto-val props insp proc-spec immutables #f #f)]
[(name parent-rtd init-count auto-count auto-val props insp proc-spec immutables guard)
(make-struct-type name parent-rtd init-count auto-count auto-val props insp proc-spec immutables guard #f)]
[(name parent-rtd init-count auto-count auto-val props insp proc-spec immutables guard constructor-name)
(let* ([finish! (check-make-struct-type-arguments 'make-struct-type name parent-rtd init-count auto-count
props insp proc-spec immutables guard constructor-name #f)]
[prefab-uid (and (eq? insp 'prefab)
(structure-type-lookup-prefab-uid name parent-rtd init-count auto-count auto-val immutables))]
[parent-rtd* (strip-impersonator parent-rtd)]
[parent-fi (if parent-rtd*
(struct-type-field-info parent-rtd*)
empty-field-info)]
[rtd (make-record-type-descriptor name
parent-rtd*
prefab-uid
(#%ormap (lambda (p) (eq? prop:sealed (car p))) props)
#f
(cons
(+ init-count auto-count)
(let ([mask (sub1 (general-arithmetic-shift 1 (+ init-count auto-count)))])
(if (eq? insp 'prefab)
mask
(let loop ([imms (if (exact-nonnegative-integer? proc-spec)
(cons proc-spec immutables)
immutables)]
[mask mask])
(cond
[(null? imms) mask]
[else
(let ([m (bitwise-not (arithmetic-shift 1 (car imms)))])
(loop (cdr imms) (bitwise-and mask m)))]))))))]
[parent-auto*-count (get-field-info-auto*-count parent-fi)]
[parent-init*-count (get-field-info-init*-count parent-fi)]
[parent-total*-count (get-field-info-total*-count parent-fi)]
[init*-count (+ init-count parent-init*-count)]
[auto*-count (+ auto-count parent-auto*-count)]
[auto-field-adder (and (positive? auto*-count)
(let ([pfa (get-field-info-auto-adder parent-fi)])
(lambda (args)
(args-insert args init-count auto-count auto-val pfa))))]
[constructor-name (or constructor-name
(string->symbol (string-append "make-" (symbol->string name))))])
(when (or parent-rtd* auto-field-adder)
(let ([field-info (make-field-info init*-count auto*-count auto-field-adder)])
(putprop (record-type-uid rtd) 'field-info field-info)))
(finish! rtd)
(let ([ctr (struct-type-constructor-add-guards
(let ([c (record-constructor rtd)])
(procedure-rename
(if (zero? auto*-count)
c
(procedure-reduce-arity
(lambda args
(apply c (reverse (auto-field-adder (reverse args)))))
init*-count))
constructor-name))
rtd
constructor-name)]
[pred (procedure-rename
(lambda (v)
(or (record? v rtd)
(and (impersonator? v)
(record? (impersonator-val v) rtd))))
(string->symbol (string-append (symbol->string name) "?")))])
(values rtd
(|#%struct-constructor| ctr (procedure-arity-mask ctr))
(|#%struct-predicate| pred)
(make-position-based-accessor rtd parent-total*-count (+ init-count auto-count))
(make-position-based-mutator rtd parent-total*-count (+ init-count auto-count)))))]))
;; Field count (init + auto) not including parent fields
(define (record-type-field-count rtd)
(fx- (#%$record-type-field-count rtd)
(let ([parent-rtd (record-type-parent rtd)])
(if parent-rtd
(#%$record-type-field-count parent-rtd)
0))))
;; Used by a `schemify` transformation:
(define (structure-type-lookup-prefab-uid name parent-rtd* init-count auto-count auto-val immutables)
;; Return a UID for a prefab structure type. We can assume that
;; `immutables` is well-formed, and checking an error reporting will
;; happen latter if necessary.
(let ([prefab-key (derive-prefab-key name
(and parent-rtd*
(getprop (record-type-uid parent-rtd*) 'prefab-key+count))
init-count
immutables auto-count auto-val)]
[total*-count (+ (if parent-rtd*
(struct-type-total*-field-count parent-rtd*)
0)
init-count
auto-count)])
(record-type-uid
(prefab-key+count->rtd (cons prefab-key total*-count)))))
;; An emphemeron-weak, `equal?`-based hash table that maps (cons prefab-key
;; total-field-count) to rtd. We'll create a table without a lock, and
;; we'll use it for all places, which means that we need to use a
;; global lock to access the table. Compute a hash code outside the
;; lock, though, just in case computing the code needs the lock.
(define prefabs #f)
;; Call with lock:
(define (prefab-ref prefab-key+count code)
(and prefabs
(hashtable-ref prefabs (cons code prefab-key+count) #f)))
(define (prefab-key+count->rtd prefab-key+count)
(let ([code (equal-hash-code prefab-key+count)])
(cond
[(with-global-lock (prefab-ref prefab-key+count code))
=> (lambda (rtd) rtd)]
[else
(let* ([prefab-key (car prefab-key+count)]
[name (if (symbol? prefab-key)
prefab-key
(car prefab-key))]
[parent-prefab-key+count
(prefab-key->parent-prefab-key+count (car prefab-key+count))]
[parent-rtd (and parent-prefab-key+count
(prefab-key+count->rtd parent-prefab-key+count))]
[total-count (- (cdr prefab-key+count)
(if parent-prefab-key+count
(cdr parent-prefab-key+count)
0))]
[uid (encode-prefab-key+count-as-symbol prefab-key+count)]
[rtd (make-record-type-descriptor name
parent-rtd
uid #f #f
(cons
total-count
;; All fields must be reported as mutable, because
;; we might need to mutate to create cyclic data:
(sub1 (bitwise-arithmetic-shift-left 1 total-count))))]
[mutables (prefab-key-mutables prefab-key total-count)])
(with-global-lock
(cond
[(prefab-ref prefab-key+count code)
;; rtd was created concurrently
=> (lambda (rtd) rtd)]
[else
(let ([pr (cons code prefab-key+count)])
(putprop uid 'prefab-key+count prefab-key+count)
(putprop uid 'prefab-pr pr) ; retain
(unless prefabs (set! prefabs (make-ephemeron-hashtable car equal?)))
(hashtable-set! prefabs pr rtd)
(struct-set-default-equal+hash! rtd)
(register-mutables! mutables rtd parent-rtd)
(inspector-set! rtd 'prefab)
rtd)])))])))
(define (register-mutables! mutables rtd parent-rtd)
(unless (and (equal? '#() mutables)
(or (not parent-rtd)
(not (eq-hashtable-contains? rtd-mutables parent-rtd))))
(add-to-table! rtd-mutables rtd mutables)))
(define (check-accessor-or-mutator-index who rtd pos)
(let* ([total-count (record-type-field-count rtd)])
(unless (< pos total-count)
(if (zero? total-count)
(raise-arguments-error who
"index too large; no fields accessible"
"index" pos
"structure type" rtd)
(raise-arguments-error who
"index too large"
"index" pos
"maximum allowed index" (sub1 total-count)
"structure type" rtd)))))
(define/who make-struct-field-accessor
(case-lambda
[(pba pos name contract realm)
(check who position-based-accessor?
:contract "(and/c struct-accessor-procedure? (procedure-arity-includes/c 2))"
pba)
(check who exact-nonnegative-integer? pos)
(check who symbol? :or-false name)
(check who (lambda (x) (or (symbol? x) (string? x) (not x)))
:contract "(or/c symbol? string? #f)"
contract)
(check who symbol? realm)
(let ([rtd (position-based-accessor-rtd pba)])
(check-accessor-or-mutator-index who rtd pos)
(let* ([p (record-field-accessor rtd
(+ pos (position-based-accessor-offset pba)))]
[rec-name (record-type-name rtd)]
[proc-name (if (and contract name)
name
(if name
(make-struct-accessor-name rec-name name)
'accessor))]
[field/proc-name (if contract proc-name name)]
[wrap-p
(procedure-rename
(lambda (v)
($value
(if (record? v rtd)
(p v)
(impersonate-ref p rtd pos v field/proc-name contract realm))))
proc-name
realm)])
(|#%struct-field-accessor| wrap-p rtd pos)))]
[(pba pos name contract)
(make-struct-field-accessor pba pos name contract default-realm)]
[(pba pos name)
(make-struct-field-accessor pba pos name #f default-realm)]
[(pba pos)
(let ([name (string->symbol
(string-append-immutable "field" (if (exact-nonnegative-integer? pos)
(number->string pos)
"")))])
(make-struct-field-accessor pba pos name #f default-realm))]))
(define/who make-struct-field-mutator
(case-lambda
[(pbm pos name contract realm)
(check who position-based-mutator?
:contract "(and/c struct-mutator-procedure? (procedure-arity-includes/c 3))"
pbm)
(check who exact-nonnegative-integer? pos)
(check who symbol? :or-false name)
(check who (lambda (x) (or (symbol? x) (string? x) (not x)))
:contract "(or/c symbol? string? #f)"
contract)
(check who symbol? realm)
(let ([rtd (position-based-mutator-rtd pbm)])
(check-accessor-or-mutator-index who rtd pos)
(let* ([abs-pos (+ pos (position-based-mutator-offset pbm))]
[rec-name (record-type-name rtd)]
[mut-name (if (and contract name)
name
(if name
(make-struct-mutator-name rec-name name)
'mutator))]
[field/proc-name (if contract mut-name name)]
[wrap-p
(procedure-rename
(if (struct-type-field-mutable? rtd pos)
(let ([p (record-field-mutator rtd abs-pos)])
(lambda (v a)
(if (record? v rtd)
(p v a)
(impersonate-set! p rtd pos abs-pos v a field/proc-name contract realm))))
(lambda (v a)
(cannot-modify-by-pos-error mut-name v pos)))
mut-name
realm)])
(|#%struct-field-mutator| wrap-p rtd pos)))]
[(pbm pos name contract)
(make-struct-field-mutator pbm pos name contract default-realm)]
[(pbm pos name)
(make-struct-field-mutator pbm pos name #f default-realm)]
[(pbm pos)
(let ([name (string->symbol
(string-append-immutable "field" (if (exact-nonnegative-integer? pos)
(number->string pos)
"")))])
(make-struct-field-mutator pbm pos name #f default-realm))]))
(define (cannot-modify-by-pos-error name v pos)
(raise-arguments-error name
"cannot modify value of immutable field in structure"
"structure" v
"field index" pos))
;; Takes constructor arguments and adds auto-argument values.
;; Receives and returns `args` is in reverse order.
(define (args-insert args fields-count auto-count auto-val pfa)
(let loop ([auto-count auto-count])
(if (zero? auto-count)
(if pfa
(let loop ([fields-count fields-count] [args args])
(if (zero? fields-count)
(pfa args)
(cons (car args) (loop (fx1- fields-count) (cdr args)))))
args)
(cons auto-val (loop (fx1- auto-count))))))
;; ----------------------------------------
(define (struct-type? v) (record-type-descriptor? (strip-impersonator v)))
(define/who (procedure-struct-type? v)
(check who struct-type? v)
(procedure-struct? v))
(define (struct? v)
(let ([v (strip-impersonator v)])
(and (record? v)
(struct-type-any-transparent? (record-rtd v)))))
(define (struct-info v)
(cond
[(impersonator? v)
(if (record? (impersonator-val v))
(impersonate-struct-info v)
(values #f #t))]
[(not (record? v)) (values #f #t)]
[else (next-visible-struct-type (record-rtd v))]))
(define (next-visible-struct-type rtd)
(let loop ([rtd rtd] [skipped? #f])
(cond
[(struct-type-immediate-transparent? rtd)
(values rtd skipped?)]
[else
(let ([parent-rtd (record-type-parent rtd)])
(if parent-rtd
(loop parent-rtd #t)
(values #f #t)))])))
(define/who (struct-type-info rtd)
(check who struct-type? rtd)
(let ([rtd* (strip-impersonator rtd)])
(check-inspector-access 'struct-type-info rtd*)
(let* ([fi (struct-type-field-info rtd*)]
[parent-rtd* (record-type-parent rtd*)]
[parent-fi (if parent-rtd*
(struct-type-field-info parent-rtd*)
empty-field-info)]
[init-count (get-field-info-init-count fi parent-fi)]
[auto-count (get-field-info-auto-count fi parent-fi)]
[parent-total*-count (get-field-info-total*-count parent-fi)])
(let-values ([(next-rtd* skipped?)
(if parent-rtd*
(next-visible-struct-type parent-rtd*)
(values #f #f))])
(letrec ([get-results
(lambda ()
(values (record-type-name rtd*)
init-count
auto-count
(make-position-based-accessor rtd* parent-total*-count (+ init-count auto-count))
(make-position-based-mutator rtd* parent-total*-count (+ init-count auto-count))
(if (struct-type-prefab? rtd*)
(mutables->immutables (eq-hashtable-ref rtd-mutables rtd* '#()) init-count)
(let ([end (record-type-field-count rtd*)]
[offset (fx+ 1 (struct-type-parent-total*-count rtd*))]
[mpm (struct-type-mpm rtd*)])
(let loop ([i 0])
(cond
[(fx= i end) '()]
[(bitwise-bit-set? mpm (fx+ offset i))
(loop (fx+ i 1))]
[else (cons i (loop (fx+ i 1)))]))))
next-rtd*
skipped?))])
(cond
[(struct-type-chaperone? rtd)
(chaperone-struct-type-info rtd get-results)]
[else
(get-results)]))))))