Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 0c97493

Browse files
perostOpenModelica-Hudson
authored andcommitted
NFInst improvements.
- Improved handling of class modifiers. - Improved merging of redeclares with modifiers. - Implemented basic type matching for complex types.
1 parent c40d463 commit 0c97493

File tree

6 files changed

+134
-8
lines changed

6 files changed

+134
-8
lines changed

Compiler/NFFrontEnd/NFClassTree.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,17 @@ public
658658
element := resolveEntry(entry, tree);
659659
end lookupElement;
660660

661+
function lookupElementPtr
662+
input String name;
663+
input ClassTree tree;
664+
output Mutable<InstNode> element;
665+
protected
666+
LookupTree.Entry entry;
667+
algorithm
668+
entry := LookupTree.get(lookupTree(tree), name);
669+
element := resolveEntryPtr(entry, tree);
670+
end lookupElementPtr;
671+
661672
function applyExtends
662673
input ClassTree tree;
663674
input FuncT func;

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ algorithm
532532
end if;
533533

534534
redecl_node := expand(cls_mod.element);
535-
node := instClass(redecl_node, Modifier.NOMOD(), parent);
535+
node := instClass(redecl_node, cls_mod.mod, parent);
536536
then
537537
();
538538

@@ -738,6 +738,7 @@ function applyModifier
738738
input String clsName;
739739
protected
740740
list<Modifier> mods;
741+
Mutable<InstNode> node_ptr;
741742
InstNode node;
742743
Component comp;
743744
algorithm
@@ -749,18 +750,21 @@ algorithm
749750

750751
for mod in mods loop
751752
try
752-
node := ClassTree.lookupElement(Modifier.name(mod), cls);
753+
node_ptr := ClassTree.lookupElementPtr(Modifier.name(mod), cls);
753754
else
754755
Error.addSourceMessage(Error.MISSING_MODIFIED_ELEMENT,
755756
{Modifier.name(mod), clsName}, Modifier.info(mod));
756757
fail();
757758
end try;
758759

760+
node := Mutable.access(node_ptr);
759761
if InstNode.isComponent(node) then
760762
InstNode.componentApply(node, Component.mergeModifier, mod);
761763
else
762764
partialInstClass(node);
763-
InstNode.classApply(node, Class.mergeModifier, mod);
765+
node := InstNode.replaceClass(Class.mergeModifier(mod, InstNode.getClass(node)), node);
766+
node := InstNode.resetCache(node);
767+
Mutable.update(node_ptr, node);
764768
end if;
765769
end for;
766770

@@ -785,16 +789,19 @@ algorithm
785789
mod := Component.getModifier(comp);
786790

787791
() := match (mod, comp)
788-
case (Modifier.REDECLARE(), _)
792+
case (Modifier.REDECLARE(), Component.COMPONENT_DEF(definition = def))
789793
algorithm
790794
if not InstNode.isComponent(mod.element) then
791795
Error.addMultiSourceMessage(Error.INVALID_REDECLARE_AS,
792796
{"component", InstNode.name(node), "class"},
793797
{InstNode.info(mod.element), InstNode.info(node)});
794798
end if;
795799

796-
comp := InstNode.component(mod.element);
797-
InstNode.updateComponent(comp, node);
800+
comp_mod := Modifier.fromElement(def, parent);
801+
comp_mod := Modifier.merge(comp_mod, mod.mod);
802+
inst_comp := InstNode.component(mod.element);
803+
inst_comp := Component.setModifier(comp_mod, inst_comp);
804+
InstNode.updateComponent(inst_comp, node);
798805
instComponent(node, parent, InstNode.parent(mod.element));
799806
then
800807
();

Compiler/NFFrontEnd/NFInstNode.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,18 @@ uniontype InstNode
686686
end match;
687687
end setCachedData;
688688

689+
function resetCache
690+
input output InstNode node;
691+
algorithm
692+
() := match node
693+
case CLASS_NODE()
694+
algorithm
695+
node.cached := CachedData.empty();
696+
then
697+
();
698+
end match;
699+
end resetCache;
700+
689701
function cacheAddFunc
690702
input Function fn;
691703
input Boolean specialBuiltin;

Compiler/NFFrontEnd/NFMod.mo

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ uniontype Modifier
130130
SCode.Final finalPrefix;
131131
SCode.Each eachPrefix;
132132
InstNode element;
133+
Modifier mod;
133134
end REDECLARE;
134135

135136
record NOMOD end NOMOD;
@@ -160,7 +161,7 @@ public
160161
MODIFIER(name, mod.finalPrefix, mod.eachPrefix, binding, submod_table, mod.info);
161162

162163
case SCode.REDECL()
163-
then REDECLARE(mod.finalPrefix, mod.eachPrefix, InstNode.new(mod.element, scope));
164+
then REDECLARE(mod.finalPrefix, mod.eachPrefix, InstNode.new(mod.element, scope), NOMOD());
164165

165166
end match;
166167
end create;
@@ -271,6 +272,18 @@ public
271272
then
272273
MODIFIER(outerMod.name, outerMod.finalPrefix, outerMod.eachPrefix, binding, submods, outerMod.info);
273274

275+
case (REDECLARE(), MODIFIER())
276+
algorithm
277+
outerMod.mod := merge(outerMod.mod, innerMod);
278+
then
279+
outerMod;
280+
281+
case (MODIFIER(), REDECLARE())
282+
algorithm
283+
innerMod.mod := merge(outerMod, innerMod.mod);
284+
then
285+
innerMod;
286+
274287
case (REDECLARE(), _) then outerMod;
275288
case (_, REDECLARE()) then innerMod;
276289

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import List;
5353
import Types;
5454
import Operator = NFOperator;
5555
import Type = NFType;
56+
import Class = NFClass.Class;
57+
import ClassTree = NFClassTree;
5658

5759
public
5860

@@ -1376,6 +1378,7 @@ algorithm
13761378
Dimension dim1, dim2;
13771379
Type ety1, ety2;
13781380
Boolean compat;
1381+
InstNode cls;
13791382

13801383
case Type.INTEGER() then actualType;
13811384
case Type.REAL() then actualType;
@@ -1399,7 +1402,8 @@ algorithm
13991402

14001403
case Type.COMPLEX()
14011404
algorithm
1402-
assert(false, getInstanceName() + " IMPLEMENT ME.");
1405+
(expression, compatibleType, matchKind) :=
1406+
matchComplexTypes(actualType, expectedType, expression, allowUnknown);
14031407
then
14041408
actualType;
14051409

@@ -1412,6 +1416,84 @@ algorithm
14121416
end match;
14131417
end matchTypes;
14141418

1419+
function matchComplexTypes
1420+
input Type actualType;
1421+
input Type expectedType;
1422+
input output Expression expression;
1423+
input Boolean allowUnknown;
1424+
output Type compatibleType = actualType;
1425+
output MatchKind matchKind = MatchKind.EXACT;
1426+
protected
1427+
Class cls1, cls2;
1428+
InstNode anode, enode;
1429+
array<InstNode> comps1, comps2;
1430+
Absyn.Path path;
1431+
Type ty;
1432+
Expression e;
1433+
list<Expression> elements, matched_elements = {};
1434+
MatchKind mk;
1435+
algorithm
1436+
Type.COMPLEX(cls = anode) := actualType;
1437+
Type.COMPLEX(cls = enode) := expectedType;
1438+
cls1 := InstNode.getClass(anode);
1439+
cls2 := InstNode.getClass(enode);
1440+
1441+
() := match (cls1, cls2, expression)
1442+
case (Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps1)),
1443+
Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps2)),
1444+
Expression.RECORD(elements = elements))
1445+
algorithm
1446+
if arrayLength(comps1) <> arrayLength(comps2) or
1447+
arrayLength(comps1) <> listLength(elements) then
1448+
matchKind := MatchKind.NOT_COMPATIBLE;
1449+
else
1450+
for i in 1:arrayLength(comps1) loop
1451+
e :: elements := elements;
1452+
(e, _, mk) := matchTypes(InstNode.getType(comps1[i]), InstNode.getType(comps2[i]), e, allowUnknown);
1453+
matched_elements := e :: matched_elements;
1454+
1455+
if mk == MatchKind.CAST then
1456+
matchKind := mk;
1457+
elseif mk <> MatchKind.EXACT then
1458+
matchKind := MatchKind.NOT_COMPATIBLE;
1459+
break;
1460+
end if;
1461+
end for;
1462+
1463+
if matchKind == MatchKind.CAST then
1464+
expression.elements := listReverse(matched_elements);
1465+
end if;
1466+
end if;
1467+
then
1468+
();
1469+
1470+
case (Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps1)),
1471+
Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps2)), _)
1472+
algorithm
1473+
if arrayLength(comps1) <> arrayLength(comps2) then
1474+
matchKind := MatchKind.NOT_COMPATIBLE;
1475+
else
1476+
for i in 1:arrayLength(comps1) loop
1477+
(_, _, mk) := matchTypes(InstNode.getType(comps1[i]), InstNode.getType(comps2[i]), expression, allowUnknown);
1478+
1479+
if mk <> MatchKind.EXACT then
1480+
matchKind := MatchKind.NOT_COMPATIBLE;
1481+
break;
1482+
end if;
1483+
end for;
1484+
end if;
1485+
then
1486+
();
1487+
1488+
else
1489+
algorithm
1490+
matchKind := MatchKind.NOT_COMPATIBLE;
1491+
then
1492+
();
1493+
1494+
end match;
1495+
end matchComplexTypes;
1496+
14151497
function matchArrayTypes
14161498
input Type arrayType1;
14171499
input Type arrayType2;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ protected
887887
Type ty;
888888
algorithm
889889
for e in elements loop
890+
// TODO: Type checking.
890891
(exp, ty, var) := typeExp(e, info);
891892
variability := Types.constAnd(var, variability);
892893
expl := exp :: expl;

0 commit comments

Comments
 (0)