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