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

Commit b7bed21

Browse files
perostOpenModelica-Hudson
authored andcommitted
NFInst improvements.
- Added origin as argument to typeExp to handle 'end' and to allow specific handling of expression based on where they're occuring. - Moved detection of invalid usage of typenames from the instantiation to the typing. - Changed Variability to an enumeration for easier handling. - Improved typing of subscripts, including type checking and checking the number of subscripts.
1 parent 5dfdea2 commit b7bed21

20 files changed

Lines changed: 453 additions & 192 deletions

Compiler/FrontEnd/Static.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10217,7 +10217,7 @@ algorithm
1021710217
// Fail if the variability is wrong.
1021810218
if not Types.constEqualOrHigher(c1, c2) then
1021910219
exp_str := ExpressionDump.printExpStr(inExp);
10220-
c_str := DAEUtil.constStrFriendly(c2);
10220+
c_str := Types.unparseConst(c2);
1022110221
Error.addSourceMessageAndFail(Error.FUNCTION_SLOT_VARIABILITY,
1022210222
{fa1, exp_str, c_str}, inInfo);
1022310223
end if;

Compiler/NFFrontEnd/NFBinding.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131

3232
encapsulated uniontype NFBinding
3333
public
34-
import DAE;
3534
import Expression = NFExpression;
3635
import NFInstNode.InstNode;
3736
import SCode;
3837
import Type = NFType;
38+
import NFPrefixes.Variability;
3939

4040
protected
4141
import Dump;
@@ -62,7 +62,7 @@ public
6262
record TYPED_BINDING
6363
Expression bindingExp;
6464
Type bindingType;
65-
DAE.VarKind variability;
65+
Variability variability;
6666
Integer propagatedLevels;
6767
SourceInfo info;
6868
end TYPED_BINDING;
@@ -142,7 +142,7 @@ public
142142

143143
function variability
144144
input Binding binding;
145-
output DAE.VarKind var;
145+
output Variability var;
146146
algorithm
147147
TYPED_BINDING(variability = var) := binding;
148148
end variability;

Compiler/NFFrontEnd/NFBuiltin.mo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ encapsulated package NFBuiltin
4242

4343
public
4444
import Absyn;
45-
import DAE;
4645
import SCode;
4746
import Binding = NFBinding;
4847
import NFClass.Class;
@@ -55,6 +54,7 @@ import NFMod.Modifier;
5554
import Type = NFType;
5655
import BuiltinFuncs = NFBuiltinFuncs;
5756
import Pointer;
57+
import NFPrefixes.Variability;
5858

5959
encapsulated package Elements
6060
import SCode;
@@ -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.VarKind.CONST(),
155+
Variability.CONSTANT,
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.VarKind.CONST(),
163+
Variability.CONSTANT,
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.VarKind.CONST(),
171+
Variability,
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.VarKind.CONST(),
179+
Variability.CONSTANT,
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.VarKind.CONST(),
187+
Variability.CONSTANT,
188188
-1,
189189
Absyn.dummyInfo);
190190

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import DAE;
3636
import NFInstNode.InstNode;
3737
import Expression = NFExpression;
3838
import Type = NFType;
39+
import NFPrefixes.Variability;
3940

4041
protected
4142
import NFInstNode.CachedData;
@@ -45,12 +46,13 @@ import Inst = NFInst;
4546
import NFInstNode.InstNodeType;
4647
import Lookup = NFLookup;
4748
import Typing = NFTyping;
49+
import ExpOrigin = NFTyping.ExpOrigin;
4850
import Types;
4951
import List;
5052
import NFClass.Class;
5153
import ErrorExt;
5254
import Util;
53-
import InstUtil = NFInstUtil;
55+
import Prefixes = NFPrefixes;
5456

5557
protected
5658
import NFFunction.NamedArg;
@@ -172,7 +174,7 @@ uniontype Call
172174
input output Expression callExp;
173175
input SourceInfo info;
174176
output Type ty;
175-
output DAE.VarKind variability;
177+
output Variability variability;
176178
protected
177179
Call call, argtycall;
178180
InstNode fn_node;
@@ -181,7 +183,7 @@ uniontype Call
181183
Function fn;
182184
list<Expression> args;
183185
list<Type> arg_ty;
184-
list<DAE.VarKind> arg_var;
186+
list<Variability> arg_var;
185187
CallAttributes ca;
186188
list<TypedArg> tyArgs;
187189
algorithm
@@ -204,9 +206,9 @@ uniontype Call
204206

205207
args := list(Util.tuple31(a) for a in tyArgs);
206208

207-
variability := DAE.VarKind.CONST();
209+
variability := Variability.CONSTANT;
208210
for a in tyArgs loop
209-
variability := InstUtil.variabilityAnd(variability,Util.tuple33(a));
211+
variability := Prefixes.variabilityMax(variability, Util.tuple33(a));
210212
end for;
211213

212214
// Construct the call expression.
@@ -241,15 +243,15 @@ uniontype Call
241243
local
242244
Expression arg;
243245
Type arg_ty;
244-
DAE.VarKind arg_var;
246+
Variability arg_var;
245247
list<TypedArg> typedArgs;
246248
list<TypedNamedArg> typedNamedArgs;
247249
String name;
248250

249251
case UNTYPED_CALL() algorithm
250252
typedArgs := {};
251253
for arg in call.arguments loop
252-
(arg, arg_ty, arg_var) := Typing.typeExp(arg,info);
254+
(arg, arg_ty, arg_var) := Typing.typeExp(arg, info);
253255
typedArgs := (arg, arg_ty, arg_var)::typedArgs;
254256
end for;
255257

@@ -258,7 +260,7 @@ uniontype Call
258260
typedNamedArgs := {};
259261
for narg in call.named_args loop
260262
(name,arg) := narg;
261-
(arg, arg_ty, arg_var) := Typing.typeExp(arg,info);
263+
(arg, arg_ty, arg_var) := Typing.typeExp(arg, info);
262264
typedNamedArgs := (name, arg, arg_ty, arg_var)::typedNamedArgs;
263265
end for;
264266
listReverse(typedNamedArgs);

Compiler/NFFrontEnd/NFComponent.mo

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import SCode.Element;
4141
import SCode;
4242
import Type = NFType;
4343
import Expression = NFExpression;
44+
import NFPrefixes.Variability;
4445

4546
protected
4647
import NFInstUtil;
@@ -51,7 +52,7 @@ constant Component.Attributes CONST_ATTR =
5152
Component.Attributes.ATTRIBUTES(
5253
DAE.NON_CONNECTOR(),
5354
DAE.NON_PARALLEL(),
54-
DAE.VARIABLE(),
55+
Variability.CONTINUOUS,
5556
DAE.BIDIR(),
5657
DAE.NOT_INNER_OUTER(),
5758
DAE.PUBLIC());
@@ -60,7 +61,7 @@ constant Component.Attributes INPUT_ATTR =
6061
Component.Attributes.ATTRIBUTES(
6162
DAE.NON_CONNECTOR(),
6263
DAE.NON_PARALLEL(),
63-
DAE.VARIABLE(),
64+
Variability.CONTINUOUS,
6465
DAE.INPUT(),
6566
DAE.NOT_INNER_OUTER(),
6667
DAE.PUBLIC());
@@ -69,7 +70,7 @@ constant Component.Attributes OUTPUT_ATTR =
6970
Component.Attributes.ATTRIBUTES(
7071
DAE.NON_CONNECTOR(),
7172
DAE.NON_PARALLEL(),
72-
DAE.VARIABLE(),
73+
Variability.CONTINUOUS,
7374
DAE.OUTPUT(),
7475
DAE.NOT_INNER_OUTER(),
7576
DAE.PUBLIC());
@@ -80,7 +81,7 @@ uniontype Component
8081
// adrpo: keep the order in DAE.ATTR
8182
DAE.ConnectorType connectorType;
8283
DAE.VarParallelism parallelism;
83-
DAE.VarKind variability;
84+
Variability variability;
8485
DAE.VarDirection direction;
8586
DAE.VarInnerOuter innerOuter;
8687
DAE.VarVisibility visibility;
@@ -356,36 +357,26 @@ uniontype Component
356357

357358
function variability
358359
input Component component;
359-
output DAE.VarKind variability;
360+
output Variability variability;
360361
algorithm
361362
variability := match component
362363
case TYPED_COMPONENT(attributes = Attributes.ATTRIBUTES(variability = variability)) then variability;
363-
case TYPED_COMPONENT(attributes = Attributes.DEFAULT()) then DAE.VarKind.VARIABLE();
364+
case TYPED_COMPONENT(attributes = Attributes.DEFAULT()) then Variability.CONTINUOUS;
364365
case UNTYPED_COMPONENT(attributes = Attributes.ATTRIBUTES(variability = variability)) then variability;
365-
case UNTYPED_COMPONENT(attributes = Attributes.DEFAULT()) then DAE.VarKind.VARIABLE();
366-
case ITERATOR() then DAE.VarKind.CONST();
366+
case UNTYPED_COMPONENT(attributes = Attributes.DEFAULT()) then Variability.CONTINUOUS;
367+
case ITERATOR() then Variability.CONSTANT;
367368
else fail();
368369
end match;
369370
end variability;
370371

371372
function isConst
372373
input Component component;
373-
output Boolean isConst;
374-
algorithm
375-
isConst := match variability(component)
376-
case DAE.VarKind.CONST() then true;
377-
else false;
378-
end match;
374+
output Boolean isConst = variability(component) == Variability.CONSTANT;
379375
end isConst;
380376

381377
function isVar
382378
input Component component;
383-
output Boolean isConst;
384-
algorithm
385-
isConst := match variability(component)
386-
case DAE.VarKind.VARIABLE() then true;
387-
else false;
388-
end match;
379+
output Boolean isConst = variability(component) == Variability.CONTINUOUS;
389380
end isVar;
390381

391382
function visibility

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected
3939
import RangeIterator = NFRangeIterator;
4040
import Dimension = NFDimension;
4141
import Expression = NFExpression;
42+
import NFPrefixes.Variability;
4243

4344
import ComponentRef = NFComponentRef;
4445

@@ -101,11 +102,11 @@ public
101102

102103
function getVariability
103104
input ComponentRef cref;
104-
output DAE.VarKind var;
105+
output Variability var;
105106
algorithm
106107
var := match cref
107108
case CREF() then Component.variability(InstNode.component(cref.node));
108-
else DAE.VarKind.VARIABLE();
109+
else Variability.CONTINUOUS;
109110
end match;
110111
end getVariability;
111112

Compiler/NFFrontEnd/NFDimension.mo

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public
4141
import NFInstNode.InstNode;
4242
import Type = NFType;
4343
import ComponentRef = NFComponentRef;
44+
import NFPrefixes.Variability;
4445

4546
record RAW_DIM
4647
Absyn.Subscript dim;
@@ -64,13 +65,15 @@ public
6465

6566
record EXP
6667
Expression exp;
68+
Variability var;
6769
end EXP;
6870

6971
record UNKNOWN
7072
end UNKNOWN;
7173

7274
function fromExp
7375
input Expression exp;
76+
input Variability var;
7477
output Dimension dim;
7578
algorithm
7679
dim := match exp
@@ -93,7 +96,7 @@ public
9396
fail();
9497
end match;
9598

96-
else Dimension.EXP(exp);
99+
else Dimension.EXP(exp, var);
97100
end match;
98101
end fromExp;
99102

@@ -191,6 +194,20 @@ public
191194
end match;
192195
end isKnown;
193196

197+
function subscriptType
198+
"Returns the expected type of a subscript for the given dimension."
199+
input Dimension dim;
200+
output Type ty;
201+
algorithm
202+
ty := match dim
203+
case INTEGER() then Type.INTEGER();
204+
case BOOLEAN() then Type.BOOLEAN();
205+
case ENUM() then dim.enumType;
206+
case EXP() then Expression.typeOf(dim.exp);
207+
else Type.UNKNOWN();
208+
end match;
209+
end subscriptType;
210+
194211
function toString
195212
input Dimension dim;
196213
output String str;

Compiler/NFFrontEnd/NFExpOrigin.mo

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
encapsulated uniontype NFExpOrigin
3+
"This uniontype is used to mark where an expression comes from in typeExp, so
4+
that it can check things like 'end' only being used in subscripts."
5+
6+
protected
7+
import Dimension = NFDimension;
8+
import ExpOrigin = NFExpOrigin;
9+
10+
public
11+
record DIMENSION end DIMENSION;
12+
13+
record SUBSCRIPT
14+
Dimension dimension;
15+
end SUBSCRIPT;
16+
17+
record LHS "lhs of equation/assignment" end LHS;
18+
record RHS "rhs of equation/assignment" end RHS;
19+
record ITERATION_RANGE end ITERATION_RANGE;
20+
record BINDING end BINDING;
21+
record NO_ORIGIN end NO_ORIGIN;
22+
23+
function next
24+
"Some origins only apply to the whole expression. Tuples can for example only
25+
be used on the lhs of an equation/assignment, but only if it's alone on the
26+
lhs and not part of a larger expression. This function returns the next
27+
appropriate origin when moving into an expression."
28+
input ExpOrigin origin;
29+
output ExpOrigin nextOrigin;
30+
algorithm
31+
nextOrigin := match origin
32+
case LHS() then NO_ORIGIN();
33+
case RHS() then NO_ORIGIN();
34+
case ITERATION_RANGE() then NO_ORIGIN();
35+
else origin;
36+
end match;
37+
end next;
38+
39+
annotation(__OpenModelica_Interface="frontend");
40+
end NFExpOrigin;
41+

0 commit comments

Comments
 (0)