Skip to content

Commit 3eae525

Browse files
authored
Improve Flat Modelica output (#8231)
- Only print visibility for blocks of variables, not for every variable. - Remove unnecessary public section at end of variable list. Fixes #8227.
1 parent da94d68 commit 3eae525

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ protected
5252
import DAE.ElementSource;
5353
import MetaModelica.Dangerous.listReverseInPlace;
5454
import Util;
55+
import Prefixes = NFPrefixes;
56+
import NFPrefixes.Visibility;
5557

5658
import FlatModel = NFFlatModel;
5759

@@ -213,6 +215,7 @@ public
213215
input output IOStream.IOStream s;
214216
protected
215217
FlatModel flat_model = flatModel;
218+
Visibility visibility = Visibility.PUBLIC;
216219
algorithm
217220
s := IOStream.append(s, "class '" + flat_model.name + "'\n");
218221

@@ -231,12 +234,16 @@ public
231234
end for;
232235

233236
for v in flat_model.variables loop
237+
if visibility <> Variable.visibility(v) then
238+
visibility := Variable.visibility(v);
239+
s := IOStream.append(s, Prefixes.visibilityString(visibility));
240+
s := IOStream.append(s, "\n");
241+
end if;
242+
234243
s := Variable.toFlatStream(v, " ", printBindingTypes, s);
235244
s := IOStream.append(s, ";\n");
236245
end for;
237246

238-
s := IOStream.append(s, "public\n");
239-
240247
if not listEmpty(flat_model.initialEquations) then
241248
s := IOStream.append(s, "initial equation\n");
242249
s := Equation.toFlatStreamList(flat_model.initialEquations, " ", s);

OMCompiler/Compiler/NFFrontEnd/NFVariable.mo

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ public
162162
output Variability variability = variable.attributes.variability;
163163
end variability;
164164

165+
function visibility
166+
input Variable variable;
167+
output Visibility visibility = variable.visibility;
168+
end visibility;
169+
165170
function isEmptyArray
166171
input Variable variable;
167172
output Boolean isEmpty = Type.isEmptyArray(variable.ty);
@@ -316,12 +321,6 @@ public
316321
algorithm
317322
s := IOStream.append(s, indent);
318323

319-
if var.visibility == Visibility.PROTECTED then
320-
s := IOStream.append(s, "protected ");
321-
else
322-
s := IOStream.append(s, "public ");
323-
end if;
324-
325324
s := Component.Attributes.toFlatStream(var.attributes, var.ty, s, ComponentRef.isSimple(var.name));
326325
s := IOStream.append(s, Type.toFlatString(var.ty));
327326
s := IOStream.append(s, " ");

testsuite/flattening/modelica/scodeinst/CombineSubscripts3.mo

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ end CombineSubscripts3;
2121

2222
// Result:
2323
// class 'CombineSubscripts3'
24-
// public Real[3] 'b.p';
25-
// public Real[3, 4] 'b.x';
26-
// public
24+
// Real[3] 'b.p';
25+
// Real[3, 4] 'b.x';
2726
// equation
2827
// for 'i' in 1:3 loop
2928
// for 'j' in 2:3 loop

testsuite/openmodelica/flatmodelica/SD.mo

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ end SD;
4343

4444
// Result:
4545
// class 'SD'
46-
// public parameter Integer 'N' = 3;
47-
// public parameter Real[3] 'p' = {1.0, 1.5, 2.0};
48-
// public Real[3] 'c.c.f';
49-
// public Real[3] 'c.c.e';
50-
// public Real[3, {3, 4, 5}] 'c.x';
51-
// public parameter Real[3] 'c.p' = 'p'[:];
52-
// public parameter Integer[3] 'c.N' = {3, 4, 5};
53-
// public parameter Real 's.p' = 3.0;
54-
// public Real 's.c.e';
55-
// public Real 's.c.f';
56-
// public
46+
// parameter Integer 'N' = 3;
47+
// parameter Real[3] 'p' = {1.0, 1.5, 2.0};
48+
// Real[3] 'c.c.f';
49+
// Real[3] 'c.c.e';
50+
// Real[3, {3, 4, 5}] 'c.x';
51+
// parameter Real[3] 'c.p' = 'p'[:];
52+
// parameter Integer[3] 'c.N' = {3, 4, 5};
53+
// parameter Real 's.p' = 3.0;
54+
// Real 's.c.e';
55+
// Real 's.c.f';
5756
// equation
5857
// 'c.x'[:,1] = 'c.c.e'[:];
5958
// 'c.x'[:,'c.N'[:]] = 'c.c.f'[:];

0 commit comments

Comments
 (0)