Minor. introduce method ExpressionReceiver.create and replace almost all usages of constructor ExpressionReceiver.

This commit is contained in:
Stanislav Erokhin
2015-11-15 21:48:38 +03:00
parent e606c0cdf5
commit 1c9136a8cd
14 changed files with 31 additions and 22 deletions
@@ -145,7 +145,7 @@ public class DelegatedPropertyResolver {
KtPsiFactory psiFactory = KtPsiFactory(delegateExpression); KtPsiFactory psiFactory = KtPsiFactory(delegateExpression);
List<KtExpression> arguments = Collections.singletonList(createExpressionForProperty(psiFactory)); List<KtExpression> arguments = Collections.singletonList(createExpressionForProperty(psiFactory));
ExpressionReceiver receiver = new ExpressionReceiver(delegateExpression, delegateType); ExpressionReceiver receiver = ExpressionReceiver.create(delegateExpression, delegateType, trace.getBindingContext());
Pair<Call, OverloadResolutionResults<FunctionDescriptor>> resolutionResult = Pair<Call, OverloadResolutionResults<FunctionDescriptor>> resolutionResult =
fakeCallResolver.makeAndResolveFakeCallInContext(receiver, context, arguments, PROPERTY_DELEGATED_FUNCTION_NAME, delegateExpression); fakeCallResolver.makeAndResolveFakeCallInContext(receiver, context, arguments, PROPERTY_DELEGATED_FUNCTION_NAME, delegateExpression);
@@ -261,7 +261,7 @@ public class DelegatedPropertyResolver {
} }
Name functionName = isGet ? GETTER_NAME : SETTER_NAME; Name functionName = isGet ? GETTER_NAME : SETTER_NAME;
ExpressionReceiver receiver = new ExpressionReceiver(delegateExpression, delegateType); ExpressionReceiver receiver = ExpressionReceiver.create(delegateExpression, delegateType, trace.getBindingContext());
Pair<Call, OverloadResolutionResults<FunctionDescriptor>> resolutionResult = Pair<Call, OverloadResolutionResults<FunctionDescriptor>> resolutionResult =
fakeCallResolver.makeAndResolveFakeCallInContext(receiver, context, arguments, functionName, delegateExpression); fakeCallResolver.makeAndResolveFakeCallInContext(receiver, context, arguments, functionName, delegateExpression);
@@ -348,7 +348,9 @@ public class CallExpressionResolver {
} }
QualifierReceiver qualifierReceiver = (QualifierReceiver) context.trace.get(BindingContext.QUALIFIER, element.getReceiver()); QualifierReceiver qualifierReceiver = (QualifierReceiver) context.trace.get(BindingContext.QUALIFIER, element.getReceiver());
ReceiverValue receiver = qualifierReceiver == null ? new ExpressionReceiver(element.getReceiver(), receiverType) : qualifierReceiver; ReceiverValue receiver = qualifierReceiver == null ?
ExpressionReceiver.create(element.getReceiver(), receiverType, context.trace.getBindingContext()) :
qualifierReceiver;
boolean lastStage = element.getQualified() == expression; boolean lastStage = element.getQualified() == expression;
assert lastStage == (element == elementChain.getLast()); assert lastStage == (element == elementChain.getLast());
@@ -315,7 +315,7 @@ public class CallResolver {
} }
KotlinType calleeType = expressionTypingServices.safeGetType( KotlinType calleeType = expressionTypingServices.safeGetType(
context.scope, calleeExpression, expectedType, context.dataFlowInfo, context.trace); context.scope, calleeExpression, expectedType, context.dataFlowInfo, context.trace);
ExpressionReceiver expressionReceiver = new ExpressionReceiver(calleeExpression, calleeType); ExpressionReceiver expressionReceiver = ExpressionReceiver.create(calleeExpression, calleeType, context.trace.getBindingContext());
Call call = new CallTransformer.CallForImplicitInvoke(context.call.getExplicitReceiver(), expressionReceiver, context.call); Call call = new CallTransformer.CallForImplicitInvoke(context.call.getExplicitReceiver(), expressionReceiver, context.call);
TracingStrategyForInvoke tracingForInvoke = new TracingStrategyForInvoke(calleeExpression, call, calleeType); TracingStrategyForInvoke tracingForInvoke = new TracingStrategyForInvoke(calleeExpression, call, calleeType);
@@ -235,7 +235,8 @@ public class CallTransformer<D extends CallableDescriptor, F extends D> {
KtExpression calleeExpression = task.call.getCalleeExpression(); KtExpression calleeExpression = task.call.getCalleeExpression();
if (calleeExpression == null) return Collections.emptyList(); if (calleeExpression == null) return Collections.emptyList();
ExpressionReceiver variableReceiver = new ExpressionReceiver(calleeExpression, variableResolvedCall.getResultingDescriptor().getType()); ExpressionReceiver variableReceiver = ExpressionReceiver.create(
calleeExpression, variableResolvedCall.getResultingDescriptor().getType(), context.trace.getBindingContext());
Call functionCall = new CallForImplicitInvoke(context.explicitExtensionReceiverForInvoke, variableReceiver, task.call); Call functionCall = new CallForImplicitInvoke(context.explicitExtensionReceiverForInvoke, variableReceiver, task.call);
DelegatingBindingTrace variableCallTrace = context.candidateCall.getTrace(); DelegatingBindingTrace variableCallTrace = context.candidateCall.getTrace();
@@ -364,7 +364,7 @@ public class CandidateResolver(
expectedType: KotlinType, expectedType: KotlinType,
actualType: KotlinType, actualType: KotlinType,
context: ResolutionContext<*>): KotlinType? { context: ResolutionContext<*>): KotlinType? {
val receiverToCast = ExpressionReceiver(KtPsiUtil.safeDeparenthesize(expression), actualType) val receiverToCast = ExpressionReceiver.create(KtPsiUtil.safeDeparenthesize(expression), actualType, context.trace.bindingContext)
val variants = smartCastManager.getSmartCastVariantsExcludingReceiver(context, receiverToCast) val variants = smartCastManager.getSmartCastVariantsExcludingReceiver(context, receiverToCast)
for (possibleType in variants) { for (possibleType in variants) {
if (KotlinTypeChecker.DEFAULT.isSubtypeOf(possibleType, expectedType)) { if (KotlinTypeChecker.DEFAULT.isSubtypeOf(possibleType, expectedType)) {
@@ -18,10 +18,19 @@ package org.jetbrains.kotlin.resolve.scopes.receivers;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.KtExpression; import org.jetbrains.kotlin.psi.KtExpression;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.types.KotlinType; import org.jetbrains.kotlin.types.KotlinType;
public class ExpressionReceiver extends AbstractReceiverValue implements ReceiverValue { public class ExpressionReceiver extends AbstractReceiverValue implements ReceiverValue {
public static ExpressionReceiver create(
@NotNull KtExpression expression,
@NotNull KotlinType type,
@NotNull BindingContext bindingContext
) {
return new ExpressionReceiver(expression, type);
}
private final KtExpression expression; private final KtExpression expression;
public ExpressionReceiver(@NotNull KtExpression expression, @NotNull KotlinType type) { public ExpressionReceiver(@NotNull KtExpression expression, @NotNull KotlinType type) {
@@ -760,7 +760,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
// Type check the base expression // Type check the base expression
KotlinTypeInfo typeInfo = facade.safeGetTypeInfo(baseExpression, context); KotlinTypeInfo typeInfo = facade.safeGetTypeInfo(baseExpression, context);
KotlinType type = ExpressionTypingUtils.safeGetType(typeInfo); KotlinType type = ExpressionTypingUtils.safeGetType(typeInfo);
ExpressionReceiver receiver = new ExpressionReceiver(baseExpression, type); ExpressionReceiver receiver = ExpressionReceiver.create(baseExpression, type, context.trace.getBindingContext());
Call call = CallMaker.makeCall(receiver, expression); Call call = CallMaker.makeCall(receiver, expression);
@@ -1495,7 +1495,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
KotlinType arrayType = ExpressionTypingUtils.safeGetType(arrayTypeInfo); KotlinType arrayType = ExpressionTypingUtils.safeGetType(arrayTypeInfo);
ExpressionTypingContext context = oldContext.replaceDataFlowInfo(arrayTypeInfo.getDataFlowInfo()); ExpressionTypingContext context = oldContext.replaceDataFlowInfo(arrayTypeInfo.getDataFlowInfo());
ExpressionReceiver receiver = new ExpressionReceiver(arrayExpression, arrayType); ExpressionReceiver receiver = ExpressionReceiver.create(arrayExpression, arrayType, context.trace.getBindingContext());
if (!isGet) assert rightHandSide != null; if (!isGet) assert rightHandSide != null;
Call call = isGet Call call = isGet
@@ -71,19 +71,15 @@ public class ExpressionTypingUtils {
return receiverValue; return receiverValue;
} }
@Nullable
public static ExpressionReceiver getExpressionReceiver(@NotNull KtExpression expression, @Nullable KotlinType type) {
if (type == null) return null;
return new ExpressionReceiver(expression, type);
}
@Nullable @Nullable
public static ExpressionReceiver getExpressionReceiver( public static ExpressionReceiver getExpressionReceiver(
@NotNull ExpressionTypingFacade facade, @NotNull ExpressionTypingFacade facade,
@NotNull KtExpression expression, @NotNull KtExpression expression,
ExpressionTypingContext context ExpressionTypingContext context
) { ) {
return getExpressionReceiver(expression, facade.getTypeInfo(expression, context).getType()); KotlinType type = facade.getTypeInfo(expression, context).getType();
if (type == null) return null;
return ExpressionReceiver.create(expression, type, context.trace.getBindingContext());
} }
@NotNull @NotNull
@@ -93,7 +89,7 @@ public class ExpressionTypingUtils {
ExpressionTypingContext context ExpressionTypingContext context
) { ) {
KotlinType type = safeGetType(facade.safeGetTypeInfo(expression, context)); KotlinType type = safeGetType(facade.safeGetTypeInfo(expression, context));
return new ExpressionReceiver(expression, type); return ExpressionReceiver.create(expression, type, context.trace.getBindingContext());
} }
@NotNull @NotNull
@@ -250,7 +250,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
temporary.commit(); temporary.commit();
return rightInfo.clearType(); return rightInfo.clearType();
} }
ExpressionReceiver receiver = new ExpressionReceiver(left, leftType); ExpressionReceiver receiver = ExpressionReceiver.create(left, leftType, context.trace.getBindingContext());
// We check that defined only one of '+=' and '+' operations, and call it (in the case '+' we then also assign) // We check that defined only one of '+=' and '+' operations, and call it (in the case '+' we then also assign)
// Check for '+=' // Check for '+='
@@ -248,7 +248,7 @@ public fun CallTypeAndReceiver<*, *>.receiverTypes(
val receiverValues = if (receiverExpression != null) { val receiverValues = if (receiverExpression != null) {
val expressionType = bindingContext.getType(receiverExpression) val expressionType = bindingContext.getType(receiverExpression)
expressionType?.let { listOf(ExpressionReceiver(receiverExpression, expressionType)) } ?: return emptyList() expressionType?.let { listOf(ExpressionReceiver.create(receiverExpression, expressionType, bindingContext)) } ?: return emptyList()
} }
else { else {
val resolutionScope = contextElement.getResolutionScope(bindingContext, resolutionFacade) val resolutionScope = contextElement.getResolutionScope(bindingContext, resolutionFacade)
@@ -63,7 +63,7 @@ public class ShadowedDeclarationsFilter(
val explicitReceiverValue = receiverExpression?.let { val explicitReceiverValue = receiverExpression?.let {
val type = bindingContext.getType(it) ?: return null val type = bindingContext.getType(it) ?: return null
ExpressionReceiver(it, type) ExpressionReceiver.create(it, type, bindingContext)
} ?: ReceiverValue.NO_RECEIVER } ?: ReceiverValue.NO_RECEIVER
return ShadowedDeclarationsFilter(bindingContext, resolutionFacade, context, explicitReceiverValue) return ShadowedDeclarationsFilter(bindingContext, resolutionFacade, context, explicitReceiverValue)
} }
@@ -370,7 +370,8 @@ abstract class CompletionSession(
val runtimeType = evaluator(explicitReceiver) val runtimeType = evaluator(explicitReceiver)
if (runtimeType == null || runtimeType == type) return null if (runtimeType == null || runtimeType == type) return null
val (variants, notImportedExtensions) = collectReferenceVariants(descriptorKindFilter!!, nameExpression!!, ExpressionReceiver(explicitReceiver, runtimeType)) val expressionReceiver = ExpressionReceiver.create(explicitReceiver, runtimeType, bindingContext)
val (variants, notImportedExtensions) = collectReferenceVariants(descriptorKindFilter!!, nameExpression!!, expressionReceiver)
val filteredVariants = filterVariantsForRuntimeReceiverType(variants, referenceVariants.imported) val filteredVariants = filterVariantsForRuntimeReceiverType(variants, referenceVariants.imported)
val filteredNotImportedExtensions = filterVariantsForRuntimeReceiverType(notImportedExtensions, referenceVariants.notImportedExtensions) val filteredNotImportedExtensions = filterVariantsForRuntimeReceiverType(notImportedExtensions, referenceVariants.notImportedExtensions)
@@ -72,8 +72,8 @@ public class IterableTypesDetection(
if (!canBeIterable(type)) return null if (!canBeIterable(type)) return null
val expression = KtPsiFactory(project).createExpression("fake") val expression = KtPsiFactory(project).createExpression("fake")
val expressionReceiver = ExpressionReceiver(expression, type.type)
val context = ExpressionTypingContext.newContext(BindingTraceContext(), scope, DataFlowInfo.EMPTY, TypeUtils.NO_EXPECTED_TYPE) val context = ExpressionTypingContext.newContext(BindingTraceContext(), scope, DataFlowInfo.EMPTY, TypeUtils.NO_EXPECTED_TYPE)
val expressionReceiver = ExpressionReceiver.create(expression, type.type, context.trace.bindingContext)
val elementType = forLoopConventionsChecker.checkIterableConvention(expressionReceiver, context) val elementType = forLoopConventionsChecker.checkIterableConvention(expressionReceiver, context)
return elementType?.let { FuzzyType(it, type.freeParameters) } return elementType?.let { FuzzyType(it, type.freeParameters) }
} }
@@ -44,7 +44,7 @@ fun DeclarationDescriptorWithVisibility.isVisible(
val receiver = element.getReceiverExpression() val receiver = element.getReceiverExpression()
val type = receiver?.let { bindingContext.getType(it) } val type = receiver?.let { bindingContext.getType(it) }
val explicitReceiver = type?.let { ExpressionReceiver(receiver!!, it) } val explicitReceiver = type?.let { ExpressionReceiver.create(receiver!!, it, bindingContext) }
if (explicitReceiver != null) { if (explicitReceiver != null) {
val normalizeReceiver = ExpressionTypingUtils.normalizeReceiverValueForVisibility(explicitReceiver, bindingContext) val normalizeReceiver = ExpressionTypingUtils.normalizeReceiverValueForVisibility(explicitReceiver, bindingContext)