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

Commit 46fdf1a

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Restrict cardinality usage.
- Implemented stricter checks for where cardinality is allowed to be used, to improve the user feedback. Belonging to [master]: - OpenModelica/OpenModelica#99 - #2949 - OpenModelica/OpenModelica-testsuite#1128
1 parent 4961b36 commit 46fdf1a

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

Compiler/NFFrontEnd/NFBuiltinCall.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,15 @@ protected
12811281
Function fn;
12821282
InstNode node;
12831283
algorithm
1284+
// cardinality may only be used in a condition of an assert or
1285+
// if-statement/equation (the specification says only if-statement,
1286+
// but e.g. the MSL only uses them in if-equations and asserts).
1287+
if not (ExpOrigin.flagSet(origin, ExpOrigin.CONDITION) and
1288+
(ExpOrigin.flagSet(origin, ExpOrigin.IF) or
1289+
ExpOrigin.flagSet(origin, ExpOrigin.ASSERT))) then
1290+
Error.addSourceMessageAndFail(Error.INVALID_CARDINALITY_CONTEXT, {}, info);
1291+
end if;
1292+
12841293
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
12851294
assertNoNamedParams("cardinality", named_args, info);
12861295

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ package ExpOrigin
135135
constant Type SUBEXPRESSION = intBitLShift(1, 16); // Part of a larger expression.
136136
constant Type CONNECT = intBitLShift(1, 17); // Part of connect argument.
137137
constant Type NOEVENT = intBitLShift(1, 18); // Part of noEvent argument.
138+
constant Type ASSERT = intBitLShift(1, 19); // Part of assert argument.
138139

139140
// Combined flags:
140141
constant Type EQ_SUBEXPRESSION = intBitOr(EQUATION, SUBEXPRESSION);
@@ -2402,9 +2403,11 @@ algorithm
24022403
case Equation.ASSERT()
24032404
algorithm
24042405
info := ElementSource.getInfo(eq.source);
2405-
e1 := typeOperatorArg(eq.condition, Type.BOOLEAN(), origin, "assert", "condition", 1, info);
2406-
e2 := typeOperatorArg(eq.message, Type.STRING(), origin, "assert", "message", 2, info);
2407-
e3 := typeOperatorArg(eq.level, NFBuiltin.ASSERTIONLEVEL_TYPE, origin, "assert", "level", 3, info);
2406+
next_origin := ExpOrigin.setFlag(origin, ExpOrigin.ASSERT);
2407+
e1 := typeOperatorArg(eq.condition, Type.BOOLEAN(),
2408+
ExpOrigin.setFlag(next_origin, ExpOrigin.CONDITION), "assert", "condition", 1, info);
2409+
e2 := typeOperatorArg(eq.message, Type.STRING(), next_origin, "assert", "message", 2, info);
2410+
e3 := typeOperatorArg(eq.level, NFBuiltin.ASSERTIONLEVEL_TYPE, next_origin, "assert", "level", 3, info);
24082411
then
24092412
Equation.ASSERT(e1, e2, e3, eq.source);
24102413

@@ -2562,7 +2565,7 @@ algorithm
25622565
list<tuple<Expression, list<Statement>>> tybrs;
25632566
InstNode iterator;
25642567
MatchKind mk;
2565-
Integer next_origin;
2568+
ExpOrigin.Type next_origin, cond_origin;
25662569
SourceInfo info;
25672570

25682571
case Statement.ASSIGNMENT()
@@ -2602,11 +2605,14 @@ algorithm
26022605

26032606
case Statement.IF()
26042607
algorithm
2608+
next_origin := ExpOrigin.setFlag(origin, ExpOrigin.IF);
2609+
cond_origin := ExpOrigin.setFlag(next_origin, ExpOrigin.CONDITION);
2610+
26052611
tybrs := list(
26062612
match br case(cond, body)
26072613
algorithm
2608-
e1 := typeCondition(cond, origin, st.source, Error.IF_CONDITION_TYPE_ERROR);
2609-
sts1 := list(typeStatement(bst, origin) for bst in body);
2614+
e1 := typeCondition(cond, cond_origin, st.source, Error.IF_CONDITION_TYPE_ERROR);
2615+
sts1 := list(typeStatement(bst, next_origin) for bst in body);
26102616
then (e1, sts1);
26112617
end match
26122618
for br in st.branches);
@@ -2631,9 +2637,11 @@ algorithm
26312637
case Statement.ASSERT()
26322638
algorithm
26332639
info := ElementSource.getInfo(st.source);
2634-
e1 := typeOperatorArg(st.condition, Type.BOOLEAN(), origin, "assert", "condition", 1, info);
2635-
e2 := typeOperatorArg(st.message, Type.STRING(), origin, "assert", "message", 2, info);
2636-
e3 := typeOperatorArg(st.level, NFBuiltin.ASSERTIONLEVEL_TYPE, origin, "assert", "level", 3, info);
2640+
next_origin := ExpOrigin.setFlag(origin, ExpOrigin.ASSERT);
2641+
e1 := typeOperatorArg(st.condition, Type.BOOLEAN(),
2642+
ExpOrigin.setFlag(next_origin, ExpOrigin.CONDITION), "assert", "condition", 1, info);
2643+
e2 := typeOperatorArg(st.message, Type.STRING(), next_origin, "assert", "message", 2, info);
2644+
e3 := typeOperatorArg(st.level, NFBuiltin.ASSERTIONLEVEL_TYPE, next_origin, "assert", "level", 3, info);
26372645
then
26382646
Statement.ASSERT(e1, e2, e3, st.source);
26392647

@@ -2718,12 +2726,13 @@ protected
27182726
list<Equation> eql;
27192727
Variability accum_var = Variability.CONSTANT, var;
27202728
list<Equation.Branch> bl = {}, bl2 = {};
2721-
Integer next_origin = origin;
2729+
ExpOrigin.Type next_origin = ExpOrigin.setFlag(origin, ExpOrigin.IF);
2730+
ExpOrigin.Type cond_origin = ExpOrigin.setFlag(next_origin, ExpOrigin.CONDITION);
27222731
algorithm
27232732
// Type the conditions of all the branches.
27242733
for b in branches loop
27252734
Equation.Branch.BRANCH(cond, _, eql) := b;
2726-
(cond, var) := typeCondition(cond, origin, source, Error.IF_CONDITION_TYPE_ERROR);
2735+
(cond, var) := typeCondition(cond, cond_origin, source, Error.IF_CONDITION_TYPE_ERROR);
27272736

27282737
if var > Variability.PARAMETER or isNonExpandableExp(cond) then
27292738
// If the condition doesn't fulfill the requirements for allowing

Compiler/Util/Error.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ public constant Message INVALID_REDUCTION_TYPE = MESSAGE(344, TRANSLATION(), ERR
818818
Util.gettext("Invalid expression ‘%s‘ of type %s in %s reduction, expected %s."));
819819
public constant Message INVALID_COMPONENT_PREFIX = MESSAGE(345, TRANSLATION(), ERROR(),
820820
Util.gettext("Prefix ‘%s‘ on component ‘%s‘ not allowed in class specialization ‘%s‘."));
821+
public constant Message INVALID_CARDINALITY_CONTEXT = MESSAGE(346, TRANSLATION(), ERROR(),
822+
Util.gettext("cardinality may only be used in the condition of an if-statement/equation or an assert."));
821823
public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSLATION(), WARNING(),
822824
Util.gettext("The initial conditions are not fully specified. %s."));
823825
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),

0 commit comments

Comments
 (0)