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.resolve.scopes.receivers.ReceiverValue;
024
025 import java.util.Collection;
026 import java.util.List;
027
028 import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
029
030 public class ResolutionCandidate<D extends CallableDescriptor> {
031 private final D candidateDescriptor;
032 private ReceiverValue thisObject; // receiver object of a method
033 private ReceiverValue receiverArgument; // receiver of an extension function
034 private ExplicitReceiverKind explicitReceiverKind;
035 private Boolean isSafeCall;
036
037 private ResolutionCandidate(@NotNull D descriptor, @NotNull ReceiverValue thisObject, @NotNull ReceiverValue receiverArgument,
038 @NotNull ExplicitReceiverKind explicitReceiverKind, @Nullable Boolean isSafeCall) {
039 this.candidateDescriptor = descriptor;
040 this.thisObject = thisObject;
041 this.receiverArgument = receiverArgument;
042 this.explicitReceiverKind = explicitReceiverKind;
043 this.isSafeCall = isSafeCall;
044 }
045
046 /*package*/ static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull D descriptor) {
047 return new ResolutionCandidate<D>(descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
048 }
049
050 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull D descriptor, boolean isSafeCall) {
051 return create(descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, isSafeCall);
052 }
053
054 public static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull D descriptor, @NotNull ReceiverValue thisObject,
055 @NotNull ReceiverValue receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind, boolean isSafeCall) {
056 return new ResolutionCandidate<D>(descriptor, thisObject, receiverArgument, explicitReceiverKind, isSafeCall);
057 }
058
059 public void setThisObject(@NotNull ReceiverValue thisObject) {
060 this.thisObject = thisObject;
061 }
062
063 public void setReceiverArgument(@NotNull ReceiverValue receiverArgument) {
064 this.receiverArgument = receiverArgument;
065 }
066
067 public void setExplicitReceiverKind(@NotNull ExplicitReceiverKind explicitReceiverKind) {
068 this.explicitReceiverKind = explicitReceiverKind;
069 }
070
071 @NotNull
072 public D getDescriptor() {
073 return candidateDescriptor;
074 }
075
076 @NotNull
077 public ReceiverValue getThisObject() {
078 return thisObject;
079 }
080
081 @NotNull
082 public ReceiverValue getReceiverArgument() {
083 return receiverArgument;
084 }
085
086 @NotNull
087 public ExplicitReceiverKind getExplicitReceiverKind() {
088 return explicitReceiverKind;
089 }
090
091 @NotNull
092 public static <D extends CallableDescriptor> List<ResolutionCandidate<D>> convertCollection(@NotNull Collection<? extends D> descriptors, boolean isSafeCall) {
093 List<ResolutionCandidate<D>> result = Lists.newArrayList();
094 for (D descriptor : descriptors) {
095 result.add(create(descriptor, isSafeCall));
096 }
097 return result;
098 }
099
100 public void setSafeCall(boolean safeCall) {
101 assert isSafeCall == null;
102 isSafeCall = safeCall;
103 }
104
105 public boolean isSafeCall() {
106 assert isSafeCall != null;
107 return isSafeCall;
108 }
109
110 @Override
111 public String toString() {
112 return candidateDescriptor.toString();
113 }
114 }