JVM IR: add KotlinTypeMapperBase, do not use KotlinTypeMapper in metadata serialization
This commit is contained in:
+6
-2
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.codegen.coroutines.isResumeImplMethodName
|
||||
import org.jetbrains.kotlin.codegen.inline.coroutines.CoroutineTransformer
|
||||
import org.jetbrains.kotlin.codegen.inline.coroutines.FOR_INLINE_SUFFIX
|
||||
import org.jetbrains.kotlin.codegen.serialization.JvmCodegenStringTable
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapperBase
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
|
||||
import org.jetbrains.kotlin.load.kotlin.FileBasedKotlinClass
|
||||
import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
@@ -46,6 +47,9 @@ class AnonymousObjectTransformer(
|
||||
private lateinit var sourceMapper: SourceMapper
|
||||
private val languageVersionSettings = inliningContext.state.languageVersionSettings
|
||||
|
||||
// TODO: use IrTypeMapper in the IR backend
|
||||
private val typeMapper: KotlinTypeMapperBase = state.typeMapper
|
||||
|
||||
override fun doTransform(parentRemapper: FieldRemapper): InlineResult {
|
||||
val innerClassNodes = ArrayList<InnerClassNode>()
|
||||
val classBuilder = createRemappingClassBuilderViaFactory(inliningContext)
|
||||
@@ -226,7 +230,7 @@ class AnonymousObjectTransformer(
|
||||
when (header.kind) {
|
||||
KotlinClassHeader.Kind.CLASS -> {
|
||||
val (nameResolver, classProto) = JvmProtoBufUtil.readClassDataFrom(data, strings)
|
||||
val newStringTable = JvmCodegenStringTable(state.typeMapper, nameResolver)
|
||||
val newStringTable = JvmCodegenStringTable(typeMapper, nameResolver)
|
||||
val newProto = classProto.toBuilder().apply {
|
||||
setExtension(JvmProtoBuf.anonymousObjectOriginName, newStringTable.getStringIndex(oldObjectType.internalName))
|
||||
}.build()
|
||||
@@ -234,7 +238,7 @@ class AnonymousObjectTransformer(
|
||||
}
|
||||
KotlinClassHeader.Kind.SYNTHETIC_CLASS -> {
|
||||
val (nameResolver, functionProto) = JvmProtoBufUtil.readFunctionDataFrom(data, strings)
|
||||
val newStringTable = JvmCodegenStringTable(state.typeMapper, nameResolver)
|
||||
val newStringTable = JvmCodegenStringTable(typeMapper, nameResolver)
|
||||
val newProto = functionProto.toBuilder().apply {
|
||||
setExtension(JvmProtoBuf.lambdaClassOriginName, newStringTable.getStringIndex(oldObjectType.internalName))
|
||||
}.build()
|
||||
|
||||
+4
-6
@@ -5,7 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen.serialization
|
||||
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapperBase
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmNameResolver
|
||||
@@ -15,12 +15,11 @@ import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.serialization.DescriptorAwareStringTable
|
||||
|
||||
class JvmCodegenStringTable @JvmOverloads constructor(
|
||||
private val typeMapper: KotlinTypeMapper,
|
||||
private val typeMapper: KotlinTypeMapperBase,
|
||||
nameResolver: JvmNameResolver? = null
|
||||
) : JvmStringTable(nameResolver), DescriptorAwareStringTable {
|
||||
override fun getLocalClassIdReplacement(descriptor: ClassifierDescriptorWithTypeParameters): ClassId {
|
||||
val container = descriptor.containingDeclaration
|
||||
return when (container) {
|
||||
override fun getLocalClassIdReplacement(descriptor: ClassifierDescriptorWithTypeParameters): ClassId =
|
||||
when (val container = descriptor.containingDeclaration) {
|
||||
is ClassifierDescriptorWithTypeParameters -> getLocalClassIdReplacement(container).createNestedClassId(descriptor.name)
|
||||
is PackageFragmentDescriptor -> {
|
||||
throw IllegalStateException("getLocalClassIdReplacement should only be called for local classes: $descriptor")
|
||||
@@ -30,5 +29,4 @@ class JvmCodegenStringTable @JvmOverloads constructor(
|
||||
ClassId(fqName.parent(), FqName.topLevel(fqName.shortName()), true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.codegen.binding.CodegenBinding
|
||||
import org.jetbrains.kotlin.codegen.createFreeFakeLocalPropertyDescriptor
|
||||
import org.jetbrains.kotlin.codegen.serialization.JvmSerializationBindings.*
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapperBase
|
||||
import org.jetbrains.kotlin.config.ApiVersion
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -39,10 +40,13 @@ import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.commons.Method
|
||||
|
||||
class JvmSerializerExtension(private val bindings: JvmSerializationBindings, state: GenerationState) : SerializerExtension() {
|
||||
class JvmSerializerExtension @JvmOverloads constructor(
|
||||
private val bindings: JvmSerializationBindings,
|
||||
state: GenerationState,
|
||||
private val typeMapper: KotlinTypeMapperBase = state.typeMapper
|
||||
) : SerializerExtension() {
|
||||
private val globalBindings = state.globalSerializationBindings
|
||||
private val codegenBinding = state.bindingContext
|
||||
private val typeMapper = state.typeMapper
|
||||
override val stringTable = JvmCodegenStringTable(typeMapper)
|
||||
private val useTypeTable = state.useTypeTableInSerializer
|
||||
private val moduleName = state.moduleName
|
||||
|
||||
@@ -89,7 +89,7 @@ class KotlinTypeMapper @JvmOverloads constructor(
|
||||
private val isIrBackend: Boolean = false,
|
||||
private val typePreprocessor: ((KotlinType) -> KotlinType?)? = null,
|
||||
private val namePreprocessor: ((ClassDescriptor) -> String?)? = null
|
||||
) {
|
||||
): KotlinTypeMapperBase() {
|
||||
private val isReleaseCoroutines = languageVersionSettings.supportsFeature(LanguageFeature.ReleaseCoroutines)
|
||||
|
||||
private val typeMappingConfiguration = object : TypeMappingConfiguration<Type> {
|
||||
@@ -224,7 +224,7 @@ class KotlinTypeMapper @JvmOverloads constructor(
|
||||
return mapType(type, signatureVisitor, TypeMappingMode.GENERIC_ARGUMENT)
|
||||
}
|
||||
|
||||
fun mapClass(classifier: ClassifierDescriptor): Type {
|
||||
override fun mapClass(classifier: ClassifierDescriptor): Type {
|
||||
return mapType(classifier.defaultType, null, TypeMappingMode.CLASS_DECLARATION)
|
||||
}
|
||||
|
||||
@@ -274,10 +274,6 @@ class KotlinTypeMapper @JvmOverloads constructor(
|
||||
return mapInlineClassType(kotlinType, TypeMappingMode.DEFAULT, typeMappingConfiguration)
|
||||
}
|
||||
|
||||
fun mapDefaultImpls(descriptor: ClassDescriptor): Type {
|
||||
return Type.getObjectType(mapType(descriptor).internalName + JvmAbi.DEFAULT_IMPLS_SUFFIX)
|
||||
}
|
||||
|
||||
private fun writeGenericType(
|
||||
type: KotlinType,
|
||||
asmType: Type,
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.codegen.state
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
|
||||
abstract class KotlinTypeMapperBase {
|
||||
abstract fun mapClass(classifier: ClassifierDescriptor): Type
|
||||
|
||||
fun mapDefaultImpls(descriptor: ClassDescriptor): Type =
|
||||
Type.getObjectType(mapClass(descriptor).internalName + JvmAbi.DEFAULT_IMPLS_SUFFIX)
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.InlineClassAbi
|
||||
import org.jetbrains.kotlin.codegen.ClassBuilder
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
@@ -24,6 +25,7 @@ import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrLocalDelegatedPropertySymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
@@ -92,6 +94,9 @@ class JvmBackendContext(
|
||||
internal fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol =
|
||||
symbolTable.referenceClass(descriptor)
|
||||
|
||||
internal fun referenceTypeParameter(descriptor: TypeParameterDescriptor): IrTypeParameterSymbol =
|
||||
symbolTable.referenceTypeParameter(descriptor)
|
||||
|
||||
override fun log(message: () -> String) {
|
||||
/*TODO*/
|
||||
if (inVerbosePhase) {
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@ open class ClassCodegen protected constructor(
|
||||
|
||||
private var sourceMapper: DefaultSourceMapper? = null
|
||||
|
||||
private val serializerExtension = JvmSerializerExtension(visitor.serializationBindings, state)
|
||||
private val serializerExtension = JvmSerializerExtension(visitor.serializationBindings, state, typeMapper)
|
||||
private val serializer: DescriptorSerializer? =
|
||||
when (val metadata = irClass.metadata) {
|
||||
is MetadataSource.Class -> DescriptorSerializer.create(metadata.descriptor, serializerExtension, parentClassCodegen?.serializer)
|
||||
|
||||
+15
-1
@@ -12,6 +12,10 @@ import org.jetbrains.kotlin.codegen.JvmCodegenUtil
|
||||
import org.jetbrains.kotlin.codegen.signature.AsmTypeFactory
|
||||
import org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
|
||||
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapperBase
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
|
||||
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
|
||||
@@ -29,9 +33,19 @@ import org.jetbrains.kotlin.resolve.jvm.AsmTypes
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
|
||||
class IrTypeMapper(private val context: JvmBackendContext) {
|
||||
class IrTypeMapper(private val context: JvmBackendContext) : KotlinTypeMapperBase() {
|
||||
internal val typeSystem = IrTypeCheckerContext(context.irBuiltIns)
|
||||
|
||||
override fun mapClass(classifier: ClassifierDescriptor): Type =
|
||||
when (classifier) {
|
||||
is ClassDescriptor ->
|
||||
mapClass(context.referenceClass(classifier).owner)
|
||||
is TypeParameterDescriptor ->
|
||||
mapType(context.referenceTypeParameter(classifier).owner.defaultType)
|
||||
else ->
|
||||
error("Unknown descriptor: $classifier")
|
||||
}
|
||||
|
||||
fun classInternalName(irClass: IrClass): String =
|
||||
context.getLocalClassInfo(irClass)?.internalName
|
||||
?: JvmCodegenUtil.sanitizeNameIfNeeded(computeInternalName(irClass), context.state.languageVersionSettings)
|
||||
|
||||
Reference in New Issue
Block a user