Skip to content

Commit 6db8c7b

Browse files
authored
Dump the type name of redeclares in getModelInstance (#10351)
Fixes #10348
1 parent 1b96218 commit 6db8c7b

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ algorithm
10371037
json := JSON.addPairNotNull("dims", dumpJSONClassDims(node, def), json);
10381038
json := JSON.addPair("restriction",
10391039
JSON.makeString(Restriction.toString(InstNode.restriction(node))), json);
1040-
json := dumpJSONSCodeMod(SCodeUtil.elementMod(def), json);
1040+
json := dumpJSONSCodeMod(SCodeUtil.elementMod(def), scope, json);
10411041

10421042
json := JSON.addPairNotNull("prefixes", dumpJSONClassPrefixes(def, node), json);
10431043

@@ -1174,7 +1174,7 @@ algorithm
11741174
ext_def := InstNode.extendsDefinition(node);
11751175

11761176
json := JSON.addPair("$kind", JSON.makeString("extends"), json);
1177-
json := dumpJSONSCodeMod(SCodeUtil.elementMod(ext_def), json);
1177+
json := dumpJSONSCodeMod(SCodeUtil.elementMod(ext_def), node, json);
11781178
json := dumpJSONCommentOpt(SCodeUtil.getElementComment(ext_def), node, json);
11791179

11801180
if Class.isOnlyBuiltin(InstNode.getClass(node)) then
@@ -1218,13 +1218,13 @@ algorithm
12181218
json := JSON.addPairNotNull("dims", dumpJSONDims(Util.getOption(odims), {}), json);
12191219
end if;
12201220

1221-
json := dumpJSONSCodeMod(cdef.modifications, json);
1221+
json := dumpJSONSCodeMod(cdef.modifications, scope, json);
12221222
then
12231223
();
12241224

12251225
case SCode.ClassDef.CLASS_EXTENDS()
12261226
algorithm
1227-
json := dumpJSONSCodeMod(cdef.modifications, json);
1227+
json := dumpJSONSCodeMod(cdef.modifications, scope, json);
12281228
then
12291229
();
12301230

@@ -1267,7 +1267,7 @@ algorithm
12671267
json := JSON.addPair("$kind", JSON.makeString("component"), json);
12681268
json := JSON.addPair("name", JSON.makeString(InstNode.name(node)), json);
12691269
json := JSON.addPair("type", dumpJSONComponentType(cls, node, comp.ty, isDeleted = true), json);
1270-
json := dumpJSONSCodeMod(elem.modifications, json);
1270+
json := dumpJSONSCodeMod(elem.modifications, scope, json);
12711271
json := JSON.addPair("condition", JSON.makeBoolean(false), json);
12721272
json := JSON.addPairNotNull("prefixes", dumpJSONAttributes(elem.attributes, elem.prefixes, scope), json);
12731273
json := dumpJSONCommentOpt(SOME(elem.comment), scope, json);
@@ -1285,7 +1285,7 @@ algorithm
12851285
dumpJSONDims(elem.attributes.arrayDims, Type.arrayDims(comp.ty)), json);
12861286
end if;
12871287

1288-
json := dumpJSONSCodeMod(elem.modifications, json);
1288+
json := dumpJSONSCodeMod(elem.modifications, scope, json);
12891289

12901290
is_constant := comp.attributes.variability <= Variability.PARAMETER;
12911291
if Binding.isExplicitlyBound(comp.binding) then
@@ -1540,7 +1540,7 @@ algorithm
15401540
algorithm
15411541
json := JSON.makeNull();
15421542
json := JSON.addPair("constrainedby", dumpJSONPath(cc.constrainingClass), json);
1543-
json := dumpJSONSCodeMod(cc.modifier, json);
1543+
json := dumpJSONSCodeMod(cc.modifier, scope, json);
15441544
json := dumpJSONCommentOpt(SOME(cc.comment), scope, json);
15451545
then
15461546
json;
@@ -1941,16 +1941,18 @@ end dumpJSONReplaceableElements;
19411941

19421942
function dumpJSONSCodeMod
19431943
input SCode.Mod mod;
1944+
input InstNode scope;
19441945
input output JSON json;
19451946
protected
19461947
JSON j;
19471948
algorithm
1948-
j := dumpJSONSCodeMod_impl(mod);
1949+
j := dumpJSONSCodeMod_impl(mod, scope);
19491950
json := JSON.addPairNotNull("modifiers", j, json);
19501951
end dumpJSONSCodeMod;
19511952

19521953
function dumpJSONSCodeMod_impl
19531954
input SCode.Mod mod;
1955+
input InstNode scope;
19541956
output JSON json = JSON.makeNull();
19551957
protected
19561958
JSON binding_json;
@@ -1959,7 +1961,7 @@ algorithm
19591961
case SCode.Mod.MOD()
19601962
algorithm
19611963
for m in mod.subModLst loop
1962-
json := JSON.addPair(m.ident, dumpJSONSCodeMod_impl(m.mod), json);
1964+
json := JSON.addPair(m.ident, dumpJSONSCodeMod_impl(m.mod, scope), json);
19631965
end for;
19641966

19651967
if SCodeUtil.finalBool(mod.finalPrefix) then
@@ -1994,13 +1996,37 @@ algorithm
19941996

19951997
binding_json := JSON.makeString(SCodeDump.unparseElementStr(mod.element));
19961998
json := JSON.addPair("$value", binding_json, json);
1999+
json := dumpJSONRedeclareType(mod.element, scope, json);
19972000
then
19982001
();
19992002

20002003
else ();
20012004
end match;
20022005
end dumpJSONSCodeMod_impl;
20032006

2007+
function dumpJSONRedeclareType
2008+
input SCode.Element element;
2009+
input InstNode scope;
2010+
input output JSON json;
2011+
protected
2012+
Absyn.Path path;
2013+
InstContext.Type context;
2014+
InstNode cls;
2015+
algorithm
2016+
() := matchcontinue element
2017+
case SCode.Element.COMPONENT()
2018+
algorithm
2019+
path := AbsynUtil.typeSpecPath(element.typeSpec);
2020+
context := InstContext.set(NFInstContext.RELAXED, NFInstContext.FAST_LOOKUP);
2021+
cls := Lookup.lookupName(path, scope, context, checkAccessViolations = false);
2022+
json := JSON.addPair("$type", dumpJSONNodePath(cls), json);
2023+
then
2024+
();
2025+
2026+
else ();
2027+
end matchcontinue;
2028+
end dumpJSONRedeclareType;
2029+
20042030
function dumpJSONChoicesAnnotation
20052031
input list<SCode.SubMod> mods;
20062032
input InstNode scope;
@@ -2025,7 +2051,7 @@ algorithm
20252051
else m;
20262052
end match;
20272053

2028-
j := JSON.addElement(dumpJSONSCodeMod_impl(m.mod), j);
2054+
j := JSON.addElement(dumpJSONSCodeMod_impl(m.mod, scope), j);
20292055
end for;
20302056

20312057
json := JSON.addPair("choice", j, json);

doc/instanceAPI/getModelInstance.schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@
419419
"type": "string",
420420
"description": "The string representation of the binding equation"
421421
},
422+
"$type": {
423+
"type": "string",
424+
"description": "The fully qualified type name of a redeclare element"
425+
}
422426
"final": {
423427
"type": "boolean",
424428
"description": "Whether the modifier is final or not"

testsuite/openmodelica/instance-API/GetModelInstanceChoices2.mos

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@
66
//
77

88
loadString("
9-
model M
10-
replaceable Real x annotation(
11-
choices(
12-
choice(redeclare Real x = 1.0 \"A\"),
13-
choice(redeclare Real x = 2.0 \"B\")));
14-
end M;
9+
package P
10+
package P2
11+
type OtherReal = Real;
12+
end P2;
13+
14+
model M
15+
replaceable Real x annotation(
16+
choices(
17+
choice(redeclare Real x = 1.0 \"A\"),
18+
choice(redeclare Real x = 2.0 \"B\"),
19+
choice(redeclare P2.OtherReal x = 3.0 \"C\"),
20+
choice(redeclare MissingReal x = 4.0 \"D\")));
21+
end M;
22+
end P;
1523
");
1624

17-
getModelInstance(M, prettyPrint=true);
25+
getModelInstance(P.M, prettyPrint=true);
1826
getErrorString();
1927

2028
// Result:
2129
// true
2230
// "{
23-
// \"name\": \"M\",
31+
// \"name\": \"P.M\",
2432
// \"restriction\": \"model\",
2533
// \"elements\": [
2634
// {
@@ -34,10 +42,19 @@ getErrorString();
3442
// \"choices\": {
3543
// \"choice\": [
3644
// {
37-
// \"$value\": \"redeclare Real x = 1.0 \\\"A\\\"\"
45+
// \"$value\": \"redeclare Real x = 1.0 \\\"A\\\"\",
46+
// \"$type\": \"Real\"
47+
// },
48+
// {
49+
// \"$value\": \"redeclare Real x = 2.0 \\\"B\\\"\",
50+
// \"$type\": \"Real\"
51+
// },
52+
// {
53+
// \"$value\": \"redeclare P2.OtherReal x = 3.0 \\\"C\\\"\",
54+
// \"$type\": \"P.P2.OtherReal\"
3855
// },
3956
// {
40-
// \"$value\": \"redeclare Real x = 2.0 \\\"B\\\"\"
57+
// \"$value\": \"redeclare MissingReal x = 4.0 \\\"D\\\"\"
4158
// }
4259
// ]
4360
// }
@@ -46,10 +63,10 @@ getErrorString();
4663
// ],
4764
// \"source\": {
4865
// \"filename\": \"<interactive>\",
49-
// \"lineStart\": 2,
50-
// \"columnStart\": 3,
51-
// \"lineEnd\": 7,
52-
// \"columnEnd\": 8
66+
// \"lineStart\": 7,
67+
// \"columnStart\": 5,
68+
// \"lineEnd\": 14,
69+
// \"columnEnd\": 10
5370
// }
5471
// }"
5572
// ""

0 commit comments

Comments
 (0)