Renamed "containingDeclaration" parameters to "containingDeclarationOrModule" to make it more clear that we can pass ModuleDescriptor instead of precise containing declaration
This commit is contained in:
+11
-11
@@ -57,7 +57,7 @@ public class DataFlowValueFactory {
|
|||||||
@NotNull JetExpression expression,
|
@NotNull JetExpression expression,
|
||||||
@NotNull JetType type,
|
@NotNull JetType type,
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration
|
@NotNull DeclarationDescriptor containingDeclarationOrModule
|
||||||
) {
|
) {
|
||||||
if (expression instanceof JetConstantExpression) {
|
if (expression instanceof JetConstantExpression) {
|
||||||
JetConstantExpression constantExpression = (JetConstantExpression) expression;
|
JetConstantExpression constantExpression = (JetConstantExpression) expression;
|
||||||
@@ -67,7 +67,7 @@ public class DataFlowValueFactory {
|
|||||||
if (KotlinBuiltIns.getInstance().getNullableNothingType().equals(type)) {
|
if (KotlinBuiltIns.getInstance().getNullableNothingType().equals(type)) {
|
||||||
return DataFlowValue.NULL; // 'null' is the only inhabitant of 'Nothing?'
|
return DataFlowValue.NULL; // 'null' is the only inhabitant of 'Nothing?'
|
||||||
}
|
}
|
||||||
IdentifierInfo result = getIdForStableIdentifier(expression, bindingContext, containingDeclaration);
|
IdentifierInfo result = getIdForStableIdentifier(expression, bindingContext, containingDeclarationOrModule);
|
||||||
return new DataFlowValue(result == NO_IDENTIFIER_INFO ? expression : result.id, type, result.isStable, getImmanentNullability(type));
|
return new DataFlowValue(result == NO_IDENTIFIER_INFO ? expression : result.id, type, result.isStable, getImmanentNullability(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ public class DataFlowValueFactory {
|
|||||||
public static DataFlowValue createDataFlowValue(
|
public static DataFlowValue createDataFlowValue(
|
||||||
@NotNull ReceiverValue receiverValue,
|
@NotNull ReceiverValue receiverValue,
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration
|
@NotNull DeclarationDescriptor containingDeclarationOrModule
|
||||||
) {
|
) {
|
||||||
if (receiverValue instanceof TransientReceiver || receiverValue instanceof ScriptReceiver) {
|
if (receiverValue instanceof TransientReceiver || receiverValue instanceof ScriptReceiver) {
|
||||||
// SCRIPT: smartcasts data flow
|
// SCRIPT: smartcasts data flow
|
||||||
@@ -105,7 +105,7 @@ public class DataFlowValueFactory {
|
|||||||
return createDataFlowValue(((ExpressionReceiver) receiverValue).getExpression(),
|
return createDataFlowValue(((ExpressionReceiver) receiverValue).getExpression(),
|
||||||
receiverValue.getType(),
|
receiverValue.getType(),
|
||||||
bindingContext,
|
bindingContext,
|
||||||
containingDeclaration);
|
containingDeclarationOrModule);
|
||||||
}
|
}
|
||||||
else if (receiverValue == ReceiverValue.NO_RECEIVER) {
|
else if (receiverValue == ReceiverValue.NO_RECEIVER) {
|
||||||
throw new IllegalArgumentException("No DataFlowValue exists for ReceiverValue.NO_RECEIVER");
|
throw new IllegalArgumentException("No DataFlowValue exists for ReceiverValue.NO_RECEIVER");
|
||||||
@@ -175,25 +175,25 @@ public class DataFlowValueFactory {
|
|||||||
private static IdentifierInfo getIdForStableIdentifier(
|
private static IdentifierInfo getIdForStableIdentifier(
|
||||||
@Nullable JetExpression expression,
|
@Nullable JetExpression expression,
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration
|
@NotNull DeclarationDescriptor containingDeclarationOrModule
|
||||||
) {
|
) {
|
||||||
if (expression != null) {
|
if (expression != null) {
|
||||||
JetExpression deparenthesized = JetPsiUtil.deparenthesize(expression);
|
JetExpression deparenthesized = JetPsiUtil.deparenthesize(expression);
|
||||||
if (expression != deparenthesized) {
|
if (expression != deparenthesized) {
|
||||||
return getIdForStableIdentifier(deparenthesized, bindingContext, containingDeclaration);
|
return getIdForStableIdentifier(deparenthesized, bindingContext, containingDeclarationOrModule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (expression instanceof JetQualifiedExpression) {
|
if (expression instanceof JetQualifiedExpression) {
|
||||||
JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) expression;
|
JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) expression;
|
||||||
JetExpression receiverExpression = qualifiedExpression.getReceiverExpression();
|
JetExpression receiverExpression = qualifiedExpression.getReceiverExpression();
|
||||||
JetExpression selectorExpression = qualifiedExpression.getSelectorExpression();
|
JetExpression selectorExpression = qualifiedExpression.getSelectorExpression();
|
||||||
IdentifierInfo receiverId = getIdForStableIdentifier(receiverExpression, bindingContext, containingDeclaration);
|
IdentifierInfo receiverId = getIdForStableIdentifier(receiverExpression, bindingContext, containingDeclarationOrModule);
|
||||||
IdentifierInfo selectorId = getIdForStableIdentifier(selectorExpression, bindingContext, containingDeclaration);
|
IdentifierInfo selectorId = getIdForStableIdentifier(selectorExpression, bindingContext, containingDeclarationOrModule);
|
||||||
|
|
||||||
return combineInfo(receiverId, selectorId);
|
return combineInfo(receiverId, selectorId);
|
||||||
}
|
}
|
||||||
if (expression instanceof JetSimpleNameExpression) {
|
if (expression instanceof JetSimpleNameExpression) {
|
||||||
return getIdForSimpleNameExpression((JetSimpleNameExpression) expression, bindingContext, containingDeclaration);
|
return getIdForSimpleNameExpression((JetSimpleNameExpression) expression, bindingContext, containingDeclarationOrModule);
|
||||||
}
|
}
|
||||||
else if (expression instanceof JetThisExpression) {
|
else if (expression instanceof JetThisExpression) {
|
||||||
JetThisExpression thisExpression = (JetThisExpression) expression;
|
JetThisExpression thisExpression = (JetThisExpression) expression;
|
||||||
@@ -211,7 +211,7 @@ public class DataFlowValueFactory {
|
|||||||
private static IdentifierInfo getIdForSimpleNameExpression(
|
private static IdentifierInfo getIdForSimpleNameExpression(
|
||||||
@NotNull JetSimpleNameExpression simpleNameExpression,
|
@NotNull JetSimpleNameExpression simpleNameExpression,
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration
|
@NotNull DeclarationDescriptor containingDeclarationOrModule
|
||||||
) {
|
) {
|
||||||
DeclarationDescriptor declarationDescriptor = bindingContext.get(REFERENCE_TARGET, simpleNameExpression);
|
DeclarationDescriptor declarationDescriptor = bindingContext.get(REFERENCE_TARGET, simpleNameExpression);
|
||||||
if (declarationDescriptor instanceof VariableDescriptor) {
|
if (declarationDescriptor instanceof VariableDescriptor) {
|
||||||
@@ -221,7 +221,7 @@ public class DataFlowValueFactory {
|
|||||||
// KT-4113
|
// KT-4113
|
||||||
// for now it fails for resolving 'invoke' convention, return it after 'invoke' algorithm changes
|
// for now it fails for resolving 'invoke' convention, return it after 'invoke' algorithm changes
|
||||||
// assert resolvedCall != null : "Cannot create right identifier info if the resolved call is not known yet for
|
// assert resolvedCall != null : "Cannot create right identifier info if the resolved call is not known yet for
|
||||||
ModuleDescriptor usageModuleDescriptor = DescriptorUtils.getContainingModuleOrNull(containingDeclaration);
|
ModuleDescriptor usageModuleDescriptor = DescriptorUtils.getContainingModuleOrNull(containingDeclarationOrModule);
|
||||||
IdentifierInfo receiverInfo =
|
IdentifierInfo receiverInfo =
|
||||||
resolvedCall != null ? getIdForImplicitReceiver(resolvedCall.getDispatchReceiver(), simpleNameExpression) : null;
|
resolvedCall != null ? getIdForImplicitReceiver(resolvedCall.getDispatchReceiver(), simpleNameExpression) : null;
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -61,12 +61,12 @@ public class SmartCastUtils {
|
|||||||
public static List<JetType> getSmartCastVariants(
|
public static List<JetType> getSmartCastVariants(
|
||||||
@NotNull ReceiverValue receiverToCast,
|
@NotNull ReceiverValue receiverToCast,
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration,
|
@NotNull DeclarationDescriptor containingDeclarationOrModule,
|
||||||
@NotNull DataFlowInfo dataFlowInfo
|
@NotNull DataFlowInfo dataFlowInfo
|
||||||
) {
|
) {
|
||||||
List<JetType> variants = Lists.newArrayList();
|
List<JetType> variants = Lists.newArrayList();
|
||||||
variants.add(receiverToCast.getType());
|
variants.add(receiverToCast.getType());
|
||||||
variants.addAll(getSmartCastVariantsExcludingReceiver(bindingContext, containingDeclaration, dataFlowInfo, receiverToCast));
|
variants.addAll(getSmartCastVariantsExcludingReceiver(bindingContext, containingDeclarationOrModule, dataFlowInfo, receiverToCast));
|
||||||
return variants;
|
return variants;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,11 +74,11 @@ public class SmartCastUtils {
|
|||||||
public static List<JetType> getSmartCastVariantsWithLessSpecificExcluded(
|
public static List<JetType> getSmartCastVariantsWithLessSpecificExcluded(
|
||||||
@NotNull ReceiverValue receiverToCast,
|
@NotNull ReceiverValue receiverToCast,
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration,
|
@NotNull DeclarationDescriptor containingDeclarationOrModule,
|
||||||
@NotNull DataFlowInfo dataFlowInfo
|
@NotNull DataFlowInfo dataFlowInfo
|
||||||
) {
|
) {
|
||||||
final List<JetType> variants = getSmartCastVariants(receiverToCast, bindingContext,
|
final List<JetType> variants = getSmartCastVariants(receiverToCast, bindingContext,
|
||||||
containingDeclaration, dataFlowInfo);
|
containingDeclarationOrModule, dataFlowInfo);
|
||||||
return KotlinPackage.filter(variants, new Function1<JetType, Boolean>() {
|
return KotlinPackage.filter(variants, new Function1<JetType, Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public Boolean invoke(final JetType type) {
|
public Boolean invoke(final JetType type) {
|
||||||
@@ -112,7 +112,7 @@ public class SmartCastUtils {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static Collection<JetType> getSmartCastVariantsExcludingReceiver(
|
public static Collection<JetType> getSmartCastVariantsExcludingReceiver(
|
||||||
@NotNull BindingContext bindingContext,
|
@NotNull BindingContext bindingContext,
|
||||||
@NotNull DeclarationDescriptor containingDeclaration,
|
@NotNull DeclarationDescriptor containingDeclarationOrModule,
|
||||||
@NotNull DataFlowInfo dataFlowInfo,
|
@NotNull DataFlowInfo dataFlowInfo,
|
||||||
@NotNull ReceiverValue receiverToCast
|
@NotNull ReceiverValue receiverToCast
|
||||||
) {
|
) {
|
||||||
@@ -124,7 +124,7 @@ public class SmartCastUtils {
|
|||||||
}
|
}
|
||||||
else if (receiverToCast instanceof ExpressionReceiver) {
|
else if (receiverToCast instanceof ExpressionReceiver) {
|
||||||
DataFlowValue dataFlowValue = DataFlowValueFactory.createDataFlowValue(
|
DataFlowValue dataFlowValue = DataFlowValueFactory.createDataFlowValue(
|
||||||
receiverToCast, bindingContext, containingDeclaration);
|
receiverToCast, bindingContext, containingDeclarationOrModule);
|
||||||
return dataFlowInfo.getPossibleTypes(dataFlowValue);
|
return dataFlowInfo.getPossibleTypes(dataFlowValue);
|
||||||
}
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import org.jetbrains.kotlin.idea.util.makeNotNullable
|
|||||||
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
|
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
|
||||||
import org.jetbrains.kotlin.utils.singletonOrEmptyList
|
import org.jetbrains.kotlin.utils.singletonOrEmptyList
|
||||||
|
|
||||||
class SmartCastCalculator(val bindingContext: BindingContext, val containingDeclaration: DeclarationDescriptor) {
|
class SmartCastCalculator(val bindingContext: BindingContext, val containingDeclarationOrModule: DeclarationDescriptor) {
|
||||||
public fun calculate(position: JetExpression, receiver: JetExpression?): (VariableDescriptor) -> Collection<JetType> {
|
public fun calculate(position: JetExpression, receiver: JetExpression?): (VariableDescriptor) -> Collection<JetType> {
|
||||||
val dataFlowInfo = bindingContext.getDataFlowInfo(position)
|
val dataFlowInfo = bindingContext.getDataFlowInfo(position)
|
||||||
val (variableToTypes, notNullVariables) = processDataFlowInfo(dataFlowInfo, receiver)
|
val (variableToTypes, notNullVariables) = processDataFlowInfo(dataFlowInfo, receiver)
|
||||||
@@ -66,7 +66,7 @@ class SmartCastCalculator(val bindingContext: BindingContext, val containingDecl
|
|||||||
val dataFlowValueToVariable: (DataFlowValue) -> VariableDescriptor?
|
val dataFlowValueToVariable: (DataFlowValue) -> VariableDescriptor?
|
||||||
if (receiver != null) {
|
if (receiver != null) {
|
||||||
val receiverType = bindingContext[BindingContext.EXPRESSION_TYPE, receiver] ?: return ProcessDataFlowInfoResult()
|
val receiverType = bindingContext[BindingContext.EXPRESSION_TYPE, receiver] ?: return ProcessDataFlowInfoResult()
|
||||||
val receiverId = DataFlowValueFactory.createDataFlowValue(receiver, receiverType, bindingContext, containingDeclaration).getId()
|
val receiverId = DataFlowValueFactory.createDataFlowValue(receiver, receiverType, bindingContext, containingDeclarationOrModule).getId()
|
||||||
dataFlowValueToVariable = { value ->
|
dataFlowValueToVariable = { value ->
|
||||||
val id = value.getId()
|
val id = value.getId()
|
||||||
if (id is Pair<*, *> && id.first == receiverId) id.second as? VariableDescriptor else null
|
if (id is Pair<*, *> && id.first == receiverId) id.second as? VariableDescriptor else null
|
||||||
|
|||||||
Reference in New Issue
Block a user