Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit d82185e

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Various fixes.
- Changed ExpandExp.expandCref to return an array with the same dimensions as the input cref, even when the cref's type has zero-dimensions. - Added case for Expression.TYPENAME in Expression.typeOf. - Added better error checking when deducing unknown dimensions. - Added check for negative dimension sizes. Belonging to [master]: - OpenModelica/OpenModelica#130 - #3042
1 parent 2ee23a6 commit d82185e

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Compiler/NFFrontEnd/NFExpandExp.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public
121121
case Expression.CREF(cref = ComponentRef.CREF())
122122
algorithm
123123
if Type.hasZeroDimension(crefExp.ty) then
124-
arrayExp := Expression.makeEmptyArray(Type.ARRAY(Type.arrayElementType(crefExp.ty), {Dimension.fromInteger(0)}));
124+
arrayExp := Expression.makeEmptyArray(crefExp.ty);
125125
expanded := true;
126126
elseif Type.hasKnownSize(crefExp.ty) then
127127
subs := expandCref2(crefExp.cref);

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ public
778778
case ENUM_LITERAL() then exp.ty;
779779
case CLKCONST() then Type.CLOCK();
780780
case CREF() then exp.ty;
781+
case TYPENAME() then exp.ty;
781782
case ARRAY() then exp.ty;
782783
case RANGE() then exp.ty;
783784
case TUPLE() then exp.ty;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ algorithm
522522
Dimension dim;
523523
Binding b;
524524
Type ty;
525+
TypingError ty_err;
525526

526527
// Print an error when a dimension that's currently being processed is
527528
// found, which indicates a dependency loop. Another way of handling this
@@ -608,7 +609,7 @@ algorithm
608609
end if;
609610
end if;
610611

611-
dim := match b
612+
(dim, ty_err) := match b
612613
// Print an error if there's no binding.
613614
case Binding.UNBOUND()
614615
algorithm
@@ -621,18 +622,25 @@ algorithm
621622
// to get the dimension we're looking for.
622623
case Binding.UNTYPED_BINDING()
623624
algorithm
624-
dim := typeExpDim(b.bindingExp, index + Binding.countPropagatedDims(b),
625-
ExpOrigin.setFlag(origin, ExpOrigin.DIMENSION), info);
625+
(dim, _, ty_err) := typeExpDim(b.bindingExp, index + Binding.countPropagatedDims(b),
626+
ExpOrigin.setFlag(origin, ExpOrigin.DIMENSION), info);
626627
then
627-
dim;
628+
(dim, ty_err);
628629

629630
// A typed binding, get the dimension from the binding's type.
630631
case Binding.TYPED_BINDING()
632+
then nthDimensionBoundsChecked(b.bindingType, index + Binding.countPropagatedDims(b));
633+
end match;
634+
635+
() := match ty_err
636+
case TypingError.OUT_OF_BOUNDS()
631637
algorithm
632-
dim := nthDimensionBoundsChecked(b.bindingType, index + Binding.countPropagatedDims(b));
638+
Error.addSourceMessage(Error.DIMENSION_DEDUCTION_FROM_BINDING_FAILURE,
639+
{String(index), InstNode.name(component), Binding.toString(b)}, info);
633640
then
634-
dim;
641+
fail();
635642

643+
else ();
636644
end match;
637645

638646
// Make sure the dimension is constant evaluted, and also mark it as structural.
@@ -644,6 +652,12 @@ algorithm
644652
then
645653
Dimension.fromExp(exp, dim.var);
646654

655+
case Dimension.UNKNOWN()
656+
algorithm
657+
Error.addInternalError(getInstanceName() + " returned unknown dimension in a non-function context", info);
658+
then
659+
fail();
660+
647661
else dim;
648662
end match;
649663

@@ -654,8 +668,31 @@ algorithm
654668
// Other kinds of dimensions are already typed.
655669
else dimension;
656670
end match;
671+
672+
verifyDimension(dimension, component, info);
657673
end typeDimension;
658674

675+
function verifyDimension
676+
input Dimension dimension;
677+
input InstNode component;
678+
input SourceInfo info;
679+
algorithm
680+
() := match dimension
681+
case Dimension.INTEGER()
682+
algorithm
683+
// Check that integer dimensions are not negative.
684+
if dimension.size < 0 then
685+
Error.addSourceMessage(Error.NEGATIVE_DIMENSION_INDEX,
686+
{String(dimension.size), InstNode.name(component)}, info);
687+
fail();
688+
end if;
689+
then
690+
();
691+
692+
else ();
693+
end match;
694+
end verifyDimension;
695+
659696
function getRecordElementBinding
660697
"Tries to fetch the binding for a given record field by using the binding of
661698
the record instance."
@@ -1885,13 +1922,6 @@ algorithm
18851922
case Expression.SIZE()
18861923
algorithm
18871924
(exp, exp_ty, _) := typeExp(sizeExp.exp, next_origin, info);
1888-
1889-
// The first argument must be an array of any type.
1890-
if not Type.isArray(exp_ty) then
1891-
Error.addSourceMessage(Error.INVALID_ARGUMENT_TYPE_FIRST_ARRAY, {"size"}, info);
1892-
fail();
1893-
end if;
1894-
18951925
sizeType := Type.sizeType(exp_ty);
18961926
then
18971927
(Expression.SIZE(exp, NONE()), sizeType, Variability.PARAMETER);

Compiler/Util/Error.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,8 @@ public constant Message INST_RECURSION_LIMIT_REACHED = MESSAGE(349, TRANSLATION(
827827
Util.gettext("Recursion limit reached while instantiating ‘%s‘."));
828828
public constant Message WHEN_IF_VARIABLE_MISMATCH = MESSAGE(350, TRANSLATION(), ERROR(),
829829
Util.gettext("The branches of an if-equation inside a when-equation must have the same set of component references on the left-hand side."));
830+
public constant Message DIMENSION_DEDUCTION_FROM_BINDING_FAILURE = MESSAGE(351, TRANSLATION(), ERROR(),
831+
Util.gettext("Dimension %s of ‘%s‘ could not be deduced from the component's binding equation ‘%s‘."));
830832
public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSLATION(), WARNING(),
831833
Util.gettext("The initial conditions are not fully specified. %s."));
832834
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),

0 commit comments

Comments
 (0)