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:
+4
-9
@@ -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.calls.model.ResolvedValueArgument
|
||||||
import org.jetbrains.kotlin.resolve.constants.*
|
import org.jetbrains.kotlin.resolve.constants.*
|
||||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
|
import org.jetbrains.kotlin.resolve.descriptorUtil.*
|
||||||
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.jvm.diagnostics.JvmDeclarationOrigin
|
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.resolve.source.getPsi
|
||||||
import org.jetbrains.kotlin.types.ErrorUtils
|
import org.jetbrains.kotlin.types.ErrorUtils
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
@@ -123,8 +119,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
|
|||||||
val bindings: Map<String, KaptJavaFileObject>
|
val bindings: Map<String, KaptJavaFileObject>
|
||||||
get() = mutableBindings
|
get() = mutableBindings
|
||||||
|
|
||||||
private val typeMapper
|
private val typeMapper = KaptTypeMapper
|
||||||
get() = kaptContext.generationState.typeMapper
|
|
||||||
|
|
||||||
val treeMaker = TreeMaker.instance(kaptContext.context) as KaptTreeMaker
|
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 declaration = kaptContext.origins[clazz]?.element as? KtClassOrObject ?: return defaultSuperTypes
|
||||||
val declarationDescriptor = kaptContext.bindingContext[BindingContext.CLASS, declaration] ?: 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
|
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
|
private fun getFileForClass(c: ClassNode): KtFile? = kaptContext.origins[c]?.element?.containingFile as? KtFile
|
||||||
|
|
||||||
|
|||||||
+3
-5
@@ -83,7 +83,7 @@ class ErrorTypeCorrector(
|
|||||||
|
|
||||||
private fun convert(type: SimpleType): JCTree.JCExpression {
|
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
|
// 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 {
|
private fun convertUserType(type: KtUserType, substitutions: SubstitutionMap): JCTree.JCExpression {
|
||||||
@@ -98,16 +98,14 @@ class ErrorTypeCorrector(
|
|||||||
return convert(actualType, typeAlias.getSubstitutions(type))
|
return convert(actualType, typeAlias.getSubstitutions(type))
|
||||||
}
|
}
|
||||||
is ClassConstructorDescriptor -> {
|
is ClassConstructorDescriptor -> {
|
||||||
val asmType = converter.kaptContext.generationState.typeMapper
|
val asmType = KaptTypeMapper.mapType(target.constructedClass.defaultType, TypeMappingMode.GENERIC_ARGUMENT)
|
||||||
.mapType(target.constructedClass.defaultType, null, TypeMappingMode.GENERIC_ARGUMENT)
|
|
||||||
|
|
||||||
baseExpression = converter.treeMaker.Type(asmType)
|
baseExpression = converter.treeMaker.Type(asmType)
|
||||||
}
|
}
|
||||||
is ClassDescriptor -> {
|
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,
|
// 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.
|
// so it's impossible it to be unboxed primitive.
|
||||||
val asmType = converter.kaptContext.generationState.typeMapper
|
val asmType = KaptTypeMapper.mapType(target.defaultType, TypeMappingMode.GENERIC_ARGUMENT)
|
||||||
.mapType(target.defaultType, null, TypeMappingMode.GENERIC_ARGUMENT)
|
|
||||||
|
|
||||||
baseExpression = converter.treeMaker.Type(asmType)
|
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
|
// STRICT
|
||||||
|
|
||||||
//FILE: test/ClassRefAnnotation.java
|
//FILE: test/ClassRefAnnotation.java
|
||||||
|
|||||||
-1
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// CORRECT_ERROR_TYPES
|
// CORRECT_ERROR_TYPES
|
||||||
// STRICT
|
// STRICT
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// WITH_STDLIB
|
// WITH_STDLIB
|
||||||
|
|
||||||
@file:JvmName("FacadeName")
|
@file:JvmName("FacadeName")
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
class T : Runnable {
|
class T : Runnable {
|
||||||
@Override
|
@Override
|
||||||
public override fun run() {}
|
public override fun run() {}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// FILE: androidx/annotation/RecentlyNullable.java
|
// FILE: androidx/annotation/RecentlyNullable.java
|
||||||
package androidx.annotation;
|
package androidx.annotation;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// DUMP_DEFAULT_PARAMETER_VALUES
|
// DUMP_DEFAULT_PARAMETER_VALUES
|
||||||
|
|
||||||
package test
|
package test
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
class Subject {
|
class Subject {
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
package test
|
package test
|
||||||
|
|
||||||
internal class Simple {
|
internal class Simple {
|
||||||
|
|||||||
Reference in New Issue
Block a user