Skip to content

Commit 87b64f8

Browse files
authored
Improve constraining modifiers on classes (#8298)
- Change the merging of modifiers on classes so that the constraining class from the replaced class has lower priority than the constraining class on the replacing class.
1 parent 0e5320a commit 87b64f8

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

OMCompiler/Compiler/NFFrontEnd/NFClass.mo

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
9393
record PARTIAL_CLASS
9494
ClassTree elements;
9595
Modifier modifier;
96+
Modifier ccMod;
9697
Prefixes prefixes;
9798
end PARTIAL_CLASS;
9899

@@ -107,13 +108,15 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
107108
record EXPANDED_CLASS
108109
ClassTree elements;
109110
Modifier modifier;
111+
Modifier ccMod;
110112
Prefixes prefixes;
111113
Restriction restriction;
112114
end EXPANDED_CLASS;
113115

114116
record EXPANDED_DERIVED
115117
InstNode baseClass;
116118
Modifier modifier;
119+
Modifier ccMod;
117120
array<Dimension> dims;
118121
Prefixes prefixes;
119122
Component.Attributes attributes;
@@ -154,7 +157,7 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
154157
ClassTree tree;
155158
algorithm
156159
tree := ClassTree.fromSCode(elements, isClassExtends, scope);
157-
cls := PARTIAL_CLASS(tree, Modifier.NOMOD(), prefixes);
160+
cls := PARTIAL_CLASS(tree, Modifier.NOMOD(), Modifier.NOMOD(), prefixes);
158161
end fromSCode;
159162

160163
function fromEnumeration
@@ -187,7 +190,7 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
187190
algorithm
188191
cls := match cls
189192
case PARTIAL_CLASS()
190-
then EXPANDED_CLASS(cls.elements, cls.modifier, cls.prefixes, Restriction.UNKNOWN());
193+
then EXPANDED_CLASS(cls.elements, cls.modifier, cls.ccMod, cls.prefixes, Restriction.UNKNOWN());
191194
end match;
192195
end initExpandedClass;
193196

@@ -331,6 +334,18 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
331334
end match;
332335
end getModifier;
333336

337+
function getCCModifier
338+
input Class cls;
339+
output Modifier modifier;
340+
algorithm
341+
modifier := match cls
342+
case PARTIAL_CLASS() then cls.ccMod;
343+
case EXPANDED_CLASS() then cls.ccMod;
344+
case EXPANDED_DERIVED() then cls.ccMod;
345+
else Modifier.NOMOD();
346+
end match;
347+
end getCCModifier;
348+
334349
function setModifier
335350
input Modifier modifier;
336351
input output Class cls;

OMCompiler/Compiler/NFFrontEnd/NFInst.mo

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ algorithm
334334
then
335335
Class.fromEnumeration(cdef.enumLst, ty, prefs, scope);
336336

337-
else Class.PARTIAL_CLASS(NFClassTree.EMPTY, Modifier.NOMOD(), prefs);
337+
else Class.PARTIAL_CLASS(NFClassTree.EMPTY, Modifier.NOMOD(), Modifier.NOMOD(), prefs);
338338
end match;
339339
end partialInstClass2;
340340

@@ -418,7 +418,7 @@ function expandClassParts
418418
protected
419419
Class cls;
420420
ClassTree cls_tree;
421-
Modifier mod;
421+
Modifier mod, cc_mod;
422422
InstNode builtin_ext;
423423
Class.Prefixes prefs;
424424
Restriction res;
@@ -428,7 +428,7 @@ algorithm
428428
cls := Class.initExpandedClass(cls);
429429
node := InstNode.updateClass(cls, node);
430430

431-
Class.EXPANDED_CLASS(elements = cls_tree, modifier = mod, prefixes = prefs) := cls;
431+
Class.EXPANDED_CLASS(elements = cls_tree, modifier = mod, ccMod = cc_mod, prefixes = prefs) := cls;
432432
builtin_ext := ClassTree.mapFoldExtends(cls_tree, expandExtends, InstNode.EMPTY_NODE());
433433

434434
if InstNode.name(builtin_ext) == "ExternalObject" then
@@ -440,7 +440,7 @@ algorithm
440440

441441
cls_tree := ClassTree.expand(cls_tree);
442442
res := Restriction.fromSCode(SCodeUtil.getClassRestriction(def));
443-
cls := Class.EXPANDED_CLASS(cls_tree, mod, prefs, res);
443+
cls := Class.EXPANDED_CLASS(cls_tree, mod, cc_mod, prefs, res);
444444
node := InstNode.updateClass(cls, node);
445445
end if;
446446
end expandClassParts;
@@ -683,7 +683,7 @@ protected
683683
SCode.Attributes sattrs;
684684
Component.Attributes attrs;
685685
list<Dimension> dims;
686-
Modifier mod;
686+
Modifier mod, cc_mod;
687687
Restriction res;
688688
algorithm
689689
SCode.DERIVED(typeSpec = ty, attributes = sattrs) := definition;
@@ -713,9 +713,10 @@ algorithm
713713
attrs := instDerivedAttributes(sattrs);
714714
dims := list(Dimension.RAW_DIM(d, InstNode.parent(node)) for d in AbsynUtil.typeSpecDimensions(ty));
715715
mod := Class.getModifier(cls);
716+
cc_mod := Class.getCCModifier(cls);
716717

717718
res := Restriction.fromSCode(SCodeUtil.getClassRestriction(element));
718-
cls := Class.EXPANDED_DERIVED(ext_node, mod, listArray(dims), prefs, attrs, res);
719+
cls := Class.EXPANDED_DERIVED(ext_node, mod, cc_mod, listArray(dims), prefs, attrs, res);
719720
node := InstNode.updateClass(cls, node);
720721
end expandClassDerived;
721722

@@ -809,6 +810,7 @@ algorithm
809810
// Fetch modification on the class definition (for class extends).
810811
mod := instElementModifier(InstNode.definition(node), node, par);
811812
mod := Modifier.propagate(mod, node, par);
813+
mod := Modifier.merge(mod, cls.ccMod);
812814
// Merge with any outer modifications.
813815
outer_mod := Modifier.propagate(cls.modifier, node, par);
814816
outer_mod := Modifier.merge(outerMod, outer_mod);
@@ -857,6 +859,7 @@ algorithm
857859
// Merge outer modifiers and attributes.
858860
mod := instElementModifier(InstNode.definition(node), node, InstNode.rootParent(node));
859861
mod := Modifier.propagate(mod, node, par);
862+
mod := Modifier.merge(mod, cls.ccMod);
860863
outer_mod := Modifier.propagate(cls.modifier, node, par);
861864
outer_mod := Modifier.merge(outerMod, outer_mod);
862865
mod := Modifier.merge(outer_mod, mod);
@@ -1314,7 +1317,7 @@ algorithm
13141317
rdcl_cls := InstNode.getClass(redeclareNode);
13151318

13161319
mod := Class.getModifier(rdcl_cls);
1317-
mod := Modifier.merge(mod, constrainingMod);
1320+
//mod := Modifier.merge(mod, constrainingMod);
13181321
mod := Modifier.merge(outerMod, mod);
13191322

13201323
prefs := mergeRedeclaredClassPrefixes(Class.getPrefixes(orig_cls),
@@ -1346,6 +1349,7 @@ algorithm
13461349
orig_node := InstNode.setNodeType(node_ty, orig_node);
13471350
rdcl_cls.elements := ClassTree.setClassExtends(orig_node, rdcl_cls.elements);
13481351
rdcl_cls.modifier := mod;
1352+
rdcl_cls.ccMod := constrainingMod;
13491353
rdcl_cls.prefixes := prefs;
13501354
then
13511355
rdcl_cls;
@@ -1365,6 +1369,7 @@ algorithm
13651369
algorithm
13661370
rdcl_cls.prefixes := prefs;
13671371
rdcl_cls.modifier := mod;
1372+
rdcl_cls.ccMod := constrainingMod;
13681373
then
13691374
rdcl_cls;
13701375

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// name: ConstrainingClassFunc2
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
7+
function f
8+
input Real x;
9+
output Real y;
10+
end f;
11+
12+
model A
13+
replaceable function f2 = f(x = 0);
14+
15+
Real x = f2();
16+
end A;
17+
18+
model ConstrainingClassFunc2
19+
A a(redeclare function f2 = f(x = 1));
20+
end ConstrainingClassFunc2;
21+
22+
// Result:
23+
// function ConstrainingClassFunc2.a.f2
24+
// input Real x = 1.0;
25+
// output Real y;
26+
// end ConstrainingClassFunc2.a.f2;
27+
//
28+
// class ConstrainingClassFunc2
29+
// Real a.x = ConstrainingClassFunc2.a.f2(1.0);
30+
// end ConstrainingClassFunc2;
31+
// endResult

testsuite/flattening/modelica/scodeinst/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ ConstrainingClass1.mo \
306306
ConstrainingClass2.mo \
307307
ConstrainingClass3.mo \
308308
ConstrainingClassFunc1.mo \
309+
ConstrainingClassFunc2.mo \
309310
DimCyclic1.mo \
310311
DimCyclic2.mo \
311312
DimCyclic3.mo \

0 commit comments

Comments
 (0)