Skip to content

Commit de70371

Browse files
authored
Fix annotation instantiation in getModelInstance (#13491)
- Move `Typing.crefContext` to `InstContext.nodeContext` and use it in `Ceval` when typing components to make sure global context flags are taken into account. Fixes #13418
1 parent 3675193 commit de70371

File tree

5 files changed

+256
-31
lines changed

5 files changed

+256
-31
lines changed

OMCompiler/Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,7 @@ protected
346346
Type cref_ty, exp_ty;
347347
Integer dim_diff;
348348
algorithm
349-
exp_context := if InstNode.isFunction(InstNode.explicitParent(node))
350-
then NFInstContext.FUNCTION else NFInstContext.CLASS;
351-
349+
exp_context := InstContext.nodeContext(node, target.context);
352350
Typing.typeComponentBinding(node, exp_context, typeChildren = false);
353351
comp := InstNode.component(node);
354352
binding := Component.getBinding(comp);
@@ -696,8 +694,7 @@ protected
696694
algorithm
697695
parent_cr := ComponentRef.rest(cref);
698696
parent := ComponentRef.node(parent_cr);
699-
exp_context := if InstNode.isFunction(InstNode.explicitParent(parent))
700-
then NFInstContext.FUNCTION else NFInstContext.CLASS;
697+
exp_context := InstContext.nodeContext(parent, target.context);
701698

702699
comp := InstNode.component(parent);
703700
binding := Component.getBinding(comp);

OMCompiler/Compiler/NFFrontEnd/NFInstContext.mo

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
encapsulated package NFInstContext
3333
"Used by the instantation to keep track of in which context the instantiation is done."
3434

35+
import NFInstNode.InstNode;
36+
37+
protected
38+
import Restriction = NFRestriction;
39+
40+
public
3541
type Type = Integer;
3642

3743
// Flag values:
@@ -280,6 +286,33 @@ encapsulated package NFInstContext
280286
output Boolean isSingle = context < ITERATION_RANGE - 1;
281287
end isSingleExpression;
282288

289+
function nodeContext
290+
"Constructs a context for a node based on where the element that the node
291+
represents is declared rather than where it is used."
292+
input InstNode node;
293+
input Type currentContext;
294+
output Type nodeContext;
295+
protected
296+
InstNode parent;
297+
Restriction parent_res;
298+
algorithm
299+
// Copy the global flags from the current context.
300+
nodeContext := clearScopeFlags(currentContext);
301+
parent := InstNode.explicitParent(node);
302+
303+
// Records might be record constructors that should count as functions here,
304+
// such record constructors are always root classes.
305+
if not InstNode.isRootClass(parent) then
306+
nodeContext := set(nodeContext, CLASS);
307+
return;
308+
end if;
309+
310+
// Try to determine whether we're in a function or a class.
311+
parent_res := InstNode.restriction(parent);
312+
nodeContext := if Restriction.isFunction(parent_res) or Restriction.isRecord(parent_res) then
313+
set(nodeContext, FUNCTION) else set(nodeContext, CLASS);
314+
end nodeContext;
315+
283316
annotation(__OpenModelica_Interface="frontend");
284317
end NFInstContext;
285318

OMCompiler/Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,7 @@ algorithm
20172017
// The context used when typing a component node depends on where the
20182018
// component was declared, not where it's used. This can be different to
20192019
// the given context, e.g. for package constants used in a function.
2020-
node_ty := typeComponent(cref.node, crefContext(cref.node, context), typeChildren = firstPart or not InstContext.inDimension(context));
2020+
node_ty := typeComponent(cref.node, InstContext.nodeContext(cref.node, context), typeChildren = firstPart or not InstContext.inDimension(context));
20212021

20222022
(subs, subs_var) := typeSubscripts(cref.subscripts, node_ty, Expression.CREF(node_ty, cref), context, info);
20232023
(rest_cr, rest_var) := typeCref2(cref.restCref, context, info, false);
@@ -2044,31 +2044,6 @@ algorithm
20442044
end match;
20452045
end typeCref2;
20462046

2047-
function crefContext
2048-
input InstNode crefNode;
2049-
input InstContext.Type currentContext;
2050-
output InstContext.Type context;
2051-
protected
2052-
InstNode parent;
2053-
Restriction parent_res;
2054-
algorithm
2055-
parent := InstNode.explicitParent(crefNode);
2056-
2057-
context := InstContext.clearScopeFlags(currentContext);
2058-
2059-
// Records might actually be record constructors that should count as
2060-
// functions here, such record constructors are always root classes.
2061-
if not InstNode.isRootClass(parent) then
2062-
context := InstContext.set(context, NFInstContext.CLASS);
2063-
return;
2064-
end if;
2065-
2066-
parent_res := InstNode.restriction(parent);
2067-
context := if Restriction.isFunction(parent_res) or Restriction.isRecord(parent_res) then
2068-
InstContext.set(context, NFInstContext.FUNCTION) else
2069-
InstContext.set(context, NFInstContext.CLASS);
2070-
end crefContext;
2071-
20722047
function typeSubscripts
20732048
input list<Subscript> subscripts;
20742049
input Type crefType;
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// name: GetModelInstanceAnnotation13
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
//
7+
8+
loadString("
9+
package P
10+
model C
11+
parameter Boolean b = false;
12+
parameter E e = E.B;
13+
parameter Real x = 0 annotation(Dialog(enable = e == E.A));
14+
end C;
15+
16+
type E = enumeration(A, B, C);
17+
end P;
18+
19+
model M
20+
parameter Boolean b = c.b;
21+
Real x if b;
22+
replaceable model C = P.C;
23+
C c;
24+
end M;
25+
");
26+
27+
getModelInstance(M, prettyPrint=true);
28+
getErrorString();
29+
30+
// Result:
31+
// true
32+
// "{
33+
// \"name\": \"M\",
34+
// \"restriction\": \"model\",
35+
// \"elements\": [
36+
// {
37+
// \"$kind\": \"component\",
38+
// \"name\": \"b\",
39+
// \"type\": \"Boolean\",
40+
// \"modifiers\": \"c.b\",
41+
// \"value\": {
42+
// \"binding\": {
43+
// \"$kind\": \"cref\",
44+
// \"parts\": [
45+
// {
46+
// \"name\": \"c\"
47+
// },
48+
// {
49+
// \"name\": \"b\"
50+
// }
51+
// ]
52+
// },
53+
// \"value\": false
54+
// },
55+
// \"prefixes\": {
56+
// \"variability\": \"parameter\"
57+
// }
58+
// },
59+
// {
60+
// \"$kind\": \"component\",
61+
// \"name\": \"x\",
62+
// \"type\": \"Real\",
63+
// \"condition\": false
64+
// },
65+
// {
66+
// \"$kind\": \"class\",
67+
// \"name\": \"C\",
68+
// \"restriction\": \"model\",
69+
// \"prefixes\": {
70+
// \"replaceable\": true
71+
// },
72+
// \"baseClass\": \"P.C\",
73+
// \"source\": {
74+
// \"filename\": \"<interactive>\",
75+
// \"lineStart\": 15,
76+
// \"columnStart\": 17,
77+
// \"lineEnd\": 15,
78+
// \"columnEnd\": 30
79+
// }
80+
// },
81+
// {
82+
// \"$kind\": \"component\",
83+
// \"name\": \"c\",
84+
// \"type\": {
85+
// \"name\": \"M.C\",
86+
// \"restriction\": \"model\",
87+
// \"prefixes\": {
88+
// \"replaceable\": true
89+
// },
90+
// \"elements\": [
91+
// {
92+
// \"$kind\": \"extends\",
93+
// \"baseClass\": {
94+
// \"name\": \"P.C\",
95+
// \"restriction\": \"model\",
96+
// \"elements\": [
97+
// {
98+
// \"$kind\": \"component\",
99+
// \"name\": \"b\",
100+
// \"type\": \"Boolean\",
101+
// \"modifiers\": \"false\",
102+
// \"value\": {
103+
// \"binding\": false
104+
// },
105+
// \"prefixes\": {
106+
// \"variability\": \"parameter\"
107+
// }
108+
// },
109+
// {
110+
// \"$kind\": \"component\",
111+
// \"name\": \"e\",
112+
// \"type\": {
113+
// \"name\": \"P.E\",
114+
// \"restriction\": \"type\",
115+
// \"elements\": [
116+
// {
117+
// \"$kind\": \"extends\",
118+
// \"baseClass\": \"enumeration\"
119+
// },
120+
// {
121+
// \"$kind\": \"component\",
122+
// \"name\": \"A\"
123+
// },
124+
// {
125+
// \"$kind\": \"component\",
126+
// \"name\": \"B\"
127+
// },
128+
// {
129+
// \"$kind\": \"component\",
130+
// \"name\": \"C\"
131+
// }
132+
// ],
133+
// \"source\": {
134+
// \"filename\": \"<interactive>\",
135+
// \"lineStart\": 9,
136+
// \"columnStart\": 5,
137+
// \"lineEnd\": 9,
138+
// \"columnEnd\": 34
139+
// }
140+
// },
141+
// \"modifiers\": \"E.B\",
142+
// \"value\": {
143+
// \"binding\": {
144+
// \"$kind\": \"enum\",
145+
// \"name\": \"P.E.B\",
146+
// \"index\": 2
147+
// }
148+
// },
149+
// \"prefixes\": {
150+
// \"variability\": \"parameter\"
151+
// }
152+
// },
153+
// {
154+
// \"$kind\": \"component\",
155+
// \"name\": \"x\",
156+
// \"type\": \"Real\",
157+
// \"modifiers\": \"0\",
158+
// \"value\": {
159+
// \"binding\": 0
160+
// },
161+
// \"prefixes\": {
162+
// \"variability\": \"parameter\"
163+
// },
164+
// \"annotation\": {
165+
// \"Dialog\": {
166+
// \"enable\": {
167+
// \"$kind\": \"binary_op\",
168+
// \"lhs\": {
169+
// \"$kind\": \"cref\",
170+
// \"parts\": [
171+
// {
172+
// \"name\": \"c\"
173+
// },
174+
// {
175+
// \"name\": \"e\"
176+
// }
177+
// ]
178+
// },
179+
// \"op\": \"==\",
180+
// \"rhs\": {
181+
// \"$kind\": \"enum\",
182+
// \"name\": \"P.E.A\",
183+
// \"index\": 1
184+
// }
185+
// }
186+
// }
187+
// }
188+
// }
189+
// ],
190+
// \"source\": {
191+
// \"filename\": \"<interactive>\",
192+
// \"lineStart\": 3,
193+
// \"columnStart\": 5,
194+
// \"lineEnd\": 7,
195+
// \"columnEnd\": 10
196+
// }
197+
// }
198+
// }
199+
// ],
200+
// \"source\": {
201+
// \"filename\": \"<interactive>\",
202+
// \"lineStart\": 15,
203+
// \"columnStart\": 17,
204+
// \"lineEnd\": 15,
205+
// \"columnEnd\": 30
206+
// }
207+
// }
208+
// }
209+
// ],
210+
// \"source\": {
211+
// \"filename\": \"<interactive>\",
212+
// \"lineStart\": 12,
213+
// \"columnStart\": 3,
214+
// \"lineEnd\": 17,
215+
// \"columnEnd\": 8
216+
// }
217+
// }"
218+
// ""
219+
// endResult

testsuite/openmodelica/instance-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GetModelInstanceAnnotation9.mos \
1414
GetModelInstanceAnnotation10.mos \
1515
GetModelInstanceAnnotation11.mos \
1616
GetModelInstanceAnnotation12.mos \
17+
GetModelInstanceAnnotation13.mos \
1718
GetModelInstanceAttributes1.mos \
1819
GetModelInstanceAttributes2.mos \
1920
GetModelInstanceBinding1.mos \

0 commit comments

Comments
 (0)