Skip to content

Commit

Permalink
[NF] Clone inherited classes.
Browse files Browse the repository at this point in the history
- Clone inherited classes so that they get unique caches, to prevent e.g.
  the same record constructor instance from being shared between classes
  where the record has different modifiers or use package constants with
  different bindings.

Belonging to [master]:
  - OpenModelica/OMCompiler#2803
  - OpenModelica/OpenModelica-testsuite#1082
  • Loading branch information
perost authored and OpenModelica-Hudson committed Nov 28, 2018
1 parent 96dfd76 commit 7f7fb53
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -284,6 +284,24 @@ uniontype Class
end match;
end setClassTree;

function classTreeApply
input output Class cls;
input FuncType func;

partial function FuncType
input output ClassTree tree;
end FuncType;
algorithm
() := match cls
case Class.PARTIAL_CLASS() algorithm cls.elements := func(cls.elements); then ();
case Class.EXPANDED_CLASS() algorithm cls.elements := func(cls.elements); then ();
case Class.PARTIAL_BUILTIN() algorithm cls.elements := func(cls.elements); then ();
case Class.INSTANCED_CLASS() algorithm cls.elements := func(cls.elements); then ();
case Class.INSTANCED_BUILTIN() algorithm cls.elements := func(cls.elements); then ();
else ();
end match;
end classTreeApply;

function getModifier
input Class cls;
output Modifier modifier;
Expand Down
19 changes: 19 additions & 0 deletions Compiler/NFFrontEnd/NFClassTree.mo
Expand Up @@ -704,6 +704,25 @@ public
tree := FLAT_TREE(ltree, listArray({}), comps, listArray({}), DuplicateTree.new());
end fromRecordConstructor;

function clone
input ClassTree tree;
output ClassTree outTree;
algorithm
outTree := match tree
local
array<InstNode> clss;

case EXPANDED_TREE()
algorithm
clss := arrayCopy(tree.classes);
clss := Array.mapNoCopy(clss, InstNode.clone);
then
EXPANDED_TREE(tree.tree, clss, tree.components, tree.exts, tree.imports, tree.duplicates);

else tree;
end match;
end clone;

function mapRedeclareChains
input ClassTree tree;
input FuncT func;
Expand Down
2 changes: 2 additions & 0 deletions Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -431,6 +431,7 @@ algorithm
checkExtendsLoop(base_node, base_path, info);
checkReplaceableBaseClass(base_nodes, base_path, info);
base_node := expand(base_node);
base_node := InstNode.clone(base_node);

ext := InstNode.setNodeType(InstNodeType.BASE_CLASS(scope, def), base_node);

Expand Down Expand Up @@ -656,6 +657,7 @@ algorithm
end if;

ext_node := expand(ext_node);
ext_node := InstNode.clone(ext_node);

// Fetch the needed information from the class definition and construct a EXPANDED_DERIVED.
cls := InstNode.getClass(node);
Expand Down
22 changes: 22 additions & 0 deletions Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -50,6 +50,7 @@ protected
import List;
import ConvertDAE = NFConvertDAE;
import Restriction = NFRestriction;
import NFClassTree.ClassTree;

public
uniontype InstNodeType
Expand Down Expand Up @@ -1454,6 +1455,27 @@ uniontype InstNode
else false;
end match;
end isPartial;

function clone
input output InstNode node;
algorithm
() := match node
local
Class cls;

case CLASS_NODE()
algorithm
cls := Pointer.access(node.cls);
cls := Class.classTreeApply(cls, ClassTree.clone);
node.cls := Pointer.create(cls);
node.caches := CachedData.empty();
then
();

else ();
end match;
end clone;

end InstNode;

annotation(__OpenModelica_Interface="frontend");
Expand Down

0 comments on commit 7f7fb53

Please sign in to comment.