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 com.google.common.collect.Lists;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.descriptors.CallableDescriptor;
022    import org.jetbrains.kotlin.psi.Call;
023    import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
024    
025    import java.util.Collection;
026    import java.util.List;
027    
028    import static org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
029    
030    public class ResolutionCandidate<D extends CallableDescriptor> {
031        private final Call call;
032        private final D candidateDescriptor;
033        private ReceiverValue dispatchReceiver; // receiver object of a method
034        private ReceiverValue extensionReceiver; // 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 ReceiverValue extensionReceiver, @NotNull ExplicitReceiverKind explicitReceiverKind
040        ) {
041            this.call = call;
042            this.candidateDescriptor = descriptor;
043            this.dispatchReceiver = dispatchReceiver;
044            this.extensionReceiver = extensionReceiver;
045            this.explicitReceiverKind = explicitReceiverKind;
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, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER);
052        }
053    
054        public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
055                @NotNull Call call, @NotNull D descriptor, @NotNull ReceiverValue dispatchReceiver,
056                @NotNull ReceiverValue receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind
057        ) {
058            return new ResolutionCandidate<D>(call, descriptor, dispatchReceiver, receiverArgument, explicitReceiverKind);
059        }
060    
061        public void setDispatchReceiver(@NotNull ReceiverValue dispatchReceiver) {
062            this.dispatchReceiver = dispatchReceiver;
063        }
064    
065        public void setExtensionReceiver(@NotNull ReceiverValue extensionReceiver) {
066            this.extensionReceiver = extensionReceiver;
067        }
068    
069        public void setExplicitReceiverKind(@NotNull ExplicitReceiverKind explicitReceiverKind) {
070            this.explicitReceiverKind = explicitReceiverKind;
071        }
072    
073        @NotNull
074        public Call getCall() {
075            return call;
076        }
077    
078        @NotNull
079        public D getDescriptor() {
080            return candidateDescriptor;
081        }
082    
083        @NotNull
084        public ReceiverValue getDispatchReceiver() {
085            return dispatchReceiver;
086        }
087    
088        @NotNull
089        public ReceiverValue getExtensionReceiver() {
090            return extensionReceiver;
091        }
092    
093        @NotNull
094        public ExplicitReceiverKind getExplicitReceiverKind() {
095            return explicitReceiverKind;
096        }
097    
098        @NotNull
099        public static <D extends CallableDescriptor> List<ResolutionCandidate<D>> convertCollection(
100                @NotNull Call call, @NotNull Collection<? extends D> descriptors
101        ) {
102            List<ResolutionCandidate<D>> result = Lists.newArrayList();
103            for (D descriptor : descriptors) {
104                result.add(create(call, descriptor));
105            }
106            return result;
107        }
108    
109        @Override
110        public String toString() {
111            return candidateDescriptor.toString();
112        }
113    }