public Map<String, JetType> getPrimitiveTypesMap() { if (primitiveTypesMap == null) { primitiveTypesMap = new HashMap<String, JetType>(); for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) { PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType(); primitiveTypesMap.put( jvmPrimitiveType.getName(), standardLibrary.getPrimitiveJetType(primitiveType)); primitiveTypesMap.put( "[" + jvmPrimitiveType.getName(), standardLibrary.getPrimitiveArrayJetType(primitiveType)); primitiveTypesMap.put( jvmPrimitiveType.getWrapper().getFqName(), standardLibrary.getNullablePrimitiveJetType(primitiveType)); } primitiveTypesMap.put("void", JetStandardClasses.getUnitType()); } return primitiveTypesMap; }
private JetType getPrimitiveType(char descriptor, boolean nullable) { if (!nullable) { for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) { if (jvmPrimitiveType.getJvmLetter() == descriptor) { return jetStandardLibrary.getPrimitiveJetType(jvmPrimitiveType.getPrimitiveType()); } } if (descriptor == 'V') { return JetStandardClasses.getUnitType(); } } else { for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) { if (jvmPrimitiveType.getJvmLetter() == descriptor) { return jetStandardLibrary.getNullablePrimitiveJetType( jvmPrimitiveType.getPrimitiveType()); } } if (descriptor == 'V') { throw new IllegalStateException("incorrect signature: nullable void"); } } throw new IllegalStateException("incorrect signature"); }
@Override public JetType visitFunctionLiteralExpression( JetFunctionLiteralExpression expression, ExpressionTypingContext context) { JetFunctionLiteral functionLiteral = expression.getFunctionLiteral(); JetBlockExpression bodyExpression = functionLiteral.getBodyExpression(); if (bodyExpression == null) return null; JetType expectedType = context.expectedType; boolean functionTypeExpected = expectedType != TypeUtils.NO_EXPECTED_TYPE && JetStandardClasses.isFunctionType(expectedType); NamedFunctionDescriptorImpl functionDescriptor = createFunctionDescriptor(expression, context, functionTypeExpected); List<JetType> parameterTypes = Lists.newArrayList(); List<ValueParameterDescriptor> valueParameters = functionDescriptor.getValueParameters(); for (ValueParameterDescriptor valueParameter : valueParameters) { parameterTypes.add(valueParameter.getOutType()); } ReceiverDescriptor receiverParameter = functionDescriptor.getReceiverParameter(); JetType receiver = receiverParameter != NO_RECEIVER ? receiverParameter.getType() : null; JetType returnType = TypeUtils.NO_EXPECTED_TYPE; JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope( context.scope, functionDescriptor, context.trace); JetTypeReference returnTypeRef = functionLiteral.getReturnTypeRef(); if (returnTypeRef != null) { returnType = context.getTypeResolver().resolveType(context.scope, returnTypeRef); context .getServices() .checkFunctionReturnType( expression, context .replaceScope(functionInnerScope) .replaceExpectedType(returnType) .replaceExpectedReturnType(returnType) .replaceDataFlowInfo(context.dataFlowInfo)); } else { if (functionTypeExpected) { returnType = JetStandardClasses.getReturnTypeFromFunctionType(expectedType); } returnType = context .getServices() .getBlockReturnedType( functionInnerScope, bodyExpression, CoercionStrategy.COERCION_TO_UNIT, context.replaceExpectedType(returnType).replaceExpectedReturnType(returnType)); } JetType safeReturnType = returnType == null ? ErrorUtils.createErrorType("<return type>") : returnType; functionDescriptor.setReturnType(safeReturnType); boolean hasDeclaredValueParameters = functionLiteral.getValueParameterList() != null; if (!hasDeclaredValueParameters && functionTypeExpected) { JetType expectedReturnType = JetStandardClasses.getReturnTypeFromFunctionType(expectedType); if (JetStandardClasses.isUnit(expectedReturnType)) { functionDescriptor.setReturnType(JetStandardClasses.getUnitType()); return DataFlowUtils.checkType( JetStandardClasses.getFunctionType( Collections.<AnnotationDescriptor>emptyList(), receiver, parameterTypes, JetStandardClasses.getUnitType()), expression, context); } } return DataFlowUtils.checkType( JetStandardClasses.getFunctionType( Collections.<AnnotationDescriptor>emptyList(), receiver, parameterTypes, safeReturnType), expression, context); }