Skip to content
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
15 changes: 10 additions & 5 deletions base/src/main/java/org/aya/resolve/ResolveInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
* @param imports importing information, it only contains the modules that is explicitly imported,
* should not be confused with the {@code import} in {@link ModuleContext#importModuleContext}
* the prim factory will be copied to the current one
* @param reExports re-exporting module, it is {@link ModuleName.Qualified} rather than {@link String}
* because we can re-export a module inside another module without import it.
*/
@Debug.Renderer(text = "modulePath().toString()")
public record ResolveInfo(
Expand All @@ -53,7 +51,6 @@ public record ResolveInfo(
@NotNull AyaBinOpSet opSet,
@NotNull MutableMap<AnyDef, OpRenameInfo> opRename,
@NotNull MutableMap<ModuleName.Qualified, ImportInfo> imports,
@NotNull MutableMap<ModuleName.Qualified, UseHide> reExports,
@NotNull MutableGraph<TyckOrder> depGraph
) {
public ResolveInfo(
Expand All @@ -63,7 +60,7 @@ public ResolveInfo(
@NotNull AyaBinOpSet opSet
) {
this(thisModule, primFactory, shapeFactory, new GlobalInstanceSet(), opSet,
MutableMap.create(), MutableMap.create(), MutableMap.create(), MutableGraph.create());
MutableMap.create(), MutableMap.create(), MutableGraph.create());
}
public @NotNull TyckState makeTyckState() {
return new TyckState(shapeFactory, primFactory);
Expand All @@ -76,7 +73,13 @@ public ExprTycker newTycker(@NotNull Reporter reporter) {
return new ExprTycker(makeTyckState(), new InstanceSet(instancesSet), reporter, modulePath());
}

public record ImportInfo(@NotNull ResolveInfo resolveInfo, boolean reExport) { }
/// @param open only used by serialization, not null if it should be [ResolveInfo#open]
public record ImportInfo(
@NotNull ResolveInfo resolveInfo,
boolean reExport,
@Nullable Stmt.Accessibility open
) { }

public record OpRenameInfo(
@NotNull Context bindCtx, @NotNull RenamedOpDecl renamed,
@NotNull BindBlock bind, boolean reExport
Expand Down Expand Up @@ -107,6 +110,8 @@ public void renameOp(
opRename.put(defVar, new OpRenameInfo(bindCtx, renamed, bind, reExport));
}

/// Called when a module opens another imported module, note that [#opSet] and [#shapeFactory] are not affected by
/// {@param acc}
public void open(@NotNull ResolveInfo other, @NotNull SourcePos sourcePos, @NotNull Stmt.Accessibility acc) {
// open defined operator and their bindings
opSet.importBind(other.opSet, sourcePos);
Expand Down
8 changes: 3 additions & 5 deletions base/src/main/java/org/aya/resolve/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.aya.syntax.concrete.stmt.ModuleName;
import org.aya.syntax.context.Candidate;
import org.aya.syntax.context.ContextView;
import org.aya.syntax.ref.AnyVar;
import org.aya.syntax.ref.GenerateKind;
import org.aya.syntax.ref.LocalVar;
import org.aya.syntax.ref.ModulePath;
import org.aya.syntax.ref.*;
import org.aya.util.position.SourcePos;
import org.aya.util.reporter.Reporter;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -103,7 +100,8 @@ default Context bind(@NotNull LocalVar ref, @NotNull Predicate<@Nullable Candida
return derive(new ModulePath(ImmutableSeq.of(extraName)));
}

/// Note that this won't increase [QPath#fileLevelSize]
default @NotNull ModuleContext derive(@NotNull ModulePath extraName) {
return new PhysicalModuleContext(this, modulePath().derive(extraName));
return new PhysicalModuleContext(this, qualifiedPath().derive(extraName));
}
}
5 changes: 3 additions & 2 deletions base/src/main/java/org/aya/resolve/context/EmptyContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.aya.syntax.context.ModuleExport;
import org.aya.syntax.ref.AnyVar;
import org.aya.syntax.ref.ModulePath;
import org.aya.syntax.ref.QPath;
import org.aya.util.position.SourcePos;
import org.aya.util.reporter.Reporter;
import org.jetbrains.annotations.NotNull;
Expand All @@ -29,10 +30,10 @@ public record EmptyContext(@NotNull Path underlyingFile) implements Context {
) { return null; }

@Override public @NotNull PhysicalModuleContext derive(@NotNull ModulePath extraName) {
return new PhysicalModuleContext(this, extraName);
return new PhysicalModuleContext(this, QPath.fileLevel(extraName));
}

@Override public @NotNull ModulePath modulePath() {
@Override public @NotNull QPath qualifiedPath() {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.aya.syntax.context.ModuleSymbol;
import org.aya.syntax.ref.AnyVar;
import org.aya.syntax.ref.ModulePath;
import org.aya.syntax.ref.QPath;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
Expand All @@ -18,20 +19,20 @@ public record NoExportContext(
@NotNull Context parent,
@NotNull ModuleSymbol<AnyVar> symbols,
@NotNull MutableMap<ModuleName.Qualified, ModuleExport> modules,
@Override @NotNull ModulePath modulePath
@Override @NotNull QPath qualifiedPath
) implements ModuleContext {
public NoExportContext(
@NotNull Context parent,
@NotNull ModuleSymbol<AnyVar> symbols,
@NotNull MutableMap<ModuleName.Qualified, ModuleExport> modules
) {
this(parent, symbols, modules, parent.modulePath().derive(":NoExport"));
this(parent, symbols, modules, parent.qualifiedPath().derive(":NoExport"));
}

public NoExportContext(@NotNull Context parent) {
this(parent, new ModuleSymbol<>(), MutableHashMap.create());
}

@Override public @NotNull Path underlyingFile() { return parent.underlyingFile(); }
@Override public @NotNull ModuleExport exports() { return new ModuleExport(); }
@Override public @NotNull ModuleExport exports() { return new ModuleExport(qualifiedPath); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.aya.syntax.context.ModuleSymbol;
import org.aya.syntax.ref.AnyDefVar;
import org.aya.syntax.ref.AnyVar;
import org.aya.syntax.ref.ModulePath;
import org.aya.syntax.ref.QPath;
import org.aya.util.position.SourcePos;
import org.aya.util.reporter.Reporter;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,17 +23,18 @@
*/
public class PhysicalModuleContext implements ModuleContext {
public final @NotNull Context parent;
public final @NotNull ModuleExport exports = new ModuleExport();
public final @NotNull ModuleExport exports;
public final @NotNull ModuleSymbol<AnyVar> symbols = new ModuleSymbol<>();
public final @NotNull MutableMap<ModuleName.Qualified, ModuleExport> modules = MutableHashMap.create();
private final @NotNull ModulePath modulePath;
@Override public @NotNull ModulePath modulePath() { return modulePath; }
private final @NotNull QPath qualifiedPath;
@Override public @NotNull QPath qualifiedPath() { return qualifiedPath; }

private @Nullable NoExportContext exampleContext;

public PhysicalModuleContext(@NotNull Context parent, @NotNull ModulePath modulePath) {
public PhysicalModuleContext(@NotNull Context parent, @NotNull QPath qualifiedPath) {
this.parent = parent;
this.modulePath = modulePath;
this.qualifiedPath = qualifiedPath;
this.exports = new ModuleExport(qualifiedPath);
}

@Override public boolean importModule(
Expand Down
14 changes: 6 additions & 8 deletions base/src/main/java/org/aya/resolve/visitor/StmtPreResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ImmutableSeq<ResolvingStmt> resolveStmt(@NotNull ImmutableSeq<Stmt> stmts
context.importModuleContext(importedName, mod, cmd.accessibility(), cmd.sourcePos(), thisReporter);
resolveInfo.primFactory().importFrom(success.primFactory());

var importInfo = new ResolveInfo.ImportInfo(success, cmd.accessibility() == Stmt.Accessibility.Public);
var importInfo = new ResolveInfo.ImportInfo(success, cmd.accessibility() == Stmt.Accessibility.Public, null);
resolveInfo.imports().put(importedName, importInfo);
yield null;
}
Expand All @@ -103,15 +103,13 @@ public ImmutableSeq<ResolvingStmt> resolveStmt(@NotNull ImmutableSeq<Stmt> stmts
var success = ctx.openModule(mod, acc, cmd.sourcePos(), useHide, thisReporter);
if (!success) yield null;

// store top-level re-exports
// FIXME: this is not enough, because submodule export definitions are not stored
if (ctx == resolveInfo.thisModule()) {
if (acc == Stmt.Accessibility.Public) resolveInfo.reExports().put(mod, useHide);
}
// open necessities from imported modules (not submodules)
// because the module itself and its submodules share the same ResolveInfo
resolveInfo.imports().getOption(mod).ifDefined(modResolveInfo ->
resolveInfo.open(modResolveInfo.resolveInfo(), cmd.sourcePos(), acc));
resolveInfo.imports().getOption(mod).ifDefined(modResolveInfo -> {
// TODO: open regardless the strategy(use / hide)? the user may be able to refer hidden data by `shapeFactory`
resolveInfo.open(modResolveInfo.resolveInfo(), cmd.sourcePos(), acc);
resolveInfo.imports().put(mod, new ResolveInfo.ImportInfo(modResolveInfo.resolveInfo(), modResolveInfo.reExport(), acc));
});

// renaming as infix
if (useHide.strategy() == UseHide.Strategy.Using) useHide.list().forEach(use -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void loadFile(@NotNull Path file) {
private @NotNull ResolveInfo makeResolveInfo(@NotNull ModuleContext ctx) {
var resolveInfo = new ResolveInfo(ctx, tcState.primFactory, tcState.shapeFactory, opSet);
imports.forEach(ii -> resolveInfo.imports().put(
ii.modulePath().asName(), new ResolveInfo.ImportInfo(ii, false)));
ii.modulePath().asName(), new ResolveInfo.ImportInfo(ii, false, null)));
return resolveInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import org.aya.syntax.concrete.stmt.Stmt;
import org.aya.syntax.context.ModuleExport;
import org.aya.syntax.context.ModuleSymbol;
import org.aya.syntax.ref.AnyDefVar;
import org.aya.syntax.ref.AnyVar;
import org.aya.syntax.ref.DefVar;
import org.aya.syntax.ref.ModulePath;
import org.aya.syntax.ref.*;
import org.aya.util.RepoLike;
import org.aya.util.position.SourcePos;
import org.aya.util.reporter.Reporter;
Expand All @@ -34,7 +31,8 @@ public final class ReplContext extends PhysicalModuleContext implements RepoLike
private @Nullable ImmutableMap<String, ModuleTrie> moduleTree = null;

public ReplContext(@NotNull Context parent, @NotNull ModulePath name) {
super(parent, name);
// QPath is used for serialization only
super(parent, QPath.fileLevel(name));
}

@Override public boolean importSymbol(
Expand Down
Loading