Skip to content

Commit

Permalink
Fix for bug #1819
Browse files Browse the repository at this point in the history
- Only disallow assignment to input in functions.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12964 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Sep 18, 2012
1 parent 8c530cc commit 34f8bde
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 53 deletions.
12 changes: 0 additions & 12 deletions Compiler/FrontEnd/Algorithm.mo
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,6 @@ algorithm
then
fail();

/*
adrpo: this is now done in Static.checkAssignmentToInput
// assignment to an input, report error - but keep going anyway; bootstrapping needs this :(
case (lhs,_,rhs,_,DAE.ATTR(direction=Absyn.INPUT()),_,source)
equation
false = Config.acceptMetaModelicaGrammar();
lhs_str = ExpressionDump.printExpStr(lhs);
Error.addSourceMessage(Error.ASSIGN_READONLY_ERROR, {"input",lhs_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();*/

// assignment to parameter ok in initial algorithm
case (lhs,lhprop,rhs,rhprop,_,SCode.INITIAL(),source)
equation
Expand All @@ -202,7 +191,6 @@ algorithm
case (lhs,lhprop,rhs,rhprop,DAE.ATTR(direction=direction),_,source)
equation
DAE.C_VAR() = Types.propAnyConst(lhprop);
true = Config.acceptMetaModelicaGrammar() or (not valueEq(Absyn.INPUT(),direction));
outStatement = makeAssignment2(lhs,lhprop,rhs,rhprop,source);
then outStatement;

Expand Down
15 changes: 7 additions & 8 deletions Compiler/FrontEnd/InstSection.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4624,14 +4624,13 @@ algorithm
Absyn.Exp left;
DAE.Pattern pattern;
DAE.Attributes attr;
Absyn.Direction direction;
DAE.ElementSource source;

// v := expr;
case (cache,env,ih,pre,Absyn.CREF(cr),e_1,eprop,info,source,initial_,impl,unrollForLoops,_)
equation
(cache,SOME((DAE.CREF(ce,t),cprop,attr as DAE.ATTR(direction = direction)))) = Static.elabCref(cache, env, cr, impl, false, pre, info);
Static.checkAssignmentToInput(Dump.printComponentRefStr(cr), direction, info, Static.bDisallowTopLevelInputs);
(cache,SOME((DAE.CREF(ce,t),cprop,attr))) = Static.elabCref(cache, env, cr, impl, false, pre, info);
Static.checkAssignmentToInput(var, attr, env, Static.bDisallowTopLevelInputs, info);
(cache, ce_1) = Static.canonCref(cache, env, ce, impl);
(cache, ce_1) = PrefixUtil.prefixCref(cache, env, ih, pre, ce_1);
(cache, e_1, eprop) = Ceval.cevalIfConstant(cache, env, e_1, eprop, impl, info);
Expand All @@ -4657,8 +4656,8 @@ algorithm
// v[i] := expr (in e.g. for loops)
case (cache,env,ih,pre,Absyn.CREF(cr),e_1,eprop,info,source,initial_,impl,unrollForLoops,_)
equation
(cache,SOME((cre,cprop,attr as DAE.ATTR(direction = direction)))) = Static.elabCref(cache,env, cr, impl,false,pre,info);
Static.checkAssignmentToInput(Dump.printComponentRefStr(cr), direction, info, Static.bDisallowTopLevelInputs);
(cache,SOME((cre,cprop,attr))) = Static.elabCref(cache,env, cr, impl,false,pre,info);
Static.checkAssignmentToInput(var, attr, env, Static.bDisallowTopLevelInputs, info);
(cache,cre2) = PrefixUtil.prefixExp(cache, env, ih, cre, pre);
(cache, e_1, eprop) = Ceval.cevalIfConstant(cache, env, e_1, eprop, impl, info);
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
Expand All @@ -4674,7 +4673,7 @@ algorithm
(cache, e_1 as DAE.CALL(path=_), eprop) = Ceval.cevalIfConstant(cache, env, e_1, eprop, impl, info);
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
(cache,expl_1,cprops,attrs,_) = Static.elabExpCrefNoEvalList(cache, env, expl, impl, NONE(), false, pre, info, Error.getNumErrorMessages());
Static.checkAssignmentToInputs(cache, env, expl, attrs, pre, info, impl);
Static.checkAssignmentToInputs(expl, attrs, env, info);
(cache,expl_2) = PrefixUtil.prefixExpList(cache, env, ih, expl_1, pre);
source = DAEUtil.addElementSourceFileInfo(source, info);
stmt = Algorithm.makeTupleAssignment(expl_2, cprops, e_2, eprop, initial_, source);
Expand All @@ -4690,7 +4689,7 @@ algorithm
(cache, e_1 as DAE.MATCHEXPRESSION(matchType=_), eprop) = Ceval.cevalIfConstant(cache, env, e_1, eprop, impl, info);
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
(cache,expl_1,cprops,attrs,_) = Static.elabExpCrefNoEvalList(cache, env, expl, impl, NONE(), false, pre, info, Error.getNumErrorMessages());
Static.checkAssignmentToInputs(cache, env, expl, attrs, pre, info, impl);
Static.checkAssignmentToInputs(expl, attrs, env, info);
(cache,expl_2) = PrefixUtil.prefixExpList(cache, env, ih, expl_1, pre);
source = DAEUtil.addElementSourceFileInfo(source, info);
stmt = Algorithm.makeTupleAssignment(expl_2, cprops, e_2, eprop, initial_, source);
Expand All @@ -4713,7 +4712,7 @@ algorithm
(cache, e_1 as DAE.TUPLE(PR = expl_1), eprop) = Ceval.cevalIfConstant(cache, env, e_1, eprop, impl, info);
(_,_,_) = Ceval.ceval(Env.emptyCache(),Env.emptyEnv, e_1, false,NONE(), Ceval.MSG(info));
(cache,expl_2,cprops,attrs,_) = Static.elabExpCrefNoEvalList(cache,env, expl, impl,NONE(),false,pre,info, Error.getNumErrorMessages());
Static.checkAssignmentToInputs(cache, env, expl, attrs, pre, info, impl);
Static.checkAssignmentToInputs(expl, attrs, env, info);
(cache,expl_2) = PrefixUtil.prefixExpList(cache, env, ih, expl_2, pre);
eprops = Types.propTuplePropList(eprop);
source = DAEUtil.addElementSourceFileInfo(source, info);
Expand Down
10 changes: 6 additions & 4 deletions Compiler/FrontEnd/Patternm.mo
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ algorithm
Absyn.Direction direction;
Env.Cache cache;
Absyn.Exp lhs;
DAE.Attributes attr;

case (cache,env,Absyn.INTEGER(i),ty,info,_,_)
equation
Expand Down Expand Up @@ -299,17 +300,18 @@ algorithm

case (cache,env,Absyn.AS(id,exp),ty2,info,_,allowTopLevelInputs)
equation
(cache,DAE.TYPES_VAR(ty = ty1, attributes = DAE.ATTR(direction = direction)),_,_) = Lookup.lookupIdent(cache,env,id);
Static.checkAssignmentToInput(id, direction, info, allowTopLevelInputs);
(cache,DAE.TYPES_VAR(ty = ty1, attributes = attr),_,_) = Lookup.lookupIdent(cache,env,id);
lhs = Absyn.CREF(Absyn.CREF_IDENT(id, {}));
Static.checkAssignmentToInput(lhs, attr, env, allowTopLevelInputs, info);
et = validPatternType(ty1,ty2,inLhs,info);
(cache,pattern) = elabPattern(cache,env,exp,ty2,info,Static.bDisallowTopLevelInputs);
pattern = Util.if_(Types.isFunctionType(ty2), DAE.PAT_AS_FUNC_PTR(id,pattern), DAE.PAT_AS(id,et,pattern));
then (cache,pattern);

case (cache,env,Absyn.CREF(Absyn.CREF_IDENT(id,{})),ty2,info,_,allowTopLevelInputs)
equation
(cache,DAE.TYPES_VAR(ty = ty1, attributes = DAE.ATTR(direction = direction)),_,_) = Lookup.lookupIdent(cache,env,id);
Static.checkAssignmentToInput(id, direction, info, allowTopLevelInputs);
(cache,DAE.TYPES_VAR(ty = ty1, attributes = attr),_,_) = Lookup.lookupIdent(cache,env,id);
Static.checkAssignmentToInput(inLhs, attr, env, allowTopLevelInputs, info);
et = validPatternType(ty1,ty2,inLhs,info);
pattern = Util.if_(Types.isFunctionType(ty2), DAE.PAT_AS_FUNC_PTR(id,DAE.PAT_WILD()), DAE.PAT_AS(id,et,DAE.PAT_WILD()));
then (cache,pattern);
Expand Down
78 changes: 49 additions & 29 deletions Compiler/FrontEnd/Static.mo
Original file line number Diff line number Diff line change
Expand Up @@ -289,52 +289,72 @@ algorithm
end elabExp;

public function checkAssignmentToInput
input Absyn.Ident inName;
input Absyn.Direction inDirection;
input Absyn.Exp inExp;
input DAE.Attributes inAttributes;
input Env.Env inEnv;
input Boolean inAllowTopLevelInputs;
input Absyn.Info inInfo;
input Boolean allowTopLevelInputs;
algorithm
_ := matchcontinue(inName, inDirection, inInfo, allowTopLevelInputs)
// assignment to an input, report error - but keep going anyway; bootstrapping needs this :(
case (inName, Absyn.INPUT(), inInfo, false)
_ := matchcontinue(inExp, inAttributes, inEnv, inAllowTopLevelInputs, inInfo)
case (_, _, _, _, _)
equation
Error.addSourceMessage(Error.ASSIGN_READONLY_ERROR, {"input",inName}, inInfo);
true = inAllowTopLevelInputs or not Env.inFunctionScope(inEnv);
then
fail();
case (inName, _, inInfo, _) then ();
();

else
equation
checkAssignmentToInput2(inExp, inAttributes, inInfo);
then
();

end matchcontinue;
end checkAssignmentToInput;

public function checkAssignmentToInputs
input Env.Cache inCache;
input Env.Env inEnv;
input list<Absyn.Exp> inExpsCrefs;
input list<DAE.Attributes> inAttributes;
input Prefix.Prefix inPre;
protected function checkAssignmentToInput2
input Absyn.Exp inExp;
input DAE.Attributes inAttributes;
input Absyn.Info inInfo;
input Boolean impl;
algorithm
_ := matchcontinue(inCache, inEnv, inExpsCrefs, inAttributes, inPre, inInfo, impl)
_ := match(inExp, inAttributes, inInfo)
local
Absyn.ComponentRef cr;
list<Absyn.Exp> rest;
Absyn.Direction direction;
list<DAE.Attributes> restAttr;

case (inCache, inEnv, {}, {}, inPre, inInfo, impl) then ();

case (inCache, inEnv, Absyn.CREF(cr)::rest, DAE.ATTR(direction=direction)::restAttr,inPre, inInfo, impl)
String cr_str;

case (Absyn.CREF(cr), DAE.ATTR(direction = Absyn.INPUT()), _)
equation
cr_str = Dump.printComponentRefStr(cr);
Error.addSourceMessage(Error.ASSIGN_READONLY_ERROR,
{"input", cr_str}, inInfo);
then
fail();

else ();

end match;
end checkAssignmentToInput2;

public function checkAssignmentToInputs
input list<Absyn.Exp> inExpCrefs;
input list<DAE.Attributes> inAttributes;
input Env.Env inEnv;
input Absyn.Info inInfo;
protected
Boolean func_scope;
algorithm
_ := matchcontinue(inExpCrefs, inAttributes, inEnv, inInfo)
case (_, _, _, _)
equation
checkAssignmentToInput(Dump.printComponentRefStr(cr), direction, inInfo, bDisallowTopLevelInputs);
checkAssignmentToInputs(inCache, inEnv, rest, restAttr, inPre, inInfo, impl);
false = Env.inFunctionScope(inEnv);
then
();
case (inCache, inEnv, _::rest, _::restAttr, inPre, inInfo, impl)

else
equation
checkAssignmentToInputs(inCache, inEnv, rest, restAttr, inPre, inInfo, impl);
List.threadMap1_0(inExpCrefs, inAttributes, checkAssignmentToInput2, inInfo);
then
();

end matchcontinue;
end checkAssignmentToInputs;

Expand Down
34 changes: 34 additions & 0 deletions Compiler/Util/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5368,6 +5368,40 @@ algorithm
end match;
end threadMap1_tail;

public function threadMap1_0
"Takes two lists and a function, and applies the function to each element of
the lists in a pairwise fashion. This function also takes an extra argument
which is passed to the mapping function, but returns no result."
input list<ElementType1> inList1;
input list<ElementType2> inList2;
input MapFunc inMapFunc;
input ArgType1 inArg1;

partial function MapFunc
input ElementType1 inElement1;
input ElementType2 inElement2;
input ArgType1 inArg1;
end MapFunc;
algorithm
_ := match(inList1, inList2, inMapFunc, inArg1)
local
ElementType1 e1;
list<ElementType1> rest1;
ElementType2 e2;
list<ElementType2> rest2;
ElementOutType res;

case ({}, {}, _, _) then ();
case (e1 :: rest1, e2 :: rest2, _, _)
equation
inMapFunc(e1, e2, inArg1);
threadMap1_0(rest1, rest2, inMapFunc, inArg1);
then
();
end match;
end threadMap1_0;


public function threadMap2
"Takes two lists and a function and threads (interleaves) and maps the
elements of two lists, creating a new list. This function also takes two
Expand Down

0 comments on commit 34f8bde

Please sign in to comment.