Skip to content

Commit 635218f

Browse files
authored
Handle unknown dimensions better in getModelInstance (#12690)
- Allow `fill` to have unknown dimensions when using the instance API. - Use the correct variability for invalid components. Fixes #12662
1 parent 2fd0df3 commit 635218f

File tree

5 files changed

+179
-3
lines changed

5 files changed

+179
-3
lines changed

OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ protected
953953
(arg, arg_ty, arg_var, arg_pur) := Typing.typeExp(arg, context, info);
954954

955955
if not (InstContext.inAlgorithm(context) or InstContext.inFunction(context)) then
956-
if arg_var > Variability.PARAMETER then
956+
if arg_var > Variability.PARAMETER and not InstContext.inInstanceAPI(context) then
957957
Error.addSourceMessageAndFail(Error.NON_PARAMETER_EXPRESSION_DIMENSION,
958958
{Expression.toString(arg), String(index),
959959
List.toString(fillArg :: dimensionArgs, Expression.toString,
@@ -962,7 +962,7 @@ protected
962962

963963
if arg_pur == Purity.PURE and not Structural.isExpressionNotFixed(arg) then
964964
Structural.markExp(arg);
965-
arg := Ceval.evalExp(arg);
965+
arg := if InstContext.inInstanceAPI(context) then Ceval.tryEvalExp(arg) else Ceval.evalExp(arg);
966966
arg_ty := Expression.typeOf(arg);
967967
end if;
968968
end if;

OMCompiler/Compiler/NFFrontEnd/NFComponent.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ public
508508
case COMPONENT(attributes = Attributes.ATTRIBUTES(variability = variability)) then variability;
509509
case ITERATOR() then component.variability;
510510
case ENUM_LITERAL() then Variability.CONSTANT;
511+
case INVALID_COMPONENT() then variability(component.component);
511512
else Variability.CONTINUOUS;
512513
end match;
513514
end variability;

testsuite/openmodelica/instance-API/GetModelInstanceDim2.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// name: GetModelInstanceDim1
1+
// name: GetModelInstanceDim2
22
// keywords:
33
// status: correct
44
// cflags: -d=newInst
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// name: GetModelInstanceDim3
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
//
7+
8+
loadString("
9+
block Table
10+
parameter Real offset[:] = {0};
11+
end Table;
12+
13+
model M
14+
parameter Real table[:, :];
15+
parameter Real offset[:] = fill(0, nout);
16+
final parameter Integer nout = size(table, 2) - 1;
17+
Table tab(final offset = offset);
18+
end M;
19+
");
20+
21+
getModelInstance(M, prettyPrint = true);
22+
getErrorString();
23+
24+
// Result:
25+
// true
26+
// "{
27+
// \"name\": \"M\",
28+
// \"restriction\": \"model\",
29+
// \"elements\": [
30+
// {
31+
// \"$kind\": \"component\",
32+
// \"name\": \"table\",
33+
// \"type\": \"Real\",
34+
// \"dims\": {
35+
// \"absyn\": [
36+
// \":\",
37+
// \":\"
38+
// ],
39+
// \"typed\": [
40+
// \":\",
41+
// \":\"
42+
// ]
43+
// },
44+
// \"prefixes\": {
45+
// \"variability\": \"parameter\"
46+
// }
47+
// },
48+
// {
49+
// \"$kind\": \"component\",
50+
// \"name\": \"offset\",
51+
// \"type\": \"Real\",
52+
// \"dims\": {
53+
// \"absyn\": [
54+
// \":\"
55+
// ],
56+
// \"typed\": [
57+
// \"nout\"
58+
// ]
59+
// },
60+
// \"modifiers\": \"fill(0, nout)\",
61+
// \"value\": {
62+
// \"binding\": {
63+
// \"$kind\": \"call\",
64+
// \"name\": \"fill\",
65+
// \"arguments\": [
66+
// 0,
67+
// {
68+
// \"$kind\": \"cref\",
69+
// \"parts\": [
70+
// {
71+
// \"name\": \"nout\"
72+
// }
73+
// ]
74+
// }
75+
// ]
76+
// }
77+
// },
78+
// \"prefixes\": {
79+
// \"variability\": \"parameter\"
80+
// }
81+
// },
82+
// {
83+
// \"$kind\": \"component\",
84+
// \"name\": \"nout\",
85+
// \"type\": \"Integer\",
86+
// \"modifiers\": \"size(table, 2) - 1\",
87+
// \"value\": {
88+
// \"binding\": {
89+
// \"$kind\": \"binary_op\",
90+
// \"lhs\": {
91+
// \"$kind\": \"call\",
92+
// \"name\": \"size\",
93+
// \"arguments\": [
94+
// {
95+
// \"$kind\": \"cref\",
96+
// \"parts\": [
97+
// {
98+
// \"name\": \"table\"
99+
// }
100+
// ]
101+
// },
102+
// 2
103+
// ]
104+
// },
105+
// \"op\": \"-\",
106+
// \"rhs\": 1
107+
// }
108+
// },
109+
// \"prefixes\": {
110+
// \"final\": true,
111+
// \"variability\": \"parameter\"
112+
// }
113+
// },
114+
// {
115+
// \"$kind\": \"component\",
116+
// \"name\": \"tab\",
117+
// \"type\": {
118+
// \"name\": \"Table\",
119+
// \"restriction\": \"block\",
120+
// \"elements\": [
121+
// {
122+
// \"$kind\": \"component\",
123+
// \"name\": \"offset\",
124+
// \"type\": \"Real\",
125+
// \"dims\": {
126+
// \"absyn\": [
127+
// \":\"
128+
// ],
129+
// \"typed\": [
130+
// \"nout\"
131+
// ]
132+
// },
133+
// \"modifiers\": \"{0}\",
134+
// \"value\": {
135+
// \"binding\": {
136+
// \"$kind\": \"cref\",
137+
// \"parts\": [
138+
// {
139+
// \"name\": \"offset\"
140+
// }
141+
// ]
142+
// }
143+
// },
144+
// \"prefixes\": {
145+
// \"variability\": \"parameter\"
146+
// }
147+
// }
148+
// ],
149+
// \"source\": {
150+
// \"filename\": \"<interactive>\",
151+
// \"lineStart\": 2,
152+
// \"columnStart\": 3,
153+
// \"lineEnd\": 4,
154+
// \"columnEnd\": 12
155+
// }
156+
// },
157+
// \"modifiers\": {
158+
// \"offset\": {
159+
// \"final\": true,
160+
// \"$value\": \"offset\"
161+
// }
162+
// }
163+
// }
164+
// ],
165+
// \"source\": {
166+
// \"filename\": \"<interactive>\",
167+
// \"lineStart\": 6,
168+
// \"columnStart\": 3,
169+
// \"lineEnd\": 11,
170+
// \"columnEnd\": 8
171+
// }
172+
// }"
173+
// ""
174+
// endResult

testsuite/openmodelica/instance-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ GetModelInstanceDerived2.mos \
4242
GetModelInstanceDerived3.mos \
4343
GetModelInstanceDim1.mos \
4444
GetModelInstanceDim2.mos \
45+
GetModelInstanceDim3.mos \
4546
GetModelInstanceDuplicate1.mos \
4647
GetModelInstanceEnum1.mos \
4748
GetModelInstanceEnum2.mos \

0 commit comments

Comments
 (0)