Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Fix scalar(size(x))


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17334 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 23, 2013
1 parent a15d519 commit 4f63527
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
26 changes: 26 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -1134,9 +1134,35 @@ algorithm
e = Expression.makeArray(es, tp, false);
then e;

case DAE.CALL(path=Absyn.IDENT("scalar"),expLst=e::{},attr=DAE.CALL_ATTR(ty=tp))
equation
e = simplifyScalar(e,tp);
then e;

end matchcontinue;
end simplifyBuiltinCalls;

protected function simplifyScalar "Handle the scalar() operator"
input DAE.Exp inExp;
input DAE.Type tp;
output DAE.Exp exp;
algorithm
exp := match (inExp,tp)
case (DAE.ARRAY(array={exp}),_)
then Expression.makeBuiltinCall("scalar", {exp}, tp);
case (DAE.MATRIX(matrix={{exp}}),_)
then Expression.makeBuiltinCall("scalar", {exp}, tp);
case (DAE.SIZE(exp=exp,sz=NONE()),_)
equation
(_,{_}) = Types.flattenArrayTypeOpt(Expression.typeof(inExp));
then DAE.SIZE(exp,SOME(DAE.ICONST(1)));
case (_,_)
equation
(_,{}) = Types.flattenArrayTypeOpt(Expression.typeof(inExp));
then inExp;
end match;
end simplifyScalar;

protected function makeNestedReduction
input DAE.Exp inExp;
input String inName;
Expand Down
30 changes: 28 additions & 2 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -5548,13 +5548,16 @@ algorithm
Env.Env env;
Env.Cache cache;
Absyn.Exp aexp;
DAE.Dimensions dims;

case (cache, env, {aexp}, _, _, _, _)
equation
(cache, e, DAE.PROP(tp, c), _) =
elabExp(cache, env, aexp, inImpl, NONE(), true, inPrefix, inInfo);
e = arrayScalar(e, 1, "scalar", inInfo);
scalar_tp = Types.arrayElementType(tp);
(scalar_tp,dims) = Types.flattenArrayTypeOpt(tp);
List.map2_0(dims,checkTypeScalar,tp,inInfo);
e = Util.if_(List.isEmpty(dims), e, Expression.makeBuiltinCall("scalar", {e}, scalar_tp));
(e,_) = ExpressionSimplify.simplify1(e);
then
(cache, e, DAE.PROP(scalar_tp, c));

Expand Down Expand Up @@ -6115,6 +6118,29 @@ algorithm
end match;
end arrayScalar;

protected function checkTypeScalar
"Returns the scalar value of an array, or prints an error message and fails if
any dimension of the array isn't of size 1."
input DAE.Dimension inDim;
input DAE.Type ty;
input Absyn.Info inInfo;
algorithm
_ := match(inDim, ty, inInfo)
local
String ty_str;
// An array with one element.
case (DAE.DIM_INTEGER(1),_,_) then ();
case (DAE.DIM_EXP(_),_,_) then ();
case (DAE.DIM_UNKNOWN(),_,_) then ();
// Any other dimension
else
equation
ty_str = Types.unparseType(ty);
Error.addSourceMessage(Error.INVALID_ARRAY_DIM_IN_SCALAR_OP, {ty_str}, inInfo);
then fail();
end match;
end checkTypeScalar;

public function elabBuiltinHandlerGeneric "
This function dispatches the elaboration of special builtin operators by
returning the appropriate function, see also elab_builtin_handler.
Expand Down
2 changes: 2 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -668,6 +668,8 @@ public constant Message CLASS_ANNOTATION_DOES_NOT_EXIST = MESSAGE(535, SCRIPTING
Util.gettext("Could not find class annotation %s in class %s."));
public constant Message SEPARATE_COMPILATION_PACKAGE_FAILED = MESSAGE(536, SCRIPTING(), ERROR(),
Util.gettext("Failed to compile all functions in package %s."));
public constant Message INVALID_ARRAY_DIM_IN_SCALAR_OP = MESSAGE(537, TRANSLATION(), ERROR(),
Util.gettext("The operator scalar requires all dimension size to be 1, but the input has type %s."));

public constant Message MATCH_SHADOWING = MESSAGE(5001, TRANSLATION(), ERROR(),
Util.gettext("Local variable '%s' shadows another variable."));
Expand Down

0 comments on commit 4f63527

Please sign in to comment.