Skip to content

Commit

Permalink
[NF] Improve Inst.isDiscreteClass.
Browse files Browse the repository at this point in the history
- Handle classes extended from builtin types better.
  • Loading branch information
perost committed Nov 20, 2019
1 parent c7013c1 commit a7ebec2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
28 changes: 24 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -1951,15 +1951,35 @@ function updateComponentVariability
protected
Variability var = attr.variability;
algorithm
if referenceEq(attr, NFComponent.DEFAULT_ATTR) and
Type.isDiscrete(Class.getType(cls, clsNode)) then
if referenceEq(attr, NFComponent.DEFAULT_ATTR) and isDiscreteClass(clsNode) then
attr := NFComponent.IMPL_DISCRETE_ATTR;
elseif var == Variability.CONTINUOUS and
Type.isDiscrete(Class.getType(cls, clsNode)) then
elseif var == Variability.CONTINUOUS and isDiscreteClass(clsNode) then
attr.variability := Variability.IMPLICITLY_DISCRETE;
end if;
end updateComponentVariability;

function isDiscreteClass
input InstNode clsNode;
output Boolean isDiscrete;
protected
InstNode base_node;
Class cls;
array<InstNode> exts;
algorithm
base_node := Class.lastBaseClass(clsNode);
cls := InstNode.getClass(base_node);

isDiscrete := match cls
case Class.EXPANDED_CLASS(restriction = Restriction.TYPE())
algorithm
exts := ClassTree.getExtends(cls.elements);
then
if arrayLength(exts) == 1 then isDiscreteClass(exts[1]) else false;

else Type.isDiscrete(Class.getType(cls, base_node));
end match;
end isDiscreteClass;

function instTypeSpec
input Absyn.TypeSpec typeSpec;
input Modifier modifier;
Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -826,6 +826,7 @@ TypeDim3.mo \
TypeDim4.mo \
TypeDimNonType1.mo \
TypeExtends1.mo \
TypeExtends2.mo \
TypeMissingBaseType1.mo \
TypenameInvalid1.mo \
TypenameInvalid2.mo \
Expand Down
34 changes: 34 additions & 0 deletions testsuite/flattening/modelica/scodeinst/TypeExtends2.mo
@@ -0,0 +1,34 @@
// name: TypeExtends2
// keywords:
// status: correct
// cflags: -d=newInst
//

type TypeInteger
extends Integer;
end TypeInteger;

type TypeInteger2
extends TypeInteger;
end TypeInteger2;

type Color = TypeInteger2[3](each min = 0, each max = 255);

model TypeExtends2
Color color = {0, 0, 0};
Integer icolor[3] = color;
end TypeExtends2;

// Result:
// class TypeExtends2
// Integer color[1](min = 0, max = 255);
// Integer color[2](min = 0, max = 255);
// Integer color[3](min = 0, max = 255);
// Integer icolor[1];
// Integer icolor[2];
// Integer icolor[3];
// equation
// color = {0, 0, 0};
// icolor = color;
// end TypeExtends2;
// endResult

0 comments on commit a7ebec2

Please sign in to comment.