@@ -595,14 +595,17 @@ algorithm
595595 local
596596 tuple< Integer ,DAE . ExpType ,Integer > tpl;
597597 list< list< DAE . Pattern >> patternMatrix;
598+ list< Option < list< DAE . Pattern >>> optPatternMatrix;
599+ Integer numNonEmptyColumns;
598600 String str;
599601 DAE . ExpType ty;
600602 case (Absyn . MATCHCONTINUE (),_,_) then DAE . MATCHCONTINUE ();
601603 case (_,cases,_)
602604 equation
603605 true = listLength(cases) > 2 ;
604606 patternMatrix = Util . transposeList(Util . listMap(cases,getCasePatterns));
605- tpl = findPatternToConvertToSwitch(patternMatrix,0 ,listLength(patternMatrix),info);
607+ (optPatternMatrix,numNonEmptyColumns) = removeWildPatternColumnsFromMatrix(patternMatrix,{},0 );
608+ tpl = findPatternToConvertToSwitch(optPatternMatrix,0 ,numNonEmptyColumns,info);
606609 (_,ty,_) = tpl;
607610 str = ExpressionDump . typeString(ty);
608611 Error . assertionOrAddSourceMessage(not RTOpts . debugFlag("patternmAllInfo" ),Error . MATCH_TO_SWITCH_OPTIMIZATION , {str}, info);
@@ -611,8 +614,31 @@ algorithm
611614 end matchcontinue;
612615end optimizeMatchToSwitch;
613616
614- protected function findPatternToConvertToSwitch
617+ protected function removeWildPatternColumnsFromMatrix
615618 input list< list< DAE . Pattern >> patternMatrix;
619+ input list< Option < list< DAE . Pattern >>> acc;
620+ input Integer numAcc;
621+ output list< Option < list< DAE . Pattern >>> optPatternMatrix;
622+ output Integer numNonEmptyColumns;
623+ algorithm
624+ (optPatternMatrix,numNonEmptyColumns) := matchcontinue (patternMatrix,acc,numAcc)
625+ local
626+ Boolean alwaysMatch;
627+ list< DAE . Pattern > pats;
628+ Option < list< DAE . Pattern >> optPats;
629+ case ({},acc,numAcc) then (listReverse(acc),numAcc);
630+ case (pats::patternMatrix,acc,numAcc)
631+ equation
632+ alwaysMatch = allPatternsAlwaysMatch(Util . listStripLast(pats));
633+ optPats = Util . if_(alwaysMatch,NONE (),SOME (pats));
634+ numAcc = Util . if_(alwaysMatch,numAcc,numAcc+ 1 );
635+ (acc,numAcc) = removeWildPatternColumnsFromMatrix(patternMatrix,optPats::acc,numAcc);
636+ then (acc,numAcc);
637+ end matchcontinue;
638+ end removeWildPatternColumnsFromMatrix;
639+
640+ protected function findPatternToConvertToSwitch
641+ input list< Option < list< DAE . Pattern >>> patternMatrix;
616642 input Integer index;
617643 input Integer numPatternsInMatrix "If there is only 1 pattern, we can optimize the default case" ;
618644 input Absyn . Info info;
@@ -624,7 +650,7 @@ algorithm
624650 String str;
625651 DAE . ExpType ty;
626652 Integer extraarg;
627- case (pats::patternMatrix,index,numPatternsInMatrix,info)
653+ case (SOME ( pats) ::patternMatrix,index,numPatternsInMatrix,info)
628654 equation
629655 (ty,extraarg) = findPatternToConvertToSwitch2(pats, {}, DAE . ET_OTHER (), numPatternsInMatrix);
630656 then ((index,ty,extraarg));
@@ -1768,6 +1794,22 @@ algorithm
17681794 end match;
17691795end allPatternsWild;
17701796
1797+ protected function allPatternsAlwaysMatch
1798+ "Returns true if all patterns in the list are wildcards or as-bindings"
1799+ input list< DAE . Pattern > pats;
1800+ output Boolean b;
1801+ algorithm
1802+ b := match pats
1803+ local
1804+ DAE . Pattern pat;
1805+ case {} then true ;
1806+ case DAE . PAT_WILD ()::pats then allPatternsAlwaysMatch(pats);
1807+ case DAE . PAT_AS (pat= pat)::pats then allPatternsAlwaysMatch(pat::pats);
1808+ case DAE . PAT_AS_FUNC_PTR (pat= pat)::pats then allPatternsAlwaysMatch(pat::pats);
1809+ else false ;
1810+ end match;
1811+ end allPatternsAlwaysMatch;
1812+
17711813protected function getCasePatterns
17721814"Accessor function for DAE.Case"
17731815 input DAE . MatchCase case_;
0 commit comments