Skip to content

Commit 13b595d

Browse files
hkielOpenModelica-Hudson
authored andcommitted
avoid failure(_=BaseHashTable.get()), use hasKey() instead
Belonging to [master]: - OpenModelica/OMCompiler#2306
1 parent 43f36d5 commit 13b595d

File tree

7 files changed

+76
-105
lines changed

7 files changed

+76
-105
lines changed

Compiler/BackEnd/BackendVarTransform.mo

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ algorithm
279279
list<DAE.Ident> iv;
280280
Option<HashTable2.HashTable> derConst;
281281
case ((REPLACEMENTS(hashTable=ht)),src,_) /* source dest */
282-
equation
283-
_ = BaseHashTable.get(src,ht) "if rule a->b exists, fail";
282+
guard
283+
BaseHashTable.hasKey(src,ht) "if rule a->b exists, fail"
284284
then
285285
fail();
286286
case ((REPLACEMENTS(ht,invHt,eht,iv,derConst)),src,dst)
@@ -371,26 +371,18 @@ protected function addReplacementInv2 "
371371
input DAE.ComponentRef dst;
372372
input DAE.ComponentRef src;
373373
output HashTable3.HashTable outInvHt;
374+
protected
375+
list<DAE.ComponentRef> srcs;
374376
algorithm
375-
outInvHt:=
376-
matchcontinue (invHt,dst,src)
377-
local
378-
HashTable3.HashTable invHt_1;
379-
list<DAE.ComponentRef> srcs;
380-
case (_,_,_)
381-
equation
382-
failure(_ = BaseHashTable.get(dst,invHt)) "No previous elt for dst -> src";
383-
invHt_1 = BaseHashTable.add((dst, {src}),invHt);
384-
then
385-
invHt_1;
386-
case (_,_,_)
387-
equation
388-
srcs = BaseHashTable.get(dst,invHt) "previous elt for dst -> src, append..";
389-
srcs = src::srcs;
390-
invHt_1 = BaseHashTable.add((dst, srcs),invHt);
391-
then
392-
invHt_1;
393-
end matchcontinue;
377+
if BaseHashTable.hasKey(dst,invHt) then
378+
// previous elt for dst -> src, append..
379+
srcs := BaseHashTable.get(dst,invHt);
380+
srcs := src::srcs;
381+
outInvHt := BaseHashTable.add((dst, srcs),invHt);
382+
else
383+
// No previous elt for dst -> src
384+
outInvHt:= BaseHashTable.add((dst, {src}),invHt);
385+
end if;
394386
end addReplacementInv2;
395387

396388
protected function makeTransitive "
@@ -559,22 +551,22 @@ algorithm
559551
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()),NONE())
560552
equation
561553
precr = ComponentReference.makeCrefIdent(ident,ty,{});
562-
failure(_ = BaseHashTable.get(precr,extendrepl));
554+
false = BaseHashTable.hasKey(precr,extendrepl);
563555
// update Replacements
564556
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
565557
then erepl;
566558
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_ARRAY()),SOME(pcr))
567559
equation
568560
precr = ComponentReference.makeCrefIdent(ident,ty,{});
569561
precr1 = ComponentReference.joinCrefs(pcr,precr);
570-
failure(_ = BaseHashTable.get(precr1,extendrepl));
562+
false = BaseHashTable.hasKey(precr1,extendrepl);
571563
// update Replacements
572564
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
573565
then erepl;
574566
case (_,DAE.CREF_IDENT(ident=ident,identType=ty as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_),varLst=varLst)),NONE())
575567
equation
576568
precr = ComponentReference.makeCrefIdent(ident,ty,{});
577-
failure(_ = BaseHashTable.get(precr,extendrepl));
569+
false = BaseHashTable.hasKey(precr,extendrepl);
578570
// update Replacements
579571
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
580572
// Create a list of crefs from names
@@ -585,7 +577,7 @@ algorithm
585577
equation
586578
_ = ComponentReference.makeCrefIdent(ident,ty,{});
587579
precr1 = ComponentReference.joinCrefs(pcr,cr);
588-
failure(_ = BaseHashTable.get(precr1,extendrepl));
580+
false = BaseHashTable.hasKey(precr1,extendrepl);
589581
// update Replacements
590582
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
591583
// Create a list of crefs from names
@@ -595,15 +587,15 @@ algorithm
595587
case (_,DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_),NONE())
596588
equation
597589
precr = ComponentReference.makeCrefIdent(ident,ty,{});
598-
failure(_ = BaseHashTable.get(precr,extendrepl));
590+
false = BaseHashTable.hasKey(precr,extendrepl);
599591
// update Replacements
600592
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
601593
then erepl;
602594
case (_,DAE.CREF_IDENT(ident=ident,identType=ty,subscriptLst=_::_),SOME(pcr))
603595
equation
604596
precr = ComponentReference.makeCrefIdent(ident,ty,{});
605597
precr1 = ComponentReference.joinCrefs(pcr,precr);
606-
failure(_ = BaseHashTable.get(precr1,extendrepl));
598+
false = BaseHashTable.hasKey(precr1,extendrepl);
607599
// update Replacements
608600
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
609601
then erepl;
@@ -613,7 +605,7 @@ algorithm
613605
case (_,DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=subcr),NONE())
614606
equation
615607
precr = ComponentReference.makeCrefIdent(ident,ty,{});
616-
failure(_ = BaseHashTable.get(precr,extendrepl));
608+
false = BaseHashTable.hasKey(precr,extendrepl);
617609
// update Replacements
618610
erepl = BaseHashTable.add((precr, DAE.RCONST(0.0)),extendrepl);
619611
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
@@ -623,7 +615,7 @@ algorithm
623615
equation
624616
precr = ComponentReference.makeCrefIdent(ident,ty,{});
625617
precr1 = ComponentReference.joinCrefs(pcr,precr);
626-
failure(_ = BaseHashTable.get(precr1,extendrepl));
618+
false = BaseHashTable.hasKey(precr1,extendrepl);
627619
// update Replacements
628620
erepl = BaseHashTable.add((precr1, DAE.RCONST(0.0)),extendrepl);
629621
precrn = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
@@ -793,18 +785,17 @@ public function hasReplacement "
793785
output Boolean bOut;
794786
algorithm
795787
bOut:=
796-
matchcontinue (inVariableReplacements,inComponentRef)
788+
match (inVariableReplacements,inComponentRef)
797789
local
798790
DAE.ComponentRef src;
799791
DAE.Exp dst;
800792
HashTable2.HashTable ht;
801793
case (REPLACEMENTS(hashTable=ht),src)
802-
equation
803-
_ = BaseHashTable.get(src,ht);
794+
guard BaseHashTable.hasKey(src,ht)
804795
then
805796
true;
806797
else false;
807-
end matchcontinue;
798+
end match;
808799
end hasReplacement;
809800

810801
public function hasReplacementCrefFirst "
@@ -815,18 +806,17 @@ public function hasReplacementCrefFirst "
815806
output Boolean bOut;
816807
algorithm
817808
bOut:=
818-
matchcontinue (inComponentRef,inVariableReplacements)
809+
match (inComponentRef,inVariableReplacements)
819810
local
820811
DAE.ComponentRef src;
821812
DAE.Exp dst;
822813
HashTable2.HashTable ht;
823814
case (src,REPLACEMENTS(hashTable=ht))
824-
equation
825-
_ = BaseHashTable.get(src,ht);
815+
guard BaseHashTable.hasKey(src,ht)
826816
then
827817
true;
828818
else false;
829-
end matchcontinue;
819+
end match;
830820
end hasReplacementCrefFirst;
831821

832822
public function hasNoReplacementCrefFirst "
@@ -837,18 +827,17 @@ public function hasNoReplacementCrefFirst "
837827
output Boolean bOut;
838828
algorithm
839829
bOut:=
840-
matchcontinue (inComponentRef,inVariableReplacements)
830+
match (inComponentRef,inVariableReplacements)
841831
local
842832
DAE.ComponentRef src;
843833
DAE.Exp dst;
844834
HashTable2.HashTable ht;
845835
case (src,REPLACEMENTS(hashTable=ht))
846-
equation
847-
_ = BaseHashTable.get(src,ht);
836+
guard BaseHashTable.hasKey(src,ht)
848837
then
849838
false;
850839
else true;
851-
end matchcontinue;
840+
end match;
852841
end hasNoReplacementCrefFirst;
853842

854843
public function varHasNoReplacement "
@@ -859,18 +848,17 @@ public function varHasNoReplacement "
859848
output Boolean bOut;
860849
algorithm
861850
bOut:=
862-
matchcontinue (var,inVariableReplacements)
851+
match (var,inVariableReplacements)
863852
local
864853
DAE.ComponentRef src;
865854
DAE.Exp dst;
866855
HashTable2.HashTable ht;
867856
case (BackendDAE.VAR(varName=src),REPLACEMENTS(hashTable=ht))
868-
equation
869-
_ = BaseHashTable.get(src,ht);
857+
guard BaseHashTable.hasKey(src,ht)
870858
then
871859
false;
872860
else true;
873-
end matchcontinue;
861+
end match;
874862
end varHasNoReplacement;
875863

876864
public function getReplacementVarArraySize

Compiler/BackEnd/Uncertainties.mo

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ protected function findArraysPartiallyIndexed2 "
17581758
output HashTable.HashTable outHt;
17591759

17601760
algorithm
1761-
outHt := matchcontinue(inRef,indubRef,inht)
1761+
outHt := match(inRef,indubRef,inht)
17621762
local
17631763
DAE.ComponentRef c1,c2;
17641764
DAE.Exp e1;
@@ -1768,35 +1768,26 @@ algorithm
17681768
case({}, _, ht) then ht;
17691769

17701770
case(((DAE.CREF(c1,_))::expl1),dubRef,ht)
1771-
equation
1772-
c2 = ComponentReference.crefStripLastSubs(c1);
1773-
failure(_ = BaseHashTable.get(c2,dubRef));
1774-
dubRef = BaseHashTable.add((c2,1),dubRef);
1775-
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
1776-
then ht;
1777-
1778-
case(((DAE.CREF(c1,_))::expl1),dubRef,ht)
1779-
equation
1780-
c2 = ComponentReference.crefStripLastSubs(c1);
1781-
_ = BaseHashTable.get(c2,dubRef);
1782-
_ = BaseHashTable.get(c2,ht);// if we have one occurrence, most likely it will be more.
1783-
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
1771+
algorithm
1772+
c2 := ComponentReference.crefStripLastSubs(c1);
1773+
if BaseHashTable.hasKey(c2,dubRef) then
1774+
if BaseHashTable.hasKey(c2,ht) then
1775+
// if we have one occurrence, most likely it will be more.
1776+
else
1777+
ht := BaseHashTable.add((c2,1),ht);
1778+
end if;
1779+
else
1780+
dubRef := BaseHashTable.add((c2,1),dubRef);
1781+
end if;
1782+
ht := findArraysPartiallyIndexed2(expl1,dubRef,ht);
17841783
then ht;
17851784

1786-
case(((DAE.CREF(c1,_))::expl1),dubRef,ht)
1787-
equation
1788-
c2 = ComponentReference.crefStripLastSubs(c1);
1789-
_ = BaseHashTable.get(c2,dubRef);
1790-
failure(_ = BaseHashTable.get(c2,ht));
1791-
ht = BaseHashTable.add((c2,1),ht);
1792-
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
1793-
then ht;
17941785
case(_::expl1,dubRef,ht)
17951786
equation
17961787
ht = findArraysPartiallyIndexed2(expl1,dubRef,ht);
17971788
then
17981789
ht;
1799-
end matchcontinue;
1790+
end match;
18001791
end findArraysPartiallyIndexed2;
18011792

18021793

@@ -2228,26 +2219,27 @@ protected function moveVariables2 "
22282219
output list<BackendDAE.Var> outVarLst2;
22292220
algorithm
22302221
(outVarLst1,outVarLst2):=
2231-
matchcontinue (inVarLst1,inVarLst2,hashTable)
2222+
match (inVarLst1,inVarLst2,hashTable)
22322223
local
22332224
list<BackendDAE.Var> globalKnownVars,vs_1,knvars_1,vs;
22342225
BackendDAE.Var v;
22352226
DAE.ComponentRef cr;
22362227
HashTable.HashTable mvars;
22372228
case ({},globalKnownVars,_) then ({},globalKnownVars);
22382229
case (((v as BackendDAE.VAR(varName = cr)) :: vs),globalKnownVars,mvars)
2239-
equation
2240-
_ = BaseHashTable.get(cr,mvars) "alg var moved to known vars";
2241-
(vs_1,knvars_1) = moveVariables2(vs, globalKnownVars, mvars);
2242-
then
2243-
(vs_1,(v :: knvars_1));
2244-
case (((v as BackendDAE.VAR(varName = cr)) :: vs),globalKnownVars,mvars)
2245-
equation
2246-
failure(_ = BaseHashTable.get(cr,mvars)) "alg var not moved to known vars";
2247-
(vs_1,knvars_1) = moveVariables2(vs, globalKnownVars, mvars);
2230+
algorithm
2231+
if BaseHashTable.hasKey(cr,mvars) then
2232+
// alg var moved to known vars
2233+
(vs_1,knvars_1) := moveVariables2(vs, globalKnownVars, mvars);
2234+
knvars_1 := (v :: knvars_1);
2235+
else
2236+
// alg var not moved to known vars
2237+
(vs_1,knvars_1) := moveVariables2(vs, globalKnownVars, mvars);
2238+
vs_1 := (v :: vs_1);
2239+
end if;
22482240
then
2249-
((v :: vs_1),knvars_1);
2250-
end matchcontinue;
2241+
(vs_1,knvars_1);
2242+
end match;
22512243
end moveVariables2;
22522244

22532245
replaceable type ElementType subtypeof Any;

Compiler/FrontEnd/ConnectionGraph.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ algorithm
858858
setRootDistance(nextLevel,table,distance+1,{},irooted);
859859
case(cr::rest,_,_,_,_)
860860
equation
861-
failure(_ = BaseHashTable.get(cr, irooted));
861+
false = BaseHashTable.hasKey(cr, irooted);
862862
rooted = BaseHashTable.add((cr,distance),irooted);
863863
next = BaseHashTable.get(cr, table);
864864
//print("- ConnectionGraph.setRootDistance: Set Distance " +
@@ -870,7 +870,7 @@ algorithm
870870
setRootDistance(rest,table,distance,next,rooted);
871871
case(cr::rest,_,_,_,_)
872872
equation
873-
failure(_ = BaseHashTable.get(cr, irooted));
873+
false = BaseHashTable.hasKey(cr, irooted);
874874
rooted = BaseHashTable.add((cr,distance),irooted);
875875
//print("- ConnectionGraph.setRootDistance: Set Distance " +
876876
// ComponentReference.printComponentRefStr(cr) + " , " + intString(distance) + "\n");

Compiler/FrontEnd/InstUtil.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8173,8 +8173,8 @@ modifiers to prevent infinite recursion"
81738173
algorithm
81748174
(outMods,outCmod,outM) := matchcontinue(variability,updatedComps,cref,mods,cmod,m)
81758175
case (_,_,_,_,_,_)
8176+
guard BaseHashTable.hasKey(cref,updatedComps)
81768177
equation
8177-
_ = BaseHashTable.get(cref,updatedComps);
81788178
checkVariabilityOfUpdatedComponent(variability,cref);
81798179
then (DAE.NOMOD(),DAE.NOMOD(),SCode.NOMOD());
81808180

Compiler/FrontEnd/Patternm.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ algorithm
16491649
case ({},_,acc,unusedHt) then (listReverse(acc),unusedHt);
16501650
case (DAE.VAR(componentRef=DAE.CREF_IDENT(ident=name), source=DAE.SOURCE(info=info))::rest,_,acc,unusedHt)
16511651
equation
1652-
failure(_ = BaseHashTable.get(name, ht));
1652+
false = BaseHashTable.hasKey(name, ht);
16531653
unusedHt = BaseHashTable.add((name,Absyn.IDENT("")),unusedHt);
16541654
Error.assertionOrAddSourceMessage(not Flags.isSet(Flags.PATTERNM_ALL_INFO),Error.META_UNUSED_DECL, {name}, info);
16551655
(acc,unusedHt) = filterUnusedDecls(rest,ht,acc,unusedHt);

Compiler/SimCode/SimCodeFunctionUtil.mo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,8 +2349,7 @@ algorithm
23492349
HashTableStringToPath.HashTable ht;
23502350

23512351
case (_, _, ht, _)
2352-
equation
2353-
BaseHashTable.get(pathstr, ht);
2352+
guard BaseHashTable.hasKey(pathstr, ht)
23542353
then ht;
23552354

23562355
case (path, _, ht, _)

Compiler/Util/VarTransform.mo

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -945,26 +945,18 @@ protected function addReplacementInv2 "
945945
input DAE.ComponentRef dst;
946946
input DAE.ComponentRef src;
947947
output HashTable3.HashTable outInvHt;
948+
protected
949+
list<DAE.ComponentRef> srcs;
948950
algorithm
949-
outInvHt:=
950-
matchcontinue (invHt,dst,src)
951-
local
952-
HashTable3.HashTable invHt_1;
953-
list<DAE.ComponentRef> srcs;
954-
case (_,_,_)
955-
equation
956-
failure(_ = BaseHashTable.get(dst,invHt)) "No previous elt for dst -> src" ;
957-
invHt_1 = BaseHashTable.add((dst, {src}),invHt);
958-
then
959-
invHt_1;
960-
case (_,_,_)
961-
equation
962-
srcs = BaseHashTable.get(dst,invHt) "previous elt for dst -> src, append.." ;
963-
srcs = amortizeUnion(src::srcs);//List.union({},src::srcs);
964-
invHt_1 = BaseHashTable.add((dst, srcs),invHt);
965-
then
966-
invHt_1;
967-
end matchcontinue;
951+
if BaseHashTable.hasKey(dst,invHt) then
952+
// previous elt for dst -> src, append.
953+
srcs := BaseHashTable.get(dst,invHt);
954+
srcs := amortizeUnion(src::srcs);//List.union({},src::srcs);
955+
outInvHt := BaseHashTable.add((dst, srcs),invHt);
956+
else
957+
// No previous elt for dst -> src
958+
outInvHt := BaseHashTable.add((dst, {src}),invHt);
959+
end if;
968960
end addReplacementInv2;
969961

970962
protected function amortizeUnion "performs listUnion but in an 'amortized' way, by only doing it occasionally"

0 commit comments

Comments
 (0)