Skip to content

Commit 58602fd

Browse files
authored
Change Tpl.mo to use Mutable instead of Array (#15590)
Arrays in datatypes that can be constants are problematic (Mutable is as well, but less so).
1 parent 06cbae5 commit 58602fd

3 files changed

Lines changed: 72 additions & 69 deletions

File tree

OMCompiler/Compiler/Template/Tpl.mo

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import Error;
5151
import File;
5252
import Flags;
5353
import List;
54+
import Mutable;
5455
import Print;
5556
import StackOverflow;
5657
import StringUtil;
@@ -70,9 +71,9 @@ uniontype Text
7071
end MEM_TEXT;
7172
record FILE_TEXT
7273
Option<Integer> opaqueFile;
73-
array<Integer> nchars, aind;
74-
array<Boolean> isstart;
75-
array<list<BlockTypeFileText>> blocksStack;
74+
Mutable<Integer> nchars, aind;
75+
Mutable<Boolean> isstart;
76+
Mutable<list<BlockTypeFileText>> blocksStack;
7677
end FILE_TEXT;
7778
end Text;
7879

@@ -84,8 +85,8 @@ uniontype BlockTypeFileText
8485
BlockType bt "The block type";
8586
Integer nchars, aind;
8687
Boolean isstart;
87-
array<Integer> tell "Usage depends on bt; stores the last file position to know if it is empty or not.";
88-
array<Option<StringToken>> septok;
88+
Mutable<Integer> tell "Usage depends on bt; stores the last file position to know if it is empty or not.";
89+
Mutable<Option<StringToken>> septok;
8990
end BT_FILE_TEXT;
9091
end BlockTypeFileText;
9192

@@ -135,7 +136,7 @@ uniontype BlockType
135136
record BT_ITER "Iteration items block, every token in the block is an item.
136137
index0 is the active index during the build phase, then it is the last one + 1."
137138
IterOptions options;
138-
array<Integer> index0;
139+
Mutable<Integer> index0;
139140
end BT_ITER;
140141
end BlockType;
141142

@@ -475,7 +476,7 @@ algorithm
475476
then isAtStartOfLineTok(tok);
476477

477478
case FILE_TEXT()
478-
then arrayGet(text.isstart,1);
479+
then Mutable.access(text.isstart);
479480

480481
end match;
481482
end isAtStartOfLine;
@@ -557,30 +558,30 @@ algorithm
557558

558559
case FILE_TEXT()
559560
algorithm
560-
nchars := arrayGet(txt.nchars,1);
561-
aind := arrayGet(txt.aind,1);
562-
isstart := arrayGet(txt.isstart,1);
563-
arrayUpdate(txt.blocksStack, 1, BT_FILE_TEXT(inBlockType, nchars, aind, isstart, arrayCreate(1, textFileTell(txt)), arrayCreate(1, NONE()))::arrayGet(txt.blocksStack, 1));
561+
nchars := Mutable.access(txt.nchars);
562+
aind := Mutable.access(txt.aind);
563+
isstart := Mutable.access(txt.isstart);
564+
Mutable.update(txt.blocksStack, BT_FILE_TEXT(inBlockType, nchars, aind, isstart, Mutable.create(textFileTell(txt)), Mutable.create(NONE()))::Mutable.access(txt.blocksStack));
564565
_ := match inBlockType
565566
case BT_INDENT(width = w)
566567
algorithm
567-
arrayUpdate(txt.nchars, 1, nchars+w);
568-
arrayUpdate(txt.aind, 1, aind+w);
568+
Mutable.update(txt.nchars, nchars+w);
569+
Mutable.update(txt.aind, aind+w);
569570
then ();
570571
case BT_ABS_INDENT(width = w)
571572
algorithm
572573
if isstart then
573-
arrayUpdate(txt.nchars, 1, 0);
574+
Mutable.update(txt.nchars, 0);
574575
end if;
575-
arrayUpdate(txt.aind, 1, w);
576+
Mutable.update(txt.aind, w);
576577
then ();
577578
case BT_REL_INDENT(offset = w)
578579
algorithm
579-
arrayUpdate(txt.aind, 1, aind + w);
580+
Mutable.update(txt.aind, aind + w);
580581
then ();
581582
case BT_ANCHOR(offset = w)
582583
algorithm
583-
arrayUpdate(txt.aind, 1, nchars + w);
584+
Mutable.update(txt.aind, nchars + w);
584585
then ();
585586
else ();
586587
end match;
@@ -628,15 +629,15 @@ algorithm
628629

629630
case FILE_TEXT()
630631
algorithm
631-
blk::rest := arrayGet(txt.blocksStack, 1);
632-
arrayUpdate(txt.blocksStack, 1, rest);
632+
blk::rest := Mutable.access(txt.blocksStack);
633+
Mutable.update(txt.blocksStack, rest);
633634
_ := match blk.bt
634635
case BT_INDENT()
635636
algorithm
636-
if arrayGet(txt.isstart,1) then
637-
arrayUpdate(txt.nchars, 1, blk.nchars);
637+
if Mutable.access(txt.isstart) then
638+
Mutable.update(txt.nchars, blk.nchars);
638639
end if;
639-
arrayUpdate(txt.aind, 1, blk.aind);
640+
Mutable.update(txt.aind, blk.aind);
640641
then ();
641642
case _ guard match blk.bt
642643
// All these have the same cases
@@ -645,24 +646,24 @@ algorithm
645646
case BT_ANCHOR() then true;
646647
end match
647648
algorithm
648-
oldisstart := arrayGet(txt.isstart,1);
649+
oldisstart := Mutable.access(txt.isstart);
649650
if oldisstart then
650-
if textFileTell(txt)==arrayGet(blk.tell,1) then
651+
if textFileTell(txt)==Mutable.access(blk.tell) then
651652
// No update, restore nchars
652-
arrayUpdate(txt.nchars, 1, blk.nchars);
653+
Mutable.update(txt.nchars, blk.nchars);
653654
else
654655
// Update; restore depends on if we are at start of line
655-
if arrayGet(txt.isstart,1) then
656-
arrayUpdate(txt.nchars, 1, blk.aind);
656+
if Mutable.access(txt.isstart) then
657+
Mutable.update(txt.nchars, blk.aind);
657658
end if;
658659
end if;
659660
else
660661
// Was not at start of line before
661-
if arrayGet(txt.isstart,1) then
662-
arrayUpdate(txt.nchars, 1, blk.aind);
662+
if Mutable.access(txt.isstart) then
663+
Mutable.update(txt.nchars, blk.aind);
663664
end if;
664665
end if;
665-
arrayUpdate(txt.aind, 1, blk.aind);
666+
Mutable.update(txt.aind, blk.aind);
666667
then ();
667668
else ();
668669
end match;
@@ -699,7 +700,7 @@ algorithm
699700
then //let the existing tokens on stack in the text block and start iterating
700701
MEM_TEXT(
701702
{},
702-
({}, BT_ITER(iopts, arrayCreate(1,i0))) :: (toks, BT_TEXT()) :: blstack);
703+
({}, BT_ITER(iopts, Mutable.create(i0))) :: (toks, BT_TEXT()) :: blstack);
703704

704705
case (FILE_TEXT(),
705706
iopts as ITER_OPTIONS(
@@ -712,7 +713,7 @@ algorithm
712713
Error.addInternalError("Tpl.mo FILE_TEXT does not support aligning or wrapping elements", sourceInfo());
713714
then fail();
714715
end match;
715-
pushBlock(txt, BT_ITER(inIterOptions, arrayCreate(1,i0)));
716+
pushBlock(txt, BT_ITER(inIterOptions, Mutable.create(i0)));
716717
then txt;
717718

718719
//should not ever happen
@@ -753,7 +754,7 @@ algorithm
753754

754755
case FILE_TEXT()
755756
algorithm
756-
arrayUpdate(txt.blocksStack, 1, listRest(arrayGet(txt.blocksStack, 1)));
757+
Mutable.update(txt.blocksStack, listRest(Mutable.access(txt.blocksStack)));
757758
then txt;
758759

759760
//should not ever happen
@@ -776,12 +777,12 @@ algorithm
776777
StringToken tok, emptok;
777778
list<tuple<Tokens,BlockType>> blstack;
778779
IterOptions iopts;
779-
array<Integer> i0, tell;
780+
Mutable<Integer> i0, tell;
780781
BlockType bt;
781782
Integer tellpos, curIndex;
782783
Text txt2;
783784
Boolean haveToken;
784-
array<Option<StringToken>> septok;
785+
Mutable<Option<StringToken>> septok;
785786

786787
//empty iteration segment and 'empty' option is NONE(), so do nothing
787788
case (txt as MEM_TEXT(
@@ -800,7 +801,7 @@ algorithm
800801
index0 = i0)) :: blstack
801802
))
802803
algorithm
803-
arrayUpdate(i0, 1, arrayGet(i0,1) + 1);
804+
Mutable.update(i0, Mutable.access(i0) + 1);
804805
then
805806
MEM_TEXT(
806807
{},
@@ -814,7 +815,7 @@ algorithm
814815
blocksStack = (itertoks, bt as BT_ITER(index0 = i0)) :: blstack
815816
))
816817
algorithm
817-
arrayUpdate(i0, 1, arrayGet(i0,1) + 1);
818+
Mutable.update(i0, Mutable.access(i0) + 1);
818819
then
819820
MEM_TEXT(
820821
{},
@@ -827,7 +828,7 @@ algorithm
827828
blocksStack = (itertoks, bt as BT_ITER(index0 = i0)) :: blstack
828829
))
829830
algorithm
830-
arrayUpdate(i0, 1, arrayGet(i0,1) + 1);
831+
Mutable.update(i0, Mutable.access(i0) + 1);
831832
then
832833
MEM_TEXT(
833834
{},
@@ -836,14 +837,14 @@ algorithm
836837

837838
case FILE_TEXT()
838839
algorithm
839-
_ := match listGet(arrayGet(txt.blocksStack,1),1)
840+
_ := match listGet(Mutable.access(txt.blocksStack),1)
840841
case BT_FILE_TEXT(bt=BT_ITER(options = iopts, index0=i0), tell=tell, septok=septok)
841842
algorithm
842843
// Either the iterator always increments, or the file position changed
843844
tellpos := textFileTell(txt);
844-
if arrayGet(tell,1)<>tellpos then
845+
if Mutable.access(tell)<>tellpos then
845846
// Update file position and increment i0. Else, we are at the same position and state as before.
846-
arrayUpdate(tell, 1, tellpos);
847+
Mutable.update(tell, tellpos);
847848
txt2 := txt;
848849
haveToken := true;
849850
else
@@ -855,16 +856,16 @@ algorithm
855856
then txt;
856857
case SOME(emptok)
857858
algorithm
858-
arrayUpdate(i0, 1, arrayGet(i0,1) + 1);
859+
Mutable.update(i0, Mutable.access(i0) + 1);
859860
haveToken := true;
860861
then writeTok(txt, emptok);
861862
end match;
862863
end if;
863864
if haveToken then
864865
// Handle separator
865-
curIndex := arrayGet(i0,1);
866-
arrayUpdate(septok, 1, iopts.separator);
867-
arrayUpdate(i0, 1, curIndex + 1);
866+
curIndex := Mutable.access(i0);
867+
Mutable.update(septok, iopts.separator);
868+
Mutable.update(i0, curIndex + 1);
868869
end if;
869870
then ();
870871
end match;
@@ -886,16 +887,16 @@ public function getIteri_i0
886887
algorithm
887888
outI0 := match (inText)
888889
local
889-
array<Integer> i0;
890+
Mutable<Integer> i0;
890891

891892
case (MEM_TEXT(
892893
blocksStack = (_, BT_ITER(index0 = i0)) :: _
893894
))
894895
then
895-
arrayGet(i0,1);
896+
Mutable.access(i0);
896897

897898
case FILE_TEXT()
898-
then match listGet(arrayGet(inText.blocksStack,1),1) case BT_FILE_TEXT(bt=BT_ITER(index0=i0)) then arrayGet(i0,1); end match;
899+
then match listGet(Mutable.access(inText.blocksStack),1) case BT_FILE_TEXT(bt=BT_ITER(index0=i0)) then Mutable.access(i0); end match;
899900

900901
//should not ever happen
901902
case (_ )
@@ -1089,13 +1090,13 @@ algorithm
10891090
_ := match inText
10901091
case FILE_TEXT()
10911092
algorithm
1092-
nchars := arrayGet(inText.nchars, 1);
1093-
aind := arrayGet(inText.aind, 1);
1094-
isstart := arrayGet(inText.isstart, 1);
1093+
nchars := Mutable.access(inText.nchars);
1094+
aind := Mutable.access(inText.aind);
1095+
isstart := Mutable.access(inText.isstart);
10951096
(nchars, isstart, aind) := tokFile(file, inStringToken, nchars, isstart, aind);
1096-
arrayUpdate(inText.nchars, 1, nchars);
1097-
arrayUpdate(inText.aind, 1, aind);
1098-
arrayUpdate(inText.isstart, 1, isstart);
1097+
Mutable.update(inText.nchars, nchars);
1098+
Mutable.update(inText.aind, aind);
1099+
Mutable.update(inText.isstart, isstart);
10991100
then ();
11001101
end match;
11011102
end tokFileText;
@@ -2464,7 +2465,7 @@ algorithm
24642465
System.appendFile(Testsuite.getTempFilesFile(), fileName + "\n");
24652466
end if;
24662467
File.open(file, fileName, File.Mode.Write);
2467-
text := writeText(FILE_TEXT(File.getReference(file), arrayCreate(1, 0), arrayCreate(1, 0), arrayCreate(1, true), arrayCreate(1, {})), text);
2468+
text := writeText(FILE_TEXT(File.getReference(file), Mutable.create(0), Mutable.create(0), Mutable.create(true), Mutable.create({})), text);
24682469
end redirectToFile;
24692470

24702471
public function closeFile
@@ -2512,25 +2513,25 @@ algorithm
25122513
case FILE_TEXT()
25132514
algorithm
25142515
handleTok(inText);
2515-
nchars := arrayGet(inText.nchars, 1);
2516+
nchars := Mutable.access(inText.nchars);
25162517
if not line then
2517-
if arrayGet(inText.isstart,1) then
2518+
if Mutable.access(inText.isstart) then
25182519
File.writeSpace(file, nchars);
25192520
File.write(file, str);
2520-
arrayUpdate(inText.nchars, 1, nchars+stringLength(str));
2521-
arrayUpdate(inText.isstart, 1, false);
2521+
Mutable.update(inText.nchars, nchars+stringLength(str));
2522+
Mutable.update(inText.isstart, false);
25222523
else
25232524
File.write(file, str);
2524-
arrayUpdate(inText.nchars, 1, nchars+stringLength(str));
2525+
Mutable.update(inText.nchars, nchars+stringLength(str));
25252526
end if;
25262527
else
2527-
if arrayGet(inText.isstart,1) then
2528+
if Mutable.access(inText.isstart) then
25282529
File.writeSpace(file, nchars);
25292530
else
2530-
arrayUpdate(inText.isstart,1,true);
2531+
Mutable.update(inText.isstart, true);
25312532
end if;
25322533
File.write(file, str);
2533-
arrayUpdate(inText.nchars,1,arrayGet(inText.aind,1));
2534+
Mutable.update(inText.nchars, Mutable.access(inText.aind));
25342535
end if;
25352536
then ();
25362537
end match;
@@ -2546,8 +2547,8 @@ algorithm
25462547
case FILE_TEXT()
25472548
algorithm
25482549
File.write(file, "\n");
2549-
arrayUpdate(inText.nchars, 1, arrayGet(inText.aind, 1));
2550-
arrayUpdate(inText.isstart, 1, true);
2550+
Mutable.update(inText.nchars, Mutable.access(inText.aind));
2551+
Mutable.update(inText.isstart, true);
25512552
then ();
25522553
end match;
25532554
end newlineFile;
@@ -2565,18 +2566,18 @@ protected function handleTok "Handle a new token, for example separators"
25652566
input Text txt;
25662567
protected
25672568
StringToken septok;
2568-
array<Option<StringToken>> aseptok;
2569+
Mutable<Option<StringToken>> aseptok;
25692570
algorithm
25702571
_ := match txt
25712572
case FILE_TEXT()
25722573
algorithm
2573-
_ := match arrayGet(txt.blocksStack, 1)
2574+
_ := match Mutable.access(txt.blocksStack)
25742575
case (BT_FILE_TEXT(bt=BT_ITER(), septok=aseptok)::_)
25752576
algorithm
2576-
_ := match arrayGet(aseptok,1)
2577+
_ := match Mutable.access(aseptok)
25772578
case SOME(septok)
25782579
algorithm
2579-
arrayUpdate(aseptok,1,NONE());
2580+
Mutable.update(aseptok,NONE());
25802581
tokFileText(txt, septok, doHandleTok=false);
25812582
then ();
25822583
else ();

testsuite/special/MatlabTranslator/LoadCompilerSources.mos

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ if true then /* Suppress output */
256256
prefixPath + "Util/Lapack.mo",
257257
prefixPath + "Util/List.mo",
258258
prefixPath + "Util/ModelicaExternalC.mo",
259+
prefixPath + "Util/Mutable.mo",
259260
prefixPath + "Util/Print.mo",
260261
prefixPath + "Util/PriorityQueue.mo",
261262
prefixPath + "Util/Settings.mo",

testsuite/special/MatlabTranslator/SCRIPT.mos

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ files := {
3131
"../../../OMCompiler/Compiler/Util/Testsuite.mo",
3232
"../../../OMCompiler/Compiler/Util/Corba.mo",
3333
"../../../OMCompiler/Compiler/Util/File.mo",
34+
"../../../OMCompiler/Compiler/Util/Mutable.mo",
3435
"../../../OMCompiler/Compiler/Util/Util.mo",
3536
"../../../OMCompiler/Compiler/Util/System.mo",
3637
"../../../OMCompiler/Compiler/SimCode/SimCodeUtil.mo",

0 commit comments

Comments
 (0)