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

Class resolver #1134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions base/src/main/java/org/aya/tyck/ClassResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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.tyck;

import kala.collection.MapLike;
import kala.collection.immutable.ImmutableMap;
import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableList;
import kala.collection.mutable.MutableMap;
import org.aya.resolve.context.Candidate;
import org.aya.resolve.context.ModuleContext;
import org.aya.syntax.core.def.AnyDef;
import org.aya.syntax.core.def.ClassDefLike;
import org.aya.syntax.core.def.MemberDefLike;
import org.aya.syntax.core.term.call.ClassCall;
import org.aya.syntax.ref.AnyDefVar;
import org.aya.syntax.ref.AnyVar;
import org.aya.syntax.ref.LocalCtx;
import org.aya.syntax.ref.LocalVar;
import org.aya.tyck.tycker.Stateful;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class ClassResolver {
/**
* The module context we are in, it is considered immutable during the lifetime of this resolver.
*/
public final @NotNull ModuleContext env;
public @Nullable ImmutableMap<ClassDefLike, ImmutableSeq<AnyDefVar>> envCache;

public ClassResolver(@NotNull ModuleContext env) {
this.env = env;
}

public @NotNull ImmutableMap<ClassDefLike, ImmutableSeq<AnyDefVar>> getEnvClassInstance() {
if (envCache != null) return envCache;
var result = env.symbols().table()
.valuesView()
.flatMap(Candidate::getAll)
.filterIsInstance(AnyDefVar.class);

var builder = MutableMap.<ClassDefLike, ImmutableSeq<AnyDefVar>>create();
throw new UnsupportedOperationException("TODO");

// TODO: we cannot use the instance that is defined in the same file,
// cause they are tycked in the same cycle, which might:
// * Add unexpected dependencies
// * May not have core

// return envCache;
}

public void resolve(
@NotNull MemberDefLike field,
@NotNull ImmutableSeq<Jdg> args,
@NotNull LocalCtx ctx,
@NotNull Stateful normalizerProvider
) {
var candies = findCandidates(field, ctx, normalizerProvider);
var matches = MutableList.<AnyVar>create();


}

/**
* Find the candidate for the invocation of {@code field args}
*
* @return the candidates, either {@link AnyDefVar} or {@link LocalVar}
*/
public @NotNull ImmutableSeq<AnyVar> findCandidates(
@NotNull MemberDefLike field,
@NotNull LocalCtx ctx,
@NotNull Stateful normalizerProvider
) {
var candies = MutableList.<AnyVar>create();
// find from localCtx
// can be improved by forEach and extractLocal (if there is)
ctx.extract().forEach(v -> {
if (normalizerProvider.whnf(ctx.get(v)) instanceof ClassCall call && field.classRef().equals(call.ref())) {
candies.append(AnyDef.toVar(call.ref()));
}
});

// find from environment
getEnvClassInstance().getOption(field.classRef())
.forEach(candies::appendAll);

return candies.toImmutableSeq();
}
}
Loading