Skip to content

Commit 08f36de

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Fix stack overflow
1 parent 7153a59 commit 08f36de

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

Compiler/FrontEnd/Absyn.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6721,6 +6721,16 @@ algorithm
67216721
end match;
67226722
end isElementItem;
67236723

6724+
public function isAlgorithmItem
6725+
input AlgorithmItem inAlg;
6726+
output Boolean outIsClass;
6727+
algorithm
6728+
outIsClass := match inAlg
6729+
case ALGORITHMITEM() then true;
6730+
else false;
6731+
end match;
6732+
end isAlgorithmItem;
6733+
67246734
public function isElementItemClassNamed
67256735
input String inName;
67266736
input ElementItem inElement;

Compiler/FrontEnd/SCodeUtil.mo

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -795,37 +795,24 @@ end translateClassdefInitialalgorithms;
795795

796796
public function translateClassdefAlgorithmitems
797797
input list<Absyn.AlgorithmItem> inStatements;
798-
output list<SCode.Statement> outStatements = {};
799-
protected
800-
SCode.Comment cmt;
801-
SourceInfo info;
802-
SCode.Statement s;
803-
algorithm
804-
for stmt in inStatements loop
805-
_ := match stmt
806-
case Absyn.ALGORITHMITEM(info = info)
807-
algorithm
808-
(cmt, info) := translateCommentWithLineInfoChanges(stmt.comment, info);
809-
s := translateClassdefAlgorithmItem(stmt.algorithm_, cmt, info);
810-
outStatements := s :: outStatements;
811-
then
812-
();
813-
814-
else ();
815-
end match;
816-
end for;
817-
818-
outStatements := listReverse(outStatements);
798+
output list<SCode.Statement> outStatements;
799+
algorithm
800+
outStatements := list(translateClassdefAlgorithmItem(stmt) for stmt guard Absyn.isAlgorithmItem(stmt) in inStatements);
819801
end translateClassdefAlgorithmitems;
820802

821803
protected function translateClassdefAlgorithmItem
822804
"Translates an Absyn algorithm (statement) into SCode statement."
823-
input Absyn.Algorithm inAlgorithm;
824-
input SCode.Comment inComment;
825-
input SourceInfo inInfo;
805+
input Absyn.AlgorithmItem inAlgorithm;
826806
output SCode.Statement outStatement;
807+
protected
808+
Option<Absyn.Comment> absynComment;
809+
SCode.Comment comment;
810+
SourceInfo info;
811+
Absyn.Algorithm alg;
827812
algorithm
828-
outStatement := match inAlgorithm
813+
Absyn.ALGORITHMITEM(algorithm_=alg, comment=absynComment, info=info) := inAlgorithm;
814+
(comment, info) := translateCommentWithLineInfoChanges(absynComment, info);
815+
outStatement := match alg
829816
local
830817
list<SCode.Statement> body, else_body;
831818
list<tuple<Absyn.Exp, list<SCode.Statement>>> branches;
@@ -836,101 +823,101 @@ algorithm
836823
Absyn.ComponentRef cr;
837824

838825
case Absyn.ALG_ASSIGN()
839-
then SCode.ALG_ASSIGN(inAlgorithm.assignComponent, inAlgorithm.value,
840-
inComment, inInfo);
826+
then SCode.ALG_ASSIGN(alg.assignComponent, alg.value,
827+
comment, info);
841828

842829
case Absyn.ALG_IF()
843830
algorithm
844-
body := translateClassdefAlgorithmitems(inAlgorithm.trueBranch);
845-
else_body := translateClassdefAlgorithmitems(inAlgorithm.elseBranch);
846-
branches := translateAlgBranches(inAlgorithm.elseIfAlgorithmBranch);
831+
body := translateClassdefAlgorithmitems(alg.trueBranch);
832+
else_body := translateClassdefAlgorithmitems(alg.elseBranch);
833+
branches := translateAlgBranches(alg.elseIfAlgorithmBranch);
847834
then
848-
SCode.ALG_IF(inAlgorithm.ifExp, body, branches, else_body, inComment, inInfo);
835+
SCode.ALG_IF(alg.ifExp, body, branches, else_body, comment, info);
849836

850837
case Absyn.ALG_FOR()
851838
algorithm
852-
body := translateClassdefAlgorithmitems(inAlgorithm.forBody);
839+
body := translateClassdefAlgorithmitems(alg.forBody);
853840

854841
// Convert for-loops with multiple iterators into nested for-loops.
855-
for i in listReverse(inAlgorithm.iterators) loop
856-
(iter_name, iter_range) := translateIterator(i, inInfo);
857-
body := {SCode.ALG_FOR(iter_name, iter_range, body, inComment, inInfo)};
842+
for i in listReverse(alg.iterators) loop
843+
(iter_name, iter_range) := translateIterator(i, info);
844+
body := {SCode.ALG_FOR(iter_name, iter_range, body, comment, info)};
858845
end for;
859846
then
860847
listHead(body);
861848

862849
case Absyn.ALG_PARFOR()
863850
algorithm
864-
body := translateClassdefAlgorithmitems(inAlgorithm.parforBody);
851+
body := translateClassdefAlgorithmitems(alg.parforBody);
865852

866853
// Convert for-loops with multiple iterators into nested for-loops.
867-
for i in listReverse(inAlgorithm.iterators) loop
868-
(iter_name, iter_range) := translateIterator(i, inInfo);
869-
body := {SCode.ALG_PARFOR(iter_name, iter_range, body, inComment, inInfo)};
854+
for i in listReverse(alg.iterators) loop
855+
(iter_name, iter_range) := translateIterator(i, info);
856+
body := {SCode.ALG_PARFOR(iter_name, iter_range, body, comment, info)};
870857
end for;
871858
then
872859
listHead(body);
873860

874861
case Absyn.ALG_WHILE()
875862
algorithm
876-
body := translateClassdefAlgorithmitems(inAlgorithm.whileBody);
863+
body := translateClassdefAlgorithmitems(alg.whileBody);
877864
then
878-
SCode.ALG_WHILE(inAlgorithm.boolExpr, body, inComment, inInfo);
865+
SCode.ALG_WHILE(alg.boolExpr, body, comment, info);
879866

880867
case Absyn.ALG_WHEN_A()
881868
algorithm
882-
branches := translateAlgBranches((inAlgorithm.boolExpr, inAlgorithm.whenBody)
883-
:: inAlgorithm.elseWhenAlgorithmBranch);
869+
branches := translateAlgBranches((alg.boolExpr, alg.whenBody)
870+
:: alg.elseWhenAlgorithmBranch);
884871
then
885-
SCode.ALG_WHEN_A(branches, inComment, inInfo);
872+
SCode.ALG_WHEN_A(branches, comment, info);
886873

887874
// assert(condition, message)
888875
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
889876
functionArgs = Absyn.FUNCTIONARGS(args = {e1, e2}, argNames = {}))
890-
then SCode.ALG_ASSERT(e1, e2, ASSERTION_LEVEL_ERROR, inComment, inInfo);
877+
then SCode.ALG_ASSERT(e1, e2, ASSERTION_LEVEL_ERROR, comment, info);
891878

892879
// assert(condition, message, level)
893880
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
894881
functionArgs = Absyn.FUNCTIONARGS(args = {e1, e2, e3}, argNames = {}))
895-
then SCode.ALG_ASSERT(e1, e2, e3, inComment, inInfo);
882+
then SCode.ALG_ASSERT(e1, e2, e3, comment, info);
896883

897884
// assert(condition, message, level = arg)
898885
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
899886
functionArgs = Absyn.FUNCTIONARGS(args = {e1, e2},
900887
argNames = {Absyn.NAMEDARG("level", e3)}))
901-
then SCode.ALG_ASSERT(e1, e2, e3, inComment, inInfo);
888+
then SCode.ALG_ASSERT(e1, e2, e3, comment, info);
902889

903890
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "terminate"),
904891
functionArgs = Absyn.FUNCTIONARGS(args = {e1}, argNames = {}))
905-
then SCode.ALG_TERMINATE(e1, inComment, inInfo);
892+
then SCode.ALG_TERMINATE(e1, comment, info);
906893

907894
case Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "reinit"),
908895
functionArgs = Absyn.FUNCTIONARGS(args = {Absyn.CREF(componentRef = cr), e2},
909896
argNames = {}))
910-
then SCode.ALG_REINIT(cr, e2, inComment, inInfo);
897+
then SCode.ALG_REINIT(cr, e2, comment, info);
911898

912899
case Absyn.ALG_NORETCALL()
913900
algorithm
914-
e1 := Absyn.CALL(inAlgorithm.functionCall, inAlgorithm.functionArgs);
901+
e1 := Absyn.CALL(alg.functionCall, alg.functionArgs);
915902
then
916-
SCode.ALG_NORETCALL(e1, inComment, inInfo);
903+
SCode.ALG_NORETCALL(e1, comment, info);
917904

918905
case Absyn.ALG_FAILURE()
919906
algorithm
920-
body := translateClassdefAlgorithmitems(inAlgorithm.equ);
907+
body := translateClassdefAlgorithmitems(alg.equ);
921908
then
922-
SCode.ALG_FAILURE(body, inComment, inInfo);
909+
SCode.ALG_FAILURE(body, comment, info);
923910

924911
case Absyn.ALG_TRY()
925912
algorithm
926-
body := translateClassdefAlgorithmitems(inAlgorithm.body);
927-
else_body := translateClassdefAlgorithmitems(inAlgorithm.elseBody);
913+
body := translateClassdefAlgorithmitems(alg.body);
914+
else_body := translateClassdefAlgorithmitems(alg.elseBody);
928915
then
929-
SCode.ALG_TRY(body, else_body, inComment, inInfo);
916+
SCode.ALG_TRY(body, else_body, comment, info);
930917

931-
case Absyn.ALG_RETURN() then SCode.ALG_RETURN(inComment, inInfo);
932-
case Absyn.ALG_BREAK() then SCode.ALG_BREAK(inComment, inInfo);
933-
case Absyn.ALG_CONTINUE() then SCode.ALG_CONTINUE(inComment, inInfo);
918+
case Absyn.ALG_RETURN() then SCode.ALG_RETURN(comment, info);
919+
case Absyn.ALG_BREAK() then SCode.ALG_BREAK(comment, info);
920+
case Absyn.ALG_CONTINUE() then SCode.ALG_CONTINUE(comment, info);
934921

935922
end match;
936923
end translateClassdefAlgorithmItem;

0 commit comments

Comments
 (0)