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.Receiver;
024 import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
025 import org.jetbrains.kotlin.types.TypeSubstitutor;
026
027 public class ResolutionCandidate<D extends CallableDescriptor> {
028 private final Call call;
029 private final D candidateDescriptor;
030 private final TypeSubstitutor knownTypeParametersResultingSubstitutor;
031 private ReceiverValue dispatchReceiver; // receiver object of a method
032 private Receiver receiverArgument; // receiver of an extension function
033 private ExplicitReceiverKind explicitReceiverKind;
034
035 private ResolutionCandidate(
036 @NotNull Call call, @NotNull D descriptor, @Nullable ReceiverValue dispatchReceiver,
037 @Nullable Receiver receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind,
038 @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
039 ) {
040 this.call = call;
041 this.candidateDescriptor = descriptor;
042 this.dispatchReceiver = dispatchReceiver;
043 this.receiverArgument = receiverArgument;
044 this.explicitReceiverKind = explicitReceiverKind;
045 this.knownTypeParametersResultingSubstitutor = knownTypeParametersResultingSubstitutor;
046 }
047
048 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
049 @NotNull Call call, @NotNull D descriptor
050 ) {
051 return new ResolutionCandidate<D>(call, descriptor, null, null, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
052 }
053
054 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
055 @NotNull Call call, @NotNull D descriptor, @Nullable ReceiverValue dispatchReceiver,
056 @Nullable Receiver receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind,
057 @Nullable TypeSubstitutor knownTypeParametersResultingSubstitutor
058 ) {
059 return new ResolutionCandidate<D>(call, descriptor, dispatchReceiver, receiverArgument, explicitReceiverKind,
060 knownTypeParametersResultingSubstitutor);
061 }
062
063 public void setDispatchReceiver(@Nullable ReceiverValue dispatchReceiver) {
064 this.dispatchReceiver = dispatchReceiver;
065 }
066
067 public void setReceiverArgument(@Nullable ReceiverValue receiverArgument) {
068 this.receiverArgument = receiverArgument;
069 }
070
071 public void setExplicitReceiverKind(@NotNull ExplicitReceiverKind explicitReceiverKind) {
072 this.explicitReceiverKind = explicitReceiverKind;
073 }
074
075 @NotNull
076 public Call getCall() {
077 return call;
078 }
079
080 @NotNull
081 public D getDescriptor() {
082 return candidateDescriptor;
083 }
084
085 @Nullable
086 public ReceiverValue getDispatchReceiver() {
087 return dispatchReceiver;
088 }
089
090 @Nullable
091 public Receiver getReceiverArgument() {
092 return receiverArgument;
093 }
094
095 @NotNull
096 public ExplicitReceiverKind getExplicitReceiverKind() {
097 return explicitReceiverKind;
098 }
099
100 @Nullable
101 public TypeSubstitutor getKnownTypeParametersResultingSubstitutor() {
102 return knownTypeParametersResultingSubstitutor;
103 }
104
105 @Override
106 public String toString() {
107 return candidateDescriptor.toString();
108 }
109 }