Skip to content

Commit

Permalink
Detect tuples used as expressions
Browse files Browse the repository at this point in the history
This fixes ticket:4007. Tuples may only be used on the left side of an
assignment in Modelica. In MetaModelica, we allow tuples anywhere.
  • Loading branch information
sjoelund committed Sep 7, 2016
1 parent 863a98b commit 3423a6e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
7 changes: 3 additions & 4 deletions Compiler/FrontEnd/InstSection.mo
Expand Up @@ -334,8 +334,7 @@ algorithm
// Check that the equation is valid if the lhs is a tuple.
checkTupleCallEquationMessage(lhs_aexp, rhs_aexp, info);

(outCache, lhs_exp, lhs_prop) :=
Static.elabExp(inCache, inEnv, lhs_aexp, inImpl, NONE(), true, inPrefix, info);
(outCache, lhs_exp, lhs_prop) := Static.elabExpLHS(inCache, inEnv, lhs_aexp, inImpl, NONE(), true, inPrefix, info);
(outCache, rhs_exp, rhs_prop) :=
Static.elabExp(inCache, inEnv, rhs_aexp, inImpl, NONE(), true, inPrefix, info);

Expand Down Expand Up @@ -1112,7 +1111,7 @@ algorithm
true = boolOr(b3,b4);
true = Expression.containFunctioncall(elabedE2);
(e1,prop) = expandTupleEquationWithWild(e1,prop2,prop);
(cache,elabedE1_2,prop1,_) = Static.elabExp(cache,env, e1, impl,NONE(),false,pre,info);
(cache,elabedE1_2,prop1,_) = Static.elabExpLHS(cache,env, e1, impl,NONE(),false,pre,info);
(cache, elabedE1_2, prop1) = Ceval.cevalIfConstant(cache, env, elabedE1_2, prop1, impl, info);
(cache,elabedE2_2,prop2,_) = Static.elabExp(cache,env, e2, impl,NONE(),false,pre,info);
(cache, elabedE2_2, prop2) = Ceval.cevalIfConstant(cache, env, elabedE2_2, prop2, impl, info);
Expand Down Expand Up @@ -5099,7 +5098,7 @@ algorithm
equation
Absyn.CALL() = inRhs;
true = List.all(expl, Absyn.isCref);
(cache,e_1,prop1,_) = Static.elabExp(cache,inEnv,e1,inImpl,NONE(),false,inPre,info);
(cache,e_1,prop1,_) = Static.elabExpLHS(cache,inEnv,e1,inImpl,NONE(),false,inPre,info);
lt = Types.getPropType(prop1);
rt = Types.getPropType(prop2);
false = Types.subtype(lt, rt);
Expand Down
50 changes: 34 additions & 16 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -288,18 +288,7 @@ function: elabExp
DAE.Properties type, and include the type and the variability of the
expression. This function performs analysis, and returns an
DAE.Exp and the properties."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Exp inExp;
input Boolean inImplicit;
input Option<GlobalScript.SymbolTable> inST;
input Boolean inDoVect;
input Prefix.Prefix inPrefix;
input SourceInfo inInfo;
output FCore.Cache outCache = inCache;
output DAE.Exp outExp;
output DAE.Properties outProperties;
output Option<GlobalScript.SymbolTable> outST = inST;
extends PartialElabExpFunc;
protected
Absyn.Exp e;
Integer num_errmsgs;
Expand Down Expand Up @@ -354,7 +343,7 @@ algorithm
end try;
end elabExp;

protected partial function PartialElabExpFunc
public partial function PartialElabExpFunc
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Exp inExp;
Expand Down Expand Up @@ -642,6 +631,13 @@ end elabExp_PartEvalFunction;

protected function elabExp_Tuple
extends PartialElabExpFunc;
algorithm
(outCache, outExp, outProperties, outST) := elabExp_Tuple_LHS_RHS(inCache, inEnv, inExp, inImplicit, inST, inDoVect, inPrefix, inInfo);
end elabExp_Tuple;

protected function elabExp_Tuple_LHS_RHS
extends PartialElabExpFunc;
input Boolean isLhs=false;
protected
list<Absyn.Exp> el;
list<DAE.Exp> expl;
Expand All @@ -651,10 +647,25 @@ protected
algorithm
Absyn.TUPLE(expressions = el) := inExp;
(outCache, expl, props) := elabTuple(outCache, inEnv, el, inImplicit,
inDoVect, inPrefix, inInfo);
inDoVect, inPrefix, inInfo, isLhs);
(types, consts) := splitProps(props);
(outExp, outProperties) := fixTupleMetaModelica(expl, types, consts);
end elabExp_Tuple;
end elabExp_Tuple_LHS_RHS;

public function elabExpLHS "Special check for tuples, which only occur on the LHS"
extends PartialElabExpFunc;
algorithm
(outCache, outExp, outProperties, outST) := match inExp
case Absyn.TUPLE()
algorithm
(outCache, outExp, outProperties, outST) := elabExp_Tuple_LHS_RHS(inCache, inEnv, inExp, inImplicit, inST, inDoVect, inPrefix, inInfo, isLhs=true);
then (outCache, outExp, outProperties, outST);
else
algorithm
(outCache, outExp, outProperties, outST) := elabExp(inCache, inEnv, inExp, inImplicit, inST, inDoVect, inPrefix, inInfo);
then (outCache, outExp, outProperties, outST);
end match;
end elabExpLHS;

protected function elabExp_Range
"Elaborates a range expression on the form start:stop or start:step:stop."
Expand Down Expand Up @@ -2287,7 +2298,7 @@ algorithm
// The output from functions does just have one const flag. Fix this!!
case (cache,env,Absyn.TUPLE(expressions = (es as (_ :: _))),impl,pre,_)
equation
(cache,es_1,props) = elabTuple(cache,env,es,impl,false,pre,info);
(cache,es_1,props) = elabTuple(cache,env,es,impl,false,pre,info,false);
(types,consts) = splitProps(props);
then
(cache,DAE.TUPLE(es_1),DAE.PROP_TUPLE(DAE.T_TUPLE(types,NONE(),DAE.emptyTypeSource),DAE.TUPLE_CONST(consts)));
Expand Down Expand Up @@ -2534,13 +2545,20 @@ protected function elabTuple
input Boolean inDoVect;
input Prefix.Prefix inPrefix;
input SourceInfo inInfo;
input Boolean isLhs;
output FCore.Cache outCache = inCache;
output list<DAE.Exp> outExpl = {};
output list<DAE.Properties> outProperties = {};
protected
DAE.Exp exp;
DAE.Properties prop;
algorithm

if if not isLhs then not Config.acceptMetaModelicaGrammar() else false then
Error.addSourceMessage(Error.RHS_TUPLE_EXPRESSION, {Dump.printExpStr(Absyn.TUPLE(inExpl))}, inInfo);
fail();
end if;

for e in inExpl loop
(outCache, exp, prop) :=
elabExp(outCache, inEnv, e, inImplicit, NONE(), inDoVect, inPrefix, inInfo);
Expand Down
2 changes: 2 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -706,6 +706,8 @@ public constant Message NO_JACONIAN_TORNLINEAR_SYSTEM = MESSAGE(289, SYMBOLIC(),
Util.gettext("A torn linear system has no symbolic jacobian and currently there are no means to solve that numerically. Please compile with the module \"calculateStrongComponentJacobians\" to provide symbolic jacobians for torn linear systems."));
public constant Message EXT_FN_SINGLE_RETURN_ARRAY = MESSAGE(290, TRANSLATION(), WARNING(),
Util.gettext("An external declaration with a single output without explicit mapping is defined as having the output as the lhs, but language %s does not support this for array variables. OpenModelica will put the output as an input (as is done when there is more than 1 output), but this is not according to the Modelica Specification. Use an explicit mapping instead of the implicit one to suppress this warning."));
public constant Message RHS_TUPLE_EXPRESSION = MESSAGE(291, TRANSLATION(), ERROR(),
Util.gettext("Tuple expressions may only occur on the left side of an assignment or equation with a single function call on the right side. Got the following expression: %s."));

public constant Message UNBOUND_PARAMETER_WITH_START_VALUE_WARNING = MESSAGE(499, TRANSLATION(), WARNING(),
Util.gettext("Parameter %s has no value, and is fixed during initialization (fixed=true), using available start value (start=%s) as default value."));
Expand Down

0 comments on commit 3423a6e

Please sign in to comment.