001 /*
002 * Copyright 2010-2016 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.resolve.calls.tasks;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.descriptors.CallableDescriptor;
022 import org.jetbrains.kotlin.psi.Call;
023 import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
024 import org.jetbrains.kotlin.types.TypeSubstitutor;
025
026 public class ResolutionCandidate<D extends CallableDescriptor> {
027 private final Call call;
028 private final D candidateDescriptor;
029 private final TypeSubstitutor knownTypeParametersResultingSubstitutor;
030 private ReceiverValue dispatchReceiver; // receiver object of a method
031 private ExplicitReceiverKind explicitReceiverKind;
032
033 private ResolutionCandidate(
034 @NotNull Call call, @NotNull D descriptor, @Nullable ReceiverValue dispatchReceiver,
035 @NotNull ExplicitReceiverKind explicitReceiverKind,
036 @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
037 ) {
038 this.call = call;
039 this.candidateDescriptor = descriptor;
040 this.dispatchReceiver = dispatchReceiver;
041 this.explicitReceiverKind = explicitReceiverKind;
042 this.knownTypeParametersResultingSubstitutor = knownTypeParametersResultingSubstitutor;
043 }
044
045 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
046 @NotNull Call call, @NotNull D descriptor
047 ) {
048 return new ResolutionCandidate<D>(call, descriptor, null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
049 }
050
051 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
052 @NotNull Call call, @NotNull D descriptor, @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
053 ) {
054 return new ResolutionCandidate<D>(call, descriptor,
055 null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
056 knownTypeParametersResultingSubstitutor);
057 }
058
059 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
060 @NotNull Call call, @NotNull D descriptor, @Nullable ReceiverValue dispatchReceiver,
061 @NotNull ExplicitReceiverKind explicitReceiverKind,
062 @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
063 ) {
064 return new ResolutionCandidate<D>(call, descriptor, dispatchReceiver, explicitReceiverKind,
065 knownTypeParametersResultingSubstitutor);
066 }
067
068 public void setDispatchReceiver(@Nullable ReceiverValue dispatchReceiver) {
069 this.dispatchReceiver = dispatchReceiver;
070 }
071
072 public void setExplicitReceiverKind(@NotNull ExplicitReceiverKind explicitReceiverKind) {
073 this.explicitReceiverKind = explicitReceiverKind;
074 }
075
076 @NotNull
077 public Call getCall() {
078 return call;
079 }
080
081 @NotNull
082 public D getDescriptor() {
083 return candidateDescriptor;
084 }
085
086 @Nullable
087 public ReceiverValue getDispatchReceiver() {
088 return dispatchReceiver;
089 }
090
091 @NotNull
092 public ExplicitReceiverKind getExplicitReceiverKind() {
093 return explicitReceiverKind;
094 }
095
096 @Nullable
097 public TypeSubstitutor getKnownTypeParametersResultingSubstitutor() {
098 return knownTypeParametersResultingSubstitutor;
099 }
100
101 @Override
102 public String toString() {
103 return candidateDescriptor.toString();
104 }
105 }