All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.tomassetti.symbolsolver.model.resolution.Context Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
package me.tomassetti.symbolsolver.model.resolution;

import me.tomassetti.symbolsolver.model.declarations.MethodDeclaration;
import me.tomassetti.symbolsolver.model.declarations.TypeDeclaration;
import me.tomassetti.symbolsolver.model.declarations.ValueDeclaration;
import me.tomassetti.symbolsolver.model.invokations.MethodUsage;
import me.tomassetti.symbolsolver.model.typesystem.TypeUsage;

import java.util.List;
import java.util.Optional;

/**
 * Context is very similar to scope.
 * In the context we look for solving symbols.
 *
 * @author Federico Tomassetti
 */
public interface Context {

    Context getParent();

    /* Type resolution */

    default Optional solveGenericType(String name, TypeSolver typeSolver) {
        return Optional.empty();
    }

    default SymbolReference solveType(String name, TypeSolver typeSolver) {
        Context parent = getParent();
        if (parent == null) {
            return SymbolReference.unsolved(TypeDeclaration.class);
        } else {
            return getParent().solveType(name, typeSolver);
        }
    }

    /* Symbol resolution */

    SymbolReference solveSymbol(String name, TypeSolver typeSolver);

    default Optional solveSymbolAsValue(String name, TypeSolver typeSolver) {
        SymbolReference ref = solveSymbol(name, typeSolver);
        if (ref.isSolved()) {
            Value value = Value.from(ref.getCorrespondingDeclaration(), typeSolver);
            return Optional.of(value);
        } else {
            return Optional.empty();
        }
    }

    /* Methods resolution */

    /**
     * We find the method declaration which is the best match for the given name and list of parameters.
     */
    SymbolReference solveMethod(String name, List parameterTypes, TypeSolver typeSolver);

    /**
     * Similar to solveMethod but we return a MethodUsage. A MethodUsage corresponds to a MethodDeclaration plus the
     * resolved type variables.
     */
    default Optional solveMethodAsUsage(String name, List parameterTypes, TypeSolver typeSolver) {
        SymbolReference methodSolved = solveMethod(name, parameterTypes, typeSolver);
        if (methodSolved.isSolved()) {
            MethodDeclaration methodDeclaration = methodSolved.getCorrespondingDeclaration();
            return Optional.of(methodDeclaration.resolveTypeVariables(this, parameterTypes));
        } else {
            return Optional.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy