Skip to content

Commit

Permalink
[NF] Type-check array constructor
Browse files Browse the repository at this point in the history
This resolves ticket:4779.

Belonging to [master]:
  - OpenModelica/OMCompiler#2233
  - OpenModelica/OpenModelica-testsuite#859
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Feb 26, 2018
1 parent ee3f9cf commit ca288a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -1674,8 +1674,8 @@ protected
if dim_size == -1 then
dim_size := Dimension.size(dim);
else
Error.addSourceMessageAndFail(Error.INVALID_ARRAY_DIM_IN_CONVERSION_OP,
{String(i), "vector", "1", Dimension.toString(dim)}, info);
Error.addSourceMessageAndFail(Error.NF_VECTOR_INVALID_DIMENSIONS,
{Type.toString(ty), toString(call)}, info);
end if;
end if;
end if;
Expand Down
37 changes: 31 additions & 6 deletions Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -1465,19 +1465,44 @@ function typeArray
output Variability variability = Variability.CONSTANT;
protected
Expression exp;
list<Expression> expl = {};
list<Expression> expl = {}, expl2 = {};
Variability var;
Type ty;
Type ty1 = Type.UNKNOWN(), ty2, ty3;
list<Type> tys = {};
MatchKind mk;
Integer n=1;
algorithm
for e in elements loop
// TODO: Type checking.
(exp, ty, var) := typeExp(e, origin, info);
(exp, ty2, var) := typeExp(e, origin, info);
variability := Prefixes.variabilityMax(var, variability);
(, ty3, mk) := TypeCheck.matchTypes(ty2, ty1, exp, allowUnknown = true);
if TypeCheck.isIncompatibleMatch(mk) then
// Try the other way around to get the super-type of the array
(, ty3, mk) := TypeCheck.matchTypes(ty1, ty2, exp, allowUnknown = false);
if TypeCheck.isCompatibleMatch(mk) then
ty1 := ty3;
end if;
else
ty1 := ty3;
end if;
expl := exp :: expl;
tys := ty2 :: tys;
n := n+1;
end for;
// Give the actual error-messages here after we got the super-type of the array
for e in expl loop
ty2::tys := tys;
(exp, , mk) := TypeCheck.matchTypes(ty2, ty1, e);
expl2 := exp::expl2;
n := n-1;
if TypeCheck.isIncompatibleMatch(mk) then
Error.addSourceMessage(Error.NF_ARRAY_TYPE_MISMATCH, {String(n), Expression.toString(exp), Type.toString(ty2), Type.toString(ty1)}, info);
fail();
end if;
end for;

arrayType := Type.liftArrayLeft(ty, Dimension.fromExpList(expl));
arrayExp := Expression.ARRAY(arrayType, listReverse(expl));
arrayType := Type.liftArrayLeft(ty1, Dimension.fromExpList(expl2));
arrayExp := Expression.ARRAY(arrayType, expl2);
end typeArray;

function typeRange
Expand Down
4 changes: 4 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -935,6 +935,10 @@ public constant Message MIXED_DETERMINED = MESSAGE(583, SYMBOLIC(), ERROR(),
Util.gettext("The given system is mixed-determined. [index > %s]\nPlease checkout the option \"--maxMixedDeterminedIndex\"."));
public constant Message STACK_OVERFLOW_DETAILED = MESSAGE(584, SCRIPTING(), ERROR(),
Util.gettext("Stack overflow occurred while evaluating %s:\n%s"));
public constant Message NF_VECTOR_INVALID_DIMENSIONS = MESSAGE(585, TRANSLATION(), ERROR(),
Util.gettext("Invalid dimensions %s in %s, no more than one dimension may have size > 1."));
public constant Message NF_ARRAY_TYPE_MISMATCH = MESSAGE(586, TRANSLATION(), ERROR(),
Util.gettext("Array types mismatch. Argument %s (%s) has type %s whereas previous arguments have 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 ca288a2

Please sign in to comment.