Skip to content

Commit 0da2822

Browse files
committed
further improve clash resolving impl
1 parent 0546800 commit 0da2822

File tree

1 file changed

+22
-40
lines changed

1 file changed

+22
-40
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/Deforestation.scala

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -651,45 +651,30 @@ class Deforest(using TL, Raise, Elaborator.State):
651651
val ctorToDtor = ctorDests.ctorDests
652652
val dtorToCtor = dtorSources.dtorSources
653653

654-
def removeCtor(rm: Iterable[ResultId]): Unit =
655-
if rm.nonEmpty then
656-
tl.log("rm ctor: " + rm.map(c => c.getClsSymOfUid.nme).mkString(" | "))
657-
var toDeleteDtors: Ls[DtorExpr] = Nil
658-
for
659-
r <- rm
660-
CtorDest(mat, sels, _) <- ctorToDtor.remove(r)
661-
do
662-
mat.keys.foreach: s =>
663-
toDeleteDtors = DtorExpr.Match(s) :: toDeleteDtors
664-
sels.foreach: s =>
665-
toDeleteDtors = DtorExpr.Sel(s.expr) :: toDeleteDtors
666-
removeDtor(toDeleteDtors)
654+
def removeCtor(rm: ResultId): Unit =
655+
for CtorDest(mat, sels, _) <- ctorToDtor.remove(rm) do
656+
for s <- mat.keys do removeDtor(DtorExpr.Match(s))
657+
for s <- sels do removeDtor(DtorExpr.Sel(s.expr))
667658

668-
def removeDtor(rm: Iterable[DtorExpr]): Unit =
669-
if rm.nonEmpty then
670-
tl.log("rm dtor: " + rm.mkString(" | "))
671-
var toDeleteCtors: Ls[ResultId] = Nil
672-
for
673-
r <- rm
674-
c <- dtorToCtor.remove(r)
675-
x <- c.ctors
676-
do toDeleteCtors = x :: toDeleteCtors
677-
removeCtor(toDeleteCtors)
659+
def removeDtor(rm: DtorExpr) =
660+
for
661+
c <- dtorToCtor.remove(rm)
662+
x <- c.ctors
663+
do
664+
removeCtor(x)
678665

679666
// remove clashes:
680-
removeCtor(
681-
ctorToDtor.filterNot { case _ -> CtorDest(dtors, sels, noCons) =>
682-
((dtors.size == 0 && sels.size == 1)
683-
|| (dtors.size == 1 && {
684-
val scrutRef@Value.Ref(scrut) = dtors.head._1.getResult
685-
sels.forall { s => s.expr.getResult match
686-
case Select(Value.Ref(l), nme) => (l === scrut) && s.inMatching.contains(scrutRef.uid) // need to be in the matching arms, and checking the scrutinee
687-
case _ => false }
688-
}))
689-
&& !noCons
690-
}.keys
691-
)
692-
removeDtor(dtorToCtor.filter(_._2.noProd).keys)
667+
ctorToDtor.filterNot { case _ -> CtorDest(dtors, sels, noCons) =>
668+
((dtors.size == 0 && sels.size == 1)
669+
|| (dtors.size == 1 && {
670+
val scrutRef@Value.Ref(scrut) = dtors.head._1.getResult
671+
sels.forall { s => s.expr.getResult match
672+
case Select(Value.Ref(l), nme) => (l === scrut) && s.inMatching.contains(scrutRef.uid) // need to be in the matching arms, and checking the scrutinee
673+
case _ => false }
674+
}))
675+
&& !noCons
676+
}.keys.foreach(removeCtor)
677+
dtorToCtor.filter(_._2.noProd).keys.foreach(removeDtor)
693678

694679
// remove cycle:
695680
def getCtorInArm(ctor: ResultId, dtor: Match) =
@@ -718,14 +703,11 @@ class Deforest(using TL, Raise, Elaborator.State):
718703
go(newCtorsAndNewMatches)
719704
go(Ls(ctor -> dtor))
720705

721-
var toRmCtor: Ls[ResultId] = Nil
722706
for
723707
(c, CtorDest(matches, sels, _)) <- ctorToDtor
724708
m <- matches.values
725709
x <- findCycle(c, m)
726-
do toRmCtor = x :: toRmCtor
727-
728-
removeCtor(toRmCtor)
710+
do removeCtor(x)
729711

730712
ctorToDtor -> dtorToCtor
731713

0 commit comments

Comments
 (0)