Skip to content

Commit dfec779

Browse files
committed
Improve NFApi.updateMovedPath/Cref
Fixes OpenModelica#15065
1 parent 3fb9017 commit dfec779

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,21 +2868,24 @@ algorithm
28682868
if AbsynUtil.pathIsFullyQualified(qualified_path) then
28692869
qualified_path := AbsynUtil.makeNotFullyQualified(qualified_path);
28702870

2871-
if AbsynUtil.pathIsQual(qualified_path) then
2872-
// If the path is qualified it needs to be joined with the original path,
2873-
// but we remove any part of the path that's the same as the destination.
2871+
if AbsynUtil.pathIsIdent(qualified_path) and
2872+
AbsynUtil.pathFirstIdent(qualified_path) == AbsynUtil.pathFirstIdent(env.destinationPath) then
2873+
// Special case, the path refers to the destination package, e.g. moving A.B.C into A.
2874+
path := AbsynUtil.pathRest(path);
2875+
else
2876+
// Remove any part of the qualified path that's the same as the destination.
28742877
opt_path := AbsynUtil.pathStripSamePrefix(qualified_path, env.destinationPath);
28752878

2879+
// Replace the first identifier in the original path with the remaining qualified path.
28762880
if isSome(opt_path) then
28772881
SOME(qualified_path) := opt_path;
28782882

2879-
if AbsynUtil.pathIsQual(qualified_path) then
2880-
path := AbsynUtil.joinPaths(AbsynUtil.pathPrefix(qualified_path), path);
2883+
if AbsynUtil.pathIsQual(path) then
2884+
path := AbsynUtil.joinPaths(qualified_path, AbsynUtil.pathRest(path));
2885+
else
2886+
path := qualified_path;
28812887
end if;
28822888
end if;
2883-
elseif AbsynUtil.pathFirstIdent(qualified_path) == AbsynUtil.pathFirstIdent(env.destinationPath) then
2884-
// Special case, the path refers to the destination package, e.g. moving path A.B.C into A.
2885-
path := AbsynUtil.pathRest(path);
28862889
end if;
28872890
end if;
28882891
end updateMovedPath;
@@ -2967,6 +2970,7 @@ function updateMovedCref
29672970
input MoveEnv env;
29682971
protected
29692972
Absyn.Path qualified_path;
2973+
Absyn.ComponentRef qualified_cref;
29702974
Option<Absyn.Path> opt_path;
29712975
algorithm
29722976
if AbsynUtil.crefIsFullyQualified(cref) or AbsynUtil.crefIsWild(cref) then
@@ -2987,21 +2991,25 @@ algorithm
29872991
if AbsynUtil.pathIsFullyQualified(qualified_path) then
29882992
qualified_path := AbsynUtil.makeNotFullyQualified(qualified_path);
29892993

2990-
if AbsynUtil.pathIsQual(qualified_path) then
2991-
// If the path is qualified it needs to be joined with the original cref,
2992-
// but we remove any part of the path that's the same as the destination.
2994+
if AbsynUtil.pathIsIdent(qualified_path) and
2995+
AbsynUtil.pathFirstIdent(qualified_path) == AbsynUtil.pathFirstIdent(env.destinationPath) then
2996+
// Special case, the cref refers to the destination package, e.g. moving A.B.C into A.
2997+
cref := AbsynUtil.crefStripFirst(cref);
2998+
else
2999+
// Remove any part of the qualified path that's the same as the destination.
29933000
opt_path := AbsynUtil.pathStripSamePrefix(qualified_path, env.destinationPath);
29943001

3002+
// Replace the first identifier in the original cref with the remaining qualified path.
29953003
if isSome(opt_path) then
29963004
SOME(qualified_path) := opt_path;
3005+
qualified_cref := AbsynUtil.pathToCref(qualified_path);
29973006

2998-
if AbsynUtil.pathIsQual(qualified_path) then
2999-
cref := AbsynUtil.joinCrefs(AbsynUtil.pathToCref(AbsynUtil.pathPrefix(qualified_path)), cref);
3007+
if AbsynUtil.crefIsQual(cref) then
3008+
cref := AbsynUtil.joinCrefs(qualified_cref, AbsynUtil.crefStripFirst(cref));
3009+
else
3010+
cref := qualified_cref;
30003011
end if;
30013012
end if;
3002-
elseif AbsynUtil.pathFirstIdent(qualified_path) == AbsynUtil.pathFirstIdent(env.destinationPath) then
3003-
// Special case, the cref refers to the destination package, e.g. moving path A.B.C into A.
3004-
cref := AbsynUtil.crefStripFirst(cref);
30053013
end if;
30063014
end if;
30073015
end updateMovedCref;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// name: CopyClass8
2+
// keywords:
3+
// status: correct
4+
//
5+
6+
loadString("
7+
package Modelica
8+
package Constants
9+
constant Real pi = 3.14;
10+
end Constants;
11+
end Modelica;
12+
13+
package ImportDup
14+
import C = Modelica.Constants;
15+
import M = Modelica;
16+
17+
model Orig
18+
parameter Real foo = C.pi*2;
19+
package Consts = C;
20+
package PM = M;
21+
end Orig;
22+
end ImportDup;
23+
");
24+
25+
copyClass(ImportDup.Orig, "Duplicate");
26+
list(Duplicate);
27+
28+
// Result:
29+
// true
30+
// true
31+
// "model Duplicate
32+
// parameter Real foo = Modelica.Constants.pi*2;
33+
// package Consts = Modelica.Constants;
34+
// package PM = Modelica;
35+
// end Duplicate;"
36+
// endResult

testsuite/openmodelica/interactive-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ CopyClass4.mos \
3434
CopyClass5.mos \
3535
CopyClass6.mos \
3636
CopyClass7.mos \
37+
CopyClass8.mos \
3738
CopyClassInvalid1.mos \
3839
CopyClassRedeclare1.mos \
3940
DefaultComponentName.mos \

0 commit comments

Comments
 (0)