Kapt: use descriptorBasedTypeMapping for type mapping

Since all anonymous types are approximated to a supertype in kapt, type
mapping doesn't require backend information (mapping of anonymous types
to class names) and can be performed statically, independently from
particular JVM backend internals, via descriptorBasedTypeMapping.

 #KT-49682
This commit is contained in:
Alexander Udalov
2021-12-08 23:06:06 +01:00
parent 754ad100a9
commit bcd221f714
11 changed files with 41 additions and 25 deletions
@@ -56,12 +56,8 @@ import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument
import org.jetbrains.kotlin.resolve.constants.*
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
import org.jetbrains.kotlin.resolve.descriptorUtil.*
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
@@ -123,8 +119,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
val bindings: Map<String, KaptJavaFileObject>
get() = mutableBindings
private val typeMapper
get() = kaptContext.generationState.typeMapper
private val typeMapper = KaptTypeMapper
val treeMaker = TreeMaker.instance(kaptContext.context) as KaptTreeMaker
@@ -510,7 +505,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
val declaration = kaptContext.origins[clazz]?.element as? KtClassOrObject ?: return defaultSuperTypes
val declarationDescriptor = kaptContext.bindingContext[BindingContext.CLASS, declaration] ?: return defaultSuperTypes
if (typeMapper.mapType(declarationDescriptor) != Type.getObjectType(clazz.name)) {
if (typeMapper.mapType(declarationDescriptor.defaultType) != Type.getObjectType(clazz.name)) {
return defaultSuperTypes
}
@@ -1494,7 +1489,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
}
}
private fun convertKotlinType(type: KotlinType): Type = typeMapper.mapType(type, null, TypeMappingMode.GENERIC_ARGUMENT)
private fun convertKotlinType(type: KotlinType): Type = typeMapper.mapType(type, TypeMappingMode.GENERIC_ARGUMENT)
private fun getFileForClass(c: ClassNode): KtFile? = kaptContext.origins[c]?.element?.containingFile as? KtFile
@@ -83,7 +83,7 @@ class ErrorTypeCorrector(
private fun convert(type: SimpleType): JCTree.JCExpression {
// TODO now the raw Java type is returned. In future we need to properly convert all type parameters
return treeMaker.Type(converter.kaptContext.generationState.typeMapper.mapType(type))
return treeMaker.Type(KaptTypeMapper.mapType(type))
}
private fun convertUserType(type: KtUserType, substitutions: SubstitutionMap): JCTree.JCExpression {
@@ -98,16 +98,14 @@ class ErrorTypeCorrector(
return convert(actualType, typeAlias.getSubstitutions(type))
}
is ClassConstructorDescriptor -> {
val asmType = converter.kaptContext.generationState.typeMapper
.mapType(target.constructedClass.defaultType, null, TypeMappingMode.GENERIC_ARGUMENT)
val asmType = KaptTypeMapper.mapType(target.constructedClass.defaultType, TypeMappingMode.GENERIC_ARGUMENT)
baseExpression = converter.treeMaker.Type(asmType)
}
is ClassDescriptor -> {
// We only get here if some type were an error type. In other words, 'type' is either an error type or its argument,
// so it's impossible it to be unboxed primitive.
val asmType = converter.kaptContext.generationState.typeMapper
.mapType(target.defaultType, null, TypeMappingMode.GENERIC_ARGUMENT)
val asmType = KaptTypeMapper.mapType(target.defaultType, TypeMappingMode.GENERIC_ARGUMENT)
baseExpression = converter.treeMaker.Type(asmType)
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2021 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.kapt3.stubs
import org.jetbrains.kotlin.codegen.signature.AsmTypeFactory
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.load.kotlin.TypeMappingConfiguration
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
import org.jetbrains.kotlin.load.kotlin.mapType
import org.jetbrains.kotlin.types.CommonSupertypes
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.org.objectweb.asm.Type
internal object KaptTypeMapper {
private val configuration = object : TypeMappingConfiguration<Type> {
override fun commonSupertype(types: Collection<KotlinType>): KotlinType =
CommonSupertypes.commonSupertype(types)
override fun getPredefinedTypeForClass(classDescriptor: ClassDescriptor): Type? = null
override fun getPredefinedInternalNameForClass(classDescriptor: ClassDescriptor): String? = null
override fun processErrorType(kotlinType: KotlinType, descriptor: ClassDescriptor) {
}
override fun preprocessType(kotlinType: KotlinType): KotlinType? = null
}
fun mapType(type: KotlinType, mode: TypeMappingMode = TypeMappingMode.DEFAULT): Type =
mapType(type, AsmTypeFactory, mode, configuration, null)
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// STRICT
//FILE: test/ClassRefAnnotation.java
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// CORRECT_ERROR_TYPES
// STRICT
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// WITH_STDLIB
@file:JvmName("FacadeName")
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class T : Runnable {
@Override
public override fun run() {}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// FILE: androidx/annotation/RecentlyNullable.java
package androidx.annotation;
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// DUMP_DEFAULT_PARAMETER_VALUES
package test
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class Subject {
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
package test
internal class Simple {