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 org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
021    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
022    import org.jetbrains.jet.lang.psi.Call;
023    import org.jetbrains.jet.lang.psi.JetReferenceExpression;
024    import org.jetbrains.jet.lang.resolve.BindingTrace;
025    import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
026    import org.jetbrains.jet.lang.resolve.calls.model.VariableAsFunctionResolvedCall;
027    import org.jetbrains.jet.lang.types.ErrorUtils;
028    
029    import java.util.Collection;
030    
031    import static org.jetbrains.jet.lang.diagnostics.Errors.UNRESOLVED_REFERENCE;
032    import static org.jetbrains.jet.lang.diagnostics.Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER;
033    import static org.jetbrains.jet.lang.resolve.BindingContext.*;
034    
035    public class TracingStrategyImpl extends AbstractTracingStrategy {
036        private final JetReferenceExpression reference;
037    
038        private TracingStrategyImpl(@NotNull JetReferenceExpression reference, @NotNull Call call) {
039            super(reference, call);
040            this.reference = reference;
041        }
042    
043        @NotNull
044        public static TracingStrategy create(@NotNull JetReferenceExpression reference, @NotNull Call call) {
045            return new TracingStrategyImpl(reference, call);
046        }
047    
048        @Override
049        public <D extends CallableDescriptor> void bindReference(@NotNull BindingTrace trace, @NotNull ResolvedCall<D> resolvedCall) {
050            CallableDescriptor descriptor = resolvedCall.getCandidateDescriptor();
051            if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
052                descriptor = ((VariableAsFunctionResolvedCall) resolvedCall).getVariableCall().getCandidateDescriptor();
053            }
054            DeclarationDescriptor storedReference = trace.get(REFERENCE_TARGET, reference);
055            if (storedReference == null || !ErrorUtils.isError(descriptor)) {
056                trace.record(REFERENCE_TARGET, reference, descriptor);
057            }
058        }
059    
060        @Override
061        public <D extends CallableDescriptor> void bindResolvedCall(@NotNull BindingTrace trace, @NotNull ResolvedCall<D> resolvedCall) {
062            trace.record(RESOLVED_CALL, call.getCalleeExpression(), resolvedCall);
063            trace.record(CALL, call.getCalleeExpression(), call);
064        }
065    
066        @Override
067        public void unresolvedReference(@NotNull BindingTrace trace) {
068            trace.report(UNRESOLVED_REFERENCE.on(reference, reference));
069        }
070    
071        @Override
072        public <D extends CallableDescriptor> void unresolvedReferenceWrongReceiver(@NotNull BindingTrace trace, @NotNull Collection<? extends ResolvedCall<D>> candidates) {
073            trace.report(UNRESOLVED_REFERENCE_WRONG_RECEIVER.on(reference, candidates));
074        }
075    }