Skip to content

Commit 80c38cb

Browse files
authored
Merge pull request #16765 from xedin/more-space-engine-cleanups
[SpaceEngine] `intersect` cleanups
2 parents 3d3daec + 1101070 commit 80c38cb

File tree

1 file changed

+49
-75
lines changed

1 file changed

+49
-75
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,14 @@ namespace {
377377

378378
// (_ : Ty1) <= (_ : Ty2) iff D(Ty1) == D(Ty2)
379379
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());
383381
if (or1Space.isSubspace(other, TC, DC)) {
384382
return true;
385383
}
386384
}
387385

388386
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());
392388
return this->isSubspace(or2Space, TC, DC);
393389
}
394390

@@ -406,17 +402,13 @@ namespace {
406402
if (!canDecompose(this->getType(), DC)) {
407403
return false;
408404
}
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());
412406
return or1Space.isSubspace(other, TC, DC);
413407
}
414408
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
415409
// (_ : Ty1) <= H(p1 | ... | pn) iff D(Ty1) <= H(p1 | ... | pn)
416410
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());
420412
return or1Space.isSubspace(other, TC, DC);
421413
}
422414
// An undecomposable type is always larger than its constructor space.
@@ -501,6 +493,11 @@ namespace {
501493
}
502494
}
503495

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+
504501
// Returns the intersection of this space with another. The intersection
505502
// is the largest shared subspace occupied by both arguments.
506503
Space intersect(const Space &other, TypeChecker &TC,
@@ -519,11 +516,10 @@ namespace {
519516
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Disjunct): {
520517
// S & (S1 || ... || Sn) iff (S & S1) && ... && (S & Sn)
521518
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); });
527523
return Space::forDisjunct(intersectedCases);
528524
}
529525

@@ -533,38 +529,26 @@ namespace {
533529
PAIRCASE (SpaceKind::Disjunct, SpaceKind::BooleanConstant):
534530
PAIRCASE (SpaceKind::Disjunct, SpaceKind::UnknownCase): {
535531
// (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);
543533
}
544534
PAIRCASE (SpaceKind::Type, SpaceKind::Type): {
545535
// Optimization: The intersection of equal types is that type.
546536
if (this->getType()->isEqual(other.getType())) {
547537
return other;
548538
} 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);
553541
} 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);
558544
} else {
559545
return other;
560546
}
561547
}
562548
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
563549
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);
568552
} else {
569553
return other;
570554
}
@@ -582,10 +566,11 @@ namespace {
582566

583567
PAIRCASE (SpaceKind::Constructor, SpaceKind::UnknownCase): {
584568
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+
});
589574
return Space::forConstructor(this->getType(), this->getHead(),
590575
this->canDowngradeToWarning(),
591576
newSubSpaces);
@@ -609,21 +594,21 @@ namespace {
609594
auto j = other.getSpaces().begin();
610595
for (; i != this->getSpaces().end() && j != other.getSpaces().end();
611596
++i, ++j) {
612-
auto intersection = (*i).intersect(*j, TC, DC).simplify(TC, DC);
597+
auto result = intersect(*i, *j, TC, DC);
613598
// If at least one of the constructor sub-spaces is empty,
614599
// it makes the whole space empty as well.
615-
if (intersection.isEmpty()) {
600+
if (result.isEmpty()) {
616601
return Space();
617602
}
618-
paramSpace.push_back(intersection);
603+
paramSpace.push_back(result);
619604
}
620605

621606
return Space::forDisjunct(paramSpace);
622607
}
623608

624609
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Type):
625610
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Constructor):
626-
return other.intersect(*this, TC, DC);
611+
return intersect(other, *this, TC, DC);
627612
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::UnknownCase):
628613
if (other.isAllowedButNotRequired())
629614
return other;
@@ -638,10 +623,8 @@ namespace {
638623
}
639624

640625
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);
645628
}
646629
return Space();
647630
}
@@ -651,14 +634,7 @@ namespace {
651634
return Space();
652635

653636
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);
662638
}
663639

664640
PAIRCASE (SpaceKind::Empty, SpaceKind::BooleanConstant):
@@ -701,23 +677,18 @@ namespace {
701677
if (this->getType()->isEqual(other.getType())) {
702678
return Space();
703679
} 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);
707682
} 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);
712685
}
713686
return Space();
714687
}
715688
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
716689
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);
721692
} else {
722693
return *this;
723694
}
@@ -802,7 +773,7 @@ namespace {
802773
auto &s2 = *j;
803774
// If the intersection of each subspace is ever empty then the
804775
// 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()) {
806777
return *this;
807778
}
808779

@@ -862,10 +833,8 @@ namespace {
862833
}
863834

864835
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);
869838
}
870839
return *this;
871840
}
@@ -876,9 +845,7 @@ namespace {
876845

877846
PAIRCASE (SpaceKind::Type, SpaceKind::BooleanConstant): {
878847
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());
882849
return orSpace.minus(other, TC, DC, minusCount);
883850
} else {
884851
return *this;
@@ -1117,6 +1084,13 @@ namespace {
11171084
}
11181085
}
11191086

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+
11201094
static bool canDecompose(Type tp, const DeclContext *DC) {
11211095
return tp->is<TupleType>() || tp->isBool() ||
11221096
tp->getEnumOrBoundGenericEnum();

0 commit comments

Comments
 (0)