Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to removal of old JVM backend #2255

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import com.intellij.psi.impl.source.PsiClassReferenceType
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.codegen.ClassBuilderMode
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
import org.jetbrains.kotlin.container.ComponentProvider
Expand All @@ -66,9 +66,11 @@ import org.jetbrains.kotlin.container.tryGetService
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.load.java.components.TypeUsage
import org.jetbrains.kotlin.load.java.descriptors.JavaForKotlinOverridePropertyDescriptor
import org.jetbrains.kotlin.load.java.descriptors.getImplClassNameForDeserialized
import org.jetbrains.kotlin.load.java.lazy.*
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaTypeParameterDescriptor
import org.jetbrains.kotlin.load.java.lazy.types.JavaTypeResolver
Expand Down Expand Up @@ -106,13 +108,15 @@ import org.jetbrains.kotlin.resolve.multiplatform.findExpects
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DescriptorWithContainerSource
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
import org.jetbrains.kotlin.types.typeUtil.substitute
import org.jetbrains.kotlin.types.typeUtil.supertypes
import org.jetbrains.kotlin.util.containingNonLocalDeclaration
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type
import java.io.File
import java.util.*

Expand Down Expand Up @@ -356,20 +360,28 @@ class ResolverImpl(
override fun mapToJvmSignature(declaration: KSDeclaration): String? = mapToJvmSignatureInternal(declaration)

internal fun mapToJvmSignatureInternal(declaration: KSDeclaration): String? = when (declaration) {
is KSClassDeclaration -> resolveClassDeclaration(declaration)?.let { typeMapper.mapType(it).descriptor }
is KSClassDeclaration -> resolveClassDeclaration(declaration)?.let {
typeMapper.mapType(it.defaultType).descriptor
}
is KSFunctionDeclaration -> resolveFunctionDeclaration(declaration)?.let {
when (it) {
is FunctionDescriptor -> typeMapper.mapAsmMethod(it).descriptor
is PropertyDescriptor -> typeMapper.mapFieldSignature(it.type, it) ?: typeMapper.mapType(it).descriptor
is PropertyDescriptor -> typeMapper.mapPropertySignature(it)
else -> throw IllegalStateException("Unexpected descriptor type for declaration: $declaration")
}
}
is KSPropertyDeclaration -> resolvePropertyDeclaration(declaration)?.let {
typeMapper.mapFieldSignature(it.type, it) ?: typeMapper.mapType(it).descriptor
typeMapper.mapPropertySignature(it)
}
else -> null
}

private fun KotlinTypeMapper.mapPropertySignature(descriptor: PropertyDescriptor): String? {
val sw = BothSignatureWriter(BothSignatureWriter.Mode.TYPE)
writeFieldSignature(descriptor.type, descriptor, sw)
return sw.makeJavaGenericSignature() ?: mapType(descriptor.type).descriptor
}

override fun overrides(overrider: KSDeclaration, overridee: KSDeclaration): Boolean {
fun resolveForOverride(declaration: KSDeclaration): DeclarationDescriptor? {
return when (declaration) {
Expand Down Expand Up @@ -880,22 +892,13 @@ class ResolverImpl(

@KspExperimental
override fun getJvmName(accessor: KSPropertyAccessor): String? {
val descriptor = resolvePropertyAccessorDeclaration(accessor)

return descriptor?.let {
// KotlinTypeMapper.mapSignature always uses OwnerKind.IMPLEMENTATION
typeMapper.mapFunctionName(descriptor, OwnerKind.IMPLEMENTATION)
}
return resolvePropertyAccessorDeclaration(accessor)?.let(typeMapper::mapFunctionName)
}

@KspExperimental
override fun getJvmName(declaration: KSFunctionDeclaration): String? {
// function names might be mangled if they receive inline class parameters or they are internal
val descriptor = resolveFunctionDeclaration(declaration)
return (descriptor as? FunctionDescriptor)?.let {
// KotlinTypeMapper.mapSignature always uses OwnerKind.IMPLEMENTATION
typeMapper.mapFunctionName(it, OwnerKind.IMPLEMENTATION)
}
return (resolveFunctionDeclaration(declaration) as? FunctionDescriptor)?.let(typeMapper::mapFunctionName)
}

@KspExperimental
Expand All @@ -911,11 +914,35 @@ class ResolverImpl(
}

private fun getJvmOwnerQualifiedName(descriptor: DeclarationDescriptor): String? {
return try {
typeMapper.mapImplementationOwner(descriptor).className
} catch (unsupported: UnsupportedOperationException) {
null
return typeMapper.mapJvmImplementationOwner(descriptor)?.className
}

private fun KotlinTypeMapper.mapJvmImplementationOwner(descriptor: DeclarationDescriptor): Type? {
if (descriptor is ConstructorDescriptor) {
return mapClass(descriptor.constructedClass)
}

return when (val container = descriptor.containingDeclaration) {
is PackageFragmentDescriptor ->
internalNameForPackageMemberOwner(descriptor as CallableMemberDescriptor)?.let(Type::getObjectType)
is ClassDescriptor ->
mapClass(container)
else -> null
}
}

private fun internalNameForPackageMemberOwner(descriptor: CallableMemberDescriptor): String? {
val file = DescriptorToSourceUtils.getContainingFile(descriptor)
if (file != null) {
return JvmFileClassUtil.getFileClassInternalName(file)
}

val directMember = DescriptorUtils.getDirectMember(descriptor)
if (directMember is DescriptorWithContainerSource) {
return directMember.getImplClassNameForDeserialized()?.internalName
}

return null
}

// TODO: refactor and reuse BinaryClassInfoCache
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copied from kotlinc
org.gradle.jvmargs=-Duser.country=US -Dkotlin.daemon.jvm.options=-Xmx4096m -Dfile.encoding=UTF-8

kotlinBaseVersion=2.1.20-dev-3305
kotlinBaseVersion=2.1.20-dev-5634
agpBaseVersion=7.3.1
agpTestVersion=8.7.1
intellijVersion=233.13135.128
Expand Down
Loading