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