Skip to content

Commit 1101070

Browse files
committed
[SpaceEngine] NFC: Extract type decomposition as disjuct into function
It seems benefitial to have type decomposition into disjunct space as a separate function, because that code is regularly repeated in `intersect`, `isSubspace` and `minus` methods.
1 parent f950d04 commit 1101070

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 28 additions & 46 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.
@@ -544,24 +536,18 @@ namespace {
544536
if (this->getType()->isEqual(other.getType())) {
545537
return other;
546538
} else if (canDecompose(this->getType(), DC)) {
547-
SmallVector<Space, 4> spaces;
548-
decompose(TC, DC, this->getType(), spaces);
549-
auto decomposition = Space::forDisjunct(spaces);
539+
auto decomposition = decompose(TC, DC, this->getType());
550540
return intersect(decomposition, other, TC, DC);
551541
} else if (canDecompose(other.getType(), DC)) {
552-
SmallVector<Space, 4> spaces;
553-
decompose(TC, DC, other.getType(), spaces);
554-
auto disjunctSp = Space::forDisjunct(spaces);
555-
return intersect(*this, disjunctSp, TC, DC);
542+
auto decomposition = decompose(TC, DC, other.getType());
543+
return intersect(*this, decomposition, TC, DC);
556544
} else {
557545
return other;
558546
}
559547
}
560548
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
561549
if (canDecompose(this->getType(), DC)) {
562-
SmallVector<Space, 4> spaces;
563-
decompose(TC, DC, this->getType(), spaces);
564-
auto decomposition = Space::forDisjunct(spaces);
550+
auto decomposition = decompose(TC, DC, this->getType());
565551
return intersect(decomposition, other, TC, DC);
566552
} else {
567553
return other;
@@ -620,8 +606,8 @@ namespace {
620606
return Space::forDisjunct(paramSpace);
621607
}
622608

623-
PAIRCASE(SpaceKind::UnknownCase, SpaceKind::Type):
624-
PAIRCASE(SpaceKind::UnknownCase, SpaceKind::Constructor):
609+
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Type):
610+
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::Constructor):
625611
return intersect(other, *this, TC, DC);
626612
PAIRCASE (SpaceKind::UnknownCase, SpaceKind::UnknownCase):
627613
if (other.isAllowedButNotRequired())
@@ -637,10 +623,8 @@ namespace {
637623
}
638624

639625
if (canDecompose(other.getType(), DC)) {
640-
SmallVector<Space, 4> spaces;
641-
decompose(TC, DC, other.getType(), spaces);
642-
auto disjunctSp = Space::forDisjunct(spaces);
643-
return intersect(*this, disjunctSp, TC, DC);
626+
auto decomposition = decompose(TC, DC, other.getType());
627+
return intersect(*this, decomposition, TC, DC);
644628
}
645629
return Space();
646630
}
@@ -693,23 +677,18 @@ namespace {
693677
if (this->getType()->isEqual(other.getType())) {
694678
return Space();
695679
} else if (canDecompose(this->getType(), DC)) {
696-
SmallVector<Space, 4> spaces;
697-
this->decompose(TC, DC, this->getType(), spaces);
698-
return intersect(Space::forDisjunct(spaces), other, TC, DC);
680+
auto decomposition = decompose(TC, DC, this->getType());
681+
return intersect(decomposition, other, TC, DC);
699682
} else if (canDecompose(other.getType(), DC)) {
700-
SmallVector<Space, 4> spaces;
701-
this->decompose(TC, DC, other.getType(), spaces);
702-
auto decomp = Space::forDisjunct(spaces);
703-
return intersect(*this, decomp, TC, DC);
683+
auto decomposition = decompose(TC, DC, other.getType());
684+
return intersect(*this, decomposition, TC, DC);
704685
}
705686
return Space();
706687
}
707688
PAIRCASE (SpaceKind::Type, SpaceKind::Constructor): {
708689
if (canDecompose(this->getType(), DC)) {
709-
SmallVector<Space, 4> spaces;
710-
this->decompose(TC, DC, this->getType(), spaces);
711-
auto decomp = Space::forDisjunct(spaces);
712-
return decomp.minus(other, TC, DC, minusCount);
690+
auto decomposition = decompose(TC, DC, this->getType());
691+
return decomposition.minus(other, TC, DC, minusCount);
713692
} else {
714693
return *this;
715694
}
@@ -854,10 +833,8 @@ namespace {
854833
}
855834

856835
if (canDecompose(other.getType(), DC)) {
857-
SmallVector<Space, 4> spaces;
858-
this->decompose(TC, DC, other.getType(), spaces);
859-
auto disjunctSp = Space::forDisjunct(spaces);
860-
return this->minus(disjunctSp, TC, DC, minusCount);
836+
auto decomposition = decompose(TC, DC, other.getType());
837+
return this->minus(decomposition, TC, DC, minusCount);
861838
}
862839
return *this;
863840
}
@@ -868,9 +845,7 @@ namespace {
868845

869846
PAIRCASE (SpaceKind::Type, SpaceKind::BooleanConstant): {
870847
if (canDecompose(this->getType(), DC)) {
871-
SmallVector<Space, 4> spaces;
872-
this->decompose(TC, DC, this->getType(), spaces);
873-
auto orSpace = Space::forDisjunct(spaces);
848+
auto orSpace = decompose(TC, DC, this->getType());
874849
return orSpace.minus(other, TC, DC, minusCount);
875850
} else {
876851
return *this;
@@ -1109,6 +1084,13 @@ namespace {
11091084
}
11101085
}
11111086

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

0 commit comments

Comments
 (0)