Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing CompilerContext.Module #8144

Merged
merged 9 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public Void run(RuntimeContext ctx) {
return;
}

serializationManager.serializeModule(module, useGlobalCacheLocations, false);
serializationManager.serializeModule(
module.asCompilerModule(), useGlobalCacheLocations, false);
});
} finally {
ctx.locking().releaseWriteCompilationLock();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.interpreter.instrument.command

import java.util.logging.Level
import org.enso.interpreter.runtime.Module
import org.enso.interpreter.instrument.{CacheInvalidation, InstrumentFrame}
import org.enso.interpreter.instrument.execution.RuntimeContext
import org.enso.interpreter.instrument.job.{EnsureCompiledJob, ExecuteJob}
Expand Down Expand Up @@ -55,7 +56,7 @@ class RenameProjectCmd(
)

projectModules.foreach { module =>
module.setIndexed(false)
Module.fromCompilerModule(module).setIndexed(false)
ctx.endpoint.sendToClient(
Api.Response(
Api.SuggestionsDatabaseModuleUpdateNotification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ final class AnalyzeModuleInScopeJob(
.log(Level.FINEST, s"Analyzing module in scope ${module.getName}")
val moduleName = module.getName
val newSuggestions =
SuggestionBuilder(module, ctx.executionService.getContext.getCompiler)
SuggestionBuilder(
module.asCompilerModule(),
ctx.executionService.getContext.getCompiler
)
.build(moduleName, module.getIr)
.filter(Suggestion.isGlobal)
val prevExports = ModuleExports(moduleName.toString, Set())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object AnalyzeModuleJob {
SuggestionBuilder(changeset.source, compiler)
.build(moduleName, changeset.ir)
val newSuggestions =
SuggestionBuilder(module, compiler)
SuggestionBuilder(module.asCompilerModule(), compiler)
.build(moduleName, module.getIr)
val diff = SuggestionDiff
.compute(prevSuggestions, newSuggestions)
Expand All @@ -77,7 +77,7 @@ object AnalyzeModuleJob {
ctx.executionService.getLogger
.log(Level.FINEST, s"Analyzing not-indexed module ${module.getName}")
val newSuggestions =
SuggestionBuilder(module, compiler)
SuggestionBuilder(module.asCompilerModule(), compiler)
.build(moduleName, module.getIr)
val prevExports = ModuleExports(moduleName.toString, Set())
val newExports = exportsBuilder.build(moduleName, module.getIr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ final class EnsureCompiledJob(
case Right(compilerResult) =>
val status = runCompilationDiagnostics(module)
(
modules.addAll(compilerResult.compiledModules).addOne(module),
modules
.addAll(
compilerResult.compiledModules
.map(Module.fromCompilerModule(_))
)
.addOne(module),
statuses += status
)
}
Expand Down Expand Up @@ -168,7 +173,7 @@ final class EnsureCompiledJob(
.runModule(
module.getIr,
ModuleContext(
module,
module.asCompilerModule(),
compilerConfig = ctx.executionService.getContext.getCompilerConfig
)
)
Expand Down Expand Up @@ -230,7 +235,8 @@ final class EnsureCompiledJob(
if (!compilationStage.isAtLeast(CompilationStage.AFTER_CODEGEN)) {
ctx.executionService.getLogger
.log(Level.FINEST, s"Compiling ${module.getName}.")
val result = ctx.executionService.getContext.getCompiler.run(module)
val result = ctx.executionService.getContext.getCompiler
.run(module.asCompilerModule())
result.copy(compiledModules =
result.compiledModules.filter(_.getName != module.getName)
)
Expand Down Expand Up @@ -498,7 +504,11 @@ final class EnsureCompiledJob(
val packageRepository =
ctx.executionService.getContext.getCompiler.packageRepository
packageRepository.getMainProjectPackage
.map(pkg => packageRepository.getModulesForLibrary(pkg.libraryName))
.map(pkg =>
packageRepository
.getModulesForLibrary(pkg.libraryName)
.map(Module.fromCompilerModule(_))
)
.getOrElse(Seq())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
import com.oracle.truffle.api.source.Source;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Level;
import org.enso.compiler.Cache;
import org.enso.compiler.Compiler;
import org.enso.compiler.ModuleCache;
import org.enso.compiler.PackageRepository;
import org.enso.compiler.Passes;
import org.enso.compiler.SerializationManager;
import org.enso.compiler.core.ir.Expression;
import org.enso.compiler.data.BindingsMap;
import org.enso.compiler.data.CompilerConfig;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.Module;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.scope.LocalScope;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.scope.TopLevelScope;
import org.enso.pkg.Package;
import org.enso.pkg.QualifiedName;
import org.enso.polyglot.CompilationStage;

Expand Down Expand Up @@ -47,7 +50,7 @@ public interface CompilerContext {

void notifySerializeModule(QualifiedName moduleName);

TopLevelScope getTopScope();
Module findTopScopeModule(String name);

// threads
boolean isCreateThreadAllowed();
Expand Down Expand Up @@ -113,4 +116,38 @@ public static interface Updater {

void invalidateCache();
}

public abstract static class Module {
public abstract Source getSource() throws IOException;

public abstract String getPath();

public abstract Package<TruffleFile> getPackage();

public abstract boolean isSameAs(org.enso.interpreter.runtime.Module m);

public abstract org.enso.interpreter.runtime.scope.ModuleScope getScope();

public abstract QualifiedName getName();

public abstract Type findType(String name);

public abstract BindingsMap getBindingsMap();

public abstract TruffleFile getSourceFile();

public abstract List<QualifiedName> getDirectModulesRefs();

public abstract ModuleCache getCache();

public abstract CompilationStage getCompilationStage();

public abstract boolean isSynthetic();

public abstract boolean hasCrossModuleLinks();

public abstract org.enso.compiler.core.ir.Module getIr();

public abstract boolean isPrivate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected ExecutableNode parse(InlineParsingRequest request) throws InlineParsin
scala.Option.apply(new PrintStream(outputRedirect))
);
var moduleContext = new ModuleContext(
module, redirectConfigWithStrictErrors,
module.asCompilerModule(), redirectConfigWithStrictErrors,
scala.Option.empty(),
scala.Option.empty(),
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Object execute(VirtualFrame frame) {
} else {
module = new Module(name, null, sourceCode.getCharacters().toString());
}
ctx.getPackageRepository().registerModuleCreatedInRuntime(module);
ctx.getPackageRepository().registerModuleCreatedInRuntime(module.asCompilerModule());
}
// Note [Static Passes]
return module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.WeakHashMap;
import java.util.logging.Level;
import org.enso.compiler.ModuleCache;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.context.SimpleUpdate;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.Expression;
Expand Down Expand Up @@ -151,6 +152,16 @@ private Module(
this.synthetic = synthetic;
}

/**
* Unwraps runtime module from compiler module.
*
* @param module module created by {@link #asCompilerModule()} method
* @return
*/
public static Module fromCompilerModule(CompilerContext.Module module) {
return ((TruffleCompilerContext.Module) module).unsafeModule();
}

/**
* Creates an empty module.
*
Expand Down Expand Up @@ -374,7 +385,7 @@ private void compile(EnsoContext context) throws IOException {
if (source == null) return;
scope.reset();
compilationStage = CompilationStage.INITIAL;
context.getCompiler().run(this);
context.getCompiler().run(asCompilerModule());
}

/** @return IR defined by this module. */
Expand Down Expand Up @@ -519,6 +530,15 @@ void setHasCrossModuleLinks(boolean hasCrossModuleLinks) {
this.hasCrossModuleLinks = hasCrossModuleLinks;
}

/**
* Turns this module into appropriate {@link CompilerContext} wrapper.
*
* @return instance of {@link CompilerContext.Module} that delegates to this module
*/
public final CompilerContext.Module asCompilerModule() {
return new TruffleCompilerContext.Module(this);
}

/**
* Handles member invocations through the polyglot API.
*
Expand Down Expand Up @@ -606,12 +626,12 @@ private static Object evalExpression(
}

private static Object generateDocs(Module module, EnsoContext context) {
return context.getCompiler().generateDocs(module);
return context.getCompiler().generateDocs(module.asCompilerModule());
}

@CompilerDirectives.TruffleBoundary
private static Object gatherImportStatements(Module module, EnsoContext context) {
String[] imports = context.getCompiler().gatherImportStatements(module);
String[] imports = context.getCompiler().gatherImportStatements(module.asCompilerModule());
return ArrayLikeHelpers.wrapStrings(imports);
}

Expand Down
Loading