Skip to content

Commit 946d35c

Browse files
authored
Check for mutally cyclic components for MM (#902)
* Avoid reodering but check cycles for MM * Added tests for mutally recursive constants for MM
1 parent 979b43b commit 946d35c

File tree

6 files changed

+87
-8
lines changed

6 files changed

+87
-8
lines changed

OMCompiler/Compiler/FrontEnd/InstUtil.mo

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,8 @@ end addNomod;
14091409

14101410
public function sortElementList
14111411
"Sorts constants and parameters by dependencies, so that they are instantiated
1412-
before they are used."
1412+
before they are used.
1413+
For MetaModelica we just check for cycles. We do not do reordering"
14131414
input output list<Element> inElements;
14141415
input FCore.Graph inEnv;
14151416
input Boolean isFunctionScope;
@@ -1419,16 +1420,14 @@ protected
14191420
list<tuple<Element, list<Element>>> cycles;
14201421
list<tuple<Element, list<Element>>> g;
14211422
algorithm
1422-
// no sorting for meta-modelica!
1423+
// sort the elements according to the dependencies
1424+
g := Graph.buildGraph(inElements, getElementDependencies, (inElements,isFunctionScope));
1425+
(outE, cycles) := Graph.topologicalSort(g, isElementEqual);
14231426
if not Config.acceptMetaModelicaGrammar() then
1424-
// sort the elements according to the dependencies
1425-
g := Graph.buildGraph(inElements, getElementDependencies, (inElements,isFunctionScope));
1426-
(outE, cycles) := Graph.topologicalSort(g, isElementEqual);
1427-
// printGraph(inEnv, g, outE, cycles);
14281427
// append the elements in the cycles as they might not actually be cycles, but they depend on elements not in the list (i.e. package constants, etc)!
14291428
inElements := listAppend(outE, List.map(cycles, Util.tuple21));
1430-
checkCyclicalComponents(cycles, inEnv);
14311429
end if;
1430+
checkCyclicalComponents(cycles, inEnv);
14321431
end sortElementList;
14331432

14341433
protected function printGraph

testsuite/metamodelica/meta/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ MatchShadowing.mo \
7777
OptimizeContinue.mo \
7878
OptimizeMatchToIfExp.mo \
7979
OptionInteractive.mos \
80+
PackageConst1.mos \
8081
PartialFn1.mo \
8182
PartialFn2.mo \
8283
PartialFn3.mos \
@@ -133,7 +134,8 @@ Uniontype14.mos \
133134
Uniontype15.mos \
134135
UniontypeFunc1.mos \
135136
UniontypeConst1.mos \
136-
UniontypeConst2.mos
137+
UniontypeConst2.mos \
138+
UniontypeConst3.mos \
137139
# test that currently fail. Move up when fixed.
138140
# Run make testfailing
139141
FAILINGTESTFILES=
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package PKT
2+
constant Real CONST1 = CONST2 * 3;
3+
constant Real CONST2 = CONST1 + 3;
4+
function f
5+
output Real ro;
6+
algorithm
7+
ro := CONST1 + CONST2;
8+
end f;
9+
end PKT;
10+
11+
function test
12+
output Real x;
13+
algorithm
14+
x := PKT.f();
15+
end test;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// name: PackageConst1
2+
// keywords: package const recursion
3+
// status: correct
4+
// cflags: -g=MetaModelica -d=gen
5+
//
6+
// Tests self recursive constants inside of packages
7+
//
8+
9+
loadFile("PackageConst1.mo"); getErrorString();
10+
test(); getErrorString();
11+
12+
// Result:
13+
// true
14+
// ""
15+
//
16+
// "Error: Cyclically dependent constants or parameters found in scope PKT: {CONST2,CONST1} (ignore with -d=ignoreCycles).
17+
// [metamodelica/meta/PackageConst1.mo:14:3-14:15:writable] Error: Class PKT.f not found in scope test (looking for a function or record).
18+
// Error: Cyclically dependent constants or parameters found in scope PKT: {CONST2,CONST1} (ignore with -d=ignoreCycles).
19+
// [metamodelica/meta/PackageConst1.mo:14:3-14:15:writable] Error: Class PKT.f not found in scope test (looking for a function or record).
20+
// Error: Cyclically dependent constants or parameters found in scope PKT: {CONST2,CONST1} (ignore with -d=ignoreCycles).
21+
// [metamodelica/meta/PackageConst1.mo:14:3-14:15:writable] Error: Class PKT.f not found in scope test (looking for a function or record).
22+
// "
23+
24+
// endResult
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
uniontype UT
2+
constant Real CONST1 = CONST2 * 3;
3+
constant Real CONST2 = CONST1 + 3;
4+
function f
5+
output Real ro;
6+
algorithm
7+
ro := CONST1 + CONST2;
8+
end f;
9+
end UT;
10+
11+
function test
12+
output Real x;
13+
algorithm
14+
x := UT.f();
15+
end test;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// name: UniontypeConst3
2+
// keywords: uniontype const recursion
3+
// status: correct
4+
// cflags: -g=MetaModelica -d=gen
5+
//
6+
// Tests self recursive constants inside of uniontypes.
7+
//
8+
9+
loadFile("UniontypeConst3.mo"); getErrorString();
10+
test(); getErrorString();
11+
12+
// Result:
13+
// true
14+
// ""
15+
//
16+
//"Error: Cyclically dependent constants or parameters found in scope UT: {CONST2,CONST1} (ignore with -d=ignoreCycles).
17+
//[metamodelica/meta/UniontypeConst3.mo:14:3-14:14:writable] Error: Class UT.f not found in scope test (looking for a function or record).
18+
//Error: Cyclically dependent constants or parameters found in scope UT: {CONST2,CONST1} (ignore with -d=ignoreCycles).
19+
//[metamodelica/meta/UniontypeConst3.mo:14:3-14:14:writable] Error: Class UT.f not found in scope test (looking for a function or record).
20+
//Error: Cyclically dependent constants or parameters found in scope UT: {CONST2,CONST1} (ignore with -d=ignoreCycles).
21+
//[metamodelica/meta/UniontypeConst3.mo:14:3-14:14:writable] Error: Class UT.f not found in scope test (looking for a function or record).
22+
//"
23+
24+
// endResult

0 commit comments

Comments
 (0)