From af92c762bf2d6e3a33b501facc46f7d74d530c32 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Tue, 1 Dec 2026 13:34:05 +0100 Subject: [PATCH 01/17] Remove unused keyword argument `updateBOMneeded` --- .../lang/rascalcore/check/CheckerCommon.rsc | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 73f69c58bfa..da7193ceb69 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -339,11 +339,11 @@ 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*/); + ms = removeOldestTModelFromCache(ms); todo -= candidate; } for(candidate <- todo){ - ms = removeTModel(candidate, ms/*, updateBOMneeded=true*/); + ms = removeTModel(candidate, ms); ms = deleteProperty(candidate, ms, bom_update_needed()); } return ms; @@ -380,17 +380,13 @@ ModuleStatus updateBOM(MODID moduleId, ModuleStatus ms){ return ms; } -ModuleStatus removeTModel(MODID candidate, ModuleStatus ms, bool updateBOMneeded = false){ +ModuleStatus removeTModel(MODID candidate, ModuleStatus ms){ assert isModuleId(candidate) : "removeTModel: "; - if( updateBOMneeded - || candidate in ms.tmodels - && !hasAnyProperty(candidate, ms, tpl_saved(), rsc_not_found(), tpl_from_library()) + if( 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()); + ms = deleteProperty(candidate, ms, bom_update_needed()); = getTPLWriteLoc(candidate, pcfg); tm = ms.tmodels[candidate]; ms = addProperty(candidate, ms, tpl_saved()); @@ -407,10 +403,10 @@ ModuleStatus removeTModel(MODID candidate, ModuleStatus ms, bool updateBOMneeded return ms; } -ModuleStatus removeOldestTModelFromCache(ModuleStatus ms, bool updateBOMneeded = false){ +ModuleStatus removeOldestTModelFromCache(ModuleStatus ms){ if(size(ms.tmodelLIFO) > 0){ candidate = ms.tmodelLIFO[-1]; - ms = removeTModel(candidate, ms, updateBOMneeded=updateBOMneeded); + ms = removeTModel(candidate, ms); if(traceTModelCache) println("*** deleted tmodel , tmodels: , lifo: "); ms.tmodelLIFO = ms.tmodelLIFO[..-1]; } From 56502e5f2f494cc0a5ad4acb7f0db24ef18df6f3 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Tue, 1 Dec 2026 13:35:01 +0100 Subject: [PATCH 02/17] Remove unused function `updateBOM` --- .../lang/rascalcore/check/CheckerCommon.rsc | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index da7193ceb69..1fe70939af6 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -359,27 +359,6 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ + { }; } -ModuleStatus updateBOM(MODID moduleId, ModuleStatus ms){ - if(hasAnyProperty(moduleId, ms, rsc_not_found(), tpl_from_library())){ - return 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: "); - } - } else{ - println("Could not update BOM of "); - } - return ms; -} - ModuleStatus removeTModel(MODID candidate, ModuleStatus ms){ assert isModuleId(candidate) : "removeTModel: "; if( candidate in ms.tmodels From 462f7c0b0324de4fc859fb7fab798f55352a2db8 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Tue, 1 Dec 2026 15:22:03 +0100 Subject: [PATCH 03/17] Remove few remaining occurrences of `ModuleStatus.tmodels` outside `CheckerCommon` (and introduce new auxiliary function to do so) --- src/org/rascalmpl/compiler/CheckTestSources.rsc | 1 - .../compiler/lang/rascalcore/check/CheckerCommon.rsc | 6 ++++++ src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc | 1 - .../lang/rascalcore/compile/muRascal2Java/CodeGen.rsc | 6 +++--- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/org/rascalmpl/compiler/CheckTestSources.rsc b/src/org/rascalmpl/compiler/CheckTestSources.rsc index 34fe25dc206..a6793991855 100644 --- a/src/org/rascalmpl/compiler/CheckTestSources.rsc +++ b/src/org/rascalmpl/compiler/CheckTestSources.rsc @@ -104,7 +104,6 @@ tuple[str, int] safeCheck(str \module, RascalCompilerConfig compilerConfig) { ModuleStatus result = rascalTModelForNames([\module], compilerConfig, dummy_compile1); - //iprintln(result.tmodels[\module].facts); = getTModelForModule(\module, result); if(found && !isEmpty(tm.messages)){ if(/error(_,_) := tm.messages){ diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 1fe70939af6..466533fd40c 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -454,6 +454,12 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu return ", 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){ paths = {}; for( <- paths){ diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc index 4b37a1842eb..529ea547d4d 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc @@ -389,7 +389,6 @@ map[str, loc] getModuleScopes(TModel tm) // } tuple[map[MODID,TModel], ModuleStatus] prepareForCompilation(set[MODID] component, map[MODID,set[MODID]] m_imports, map[MODID,set[MODID]] m_extends, ModuleStatus ms, TModel tm){ - //map[str,TModel] tmodels = (); //ms.tmodels; pcfg = ms.pathConfig; dependencies_ok = true; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc index 273f5b44dba..faaadf94f71 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc @@ -72,12 +72,12 @@ int naux = 0; // Generate code and test class for a single Rascal module tuple[JCode, JCode, JCode, list[value]] muRascal2Java(MuModule m, ModuleStatus ms){ - map[MODID,TModel] tmodels = ms.tmodels; + moduleName = m.name; + MODID moduleId = moduleName2moduleId(moduleName); + map[MODID,TModel] tmodels = getImportedAndExtendedTModelsForModule(moduleId, ms); map[MODID,loc]moduleLocs = ms.moduleLocs; PathConfig pcfg = ms.pathConfig; naux = 0; - moduleName = m.name; - MODID moduleId = moduleName2moduleId(moduleName); locsModule = invertUnique(moduleLocs); module_scope = moduleLocs[moduleId]; = getTModelForModule(moduleId, ms); From 8a58c5ad7c5060b4e213f0ac88ba187427fb16ae Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 15:44:37 +0100 Subject: [PATCH 04/17] Add function to validate TPL version (and capitalize the constructor of the exception in accordance with other constructors of exceptions) --- .../rascalcore/check/BasicRascalConfig.rsc | 9 +++++++ .../lang/rascalcore/check/Checker.rsc | 4 +-- .../lang/rascalcore/check/CheckerCommon.rsc | 25 ++++++++----------- .../compiler/lang/rascalcore/check/Import.rsc | 2 +- .../rascalcore/compile/CompileTimeError.rsc | 2 +- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc index 814048e778c..70ab58292e5 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc @@ -182,6 +182,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 (required: ; please update the `rascal` dependency in `pom.xml` to version or higher, and clear the `target` folder)"; + throw RascalTplVersionError(moduleName, tpl, version, txt); + } +} + str getCurrentRascalTplVersion() = currentRascalTplVersion; str getMinimumRascalVersion() = minimumRascalVersion; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc index c796d38de73..d0123337ab7 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc @@ -271,7 +271,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 }; @@ -388,7 +388,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 `` has outdated Rascal TPL version , no source found", tpl) ]; ms.messages[mid] ? {} += { error("Import/extend of `` has ", diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 466533fd40c..0ab731ac608 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -426,26 +426,21 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu = getTPLReadLoc(moduleId, pcfg); if(found){ if(traceTPL) println("*** reading tmodel "); - 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 ; - } + validateRascalTplVersion(tm, tplLoc); + 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 ; } catch e: { return : ", tplLoc)]), ms>; } - msg = "outdated Rascal TPL version (required: ; please update the `rascal` dependency in `pom.xml` to version or higher, and clear the `target` folder)"; - throw rascalTplVersionError(qualifiedModuleName, tplLoc, tmVersion, msg); } mloc = tplLoc; try { diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc index 529ea547d4d..d0a20178d71 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc @@ -244,7 +244,7 @@ ModuleStatus getImportAndExtendGraph(MODID moduleId, ModuleStatus ms){ return completeModuleStatus(ms); } } - } catch rascalTplVersionError(str moduleName, loc tplLoc, str version, str txt):{ + } catch RascalTplVersionError(str moduleName, loc tplLoc, str version, str txt):{ ms = addProperty(moduleName2moduleId(moduleName), ms, tpl_version_error()); // Need to recheck since TModel uses incompatible TPL version } diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/CompileTimeError.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/CompileTimeError.rsc index 1b597e6fefd..be51615c26a 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/CompileTimeError.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/CompileTimeError.rsc @@ -31,5 +31,5 @@ import Message; data Exception = CompileTimeError(Message msg) | InternalCompilerError(Message msg) - | rascalTplVersionError(str moduleName, loc tpl, str version, str txt) + | RascalTplVersionError(str moduleName, loc tpl, str version, str txt) ; \ No newline at end of file From f6c735e55d898655edbd9acb08374823c25ae275 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 16:35:26 +0100 Subject: [PATCH 05/17] Update `getRascalModuleLocation` to be able to optionally return a default location when otherwise an exception would be thrown --- .../lang/rascalcore/check/CheckerCommon.rsc | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 0ab731ac608..8c0c3db9f48 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -317,12 +317,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; +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 getRascalModuleLocation(moduleId2moduleName(moduleId), ms.pathConfig); } /* @@ -442,10 +450,7 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu return : ", tplLoc)]), ms>; } } - mloc = tplLoc; - try { - mloc = getRascalModuleLocation(moduleId, ms); - } catch _: /* ignore when this fails */; + mloc = getRascalModuleLocation(moduleId, ms, ifThrows = tplLoc); return ", mloc)]), ms>; } From 9f004d8096bf2099ff220b9bbf08baab0d537d7f Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 10:05:23 +0100 Subject: [PATCH 06/17] Fix issue that an import was missing --- .../compiler/lang/rascalcore/check/BasicRascalConfig.rsc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc index 70ab58292e5..dcf77f9a949 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc @@ -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; From 4ec683e19af70441084c614a2237be46994c3a07 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 09:31:54 +0100 Subject: [PATCH 07/17] Remove `tmodelLIFO` field from `moduleStatus` constructor and replace `removeOldestTModelFromCache` with `...Random...` (scaffolding) --- .../lang/rascalcore/check/CheckerCommon.rsc | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 8c0c3db9f48..95e7102174c 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -93,7 +93,6 @@ data ModuleStatus = 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, @@ -144,7 +143,6 @@ ModuleStatus moduleStatus( // TEMPORARY FOR COMPATIBILITY BETWEEN VERSI 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, @@ -155,7 +153,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()); @@ -346,8 +344,8 @@ 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); + for(candidate <- ms.tmodels){ + ms = removeTModel(candidate, ms); todo -= candidate; } for(candidate <- todo){ @@ -390,13 +388,13 @@ ModuleStatus removeTModel(MODID candidate, ModuleStatus ms){ return ms; } -ModuleStatus removeOldestTModelFromCache(ModuleStatus ms){ - if(size(ms.tmodelLIFO) > 0){ - candidate = ms.tmodelLIFO[-1]; - ms = removeTModel(candidate, ms); - if(traceTModelCache) println("*** deleted tmodel , tmodels: , lifo: "); - ms.tmodelLIFO = ms.tmodelLIFO[..-1]; - } +ModuleStatus removeRandomTModelFromCacheExcept(MODID moduleId, ModuleStatus ms){ + candidates = ms.tmodels<0> - moduleId; + if ({} == candidates) return ms; + + candidate = getOneFrom(candidates); + ms = removeTModel(candidate, ms); + if(traceTModelCache) println("*** deleted tmodel , tmodels: "); return ms; } @@ -404,11 +402,8 @@ ModuleStatus addTModel (MODID moduleId, TModel tm, ModuleStatus ms){ if(traceTModelCache) println("addTModel: "); 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); - } + while(size(ms.tmodels) >= tmodelCacheSize && {moduleId} != ms.tmodels<0>){ + ms = removeRandomTModelFromCacheExcept(moduleId, ms); } } return ms; @@ -427,8 +422,8 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu tm = ms.tmodels[moduleId]; return ; } - while(size(ms.tmodels) >= tmodelCacheSize && size(ms.tmodelLIFO) > 0 && ms.tmodelLIFO[-1] != moduleId){ - ms = removeOldestTModelFromCache(ms); + while(size(ms.tmodels) >= tmodelCacheSize && {moduleId} != ms.tmodels<0>){ + ms = removeRandomTModelFromCacheExcept(moduleId, ms); } qualifiedModuleName = moduleId2moduleName(moduleId); = getTPLReadLoc(moduleId, pcfg); @@ -444,7 +439,6 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu } 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 ; } catch e: { return : ", tplLoc)]), ms>; From c23e3ea12df6911bd19ba751cfbb52c9a13cfe40 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 10:09:45 +0100 Subject: [PATCH 08/17] Remove `tmodels` field from `moduleStatus` constructor and update code accordingly (no more TModel caching; always write/read to TPL files) --- .../lang/rascalcore/check/Checker.rsc | 2 +- .../lang/rascalcore/check/CheckerCommon.rsc | 44 +++---------------- 2 files changed, 6 insertions(+), 40 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc index d0123337ab7..2a092cd802b 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc @@ -164,6 +164,7 @@ ModuleStatus rascalTModelForLocs( } = libraryDependenciesAreCompatible(mids, ms); + // compatibleLibs = false; if(compatibleLibs && uptodateTPls(mlocs, mnames, pcfg)){ for (i <- index(mids)) { @@ -474,7 +475,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); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 95e7102174c..cf3f6298b90 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -92,7 +92,6 @@ data ModuleStatus = rel[MODID, PathRole, MODID] paths, map[MODID, Module] parseTrees, list[MODID] parseTreeLIFO, - map[MODID, TModel] tmodels, set[MODID] changedModules, map[MODID,loc] moduleLocs, map[MODID,datetime] moduleLastModified, @@ -142,7 +141,6 @@ ModuleStatus moduleStatus( // TEMPORARY FOR COMPATIBILITY BETWEEN VERSI rel[MODID, PathRole, MODID] _paths, map[MODID, Module] _parseTrees, list[MODID] _parseTreeLIFO, - map[MODID, TModel] _tmodels, set[MODID] _changedModules, map[MODID,loc] _moduleLocs, map[MODID,datetime] _moduleLastModified, @@ -153,7 +151,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()); @@ -344,12 +342,7 @@ int tmodelCacheSize = 30; // should be > 0 ModuleStatus clearTModelCache(ModuleStatus ms){ todo = { mname | mname <- ms.status, hasProperty(mname, ms, bom_update_needed())}; - for(candidate <- ms.tmodels){ - ms = removeTModel(candidate, ms); - todo -= candidate; - } for(candidate <- todo){ - ms = removeTModel(candidate, ms); ms = deleteProperty(candidate, ms, bom_update_needed()); } return ms; @@ -365,15 +358,12 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ + { }; } -ModuleStatus removeTModel(MODID candidate, ModuleStatus ms){ - assert isModuleId(candidate) : "removeTModel: "; - if( candidate in ms.tmodels - && !hasAnyProperty(candidate, ms, tpl_saved(), rsc_not_found(), tpl_from_library()) - ){ +ModuleStatus saveTModel(MODID candidate, TModel tm, ModuleStatus ms){ + assert isModuleId(candidate) : "saveTModel: "; + if(!hasAnyProperty(candidate, ms, tpl_saved(), tpl_from_library())){ pcfg = ms.pathConfig; ms = deleteProperty(candidate, ms, bom_update_needed()); = getTPLWriteLoc(candidate, pcfg); - tm = ms.tmodels[candidate]; ms = addProperty(candidate, ms, tpl_saved()); if(ms.compilerConfig.verbose) println("Saving tmodel for before removing from cache"); try { @@ -384,28 +374,12 @@ ModuleStatus removeTModel(MODID candidate, ModuleStatus ms){ ms.messages[candidate] += { error("Cannot write TPL file , reason: ", mloc) }; } } - ms.tmodels = delete(ms.tmodels, candidate); - return ms; -} - -ModuleStatus removeRandomTModelFromCacheExcept(MODID moduleId, ModuleStatus ms){ - candidates = ms.tmodels<0> - moduleId; - if ({} == candidates) return ms; - - candidate = getOneFrom(candidates); - ms = removeTModel(candidate, ms); - if(traceTModelCache) println("*** deleted tmodel , tmodels: "); return ms; } ModuleStatus addTModel (MODID moduleId, TModel tm, ModuleStatus ms){ if(traceTModelCache) println("addTModel: "); - if(tmodelCacheSize > 0){ - ms.tmodels[moduleId] = tm; - while(size(ms.tmodels) >= tmodelCacheSize && {moduleId} != ms.tmodels<0>){ - ms = removeRandomTModelFromCacheExcept(moduleId, ms); - } - } + ms = saveTModel(moduleId, tm, ms); return ms; } @@ -418,13 +392,6 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu assert isModuleId(moduleId) : "getTModelForModule: "; if(traceTModelCache) println("getTModelForModule: "); pcfg = ms.pathConfig; - if(moduleId in ms.tmodels){ - tm = ms.tmodels[moduleId]; - return ; - } - while(size(ms.tmodels) >= tmodelCacheSize && {moduleId} != ms.tmodels<0>){ - ms = removeRandomTModelFromCacheExcept(moduleId, ms); - } qualifiedModuleName = moduleId2moduleName(moduleId); = getTPLReadLoc(moduleId, pcfg); if(found){ @@ -432,7 +399,6 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu try { tm = readBinaryValueFile(ReifiedTModel, tplLoc); validateRascalTplVersion(tm, tplLoc); - ms.tmodels[moduleId] = tm; mloc = getRascalModuleLocation(moduleId, ms); if(isModuleLocationInLibs(mloc, pcfg)){ ms = addProperty(moduleId, ms, tpl_from_library()); From 1bed283d17053b8f3d43dbaf45b2eac30a575661 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 13:10:40 +0100 Subject: [PATCH 09/17] Add function to load/save/read/write TPL files (using memoization instead of explicit caching) --- .../lang/rascalcore/check/CheckerCommon.rsc | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index cf3f6298b90..bb5faec008c 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -68,6 +68,8 @@ void checkSupportedByParserGenerator(Tree t, Collector c){ }); } +alias BOM = rel[str, datetime, PathRole]; + data ModuleProperty = rsc_not_found() | rsc_changed() @@ -97,6 +99,7 @@ data ModuleStatus = map[MODID,datetime] moduleLastModified, map[MODID, set[Message]] messages, map[MODID, set[ModuleProperty]] status, + map[loc /* Not `MODID` but TPL location */, BOM] boms, PathConfig pathConfig, RascalCompilerConfig compilerConfig ); @@ -146,12 +149,13 @@ ModuleStatus moduleStatus( // TEMPORARY FOR COMPATIBILITY BETWEEN VERSI map[MODID,datetime] _moduleLastModified, map[MODID, set[Message]] _messages, map[MODID, set[ModuleProperty]] _status, + map[MODID, BOM] _boms, PathConfig pathConfig, RascalCompilerConfig compilerConfig) = newModuleStatus(pathConfig, compilerConfig); ModuleStatus newModuleStatus(PathConfig pcfg, TypePalConfig tcfg) - = moduleStatus({}, (), [], {}, (), (), (), (), pcfg, tcfg); + = moduleStatus({}, (), [], {}, (), (), (), (), (), pcfg, tcfg); ModuleStatus newModuleStatus() = newModuleStatus(pathConfig(), tconfig()); @@ -358,8 +362,9 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ + { }; } -ModuleStatus saveTModel(MODID candidate, TModel tm, ModuleStatus ms){ - assert isModuleId(candidate) : "saveTModel: "; +ModuleStatus addTModel(MODID candidate, TModel tm, ModuleStatus ms){ + assert isModuleId(candidate) : "addTModel: "; + if(traceTModelCache) println("addTModel: "); if(!hasAnyProperty(candidate, ms, tpl_saved(), tpl_from_library())){ pcfg = ms.pathConfig; ms = deleteProperty(candidate, ms, bom_update_needed()); @@ -367,7 +372,7 @@ ModuleStatus saveTModel(MODID candidate, TModel tm, ModuleStatus ms){ ms = addProperty(candidate, ms, tpl_saved()); if(ms.compilerConfig.verbose) println("Saving tmodel for before removing from cache"); try { - writeBinaryValueFile(tplLoc, tm, optimizeSharing = true); + ms = saveTModel(tplLoc, tm, ms); if(traceTPL) println("Written "); } catch value e: { mloc = ms.moduleLocs[candidate] ? |unknown:///|; @@ -377,13 +382,53 @@ ModuleStatus saveTModel(MODID candidate, TModel tm, ModuleStatus ms){ return ms; } -ModuleStatus addTModel (MODID moduleId, TModel tm, ModuleStatus ms){ - if(traceTModelCache) println("addTModel: "); - ms = saveTModel(moduleId, tm, ms); +// -- Loading/saving TPL files -- + +ModuleStatus saveTModel(loc l, TModel tm, ModuleStatus ms) { + writeTplFile(l, tm); + if (key_bom in tm.store, BOM bom := tm.store[key_bom]) { + ms.boms[l] = bom; + } return ms; } -private type[TModel] ReifiedTModel = #TModel; // precomputed for efficiency +TModel loadTModel(loc l, ModuleStatus ms) { + datetime stamp = lastModified(l); + TModel tm = readTplFile(l, stamp); + if (l in ms.boms) { + tm.store[key_bom] = ms.boms[l]; + } + return tm; +} + +// -- Reading/writing TPL files (including memoization) -- + +private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency + +// Auxiliary global variables to mock `readTplFile` calls (in the body of `writeTplFile`) +private tuple[loc, datetime, TModel] readTplFileCallNone = <|unknown:///|, startOfEpoch, tmodel()>; +private tuple[loc, datetime, TModel] readTplFileCall = readTplFileCallNone; + +@memo +private TModel readTplFile(loc l, datetime stamp) { + if ( := readTplFileCall) { + readTplFileCall = readTplFileCallNone; + return tm; + } else { + return readBinaryValueFile(ReifiedTModel, l); + } +} + +private void writeTplFile(loc l, TModel tm) { + writeBinaryValueFile(l, tm, optimizeSharing = true); + + // Force `tm` to be memoized by mocking a `readTplFile` call + datetime stamp = lastModified(l); + readTplFileCall = ; + readTplFile(l, stamp); +} + +// -- tuple[bool, TModel, ModuleStatus] getTModelForModule(str moduleName, ModuleStatus ms) = getTModelForModule(moduleName2moduleId(moduleName), ms); @@ -397,7 +442,7 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu if(found){ if(traceTPL) println("*** reading tmodel "); try { - tm = readBinaryValueFile(ReifiedTModel, tplLoc); + tm = loadTModel(tplLoc, ms); validateRascalTplVersion(tm, tplLoc); mloc = getRascalModuleLocation(moduleId, ms); if(isModuleLocationInLibs(mloc, pcfg)){ From 60bc0fb2ae8223c85396f2c3367bc1ed1fcb94df Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 13:41:20 +0100 Subject: [PATCH 10/17] Remove remnants of explicit TModel cache (replaced with memoization in previous commit) --- .../lang/rascalcore/check/Checker.rsc | 7 +----- .../lang/rascalcore/check/CheckerCommon.rsc | 22 ------------------- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc index 2a092cd802b..3849c25fbb5 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/Checker.rsc @@ -194,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>; = stronglyConnectedComponentsAndTopSort(imports_and_extends); map[MODID, set[MODID]] module2component = (m : c | c <- components, m <- c); @@ -404,7 +399,7 @@ ModuleStatus rascalTModelForLocs( } jobEnd(jobName); - return clearTModelCache(ms); + return ms; } bool implicitlyUsesParseTree(str modulePath, rel[str, AType] factsByPath){ diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index bb5faec008c..84c6bd8ef58 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -171,7 +171,6 @@ bool isModuleLocationInLibs(loc l, PathConfig pcfg){ bool traceTPL = false; bool traceParseTreeCache = false; -bool traceTModelCache = false; tuple[bool,loc] getTPLReadLoc(str qualifiedModuleName, PathConfig pcfg) = getTPLReadLoc(moduleName2moduleId(qualifiedModuleName), pcfg); @@ -333,25 +332,6 @@ loc getRascalModuleLocation(MODID moduleId, ModuleStatus ms, loc ifThrows = |unk } } -/* - * 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 <- todo){ - ms = deleteProperty(candidate, ms, bom_update_needed()); - } - return ms; -} - rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ pcfg = ms.pathConfig; qualifiedModuleName = moduleId2moduleName(moduleId); @@ -364,7 +344,6 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ ModuleStatus addTModel(MODID candidate, TModel tm, ModuleStatus ms){ assert isModuleId(candidate) : "addTModel: "; - if(traceTModelCache) println("addTModel: "); if(!hasAnyProperty(candidate, ms, tpl_saved(), tpl_from_library())){ pcfg = ms.pathConfig; ms = deleteProperty(candidate, ms, bom_update_needed()); @@ -435,7 +414,6 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(str moduleName, ModuleStatu tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatus ms){ assert isModuleId(moduleId) : "getTModelForModule: "; - if(traceTModelCache) println("getTModelForModule: "); pcfg = ms.pathConfig; qualifiedModuleName = moduleId2moduleName(moduleId); = getTPLReadLoc(moduleId, pcfg); From 59280b5e079a62ec72d796d7185195d7bd6e2a91 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 2 Dec 2026 15:49:39 +0100 Subject: [PATCH 11/17] Replace `lastModified` with `md5HashFile` as stamps for `readTplFile` memoization --- .../lang/rascalcore/check/CheckerCommon.rsc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 84c6bd8ef58..3406052bd8c 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -372,8 +372,7 @@ ModuleStatus saveTModel(loc l, TModel tm, ModuleStatus ms) { } TModel loadTModel(loc l, ModuleStatus ms) { - datetime stamp = lastModified(l); - TModel tm = readTplFile(l, stamp); + TModel tm = readTplFile(l, toStamp(l)); if (l in ms.boms) { tm.store[key_bom] = ms.boms[l]; } @@ -385,11 +384,11 @@ TModel loadTModel(loc l, ModuleStatus ms) { private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency // Auxiliary global variables to mock `readTplFile` calls (in the body of `writeTplFile`) -private tuple[loc, datetime, TModel] readTplFileCallNone = <|unknown:///|, startOfEpoch, tmodel()>; -private tuple[loc, datetime, TModel] readTplFileCall = readTplFileCallNone; +private tuple[loc, value, TModel] readTplFileCallNone = <|unknown:///|, 0, tmodel()>; +private tuple[loc, value, TModel] readTplFileCall = readTplFileCallNone; @memo -private TModel readTplFile(loc l, datetime stamp) { +private TModel readTplFile(loc l, value stamp) { if ( := readTplFileCall) { readTplFileCall = readTplFileCallNone; return tm; @@ -402,11 +401,15 @@ private void writeTplFile(loc l, TModel tm) { writeBinaryValueFile(l, tm, optimizeSharing = true); // Force `tm` to be memoized by mocking a `readTplFile` call - datetime stamp = lastModified(l); + value stamp = toStamp(l); readTplFileCall = ; readTplFile(l, stamp); } +private value toStamp(loc l) { + return md5HashFile(l); +} + // -- tuple[bool, TModel, ModuleStatus] getTModelForModule(str moduleName, ModuleStatus ms) From c90756b34f32847ffb852aaeada778a9f2dde522 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 10:07:46 +0100 Subject: [PATCH 12/17] Move logic to find TPL locations into `saveTModel` and `loadTModel` --- .../lang/rascalcore/check/CheckerCommon.rsc | 93 ++++++++++--------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 3406052bd8c..21e5f90c2be 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -342,20 +342,17 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ + { }; } -ModuleStatus addTModel(MODID candidate, TModel tm, ModuleStatus ms){ - assert isModuleId(candidate) : "addTModel: "; - if(!hasAnyProperty(candidate, ms, tpl_saved(), tpl_from_library())){ - pcfg = ms.pathConfig; - ms = deleteProperty(candidate, ms, bom_update_needed()); - = getTPLWriteLoc(candidate, pcfg); - ms = addProperty(candidate, ms, tpl_saved()); - if(ms.compilerConfig.verbose) println("Saving tmodel for before removing from cache"); +ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){ + assert isModuleId(moduleId) : "addTModel: "; + if(!hasAnyProperty(moduleId, ms, tpl_saved(), tpl_from_library())){ + ms = deleteProperty(moduleId, ms, bom_update_needed()); + if(ms.compilerConfig.verbose) println("Saving tmodel for "); try { - ms = saveTModel(tplLoc, tm, ms); - if(traceTPL) println("Written "); - } catch value e: { - mloc = ms.moduleLocs[candidate] ? |unknown:///|; - ms.messages[candidate] += { error("Cannot write TPL file , reason: ", mloc) }; + ms = saveTModel(moduleId, tm, ms); + ms = addProperty(moduleId, ms, tpl_saved()); + } catch e: { + mloc = ms.moduleLocs[moduleId] ? |unknown:///|; + ms.messages[moduleId] += { error("Cannot save TModel for , reason: ", mloc) }; } } return ms; @@ -363,20 +360,31 @@ ModuleStatus addTModel(MODID candidate, TModel tm, ModuleStatus ms){ // -- Loading/saving TPL files -- -ModuleStatus saveTModel(loc l, TModel tm, ModuleStatus ms) { - writeTplFile(l, tm); - if (key_bom in tm.store, BOM bom := tm.store[key_bom]) { - ms.boms[l] = bom; +ModuleStatus saveTModel(MODID moduleId, TModel tm, ModuleStatus ms) throws IO { + if (<_, loc l> := getTPLWriteLoc(moduleId, ms.pathConfig)) { + if (traceTPL) println("Saving to ..."); + writeTplFile(l, tm); + if (key_bom in tm.store, BOM bom := tm.store[key_bom]) { + ms.boms[l] = bom; + } + return ms; + } else { + throw IO("Cannot find TPL location for "); } - return ms; } -TModel loadTModel(loc l, ModuleStatus ms) { - TModel tm = readTplFile(l, toStamp(l)); - if (l in ms.boms) { - tm.store[key_bom] = ms.boms[l]; +TModel loadTModel(MODID moduleId, ModuleStatus ms) throws IO { + if ( := getTPLReadLoc(moduleId, ms.pathConfig)) { + if (traceTPL) println("Loading from ..."); + TModel tm = readTplFile(l, toStamp(l)); + validateRascalTplVersion(tm, l); + if (l in ms.boms) { + tm.store[key_bom] = ms.boms[l]; + } + return tm; + } else { + throw IO("Cannot find TPL location for "); } - return tm; } // -- Reading/writing TPL files (including memoization) -- @@ -389,16 +397,20 @@ private tuple[loc, value, TModel] readTplFileCall = readTplFileCallNone; @memo private TModel readTplFile(loc l, value stamp) { + // Special case (mock call to force memoization) if ( := readTplFileCall) { readTplFileCall = readTplFileCallNone; return tm; - } else { - return readBinaryValueFile(ReifiedTModel, l); } + // General case + TModel tm = readBinaryValueFile(ReifiedTModel, l); + if (traceTPL) println("Read "); + return tm; } private void writeTplFile(loc l, TModel tm) { writeBinaryValueFile(l, tm, optimizeSharing = true); + if (traceTPL) println("Written "); // Force `tm` to be memoized by mocking a `readTplFile` call value stamp = toStamp(l); @@ -417,27 +429,20 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(str moduleName, ModuleStatu tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatus ms){ assert isModuleId(moduleId) : "getTModelForModule: "; - pcfg = ms.pathConfig; - qualifiedModuleName = moduleId2moduleName(moduleId); - = getTPLReadLoc(moduleId, pcfg); - if(found){ - if(traceTPL) println("*** reading tmodel "); - try { - tm = loadTModel(tplLoc, ms); - validateRascalTplVersion(tm, tplLoc); - 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 - return ; - } catch e: { - return : ", tplLoc)]), ms>; + loc moduleLoc = getRascalModuleLocation(moduleId, ms, ifThrows = |unknown:///|); + try { + TModel tm = loadTModel(moduleId, ms); + ms = addProperty(moduleId, ms, tpl_uptodate(), tpl_saved()); + if (isModuleLocationInLibs(moduleLoc, ms.pathConfig)) { + ms = addProperty(moduleId, ms, tpl_from_library()); } + return ; + } catch e: RascalTplVersionError(_, _, _, _): { + throw e; + } catch e: { + str moduleName = moduleId2moduleName(moduleId); + return , reason: ", moduleLoc)]), ms>; } - mloc = getRascalModuleLocation(moduleId, ms, ifThrows = tplLoc); - return ", mloc)]), ms>; } map[MODID, TModel] getImportedAndExtendedTModelsForModule(MODID moduleId, ModuleStatus ms, bool includeImported = true, bool includeExtended = true) { From 7a8009e0e935a4cc6a92f738a2d64735ad5c7ddf Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 10:18:19 +0100 Subject: [PATCH 13/17] Fix issue that `ModuleStatus` wasn't backward-compatible --- .../compiler/lang/rascalcore/check/CheckerCommon.rsc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 21e5f90c2be..1fb5ab0b057 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -140,16 +140,17 @@ 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, - 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, map[MODID, set[ModuleProperty]] _status, - map[MODID, BOM] _boms, PathConfig pathConfig, RascalCompilerConfig compilerConfig) = newModuleStatus(pathConfig, compilerConfig); From dc02e91e440b470b12561d5dadb47e178e619a61 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 12:25:51 +0100 Subject: [PATCH 14/17] Add functions to load/save BOMs --- .../lang/rascalcore/check/CheckerCommon.rsc | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 1fb5ab0b057..8a094da512e 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -99,7 +99,7 @@ data ModuleStatus = map[MODID,datetime] moduleLastModified, map[MODID, set[Message]] messages, map[MODID, set[ModuleProperty]] status, - map[loc /* Not `MODID` but TPL location */, BOM] boms, + map[MODID, BOM] boms, PathConfig pathConfig, RascalCompilerConfig compilerConfig ); @@ -343,6 +343,20 @@ rel[str,datetime,PathRole] makeBom(MODID moduleId, ModuleStatus ms){ + { }; } +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; +} + +TModel loadBOM(MODID moduleId, TModel tm, ModuleStatus ms) throws IO { + if (moduleId in ms.boms) { + tm.store[key_bom] = ms.boms[moduleId]; + } + return tm; +} + ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){ assert isModuleId(moduleId) : "addTModel: "; if(!hasAnyProperty(moduleId, ms, tpl_saved(), tpl_from_library())){ @@ -350,6 +364,7 @@ ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){ if(ms.compilerConfig.verbose) println("Saving tmodel for "); try { ms = saveTModel(moduleId, tm, ms); + ms = saveBOM(moduleId, tm, ms); ms = addProperty(moduleId, ms, tpl_saved()); } catch e: { mloc = ms.moduleLocs[moduleId] ? |unknown:///|; @@ -365,9 +380,6 @@ ModuleStatus saveTModel(MODID moduleId, TModel tm, ModuleStatus ms) throws IO { if (<_, loc l> := getTPLWriteLoc(moduleId, ms.pathConfig)) { if (traceTPL) println("Saving to ..."); writeTplFile(l, tm); - if (key_bom in tm.store, BOM bom := tm.store[key_bom]) { - ms.boms[l] = bom; - } return ms; } else { throw IO("Cannot find TPL location for "); @@ -379,9 +391,6 @@ TModel loadTModel(MODID moduleId, ModuleStatus ms) throws IO { if (traceTPL) println("Loading from ..."); TModel tm = readTplFile(l, toStamp(l)); validateRascalTplVersion(tm, l); - if (l in ms.boms) { - tm.store[key_bom] = ms.boms[l]; - } return tm; } else { throw IO("Cannot find TPL location for "); @@ -433,6 +442,7 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu loc moduleLoc = getRascalModuleLocation(moduleId, ms, ifThrows = |unknown:///|); try { TModel tm = loadTModel(moduleId, ms); + 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()); From 8237e0952b281607c84b959a3d9f07aac79cb62c Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 13:38:56 +0100 Subject: [PATCH 15/17] Remove `ModuleStatus` from the signatures of `saveTModel` and `loadTModel` --- .../lang/rascalcore/check/CheckerCommon.rsc | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 8a094da512e..d9fb4155e3a 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -363,7 +363,7 @@ ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){ ms = deleteProperty(moduleId, ms, bom_update_needed()); if(ms.compilerConfig.verbose) println("Saving tmodel for "); try { - ms = saveTModel(moduleId, tm, ms); + saveTModel(moduleId, tm, ms.pathConfig); ms = saveBOM(moduleId, tm, ms); ms = addProperty(moduleId, ms, tpl_saved()); } catch e: { @@ -376,20 +376,19 @@ ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){ // -- Loading/saving TPL files -- -ModuleStatus saveTModel(MODID moduleId, TModel tm, ModuleStatus ms) throws IO { - if (<_, loc l> := getTPLWriteLoc(moduleId, ms.pathConfig)) { - if (traceTPL) println("Saving to ..."); +void saveTModel(MODID moduleId, TModel tm, PathConfig pcfg) throws IO { + if (<_, loc l> := getTPLWriteLoc(moduleId, pcfg)) { + if (traceTPL) println("saveModule: Saving `` to ..."); writeTplFile(l, tm); - return ms; } else { throw IO("Cannot find TPL location for "); } } -TModel loadTModel(MODID moduleId, ModuleStatus ms) throws IO { - if ( := getTPLReadLoc(moduleId, ms.pathConfig)) { - if (traceTPL) println("Loading from ..."); - TModel tm = readTplFile(l, toStamp(l)); +TModel loadTModel(MODID moduleId, PathConfig pcfg) throws IO { + if ( := getTPLReadLoc(moduleId, pcfg)) { + if (traceTPL) println("loadModule: Loading `` from ..."); + TModel tm = readTplFile(l); validateRascalTplVersion(tm, l); return tm; } else { @@ -405,6 +404,10 @@ private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency private tuple[loc, value, TModel] readTplFileCallNone = <|unknown:///|, 0, tmodel()>; private tuple[loc, value, TModel] readTplFileCall = readTplFileCallNone; +private TModel readTplFile(loc l) { + return readTplFile(l, toStamp(l)); +} + @memo private TModel readTplFile(loc l, value stamp) { // Special case (mock call to force memoization) @@ -441,7 +444,7 @@ tuple[bool, TModel, ModuleStatus] getTModelForModule(MODID moduleId, ModuleStatu assert isModuleId(moduleId) : "getTModelForModule: "; loc moduleLoc = getRascalModuleLocation(moduleId, ms, ifThrows = |unknown:///|); try { - TModel tm = loadTModel(moduleId, ms); + TModel tm = loadTModel(moduleId, ms.pathConfig); tm = loadBOM(moduleId, tm, ms); ms = addProperty(moduleId, ms, tpl_uptodate(), tpl_saved()); if (isModuleLocationInLibs(moduleLoc, ms.pathConfig)) { From afd4a5678cc86fefa74824f2010beae292b461ab Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 15:12:55 +0100 Subject: [PATCH 16/17] Add module `TModelIO` with utility functions to save/load `TModel` values to/from TPL files (moved out of `CheckerCommon`) --- .../lang/rascalcore/check/CheckerCommon.rsc | 96 +------------ .../lang/rascalcore/check/TModelIO.rsc | 135 ++++++++++++++++++ 2 files changed, 136 insertions(+), 95 deletions(-) create mode 100644 src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index d9fb4155e3a..ecef716357b 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc @@ -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; @@ -170,40 +171,8 @@ bool isModuleLocationInLibs(loc l, PathConfig pcfg){ return res; } -bool traceTPL = false; bool traceParseTreeCache = 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: "; - parts = split("/", moduleId.path); - parts = parts[0 .. size(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 + "" + fileName; - if(exists(fileLoc)){ - if(traceTPL) println("getTPLReadLoc: =\> "); - return ; - } else { - ;// if(traceTPL) - // println("getTPLReadLoc: DOES NOT EXIST: "); - } - } - return ; -} - -tuple[bool,loc] getTPLWriteLoc(MODID moduleId, PathConfig pcfg){ - assert isModuleId(moduleId) : "getTPLWriteLoc: "; - qualifiedModuleName = moduleId2moduleName(moduleId); - fileName = ".tpl"; - tplLoc = getGeneratedResourcesDir(qualifiedModuleName, pcfg) + fileName; - return ; -} - datetime startOfEpoch = $2000-01-01T00:00:00.000+00:00$; datetime getLastModified(MODID moduleId, ModuleStatus ms){ @@ -374,69 +343,6 @@ ModuleStatus addTModel(MODID moduleId, TModel tm, ModuleStatus ms){ return ms; } -// -- Loading/saving TPL files -- - -void saveTModel(MODID moduleId, TModel tm, PathConfig pcfg) throws IO { - if (<_, loc l> := getTPLWriteLoc(moduleId, pcfg)) { - if (traceTPL) println("saveModule: Saving `` to ..."); - writeTplFile(l, tm); - } else { - throw IO("Cannot find TPL location for "); - } -} - -TModel loadTModel(MODID moduleId, PathConfig pcfg) throws IO { - if ( := getTPLReadLoc(moduleId, pcfg)) { - if (traceTPL) println("loadModule: Loading `` from ..."); - TModel tm = readTplFile(l); - validateRascalTplVersion(tm, l); - return tm; - } else { - throw IO("Cannot find TPL location for "); - } -} - -// -- Reading/writing TPL files (including memoization) -- - -private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency - -// Auxiliary global variables to mock `readTplFile` calls (in the body of `writeTplFile`) -private tuple[loc, value, TModel] readTplFileCallNone = <|unknown:///|, 0, tmodel()>; -private tuple[loc, value, TModel] readTplFileCall = readTplFileCallNone; - -private TModel readTplFile(loc l) { - return readTplFile(l, toStamp(l)); -} - -@memo -private TModel readTplFile(loc l, value stamp) { - // Special case (mock call to force memoization) - if ( := readTplFileCall) { - readTplFileCall = readTplFileCallNone; - return tm; - } - // General case - TModel tm = readBinaryValueFile(ReifiedTModel, l); - if (traceTPL) println("Read "); - return tm; -} - -private void writeTplFile(loc l, TModel tm) { - writeBinaryValueFile(l, tm, optimizeSharing = true); - if (traceTPL) println("Written "); - - // Force `tm` to be memoized by mocking a `readTplFile` call - value stamp = toStamp(l); - readTplFileCall = ; - readTplFile(l, stamp); -} - -private value toStamp(loc l) { - return md5HashFile(l); -} - -// -- - tuple[bool, TModel, ModuleStatus] getTModelForModule(str moduleName, ModuleStatus ms) = getTModelForModule(moduleName2moduleId(moduleName), ms); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc new file mode 100644 index 00000000000..f0c7cf2c6ee --- /dev/null +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc @@ -0,0 +1,135 @@ +@license{ +Copyright (c) 2018-2026, NWO-I CWI, Swat.engineering and Paul Klint +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +} +@synopsis{ +Utility functions to save/load `TModel` values to/from TPL files +} +@bootstrapParser +module lang::rascalcore::check::TModelIO + +import Exception; +import IO; +import List; +import String; +import ValueIO; +import lang::rascalcore::CompilerPathConfig; +import lang::rascalcore::check::BasicRascalConfig; +import lang::rascalcore::check::LogicalLocations; +import lang::rascalcore::compile::util::Names; +import util::Memo; + +bool traceTPL = false; + +// -- Saving/loading TPL files -- + +void saveTModel(MODID moduleId, TModel tm, PathConfig pcfg) throws IO { + if (<_, loc l> := getTPLWriteLoc(moduleId, pcfg)) { + if (traceTPL) println("saveTModel: Saving `` to ..."); + writeTplFile(l, tm); + } else { + throw IO("Cannot find TPL location for "); + } +} + +TModel loadTModel(MODID moduleId, PathConfig pcfg) throws IO { + if ( := getTPLReadLoc(moduleId, pcfg)) { + if (traceTPL) println("loadTModel: Loading `` from ..."); + TModel tm = readTplFile(l); + validateRascalTplVersion(tm, l); + return tm; + } else { + throw IO("Cannot find TPL location for "); + } +} + +// -- TPL file locations -- + +tuple[bool, loc] getTPLWriteLoc(MODID moduleId, PathConfig pcfg){ + assert isModuleId(moduleId) : "getTPLWriteLoc: "; + qualifiedModuleName = moduleId2moduleName(moduleId); + fileName = ".tpl"; + tplLoc = getGeneratedResourcesDir(qualifiedModuleName, pcfg) + fileName; + return ; +} + +tuple[bool, loc] getTPLReadLoc(str qualifiedModuleName, PathConfig pcfg) + = getTPLReadLoc(moduleName2moduleId(qualifiedModuleName), pcfg); + +tuple[bool, loc] getTPLReadLoc(MODID moduleId, PathConfig pcfg){ + assert isModuleId(moduleId) : "getTPLReadLoc: "; + parts = split("/", moduleId.path); + parts = parts[0 .. size(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 + "" + fileName; + if (exists(fileLoc)){ + if (traceTPL) println("getTPLReadLoc: =\> "); + return ; + } + } + return ; +} + +// -- Writing/reading TPL files (including memoization) -- + +private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency + +// Auxiliary global variables to mock `readTplFile` calls (in the body of `writeTplFile`) +private tuple[loc, value, TModel] readTplFileCallNone = <|unknown:///|, 0, tmodel()>; +private tuple[loc, value, TModel] readTplFileCall = readTplFileCallNone; + +private void writeTplFile(loc l, TModel tm) { + writeBinaryValueFile(l, tm, optimizeSharing = true); + if (traceTPL) println("writeTplFile: Written `` to "); + + // Force `tm` to be memoized by mocking a `readTplFile` call + value stamp = toStamp(l); + readTplFileCall = ; + readTplFile(l, stamp); +} + +private TModel readTplFile(loc l) { + return readTplFile(l, toStamp(l)); +} + +@memo={expireAfter(minutes=10), maximumSize(1000)} +private TModel readTplFile(loc l, value stamp) { + // Special case (mock call to force memoization) + if ( := readTplFileCall) { + readTplFileCall = readTplFileCallNone; + return tm; + } + // General case + TModel tm = readBinaryValueFile(ReifiedTModel, l); + if (traceTPL) println("readTplFile: Read `` from "); + return tm; +} + +private value toStamp(loc l) { + return md5HashFile(l); +} \ No newline at end of file From 6575297355731a5c1604ea115cdb407c3f2ff515 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Fri, 4 Dec 2026 16:35:45 +0100 Subject: [PATCH 17/17] Add `TplFiles` ADT to simplify caching-through-memoization implementation --- .../lang/rascalcore/check/TModelIO.rsc | 66 +++++++++++-------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc index f0c7cf2c6ee..3fcb6f8be7f 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc @@ -97,39 +97,51 @@ tuple[bool, loc] getTPLReadLoc(MODID moduleId, PathConfig pcfg){ // -- Writing/reading TPL files (including memoization) -- -private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency - -// Auxiliary global variables to mock `readTplFile` calls (in the body of `writeTplFile`) -private tuple[loc, value, TModel] readTplFileCallNone = <|unknown:///|, 0, tmodel()>; -private tuple[loc, value, TModel] readTplFileCall = readTplFileCallNone; - private void writeTplFile(loc l, TModel tm) { - writeBinaryValueFile(l, tm, optimizeSharing = true); + tplFiles.write(l, tm); if (traceTPL) println("writeTplFile: Written `` to "); - - // Force `tm` to be memoized by mocking a `readTplFile` call - value stamp = toStamp(l); - readTplFileCall = ; - readTplFile(l, stamp); } private TModel readTplFile(loc l) { - return readTplFile(l, toStamp(l)); -} - -@memo={expireAfter(minutes=10), maximumSize(1000)} -private TModel readTplFile(loc l, value stamp) { - // Special case (mock call to force memoization) - if ( := readTplFileCall) { - readTplFileCall = readTplFileCallNone; - return tm; - } - // General case - TModel tm = readBinaryValueFile(ReifiedTModel, l); + TModel tm = tplFiles.read(l); if (traceTPL) println("readTplFile: Read `` from "); return tm; } -private value toStamp(loc l) { - return md5HashFile(l); -} \ No newline at end of file +@synopsis{ +Representation of a collection of TPL files. The contents of the files in the +collection are represented as `TModel` values and cached through memoization. +} +private data TplFiles = files(void(loc, TModel) write, TModel(loc) read); + +private type[TModel] ReifiedTModel = #TModel; // Precomputed for efficiency + +private TplFiles tplFiles = TplFiles() { + + // Computes a `TModel` value when no such value has been memoized yet for + // (the arguments of) the next `access` call. It must be set right before + // each such a call. + TModel() accessIfNotMemoized; + + void TplFiles_write(loc l, TModel tm) { + writeBinaryValueFile(l, tm, optimizeSharing = true); + accessIfNotMemoized = TModel() { return tm; }; + access(l, toStamp(l)); // Force caching through memoization + } + + TModel TplFiles_read(loc l) { + accessIfNotMemoized = TModel() { return readBinaryValueFile(ReifiedTModel, l); }; + return access(l, toStamp(l)); + } + + @memo={expireAfter(minutes=10), maximumSize(1000)} + private TModel access(loc l, value _stamp) { // `_stamp` is used for memoization + return accessIfNotMemoized(); + } + + private value toStamp(loc l) { + return md5HashFile(l); // More robust than `lastModified` when multiple typecheckers run in parallel and possibly write TPL files for the same modules (e.g., in the integration tests) + } + + return TplFiles::files(TplFiles_write, TplFiles_read); +}();