-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattrs.lisp
More file actions
1495 lines (1353 loc) · 53.5 KB
/
attrs.lisp
File metadata and controls
1495 lines (1353 loc) · 53.5 KB
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
(defpackage :functional-trees/attrs
(:nicknames :ft/attrs)
(:use
:common-lisp
:alexandria
:curry-compose-reader-macros
:functional-trees/functional-trees
:iterate
:named-readtables)
(:import-from :bordeaux-threads)
(:import-from :cl-store)
(:import-from :closer-mop)
(:import-from :fset)
(:shadowing-import-from
:trivial-garbage
:make-weak-hash-table)
(:shadowing-import-from
:fset
:equal?
:mapc
:mapcar
:subst
:subst-if
:subst-if-not)
(:import-from
:functional-trees
:intervals-of-node
:set-difference/hash)
(:import-from
:serapeum
:->
:@
:and-let*
:assure
:boolean-if
:boolean-unless
:bound-value
:box
:boxp
:callf
:def
:defconst
:defparameter-unbound
:defplace
:defstruct-read-only
:defsubst
:defvar-unbound
:do-each
:econd
:etypecase-of
:lret
:mvlet*
:nest
:nlet
:pop-assoc
:receive
:unbox
:standard/context
:vect
:with-boolean
:with-thunk)
(:export
:*approximation*
:*attrs*
:*enable-cross-session-cache*
:*read-only*
:*max-circular-iterations*
:attr-missing
:attr-proxy
:attribute-error
:attrs-root
:attrs-root
:attrs-root*
:circular-attribute
:def-attr-fun
:divergent-attribute
:finalize-approximation
:read-only-attributes
:handle-circular-attribute
:has-attribute-p
:invalidate-subroot
:reachable?
:update-subroot-mapping
:session-shadowing
:subroot
:subroot?
:uncomputed-attr
:unreachable-node
:with-ensure-attr-session
:with-attr-session
:with-attr-table))
(in-package :functional-trees/attrs)
(in-readtable :curry-compose-reader-macros)
;;; Memoized values
(defvar *allow-circle* t
"Whether to allow circular attributes.")
(defvar *circle* nil
"The currently executing circular attribute evaluation, if any.")
(defparameter *max-circular-iterations* 100)
(defgeneric attribute-bottom (attr)
(:documentation
"Return the bottom value for a circular attribute.
This can return multiple values.")
(:method ((attr t))
(values)))
(defgeneric attribute-converged-p (attr old new)
(:documentation
"Return true if OLD and NEW have converged.")
(:method ((attr t) x y)
(equal x y)))
(defclass circular-eval ()
((changep
:documentation "Has there been a change in this iteration?"
:initform nil
:type boolean)
(iteration
:reader circle-iteration
:documentation "How many iterations have been performed?"
:initform 0
:type (integer 0))
(max-visit-count
:documentation "Count the max number of accesses"
:accessor max-visit-count
:initform 0)
(memo-cells
:accessor memo-cells
:documentation "Holds memo cells to memoize when done."
:initform (vect)
:type vector))
(:documentation "Circular evaluation state"))
(defvar *approximation* nil)
(def +final-value+ -1)
(deftype iteration ()
'fixnum)
(declaim (inline approximation))
(defstruct (approximation
(:constructor approximation
(&optional values visiting-p)))
"Holds the approximation to attributes during circular evaluation."
(values nil :type list)
(visiting-p nil :type boolean)
;; The overall cycle iteration where the value was last computed.
;; Tracked to avoid evaluating the same cycle more than once. NB
;; iteration 0 never actually happens.
(iteration 0 :type iteration)
(visit-count 0 :type (unsigned-byte 32)))
(defun finalize-approximation ()
"Mark the approximation currently being computed as having converged."
(when-let (a *approximation*)
(setf (approximation-iteration a) +final-value+)))
(defsubst approximation-finalized-p (a)
(eql (approximation-iteration a) +final-value+))
(deftype memoized-value ()
"Type of a attribute value: either an approximation, or a list of the
values returned by the attribute function."
'(or approximation list))
(deftype memo-cell ()
'(cons symbol memoized-value))
(declaim
(inline
cell-attr
cell-data
cell-values
(setf cell-attr)
(setf cell-data)
(setf cell-values)))
(defplace cell-attr (memo-cell)
"Return the attribute of MEMO-CELL."
(car memo-cell))
(defplace cell-data (memo-cell)
"Return the data of MEMO-CELL (an approximation or a list of values)."
(cdr memo-cell))
(defun cell-values (memo-cell)
(let ((data (cell-data memo-cell)))
(etypecase-of memoized-value data
(approximation (approximation-values data))
(list (values-list data)))))
(defun (setf cell-values) (values memo-cell)
(declare (list values))
(with-accessors ((data cell-data)) memo-cell
(etypecase-of memoized-value data
(approximation (setf (approximation-values data) values))
(list (setf data values)))))
(defun mark-visiting (memo-cell &aux (circle *circle*))
(declare (memo-cell memo-cell))
(with-accessors ((visiting-p approximation-visiting-p)
(visit-count approximation-visit-count))
(cell-data memo-cell)
(setf visiting-p t)
(incf visit-count)
(when circle
(maxf (max-visit-count circle)
visit-count))))
(defun mark-not-visiting (memo-cell)
(declare (memo-cell memo-cell))
(setf (approximation-visiting-p (cell-data memo-cell)) nil))
(defun call/visit (memo-cell body-fn)
(declare (function body-fn))
(let ((visiting-initially-p
(approximation-visiting-p (cell-data memo-cell))))
(mark-visiting memo-cell)
(multiple-value-prog1 (funcall body-fn)
(setf (approximation-visiting-p (cell-data memo-cell))
visiting-initially-p))))
(defmacro with-visit ((memo-cell) &body body)
(with-thunk (body)
`(call/visit ,memo-cell ,body)))
;;; Attribute sessions.
(defvar-unbound *attrs*
"Holds the current attribute session.")
(defparameter *read-only* nil
"When non-nil, signal an error if an attribute is recomputed.")
(defvar *attribute-trail* nil
"Holds a stack-allocated list of (fn-name . node) pairs currently
being calculated, useful when diagnosing circular dependencies between
attributes.")
(defvar *enable-cross-session-cache* t
"If non-nil, cache attributes across sessions.")
(def +cache-lock+ (bt:make-lock))
;;; Use defparameter as we want the cache to be cleared if the system
;;; is reloaded.
(defparameter *session-cache*
(tg:make-weak-hash-table
:test 'eq
:weakness :key
:weakness-matters t)
"Global (weak) cache of attribute sessions.
Roots are immutable, so if we have previously computed attributes for
them we can reuse them.
Practically this has the effect that we do not have to to update
the node to subroot mapping.")
(defun cache-lookup (key)
(bt:with-lock-held (+cache-lock+)
(gethash key *session-cache*)))
(defun (setf cache-lookup) (value key)
(bt:with-lock-held (+cache-lock+)
(setf (gethash key *session-cache*) value)))
(defvar *subroot-stack* nil
"Stack of subroots whose attributes are being computed.
While subroots cannot be nested in the node tree, computation of their
attributes can be dynamically nested when one depends on the other.")
(defvar *inherit-default* t
"Whether to inherit attr sessions by default.
Inheriting a session means that if there is already a session in
progress for root A, and you try to start a session for root B, then
if B is reachable from A the session for A is reused.")
;;; Classes
(defclass attrs-root ()
((subroot-map
:documentation "Tables from subroots to attributes and from subroots to dependencies."
:type (or subroot-map box)
:initarg :subroot-map))
(:documentation "Mixin that marks a class as a root.
This is important; it controls subroot copying behavior."))
(defmethod cl-store:serializable-slots :around ((self attrs-root))
(remove 'subroot-map
(call-next-method)
:key #'closer-mop:slot-definition-name))
(defmethod copy :around ((root attrs-root) &key)
"Carry forward (copying) the subroots from the old root."
(lret ((result (call-next-method)))
(if *enable-cross-session-cache*
(when-let (old-map
(and (slot-boundp root 'subroot-map)
(slot-value root 'subroot-map)))
(setf (slot-value result 'subroot-map)
(if (boxp old-map) old-map (box old-map))))
(slot-makunbound result 'subroot-map))))
(defclass subroot ()
()
(:documentation "Mixin that marks a class as a subroot.
This is for convenience and entirely equivalent to specializing
`subroot?' to return non-nil."))
(defgeneric subroot? (x)
(:documentation "Is X a subroot?")
(:method ((x t))
nil)
(:method ((x subroot))
t))
(defstruct-read-only (subroot-map
(:conc-name subroot-map.)
(:constructor
make-subroot-map
(&key (subroot->attr-table (make-weak-node-table))
(subroot->deps (make-weak-node-table))
(node->proxy (make-weak-node-table)))))
"Data structure associated with a root to track its subroots."
;; Table from subroots to attributes
(subroot->attr-table :type hash-table)
;; Table from subroots to dependencies
(subroot->deps :type hash-table)
;; Table of node proxies. These may be stored as attribute values so
;; they need to be copied along with the subroots.
(node->proxy :type hash-table))
(defmethod print-object ((self subroot-map) stream)
(print-unreadable-object (self stream :type t :identity t)))
(defstruct (cow-table
(:conc-name cow-table.)
(:copier nil))
"COW (copy-on-write) wrapper for a hash table."
(hash-table (make-weak-node-table) :type hash-table)
(hash-table-copied-p nil :type boolean))
(defun cow-table.on-write (cow-table)
"Do the actual on-write copy."
(unless (cow-table.hash-table-copied-p cow-table)
(callf #'copy-weak-node-table (cow-table.hash-table cow-table))
(setf (cow-table.hash-table-copied-p cow-table) t))
cow-table)
(defun copy-cow-table (cow-table)
"Copy COW-TABLE without copying the wrapped hash table."
(make-cow-table
:hash-table (cow-table.hash-table cow-table)
:hash-table-copied-p nil))
(defun cow-table.ref (cow-table key)
"Lookup KEY in COW-TABLE's wrapped hash table."
(gethash key (cow-table.hash-table cow-table)))
(defun ref (table key)
"Lookup KEY in a COW table or a bare hash table."
(etypecase table
(hash-table
(gethash key table))
(cow-table
(cow-table.ref table key))))
(defun (setf cow-table.ref) (value cow-table key)
"Set KEY to VALUE in COW-TABLE's wrapped hash table."
(symbol-macrolet ((ht (cow-table.hash-table cow-table)))
(if (eq value (@ ht key))
;; Optimization: don't copy if the value is the same.
value
(progn
(cow-table.on-write cow-table)
(setf (@ ht key) value)))))
(defun (setf ref) (value table key)
"Set KEY to VALUE in TABLE, a COW table or bare hash table."
(etypecase table
(hash-table
(setf (gethash key table) value))
(cow-table
(setf (cow-table.ref table key) value))))
(defun copy-subroot->attr-table (table)
"Copy the table from subroots to attr tables, ensuring each attr
table is a fresh table."
(lret ((table (copy-weak-node-table table)))
(iter (for (subroot attr-table) in-hashtable table)
(setf (@ table subroot)
(copy-cow-table attr-table)))))
(-> copy-subroot-map (subroot-map) subroot-map)
(defun copy-subroot-map (map)
"Copy MAP and its constituent tables.
The new subroot map should be fully independent of MAP."
(make-subroot-map
:subroot->attr-table
(copy-subroot->attr-table (subroot-map.subroot->attr-table map))
:subroot->deps (copy-weak-node-table (subroot-map.subroot->deps map))
:node->proxy (copy-weak-node-table (subroot-map.node->proxy map))))
(defstruct (attrs
(:conc-name attrs.)
(:constructor make-attrs (root)))
"An attribute session.
This holds at least the root of the attribute computation."
(root (error "No root") :type attrs-root :read-only t)
;; This is only used if *enable-cross-session-cache* is nil.
(table (make-weak-node-table) :type hash-table :read-only t)
(cachep *enable-cross-session-cache* :type boolean :read-only t)
;; TODO Consider moving the subroot mapping into the subroot-map.
;; That would enable getting rid of the session cache.
(node->subroot
(if *enable-cross-session-cache*
(compute-node->subroot-itree root)
(fset:convert 'ft/it:itree nil))
:type ft/it::itree)
;; Track live subroots.
(old-subroots nil :type list)
(old-subrootless nil :type list))
(defun node-subroot (node &key (attrs *attrs*) (error t))
"Look up the subroot for a node."
(setf node (fset:convert 'node node))
(when-let (itree-node
(ft/it::itree-find-node-splay
(attrs.node->subroot attrs)
(serial-number node)))
(let ((subroot (ft/it:node-data itree-node)))
(if (and (not (eql node subroot))
(subroot? node))
(and error
(error 'nested-subroots
:outer subroot
:inner node))
subroot))))
(defsubst attrs-root (attrs)
(attrs.root attrs))
(defsubst attrs-root* ()
"Get the root of the current attrs table."
(attrs-root *attrs*))
(defmethod fset:convert ((to (eql 'node)) (attrs attrs) &key)
(attrs-root attrs))
;;; Subroot implementation
(defun make-weak-node-table (&rest args)
"Make a weak 'eq hash table."
(multiple-value-call #'make-weak-hash-table
:weakness :key
:test #'eq
#+sbcl
(values :synchronized t)
(values-list args)))
(defun copy-weak-node-table (table-in &rest args)
"Copy a weak 'eq hash table."
(let ((table-out (apply #'make-weak-node-table :size (hash-table-size table-in) args)))
(iter (for (k v) in-hashtable table-in)
(setf (gethash k table-out) v))
table-out))
;;; A root node has an associated map of subroot nodes. When the root
;;; is copied, the new root gets its own copy of the map. The subroots
;;; are where the attributes are actually stored; each subroot has an
;;; attribute table.
;;; Subroots are invalidated as follows: of course, since functional
;;; trees are functional, when a given node is changed it and its
;;; parents (including its subroot) are replaced. Such replaced
;;; subroots that are no longer part of the tree are trivially
;;; invalid. Then, recursively, any subroot that depends on an invalid
;;; subroot is invalidated.
;;; Subroot dependencies are recorded when calculating the attributes.
;;; When an attribute is being calculated on a node, that node's
;;; dominating subroot is placed on a stack. All subroots already on
;;; the stack when calculating a node's attributes are marked as
;;; depending on the current nodes' dominating subroot.
(defun dominating-subroot (root node &key (error t))
"Find the dominating subroot of NODE."
(declare (optimize (debug 0))) ;allow tail recursion
;; TODO Enforce that subroots cannot be nested?
(let ((real-root (fset:convert 'node root))
(real-node (fset:convert 'node node)))
(cond ((eql real-root real-node) nil)
((subroot? real-node) real-node)
(t
(multiple-value-bind (rpath node-found)
(rpath-to-node real-root real-node)
(if (null rpath)
(if-let ((proxy (attr-proxy real-node)))
(dominating-subroot root proxy)
(progn
(when error
(cerror "Return nil"
'unreachable-node
:root root
:node node))
nil))
;; There's something at that path, but not this
;; identical node.
(when (eql node node-found)
(find-if #'subroot? rpath))))))))
(defun reachable-in-attrs? (node &key (proxy t))
"Does NODE have a subroot (or a proxy) recorded in the current
attribute tables?"
(when-let (attrs (bound-value '*attrs*))
(let ((node (fset:convert 'node node)))
(or (node-subroot node)
(when-let (proxy (and proxy (attr-proxy node)))
(node-subroot proxy))))))
(defun reachable-from-root? (root dest &key (proxy t))
"Is DEST reachable from ROOT? This is careful to check not just if
DEST has a path, but if DEST is the node at that path."
(let* ((root-node (fset:convert 'node root))
(dest-node (fset:convert 'node dest)))
(or (eql root-node dest-node)
(eql dest-node
(nth-value 1
(rpath-to-node root-node dest-node :error nil)))
(when (and proxy (boundp '*attrs*))
(when-let ((proxy (attr-proxy dest-node)))
(reachable-from-root? root proxy))))))
(defun reachable? (dest &key
(proxy t)
use-attrs
(from (attrs-root*)))
(if use-attrs
(or (reachable-in-attrs? dest :proxy proxy)
(reachable-from-root? from dest :proxy proxy))
(reachable-from-root? from dest :proxy proxy)))
(defun current-subroot (node)
"Return the dominating subroot for NODE."
(nlet retry ()
(let ((attrs-root (attrs-root *attrs*)))
(restart-case
(or (when (boundp '*attrs*)
(or (node-subroot node)
(when-let (p (attr-proxy node))
(node-subroot p))))
(dominating-subroot attrs-root node)
attrs-root)
(update-subroot-mapping ()
:report "Update node-subroot mapping in tree"
(update-subroot-mapping)
(retry))))))
(defun collect-subroots (node)
(declare (node node))
(let (subroots subrootless)
(labels ((collect-subroots (node)
(cond ((subroot? node)
(push node subroots))
((typep node 'node)
(push node subrootless)
(handler-case
(progn
(dolist (c (children node))
(collect-subroots c)))
(error ()
(do-child-slot-children (c node)
(when (typep c 'node)
(collect-subroots c)))))))))
(collect-subroots node))
(values subroots subrootless)))
(defun compute-node->subroot-itree (root &key old-subroots
old-subrootless-nodes
old-itree
&aux (root-node (fset:convert 'node root)))
(labels ((self-interval (node &aux (sn (serial-number node)))
;; "Rootless" asts are their own subroots.
(cons sn sn))
(rootless-mapping (node)
(list (self-interval node) root-node))
(subroot-intervals (subroot)
(mapcar (lambda (interval)
(list interval subroot))
(intervals-of-node subroot)))
(build-itree (current-subroots subrootless-nodes)
(if (not (or old-subroots old-subrootless-nodes))
(fset:convert
'ft/it:itree
(append
(mapcar #'rootless-mapping subrootless-nodes)
(mappend #'subroot-intervals current-subroots)))
(let ((removed-intervals
(append
;; These refer to the old root, now trivially invalid.
(mapcar #'self-interval old-subrootless-nodes)
(mappend
#'intervals-of-node
(set-difference/hash old-subroots current-subroots))))
(added-intervals
(append
;; Update to refer to the new root.
(mapcar #'rootless-mapping subrootless-nodes)
(mappend
#'subroot-intervals
(set-difference/hash current-subroots old-subroots)))))
;; TODO Handle collisions more explicably?
(ft/it:itree-add-intervals
(ft/it::itree-remove-intervals
old-itree
removed-intervals)
added-intervals)))))
(mvlet* ((current-subroots subrootless-nodes (collect-subroots root-node))
(itree (build-itree current-subroots subrootless-nodes)))
(values itree current-subroots subrootless-nodes))))
(defun update-subroot-mapping (&key (attrs *attrs*))
"Update the node-to-subroot mapping of ATTRS.
This should be done if the root has been mutated."
(declare (attrs attrs))
(setf (values (attrs.node->subroot attrs)
(attrs.old-subroots attrs)
(attrs.old-subrootless attrs))
(compute-node->subroot-itree
(attrs.root attrs)
:old-itree (attrs.node->subroot attrs)
:old-subroots (attrs.old-subroots attrs)
:old-subrootless-nodes (attrs.old-subrootless attrs)))
attrs)
(defun ensure-subroot-map (attrs)
"Ensure a subroot map for ATTRS.
This implements the subroot map copy-on-write behavior: after this
function is called, the root of ATTRs will have its own subroot map."
(let ((root (attrs-root attrs)))
(assert (slot-exists-p root 'subroot-map))
(with-slots (subroot-map) root
(if (slot-boundp root 'subroot-map)
(if (typep subroot-map 'subroot-map)
subroot-map
(setf subroot-map
(copy-subroot-map (unbox subroot-map))))
(setf subroot-map (make-subroot-map))))))
(defsubst attrs.subroot->attr-table (attrs)
"Ensure the subroots table for ATTRS."
(subroot-map.subroot->attr-table (ensure-subroot-map attrs)))
(defsubst attrs.subroot->deps (attrs)
"Ensure the subroot dependencies table for ATTRS."
(subroot-map.subroot->deps (ensure-subroot-map attrs)))
(defsubst attrs.node->proxy (attrs)
"Ensure the attr proxy table for ATTRS."
(subroot-map.node->proxy (ensure-subroot-map attrs)))
(defun node-attr-table (node)
(if *enable-cross-session-cache*
(cow-table (current-subroot node))
(attrs.table *attrs*)))
(defun cow-table (subroot)
"Get the attr table for SUBROOT in the current session.
This implements copy-on-write logic."
(ensure-gethash subroot
(attrs.subroot->attr-table *attrs*)
(make-cow-table)))
(defun subroot-deps (subroot)
"Get the dependencies of SUBROOT in the current session."
(gethash subroot (attrs.subroot->deps *attrs*)))
(defun (setf subroot-deps) (value subroot)
(setf (gethash subroot (attrs.subroot->deps *attrs*))
(remove-if (compose #'null #'tg:weak-pointer-value)
value)))
(defun attr-proxy (node)
"Get the attr proxy for NODE.
A node that is not in the tree can have a node in the tree as its
\"attribute proxy\".
This is useful for when, say, we need to answer a query with a node,
but the actual node in the tree is somehow unsuitable. We can return a
node proxied into the tree instead."
(lret ((proxy (@ (attrs.node->proxy *attrs*) node)))
(when (and proxy *enable-cross-session-cache*)
(unless (reachable? proxy :use-attrs t)
(error 'unreachable-proxy
:root (attrs-root*)
:proxy proxy
:node node)))))
(defun (setf attr-proxy) (proxy node)
(let* ((attrs *attrs*)
(root (attrs-root attrs))
(node->proxy (attrs.node->proxy *attrs*))
(proxy-subroot
(progn
;; Update the subroot mapping before querying it.
(update-subroot-mapping :attrs attrs)
(node-subroot proxy))))
(when-let (real-proxy (@ node->proxy proxy))
(when (@ node->proxy real-proxy)
;; This shouldn't be possible.
(error 'proxy-has-proxy
:node node
:proxy proxy))
(return-from attr-proxy
(setf (attr-proxy node) real-proxy)))
;; Can't proxy a node with a proxy.
;; A node can't proxy itself.
(when (eq proxy node)
(error 'self-proxy
:proxy proxy
:node node))
;; Proxying a node already in the tree would be useless.
(when (reachable? node :proxy nil :from root)
(error 'useless-proxy
:node node
:proxy proxy))
;; A node that's not in the tree can't be a proxy.
(unless proxy-subroot
(error 'unreachable-proxy
:root (attrs-root*)
:node node
:proxy proxy))
;; The node must not contain its proxy. (See below on proxying the
;; descendants of a node.)
(when (rpath-to-node node proxy)
(error 'node-contains-proxy
:node node
:proxy proxy))
;; If we are proxying to another subroot, the proxied node might
;; be recorded somewhere, making the proxy subroot an implicit
;; dependency of whatever attribute we're calculating.
(record-deps proxy :current-subroot proxy-subroot :root root)
;; It would be surprising to pull out parts of NODE and find later
;; they have no connection to PROXY. So all descendants of NODE
;; implicitly inherit PROXY (unless they have one already).
(labels ((set-proxy (node)
(ensure-gethash node node->proxy proxy)
(handler-case
(dolist (c (children node))
(set-proxy c))
(error ()
(do-child-slot-children (c node)
(when (typep c 'node)
(set-proxy c)))))))
(set-proxy node)))
proxy)
(defun call/attr-session (root fn &key
ensure
(inherit *inherit-default*)
(shadow t)
(cache *enable-cross-session-cache*)
(read-only *read-only*))
"Invoke FN with an attrs instance for ROOT.
ROOT might be an attrs instance itself.
If the active attrs instance has ROOT for its root, it is not
replaced.
ENSURE only ensures that some attribute session is active. If there is
an attribute session, this is equivalent to `(funcall fn)'. All other
options are discarded and ignored.
If CACHE is non-nil, attributes may be cached across sessions.
If INHERIT is non-nil, the dynamically enclosing attr session may be
reused if ROOT is reachable from the outer session's root.
If SHADOW is T, then the new attribute session may shadow an enclosing
attribute session. The behavior is different depending on INHERIT.
SHADOW T -> No error
SHADOW nil, INHERIT nil -> Error on shadowing
SHADOW nil, INHERIT T -> Error on shadowing, unless inherited"
(declare (optimize (debug 0)))
(when (and ensure (boundp '*attrs*))
(return-from call/attr-session (funcall fn)))
(let* ((new nil)
(shadowing? nil)
(*enable-cross-session-cache* cache)
(*attrs*
(cond
((typep root 'attrs) root)
((and cache (cache-lookup root)))
((and (boundp '*attrs*)
inherit
(reachable? root :from (attrs-root *attrs*)))
*attrs*)
((and (boundp '*attrs*)
(not shadow))
(error 'session-shadowing
:outer (attrs-root *attrs*)
:inner root
:inherit inherit))
(t
(lret ((attrs (make-attrs root)))
(when cache
(setf (cache-lookup root) attrs))
(setf new t
shadowing? t)))))
(*circle*
;; Don't continue cycles in the outer attribute session.
(if shadowing? nil *circle*))
(*subroot-stack*
;; Don't continue cycles in the outer attribute session.
(if shadowing? nil *subroot-stack*))
(*read-only*
(if shadowing? read-only *read-only*)))
(unless (eql cache (attrs.cachep *attrs*))
(error 'incompatible-cache-option))
;; The session is "new" if the root was unknown. But it could
;; still have a subroot map attached. If so, make sure it's up to
;; date.
(when (and (not *read-only*) cache new)
(invalidate-subroots *attrs*))
(funcall fn)))
(defun delete-invalid-proxies (attrs)
"Delete any invalid node->proxy mappings.
Node-to-proxy mappings are invalid when the proxy is no longer in the
tree, or when the node itself has been inserted into the tree."
(update-subroot-mapping :attrs attrs)
(let ((node->proxy (attrs.node->proxy attrs)))
(iter (for (node proxy) in-hashtable node->proxy)
;; Delete proxies that no longer point into the tree.
(unless (node-subroot proxy)
(remhash node node->proxy))
;; Delete proxies for nodes that have been inserted into the
;; tree.
(when (node-subroot node)
(remhash node node->proxy)))))
(defun dump-subroot-dependency-graph (attrs)
"Extract the subroot dependency graph from ATTRS for debugging."
(let ((seen (fset:empty-ch-set))
(subroot->attr-table (attrs.subroot->attr-table attrs))
(subroot->deps (attrs.subroot->deps attrs)))
(labels ((subroot-deps (subroot)
(if (fset:lookup seen subroot)
`(:circle ,subroot)
(progn
(fset:includef seen subroot)
(cons subroot
(mapcar (lambda (dep)
(subroot-deps
(tg:weak-pointer-value dep)))
(gethash subroot subroot->deps)))))))
(iter (for (subroot nil) in-hashtable subroot->attr-table)
(collect (subroot-deps subroot))))))
(defun invalidate-subroots (attrs)
"Recursively invalidate subroots in ATTRS.
Invalid subroots are either unreachable subroots, or
depend on invalid subroots."
(delete-invalid-proxies attrs)
(let ((subroot->attr-table (attrs.subroot->attr-table attrs))
(subroot->deps (attrs.subroot->deps attrs))
(removed (fset:empty-set)))
;; Remove unreachable subroots from the table.
(iter (for (subroot nil) in-hashtable subroot->attr-table)
;; Cheap reachability check.
(for reachable?
= (node-subroot subroot :attrs attrs :error nil))
(unless reachable?
(remhash subroot subroot->attr-table)
(remhash subroot subroot->deps)
(fset:includef removed subroot)))
;; Recursively uncache any subroot that depends on an
;; unreachable subroot.
(iter (for newly-removed-count =
(iter (for (subroot deps) in-hashtable subroot->deps)
(when (iter (for ptr in deps)
(for dep = (tg:weak-pointer-value ptr))
(thereis
(or (null dep) ;Already GC'd.
(not (gethash dep subroot->attr-table)))))
(remhash subroot subroot->deps)
(remhash subroot subroot->attr-table)
(fset:includef removed subroot)
(sum 1))))
(until (zerop newly-removed-count)))
removed))
(defun invalidate-subroot (subroot &key (attrs (bound-value '*attrs*)))
"Invalidate attributes on SUBROOT.
This should be used if SUBROOT is mutated."
(when attrs
(remhash subroot (attrs.subroot->attr-table attrs))
subroot))
(defun record-deps (node &key
(current-subroot (current-subroot node))
(root (attrs-root*))
(subroot-stack *subroot-stack*))
"Record CURRENT-SUBROOT as a dependency of the subroots in SUBROOT-STACK."
(unless (eql root current-subroot)
(iter (for depender in subroot-stack)
;; Avoid circular dependencies.
(unless (eql current-subroot depender)
(unless (member current-subroot
(subroot-deps depender)
:key #'tg:weak-pointer-value)
(push (tg:make-weak-pointer current-subroot)
(subroot-deps depender)))))))
(defun call/record-subroot-deps (node fn &aux (root (attrs-root*)))
(cond ((not *enable-cross-session-cache*)
(funcall fn))
((eql node root)
;; If we are computing top-down (after an attr-missing call),
;; mask the subroot stack.
(let ((*subroot-stack* nil))
(funcall fn)))
(t
(let* ((current-subroot (current-subroot node))
(*subroot-stack*
(if (subroot? current-subroot)
(adjoin current-subroot *subroot-stack*)
;; If there is no subroot but there is a subroot
;; stack, that also means we're computing
;; top-down and should mask the subroot stack.
nil)))
(record-deps node
:current-subroot current-subroot
:root root)
(funcall fn)))))
(defmacro with-record-subroot-deps ((node) &body body)
(with-thunk ((body :name record-subroot-deps))
`(call/record-subroot-deps ,node ,body)))
;;; API
(defmacro with-attr-table (root &body body)
"Create an ATTRS structure with root ROOT
and bind it to *ATTRS*, then evaluate BODY
in an implicit PROGN. If ROOT is an ATTRS
structure, simply bind *ATTRS* to it."
`(with-attr-session (,root)
,@body))
(defmacro with-attr-session ((root &rest args &key &allow-other-keys)
&body body)
"Like `with-attr-table', but allowing keyword arguments."
(with-thunk (body)
`(call/attr-session ,root ,body ,@args)))
(defmacro with-ensure-attr-session
((root &rest args &key &allow-other-keys)
&body body)
(with-thunk (body)
`(if (boundp '*attrs*)
,body
(with-attr-sesion (,root :shadow nil :inherit t ,@args)
,@body))))
(defmacro def-attr-fun (name (&rest optional-args) &body methods)
"Define an attribute.
The attribute function is always at least arity 1, the node whose
attribute is being computed; if OPTIONAL-ARGS are provided they are
the inputs for the attribute computation (e.g. in the symbol table,
the symbol tables of parents or prior siblings).
Attribute functions without OPTIONAL-ARGS are \"synthesized\"
attributes; attribute functions with OPTIONAL-ARGS are \"inherited\"
attributes.
If you are defining an inherited attribute you may also want to
specialize `attr-missing' to restart the computation of the attribute
from the root node, if it has not been computed for the node passed in
here.
To define a circular attribute, supply the `(:circular)' option with
two arguments.. The first argument is the converge test; the second
value is a function to call to get the bottom value \(defaults to
`nil' if not provided). Both arguments are evaluated in the lexical
environment of the point of definition.. You can return multiple
values in the bottom thunk, but returning no values means an attribute
will not be considered circular.