-
Notifications
You must be signed in to change notification settings - Fork 1
/
pattern.cc
1573 lines (1486 loc) · 53.5 KB
/
pattern.cc
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
/* -*-Mode: c++;-*-
Copyright (c) 2003-2008 John Plevyak, All Rights Reserved
*/
#include "ifadefs.h"
#include "pattern.h"
#include "ast.h"
#include "builtin.h"
#include "fa.h"
#include "fun.h"
#include "if1.h"
#include "log.h"
#include "pdb.h"
#include "pnode.h"
#include "var.h"
// Key to names of position variables
// ABCD
// A = a | f, actual or formal (MANDITORY)
// B = c, cannonical (OPTIONAL)
// C = p, positional (OPTIONAL)
// D = p, position (MANDITORY)
// Examples:
// acpp - actual, cannonical, positional position
// acnp - actual, cannonical, named position
// ap - actual non-cannonical, positional or named position
int pattern_match_hits = 0, pattern_match_complete = 0, pattern_matches = 0;
class PMatch : public Match {
public:
Map<MPosition *, MPosition *> actual_to_formal_position;
Map<MPosition *, AVar *> actuals;
Map<MPosition *, Sym *> formal_dispatch_types;
Map<MPosition *, AType *> actual_filters; // actual -> type, point-wise and
// includes named arguments
Map<MPosition *, MPosition *> actual_named_to_positional;
Map<MPosition *, MPosition *> formal_to_actual_position;
Map<MPosition *, MPosition *> order_substitutions; // formal position -> actual position
Vec<MPosition *> default_args; // formal positions
Map<Sym *, Sym *> generic_substitutions;
Map<MPosition *, Sym *> coercion_substitutions; // formal position -> coercion symbol
Map<MPosition *, Sym *> promotion_substitutions; // formal position -> coercion symbol
Match *cache_copy();
PMatch(Fun *afun) : Match(afun) {}
};
#define forv_PMatch(_p, _v) forv_Vec(PMatch, _p, _v)
typedef Map<Fun *, PMatch *> PMatchMap;
typedef MapElem<Fun *, PMatch *> PMatchElem;
class MatchCacheEntry : public Vec<Match *> {
public:
int is_closure;
Partial_kind partial;
PNode *visibility_point;
MapMPositionAType all_args;
MatchCacheEntry(int ais_closure, Partial_kind apartial, PNode *avp)
: is_closure(ais_closure), partial(apartial), visibility_point(avp) {}
};
#define forv_MatchCacheEntry(_x, _v) forv_Vec(MatchCacheEntry, _x, _v)
class MatchCache : public Vec<MatchCacheEntry *> {};
class Matcher {
public:
AVar *send;
AVar *arg0;
IFAAST *ast;
int is_closure;
int is_error;
Partial_kind partial;
PMatchMap match_map;
Vec<Fun *> function_values; // functions passed in directly and varargs functions
Map<MPosition *, int> mapped_positions;
MapMPositionAType all_args;
// C++'s manditory redundant declarations, tedious++
int pattern_match_sym(Sym *, MPosition *, Vec<Fun *> *, Vec<Fun *> &, int);
void pattern_match_sym_internal(Sym *, MPosition *, Vec<Fun *> &, Vec<Sym *> &);
void pattern_match_arg(Sym *, MPosition *, Vec<Fun *> &, Vec<Fun *> &, int);
void build_positional_map(MPosition &, int, Vec<Fun *> **);
void update_match_map(AVar *, CreationSet *, MPosition *, MPosition *, Vec<Fun *> &);
void find_arg_matches(AVar *, MPosition &, MPosition *, MPosition *, Vec<Fun *> **, int, int);
void find_all_matches(CreationSet *, Vec<AVar *> &, Vec<cchar *> &, Vec<Fun *> **, MPosition &, int);
void find_single_match(CreationSet *, Vec<AVar *> &, Vec<Fun *> **, MPosition &, int);
void find_best_cs_match(Vec<CreationSet *> &, MPosition &p, Vec<Fun *> &, Vec<Fun *> &, int);
void find_best_matches(Vec<AVar *> &, Vec<CreationSet *> &, Vec<Fun *> &, MPosition &, Vec<Fun *> &, int,
int iarg = 0);
int covers_formals(Fun *, Vec<CreationSet *> &, MPosition &, int);
PMatch *build_PMatch(Fun *, PMatch *);
void instantiation_wrappers_and_partial_application(Vec<Fun *> &);
Fun *build(PMatch *m, Vec<Fun *> &matches);
int subsumes(PMatch *x, PMatch *y, MPosition &app, Vec<CreationSet *> &csargs);
void generic_arguments(Vec<CreationSet *> &, MPosition &, Vec<Fun *> &, int);
void set_filters(Vec<CreationSet *> &, MPosition &, Vec<Fun *> &);
void reverify_filters(Vec<Fun *> &);
void cannonicalize_matches(Vec<Fun *> &, int, Partial_kind, Vec<Match *> &, PNode *);
Matcher(AVar *send, AVar *arg0, int is_closure, Partial_kind partial);
};
static void log_dispatch_cs_match(Matcher &matcher, Vec<CreationSet *> &csargs, MPosition &app,
Vec<Fun *> &local_matches);
static void log_dispatch_matches(Matcher &matcher, Vec<PMatch *> &matches);
static void log_dispatch_funs(Matcher &matcher, Vec<Fun *> &funs);
static ChainHash<MPosition *, MPositionHashFuns> cannonical_mposition;
static Map<Sym *, Map<MPosition *, Vec<Fun *> *> *> sym_match_cache;
Match *PMatch::cache_copy() {
Match *m = new Match(fun);
m->formal_filters.copy(formal_filters);
m->visibility_points.copy(visibility_points);
m->is_partial = is_partial;
return m;
}
void Match::merge(Match *m) {
// Merge Matches
assert(fun == m->fun);
forv_MPosition(p, fun->positional_arg_positions) {
AType *t1 = formal_filters.get(p), *t2 = m->formal_filters.get(p);
if (t1 && t2)
formal_filters.put(p, type_union(t1, t2));
else if (!t1 && t2)
formal_filters.put(p, t2);
}
visibility_points.set_union(m->visibility_points);
}
static Sym *dispatch_type(Sym *a) {
if (is_Sym_OUT(a)) return sym_unknown_type;
if (a->is_symbol) return a;
if (a->type && a->type->is_symbol) return a->type;
if (a->must_specialize && a->must_specialize->type_kind != Type_VARIABLE) return a->must_specialize;
return sym_any;
}
void MPosition::copy(MPosition &p) {
pos.copy(p.pos);
up = p.up;
next = p.next;
if (p.cp == CANNONICAL_MPOSITION)
cp = &p;
else
cp = p.cp;
}
MPosition::MPosition(MPosition &p) { copy(p); }
MPosition *cannonicalize_mposition(MPosition &p) {
if (p.cp) return p.cp;
MPosition *cp = cannonical_mposition.get(&p);
if (cp) {
p.cp = cp;
return cp;
}
cp = new MPosition;
cp->pos.copy(p.pos);
cannonical_mposition.put(cp);
cp->cp = CANNONICAL_MPOSITION;
if (cp->pos.n > 1) {
MPosition pp(*cp);
pp.pop();
cp->up = cannonicalize_mposition(pp);
}
return cp;
}
static MPosition *to_formal(MPosition *acp, PMatch *m) {
MPosition *fcp = m->actual_to_formal_position.get(acp);
MPosition *result = fcp ? fcp : acp;
return result;
}
static MPosition *to_actual(MPosition *fcp, PMatch *m) {
MPosition *acp = m->formal_to_actual_position.get(fcp);
MPosition *result = acp ? acp : fcp;
return result;
}
static MPosition *cannonicalize_to_formal(MPosition &ap, PMatch *m) {
return to_formal(cannonical_mposition.get(&ap), m);
}
// Given type, cp, partial_matches, return funs, using done
void Matcher::pattern_match_sym_internal(Sym *type, MPosition *fcp, Vec<Fun *> &funs, Vec<Sym *> &done) {
if (!done.set_add(type)) return;
if (type->match_type) {
Vec<Fun *> *fs = type->match_type->funs.get(fcp);
if (fs) funs.set_union(*fs);
}
forv_Sym(tt, type->dispatch_types) pattern_match_sym_internal(tt, fcp, funs, done);
}
void Matcher::pattern_match_arg(Sym *type, MPosition *cp, Vec<Fun *> &test, Vec<Fun *> &result, int facp) {
Vec<PMatch *> matches;
Vec<MPosition *> positions;
forv_Fun(f, test) if (f) {
if (facp) {
PMatch *m = match_map.get(f);
cp = facp ? to_formal(cp, m) : cp;
}
Sym *a = f->arg_syms.get(cp);
if (!a) {
if (f->is_varargs) result.set_add(f);
continue;
}
Sym *m_type = dispatch_type(a);
if (m_type->specializers.set_in(type)) result.set_add(f);
}
}
// Given type, cp, partial_matches, return funs, using done
int Matcher::pattern_match_sym(Sym *type, MPosition *acp, Vec<Fun *> *local_matches, Vec<Fun *> &funs,
int out_of_position) {
if (out_of_position) {
pattern_match_arg(type, acp, *local_matches, funs, 1);
return funs.n;
}
pattern_match_arg(type, acp, function_values, funs, 0);
Vec<Sym *> done;
Vec<Fun *> new_funs;
Map<MPosition *, Vec<Fun *> *> *pfm = sym_match_cache.get(type);
Vec<Fun *> *vfn = 0;
if (pfm) {
vfn = pfm->get(acp);
if (vfn) goto Ldone;
}
vfn = new Vec<Fun *>;
pattern_match_sym_internal(type, acp, *vfn, done);
if (!pfm) sym_match_cache.put(type, (pfm = new Map<MPosition *, Vec<Fun *> *>));
pfm->put(acp, vfn);
Ldone:
if (local_matches) {
if (local_matches->n > vfn->n) {
forv_Fun(f, *vfn) if (f) if (local_matches->set_in(f)) funs.set_add(f);
} else {
forv_Fun(f, *local_matches) if (f) if (vfn->set_in(f)) funs.set_add(f);
}
} else
funs.set_union(*vfn);
return funs.n;
}
Matcher::Matcher(AVar *asend, AVar *aarg0, int ais_closure, Partial_kind apartial) {
send = asend;
arg0 = aarg0;
is_closure = ais_closure;
is_error = 0;
partial = apartial;
ast = asend->var->def->code->ast;
}
void Matcher::update_match_map(AVar *a, CreationSet *cs, MPosition *acp, MPosition *acpp, Vec<Fun *> &new_matches) {
forv_Fun(f, new_matches) if (f) {
PMatch *m = match_map.get(f);
if (!m) {
m = new PMatch(f);
match_map.put(f, m);
}
AType *t = m->actual_filters.get(acp);
if (!t) {
t = new AType;
m->actual_filters.put(acp, t);
if (acp != acpp) m->actual_named_to_positional.put(acp, acpp);
}
t->set_add(cs);
m->actuals.put(acpp, a);
}
}
int positional_to_named(PNode *pn, CreationSet *cs, Vec<cchar *> &names, MPosition &pp, MPosition *np) {
int i = Position2int(pp.last()) - 1;
if (i < names.n && names[i]) {
np->copy(pp);
np->set_top(names[i]);
return 1;
}
return 0;
}
// This is an approximation. It should really take into account
// the specific CreationSet which contributed at each MPosition
static void add_all_args(MapMPositionAType &m, MPosition *cp, AType *t) {
AType *tt = m.get(cp);
if (!tt)
m.put(cp, t);
else
m.put(cp, type_union(tt, t));
}
void Matcher::find_arg_matches(AVar *a, MPosition &ap, MPosition *acp, MPosition *acpp, Vec<Fun *> **partial_matches,
int recurse, int out_of_position) {
a->arg_of_send.add(send);
Vec<Fun *> funs;
add_all_args(all_args, acpp, a->out);
forv_CreationSet(cs, a->out->sorted) {
Sym *sym = cs->sym;
if (a->var->sym->aspect) {
if (!a->var->sym->aspect->specializers.set_in(cs->sym) &&
(cs->sym != sym_nil_type || !sym_object->specializers.set_in(a->var->sym->aspect)))
continue;
sym = a->var->sym->aspect;
}
Vec<Fun *> new_funs;
if (!pattern_match_sym(sym, acp, *partial_matches, new_funs, out_of_position)) continue;
update_match_map(a, cs, acp, acpp, new_funs);
if (recurse && cs->vars.n) { // for recursive ap == app
Vec<Fun *> rec_funs, tfuns, *rec_funs_p = &rec_funs;
tfuns.move(new_funs);
forv_Fun(f, tfuns) if (f) {
Sym *a = f->arg_syms.get(to_formal(acpp, match_map.get(f)));
assert(a || f->is_varargs);
if (a && a->is_pattern)
rec_funs.set_add(f);
else
new_funs.set_add(f);
}
if (rec_funs.n) {
find_all_matches(cs, cs->vars, cs->sym->has_names, &rec_funs_p, ap, out_of_position);
funs.set_union(rec_funs);
}
}
funs.set_union(new_funs);
}
if (!*partial_matches) {
*partial_matches = new Vec<Fun *>(funs);
} else {
Vec<Fun *> saved_matches;
saved_matches.move(**partial_matches);
forv_Fun(f, saved_matches) if (f) {
if (funs.set_in(f))
(*partial_matches)->set_add(f);
else if (acpp->up && !f->arg_syms.get(to_formal(acpp->up, match_map.get(f)))->is_pattern)
(*partial_matches)->set_add(f);
}
(*partial_matches)->set_union(funs);
}
}
int compar_last_position(const void *ai, const void *aj) {
MPosition *i = *(MPosition **)ai;
MPosition *j = *(MPosition **)aj;
if (i->pos.n > j->pos.n)
return 1;
else if (i->pos.n < j->pos.n)
return -1;
int ii = Position2int(i->pos[i->pos.n - 1]);
int jj = Position2int(j->pos[j->pos.n - 1]);
return (ii > jj) ? 1 : ((ii < jj) ? -1 : 0);
}
void Matcher::build_positional_map(MPosition &app, int nactuals, Vec<Fun *> **funs) {
if (*funs) {
forv_Fun(f, **funs) if (f) {
PMatch *m = match_map.get(f);
Vec<MPosition *> fcpps_for_fcnps, acpps_for_acnps, unused_acpps, unused_fcpps;
// map and collect named argument positions (at this level)
for (int i = 0; i < m->actual_filters.n; i++) {
MPosition *acp = m->actual_filters[i].key;
if (acp && !acp->last_is_positional() && acp->prefix_to_last(app)) {
MPosition *acnp = acp;
MPosition fnp;
if (acp->up) fnp.copy(*to_formal(acp->up, m));
fnp.push(acp->last());
MPosition *fcnp = cannonicalize_mposition(fnp);
MPosition *acpp = m->actual_named_to_positional.get(acnp);
MPosition *fcpp = f->named_to_positional.get(fcnp);
acpps_for_acnps.set_add(acpp);
fcpps_for_fcnps.set_add(fcpp);
m->actual_to_formal_position.put(acpp, fcpp);
m->formal_to_actual_position.put(fcpp, acpp);
if (acpp != fcpp) mapped_positions.put(acpp, 1);
}
}
app.push(1);
// collect actual positions not used by named arguments (at this level)
for (int i = 0; i < nactuals; i++) {
MPosition *acpp = cannonicalize_mposition(app);
if (!acpps_for_acnps.set_in(acpp)) unused_acpps.add(acpp);
app.inc();
}
app.pop();
// sort unused positions
qsort(unused_acpps.v, unused_acpps.n, sizeof(void *), compar_last_position);
// build a maps for actual positionals <-> formal positionals
MPosition fpp(*cannonicalize_to_formal(app, m));
fpp.push(1);
for (int i = 0; i < unused_acpps.n; i++) {
MPosition *fcpp = cannonicalize_mposition(fpp);
while (fcpps_for_fcnps.set_in(fcpp)) {
fpp.inc();
fcpp = cannonicalize_mposition(fpp);
}
m->actual_to_formal_position.put(unused_acpps[i], fcpp);
m->formal_to_actual_position.put(fcpp, unused_acpps[i]);
if (unused_acpps[i] != fcpp) mapped_positions.put(unused_acpps[i], 1);
fpp.inc();
}
}
}
}
void Matcher::find_all_matches(CreationSet *cs, Vec<AVar *> &args, Vec<cchar *> &names, Vec<Fun *> **partial_matches,
MPosition &app, int out_of_position) {
// determine named arguments
int some_named = 0;
app.push(1);
forv_AVar(av, args) {
MPosition anp;
if (positional_to_named(send->var->def, cs, names, app, &anp)) {
MPosition *acpp = cannonicalize_mposition(app);
if (acpp) {
MPosition *acnp = cannonicalize_mposition(anp);
find_arg_matches(av, anp, acnp, acpp, partial_matches, 0, out_of_position);
some_named = 1;
}
}
app.inc();
}
// build positional map
app.pop();
if (some_named) build_positional_map(app, args.n, partial_matches);
// match all arguments
app.push(1);
forv_AVar(av, args) {
MPosition *acpp = cannonicalize_mposition(app);
if (acpp) {
int local_out_of_position = out_of_position;
if (!local_out_of_position) {
if (mapped_positions.get(acpp)) local_out_of_position = 1;
}
find_arg_matches(av, app, acpp, acpp, partial_matches, 1, local_out_of_position);
}
app.inc();
}
app.pop();
}
static int subsumes_arg(PMatch *x, PMatch *y, MPosition *acpp, CreationSet *cs, int *identical) {
MPosition *xp = to_formal(acpp, x), *yp = to_formal(acpp, y);
Sym *xtype = dispatch_type(x->fun->arg_syms.get(xp));
Sym *ytype = dispatch_type(y->fun->arg_syms.get(yp));
Sym *atype = cs->sym;
if (xtype != ytype) *identical = 0;
// promotion
int pri = 1 << 10;
if (!x->promotion_substitutions.get(xp) && y->promotion_substitutions.get(yp)) return -pri;
if (x->promotion_substitutions.get(xp) && !y->promotion_substitutions.get(yp)) return pri;
xtype = x->formal_dispatch_types.get(xp);
ytype = y->formal_dispatch_types.get(yp);
xtype = xtype->type; // ignore constants
ytype = ytype->type; // ignore constants
// sum by value
pri = pri / 2;
if (xtype != ytype) {
if (xtype->isa.set_in(atype)) {
if (!ytype->isa.set_in(atype)) return -pri;
} else if (ytype->isa.set_in(atype))
return pri;
}
atype = atype->type;
// sum by type
pri = pri / 2;
if (xtype != ytype) {
if (xtype->isa.set_in(atype)) {
if (!ytype->isa.set_in(atype)) return -pri;
} else if (ytype->isa.set_in(atype))
return pri;
}
// type hierarchy
pri = pri / 2;
if (xtype != ytype) {
if (xtype->specializers.set_in(ytype)) {
if (!ytype->specializers.set_in(xtype)) return pri;
} else if (ytype->specializers.set_in(xtype))
return -pri;
}
// coercion
pri = pri / 2;
if (xtype != ytype) {
int xx = !!x->coercion_substitutions.get(xp), yy = !!y->coercion_substitutions.get(yp);
if (!xx && yy) return -pri;
if (xx && !yy) return pri;
if (xx && yy) {
Sym *t = coerce_num(xtype, ytype);
if (t == ytype) return -pri;
if (t == xtype) return pri;
}
}
// instantiation
pri = pri / 2;
xtype = x->generic_substitutions.get(x->fun->arg_syms.get(xp)->must_specialize);
ytype = y->generic_substitutions.get(y->fun->arg_syms.get(yp)->must_specialize);
xtype = xtype ? xtype->type : 0; // ignore parameters
ytype = ytype ? ytype->type : 0; // ignore parameters
if (xtype && !ytype) return pri;
if (!xtype && ytype) return -pri;
// parameter
pri = pri / 2;
xtype = x->generic_substitutions.get(x->fun->arg_syms.get(xp)->must_specialize);
ytype = y->generic_substitutions.get(y->fun->arg_syms.get(yp)->must_specialize);
if ((xtype && xtype->is_constant) && (!ytype || !ytype->is_constant)) return -pri;
if ((!xtype || !xtype->is_constant) && (ytype && ytype->is_constant)) return pri;
// constant
pri = pri / 2;
xtype = x->formal_dispatch_types.get(xp);
ytype = y->formal_dispatch_types.get(yp);
if (xtype != ytype) {
if (xtype->specializers.set_in(ytype)) {
if (!ytype->specializers.set_in(xtype)) return pri;
} else if (ytype->specializers.set_in(xtype))
return -pri;
}
return 0;
}
#define ABS(_x) (((_x) < 0) ? -(_x) : (_x))
int Matcher::subsumes(PMatch *x, PMatch *y, MPosition &app, Vec<CreationSet *> &csargs) {
int result = 0, identical = 1;
app.push(1);
for (int i = 0; i < csargs.n; i++) {
MPosition *acpp = cannonicalize_mposition(app);
int r = subsumes_arg(x, y, acpp, csargs[i], &identical);
if (ABS(r) > ABS(result))
result = r;
else if (ABS(r) < ABS(result)) {
} else if (r && r != result) {
result = 0; // ambiguous
goto Lreturn;
}
app.inc();
}
if (identical && !result) {
if (x->fun->sym->nesting_depth > y->fun->sym->nesting_depth) result = -1;
if (x->fun->sym->nesting_depth < y->fun->sym->nesting_depth) result = 1;
}
Lreturn:
app.pop();
log(LOG_DISPATCH, "%d- %d subsumes? %d: %d\n", send->var->sym->id, x->fun->sym->id, y->fun->sym->id, result);
result = result < 0 ? -1 : ((result > 0) ? 1 : 0);
return result;
}
void Matcher::set_filters(Vec<CreationSet *> &csargs, MPosition &app, Vec<Fun *> &matches) {
forv_Fun(f, matches) {
PMatch *m = match_map.get(f);
app.push(1);
for (int i = 0; i < csargs.n; i++) {
MPosition *fcpp = cannonicalize_to_formal(app, m);
AType *t = m->formal_filters.get(fcpp);
if (!t) {
t = new AType;
m->formal_filters.put(fcpp, t);
}
t->set_add(csargs[i]);
app.inc();
}
app.pop();
}
}
PMatch *Matcher::build_PMatch(Fun *f, PMatch *m) {
PMatch *mm = match_map.get(f);
if (!mm) {
mm = new PMatch(f);
match_map.put(f, mm);
}
assert(m != mm);
// NOTE: ensure handling is correct destructuring
mm->actuals.copy(m->actuals);
mm->formal_dispatch_types.copy(m->formal_dispatch_types);
mm->actual_filters.clear(); // not needed
form_MPositionAType(x, m->formal_filters) {
if (AType *t = mm->formal_filters.get(x->key))
t->set_union(*x->value);
else
mm->formal_filters.put(x->key, x->value);
}
mm->actual_named_to_positional.clear(); // not needed
mm->actual_to_formal_position.copy(m->actual_to_formal_position);
mm->formal_to_actual_position.copy(m->formal_to_actual_position);
mm->order_substitutions.copy(m->order_substitutions);
mm->default_args.copy(m->default_args);
mm->generic_substitutions.copy(m->generic_substitutions);
mm->coercion_substitutions.copy(m->coercion_substitutions);
mm->promotion_substitutions.copy(m->promotion_substitutions);
mm->is_partial = m->is_partial;
return mm;
}
template <class C>
class MapCacheHashFns : public gc {
public:
static unsigned int hash(C a) {
unsigned int h = 0, x = 0;
for (int i = 0; i < a->n; i++)
if (a->v[i].key) {
h += open_hash_primes[x++ % 256] * (uintptr_t)a->v[i].key;
h += open_hash_primes[x++ % 256] * (uintptr_t)a->v[i].value;
}
return h;
}
static int equal(C a, C b) {
for (int i = 0; i < a->n; i++)
if (a->v[i].key && b->get(a->v[i].key) != a->v[i].value) return 0;
for (int i = 0; i < b->n; i++)
if (b->v[i].key && a->get(b->v[i].key) != b->v[i].value) return 0;
return 1;
}
};
typedef Map<MPosition *, Sym *> CoercionMap;
typedef CoercionMap PromotionMap;
typedef Map<Sym *, Sym *> GenericMap;
typedef Map<MPosition *, MPosition *> OrderMap;
class CoercionCache : public gc {
public:
HashMap<CoercionMap *, MapCacheHashFns<CoercionMap *>, Fun *> cache;
};
class PromotionCache : public CoercionCache {};
class GenericCache : public gc {
public:
HashMap<GenericMap *, MapCacheHashFns<GenericMap *>, Fun *> cache;
};
class OrderCache : public gc {
public:
HashMap<OrderMap *, MapCacheHashFns<OrderMap *>, Fun *> cache;
};
class DefaultCacheHashFns {
public:
static unsigned int hash(Vec<MPosition *> *a) {
unsigned int h = 0, x = 0;
forv_MPosition(p, *a) h += open_hash_primes[x++ % 256] * (uintptr_t)a;
return h;
}
static int equal(Vec<MPosition *> *a, Vec<MPosition *> *b) {
if (a->n != b->n) return 0;
for (int i = 0; i < a->n; i++)
if (a->v[i] != b->v[i]) return 0;
return 1;
}
};
class DefaultCache : public gc {
public:
HashMap<Vec<MPosition *> *, DefaultCacheHashFns, Fun *> cache;
};
static void fixup_maps(PMatch *m, Map<MPosition *, MPosition *> &formal_to_formal) {
Map<MPosition *, MPosition *> a2f, f2a;
a2f.move(m->actual_to_formal_position);
f2a.move(m->formal_to_actual_position);
form_MPositionMPosition(x, a2f) {
if (MPosition *y = formal_to_formal.get(x->value)) m->actual_to_formal_position.put(x->key, y);
}
form_MPositionMPosition(x, f2a) {
if (MPosition *y = formal_to_formal.get(x->key)) m->formal_to_actual_position.put(y, x->value);
}
Map<MPosition *, AType *> formal_filters;
formal_filters.move(m->formal_filters);
form_MPositionAType(x, formal_filters) {
if (MPosition *y = formal_to_formal.get(x->key)) m->formal_filters.put(y, x->value);
}
}
static void fixup_maps_for_defaults(PMatch *m, Fun *oldf, Vec<MPosition *> &defaults) {
Vec<MPosition *> defaults_set;
defaults_set.set_union(defaults);
Map<MPosition *, MPosition *> formal_to_formal;
forv_MPosition(p, oldf->positional_arg_positions) {
// if that formal position (or any above) nolonger exists, skip it
MPosition tmp(*p), newp;
for (int i = p->pos.n; i <= 0; i--) {
tmp.pos.n = i;
tmp.cp = 0; // not cannonical
if (defaults_set.set_in(cannonicalize_mposition(tmp))) goto Lskip;
}
// at each level, count those before which nolonger exist and
// subtract from newp
for (int i = 0; i < p->pos.n; i++) {
int offset = 0;
tmp.copy(*p);
tmp.pos.n = i + 1;
for (int j = 0; j < Position2int(p->pos[i]); j++) {
tmp.pos[i] = int2Position(j + 1);
tmp.cp = 0; // not cannonical
if (defaults_set.set_in(cannonicalize_mposition(tmp))) offset++;
}
newp.push(int2Position(Position2int(p->pos[i]) - offset));
}
formal_to_formal.put(p, cannonicalize_mposition(newp));
Lskip:;
}
fixup_maps(m, formal_to_formal);
}
Fun *Matcher::build(PMatch *m, Vec<Fun *> &done_matches) {
Fun *f = m->fun, *fnew;
if (m->generic_substitutions.n) {
GenericCache *c = f->generic_cache;
if (!c) c = f->generic_cache = new GenericCache;
if (!(fnew = c->cache.get(&m->generic_substitutions))) {
f = if1->callback->instantiate_generic(f, m->generic_substitutions);
if (f) c->cache.put(new GenericMap(m->generic_substitutions), f);
} else
f = fnew;
if (!f || done_matches.set_in(f)) return 0;
done_matches.set_add(f);
m->generic_substitutions.clear();
m = build_PMatch(f, m);
}
if (m->default_args.n) {
qsort(m->default_args.v, m->default_args.n, sizeof(void *), compar_last_position);
DefaultCache *c = f->default_cache;
Fun *oldf = f;
if (!c) c = f->default_cache = new DefaultCache;
if (!(fnew = c->cache.get(&m->default_args))) {
f = if1->callback->default_wrapper(f, m->default_args);
if (f) c->cache.put(new Vec<MPosition *>(m->default_args), f);
} else
f = fnew;
if (!f || done_matches.set_in(f)) return 0;
done_matches.set_add(f);
Vec<MPosition *> defaults;
defaults.move(m->default_args);
m = build_PMatch(f, m);
fixup_maps_for_defaults(m, oldf, defaults);
}
if (m->coercion_substitutions.n) {
CoercionCache *c = f->coercion_cache;
if (!c) c = f->coercion_cache = new CoercionCache;
if (!(fnew = c->cache.get(&m->coercion_substitutions))) {
f = if1->callback->coercion_wrapper(f, m->coercion_substitutions);
if (f) c->cache.put(new CoercionMap(m->coercion_substitutions), f);
} else
f = fnew;
if (!f || done_matches.set_in(f)) return 0;
done_matches.set_add(f);
m->coercion_substitutions.clear();
m = build_PMatch(f, m);
}
if (m->promotion_substitutions.n) {
PromotionCache *c = f->promotion_cache;
if (!c) c = f->promotion_cache = new PromotionCache;
if (!(fnew = c->cache.get(&m->promotion_substitutions))) {
f = if1->callback->promotion_wrapper(f, m->promotion_substitutions);
if (f) c->cache.put(new PromotionMap(m->promotion_substitutions), f);
} else
f = fnew;
if (!f || done_matches.set_in(f)) return 0;
done_matches.set_add(f);
m->promotion_substitutions.clear();
m = build_PMatch(f, m);
}
int order_change = 0;
forv_MPosition(p, f->positional_arg_positions) {
MPosition *acp = to_actual(p, m);
if (p != acp) order_change = 1;
m->order_substitutions.put(p, acp);
}
if (order_change) {
OrderCache *c = f->order_cache;
if (!c) c = f->order_cache = new OrderCache;
if (!(fnew = c->cache.get(&m->order_substitutions))) {
f = if1->callback->order_wrapper(f, m->order_substitutions);
if (f) c->cache.put(new OrderMap(m->order_substitutions), f);
} else
f = fnew;
if (!f || done_matches.set_in(f)) return 0;
done_matches.set_add(f);
Map<MPosition *, MPosition *> order_substitutions;
order_substitutions.move(m->order_substitutions);
m = build_PMatch(f, m);
fixup_maps(m, order_substitutions);
} else
m->order_substitutions.clear();
return f;
}
void Matcher::instantiation_wrappers_and_partial_application(Vec<Fun *> &matches) {
Vec<Fun *> new_matches, complete, done_matches;
done_matches.set_union(matches);
forv_Fun(f, matches) {
PMatch *m = match_map.get(f);
if (!m->is_partial) f = build(m, done_matches);
if (!f) continue;
if (!m->is_partial) complete.set_add(f);
if (f->is_generic && !m->is_partial)
fail("instantiation failure for function '%s'", f->sym->name ? f->sym->name : "<unknown>");
new_matches.set_add(f);
}
complete.set_to_vec();
new_matches.set_to_vec();
if (complete.n > 1) {
type_violation(ATypeViolation_DISPATCH_AMBIGUITY, arg0, arg0->out, send, new Vec<Fun *>(complete));
is_error = 1;
}
matches.move(new_matches);
}
int Matcher::covers_formals(Fun *f, Vec<CreationSet *> &csargs, MPosition &p, int top_level) {
if (f->is_varargs) return 1;
int result = 1;
PMatch *m = match_map.get(f);
Vec<MPosition *> formals;
p.push(1);
for (int i = 0; i < csargs.n; i++) {
MPosition *cp = cannonicalize_to_formal(p, m);
formals.set_add(cp);
p.inc();
}
p.pop();
for (int i = 0; i < f->arg_positions.n; i++) {
MPosition *pp = f->arg_positions[i];
if (pp->prefix_to_last(p) && pp->last_is_positional()) {
if (formals.set_in(pp)) continue;
if (f->default_args.get(pp)) {
m->default_args.add(pp);
continue;
}
if (top_level && partial != Partial_NEVER)
m->is_partial = 1;
else
result = 0;
}
}
if (top_level && result) {
// handle x.y(z) as ((#y, x), z) differently for paren vs. paren-less
// methods
if (partial == Partial_OK && !f->is_eager) m->is_partial = 1;
// ensure that lazy Partial_OKs are returned as closures
if (partial == Partial_NEVER && f->is_lazy && !is_closure) result = 0;
// ensure that eager functions are not called from a closure
if (f->is_eager && is_closure) result = 0;
}
return result;
}
static int unify_instantiated(Sym *generic_type, Sym *concrete_type, Map<Sym *, Sym *> &substitutions) {
Sym *sym = concrete_type;
while (sym != generic_type) {
substitutions.map_union(sym->substitutions);
sym = sym->instantiates;
if (!sym) return 0;
}
return 1;
}
static int make_generic_substitution(IFAAST *ast, Sym *generic_type, Sym *concrete_type,
Map<Sym *, Sym *> &substitutions) {
Map<Sym *, Sym *> subs;
if (unify_instantiated(generic_type, concrete_type, subs)) {
for (int i = 0; i < subs.n; i++)
if (!make_generic_substitution(ast, subs[i].key, subs.v[i].value, substitutions)) return 0;
}
Sym *old = substitutions.get(generic_type);
if (old && old != concrete_type) {
// conflicting generic variable substitutions
return 0;
}
substitutions.put(generic_type, concrete_type);
return 1;
}
static int unify_generic_type(Sym *formal, Sym *generic_type, Sym *actual, Sym *concrete_value,
Map<Sym *, Sym *> &substitutions, IFAAST *ast) {
Sym *concrete_type = concrete_value->type;
if (concrete_type == sym_nil_type) {
Sym *gtype = substitutions.get(generic_type);
if (!gtype) gtype = generic_type;
if (gtype != sym_nil_type && gtype != sym_any->meta_type) return 0;
}
if (formal->is_generic) {
if (generic_type == actual) return make_generic_substitution(ast, formal, actual, substitutions);
if (concrete_type->is_meta_type) {
if (!generic_type->is_meta_type) return 0;
Sym *generic = 0;
int bind_to_value = 0;
if (!if1->callback->formal_to_generic(formal, &generic, &bind_to_value)) return 1;
if (generic_type->specializers.set_in(concrete_type))
return make_generic_substitution(ast, generic, concrete_value->meta_type, substitutions);
return 0;
}
if (generic_type->specializers.set_in(concrete_type)) {
Sym *generic = 0;
int bind_to_value = 0;
if (!if1->callback->formal_to_generic(formal, &generic, &bind_to_value)) return 1;
return make_generic_substitution(ast, generic, bind_to_value ? concrete_value : concrete_type, substitutions);
}
if (generic_type->specializers.set_in(actual)) return make_generic_substitution(ast, formal, actual, substitutions);
return 0;
} else {
Sym *type = substitutions.get(generic_type);
if (!type) type = substitutions.get(generic_type->meta_type);
if (type == concrete_type) return 1;
if (concrete_type->instantiates == generic_type)
if (make_generic_substitution(ast, generic_type, concrete_type, substitutions)) return 1;
return 0;
}
if (generic_type == concrete_type) return 1;
return 0;
}
static Sym *get_generic_type(Sym *formal) {
Sym *formal_type = formal->must_specialize;
if (formal->is_generic) {
if (formal_type) return formal_type;
return formal->type;
}
if (formal_type && formal_type->type_kind == Type_VARIABLE) return formal_type;
if (formal_type && formal_type->is_generic) return formal_type;
if (formal->type && formal->type->type_kind == Type_VARIABLE) return formal->type;
return 0;
}
static void instantiate_formal_types(PMatch *m) {
form_MPositionSym(x, m->formal_dispatch_types) {
Sym *formal = m->fun->arg_syms.get(x->key);
Sym *type = get_generic_type(formal);
if (type) {
Sym *generic = formal->is_generic ? formal : type;
Sym *instantiated = if1->callback->instantiate(generic, m->generic_substitutions);
if (instantiated) x->value = instantiated;
}
}
}
static int generic_substitutions(PMatch **am, MPosition &app, Vec<CreationSet *> &args, IFAAST *ast) {
MPosition local_app(app);
PMatch *m = *am;
if (!m->fun->is_generic) return 1;
local_app.push(1);
for (int i = 0; i < args.n; i++) {
MPosition *acpp = cannonicalize_mposition(local_app);
MPosition *fcpp = to_formal(acpp, m);
AVar *actual = m->actuals.get(acpp);
CreationSet *cs = args[i];
Sym *concrete_value = actual->var->sym->aspect ? actual->var->sym->aspect : cs->sym;
Sym *formal = m->fun->arg_syms.get(fcpp);
if (!formal && m->fun->is_varargs) goto Lnext;
{
Sym *generic_type = get_generic_type(formal);
if (generic_type) {
if (!unify_generic_type(formal, generic_type, actual->var->sym, concrete_value, m->generic_substitutions, ast))
return 0;
instantiate_formal_types(m);
}
}
Lnext:;
local_app.inc();
}
if (m->fun->is_generic && !m->generic_substitutions.n && !m->is_partial)
fail("unification failure for generic function '%s'", m->fun->sym->name ? m->fun->sym->name : "<unknown>");
return 1;
}
static void coercion_uses(PMatch **am, MPosition &app, Vec<CreationSet *> &args) {
PMatch *m = *am;
app.push(1);
for (int i = 0; i < args.n; i++) {
MPosition *acpp = cannonicalize_mposition(app);
MPosition *fcpp = to_formal(acpp, m);
AVar *a = m->actuals.get(acpp);
CreationSet *cs = args[i];
Sym *concrete_type = a->var->sym->aspect ? a->var->sym->aspect : cs->sym;
concrete_type = concrete_type->type;
Sym *formal_type = m->formal_dispatch_types.get(fcpp);
if (formal_type == concrete_type || (!formal_type && m->fun->is_varargs)) goto LnextArg;
{
Sym *coerced_type = if1->callback->coerce(concrete_type, formal_type);
if (coerced_type && concrete_type != coerced_type) {
m->coercion_substitutions.put(fcpp, concrete_type);
m->formal_dispatch_types.put(fcpp, coerced_type);
}
}
LnextArg:;
app.inc();
}
app.pop();
}
static int verify_arg(Sym *actual, CreationSet *cs, PMatch *m, MPosition *fcpp) {
if (m->promotion_substitutions.get(fcpp) || m->coercion_substitutions.get(fcpp)) return 1;
Sym *formal_type = m->formal_dispatch_types.get(fcpp);
if (!formal_type) return 1;
Sym *actual_type = cs->sym;
if (cs->sym == sym_nil_type && actual->aspect && sym_object->specializers.set_in(actual->aspect))
actual_type = actual->aspect;
if (formal_type != actual_type && !formal_type->specializers.set_in(actual_type->type)) return 0;
return 1;
}
static int verify_args(PMatch *m, MPosition &app, Vec<CreationSet *> &args) {
app.push(1);