Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Refactoring: Add expression for level in assert-equations


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12866 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 11, 2012
1 parent db38494 commit b1cda5a
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 55 deletions.
7 changes: 4 additions & 3 deletions Compiler/BackEnd/PartFn.mo
Expand Up @@ -213,7 +213,7 @@ algorithm
Ident i;
Absyn.Path p;
list<DAE.Exp> elst,elst_1;
DAE.Exp e,e_1,e1,e1_1,e2,e2_1;
DAE.Exp e,e_1,e1,e1_1,e2,e2_1,e3,e3_1;
DAE.Algorithm alg,alg_1;
DAE.ElementSource source;
list<DAE.Function> dae;
Expand Down Expand Up @@ -329,12 +329,13 @@ algorithm
then
(DAE.COMP(i,elts_1,source,absynCommentOption),dae);

case(DAE.ASSERT(e1,e2,source),dae)
case(DAE.ASSERT(e1,e2,e3,source),dae)
equation
((e1_1,dae)) = Expression.traverseExp(e1,elabExp,dae);
((e2_1,dae)) = Expression.traverseExp(e2,elabExp,dae);
((e3_1,dae)) = Expression.traverseExp(e3,elabExp,dae);
then
(DAE.ASSERT(e1_1,e2_1,source),dae);
(DAE.ASSERT(e1_1,e2_1,e3_1,source),dae);

case(DAE.TERMINATE(e,source),dae)
equation
Expand Down
4 changes: 4 additions & 0 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -268,6 +268,7 @@ public uniontype Element
record ASSERT " The Modelica builtin assert"
Exp condition;
Exp message;
Exp level;
ElementSource source "the origin of the component/equation/algorithm";
end ASSERT;

Expand Down Expand Up @@ -302,6 +303,9 @@ public uniontype Element

end Element;

public constant Exp ASSERTIONLEVEL_ERROR = ENUM_LITERAL(Absyn.QUALIFIED("AssertionLevel",Absyn.IDENT("error")),1);
public constant Exp ASSERTIONLEVEL_WARNING = ENUM_LITERAL(Absyn.QUALIFIED("AssertionLevel",Absyn.IDENT("warning")),2);

public uniontype Function
record FUNCTION " A Modelica function"
Absyn.Path path;
Expand Down
16 changes: 13 additions & 3 deletions Compiler/FrontEnd/DAEDump.mo
Expand Up @@ -2982,8 +2982,8 @@ protected function dumpEquationsStream "function: dumpEquationsStream
algorithm
outStream := match (inElementLst, inStream)
local
String s1,s2,s;
DAE.Exp e1,e2,e;
String s1,s2,s3,s;
DAE.Exp e1,e2,e3,e;
list<DAE.Exp> conds,expl;
list<DAE.Element> xs,xs1,xs2;
list<list<DAE.Element>> tb;
Expand Down Expand Up @@ -3037,14 +3037,24 @@ algorithm
then
str;

case ((DAE.ASSERT(condition=e1,message = e2) :: xs), str)
case ((DAE.ASSERT(condition=e1,message = e2,level = DAE.ENUM_LITERAL(index=1)) :: xs), str)
equation
s1 = ExpressionDump.printExpStr(e1);
s2 = ExpressionDump.printExpStr(e2);
str = IOStream.appendList(str, {" assert(",s1,",",s2,");\n"});
str = dumpEquationsStream(xs, str);
then
str;

case ((DAE.ASSERT(condition=e1,message = e2,level = e3) :: xs), str)
equation
s1 = ExpressionDump.printExpStr(e1);
s2 = ExpressionDump.printExpStr(e2);
s3 = ExpressionDump.printExpStr(e3);
str = IOStream.appendList(str, {" assert(",s1,",",s2,",",s3,");\n"});
str = dumpEquationsStream(xs, str);
then
str;

case (DAE.TERMINATE(message=e1) :: xs, str)
equation
Expand Down
16 changes: 9 additions & 7 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -1943,7 +1943,7 @@ algorithm
list<Absyn.Path> h;
Option<DAE.VariableAttributes> dae_var_attr;
Option<SCode.Comment> comment;
DAE.Exp e_1,e1_1,e2_1,e1,e2,e_2,e;
DAE.Exp e_1,e1_1,e2_1,e1,e2,e_2,e,e3,e_3;
Absyn.Path p;
Absyn.InnerOuter io;
list<DAE.Exp> conds, conds_1;
Expand Down Expand Up @@ -2090,13 +2090,14 @@ algorithm
then
(DAE.COMP(id,elts2,source,comment) :: elts_1);

case ((DAE.ASSERT(condition = e1,message=e2,source = source) :: elts))
case ((DAE.ASSERT(condition = e1,message=e2,level=e3,source = source) :: elts))
equation
elts_1 = toModelicaFormElts(elts);
e_1 = toModelicaFormExp(e1);
e_2 = toModelicaFormExp(e2);
e_3 = toModelicaFormExp(e3);
then
(DAE.ASSERT(e_1,e_2,source) :: elts_1);
(DAE.ASSERT(e_1,e_2,e_3,source) :: elts_1);
case ((DAE.TERMINATE(message = e1,source = source) :: elts))
equation
elts_1 = toModelicaFormElts(elts);
Expand Down Expand Up @@ -3883,7 +3884,7 @@ algorithm
DAE.ConnectorType ct;
DAE.VarParallelism prl;
DAE.VarVisibility prot;
DAE.Exp e,e2,e22,e1,e11,maybeCrExp;
DAE.Exp e,e2,e22,e1,e11,maybeCrExp,e3,e32;
Option<DAE.VariableAttributes> attr;
Option<SCode.Comment> cmt;
Option<DAE.Exp> optExp;
Expand All @@ -3896,7 +3897,7 @@ algorithm
Absyn.Path path;
list<DAE.Exp> expl;
DAE.ElementSource source "the origin of the element";
Type_a extraArg;
Type_a extraArg;

case(DAE.VAR(cr,kind,dir,prl,prot,tp,optExp,dims,ct,source,attr,cmt,io),func,extraArg)
equation
Expand Down Expand Up @@ -4009,11 +4010,12 @@ algorithm
case(elt as DAE.EXTOBJECTCLASS(path,source),func,extraArg)
then (elt,extraArg);

case(DAE.ASSERT(e1,e2,source),func,extraArg)
case(DAE.ASSERT(e1,e2,e3,source),func,extraArg)
equation
((e11,extraArg)) = func((e1,extraArg));
((e22,extraArg)) = func((e2,extraArg));
elt = DAE.ASSERT(e11,e22,source);
((e32,extraArg)) = func((e3,extraArg));
elt = DAE.ASSERT(e11,e22,e32,source);
then
(elt,extraArg);

Expand Down
9 changes: 5 additions & 4 deletions Compiler/FrontEnd/Inline.mo
Expand Up @@ -825,7 +825,7 @@ algorithm
DAE.VarParallelism parallelism;
DAE.VarVisibility protection;
DAE.Type ty;
DAE.Exp binding,binding_1,exp,exp_1,exp1,exp1_1,exp2,exp2_1;
DAE.Exp binding,binding_1,exp,exp_1,exp1,exp1_1,exp2,exp2_1,exp3,exp3_1;
DAE.InstDims dims;
DAE.ConnectorType ct;
Option<DAE.VariableAttributes> variableAttributesOption;
Expand Down Expand Up @@ -959,13 +959,14 @@ algorithm
then
(DAE.COMP(i,elist_1,source,absynCommentOption),true);

case(DAE.ASSERT(exp1,exp2,source) ,fns)
case(DAE.ASSERT(exp1,exp2,exp3,source) ,fns)
equation
(exp1_1,source,b1) = inlineExp(exp1,fns,source);
(exp2_1,source,b2) = inlineExp(exp2,fns,source);
true = b1 or b2;
(exp3_1,source,b3) = inlineExp(exp3,fns,source);
true = b1 or b2 or b3;
then
(DAE.ASSERT(exp1_1,exp2_1,source),true);
(DAE.ASSERT(exp1_1,exp2_1,exp3_1,source),true);

case(DAE.TERMINATE(exp,source),fns)
equation
Expand Down
7 changes: 4 additions & 3 deletions Compiler/FrontEnd/InstExtends.mo
Expand Up @@ -1047,7 +1047,7 @@ algorithm
local
String id;
Absyn.ComponentRef cref,cref1,cref2;
Absyn.Exp exp,exp1,exp2;
Absyn.Exp exp,exp1,exp2,exp3;
list<Absyn.Exp> expl;
list<SCode.EEquation> eql;
list<list<SCode.EEquation>> eqll;
Expand Down Expand Up @@ -1086,11 +1086,12 @@ algorithm
(cache,eql) = fixList(cache,env,eql,ht,fixEEquation);
(cache,whenlst) = fixListTuple2(cache,env,whenlst,ht,fixExp,fixListEEquation);
then (cache,SCode.EQ_WHEN(exp,eql,whenlst,comment,info));
case (cache,env,SCode.EQ_ASSERT(exp1,exp2,comment,info),ht)
case (cache,env,SCode.EQ_ASSERT(exp1,exp2,exp3,comment,info),ht)
equation
(cache,exp1) = fixExp(cache,env,exp1,ht);
(cache,exp2) = fixExp(cache,env,exp2,ht);
then (cache,SCode.EQ_ASSERT(exp1,exp2,comment,info));
(cache,exp3) = fixExp(cache,env,exp3,ht);
then (cache,SCode.EQ_ASSERT(exp1,exp2,exp3,comment,info));
case (cache,env,SCode.EQ_TERMINATE(exp,comment,info),ht)
equation
(cache,exp) = fixExp(cache,env,exp,ht);
Expand Down
22 changes: 14 additions & 8 deletions Compiler/FrontEnd/InstSection.mo
Expand Up @@ -421,10 +421,10 @@ algorithm
SCode.Initial initial_;
Boolean impl;
String i,s;
Absyn.Exp e2,e1,e,ee;
Absyn.Exp e2,e1,e,ee,e3;
list<Absyn.Exp> conditions;
DAE.Exp e1_1,e2_1,e1_2,e2_2,e_1,e_2;
DAE.Properties prop1,prop2;
DAE.Exp e1_1,e2_1,e1_2,e2_2,e_1,e_2,e3_1,e3_2;
DAE.Properties prop1,prop2,prop3;
list<SCode.EEquation> b,fb,el,eel;
list<list<SCode.EEquation>> tb;
list<tuple<Absyn.Exp, list<SCode.EEquation>>> eex;
Expand All @@ -447,7 +447,7 @@ algorithm
Absyn.Info info;
DAE.Element daeElt2;
list<DAE.ComponentRef> lhsCrefs,lhsCrefsRec;
Integer i1,ipriority;
Integer i1,ipriority,index;
list<DAE.Element> daeElts,daeElts3;
DAE.ComponentRef cr_,cr1_,cr2_;
DAE.Type t;
Expand Down Expand Up @@ -704,19 +704,24 @@ algorithm
fail();

// assert statements
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_ASSERT(condition = e1,message = e2,info = info),initial_,impl,graph)
case (cache,env,ih,mod,pre,csets,ci_state,SCode.EQ_ASSERT(condition = e1,message = e2,level = e3, info = info),initial_,impl,graph)
equation
(cache,e1_1,prop1 as DAE.PROP(DAE.T_BOOL(varLst = _),_),_) = Static.elabExp(cache,env, e1, impl,NONE(),true,pre,info) "assert statement" ;
(cache, e1_1, prop1) = Ceval.cevalIfConstant(cache, env, e1_1, prop1, impl, info);
(cache,e2_1,prop2 as DAE.PROP(DAE.T_STRING(varLst = _),_),_) = Static.elabExp(cache,env, e2, impl,NONE(),true,pre,info);
(cache, e2_1, prop2) = Ceval.cevalIfConstant(cache, env, e2_1, prop2, impl, info);
(cache,e3_1,prop3 as DAE.PROP(DAE.T_ENUMERATION(path = Absyn.FULLYQUALIFIED(Absyn.IDENT("AssertionLevel"))),_),_) = Static.elabExp(cache,env, e3, impl,NONE(),true,pre,info);

(cache,e1_1,prop1) = Ceval.cevalIfConstant(cache, env, e1_1, prop1, impl, info);
(cache,e2_1,prop2) = Ceval.cevalIfConstant(cache, env, e2_1, prop2, impl, info);
(cache,e3_1,prop3) = Ceval.cevalIfConstant(cache, env, e3_1, prop3, impl, info);

(cache,e1_2) = PrefixUtil.prefixExp(cache, env, ih, e1_1, pre);
(cache,e2_2) = PrefixUtil.prefixExp(cache, env, ih, e2_1, pre);
(cache,e3_2) = PrefixUtil.prefixExp(cache, env, ih, e3_1, pre);

// set the source of this element
source = DAEUtil.createElementSource(info, Env.getEnvPath(env), PrefixUtil.prefixToCrefOpt(pre), NONE(), NONE());

dae = DAE.DAE({DAE.ASSERT(e1_2,e2_2,source)});
dae = DAE.DAE({DAE.ASSERT(e1_2,e2_2,e3_2,source)});
then
(cache,env,ih,dae,csets,ci_state,graph);

Expand Down Expand Up @@ -4046,6 +4051,7 @@ algorithm
DAE.ASSERT(
DAE.RELATION(crefExp1,DAE.EQUAL(DAE.T_BOOL_DEFAULT),crefExp2,-1,NONE()),
DAE.SCONST("automatically generated from connect"),
DAE.ASSERTIONLEVEL_ERROR,
source) // set the origin of the element
}),graph);

Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/InstTypes.mo
Expand Up @@ -294,6 +294,7 @@ public uniontype Equation
record ASSERT_EQUATION
DAE.Exp condition "The assert condition.";
DAE.Exp message "The message to display if the assert fails.";
DAE.Exp level "Error or warning";
Absyn.Info info;
end ASSERT_EQUATION;

Expand Down
15 changes: 9 additions & 6 deletions Compiler/FrontEnd/SCode.mo
Expand Up @@ -312,6 +312,7 @@ uniontype EEquation
record EQ_ASSERT "the assert equation"
Absyn.Exp condition "the assert condition";
Absyn.Exp message "the assert message";
Absyn.Exp level;
Option<Comment> comment;
Absyn.Info info;
end EQ_ASSERT;
Expand Down Expand Up @@ -1808,7 +1809,7 @@ algorithm
local
String id, id_1;
list<tuple<Absyn.ComponentRef, Integer>> lst,lst_1,lst_2,lst_3;
Absyn.Exp e_1,e_2;
Absyn.Exp e_1,e_2,e_3;
list<Absyn.Exp> eLst;
Absyn.ComponentRef cr_1, cr_2;
list<EEquation> eeqLst;
Expand Down Expand Up @@ -1862,11 +1863,12 @@ algorithm
lst_3=findIteratorInElsewhen(id,ew);
lst=List.flatten({lst_1,lst_2,lst_3});
then lst;
case (id,EQ_ASSERT(condition = e_1, message = e_2))
case (id,EQ_ASSERT(condition = e_1, message = e_2, level = e_3))
equation
lst_1=Absyn.findIteratorInExp(id,e_1);
lst_2=Absyn.findIteratorInExp(id,e_2);
lst=listAppend(lst_1,lst_2);
lst_3=Absyn.findIteratorInExp(id,e_3);
lst=List.flatten({lst_1,lst_2,lst_3});
then lst;
case (id,EQ_TERMINATE(message = e_1))
equation
Expand Down Expand Up @@ -2483,7 +2485,7 @@ algorithm
TraverseFunc traverser;
Argument arg;
tuple<TraverseFunc, Argument> tup;
Absyn.Exp e1, e2;
Absyn.Exp e1, e2, e3;
list<Absyn.Exp> expl1;
list<list<EEquation>> then_branch;
list<EEquation> else_branch, eql;
Expand Down Expand Up @@ -2527,12 +2529,13 @@ algorithm
then
(EQ_WHEN(e1, eql, else_when, comment, info), tup);

case (EQ_ASSERT(e1, e2, comment, info), (traverser, arg))
case (EQ_ASSERT(e1, e2, e3, comment, info), (traverser, arg))
equation
((e1, arg)) = traverser((e1, arg));
((e2, arg)) = traverser((e2, arg));
((e3, arg)) = traverser((e3, arg));
then
(EQ_ASSERT(e1, e2, comment, info), (traverser, arg));
(EQ_ASSERT(e1, e2, e3, comment, info), (traverser, arg));

case (EQ_TERMINATE(e1, comment, info), (traverser, arg))
equation
Expand Down
6 changes: 3 additions & 3 deletions Compiler/FrontEnd/SCodeExpand.mo
Expand Up @@ -677,7 +677,7 @@ protected function expandEquation
algorithm
outElements := matchcontinue(inEquation, inSubscripts, inAccumEl)
local
DAE.Exp rhs, lhs, exp, msg;
DAE.Exp rhs, lhs, exp, msg, level;
DAE.ComponentRef cref1;
DAE.Type ty1;
list<DAE.Element> accum_el;
Expand Down Expand Up @@ -731,11 +731,11 @@ algorithm
then
accum_el;

case (InstTypes.ASSERT_EQUATION(condition = exp, message = msg, info = info), _, _)
case (InstTypes.ASSERT_EQUATION(condition = exp, message = msg, level = level, info = info), _, _)
equation
ty1 = Expression.typeof(exp);
dims = Types.getDimensions(ty1);
accum_el = DAE.ASSERT(exp, msg, DAE.emptyElementSource)::inAccumEl;
accum_el = DAE.ASSERT(exp, msg, level, DAE.emptyElementSource)::inAccumEl;
then
accum_el;

Expand Down
9 changes: 5 additions & 4 deletions Compiler/FrontEnd/SCodeInst.mo
Expand Up @@ -2969,8 +2969,8 @@ protected function instEEquation
algorithm
(outEquation,outFunctions) := matchcontinue (inEquation, inEnv, inPrefix, inFunctions)
local
Absyn.Exp exp1, exp2;
DAE.Exp dexp1, dexp2;
Absyn.Exp exp1, exp2, exp3;
DAE.Exp dexp1, dexp2, dexp3;
Absyn.ComponentRef cref1, cref2;
DAE.ComponentRef dcref1, dcref2;
Absyn.Info info;
Expand Down Expand Up @@ -3044,12 +3044,13 @@ algorithm
then
(InstTypes.WHEN_EQUATION(inst_branches, info), functions);

case (SCode.EQ_ASSERT(condition = exp1, message = exp2, info = info), _, _, functions)
case (SCode.EQ_ASSERT(condition = exp1, message = exp2, level = exp3, info = info), _, _, functions)
equation
(dexp1,functions) = instExp(exp1, inEnv, inPrefix, info, functions);
(dexp2,functions) = instExp(exp2, inEnv, inPrefix, info, functions);
(dexp3,functions) = instExp(exp3, inEnv, inPrefix, info, functions);
then
(InstTypes.ASSERT_EQUATION(dexp1, dexp2, info), functions);
(InstTypes.ASSERT_EQUATION(dexp1, dexp2, dexp3, info), functions);

case (SCode.EQ_TERMINATE(message = exp1, info = info), _, _, functions)
equation
Expand Down
10 changes: 7 additions & 3 deletions Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -1717,7 +1717,7 @@ algorithm
outEEquation := matchcontinue (inEquation,inComment,inInfo,inIsInitial)
local
list<SCode.EEquation> tb_1,fb_1,eb_1,l_1;
Absyn.Exp e,ee,econd_1,cond,econd,e1,e2;
Absyn.Exp e,ee,econd_1,cond,econd,e1,e2,e3;
list<Absyn.EquationItem> tb,fb,ei,eb,l;
SCode.EEquation eq;
list<tuple<Absyn.Exp, list<Absyn.EquationItem>>> eis,elsewhen_;
Expand Down Expand Up @@ -1813,8 +1813,12 @@ algorithm
fail();

case (Absyn.EQ_NORETCALL(functionName = Absyn.CREF_IDENT("assert", _),
functionArgs = Absyn.FUNCTIONARGS(args = {e1,e2},argNames = {})),com,info,_)
then SCode.EQ_ASSERT(e1,e2,com,info);
functionArgs = Absyn.FUNCTIONARGS(args = {e1,e2},argNames = {})),com,info,_)
then SCode.EQ_ASSERT(e1,e2,Absyn.CREF(Absyn.CREF_QUAL("AssertionLevel",{},Absyn.CREF_IDENT("error",{}))),com,info);

case (Absyn.EQ_NORETCALL(functionName = Absyn.CREF_IDENT("assert", _),
functionArgs = Absyn.FUNCTIONARGS(args = {e1,e2,e3},argNames = {})),com,info,_)
then SCode.EQ_ASSERT(e1,e2,e3,com,info);

case (Absyn.EQ_NORETCALL(functionName = Absyn.CREF_IDENT("terminate", _),
functionArgs = Absyn.FUNCTIONARGS(args = {e1},argNames = {})),com,info,_)
Expand Down

0 comments on commit b1cda5a

Please sign in to comment.