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/BasicRascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc index 814048e778c..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; @@ -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 (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..3849c25fbb5 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)) { @@ -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>; = stronglyConnectedComponentsAndTopSort(imports_and_extends); map[MODID, set[MODID]] module2component = (m : c | c <- components, m <- c); @@ -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 }; @@ -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 `` has outdated Rascal TPL version , no source found", tpl) ]; ms.messages[mid] ? {} += { error("Import/extend of `` has ", @@ -403,7 +399,7 @@ ModuleStatus rascalTModelForLocs( } jobEnd(jobName); - return clearTModelCache(ms); + return ms; } bool implicitlyUsesParseTree(str modulePath, rel[str, AType] factsByPath){ @@ -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); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CheckerCommon.rsc index 73f69c58bfa..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; @@ -68,6 +69,8 @@ void checkSupportedByParserGenerator(Tree t, Collector c){ }); } +alias BOM = rel[str, datetime, PathRole]; + data ModuleProperty = rsc_not_found() | rsc_changed() @@ -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 ); @@ -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, @@ -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()); @@ -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: "; - 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$; @@ -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){ @@ -359,124 +312,63 @@ 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 "); +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: "; - 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()); - = 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 { - writeBinaryValueFile(tplLoc, tm, optimizeSharing = true); - if(traceTPL) println("Written "); - } catch value e: { - mloc = ms.moduleLocs[candidate] ? |unknown:///|; - ms.messages[candidate] += { error("Cannot write TPL file , reason: ", 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 , tmodels: , lifo: "); - ms.tmodelLIFO = ms.tmodelLIFO[..-1]; - } - return ms; -} - -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); - } +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 { + 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 , reason: ", 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: "; - if(traceTModelCache) println("getTModelForModule: "); - pcfg = ms.pathConfig; - if(moduleId in ms.tmodels){ - tm = ms.tmodels[moduleId]; - return ; - } - while(size(ms.tmodels) >= tmodelCacheSize && size(ms.tmodelLIFO) > 0 && ms.tmodelLIFO[-1] != moduleId){ - ms = removeOldestTModelFromCache(ms); - } - qualifiedModuleName = moduleId2moduleName(moduleId); - = 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 ; - } - } catch e: { - return : ", 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 (required: ; please update the `rascal` dependency in `pom.xml` to version or higher, and clear the `target` folder)"; - throw rascalTplVersionError(qualifiedModuleName, tplLoc, tmVersion, msg); + return ; + } catch e: RascalTplVersionError(_, _, _, _): { + throw e; + } catch e: { + str moduleName = moduleId2moduleName(moduleId); + return , reason: ", moduleLoc)]), ms>; } - mloc = tplLoc; - try { - mloc = getRascalModuleLocation(moduleId, ms); - } catch _: /* ignore when this fails */; - 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){ diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/Import.rsc index 4b37a1842eb..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 } @@ -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/check/TModelIO.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc new file mode 100644 index 00000000000..3fcb6f8be7f --- /dev/null +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/TModelIO.rsc @@ -0,0 +1,147 @@ +@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 void writeTplFile(loc l, TModel tm) { + tplFiles.write(l, tm); + if (traceTPL) println("writeTplFile: Written `` to "); +} + +private TModel readTplFile(loc l) { + TModel tm = tplFiles.read(l); + if (traceTPL) println("readTplFile: Read `` from "); + return tm; +} + +@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); +}(); 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 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);