Skip to content

Commit

Permalink
[NF] Improve handling of matrix().
Browse files Browse the repository at this point in the history
- Use matrix(A) = promote(A, 2) when ndims(A) < 2,
  and matrix(A) = A when ndims(A) = 2.

Belonging to [master]:
  - OpenModelica/OMCompiler#2612
  - OpenModelica/OpenModelica-testsuite#1016
  • Loading branch information
perost authored and OpenModelica-Hudson committed Aug 21, 2018
1 parent 07b398d commit f558b3b
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions Compiler/NFFrontEnd/NFBuiltinCall.mo
Expand Up @@ -1039,7 +1039,7 @@ protected
Function fn;
list<Dimension> dims;
Dimension dim1, dim2;
Integer i;
Integer i, ndims;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("matrix", named_args, info);
Expand All @@ -1051,32 +1051,32 @@ protected

(arg, ty, variability) := Typing.typeExp(listHead(args), origin, info);
dims := Type.arrayDims(ty);
ndims := listLength(dims);

if ndims < 2 then
// matrix(A) where A is a scalar or vector returns promote(A, 2).
(callExp, ty) := Expression.promote(arg, ty, 2);
elseif ndims == 2 then
// matrix(A) where A is a matrix just returns A.
callExp := arg;
else
// matrix requires all but the first two dimensions to have size 1.
dim1 :: dim2 :: dims := dims;
i := 3;

for dim in dims loop
if Dimension.isKnown(dim) and Dimension.size(dim) > 1 then
Error.addSourceMessageAndFail(Error.INVALID_ARRAY_DIM_IN_CONVERSION_OP,
{String(i), "matrix", "1", Dimension.toString(dim)}, info);
end if;

dims := match listLength(dims)
case 0 then {Dimension.fromInteger(1), Dimension.fromInteger(1)};
case 1 then {listHead(dims), Dimension.fromInteger(1)};
case 2 then dims;
else
algorithm
// matrix requires all but the first two dimensions to have size 1.
dim1 :: dim2 :: dims := dims;
i := 3;

for dim in dims loop
if Dimension.isKnown(dim) and Dimension.size(dim) > 1 then
Error.addSourceMessageAndFail(Error.INVALID_ARRAY_DIM_IN_CONVERSION_OP,
{String(i), "matrix", "1", Dimension.toString(dim)}, info);
end if;

i := i + 1;
end for;
then
{dim1, dim2};
end match;
i := i + 1;
end for;

ty := Type.ARRAY(Type.arrayElementType(ty), dims);
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
ty := Type.ARRAY(Type.arrayElementType(ty), {dim1, dim2});
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
end if;
end typeMatrixCall;

function typeCatCall
Expand Down

0 comments on commit f558b3b

Please sign in to comment.