Skip to content

Commit 88eb889

Browse files
sjoelundadrpo
authored andcommitted
Add scripting API for conversion versions
This allows for example to see which versions do not need to be converted if a newer MSL is loaded.
1 parent 31c432b commit 88eb889

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3862,6 +3862,18 @@ Returns the libraries used by the package {{\"Library1\",\"Version\"},{\"Library
38623862
preferredView="text");
38633863
end getUses;
38643864

3865+
function getConversionsFromVersions
3866+
input TypeName pack;
3867+
output String[:] withoutConversion;
3868+
output String[:] withConversion;
3869+
external "builtin";
3870+
annotation(
3871+
Documentation(info="<html>
3872+
Returns the versions this library can convert from with and without conversions.
3873+
</html>"),
3874+
preferredView="text");
3875+
end getConversionsFromVersions;
3876+
38653877
function getDerivedClassModifierNames "Returns the derived class modifier names.
38663878
Example command:
38673879
type Resistance = Real(final quantity=\"Resistance\",final unit=\"Ohm\");

OMCompiler/Compiler/Script/CevalScriptBackend.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,7 @@ algorithm
19511951
SimCode.SimulationSettings simSettings;
19521952
Boolean dumpExtractionSteps, requireExactVersion;
19531953
list<tuple<Absyn.Path,list<String>,Boolean>> uses;
1954+
list<String> withoutConversion, withConversion;
19541955
Config.LanguageStandard oldLanguageStd;
19551956
SCode.Element cl;
19561957
list<SCode.Element> cls, elts;
@@ -2356,6 +2357,14 @@ algorithm
23562357
then
23572358
(cache,v);
23582359

2360+
case (cache,_,"getConversionsFromVersions",{Values.CODE(Absyn.C_TYPENAME(classpath))},_)
2361+
equation
2362+
(absynClass as Absyn.CLASS()) = Interactive.getPathedClassInProgram(classpath, SymbolTable.getAbsyn());
2363+
(withoutConversion,withConversion) = Interactive.getConversionAnnotation(absynClass);
2364+
v = Values.TUPLE({ValuesUtil.makeArray(List.map(withoutConversion,ValuesUtil.makeString)), ValuesUtil.makeArray(List.map(withConversion,ValuesUtil.makeString))});
2365+
then
2366+
(cache,v);
2367+
23592368
case (cache,_,"getDerivedClassModifierNames",{Values.CODE(Absyn.C_TYPENAME(classpath))},_)
23602369
equation
23612370
absynClass = Interactive.getPathedClassInProgram(classpath, SymbolTable.getAbsyn());

OMCompiler/Compiler/Script/Interactive.mo

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11427,6 +11427,86 @@ algorithm
1142711427
end match;
1142811428
end getUsesAnnotationString2;
1142911429

11430+
public function getConversionAnnotation
11431+
"Returns the uses-annotations of the top-level classes in the given program."
11432+
input Absyn.Class cls;
11433+
output list<String> withoutConversion = {}, withConversion = {};
11434+
protected
11435+
Option<tuple<list<String>,list<String>>> opt_conversion;
11436+
list<Absyn.Class> classes;
11437+
algorithm
11438+
opt_conversion := AbsynUtil.getNamedAnnotationInClass(cls, Absyn.Path.IDENT("conversion"), getConversionAnnotationString);
11439+
(withoutConversion,withConversion) := match opt_conversion
11440+
case SOME((withoutConversion,withConversion)) then (withoutConversion,withConversion);
11441+
else ({},{});
11442+
end match;
11443+
end getConversionAnnotation;
11444+
11445+
protected function getConversionAnnotationString
11446+
input Option<Absyn.Modification> mod;
11447+
output tuple<list<String>,list<String>> result;
11448+
protected
11449+
list<Absyn.ElementArg> arglst, arglst2;
11450+
list<String> without = {}, with = {};
11451+
list<Absyn.Exp> exps;
11452+
String version, name;
11453+
SourceInfo info;
11454+
algorithm
11455+
SOME(Absyn.CLASSMOD(elementArgLst = arglst)) := mod;
11456+
for arg in arglst loop
11457+
_ := match arg
11458+
11459+
case Absyn.MODIFICATION(path = Absyn.IDENT(name = "noneFromVersion"),
11460+
modification=SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.STRING(version)))))
11461+
algorithm
11462+
without := version :: without;
11463+
then ();
11464+
11465+
case Absyn.MODIFICATION(path = Absyn.IDENT(name = "from"),
11466+
modification=SOME(Absyn.CLASSMOD(elementArgLst=arglst2)), info=info)
11467+
algorithm
11468+
arglst2 := List.filterOnTrue(arglst2, filterIsVersionElement);
11469+
_ := matchcontinue arglst2
11470+
case {Absyn.MODIFICATION(modification=SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.STRING(version)))))}
11471+
algorithm
11472+
with := version :: with;
11473+
then ();
11474+
case {Absyn.MODIFICATION(modification=SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.ARRAY(exps)))))}
11475+
algorithm
11476+
for exp in exps loop
11477+
Absyn.STRING(version) := exp;
11478+
with := version :: with;
11479+
end for;
11480+
then ();
11481+
else
11482+
algorithm
11483+
Error.addSourceMessage(Error.CONVERSION_MISSING_FROM_VERSION, {Dump.unparseElementArgStr(arg)}, info);
11484+
then ();
11485+
end matchcontinue;
11486+
then ();
11487+
11488+
case Absyn.MODIFICATION(info = info, path = Absyn.IDENT(name = name))
11489+
equation
11490+
if not Util.stringStartsWith(name, "__") then
11491+
Error.addSourceMessage(Error.CONVERSION_UNKNOWN_ANNOTATION, {name}, info);
11492+
end if;
11493+
then ();
11494+
11495+
end match;
11496+
end for;
11497+
result := (listReverse(without), listReverse(with));
11498+
end getConversionAnnotationString;
11499+
11500+
protected function filterIsVersionElement
11501+
input Absyn.ElementArg eltArg;
11502+
output Boolean b;
11503+
algorithm
11504+
b := match eltArg
11505+
case Absyn.MODIFICATION(path = Absyn.IDENT(name = "version")) then true;
11506+
else false;
11507+
end match;
11508+
end filterIsVersionElement;
11509+
1143011510
protected function getIconAnnotation
1143111511
"This function takes a Path and a Program and returns a comma separated
1143211512
string of values for the icon annotation for the class named by the

OMCompiler/Compiler/Util/Error.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,10 @@ public constant ErrorTypes.Message SERIALIZED_SIZE = ErrorTypes.MESSAGE(5046, Er
10801080
Gettext.gettext("%s uses %s of memory (%s without GC overhead; %s is consumed by not performing String sharing)."));
10811081
public constant ErrorTypes.Message META_MATCH_CONSTANT = ErrorTypes.MESSAGE(5047, ErrorTypes.TRANSLATION(), ErrorTypes.NOTIFICATION(),
10821082
Gettext.gettext("Match input %s is a constant value."));
1083+
public constant ErrorTypes.Message CONVERSION_MISSING_FROM_VERSION = ErrorTypes.MESSAGE(5048, ErrorTypes.SCRIPTING(), ErrorTypes.WARNING(),
1084+
Gettext.gettext("Conversion-annotation is missing version for from-conversion: %s."));
1085+
public constant ErrorTypes.Message CONVERSION_UNKNOWN_ANNOTATION = ErrorTypes.MESSAGE(5049, ErrorTypes.SCRIPTING(), ErrorTypes.WARNING(),
1086+
Gettext.gettext("Conversion-annotation contains unknown element: %s."));
10831087

10841088

10851089
public constant ErrorTypes.Message COMPILER_ERROR = ErrorTypes.MESSAGE(5999, ErrorTypes.TRANSLATION(), ErrorTypes.ERROR(),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// status: correct
2+
3+
loadString("package P1
4+
annotation(conversion(from(version={x,y,z})));
5+
end P1;");
6+
getConversionsFromVersions(P1);getErrorString();
7+
loadString("package P2
8+
annotation(conversion(xfrom()));
9+
end P2;");
10+
getConversionsFromVersions(P2);getErrorString();
11+
loadString("package P3
12+
annotation(conversion(noneFromVersion=\"1.2.3\",from(version=\"1.2.4\"),from(version={\"1.2.5\",\"1.2.6\"}),noneFromVersion=\"1.2.7\"));
13+
end P3;");
14+
getConversionsFromVersions(P3);getErrorString();
15+
16+
// Result:
17+
// true
18+
// ({},{})
19+
// "[<interactive>:2:25-2:46:writable] Warning: Conversion-annotation is missing version for from-conversion: from(version = {x, y, z}).
20+
// "
21+
// true
22+
// ({},{})
23+
// "[<interactive>:2:25-2:32:writable] Warning: Conversion-annotation contains unknown element: xfrom.
24+
// "
25+
// true
26+
// ({"1.2.3","1.2.7"},{"1.2.4","1.2.5","1.2.6"})
27+
// ""
28+
// endResult

testsuite/openmodelica/interactive-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Bug3979.mos \
1818
Bug4209.mos \
1919
Bug4248.mos \
2020
ConvertUnits.mos \
21+
ConversionVersions.mos \
2122
CopyClass.mos \
2223
choicesAllMatching.mos \
2324
DefaultComponentName.mos \

0 commit comments

Comments
 (0)