Skip to content
← Back

src/syntax/process/infer-effects/effect_solvers.ghul

1
namespace Syntax.Process is
2
use Function = Semantic.Symbols.Function
3
use Symbol = Semantic.Symbols.Symbol
4
use Semantic.Symbols.STORE_FREE_IMPORTS
5
6
// Solves the whole-program effect relations the crossing discharge
7
// reads, over the same per-function facts STORE_FREE_FIXPOINT
8
// consumes: member-granular may-write and may-write-null sets, the
9
// store-free set, and member read closures. Nothing here writes a
10
// store-free bit; the solved sets are installed into EFFECTS.
11
// One solve's rows, keyed on the functions, kept so the next solve
12
// can start from them: a row whose inputs did not change, directly
13
// or through anything it absorbs, is the same row.
14
class EFFECT_SOLUTION(
15
write_sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
16
write_unbounded: Collections.SET[Function],
17
null_sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
18
null_unbounded: Collections.SET[Function],
19
store_free: Collections.SET[Function],
20
constructs_store_free: Collections.SET[Function],
21
read_sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
22
read_unbounded: Collections.SET[Function],
23
reads_elements: Collections.SET[Function],
24
writes_elements: Collections.SET[Function]
25
)
26
27
class EFFECT_SOLVERS is
28
_facts: Collections.MutableMap[Function, STORE_FREE_FACTS]
29
30
// Set to solve from a previous solution: only the rows in
31
// `_affected` are re-derived, every other row is taken from
32
// `_previous` as it stands.
33
_previous: EFFECT_SOLUTION?
34
_affected: Collections.SET[Function]?
35
36
// Functions that invoke a function-typed parameter, directly
37
// or by forwarding one to something that does. Their write
38
// sets absorb the write sets of every function value passed
39
// to them at any recorded call site.
40
_param_invokers: Collections.SET[Function]?
41
42
_dependants: EFFECT_DEPENDANTS?
43
44
init(facts: Collections.MutableMap[Function, STORE_FREE_FACTS]) is
45
_facts = facts
46
si
47
48
dependants: EFFECT_DEPENDANTS is
49
if _dependants? then
50
return _dependants
51
fi
52
53
let result = EFFECT_DEPENDANTS(_facts, param_invokers)
54
55
_dependants = result
56
57
return result
58
si
59
60
// Solve from `previous`, re-deriving only the rows of the
61
// functions whose own inputs changed and of everything that
62
// absorbs one of them, directly or through a chain. A row
63
// outside that closure has exactly the inputs it had, so it
64
// keeps its previous value.
65
seed_from(previous: EFFECT_SOLUTION, changed: Collections.Iterable[Function]) is
66
let affected = Collections.SET[Function]()
67
let frontier = Collections.LIST[Function]()
68
69
for function in changed do
70
if affected.add(function) then
71
frontier.add(function)
72
fi
73
od
74
75
let i mut = 0
76
77
while i < frontier.count do
78
let target = frontier[i]
79
i = i + 1
80
81
for functions in [dependants.dependants_of(target), dependants.dependants_through_unwalked(target)] do
82
if !functions? then
83
continue
84
fi
85
86
for function in functions do
87
if affected.add(function) then
88
frontier.add(function)
89
fi
90
od
91
od
92
od
93
94
_previous = previous
95
_affected = affected
96
si
97
98
// The functions whose rows this solve derives: every walked
99
// function, or the affected ones when seeded.
100
affected_count: int => if let affected = _affected then affected.count else _facts.count fi
101
102
// Whether `function`'s row is derived here rather than kept.
103
_is_derived(function: Function) -> bool is
104
if !_previous? \/ !_affected? then
105
return true
106
fi
107
108
return _affected.contains(function)
109
si
110
111
// The rows derived by this solve, in a stable order.
112
_derived: Collections.Iterable[Function] is
113
if let affected = _affected then
114
let result = Collections.LIST[Function]()
115
116
for function in affected do
117
if _facts.contains_key(function) then
118
result.add(function)
119
fi
120
od
121
122
return result
123
fi
124
125
return _facts.keys
126
si
127
128
param_invokers: Collections.SET[Function] is
129
if _param_invokers? then
130
return _param_invokers
131
fi
132
133
let invokers = Collections.SET[Function]()
134
135
for function in _facts.keys do
136
let record = EFFECT_FACTS.record_for(function)
137
138
if record? /\ record.invokes_function_param then
139
invokers.add(function)
140
fi
141
od
142
143
let changed mut = true
144
145
while changed do
146
changed = false
147
148
for function in _facts.keys do
149
if invokers.contains(function) then
150
continue
151
fi
152
153
let record = EFFECT_FACTS.record_for(function)
154
155
if !record? then
156
continue
157
fi
158
159
for target in record.forwards_param_to do
160
if invokers.contains(target) then
161
invokers.add(function)
162
changed = true
163
fi
164
od
165
od
166
od
167
168
_param_invokers = invokers
169
170
return invokers
171
si
172
173
// The store-free relation, solved by STORE_FREE_FIXPOINT
174
// itself so the two can never drift apart. The construction
175
// relation rides along: a constructor in it writes nothing
176
// beyond its own fresh receiver's state, which no
177
// pre-existing fact can see.
178
solve() -> (
179
store_free: Collections.SET[Function],
180
constructs_store_free: Collections.SET[Function]
181
) is
182
let solved = STORE_FREE_FIXPOINT().solve(_facts, dependants, _previous, _affected)
183
184
let safe = Collections.SET[Function]()
185
let construction_safe = Collections.SET[Function]()
186
187
for function in _facts.keys do
188
if !solved.unsafe.contains(function) then
189
safe.add(function)
190
fi
191
192
if !solved.construction_unsafe.contains(function) then
193
construction_safe.add(function)
194
fi
195
od
196
197
return (store_free = safe, constructs_store_free = construction_safe)
198
si
199
200
// What each function may write, at member-symbol granularity:
201
// its own writes, plus those of everything it can dispatch to.
202
// `unbounded` names the functions for which no such set exists
203
// - the walk lost track, a callee was not walked, or an
204
// override outside the compilation could write anything.
205
//
206
// Receiver-blind, exactly as NARROWING_FLOW.on_member_store is:
207
// a write to `other.f` and one to `self.f` are the same entry,
208
// because the two receivers may alias.
209
write_sets(close_open: bool, trust_imports: bool) -> (
210
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
211
unbounded: Collections.SET[Function]
212
) is
213
let sets = Collections.MAP[Function, Collections.SET[Symbol]]()
214
let unbounded = Collections.SET[Function]()
215
let shadow = STORE_FREE_FIXPOINT()
216
217
let previous = _previous
218
219
for function in _facts.keys do
220
if !_is_derived(function) /\ previous? /\ previous.write_sets.contains_key(function) then
221
sets[function] = previous.write_sets[function]
222
223
if previous.write_unbounded.contains(function) then
224
unbounded.add(function)
225
fi
226
227
continue
228
fi
229
230
// A body marked @trusted_store_free() writes nothing of
231
// its own and is not opened by overrides elsewhere, so it
232
// is seeded empty, as an import on the curated list is
233
// never seeded at all.
234
if function.is_trusted_store_free_declared then
235
sets[function] = Collections.SET[Symbol]()
236
continue
237
fi
238
239
let set = Collections.SET[Symbol]()
240
let record = EFFECT_FACTS.record_for(function)
241
242
if record? then
243
for member in record.writes do
244
set.add(member)
245
od
246
247
if record.writes_unbounded then
248
unbounded.add(function)
249
fi
250
else
251
unbounded.add(function)
252
fi
253
254
// An override in another assembly could write anything.
255
// `close_open` drops that, which is the whole-program
256
// assumption a self-contained program like the compiler
257
// could legitimately make.
258
if !close_open /\ shadow.is_openly_dispatchable(function) then
259
unbounded.add(function)
260
fi
261
262
sets[function] = set
263
od
264
265
// Every row absorbs all its targets once; from then on a
266
// target is re-absorbed only into the rows that depend on
267
// it, and only when its own row changed. A row's set only
268
// grows and its unbounded flag only sets, so a target that
269
// has not changed since it was absorbed has nothing new to
270
// contribute.
271
let changed = Collections.SET[Function]()
272
273
for function in _derived do
274
let before = sets[function].count
275
276
if _absorb(function, sets, unbounded, trust_imports) \/ sets[function].count != before then
277
changed.add(function)
278
fi
279
od
280
281
let dirty mut = changed
282
283
while dirty.count > 0 do
284
let next = Collections.SET[Function]()
285
286
for target in dirty do
287
let dependants = self.dependants.dependants_of(target)
288
289
if !dependants? then
290
continue
291
fi
292
293
for function in dependants do
294
if function.is_trusted_store_free_declared then
295
let trusted_before = sets[function].count
296
297
if _absorb(function, sets, unbounded, trust_imports) \/ sets[function].count != trusted_before then
298
next.add(function)
299
fi
300
301
continue
302
fi
303
304
if unbounded.contains(function) then
305
continue
306
fi
307
308
let before = sets[function].count
309
310
if _absorb_target(function, target, sets, unbounded, trust_imports) \/ sets[function].count != before then
311
next.add(function)
312
fi
313
od
314
od
315
316
dirty = next
317
od
318
319
return (sets = sets, unbounded = unbounded)
320
si
321
322
// Pull every dispatch target's writes into `function`'s set.
323
// Returns whether the function became unbounded.
324
_absorb(
325
function: Function,
326
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
327
unbounded: Collections.SET[Function],
328
trust_imports: bool
329
) -> bool is
330
// Once unbounded, nothing more can change: queries answer
331
// conservatively without reading the member set, and
332
// callers read only the unbounded flag.
333
if unbounded.contains(function) then
334
return false
335
fi
336
337
// A body marked @trusted_store_free() contributes nothing of its
338
// own: what a call can reach is its judged overriders.
339
if function.is_trusted_store_free_declared then
340
return _absorb_target(function, function, sets, unbounded, trust_imports)
341
fi
342
343
let became mut = false
344
345
for callee in _facts[function].callees do
346
if _absorb_target(function, callee, sets, unbounded, trust_imports) then
347
became = true
348
fi
349
od
350
351
for callee in _facts[function].construction_callees do
352
if _absorb_target(function, callee, sets, unbounded, trust_imports) then
353
became = true
354
fi
355
od
356
357
// Effect-polymorphic absorption: an invoked function-typed
358
// parameter is bounded by the values recorded flowing in
359
// at the function's call sites; a value nothing could name
360
// makes the invocation unboundable after all.
361
if param_invokers.contains(function) then
362
if EFFECT_FACTS.passed_opaque.contains(function) then
363
if !unbounded.contains(function) then
364
unbounded.add(function)
365
became = true
366
fi
367
else
368
let passed = EFFECT_FACTS.passed_functions_for(function)
369
370
if passed? then
371
for value in passed do
372
if _absorb_target(function, value, sets, unbounded, trust_imports) then
373
became = true
374
fi
375
od
376
fi
377
fi
378
fi
379
380
let overriders = function.overriders
381
382
if overriders? then
383
for overrider in overriders do
384
if !isa Function(overrider) then
385
if !unbounded.contains(function) then
386
unbounded.add(function)
387
became = true
388
fi
389
390
continue
391
fi
392
393
if _absorb_target(function, cast Function(overrider), sets, unbounded, trust_imports) then
394
became = true
395
fi
396
od
397
fi
398
399
return became
400
si
401
402
_absorb_target(
403
function: Function,
404
target: Function,
405
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
406
unbounded: Collections.SET[Function],
407
trust_imports: bool
408
) -> bool is
409
if target.is_declared_pure then
410
return false
411
fi
412
413
// A body marked @trusted_store_free() writes nothing; its
414
// overriders outside the libraries are still judged, as an
415
// import's in-assembly overriders are.
416
if target.is_trusted_store_free_declared then
417
let became mut = false
418
419
for overrider in _judged_overriders(target) do
420
became = _absorb_target(function, overrider, sets, unbounded, trust_imports) \/ became
421
od
422
423
return became
424
fi
425
426
if !sets.contains_key(target) then
427
// not walked here: a trusted store-free import writes
428
// nothing, a receiver-interior mutator writes only its
429
// own receiver's internal state - never a ghul member -
430
// and anything else could write anything
431
if
432
trust_imports \/
433
STORE_FREE_IMPORTS.is_store_free(target) \/
434
target.writes_only_receiver_interior
435
then
436
return false
437
fi
438
439
unbounded.add(function)
440
return true
441
fi
442
443
if unbounded.contains(target) then
444
// the caller's members no longer matter either, so
445
// skip the union
446
unbounded.add(function)
447
return true
448
fi
449
450
let function_set = sets[function]
451
452
for member in sets[target] do
453
function_set.add(member)
454
od
455
456
return false
457
si
458
459
// The member-keyed may-null relation: which members each
460
// function (or anything it dispatches to) may assign a
461
// possibly-absent value. Open dispatch makes the set
462
// unboundable.
463
//
464
// `trust_imports` selects the import model. Trusted, an
465
// unwalked callee contributes nothing, on the argument that
466
// imported code cannot assign a ghul member except by
467
// re-entering ghul code - an argument with a known leak
468
// (delegate re-entry), so it is a measurement configuration
469
// only. Untrusted - the production setting - an unwalked
470
// callee that is not declared pure and not on the curated
471
// store-free whitelist makes the caller unboundable.
472
may_null_sets(close_open: bool, trust_imports: bool) -> (
473
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
474
unbounded: Collections.SET[Function]
475
) is
476
let sets = Collections.MAP[Function, Collections.SET[Symbol]]()
477
let unbounded = Collections.SET[Function]()
478
let shadow = STORE_FREE_FIXPOINT()
479
480
let previous = _previous
481
482
for function in _facts.keys do
483
if !_is_derived(function) /\ previous? /\ previous.null_sets.contains_key(function) then
484
sets[function] = previous.null_sets[function]
485
486
if previous.null_unbounded.contains(function) then
487
unbounded.add(function)
488
fi
489
490
continue
491
fi
492
493
// A body marked @trusted_store_free() writes nothing of
494
// its own and is not opened by overrides elsewhere, so it
495
// is seeded empty, as an import on the curated list is
496
// never seeded at all.
497
if function.is_trusted_store_free_declared then
498
sets[function] = Collections.SET[Symbol]()
499
continue
500
fi
501
502
let set = Collections.SET[Symbol]()
503
let record = EFFECT_FACTS.record_for(function)
504
505
if record? then
506
for m in record.nulling_writes do
507
set.add(m)
508
od
509
510
// an unbounded write could write null just as well
511
// as it could write anything else, so the nulling
512
// set is only a bound where the write set is
513
if record.nulling_unbounded \/ record.writes_unbounded then
514
unbounded.add(function)
515
fi
516
else
517
unbounded.add(function)
518
fi
519
520
if !close_open /\ shadow.is_openly_dispatchable(function) then
521
unbounded.add(function)
522
fi
523
524
sets[function] = set
525
od
526
527
let changed = Collections.SET[Function]()
528
529
for function in _derived do
530
if _absorb_nulling(function, sets, unbounded, trust_imports) then
531
changed.add(function)
532
fi
533
od
534
535
let dirty mut = changed
536
537
while dirty.count > 0 do
538
let next = Collections.SET[Function]()
539
540
for target in dirty do
541
let dependants = self.dependants.dependants_of(target)
542
543
if !dependants? then
544
continue
545
fi
546
547
for function in dependants do
548
if function.is_trusted_store_free_declared then
549
let trusted_before = sets[function].count
550
551
if _absorb_nulling(function, sets, unbounded, trust_imports) \/ sets[function].count != trusted_before then
552
next.add(function)
553
fi
554
555
continue
556
fi
557
558
if unbounded.contains(function) then
559
continue
560
fi
561
562
if _absorb_nulling_target(function, target, sets, unbounded, trust_imports) then
563
next.add(function)
564
fi
565
od
566
od
567
568
dirty = next
569
od
570
571
return (sets = sets, unbounded = unbounded)
572
si
573
574
_absorb_nulling(
575
function: Function,
576
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
577
unbounded: Collections.SET[Function],
578
trust_imports: bool
579
) -> bool is
580
// Once unbounded, nothing more can change - see _absorb.
581
if unbounded.contains(function) then
582
return false
583
fi
584
585
if function.is_trusted_store_free_declared then
586
return _absorb_nulling_target(function, function, sets, unbounded, trust_imports)
587
fi
588
589
let became mut = false
590
591
for callee in _facts[function].callees do
592
if _absorb_nulling_target(function, callee, sets, unbounded, trust_imports) then
593
became = true
594
fi
595
od
596
597
for callee in _facts[function].construction_callees do
598
if _absorb_nulling_target(function, callee, sets, unbounded, trust_imports) then
599
became = true
600
fi
601
od
602
603
if param_invokers.contains(function) then
604
if EFFECT_FACTS.passed_opaque.contains(function) then
605
if !unbounded.contains(function) then
606
unbounded.add(function)
607
became = true
608
fi
609
else
610
let passed = EFFECT_FACTS.passed_functions_for(function)
611
612
if passed? then
613
for value in passed do
614
if _absorb_nulling_target(function, value, sets, unbounded, trust_imports) then
615
became = true
616
fi
617
od
618
fi
619
fi
620
fi
621
622
let overriders = function.overriders
623
624
if overriders? then
625
for overrider in overriders do
626
if !isa Function(overrider) then
627
if !unbounded.contains(function) then
628
unbounded.add(function)
629
became = true
630
fi
631
632
continue
633
fi
634
635
if _absorb_nulling_target(function, cast Function(overrider), sets, unbounded, trust_imports) then
636
became = true
637
fi
638
od
639
fi
640
641
return became
642
si
643
644
_absorb_nulling_target(
645
function: Function,
646
target: Function,
647
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
648
unbounded: Collections.SET[Function],
649
trust_imports: bool
650
) -> bool is
651
if target.is_declared_pure then
652
return false
653
fi
654
655
if target.is_trusted_store_free_declared then
656
let became mut = false
657
658
for overrider in _judged_overriders(target) do
659
became = _absorb_nulling_target(function, overrider, sets, unbounded, trust_imports) \/ became
660
od
661
662
return became
663
fi
664
665
if !sets.contains_key(target) then
666
// Same tiers as _absorb_target; a receiver-interior
667
// mutator cannot assign a ghul member at all, null or
668
// otherwise.
669
if
670
trust_imports \/
671
STORE_FREE_IMPORTS.is_store_free(target) \/
672
target.writes_only_receiver_interior
673
then
674
return false
675
fi
676
677
unbounded.add(function)
678
return true
679
fi
680
681
if unbounded.contains(target) then
682
// the caller's members no longer matter either, so
683
// skip the union
684
unbounded.add(function)
685
return true
686
fi
687
688
let became mut = false
689
let function_set = sets[function]
690
691
for m in sets[target] do
692
if !function_set.contains(m) then
693
function_set.add(m)
694
became = true
695
fi
696
od
697
698
return became
699
si
700
701
// The member read closure: which members each function - or
702
// anything it can dispatch to or invoke - may read, plus
703
// whether it may read or write collection/array elements.
704
// Consumed by the crossing discharge for facts read through a
705
// getter: a crossing callee whose write set is disjoint from
706
// the getter's read closure provably leaves the getter's
707
// answer alone. An unwalked callee makes the reads
708
// unboundable, except the curated import tiers: a store-free
709
// import reads only its receiver's interior, and a
710
// receiver-interior mutator additionally writes it.
711
read_sets() -> (
712
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
713
unbounded: Collections.SET[Function],
714
reads_elements: Collections.SET[Function],
715
writes_elements: Collections.SET[Function]
716
) is
717
let sets = Collections.MAP[Function, Collections.SET[Symbol]]()
718
let unbounded = Collections.SET[Function]()
719
let reads_elements = Collections.SET[Function]()
720
let writes_elements = Collections.SET[Function]()
721
let shadow = STORE_FREE_FIXPOINT()
722
723
let previous = _previous
724
725
for function in _facts.keys do
726
if !_is_derived(function) /\ previous? /\ previous.read_sets.contains_key(function) then
727
sets[function] = previous.read_sets[function]
728
729
if previous.read_unbounded.contains(function) then
730
unbounded.add(function)
731
fi
732
733
if previous.reads_elements.contains(function) then
734
reads_elements.add(function)
735
fi
736
737
if previous.writes_elements.contains(function) then
738
writes_elements.add(function)
739
fi
740
741
continue
742
fi
743
744
let set = Collections.SET[Symbol]()
745
let record = EFFECT_FACTS.record_for(function)
746
747
if record? then
748
for m in record.reads do
749
set.add(m)
750
od
751
752
if record.reads_elements then
753
reads_elements.add(function)
754
fi
755
756
if record.writes_elements then
757
writes_elements.add(function)
758
fi
759
760
if record.own_unbounded \/ record.writes_unbounded then
761
unbounded.add(function)
762
fi
763
else
764
unbounded.add(function)
765
fi
766
767
if shadow.is_openly_dispatchable(function) then
768
unbounded.add(function)
769
writes_elements.add(function)
770
fi
771
772
sets[function] = set
773
od
774
775
let changed = Collections.SET[Function]()
776
777
for function in _derived do
778
if _absorb_reads(function, sets, unbounded, reads_elements, writes_elements) then
779
changed.add(function)
780
fi
781
od
782
783
let dirty mut = changed
784
785
// An unbounded row is not skipped here: the element flags
786
// propagate through it regardless of its closure.
787
while dirty.count > 0 do
788
let next = Collections.SET[Function]()
789
790
for target in dirty do
791
let dependants = self.dependants.dependants_of(target)
792
793
if !dependants? then
794
continue
795
fi
796
797
for function in dependants do
798
if _absorb_reads_target(function, target, sets, unbounded, reads_elements, writes_elements) then
799
next.add(function)
800
fi
801
od
802
od
803
804
dirty = next
805
od
806
807
return (sets = sets, unbounded = unbounded, reads_elements = reads_elements, writes_elements = writes_elements)
808
si
809
810
_absorb_reads(
811
function: Function,
812
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
813
unbounded: Collections.SET[Function],
814
reads_elements: Collections.SET[Function],
815
writes_elements: Collections.SET[Function]
816
) -> bool is
817
let became mut = false
818
819
for callee in _facts[function].callees do
820
if _absorb_reads_target(function, callee, sets, unbounded, reads_elements, writes_elements) then
821
became = true
822
fi
823
od
824
825
for callee in _facts[function].construction_callees do
826
if _absorb_reads_target(function, callee, sets, unbounded, reads_elements, writes_elements) then
827
became = true
828
fi
829
od
830
831
if param_invokers.contains(function) then
832
if EFFECT_FACTS.passed_opaque.contains(function) then
833
became = _add_once(unbounded, function) \/ became
834
became = _add_once(writes_elements, function) \/ became
835
else
836
let passed = EFFECT_FACTS.passed_functions_for(function)
837
838
if passed? then
839
for value in passed do
840
if _absorb_reads_target(function, value, sets, unbounded, reads_elements, writes_elements) then
841
became = true
842
fi
843
od
844
fi
845
fi
846
fi
847
848
let overriders = function.overriders
849
850
if overriders? then
851
for overrider in overriders do
852
if !isa Function(overrider) then
853
became = _add_once(unbounded, function) \/ became
854
became = _add_once(writes_elements, function) \/ became
855
856
continue
857
fi
858
859
if _absorb_reads_target(function, cast Function(overrider), sets, unbounded, reads_elements, writes_elements) then
860
became = true
861
fi
862
od
863
fi
864
865
return became
866
si
867
868
// The overriders of a trusted body that this compilation judges:
869
// those outside the library sources.
870
_judged_overriders(target: Function) -> Collections.LIST[Function] is
871
let result = Collections.LIST[Function]()
872
873
if let overriders = target.overriders then
874
for overrider in overriders do
875
if let function: Function = overrider then
876
if !STORE_FREE_FIXPOINT.is_library_member(function) then
877
result.add(function)
878
fi
879
fi
880
od
881
fi
882
883
return result
884
si
885
886
_absorb_reads_target(
887
function: Function,
888
target: Function,
889
sets: Collections.MutableMap[Function, Collections.SET[Symbol]],
890
unbounded: Collections.SET[Function],
891
reads_elements: Collections.SET[Function],
892
writes_elements: Collections.SET[Function]
893
) -> bool is
894
let became mut = false
895
896
if !sets.contains_key(target) then
897
// not walked here. A curated store-free import reads
898
// only its receiver's interior; a receiver-interior
899
// mutator also writes it. A declared-pure import
900
// stores nothing but can read anything; everything
901
// else could do either.
902
// Declared purity is checked before the
903
// receiver-interior tier: `pure` implies the
904
// store-free bit, which the receiver-interior test
905
// accepts, but a pure function promises nothing
906
// about what it reads.
907
if STORE_FREE_IMPORTS.is_store_free(target) then
908
became = _add_once(reads_elements, function)
909
elif target.is_declared_pure then
910
became = _add_once(unbounded, function)
911
elif target.writes_only_receiver_interior then
912
became = _add_once(reads_elements, function) \/ became
913
became = _add_once(writes_elements, function) \/ became
914
else
915
became = _add_once(unbounded, function) \/ became
916
became = _add_once(writes_elements, function) \/ became
917
fi
918
919
return became
920
fi
921
922
if unbounded.contains(target) then
923
became = _add_once(unbounded, function) \/ became
924
elif !unbounded.contains(function) then
925
// the member union matters only while both closures
926
// are still bounded - an unbounded closure answers
927
// null whatever its set holds. The element flags
928
// below still propagate regardless, because
929
// may_write_elements consults them independently of
930
// the read closure.
931
let function_set = sets[function]
932
933
for m in sets[target] do
934
if !function_set.contains(m) then
935
function_set.add(m)
936
became = true
937
fi
938
od
939
fi
940
941
if reads_elements.contains(target) then
942
became = _add_once(reads_elements, function) \/ became
943
fi
944
945
if writes_elements.contains(target) then
946
became = _add_once(writes_elements, function) \/ became
947
fi
948
949
return became
950
si
951
952
_add_once(set: Collections.SET[Function], function: Function) -> bool static is
953
if set.contains(function) then
954
return false
955
fi
956
957
set.add(function)
958
return true
959
si
960
si
961
si