Skip to content

Commit 3a59575

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Improve operator overloading and error-messages
1 parent ced8714 commit 3a59575

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

Compiler/FrontEnd/OperatorOverloading.mo

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function binary
8585
output DAE.Properties outProp;
8686
algorithm
8787
(outCache, outExp, outProp) :=
88-
matchcontinue(inCache,inEnv,inOperator1, inProp1, inExp1, inProp2, inExp2, AbExp, AbExp1, AbExp2, inImpl, inSymTab, inPre, inInfo)
88+
match (inCache,inEnv,inOperator1, inProp1, inExp1, inProp2, inExp2, AbExp, AbExp1, AbExp2, inImpl, inSymTab, inPre, inInfo)
8989
local
9090
FCore.Cache cache;
9191
FCore.Graph env;
@@ -103,52 +103,49 @@ algorithm
103103
Boolean didInline;
104104

105105
// handle tuple op non_tuple
106-
case (_, _, _, props1 as DAE.PROP_TUPLE(), _, DAE.PROP(), _, _, _, _, _, _, _, _)
106+
case (_, _, _, props1 as DAE.PROP_TUPLE(), _, DAE.PROP(), _, _, _, _, _, _, _, _) guard not Config.acceptMetaModelicaGrammar()
107107
equation
108-
false = Config.acceptMetaModelicaGrammar();
109108
(prop as DAE.PROP(type1, _)) = Types.propTupleFirstProp(props1);
110109
exp = DAE.TSUB(inExp1, 1, type1);
111-
(_, exp, prop) = binary(inCache, inEnv, inOperator1, prop, exp, inProp2, inExp2, AbExp, AbExp1, AbExp2, inImpl, inSymTab, inPre, inInfo);
112-
then
113-
(inCache, exp, prop);
110+
(cache, exp, prop) = binary(inCache, inEnv, inOperator1, prop, exp, inProp2, inExp2, AbExp, AbExp1, AbExp2, inImpl, inSymTab, inPre, inInfo);
111+
then (cache, exp, prop);
114112

115113
// handle non_tuple op tuple
116-
case (_, _, _, DAE.PROP(), _, props2 as DAE.PROP_TUPLE(), _, _, _, _, _, _, _, _)
114+
case (_, _, _, DAE.PROP(), _, props2 as DAE.PROP_TUPLE(), _, _, _, _, _, _, _, _) guard not Config.acceptMetaModelicaGrammar()
117115
equation
118-
false = Config.acceptMetaModelicaGrammar();
119116
(prop as DAE.PROP(type2, _)) = Types.propTupleFirstProp(props2);
120117
exp = DAE.TSUB(inExp2, 1, type2);
121-
(_, exp, prop) = binary(inCache, inEnv, inOperator1, inProp1, inExp1, prop, exp, AbExp, AbExp1, AbExp2, inImpl, inSymTab, inPre, inInfo);
122-
then
123-
(inCache, exp, prop);
118+
(cache, exp, prop) = binary(inCache, inEnv, inOperator1, inProp1, inExp1, prop, exp, AbExp, AbExp1, AbExp2, inImpl, inSymTab, inPre, inInfo);
119+
then (cache, exp, prop);
120+
121+
case (cache, env, aboper, DAE.PROP(type1,const1), exp1, DAE.PROP(type2,const2), exp2, _, _, _, _, _, _, _)
122+
algorithm
123+
if Types.isRecord(Types.arrayElementType(type1)) or Types.isRecord(Types.arrayElementType(type2)) then
124+
// Overloaded records
125+
(cache, exp, _, otype) := binaryUserdef(cache,env,aboper,inExp1,inExp2,type1,type2,inImpl,inSymTab,inPre,inInfo);
126+
functionTree := FCore.getFunctionTree(cache);
127+
exp := ExpressionSimplify.simplify1(exp);
128+
(exp,_,didInline,_) := Inline.inlineExp(exp,(SOME(functionTree),{DAE.BUILTIN_EARLY_INLINE(),DAE.EARLY_INLINE()}),DAE.emptyElementSource);
129+
exp := ExpressionSimplify.condsimplify(didInline,exp);
130+
const := Types.constAnd(const1, const2);
131+
prop := DAE.PROP(otype,const);
132+
else // Normal operator deoverloading
133+
if Types.isBoxedType(type1) and Types.isBoxedType(type2) then
134+
// Do the MetaModelica type-casting here for simplicity
135+
(exp1, type1) := Types.matchType(exp1, type1, Types.unboxedType(type1), true);
136+
(exp2, type2) := Types.matchType(exp2, type2, Types.unboxedType(type2), true);
137+
end if;
138+
(opList, type1, exp1, type2, exp2) := operatorsBinary(aboper, type1, exp1, type2, exp2);
139+
(oper, {exp1,exp2}, otype) := deoverload(opList, {(exp1,type1), (exp2,type2)}, AbExp, inPre, inInfo);
140+
const := Types.constAnd(const1, const2);
141+
exp := replaceOperatorWithFcall(AbExp, exp1,oper,SOME(exp2), const);
142+
exp := ExpressionSimplify.simplify(exp);
143+
prop := DAE.PROP(otype,const);
144+
warnUnsafeRelations(inEnv,AbExp,const, type1,type2,exp1,exp2,oper,inPre,inInfo);
145+
end if;
146+
then (cache, exp, prop);
124147

125-
case (_, _, aboper, DAE.PROP(type1,const1), exp1, DAE.PROP(type2,const2), exp2, _, _, _, _, _, _, _)
126-
equation
127-
false = Types.isRecord(Types.arrayElementType(type1));
128-
false = Types.isRecord(Types.arrayElementType(type2));
129-
(opList, type1,exp1,type2,exp2) = operatorsBinary(aboper, type1, exp1, type2, exp2);
130-
(oper, {exp1,exp2}, otype) = deoverload(opList, {(exp1,type1), (exp2,type2)}, AbExp, inPre, inInfo);
131-
const = Types.constAnd(const1, const2);
132-
exp = replaceOperatorWithFcall(AbExp, exp1,oper,SOME(exp2), const);
133-
(exp,_) = ExpressionSimplify.simplify(exp);
134-
prop = DAE.PROP(otype,const);
135-
warnUnsafeRelations(inEnv,AbExp,const, type1,type2,exp1,exp2,oper,inPre,inInfo);
136-
then
137-
(inCache,exp, prop);
138-
139-
case(cache, env, aboper, DAE.PROP(type1, const1), _, DAE.PROP(type2, const2), _, _, _, _, _, _, _, _)
140-
equation
141-
true = Types.isRecord(Types.arrayElementType(type1)) or Types.isRecord(Types.arrayElementType(type2));
142-
(cache, exp, _, otype) = binaryUserdef(cache,env,aboper,inExp1,inExp2,type1,type2,inImpl,inSymTab,inPre,inInfo);
143-
functionTree = FCore.getFunctionTree(cache);
144-
(exp,_) = ExpressionSimplify.simplify1(exp);
145-
(exp,_,didInline,_) = Inline.inlineExp(exp,(SOME(functionTree),{DAE.BUILTIN_EARLY_INLINE(),DAE.EARLY_INLINE()}),DAE.emptyElementSource);
146-
(exp,_) = ExpressionSimplify.condsimplify(didInline,exp);
147-
const = Types.constAnd(const1, const2);
148-
then
149-
(cache, exp, DAE.PROP(otype,const));
150-
151-
end matchcontinue;
148+
end match;
152149
end binary;
153150

154151
function unary
@@ -1165,9 +1162,6 @@ algorithm
11651162
case (Absyn.EQUAL(),t1,e1,t2,e2)
11661163
equation
11671164
enum_op = makeEnumOperator(DAE.EQUAL(DAE.T_ENUMERATION_DEFAULT), t1, t2);
1168-
types = if Types.isBoxedType(t1) and Types.isBoxedType(t2)
1169-
then {(DAE.EQUAL(DAE.T_METABOXED_DEFAULT),{t1,t2},DAE.T_BOOL_DEFAULT)}
1170-
else {};
11711165
types =
11721166
(DAE.EQUAL(DAE.T_INTEGER_DEFAULT),
11731167
{DAE.T_INTEGER_DEFAULT,DAE.T_INTEGER_DEFAULT},DAE.T_BOOL_DEFAULT)::
@@ -1178,16 +1172,13 @@ algorithm
11781172
{DAE.T_STRING_DEFAULT,DAE.T_STRING_DEFAULT},DAE.T_BOOL_DEFAULT)::
11791173
(DAE.EQUAL(DAE.T_BOOL_DEFAULT),
11801174
{DAE.T_BOOL_DEFAULT,DAE.T_BOOL_DEFAULT},DAE.T_BOOL_DEFAULT)::
1181-
types;
1175+
{};
11821176
then
11831177
(types,t1,e1,t2,e2);
11841178

11851179
case (Absyn.NEQUAL(),t1,e1,t2,e2)
11861180
equation
11871181
enum_op = makeEnumOperator(DAE.NEQUAL(DAE.T_ENUMERATION_DEFAULT), t1, t2);
1188-
types = if Types.isBoxedType(t1) and Types.isBoxedType(t2)
1189-
then {(DAE.NEQUAL(DAE.T_METABOXED_DEFAULT),{t1,t2},DAE.T_BOOL_DEFAULT)}
1190-
else {};
11911182
types =
11921183
(DAE.NEQUAL(DAE.T_INTEGER_DEFAULT),
11931184
{DAE.T_INTEGER_DEFAULT,DAE.T_INTEGER_DEFAULT},DAE.T_BOOL_DEFAULT)::
@@ -1198,7 +1189,7 @@ algorithm
11981189
{DAE.T_STRING_DEFAULT,DAE.T_STRING_DEFAULT},DAE.T_BOOL_DEFAULT)::
11991190
(DAE.NEQUAL(DAE.T_BOOL_DEFAULT),
12001191
{DAE.T_BOOL_DEFAULT,DAE.T_BOOL_DEFAULT},DAE.T_BOOL_DEFAULT)::
1201-
types;
1192+
{};
12021193
then
12031194
(types,t1,e1,t2,e2);
12041195

Compiler/Template/CodegenC.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8804,7 +8804,7 @@ case rel as RELATION(__) then
88048804
case NEQUAL(ty = T_REAL(__)) then '(<%e1%> != <%e2%>)'
88058805
case NEQUAL(ty = T_ENUMERATION(__)) then '(<%e1%> != <%e2%>)'
88068806

8807-
else error(sourceInfo(), 'daeExpRelation:ERR')
8807+
else error(sourceInfo(), 'daeExpRelation <%printExpStr(exp)%>')
88088808
end daeExpRelation;
88098809

88108810

0 commit comments

Comments
 (0)