Refactoring: extracted 'BindingContext.getDataFlowInfo'

This commit is contained in:
Svetlana Isakova
2014-08-23 12:23:29 +04:00
parent 0b9c62ab54
commit 3767187027
10 changed files with 26 additions and 23 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.bindingContextUtil.BindingContextUtilPackage;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.callUtil.CallUtilPackage;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
@@ -145,10 +146,7 @@ public class BindingContextUtils {
@Nullable
public static JetTypeInfo getRecordedTypeInfo(@NotNull JetExpression expression, @NotNull BindingContext context) {
if (!context.get(BindingContext.PROCESSED, expression)) return null;
DataFlowInfo dataFlowInfo = context.get(BindingContext.EXPRESSION_DATA_FLOW_INFO, expression);
if (dataFlowInfo == null) {
dataFlowInfo = DataFlowInfo.EMPTY;
}
DataFlowInfo dataFlowInfo = BindingContextUtilPackage.getDataFlowInfo(context, expression);
JetType type = context.get(BindingContext.EXPRESSION_TYPE, expression);
return JetTypeInfo.create(type, dataFlowInfo);
}
@@ -54,3 +54,6 @@ public fun <C : ResolutionContext<C>> ResolutionContext<C>.recordScopeAndDataFlo
trace.record(BindingContext.EXPRESSION_DATA_FLOW_INFO, expression, dataFlowInfo)
}
}
public fun BindingContext.getDataFlowInfo(expression: JetExpression?): DataFlowInfo =
expression?.let { this[BindingContext.EXPRESSION_DATA_FLOW_INFO, it] } ?: DataFlowInfo.EMPTY
@@ -46,6 +46,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.jetbrains.jet.lang.resolve.bindingContextUtil.BindingContextUtilPackage.getDataFlowInfo;
public class ElementResolver {
protected final ResolveSession resolveSession;
@@ -216,12 +218,12 @@ public class ElementResolver {
codeFragmentScope
);
DataFlowInfo dataFlowInfoForContextElement = contextForElement.get(BindingContext.EXPRESSION_DATA_FLOW_INFO, contextExpression);
DataFlowInfo dataFlowInfoForContextElement = getDataFlowInfo(contextForElement, contextExpression);
AnalyzerPackage.computeTypeInContext(
(JetExpression) codeFragmentExpression,
chainedScope,
trace,
dataFlowInfoForContextElement == null ? DataFlowInfo.EMPTY : dataFlowInfoForContextElement,
dataFlowInfoForContextElement,
TypeUtils.NO_EXPECTED_TYPE,
resolveSession.getModuleDescriptor()
);
@@ -63,6 +63,7 @@ import org.jetbrains.jet.analyzer.analyzeInContext
import org.jetbrains.jet.lang.resolve.BindingTraceContext
import org.jetbrains.jet.lang.types.TypeUtils
import org.jetbrains.jet.lang.resolve.scopes.ChainedScope
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getDataFlowInfo
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor
public trait CacheExtension<T> {
@@ -289,8 +290,7 @@ private object KotlinResolveDataProvider {
"Scope for resolve code fragment",
scopeForContextElement, codeFragmentScope)
val dataFlowInfoForContextElement = contextForElement[BindingContext.EXPRESSION_DATA_FLOW_INFO, contextElement]
val dataFlowInfo = dataFlowInfoForContextElement ?: DataFlowInfo.EMPTY
val dataFlowInfo = contextForElement.getDataFlowInfo(contextElement)
return codeFragmentExpression.analyzeInContext(
chainedScope,
BindingTraceContext(),
@@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.psi.JetReturnExpression
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody
import org.jetbrains.jet.lang.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.jet.lang.psi.psiUtil.getTextWithLocation
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getDataFlowInfo
public class RemoveExplicitTypeArguments : JetSelfTargetingIntention<JetTypeArgumentList>(
"remove.explicit.type.arguments", javaClass()) {
@@ -72,7 +73,7 @@ public class RemoveExplicitTypeArguments : JetSelfTargetingIntention<JetTypeArgu
else {
TypeUtils.NO_EXPECTED_TYPE
}
val dataFlow = context[BindingContext.EXPRESSION_DATA_FLOW_INFO, callExpression] ?: DataFlowInfo.EMPTY
val dataFlow = context.getDataFlowInfo(callExpression)
val resolutionResults = injector.getExpressionTypingServices()?.getCallResolver()?.resolveFunctionCall(
BindingTraceContext(), scope, untypedCall, jType, dataFlow, false)
assert (resolutionResults?.isSingleResult() ?: true) { "Removing type arguments changed resolve for: " +
@@ -39,6 +39,8 @@ import org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils;
import java.util.*;
import static org.jetbrains.jet.lang.resolve.bindingContextUtil.BindingContextUtilPackage.getDataFlowInfo;
public final class TipsManager {
private TipsManager() {
@@ -66,11 +68,7 @@ public final class TipsManager {
if (expressionType != null && resolutionScope != null && !expressionType.isError()) {
ExpressionReceiver receiverValue = new ExpressionReceiver(receiverExpression, expressionType);
DataFlowInfo info = context.get(BindingContext.EXPRESSION_DATA_FLOW_INFO, expression);
if (info == null) {
info = DataFlowInfo.EMPTY;
}
DataFlowInfo info = getDataFlowInfo(context, expression);
List<JetType> variantsForExplicitReceiver = AutoCastUtils.getAutoCastVariants(receiverValue, context, info);
for (JetType variant : variantsForExplicitReceiver) {
@@ -58,6 +58,7 @@ import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace
import org.jetbrains.jet.lang.psi.JetPrefixExpression
import org.jetbrains.jet.lang.resolve.calls.util.DelegatingCall
import org.jetbrains.jet.lang.psi.JetFunctionLiteralArgument
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getDataFlowInfo
enum class Tail {
COMMA
@@ -128,7 +129,7 @@ class ExpectedInfos(val bindingContext: BindingContext, val moduleDescriptor: Mo
val resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, calleeExpression] ?: return null //TODO: discuss it
val expectedType = (callElement as? JetExpression)?.let { bindingContext[BindingContext.EXPECTED_EXPRESSION_TYPE, it] } ?: TypeUtils.NO_EXPECTED_TYPE
val dataFlowInfo = (callElement as? JetExpression)?.let { bindingContext[BindingContext.EXPRESSION_DATA_FLOW_INFO, it] } ?: DataFlowInfo.EMPTY
val dataFlowInfo = bindingContext.getDataFlowInfo(calleeExpression)
val callResolutionContext = BasicCallResolutionContext.create(
DelegatingBindingTrace(bindingContext, "Temporary trace for completion"),
resolutionScope,
@@ -35,10 +35,11 @@ import java.util.HashSet
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.scopes.receivers.ThisReceiver
import org.jetbrains.jet.plugin.util.makeNotNullable
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getDataFlowInfo
class TypesWithAutoCasts(val bindingContext: BindingContext) {
public fun calculate(expression: JetExpression, receiver: JetExpression?): (DeclarationDescriptor) -> Iterable<JetType> {
val dataFlowInfo = bindingContext[BindingContext.EXPRESSION_DATA_FLOW_INFO, expression]
val dataFlowInfo = bindingContext.getDataFlowInfo(expression)
val (variableToTypes: Map<VariableDescriptor, Collection<JetType>>, notNullVariables: Set<VariableDescriptor>)
= processDataFlowInfo(dataFlowInfo, receiver)
@@ -77,8 +78,8 @@ class TypesWithAutoCasts(val bindingContext: BindingContext) {
val notNullVariables: Set<VariableDescriptor> = Collections.emptySet()
)
private fun processDataFlowInfo(dataFlowInfo: DataFlowInfo?, receiver: JetExpression?): ProcessDataFlowInfoResult {
if (dataFlowInfo == null) return ProcessDataFlowInfoResult()
private fun processDataFlowInfo(dataFlowInfo: DataFlowInfo, receiver: JetExpression?): ProcessDataFlowInfoResult {
if (dataFlowInfo == DataFlowInfo.EMPTY) return ProcessDataFlowInfoResult()
val dataFlowValueToVariable: (DataFlowValue) -> VariableDescriptor?
if (receiver != null) {
@@ -40,6 +40,7 @@ import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.ObservableBindingTrace;
import org.jetbrains.jet.lang.resolve.bindingContextUtil.BindingContextUtilPackage;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -124,10 +125,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
final JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, expression); //can be null or error type
JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, expression);
if (scope != null) {
DataFlowInfo dataFlowInfo = bindingContext.get(BindingContext.EXPRESSION_DATA_FLOW_INFO, expression);
if (dataFlowInfo == null) {
dataFlowInfo = DataFlowInfo.EMPTY;
}
DataFlowInfo dataFlowInfo = BindingContextUtilPackage.getDataFlowInfo(bindingContext, expression);
ObservableBindingTrace bindingTrace = new ObservableBindingTrace(new BindingTraceContext());
JetType typeNoExpectedType = AnalyzerPackage.computeTypeInfoInContext(
@@ -29,6 +29,7 @@ import java.io.File
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.jet.lang.resolve.bindingContextUtil.getDataFlowInfo
public abstract class AbstractDataFlowValueRenderingTest: JetLightCodeInsightFixtureTestCase() {
override fun getTestDataPath() : String {
@@ -46,7 +47,7 @@ public abstract class AbstractDataFlowValueRenderingTest: JetLightCodeInsightFix
val jetFile = fixture.getFile() as JetFile
val element = jetFile.findElementAt(fixture.getCaretOffset())
val expression = PsiTreeUtil.getParentOfType(element, javaClass<JetExpression>())!!
val info = AnalyzerFacadeWithCache.getContextForElement(expression)[BindingContext.EXPRESSION_DATA_FLOW_INFO, expression]!!
val info = AnalyzerFacadeWithCache.getContextForElement(expression).getDataFlowInfo(expression)
val allValues = (info.getCompleteTypeInfo().keySet() + info.getCompleteNullabilityInfo().keySet()).toSet()
val actual = allValues.map { renderDataFlowValue(it) }.filterNotNull().sort().makeString("\n")