Skip to content

Commit

Permalink
use common algorithm in runtime method resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Oct 25, 2024
1 parent 436b660 commit 383e2ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
* <ol>
* <li>in the runtime ({@link
* org.enso.interpreter.runtime.scope.ModuleScope#lookupMethodDefinition}),
* <li>in the type checker ({@link
* org.enso.compiler.pass.analyse.types.MethodTypeResolver#lookupMethodDefinition}).
* <li>in the type checker ({@link org.enso.compiler.pass.analyse.types.MethodTypeResolver}).
* </ol>
*
* <p>To ensure that all usages stay consistent, they should all rely on the logic implemented in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.enso.compiler.context.CommonModuleScopeShape;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.context.MethodResolutionAlgorithm;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.Module;
import org.enso.interpreter.runtime.callable.function.Function;
Expand All @@ -20,7 +22,8 @@

/** A representation of Enso's per-file top-level scope. */
@ExportLibrary(TypesLibrary.class)
public final class ModuleScope implements EnsoObject {
public final class ModuleScope
implements EnsoObject, CommonModuleScopeShape<Function, Type, ImportExportScope> {
private final Type associatedType;
private final Module module;
private final Map<String, Supplier<TruffleObject>> polyglotSymbols;
Expand Down Expand Up @@ -88,21 +91,40 @@ public Module getModule() {
*/
@CompilerDirectives.TruffleBoundary
public Function lookupMethodDefinition(Type type, String name) {
var definedWithAtom = type.getDefinitionScope().getMethodForType(type, name);
if (definedWithAtom != null) {
return definedWithAtom;
return methodResolutionAlgorithm.lookupMethodDefinition(type, name);
}

private final RuntimeMethodResolution methodResolutionAlgorithm = new RuntimeMethodResolution();

private class RuntimeMethodResolution
extends MethodResolutionAlgorithm<Function, Type, ImportExportScope, ModuleScope> {
@Override
protected ModuleScope findDefinitionScope(Type type) {
return type.getDefinitionScope();
}

var definedHere = getMethodForType(type, name);
if (definedHere != null) {
return definedHere;
@Override
protected ModuleScope getCurrentModuleScope() {
return ModuleScope.this;
}

return imports.stream()
.map(scope -> scope.getExportedMethod(type, name))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
@Override
protected Function findExportedMethodInImportScope(
ImportExportScope importExportScope, Type type, String methodName) {
return importExportScope.getExportedMethod(type, methodName);
}

@Override
protected Function onMultipleDefinitionsFromImports(
String methodName, List<MethodFromImport<Function, ImportExportScope>> methodFromImports) {
assert !methodFromImports.isEmpty();
return methodFromImports.get(0).resolvedType();
}
}

@Override
public Collection<ImportExportScope> getImports() {
return imports;
}

/**
Expand Down

0 comments on commit 383e2ff

Please sign in to comment.