@@ -377,18 +377,14 @@ namespace {
377
377
378
378
// (_ : Ty1) <= (_ : Ty2) iff D(Ty1) == D(Ty2)
379
379
if (canDecompose (this ->getType (), DC)) {
380
- SmallVector<Space, 4 > disjuncts;
381
- decompose (TC, DC, this ->getType (), disjuncts);
382
- Space or1Space = Space::forDisjunct (disjuncts);
380
+ Space or1Space = decompose (TC, DC, this ->getType ());
383
381
if (or1Space.isSubspace (other, TC, DC)) {
384
382
return true ;
385
383
}
386
384
}
387
385
388
386
if (canDecompose (other.getType (), DC)) {
389
- SmallVector<Space, 4 > disjuncts;
390
- decompose (TC, DC, other.getType (), disjuncts);
391
- Space or2Space = Space::forDisjunct (disjuncts);
387
+ Space or2Space = decompose (TC, DC, other.getType ());
392
388
return this ->isSubspace (or2Space, TC, DC);
393
389
}
394
390
@@ -406,17 +402,13 @@ namespace {
406
402
if (!canDecompose (this ->getType (), DC)) {
407
403
return false ;
408
404
}
409
- SmallVector<Space, 4 > disjuncts;
410
- decompose (TC, DC, this ->getType (), disjuncts);
411
- Space or1Space = Space::forDisjunct (disjuncts);
405
+ Space or1Space = decompose (TC, DC, this ->getType ());
412
406
return or1Space.isSubspace (other, TC, DC);
413
407
}
414
408
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
415
409
// (_ : Ty1) <= H(p1 | ... | pn) iff D(Ty1) <= H(p1 | ... | pn)
416
410
if (canDecompose (this ->getType (), DC)) {
417
- SmallVector<Space, 4 > disjuncts;
418
- decompose (TC, DC, this ->getType (), disjuncts);
419
- Space or1Space = Space::forDisjunct (disjuncts);
411
+ Space or1Space = decompose (TC, DC, this ->getType ());
420
412
return or1Space.isSubspace (other, TC, DC);
421
413
}
422
414
// An undecomposable type is always larger than its constructor space.
@@ -501,6 +493,11 @@ namespace {
501
493
}
502
494
}
503
495
496
+ static Space intersect (const Space &a, const Space &b, TypeChecker &TC,
497
+ const DeclContext *DC) {
498
+ return a.intersect (b, TC, DC).simplify (TC, DC);
499
+ }
500
+
504
501
// Returns the intersection of this space with another. The intersection
505
502
// is the largest shared subspace occupied by both arguments.
506
503
Space intersect (const Space &other, TypeChecker &TC,
@@ -519,11 +516,10 @@ namespace {
519
516
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Disjunct): {
520
517
// S & (S1 || ... || Sn) iff (S & S1) && ... && (S & Sn)
521
518
SmallVector<Space, 4 > intersectedCases;
522
- std::transform (other.getSpaces ().begin (), other.getSpaces ().end (),
523
- std::back_inserter (intersectedCases),
524
- [&](const Space &s) {
525
- return this ->intersect (s, TC, DC);
526
- });
519
+ std::transform (
520
+ other.getSpaces ().begin (), other.getSpaces ().end (),
521
+ std::back_inserter (intersectedCases),
522
+ [&](const Space &s) { return intersect (*this , s, TC, DC); });
527
523
return Space::forDisjunct (intersectedCases);
528
524
}
529
525
@@ -533,38 +529,26 @@ namespace {
533
529
PAIRCASE (SpaceKind::Disjunct, SpaceKind::BooleanConstant):
534
530
PAIRCASE (SpaceKind::Disjunct, SpaceKind::UnknownCase): {
535
531
// (S1 || ... || Sn) & S iff (S & S1) && ... && (S & Sn)
536
- SmallVector<Space, 4 > intersectedCases;
537
- std::transform (this ->getSpaces ().begin (), this ->getSpaces ().end (),
538
- std::back_inserter (intersectedCases),
539
- [&](const Space &s) {
540
- return s.intersect (other, TC, DC);
541
- });
542
- return Space::forDisjunct (intersectedCases);
532
+ return intersect (other, *this , TC, DC);
543
533
}
544
534
PAIRCASE (SpaceKind::Type, SpaceKind::Type): {
545
535
// Optimization: The intersection of equal types is that type.
546
536
if (this ->getType ()->isEqual (other.getType ())) {
547
537
return other;
548
538
} else if (canDecompose (this ->getType (), DC)) {
549
- SmallVector<Space, 4 > spaces;
550
- decompose (TC, DC, this ->getType (), spaces);
551
- auto decomposition = Space::forDisjunct (spaces);
552
- return decomposition.intersect (other, TC, DC);
539
+ auto decomposition = decompose (TC, DC, this ->getType ());
540
+ return intersect (decomposition, other, TC, DC);
553
541
} else if (canDecompose (other.getType (), DC)) {
554
- SmallVector<Space, 4 > spaces;
555
- decompose (TC, DC, other.getType (), spaces);
556
- auto disjunctSp = Space::forDisjunct (spaces);
557
- return this ->intersect (disjunctSp, TC, DC);
542
+ auto decomposition = decompose (TC, DC, other.getType ());
543
+ return intersect (*this , decomposition, TC, DC);
558
544
} else {
559
545
return other;
560
546
}
561
547
}
562
548
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
563
549
if (canDecompose (this ->getType (), DC)) {
564
- SmallVector<Space, 4 > spaces;
565
- decompose (TC, DC, this ->getType (), spaces);
566
- auto decomposition = Space::forDisjunct (spaces);
567
- return decomposition.intersect (other, TC, DC);
550
+ auto decomposition = decompose (TC, DC, this ->getType ());
551
+ return intersect (decomposition, other, TC, DC);
568
552
} else {
569
553
return other;
570
554
}
@@ -582,10 +566,11 @@ namespace {
582
566
583
567
PAIRCASE (SpaceKind::Constructor, SpaceKind::UnknownCase): {
584
568
SmallVector<Space, 4 > newSubSpaces;
585
- for (auto subSpace : this ->getSpaces ()) {
586
- Space nextSpace = subSpace.intersect (other, TC, DC);
587
- newSubSpaces.push_back (nextSpace.simplify (TC, DC));
588
- }
569
+ std::transform (this ->getSpaces ().begin (), this ->getSpaces ().end (),
570
+ std::back_inserter (newSubSpaces),
571
+ [&](const Space &subSpace) {
572
+ return intersect (subSpace, other, TC, DC);
573
+ });
589
574
return Space::forConstructor (this ->getType (), this ->getHead (),
590
575
this ->canDowngradeToWarning (),
591
576
newSubSpaces);
@@ -609,21 +594,21 @@ namespace {
609
594
auto j = other.getSpaces ().begin ();
610
595
for (; i != this ->getSpaces ().end () && j != other.getSpaces ().end ();
611
596
++i, ++j) {
612
- auto intersection = (*i). intersect ( *j, TC, DC). simplify ( TC, DC);
597
+ auto result = intersect (*i, *j, TC, DC);
613
598
// If at least one of the constructor sub-spaces is empty,
614
599
// it makes the whole space empty as well.
615
- if (intersection .isEmpty ()) {
600
+ if (result .isEmpty ()) {
616
601
return Space ();
617
602
}
618
- paramSpace.push_back (intersection );
603
+ paramSpace.push_back (result );
619
604
}
620
605
621
606
return Space::forDisjunct (paramSpace);
622
607
}
623
608
624
609
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Type):
625
610
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Constructor):
626
- return other. intersect (*this , TC, DC);
611
+ return intersect (other, *this , TC, DC);
627
612
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::UnknownCase):
628
613
if (other.isAllowedButNotRequired ())
629
614
return other;
@@ -638,10 +623,8 @@ namespace {
638
623
}
639
624
640
625
if (canDecompose (other.getType (), DC)) {
641
- SmallVector<Space, 4 > spaces;
642
- decompose (TC, DC, other.getType (), spaces);
643
- auto disjunctSp = Space::forDisjunct (spaces);
644
- return this ->intersect (disjunctSp, TC, DC);
626
+ auto decomposition = decompose (TC, DC, other.getType ());
627
+ return intersect (*this , decomposition, TC, DC);
645
628
}
646
629
return Space ();
647
630
}
@@ -651,14 +634,7 @@ namespace {
651
634
return Space ();
652
635
653
636
PAIRCASE (SpaceKind::Type, SpaceKind::BooleanConstant): {
654
- if (canDecompose (this ->getType (), DC)) {
655
- SmallVector<Space, 4 > spaces;
656
- decompose (TC, DC, this ->getType (), spaces);
657
- auto disjunctSp = Space::forDisjunct (spaces);
658
- return disjunctSp.intersect (other, TC, DC);
659
- } else {
660
- return Space ();
661
- }
637
+ return intersect (other, *this , TC, DC);
662
638
}
663
639
664
640
PAIRCASE (SpaceKind::Empty, SpaceKind::BooleanConstant):
@@ -701,23 +677,18 @@ namespace {
701
677
if (this ->getType ()->isEqual (other.getType ())) {
702
678
return Space ();
703
679
} else if (canDecompose (this ->getType (), DC)) {
704
- SmallVector<Space, 4 > spaces;
705
- this ->decompose (TC, DC, this ->getType (), spaces);
706
- return Space::forDisjunct (spaces).intersect (other, TC, DC);
680
+ auto decomposition = decompose (TC, DC, this ->getType ());
681
+ return intersect (decomposition, other, TC, DC);
707
682
} else if (canDecompose (other.getType (), DC)) {
708
- SmallVector<Space, 4 > spaces;
709
- this ->decompose (TC, DC, other.getType (), spaces);
710
- auto decomp = Space::forDisjunct (spaces);
711
- return this ->intersect (decomp, TC, DC);
683
+ auto decomposition = decompose (TC, DC, other.getType ());
684
+ return intersect (*this , decomposition, TC, DC);
712
685
}
713
686
return Space ();
714
687
}
715
688
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
716
689
if (canDecompose (this ->getType (), DC)) {
717
- SmallVector<Space, 4 > spaces;
718
- this ->decompose (TC, DC, this ->getType (), spaces);
719
- auto decomp = Space::forDisjunct (spaces);
720
- return decomp.minus (other, TC, DC, minusCount);
690
+ auto decomposition = decompose (TC, DC, this ->getType ());
691
+ return decomposition.minus (other, TC, DC, minusCount);
721
692
} else {
722
693
return *this ;
723
694
}
@@ -802,7 +773,7 @@ namespace {
802
773
auto &s2 = *j;
803
774
// If the intersection of each subspace is ever empty then the
804
775
// two spaces are disjoint and their difference is the first space.
805
- if (s1. intersect (s2, TC, DC). simplify ( TC, DC).isEmpty ()) {
776
+ if (intersect (s1, s2, TC, DC).isEmpty ()) {
806
777
return *this ;
807
778
}
808
779
@@ -862,10 +833,8 @@ namespace {
862
833
}
863
834
864
835
if (canDecompose (other.getType (), DC)) {
865
- SmallVector<Space, 4 > spaces;
866
- this ->decompose (TC, DC, other.getType (), spaces);
867
- auto disjunctSp = Space::forDisjunct (spaces);
868
- return this ->minus (disjunctSp, TC, DC, minusCount);
836
+ auto decomposition = decompose (TC, DC, other.getType ());
837
+ return this ->minus (decomposition, TC, DC, minusCount);
869
838
}
870
839
return *this ;
871
840
}
@@ -876,9 +845,7 @@ namespace {
876
845
877
846
PAIRCASE (SpaceKind::Type, SpaceKind::BooleanConstant): {
878
847
if (canDecompose (this ->getType (), DC)) {
879
- SmallVector<Space, 4 > spaces;
880
- this ->decompose (TC, DC, this ->getType (), spaces);
881
- auto orSpace = Space::forDisjunct (spaces);
848
+ auto orSpace = decompose (TC, DC, this ->getType ());
882
849
return orSpace.minus (other, TC, DC, minusCount);
883
850
} else {
884
851
return *this ;
@@ -1117,6 +1084,13 @@ namespace {
1117
1084
}
1118
1085
}
1119
1086
1087
+ static Space decompose (TypeChecker &TC, const DeclContext *DC,
1088
+ Type type) {
1089
+ SmallVector<Space, 4 > spaces;
1090
+ decompose (TC, DC, type, spaces);
1091
+ return Space::forDisjunct (spaces);
1092
+ }
1093
+
1120
1094
static bool canDecompose (Type tp, const DeclContext *DC) {
1121
1095
return tp->is <TupleType>() || tp->isBool () ||
1122
1096
tp->getEnumOrBoundGenericEnum ();
0 commit comments