Skip to content

Commit

Permalink
Check license features of sub packages (#7607)
Browse files Browse the repository at this point in the history
Fixes #7502
  • Loading branch information
adeas31 committed Jun 23, 2021
1 parent 3037365 commit d6666e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
68 changes: 47 additions & 21 deletions OMCompiler/Compiler/FrontEnd/ClassLoader.mo
Expand Up @@ -142,7 +142,7 @@ protected function loadClassFromMps
protected
String mp, name, pwd, cmd, version, userLibraries;
Boolean isDir, impactOK;
Absyn.Class cl;
Option<Absyn.Class> cl;
list<String> versionsThatProvideTheWanted, commands;
algorithm
try
Expand Down Expand Up @@ -172,7 +172,11 @@ algorithm
// print("System.getLoadModelPath: " + id + " {" + stringDelimitList(prios,",") + "} " + stringDelimitList(mps,",") + " => " + mp + " " + name + " " + boolString(isDir));
Config.setLanguageStandardFromMSL(name);
cl := loadClassFromMp(id, mp, name, isDir, encoding, encrypted);
outProgram := Absyn.PROGRAM({cl},Absyn.TOP());
if (isSome(cl)) then
outProgram := Absyn.PROGRAM({Util.getOption(cl)},Absyn.TOP());
else
outProgram := Absyn.PROGRAM({},Absyn.TOP());
end if;
end loadClassFromMps;

public function loadClassFromMp
Expand All @@ -182,12 +186,12 @@ public function loadClassFromMp
input Boolean isDir;
input Option<String> optEncoding;
input Boolean encrypted = false;
output Absyn.Class outClass;
output Option<Absyn.Class> outClass;
algorithm
outClass := match (id,path,name,isDir,optEncoding)
local
String pd,encoding,encodingfile;
Absyn.Class cl;
Option<Absyn.Class> cl;
list<String> filenames;
LoadFileStrategy strategy;
Boolean lveStarted;
Expand All @@ -200,7 +204,7 @@ algorithm
encodingfile = stringAppendList({path,pd,"package.encoding"});
encoding = System.trimChar(System.trimChar(if System.regularFileExists(encodingfile) then System.readFile(encodingfile) else Util.getOptionOrDefault(optEncoding,"UTF-8"),"\n")," ");
strategy = STRATEGY_ON_DEMAND(encoding);
cl = parsePackageFile(path + pd + name, strategy, false, Absyn.TOP(), id);
cl = parsePackageFile(path + pd + name, strategy, false, Absyn.TOP(), id, encrypted);
then
cl;

Expand Down Expand Up @@ -262,7 +266,7 @@ protected function loadCompletePackageFromMp
input Absyn.Within inWithin;
input Integer numError;
input Boolean encrypted = false;
output Absyn.Class cl;
output Option<Absyn.Class> cl;
algorithm
cl := matchcontinue (id,inIdent,inString,inWithin)
local
Expand All @@ -275,6 +279,8 @@ algorithm
list<Absyn.ClassPart> cp;
Option<String> cmt;
SourceInfo info;
Option<Absyn.Class> opt_cl;
Absyn.Class class_;
Absyn.Path path;
Absyn.Within w2;
list<PackageOrder> reverseOrder;
Expand All @@ -290,13 +296,17 @@ algorithm
fail();
end if;
// print("Look for " + packagefile + "\n");
(cl as Absyn.CLASS(name,pp,fp,ep,r,Absyn.PARTS(tv,ca,cp,ann,cmt),info)) = parsePackageFile(packagefile, strategy, true, within_, id);
opt_cl = parsePackageFile(packagefile, strategy, true, within_, id, encrypted);
// print("Got " + packagefile + "\n");
reverseOrder = getPackageContentNames(cl, orderfile, mp_1, Error.getNumErrorMessages(), encrypted);
path = AbsynUtil.joinWithinPath(within_,Absyn.IDENT(id));
w2 = Absyn.WITHIN(path);
cp = List.fold4(reverseOrder, loadCompletePackageFromMp2, mp_1, strategy, w2, encrypted, {});
then Absyn.CLASS(name,pp,fp,ep,r,Absyn.PARTS(tv,ca,cp,ann,cmt),info);
if (isSome(opt_cl)) then
(class_ as Absyn.CLASS(name,pp,fp,ep,r,Absyn.PARTS(tv,ca,cp,ann,cmt),info)) = Util.getOption(opt_cl);
reverseOrder = getPackageContentNames(class_, orderfile, mp_1, Error.getNumErrorMessages(), encrypted);
path = AbsynUtil.joinWithinPath(within_,Absyn.IDENT(id));
w2 = Absyn.WITHIN(path);
cp = List.fold4(reverseOrder, loadCompletePackageFromMp2, mp_1, strategy, w2, encrypted, {});
opt_cl = SOME(Absyn.CLASS(name,pp,fp,ep,r,Absyn.PARTS(tv,ca,cp,ann,cmt),info));
end if;
then opt_cl;
case (_,pack,mp,_)
equation
true = numError == Error.getNumErrorMessages();
Expand Down Expand Up @@ -341,7 +351,7 @@ algorithm
Absyn.ElementItem ei;
String pd,file,id;
Absyn.ClassPart cp;
Absyn.Class cl;
Option<Absyn.Class> cl;
Boolean bDirectoryAndFileExists;

case CLASSPART(cp)
Expand All @@ -366,17 +376,25 @@ algorithm
bDirectoryAndFileExists = System.directoryExists(mp + pd + id) and System.regularFileExists(file);
if bDirectoryAndFileExists then
cl = loadCompletePackageFromMp(id,id,mp,strategy,w1,Error.getNumErrorMessages(),encrypted);
ei = AbsynUtil.makeClassElement(cl);
cps = mergeBefore(Absyn.PUBLIC({ei}),acc);
if (isSome(cl)) then
ei = AbsynUtil.makeClassElement(Util.getOption(cl));
cps = mergeBefore(Absyn.PUBLIC({ei}),acc);
else
cps = {};
end if;
else
file = mp + pd + id + (if encrypted then ".moc" else ".mo");
if not System.regularFileExists(file) then
Error.addInternalError("Expected file " + file + " to exist", sourceInfo());
fail();
end if;
cl = parsePackageFile(file, strategy, false, w1, id);
ei = AbsynUtil.makeClassElement(cl);
cps = mergeBefore(Absyn.PUBLIC({ei}),acc);
if (isSome(cl)) then
ei = AbsynUtil.makeClassElement(Util.getOption(cl));
cps = mergeBefore(Absyn.PUBLIC({ei}),acc);
else
cps = {};
end if;
end if;
then cps;

Expand All @@ -390,8 +408,10 @@ public function parsePackageFile
input Boolean expectPackage;
input Absyn.Within w1 "Expected within of the package";
input String pack "Expected name of the package";
output Absyn.Class cl;
input Boolean encrypted = false;
output Option<Absyn.Class> cl;
protected
Absyn.Class class_;
list<Absyn.Class> cs;
Absyn.Within w2;
list<String> classNames;
Expand All @@ -403,10 +423,16 @@ algorithm
classNames := List.map(cs, AbsynUtil.getClassName);
str := stringDelimitList(classNames,", ");
if not listLength(cs)==1 then
Error.addSourceMessage(Error.LIBRARY_ONE_PACKAGE_PER_FILE, {str}, SOURCEINFO(name,true,0,0,0,0,0.0));
fail();
if encrypted then
cl := NONE();
return;
else
Error.addSourceMessage(Error.LIBRARY_ONE_PACKAGE_PER_FILE, {str}, SOURCEINFO(name,true,0,0,0,0,0.0));
fail();
end if;
end if;
(cl as Absyn.CLASS(name=cname,body=body,info=info))::{} := cs;
(class_ as Absyn.CLASS(name=cname,body=body,info=info))::{} := cs;
cl := SOME(class_);
if not stringEqual(cname,pack) then
if stringEqual(System.tolower(cname), System.tolower(pack)) then
Error.addSourceMessage(Error.LIBRARY_UNEXPECTED_NAME_CASE_SENSITIVE, {pack,cname}, info);
Expand Down
8 changes: 7 additions & 1 deletion OMCompiler/Compiler/Script/CevalScript.mo
Expand Up @@ -382,6 +382,7 @@ protected
String pathStr, versions, className, version, modelicaPath, thisModelicaPath, dir;
Absyn.Program p, pnew;
ErrorTypes.MessageTokens msgTokens;
Option<Absyn.Class> cl;
algorithm
(path, _, versionsLst, onlyCheckFirstModelicaPath) := modelToLoad;
(modelicaPath, forceLoad, notifyLoad, checkUses, requireExactVersion, encrypted) := inArg;
Expand All @@ -401,7 +402,12 @@ algorithm
pnew := ClassLoader.loadClass(path, versionsLst, thisModelicaPath, NONE(), requireExactVersion, encrypted);
else
dir := System.dirname(pathToFile);
pnew := Absyn.PROGRAM({ClassLoader.loadClassFromMp(AbsynUtil.pathFirstIdent(path), System.dirname(dir), System.basename(dir), true, NONE(), encrypted)}, Absyn.TOP());
cl := ClassLoader.loadClassFromMp(AbsynUtil.pathFirstIdent(path), System.dirname(dir), System.basename(dir), true, NONE(), encrypted);
if (isSome(cl)) then
pnew := Absyn.PROGRAM({Util.getOption(cl)}, Absyn.TOP());
else
pnew := Absyn.PROGRAM({}, Absyn.TOP());
end if;
end if;
version := getPackageVersion(path, pnew);
b := not notifyLoad or forceLoad;
Expand Down

0 comments on commit d6666e6

Please sign in to comment.