Move static function type utilities from KotlinBuiltIns to functionTypes.kt

This commit is contained in:
Alexander Udalov
2016-03-14 13:12:04 +03:00
parent 569a5888ff
commit fd344561fc
39 changed files with 248 additions and 224 deletions
@@ -24,6 +24,7 @@ import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.FunctionTypesKt;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor;
@@ -443,7 +444,7 @@ public class BodyResolver {
if (classDescriptor != null) {
if (ErrorUtils.isError(classDescriptor)) continue;
if (KotlinBuiltIns.isExactExtensionFunctionType(supertype)) {
if (FunctionTypesKt.isExactExtensionFunctionType(supertype)) {
trace.report(SUPERTYPE_IS_EXTENSION_FUNCTION_TYPE.on(typeReference));
}
@@ -27,6 +27,7 @@ import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.FunctionTypesKt;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter;
@@ -611,7 +612,7 @@ public class DescriptorResolver {
if (DynamicTypesKt.isDynamic(upperBoundType)) {
trace.report(DYNAMIC_UPPER_BOUND.on(upperBound));
}
if (KotlinBuiltIns.isExactExtensionFunctionType(upperBoundType)) {
if (FunctionTypesKt.isExactExtensionFunctionType(upperBoundType)) {
trace.report(UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE.on(upperBound));
}
}
@@ -17,6 +17,9 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType
import org.jetbrains.kotlin.builtins.getValueParametersFromFunctionType
import org.jetbrains.kotlin.builtins.isFunctionOrExtensionFunctionType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl
@@ -217,12 +220,12 @@ class FunctionDescriptorResolver(
)
}
private fun KotlinType.functionTypeExpected() = !TypeUtils.noExpectedType(this) && KotlinBuiltIns.isFunctionOrExtensionFunctionType(this)
private fun KotlinType.functionTypeExpected() = !TypeUtils.noExpectedType(this) && isFunctionOrExtensionFunctionType
private fun KotlinType.getReceiverType(): KotlinType? =
if (functionTypeExpected()) KotlinBuiltIns.getReceiverType(this) else null
if (functionTypeExpected()) getReceiverTypeFromFunctionType(this) else null
private fun KotlinType.getValueParameters(owner: FunctionDescriptor): List<ValueParameterDescriptor>? =
if (functionTypeExpected()) KotlinBuiltIns.getValueParameters(owner, this) else null
if (functionTypeExpected()) getValueParametersFromFunctionType(owner, this) else null
fun resolvePrimaryConstructorDescriptor(
scope: LexicalScope,
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isFunctionOrExtensionFunctionType
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors
@@ -27,16 +27,14 @@ import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtParameter
object InlineParameterChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration,
descriptor: DeclarationDescriptor,
diagnosticHolder: DiagnosticSink,
bindingContext: BindingContext
override fun check(
declaration: KtDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink, bindingContext: BindingContext
) {
if (declaration is KtFunction) {
val inline = declaration.hasModifier(KtTokens.INLINE_KEYWORD)
for (parameter in declaration.valueParameters) {
val parameterDescriptor = bindingContext.get(BindingContext.VALUE_PARAMETER, parameter)
if (!inline || (parameterDescriptor != null && !KotlinBuiltIns.isFunctionOrExtensionFunctionType(parameterDescriptor.type))) {
if (!inline || (parameterDescriptor != null && !parameterDescriptor.type.isFunctionOrExtensionFunctionType)) {
parameter.reportIncorrectInline(KtTokens.NOINLINE_KEYWORD, diagnosticHolder)
parameter.reportIncorrectInline(KtTokens.CROSSINLINE_KEYWORD, diagnosticHolder)
}
@@ -50,4 +48,4 @@ object InlineParameterChecker : DeclarationChecker {
diagnosticHolder.report(Errors.ILLEGAL_INLINE_PARAMETER_MODIFIER.on(modifier, modifierToken))
}
}
}
}
@@ -16,8 +16,7 @@
package org.jetbrains.kotlin.resolve.callableReferences
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.builtins.*
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor
@@ -211,11 +210,11 @@ private fun bindFunctionReference(expression: KtCallableReferenceExpression, typ
)
functionDescriptor.initialize(
KotlinBuiltIns.getReceiverType(type),
getReceiverTypeFromFunctionType(type),
null,
emptyList(),
KotlinBuiltIns.getValueParameters(functionDescriptor, type),
KotlinBuiltIns.getReturnTypeFromFunctionType(type),
getValueParametersFromFunctionType(functionDescriptor, type),
getReturnTypeFromFunctionType(type),
Modality.FINAL,
Visibilities.PUBLIC
)
@@ -18,8 +18,8 @@ package org.jetbrains.kotlin.resolve.calls
import com.google.common.collect.Lists
import com.google.common.collect.Sets
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.builtins.isExactExtensionFunctionType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.SUPER_CANT_BE_EXTENSION_RECEIVER
@@ -276,12 +276,11 @@ class CandidateResolver(
private fun CallCandidateResolutionContext<*>.checkNonExtensionCalledWithReceiver() = checkAndReport {
val call = candidateCall.call
if (call is CallTransformer.CallForImplicitInvoke && candidateCall.extensionReceiver != null
&& candidateCall.dispatchReceiver != null
if (call is CallTransformer.CallForImplicitInvoke &&
candidateCall.extensionReceiver != null &&
candidateCall.dispatchReceiver != null
) {
if (call.dispatchReceiver == candidateCall.dispatchReceiver
&& !KotlinBuiltIns.isExactExtensionFunctionType(call.dispatchReceiver.type)
) {
if (call.dispatchReceiver == candidateCall.dispatchReceiver && !call.dispatchReceiver.type.isExactExtensionFunctionType) {
tracing.nonExtensionFunctionCalledAsExtension(trace)
return@checkAndReport OTHER_ERROR
}
@@ -16,8 +16,8 @@
package org.jetbrains.kotlin.resolve.calls
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.builtins.isFunctionOrExtensionFunctionType
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
@@ -238,11 +238,10 @@ class GenericCandidateResolver(private val argumentTypeResolver: ArgumentTypeRes
) = if (!TypeUtils.noExpectedType(context.expectedType) &&
ownerReturnType != null &&
TypeUtils.isTypeParameter(ownerReturnType) &&
KotlinBuiltIns.isFunctionOrExtensionFunctionType(literalExpectedType) &&
literalExpectedType.isFunctionOrExtensionFunctionType &&
getReturnTypeForCallable(literalExpectedType) == ownerReturnType)
context.expectedType
else DONT_CARE
context.expectedType
else DONT_CARE
private fun <D : CallableDescriptor> addConstraintForFunctionLiteralArgument(
functionLiteral: KtFunction,
@@ -259,8 +258,7 @@ class GenericCandidateResolver(private val argumentTypeResolver: ArgumentTypeRes
if (expectedType == null || TypeUtils.isDontCarePlaceholder(expectedType)) {
expectedType = argumentTypeResolver.getShapeTypeOfFunctionLiteral(functionLiteral, context.scope, context.trace, false)
}
if (expectedType == null || !KotlinBuiltIns.isFunctionOrExtensionFunctionType(expectedType) ||
hasUnknownFunctionParameter(expectedType)) {
if (expectedType == null || !expectedType.isFunctionOrExtensionFunctionType || hasUnknownFunctionParameter(expectedType)) {
return
}
val dataFlowInfoForArguments = context.candidateCall.dataFlowInfoForArguments
@@ -330,7 +328,7 @@ class GenericCandidateResolver(private val argumentTypeResolver: ArgumentTypeRes
return substitutedType
val shapeType = argumentTypeResolver.getShapeTypeOfCallableReference(callableReference, context, false)
if (shapeType != null && KotlinBuiltIns.isFunctionOrExtensionFunctionType(shapeType) && !hasUnknownFunctionParameter(shapeType))
if (shapeType != null && shapeType.isFunctionOrExtensionFunctionType && !hasUnknownFunctionParameter(shapeType))
return shapeType
return null
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.resolve.calls.checkers;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.builtins.FunctionTypesKt;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.lexer.KtToken;
@@ -232,9 +232,10 @@ class InlineChecker implements CallChecker {
}
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
boolean isInvoke = descriptor.getName().equals(OperatorNameConventions.INVOKE) &&
containingDeclaration instanceof ClassDescriptor &&
KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(((ClassDescriptor) containingDeclaration).getDefaultType());
boolean isInvoke =
descriptor.getName().equals(OperatorNameConventions.INVOKE) &&
containingDeclaration instanceof ClassDescriptor &&
FunctionTypesKt.isExactFunctionOrExtensionFunctionType(((ClassDescriptor) containingDeclaration).getDefaultType());
return isInvoke || InlineUtil.isInline(descriptor);
}
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.resolve.calls.checkers
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isExactExtensionFunctionType
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
@@ -29,7 +29,8 @@ class InvokeConventionChecker : CallChecker {
if (resolvedCall is VariableAsFunctionResolvedCallImpl) {
val functionCall = resolvedCall.functionCall
val variableCall = resolvedCall.variableCall
if (functionCall.dispatchReceiver != null && functionCall.extensionReceiver != null && KotlinBuiltIns.isExactExtensionFunctionType(variableCall.resultingDescriptor.type)) {
if (functionCall.dispatchReceiver != null && functionCall.extensionReceiver != null &&
variableCall.resultingDescriptor.type.isExactExtensionFunctionType) {
if (variableCall.dispatchReceiver is ExpressionReceiver || variableCall.extensionReceiver is ExpressionReceiver) {
val callElement = variableCall.call.callElement
context.trace.report(Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.on(callElement))
@@ -37,5 +38,4 @@ class InvokeConventionChecker : CallChecker {
}
}
}
}
}
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.resolve.calls.inference
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderImpl.ConstraintKind.EQUAL
@@ -419,7 +419,7 @@ internal fun createTypeForFunctionPlaceholder(
val functionPlaceholderTypeConstructor = functionPlaceholder.constructor as FunctionPlaceholderTypeConstructor
val isExtension = KotlinBuiltIns.isExtensionFunctionType(expectedType)
val isExtension = expectedType.isExtensionFunctionType
val newArgumentTypes = if (!functionPlaceholderTypeConstructor.hasDeclaredArguments) {
val typeParamSize = expectedType.constructor.parameters.size
// the first parameter is receiver (if present), the last one is return type,
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.resolve.calls.tasks;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.builtins.FunctionTypesKt;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.psi.Call;
import org.jetbrains.kotlin.psi.KtExpression;
@@ -87,7 +87,7 @@ public class TracingStrategyForInvoke extends AbstractTracingStrategy {
}
private void functionExpectedOrNoReceiverAllowed(BindingTrace trace) {
if (KotlinBuiltIns.isFunctionType(calleeType)) {
if (FunctionTypesKt.isFunctionType(calleeType)) {
trace.report(NO_RECEIVER_ALLOWED.on(reference));
}
else {
@@ -20,6 +20,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.FunctionTypesKt;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.psi.*;
@@ -35,7 +36,7 @@ public class InlineUtil {
public static boolean isInlineLambdaParameter(@NotNull ParameterDescriptor valueParameterOrReceiver) {
return !(valueParameterOrReceiver instanceof ValueParameterDescriptor
&& ((ValueParameterDescriptor) valueParameterOrReceiver).isNoinline()) &&
KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(valueParameterOrReceiver.getOriginal().getType());
FunctionTypesKt.isExactFunctionOrExtensionFunctionType(valueParameterOrReceiver.getOriginal().getType());
}
public static boolean isInline(@Nullable DeclarationDescriptor descriptor) {
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.types.expressions
import com.google.common.collect.Lists
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
import org.jetbrains.kotlin.builtins.isFunctionOrExtensionFunctionType
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
@@ -142,7 +144,7 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
if (!expression.functionLiteral.hasBody()) return null
val expectedType = context.expectedType
val functionTypeExpected = !noExpectedType(expectedType) && KotlinBuiltIns.isFunctionOrExtensionFunctionType(expectedType)
val functionTypeExpected = !noExpectedType(expectedType) && expectedType.isFunctionOrExtensionFunctionType
val functionDescriptor = createFunctionLiteralDescriptor(expression, context)
expression.valueParameters.forEach {
@@ -191,7 +193,7 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
functionDescriptor: SimpleFunctionDescriptorImpl,
functionTypeExpected: Boolean
): KotlinType {
val expectedReturnType = if (functionTypeExpected) KotlinBuiltIns.getReturnTypeFromFunctionType(context.expectedType) else null
val expectedReturnType = if (functionTypeExpected) getReturnTypeFromFunctionType(context.expectedType) else null
val returnType = computeUnsafeReturnType(expression, context, functionDescriptor, expectedReturnType);
if (!expression.functionLiteral.hasDeclaredReturnType() && functionTypeExpected) {
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.resolve.calls.tower
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isExactExtensionFunctionType
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tasks.createSynthesizedInvokes
@@ -141,7 +141,7 @@ private class InvokeExtensionScopeTowerProcessor<C>(
private fun ScopeTower.getExtensionInvokeCandidateDescriptor(
extensionFunctionReceiver: ReceiverValue
): CandidateWithBoundDispatchReceiver<FunctionDescriptor>? {
if (!KotlinBuiltIns.isExactExtensionFunctionType(extensionFunctionReceiver.type)) return null
if (!extensionFunctionReceiver.type.isExactExtensionFunctionType) return null
val invokeDescriptor = extensionFunctionReceiver.type.memberScope.getContributedFunctions(OperatorNameConventions.INVOKE, location).single()
val synthesizedInvoke = createSynthesizedInvokes(listOf(invokeDescriptor)).single()