All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.jetbrains.kotlin.resolve.calls.model.ResolvedCallImpl Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.calls.model;
import com.google.common.collect.Maps;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
import org.jetbrains.kotlin.psi.Call;
import org.jetbrains.kotlin.psi.ValueArgument;
import org.jetbrains.kotlin.resolve.DelegatingBindingTrace;
import org.jetbrains.kotlin.resolve.calls.callResolverUtil.CallResolverUtilKt;
import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystem;
import org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus;
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind;
import org.jetbrains.kotlin.resolve.calls.tasks.ResolutionCandidate;
import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy;
import org.jetbrains.kotlin.resolve.scopes.receivers.Receiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeProjection;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus.INCOMPLETE_TYPE_INFERENCE;
import static org.jetbrains.kotlin.resolve.calls.results.ResolutionStatus.UNKNOWN_STATUS;
public class ResolvedCallImpl implements MutableResolvedCall {
private static final Logger LOG = Logger.getInstance(ResolvedCallImpl.class);
public static final Function, CallableDescriptor> MAP_TO_CANDIDATE = new Function, CallableDescriptor>() {
@Override
public CallableDescriptor fun(MutableResolvedCall> resolvedCall) {
return resolvedCall.getCandidateDescriptor();
}
};
public static final Function, CallableDescriptor> MAP_TO_RESULT = new Function, CallableDescriptor>() {
@Override
public CallableDescriptor fun(MutableResolvedCall> resolvedCall) {
return resolvedCall.getResultingDescriptor();
}
};
@NotNull
public static ResolvedCallImpl create(
@NotNull ResolutionCandidate candidate,
@NotNull DelegatingBindingTrace trace,
@NotNull TracingStrategy tracing,
@NotNull MutableDataFlowInfoForArguments dataFlowInfoForArguments
) {
return new ResolvedCallImpl(candidate, trace, tracing, dataFlowInfoForArguments);
}
private final Call call;
private final D candidateDescriptor;
private D resultingDescriptor; // Probably substituted
private final ReceiverValue dispatchReceiver; // receiver object of a method
private final Receiver extensionReceiver; // receiver of an extension function
private final ExplicitReceiverKind explicitReceiverKind;
private final TypeSubstitutor knownTypeParametersSubstitutor;
private final Map typeArguments = Maps.newLinkedHashMap();
private final Map valueArguments = Maps.newLinkedHashMap();
private final MutableDataFlowInfoForArguments dataFlowInfoForArguments;
private final Map argumentToParameterMap = Maps.newHashMap();
private DelegatingBindingTrace trace;
private TracingStrategy tracing;
private ResolutionStatus status = UNKNOWN_STATUS;
private ConstraintSystem constraintSystem = null;
private Boolean hasInferredReturnType = null;
private boolean completed = false;
private ResolvedCallImpl(
@NotNull ResolutionCandidate candidate,
@NotNull DelegatingBindingTrace trace,
@NotNull TracingStrategy tracing,
@NotNull MutableDataFlowInfoForArguments dataFlowInfoForArguments
) {
this.call = candidate.getCall();
this.candidateDescriptor = candidate.getDescriptor();
this.dispatchReceiver = candidate.getDispatchReceiver();
this.extensionReceiver = candidate.getReceiverArgument();
this.explicitReceiverKind = candidate.getExplicitReceiverKind();
this.knownTypeParametersSubstitutor = candidate.getKnownTypeParametersResultingSubstitutor();
this.trace = trace;
this.tracing = tracing;
this.dataFlowInfoForArguments = dataFlowInfoForArguments;
}
public ResolvedCallImpl(
@NotNull Call call,
@NotNull D candidateDescriptor,
@NotNull ReceiverValue dispatchReceiver,
@NotNull ReceiverValue extensionReceiver,
@NotNull ExplicitReceiverKind explicitReceiverKind,
@Nullable TypeSubstitutor knownTypeParametersSubstitutor,
@NotNull DelegatingBindingTrace trace,
@NotNull TracingStrategy tracing,
@NotNull MutableDataFlowInfoForArguments dataFlowInfoForArguments
) {
this.call = call;
this.candidateDescriptor = candidateDescriptor;
this.dispatchReceiver = dispatchReceiver;
this.extensionReceiver = extensionReceiver;
this.explicitReceiverKind = explicitReceiverKind;
this.knownTypeParametersSubstitutor = knownTypeParametersSubstitutor;
this.trace = trace;
this.tracing = tracing;
this.dataFlowInfoForArguments = dataFlowInfoForArguments;
}
@Override
@NotNull
public ResolutionStatus getStatus() {
return status;
}
@Override
public void addStatus(@NotNull ResolutionStatus status) {
this.status = this.status.combine(status);
}
@Override
public void setStatusToSuccess() {
assert status == INCOMPLETE_TYPE_INFERENCE || status == UNKNOWN_STATUS;
status = ResolutionStatus.SUCCESS;
}
@Override
@NotNull
public DelegatingBindingTrace getTrace() {
assertNotCompleted("Trace");
return trace;
}
@Override
@NotNull
public TracingStrategy getTracingStrategy() {
assertNotCompleted("TracingStrategy");
return tracing;
}
@NotNull
@Override
public Call getCall() {
return call;
}
@Override
@NotNull
public D getCandidateDescriptor() {
return candidateDescriptor;
}
@Override
@NotNull
public D getResultingDescriptor() {
return resultingDescriptor == null ? candidateDescriptor : resultingDescriptor;
}
@Override
public void setResultingSubstitutor(@NotNull TypeSubstitutor substitutor) {
resultingDescriptor = (D) candidateDescriptor.substitute(substitutor);
assert resultingDescriptor != null : candidateDescriptor;
for (TypeParameterDescriptor typeParameter : candidateDescriptor.getTypeParameters()) {
TypeProjection typeArgumentProjection = substitutor.getSubstitution().get(typeParameter.getDefaultType());
if (typeArgumentProjection != null) {
typeArguments.put(typeParameter, typeArgumentProjection.getType());
}
}
Map substitutedParametersMap = Maps.newHashMap();
for (ValueParameterDescriptor valueParameterDescriptor : resultingDescriptor.getValueParameters()) {
substitutedParametersMap.put(valueParameterDescriptor.getOriginal(), valueParameterDescriptor);
}
Map originalValueArguments = Maps.newLinkedHashMap(valueArguments);
valueArguments.clear();
for (Map.Entry entry : originalValueArguments.entrySet()) {
ValueParameterDescriptor substitutedVersion = substitutedParametersMap.get(entry.getKey().getOriginal());
assert substitutedVersion != null : entry.getKey();
valueArguments.put(substitutedVersion, entry.getValue());
}
Map originalArgumentToParameterMap = Maps.newLinkedHashMap(argumentToParameterMap);
argumentToParameterMap.clear();
for (Map.Entry entry : originalArgumentToParameterMap.entrySet()) {
ArgumentMatchImpl argumentMatch = entry.getValue();
ValueParameterDescriptor valueParameterDescriptor = argumentMatch.getValueParameter();
ValueParameterDescriptor substitutedVersion = substitutedParametersMap.get(valueParameterDescriptor.getOriginal());
assert substitutedVersion != null : valueParameterDescriptor;
argumentToParameterMap.put(entry.getKey(), argumentMatch.replaceValueParameter(substitutedVersion));
}
}
@Override
public void setConstraintSystem(@NotNull ConstraintSystem constraintSystem) {
this.constraintSystem = constraintSystem;
}
@Nullable
@Override
public ConstraintSystem getConstraintSystem() {
assertNotCompleted("ConstraintSystem");
return constraintSystem;
}
@Override
public void recordValueArgument(@NotNull ValueParameterDescriptor valueParameter, @NotNull ResolvedValueArgument valueArgument) {
assert !valueArguments.containsKey(valueParameter) : valueParameter + " -> " + valueArgument;
valueArguments.put(valueParameter, valueArgument);
for (ValueArgument argument : valueArgument.getArguments()) {
argumentToParameterMap.put(argument, new ArgumentMatchImpl(valueParameter));
}
}
@Override
@NotNull
public Receiver getExtensionReceiver() {
return extensionReceiver;
}
@Override
@NotNull
public ReceiverValue getDispatchReceiver() {
return dispatchReceiver;
}
@Override
@NotNull
public ExplicitReceiverKind getExplicitReceiverKind() {
return explicitReceiverKind;
}
@Override
@NotNull
public Map getValueArguments() {
return valueArguments;
}
@Nullable
@Override
public List getValueArgumentsByIndex() {
List arguments = new ArrayList(candidateDescriptor.getValueParameters().size());
for (int i = 0; i < candidateDescriptor.getValueParameters().size(); ++i) {
arguments.add(null);
}
for (Map.Entry entry : valueArguments.entrySet()) {
ValueParameterDescriptor parameterDescriptor = entry.getKey();
ResolvedValueArgument value = entry.getValue();
ResolvedValueArgument oldValue = arguments.set(parameterDescriptor.getIndex(), value);
if (oldValue != null) {
return null;
}
}
for (int i = 0; i < arguments.size(); i++) {
Object o = arguments.get(i);
if (o == null) {
return null;
}
}
return arguments;
}
@Override
public void recordArgumentMatchStatus(@NotNull ValueArgument valueArgument, @NotNull ArgumentMatchStatus matchStatus) {
ArgumentMatchImpl argumentMatch = argumentToParameterMap.get(valueArgument);
argumentMatch.recordMatchStatus(matchStatus);
}
@NotNull
@Override
public ArgumentMapping getArgumentMapping(@NotNull ValueArgument valueArgument) {
ArgumentMatch argumentMatch = argumentToParameterMap.get(valueArgument);
if (argumentMatch == null) {
if (ArgumentMappingKt.isReallySuccess(this)) {
LOG.error("ArgumentUnmapped for " + valueArgument + " in successfully resolved call: " + call.getCallElement().getText());
}
return ArgumentUnmapped.INSTANCE$;
}
return argumentMatch;
}
@NotNull
@Override
public Map getTypeArguments() {
return typeArguments;
}
@Override
public boolean isSafeCall() {
return CallUtilKt.isSafeCall(call);
}
@NotNull
@Override
public MutableDataFlowInfoForArguments getDataFlowInfoForArguments() {
return dataFlowInfoForArguments;
}
@Override
public boolean hasInferredReturnType() {
if (!completed) {
hasInferredReturnType = constraintSystem == null ||
CallResolverUtilKt.hasInferredReturnType(candidateDescriptor, constraintSystem);
}
assert hasInferredReturnType != null : "The property 'hasInferredReturnType' was not set when the call was completed.";
return hasInferredReturnType;
}
@Override
public void markCallAsCompleted() {
if (!completed) {
hasInferredReturnType();
}
trace = null;
constraintSystem = null;
tracing = null;
completed = true;
}
@Override
public boolean isCompleted() {
return completed;
}
private void assertNotCompleted(String elementName) {
assert !completed: elementName + " is erased after resolution completion.";
}
@Override
@Nullable
public TypeSubstitutor getKnownTypeParametersSubstitutor() {
return knownTypeParametersSubstitutor;
}
}