From c8fc2c882db065b758c54a87254513c40f900344 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Fri, 16 Aug 2013 18:28:48 +0400 Subject: [PATCH] refactoring: extracted method 'getTypeOfLastExpressionInBlock' --- .../expressions/ExpressionTypingServices.java | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java index 45da542e870..c5f88374f1a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java @@ -259,40 +259,9 @@ public class ExpressionTypingServices { trace.record(STATEMENT, statement); JetExpression statementExpression = (JetExpression) statement; if (!iterator.hasNext()) { - if (noExpectedType(context.expectedType) && context.expectedType != UNIT_EXPECTED_TYPE) { - result = blockLevelVisitor.getTypeInfo(statementExpression, newContext, true); - if (coercionStrategyForLastExpression == COERCION_TO_UNIT) { - boolean mightBeUnit = false; - if (statementExpression instanceof JetDeclaration) { - mightBeUnit = true; - } - if (statementExpression instanceof JetBinaryExpression) { - JetBinaryExpression binaryExpression = (JetBinaryExpression) statementExpression; - IElementType operationType = binaryExpression.getOperationToken(); - //noinspection SuspiciousMethodCalls - if (operationType == JetTokens.EQ || OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(operationType)) { - mightBeUnit = true; - } - } - if (mightBeUnit) { - // ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments - assert result.getType() == null || KotlinBuiltIns.getInstance().isUnit(result.getType()); - result = JetTypeInfo.create(KotlinBuiltIns.getInstance().getUnitType(), newContext.dataFlowInfo); - } - } - } - else { - JetType expectedType; - if (context.expectedType == UNIT_EXPECTED_TYPE || (coercionStrategyForLastExpression == COERCION_TO_UNIT - && KotlinBuiltIns.getInstance().isUnit(context.expectedType))) { - expectedType = UNIT_EXPECTED_TYPE; - } - else { - expectedType = context.expectedType; - } - - result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceExpectedType(expectedType), true); - } + result = getTypeOfLastExpressionInBlock( + statementExpression, newContext.replaceExpectedType(context.expectedType), coercionStrategyForLastExpression, + blockLevelVisitor); } else { result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceContextDependency(ContextDependency.INDEPENDENT), true); @@ -307,6 +276,47 @@ public class ExpressionTypingServices { return result; } + private JetTypeInfo getTypeOfLastExpressionInBlock( + @NotNull JetExpression statementExpression, + @NotNull ExpressionTypingContext context, + @NotNull CoercionStrategy coercionStrategyForLastExpression, + @NotNull ExpressionTypingInternals blockLevelVisitor + ) { + if (!noExpectedType(context.expectedType) || context.expectedType == UNIT_EXPECTED_TYPE) { + JetType expectedType; + if (context.expectedType == UNIT_EXPECTED_TYPE ||//the first check is necessary to avoid invocation 'isUnit(UNIT_EXPECTED_TYPE)' + (coercionStrategyForLastExpression == COERCION_TO_UNIT && KotlinBuiltIns.getInstance().isUnit(context.expectedType))) { + expectedType = UNIT_EXPECTED_TYPE; + } + else { + expectedType = context.expectedType; + } + + return blockLevelVisitor.getTypeInfo(statementExpression, context.replaceExpectedType(expectedType), true); + } + JetTypeInfo result = blockLevelVisitor.getTypeInfo(statementExpression, context, true); + if (coercionStrategyForLastExpression == COERCION_TO_UNIT) { + boolean mightBeUnit = false; + if (statementExpression instanceof JetDeclaration) { + mightBeUnit = true; + } + if (statementExpression instanceof JetBinaryExpression) { + JetBinaryExpression binaryExpression = (JetBinaryExpression) statementExpression; + IElementType operationType = binaryExpression.getOperationToken(); + //noinspection SuspiciousMethodCalls + if (operationType == JetTokens.EQ || OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(operationType)) { + mightBeUnit = true; + } + } + if (mightBeUnit) { + // ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments + assert result.getType() == null || KotlinBuiltIns.getInstance().isUnit(result.getType()); + result = JetTypeInfo.create(KotlinBuiltIns.getInstance().getUnitType(), context.dataFlowInfo); + } + } + return result; + } + private ExpressionTypingContext createContext(ExpressionTypingContext oldContext, BindingTrace trace, WritableScope scope, DataFlowInfo dataFlowInfo, JetType expectedType) { return ExpressionTypingContext.newContext(this, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType, oldContext.expressionPosition, oldContext.contextDependency, oldContext.resolutionResultsCache);