001 /*
002 * Copyright 2010-2013 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.jet.lang.resolve.calls.tasks;
018
019 import com.google.common.collect.Lists;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
023 import org.jetbrains.jet.lang.psi.Call;
024 import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue;
025
026 import java.util.Collection;
027 import java.util.List;
028
029 import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
030
031 public class ResolutionCandidate<D extends CallableDescriptor> {
032 private final Call call;
033 private final D candidateDescriptor;
034 private ReceiverValue dispatchReceiver; // receiver object of a method
035 private ReceiverValue extensionReceiver; // receiver of an extension function
036 private ExplicitReceiverKind explicitReceiverKind;
037 private Boolean isSafeCall;
038
039 private ResolutionCandidate(
040 @NotNull Call call, @NotNull D descriptor, @NotNull ReceiverValue dispatchReceiver,
041 @NotNull ReceiverValue extensionReceiver, @NotNull ExplicitReceiverKind explicitReceiverKind, @Nullable Boolean isSafeCall
042 ) {
043 this.call = call;
044 this.candidateDescriptor = descriptor;
045 this.dispatchReceiver = dispatchReceiver;
046 this.extensionReceiver = extensionReceiver;
047 this.explicitReceiverKind = explicitReceiverKind;
048 this.isSafeCall = isSafeCall;
049 }
050
051 /*package*/ static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull Call call, @NotNull D descriptor) {
052 return new ResolutionCandidate<D>(call, descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
053 }
054
055 /* 'null' for isSafeCall parameter if it should be set later (with 'setSafeCall') */
056 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
057 @NotNull Call call, @NotNull D descriptor, @Nullable Boolean isSafeCall
058 ) {
059 return new ResolutionCandidate<D>(call, descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, isSafeCall);
060 }
061
062 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(
063 @NotNull Call call, @NotNull D descriptor, @NotNull ReceiverValue dispatchReceiver,
064 @NotNull ReceiverValue receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind, boolean isSafeCall
065 ) {
066 return new ResolutionCandidate<D>(call, descriptor, dispatchReceiver, receiverArgument, explicitReceiverKind, isSafeCall);
067 }
068
069 public void setDispatchReceiver(@NotNull ReceiverValue dispatchReceiver) {
070 this.dispatchReceiver = dispatchReceiver;
071 }
072
073 public void setExtensionReceiver(@NotNull ReceiverValue extensionReceiver) {
074 this.extensionReceiver = extensionReceiver;
075 }
076
077 public void setExplicitReceiverKind(@NotNull ExplicitReceiverKind explicitReceiverKind) {
078 this.explicitReceiverKind = explicitReceiverKind;
079 }
080
081 @NotNull
082 public Call getCall() {
083 return call;
084 }
085
086 @NotNull
087 public D getDescriptor() {
088 return candidateDescriptor;
089 }
090
091 @NotNull
092 public ReceiverValue getDispatchReceiver() {
093 return dispatchReceiver;
094 }
095
096 @NotNull
097 public ReceiverValue getExtensionReceiver() {
098 return extensionReceiver;
099 }
100
101 @NotNull
102 public ExplicitReceiverKind getExplicitReceiverKind() {
103 return explicitReceiverKind;
104 }
105
106 @NotNull
107 public static <D extends CallableDescriptor> List<ResolutionCandidate<D>> convertCollection(
108 @NotNull Call call, @NotNull Collection<? extends D> descriptors, boolean isSafeCall
109 ) {
110 List<ResolutionCandidate<D>> result = Lists.newArrayList();
111 for (D descriptor : descriptors) {
112 result.add(create(call, descriptor, isSafeCall));
113 }
114 return result;
115 }
116
117 public void setSafeCall(boolean safeCall) {
118 assert isSafeCall == null;
119 isSafeCall = safeCall;
120 }
121
122 public boolean isSafeCall() {
123 assert isSafeCall != null;
124 return isSafeCall;
125 }
126
127 @Override
128 public String toString() {
129 return candidateDescriptor.toString();
130 }
131 }