Skip to content

Commit

Permalink
Allow integer instead of enum constant when compiler is invoked with …
Browse files Browse the repository at this point in the history
…+intEnumConversion

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19543 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
henrikt-ma committed Mar 11, 2014
1 parent 180e7f5 commit 50b0d52
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
61 changes: 50 additions & 11 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -4286,13 +4286,14 @@ algorithm
list<DAE.Exp> elist_1,elist,inputs;
DAE.Type at,t;
Boolean sc, a;
Integer nmax;
Integer nmax, oi;
DAE.Dimension dim1, dim2, dim11, dim22;
DAE.Dimensions dims;
Type ty1,ty2,t1,t2,t_1,t_2,ty0,ty;
DAE.Exp begin_1,step_1,stop_1,begin,step,stop,e_1,e,exp;
list<list<DAE.Exp>> ell_1,ell,elist_big;
list<DAE.Type> tys_1,tys1,tys2;
String name;
list<String> l;
list<DAE.Var> v;
Absyn.Path path,path1,path2;
Expand All @@ -4306,7 +4307,7 @@ algorithm
list<DAE.Element> localDecls;
DAE.TypeSource ts,ts1,ts2;
list<DAE.Var> els1,els2;
Absyn.Path p1,p2;
Absyn.Path p1,p2,tp;

// if we expect notTuple and we get Tuple do DAE.TSUB(e, 1)
// we try subtype of the first tuple element with the other type!
Expand Down Expand Up @@ -4503,21 +4504,23 @@ algorithm
then
(DAE.TUPLE(elist_1),DAE.T_TUPLE(tys_1,ts2));

// Convert an integer literal to an enumeration
// This is widely used in Modelica.Electrical.Digital
/* Commented out for when someone complains...
// Implicit conversion from Integer literal to an enumeration
// This is not a valid Modelica conversion, but was widely used in the past,
// by, for instance, Modelica.Electrical.Digital.
// Enable with +intEnumConversion.
case (exp as DAE.ICONST(oi),
DAE.T_INTEGER(varLst = _),
DAE.T_ENUMERATION(index=_, path=tp, names = l, source = ts2),
printFailtrace)
equation
// TODO! FIXME! check boundaries if the integer literal is not outside the enum range
DAE.T_INTEGER(source = _),
t2 as DAE.T_ENUMERATION(path = tp, names = l),
_)
equation
true = Config.intEnumConversion();
// It would be good to have the source location of exp here, so that we could pass it to typeConvertIntToEnumCheck.
true = typeConvertIntToEnumCheck(exp, t2); // Will warn or report error depending on whether oi is out of range.
// select from enum list:
name = listNth(l, oi-1); // listNth indexes from 0
tp = Absyn.joinPaths(tp, Absyn.IDENT(name));
then
(DAE.ENUM_LITERAL(tp, oi),expected);
*/

// Implicit conversion from Integer to Real
case (e,
Expand Down Expand Up @@ -7837,4 +7840,40 @@ algorithm
end match;
end isEmptyOrNoRetcall;

protected function typeConvertIntToEnumCheck "
Deal with the invalid conversions from Integer to enumeration.
If the Integer corresponds to the toInteger value of an enumeration constant,
just give a warning, otherwise report an error.
Returns false if an error was reported, otherwise true.
"
input DAE.Exp exp;
input DAE.Type expected;
output Boolean conversionOK;
algorithm
conversionOK := matchcontinue (exp, expected)
local
Integer oi;
Absyn.Path tp;
list<String> l;
String pathStr, intStr, enumConst;
case (DAE.ICONST(oi),
DAE.T_ENUMERATION(path = tp, names = l))
equation
true = (1 <= oi and oi <= listLength(l));
pathStr = Absyn.pathString(tp);
intStr = intString(oi);
enumConst = listNth(l, oi);
Error.addMessage(Error.INTEGER_ENUMERATION_CONVERSION_WARNING, {intStr, pathStr, enumConst});
then true;
case (DAE.ICONST(oi),
DAE.T_ENUMERATION(path = tp))
equation
pathStr = Absyn.pathString(tp);
intStr = intString(oi);
Error.addMessage(Error.INTEGER_ENUMERATION_OUT_OF_RANGE, {pathStr, intStr});
then false;
end matchcontinue;
end typeConvertIntToEnumCheck;


end Types;
8 changes: 7 additions & 1 deletion Compiler/Util/Config.mo
Expand Up @@ -563,5 +563,11 @@ algorithm
outScalarizeBindings := Flags.getConfigBool(Flags.SCALARIZE_BINDINGS);
end scalarizeBindings;

end Config;
public function intEnumConversion
output Boolean outIntEnumConversion;
algorithm
outIntEnumConversion := Flags.getConfigBool(Flags.INT_ENUM_CONVERSION);
end intEnumConversion;


end Config;
4 changes: 4 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -710,6 +710,10 @@ public constant Message NON_STANDARD_OPERATOR_CLASS_DIRECTORY = MESSAGE(539, TRA
Util.gettext("classDirectory() is a non-standard operator that was replaced by Modelica.Utilities.Files.loadResource(uri) before it was added to the language specification."));
public constant Message PACKAGE_DUPLICATE_CHILDREN = MESSAGE(540, TRANSLATION(), ERROR(),
Util.gettext("The same class is defined in multiple files: %s."));
public constant Message INTEGER_ENUMERATION_CONVERSION_WARNING = MESSAGE(541, TRANSLATION(), WARNING(),
Util.gettext("Integer (%s) to enumeration (%s) conversion is not valid Modelica, please use enumeration constant (%s) instead."));
public constant Message INTEGER_ENUMERATION_OUT_OF_RANGE = MESSAGE(542, TRANSLATION(), ERROR(),
Util.gettext("The Integer to %s conversion failed, as the Integer value %s does not correspond to the toInteger value of any enumeration constant."));

public constant Message MATCH_SHADOWING = MESSAGE(5001, TRANSLATION(), ERROR(),
Util.gettext("Local variable '%s' shadows another variable."));
Expand Down
11 changes: 8 additions & 3 deletions Compiler/Util/Flags.mo
Expand Up @@ -865,8 +865,12 @@ constant ConfigFlag GENERATE_SYMBOLIC_JACOBIAN = CONFIG_FLAG(55, "generateSymbol
constant ConfigFlag GENERATE_SYMBOLIC_LINEARIZATION = CONFIG_FLAG(56, "generateSymbolicLinearization",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Generates symbolic linearization matrixes A,B,C,D for linear model:\n\t\t\\dot x = Ax + Bu\n\t\ty = Cx +Du"));



constant ConfigFlag INT_ENUM_CONVERSION = CONFIG_FLAG(57, "intEnumConversion",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Allow Integer to enumeration conversion."));


// This is a list of all configuration flags. A flag can not be used unless it's
// in this list, and the list is checked at initialization so that all flags are
// sorted by index (and thus have unique indices).
Expand Down Expand Up @@ -926,7 +930,8 @@ constant list<ConfigFlag> allConfigFlags = {
REWRITE_RULES_FILE,
REPLACE_HOMOTOPY,
GENERATE_SYMBOLIC_JACOBIAN,
GENERATE_SYMBOLIC_LINEARIZATION
GENERATE_SYMBOLIC_LINEARIZATION,
INT_ENUM_CONVERSION
};

public function new
Expand Down

0 comments on commit 50b0d52

Please sign in to comment.