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.intellij.psi.PsiElement;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.builtins.FunctionTypesKt;
022    import org.jetbrains.kotlin.descriptors.CallableDescriptor;
023    import org.jetbrains.kotlin.psi.Call;
024    import org.jetbrains.kotlin.psi.KtExpression;
025    import org.jetbrains.kotlin.psi.KtReferenceExpression;
026    import org.jetbrains.kotlin.psi.KtSimpleNameExpression;
027    import org.jetbrains.kotlin.resolve.BindingContext;
028    import org.jetbrains.kotlin.resolve.BindingTrace;
029    import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
030    import org.jetbrains.kotlin.types.KotlinType;
031    
032    import java.util.Collection;
033    
034    import static org.jetbrains.kotlin.diagnostics.Errors.FUNCTION_EXPECTED;
035    import static org.jetbrains.kotlin.diagnostics.Errors.NO_RECEIVER_ALLOWED;
036    import static org.jetbrains.kotlin.resolve.BindingContext.CALL;
037    import static org.jetbrains.kotlin.resolve.BindingContext.RESOLVED_CALL;
038    
039    public class TracingStrategyForInvoke extends AbstractTracingStrategy {
040        private final KotlinType calleeType;
041    
042        public TracingStrategyForInvoke(
043                @NotNull KtExpression reference,
044                @NotNull Call call,
045                @NotNull KotlinType calleeType
046        ) {
047            super(reference, call);
048            this.calleeType = calleeType;
049        }
050    
051        @Override
052        public void bindCall(@NotNull BindingTrace trace, @NotNull Call call) {
053            // If reference is a simple name, it's 'variable as function call' case ('foo(a, b)' where 'foo' is a variable).
054            // The outer call is bound ('foo(a, b)'), while 'invoke' call for this case is 'foo.invoke(a, b)' and shouldn't be bound.
055            if (reference instanceof KtSimpleNameExpression) return;
056            trace.record(CALL, reference, call);
057        }
058    
059        @Override
060        public <D extends CallableDescriptor> void bindReference(
061                @NotNull BindingTrace trace, @NotNull ResolvedCall<D> resolvedCall
062        ) {
063            PsiElement callElement = call.getCallElement();
064            if (callElement instanceof KtReferenceExpression) {
065                trace.record(BindingContext.REFERENCE_TARGET, (KtReferenceExpression) callElement, resolvedCall.getCandidateDescriptor());
066            }
067        }
068    
069        @Override
070        public <D extends CallableDescriptor> void bindResolvedCall(
071                @NotNull BindingTrace trace, @NotNull ResolvedCall<D> resolvedCall
072        ) {
073            if (reference instanceof KtSimpleNameExpression) return;
074            trace.record(RESOLVED_CALL, call, resolvedCall);
075        }
076    
077        @Override
078        public void unresolvedReference(@NotNull BindingTrace trace) {
079            functionExpectedOrNoReceiverAllowed(trace);
080        }
081    
082        @Override
083        public <D extends CallableDescriptor> void unresolvedReferenceWrongReceiver(
084                @NotNull BindingTrace trace, @NotNull Collection<? extends ResolvedCall<D>> candidates
085        ) {
086            functionExpectedOrNoReceiverAllowed(trace);
087        }
088    
089        private void functionExpectedOrNoReceiverAllowed(BindingTrace trace) {
090            if (FunctionTypesKt.isNonExtensionFunctionType(calleeType)) {
091                trace.report(NO_RECEIVER_ALLOWED.on(reference));
092            }
093            else {
094                trace.report(FUNCTION_EXPECTED.on(reference, reference, calleeType));
095            }
096        }
097    }