Skip to content

Commit

Permalink
- Added some basic sanity checking of type errors.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15062 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Feb 5, 2013
1 parent afcb08d commit 17296b2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Compiler/FrontEnd/Algorithm.mo
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ algorithm
DAE.ComponentRef cr;
DAE.Type lt, rt;
Absyn.Direction direction;
Absyn.Info info;

// assign to parameter in algorithm okay if record
case ((lhs as DAE.CREF(componentRef=cr)), lhprop, rhs, rhprop, _, SCode.NON_INITIAL(), _)
Expand Down Expand Up @@ -204,8 +205,10 @@ algorithm
rhs_str = ExpressionDump.printExpStr(rhs);
lt_str = Types.unparseType(lt);
rt_str = Types.unparseType(rt);
info = DAEUtil.getElementSourceFileInfo(source);
Types.typeErrorSanityCheck(lt_str, rt_str, info);
Error.addSourceMessage(Error.ASSIGN_TYPE_MISMATCH_ERROR,
{lhs_str, rhs_str, lt_str, rt_str}, DAEUtil.getElementSourceFileInfo(source));
{lhs_str, rhs_str, lt_str, rt_str}, info);
then
fail();

Expand Down
13 changes: 9 additions & 4 deletions Compiler/FrontEnd/Inst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9175,6 +9175,7 @@ algorithm
Prefix.Prefix pr;
DAE.Type bt;
String v_str, b_str, et_str, bt_str;
Absyn.Info info;

case (DAE.T_COMPLEX(complexClassType=ClassInf.EXTERNAL_OBJ(_)),
DAE.MOD(eqModOption = SOME(DAE.TYPED(modifierAsExp = e))),_,_,_,_)
Expand Down Expand Up @@ -9207,8 +9208,10 @@ algorithm
b_str = ExpressionDump.printExpStr(e);
et_str = Types.unparseType(tp);
bt_str = Types.unparseType(bt);
info = DAEUtil.getElementSourceFileInfo(source);
Types.typeErrorSanityCheck(et_str, bt_str, info);
Error.addSourceMessage(Error.VARIABLE_BINDING_TYPE_MISMATCH,
{v_str, b_str, et_str, bt_str}, DAEUtil.getElementSourceFileInfo(source));
{v_str, b_str, et_str, bt_str}, info);
then
fail();

Expand Down Expand Up @@ -14962,6 +14965,7 @@ algorithm
e_str = ExpressionDump.printExpStr(e);
e_str_1 = stringAppend("=", e_str);
str = PrefixUtil.printPrefixStrIgnoreNoPre(inPrefix) +& "." +& componentName;
Types.typeErrorSanityCheck(e_tp_str, tp_str, info);
Error.addSourceMessage(Error.MODIFIER_TYPE_MISMATCH_ERROR, {str,tp_str,e_str_1,e_tp_str}, info);
then
fail();
Expand Down Expand Up @@ -15122,7 +15126,7 @@ algorithm
Values.Value val;
DAE.Type ty,ty2;
DAE.Ident ident;
String binding_str, exptected_type_str, given_type_str;
String binding_str, expected_type_str, given_type_str;


// Array type and each prefix => return the expression and value.
Expand Down Expand Up @@ -15152,10 +15156,11 @@ algorithm
SOME(DAE.TYPED(modifierAsExp = exp, properties = DAE.PROP(type_ = ty)))))), ty2,_)
equation
binding_str = ExpressionDump.printExpStr(exp);
exptected_type_str = Types.unparseType(ty2);
expected_type_str = Types.unparseType(ty2);
given_type_str = Types.unparseType(ty);
Types.typeErrorSanityCheck(given_type_str, expected_type_str, inInfo);
Error.addSourceMessage(Error.VARIABLE_BINDING_TYPE_MISMATCH,
{ident, binding_str, exptected_type_str, given_type_str}, inInfo);
{ident, binding_str, expected_type_str, given_type_str}, inInfo);
then
fail();

Expand Down
6 changes: 5 additions & 1 deletion Compiler/FrontEnd/InstSection.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ algorithm
String e1_str,t1_str,e2_str,t2_str,s1,s2;
DAE.Const c;
DAE.TupleConst tp;
Absyn.Info info;

/* TODO: Weird hack to make backend happy */
case (e1 as DAE.CREF(componentRef=_), (p1 as DAE.PROP(type_ = t1 as DAE.T_COMPLEX(complexClassType=ClassInf.RECORD(_)))),
Expand Down Expand Up @@ -1366,7 +1367,9 @@ algorithm
t2_str = Types.unparseType(t2);
s1 = stringAppendList({e1_str,"=",e2_str});
s2 = stringAppendList({t1_str,"=",t2_str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {s1,s2}, DAEUtil.getElementSourceFileInfo(source));
info = DAEUtil.getElementSourceFileInfo(source);
Types.typeErrorSanityCheck(t1_str, t2_str, info);
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {s1,s2}, info);
Debug.fprintln(Flags.FAILTRACE, "- InstSection.instEqEquation failed with type mismatch in equation: " +& s1 +& " tys: " +& s2);
then
fail();
Expand Down Expand Up @@ -4976,6 +4979,7 @@ algorithm
rhs_str = ExpressionDump.printExpStr(e_2);
lt_str = Types.unparseType(lt);
rt_str = Types.unparseType(rt);
Types.typeErrorSanityCheck(lt_str, rt_str, info);
Error.addSourceMessage(Error.ASSIGN_TYPE_MISMATCH_ERROR,{lhs_str,rhs_str,lt_str,rt_str}, info);
then
fail();
Expand Down
4 changes: 4 additions & 0 deletions Compiler/FrontEnd/Static.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,7 @@ algorithm
elt_str = stringAppendList({"[",str,"]"});
t1_str = Types.unparseType(t1);
t2_str = Types.unparseType(t2);
Types.typeErrorSanityCheck(t1_str, t2_str, info);
Error.addSourceMessage(Error.TYPE_MISMATCH_ARRAY_EXP, {sp,e_str,t1_str,elt_str,t2_str}, info);
then
fail();
Expand Down Expand Up @@ -2950,6 +2951,7 @@ algorithm
el_str = ExpressionDump.printListStr(els, ExpressionDump.printExpStr, ", ");
t1_str = Types.unparseType(t1);
t2_str = Types.unparseType(t2);
Types.typeErrorSanityCheck(t1_str, t2_str, info);
Error.addSourceMessage(Error.TYPE_MISMATCH_MATRIX_EXP, {pre_str,el_str,t1_str,t2_str}, info);
then
fail();
Expand Down Expand Up @@ -11829,6 +11831,7 @@ algorithm
e2_str = ExpressionDump.printExpStr(e3);
t2_str = Types.unparseType(t3);
pre_str = PrefixUtil.printPrefixStr3(pre);
Types.typeErrorSanityCheck(t1_str, t2_str, inInfo);
Error.addSourceMessage(Error.TYPE_MISMATCH_IF_EXP, {pre_str,e1_str,t1_str,e2_str,t2_str}, inInfo);
then
fail();
Expand Down Expand Up @@ -14162,6 +14165,7 @@ algorithm
equation
e_str = ExpressionDump.printExpStr(inExp);
t_str = Types.unparseType(ty);
Types.typeErrorSanityCheck(t_str, "Integer", inInfo);
Error.addSourceMessage(Error.ARRAY_DIMENSION_INTEGER,
{e_str, t_str}, inInfo);
then
Expand Down
22 changes: 22 additions & 0 deletions Compiler/FrontEnd/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7293,6 +7293,7 @@ algorithm
equation
str1 = unparseType(ty1);
str2 = unparseType(ty2);
typeErrorSanityCheck(str1, str2, info);
Error.addSourceMessage(Error.ARRAY_TYPE_MISMATCH,{str1,str2},info);
then fail();
end matchcontinue;
Expand Down Expand Up @@ -7715,4 +7716,25 @@ algorithm
end matchcontinue;
end hasBinding;

public function typeErrorSanityCheck
input String inType1;
input String inType2;
input Absyn.Info inInfo;
algorithm
_ := matchcontinue(inType1, inType2, inInfo)
case (_, _, _)
equation
false = stringEq(inType1, inType2);
then
();

else
equation
Error.addSourceMessage(Error.ERRONEOUS_TYPE_ERROR, {inType1}, inInfo);
then
fail();

end matchcontinue;
end typeErrorSanityCheck;

end Types;
2 changes: 2 additions & 0 deletions Compiler/Util/Error.mo
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ public constant Message EXP_TYPE_MISMATCH = MESSAGE(245, TRANSLATION(), ERROR(),
Util.gettext("Expression %1 has type %3, expected type %2."));
public constant Message PACKAGE_ORDER_DUPLICATES = MESSAGE(246, TRANSLATION(), ERROR(),
Util.gettext("Found duplicate names in package.order file: %s."));
public constant Message ERRONEOUS_TYPE_ERROR = MESSAGE(247, TRANSLATION, ERROR(),
Util.gettext("Got type mismatch error, but matching types %s.\nThis is a ***COMPILER BUG***, please report it to https://trac.openmodelica.org/OpenModelica."));
public constant Message UNBOUND_PARAMETER_WITH_START_VALUE_WARNING = MESSAGE(499, TRANSLATION(), WARNING(),
Util.gettext("Parameter %s has no value, and is fixed during initialization (fixed=true), using available start value (start=%s) as default value"));
public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(),
Expand Down

0 comments on commit 17296b2

Please sign in to comment.