refactoring: extracted getValueParametersTypes

This commit is contained in:
Svetlana Isakova
2012-12-25 19:45:51 +04:00
parent b0302246a0
commit 122c2cbefb
2 changed files with 26 additions and 12 deletions
@@ -409,4 +409,13 @@ public class DescriptorUtils {
return builtIns.getArrayType(projectionKind, elementType); return builtIns.getArrayType(projectionKind, elementType);
} }
} }
@NotNull
public static List<JetType> getValueParametersTypes(@NotNull List<ValueParameterDescriptor> valueParameters) {
List<JetType> parameterTypes = Lists.newArrayList();
for (ValueParameterDescriptor parameter : valueParameters) {
parameterTypes.add(parameter.getType());
}
return parameterTypes;
}
} }
@@ -101,11 +101,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
SimpleFunctionDescriptorImpl functionDescriptor = createFunctionDescriptor(expression, context, functionTypeExpected); SimpleFunctionDescriptorImpl functionDescriptor = createFunctionDescriptor(expression, context, functionTypeExpected);
List<JetType> parameterTypes = Lists.newArrayList();
List<ValueParameterDescriptor> valueParameters = functionDescriptor.getValueParameters(); List<ValueParameterDescriptor> valueParameters = functionDescriptor.getValueParameters();
for (ValueParameterDescriptor valueParameter : valueParameters) {
parameterTypes.add(valueParameter.getType());
}
ReceiverParameterDescriptor receiverParameter = functionDescriptor.getReceiverParameter(); ReceiverParameterDescriptor receiverParameter = functionDescriptor.getReceiverParameter();
JetType receiver = DescriptorUtils.getReceiverParameterType(receiverParameter); JetType receiver = DescriptorUtils.getReceiverParameterType(receiverParameter);
@@ -148,7 +144,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
functionDescriptor.setReturnType(safeReturnType); functionDescriptor.setReturnType(safeReturnType);
JetType resultType = KotlinBuiltIns.getInstance().getFunctionType( JetType resultType = KotlinBuiltIns.getInstance().getFunctionType(
Collections.<AnnotationDescriptor>emptyList(), receiver, parameterTypes, safeReturnType); Collections.<AnnotationDescriptor>emptyList(), receiver, DescriptorUtils.getValueParametersTypes(valueParameters), safeReturnType);
if (expectedType != NO_EXPECTED_TYPE && KotlinBuiltIns.getInstance().isFunctionType(expectedType)) { if (expectedType != NO_EXPECTED_TYPE && KotlinBuiltIns.getInstance().isFunctionType(expectedType)) {
// all checks were done before // all checks were done before
return JetTypeInfo.create(resultType, context.dataFlowInfo); return JetTypeInfo.create(resultType, context.dataFlowInfo);
@@ -156,7 +152,12 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
return DataFlowUtils.checkType(resultType, expression, context, context.dataFlowInfo); return DataFlowUtils.checkType(resultType, expression, context, context.dataFlowInfo);
} }
private SimpleFunctionDescriptorImpl createFunctionDescriptor(JetFunctionLiteralExpression expression, ExpressionTypingContext context, boolean functionTypeExpected) { @NotNull
private SimpleFunctionDescriptorImpl createFunctionDescriptor(
@NotNull JetFunctionLiteralExpression expression,
@NotNull ExpressionTypingContext context,
boolean functionTypeExpected
) {
JetFunctionLiteral functionLiteral = expression.getFunctionLiteral(); JetFunctionLiteral functionLiteral = expression.getFunctionLiteral();
JetTypeReference receiverTypeRef = functionLiteral.getReceiverTypeRef(); JetTypeReference receiverTypeRef = functionLiteral.getReceiverTypeRef();
SimpleFunctionDescriptorImpl functionDescriptor = new SimpleFunctionDescriptorImpl( SimpleFunctionDescriptorImpl functionDescriptor = new SimpleFunctionDescriptorImpl(
@@ -190,7 +191,13 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
return functionDescriptor; return functionDescriptor;
} }
private List<ValueParameterDescriptor> createValueParameterDescriptors(ExpressionTypingContext context, JetFunctionLiteral functionLiteral, FunctionDescriptorImpl functionDescriptor, boolean functionTypeExpected) { @NotNull
private List<ValueParameterDescriptor> createValueParameterDescriptors(
@NotNull ExpressionTypingContext context,
@NotNull JetFunctionLiteral functionLiteral,
@NotNull FunctionDescriptorImpl functionDescriptor,
boolean functionTypeExpected
) {
List<ValueParameterDescriptor> valueParameterDescriptors = Lists.newArrayList(); List<ValueParameterDescriptor> valueParameterDescriptors = Lists.newArrayList();
List<JetParameter> declaredValueParameters = functionLiteral.getValueParameters(); List<JetParameter> declaredValueParameters = functionLiteral.getValueParameters();
@@ -203,17 +210,15 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
if (functionTypeExpected && !hasDeclaredValueParameters && expectedValueParameters.size() == 1) { if (functionTypeExpected && !hasDeclaredValueParameters && expectedValueParameters.size() == 1) {
ValueParameterDescriptor valueParameterDescriptor = expectedValueParameters.get(0); ValueParameterDescriptor valueParameterDescriptor = expectedValueParameters.get(0);
ValueParameterDescriptor it = new ValueParameterDescriptorImpl( ValueParameterDescriptor it = new ValueParameterDescriptorImpl(
functionDescriptor, 0, Collections.<AnnotationDescriptor>emptyList(), Name.identifier("it"), false, valueParameterDescriptor.getType(), valueParameterDescriptor.hasDefaultValue(), valueParameterDescriptor.getVarargElementType() functionDescriptor, 0, Collections.<AnnotationDescriptor>emptyList(), Name.identifier("it"), false,
valueParameterDescriptor.getType(), valueParameterDescriptor.hasDefaultValue(), valueParameterDescriptor.getVarargElementType()
); );
valueParameterDescriptors.add(it); valueParameterDescriptors.add(it);
context.trace.record(AUTO_CREATED_IT, it); context.trace.record(AUTO_CREATED_IT, it);
} }
else { else {
if (expectedValueParameters != null && declaredValueParameters.size() != expectedValueParameters.size()) { if (expectedValueParameters != null && declaredValueParameters.size() != expectedValueParameters.size()) {
List<JetType> expectedParameterTypes = Lists.newArrayList(); List<JetType> expectedParameterTypes = DescriptorUtils.getValueParametersTypes(expectedValueParameters);
for (ValueParameterDescriptor parameter : expectedValueParameters) {
expectedParameterTypes.add(parameter.getType());
}
context.trace.report(EXPECTED_PARAMETERS_NUMBER_MISMATCH.on(functionLiteral, expectedParameterTypes.size(), expectedParameterTypes)); context.trace.report(EXPECTED_PARAMETERS_NUMBER_MISMATCH.on(functionLiteral, expectedParameterTypes.size(), expectedParameterTypes));
} }
for (int i = 0; i < declaredValueParameters.size(); i++) { for (int i = 0; i < declaredValueParameters.size(); i++) {