[FE] Completely replace FunctionClassKind with FunctionalTypeKind

FunctionalTypeKind can be used in FE 1.0 too, so there is no need to
  keep both classes. Also, removal of FunctionClassKind simplifies work
  with FunctionalTypeKind in common code, like Analysis Api
This commit is contained in:
Dmitriy Novozhilov
2023-01-20 14:54:33 +02:00
committed by Space Team
parent 87a9103cc6
commit 67aa80562d
38 changed files with 242 additions and 262 deletions
@@ -6,7 +6,7 @@
package org.jetbrains.kotlin.builtins
import org.jetbrains.kotlin.builtins.StandardNames.FqNames.reflect
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
import org.jetbrains.kotlin.builtins.functions.FunctionalTypeKind
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
@@ -252,7 +252,7 @@ object StandardNames {
@JvmStatic
fun getKFunctionFqName(parameterCount: Int): FqNameUnsafe {
return reflect(FunctionClassKind.KFunction.classNamePrefix + parameterCount)
return reflect(FunctionalTypeKind.KFunction.classNamePrefix + parameterCount)
}
@JvmStatic
@@ -263,7 +263,7 @@ object StandardNames {
@JvmStatic
fun getSuspendFunctionName(parameterCount: Int): String {
return FunctionClassKind.SuspendFunction.classNamePrefix + parameterCount
return FunctionalTypeKind.SuspendFunction.classNamePrefix + parameterCount
}
@JvmStatic
@@ -273,7 +273,7 @@ object StandardNames {
@JvmStatic
fun getKSuspendFunctionName(parameterCount: Int): FqNameUnsafe {
return reflect(FunctionClassKind.KSuspendFunction.classNamePrefix + parameterCount)
return reflect(FunctionalTypeKind.KSuspendFunction.classNamePrefix + parameterCount)
}
@JvmStatic
@@ -1,62 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.builtins.functions
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
enum class FunctionClassKind(
val packageFqName: FqName,
val classNamePrefix: String,
val isSuspendType: Boolean,
val isReflectType: Boolean
) {
Function(StandardNames.BUILT_INS_PACKAGE_FQ_NAME, "Function", isSuspendType = false, isReflectType = false),
SuspendFunction(StandardNames.COROUTINES_PACKAGE_FQ_NAME, "SuspendFunction", isSuspendType = true, isReflectType = false),
KFunction(StandardNames.KOTLIN_REFLECT_FQ_NAME, "KFunction", isSuspendType = false, isReflectType = true),
KSuspendFunction(StandardNames.KOTLIN_REFLECT_FQ_NAME, "KSuspendFunction", isSuspendType = true, isReflectType = true);
fun numberedClassName(arity: Int) = Name.identifier("$classNamePrefix$arity")
companion object {
fun byClassNamePrefix(packageFqName: FqName, className: String): FunctionClassKind? =
values().firstOrNull { it.packageFqName == packageFqName && className.startsWith(it.classNamePrefix) }
data class KindWithArity(val kind: FunctionClassKind, val arity: Int)
fun parseClassName(className: String, packageFqName: FqName): KindWithArity? {
val kind = byClassNamePrefix(packageFqName, className) ?: return null
val prefix = kind.classNamePrefix
val arity = toInt(className.substring(prefix.length)) ?: return null
// TODO: validate arity, should be <= 255
return KindWithArity(kind, arity)
}
@JvmStatic
fun getFunctionalClassKind(className: String, packageFqName: FqName) =
parseClassName(className, packageFqName)?.kind
fun hasArityAtTheEnd(prefix: String, className: String): Boolean {
return toInt(className.substring(prefix.length)) != null
}
private fun toInt(s: String): Int? {
if (s.isEmpty()) return null
var result = 0
for (c in s) {
val d = c - '0'
if (d !in 0..9) return null
result = result * 10 + d
}
return result
}
}
}
@@ -0,0 +1,145 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.builtins.functions
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
/**
* [FunctionalTypeKind] describes a family of various similar functional types (like kotlin.FunctionN)
* All types in the same family have corresponding shape of classId: [packageFqName].[classNamePrefix]N,
* where `N` is an arity of function
*
* Functional type kind may be either reflect type or non-reflect type
* Each non-reflect kind should have corresponding reflect kind and vice-versa (like `kotlin.FunctionN` and `kotlin.reflection.KFunctionN`)
*
* Classes for such functional types are synthetic and generated by compiler itself
* [annotationOnInvokeClassId] is a classId of annotation, which will be added to `invoke` function of generated
* functional interface. This argument is mandatory for non-standard functional kinds
*
* Assume we have `some.CustomFunctionN` and `some.KCustomFunctionN` families with [annotationOnInvokeClassId] = /some.Anno
* Classes for those kinds will look like this:
*
* interface CustomFunctionN<P1, P2, ..., PN, R> : kotlin.Function<R> {
* @some.Anno
* operator fun invoke(p1: P1, p2: P2, ..., pN: PN): R
* }
*
* interface KCustomFunctionN<P1, P2, ..., PN, R> : kotlin.reflect.KFunction<R>, some.CustomFunctionN<P1, P2, ..., PN, R> {
* @some.Anno
* override operator fun invoke(p1: P1, p2: P2, ..., pN: PN): R
* }
*
* Note that if you provide some new functional type kind it's your responsibility to handle all references to it in backend
* with [IrGenerationExtension] implementation
*/
abstract class FunctionalTypeKind internal constructor(
val packageFqName: FqName,
val classNamePrefix: String,
val isReflectType: Boolean,
val annotationOnInvokeClassId: ClassId?
) {
/*
* This constructor is needed to enforce not nullable [annotationOnInvokeClassId] for
* functional kinds provided by compiler plugins
*/
constructor(
packageFqName: FqName,
classNamePrefix: String,
annotationOnInvokeClassId: ClassId,
isReflectType: Boolean
) : this(packageFqName, classNamePrefix, isReflectType, annotationOnInvokeClassId)
/*
* Specifies how corresponding type will be rendered
* E.g. if `prefixForTypeRender = @Some` and type is `some.CustomFunction2<Int, String, Double>`,
* then type will be rendered as `@Some (Int, String) -> Double`
*/
open val prefixForTypeRender: String?
get() = null
/**
* @return corresponding non-reflect kind for reflect kind
* @return [this] if [isReflectType] is false
*
* Should be overridden for reflect kinds
*/
open fun nonReflectKind(): FunctionalTypeKind {
return if (isReflectType) error("Should be overridden explicitly") else this
}
/**
* @return corresponding reflect kind for non-reflect kind
* @return [this] if [isReflectType] is true
*
* Should be overridden for non reflect kinds
*/
open fun reflectKind(): FunctionalTypeKind {
return if (isReflectType) this else error("Should be overridden explicitly")
}
fun numberedClassName(arity: Int): Name = Name.identifier("$classNamePrefix$arity")
override fun toString(): String {
return "$packageFqName.${classNamePrefix}N"
}
// ------------------------------------------- Builtin functional kinds -------------------------------------------
object Function : FunctionalTypeKind(
StandardNames.BUILT_INS_PACKAGE_FQ_NAME,
"Function",
isReflectType = false,
annotationOnInvokeClassId = null
) {
override fun reflectKind(): FunctionalTypeKind = KFunction
}
object SuspendFunction : FunctionalTypeKind(
StandardNames.COROUTINES_PACKAGE_FQ_NAME,
"SuspendFunction",
isReflectType = false,
annotationOnInvokeClassId = null
) {
override val prefixForTypeRender: String
get() = "suspend"
override fun reflectKind(): FunctionalTypeKind = KSuspendFunction
}
object KFunction : FunctionalTypeKind(
StandardNames.KOTLIN_REFLECT_FQ_NAME,
"KFunction",
isReflectType = true,
annotationOnInvokeClassId = null
) {
override fun nonReflectKind(): FunctionalTypeKind = Function
}
object KSuspendFunction : FunctionalTypeKind(
StandardNames.KOTLIN_REFLECT_FQ_NAME,
"KSuspendFunction",
isReflectType = true,
annotationOnInvokeClassId = null
) {
override fun nonReflectKind(): FunctionalTypeKind = SuspendFunction
}
}
val FunctionalTypeKind.isBuiltin: Boolean
get() = when (this) {
FunctionalTypeKind.Function,
FunctionalTypeKind.SuspendFunction,
FunctionalTypeKind.KFunction,
FunctionalTypeKind.KSuspendFunction -> true
else -> false
}
val FunctionalTypeKind.isSuspendType: Boolean
get() = this == FunctionalTypeKind.SuspendFunction || this == FunctionalTypeKind.KSuspendFunction
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.builtins.functions
import org.jetbrains.kotlin.name.FqName
@RequiresOptIn
annotation class AllowedToUsedOnlyInK1
class FunctionalTypeKindExtractor(kinds: List<FunctionalTypeKind>) {
companion object {
/**
* This instance should be used only in:
* - FE 1.0, since it does not support custom functional kinds from plugins
* - places in FIR where session is not accessible by design (like in renderer of FIR elements)
*/
@JvmStatic
@AllowedToUsedOnlyInK1
val Default = FunctionalTypeKindExtractor(
listOf(
FunctionalTypeKind.Function,
FunctionalTypeKind.SuspendFunction,
FunctionalTypeKind.KFunction,
FunctionalTypeKind.KSuspendFunction,
)
)
}
private val knownKindsByPackageFqName = kinds.groupBy { it.packageFqName }
fun getFunctionalClassKind(packageFqName: FqName, className: String): FunctionalTypeKind? {
return getFunctionalClassKindWithArity(packageFqName, className)?.kind
}
fun getFunctionalClassKindWithArity(packageFqName: FqName, className: String): KindWithArity? {
val kinds = knownKindsByPackageFqName[packageFqName] ?: return null
for (kind in kinds) {
if (!className.startsWith(kind.classNamePrefix)) continue
val arity = toInt(className.substring(kind.classNamePrefix.length)) ?: continue
return KindWithArity(kind, arity)
}
return null
}
fun hasKindWithSpecificPackage(packageFqName: FqName): Boolean {
return packageFqName in knownKindsByPackageFqName
}
data class KindWithArity(val kind: FunctionalTypeKind, val arity: Int)
private fun toInt(s: String): Int? {
if (s.isEmpty()) return null
var result = 0
for (c in s) {
val d = c - '0'
if (d !in 0..9) return null
result = result * 10 + d
}
return result
}
}