Skip to content

Commit

Permalink
Improve conditional array type handling (#8414)
Browse files Browse the repository at this point in the history
- Return unknown dimensions for conditional array types in
  Type.arrayDims instead of an empty array, so at least the number of
  dimensions is correct (needed by `cat`).
- Handle type checking when both the expected and the actual type is a
  conditonal array type.
  • Loading branch information
perost committed Jan 13, 2022
1 parent 6e1b5f7 commit cd76794
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -695,6 +695,7 @@ public
case ARRAY() then ty.dimensions;
case FUNCTION() then arrayDims(Function.returnType(ty.fn));
case METABOXED() then arrayDims(ty.ty);
case CONDITIONAL_ARRAY() then List.fill(Dimension.UNKNOWN(), dimensionCount(ty.trueType));
else {};
end match;
end arrayDims;
Expand Down
53 changes: 51 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFTypeCheck.mo
Expand Up @@ -1623,6 +1623,13 @@ algorithm
then
compatibleType;

case Type.CONDITIONAL_ARRAY()
algorithm
(expression, compatibleType, matchKind) :=
matchConditionalArrayTypes(actualType, expectedType, expression, allowUnknown);
then
compatibleType;

else
algorithm
Error.assertion(false, getInstanceName() + " got unknown type.", sourceInfo());
Expand Down Expand Up @@ -2240,6 +2247,48 @@ algorithm
end matchConditionalArrayExp;

function matchConditionalArrayTypes
input Type actualType;
input Type expectedType;
input output Expression exp;
input Boolean allowUnknown;
output Type compatibleType;
output MatchKind matchKind;
protected
Type actual_true_ty, actual_false_ty;
Type expected_true_ty, expected_false_ty;
Type true_ty, false_ty;
Expression true_exp, false_exp;
algorithm
Type.CONDITIONAL_ARRAY(trueType = actual_true_ty, falseType = actual_false_ty) := actualType;
Type.CONDITIONAL_ARRAY(trueType = expected_true_ty, falseType = expected_false_ty) := expectedType;

() := match exp
case Expression.IF()
algorithm
(true_exp, true_ty, matchKind) :=
matchTypes(actual_true_ty, expected_true_ty, exp.trueBranch, allowUnknown);

if not isCompatibleMatch(matchKind) then
compatibleType := actualType;
return;
end if;

(false_exp, false_ty, matchKind) :=
matchTypes(actual_false_ty, expected_false_ty, exp.falseBranch, allowUnknown);

if not isCompatibleMatch(matchKind) then
compatibleType := actualType;
return;
end if;

compatibleType := Type.CONDITIONAL_ARRAY(true_ty, false_ty, NFType.Branch.NONE);
exp := Expression.IF(compatibleType, exp.condition, true_exp, false_exp);
then
();
end match;
end matchConditionalArrayTypes;

function matchConditionalArrayTypes_cast
input Type condType;
input Type expectedType;
input output Expression exp;
Expand Down Expand Up @@ -2300,7 +2349,7 @@ algorithm
exp := Expression.typeCast(exp, cond_ty);
end if;
end if;
end matchConditionalArrayTypes;
end matchConditionalArrayTypes_cast;

function matchTypes_cast
input Type actualType;
Expand Down Expand Up @@ -2390,7 +2439,7 @@ algorithm
case (Type.CONDITIONAL_ARRAY(), _)
algorithm
(expression, compatibleType, matchKind) :=
matchConditionalArrayTypes(actualType, expectedType, expression, allowUnknown);
matchConditionalArrayTypes_cast(actualType, expectedType, expression, allowUnknown);
then
(compatibleType, matchKind);

Expand Down
29 changes: 29 additions & 0 deletions testsuite/flattening/modelica/scodeinst/IfExpression14.mo
@@ -0,0 +1,29 @@
// name: IfExpression14
// keywords:
// status: correct
// cflags: -d=newInst
//

model IfExpression14
parameter Boolean cond = false;
Real[3] a = if cond then {1.0} else {1.0, 2.0, 3.0};
Real x[:] = cat(1, a, a);
end IfExpression14;

// Result:
// class IfExpression14
// parameter Boolean cond = false;
// Real a[1];
// Real a[2];
// Real a[3];
// Real x[1];
// Real x[2];
// Real x[3];
// Real x[4];
// Real x[5];
// Real x[6];
// equation
// a = {1.0, 2.0, 3.0};
// x = {a[1], a[2], a[3], a[1], a[2], a[3]};
// end IfExpression14;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -673,6 +673,7 @@ IfExpression10.mo \
IfExpression11.mo \
IfExpression12.mo \
IfExpression13.mo \
IfExpression14.mo \
IfEquationInvalidCond1.mo \
ih1.mo \
ih2.mo \
Expand Down

0 comments on commit cd76794

Please sign in to comment.