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