Skip to content

Commit 20d7f29

Browse files
authored
Improve getAllSubtypeOf API (#13889)
- Return an empty list if the class being searched for does not exist. - Rename `AllLoadedClasses` used as default value by some APIs to `__OpenModelica_AllLoadedClasses` to reduce the risk of name conflicts. - Use the top scope as scope in NFApi.frontEndLookup when searching all loaded classes instead of failing.
1 parent 8ae74ad commit 20d7f29

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

OMCompiler/Compiler/Script/Interactive.mo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,7 @@ end removeExtendsModifiersInElement;
35373537
public function mkFullyQual
35383538
input GraphicEnvCache env;
35393539
input Absyn.Path ipath;
3540+
input Boolean failOnError = false;
35403541
output FCore.Cache ocache;
35413542
output Absyn.Path opath;
35423543
protected
@@ -3546,7 +3547,7 @@ algorithm
35463547
if Flags.isSet(Flags.NF_API) then
35473548
ocache := cacheFromGraphicEnvCache(env);
35483549
(program, cpath) := cacheProgramAndPath(env);
3549-
opath := NFApi.mkFullyQual(program, cpath, ipath);
3550+
opath := NFApi.mkFullyQual(program, cpath, ipath, failOnError);
35503551
else
35513552
(ocache, opath) := Inst.makeFullyQualified(cacheFromGraphicEnvCache(env), envFromGraphicEnvCache(env), ipath);
35523553
end if;

OMCompiler/Compiler/Script/InteractiveUtil.mo

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,7 @@ end getElementAttributeValues;
23642364
public function qualifyPath
23652365
input GraphicEnvCache inEnv;
23662366
input Absyn.Path inPath;
2367+
input Boolean failOnError = false;
23672368
output Absyn.Path outPath;
23682369
protected
23692370
String n;
@@ -2378,12 +2379,16 @@ algorithm
23782379
algorithm
23792380
try
23802381
if Flags.isSet(Flags.NF_API) then
2381-
(_, outPath) := Interactive.mkFullyQual(inEnv, inPath);
2382+
(_, outPath) := Interactive.mkFullyQual(inEnv, inPath, failOnError);
23822383
else
23832384
outPath := qualifyType(Interactive.envFromGraphicEnvCache(inEnv), inPath);
23842385
end if;
23852386
else
2386-
outPath := inPath;
2387+
if failOnError then
2388+
fail();
2389+
else
2390+
outPath := inPath;
2391+
end if;
23872392
end try;
23882393
then
23892394
outPath;
@@ -3864,9 +3869,10 @@ algorithm
38643869

38653870
try
38663871
genv := createEnvironment(inProgram, NONE(), inParentClass);
3867-
fqpath := qualifyPath(genv, inClass);
3872+
fqpath := qualifyPath(genv, inClass, failOnError = true);
38683873
else
3869-
fqpath := inClass;
3874+
paths := {};
3875+
return;
38703876
end try;
38713877

38723878
paths := {};

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ function mkFullyQual
395395
input Absyn.Program absynProgram;
396396
input Absyn.Path classPath;
397397
input Absyn.Path pathToQualify;
398+
input Boolean failOnError = false;
398399
output Absyn.Path qualPath = pathToQualify;
399400
protected
400401
InstNode top, expanded_cls, cls;
@@ -442,15 +443,19 @@ algorithm
442443
FlagsUtil.set(Flags.SCODE_INST, b);
443444
FlagsUtil.set(Flags.NF_SCALARIZE, s);
444445
else
445-
// do not fail, just return the Absyn path
446-
qualPath := pathToQualify;
447-
448446
if not Flags.isSet(Flags.NF_API_NOISE) then
449447
ErrorExt.rollBack("NFApi.mkFullyQual");
450448
end if;
451449

452450
FlagsUtil.set(Flags.SCODE_INST, b);
453451
FlagsUtil.set(Flags.NF_SCALARIZE, s);
452+
453+
if failOnError then
454+
fail();
455+
else
456+
// do not fail, just return the Absyn path
457+
qualPath := pathToQualify;
458+
end if;
454459
end try;
455460

456461
if Flags.isSet(Flags.EXEC_STAT) then
@@ -712,10 +717,15 @@ algorithm
712717
name := AbsynUtil.pathString(classPath);
713718

714719
(program, top) := mkTop(absynProgram, name);
715-
cls := Inst.lookupRootClass(classPath, top, FAST_CONTEXT);
716720

717-
// Expand the class.
718-
expanded_cls := NFInst.expand(cls, FAST_CONTEXT);
721+
if AbsynUtil.pathEqual(classPath, Absyn.IDENT("AllLoadedClasses")) then
722+
expanded_cls := top;
723+
else
724+
cls := Inst.lookupRootClass(classPath, top, FAST_CONTEXT);
725+
726+
// Expand the class.
727+
expanded_cls := NFInst.expand(cls, FAST_CONTEXT);
728+
end if;
719729

720730
if Flags.isSet(Flags.EXEC_STAT) then
721731
execStat("NFApi.frontEndLookup_dispatch("+ name +")");

OMCompiler/Compiler/Stubs/NFApi.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function mkFullyQual
5757
input Absyn.Program absynProgram;
5858
input Absyn.Path classPath;
5959
input Absyn.Path pathToQualify;
60+
input Boolean failOnError = false;
6061
output Absyn.Path qualPath = pathToQualify;
6162
end mkFullyQual;
6263

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// name: GetAllSubtypeOf4
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
// Tests the getAllSubtypeOf API function.
7+
//
8+
9+
loadString("
10+
model M1 = Base;
11+
12+
model M
13+
M1 m1;
14+
end M;
15+
");
16+
17+
getAllSubtypeOf(Base, M, false, false, false);
18+
getErrorString();
19+
20+
// Result:
21+
// true
22+
// {}
23+
// ""
24+
// endResult

testsuite/openmodelica/interactive-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ForStatement8.mos \
4848
GetAllSubtypeOf1.mos \
4949
GetAllSubtypeOf2.mos \
5050
GetAllSubtypeOf3.mos \
51+
GetAllSubtypeOf4.mos \
5152
getClassComment.mos \
5253
getClassNames.mos \
5354
getCommandLineOptions.mos \

0 commit comments

Comments
 (0)