001 /*
002 * Copyright 2010-2015 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 import static org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
027
028 public class ResolutionCandidate<D extends CallableDescriptor> {
029 private final Call call;
030 private final D candidateDescriptor;
031 private final TypeSubstitutor knownTypeParametersResultingSubstitutor;
032 private ReceiverValue dispatchReceiver; // receiver object of a method
033 private ReceiverValue extensionReceiver; // receiver of an extension function
034 private ExplicitReceiverKind explicitReceiverKind;
035
036 private ResolutionCandidate(
037 @NotNull Call call, @NotNull D descriptor, @NotNull ReceiverValue dispatchReceiver,
038 @NotNull ReceiverValue extensionReceiver, @NotNull ExplicitReceiverKind explicitReceiverKind,
039 @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
040 ) {
041 this.call = call;
042 this.candidateDescriptor = descriptor;
043 this.dispatchReceiver = dispatchReceiver;
044 this.extensionReceiver = extensionReceiver;
045 this.explicitReceiverKind = explicitReceiverKind;
046 this.knownTypeParametersResultingSubstitutor = knownTypeParametersResultingSubstitutor;
047 }
048
049 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
050 @NotNull Call call, @NotNull D descriptor
051 ) {
052 return new ResolutionCandidate<D>(call, descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
053 }
054
055 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
056 @NotNull Call call, @NotNull D descriptor, @NotNull ReceiverValue dispatchReceiver,
057 @NotNull ReceiverValue receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind,
058 @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
059 ) {
060 return new ResolutionCandidate<D>(call, descriptor, dispatchReceiver, receiverArgument, explicitReceiverKind,
061 knownTypeParametersResultingSubstitutor);
062 }
063
064 public void setDispatchReceiver(@NotNull ReceiverValue dispatchReceiver) {
065 this.dispatchReceiver = dispatchReceiver;
066 }
067
068 public void setExtensionReceiver(@NotNull ReceiverValue extensionReceiver) {
069 this.extensionReceiver = extensionReceiver;
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 @NotNull
087 public ReceiverValue getDispatchReceiver() {
088 return dispatchReceiver;
089 }
090
091 @NotNull
092 public ReceiverValue getExtensionReceiver() {
093 return extensionReceiver;
094 }
095
096 @NotNull
097 public ExplicitReceiverKind getExplicitReceiverKind() {
098 return explicitReceiverKind;
099 }
100
101 @Nullable
102 public TypeSubstitutor getKnownTypeParametersResultingSubstitutor() {
103 return knownTypeParametersResultingSubstitutor;
104 }
105
106 @Override
107 public String toString() {
108 return candidateDescriptor.toString();
109 }
110 }