@@ -501,6 +501,11 @@ namespace {
501
501
}
502
502
}
503
503
504
+ static Space intersect (const Space &a, const Space &b, TypeChecker &TC,
505
+ const DeclContext *DC) {
506
+ return a.intersect (b, TC, DC).simplify (TC, DC);
507
+ }
508
+
504
509
// Returns the intersection of this space with another. The intersection
505
510
// is the largest shared subspace occupied by both arguments.
506
511
Space intersect (const Space &other, TypeChecker &TC,
@@ -519,11 +524,10 @@ namespace {
519
524
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Disjunct): {
520
525
// S & (S1 || ... || Sn) iff (S & S1) && ... && (S & Sn)
521
526
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
- });
527
+ std::transform (
528
+ other.getSpaces ().begin (), other.getSpaces ().end (),
529
+ std::back_inserter (intersectedCases),
530
+ [&](const Space &s) { return intersect (*this , s, TC, DC); });
527
531
return Space::forDisjunct (intersectedCases);
528
532
}
529
533
@@ -533,13 +537,7 @@ namespace {
533
537
PAIRCASE (SpaceKind::Disjunct, SpaceKind::BooleanConstant):
534
538
PAIRCASE (SpaceKind::Disjunct, SpaceKind::UnknownCase): {
535
539
// (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);
540
+ return intersect (other, *this , TC, DC);
543
541
}
544
542
PAIRCASE (SpaceKind::Type, SpaceKind::Type): {
545
543
// Optimization: The intersection of equal types is that type.
@@ -549,12 +547,12 @@ namespace {
549
547
SmallVector<Space, 4 > spaces;
550
548
decompose (TC, DC, this ->getType (), spaces);
551
549
auto decomposition = Space::forDisjunct (spaces);
552
- return decomposition. intersect (other, TC, DC);
550
+ return intersect (decomposition, other, TC, DC);
553
551
} else if (canDecompose (other.getType (), DC)) {
554
552
SmallVector<Space, 4 > spaces;
555
553
decompose (TC, DC, other.getType (), spaces);
556
554
auto disjunctSp = Space::forDisjunct (spaces);
557
- return this -> intersect (disjunctSp, TC, DC);
555
+ return intersect (* this , disjunctSp, TC, DC);
558
556
} else {
559
557
return other;
560
558
}
@@ -564,7 +562,7 @@ namespace {
564
562
SmallVector<Space, 4 > spaces;
565
563
decompose (TC, DC, this ->getType (), spaces);
566
564
auto decomposition = Space::forDisjunct (spaces);
567
- return decomposition. intersect (other, TC, DC);
565
+ return intersect (decomposition, other, TC, DC);
568
566
} else {
569
567
return other;
570
568
}
@@ -582,10 +580,11 @@ namespace {
582
580
583
581
PAIRCASE (SpaceKind::Constructor, SpaceKind::UnknownCase): {
584
582
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
- }
583
+ std::transform (this ->getSpaces ().begin (), this ->getSpaces ().end (),
584
+ std::back_inserter (newSubSpaces),
585
+ [&](const Space &subSpace) {
586
+ return intersect (subSpace, other, TC, DC);
587
+ });
589
588
return Space::forConstructor (this ->getType (), this ->getHead (),
590
589
this ->canDowngradeToWarning (),
591
590
newSubSpaces);
@@ -609,21 +608,21 @@ namespace {
609
608
auto j = other.getSpaces ().begin ();
610
609
for (; i != this ->getSpaces ().end () && j != other.getSpaces ().end ();
611
610
++i, ++j) {
612
- auto intersection = (*i). intersect ( *j, TC, DC). simplify ( TC, DC);
611
+ auto result = intersect (*i, *j, TC, DC);
613
612
// If at least one of the constructor sub-spaces is empty,
614
613
// it makes the whole space empty as well.
615
- if (intersection .isEmpty ()) {
614
+ if (result .isEmpty ()) {
616
615
return Space ();
617
616
}
618
- paramSpace.push_back (intersection );
617
+ paramSpace.push_back (result );
619
618
}
620
619
621
620
return Space::forDisjunct (paramSpace);
622
621
}
623
622
624
- PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Type):
625
- PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Constructor):
626
- return other. intersect (*this , TC, DC);
623
+ PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Type):
624
+ PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Constructor):
625
+ return intersect (other, *this , TC, DC);
627
626
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::UnknownCase):
628
627
if (other.isAllowedButNotRequired ())
629
628
return other;
@@ -641,7 +640,7 @@ namespace {
641
640
SmallVector<Space, 4 > spaces;
642
641
decompose (TC, DC, other.getType (), spaces);
643
642
auto disjunctSp = Space::forDisjunct (spaces);
644
- return this -> intersect (disjunctSp, TC, DC);
643
+ return intersect (* this , disjunctSp, TC, DC);
645
644
}
646
645
return Space ();
647
646
}
@@ -651,14 +650,7 @@ namespace {
651
650
return Space ();
652
651
653
652
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
- }
653
+ return intersect (other, *this , TC, DC);
662
654
}
663
655
664
656
PAIRCASE (SpaceKind::Empty, SpaceKind::BooleanConstant):
@@ -703,12 +695,12 @@ namespace {
703
695
} else if (canDecompose (this ->getType (), DC)) {
704
696
SmallVector<Space, 4 > spaces;
705
697
this ->decompose (TC, DC, this ->getType (), spaces);
706
- return Space::forDisjunct (spaces). intersect ( other, TC, DC);
698
+ return intersect ( Space::forDisjunct (spaces), other, TC, DC);
707
699
} else if (canDecompose (other.getType (), DC)) {
708
700
SmallVector<Space, 4 > spaces;
709
701
this ->decompose (TC, DC, other.getType (), spaces);
710
702
auto decomp = Space::forDisjunct (spaces);
711
- return this -> intersect (decomp, TC, DC);
703
+ return intersect (* this , decomp, TC, DC);
712
704
}
713
705
return Space ();
714
706
}
@@ -802,7 +794,7 @@ namespace {
802
794
auto &s2 = *j;
803
795
// If the intersection of each subspace is ever empty then the
804
796
// two spaces are disjoint and their difference is the first space.
805
- if (s1. intersect (s2, TC, DC). simplify ( TC, DC).isEmpty ()) {
797
+ if (intersect (s1, s2, TC, DC).isEmpty ()) {
806
798
return *this ;
807
799
}
808
800
0 commit comments