Skip to content

Commit

Permalink
Don't instantiate outer components (#10919)
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Jul 3, 2023
1 parent e015287 commit 91dba94
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 29 deletions.
57 changes: 31 additions & 26 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1443,18 +1443,16 @@ algorithm
fail();
end if;

if InstNode.isOnlyOuter(node) then
Error.addSourceMessage(Error.OUTER_ELEMENT_MOD,
{Modifier.toString(mod, printName = false), Modifier.name(mod)},
Modifier.info(mod));
fail();
end if;

if InstNode.isComponent(node) then
InstNode.componentApply(node, Component.mergeModifier, mod);
else
if InstNode.isOnlyOuter(node) then
// Modifying an outer class is illegal. We can't check that in instClass
// since we get the inner class there, so we check it here instead.
Error.addSourceMessage(Error.OUTER_ELEMENT_MOD,
{Modifier.toString(mod, printName = false), Modifier.name(mod)},
Modifier.info(mod));
fail();
end if;

partialInstClass(node);
node := InstNode.replaceClass(Class.mergeModifier(mod, InstNode.getClass(node)), node);
node := InstNode.clearPackageCache(node);
Expand Down Expand Up @@ -1715,7 +1713,8 @@ protected
InstContext.Type next_context;
list<Subscript> propagated_subs;
algorithm
comp_node := InstNode.resolveOuter(node);
checkOuterComponentMod(node);
comp_node := InstNode.resolveInner(node);
comp := InstNode.component(comp_node);
parent := InstNode.parent(comp_node);

Expand All @@ -1729,8 +1728,6 @@ algorithm
Component.COMPONENT_DEF(definition = def, modifier = outer_mod) := comp;

if Modifier.isRedeclare(outer_mod) then
checkOuterComponentMod(outer_mod, def, comp_node);

Modifier.REDECLARE(element = rdcl_node, innerMod = inner_mod,
outerMod = outer_mod, constrainingMod = cc_mod, propagatedSubs = propagated_subs) := outer_mod;

Expand Down Expand Up @@ -1785,7 +1782,6 @@ algorithm

mod := Modifier.merge(mod, innerMod);
mod := Modifier.merge(outerMod, mod);
checkOuterComponentMod(mod, component, node);

dims := list(Dimension.RAW_DIM(d, parent) for d in component.attributes.arrayDims);
binding := if useBinding then Modifier.binding(mod) else NFBinding.EMPTY_BINDING;
Expand Down Expand Up @@ -2053,17 +2049,24 @@ algorithm
end redeclareComponent;

function checkOuterComponentMod
"Prints an error message and fails if it gets an outer component and a
non-empty modifier."
input Modifier mod;
input SCode.Element component;
"Prints an error message and fails if it gets an outer component with a modifier."
input InstNode node;
protected
InstNode outer_node;
SCode.Element elem;
SCode.Mod smod;
algorithm
if not Modifier.isEmpty(mod) and
AbsynUtil.isOnlyOuter(SCodeUtil.prefixesInnerOuter(SCodeUtil.elementPrefixes(component))) then
Error.addSourceMessage(Error.OUTER_ELEMENT_MOD,
{Modifier.toString(mod, printName = false), InstNode.name(node)}, InstNode.info(node));
fail();
outer_node := InstNode.resolveOuter(node);
elem := InstNode.definition(outer_node);

if AbsynUtil.isOnlyOuter(SCodeUtil.prefixesInnerOuter(SCodeUtil.elementPrefixes(elem))) then
smod := SCodeUtil.componentMod(elem);

if not SCodeUtil.isEmptyMod(smod) then
Error.addSourceMessage(Error.OUTER_ELEMENT_MOD,
{SCodeDump.printModStr(smod), InstNode.name(outer_node)}, InstNode.info(outer_node));
fail();
end if;
end if;
end checkOuterComponentMod;

Expand Down Expand Up @@ -2399,14 +2402,19 @@ function instComponentExpressions
input InstNode component;
input InstContext.Type context;
protected
InstNode node = InstNode.resolveOuter(component);
InstNode node = InstNode.resolveInner(component);
Component c = InstNode.component(node);
array<Dimension> dims;
algorithm
() := match c
case Component.COMPONENT(ty = Type.UNTYPED(dimensions = dims))
guard c.state == ComponentState.PartiallyInstantiated
algorithm
// This is to avoid instantiating the same component multiple times,
// which can otherwise happen with duplicate components at this stage.
c.state := ComponentState.FullyInstantiated;
InstNode.updateComponent(c, node);

c.binding := instBinding(c.binding, context);
c.condition := instBinding(c.condition, context);

Expand All @@ -2418,9 +2426,6 @@ algorithm
dims[i] := instDimension(dims[i], context, c.info);
end for;

// This is to avoid instantiating the same component multiple times,
// which can otherwise happen with duplicate components at this stage.
c.state := ComponentState.FullyInstantiated;
InstNode.updateComponent(c, node);
then
();
Expand Down
24 changes: 24 additions & 0 deletions testsuite/flattening/modelica/scodeinst/InnerOuter12.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// name: InnerOuter12
// keywords:
// status: correct
// cflags: -d=newInst
//

model A
outer B b;
Real x;
end A;

model B
A a;
end B;

model InnerOuter12
inner B b;
end InnerOuter12;

// Result:
// class InnerOuter12
// Real b.a.x;
// end InnerOuter12;
// endResult
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end InnerOuterInvalidMod1;

// Result:
// Error processing file: InnerOuterInvalidMod1.mo
// [flattening/modelica/scodeinst/InnerOuterInvalidMod1.mo:8:3-8:21:writable] Error: Modifier '= 1.0' found on outer element x.
// [flattening/modelica/scodeinst/InnerOuterInvalidMod1.mo:8:3-8:21:writable] Error: Modifier ' = 1.0' found on outer element x.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end InnerOuterInvalidMod2;

// Result:
// Error processing file: InnerOuterInvalidMod2.mo
// [flattening/modelica/scodeinst/InnerOuterInvalidMod2.mo:8:3-8:15:writable] Error: Modifier '= 1.0' found on outer element x.
// [flattening/modelica/scodeinst/InnerOuterInvalidMod2.mo:13:7-13:14:writable] Error: Modifier '= 1.0' found on outer element x.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end InnerOuterInvalidMod3;

// Result:
// Error processing file: InnerOuterInvalidMod3.mo
// [flattening/modelica/scodeinst/InnerOuterInvalidMod3.mo:8:3-8:27:writable] Error: Modifier 'redeclare Real x = 1.0' found on outer element x.
// [flattening/modelica/scodeinst/InnerOuterInvalidMod3.mo:13:7-13:29:writable] Error: Modifier 'redeclare Real x = 1.0' found on outer element x.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ InnerOuter8.mo \
InnerOuter9.mo \
InnerOuter10.mo \
InnerOuter11.mo \
InnerOuter12.mo \
InnerOuterClass1.mo \
InnerOuterDuplicate1.mo \
InnerOuterInvalidMod1.mo \
Expand Down

0 comments on commit 91dba94

Please sign in to comment.