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

Commit 50d02d9

Browse files
perostOpenModelica-Hudson
authored andcommitted
NFInst improvements.
- Replaced all use of DAE.Const with DAE.VarKind, to be able to handle discrete variability and avoid unnecessary conversions. - Added variability check for bindings. - Fixed bug in unknown dimension inference that would make it fail on propagated bindings in some cases. - Added type check for array dimensions. - Moved setting a component's type to before typing its children, to handle the case where a child is dependent on its parent's type. - Removed deprecated file NFFunc.mo and deprecated code in NFInstUtil.
1 parent d3dffea commit 50d02d9

20 files changed

+315
-3358
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ function spatialDistribution "Not yet implemented"
753753
input Real x;
754754
input Boolean positiveVelocity;
755755
parameter input Real initialPoints[:](each min = 0, each max = 1) = {0.0, 1.0};
756-
parameter input Real initialValues[size(initialPoints)] = {0.0, 0.0};
756+
parameter input Real initialValues[size(initialPoints, 1)] = {0.0, 0.0};
757757
output Real out0;
758758
output Real out1;
759759
external "builtin";

Compiler/NFFrontEnd/NFBinding.mo

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public
6262
record TYPED_BINDING
6363
Expression bindingExp;
6464
Type bindingType;
65-
DAE.Const variability;
65+
DAE.VarKind variability;
6666
Integer propagatedLevels;
6767
SourceInfo info;
6868
end TYPED_BINDING;
@@ -142,12 +142,9 @@ public
142142

143143
function variability
144144
input Binding binding;
145-
output DAE.Const var;
145+
output DAE.VarKind var;
146146
algorithm
147-
var := match binding
148-
case TYPED_BINDING() then binding.variability;
149-
else DAE.Const.C_UNKNOWN();
150-
end match;
147+
TYPED_BINDING(variability = var) := binding;
151148
end variability;
152149

153150
function getInfo

Compiler/NFFrontEnd/NFBuiltin.mo

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,39 +152,39 @@ constant Binding STATESELECT_NEVER_BINDING =
152152
Binding.TYPED_BINDING(
153153
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "never", 1),
154154
STATESELECT_TYPE_TYPE,
155-
DAE.C_CONST(),
155+
DAE.VarKind.CONST(),
156156
-1,
157157
Absyn.dummyInfo);
158158

159159
constant Binding STATESELECT_AVOID_BINDING =
160160
Binding.TYPED_BINDING(
161161
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "avoid", 2),
162162
STATESELECT_TYPE_TYPE,
163-
DAE.C_CONST(),
163+
DAE.VarKind.CONST(),
164164
-1,
165165
Absyn.dummyInfo);
166166

167167
constant Binding STATESELECT_DEFAULT_BINDING =
168168
Binding.TYPED_BINDING(
169169
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "default", 3),
170170
STATESELECT_TYPE_TYPE,
171-
DAE.C_CONST(),
171+
DAE.VarKind.CONST(),
172172
-1,
173173
Absyn.dummyInfo);
174174

175175
constant Binding STATESELECT_PREFER_BINDING =
176176
Binding.TYPED_BINDING(
177177
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "prefer", 4),
178178
STATESELECT_TYPE_TYPE,
179-
DAE.C_CONST(),
179+
DAE.VarKind.CONST(),
180180
-1,
181181
Absyn.dummyInfo);
182182

183183
constant Binding STATESELECT_ALWAYS_BINDING =
184184
Binding.TYPED_BINDING(
185185
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "always", 5),
186186
STATESELECT_TYPE_TYPE,
187-
DAE.C_CONST(),
187+
DAE.VarKind.CONST(),
188188
-1,
189189
Absyn.dummyInfo);
190190

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import List;
5050
import NFClass.Class;
5151
import ErrorExt;
5252
import Util;
53+
import InstUtil = NFInstUtil;
5354

5455
protected
5556
import NFFunction.NamedArg;
@@ -171,7 +172,7 @@ uniontype Call
171172
input output Expression callExp;
172173
input SourceInfo info;
173174
output Type ty;
174-
output DAE.Const variability;
175+
output DAE.VarKind variability;
175176
protected
176177
Call call, argtycall;
177178
InstNode fn_node;
@@ -180,7 +181,7 @@ uniontype Call
180181
Function fn;
181182
list<Expression> args;
182183
list<Type> arg_ty;
183-
list<DAE.Const> arg_var;
184+
list<DAE.VarKind> arg_var;
184185
CallAttributes ca;
185186
list<TypedArg> tyArgs;
186187
algorithm
@@ -203,9 +204,9 @@ uniontype Call
203204

204205
args := list(Util.tuple31(a) for a in tyArgs);
205206

206-
variability := DAE.C_CONST();
207+
variability := DAE.VarKind.CONST();
207208
for a in tyArgs loop
208-
variability := Types.constAnd(variability,Util.tuple33(a));
209+
variability := InstUtil.variabilityAnd(variability,Util.tuple33(a));
209210
end for;
210211

211212
// Construct the call expression.
@@ -240,25 +241,25 @@ uniontype Call
240241
local
241242
Expression arg;
242243
Type arg_ty;
243-
DAE.Const arg_const;
244+
DAE.VarKind arg_var;
244245
list<TypedArg> typedArgs;
245246
list<TypedNamedArg> typedNamedArgs;
246247
String name;
247248

248249
case UNTYPED_CALL() algorithm
249250
typedArgs := {};
250251
for arg in call.arguments loop
251-
(arg, arg_ty, arg_const) := Typing.typeExp(arg,info);
252-
typedArgs := (arg, arg_ty, arg_const)::typedArgs;
252+
(arg, arg_ty, arg_var) := Typing.typeExp(arg,info);
253+
typedArgs := (arg, arg_ty, arg_var)::typedArgs;
253254
end for;
254255

255256
typedArgs := listReverse(typedArgs);
256257

257258
typedNamedArgs := {};
258259
for narg in call.named_args loop
259260
(name,arg) := narg;
260-
(arg, arg_ty, arg_const) := Typing.typeExp(arg,info);
261-
typedNamedArgs := (name, arg, arg_ty, arg_const)::typedNamedArgs;
261+
(arg, arg_ty, arg_var) := Typing.typeExp(arg,info);
262+
typedNamedArgs := (name, arg, arg_ty, arg_var)::typedNamedArgs;
262263
end for;
263264
listReverse(typedNamedArgs);
264265

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Type = NFType;
4545

4646
uniontype EvalTarget
4747
record DIMENSION
48-
String name;
48+
InstNode component;
4949
Integer index;
5050
Expression exp;
5151
SourceInfo info;
@@ -235,7 +235,7 @@ algorithm
235235
case EvalTarget.DIMENSION()
236236
algorithm
237237
Error.addSourceMessage(Error.STRUCTURAL_PARAMETER_OR_CONSTANT_WITH_NO_BINDING,
238-
{Expression.toString(exp), target.name}, target.info);
238+
{Expression.toString(exp), InstNode.name(target.component)}, target.info);
239239
then
240240
fail();
241241

Compiler/NFFrontEnd/NFClass.mo

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,6 @@ uniontype Class
163163
end match;
164164
end isBuiltin;
165165

166-
function getType
167-
input Class cls;
168-
output Type ty;
169-
algorithm
170-
ty := match cls
171-
case PARTIAL_BUILTIN() then cls.ty;
172-
case INSTANCED_BUILTIN() then cls.ty;
173-
else Type.UNKNOWN();
174-
end match;
175-
end getType;
176-
177166
function classTree
178167
input Class cls;
179168
output ClassTree tree;

Compiler/NFFrontEnd/NFComponent.mo

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ uniontype Component
228228
algorithm
229229
ty := match component
230230
case TYPED_COMPONENT() then component.ty;
231-
case UNTYPED_COMPONENT() then Class.getType(InstNode.getClass(component.classInst));
231+
case UNTYPED_COMPONENT() then InstNode.getType(component.classInst);
232232
case ITERATOR() then component.ty;
233233
else Type.UNKNOWN();
234234
end match;
@@ -467,6 +467,18 @@ uniontype Component
467467
else false;
468468
end match;
469469
end isRedeclare;
470+
471+
function dimensionCount
472+
input Component component;
473+
output Integer count;
474+
algorithm
475+
count := match component
476+
case UNTYPED_COMPONENT() then arrayLength(component.dimensions);
477+
case TYPED_COMPONENT() then listLength(Type.arrayDims(component.ty));
478+
else 0;
479+
end match;
480+
end dimensionCount;
481+
470482
end Component;
471483

472484
annotation(__OpenModelica_Interface="frontend");

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,11 @@ public
101101

102102
function getVariability
103103
input ComponentRef cref;
104-
output DAE.Const var;
104+
output DAE.VarKind var;
105105
algorithm
106106
var := match cref
107-
case CREF()
108-
then
109-
match Component.variability(InstNode.component(cref.node))
110-
case DAE.VarKind.VARIABLE() then DAE.Const.C_VAR();
111-
case DAE.VarKind.DISCRETE() then DAE.Const.C_VAR();
112-
case DAE.VarKind.PARAM() then DAE.Const.C_PARAM();
113-
case DAE.VarKind.CONST() then DAE.Const.C_CONST();
114-
else DAE.Const.C_UNKNOWN();
115-
end match;
116-
117-
else DAE.Const.C_UNKNOWN();
107+
case CREF() then Component.variability(InstNode.component(cref.node));
108+
else DAE.VarKind.VARIABLE();
118109
end match;
119110
end getVariability;
120111

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ public
905905
String s;
906906
Expression e;
907907
Type t;
908-
DAE.Const c;
908+
DAE.VarKind v;
909909

910910
case Call.UNTYPED_CALL()
911911
algorithm
@@ -926,15 +926,15 @@ public
926926
tnargs := {};
927927

928928
for arg in call.arguments loop
929-
(e, t, c) := arg;
929+
(e, t, v) := arg;
930930
e := traverse(e, func);
931-
targs := (e, t, c) :: targs;
931+
targs := (e, t, v) :: targs;
932932
end for;
933933

934934
for arg in call.named_args loop
935-
(s, e, t, c) := arg;
935+
(s, e, t, v) := arg;
936936
e := traverse(e, func);
937-
tnargs := (s, e, t, c) :: tnargs;
937+
tnargs := (s, e, t, v) :: tnargs;
938938
end for;
939939
then
940940
Call.ARG_TYPED_CALL(call.ref, listReverse(targs), listReverse(tnargs));
@@ -1089,5 +1089,42 @@ public
10891089
end match;
10901090
end expandCref4;
10911091

1092+
function arrayFirstScalar
1093+
"Returns the first scalar element of an array. Fails if the array is empty."
1094+
input Expression arrayExp;
1095+
output Expression exp;
1096+
algorithm
1097+
exp := match arrayExp
1098+
case ARRAY() then arrayFirstScalar(listHead(arrayExp.elements));
1099+
else arrayExp;
1100+
end match;
1101+
end arrayFirstScalar;
1102+
1103+
function arrayAllEqual
1104+
"Checks if all scalar elements in an array are equal to each other."
1105+
input Expression arrayExp;
1106+
output Boolean allEqual;
1107+
algorithm
1108+
allEqual := matchcontinue arrayExp
1109+
case ARRAY()
1110+
then arrayAllEqual2(arrayExp, arrayFirstScalar(arrayExp));
1111+
else true;
1112+
end matchcontinue;
1113+
end arrayAllEqual;
1114+
1115+
function arrayAllEqual2
1116+
input Expression arrayExp;
1117+
input Expression element;
1118+
output Boolean allEqual;
1119+
algorithm
1120+
allEqual := match arrayExp
1121+
case ARRAY(elements = ARRAY() :: _)
1122+
then List.map1BoolAnd(arrayExp.elements, arrayAllEqual2, element);
1123+
case ARRAY()
1124+
then List.map1BoolAnd(arrayExp.elements, isEqual, element);
1125+
else true;
1126+
end match;
1127+
end arrayAllEqual2;
1128+
10921129
annotation(__OpenModelica_Interface="frontend");
10931130
end NFExpression;

0 commit comments

Comments
 (0)