Skip to content

Commit

Permalink
Dump original bindings in getModelInstance (#10419)
Browse files Browse the repository at this point in the history
- Keep references to the original bindings for components in
  getModelInstance, so they can be dumped also for components whose
  bindings are evaluated and overwritten by the frontend.

Fixes #10188
  • Loading branch information
perost committed Mar 20, 2023
1 parent 4101671 commit 379daf2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -794,5 +794,17 @@ public
binding := FLAT_BINDING(exp, var, source);
end makeFlat;

function isEvaluated
input Binding binding;
output Boolean evaluated;
algorithm
evaluated := match binding
case TYPED_BINDING()
then Mutable.access(binding.evalState) == EvalState.EVALUATED;
case CEVAL_BINDING() then true;
else false;
end match;
end isEvaluated;

annotation(__OpenModelica_Interface="frontend");
end NFBinding;
38 changes: 32 additions & 6 deletions OMCompiler/Compiler/Script/NFApi.mo
Expand Up @@ -836,6 +836,7 @@ end getInheritedClasses;
uniontype InstanceTree
record COMPONENT
InstNode node;
Option<Binding> binding;
InstanceTree cls;
end COMPONENT;

Expand Down Expand Up @@ -1035,19 +1036,29 @@ function buildInstanceTreeComponent
input Mutable<InstNode> compNode;
output InstanceTree tree;
protected
InstNode node, cls_node;
InstNode node, inner_node, cls_node;
InstanceTree cls;
Binding binding;
Option<Binding> opt_binding;
algorithm
node := Mutable.access(compNode);
cls_node := InstNode.classScope(InstNode.resolveInner(node));
inner_node := InstNode.resolveInner(node);
cls_node := InstNode.classScope(inner_node);

if InstNode.isEmpty(cls_node) then
cls := InstanceTree.EMPTY();
else
cls := buildInstanceTree(cls_node);
end if;

tree := InstanceTree.COMPONENT(node, cls);
if InstNode.isComponent(inner_node) then
binding := Component.getBinding(InstNode.component(inner_node));
opt_binding := if Binding.isBound(binding) then SOME(binding) else NONE();
else
opt_binding := NONE();
end if;

tree := InstanceTree.COMPONENT(node, opt_binding, cls);
end buildInstanceTreeComponent;

function dumpJSONInstanceTree
Expand Down Expand Up @@ -1189,7 +1200,7 @@ algorithm
j := match e
case InstanceTree.CLASS(isExtends = true) then dumpJSONExtends(e, isDeleted);
case InstanceTree.CLASS() then dumpJSONReplaceableClass(e.node, scope);
case InstanceTree.COMPONENT() then dumpJSONComponent(e.node, e.cls);
case InstanceTree.COMPONENT() then dumpJSONComponent(e.node, e.binding, e.cls);
else JSON.makeNull();
end match;

Expand Down Expand Up @@ -1275,6 +1286,7 @@ end dumpJSONReplaceableClass;

function dumpJSONComponent
input InstNode component;
input Option<Binding> originalBinding;
input InstanceTree cls;
output JSON json = JSON.makeNull();
protected
Expand Down Expand Up @@ -1326,7 +1338,7 @@ algorithm

is_constant := comp.attributes.variability <= Variability.PARAMETER;
if Binding.isExplicitlyBound(comp.binding) then
json := JSON.addPair("value", dumpJSONBinding(comp.binding, evaluate = is_constant), json);
json := JSON.addPair("value", dumpJSONBinding(comp.binding, originalBinding, evaluate = is_constant), json);
end if;

if Binding.isBound(comp.condition) then
Expand Down Expand Up @@ -1429,12 +1441,26 @@ end dumpJSONTypeName;

function dumpJSONBinding
input Binding binding;
input Option<Binding> originalBinding = NONE();
input Boolean evaluate = true;
output JSON json = JSON.makeNull();
protected
Expression exp;
Binding bind = binding;
algorithm
exp := Binding.getExp(binding);
// If the binding has been evaluated by the frontend, try to use the original
// binding that we saved when building the instance tree instead.
if isSome(originalBinding) and Binding.isEvaluated(binding) then
try
// Instantiating the binding should be enough, we don't really care about
// what the type of it is.
bind := Inst.instBinding(Util.getOption(originalBinding),
InstContext.set(NFInstContext.RELAXED, NFInstContext.INSTANCE_API));
else
end try;
end if;

exp := Binding.getExp(bind);
exp := Expression.map(exp, Expression.expandSplitIndices);
json := JSON.addPair("binding", Expression.toJSON(exp), json);

Expand Down

0 comments on commit 379daf2

Please sign in to comment.