-
Notifications
You must be signed in to change notification settings - Fork 68
/
discrimination.dylan
1257 lines (1161 loc) · 47.7 KB
/
discrimination.dylan
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
language: infix-dylan
module: dispatch-engine-internal
Copyright: Original Code is Copyright (c) 1995-2004 Functional Objects, Inc.
All rights reserved.
License: See License.txt in this distribution for details.
Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
define function compute-headed-methods (ds :: <dispatch-state>)
=> ()
let headed-methods :: <pair> = pair(#f, #());
for (m in generic-function-methods(%ds-gf(ds)), ptr :: <pair> = headed-methods then tail(ptr))
tail(ptr) := pair(m, #())
end for;
%ds-headed-methods(ds) := headed-methods;
end function;
define function prune-methods-by-known-class
(argnum :: <integer>, cls :: <class>, ds :: <dispatch-state>)
=> (all-accounted-for? :: <boolean>);
let headed-methods :: <pair> = %ds-headed-methods(ds);
local method loop (prev :: <pair>, subl :: <list>, allall?)
if (subl == #())
allall?
else
let meth :: <method> = primitive-the(<method>, head(subl));
let nxt :: <list> = %proper-list-tail(subl);
let (some?, all?) = grounded-has-instances?(cls, %method-specializer(meth, argnum));
if (some?)
loop(subl, nxt, all? & allall?)
else
tail(prev) := nxt;
loop(prev, nxt, allall?)
end if
end if
end method;
let the-methods :: <list> = %proper-list-tail(headed-methods);
loop(headed-methods, the-methods, #t)
end function;
define function consider-arg-discriminated (ds :: <dispatch-state>, argnum :: <integer>, arg)
=> ()
local method loop (prev :: <pair>, subl :: <list>)
if (subl == #())
%ds-add-argnum(argnum, ds)
else
let meth :: <method> = primitive-the(<method>, head(subl));
if (grounded-instance?(arg, %method-specializer(meth, argnum)))
loop(subl, %proper-list-tail(subl))
else
let nxt :: <list> = %proper-list-tail(subl);
tail(prev) := nxt;
loop(prev, nxt)
end if
end if
end method;
let headed-methods :: <pair> = %ds-headed-methods(ds);
let the-methods :: <list> = %proper-list-tail(headed-methods);
loop(headed-methods, the-methods)
end function;
define function compute-dispatch-engine (ds :: <dispatch-state>)
=> (new-engine)
// Now, we have to figure out how to deal with various combinations of
// cache-header-engine-nodes and caches.
let cache = %ds-cache(ds);
let parent :: <dispatch-starter> = %ds-parent(ds);
if (~*call-site-caches-enabled?*)
select (parent by instance?)
<common-root-cache-header-engine-node> =>
handle-simple-call-site-cache-head(ds, cache, parent);
<cache-header-engine-node> =>
cache-header-punt(ds, cache, parent);
otherwise =>
handle-standard-dispatch-miss(ds, cache, parent)
end select
else
select (parent by instance?)
<generic-function> =>
handle-standard-dispatch-miss(ds, cache, parent);
<simple-typechecked-cache-header-engine-node> =>
handle-simple-typechecked-cache-head(ds, cache, parent);
<common-root-cache-header-engine-node> =>
handle-simple-call-site-cache-head(ds, cache, parent);
<simple-call-site-cache-header-engine-node> =>
handle-simple-call-site-cache-head(ds, cache, parent);
<profiling-call-site-cache-header-engine-node> =>
handle-profiling-call-site-cache-head(ds, cache, parent);
<cache-header-engine-node> =>
handle-unknown-cache-head(ds, cache, parent);
end select
end if
end function;
//// Handle ordinary dispatch miss.
define function handle-standard-dispatch-miss (ds :: <dispatch-state>, cache, parent)
=> (root-engine);
compute-headed-methods(ds);
compute-argument-precheck-mask(ds, cache);
let parent :: <dispatch-starter> = %ds-parent(ds);
compute-dispatch-from-root(ds, parent);
end function;
define function compute-dispatch-from-root (ds :: <dispatch-state>, parent :: <dispatch-starter>)
=> (root-engine)
let oengine = dispatch-start(parent);
let nengine = walk-existing-dispatch-engine(ds, oengine, walk-existing-dispatch-engine);
if (oengine ~== nengine)
subst-engine-node(nengine, oengine, ds);
dispatch-start(parent) := nengine;
end if;
nengine
end function;
define inline function subst-engine-node (new-engine-node, old-engine-node, ds :: <dispatch-state>)
=> ()
if (~pointer-id?(new-engine-node, old-engine-node)
& old-engine-node ~== $absent-engine-node)
subst-engine-node-1(new-engine-node, old-engine-node, ds)
end if
end function;
define macro engine-node-subster
{ engine-node-subster(?new:expression, ?old:expression, ?place:expression) }
=>
{ begin
let _new_ = ?new;
let _old_ = ?old;
let _v_ = ?place;
if (pointer-id?(_old_, _v_))
?place := _new_
else
subst-engine-node-2(_new_, _old_, _v_)
end if
end
}
end macro;
define function subst-engine-node-1 (new-e, old-e, ds :: <dispatch-state>) => ()
let cache = %ds-cache(ds);
select (cache by instance?)
<simple-typechecked-gf-cache-info> =>
// For these guys, we short-circuit walking all the cache-headers by
// just walking the entries, since all cache header contents should just
// be pointing to these. Then we just update the 'next' pointers of the
// cache-headers in case a top-level entry has changed.
let cache :: <simple-typechecked-gf-cache-info> = cache;
let vec :: <simple-object-vector> = simple-typechecked-gf-cache-info-entries(cache);
for (i :: <integer> from 0 below size(vec))
engine-node-subster(new-e, old-e, vector-element(vec, i))
end for;
for (e :: false-or(<cache-header-engine-node>)
in gf-cache-info-users(cache),
while: e)
if (pointer-id?(cache-header-engine-node-next(e), old-e))
cache-header-engine-node-next(e) := new-e
end if
end for;
<gf-cache-info> =>
// By default, walk/replace in all the cache header users of the g.f.
let cache :: <gf-cache-info> = cache;
let vec :: <simple-object-vector> = gf-cache-info-users(cache);
for (i :: <integer> from 0 below size(vec))
engine-node-subster(new-e, old-e, vector-element(vec, i));
end for;
otherwise => #f;
end select;
end function;
define function subst-engine-node-2 (new-e, old-e, e) => ()
select (e by instance?)
<monomorphic-by-class-discriminator> =>
let e :: <monomorphic-by-class-discriminator> = e;
engine-node-subster(new-e, old-e, monomorphic-by-class-discriminator-next(e));
<class-keyed-discriminator> =>
let e :: <class-keyed-discriminator> = e;
for (i :: <integer> from 1 below ckd-size(e) by 2)
engine-node-subster(new-e, old-e, ckd-ref(e, i))
end for;
engine-node-subster(new-e, old-e, grounded-class-keyed-discriminator-default(e));
<if-type-discriminator> =>
let e :: <if-type-discriminator> = e;
engine-node-subster(new-e, old-e, if-type-discriminator-then(e));
engine-node-subster(new-e, old-e, if-type-discriminator-else(e));
<typecheck-discriminator> =>
let e :: <typecheck-discriminator> = e;
engine-node-subster(new-e, old-e, typecheck-discriminator-next(e));
<singleton-discriminator> =>
let e :: <singleton-discriminator> = e;
let tab :: <simple-object-vector> = singleton-discriminator-table(e);
for (i from 1 below size(tab) by 2)
engine-node-subster(new-e, old-e, vector-element(tab, i))
end for;
engine-node-subster(new-e, old-e, singleton-discriminator-default(e));
<discriminator> =>
error("Unhandled discriminator in substitution: %=", e);
otherwise => #f;
end select
end function;
//define function find-low-bit-index (m :: <integer>)
// local method loop (i :: <integer>, m :: <integer>)
// if (m == 0)
// error("select-next-arg-for-discrimination got lost?")
// elseif (logbit?(0, m))
// i
// else
// loop(i + 1, ash(m, -1))
// end if
// end method;
// loop(0, m)
//end function;
define function select-next-arg-for-discrimination (ds :: <dispatch-state>)
=> (arg);
let argnum-set :: <argnum-set> = %ds-argnum-set(ds);
local method loop ()
let a :: <list> = %ds-args-to-check-first(ds);
if (a == #())
let nrequired :: <integer> = %gf-number-required(%ds-gf(ds));
let next-unchecked-arg :: <integer> = next-free-argnum(-1, argnum-set);
if (next-unchecked-arg < nrequired)
next-unchecked-arg
else
#f
end if
else
let m :: <integer> = head(a);
local method moop (m :: <integer>, i :: <integer>)
if (m == 0)
let nxt :: <list> = %proper-list-tail(a);
%ds-args-to-check-first(ds) := nxt;
loop()
elseif (logbit?(0, m) & ~argnum-considered?(i, argnum-set))
head(a) := ash(m, i);
i
else
moop(ash(m, -1), i + 1)
end if
end method;
moop(m, 0)
end if
end method;
loop()
end function;
define function walk-existing-dispatch-engine (ds :: <dispatch-state>, e, recurse :: <function>)
=> (e :: <object>);
select (e by instance?)
<by-class-discriminator> =>
let e :: <by-class-discriminator> = e;
// This one is handled a little differently than all the other discriminators, because
// it doesn't always finish the discrimination step on the given arg position.
let argnum :: <integer> = discriminator-argnum(e);
dbg("walk-existing-dispatch-engine, arg %=: %=", argnum, e);
let thisarg = vector-element(%ds-args(ds), argnum);
let argclass :: <class> = object-class(thisarg);
let all-accounted-for? = prune-methods-by-known-class(argnum, argclass, ds);
let key :: <integer> = object-class-unique-key(thisarg);
let next-e = ckd-lookup(key, e);
let new-e = if (all-accounted-for? | ~instance?(next-e, <absent-engine-node>))
%ds-add-argnum(argnum, ds);
consider-arg-discriminated(ds, argnum, thisarg);
recurse(ds, next-e, recurse)
elseif (recurse ~== walk-existing-dispatch-engine)
error("Hey, you're not supposed to get here!")
else
compute-subdiscriminator-for-arg(ds, argnum, thisarg, argclass)
end if;
if (next-e ~== new-e)
dbg(" ... arg %=: new class key %=", argnum, key);
subst-engine-node(new-e, next-e, ds);
ckd-add!(e, key, new-e)
else e
end if;
<discriminator> => // Existing discrimination code.
let e :: <discriminator> = e;
let argnum :: <integer> = discriminator-argnum(e);
dbg("walk-existing-dispatch-engine, arg %=: %=", argnum, e);
let thisarg = vector-element(%ds-args(ds), argnum);
consider-arg-discriminated(ds, argnum, thisarg);
select (e by instance?)
<class-keyed-discriminator> =>
let e :: <class-keyed-discriminator> = e;
let key = if (instance?(e, <by-class-discriminator>))
object-class-unique-key(thisarg)
else
class-unique-key(thisarg)
end if;
let next-e = ckd-lookup(key, e);
let new-e = recurse(ds, next-e, recurse);
if (next-e ~== new-e)
dbg(" ... arg %=: new class key %=", argnum, key);
subst-engine-node(new-e, next-e, ds);
ckd-add!(e, key, new-e)
else e
end if;
<if-type-discriminator> =>
let e :: <if-type-discriminator> = e;
let testp = primitive-instance?(thisarg, if-type-discriminator-type(e));
let next-e = if (testp) if-type-discriminator-then(e) else if-type-discriminator-else(e) end;
let new-e = recurse(ds, next-e, recurse);
unless (next-e == new-e)
subst-engine-node(new-e, next-e, ds);
if (testp)
dbg(" ... arg %=: new `then'", argnum);
if-type-discriminator-then(e) := new-e
else
dbg(" ... arg %=: new `else'", argnum);
if-type-discriminator-else(e) := new-e
end if
end unless;
e;
<typecheck-discriminator> =>
let e :: <typecheck-discriminator> = e;
if (primitive-instance?(thisarg, typecheck-discriminator-type(e)))
let next-e = typecheck-discriminator-next(e);
let new-e = recurse(ds, next-e, recurse);
unless (next-e == new-e)
dbg(" ... arg %=: typecheck discriminator extended.", argnum);
subst-engine-node(new-e, next-e, ds);
typecheck-discriminator-next(e) := new-e
end unless;
e
else
error("Method list incorrectly pruned?")
end if;
<singleton-discriminator> =>
let e :: <singleton-discriminator> = e;
let key = vector-element(%ds-args(ds), argnum);
let lookup = singleton-discriminator-element(e, key, #f);
let next-e = lookup | singleton-discriminator-default(e);
let new-e = recurse(ds, next-e, recurse);
if (next-e ~== new-e)
subst-engine-node(new-e, next-e, ds);
if (lookup)
dbg(" ... arg %=: new entry for singleton key %=", argnum, key);
singleton-discriminator-element(e, key) := new-e
else
dbg(" ... arg %=: new entry for singleton table default %=", argnum);
singleton-discriminator-default(e) := new-e
end if
end if;
e;
end select;
<cache-header-engine-node> =>
let e :: <cache-header-engine-node> = e;
let nxt = cache-header-engine-node-next(e);
let new = recurse(ds, nxt, recurse);
if (nxt ~== new)
subst-engine-node(new, nxt, ds);
install-cache-header-engine-node-next(new, e, %ds-gf(ds));
end if;
e;
<method>, <terminal-engine-node> =>
// We are here either because the engine-node is $absent-engine-node -- and we need
// to fill it in -- or because of a race condition and it's already been filled in --
// or because something is broken and we are looping or infinitely recursing.
// Note that once we are in here, there is no more race condition because the g.f.
// is locked.
if (recurse ~== walk-existing-dispatch-engine)
error("Hey, you're not supposed to get here!")
elseif (e == $absent-engine-node)
compute-more-dispatch-engine(ds)
else
// If there was no multithreading, we shouldn't get here. If there is,
// it's possible for another thread to have filled this in ahead of us, so
// we should just quietly give in.
e
end if;
end select
end function;
define function compute-more-dispatch-engine
(ds :: <dispatch-state>)
=> (engine :: <object> /* union(<method>, <engine-node>) */);
let argnum? = select-next-arg-for-discrimination(ds);
if (~ argnum?)
compute-terminal-engine-node(ds)
else
let argnum :: <integer> = argnum?;
compute-discriminator-for-arg(argnum, ds)
end if
end function;
// This is the interesting part, where we figure out how we must do
// discrimination on the next argument.
define function compute-subdiscriminator-for-arg
(ds :: <dispatch-state>, argnum :: <integer>,
thisarg, thisargclass :: <class>)
dbg("compute-subdiscriminator-for-arg %=", argnum);
local method ponder-this-arg (methods :: <list>, subclass-p, singletons :: <list>, others :: <list>)
if (methods == #())
consider-arg-discriminated(ds, argnum, thisarg);
let nextd = compute-more-dispatch-engine(ds);
let gf :: <generic-function> = %ds-gf(ds);
if (subclass-p | (singletons ~== #() & subtype?(thisargclass, <class>)))
ckd-add!(make-by-singleton-class-discriminator(argnum, gf, 4, $absent-engine-node),
class-unique-key(thisarg), nextd)
elseif (singletons ~== #())
let d = make-single-class-singleton-discriminator(singletons, argnum, gf);
local method poo (l :: <list>)
if (l == #())
dbg("... arg %=: singletons (default)", argnum);
singleton-discriminator-default(d)
:= compute-default-subdiscriminator(argnum, gf, thisarg, others, nextd)
elseif (thisarg == head(l))
dbg("... arg %=: singletons, key = %=", argnum, thisarg);
singleton-discriminator-element(d, thisarg) := nextd
else
poo(tail(l))
end if
end method;
poo(singletons);
d
else
compute-default-subdiscriminator(argnum, gf, thisarg, others, nextd)
end if
else
let meth :: <method> = head(methods);
let spec :: <type> = %method-specializer(meth, argnum);
let (subclass-p, singletons :: <list>, others :: <list>)
= ponder-a-specializer(spec, thisargclass, subclass-p, singletons, others);
ponder-this-arg(tail(methods), subclass-p, singletons, others)
end if
end method;
let methlist :: <list> = %proper-list-tail(%ds-headed-methods(ds));
ponder-this-arg(methlist, #f, #(), #())
end function;
define function ponder-a-specializer
(spec :: <type>, thisargclass :: <class>, subclass-p, singletons :: <list>, others :: <list>)
=> (subclass-p, singletons :: <list>, others :: <list>);
select (spec by instance?)
<class> =>
values(subclass-p, singletons, others);
<singleton> =>
let spec :: <singleton> = spec;
let obj = spec.singleton-object;
let singletons :: <list>
= if (object-class(obj) == thisargclass) pair(obj, singletons) else singletons end;
values(subclass-p, singletons, others);
<subclass> =>
// values(subclass-p | grounded-subtype?(thisargclass, <class>), singletons, others);
// @@@@@@
// this would get triggered with limited collection types -- jb 31MAR99
// debug-assert(grounded-subtype?(thisargclass, <class>),
// "ponder-a-specializer: %= was expected to be a subtype of <class>", thisargclass);
values(#t, singletons, others);
<union> =>
let spec :: <union> = spec;
let (subclass-p, singletons :: <list>, others :: <list>)
= ponder-a-specializer(union-type1(spec), thisargclass, subclass-p, singletons, others);
ponder-a-specializer(union-type2(spec), thisargclass, subclass-p, singletons, others);
<limited-type> =>
values(subclass-p, singletons,
if (grounded-has-instances?(thisargclass, spec)) pair(spec, others) else others end);
end select
end function;
define function compute-default-subdiscriminator
(argnum :: <integer>, gf :: <generic-function>, thisarg, specs :: <list>, nextd)
local method subsumed?(t :: <type>, l :: <list>)
if (l == #())
#f
elseif (grounded-subtype?(t, head(l)))
#t
else
subsumed?(t, tail(l))
end if
end method;
local method foo (specs :: <list>, truelist :: <list>, falselist :: <list>, nextd)
if (specs == #())
nextd
else
let spec :: <type> = head(specs);
let nextspecs :: <list> = %proper-list-tail(specs);
let (thend, elsed) = if (nextd == $absent-engine-node)
values(nextd, nextd)
elseif (primitive-instance?(thisarg, spec))
values(nextd, $absent-engine-node)
else
values($absent-engine-node, nextd)
end if;
if (subsumed?(spec, truelist))
foo(nextspecs, truelist, falselist, thend)
elseif (subsumed?(spec, falselist))
foo(nextspecs, truelist, falselist, elsed)
else
make-if-type-discriminator(argnum, gf, spec,
foo(nextspecs, pair(spec, truelist), falselist, thend),
foo(nextspecs, truelist, pair(spec, falselist), elsed))
end if
end if
end method;
foo(specs, #(), #(), nextd)
end function;
define function compute-discriminator-for-arg (argnum :: <integer>, ds :: <dispatch-state>)
=> (engine)
let thisarg = vector-element(%ds-args(ds), argnum);
let thisargclass = object-class(thisarg);
if (tail(%ds-headed-methods(ds)) == #())
// No specializers (the methods list was empty).
dispinapplicable(ds)
else
let (secondary-p :: <boolean>, force-blowup-p :: <boolean>,
all-subtypes-p :: <boolean>, some-subtypes-p :: <boolean>,
subtype-exception :: false-or(<type>))
= ponder-this-arg (ds, thisarg, thisargclass, argnum);
if (~force-blowup-p & all-subtypes-p)
// No discrimination needed: the known arg type is a subtype of all specializers.
consider-arg-discriminated(ds, argnum, thisarg);
compute-more-dispatch-engine(ds)
else
let gf :: <generic-function> = %ds-gf(ds);
if (force-blowup-p | ~subtype-exception)
let nextd
= if (secondary-p)
prune-methods-by-known-class(argnum, thisargclass, ds);
compute-subdiscriminator-for-arg(ds, argnum, thisarg, thisargclass)
else
consider-arg-discriminated(ds, argnum, thisarg);
compute-more-dispatch-engine(ds)
end if;
ckd-add!(make-by-class-discriminator(argnum, gf, 1),
class-unique-key(thisargclass), nextd)
else
consider-arg-discriminated(ds, argnum, thisarg);
let nextd = compute-more-dispatch-engine(ds);
if (some-subtypes-p)
// We have to distinguish between one specializer and the rest.
let (thend, elsed) = if (primitive-instance?(thisarg, subtype-exception))
values(nextd, $absent-engine-node)
else
values($absent-engine-node, nextd)
end if;
make-if-type-discriminator(argnum, gf, subtype-exception, thend, elsed)
else
// There is only one specializer, but we don't have type info for it.
make-typecheck-discriminator(argnum, gf, subtype-exception, nextd)
end if
end if
end if
end if
end function;
define function secondary-dispatch-specializer? (spec :: <type>, thisargclass :: <class>)
=> (secondary-p :: <boolean>)
if (instance?(spec, <union>))
let spec :: <union> = spec;
secondary-dispatch-specializer?(spec.union-type1, thisargclass)
| secondary-dispatch-specializer?(spec.union-type2, thisargclass)
else
let (some?, all?) = grounded-has-instances?(thisargclass, spec);
some? & (~ all?)
end if
end function;
define function ponder-this-arg (ds :: <dispatch-state>, thisarg,
thisargclass :: <class>, argnum :: <integer>)
=> (secondary-p :: <boolean>, force-blowup-p :: <boolean>,
all-subtypes-p :: <boolean>, some-subtypes-p :: <boolean>,
subtype-exception :: false-or(<type>))
let methods :: <list> = %proper-list-tail(%ds-headed-methods(ds));
let gf :: <generic-function> = %ds-gf(ds);
let knownargtype :: <type> = %ds-argtype(ds, argnum);
local method loop (methods :: <list>, secondary-p :: <boolean>, force-blowup-p :: <boolean>,
all-subtypes-p :: <boolean>, some-subtypes-p :: <boolean>, subtype-exception)
// TODO: TYPIST CAN'T PROVE THIS AND THEN THIS BLOWS TAIL CALLS
// => (secondary-p :: <boolean>, force-blowup-p :: <boolean>,
// all-subtypes-p :: <boolean>, some-subtypes-p :: <boolean>,
// subtype-exception :: false-or(<type>));
if (methods == #())
values(secondary-p, force-blowup-p, all-subtypes-p, some-subtypes-p,
subtype-exception ~== #t & subtype-exception)
else
let meth :: <method> = head(methods);
let spec :: <type> = %method-specializer(meth, argnum);
let secondary-p :: <boolean>
= secondary-p | secondary-dispatch-specializer?(spec, thisargclass);
let force-blowup-p :: <boolean>
= force-blowup-p | slot-method-requiring-class-discrimination?(meth, argnum);
let methods :: <list> = %proper-list-tail(methods);
if (concrete-subtype?(knownargtype, spec, gf))
loop(methods, secondary-p, force-blowup-p, all-subtypes-p, #t, subtype-exception)
else
loop(methods, secondary-p, force-blowup-p, #f, some-subtypes-p,
if (subtype-exception)
if (subtype-exception == #t | ~same-specializer?(subtype-exception, spec))
#t
else
spec
end if
else
spec
end if)
end if
end if
end method;
loop(methods, #f, #f, #t, #f, #f)
end function;
define function determine-call-keywords (gf :: <generic-function>, methods :: <list>)
let sig :: <signature> = function-signature(gf);
if (signature-all-keys?(sig))
#t
elseif (signature-key?(sig))
local method outer (meths :: <list>, ans :: <list>, nans :: <integer>)
if (meths == #())
let v :: <simple-object-vector> = make(<simple-object-vector>, size: nans);
for (i :: <integer> from 0, x in ans) vector-element(v, i) := x end;
v
else
let m :: <method> = head(meths);
if (signature-all-keys?(function-signature(m)))
#t
else
let keys :: <simple-object-vector> = keyword-specifiers(m);
let n :: <integer> = size(keys);
local method inner (i :: <integer>, ans :: <list>, nans :: <integer>)
if (i == n)
outer(tail(meths), ans, nans)
else
let k = vector-element(keys, i);
if (member?(k, ans))
inner(i + 2, ans, nans)
else
inner(i + 2, pair(k, ans), nans + 1)
end if
end if
end method;
inner(0, ans, nans)
end if
end if
end method;
outer(methods, #(), 0)
else
#f
end if
end function;
define function compute-terminal-engine-node (ds :: <dispatch-state>)
=> (terminal-thing :: <object> /* union(<method>, <engine-node>) */);
let methlist :: <list> = %proper-list-tail(%ds-headed-methods(ds));
let keys = determine-call-keywords(%ds-gf(ds), methlist);
let (ordered :: <list>, ambig :: <list>) = sort-applicable-methods(methlist, %ds-args(ds));
dbg("Terminal engine node: ordered methods = %=, ambig = %=", ordered, ambig);
let ans = transmogrify-method-list-grounded
(ds, ordered, ambig, keys,
*gracefully-dispatch-to-ambiguous-methods*);
dbg("Terminal engine node = %=", ans);
let parent
= %ds-parent(ds);
let profiling-parent
= if (instance?(parent, <cache-header-engine-node>))
cache-header-engine-node-parent(parent)
else
parent
end if;
if (*profile-all-terminal-engine-nodes?*
| instance?(profiling-parent, <profiling-call-site-cache-header-engine-node>))
let new :: <profiling-call-site-cache-header-engine-node>
= bootstrap-typed-allocate-engine-node(<profiling-call-site-cache-header-engine-node>,
engine-node$k-profiling-cache-header,
0);
cache-header-engine-node-parent(new) := parent;
primitive-initialize-engine-node(new);
%profile-count-low(new) := as(<machine-word>, 0);
%profile-count-high(new) := as(<machine-word>, 0);
install-cache-header-engine-node-next(new, ans, %ds-gf(ds));
new
else
ans
end if
end function;
// Produce an engine-node for a method.
define function transmogrify-method-list-grounded
(ds :: <dispatch-state>, ordered :: <list>, ambig :: <list>, keyspec, kludge?)
=> (frob :: <object> /* union(<engine-node>, <method>) */);
let gf :: <generic-function> = %ds-gf(ds);
let args :: <simple-object-vector> = %ds-args(ds);
if (ordered == #())
if (ambig == #())
dbg("No applicable methods, punting...");
dispinapplicable(ds)
elseif (kludge?)
dbg("Trying to salvage ambiguous methods");
let (nordered :: <list>, nambig :: <list>)
= sort-applicable-methods-desperately(ambig, args);
unless (nordered == #())
let args = reconstruct-args-from-mepargs(gf, args);
dispwarn(make(<ambiguous-methods-warning>,
generic: gf, arguments: args, ambiguous: ambig,
format-string:
"Method specificity of unorderable methods %= applying %= to %= "
"was determined with arbitrary and capricious rules.",
format-arguments: vector(ambig, gf, args)),
ds)
end unless;
transmogrify-method-list-grounded(ds, nordered, nambig, keyspec, #f)
else
dbg("No orderable methods, making ambiguous-method engine node.");
make-ambiguous-methods-engine-node(ordered, ambig)
end if
else
let m :: <method> = head(ordered);
select (m by instance?)
<accessor-method> =>
let m :: <accessor-method> = m;
make-slot-access-engine-node(m, ds);
otherwise =>
let nextp = function-next?(m);
let more
= if (nextp)
let moremeths :: <list> = %proper-list-tail(ordered);
transmogrify-method-list-tail-grounded(ds, ordered, moremeths, ambig, kludge?)
else #()
end if;
if (~more)
$absent-engine-node
elseif (~nextp & more == #() & (keyspec == #t | keyspec == #f)
& primitive-callable-as-engine-node?(m))
m
else
make-single-method-engine-node(m, data: more, keys: keyspec)
end if
end select
end if
end function;
// Recursively generate the next-method chain.
define function transmogrify-method-list-tail-grounded
(ds :: <dispatch-state>, ordered :: <list>, subordered :: <list>,
ambig :: <list>, kludge? :: <boolean>)
=> (frob :: <list>);
if (subordered == #())
if (ambig == #())
#()
elseif (kludge?)
dbg("Trying to salvage ambiguous method list tail");
let args :: <simple-object-vector> = %ds-args(ds);
let (nordered :: <list>, nambig :: <list>)
= sort-applicable-methods-desperately(ambig, args);
unless (nordered == #())
let gf = %ds-gf(ds);
let args = reconstruct-args-from-mepargs(gf, args);
dispwarn(make(<ambiguous-methods-warning>,
generic: gf, arguments: args,
ordered: ordered, ambiguous: ambig,
format-string:
"Applying %= to %=, ambiguous method ordering of group %= after "
"successfully ordered methods %= "
"was determined with arbitrary and capricious rules.",
format-arguments: vector(gf, args, ambig, ordered)),
ds)
end unless;
transmogrify-method-list-tail-grounded(ds, ordered, nordered, nambig, #f)
else
make-ambiguous-methods-next-method(ordered, ambig, %ds-gf(ds))
end if
else
let m :: <method> = head(subordered);
let more
= if (function-next?(m))
let othermeths :: <list> = %proper-list-tail(subordered);
transmogrify-method-list-tail-grounded(ds, ordered, othermeths,
ambig, kludge?);
else
#()
end if;
more & select (m by instance?)
<accessor-method> =>
let m :: <accessor-method> = m;
make-slot-accessing-next-method-chain(ds, m);
<method> => pair(m, more);
end select
end if
end function;
define function %method-applicable?
(meth :: <method>, args :: <simple-object-vector>)
=> (answer :: <boolean>);
let n :: <integer> = size(args);
local method loop (i :: <integer>)
if (i == n)
#t
elseif (primitive-instance?(vector-element(args, i), %method-specializer(meth, i)))
loop(i + 1)
else
#f
end if
end method;
loop(0)
end function;
define inline-only function sort-applicable-methods
(methlist :: <list>, args :: <simple-object-vector>)
=> (ordered-unambiguous :: <list>, unordered-ambiguous :: <list>);
compute-sorted-applicable-methods-1(methlist, args, %order-methods);
end function;
define inline-only function sort-applicable-methods-desperately
(methlist :: <list>, args :: <simple-object-vector>)
=> (ordered-unambiguous :: <list>, unordered-ambiguous :: <list>);
compute-sorted-applicable-methods-1(methlist, args, %order-methods-desperately);
end function;
define function compute-sorted-applicable-methods
(gf :: <generic-function>, args :: <simple-object-vector>)
=> (ordered :: <list>, ambig :: <list>);
compute-sorted-applicable-methods-1(for (ans = #() then if (%method-applicable?(m, args)) pair(m, ans) else ans end,
m in generic-function-methods(gf))
finally ans
end for,
args, %order-methods)
end function;
define function compute-sorted-applicable-methods-1
(methlist :: <list>, args :: <simple-object-vector>, order-the-methods :: <method>)
=> (ordered :: <list>, ambig :: <list>);
let ohead :: <pair> = pair(#f, #());
let ahead :: <pair> = pair(#f, #());
local method loop (methlist :: <list>) => ()
unless (methlist == #())
let meth :: <method> = head(methlist);
local method make-ambiguous (headed-list :: <pair>) => ();
local method loop (l :: <list>)
unless (l == #())
let t1 :: <list> = %proper-list-tail(l);
tail(l) := tail(ahead);
tail(ahead) := l;
loop(t1)
end unless
end method;
loop(tail(headed-list));
tail(headed-list) := #();
tail(ahead) := pair(meth, tail(ahead))
end method;
local method precedes-all? (l :: <list>)
local method loop (l :: <list>)
if (l == #())
#t
elseif (order-the-methods(meth, head(l), args) ~== #"<")
#f
else
loop(tail(l))
end if
end method;
loop(l)
end method;
local method check-subsequent-ambiguities (oprev :: <pair>) => ();
if (~precedes-all?(tail(oprev)) | ~precedes-all?(tail(ahead)))
make-ambiguous(oprev)
else
tail(oprev) := pair(meth, tail(oprev))
end if;
end method;
local method insert (oprev :: <pair>, osub :: <list>) => ()
if (empty?(osub))
check-subsequent-ambiguities(oprev)
else
let indic = order-the-methods(meth, head(osub), args);
if (indic == #"<") // Comes before current one.
check-subsequent-ambiguities(oprev)
elseif (indic == #">") // Comes after, check further.
insert(osub, tail(osub))
else // Ambiguous. All following ordered methods are too.
unless (indic == #"=" & *gracefully-ignore-duplicate-methods*)
make-ambiguous(oprev)
end unless
end if
end if
end method;
insert(ohead, tail(ohead));
loop(tail(methlist))
end unless
end method;
loop(methlist);
values(tail(ohead), tail(ahead))
end function;
define variable *gracefully-dispatch-to-ambiguous-methods* = #t;
// See the code in compute-sorted-applicable-methods-1. What this does is cause
// a duplicated method to get tossed on the floor rather than considered
// ambiguous. Doing this in any kind of consistent way relies on consistent
// adding of methods to the g.f. - which is (mostly) done (latest addition
// comes first in the generic-function-methods) and on the preservation of this
// ordering through the discrimination pruning (which is also done).
define variable *gracefully-ignore-duplicate-methods* = #t;
define abstract class <ambiguous-methods> (<simple-condition>)
constant slot ambiguous-methods-generic :: <generic-function>, required-init-keyword: generic:;
constant slot ambiguous-methods-ordered :: <list>, init-keyword: ordered:, init-value: #();
constant slot ambiguous-methods-ambiguous :: <list>, required-init-keyword: ambiguous:;
constant slot ambiguous-methods-arguments :: <sequence>, required-init-keyword: arguments:;
end class;
define class <ambiguous-methods-warning> (<ambiguous-methods>, <warning>)
end class;
define class <ambiguous-methods-error> (<ambiguous-methods>, <error>)
end class;
define function %order-methods-desperately (m1 :: <method>, m2 :: <method>,
args :: <simple-object-vector>)
=> (v :: <symbol>);
let nreq :: <integer> = %method-number-required(m1);
local method loop (idx :: <integer>, ambigp)
if (idx == nreq)
if (ambigp) #"<>" else #"=" end
else
let s1 :: <type> = %method-specializer(m1, idx);
let s2 :: <type> = %method-specializer(m2, idx);
let arg = vector-element(args, idx);
let cmp0 :: <symbol> = %order-specializers(s1, s2, arg);
let cmp = if (cmp0 == #"<>") %order-specializers-desperately(s1, s2, arg) else cmp0 end;
if (cmp == #"<" | cmp == #">")
cmp
else
loop(idx + 1, ambigp | cmp == #"<>")
end if
end if
end method;
loop(0, #f)
end function;
define function %order-methods
(meth1 :: <method>, meth2 :: <method>, args :: <simple-object-vector>)
=> (order :: <symbol>);
let nreq :: <integer> = %method-number-required(meth1);
local method loop (state :: <symbol>, idx :: <integer>)
if (idx == nreq)
state
else
let meth1spec :: <type> = %method-specializer(meth1, idx);
let meth2spec :: <type> = %method-specializer(meth2, idx);
let cmp :: <symbol> = %order-specializers(meth1spec, meth2spec,
vector-element(args, idx));
let idx :: <integer> = idx + 1;
if (cmp == #"=")
loop(state, idx)
elseif (cmp ~== #"<>" & (state == #"=" | cmp == state))
loop(cmp, idx)
else
#"<>"
end if
end if
end method;
loop (#"=", 0)
end function;