[FIR] Consistently use _function_ instead of _functional_ in names of classes and functions
This commit is contained in:
committed by
Space Team
parent
f268ab8858
commit
89c42e20c9
+2
-2
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.types.functionalTypeService
|
||||
import org.jetbrains.kotlin.fir.types.functionTypeService
|
||||
import org.jetbrains.kotlin.name.CallableId
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
@@ -116,7 +116,7 @@ class FirCachingCompositeSymbolProvider(
|
||||
}
|
||||
|
||||
private fun isNameForFunctionClass(classId: ClassId): Boolean {
|
||||
return session.functionalTypeService.getKindByClassNamePrefix(classId.packageFqName, classId.shortClassName.asString()) != null
|
||||
return session.functionTypeService.getKindByClassNamePrefix(classId.packageFqName, classId.shortClassName.asString()) != null
|
||||
}
|
||||
|
||||
@OptIn(FirSymbolProviderInternals::class)
|
||||
|
||||
+24
-24
@@ -5,9 +5,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.resolve.providers.impl
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionalTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.isBuiltin
|
||||
import org.jetbrains.kotlin.builtins.functions.isSuspendType
|
||||
import org.jetbrains.kotlin.builtins.functions.isSuspendOrKSuspendFunction
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
@@ -39,14 +39,14 @@ import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
|
||||
/*
|
||||
* Provides functional interfaces for functional kinds from compiler plugins
|
||||
* Provides function interfaces for function kinds from compiler plugins
|
||||
*/
|
||||
class FirExtensionSyntheticFunctionalInterfaceProvider(
|
||||
class FirExtensionSyntheticFunctionInterfaceProvider(
|
||||
session: FirSession,
|
||||
moduleData: FirModuleData,
|
||||
kotlinScopeProvider: FirKotlinScopeProvider
|
||||
) : FirSyntheticFunctionalInterfaceProviderBase(session, moduleData, kotlinScopeProvider) {
|
||||
override fun FunctionalTypeKind.isAcceptable(): Boolean {
|
||||
) : FirSyntheticFunctionInterfaceProviderBase(session, moduleData, kotlinScopeProvider) {
|
||||
override fun FunctionTypeKind.isAcceptable(): Boolean {
|
||||
return !this.isBuiltin
|
||||
}
|
||||
}
|
||||
@@ -54,17 +54,17 @@ class FirExtensionSyntheticFunctionalInterfaceProvider(
|
||||
/*
|
||||
* Provides kotlin.FunctionN, kotlin.coroutines.SuspendFunctionN, kotlin.reflect.KFunctionN and kotlin.reflect.KSuspendFunctionN
|
||||
*/
|
||||
class FirBuiltinSyntheticFunctionalInterfaceProvider(
|
||||
class FirBuiltinSyntheticFunctionInterfaceProvider(
|
||||
session: FirSession,
|
||||
moduleData: FirModuleData,
|
||||
kotlinScopeProvider: FirKotlinScopeProvider
|
||||
) : FirSyntheticFunctionalInterfaceProviderBase(session, moduleData, kotlinScopeProvider) {
|
||||
override fun FunctionalTypeKind.isAcceptable(): Boolean {
|
||||
) : FirSyntheticFunctionInterfaceProviderBase(session, moduleData, kotlinScopeProvider) {
|
||||
override fun FunctionTypeKind.isAcceptable(): Boolean {
|
||||
return this.isBuiltin
|
||||
}
|
||||
}
|
||||
|
||||
abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
abstract class FirSyntheticFunctionInterfaceProviderBase(
|
||||
session: FirSession,
|
||||
val moduleData: FirModuleData,
|
||||
val kotlinScopeProvider: FirKotlinScopeProvider
|
||||
@@ -83,7 +83,7 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
override fun getTopLevelPropertySymbolsTo(destination: MutableList<FirPropertySymbol>, packageFqName: FqName, name: Name) {}
|
||||
|
||||
override fun getPackage(fqName: FqName): FqName? {
|
||||
return fqName.takeIf { session.functionalTypeService.hasKindWithSpecificPackage(it) }
|
||||
return fqName.takeIf { session.functionTypeService.hasKindWithSpecificPackage(it) }
|
||||
}
|
||||
|
||||
override fun computePackageSetWithTopLevelCallables(): Set<String> {
|
||||
@@ -91,7 +91,7 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
}
|
||||
|
||||
/**
|
||||
* This method has no sence for synthetic functional interfaces
|
||||
* This method has no sense for synthetic function interfaces
|
||||
*/
|
||||
override fun knownTopLevelClassifiersInPackage(packageFqName: FqName): Set<String>? {
|
||||
return emptySet()
|
||||
@@ -101,20 +101,20 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
return emptySet()
|
||||
}
|
||||
|
||||
private val cache = moduleData.session.firCachesFactory.createCache(::createSyntheticFunctionalInterface)
|
||||
private val cache = moduleData.session.firCachesFactory.createCache(::createSyntheticFunctionInterface)
|
||||
|
||||
protected abstract fun FunctionalTypeKind.isAcceptable(): Boolean
|
||||
protected abstract fun FunctionTypeKind.isAcceptable(): Boolean
|
||||
|
||||
private fun createSyntheticFunctionalInterface(classId: ClassId): FirRegularClassSymbol? {
|
||||
private fun createSyntheticFunctionInterface(classId: ClassId): FirRegularClassSymbol? {
|
||||
return with(classId) {
|
||||
val className = relativeClassName.asString()
|
||||
val kind = session.functionalTypeService.getKindByClassNamePrefix(packageFqName, className) ?: return null
|
||||
val kind = session.functionTypeService.getKindByClassNamePrefix(packageFqName, className) ?: return null
|
||||
if (!kind.isAcceptable()) return null
|
||||
val prefix = kind.classNamePrefix
|
||||
val arity = className.substring(prefix.length).toIntOrNull() ?: return null
|
||||
FirRegularClassSymbol(classId).apply symbol@{
|
||||
buildRegularClass klass@{
|
||||
moduleData = this@FirSyntheticFunctionalInterfaceProviderBase.moduleData
|
||||
moduleData = this@FirSyntheticFunctionInterfaceProviderBase.moduleData
|
||||
origin = FirDeclarationOrigin.BuiltIns
|
||||
name = relativeClassName.shortName()
|
||||
status = FirResolvedDeclarationStatusImpl(
|
||||
@@ -136,7 +136,7 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
typeParameters.addAll(
|
||||
(1..arity).map {
|
||||
buildTypeParameter {
|
||||
moduleData = this@FirSyntheticFunctionalInterfaceProviderBase.moduleData
|
||||
moduleData = this@FirSyntheticFunctionInterfaceProviderBase.moduleData
|
||||
resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES
|
||||
origin = FirDeclarationOrigin.BuiltIns
|
||||
name = Name.identifier("P$it")
|
||||
@@ -150,7 +150,7 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
)
|
||||
typeParameters.add(
|
||||
buildTypeParameter {
|
||||
moduleData = this@FirSyntheticFunctionalInterfaceProviderBase.moduleData
|
||||
moduleData = this@FirSyntheticFunctionInterfaceProviderBase.moduleData
|
||||
resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES
|
||||
origin = FirDeclarationOrigin.BuiltIns
|
||||
name = Name.identifier("R")
|
||||
@@ -175,13 +175,13 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
isInline = false
|
||||
isTailRec = false
|
||||
isExternal = false
|
||||
isSuspend = kind.isSuspendType
|
||||
isSuspend = kind.isSuspendOrKSuspendFunction
|
||||
}
|
||||
val typeArguments = typeParameters.map {
|
||||
ConeTypeParameterTypeImpl(it.symbol.toLookupTag(), false).toFirResolvedTypeRef()
|
||||
}
|
||||
|
||||
fun createSuperType(kind: FunctionalTypeKind): FirResolvedTypeRef {
|
||||
fun createSuperType(kind: FunctionTypeKind): FirResolvedTypeRef {
|
||||
return kind.classId(arity).toLookupTag()
|
||||
.constructClassType(typeArguments.map { it.type }.toTypedArray(), isNullable = false)
|
||||
.toFirResolvedTypeRef()
|
||||
@@ -200,7 +200,7 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
|
||||
addDeclaration(
|
||||
buildSimpleFunction {
|
||||
moduleData = this@FirSyntheticFunctionalInterfaceProviderBase.moduleData
|
||||
moduleData = this@FirSyntheticFunctionInterfaceProviderBase.moduleData
|
||||
resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES
|
||||
origin = FirDeclarationOrigin.BuiltIns
|
||||
returnTypeRef = typeArguments.last()
|
||||
@@ -213,7 +213,7 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
valueParameters += typeArguments.dropLast(1).mapIndexed { index, typeArgument ->
|
||||
val parameterName = Name.identifier("p${index + 1}")
|
||||
buildValueParameter {
|
||||
moduleData = this@FirSyntheticFunctionalInterfaceProviderBase.moduleData
|
||||
moduleData = this@FirSyntheticFunctionInterfaceProviderBase.moduleData
|
||||
containingFunctionSymbol = this@buildSimpleFunction.symbol
|
||||
origin = FirDeclarationOrigin.BuiltIns
|
||||
resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES
|
||||
@@ -242,5 +242,5 @@ abstract class FirSyntheticFunctionalInterfaceProviderBase(
|
||||
}
|
||||
}
|
||||
|
||||
private fun FunctionalTypeKind.classId(arity: Int) = ClassId(packageFqName, numberedClassName(arity))
|
||||
private fun FunctionTypeKind.classId(arity: Int) = ClassId(packageFqName, numberedClassName(arity))
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.types
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionalTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.modality
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeIntermediateDiagnostic
|
||||
@@ -67,7 +67,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
): SimpleTypeMarker {
|
||||
val attributesList = attributes?.filterIsInstanceTo<ConeAttribute<*>, MutableList<ConeAttribute<*>>>(mutableListOf())
|
||||
val coneAttributes: ConeAttributes = if (isExtensionFunction) {
|
||||
require(constructor is ConeClassLikeLookupTag && constructor.isSomeFunctionalType(session))
|
||||
require(constructor is ConeClassLikeLookupTag && constructor.isSomeFunctionType(session))
|
||||
// We don't want to create new instance of ConeAttributes which
|
||||
// contains only CompilerConeAttributes.ExtensionFunctionType
|
||||
// to avoid memory consumption
|
||||
@@ -224,10 +224,10 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
return this.typeConstructor().isUnitTypeConstructor() && !this.isNullable
|
||||
}
|
||||
|
||||
override fun KotlinTypeMarker.isBuiltinFunctionalTypeOrSubtype(): Boolean {
|
||||
override fun KotlinTypeMarker.isBuiltinFunctionTypeOrSubtype(): Boolean {
|
||||
require(this is ConeKotlinType)
|
||||
return this.isTypeOrSubtypeOf {
|
||||
(it.lowerBoundIfFlexible() as ConeKotlinType).isSomeFunctionalType(session)
|
||||
(it.lowerBoundIfFlexible() as ConeKotlinType).isSomeFunctionType(session)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -464,7 +464,7 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
|
||||
override fun KotlinTypeMarker.isFunctionOrKFunctionWithAnySuspendability(): Boolean {
|
||||
require(this is ConeKotlinType)
|
||||
return this.isSomeFunctionalType(session)
|
||||
return this.isSomeFunctionType(session)
|
||||
}
|
||||
|
||||
fun ConeKotlinType.isTypeOrSubtypeOf(predicate: (ConeKotlinType) -> Boolean): Boolean {
|
||||
@@ -495,34 +495,35 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
return (this.lowerBoundIfFlexible() as? ConeKotlinType)?.isExtensionFunctionType(session) == true
|
||||
}
|
||||
|
||||
override fun KotlinTypeMarker.extractArgumentsForFunctionalTypeOrSubtype(): List<KotlinTypeMarker> {
|
||||
val builtInFunctionalType = getFunctionalTypeFromSupertypes() as ConeKotlinType
|
||||
override fun KotlinTypeMarker.extractArgumentsForFunctionTypeOrSubtype(): List<KotlinTypeMarker> {
|
||||
val builtInFunctionType = getFunctionTypeFromSupertypes() as ConeKotlinType
|
||||
return buildList {
|
||||
// excluding return type
|
||||
for (index in 0 until builtInFunctionalType.argumentsCount() - 1) {
|
||||
add(builtInFunctionalType.getArgument(index).getType())
|
||||
for (index in 0 until builtInFunctionType.argumentsCount() - 1) {
|
||||
add(builtInFunctionType.getArgument(index).getType())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun KotlinTypeMarker.getFunctionalTypeFromSupertypes(): KotlinTypeMarker {
|
||||
override fun KotlinTypeMarker.getFunctionTypeFromSupertypes(): KotlinTypeMarker {
|
||||
require(this is ConeKotlinType)
|
||||
assert(this.isBuiltinFunctionalTypeOrSubtype()) {
|
||||
assert(this.isBuiltinFunctionTypeOrSubtype()) {
|
||||
"Not a function type or subtype: ${this.renderForDebugging()}"
|
||||
}
|
||||
|
||||
return fullyExpandedType(session).let {
|
||||
val simpleType = it.lowerBoundIfFlexible()
|
||||
if ((simpleType as ConeKotlinType).isSomeFunctionalType(session))
|
||||
if ((simpleType as ConeKotlinType).isSomeFunctionType(session))
|
||||
this
|
||||
else {
|
||||
var functionalSupertype: KotlinTypeMarker? = null
|
||||
simpleType.anySuperTypeConstructor { type ->
|
||||
simpleType.fastCorrespondingSupertypes(type.typeConstructor())?.any { superType ->
|
||||
val isFunctional = (superType as ConeKotlinType).isSomeFunctionalType(session)
|
||||
if (isFunctional)
|
||||
val isFunction = (superType as ConeKotlinType).isSomeFunctionType(session)
|
||||
if (isFunction) {
|
||||
functionalSupertype = superType
|
||||
isFunctional
|
||||
}
|
||||
isFunction
|
||||
} ?: false
|
||||
}
|
||||
functionalSupertype ?: error("Failed to find functional supertype for $simpleType")
|
||||
@@ -530,16 +531,16 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo
|
||||
}
|
||||
}
|
||||
|
||||
override fun KotlinTypeMarker.functionalTypeKind(): FunctionalTypeKind? {
|
||||
override fun KotlinTypeMarker.functionTypeKind(): FunctionTypeKind? {
|
||||
require(this is ConeKotlinType)
|
||||
return this.functionalTypeKind(session)
|
||||
return this.functionTypeKind(session)
|
||||
}
|
||||
|
||||
override fun getNonReflectFunctionTypeConstructor(parametersNumber: Int, kind: FunctionalTypeKind): TypeConstructorMarker {
|
||||
override fun getNonReflectFunctionTypeConstructor(parametersNumber: Int, kind: FunctionTypeKind): TypeConstructorMarker {
|
||||
return kind.nonReflectKind().numberedClassId(parametersNumber).toLookupTag()
|
||||
}
|
||||
|
||||
override fun getReflectFunctionTypeConstructor(parametersNumber: Int, kind: FunctionalTypeKind): TypeConstructorMarker {
|
||||
override fun getReflectFunctionTypeConstructor(parametersNumber: Int, kind: FunctionTypeKind): TypeConstructorMarker {
|
||||
return kind.reflectKind().numberedClassId(parametersNumber).toLookupTag()
|
||||
}
|
||||
|
||||
|
||||
+21
-21
@@ -5,30 +5,30 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.types
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionalTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionalTypeKindExtractor
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKindExtractor
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassId
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isSuspend
|
||||
import org.jetbrains.kotlin.fir.extensions.FirFunctionalTypeKindExtension
|
||||
import org.jetbrains.kotlin.fir.extensions.FirFunctionTypeKindExtension
|
||||
import org.jetbrains.kotlin.fir.extensions.extensionService
|
||||
import org.jetbrains.kotlin.fir.extensions.functionalTypeKindExtensions
|
||||
import org.jetbrains.kotlin.fir.extensions.functionTypeKindExtensions
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirAnonymousFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
class FirFunctionalTypeKindServiceImpl(private val session: FirSession) : FirFunctionalTypeKindService() {
|
||||
private val nonReflectKindsFromExtensions = mutableListOf<FunctionalTypeKind>()
|
||||
class FirFunctionTypeKindServiceImpl(private val session: FirSession) : FirFunctionTypeKindService() {
|
||||
private val nonReflectKindsFromExtensions = mutableListOf<FunctionTypeKind>()
|
||||
|
||||
override val extractor: FunctionalTypeKindExtractor = run {
|
||||
override val extractor: FunctionTypeKindExtractor = run {
|
||||
val kinds = buildList {
|
||||
add(FunctionalTypeKind.Function)
|
||||
add(FunctionalTypeKind.SuspendFunction)
|
||||
add(FunctionalTypeKind.KFunction)
|
||||
add(FunctionalTypeKind.KSuspendFunction)
|
||||
add(FunctionTypeKind.Function)
|
||||
add(FunctionTypeKind.SuspendFunction)
|
||||
add(FunctionTypeKind.KFunction)
|
||||
add(FunctionTypeKind.KSuspendFunction)
|
||||
|
||||
val registrar = object : FirFunctionalTypeKindExtension.FunctionalTypeKindRegistrar {
|
||||
override fun registerKind(nonReflectKind: FunctionalTypeKind, reflectKind: FunctionalTypeKind) {
|
||||
val registrar = object : FirFunctionTypeKindExtension.FunctionTypeKindRegistrar {
|
||||
override fun registerKind(nonReflectKind: FunctionTypeKind, reflectKind: FunctionTypeKind) {
|
||||
require(nonReflectKind.reflectKind() == reflectKind)
|
||||
require(reflectKind.nonReflectKind() == nonReflectKind)
|
||||
add(nonReflectKind)
|
||||
@@ -37,7 +37,7 @@ class FirFunctionalTypeKindServiceImpl(private val session: FirSession) : FirFun
|
||||
}
|
||||
}
|
||||
|
||||
for (extension in session.extensionService.functionalTypeKindExtensions) {
|
||||
for (extension in session.extensionService.functionTypeKindExtensions) {
|
||||
with(extension) { registrar.registerKinds() }
|
||||
}
|
||||
}.also { kinds ->
|
||||
@@ -47,18 +47,18 @@ class FirFunctionalTypeKindServiceImpl(private val session: FirSession) : FirFun
|
||||
}
|
||||
}
|
||||
|
||||
FunctionalTypeKindExtractor(kinds)
|
||||
FunctionTypeKindExtractor(kinds)
|
||||
}
|
||||
|
||||
override fun extractSingleSpecialKindForFunction(functionSymbol: FirFunctionSymbol<*>): FunctionalTypeKind? {
|
||||
override fun extractSingleSpecialKindForFunction(functionSymbol: FirFunctionSymbol<*>): FunctionTypeKind? {
|
||||
if (nonReflectKindsFromExtensions.isEmpty()) {
|
||||
return FunctionalTypeKind.SuspendFunction.takeIf { functionSymbol.isSuspend }
|
||||
return FunctionTypeKind.SuspendFunction.takeIf { functionSymbol.isSuspend }
|
||||
}
|
||||
|
||||
return extractAllSpecialKindsForFunction(functionSymbol).singleOrNull()
|
||||
}
|
||||
|
||||
override fun extractAllSpecialKindsForFunction(functionSymbol: FirFunctionSymbol<*>): List<FunctionalTypeKind> {
|
||||
override fun extractAllSpecialKindsForFunction(functionSymbol: FirFunctionSymbol<*>): List<FunctionTypeKind> {
|
||||
return extractSpecialKindsImpl(
|
||||
functionSymbol,
|
||||
{ isSuspend },
|
||||
@@ -71,7 +71,7 @@ class FirFunctionalTypeKindServiceImpl(private val session: FirSession) : FirFun
|
||||
)
|
||||
}
|
||||
|
||||
override fun extractAllSpecialKindsForFunctionalTypeRef(typeRef: FirFunctionTypeRef): List<FunctionalTypeKind> {
|
||||
override fun extractAllSpecialKindsForFunctionTypeRef(typeRef: FirFunctionTypeRef): List<FunctionTypeKind> {
|
||||
return extractSpecialKindsImpl(typeRef, { isSuspend }, { annotations.mapNotNull { it.toAnnotationClassId(session) } })
|
||||
}
|
||||
|
||||
@@ -79,10 +79,10 @@ class FirFunctionalTypeKindServiceImpl(private val session: FirSession) : FirFun
|
||||
source: T,
|
||||
isSuspend: T.() -> Boolean,
|
||||
annotations: T.() -> List<ClassId>
|
||||
): List<FunctionalTypeKind> {
|
||||
): List<FunctionTypeKind> {
|
||||
return buildList {
|
||||
if (source.isSuspend()) {
|
||||
add(FunctionalTypeKind.SuspendFunction)
|
||||
add(FunctionTypeKind.SuspendFunction)
|
||||
}
|
||||
if (nonReflectKindsFromExtensions.isNotEmpty()) {
|
||||
for (annotationClassId in source.annotations()) {
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.types
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionalTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.isRegularFunction
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
|
||||
import org.jetbrains.kotlin.builtins.functions.isBasicFunctionOrKFunction
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.FirClass
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFunction
|
||||
@@ -28,85 +28,80 @@ import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.runUnless
|
||||
|
||||
// ---------------------------------------------- is type is a functional type ----------------------------------------------
|
||||
// ---------------------------------------------- is type is a function type ----------------------------------------------
|
||||
|
||||
fun ConeKotlinType.functionalTypeKind(session: FirSession): FunctionalTypeKind? {
|
||||
fun ConeKotlinType.functionTypeKind(session: FirSession): FunctionTypeKind? {
|
||||
if (this !is ConeClassLikeType) return null
|
||||
return fullyExpandedType(session).lookupTag.functionalTypeKind(session)
|
||||
return fullyExpandedType(session).lookupTag.functionTypeKind(session)
|
||||
}
|
||||
|
||||
private fun ConeClassLikeLookupTag.functionalTypeKind(session: FirSession): FunctionalTypeKind? {
|
||||
private fun ConeClassLikeLookupTag.functionTypeKind(session: FirSession): FunctionTypeKind? {
|
||||
val classId = classId
|
||||
return session.functionalTypeService.getKindByClassNamePrefix(classId.packageFqName, classId.shortClassName.asString())
|
||||
return session.functionTypeService.getKindByClassNamePrefix(classId.packageFqName, classId.shortClassName.asString())
|
||||
}
|
||||
|
||||
private inline fun ConeKotlinType.isFunctionalTypeWithPredicate(
|
||||
private inline fun ConeKotlinType.isFunctionTypeWithPredicate(
|
||||
session: FirSession,
|
||||
errorOnNotFunctionalType: Boolean = false,
|
||||
predicate: (FunctionalTypeKind) -> Boolean
|
||||
errorOnNotFunctionType: Boolean = false,
|
||||
predicate: (FunctionTypeKind) -> Boolean
|
||||
): Boolean {
|
||||
val kind = functionalTypeKind(session)
|
||||
?: if (errorOnNotFunctionalType) error("$this is not a functional type") else return false
|
||||
val kind = functionTypeKind(session)
|
||||
?: if (errorOnNotFunctionType) error("$this is not a function type") else return false
|
||||
return predicate(kind)
|
||||
}
|
||||
|
||||
// Function
|
||||
fun ConeKotlinType.isSimpleFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionalTypeWithPredicate(session) { it == FunctionalTypeKind.Function }
|
||||
}
|
||||
|
||||
// Function, SuspendFunction, [Custom]Function
|
||||
fun ConeKotlinType.isNonReflectFunctionalType(session: FirSession): Boolean {
|
||||
return isFunctionalTypeWithPredicate(session) { !it.isReflectType }
|
||||
fun ConeKotlinType.isBasicFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionTypeWithPredicate(session) { it == FunctionTypeKind.Function }
|
||||
}
|
||||
|
||||
// SuspendFunction, KSuspendFunction
|
||||
fun ConeKotlinType.isSuspendFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionalTypeWithPredicate(session) {
|
||||
it == FunctionalTypeKind.SuspendFunction || it == FunctionalTypeKind.KSuspendFunction
|
||||
fun ConeKotlinType.isSuspendOrKSuspendFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionTypeWithPredicate(session) {
|
||||
it == FunctionTypeKind.SuspendFunction || it == FunctionTypeKind.KSuspendFunction
|
||||
}
|
||||
}
|
||||
|
||||
// KFunction, KSuspendFunction, K[Custom]Function
|
||||
fun ConeKotlinType.isReflectFunctionalType(session: FirSession): Boolean {
|
||||
return isFunctionalTypeWithPredicate(session) { it.isReflectType }
|
||||
fun ConeKotlinType.isReflectFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionTypeWithPredicate(session) { it.isReflectType }
|
||||
}
|
||||
|
||||
// Function, SuspendFunction, [Custom]Function
|
||||
fun ConeKotlinType.isNonReflectFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionTypeWithPredicate(session) { !it.isReflectType }
|
||||
}
|
||||
|
||||
// Function, SuspendFunction, [Custom]Function, KFunction, KSuspendFunction, K[Custom]Function
|
||||
fun ConeKotlinType.isSomeFunctionalType(session: FirSession): Boolean {
|
||||
return functionalTypeKind(session) != null
|
||||
fun ConeKotlinType.isSomeFunctionType(session: FirSession): Boolean {
|
||||
return functionTypeKind(session) != null
|
||||
}
|
||||
|
||||
// Function, SuspendFunction, [Custom]Function, KFunction, KSuspendFunction, K[Custom]Function
|
||||
fun ConeClassLikeLookupTag.isSomeFunctionalType(session: FirSession): Boolean {
|
||||
return functionalTypeKind(session) != null
|
||||
}
|
||||
|
||||
// Function, KFunction
|
||||
private fun ConeKotlinType.isSimpleFunctionalType(session: FirSession, errorOnNotFunctionalType: Boolean): Boolean {
|
||||
return isFunctionalTypeWithPredicate(session, errorOnNotFunctionalType) { it.isRegularFunction }
|
||||
fun ConeClassLikeLookupTag.isSomeFunctionType(session: FirSession): Boolean {
|
||||
return functionTypeKind(session) != null
|
||||
}
|
||||
|
||||
// SuspendFunction, [Custom]Function, KSuspendFunction, K[Custom]Function
|
||||
fun ConeKotlinType.isNotSimpleFunctionalType(session: FirSession): Boolean {
|
||||
return isFunctionalTypeWithPredicate(session, errorOnNotFunctionalType = false) { !it.isRegularFunction }
|
||||
fun ConeKotlinType.isNotBasicFunctionType(session: FirSession): Boolean {
|
||||
return isFunctionTypeWithPredicate(session, errorOnNotFunctionType = false) { !it.isBasicFunctionOrKFunction }
|
||||
}
|
||||
|
||||
// ---------------------------------------------- functional type conversions ----------------------------------------------
|
||||
// ---------------------------------------------- function type conversions ----------------------------------------------
|
||||
|
||||
/*
|
||||
* SuspendFunction/[Custom]Function -> Function
|
||||
* KSuspendFunction/K[Custom]Function -> KFunction
|
||||
*/
|
||||
fun ConeKotlinType.customFunctionalTypeToSimpleFunctionalType(session: FirSession): ConeClassLikeType {
|
||||
val kind = functionalTypeKind(session)
|
||||
require(kind != null && kind != FunctionalTypeKind.Function && kind != FunctionalTypeKind.KFunction)
|
||||
fun ConeKotlinType.customFunctionTypeToSimpleFunctionType(session: FirSession): ConeClassLikeType {
|
||||
val kind = functionTypeKind(session)
|
||||
require(kind != null && kind != FunctionTypeKind.Function && kind != FunctionTypeKind.KFunction)
|
||||
val newKind = if (kind.isReflectType) {
|
||||
FunctionalTypeKind.KFunction
|
||||
FunctionTypeKind.KFunction
|
||||
} else {
|
||||
FunctionalTypeKind.Function
|
||||
FunctionTypeKind.Function
|
||||
}
|
||||
return createFunctionalTypeWithNewKind(session, newKind)
|
||||
return createFunctionTypeWithNewKind(session, newKind)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -114,68 +109,73 @@ fun ConeKotlinType.customFunctionalTypeToSimpleFunctionalType(session: FirSessio
|
||||
* KSuspendFunction -> SuspendFunction
|
||||
* K[Custom]Function -> [Custom]Function
|
||||
*/
|
||||
fun ConeKotlinType.reflectFunctionalTypeToNonReflectFunctionalType(session: FirSession): ConeClassLikeType {
|
||||
val kind = functionalTypeKind(session)
|
||||
fun ConeKotlinType.reflectFunctionTypeToNonReflectFunctionType(session: FirSession): ConeClassLikeType {
|
||||
val kind = functionTypeKind(session)
|
||||
require(kind != null && kind.isReflectType)
|
||||
return createFunctionalTypeWithNewKind(session, kind.nonReflectKind())
|
||||
return createFunctionTypeWithNewKind(session, kind.nonReflectKind())
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.createFunctionalTypeWithNewKind(session: FirSession, kind: FunctionalTypeKind): ConeClassLikeType {
|
||||
private fun ConeKotlinType.createFunctionTypeWithNewKind(session: FirSession, kind: FunctionTypeKind): ConeClassLikeType {
|
||||
val expandedType = fullyExpandedType(session)
|
||||
val functionalTypeId = ClassId(kind.packageFqName, kind.numberedClassName(expandedType.typeArguments.size - 1))
|
||||
return functionalTypeId.toLookupTag().constructClassType(
|
||||
val functionTypeId = ClassId(kind.packageFqName, kind.numberedClassName(expandedType.typeArguments.size - 1))
|
||||
return functionTypeId.toLookupTag().constructClassType(
|
||||
expandedType.typeArguments,
|
||||
isNullable = false,
|
||||
attributes = expandedType.attributes
|
||||
)
|
||||
}
|
||||
|
||||
// ---------------------------------------------- functional type subtyping ----------------------------------------------
|
||||
// ---------------------------------------------- function type subtyping ----------------------------------------------
|
||||
|
||||
// expectedFunctionalType is kotlin.FunctionN or kotlin.reflect.KFunctionN
|
||||
fun ConeKotlinType.findSubtypeOfSimpleFunctionalType(session: FirSession, expectedFunctionalType: ConeClassLikeType): ConeKotlinType? {
|
||||
require(expectedFunctionalType.isSimpleFunctionalType(session, errorOnNotFunctionalType = true))
|
||||
return findSubtypeOfSimpleFunctionalTypeImpl(session, expectedFunctionalType)
|
||||
// expectedfunctionType is kotlin.FunctionN or kotlin.reflect.KFunctionN
|
||||
fun ConeKotlinType.findSubtypeOfBasicFunctionType(session: FirSession, expectedFunctionType: ConeClassLikeType): ConeKotlinType? {
|
||||
require(expectedFunctionType.isFunctionOrKFunctionType(session, errorOnNotFunctionType = true))
|
||||
return findSubtypeOfBasicFunctionTypeImpl(session, expectedFunctionType)
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.findSubtypeOfSimpleFunctionalTypeImpl(
|
||||
// Function, KFunction
|
||||
private fun ConeKotlinType.isFunctionOrKFunctionType(session: FirSession, errorOnNotFunctionType: Boolean): Boolean {
|
||||
return isFunctionTypeWithPredicate(session, errorOnNotFunctionType) { it.isBasicFunctionOrKFunction }
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.findSubtypeOfBasicFunctionTypeImpl(
|
||||
session: FirSession,
|
||||
expectedFunctionalType: ConeClassLikeType
|
||||
expectedFunctionType: ConeClassLikeType
|
||||
): ConeKotlinType? {
|
||||
return when (this) {
|
||||
is ConeClassLikeType -> {
|
||||
when {
|
||||
// Expect the argument type is a simple functional type.
|
||||
isNotSimpleFunctionalType(session) -> null
|
||||
isSubtypeOfFunctionalType(session, expectedFunctionalType) -> this
|
||||
// Expect the argument type is a simple function type.
|
||||
isNotBasicFunctionType(session) -> null
|
||||
isSubtypeOfFunctionType(session, expectedFunctionType) -> this
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
is ConeIntersectionType -> {
|
||||
runUnless(intersectedTypes.any { it.isNotSimpleFunctionalType(session) }) {
|
||||
intersectedTypes.find { it.findSubtypeOfSimpleFunctionalTypeImpl(session, expectedFunctionalType) != null }
|
||||
runUnless(intersectedTypes.any { it.isNotBasicFunctionType(session) }) {
|
||||
intersectedTypes.find { it.findSubtypeOfBasicFunctionTypeImpl(session, expectedFunctionType) != null }
|
||||
}
|
||||
}
|
||||
|
||||
is ConeTypeParameterType -> {
|
||||
val bounds = lookupTag.typeParameterSymbol.resolvedBounds.map { it.coneType }
|
||||
runUnless(bounds.any { it.isNotSimpleFunctionalType(session) }) {
|
||||
bounds.find { it.findSubtypeOfSimpleFunctionalTypeImpl(session, expectedFunctionalType) != null }
|
||||
runUnless(bounds.any { it.isNotBasicFunctionType(session) }) {
|
||||
bounds.find { it.findSubtypeOfBasicFunctionTypeImpl(session, expectedFunctionType) != null }
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.isSubtypeOfFunctionalType(session: FirSession, expectedFunctionalType: ConeClassLikeType): Boolean {
|
||||
return AbstractTypeChecker.isSubtypeOf(session.typeContext, this, expectedFunctionalType.replaceArgumentsWithStarProjections())
|
||||
private fun ConeKotlinType.isSubtypeOfFunctionType(session: FirSession, expectedFunctionType: ConeClassLikeType): Boolean {
|
||||
return AbstractTypeChecker.isSubtypeOf(session.typeContext, this, expectedFunctionType.replaceArgumentsWithStarProjections())
|
||||
}
|
||||
|
||||
// ---------------------------------------------- functional type scope utils ----------------------------------------------
|
||||
// ---------------------------------------------- function type scope utils ----------------------------------------------
|
||||
|
||||
fun ConeClassLikeType.findBaseInvokeSymbol(session: FirSession, scopeSession: ScopeSession): FirNamedFunctionSymbol? {
|
||||
require(this.isSomeFunctionalType(session))
|
||||
require(this.isSomeFunctionType(session))
|
||||
val functionN = (lookupTag.toSymbol(session)?.fir as? FirClass) ?: return null
|
||||
var baseInvokeSymbol: FirNamedFunctionSymbol? = null
|
||||
functionN.unsubstitutedScope(
|
||||
@@ -192,10 +192,10 @@ fun ConeClassLikeType.findBaseInvokeSymbol(session: FirSession, scopeSession: Sc
|
||||
fun ConeKotlinType.findContributedInvokeSymbol(
|
||||
session: FirSession,
|
||||
scopeSession: ScopeSession,
|
||||
expectedFunctionalType: ConeClassLikeType,
|
||||
expectedFunctionType: ConeClassLikeType,
|
||||
shouldCalculateReturnTypesOfFakeOverrides: Boolean
|
||||
): FirFunctionSymbol<*>? {
|
||||
val baseInvokeSymbol = expectedFunctionalType.findBaseInvokeSymbol(session, scopeSession) ?: return null
|
||||
val baseInvokeSymbol = expectedFunctionType.findBaseInvokeSymbol(session, scopeSession) ?: return null
|
||||
|
||||
val fakeOverrideTypeCalculator = if (shouldCalculateReturnTypesOfFakeOverrides) {
|
||||
FakeOverrideTypeCalculator.Forced
|
||||
@@ -227,10 +227,10 @@ fun ConeKotlinType.findContributedInvokeSymbol(
|
||||
return if (overriddenInvoke != null) declaredInvoke else null
|
||||
}
|
||||
|
||||
// ---------------------------------------------- functional type type argument extraction ----------------------------------------------
|
||||
// ---------------------------------------------- function type type argument extraction ----------------------------------------------
|
||||
|
||||
fun ConeKotlinType.receiverType(session: FirSession): ConeKotlinType? {
|
||||
if (!isSomeFunctionalType(session) || !isExtensionFunctionType(session)) return null
|
||||
if (!isSomeFunctionType(session) || !isExtensionFunctionType(session)) return null
|
||||
return fullyExpandedType(session).let { expanded ->
|
||||
expanded.typeArguments[expanded.contextReceiversNumberForFunctionType].typeOrDefault(session.builtinTypes.nothingType.type)
|
||||
}
|
||||
@@ -255,8 +255,8 @@ private fun ConeTypeProjection.typeOrDefault(default: ConeKotlinType): ConeKotli
|
||||
|
||||
// ----------------- TODO fir utils
|
||||
|
||||
fun FirFunction.specialFunctionalTypeKind(session: FirSession): FunctionalTypeKind? {
|
||||
fun FirFunction.specialFunctionTypeKind(session: FirSession): FunctionTypeKind? {
|
||||
return (symbol as? FirNamedFunctionSymbol)?.let {
|
||||
session.functionalTypeService.extractSingleSpecialKindForFunction(it)
|
||||
session.functionTypeService.extractSingleSpecialKindForFunction(it)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user