Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
af92c76
Remove unused keyword argument `updateBOMneeded`
sungshik Dec 1, 2026
56502e5
Remove unused function `updateBOM`
sungshik Dec 1, 2026
462f7c0
Remove few remaining occurrences of `ModuleStatus.tmodels` outside `C…
sungshik Dec 1, 2026
8a58c5a
Add function to validate TPL version (and capitalize the constructor …
sungshik Dec 2, 2026
f6c735e
Update `getRascalModuleLocation` to be able to optionally return a de…
sungshik Dec 2, 2026
9f004d8
Fix issue that an import was missing
sungshik Dec 4, 2026
4ec683e
Remove `tmodelLIFO` field from `moduleStatus` constructor and replace…
sungshik Dec 2, 2026
c23e3ea
Remove `tmodels` field from `moduleStatus` constructor and update cod…
sungshik Dec 2, 2026
1bed283
Add function to load/save/read/write TPL files (using memoization ins…
sungshik Dec 2, 2026
60bc0fb
Remove remnants of explicit TModel cache (replaced with memoization i…
sungshik Dec 2, 2026
59280b5
Replace `lastModified` with `md5HashFile` as stamps for `readTplFile`…
sungshik Dec 2, 2026
c90756b
Move logic to find TPL locations into `saveTModel` and `loadTModel`
sungshik Dec 4, 2026
7a8009e
Fix issue that `ModuleStatus` wasn't backward-compatible
sungshik Dec 4, 2026
dc02e91
Add functions to load/save BOMs
sungshik Dec 4, 2026
8237e09
Remove `ModuleStatus` from the signatures of `saveTModel` and `loadTM…
sungshik Dec 4, 2026
afd4a56
Add module `TModelIO` with utility functions to save/load `TModel` va…
sungshik Dec 4, 2026
6575297
Add `TplFiles` ADT to simplify caching-through-memoization implementa…
sungshik Dec 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/org/rascalmpl/compiler/CheckTestSources.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ tuple[str, int] safeCheck(str \module, RascalCompilerConfig compilerConfig) {
ModuleStatus result = rascalTModelForNames([\module],
compilerConfig,
dummy_compile1);
//iprintln(result.tmodels[\module].facts);
<found, tm, result> = getTModelForModule(\module, result);
if(found && !isEmpty(tm.messages)){
if(/error(_,_) := tm.messages){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module lang::rascalcore::check::BasicRascalConfig
*/

import lang::rascal::\syntax::Rascal;
import lang::rascalcore::compile::CompileTimeError;
import Location;
import util::SemVer;
import String;
Expand Down Expand Up @@ -182,6 +183,15 @@ str normalizedMD5Hash(value contribs...)
bool isValidRascalTplVersion(str version)
= equalVersion(version, currentRascalTplVersion);

void validateRascalTplVersion(TModel tm, loc tpl) throws RascalTplVersionError {
str version = tm.rascalTplVersion;
if (!isValidRascalTplVersion(version)) {
str moduleName = tm.modelName;
str txt = "outdated Rascal TPL version <version> (required: <getCurrentRascalTplVersion()>; please update the `rascal` dependency in `pom.xml` to version <getMinimumRascalVersion()> or higher, and clear the `target` folder)";
throw RascalTplVersionError(moduleName, tpl, version, txt);
}
}

str getCurrentRascalTplVersion() = currentRascalTplVersion;
str getMinimumRascalVersion() = minimumRascalVersion;

Expand Down
13 changes: 4 additions & 9 deletions src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ ModuleStatus rascalTModelForLocs(
}

<compatibleLibs, ms> = libraryDependenciesAreCompatible(mids, ms);
// compatibleLibs = false;

if(compatibleLibs && uptodateTPls(mlocs, mnames, pcfg)){
for (i <- index(mids)) {
Expand Down Expand Up @@ -193,11 +194,6 @@ ModuleStatus rascalTModelForLocs(
topModuleNames = mnames;
try {
ms = getImportAndExtendGraph(topModuleIds, ms);

// if(/error(_,_) := ms.messages){
// return clearTModelCache(ms);
// }

imports_and_extends = ms.paths<0,2>;
<components, sorted> = stronglyConnectedComponentsAndTopSort(imports_and_extends);
map[MODID, set[MODID]] module2component = (m : c | c <- components, m <- c);
Expand Down Expand Up @@ -271,7 +267,7 @@ ModuleStatus rascalTModelForLocs(
ms = addProperty(m, ms, tpl_uptodate(), checked(), bom_update_needed());
}
}
} catch rascalTplVersionError(_,_,_,_): {
} catch RascalTplVersionError(_,_,_,_): {
ms = addProperty(m, ms, tpl_version_error());
// m_compatible remains false
};
Expand Down Expand Up @@ -388,7 +384,7 @@ ModuleStatus rascalTModelForLocs(
for(MODID mid <- topModuleIds){
ms.messages[mid] = { error("Parse error", src) };
}
} catch rascalTplVersionError(str moduleName, loc tpl, str version, str txt):{
} catch RascalTplVersionError(str moduleName, loc tpl, str version, str txt):{
for(MODID mid <- topModuleIds){
causes = [ info("Module `<moduleName>` has outdated Rascal TPL version <version>, no source found", tpl) ];
ms.messages[mid] ? {} += { error("Import/extend of `<moduleName>` has <txt>",
Expand All @@ -403,7 +399,7 @@ ModuleStatus rascalTModelForLocs(
}

jobEnd(jobName);
return clearTModelCache(ms);
return ms;
}

bool implicitlyUsesParseTree(str modulePath, rel[str, AType] factsByPath){
Expand Down Expand Up @@ -474,7 +470,6 @@ tuple[TModel, ModuleStatus] rascalTModelComponent(set[MODID] moduleIds, ModuleSt
map[MODID, Module] idTrees = ();
for(MODID mid <- moduleIds){
mname = moduleId2moduleName(mid);
ms = removeTModel(mid, ms);
mloc = |unknown:///|(0,0,<0,0>,<0,0>);
try {
mloc = getRascalModuleLocation(mid, ms);
Expand Down
228 changes: 60 additions & 168 deletions src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extend lang::rascalcore::check::BasicRascalConfig;
extend lang::rascalcore::check::ModuleLocations;
extend lang::rascalcore::CompilerPathConfig;
extend lang::rascalcore::check::LogicalLocations;
extend lang::rascalcore::check::TModelIO;

extend analysis::typepal::Collector;

Expand Down Expand Up @@ -68,6 +69,8 @@ void checkSupportedByParserGenerator(Tree t, Collector c){
});
}

alias BOM = rel[str, datetime, PathRole];

data ModuleProperty =
rsc_not_found()
| rsc_changed()
Expand All @@ -92,13 +95,12 @@ data ModuleStatus =
rel[MODID, PathRole, MODID] paths,
map[MODID, Module] parseTrees,
list[MODID] parseTreeLIFO,
map[MODID, TModel] tmodels,
list[MODID] tmodelLIFO,
set[MODID] changedModules,
map[MODID,loc] moduleLocs,
map[MODID,datetime] moduleLastModified,
map[MODID, set[Message]] messages,
map[MODID, set[ModuleProperty]] status,
map[MODID, BOM] boms,
PathConfig pathConfig,
RascalCompilerConfig compilerConfig
);
Expand Down Expand Up @@ -139,13 +141,13 @@ ModuleStatus deleteProperty(MODID mid, ModuleStatus ms, ModuleProperty propertie
}

ModuleStatus moduleStatus( // TEMPORARY FOR COMPATIBILITY BETWEEN VERSIONS
rel[str, PathRole, str] _strPaths,
/* Deprecated */ rel[str, PathRole, str] _strPaths,
rel[MODID, PathRole, MODID] _paths,
map[MODID, Module] _parseTrees,
list[MODID] _parseTreeLIFO,
map[MODID, TModel] _tmodels,
list[MODID] _tmodelLIFO,
set[MODID] _changedModules,
/* Deprecated */ map[MODID, TModel] _tmodels,
/* Deprecated */ list[MODID] _tmodelLIFO,
/* Deprecated */ set[MODID] _changedModules,
map[MODID,loc] _moduleLocs,
map[MODID,datetime] _moduleLastModified,
map[MODID, set[Message]] _messages,
Expand All @@ -155,7 +157,7 @@ ModuleStatus moduleStatus( // TEMPORARY FOR COMPATIBILITY BETWEEN VERSI
= newModuleStatus(pathConfig, compilerConfig);

ModuleStatus newModuleStatus(PathConfig pcfg, TypePalConfig tcfg)
= moduleStatus({}, (), [], (), [], {}, (), (), (), (), pcfg, tcfg);
= moduleStatus({}, (), [], {}, (), (), (), (), (), pcfg, tcfg);

ModuleStatus newModuleStatus()
= newModuleStatus(pathConfig(), tconfig());
Expand All @@ -169,40 +171,7 @@ bool isModuleLocationInLibs(loc l, PathConfig pcfg){
return res;
}

bool traceTPL = false;
bool traceParseTreeCache = false;
bool traceTModelCache = false;

tuple[bool,loc] getTPLReadLoc(str qualifiedModuleName, PathConfig pcfg)
= getTPLReadLoc(moduleName2moduleId(qualifiedModuleName), pcfg);

tuple[bool,loc] getTPLReadLoc(MODID moduleId, PathConfig pcfg){
assert isModuleId(moduleId) : "getTPLReadLoc: <moduleId>";
parts = split("/", moduleId.path);
parts = parts[0 .. size(parts)-1] + "$<parts[-1]>";
fileName = intercalate("/", parts) + ".tpl";
dirName = makeDirName(parts);

for(loc dir <- [pcfg.generatedResources, pcfg.bin] + pcfg.libs){ // In a bin or lib directory?
fileLoc = dir + "<getCompiledPackage()>" + fileName;
if(exists(fileLoc)){
if(traceTPL) println("getTPLReadLoc: <moduleId> =\> <fileLoc>");
return <true, fileLoc>;
} else {
;// if(traceTPL)
// println("getTPLReadLoc: DOES NOT EXIST: <fileLoc>");
}
}
return <false, |error:///|>;
}

tuple[bool,loc] getTPLWriteLoc(MODID moduleId, PathConfig pcfg){
assert isModuleId(moduleId) : "getTPLWriteLoc: <moduleId>";
qualifiedModuleName = moduleId2moduleName(moduleId);
fileName = "<asFileName(qualifiedModuleName)>.tpl";
tplLoc = getGeneratedResourcesDir(qualifiedModuleName, pcfg) + fileName;
return <exists(tplLoc), tplLoc>;
}

datetime startOfEpoch = $2000-01-01T00:00:00.000+00:00$;

Expand Down Expand Up @@ -317,36 +286,20 @@ tuple[bool, Module, ModuleStatus] getModuleParseTree(MODID moduleId, ModuleStatu
}
}

loc getRascalModuleLocation(MODID moduleId, ModuleStatus ms){
if(moduleId in ms.moduleLocs){
loc l = ms.moduleLocs[moduleId];
if(!isRascalLogicalLoc(l)) return l;
}
return getRascalModuleLocation(moduleId2moduleName(moduleId), ms.pathConfig);
}

/*
* We implement a caching mechanism for TModels with the following properties:
* - tmodelCacheSize tmodels are cached.
* - In TModels on file (.tpl) physical locations have been replaced by logical locations where possible.
* - The policy is to always keep TModels in the cache in this logical form.
* - During its presence in the cache, the BOM of a TModel may get updated.
* - When a TModel has to be removed from the cache, it is written back to file (if needed).
*/

int tmodelCacheSize = 30; // should be > 0

ModuleStatus clearTModelCache(ModuleStatus ms){
todo = { mname | mname <- ms.status, hasProperty(mname, ms, bom_update_needed())};
for(candidate <- ms.tmodelLIFO){
ms = removeOldestTModelFromCache(ms/*, updateBOMneeded=true*/);
todo -= candidate;
}
for(candidate <- todo){
ms = removeTModel(candidate, ms/*, updateBOMneeded=true*/);
ms = deleteProperty(candidate, ms, bom_update_needed());
loc getRascalModuleLocation(MODID moduleId, ModuleStatus ms, loc ifThrows = |unknown:///|){
try {
if(moduleId in ms.moduleLocs){
loc l = ms.moduleLocs[moduleId];
if(!isRascalLogicalLoc(l)) return l;
}
return getRascalModuleLocation(moduleId2moduleName(moduleId), ms.pathConfig);
} catch e: {
if (ifThrows?) {
return ifThrows;
} else {
throw e;
}
}
return ms;
}

rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){
Expand All @@ -359,124 +312,63 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){
+ { <qualifiedModuleName, getLastModified(moduleId, ms), importPath() > };
}

ModuleStatus updateBOM(MODID moduleId, ModuleStatus ms){
if(hasAnyProperty(moduleId, ms, rsc_not_found(), tpl_from_library())){
return ms;
}
<found, tm, ms> = getTModelForModule(moduleId, ms);
if(found){

newBom = makeBom(moduleId, ms);
if(newBom != tm.store[key_bom]){
tm.store[key_bom] = newBom;
ms = deleteProperty(moduleId, ms, tpl_saved(), bom_update_needed());
ms = addTModel(moduleId, tm, ms);

if(ms.compilerConfig.logWrittenFiles) println("Updated BOM: <moduleId>");
}
} else{
println("Could not update BOM of <moduleId>");
ModuleStatus saveBOM(MODID moduleId, TModel tm, ModuleStatus ms) throws IO {
if (key_bom in tm.store, BOM bom := tm.store[key_bom]) {
ms.boms[moduleId] = bom;
}
return ms;
}

ModuleStatus removeTModel(MODID candidate, ModuleStatus ms, bool updateBOMneeded = false){
assert isModuleId(candidate) : "removeTModel: <candidate>";
if( updateBOMneeded
|| candidate in ms.tmodels
&& !hasAnyProperty(candidate, ms, tpl_saved(), rsc_not_found(), tpl_from_library())
){
pcfg = ms.pathConfig;
if(updateBOMneeded){
ms = updateBOM(candidate, ms);
}
ms = deleteProperty(candidate, ms, bom_update_needed());
<found, tplLoc> = getTPLWriteLoc(candidate, pcfg);
tm = ms.tmodels[candidate];
ms = addProperty(candidate, ms, tpl_saved());
if(ms.compilerConfig.verbose) println("Saving tmodel for <moduleId2moduleName(candidate)> before removing from cache");
try {
writeBinaryValueFile(tplLoc, tm, optimizeSharing = true);
if(traceTPL) println("Written <tplLoc>");
} catch value e: {
mloc = ms.moduleLocs[candidate] ? |unknown:///|;
ms.messages[candidate] += { error("Cannot write TPL file <tplLoc>, reason: <e>", mloc) };
}
TModel loadBOM(MODID moduleId, TModel tm, ModuleStatus ms) throws IO {
if (moduleId in ms.boms) {
tm.store[key_bom] = ms.boms[moduleId];
}
ms.tmodels = delete(ms.tmodels, candidate);
return ms;
return tm;
}

ModuleStatus removeOldestTModelFromCache(ModuleStatus ms, bool updateBOMneeded = false){
if(size(ms.tmodelLIFO) > 0){
candidate = ms.tmodelLIFO[-1];
ms = removeTModel(candidate, ms, updateBOMneeded=updateBOMneeded);
if(traceTModelCache) println("*** deleted tmodel <candidate>, tmodels: <size(ms.tmodels)>, lifo: <size(ms.tmodelLIFO)>");
ms.tmodelLIFO = ms.tmodelLIFO[..-1];
}
return ms;
}

ModuleStatus addTModel (MODID moduleId, TModel tm, ModuleStatus ms){
if(traceTModelCache) println("addTModel: <moduleId>");
if(tmodelCacheSize > 0){
ms.tmodels[moduleId] = tm;
if(moduleId notin ms.tmodelLIFO){
ms.tmodelLIFO = [moduleId, *ms.tmodelLIFO];
while(size(ms.tmodels) >= tmodelCacheSize && size(ms.tmodelLIFO) > 0 && ms.tmodelLIFO[-1] != moduleId){
ms = removeOldestTModelFromCache(ms);
}
ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){
assert isModuleId(moduleId) : "addTModel: <moduleId>";
if(!hasAnyProperty(moduleId, ms, tpl_saved(), tpl_from_library())){
ms = deleteProperty(moduleId, ms, bom_update_needed());
if(ms.compilerConfig.verbose) println("Saving tmodel for <moduleId2moduleName(moduleId)>");
try {
saveTModel(moduleId, tm, ms.pathConfig);
ms = saveBOM(moduleId, tm, ms);
ms = addProperty(moduleId, ms, tpl_saved());
} catch e: {
mloc = ms.moduleLocs[moduleId] ? |unknown:///|;
ms.messages[moduleId] += { error("Cannot save TModel for <moduleId2moduleName(moduleId)>, reason: <e>", mloc) };
}
}
return ms;
}

private type[TModel] ReifiedTModel = #TModel; // precomputed for efficiency

tuple[bool, TModel, ModuleStatus] getTModelForModule(str moduleName, ModuleStatus ms)
= getTModelForModule(moduleName2moduleId(moduleName), ms);

tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatus ms){
assert isModuleId(moduleId) : "getTModelForModule: <moduleId>";
if(traceTModelCache) println("getTModelForModule: <moduleId> <moduleId in ms.status ? ms.status[moduleId] : "{}">");
pcfg = ms.pathConfig;
if(moduleId in ms.tmodels){
tm = ms.tmodels[moduleId];
return <true, tm, ms>;
}
while(size(ms.tmodels) >= tmodelCacheSize && size(ms.tmodelLIFO) > 0 && ms.tmodelLIFO[-1] != moduleId){
ms = removeOldestTModelFromCache(ms);
}
qualifiedModuleName = moduleId2moduleName(moduleId);
<found, tplLoc> = getTPLReadLoc(moduleId, pcfg);
if(found){
if(traceTPL) println("*** reading tmodel <tplLoc>");
tmVersion = "0.0.0";
try {
tm = readBinaryValueFile(ReifiedTModel, tplLoc);
tmVersion = tm.rascalTplVersion;
if(isValidRascalTplVersion(tmVersion)){
ms.tmodels[moduleId] = tm;
mloc = getRascalModuleLocation(moduleId, ms);
if(isModuleLocationInLibs(mloc, pcfg)){
ms = addProperty(moduleId, ms, tpl_from_library());
}
ms = addProperty(moduleId, ms, tpl_uptodate(), tpl_saved());
//do not include errors from tm in ModuleStatus to avoid that they become persistent
ms.tmodelLIFO = [moduleId, *ms.tmodelLIFO];
return <true, tm, ms>;
}
} catch e: {
return <false, tmodel(modelName=moduleId2moduleName(moduleId), messages=[error("Cannot read TPL for <qualifiedModuleName>: <e>", tplLoc)]), ms>;
loc moduleLoc = getRascalModuleLocation(moduleId, ms, ifThrows = |unknown:///|);
try {
TModel tm = loadTModel(moduleId, ms.pathConfig);
tm = loadBOM(moduleId, tm, ms);
ms = addProperty(moduleId, ms, tpl_uptodate(), tpl_saved());
if (isModuleLocationInLibs(moduleLoc, ms.pathConfig)) {
ms = addProperty(moduleId, ms, tpl_from_library());
}
msg = "outdated Rascal TPL version <tmVersion != "0.0.0" ? tmVersion + " " : "">(required: <getCurrentRascalTplVersion()>; please update the `rascal` dependency in `pom.xml` to version <getMinimumRascalVersion()> or higher, and clear the `target` folder)";
throw rascalTplVersionError(qualifiedModuleName, tplLoc, tmVersion, msg);
return <true, tm, ms>;
} catch e: RascalTplVersionError(_, _, _, _): {
throw e;
} catch e: {
str moduleName = moduleId2moduleName(moduleId);
return <false, tmodel(modelName = moduleName, messages = [error("Cannot load TModel for <moduleName>, reason: <e>", moduleLoc)]), ms>;
}
mloc = tplLoc;
try {
mloc = getRascalModuleLocation(moduleId, ms);
} catch _: /* ignore when this fails */;
return <false, tmodel(modelName=qualifiedModuleName, messages=[error("Cannot read TPL for <qualifiedModuleName>", mloc)]), ms>;
}

map[MODID, TModel] getImportedAndExtendedTModelsForModule(MODID moduleId, ModuleStatus ms, bool includeImported = true, bool includeExtended = true) {
set[MODID] imported = includeImported ? ms.paths[moduleId, importPath()] : {};
set[MODID] extended = includeExtended ? ms.paths[moduleId, extendPath()] : {};
return (m: tm | MODID m <- imported + extended, <_, tm, _> := getTModelForModule(m, ms));
}

rel[loc from, PathRole r, loc to] getPaths(rel[MODID from, PathRole r, MODID to] paths, ModuleStatus ms){
Expand Down
Loading
Loading