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

org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResultsImpl Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.resolve.calls.results;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.resolve.DelegatingBindingTrace;
import org.jetbrains.kotlin.resolve.calls.model.MutableResolvedCall;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;

import java.util.Collection;
import java.util.Collections;

public class OverloadResolutionResultsImpl implements OverloadResolutionResults {

    public static  OverloadResolutionResultsImpl success(@NotNull MutableResolvedCall candidate) {
        return new OverloadResolutionResultsImpl<>(Code.SUCCESS, Collections.singleton(candidate));
    }

    public static  OverloadResolutionResultsImpl nameNotFound() {
        OverloadResolutionResultsImpl results = new OverloadResolutionResultsImpl<>(
                Code.NAME_NOT_FOUND, Collections.>emptyList());
        results.setAllCandidates(Collections.emptyList());
        return results;
    }

    public static  OverloadResolutionResultsImpl singleFailedCandidate(MutableResolvedCall candidate) {
        return new OverloadResolutionResultsImpl<>(Code.SINGLE_CANDIDATE_ARGUMENT_MISMATCH, Collections.singleton(candidate));
    }
    public static  OverloadResolutionResultsImpl manyFailedCandidates(Collection> failedCandidates) {
        return new OverloadResolutionResultsImpl<>(Code.MANY_FAILED_CANDIDATES, failedCandidates);
    }

    public static  OverloadResolutionResultsImpl candidatesWithWrongReceiver(Collection> failedCandidates) {
        return new OverloadResolutionResultsImpl<>(Code.CANDIDATES_WITH_WRONG_RECEIVER, failedCandidates);
    }

    public static  OverloadResolutionResultsImpl ambiguity(Collection> candidates) {
        return new OverloadResolutionResultsImpl<>(Code.AMBIGUITY, candidates);
    }

    public static  OverloadResolutionResultsImpl incompleteTypeInference(Collection> candidates) {
        return new OverloadResolutionResultsImpl<>(Code.INCOMPLETE_TYPE_INFERENCE, candidates);
    }

    public static  OverloadResolutionResultsImpl incompleteTypeInference(MutableResolvedCall candidate) {
        return incompleteTypeInference(Collections.singleton(candidate));
    }

    private final Collection> results;
    private final Code resultCode;
    private DelegatingBindingTrace trace;
    private Collection> allCandidates;

    private OverloadResolutionResultsImpl(@NotNull Code resultCode, @NotNull Collection> results) {
        this.results = results;
        this.resultCode = resultCode;
    }

    @Override
    @NotNull
    public Collection> getResultingCalls() {
        return results;
    }

    @Override
    @NotNull
    public MutableResolvedCall getResultingCall() {
        assert isSingleResult();
        return results.iterator().next();
    }

    @NotNull
    @Override
    public D getResultingDescriptor() {
        return getResultingCall().getResultingDescriptor();
    }

    @Override
    @NotNull
    public Code getResultCode() {
        return resultCode;
    }

    @Override
    public boolean isSuccess() {
        return resultCode.isSuccess();
    }

    @Override
    public boolean isSingleResult() {
        return results.size() == 1 && getResultCode() != Code.CANDIDATES_WITH_WRONG_RECEIVER;
    }

    @Override
    public boolean isNothing() {
        return resultCode == Code.NAME_NOT_FOUND;
    }

    @Override
    public boolean isAmbiguity() {
        return resultCode == Code.AMBIGUITY;
    }

    @Override
    public boolean isIncomplete() {
        return resultCode == Code.INCOMPLETE_TYPE_INFERENCE;
    }

    public DelegatingBindingTrace getTrace() {
        return trace;
    }

    public OverloadResolutionResultsImpl setTrace(DelegatingBindingTrace trace) {
        this.trace = trace;
        return this;
    }

    public void setAllCandidates(@Nullable Collection> allCandidates) {
        this.allCandidates = allCandidates;
    }

    @Nullable
    @Override
    public Collection> getAllCandidates() {
        return allCandidates;
    }

    @NotNull
    public OverloadResolutionResultsImpl changeStatusToSuccess() {
        if (getResultCode() == Code.SUCCESS) return this;

        assert isSingleResult() && getResultCode() == Code.INCOMPLETE_TYPE_INFERENCE :
                "Only incomplete type inference status with one candidate can be changed to success: " +
                getResultCode() + "\n" + getResultingCalls();
        OverloadResolutionResultsImpl newResults = new OverloadResolutionResultsImpl<>(Code.SUCCESS, getResultingCalls());
        newResults.setAllCandidates(getAllCandidates());
        return newResults;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy