fix indication for when imported modules are library modules or not.

This fixes a bug where syslib and such gets optimized away when it is loaded from an alternative library location using the configurable target library path property setting.
This commit is contained in:
Irmen de Jong 2025-01-26 21:19:29 +01:00
parent bb75be0b44
commit ee784e1ccc
9 changed files with 38 additions and 20 deletions

View File

@ -21,12 +21,12 @@ object ImportFileSystem {
fun expandTilde(path: Path): Path = Path(expandTilde(path.toString()))
fun getFile(path: Path): SourceCode {
fun getFile(path: Path, isLibrary: Boolean=false): SourceCode {
val normalized = path.absolute().normalize()
val cached = cache[normalized.toString()]
if (cached != null)
return cached
val file = SourceCode.File(normalized)
val file = SourceCode.File(normalized, isLibrary)
cache[normalized.toString()] = file
return file
}

View File

@ -22,6 +22,11 @@ sealed class SourceCode {
*/
abstract val isFromFilesystem: Boolean
/**
* Whether this [SourceCode] instance was created from a library module file
*/
abstract val isFromLibrary: Boolean
/**
* The logical name of the source code unit. Usually the module's name.
*/
@ -76,6 +81,7 @@ sealed class SourceCode {
override val text = origText.replace("\\R".toRegex(), "\n") // normalize line endings
override val isFromResources = false
override val isFromFilesystem = false
override val isFromLibrary = false
override val origin = "$STRINGSOURCEPREFIX${System.identityHashCode(text).toString(16)}"
override val name = "<unnamed-text>"
}
@ -89,7 +95,7 @@ sealed class SourceCode {
* @throws NoSuchFileException if the file does not exist
* @throws FileSystemException if the file cannot be read
*/
internal class File(path: Path): SourceCode() {
internal class File(path: Path, override val isFromLibrary: Boolean): SourceCode() {
override val text: String
override val origin: String
override val name: String
@ -120,6 +126,7 @@ sealed class SourceCode {
override val isFromResources = true
override val isFromFilesystem = false
override val isFromLibrary = true
override val origin = "$LIBRARYFILEPREFIX$normalized"
override val text: String
override val name: String
@ -146,6 +153,7 @@ sealed class SourceCode {
class Generated(override val name: String) : SourceCode() {
override val isFromResources: Boolean = false
override val isFromFilesystem: Boolean = false
override val isFromLibrary: Boolean = false
override val origin: String = name
override val text: String = "<generated code node, no text representation>"
}

View File

@ -73,8 +73,8 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
try {
val totalTime = measureTimeMillis {
val sourceDirs = if(compTarget.libraryPath!=null) listOf(compTarget.libraryPath.toString()) + args.sourceDirs else args.sourceDirs
val (program, options, imported) = parseMainModule(args.filepath, args.errors, compTarget, sourceDirs)
val libraryDirs = if(compTarget.libraryPath!=null) listOf(compTarget.libraryPath.toString()) else emptyList()
val (program, options, imported) = parseMainModule(args.filepath, args.errors, compTarget, args.sourceDirs, libraryDirs)
compilationOptions = options
with(compilationOptions) {
@ -319,12 +319,13 @@ private class BuiltinFunctionsFacade(functions: Map<String, FSignature>): IBuilt
fun parseMainModule(filepath: Path,
errors: IErrorReporter,
compTarget: ICompilationTarget,
sourceDirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
sourceDirs: List<String>,
libraryDirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
val program = Program(filepath.nameWithoutExtension, bf, compTarget, compTarget)
bf.program = program
val importer = ModuleImporter(program, compTarget.name, errors, sourceDirs)
val importer = ModuleImporter(program, compTarget.name, errors, sourceDirs, libraryDirs)
val importedModuleResult = importer.importMainModule(filepath)
importedModuleResult.onFailure { throw it }
errors.report()

View File

@ -21,9 +21,11 @@ import kotlin.io.path.exists
class ModuleImporter(private val program: Program,
private val compilationTargetName: String,
val errors: IErrorReporter,
sourceDirs: List<String>) {
sourceDirs: List<String>,
libraryDirs: List<String>) {
private val sourcePaths: List<Path> = sourceDirs.map { Path(it).absolute().normalize() }.toSortedSet().toList()
private val libraryPaths: List<Path> = libraryDirs.map { Path(it).absolute().normalize() }.toSortedSet().toList()
fun importMainModule(filePath: Path): Result<Module, NoSuchFileException> {
val searchIn = (listOf(Path("").absolute()) + sourcePaths).toSortedSet()
@ -33,7 +35,7 @@ class ModuleImporter(private val program: Program,
if(programPath.exists()) {
println("Compiling program ${Path("").absolute().relativize(programPath)}")
println("Compiler target: $compilationTargetName")
val source = ImportFileSystem.getFile(programPath)
val source = ImportFileSystem.getFile(programPath, false)
return Ok(importModule(source))
}
}
@ -131,17 +133,25 @@ class ModuleImporter(private val program: Program,
private fun getModuleFromFile(name: String, importingModule: Module?): Result<SourceCode, NoSuchFileException> {
val fileName = "$name.p8"
val locations =
if (importingModule == null) { // <=> imported from library module
val normalLocations =
if (importingModule == null) {
sourcePaths
} else {
val pathFromImportingModule = (Path(importingModule.position.file).parent ?: Path("")).absolute().normalize()
listOf(pathFromImportingModule) + sourcePaths
}
locations.forEach {
libraryPaths.forEach {
try {
return Ok(ImportFileSystem.getFile(it.resolve(fileName)))
return Ok(ImportFileSystem.getFile(it.resolve(fileName), true))
} catch (_: NoSuchFileException) {
}
}
normalLocations.forEach {
try {
return Ok(ImportFileSystem.getFile(it.resolve(fileName), false))
} catch (_: NoSuchFileException) {
}
}

View File

@ -767,9 +767,9 @@ class SimplifiedAstMaker(private val program: Program, private val errors: IErro
} else {
val sib = Path(source.origin).resolveSibling(filename)
if (sib.isRegularFile())
Ok(ImportFileSystem.getFile(sib).text)
Ok(ImportFileSystem.getFile(sib, source.isFromLibrary).text)
else
Ok(ImportFileSystem.getFile(Path(filename)).text)
Ok(ImportFileSystem.getFile(Path(filename), source.isFromLibrary).text)
}
}

View File

@ -29,7 +29,7 @@ class TestModuleImporter: FunSpec({
}
fun makeImporter(errors: IErrorReporter? = null, searchIn: Iterable<String>) =
ModuleImporter(program, "blah", errors ?: ErrorReporterForTests(false), searchIn.toList())
ModuleImporter(program, "blah", errors ?: ErrorReporterForTests(false), searchIn.toList(), emptyList())
fun makeImporter(errors: IErrorReporter?, vararg searchIn: String): ModuleImporter {
return makeImporter(errors, searchIn.asList())

View File

@ -92,7 +92,7 @@ main {
val filenameBase = "on_the_fly_test_" + sourceText.hashCode().toUInt().toString(16)
val filepath = outputDir.resolve("$filenameBase.p8")
filepath.toFile().writeText(sourceText)
val (program, options, importedfiles) = parseMainModule(filepath, errors, C64Target(), emptyList())
val (program, options, importedfiles) = parseMainModule(filepath, errors, C64Target(), emptyList(), emptyList())
program.toplevelModule.name shouldBe filenameBase
withClue("all imports other than the test source must have been internal resources library files") {

View File

@ -341,7 +341,7 @@ open class Module(final override val statements: MutableList<Statement>,
program.encoding.defaultEncoding
}
val isLibrary get() = source.isFromResources
val isLibrary get() = source.isFromLibrary
}

View File

@ -23,8 +23,7 @@ class Program(val name: String,
init {
// insert a container module for all interned strings later
val internedStringsModule =
Module(mutableListOf(), Position.DUMMY, SourceCode.Generated(internedStringsModuleName))
val internedStringsModule = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated(internedStringsModuleName))
val block = Block(internedStringsModuleName, null, mutableListOf(), true, Position.DUMMY)
val directive = Directive("%option", listOf(DirectiveArg(null,"no_symbol_prefixing", null, Position.DUMMY)), Position.DUMMY)
block.statements.add(directive)