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