Skip to content

Commit

Permalink
- Added flag to do strict checks so that inputs/outputs of function a…
Browse files Browse the repository at this point in the history
…nd match expression are the same

git-svn-id: https://openmodelica.org/svn/OpenModelica/branches/sjoelund-functiontree@6498 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 21, 2010
1 parent bde7b84 commit df0dc73
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions Compiler/Absyn.mo
Expand Up @@ -1902,6 +1902,7 @@ algorithm
s2 = printComponentRefStr(child);
s1 = "." +& s2;
then s1;
case (WILD()) then "_";
end matchcontinue;
end printComponentRefStr;

Expand Down
4 changes: 3 additions & 1 deletion Compiler/Error.mo
Expand Up @@ -278,6 +278,7 @@ public constant ErrorID META_INVALID_PATTERN=5012;
public constant ErrorID META_MATCH_INPUT_OUTPUT_NON_CREF=5013;
public constant ErrorID META_MATCH_GENERAL_FAILURE=5014;
public constant ErrorID META_CONS_TYPE_MATCH=5015;
public constant ErrorID META_STRICT_RML_MATCH_IN_OUT=5016;

protected constant list<tuple<Integer, MessageType, Severity, String>> errorTable=
{(SYNTAX_ERROR,SYNTAX(),ERROR(),"Syntax error near: %s"),
Expand Down Expand Up @@ -627,7 +628,8 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
(META_MATCH_GENERAL_FAILURE,TRANSLATION(),ERROR(),"Failed to elaborate match expression %s := %s"),
(TUPLE_ASSIGN_CREFS_ONLY,TRANSLATION(),ERROR(),"Tuple assignment only allowed for tuple of component references in lhs (in %s)"),
(META_CONS_TYPE_MATCH,TRANSLATION(),ERROR(),"Failed to match types of cons expression %s. The head has type %s and the tail %s."),
(LOOKUP_FUNCTION_GOT_CLASS,TRANSLATION(),ERROR(),"Looking for a function %s but found a %s.")
(LOOKUP_FUNCTION_GOT_CLASS,TRANSLATION(),ERROR(),"Looking for a function %s but found a %s."),
(META_STRICT_RML_MATCH_IN_OUT,TRANSLATION(),ERROR(),"Strict RML enforces match expression input and output to be the same as the function's. %s.")
};

protected import ErrorExt;
Expand Down
83 changes: 83 additions & 0 deletions Compiler/Inst.mo
Expand Up @@ -9891,6 +9891,7 @@ algorithm
case (cache,env,ih,mod,pre,csets,(c as SCode.CLASS(name = n,restriction = r)),inst_dims)
equation
failure(SCode.R_RECORD() = r);
true = strictRMLCheck(RTOpts.debugFlag("rml"),c);
(cache,env,ih,funs) = implicitFunctionInstantiation2(cache,env,ih,mod,pre,csets,c,inst_dims);
cache = Env.addDaeFunction(cache, funs);
then (cache,env,ih);
Expand Down Expand Up @@ -14971,4 +14972,86 @@ algorithm
end matchcontinue;
end connectImplicitlyConnectedFlow2;

protected function strictRMLCheck
"If we are checking for strict RML, and function containing a single statement
that is a match expression must be on the form (outputs) := matchcontinue (inputs).
RML does not check this even though it's translated to this internally, so we
must check for it to warn the user."
input Boolean b;
input SCode.Class c;
output Boolean isOK;
algorithm
isOK := matchcontinue (b,c)
local
list<SCode.Element> elts,inelts,outelts;
list<String> innames,outnames;
list<Absyn.Exp> outcrefs,increfs;
Absyn.Exp comp,inputs;
Absyn.Info info;
case (false,_) then true;
case (_,SCode.CLASS(info = info, restriction = SCode.R_FUNCTION(), classDef = SCode.PARTS(elementLst = elts, normalAlgorithmLst = {SCode.ALGORITHM({SCode.ALG_ASSIGN(assignComponent = comp, value = Absyn.MATCHEXP(inputExp = inputs))})})))
equation
outcrefs = MetaUtil.extractListFromTuple(comp,0);
increfs = MetaUtil.extractListFromTuple(inputs,0);
inelts = Util.listSelect1(elts, Absyn.INPUT(), SCode.isComponentWithDirection);
outelts = Util.listSelect1(elts, Absyn.OUTPUT(), SCode.isComponentWithDirection);
innames = Util.listMap(inelts, SCode.elementName);
outnames = Util.listMap(outelts, SCode.elementName);
then strictRMLCheck2(increfs,outcrefs,innames,outnames,info);
case (_,_) then true;
end matchcontinue;
end strictRMLCheck;

protected function strictRMLCheck2
"If we are checking for strict RML, and function containing a single statement
that is a match expression must be on the form (outputs) := matchcontinue (inputs).
RML does not check this even though it's translated to this internally, so we
must check for it to warn the user."
input list<Absyn.Exp> increfs;
input list<Absyn.Exp> outcrefs;
input list<String> innames;
input list<String> outnames;
input Absyn.Info info;
output Boolean b;
algorithm
b := matchcontinue (increfs,outcrefs,innames,outnames,info)
local
list<String> names;
case (increfs,outcrefs,innames,outnames,info)
equation
true = (listLength(increfs) <> listLength(innames));
Error.addSourceMessage(Error.META_STRICT_RML_MATCH_IN_OUT, {"Number of input arguments don't match"}, info);
then false;
case (increfs,outcrefs,innames,outnames as _::_,info)
equation
true = (listLength(outcrefs) <> listLength(outnames));
Error.addSourceMessage(Error.META_STRICT_RML_MATCH_IN_OUT, {"Number of output arguments don't match"}, info);
then false;
case (increfs,outcrefs,innames,outnames,info)
equation
failure(_ = Util.listMap(increfs, Absyn.expCref));
Error.addSourceMessage(Error.META_STRICT_RML_MATCH_IN_OUT, {"Input expression was not a tuple of component references"}, info);
then false;
case (increfs,outcrefs,innames,outnames,info)
equation
failure(_ = Util.listMap(outcrefs, Absyn.expCref));
Error.addSourceMessage(Error.META_STRICT_RML_MATCH_IN_OUT, {"Output expression was not a tuple of component references"}, info);
then false;
case (increfs,outcrefs,innames,outnames,info)
equation
names = Util.listMap(increfs, Absyn.expComponentRefStr);
failure(equality(names = innames));
Error.addSourceMessage(Error.META_STRICT_RML_MATCH_IN_OUT, {"The input does not match"}, info);
then false;
case (increfs,{Absyn.CREF(Absyn.WILD())},innames,{},info) then true;
case (increfs,outcrefs,innames,outnames,info)
equation
names = Util.listMap(outcrefs, Absyn.expComponentRefStr);
failure(equality(names = outnames));
Error.addSourceMessage(Error.META_STRICT_RML_MATCH_IN_OUT, {"The output does not match"}, info);
then false;
case (_,_,_,_,_) then true;
end matchcontinue;
end strictRMLCheck2;

end Inst;
13 changes: 13 additions & 0 deletions Compiler/SCode.mo
Expand Up @@ -2758,5 +2758,18 @@ algorithm
end matchcontinue;
end emptyModOrEquality;

public function isComponentWithDirection
input Element elt;
input Absyn.Direction dir1;
output Boolean b;
algorithm
b := matchcontinue (elt,dir1)
local
Absyn.Direction dir2;
case (COMPONENT(attributes = ATTR(direction = dir2)),dir1) then directionEqual(dir1,dir2);
case (_,_) then false;
end matchcontinue;
end isComponentWithDirection;

end SCode;

0 comments on commit df0dc73

Please sign in to comment.