-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve: implement VariableDependencyCollector
- Loading branch information
Showing
3 changed files
with
104 additions
and
27 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
base/src/main/java/org/aya/resolve/error/CyclicDependencyError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang. | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file. | ||
package org.aya.resolve.error; | ||
|
||
import org.aya.pretty.doc.Doc; | ||
import org.aya.util.prettier.PrettierOptions; | ||
import org.aya.util.reporter.Problem; | ||
import org.aya.syntax.ref.GeneralizedVar; | ||
import org.aya.util.error.SourcePos; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public record CyclicDependencyError( | ||
@NotNull SourcePos sourcePos, | ||
@NotNull GeneralizedVar var | ||
) implements Problem { | ||
@Override public @NotNull Severity level() { return Severity.ERROR; } | ||
@Override public @NotNull Stage stage() { return Stage.RESOLVE; } | ||
@Override public @NotNull Doc describe(@NotNull PrettierOptions options) { | ||
return Doc.sep( | ||
Doc.plain("Cyclic dependency detected in variable declaration:"), | ||
Doc.plain(var.name()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
base/src/main/java/org/aya/resolve/visitor/VariableDependencyCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang. | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file. | ||
package org.aya.resolve.visitor; | ||
|
||
import kala.collection.immutable.ImmutableSeq; | ||
import kala.collection.mutable.MutableList; | ||
import kala.collection.mutable.MutableSet; | ||
import org.aya.util.error.PosedUnaryOperator; | ||
import org.aya.resolve.context.Context; | ||
import org.aya.resolve.error.CyclicDependencyError; | ||
import org.aya.syntax.concrete.Expr; | ||
import org.aya.syntax.ref.GeneralizedVar; | ||
import org.aya.util.error.SourcePos; | ||
import org.aya.util.reporter.Reporter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class VariableDependencyCollector { | ||
private final Map<GeneralizedVar, ImmutableSeq<GeneralizedVar>> dependencies = new HashMap<>(); | ||
private final Reporter reporter; | ||
private final MutableSet<GeneralizedVar> visiting = MutableSet.create(); | ||
|
||
public VariableDependencyCollector(Reporter reporter) { | ||
this.reporter = reporter; | ||
} | ||
|
||
public void registerVariable(GeneralizedVar var) { | ||
if (dependencies.containsKey(var)) return; | ||
|
||
// If var is already being visited, we found a cycle. | ||
if (!visiting.add(var)) { | ||
reporter.report(new CyclicDependencyError(var.sourcePos(), var)); | ||
throw new Context.ResolvingInterruptedException(); | ||
} | ||
|
||
var deps = collectReferences(var); | ||
dependencies.put(var, deps); | ||
visiting.remove(var); | ||
|
||
// Recursively register dependencies | ||
for (var dep : deps) registerVariable(dep); | ||
} | ||
|
||
public ImmutableSeq<GeneralizedVar> getDependencies(GeneralizedVar var) { | ||
return dependencies.getOrDefault(var, ImmutableSeq.empty()); | ||
} | ||
|
||
private ImmutableSeq<GeneralizedVar> collectReferences(GeneralizedVar var) { | ||
var type = var.owner.type; | ||
var collector = new StaticGeneralizedVarCollector(); | ||
type.descent(collector); | ||
return collector.getCollected(); | ||
} | ||
|
||
private static class StaticGeneralizedVarCollector 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(); | ||
} | ||
} | ||
} |