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;
018
019 import com.google.common.base.Function;
020 import com.google.common.collect.Collections2;
021 import com.google.common.collect.Lists;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
024 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
025 import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
026 import org.jetbrains.jet.lang.psi.*;
027 import org.jetbrains.jet.lang.resolve.ChainedTemporaryBindingTrace;
028 import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace;
029 import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
030 import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
031 import org.jetbrains.jet.lang.resolve.calls.context.CallCandidateResolutionContext;
032 import org.jetbrains.jet.lang.resolve.calls.context.ContextDependency;
033 import org.jetbrains.jet.lang.resolve.calls.model.MutableResolvedCall;
034 import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCallImpl;
035 import org.jetbrains.jet.lang.resolve.calls.model.VariableAsFunctionResolvedCallImpl;
036 import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResults;
037 import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResultsImpl;
038 import org.jetbrains.jet.lang.resolve.calls.tasks.ExplicitReceiverKind;
039 import org.jetbrains.jet.lang.resolve.calls.tasks.ResolutionCandidate;
040 import org.jetbrains.jet.lang.resolve.calls.tasks.ResolutionTask;
041 import org.jetbrains.jet.lang.resolve.calls.tasks.TracingStrategyForInvoke;
042 import org.jetbrains.jet.lang.resolve.calls.util.DelegatingCall;
043 import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver;
044 import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue;
045 import org.jetbrains.jet.lang.types.JetType;
046
047 import java.util.Collection;
048 import java.util.Collections;
049 import java.util.List;
050
051 /**
052 * CallTransformer treats specially 'variable as function' call case, other cases keeps unchanged (base realization).
053 *
054 * For the call 'b.foo(1)' where foo is a variable that has method 'invoke' (for example of function type)
055 * CallTransformer creates two contexts, two calls in each, and performs second ('invoke') call resolution:
056 *
057 * context#1. calls: 'b.foo' 'invoke(1)'
058 * context#2. calls: 'foo' 'b.invoke(1)'
059 *
060 * If success VariableAsFunctionResolvedCall is created.
061 */
062 public class CallTransformer<D extends CallableDescriptor, F extends D> {
063 private CallTransformer() {}
064
065 /**
066 * Returns two contexts for 'variable as function' case (in FUNCTION_CALL_TRANSFORMER), one context otherwise
067 */
068 @NotNull
069 public Collection<CallCandidateResolutionContext<D>> createCallContexts(@NotNull ResolutionCandidate<D> candidate,
070 @NotNull ResolutionTask<D, F> task,
071 @NotNull TemporaryBindingTrace candidateTrace
072 ) {
073 ResolvedCallImpl<D> candidateCall = ResolvedCallImpl.create(candidate, candidateTrace, task.tracing, task.dataFlowInfoForArguments);
074 return Collections.singleton(CallCandidateResolutionContext.create(candidateCall, task, candidateTrace, task.tracing));
075 }
076
077 /**
078 * Returns collection of resolved calls for 'invoke' for 'variable as function' case (in FUNCTION_CALL_TRANSFORMER),
079 * the resolved call from callCandidateResolutionContext otherwise
080 */
081 @NotNull
082 public Collection<MutableResolvedCall<F>> transformCall(@NotNull CallCandidateResolutionContext<D> callCandidateResolutionContext,
083 @NotNull CallResolver callResolver,
084 @NotNull ResolutionTask<D, F> task
085 ) {
086 return Collections.singleton((MutableResolvedCall<F>) callCandidateResolutionContext.candidateCall);
087 }
088
089
090 public static CallTransformer<VariableDescriptor, VariableDescriptor> PROPERTY_CALL_TRANSFORMER = new CallTransformer<VariableDescriptor, VariableDescriptor>();
091
092 public static CallTransformer<CallableDescriptor, FunctionDescriptor> FUNCTION_CALL_TRANSFORMER = new CallTransformer<CallableDescriptor, FunctionDescriptor>() {
093 @NotNull
094 @Override
095 public Collection<CallCandidateResolutionContext<CallableDescriptor>> createCallContexts(@NotNull ResolutionCandidate<CallableDescriptor> candidate,
096 @NotNull ResolutionTask<CallableDescriptor, FunctionDescriptor> task, @NotNull TemporaryBindingTrace candidateTrace) {
097
098 if (candidate.getDescriptor() instanceof FunctionDescriptor) {
099 return super.createCallContexts(candidate, task, candidateTrace);
100 }
101
102 assert candidate.getDescriptor() instanceof VariableDescriptor;
103
104 boolean hasReceiver = candidate.getReceiverArgument().exists();
105 Call variableCall = stripCallArguments(task);
106 if (!hasReceiver) {
107 CallCandidateResolutionContext<CallableDescriptor> context = CallCandidateResolutionContext.create(
108 ResolvedCallImpl.create(candidate, candidateTrace, task.tracing, task.dataFlowInfoForArguments), task, candidateTrace, task.tracing, variableCall);
109 return Collections.singleton(context);
110 }
111 CallCandidateResolutionContext<CallableDescriptor> contextWithReceiver = createContextWithChainedTrace(
112 candidate, variableCall, candidateTrace, task, ReceiverValue.NO_RECEIVER);
113
114 Call variableCallWithoutReceiver = stripReceiver(variableCall);
115 ResolutionCandidate<CallableDescriptor> candidateWithoutReceiver = ResolutionCandidate.create(
116 candidate.getCall(), candidate.getDescriptor(), candidate.getThisObject(), ReceiverValue.NO_RECEIVER,
117 ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, false);
118
119 CallCandidateResolutionContext<CallableDescriptor> contextWithoutReceiver = createContextWithChainedTrace(
120 candidateWithoutReceiver, variableCallWithoutReceiver, candidateTrace, task, variableCall.getExplicitReceiver());
121
122 return Lists.newArrayList(contextWithReceiver, contextWithoutReceiver);
123 }
124
125 private CallCandidateResolutionContext<CallableDescriptor> createContextWithChainedTrace(
126 @NotNull ResolutionCandidate<CallableDescriptor> candidate, @NotNull Call call, @NotNull TemporaryBindingTrace temporaryTrace,
127 @NotNull ResolutionTask<CallableDescriptor, FunctionDescriptor> task, @NotNull ReceiverValue receiverValue
128 ) {
129 ChainedTemporaryBindingTrace chainedTrace = ChainedTemporaryBindingTrace.create(temporaryTrace, "chained trace to resolve candidate", candidate);
130 ResolvedCallImpl<CallableDescriptor> resolvedCall = ResolvedCallImpl.create(candidate, chainedTrace, task.tracing, task.dataFlowInfoForArguments);
131 return CallCandidateResolutionContext.create(resolvedCall, task, chainedTrace, task.tracing, call, receiverValue);
132 }
133
134 private Call stripCallArguments(@NotNull ResolutionTask<CallableDescriptor, FunctionDescriptor> task) {
135 return new DelegatingCall(task.call) {
136 @Override
137 public JetValueArgumentList getValueArgumentList() {
138 return null;
139 }
140
141 @NotNull
142 @Override
143 public List<? extends ValueArgument> getValueArguments() {
144 return Collections.emptyList();
145 }
146
147 @NotNull
148 @Override
149 public List<JetExpression> getFunctionLiteralArguments() {
150 return Collections.emptyList();
151 }
152
153 @NotNull
154 @Override
155 public List<JetTypeProjection> getTypeArguments() {
156 return Collections.emptyList();
157 }
158
159 @Override
160 public JetTypeArgumentList getTypeArgumentList() {
161 return null;
162 }
163 };
164 }
165
166 private Call stripReceiver(@NotNull Call variableCall) {
167 return new DelegatingCall(variableCall) {
168 @NotNull
169 @Override
170 public ReceiverValue getExplicitReceiver() {
171 return ReceiverValue.NO_RECEIVER;
172 }
173 };
174 }
175
176 @NotNull
177 @Override
178 public Collection<MutableResolvedCall<FunctionDescriptor>> transformCall(
179 @NotNull CallCandidateResolutionContext<CallableDescriptor> context,
180 @NotNull CallResolver callResolver,
181 @NotNull ResolutionTask<CallableDescriptor, FunctionDescriptor> task
182 ) {
183 CallableDescriptor descriptor = context.candidateCall.getCandidateDescriptor();
184 if (descriptor instanceof FunctionDescriptor) {
185 return super.transformCall(context, callResolver, task);
186 }
187
188 assert descriptor instanceof VariableDescriptor;
189 JetType returnType = descriptor.getReturnType();
190 if (returnType == null) {
191 return Collections.emptyList();
192 }
193
194 final MutableResolvedCall<VariableDescriptor> variableResolvedCall = (MutableResolvedCall)context.candidateCall;
195
196 JetExpression calleeExpression = task.call.getCalleeExpression();
197 if (calleeExpression == null) return Collections.emptyList();
198
199 ExpressionReceiver variableReceiver = new ExpressionReceiver(calleeExpression, variableResolvedCall.getResultingDescriptor().getType());
200 Call functionCall = new CallForImplicitInvoke(context.explicitExtensionReceiverForInvoke, variableReceiver, task.call);
201
202 DelegatingBindingTrace variableCallTrace = context.candidateCall.getTrace();
203 BasicCallResolutionContext basicCallResolutionContext = BasicCallResolutionContext.create(
204 context.replaceBindingTrace(variableCallTrace).replaceContextDependency(ContextDependency.DEPENDENT),
205 functionCall, context.checkArguments, context.dataFlowInfoForArguments);
206
207 // 'invoke' call resolve
208 TracingStrategyForInvoke tracingForInvoke = new TracingStrategyForInvoke(
209 calleeExpression, functionCall, variableReceiver.getType());
210 OverloadResolutionResults<FunctionDescriptor> results = callResolver.resolveCallForInvoke(
211 basicCallResolutionContext, tracingForInvoke);
212 Collection<MutableResolvedCall<FunctionDescriptor>> calls = ((OverloadResolutionResultsImpl<FunctionDescriptor>)results).getResultingCalls();
213
214 return Collections2.transform(calls, new Function<MutableResolvedCall<FunctionDescriptor>, MutableResolvedCall<FunctionDescriptor>>() {
215 @Override
216 public MutableResolvedCall<FunctionDescriptor> apply(MutableResolvedCall<FunctionDescriptor> functionResolvedCall) {
217 return new VariableAsFunctionResolvedCallImpl(functionResolvedCall, variableResolvedCall);
218 }
219 });
220 }
221 };
222
223 public static class CallForImplicitInvoke extends DelegatingCall {
224 final Call outerCall;
225 final ReceiverValue explicitExtensionReceiver;
226 final ExpressionReceiver calleeExpressionAsThisObject;
227 final JetSimpleNameExpression fakeInvokeExpression;
228
229 public CallForImplicitInvoke(
230 @NotNull ReceiverValue explicitExtensionReceiver,
231 @NotNull ExpressionReceiver calleeExpressionAsThisObject,
232 @NotNull Call call
233 ) {
234 super(call);
235 this.outerCall = call;
236 this.explicitExtensionReceiver = explicitExtensionReceiver;
237 this.calleeExpressionAsThisObject = calleeExpressionAsThisObject;
238 this.fakeInvokeExpression = (JetSimpleNameExpression) JetPsiFactory.createExpression(call.getCallElement().getProject(), "invoke");
239 }
240
241 @NotNull
242 @Override
243 public ReceiverValue getExplicitReceiver() {
244 return explicitExtensionReceiver;
245 }
246
247 @NotNull
248 @Override
249 public ReceiverValue getThisObject() {
250 return calleeExpressionAsThisObject;
251 }
252
253 @Override
254 public JetExpression getCalleeExpression() {
255 return fakeInvokeExpression;
256 }
257
258 @NotNull
259 @Override
260 public JetElement getCallElement() {
261 return outerCall.getCallElement();
262 }
263
264 @NotNull
265 @Override
266 public CallType getCallType() {
267 return CallType.INVOKE;
268 }
269
270 @NotNull
271 public Call getOuterCall() {
272 return outerCall;
273 }
274 }
275 }