refactoring: extracted method 'getTypeOfLastExpressionInBlock'

This commit is contained in:
Svetlana Isakova
2013-08-16 18:28:48 +04:00
parent 33ab7a3789
commit c8fc2c882d
@@ -259,8 +259,42 @@ public class ExpressionTypingServices {
trace.record(STATEMENT, statement); trace.record(STATEMENT, statement);
JetExpression statementExpression = (JetExpression) statement; JetExpression statementExpression = (JetExpression) statement;
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
if (noExpectedType(context.expectedType) && context.expectedType != UNIT_EXPECTED_TYPE) { result = getTypeOfLastExpressionInBlock(
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext, true); statementExpression, newContext.replaceExpectedType(context.expectedType), coercionStrategyForLastExpression,
blockLevelVisitor);
}
else {
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceContextDependency(ContextDependency.INDEPENDENT), true);
}
DataFlowInfo newDataFlowInfo = result.getDataFlowInfo();
if (newDataFlowInfo != context.dataFlowInfo) {
newContext = newContext.replaceDataFlowInfo(newDataFlowInfo);
}
blockLevelVisitor = ExpressionTypingVisitorDispatcher.createForBlock(scope);
}
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) { if (coercionStrategyForLastExpression == COERCION_TO_UNIT) {
boolean mightBeUnit = false; boolean mightBeUnit = false;
if (statementExpression instanceof JetDeclaration) { if (statementExpression instanceof JetDeclaration) {
@@ -277,33 +311,9 @@ public class ExpressionTypingServices {
if (mightBeUnit) { if (mightBeUnit) {
// ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments // ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments
assert result.getType() == null || KotlinBuiltIns.getInstance().isUnit(result.getType()); assert result.getType() == null || KotlinBuiltIns.getInstance().isUnit(result.getType());
result = JetTypeInfo.create(KotlinBuiltIns.getInstance().getUnitType(), newContext.dataFlowInfo); result = JetTypeInfo.create(KotlinBuiltIns.getInstance().getUnitType(), context.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);
}
}
else {
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceContextDependency(ContextDependency.INDEPENDENT), true);
}
DataFlowInfo newDataFlowInfo = result.getDataFlowInfo();
if (newDataFlowInfo != context.dataFlowInfo) {
newContext = newContext.replaceDataFlowInfo(newDataFlowInfo);
}
blockLevelVisitor = ExpressionTypingVisitorDispatcher.createForBlock(scope);
}
return result; return result;
} }