Introduce support for plugin-defined intrinsics in JVM IR backend:
Add intrinsic for kotlinx.serialization.serializer<T>() function. Plugin intrinsic for old backend is removed because it is too hard and unjustifiable to unify them.
This commit is contained in:
@@ -10,6 +10,7 @@ dependencies {
|
||||
api(project(":compiler:ir.tree"))
|
||||
api(project(":compiler:ir.interpreter"))
|
||||
compileOnly(intellijCore())
|
||||
compileOnly(commonDependency("org.jetbrains.intellij.deps:asm-all"))
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
||||
+17
@@ -5,9 +5,15 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.common.extensions
|
||||
|
||||
import org.jetbrains.org.objectweb.asm.tree.InsnList
|
||||
import org.jetbrains.org.objectweb.asm.tree.MethodInsnNode
|
||||
import org.jetbrains.kotlin.backend.common.BackendContext
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
|
||||
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
|
||||
interface IrGenerationExtension : IrDeserializer.IrLinkerExtension {
|
||||
companion object :
|
||||
@@ -16,4 +22,15 @@ interface IrGenerationExtension : IrDeserializer.IrLinkerExtension {
|
||||
)
|
||||
|
||||
fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext)
|
||||
|
||||
// TODO: normal dependency & typing
|
||||
fun retrieveIntrinsic(symbol: IrFunctionSymbol): Any? = null
|
||||
|
||||
|
||||
fun applyPluginDefinedReifiedOperationMarker(
|
||||
insn: MethodInsnNode,
|
||||
instructions: InsnList,
|
||||
type: IrType,
|
||||
jvmBackendContext: BackendContext,
|
||||
): Int = -1
|
||||
}
|
||||
|
||||
+24
@@ -5,6 +5,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.jvm.codegen
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.backend.jvm.intrinsics.SignatureString
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.getCallableReferenceOwnerKClassType
|
||||
import org.jetbrains.kotlin.backend.jvm.ir.getCallableReferenceTopLevelFlag
|
||||
@@ -12,12 +14,14 @@ import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.descriptors.toIrBasedKotlinType
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classOrNull
|
||||
import org.jetbrains.kotlin.ir.types.toKotlinType
|
||||
import org.jetbrains.kotlin.ir.util.allParametersCount
|
||||
import org.jetbrains.kotlin.ir.util.defaultType
|
||||
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
|
||||
@@ -31,6 +35,8 @@ import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.Type.INT_TYPE
|
||||
import org.jetbrains.org.objectweb.asm.Type.VOID_TYPE
|
||||
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
||||
import org.jetbrains.org.objectweb.asm.tree.InsnList
|
||||
import org.jetbrains.org.objectweb.asm.tree.MethodInsnNode
|
||||
|
||||
class IrInlineIntrinsicsSupport(
|
||||
private val classCodegen: ClassCodegen,
|
||||
@@ -120,4 +126,22 @@ class IrInlineIntrinsicsSupport(
|
||||
classCodegen.context.ktDiagnosticReporter.at(reportErrorsOn, containingFile)
|
||||
.report(JvmBackendErrors.TYPEOF_NON_REIFIED_TYPE_PARAMETER_WITH_RECURSIVE_BOUND, typeParameterName.asString())
|
||||
}
|
||||
|
||||
override fun applyPluginDefinedReifiedOperationMarker(
|
||||
insn: MethodInsnNode,
|
||||
instructions: InsnList,
|
||||
type: IrType,
|
||||
asmType: Type
|
||||
): Int {
|
||||
return IrGenerationExtension.getInstances(classCodegen.context.state.project)
|
||||
.map {
|
||||
it.applyPluginDefinedReifiedOperationMarker(
|
||||
insn,
|
||||
instructions,
|
||||
type,
|
||||
classCodegen.context
|
||||
)
|
||||
}
|
||||
.maxOrNull() ?: -1
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -286,8 +286,10 @@ open class JvmIrCodegenFactory(
|
||||
if (evaluatorFragmentInfoForPsi2Ir != null) {
|
||||
context.localDeclarationsLoweringData = mutableMapOf()
|
||||
}
|
||||
// todo: pass it here
|
||||
val generationExtensions = IrGenerationExtension.getInstances(state.project)
|
||||
val intrinsics by lazy { IrIntrinsicMethods(irModuleFragment.irBuiltins, context.ir.symbols) }
|
||||
context.getIntrinsic = { symbol: IrFunctionSymbol -> intrinsics.getIntrinsic(symbol) }
|
||||
context.getIntrinsic = { symbol: IrFunctionSymbol -> intrinsics.getIntrinsic(symbol) ?: generationExtensions.firstNotNullOfOrNull { it.retrieveIntrinsic(symbol) as? IntrinsicMarker } }
|
||||
/* JvmBackendContext creates new unbound symbols, have to resolve them. */
|
||||
ExternalDependenciesGenerator(symbolTable, irProviders).generateUnboundSymbolsAsDependencies()
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ class JvmBackendContext(
|
||||
}
|
||||
}
|
||||
|
||||
internal fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol =
|
||||
fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol =
|
||||
symbolTable.lazyWrapper.referenceClass(descriptor)
|
||||
|
||||
internal fun referenceTypeParameter(descriptor: TypeParameterDescriptor): IrTypeParameterSymbol =
|
||||
|
||||
Reference in New Issue
Block a user