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

resolve: add introduceDependencies for variables #1226

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
38 changes: 38 additions & 0 deletions base/src/main/java/org/aya/resolve/visitor/ExprResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public ExprResolver(@NotNull Context ctx, boolean allowGeneralizing) {
// Ordered set semantics. Do not expect too many generalized vars.
var owner = generalized.owner;
assert owner != null : "Sanity check";
introduceDependencies(generalized);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need it here? It seems you called it twice

var param = owner.toExpr(false, generalized.toLocal());
allowedGeneralizes.put(generalized, param);
addReference(owner);
Expand Down Expand Up @@ -255,9 +256,46 @@ private void addReference(@NotNull DefVar<?, ?> defVar) {
});
}

private void introduceDependencies(@NotNull GeneralizedVar var) {
if (allowedGeneralizes.containsKey(var)) return;

var dependencies = getDependencies(var);
for (var dep : dependencies) {
introduceDependencies(dep);
}

var owner = var.owner;
assert owner != null : "GeneralizedVar owner should not be null";
var param = owner.toExpr(false, var.toLocal());
allowedGeneralizes.put(var, param);
addReference(owner);
}

private @NotNull ImmutableSeq<GeneralizedVar> getDependencies(@NotNull GeneralizedVar var) {
var collector = new GeneralizedVarCollector();
var.owner.type.descent(collector);
return collector.getCollected();
}

private static class GeneralizedVarCollector implements PosedUnaryOperator<Expr> {
private final MutableList<GeneralizedVar> collected = MutableList.create();

@Override
public @NotNull Expr apply(@NotNull SourcePos pos, @NotNull Expr expr) {
if (expr instanceof Expr.Ref ref && ref.var() instanceof GeneralizedVar gvar) {
collected.append(gvar);
}
return expr.descent(this);
}

public ImmutableSeq<GeneralizedVar> getCollected() {
return collected.toImmutableSeq();
}
}
public @NotNull AnyVar resolve(@NotNull QualifiedID name) {
var result = ctx.get(name);
if (result instanceof GeneralizedVar gvar) {
introduceDependencies(gvar);
var gened = allowedGeneralizes.getOrNull(gvar);
if (gened != null) return gened.ref();
}
Expand Down
Loading