Skip to content

Commit 2f109a5

Browse files
authored
merge: Refactor CompiledModule/ResolveInfo (#1449)
This PR slightly refactors CompiledModule and ResolveInfo, by serializing/deserializing the `ResolveInfo.thisModule.export` directly instead of serializing/deserializing the context-related commands (such as Command.Import). Q: Why don't serialize context-related commands? A: Commands are imperative, thus the order is important, furthermore, some definitions act like a command, such as `inductive`, which introduces a sub-module, and it is very stupid to store the order information... Although serializing `ModuleExport` directly may increase the size of `,ayac`, especially `prelude.aya`.
2 parents fb4dbf2 + 090c8c0 commit 2f109a5

File tree

16 files changed

+443
-190
lines changed

16 files changed

+443
-190
lines changed

base/src/main/java/org/aya/resolve/ResolveInfo.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
* @param imports importing information, it only contains the modules that is explicitly imported,
4242
* should not be confused with the {@code import} in {@link ModuleContext#importModuleContext}
4343
* the prim factory will be copied to the current one
44-
* @param reExports re-exporting module, it is {@link ModuleName.Qualified} rather than {@link String}
45-
* because we can re-export a module inside another module without import it.
4644
*/
4745
@Debug.Renderer(text = "modulePath().toString()")
4846
public record ResolveInfo(
@@ -53,7 +51,6 @@ public record ResolveInfo(
5351
@NotNull AyaBinOpSet opSet,
5452
@NotNull MutableMap<AnyDef, OpRenameInfo> opRename,
5553
@NotNull MutableMap<ModuleName.Qualified, ImportInfo> imports,
56-
@NotNull MutableMap<ModuleName.Qualified, UseHide> reExports,
5754
@NotNull MutableGraph<TyckOrder> depGraph
5855
) {
5956
public ResolveInfo(
@@ -63,7 +60,7 @@ public ResolveInfo(
6360
@NotNull AyaBinOpSet opSet
6461
) {
6562
this(thisModule, primFactory, shapeFactory, new GlobalInstanceSet(), opSet,
66-
MutableMap.create(), MutableMap.create(), MutableMap.create(), MutableGraph.create());
63+
MutableMap.create(), MutableMap.create(), MutableGraph.create());
6764
}
6865
public @NotNull TyckState makeTyckState() {
6966
return new TyckState(shapeFactory, primFactory);
@@ -76,7 +73,13 @@ public ExprTycker newTycker(@NotNull Reporter reporter) {
7673
return new ExprTycker(makeTyckState(), new InstanceSet(instancesSet), reporter, modulePath());
7774
}
7875

79-
public record ImportInfo(@NotNull ResolveInfo resolveInfo, boolean reExport) { }
76+
/// @param open only used by serialization, not null if it should be [ResolveInfo#open]
77+
public record ImportInfo(
78+
@NotNull ResolveInfo resolveInfo,
79+
boolean reExport,
80+
@Nullable Stmt.Accessibility open
81+
) { }
82+
8083
public record OpRenameInfo(
8184
@NotNull Context bindCtx, @NotNull RenamedOpDecl renamed,
8285
@NotNull BindBlock bind, boolean reExport
@@ -107,6 +110,8 @@ public void renameOp(
107110
opRename.put(defVar, new OpRenameInfo(bindCtx, renamed, bind, reExport));
108111
}
109112

113+
/// Called when a module opens another imported module, note that [#opSet] and [#shapeFactory] are not affected by
114+
/// {@param acc}
110115
public void open(@NotNull ResolveInfo other, @NotNull SourcePos sourcePos, @NotNull Stmt.Accessibility acc) {
111116
// open defined operator and their bindings
112117
opSet.importBind(other.opSet, sourcePos);

base/src/main/java/org/aya/resolve/context/Context.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import org.aya.syntax.concrete.stmt.ModuleName;
99
import org.aya.syntax.context.Candidate;
1010
import org.aya.syntax.context.ContextView;
11-
import org.aya.syntax.ref.AnyVar;
12-
import org.aya.syntax.ref.GenerateKind;
13-
import org.aya.syntax.ref.LocalVar;
14-
import org.aya.syntax.ref.ModulePath;
11+
import org.aya.syntax.ref.*;
1512
import org.aya.util.position.SourcePos;
1613
import org.aya.util.reporter.Reporter;
1714
import org.jetbrains.annotations.NotNull;
@@ -103,7 +100,8 @@ default Context bind(@NotNull LocalVar ref, @NotNull Predicate<@Nullable Candida
103100
return derive(new ModulePath(ImmutableSeq.of(extraName)));
104101
}
105102

103+
/// Note that this won't increase [QPath#fileLevelSize]
106104
default @NotNull ModuleContext derive(@NotNull ModulePath extraName) {
107-
return new PhysicalModuleContext(this, modulePath().derive(extraName));
105+
return new PhysicalModuleContext(this, qualifiedPath().derive(extraName));
108106
}
109107
}

base/src/main/java/org/aya/resolve/context/EmptyContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.aya.syntax.context.ModuleExport;
99
import org.aya.syntax.ref.AnyVar;
1010
import org.aya.syntax.ref.ModulePath;
11+
import org.aya.syntax.ref.QPath;
1112
import org.aya.util.position.SourcePos;
1213
import org.aya.util.reporter.Reporter;
1314
import org.jetbrains.annotations.NotNull;
@@ -29,10 +30,10 @@ public record EmptyContext(@NotNull Path underlyingFile) implements Context {
2930
) { return null; }
3031

3132
@Override public @NotNull PhysicalModuleContext derive(@NotNull ModulePath extraName) {
32-
return new PhysicalModuleContext(this, extraName);
33+
return new PhysicalModuleContext(this, QPath.fileLevel(extraName));
3334
}
3435

35-
@Override public @NotNull ModulePath modulePath() {
36+
@Override public @NotNull QPath qualifiedPath() {
3637
throw new UnsupportedOperationException();
3738
}
3839

base/src/main/java/org/aya/resolve/context/NoExportContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.aya.syntax.context.ModuleSymbol;
1010
import org.aya.syntax.ref.AnyVar;
1111
import org.aya.syntax.ref.ModulePath;
12+
import org.aya.syntax.ref.QPath;
1213
import org.jetbrains.annotations.NotNull;
1314

1415
import java.nio.file.Path;
@@ -18,20 +19,20 @@ public record NoExportContext(
1819
@NotNull Context parent,
1920
@NotNull ModuleSymbol<AnyVar> symbols,
2021
@NotNull MutableMap<ModuleName.Qualified, ModuleExport> modules,
21-
@Override @NotNull ModulePath modulePath
22+
@Override @NotNull QPath qualifiedPath
2223
) implements ModuleContext {
2324
public NoExportContext(
2425
@NotNull Context parent,
2526
@NotNull ModuleSymbol<AnyVar> symbols,
2627
@NotNull MutableMap<ModuleName.Qualified, ModuleExport> modules
2728
) {
28-
this(parent, symbols, modules, parent.modulePath().derive(":NoExport"));
29+
this(parent, symbols, modules, parent.qualifiedPath().derive(":NoExport"));
2930
}
3031

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

3536
@Override public @NotNull Path underlyingFile() { return parent.underlyingFile(); }
36-
@Override public @NotNull ModuleExport exports() { return new ModuleExport(); }
37+
@Override public @NotNull ModuleExport exports() { return new ModuleExport(qualifiedPath); }
3738
}

base/src/main/java/org/aya/resolve/context/PhysicalModuleContext.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.aya.syntax.context.ModuleSymbol;
1111
import org.aya.syntax.ref.AnyDefVar;
1212
import org.aya.syntax.ref.AnyVar;
13-
import org.aya.syntax.ref.ModulePath;
13+
import org.aya.syntax.ref.QPath;
1414
import org.aya.util.position.SourcePos;
1515
import org.aya.util.reporter.Reporter;
1616
import org.jetbrains.annotations.NotNull;
@@ -23,17 +23,18 @@
2323
*/
2424
public class PhysicalModuleContext implements ModuleContext {
2525
public final @NotNull Context parent;
26-
public final @NotNull ModuleExport exports = new ModuleExport();
26+
public final @NotNull ModuleExport exports;
2727
public final @NotNull ModuleSymbol<AnyVar> symbols = new ModuleSymbol<>();
2828
public final @NotNull MutableMap<ModuleName.Qualified, ModuleExport> modules = MutableHashMap.create();
29-
private final @NotNull ModulePath modulePath;
30-
@Override public @NotNull ModulePath modulePath() { return modulePath; }
29+
private final @NotNull QPath qualifiedPath;
30+
@Override public @NotNull QPath qualifiedPath() { return qualifiedPath; }
3131

3232
private @Nullable NoExportContext exampleContext;
3333

34-
public PhysicalModuleContext(@NotNull Context parent, @NotNull ModulePath modulePath) {
34+
public PhysicalModuleContext(@NotNull Context parent, @NotNull QPath qualifiedPath) {
3535
this.parent = parent;
36-
this.modulePath = modulePath;
36+
this.qualifiedPath = qualifiedPath;
37+
this.exports = new ModuleExport(qualifiedPath);
3738
}
3839

3940
@Override public boolean importModule(

base/src/main/java/org/aya/resolve/visitor/StmtPreResolver.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ImmutableSeq<ResolvingStmt> resolveStmt(@NotNull ImmutableSeq<Stmt> stmts
9090
context.importModuleContext(importedName, mod, cmd.accessibility(), cmd.sourcePos(), thisReporter);
9191
resolveInfo.primFactory().importFrom(success.primFactory());
9292

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

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

116114
// renaming as infix
117115
if (useHide.strategy() == UseHide.Strategy.Using) useHide.list().forEach(use -> {

cli-impl/src/main/java/org/aya/cli/interactive/ReplCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private void loadFile(@NotNull Path file) {
182182
private @NotNull ResolveInfo makeResolveInfo(@NotNull ModuleContext ctx) {
183183
var resolveInfo = new ResolveInfo(ctx, tcState.primFactory, tcState.shapeFactory, opSet);
184184
imports.forEach(ii -> resolveInfo.imports().put(
185-
ii.modulePath().asName(), new ResolveInfo.ImportInfo(ii, false)));
185+
ii.modulePath().asName(), new ResolveInfo.ImportInfo(ii, false, null)));
186186
return resolveInfo;
187187
}
188188

cli-impl/src/main/java/org/aya/cli/interactive/ReplContext.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import org.aya.syntax.concrete.stmt.Stmt;
1717
import org.aya.syntax.context.ModuleExport;
1818
import org.aya.syntax.context.ModuleSymbol;
19-
import org.aya.syntax.ref.AnyDefVar;
20-
import org.aya.syntax.ref.AnyVar;
21-
import org.aya.syntax.ref.DefVar;
22-
import org.aya.syntax.ref.ModulePath;
19+
import org.aya.syntax.ref.*;
2320
import org.aya.util.RepoLike;
2421
import org.aya.util.position.SourcePos;
2522
import org.aya.util.reporter.Reporter;
@@ -34,7 +31,8 @@ public final class ReplContext extends PhysicalModuleContext implements RepoLike
3431
private @Nullable ImmutableMap<String, ModuleTrie> moduleTree = null;
3532

3633
public ReplContext(@NotNull Context parent, @NotNull ModulePath name) {
37-
super(parent, name);
34+
// QPath is used for serialization only
35+
super(parent, QPath.fileLevel(name));
3836
}
3937

4038
@Override public boolean importSymbol(

0 commit comments

Comments
 (0)